decompiled_code:
__int64 __fastcall cf_socket_register(__int64 a1, int a2, __int64 a3, __int64 a4)
{
int v4; // ebx
_DWORD *v7; // rdx
_QWORD *v8; // rax
__int64 v9; // rax
int v10; // ecx
__int64 result; // rax

v4 = numRegistered;
if ( numRegistered == 1023 )
{
cf_error_log("compframe_sockets.c", 90LL, "Max number of sockets reached!\n");
result = 0LL;
}
else
{
v7 = malloc(0x30uLL);
v7[4] = a2;
*((_QWORD *)v7 + 4) = a1;
*((_QWORD *)v7 + 5) = a4;
*((_QWORD *)v7 + 3) = a3;
*((_QWORD *)v7 + 1) = 0LL;
v8 = socketHead;
*(_QWORD *)v7 = socketHead;
if ( v8 )
v8[1] = v7;
socketHead = v7;
v9 = v4 + 1;
do
{
*((_DWORD *)&pollFD + 2 * v9 - 2) = v7[4];
*(_WORD *)(0x55C + 8 * v9) = 1;
v10 = v9;
v7 = *(_DWORD **)v7;
++v9;
}
while ( v7 );
numRegistered = v10;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2, __int64 a3, __int64 a4)
{
int v4; // ebx
_DWORD *v7; // rdx
_QWORD *v8; // rax
__int64 v9; // rax
int v10; // ecx
__int64 result; // rax

v4 = numRegistered;
if ( numRegistered == 1023 )
{
cf_error_log("compframe_sockets.c", 90LL, "Max number of sockets reached!\n");
result = 0LL;
}
else
{
v7 = malloc(0x30uLL);
v7[4] = a2;
*((_QWORD *)v7 + 4) = a1;
*((_QWORD *)v7 + 5) = a4;
*((_QWORD *)v7 + 3) = a3;
*((_QWORD *)v7 + 1) = 0LL;
v8 = socketHead;
*(_QWORD *)v7 = socketHead;
if ( v8 )
v8[1] = v7;
socketHead = v7;
v9 = v4 + 1;
do
{
*((_DWORD *)&pollFD + 2 * v9 - 2) = v7[4];
*(_WORD *)(0x55C + 8 * v9) = 1;
v10 = v9;
v7 = *(_DWORD **)v7;
++v9;
}
while ( v7 );
numRegistered = v10;
result = 1LL;
}
return result;
}

original_code:
int
cf_socket_register(void *comp, int sd, cf_sock_callback_t fp, void *userData)
{
    if ((numRegistered + 1) == 1024) {
        cf_error_log("compframe_sockets.c", 90, "Max number of sockets reached!\n");
        return 0;
    }
    cf_socket_t *s = malloc(sizeof(cf_socket_t));
    s->sd = sd;
    s->comp = comp;
    s->userData = userData;
    s->fp = fp;
    do { (s)->prev = ((void *)0); (s)->next = (socketHead); if ((socketHead)) { (socketHead)->prev = (s); } (socketHead) = (s); } while (0);
    for (s = socketHead; s != ((void *)0); s = s->next) {
        pollFD[numRegistered].fd = s->sd;
        pollFD[numRegistered].events = 0x001;
        numRegistered++;
    }
    return 1;
}

canonical_original_code:
int cf_socket_register(void *comp, int sd, int (*fp)(void *comp, int sd, void *userData, enum cf_sock_event_t ev), void *userData)
{
    if ((numRegistered + 1) == 1024) {
        cf_error_log("compframe_sockets.c", 90, "Max number of sockets reached!\n");
        return 0;
    }
    struct cf_socket_t *s = malloc(sizeof(cf_socket_t));
    s->sd = sd;
    s->comp = comp;
    s->userData = userData;
    s->fp = fp;
    do { (s)->prev = ((void *)0); (s)->next = (socketHead); if ((socketHead)) { (socketHead)->prev = (s); } (socketHead) = (s); } while (0);
    for (s = socketHead; s != ((void *)0); s = s->next) {
        pollFD[numRegistered].fd = s->sd;
        pollFD[numRegistered].events = 0x001;
        numRegistered++;
    }
    return 1;
}

variable_types:
comp
void *

variable_types: sd int

variable_types: int (*)(void *comp, int sd, void *userData, enum cf_sock_event_t { CF_SOCKET_STUFF_TO_READ=0, CF_SOCKET_CLOSED=1, CF_SOCKET_BLOCKING=2, CF_SOCKET_NOT_BLOCKING=3 } ev)

variable_types: struct cf_socket_t { struct cf_socket_t *next; struct cf_socket_t *prev; int sd; int (*fp)(void *comp, int sd, void *userData, enum cf_sock_event_t { CF_SOCKET_STUFF_TO_READ=0, CF_SOCKET_CLOSED=1, CF_SOCKET_BLOCKING=2, CF_SOCKET_NOT_BLOCKING=3 } ev); void *comp; void *userData; } *

user_defined_types: struct cf_socket_t { struct cf_socket_t *next; struct cf_socket_t *prev; int sd; int (*fp)(void *comp, int sd, void *userData, enum cf_sock_event_t ev); void *comp; void *userData; }


repo: ptorpman/compframe

decompiled_code:
__int64 __fastcall cf_socket_deregister(unsigned int a1)
{
_DWORD *v1; // rbx
__int64 result; // rax
_QWORD *v3; // rax
__int64 v4; // rdx
_DWORD *v5; // rbx
int v6; // eax
__int64 v7; // rdx
_QWORD *v8; // rax

v1 = socketHead;
if ( socketHead )
{
while ( v1[4] != a1 )
{
v1 = *(_DWORD **)v1;
if ( !v1 )
goto LABEL_4;
}
cf_trace_log("compframe_sockets.c", 121LL, 2LL, "Removed socket %d.\n", a1);
v3 = (_QWORD *)*((_QWORD *)v1 + 1);
if ( v3 )
{
v4 = *(_QWORD *)v1;
if ( *(_QWORD *)v1 )
{
*v3 = v4;
*(_QWORD *)(v4 + 8) = *((_QWORD *)v1 + 1);
}
else
{
*v3 = 0LL;
}
}
else
{
v8 = *(_QWORD **)v1;
socketHead = v8;
if ( v8 )
v8[1] = 0LL;
}
free(v1);
numRegistered = 0;
v5 = socketHead;
result = 1LL;
if ( socketHead )
{
do
{
cf_trace_log("compframe_sockets.c", 139LL, 2LL, "Socket %d is in list.\n", (unsigned int)v5[4]);
v6 = numRegistered;
v7 = numRegistered;
*(&pollFD.fd + 2 * numRegistered) = v5[4];
*(&pollFD.events + 4 * v7) = 1;
numRegistered = v6 + 1;
v5 = *(_DWORD **)v5;
}
while ( v5 );
result = 1LL;
}
}
else
{
LABEL_4:
cf_error_log("compframe_sockets.c", 132LL, "Socket not found (%d)!\n", a1);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
_DWORD *v1; // rbx
__int64 result; // rax
_QWORD *v3; // rax
__int64 v4; // rdx
_DWORD *v5; // rbx
int v6; // eax
__int64 v7; // rdx
_QWORD *v8; // rax

v1 = socketHead;
if ( socketHead )
{
while ( v1[4] != a1 )
{
v1 = *(_DWORD **)v1;
if ( !v1 )
goto LABEL_4;
}
cf_trace_log("compframe_sockets.c", 121LL, 2LL, "Removed socket %d.\n", a1);
v3 = (_QWORD *)*((_QWORD *)v1 + 1);
if ( v3 )
{
v4 = *(_QWORD *)v1;
if ( *(_QWORD *)v1 )
{
*v3 = v4;
*(_QWORD *)(v4 + 8) = *((_QWORD *)v1 + 1);
}
else
{
*v3 = 0LL;
}
}
else
{
v8 = *(_QWORD **)v1;
socketHead = v8;
if ( v8 )
v8[1] = 0LL;
}
free(v1);
numRegistered = 0;
v5 = socketHead;
result = 1LL;
if ( socketHead )
{
do
{
cf_trace_log("compframe_sockets.c", 139LL, 2LL, "Socket %d is in list.\n", (unsigned int)v5[4]);
v6 = numRegistered;
v7 = numRegistered;
*(&pollFD.fd + 2 * numRegistered) = v5[4];
*(&pollFD.events + 4 * v7) = 1;
numRegistered = v6 + 1;
v5 = *(_DWORD **)v5;
}
while ( v5 );
result = 1LL;
}
}
else
{
LABEL_4:
cf_error_log("compframe_sockets.c", 132LL, "Socket not found (%d)!\n", a1);
result = 0LL;
}
return result;
}

original_code:
int
cf_socket_deregister(int sd)
{
    int found = 0;
    for (cf_socket_t * s = socketHead; s != ((void *)0); s = s->next) {
        if (s->sd == sd) {
            cf_trace_log("compframe_sockets.c", 121, CF_TRACE_DEBUG,
                         "Removed socket %d.\n", sd);
            do { if ((s)->prev == ((void *)0)) { (socketHead) = (s)->next; if ((socketHead)) { (socketHead)->prev = ((void *)0); } } else if ((s)->next == ((void *)0)) { (s)->prev->next = ((void *)0); } else { (s)->prev->next = (s)->next; (s)->next->prev = (s)->prev; } } while (0);
            free(s);
            found = 1;
            break;
        }
    }
    if (!found) {
        cf_error_log("compframe_sockets.c", 132, "Socket not found (%d)!\n", sd);
        return 0;
    }
    numRegistered = 0;
    for (cf_socket_t * s = socketHead; s != ((void *)0); s = s->next) {
        cf_trace_log("compframe_sockets.c", 139, CF_TRACE_DEBUG,
                     "Socket %d is in list.\n", s->sd);
        pollFD[numRegistered].fd = s->sd;
        pollFD[numRegistered].events = 0x001;
        numRegistered++;
    }
    return 1;
}

canonical_original_code:
int cf_socket_deregister(int sd)
{
    int found = 0;
    for (struct cf_socket_t *s = socketHead; s != ((void *)0); s = s->next) {
        if (s->sd == sd) {
            cf_trace_log("compframe_sockets.c", 121, CF_TRACE_DEBUG,
                         "Removed socket %d.\n", sd);
            do { if ((s)->prev == ((void *)0)) { (socketHead) = (s)->next; if ((socketHead)) { (socketHead)->prev = ((void *)0); } } else if ((s)->next == ((void *)0)) { (s)->prev->next = ((void *)0); } else { (s)->prev->next = (s)->next; (s)->next->prev = (s)->prev; } } while (0);
            free(s);
            found = 1;
            break;
        }
    }
    if (!found) {
        cf_error_log("compframe_sockets.c", 132, "Socket not found (%d)!\n", sd);
        return 0;
    }
    numRegistered = 0;
    for (struct cf_socket_t *s = socketHead; s != ((void *)0); s = s->next) {
        cf_trace_log("compframe_sockets.c", 139, CF_TRACE_DEBUG,
                     "Socket %d is in list.\n", s->sd);
        pollFD[numRegistered].fd = s->sd;
        pollFD[numRegistered].events = 0x001;
        numRegistered++;
    }
    return 1;
}

variable_types: found int

return_type: int

user_defined_types: enum cf_sock_event_t { CF_SOCKET_STUFF_TO_READ=0, CF_SOCKET_CLOSED=1, CF_SOCKET_BLOCKING=2, CF_SOCKET_NOT_BLOCKING=3 }


decompiled_code:
__int64 cf_sockets_poll()
{
int v0; // eax
unsigned int v1; // er12
__int64 v2; // rbx
__int16 v3; // dx
_DWORD *v4; // rax
__int64 v5; // rsi

v0 = poll(&pollFD, numRegistered, 10);
if ( v0 <= 0 )
return (unsigned int)-1;
v1 = v0;
if ( numRegistered > 0 )
{
v2 = 0LL;
do
{
v3 = *(&pollFD.revents + 4 * v2);
if ( v3 == 1 || v3 == 16 )
{
v4 = socketHead;
if ( socketHead )
{
v5 = *((unsigned int *)&pollFD.fd + 2 * v2);
while ( v4[4] != (_DWORD)v5 )
{
v4 = *(_DWORD **)v4;
if ( !v4 )
goto LABEL_6;
}
if ( v3 == 1 )
(*((void (__fastcall **)(_QWORD, __int64, _QWORD, _QWORD))v4 + 3))(
*((_QWORD *)v4 + 4),
v5,
*((_QWORD *)v4 + 5),
0LL);
else
(*((void (__fastcall **)(_QWORD, __int64, _QWORD, __int64))v4 + 3))(
*((_QWORD *)v4 + 4),
v5,
*((_QWORD *)v4 + 5),
1LL);
}
}
LABEL_6:
++v2;
}
while ( numRegistered > (int)v2 );
}
return v1;
}

canonical_decompiled_code:
__int64 func4()
{
int v0; // eax
unsigned int v1; // er12
__int64 v2; // rbx
__int16 v3; // dx
_DWORD *v4; // rax
__int64 v5; // rsi

v0 = poll(&pollFD, numRegistered, 10);
if ( v0 <= 0 )
return (unsigned int)-1;
v1 = v0;
if ( numRegistered > 0 )
{
v2 = 0LL;
do
{
v3 = *(&pollFD.revents + 4 * v2);
if ( v3 == 1 || v3 == 16 )
{
v4 = socketHead;
if ( socketHead )
{
v5 = *((unsigned int *)&pollFD.fd + 2 * v2);
while ( v4[4] != (_DWORD)v5 )
{
v4 = *(_DWORD **)v4;
if ( !v4 )
goto LABEL_6;
}
if ( v3 == 1 )
(*((void (__fastcall **)(_QWORD, __int64, _QWORD, _QWORD))v4 + 3))(
*((_QWORD *)v4 + 4),
v5,
*((_QWORD *)v4 + 5),
0LL);
else
(*((void (__fastcall **)(_QWORD, __int64, _QWORD, __int64))v4 + 3))(
*((_QWORD *)v4 + 4),
v5,
*((_QWORD *)v4 + 5),
1LL);
}
}
LABEL_6:
++v2;
}
while ( numRegistered > (int)v2 );
}
return v1;
}

original_code:
int
cf_sockets_poll(void)
{
    int res;
    res = poll(pollFD, numRegistered, 10);
    if (res <= 0) {
        return -1;
    }
    for (int i = 0; i < numRegistered; i++) {
        if (pollFD[i].revents == 0x001 || pollFD[i].revents == 0x010) {
            for (cf_socket_t * s = socketHead; s != ((void *)0); s = s->next) {
                if (s->sd == pollFD[i].fd) {
                    if (pollFD[i].revents == 0x001) {
                        s->fp(s->comp, s->sd, s->userData,
                              CF_SOCKET_STUFF_TO_READ);
                    }
                    else {
                        s->fp(s->comp, s->sd, s->userData, CF_SOCKET_CLOSED);
                    }
                    break;
                }
            }
        }
    }
    return res;
}

canonical_original_code:
int cf_sockets_poll()
{
    int res;
    res = poll(pollFD, numRegistered, 10);
    if (res <= 0) {
        return -1;
    }
    for (int i = 0; i < numRegistered; i++) {
        if (pollFD[i].revents == 0x001 || pollFD[i].revents == 0x010) {
            for (struct cf_socket_t *s = socketHead; s != ((void *)0); s = s->next) {
                if (s->sd == pollFD[i].fd) {
                    if (pollFD[i].revents == 0x001) {
                        s->fp(s->comp, s->sd, s->userData,
                              CF_SOCKET_STUFF_TO_READ);
                    }
                    else {
                        s->fp(s->comp, s->sd, s->userData, CF_SOCKET_CLOSED);
                    }
                    break;
                }
            }
        }
    }
    return res;
}

variable_types: res int

variable_types: i int


decompiled_code:
int __fastcall cf_socket_connect(const char *a1, __int16 a2, int *a3)
{
struct hostent *v4; // rax
struct hostent *v5; // rbx
int result; // eax
struct sockaddr dest[2]; // [rsp+0h] [rbp-28h] BYREF

*(_QWORD *)&dest[0].sa_family = 0LL;
*(_QWORD *)&dest[0].sa_data[6] = 0LL;
v4 = gethostbyname(a1);
if ( !v4 )
return -1;
v5 = v4;
dest[0].sa_family = 2;
*(_WORD *)dest[0].sa_data = _bswap_16(a2);
memmove(&dest[0].sa_data[2], *(const void **)v5->h_addr_list, v5->h_length);
result = socket(2, 1, 0);
*a3 = result;
if ( result != -1 )
result = connect(result, dest, 0x10u);
return result;
}

canonical_decompiled_code:
int __fastcall func5(const char *a1, __int16 a2, int *a3)
{
struct hostent *v4; // rax
struct hostent *v5; // rbx
int result; // eax
struct sockaddr dest[2]; // [rsp+0h] [rbp-28h] BYREF

*(_QWORD *)&dest[0].sa_family = 0LL;
*(_QWORD *)&dest[0].sa_data[6] = 0LL;
v4 = gethostbyname(a1);
if ( !v4 )
return -1;
v5 = v4;
dest[0].sa_family = 2;
*(_WORD *)dest[0].sa_data = func0(a2);
memmove(&dest[0].sa_data[2], *(const void **)v5->h_addr_list, v5->h_length);
result = socket(2, 1, 0);
*a3 = result;
if ( result != -1 )
result = connect(result, dest, 0x10u);
return result;
}

original_code:
int
cf_socket_connect(char *host, int port, int *sd)
{
    struct sockaddr_in peer;
    struct hostent *hp;
    bzero((char *) &peer, sizeof(peer));
    hp = gethostbyname(host);
    if (hp == ((void *)0)) {
        return -1;
    }
    peer.sin_family = 2;
    peer.sin_port = htons(port);
    bcopy((char *) hp->h_addr_list[0], (char *) &peer.sin_addr, hp->h_length);
    *sd = socket(2, SOCK_STREAM, 0);
    if (*sd == -1) {
        return -1;
    }
    return connect(*sd, (struct sockaddr *) &peer, sizeof(peer));
}

variable_types:
host
char *

variable_types: port int

variable_types:
sd
int *

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


decompiled_code:
__int64 __fastcall cf_socket_server(__int16 a1)
{
int v1; // eax
unsigned int v2; // ebx
socklen_t len; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

len = 16;
*(_DWORD *)&addr.sa_data[10] = 0;
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)_bswap_32(0);
*(_WORD *)addr.sa_data = _bswap_16(a1);
v1 = socket(2, 1, 0);
v2 = v1;
if ( v1 != -1 )
{
if ( bind(v1, &addr, 0x10u) == -1 )
{
v2 = -1;
}
else if ( getsockname(v2, &addr, &len) == -1 )
{
v2 = -1;
}
else if ( listen(v2, 5) == -1 )
{
v2 = -1;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int16 a1)
{
int v1; // eax
unsigned int v2; // ebx
socklen_t len; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

len = 16;
*(_DWORD *)&addr.sa_data[10] = 0;
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)func1(0);
*(_WORD *)addr.sa_data = func0(a1);
v1 = socket(2, 1, 0);
v2 = v1;
if ( v1 != -1 )
{
if ( bind(v1, &addr, 0x10u) == -1 )
{
v2 = -1;
}
else if ( getsockname(v2, &addr, &len) == -1 )
{
v2 = -1;
}
else if ( listen(v2, 5) == -1 )
{
v2 = -1;
}
}
return v2;
}

original_code:
int
cf_socket_server(int port)
{
    struct sockaddr_in serv_addr;
    int result;
    int size = sizeof(serv_addr);
    int newSocket = -1;
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = 2;
    serv_addr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
    serv_addr.sin_port = htons(port);
    newSocket = socket(2, SOCK_STREAM, 0);
    if (newSocket == -1) {
        return -1;
    }
    result = bind(newSocket, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
    if (result == -1) {
        return -1;
    }
    result =
        getsockname(newSocket, (struct sockaddr *) &serv_addr,
                    (socklen_t *) & size);
    if (result == -1) {
        return -1;
    }
    result = listen(newSocket, 5);
    if (result == -1) {
        return -1;
    }
    return newSocket;
}

canonical_original_code:
int cf_socket_server(int port)
{
    struct sockaddr_in serv_addr;
    int result;
    int size = sizeof(serv_addr);
    int newSocket = -1;
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = 2;
    serv_addr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
    serv_addr.sin_port = htons(port);
    newSocket = socket(2, SOCK_STREAM, 0);
    if (newSocket == -1) {
        return -1;
    }
    result = bind(newSocket, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
    if (result == -1) {
        return -1;
    }
    result =
        getsockname(newSocket, (struct sockaddr *) &serv_addr,
                    (unsigned int *) & size);
    if (result == -1) {
        return -1;
    }
    result = listen(newSocket, 5);
    if (result == -1) {
        return -1;
    }
    return newSocket;
}

variable_types: result int

variable_types: size int

variable_types: newSocket int


decompiled_code:
__int64 __fastcall cf_socket_write(int fd, void *buf, size_t n)
{
unsigned int v4; // er13

do
v4 = write(fd, buf, n);
while ( v4 == -1 && *_errno_location() == 11 );
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func7(int fd, void *buf, size_t n)
{
unsigned int v4; // er13

do
v4 = write(fd, buf, n);
while ( v4 == -1 && *_errno_location() == 11 );
return v4;
}

original_code:
int
cf_socket_write(int sd, unsigned char *buf, size_t n)
{
    int result = -1;
    do {
        result = write(sd, buf, n);
    } while (result == -1 && (*__errno_location ()) == 11);
    return result;
}

canonical_original_code:
int cf_socket_write(int sd, unsigned char *buf, unsigned long n)
{
    int result = -1;
    do {
        result = write(sd, buf, n);
    } while (result == -1 && (*__errno_location ()) == 11);
    return result;
}

variable_types:
buf
unsigned char *

variable_types:
n
unsigned long


decompiled_code:
__int64 __fastcall cf_socket_name_get(int a1, char **a2, _DWORD *a3)
{
unsigned int v4; // ebx
char v6[1036]; // [rsp+0h] [rbp-438h] BYREF
socklen_t len; // [rsp+40Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+410h] [rbp-28h] BYREF

len = 16;
if ( getsockname(a1, &addr, &len) )
return (unsigned int)-1;
v4 = 0;
if ( gethostname(v6, 0x400uLL) == -1 )
return (unsigned int)-1;
*a3 = (unsigned __int16)_bswap_16(*(__int16 *)addr.sa_data);
*a2 = strdup(v6);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, char **a2, _DWORD *a3)
{
unsigned int v4; // ebx
char v6[1036]; // [rsp+0h] [rbp-438h] BYREF
socklen_t len; // [rsp+40Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+410h] [rbp-28h] BYREF

len = 16;
if ( getsockname(a1, &addr, &len) )
return (unsigned int)-1;
v4 = 0;
if ( gethostname(v6, 0x400uLL) == -1 )
return (unsigned int)-1;
*a3 = (unsigned __int16)func0(*(__int16 *)addr.sa_data);
*a2 = strdup(v6);
return v4;
}

original_code:
int
cf_socket_name_get(int sd, char **host, int *port)
{
    struct sockaddr_in inA;
    int addr_len = sizeof(inA);
    char tmphostname[1024];
    int result;
    if (getsockname(sd, (struct sockaddr *) &inA, (socklen_t *) & addr_len) !=
        0) {
        host = ((void *)0);
        return -1;
    }
    result = gethostname(tmphostname, sizeof(tmphostname));
    if (result == -1) {
        host = ((void *)0);
        return -1;
    }
    *port = ntohs(inA.sin_port);
    *host = strdup(tmphostname);
    return 0;
}

canonical_original_code:
int cf_socket_name_get(int sd, char **host, int *port)
{
    struct sockaddr_in inA;
    int addr_len = sizeof(inA);
    char tmphostname[1024];
    int result;
    if (getsockname(sd, (struct sockaddr *) &inA, (unsigned int *) & addr_len) !=
        0) {
        host = ((void *)0);
        return -1;
    }
    result = gethostname(tmphostname, sizeof(tmphostname));
    if (result == -1) {
        host = ((void *)0);
        return -1;
    }
    *port = ntohs(inA.sin_port);
    *host = strdup(tmphostname);
    return 0;
}

variable_types:
host
char **

variable_types:
port
int *

variable_types:
tmphostname
char [1024]


decompiled_code:
__int64 __fastcall cf_socket_port_get(int a1)
{
__int64 result; // rax
socklen_t len; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

len = 16;
if ( getsockname(a1, &addr, &len) )
result = 0xFFFFFFFFLL;
else
result = (unsigned __int16)_bswap_16(*(__int16 *)addr.sa_data);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1)
{
__int64 result; // rax
socklen_t len; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

len = 16;
if ( getsockname(a1, &addr, &len) )
result = 0xFFFFFFFFLL;
else
result = (unsigned __int16)func0(*(__int16 *)addr.sa_data);
return result;
}

original_code:
int
cf_socket_port_get(int sd)
{
    struct sockaddr_in host;
    int addr_len = sizeof(host);
    if (getsockname(sd, (struct sockaddr *) &host, (socklen_t *) & addr_len) !=
        0) {
        return -1;
    }
    return ntohs(host.sin_port);
}

canonical_original_code:
int cf_socket_port_get(int sd)
{
    struct sockaddr_in host;
    int addr_len = sizeof(host);
    if (getsockname(sd, (struct sockaddr *) &host, (unsigned int *) & addr_len) !=
        0) {
        return -1;
    }
    return ntohs(host.sin_port);
}

variable_types: sd int


decompiled_code:
int __fastcall cf_socket_bytes_to_read(int a1)
{
int result; // eax
int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = 0;
result = ioctl(a1, 0x541BuLL, v2);
if ( result )
result = v2[0];
return result;
}

canonical_decompiled_code:
int __fastcall func10(int a1)
{
int result; // eax
int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = 0;
result = ioctl(a1, 0x541BuLL, v2);
if ( result )
result = v2[0];
return result;
}

original_code:
int
cf_socket_bytes_to_read(int sd)
{
    int bytes = 0;
    int result = -1;
    result = ioctl(sd, 0x541B, &bytes);
    if (result) {
        return bytes;
    }
    return result;
}

variable_types: bytes int


decompiled_code:
int __fastcall cf_socket_reusable(int a1)
{
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

optval[0] = 1;
return setsockopt(a1, 1, 2, optval, 4u);
}

canonical_decompiled_code:
int __fastcall func11(int a1)
{
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

optval[0] = 1;
return setsockopt(a1, 1, 2, optval, 4u);
}

original_code:
int
cf_socket_reusable(int sd)
{
    int flag = 1;
    return setsockopt(sd, 1, 2, (char *) &flag,
                      sizeof(flag));
}

variable_types: flag int


decompiled_code:
int cf_info_log(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, format);
fwrite("*** INFO # ", 1uLL, 0xBuLL, stderr);
return vfprintf(stderr, format, arg);
}

canonical_decompiled_code:
int func0(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, format);
fwrite("*** INFO # ", 1uLL, 0xBuLL, stderr);
return vfprintf(stderr, format, arg);
}

original_code:
void
cf_info_log(const char *format, ...)
{
    va_list ap;
    __builtin_va_start(ap,format);
    fprintf(stderr, "*** INFO # ");
    vfprintf(stderr, format, ap);
    __builtin_va_end(ap);
}

canonical_original_code:
void cf_info_log(char *format, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,format);
    fprintf(stderr, "*** INFO # ");
    vfprintf(stderr, format, ap);
    __builtin_va_end(ap);
}

variable_types:
format
char *

return_type: void


decompiled_code:
int cf_error_log(const char *a1, unsigned int a2, const char *a3, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, a3);
fprintf(stderr, "*** ERROR %s:%d # ", a1, a2);
return vfprintf(stderr, a3, arg);
}

canonical_decompiled_code:
int func1(const char *a1, unsigned int a2, const char *a3, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, a3);
fprintf(stderr, "*** ERROR %s:%d # ", a1, a2);
return vfprintf(stderr, a3, arg);
}

