repo: ptorpman/threet

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

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

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

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

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; // er12
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; // er12
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

variable_types: res int

return_type: int

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:
void __fastcall __noreturn tcp_recv_thread(void *a1)
{
pthread_mutex_t *v1; // r15
int v3; // edi
int v4; // eax
int v5; // ebx
socklen_t addr_len; // [rsp+Ch] [rbp-1004Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-10048h] BYREF
char buf[65592]; // [rsp+20h] [rbp-10038h] BYREF

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

canonical_decompiled_code:
void __fastcall __noreturn func2(void *a1)
{
pthread_mutex_t *v1; // r15
int v3; // edi
int v4; // eax
int v5; // ebx
socklen_t addr_len; // [rsp+Ch] [rbp-1004Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-10048h] BYREF
char buf[65592]; // [rsp+20h] [rbp-10038h] BYREF

v1 = (pthread_mutex_t *)((char *)a1 + 24);
while ( 1 )
{
v3 = *(_DWORD *)a1;
addr_len = 0;
v4 = recvfrom(v3, buf, 0xFFFFuLL, 0, &addr, &addr_len);
if ( v4 > 0 )
{
v5 = v4;
pthread_mutex_lock(v1);
*((_QWORD *)a1 + 9) += v5;
*((_QWORD *)a1 + 8) += v5;
pthread_mutex_unlock(v1);
}
else if ( !v4 )
{
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; // er12
int v2; // edi
struct pollfd fds; // [rsp+8h] [rbp-10h] BYREF

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

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

v0 = 0;
fds.fd = serverSocket;
fds.events = 1;
if ( poll(&fds, 1uLL, 10) > 0 && fds.revents == 1 )
{
v2 = func1(fds.fd);
if ( v2 < 0 )
{
v0 = -1;
inform_user("ERROR: Failed to accept connection!\n");
}
else
{
v0 = func3(v2);
if ( v0 )
{
v0 = -1;
inform_user("ERROR: Failed to start client thread!\n");
}
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 v3[9]; // [rsp+Fh] [rbp-9h] BYREF

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

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

v3[0] = a1;
return func1(v3, 1LL, 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, unsigned int a2, const char *a3)
{
char v4; // [rsp+Bh] [rbp-Dh] BYREF
unsigned __int32 v5; // [rsp+Ch] [rbp-Ch]

v4 = a1;
v5 = _byteswap_ulong(a2);
return send_msg_to_aggregator(&v4, 5LL, a3);
}

canonical_decompiled_code:
__int64 __fastcall func3(char a1, unsigned int a2, const char *a3)
{
char v4; // [rsp+Bh] [rbp-Dh] BYREF
unsigned __int32 v5; // [rsp+Ch] [rbp-Ch]

v4 = a1;
v5 = _byteswap_ulong(a2);
return func1(&v4, 5LL, 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(const void *a1, __int64 a2, const char *a3)
{
struct hostent *v4; // rax
int v5; // eax
int v6; // ebp
__int16 v7; // ax
size_t v8; // rdx
int v9; // ecx
const struct sockaddr *v10; // r8
socklen_t v11; // er9
unsigned int v12; // er12
struct in_addr inp; // [rsp+0h] [rbp-30h] BYREF
int optval; // [rsp+4h] [rbp-2Ch] BYREF
__int128 v16; // [rsp+8h] [rbp-28h]

optval = 1;
v16 = 0LL;
v4 = gethostbyname(a3);
if ( v4 )
{
inp.s_addr = **(_DWORD **)v4->h_addr_list;
}
else if ( !inet_aton(a3, &inp) )
{
v12 = -1;
debug_print("ERROR: Unknown server address!\n");
return v12;
}
v5 = socket(2, 2, 0);
v6 = v5;
if ( v5 < 0 )
{
v12 = -1;
debug_print("ERROR: Failed to open socket!\n");
return v12;
}
setsockopt(v5, 1, 2, &optval, 4u);
LOWORD(v16) = 2;
DWORD1(v16) = inp;
v7 = get_aggregator_port();
WORD1(v16) = _bswap_16(v7);
v12 = sendto(v6, a1, v8, v9, v10, v11);
if ( (v12 & 0x80000000) == 0 )
return v12;
inform_user("ERROR: Failed to send to aggregator!\n");
return v12;
}

canonical_decompiled_code:
__int64 __fastcall func1(const void *a1, __int64 a2, const char *a3)
{
struct hostent *v4; // rax
int v5; // eax
int v6; // ebp
__int16 v7; // ax
size_t v8; // rdx
int v9; // ecx
const struct sockaddr *v10; // r8
socklen_t v11; // er9
unsigned int v12; // er12
struct in_addr inp; // [rsp+0h] [rbp-30h] BYREF
int optval; // [rsp+4h] [rbp-2Ch] BYREF
__int128 v16; // [rsp+8h] [rbp-28h]

optval = 1;
v16 = 0LL;
v4 = gethostbyname(a3);
if ( v4 )
{
inp.s_addr = **(_DWORD **)v4->h_addr_list;
}
else if ( !inet_aton(a3, &inp) )
{
v12 = -1;
debug_print("ERROR: Unknown server address!\n");
return v12;
}
v5 = socket(2, 2, 0);
v6 = v5;
if ( v5 < 0 )
{
v12 = -1;
debug_print("ERROR: Failed to open socket!\n");
return v12;
}
setsockopt(v5, 1, 2, &optval, 4u);
LOWORD(v16) = 2;
DWORD1(v16) = inp;
v7 = get_aggregator_port();
WORD1(v16) = func0(v7);
v12 = sendto(v6, a1, v8, v9, v10, v11);
if ( (v12 & 0x80000000) == 0 )
return v12;
inform_user("ERROR: Failed to send to aggregator!\n");
return v12;
}

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


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);
        }
    }
}

variable_types: res int

return_type: void


decompiled_code:
__int64 __fastcall server_start(__int64 a1)
{
int v1; // eax
__int64 v2; // rdx
__int64 v3; // rsi

memset(&gServers, 0, (size_t)&unk_7FFF8);
v1 = *(_DWORD *)a1;
gAppArgs = a1;
v2 = *(unsigned int *)(a1 + 272);
v3 = *(unsigned __int16 *)(a1 + 8);
if ( v1 )
{
if ( (int)udp_create_servers(&gServers, v3, v2) < 0 )
inform_user("ERROR: Failed to create UDP servers!\n");
return eternal_loop();
}
if ( (int)tcp_create_server(&gServers, v3, v2) >= 0 )
return eternal_loop();
return inform_user("ERROR: Failed to open TCP server socket!\n");
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
int v1; // eax
__int64 v2; // rdx
__int64 v3; // rsi

memset(&gServers, 0, (size_t)&unk_7FFF8);
v1 = *(_DWORD *)a1;
gAppArgs = a1;
v2 = *(unsigned int *)(a1 + 272);
v3 = *(unsigned __int16 *)(a1 + 8);
if ( v1 )
{
if ( (int)udp_create_servers(&gServers, v3, v2) < 0 )
inform_user("ERROR: Failed to create UDP servers!\n");
return func0();
}
if ( (int)tcp_create_server(&gServers, v3, v2) >= 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(unsigned __int16 a1, int a2)
{
int v2; // er12
socklen_t v3; // edx
int v4; // ebx
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr type[2]; // [rsp+10h] [rbp-28h] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func5(unsigned __int16 a1, int a2)
{
int v2; // er12
socklen_t v3; // edx
int v4; // ebx
int optval; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr type[2]; // [rsp+10h] [rbp-28h] BYREF

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

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 anonymous0; // [rsp+0h] [rbp+0h] BYREF
struct timeval tv; // [rsp+10h] [rbp+10h] BYREF

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

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

gettimeofday(&anonymous0, 0LL);
while ( 1 )
{
gettimeofday(&tv, 0LL);
usleep(0xF4240u);
report_statistics(&anonymous0, &tv, &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
int v2; // eax
unsigned int v3; // ebp
socklen_t v4; // edx
int v5; // eax
__int64 v6; // rcx
int v7; // eax
__int64 v8; // rdx
__int64 v9; // rax
__int64 v10; // rdx
double v11; // xmm1_8
bool v12; // zf
__int64 result; // rax
int i; // [rsp+Ch] [rbp-7Ch]
socklen_t addr_len; // [rsp+18h] [rbp-70h] BYREF
struct pollfd fds; // [rsp+1Ch] [rbp-6Ch] BYREF
char buf[10]; // [rsp+26h] [rbp-62h] BYREF
struct sockaddr v18; // [rsp+30h] [rbp-58h] BYREF
struct sockaddr addr; // [rsp+40h] [rbp-48h] BYREF

v0 = socket(2, 2, 0);
if ( v0 < 0 )
{
inform_user("ERROR: Failed to open socket!\n");
result = 1LL;
}
else
{
v1 = v0;
*(_QWORD *)&v18.sa_data[6] = 0LL;
v18.sa_family = 2;
v2 = _bswap_32_constprop_0(2LL, &v18, 16LL);
v3 = gPort;
*(_DWORD *)&v18.sa_data[2] = v2;
*(_WORD *)v18.sa_data = _bswap_16(gPort);
if ( bind(v1, &v18, v4) < 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", v3, (unsigned int)gNumClients);
for ( i = 1; ; ++i )
{
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, &addr, &addr_len);
if ( v5 != 1 )
break;
++gNumRegistered;
}
}
while ( v5 != 5 );
v6 = gNumClients;
v7 = (unsigned __int8)buf[4]
+ ((unsigned __int8)buf[1] << 24)
+ ((unsigned __int8)buf[2] << 16)
+ ((unsigned __int8)buf[3] << 8);
v8 = (unsigned __int8)buf[0];
gClientReported[(unsigned __int8)buf[0]] = 1;
gClientTp[v8] = v7;
if ( (int)v6 <= 0 )
break;
v9 = 0LL;
LODWORD(v10) = 0;
while ( gClientReported[v9] )
{
v10 = (unsigned int)(gClientTp[v9++] + v10);
if ( v6 == v9 )
goto LABEL_12;
}
}
v10 = 0LL;
LABEL_12:
v11 = *(double *)&gAvgTput + (double)(int)v10;
*(double *)&gAvgTput = v11;
if ( v11 != 0.0 )
break;
memset(gClientTp, 0, 0x3F8uLL);
gClientTp[254] = 0;
memset(gClientReported, 0, 0x3F8uLL);
gClientReported[254] = 0;
}
inform_user(
"* #%-4d Aggregated throughput: %u kbps (%0.2f Mbps) Avg: %0.2f Mbps\n",
(unsigned int)i,
v10,
(double)(int)v10 * 0.0009765625,
v11 / (double)i * 0.0009765625);
memset(gClientTp, 0, 0x3F8uLL);
gClientTp[254] = 0;
memset(gClientReported, 0, 0x3F8uLL);
v12 = i == gNumMeasurements;
gClientReported[254] = 0;
if ( v12 )
break;
}
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
int v2; // eax
unsigned int v3; // ebp
socklen_t v4; // edx
int v5; // eax
__int64 v6; // rcx
int v7; // eax
__int64 v8; // rdx
__int64 v9; // rax
__int64 v10; // rdx
double v11; // xmm1_8
bool v12; // zf
__int64 result; // rax
int i; // [rsp+Ch] [rbp-7Ch]
socklen_t addr_len; // [rsp+18h] [rbp-70h] BYREF
struct pollfd fds; // [rsp+1Ch] [rbp-6Ch] BYREF
char buf[10]; // [rsp+26h] [rbp-62h] BYREF
struct sockaddr v18; // [rsp+30h] [rbp-58h] BYREF
struct sockaddr addr; // [rsp+40h] [rbp-48h] BYREF

v0 = socket(2, 2, 0);
if ( v0 < 0 )
{
func8("ERROR: Failed to open socket!\n");
result = 1LL;
}
else
{
v1 = v0;
*(_QWORD *)&v18.sa_data[6] = 0LL;
v18.sa_family = 2;
v2 = func11(2LL, &v18, 16LL);
v3 = gPort;
*(_DWORD *)&v18.sa_data[2] = v2;
*(_WORD *)v18.sa_data = func10(gPort);
if ( bind(v1, &v18, v4) < 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", v3, (unsigned int)gNumClients);
for ( i = 1; ; ++i )
{
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, &addr, &addr_len);
if ( v5 != 1 )
break;
++gNumRegistered;
}
}
while ( v5 != 5 );
v6 = gNumClients;
v7 = (unsigned __int8)buf[4]
+ ((unsigned __int8)buf[1] << 24)
+ ((unsigned __int8)buf[2] << 16)
+ ((unsigned __int8)buf[3] << 8);
v8 = (unsigned __int8)buf[0];
gClientReported[(unsigned __int8)buf[0]] = 1;
gClientTp[v8] = v7;
if ( (int)v6 <= 0 )
break;
v9 = 0LL;
LODWORD(v10) = 0;
while ( gClientReported[v9] )
{
v10 = (unsigned int)(gClientTp[v9++] + v10);
if ( v6 == v9 )
goto LABEL_12;
}
}
v10 = 0LL;
LABEL_12:
v11 = *(double *)&gAvgTput + (double)(int)v10;
*(double *)&gAvgTput = v11;
if ( v11 != 0.0 )
break;
memset(gClientTp, 0, 0x3F8uLL);
gClientTp[254] = 0;
memset(gClientReported, 0, 0x3F8uLL);
gClientReported[254] = 0;
}
func8(
"* #%-4d Aggregated throughput: %u kbps (%0.2f Mbps) Avg: %0.2f Mbps\n",
(unsigned int)i,
v10,
(double)(int)v10 * 0.0009765625,
v11 / (double)i * 0.0009765625);
memset(gClientTp, 0, 0x3F8uLL);
gClientTp[254] = 0;
memset(gClientReported, 0, 0x3F8uLL);
v12 = i == gNumMeasurements;
gClientReported[254] = 0;
if ( v12 )
break;
}
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


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er13
const char **v5; // rbx
unsigned __int8 *v6; // rbp
int v7; // edx
const char *v8; // rdi
int v10; // eax
char v11; // [rsp+0h] [rbp-38h]

v3 = 3;
v5 = argv + 2;
memset(gClientTp, 0, 0x3F8uLL);
gClientTp[254] = 0;
memset(gClientReported, 0, 0x3F8uLL);
gClientReported[254] = 0;
if ( argc > 1 )
{
while ( 1 )
{
v6 = (unsigned __int8 *)*(v5 - 1);
v7 = *v6;
if ( v7 == 45 && v6[1] == 104 && !v6[2] || !strcmp(*(v5 - 1), "--help") )
goto LABEL_17;
if ( v7 != 45 )
break;
if ( v6[1] != 110 || v6[2] )
{
if ( v6[1] != 112 || v6[2] )
break;
v8 = *v5;
if ( !*v5 )
goto LABEL_28;
if ( (unsigned int)__isoc99_sscanf() != 1 )
{
inform_user("ERROR: Incorrect port number '%s'\n", v11);
return 1;
}
gPort = 0;
v10 = v3;
}
else
{
v8 = *v5;
if ( !*v5 )
goto LABEL_28;
if ( (unsigned int)__isoc99_sscanf() != 1 )
{
inform_user("ERROR: Incorrect number of clients/servers '%s'\n", v11);
return 1;
}
gNumClients = 0;
v10 = v3;
}
LABEL_23:
v3 += 2;
v5 += 2;
if ( v10 >= argc )
{
if ( gPort != -1 )
return start_aggregating(v8, "%d");
inform_user("Bad port number! (%d)\n", v11);
print_usage("Bad port number! (%d)\n", 0xFFFFFFFFLL);
return 1;
}
}
if ( strcmp(*(v5 - 1), "-m") )
{
inform_user("Bad parameter! (%s)\n", v11);
LABEL_28:
((void (*)(void))print_usage)();
return 1;
}
v8 = *v5;
if ( !*v5 )
goto LABEL_28;
if ( (unsigned int)__isoc99_sscanf() != 1 )
{
inform_user("ERROR: Incorrect measurement limit '%s'\n", v11);
return 1;
}
gNumMeasurements = 0;
v10 = v3;
goto LABEL_23;
}
LABEL_17:
((void (*)(void))print_usage)();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er13
const char **v5; // rbx
unsigned __int8 *v6; // rbp
int v7; // edx
const char *v8; // rdi
int v10; // eax
char v11; // [rsp+0h] [rbp-38h]

v3 = 3;
v5 = argv + 2;
memset(gClientTp, 0, 0x3F8uLL);
gClientTp[254] = 0;
memset(gClientReported, 0, 0x3F8uLL);
gClientReported[254] = 0;
if ( argc > 1 )
{
while ( 1 )
{
v6 = (unsigned __int8 *)*(v5 - 1);
v7 = *v6;
if ( v7 == 45 && v6[1] == 104 && !v6[2] || !strcmp(*(v5 - 1), "--help") )
goto LABEL_17;
if ( v7 != 45 )
break;
if ( v6[1] != 110 || v6[2] )
{
if ( v6[1] != 112 || v6[2] )
break;
v8 = *v5;
if ( !*v5 )
goto LABEL_28;
if ( (unsigned int)__isoc99_sscanf() != 1 )
{
func8("ERROR: Incorrect port number '%s'\n", v11);
return 1;
}
gPort = 0;
v10 = v3;
}
else
{
v8 = *v5;
if ( !*v5 )
goto LABEL_28;
if ( (unsigned int)__isoc99_sscanf() != 1 )
{
func8("ERROR: Incorrect number of clients/servers '%s'\n", v11);
return 1;
}
gNumClients = 0;
v10 = v3;
}
LABEL_23:
v3 += 2;
v5 += 2;
if ( v10 >= argc )
{
if ( gPort != -1 )
return func12(v8, "%d");
func8("Bad port number! (%d)\n", v11);
func9("Bad port number! (%d)\n", 0xFFFFFFFFLL);
return 1;
}
}
if ( strcmp(*(v5 - 1), "-m") )
{
func8("Bad parameter! (%s)\n", v11);
LABEL_28:
((void (*)(void))print_usage)();
return 1;
}
v8 = *v5;
if ( !*v5 )
goto LABEL_28;
if ( (unsigned int)__isoc99_sscanf() != 1 )
{
func8("ERROR: Incorrect measurement limit '%s'\n", v11);
return 1;
}
gNumMeasurements = 0;
v10 = v3;
goto LABEL_23;
}
LABEL_17:
((void (*)(void))print_usage)();
return 0;
}

original_code:
int
main(int argc, char** argv)
{
    memset(gClientTp, 0, sizeof(gClientTp));
    memset(gClientReported, 0, sizeof(gClientReported));
    int i = 1;
    int val = 0;
    if (argc < 2) {
        print_usage();
        return 0;
    }
    while (i < argc) {
        if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
            print_usage();
            return 0;
        }
        else if (!strcmp(argv[i], "-n")) {
            if (argv[i + 1] == ((void *)0)) {
                print_usage();
                return 1;
            }
            if (sscanf(argv[i+1], "%d", &val) != 1) {
                inform_user("ERROR: Incorrect number of clients/servers '%s'\n",
                            argv[i+1]);
                return 1;
            }
            gNumClients = val;
            i += 2;
            continue;
        }
        else if (!strcmp(argv[i], "-p")) {
            if (argv[i + 1] == ((void *)0)) {
                print_usage();
                return 1;
            }
            if (sscanf(argv[i+1], "%d", &val) != 1) {
                inform_user("ERROR: Incorrect port number '%s'\n",
                            argv[i+1]);
                return 1;
            }
            gPort = val;
            i += 2;
            continue;
        }
        else if (!strcmp(argv[i], "-m")) {
            if (argv[i + 1] == ((void *)0)) {
                print_usage();
                return 1;
            }
            if (sscanf(argv[i+1], "%d", &val) != 1) {
                inform_user("ERROR: Incorrect measurement limit '%s'\n",
                            argv[i+1]);
                return 1;
            }
            gNumMeasurements = val;
            i += 2;
            continue;
        }
        else {
            inform_user("Bad parameter! (%s)\n", argv[i]);
            print_usage();
            return 1;
        }
    }
    if (gPort == -1) {
        inform_user("Bad port number! (%d)\n", gPort);
        print_usage();
        return 1;
    }
    return start_aggregating();
}

canonical_original_code:
int main(int argc, char **argv)
{
    memset(gClientTp, 0, sizeof(gClientTp));
    memset(gClientReported, 0, sizeof(gClientReported));
    int i = 1;
    int val = 0;
    if (argc < 2) {
        print_usage();
        return 0;
    }
    while (i < argc) {
        if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
            print_usage();
            return 0;
        }
        else if (!strcmp(argv[i], "-n")) {
            if (argv[i + 1] == ((void *)0)) {
                print_usage();
                return 1;
            }
            if (sscanf(argv[i+1], "%d", &val) != 1) {
                inform_user("ERROR: Incorrect number of clients/servers '%s'\n",
                            argv[i+1]);
                return 1;
            }
            gNumClients = val;
            i += 2;
            continue;
        }
        else if (!strcmp(argv[i], "-p")) {
            if (argv[i + 1] == ((void *)0)) {
                print_usage();
                return 1;
            }
            if (sscanf(argv[i+1], "%d", &val) != 1) {
                inform_user("ERROR: Incorrect port number '%s'\n",
                            argv[i+1]);
                return 1;
            }
            gPort = val;
            i += 2;
            continue;
        }
        else if (!strcmp(argv[i], "-m")) {
            if (argv[i + 1] == ((void *)0)) {
                print_usage();
                return 1;
            }
            if (sscanf(argv[i+1], "%d", &val) != 1) {
                inform_user("ERROR: Incorrect measurement limit '%s'\n",
                            argv[i+1]);
                return 1;
            }
            gNumMeasurements = val;
            i += 2;
            continue;
        }
        else {
            inform_user("Bad parameter! (%s)\n", argv[i]);
            print_usage();
            return 1;
        }
    }
    if (gPort == -1) {
        inform_user("Bad port number! (%d)\n", gPort);
        print_usage();
        return 1;
    }
    return start_aggregating();
}

variable_types: argc int

variable_types:
argv
char **

variable_types: i int

variable_types: val int


decompiled_code:
int __fastcall print_usage(const char *a1)
{
return inform_user("Usage: aggregator [options]\n"
" Options:\n"
" -n <num> Number of clients/servers to aggregate\n"
" -p <port> Port number to listen to\n"
" -m <num> Number of measurements to receive (Default: 60)\n"
" -h Display this text\n"
"\n"
"For bug reporting and suggestions, mail peter@torpman.se\n");
}

canonical_decompiled_code:
int __fastcall func9(const char *a1)
{
return func8("Usage: aggregator [options]\n"
" Options:\n"
" -n <num> Number of clients/servers to aggregate\n"
" -p <port> Port number to listen to\n"
" -m <num> Number of measurements to receive (Default: 60)\n"
" -h Display this text\n"
"\n"
"For bug reporting and suggestions, mail peter@torpman.se\n");
}

original_code:
static void
print_usage(void)
{
    inform_user(
        "Usage: aggregator [options]\n"
        "  Options:\n"
        "  -n <num>    Number of clients/servers to aggregate\n"
        "  -p <port>   Port number to listen to\n"
        "  -m <num>    Number of measurements to receive (Default: 60)\n"
        "  -h          Display this text\n\n"
        "For bug reporting and suggestions, mail peter@torpman.se\n");
}

canonical_original_code:
static void print_usage()
{
    inform_user(
        "Usage: aggregator [options]\n"
        "  Options:\n"
        "  -n <num>    Number of clients/servers to aggregate\n"
        "  -p <port>   Port number to listen to\n"
        "  -m <num>    Number of measurements to receive (Default: 60)\n"
        "  -h          Display this text\n\n"
        "For bug reporting and suggestions, mail peter@torpman.se\n");
}


decompiled_code:
__int64 __fastcall get_size(char *s)
{
__int64 result; // rax
unsigned int v2; // eax
int v3; // er8

if ( strchr(s, 107) )
{
if ( (unsigned int)__isoc99_sscanf() == 1 )
return 0LL;
LABEL_7:
fprintf(stderr, "ERROR: Incorrect value '%s'\n", s);
return 0xFFFFFFFFLL;
}
if ( strchr(s, 77) )
{
if ( (unsigned int)__isoc99_sscanf() == 1 )
return 0LL;
goto LABEL_7;
}
if ( strchr(s, 71) )
{
v2 = __isoc99_sscanf();
if ( v2 == 1 )
{
result = 0LL;
}
else
{
fprintf(stderr, "ERROR: Incorrect value '%s' (%d)\n", s, v2);
result = 0xFFFFFFFFLL;
}
}
else
{
v3 = __isoc99_sscanf();
result = 0LL;
if ( v3 != 1 )
goto LABEL_7;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(char *s)
{
__int64 result; // rax
unsigned int v2; // eax
int v3; // er8

if ( strchr(s, 107) )
{
if ( (unsigned int)__isoc99_sscanf() == 1 )
return 0LL;
LABEL_7:
fprintf(stderr, "ERROR: Incorrect value '%s'\n", s);
return 0xFFFFFFFFLL;
}
if ( strchr(s, 77) )
{
if ( (unsigned int)__isoc99_sscanf() == 1 )
return 0LL;
goto LABEL_7;
}
if ( strchr(s, 71) )
{
v2 = __isoc99_sscanf();
if ( v2 == 1 )
{
result = 0LL;
}
else
{
fprintf(stderr, "ERROR: Incorrect value '%s' (%d)\n", s, v2);
result = 0xFFFFFFFFLL;
}
}
else
{
v3 = __isoc99_sscanf();
result = 0LL;
if ( v3 != 1 )
goto LABEL_7;
}
return result;
}

original_code:
static uint32_t
get_size(char* str)
{
  uint32_t val = 0;
  if (strchr(str, 'k')) {
    if (sscanf(str, "%u%*c", &val) != 1) {
      fprintf(stderr, "ERROR: Incorrect value '%s'\n", str);
      return -1;
    }
    return (val * 1024);
  }
  else if (strchr(str, 'M')) {
    if (sscanf(str, "%u%*c", &val) != 1) {
      fprintf(stderr, "ERROR: Incorrect value '%s'\n", str);
      return -1;
    }
    return (val * 1024 * 1024);
  }
  else if (strchr(str, 'G')) {
    int res = sscanf(str, "%uG", &val);
    if (res != 1) {
      fprintf(stderr, "ERROR: Incorrect value '%s' (%d)\n", str, res);
      return -1;
    }
    return (val * 1024 * 1024 * 1024);
  }
  else {
    if (sscanf(str, "%u", &val) != 1) {
      fprintf(stderr, "ERROR: Incorrect value '%s'\n", str);
      return -1;
    }
    return val;
  }
}

canonical_original_code:
static unsigned int get_size(char *str)
{
  unsigned int val = 0;
  if (strchr(str, 'k')) {
    if (sscanf(str, "%u%*c", &val) != 1) {
      fprintf(stderr, "ERROR: Incorrect value '%s'\n", str);
      return -1;
    }
    return (val * 1024);
  }
  else if (strchr(str, 'M')) {
    if (sscanf(str, "%u%*c", &val) != 1) {
      fprintf(stderr, "ERROR: Incorrect value '%s'\n", str);
      return -1;
    }
    return (val * 1024 * 1024);
  }
  else if (strchr(str, 'G')) {
    int res = sscanf(str, "%uG", &val);
    if (res != 1) {
      fprintf(stderr, "ERROR: Incorrect value '%s' (%d)\n", str, res);
      return -1;
    }
    return (val * 1024 * 1024 * 1024);
  }
  else {
    if (sscanf(str, "%u", &val) != 1) {
      fprintf(stderr, "ERROR: Incorrect value '%s'\n", str);
      return -1;
    }
    return val;
  }
}

variable_types:
str
char *

variable_types:
val
unsigned int

variable_types: res int

return_type: unsigned int


decompiled_code:
// local variable allocation has failed, the output may be wrong!
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er13
const char **v4; // r14
int v5; // er15
const char *v6; // rbp
__int64 v7; // r10
unsigned int v8; // eax
bool v9; // cf
bool v10; // zf
__int64 v11; // rcx
bool v12; // cf
bool v13; // zf
__int64 v14; // rcx
const char **v15; // rbp
struct _IO_FILE *v16; // rdi
int result; // eax
const char **v18; // rbp
int v19; // eax
const char **v20; // rbp
int v21; // eax
__int64 v22; // [rsp-10h] [rbp-68h]
unsigned int v23; // [rsp+1Ch] [rbp-3Ch]

v23 = 0;
*(__m128i *)gTestFile = _mm_load_si128((const __m128i *)&xmmword_402610);
if ( argc <= 1 )
{
LABEL_48:
print_usage(*(_QWORD *)&argc, argv, envp);
return 0;
}
v3 = argc;
v4 = argv;
v5 = 1;
do
{
v6 = v4[v5];
v7 = v5;
v8 = *(unsigned __int8 *)v6;
v9 = v8 < 0x2D;
v10 = v8 == 45;
if ( v8 == 45 )
{
v9 = v6[1] < 0x68u;
v10 = v6[1] == 104;
if ( v6[1] == 104 )
{
v9 = 0;
v10 = v6[2] == 0;
if ( !v6[2] )
goto LABEL_48;
}
}
v11 = 7LL;
argv = (const char **)v4[v5];
*(_QWORD *)&argc = "--help";
do
{
if ( !v11 )
break;
v9 = *(_BYTE *)argv < **(_BYTE **)&argc;
v10 = *(_BYTE *)argv == **(_BYTE **)&argc;
argv = (const char **)((char *)argv + 1);
++*(_QWORD *)&argc;
--v11;
}
while ( v10 );
LOBYTE(envp) = (!v9 && !v10) - v9;
if ( !(_BYTE)envp )
goto LABEL_48;
v12 = v8 < 0x2D;
v13 = v8 == 45;
if ( v8 == 45 )
{
v12 = v6[1] < 0x76u;
v13 = v6[1] == 118;
if ( v6[1] == 118 )
{
v12 = 0;
v13 = v6[2] == 0;
if ( !v6[2] )
goto LABEL_49;
}
}
v14 = 10LL;
argv = (const char **)v4[v5];
*(_QWORD *)&argc = "--version";
do
{
if ( !v14 )
break;
v12 = *(_BYTE *)argv < **(_BYTE **)&argc;
v13 = *(_BYTE *)argv == **(_BYTE **)&argc;
argv = (const char **)((char *)argv + 1);
++*(_QWORD *)&argc;
--v14;
}
while ( v13 );
LOBYTE(envp) = (!v12 && !v13) - v12;
if ( !(_BYTE)envp )
{
LABEL_49:
print_version(*(_QWORD *)&argc, argv);
return 0;
}
if ( v8 != 45 )
{
LABEL_18:
*(_QWORD *)&argc = v4[v5];
if ( strcmp(*(const char **)&argc, "-f") )
{
argv = (const char **)"Bad parameter! (%s)\n";
*(_QWORD *)&argc = stderr;
fprintf(stderr, "Bad parameter! (%s)\n", v6);
LABEL_51:
print_usage(*(_QWORD *)&argc, argv, envp);
return 1;
}
argv = (const char **)v4[v5 + 1];
if ( !argv )
goto LABEL_51;
strcpy(gTestFile, (const char *)argv);
*(_QWORD *)&argc = gTestFile;
argv = 0LL;
if ( !access(gTestFile, 0) )
{
fprintf(stderr, "ERROR: File exists (%s)\n", gTestFile);
return 1;
}
v5 += 2;
continue;
}
if ( v6[1] == 114 && !v6[2] )
{
++v5;
gTestType = 0;
continue;
}
if ( v6[1] == 119 && !v6[2] )
{
++v5;
gTestType = 1;
continue;
}
if ( v6[1] != 115 || v6[2] )
{
if ( v6[1] != 98 || v6[2] )
{
if ( v6[1] != 116 || v6[2] )
goto LABEL_18;
v15 = &v4[v7 + 1];
*(_QWORD *)&argc = *v15;
if ( !*v15 )
goto LABEL_51;
argv = (const char **)"%u";
if ( (unsigned int)__isoc99_sscanf() != 1 )
{
fprintf(stderr, "ERROR: Incorrect duration value '%s'\n", *v15);
return 1;
}
envp = (const char **)v23;
v5 += 2;
gDuration = v23;
}
else
{
v20 = &v4[v7 + 1];
*(_QWORD *)&argc = *v20;
if ( !*v20 )
goto LABEL_51;
v21 = get_size(*(char **)&argc);
v23 = v21;
if ( v21 <= 0 )
{
fprintf(stderr, "ERROR: Incorrect block size value '%s'\n", *v20);
return 1;
}
envp = (const char **)&gBlockSize;
v5 += 2;
gBlockSize = v21;
}
}
else
{
v18 = &v4[v7 + 1];
*(_QWORD *)&argc = *v18;
if ( !*v18 )
goto LABEL_51;
v19 = get_size(*(char **)&argc);
v23 = v19;
if ( v19 <= 0 )
{
fprintf(stderr, "ERROR: Incorrect size value '%s'\n", *v18);
return 1;
}
envp = (const char **)&gFileSize;
v5 += 2;
gFileSize = v19;
}
}
while ( v5 < v3 );
v16 = stderr;
fprintf(
stderr,
"* Using file size %u bytes (%ukB %uMB %uGB). Duration: %u seconds.\n",
gFileSize,
gFileSize >> 10,
gFileSize >> 20,
gFileSize >> 30,
gDuration);
if ( gTestType )
{
if ( gTestType == 1 )
run_write_test(v16, "* Using file size %u bytes (%ukB %uMB %uGB). Duration: %u seconds.\n", v22);
result = 0;
}
else
{
run_read_test(v16, "* Using file size %u bytes (%ukB %uMB %uGB). Duration: %u seconds.\n", v22);
result = 0;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er13
const char **v4; // r14
int v5; // er15
const char *v6; // rbp
__int64 v7; // r10
unsigned int v8; // eax
bool v9; // cf
bool v10; // zf
__int64 v11; // rcx
bool v12; // cf
bool v13; // zf
__int64 v14; // rcx
const char **v15; // rbp
struct _IO_FILE *v16; // rdi
int result; // eax
const char **v18; // rbp
int v19; // eax
const char **v20; // rbp
int v21; // eax
__int64 v22; // [rsp-10h] [rbp-68h]
unsigned int v23; // [rsp+1Ch] [rbp-3Ch]

v23 = 0;
*(__m128i *)gTestFile = _mm_load_si128((const __m128i *)&xmmword_402610);
if ( argc <= 1 )
{
LABEL_48:
func8(*(_QWORD *)&argc, argv, envp);
return 0;
}
v3 = argc;
v4 = argv;
v5 = 1;
do
{
v6 = v4[v5];
v7 = v5;
v8 = *(unsigned __int8 *)v6;
v9 = v8 < 0x2D;
v10 = v8 == 45;
if ( v8 == 45 )
{
v9 = v6[1] < 0x68u;
v10 = v6[1] == 104;
if ( v6[1] == 104 )
{
v9 = 0;
v10 = v6[2] == 0;
if ( !v6[2] )
goto LABEL_48;
}
}
v11 = 7LL;
argv = (const char **)v4[v5];
*(_QWORD *)&argc = "--help";
do
{
if ( !v11 )
break;
v9 = *(_BYTE *)argv < **(_BYTE **)&argc;
v10 = *(_BYTE *)argv == **(_BYTE **)&argc;
argv = (const char **)((char *)argv + 1);
++*(_QWORD *)&argc;
--v11;
}
while ( v10 );
LOBYTE(envp) = (!v9 && !v10) - v9;
if ( !(_BYTE)envp )
goto LABEL_48;
v12 = v8 < 0x2D;
v13 = v8 == 45;
if ( v8 == 45 )
{
v12 = v6[1] < 0x76u;
v13 = v6[1] == 118;
if ( v6[1] == 118 )
{
v12 = 0;
v13 = v6[2] == 0;
if ( !v6[2] )
goto LABEL_49;
}
}
v14 = 10LL;
argv = (const char **)v4[v5];
*(_QWORD *)&argc = "--version";
do
{
if ( !v14 )
break;
v12 = *(_BYTE *)argv < **(_BYTE **)&argc;
v13 = *(_BYTE *)argv == **(_BYTE **)&argc;
argv = (const char **)((char *)argv + 1);
++*(_QWORD *)&argc;
--v14;
}
while ( v13 );
LOBYTE(envp) = (!v12 && !v13) - v12;
if ( !(_BYTE)envp )
{
LABEL_49:
func7(*(_QWORD *)&argc, argv);
return 0;
}
if ( v8 != 45 )
{
LABEL_18:
*(_QWORD *)&argc = v4[v5];
if ( strcmp(*(const char **)&argc, "-f") )
{
argv = (const char **)"Bad parameter! (%s)\n";
*(_QWORD *)&argc = stderr;
fprintf(stderr, "Bad parameter! (%s)\n", v6);
LABEL_51:
func8(*(_QWORD *)&argc, argv, envp);
return 1;
}
argv = (const char **)v4[v5 + 1];
if ( !argv )
goto LABEL_51;
strcpy(gTestFile, (const char *)argv);
*(_QWORD *)&argc = gTestFile;
argv = 0LL;
if ( !access(gTestFile, 0) )
{
fprintf(stderr, "ERROR: File exists (%s)\n", gTestFile);
return 1;
}
v5 += 2;
continue;
}
if ( v6[1] == 114 && !v6[2] )
{
++v5;
gTestType = 0;
continue;
}
if ( v6[1] == 119 && !v6[2] )
{
++v5;
gTestType = 1;
continue;
}
if ( v6[1] != 115 || v6[2] )
{
if ( v6[1] != 98 || v6[2] )
{
if ( v6[1] != 116 || v6[2] )
goto LABEL_18;
v15 = &v4[v7 + 1];
*(_QWORD *)&argc = *v15;
if ( !*v15 )
goto LABEL_51;
argv = (const char **)"%u";
if ( (unsigned int)__isoc99_sscanf() != 1 )
{
fprintf(stderr, "ERROR: Incorrect duration value '%s'\n", *v15);
return 1;
}
envp = (const char **)v23;
v5 += 2;
gDuration = v23;
}
else
{
v20 = &v4[v7 + 1];
*(_QWORD *)&argc = *v20;
if ( !*v20 )
goto LABEL_51;
v21 = func9(*(char **)&argc);
v23 = v21;
if ( v21 <= 0 )
{
fprintf(stderr, "ERROR: Incorrect block size value '%s'\n", *v20);
return 1;
}
envp = (const char **)&gBlockSize;
v5 += 2;
gBlockSize = v21;
}
}
else
{
v18 = &v4[v7 + 1];
*(_QWORD *)&argc = *v18;
if ( !*v18 )
goto LABEL_51;
v19 = func9(*(char **)&argc);
v23 = v19;
if ( v19 <= 0 )
{
fprintf(stderr, "ERROR: Incorrect size value '%s'\n", *v18);
return 1;
}
envp = (const char **)&gFileSize;
v5 += 2;
gFileSize = v19;
}
}
while ( v5 < v3 );
v16 = stderr;
fprintf(
stderr,
"* Using file size %u bytes (%ukB %uMB %uGB). Duration: %u seconds.\n",
gFileSize,
gFileSize >> 10,
gFileSize >> 20,
gFileSize >> 30,
gDuration);
if ( gTestType )
{
if ( gTestType == 1 )
func11(v16, "* Using file size %u bytes (%ukB %uMB %uGB). Duration: %u seconds.\n", v22);
result = 0;
}
else
{
func10(v16, "* Using file size %u bytes (%ukB %uMB %uGB). Duration: %u seconds.\n", v22);
result = 0;
}
return result;
}

original_code:
int
main(int argc, char** argv)
{
  strcpy(gTestFile, "/tmp/fstest.tmp");
  int i = 1;
  int val = 0;
  if (argc < 2) {
    print_usage();
    return 0;
  }
  while (i < argc) {
    if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
      print_usage();
      return 0;
    }
    else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) {
      print_version();
      return 0;
    }
    else if (!strcmp(argv[i], "-r")) {
      gTestType = TEST_TYPE_READ;
      i++;
      continue;
    }
    else if (!strcmp(argv[i], "-w")) {
      gTestType = TEST_TYPE_WRITE;
      i++;
      continue;
    }
    else if (!strcmp(argv[i], "-s")) {
      if (argv[i + 1] == ((void *)0)) {
        print_usage();
        return 1;
      }
      val = get_size(argv[i+1]);
      if (val <= 0) {
        fprintf(stderr, "ERROR: Incorrect size value '%s'\n", argv[i+1]);
        return 1;
      }
      gFileSize = (uint32_t) val;
      i += 2;
    }
    else if (!strcmp(argv[i], "-b")) {
      if (argv[i + 1] == ((void *)0)) {
        print_usage();
        return 1;
      }
      val = get_size(argv[i+1]);
      if (val <= 0) {
        fprintf(stderr, "ERROR: Incorrect block size value '%s'\n", argv[i+1]);
        return 1;
      }
      gBlockSize = (uint32_t) val;
      i += 2;
    }
    else if (!strcmp(argv[i], "-t")) {
      if (argv[i + 1] == ((void *)0)) {
        print_usage();
        return 1;
      }
      if (sscanf(argv[i+1], "%u", &val) != 1) {
        fprintf(stderr, "ERROR: Incorrect duration value '%s'\n", argv[i+1]);
        return 1;
      }
      gDuration = val;
      i += 2;
    }
    else if (!strcmp(argv[i], "-f")) {
      if (argv[i + 1] == ((void *)0)) {
        print_usage();
        return 1;
      }
      strcpy(gTestFile, argv[i+1]);
      if (access(gTestFile, 0) == 0) {
        fprintf(stderr, "ERROR: File exists (%s)\n", gTestFile);
        return 1;
      }
      i += 2;
    }
    else {
      fprintf(stderr, "Bad parameter! (%s)\n", argv[i]);
      print_usage();
      return 1;
    }
  }
  fprintf(stderr, "* Using file size %u bytes (%ukB %uMB %uGB). Duration: %u seconds.\n",
          gFileSize, gFileSize / 1024, gFileSize / (1024 * 1024),
          gFileSize / (1024 * 1024 * 1024), gDuration);
  switch (gTestType) {
  case TEST_TYPE_READ:
    run_read_test();
    break;
  case TEST_TYPE_WRITE:
    run_write_test();
    break;
  }
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  strcpy(gTestFile, "/tmp/fstest.tmp");
  int i = 1;
  int val = 0;
  if (argc < 2) {
    print_usage();
    return 0;
  }
  while (i < argc) {
    if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
      print_usage();
      return 0;
    }
    else if (!strcmp(argv[i], "-v") || !strcmp(argv[i], "--version")) {
      print_version();
      return 0;
    }
    else if (!strcmp(argv[i], "-r")) {
      gTestType = TEST_TYPE_READ;
      i++;
      continue;
    }
    else if (!strcmp(argv[i], "-w")) {
      gTestType = TEST_TYPE_WRITE;
      i++;
      continue;
    }
    else if (!strcmp(argv[i], "-s")) {
      if (argv[i + 1] == ((void *)0)) {
        print_usage();
        return 1;
      }
      val = get_size(argv[i+1]);
      if (val <= 0) {
        fprintf(stderr, "ERROR: Incorrect size value '%s'\n", argv[i+1]);
        return 1;
      }
      gFileSize = (unsigned int) val;
      i += 2;
    }
    else if (!strcmp(argv[i], "-b")) {
      if (argv[i + 1] == ((void *)0)) {
        print_usage();
        return 1;
      }
      val = get_size(argv[i+1]);
      if (val <= 0) {
        fprintf(stderr, "ERROR: Incorrect block size value '%s'\n", argv[i+1]);
        return 1;
      }
      gBlockSize = (unsigned int) val;
      i += 2;
    }
    else if (!strcmp(argv[i], "-t")) {
      if (argv[i + 1] == ((void *)0)) {
        print_usage();
        return 1;
      }
      if (sscanf(argv[i+1], "%u", &val) != 1) {
        fprintf(stderr, "ERROR: Incorrect duration value '%s'\n", argv[i+1]);
        return 1;
      }
      gDuration = val;
      i += 2;
    }
    else if (!strcmp(argv[i], "-f")) {
      if (argv[i + 1] == ((void *)0)) {
        print_usage();
        return 1;
      }
      strcpy(gTestFile, argv[i+1]);
      if (access(gTestFile, 0) == 0) {
        fprintf(stderr, "ERROR: File exists (%s)\n", gTestFile);
        return 1;
      }
      i += 2;
    }
    else {
      fprintf(stderr, "Bad parameter! (%s)\n", argv[i]);
      print_usage();
      return 1;
    }
  }
  fprintf(stderr, "* Using file size %u bytes (%ukB %uMB %uGB). Duration: %u seconds.\n",
          gFileSize, gFileSize / 1024, gFileSize / (1024 * 1024),
          gFileSize / (1024 * 1024 * 1024), gDuration);
  switch (gTestType) {
  case TEST_TYPE_READ:
    run_read_test();
    break;
  case TEST_TYPE_WRITE:
    run_write_test();
    break;
  }
  return 0;
}


