decompiled_code:
__int64 __fastcall gr_iter_next(void *ptr)
{
__int64 *v1; // r12
unsigned int (__fastcall *v2)(_QWORD); // rbx
__int64 v3; // rax
__int64 v4; // rax
__int64 v5; // rdx
__int64 v6; // rdx
__int64 result; // rax

if ( !ptr )
return 0LL;
v1 = *(__int64 **)ptr;
v2 = (unsigned int (__fastcall *)(_QWORD))*((_QWORD *)ptr + 2);
if ( *(_QWORD *)ptr )
{
v3 = *((int *)ptr + 2);
while ( *((_DWORD *)v1 + 2) <= (int)v3 )
{
LABEL_11:
v1 = (__int64 *)v1[3];
v3 = 0LL;
*((_DWORD *)ptr + 2) = 0;
*(_QWORD *)ptr = v1;
if ( !v1 )
goto LABEL_12;
}
if ( v2 )
{
while ( !v2(*(_QWORD *)(*v1 + 16LL * (int)v3)) )
{
LODWORD(v3) = *((_DWORD *)ptr + 2) + 1;
*((_DWORD *)ptr + 2) = v3;
if ( (int)v3 >= *((_DWORD *)v1 + 2) )
goto LABEL_11;
}
v4 = *((int *)ptr + 2);
v5 = *v1;
*((_DWORD *)ptr + 2) = v4 + 1;
result = *(_QWORD *)(v5 + 16 * v4);
}
else
{
v6 = *v1;
*((_DWORD *)ptr + 2) = v3 + 1;
result = *(_QWORD *)(v6 + 16 * v3);
}
}
else
{
LABEL_12:
free(ptr);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(void *ptr)
{
__int64 *v1; // r12
unsigned int (__fastcall *v2)(_QWORD); // rbx
__int64 v3; // rax
__int64 v4; // rax
__int64 v5; // rdx
__int64 v6; // rdx
__int64 result; // rax

if ( !ptr )
return 0LL;
v1 = *(__int64 **)ptr;
v2 = (unsigned int (__fastcall *)(_QWORD))*((_QWORD *)ptr + 2);
if ( *(_QWORD *)ptr )
{
v3 = *((int *)ptr + 2);
while ( *((_DWORD *)v1 + 2) <= (int)v3 )
{
LABEL_11:
v1 = (__int64 *)v1[3];
v3 = 0LL;
*((_DWORD *)ptr + 2) = 0;
*(_QWORD *)ptr = v1;
if ( !v1 )
goto LABEL_12;
}
if ( v2 )
{
while ( !v2(*(_QWORD *)(*v1 + 16LL * (int)v3)) )
{
LODWORD(v3) = *((_DWORD *)ptr + 2) + 1;
*((_DWORD *)ptr + 2) = v3;
if ( (int)v3 >= *((_DWORD *)v1 + 2) )
goto LABEL_11;
}
v4 = *((int *)ptr + 2);
v5 = *v1;
*((_DWORD *)ptr + 2) = v4 + 1;
result = *(_QWORD *)(v5 + 16 * v4);
}
else
{
v6 = *v1;
*((_DWORD *)ptr + 2) = v3 + 1;
result = *(_QWORD *)(v6 + 16 * v3);
}
}
else
{
LABEL_12:
free(ptr);
result = 0LL;
}
return result;
}

original_code:
void *gr_iter_next(iter_t iter)
{
 gr_t gr;
 iter_return_p f;
 if (!iter) return ((void *)0);
 f = iter->f;
 recurse:
 gr = iter->gr;
 if (!gr) {
  free(iter);
  return ((void *)0);
 }
 while (iter->cur < gr->num) {
  if (f) {
   if ((*f)(gr->ptrs[iter->cur].data))
    return gr->ptrs[iter->cur++].data;
   else iter->cur++;
  }
  else return gr->ptrs[iter->cur++].data;
 }
 iter->gr = gr->chain;
 iter->cur = 0;
 goto recurse;
}

canonical_original_code:
void *gr_iter_next(struct iterator_context *iter)
{
 struct genresource *gr;
 int (*f)(void *);
 if (!iter) return ((void *)0);
 f = iter->f;
 recurse:
 gr = iter->gr;
 if (!gr) {
  free(iter);
  return ((void *)0);
 }
 while (iter->cur < gr->num) {
  if (f) {
   if ((*f)(gr->ptrs[iter->cur].data))
    return gr->ptrs[iter->cur++].data;
   else iter->cur++;
  }
  else return gr->ptrs[iter->cur++].data;
 }
 iter->gr = gr->chain;
 iter->cur = 0;
 goto recurse;
}

variable_types: struct iterator_context { struct genresource { struct resource_t { void *data; int (*ff)(void *); } *ptrs; int num; int max; struct genresource *next; struct genresource *chain; } *gr; int cur; int (*f)(void *); } *

variable_types:
f
int (*)(void *)

user_defined_types: struct iterator_context { struct genresource *gr; int cur; int (*f)(void *); }


repo: adsgray/CServer

decompiled_code:
_BOOL8 __fastcall gr_free_filed(void *ptr)
{
int v1; // ebx

v1 = close(*(_DWORD *)ptr);
free(ptr);
return v1 != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(void *ptr)
{
int v1; // ebx

v1 = close(*(_DWORD *)ptr);
free(ptr);
return v1 != 0;
}

original_code:
int gr_free_filed(void *foo)
{
 int ret_val = 0;
 struct filed_wrapper *fd = foo;
 if (fd >= 0 && close(fd->filed) != 0) ret_val = 1;
 free(foo);
 return ret_val;
}

variable_types:
foo
void *

variable_types:
fd
struct filed_wrapper { int filed; } *

return_type: int


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

result = new_handler();
*(_QWORD *)(result + 48) = SMTP_info_con;
return result;
}

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

result = new_handler();
*(_QWORD *)(result + 48) = SMTP_info_con;
return result;
}

original_code:
static handler_t SMTP_Info_handler()
{
 handler_t hd;
 hd = new_handler();
 hd->handle_con = SMTP_info_con;
 return hd;
}

canonical_original_code:
static struct _handler_t *SMTP_Info_handler()
{
 struct _handler_t *hd;
 hd = new_handler();
 hd->handle_con = SMTP_info_con;
 return hd;
}

variable_types: struct _handler_t { struct _queue_connector { void *prev; } _qcon; struct _server_t { char *name; char *pidfile; char *logfile; int logfd; int *stats; unsigned short port; int backlog; int numthreads; int maxcon; int uid; int listen_sk; struct handler_rec { unsigned long tid; struct _handler_t *hd; } *handlers; struct _handler_t *(*newhandler)(); void (*summary)(struct _server_t *, int); int (*destroy)(void *); void *(*start)(void *); struct _wait_queue_t { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } m; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } w; int num; int max; int num_refused; struct _queue_t { struct _queue_connector *first; void (*push)(struct _queue_t *, void *); void *(*pop)(struct _queue_t *); int (*destroy)(void *); } *cons_queue; int (*add_con)(struct _wait_queue_t *, struct _connection_t { struct _queue_connector _qcon; int sk; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *name; void *clientdata; struct _connection_t *prev; int (*destroy)(void *); } *); void (*wait_on)(struct _wait_queue_t *, struct _handler_t *); int (*destroy)(void *); } *wq; } *svr; int hd_state; struct _connection_t *con; void *(*start)(void *); void (*handle_con)(struct _handler_t *); char *(*get_state)(struct _handler_t *); int (*destroy)(void *); } *

user_defined_types: struct _handler_t { struct _queue_connector _qcon; struct _server_t *svr; int hd_state; struct _connection_t *con; void *(*start)(void *); void (*handle_con)(struct _handler_t *); char *(*get_state)(struct _handler_t *); int (*destroy)(void *); }

user_defined_types: struct _server_t { char *name; char *pidfile; char *logfile; int logfd; int *stats; unsigned short port; int backlog; int numthreads; int maxcon; int uid; int listen_sk; struct handler_rec *handlers; struct _handler_t *(*newhandler)(); void (*summary)(struct _server_t *, int); int (*destroy)(void *); void *(*start)(void *); struct _wait_queue_t *wq; }

user_defined_types: struct _wait_queue_t { union pthread_mutex_t m; union pthread_cond_t w; int num; int max; int num_refused; struct _queue_t *cons_queue; int (*add_con)(struct _wait_queue_t *, struct _connection_t *); void (*wait_on)(struct _wait_queue_t *, struct _handler_t *); int (*destroy)(void *); }

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

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

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

user_defined_types: struct _connection_t { struct _queue_connector _qcon; int sk; struct sockaddr_in *name; void *clientdata; struct _connection_t *prev; int (*destroy)(void *); }

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


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

result = new_handler();
*(_QWORD *)(result + 48) = SMTP_with_options;
return result;
}

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

result = new_handler();
*(_QWORD *)(result + 48) = SMTP_with_options;
return result;
}

original_code:
static handler_t SMTP_new_handler()
{
 handler_t hd;
 hd = new_handler();
 hd->handle_con = SMTP_with_options;
 return hd;
}

canonical_original_code:
static struct _handler_t *SMTP_new_handler()
{
 struct _handler_t *hd;
 hd = new_handler();
 hd->handle_con = SMTP_with_options;
 return hd;
}

return_type: struct _handler_t { struct _queue_connector { void *prev; } _qcon; struct _server_t { char *name; char *pidfile; char *logfile; int logfd; int *stats; unsigned short port; int backlog; int numthreads; int maxcon; int uid; int listen_sk; struct handler_rec { unsigned long tid; struct _handler_t *hd; } *handlers; struct _handler_t *(*newhandler)(); void (*summary)(struct _server_t *, int); int (*destroy)(void *); void *(*start)(void *); struct _wait_queue_t { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } m; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } w; int num; int max; int num_refused; struct _queue_t { struct _queue_connector *first; void (*push)(struct _queue_t *, void *); void *(*pop)(struct _queue_t *); int (*destroy)(void *); } *cons_queue; int (*add_con)(struct _wait_queue_t *, struct _connection_t { struct _queue_connector _qcon; int sk; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *name; void *clientdata; struct _connection_t *prev; int (*destroy)(void *); } *); void (*wait_on)(struct _wait_queue_t *, struct _handler_t *); int (*destroy)(void *); } *wq; } *svr; int hd_state; struct _connection_t *con; void *(*start)(void *); void (*handle_con)(struct _handler_t *); char *(*get_state)(struct _handler_t *); int (*destroy)(void *); } *


decompiled_code:
ssize_t __fastcall server_handler_summary(__int64 a1, int a2)
{
ssize_t result; // rax
__int64 v3; // rbx
__int64 v4; // r15
__int64 v5; // rdx
const char *v6; // rax
__int64 v7; // rdx
size_t v8; // rax
char v9[568]; // [rsp-238h] [rbp-238h] BYREF

result = *(unsigned int *)(a1 + 48);
if ( (int)result > 0 )
{
v3 = 0LL;
do
{
v4 = 16 * v3;
v5 = *(_QWORD *)(*(_QWORD *)(a1 + 64) + 16 * v3 + 8);
memset(v9, 0, 0x200uLL);
v6 = (const char *)(*(__int64 (__fastcall **)(__int64))(v5 + 56))(v5);
v7 = (unsigned int)v3++;
sprintf(v9, "handler %d: tid=0x%x state=%s\n", v7, *(_QWORD *)(*(_QWORD *)(a1 + 64) + v4), v6);
v8 = strlen(v9);
result = write(a2, v9, v8);
}
while ( *(_DWORD *)(a1 + 48) > (int)v3 );
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func2(__int64 a1, int a2)
{
ssize_t result; // rax
__int64 v3; // rbx
__int64 v4; // r15
__int64 v5; // rdx
const char *v6; // rax
__int64 v7; // rdx
size_t v8; // rax
char v9[568]; // [rsp-238h] [rbp-238h] BYREF

result = *(unsigned int *)(a1 + 48);
if ( (int)result > 0 )
{
v3 = 0LL;
do
{
v4 = 16 * v3;
v5 = *(_QWORD *)(*(_QWORD *)(a1 + 64) + 16 * v3 + 8);
memset(v9, 0, 0x200uLL);
v6 = (const char *)(*(__int64 (__fastcall **)(__int64))(v5 + 56))(v5);
v7 = (unsigned int)v3++;
sprintf(v9, "handler %d: tid=0x%x state=%s\n", v7, *(_QWORD *)(*(_QWORD *)(a1 + 64) + v4), v6);
v8 = strlen(v9);
result = write(a2, v9, v8);
}
while ( *(_DWORD *)(a1 + 48) > (int)v3 );
}
return result;
}

original_code:
static void server_handler_summary(server_t svr, int fd)
{
 char buf[512];
 int i;
 handler_t hd;
 for (i = 0; i < svr->numthreads; i++) {
  hd = svr->handlers[i].hd;
  memset((void *)buf, 0, 512);
  sprintf(buf, "handler %d: tid=0x%x state=%s\n",
   i, (int)svr->handlers[i].tid, hd->get_state(hd));
  (void)write(fd, buf, strlen(buf));
 }
}

canonical_original_code:
static void server_handler_summary(struct _server_t *svr, int fd)
{
 char buf[512];
 int i;
 struct _handler_t *hd;
 for (i = 0; i < svr->numthreads; i++) {
  hd = svr->handlers[i].hd;
  memset((void *)buf, 0, 512);
  sprintf(buf, "handler %d: tid=0x%x state=%s\n",
   i, (int)svr->handlers[i].tid, hd->get_state(hd));
  (void)write(fd, buf, strlen(buf));
 }
}

variable_types: struct _server_t { char *name; char *pidfile; char *logfile; int logfd; int *stats; unsigned short port; int backlog; int numthreads; int maxcon; int uid; int listen_sk; struct handler_rec { unsigned long tid; struct _handler_t { struct _queue_connector { void *prev; } _qcon; struct _server_t *svr; int hd_state; struct _connection_t { struct _queue_connector _qcon; int sk; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *name; void *clientdata; struct _connection_t *prev; int (*destroy)(void *); } *con; void *(*start)(void *); void (*handle_con)(struct _handler_t *); char *(*get_state)(struct _handler_t *); int (*destroy)(void *); } *hd; } *handlers; struct _handler_t *(*newhandler)(); void (*summary)(struct _server_t *, int); int (*destroy)(void *); void *(*start)(void *); struct _wait_queue_t { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } m; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } w; int num; int max; int num_refused; struct _queue_t { struct _queue_connector *first; void (*push)(struct _queue_t *, void *); void *(*pop)(struct _queue_t *); int (*destroy)(void *); } *cons_queue; int (*add_con)(struct _wait_queue_t *, struct _connection_t *); void (*wait_on)(struct _wait_queue_t *, struct _handler_t *); int (*destroy)(void *); } *wq; } *