original_code:
void
cf_error_log(const char *file, int line, const char *format, ...)
{
    va_list ap;
    __builtin_va_start(ap,format);
    fprintf(stderr, "*** ERROR %s:%d # ", file, line);
    vfprintf(stderr, format, ap);
    __builtin_va_end(ap);
}

canonical_original_code:
void cf_error_log(char *file, int line, char *format, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,format);
    fprintf(stderr, "*** ERROR %s:%d # ", file, line);
    vfprintf(stderr, format, ap);
    __builtin_va_end(ap);
}

variable_types:
file
char *

variable_types: line int


decompiled_code:
int cf_trace_log(const char *a1, unsigned int a2, unsigned int a3, const char *a4, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

if ( cfTraceLevel >= a3 )
{
va_start(arg, a4);
fprintf(stderr, "***[%-7s] %-20s:%-5d # ", cf_trace_level_names[a3], a1, a2);
result = vfprintf(stderr, a4, arg);
}
return result;
}

canonical_decompiled_code:
int func2(const char *a1, unsigned int a2, unsigned int a3, const char *a4, ...)
{
int result; // eax
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

if ( cfTraceLevel >= a3 )
{
va_start(arg, a4);
fprintf(stderr, "***[%-7s] %-20s:%-5d # ", cf_trace_level_names[a3], a1, a2);
result = vfprintf(stderr, a4, arg);
}
return result;
}

