repo: jaccovanschaik/libjvs

decompiled_code:
void __fastcall v3Set(double *a1, double a2, double a3, double a4)
{
*a1 = a2;
a1[1] = a3;
a1[2] = a4;
}

canonical_decompiled_code:
void __fastcall func2(double *a1, double a2, double a3, double a4)
{
*a1 = a2;
a1[1] = a3;
a1[2] = a4;
}

original_code:
void v3Set(Vector3 *v, double x, double y, double z)
{
    v->r[0] = x;
    v->r[1] = y;
    v->r[2] = z;
}

canonical_original_code:
void v3Set(struct Vector3 *v, double x, double y, double z)
{
    v->r[0] = x;
    v->r[1] = y;
    v->r[2] = z;
}

variable_types:
v
struct Vector3 { double r[3]; } *

variable_types: x double

variable_types: y double

variable_types: z double


decompiled_code:
void __fastcall v3Add(double *a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12)
{
*a1 = *a1 + a10;
a1[1] = a1[1] + a11;
a1[2] = a1[2] + a12;
}

canonical_decompiled_code:
void __fastcall func4(double *a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12)
{
*a1 = *a1 + a10;
a1[1] = a1[1] + a11;
a1[2] = a1[2] + a12;
}

canonical_original_code:
void v3Add(struct Vector3 *v, struct Vector3 d)
{
    v->r[0] += d.r[0];
    v->r[1] += d.r[1];
    v->r[2] += d.r[2];
}

variable_types:
d
struct Vector3

return_type: void


decompiled_code:
void __fastcall v3Sub(double *a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12)
{
*a1 = *a1 - a10;
a1[1] = a1[1] - a11;
a1[2] = a1[2] - a12;
}

canonical_decompiled_code:
void __fastcall func6(double *a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12)
{
*a1 = *a1 - a10;
a1[1] = a1[1] - a11;
a1[2] = a1[2] - a12;
}

canonical_original_code:
void v3Sub(struct Vector3 *v, struct Vector3 d)
{
    v->r[0] -= d.r[0];
    v->r[1] -= d.r[1];
    v->r[2] -= d.r[2];
}


decompiled_code:
double __fastcall v3LenSquared(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11)
{
return a9 * a9 + a10 * a10 + a11 * a11;
}

canonical_decompiled_code:
double __fastcall func7(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11)
{
return a9 * a9 + a10 * a10 + a11 * a11;
}

original_code:
double v3LenSquared(Vector3 v)
{
    return v.r[0] * v.r[0] + v.r[1] * v.r[1] + v.r[2] * v.r[2];
}

canonical_original_code:
double v3LenSquared(struct Vector3 v)
{
    return v.r[0] * v.r[0] + v.r[1] * v.r[1] + v.r[2] * v.r[2];
}

variable_types:
v
struct Vector3 { double r[3]; }

return_type: double


decompiled_code:
double __fastcall v3Len(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11)
{
double v17; // xmm0_8
double result; // xmm0_8

v17 = v3LenSquared(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11);
if ( v17 < 0.0 )
result = sqrt(v17);
else
result = sqrt(v17);
return result;
}

canonical_decompiled_code:
double __fastcall func8(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11)
{
double v17; // xmm0_8
double result; // xmm0_8

v17 = func7(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11);
if ( v17 < 0.0 )
result = sqrt(v17);
else
result = sqrt(v17);
return result;
}

original_code:
double v3Len(Vector3 v)
{
    return sqrt(v3LenSquared(v));
}

canonical_original_code:
double v3Len(struct Vector3 v)
{
    return sqrt(v3LenSquared(v));
}


original_code:
void v3Scale(Vector3 *v, double scale)
{
    v->r[0] *= scale;
    v->r[1] *= scale;
    v->r[2] *= scale;
}

canonical_original_code:
void v3Scale(struct Vector3 *v, double scale)
{
    v->r[0] *= scale;
    v->r[1] *= scale;
    v->r[2] *= scale;
}

variable_types: scale double


decompiled_code:
void __fastcall v3Normalize(double *a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9)
{
double v9; // xmm0_8

v9 = v3Len(a2, a3, a4, a5, a6, a7, a8, a9, *a1, a1[1], a1[2]);
*a1 = *a1 / v9;
a1[1] = a1[1] / v9;
a1[2] = a1[2] / v9;
}

canonical_decompiled_code:
void __fastcall func11(double *a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9)
{
double v9; // xmm0_8

v9 = func8(a2, a3, a4, a5, a6, a7, a8, a9, *a1, a1[1], a1[2]);
*a1 = *a1 / v9;
a1[1] = a1[1] / v9;
a1[2] = a1[2] / v9;
}

original_code:
void v3Normalize(Vector3 *v)
{
    double len = v3Len(*v);
    v->r[0] /= len;
    v->r[1] /= len;
    v->r[2] /= len;
}

canonical_original_code:
void v3Normalize(struct Vector3 *v)
{
    double len = v3Len(*v);
    v->r[0] /= len;
    v->r[1] /= len;
    v->r[2] /= len;
}

variable_types: len double


decompiled_code:
double __fastcall v3Dot(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13, double a14)
{
return a9 * a12 + a10 * a13 + a11 * a14;
}

canonical_decompiled_code:
double __fastcall func13(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13, double a14)
{
return a9 * a12 + a10 * a13 + a11 * a14;
}

canonical_original_code:
double v3Dot(struct Vector3 v1, struct Vector3 v2)
{
    return v1.r[0] * v2.r[0] + v1.r[1] * v2.r[1] + v1.r[2] * v2.r[2];
}


decompiled_code:
double __fastcall v3Angle(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13, double a14)
{
double v20; // xmm0_8

v20 = v3Cos(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14);
return acos(v20);
}

canonical_decompiled_code:
double __fastcall func15(double a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, double a12, double a13, double a14)
{
double v20; // xmm0_8

v20 = func14(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14);
return acos(v20);
}

original_code:
double v3Angle(Vector3 v1, Vector3 v2)
{
    return acos(v3Cos(v1, v2));
}

canonical_original_code:
double v3Angle(struct Vector3 v1, struct Vector3 v2)
{
    return acos(v3Cos(v1, v2));
}


decompiled_code:
__int64 __fastcall ns_add_connection(__int64 a1, unsigned int a2)
{
void *v2; // rax

v2 = calloc(1uLL, 0x18uLL);
paSet(a1 + 56, a2, v2);
return disOnData(a1, a2, &ns_handle_data, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2)
{
void *v2; // rax

v2 = calloc(1uLL, 0x18uLL);
paSet(a1 + 56, a2, v2);
return disOnData(a1, a2, &ns_handle_data, 0LL);
}

original_code:
static void ns_add_connection(NS *ns, int fd)
{
    NS_Connection *conn = calloc(1, sizeof(NS_Connection));
    paSet(&ns->connections, fd, conn);
    if (0) _dbgPrint(stderr, "ns.c", 90, __func__, "New connection on fd %d\n", fd);
    disOnData(&ns->dis, fd, ns_handle_data, ((void *)0));
}

canonical_original_code:
static void ns_add_connection(struct NS *ns, int fd)
{
    struct NS_Connection *conn = calloc(1, sizeof(NS_Connection));
    paSet(&ns->connections, fd, conn);
    if (0) _dbgPrint(stderr, "ns.c", 90, __func__, "New connection on fd %d\n", fd);
    disOnData(&ns->dis, fd, ns_handle_data, ((void *)0));
}

variable_types: struct NS { struct Dispatcher { struct PointerArray { int num_ptrs; void **ptr; } files; struct List { struct ListNode { struct ListNode *next; struct ListNode *prev; struct List *list; } *head; struct ListNode *tail; int length; } timers; struct timeval { long tv_sec; long tv_usec; } tv; } dis; struct PointerArray connections; void (*on_connect_cb)(struct NS *ns, int fd, void *udata); void *on_connect_udata; void (*on_disconnect_cb)(struct NS *ns, int fd, void *udata); void *on_disconnect_udata; void (*on_error_cb)(struct NS *ns, int fd, int error, void *udata); void *on_error_udata; void (*on_socket_cb)(struct NS *ns, int fd, char *buffer, int size, void *udata); void *on_socket_udata; } *

variable_types: fd int

variable_types: struct NS_Connection { struct Buffer { char *data; unsigned long used; unsigned long size; } incoming; } *

user_defined_types: struct NS { struct Dispatcher dis; struct PointerArray connections; void (*on_connect_cb)(struct NS *ns, int fd, void *udata); void *on_connect_udata; void (*on_disconnect_cb)(struct NS *ns, int fd, void *udata); void *on_disconnect_udata; void (*on_error_cb)(struct NS *ns, int fd, int error, void *udata); void *on_error_udata; void (*on_socket_cb)(struct NS *ns, int fd, char *buffer, int size, void *udata); void *on_socket_udata; }

user_defined_types: struct NS_Connection { struct Buffer incoming; }

user_defined_types: struct Dispatcher { struct PointerArray files; struct List timers; struct timeval tv; }

user_defined_types: struct List { struct ListNode *head; struct ListNode *tail; int length; }


nsInit

decompiled_code:
__int64 __fastcall nsInit(_QWORD *a1)
{
__int64 result; // rax

*a1 = 0LL;
a1[16] = 0LL;
result = 0LL;
memset(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 136) >> 3));
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1)
{
__int64 result; // rax

*a1 = 0LL;
a1[16] = 0LL;
result = 0LL;
memset(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 136) >> 3));
return result;
}

original_code:
void nsInit(NS *ns)
{
    memset(ns, 0, sizeof(NS));
}

canonical_original_code:
void nsInit(struct NS *ns)
{
    memset(ns, 0, sizeof(NS));
}

user_defined_types: struct PointerArray { int num_ptrs; void **ptr; }

user_defined_types: struct timeval { long tv_sec; long tv_usec; }

user_defined_types: struct ListNode { struct ListNode *next; struct ListNode *prev; struct List *list; }


nsCreate

decompiled_code:
void *nsCreate()
{
void *v0; // rbx

v0 = calloc(1uLL, 0x88uLL);
disInit(v0);
return v0;
}

canonical_decompiled_code:
void *func2()
{
void *v0; // rbx

v0 = calloc(1uLL, 0x88uLL);
disInit(v0);
return v0;
}

original_code:
NS *nsCreate(void)
{
    NS *ns = calloc(1, sizeof(NS));
    disInit(&ns->dis);
    return ns;
}

canonical_original_code:
struct NS *nsCreate()
{
    struct NS *ns = calloc(1, sizeof(NS));
    disInit(&ns->dis);
    return ns;
}

return_type: struct NS { struct Dispatcher { struct PointerArray { int num_ptrs; void **ptr; } files; struct List { struct ListNode { struct ListNode *next; struct ListNode *prev; struct List *list; } *head; struct ListNode *tail; int length; } timers; struct timeval { long tv_sec; long tv_usec; } tv; } dis; struct PointerArray connections; void (*on_connect_cb)(struct NS *ns, int fd, void *udata); void *on_connect_udata; void (*on_disconnect_cb)(struct NS *ns, int fd, void *udata); void *on_disconnect_udata; void (*on_error_cb)(struct NS *ns, int fd, int error, void *udata); void *on_error_udata; void (*on_socket_cb)(struct NS *ns, int fd, char *buffer, int size, void *udata); void *on_socket_udata; } *


nsListen

decompiled_code:
__int64 __fastcall nsListen(__int64 a1, __int64 a2, unsigned __int16 a3)
{
int v3; // eax
unsigned int v4; // ebx

v3 = tcpListen(a2, a3);
if ( v3 < 0 )
return (unsigned int)-1;
v4 = v3;
disOnData(a1, (unsigned int)v3, ns_accept_connection, 0LL);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, unsigned __int16 a3)
{
int v3; // eax
unsigned int v4; // ebx

v3 = tcpListen(a2, a3);
if ( v3 < 0 )
return (unsigned int)-1;
v4 = v3;
disOnData(a1, (unsigned int)v3, ns_accept_connection, 0LL);
return v4;
}

original_code:
int nsListen(NS *ns, const char *host, uint16_t port)
{
    int listen_fd;
    if ((listen_fd = tcpListen(host, port)) < 0) {
        return -1;
    }
    disOnData(&ns->dis, listen_fd, ns_accept_connection, ((void *)0));
    return listen_fd;
}

canonical_original_code:
int nsListen(struct NS *ns, char *host, unsigned short port)
{
    int listen_fd;
    if ((listen_fd = tcpListen(host, port)) < 0) {
        return -1;
    }
    disOnData(&ns->dis, listen_fd, ns_accept_connection, ((void *)0));
    return listen_fd;
}

variable_types:
host
char *

variable_types:
port
unsigned short

return_type: int


nsOnConnect

original_code:
void nsOnConnect(NS *ns, void (*cb)(NS *ns, int fd, void *udata), void *udata)
{
    ns->on_connect_cb = cb;
    ns->on_connect_udata = udata;
}

canonical_original_code:
void nsOnConnect(struct NS *ns, void (*cb)(struct NS *ns, int fd, void *udata), void *udata)
{
    ns->on_connect_cb = cb;
    ns->on_connect_udata = udata;
}

variable_types: void (*)(struct NS *ns, int fd, void *udata)

variable_types:
udata
void *


nsOnDisconnect

decompiled_code:
void __fastcall nsOnDisconnect(__int64 a1, __int64 a2, __int64 a3)
{
*(_QWORD *)(a1 + 88) = a2;
*(_QWORD *)(a1 + 96) = a3;
}

original_code:
void nsOnDisconnect(NS *ns, void (*cb)(NS *ns, int fd, void *udata), void *udata)
{
    ns->on_disconnect_cb = cb;
    ns->on_disconnect_udata = udata;
}

canonical_original_code:
void nsOnDisconnect(struct NS *ns, void (*cb)(struct NS *ns, int fd, void *udata), void *udata)
{
    ns->on_disconnect_cb = cb;
    ns->on_disconnect_udata = udata;
}


nsOnSocket

original_code:
void nsOnSocket(NS *ns, void (*cb)(NS *ns, int fd, const char *buffer, int size, void *udata),
        void *udata)
{
    ns->on_socket_cb = cb;
    ns->on_socket_udata = udata;
}

canonical_original_code:
void nsOnSocket(struct NS *ns, void (*cb)(struct NS *ns, int fd, char *buffer, int size, void *udata), void *udata)
{
    ns->on_socket_cb = cb;
    ns->on_socket_udata = udata;
}