variable_types: fd int

variable_types:
buf
char [512]

variable_types: i int

return_type: void

user_defined_types: struct handler_rec { unsigned long tid; struct _handler_t *hd; }


decompiled_code:
// bad sp value at call has been detected, the output may be wrong!
void __fastcall SMTP_summary(__int64 a1, int a2)
{
__int64 v2; // r8
size_t v3; // rax
size_t v4; // rax
unsigned int *v5; // rax
const char *v6; // rdx
__int64 v7; // r9
__int64 v8; // r8
size_t v9; // rax
__int64 v10; // r9
__int64 v11; // r8
__int64 v12; // rcx
size_t v13; // rax
_QWORD v14[69]; // [rsp+60h] [rbp-228h] BYREF

if ( a1 )
{
v2 = *(unsigned __int16 *)(a1 + 40);
memset(v14, 0, 0x200uLL);
sprintf(
(char *)v14,
"%s pidf=%s port=%d numth=%d maxcon=%d\n uid=%d numcon=%d num_ref=%d\n\n",
*(_QWORD *)a1,
*(_QWORD *)(a1 + 8),
v2,
*(unsigned int *)(a1 + 48),
*(unsigned int *)(a1 + 52),
*(unsigned int *)(a1 + 56),
*(unsigned int *)(*(_QWORD *)(a1 + 104) + 88LL),
*(unsigned int *)(*(_QWORD *)(a1 + 104) + 96LL));
v3 = strlen((const char *)v14);
write(a2, v14, v3);
server_handler_summary(a1, a2);
memset(v14, 0, 0x200uLL);
sprintf(
(char *)v14,
"%s pidf=%s logf=%s port=%d numth=%d\n maxcon=%d uid=%d numcon=%d num_ref=%d\n\n",
*(const char **)SMTP_server,
*(const char **)(SMTP_server + 8),
*(const char **)(SMTP_server + 16),
*(unsigned __int16 *)(SMTP_server + 40),
*(unsigned int *)(SMTP_server + 48),
*(unsigned int *)(SMTP_server + 52),
*(unsigned int *)(SMTP_server + 56),
*(unsigned int *)(*(_QWORD *)(SMTP_server + 104) + 88LL),
*(unsigned int *)(*(_QWORD *)(SMTP_server + 104) + 96LL));
v4 = strlen((const char *)v14);
write(a2, v14, v4);
server_handler_summary(SMTP_server, a2);
memset(v14, 0, 0x200uLL);
v5 = *(unsigned int **)(SMTP_server + 32);
v6 = *(const char **)SMTP_server;
v7 = v5[2];
v8 = v5[1];
v14[1] = v5[4];
v14[0] = v5[3];
sprintf((char *)v14, "%s cons=%d bytes=%d 4xx=%d 5xx=%d dropped=%d\n\n", v6, *v5, v8, v7);
v9 = strlen((const char *)v14);
write(a2, v14, v9);
memset(v14, 0, 0x200uLL);
v10 = *(unsigned int *)(options + 12);
v11 = *(unsigned int *)(options + 8);
v12 = *(unsigned int *)(options + 4);
LODWORD(v14[0]) = *(_DWORD *)(options + 16);
sprintf(
(char *)v14,
"min_delay=%d max_delay=%d percent_4xx=%d percent_5xx=%d percent_dropped=%d\n\n",
*(unsigned int *)options,
v12,
v11,
v10);
v13 = strlen((const char *)v14);
write(a2, v14, v13);
}
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1, int a2)
{
__int64 v2; // r8
size_t v3; // rax
size_t v4; // rax
unsigned int *v5; // rax
const char *v6; // rdx
__int64 v7; // r9
__int64 v8; // r8
size_t v9; // rax
__int64 v10; // r9
__int64 v11; // r8
__int64 v12; // rcx
size_t v13; // rax
_QWORD v14[69]; // [rsp+60h] [rbp-228h] BYREF

if ( a1 )
{
v2 = *(unsigned __int16 *)(a1 + 40);
memset(v14, 0, 0x200uLL);
sprintf(
(char *)v14,
"%s pidf=%s port=%d numth=%d maxcon=%d\n uid=%d numcon=%d num_ref=%d\n\n",
*(_QWORD *)a1,
*(_QWORD *)(a1 + 8),
v2,
*(unsigned int *)(a1 + 48),
*(unsigned int *)(a1 + 52),
*(unsigned int *)(a1 + 56),
*(unsigned int *)(*(_QWORD *)(a1 + 104) + 88LL),
*(unsigned int *)(*(_QWORD *)(a1 + 104) + 96LL));
v3 = strlen((const char *)v14);
write(a2, v14, v3);
func2(a1, a2);
memset(v14, 0, 0x200uLL);
sprintf(
(char *)v14,
"%s pidf=%s logf=%s port=%d numth=%d\n maxcon=%d uid=%d numcon=%d num_ref=%d\n\n",
*(const char **)SMTP_server,
*(const char **)(SMTP_server + 8),
*(const char **)(SMTP_server + 16),
*(unsigned __int16 *)(SMTP_server + 40),
*(unsigned int *)(SMTP_server + 48),
*(unsigned int *)(SMTP_server + 52),
*(unsigned int *)(SMTP_server + 56),
*(unsigned int *)(*(_QWORD *)(SMTP_server + 104) + 88LL),
*(unsigned int *)(*(_QWORD *)(SMTP_server + 104) + 96LL));
v4 = strlen((const char *)v14);
write(a2, v14, v4);
func2(SMTP_server, a2);
memset(v14, 0, 0x200uLL);
v5 = *(unsigned int **)(SMTP_server + 32);
v6 = *(const char **)SMTP_server;
v7 = v5[2];
v8 = v5[1];
v14[1] = v5[4];
v14[0] = v5[3];
sprintf((char *)v14, "%s cons=%d bytes=%d 4xx=%d 5xx=%d dropped=%d\n\n", v6, *v5, v8, v7);
v9 = strlen((const char *)v14);
write(a2, v14, v9);
memset(v14, 0, 0x200uLL);
v10 = *(unsigned int *)(options + 12);
v11 = *(unsigned int *)(options + 8);
v12 = *(unsigned int *)(options + 4);
LODWORD(v14[0]) = *(_DWORD *)(options + 16);
sprintf(
(char *)v14,
"min_delay=%d max_delay=%d percent_4xx=%d percent_5xx=%d percent_dropped=%d\n\n",
*(unsigned int *)options,
v12,
v11,
v10);
v13 = strlen((const char *)v14);
write(a2, v14, v13);
}
}

original_code:
static void SMTP_summary(server_t self, int fd)
{
 char buf[512];
 if (!self) return;
 memset((void *)buf, 0, 512);
 sprintf(buf, "%s pidf=%s port=%d numth=%d maxcon=%d\n uid=%d numcon=%d num_ref=%d\n\n",
  self->name, self->pidfile, self->port, self->numthreads,
  self->maxcon, self->uid, self->wq->num, self->wq->num_refused);
 (void)write(fd, buf, strlen(buf));
 server_handler_summary(self, fd);
 memset((void *)buf, 0, 512);
 sprintf(buf, "%s pidf=%s logf=%s port=%d numth=%d\n maxcon=%d uid=%d numcon=%d num_ref=%d\n\n",
  SMTP_server->name, SMTP_server->pidfile, SMTP_server->logfile,
  SMTP_server->port, SMTP_server->numthreads,
  SMTP_server->maxcon, SMTP_server->uid,
  SMTP_server->wq->num, SMTP_server->wq->num_refused);
 (void)write(fd, buf, strlen(buf));
 server_handler_summary(SMTP_server, fd);
 memset((void *)buf, 0, 512);
 sprintf(buf, "%s cons=%d bytes=%d 4xx=%d 5xx=%d dropped=%d\n\n",
  SMTP_server->name,
  SMTP_server->stats[SMTP_connections_processed],
  SMTP_server->stats[SMTP_bytes_processed],
  SMTP_server->stats[SMTP_4xx],
  SMTP_server->stats[SMTP_5xx],
  SMTP_server->stats[SMTP_dropped]);
 (void)write(fd, buf, strlen(buf));
 memset((void *)buf, 0, 512);
 sprintf(buf, "min_delay=%d max_delay=%d percent_4xx=%d percent_5xx=%d percent_dropped=%d\n\n",
  options->min_delay, options->max_delay, options->percent_4xx,
  options->percent_5xx, options->percent_dropped);
 (void)write(fd, buf, strlen(buf));
}

canonical_original_code:
static void SMTP_summary(struct _server_t *self, int fd)
{
 char buf[512];
 if (!self) return;
 memset((void *)buf, 0, 512);
 sprintf(buf, "%s pidf=%s port=%d numth=%d maxcon=%d\n uid=%d numcon=%d num_ref=%d\n\n",
  self->name, self->pidfile, self->port, self->numthreads,
  self->maxcon, self->uid, self->wq->num, self->wq->num_refused);
 (void)write(fd, buf, strlen(buf));
 server_handler_summary(self, fd);
 memset((void *)buf, 0, 512);
 sprintf(buf, "%s pidf=%s logf=%s port=%d numth=%d\n maxcon=%d uid=%d numcon=%d num_ref=%d\n\n",
  SMTP_server->name, SMTP_server->pidfile, SMTP_server->logfile,
  SMTP_server->port, SMTP_server->numthreads,
  SMTP_server->maxcon, SMTP_server->uid,
  SMTP_server->wq->num, SMTP_server->wq->num_refused);
 (void)write(fd, buf, strlen(buf));
 server_handler_summary(SMTP_server, fd);
 memset((void *)buf, 0, 512);
 sprintf(buf, "%s cons=%d bytes=%d 4xx=%d 5xx=%d dropped=%d\n\n",
  SMTP_server->name,
  SMTP_server->stats[SMTP_connections_processed],
  SMTP_server->stats[SMTP_bytes_processed],
  SMTP_server->stats[SMTP_4xx],
  SMTP_server->stats[SMTP_5xx],
  SMTP_server->stats[SMTP_dropped]);
 (void)write(fd, buf, strlen(buf));
 memset((void *)buf, 0, 512);
 sprintf(buf, "min_delay=%d max_delay=%d percent_4xx=%d percent_5xx=%d percent_dropped=%d\n\n",
  options->min_delay, options->max_delay, options->percent_4xx,
  options->percent_5xx, options->percent_dropped);
 (void)write(fd, buf, strlen(buf));
}

user_defined_types: struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; }

user_defined_types: struct _queue_t { struct _queue_connector *first; void (*push)(struct _queue_t *, void *); void *(*pop)(struct _queue_t *); int (*destroy)(void *); }

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


decompiled_code:
__int64 __fastcall SMTP_set_command(char *haystack)
{
unsigned int v1; // er12
char *v2; // rax
char *v4; // rax
char *v5; // rax
char *v6; // rax
char *v7; // rax

v1 = 0;
if ( !*haystack )
return v1;
v2 = strstr(haystack, "min_delay");
if ( v2 )
{
v1 = 1;
*(_DWORD *)options = atoi(v2 + 10);
return v1;
}
v4 = strstr(haystack, "max_delay");
if ( !v4 )
{
v5 = strstr(haystack, "percent_4xx");
if ( v5 )
{
v1 = 1;
*(_DWORD *)(options + 8) = atoi(v5 + 12);
}
else
{
v6 = strstr(haystack, "percent_5xx");
if ( v6 )
{
v1 = 1;
*(_DWORD *)(options + 12) = atoi(v6 + 12);
}
else
{
v7 = strstr(haystack, "percent_dropped");
if ( v7 )
{
v1 = 1;
*(_DWORD *)(options + 16) = atoi(v7 + 16);
}
}
}
return v1;
}
*(_DWORD *)(options + 4) = atoi(v4 + 10);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *haystack)
{
unsigned int v1; // er12
char *v2; // rax
char *v4; // rax
char *v5; // rax
char *v6; // rax
char *v7; // rax

v1 = 0;
if ( !*haystack )
return v1;
v2 = strstr(haystack, "min_delay");
if ( v2 )
{
v1 = 1;
*(_DWORD *)options = atoi(v2 + 10);
return v1;
}
v4 = strstr(haystack, "max_delay");
if ( !v4 )
{
v5 = strstr(haystack, "percent_4xx");
if ( v5 )
{
v1 = 1;
*(_DWORD *)(options + 8) = atoi(v5 + 12);
}
else
{
v6 = strstr(haystack, "percent_5xx");
if ( v6 )
{
v1 = 1;
*(_DWORD *)(options + 12) = atoi(v6 + 12);
}
else
{
v7 = strstr(haystack, "percent_dropped");
if ( v7 )
{
v1 = 1;
*(_DWORD *)(options + 16) = atoi(v7 + 16);
}
}
}
return v1;
}
*(_DWORD *)(options + 4) = atoi(v4 + 10);
return 1LL;
}

original_code:
static int SMTP_set_command(char *cmd)
{
 int num;
 char *pos;
 if (!strlen(cmd)) return 0;
 if ((pos = strstr(cmd, "min_delay")) != ((void *)0)) {
  pos += strlen("min_delay") + 1;
  num = atoi(pos);
  options->min_delay = num;
 }
 else if ((pos = strstr(cmd, "max_delay")) != ((void *)0)) {
  pos += strlen("max_delay") + 1;
  num = atoi(pos);
  options->max_delay = num;
 }
 else if ((pos = strstr(cmd, "percent_4xx")) != ((void *)0)) {
  pos += strlen("percent_4xx") + 1;
  num = atoi(pos);
  options->percent_4xx = num;
 }
 else if ((pos = strstr(cmd, "percent_5xx")) != ((void *)0)) {
  pos += strlen("percent_5xx") + 1;
  num = atoi(pos);
  options->percent_5xx = num;
 }
 else if ((pos = strstr(cmd, "percent_dropped")) != ((void *)0)) {
  pos += strlen("percent_dropped") + 1;
  num = atoi(pos);
  options->percent_dropped = num;
 } else return 0;
 return 1;
}

variable_types:
cmd
char *

variable_types: num int

variable_types:
pos
char *