original_code:
void
cf_trace_log(const char *file,
             int line, CfTraceLevel level, const char *format, ...)
{
    if (level > cfTraceLevel) {
        return;
    }
    va_list ap;
    __builtin_va_start(ap,format);
    fprintf(stderr,
            "***[%-7s] %-20s:%-5d # ", cf_trace_level_names[level], file, line);
    vfprintf(stderr, format, ap);
    __builtin_va_end(ap);
}

canonical_original_code:
void cf_trace_log(char *file, int line, enum CfTraceLevel level, char *format, ...)
{
    if (level > cfTraceLevel) {
        return;
    }
    __builtin_va_list ap;
    __builtin_va_start(ap,format);
    fprintf(stderr,
            "***[%-7s] %-20s:%-5d # ", cf_trace_level_names[level], file, line);
    vfprintf(stderr, format, ap);
    __builtin_va_end(ap);
}

variable_types: enum CfTraceLevel { CF_TRACE_OFF=0, CF_TRACE_INFO=1, CF_TRACE_DEBUG=2, CF_TRACE_MASSIVE=3 }


decompiled_code:
void __fastcall cf_trace_level_set(int a1)
{
cfTraceLevel = a1;
}

canonical_decompiled_code:
void __fastcall func3(int a1)
{
cfTraceLevel = a1;
}

original_code:
void
cf_trace_level_set(int level)
{
    cfTraceLevel = (CfTraceLevel) level;
}

canonical_original_code:
void cf_trace_level_set(int level)
{
    cfTraceLevel = (enum CfTraceLevel) level;
}

variable_types: level int

user_defined_types: enum CfTraceLevel { CF_TRACE_OFF=0, CF_TRACE_INFO=1, CF_TRACE_DEBUG=2, CF_TRACE_MASSIVE=3 }


decompiled_code:
int __fastcall file_get_802_addr(__int64 a1, _QWORD *a2)
{
ssize_t i; // rax
int v3; // ebx
int v5; // [rsp+0h] [rbp-48h] BYREF
char v6; // [rsp+4h] [rbp-44h] BYREF
char v7; // [rsp+8h] [rbp-40h] BYREF
char v8; // [rsp+Ch] [rbp-3Ch] BYREF
char v9; // [rsp+10h] [rbp-38h] BYREF
char v10; // [rsp+14h] [rbp-34h] BYREF
char buf[37]; // [rsp+23h] [rbp-25h] BYREF

*a2 = 0LL;
LODWORD(i) = open("/etc/ieee_802_addr", 0);
if ( (int)i < 0 )
{
*a2 = 341839902LL;
}
else
{
v3 = i;
i = read(i, buf, 0xCuLL);
if ( i > 11 )
{
close(v3);
buf[12] = 0;
LODWORD(i) = _isoc99_sscanf(buf, "%2x%2x%2x%2x%2x%2x", &v5, &v6, &v7, &v8, &v9, &v10);
if ( (_DWORD)i == 6 )
{
for ( i = 0LL; i != 6; ++i )
*(_BYTE *)(a1 + i) = *(&v5 + i);
}
else
{
*a2 = 341839903LL;
}
}
else
{
*a2 = 341839902LL;
}
}
return i;
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, _QWORD *a2)
{
ssize_t i; // rax
int v3; // ebx
int v5; // [rsp+0h] [rbp-48h] BYREF
char v6; // [rsp+4h] [rbp-44h] BYREF
char v7; // [rsp+8h] [rbp-40h] BYREF
char v8; // [rsp+Ch] [rbp-3Ch] BYREF
char v9; // [rsp+10h] [rbp-38h] BYREF
char v10; // [rsp+14h] [rbp-34h] BYREF
char buf[37]; // [rsp+23h] [rbp-25h] BYREF

*a2 = 0LL;
LODWORD(i) = open("/etc/ieee_802_addr", 0);
if ( (int)i < 0 )
{
*a2 = 341839902LL;
}
else
{
v3 = i;
i = read(i, buf, 0xCuLL);
if ( i > 11 )
{
close(v3);
buf[12] = 0;
LODWORD(i) = _isoc99_sscanf(buf, "%2x%2x%2x%2x%2x%2x", &v5, &v6, &v7, &v8, &v9, &v10);
if ( (_DWORD)i == 6 )
{
for ( i = 0LL; i != 6; ++i )
*(_BYTE *)(a1 + i) = *(&v5 + i);
}
else
{
*a2 = 341839903LL;
}
}
else
{
*a2 = 341839902LL;
}
}
return i;
}

variable_types: struct dce_802_addr_s_t { unsigned char eaddr[6]; } *

variable_types:
st
unsigned long *

variable_types: fd int

variable_types:
buf
char [13]

variable_types:
e
int [6]

variable_types: i int


decompiled_code:
__int64 __fastcall solaris_get_hwaddr(int fd, const char *a2, __int64 a3)
{
int v4; // edx
__int64 result; // rax
int v6; // edx
__m128i v7; // [rsp+0h] [rbp-98h] BYREF
int v8; // [rsp+12h] [rbp-86h]
__int16 v9; // [rsp+16h] [rbp-82h]
char dest[16]; // [rsp+50h] [rbp-48h] BYREF
__m128i v11[3]; // [rsp+60h] [rbp-38h] BYREF

strcpy(dest, a2);
v4 = ioctl(fd, 0x8915uLL, dest);
result = 0LL;
if ( v4 >= 0 )
{
v7 = _mm_loadu_si128(v11);
v6 = ioctl(fd, 0x8954uLL, &v7);
result = 0LL;
if ( v6 >= 0 )
{
*(_DWORD *)a3 = v8;
*(_WORD *)(a3 + 4) = v9;
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, const char *a2, __int64 a3)
{
int v4; // edx
__int64 result; // rax
int v6; // edx
__m128i v7; // [rsp+0h] [rbp-98h] BYREF
int v8; // [rsp+12h] [rbp-86h]
__int16 v9; // [rsp+16h] [rbp-82h]
char dest[16]; // [rsp+50h] [rbp-48h] BYREF
__m128i v11[3]; // [rsp+60h] [rbp-38h] BYREF

strcpy(dest, a2);
v4 = ioctl(fd, 0x8915uLL, dest);
result = 0LL;
if ( v4 >= 0 )
{
v7 = _mm_loadu_si128(v11);
v6 = ioctl(fd, 0x8954uLL, &v7);
result = 0LL;
if ( v6 >= 0 )
{
*(_DWORD *)a3 = v8;
*(_WORD *)(a3 + 4) = v9;
result = 1LL;
}
}
return result;
}

original_code:
int solaris_get_hwaddr(int afinet_socket,
                       const char *interface_name,
                       dce_802_addr_t *addr)
{
  struct ifreq ifr;
  struct arpreq arpr;
  strcpy (ifr.ifr_ifrn.ifrn_name, interface_name);
  if (ioctl (afinet_socket, 0x8915, &ifr) < 0)
    {
      return 0;
    }
  memcpy(&(arpr.arp_pa), &(ifr.ifr_ifru.ifru_addr), sizeof(struct sockaddr));
  if (ioctl (afinet_socket, 0x8954, &arpr) < 0)
    {
      return 0;
    }
  memcpy(addr->eaddr, arpr.arp_ha.sa_data, 6);
  return 1;
}

canonical_original_code:
int solaris_get_hwaddr(int afinet_socket, char *interface_name, struct dce_802_addr_s_t *addr)
{
  struct ifreq ifr;
  struct arpreq arpr;
  strcpy (ifr.ifr_ifrn.ifrn_name, interface_name);
  if (ioctl (afinet_socket, 0x8915, &ifr) < 0)
    {
      return 0;
    }
  memcpy(&(arpr.arp_pa), &(ifr.ifr_ifru.ifru_addr), sizeof(struct sockaddr));
  if (ioctl (afinet_socket, 0x8954, &arpr) < 0)
    {
      return 0;
    }
  memcpy(addr->eaddr, arpr.arp_ha.sa_data, 6);
  return 1;
}

variable_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }

variable_types: struct arpreq { struct sockaddr { unsigned short sa_family; char sa_data[14]; } arp_pa; struct sockaddr arp_ha; int arp_flags; struct sockaddr arp_netmask; char arp_dev[16]; }

user_defined_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }

user_defined_types: struct arpreq { struct sockaddr arp_pa; struct sockaddr arp_ha; int arp_flags; struct sockaddr arp_netmask; char arp_dev[16]; }

user_defined_types: struct dce_802_addr_s_t { unsigned char eaddr[6]; }


