decompiled_code:
__int64 __fastcall flush_defered(unsigned int *a1)
{
unsigned int v1; // ebp
unsigned int v2; // eax

if ( verbose )
fprintf(stderr, "flushing defered data to fd %d\n", *a1);
v1 = write(*a1, *((const void **)a1 + 2), (int)a1[6]);
if ( v1 != -1 )
{
v2 = a1[6];
if ( v2 == v1 )
{
free(*((void **)a1 + 1));
*((_QWORD *)a1 + 1) = 0LL;
*((_QWORD *)a1 + 2) = 0LL;
a1[6] = 0;
}
else
{
*((_QWORD *)a1 + 2) += (int)v1;
a1[6] = v2 - v1;
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int *a1)
{
unsigned int v1; // ebp
unsigned int v2; // eax

if ( verbose )
fprintf(stderr, "flushing defered data to fd %d\n", *a1);
v1 = write(*a1, *((const void **)a1 + 2), (int)a1[6]);
if ( v1 != -1 )
{
v2 = a1[6];
if ( v2 == v1 )
{
free(*((void **)a1 + 1));
*((_QWORD *)a1 + 1) = 0LL;
*((_QWORD *)a1 + 2) = 0LL;
a1[6] = 0;
}
else
{
*((_QWORD *)a1 + 2) += (int)v1;
a1[6] = v2 - v1;
}
}
return v1;
}

original_code:
int flush_defered(struct queue *q)
{
    int n;
    if (verbose)
        fprintf(stderr, "flushing defered data to fd %d\n", q->fd);
    n = write(q->fd, q->defered_data, q->defered_data_size);
    if (n == -1)
        return n;
    if (n == q->defered_data_size) {
        free(q->begin_defered_data);
        q->begin_defered_data = ((void *)0);
        q->defered_data = ((void *)0);
        q->defered_data_size = 0;
    } else {
        q->defered_data += n;
        q->defered_data_size -= n;
    }
    return n;
}

variable_types: n int

user_defined_types: struct queue { int fd; void *begin_defered_data; void *defered_data; int defered_data_size; }


repo: guilhem/sslh

decompiled_code:
int __fastcall dump_connection(unsigned int *a1)
{
printf("state: %d\n", *a1);
printf("fd %d, %d defered\n", a1[4], a1[10]);
return printf("fd %d, %d defered\n", a1[12], a1[18]);
}

canonical_decompiled_code:
int __fastcall func3(unsigned int *a1)
{
printf("state: %d\n", *a1);
printf("fd %d, %d defered\n", a1[4], a1[10]);
return printf("fd %d, %d defered\n", a1[12], a1[18]);
}

original_code:
void dump_connection(struct connection *cnx)
{
    printf("state: %d\n", cnx->state);
    printf("fd %d, %d defered\n", cnx->q[0].fd, cnx->q[0].defered_data_size);
    printf("fd %d, %d defered\n", cnx->q[1].fd, cnx->q[1].defered_data_size);
}

variable_types: struct connection { enum connection_state { ST_PROBING=1, ST_SHOVELING=2 } state; long probe_timeout; struct queue { int fd; void *begin_defered_data; void *defered_data; int defered_data_size; } q[2]; } *

return_type: void

user_defined_types: struct connection { enum connection_state state; long probe_timeout; struct queue q[2]; }


decompiled_code:
void log_message(int a1, const char *a2, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
if ( foreground )
vfprintf(stderr, a2, arg);
else
vsyslog(a1, a2, arg);
}

canonical_decompiled_code:
void func4(int a1, const char *a2, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, a2);
if ( foreground )
vfprintf(stderr, a2, arg);
else
vsyslog(a1, a2, arg);
}

original_code:
void log_message(int type, char* msg, ...)
{
    va_list ap;
    __builtin_va_start(ap,msg);
    if (foreground)
        vfprintf(stderr, msg, ap);
    else
        vsyslog(type, msg, ap);
    __builtin_va_end(ap);
}

canonical_original_code:
void log_message(int type, char *msg, ...)
{
    __builtin_va_list ap;
    __builtin_va_start(ap,msg);
    if (foreground)
        vfprintf(stderr, msg, ap);
    else
        vsyslog(type, msg, ap);
    __builtin_va_end(ap);
}

variable_types: type int

variable_types:
msg
char *


decompiled_code:
__int64 __fastcall fd2fd(unsigned int *a1, int *a2)
{
int v2; // er13
unsigned int v3; // er14
signed int v4; // eax
unsigned int v5; // ebp
signed int v6; // ebx
__int64 v8; // rcx
signed int v9; // eax
__int64 v10; // rcx
char v11[8232]; // [rsp+0h] [rbp-2028h] BYREF

v2 = *a1;
v3 = *a2;
v4 = read(*a2, v11, 0x2000uLL);
v5 = v4;
if ( v4 == -1 )
{
v8 = (unsigned int)*_errno_location();
switch ( (_DWORD)v8 )
{
case 0x20:
v5 = 0;
break;
case 0x68:
v5 = 0;
break;
case 0xB:
if ( verbose )
fprintf(stderr, "reading 0 from %d\n", v3);
break;
default:
log_message(2, "%s: %d\n", "read", v8);
break;
}
}
else
{
v6 = v4;
if ( v4 )
{
v9 = write(v2, v11, v4);
v5 = v9;
if ( v9 == -1 )
{
v10 = (unsigned int)*_errno_location();
switch ( (_DWORD)v10 )
{
case 0x20:
v5 = 0;
break;
case 0x68:
v5 = 0;
break;
case 0xB:
defer_write(a1, v11, v6);
v5 = -2;
break;
default:
log_message(2, "%s: %d\n", "write", v10);
break;
}
}
else if ( v6 > v9 )
{
defer_write(a1, &v11[v9], v6 - v9);
v5 = -2;
}
}
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int *a1, int *a2)
{
int v2; // er13
unsigned int v3; // er14
signed int v4; // eax
unsigned int v5; // ebp
signed int v6; // ebx
__int64 v8; // rcx
signed int v9; // eax
__int64 v10; // rcx
char v11[8232]; // [rsp+0h] [rbp-2028h] BYREF

v2 = *a1;
v3 = *a2;
v4 = read(*a2, v11, 0x2000uLL);
v5 = v4;
if ( v4 == -1 )
{
v8 = (unsigned int)*_errno_location();
switch ( (_DWORD)v8 )
{
case 0x20:
v5 = 0;
break;
case 0x68:
v5 = 0;
break;
case 0xB:
if ( verbose )
fprintf(stderr, "reading 0 from %d\n", v3);
break;
default:
func4(2, "%s: %d\n", "read", v8);
break;
}
}
else
{
v6 = v4;
if ( v4 )
{
v9 = write(v2, v11, v4);
v5 = v9;
if ( v9 == -1 )
{
v10 = (unsigned int)*_errno_location();
switch ( (_DWORD)v10 )
{
case 0x20:
v5 = 0;
break;
case 0x68:
v5 = 0;
break;
case 0xB:
func0(a1, v11, v6);
v5 = -2;
break;
default:
func4(2, "%s: %d\n", "write", v10);
break;
}
}
else if ( v6 > v9 )
{
func0(a1, &v11[v9], v6 - v9);
v5 = -2;
}
}
}
return v5;
}

original_code:
int fd2fd(struct queue *target_q, struct queue *from_q)
{
   char buffer[8192];
   int target, from, size_r, size_w;
   target = target_q->fd;
   from = from_q->fd;
   size_r = read(from, buffer, sizeof(buffer));
   if (size_r == -1) {
       switch ((*__errno_location ())) {
       case 11:
           if (verbose)
               fprintf(stderr, "reading 0 from %d\n", from);
           return -1;
       case 104:
       case 32:
           return 0;
       }
   }
   if (size_r == -1) { log_message(2, "%s: %d\n", "read", (*__errno_location ())); return size_r; };
   if (size_r == 0)
      return 0;
   size_w = write(target, buffer, size_r);
   if ((size_w == -1)) {
       switch ((*__errno_location ())) {
       case 11:
           defer_write(target_q, buffer, size_r);
           return -2;
       case 104:
       case 32:
           return 0;
       }
   } else if (size_w < size_r) {
       defer_write(target_q, buffer + size_w, size_r - size_w);
       return -2;
   }
   if (size_w == -1) { log_message(2, "%s: %d\n", "write", (*__errno_location ())); return size_w; };
   return size_w;
}

canonical_original_code:
int fd2fd(struct queue *target_q, struct queue *from_q)
{
   char buffer[8192];
   int target; int from; int size_r; int size_w;
   target = target_q->fd;
   from = from_q->fd;
   size_r = read(from, buffer, sizeof(buffer));
   if (size_r == -1) {
       switch ((*__errno_location ())) {
       case 11:
           if (verbose)
               fprintf(stderr, "reading 0 from %d\n", from);
           return -1;
       case 104:
       case 32:
           return 0;
       }
   }
   if (size_r == -1) { log_message(2, "%s: %d\n", "read", (*__errno_location ())); return size_r; };
   if (size_r == 0)
      return 0;
   size_w = write(target, buffer, size_r);
   if ((size_w == -1)) {
       switch ((*__errno_location ())) {
       case 11:
           defer_write(target_q, buffer, size_r);
           return -2;
       case 104:
       case 32:
           return 0;
       }
   } else if (size_w < size_r) {
       defer_write(target_q, buffer + size_w, size_r - size_w);
       return -2;
   }
   if (size_w == -1) { log_message(2, "%s: %d\n", "write", (*__errno_location ())); return size_w; };
   return size_w;
}

variable_types: struct queue { int fd; void *begin_defered_data; void *defered_data; int defered_data_size; } *

variable_types:
buffer
char [8192]

variable_types: target int

variable_types: from int

return_type: int


sprintaddr

decompiled_code:
char *__fastcall sprintaddr(char *s, size_t maxlen, __int64 a3)
{
int v4; // eax
int v5; // eax
const char *v7; // rax
int v8; // eax
const char *v9; // rax
char serv[2]; // [rsp+8h] [rbp-448h] BYREF
char host[2]; // [rsp+28h] [rbp-428h] BYREF

v4 = numeric;
if ( numeric )
v4 = 3;
v5 = getnameinfo(*(const struct sockaddr **)(a3 + 24), *(_DWORD *)(a3 + 16), host, 0x401u, serv, 0x20u, v4);
if ( v5 )
{
v7 = gai_strerror(v5);
log_message(3, "sprintaddr:getnameinfo: %s\n", v7);
v8 = getnameinfo(*(const struct sockaddr **)(a3 + 24), *(_DWORD *)(a3 + 16), host, 0x401u, serv, 0x20u, 3);
if ( v8 )
{
v9 = gai_strerror(v8);
log_message(3, "sprintaddr:getnameinfo(NUM): %s\n", v9);
strcpy(host, "?");
strcpy(serv, "?");
}
}
snprintf(s, maxlen, "%s:%s", host, serv);
return s;
}

canonical_decompiled_code:
char *__fastcall func6(char *s, size_t maxlen, __int64 a3)
{
int v4; // eax
int v5; // eax
const char *v7; // rax
int v8; // eax
const char *v9; // rax
char serv[2]; // [rsp+8h] [rbp-448h] BYREF
char host[2]; // [rsp+28h] [rbp-428h] BYREF

v4 = numeric;
if ( numeric )
v4 = 3;
v5 = getnameinfo(*(const struct sockaddr **)(a3 + 24), *(_DWORD *)(a3 + 16), host, 0x401u, serv, 0x20u, v4);
if ( v5 )
{
v7 = gai_strerror(v5);
func4(3, "sprintaddr:getnameinfo: %s\n", v7);
v8 = getnameinfo(*(const struct sockaddr **)(a3 + 24), *(_DWORD *)(a3 + 16), host, 0x401u, serv, 0x20u, 3);
if ( v8 )
{
v9 = gai_strerror(v8);
func4(3, "sprintaddr:getnameinfo(NUM): %s\n", v9);
strcpy(host, "?");
strcpy(serv, "?");
}
}
snprintf(s, maxlen, "%s:%s", host, serv);
return s;
}

original_code:
char* sprintaddr(char* buf, size_t size, struct addrinfo *a)
{
   char host[1025], serv[32];
   int res;
   res = getnameinfo(a->ai_addr, a->ai_addrlen,
               host, sizeof(host),
               serv, sizeof(serv),
               numeric ? 1 | 2 : 0 );
   if (res) {
       log_message(3, "sprintaddr:getnameinfo: %s\n", gai_strerror(res));
       res = getnameinfo(a->ai_addr, a->ai_addrlen,
                         host, sizeof(host),
                         serv, sizeof(serv),
                         1 | 2);
       if (res) {
           log_message(3, "sprintaddr:getnameinfo(NUM): %s\n", gai_strerror(res));
           strcpy(host, "?");
           strcpy(serv, "?");
       }
   }
   snprintf(buf, size, "%s:%s", host, serv);
   return buf;
}

canonical_original_code:
char *sprintaddr(char *buf, unsigned long size, struct addrinfo *a)
{
   char host[1025]; char serv[32];
   int res;
   res = getnameinfo(a->ai_addr, a->ai_addrlen,
               host, sizeof(host),
               serv, sizeof(serv),
               numeric ? 1 | 2 : 0 );
   if (res) {
       log_message(3, "sprintaddr:getnameinfo: %s\n", gai_strerror(res));
       res = getnameinfo(a->ai_addr, a->ai_addrlen,
                         host, sizeof(host),
                         serv, sizeof(serv),
                         1 | 2);
       if (res) {
           log_message(3, "sprintaddr:getnameinfo(NUM): %s\n", gai_strerror(res));
           strcpy(host, "?");
           strcpy(serv, "?");
       }
   }
   snprintf(buf, size, "%s:%s", host, serv);
   return buf;
}

variable_types:
buf
char *

variable_types:
size
unsigned long

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

variable_types:
host
char [1025]

variable_types:
serv
char [32]

variable_types: res int

return_type: char *

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


decompiled_code:
void __fastcall check_res_dumpdie(int a1, __int64 a2, const char *a3)
{
__int64 v3; // rbx
__int64 v4; // rbp
__int64 v5; // r12
int *v7; // rax
char *v8; // r12
char *v9; // rax
char v10; // [rsp-428h] [rbp-428h] BYREF
__int64 v11; // [rsp-18h] [rbp-18h]
__int64 v12; // [rsp-10h] [rbp-10h]
__int64 v13; // [rsp-8h] [rbp-8h]

if ( a1 == -1 )
{
v13 = v5;
v12 = v4;
v11 = v3;
v7 = _errno_location();
v8 = strerror(*v7);
v9 = sprintaddr(&v10, 0x401uLL, a2);
fprintf(stderr, "%s:%s: %s\n", v9, a3, v8);
exit(1);
}
}

original_code:
void check_res_dumpdie(int res, struct addrinfo *addr, char* syscall)
{
    char buf[1025];
    if (res == -1) {
        fprintf(stderr, "%s:%s: %s\n",
                sprintaddr(buf, sizeof(buf), addr),
                syscall,
                strerror((*__errno_location ())));
        exit(1);
    }
}

canonical_original_code:
void check_res_dumpdie(int res, struct addrinfo *addr, char *syscall)
{
    char buf[1025];
    if (res == -1) {
        fprintf(stderr, "%s:%s: %s\n",
                sprintaddr(buf, sizeof(buf), addr),
                syscall,
                strerror((*__errno_location ())));
        exit(1);
    }
}

variable_types:
syscall
char *

variable_types:
buf
char [1025]

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


decompiled_code:
__int64 __fastcall start_listen_sockets(void **a1, __int64 a2)
{
__int64 v2; // rbx
__int64 v3; // rax
int v4; // er13
__int64 v5; // rbp
int *v6; // r14
int v7; // eax
int v8; // eax
int v9; // eax
bool v10; // dl
int optval[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v2 = a2;
if ( !a2 )
{
v4 = verbose;
if ( !verbose )
{
*a1 = malloc(0LL);
return (unsigned int)v4;
}
v4 = 0;
goto LABEL_11;
}
v3 = a2;
v4 = 0;
do
{
++v4;
v3 = *(_QWORD *)(v3 + 40);
}
while ( v3 );
if ( verbose )
LABEL_11:
fprintf(stderr, "listening to %d addresses\n", (unsigned int)v4);
*a1 = malloc(4LL * v4);
if ( v4 > 0 && a2 )
{
v5 = 0LL;
do
{
v6 = (int *)((char *)*a1 + 4 * v5);
*v6 = socket(**(unsigned __int16 **)(v2 + 24), 1, 0);
check_res_dumpdie(*((_DWORD *)*a1 + v5), v2, "socket");
optval[0] = 1;
v7 = setsockopt(*((_DWORD *)*a1 + v5), 1, 2, optval, 4u);
check_res_dumpdie(v7, v2, "setsockopt");
v8 = bind(*((_DWORD *)*a1 + v5), *(const struct sockaddr **)(v2 + 24), *(_DWORD *)(v2 + 16));
check_res_dumpdie(v8, v2, "bind");
v9 = listen(*((_DWORD *)*a1 + v5), 50);
check_res_dumpdie(v9, v2, "listen");
v2 = *(_QWORD *)(v2 + 40);
v10 = (int)v5++ + 1 < v4;
}
while ( v2 != 0 && v10 );
}
return (unsigned int)v4;
}

canonical_decompiled_code:
__int64 __fastcall func8(void **a1, __int64 a2)
{
__int64 v2; // rbx
__int64 v3; // rax
int v4; // er13
__int64 v5; // rbp
int *v6; // r14
int v7; // eax
int v8; // eax
int v9; // eax
bool v10; // dl
int optval[11]; // [rsp+Ch] [rbp-2Ch] BYREF

v2 = a2;
if ( !a2 )
{
v4 = verbose;
if ( !verbose )
{
*a1 = malloc(0LL);
return (unsigned int)v4;
}
v4 = 0;
goto LABEL_11;
}
v3 = a2;
v4 = 0;
do
{
++v4;
v3 = *(_QWORD *)(v3 + 40);
}
while ( v3 );
if ( verbose )
LABEL_11:
fprintf(stderr, "listening to %d addresses\n", (unsigned int)v4);
*a1 = malloc(4LL * v4);
if ( v4 > 0 && a2 )
{
v5 = 0LL;
do
{
v6 = (int *)((char *)*a1 + 4 * v5);
*v6 = socket(**(unsigned __int16 **)(v2 + 24), 1, 0);
func7(*((_DWORD *)*a1 + v5), v2, "socket");
optval[0] = 1;
v7 = setsockopt(*((_DWORD *)*a1 + v5), 1, 2, optval, 4u);
func7(v7, v2, "setsockopt");
v8 = bind(*((_DWORD *)*a1 + v5), *(const struct sockaddr **)(v2 + 24), *(_DWORD *)(v2 + 16));
func7(v8, v2, "bind");
v9 = listen(*((_DWORD *)*a1 + v5), 50);
func7(v9, v2, "listen");
v2 = *(_QWORD *)(v2 + 40);
v10 = (int)v5++ + 1 < v4;
}
while ( v2 != 0 && v10 );
}
return (unsigned int)v4;
}

original_code:
int start_listen_sockets(int *sockfd[], struct addrinfo *addr_list)
{
   struct sockaddr_storage *saddr;
   struct addrinfo *addr;
   int i, res, reuse;
   int num_addr = 0;
   for (addr = addr_list; addr; addr = addr->ai_next)
       num_addr++;
   if (verbose)
       fprintf(stderr, "listening to %d addresses\n", num_addr);
   *sockfd = malloc(num_addr * sizeof(*sockfd[0]));
   for (i = 0, addr = addr_list; i < num_addr && addr; i++, addr = addr->ai_next) {
       if (!addr) {
           fprintf(stderr, "FATAL: Inconsistent listen number. This should not happen.\n");
           exit(1);
       }
       saddr = (struct sockaddr_storage*)addr->ai_addr;
       (*sockfd)[i] = socket(saddr->ss_family, SOCK_STREAM, 0);
       check_res_dumpdie((*sockfd)[i], addr, "socket");
       reuse = 1;
       res = setsockopt((*sockfd)[i], 1, 2, (char*)&reuse, sizeof(reuse));
       check_res_dumpdie(res, addr, "setsockopt");
       res = bind((*sockfd)[i], addr->ai_addr, addr->ai_addrlen);
       check_res_dumpdie(res, addr, "bind");
       res = listen ((*sockfd)[i], 50);
       check_res_dumpdie(res, addr, "listen");
   }
   return num_addr;
}

canonical_original_code:
int start_listen_sockets(int *sockfd[], struct addrinfo *addr_list)
{
   struct sockaddr_storage *saddr;
   struct addrinfo *addr;
   int i; int res; int reuse;
   int num_addr = 0;
   for (addr = addr_list; addr; addr = addr->ai_next)
       num_addr++;
   if (verbose)
       fprintf(stderr, "listening to %d addresses\n", num_addr);
   *sockfd = malloc(num_addr * sizeof(*sockfd[0]));
   for (i = 0, addr = addr_list; i < num_addr && addr; i++, addr = addr->ai_next) {
       if (!addr) {
           fprintf(stderr, "FATAL: Inconsistent listen number. This should not happen.\n");
           exit(1);
       }
       saddr = (struct sockaddr_storage *)addr->ai_addr;
       (*sockfd)[i] = socket(saddr->ss_family, SOCK_STREAM, 0);
       check_res_dumpdie((*sockfd)[i], addr, "socket");
       reuse = 1;
       res = setsockopt((*sockfd)[i], 1, 2, (char *)&reuse, sizeof(reuse));
       check_res_dumpdie(res, addr, "setsockopt");
       res = bind((*sockfd)[i], addr->ai_addr, addr->ai_addrlen);
       check_res_dumpdie(res, addr, "bind");
       res = listen ((*sockfd)[i], 50);
       check_res_dumpdie(res, addr, "listen");
   }
   return num_addr;
}

variable_types:
sockfd
int *[]

variable_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; } *

variable_types: i int

variable_types: reuse int


decompiled_code:
__int64 __fastcall connect_addr(__int64 a1, const char *a2)
{
__int64 v2; // rbx
unsigned int v3; // ebp
unsigned int v4; // er13
unsigned int v5; // ebp
char *v6; // rax
int *v7; // rax
char *v8; // rax
int v9; // eax
int *v10; // rax
char *v11; // rax
char v13[1080]; // [rsp+0h] [rbp-438h] BYREF

v2 = a1;
if ( !a1 )
return (unsigned int)-1;
do
{
if ( verbose )
{
v4 = *(_DWORD *)(v2 + 16);
v5 = **(unsigned __int16 **)(v2 + 24);
v6 = sprintaddr(v13, 0x401uLL, v2);
fprintf(stderr, "connecting to %s family %d len %d\n", v6, v5, v4);
}
v9 = socket(*(_DWORD *)(v2 + 4), 1, 0);
v3 = v9;
if ( v9 == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
log_message(3, "forward to %s failed:socket: %s\n", a2, v8);
}
else
{
if ( connect(v9, *(const struct sockaddr **)(v2 + 24), *(_DWORD *)(v2 + 16)) != -1 )
return v3;
v10 = _errno_location();
v11 = strerror(*v10);
log_message(3, "forward to %s failed:connect: %s\n", a2, v11);
}
v2 = *(_QWORD *)(v2 + 40);
}
while ( v2 );
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, const char *a2)
{
__int64 v2; // rbx
unsigned int v3; // ebp
unsigned int v4; // er13
unsigned int v5; // ebp
char *v6; // rax
int *v7; // rax
char *v8; // rax
int v9; // eax
int *v10; // rax
char *v11; // rax
char v13[1080]; // [rsp+0h] [rbp-438h] BYREF

v2 = a1;
if ( !a1 )
return (unsigned int)-1;
do
{
if ( verbose )
{
v4 = *(_DWORD *)(v2 + 16);
v5 = **(unsigned __int16 **)(v2 + 24);
v6 = func6(v13, 0x401uLL, v2);
fprintf(stderr, "connecting to %s family %d len %d\n", v6, v5, v4);
}
v9 = socket(*(_DWORD *)(v2 + 4), 1, 0);
v3 = v9;
if ( v9 == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
func4(3, "forward to %s failed:socket: %s\n", a2, v8);
}
else
{
if ( connect(v9, *(const struct sockaddr **)(v2 + 24), *(_DWORD *)(v2 + 16)) != -1 )
return v3;
v10 = _errno_location();
v11 = strerror(*v10);
func4(3, "forward to %s failed:connect: %s\n", a2, v11);
}
v2 = *(_QWORD *)(v2 + 40);
}
while ( v2 );
return (unsigned int)-1;
}

original_code:
int connect_addr(struct addrinfo *addr, const char* cnx_name)
{
    struct addrinfo *a;
    char buf[1025];
    int fd, res;
    for (a = addr; a; a = a->ai_next) {
        if (verbose)
            fprintf(stderr, "connecting to %s family %d len %d\n",
                    sprintaddr(buf, sizeof(buf), a),
                    a->ai_addr->sa_family, a->ai_addrlen);
        fd = socket(a->ai_family, SOCK_STREAM, 0);
        if (fd == -1) {
            log_message(3, "forward to %s failed:socket: %s\n", cnx_name, strerror((*__errno_location ())));
        } else {
            res = connect(fd, a->ai_addr, a->ai_addrlen);
            if (res == -1) {
                log_message(3, "forward to %s failed:connect: %s\n",
                            cnx_name, strerror((*__errno_location ())));
            } else {
                return fd;
            }
        }
    }
    return -1;
}

canonical_original_code:
int connect_addr(struct addrinfo *addr, char *cnx_name)
{
    struct addrinfo *a;
    char buf[1025];
    int fd; int res;
    for (a = addr; a; a = a->ai_next) {
        if (verbose)
            fprintf(stderr, "connecting to %s family %d len %d\n",
                    sprintaddr(buf, sizeof(buf), a),
                    a->ai_addr->sa_family, a->ai_addrlen);
        fd = socket(a->ai_family, SOCK_STREAM, 0);
        if (fd == -1) {
            log_message(3, "forward to %s failed:socket: %s\n", cnx_name, strerror((*__errno_location ())));
        } else {
            res = connect(fd, a->ai_addr, a->ai_addrlen);
            if (res == -1) {
                log_message(3, "forward to %s failed:connect: %s\n",
                            cnx_name, strerror((*__errno_location ())));
            } else {
                return fd;
            }
        }
    }
    return -1;
}

variable_types: fd int


decompiled_code:
__int64 __fastcall resolve_split_name(struct addrinfo **pai, char *name, char *service)
{
int v4; // eax
unsigned int v5; // ebx
const char *v7; // rax
struct addrinfo v8; // [rsp+0h] [rbp-48h] BYREF

*(_QWORD *)&v8.ai_flags = 0LL;
*(_QWORD *)&v8.ai_socktype = 1LL;
*(_QWORD *)&v8.ai_addrlen = 0LL;
v8.ai_addr = 0LL;
v8.ai_canonname = 0LL;
v8.ai_next = 0LL;
v4 = getaddrinfo(name, service, &v8, pai);
v5 = v4;
if ( v4 )
{
v7 = gai_strerror(v4);
log_message(3, "%s `%s:%s'\n", v7, name, service);
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func10(struct addrinfo **pai, char *name, char *service)
{
int v4; // eax
unsigned int v5; // ebx
const char *v7; // rax
struct addrinfo v8; // [rsp+0h] [rbp-48h] BYREF

*(_QWORD *)&v8.ai_flags = 0LL;
*(_QWORD *)&v8.ai_socktype = 1LL;
*(_QWORD *)&v8.ai_addrlen = 0LL;
v8.ai_addr = 0LL;
v8.ai_canonname = 0LL;
v8.ai_next = 0LL;
v4 = getaddrinfo(name, service, &v8, pai);
v5 = v4;
if ( v4 )
{
v7 = gai_strerror(v4);
func4(3, "%s `%s:%s'\n", v7, name, service);
}
return v5;
}

original_code:
int resolve_split_name(struct addrinfo **out, const char* host, const char* serv)
{
   struct addrinfo hint;
   int res;
   memset(&hint, 0, sizeof(hint));
   hint.ai_family = 0;
   hint.ai_socktype = SOCK_STREAM;
   res = getaddrinfo(host, serv, &hint, out);
   if (res)
      log_message(3, "%s `%s:%s'\n", gai_strerror(res), host, serv);
   return res;
}

canonical_original_code:
int resolve_split_name(struct addrinfo **out, char *host, char *serv)
{
   struct addrinfo hint;
   int res;
   memset(&hint, 0, sizeof(hint));
   hint.ai_family = 0;
   hint.ai_socktype = SOCK_STREAM;
   res = getaddrinfo(host, serv, &hint, out);
   if (res)
      log_message(3, "%s `%s:%s'\n", gai_strerror(res), host, serv);
   return res;
}

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

variable_types:
host
char *

variable_types:
serv
char *


decompiled_code:
__int64 __fastcall resolve_name(struct addrinfo **pai, char *name)
{
char *v2; // rax
__int64 result; // rax
int v4; // ebp
const char *v5; // rax

v2 = strrchr(name, 58);
if ( !v2 )
{
fprintf(stderr, "%s: names must be fully specified as hostname:port\n", name);
exit(1);
}
*v2 = 0;
result = resolve_split_name(pai, name, v2 + 1);
v4 = result;
if ( (_DWORD)result )
{
v5 = gai_strerror(result);
fprintf(stderr, "%s `%s'\n", v5, name);
if ( v4 == -8 )
fwrite("(Check you have specified all ports)\n", 1uLL, 0x25uLL, stderr);
exit(4);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(struct addrinfo **pai, char *name)
{
char *v2; // rax
__int64 result; // rax
int v4; // ebp
const char *v5; // rax

v2 = strrchr(name, 58);
if ( !v2 )
{
fprintf(stderr, "%s: names must be fully specified as hostname:port\n", name);
exit(1);
}
*v2 = 0;
result = func10(pai, name, v2 + 1);
v4 = result;
if ( (_DWORD)result )
{
v5 = gai_strerror(result);
fprintf(stderr, "%s `%s'\n", v5, name);
if ( v4 == -8 )
fwrite("(Check you have specified all ports)\n", 1uLL, 0x25uLL, stderr);
exit(4);
}
return result;
}

original_code:
void resolve_name(struct addrinfo **out, char* fullname)
{
   char *serv, *host;
   int res;
   char *sep = strrchr(fullname, ':');
   if (!sep)
   {
      fprintf(stderr, "%s: names must be fully specified as hostname:port\n", fullname);
      exit(1);
   }
   host = fullname;
   serv = sep+1;
   *sep = 0;
   res = resolve_split_name(out, host, serv);
   if (res) {
      fprintf(stderr, "%s `%s'\n", gai_strerror(res), fullname);
      if (res == -8)
         fprintf(stderr, "(Check you have specified all ports)\n");
      exit(4);
   }
}

canonical_original_code:
void resolve_name(struct addrinfo **out, char *fullname)
{
   char *serv; char *host;
   int res;
   char *sep = strrchr(fullname, ':');
   if (!sep)
   {
      fprintf(stderr, "%s: names must be fully specified as hostname:port\n", fullname);
      exit(1);
   }
   host = fullname;
   serv = sep+1;
   *sep = 0;
   res = resolve_split_name(out, host, serv);
   if (res) {
      fprintf(stderr, "%s `%s'\n", gai_strerror(res), fullname);
      if (res == -8)
         fprintf(stderr, "(Check you have specified all ports)\n");
      exit(4);
   }
}

variable_types:
fullname
char *

variable_types:
sep
char *


decompiled_code:
void __fastcall log_connection(__int64 a1)
{
char v1[1072]; // [rsp+0h] [rbp-1178h] BYREF
char v2[1072]; // [rsp+430h] [rbp-D48h] BYREF
char v3[1072]; // [rsp+860h] [rbp-918h] BYREF
char s[1072]; // [rsp+C90h] [rbp-4E8h] BYREF
struct sockaddr addr; // [rsp+10C0h] [rbp-B8h] BYREF
char v6[16]; // [rsp+1140h] [rbp-38h] BYREF
socklen_t len; // [rsp+1150h] [rbp-28h] BYREF
struct sockaddr *v8; // [rsp+1158h] [rbp-20h]

v8 = &addr;
len = 128;
if ( getpeername(*(_DWORD *)(a1 + 16), &addr, &len) != -1 )
{
sprintaddr(s, 0x422uLL, (__int64)v6);
len = 128;
if ( getsockname(*(_DWORD *)(a1 + 16), v8, &len) != -1 )
{
sprintaddr(v3, 0x422uLL, (__int64)v6);
len = 128;
if ( getpeername(*(_DWORD *)(a1 + 48), v8, &len) != -1 )
{
sprintaddr(v1, 0x422uLL, (__int64)v6);
len = 128;
if ( getsockname(*(_DWORD *)(a1 + 48), v8, &len) != -1 )
{
sprintaddr(v2, 0x422uLL, (__int64)v6);
log_message(6, "connection from %s to %s forwarded from %s to %s\n", s, v3, v2, v1);
}
}
}
}
}

canonical_decompiled_code:
void __fastcall func12(__int64 a1)
{
char v1[1072]; // [rsp+0h] [rbp-1178h] BYREF
char v2[1072]; // [rsp+430h] [rbp-D48h] BYREF
char v3[1072]; // [rsp+860h] [rbp-918h] BYREF
char s[1072]; // [rsp+C90h] [rbp-4E8h] BYREF
struct sockaddr addr; // [rsp+10C0h] [rbp-B8h] BYREF
char v6[16]; // [rsp+1140h] [rbp-38h] BYREF
socklen_t len; // [rsp+1150h] [rbp-28h] BYREF
struct sockaddr *v8; // [rsp+1158h] [rbp-20h]

v8 = &addr;
len = 128;
if ( getpeername(*(_DWORD *)(a1 + 16), &addr, &len) != -1 )
{
func6(s, 0x422uLL, (__int64)v6);
len = 128;
if ( getsockname(*(_DWORD *)(a1 + 16), v8, &len) != -1 )
{
func6(v3, 0x422uLL, (__int64)v6);
len = 128;
if ( getpeername(*(_DWORD *)(a1 + 48), v8, &len) != -1 )
{
func6(v1, 0x422uLL, (__int64)v6);
len = 128;
if ( getsockname(*(_DWORD *)(a1 + 48), v8, &len) != -1 )
{
func6(v2, 0x422uLL, (__int64)v6);
func4(6, "connection from %s to %s forwarded from %s to %s\n", s, v3, v2, v1);
}
}
}
}
}

original_code:
void log_connection(struct connection *cnx)
{
    struct addrinfo addr;
    struct sockaddr_storage ss;
    char peer[(1025 + 32 + 1)], service[(1025 + 32 + 1)],
        local[(1025 + 32 + 1)], target[(1025 + 32 + 1)];
    int res;
    addr.ai_addr = (struct sockaddr*)&ss;
    addr.ai_addrlen = sizeof(ss);
    res = getpeername(cnx->q[0].fd, addr.ai_addr, &addr.ai_addrlen);
    if (res == -1) return;
    sprintaddr(peer, sizeof(peer), &addr);
    addr.ai_addrlen = sizeof(ss);
    res = getsockname(cnx->q[0].fd, addr.ai_addr, &addr.ai_addrlen);
    if (res == -1) return;
    sprintaddr(service, sizeof(service), &addr);
    addr.ai_addrlen = sizeof(ss);
    res = getpeername(cnx->q[1].fd, addr.ai_addr, &addr.ai_addrlen);
    if (res == -1) return;
    sprintaddr(target, sizeof(target), &addr);
    addr.ai_addrlen = sizeof(ss);
    res = getsockname(cnx->q[1].fd, addr.ai_addr, &addr.ai_addrlen);
    if (res == -1) return;
    sprintaddr(local, sizeof(local), &addr);
    log_message(6, "connection from %s to %s forwarded from %s to %s\n",
                peer,
                service,
                local,
                target);
}

canonical_original_code:
void log_connection(struct connection *cnx)
{
    struct addrinfo addr;
    struct sockaddr_storage ss;
    char peer[-1]; char service[-1]; char local[-1]; char target[-1];
    int res;
    addr.ai_addr = (struct sockaddr *)&ss;
    addr.ai_addrlen = sizeof(ss);
    res = getpeername(cnx->q[0].fd, addr.ai_addr, &addr.ai_addrlen);
    if (res == -1) return;
    sprintaddr(peer, sizeof(peer), &addr);
    addr.ai_addrlen = sizeof(ss);
    res = getsockname(cnx->q[0].fd, addr.ai_addr, &addr.ai_addrlen);
    if (res == -1) return;
    sprintaddr(service, sizeof(service), &addr);
    addr.ai_addrlen = sizeof(ss);
    res = getpeername(cnx->q[1].fd, addr.ai_addr, &addr.ai_addrlen);
    if (res == -1) return;
    sprintaddr(target, sizeof(target), &addr);
    addr.ai_addrlen = sizeof(ss);
    res = getsockname(cnx->q[1].fd, addr.ai_addr, &addr.ai_addrlen);
    if (res == -1) return;
    sprintaddr(local, sizeof(local), &addr);
    log_message(6, "connection from %s to %s forwarded from %s to %s\n",
                peer,
                service,
                local,
                target);
}

variable_types: struct connection { enum connection_state { ST_PROBING=1, ST_SHOVELING=2 } state; long probe_timeout; struct queue { int fd; void *begin_defered_data; void *defered_data; int defered_data_size; } q[2]; } *

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

variable_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }

variable_types:
peer
char [-1]

variable_types:
service
char [-1]

variable_types:
local
char [-1]

variable_types:
target
char [-1]


decompiled_code:
__int64 check_access_rights()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func13()
{
return 0LL;
}

original_code:
int check_access_rights(int in_socket, const char* service)
{
    return 0;
}

canonical_original_code:
int check_access_rights(int in_socket, char *service)
{
    return 0;
}

variable_types:
service
char *


decompiled_code:
int setup_signals()
{
int result; // eax
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

memset(&v1, 0, sizeof(v1));
v1.sa_flags = 2;
if ( sigaction(17, &v1, 0LL) == -1 )
{
perror("sigaction");
exit(1);
}
memset(&v1.sa_mask, 0, 0x90uLL);
v1.sa_handler = (__sighandler_t)&exit;
if ( sigaction(15, &v1, 0LL) == -1 )
{
perror("sigaction");
exit(1);
}
v1.sa_handler = (__sighandler_t)(defer_write + 1);
result = sigaction(13, &v1, 0LL);
if ( result == -1 )
{
perror("sigaction");
exit(1);
}
return result;
}

canonical_decompiled_code:
int func14()
{
int result; // eax
struct sigaction v1; // [rsp+0h] [rbp-A8h] BYREF

memset(&v1, 0, sizeof(v1));
v1.sa_flags = 2;
if ( sigaction(17, &v1, 0LL) == -1 )
{
perror("sigaction");
exit(1);
}
memset(&v1.sa_mask, 0, 0x90uLL);
v1.sa_handler = (__sighandler_t)&exit;
if ( sigaction(15, &v1, 0LL) == -1 )
{
perror("sigaction");
exit(1);
}
v1.sa_handler = (__sighandler_t)(defer_write + 1);
result = sigaction(13, &v1, 0LL);
if ( result == -1 )
{
perror("sigaction");
exit(1);
}
return result;
}

original_code:
void setup_signals(void)
{
    int res;
    struct sigaction action;
    memset(&action, 0, sizeof(action));
    action.__sigaction_handler.sa_handler = ((void *)0);
    action.sa_flags = 2;
    res = sigaction(17, &action, ((void *)0));
    if (res == -1) { perror("sigaction"); exit(1); };
    memset(&action, 0, sizeof(action));
    action.__sigaction_handler.sa_handler = exit;
    res = sigaction(15, &action, ((void *)0));
    if (res == -1) { perror("sigaction"); exit(1); };
    action.__sigaction_handler.sa_handler = ((__sighandler_t) 1);
    res = sigaction(13, &action, ((void *)0));
    if (res == -1) { perror("sigaction"); exit(1); };
}

canonical_original_code:
void setup_signals()
{
    int res;
    struct sigaction action;
    memset(&action, 0, sizeof(action));
    action.__sigaction_handler.sa_handler = ((void *)0);
    action.sa_flags = 2;
    res = sigaction(17, &action, ((void *)0));
    if (res == -1) { perror("sigaction"); exit(1); };
    memset(&action, 0, sizeof(action));
    action.__sigaction_handler.sa_handler = exit;
    res = sigaction(15, &action, ((void *)0));
    if (res == -1) { perror("sigaction"); exit(1); };
    action.__sigaction_handler.sa_handler = ((void (*)(int)) 1);
    res = sigaction(13, &action, ((void *)0));
    if (res == -1) { perror("sigaction"); exit(1); };
}

variable_types: res int

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


decompiled_code:
void __fastcall setup_syslog(const char *a1)
{
char *v1; // rbx
unsigned int v2; // ebp
char *v3; // rax
char *ptr; // [rsp+8h] [rbp-20h] BYREF

v1 = strdup(a1);
v2 = getpid();
v3 = _xpg_basename(v1);
asprintf(&ptr, "%s[%d]", v3, v2);
openlog(ptr, 2, 32);
free(v1);
log_message(6, "%s %s started\n", server_type, "1.14");
}

canonical_decompiled_code:
void __fastcall func15(const char *a1)
{
char *v1; // rbx
unsigned int v2; // ebp
char *v3; // rax
char *ptr; // [rsp+8h] [rbp-20h] BYREF

v1 = strdup(a1);
v2 = getpid();
v3 = _xpg_basename(v1);
asprintf(&ptr, "%s[%d]", v3, v2);
openlog(ptr, 2, 32);
free(v1);
func4(6, "%s %s started\n", server_type, "1.14");
}

original_code:
void setup_syslog(const char* bin_name) {
    char *name1, *name2;
    name1 = strdup(bin_name);
    asprintf(&name2, "%s[%d]", __xpg_basename(name1), getpid());
    openlog(name2, 0x02, (4<<3));
    free(name1);
    log_message(6, "%s %s started\n", server_type, "1.14");
}

canonical_original_code:
void setup_syslog(char *bin_name) {
    char *name1; char *name2;
    name1 = strdup(bin_name);
    asprintf(&name2, "%s[%d]", __xpg_basename(name1), getpid());
    openlog(name2, 0x02, (4<<3));
    free(name1);
    log_message(6, "%s %s started\n", server_type, "1.14");
}


decompiled_code:
int __fastcall drop_privileges(const char *a1)
{
struct passwd *v1; // rax
struct passwd *v2; // rbx

v1 = getpwnam(a1);
if ( !v1 )
{
fprintf(stderr, "%s: not found\n", a1);
exit(2);
}
v2 = v1;
if ( verbose )
fprintf(stderr, "turning into %s\n", a1);
if ( setgid(v2->pw_gid) == -1 )
{
perror("setgid");
exit(1);
}
return setuid(v2->pw_uid);
}

canonical_decompiled_code:
int __fastcall func16(const char *a1)
{
struct passwd *v1; // rax
struct passwd *v2; // rbx

v1 = getpwnam(a1);
if ( !v1 )
{
fprintf(stderr, "%s: not found\n", a1);
exit(2);
}
v2 = v1;
if ( verbose )
fprintf(stderr, "turning into %s\n", a1);
if ( setgid(v2->pw_gid) == -1 )
{
perror("setgid");
exit(1);
}
return setuid(v2->pw_uid);
}

original_code:
void drop_privileges(const char* user_name)
{
    int res;
    struct passwd *pw = getpwnam(user_name);
    if (!pw) {
        fprintf(stderr, "%s: not found\n", user_name);
        exit(2);
    }
    if (verbose)
        fprintf(stderr, "turning into %s\n", user_name);
    res = setgid(pw->pw_gid);
    if (res == -1) { perror("setgid"); exit(1); };
    setuid(pw->pw_uid);
    if (res == -1) { perror("setuid"); exit(1); };
}

canonical_original_code:
void drop_privileges(char *user_name)
{
    int res;
    struct passwd *pw = getpwnam(user_name);
    if (!pw) {
        fprintf(stderr, "%s: not found\n", user_name);
        exit(2);
    }
    if (verbose)
        fprintf(stderr, "turning into %s\n", user_name);
    res = setgid(pw->pw_gid);
    if (res == -1) { perror("setgid"); exit(1); };
    setuid(pw->pw_uid);
    if (res == -1) { perror("setuid"); exit(1); };
}

variable_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; } *


decompiled_code:
void make_alloptions()
{
int v0; // eax
int v1; // eax

builtins = get_builtins();
v0 = get_num_builtins();
all_options = (struct option *)calloc(v0 + 12LL, 0x20uLL);
qmemcpy(all_options, &const_options, 0x180uLL);
v1 = get_num_builtins();
append_protocols((__int64)all_options, 11, (_QWORD *)builtins, v1);
}

canonical_decompiled_code:
void func1()
{
int v0; // eax
int v1; // eax

builtins = get_builtins();
v0 = get_num_builtins();
all_options = (struct option *)calloc(v0 + 12LL, 0x20uLL);
qmemcpy(all_options, &const_options, 0x180uLL);
v1 = get_num_builtins();
func0((__int64)all_options, 11, (_QWORD *)builtins, v1);
}

original_code:
static void make_alloptions(void)
{
    builtins = get_builtins();
    all_options = calloc((sizeof(const_options) / sizeof(const_options[0])) + get_num_builtins(), sizeof(struct option));
    memcpy(all_options, const_options, sizeof(const_options));
    append_protocols(all_options, (sizeof(const_options) / sizeof(const_options[0])) - 1, builtins, get_num_builtins());
}

canonical_original_code:
static void make_alloptions()
{
    builtins = get_builtins();
    all_options = calloc((sizeof(const_options) / sizeof(const_options[0])) + get_num_builtins(), sizeof(struct option));
    memcpy(all_options, const_options, sizeof(const_options));
    append_protocols(all_options, (sizeof(const_options) / sizeof(const_options[0])) - 1, builtins, get_num_builtins());
}

user_defined_types: struct option { char *name; int has_arg; int *flag; int val; }


decompiled_code:
int print_usage()
{
const char **v0; // rbx
int i; // ebp
char *ptr; // [rsp+8h] [rbp-20h] BYREF

ptr = (char *)&unk_C44;
v0 = (const char **)get_builtins();
for ( i = 0; (int)get_num_builtins() > i; ++i )
{
asprintf(&ptr, "%s\t[--%s <addr>]\n", ptr, *v0);
v0 += 6;
}
return fprintf(stderr, USAGE_STRING, ptr);
}

canonical_decompiled_code:
int func7()
{
const char **v0; // rbx
int i; // ebp
char *ptr; // [rsp+8h] [rbp-20h] BYREF

ptr = (char *)&unk_C44;
v0 = (const char **)get_builtins();
for ( i = 0; (int)get_num_builtins() > i; ++i )
{
asprintf(&ptr, "%s\t[--%s <addr>]\n", ptr, *v0);
v0 += 6;
}
return fprintf(stderr, USAGE_STRING, ptr);
}

original_code:
static void print_usage(void)
{
    struct proto *p;
    int i;
    char *prots = "";
    p = get_builtins();
    for (i = 0; i < get_num_builtins(); i++)
        asprintf(&prots, "%s\t[--%s <addr>]\n", prots, p[i].description);
    fprintf(stderr, USAGE_STRING, prots);
}

canonical_original_code:
static void print_usage()
{
    struct proto *p;
    int i;
    char *prots = "";
    p = get_builtins();
    for (i = 0; i < get_num_builtins(); i++)
        asprintf(&prots, "%s\t[--%s <addr>]\n", prots, p[i].description);
    fprintf(stderr, USAGE_STRING, prots);
}

variable_types: struct proto { char *description; char *service; struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *saddr; int (*probe)(char *, int, struct proto *); void *data; struct proto *next; } *

variable_types: i int

variable_types:
prots
char *

user_defined_types: struct proto { char *description; char *service; struct addrinfo *saddr; int (*probe)(char *, int, struct proto *); void *data; struct proto *next; }


printsettings

decompiled_code:
int printsettings()
{
__int64 i; // rbx
__int64 v1; // rdx
__int64 v2; // r13
unsigned int v3; // er12
const char *v4; // rbp
const char *v5; // rax
__int64 j; // rbx
const char *v7; // rax
const char **v8; // rax
_BYTE v10[1080]; // [rsp+0h] [rbp-438h] BYREF

for ( i = get_first_protocol(); i; i = *(_QWORD *)(i + 40) )
{
v1 = *(_QWORD *)(i + 16);
v2 = **(unsigned __int16 **)(v1 + 24);
v3 = *(_DWORD *)(v1 + 4);
v4 = *(const char **)(i + 8);
v5 = (const char *)sprintaddr(v10, 1025LL);
fprintf(stderr, "%s addr: %s. libwrap service: %s family %d %d\n", *(const char **)i, v5, v4, v3, v2);
}
fwrite("listening on:\n", 1uLL, 0xEuLL, stderr);
for ( j = addr_listen; j; j = *(_QWORD *)(j + 40) )
{
v7 = (const char *)sprintaddr(v10, 1025LL);
fprintf(stderr, "\t%s\n", v7);
}
v8 = (const char **)timeout_protocol();
return fprintf(stderr, "timeout: %d\non-timeout: %s\n", probing_timeout, *v8);
}

canonical_decompiled_code:
int func9()
{
__int64 i; // rbx
__int64 v1; // rdx
__int64 v2; // r13
unsigned int v3; // er12
const char *v4; // rbp
const char *v5; // rax
__int64 j; // rbx
const char *v7; // rax
const char **v8; // rax
_BYTE v10[1080]; // [rsp+0h] [rbp-438h] BYREF

for ( i = get_first_protocol(); i; i = *(_QWORD *)(i + 40) )
{
v1 = *(_QWORD *)(i + 16);
v2 = **(unsigned __int16 **)(v1 + 24);
v3 = *(_DWORD *)(v1 + 4);
v4 = *(const char **)(i + 8);
v5 = (const char *)sprintaddr(v10, 1025LL);
fprintf(stderr, "%s addr: %s. libwrap service: %s family %d %d\n", *(const char **)i, v5, v4, v3, v2);
}
fwrite("listening on:\n", 1uLL, 0xEuLL, stderr);
for ( j = addr_listen; j; j = *(_QWORD *)(j + 40) )
{
v7 = (const char *)sprintaddr(v10, 1025LL);
fprintf(stderr, "\t%s\n", v7);
}
v8 = (const char **)timeout_protocol();
return fprintf(stderr, "timeout: %d\non-timeout: %s\n", probing_timeout, *v8);
}

original_code:
static void printsettings(void)
{
    char buf[1025];
    struct addrinfo *a;
    struct proto *p;
    for (p = get_first_protocol(); p; p = p->next) {
        fprintf(stderr,
                "%s addr: %s. libwrap service: %s family %d %d\n",
                p->description,
                sprintaddr(buf, sizeof(buf), p->saddr),
                p->service,
                p->saddr->ai_family,
                p->saddr->ai_addr->sa_family);
    }
    fprintf(stderr, "listening on:\n");
    for (a = addr_listen; a; a = a->ai_next) {
        fprintf(stderr, "\t%s\n", sprintaddr(buf, sizeof(buf), a));
    }
    fprintf(stderr, "timeout: %d\non-timeout: %s\n", probing_timeout,
            timeout_protocol()->description);
}

canonical_original_code:
static void printsettings()
{
    char buf[1025];
    struct addrinfo *a;
    struct proto *p;
    for (p = get_first_protocol(); p; p = p->next) {
        fprintf(stderr,
                "%s addr: %s. libwrap service: %s family %d %d\n",
                p->description,
                sprintaddr(buf, sizeof(buf), p->saddr),
                p->service,
                p->saddr->ai_family,
                p->saddr->ai_addr->sa_family);
    }
    fprintf(stderr, "listening on:\n");
    for (a = addr_listen; a; a = a->ai_next) {
        fprintf(stderr, "\t%s\n", sprintaddr(buf, sizeof(buf), a));
    }
    fprintf(stderr, "timeout: %d\non-timeout: %s\n", probing_timeout,
            timeout_protocol()->description);
}

variable_types:
buf
char [1025]

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


decompiled_code:
__int64 __fastcall parse_cmdline(int argc, char **argv, __m128i *a3)
{
__m128i *v5; // rax
const __m128i *v6; // rdx
int v7; // ebx
__m128i *v8; // rbp
__m128i *v9; // r15
__int64 v10; // rax
void *v11; // rdi
__int64 v12; // rdi
__int64 result; // rax
const char **v14; // [rsp+0h] [rbp-48h]
__int64 v15; // [rsp+8h] [rbp-40h]

optind = 1;
opterr = 1;
while ( 1 )
{
v7 = getopt_long_only(argc, argv, "vt:T:p:VP:F:", all_options, 0LL);
if ( v7 == -1 )
break;
if ( v7 )
{
if ( v7 <= 999 )
{
if ( v7 <= 118 )
{
if ( v7 > 69 )
{
switch ( v7 )
{
case 'F':
continue;
case 'P':
pid_file = optarg;
continue;
case 'V':
printf("%s %s\n", server_type, "1.14");
exit(0);
case 'p':
v10 = addr_listen;
v11 = &addr_listen;
if ( addr_listen )
{
do
{
v12 = v10;
v10 = *(_QWORD *)(v10 + 40);
}
while ( v10 );
v11 = (void *)(v12 + 40);
}
resolve_name(v11, optarg);
continue;
case 't':
probing_timeout = atoi(optarg);
continue;
case 'u':
user_name = optarg;
continue;
case 'v':
++verbose;
continue;
default:
break;
}
}
LABEL_26:
print_usage();
exit(2);
}
if ( v7 != 257 )
goto LABEL_26;
set_ontimeout(optarg);
}
else if ( a3 )
{
v15 = 48LL * v7 - 48000;
v14 = (const char **)(builtins + v15);
v8 = a3;
do
{
v9 = v8;
v8 = (__m128i *)v8[2].m128i_i64[1];
if ( !v8 )
{
v5 = (__m128i *)calloc(1uLL, 0x30uLL);
v9[2].m128i_i64[1] = (__int64)v5;
goto LABEL_3;
}
}
while ( strcmp((const char *)v9->m128i_i64[0], *v14) );
resolve_name(&v9[1], optarg);
v9[1].m128i_i64[1] = *(_QWORD *)(builtins + v15 + 24);
}
else
{
v5 = (__m128i *)calloc(1uLL, 0x30uLL);
a3 = v5;
LABEL_3:
v6 = (const __m128i *)(builtins + 48LL * v7 - 48000);
*v5 = _mm_loadu_si128(v6);
v5[1] = _mm_loadu_si128(v6 + 1);
v5[2] = _mm_loadu_si128(v6 + 2);
resolve_name(&v5[1], optarg);
}
}
}
if ( !a3 )
{
fwrite("At least one target protocol must be specified.\n", 1uLL, 0x30uLL, stderr);
exit(2);
}
result = set_protocol_list(a3);
if ( !addr_listen )
{
fwrite("No listening address specified; use at least one -p option\n", 1uLL, 0x3BuLL, stderr);
exit(1);
}
if ( background )
foreground = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(int argc, char **argv, __m128i *a3)
{
__m128i *v5; // rax
const __m128i *v6; // rdx
int v7; // ebx
__m128i *v8; // rbp
__m128i *v9; // r15
__int64 v10; // rax
void *v11; // rdi
__int64 v12; // rdi
__int64 result; // rax
const char **v14; // [rsp+0h] [rbp-48h]
__int64 v15; // [rsp+8h] [rbp-40h]

optind = 1;
opterr = 1;
while ( 1 )
{
v7 = getopt_long_only(argc, argv, "vt:T:p:VP:F:", all_options, 0LL);
if ( v7 == -1 )
break;
if ( v7 )
{
if ( v7 <= 999 )
{
if ( v7 <= 118 )
{
if ( v7 > 69 )
{
switch ( v7 )
{
case 'F':
continue;
case 'P':
pid_file = optarg;
continue;
case 'V':
printf("%s %s\n", server_type, "1.14");
exit(0);
case 'p':
v10 = addr_listen;
v11 = &addr_listen;
if ( addr_listen )
{
do
{
v12 = v10;
v10 = *(_QWORD *)(v10 + 40);
}
while ( v10 );
v11 = (void *)(v12 + 40);
}
resolve_name(v11, optarg);
continue;
case 't':
probing_timeout = atoi(optarg);
continue;
case 'u':
user_name = optarg;
continue;
case 'v':
++verbose;
continue;
default:
break;
}
}
LABEL_26:
func7();
exit(2);
}
if ( v7 != 257 )
goto LABEL_26;
set_ontimeout(optarg);
}
else if ( a3 )
{
v15 = 48LL * v7 - 48000;
v14 = (const char **)(builtins + v15);
v8 = a3;
do
{
v9 = v8;
v8 = (__m128i *)v8[2].m128i_i64[1];
if ( !v8 )
{
v5 = (__m128i *)calloc(1uLL, 0x30uLL);
v9[2].m128i_i64[1] = (__int64)v5;
goto LABEL_3;
}
}
while ( strcmp((const char *)v9->m128i_i64[0], *v14) );
resolve_name(&v9[1], optarg);
v9[1].m128i_i64[1] = *(_QWORD *)(builtins + v15 + 24);
}
else
{
v5 = (__m128i *)calloc(1uLL, 0x30uLL);
a3 = v5;
LABEL_3:
v6 = (const __m128i *)(builtins + 48LL * v7 - 48000);
*v5 = _mm_loadu_si128(v6);
v5[1] = _mm_loadu_si128(v6 + 1);
v5[2] = _mm_loadu_si128(v6 + 2);
resolve_name(&v5[1], optarg);
}
}
}
if ( !a3 )
{
fwrite("At least one target protocol must be specified.\n", 1uLL, 0x30uLL, stderr);
exit(2);
}
result = set_protocol_list(a3);
if ( !addr_listen )
{
fwrite("No listening address specified; use at least one -p option\n", 1uLL, 0x3BuLL, stderr);
exit(1);
}
if ( background )
foreground = 0;
return result;
}

original_code:
static void parse_cmdline(int argc, char* argv[], struct proto* prots)
{
    int c;
    struct addrinfo **a;
    struct proto *p;
    optind = 1;
    opterr = 1;
next_arg:
    while ((c = getopt_long_only(argc, argv, optstr, all_options, ((void *)0))) != -1) {
        if (c == 0) continue;
        if (c >= 1000) {
            if (prots)
                for (p = prots; p && p->next; p = p->next) {
                    if (!strcmp(p->description, builtins[c-1000].description)) {
                        resolve_name(&(p->saddr), optarg);
                        p->probe = builtins[c-1000].probe;
                        goto next_arg;
                    }
                }
            if (!prots) {
                p = prots = calloc(1, sizeof(*p));
            } else {
                p->next = calloc(1, sizeof(*p));
                p = p->next;
            }
            memcpy(p, &builtins[c-1000], sizeof(*p));
            resolve_name(&(p->saddr), optarg);
            continue;
        }
        switch (c) {
        case 'F':
            break;
        case 't':
             probing_timeout = atoi(optarg);
            break;
        case 257:
            set_ontimeout(optarg);
            break;
        case 'p':
            for (a = &addr_listen; *a; a = &((*a)->ai_next));
            resolve_name(a, optarg);
            break;
        case 'V':
            printf("%s %s\n", server_type, "1.14");
            exit(0);
        case 'u':
            user_name = optarg;
            break;
        case 'P':
            pid_file = optarg;
            break;
        case 'v':
            verbose++;
            break;
        default:
            print_usage();
            exit(2);
        }
    }
    if (!prots) {
        fprintf(stderr, "At least one target protocol must be specified.\n");
        exit(2);
    }
    set_protocol_list(prots);
    if (!addr_listen) {
        fprintf(stderr, "No listening address specified; use at least one -p option\n");
        exit(1);
    }
    if (background)
        foreground = 0;
}

canonical_original_code:
static void parse_cmdline(int argc, char *argv[], struct proto *prots)
{
    int c;
    struct addrinfo **a;
    struct proto *p;
    optind = 1;
    opterr = 1;
next_arg:
    while ((c = getopt_long_only(argc, argv, optstr, all_options, ((void *)0))) != -1) {
        if (c == 0) continue;
        if (c >= 1000) {
            if (prots)
                for (p = prots; p && p->next; p = p->next) {
                    if (!strcmp(p->description, builtins[c-1000].description)) {
                        resolve_name(&(p->saddr), optarg);
                        p->probe = builtins[c-1000].probe;
                        goto next_arg;
                    }
                }
            if (!prots) {
                p = prots = calloc(1, sizeof(*p));
            } else {
                p->next = calloc(1, sizeof(*p));
                p = p->next;
            }
            memcpy(p, &builtins[c-1000], sizeof(*p));
            resolve_name(&(p->saddr), optarg);
            continue;
        }
        switch (c) {
        case 'F':
            break;
        case 't':
             probing_timeout = atoi(optarg);
            break;
        case 257:
            set_ontimeout(optarg);
            break;
        case 'p':
            for (a = &addr_listen; *a; a = &((*a)->ai_next));
            resolve_name(a, optarg);
            break;
        case 'V':
            printf("%s %s\n", server_type, "1.14");
            exit(0);
        case 'u':
            user_name = optarg;
            break;
        case 'P':
            pid_file = optarg;
            break;
        case 'v':
            verbose++;
            break;
        default:
            print_usage();
            exit(2);
        }
    }
    if (!prots) {
        fprintf(stderr, "At least one target protocol must be specified.\n");
        exit(2);
    }
    set_protocol_list(prots);
    if (!addr_listen) {
        fprintf(stderr, "No listening address specified; use at least one -p option\n");
        exit(1);
    }
    if (background)
        foreground = 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: c int

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


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
__int64 v5; // [rsp+0h] [rbp-28h] BYREF
__m128i *v6; // [rsp+8h] [rbp-20h] BYREF

v6 = 0LL;
pid_file = 0LL;
user_name = 0LL;
cmdline_config(argc, (char **)argv, &v6);
parse_cmdline(argc, (char **)argv, v6);
if ( inetd )
{
verbose = 0;
start_shoveler(0LL);
exit(0);
}
if ( verbose )
printsettings();
v3 = start_listen_sockets(&v5, addr_listen);
if ( !foreground )
{
if ( fork() > 0 )
exit(0);
if ( !getuid() && setsid() == -1 )
{
perror("setsid: already process leader");
exit(1);
}
}
setup_signals();
if ( pid_file )
write_pid_file();
if ( user_name )
drop_privileges();
setup_syslog(*argv);
main_loop(v5, v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
__int64 v5; // [rsp+0h] [rbp-28h] BYREF
__m128i *v6; // [rsp+8h] [rbp-20h] BYREF

v6 = 0LL;
pid_file = 0LL;
user_name = 0LL;
func6(argc, (char **)argv, &v6);
func8(argc, (char **)argv, v6);
if ( inetd )
{
verbose = 0;
start_shoveler(0LL);
exit(0);
}
if ( verbose )
func9();
v3 = start_listen_sockets(&v5, addr_listen);
if ( !foreground )
{
if ( fork() > 0 )
exit(0);
if ( !getuid() && setsid() == -1 )
{
perror("setsid: already process leader");
exit(1);
}
}
setup_signals();
if ( pid_file )
write_pid_file();
if ( user_name )
drop_privileges();
setup_syslog(*argv);
main_loop(v5, v3);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
   extern char *optarg;
   extern int optind;
   int res, num_addr_listen;
   struct proto* protocols = ((void *)0);
   int *listen_sockets;
   pid_file = ((void *)0);
   user_name = ((void *)0);
   cmdline_config(argc, argv, &protocols);
   parse_cmdline(argc, argv, protocols);
   if (inetd)
   {
       verbose = 0;
       start_shoveler(0);
       exit(0);
   }
   if (verbose)
       printsettings();
   num_addr_listen = start_listen_sockets(&listen_sockets, addr_listen);
   if (!foreground) {
       if (fork() > 0) exit(0);
       if (getuid() == 0) {
           res = setsid();
           if (res == -1) { perror("setsid: already process leader"); exit(1); };
       }
   }
   setup_signals();
   if (pid_file)
       write_pid_file(pid_file);
   if (user_name)
       drop_privileges(user_name);
   setup_syslog(argv[0]);
   main_loop(listen_sockets, num_addr_listen);
   return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
   extern char *optarg;
   extern int optind;
   int res; int num_addr_listen;
   struct proto *protocols = ((void *)0);
   int *listen_sockets;
   pid_file = ((void *)0);
   user_name = ((void *)0);
   cmdline_config(argc, argv, &protocols);
   parse_cmdline(argc, argv, protocols);
   if (inetd)
   {
       verbose = 0;
       start_shoveler(0);
       exit(0);
   }
   if (verbose)
       printsettings();
   num_addr_listen = start_listen_sockets(&listen_sockets, addr_listen);
   if (!foreground) {
       if (fork() > 0) exit(0);
       if (getuid() == 0) {
           res = setsid();
           if (res == -1) { perror("setsid: already process leader"); exit(1); };
       }
   }
   setup_signals();
   if (pid_file)
       write_pid_file(pid_file);
   if (user_name)
       drop_privileges(user_name);
   setup_syslog(argv[0]);
   main_loop(listen_sockets, num_addr_listen);
   return 0;
}

variable_types:
optarg
char *

variable_types: optind int


decompiled_code:
__int64 is_true()
{
return 1LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 1LL;
}

original_code: static int is_true(const char *p, int len, struct proto* proto) { return 1; }

canonical_original_code: static int is_true(char *p, int len, struct proto *proto) { return 1; }

variable_types:
p
char *

variable_types: len int


decompiled_code:
_BOOL8 __fastcall is_tls_protocol(_BYTE *a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( *a1 == 22 && a1[1] == 3 )
result = a1[2] <= 3u;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(_BYTE *a1)
{
_BOOL8 result; // rax

result = 0LL;
if ( *a1 == 22 && a1[1] == 3 )
result = a1[2] <= 3u;
return result;
}

original_code:
static int is_tls_protocol(const char *p, int len, struct proto *proto)
{
    return p[0] == 0x16 && p[1] == 0x03 && ( p[2] >= 0 && p[2] <= 0x03);
}

canonical_original_code:
static int is_tls_protocol(char *p, int len, struct proto *proto)
{
    return p[0] == 0x16 && p[1] == 0x03 && ( p[2] >= 0 && p[2] <= 0x03);
}


decompiled_code:
_BOOL8 __fastcall is_xmpp_protocol(const char *a1)
{
return strstr(a1, "jabber") != 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(const char *a1)
{
return strstr(a1, "jabber") != 0LL;
}

original_code:
static int is_xmpp_protocol( const char *p, int len, struct proto *proto)
{
    return strstr(p, "jabber") ? 1 : 0;
}

canonical_original_code:
static int is_xmpp_protocol(char *p, int len, struct proto *proto)
{
    return strstr(p, "jabber") ? 1 : 0;
}


decompiled_code:
_BOOL8 __fastcall is_http_protocol(const char *a1)
{
return strstr(a1, "HTTP") != 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(const char *a1)
{
return strstr(a1, "HTTP") != 0LL;
}

original_code:
static int is_http_protocol(const char *p, int len, struct proto *proto)
{
    if (strstr(p, "HTTP"))
        return 1;
    probe_http_method(p, "OPTIONS");
    probe_http_method(p, "GET");
    probe_http_method(p, "HEAD");
    probe_http_method(p, "POST");
    probe_http_method(p, "PUT");
    probe_http_method(p, "DELETE");
    probe_http_method(p, "TRACE");
    probe_http_method(p, "CONNECT");
    return 0;
}

canonical_original_code:
static int is_http_protocol(char *p, int len, struct proto *proto)
{
    if (strstr(p, "HTTP"))
        return 1;
    probe_http_method(p, "OPTIONS");
    probe_http_method(p, "GET");
    probe_http_method(p, "HEAD");
    probe_http_method(p, "POST");
    probe_http_method(p, "PUT");
    probe_http_method(p, "DELETE");
    probe_http_method(p, "TRACE");
    probe_http_method(p, "CONNECT");
    return 0;
}


decompiled_code:
char **__fastcall get_protocol(char *s2)
{
__int64 v1; // rbx

v1 = 0LL;
while ( strcmp(builtins[6 * v1], s2) )
{
if ( ++v1 == 8 )
return 0LL;
}
return &builtins[6 * (int)v1];
}

canonical_decompiled_code:
char **__fastcall func4(char *s2)
{
__int64 v1; // rbx

v1 = 0LL;
while ( strcmp(builtins[6 * v1], s2) )
{
if ( ++v1 == 8 )
return 0LL;
}
return &builtins[6 * (int)v1];
}

original_code:
static struct proto* get_protocol(const char* description)
{
    int i;
    for (i = 0; i < (sizeof(builtins) / sizeof(builtins[0])); i++) {
        if (!strcmp(builtins[i].description, description)) {
            return &builtins[i];
        }
    }
    return ((void *)0);
}

canonical_original_code:
static struct proto *get_protocol(char *description)
{
    int i;
    for (i = 0; i < (sizeof(builtins) / sizeof(builtins[0])); i++) {
        if (!strcmp(builtins[i].description, description)) {
            return &builtins[i];
        }
    }
    return ((void *)0);
}

variable_types:
description
char *

return_type: struct proto { char *description; char *service; struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *saddr; int (*probe)(char *, int, struct proto *); void *data; struct proto *next; } *


decompiled_code:
_BOOL8 __fastcall is_tinc_protocol(const void *a1)
{
return memcmp(a1, "0 ", 2uLL) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func5(const void *a1)
{
return memcmp(a1, "0 ", 2uLL) == 0;
}

original_code:
static int is_tinc_protocol( const char *p, int len, struct proto *proto)
{
    return !strncmp(p, "0 ", 2);
}

canonical_original_code:
static int is_tinc_protocol(char *p, int len, struct proto *proto)
{
    return !strncmp(p, "0 ", 2);
}


decompiled_code:
_BOOL8 __fastcall is_ssh_protocol(const void *a1)
{
return memcmp(a1, "SSH-", 4uLL) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(const void *a1)
{
return memcmp(a1, "SSH-", 4uLL) == 0;
}

original_code:
static int is_ssh_protocol(const char *p, int len, struct proto *proto)
{
    if (!strncmp(p, "SSH-", 4)) {
        return 1;
    }
    return 0;
}

canonical_original_code:
static int is_ssh_protocol(char *p, int len, struct proto *proto)
{
    if (!strncmp(p, "SSH-", 4)) {
        return 1;
    }
    return 0;
}


decompiled_code:
_BOOL8 __fastcall is_openvpn_protocol(__int16 *a1, int a2)
{
return a2 - 2 == (unsigned __int16)_bswap_16(*a1);
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(__int16 *a1, int a2)
{
return a2 - 2 == (unsigned __int16)func7(*a1);
}

original_code:
static int is_openvpn_protocol (const char*p,int len, struct proto *proto)
{
    int packet_len = ntohs(*(uint16_t*)p);
    return packet_len == len - 2;
}

canonical_original_code:
static int is_openvpn_protocol(char *p, int len, struct proto *proto)
{
    int packet_len = ntohs(*(uint16_t*)p);
    return packet_len == len - 2;
}


decompiled_code:
char **get_builtins()
{
return builtins;
}

canonical_decompiled_code:
char **func10()
{
return builtins;
}

original_code:
struct proto* get_builtins(void) {
    return builtins;
}

canonical_original_code:
struct proto *get_builtins() {
    return builtins;
}


decompiled_code:
__int64 get_num_builtins()
{
return 8LL;
}

canonical_decompiled_code:
__int64 func11()
{
return 8LL;
}

original_code:
int get_num_builtins(void) {
    return (sizeof(builtins) / sizeof(builtins[0]));
}

canonical_original_code:
int get_num_builtins() {
    return (sizeof(builtins) / sizeof(builtins[0]));
}


decompiled_code:
int __fastcall set_ontimeout(const char *a1)
{
return asprintf(&on_timeout, "%s", a1);
}

canonical_decompiled_code:
int __fastcall func12(const char *a1)
{
return asprintf(&on_timeout, "%s", a1);
}

original_code:
void set_ontimeout(const char* name)
{
    asprintf(&on_timeout, "%s", name);
}

canonical_original_code:
void set_ontimeout(char *name)
{
    asprintf(&on_timeout, "%s", name);
}

variable_types:
name
char *


decompiled_code:
__int64 get_first_protocol()
{
return protocols;
}

canonical_decompiled_code:
__int64 func13()
{
return protocols;
}

original_code:
struct proto* get_first_protocol(void)
{
    return protocols;
}

canonical_original_code:
struct proto *get_first_protocol()
{
    return protocols;
}


decompiled_code:
__int64 timeout_protocol()
{
__int64 v0; // rax
__int64 v1; // r12
const char *v2; // rbp
__int64 v3; // rbx

v0 = get_first_protocol();
v1 = v0;
if ( v0 )
{
v2 = on_timeout;
v3 = v0;
while ( strcmp(*(const char **)v3, v2) )
{
v3 = *(_QWORD *)(v3 + 40);
if ( !v3 )
return v1;
}
v1 = v3;
}
return v1;
}

canonical_decompiled_code:
__int64 func14()
{
__int64 v0; // rax
__int64 v1; // r12
const char *v2; // rbp
__int64 v3; // rbx

v0 = func13();
v1 = v0;
if ( v0 )
{
v2 = on_timeout;
v3 = v0;
while ( strcmp(*(const char **)v3, v2) )
{
v3 = *(_QWORD *)(v3 + 40);
if ( !v3 )
return v1;
}
v1 = v3;
}
return v1;
}

original_code:
struct proto* timeout_protocol(void)
{
    struct proto* p = get_first_protocol();
    for (; p && strcmp(p->description, on_timeout); p = p->next);
    if (p) return p;
    return get_first_protocol();
}

canonical_original_code:
struct proto *timeout_protocol()
{
    struct proto *p = get_first_protocol();
    for (; p && strcmp(p->description, on_timeout); p = p->next);
    if (p) return p;
    return get_first_protocol();
}


decompiled_code:
void __fastcall set_protocol_list(__int64 a1)
{
protocols = a1;
}

canonical_decompiled_code:
void __fastcall func15(__int64 a1)
{
protocols = a1;
}

original_code:
void set_protocol_list(struct proto* prots)
{
    protocols = prots;
}

canonical_original_code:
void set_protocol_list(struct proto *prots)
{
    protocols = prots;
}


hexdump

decompiled_code:
int __fastcall hexdump(__int64 a1, unsigned int a2)
{
int result; // eax
__int64 v4; // r15
__int64 i; // rbp
unsigned int v6; // ebx
const unsigned __int16 **v7; // rax
unsigned __int8 v8; // di

result = 16 - (a2 & 0xF);
if ( (a2 & 0xF) == 0 )
result = 0;
v4 = result + a2;
for ( i = 0LL; i != v4; ++i )
{
if ( (i & 0xF) == 0 )
printf("0x%06x: ", (unsigned int)i);
if ( a2 <= (unsigned int)i )
result = printf(" ");
else
result = printf("%02x ", *(unsigned __int8 *)(a1 + i));
if ( (i & 0xF) == 15 )
{
if ( (int)i - 15 <= (unsigned int)i )
{
v6 = i - 15;
do
{
if ( a2 <= v6 )
{
putchar(32);
}
else
{
v7 = _ctype_b_loc();
v8 = *(_BYTE *)(a1 + v6);
if ( ((*v7)[(char)v8] & 0x4000) != 0 )
putchar(v8);
else
putchar(46);
}
++v6;
}
while ( (_DWORD)i - 15 + ((int)i + 1 < (unsigned int)(i - 14) ? 0xFFFFFFF1 : 0) + 16 != v6 );
}
result = putchar(10);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func16(__int64 a1, unsigned int a2)
{
int result; // eax
__int64 v4; // r15
__int64 i; // rbp
unsigned int v6; // ebx
const unsigned __int16 **v7; // rax
unsigned __int8 v8; // di

result = 16 - (a2 & 0xF);
if ( (a2 & 0xF) == 0 )
result = 0;
v4 = result + a2;
for ( i = 0LL; i != v4; ++i )
{
if ( (i & 0xF) == 0 )
printf("0x%06x: ", (unsigned int)i);
if ( a2 <= (unsigned int)i )
result = printf(" ");
else
result = printf("%02x ", *(unsigned __int8 *)(a1 + i));
if ( (i & 0xF) == 15 )
{
if ( (int)i - 15 <= (unsigned int)i )
{
v6 = i - 15;
do
{
if ( a2 <= v6 )
{
putchar(32);
}
else
{
v7 = _ctype_b_loc();
v8 = *(_BYTE *)(a1 + v6);
if ( ((*v7)[(char)v8] & 0x4000) != 0 )
putchar(v8);
else
putchar(46);
}
++v6;
}
while ( (_DWORD)i - 15 + ((int)i + 1 < (unsigned int)(i - 14) ? 0xFFFFFFF1 : 0) + 16 != v6 );
}
result = putchar(10);
}
}
return result;
}

original_code:
void hexdump(const char *mem, unsigned int len)
{
    unsigned int i, j;
    for(i = 0; i < len + ((len % 16) ? (16 - len % 16) : 0); i++)
    {
        if(i % 16 == 0)
            printf("0x%06x: ", i);
        if(i < len)
            printf("%02x ", 0xFF & mem[i]);
        else
            printf("   ");
        if(i % 16 == (16 - 1)) {
            for(j = i - (16 - 1); j <= i; j++) {
                if(j >= len)
                    putchar(' ');
                else if(((*__ctype_b_loc ())[(int) ((mem[j]))] & (unsigned short int) _ISprint))
                    putchar(0xFF & mem[j]);
                else
                    putchar('.');
            }
            putchar('\n');
        }
    }
}

canonical_original_code:
void hexdump(char *mem, unsigned int len)
{
    unsigned int i; unsigned int j;
    for(i = 0; i < len + ((len % 16) ? (16 - len % 16) : 0); i++)
    {
        if(i % 16 == 0)
            printf("0x%06x: ", i);
        if(i < len)
            printf("%02x ", 0xFF & mem[i]);
        else
            printf("   ");
        if(i % 16 == (16 - 1)) {
            for(j = i - (16 - 1); j <= i; j++) {
                if(j >= len)
                    putchar(' ');
                else if(((*__ctype_b_loc ())[(int) ((mem[j]))] & (unsigned short) _ISprint))
                    putchar(0xFF & mem[j]);
                else
                    putchar('.');
            }
            putchar('\n');
        }
    }
}

variable_types:
mem
char *

variable_types:
len
unsigned int

variable_types:
i
unsigned int

variable_types:
j
unsigned int


decompiled_code:
const char **__fastcall probe_client_protocol(__int64 a1)
{
int v1; // eax
const char **v2; // rbx
unsigned int v4; // ebp
_BYTE v5[8216]; // [rsp+0h] [rbp-2018h] BYREF

v1 = read(*(_DWORD *)(a1 + 16), v5, 0x2000uLL);
if ( v1 > 0 && (v4 = v1, defer_write(a1 + 48, v5, (unsigned int)v1), (v2 = (const char **)protocols) != 0LL) )
{
while ( 1 )
{
if ( v2[3] )
{
if ( verbose )
fprintf(stderr, "probing for %s\n", *v2);
if ( ((unsigned int (__fastcall *)(_BYTE *, _QWORD, const char **))v2[3])(v5, v4, v2) )
break;
}
v2 = (const char **)v2[5];
if ( !v2 )
goto LABEL_2;
}
if ( verbose )
fprintf(stderr, "probe %s successful\n", *v2);
}
else
{
LABEL_2:
if ( verbose )
fprintf(stderr, "all probes failed, connecting to first protocol: %s\n", *(const char **)protocols);
v2 = (const char **)protocols;
}
return v2;
}

canonical_decompiled_code:
const char **__fastcall func17(__int64 a1)
{
int v1; // eax
const char **v2; // rbx
unsigned int v4; // ebp
_BYTE v5[8216]; // [rsp+0h] [rbp-2018h] BYREF

v1 = read(*(_DWORD *)(a1 + 16), v5, 0x2000uLL);
if ( v1 > 0 && (v4 = v1, defer_write(a1 + 48, v5, (unsigned int)v1), (v2 = (const char **)protocols) != 0LL) )
{
while ( 1 )
{
if ( v2[3] )
{
if ( verbose )
fprintf(stderr, "probing for %s\n", *v2);
if ( ((unsigned int (__fastcall *)(_BYTE *, _QWORD, const char **))v2[3])(v5, v4, v2) )
break;
}
v2 = (const char **)v2[5];
if ( !v2 )
goto LABEL_2;
}
if ( verbose )
fprintf(stderr, "probe %s successful\n", *v2);
}
else
{
LABEL_2:
if ( verbose )
fprintf(stderr, "all probes failed, connecting to first protocol: %s\n", *(const char **)protocols);
v2 = (const char **)protocols;
}
return v2;
}

original_code:
struct proto* probe_client_protocol(struct connection *cnx)
{
    char buffer[8192];
    struct proto *p;
    int n;
    n = read(cnx->q[0].fd, buffer, sizeof(buffer));
    if (n > 0) {
        defer_write(&cnx->q[1], buffer, n);
        for (p = protocols; p; p = p->next) {
            if (! p->probe) continue;
            if (verbose) fprintf(stderr, "probing for %s\n", p->description);
            if (p->probe(buffer, n, p)) {
                if (verbose) fprintf(stderr, "probe %s successful\n", p->description);
                return p;
            }
        }
    }
    if (verbose)
        fprintf(stderr,
                "all probes failed, connecting to first protocol: %s\n",
                protocols->description);
    return protocols;
}

canonical_original_code:
struct proto *probe_client_protocol(struct connection *cnx)
{
    char buffer[8192];
    struct proto *p;
    int n;
    n = read(cnx->q[0].fd, buffer, sizeof(buffer));
    if (n > 0) {
        defer_write(&cnx->q[1], buffer, n);
        for (p = protocols; p; p = p->next) {
            if (! p->probe) continue;
            if (verbose) fprintf(stderr, "probing for %s\n", p->description);
            if (p->probe(buffer, n, p)) {
                if (verbose) fprintf(stderr, "probe %s successful\n", p->description);
                return p;
            }
        }
    }
    if (verbose)
        fprintf(stderr,
                "all probes failed, connecting to first protocol: %s\n",
                protocols->description);
    return protocols;
}

variable_types: struct connection { enum connection_state { ST_PROBING=1, ST_SHOVELING=2 } state; long probe_timeout; struct queue { int fd; void *begin_defered_data; void *defered_data; int defered_data_size; } q[2]; } *

variable_types:
buffer
char [8192]

variable_types: struct proto { char *description; char *service; struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; } *saddr; int (*probe)(char *, int, struct proto *); void *data; struct proto *next; } *

variable_types: n int


decompiled_code:
int __fastcall stop_listeners(int sig)
{
__int64 v1; // rbp
__int64 v2; // rbx
int result; // eax

if ( listener_pid_number > 0 )
{
v1 = 4LL * (unsigned int)listener_pid_number;
v2 = 0LL;
do
{
result = kill(*(_DWORD *)(listener_pid + v2), sig);
v2 += 4LL;
}
while ( v2 != v1 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func0(int sig)
{
__int64 v1; // rbp
__int64 v2; // rbx
int result; // eax

if ( listener_pid_number > 0 )
{
v1 = 4LL * (unsigned int)listener_pid_number;
v2 = 0LL;
do
{
result = kill(*(_DWORD *)(listener_pid + v2), sig);
v2 += 4LL;
}
while ( v2 != v1 );
}
return result;
}

original_code:
void stop_listeners(int sig)
{
    int i;
    for (i = 0; i < listener_pid_number; i++) {
        kill(listener_pid[i], sig);
    }
}

variable_types: sig int

variable_types: i int


shovel

original_code:
int shovel(struct connection *cnx)
{
   fd_set fds;
   int res, i;
   int max_fd = (((cnx->q[0].fd) > (cnx->q[1].fd)) ? (cnx->q[0].fd) : (cnx->q[1].fd)) + 1;
   do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->fds_bits)[0]) : "memory"); } while (0);
   while (1) {
      ((void) (((&fds)->fds_bits)[((cnx->q[0].fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((cnx->q[0].fd) % (8 * (int) sizeof (__fd_mask)))))));
      ((void) (((&fds)->fds_bits)[((cnx->q[1].fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((cnx->q[1].fd) % (8 * (int) sizeof (__fd_mask)))))));
      res = select(
                   max_fd,
                   &fds,
                   ((void *)0),
                   ((void *)0),
                   ((void *)0)
                  );
      if (res == -1) { perror("select"); exit(1); };
      for (i = 0; i < 2; i++) {
          if (((((&fds)->fds_bits)[((cnx->q[i].fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((cnx->q[i].fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
              res = fd2fd(&cnx->q[1-i], &cnx->q[i]);
              if (!res) {
                  if (verbose)
                      fprintf(stderr, "%s %s", i ? "client" : "server", "socket closed\n");
                  return res;
              }
          }
      }
   }
}

canonical_original_code:
int shovel(struct connection *cnx)
{
   struct fd_set fds;
   int res; int i;
   int max_fd = (((cnx->q[0].fd) > (cnx->q[1].fd)) ? (cnx->q[0].fd) : (cnx->q[1].fd)) + 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" (&((&fds)->fds_bits)[0]) : "memory"); } while (0);
   while (1) {
      ((void) (((&fds)->fds_bits)[((cnx->q[0].fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((cnx->q[0].fd) % (8 * (int) sizeof (__fd_mask)))))));
      ((void) (((&fds)->fds_bits)[((cnx->q[1].fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((cnx->q[1].fd) % (8 * (int) sizeof (__fd_mask)))))));
      res = select(
                   max_fd,
                   &fds,
                   ((void *)0),
                   ((void *)0),
                   ((void *)0)
                  );
      if (res == -1) { perror("select"); exit(1); };
      for (i = 0; i < 2; i++) {
          if (((((&fds)->fds_bits)[((cnx->q[i].fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((cnx->q[i].fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
              res = fd2fd(&cnx->q[1-i], &cnx->q[i]);
              if (!res) {
                  if (verbose)
                      fprintf(stderr, "%s %s", i ? "client" : "server", "socket closed\n");
                  return res;
              }
          }
      }
   }
}

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

variable_types: res int

user_defined_types: enum connection_state { ST_PROBING=1, ST_SHOVELING=2 }


decompiled_code:
void __fastcall __noreturn start_shoveler(int fd)
{
__int64 v1; // r12
_QWORD *v2; // rbp
__int64 v3; // r12
int v4; // eax
int v5; // ebp
_DWORD v6[12]; // [rsp+0h] [rbp-F8h] BYREF
int v7[8]; // [rsp+30h] [rbp-C8h] BYREF
struct timeval timeout; // [rsp+50h] [rbp-A8h] BYREF
fd_set readfds; // [rsp+60h] [rbp-98h] BYREF

init_cnx(v6);
memset(&readfds, 0, sizeof(readfds));
v1 = 1LL << (fd % 64);
readfds.fds_bits[fd / 64] |= v1;
timeout.tv_usec = 0LL;
timeout.tv_sec = probing_timeout;
if ( select(fd + 1, &readfds, 0LL, 0LL, &timeout) == -1 )
perror("select");
v6[4] = fd;
if ( (readfds.fds_bits[fd / 64] & v1) != 0 )
v2 = (_QWORD *)probe_client_protocol(v6);
else
v2 = (_QWORD *)timeout_protocol();
v3 = v2[2];
if ( !v2[1] || !(unsigned int)check_access_rights((unsigned int)fd) )
{
v4 = connect_addr(v3, *v2);
v5 = v4;
if ( v4 != -1 )
{
v7[0] = v4;
log_connection(v6);
flush_defered(v7);
shovel((__int64)v6);
close(fd);
close(v5);
if ( verbose )
fwrite("connection closed down\n", 1uLL, 0x17uLL, stderr);
exit(0);
}
perror("connect");
exit(1);
}
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func2(int fd)
{
__int64 v1; // r12
_QWORD *v2; // rbp
__int64 v3; // r12
int v4; // eax
int v5; // ebp
_DWORD v6[12]; // [rsp+0h] [rbp-F8h] BYREF
int v7[8]; // [rsp+30h] [rbp-C8h] BYREF
struct timeval timeout; // [rsp+50h] [rbp-A8h] BYREF
fd_set readfds; // [rsp+60h] [rbp-98h] BYREF

init_cnx(v6);
memset(&readfds, 0, sizeof(readfds));
v1 = 1LL << (fd % 64);
readfds.fds_bits[fd / 64] |= v1;
timeout.tv_usec = 0LL;
timeout.tv_sec = probing_timeout;
if ( select(fd + 1, &readfds, 0LL, 0LL, &timeout) == -1 )
perror("select");
v6[4] = fd;
if ( (readfds.fds_bits[fd / 64] & v1) != 0 )
v2 = (_QWORD *)probe_client_protocol(v6);
else
v2 = (_QWORD *)timeout_protocol();
v3 = v2[2];
if ( !v2[1] || !(unsigned int)check_access_rights((unsigned int)fd) )
{
v4 = connect_addr(v3, *v2);
v5 = v4;
if ( v4 != -1 )
{
v7[0] = v4;
log_connection(v6);
flush_defered(v7);
func1((__int64)v6);
close(fd);
close(v5);
if ( verbose )
fwrite("connection closed down\n", 1uLL, 0x17uLL, stderr);
exit(0);
}
perror("connect");
exit(1);
}
exit(0);
}

original_code:
void start_shoveler(int in_socket)
{
   fd_set fds;
   struct timeval tv;
   struct addrinfo *saddr;
   int res;
   int out_socket;
   struct connection cnx;
   struct proto *prot;
   init_cnx(&cnx);
   do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->fds_bits)[0]) : "memory"); } while (0);
   ((void) (((&fds)->fds_bits)[((in_socket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((in_socket) % (8 * (int) sizeof (__fd_mask)))))));
   memset(&tv, 0, sizeof(tv));
   tv.tv_sec = probing_timeout;
   res = select(in_socket + 1, &fds, ((void *)0), ((void *)0), &tv);
   if (res == -1)
      perror("select");
   cnx.q[0].fd = in_socket;
   if (((((&fds)->fds_bits)[((in_socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((in_socket) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
       prot = probe_client_protocol(&cnx);
   } else {
       prot = timeout_protocol();
   }
   saddr = prot->saddr;
   if (prot->service &&
       check_access_rights(in_socket, prot->service)) {
       exit(0);
   }
   out_socket = connect_addr(saddr, prot->description);
   if (out_socket == -1) { perror("connect"); exit(1); };
   cnx.q[1].fd = out_socket;
   log_connection(&cnx);
   flush_defered(&cnx.q[1]);
   shovel(&cnx);
   close(in_socket);
   close(out_socket);
   if (verbose)
      fprintf(stderr, "connection closed down\n");
   exit(0);
}

canonical_original_code:
void start_shoveler(int in_socket)
{
   struct fd_set fds;
   struct timeval tv;
   struct addrinfo *saddr;
   int res;
   int out_socket;
   struct connection cnx;
   struct proto *prot;
   init_cnx(&cnx);
   do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->fds_bits)[0]) : "memory"); } while (0);
   ((void) (((&fds)->fds_bits)[((in_socket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((in_socket) % (8 * (int) sizeof (__fd_mask)))))));
   memset(&tv, 0, sizeof(tv));
   tv.tv_sec = probing_timeout;
   res = select(in_socket + 1, &fds, ((void *)0), ((void *)0), &tv);
   if (res == -1)
      perror("select");
   cnx.q[0].fd = in_socket;
   if (((((&fds)->fds_bits)[((in_socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((in_socket) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
       prot = probe_client_protocol(&cnx);
   } else {
       prot = timeout_protocol();
   }
   saddr = prot->saddr;
   if (prot->service &&
       check_access_rights(in_socket, prot->service)) {
       exit(0);
   }
   out_socket = connect_addr(saddr, prot->description);
   if (out_socket == -1) { perror("connect"); exit(1); };
   cnx.q[1].fd = out_socket;
   log_connection(&cnx);
   flush_defered(&cnx.q[1]);
   shovel(&cnx);
   close(in_socket);
   close(out_socket);
   if (verbose)
      fprintf(stderr, "connection closed down\n");
   exit(0);
}

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

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

variable_types: struct connection { enum connection_state { ST_PROBING=1, ST_SHOVELING=2 } state; long probe_timeout; struct queue { int fd; void *begin_defered_data; void *defered_data; int defered_data_size; } q[2]; }


decompiled_code:
__pid_t __fastcall main_loop(__int64 a1, int a2)
{
__int64 v2; // rbx
__pid_t *v3; // rbp
__pid_t v4; // eax
int *v6; // rbx
unsigned int v7; // eax
int v8; // ebp
struct sigaction v9; // [rsp+0h] [rbp-C8h] BYREF

listener_pid_number = a2;
listener_pid = (__int64)malloc(4LL * a2);
if ( a2 > 0 )
{
v2 = 0LL;
do
{
v3 = (__pid_t *)(listener_pid + v2);
v4 = fork();
*v3 = v4;
if ( !v4 )
{
v6 = (int *)(a1 + v2);
while ( 1 )
{
v7 = accept(*v6, 0LL, 0LL);
v8 = v7;
if ( verbose )
fprintf(stderr, "accepted fd %d\n", v7);
if ( !fork() )
{
close(*v6);
start_shoveler(v8);
}
close(v8);
}
}
v2 += 4LL;
}
while ( v2 != 4LL * (unsigned int)a2 );
}
memset(&v9.sa_mask, 0, 0x90uLL);
v9.sa_handler = (__sighandler_t)stop_listeners;
if ( sigaction(15, &v9, 0LL) == -1 )
{
perror("sigaction");
exit(1);
}
return wait(0LL);
}

canonical_decompiled_code:
__pid_t __fastcall func3(__int64 a1, int a2)
{
__int64 v2; // rbx
__pid_t *v3; // rbp
__pid_t v4; // eax
int *v6; // rbx
unsigned int v7; // eax
int v8; // ebp
struct sigaction v9; // [rsp+0h] [rbp-C8h] BYREF

listener_pid_number = a2;
listener_pid = (__int64)malloc(4LL * a2);
if ( a2 > 0 )
{
v2 = 0LL;
do
{
v3 = (__pid_t *)(listener_pid + v2);
v4 = fork();
*v3 = v4;
if ( !v4 )
{
v6 = (int *)(a1 + v2);
while ( 1 )
{
v7 = accept(*v6, 0LL, 0LL);
v8 = v7;
if ( verbose )
fprintf(stderr, "accepted fd %d\n", v7);
if ( !fork() )
{
close(*v6);
func2(v8);
}
close(v8);
}
}
v2 += 4LL;
}
while ( v2 != 4LL * (unsigned int)a2 );
}
memset(&v9.sa_mask, 0, 0x90uLL);
v9.sa_handler = (__sighandler_t)stop_listeners;
if ( sigaction(15, &v9, 0LL) == -1 )
{
perror("sigaction");
exit(1);
}
return wait(0LL);
}

original_code:
void main_loop(int listen_sockets[], int num_addr_listen)
{
    int in_socket, i, res;
    struct sigaction action;
    listener_pid_number = num_addr_listen;
    listener_pid = malloc(listener_pid_number * sizeof(listener_pid[0]));
    for (i = 0; i < num_addr_listen; i++) {
        if (!(listener_pid[i] = fork())) {
            while (1)
            {
                in_socket = accept(listen_sockets[i], 0, 0);
                if (verbose) fprintf(stderr, "accepted fd %d\n", in_socket);
                if (!fork())
                {
                    close(listen_sockets[i]);
                    start_shoveler(in_socket);
                    exit(0);
                }
                close(in_socket);
            }
        }
    }
    memset(&action, 0, sizeof(action));
    action.__sigaction_handler.sa_handler = stop_listeners;
    res = sigaction(15, &action, ((void *)0));
    if (res == -1) { perror("sigaction"); exit(1); };
    wait(((void *)0));
}

canonical_original_code:
void main_loop(int listen_sockets[], int num_addr_listen)
{
    int in_socket; int i; int res;
    struct sigaction action;
    listener_pid_number = num_addr_listen;
    listener_pid = malloc(listener_pid_number * sizeof(listener_pid[0]));
    for (i = 0; i < num_addr_listen; i++) {
        if (!(listener_pid[i] = fork())) {
            while (1)
            {
                in_socket = accept(listen_sockets[i], 0, 0);
                if (verbose) fprintf(stderr, "accepted fd %d\n", in_socket);
                if (!fork())
                {
                    close(listen_sockets[i]);
                    start_shoveler(in_socket);
                    exit(0);
                }
                close(in_socket);
            }
        }
    }
    memset(&action, 0, sizeof(action));
    action.__sigaction_handler.sa_handler = stop_listeners;
    res = sigaction(15, &action, ((void *)0));
    if (res == -1) { perror("sigaction"); exit(1); };
    wait(((void *)0));
}

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }


freeMemory

repo: guillaumeBoufflers/malloc

decompiled_code:
void __fastcall freeMemory(_QWORD *a1)
{
_QWORD *v1; // rax

if ( (_QWORD *)MALLOC_LIST != a1 || MALLOC_PTR_END )
{
if ( (_QWORD *)MALLOC_PTR_END == a1 )
{
v1 = (_QWORD *)a1[1];
if ( v1 )
{
MALLOC_PTR_END = a1[1];
*v1 = 0LL;
brk((void *)(a1[1] + *(_QWORD *)(a1[1] + 16LL) + 32LL));
}
}
}
else
{
brk(a1);
MALLOC_LIST = 0LL;
MALLOC_PTR_END = 0LL;
}
}

canonical_decompiled_code:
void __fastcall func7(_QWORD *a1)
{
_QWORD *v1; // rax

if ( (_QWORD *)MALLOC_LIST != a1 || MALLOC_PTR_END )
{
if ( (_QWORD *)MALLOC_PTR_END == a1 )
{
v1 = (_QWORD *)a1[1];
if ( v1 )
{
MALLOC_PTR_END = a1[1];
*v1 = 0LL;
brk((void *)(a1[1] + *(_QWORD *)(a1[1] + 16LL) + 32LL));
}
}
}
else
{
brk(a1);
MALLOC_LIST = 0LL;
MALLOC_PTR_END = 0LL;
}
}

original_code:
static void freeMemory(t_malloc *tmp)
{
  if (tmp == MALLOC_LIST && !MALLOC_PTR_END)
    {
      brk(MALLOC_LIST);
      MALLOC_LIST = ((void *)0);
      MALLOC_PTR_END = ((void *)0);
    }
  else if (tmp == MALLOC_PTR_END && tmp->prev)
    {
      MALLOC_PTR_END = tmp->prev;
      MALLOC_PTR_END->next = ((void *)0);
      brk((void*)tmp->prev + MALLOC_BLOCK_SIZE + tmp->prev->size);
    }
}

canonical_original_code:
static void freeMemory(struct s_malloc *tmp)
{
  if (tmp == MALLOC_LIST && !MALLOC_PTR_END)
    {
      brk(MALLOC_LIST);
      MALLOC_LIST = ((void *)0);
      MALLOC_PTR_END = ((void *)0);
    }
  else if (tmp == MALLOC_PTR_END && tmp->prev)
    {
      MALLOC_PTR_END = tmp->prev;
      MALLOC_PTR_END->next = ((void *)0);
      brk((void *)tmp->prev + MALLOC_BLOCK_SIZE + tmp->prev->size);
    }
}

variable_types: struct s_malloc { struct s_malloc *next; struct s_malloc *prev; unsigned long size; char isFree; } *


malloc

decompiled_code:
__int64 __fastcall malloc(__int64 a1)
{
unsigned __int64 v1; // rbx
__int64 v2; // rbp
_QWORD *v4; // r12
__int64 v5; // rax

pthread_mutex_lock(lock);
v1 = (a1 - 1) & 0xFFFFFFFFFFFFFFFCLL;
v2 = fitAllocatedSpace(v1 + 4);
if ( !v2 )
{
v4 = sbrk(0LL);
if ( sbrk(v1 + 36) == (void *)-1LL )
{
pthread_mutex_unlock(lock);
}
else
{
v4[2] = v1 + 4;
*((_BYTE *)v4 + 24) = 0;
*v4 = 0LL;
v4[1] = 0LL;
v5 = MALLOC_PTR_END;
if ( MALLOC_PTR_END )
{
*(_QWORD *)MALLOC_PTR_END = v4;
v4[1] = v5;
}
else
{
MALLOC_LIST = (__int64)v4;
}
MALLOC_PTR_END = (__int64)v4;
pthread_mutex_unlock(lock);
v2 = (__int64)(v4 + 4);
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
unsigned __int64 v1; // rbx
__int64 v2; // rbp
_QWORD *v4; // r12
__int64 v5; // rax

pthread_mutex_lock(lock);
v1 = (a1 - 1) & 0xFFFFFFFFFFFFFFFCLL;
v2 = func6(v1 + 4);
if ( !v2 )
{
v4 = sbrk(0LL);
if ( sbrk(v1 + 36) == (void *)-1LL )
{
pthread_mutex_unlock(lock);
}
else
{
v4[2] = v1 + 4;
*((_BYTE *)v4 + 24) = 0;
*v4 = 0LL;
v4[1] = 0LL;
v5 = MALLOC_PTR_END;
if ( MALLOC_PTR_END )
{
*(_QWORD *)MALLOC_PTR_END = v4;
v4[1] = v5;
}
else
{
MALLOC_LIST = (__int64)v4;
}
MALLOC_PTR_END = (__int64)v4;
pthread_mutex_unlock(lock);
v2 = (__int64)(v4 + 4);
}
}
return v2;
}

original_code:
void *malloc(size_t size)
{
  t_malloc *new;
  void *p;
  pthread_mutex_lock(&lock[0]);
  size = (((((size) -1)>>2)<<2)+4);
  if ((p = fitAllocatedSpace(size)) != ((void *)0))
    return (p);
  new = sbrk(0);
  if (sbrk(MALLOC_BLOCK_SIZE + size) == (void*) -1)
    {
      pthread_mutex_unlock(&lock[0]);
      return (((void *)0));
    }
  new->size = size;
  new->isFree = 0;
  new->next = ((void *)0);
  new->prev = ((void *)0);
  if (MALLOC_PTR_END == ((void *)0))
    MALLOC_LIST = new;
  else
    {
      MALLOC_PTR_END->next = new;
      new->prev = MALLOC_PTR_END;
    }
  MALLOC_PTR_END = new;
  pthread_mutex_unlock(&lock[0]);
  return ((void*)new + MALLOC_BLOCK_SIZE);
}

canonical_original_code:
void *malloc(unsigned long size)
{
  struct s_malloc *new;
  void *p;
  pthread_mutex_lock(&lock[0]);
  size = (((((size) -1)>>2)<<2)+4);
  if ((p = fitAllocatedSpace(size)) != ((void *)0))
    return (p);
  new = sbrk(0);
  if (sbrk(MALLOC_BLOCK_SIZE + size) == (void *) -1)
    {
      pthread_mutex_unlock(&lock[0]);
      return (((void *)0));
    }
  new->size = size;
  new->isFree = 0;
  new->next = ((void *)0);
  new->prev = ((void *)0);
  if (MALLOC_PTR_END == ((void *)0))
    MALLOC_LIST = new;
  else
    {
      MALLOC_PTR_END->next = new;
      new->prev = MALLOC_PTR_END;
    }
  MALLOC_PTR_END = new;
  pthread_mutex_unlock(&lock[0]);
  return ((void *)new + MALLOC_BLOCK_SIZE);
}

variable_types:
size
unsigned long

variable_types:
p
void *

return_type: void *

user_defined_types: struct s_malloc { struct s_malloc *next; struct s_malloc *prev; unsigned long size; char isFree; }


calloc

decompiled_code:
void *__fastcall calloc(__int64 a1, __int64 a2)
{
return malloc(a2 * a1);
}

canonical_decompiled_code:
void *__fastcall func9(__int64 a1, __int64 a2)
{
return func8(a2 * a1);
}

original_code:
void *calloc(size_t nmemb, size_t size)
{
  return (malloc(nmemb * size));
}

canonical_original_code:
void *calloc(unsigned long nmemb, unsigned long size)
{
  return (malloc(nmemb * size));
}

variable_types:
nmemb
unsigned long


handleLeaks

decompiled_code:
void __fastcall handleLeaks(__int64 a1)
{
if ( a1 && *(_QWORD *)a1 && *(_BYTE *)(a1 + 24) == 1 )
*(_QWORD *)(a1 + 16) = (*(_QWORD *)a1 - (a1 + 32)) & 0xFFFFFFFFFFFFFFFCLL;
}

original_code:
void handleLeaks(t_malloc *tmp)
{
  if (tmp && tmp->prev &&
      ((void*)tmp - ((void*)tmp->prev + MALLOC_BLOCK_SIZE + tmp->prev->size) == 0))
      tmp = memcpy((void*)tmp->prev + MALLOC_BLOCK_SIZE + tmp->prev->size, tmp, MALLOC_BLOCK_SIZE);
  if (tmp && tmp->next && tmp->isFree == 1)
    tmp->size = ((((((void*)tmp->next - ((void*)tmp + MALLOC_BLOCK_SIZE)))>>2)<<2));
}

canonical_original_code:
void handleLeaks(struct s_malloc *tmp)
{
  if (tmp && tmp->prev &&
      ((void *)tmp - ((void *)tmp->prev + MALLOC_BLOCK_SIZE + tmp->prev->size) == 0))
      tmp = memcpy((void *)tmp->prev + MALLOC_BLOCK_SIZE + tmp->prev->size, tmp, MALLOC_BLOCK_SIZE);
  if (tmp && tmp->next && tmp->isFree == 1)
    tmp->size = ((((((void *)tmp->next - ((void *)tmp + MALLOC_BLOCK_SIZE)))>>2)<<2));
}


free

decompiled_code:
int __fastcall free(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // rax
__int64 v3; // rdi

v1 = MALLOC_LIST;
pthread_mutex_lock(lock);
if ( !v1 )
return pthread_mutex_unlock(lock);
while ( a1 != v1 + 32 )
{
v1 = *(_QWORD *)v1;
if ( !v1 )
return pthread_mutex_unlock(lock);
}
*(_BYTE *)(v1 + 24) = 1;
v2 = *(_QWORD *)(v1 + 8);
if ( v2 && *(_BYTE *)(v2 + 24) == 1 )
{
*(_QWORD *)(v2 + 16) += *(_QWORD *)(v1 + 16) + 32LL;
deleteNode((_QWORD *)v1);
}
v3 = *(_QWORD *)v1;
if ( *(_QWORD *)v1 )
{
if ( *(_BYTE *)(v3 + 24) == 1 )
{
*(_QWORD *)(v1 + 16) += (*(_QWORD *)(v3 + 16) + 32LL) & 0xFFFFFFFFFFFFFFFCLL;
deleteNode((_QWORD *)v3);
}
}
handleLeaks(v1);
freeMemory((_QWORD *)v1);
return pthread_mutex_unlock(lock);
}

canonical_decompiled_code:
int __fastcall func11(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // rax
__int64 v3; // rdi

v1 = MALLOC_LIST;
pthread_mutex_lock(lock);
if ( !v1 )
return pthread_mutex_unlock(lock);
while ( a1 != v1 + 32 )
{
v1 = *(_QWORD *)v1;
if ( !v1 )
return pthread_mutex_unlock(lock);
}
*(_BYTE *)(v1 + 24) = 1;
v2 = *(_QWORD *)(v1 + 8);
if ( v2 && *(_BYTE *)(v2 + 24) == 1 )
{
*(_QWORD *)(v2 + 16) += *(_QWORD *)(v1 + 16) + 32LL;
deleteNode((_QWORD *)v1);
}
v3 = *(_QWORD *)v1;
if ( *(_QWORD *)v1 )
{
if ( *(_BYTE *)(v3 + 24) == 1 )
{
*(_QWORD *)(v1 + 16) += (*(_QWORD *)(v3 + 16) + 32LL) & 0xFFFFFFFFFFFFFFFCLL;
deleteNode((_QWORD *)v3);
}
}
func10(v1);
func7((_QWORD *)v1);
return pthread_mutex_unlock(lock);
}

original_code:
void free(void *ptr)
{
  t_malloc *tmp;
  tmp = MALLOC_LIST;
  pthread_mutex_lock(&lock[0]);
  while (tmp)
    {
      if ((void*)tmp + MALLOC_BLOCK_SIZE == ptr)
 {
   tmp->isFree = 1;
   if (tmp->prev && tmp->prev->isFree == 1)
     {
       tmp->prev->size += tmp->size + MALLOC_BLOCK_SIZE;
       deleteNode(tmp);
     }
   if (tmp->next && tmp->next->isFree == 1)
     {
       tmp->size += (((((tmp->next->size + MALLOC_BLOCK_SIZE))>>2)<<2));
       deleteNode(tmp->next);
     }
   handleLeaks(tmp);
   freeMemory(tmp);
   pthread_mutex_unlock(&lock[0]);
   return;
 }
      tmp = tmp->next;
    }
  pthread_mutex_unlock(&lock[0]);
}

canonical_original_code:
void free(void *ptr)
{
  struct s_malloc *tmp;
  tmp = MALLOC_LIST;
  pthread_mutex_lock(&lock[0]);
  while (tmp)
    {
      if ((void *)tmp + MALLOC_BLOCK_SIZE == ptr)
 {
   tmp->isFree = 1;
   if (tmp->prev && tmp->prev->isFree == 1)
     {
       tmp->prev->size += tmp->size + MALLOC_BLOCK_SIZE;
       deleteNode(tmp);
     }
   if (tmp->next && tmp->next->isFree == 1)
     {
       tmp->size += (((((tmp->next->size + MALLOC_BLOCK_SIZE))>>2)<<2));
       deleteNode(tmp->next);
     }
   handleLeaks(tmp);
   freeMemory(tmp);
   pthread_mutex_unlock(&lock[0]);
   return;
 }
      tmp = tmp->next;
    }
  pthread_mutex_unlock(&lock[0]);
}

variable_types:
ptr
void *


doRealloc

decompiled_code:
void *__fastcall doRealloc(void *ptr, __int64 a2, size_t a3)
{
void *v3; // rbx

v3 = malloc(a3);
memcpy(v3, ptr, *(_QWORD *)(a2 + 16));
free(ptr);
pthread_mutex_unlock(&lock[1]);
return v3;
}

canonical_decompiled_code:
void *__fastcall func12(void *ptr, __int64 a2, size_t a3)
{
void *v3; // rbx

v3 = func8(a3);
memcpy(v3, ptr, *(_QWORD *)(a2 + 16));
func11(ptr);
pthread_mutex_unlock(&lock[1]);
return v3;
}

original_code:
static void *doRealloc(void *ptr, t_malloc *node, size_t size)
{
  t_malloc *next;
  void *s;
  s = ptr;
  ptr = malloc(size);
  memcpy(ptr, s, node->size);
  free(s);
  pthread_mutex_unlock(&lock[1]);
  return (ptr);
}

canonical_original_code:
static void *doRealloc(void *ptr, struct s_malloc *node, unsigned long size)
{
  struct s_malloc *next;
  void *s;
  s = ptr;
  ptr = malloc(size);
  memcpy(ptr, s, node->size);
  free(s);
  pthread_mutex_unlock(&lock[1]);
  return (ptr);
}

variable_types:
s
void *


realloc

decompiled_code:
void *__fastcall realloc(void *ptr, __int64 a2)
{
size_t v2; // rbx
void *result; // rax

v2 = ((a2 - 1) & 0xFFFFFFFFFFFFFFFCLL) + 4;
if ( !ptr )
return malloc(((a2 - 1) & 0xFFFFFFFFFFFFFFFCLL) + 4);
if ( ((a2 - 1) & 0xFFFFFFFFFFFFFFFCLL) == -4LL )
{
free(ptr);
result = 0LL;
}
else
{
pthread_mutex_lock(&lock[1]);
if ( *((_QWORD *)ptr - 2) >= v2 )
{
*((_QWORD *)ptr - 2) = v2;
pthread_mutex_unlock(&lock[1]);
result = ptr;
}
else
{
result = doRealloc(ptr, (__int64)ptr - 32, v2);
}
}
return result;
}

canonical_decompiled_code:
void *__fastcall func13(void *ptr, __int64 a2)
{
size_t v2; // rbx
void *result; // rax

v2 = ((a2 - 1) & 0xFFFFFFFFFFFFFFFCLL) + 4;
if ( !ptr )
return func8(((a2 - 1) & 0xFFFFFFFFFFFFFFFCLL) + 4);
if ( ((a2 - 1) & 0xFFFFFFFFFFFFFFFCLL) == -4LL )
{
func11(ptr);
result = 0LL;
}
else
{
pthread_mutex_lock(&lock[1]);
if ( *((_QWORD *)ptr - 2) >= v2 )
{
*((_QWORD *)ptr - 2) = v2;
pthread_mutex_unlock(&lock[1]);
result = ptr;
}
else
{
result = func12(ptr, (__int64)ptr - 32, v2);
}
}
return result;
}

original_code:
void *realloc(void *ptr, size_t size)
{
  t_malloc *node;
  size = (((((size) -1)>>2)<<2)+4);
  if (!ptr)
    return(malloc(size));
  else if (size == 0)
    {
      free(ptr);
      return (((void *)0));
    }
  else
    {
      pthread_mutex_lock(&lock[1]);
      node = ptr - MALLOC_BLOCK_SIZE;
      if (node->size < size)
 return(doRealloc(ptr, node, size));
      else
 node->size = size;
      pthread_mutex_unlock(&lock[1]);
      return (ptr);
    }
}

canonical_original_code:
void *realloc(void *ptr, unsigned long size)
{
  struct s_malloc *node;
  size = (((((size) -1)>>2)<<2)+4);
  if (!ptr)
    return(malloc(size));
  else if (size == 0)
    {
      free(ptr);
      return (((void *)0));
    }
  else
    {
      pthread_mutex_lock(&lock[1]);
      node = ptr - MALLOC_BLOCK_SIZE;
      if (node->size < size)
 return(doRealloc(ptr, node, size));
      else
 node->size = size;
      pthread_mutex_unlock(&lock[1]);
      return (ptr);
    }
}