decompiled_code:
void __fastcall SMTP_info_con(__int64 a1)
{
__int64 v1; // rbx
const char **v2; // r12
const char *v3; // rbp
size_t v4; // rax
_DWORD v5[70]; // [rsp+0h] [rbp-118h] BYREF

v1 = *(_QWORD *)(a1 + 32);
if ( v1 )
{
*(_DWORD *)(a1 + 24) = 1;
v2 = *(const char ***)(a1 + 16);
fwrite("SMTP_info_con\n", 1uLL, 0xEuLL, stderr);
v3 = *v2;
v4 = strlen(*v2);
write(*(_DWORD *)(v1 + 16), v3, v4);
do
{
if ( write(*(_DWORD *)(v1 + 16), "> ", 2uLL) <= 0 )
break;
memset(v5, 0, 0x100uLL);
if ( !read(*(_DWORD *)(v1 + 16), v5, 0xFFuLL) )
break;
switch ( v5[0] )
{
case 0x6F666E69:
SMTP_summary((__int64)v2, *(_DWORD *)(v1 + 16));
break;
case 0x6C6C696B:
(*(void (__fastcall **)(__int64))(SMTP_server + 88))(SMTP_server);
if ( write(*(_DWORD *)(v1 + 16), "SMTP_server destroyed... now what?\r\n", 0x24uLL) <= 0 )
goto LABEL_11;
break;
case 0x706C6568:
if ( write(*(_DWORD *)(v1 + 16), "Commands:\r\n", 0xBuLL) <= 0
|| write(*(_DWORD *)(v1 + 16), "info -- get info on threads.\r\n", 0x24uLL) <= 0
|| write(*(_DWORD *)(v1 + 16), "kill -- kill server. Currently unclean.\r\n", 0x2FuLL) <= 0
|| write(*(_DWORD *)(v1 + 16), "set opt n -- set a server variable.\r\n", 0x26uLL) <= 0
|| write(*(_DWORD *)(v1 + 16), " recognized opts are:\r\n", 0x24uLL) <= 0
|| write(*(_DWORD *)(v1 + 16), " min_delay: minimum delay for responses\r\n", 0x2FuLL) <= 0
|| write(*(_DWORD *)(v1 + 16), " max_delay: maximum delay for responses\r\n", 0x2FuLL) <= 0
|| write(*(_DWORD *)(v1 + 16), " percent_4xx: percentage of 4xx responses\r\n", 0x2FuLL) <= 0
|| write(*(_DWORD *)(v1 + 16), " percent_5xx: percentage of 5xx responses\r\n", 0x2FuLL) <= 0
|| write(*(_DWORD *)(v1 + 16), " percent_dropped: probability of dropping con NOW\r\n", 0x33uLL) <= 0
|| write(*(_DWORD *)(v1 + 16), "\r\n", 2uLL) <= 0 )
{
goto LABEL_11;
}
break;
case 0x20746573:
if ( !(unsigned int)SMTP_set_command((char *)&v5[1])
&& write(*(_DWORD *)(v1 + 16), "set command error\r\n", 0x13uLL) <= 0 )
{
goto LABEL_11;
}
break;
default:
if ( write(*(_DWORD *)(v1 + 16), "type \"help\" for help\r\n", 0x16uLL) <= 0 )
goto LABEL_11;
break;
}
}
while ( v5[0] != 1953068401 );
LABEL_11:
(*(void (__fastcall **)(__int64))(v1 + 56))(v1);
}
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1)
{
__int64 v1; // rbx
const char **v2; // r12
const char *v3; // rbp
size_t v4; // rax
_DWORD v5[70]; // [rsp+0h] [rbp-118h] BYREF

v1 = *(_QWORD *)(a1 + 32);
if ( v1 )
{
*(_DWORD *)(a1 + 24) = 1;
v2 = *(const char ***)(a1 + 16);
fwrite("SMTP_info_con\n", 1uLL, 0xEuLL, stderr);
v3 = *v2;
v4 = strlen(*v2);
write(*(_DWORD *)(v1 + 16), v3, v4);
do
{
if ( write(*(_DWORD *)(v1 + 16), "> ", 2uLL) <= 0 )
break;
memset(v5, 0, 0x100uLL);
if ( !read(*(_DWORD *)(v1 + 16), v5, 0xFFuLL) )
break;
switch ( v5[0] )
{
case 0x6F666E69:
func3((__int64)v2, *(_DWORD *)(v1 + 16));
break;
case 0x6C6C696B:
(*(void (__fastcall **)(__int64))(SMTP_server + 88))(SMTP_server);
if ( write(*(_DWORD *)(v1 + 16), "SMTP_server destroyed... now what?\r\n", 0x24uLL) <= 0 )
goto LABEL_11;
break;
case 0x706C6568:
if ( write(*(_DWORD *)(v1 + 16), "Commands:\r\n", 0xBuLL) <= 0
|| write(*(_DWORD *)(v1 + 16), "info -- get info on threads.\r\n", 0x24uLL) <= 0
|| write(*(_DWORD *)(v1 + 16), "kill -- kill server. Currently unclean.\r\n", 0x2FuLL) <= 0
|| write(*(_DWORD *)(v1 + 16), "set opt n -- set a server variable.\r\n", 0x26uLL) <= 0
|| write(*(_DWORD *)(v1 + 16), " recognized opts are:\r\n", 0x24uLL) <= 0
|| write(*(_DWORD *)(v1 + 16), " min_delay: minimum delay for responses\r\n", 0x2FuLL) <= 0
|| write(*(_DWORD *)(v1 + 16), " max_delay: maximum delay for responses\r\n", 0x2FuLL) <= 0
|| write(*(_DWORD *)(v1 + 16), " percent_4xx: percentage of 4xx responses\r\n", 0x2FuLL) <= 0
|| write(*(_DWORD *)(v1 + 16), " percent_5xx: percentage of 5xx responses\r\n", 0x2FuLL) <= 0
|| write(*(_DWORD *)(v1 + 16), " percent_dropped: probability of dropping con NOW\r\n", 0x33uLL) <= 0
|| write(*(_DWORD *)(v1 + 16), "\r\n", 2uLL) <= 0 )
{
goto LABEL_11;
}
break;
case 0x20746573:
if ( !(unsigned int)func4((char *)&v5[1])
&& write(*(_DWORD *)(v1 + 16), "set command error\r\n", 0x13uLL) <= 0 )
{
goto LABEL_11;
}
break;
default:
if ( write(*(_DWORD *)(v1 + 16), "type \"help\" for help\r\n", 0x16uLL) <= 0 )
goto LABEL_11;
break;
}
}
while ( v5[0] != 1953068401 );
LABEL_11:
(*(void (__fastcall **)(__int64))(v1 + 56))(v1);
}
}

original_code:
static void SMTP_info_con(handler_t self)
{
 char buf[256];
 size_t n;
 server_t svr = self->svr;
 connection_t con = self->con;
 char *info_prompt = "> ";
 if (!con) return;
 self->hd_state = HD_ACTIVE;
 fprintf(stderr, "SMTP_info_con\n");
 (void)write(con->sk, svr->name, strlen(svr->name));
 do {
  if ( (strlen(info_prompt) > 0) && (write(con->sk, (void *)info_prompt, strlen(info_prompt)) <= 0) ) goto out;
                memset((void *)buf, 0, 256);
  n = read(con->sk, (void *)buf, 255);
  if (n > 0) {
   if (strncmp(buf, "info", 4) == 0)
    SMTP_summary(svr, con->sk);
   else if (strncmp(buf, "kill", 4) == 0) {
                                          ;
    SMTP_server->destroy(SMTP_server);
    if ( (strlen("SMTP_server destroyed... now what?\r\n") > 0) && (write(con->sk, (void *)"SMTP_server destroyed... now what?\r\n", strlen("SMTP_server destroyed... now what?\r\n")) <= 0) ) goto out;
   }
   else if (strncmp(buf, "help", 4) == 0) {
    if ( (strlen("Commands:\r\n") > 0) && (write(con->sk, (void *)"Commands:\r\n", strlen("Commands:\r\n")) <= 0) ) goto out;
    if ( (strlen("info       -- get info on threads.\r\n") > 0) && (write(con->sk, (void *)"info       -- get info on threads.\r\n", strlen("info       -- get info on threads.\r\n")) <= 0) ) goto out;
    if ( (strlen("kill       -- kill server. Currently unclean.\r\n") > 0) && (write(con->sk, (void *)"kill       -- kill server. Currently unclean.\r\n", strlen("kill       -- kill server. Currently unclean.\r\n")) <= 0) ) goto out;
    if ( (strlen("set opt n  -- set a server variable.\r\n") > 0) && (write(con->sk, (void *)"set opt n  -- set a server variable.\r\n", strlen("set opt n  -- set a server variable.\r\n")) <= 0) ) goto out;
    if ( (strlen("              recognized opts are:\r\n") > 0) && (write(con->sk, (void *)"              recognized opts are:\r\n", strlen("              recognized opts are:\r\n")) <= 0) ) goto out;
    if ( (strlen("       min_delay: minimum delay for responses\r\n") > 0) && (write(con->sk, (void *)"       min_delay: minimum delay for responses\r\n", strlen("       min_delay: minimum delay for responses\r\n")) <= 0) ) goto out;
    if ( (strlen("       max_delay: maximum delay for responses\r\n") > 0) && (write(con->sk, (void *)"       max_delay: maximum delay for responses\r\n", strlen("       max_delay: maximum delay for responses\r\n")) <= 0) ) goto out;
    if ( (strlen("     percent_4xx: percentage of 4xx responses\r\n") > 0) && (write(con->sk, (void *)"     percent_4xx: percentage of 4xx responses\r\n", strlen("     percent_4xx: percentage of 4xx responses\r\n")) <= 0) ) goto out;
    if ( (strlen("     percent_5xx: percentage of 5xx responses\r\n") > 0) && (write(con->sk, (void *)"     percent_5xx: percentage of 5xx responses\r\n", strlen("     percent_5xx: percentage of 5xx responses\r\n")) <= 0) ) goto out;
    if ( (strlen(" percent_dropped: probability of dropping con NOW\r\n") > 0) && (write(con->sk, (void *)" percent_dropped: probability of dropping con NOW\r\n", strlen(" percent_dropped: probability of dropping con NOW\r\n")) <= 0) ) goto out;
    if ( (strlen("\r\n") > 0) && (write(con->sk, (void *)"\r\n", strlen("\r\n")) <= 0) ) goto out;
   }
   else if (strncmp(buf, "set ", 4) == 0) {
    if (!SMTP_set_command(buf + 4))
     if ( (strlen("set command error\r\n") > 0) && (write(con->sk, (void *)"set command error\r\n", strlen("set command error\r\n")) <= 0) ) goto out;
   } else {
    if ( (strlen("type \"help\" for help\r\n") > 0) && (write(con->sk, (void *)"type \"help\" for help\r\n", strlen("type \"help\" for help\r\n")) <= 0) ) goto out;
   }
  } else if (n == -1) {
   fprintf(stderr, "SMTP_info_con: closing connection\n");
   goto out;
  } else {
   goto out;
  }
 } while (strncmp(buf, "quit", 4) != 0);
 out:
 con->destroy(con);
}

canonical_original_code:
static void SMTP_info_con(struct _handler_t *self)
{
 char buf[256];
 unsigned long n;
 struct _server_t *svr = self->svr;
 struct _connection_t *con = self->con;
 char *info_prompt = "> ";
 if (!con) return;
 self->hd_state = HD_ACTIVE;
 fprintf(stderr, "SMTP_info_con\n");
 (void)write(con->sk, svr->name, strlen(svr->name));
 do {
  if ( (strlen(info_prompt) > 0) && (write(con->sk, (void *)info_prompt, strlen(info_prompt)) <= 0) ) goto out;
                memset((void *)buf, 0, 256);
  n = read(con->sk, (void *)buf, 255);
  if (n > 0) {
   if (strncmp(buf, "info", 4) == 0)
    SMTP_summary(svr, con->sk);
   else if (strncmp(buf, "kill", 4) == 0) {
                                          ;
    SMTP_server->destroy(SMTP_server);
    if ( (strlen("SMTP_server destroyed... now what?\r\n") > 0) && (write(con->sk, (void *)"SMTP_server destroyed... now what?\r\n", strlen("SMTP_server destroyed... now what?\r\n")) <= 0) ) goto out;
   }
   else if (strncmp(buf, "help", 4) == 0) {
    if ( (strlen("Commands:\r\n") > 0) && (write(con->sk, (void *)"Commands:\r\n", strlen("Commands:\r\n")) <= 0) ) goto out;
    if ( (strlen("info       -- get info on threads.\r\n") > 0) && (write(con->sk, (void *)"info       -- get info on threads.\r\n", strlen("info       -- get info on threads.\r\n")) <= 0) ) goto out;
    if ( (strlen("kill       -- kill server. Currently unclean.\r\n") > 0) && (write(con->sk, (void *)"kill       -- kill server. Currently unclean.\r\n", strlen("kill       -- kill server. Currently unclean.\r\n")) <= 0) ) goto out;
    if ( (strlen("set opt n  -- set a server variable.\r\n") > 0) && (write(con->sk, (void *)"set opt n  -- set a server variable.\r\n", strlen("set opt n  -- set a server variable.\r\n")) <= 0) ) goto out;
    if ( (strlen("              recognized opts are:\r\n") > 0) && (write(con->sk, (void *)"              recognized opts are:\r\n", strlen("              recognized opts are:\r\n")) <= 0) ) goto out;
    if ( (strlen("       min_delay: minimum delay for responses\r\n") > 0) && (write(con->sk, (void *)"       min_delay: minimum delay for responses\r\n", strlen("       min_delay: minimum delay for responses\r\n")) <= 0) ) goto out;
    if ( (strlen("       max_delay: maximum delay for responses\r\n") > 0) && (write(con->sk, (void *)"       max_delay: maximum delay for responses\r\n", strlen("       max_delay: maximum delay for responses\r\n")) <= 0) ) goto out;
    if ( (strlen("     percent_4xx: percentage of 4xx responses\r\n") > 0) && (write(con->sk, (void *)"     percent_4xx: percentage of 4xx responses\r\n", strlen("     percent_4xx: percentage of 4xx responses\r\n")) <= 0) ) goto out;
    if ( (strlen("     percent_5xx: percentage of 5xx responses\r\n") > 0) && (write(con->sk, (void *)"     percent_5xx: percentage of 5xx responses\r\n", strlen("     percent_5xx: percentage of 5xx responses\r\n")) <= 0) ) goto out;
    if ( (strlen(" percent_dropped: probability of dropping con NOW\r\n") > 0) && (write(con->sk, (void *)" percent_dropped: probability of dropping con NOW\r\n", strlen(" percent_dropped: probability of dropping con NOW\r\n")) <= 0) ) goto out;
    if ( (strlen("\r\n") > 0) && (write(con->sk, (void *)"\r\n", strlen("\r\n")) <= 0) ) goto out;
   }
   else if (strncmp(buf, "set ", 4) == 0) {
    if (!SMTP_set_command(buf + 4))
     if ( (strlen("set command error\r\n") > 0) && (write(con->sk, (void *)"set command error\r\n", strlen("set command error\r\n")) <= 0) ) goto out;
   } else {
    if ( (strlen("type \"help\" for help\r\n") > 0) && (write(con->sk, (void *)"type \"help\" for help\r\n", strlen("type \"help\" for help\r\n")) <= 0) ) goto out;
   }
  } else if (n == -1) {
   fprintf(stderr, "SMTP_info_con: closing connection\n");
   goto out;
  } else {
   goto out;
  }
 } while (strncmp(buf, "quit", 4) != 0);
 out:
 con->destroy(con);
}