decompiled_code:
int __fastcall solaris_get_802_addr(void *s1, _QWORD *a2)
{
int v2; // eax
int v3; // ebp
const char *v4; // rbx
unsigned __int64 v5; // r13
int v6; // eax
int result; // eax
__int64 v8; // [rsp+0h] [rbp-438h] BYREF
int v9; // [rsp+400h] [rbp-38h] BYREF
const char *v10; // [rsp+408h] [rbp-30h]

*a2 = 341839902LL;
v2 = socket(2, 2, 0);
if ( v2 < 0 )
{
puts("Cant open socket");
exit(1);
}
v3 = v2;
v9 = 1024;
v10 = (const char *)&v8;
if ( ioctl(v2, 0x8912uLL, &v9) < 0 )
{
puts("ioctl(SIOCGIFCONF) failed");
exit(1);
}
v4 = v10;
v5 = (unsigned __int64)&v10[v9];
if ( (unsigned __int64)v10 < v5 )
{
do
{
v6 = solaris_get_hwaddr(v3, v4, (__int64)s1);
v4 += 40;
}
while ( !v6 && v5 > (unsigned __int64)v4 );
}
result = memcmp(s1, &null_802_hwaddr, 6uLL);
if ( result )
{
result = close(v3);
*a2 = 0LL;
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(void *s1, _QWORD *a2)
{
int v2; // eax
int v3; // ebp
const char *v4; // rbx
unsigned __int64 v5; // r13
int v6; // eax
int result; // eax
__int64 v8; // [rsp+0h] [rbp-438h] BYREF
int v9; // [rsp+400h] [rbp-38h] BYREF
const char *v10; // [rsp+408h] [rbp-30h]

*a2 = 341839902LL;
v2 = socket(2, 2, 0);
if ( v2 < 0 )
{
puts("Cant open socket");
exit(1);
}
v3 = v2;
v9 = 1024;
v10 = (const char *)&v8;
if ( ioctl(v2, 0x8912uLL, &v9) < 0 )
{
puts("ioctl(SIOCGIFCONF) failed");
exit(1);
}
v4 = v10;
v5 = (unsigned __int64)&v10[v9];
if ( (unsigned __int64)v10 < v5 )
{
do
{
v6 = func1(v3, v4, (__int64)s1);
v4 += 40;
}
while ( !v6 && v5 > (unsigned __int64)v4 );
}
result = memcmp(s1, &null_802_hwaddr, 6uLL);
if ( result )
{
result = close(v3);
*a2 = 0LL;
}
return result;
}

original_code:
static void
solaris_get_802_addr(
    dce_802_addr_t *addr,
    error_status_t *st
    )
{
  int afinet_socket;
  struct ifconf ifc;
  struct ifreq *ifr, *last_ifr;
  unsigned char buf[1024];
  int n_ifs, i;
  int prev_size = sizeof(struct ifreq);
  int continue_search;
  *st = (0x1460101e);
  afinet_socket = socket(2, SOCK_DGRAM, 0);
  if (afinet_socket < 0)
    {
      printf ("Cant open socket\n");
      exit(1);
    }
  ifc.ifc_len = sizeof (buf);
  ifc.ifc_ifcu.ifcu_buf = (caddr_t) buf;
  if (ioctl (afinet_socket, (int) 0x8912, (caddr_t) &ifc) < 0)
    {
      printf ("ioctl(SIOCGIFCONF) failed\n");
      exit(1);
    }
  n_ifs = ifc.ifc_len / sizeof (struct ifreq);
  last_ifr = (struct ifreq *) (ifc.ifc_ifcu.ifcu_buf + ifc.ifc_len);
  continue_search = 1;
  for (i=0, ifr = ifc.ifc_ifcu.ifcu_req;
       (ifr < last_ifr) && continue_search;
       i++, ifr = (struct ifreq *)(( (char *) ifr ) + prev_size))
    {
      continue_search = ! solaris_get_hwaddr(afinet_socket,
                                             ifr->ifr_ifrn.ifrn_name,
                                             addr);
    }
  if (memcmp(addr, null_802_hwaddr, 6) == 0)
    {
      return;
    }
  close(afinet_socket);
  *st = (0);
}

canonical_original_code:
static void solaris_get_802_addr(struct dce_802_addr_s_t *addr, unsigned long *st)
{
  int afinet_socket;
  struct ifconf ifc;
  struct ifreq *ifr; struct ifreq *last_ifr;
  unsigned char buf[1024];
  int n_ifs; int i;
  int prev_size = sizeof(struct ifreq);
  int continue_search;
  *st = (0x1460101e);
  afinet_socket = socket(2, SOCK_DGRAM, 0);
  if (afinet_socket < 0)
    {
      printf ("Cant open socket\n");
      exit(1);
    }
  ifc.ifc_len = sizeof (buf);
  ifc.ifc_ifcu.ifcu_buf = (char *) buf;
  if (ioctl (afinet_socket, (int) 0x8912, (caddr_t) &ifc) < 0)
    {
      printf ("ioctl(SIOCGIFCONF) failed\n");
      exit(1);
    }
  n_ifs = ifc.ifc_len / sizeof (struct ifreq);
  last_ifr = (struct ifreq *) (ifc.ifc_ifcu.ifcu_buf + ifc.ifc_len);
  continue_search = 1;
  for (i=0, ifr = ifc.ifc_ifcu.ifcu_req;
       (ifr < last_ifr) && continue_search;
       i++, ifr = (struct ifreq *)(( (char *) ifr ) + prev_size))
    {
      continue_search = ! solaris_get_hwaddr(afinet_socket,
                                             ifr->ifr_ifrn.ifrn_name,
                                             addr);
    }
  if (memcmp(addr, null_802_hwaddr, 6) == 0)
    {
      return;
    }
  close(afinet_socket);
  *st = (0);
}

variable_types: struct ifconf { int ifc_len; union { char *ifcu_buf; struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; } *ifcu_req; } ifc_ifcu; }

variable_types:
buf
unsigned char [1024]

user_defined_types: struct ifconf { int ifc_len; union { char *ifcu_buf; struct ifreq *ifcu_req; } ifc_ifcu; }


decompiled_code:
int __fastcall dce_get_802_addr(void *a1, _QWORD *a2)
{
int result; // eax

result = solaris_get_802_addr(a1, a2);
if ( *a2 )
result = file_get_802_addr((__int64)a1, a2);
return result;
}

canonical_decompiled_code:
int __fastcall func3(void *a1, _QWORD *a2)
{
int result; // eax

result = func2(a1, a2);
if ( *a2 )
result = func0((__int64)a1, a2);
return result;
}

original_code:
void
dce_get_802_addr (
    dce_802_addr_t *addr,
    error_status_t *st
)
{
  solaris_get_802_addr(addr, st);
  if (*st != (0))
    {
      file_get_802_addr(addr, st);
    }
}

canonical_original_code:
void dce_get_802_addr(struct dce_802_addr_s_t *addr, unsigned long *st)
{
  solaris_get_802_addr(addr, st);
  if (*st != (0))
    {
      file_get_802_addr(addr, st);
    }
}


assert

decompiled_code:
void __noreturn assert()
{
;
}

canonical_decompiled_code:
void __noreturn func4()
{
;
}

original_code: void assert(boolean foo) { (void)foo; }

canonical_original_code: void assert(unsigned int foo) { (void)foo; }

variable_types:
foo
unsigned int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v4; // rax
char v6[32]; // [rsp+10h] [rbp-A0h] BYREF
char v7[32]; // [rsp+30h] [rbp-80h] BYREF
char v8[32]; // [rsp+50h] [rbp-60h] BYREF
char v9[32]; // [rsp+70h] [rbp-40h] BYREF
char *v10; // [rsp+90h] [rbp-20h]
int v11; // [rsp+98h] [rbp-18h]
int i; // [rsp+9Ch] [rbp-14h]

while ( 1 )
{
v11 = getopt(argc, (char *const *)argv, "visch?n:e:l:g:t:d:m:");
if ( v11 == -1 )
break;
uuid_args = 0LL;
switch ( v11 )
{
case '?':
case 'h':
case 'v':
usage();
case 'c':
opt_com_initializer = 1;
break;
case 'd':
opt_testdceuuid = 1;
uuid_args = optarg;
break;
case 'e':
opt_equals = 1;
uuid_args = optarg;
break;
case 'g':
opt_greaterthan = 1;
uuid_args = optarg;
break;
case 'i':
opt_idl_hdrgen = 1;
break;
case 'l':
opt_lessthan = 1;
uuid_args = optarg;
break;
case 'm':
opt_testmsguid = 1;
uuid_args = optarg;
break;
case 'n':
num_uuids = atoi(optarg);
break;
case 's':
opt_c_initializer = 1;
break;
case 't':
opt_test = 1;
uuid_args = optarg;
break;
default:
usage();
}
}
if ( opt_test || opt_testmsguid || opt_testdceuuid )
{
uuid::uuid((uuid *)v6, uuid_args);
uuid::operator=(&uuid1, v6);
uuid::~uuid(v6);
if ( (unsigned __int8)uuid::IsNil((uuid *)&uuid1) )
{
fwrite("Error: argument must be a valid UUID\n", 1uLL, 0x25uLL, stderr);
exit(1);
}
}
if ( opt_equals || opt_lessthan || opt_greaterthan )
{
if ( !uuid_args )
{
fwrite("Error: need UUID arguments \n", 1uLL, 0x1CuLL, stderr);
exit(1);
}
v10 = strtok(uuid_args, ",:/");
if ( v10 )
{
uuid::uuid((uuid *)v7, v10);
uuid::operator=(&uuid1, v7);
uuid::~uuid(v7);
}
v10 = strtok(0LL, ",:/");
if ( v10 )
{
uuid::uuid((uuid *)v8, v10);
uuid::operator=(&uuid2, v8);
uuid::~uuid(v8);
}
if ( (unsigned __int8)uuid::IsNil((uuid *)&uuid1) || (unsigned __int8)uuid::IsNil((uuid *)&uuid2) )
{
fwrite("Error: one or both of the UUID arguments is invalid\n", 1uLL, 0x34uLL, stderr);
exit(1);
}
}
if ( opt_idl_hdrgen )
mk_dce_idl_hdr();
if ( opt_com_initializer )
mk_com_initializer();
if ( opt_c_initializer )
mk_c_initializer();
if ( opt_testmsguid )
test_ms_guid();
if ( opt_testdceuuid )
test_dce_uuid();
if ( opt_lessthan )
test_lt();
if ( opt_greaterthan )
test_gt();
if ( opt_equals )
test_eq();
if ( opt_test )
test_dce_uuid();
for ( i = 0; i < num_uuids; ++i )
{
uuid::uuid((uuid *)v9);
v4 = (const char *)uuid::String((uuid *)v9);
fprintf(stdout, "%s\n", v4);
uuid::~uuid(v9);
}
exit(0);
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v4; // rax
char v6[32]; // [rsp+10h] [rbp-A0h] BYREF
char v7[32]; // [rsp+30h] [rbp-80h] BYREF
char v8[32]; // [rsp+50h] [rbp-60h] BYREF
char v9[32]; // [rsp+70h] [rbp-40h] BYREF
char *v10; // [rsp+90h] [rbp-20h]
int v11; // [rsp+98h] [rbp-18h]
int i; // [rsp+9Ch] [rbp-14h]

while ( 1 )
{
v11 = getopt(argc, (char *const *)argv, "visch?n:e:l:g:t:d:m:");
if ( v11 == -1 )
break;
uuid_args = 0LL;
switch ( v11 )
{
case '?':
case 'h':
case 'v':
func0();
case 'c':
opt_com_initializer = 1;
break;
case 'd':
opt_testdceuuid = 1;
uuid_args = optarg;
break;
case 'e':
opt_equals = 1;
uuid_args = optarg;
break;
case 'g':
opt_greaterthan = 1;
uuid_args = optarg;
break;
case 'i':
opt_idl_hdrgen = 1;
break;
case 'l':
opt_lessthan = 1;
uuid_args = optarg;
break;
case 'm':
opt_testmsguid = 1;
uuid_args = optarg;
break;
case 'n':
num_uuids = atoi(optarg);
break;
case 's':
opt_c_initializer = 1;
break;
case 't':
opt_test = 1;
uuid_args = optarg;
break;
default:
func0();
}
}
if ( opt_test || opt_testmsguid || opt_testdceuuid )
{
uuid::uuid((uuid *)v6, uuid_args);
uuid::operator=(&uuid1, v6);
uuid::~uuid(v6);
if ( (unsigned __int8)uuid::IsNil((uuid *)&uuid1) )
{
fwrite("Error: argument must be a valid UUID\n", 1uLL, 0x25uLL, stderr);
exit(1);
}
}
if ( opt_equals || opt_lessthan || opt_greaterthan )
{
if ( !uuid_args )
{
fwrite("Error: need UUID arguments \n", 1uLL, 0x1CuLL, stderr);
exit(1);
}
v10 = strtok(uuid_args, ",:/");
if ( v10 )
{
uuid::uuid((uuid *)v7, v10);
uuid::operator=(&uuid1, v7);
uuid::~uuid(v7);
}
v10 = strtok(0LL, ",:/");
if ( v10 )
{
uuid::uuid((uuid *)v8, v10);
uuid::operator=(&uuid2, v8);
uuid::~uuid(v8);
}
if ( (unsigned __int8)uuid::IsNil((uuid *)&uuid1) || (unsigned __int8)uuid::IsNil((uuid *)&uuid2) )
{
fwrite("Error: one or both of the UUID arguments is invalid\n", 1uLL, 0x34uLL, stderr);
exit(1);
}
}
if ( opt_idl_hdrgen )
func1();
if ( opt_com_initializer )
func3();
if ( opt_c_initializer )
func2();
if ( opt_testmsguid )
func4();
if ( opt_testdceuuid )
func5();
if ( opt_lessthan )
func6();
if ( opt_greaterthan )
func7();
if ( opt_equals )
func8();
if ( opt_test )
func5();
for ( i = 0; i < num_uuids; ++i )
{
uuid::uuid((uuid *)v9);
v4 = (const char *)uuid::String((uuid *)v9);
fprintf(stdout, "%s\n", v4);
uuid::~uuid(v9);
}
exit(0);
}