decompiled_code:
size_t print_version()
{
return fwrite(
"fstest (Torpman's Test Tools) 0.1\n"
"Copyright (C) 2012 Peter 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",
1uLL,
0x122uLL,
stderr);
}

canonical_decompiled_code:
size_t func7()
{
return fwrite(
"fstest (Torpman's Test Tools) 0.1\n"
"Copyright (C) 2012 Peter 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",
1uLL,
0x122uLL,
stderr);
}

original_code:
static void
print_version(void)
{
    fprintf(stderr,
            "fstest (Torpman's Test Tools) 0.1\n"
            "Copyright (C) 2012 Peter Torpman (peter@torpman.se)\n"
            "This is free software.  \nYou 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");
}

canonical_original_code:
static void print_version()
{
    fprintf(stderr,
            "fstest (Torpman's Test Tools) 0.1\n"
            "Copyright (C) 2012 Peter Torpman (peter@torpman.se)\n"
            "This is free software.  \nYou 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");
}


decompiled_code:
size_t print_usage()
{
return fwrite(
"Usage: fstest [options]\n"
" Options:\n"
" -r Read test\n"
" -w Write test\n"
" -d Delete test\n"
" -s <size> Size in bytes. Can specify using k, M and G.\n"
" -b <size> Number of bytes to read/write per operation. Can specify using k, M and G.\n"
" -t <secs> Duration in seconds. \n"
" -f <name> File name (including path)\n"
" -v Display version information\n"
" -h Display this text\n"
"\n"
"For bug reporting and suggestions, mail peter@torpman.se\n",
1uLL,
0x1CAuLL,
stderr);
}