variable_types:
buf
char [256]

variable_types:
n
unsigned long

variable_types: struct _connection_t { struct _queue_connector { void *prev; } _qcon; int sk; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *name; void *clientdata; struct _connection_t *prev; int (*destroy)(void *); } *


predicate

original_code:
static int predicate(int percent)
{
 int foo;
 foo = ((rand() >> 5) % 100) + 1;
 return (foo <= percent);
}

variable_types: percent int

variable_types: foo int


decompiled_code:
_BOOL8 random_close()
{
return predicate(*(_DWORD *)(options + 16));
}

canonical_decompiled_code:
_BOOL8 func7()
{
return func6(*(_DWORD *)(options + 16));
}

original_code:
static int random_close()
{
 return predicate(options->percent_dropped);
}


decompiled_code:
const char *__fastcall get_random_reply(__int64 a1, int *a2)
{
__int64 v2; // r13
_BOOL4 v3; // er12
_BOOL4 v4; // edx
int v5; // eax
const char *result; // rax

v2 = options;
v3 = predicate(*(_DWORD *)(options + 8));
v4 = predicate(*(_DWORD *)(v2 + 12));
v5 = *a2;
if ( *a2 == 6 )
{
if ( v3 )
{
pthread_mutex_lock(&stat_mutex);
++*(_DWORD *)(*(_QWORD *)(a1 + 32) + 8LL);
pthread_mutex_unlock(&stat_mutex);
result = "450 temp error\r\n";
}
else
{
result = "250 looks good\r\n";
if ( v4 )
{
pthread_mutex_lock(&stat_mutex);
++*(_DWORD *)(*(_QWORD *)(a1 + 32) + 12LL);
pthread_mutex_unlock(&stat_mutex);
result = "550 permanent error\r\n";
}
}
}
else
{
if ( v5 == 8 )
{
*a2 = 5;
return "250 looks good\r\n";
}
if ( v5 != 5 )
return "250 looks good\r\n";
if ( v4 )
{
pthread_mutex_lock(&stat_mutex);
++*(_DWORD *)(*(_QWORD *)(a1 + 32) + 12LL);
pthread_mutex_unlock(&stat_mutex);
*a2 = 5;
result = "550 permanent error\r\n";
}
else
{
*a2 = 6;
result = "250 looks good\r\n";
}
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func8(__int64 a1, int *a2)
{
__int64 v2; // r13
_BOOL4 v3; // er12
_BOOL4 v4; // edx
int v5; // eax
const char *result; // rax

v2 = options;
v3 = func6(*(_DWORD *)(options + 8));
v4 = func6(*(_DWORD *)(v2 + 12));
v5 = *a2;
if ( *a2 == 6 )
{
if ( v3 )
{
pthread_mutex_lock(&stat_mutex);
++*(_DWORD *)(*(_QWORD *)(a1 + 32) + 8LL);
pthread_mutex_unlock(&stat_mutex);
result = "450 temp error\r\n";
}
else
{
result = "250 looks good\r\n";
if ( v4 )
{
pthread_mutex_lock(&stat_mutex);
++*(_DWORD *)(*(_QWORD *)(a1 + 32) + 12LL);
pthread_mutex_unlock(&stat_mutex);
result = "550 permanent error\r\n";
}
}
}
else
{
if ( v5 == 8 )
{
*a2 = 5;
return "250 looks good\r\n";
}
if ( v5 != 5 )
return "250 looks good\r\n";
if ( v4 )
{
pthread_mutex_lock(&stat_mutex);
++*(_DWORD *)(*(_QWORD *)(a1 + 32) + 12LL);
pthread_mutex_unlock(&stat_mutex);
*a2 = 5;
result = "550 permanent error\r\n";
}
else
{
*a2 = 6;
result = "250 looks good\r\n";
}
}
return result;
}

original_code:
static char *get_random_reply(server_t svr,
         SMTP_cstate *st)
{
 char *rval = "250 looks good\r\n";
 int p4xx, p5xx;
 p4xx = predicate(options->percent_4xx);
 p5xx = predicate(options->percent_5xx);
 switch(*st) {
 case ST_HELO:
 case ST_DATA:
  break;
 case ST_DOT:
  *st = ST_MAIL_FROM;
  break;
 case ST_MAIL_FROM:
  if (p5xx) {
   pthread_mutex_lock(&stat_mutex); svr->stats[SMTP_5xx] += 1; pthread_mutex_unlock(&stat_mutex);
   rval = "550 permanent error\r\n";
   *st = ST_MAIL_FROM;
  } else {
   *st = ST_RCPT_TO;
  }
  break;
 case ST_RCPT_TO:
  if (p4xx) {
   pthread_mutex_lock(&stat_mutex); svr->stats[SMTP_4xx] += 1; pthread_mutex_unlock(&stat_mutex);
   rval = "450 temp error\r\n";
  }
  else if (p5xx) {
   pthread_mutex_lock(&stat_mutex); svr->stats[SMTP_5xx] += 1; pthread_mutex_unlock(&stat_mutex);
   rval = "550 permanent error\r\n";
  }
  break;
 }
 return rval;
}

canonical_original_code:
static char *get_random_reply(struct _server_t *svr, enum SMTP_cstate *st)
{
 char *rval = "250 looks good\r\n";
 int p4xx; int p5xx;
 p4xx = predicate(options->percent_4xx);
 p5xx = predicate(options->percent_5xx);
 switch(*st) {
 case ST_HELO:
 case ST_DATA:
  break;
 case ST_DOT:
  *st = ST_MAIL_FROM;
  break;
 case ST_MAIL_FROM:
  if (p5xx) {
   pthread_mutex_lock(&stat_mutex); svr->stats[SMTP_5xx] += 1; pthread_mutex_unlock(&stat_mutex);
   rval = "550 permanent error\r\n";
   *st = ST_MAIL_FROM;
  } else {
   *st = ST_RCPT_TO;
  }
  break;
 case ST_RCPT_TO:
  if (p4xx) {
   pthread_mutex_lock(&stat_mutex); svr->stats[SMTP_4xx] += 1; pthread_mutex_unlock(&stat_mutex);
   rval = "450 temp error\r\n";
  }
  else if (p5xx) {
   pthread_mutex_lock(&stat_mutex); svr->stats[SMTP_5xx] += 1; pthread_mutex_unlock(&stat_mutex);
   rval = "550 permanent error\r\n";
  }
  break;
 }
 return rval;
}

variable_types: enum SMTP_cstate { ST_HELO=expr, ST_MAIL_FROM=expr, ST_RCPT_TO=expr, ST_DATA=expr, ST_DOT=expr, ST_QUIT=expr } *

variable_types:
rval
char *

return_type: char *


decompiled_code:
void __fastcall SMTP_with_options(__int64 a1)
{
__int64 v1; // r13
__int64 v2; // r12
ssize_t v3; // rbx
unsigned int v4; // ebx
size_t v5; // rdx
const char *v6; // rsi
const char *v7; // rax
unsigned int v8; // [rsp+1Ch] [rbp-12Ch] BYREF
char buf[296]; // [rsp+20h] [rbp-128h] BYREF

v1 = *(_QWORD *)(a1 + 32);
v2 = *(_QWORD *)(a1 + 16);
*(_DWORD *)(a1 + 24) = 1;
v8 = 4;
if ( !v1 )
return;
fwrite("SMTP_with_options\n", 1uLL, 0x12uLL, stderr);
do_random_delay_constprop_0();
if ( write(*(_DWORD *)(v1 + 16), "220 SMTPsink v0.9.0\r\n", 0x15uLL) <= 0 )
goto LABEL_13;
while ( 1 )
{
while ( 1 )
{
if ( random_close() )
{
pthread_mutex_lock(&stat_mutex);
++*(_DWORD *)(*(_QWORD *)(v2 + 32) + 16LL);
pthread_mutex_unlock(&stat_mutex);
goto LABEL_13;
}
memset(buf, 0, 0x100uLL);
v3 = read(*(_DWORD *)(v1 + 16), buf, 0xFFuLL);
do_random_delay_constprop_0();
if ( !v3 )
goto LABEL_13;
pthread_mutex_lock(&stat_mutex);
*(_DWORD *)(*(_QWORD *)(v2 + 32) + 4LL) += v3;
pthread_mutex_unlock(&stat_mutex);
v4 = v8;
if ( v8 != 8 )
break;
if ( !strncasecmp(buf, ".\r\n", 3uLL) || strstr(buf, "\r\n.\r\n") )
goto LABEL_20;
}
if ( !strncasecmp(buf, "quit", 4uLL) )
break;
if ( v4 == 5 )
{
if ( strncasecmp(buf, "mail from:", 0xAuLL) )
goto LABEL_22;
goto LABEL_20;
}
if ( v4 > 5 )
{
if ( v4 > 7 )
{
LABEL_28:
v5 = 16LL;
v6 = "250 looks good\r\n";
goto LABEL_12;
}
if ( strncasecmp(buf, "rcpt to:", 8uLL) )
{
if ( strncasecmp(buf, "data\r\n", 6uLL) )
goto LABEL_22;
v8 = 8;
v5 = 17LL;
v6 = "354 Go ahead...\r\n";
goto LABEL_12;
}
LABEL_20:
v7 = get_random_reply(v2, (int *)&v8);
if ( *v7 )
{
v6 = v7;
v5 = strlen(v7);
goto LABEL_12;
}
}
else
{
if ( v4 != 4 )
goto LABEL_28;
if ( strncasecmp(buf, "helo", 4uLL) )
{
LABEL_22:
pthread_mutex_lock(&stat_mutex);
++*(_DWORD *)(*(_QWORD *)(v2 + 32) + 12LL);
pthread_mutex_unlock(&stat_mutex);
v5 = 21LL;
v6 = "550 permanent error\r\n";
goto LABEL_12;
}
v8 = 5;
v5 = 16LL;
v6 = "250 looks good\r\n";
LABEL_12:
if ( write(*(_DWORD *)(v1 + 16), v6, v5) <= 0 )
goto LABEL_13;
}
}
write(*(_DWORD *)(v1 + 16), "220 see ya\r\n", 0xCuLL);
LABEL_13:
pthread_mutex_lock(&stat_mutex);
++**(_DWORD **)(v2 + 32);
pthread_mutex_unlock(&stat_mutex);
(*(void (__fastcall **)(__int64))(v1 + 56))(v1);
}

canonical_decompiled_code:
void __fastcall func10(__int64 a1)
{
__int64 v1; // r13
__int64 v2; // r12
ssize_t v3; // rbx
unsigned int v4; // ebx
size_t v5; // rdx
const char *v6; // rsi
const char *v7; // rax
unsigned int v8; // [rsp+1Ch] [rbp-12Ch] BYREF
char buf[296]; // [rsp+20h] [rbp-128h] BYREF

v1 = *(_QWORD *)(a1 + 32);
v2 = *(_QWORD *)(a1 + 16);
*(_DWORD *)(a1 + 24) = 1;
v8 = 4;
if ( !v1 )
return;
fwrite("SMTP_with_options\n", 1uLL, 0x12uLL, stderr);
func9();
if ( write(*(_DWORD *)(v1 + 16), "220 SMTPsink v0.9.0\r\n", 0x15uLL) <= 0 )
goto LABEL_13;
while ( 1 )
{
while ( 1 )
{
if ( func7() )
{
pthread_mutex_lock(&stat_mutex);
++*(_DWORD *)(*(_QWORD *)(v2 + 32) + 16LL);
pthread_mutex_unlock(&stat_mutex);
goto LABEL_13;
}
memset(buf, 0, 0x100uLL);
v3 = read(*(_DWORD *)(v1 + 16), buf, 0xFFuLL);
func9();
if ( !v3 )
goto LABEL_13;
pthread_mutex_lock(&stat_mutex);
*(_DWORD *)(*(_QWORD *)(v2 + 32) + 4LL) += v3;
pthread_mutex_unlock(&stat_mutex);
v4 = v8;
if ( v8 != 8 )
break;
if ( !strncasecmp(buf, ".\r\n", 3uLL) || strstr(buf, "\r\n.\r\n") )
goto LABEL_20;
}
if ( !strncasecmp(buf, "quit", 4uLL) )
break;
if ( v4 == 5 )
{
if ( strncasecmp(buf, "mail from:", 0xAuLL) )
goto LABEL_22;
goto LABEL_20;
}
if ( v4 > 5 )
{
if ( v4 > 7 )
{
LABEL_28:
v5 = 16LL;
v6 = "250 looks good\r\n";
goto LABEL_12;
}
if ( strncasecmp(buf, "rcpt to:", 8uLL) )
{
if ( strncasecmp(buf, "data\r\n", 6uLL) )
goto LABEL_22;
v8 = 8;
v5 = 17LL;
v6 = "354 Go ahead...\r\n";
goto LABEL_12;
}
LABEL_20:
v7 = func8(v2, (int *)&v8);
if ( *v7 )
{
v6 = v7;
v5 = strlen(v7);
goto LABEL_12;
}
}
else
{
if ( v4 != 4 )
goto LABEL_28;
if ( strncasecmp(buf, "helo", 4uLL) )
{
LABEL_22:
pthread_mutex_lock(&stat_mutex);
++*(_DWORD *)(*(_QWORD *)(v2 + 32) + 12LL);
pthread_mutex_unlock(&stat_mutex);
v5 = 21LL;
v6 = "550 permanent error\r\n";
goto LABEL_12;
}
v8 = 5;
v5 = 16LL;
v6 = "250 looks good\r\n";
LABEL_12:
if ( write(*(_DWORD *)(v1 + 16), v6, v5) <= 0 )
goto LABEL_13;
}
}
write(*(_DWORD *)(v1 + 16), "220 see ya\r\n", 0xCuLL);
LABEL_13:
pthread_mutex_lock(&stat_mutex);
++**(_DWORD **)(v2 + 32);
pthread_mutex_unlock(&stat_mutex);
(*(void (__fastcall **)(__int64))(v1 + 56))(v1);
}

original_code:
static void SMTP_with_options(handler_t self)
{
 char buf[256];
 size_t n;
 server_t svr = self->svr;
 connection_t con = self->con;
 SMTP_cstate st = ST_HELO;
 char *reply = ((void *)0);
 self->hd_state = HD_ACTIVE;
 if (!con) return;
 fprintf(stderr, "SMTP_with_options\n");
 do_random_delay(st);
 if ( (strlen("220 SMTPsink v0.9.0\r\n") > 0) && (write(con->sk, (void *)"220 SMTPsink v0.9.0\r\n", strlen("220 SMTPsink v0.9.0\r\n")) <= 0) ) goto out;
 for (;;) {
  if (random_close()) {
   pthread_mutex_lock(&stat_mutex); svr->stats[SMTP_dropped] += 1; pthread_mutex_unlock(&stat_mutex);
   goto out;
  }
                memset((void *)buf, 0, 256);
  n = read(con->sk, (void *)buf, 255);
  do_random_delay(st);
  if (n > 0) {
   pthread_mutex_lock(&stat_mutex); svr->stats[SMTP_bytes_processed] += n; pthread_mutex_unlock(&stat_mutex);
   if (st != ST_DOT && !strncasecmp(buf, "quit", 4)) {
    if ( (strlen("220 see ya\r\n") > 0) && (write(con->sk, (void *)"220 see ya\r\n", strlen("220 see ya\r\n")) <= 0) ) goto out;
    break;
   }
   reply = "250 looks good\r\n";
   switch(st) {
   case ST_HELO:
    if (strncasecmp(buf, "helo", strlen("helo")) != 0) {
     pthread_mutex_lock(&stat_mutex); svr->stats[SMTP_5xx] += 1; pthread_mutex_unlock(&stat_mutex);
     reply = "550 permanent error\r\n";
    } else {
     st = ST_MAIL_FROM;
    }
    break;
   case ST_MAIL_FROM:
    if (strncasecmp(buf, "mail from:", strlen("mail from:")) != 0) {
     pthread_mutex_lock(&stat_mutex); svr->stats[SMTP_5xx] += 1; pthread_mutex_unlock(&stat_mutex);
     reply = "550 permanent error\r\n";
    } else {
     reply = get_random_reply(svr, &st);
    }
    break;
   case ST_RCPT_TO:
   case ST_DATA:
    if (!strncasecmp(buf, "rcpt to:", strlen("rcpt to:"))) {
     reply = get_random_reply(svr, &st);
    } else if (!strncasecmp(buf, "data\r\n", strlen("data\r\n"))) {
     st = ST_DOT;
     reply = "354 Go ahead...\r\n";
    } else {
     pthread_mutex_lock(&stat_mutex); svr->stats[SMTP_5xx] += 1; pthread_mutex_unlock(&stat_mutex);
     reply = "550 permanent error\r\n";
    }
    break;
   case ST_DOT:
    if (!strncasecmp(buf, ".\r\n", strlen(".\r\n"))
        || (strstr(buf, "\r\n.\r\n") != ((void *)0) )) {
     reply = get_random_reply(svr, &st);
    } else reply = "";
    break;
   }
   if ( (strlen(reply) > 0) && (write(con->sk, (void *)reply, strlen(reply)) <= 0) ) goto out;
  } else if (n == -1) {
   fprintf(stderr, "SMTP_with_options: closing connection\n");
   goto out;
  } else {
   goto out;
  }
 }
 out:
 pthread_mutex_lock(&stat_mutex); svr->stats[SMTP_connections_processed] += 1; pthread_mutex_unlock(&stat_mutex);
 con->destroy(con);
}

canonical_original_code:
static void SMTP_with_options(struct _handler_t *self)
{
 char buf[256];
 unsigned long n;
 struct _server_t *svr = self->svr;
 struct _connection_t *con = self->con;
 enum SMTP_cstate st = ST_HELO;
 char *reply = ((void *)0);
 self->hd_state = HD_ACTIVE;
 if (!con) return;
 fprintf(stderr, "SMTP_with_options\n");
 do_random_delay(st);
 if ( (strlen("220 SMTPsink v0.9.0\r\n") > 0) && (write(con->sk, (void *)"220 SMTPsink v0.9.0\r\n", strlen("220 SMTPsink v0.9.0\r\n")) <= 0) ) goto out;
 for (;;) {
  if (random_close()) {
   pthread_mutex_lock(&stat_mutex); svr->stats[SMTP_dropped] += 1; pthread_mutex_unlock(&stat_mutex);
   goto out;
  }
                memset((void *)buf, 0, 256);
  n = read(con->sk, (void *)buf, 255);
  do_random_delay(st);
  if (n > 0) {
   pthread_mutex_lock(&stat_mutex); svr->stats[SMTP_bytes_processed] += n; pthread_mutex_unlock(&stat_mutex);
   if (st != ST_DOT && !strncasecmp(buf, "quit", 4)) {
    if ( (strlen("220 see ya\r\n") > 0) && (write(con->sk, (void *)"220 see ya\r\n", strlen("220 see ya\r\n")) <= 0) ) goto out;
    break;
   }
   reply = "250 looks good\r\n";
   switch(st) {
   case ST_HELO:
    if (strncasecmp(buf, "helo", strlen("helo")) != 0) {
     pthread_mutex_lock(&stat_mutex); svr->stats[SMTP_5xx] += 1; pthread_mutex_unlock(&stat_mutex);
     reply = "550 permanent error\r\n";
    } else {
     st = ST_MAIL_FROM;
    }
    break;
   case ST_MAIL_FROM:
    if (strncasecmp(buf, "mail from:", strlen("mail from:")) != 0) {
     pthread_mutex_lock(&stat_mutex); svr->stats[SMTP_5xx] += 1; pthread_mutex_unlock(&stat_mutex);
     reply = "550 permanent error\r\n";
    } else {
     reply = get_random_reply(svr, &st);
    }
    break;
   case ST_RCPT_TO:
   case ST_DATA:
    if (!strncasecmp(buf, "rcpt to:", strlen("rcpt to:"))) {
     reply = get_random_reply(svr, &st);
    } else if (!strncasecmp(buf, "data\r\n", strlen("data\r\n"))) {
     st = ST_DOT;
     reply = "354 Go ahead...\r\n";
    } else {
     pthread_mutex_lock(&stat_mutex); svr->stats[SMTP_5xx] += 1; pthread_mutex_unlock(&stat_mutex);
     reply = "550 permanent error\r\n";
    }
    break;
   case ST_DOT:
    if (!strncasecmp(buf, ".\r\n", strlen(".\r\n"))
        || (strstr(buf, "\r\n.\r\n") != ((void *)0) )) {
     reply = get_random_reply(svr, &st);
    } else reply = "";
    break;
   }
   if ( (strlen(reply) > 0) && (write(con->sk, (void *)reply, strlen(reply)) <= 0) ) goto out;
  } else if (n == -1) {
   fprintf(stderr, "SMTP_with_options: closing connection\n");
   goto out;
  } else {
   goto out;
  }
 }
 out:
 pthread_mutex_lock(&stat_mutex); svr->stats[SMTP_connections_processed] += 1; pthread_mutex_unlock(&stat_mutex);
 con->destroy(con);
}

variable_types: enum SMTP_cstate { ST_HELO=expr, ST_MAIL_FROM=expr, ST_RCPT_TO=expr, ST_DATA=expr, ST_DOT=expr, ST_QUIT=expr }

variable_types:
reply
char *


decompiled_code:
__int64 __fastcall smtp_server(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // rax
void *(*v3)(void *); // rdx
pthread_t newthread[4]; // [rsp+8h] [rbp-20h] BYREF

v1 = new_server(0LL);
*(_QWORD *)v1 = "220 SMTPsink v0.9.0\r\n";
*(_QWORD *)(v1 + 8) = "/tmp/smtpsink.pid";
*(_QWORD *)(v1 + 16) = "/tmp/smtpsink.log";
SMTP_server = v1;
*(_WORD *)(v1 + 40) = 2525;
*(_DWORD *)(v1 + 44) = 15;
*(_QWORD *)(v1 + 48) = 0x3200000023LL;
*(_QWORD *)(v1 + 72) = SMTP_new_handler;
*(_QWORD *)(v1 + 32) = ag_malloc(20LL);
v2 = new_server(0LL);
*(_WORD *)(v2 + 40) = 2552;
*(_QWORD *)v2 = "SMTPinfo v0.9.0\r\n";
v3 = *(void *(**)(void *))(v2 + 96);
*(_DWORD *)(v2 + 44) = 2;
*(_QWORD *)(v2 + 48) = 0x500000005LL;
*(_QWORD *)(v2 + 72) = SMTP_Info_handler;
options = a1;
pthread_create(newthread, 0LL, v3, (void *)v2);
(*(void (__fastcall **)(__int64))(SMTP_server + 96))(SMTP_server);
return 0LL;
}

original_code:
void *smtp_server(void *arg)
{
        pthread_t tid;
 SMTP_server = new_server(((void *)0));
 SMTP_server->name = "220 SMTPsink v0.9.0\r\n";
 SMTP_server->pidfile = "/tmp/smtpsink.pid";
 SMTP_server->logfile = "/tmp/smtpsink.log";
 SMTP_server->port = 2525;
 SMTP_server->backlog = 15;
 SMTP_server->numthreads = 35;
 SMTP_server->maxcon = 50;
 SMTP_server->newhandler = SMTP_new_handler;
 SMTP_server->stats = ag_malloc(sizeof(int) * SMTP_stat_end);
 SMTP_info_server = new_server(((void *)0));
 SMTP_info_server->name = "SMTPinfo v0.9.0\r\n";
 SMTP_info_server->port = 2552;
 SMTP_info_server->backlog = 2;
 SMTP_info_server->numthreads = 5;
 SMTP_info_server->maxcon = 5;
 SMTP_info_server->newhandler = SMTP_Info_handler;
        options = arg;
 (void) pthread_create(&tid, ((void *)0), SMTP_info_server->start, (void *)SMTP_info_server);
 (void) SMTP_server->start((void *)SMTP_server);
 return ((void *)0);
}

canonical_original_code:
void *smtp_server(void *arg)
{
        unsigned long tid;
 SMTP_server = new_server(((void *)0));
 SMTP_server->name = "220 SMTPsink v0.9.0\r\n";
 SMTP_server->pidfile = "/tmp/smtpsink.pid";
 SMTP_server->logfile = "/tmp/smtpsink.log";
 SMTP_server->port = 2525;
 SMTP_server->backlog = 15;
 SMTP_server->numthreads = 35;
 SMTP_server->maxcon = 50;
 SMTP_server->newhandler = SMTP_new_handler;
 SMTP_server->stats = ag_malloc(sizeof(int) * SMTP_stat_end);
 SMTP_info_server = new_server(((void *)0));
 SMTP_info_server->name = "SMTPinfo v0.9.0\r\n";
 SMTP_info_server->port = 2552;
 SMTP_info_server->backlog = 2;
 SMTP_info_server->numthreads = 5;
 SMTP_info_server->maxcon = 5;
 SMTP_info_server->newhandler = SMTP_Info_handler;
        options = arg;
 (void) pthread_create(&tid, ((void *)0), SMTP_info_server->start, (void *)SMTP_info_server);
 (void) SMTP_server->start((void *)SMTP_server);
 return ((void *)0);
}

variable_types:
arg
void *

variable_types:
tid
unsigned long

return_type: void *


decompiled_code:
__int64 __fastcall default_destroy_wq(void *ptr)
{
unsigned int v1; // er12

v1 = (*(__int64 (__fastcall **)(_QWORD))(*((_QWORD *)ptr + 13) + 32LL))(*((_QWORD *)ptr + 13));
free(ptr);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(void *ptr)
{
unsigned int v1; // er12

v1 = (*(__int64 (__fastcall **)(_QWORD))(*((_QWORD *)ptr + 13) + 32LL))(*((_QWORD *)ptr + 13));
free(ptr);
return v1;
}

original_code:
static int default_destroy_wq(void *arg)
{
 int rval = 0;
 wait_queue_t wq = arg;
 rval += wq->cons_queue->destroy(wq->cons_queue);
 free(wq);
 wq = ((void *)0);
 return rval;
}

canonical_original_code:
static int default_destroy_wq(void *arg)
{
 int rval = 0;
 struct _wait_queue_t *wq = arg;
 rval += wq->cons_queue->destroy(wq->cons_queue);
 free(wq);
 wq = ((void *)0);
 return rval;
}

variable_types: rval int

variable_types: struct _wait_queue_t { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } m; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } w; int num; int max; int num_refused; struct _queue_t { struct _queue_connector { void *prev; } *first; void (*push)(struct _queue_t *, void *); void *(*pop)(struct _queue_t *); int (*destroy)(void *); } *cons_queue; int (*add_con)(struct _wait_queue_t *, struct _connection_t { struct _queue_connector _qcon; int sk; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *name; void *clientdata; struct _connection_t *prev; int (*destroy)(void *); } *); void (*wait_on)(struct _wait_queue_t *, struct _handler_t { struct _queue_connector _qcon; struct _server_t { char *name; char *pidfile; char *logfile; int logfd; int *stats; unsigned short port; int backlog; int numthreads; int maxcon; int uid; int listen_sk; struct handler_rec { unsigned long tid; struct _handler_t *hd; } *handlers; struct _handler_t *(*newhandler)(); void (*summary)(struct _server_t *, int); int (*destroy)(void *); void *(*start)(void *); struct _wait_queue_t *wq; } *svr; int hd_state; struct _connection_t *con; void *(*start)(void *); void (*handle_con)(struct _handler_t *); char *(*get_state)(struct _handler_t *); int (*destroy)(void *); } *); int (*destroy)(void *); } *


decompiled_code:
__int64 __fastcall default_add_con(pthread_mutex_t *mutex, __int64 a2)
{
struct __pthread_internal_list *v2; // rbx
__int64 v3; // rdx
__int64 result; // rax

v2 = mutex[2].__list.__prev;
pthread_mutex_lock(mutex);
v3 = (unsigned int)mutex[2].__owner;
if ( (int)v3 >= (signed int)mutex[2].__nusers )
{
fprintf(stderr, "default_add_on: %d connections reached\n", v3);
++mutex[2].__kind;
pthread_mutex_unlock(mutex);
result = 0LL;
}
else
{
((void (__fastcall *)(struct __pthread_internal_list *, __int64))v2[1].__prev)(v2, a2);
++mutex[2].__owner;
pthread_cond_signal((pthread_cond_t *)&mutex[1]);
pthread_mutex_unlock(mutex);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(pthread_mutex_t *mutex, __int64 a2)
{
struct __pthread_internal_list *v2; // rbx
__int64 v3; // rdx
__int64 result; // rax

v2 = mutex[2].__list.__prev;
pthread_mutex_lock(mutex);
v3 = (unsigned int)mutex[2].__owner;
if ( (int)v3 >= (signed int)mutex[2].__nusers )
{
fprintf(stderr, "default_add_on: %d connections reached\n", v3);
++mutex[2].__kind;
pthread_mutex_unlock(mutex);
result = 0LL;
}
else
{
((void (__fastcall *)(struct __pthread_internal_list *, __int64))v2[1].__prev)(v2, a2);
++mutex[2].__owner;
pthread_cond_signal((pthread_cond_t *)&mutex[1]);
pthread_mutex_unlock(mutex);
result = 1LL;
}
return result;
}

original_code:
static int default_add_con(wait_queue_t self, connection_t con)
{
 queue_t q = self->cons_queue;
 pthread_mutex_lock(&self->m);
 if (self->num >= self->max) {
  fprintf(stderr, "default_add_on: %d connections reached\n",
    self->num);
  self->num_refused++;
  pthread_mutex_unlock(&self->m);
  return 0;
 }
 q->push(q, con);
 self->num++;
 pthread_cond_signal(&self->w);
 pthread_mutex_unlock(&self->m);
 return 1;
}

canonical_original_code:
static int default_add_con(struct _wait_queue_t *self, struct _connection_t *con)
{
 struct _queue_t *q = self->cons_queue;
 pthread_mutex_lock(&self->m);
 if (self->num >= self->max) {
  fprintf(stderr, "default_add_on: %d connections reached\n",
    self->num);
  self->num_refused++;
  pthread_mutex_unlock(&self->m);
  return 0;
 }
 q->push(q, con);
 self->num++;
 pthread_cond_signal(&self->w);
 pthread_mutex_unlock(&self->m);
 return 1;
}

variable_types:
con
struct _connection_t *

variable_types: struct _queue_t { struct _queue_connector { void *prev; } *first; void (*push)(struct _queue_t *, void *); void *(*pop)(struct _queue_t *); int (*destroy)(void *); } *


decompiled_code:
void __fastcall __noreturn default_wait_on(pthread_mutex_t *mutex, __int64 a2)
{
struct __pthread_internal_list *v2; // r13
pthread_cond_t *v3; // r14
__int64 v4; // rax
__int64 v5; // rbp
int v6; // eax

v2 = mutex[2].__list.__prev;
v3 = (pthread_cond_t *)&mutex[1];
pthread_mutex_lock(mutex);
while ( 1 )
{
v6 = mutex[2].__owner;
do
{
if ( !v6 )
{
*(_DWORD *)(a2 + 24) = 2;
pthread_cond_wait(v3, mutex);
v6 = mutex[2].__owner;
}
}
while ( v6 <= 0 );
v4 = ((__int64 (__fastcall *)(struct __pthread_internal_list *))v2[1].__next)(v2);
--mutex[2].__owner;
v5 = v4;
pthread_cond_signal(v3);
pthread_mutex_unlock(mutex);
*(_QWORD *)(a2 + 32) = v5;
(*(void (__fastcall **)(__int64))(a2 + 48))(a2);
pthread_mutex_lock(mutex);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func2(pthread_mutex_t *mutex, __int64 a2)
{
struct __pthread_internal_list *v2; // r13
pthread_cond_t *v3; // r14
__int64 v4; // rax
__int64 v5; // rbp
int v6; // eax

v2 = mutex[2].__list.__prev;
v3 = (pthread_cond_t *)&mutex[1];
pthread_mutex_lock(mutex);
while ( 1 )
{
v6 = mutex[2].__owner;
do
{
if ( !v6 )
{
*(_DWORD *)(a2 + 24) = 2;
pthread_cond_wait(v3, mutex);
v6 = mutex[2].__owner;
}
}
while ( v6 <= 0 );
v4 = ((__int64 (__fastcall *)(struct __pthread_internal_list *))v2[1].__next)(v2);
--mutex[2].__owner;
v5 = v4;
pthread_cond_signal(v3);
pthread_mutex_unlock(mutex);
*(_QWORD *)(a2 + 32) = v5;
(*(void (__fastcall **)(__int64))(a2 + 48))(a2);
pthread_mutex_lock(mutex);
}
}

original_code:
static void default_wait_on(wait_queue_t self, handler_t hd)
{
 connection_t con;
 queue_t q = self->cons_queue;
 pthread_mutex_lock(&self->m);
 for (;;) {
  if (self->num == 0) {
   hd->hd_state = HD_WAITING;
   pthread_cond_wait(&self->w, &self->m);
  }
  if (self->num > 0) {
    con = q->pop(q);
    self->num--;
    pthread_cond_signal(&self->w);
    pthread_mutex_unlock(&self->m);
    hd->con = con;
    hd->handle_con(hd);
    pthread_mutex_lock(&self->m);
  }
 }
}

canonical_original_code:
static void default_wait_on(struct _wait_queue_t *self, struct _handler_t *hd)
{
 struct _connection_t *con;
 struct _queue_t *q = self->cons_queue;
 pthread_mutex_lock(&self->m);
 for (;;) {
  if (self->num == 0) {
   hd->hd_state = HD_WAITING;
   pthread_cond_wait(&self->w, &self->m);
  }
  if (self->num > 0) {
    con = q->pop(q);
    self->num--;
    pthread_cond_signal(&self->w);
    pthread_mutex_unlock(&self->m);
    hd->con = con;
    hd->handle_con(hd);
    pthread_mutex_lock(&self->m);
  }
 }
}

variable_types:
hd
struct _handler_t *


decompiled_code:
__int64 new_wait_queue()
{
__int64 v0; // r12
__int64 v1; // rax

v0 = ag_malloc(136LL);
v1 = new_queue();
*(_QWORD *)(v0 + 120) = default_wait_on;
*(_QWORD *)(v0 + 104) = v1;
*(_QWORD *)(v0 + 112) = default_add_con;
*(_QWORD *)(v0 + 128) = default_destroy_wq;
pthread_cond_init((pthread_cond_t *)(v0 + 40), 0LL);
pthread_mutex_init((pthread_mutex_t *)v0, 0LL);
return v0;
}

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

v0 = ag_malloc(136LL);
v1 = new_queue();
*(_QWORD *)(v0 + 120) = default_wait_on;
*(_QWORD *)(v0 + 104) = v1;
*(_QWORD *)(v0 + 112) = default_add_con;
*(_QWORD *)(v0 + 128) = default_destroy_wq;
pthread_cond_init((pthread_cond_t *)(v0 + 40), 0LL);
pthread_mutex_init((pthread_mutex_t *)v0, 0LL);
return v0;
}

original_code:
wait_queue_t new_wait_queue()
{
 wait_queue_t wq;
 wq = ag_malloc(sizeof(struct _wait_queue_t));
 wq->cons_queue = new_queue();
 wq->wait_on = default_wait_on;
 wq->add_con = default_add_con;
 wq->destroy = default_destroy_wq;
 pthread_cond_init(&wq->w, ((void *)0));
 pthread_mutex_init(&wq->m, ((void *)0));
 return wq;
}

canonical_original_code:
struct _wait_queue_t *new_wait_queue()
{
 struct _wait_queue_t *wq;
 wq = ag_malloc(sizeof(struct _wait_queue_t));
 wq->cons_queue = new_queue();
 wq->wait_on = default_wait_on;
 wq->add_con = default_add_con;
 wq->destroy = default_destroy_wq;
 pthread_cond_init(&wq->w, ((void *)0));
 pthread_mutex_init(&wq->m, ((void *)0));
 return wq;
}

return_type: struct _wait_queue_t { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } m; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } w; int num; int max; int num_refused; struct _queue_t { struct _queue_connector { void *prev; } *first; void (*push)(struct _queue_t *, void *); void *(*pop)(struct _queue_t *); int (*destroy)(void *); } *cons_queue; int (*add_con)(struct _wait_queue_t *, struct _connection_t { struct _queue_connector _qcon; int sk; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *name; void *clientdata; struct _connection_t *prev; int (*destroy)(void *); } *); void (*wait_on)(struct _wait_queue_t *, struct _handler_t { struct _queue_connector _qcon; struct _server_t { char *name; char *pidfile; char *logfile; int logfd; int *stats; unsigned short port; int backlog; int numthreads; int maxcon; int uid; int listen_sk; struct handler_rec { unsigned long tid; struct _handler_t *hd; } *handlers; struct _handler_t *(*newhandler)(); void (*summary)(struct _server_t *, int); int (*destroy)(void *); void *(*start)(void *); struct _wait_queue_t *wq; } *svr; int hd_state; struct _connection_t *con; void *(*start)(void *); void (*handle_con)(struct _handler_t *); char *(*get_state)(struct _handler_t *); int (*destroy)(void *); } *); int (*destroy)(void *); } *


decompiled_code:
void *__fastcall ag_malloc(size_t size)
{
void *result; // rax

result = calloc(1uLL, size);
if ( !result )
{
fprintf(stderr, "malloc [%d] failed\n", size);
abort();
}
return result;
}

canonical_decompiled_code:
void *__fastcall func0(size_t size)
{
void *result; // rax

result = calloc(1uLL, size);
if ( !result )
{
fprintf(stderr, "malloc [%d] failed\n", size);
abort();
}
return result;
}

original_code:
void *ag_malloc(size_t sz)
{
 void *r;
 r = calloc(1,sz);
 if (!r) {
  fprintf(stderr, "malloc [%d] failed\n", sz);
  abort();
 }
 return r;
}

canonical_original_code:
void *ag_malloc(unsigned long sz)
{
 void *r;
 r = calloc(1,sz);
 if (!r) {
  fprintf(stderr, "malloc [%d] failed\n", sz);
  abort();
 }
 return r;
}

variable_types:
sz
unsigned long

variable_types:
r
void *


decompiled_code:
__int64 __fastcall accept_connection(int fd)
{
struct sockaddr *v1; // rbx
int v2; // ebp
__int64 result; // rax
socklen_t addr_len[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = (struct sockaddr *)ag_malloc(16LL);
v2 = accept(fd, v1, addr_len);
if ( v2 <= 0 )
fwrite("accept_connection: error\n", 1uLL, 0x19uLL, stderr);
if ( addr_len[0] > 0x10 )
{
fwrite("accept_connection: memory corruption.\n", 1uLL, 0x26uLL, stderr);
exit(1);
}
result = new_connection();
*(_DWORD *)(result + 16) = v2;
*(_QWORD *)(result + 24) = v1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd)
{
struct sockaddr *v1; // rbx
int v2; // ebp
__int64 result; // rax
socklen_t addr_len[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v1 = (struct sockaddr *)ag_malloc(16LL);
v2 = accept(fd, v1, addr_len);
if ( v2 <= 0 )
fwrite("accept_connection: error\n", 1uLL, 0x19uLL, stderr);
if ( addr_len[0] > 0x10 )
{
fwrite("accept_connection: memory corruption.\n", 1uLL, 0x26uLL, stderr);
exit(1);
}
result = new_connection();
*(_DWORD *)(result + 16) = v2;
*(_QWORD *)(result + 24) = v1;
return result;
}

original_code:
static connection_t accept_connection(int lsock)
{
 int sock;
 struct sockaddr_in *name;
 int len;
 connection_t con;
 name = ag_malloc(sizeof(struct sockaddr_in));
 if ((sock = accept(lsock, name, &len)) <= 0) {
  fprintf(stderr, "accept_connection: error\n");
 }
 if (len > sizeof(struct sockaddr_in)) {
  fprintf(stderr, "accept_connection: memory corruption.\n");
  exit(1);
 }
 con = new_connection();
 con->sk = sock;
 con->name = name;
 return con;
}

canonical_original_code:
static struct _connection_t *accept_connection(int lsock)
{
 int sock;
 struct sockaddr_in *name;
 int len;
 struct _connection_t *con;
 name = ag_malloc(sizeof(struct sockaddr_in));
 if ((sock = accept(lsock, name, &len)) <= 0) {
  fprintf(stderr, "accept_connection: error\n");
 }
 if (len > sizeof(struct sockaddr_in)) {
  fprintf(stderr, "accept_connection: memory corruption.\n");
  exit(1);
 }
 con = new_connection();
 con->sk = sock;
 con->name = name;
 return con;
}

variable_types: lsock int

variable_types: sock int

variable_types: len int

return_type: struct _connection_t { struct _queue_connector { void *prev; } _qcon; int sk; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *name; void *clientdata; struct _connection_t *prev; int (*destroy)(void *); } *


decompiled_code:
ssize_t __fastcall default_summary(__int64 a1, int a2)
{
__int64 v2; // r9
__int64 v3; // r8
size_t v4; // rax
ssize_t result; // rax
__int64 v6; // rbx
__int64 v7; // r15
__int64 v8; // rdx
const char *v9; // rax
__int64 v10; // rdx
size_t v11; // rax
_BYTE savedregs[568]; // [rsp+0h] [rbp+0h] BYREF

v2 = *(unsigned __int16 *)(a1 + 40);
v3 = *(_QWORD *)(a1 + 16);
memset(savedregs, 0, 0x200uLL);
sprintf(
savedregs,
"%s pidf=%s logf=%s port=%d numth=%d maxcon=%d uid=%d numcon=%d num_ref=%d\n",
*(_QWORD *)a1,
*(_QWORD *)(a1 + 8),
v3,
v2,
*(unsigned int *)(a1 + 48),
*(unsigned int *)(a1 + 52),
*(unsigned int *)(a1 + 56),
*(unsigned int *)(*(_QWORD *)(a1 + 104) + 88LL),
*(unsigned int *)(*(_QWORD *)(a1 + 104) + 96LL));
v4 = strlen(savedregs);
write(a2, savedregs, v4);
result = *(unsigned int *)(a1 + 48);
if ( (int)result > 0 )
{
v6 = 0LL;
do
{
v7 = 16 * v6;
v8 = *(_QWORD *)(*(_QWORD *)(a1 + 64) + 16 * v6 + 8);
memset(savedregs, 0, 0x200uLL);
v9 = (const char *)(*(__int64 (__fastcall **)(__int64))(v8 + 56))(v8);
v10 = (unsigned int)v6++;
sprintf(savedregs, "handler %d: tid=0x%x state=%s\n", v10, *(_QWORD *)(*(_QWORD *)(a1 + 64) + v7), v9);
v11 = strlen(savedregs);
result = write(a2, savedregs, v11);
}
while ( *(_DWORD *)(a1 + 48) > (int)v6 );
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func2(__int64 a1, int a2)
{
__int64 v2; // r9
__int64 v3; // r8
size_t v4; // rax
ssize_t result; // rax
__int64 v6; // rbx
__int64 v7; // r15
__int64 v8; // rdx
const char *v9; // rax
__int64 v10; // rdx
size_t v11; // rax
_BYTE savedregs[568]; // [rsp+0h] [rbp+0h] BYREF

v2 = *(unsigned __int16 *)(a1 + 40);
v3 = *(_QWORD *)(a1 + 16);
memset(savedregs, 0, 0x200uLL);
sprintf(
savedregs,
"%s pidf=%s logf=%s port=%d numth=%d maxcon=%d uid=%d numcon=%d num_ref=%d\n",
*(_QWORD *)a1,
*(_QWORD *)(a1 + 8),
v3,
v2,
*(unsigned int *)(a1 + 48),
*(unsigned int *)(a1 + 52),
*(unsigned int *)(a1 + 56),
*(unsigned int *)(*(_QWORD *)(a1 + 104) + 88LL),
*(unsigned int *)(*(_QWORD *)(a1 + 104) + 96LL));
v4 = strlen(savedregs);
write(a2, savedregs, v4);
result = *(unsigned int *)(a1 + 48);
if ( (int)result > 0 )
{
v6 = 0LL;
do
{
v7 = 16 * v6;
v8 = *(_QWORD *)(*(_QWORD *)(a1 + 64) + 16 * v6 + 8);
memset(savedregs, 0, 0x200uLL);
v9 = (const char *)(*(__int64 (__fastcall **)(__int64))(v8 + 56))(v8);
v10 = (unsigned int)v6++;
sprintf(savedregs, "handler %d: tid=0x%x state=%s\n", v10, *(_QWORD *)(*(_QWORD *)(a1 + 64) + v7), v9);
v11 = strlen(savedregs);
result = write(a2, savedregs, v11);
}
while ( *(_DWORD *)(a1 + 48) > (int)v6 );
}
return result;
}

original_code:
static void default_summary(server_t self, int fd)
{
 char buf[512];
 handler_t hd;
 int i;
 memset((void *)buf, 0, 512);
 sprintf(buf, "%s pidf=%s logf=%s port=%d numth=%d maxcon=%d uid=%d numcon=%d num_ref=%d\n",
  self->name, self->pidfile, self->logfile, self->port, self->numthreads,
  self->maxcon, self->uid, self->wq->num, self->wq->num_refused);
 (void)write(fd, buf, strlen(buf));
 for (i = 0; i < self->numthreads; i++) {
  hd = self->handlers[i].hd;
  memset((void *)buf, 0, 512);
  sprintf(buf, "handler %d: tid=0x%x state=%s\n",
   i, (int)self->handlers[i].tid, hd->get_state(hd));
  (void)write(fd, buf, strlen(buf));
 }
}

canonical_original_code:
static void default_summary(struct _server_t *self, int fd)
{
 char buf[512];
 struct _handler_t *hd;
 int i;
 memset((void *)buf, 0, 512);
 sprintf(buf, "%s pidf=%s logf=%s port=%d numth=%d maxcon=%d uid=%d numcon=%d num_ref=%d\n",
  self->name, self->pidfile, self->logfile, self->port, self->numthreads,
  self->maxcon, self->uid, self->wq->num, self->wq->num_refused);
 (void)write(fd, buf, strlen(buf));
 for (i = 0; i < self->numthreads; i++) {
  hd = self->handlers[i].hd;
  memset((void *)buf, 0, 512);
  sprintf(buf, "handler %d: tid=0x%x state=%s\n",
   i, (int)self->handlers[i].tid, hd->get_state(hd));
  (void)write(fd, buf, strlen(buf));
 }
}

variable_types: struct _server_t { char *name; char *pidfile; char *logfile; int logfd; int *stats; unsigned short port; int backlog; int numthreads; int maxcon; int uid; int listen_sk; struct handler_rec { unsigned long tid; struct _handler_t { struct _queue_connector { void *prev; } _qcon; struct _server_t *svr; int hd_state; struct _connection_t { struct _queue_connector _qcon; int sk; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *name; void *clientdata; struct _connection_t *prev; int (*destroy)(void *); } *con; void *(*start)(void *); void (*handle_con)(struct _handler_t *); char *(*get_state)(struct _handler_t *); int (*destroy)(void *); } *hd; } *handlers; struct _handler_t *(*newhandler)(); void (*summary)(struct _server_t *, int); int (*destroy)(void *); void *(*start)(void *); struct _wait_queue_t { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } m; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } w; int num; int max; int num_refused; struct _queue_t { struct _queue_connector *first; void (*push)(struct _queue_t *, void *); void *(*pop)(struct _queue_t *); int (*destroy)(void *); } *cons_queue; int (*add_con)(struct _wait_queue_t *, struct _connection_t *); void (*wait_on)(struct _wait_queue_t *, struct _handler_t *); int (*destroy)(void *); } *wq; } *

variable_types: fd int

variable_types:
buf
char [512]

variable_types: struct _handler_t { struct _queue_connector { void *prev; } _qcon; struct _server_t { char *name; char *pidfile; char *logfile; int logfd; int *stats; unsigned short port; int backlog; int numthreads; int maxcon; int uid; int listen_sk; struct handler_rec { unsigned long tid; struct _handler_t *hd; } *handlers; struct _handler_t *(*newhandler)(); void (*summary)(struct _server_t *, int); int (*destroy)(void *); void *(*start)(void *); struct _wait_queue_t { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } m; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } w; int num; int max; int num_refused; struct _queue_t { struct _queue_connector *first; void (*push)(struct _queue_t *, void *); void *(*pop)(struct _queue_t *); int (*destroy)(void *); } *cons_queue; int (*add_con)(struct _wait_queue_t *, struct _connection_t { struct _queue_connector _qcon; int sk; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *name; void *clientdata; struct _connection_t *prev; int (*destroy)(void *); } *); void (*wait_on)(struct _wait_queue_t *, struct _handler_t *); int (*destroy)(void *); } *wq; } *svr; int hd_state; struct _connection_t *con; void *(*start)(void *); void (*handle_con)(struct _handler_t *); char *(*get_state)(struct _handler_t *); int (*destroy)(void *); } *

variable_types: i int


original_code:
static int default_destroy_svr(void *arg)
{
 server_t self = arg;
 int i;
 if (self->logfd > 0) (void) close(self->logfd);
 if (self->stats) free(self->stats);
 self->wq->destroy(self->wq);
 for (i = 0; i < self->numthreads; i++) {
  if (self->handlers[i].hd->con)
   self->handlers[i].hd->con->destroy(self->handlers[i].hd->con);
  (void) pthread_cancel(self->handlers[i].tid);
 }
 (void) close(self->listen_sk);
 return 1;
}

canonical_original_code:
static int default_destroy_svr(void *arg)
{
 struct _server_t *self = arg;
 int i;
 if (self->logfd > 0) (void) close(self->logfd);
 if (self->stats) free(self->stats);
 self->wq->destroy(self->wq);
 for (i = 0; i < self->numthreads; i++) {
  if (self->handlers[i].hd->con)
   self->handlers[i].hd->con->destroy(self->handlers[i].hd->con);
  (void) pthread_cancel(self->handlers[i].tid);
 }
 (void) close(self->listen_sk);
 return 1;
}

variable_types:
arg
void *


decompiled_code:
__int64 __fastcall setup_connection(__int64 a1)
{
int v2; // eax
__int16 v3; // di
unsigned int v4; // er12
socklen_t v5; // edx
struct sockaddr v7[2]; // [rsp+0h] [rbp-28h] BYREF

v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
perror("socket");
exit(1);
}
v3 = *(_WORD *)(a1 + 40);
v4 = v2;
v7[0].sa_family = 2;
*(_WORD *)v7[0].sa_data = _bswap_16(v3);
*(_DWORD *)&v7[0].sa_data[2] = _bswap_32_constprop_0(v4, v7);
if ( bind(v4, v7, v5) < 0 )
{
perror("bind");
exit(1);
}
if ( listen(v4, *(_DWORD *)(a1 + 44)) == -1 )
{
perror("listen");
exit(1);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
int v2; // eax
__int16 v3; // di
unsigned int v4; // er12
socklen_t v5; // edx
struct sockaddr v7[2]; // [rsp+0h] [rbp-28h] BYREF

v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
perror("socket");
exit(1);
}
v3 = *(_WORD *)(a1 + 40);
v4 = v2;
v7[0].sa_family = 2;
*(_WORD *)v7[0].sa_data = func0(v3);
*(_DWORD *)&v7[0].sa_data[2] = func4(v4, v7);
if ( bind(v4, v7, v5) < 0 )
{
perror("bind");
exit(1);
}
if ( listen(v4, *(_DWORD *)(a1 + 44)) == -1 )
{
perror("listen");
exit(1);
}
return v4;
}

original_code:
static int setup_connection(server_t svr)
{
 int sock;
 struct sockaddr_in name;
 sock = socket(2, SOCK_STREAM , 0);
 if (sock < 0) {
  perror("socket");
  exit(1);
 }
 name.sin_family = 2;
 name.sin_port = htons(svr->port);
 name.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
 if (bind(sock, (struct sockaddr *) &name, sizeof(name)) < 0)
 {
  perror("bind");
  exit(1);
 }
 if (listen(sock, svr->backlog) == -1) {
  perror("listen");
  exit(1);
 }
 return sock;
}

canonical_original_code:
static int setup_connection(struct _server_t *svr)
{
 int sock;
 struct sockaddr_in name;
 sock = socket(2, SOCK_STREAM , 0);
 if (sock < 0) {
  perror("socket");
  exit(1);
 }
 name.sin_family = 2;
 name.sin_port = htons(svr->port);
 name.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
 if (bind(sock, (struct sockaddr *) &name, sizeof(name)) < 0)
 {
  perror("bind");
  exit(1);
 }
 if (listen(sock, svr->backlog) == -1) {
  perror("listen");
  exit(1);
 }
 return sock;
}

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

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


original_code:
static void *default_start_server(void *arg)
{
 server_t self = arg;
 connection_t con;
 int i;
 if (!self) return ((void *)0);
 become_daemon(self);
 self->wq->max = self->maxcon;
 self->handlers = ag_malloc(sizeof(handler_rec) * self->numthreads);
 for (i = 0; i < self->numthreads; i++) {
  handler_t hd;
  hd = self->newhandler();
  hd->svr = self;
  self->handlers[i].hd = hd;
  (void) pthread_create(&self->handlers[i].tid,
          ((void *)0),
          hd->start,
          (void *)hd
   );
 }
 self->listen_sk = setup_connection(self);
 if (self->uid) become_user(self);
 for (;;) {
  con = accept_connection(self->listen_sk);
  if (!self->wq->add_con(self->wq, con)) con->destroy(con);
 }
}

canonical_original_code:
static void *default_start_server(void *arg)
{
 struct _server_t *self = arg;
 struct _connection_t *con;
 int i;
 if (!self) return ((void *)0);
 become_daemon(self);
 self->wq->max = self->maxcon;
 self->handlers = ag_malloc(sizeof(handler_rec) * self->numthreads);
 for (i = 0; i < self->numthreads; i++) {
  struct _handler_t *hd;
  hd = self->newhandler();
  hd->svr = self;
  self->handlers[i].hd = hd;
  (void) pthread_create(&self->handlers[i].tid,
          ((void *)0),
          hd->start,
          (void *)hd
   );
 }
 self->listen_sk = setup_connection(self);
 if (self->uid) become_user(self);
 for (;;) {
  con = accept_connection(self->listen_sk);
  if (!self->wq->add_con(self->wq, con)) con->destroy(con);
 }
}

variable_types: struct _connection_t { struct _queue_connector { void *prev; } _qcon; int sk; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *name; void *clientdata; struct _connection_t *prev; int (*destroy)(void *); } *


decompiled_code:
__int64 __fastcall default_destroy_q(void *a1)
{
free(a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *a1)
{
free(a1);
return 0LL;
}

original_code:
static int default_destroy_q(void *arg)
{
 queue_t self = arg;
 free(self);
 self = ((void *)0);
 return 0;
}

canonical_original_code:
static int default_destroy_q(void *arg)
{
 struct _queue_t *self = arg;
 free(self);
 self = ((void *)0);
 return 0;
}


decompiled_code:
_QWORD *new_queue()
{
_QWORD *result; // rax

result = (_QWORD *)ag_malloc(40LL);
result[2] = default_push;
result[3] = default_pop;
result[4] = default_destroy_q;
return result;
}

canonical_decompiled_code:
_QWORD *func2()
{
_QWORD *result; // rax

result = (_QWORD *)ag_malloc(40LL);
result[2] = default_push;
result[3] = default_pop;
result[4] = default_destroy_q;
return result;
}

original_code:
queue_t new_queue()
{
 queue_t q;
 q = ag_malloc(sizeof(struct _queue_t));
 q->push = default_push;
 q->pop = default_pop;
 q->destroy = default_destroy_q;
 return q;
}

canonical_original_code:
struct _queue_t *new_queue()
{
 struct _queue_t *q;
 q = ag_malloc(sizeof(struct _queue_t));
 q->push = default_push;
 q->pop = default_pop;
 q->destroy = default_destroy_q;
 return q;
}

return_type: struct _queue_t { struct _queue_connector { void *prev; } *first; void (*push)(struct _queue_t *, void *); void *(*pop)(struct _queue_t *); int (*destroy)(void *); } *


decompiled_code:
char *__fastcall default_get_state(__int64 a1)
{
return (&handler_states)[*(int *)(a1 + 24)];
}

canonical_decompiled_code:
char *__fastcall func0(__int64 a1)
{
return (&handler_states)[*(int *)(a1 + 24)];
}

original_code:
static char *default_get_state(handler_t self)
{
 return handler_states[self->hd_state];
}

canonical_original_code:
static char *default_get_state(struct _handler_t *self)
{
 return handler_states[self->hd_state];
}


original_code:
static void *default_start(void *arg)
{
 handler_t self = arg;
 wait_queue_t wq = self->svr->wq;
 if (!self) pthread_exit(((void *)0));
 self->hd_state = HD_STARTING;
 wq->wait_on(wq, self);
 return ((void *)0);
}

canonical_original_code:
static void *default_start(void *arg)
{
 struct _handler_t *self = arg;
 struct _wait_queue_t *wq = self->svr->wq;
 if (!self) pthread_exit(((void *)0));
 self->hd_state = HD_STARTING;
 wq->wait_on(wq, self);
 return ((void *)0);
}

variable_types: struct _wait_queue_t { union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } m; union pthread_cond_t { struct __pthread_cond_s { union { unsigned long long __wseq; struct { unsigned int __low; unsigned int __high; } __wseq32; }; union { unsigned long long __g1_start; struct { unsigned int __low; unsigned int __high; } __g1_start32; }; unsigned int __g_refs[2]; unsigned int __g_size[2]; unsigned int __g1_orig_size; unsigned int __wrefs; unsigned int __g_signals[2]; } __data; char __size[48]; long long __align; } w; int num; int max; int num_refused; struct _queue_t { struct _queue_connector { void *prev; } *first; void (*push)(struct _queue_t *, void *); void *(*pop)(struct _queue_t *); int (*destroy)(void *); } *cons_queue; int (*add_con)(struct _wait_queue_t *, struct _connection_t { struct _queue_connector _qcon; int sk; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } *name; void *clientdata; struct _connection_t *prev; int (*destroy)(void *); } *); void (*wait_on)(struct _wait_queue_t *, struct _handler_t { struct _queue_connector _qcon; struct _server_t { char *name; char *pidfile; char *logfile; int logfd; int *stats; unsigned short port; int backlog; int numthreads; int maxcon; int uid; int listen_sk; struct handler_rec { unsigned long tid; struct _handler_t *hd; } *handlers; struct _handler_t *(*newhandler)(); void (*summary)(struct _server_t *, int); int (*destroy)(void *); void *(*start)(void *); struct _wait_queue_t *wq; } *svr; int hd_state; struct _connection_t *con; void *(*start)(void *); void (*handle_con)(struct _handler_t *); char *(*get_state)(struct _handler_t *); int (*destroy)(void *); } *); int (*destroy)(void *); } *