original_code:
int main(int argc, char** argv)
{
  struct pollfd fds[2];
  if (argc != 4) {
    fprintf(stderr,"Usage: sample_m_client <host> <port> <receiver>\n");
    return 1;
  }
  void* conn = cfm_connection_open(argv[1],atoi(argv[2]));
  if (!conn) {
    fprintf(stderr,"Connection could not be established\n");
    return 1;
  }
  int channel =
    cfm_channel_open(conn,
                     "433e76d0-77d1-460d-9321-e2dc8dc8bd59",
                     argv[3],
                     myOpenCB,
                     myCloseCB,
                     myMsgCB,
                     myErrorCB,
                     ((void *)0));
  if (channel == -1) {
    fprintf(stderr,"Channel could not be opened\n");
    cfm_connection_close(conn);
  }
  channel = cfm_channel_open(conn,
                             "433e76d0-77d1-460d-9321-e2dc8dc8bd59",
                             argv[3],
                             myOpenCB,
                             myCloseCB,
                             myMsgCB,
                             myErrorCB,
                             ((void *)0));
  if (channel == -1) {
    fprintf(stderr,"Channel could not be opened\n");
    cfm_connection_close(conn);
  }
  fds[0].fd = cfm_connection_sd_get(conn);
  fds[0].events = 0x001;
  int res;
  while (1) {
    res = poll(fds, 1, 10);
    if (fds[0].revents == 0x001) {
      if (cfm_sockets_handle(fds[0].fd) < 0) {
        conn = cfm_connection_find(fds[0].fd);
        cfm_connection_close(conn);
        fprintf(stderr,"Info: Connection closed!\n");
        return 1;
      }
    }
  }
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  struct pollfd fds[2];
  if (argc != 4) {
    fprintf(stderr,"Usage: sample_m_client <host> <port> <receiver>\n");
    return 1;
  }
  void *conn = cfm_connection_open(argv[1],atoi(argv[2]));
  if (!conn) {
    fprintf(stderr,"Connection could not be established\n");
    return 1;
  }
  int channel = cfm_channel_open(conn,
                     "433e76d0-77d1-460d-9321-e2dc8dc8bd59",
                     argv[3],
                     myOpenCB,
                     myCloseCB,
                     myMsgCB,
                     myErrorCB,
                     ((void *)0));
  if (channel == -1) {
    fprintf(stderr,"Channel could not be opened\n");
    cfm_connection_close(conn);
  }
  channel = cfm_channel_open(conn,
                             "433e76d0-77d1-460d-9321-e2dc8dc8bd59",
                             argv[3],
                             myOpenCB,
                             myCloseCB,
                             myMsgCB,
                             myErrorCB,
                             ((void *)0));
  if (channel == -1) {
    fprintf(stderr,"Channel could not be opened\n");
    cfm_connection_close(conn);
  }
  fds[0].fd = cfm_connection_sd_get(conn);
  fds[0].events = 0x001;
  int res;
  while (1) {
    res = poll(fds, 1, 10);
    if (fds[0].revents == 0x001) {
      if (cfm_sockets_handle(fds[0].fd) < 0) {
        conn = cfm_connection_find(fds[0].fd);
        cfm_connection_close(conn);
        fprintf(stderr,"Info: Connection closed!\n");
        return 1;
      }
    }
  }
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct pollfd { int fd; short events; short revents; } [2]

variable_types:
conn
void *

variable_types: channel int

variable_types: res int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // r13
int v4; // ebx
char *v5; // r14
unsigned __int8 *v6; // r8
int v7; // eax
int v8; // er12
int v10; // eax
CFRegistry *v11; // rdi
char *v12; // r12
DIR *v13; // rbp
struct dirent *v14; // rax
CFRegistry *v15; // rdi
CFRegistry *v16; // rdi
CFRegistry *v17; // rdi
CFRegistry *v18; // rdi
__int64 v19; // rbp
__int64 (__fastcall *v20)(__int64, char *, const char *); // rbx
__int64 v21; // rax
CFRegistry *v22; // rdi
__int64 v23; // rax
__int64 v24; // rax
int v25; // eax
size_t v26; // rax
void *v27; // r14
void (*v28)(void); // r14
char *v29; // rax
char *save_ptr; // [rsp+8h] [rbp-440h] BYREF
char s[8]; // [rsp+10h] [rbp-438h] BYREF
__int64 v32; // [rsp+18h] [rbp-430h]
char v33[1064]; // [rsp+20h] [rbp-428h] BYREF

if ( argc > 1 )
{
v3 = argv + 2;
v4 = 3;
v5 = 0LL;
while ( 1 )
{
while ( 1 )
{
v6 = (unsigned __int8 *)*(v3 - 1);
v7 = *v6;
if ( v7 == 45 && v6[1] == 104 && !v6[2] || !strcmp(*(v3 - 1), "--help") )
{
print_usage();
return 0;
}
if ( v7 == 45 && v6[1] == 118 && !v6[2] || !strcmp(*(v3 - 1), "--version") )
{
v8 = 0;
fprintf(
stderr,
"compframe %s\n"
"Copyright (C) 2007-2013 Peter R. Torpman (peter@torpman.se)\n"
"This is free software. \n"
"You may redistribute copies of it under the terms of the GNU General Public License \n"
"<http://www.gnu.org/licenses/gpl.html>.\n"
"There is NO WARRANTY, to the extent permitted by law.\n",
"0.5.3");
return v8;
}
if ( v7 != 45 )
goto LABEL_12;
if ( v6[1] != 100 || v6[2] )
break;
cf_trace_log("CFMain.cc", 151LL, 1LL, "Handling -d \n", *v3);
v5 = (char *)*v3;
if ( !*v3 )
goto LABEL_13;
LABEL_38:
v25 = v4;
v3 += 2;
v4 += 2;
if ( v25 >= argc )
{
LABEL_23:
if ( !v5 )
goto LABEL_59;
goto LABEL_24;
}
}
if ( v6[1] == 102 && !v6[2] )
{
cf_trace_log("CFMain.cc", 166LL, 1LL, "Handling -c \n", *v3);
if ( !*v3 )
goto LABEL_13;
cfgFile = (__int64)*v3;
goto LABEL_38;
}
if ( v6[1] != 116 || v6[2] )
{
LABEL_12:
cf_error_log("CFMain.cc", 210LL, "Bad parameter! (%s)\n", *(v3 - 1));
LABEL_13:
print_usage();
return 1;
}
cf_trace_log("CFMain.cc", 183LL, 1LL, "Handling -t \n", *v3);
if ( !*v3 )
{
print_usage();
goto LABEL_54;
}
if ( sscanf(*v3, "%d", s) != 1 )
{
cf_error_log("CFMain.cc", 194LL, "Bad trace level! (%s)\n", *v3);
goto LABEL_54;
}
if ( *(_DWORD *)s > 3u )
{
cf_error_log("CFMain.cc", 200LL, "Bad trace level! (%s)\n", *v3);
goto LABEL_54;
}
cf_trace_level_set();
v10 = v4;
v3 += 2;
v4 += 2;
if ( v10 >= argc )
goto LABEL_23;
}
}
LABEL_59:
v5 = getenv("CF_COMP_DIR");
if ( v5 )
{
LABEL_24:
v11 = (CFRegistry *)v5;
v12 = strtok_r(v5, ":\n\t ", &save_ptr);
if ( v12 )
{
while ( 1 )
{
v13 = opendir(v12);
if ( !v13 )
break;
cf_trace_log("CFMain.cc", 245LL, 1LL, "Looking for component libraries in - %s \n", v12);
while ( 1 )
{
v14 = readdir(v13);
if ( !v14 )
break;
sprintf(s, "%s/%s", v12, v14->d_name);
if ( strstr(s, ".so") )
{
cf_trace_log("CFMain.cc", 252LL, 1LL, " Init lib - %s \n", s);
v26 = strlen(s);
if ( v26 > 3
&& *((_BYTE *)&save_ptr + v26 + 5) == 46
&& *((_BYTE *)&save_ptr + v26 + 6) == 115
&& *((_BYTE *)&save_ptr + v26 + 7) == 111 )
{
v27 = dlopen(s, 1);
if ( v27 )
{
dlerror();
v28 = (void (*)(void))dlsym(v27, "dlopen_this");
if ( !dlerror() )
v28();
}
else
{
v29 = dlerror();
cf_error_log("CFMain.cc", 83LL, "Could not open %s! (Error=%s)\n", s, v29);
}
}
}
}
v11 = 0LL;
v12 = strtok_r(0LL, ":\n\t", &save_ptr);
if ( !v12 )
goto LABEL_30;
}
cf_error_log("CFMain.cc", 237LL, "Bad component directory! (%s)\n", v12);
LABEL_54:
v8 = 1;
}
else
{
LABEL_30:
v15 = (CFRegistry *)CFRegistry::instance(v11);
(*(void (__fastcall **)(CFRegistry *, const char *))(*(_QWORD *)v15 + 40LL))(v15, "S");
v16 = (CFRegistry *)CFRegistry::instance(v15);
(*(void (__fastcall **)(CFRegistry *, const char *))(*(_QWORD *)v16 + 40LL))(v16, "C");
v17 = (CFRegistry *)CFRegistry::instance(v16);
(*(void (__fastcall **)(CFRegistry *, const char *))(*(_QWORD *)v17 + 40LL))(v17, "M");
v18 = (CFRegistry *)CFRegistry::instance(v17);
(*(void (__fastcall **)(CFRegistry *, const char *))(*(_QWORD *)v18 + 40LL))(v18, "Cfg");
v19 = CFRegistry::instance(v18);
v20 = *(__int64 (__fastcall **)(__int64, char *, const char *))(*(_QWORD *)v19 + 104LL);
*(_QWORD *)s = v33;
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::_S_copy_chars(v33, "S");
v32 = 1LL;
*(_BYTE *)(*(_QWORD *)s + 1LL) = 0;
v21 = v20(v19, s, "ISchedulerControl");
if ( v21 )
v21 -= 8LL;
sIface = v21;
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::_M_dispose(s);
if ( !cfgFile )
goto LABEL_45;
v22 = (CFRegistry *)CFRegistry::instance((CFRegistry *)s);
cfgObj = (*(__int64 (__fastcall **)(CFRegistry *, const char *))(*(_QWORD *)v22 + 24LL))(v22, "Cfg");
v23 = CFRegistry::instance(v22);
v24 = (*(__int64 (__fastcall **)(__int64, __int64, const char *))(*(_QWORD *)v23 + 72LL))(v23, cfgObj, "IConfig");
if ( !v24 )
main_cold();
v8 = (*(__int64 (__fastcall **)(__int64, __int64))(*(_QWORD *)(v24 - 8) + 16LL))(v24 - 8, cfgFile);
if ( v8 )
{
LABEL_45:
v8 = 0;
(*(void (__fastcall **)(__int64))(*(_QWORD *)sIface + 16LL))(sIface);
}
else
{
cf_error_log("CFMain.cc", 292LL, "Configuration file error!\n");
}
}
}
else
{
cf_error_log("CFMain.cc", 226LL, "No component directory specified! (CF_COMP_DIR)\n");
v8 = 1;
}
return v8;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // r13
int v4; // ebx
char *v5; // r14
unsigned __int8 *v6; // r8
int v7; // eax
int v8; // er12
int v10; // eax
CFRegistry *v11; // rdi
char *v12; // r12
DIR *v13; // rbp
struct dirent *v14; // rax
CFRegistry *v15; // rdi
CFRegistry *v16; // rdi
CFRegistry *v17; // rdi
CFRegistry *v18; // rdi
__int64 v19; // rbp
__int64 (__fastcall *v20)(__int64, char *, const char *); // rbx
__int64 v21; // rax
CFRegistry *v22; // rdi
__int64 v23; // rax
__int64 v24; // rax
int v25; // eax
size_t v26; // rax
void *v27; // r14
void (*v28)(void); // r14
char *v29; // rax
char *save_ptr; // [rsp+8h] [rbp-440h] BYREF
char s[8]; // [rsp+10h] [rbp-438h] BYREF
__int64 v32; // [rsp+18h] [rbp-430h]
char v33[1064]; // [rsp+20h] [rbp-428h] BYREF

if ( argc > 1 )
{
v3 = argv + 2;
v4 = 3;
v5 = 0LL;
while ( 1 )
{
while ( 1 )
{
v6 = (unsigned __int8 *)*(v3 - 1);
v7 = *v6;
if ( v7 == 45 && v6[1] == 104 && !v6[2] || !strcmp(*(v3 - 1), "--help") )
{
func0();
return 0;
}
if ( v7 == 45 && v6[1] == 118 && !v6[2] || !strcmp(*(v3 - 1), "--version") )
{
v8 = 0;
fprintf(
stderr,
"compframe %s\n"
"Copyright (C) 2007-2013 Peter R. Torpman (peter@torpman.se)\n"
"This is free software. \n"
"You may redistribute copies of it under the terms of the GNU General Public License \n"
"<http://www.gnu.org/licenses/gpl.html>.\n"
"There is NO WARRANTY, to the extent permitted by law.\n",
"0.5.3");
return v8;
}
if ( v7 != 45 )
goto LABEL_12;
if ( v6[1] != 100 || v6[2] )
break;
cf_trace_log("CFMain.cc", 151LL, 1LL, "Handling -d \n", *v3);
v5 = (char *)*v3;
if ( !*v3 )
goto LABEL_13;
LABEL_38:
v25 = v4;
v3 += 2;
v4 += 2;
if ( v25 >= argc )
{
LABEL_23:
if ( !v5 )
goto LABEL_59;
goto LABEL_24;
}
}
if ( v6[1] == 102 && !v6[2] )
{
cf_trace_log("CFMain.cc", 166LL, 1LL, "Handling -c \n", *v3);
if ( !*v3 )
goto LABEL_13;
cfgFile = (__int64)*v3;
goto LABEL_38;
}
if ( v6[1] != 116 || v6[2] )
{
LABEL_12:
cf_error_log("CFMain.cc", 210LL, "Bad parameter! (%s)\n", *(v3 - 1));
LABEL_13:
func0();
return 1;
}
cf_trace_log("CFMain.cc", 183LL, 1LL, "Handling -t \n", *v3);
if ( !*v3 )
{
func0();
goto LABEL_54;
}
if ( sscanf(*v3, "%d", s) != 1 )
{
cf_error_log("CFMain.cc", 194LL, "Bad trace level! (%s)\n", *v3);
goto LABEL_54;
}
if ( *(_DWORD *)s > 3u )
{
cf_error_log("CFMain.cc", 200LL, "Bad trace level! (%s)\n", *v3);
goto LABEL_54;
}
cf_trace_level_set();
v10 = v4;
v3 += 2;
v4 += 2;
if ( v10 >= argc )
goto LABEL_23;
}
}
LABEL_59:
v5 = getenv("CF_COMP_DIR");
if ( v5 )
{
LABEL_24:
v11 = (CFRegistry *)v5;
v12 = strtok_r(v5, ":\n\t ", &save_ptr);
if ( v12 )
{
while ( 1 )
{
v13 = opendir(v12);
if ( !v13 )
break;
cf_trace_log("CFMain.cc", 245LL, 1LL, "Looking for component libraries in - %s \n", v12);
while ( 1 )
{
v14 = readdir(v13);
if ( !v14 )
break;
sprintf(s, "%s/%s", v12, v14->d_name);
if ( strstr(s, ".so") )
{
cf_trace_log("CFMain.cc", 252LL, 1LL, " Init lib - %s \n", s);
v26 = strlen(s);
if ( v26 > 3
&& *((_BYTE *)&save_ptr + v26 + 5) == 46
&& *((_BYTE *)&save_ptr + v26 + 6) == 115
&& *((_BYTE *)&save_ptr + v26 + 7) == 111 )
{
v27 = dlopen(s, 1);
if ( v27 )
{
dlerror();
v28 = (void (*)(void))dlsym(v27, "dlopen_this");
if ( !dlerror() )
v28();
}
else
{
v29 = dlerror();
cf_error_log("CFMain.cc", 83LL, "Could not open %s! (Error=%s)\n", s, v29);
}
}
}
}
v11 = 0LL;
v12 = strtok_r(0LL, ":\n\t", &save_ptr);
if ( !v12 )
goto LABEL_30;
}
cf_error_log("CFMain.cc", 237LL, "Bad component directory! (%s)\n", v12);
LABEL_54:
v8 = 1;
}
else
{
LABEL_30:
v15 = (CFRegistry *)CFRegistry::instance(v11);
(*(void (__fastcall **)(CFRegistry *, const char *))(*(_QWORD *)v15 + 40LL))(v15, "S");
v16 = (CFRegistry *)CFRegistry::instance(v15);
(*(void (__fastcall **)(CFRegistry *, const char *))(*(_QWORD *)v16 + 40LL))(v16, "C");
v17 = (CFRegistry *)CFRegistry::instance(v16);
(*(void (__fastcall **)(CFRegistry *, const char *))(*(_QWORD *)v17 + 40LL))(v17, "M");
v18 = (CFRegistry *)CFRegistry::instance(v17);
(*(void (__fastcall **)(CFRegistry *, const char *))(*(_QWORD *)v18 + 40LL))(v18, "Cfg");
v19 = CFRegistry::instance(v18);
v20 = *(__int64 (__fastcall **)(__int64, char *, const char *))(*(_QWORD *)v19 + 104LL);
*(_QWORD *)s = v33;
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::_S_copy_chars(v33, "S");
v32 = 1LL;
*(_BYTE *)(*(_QWORD *)s + 1LL) = 0;
v21 = v20(v19, s, "ISchedulerControl");
if ( v21 )
v21 -= 8LL;
sIface = v21;
std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>>::_M_dispose(s);
if ( !cfgFile )
goto LABEL_45;
v22 = (CFRegistry *)CFRegistry::instance((CFRegistry *)s);
cfgObj = (*(__int64 (__fastcall **)(CFRegistry *, const char *))(*(_QWORD *)v22 + 24LL))(v22, "Cfg");
v23 = CFRegistry::instance(v22);
v24 = (*(__int64 (__fastcall **)(__int64, __int64, const char *))(*(_QWORD *)v23 + 72LL))(v23, cfgObj, "IConfig");
if ( !v24 )
func1();
v8 = (*(__int64 (__fastcall **)(__int64, __int64))(*(_QWORD *)(v24 - 8) + 16LL))(v24 - 8, cfgFile);
if ( v8 )
{
LABEL_45:
v8 = 0;
(*(void (__fastcall **)(__int64))(*(_QWORD *)sIface + 16LL))(sIface);
}
else
{
cf_error_log("CFMain.cc", 292LL, "Configuration file error!\n");
}
}
}
else
{
cf_error_log("CFMain.cc", 226LL, "No component directory specified! (CF_COMP_DIR)\n");
v8 = 1;
}
return v8;
}

user_defined_types: struct pollfd { int fd; short events; short revents; }


repo: ptorpman/threet