variable_types: void (*)(struct NS *ns, int fd, char *buffer, int size, void *udata)


nsOnError

original_code:
void nsOnError(NS *ns, void (*cb)(NS *ns, int fd, int error, void *udata), void *udata)
{
    ns->on_error_cb = cb;
    ns->on_error_udata = udata;
}

canonical_original_code:
void nsOnError(struct NS *ns, void (*cb)(struct NS *ns, int fd, int error, void *udata), void *udata)
{
    ns->on_error_cb = cb;
    ns->on_error_udata = udata;
}

variable_types: void (*)(struct NS *ns, int fd, int error, void *udata)


nsConnect

decompiled_code:
__int64 __fastcall nsConnect(__int64 a1, __int64 a2, unsigned __int16 a3)
{
signed int v3; // eax
unsigned int v4; // ebx

v3 = tcpConnect(a2, a3);
v4 = v3;
if ( v3 >= 0 )
ns_add_connection(a1, v3);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2, unsigned __int16 a3)
{
signed int v3; // eax
unsigned int v4; // ebx

v3 = tcpConnect(a2, a3);
v4 = v3;
if ( v3 >= 0 )
func0(a1, v3);
return v4;
}

original_code:
int nsConnect(NS *ns, const char *host, uint16_t port)
{
    int fd = tcpConnect(host, port);
    if (fd >= 0) {
        ns_add_connection(ns, fd);
    }
    return fd;
}

canonical_original_code:
int nsConnect(struct NS *ns, char *host, unsigned short port)
{
    int fd = tcpConnect(host, port);
    if (fd >= 0) {
        ns_add_connection(ns, fd);
    }
    return fd;
}


nsDisconnect

decompiled_code:
void __fastcall nsDisconnect(__int64 a1, unsigned int a2)
{
void *v2; // rbp

v2 = (void *)paGet(a1 + 56);
close(a2);
disDropData(a1, a2);
paDrop(a1 + 56, a2);
bufRewind(v2);
free(v2);
}

canonical_decompiled_code:
void __fastcall func9(__int64 a1, unsigned int a2)
{
void *v2; // rbp

v2 = (void *)paGet(a1 + 56);
close(a2);
disDropData(a1, a2);
paDrop(a1 + 56, a2);
bufRewind(v2);
free(v2);
}

original_code:
void nsDisconnect(NS *ns, int fd)
{
    NS_Connection *conn = paGet(&ns->connections, fd);
    if (0) _dbgPrint(stderr, "ns.c", 215, __func__, "Closing fd %d\n", fd);
    close(fd);
    disDropData(&ns->dis, fd);
    paDrop(&ns->connections, fd);
    bufRewind(&conn->incoming);
    free(conn);
}

canonical_original_code:
void nsDisconnect(struct NS *ns, int fd)
{
    struct NS_Connection *conn = paGet(&ns->connections, fd);
    if (0) _dbgPrint(stderr, "ns.c", 215, __func__, "Closing fd %d\n", fd);
    close(fd);
    disDropData(&ns->dis, fd);
    paDrop(&ns->connections, fd);
    bufRewind(&conn->incoming);
    free(conn);
}

user_defined_types: struct Buffer { char *data; unsigned long used; unsigned long size; }


decompiled_code:
void __fastcall ns_handle_data(_QWORD *a1, unsigned int a2)
{
__int64 v2; // r12
int v3; // eax
void (__fastcall *v4)(_QWORD *, _QWORD, _QWORD); // rax
void (__fastcall *v5)(_QWORD *, _QWORD, __int64, _QWORD, __int64); // r13
__int64 v6; // r15
unsigned int v7; // er14
__int64 v8; // rax
void (__fastcall *v9)(_QWORD *, _QWORD, _QWORD, _QWORD); // r12
int *v10; // rax
_BYTE v11[9064]; // [rsp+0h] [rbp-2368h] BYREF

v2 = paGet(a1 + 7);
v3 = read(a2, v11, 0x2328uLL);
if ( v3 > 0 )
{
bufAdd(v2, v11, v3);
v5 = (void (__fastcall *)(_QWORD *, _QWORD, __int64, _QWORD, __int64))a1[15];
if ( v5 )
{
v6 = a1[16];
v7 = bufLen(v2);
v8 = bufGet(v2);
v5(a1, a2, v8, v7, v6);
}
}
else if ( v3 )
{
nsDisconnect((__int64)a1, a2);
v9 = (void (__fastcall *)(_QWORD *, _QWORD, _QWORD, _QWORD))a1[13];
if ( v9 )
{
v10 = _errno_location();
v9(a1, a2, (unsigned int)*v10, a1[14]);
}
}
else
{
nsDisconnect((__int64)a1, a2);
v4 = (void (__fastcall *)(_QWORD *, _QWORD, _QWORD))a1[11];
if ( v4 )
v4(a1, a2, a1[12]);
}
}

canonical_decompiled_code:
void __fastcall func10(_QWORD *a1, unsigned int a2)
{
__int64 v2; // r12
int v3; // eax
void (__fastcall *v4)(_QWORD *, _QWORD, _QWORD); // rax
void (__fastcall *v5)(_QWORD *, _QWORD, __int64, _QWORD, __int64); // r13
__int64 v6; // r15
unsigned int v7; // er14
__int64 v8; // rax
void (__fastcall *v9)(_QWORD *, _QWORD, _QWORD, _QWORD); // r12
int *v10; // rax
_BYTE v11[9064]; // [rsp+0h] [rbp-2368h] BYREF

v2 = paGet(a1 + 7);
v3 = read(a2, v11, 0x2328uLL);
if ( v3 > 0 )
{
bufAdd(v2, v11, v3);
v5 = (void (__fastcall *)(_QWORD *, _QWORD, __int64, _QWORD, __int64))a1[15];
if ( v5 )
{
v6 = a1[16];
v7 = bufLen(v2);
v8 = bufGet(v2);
v5(a1, a2, v8, v7, v6);
}
}
else if ( v3 )
{
func9((__int64)a1, a2);
v9 = (void (__fastcall *)(_QWORD *, _QWORD, _QWORD, _QWORD))a1[13];
if ( v9 )
{
v10 = _errno_location();
v9(a1, a2, (unsigned int)*v10, a1[14]);
}
}
else
{
func9((__int64)a1, a2);
v4 = (void (__fastcall *)(_QWORD *, _QWORD, _QWORD))a1[11];
if ( v4 )
v4(a1, a2, a1[12]);
}
}

original_code:
static void ns_handle_data(Dispatcher *dis, int fd, __attribute__((unused)) void *udata)
{
    char data[9000];
    NS *ns = (NS *) dis;
    NS_Connection *conn = paGet(&ns->connections, fd);
    int n;
    if (0) _dbgPrint(stderr, "ns.c", 41, __func__, "Reading from fd %d...\n", fd);
    n = read(fd, data, sizeof(data));
    if (0) _dbgPrint(stderr, "ns.c", 45, __func__, "read() returned %d.\n", n);
    if (n > 0) {
        if (0) _dbgPrint(stderr, "ns.c", 48, __func__, "Adding to incoming buffer.\n");
        bufAdd(&conn->incoming, data, n);
        if (ns->on_socket_cb != ((void *)0)) {
            if (0) _dbgPrint(stderr, "ns.c", 53, __func__, "Calling on_socket_cb.\n");
            ns->on_socket_cb(ns, fd,
                    bufGet(&conn->incoming), bufLen(&conn->incoming),
                    ns->on_socket_udata);
        }
    }
    else if (n == 0) {
        if (0) _dbgPrint(stderr, "ns.c", 61, __func__, "End of file, disconnecting.\n");
        nsDisconnect(ns, fd);
        if (ns->on_disconnect_cb != ((void *)0)) {
            if (0) _dbgPrint(stderr, "ns.c", 66, __func__, "Calling on_disconnect_cb.\n");
            ns->on_disconnect_cb(ns, fd, ns->on_disconnect_udata);
        }
    }
    else {
        if (0) _dbgPrint(stderr, "ns.c", 72, __func__, "Error, disconnecting.\n");
        nsDisconnect(ns, fd);
        if (ns->on_error_cb != ((void *)0)) {
            if (0) _dbgPrint(stderr, "ns.c", 77, __func__, "Calling on_error_cb.\n");
            ns->on_error_cb(ns, fd, (*__errno_location ()), ns->on_error_udata);
        }
    }
}

canonical_original_code:
static void ns_handle_data(struct Dispatcher *dis, int fd, void *udata)
{
    char data[9000];
    struct NS *ns = (NS *) dis;
    struct NS_Connection *conn = paGet(&ns->connections, fd);
    int n;
    if (0) _dbgPrint(stderr, "ns.c", 41, __func__, "Reading from fd %d...\n", fd);
    n = read(fd, data, sizeof(data));
    if (0) _dbgPrint(stderr, "ns.c", 45, __func__, "read() returned %d.\n", n);
    if (n > 0) {
        if (0) _dbgPrint(stderr, "ns.c", 48, __func__, "Adding to incoming buffer.\n");
        bufAdd(&conn->incoming, data, n);
        if (ns->on_socket_cb != ((void *)0)) {
            if (0) _dbgPrint(stderr, "ns.c", 53, __func__, "Calling on_socket_cb.\n");
            ns->on_socket_cb(ns, fd,
                    bufGet(&conn->incoming), bufLen(&conn->incoming),
                    ns->on_socket_udata);
        }
    }
    else if (n == 0) {
        if (0) _dbgPrint(stderr, "ns.c", 61, __func__, "End of file, disconnecting.\n");
        nsDisconnect(ns, fd);
        if (ns->on_disconnect_cb != ((void *)0)) {
            if (0) _dbgPrint(stderr, "ns.c", 66, __func__, "Calling on_disconnect_cb.\n");
            ns->on_disconnect_cb(ns, fd, ns->on_disconnect_udata);
        }
    }
    else {
        if (0) _dbgPrint(stderr, "ns.c", 72, __func__, "Error, disconnecting.\n");
        nsDisconnect(ns, fd);
        if (ns->on_error_cb != ((void *)0)) {
            if (0) _dbgPrint(stderr, "ns.c", 77, __func__, "Calling on_error_cb.\n");
            ns->on_error_cb(ns, fd, (*__errno_location ()), ns->on_error_udata);
        }
    }
}

variable_types: struct Dispatcher { struct PointerArray { int num_ptrs; void **ptr; } files; struct List { struct ListNode { struct ListNode *next; struct ListNode *prev; struct List *list; } *head; struct ListNode *tail; int length; } timers; struct timeval { long tv_sec; long tv_usec; } tv; } *

variable_types:
data
char [9000]

variable_types: n int


nsDropData