decompiled_code:
__int64 __fastcall default_destroy_hd(void *ptr)
{
__int64 v1; // rax

if ( !ptr )
return 1LL;
v1 = *((_QWORD *)ptr + 4);
*((_DWORD *)ptr + 6) = 3;
(*(void (__fastcall **)(__int64))(v1 + 56))(v1);
free(ptr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(void *ptr)
{
__int64 v1; // rax

if ( !ptr )
return 1LL;
v1 = *((_QWORD *)ptr + 4);
*((_DWORD *)ptr + 6) = 3;
(*(void (__fastcall **)(__int64))(v1 + 56))(v1);
free(ptr);
return 0LL;
}

original_code:
static int default_destroy_hd(void *arg)
{
 handler_t hd = arg;
 if (!hd) return 1;
 hd->hd_state = HD_STOPPING;
 hd->con->destroy(hd->con);
 free(hd);
 hd = ((void *)0);
 return 0;
}

canonical_original_code:
static int default_destroy_hd(void *arg)
{
 struct _handler_t *hd = arg;
 if (!hd) return 1;
 hd->hd_state = HD_STOPPING;
 hd->con->destroy(hd->con);
 free(hd);
 hd = ((void *)0);
 return 0;
}


original_code:
static void default_handle_con(handler_t self)
{
 server_t svr = self->svr;
 connection_t con = self->con;
 if (!con) return;
 self->hd_state = HD_ACTIVE;
 if (svr->name) {
   write(con->sk, svr->name, strlen(svr->name));
   write(con->sk, "\r\n", 2);
 }
 con->destroy(con);
}

canonical_original_code:
static void default_handle_con(struct _handler_t *self)
{
 struct _server_t *svr = self->svr;
 struct _connection_t *con = self->con;
 if (!con) return;
 self->hd_state = HD_ACTIVE;
 if (svr->name) {
   write(con->sk, svr->name, strlen(svr->name));
   write(con->sk, "\r\n", 2);
 }
 con->destroy(con);
}


decompiled_code:
_QWORD *new_handler()
{
_QWORD *result; // rax

result = (_QWORD *)ag_malloc(72LL);
result[5] = default_start;
result[6] = default_handle_con;
result[7] = default_get_state;
result[8] = default_destroy_hd;
return result;
}

canonical_decompiled_code:
_QWORD *func4()
{
_QWORD *result; // rax

result = (_QWORD *)ag_malloc(72LL);
result[5] = default_start;
result[6] = default_handle_con;
result[7] = default_get_state;
result[8] = default_destroy_hd;
return result;
}

original_code:
handler_t new_handler()
{
 handler_t hd;
 hd = ag_malloc(sizeof(struct _handler_t));
 hd->start = default_start;
 hd->handle_con = default_handle_con;
 hd->get_state = default_get_state;
 hd->destroy = default_destroy_hd;
 return hd;
}

canonical_original_code:
struct _handler_t *new_handler()
{
 struct _handler_t *hd;
 hd = ag_malloc(sizeof(struct _handler_t));
 hd->start = default_start;
 hd->handle_con = default_handle_con;
 hd->get_state = default_get_state;
 hd->destroy = default_destroy_hd;
 return hd;
}


decompiled_code:
__int64 __fastcall destroy_connection(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
int v4; // edi

if ( !ptr )
return 1LL;
v2 = (void *)*((_QWORD *)ptr + 4);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 3);
if ( v3 )
free(v3);
v4 = *((_DWORD *)ptr + 4);
if ( v4 > 0 )
close(v4);
free(ptr);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(void *ptr)
{
void *v2; // rdi
void *v3; // rdi
int v4; // edi

if ( !ptr )
return 1LL;
v2 = (void *)*((_QWORD *)ptr + 4);
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 3);
if ( v3 )
free(v3);
v4 = *((_DWORD *)ptr + 4);
if ( v4 > 0 )
close(v4);
free(ptr);
return 0LL;
}

original_code:
static int destroy_connection(void *arg)
{
 connection_t con = arg;
 if (!con) return 1;
 if (con->clientdata) free(con->clientdata);
 if (con->name) free(con->name);
 if (con->sk > 0) close(con->sk);
 free(con);
 return 0;
}

canonical_original_code:
static int destroy_connection(void *arg)
{
 struct _connection_t *con = arg;
 if (!con) return 1;
 if (con->clientdata) free(con->clientdata);
 if (con->name) free(con->name);
 if (con->sk > 0) close(con->sk);
 free(con);
 return 0;
}


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

result = ag_malloc(64LL);
*(_QWORD *)(result + 56) = destroy_connection;
return result;
}

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