decompiled_code:
__int64 __fastcall tcp_create_server_socket(__int64 a1, int a2)
{
int v2; // eax
unsigned int v3; // ebx
int v4; // er12

v2 = server_create_and_bind_socket(a1, 1LL);
if ( v2 < 0 )
{
inform_user("ERROR: Failed to open socket!\n");
v3 = -1;
}
else
{
v3 = v2;
v4 = listen(v2, a2);
if ( v4 < 0 )
{
inform_user("ERROR: Failed to listen on socket!\n");
v3 = v4;
}
else
{
inform_user("* Server socket bound to port %u\n", (unsigned int)a1);
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
int v2; // eax
unsigned int v3; // ebx
int v4; // er12

v2 = server_create_and_bind_socket(a1, 1LL);
if ( v2 < 0 )
{
inform_user("ERROR: Failed to open socket!\n");
v3 = -1;
}
else
{
v3 = v2;
v4 = listen(v2, a2);
if ( v4 < 0 )
{
inform_user("ERROR: Failed to listen on socket!\n");
v3 = v4;
}
else
{
inform_user("* Server socket bound to port %u\n", (unsigned int)a1);
}
}
return v3;
}

original_code:
static int
tcp_create_server_socket(uint32_t port, uint32_t numConn)
{
    int sock = server_create_and_bind_socket(port, SOCK_STREAM);
    if (sock < 0) {
        inform_user("ERROR: Failed to open socket!\n");
        return -1;
    }
    int res = listen(sock, numConn);
    if (res < 0) {
        inform_user("ERROR: Failed to listen on socket!\n");
        return res;
    }
    inform_user("* Server socket bound to port %u\n", port);
    return sock;
}

canonical_original_code:
static int tcp_create_server_socket(unsigned int port, unsigned int numConn)
{
    int sock = server_create_and_bind_socket(port, SOCK_STREAM);
    if (sock < 0) {
        inform_user("ERROR: Failed to open socket!\n");
        return -1;
    }
    int res = listen(sock, numConn);
    if (res < 0) {
        inform_user("ERROR: Failed to listen on socket!\n");
        return res;
    }
    inform_user("* Server socket bound to port %u\n", port);
    return sock;
}

variable_types:
port
unsigned int

variable_types:
numConn
unsigned int

variable_types: sock int


decompiled_code:
__int64 __fastcall accept_new_connection(int a1)
{
int v1; // ebx
socklen_t addr_len; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

addr_len = 16;
v1 = accept(a1, &addr, &addr_len);
if ( v1 < 0 )
inform_user("ERROR: Failed to accept connection!\n");
inform_user("ACCEPT: %d!\n", (unsigned int)v1);
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
int v1; // ebx
socklen_t addr_len; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

addr_len = 16;
v1 = accept(a1, &addr, &addr_len);
if ( v1 < 0 )
inform_user("ERROR: Failed to accept connection!\n");
inform_user("ACCEPT: %d!\n", (unsigned int)v1);
return (unsigned int)v1;
}

original_code:
static int
accept_new_connection(int fd)
{
    struct sockaddr_in cliAddr;
    uint32_t cliLen = sizeof(struct sockaddr_in);
    int res;
    res = accept(fd, (struct sockaddr*)&cliAddr, &cliLen);
    if (res < 0) {
        inform_user("ERROR: Failed to accept connection!\n");
    }
    inform_user("ACCEPT: %d!\n", res);
    return res;
}

canonical_original_code:
static int accept_new_connection(int fd)
{
    struct sockaddr_in cliAddr;
    unsigned int cliLen = sizeof(struct sockaddr_in);
    int res;
    res = accept(fd, (struct sockaddr *)&cliAddr, &cliLen);
    if (res < 0) {
        inform_user("ERROR: Failed to accept connection!\n");
    }
    inform_user("ACCEPT: %d!\n", res);
    return res;
}

variable_types: fd int

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

variable_types:
cliLen
unsigned int


decompiled_code:
void __fastcall __noreturn tcp_recv_thread(void *a1)
{
int v1; // eax
int v2; // ebx
socklen_t addr_len; // [rsp+Ch] [rbp-1003Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-10038h] BYREF
char buf[65576]; // [rsp+20h] [rbp-10028h] BYREF

while ( 1 )
{
addr_len = 0;
v1 = recvfrom(*(_DWORD *)a1, buf, 0xFFFFuLL, 0, &addr, &addr_len);
v2 = v1;
if ( v1 > 0 )
{
pthread_mutex_lock((pthread_mutex_t *)((char *)a1 + 24));
*((_QWORD *)a1 + 9) += v2;
*((_QWORD *)a1 + 8) += v2;
pthread_mutex_unlock((pthread_mutex_t *)((char *)a1 + 24));
}
else if ( !v1 )
{
pthread_exit(a1);
}
}
}

canonical_decompiled_code:
void __fastcall __noreturn func3(void *a1)
{
int v1; // eax
int v2; // ebx
socklen_t addr_len; // [rsp+Ch] [rbp-1003Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-10038h] BYREF
char buf[65576]; // [rsp+20h] [rbp-10028h] BYREF

while ( 1 )
{
addr_len = 0;
v1 = recvfrom(*(_DWORD *)a1, buf, 0xFFFFuLL, 0, &addr, &addr_len);
v2 = v1;
if ( v1 > 0 )
{
pthread_mutex_lock((pthread_mutex_t *)((char *)a1 + 24));
*((_QWORD *)a1 + 9) += v2;
*((_QWORD *)a1 + 8) += v2;
pthread_mutex_unlock((pthread_mutex_t *)((char *)a1 + 24));
}
else if ( !v1 )
{
pthread_exit(a1);
}
}
}

original_code:
static void*
tcp_recv_thread(void* data)
{
    client_data_t* cliData = (client_data_t*)data;
    char buff[0xFFFF];
    size_t buffLen = 0xFFFF;
    int res = 0;
    while (1) {
        struct sockaddr_in sockAddr;
        socklen_t addrLen = 0;
        res = recvfrom(cliData->sock, buff, buffLen, 0, (struct sockaddr*) &sockAddr, &addrLen);
        if (res > 0) {
            pthread_mutex_lock(&cliData->mutex);
            cliData->numSinceMeasure += res;
            cliData->numSinceStart += res;
            pthread_mutex_unlock(&cliData->mutex);
        }
        if (res == 0) {
            pthread_exit(cliData);
            server_remove(cliData);
            free(cliData);
        }
    }
    pthread_exit(cliData);
}

canonical_original_code:
static void *tcp_recv_thread(void *data)
{
    struct client_data_t *cliData = (client_data_t*)data;
    char buff[65535];
    unsigned long buffLen = 0xFFFF;
    int res = 0;
    while (1) {
        struct sockaddr_in sockAddr;
        unsigned int addrLen = 0;
        res = recvfrom(cliData->sock, buff, buffLen, 0, (struct sockaddr *) &sockAddr, &addrLen);
        if (res > 0) {
            pthread_mutex_lock(&cliData->mutex);
            cliData->numSinceMeasure += res;
            cliData->numSinceStart += res;
            pthread_mutex_unlock(&cliData->mutex);
        }
        if (res == 0) {
            pthread_exit(cliData);
            server_remove(cliData);
            free(cliData);
        }
    }
    pthread_exit(cliData);
}

variable_types:
data
void *

variable_types: struct client_data_t { int sock; int clientNum; int port; unsigned long thread; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; unsigned long numSinceStart; unsigned long numSinceMeasure; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } serverAddress; } *

variable_types:
buff
char [65535]

variable_types:
buffLen
unsigned long

variable_types:
addrLen
unsigned int

return_type: void *

user_defined_types: struct client_data_t { int sock; int clientNum; int port; unsigned long thread; union pthread_mutex_t mutex; unsigned long numSinceStart; unsigned long numSinceMeasure; struct sockaddr_in serverAddress; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
__int64 __fastcall tcp_create_server(__int64 a1, unsigned int a2, int a3)
{
__int64 result; // rax

result = tcp_create_server_socket(a2, a3);
serverSocket = result;
if ( (int)result < 0 )
{
inform_user("ERROR: Failed to open TCP server socket!\n");
result = (unsigned int)serverSocket;
}
return result;
}

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

result = func0(a2, a3);
serverSocket = result;
if ( (int)result < 0 )
{
inform_user("ERROR: Failed to open TCP server socket!\n");
result = (unsigned int)serverSocket;
}
return result;
}

original_code:
int
tcp_create_server(client_data_t** servers, uint32_t basePort, uint32_t numConn)
{
    serverSocket = tcp_create_server_socket(basePort, numConn);
    if (serverSocket < 0) {
        inform_user("ERROR: Failed to open TCP server socket!\n");
        return serverSocket;
    }
    return serverSocket;
}

canonical_original_code:
int tcp_create_server(struct client_data_t **servers, unsigned int basePort, unsigned int numConn)
{
    serverSocket = tcp_create_server_socket(basePort, numConn);
    if (serverSocket < 0) {
        inform_user("ERROR: Failed to open TCP server socket!\n");
        return serverSocket;
    }
    return serverSocket;
}

variable_types: struct client_data_t { int sock; int clientNum; int port; unsigned long thread; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; unsigned long numSinceStart; unsigned long numSinceMeasure; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } serverAddress; } **

variable_types:
basePort
unsigned int


decompiled_code:
__int64 tcp_handle_new_connection()
{
unsigned int v0; // ebx
int v2; // edi
struct pollfd fds; // [rsp+8h] [rbp-10h] BYREF

fds.fd = serverSocket;
fds.events = 1;
v0 = 0;
if ( poll(&fds, 1uLL, 10) > 0 && fds.revents == 1 )
{
v2 = accept_new_connection(fds.fd);
if ( v2 < 0 )
{
inform_user("ERROR: Failed to accept connection!\n");
v0 = -1;
}
else
{
v0 = tcp_create_receiver(v2, 1);
if ( v0 )
{
inform_user("ERROR: Failed to start client thread!\n");
v0 = -1;
}
else
{
inform_user("Client connected!\n");
}
}
}
return v0;
}

canonical_decompiled_code:
__int64 func5()
{
unsigned int v0; // ebx
int v2; // edi
struct pollfd fds; // [rsp+8h] [rbp-10h] BYREF

fds.fd = serverSocket;
fds.events = 1;
v0 = 0;
if ( poll(&fds, 1uLL, 10) > 0 && fds.revents == 1 )
{
v2 = func1(fds.fd);
if ( v2 < 0 )
{
inform_user("ERROR: Failed to accept connection!\n");
v0 = -1;
}
else
{
v0 = func2(v2, 1);
if ( v0 )
{
inform_user("ERROR: Failed to start client thread!\n");
v0 = -1;
}
else
{
inform_user("Client connected!\n");
}
}
}
return v0;
}

original_code:
int
tcp_handle_new_connection(void)
{
    struct pollfd fd[1];
    int numConnected = 1;
    fd[0].fd = serverSocket;
    fd[0].events = 0x001;
    int res = poll(fd, 1, 10);
    if (res < 0) {
        return 0;
    }
    if (res > 0 && fd[0].revents == 0x001) {
        int cliSock = accept_new_connection(fd[0].fd);
        if (cliSock < 0) {
            inform_user("ERROR: Failed to accept connection!\n");
            return -1;
        }
        if (tcp_create_receiver(cliSock, numConnected) != 0) {
            inform_user("ERROR: Failed to start client thread!\n");
            return -1;
        }
        inform_user("Client connected!\n");
        numConnected++;
    }
    return 0;
}

canonical_original_code:
int tcp_handle_new_connection()
{
    struct pollfd fd[1];
    int numConnected = 1;
    fd[0].fd = serverSocket;
    fd[0].events = 0x001;
    int res = poll(fd, 1, 10);
    if (res < 0) {
        return 0;
    }
    if (res > 0 && fd[0].revents == 0x001) {
        int cliSock = accept_new_connection(fd[0].fd);
        if (cliSock < 0) {
            inform_user("ERROR: Failed to accept connection!\n");
            return -1;
        }
        if (tcp_create_receiver(cliSock, numConnected) != 0) {
            inform_user("ERROR: Failed to start client thread!\n");
            return -1;
        }
        inform_user("Client connected!\n");
        numConnected++;
    }
    return 0;
}

variable_types: struct pollfd { int fd; short events; short revents; } [1]

variable_types: numConnected int

variable_types: cliSock int


decompiled_code:
__int64 __fastcall aggregator_register(char a1, const char *a2)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

buf[0] = a1;
return send_msg_to_aggregator(buf, 1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func2(char a1, const char *a2)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

buf[0] = a1;
return func1(buf, 1, a2);
}

original_code:
int
aggregator_register(int proc_id, char* server_ip)
{
    uint8_t msg[1];
    msg[0] = proc_id;
    return send_msg_to_aggregator(msg, 1, server_ip);
}

canonical_original_code:
int aggregator_register(int proc_id, char *server_ip)
{
    unsigned char msg[1];
    msg[0] = proc_id;
    return send_msg_to_aggregator(msg, 1, server_ip);
}

variable_types:
msg
unsigned char [1]


decompiled_code:
__int64 __fastcall aggregator_report(char a1, int a2, const char *a3)
{
char buf[13]; // [rsp+Bh] [rbp-Dh] BYREF

buf[0] = a1;
buf[1] = HIBYTE(a2);
buf[2] = BYTE2(a2);
buf[3] = BYTE1(a2);
buf[4] = a2;
return send_msg_to_aggregator(buf, 5, a3);
}

canonical_decompiled_code:
__int64 __fastcall func3(char a1, int a2, const char *a3)
{
char buf[13]; // [rsp+Bh] [rbp-Dh] BYREF

buf[0] = a1;
buf[1] = HIBYTE(a2);
buf[2] = BYTE2(a2);
buf[3] = BYTE1(a2);
buf[4] = a2;
return func1(buf, 5, a3);
}

original_code:
int
aggregator_report(int proc_id, uint32_t tput, char* server_ip)
{
    uint8_t msg[5];
    msg[0] = proc_id;
    msg[1] = (tput >> 24) & 0xFF;
    msg[2] = (tput >> 16) & 0xFF;
    msg[3] = (tput >> 8) & 0xFF;
    msg[4] = tput & 0xFF;
    return send_msg_to_aggregator(msg, 5, server_ip);
}

canonical_original_code:
int aggregator_report(int proc_id, unsigned int tput, char *server_ip)
{
    unsigned char msg[5];
    msg[0] = proc_id;
    msg[1] = (tput >> 24) & 0xFF;
    msg[2] = (tput >> 16) & 0xFF;
    msg[3] = (tput >> 8) & 0xFF;
    msg[4] = tput & 0xFF;
    return send_msg_to_aggregator(msg, 5, server_ip);
}

variable_types:
tput
unsigned int

variable_types:
msg
unsigned char [5]


decompiled_code:
__int64 __fastcall send_msg_to_aggregator(void *buf, int a2, const char *a3)
{
struct hostent *v5; // rax
int v6; // eax
int v7; // ebx
__int64 v8; // rdi
__int16 v9; // ax
int v10; // ebx
struct sockaddr v12; // [rsp+0h] [rbp-38h] BYREF
int optval; // [rsp+18h] [rbp-20h] BYREF
struct in_addr inp; // [rsp+1Ch] [rbp-1Ch] BYREF

optval = 1;
*(_QWORD *)&v12.sa_family = 0LL;
*(_QWORD *)&v12.sa_data[6] = 0LL;
v5 = gethostbyname(a3);
if ( v5 )
{
inp.s_addr = **(_DWORD **)v5->h_addr_list;
}
else if ( !inet_aton(a3, &inp) )
{
debug_print("ERROR: Unknown server address!\n");
return (unsigned int)-1;
}
v6 = socket(2, 2, 0);
v7 = v6;
if ( v6 < 0 )
{
debug_print("ERROR: Failed to open socket!\n");
v10 = -1;
}
else
{
v8 = (unsigned int)v6;
setsockopt(v6, 1, 2, &optval, 4u);
v12.sa_family = 2;
*(struct in_addr *)&v12.sa_data[2] = inp;
v9 = get_aggregator_port(v8, 1LL);
*(_WORD *)v12.sa_data = _bswap_16(v9);
v10 = sendto(v7, buf, a2, 0, &v12, 0x10u);
if ( v10 < 0 )
inform_user("ERROR: Failed to send to aggregator!\n");
}
return (unsigned int)v10;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *buf, int a2, const char *a3)
{
struct hostent *v5; // rax
int v6; // eax
int v7; // ebx
__int64 v8; // rdi
__int16 v9; // ax
int v10; // ebx
struct sockaddr v12; // [rsp+0h] [rbp-38h] BYREF
int optval; // [rsp+18h] [rbp-20h] BYREF
struct in_addr inp; // [rsp+1Ch] [rbp-1Ch] BYREF

optval = 1;
*(_QWORD *)&v12.sa_family = 0LL;
*(_QWORD *)&v12.sa_data[6] = 0LL;
v5 = gethostbyname(a3);
if ( v5 )
{
inp.s_addr = **(_DWORD **)v5->h_addr_list;
}
else if ( !inet_aton(a3, &inp) )
{
debug_print("ERROR: Unknown server address!\n");
return (unsigned int)-1;
}
v6 = socket(2, 2, 0);
v7 = v6;
if ( v6 < 0 )
{
debug_print("ERROR: Failed to open socket!\n");
v10 = -1;
}
else
{
v8 = (unsigned int)v6;
setsockopt(v6, 1, 2, &optval, 4u);
v12.sa_family = 2;
*(struct in_addr *)&v12.sa_data[2] = inp;
v9 = get_aggregator_port(v8, 1LL);
*(_WORD *)v12.sa_data = func0(v9);
v10 = sendto(v7, buf, a2, 0, &v12, 0x10u);
if ( v10 < 0 )
inform_user("ERROR: Failed to send to aggregator!\n");
}
return (unsigned int)v10;
}