canonical_decompiled_code:
size_t func8()
{
return fwrite(
"Usage: fstest [options]\n"
" Options:\n"
" -r Read test\n"
" -w Write test\n"
" -d Delete test\n"
" -s <size> Size in bytes. Can specify using k, M and G.\n"
" -b <size> Number of bytes to read/write per operation. Can specify using k, M and G.\n"
" -t <secs> Duration in seconds. \n"
" -f <name> File name (including path)\n"
" -v Display version information\n"
" -h Display this text\n"
"\n"
"For bug reporting and suggestions, mail peter@torpman.se\n",
1uLL,
0x1CAuLL,
stderr);
}

original_code:
static void
print_usage(void)
{
    fprintf(stderr,
            "Usage: fstest [options]\n"
            "  Options:\n"
            "  -r         Read test\n"
            "  -w         Write test\n"
            "  -d         Delete test\n"
            "  -s <size>  Size in bytes. Can specify using k, M and G.\n"
            "  -b <size>  Number of bytes to read/write per operation. "
            "Can specify using k, M and G.\n"
            "  -t <secs>  Duration in seconds. \n"
            "  -f <name>  File name (including path)\n"
            "  -v         Display version information\n"
            "  -h         Display this text\n\n"
            "For bug reporting and suggestions, mail peter@torpman.se\n");
}