result = ag_malloc(64LL);
*(_QWORD *)(result + 56) = destroy_connection;
return result;
}

original_code:
connection_t new_connection()
{
 connection_t con;
 con = ag_malloc(sizeof(struct _connection_t));
 con->destroy = destroy_connection;
 return con;
}

canonical_original_code:
struct _connection_t *new_connection()
{
 struct _connection_t *con;
 con = ag_malloc(sizeof(struct _connection_t));
 con->destroy = destroy_connection;
 return con;
}


decompiled_code:
__int64 __fastcall d_default_handler(__int64 a1, __int64 a2)
{
__int64 v2; // rbp
_BYTE *v3; // r12
size_t v5; // rax

v2 = *(_QWORD *)(a1 + 32);
if ( write(*(_DWORD *)(v2 + 16), "content-type: text/html\r\n\r\n", 0x1BuLL) <= 0 )
return 1LL;
v3 = *(_BYTE **)(a2 + 120);
if ( !*v3 )
return 1LL;
v5 = strlen(*(const char **)(a2 + 120));
write(*(_DWORD *)(v2 + 16), v3, v5);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int64 v2; // rbp
_BYTE *v3; // r12
size_t v5; // rax

v2 = *(_QWORD *)(a1 + 32);
if ( write(*(_DWORD *)(v2 + 16), "content-type: text/html\r\n\r\n", 0x1BuLL) <= 0 )
return 1LL;
v3 = *(_BYTE **)(a2 + 120);
if ( !*v3 )
return 1LL;
v5 = strlen(*(const char **)(a2 + 120));
write(*(_DWORD *)(v2 + 16), v3, v5);
return 1LL;
}