original_code:
static int
send_msg_to_aggregator(uint8_t* msg, int msgLen, char* server_ip)
{
    struct hostent* he;
    struct in_addr inAddr;
    int sock = -1;
    int reuse = 1;
    int res = 0;
    struct sockaddr_in sinAddr;
    memset(&sinAddr, 0, sizeof(struct sockaddr_in));
    he = gethostbyname(server_ip);
    if (!he) {
        if (inet_aton(server_ip, &inAddr) == 0) {
            debug_print( "ERROR: Unknown server address!\n");
            return -1;
        }
    }
    else {
        inAddr = *((struct in_addr*) he->h_addr_list[0]);
    }
    sock = socket(2, SOCK_DGRAM, 0);
    if (sock < 0) {
        debug_print( "ERROR: Failed to open socket!\n");
        return -1;
    }
    setsockopt(sock, 1, 2, (char*) &reuse, sizeof(reuse));
    sinAddr.sin_family = 2;
    sinAddr.sin_addr = inAddr;
    sinAddr.sin_port = htons(get_aggregator_port());
    res = sendto(sock, msg, msgLen, 0,
                 (struct sockaddr*) &sinAddr, sizeof (struct sockaddr_in));
    if (res < 0) {
        inform_user("ERROR: Failed to send to aggregator!\n");
    }
    return res;
}

canonical_original_code:
static int send_msg_to_aggregator(unsigned char *msg, int msgLen, char *server_ip)
{
    struct hostent *he;
    struct in_addr inAddr;
    int sock = -1;
    int reuse = 1;
    int res = 0;
    struct sockaddr_in sinAddr;
    memset(&sinAddr, 0, sizeof(struct sockaddr_in));
    he = gethostbyname(server_ip);
    if (!he) {
        if (inet_aton(server_ip, &inAddr) == 0) {
            debug_print( "ERROR: Unknown server address!\n");
            return -1;
        }
    }
    else {
        inAddr = *((struct in_addr *) he->h_addr_list[0]);
    }
    sock = socket(2, SOCK_DGRAM, 0);
    if (sock < 0) {
        debug_print( "ERROR: Failed to open socket!\n");
        return -1;
    }
    setsockopt(sock, 1, 2, (char *) &reuse, sizeof(reuse));
    sinAddr.sin_family = 2;
    sinAddr.sin_addr = inAddr;
    sinAddr.sin_port = htons(get_aggregator_port());
    res = sendto(sock, msg, msgLen, 0,
                 (struct sockaddr *) &sinAddr, sizeof (struct sockaddr_in));
    if (res < 0) {
        inform_user("ERROR: Failed to send to aggregator!\n");
    }
    return res;
}

variable_types:
msg
unsigned char *

variable_types: msgLen int

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

variable_types:
inAddr
struct in_addr { unsigned int s_addr; }

variable_types: sock int

variable_types: reuse int

variable_types: res int


decompiled_code:
__int64 eternal_loop()
{
__int64 result; // rax

if ( pthread_create(&gTpThread, 0LL, tp_thread, 0LL) < 0 )
return inform_user("ERROR: Failed to start througput thread!\n");
if ( (unsigned int)get_aggregator_port() )
aggregator_register(*(unsigned int *)(gAppArgs + 284), "127.0.0.1");
do
{
while ( *(_DWORD *)gAppArgs )
usleep(0x2710u);
result = tcp_handle_new_connection();
}
while ( (int)result >= 0 );
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax

if ( pthread_create(&gTpThread, 0LL, tp_thread, 0LL) < 0 )
return inform_user("ERROR: Failed to start througput thread!\n");
if ( (unsigned int)get_aggregator_port() )
aggregator_register(*(unsigned int *)(gAppArgs + 284), "127.0.0.1");
do
{
while ( *(_DWORD *)gAppArgs )
usleep(0x2710u);
result = tcp_handle_new_connection();
}
while ( (int)result >= 0 );
return result;
}

original_code:
static void
eternal_loop(void)
{
    int res = pthread_create(&gTpThread, ((void *)0), tp_thread, ((void *)0));
    if (res < 0) {
        inform_user("ERROR: Failed to start througput thread!\n");
        return;
    }
    if (get_aggregator_port() != 0) {
        aggregator_register(gAppArgs->processId, "127.0.0.1");
    }
    while (1) {
        if (!gAppArgs->useUdp) {
            if (tcp_handle_new_connection() < 0) {
                return;
            }
            else {
            }
        } else {
            usleep(10000);
        }
    }
}

canonical_original_code:
static void eternal_loop()
{
    int res = pthread_create(&gTpThread, ((void *)0), tp_thread, ((void *)0));
    if (res < 0) {
        inform_user("ERROR: Failed to start througput thread!\n");
        return;
    }
    if (get_aggregator_port() != 0) {
        aggregator_register(gAppArgs->processId, "127.0.0.1");
    }
    while (1) {
        if (!gAppArgs->useUdp) {
            if (tcp_handle_new_connection() < 0) {
                return;
            }
            else {
            }
        } else {
            usleep(10000);
        }
    }
}


decompiled_code:
__int64 __fastcall server_start(__int64 a1)
{
char *v1; // rax

v1 = (char *)&gServers;
do
{
*(_QWORD *)v1 = 0LL;
v1 += 8;
}
while ( v1 != &byte_7FFF8[(_QWORD)&gServers] );
gAppArgs = a1;
if ( *(_DWORD *)a1 )
{
if ( (int)udp_create_servers(&gServers, *(unsigned __int16 *)(a1 + 8), *(unsigned int *)(a1 + 272)) < 0 )
inform_user("ERROR: Failed to create UDP servers!\n");
return eternal_loop();
}
if ( (int)tcp_create_server(&gServers, *(unsigned __int16 *)(a1 + 8), *(unsigned int *)(a1 + 272)) >= 0 )
return eternal_loop();
return inform_user("ERROR: Failed to open TCP server socket!\n");
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
char *v1; // rax

v1 = (char *)&gServers;
do
{
*(_QWORD *)v1 = 0LL;
v1 += 8;
}
while ( v1 != &byte_7FFF8[(_QWORD)&gServers] );
gAppArgs = a1;
if ( *(_DWORD *)a1 )
{
if ( (int)udp_create_servers(&gServers, *(unsigned __int16 *)(a1 + 8), *(unsigned int *)(a1 + 272)) < 0 )
inform_user("ERROR: Failed to create UDP servers!\n");
return func0();
}
if ( (int)tcp_create_server(&gServers, *(unsigned __int16 *)(a1 + 8), *(unsigned int *)(a1 + 272)) >= 0 )
return func0();
return inform_user("ERROR: Failed to open TCP server socket!\n");
}

original_code:
void server_start(app_arg_t* args)
{
    for(int i = 0; i < 0xFFFF; i++) {
        gServers[i] = ((void *)0);
    }
    gAppArgs = args;
    int res = -1;
    if (!args->useUdp) {
        if (tcp_create_server(gServers, args->serverPort, args->numConnections) < 0) {
            inform_user("ERROR: Failed to open TCP server socket!\n");
            return;
        }
    } else {
        if (udp_create_servers(gServers, args->serverPort, args->numConnections) < 0) {
            inform_user("ERROR: Failed to create UDP servers!\n");
        }
    }
    eternal_loop();
}

canonical_original_code:
void server_start(struct app_arg_t *args)
{
    for(int i = 0; i < 0xFFFF; i++) {
        gServers[i] = ((void *)0);
    }
    gAppArgs = args;
    int res = -1;
    if (!args->useUdp) {
        if (tcp_create_server(gServers, args->serverPort, args->numConnections) < 0) {
            inform_user("ERROR: Failed to open TCP server socket!\n");
            return;
        }
    } else {
        if (udp_create_servers(gServers, args->serverPort, args->numConnections) < 0) {
            inform_user("ERROR: Failed to create UDP servers!\n");
        }
    }
    eternal_loop();
}

variable_types: struct app_arg_t { int useUdp; int isServer; unsigned short serverPort; char serverIp[255]; unsigned int wantedThroughput; int numConnections; int aggregatorPort; unsigned short packetSize; unsigned short usedPacketSize; int processId; } *

variable_types: i int


decompiled_code:
__int64 __fastcall server_create_and_bind_socket(__int16 a1, int a2)
{
int v2; // ebx
int v3; // ebp
struct sockaddr v5; // [rsp+0h] [rbp-38h] BYREF
int optval[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

if ( a2 == 2 )
v2 = socket(2, 2, 17);
else
v2 = socket(2, a2, 0);
if ( v2 < 0 )
{
inform_user("ERROR: Failed to open socket!\n");
v2 = -1;
}
else
{
optval[0] = 1;
setsockopt(v2, 1, 2, optval, 4u);
*(_DWORD *)&v5.sa_data[10] = 0;
v5.sa_family = 2;
*(_QWORD *)&v5.sa_data[2] = (unsigned int)_bswap_32(0);
*(_WORD *)v5.sa_data = _bswap_16(a1);
v3 = bind(v2, &v5, 0x10u);
if ( v3 < 0 )
{
inform_user("ERROR: Failed to bind socket!\n");
v2 = v3;
}
}
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int16 a1, int a2)
{
int v2; // ebx
int v3; // ebp
struct sockaddr v5; // [rsp+0h] [rbp-38h] BYREF
int optval[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

if ( a2 == 2 )
v2 = socket(2, 2, 17);
else
v2 = socket(2, a2, 0);
if ( v2 < 0 )
{
inform_user("ERROR: Failed to open socket!\n");
v2 = -1;
}
else
{
optval[0] = 1;
setsockopt(v2, 1, 2, optval, 4u);
*(_DWORD *)&v5.sa_data[10] = 0;
v5.sa_family = 2;
*(_QWORD *)&v5.sa_data[2] = (unsigned int)func2(0);
*(_WORD *)v5.sa_data = func3(a1);
v3 = bind(v2, &v5, 0x10u);
if ( v3 < 0 )
{
inform_user("ERROR: Failed to bind socket!\n");
v2 = v3;
}
}
return (unsigned int)v2;
}

original_code:
int
server_create_and_bind_socket(int port, int proto)
{
    int sock = 0;
    if (proto == SOCK_DGRAM) {
        sock = socket(2, proto, IPPROTO_UDP);
    } else {
        sock = socket(2, proto, 0);
    }
    if (sock < 0) {
        inform_user("ERROR: Failed to open socket!\n");
        return -1;
    }
    int reuse = 1;
    setsockopt(sock, 1, 2, (char*)&reuse, sizeof(reuse));
    struct sockaddr_in servAddr;
    memset(&servAddr, 0, sizeof(struct sockaddr_in));
    servAddr.sin_family = 2;
    servAddr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
    servAddr.sin_port = htons(port);
    int res = bind(sock, (struct sockaddr*)&servAddr, sizeof(servAddr));
    if (res < 0) {
        inform_user("ERROR: Failed to bind socket!\n");
        return res;
    }
    return sock;
}

canonical_original_code:
int server_create_and_bind_socket(int port, int proto)
{
    int sock = 0;
    if (proto == SOCK_DGRAM) {
        sock = socket(2, proto, IPPROTO_UDP);
    } else {
        sock = socket(2, proto, 0);
    }
    if (sock < 0) {
        inform_user("ERROR: Failed to open socket!\n");
        return -1;
    }
    int reuse = 1;
    setsockopt(sock, 1, 2, (char *)&reuse, sizeof(reuse));
    struct sockaddr_in servAddr;
    memset(&servAddr, 0, sizeof(struct sockaddr_in));
    servAddr.sin_family = 2;
    servAddr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
    servAddr.sin_port = htons(port);
    int res = bind(sock, (struct sockaddr*)&servAddr, sizeof(servAddr));
    if (res < 0) {
        inform_user("ERROR: Failed to bind socket!\n");
        return res;
    }
    return sock;
}

variable_types: port int

variable_types: proto int

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }


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

result = ++gAdded;
gServers[gAdded] = a1;
return result;
}

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

result = ++gAdded;
gServers[gAdded] = a1;
return result;
}

original_code:
void
server_add(client_data_t* data)
{
    gServers[++gAdded] = data;
}

canonical_original_code:
void server_add(struct client_data_t *data)
{
    gServers[++gAdded] = data;
}

variable_types: struct client_data_t { int sock; int clientNum; int port; unsigned long thread; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; unsigned long numSinceStart; unsigned long numSinceMeasure; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } serverAddress; } *


decompiled_code:
void __fastcall __noreturn tp_thread(void *a1)
{
struct timeval v1; // [rsp+0h] [rbp-28h] BYREF
struct timeval tv; // [rsp+10h] [rbp-18h] BYREF

gettimeofday(&tv, 0LL);
while ( 1 )
{
gettimeofday(&v1, 0LL);
usleep(0xF4240u);
report_statistics(&tv, &v1, &gServers, *(unsigned int *)(gAppArgs + 272), *(unsigned int *)(gAppArgs + 284));
}
}

canonical_decompiled_code:
void __fastcall __noreturn func1(void *a1)
{
struct timeval v1; // [rsp+0h] [rbp-28h] BYREF
struct timeval tv; // [rsp+10h] [rbp-18h] BYREF

gettimeofday(&tv, 0LL);
while ( 1 )
{
gettimeofday(&v1, 0LL);
usleep(0xF4240u);
report_statistics(&tv, &v1, &gServers, *(unsigned int *)(gAppArgs + 272), *(unsigned int *)(gAppArgs + 284));
}
}

original_code:
static void*
tp_thread(void* data)
{
    struct timeval startTime;
    struct timeval measurementStartTime;
    gettimeofday(&startTime, ((void *)0));
    while (1) {
        gettimeofday(&measurementStartTime, ((void *)0));
        usleep(1000000);
        report_statistics(&startTime, &measurementStartTime, gServers,
                          gAppArgs->numConnections, gAppArgs->processId);
    }
    pthread_exit(((void *)0));
}