decompiled_code:
__int64 __fastcall nsDropData(__int64 a1, __int64 a2)
{
return disDropData(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, __int64 a2)
{
return disDropData(a1, a2);
}

original_code:
void nsDropData(NS *ns, int fd)
{
    if (0) _dbgPrint(stderr, "ns.c", 246, __func__, "Calling disDropData for fd %d\n", fd);
    disDropData(&ns->dis, fd);
}

canonical_original_code:
void nsDropData(struct NS *ns, int fd)
{
    if (0) _dbgPrint(stderr, "ns.c", 246, __func__, "Calling disDropData for fd %d\n", fd);
    disDropData(&ns->dis, fd);
}


nsWrite

decompiled_code:
__int64 nsWrite()
{
return disWrite();
}

canonical_decompiled_code:
__int64 func13()
{
return disWrite();
}

original_code:
void nsWrite(NS *ns, int fd, const char *data, size_t size)
{
    disWrite(&ns->dis, fd, data, size);
}

canonical_original_code:
void nsWrite(struct NS *ns, int fd, char *data, unsigned long size)
{
    disWrite(&ns->dis, fd, data, size);
}

variable_types:
data
char *

variable_types:
size
unsigned long


nsVaPack

decompiled_code:
void __fastcall nsVaPack(__int64 a1, unsigned int a2, __int64 a3)
{
int v3; // eax
void *ptr; // [rsp+8h] [rbp-20h] BYREF

v3 = vastrpack(&ptr, a3);
nsWrite(a1, a2, ptr, v3);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func14(__int64 a1, unsigned int a2, __int64 a3)
{
int v3; // eax
void *ptr; // [rsp+8h] [rbp-20h] BYREF

v3 = vastrpack(&ptr, a3);
func13(a1, a2, ptr, v3);
free(ptr);
}

original_code:
void nsVaPack(NS *ns, int fd, va_list ap)
{
    char *str;
    int n = vastrpack(&str, ap);
    nsWrite(ns, fd, str, n);
    free(str);
}

canonical_original_code:
void nsVaPack(struct NS *ns, int fd, __builtin_va_list ap)
{
    char *str;
    int n = vastrpack(&str, ap);
    nsWrite(ns, fd, str, n);
    free(str);
}

variable_types:
str
char *


nsPack

decompiled_code:
void nsPack(__int64 a1, unsigned int a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
nsVaPack(a1, a2, (__int64)va);
}

canonical_decompiled_code:
void func15(__int64 a1, unsigned int a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
func14(a1, a2, (__int64)va);
}

original_code:
void nsPack(NS *ns, int fd, ...)
{
    va_list ap;
    __builtin_va_start(ap,fd);
    nsVaPack(ns, fd, ap);
    __builtin_va_end(ap);
}

canonical_original_code:
void nsPack(struct NS *ns, int fd, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,fd);
    nsVaPack(ns, fd, ap);
    __builtin_va_end(ap);
}


nsIncoming

decompiled_code:
__int64 __fastcall nsIncoming(__int64 a1)
{
__int64 v1; // rax

v1 = paGet(a1 + 56);
return bufGet(v1);
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1)
{
__int64 v1; // rax

v1 = paGet(a1 + 56);
return bufGet(v1);
}

original_code:
const char *nsIncoming(NS *ns, int fd)
{
    NS_Connection *conn = paGet(&ns->connections, fd);
    return bufGet(&conn->incoming);
}

canonical_original_code:
char *nsIncoming(struct NS *ns, int fd)
{
    struct NS_Connection *conn = paGet(&ns->connections, fd);
    return bufGet(&conn->incoming);
}

return_type: char *


nsAvailable

decompiled_code:
__int64 __fastcall nsAvailable(__int64 a1)
{
__int64 v1; // rax

v1 = paGet(a1 + 56);
return bufLen(v1);
}

canonical_decompiled_code:
__int64 __fastcall func17(__int64 a1)
{
__int64 v1; // rax

v1 = paGet(a1 + 56);
return bufLen(v1);
}

original_code:
int nsAvailable(NS *ns, int fd)
{
    NS_Connection *conn = paGet(&ns->connections, fd);
    return bufLen(&conn->incoming);
}

canonical_original_code:
int nsAvailable(struct NS *ns, int fd)
{
    struct NS_Connection *conn = paGet(&ns->connections, fd);
    return bufLen(&conn->incoming);
}


nsDiscard

decompiled_code:
__int64 __fastcall nsDiscard(__int64 a1, __int64 a2, int a3)
{
__int64 v4; // rax

v4 = paGet(a1 + 56);
return bufTrim(v4, a3, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 a1, __int64 a2, int a3)
{
__int64 v4; // rax

v4 = paGet(a1 + 56);
return bufTrim(v4, a3, 0LL);
}

original_code:
void nsDiscard(NS *ns, int fd, int length)
{
    NS_Connection *conn = paGet(&ns->connections, fd);
    bufTrim(&conn->incoming, length, 0);
}

canonical_original_code:
void nsDiscard(struct NS *ns, int fd, int length)
{
    struct NS_Connection *conn = paGet(&ns->connections, fd);
    bufTrim(&conn->incoming, length, 0);
}

variable_types: length int


nsFdCount

decompiled_code:
__int64 nsFdCount()
{
return disFdCount();
}

canonical_decompiled_code:
__int64 func21()
{
return disFdCount();
}

original_code:
int nsFdCount(NS *ns)
{
    return disFdCount(&ns->dis);
}

canonical_original_code:
int nsFdCount(struct NS *ns)
{
    return disFdCount(&ns->dis);
}


nsOwnsFd

decompiled_code:
__int64 nsOwnsFd()
{
return disOwnsFd();
}

canonical_decompiled_code:
__int64 func22()
{
return disOwnsFd();
}

original_code:
int nsOwnsFd(NS *ns, int fd)
{
    return disOwnsFd(&ns->dis, fd);
}

canonical_original_code:
int nsOwnsFd(struct NS *ns, int fd)
{
    return disOwnsFd(&ns->dis, fd);
}


nsPrepareSelect

decompiled_code:
__int64 nsPrepareSelect()
{
return disPrepareSelect();
}

canonical_decompiled_code:
__int64 func23()
{
return disPrepareSelect();
}

original_code:
int nsPrepareSelect(NS *ns, int *nfds, fd_set *rfds, fd_set *wfds,
        struct timeval **tv)
{
    return disPrepareSelect(&ns->dis, nfds, rfds, wfds, tv);
}

canonical_original_code:
int nsPrepareSelect(struct NS *ns, int *nfds, struct fd_set *rfds, struct fd_set *wfds, struct timeval **tv)
{
    return disPrepareSelect(&ns->dis, nfds, rfds, wfds, tv);
}

variable_types:
nfds
int *

variable_types:
rfds
struct fd_set { long fds_bits[-1]; } *

variable_types:
wfds
struct fd_set *

variable_types:
tv
struct timeval **


nsHandleTimer

decompiled_code:
__int64 nsHandleTimer()
{
return disHandleTimer();
}

canonical_decompiled_code:
__int64 func24()
{
return disHandleTimer();
}

original_code:
void nsHandleTimer(NS *ns)
{
    disHandleTimer(&ns->dis);
}

canonical_original_code:
void nsHandleTimer(struct NS *ns)
{
    disHandleTimer(&ns->dis);
}

variable_types: struct NS { struct Dispatcher { struct PointerArray { int num_ptrs; void **ptr; } files; struct List { struct ListNode { struct ListNode *next; struct ListNode *prev; struct List *list; } *head; struct ListNode *tail; int length; } timers; struct timeval { long tv_sec; long tv_usec; } tv; } dis; struct PointerArray connections; void (*on_connect_cb)(struct NS *ns, int fd, void *udata); void *on_connect_udata; void (*on_disconnect_cb)(struct NS *ns, int fd, void *udata); void *on_disconnect_udata; void (*on_error_cb)(struct NS *ns, int fd, int error, void *udata); void *on_error_udata; void (*on_socket_cb)(struct NS *ns, int fd, char *buffer, int size, void *udata); void *on_socket_udata; } *


nsHandleFiles

decompiled_code:
__int64 nsHandleFiles()
{
return disHandleFiles();
}

canonical_decompiled_code:
__int64 func25()
{
return disHandleFiles();
}

original_code:
void nsHandleFiles(NS *ns, int nfds, fd_set *rfds, fd_set *wfds)
{
    disHandleFiles(&ns->dis, nfds, rfds, wfds);
}

canonical_original_code:
void nsHandleFiles(struct NS *ns, int nfds, struct fd_set *rfds, struct fd_set *wfds)
{
    disHandleFiles(&ns->dis, nfds, rfds, wfds);
}

variable_types: nfds int


nsHandleReadable

decompiled_code:
__int64 nsHandleReadable()
{
return disHandleReadable();
}

canonical_decompiled_code:
__int64 func26()
{
return disHandleReadable();
}

original_code:
void nsHandleReadable(NS *ns, int fd)
{
    disHandleReadable(&ns->dis, fd);
}

canonical_original_code:
void nsHandleReadable(struct NS *ns, int fd)
{
    disHandleReadable(&ns->dis, fd);
}

variable_types: fd int


nsHandleWritable

decompiled_code:
__int64 nsHandleWritable()
{
return disHandleWritable();
}

canonical_decompiled_code:
__int64 func27()
{
return disHandleWritable();
}

original_code:
void nsHandleWritable(NS *ns, int fd)
{
    disHandleWritable(&ns->dis, fd);
}

canonical_original_code:
void nsHandleWritable(struct NS *ns, int fd)
{
    disHandleWritable(&ns->dis, fd);
}


nsProcessSelect

decompiled_code:
__int64 nsProcessSelect()
{
return disProcessSelect();
}

canonical_decompiled_code:
__int64 func28()
{
return disProcessSelect();
}

original_code:
void nsProcessSelect(NS *ns, int r, int nfds, fd_set *rfds, fd_set *wfds)
{
    disProcessSelect(&ns->dis, r, nfds, rfds, wfds);
}

canonical_original_code:
void nsProcessSelect(struct NS *ns, int r, int nfds, struct fd_set *rfds, struct fd_set *wfds)
{
    disProcessSelect(&ns->dis, r, nfds, rfds, wfds);
}

variable_types: r int


nsHandleEvents

decompiled_code:
__int64 nsHandleEvents()
{
return disHandleEvents();
}

canonical_decompiled_code:
__int64 func29()
{
return disHandleEvents();
}

original_code:
int nsHandleEvents(NS *ns)
{
    return disHandleEvents(&ns->dis);
}

canonical_original_code:
int nsHandleEvents(struct NS *ns)
{
    return disHandleEvents(&ns->dis);
}


nsRun

decompiled_code:
__int64 nsRun()
{
return disRun();
}

canonical_decompiled_code:
__int64 func30()
{
return disRun();
}

original_code:
int nsRun(NS *ns)
{
    return disRun(&ns->dis);
}

canonical_original_code:
int nsRun(struct NS *ns)
{
    return disRun(&ns->dis);
}


nsClose

decompiled_code:
__int64 nsClose()
{
return disClose();
}

canonical_decompiled_code:
__int64 func31()
{
return disClose();
}

original_code:
void nsClose(NS *ns)
{
    disClose((Dispatcher *) ns);
}

canonical_original_code:
void nsClose(struct NS *ns)
{
    disClose((struct Dispatcher *) ns);
}


nsClear

decompiled_code:
__int64 __fastcall nsClear(_QWORD *a1)
{
__int64 result; // rax

disClear();
*a1 = 0LL;
a1[16] = 0LL;
result = 0LL;
memset(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 136) >> 3));
return result;
}

canonical_decompiled_code:
__int64 __fastcall func32(_QWORD *a1)
{
__int64 result; // rax

disClear();
*a1 = 0LL;
a1[16] = 0LL;
result = 0LL;
memset(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 136) >> 3));
return result;
}

original_code:
void nsClear(NS *ns)
{
    disClear(&ns->dis);
    memset(ns, 0, sizeof(NS));
}

canonical_original_code:
void nsClear(struct NS *ns)
{
    disClear(&ns->dis);
    memset(ns, 0, sizeof(NS));
}


nsDestroy

decompiled_code:
void __fastcall nsDestroy(void *ptr)
{
nsClose();
free(ptr);
}

canonical_decompiled_code:
void __fastcall func33(void *ptr)
{
func31();
free(ptr);
}

original_code:
void nsDestroy(NS *ns)
{
    nsClose(ns);
    free(ns);
}

canonical_original_code:
void nsDestroy(struct NS *ns)
{
    nsClose(ns);
    free(ns);
}


tvNormalized

original_code:
struct timeval tvNormalized(struct timeval tv)
{
    struct timeval dup = tv;
    while (dup.tv_usec < 0) {
        dup.tv_sec--;
        dup.tv_usec += 1000000L;
    }
    while (dup.tv_usec >= 1000000L) {
        dup.tv_sec++;
        dup.tv_usec -= 1000000L;
    }
    return dup;
}

variable_types:
tv
struct timeval

variable_types: struct timeval { long tv_sec; long tv_usec; }


tvNormalize

decompiled_code:
__int64 __fastcall tvNormalize(__int64 *a1)
{
__int64 result; // rax
__int64 v2; // rdx

result = tvNormalized(*a1, a1[1]);
*a1 = result;
a1[1] = v2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 *a1)
{
__int64 result; // rax
__int64 v2; // rdx

result = func0(*a1, a1[1]);
*a1 = result;
a1[1] = v2;
return result;
}

original_code:
void tvNormalize(struct timeval *tv)
{
    *tv = tvNormalized(*tv);
}

variable_types: struct timeval { long tv_sec; long tv_usec; } *


tvMake

decompiled_code:
__int64 __fastcall tvMake(__int64 a1, __int64 a2)
{
__int64 v3[3]; // [rsp+0h] [rbp-18h] BYREF

v3[0] = a1;
v3[1] = a2;
tvNormalize(v3);
return v3[0];
}

original_code:
struct timeval tvMake(long sec, long usec)
{
    struct timeval t = {
        .tv_sec = sec,
        .tv_usec = usec
    };
    tvNormalize(&t);
    return t;
}

variable_types: sec long

variable_types: usec long

return_type: struct timeval { long tv_sec; long tv_usec; }


tvNow

decompiled_code:
__time_t tvNow()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

v1.tv_sec = 0LL;
v1.tv_usec = 0LL;
gettimeofday(&v1, 0LL);
return v1.tv_sec;
}

canonical_decompiled_code:
__time_t func3()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

v1.tv_sec = 0LL;
v1.tv_usec = 0LL;
gettimeofday(&v1, 0LL);
return v1.tv_sec;
}

original_code:
struct timeval tvNow(void)
{
    struct timeval tv = { 0 };
    gettimeofday(&tv, ((void *)0));
    return tv;
}

canonical_original_code:
struct timeval tvNow()
{
    struct timeval tv = { 0 };
    gettimeofday(&tv, ((void *)0));
    return tv;
}


tvCompare