canonical_original_code:
static void print_usage()
{
    fprintf(stderr,
            "Usage: fstest [options]\n"
            "  Options:\n"
            "  -r         Read test\n"
            "  -w         Write test\n"
            "  -d         Delete test\n"
            "  -s <size>  Size in bytes. Can specify using k, M and G.\n"
            "  -b <size>  Number of bytes to read/write per operation. "
            "Can specify using k, M and G.\n"
            "  -t <secs>  Duration in seconds. \n"
            "  -f <name>  File name (including path)\n"
            "  -v         Display version information\n"
            "  -h         Display this text\n\n"
            "For bug reporting and suggestions, mail peter@torpman.se\n");
}


decompiled_code:
void __fastcall allocate_clients(int a1)
{
char *v1; // rbx
char *v2; // rax

if ( a1 > 0 )
{
v1 = (char *)&gClients;
do
{
v1 += 8;
v2 = (char *)malloc(0x60uLL);
*((_QWORD *)v1 - 1) = v2;
pthread_mutex_init((pthread_mutex_t *)(v2 + 24), 0LL);
}
while ( v1 != (char *)&gClients + 8 * (unsigned int)(a1 - 1) + 8 );
}
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
char *v1; // rbx
char *v2; // rax

if ( a1 > 0 )
{
v1 = (char *)&gClients;
do
{
v1 += 8;
v2 = (char *)malloc(0x60uLL);
*((_QWORD *)v1 - 1) = v2;
pthread_mutex_init((pthread_mutex_t *)(v2 + 24), 0LL);
}
while ( v1 != (char *)&gClients + 8 * (unsigned int)(a1 - 1) + 8 );
}
}

