decompiled_code:
__int64 set_all_promisc()
{
int v0; // er14
__int64 result; // rax
int v2; // ebx
void *v3; // rsp
unsigned int v4; // ebx
__int64 v5; // rcx
int *v6; // r15
int v7[3]; // [rsp+0h] [rbp-40h] BYREF
int *v8; // [rsp+8h] [rbp-38h]

v0 = socket(2, 2, 0);
result = 0LL;
if ( v0 > 0 )
{
v2 = 320;
do
{
v2 *= 2;
v7[0] = v2;
v3 = alloca(v2 + 8LL);
v8 = v7;
if ( ioctl(v0, 0x8912uLL, v7) )
goto LABEL_5;
}
while ( v7[0] >= v2 );
if ( (unsigned __int64)v7[0] > 0x27 )
{
v4 = 0;
v5 = 0LL;
while ( 1 )
{
v6 = &v7[10 * v5];
if ( ioctl(v0, 0x8913uLL, v6) )
break;
*((_WORD *)v6 + 8) |= 0x100u;
if ( ioctl(v0, 0x8914uLL, v6) )
break;
v5 = ++v4;
if ( v4 >= v7[0] / 0x28uLL )
goto LABEL_13;
}
LABEL_5:
close(v0);
return 0LL;
}
LABEL_13:
close(v0);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
int v0; // er14
__int64 result; // rax
int v2; // ebx
void *v3; // rsp
unsigned int v4; // ebx
__int64 v5; // rcx
int *v6; // r15
int v7[3]; // [rsp+0h] [rbp-40h] BYREF
int *v8; // [rsp+8h] [rbp-38h]

v0 = socket(2, 2, 0);
result = 0LL;
if ( v0 > 0 )
{
v2 = 320;
do
{
v2 *= 2;
v7[0] = v2;
v3 = alloca(v2 + 8LL);
v8 = v7;
if ( ioctl(v0, 0x8912uLL, v7) )
goto LABEL_5;
}
while ( v7[0] >= v2 );
if ( (unsigned __int64)v7[0] > 0x27 )
{
v4 = 0;
v5 = 0LL;
while ( 1 )
{
v6 = &v7[10 * v5];
if ( ioctl(v0, 0x8913uLL, v6) )
break;
*((_WORD *)v6 + 8) |= 0x100u;
if ( ioctl(v0, 0x8914uLL, v6) )
break;
v5 = ++v4;
if ( v4 >= v7[0] / 0x28uLL )
goto LABEL_13;
}
LABEL_5:
close(v0);
return 0LL;
}
LABEL_13:
close(v0);
result = 1LL;
}
return result;
}

original_code:
int set_all_promisc()
{
 struct ifreq * ifaces;
 int ifaces_size=8 * sizeof(struct ifreq);
 struct ifconf param;
 int sock;
 unsigned int i;
 sock = socket(2, SOCK_DGRAM, IPPROTO_IP);
 if (sock <= 0)
  return 0;
        do {
        ifaces_size*=2;
        ifaces=__builtin_alloca (ifaces_size);
 param.ifc_len = ifaces_size;
 param.ifc_ifcu.ifcu_req = ifaces;
 if (ioctl(sock, 0x8912, &param))
  goto err;
 } while (param.ifc_len>=ifaces_size);
 for (i = 0; i < param.ifc_len / sizeof(struct ifreq); i++) {
  if (ioctl(sock, 0x8913, ifaces + i))
   goto err;
  ifaces[i].ifr_ifru.ifru_flags |= IFF_PROMISC;
  if (ioctl(sock, 0x8914, ifaces + i))
   goto err;
 }
 close(sock);
 return 1;
err:
 close(sock);
 return 0;
}

canonical_original_code:
int set_all_promisc()
{
 struct ifreq *ifaces;
 int ifaces_size = 8 * sizeof(struct ifreq);
 struct ifconf param;
 int sock;
 unsigned int i;
 sock = socket(2, SOCK_DGRAM, IPPROTO_IP);
 if (sock <= 0)
  return 0;
        do {
        ifaces_size*=2;
        ifaces=__builtin_alloca (ifaces_size);
 param.ifc_len = ifaces_size;
 param.ifc_ifcu.ifcu_req = ifaces;
 if (ioctl(sock, 0x8912, &param))
  goto err;
 } while (param.ifc_len>=ifaces_size);
 for (i = 0; i < param.ifc_len / sizeof(struct ifreq); i++) {
  if (ioctl(sock, 0x8913, ifaces + i))
   goto err;
  ifaces[i].ifr_ifru.ifru_flags |= IFF_PROMISC;
  if (ioctl(sock, 0x8914, ifaces + i))
   goto err;
 }
 close(sock);
 return 1;
err:
 close(sock);
 return 0;
}

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

variable_types: sock int

variable_types:
i
unsigned int

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

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


decompiled_code:
__int64 __fastcall scan_hash(int a1)
{
return (unsigned int)(((a1 >> 16) ^ (a1 % 0x10000)) % nids_params[11]);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
return (unsigned int)(((a1 >> 16) ^ (a1 % 0x10000)) % nids_params[11]);
}

original_code:
static int
scan_hash(int addr)
{
  return ((addr % 65536) ^ (addr >> 16)) % (nids_params.scan_num_hosts);
}

variable_types: addr int

return_type: int


gettime

decompiled_code:
__int64 gettime()
{
__int64 result; // rax
int v1; // ecx
struct timeval v2; // [rsp+0h] [rbp-18h] BYREF

result = (unsigned int)timenow;
if ( !timenow )
{
gettimeofday(&v2, 0LL);
v1 = 1000 * (LODWORD(v2.tv_sec) - time0);
result = v1 + (unsigned int)(v2.tv_usec / 1000);
timenow = v1 + v2.tv_usec / 1000;
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax
int v1; // ecx
struct timeval v2; // [rsp+0h] [rbp-18h] BYREF

result = (unsigned int)timenow;
if ( !timenow )
{
gettimeofday(&v2, 0LL);
v1 = 1000 * (LODWORD(v2.tv_sec) - time0);
result = v1 + (unsigned int)(v2.tv_usec / 1000);
timenow = v1 + v2.tv_usec / 1000;
}
return result;
}

original_code:
static int
gettime()
{
  struct timeval tv;
  if (timenow)
    return timenow;
  gettimeofday(&tv, 0);
  timenow = (tv.tv_sec - time0) * 1000 + tv.tv_usec / 1000;
  return timenow;
}

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


decompiled_code:
__int64 scan_init()
{
__int64 result; // rax
size_t v1; // rdi
struct timeval v2; // [rsp+0h] [rbp-18h] BYREF

result = (unsigned int)nids_params[11];
if ( (int)result > 0 )
{
gettimeofday(&v2, 0LL);
v1 = (int)nids_params[11];
time0 = v2.tv_sec;
result = (__int64)calloc(v1, 8uLL);
hashhost = (void *)result;
if ( !result )
result = (*(__int64 (__fastcall **)(const char *))&nids_params[14])("scan_init");
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 result; // rax
size_t v1; // rdi
struct timeval v2; // [rsp+0h] [rbp-18h] BYREF

result = (unsigned int)nids_params[11];
if ( (int)result > 0 )
{
gettimeofday(&v2, 0LL);
v1 = (int)nids_params[11];
time0 = v2.tv_sec;
result = (__int64)calloc(v1, 8uLL);
hashhost = (void *)result;
if ( !result )
result = (*(__int64 (__fastcall **)(const char *))&nids_params[14])("scan_init");
}
return result;
}

original_code:
void
scan_init(void)
{
  struct timeval tv;
  if (nids_params.scan_num_hosts > 0) {
    gettimeofday(&tv, 0);
    time0 = tv.tv_sec;
    hashhost = (struct host **) calloc(nids_params.scan_num_hosts, sizeof(struct host *));
    if (!hashhost)
      nids_params.no_mem("scan_init");
  }
}

canonical_original_code:
void scan_init()
{
  struct timeval tv;
  if (nids_params.scan_num_hosts > 0) {
    gettimeofday(&tv, 0);
    time0 = tv.tv_sec;
    hashhost = (struct host **) calloc(nids_params.scan_num_hosts, sizeof(struct host *));
    if (!hashhost)
      nids_params.no_mem("scan_init");
  }
}

return_type: void

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

user_defined_types: struct host { struct host *next; struct host *prev; unsigned int addr; int modtime; int n_packets; struct scan *packets; }

user_defined_types: struct scan { unsigned int addr; unsigned short port; unsigned char flags; }


decompiled_code:
void scan_exit()
{
if ( hashhost )
{
free(hashhost);
hashhost = 0LL;
}
}

canonical_decompiled_code:
void func4()
{
if ( hashhost )
{
free(hashhost);
hashhost = 0LL;
}
}

original_code:
void
scan_exit(void)
{
  if (hashhost) {
    free(hashhost);
    hashhost = ((void *)0);
  }
}

canonical_original_code:
void scan_exit()
{
  if (hashhost) {
    free(hashhost);
    hashhost = ((void *)0);
  }
}


decompiled_code:
// write access to const memory has been detected, the output may be wrong!
__int64 __fastcall detect_scan(__int64 a1)
{
__int64 result; // rax
__int64 v3; // r12
__int64 v4; // rdi
int v5; // eax
int v6; // ecx
__int64 v7; // r15
__int64 v8; // rsi
_QWORD *v9; // rbx
__int64 v10; // rdx
_QWORD *v11; // r14
int v12; // eax
int v13; // ebp
__int64 v14; // rdx
int v15; // eax
int v16; // er8
__int16 v17; // di
int v18; // edx
__int16 v19; // si
int v20; // er8
__int64 v21; // r9
__int64 v22; // rax
int v23; // er8
bool v24; // cc
unsigned int v25; // eax
_QWORD *v26; // rax
__int64 v27; // rcx
_QWORD *v28; // r8
_QWORD *v29; // rax
_QWORD *v30; // r15
char *v31; // [rsp+0h] [rbp-48h]
char *v32; // [rsp+8h] [rbp-40h]

result = (unsigned int)nids_params[11];
if ( (int)result <= 0 )
return result;
v3 = a1 + ((4 * *(_BYTE *)a1) & 0x3C);
v4 = *(unsigned int *)(a1 + 12);
v5 = scan_hash(v4);
timenow = 0;
v7 = 8LL * v5;
v8 = *(_QWORD *)((char *)hashhost + v7);
if ( v8 )
{
v9 = *(_QWORD **)((char *)hashhost + v7);
v10 = 0x7FFFFFFFLL;
v11 = 0LL;
v12 = 0;
while ( 1 )
{
v13 = *((_DWORD *)v9 + 5);
if ( v6 == *((_DWORD *)v9 + 4) )
goto LABEL_8;
if ( (int)v10 > v13 )
{
v10 = (unsigned int)v13;
v11 = v9;
}
v9 = (_QWORD *)*v9;
++v12;
if ( !v9 )
{
if ( v12 == 10 )
goto LABEL_21;
v32 = (char *)hashhost + v7;
v4 = 8LL * (int)nids_params[13] + 48;
v26 = malloc(v4);
v28 = v32;
v11 = v26;
if ( v26 )
{
v26[4] = v26 + 5;
goto LABEL_24;
}
goto LABEL_29;
}
}
}
v31 = (char *)hashhost + v7;
v4 = 8LL * (int)nids_params[13] + 48;
v29 = malloc(v4);
v28 = v31;
v11 = v29;
if ( v29 )
{
v29[4] = v29 + 5;
}
else
{
LABEL_29:
v4 = (__int64)"detect_scan";
v11 = 0LL;
(*(void (__fastcall **)(const char *, __int64, __int64, __int64, _QWORD *))&nids_params[14])(
"detect_scan",
v8,
v10,
v27,
v28);
v30 = (char *)hashhost + v7;
v8 = *v30;
v28 = v30;
*(_QWORD *)gettime = 40LL;
if ( v8 )
{
LABEL_24:
*(_QWORD *)(v8 + 8) = v11;
*v11 = *v28;
goto LABEL_25;
}
}
*v11 = 0LL;
LABEL_25:
v11[1] = 0LL;
v6 = *(_DWORD *)(a1 + 12);
*v28 = v11;
LABEL_21:
*((_DWORD *)v11 + 4) = v6;
v9 = v11;
v25 = gettime(v4, v8, v10);
*(_QWORD *)((char *)v11 + 20) = v25;
v13 = v25;
LABEL_8:
if ( (signed int)(v13 - gettime(v4, v8, v10)) > nids_params[12] )
*((_DWORD *)v9 + 6) = 0;
v15 = gettime(v4, v8, v14);
v16 = *((_DWORD *)v9 + 6);
v17 = *(_WORD *)(v3 + 2);
*((_DWORD *)v9 + 5) = v15;
if ( v16 <= 0 )
{
v19 = _bswap_16(v17);
LABEL_17:
v22 = v20;
v23 = v20 + 1;
result = v21 + 8 * v22;
*(_DWORD *)result = v18;
*(_WORD *)(result + 4) = v19;
*(_BYTE *)(result + 6) = *(_BYTE *)(v3 + 13);
v24 = v23 <= nids_params[13];
*((_DWORD *)v9 + 6) = v23;
if ( !v24 )
{
result = (*(__int64 (__fastcall **)(__int64, _QWORD, _QWORD, _QWORD *))&nids_params[8])(4LL, 0LL, 0LL, v9);
*((_DWORD *)v9 + 6) = 0;
}
}
else
{
v19 = _bswap_16(v17);
result = v21;
while ( *(_DWORD *)result != v18 || *(_WORD *)(result + 4) != v19 )
{
result += 8LL;
if ( result == v21 + 8LL * (unsigned int)(v20 - 1) + 8 )
goto LABEL_17;
}
}
return result;
}

original_code:
void
detect_scan(struct ip * iph)
{
  int i;
  struct tcphdr *th;
  int hash;
  struct host *this_host;
  struct host *oldest;
  int mtime = 2147483647;
  if (nids_params.scan_num_hosts <= 0)
    return;
  th = (struct tcphdr *) (((char *) iph) + 4 * iph->ip_hl);
  hash = scan_hash(iph->ip_src.s_addr);
  this_host = hashhost[hash];
  oldest = 0;
  timenow = 0;
  for (i = 0; this_host && this_host->addr != iph->ip_src.s_addr; i++) {
    if (this_host->modtime < mtime) {
      mtime = this_host->modtime;
      oldest = this_host;
    }
    this_host = this_host->next;
  }
  if (!this_host) {
    if (i == 10)
      this_host = oldest;
    else {
      this_host = (struct host *) malloc(sizeof(struct host) + (nids_params.scan_num_ports + 1) * sizeof(struct scan));
      if (!this_host)
 nids_params.no_mem("detect_scan");
      this_host->packets = (struct scan *) (((char *) this_host) + sizeof(struct host));
      if (hashhost[hash]) {
 hashhost[hash]->prev = this_host;
 this_host->next = hashhost[hash];
      }
      else
 this_host->next = 0;
      this_host->prev = 0;
      hashhost[hash] = this_host;
    }
    this_host->addr = iph->ip_src.s_addr;
    this_host->modtime = gettime();
    this_host->n_packets = 0;
  }
  if (this_host->modtime - gettime() > nids_params.scan_delay)
    this_host->n_packets = 0;
  this_host->modtime = gettime();
  for (i = 0; i < this_host->n_packets; i++)
    if (this_host->packets[i].addr == iph->ip_dst.s_addr &&
 this_host->packets[i].port == ntohs(th->th_dport))
      return;
  this_host->packets[this_host->n_packets].addr = iph->ip_dst.s_addr;
  this_host->packets[this_host->n_packets].port = ntohs(th->th_dport);
  this_host->packets[this_host->n_packets].flags = *((unsigned char *) (th) + 13);
  this_host->n_packets++;
  if (this_host->n_packets > nids_params.scan_num_ports) {
    nids_params.syslog(NIDS_WARN_SCAN, 0, 0, this_host);
    this_host->n_packets = 0;
  }
}

canonical_original_code:
void detect_scan(struct ip *iph)
{
  int i;
  struct tcphdr *th;
  int hash;
  struct host *this_host;
  struct host *oldest;
  int mtime = 2147483647;
  if (nids_params.scan_num_hosts <= 0)
    return;
  th = (struct tcphdr *) (((char *) iph) + 4 * iph->ip_hl);
  hash = scan_hash(iph->ip_src.s_addr);
  this_host = hashhost[hash];
  oldest = 0;
  timenow = 0;
  for (i = 0; this_host && this_host->addr != iph->ip_src.s_addr; i++) {
    if (this_host->modtime < mtime) {
      mtime = this_host->modtime;
      oldest = this_host;
    }
    this_host = this_host->next;
  }
  if (!this_host) {
    if (i == 10)
      this_host = oldest;
    else {
      this_host = (struct host *) malloc(sizeof(struct host) + (nids_params.scan_num_ports + 1) * sizeof(struct scan));
      if (!this_host)
 nids_params.no_mem("detect_scan");
      this_host->packets = (struct scan *) (((char *) this_host) + sizeof(struct host));
      if (hashhost[hash]) {
 hashhost[hash]->prev = this_host;
 this_host->next = hashhost[hash];
      }
      else
 this_host->next = 0;
      this_host->prev = 0;
      hashhost[hash] = this_host;
    }
    this_host->addr = iph->ip_src.s_addr;
    this_host->modtime = gettime();
    this_host->n_packets = 0;
  }
  if (this_host->modtime - gettime() > nids_params.scan_delay)
    this_host->n_packets = 0;
  this_host->modtime = gettime();
  for (i = 0; i < this_host->n_packets; i++)
    if (this_host->packets[i].addr == iph->ip_dst.s_addr &&
 this_host->packets[i].port == ntohs(th->th_dport))
      return;
  this_host->packets[this_host->n_packets].addr = iph->ip_dst.s_addr;
  this_host->packets[this_host->n_packets].port = ntohs(th->th_dport);
  this_host->packets[this_host->n_packets].flags = *((unsigned char *) (th) + 13);
  this_host->n_packets++;
  if (this_host->n_packets > nids_params.scan_num_ports) {
    nids_params.syslog(NIDS_WARN_SCAN, 0, 0, this_host);
    this_host->n_packets = 0;
  }
}

variable_types: struct ip { unsigned int ip_hl; unsigned int ip_v; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short ip_sum; struct in_addr { unsigned int s_addr; } ip_src; } *

variable_types: i int

variable_types: struct tcphdr { union { struct { unsigned short th_sport; unsigned short th_dport; unsigned int th_seq; unsigned int th_ack; unsigned char th_x2; unsigned char th_off; unsigned char th_flags; unsigned short th_win; unsigned short th_sum; unsigned short th_urp; }; struct { unsigned short source; unsigned short dest; unsigned int seq; unsigned int ack_seq; unsigned short res1; unsigned short doff; unsigned short fin; unsigned short syn; unsigned short rst; unsigned short psh; unsigned short ack; unsigned short urg; unsigned short res2; unsigned short window; unsigned short check; unsigned short urg_ptr; }; }; } *

variable_types: hash int

variable_types: struct host { struct host *next; struct host *prev; unsigned int addr; int modtime; int n_packets; struct scan { unsigned int addr; unsigned short port; unsigned char flags; } *packets; } *

variable_types: mtime int

user_defined_types: struct ip { unsigned int ip_hl; unsigned int ip_v; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short ip_sum; struct in_addr ip_src; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rax

v3 = (const char *)i3ipc_get_completions(0LL, argv, envp);
printf("completions: %s\n", v3);
return 0;
}

original_code:
int main(void) {
 char *comp;
 comp = i3ipc_get_completions(((void *)0));
 printf("completions: %s\n", comp);
 return 0;
}

canonical_original_code:
int main() {
 char *comp;
 comp = i3ipc_get_completions(((void *)0));
 printf("completions: %s\n", comp);
 return 0;
}

variable_types:
comp
char *


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int v4; // eax
const char *v5; // r13
int v6; // er12
socklen_t v7; // edx
int *v8; // r15
int *v9; // rsi
int v10; // edi
int v11; // edx
int *v12; // r13
int *v13; // r14
int v14; // eax
int v15; // eax
char *v17; // rax
size_t v18; // rax
int v19; // er13
int *v20; // rax
char *v21; // rax
size_t v22; // rax
__int64 v23; // [rsp+8h] [rbp-1CB0h]
__int64 v24; // [rsp+10h] [rbp-1CA8h]
int *v25; // [rsp+18h] [rbp-1CA0h]
int optval; // [rsp+20h] [rbp-1C98h] BYREF
int longind; // [rsp+24h] [rbp-1C94h] BYREF
time_t timer; // [rsp+28h] [rbp-1C90h] BYREF
struct sockaddr v29; // [rsp+30h] [rbp-1C88h] BYREF
struct option longopts; // [rsp+40h] [rbp-1C78h] BYREF
const char *v31; // [rsp+60h] [rbp-1C58h]
int v32; // [rsp+68h] [rbp-1C50h]
__int64 v33; // [rsp+70h] [rbp-1C48h]
int v34; // [rsp+78h] [rbp-1C40h]
fd_set readfds; // [rsp+80h] [rbp-1C38h] BYREF
char s[1040]; // [rsp+100h] [rbp-1BB8h] BYREF
char buf[6056]; // [rsp+510h] [rbp-17A8h] BYREF

v3 = 0;
optval = 5;
longopts.name = "port";
longopts.has_arg = 1;
longopts.flag = 0LL;
longopts.val = 112;
v31 = "tfo";
v32 = 0;
v33 = 0LL;
v34 = 102;
while ( 1 )
{
v4 = getopt_long(argc, (char *const *)argv, "p:f", &longopts, &longind);
if ( v4 == -1 )
{
if ( !v3 )
{
printf("Usage: %s -p <port> [-f]\n", *argv);
puts("\t-p port | --port=port\tThe port the server is listening on");
puts("\t-f | --tfo\t\tEnable TFO (disabled by default)");
return 1;
}
v6 = socket(2, 1, 0);
if ( v6 < 0 )
{
perror("socket");
return 1;
}
v29 = 0LL;
memset(s, 0, 0x400uLL);
s[1024] = 0;
setsockopt(v6, 6, 23, &optval, 4u);
v29.sa_family = 2;
*(_DWORD *)&v29.sa_data[2] = _bswap_32_constprop_0((unsigned __int16)v3, 6LL, 16LL);
*(_WORD *)v29.sa_data = _bswap_16((unsigned __int16)v3, &v29);
if ( bind(v6, &v29, v7) < 0 )
{
perror("bind");
return 1;
}
if ( listen(v6, 10) < 0 )
{
perror("listen");
return 1;
}
v8 = 0LL;
v23 = 1LL << v6;
v24 = v6 >> 6;
LABEL_11:
memset(&readfds, 0, sizeof(readfds));
v9 = v8;
v10 = v6;
readfds.fds_bits[v24] |= v23;
if ( v8 )
{
do
{
v11 = *v9;
v9 = (int *)*((_QWORD *)v9 + 1);
readfds.fds_bits[v11 / 64] |= 1LL << (v11 % 64);
if ( v10 < v11 )
v10 = v11;
}
while ( v9 );
}
while ( 1 )
{
if ( select(v10 + 1, &readfds, 0LL, 0LL, 0LL) < 0 )
{
perror("select");
return 1;
}
if ( (readfds.fds_bits[v24] & v23) != 0 )
break;
if ( v8 )
{
v25 = v8;
goto LABEL_19;
}
memset(&readfds, 0, sizeof(readfds));
v10 = v6;
readfds.fds_bits[v24] |= v23;
}
v19 = accept(v6, 0LL, 0LL);
if ( v19 < 0 )
{
perror("accept");
return 1;
}
v20 = (int *)malloc(0x10uLL);
*v20 = v19;
*((_QWORD *)v20 + 1) = v8;
v25 = v20;
timer = time(0LL);
v21 = ctime(&timer);
snprintf(s, 0x401uLL, "Hi there, the </head> time is %.24s\r\n", v21);
v22 = strlen(s);
if ( write(v19, s, v22) < 0 )
{
LABEL_32:
perror("write");
return 1;
}
LABEL_19:
v12 = v25;
v13 = 0LL;
v8 = (int *)*((_QWORD *)v25 + 1);
while ( 1 )
{
v14 = *v12 + 63;
if ( *v12 >= 0 )
v14 = *v12;
if ( (readfds.fds_bits[v14 >> 6] & (1LL << (*v12 % 64))) == 0 )
goto LABEL_20;
v15 = read(*v12, buf, 0x1770uLL);
if ( v15 < 0 )
{
if ( *__errno_location() != 104 )
{
perror("read");
return 1;
}
LABEL_27:
close(*v12);
if ( !v13 )
goto LABEL_11;
*((_QWORD *)v13 + 1) = v8;
v12 = v13;
}
else
{
if ( !v15 )
goto LABEL_27;
timer = time(0LL);
v17 = ctime(&timer);
snprintf(s, 0x401uLL, "Hi there, the </head> time is %.24s\r\n", v17);
v18 = strlen(s);
if ( write(*v12, s, v18) < 0 )
goto LABEL_32;
LABEL_20:
if ( !v8 )
{
v8 = v25;
goto LABEL_11;
}
v13 = v12;
v12 = v8;
v8 = (int *)*((_QWORD *)v8 + 1);
}
}
}
if ( v4 == 112 )
{
v5 = _bss_start;
v3 = atoi(_bss_start);
if ( v3 <= 0 )
break;
}
}
printf("Invalid port `%s'\n", v5);
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
int v4; // eax
const char *v5; // r13
int v6; // er12
socklen_t v7; // edx
int *v8; // r15
int *v9; // rsi
int v10; // edi
int v11; // edx
int *v12; // r13
int *v13; // r14
int v14; // eax
int v15; // eax
char *v17; // rax
size_t v18; // rax
int v19; // er13
int *v20; // rax
char *v21; // rax
size_t v22; // rax
__int64 v23; // [rsp+8h] [rbp-1CB0h]
__int64 v24; // [rsp+10h] [rbp-1CA8h]
int *v25; // [rsp+18h] [rbp-1CA0h]
int optval; // [rsp+20h] [rbp-1C98h] BYREF
int longind; // [rsp+24h] [rbp-1C94h] BYREF
time_t timer; // [rsp+28h] [rbp-1C90h] BYREF
struct sockaddr v29; // [rsp+30h] [rbp-1C88h] BYREF
struct option longopts; // [rsp+40h] [rbp-1C78h] BYREF
const char *v31; // [rsp+60h] [rbp-1C58h]
int v32; // [rsp+68h] [rbp-1C50h]
__int64 v33; // [rsp+70h] [rbp-1C48h]
int v34; // [rsp+78h] [rbp-1C40h]
fd_set readfds; // [rsp+80h] [rbp-1C38h] BYREF
char s[1040]; // [rsp+100h] [rbp-1BB8h] BYREF
char buf[6056]; // [rsp+510h] [rbp-17A8h] BYREF

v3 = 0;
optval = 5;
longopts.name = "port";
longopts.has_arg = 1;
longopts.flag = 0LL;
longopts.val = 112;
v31 = "tfo";
v32 = 0;
v33 = 0LL;
v34 = 102;
while ( 1 )
{
v4 = getopt_long(argc, (char *const *)argv, "p:f", &longopts, &longind);
if ( v4 == -1 )
{
if ( !v3 )
{
printf("Usage: %s -p <port> [-f]\n", *argv);
puts("\t-p port | --port=port\tThe port the server is listening on");
puts("\t-f | --tfo\t\tEnable TFO (disabled by default)");
return 1;
}
v6 = socket(2, 1, 0);
if ( v6 < 0 )
{
perror("socket");
return 1;
}
v29 = 0LL;
memset(s, 0, 0x400uLL);
s[1024] = 0;
setsockopt(v6, 6, 23, &optval, 4u);
v29.sa_family = 2;
*(_DWORD *)&v29.sa_data[2] = func8((unsigned __int16)v3, 6LL, 16LL);
*(_WORD *)v29.sa_data = func7((unsigned __int16)v3, &v29);
if ( bind(v6, &v29, v7) < 0 )
{
perror("bind");
return 1;
}
if ( listen(v6, 10) < 0 )
{
perror("listen");
return 1;
}
v8 = 0LL;
v23 = 1LL << v6;
v24 = v6 >> 6;
LABEL_11:
memset(&readfds, 0, sizeof(readfds));
v9 = v8;
v10 = v6;
readfds.fds_bits[v24] |= v23;
if ( v8 )
{
do
{
v11 = *v9;
v9 = (int *)*((_QWORD *)v9 + 1);
readfds.fds_bits[v11 / 64] |= 1LL << (v11 % 64);
if ( v10 < v11 )
v10 = v11;
}
while ( v9 );
}
while ( 1 )
{
if ( select(v10 + 1, &readfds, 0LL, 0LL, 0LL) < 0 )
{
perror("select");
return 1;
}
if ( (readfds.fds_bits[v24] & v23) != 0 )
break;
if ( v8 )
{
v25 = v8;
goto LABEL_19;
}
memset(&readfds, 0, sizeof(readfds));
v10 = v6;
readfds.fds_bits[v24] |= v23;
}
v19 = accept(v6, 0LL, 0LL);
if ( v19 < 0 )
{
perror("accept");
return 1;
}
v20 = (int *)malloc(0x10uLL);
*v20 = v19;
*((_QWORD *)v20 + 1) = v8;
v25 = v20;
timer = time(0LL);
v21 = ctime(&timer);
snprintf(s, 0x401uLL, "Hi there, the </head> time is %.24s\r\n", v21);
v22 = strlen(s);
if ( write(v19, s, v22) < 0 )
{
LABEL_32:
perror("write");
return 1;
}
LABEL_19:
v12 = v25;
v13 = 0LL;
v8 = (int *)*((_QWORD *)v25 + 1);
while ( 1 )
{
v14 = *v12 + 63;
if ( *v12 >= 0 )
v14 = *v12;
if ( (readfds.fds_bits[v14 >> 6] & (1LL << (*v12 % 64))) == 0 )
goto LABEL_20;
v15 = read(*v12, buf, 0x1770uLL);
if ( v15 < 0 )
{
if ( *__errno_location() != 104 )
{
perror("read");
return 1;
}
LABEL_27:
close(*v12);
if ( !v13 )
goto LABEL_11;
*((_QWORD *)v13 + 1) = v8;
v12 = v13;
}
else
{
if ( !v15 )
goto LABEL_27;
timer = time(0LL);
v17 = ctime(&timer);
snprintf(s, 0x401uLL, "Hi there, the </head> time is %.24s\r\n", v17);
v18 = strlen(s);
if ( write(*v12, s, v18) < 0 )
goto LABEL_32;
LABEL_20:
if ( !v8 )
{
v8 = v25;
goto LABEL_11;
}
v13 = v12;
v12 = v8;
v8 = (int *)*((_QWORD *)v8 + 1);
}
}
}
if ( v4 == 112 )
{
v5 = _bss_start;
v3 = atoi(_bss_start);
if ( v3 <= 0 )
break;
}
}
printf("Invalid port `%s'\n", v5);
return 1;
}

original_code:
int main(int argc, char *argv[])
{
    int listenfd, connfd, qlen = 5, num, port = 0, useTFO;
    struct sockaddr_in serv_addr;
    char sendBuff[1025];
    char readBuff[6000];
    time_t ticks;
    struct option options[] = {
        {"port", 1, ((void *)0), 'p'},
        {"tfo", 0, ((void *)0), 'f'}
    };
    int opt, longindex;
    fd_set selecting;
    int nfds = 0;
    struct conn *conns = ((void *)0), *newConn, *c, *prevC, *nextC;
    while ((opt = getopt_long(argc, argv, "p:f", options, &longindex)) != -1) {
        switch(opt) {
            case 'p':
                port = atoi(optarg);
                if (port <= 0) {
                    printf("Invalid port `%s'\n", optarg);
                    return 1;
                }
                break;
            case 'f':
                useTFO = 1;
                break;
        }
    }
    if (port == 0) {
        printf("Usage: %s -p <port> [-f]\n", argv[0]);
        printf("\t-p port | --port=port\tThe port the server is listening on\n");
        printf("\t-f | --tfo\t\tEnable TFO (disabled by default)\n");
        return 1;
    }
    listenfd = socket(2, SOCK_STREAM, 0);
    if (listenfd < 0) {
        perror("socket");
        return 1;
    }
    memset(&serv_addr, 0, sizeof(serv_addr));
    memset(sendBuff, 0, sizeof(sendBuff));
    if (useTFO) {
        setsockopt(listenfd, 6, 23, &qlen, sizeof(qlen));
    }
    serv_addr.sin_family = 2;
    serv_addr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
    serv_addr.sin_port = htons(port);
    if (bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
        perror("bind");
        return 1;
    }
    if (listen(listenfd, 10) < 0) {
        perror("listen");
        return 1;
    }
    while (1) {
        do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&selecting)->__fds_bits)[0]) : "memory"); } while (0);
        ((void) (((&selecting)->__fds_bits)[((listenfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((listenfd) % (8 * (int) sizeof (__fd_mask)))))));
        nfds = listenfd;
        for (c = conns; c; c = c->next) {
            ((void) (((&selecting)->__fds_bits)[((c->fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((c->fd) % (8 * (int) sizeof (__fd_mask)))))));
            nfds = c->fd > nfds ? c->fd : nfds;
        }
        nfds++;
        if (select(nfds, &selecting, ((void *)0), ((void *)0), ((void *)0)) < 0) {
            perror("select");
            return 1;
        }
        if (((((&selecting)->__fds_bits)[((listenfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((listenfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
            connfd = accept(listenfd, (struct sockaddr*)((void *)0), ((void *)0));
            if (connfd < 0) {
                perror("accept");
                return 1;
            }
            newConn = malloc(sizeof(struct conn));
            newConn->fd = connfd;
            newConn->next = conns;
            conns = newConn;
            ticks = time(((void *)0));
            snprintf(sendBuff, sizeof(sendBuff), "Hi there, the </head> time is %.24s\r\n", ctime(&ticks));
            if (write(connfd, sendBuff, strlen(sendBuff)) < 0) {
                perror("write");
                return 1;
            }
        }
        c = conns;
        prevC = ((void *)0);
        while (c) {
            nextC = c->next;
            if (((((&selecting)->__fds_bits)[((c->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((c->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
                num = read(c->fd, readBuff, sizeof(readBuff));
                if ((num < 0 && (*__errno_location ()) == 104) || num == 0) {
                    close(c->fd);
                    if (prevC) {
                        prevC->next = nextC;
                    } else {
                        conns = nextC;
                    }
                    c = prevC;
                    continue;
                } else if (num < 0) {
                    perror("read");
                    return 1;
                }
                if (num > 0) {
                    ticks = time(((void *)0));
                    snprintf(sendBuff, sizeof(sendBuff), "Hi there, the </head> time is %.24s\r\n", ctime(&ticks));
                    if (write(c->fd, sendBuff, strlen(sendBuff)) < 0) {
                        perror("write");
                        return 1;
                   }
                }
            }
            prevC = c;
            c = nextC;
        }
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int listenfd; int connfd; int qlen = 5; int num; int port = 0; int useTFO;
    struct sockaddr_in serv_addr;
    char sendBuff[1025];
    char readBuff[6000];
    long ticks;
    struct option options[] = {
        {"port", 1, ((void *)0), 'p'},
        {"tfo", 0, ((void *)0), 'f'}
    };
    int opt; int longindex;
    struct fd_set selecting;
    int nfds = 0;
    struct conn *conns = ((void *)0); struct conn *newConn; struct conn *c; struct conn *prevC; struct conn *nextC;
    while ((opt = getopt_long(argc, argv, "p:f", options, &longindex)) != -1) {
        switch(opt) {
            case 'p':
                port = atoi(optarg);
                if (port <= 0) {
                    printf("Invalid port `%s'\n", optarg);
                    return 1;
                }
                break;
            case 'f':
                useTFO = 1;
                break;
        }
    }
    if (port == 0) {
        printf("Usage: %s -p <port> [-f]\n", argv[0]);
        printf("\t-p port | --port=port\tThe port the server is listening on\n");
        printf("\t-f | --tfo\t\tEnable TFO (disabled by default)\n");
        return 1;
    }
    listenfd = socket(2, SOCK_STREAM, 0);
    if (listenfd < 0) {
        perror("socket");
        return 1;
    }
    memset(&serv_addr, 0, sizeof(serv_addr));
    memset(sendBuff, 0, sizeof(sendBuff));
    if (useTFO) {
        setsockopt(listenfd, 6, 23, &qlen, sizeof(qlen));
    }
    serv_addr.sin_family = 2;
    serv_addr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
    serv_addr.sin_port = htons(port);
    if (bind(listenfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        perror("bind");
        return 1;
    }
    if (listen(listenfd, 10) < 0) {
        perror("listen");
        return 1;
    }
    while (1) {
        do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&selecting)->__fds_bits)[0]) : "memory"); } while (0);
        ((void) (((&selecting)->__fds_bits)[((listenfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((listenfd) % (8 * (int) sizeof (__fd_mask)))))));
        nfds = listenfd;
        for (c = conns; c; c = c->next) {
            ((void) (((&selecting)->__fds_bits)[((c->fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((c->fd) % (8 * (int) sizeof (__fd_mask)))))));
            nfds = c->fd > nfds ? c->fd : nfds;
        }
        nfds++;
        if (select(nfds, &selecting, ((void *)0), ((void *)0), ((void *)0)) < 0) {
            perror("select");
            return 1;
        }
        if (((((&selecting)->__fds_bits)[((listenfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((listenfd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
            connfd = accept(listenfd, (struct sockaddr *)((void *)0), ((void *)0));
            if (connfd < 0) {
                perror("accept");
                return 1;
            }
            newConn = malloc(sizeof(struct conn));
            newConn->fd = connfd;
            newConn->next = conns;
            conns = newConn;
            ticks = time(((void *)0));
            snprintf(sendBuff, sizeof(sendBuff), "Hi there, the </head> time is %.24s\r\n", ctime(&ticks));
            if (write(connfd, sendBuff, strlen(sendBuff)) < 0) {
                perror("write");
                return 1;
            }
        }
        c = conns;
        prevC = ((void *)0);
        while (c) {
            nextC = c->next;
            if (((((&selecting)->__fds_bits)[((c->fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((c->fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
                num = read(c->fd, readBuff, sizeof(readBuff));
                if ((num < 0 && (*__errno_location ()) == 104) || num == 0) {
                    close(c->fd);
                    if (prevC) {
                        prevC->next = nextC;
                    } else {
                        conns = nextC;
                    }
                    c = prevC;
                    continue;
                } else if (num < 0) {
                    perror("read");
                    return 1;
                }
                if (num > 0) {
                    ticks = time(((void *)0));
                    snprintf(sendBuff, sizeof(sendBuff), "Hi there, the </head> time is %.24s\r\n", ctime(&ticks));
                    if (write(c->fd, sendBuff, strlen(sendBuff)) < 0) {
                        perror("write");
                        return 1;
                   }
                }
            }
            prevC = c;
            c = nextC;
        }
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: listenfd int

variable_types: connfd int

variable_types: qlen int

variable_types: num int

variable_types: port int

variable_types: useTFO 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:
sendBuff
char [1025]

variable_types:
readBuff
char [6000]

variable_types: ticks long

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []

variable_types: opt int

variable_types: longindex int

variable_types:
selecting
struct fd_set { long __fds_bits[-1]; }

variable_types: nfds int

variable_types: struct conn { int fd; struct conn *next; } *

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


xref

decompiled_code:
int __fastcall xref(char *s, int a2)
{
int i; // ebp
__int64 v3; // rbx
_DWORD *v4; // rbp
char *v5; // rax
__int64 v6; // rdi

pthread_mutex_lock(&xref_mtx);
if ( !objs )
objs = gdp_list_new();
for ( i = 0; ; ++i )
{
if ( (int)gdp_list_count() <= i )
{
v4 = malloc(0x10uLL);
v5 = strdup(s);
v4[2] = a2;
v6 = objs;
*(_QWORD *)v4 = v5;
gdp_list_add(v6, v4);
return pthread_mutex_unlock(&xref_mtx);
}
v3 = gdp_list_get(objs, (unsigned int)i);
if ( !strcmp(s, *(const char **)v3) )
break;
}
*(_DWORD *)(v3 + 8) += a2;
return pthread_mutex_unlock(&xref_mtx);
}

canonical_decompiled_code:
int __fastcall func0(char *s, int a2)
{
int i; // ebp
__int64 v3; // rbx
_DWORD *v4; // rbp
char *v5; // rax
__int64 v6; // rdi

pthread_mutex_lock(&xref_mtx);
if ( !objs )
objs = gdp_list_new();
for ( i = 0; ; ++i )
{
if ( (int)gdp_list_count() <= i )
{
v4 = malloc(0x10uLL);
v5 = strdup(s);
v4[2] = a2;
v6 = objs;
*(_QWORD *)v4 = v5;
gdp_list_add(v6, v4);
return pthread_mutex_unlock(&xref_mtx);
}
v3 = gdp_list_get(objs, (unsigned int)i);
if ( !strcmp(s, *(const char **)v3) )
break;
}
*(_DWORD *)(v3 + 8) += a2;
return pthread_mutex_unlock(&xref_mtx);
}

original_code:
void xref(char *name, int value) {
 pthread_mutex_lock(&xref_mtx);
 if (objs == ((void *)0))
  objs = gdp_list_new();
 int i;
 int found = 0;
 struct ref *r = ((void *)0);
 for (i = 0; i < gdp_list_count(objs); ++i)
 {
  r = (struct ref *)gdp_list_get(objs, i);
  if (strcmp(name, r->name) == 0) {
   r->count += value;
   found = 1;
   break;
  }
 }
 if (!found) {
  r = (struct ref *)malloc(sizeof(struct ref));
  r->name = strdup(name);
  r->count = value;
  gdp_list_add(objs , r);
 }
 pthread_mutex_unlock(&xref_mtx);
}

variable_types:
name
char *

variable_types: value int

variable_types: i int

variable_types: found int

variable_types:
r
struct ref { char *name; int count; } *


incref

decompiled_code:
int __fastcall incref(char *a1)
{
return xref(a1, 1);
}

canonical_decompiled_code:
int __fastcall func1(char *a1)
{
return func0(a1, 1);
}

original_code:
void incref(char *name) {
 xref(name, 1);
}


decref

decompiled_code:
int __fastcall decref(char *a1)
{
return xref(a1, -1);
}

canonical_decompiled_code:
int __fastcall func2(char *a1)
{
return func0(a1, -1);
}

original_code:
void decref(char *name) {
 xref(name, -1);
}


dumpobjs

decompiled_code:
int dumpobjs()
{
int i; // ebx
__int64 v1; // rsi
__int64 v2; // rax

pthread_mutex_lock(&xref_mtx);
if ( !objs )
objs = gdp_list_new();
for ( i = 0; (int)gdp_list_count() > i; ++i )
{
v1 = (unsigned int)i;
v2 = gdp_list_get(objs, v1);
printf("%s: %d\n", *(const char **)v2, *(unsigned int *)(v2 + 8));
}
return pthread_mutex_unlock(&xref_mtx);
}

canonical_decompiled_code:
int func3()
{
int i; // ebx
__int64 v1; // rsi
__int64 v2; // rax

pthread_mutex_lock(&xref_mtx);
if ( !objs )
objs = gdp_list_new();
for ( i = 0; (int)gdp_list_count() > i; ++i )
{
v1 = (unsigned int)i;
v2 = gdp_list_get(objs, v1);
printf("%s: %d\n", *(const char **)v2, *(unsigned int *)(v2 + 8));
}
return pthread_mutex_unlock(&xref_mtx);
}

original_code:
void dumpobjs() {
 pthread_mutex_lock(&xref_mtx);
 if (objs == ((void *)0))
  objs = gdp_list_new();
 int i;
 for (i = 0; i < gdp_list_count(objs); ++i)
 {
  struct ref *r = (struct ref *)gdp_list_get(objs, i);
  printf("%s: %d\n", r->name, r->count);
 }
 pthread_mutex_unlock(&xref_mtx);
}


xcalloc

decompiled_code:
void *__fastcall xcalloc(size_t a1, size_t a2)
{
void *result; // rax

result = calloc(a1, a2);
if ( !result )
{
fwrite("virtual memory exhausted.\n", 1uLL, 0x1AuLL, stderr);
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func4(size_t a1, size_t a2)
{
void *result; // rax

result = calloc(a1, a2);
if ( !result )
{
fwrite("virtual memory exhausted.\n", 1uLL, 0x1AuLL, stderr);
exit(1);
}
return result;
}

original_code:
void* xcalloc(size_t num, size_t size) {
 register void *value = calloc(num, size);
 if (value == 0) {
  fputs("virtual memory exhausted.\n", stderr);
  exit(1);
 }
 return value;
}

canonical_original_code:
void *xcalloc(unsigned long num, unsigned long size) {
 register void *value = calloc(num, size);
 if (value == 0) {
  fputs("virtual memory exhausted.\n", stderr);
  exit(1);
 }
 return value;
}

variable_types:
num
unsigned long

variable_types:
size
unsigned long

variable_types:
value
void *

return_type: void *


xmalloc

decompiled_code:
void *__fastcall xmalloc(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
{
fwrite("virtual memory exhausted.\n", 1uLL, 0x1AuLL, stderr);
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func5(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
{
fwrite("virtual memory exhausted.\n", 1uLL, 0x1AuLL, stderr);
exit(1);
}
return result;
}

original_code:
void* xmalloc(size_t size) {
 register void *value = malloc(size);
 if (value == 0) {
  fputs("virtual memory exhausted.\n", stderr);
  exit(1);
 }
 return value;
}

canonical_original_code:
void *xmalloc(unsigned long size) {
 register void *value = malloc(size);
 if (value == 0) {
  fputs("virtual memory exhausted.\n", stderr);
  exit(1);
 }
 return value;
}


decompiled_code:
void *__fastcall xmalloc0(size_t n)
{
void *v1; // rax

v1 = xmalloc(n);
return memset(v1, 0, n);
}

canonical_decompiled_code:
void *__fastcall func6(size_t n)
{
void *v1; // rax

v1 = func5(n);
return memset(v1, 0, n);
}

original_code:
void* xmalloc0(size_t size) {
 register void *value = xmalloc(size);
 memset(value, 0, size);
 return value;
}

canonical_original_code:
void *xmalloc0(unsigned long size) {
 register void *value = xmalloc(size);
 memset(value, 0, size);
 return value;
}


decompiled_code:
void *__fastcall xmalloc_dbg(size_t a1, char *a2)
{
incref(a2);
return xmalloc(a1);
}

canonical_decompiled_code:
void *__fastcall func7(size_t a1, char *a2)
{
func1(a2);
return func5(a1);
}

original_code:
void* xmalloc_dbg(size_t size, char *name) {
 incref(name);
 return xmalloc(size);
}

canonical_original_code:
void *xmalloc_dbg(unsigned long size, char *name) {
 incref(name);
 return xmalloc(size);
}


decompiled_code:
void *__fastcall xmalloc0_dbg(size_t n, char *a2)
{
incref(a2);
return xmalloc0(n);
}

canonical_decompiled_code:
void *__fastcall func8(size_t n, char *a2)
{
func1(a2);
return func6(n);
}

original_code:
void* xmalloc0_dbg(size_t size, char *name) {
 incref(name);
 return xmalloc0(size);
}

canonical_original_code:
void *xmalloc0_dbg(unsigned long size, char *name) {
 incref(name);
 return xmalloc0(size);
}


decompiled_code:
void __fastcall free_dbg(void *ptr, char *a2)
{
decref(a2);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func9(void *ptr, char *a2)
{
func2(a2);
free(ptr);
}

original_code:
void free_dbg(void* value, char *name) {
 decref(name);
 free(value);
}

canonical_original_code:
void free_dbg(void *value, char *name) {
 decref(name);
 free(value);
}


decompiled_code:
void gdp_io_thread_assure_wsa_once(void)
{
;
}

canonical_decompiled_code:
void func0(void)
{
;
}

original_code:
void gdp_io_thread_assure_wsa_once() {
}


decompiled_code:
__int64 __fastcall gdp_io_thread_getsockets(void *a1)
{
signed int v1; // er12
int i; // er13
int v3; // edx
int j; // er13
int v5; // edx

memset(a1, 0, 0x80uLL);
v1 = 0;
for ( i = 0; (int)gdp_list_count(*(_QWORD *)(instance + 208)) > i; ++i )
{
v3 = *(_DWORD *)gdp_list_get(*(_QWORD *)(instance + 208), (unsigned int)i);
*((_QWORD *)a1 + v3 / 64) |= 1LL << (v3 % 64);
if ( v1 < v3 )
v1 = v3;
}
for ( j = 0; (int)gdp_list_count(*(_QWORD *)(instance + 216)) > j; ++j )
{
v5 = *(_DWORD *)(gdp_list_get(*(_QWORD *)(instance + 216), (unsigned int)j) + 8);
*((_QWORD *)a1 + v5 / 64) |= 1LL << (v5 % 64);
if ( v1 < v5 )
v1 = v5;
}
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *a1)
{
signed int v1; // er12
int i; // er13
int v3; // edx
int j; // er13
int v5; // edx

memset(a1, 0, 0x80uLL);
v1 = 0;
for ( i = 0; (int)gdp_list_count(*(_QWORD *)(instance + 208)) > i; ++i )
{
v3 = *(_DWORD *)gdp_list_get(*(_QWORD *)(instance + 208), (unsigned int)i);
*((_QWORD *)a1 + v3 / 64) |= 1LL << (v3 % 64);
if ( v1 < v3 )
v1 = v3;
}
for ( j = 0; (int)gdp_list_count(*(_QWORD *)(instance + 216)) > j; ++j )
{
v5 = *(_DWORD *)(gdp_list_get(*(_QWORD *)(instance + 216), (unsigned int)j) + 8);
*((_QWORD *)a1 + v5 / 64) |= 1LL << (v5 % 64);
if ( v1 < v5 )
v1 = v5;
}
return (unsigned int)v1;
}

original_code:
int gdp_io_thread_getsockets(fd_set* set) {
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((set)->__fds_bits)[0]) : "memory"); } while (0);
 int i, fdmax = 0;
 for (i = 0; i < gdp_list_count(instance->servers); i++) {
  gdp_server_t* server = (gdp_server_t*)gdp_list_get(instance->servers, i);
  ((void) (((set)->__fds_bits)[((server->sock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((server->sock) % (8 * (int) sizeof (__fd_mask)))))));
  if (fdmax < server->sock)
   fdmax = server->sock;
 }
 for (i = 0; i < gdp_list_count(instance->clients); i++) {
  gdp_client_t* client = (gdp_client_t*)gdp_list_get(instance->clients, i);
  ((void) (((set)->__fds_bits)[((client->sock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((client->sock) % (8 * (int) sizeof (__fd_mask)))))));
  if (fdmax < client->sock)
   fdmax = client->sock;
 }
 return fdmax;
}

canonical_original_code:
int gdp_io_thread_getsockets(struct fd_set *set) {
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((set)->__fds_bits)[0]) : "memory"); } while (0);
 int i; int fdmax = 0;
 for (i = 0; i < gdp_list_count(instance->servers); i++) {
  struct gdp_server *server = (gdp_server_t*)gdp_list_get(instance->servers, i);
  ((void) (((set)->__fds_bits)[((server->sock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((server->sock) % (8 * (int) sizeof (__fd_mask)))))));
  if (fdmax < server->sock)
   fdmax = server->sock;
 }
 for (i = 0; i < gdp_list_count(instance->clients); i++) {
  struct gdp_client *client = (gdp_client_t*)gdp_list_get(instance->clients, i);
  ((void) (((set)->__fds_bits)[((client->sock) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((client->sock) % (8 * (int) sizeof (__fd_mask)))))));
  if (fdmax < client->sock)
   fdmax = client->sock;
 }
 return fdmax;
}

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

variable_types: fdmax int

variable_types: struct gdp_server { int sock; struct gdp_list { int count; int available; void **items; 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; } lock_obj; } *_onopenclose_callbacks; struct gdp_list *clients; } *

variable_types: struct gdp_client { char objsign[7]; int sock; struct gdp_addr { char ip[15]; int port; } addr; int _closed; int state; struct gdp_stream_writer { char *buffer; int maxsize; int size; } *_prebuffer; struct gdp_packet { char objsign[7]; struct gdp_client *client; char *buffer; int size; int current_size; } *_prepack; struct gdp_list { int count; int available; void **items; 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; } lock_obj; } *_onclose_callbacks; } *

user_defined_types: struct gdp_server { int sock; struct gdp_list *_onopenclose_callbacks; struct gdp_list *clients; }

user_defined_types: struct gdp_client { char objsign[7]; int sock; struct gdp_addr addr; int _closed; int state; struct gdp_stream_writer *_prebuffer; struct gdp_packet *_prepack; struct gdp_list *_onclose_callbacks; }

user_defined_types: struct gdp_list { int count; int available; void **items; union pthread_mutex_t lock_obj; }

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:
void __noreturn gdp_io_thread_close()
{
*(_DWORD *)(instance + 224) = 0;
exit(1);
}

canonical_decompiled_code:
void __noreturn func2()
{
*(_DWORD *)(instance + 224) = 0;
exit(1);
}

original_code:
void gdp_io_thread_close(void) {
 instance->_running = 0;
 exit(1);
 int i;
 for (i = 0; i < gdp_list_count(instance->servers); i++) {
  gdp_server_close(gdp_list_get(instance->servers, i));
 }
 gdp_list_free(instance->servers);
 instance->servers = ((void *)0);
 for (i = 0; i < gdp_list_count(instance->clients); i++) {
  gdp_client_close(gdp_list_get(instance->clients, i));
 }
 gdp_list_free(instance->clients);
 instance->clients = ((void *)0);
 pthread_once_t reset = 0;
 memcpy(&instance_once_control, &reset, sizeof(reset));
}

canonical_original_code:
void gdp_io_thread_close() {
 instance->_running = 0;
 exit(1);
 int i;
 for (i = 0; i < gdp_list_count(instance->servers); i++) {
  gdp_server_close(gdp_list_get(instance->servers, i));
 }
 gdp_list_free(instance->servers);
 instance->servers = ((void *)0);
 for (i = 0; i < gdp_list_count(instance->clients); i++) {
  gdp_client_close(gdp_list_get(instance->clients, i));
 }
 gdp_list_free(instance->clients);
 instance->clients = ((void *)0);
 int reset = 0;
 memcpy(&instance_once_control, &reset, sizeof(reset));
}

variable_types: reset int


decompiled_code:
__int64 gdp_io_thread_dequeue_recv()
{
__int64 v0; // r12

pthread_mutex_lock((pthread_mutex_t *)(instance + 104));
while ( 1 )
{
v0 = gdp_queue_deq(*(_QWORD *)(instance + 200));
if ( v0 )
break;
pthread_cond_wait((pthread_cond_t *)(instance + 144), (pthread_mutex_t *)(instance + 104));
}
pthread_mutex_unlock((pthread_mutex_t *)(instance + 104));
return v0;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 v0; // r12

pthread_mutex_lock((pthread_mutex_t *)(instance + 104));
while ( 1 )
{
v0 = gdp_queue_deq(*(_QWORD *)(instance + 200));
if ( v0 )
break;
pthread_cond_wait((pthread_cond_t *)(instance + 144), (pthread_mutex_t *)(instance + 104));
}
pthread_mutex_unlock((pthread_mutex_t *)(instance + 104));
return v0;
}

original_code:
gdp_packet_t* gdp_io_thread_dequeue_recv(void) {
 pthread_mutex_lock(&instance->recv_mutex);
 gdp_packet_t* packet = ((void *)0);
 while ((packet = (gdp_packet_t*)gdp_queue_deq(instance->_queue_recv)) == ((void *)0)) {
  pthread_cond_wait(&instance->recv_cond, &instance->recv_mutex);
 }
 pthread_mutex_unlock(&instance->recv_mutex);
 return packet;
}

canonical_original_code:
struct gdp_packet *gdp_io_thread_dequeue_recv() {
 pthread_mutex_lock(&instance->recv_mutex);
 struct gdp_packet *packet = ((void *)0);
 while ((packet = (struct gdp_packet *)gdp_queue_deq(instance->_queue_recv)) == ((void *)0)) {
  pthread_cond_wait(&instance->recv_cond, &instance->recv_mutex);
 }
 pthread_mutex_unlock(&instance->recv_mutex);
 return packet;
}

variable_types: struct gdp_packet { char objsign[7]; struct gdp_client { char objsign[7]; int sock; struct gdp_addr { char ip[15]; int port; } addr; int _closed; int state; struct gdp_stream_writer { char *buffer; int maxsize; int size; } *_prebuffer; struct gdp_packet *_prepack; struct gdp_list { int count; int available; void **items; 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; } lock_obj; } *_onclose_callbacks; } *client; char *buffer; int size; int current_size; } *

user_defined_types: struct gdp_packet { char objsign[7]; struct gdp_client *client; char *buffer; int size; int current_size; }


decompiled_code:
int __fastcall gdp_io_thread_enqueue_send(__int64 a1)
{
gdp_queue_enq(*(_QWORD *)(instance + 192), a1);
pthread_mutex_lock((pthread_mutex_t *)(instance + 16));
pthread_cond_broadcast((pthread_cond_t *)(instance + 56));
return pthread_mutex_unlock((pthread_mutex_t *)(instance + 16));
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1)
{
gdp_queue_enq(*(_QWORD *)(instance + 192), a1);
pthread_mutex_lock((pthread_mutex_t *)(instance + 16));
pthread_cond_broadcast((pthread_cond_t *)(instance + 56));
return pthread_mutex_unlock((pthread_mutex_t *)(instance + 16));
}

original_code:
void gdp_io_thread_enqueue_send(gdp_packet_t* pack) {
 gdp_queue_enq(instance->_queue_send, pack);
 pthread_mutex_lock(&instance->send_mutex);
 pthread_cond_broadcast(&instance->send_cond);
 pthread_mutex_unlock(&instance->send_mutex);
}

canonical_original_code:
void gdp_io_thread_enqueue_send(struct gdp_packet *pack) {
 gdp_queue_enq(instance->_queue_send, pack);
 pthread_mutex_lock(&instance->send_mutex);
 pthread_cond_broadcast(&instance->send_cond);
 pthread_mutex_unlock(&instance->send_mutex);
}

user_defined_types: struct gdp_addr { char ip[15]; int port; }

user_defined_types: struct gdp_stream_writer { char *buffer; int maxsize; int size; }

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


decompiled_code:
int __fastcall gdp_io_thread_enqueue_recv(__int64 a1)
{
gdp_queue_enq(*(_QWORD *)(instance + 200), a1);
pthread_mutex_lock((pthread_mutex_t *)(instance + 104));
pthread_cond_broadcast((pthread_cond_t *)(instance + 144));
return pthread_mutex_unlock((pthread_mutex_t *)(instance + 104));
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1)
{
gdp_queue_enq(*(_QWORD *)(instance + 200), a1);
pthread_mutex_lock((pthread_mutex_t *)(instance + 104));
pthread_cond_broadcast((pthread_cond_t *)(instance + 144));
return pthread_mutex_unlock((pthread_mutex_t *)(instance + 104));
}

original_code:
void gdp_io_thread_enqueue_recv(gdp_packet_t* pack) {
 gdp_queue_enq(instance->_queue_recv, pack);
 pthread_mutex_lock(&instance->recv_mutex);
 pthread_cond_broadcast(&instance->recv_cond);
 pthread_mutex_unlock(&instance->recv_mutex);
}

canonical_original_code:
void gdp_io_thread_enqueue_recv(struct gdp_packet *pack) {
 gdp_queue_enq(instance->_queue_recv, pack);
 pthread_mutex_lock(&instance->recv_mutex);
 pthread_cond_broadcast(&instance->recv_cond);
 pthread_mutex_unlock(&instance->recv_mutex);
}


decompiled_code:
__int64 __fastcall gdp_io_thread_remove_server(__int64 a1)
{
int v1; // ebx
__int64 result; // rax

gdp_list_remove_item(*(_QWORD *)(instance + 208), a1);
v1 = gdp_list_count(*(_QWORD *)(instance + 208));
result = gdp_list_count(*(_QWORD *)(instance + 216));
if ( !((_DWORD)result + v1) )
gdp_io_thread_close();
return result;
}

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

gdp_list_remove_item(*(_QWORD *)(instance + 208), a1);
v1 = gdp_list_count(*(_QWORD *)(instance + 208));
result = gdp_list_count(*(_QWORD *)(instance + 216));
if ( !((_DWORD)result + v1) )
func2();
return result;
}

original_code:
void gdp_io_thread_remove_server(gdp_server_t *server) {
 gdp_list_remove_item(instance->servers, server);
 if (gdp_list_count(instance->servers) + gdp_list_count(instance->clients) == 0)
  gdp_io_thread_close();
}

canonical_original_code:
void gdp_io_thread_remove_server(struct gdp_server *server) {
 gdp_list_remove_item(instance->servers, server);
 if (gdp_list_count(instance->servers) + gdp_list_count(instance->clients) == 0)
  gdp_io_thread_close();
}


decompiled_code:
__int64 __fastcall gdp_io_thread_remove_client(__int64 a1)
{
int v1; // ebx
__int64 result; // rax

gdp_list_remove_item(*(_QWORD *)(instance + 216), a1);
v1 = gdp_list_count(*(_QWORD *)(instance + 208));
result = gdp_list_count(*(_QWORD *)(instance + 216));
if ( !((_DWORD)result + v1) )
gdp_io_thread_close();
return result;
}

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

gdp_list_remove_item(*(_QWORD *)(instance + 216), a1);
v1 = gdp_list_count(*(_QWORD *)(instance + 208));
result = gdp_list_count(*(_QWORD *)(instance + 216));
if ( !((_DWORD)result + v1) )
func2();
return result;
}

original_code:
void gdp_io_thread_remove_client(gdp_client_t *client) {
 gdp_list_remove_item(instance->clients, client);
 if (gdp_list_count(instance->servers) + gdp_list_count(instance->clients) == 0)
  gdp_io_thread_close();
}

canonical_original_code:
void gdp_io_thread_remove_client(struct gdp_client *client) {
 gdp_list_remove_item(instance->clients, client);
 if (gdp_list_count(instance->servers) + gdp_list_count(instance->clients) == 0)
  gdp_io_thread_close();
}


decompiled_code:
__int64 __fastcall gdp_io_thread_add_server(__int64 a1)
{
return gdp_list_add(*(_QWORD *)(instance + 208), a1);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
return gdp_list_add(*(_QWORD *)(instance + 208), a1);
}

original_code:
void gdp_io_thread_add_server(gdp_server_t* server) {
 gdp_list_add(instance->servers, server);
}

canonical_original_code:
void gdp_io_thread_add_server(struct gdp_server *server) {
 gdp_list_add(instance->servers, server);
}


decompiled_code:
__int64 __fastcall gdp_io_thread_add_client(__int64 a1)
{
return gdp_list_add(*(_QWORD *)(instance + 216), a1);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
return gdp_list_add(*(_QWORD *)(instance + 216), a1);
}

original_code:
void gdp_io_thread_add_client(gdp_client_t* client) {
 gdp_list_add(instance->clients, client);
}

canonical_original_code:
void gdp_io_thread_add_client(struct gdp_client *client) {
 gdp_list_add(instance->clients, client);
}


decompiled_code:
__int64 __fastcall gdp_io_thread_internal_send(int fd, __int64 a2, int a3)
{
unsigned int v4; // er12
int v5; // eax

if ( a3 > 0 )
{
v4 = 0;
while ( 1 )
{
v5 = send(fd, (const void *)(a2 + (int)v4), (int)(a3 - v4), 0);
if ( !v5 )
break;
if ( v5 <= 0 )
{
printf("Send retornou menos que 0 = %d. SOCKET fechado.\n", (unsigned int)v5);
return 0;
}
v4 += v5;
if ( a3 <= (int)v4 )
return v4;
}
}
return 0;
}

canonical_decompiled_code:
__int64 __fastcall func10(int fd, __int64 a2, int a3)
{
unsigned int v4; // er12
int v5; // eax

if ( a3 > 0 )
{
v4 = 0;
while ( 1 )
{
v5 = send(fd, (const void *)(a2 + (int)v4), (int)(a3 - v4), 0);
if ( !v5 )
break;
if ( v5 <= 0 )
{
printf("Send retornou menos que 0 = %d. SOCKET fechado.\n", (unsigned int)v5);
return 0;
}
v4 += v5;
if ( a3 <= (int)v4 )
return v4;
}
}
return 0;
}

original_code:
int gdp_io_thread_internal_send(int sock, const char *data, int length) {
 int sent = 0;
 while (sent < length) {
  int sent_now = send(sock, data + sent, length - sent, 0);
  if (sent_now == 0) {
   return 0;
  } else if (sent_now > 0) {
   sent += sent_now;
  } else {
   printf("Send retornou menos que 0 = %d. SOCKET fechado.\n", sent_now);
   return 0;
  }
 }
 return sent;
}

canonical_original_code:
int gdp_io_thread_internal_send(int sock, char *data, int length) {
 int sent = 0;
 while (sent < length) {
  int sent_now = send(sock, data + sent, length - sent, 0);
  if (sent_now == 0) {
   return 0;
  } else if (sent_now > 0) {
   sent += sent_now;
  } else {
   printf("Send retornou menos que 0 = %d. SOCKET fechado.\n", sent_now);
   return 0;
  }
 }
 return sent;
}

variable_types: sock int

variable_types:
data
char *

variable_types: length int

variable_types: sent int


decompiled_code:
int gdp_io_thread_process_recv()
{
int v0; // eax
int v1; // ebx
int result; // eax
int *v3; // rax
int v4; // ecx
int v5; // eax
int i; // ebx
__int64 v7; // rax
__int64 v8; // r12
fd_set vars0; // [rsp+0h] [rbp+0h] BYREF

memset(&vars0, 0, sizeof(vars0));
v0 = gdp_io_thread_getsockets(&vars0);
if ( !v0 )
return printf("fdmax: %d\n", 0LL);
v1 = 0;
result = select(v0 + 1, &vars0, 0LL, 0LL, 0LL);
if ( result > 0 )
{
while ( (int)gdp_list_count(*(_QWORD *)(instance + 208)) > v1 )
{
v3 = (int *)gdp_list_get(*(_QWORD *)(instance + 208), (unsigned int)v1);
if ( v3 )
{
v4 = *v3;
v5 = *v3 + 63;
if ( v4 >= 0 )
v5 = v4;
if ( (vars0.fds_bits[v5 >> 6] & (1LL << (v4 % 64))) != 0 )
gdp_server_accept();
}
++v1;
}
for ( i = 0; ; ++i )
{
result = gdp_list_count(*(_QWORD *)(instance + 216));
if ( result <= i )
break;
v7 = gdp_list_get(*(_QWORD *)(instance + 216), (unsigned int)i);
v8 = v7;
if ( v7
&& (vars0.fds_bits[*(_DWORD *)(v7 + 8) / 64] & (1LL << (*(_DWORD *)(v7 + 8) % 64))) != 0
&& !(unsigned int)gdp_client_isclosed(v7) )
{
gdp_client_internal_recv(v8);
}
}
}
return result;
}

canonical_decompiled_code:
int func11()
{
int v0; // eax
int v1; // ebx
int result; // eax
int *v3; // rax
int v4; // ecx
int v5; // eax
int i; // ebx
__int64 v7; // rax
__int64 v8; // r12
fd_set vars0; // [rsp+0h] [rbp+0h] BYREF

memset(&vars0, 0, sizeof(vars0));
v0 = func1(&vars0);
if ( !v0 )
return printf("fdmax: %d\n", 0LL);
v1 = 0;
result = select(v0 + 1, &vars0, 0LL, 0LL, 0LL);
if ( result > 0 )
{
while ( (int)gdp_list_count(*(_QWORD *)(instance + 208)) > v1 )
{
v3 = (int *)gdp_list_get(*(_QWORD *)(instance + 208), (unsigned int)v1);
if ( v3 )
{
v4 = *v3;
v5 = *v3 + 63;
if ( v4 >= 0 )
v5 = v4;
if ( (vars0.fds_bits[v5 >> 6] & (1LL << (v4 % 64))) != 0 )
gdp_server_accept();
}
++v1;
}
for ( i = 0; ; ++i )
{
result = gdp_list_count(*(_QWORD *)(instance + 216));
if ( result <= i )
break;
v7 = gdp_list_get(*(_QWORD *)(instance + 216), (unsigned int)i);
v8 = v7;
if ( v7
&& (vars0.fds_bits[*(_DWORD *)(v7 + 8) / 64] & (1LL << (*(_DWORD *)(v7 + 8) % 64))) != 0
&& !(unsigned int)gdp_client_isclosed(v7) )
{
gdp_client_internal_recv(v8);
}
}
}
return result;
}

original_code:
void gdp_io_thread_process_recv(void) {
 fd_set sockets;
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&sockets)->__fds_bits)[0]) : "memory"); } while (0);
 int fdmax = gdp_io_thread_getsockets(&sockets);
 if (fdmax == 0) {
  printf("fdmax: %d\n", fdmax);
  return;
 }
 if (select(fdmax+1, &sockets, ((void *)0), ((void *)0), ((void *)0)) > 0)
 {
  int i;
  for (i = 0; i < gdp_list_count(instance->servers); i++) {
   gdp_server_t* server = (gdp_server_t*)gdp_list_get(instance->servers, i);
   if (server && ((((&sockets)->__fds_bits)[((server->sock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((server->sock) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    gdp_server_accept(server);
   }
  }
  for (i = 0; i < gdp_list_count(instance->clients); i++) {
   gdp_client_t* client = (gdp_client_t*)gdp_list_get(instance->clients, i);
   if (client && ((((&sockets)->__fds_bits)[((client->sock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((client->sock) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    if (!gdp_client_isclosed(client)) {
     gdp_client_internal_recv(client);
    }
   }
  }
 }
}

canonical_original_code:
void gdp_io_thread_process_recv() {
 struct fd_set sockets;
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&sockets)->__fds_bits)[0]) : "memory"); } while (0);
 int fdmax = gdp_io_thread_getsockets(&sockets);
 if (fdmax == 0) {
  printf("fdmax: %d\n", fdmax);
  return;
 }
 if (select(fdmax+1, &sockets, ((void *)0), ((void *)0), ((void *)0)) > 0)
 {
  int i;
  for (i = 0; i < gdp_list_count(instance->servers); i++) {
   struct gdp_server *server = (gdp_server_t*)gdp_list_get(instance->servers, i);
   if (server && ((((&sockets)->__fds_bits)[((server->sock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((server->sock) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    gdp_server_accept(server);
   }
  }
  for (i = 0; i < gdp_list_count(instance->clients); i++) {
   struct gdp_client *client = (gdp_client_t*)gdp_list_get(instance->clients, i);
   if (client && ((((&sockets)->__fds_bits)[((client->sock) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((client->sock) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    if (!gdp_client_isclosed(client)) {
     gdp_client_internal_recv(client);
    }
   }
  }
 }
}

variable_types:
sockets
struct fd_set { long __fds_bits[-1]; }


decompiled_code:
void *__fastcall gdp_io_thread_run_recv(void *a1)
{
if ( !*(_DWORD *)(instance + 224) )
return 0LL;
do
gdp_io_thread_process_recv();
while ( *(_DWORD *)(instance + 224) );
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func12(void *a1)
{
if ( !*(_DWORD *)(instance + 224) )
return 0LL;
do
func11();
while ( *(_DWORD *)(instance + 224) );
return 0LL;
}

original_code:
void* gdp_io_thread_run_recv(void* state) {
 while (instance->_running) {
  gdp_io_thread_process_recv();
 }
 return ((void *)0);
}

canonical_original_code:
void *gdp_io_thread_run_recv(void *state) {
 while (instance->_running) {
  gdp_io_thread_process_recv();
 }
 return ((void *)0);
}

variable_types:
state
void *


decompiled_code:
__int64 __fastcall gdp_io_thread_process_send(__int64 a1)
{
__int64 v1; // r12
int v2; // er13
__int64 v4; // rsi
int v5; // [rsp+8h] [rbp-20h] BYREF
int v6; // [rsp+Ch] [rbp-1Ch]

v1 = *(_QWORD *)(a1 + 8);
if ( (int)gdp_list_indexof(*(_QWORD *)(instance + 216), v1) >= 0 )
{
v5 = 5260359;
v6 = gdp_packet_size(a1);
v2 = v6;
if ( !(unsigned int)gdp_client_isclosed(v1)
&& !(unsigned int)gdp_io_thread_internal_send(*(_DWORD *)(v1 + 8), (__int64)&v5, 8) )
{
gdp_client_close(v1);
}
if ( !(unsigned int)gdp_client_isclosed(v1) )
{
v4 = gdp_packet_buffer(a1);
if ( !(unsigned int)gdp_io_thread_internal_send(*(_DWORD *)(v1 + 8), v4, v2) )
gdp_client_close(v1);
}
}
return gdp_packet_free(a1);
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1)
{
__int64 v1; // r12
int v2; // er13
__int64 v4; // rsi
int v5; // [rsp+8h] [rbp-20h] BYREF
int v6; // [rsp+Ch] [rbp-1Ch]

v1 = *(_QWORD *)(a1 + 8);
if ( (int)gdp_list_indexof(*(_QWORD *)(instance + 216), v1) >= 0 )
{
v5 = 5260359;
v6 = gdp_packet_size(a1);
v2 = v6;
if ( !(unsigned int)gdp_client_isclosed(v1)
&& !(unsigned int)func10(*(_DWORD *)(v1 + 8), (__int64)&v5, 8) )
{
gdp_client_close(v1);
}
if ( !(unsigned int)gdp_client_isclosed(v1) )
{
v4 = gdp_packet_buffer(a1);
if ( !(unsigned int)func10(*(_DWORD *)(v1 + 8), v4, v2) )
gdp_client_close(v1);
}
}
return gdp_packet_free(a1);
}

original_code:
void gdp_io_thread_process_send(gdp_packet_t *pack) {
 gdp_client_t* client = pack->client;
 if (gdp_list_indexof(instance->clients, client) < 0) {
  gdp_packet_free(pack);
  return;
 }
 char header[8];
 int size = gdp_packet_size(pack);
 memcpy(header, "GDP", 4);
 memcpy(header+4, &size, 4);
 if (!gdp_client_isclosed(client)) {
  if (!gdp_io_thread_internal_send(client->sock, header, 8))
   gdp_client_close(client);
 }
 if (!gdp_client_isclosed(client)) {
  if (!gdp_io_thread_internal_send(client->sock, gdp_packet_buffer(pack), size))
   gdp_client_close(client);
 }
 gdp_packet_free(pack);
}

canonical_original_code:
void gdp_io_thread_process_send(struct gdp_packet *pack) {
 struct gdp_client *client = pack->client;
 if (gdp_list_indexof(instance->clients, client) < 0) {
  gdp_packet_free(pack);
  return;
 }
 char header[8];
 int size = gdp_packet_size(pack);
 memcpy(header, "GDP", 4);
 memcpy(header+4, &size, 4);
 if (!gdp_client_isclosed(client)) {
  if (!gdp_io_thread_internal_send(client->sock, header, 8))
   gdp_client_close(client);
 }
 if (!gdp_client_isclosed(client)) {
  if (!gdp_io_thread_internal_send(client->sock, gdp_packet_buffer(pack), size))
   gdp_client_close(client);
 }
 gdp_packet_free(pack);
}

variable_types:
header
char [8]

variable_types: size int


decompiled_code:
void *__fastcall gdp_io_thread_run_send(void *a1)
{
__int64 v1; // rdi
__int64 v2; // rdi

v1 = instance;
if ( !*(_DWORD *)(instance + 224) )
return 0LL;
do
{
pthread_mutex_lock((pthread_mutex_t *)(v1 + 16));
while ( 1 )
{
v2 = gdp_queue_deq(*(_QWORD *)(instance + 192));
if ( v2 )
break;
pthread_cond_wait((pthread_cond_t *)(instance + 56), (pthread_mutex_t *)(instance + 16));
}
do
{
gdp_io_thread_process_send(v2);
v2 = gdp_queue_deq(*(_QWORD *)(instance + 192));
}
while ( v2 );
pthread_mutex_unlock((pthread_mutex_t *)(instance + 16));
v1 = instance;
}
while ( *(_DWORD *)(instance + 224) );
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func14(void *a1)
{
__int64 v1; // rdi
__int64 v2; // rdi

v1 = instance;
if ( !*(_DWORD *)(instance + 224) )
return 0LL;
do
{
pthread_mutex_lock((pthread_mutex_t *)(v1 + 16));
while ( 1 )
{
v2 = gdp_queue_deq(*(_QWORD *)(instance + 192));
if ( v2 )
break;
pthread_cond_wait((pthread_cond_t *)(instance + 56), (pthread_mutex_t *)(instance + 16));
}
do
{
func13(v2);
v2 = gdp_queue_deq(*(_QWORD *)(instance + 192));
}
while ( v2 );
pthread_mutex_unlock((pthread_mutex_t *)(instance + 16));
v1 = instance;
}
while ( *(_DWORD *)(instance + 224) );
return 0LL;
}

original_code:
void* gdp_io_thread_run_send(void* state) {
 gdp_packet_t* pack = ((void *)0);
 while (instance->_running) {
  pthread_mutex_lock(&instance->send_mutex);
  while ((pack = (gdp_packet_t*)gdp_queue_deq(instance->_queue_send)) == ((void *)0)) {
   pthread_cond_wait(&instance->send_cond, &instance->send_mutex);
  }
  while (pack != ((void *)0)) {
   gdp_io_thread_process_send(pack);
   pack = (gdp_packet_t*)gdp_queue_deq(instance->_queue_send);
  }
  pthread_mutex_unlock(&instance->send_mutex);
 }
 return ((void *)0);
}

canonical_original_code:
void *gdp_io_thread_run_send(void *state) {
 struct gdp_packet *pack = ((void *)0);
 while (instance->_running) {
  pthread_mutex_lock(&instance->send_mutex);
  while ((pack = (struct gdp_packet *)gdp_queue_deq(instance->_queue_send)) == ((void *)0)) {
   pthread_cond_wait(&instance->send_cond, &instance->send_mutex);
  }
  while (pack != ((void *)0)) {
   gdp_io_thread_process_send(pack);
   pack = (struct gdp_packet *)gdp_queue_deq(instance->_queue_send);
  }
  pthread_mutex_unlock(&instance->send_mutex);
 }
 return ((void *)0);
}


decompiled_code:
__int64 gdp_io_thread_inst()
{
pthread_once(&instance_once_control, gdp_io_thread_inst_once);
return instance;
}

canonical_decompiled_code:
__int64 func15()
{
pthread_once(&instance_once_control, gdp_io_thread_inst_once);
return instance;
}

original_code:
gdp_io_thread_t* gdp_io_thread_inst() {
 pthread_once(&instance_once_control, gdp_io_thread_inst_once);
 return instance;
}

canonical_original_code:
struct gdp_io_thread *gdp_io_thread_inst() {
 pthread_once(&instance_once_control, gdp_io_thread_inst_once);
 return instance;
}

return_type: struct gdp_io_thread { unsigned long thread_send; unsigned long thread_recv; 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; } send_mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } send_cond; union pthread_mutex_t recv_mutex; union pthread_cond_t recv_cond; struct gdp_queue { int len; struct gdp_node { struct gdp_node *next; void *value; } *head; struct gdp_node *tail; union pthread_mutex_t lock_obj; } *_queue_send; struct gdp_queue *_queue_recv; struct gdp_list { int count; int available; void **items; union pthread_mutex_t lock_obj; } *servers; struct gdp_list *clients; int _running; } *

user_defined_types: struct gdp_io_thread { unsigned long thread_send; unsigned long thread_recv; union pthread_mutex_t send_mutex; union pthread_cond_t send_cond; union pthread_mutex_t recv_mutex; union pthread_cond_t recv_cond; struct gdp_queue *_queue_send; struct gdp_queue *_queue_recv; struct gdp_list *servers; struct gdp_list *clients; int _running; }

user_defined_types: union pthread_cond_t { struct __pthread_cond_s __data; char __size[48]; long long __align; }

user_defined_types: struct gdp_queue { int len; struct gdp_node *head; struct gdp_node *tail; union pthread_mutex_t lock_obj; }


decompiled_code:
int gdp_io_thread_assure_wsa()
{
return pthread_once(&wsastartup_once_control, gdp_io_thread_assure_wsa_once);
}

canonical_decompiled_code:
int func16()
{
return pthread_once(&wsastartup_once_control, gdp_io_thread_assure_wsa_once);
}

original_code:
void gdp_io_thread_assure_wsa() {
 pthread_once(&wsastartup_once_control, gdp_io_thread_assure_wsa_once);
}


original_code:
gdp_io_thread_t* gdp_io_thread_new() {
    pthread_mutex_t mutexzero = { { 0, 0, 0, 0, 0, 0, 0, { 0, 0 } } };
    pthread_cond_t condzero = { { {0}, {0}, {0, 0}, {0, 0}, 0, 0, {0, 0} } };
 gdp_io_thread_t* io_thread = (gdp_io_thread_t*)xmalloc0(sizeof(gdp_io_thread_t));
 io_thread->servers = gdp_list_new();
 io_thread->clients = gdp_list_new();
 io_thread->_queue_send = gdp_queue_new();
 io_thread->_queue_recv = gdp_queue_new();
 io_thread->_running = 1;
 io_thread->send_mutex = mutexzero;
 io_thread->send_cond = condzero;
 io_thread->recv_mutex = mutexzero;
 io_thread->recv_cond = condzero;
 return io_thread;
}

canonical_original_code:
struct gdp_io_thread *gdp_io_thread_new() {
    union pthread_mutex_t mutexzero = { { 0, 0, 0, 0, 0, 0, 0, { 0, 0 } } };
    union pthread_cond_t condzero = { { {0}, {0}, {0, 0}, {0, 0}, 0, 0, {0, 0} } };
 struct gdp_io_thread *io_thread = (gdp_io_thread_t*)xmalloc0(sizeof(gdp_io_thread_t));
 io_thread->servers = gdp_list_new();
 io_thread->clients = gdp_list_new();
 io_thread->_queue_send = gdp_queue_new();
 io_thread->_queue_recv = gdp_queue_new();
 io_thread->_running = 1;
 io_thread->send_mutex = mutexzero;
 io_thread->send_cond = condzero;
 io_thread->recv_mutex = mutexzero;
 io_thread->recv_cond = condzero;
 return io_thread;
}

variable_types: struct gdp_io_thread { unsigned long thread_send; unsigned long thread_recv; 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; } send_mutex; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } send_cond; union pthread_mutex_t recv_mutex; union pthread_cond_t recv_cond; struct gdp_queue { int len; struct gdp_node { struct gdp_node *next; void *value; } *head; struct gdp_node *tail; union pthread_mutex_t lock_obj; } *_queue_send; struct gdp_queue *_queue_recv; struct gdp_list { int count; int available; void **items; union pthread_mutex_t lock_obj; } *servers; struct gdp_list *clients; int _running; } *


decompiled_code:
void gdp_io_thread_inst_once(void)
{
if ( !instance )
{
instance = gdp_io_thread_new();
pthread_create((pthread_t *)instance, 0LL, gdp_io_thread_run_send, 0LL);
pthread_create((pthread_t *)(instance + 8), 0LL, gdp_io_thread_run_recv, 0LL);
}
}

canonical_decompiled_code:
void func18(void)
{
if ( !instance )
{
instance = func17();
pthread_create((pthread_t *)instance, 0LL, gdp_io_thread_run_send, 0LL);
pthread_create((pthread_t *)(instance + 8), 0LL, gdp_io_thread_run_recv, 0LL);
}
}

original_code:
void gdp_io_thread_inst_once(void) {
 if (!instance) {
  instance = gdp_io_thread_new();
  pthread_create(&(instance->thread_send), ((void *)0), gdp_io_thread_run_send, ((void *)0));
  pthread_create(&(instance->thread_recv), ((void *)0), gdp_io_thread_run_recv, ((void *)0));
 }
}

canonical_original_code:
void gdp_io_thread_inst_once() {
 if (!instance) {
  instance = gdp_io_thread_new();
  pthread_create(&(instance->thread_send), ((void *)0), gdp_io_thread_run_send, ((void *)0));
  pthread_create(&(instance->thread_recv), ((void *)0), gdp_io_thread_run_recv, ((void *)0));
 }
}


decompiled_code:
__int64 __fastcall gdp_stream_reader_new(__int64 a1, int a2)
{
__int64 result; // rax

result = xmalloc0(16LL);
*(_QWORD *)result = a1;
*(_DWORD *)(result + 8) = 0;
*(_DWORD *)(result + 12) = a2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
__int64 result; // rax

result = xmalloc0(16LL);
*(_QWORD *)result = a1;
*(_DWORD *)(result + 8) = 0;
*(_DWORD *)(result + 12) = a2;
return result;
}

original_code:
gdp_stream_reader_t* gdp_stream_reader_new(char* buffer, int len) {
 gdp_stream_reader_t* reader = (gdp_stream_reader_t*)xmalloc0(sizeof(gdp_stream_reader_t));
 reader->buffer = buffer;
 reader->index = 0;
 reader->buffer_length = len;
 return reader;
}

canonical_original_code:
struct gdp_stream_reader *gdp_stream_reader_new(char *buffer, int len) {
 struct gdp_stream_reader *reader = (gdp_stream_reader_t*)xmalloc0(sizeof(gdp_stream_reader_t));
 reader->buffer = buffer;
 reader->index = 0;
 reader->buffer_length = len;
 return reader;
}

variable_types:
buffer
char *

variable_types: len int

variable_types: struct gdp_stream_reader { char *buffer; int index; int buffer_length; } *


decompiled_code:
// attributes: thunk
void gdp_stream_reader_free(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func1(void *ptr)
{
free(ptr);
}

original_code:
void gdp_stream_reader_free(gdp_stream_reader_t* reader) {
 free(reader); reader = ((void *)0);;
}

canonical_original_code:
void gdp_stream_reader_free(struct gdp_stream_reader *reader) {
 free(reader); reader = ((void *)0);;
}

user_defined_types: struct gdp_stream_reader { char *buffer; int index; int buffer_length; }


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

result = (unsigned int)(*(_DWORD *)(a1 + 12) - *(_DWORD *)(a1 + 8));
if ( (int)result < 0 )
result = 0LL;
return result;
}

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

result = (unsigned int)(*(_DWORD *)(a1 + 12) - *(_DWORD *)(a1 + 8));
if ( (int)result < 0 )
result = 0LL;
return result;
}

original_code:
int gdp_stream_reader_pending(gdp_stream_reader_t* reader) {
 return (reader->buffer_length - reader->index >= 0 ? reader->buffer_length - reader->index : 0);
}

canonical_original_code:
int gdp_stream_reader_pending(struct gdp_stream_reader *reader) {
 return (reader->buffer_length - reader->index >= 0 ? reader->buffer_length - reader->index : 0);
}


decompiled_code:
__int64 __fastcall gdp_stream_reader_read(__int64 a1)
{
int v1; // er12
__int64 v2; // rsi
int v3; // eax
int v4; // ecx
void *v5; // r8

v1 = 0;
v2 = *(int *)(a1 + 8);
if ( (int)v2 < *(_DWORD *)(a1 + 12) )
{
v3 = gdp_stream_reader_pending(a1);
v1 = v3;
if ( v4 > 0 && v3 > v4 )
v1 = v4;
memcpy(v5, (const void *)(*(_QWORD *)a1 + v2), v1);
*(_DWORD *)(a1 + 8) += v1;
}
return (unsigned int)v1;
}

original_code:
int gdp_stream_reader_read(gdp_stream_reader_t* reader, char* buffer, int size) {
 if (reader->index >= reader->buffer_length)
  return 0;
 if (size <= 0 || size > gdp_stream_reader_pending(reader))
  size = gdp_stream_reader_pending(reader);
 memcpy(buffer, reader->buffer + reader->index, size);
 reader->index += size;
 return size;
}

canonical_original_code:
int gdp_stream_reader_read(struct gdp_stream_reader *reader, char *buffer, int size) {
 if (reader->index >= reader->buffer_length)
  return 0;
 if (size <= 0 || size > gdp_stream_reader_pending(reader))
  size = gdp_stream_reader_pending(reader);
 memcpy(buffer, reader->buffer + reader->index, size);
 reader->index += size;
 return size;
}


decompiled_code:
__int64 __fastcall gdp_packet_new_from_air(__int64 a1, int a2)
{
__int64 v2; // r12

v2 = xmalloc0(32LL);
strcpy((char *)v2, "PACKET");
*(_QWORD *)(v2 + 8) = a1;
*(_DWORD *)(v2 + 24) = a2;
*(_QWORD *)(v2 + 16) = xmalloc0(a2);
return v2;
}

original_code:
gdp_packet_t* gdp_packet_new_from_air(gdp_client_t* client, int size) {
 gdp_packet_t* packet = (gdp_packet_t*)xmalloc0(sizeof(gdp_packet_t));
 strcpy(packet->objsign, "PACKET");
 packet->client = client;
 packet->size = size;
 packet->buffer = (char*)xmalloc0(size * sizeof(char));
 return packet;
}

canonical_original_code:
struct gdp_packet *gdp_packet_new_from_air(struct gdp_client *client, int size) {
 struct gdp_packet *packet = (gdp_packet_t*)xmalloc0(sizeof(gdp_packet_t));
 strcpy(packet->objsign, "PACKET");
 packet->client = client;
 packet->size = size;
 packet->buffer = (char *)xmalloc0(size * sizeof(char));
 return packet;
}

variable_types:
client
struct gdp_client *

return_type: struct gdp_packet { char objsign[7]; struct gdp_client { char objsign[7]; int sock; struct gdp_addr { char ip[15]; int port; } addr; int _closed; int state; struct gdp_stream_writer { char *buffer; int maxsize; int size; } *_prebuffer; struct gdp_packet *_prepack; struct gdp_list { int count; int available; void **items; 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; } lock_obj; } *_onclose_callbacks; } *client; char *buffer; int size; int current_size; } *


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

result = xmalloc0(32LL);
strcpy((char *)result, "PACKET");
*(_QWORD *)(result + 8) = a1;
*(_QWORD *)(result + 16) = a2;
*(_DWORD *)(result + 24) = a3;
*(_DWORD *)(result + 28) = a3;
return result;
}

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

result = xmalloc0(32LL);
strcpy((char *)result, "PACKET");
*(_QWORD *)(result + 8) = a1;
*(_QWORD *)(result + 16) = a2;
*(_DWORD *)(result + 24) = a3;
*(_DWORD *)(result + 28) = a3;
return result;
}

original_code:
gdp_packet_t* gdp_packet_new(gdp_client_t* client, char* buffer, int len) {
 gdp_packet_t* packet = (gdp_packet_t*)xmalloc0(sizeof(gdp_packet_t));
 strcpy(packet->objsign, "PACKET");
 packet->client = client;
 packet->buffer = buffer;
 packet->size = len;
 packet->current_size = len;
 return packet;
}

canonical_original_code:
struct gdp_packet *gdp_packet_new(struct gdp_client *client, char *buffer, int len) {
 struct gdp_packet *packet = (gdp_packet_t*)xmalloc0(sizeof(gdp_packet_t));
 strcpy(packet->objsign, "PACKET");
 packet->client = client;
 packet->buffer = buffer;
 packet->size = len;
 packet->current_size = len;
 return packet;
}


decompiled_code:
// attributes: thunk
void gdp_packet_free_but_buffer(void *ptr)
{
free(ptr);
}

canonical_decompiled_code:
void func2(void *ptr)
{
free(ptr);
}

original_code:
void gdp_packet_free_but_buffer(gdp_packet_t* packet) {
 free(packet); packet = ((void *)0);;
}

canonical_original_code:
void gdp_packet_free_but_buffer(struct gdp_packet *packet) {
 free(packet); packet = ((void *)0);;
}


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

if ( ptr )
{
if ( !memcmp(ptr, "PACKET", 6uLL) )
{
*(_DWORD *)ptr = 0;
*((_WORD *)ptr + 2) = 0;
*((_BYTE *)ptr + 6) = 0;
}
else
{
printf(
"CHEGOU AQUI %s:%d:%s\n FREE: esperando pacote mas nao veio. (%p)\n\n",
"../Lib/packet.c",
28LL,
"gdp_packet_free",
ptr);
}
}
else
{
printf(
"CHEGOU AQUI %s:%d:%s\n Tentando FREE no pacote NULL.%p\n\n",
"../Lib/packet.c",
26LL,
"gdp_packet_free",
0LL);
}
v2 = (void *)*((_QWORD *)ptr + 2);
if ( v2 )
{
free(v2);
*((_QWORD *)ptr + 2) = 0LL;
}
gdp_packet_free_but_buffer(ptr);
}

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

if ( ptr )
{
if ( !memcmp(ptr, "PACKET", 6uLL) )
{
*(_DWORD *)ptr = 0;
*((_WORD *)ptr + 2) = 0;
*((_BYTE *)ptr + 6) = 0;
}
else
{
printf(
"CHEGOU AQUI %s:%d:%s\n FREE: esperando pacote mas nao veio. (%p)\n\n",
"../Lib/packet.c",
28LL,
"gdp_packet_free",
ptr);
}
}
else
{
printf(
"CHEGOU AQUI %s:%d:%s\n Tentando FREE no pacote NULL.%p\n\n",
"../Lib/packet.c",
26LL,
"gdp_packet_free",
0LL);
}
v2 = (void *)*((_QWORD *)ptr + 2);
if ( v2 )
{
free(v2);
*((_QWORD *)ptr + 2) = 0LL;
}
func2(ptr);
}

original_code:
void gdp_packet_free(gdp_packet_t* packet) {
 if (!packet) {
  printf("CHEGOU AQUI %s:%d:%s\n   " "Tentando FREE no pacote NULL.%p\n" "\n", "../Lib/packet.c", 26, __FUNCTION__, packet);;
 } else if (strncmp(packet->objsign, "PACKET", 6) != 0) {
  printf("CHEGOU AQUI %s:%d:%s\n   " "FREE: esperando pacote mas nao veio. (%p)\n" "\n", "../Lib/packet.c", 28, __FUNCTION__, packet);;
 } else {
  memset(packet->objsign, 0, sizeof(packet->objsign));
 }
 if (packet->buffer) {
  free(packet->buffer); packet->buffer = ((void *)0);;
  packet->buffer = ((void *)0);
 }
 gdp_packet_free_but_buffer(packet);
}

canonical_original_code:
void gdp_packet_free(struct gdp_packet *packet) {
 if (!packet) {
  printf("CHEGOU AQUI %s:%d:%s\n   " "Tentando FREE no pacote NULL.%p\n" "\n", "../Lib/packet.c", 26, __FUNCTION__, packet);;
 } else if (strncmp(packet->objsign, "PACKET", 6) != 0) {
  printf("CHEGOU AQUI %s:%d:%s\n   " "FREE: esperando pacote mas nao veio. (%p)\n" "\n", "../Lib/packet.c", 28, __FUNCTION__, packet);;
 } else {
  memset(packet->objsign, 0, sizeof(packet->objsign));
 }
 if (packet->buffer) {
  free(packet->buffer); packet->buffer = ((void *)0);;
  packet->buffer = ((void *)0);
 }
 gdp_packet_free_but_buffer(packet);
}


decompiled_code:
void *__fastcall gdp_packet_append(__int64 a1, const void *a2, int a3)
{
void *result; // rax

result = memcpy((void *)(*(_QWORD *)(a1 + 16) + *(int *)(a1 + 28)), a2, a3);
*(_DWORD *)(a1 + 28) += a3;
return result;
}

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

result = memcpy((void *)(*(_QWORD *)(a1 + 16) + *(int *)(a1 + 28)), a2, a3);
*(_DWORD *)(a1 + 28) += a3;
return result;
}

original_code:
void gdp_packet_append(gdp_packet_t* packet, const char* buffer, int len) {
 memcpy(packet->buffer + packet->current_size, buffer, len);
 packet->current_size += len;
}

canonical_original_code:
void gdp_packet_append(struct gdp_packet *packet, char *buffer, int len) {
 memcpy(packet->buffer + packet->current_size, buffer, len);
 packet->current_size += len;
}


decompiled_code:
_BOOL8 __fastcall gdp_packet_is_complete(__int64 a1)
{
return *(_DWORD *)(a1 + 28) == *(_DWORD *)(a1 + 24);
}

original_code:
int gdp_packet_is_complete(gdp_packet_t* packet) {
 return packet->current_size == packet->size;
}

canonical_original_code:
int gdp_packet_is_complete(struct gdp_packet *packet) {
 return packet->current_size == packet->size;
}


decompiled_code:
__int64 __fastcall gdp_packet_pending(__int64 a1)
{
return (unsigned int)(*(_DWORD *)(a1 + 24) - *(_DWORD *)(a1 + 28));
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return (unsigned int)(*(_DWORD *)(a1 + 24) - *(_DWORD *)(a1 + 28));
}

original_code:
int gdp_packet_pending(gdp_packet_t* packet) {
 return packet->size - packet->current_size;
}

canonical_original_code:
int gdp_packet_pending(struct gdp_packet *packet) {
 return packet->size - packet->current_size;
}


decompiled_code:
__int64 __fastcall gdp_packet_size(__int64 a1)
{
return *(unsigned int *)(a1 + 24);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return *(unsigned int *)(a1 + 24);
}

original_code:
int gdp_packet_size(gdp_packet_t* packet) {
 return packet->size;
}

canonical_original_code:
int gdp_packet_size(struct gdp_packet *packet) {
 return packet->size;
}


decompiled_code:
__int64 __fastcall gdp_packet_buffer(__int64 a1)
{
return *(_QWORD *)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
return *(_QWORD *)(a1 + 16);
}

original_code:
char* gdp_packet_buffer(gdp_packet_t* packet) {
 return packet->buffer;
}

canonical_original_code:
char *gdp_packet_buffer(struct gdp_packet *packet) {
 return packet->buffer;
}

return_type: char *


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

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

original_code:
gdp_client_t* gdp_packet_client(gdp_packet_t* packet) {
 return packet->client;
}

canonical_original_code:
struct gdp_client *gdp_packet_client(struct gdp_packet *packet) {
 return packet->client;
}

variable_types:
packet
struct gdp_packet *

return_type: struct gdp_client { char objsign[7]; int sock; struct gdp_addr { char ip[15]; int port; } addr; int _closed; int state; struct gdp_stream_writer { char *buffer; int maxsize; int size; } *_prebuffer; struct gdp_packet { char objsign[7]; struct gdp_client *client; char *buffer; int size; int current_size; } *_prepack; struct gdp_list { int count; int available; void **items; 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; } lock_obj; } *_onclose_callbacks; } *


decompiled_code:
__int64 __fastcall gdp_packet_parse(__int64 a1, char *a2)
{
if ( *(_DWORD *)a2 == 5260359 )
return gdp_packet_new_from_air(a1, a2[4]);
fprintf(
stderr,
"Invalid signature. 0x%2x%2x%2x%2x.\n",
(unsigned int)*a2,
(unsigned int)a2[1],
(unsigned int)a2[2],
(unsigned int)a2[3]);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, char *a2)
{
if ( *(_DWORD *)a2 == 5260359 )
return func0(a1, a2[4]);
fprintf(
stderr,
"Invalid signature. 0x%2x%2x%2x%2x.\n",
(unsigned int)*a2,
(unsigned int)a2[1],
(unsigned int)a2[2],
(unsigned int)a2[3]);
return 0LL;
}

original_code:
gdp_packet_t* gdp_packet_parse(gdp_client_t* client, const char* buffer, int len) {
 if (memcmp(buffer, "GDP\0", 4) != 0) {
  fprintf(stderr, "Invalid signature. 0x%2x%2x%2x%2x.\n", *buffer, *(buffer+1), *(buffer+2), *(buffer+3));
  return ((void *)0);
 }
 int size = (int)*(buffer + 4);
 if (1024 < size) {
  fprintf(stderr, "Invalid buffer. Size '%d' greater than '%d'.\n", size, 1024);
  return ((void *)0);
 }
 return gdp_packet_new_from_air(client, size);
}

canonical_original_code:
struct gdp_packet *gdp_packet_parse(struct gdp_client *client, char *buffer, int len) {
 if (memcmp(buffer, "GDP\0", 4) != 0) {
  fprintf(stderr, "Invalid signature. 0x%2x%2x%2x%2x.\n", *buffer, *(buffer+1), *(buffer+2), *(buffer+3));
  return ((void *)0);
 }
 int size = (int)*(buffer + 4);
 if (1024 < size) {
  fprintf(stderr, "Invalid buffer. Size '%d' greater than '%d'.\n", size, 1024);
  return ((void *)0);
 }
 return gdp_packet_new_from_air(client, size);
}


decompiled_code:
__int64 __fastcall gdp_server_onclose_client(__int64 a1, __int64 a2)
{
int i; // ebx
__int64 v4; // rsi
void (__fastcall *v5)(__int64, __int64); // rax

for ( i = 0; (int)gdp_list_count(*(_QWORD *)(a2 + 8)) > i; ++i )
{
v4 = (unsigned int)i;
v5 = (void (__fastcall *)(__int64, __int64))gdp_list_get(*(_QWORD *)(a2 + 8), v4);
v5(a1, 1LL);
}
return gdp_list_remove_item(*(_QWORD *)(a2 + 16), a1);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
int i; // ebx
__int64 v4; // rsi
void (__fastcall *v5)(__int64, __int64); // rax

for ( i = 0; (int)gdp_list_count(*(_QWORD *)(a2 + 8)) > i; ++i )
{
v4 = (unsigned int)i;
v5 = (void (__fastcall *)(__int64, __int64))gdp_list_get(*(_QWORD *)(a2 + 8), v4);
v5(a1, 1LL);
}
return gdp_list_remove_item(*(_QWORD *)(a2 + 16), a1);
}

original_code:
void gdp_server_onclose_client(void* c, void* state) {
 gdp_client_t* client = (gdp_client_t*)c;
 gdp_server_t* server = (gdp_server_t*)state;
 int i;
 for (i = 0; i < gdp_list_count(server->_onopenclose_callbacks); i++) {
  onopenclose_callback callback = gdp_list_get(server->_onopenclose_callbacks, i);
  callback(client, 1);
 }
 gdp_list_remove_item(server->clients, client);
}

canonical_original_code:
void gdp_server_onclose_client(void *c, void *state) {
 struct gdp_client *client = (gdp_client_t*)c;
 struct gdp_server *server = (gdp_server_t*)state;
 int i;
 for (i = 0; i < gdp_list_count(server->_onopenclose_callbacks); i++) {
  void (*callback)(struct gdp_client *, int) = gdp_list_get(server->_onopenclose_callbacks, i);
  callback(client, 1);
 }
 gdp_list_remove_item(server->clients, client);
}

variable_types:
c
void *

variable_types: struct gdp_server { int sock; struct gdp_list { int count; int available; void **items; 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; } lock_obj; } *_onopenclose_callbacks; struct gdp_list *clients; } *

variable_types: i int

variable_types: void (*)(struct gdp_client { char objsign[7]; int sock; struct gdp_addr { char ip[15]; int port; } addr; int _closed; int state; struct gdp_stream_writer { char *buffer; int maxsize; int size; } *_prebuffer; struct gdp_packet { char objsign[7]; struct gdp_client *client; char *buffer; int size; int current_size; } *_prepack; struct gdp_list { int count; int available; void **items; 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; } lock_obj; } *_onclose_callbacks; } *, int)


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

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

original_code:
void gdp_server_add_onopenclose(gdp_server_t* server, onopenclose_callback callback) {
 gdp_list_add(server->_onopenclose_callbacks, callback);
}

canonical_original_code:
void gdp_server_add_onopenclose(struct gdp_server *server, void (*callback)(struct gdp_client *, int)) {
 gdp_list_add(server->_onopenclose_callbacks, callback);
}

variable_types: void (*)(struct gdp_client { char objsign[7]; int sock; struct gdp_addr { char ip[15]; int port; } addr; int _closed; int state; struct gdp_stream_writer { char *buffer; int maxsize; int size; } *_prebuffer; struct gdp_packet { char objsign[7]; struct gdp_client *client; char *buffer; int size; int current_size; } *_prepack; struct gdp_list *_onclose_callbacks; } *, int)


decompiled_code:
// attributes: thunk
__int64 gdp_server_recv()
{
return gdp_io_thread_dequeue_recv();
}

canonical_decompiled_code:
__int64 func3()
{
return gdp_io_thread_dequeue_recv();
}

original_code:
gdp_packet_t* gdp_server_recv(gdp_server_t* server) {
 return gdp_io_thread_dequeue_recv();
}

canonical_original_code:
struct gdp_packet *gdp_server_recv(struct gdp_server *server) {
 return gdp_io_thread_dequeue_recv();
}

variable_types: struct gdp_server { int sock; struct gdp_list *_onopenclose_callbacks; struct gdp_list *clients; } *


decompiled_code:
__int64 __fastcall gdp_server_send_packet(__int64 a1, __int64 a2)
{
return gdp_io_thread_enqueue_send(a2);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2)
{
return gdp_io_thread_enqueue_send(a2);
}

original_code:
void gdp_server_send_packet(gdp_server_t* server, gdp_packet_t* pack) {
 gdp_io_thread_enqueue_send(pack);
}

canonical_original_code:
void gdp_server_send_packet(struct gdp_server *server, struct gdp_packet *pack) {
 gdp_io_thread_enqueue_send(pack);
}

variable_types: struct gdp_packet { char objsign[7]; struct gdp_client { char objsign[7]; int sock; struct gdp_addr { char ip[15]; int port; } addr; int _closed; int state; struct gdp_stream_writer { char *buffer; int maxsize; int size; } *_prebuffer; struct gdp_packet *_prepack; struct gdp_list *_onclose_callbacks; } *client; char *buffer; int size; int current_size; } *


decompiled_code:
__int64 __fastcall gdp_server_send(__int64 a1, __int64 a2, const void *a3, int a4)
{
size_t v4; // r15
void *v6; // rax
void *v7; // rax
__int64 v8; // rax

v4 = a4;
v6 = (void *)xmalloc(a4);
v7 = memcpy(v6, a3, v4);
v8 = gdp_packet_new(a2, v7, (unsigned int)v4);
return gdp_server_send_packet(a1, v8);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, const void *a3, int a4)
{
size_t v4; // r15
void *v6; // rax
void *v7; // rax
__int64 v8; // rax

v4 = a4;
v6 = (void *)xmalloc(a4);
v7 = memcpy(v6, a3, v4);
v8 = gdp_packet_new(a2, v7, (unsigned int)v4);
return func4(a1, v8);
}

original_code:
void gdp_server_send(gdp_server_t* server, gdp_client_t* client, char* buffer, int len) {
 char* buf_clone = (char*)xmalloc(len * sizeof(char));
 memcpy(buf_clone, buffer, len);
 gdp_packet_t* pack = gdp_packet_new(client, buf_clone, len);
 gdp_server_send_packet(server, pack);
}

canonical_original_code:
void gdp_server_send(struct gdp_server *server, struct gdp_client *client, char *buffer, int len) {
 char *buf_clone = (char*)xmalloc(len * sizeof(char));
 memcpy(buf_clone, buffer, len);
 struct gdp_packet *pack = gdp_packet_new(client, buf_clone, len);
 gdp_server_send_packet(server, pack);
}

variable_types: struct gdp_client { char objsign[7]; int sock; struct gdp_addr { char ip[15]; int port; } addr; int _closed; int state; struct gdp_stream_writer { char *buffer; int maxsize; int size; } *_prebuffer; struct gdp_packet { char objsign[7]; struct gdp_client *client; char *buffer; int size; int current_size; } *_prepack; struct gdp_list *_onclose_callbacks; } *

variable_types: struct gdp_packet { char objsign[7]; struct gdp_client { char objsign[7]; int sock; struct gdp_addr { char ip[15]; int port; } addr; int _closed; int state; struct gdp_stream_writer { char *buffer; int maxsize; int size; } *_prebuffer; struct gdp_packet *_prepack; struct gdp_list { int count; int available; void **items; 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; } lock_obj; } *_onclose_callbacks; } *client; char *buffer; int size; int current_size; } *