canonical_original_code:
static void *tp_thread(void *data)
{
    struct timeval startTime;
    struct timeval measurementStartTime;
    gettimeofday(&startTime, ((void *)0));
    while (1) {
        gettimeofday(&measurementStartTime, ((void *)0));
        usleep(1000000);
        report_statistics(&startTime, &measurementStartTime, gServers,
                          gAppArgs->numConnections, gAppArgs->processId);
    }
    pthread_exit(((void *)0));
}

variable_types:
data
void *

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


decompiled_code:
int debug_print(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, format);
fwrite("*** ", 1uLL, 4uLL, stderr);
vfprintf(stderr, format, arg);
return fflush(stderr);
}

canonical_decompiled_code:
int func7(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-E0h] BYREF

va_start(arg, format);
fwrite("*** ", 1uLL, 4uLL, stderr);
vfprintf(stderr, format, arg);
return fflush(stderr);
}

original_code:
void
debug_print(const char *format, ...)
{
    va_list ap;
    __builtin_va_start(ap,format);
    fprintf(stderr, "*** ");
    vfprintf(stderr, format, ap);
    __builtin_va_end(ap);
    fflush(stderr);
}

canonical_original_code:
void debug_print(char *format, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,format);
    fprintf(stderr, "*** ");
    vfprintf(stderr, format, ap);
    __builtin_va_end(ap);
    fflush(stderr);
}

variable_types:
format
char *


decompiled_code:
int inform_user(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stdout, format, arg);
return fflush(stdout);
}

canonical_decompiled_code:
int func8(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stdout, format, arg);
return fflush(stdout);
}

original_code:
void
inform_user(const char *format, ...)
{
    va_list ap;
    __builtin_va_start(ap,format);
    vfprintf(stdout, format, ap);
    fflush(stdout);
    __builtin_va_end(ap);
}

canonical_original_code:
void inform_user(char *format, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,format);
    vfprintf(stdout, format, ap);
    fflush(stdout);
    __builtin_va_end(ap);
}


decompiled_code:
__int64 start_aggregating()
{
int v0; // eax
int v1; // ebx
unsigned int v2; // ebp
int v3; // er12
__int64 result; // rax
int v5; // eax
int v6; // eax
__int64 v7; // rdx
__int64 v8; // rax
__int64 v9; // rdx
double v10; // xmm1_8
char buf[10]; // [rsp+2h] [rbp-56h] BYREF
socklen_t addr_len; // [rsp+Ch] [rbp-4Ch] BYREF
struct sockaddr v13; // [rsp+10h] [rbp-48h] BYREF
struct pollfd fds; // [rsp+28h] [rbp-30h] BYREF
struct sockaddr addr; // [rsp+30h] [rbp-28h] BYREF

v0 = socket(2, 2, 0);
if ( v0 < 0 )
{
inform_user("ERROR: Failed to open socket!\n");
result = 1LL;
}
else
{
v1 = v0;
*(_QWORD *)&addr.sa_data[6] = 0LL;
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)_bswap_32(0);
v2 = gPort;
*(_WORD *)addr.sa_data = _bswap_16(gPort);
if ( bind(v1, &addr, 0x10u) < 0 )
{
inform_user("ERROR: Failed to bind socket!\n");
result = 1LL;
}
else
{
fds.fd = v1;
fds.events = 1;
addr_len = 16;
inform_user("* Aggregator started on port %d. Clients: %d\n", v2, (unsigned int)gNumClients);
v3 = 0;
do
{
while ( 1 )
{
while ( 1 )
{
do
{
while ( 1 )
{
do
{
while ( poll(&fds, 1uLL, -1) <= 0 )
;
}
while ( fds.revents != 1 );
v5 = recvfrom(fds.fd, buf, 0xAuLL, 0, &v13, &addr_len);
if ( v5 != 1 )
break;
++gNumRegistered;
}
}
while ( v5 != 5 );
v6 = (unsigned __int8)buf[4]
+ ((unsigned __int8)buf[1] << 24)
+ ((unsigned __int8)buf[2] << 16)
+ ((unsigned __int8)buf[3] << 8);
v7 = (unsigned __int8)buf[0];
gClientReported[(unsigned __int8)buf[0]] = 1;
gClientTp[v7] = v6;
if ( gNumClients <= 0 )
break;
v8 = 0LL;
LODWORD(v9) = 0;
while ( gClientReported[v8] )
{
v9 = (unsigned int)(gClientTp[v8++] + v9);
if ( v8 == gNumClients )
goto LABEL_15;
}
}
v9 = 0LL;
LABEL_15:
v10 = (double)(int)v9 + *(double *)&gAvgTput;
*(double *)&gAvgTput = v10;
if ( v10 != 0.0 )
break;
memset(gClientTp, 0, 0x3F8uLL);
gClientTp[254] = 0;
memset(gClientReported, 0, 0x3F8uLL);
gClientReported[254] = 0;
}
++v3;
inform_user(
"* #%-4d Aggregated throughput: %u kbps (%0.2f Mbps) Avg: %0.2f Mbps\n",
(unsigned int)v3,
v9,
(double)(int)v9 * 0.0009765625,
v10 / (double)v3 * 0.0009765625);
memset(gClientTp, 0, 0x3F8uLL);
gClientTp[254] = 0;
memset(gClientReported, 0, 0x3F8uLL);
gClientReported[254] = 0;
}
while ( gNumMeasurements != v3 );
system("pkill conntest");
inform_user("* Measurement limit (%d) reached. Exiting...\n", (unsigned int)gNumMeasurements);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 func12()
{
int v0; // eax
int v1; // ebx
unsigned int v2; // ebp
int v3; // er12
__int64 result; // rax
int v5; // eax
int v6; // eax
__int64 v7; // rdx
__int64 v8; // rax
__int64 v9; // rdx
double v10; // xmm1_8
char buf[10]; // [rsp+2h] [rbp-56h] BYREF
socklen_t addr_len; // [rsp+Ch] [rbp-4Ch] BYREF
struct sockaddr v13; // [rsp+10h] [rbp-48h] BYREF
struct pollfd fds; // [rsp+28h] [rbp-30h] BYREF
struct sockaddr addr; // [rsp+30h] [rbp-28h] BYREF

v0 = socket(2, 2, 0);
if ( v0 < 0 )
{
func8("ERROR: Failed to open socket!\n");
result = 1LL;
}
else
{
v1 = v0;
*(_QWORD *)&addr.sa_data[6] = 0LL;
addr.sa_family = 2;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)func10(0);
v2 = gPort;
*(_WORD *)addr.sa_data = func11(gPort);
if ( bind(v1, &addr, 0x10u) < 0 )
{
func8("ERROR: Failed to bind socket!\n");
result = 1LL;
}
else
{
fds.fd = v1;
fds.events = 1;
addr_len = 16;
func8("* Aggregator started on port %d. Clients: %d\n", v2, (unsigned int)gNumClients);
v3 = 0;
do
{
while ( 1 )
{
while ( 1 )
{
do
{
while ( 1 )
{
do
{
while ( poll(&fds, 1uLL, -1) <= 0 )
;
}
while ( fds.revents != 1 );
v5 = recvfrom(fds.fd, buf, 0xAuLL, 0, &v13, &addr_len);
if ( v5 != 1 )
break;
++gNumRegistered;
}
}
while ( v5 != 5 );
v6 = (unsigned __int8)buf[4]
+ ((unsigned __int8)buf[1] << 24)
+ ((unsigned __int8)buf[2] << 16)
+ ((unsigned __int8)buf[3] << 8);
v7 = (unsigned __int8)buf[0];
gClientReported[(unsigned __int8)buf[0]] = 1;
gClientTp[v7] = v6;
if ( gNumClients <= 0 )
break;
v8 = 0LL;
LODWORD(v9) = 0;
while ( gClientReported[v8] )
{
v9 = (unsigned int)(gClientTp[v8++] + v9);
if ( v8 == gNumClients )
goto LABEL_15;
}
}
v9 = 0LL;
LABEL_15:
v10 = (double)(int)v9 + *(double *)&gAvgTput;
*(double *)&gAvgTput = v10;
if ( v10 != 0.0 )
break;
memset(gClientTp, 0, 0x3F8uLL);
gClientTp[254] = 0;
memset(gClientReported, 0, 0x3F8uLL);
gClientReported[254] = 0;
}
++v3;
func8(
"* #%-4d Aggregated throughput: %u kbps (%0.2f Mbps) Avg: %0.2f Mbps\n",
(unsigned int)v3,
v9,
(double)(int)v9 * 0.0009765625,
v10 / (double)v3 * 0.0009765625);
memset(gClientTp, 0, 0x3F8uLL);
gClientTp[254] = 0;
memset(gClientReported, 0, 0x3F8uLL);
gClientReported[254] = 0;
}
while ( gNumMeasurements != v3 );
system("pkill conntest");
func8("* Measurement limit (%d) reached. Exiting...\n", (unsigned int)gNumMeasurements);
result = 0LL;
}
}
return result;
}

original_code:
static int
start_aggregating(void)
{
    int res = 0;
    int sock = socket(2, SOCK_DGRAM, 0);
    if (sock < 0) {
        inform_user("ERROR: Failed to open socket!\n");
        return 1;
    }
    struct sockaddr_in sinAddr;
    memset(&sinAddr, 0, sizeof(struct sockaddr_in));
    sinAddr.sin_family = 2;
    sinAddr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
    sinAddr.sin_port = htons(gPort);
    res = bind (sock, (struct sockaddr*) &sinAddr, sizeof(struct sockaddr_in));
    if (res < 0) {
        inform_user("ERROR: Failed to bind socket!\n");
        return 1;
    }
    struct pollfd fds[1];
    fds[0].fd = sock;
    fds[0].events = 0x001;
    struct sockaddr_in cliAddr;
    socklen_t len = sizeof(struct sockaddr_in);
    int n = 0;
    uint8_t msg[10];
    int reportNum = 0;
    inform_user("* Aggregator started on port %d. Clients: %d\n",
                gPort, gNumClients);
    while (1) {
        res = poll(fds, 1, -1);
        if (res > 0 && (fds[0].revents == 0x001)) {
            n = recvfrom(fds[0].fd, msg, 10, 0, (struct sockaddr*) &cliAddr, &len);
            if (n == 1) {
                gNumRegistered++;
                if (gNumRegistered == gNumClients) {
                    gDoReport = 1;
                }
                continue;
            }
            if (n == 5) {
                uint32_t tp = (msg[1] << 24) + (msg[2] << 16) + (msg[3] << 8) + msg[4];
                uint32_t totTp = 0;
                int doPrint = 1;
                gClientReported[msg[0]] = 1;
                gClientTp[msg[0]] = tp;
                for (int i = 0; i < gNumClients; i++) {
                    if (!gClientReported[i]) {
                        doPrint = 0;
                        break;
                    }
                    totTp += gClientTp[i];
                }
                if (doPrint) {
                    gAvgTput += totTp;
                    if (gAvgTput == 0) {
                        memset(gClientTp, 0, sizeof(gClientTp));
                        memset(gClientReported, 0, sizeof(gClientReported));
                        continue;
                    }
                    reportNum++;
                    inform_user("* #%-4d Aggregated throughput: %u kbps (%0.2f Mbps)"
                                " Avg: %0.2f Mbps\n",
                                reportNum, totTp,
                                (double) totTp / 1024,
                                (gAvgTput / (reportNum) / 1024));
                    memset(gClientTp, 0, sizeof(gClientTp));
                    memset(gClientReported, 0, sizeof(gClientReported));
                    if (reportNum == gNumMeasurements) {
                        int resX = system("pkill conntest");
                        inform_user("* Measurement limit (%d) reached. Exiting...\n", gNumMeasurements);
                        return 0;
                    }
                }
            }
        }
    }
    return 0;
}

canonical_original_code:
static int start_aggregating()
{
    int res = 0;
    int sock = socket(2, SOCK_DGRAM, 0);
    if (sock < 0) {
        inform_user("ERROR: Failed to open socket!\n");
        return 1;
    }
    struct sockaddr_in sinAddr;
    memset(&sinAddr, 0, sizeof(struct sockaddr_in));
    sinAddr.sin_family = 2;
    sinAddr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
    sinAddr.sin_port = htons(gPort);
    res = bind (sock, (struct sockaddr *) &sinAddr, sizeof(struct sockaddr_in));
    if (res < 0) {
        inform_user("ERROR: Failed to bind socket!\n");
        return 1;
    }
    struct pollfd fds[1];
    fds[0].fd = sock;
    fds[0].events = 0x001;
    struct sockaddr_in cliAddr;
    unsigned int len = sizeof(struct sockaddr_in);
    int n = 0;
    unsigned char msg[10];
    int reportNum = 0;
    inform_user("* Aggregator started on port %d. Clients: %d\n",
                gPort, gNumClients);
    while (1) {
        res = poll(fds, 1, -1);
        if (res > 0 && (fds[0].revents == 0x001)) {
            n = recvfrom(fds[0].fd, msg, 10, 0, (struct sockaddr *) &cliAddr, &len);
            if (n == 1) {
                gNumRegistered++;
                if (gNumRegistered == gNumClients) {
                    gDoReport = 1;
                }
                continue;
            }
            if (n == 5) {
                unsigned int tp = (msg[1] << 24) + (msg[2] << 16) + (msg[3] << 8) + msg[4];
                unsigned int totTp = 0;
                int doPrint = 1;
                gClientReported[msg[0]] = 1;
                gClientTp[msg[0]] = tp;
                for (int i = 0; i < gNumClients; i++) {
                    if (!gClientReported[i]) {
                        doPrint = 0;
                        break;
                    }
                    totTp += gClientTp[i];
                }
                if (doPrint) {
                    gAvgTput += totTp;
                    if (gAvgTput == 0) {
                        memset(gClientTp, 0, sizeof(gClientTp));
                        memset(gClientReported, 0, sizeof(gClientReported));
                        continue;
                    }
                    reportNum++;
                    inform_user("* #%-4d Aggregated throughput: %u kbps (%0.2f Mbps)"
                                " Avg: %0.2f Mbps\n",
                                reportNum, totTp,
                                (double) totTp / 1024,
                                (gAvgTput / (reportNum) / 1024));
                    memset(gClientTp, 0, sizeof(gClientTp));
                    memset(gClientReported, 0, sizeof(gClientReported));
                    if (reportNum == gNumMeasurements) {
                        int resX = system("pkill conntest");
                        inform_user("* Measurement limit (%d) reached. Exiting...\n", gNumMeasurements);
                        return 0;
                    }
                }
            }
        }
    }
    return 0;
}

variable_types: struct pollfd { int fd; short events; short revents; } [1]

variable_types:
len
unsigned int

variable_types: n int

variable_types:
msg
unsigned char [10]

variable_types: reportNum int

variable_types:
tp
unsigned int

variable_types:
totTp
unsigned int

variable_types: doPrint int

variable_types: resX int