original_code:
static void
allocate_clients(uint32_t num)
{
    for (int i = 0; i < (int)num; i++) {
        gClients[i] = (client_data_t*)malloc(sizeof(client_data_t));
        pthread_mutex_init(&gClients[i]->mutex, ((void *)0));
    }
}

canonical_original_code:
static void allocate_clients(unsigned int num)
{
    for (int i = 0; i < (int)num; i++) {
        gClients[i] = (struct client_data_t *)malloc(sizeof(client_data_t));
        pthread_mutex_init(&gClients[i]->mutex, ((void *)0));
    }
}

variable_types:
num
unsigned int

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
__int64 __fastcall connect_to_server(const char *a1, __int16 a2)
{
int v2; // eax
unsigned int v3; // er12
socklen_t v4; // edx
int *v6; // rax
struct in_addr inp; // [rsp+0h] [rbp-30h] BYREF
int optval; // [rsp+4h] [rbp-2Ch] BYREF
struct sockaddr v9; // [rsp+8h] [rbp-28h] BYREF

optval = 1;
v9 = 0LL;
if ( inet_aton(a1, &inp) )
{
v2 = socket(2, 1, 0);
v3 = v2;
if ( v2 < 0 )
{
v3 = -1;
debug_print("ERROR: Failed to open socket!\n");
}
else
{
setsockopt(v2, 1, 2, &optval, 4u);
v9.sa_family = 2;
*(struct in_addr *)&v9.sa_data[2] = inp;
*(_WORD *)v9.sa_data = _bswap_16(a2);
if ( connect(v3, &v9, v4) < 0 )
{
v6 = _errno_location();
v3 = -1;
debug_print("ERROR: Failed to connect socket! errno=%d\n", (unsigned int)*v6);
perror("Could not connect socket");
}
}
}
else
{
v3 = -1;
debug_print("ERROR: Unknown server address!\n");
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1, __int16 a2)
{
int v2; // eax
unsigned int v3; // er12
socklen_t v4; // edx
int *v6; // rax
struct in_addr inp; // [rsp+0h] [rbp-30h] BYREF
int optval; // [rsp+4h] [rbp-2Ch] BYREF
struct sockaddr v9; // [rsp+8h] [rbp-28h] BYREF

optval = 1;
v9 = 0LL;
if ( inet_aton(a1, &inp) )
{
v2 = socket(2, 1, 0);
v3 = v2;
if ( v2 < 0 )
{
v3 = -1;
debug_print("ERROR: Failed to open socket!\n");
}
else
{
setsockopt(v2, 1, 2, &optval, 4u);
v9.sa_family = 2;
*(struct in_addr *)&v9.sa_data[2] = inp;
*(_WORD *)v9.sa_data = func1(a2);
if ( connect(v3, &v9, v4) < 0 )
{
v6 = _errno_location();
v3 = -1;
debug_print("ERROR: Failed to connect socket! errno=%d\n", (unsigned int)*v6);
perror("Could not connect socket");
}
}
}
else
{
v3 = -1;
debug_print("ERROR: Unknown server address!\n");
}
return v3;
}

original_code:
static int
connect_to_server(char* serverIp, uint32_t serverPort)
{
    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));
    if (inet_aton(serverIp, &inAddr) == 0) {
        debug_print("ERROR: Unknown server address!\n");
        return -1;
    }
    sock = socket(2, SOCK_STREAM, 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(serverPort);
    res = connect(sock, (struct sockaddr*)&sinAddr, sizeof(struct sockaddr));
    if (res < 0) {
        debug_print("ERROR: Failed to connect socket! errno=%d\n", (*__errno_location ()));
        perror("Could not connect socket");
        return -1;
    }
    return sock;
}