decompiled_code:
__int64 __fastcall tvCompare(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( a1 >= a3 )
{
result = 1LL;
if ( a1 <= a3 )
{
result = 0xFFFFFFFFLL;
if ( a2 >= a4 )
result = a2 > a4;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( a1 >= a3 )
{
result = 1LL;
if ( a1 <= a3 )
{
result = 0xFFFFFFFFLL;
if ( a2 >= a4 )
result = a2 > a4;
}
}
return result;
}

original_code:
int tvCompare(struct timeval t1, struct timeval t0)
{
    if (t1.tv_sec < t0.tv_sec) {
        return -1;
    }
    else if (t1.tv_sec > t0.tv_sec) {
        return 1;
    }
    else if (t1.tv_usec < t0.tv_usec) {
        return -1;
    }
    else if (t1.tv_usec > t0.tv_usec) {
        return 1;
    }
    else {
        return 0;
    }
}


tvDelta

decompiled_code:
double __fastcall tvDelta(int a1, int a2, int a3, int a4)
{
return (double)(a2 - a4) / 1000000.0 + (double)(a1 - a3);
}

canonical_decompiled_code:
double __fastcall func5(int a1, int a2, int a3, int a4)
{
return (double)(a2 - a4) / 1000000.0 + (double)(a1 - a3);
}

original_code:
double tvDelta(struct timeval t1, struct timeval t0)
{
    return t1.tv_sec - t0.tv_sec
         + (double) (t1.tv_usec - t0.tv_usec) / 1000000L;
}


tvDec

decompiled_code:
__int64 __fastcall tvDec(__int64 a1, __int64 a2, double a3)
{
__int64 v4[3]; // [rsp+0h] [rbp-18h] BYREF

v4[0] = a1 - (unsigned int)(int)a3;
v4[1] = a2 - (unsigned int)(int)(fmod(a3, 1.0) * 1000000.0);
tvNormalize(v4);
return v4[0];
}

original_code:
struct timeval tvDec(struct timeval tv, double seconds)
{
    tv.tv_sec -= (long) seconds;
    tv.tv_usec -= (long) (1000000L * fmod(seconds, 1));
    tvNormalize(&tv);
    return tv;
}

variable_types: seconds double


tvInc

decompiled_code:
__int64 __fastcall tvInc(__int64 a1, __int64 a2, double a3)
{
__int64 v4[3]; // [rsp+0h] [rbp-18h] BYREF

v4[0] = (unsigned int)(int)a3 + a1;
v4[1] = (unsigned int)(int)(fmod(a3, 1.0) * 1000000.0) + a2;
tvNormalize(v4);
return v4[0];
}

original_code:
struct timeval tvInc(struct timeval tv, double seconds)
{
    tv.tv_sec += (long) seconds;
    tv.tv_usec += (long) (1000000L * fmod(seconds, 1));
    tvNormalize(&tv);
    return tv;
}


tvFromDouble

decompiled_code:
__int64 __fastcall tvFromDouble(double a1)
{
double v1; // xmm0_8

v1 = fmod(a1 * 1000000.0, 1000000.0);
return tvMake((int)a1, (unsigned int)(int)v1);
}

canonical_decompiled_code:
__int64 __fastcall func8(double a1)
{
double v1; // xmm0_8

v1 = fmod(a1 * 1000000.0, 1000000.0);
return func2((int)a1, (unsigned int)(int)v1);
}

original_code:
struct timeval tvFromDouble(double t)
{
    return tvMake((int) t, fmod(1000000L * t, 1000000L));
}

variable_types: t double


tvToDouble

decompiled_code:
double __fastcall tvToDouble(int a1, int a2)
{
return (double)a2 / 1000000.0 + (double)a1;
}

canonical_decompiled_code:
double __fastcall func9(int a1, int a2)
{
return (double)a2 / 1000000.0 + (double)a1;
}

original_code:
double tvToDouble(struct timeval tv)
{
    return (double) tv.tv_sec + (double) tv.tv_usec / 1000000L;
}


tvFromTimespec

decompiled_code:
__int64 __fastcall tvFromTimespec(__int64 a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
return a1;
}

original_code:
struct timeval tvFromTimespec(struct timespec ts)
{
    struct timeval tv = {
        .tv_sec = ts.tv_sec,
        .tv_usec = ts.tv_nsec * 1000
    };
    return tv;
}

variable_types: struct timespec { long tv_sec; long tv_nsec; }


tvFormatC

decompiled_code:
__int64 __fastcall tvFormatC(__int64 a1, int a2)
{
return t_format_c(a1, (unsigned int)(1000 * a2));
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, int a2)
{
return t_format_c(a1, (unsigned int)(1000 * a2));
}

original_code:
const char *tvFormatC(const struct timeval tv, const char *tz,
        const char *fmt)
{
    return t_format_c(tv.tv_sec, 1000 * tv.tv_usec, tz, fmt);
}

canonical_original_code:
char *tvFormatC(struct timeval tv, char *tz, char *fmt)
{
    return t_format_c(tv.tv_sec, 1000 * tv.tv_usec, tz, fmt);
}

variable_types:
tz
char *

variable_types:
fmt
char *


tvFormat

decompiled_code:
char *__fastcall tvFormat(__int64 a1, int a2)
{
const char *v2; // rax

v2 = (const char *)tvFormatC(a1, a2);
return strdup(v2);
}

canonical_decompiled_code:
char *__fastcall func12(__int64 a1, int a2)
{
const char *v2; // rax

v2 = (const char *)func11(a1, a2);
return strdup(v2);
}

original_code:
char *tvFormat(const struct timeval tv, const char *tz, const char *fmt)
{
    return strdup(tvFormatC(tv, tz, fmt));
}

canonical_original_code:
char *tvFormat(struct timeval tv, char *tz, char *fmt)
{
    return strdup(tvFormatC(tv, tz, fmt));
}


decompiled_code:
__int64 __fastcall tcp_socket(int a1)
{
int v1; // eax
unsigned int v2; // ebx

v1 = socket(a1, 1, 0);
v2 = v1;
if ( v1 != -1 && setsockopt(v1, 1, 13, &linger, 8u) )
{
close(v2);
v2 = -1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
int v1; // eax
unsigned int v2; // ebx

v1 = socket(a1, 1, 0);
v2 = v1;
if ( v1 != -1 && setsockopt(v1, 1, 13, &linger, 8u) )
{
close(v2);
v2 = -1;
}
return v2;
}

original_code:
static int tcp_socket(int family)
{
    int sd;
    if ((sd = socket(family, SOCK_STREAM, 0)) == -1) {
        if (0) _dbgError(stderr, "tcp.c", 39, __func__, "socket failed");
    }
    else if (setsockopt(sd, 1, 13,
                        &linger, sizeof(linger)) != 0)
    {
        if (0) _dbgError(stderr, "tcp.c", 44, __func__, "setsockopt(LINGER) failed");
        close(sd);
        sd = -1;
    }
    return sd;
}

variable_types: family int

variable_types: sd int


decompiled_code:
__int64 __fastcall tcp_listen(char *name, unsigned __int16 a2, int a3)
{
int v3; // eax
struct addrinfo *v4; // rbp
const char *v5; // rax
unsigned int v6; // ebx
int v8; // eax
int optval; // [rsp+4h] [rbp-64h] BYREF
char s[6]; // [rsp+Ah] [rbp-5Eh] BYREF
struct addrinfo req; // [rsp+10h] [rbp-58h] BYREF
struct addrinfo *pai; // [rsp+48h] [rbp-20h] BYREF

pai = 0LL;
*(_QWORD *)&req.ai_socktype = 1LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
req.ai_flags = 1;
req.ai_family = a3;
snprintf(s, 6uLL, "%hu", a2);
v3 = getaddrinfo(name, s, &req, &pai);
if ( v3 )
{
v5 = gai_strerror(v3);
dbgPrint(stderr, "tcp.c", 80LL, "tcp_listen", "getaddrinfo for %s:%hu failed: %s", name, a2, v5);
v6 = -1;
}
else
{
optval = 1;
v4 = pai;
if ( pai )
{
do
{
v8 = tcp_socket(v4->ai_family);
v6 = v8;
if ( v8 != -1 )
{
if ( !setsockopt(v8, 1, 2, &optval, 4u) && bind(v6, v4->ai_addr, v4->ai_addrlen) >= 0 && listen(v6, 128) != -1 )
goto LABEL_7;
close(v6);
}
v4 = v4->ai_next;
}
while ( v4 );
v6 = -1;
}
else
{
v6 = -1;
}
LABEL_7:
freeaddrinfo(pai);
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *name, unsigned __int16 a2, int a3)
{
int v3; // eax
struct addrinfo *v4; // rbp
const char *v5; // rax
unsigned int v6; // ebx
int v8; // eax
int optval; // [rsp+4h] [rbp-64h] BYREF
char s[6]; // [rsp+Ah] [rbp-5Eh] BYREF
struct addrinfo req; // [rsp+10h] [rbp-58h] BYREF
struct addrinfo *pai; // [rsp+48h] [rbp-20h] BYREF

pai = 0LL;
*(_QWORD *)&req.ai_socktype = 1LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
req.ai_flags = 1;
req.ai_family = a3;
snprintf(s, 6uLL, "%hu", a2);
v3 = getaddrinfo(name, s, &req, &pai);
if ( v3 )
{
v5 = gai_strerror(v3);
dbgPrint(stderr, "tcp.c", 80LL, "tcp_listen", "getaddrinfo for %s:%hu failed: %s", name, a2, v5);
v6 = -1;
}
else
{
optval = 1;
v4 = pai;
if ( pai )
{
do
{
v8 = func0(v4->ai_family);
v6 = v8;
if ( v8 != -1 )
{
if ( !setsockopt(v8, 1, 2, &optval, 4u) && bind(v6, v4->ai_addr, v4->ai_addrlen) >= 0 && listen(v6, 128) != -1 )
goto LABEL_7;
close(v6);
}
v4 = v4->ai_next;
}
while ( v4 );
v6 = -1;
}
else
{
v6 = -1;
}
LABEL_7:
freeaddrinfo(pai);
}
return v6;
}

original_code:
static int tcp_listen(const char *host, uint16_t port, int family)
{
    struct addrinfo *info = ((void *)0), *first_info = ((void *)0);
    struct addrinfo hint = {
        .ai_family = family,
        .ai_socktype = SOCK_STREAM,
        .ai_flags = 0x0001
    };
    char port_str[6];
    snprintf(port_str, sizeof(port_str), "%hu", port);
    int r;
    if ((r = getaddrinfo(host, port_str, &hint, &first_info)) != 0) {
        _dbgPrint(stderr, "tcp.c", 80, __func__, "getaddrinfo for %s:%hu failed: %s", host, port, gai_strerror(r));
        return -1;
    }
    int lsd = -1;
    int one = 1;
    for (info = first_info; lsd == -1 && info != ((void *)0); info = info->ai_next) {
        if ((lsd = tcp_socket(info->ai_family)) == -1) {
            if (0) _dbgError(stderr, "tcp.c", 99, __func__, "tcp_socket() failed");
        }
        else if (setsockopt(lsd, 1,
                 2, &one, sizeof(one)) != 0)
        {
            if (0) _dbgError(stderr, "tcp.c", 113, __func__, "setsockopt(REUSEADDR) failed");
            close(lsd);
            lsd = -1;
        }
        else if (bind(lsd, info->ai_addr, info->ai_addrlen) < 0) {
            if (0) _dbgAbort(stderr, "tcp.c", 118, __func__, "bind() failed");
            close(lsd);
            lsd = -1;
        }
        else if (listen(lsd, 128) == -1) {
            if (0) _dbgError(stderr, "tcp.c", 123, __func__, "listen() failed");
            close(lsd);
            lsd = -1;
        }
    }
    freeaddrinfo(first_info);
    return lsd;
}

canonical_original_code:
static int tcp_listen(char *host, unsigned short port, int family)
{
    struct addrinfo *info = ((void *)0); struct addrinfo *first_info = ((void *)0);
    struct addrinfo hint = {
        .ai_family = family,
        .ai_socktype = SOCK_STREAM,
        .ai_flags = 0x0001
    };
    char port_str[6];
    snprintf(port_str, sizeof(port_str), "%hu", port);
    int r;
    if ((r = getaddrinfo(host, port_str, &hint, &first_info)) != 0) {
        _dbgPrint(stderr, "tcp.c", 80, __func__, "getaddrinfo for %s:%hu failed: %s", host, port, gai_strerror(r));
        return -1;
    }
    int lsd = -1;
    int one = 1;
    for (info = first_info; lsd == -1 && info != ((void *)0); info = info->ai_next) {
        if ((lsd = tcp_socket(info->ai_family)) == -1) {
            if (0) _dbgError(stderr, "tcp.c", 99, __func__, "tcp_socket() failed");
        }
        else if (setsockopt(lsd, 1,
                 2, &one, sizeof(one)) != 0)
        {
            if (0) _dbgError(stderr, "tcp.c", 113, __func__, "setsockopt(REUSEADDR) failed");
            close(lsd);
            lsd = -1;
        }
        else if (bind(lsd, info->ai_addr, info->ai_addrlen) < 0) {
            if (0) _dbgAbort(stderr, "tcp.c", 118, __func__, "bind() failed");
            close(lsd);
            lsd = -1;
        }
        else if (listen(lsd, 128) == -1) {
            if (0) _dbgError(stderr, "tcp.c", 123, __func__, "listen() failed");
            close(lsd);
            lsd = -1;
        }
    }
    freeaddrinfo(first_info);
    return lsd;
}

variable_types:
host
char *

variable_types:
port
unsigned short

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *

variable_types: lsd int

variable_types: one int

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


decompiled_code:
__int64 __fastcall tcp_connect(char *name, unsigned __int16 a2, int a3)
{
int v3; // eax
struct addrinfo *v4; // rbp
unsigned int v5; // ebx
const char *v6; // rax
int v7; // eax
struct addrinfo v9; // [rsp+0h] [rbp-58h] BYREF
struct addrinfo *pai; // [rsp+30h] [rbp-28h] BYREF
char s[30]; // [rsp+3Ah] [rbp-1Eh] BYREF

v9.ai_flags = 0;
*(_QWORD *)&v9.ai_socktype = 1LL;
*(_QWORD *)&v9.ai_addrlen = 0LL;
v9.ai_addr = 0LL;
v9.ai_canonname = 0LL;
v9.ai_next = 0LL;
v9.ai_family = a3;
snprintf(s, 6uLL, "%hu", a2);
v3 = getaddrinfo(name, s, &v9, &pai);
if ( v3 )
{
v6 = gai_strerror(v3);
dbgPrint(stderr, "tcp.c", 153LL, "tcp_connect", "%s: getaddrinfo failed: %s", "tcp_connect", v6);
v5 = -1;
}
else
{
v4 = pai;
if ( pai )
{
while ( 1 )
{
v7 = tcp_socket(v4->ai_family);
v5 = v7;
if ( v7 != -1 )
{
if ( connect(v7, v4->ai_addr, v4->ai_addrlen) != -1 )
{
freeaddrinfo(pai);
return v5;
}
close(v5);
}
v4 = v4->ai_next;
if ( !v4 )
{
freeaddrinfo(pai);
return (unsigned int)-1;
}
}
}
freeaddrinfo(0LL);
v5 = -1;
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *name, unsigned __int16 a2, int a3)
{
int v3; // eax
struct addrinfo *v4; // rbp
unsigned int v5; // ebx
const char *v6; // rax
int v7; // eax
struct addrinfo v9; // [rsp+0h] [rbp-58h] BYREF
struct addrinfo *pai; // [rsp+30h] [rbp-28h] BYREF
char s[30]; // [rsp+3Ah] [rbp-1Eh] BYREF

v9.ai_flags = 0;
*(_QWORD *)&v9.ai_socktype = 1LL;
*(_QWORD *)&v9.ai_addrlen = 0LL;
v9.ai_addr = 0LL;
v9.ai_canonname = 0LL;
v9.ai_next = 0LL;
v9.ai_family = a3;
snprintf(s, 6uLL, "%hu", a2);
v3 = getaddrinfo(name, s, &v9, &pai);
if ( v3 )
{
v6 = gai_strerror(v3);
dbgPrint(stderr, "tcp.c", 153LL, "tcp_connect", "%s: getaddrinfo failed: %s", "tcp_connect", v6);
v5 = -1;
}
else
{
v4 = pai;
if ( pai )
{
while ( 1 )
{
v7 = func0(v4->ai_family);
v5 = v7;
if ( v7 != -1 )
{
if ( connect(v7, v4->ai_addr, v4->ai_addrlen) != -1 )
{
freeaddrinfo(pai);
return v5;
}
close(v5);
}
v4 = v4->ai_next;
if ( !v4 )
{
freeaddrinfo(pai);
return (unsigned int)-1;
}
}
}
freeaddrinfo(0LL);
v5 = -1;
}
return v5;
}

original_code:
static int tcp_connect(const char *host, uint16_t port, int family)
{
    int r, sd = -1;
    char port_as_text[6];
    struct addrinfo *first_info, *info;
    struct addrinfo hints = {
        .ai_family = family,
        .ai_socktype = SOCK_STREAM
    };
    snprintf(port_as_text, sizeof(port_as_text), "%hu", port);
    if ((r = getaddrinfo(host, port_as_text, &hints, &first_info)) != 0) {
        _dbgPrint(stderr, "tcp.c", 153, __func__, "%s: getaddrinfo failed: %s", __func__, gai_strerror(r));
        return -1;
    }
    for (info = first_info; sd == -1 && info != ((void *)0); info = info->ai_next) {
        if ((sd = tcp_socket(info->ai_family)) == -1 ||
            (r = connect(sd, info->ai_addr, info->ai_addrlen)) == -1)
        {
            if (sd != -1) {
                close(sd);
                sd = -1;
            }
        }
    }
    freeaddrinfo(first_info);
    if (sd == -1) {
        if (0) _dbgError(stderr, "tcp.c", 184, __func__, "socket() failed");
        return -1;
    }
    else if (r == -1) {
        if (0) _dbgError(stderr, "tcp.c", 188, __func__, "connect() failed");
        return -1;
    }
    return sd;
}

canonical_original_code:
static int tcp_connect(char *host, unsigned short port, int family)
{
    int r; int sd = -1;
    char port_as_text[6];
    struct addrinfo *first_info; struct addrinfo *info;
    struct addrinfo hints = {
        .ai_family = family,
        .ai_socktype = SOCK_STREAM
    };
    snprintf(port_as_text, sizeof(port_as_text), "%hu", port);
    if ((r = getaddrinfo(host, port_as_text, &hints, &first_info)) != 0) {
        _dbgPrint(stderr, "tcp.c", 153, __func__, "%s: getaddrinfo failed: %s", __func__, gai_strerror(r));
        return -1;
    }
    for (info = first_info; sd == -1 && info != ((void *)0); info = info->ai_next) {
        if ((sd = tcp_socket(info->ai_family)) == -1 ||
            (r = connect(sd, info->ai_addr, info->ai_addrlen)) == -1)
        {
            if (sd != -1) {
                close(sd);
                sd = -1;
            }
        }
    }
    freeaddrinfo(first_info);
    if (sd == -1) {
        if (0) _dbgError(stderr, "tcp.c", 184, __func__, "socket() failed");
        return -1;
    }
    else if (r == -1) {
        if (0) _dbgError(stderr, "tcp.c", 188, __func__, "connect() failed");
        return -1;
    }
    return sd;
}

variable_types: r int

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }


decompiled_code:
__int64 __fastcall tcp4Listen(char *a1, unsigned __int16 a2)
{
return tcp_listen(a1, a2, 2);
}

canonical_decompiled_code:
__int64 __fastcall func3(char *a1, unsigned __int16 a2)
{
return func1(a1, a2, 2);
}

original_code:
int tcp4Listen(const char *host, uint16_t port)
{
    return tcp_listen(host, port, 2);
}

canonical_original_code:
int tcp4Listen(char *host, unsigned short port)
{
    return tcp_listen(host, port, 2);
}


decompiled_code:
__int64 __fastcall tcp4Connect(char *a1, unsigned __int16 a2)
{
return tcp_connect(a1, a2, 2);
}

canonical_decompiled_code:
__int64 __fastcall func4(char *a1, unsigned __int16 a2)
{
return func2(a1, a2, 2);
}

original_code:
int tcp4Connect(const char *host, uint16_t port)
{
    return tcp_connect(host, port, 2);
}

canonical_original_code:
int tcp4Connect(char *host, unsigned short port)
{
    return tcp_connect(host, port, 2);
}


decompiled_code:
__int64 __fastcall tcp6Listen(char *a1, unsigned __int16 a2)
{
return tcp_listen(a1, a2, 10);
}

canonical_decompiled_code:
__int64 __fastcall func5(char *a1, unsigned __int16 a2)
{
return func1(a1, a2, 10);
}

original_code:
int tcp6Listen(const char *host, uint16_t port)
{
    return tcp_listen(host, port, 10);
}

canonical_original_code:
int tcp6Listen(char *host, unsigned short port)
{
    return tcp_listen(host, port, 10);
}


decompiled_code:
__int64 __fastcall tcp6Connect(char *a1, unsigned __int16 a2)
{
return tcp_connect(a1, a2, 10);
}

canonical_decompiled_code:
__int64 __fastcall func6(char *a1, unsigned __int16 a2)
{
return func2(a1, a2, 10);
}

original_code:
int tcp6Connect(const char *host, uint16_t port)
{
    return tcp_connect(host, port, 10);
}

canonical_original_code:
int tcp6Connect(char *host, unsigned short port)
{
    return tcp_connect(host, port, 10);
}


tcpListen

decompiled_code:
__int64 __fastcall tcpListen(char *a1, unsigned __int16 a2)
{
return tcp_listen(a1, a2, 0);
}

canonical_decompiled_code:
__int64 __fastcall func7(char *a1, unsigned __int16 a2)
{
return func1(a1, a2, 0);
}

original_code:
int tcpListen(const char *host, uint16_t port)
{
    return tcp_listen(host, port, 0);
}

canonical_original_code:
int tcpListen(char *host, unsigned short port)
{
    return tcp_listen(host, port, 0);
}


tcpConnect

decompiled_code:
__int64 __fastcall tcpConnect(char *a1, unsigned __int16 a2)
{
return tcp_connect(a1, a2, 0);
}

canonical_decompiled_code:
__int64 __fastcall func8(char *a1, unsigned __int16 a2)
{
return func2(a1, a2, 0);
}

original_code:
int tcpConnect(const char *host, uint16_t port)
{
    return tcp_connect(host, port, 0);
}

canonical_original_code:
int tcpConnect(char *host, unsigned short port)
{
    return tcp_connect(host, port, 0);
}


tcpAccept

decompiled_code:
__int64 __fastcall tcpAccept(int fd)
{
unsigned int v1; // ebx

do
v1 = accept(fd, 0LL, 0LL);
while ( v1 == -1 && *_errno_location() == 4 );
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func9(int fd)
{
unsigned int v1; // ebx

do
v1 = accept(fd, 0LL, 0LL);
while ( v1 == -1 && *_errno_location() == 4 );
return v1;
}

original_code:
int tcpAccept(int sd)
{
    int csd;
    do {
        csd = accept(sd, ((void *)0), ((void *)0));
    } while (csd == -1 && (*__errno_location ()) == 4);
    if (csd == -1) {
        if (0) _dbgError(stderr, "tcp.c", 270, __func__, "accept failed");
    }
    return csd;
}

variable_types: csd int


tcpRead

decompiled_code:
__int64 __fastcall tcpRead(int fd, __int64 a2, int a3)
{
unsigned int v4; // ebx
int *v5; // rax
int v6; // eax

v4 = 0;
while ( 1 )
{
v6 = read(fd, (void *)(a2 + (int)v4), (int)(a3 - v4));
if ( !v6 )
break;
if ( v6 <= 0 )
{
v5 = _errno_location();
if ( *v5 == 4 )
{
*v5 = 0;
}
else if ( *v5 == 11 )
{
*v5 = 0;
usleep(0x186A0u);
}
}
else
{
v4 += v6;
if ( a3 == v4 )
return v4;
}
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func10(int fd, __int64 a2, int a3)
{
unsigned int v4; // ebx
int *v5; // rax
int v6; // eax

v4 = 0;
while ( 1 )
{
v6 = read(fd, (void *)(a2 + (int)v4), (int)(a3 - v4));
if ( !v6 )
break;
if ( v6 <= 0 )
{
v5 = _errno_location();
if ( *v5 == 4 )
{
*v5 = 0;
}
else if ( *v5 == 11 )
{
*v5 = 0;
usleep(0x186A0u);
}
}
else
{
v4 += v6;
if ( a3 == v4 )
return v4;
}
}
return v4;
}

original_code:
int tcpRead(int fd, void *buf, int len)
{
    int res, n = 0;
    while (1) {
        if ((res = read(fd, (char *) buf + n, len - n)) == 0) {
            break;
        }
        else if (res > 0 && (n += res) == len) {
            break;
        }
        else if (res < 0 && (*__errno_location ()) == 4) {
            (*__errno_location ()) = 0;
            continue;
        }
        else if (res < 0 && ((*__errno_location ()) == 11 || (*__errno_location ()) == 11)) {
            (*__errno_location ()) = 0;
            usleep(100000);
            continue;
        }
    }
    if (res == -1) {
        if (0) _dbgError(stderr, "tcp.c", 302, __func__, "read failed");
        return -1;
    }
    else {
        return n;
    }
}

canonical_original_code:
int tcpRead(int fd, void *buf, int len)
{
    int res; int n = 0;
    while (1) {
        if ((res = read(fd, (char *) buf + n, len - n)) == 0) {
            break;
        }
        else if (res > 0 && (n += res) == len) {
            break;
        }
        else if (res < 0 && (*__errno_location ()) == 4) {
            (*__errno_location ()) = 0;
            continue;
        }
        else if (res < 0 && ((*__errno_location ()) == 11 || (*__errno_location ()) == 11)) {
            (*__errno_location ()) = 0;
            usleep(100000);
            continue;
        }
    }
    if (res == -1) {
        if (0) _dbgError(stderr, "tcp.c", 302, __func__, "read failed");
        return -1;
    }
    else {
        return n;
    }
}

variable_types: fd int

variable_types:
buf
void *

variable_types: len int

variable_types: res int

variable_types: n int


tcpWrite

decompiled_code:
__int64 __fastcall tcpWrite(int fd, __int64 a2, int a3)
{
int v4; // ebp
int v5; // ebx

v4 = 0;
do
{
v5 = write(fd, (const void *)(a2 + v4), a3 - v4);
if ( v5 <= 0 && *_errno_location() != 4 )
break;
v4 += v5;
}
while ( a3 > v4 );
if ( v5 == -1 )
v4 = -1;
return (unsigned int)v4;
}

canonical_decompiled_code:
__int64 __fastcall func11(int fd, __int64 a2, int a3)
{
int v4; // ebp
int v5; // ebx

v4 = 0;
do
{
v5 = write(fd, (const void *)(a2 + v4), a3 - v4);
if ( v5 <= 0 && *_errno_location() != 4 )
break;
v4 += v5;
}
while ( a3 > v4 );
if ( v5 == -1 )
v4 = -1;
return (unsigned int)v4;
}

original_code:
int tcpWrite(int fd, const void *buf, int len)
{
    int res, n = 0;
    do {
        res = write(fd, (const char *) buf + n, len - n);
    } while ((res > 0 || (*__errno_location ()) == 4) && (n += res) < len);
    if (res == -1) {
        if (0) _dbgError(stderr, "tcp.c", 322, __func__, "write failed");
        return -1;
    }
    else {
        return n;
    }
}

canonical_original_code:
int tcpWrite(int fd, void *buf, int len)
{
    int res; int n = 0;
    do {
        res = write(fd, (char *) buf + n, len - n);
    } while ((res > 0 || (*__errno_location ()) == 4) && (n += res) < len);
    if (res == -1) {
        if (0) _dbgError(stderr, "tcp.c", 322, __func__, "write failed");
        return -1;
    }
    else {
        return n;
    }
}


decompiled_code:
unsigned __int64 __fastcall as_increase_by(__int64 a1, __int64 a2)
{
unsigned __int64 v2; // rbx
unsigned __int64 result; // rax
size_t v4; // rax

v2 = *(_QWORD *)(a1 + 16);
for ( result = *(_QWORD *)(a1 + 8) + a2 + 1; result > v2; result = *(_QWORD *)(a1 + 8) + a2 + 1 )
{
v4 = 2 * v2;
if ( !v2 )
v4 = 16LL;
v2 = v4;
*(_QWORD *)a1 = realloc(*(void **)a1, v4);
*(_QWORD *)(a1 + 16) = v2;
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(__int64 a1, __int64 a2)
{
unsigned __int64 v2; // rbx
unsigned __int64 result; // rax
size_t v4; // rax

v2 = *(_QWORD *)(a1 + 16);
for ( result = *(_QWORD *)(a1 + 8) + a2 + 1; result > v2; result = *(_QWORD *)(a1 + 8) + a2 + 1 )
{
v4 = 2 * v2;
if ( !v2 )
v4 = 16LL;
v2 = v4;
*(_QWORD *)a1 = realloc(*(void **)a1, v4);
*(_QWORD *)(a1 + 16) = v2;
}
return result;
}

original_code:
static void as_increase_by(astring *str, size_t len)
{
    size_t new_len;
    while (str->used + len + 1 > str->size) {
        if (str->size == 0)
            new_len = 16;
        else
            new_len = 2 * str->size;
        str->data = realloc(str->data, new_len);
        str->size = new_len;
    }
}

canonical_original_code:
static void as_increase_by(struct astring *str, unsigned long len)
{
    unsigned long new_len;
    while (str->used + len + 1 > str->size) {
        if (str->size == 0)
            new_len = 16;
        else
            new_len = 2 * str->size;
        str->data = realloc(str->data, new_len);
        str->size = new_len;
    }
}

variable_types: struct astring { char *data; unsigned long used; unsigned long size; } *

variable_types:
len
unsigned long


asClear

decompiled_code:
void __fastcall asClear(__int64 a1)
{
free(*(void **)a1);
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
}

original_code:
void asClear(astring *str)
{
    free(str->data);
    memset(str, 0, sizeof(astring));
}

canonical_original_code:
void asClear(struct astring *str)
{
    free(str->data);
    memset(str, 0, sizeof(astring));
}

user_defined_types: struct astring { char *data; unsigned long used; unsigned long size; }


asDetach

decompiled_code:
char *__fastcall asDetach(char **a1)
{
char *result; // rax

result = *a1;
*a1 = 0LL;
a1[2] = 0LL;
a1[1] = 0LL;
if ( !result )
result = strdup(&LC0);
return result;
}

canonical_decompiled_code:
char *__fastcall func2(char **a1)
{
char *result; // rax

result = *a1;
*a1 = 0LL;
a1[2] = 0LL;
a1[1] = 0LL;
if ( !result )
result = strdup(&LC0);
return result;
}

original_code:
char *asDetach(astring *str)
{
    char *data = str->data;
    str->data = ((void *)0);
    str->size = 0;
    str->used = 0;
    return data ? data : strdup("");
}

canonical_original_code:
char *asDetach(struct astring *str)
{
    char *data = str->data;
    str->data = ((void *)0);
    str->size = 0;
    str->used = 0;
    return data ? data : strdup("");
}

variable_types:
data
char *


asFinish

decompiled_code:
char *__fastcall asFinish(char **a1)
{
char *v1; // rbx

v1 = *a1;
free(a1);
if ( !v1 )
v1 = strdup(&LC0);
return v1;
}

canonical_decompiled_code:
char *__fastcall func3(char **a1)
{
char *v1; // rbx

v1 = *a1;
free(a1);
if ( !v1 )
v1 = strdup(&LC0);
return v1;
}

original_code:
char *asFinish(astring *str)
{
    char *data = str->data;
    free(str);
    return data ? data : strdup("");
}

canonical_original_code:
char *asFinish(struct astring *str)
{
    char *data = str->data;
    free(str);
    return data ? data : strdup("");
}


asDestroy

decompiled_code:
void __fastcall asDestroy(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func4(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

original_code:
void asDestroy(astring *str)
{
    free(str->data);
    free(str);
}

canonical_original_code:
void asDestroy(struct astring *str)
{
    free(str->data);
    free(str);
}


asAddF

decompiled_code:
_QWORD *asAddF(_QWORD *a1, const char *a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
asAddV(a1, a2, (const __m128i *)va);
return a1;
}

canonical_decompiled_code:
_QWORD *func5(_QWORD *a1, const char *a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
asAddV(a1, a2, (const __m128i *)va);
return a1;
}

original_code:
__attribute__((format (printf, 2, 3)))
astring *asAddF(astring *str, const char *fmt, ...)
{
    va_list ap;
    __builtin_va_start(ap,fmt);
    asAddV(str, fmt, ap);
    __builtin_va_end(ap);
    return str;
}

canonical_original_code:
struct astring *asAddF(struct astring *str, char *fmt, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,fmt);
    asAddV(str, fmt, ap);
    __builtin_va_end(ap);
    return str;
}

variable_types:
str
struct astring *

variable_types:
fmt
char *

return_type: struct astring { char *data; unsigned long used; unsigned long size; } *


asGet

decompiled_code:
const char *__fastcall asGet(const char **a1)
{
const char *result; // rax

result = *a1;
if ( !*a1 )
result = &LC0;
return result;
}

canonical_decompiled_code:
const char *__fastcall func6(const char **a1)
{
const char *result; // rax

result = *a1;
if ( !*a1 )
result = &LC0;
return result;
}

original_code:
const char *asGet(const astring *str)
{
    return str->data ? str->data : "";
}

canonical_original_code:
char *asGet(struct astring *str)
{
    return str->data ? str->data : "";
}


asRewind

decompiled_code:
__int64 __fastcall asRewind(__int64 a1)
{
__int64 result; // rax

result = a1;
if ( *(_QWORD *)a1 )
**(_BYTE **)a1 = 0;
*(_QWORD *)(a1 + 8) = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
__int64 result; // rax

result = a1;
if ( *(_QWORD *)a1 )
**(_BYTE **)a1 = 0;
*(_QWORD *)(a1 + 8) = 0LL;
return result;
}

original_code:
astring *asRewind(astring *str)
{
    if (str->data != ((void *)0)) str->data[0] = 0;
    str->used = 0;
    return str;
}

canonical_original_code:
struct astring *asRewind(struct astring *str)
{
    if (str->data != ((void *)0)) str->data[0] = 0;
    str->used = 0;
    return str;
}


asCreate

decompiled_code:
_QWORD *asCreate(const char *a1, ...)
{
_QWORD *v1; // rbx
gcc_va_list va; // [rsp+8h] [rbp-E0h] BYREF

v1 = calloc(1uLL, 0x18uLL);
if ( a1 )
{
va_start(va, a1);
asSetV(v1, a1, (const __m128i *)va);
}
return v1;
}

canonical_decompiled_code:
_QWORD *func8(const char *a1, ...)
{
_QWORD *v1; // rbx
gcc_va_list va; // [rsp+8h] [rbp-E0h] BYREF

v1 = calloc(1uLL, 0x18uLL);
if ( a1 )
{
va_start(va, a1);
asSetV(v1, a1, (const __m128i *)va);
}
return v1;
}

original_code:
__attribute__((format (printf, 1, 2)))
astring *asCreate(const char *fmt, ...)
{
    astring *str = calloc(1, sizeof(*str));
    if (fmt) {
        va_list ap;
        __builtin_va_start(ap,fmt);
        asSetV(str, fmt, ap);
        __builtin_va_end(ap);
    }
    return str;
}

canonical_original_code:
struct astring *asCreate(char *fmt, ...)
{
    struct astring *str = calloc(1, sizeof(*str));
    if (fmt) {
        __builtin_va_list ap;
        __builtin_va_start(ap,fmt);
        asSetV(str, fmt, ap);
        __builtin_va_end(ap);
    }
    return str;
}


asInit

decompiled_code:
void **asInit(void **a1, const char *a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-E0h] BYREF

a1[2] = &loc_E + 2;
*a1 = calloc(1uLL, 0x10uLL);
a1[1] = 0LL;
if ( a2 )
{
va_start(va, a2);
asSetV(a1, a2, (const __m128i *)va);
}
return a1;
}

canonical_decompiled_code:
void **func9(void **a1, const char *a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-E0h] BYREF

a1[2] = &loc_E + 2;
*a1 = calloc(1uLL, 0x10uLL);
a1[1] = 0LL;
if ( a2 )
{
va_start(va, a2);
asSetV(a1, a2, (const __m128i *)va);
}
return a1;
}

original_code:
astring *asInit(astring *str, const char *fmt, ...)
{
    str->size = 16;
    str->data = calloc(1, str->size);
    str->used = 0;
    if (fmt) {
        va_list ap;
        __builtin_va_start(ap,fmt);
        asSetV(str, fmt, ap);
        __builtin_va_end(ap);
    }
    return str;
}

canonical_original_code:
struct astring *asInit(struct astring *str, char *fmt, ...)
{
    str->size = 16;
    str->data = calloc(1, str->size);
    str->used = 0;
    if (fmt) {
        __builtin_va_list ap;
        __builtin_va_start(ap,fmt);
        asSetV(str, fmt, ap);
        __builtin_va_end(ap);
    }
    return str;
}


asSetF

decompiled_code:
_QWORD *asSetF(_QWORD *a1, const char *a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
asSetV(a1, a2, (const __m128i *)va);
return a1;
}

canonical_decompiled_code:
_QWORD *func10(_QWORD *a1, const char *a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
asSetV(a1, a2, (const __m128i *)va);
return a1;
}

original_code:
__attribute__((format (printf, 2, 3)))
astring *asSetF(astring *str, const char *fmt, ...)
{
    va_list ap;
    __builtin_va_start(ap,fmt);
    asSetV(str, fmt, ap);
    __builtin_va_end(ap);
    return str;
}

canonical_original_code:
struct astring *asSetF(struct astring *str, char *fmt, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,fmt);
    asSetV(str, fmt, ap);
    __builtin_va_end(ap);
    return str;
}


asLen

decompiled_code:
__int64 __fastcall asLen(__int64 a1)
{
return *(_QWORD *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
return *(_QWORD *)(a1 + 8);
}

original_code:
int asLen(const astring *str)
{
    return str->used;
}

canonical_original_code:
int asLen(struct astring *str)
{
    return str->used;
}


asIsEmpty

decompiled_code:
_BOOL8 __fastcall asIsEmpty(__int64 a1)
{
return *(_QWORD *)(a1 + 8) == 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func12(__int64 a1)
{
return *(_QWORD *)(a1 + 8) == 0LL;
}

original_code:
int asIsEmpty(const astring *str)
{
    return str->used == 0;
}

canonical_original_code:
int asIsEmpty(struct astring *str)
{
    return str->used == 0;
}


asStrip

decompiled_code:
char **__fastcall asStrip(char **a1, unsigned __int64 a2, unsigned __int64 a3)
{
char *v4; // rdi
char *v6; // rdx
unsigned __int64 v7; // rdx
char *v8; // rax

v4 = *a1;
if ( v4 )
{
v6 = a1[1];
if ( (unsigned __int64)v6 <= a2 )
a2 = (unsigned __int64)a1[1];
v7 = (unsigned __int64)&v6[-a2];
if ( v7 <= a3 )
a3 = v7;
memmove(v4, &v4[a2], v7 - a3);
v8 = &a1[1][-a2 - a3];
a1[1] = v8;
v8[(_QWORD)*a1] = 0;
}
return a1;
}

canonical_decompiled_code:
char **__fastcall func13(char **a1, unsigned __int64 a2, unsigned __int64 a3)
{
char *v4; // rdi
char *v6; // rdx
unsigned __int64 v7; // rdx
char *v8; // rax

v4 = *a1;
if ( v4 )
{
v6 = a1[1];
if ( (unsigned __int64)v6 <= a2 )
a2 = (unsigned __int64)a1[1];
v7 = (unsigned __int64)&v6[-a2];
if ( v7 <= a3 )
a3 = v7;
memmove(v4, &v4[a2], v7 - a3);
v8 = &a1[1][-a2 - a3];
a1[1] = v8;
v8[(_QWORD)*a1] = 0;
}
return a1;
}

original_code:
astring *asStrip(astring *str, size_t left, size_t right)
{
    if (str->data == ((void *)0)) return str;
    if (left > str->used) left = str->used;
    if (right > str->used - left) right = str->used - left;
    memmove(str->data, str->data + left, str->used - left - right);
    str->used -= (left + right);
    *(str->data + str->used) = '\0';
    return str;
}

canonical_original_code:
struct astring *asStrip(struct astring *str, unsigned long left, unsigned long right)
{
    if (str->data == ((void *)0)) return str;
    if (left > str->used) left = str->used;
    if (right > str->used - left) right = str->used - left;
    memmove(str->data, str->data + left, str->used - left - right);
    str->used -= (left + right);
    *(str->data + str->used) = '\0';
    return str;
}

variable_types:
left
unsigned long

variable_types:
right
unsigned long


asCompare

decompiled_code:
int __fastcall asCompare(const char **a1, const char **a2)
{
int v2; // ebx
const char *v3; // r12
const char *v4; // rax

v2 = asLen((__int64)a1);
if ( v2 < (int)asLen((__int64)a2) )
return -1;
v3 = asGet(a2);
v4 = asGet(a1);
return memcmp(v4, v3, v2);
}

canonical_decompiled_code:
int __fastcall func14(const char **a1, const char **a2)
{
int v2; // ebx
const char *v3; // r12
const char *v4; // rax

v2 = func11((__int64)a1);
if ( v2 < (int)func11((__int64)a2) )
return -1;
v3 = func6(a2);
v4 = func6(a1);
return memcmp(v4, v3, v2);
}

original_code:
int asCompare(const astring *left, const astring *right)
{
    int len1 = asLen(left);
    int len2 = asLen(right);
    if (len1 < len2)
        return -1;
    else if (len2 > len1)
        return 1;
    else
        return memcmp(asGet(left), asGet(right), len1);
}

canonical_original_code:
int asCompare(struct astring *left, struct astring *right)
{
    int len1 = asLen(left);
    int len2 = asLen(right);
    if (len1 < len2)
        return -1;
    else if (len2 > len1)
        return 1;
    else
        return memcmp(asGet(left), asGet(right), len1);
}

variable_types:
right
struct astring *


asStartsWith

decompiled_code:
bool asStartsWith(const char **a1, const char *a2, ...)
{
char *v2; // r12
size_t v3; // rbx
const char *v4; // rax
char *s; // [rsp+0h] [rbp-E8h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, a2);
vasprintf(&s, a2, arg);
v2 = s;
v3 = strlen(s);
v4 = asGet(a1);
LODWORD(v3) = strncmp(v4, v2, v3);
free(s);
return (_DWORD)v3 == 0;
}

canonical_decompiled_code:
bool func15(const char **a1, const char *a2, ...)
{
char *v2; // r12
size_t v3; // rbx
const char *v4; // rax
char *s; // [rsp+0h] [rbp-E8h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, a2);
vasprintf(&s, a2, arg);
v2 = s;
v3 = strlen(s);
v4 = func6(a1);
LODWORD(v3) = strncmp(v4, v2, v3);
free(s);
return (_DWORD)v3 == 0;
}

original_code:
__attribute__((format (printf, 2, 3)))
_Bool asStartsWith(const astring *str, const char *fmt, ...)
{
    va_list ap;
    char *pat;
    int r;
    __builtin_va_start(ap,fmt);
    r = vasprintf(&pat, fmt, ap);
    __builtin_va_end(ap);
    r = strncmp(asGet(str), pat, strlen(pat));
    free(pat);
    return r == 0;
}

canonical_original_code:
_Bool asStartsWith(struct astring *str, char *fmt, ...)
{
    __builtin_va_list ap;
    char *pat;
    int r;
    __builtin_va_start(ap,fmt);
    r = vasprintf(&pat, fmt, ap);
    __builtin_va_end(ap);
    r = strncmp(asGet(str), pat, strlen(pat));
    free(pat);
    return r == 0;
}

variable_types:
pat
char *


asEndsWith

decompiled_code:
bool asEndsWith(const char **a1, const char *a2, ...)
{
char *v2; // r12
const char *v3; // rbp
int v4; // ebx
char *v5; // r13
size_t v6; // rax
int v7; // ebx
char *s; // [rsp+0h] [rbp-F8h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF

va_start(arg, a2);
vasprintf(&s, a2, arg);
v2 = s;
v3 = asGet(a1);
v4 = asLen((__int64)a1);
v5 = s;
v6 = strlen(s);
v7 = strcmp(&v3[v4 - v6], v2);
free(v5);
return v7 == 0;
}

canonical_decompiled_code:
bool func16(const char **a1, const char *a2, ...)
{
char *v2; // r12
const char *v3; // rbp
int v4; // ebx
char *v5; // r13
size_t v6; // rax
int v7; // ebx
char *s; // [rsp+0h] [rbp-F8h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF

va_start(arg, a2);
vasprintf(&s, a2, arg);
v2 = s;
v3 = func6(a1);
v4 = func11((__int64)a1);
v5 = s;
v6 = strlen(s);
v7 = strcmp(&v3[v4 - v6], v2);
free(v5);
return v7 == 0;
}

original_code:
__attribute__((format (printf, 2, 3)))
_Bool asEndsWith(const astring *str, const char *fmt, ...)
{
    va_list ap;
    char *pat;
    int r;
    __builtin_va_start(ap,fmt);
    r = vasprintf(&pat, fmt, ap);
    __builtin_va_end(ap);
    r = strcmp(asGet(str) + asLen(str) - strlen(pat), pat);
    free(pat);
    return r == 0;
}

canonical_original_code:
_Bool asEndsWith(struct astring *str, char *fmt, ...)
{
    __builtin_va_list ap;
    char *pat;
    int r;
    __builtin_va_start(ap,fmt);
    r = vasprintf(&pat, fmt, ap);
    __builtin_va_end(ap);
    r = strcmp(asGet(str) + asLen(str) - strlen(pat), pat);
    free(pat);
    return r == 0;
}

variable_types: r int


tsNormalized

original_code:
struct timespec tsNormalized(struct timespec ts)
{
    struct timespec dup = ts;
    while (dup.tv_nsec < 0) {
        dup.tv_sec--;
        dup.tv_nsec += 1000000000L;
    }
    while (dup.tv_nsec >= 1000000000L) {
        dup.tv_sec++;
        dup.tv_nsec -= 1000000000L;
    }
    return dup;
}

variable_types:
ts
struct timespec

variable_types: struct timespec { long tv_sec; long tv_nsec; }


tsNormalize

decompiled_code:
__int64 __fastcall tsNormalize(__int64 *a1)
{
__int64 result; // rax
__int64 v2; // rdx

result = tsNormalized(*a1, a1[1]);
*a1 = result;
a1[1] = v2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 *a1)
{
__int64 result; // rax
__int64 v2; // rdx

result = func0(*a1, a1[1]);
*a1 = result;
a1[1] = v2;
return result;
}

original_code:
void tsNormalize(struct timespec *ts)
{
    *ts = tsNormalized(*ts);
}

variable_types: struct timespec { long tv_sec; long tv_nsec; } *


tsMake

decompiled_code:
__int64 __fastcall tsMake(__int64 a1, __int64 a2)
{
__int64 v3[3]; // [rsp+0h] [rbp-18h] BYREF

v3[0] = a1;
v3[1] = a2;
tsNormalize(v3);
return v3[0];
}

original_code:
struct timespec tsMake(time_t sec, long nsec)
{
    struct timespec t = {
        .tv_sec = sec,
        .tv_nsec = nsec
    };
    tsNormalize(&t);
    return t;
}

canonical_original_code:
struct timespec tsMake(long sec, long nsec)
{
    struct timespec t = {
        .tv_sec = sec,
        .tv_nsec = nsec
    };
    tsNormalize(&t);
    return t;
}

variable_types: sec long

variable_types: nsec long

return_type: struct timespec { long tv_sec; long tv_nsec; }


tsNow

decompiled_code:
__time_t tsNow()
{
struct timespec v1; // [rsp+0h] [rbp-18h] BYREF

v1.tv_sec = 0LL;
v1.tv_nsec = 0LL;
clock_gettime(0, &v1);
return v1.tv_sec;
}

canonical_decompiled_code:
__time_t func3()
{
struct timespec v1; // [rsp+0h] [rbp-18h] BYREF

v1.tv_sec = 0LL;
v1.tv_nsec = 0LL;
clock_gettime(0, &v1);
return v1.tv_sec;
}

original_code:
struct timespec tsNow(void)
{
    struct timespec ts = { 0 };
    clock_gettime(0, &ts);
    return ts;
}

canonical_original_code:
struct timespec tsNow()
{
    struct timespec ts = { 0 };
    clock_gettime(0, &ts);
    return ts;
}

user_defined_types: struct timespec { long tv_sec; long tv_nsec; }


tsCompare

decompiled_code:
__int64 __fastcall tsCompare(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( a1 >= a3 )
{
result = 1LL;
if ( a1 <= a3 )
{
result = 0xFFFFFFFFLL;
if ( a2 >= a4 )
result = a2 > a4;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 result; // rax

result = 0xFFFFFFFFLL;
if ( a1 >= a3 )
{
result = 1LL;
if ( a1 <= a3 )
{
result = 0xFFFFFFFFLL;
if ( a2 >= a4 )
result = a2 > a4;
}
}
return result;
}

original_code:
int tsCompare(struct timespec t1, struct timespec t0)
{
    if (t1.tv_sec < t0.tv_sec) {
        return -1;
    }
    else if (t1.tv_sec > t0.tv_sec) {
        return 1;
    }
    else if (t1.tv_nsec < t0.tv_nsec) {
        return -1;
    }
    else if (t1.tv_nsec > t0.tv_nsec) {
        return 1;
    }
    else {
        return 0;
    }
}


tsDelta

decompiled_code:
double __fastcall tsDelta(int a1, int a2, int a3, int a4)
{
return (double)(a2 - a4) / 1000000000.0 + (double)(a1 - a3);
}

canonical_decompiled_code:
double __fastcall func5(int a1, int a2, int a3, int a4)
{
return (double)(a2 - a4) / 1000000000.0 + (double)(a1 - a3);
}

original_code:
double tsDelta(struct timespec t1, struct timespec t0)
{
    return t1.tv_sec - t0.tv_sec
         + (double) (t1.tv_nsec - t0.tv_nsec) / 1000000000L;
}


tsDec

decompiled_code:
__int64 __fastcall tsDec(__int64 a1, __int64 a2, double a3)
{
__int64 v4[3]; // [rsp+0h] [rbp-18h] BYREF

v4[0] = a1 - (unsigned int)(int)a3;
v4[1] = a2 - (unsigned int)(int)(fmod(a3, 1.0) * 1000000000.0);
tsNormalize(v4);
return v4[0];
}

original_code:
struct timespec tsDec(struct timespec ts, double seconds)
{
    ts.tv_sec -= (time_t) seconds;
    ts.tv_nsec -= (long) (1000000000L * fmod(seconds, 1));
    tsNormalize(&ts);
    return ts;
}

canonical_original_code:
struct timespec tsDec(struct timespec ts, double seconds)
{
    ts.tv_sec -= (long) seconds;
    ts.tv_nsec -= (long) (1000000000L * fmod(seconds, 1));
    tsNormalize(&ts);
    return ts;
}

variable_types: seconds double


tsInc

decompiled_code:
__int64 __fastcall tsInc(__int64 a1, __int64 a2, double a3)
{
__int64 v4[3]; // [rsp+0h] [rbp-18h] BYREF

v4[0] = (unsigned int)(int)a3 + a1;
v4[1] = (unsigned int)(int)(fmod(a3, 1.0) * 1000000000.0) + a2;
tsNormalize(v4);
return v4[0];
}

original_code:
struct timespec tsInc(struct timespec ts, double seconds)
{
    ts.tv_sec += (time_t) seconds;
    ts.tv_nsec += (long) (1000000000L * fmod(seconds, 1));
    tsNormalize(&ts);
    return ts;
}

canonical_original_code:
struct timespec tsInc(struct timespec ts, double seconds)
{
    ts.tv_sec += (long) seconds;
    ts.tv_nsec += (long) (1000000000L * fmod(seconds, 1));
    tsNormalize(&ts);
    return ts;
}


tsToDouble

decompiled_code:
double __fastcall tsToDouble(int a1, int a2)
{
return (double)a2 / 1000000000.0 + (double)a1;
}

canonical_decompiled_code:
double __fastcall func9(int a1, int a2)
{
return (double)a2 / 1000000000.0 + (double)a1;
}

original_code:
double tsToDouble(struct timespec ts)
{
    return (double) ts.tv_sec + (double) ts.tv_nsec / 1000000000L;
}


tsFromTimeval

decompiled_code:
__int64 __fastcall tsFromTimeval(__int64 a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
return a1;
}

original_code:
struct timespec tsFromTimeval(struct timeval tv)
{
    struct timespec ts = {
        .tv_sec = tv.tv_sec,
        .tv_nsec = tv.tv_usec * 1000
    };
    return ts;
}

variable_types: struct timeval { long tv_sec; long tv_usec; }


tsFormatC

decompiled_code:
__int64 tsFormatC()
{
return t_format_c();
}

canonical_decompiled_code:
__int64 func11()
{
return t_format_c();
}

original_code:
const char *tsFormatC(const struct timespec ts, const char *tz,
                      const char *fmt)
{
    return t_format_c(ts.tv_sec, ts.tv_nsec, tz, fmt);
}

canonical_original_code:
char *tsFormatC(struct timespec ts, char *tz, char *fmt)
{
    return t_format_c(ts.tv_sec, ts.tv_nsec, tz, fmt);
}

variable_types:
tz
char *


tsFormat

decompiled_code:
char *tsFormat()
{
const char *v0; // rax

v0 = (const char *)tsFormatC();
return strdup(v0);
}

canonical_decompiled_code:
char *func12()
{
const char *v0; // rax

v0 = (const char *)func11();
return strdup(v0);
}

original_code:
char *tsFormat(const struct timespec ts, const char *tz, const char *fmt)
{
    return strdup(tsFormatC(ts, tz, fmt));
}

canonical_original_code:
char *tsFormat(struct timespec ts, char *tz, char *fmt)
{
    return strdup(tsFormatC(ts, tz, fmt));
}


decompiled_code:
__int64 __fastcall find_branch_between(__int64 a1, unsigned __int8 a2, int a3, int a4)
{
__int64 v4; // r10
unsigned __int8 v5; // al
unsigned __int8 v6; // r9
unsigned int v7; // er8
unsigned int v8; // eax
unsigned int v9; // er8
unsigned __int8 v10; // r9

if ( a3 > a4 )
return (unsigned int)-1;
v4 = *(_QWORD *)(a1 + 16);
v5 = **(_BYTE **)(v4 + 8LL * a3);
v6 = **(_BYTE **)(v4 + 8LL * a4);
if ( v6 < a2 || v5 > a2 )
return (unsigned int)-1;
v7 = a3;
if ( v5 == a2 )
return v7;
v7 = a4;
if ( v6 == a2 )
return v7;
v8 = (a3 + a4) / 2;
v9 = v8;
v10 = **(_BYTE **)(v4 + 8LL * (int)v8);
if ( a2 != v10 )
{
if ( a2 >= v10 )
v9 = find_branch_between(a1, a2, v8 + 1, (unsigned int)(a4 - 1), v8);
else
v9 = find_branch_between(a1, a2, (unsigned int)(a3 + 1), v8 - 1, v8);
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned __int8 a2, int a3, int a4)
{
__int64 v4; // r10
unsigned __int8 v5; // al
unsigned __int8 v6; // r9
unsigned int v7; // er8
unsigned int v8; // eax
unsigned int v9; // er8
unsigned __int8 v10; // r9

if ( a3 > a4 )
return (unsigned int)-1;
v4 = *(_QWORD *)(a1 + 16);
v5 = **(_BYTE **)(v4 + 8LL * a3);
v6 = **(_BYTE **)(v4 + 8LL * a4);
if ( v6 < a2 || v5 > a2 )
return (unsigned int)-1;
v7 = a3;
if ( v5 == a2 )
return v7;
v7 = a4;
if ( v6 == a2 )
return v7;
v8 = (a3 + a4) / 2;
v9 = v8;
v10 = **(_BYTE **)(v4 + 8LL * (int)v8);
if ( a2 != v10 )
{
if ( a2 >= v10 )
v9 = func0(a1, a2, v8 + 1, (unsigned int)(a4 - 1), v8);
else
v9 = func0(a1, a2, (unsigned int)(a3 + 1), v8 - 1, v8);
}
return v9;
}

original_code:
static int find_branch_between(const Tree *tree, uint8_t id,
        int low_index, int high_index)
{
    if (low_index > high_index) return -1;
    uint8_t low_id = tree->branch[low_index]->id;
    uint8_t high_id = tree->branch[high_index]->id;
    if (id < low_id) return -1;
    if (id > high_id) return -1;
    if (id == low_id) return low_index;
    if (id == high_id) return high_index;
    int middle_index = (low_index + high_index) / 2;
    uint8_t middle_id = tree->branch[middle_index]->id;
    if (id == middle_id)
        return middle_index;
    else if (id < middle_id)
        return find_branch_between(tree, id, low_index + 1, middle_index - 1);
    else
        return find_branch_between(tree, id, middle_index + 1, high_index - 1);
}

canonical_original_code:
static int find_branch_between(struct Tree *tree, unsigned char id, int low_index, int high_index)
{
    if (low_index > high_index) return -1;
    unsigned char low_id = tree->branch[low_index]->id;
    unsigned char high_id = tree->branch[high_index]->id;
    if (id < low_id) return -1;
    if (id > high_id) return -1;
    if (id == low_id) return low_index;
    if (id == high_id) return high_index;
    int middle_index = (low_index + high_index) / 2;
    unsigned char middle_id = tree->branch[middle_index]->id;
    if (id == middle_id)
        return middle_index;
    else if (id < middle_id)
        return find_branch_between(tree, id, low_index + 1, middle_index - 1);
    else
        return find_branch_between(tree, id, middle_index + 1, high_index - 1);
}

variable_types: struct Tree { unsigned char id; void *data; struct Tree **branch; int branch_count; } *

variable_types:
id
unsigned char


decompiled_code:
__int64 __fastcall find_branch(__int64 a1, unsigned __int8 a2)
{
return find_branch_between(a1, a2, 0, *(_DWORD *)(a1 + 24) - 1);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned __int8 a2)
{
return func0(a1, a2, 0, *(_DWORD *)(a1 + 24) - 1);
}

original_code:
static int find_branch(const Tree *tree, uint8_t id)
{
    return find_branch_between(tree, id, 0, tree->branch_count - 1);
}

canonical_original_code:
static int find_branch(struct Tree *tree, unsigned char id)
{
    return find_branch_between(tree, id, 0, tree->branch_count - 1);
}

user_defined_types: struct Tree { unsigned char id; void *data; struct Tree **branch; int branch_count; }


decompiled_code:
__int64 __fastcall find_leaf(__int64 a1, unsigned __int8 *a2, __int64 a3)
{
__int64 v3; // rbx
int v6; // eax

v3 = a1;
if ( a3 )
{
v6 = find_branch(a1, *a2);
if ( v6 == -1 )
v3 = 0LL;
else
v3 = find_leaf(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL * v6), a2 + 1, a3 - 1);
}
return v3;
}

original_code:
static Tree *find_leaf(Tree *tree, const void *key, size_t key_size)
{
    if (key_size == 0) return tree;
    uint8_t id = ((uint8_t *) key)[0];
    int index = find_branch(tree, id);
    if (index == -1) {
        return ((void *)0);
    }
    else {
        return find_leaf(tree->branch[index],
                         ((uint8_t *) key) + 1, key_size - 1);
    }
}

canonical_original_code:
static struct Tree *find_leaf(struct Tree *tree, void *key, unsigned long key_size)
{
    if (key_size == 0) return tree;
    unsigned char id = ((uint8_t *) key)[0];
    int index = find_branch(tree, id);
    if (index == -1) {
        return ((void *)0);
    }
    else {
        return find_leaf(tree->branch[index],
                         ((unsigned char *) key) + 1, key_size - 1);
    }
}

variable_types:
tree
struct Tree *

variable_types:
key
void *

variable_types: index int

return_type: struct Tree { unsigned char id; void *data; struct Tree **branch; int branch_count; } *


original_code:
static void insert_branch(Tree *tree, Tree *branch, int at_index)
{
    tree->branch = realloc(tree->branch,
            (tree->branch_count + 1) * sizeof(Tree *));
    memmove(tree->branch + at_index + 1,
            tree->branch + at_index,
            (tree->branch_count - at_index) * sizeof(Tree *));
    tree->branch[at_index] = branch;
    tree->branch_count++;
}

canonical_original_code:
static void insert_branch(struct Tree *tree, struct Tree *branch, int at_index)
{
    tree->branch = realloc(tree->branch,
            (tree->branch_count + 1) * sizeof(struct Tree *));
    memmove(tree->branch + at_index + 1,
            tree->branch + at_index,
            (tree->branch_count - at_index) * sizeof(struct Tree *));
    tree->branch[at_index] = branch;
    tree->branch_count++;
}

variable_types:
branch
struct Tree *


decompiled_code:
__int64 __fastcall find_or_add_leaf(__int64 a1, unsigned __int8 *a2, __int64 a3)
{
__int64 v3; // rbx
unsigned __int8 v6; // r13
int v7; // eax
_BYTE *v8; // rdi

v3 = a1;
if ( a3 )
{
v6 = *a2;
v7 = find_branch(a1, *a2);
if ( v7 == -1 )
v8 = add_branch(a1, v6);
else
v8 = *(_BYTE **)(*(_QWORD *)(a1 + 16) + 8LL * v7);
v3 = find_or_add_leaf(v8, a2 + 1, a3 - 1);
}
return v3;
}

original_code:
static Tree *find_or_add_leaf(Tree *tree, const void *key, size_t key_size)
{
    if (key_size == 0) return tree;
    uint8_t id = ((uint8_t *) key)[0];
    int index = find_branch(tree, id);
    Tree *branch;
    if (index == -1) {
        branch = add_branch(tree, id);
    }
    else {
        branch = tree->branch[index];
    }
    return find_or_add_leaf(branch, ((uint8_t *) key) + 1, key_size - 1);
}

canonical_original_code:
static struct Tree *find_or_add_leaf(struct Tree *tree, void *key, unsigned long key_size)
{
    if (key_size == 0) return tree;
    unsigned char id = ((uint8_t *) key)[0];
    int index = find_branch(tree, id);
    struct Tree *branch;
    if (index == -1) {
        branch = add_branch(tree, id);
    }
    else {
        branch = tree->branch[index];
    }
    return find_or_add_leaf(branch, ((unsigned char *) key) + 1, key_size - 1);
}


decompiled_code:
void __fastcall destroy_iter_node(void *ptr)
{
free(*((void **)ptr + 3));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func6(void *ptr)
{
free(*((void **)ptr + 3));
free(ptr);
}

original_code:
static void destroy_iter_node(TreeIterNode *node)
{
    free(node->key);
    free(node);
}

canonical_original_code:
static void destroy_iter_node(struct TreeIterNode *node)
{
    free(node->key);
    free(node);
}

variable_types: struct TreeIterNode { struct ListNode { struct ListNode *next; struct ListNode *prev; struct List { struct ListNode *head; struct ListNode *tail; int length; } *list; } _node; char *key; unsigned long key_size; } *

user_defined_types: struct TreeIterNode { struct ListNode _node; char *key; unsigned long key_size; }


decompiled_code:
__int64 __fastcall delete_leaf(__int64 a1, unsigned __int8 *a2, __int64 a3)
{
__int64 result; // rax
int v5; // er13
__int64 v6; // r14

if ( a3 )
{
v5 = find_branch(a1, *a2);
result = 1LL;
if ( v5 != -1 )
{
v6 = *(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL * v5);
result = delete_leaf(v6, a2 + 1, a3 - 1);
if ( (_DWORD)result )
{
result = 1LL;
}
else if ( !*(_QWORD *)(v6 + 8) && !*(_DWORD *)(v6 + 24) )
{
remove_branch(a1, v5);
free((void *)v6);
result = 0LL;
}
}
}
else
{
result = 1LL;
if ( *(_QWORD *)(a1 + 8) )
{
*(_QWORD *)(a1 + 8) = 0LL;
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, unsigned __int8 *a2, __int64 a3)
{
__int64 result; // rax
int v5; // er13
__int64 v6; // r14

if ( a3 )
{
v5 = func1(a1, *a2);
result = 1LL;
if ( v5 != -1 )
{
v6 = *(_QWORD *)(*(_QWORD *)(a1 + 16) + 8LL * v5);
result = func7(v6, a2 + 1, a3 - 1);
if ( (_DWORD)result )
{
result = 1LL;
}
else if ( !*(_QWORD *)(v6 + 8) && !*(_DWORD *)(v6 + 24) )
{
func5(a1, v5);
free((void *)v6);
result = 0LL;
}
}
}
else
{
result = 1LL;
if ( *(_QWORD *)(a1 + 8) )
{
*(_QWORD *)(a1 + 8) = 0LL;
result = 0LL;
}
}
return result;
}

original_code:
static int delete_leaf(Tree *tree, const void *key, size_t key_size)
{
    if (key_size > 0) {
        uint8_t id = ((uint8_t *) key)[0];
        int index = find_branch(tree, id);
        if (index == -1) return 1;
        Tree *branch = tree->branch[index];
        if (delete_leaf(branch, ((uint8_t *) key) + 1, key_size - 1) != 0) {
            return 1;
        }
        if (branch->data == ((void *)0) && branch->branch_count == 0) {
            remove_branch(tree, index);
            free(branch);
        }
        return 0;
    }
    else {
        if (tree->data == ((void *)0)) {
            return 1;
        }
        else {
            tree->data = ((void *)0);
            return 0;
        }
    }
}

canonical_original_code:
static int delete_leaf(struct Tree *tree, void *key, unsigned long key_size)
{
    if (key_size > 0) {
        unsigned char id = ((uint8_t *) key)[0];
        int index = find_branch(tree, id);
        if (index == -1) return 1;
        struct Tree *branch = tree->branch[index];
        if (delete_leaf(branch, ((unsigned char *) key) + 1, key_size - 1) != 0) {
            return 1;
        }
        if (branch->data == ((void *)0) && branch->branch_count == 0) {
            remove_branch(tree, index);
            free(branch);
        }
        return 0;
    }
    else {
        if (tree->data == ((void *)0)) {
            return 1;
        }
        else {
            tree->data = ((void *)0);
            return 0;
        }
    }
}


original_code:
static void tree_prune(Tree *tree)
{
    for (int i = 0; i < tree->branch_count; i++) {
        Tree *branch = tree->branch[i];
        tree_prune(branch);
        if (branch->data == ((void *)0) && branch->branch_count == 0) {
            remove_branch(tree, i);
            free(branch);
        }
    }
}

canonical_original_code:
static void tree_prune(struct Tree *tree)
{
    for (int i = 0; i < tree->branch_count; i++) {
        struct Tree *branch = tree->branch[i];
        tree_prune(branch);
        if (branch->data == ((void *)0) && branch->branch_count == 0) {
            remove_branch(tree, i);
            free(branch);
        }
    }
}

variable_types: i int


original_code:
static void collect_leaves(const Tree *branch,
        uint8_t *key, size_t key_size, List *nodes)
{
    uint8_t sub_key[key_size + 1];
    memcpy(sub_key, key, key_size);
    for (int i = 0; i < branch->branch_count; i++) {
        Tree *sub_branch = branch->branch[i];
        sub_key[key_size] = sub_branch->id;
        if (sub_branch->data != ((void *)0)) {
            TreeIterNode *node = calloc(1, sizeof(TreeIterNode));
            node->key = memdup(sub_key, key_size + 1);
            node->key_size = key_size + 1;
            _listAppendTail((nodes), &(node)->_node);
        }
        collect_leaves(sub_branch, sub_key, key_size + 1, nodes);
    }
}

canonical_original_code:
static void collect_leaves(struct Tree *branch, unsigned char *key, unsigned long key_size, struct List *nodes)
{
    unsigned char sub_key[-1];
    memcpy(sub_key, key, key_size);
    for (int i = 0; i < branch->branch_count; i++) {
        struct Tree *sub_branch = branch->branch[i];
        sub_key[key_size] = sub_branch->id;
        if (sub_branch->data != ((void *)0)) {
            struct TreeIterNode *node = calloc(1, sizeof(TreeIterNode));
            node->key = memdup(sub_key, key_size + 1);
            node->key_size = key_size + 1;
            _listAppendTail((nodes), &(node)->_node);
        }
        collect_leaves(sub_branch, sub_key, key_size + 1, nodes);
    }
}

variable_types:
key
unsigned char *

variable_types: struct List { struct ListNode { struct ListNode *next; struct ListNode *prev; struct List *list; } *head; struct ListNode *tail; int length; } *


decompiled_code:
void __fastcall destroy_iterator(void *ptr)
{
void *v2; // rdi

while ( 1 )
{
v2 = (void *)listRemoveHead(ptr);
if ( !v2 )
break;
destroy_iter_node(v2);
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func10(void *ptr)
{
void *v2; // rdi

while ( 1 )
{
v2 = (void *)listRemoveHead(ptr);
if ( !v2 )
break;
func6(v2);
}
free(ptr);
}

original_code:
static void destroy_iterator(TreeIter *iter)
{
    TreeIterNode *node;
    while ((node = listRemoveHead(&iter->nodes)) != ((void *)0)) {
        destroy_iter_node(node);
    }
    free(iter);
}

canonical_original_code:
static void destroy_iterator(struct TreeIter *iter)
{
    struct TreeIterNode *node;
    while ((node = listRemoveHead(&iter->nodes)) != ((void *)0)) {
        destroy_iter_node(node);
    }
    free(iter);
}

variable_types: struct TreeIter { struct List { struct ListNode { struct ListNode *next; struct ListNode *prev; struct List *list; } *head; struct ListNode *tail; int length; } nodes; } *


treeCreate

decompiled_code:
void *treeCreate()
{
return calloc(1uLL, 0x20uLL);
}

canonical_decompiled_code:
void *func11()
{
return calloc(1uLL, 0x20uLL);
}

original_code:
Tree *treeCreate(void)
{
    Tree *tree = calloc(1, sizeof(*tree));
    return tree;
}

canonical_original_code:
struct Tree *treeCreate()
{
    struct Tree *tree = calloc(1, sizeof(*tree));
    return tree;
}


treeAdd

decompiled_code:
__int64 __fastcall treeAdd(__int64 a1, __int64 a2, unsigned __int8 *a3, __int64 a4)
{
__int64 v4; // rbx
__int64 result; // rax

v4 = find_or_add_leaf(a1, a3, a4);
result = dbgAssert(stderr, *(_QWORD *)(v4 + 8) == 0LL, "tree.c", 250LL, "treeAdd", "Key already used.\n");
*(_QWORD *)(v4 + 8) = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, __int64 a2, unsigned __int8 *a3, __int64 a4)
{
__int64 v4; // rbx
__int64 result; // rax

v4 = func4(a1, a3, a4);
result = dbgAssert(stderr, *(_QWORD *)(v4 + 8) == 0LL, "tree.c", 250LL, "treeAdd", "Key already used.\n");
*(_QWORD *)(v4 + 8) = a2;
return result;
}

original_code:
void treeAdd(Tree *tree, const void *data, const void *key, size_t key_size)
{
    Tree *leaf = find_or_add_leaf(tree, key, key_size);
    _dbgAssert(stderr, leaf->data == ((void *)0), "tree.c", 250, __func__, "Key already used.\n");
    leaf->data = data;
}

canonical_original_code:
void treeAdd(struct Tree *tree, void *data, void *key, unsigned long key_size)
{
    struct Tree *leaf = find_or_add_leaf(tree, key, key_size);
    _dbgAssert(stderr, leaf->data == ((void *)0), "tree.c", 250, __func__, "Key already used.\n");
    leaf->data = data;
}

variable_types:
data
void *


treeGet

decompiled_code:
__int64 __fastcall treeGet(__int64 a1, unsigned __int8 *a2, __int64 a3)
{
__int64 result; // rax

result = find_leaf(a1, a2, a3);
if ( result )
result = *(_QWORD *)(result + 8);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, unsigned __int8 *a2, __int64 a3)
{
__int64 result; // rax

result = func2(a1, a2, a3);
if ( result )
result = *(_QWORD *)(result + 8);
return result;
}

original_code:
void *treeGet(Tree *tree, const void *key, size_t key_size)
{
    Tree *leaf = find_leaf(tree, key, key_size);
    return leaf ? (void *) leaf->data : ((void *)0);
}

canonical_original_code:
void *treeGet(struct Tree *tree, void *key, unsigned long key_size)
{
    struct Tree *leaf = find_leaf(tree, key, key_size);
    return leaf ? (void *) leaf->data : ((void *)0);
}

return_type: void *


treeSet

decompiled_code:
__int64 __fastcall treeSet(__int64 a1, __int64 a2, unsigned __int8 *a3, __int64 a4)
{
__int64 v5; // rax
__int64 v6; // rbx
_BOOL8 v7; // rsi
__int64 result; // rax

v5 = find_leaf(a1, a3, a4);
v6 = v5;
v7 = 0LL;
if ( v5 )
v7 = *(_QWORD *)(v5 + 8) != 0LL;
result = dbgAssert(stderr, v7, "tree.c", 273LL, "treeSet", "Key doesn't exist.\n");
*(_QWORD *)(v6 + 8) = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1, __int64 a2, unsigned __int8 *a3, __int64 a4)
{
__int64 v5; // rax
__int64 v6; // rbx
_BOOL8 v7; // rsi
__int64 result; // rax

v5 = func2(a1, a3, a4);
v6 = v5;
v7 = 0LL;
if ( v5 )
v7 = *(_QWORD *)(v5 + 8) != 0LL;
result = dbgAssert(stderr, v7, "tree.c", 273LL, "treeSet", "Key doesn't exist.\n");
*(_QWORD *)(v6 + 8) = a2;
return result;
}

original_code:
void treeSet(Tree *tree, const void *data, const void *key, size_t key_size)
{
    Tree *leaf = find_leaf(tree, key, key_size);
    _dbgAssert(stderr, leaf != ((void *)0) && leaf->data != ((void *)0), "tree.c", 273, __func__, "Key doesn't exist.\n");
    leaf->data = data;
}

canonical_original_code:
void treeSet(struct Tree *tree, void *data, void *key, unsigned long key_size)
{
    struct Tree *leaf = find_leaf(tree, key, key_size);
    _dbgAssert(stderr, leaf != ((void *)0) && leaf->data != ((void *)0), "tree.c", 273, __func__, "Key doesn't exist.\n");
    leaf->data = data;
}