original_code:
static int d_default_handler(handler_t hd, request_t rq)
{
 connection_t con = hd->con;
 if ( (strlen("content-type: text/html\r\n\r\n") > 0) && (write(con->sk, (void *)"content-type: text/html\r\n\r\n", strlen("content-type: text/html\r\n\r\n")) <= 0) ) goto out;
 if ( (strlen(rq->raw) > 0) && (write(con->sk, (void *)rq->raw, strlen(rq->raw)) <= 0) ) goto out;
 out:
 return 1;
}

canonical_original_code:
static int d_default_handler(struct _handler_t *hd, struct _http_request *rq)
{
 struct _connection_t *con = hd->con;
 if ( (strlen("content-type: text/html\r\n\r\n") > 0) && (write(con->sk, (void *)"content-type: text/html\r\n\r\n", strlen("content-type: text/html\r\n\r\n")) <= 0) ) goto out;
 if ( (strlen(rq->raw) > 0) && (write(con->sk, (void *)rq->raw, strlen(rq->raw)) <= 0) ) goto out;
 out:
 return 1;
}

variable_types: struct _http_request { enum RQ_TYPE { HEAD=0, GET=1, POST=2, PUT=3, RQ_UNKNOWN=4 } rtype; char *headers[-1]; char *prot; char *path; char *body; char *content_type; char *raw; int (*destroy)(void *); } *