canonical_original_code:
static int connect_to_server(char *serverIp, unsigned int serverPort)
{
    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));
    if (inet_aton(serverIp, &inAddr) == 0) {
        debug_print("ERROR: Unknown server address!\n");
        return -1;
    }
    sock = socket(2, SOCK_STREAM, 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(serverPort);
    res = connect(sock, (struct sockaddr *)&sinAddr, sizeof(struct sockaddr));
    if (res < 0) {
        debug_print("ERROR: Failed to connect socket! errno=%d\n", (*__errno_location ()));
        perror("Could not connect socket");
        return -1;
    }
    return sock;
}

variable_types:
serverIp
char *

variable_types:
serverPort
unsigned 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: 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 connect_clients(int a1)
{
__int64 i; // r14
__int64 v2; // r13
int v3; // eax

if ( a1 <= 0 )
return 0LL;
for ( i = 0LL; i != a1; ++i )
{
v2 = gClients[i];
*(_QWORD *)v2 = 0LL;
*(_QWORD *)(v2 + 88) = 0LL;
memset(
(void *)((v2 + 8) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)v2 - (((_DWORD)v2 + 8) & 0xFFFFFFF8) + 96) >> 3));
*(_DWORD *)(v2 + 4) = i;
v3 = connect_to_server((const char *)(gAppArgs + 10), *(_WORD *)(gAppArgs + 8));
*(_DWORD *)v2 = v3;
if ( v3 < 0 )
{
debug_print("ERROR: Could not open socket (%d)!\n", (unsigned int)i);
return 1LL;
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
__int64 i; // r14
__int64 v2; // r13
int v3; // eax

if ( a1 <= 0 )
return 0LL;
for ( i = 0LL; i != a1; ++i )
{
v2 = gClients[i];
*(_QWORD *)v2 = 0LL;
*(_QWORD *)(v2 + 88) = 0LL;
memset(
(void *)((v2 + 8) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)v2 - (((_DWORD)v2 + 8) & 0xFFFFFFF8) + 96) >> 3));
*(_DWORD *)(v2 + 4) = i;
v3 = func2((const char *)(gAppArgs + 10), *(_WORD *)(gAppArgs + 8));
*(_DWORD *)v2 = v3;
if ( v3 < 0 )
{
debug_print("ERROR: Could not open socket (%d)!\n", (unsigned int)i);
return 1LL;
}
}
return 0LL;
}

original_code:
static int
connect_clients(uint32_t num)
{
    for (int i = 0; i < (int)num; i++) {
        client_data_t* cliData = gClients[i];
        memset(cliData, 0, sizeof(client_data_t));
        cliData->clientNum = i;
        cliData->sock = connect_to_server(gAppArgs->serverIp, gAppArgs->serverPort);
        if (cliData->sock < 0) {
            debug_print("ERROR: Could not open socket (%d)!\n", i);
            return 1;
        }
    }
    return 0;
}

