decompiled_code:
void __fastcall SMTP_info_con(__int64 a1)
{
__int64 v1; // rbp
const char **v2; // r12
const char *v3; // rbx
size_t v4; // rax
bool v5; // cf
bool v6; // zf
const char *v7; // rdi
__int64 v8; // rcx
char *v9; // rsi
bool v10; // al
bool v11; // cf
bool v12; // zf
const char *v13; // rdi
__int64 v14; // rcx
char *v15; // rsi
ssize_t v16; // rax
int v17; // eax
ssize_t v18; // rax
ssize_t v19; // rax
ssize_t v20; // rax
char v21[280]; // [rsp+0h] [rbp-118h] BYREF

v1 = *(_QWORD *)(a1 + 32);
if ( v1 )
{
v2 = *(const char ***)(a1 + 16);
*(_DWORD *)(a1 + 24) = 1;
fwrite("SMTP_info_con\n", 1uLL, 0xEuLL, stderr);
v3 = *v2;
v4 = strlen(*v2);
write(*(_DWORD *)(v1 + 16), v3, v4);
do
{
v9 = "> ";
if ( write(*(_DWORD *)(v1 + 16), "> ", 2uLL) <= 0 )
break;
memset(v21, 0, 0x100uLL);
v9 = v21;
if ( !read(*(_DWORD *)(v1 + 16), v21, 0xFFuLL) )
break;
v10 = memcmp(v21, "info", 4uLL) != 0;
v11 = 0;
v12 = !v10;
if ( v10 )
{
v13 = "kill";
v14 = 4LL;
v15 = v21;
do
{
if ( !v14 )
break;
v11 = (unsigned __int8)*v15 < (unsigned int)*v13;
v12 = *v15++ == *v13++;
--v14;
}
while ( v12 );
if ( (!v11 && !v12) == v11 )
{
(*(void (__fastcall **)(__int64, char *))(SMTP_server + 88))(SMTP_server, v15);
v9 = "SMTP_server destroyed... now what?\r\n";
v16 = write(*(_DWORD *)(v1 + 16), "SMTP_server destroyed... now what?\r\n", 0x24uLL);
v5 = 0;
v6 = v16 == 0;
if ( v16 <= 0 )
break;
}
else if ( !memcmp(v21, "help", 4uLL) )
{
v9 = "Commands:\r\n";
if ( write(*(_DWORD *)(v1 + 16), "Commands:\r\n", 0xBuLL) <= 0 )
break;
v9 = "info -- get info on threads.\r\n";
if ( write(*(_DWORD *)(v1 + 16), "info -- get info on threads.\r\n", 0x24uLL) <= 0 )
break;
v9 = "kill -- kill server. Currently unclean.\r\n";
if ( write(*(_DWORD *)(v1 + 16), "kill -- kill server. Currently unclean.\r\n", 0x2FuLL) <= 0 )
break;
v9 = "set opt n -- set a server variable.\r\n";
if ( write(*(_DWORD *)(v1 + 16), "set opt n -- set a server variable.\r\n", 0x26uLL) <= 0 )
break;
v9 = " recognized opts are:\r\n";
if ( write(*(_DWORD *)(v1 + 16), " recognized opts are:\r\n", 0x24uLL) <= 0 )
break;
v9 = " min_delay: minimum delay for responses\r\n";
if ( write(*(_DWORD *)(v1 + 16), " min_delay: minimum delay for responses\r\n", 0x2FuLL) <= 0 )
break;
v9 = " max_delay: maximum delay for responses\r\n";
if ( write(*(_DWORD *)(v1 + 16), " max_delay: maximum delay for responses\r\n", 0x2FuLL) <= 0 )
break;
v9 = " percent_4xx: percentage of 4xx responses\r\n";
if ( write(*(_DWORD *)(v1 + 16), " percent_4xx: percentage of 4xx responses\r\n", 0x2FuLL) <= 0 )
break;
v9 = " percent_5xx: percentage of 5xx responses\r\n";
if ( write(*(_DWORD *)(v1 + 16), " percent_5xx: percentage of 5xx responses\r\n", 0x2FuLL) <= 0 )
break;
v9 = " percent_dropped: probability of dropping con NOW\r\n";
if ( write(*(_DWORD *)(v1 + 16), " percent_dropped: probability of dropping con NOW\r\n", 0x33uLL) <= 0 )
break;
v9 = "\r\n";
v19 = write(*(_DWORD *)(v1 + 16), "\r\n", 2uLL);
v5 = 0;
v6 = v19 == 0;
if ( v19 <= 0 )
break;
}
else if ( !memcmp(v21, "set ", 4uLL) )
{
v17 = SMTP_set_command(&v21[4]);
v5 = 0;
v6 = v17 == 0;
if ( !v17 )
{
v9 = "set command error\r\n";
v18 = write(*(_DWORD *)(v1 + 16), "set command error\r\n", 0x13uLL);
v5 = 0;
v6 = v18 == 0;
if ( v18 <= 0 )
break;
}
}
else
{
v9 = "type \"help\" for help\r\n";
v20 = write(*(_DWORD *)(v1 + 16), "type \"help\" for help\r\n", 0x16uLL);
v5 = 0;
v6 = v20 == 0;
if ( v20 <= 0 )
break;
}
}
else
{
SMTP_summary((__int64)v2, *(_DWORD *)(v1 + 16));
}
v7 = "quit";
v8 = 4LL;
v9 = v21;
do
{
if ( !v8 )
break;
v5 = (unsigned __int8)*v9 < (unsigned int)*v7;
v6 = *v9++ == *v7++;
--v8;
}
while ( v6 );
}
while ( (!v5 && !v6) != v5 );
(*(void (__fastcall **)(__int64, char *))(v1 + 56))(v1, v9);
}
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1)
{
__int64 v1; // rbp
const char **v2; // r12
const char *v3; // rbx
size_t v4; // rax
bool v5; // cf
bool v6; // zf
const char *v7; // rdi
__int64 v8; // rcx
char *v9; // rsi
bool v10; // al
bool v11; // cf
bool v12; // zf
const char *v13; // rdi
__int64 v14; // rcx
char *v15; // rsi
ssize_t v16; // rax
int v17; // eax
ssize_t v18; // rax
ssize_t v19; // rax
ssize_t v20; // rax
char v21[280]; // [rsp+0h] [rbp-118h] BYREF

v1 = *(_QWORD *)(a1 + 32);
if ( v1 )
{
v2 = *(const char ***)(a1 + 16);
*(_DWORD *)(a1 + 24) = 1;
fwrite("SMTP_info_con\n", 1uLL, 0xEuLL, stderr);
v3 = *v2;
v4 = strlen(*v2);
write(*(_DWORD *)(v1 + 16), v3, v4);
do
{
v9 = "> ";
if ( write(*(_DWORD *)(v1 + 16), "> ", 2uLL) <= 0 )
break;
memset(v21, 0, 0x100uLL);
v9 = v21;
if ( !read(*(_DWORD *)(v1 + 16), v21, 0xFFuLL) )
break;
v10 = memcmp(v21, "info", 4uLL) != 0;
v11 = 0;
v12 = !v10;
if ( v10 )
{
v13 = "kill";
v14 = 4LL;
v15 = v21;
do
{
if ( !v14 )
break;
v11 = (unsigned __int8)*v15 < (unsigned int)*v13;
v12 = *v15++ == *v13++;
--v14;
}
while ( v12 );
if ( (!v11 && !v12) == v11 )
{
(*(void (__fastcall **)(__int64, char *))(SMTP_server + 88))(SMTP_server, v15);
v9 = "SMTP_server destroyed... now what?\r\n";
v16 = write(*(_DWORD *)(v1 + 16), "SMTP_server destroyed... now what?\r\n", 0x24uLL);
v5 = 0;
v6 = v16 == 0;
if ( v16 <= 0 )
break;
}
else if ( !memcmp(v21, "help", 4uLL) )
{
v9 = "Commands:\r\n";
if ( write(*(_DWORD *)(v1 + 16), "Commands:\r\n", 0xBuLL) <= 0 )
break;
v9 = "info -- get info on threads.\r\n";
if ( write(*(_DWORD *)(v1 + 16), "info -- get info on threads.\r\n", 0x24uLL) <= 0 )
break;
v9 = "kill -- kill server. Currently unclean.\r\n";
if ( write(*(_DWORD *)(v1 + 16), "kill -- kill server. Currently unclean.\r\n", 0x2FuLL) <= 0 )
break;
v9 = "set opt n -- set a server variable.\r\n";
if ( write(*(_DWORD *)(v1 + 16), "set opt n -- set a server variable.\r\n", 0x26uLL) <= 0 )
break;
v9 = " recognized opts are:\r\n";
if ( write(*(_DWORD *)(v1 + 16), " recognized opts are:\r\n", 0x24uLL) <= 0 )
break;
v9 = " min_delay: minimum delay for responses\r\n";
if ( write(*(_DWORD *)(v1 + 16), " min_delay: minimum delay for responses\r\n", 0x2FuLL) <= 0 )
break;
v9 = " max_delay: maximum delay for responses\r\n";
if ( write(*(_DWORD *)(v1 + 16), " max_delay: maximum delay for responses\r\n", 0x2FuLL) <= 0 )
break;
v9 = " percent_4xx: percentage of 4xx responses\r\n";
if ( write(*(_DWORD *)(v1 + 16), " percent_4xx: percentage of 4xx responses\r\n", 0x2FuLL) <= 0 )
break;
v9 = " percent_5xx: percentage of 5xx responses\r\n";
if ( write(*(_DWORD *)(v1 + 16), " percent_5xx: percentage of 5xx responses\r\n", 0x2FuLL) <= 0 )
break;
v9 = " percent_dropped: probability of dropping con NOW\r\n";
if ( write(*(_DWORD *)(v1 + 16), " percent_dropped: probability of dropping con NOW\r\n", 0x33uLL) <= 0 )
break;
v9 = "\r\n";
v19 = write(*(_DWORD *)(v1 + 16), "\r\n", 2uLL);
v5 = 0;
v6 = v19 == 0;
if ( v19 <= 0 )
break;
}
else if ( !memcmp(v21, "set ", 4uLL) )
{
v17 = func4(&v21[4]);
v5 = 0;
v6 = v17 == 0;
if ( !v17 )
{
v9 = "set command error\r\n";
v18 = write(*(_DWORD *)(v1 + 16), "set command error\r\n", 0x13uLL);
v5 = 0;
v6 = v18 == 0;
if ( v18 <= 0 )
break;
}
}
else
{
v9 = "type \"help\" for help\r\n";
v20 = write(*(_DWORD *)(v1 + 16), "type \"help\" for help\r\n", 0x16uLL);
v5 = 0;
v6 = v20 == 0;
if ( v20 <= 0 )
break;
}
}
else
{
func3((__int64)v2, *(_DWORD *)(v1 + 16));
}
v7 = "quit";
v8 = 4LL;
v9 = v21;
do
{
if ( !v8 )
break;
v5 = (unsigned __int8)*v9 < (unsigned int)*v7;
v6 = *v9++ == *v7++;
--v8;
}
while ( v6 );
}
while ( (!v5 && !v6) != v5 );
(*(void (__fastcall **)(__int64, char *))(v1 + 56))(v1, v9);
}
}

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