user_defined_types: struct _http_request { enum RQ_TYPE rtype; char *headers[-1]; char *prot; char *path; char *body; char *content_type; char *raw; int (*destroy)(void *); }


decompiled_code:
__int64 __fastcall d_post_handler(__int64 a1, __int64 a2)
{
__int64 v2; // rbp
_BYTE *v3; // r12
size_t v5; // rax

v2 = *(_QWORD *)(a1 + 32);
if ( write(*(_DWORD *)(v2 + 16), "content-type: text/html\r\n\r\n", 0x1BuLL) <= 0 )
return 1LL;
v3 = *(_BYTE **)(a2 + 104);
if ( !*v3 )
return 1LL;
v5 = strlen(*(const char **)(a2 + 104));
write(*(_DWORD *)(v2 + 16), v3, v5);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
__int64 v2; // rbp
_BYTE *v3; // r12
size_t v5; // rax

v2 = *(_QWORD *)(a1 + 32);
if ( write(*(_DWORD *)(v2 + 16), "content-type: text/html\r\n\r\n", 0x1BuLL) <= 0 )
return 1LL;
v3 = *(_BYTE **)(a2 + 104);
if ( !*v3 )
return 1LL;
v5 = strlen(*(const char **)(a2 + 104));
write(*(_DWORD *)(v2 + 16), v3, v5);
return 1LL;
}

original_code:
static int d_post_handler(handler_t hd, request_t rq)
{
 connection_t con = hd->con;
 if ( (strlen("content-type: text/html\r\n\r\n") > 0) && (write(con->sk, (void *)"content-type: text/html\r\n\r\n", strlen("content-type: text/html\r\n\r\n")) <= 0) ) goto out;
 if ( (strlen(rq->body) > 0) && (write(con->sk, (void *)rq->body, strlen(rq->body)) <= 0) ) goto out;
 out:
 return 1;
}

canonical_original_code:
static int d_post_handler(struct _handler_t *hd, struct _http_request *rq)
{
 struct _connection_t *con = hd->con;
 if ( (strlen("content-type: text/html\r\n\r\n") > 0) && (write(con->sk, (void *)"content-type: text/html\r\n\r\n", strlen("content-type: text/html\r\n\r\n")) <= 0) ) goto out;
 if ( (strlen(rq->body) > 0) && (write(con->sk, (void *)rq->body, strlen(rq->body)) <= 0) ) goto out;
 out:
 return 1;
}

user_defined_types: enum RQ_TYPE { HEAD=0, GET=1, POST=2, PUT=3, RQ_UNKNOWN=4 }


decompiled_code:
__int64 __fastcall d_get_handler(__int64 a1, __int64 a2)
{
__int64 v2; // rbp
_BYTE *v3; // r12
size_t v5; // rax

v2 = *(_QWORD *)(a1 + 32);
if ( write(*(_DWORD *)(v2 + 16), "content-type: text/html\r\n\r\n", 0x1BuLL) <= 0 )
return 1LL;
v3 = *(_BYTE **)(a2 + 96);
if ( !*v3 )
return 1LL;
v5 = strlen(*(const char **)(a2 + 96));
write(*(_DWORD *)(v2 + 16), v3, v5);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
__int64 v2; // rbp
_BYTE *v3; // r12
size_t v5; // rax

v2 = *(_QWORD *)(a1 + 32);
if ( write(*(_DWORD *)(v2 + 16), "content-type: text/html\r\n\r\n", 0x1BuLL) <= 0 )
return 1LL;
v3 = *(_BYTE **)(a2 + 96);
if ( !*v3 )
return 1LL;
v5 = strlen(*(const char **)(a2 + 96));
write(*(_DWORD *)(v2 + 16), v3, v5);
return 1LL;
}

original_code:
static int d_get_handler(handler_t hd, request_t rq)
{
 connection_t con = hd->con;
 if ( (strlen("content-type: text/html\r\n\r\n") > 0) && (write(con->sk, (void *)"content-type: text/html\r\n\r\n", strlen("content-type: text/html\r\n\r\n")) <= 0) ) goto out;
 if ( (strlen(rq->path) > 0) && (write(con->sk, (void *)rq->path, strlen(rq->path)) <= 0) ) goto out;
 out:
 return 1;
}

canonical_original_code:
static int d_get_handler(struct _handler_t *hd, struct _http_request *rq)
{
 struct _connection_t *con = hd->con;
 if ( (strlen("content-type: text/html\r\n\r\n") > 0) && (write(con->sk, (void *)"content-type: text/html\r\n\r\n", strlen("content-type: text/html\r\n\r\n")) <= 0) ) goto out;
 if ( (strlen(rq->path) > 0) && (write(con->sk, (void *)rq->path, strlen(rq->path)) <= 0) ) goto out;
 out:
 return 1;
}


decompiled_code:
// attributes: thunk
__int64 __fastcall d_head_handler(__int64 a1, __int64 a2)
{
return d_get_handler(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
return func2(a1, a2);
}

original_code:
static int d_head_handler(handler_t hd, request_t rq)
{
 connection_t con = hd->con;
 if ( (strlen("content-type: text/html\r\n\r\n") > 0) && (write(con->sk, (void *)"content-type: text/html\r\n\r\n", strlen("content-type: text/html\r\n\r\n")) <= 0) ) goto out;
 if ( (strlen(rq->path) > 0) && (write(con->sk, (void *)rq->path, strlen(rq->path)) <= 0) ) goto out;
 out:
 return 1;
}

canonical_original_code:
static int d_head_handler(struct _handler_t *hd, struct _http_request *rq)
{
 struct _connection_t *con = hd->con;
 if ( (strlen("content-type: text/html\r\n\r\n") > 0) && (write(con->sk, (void *)"content-type: text/html\r\n\r\n", strlen("content-type: text/html\r\n\r\n")) <= 0) ) goto out;
 if ( (strlen(rq->path) > 0) && (write(con->sk, (void *)rq->path, strlen(rq->path)) <= 0) ) goto out;
 out:
 return 1;
}


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

result = new_handler();
*(_QWORD *)(result + 48) = d_handle_con;
return result;
}

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

result = new_handler();
*(_QWORD *)(result + 48) = d_handle_con;
return result;
}

original_code:
static handler_t h_new_handler()
{
 handler_t hd;
 hd = new_handler();
 hd->handle_con = d_handle_con;
 return hd;
}

canonical_original_code:
static struct _handler_t *h_new_handler()
{
 struct _handler_t *hd;
 hd = new_handler();
 hd->handle_con = d_handle_con;
 return hd;
}