canonical_original_code:
static int connect_clients(unsigned int num)
{
    for (int i = 0; i < (int)num; i++) {
        struct client_data_t *cliData = gClients[i];
        memset(cliData, 0, sizeof(client_data_t));
        cliData->clientNum = i;
        cliData->sock = connect_to_server(gAppArgs->serverIp, gAppArgs->serverPort);
        if (cliData->sock < 0) {
            debug_print("ERROR: Could not open socket (%d)!\n", i);
            return 1;
        }
    }
    return 0;
}

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:
__int64 __fastcall create_client_udp_sockets(unsigned int a1)
{
__int64 v1; // r12
__int64 v2; // rbx
__int64 v3; // rsi
__int16 v4; // di
__int16 v5; // ax
_WORD *v6; // rdx
int *v7; // r13
int v8; // eax
__int64 v9; // rdx

if ( !a1 )
return 0LL;
v1 = a1;
v2 = 0LL;
do
{
v7 = (int *)gClients[v2];
v8 = socket(2, 2, 17);
v9 = gClients[v2];
*v7 = v8;
if ( *(int *)v9 < 0 )
return 0xFFFFFFFFLL;
v3 = gAppArgs;
*(_WORD *)(v9 + 80) = 2;
v4 = v2++ + *(_WORD *)(v3 + 8);
v5 = _bswap_16(v4);
*(v6 - 1) = v5;
inet_pton(2, (const char *)(v3 + 10), v6);
}
while ( v2 != v1 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1)
{
__int64 v1; // r12
__int64 v2; // rbx
__int64 v3; // rsi
__int16 v4; // di
__int16 v5; // ax
_WORD *v6; // rdx
int *v7; // r13
int v8; // eax
__int64 v9; // rdx

if ( !a1 )
return 0LL;
v1 = a1;
v2 = 0LL;
do
{
v7 = (int *)gClients[v2];
v8 = socket(2, 2, 17);
v9 = gClients[v2];
*v7 = v8;
if ( *(int *)v9 < 0 )
return 0xFFFFFFFFLL;
v3 = gAppArgs;
*(_WORD *)(v9 + 80) = 2;
v4 = v2++ + *(_WORD *)(v3 + 8);
v5 = func1(v4);
*(v6 - 1) = v5;
inet_pton(2, (const char *)(v3 + 10), v6);
}
while ( v2 != v1 );
return 0LL;
}

original_code:
static int create_client_udp_sockets(uint32_t num)
{
    for (uint32_t i = 0; i < num; i++) {
        gClients[i]->sock = socket(2, SOCK_DGRAM, IPPROTO_UDP);
        if (gClients[i]->sock < 0) {
            return -1;
        }
        struct sockaddr_in* sinAddr = &gClients[i]->serverAddress;
        sinAddr->sin_family = 2;
        sinAddr->sin_port = htons(gAppArgs->serverPort + i);
        inet_pton(2, gAppArgs->serverIp, &sinAddr->sin_addr.s_addr);
    }
    return 0;
}

canonical_original_code:
static int create_client_udp_sockets(unsigned int num)
{
    for (unsigned int i = 0; i < num; i++) {
        gClients[i]->sock = socket(2, SOCK_DGRAM, IPPROTO_UDP);
        if (gClients[i]->sock < 0) {
            return -1;
        }
        struct sockaddr_in *sinAddr = &gClients[i]->serverAddress;
        sinAddr->sin_family = 2;
        sinAddr->sin_port = htons(gAppArgs->serverPort + i);
        inet_pton(2, gAppArgs->serverIp, &sinAddr->sin_addr.s_addr);
    }
    return 0;
}

variable_types:
i
unsigned 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:
int __fastcall adjust_traffic(double a1)
{
int result; // eax
double v2; // xmm0_8
double v3; // xmm1_8
double v4; // xmm0_8

result = *(_DWORD *)(gAppArgs + 268);
if ( result )
{
v2 = a1 * 0.0009765625;
v3 = (double)result;
if ( v2 <= (double)result )
{
if ( v3 > v2 && v3 - v2 > v3 * 0.01 )
{
pthread_mutex_lock(&gMutexRate);
gBytesToSend = (unsigned int)(int)((v3 - v2) * 0.25 / (double)*(int *)(gAppArgs + 272)
+ (double)(int)gBytesToSend);
result = pthread_mutex_unlock(&gMutexRate);
}
}
else
{
v4 = v2 - v3;
if ( v4 > v3 * 0.01 )
{
pthread_mutex_lock(&gMutexRate);
gBytesToSend = (unsigned int)(int)((double)(int)gBytesToSend - v4 * 0.25 / (double)*(int *)(gAppArgs + 272));
result = pthread_mutex_unlock(&gMutexRate);
}
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(double a1)
{
int result; // eax
double v2; // xmm0_8
double v3; // xmm1_8
double v4; // xmm0_8

result = *(_DWORD *)(gAppArgs + 268);
if ( result )
{
v2 = a1 * 0.0009765625;
v3 = (double)result;
if ( v2 <= (double)result )
{
if ( v3 > v2 && v3 - v2 > v3 * 0.01 )
{
pthread_mutex_lock(&gMutexRate);
gBytesToSend = (unsigned int)(int)((v3 - v2) * 0.25 / (double)*(int *)(gAppArgs + 272)
+ (double)(int)gBytesToSend);
result = pthread_mutex_unlock(&gMutexRate);
}
}
else
{
v4 = v2 - v3;
if ( v4 > v3 * 0.01 )
{
pthread_mutex_lock(&gMutexRate);
gBytesToSend = (unsigned int)(int)((double)(int)gBytesToSend - v4 * 0.25 / (double)*(int *)(gAppArgs + 272));
result = pthread_mutex_unlock(&gMutexRate);
}
}
}
return result;
}

original_code:
static void
adjust_traffic(double tPutNow)
{
    double diff;
    if (gAppArgs->wantedThroughput > 0 && (tPutNow / 1024) > gAppArgs->wantedThroughput) {
        diff = (tPutNow / 1024) - gAppArgs->wantedThroughput;
        if (diff > (0.01 * gAppArgs->wantedThroughput)) {
            pthread_mutex_lock(&gMutexRate);
            gBytesToSend = gBytesToSend - ((0.25 * diff) / gAppArgs->numConnections);
            if (gBytesToSend < 0) {
                gBytesToSend = 1;
            }
            pthread_mutex_unlock(&gMutexRate);
        }
    }
    else if (gAppArgs->wantedThroughput > 0 && (tPutNow / 1024) < gAppArgs->wantedThroughput) {
        diff = gAppArgs->wantedThroughput - (tPutNow / 1024);
        if (diff > (0.01 * gAppArgs->wantedThroughput)) {
            pthread_mutex_lock(&gMutexRate);
            gBytesToSend = gBytesToSend + ((0.25 * diff) / gAppArgs->numConnections);
            pthread_mutex_unlock(&gMutexRate);
        }
    }
}

variable_types: tPutNow double

variable_types: diff double


decompiled_code:
__int64 __fastcall create_client_threads(int a1)
{
__int64 v1; // r15
__int64 *v2; // rbx
__int64 v3; // rax
__int64 result; // rax
pthread_attr_t attr; // [rsp+10h] [rbp-78h] BYREF

pthread_mutex_lock(&gMutex);
if ( pthread_create(&gTpThread, 0LL, tp_thread, 0LL) < 0 )
{
debug_print("ERROR: Failed to start throughput thread!\n");
result = 1LL;
}
else if ( a1 <= 0 )
{
LABEL_9:
pthread_mutex_unlock(&gMutex);
result = 0LL;
}
else
{
v1 = 0LL;
while ( 1 )
{
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, 1);
pthread_attr_setscope(&attr, 0);
if ( pthread_create((pthread_t *)(gClients[v1] + 16LL), &attr, send_thread, (void *)gClients[v1]) )
break;
if ( ++v1 == a1 )
goto LABEL_9;
}
v2 = gClients;
debug_print("* Failed to create sender #%d\n", *(unsigned int *)(gClients[(int)v1] + 4LL));
perror("Thread error!");
gDoExit = 1;
do
{
v3 = *v2++;
pthread_join(*(_QWORD *)(v3 + 16), 0LL);
}
while ( v2 != &gClients[(unsigned int)(a1 - 1) + 1] );
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1)
{
__int64 v1; // r15
__int64 *v2; // rbx
__int64 v3; // rax
__int64 result; // rax
pthread_attr_t attr; // [rsp+10h] [rbp-78h] BYREF

pthread_mutex_lock(&gMutex);
if ( pthread_create(&gTpThread, 0LL, tp_thread, 0LL) < 0 )
{
debug_print("ERROR: Failed to start throughput thread!\n");
result = 1LL;
}
else if ( a1 <= 0 )
{
LABEL_9:
pthread_mutex_unlock(&gMutex);
result = 0LL;
}
else
{
v1 = 0LL;
while ( 1 )
{
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, 1);
pthread_attr_setscope(&attr, 0);
if ( pthread_create((pthread_t *)(gClients[v1] + 16LL), &attr, send_thread, (void *)gClients[v1]) )
break;
if ( ++v1 == a1 )
goto LABEL_9;
}
v2 = gClients;
debug_print("* Failed to create sender #%d\n", *(unsigned int *)(gClients[(int)v1] + 4LL));
perror("Thread error!");
gDoExit = 1;
do
{
v3 = *v2++;
pthread_join(*(_QWORD *)(v3 + 16), 0LL);
}
while ( v2 != &gClients[(unsigned int)(a1 - 1) + 1] );
result = 1LL;
}
return result;
}

original_code:
static int
create_client_threads(uint32_t num)
{
    int res = 0;
    pthread_mutex_lock(&gMutex);
    res = pthread_create(&gTpThread, ((void *)0), tp_thread, ((void *)0));
    if (res < 0) {
        debug_print("ERROR: Failed to start throughput thread!\n");
        return 1;
    }
    for (int i = 0; i < (int)num; i++) {
        pthread_t t;
        pthread_attr_t attr;
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
        pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
        res = pthread_create(&gClients[i]->thread, &attr, send_thread, (void*)gClients[i]);
        if (res != 0) {
            debug_print("* Failed to create sender #%d\n", gClients[i]->clientNum);
            perror("Thread error!");
            gDoExit = 1;
            goto outro;
        }
    }
    pthread_mutex_unlock(&gMutex);
    return 0;
outro:
    for (int i = 0; i < (int)num; i++) {
        pthread_join(gClients[i]->thread, ((void *)0));
    }
    return 1;
}

canonical_original_code:
static int create_client_threads(unsigned int num)
{
    int res = 0;
    pthread_mutex_lock(&gMutex);
    res = pthread_create(&gTpThread, ((void *)0), tp_thread, ((void *)0));
    if (res < 0) {
        debug_print("ERROR: Failed to start throughput thread!\n");
        return 1;
    }
    for (int i = 0; i < (int)num; i++) {
        unsigned long t;
        union pthread_attr_t attr;
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
        pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
        res = pthread_create(&gClients[i]->thread, &attr, send_thread, (void *)gClients[i]);
        if (res != 0) {
            debug_print("* Failed to create sender #%d\n", gClients[i]->clientNum);
            perror("Thread error!");
            gDoExit = 1;
            goto outro;
        }
    }
    pthread_mutex_unlock(&gMutex);
    return 0;
outro:
    for (int i = 0; i < (int)num; i++) {
        pthread_join(gClients[i]->thread, ((void *)0));
    }
    return 1;
}

variable_types: i int

variable_types:
t
unsigned long

variable_types: union pthread_attr_t { char __size[56]; long __align; }


decompiled_code:
void __fastcall __noreturn send_thread(void *a1)
{
unsigned int v2; // ebx
int v3; // eax
int v4; // edi

pthread_mutex_lock(&gMutex);
if ( gDoExit )
LABEL_10:
pthread_exit(a1);
pthread_mutex_unlock(&gMutex);
if ( *(_WORD *)(gAppArgs + 282) )
v2 = *(unsigned __int16 *)(gAppArgs + 280);
else
v2 = gBytesToSend;
while ( 1 )
{
while ( 1 )
{
usleep(0xAu);
if ( gDoExit )
{
debug_print("Client exit\n");
close(*(_DWORD *)a1);
goto LABEL_10;
}
v4 = *(_DWORD *)a1;
if ( *(_DWORD *)gAppArgs )
break;
v3 = send(v4, &gSendBuffer, v2, 0x4000);
if ( v3 > 0 )
goto LABEL_8;
}
v3 = sendto(v4, &gSendBuffer, v2, 0, (const struct sockaddr *)a1 + 5, 0x10u);
if ( v3 > 0 )
{
LABEL_8:
*((_QWORD *)a1 + 8) += v3;
*((_QWORD *)a1 + 9) += v3;
}
}
}

canonical_decompiled_code:
void __fastcall __noreturn func7(void *a1)
{
unsigned int v2; // ebx
int v3; // eax
int v4; // edi

pthread_mutex_lock(&gMutex);
if ( gDoExit )
LABEL_10:
pthread_exit(a1);
pthread_mutex_unlock(&gMutex);
if ( *(_WORD *)(gAppArgs + 282) )
v2 = *(unsigned __int16 *)(gAppArgs + 280);
else
v2 = gBytesToSend;
while ( 1 )
{
while ( 1 )
{
usleep(0xAu);
if ( gDoExit )
{
debug_print("Client exit\n");
close(*(_DWORD *)a1);
goto LABEL_10;
}
v4 = *(_DWORD *)a1;
if ( *(_DWORD *)gAppArgs )
break;
v3 = send(v4, &gSendBuffer, v2, 0x4000);
if ( v3 > 0 )
goto LABEL_8;
}
v3 = sendto(v4, &gSendBuffer, v2, 0, (const struct sockaddr *)a1 + 5, 0x10u);
if ( v3 > 0 )
{
LABEL_8:
*((_QWORD *)a1 + 8) += v3;
*((_QWORD *)a1 + 9) += v3;
}
}
}

original_code:
static void*
send_thread(void* data)
{
    client_data_t* cliData = (client_data_t*)data;
    int res = 0;
    pthread_mutex_lock(&gMutex);
    if (gDoExit) {
        pthread_exit(cliData);
    }
    pthread_mutex_unlock(&gMutex);
    uint32_t bytesToSend = gAppArgs->usedPacketSize > 0 ? gAppArgs->packetSize : gBytesToSend;
    size_t addrSize = sizeof(struct sockaddr_in);
    while (1) {
        usleep(10);
        if (gDoExit) {
            debug_print("Client exit\n");
            close(cliData->sock);
            pthread_exit(cliData);
        }
        if (!gAppArgs->useUdp) {
            res = send(cliData->sock, gSendBuffer, bytesToSend, MSG_NOSIGNAL);
        }
        else {
            res = sendto(cliData->sock, gSendBuffer, bytesToSend, 0, (struct sockaddr*)&cliData->serverAddress, addrSize);
        }
        if (res > 0) {
            cliData->numSinceStart += res;
            cliData->numSinceMeasure += res;
        }
    }
}

canonical_original_code:
static void *send_thread(void *data)
{
    struct client_data_t *cliData = (client_data_t*)data;
    int res = 0;
    pthread_mutex_lock(&gMutex);
    if (gDoExit) {
        pthread_exit(cliData);
    }
    pthread_mutex_unlock(&gMutex);
    unsigned int bytesToSend = gAppArgs->usedPacketSize > 0 ? gAppArgs->packetSize : gBytesToSend;
    unsigned long addrSize = sizeof(struct sockaddr_in);
    while (1) {
        usleep(10);
        if (gDoExit) {
            debug_print("Client exit\n");
            close(cliData->sock);
            pthread_exit(cliData);
        }
        if (!gAppArgs->useUdp) {
            res = send(cliData->sock, gSendBuffer, bytesToSend, MSG_NOSIGNAL);
        }
        else {
            res = sendto(cliData->sock, gSendBuffer, bytesToSend, 0, (struct sockaddr *)&cliData->serverAddress, addrSize);
        }
        if (res > 0) {
            cliData->numSinceStart += res;
            cliData->numSinceMeasure += res;
        }
    }
}

variable_types:
data
void *

variable_types: res int

variable_types:
bytesToSend
unsigned int

variable_types:
addrSize
unsigned long


decompiled_code:
__int64 __fastcall client_start(__int64 a1)
{
__int64 v1; // rsi
__int64 v2; // rbx
__int16 v3; // ax
unsigned int v4; // edi
int v5; // eax
__int64 result; // rax

gAppArgs = a1;
memset(&gSendBuffer, 255, (size_t)&unk_FFFFFF);
v1 = *(unsigned __int16 *)(a1 + 280);
if ( (_WORD)v1 )
{
inform_user("* Will send packets of size: %d\n", v1);
v2 = gAppArgs;
v3 = *(_WORD *)(gAppArgs + 280);
*(_DWORD *)(gAppArgs + 268) = 0;
*(_WORD *)(v2 + 282) = v3 - 18;
}
else
{
inform_user("* Will aim for throughput: %d\n", *(unsigned int *)(a1 + 268));
v2 = gAppArgs;
}
allocate_clients(*(_DWORD *)(v2 + 272));
v4 = *(_DWORD *)(v2 + 272);
if ( *(_DWORD *)v2 )
{
if ( (unsigned int)create_client_udp_sockets(v4) )
return inform_user("* Failed to open client sockets\n");
}
else
{
v5 = connect_clients(v4);
v2 = gAppArgs;
if ( v5 )
return inform_user("* Failed to connect clients\n");
}
if ( (unsigned int)create_client_threads(*(_DWORD *)(v2 + 272)) )
return inform_user("* Failed to create client threads\n");
if ( (unsigned int)get_aggregator_port() )
aggregator_register(*(unsigned int *)(gAppArgs + 284), "127.0.0.1");
debug_print(
"Clients running (conns=%u)...(tp=%u kbps pktsize=%u)\n",
*(unsigned int *)(gAppArgs + 272),
*(unsigned int *)(gAppArgs + 268),
*(unsigned __int16 *)(gAppArgs + 280));
do
{
usleep(0x3E8u);
result = (unsigned int)gDoExit;
}
while ( !gDoExit );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
__int64 v1; // rsi
__int64 v2; // rbx
__int16 v3; // ax
unsigned int v4; // edi
int v5; // eax
__int64 result; // rax

gAppArgs = a1;
memset(&gSendBuffer, 255, (size_t)&unk_FFFFFF);
v1 = *(unsigned __int16 *)(a1 + 280);
if ( (_WORD)v1 )
{
inform_user("* Will send packets of size: %d\n", v1);
v2 = gAppArgs;
v3 = *(_WORD *)(gAppArgs + 280);
*(_DWORD *)(gAppArgs + 268) = 0;
*(_WORD *)(v2 + 282) = v3 - 18;
}
else
{
inform_user("* Will aim for throughput: %d\n", *(unsigned int *)(a1 + 268));
v2 = gAppArgs;
}
func0(*(_DWORD *)(v2 + 272));
v4 = *(_DWORD *)(v2 + 272);
if ( *(_DWORD *)v2 )
{
if ( (unsigned int)func4(v4) )
return inform_user("* Failed to open client sockets\n");
}
else
{
v5 = func3(v4);
v2 = gAppArgs;
if ( v5 )
return inform_user("* Failed to connect clients\n");
}
if ( (unsigned int)func6(*(_DWORD *)(v2 + 272)) )
return inform_user("* Failed to create client threads\n");
if ( (unsigned int)get_aggregator_port() )
aggregator_register(*(unsigned int *)(gAppArgs + 284), "127.0.0.1");
debug_print(
"Clients running (conns=%u)...(tp=%u kbps pktsize=%u)\n",
*(unsigned int *)(gAppArgs + 272),
*(unsigned int *)(gAppArgs + 268),
*(unsigned __int16 *)(gAppArgs + 280));
do
{
usleep(0x3E8u);
result = (unsigned int)gDoExit;
}
while ( !gDoExit );
return result;
}

original_code:
void client_start(app_arg_t* args)
{
    gAppArgs = args;
    int res = 0;
    memset(gSendBuffer, 0xFF, 0xFFFFFF);
    if (gAppArgs->packetSize > 0) {
        inform_user("* Will send packets of size: %d\n", gAppArgs->packetSize);
        gAppArgs->wantedThroughput = 0;
        gAppArgs->usedPacketSize = gAppArgs->packetSize - (14 + 4);
    } else {
        inform_user("* Will aim for throughput: %d\n", gAppArgs->wantedThroughput);
    }
    allocate_clients(gAppArgs->numConnections);
    if (!gAppArgs->useUdp) {
        if (connect_clients(gAppArgs->numConnections) != 0) {
            inform_user("* Failed to connect clients\n");
            return;
        }
    }
    else {
        if (create_client_udp_sockets(gAppArgs->numConnections) != 0) {
            inform_user("* Failed to open client sockets\n");
            return;
        }
    }
    if (create_client_threads(gAppArgs->numConnections) != 0) {
        inform_user("* Failed to create client threads\n");
        return;
    }
    if (get_aggregator_port() != 0) {
        aggregator_register(gAppArgs->processId, "127.0.0.1");
    }
    debug_print("Clients running (conns=%u)...(tp=%u kbps pktsize=%u)\n",
                gAppArgs->numConnections,
                gAppArgs->wantedThroughput,
                gAppArgs->packetSize);
    while (1) {
        usleep(1000);
        if (gDoExit) {
            break;
        }
    }
}

canonical_original_code:
void client_start(struct app_arg_t *args)
{
    gAppArgs = args;
    int res = 0;
    memset(gSendBuffer, 0xFF, 0xFFFFFF);
    if (gAppArgs->packetSize > 0) {
        inform_user("* Will send packets of size: %d\n", gAppArgs->packetSize);
        gAppArgs->wantedThroughput = 0;
        gAppArgs->usedPacketSize = gAppArgs->packetSize - (14 + 4);
    } else {
        inform_user("* Will aim for throughput: %d\n", gAppArgs->wantedThroughput);
    }
    allocate_clients(gAppArgs->numConnections);
    if (!gAppArgs->useUdp) {
        if (connect_clients(gAppArgs->numConnections) != 0) {
            inform_user("* Failed to connect clients\n");
            return;
        }
    }
    else {
        if (create_client_udp_sockets(gAppArgs->numConnections) != 0) {
            inform_user("* Failed to open client sockets\n");
            return;
        }
    }
    if (create_client_threads(gAppArgs->numConnections) != 0) {
        inform_user("* Failed to create client threads\n");
        return;
    }
    if (get_aggregator_port() != 0) {
        aggregator_register(gAppArgs->processId, "127.0.0.1");
    }
    debug_print("Clients running (conns=%u)...(tp=%u kbps pktsize=%u)\n",
                gAppArgs->numConnections,
                gAppArgs->wantedThroughput,
                gAppArgs->packetSize);
    while (1) {
        usleep(1000);
        if (gDoExit) {
            break;
        }
    }
}

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; } *