repo: adsgray/CServer

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

return_type: 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; // ebp
_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;
result = "250 looks good\r\n";
}
else if ( v5 == 5 )
{
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";
}
}
else
{
result = "250 looks good\r\n";
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func8(__int64 a1, int *a2)
{
__int64 v2; // r13
_BOOL4 v3; // ebp
_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;
result = "250 looks good\r\n";
}
else if ( v5 == 5 )
{
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";
}
}
else
{
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: 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: 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 *

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 handler_rec { unsigned long tid; struct _handler_t *hd; }

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 _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 __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:
void __fastcall SMTP_with_options(__int64 a1)
{
__int64 v1; // r12
__int64 v2; // r13
const char *v3; // rbx
ssize_t v4; // rbx
unsigned int v5; // ebx
size_t v6; // rax
unsigned int v7; // [rsp+Ch] [rbp-12Ch] BYREF
char s1[296]; // [rsp+10h] [rbp-128h] BYREF

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

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

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

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: 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: 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 *

return_type: void


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

v1 = new_server(0LL);
SMTP_server = v1;
*(_QWORD *)v1 = "220 SMTPsink v0.9.0\r\n";
*(_QWORD *)(v1 + 8) = "/tmp/smtpsink.pid";
*(_QWORD *)(v1 + 16) = "/tmp/smtpsink.log";
*(_WORD *)(v1 + 40) = 2525;
*(_DWORD *)(v1 + 44) = 15;
*(_DWORD *)(v1 + 48) = 35;
*(_DWORD *)(v1 + 52) = 50;
*(_QWORD *)(v1 + 72) = SMTP_new_handler;
*(_QWORD *)(v1 + 32) = ag_malloc(20LL);
v2 = new_server(0LL);
*(_QWORD *)v2 = "SMTPinfo v0.9.0\r\n";
*(_WORD *)(v2 + 40) = 2552;
*(_DWORD *)(v2 + 44) = 2;
*(_DWORD *)(v2 + 48) = 5;
*(_DWORD *)(v2 + 52) = 5;
*(_QWORD *)(v2 + 72) = SMTP_Info_handler;
options = a1;
pthread_create(newthread, 0LL, *(void *(**)(void *))(v2 + 96), (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; // ebp

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

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

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


decompiled_code:
__int64 __fastcall default_add_con(pthread_mutex_t *mutex, __int64 a2)
{
struct __pthread_internal_list *v2; // r12
__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; // r12
__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 *); } *

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 __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
void __fastcall __noreturn default_wait_on(pthread_mutex_t *mutex, __int64 a2)
{
struct __pthread_internal_list *v2; // r13
__int64 v3; // rbp

v2 = mutex[2].__list.__prev;
pthread_mutex_lock(mutex);
while ( 1 )
{
if ( !mutex[2].__owner )
{
*(_DWORD *)(a2 + 24) = 2;
pthread_cond_wait((pthread_cond_t *)&mutex[1], mutex);
}
if ( mutex[2].__owner > 0 )
{
v3 = ((__int64 (__fastcall *)(struct __pthread_internal_list *))v2[1].__next)(v2);
--mutex[2].__owner;
pthread_cond_signal((pthread_cond_t *)&mutex[1]);
pthread_mutex_unlock(mutex);
*(_QWORD *)(a2 + 32) = v3;
(*(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
__int64 v3; // rbp

v2 = mutex[2].__list.__prev;
pthread_mutex_lock(mutex);
while ( 1 )
{
if ( !mutex[2].__owner )
{
*(_DWORD *)(a2 + 24) = 2;
pthread_cond_wait((pthread_cond_t *)&mutex[1], mutex);
}
if ( mutex[2].__owner > 0 )
{
v3 = ((__int64 (__fastcall *)(struct __pthread_internal_list *))v2[1].__next)(v2);
--mutex[2].__owner;
pthread_cond_signal((pthread_cond_t *)&mutex[1]);
pthread_mutex_unlock(mutex);
*(_QWORD *)(a2 + 32) = v3;
(*(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; // rbx

v0 = ag_malloc(136LL);
*(_QWORD *)(v0 + 104) = new_queue();
*(_QWORD *)(v0 + 120) = default_wait_on;
*(_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; // rbx

v0 = ag_malloc(136LL);
*(_QWORD *)(v0 + 104) = new_queue();
*(_QWORD *)(v0 + 120) = default_wait_on;
*(_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 setup_connection(__int64 a1)
{
int v1; // eax
unsigned int v2; // ebx
struct sockaddr v4; // [rsp+0h] [rbp-28h] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
int v1; // eax
unsigned int v2; // ebx
struct sockaddr v4; // [rsp+0h] [rbp-28h] BYREF

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

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: sock int

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

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


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 func4(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: len int

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


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 _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: i int

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


decompiled_code:
ssize_t __fastcall default_summary(__int64 a1, int a2)
{
size_t v2; // rax
ssize_t result; // rax
__int64 v4; // rbp
__int64 v5; // rdx
const char *v6; // rax
size_t v7; // rax
char v8[552]; // [rsp+0h] [rbp-228h] BYREF

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

canonical_decompiled_code:
ssize_t __fastcall func6(__int64 a1, int a2)
{
size_t v2; // rax
ssize_t result; // rax
__int64 v4; // rbp
__int64 v5; // rdx
const char *v6; // rax
size_t v7; // rax
char v8[552]; // [rsp+0h] [rbp-228h] BYREF

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

variable_types:
buf
char [512]


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


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

canonical_decompiled_code:
__int64 __fastcall func1(void *ptr)
{
if ( !ptr )
return 1LL;
*((_DWORD *)ptr + 6) = 3;
(*(void (__fastcall **)(_QWORD))(*((_QWORD *)ptr + 4) + 56LL))(*((_QWORD *)ptr + 4));
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);
}


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:
_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;
}

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:
__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;
}

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:
__int64 __fastcall d_default_handler(__int64 a1, __int64 a2)
{
__int64 v2; // rbp
const char *v3; // rbx
size_t v5; // rax

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

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

v2 = *(_QWORD *)(a1 + 32);
if ( write(*(_DWORD *)(v2 + 16), "content-type: text/html\r\n\r\n", 0x1BuLL) > 0 )
{
v3 = *(const char **)(a2 + 120);
if ( *v3 )
{
v5 = strlen(v3);
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_head_handler(__int64 a1, __int64 a2)
{
__int64 v2; // rbp
const char *v3; // rbx
size_t v5; // rax

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

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

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

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

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


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

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

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

v2 = *(_QWORD *)(a1 + 32);
if ( write(*(_DWORD *)(v2 + 16), "content-type: text/html\r\n\r\n", 0x1BuLL) > 0 )
{
v3 = *(const char **)(a2 + 104);
if ( *v3 )
{
v5 = strlen(v3);
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;
}


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

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

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
__int64 v2; // rbp
const char *v3; // rbx
size_t v5; // rax

v2 = *(_QWORD *)(a1 + 32);
if ( write(*(_DWORD *)(v2 + 16), "content-type: text/html\r\n\r\n", 0x1BuLL) > 0 )
{
v3 = *(const char **)(a2 + 96);
if ( *v3 )
{
v5 = strlen(v3);
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:
__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;
}


decompiled_code:
void *get_addr()
{
int v0; // eax
void *result; // rax

v0 = shmget(4662, 0x1004uLL, 950);
if ( v0 < 0 )
{
perror("shmget");
result = 0LL;
}
else
{
result = shmat(v0, 0LL, 0);
if ( result == (void *)-1LL )
{
perror("shmat");
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
void *func8()
{
int v0; // eax
void *result; // rax

v0 = shmget(4662, 0x1004uLL, 950);
if ( v0 < 0 )
{
perror("shmget");
result = 0LL;
}
else
{
result = shmat(v0, 0LL, 0);
if ( result == (void *)-1LL )
{
perror("shmat");
result = 0LL;
}
}
return result;
}

original_code:
static Shared_use_st* get_addr()
{
    void *shm = ((void *)0);
    int shmid;
    shmid = shmget((key_t)0x1236, sizeof(Shared_use_st), 0666|01000);
    if (shmid < 0) {perror("shmget");return ((void *)0);}
    shm = shmat(shmid, 0, 0);
    if(shm == (void*)-1){perror("shmat");return ((void *)0);};
    return (struct shared_use_st*)shm;
}

canonical_original_code:
static struct shared_use_st *get_addr()
{
    void *shm = ((void *)0);
    int shmid;
    shmid = shmget((int)0x1236, sizeof(Shared_use_st), 0666|01000);
    if (shmid < 0) {perror("shmget");return ((void *)0);}
    shm = shmat(shmid, 0, 0);
    if(shm == (void *)-1){perror("shmat");return ((void *)0);};
    return (struct shared_use_st *)shm;
}

variable_types:
shm
void *

variable_types: shmid int

return_type: struct shared_use_st { int size; char text[4096]; } *


decompiled_code:
void __fastcall untach_addr(const void *a1)
{
if ( shmdt(a1) == -1 )
perror("shmdt");
}

canonical_decompiled_code:
void __fastcall func9(const void *a1)
{
if ( shmdt(a1) == -1 )
perror("shmdt");
}

original_code:
static void untach_addr(Shared_use_st* addr)
{
    if(shmdt((void*)addr) == -1)
    {
 perror("shmdt");
    }
}

canonical_original_code:
static void untach_addr(struct shared_use_st *addr)
{
    if(shmdt((void *)addr) == -1)
    {
 perror("shmdt");
    }
}

variable_types: struct shared_use_st { int size; char text[4096]; } *


decompiled_code:
__int64 __fastcall copy_from_share_mem(_QWORD *a1)
{
_QWORD *v1; // rax

v1 = get_addr();
if ( !v1 )
return 0xFFFFFFFFLL;
*a1 = *v1;
*(_QWORD *)((char *)a1 + 4092) = *(_QWORD *)((char *)v1 + 4092);
qmemcpy(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)((char *)v1 - ((char *)a1 - ((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 4100) >> 3));
untach_addr(v1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func12(_QWORD *a1)
{
_QWORD *v1; // rax

v1 = func8();
if ( !v1 )
return 0xFFFFFFFFLL;
*a1 = *v1;
*(_QWORD *)((char *)a1 + 4092) = *(_QWORD *)((char *)v1 + 4092);
qmemcpy(
(void *)((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL),
(const void *)((char *)v1 - ((char *)a1 - ((unsigned __int64)(a1 + 1) & 0xFFFFFFFFFFFFFFF8LL))),
8LL * (((unsigned int)a1 - (((_DWORD)a1 + 8) & 0xFFFFFFF8) + 4100) >> 3));
func9(v1);
return 0LL;
}

original_code:
static int copy_from_share_mem(Shared_use_st* st)
{
    struct shared_use_st *shared;
    shared=get_addr();
    if (shared == ((void *)0)) return -1;
    memcpy((void*)st,(void*)shared,sizeof(Shared_use_st));
    untach_addr(shared);
    return 0;
}

canonical_original_code:
static int copy_from_share_mem(struct shared_use_st *st)
{
    struct shared_use_st *shared;
    shared=get_addr();
    if (shared == ((void *)0)) return -1;
    memcpy((void *)st,(void *)shared,sizeof(Shared_use_st));
    untach_addr(shared);
    return 0;
}

user_defined_types: struct shared_use_st { int size; char text[4096]; }


decompiled_code:
int __fastcall copy_to_clip_baord(const char *a1)
{
char v2[1032]; // [rsp+0h] [rbp-408h] BYREF

printf("\x1B[35m%s:copyed\n\x1B[m", a1);
snprintf(v2, 0x400uLL, "echo \"%s\" | xclip -selection clipboard", a1);
return system(v2);
}

canonical_decompiled_code:
int __fastcall func13(const char *a1)
{
char v2[1032]; // [rsp+0h] [rbp-408h] BYREF

printf("\x1B[35m%s:copyed\n\x1B[m", a1);
snprintf(v2, 0x400uLL, "echo \"%s\" | xclip -selection clipboard", a1);
return system(v2);
}

original_code:
static void copy_to_clip_baord(const char* str)
{
    char cmd[1024];
    printf("\033[35m""%s:copyed\n""\033[m",str);
    snprintf(cmd,sizeof(cmd),"echo \"%s\" | xclip -selection clipboard",str);
    system(cmd);
}

canonical_original_code:
static void copy_to_clip_baord(char *str)
{
    char cmd[1024];
    printf("\033[35m""%s:copyed\n""\033[m",str);
    snprintf(cmd,sizeof(cmd),"echo \"%s\" | xclip -selection clipboard",str);
    system(cmd);
}

variable_types:
str
char *

variable_types:
cmd
char [1024]


main

canonical_name: main

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

if ( pollRead(0, 1) > 0 )
{
write_to_mem();
}
else if ( argc == 1 )
{
read_from_mem(1);
return 0;
}
if ( argc > 1 )
{
v4 = atoi(argv[1]);
read_from_mem(v4);
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // eax

if ( func7(0, 1) > 0 )
{
func11();
}
else if ( argc == 1 )
{
func14(1);
return 0;
}
if ( argc > 1 )
{
v4 = atoi(argv[1]);
func14(v4);
}
return 0;
}

original_code:
int main(int argc,char* argv[])
{
    int isHaveData=0;
    if (pollRead(0,1) > 0){
 isHaveData=1;
 write_to_mem();
     }
    if ( !isHaveData && argc == 1){
 read_from_mem(1);
 return 0;
    }
    if (argc > 1){
 read_from_mem(atoi(argv[1]));
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int isHaveData = 0;
    if (pollRead(0,1) > 0){
 isHaveData=1;
 write_to_mem();
     }
    if ( !isHaveData && argc == 1){
 read_from_mem(1);
 return 0;
    }
    if (argc > 1){
 read_from_mem(atoi(argv[1]));
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: isHaveData int


repo: aep/confine

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // r12
const char *v4; // r13
size_t v5; // rax
char *v6; // rbx
__uid_t v7; // eax
__gid_t v8; // eax
int result; // eax

if ( argc <= 2 )
{
fprintf(stderr, "usage: %s /path/to/allow someprogram someargs\n", *argv);
result = 3;
}
else
{
v3 = (char *)malloc(0x7D0uLL);
getcwd(v3, 0x7D0uLL);
if ( unshare(0x20000) )
perror("unshare");
if ( mount("/", "/", 0LL, 0x44000uLL, 0LL) )
perror("make private");
if ( mount("/", "/root", 0LL, 0x45001uLL, 0LL) )
perror("bind /");
if ( mount("/", "/root", 0LL, 0x45021uLL, 0LL) )
perror("bind /");
v4 = argv[1];
v5 = strlen(v4);
v6 = (char *)malloc(v5 + 6);
strcpy(v6, "/root/");
strcat(v6, v4);
if ( mount(v4, v6, 0LL, 0x41000uLL, 0LL) )
perror("bind ");
chroot("/root");
v7 = getuid();
seteuid(v7);
v8 = getgid();
setegid(v8);
chdir(v3);
execvp(argv[2], (char *const *)argv + 3);
result = 0;
}
return result;
}

original_code:
int main(int argc, char **argv)
{
    if (argc < 3) {
        fprintf(stderr, "usage: %s /path/to/allow someprogram someargs\n", argv[0]);
        return 3;
    }
    char *pwd = malloc (2000);
    getcwd(pwd, 2000);
    int r = unshare(0x00020000);
    if (r != 0)
        perror("unshare");
    r = mount("/","/", 0, MS_PRIVATE | MS_REC, 0);
    if (r != 0)
        perror("make private");
    r = mount("/", "/root", 0, MS_BIND | MS_RDONLY | MS_REC | MS_PRIVATE , 0);
    if (r != 0)
        perror("bind /");
    r = mount("/", "/root", 0, MS_BIND | MS_RDONLY | MS_REC | MS_PRIVATE | MS_REMOUNT, 0);
    if (r != 0)
        perror("bind /");
    char *m = malloc(strlen("/root/") + strlen(argv[1]));
    strcpy(m, "/root/");
    strcat(m, argv[1]);
    r = mount(argv[1], m, 0, MS_BIND | MS_PRIVATE, 0);
    if (r != 0)
        perror("bind ");
    chroot ("/root");
    seteuid(getuid());
    setegid(getgid());
    chdir(pwd);
    execvp(argv[2] , argv + 3);
}

variable_types:
argv
char **

variable_types:
pwd
char *

variable_types: r int

variable_types:
m
char *


repo: aerospike/act

decompiled_code:
void *__fastcall run_salt(void *a1)
{
__int64 v1; // rbx
unsigned __int64 v2; // r14
unsigned __int64 v3; // r12
void *v4; // rbp
unsigned int v5; // er13
unsigned __int64 v6; // rbx

rand_seed_thread();
v1 = g_blocks_per_salt_thread;
v2 = g_blocks_per_salt_thread;
v3 = 0LL;
if ( (_DWORD)a1 == 7 )
{
v2 = g_extra_blocks_to_salt + g_blocks_per_salt_thread;
v3 = 1LL;
if ( (unsigned __int64)(g_extra_blocks_to_salt + g_blocks_per_salt_thread) > 0x63 )
v3 = v2 / 0x64;
}
v4 = act_valloc(0x20000uLL);
if ( v4 )
{
v5 = fd_get();
if ( v5 == -1 )
{
printf("ERROR: open in salt thread %u\n", (unsigned int)a1);
free(v4);
}
else if ( ((unsigned int)a1 * v1) << 17 == lseek64(v5, ((unsigned int)a1 * v1) << 17, 0) )
{
v6 = 0LL;
if ( v2 )
{
while ( 1 )
{
rand_fill(v4, 0x20000LL, 100LL);
if ( !(unsigned __int8)write_all(v5, v4, 0x20000LL) )
break;
if ( v3 && !(v6 % v3) )
{
putchar(46);
fflush(stdout);
}
if ( v2 == ++v6 )
goto LABEL_13;
}
printf("ERROR: write in salt thread %u\n", (unsigned int)a1);
}
LABEL_13:
if ( v3 )
putchar(10);
close(v5);
free(v4);
}
else
{
printf("ERROR: seek in salt thread %u\n", (unsigned int)a1);
close(v5);
free(v4);
}
}
else
{
printf("ERROR: valloc in salt thread %u\n", (unsigned int)a1);
}
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func2(void *a1)
{
__int64 v1; // rbx
unsigned __int64 v2; // r14
unsigned __int64 v3; // r12
void *v4; // rbp
unsigned int v5; // er13
unsigned __int64 v6; // rbx

rand_seed_thread();
v1 = g_blocks_per_salt_thread;
v2 = g_blocks_per_salt_thread;
v3 = 0LL;
if ( (_DWORD)a1 == 7 )
{
v2 = g_extra_blocks_to_salt + g_blocks_per_salt_thread;
v3 = 1LL;
if ( (unsigned __int64)(g_extra_blocks_to_salt + g_blocks_per_salt_thread) > 0x63 )
v3 = v2 / 0x64;
}
v4 = func0(0x20000uLL);
if ( v4 )
{
v5 = func1();
if ( v5 == -1 )
{
printf("ERROR: open in salt thread %u\n", (unsigned int)a1);
free(v4);
}
else if ( ((unsigned int)a1 * v1) << 17 == lseek64(v5, ((unsigned int)a1 * v1) << 17, 0) )
{
v6 = 0LL;
if ( v2 )
{
while ( 1 )
{
rand_fill(v4, 0x20000LL, 100LL);
if ( !(unsigned __int8)write_all(v5, v4, 0x20000LL) )
break;
if ( v3 && !(v6 % v3) )
{
putchar(46);
fflush(stdout);
}
if ( v2 == ++v6 )
goto LABEL_13;
}
printf("ERROR: write in salt thread %u\n", (unsigned int)a1);
}
LABEL_13:
if ( v3 )
putchar(10);
close(v5);
free(v4);
}
else
{
printf("ERROR: seek in salt thread %u\n", (unsigned int)a1);
close(v5);
free(v4);
}
}
else
{
printf("ERROR: valloc in salt thread %u\n", (unsigned int)a1);
}
return 0LL;
}

original_code:
static void*
run_salt(void* pv_n)
{
 rand_seed_thread();
 uint32_t n = (uint32_t)(uint64_t)pv_n;
 uint64_t offset = n * g_blocks_per_salt_thread * (1024 * 128);
 uint64_t blocks_to_salt = g_blocks_per_salt_thread;
 uint64_t progress_blocks = 0;
 _Bool last_thread = n + 1 == 8;
 if (last_thread) {
  blocks_to_salt += g_extra_blocks_to_salt;
  progress_blocks = blocks_to_salt / 100;
  if (! progress_blocks) {
   progress_blocks = 1;
  }
 }
 uint8_t* buf = act_valloc((1024 * 128));
 if (! buf) {
  printf("ERROR: valloc in salt thread %" "u" "\n", n);
  return ((void *)0);
 }
 int fd = fd_get();
 if (fd == -1) {
  printf("ERROR: open in salt thread %" "u" "\n", n);
  free(buf);
  return ((void *)0);
 }
 if (lseek(fd, offset, 0) != offset) {
  printf("ERROR: seek in salt thread %" "u" "\n", n);
  close(fd);
  free(buf);
  return ((void *)0);
 }
 for (uint64_t b = 0; b < blocks_to_salt; b++) {
  rand_fill(buf, (1024 * 128), 100);
  if (! write_all(fd, buf, (1024 * 128))) {
   printf("ERROR: write in salt thread %" "u" "\n", n);
   break;
  }
  if (progress_blocks && ! (b % progress_blocks)) {
   printf(".");
   fflush(stdout);
  }
 }
 if (progress_blocks) {
  printf("\n");
 }
 close(fd);
 free(buf);
 return ((void *)0);
}

canonical_original_code:
static void *run_salt(void *pv_n)
{
 rand_seed_thread();
 unsigned int n = (uint32_t)(uint64_t)pv_n;
 unsigned long offset = n * g_blocks_per_salt_thread * (1024 * 128);
 unsigned long blocks_to_salt = g_blocks_per_salt_thread;
 unsigned long progress_blocks = 0;
 _Bool last_thread = n + 1 == 8;
 if (last_thread) {
  blocks_to_salt += g_extra_blocks_to_salt;
  progress_blocks = blocks_to_salt / 100;
  if (! progress_blocks) {
   progress_blocks = 1;
  }
 }
 unsigned char *buf = act_valloc((1024 * 128));
 if (! buf) {
  printf("ERROR: valloc in salt thread %" "u" "\n", n);
  return ((void *)0);
 }
 int fd = fd_get();
 if (fd == -1) {
  printf("ERROR: open in salt thread %" "u" "\n", n);
  free(buf);
  return ((void *)0);
 }
 if (lseek(fd, offset, 0) != offset) {
  printf("ERROR: seek in salt thread %" "u" "\n", n);
  close(fd);
  free(buf);
  return ((void *)0);
 }
 for (unsigned long b = 0; b < blocks_to_salt; b++) {
  rand_fill(buf, (1024 * 128), 100);
  if (! write_all(fd, buf, (1024 * 128))) {
   printf("ERROR: write in salt thread %" "u" "\n", n);
   break;
  }
  if (progress_blocks && ! (b % progress_blocks)) {
   printf(".");
   fflush(stdout);
  }
 }
 if (progress_blocks) {
  printf("\n");
 }
 close(fd);
 free(buf);
 return ((void *)0);
}

variable_types:
n
unsigned int

variable_types:
offset
unsigned long

variable_types:
buf
unsigned char *

variable_types: fd int

variable_types:
b
unsigned long