original_code:
int add_new_appstruct(struct AppStruct **appstructs, int array_size,
                      int element_index, char *user, char *app, int port) {
  if (array_size <= element_index + 1) {
    int index, new_size;
    struct AppStruct *newstructs;
    struct AppStruct *oldstructs = *appstructs;
    new_size = (array_size > 0) ? 2 * array_size : (2);
    newstructs = calloc(new_size, sizeof(struct AppStruct));
    for (index = 0; index < element_index; index++) {
      newstructs[index] = oldstructs[index];
    }
    for (index = element_index; index < new_size; index++) {
      newstructs[index].args = calloc((2), sizeof(char *));
      newstructs[index].argnum = 0;
    }
    *appstructs = newstructs;
    array_size = new_size;
    if (oldstructs != ((void *)0)) free(oldstructs);
  }
  (*appstructs)[element_index].port = port;
  (*appstructs)[element_index].user = strdup(user);
  (*appstructs)[element_index].app = strdup(app);
  return array_size;
}

canonical_original_code:
int add_new_appstruct(struct AppStruct **appstructs, int array_size, int element_index, char *user, char *app, int port) {
  if (array_size <= element_index + 1) {
    int index; int new_size;
    struct AppStruct *newstructs;
    struct AppStruct *oldstructs = *appstructs;
    new_size = (array_size > 0) ? 2 * array_size : (2);
    newstructs = calloc(new_size, sizeof(struct AppStruct));
    for (index = 0; index < element_index; index++) {
      newstructs[index] = oldstructs[index];
    }
    for (index = element_index; index < new_size; index++) {
      newstructs[index].args = calloc((2), sizeof(char *));
      newstructs[index].argnum = 0;
    }
    *appstructs = newstructs;
    array_size = new_size;
    if (oldstructs != ((void *)0)) free(oldstructs);
  }
  (*appstructs)[element_index].port = port;
  (*appstructs)[element_index].user = strdup(user);
  (*appstructs)[element_index].app = strdup(app);
  return array_size;
}

variable_types: struct AppStruct { int port; char *user; char *app; char **args; int argnum; } **

variable_types:
user
char *

variable_types:
app
char *

variable_types: port int

variable_types: index int


decompiled_code:
__int64 __fastcall compare_appstruct(int a1, int a2, int a3, int a4, int a5, int a6, int a7, char *a8, char *s1, __int64 a10, int a11, int a12, char *a13, char *s2, __int64 a15, int a16)
{
__int64 result; // rax
int v17; // edx
int v18; // edx
__int64 i; // rbx

result = 0xFFFFFFFFLL;
if ( a7 == a12 )
{
v17 = strcmp(s1, s2);
result = 0xFFFFFFFFLL;
if ( !v17 )
{
v18 = strcmp(a8, a13);
result = 0xFFFFFFFFLL;
if ( !v18 && a16 == a11 )
{
if ( a11 <= 1 )
{
result = 1LL;
}
else
{
for ( i = 0LL; !strcmp(*(const char **)(a10 + 8 * i), *(const char **)(a15 + 8 * i)); ++i )
{
if ( i == a11 - 2 )
return 1LL;
}
result = 0xFFFFFFFFLL;
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, int a2, int a3, int a4, int a5, int a6, int a7, char *a8, char *s1, __int64 a10, int a11, int a12, char *a13, char *s2, __int64 a15, int a16)
{
__int64 result; // rax
int v17; // edx
int v18; // edx
__int64 i; // rbx

result = 0xFFFFFFFFLL;
if ( a7 == a12 )
{
v17 = strcmp(s1, s2);
result = 0xFFFFFFFFLL;
if ( !v17 )
{
v18 = strcmp(a8, a13);
result = 0xFFFFFFFFLL;
if ( !v18 && a16 == a11 )
{
if ( a11 <= 1 )
{
result = 1LL;
}
else
{
for ( i = 0LL; !strcmp(*(const char **)(a10 + 8 * i), *(const char **)(a15 + 8 * i)); ++i )
{
if ( i == a11 - 2 )
return 1LL;
}
result = 0xFFFFFFFFLL;
}
}
}
}
return result;
}

original_code:
int compare_appstruct(struct AppStruct new_appstruct,
                      struct AppStruct old_appstruct) {
  int not_same = -1;
  int is_same = 1;
  if (new_appstruct.port != old_appstruct.port) {
    return not_same;
  }
  if (strcmp(new_appstruct.app, old_appstruct.app) != 0) {
    return not_same;
  }
  if (strcmp(new_appstruct.user, old_appstruct.user) != 0) {
    return not_same;
  }
  if (new_appstruct.argnum != old_appstruct.argnum) {
    return not_same;
  }
  int index;
  for (index = 0; index < new_appstruct.argnum - 1; index++) {
    if (strcmp(new_appstruct.args[index], old_appstruct.args[index]) != 0) {
      return not_same;
    }
  }
  return is_same;
}

variable_types: struct AppStruct { int port; char *user; char *app; char **args; int argnum; }

return_type: int


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

if ( a2 <= 0 )
return 0xFFFFFFFFLL;
result = 0LL;
while ( *a1 != a3 )
{
result = (unsigned int)(result + 1);
a1 += 10;
if ( a2 == (_DWORD)result )
return 0xFFFFFFFFLL;
}
return result;
}

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

if ( a2 <= 0 )
return 0xFFFFFFFFLL;
result = 0LL;
while ( *a1 != a3 )
{
result = (unsigned int)(result + 1);
a1 += 10;
if ( a2 == (_DWORD)result )
return 0xFFFFFFFFLL;
}
return result;
}

original_code:
int search_for_port(struct AppStruct *appstructs, int size, int port) {
  int index;
  int not_found = -1;
  for (index = 0; index < size; index++) {
    if (port == appstructs[index].port) {
      return index;
    }
  }
  return not_found;
}

variable_types: struct AppStruct { int port; char *user; char *app; char **args; int argnum; } *

variable_types: size int


decompiled_code:
__int64 __fastcall search_for_inet(__int64 a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, char *s1)
{
__int64 v10; // rbx
unsigned int v11; // ebp

if ( a2 <= 0 )
return (unsigned int)-1;
v10 = a1 + 8;
v11 = 0;
while ( *(_DWORD *)v10 != a8 || strcmp(s1, *(const char **)(v10 + 16)) )
{
++v11;
v10 += 48LL;
if ( a2 == v11 )
return (unsigned int)-1;
}
return v11;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8, int a9, char *s1)
{
__int64 v10; // rbx
unsigned int v11; // ebp

if ( a2 <= 0 )
return (unsigned int)-1;
v10 = a1 + 8;
v11 = 0;
while ( *(_DWORD *)v10 != a8 || strcmp(s1, *(const char **)(v10 + 16)) )
{
++v11;
v10 += 48LL;
if ( a2 == v11 )
return (unsigned int)-1;
}
return v11;
}

original_code:
int search_for_inet(struct InetStruct inetstruct,
                    struct InetStruct *inetstructs, int size) {
  int index;
  int not_found = -1;
  struct AppStruct source_app, target_app;
  source_app = inetstruct.app;
  for (index = 0; index < size; index++) {
    target_app = inetstructs[index].app;
    if (source_app.port == target_app.port &&
        strcmp(source_app.app, target_app.app) == 0) {
      return index;
    }
  }
  return not_found;
}

canonical_original_code:
int search_for_inet(struct InetStruct inetstruct, struct InetStruct *inetstructs, int size) {
  int index;
  int not_found = -1;
  struct AppStruct source_app; struct AppStruct target_app;
  source_app = inetstruct.app;
  for (index = 0; index < size; index++) {
    target_app = inetstructs[index].app;
    if (source_app.port == target_app.port &&
        strcmp(source_app.app, target_app.app) == 0) {
      return index;
    }
  }
  return not_found;
}

variable_types: struct InetStruct { int sock; struct AppStruct { int port; char *user; char *app; char **args; int argnum; } app; }

variable_types:
inetstructs
struct InetStruct *

user_defined_types: struct InetStruct { int sock; struct AppStruct app; }


original_code:
struct InetStruct *verify_inetstructs(struct InetStruct *new_inetstructs,
                                      struct InetStruct *old_inetstructs,
                                      int new_number_of_apps,
                                      int number_of_apps) {
  int new_index, old_index;
  struct InetStruct *inetstruct, *old;
  for (new_index = 0; new_index < new_number_of_apps; new_index++) {
    inetstruct = &(new_inetstructs[new_index]);
    old_index = search_for_inet(*inetstruct, old_inetstructs, number_of_apps);
    if (old_index != -1) {
      old = &(old_inetstructs[old_index]);
      if (compare_appstruct(inetstruct->app, old->app) > 0) {
        inetstruct->sock = old->sock;
        old->sock = -1;
        syslog(6, "Keep connected sock for app->%s, port->%d\n",
               inetstruct->app.app, inetstruct->app.port);
      }
    }
  }
  for (old_index = 0; old_index < number_of_apps; old_index++) {
    old = &(old_inetstructs[old_index]);
    if (old->sock != -1) {
      close(old->sock);
      old->sock = -1;
    }
  }
  return new_inetstructs;
}

canonical_original_code:
struct InetStruct *verify_inetstructs(struct InetStruct *new_inetstructs, struct InetStruct *old_inetstructs, int new_number_of_apps, int number_of_apps) {
  int new_index; int old_index;
  struct InetStruct *inetstruct; struct InetStruct *old;
  for (new_index = 0; new_index < new_number_of_apps; new_index++) {
    inetstruct = &(new_inetstructs[new_index]);
    old_index = search_for_inet(*inetstruct, old_inetstructs, number_of_apps);
    if (old_index != -1) {
      old = &(old_inetstructs[old_index]);
      if (compare_appstruct(inetstruct->app, old->app) > 0) {
        inetstruct->sock = old->sock;
        old->sock = -1;
        syslog(6, "Keep connected sock for app->%s, port->%d\n",
               inetstruct->app.app, inetstruct->app.port);
      }
    }
  }
  for (old_index = 0; old_index < number_of_apps; old_index++) {
    old = &(old_inetstructs[old_index]);
    if (old->sock != -1) {
      close(old->sock);
      old->sock = -1;
    }
  }
  return new_inetstructs;
}

variable_types: struct InetStruct { int sock; struct AppStruct { int port; char *user; char *app; char **args; int argnum; } app; } *


quit

decompiled_code:
void __fastcall __noreturn quit(int status)
{
SDL_Quit();
exit(status);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(int status)
{
SDL_Quit();
exit(status);
}

original_code:
void quit (int rc) {
  SDL_Quit();
  exit(rc);
}

canonical_original_code:
void quit(int rc) {
  SDL_Quit();
  exit(rc);
}

variable_types: rc int

return_type: void


decompiled_code:
__int64 __fastcall get_color_rgb(int a1, __int16 a2)
{
return (a1 << 17) & 0xFF0000 | (unsigned __int8)(a1 + a2) | (unsigned int)(unsigned __int16)(768 * a2);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, __int16 a2)
{
return (a1 << 17) & 0xFF0000 | (unsigned __int8)(a1 + a2) | (unsigned int)(unsigned __int16)(768 * a2);
}

variable_types: u int

variable_types: v int


decompiled_code:
__int64 make_table()
{
__int64 v0; // r12
int i; // ebp
__int64 j; // rbx
__int64 result; // rax

v0 = 0LL;
for ( i = 0; i != 256; ++i )
{
for ( j = 0LL; j != 256; ++j )
{
result = get_color_rgb(i, j);
pattern[j + v0] = result;
}
v0 += 256LL;
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // r12
int i; // ebp
__int64 j; // rbx
__int64 result; // rax

v0 = 0LL;
for ( i = 0; i != 256; ++i )
{
for ( j = 0LL; j != 256; ++j )
{
result = func1(i, j);
pattern[j + v0] = result;
}
v0 += 256LL;
}
return result;
}

original_code:
void make_table(){
  int i,j;
  for (i = 0; i < 256; i++) {
    for (j = 0; j < 256; j++) {
      pattern[i][j] = get_color_rgb(i, j);
    }
  }
}

canonical_original_code:
void make_table(){
  int i; int j;
  for (i = 0; i < 256; i++) {
    for (j = 0; j < 256; j++) {
      pattern[i][j] = get_color_rgb(i, j);
    }
  }
}

variable_types: i int

variable_types: j int


original_code:
void draw_screen(int* scr, int pitch, int w, int h, int half_width, int half_height) {
  float ticks = SDL_GetTicks();
  float t1 = ticks * 0.00010f;
  float t2 = ticks * 0.00030f;
  float zoom = 4.0f + 2.0f * sinf(t2);
  float angle = 2.0 * 3.1415f * sinf(t1);
  float zoom_cosf = zoom * cosf(angle);
  float zoom_sinf = zoom * sinf(angle);
  float half_w_zoom_cosf = half_width * zoom_cosf;
  float half_w_zoom_sinf = half_width * zoom_sinf;
  float half_h_zoom_cosf = half_height * zoom_cosf;
  float half_h_zoom_sinf = half_height * zoom_sinf;
  float u_offset = half_w_zoom_cosf + half_h_zoom_sinf;
  float v_offset = half_w_zoom_sinf + half_h_zoom_cosf;
  int y;
  int x;
  float u, v;
  float temp_u = 0 - zoom_sinf - u_offset;
  float temp_v = 0 - zoom_cosf - v_offset;
  int one_fourth_pitch = pitch / 4;
  int y_pitch = 0 - one_fourth_pitch;
  for ( y = 0 ; y < h ; y++ ) {
    y_pitch += one_fourth_pitch;
    temp_u += zoom_sinf;
    temp_v += zoom_cosf;
    u = temp_u - zoom_cosf;
    v = temp_v + zoom_sinf;
    for ( x = 0 ; x < w ; x++ ) {
      u += zoom_cosf;
      v -= zoom_sinf;
      scr[x + y_pitch] = pattern[(int)u & 255][(int)v & 255];
    }
  }
}

canonical_original_code:
void draw_screen(int *scr, int pitch, int w, int h, int half_width, int half_height) {
  float ticks = SDL_GetTicks();
  float t1 = ticks * 0.00010f;
  float t2 = ticks * 0.00030f;
  float zoom = 4.0f + 2.0f * sinf(t2);
  float angle = 2.0 * 3.1415f * sinf(t1);
  float zoom_cosf = zoom * cosf(angle);
  float zoom_sinf = zoom * sinf(angle);
  float half_w_zoom_cosf = half_width * zoom_cosf;
  float half_w_zoom_sinf = half_width * zoom_sinf;
  float half_h_zoom_cosf = half_height * zoom_cosf;
  float half_h_zoom_sinf = half_height * zoom_sinf;
  float u_offset = half_w_zoom_cosf + half_h_zoom_sinf;
  float v_offset = half_w_zoom_sinf + half_h_zoom_cosf;
  int y;
  int x;
  float u; float v;
  float temp_u = 0 - zoom_sinf - u_offset;
  float temp_v = 0 - zoom_cosf - v_offset;
  int one_fourth_pitch = pitch / 4;
  int y_pitch = 0 - one_fourth_pitch;
  for ( y = 0 ; y < h ; y++ ) {
    y_pitch += one_fourth_pitch;
    temp_u += zoom_sinf;
    temp_v += zoom_cosf;
    u = temp_u - zoom_cosf;
    v = temp_v + zoom_sinf;
    for ( x = 0 ; x < w ; x++ ) {
      u += zoom_cosf;
      v -= zoom_sinf;
      scr[x + y_pitch] = pattern[(int)u & 255][(int)v & 255];
    }
  }
}

variable_types:
scr
int *

variable_types: pitch int

variable_types: w int

variable_types: h int

variable_types: ticks float

variable_types: zoom float

variable_types: angle float

variable_types: y int

variable_types: x int

variable_types: u float

variable_types: v float


decompiled_code:
void mark_reload_config()
{
got_a_sighup = 1;
}

canonical_decompiled_code:
void func0()
{
got_a_sighup = 1;
}

original_code:
void mark_reload_config(int signum) {
  got_a_sighup = 1;
}

variable_types: signum int


decompiled_code:
void __fastcall folk_and_forget(int fd, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, __int64 a8, char *path, char **argv)
{
__pid_t v10; // eax
unsigned int v11; // ebx
int *v12; // rax
char *v13; // rax
__int64 i; // rbx
int *v15; // rax
char *v16; // rax

v10 = fork();
if ( v10 < 0 )
{
v12 = _errno_location();
v13 = strerror(*v12);
syslog(3, "fork: %s", v13);
close(fd);
}
else
{
v11 = v10;
if ( v10 <= 0 )
{
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
for ( i = 3LL; sysconf(4) >= i; ++i )
close(i);
close(fd);
lower_privilege(a8);
execv(path, argv);
v15 = _errno_location();
v16 = strerror(*v15);
syslog(3, "execv: %s", v16);
exit(1);
}
close(fd);
syslog(6, "Child with pid %d is forked.\n", v11);
}
}

canonical_decompiled_code:
void __fastcall func2(int fd, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, int a7, __int64 a8, char *path, char **argv)
{
__pid_t v10; // eax
unsigned int v11; // ebx
int *v12; // rax
char *v13; // rax
__int64 i; // rbx
int *v15; // rax
char *v16; // rax

v10 = fork();
if ( v10 < 0 )
{
v12 = _errno_location();
v13 = strerror(*v12);
syslog(3, "fork: %s", v13);
close(fd);
}
else
{
v11 = v10;
if ( v10 <= 0 )
{
dup2(fd, 0);
dup2(fd, 1);
dup2(fd, 2);
for ( i = 3LL; sysconf(4) >= i; ++i )
close(i);
close(fd);
lower_privilege(a8);
execv(path, argv);
v15 = _errno_location();
v16 = strerror(*v15);
syslog(3, "execv: %s", v16);
exit(1);
}
close(fd);
syslog(6, "Child with pid %d is forked.\n", v11);
}
}

original_code:
void folk_and_forget(int conn, struct AppStruct appstruct) {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    syslog(3, "fork: %s", strerror((*__errno_location ())));
    close(conn);
  } else if (pid > 0) {
    close(conn);
    syslog(6, "Child  with pid %d is forked.\n", pid);
  } else {
    int i;
    for (i = 0; i < 3; i++) {
      dup2(conn, i);
    }
    for (i = 3; i <= sysconf(_SC_OPEN_MAX); i++) {
      close(i);
    }
    close(conn);
    lower_privilege(appstruct.user);
    execv(appstruct.app, appstruct.args);
    syslog(3, "execv: %s", strerror((*__errno_location ())));
    _exit(1);
  }
}

canonical_original_code:
void folk_and_forget(int conn, struct AppStruct appstruct) {
  int pid;
  pid = fork();
  if (pid < 0) {
    syslog(3, "fork: %s", strerror((*__errno_location ())));
    close(conn);
  } else if (pid > 0) {
    close(conn);
    syslog(6, "Child  with pid %d is forked.\n", pid);
  } else {
    int i;
    for (i = 0; i < 3; i++) {
      dup2(conn, i);
    }
    for (i = 3; i <= sysconf(_SC_OPEN_MAX); i++) {
      close(i);
    }
    close(conn);
    lower_privilege(appstruct.user);
    execv(appstruct.app, appstruct.args);
    syslog(3, "execv: %s", strerror((*__errno_location ())));
    _exit(1);
  }
}

variable_types: conn int

variable_types: struct AppStruct { int port; char *user; char *app; char **args; int argnum; }

variable_types: pid int


decompiled_code:
__int64 __fastcall poll_listen_socks(struct pollfd a1, int a2)
{
void *v3; // rsp
_DWORD *v4; // rdx
__int16 *v5; // rax
__int16 *v6; // rsi
int *v7; // rbx
char *v8; // rax
__int64 result; // rax
int *v10; // rax
char *v11; // rax
struct pollfd v12; // r12
struct pollfd *v13; // rbx
int v14; // eax
__int64 v15; // rdx
__int64 v16; // rcx
__int64 v17; // r8
__int64 v18; // r9
struct pollfd v19; // [rsp+0h] [rbp-60h] BYREF
nfds_t nfds; // [rsp+8h] [rbp-58h]
socklen_t addr_len; // [rsp+1Ch] [rbp-44h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-40h] BYREF

v19 = a1;
nfds = a2;
v3 = alloca(8LL * a2);
if ( a2 > 0 )
{
v4 = (_DWORD *)a1;
v5 = &v19.events;
v6 = (__int16 *)&v19 + 4 * (unsigned int)(a2 - 1) + 6;
do
{
*((_DWORD *)v5 - 1) = *v4;
*v5 = 1;
v4 += 12;
v5 += 4;
}
while ( v5 != v6 );
}
do
{
while ( poll(&v19, nfds, -1) < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
syslog(3, "poll: %s", v8);
if ( *v7 == 4 )
{
result = (unsigned int)got_a_sighup;
if ( got_a_sighup )
return result;
}
}
v12 = v19;
v13 = (struct pollfd *)&v19.revents;
if ( a2 > 0 )
{
do
{
if ( (v13->fd & 1) != 0 )
{
addr_len = 16;
v14 = accept(*(int *)((char *)&v13[-1].fd + 2), &addr, &addr_len);
if ( v14 == -1 )
{
v10 = _errno_location();
v11 = strerror(*v10);
syslog(3, "accept: %s", v11);
}
else
{
folk_and_forget(
v14,
(__int64)&addr,
v15,
v16,
v17,
v18,
*(_QWORD *)(*(_QWORD *)&v12 + 8LL),
*(_QWORD *)(*(_QWORD *)&v12 + 16LL),
*(char **)(*(_QWORD *)&v12 + 24LL),
*(char ***)(*(_QWORD *)&v12 + 32LL));
}
}
*(_QWORD *)&v12 += 48LL;
++v13;
}
while ( v13 != (struct pollfd *)((char *)&v19 + 8 * (unsigned int)(a2 - 1) + 14) );
}
result = (unsigned int)got_a_sighup;
}
while ( !got_a_sighup );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(struct pollfd a1, int a2)
{
void *v3; // rsp
_DWORD *v4; // rdx
__int16 *v5; // rax
__int16 *v6; // rsi
int *v7; // rbx
char *v8; // rax
__int64 result; // rax
int *v10; // rax
char *v11; // rax
struct pollfd v12; // r12
struct pollfd *v13; // rbx
int v14; // eax
__int64 v15; // rdx
__int64 v16; // rcx
__int64 v17; // r8
__int64 v18; // r9
struct pollfd v19; // [rsp+0h] [rbp-60h] BYREF
nfds_t nfds; // [rsp+8h] [rbp-58h]
socklen_t addr_len; // [rsp+1Ch] [rbp-44h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-40h] BYREF

v19 = a1;
nfds = a2;
v3 = alloca(8LL * a2);
if ( a2 > 0 )
{
v4 = (_DWORD *)a1;
v5 = &v19.events;
v6 = (__int16 *)&v19 + 4 * (unsigned int)(a2 - 1) + 6;
do
{
*((_DWORD *)v5 - 1) = *v4;
*v5 = 1;
v4 += 12;
v5 += 4;
}
while ( v5 != v6 );
}
do
{
while ( poll(&v19, nfds, -1) < 0 )
{
v7 = _errno_location();
v8 = strerror(*v7);
syslog(3, "poll: %s", v8);
if ( *v7 == 4 )
{
result = (unsigned int)got_a_sighup;
if ( got_a_sighup )
return result;
}
}
v12 = v19;
v13 = (struct pollfd *)&v19.revents;
if ( a2 > 0 )
{
do
{
if ( (v13->fd & 1) != 0 )
{
addr_len = 16;
v14 = accept(*(int *)((char *)&v13[-1].fd + 2), &addr, &addr_len);
if ( v14 == -1 )
{
v10 = _errno_location();
v11 = strerror(*v10);
syslog(3, "accept: %s", v11);
}
else
{
func2(
v14,
(__int64)&addr,
v15,
v16,
v17,
v18,
*(_QWORD *)(*(_QWORD *)&v12 + 8LL),
*(_QWORD *)(*(_QWORD *)&v12 + 16LL),
*(char **)(*(_QWORD *)&v12 + 24LL),
*(char ***)(*(_QWORD *)&v12 + 32LL));
}
}
*(_QWORD *)&v12 += 48LL;
++v13;
}
while ( v13 != (struct pollfd *)((char *)&v19 + 8 * (unsigned int)(a2 - 1) + 14) );
}
result = (unsigned int)got_a_sighup;
}
while ( !got_a_sighup );
return result;
}

original_code:
void poll_listen_socks(struct InetStruct *inetstructs, int number_of_apps) {
  int index;
  int conn;
  struct pollfd fds[number_of_apps];
  for (index = 0; index < number_of_apps; index++) {
    fds[index].fd = inetstructs[index].sock;
    fds[index].events = 0x001;
  }
  while (1) {
    if ((poll(fds, number_of_apps, -1)) < 0) {
      syslog(3, "poll: %s", strerror((*__errno_location ())));
      if (((*__errno_location ()) == 4) && (got_a_sighup != 0)) {
        break;
      } else {
        continue;
      }
    }
    for (index = 0; index < number_of_apps; index++) {
      struct pollfd *fd = &(fds[index]);
      if (fd->revents & 0x001) {
        struct sockaddr_in peer;
        socklen_t size = sizeof(peer);
        if ((conn = accept(fd->fd, (struct sockaddr *)&peer, &size)) == -1) {
          syslog(3, "accept: %s", strerror((*__errno_location ())));
        } else {
          folk_and_forget(conn, (inetstructs[index]).app);
        }
      }
    }
    if (got_a_sighup != 0) break;
  }
}

canonical_original_code:
void poll_listen_socks(struct InetStruct *inetstructs, int number_of_apps) {
  int index;
  int conn;
  struct pollfd fds[-1];
  for (index = 0; index < number_of_apps; index++) {
    fds[index].fd = inetstructs[index].sock;
    fds[index].events = 0x001;
  }
  while (1) {
    if ((poll(fds, number_of_apps, -1)) < 0) {
      syslog(3, "poll: %s", strerror((*__errno_location ())));
      if (((*__errno_location ()) == 4) && (got_a_sighup != 0)) {
        break;
      } else {
        continue;
      }
    }
    for (index = 0; index < number_of_apps; index++) {
      struct pollfd *fd = &(fds[index]);
      if (fd->revents & 0x001) {
        struct sockaddr_in peer;
        unsigned int size = sizeof(peer);
        if ((conn = accept(fd->fd, (struct sockaddr *)&peer, &size)) == -1) {
          syslog(3, "accept: %s", strerror((*__errno_location ())));
        } else {
          folk_and_forget(conn, (inetstructs[index]).app);
        }
      }
    }
    if (got_a_sighup != 0) break;
  }
}

variable_types: struct InetStruct { int sock; struct AppStruct { int port; char *user; char *app; char **args; int argnum; } app; } *

variable_types: index int

variable_types: struct pollfd { int fd; short events; short revents; } [-1]

variable_types: struct pollfd { int fd; short events; short revents; } *

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

variable_types:
size
unsigned int

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

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


decompiled_code:
__int64 __fastcall generate_a_socket(unsigned int a1, const char *a2)
{
struct protoent *v2; // rax
int v3; // eax
unsigned int v4; // ebx
char *v5; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
struct sockaddr v13; // [rsp+0h] [rbp-38h] BYREF
int optval[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

optval[0] = 1;
v2 = getprotobyname("tcp");
v3 = socket(2, 1, v2->p_proto);
if ( v3 == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
syslog(3, "socket: %s", v8);
syslog(3, "Failed to create socket for %s on port %d\n", a2, a1);
exit(1);
}
v4 = v3;
setsockopt(v3, 1, 2, optval, 4u);
v13.sa_family = 2;
*(_WORD *)v13.sa_data = _bswap_16(a1);
*(_DWORD *)&v13.sa_data[2] = 0;
if ( bind(v4, &v13, 0x10u) )
{
v9 = _errno_location();
v10 = strerror(*v9);
syslog(3, "bind: %s", v10);
syslog(3, "Failed to bind %d to %s\n", a1, a2);
exit(1);
}
if ( listen(v4, 2) )
{
v11 = _errno_location();
v12 = strerror(*v11);
syslog(3, "listen: %s", v12);
exit(1);
}
v5 = inet_ntoa(*(struct in_addr *)&v13.sa_data[2]);
syslog(6, "App->%s is listening %s:%d on fd %d\n", a2, v5, a1, v4);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1, const char *a2)
{
struct protoent *v2; // rax
int v3; // eax
unsigned int v4; // ebx
char *v5; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
struct sockaddr v13; // [rsp+0h] [rbp-38h] BYREF
int optval[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

optval[0] = 1;
v2 = getprotobyname("tcp");
v3 = socket(2, 1, v2->p_proto);
if ( v3 == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
syslog(3, "socket: %s", v8);
syslog(3, "Failed to create socket for %s on port %d\n", a2, a1);
exit(1);
}
v4 = v3;
setsockopt(v3, 1, 2, optval, 4u);
v13.sa_family = 2;
*(_WORD *)v13.sa_data = func1(a1);
*(_DWORD *)&v13.sa_data[2] = 0;
if ( bind(v4, &v13, 0x10u) )
{
v9 = _errno_location();
v10 = strerror(*v9);
syslog(3, "bind: %s", v10);
syslog(3, "Failed to bind %d to %s\n", a1, a2);
exit(1);
}
if ( listen(v4, 2) )
{
v11 = _errno_location();
v12 = strerror(*v11);
syslog(3, "listen: %s", v12);
exit(1);
}
v5 = inet_ntoa(*(struct in_addr *)&v13.sa_data[2]);
syslog(6, "App->%s is listening %s:%d on fd %d\n", a2, v5, a1, v4);
return v4;
}

original_code:
int generate_a_socket(int port, char *app) {
  int sock;
  int optval = 1;
  struct sockaddr_in listen_addr;
  sock = socket(2, SOCK_STREAM, getprotobyname("tcp")->p_proto);
  if (sock == -1) {
    syslog(3, "socket: %s", strerror((*__errno_location ())));
    syslog(3, "Failed to create socket for %s on port %d\n", app, port);
    exit(1);
  }
  setsockopt(sock, 1, 2, (void*)&optval, sizeof(optval));
  listen_addr.sin_family = 2;
  listen_addr.sin_port = htons(port);
  listen_addr.sin_addr.s_addr = ((in_addr_t) 0x00000000);
  if (bind(sock, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) != 0) {
    syslog(3, "bind: %s", strerror((*__errno_location ())));
    syslog(3, "Failed to bind %d to %s\n", port, app);
    exit(1);
  }
  if (listen(sock, 2) != 0) {
    syslog(3, "listen: %s", strerror((*__errno_location ())));
    exit(1);
  }
  syslog(6, "App->%s is listening %s:%d on fd %d\n", app,
         inet_ntoa(listen_addr.sin_addr), port, sock);
  return sock;
}

canonical_original_code:
int generate_a_socket(int port, char *app) {
  int sock;
  int optval = 1;
  struct sockaddr_in listen_addr;
  sock = socket(2, SOCK_STREAM, getprotobyname("tcp")->p_proto);
  if (sock == -1) {
    syslog(3, "socket: %s", strerror((*__errno_location ())));
    syslog(3, "Failed to create socket for %s on port %d\n", app, port);
    exit(1);
  }
  setsockopt(sock, 1, 2, (void *)&optval, sizeof(optval));
  listen_addr.sin_family = 2;
  listen_addr.sin_port = htons(port);
  listen_addr.sin_addr.s_addr = ((unsigned int) 0x00000000);
  if (bind(sock, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) != 0) {
    syslog(3, "bind: %s", strerror((*__errno_location ())));
    syslog(3, "Failed to bind %d to %s\n", port, app);
    exit(1);
  }
  if (listen(sock, 2) != 0) {
    syslog(3, "listen: %s", strerror((*__errno_location ())));
    exit(1);
  }
  syslog(6, "App->%s is listening %s:%d on fd %d\n", app,
         inet_ntoa(listen_addr.sin_addr), port, sock);
  return sock;
}

variable_types: port int

variable_types:
app
char *

variable_types: sock int

variable_types: optval int


decompiled_code:
__int64 __fastcall generate_listen_socks(__int64 a1, int a2)
{
__int64 v2; // rbx

if ( a2 > 0 )
{
v2 = a1;
do
{
if ( *(_DWORD *)v2 == -1 )
*(_DWORD *)v2 = generate_a_socket(*(_DWORD *)(v2 + 8), *(const char **)(v2 + 24));
v2 += 48LL;
}
while ( v2 != a1 + 48LL * (unsigned int)(a2 - 1) + 48 );
}
return a1;
}

original_code:
struct InetStruct *generate_listen_socks(struct InetStruct *inetstructs,
                                         int number_of_apps) {
  int i;
  struct AppStruct *appstruct;
  for (i = 0; i < number_of_apps; i++) {
    if (inetstructs[i].sock == -1) {
      appstruct = &((inetstructs[i]).app);
      inetstructs[i].sock = generate_a_socket(appstruct->port, appstruct->app);
    }
  }
  return inetstructs;
}

variable_types:
inetstructs
struct InetStruct *

variable_types: i int

variable_types: struct AppStruct { int port; char *user; char *app; char **args; int argnum; } *

return_type: struct InetStruct { int sock; struct AppStruct { int port; char *user; char *app; char **args; int argnum; } app; } *


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
void *v3; // rbx
__int64 v4; // rbp
unsigned int v5; // [rsp+8h] [rbp-20h] BYREF
unsigned int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF
struct pollfd v7; // 0:rdi.8

if ( argc > 1 )
{
daemonize("simple_inetd", argv, envp);
atexit((void (*)(void))&clean_pidfile);
register_handler(17LL, &clean_children);
register_handler(15LL, &clean_and_exit);
register_handler(1LL, mark_reload_config);
v3 = 0LL;
while ( 1 )
{
if ( !v3 )
v3 = (void *)parse_config(argv[1], v6);
if ( got_a_sighup <= 0 )
{
v4 = (__int64)v3;
}
else
{
got_a_sighup = 0;
v4 = parse_config(argv[1], &v5);
verify_inetstructs(v4, v3, v5, v6[0]);
free(v3);
v6[0] = v5;
}
v3 = (void *)generate_listen_socks(v4, v6[0]);
v7 = (struct pollfd)v3;
poll_listen_socks(v7, v6[0]);
}
}
printf("Usage: %s config-file\n", *argv);
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
void *v3; // rbx
__int64 v4; // rbp
unsigned int v5; // [rsp+8h] [rbp-20h] BYREF
unsigned int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF
struct pollfd v7; // 0:rdi.8

if ( argc > 1 )
{
daemonize("simple_inetd", argv, envp);
atexit((void (*)(void))&clean_pidfile);
register_handler(17LL, &clean_children);
register_handler(15LL, &clean_and_exit);
register_handler(1LL, mark_reload_config);
v3 = 0LL;
while ( 1 )
{
if ( !v3 )
v3 = (void *)parse_config(argv[1], v6);
if ( got_a_sighup <= 0 )
{
v4 = (__int64)v3;
}
else
{
got_a_sighup = 0;
v4 = parse_config(argv[1], &v5);
verify_inetstructs(v4, v3, v5, v6[0]);
free(v3);
v6[0] = v5;
}
v3 = (void *)func5(v4, v6[0]);
v7 = (struct pollfd)v3;
func3(v7, v6[0]);
}
}
printf("Usage: %s config-file\n", *argv);
exit(1);
}

original_code:
int main(int argc, char * argv[]) {
  int number_of_apps;
  char *name = "simple_inetd";
  struct InetStruct *inetstructs = ((void *)0);
  if (argc < 2) {
    printf("Usage: %s config-file\n", argv[0]);
    exit(1);
  }
  daemonize(name);
  atexit(clean_pidfile);
  register_handler(17, clean_children);
  register_handler(15, clean_and_exit);
  register_handler(1, mark_reload_config);
  while (1) {
    if (inetstructs == ((void *)0)) {
      inetstructs = parse_config(argv[1], &number_of_apps);
    }
    if (got_a_sighup > 0) {
      got_a_sighup = 0;
      int new_number_of_apps;
      struct InetStruct *new_inetstructs;
      new_inetstructs = parse_config(argv[1], &new_number_of_apps);
      verify_inetstructs(new_inetstructs, inetstructs,
                         new_number_of_apps, number_of_apps);
      free(inetstructs);
      inetstructs = new_inetstructs;
      number_of_apps = new_number_of_apps;
    }
    inetstructs = generate_listen_socks(inetstructs, number_of_apps);
    poll_listen_socks(inetstructs, number_of_apps);
  }
}

canonical_original_code:
int main(int argc, char *argv[]) {
  int number_of_apps;
  char *name = "simple_inetd";
  struct InetStruct *inetstructs = ((void *)0);
  if (argc < 2) {
    printf("Usage: %s config-file\n", argv[0]);
    exit(1);
  }
  daemonize(name);
  atexit(clean_pidfile);
  register_handler(17, clean_children);
  register_handler(15, clean_and_exit);
  register_handler(1, mark_reload_config);
  while (1) {
    if (inetstructs == ((void *)0)) {
      inetstructs = parse_config(argv[1], &number_of_apps);
    }
    if (got_a_sighup > 0) {
      got_a_sighup = 0;
      int new_number_of_apps;
      struct InetStruct *new_inetstructs;
      new_inetstructs = parse_config(argv[1], &new_number_of_apps);
      verify_inetstructs(new_inetstructs, inetstructs,
                         new_number_of_apps, number_of_apps);
      free(inetstructs);
      inetstructs = new_inetstructs;
      number_of_apps = new_number_of_apps;
    }
    inetstructs = generate_listen_socks(inetstructs, number_of_apps);
    poll_listen_socks(inetstructs, number_of_apps);
  }
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
name
char *

user_defined_types: struct AppStruct { int port; char *user; char *app; char **args; int argnum; }


decompiled_code:
__int64 __fastcall parse_number(char *nptr, char *a2, int a3)
{
int *v4; // rbp
int v5; // eax
unsigned int v6; // edx
char *endptr; // [rsp+8h] [rbp-20h] BYREF

endptr = a2;
v4 = _errno_location();
*v4 = 0;
v5 = strtol(nptr, &endptr, 10);
if ( endptr == nptr )
return (unsigned int)-1;
v6 = v5;
if ( *v4 && !v5 )
return (unsigned int)-1;
if ( a3 < v5 || v5 <= 0 )
v6 = -2;
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *nptr, char *a2, int a3)
{
int *v4; // rbp
int v5; // eax
unsigned int v6; // edx
char *endptr; // [rsp+8h] [rbp-20h] BYREF

endptr = a2;
v4 = _errno_location();
*v4 = 0;
v5 = strtol(nptr, &endptr, 10);
if ( endptr == nptr )
return (unsigned int)-1;
v6 = v5;
if ( *v4 && !v5 )
return (unsigned int)-1;
if ( a3 < v5 || v5 <= 0 )
v6 = -2;
return v6;
}

original_code:
int parse_number(char *str, char *endptr, int max_limit) {
  int value;
  int base = 10;
  int no_number = -1;
  int out_of_range = -2;
  (*__errno_location ()) = 0;
  value = strtol(str, &endptr, base);
  if (((*__errno_location ()) == 34 && (value == 0x7fffffffffffffffL || value == (-0x7fffffffffffffffL - 1L)))) {
    return out_of_range;
  }
  if ((endptr == str) || ((*__errno_location ()) !=0 && value == 0)) {
    return no_number;
  }
  if (value > max_limit || value < 1) {
    return out_of_range;
  }
  return value;
}

variable_types:
str
char *

variable_types:
endptr
char *

variable_types: value int

variable_types: base int


decompiled_code:
_BOOL8 is_pidfile_existed()
{
return access("/var/run/simple_inetd.pid", 0) == 0;
}

canonical_decompiled_code:
_BOOL8 func1()
{
return access("/var/run/simple_inetd.pid", 0) == 0;
}

original_code:
int is_pidfile_existed() {
  if (access("/var/run/simple_inetd.pid", 0) == 0) {
    return 1;
  }
  return 0;
}


decompiled_code:
__int64 get_daemon_pid()
{
int v0; // eax
unsigned int v1; // ebx
int v2; // eax
int *v4; // rax
char *v5; // rax
char v6[264]; // [rsp+0h] [rbp-108h] BYREF

v0 = open("/var/run/simple_inetd.pid", 0);
v1 = v0;
if ( v0 == -1 )
{
v4 = _errno_location();
v5 = strerror(*v4);
syslog(3, "open:%s", v5);
}
else if ( read(v0, v6, 0x100uLL) <= 0 )
{
v1 = -1;
}
else
{
v2 = parse_number(v6, 0LL, 0x7FFFFFFF);
v1 = -1;
if ( v2 >= 0 )
v1 = v2;
}
return v1;
}

canonical_decompiled_code:
__int64 func2()
{
int v0; // eax
unsigned int v1; // ebx
int v2; // eax
int *v4; // rax
char *v5; // rax
char v6[264]; // [rsp+0h] [rbp-108h] BYREF

v0 = open("/var/run/simple_inetd.pid", 0);
v1 = v0;
if ( v0 == -1 )
{
v4 = _errno_location();
v5 = strerror(*v4);
syslog(3, "open:%s", v5);
}
else if ( read(v0, v6, 0x100uLL) <= 0 )
{
v1 = -1;
}
else
{
v2 = func0(v6, 0LL, 0x7FFFFFFF);
v1 = -1;
if ( v2 >= 0 )
v1 = v2;
}
return v1;
}

original_code:
int get_daemon_pid() {
  int pid_num, fd;
  char buffer[256];
  char *endptr;
  fd = open("/var/run/simple_inetd.pid", 00);
  if (fd == -1) {
    syslog(3, "open:%s", strerror((*__errno_location ())));
    return (-1);
  }
  if (read(fd, buffer, 256) > 0) {
    if ((pid_num = parse_number(buffer, endptr, 0x7fffffff)) < 0) {
      return (-1);
    }
    return pid_num;
  }
  return (-1);
}

canonical_original_code:
int get_daemon_pid() {
  int pid_num; int fd;
  char buffer[256];
  char *endptr;
  fd = open("/var/run/simple_inetd.pid", 00);
  if (fd == -1) {
    syslog(3, "open:%s", strerror((*__errno_location ())));
    return (-1);
  }
  if (read(fd, buffer, 256) > 0) {
    if ((pid_num = parse_number(buffer, endptr, 0x7fffffff)) < 0) {
      return (-1);
    }
    return pid_num;
  }
  return (-1);
}

variable_types: fd int

variable_types:
buffer
char [256]


decompiled_code:
__int64 is_daemon_running()
{
_BOOL4 v0; // edx
__int64 result; // rax
__pid_t v2; // edi
int v3; // edx

v0 = is_pidfile_existed();
result = 0xFFFFFFFFLL;
if ( v0 )
{
v2 = get_daemon_pid();
result = 0xFFFFFFFFLL;
if ( v2 >= 0 )
{
v3 = kill(v2, 0);
result = 1LL;
if ( v3 )
{
result = (unsigned int)*_errno_location();
if ( (_DWORD)result != 1 )
result = 0xFFFFFFFFLL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
_BOOL4 v0; // edx
__int64 result; // rax
__pid_t v2; // edi
int v3; // edx

v0 = func1();
result = 0xFFFFFFFFLL;
if ( v0 )
{
v2 = func2();
result = 0xFFFFFFFFLL;
if ( v2 >= 0 )
{
v3 = kill(v2, 0);
result = 1LL;
if ( v3 )
{
result = (unsigned int)*_errno_location();
if ( (_DWORD)result != 1 )
result = 0xFFFFFFFFLL;
}
}
}
return result;
}

original_code:
int is_daemon_running() {
  int pid;
  int kill_result;
  if (!is_pidfile_existed()) {
    return (-1);
  }
  pid = get_daemon_pid();
  if (pid < 0) return (-1);
  kill_result = kill(pid, 0);
  if (kill_result == 0) {
    return 1;
  } else if ((*__errno_location ()) == 1) {
    return 1;
  } else if ((*__errno_location ()) == 3) {
    return (-1);
  }
  return (-1);
}

variable_types: pid int


decompiled_code:
void clean_pidfile()
{
int *v0; // rax
char *v1; // rax

if ( (int)is_pidfile_existed() > 0 && unlink("/var/run/simple_inetd.pid") == -1 )
{
v0 = _errno_location();
v1 = strerror(*v0);
syslog(3, "unlink:%s", v1);
}
}

canonical_decompiled_code:
void func5()
{
int *v0; // rax
char *v1; // rax

if ( (int)func1() > 0 && unlink("/var/run/simple_inetd.pid") == -1 )
{
v0 = _errno_location();
v1 = strerror(*v0);
syslog(3, "unlink:%s", v1);
}
}

original_code:
void clean_pidfile(void) {
  if (is_pidfile_existed() > 0) {
    if (unlink("/var/run/simple_inetd.pid") == -1) {
      syslog(3, "unlink:%s", strerror((*__errno_location ())));
    }
  }
}

canonical_original_code:
void clean_pidfile() {
  if (is_pidfile_existed() > 0) {
    if (unlink("/var/run/simple_inetd.pid") == -1) {
      syslog(3, "unlink:%s", strerror((*__errno_location ())));
    }
  }
}


decompiled_code:
void __noreturn clean_and_exit()
{
clean_pidfile();
exit(0);
}

canonical_decompiled_code:
void __noreturn func6()
{
func5();
exit(0);
}

original_code:
void clean_and_exit(int signo) {
  clean_pidfile();
  _exit(0);
}

variable_types: signo int


decompiled_code:
unsigned int clean_children()
{
unsigned int result; // eax
int stat_loc; // [rsp+Ch] [rbp-Ch] BYREF

while ( 1 )
{
result = waitpid(-1, &stat_loc, 11);
if ( (int)result <= 0 )
break;
if ( (stat_loc & 0x7F) != 0 )
{
if ( __OFSUB__((stat_loc & 0x7F) + 1, 1) || (stat_loc & 0x7F) == 0 )
{
if ( (_BYTE)stat_loc == 127 )
{
syslog(6, "Child with pid %d stopped by signal %d\n", result, BYTE1(stat_loc));
}
else if ( stat_loc == 0xFFFF )
{
syslog(6, "Child with pid %d continued to execution\n", result);
}
}
else
{
syslog(6, "Child with pid %d killed by signal %d\n", result, stat_loc & 0x7F);
}
}
else
{
syslog(6, "Child with pid %d exited, status=%d\n", result, BYTE1(stat_loc));
}
}
return result;
}

canonical_decompiled_code:
unsigned int func7()
{
unsigned int result; // eax
int stat_loc; // [rsp+Ch] [rbp-Ch] BYREF

while ( 1 )
{
result = waitpid(-1, &stat_loc, 11);
if ( (int)result <= 0 )
break;
if ( (stat_loc & 0x7F) != 0 )
{
if ( __OFSUB__((stat_loc & 0x7F) + 1, 1) || (stat_loc & 0x7F) == 0 )
{
if ( (_BYTE)stat_loc == 127 )
{
syslog(6, "Child with pid %d stopped by signal %d\n", result, BYTE1(stat_loc));
}
else if ( stat_loc == 0xFFFF )
{
syslog(6, "Child with pid %d continued to execution\n", result);
}
}
else
{
syslog(6, "Child with pid %d killed by signal %d\n", result, stat_loc & 0x7F);
}
}
else
{
syslog(6, "Child with pid %d exited, status=%d\n", result, BYTE1(stat_loc));
}
}
return result;
}

original_code:
void clean_children(int signo) {
  int status;
  pid_t pid;
  while (1) {
    pid = waitpid(-1, &status, 1 | 2 | 8);
    if (pid > 0) {
      if ((((status) & 0x7f) == 0)) {
        syslog(6, "Child with pid %d exited, status=%d\n",
               pid, (((status) & 0xff00) >> 8));
      } else if ((((signed char) (((status) & 0x7f) + 1) >> 1) > 0)) {
        syslog(6, "Child with pid %d killed by signal %d\n",
               pid, ((status) & 0x7f));
      } else if ((((status) & 0xff) == 0x7f)) {
        syslog(6, "Child with pid %d stopped by signal %d\n",
               pid, (((status) & 0xff00) >> 8));
      } else if (((status) == 0xffff)) {
        syslog(6, "Child with pid %d continued to execution\n", pid);
      }
    } else {
      break;
    }
  }
}

canonical_original_code:
void clean_children(int signo) {
  int status;
  int pid;
  while (1) {
    pid = waitpid(-1, &status, 1 | 2 | 8);
    if (pid > 0) {
      if ((((status) & 0x7f) == 0)) {
        syslog(6, "Child with pid %d exited, status=%d\n",
               pid, (((status) & 0xff00) >> 8));
      } else if ((((char) (((status) & 0x7f) + 1) >> 1) > 0)) {
        syslog(6, "Child with pid %d killed by signal %d\n",
               pid, ((status) & 0x7f));
      } else if ((((status) & 0xff) == 0x7f)) {
        syslog(6, "Child with pid %d stopped by signal %d\n",
               pid, (((status) & 0xff00) >> 8));
      } else if (((status) == 0xffff)) {
        syslog(6, "Child with pid %d continued to execution\n", pid);
      }
    } else {
      break;
    }
  }
}

variable_types: status int


decompiled_code:
int __fastcall register_handler(int sig, void (*a2)(int))
{
int result; // eax
int *v3; // rax
char *v4; // rax
struct sigaction v5; // [rsp+0h] [rbp-B8h] BYREF

sigemptyset(&v5.sa_mask);
v5.sa_flags = 0;
v5.sa_handler = a2;
result = sigaction(sig, &v5, 0LL);
if ( result == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
syslog(3, "sigaction: %s", v4);
exit(1);
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(int sig, void (*a2)(int))
{
int result; // eax
int *v3; // rax
char *v4; // rax
struct sigaction v5; // [rsp+0h] [rbp-B8h] BYREF

sigemptyset(&v5.sa_mask);
v5.sa_flags = 0;
v5.sa_handler = a2;
result = sigaction(sig, &v5, 0LL);
if ( result == -1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
syslog(3, "sigaction: %s", v4);
exit(1);
}
return result;
}

original_code:
void register_handler(int signum, void (*handler)(int)) {
  struct sigaction sa;
  sigemptyset(&sa.sa_mask);
  sa.sa_flags = 0;
  sa.__sigaction_handler.sa_handler = handler;
  if (sigaction(signum, &sa, ((void *)0)) == -1) {
    syslog(3, "sigaction: %s", strerror((*__errno_location ())));
    exit(1);
  }
}

variable_types: signum int

variable_types:
handler
void (*)(int)

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

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

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


daemonize

decompiled_code:
void __fastcall daemonize(char *ident)
{
__pid_t v1; // eax
__pid_t v2; // eax
unsigned int v3; // eax
int v4; // eax
int v5; // ebp
int v6; // eax
int v7; // ebp
unsigned int v8; // eax

v1 = fork();
if ( v1 < 0 )
{
perror("fork:");
exit(1);
}
if ( v1 > 0 )
exit(0);
umask(0);
if ( setsid() == -1 )
{
perror("setsid:");
exit(1);
}
v2 = fork();
if ( v2 < 0 )
{
perror("fork:");
exit(1);
}
if ( v2 > 0 )
exit(0);
if ( (int)is_daemon_running() >= 0 )
{
v8 = get_daemon_pid();
printf("Daemon is already running with pid %d!\n", v8);
exit(1);
}
v3 = getpid();
if ( (int)write_daemon_pid(v3) < 0 )
{
printf("Cannot write pid to %s\n", "/var/run/simple_inetd.pid");
exit(1);
}
v4 = open("/dev/null", 0);
v5 = v4;
if ( v4 != -1 )
{
dup2(v4, 0);
close(v5);
}
v6 = open("/dev/null", 2);
v7 = v6;
if ( v6 != -1 )
{
dup2(v6, 1);
dup2(v7, 2);
close(v7);
}
if ( chdir("/") == -1 )
{
perror("chdir:");
exit(1);
}
openlog(ident, 9, 24);
syslog(6, "%s is daemonized successfully!", ident);
}

canonical_decompiled_code:
void __fastcall func9(char *ident)
{
__pid_t v1; // eax
__pid_t v2; // eax
unsigned int v3; // eax
int v4; // eax
int v5; // ebp
int v6; // eax
int v7; // ebp
unsigned int v8; // eax

v1 = fork();
if ( v1 < 0 )
{
perror("fork:");
exit(1);
}
if ( v1 > 0 )
exit(0);
umask(0);
if ( setsid() == -1 )
{
perror("setsid:");
exit(1);
}
v2 = fork();
if ( v2 < 0 )
{
perror("fork:");
exit(1);
}
if ( v2 > 0 )
exit(0);
if ( (int)func3() >= 0 )
{
v8 = func2();
printf("Daemon is already running with pid %d!\n", v8);
exit(1);
}
v3 = getpid();
if ( (int)func4(v3) < 0 )
{
printf("Cannot write pid to %s\n", "/var/run/simple_inetd.pid");
exit(1);
}
v4 = open("/dev/null", 0);
v5 = v4;
if ( v4 != -1 )
{
dup2(v4, 0);
close(v5);
}
v6 = open("/dev/null", 2);
v7 = v6;
if ( v6 != -1 )
{
dup2(v6, 1);
dup2(v7, 2);
close(v7);
}
if ( chdir("/") == -1 )
{
perror("chdir:");
exit(1);
}
openlog(ident, 9, 24);
syslog(6, "%s is daemonized successfully!", ident);
}

original_code:
void daemonize(char *name) {
  pid_t pid, sid;
  pid = fork();
  if (pid < 0) {
    perror("fork:");
    exit(1);
  } else if (pid > 0) {
    _exit(0);
  } else {
    int fd, daemon_pid;
    umask(0);
    sid = setsid();
    if (sid == -1) {
      perror("setsid:");
      exit(1);
    }
    pid = fork();
    if (pid < 0) {
      perror("fork:");
      exit(1);
    } else if (pid > 0) {
      exit(0);
    } else {
      if (is_daemon_running() < 0) {
        if (write_daemon_pid(getpid()) < 0) {
          printf("Cannot write pid to %s\n", "/var/run/simple_inetd.pid");
          exit(1);
        }
      } else {
        printf("Daemon is already running with pid %d!\n", get_daemon_pid());
        exit(1);
      }
      fd = open("/dev/null", 00);
      if (fd != -1) {
        dup2(fd, 0);
        close(fd);
      }
      fd = open("/dev/null", 02);
      if (fd != -1) {
        dup2(fd, 1);
        dup2(fd, 2);
        close(fd);
      }
      if (chdir("/") == -1) {
        perror("chdir:");
        exit(1);
      }
      openlog(name, 0x08 | 0x01, (3<<3));
      syslog(6, "%s is daemonized successfully!", name);
    }
  }
}

canonical_original_code:
void daemonize(char *name) {
  int pid; int sid;
  pid = fork();
  if (pid < 0) {
    perror("fork:");
    exit(1);
  } else if (pid > 0) {
    _exit(0);
  } else {
    int fd; int daemon_pid;
    umask(0);
    sid = setsid();
    if (sid == -1) {
      perror("setsid:");
      exit(1);
    }
    pid = fork();
    if (pid < 0) {
      perror("fork:");
      exit(1);
    } else if (pid > 0) {
      exit(0);
    } else {
      if (is_daemon_running() < 0) {
        if (write_daemon_pid(getpid()) < 0) {
          printf("Cannot write pid to %s\n", "/var/run/simple_inetd.pid");
          exit(1);
        }
      } else {
        printf("Daemon is already running with pid %d!\n", get_daemon_pid());
        exit(1);
      }
      fd = open("/dev/null", 00);
      if (fd != -1) {
        dup2(fd, 0);
        close(fd);
      }
      fd = open("/dev/null", 02);
      if (fd != -1) {
        dup2(fd, 1);
        dup2(fd, 2);
        close(fd);
      }
      if (chdir("/") == -1) {
        perror("chdir:");
        exit(1);
      }
      openlog(name, 0x08 | 0x01, (3<<3));
      syslog(6, "%s is daemonized successfully!", name);
    }
  }
}

variable_types: sid int


decompiled_code:
void __fastcall lower_privilege(const char *a1)
{
struct passwd *v1; // rax
struct passwd *v2; // rbx
int *v3; // rax
char *v4; // rax
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax

v1 = getpwnam(a1);
if ( !v1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
syslog(3, "getpwnam: %s", v4);
exit(1);
}
v2 = v1;
if ( initgroups(v1->pw_name, v1->pw_gid) == -1 )
{
v5 = _errno_location();
v6 = strerror(*v5);
syslog(3, "initgroups: %s", v6);
exit(1);
}
if ( setgid(v2->pw_gid) == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
syslog(3, "setgid: %s", v8);
exit(1);
}
syslog(6, "setgid to %d", v2->pw_gid);
if ( setuid(v2->pw_uid) == -1 )
{
v9 = _errno_location();
v10 = strerror(*v9);
syslog(3, "setuid: %s", v10);
exit(1);
}
syslog(6, "setuid to %s", a1);
}

canonical_decompiled_code:
void __fastcall func10(const char *a1)
{
struct passwd *v1; // rax
struct passwd *v2; // rbx
int *v3; // rax
char *v4; // rax
int *v5; // rax
char *v6; // rax
int *v7; // rax
char *v8; // rax
int *v9; // rax
char *v10; // rax

v1 = getpwnam(a1);
if ( !v1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
syslog(3, "getpwnam: %s", v4);
exit(1);
}
v2 = v1;
if ( initgroups(v1->pw_name, v1->pw_gid) == -1 )
{
v5 = _errno_location();
v6 = strerror(*v5);
syslog(3, "initgroups: %s", v6);
exit(1);
}
if ( setgid(v2->pw_gid) == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
syslog(3, "setgid: %s", v8);
exit(1);
}
syslog(6, "setgid to %d", v2->pw_gid);
if ( setuid(v2->pw_uid) == -1 )
{
v9 = _errno_location();
v10 = strerror(*v9);
syslog(3, "setuid: %s", v10);
exit(1);
}
syslog(6, "setuid to %s", a1);
}

original_code:
void lower_privilege(char *user) {
  struct passwd *pwdent;
  pwdent = getpwnam(user);
  if (pwdent == ((void *)0)) {
    syslog(3, "getpwnam: %s", strerror((*__errno_location ())));
    _exit(1);
  }
  if (initgroups(pwdent->pw_name, pwdent->pw_gid) == -1) {
    syslog(3, "initgroups: %s", strerror((*__errno_location ())));
    _exit(1);
  }
  if (setgid(pwdent->pw_gid) == -1) {
    syslog(3, "setgid: %s", strerror((*__errno_location ())));
    _exit(1);
  } else {
    syslog(6, "setgid to %d", pwdent->pw_gid);
  }
  if (setuid(pwdent->pw_uid) == -1) {
    syslog(3, "setuid: %s", strerror((*__errno_location ())));
    _exit(1);
  } else {
    syslog(6, "setuid to %s", user);
  }
}

variable_types:
user
char *

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


decompiled_code:
__int64 __fastcall select_even(_DWORD *a1)
{
return ((unsigned __int8)*a1 ^ 1) & 1;
}

canonical_decompiled_code:
__int64 __fastcall func7(_DWORD *a1)
{
return ((unsigned __int8)*a1 ^ 1) & 1;
}

original_code:
int select_even(const void *integer) {
  if ((*(int *)integer) % 2) {
    return 0;
  }
  return 1;
}

canonical_original_code:
int select_even(void *integer) {
  if ((*(int *)integer) % 2) {
    return 0;
  }
  return 1;
}

variable_types:
integer
void *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int64 i; // rbx
unsigned int *v5; // [rsp+0h] [rbp-48h] BYREF
unsigned __int64 v6; // [rsp+8h] [rbp-40h] BYREF
int src[14]; // [rsp+10h] [rbp-38h] BYREF

src[0] = 1;
src[1] = 3;
src[2] = 7;
src[3] = 6;
src[4] = 9;
src[5] = 13;
src[6] = 15;
src[7] = 20;
src[8] = 37;
src[9] = 40;
v5 = 0LL;
filter(src, 4uLL, 10LL, (void **)&v5, &v6, (unsigned int (__fastcall *)(void *))select_even);
printf("%d even numbers:\n", (unsigned int)v6);
if ( v6 )
{
for ( i = 0LL; i < v6; ++i )
printf("%d ", v5[i]);
}
putchar(10);
return 0;
}

original_code:
int main(int argc, char *argv[]) {
  int i;
  int input_array[10] = {1, 3, 7, 6, 9, 13, 15, 20, 37, 40};
  size_t output_number;
  int *output_array = ((void *)0);
  filter((void *)input_array,
         sizeof(int),
         10,
         (void **)&output_array,
         (void *)&output_number,
         select_even);
  printf("%d even numbers:\n", (int)output_number);
  for (i = 0; i < output_number; i++) {
    printf("%d ", output_array[i]);
  }
  printf("\n");
}

canonical_original_code:
int main(int argc, char *argv[]) {
  int i;
  int input_array[10] = {1, 3, 7, 6, 9, 13, 15, 20, 37, 40};
  unsigned long output_number;
  int *output_array = ((void *)0);
  filter((void *)input_array,
         sizeof(int),
         10,
         (void **)&output_array,
         (void *)&output_number,
         select_even);
  printf("%d even numbers:\n", (int)output_number);
  for (i = 0; i < output_number; i++) {
    printf("%d ", output_array[i]);
  }
  printf("\n");
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: i int


decompiled_code:
int __fastcall filter_files(char *s, __int64 a2, __int64 a3)
{
int v4; // eax
char *v5; // rax
int v6; // eax
int v7; // eax
unsigned int v8; // eax
int result; // eax
struct stat v10; // [rsp+0h] [rbp-A8h] BYREF

if ( *(int *)a3 > 0 && strcmp((const char *)(a2 + 19), *(const char **)(a3 + 8)) )
goto LABEL_16;
v4 = *(_DWORD *)(a3 + 16);
if ( v4 != -1 && v4 != *(unsigned __int8 *)(a2 + 18) )
goto LABEL_16;
if ( *(_DWORD *)(a3 + 20) == -1 && *(_QWORD *)(a3 + 24) == -1LL )
goto LABEL_14;
v5 = normalize_path(s, (char *)(a2 + 19));
if ( (unsigned int)lstat(v5, &v10) == -1 )
{
perror("lstat");
exit(1);
}
if ( ((v6 = *(_DWORD *)(a3 + 20), v6 == -1) || v6 == v10.st_uid)
&& ((v7 = *(_DWORD *)(a3 + 24), v7 == -1) || v7 == v10.st_gid)
&& ((v8 = *(_DWORD *)(a3 + 28), v8 == -1) || v10.st_size >= v8) )
{
LABEL_14:
result = printf("%s/%s\n", s, (const char *)(a2 + 19));
}
else
{
LABEL_16:
result = 0;
}
return result;
}

canonical_decompiled_code:
int __fastcall func9(char *s, __int64 a2, __int64 a3)
{
int v4; // eax
char *v5; // rax
int v6; // eax
int v7; // eax
unsigned int v8; // eax
int result; // eax
struct stat v10; // [rsp+0h] [rbp-A8h] BYREF

if ( *(int *)a3 > 0 && strcmp((const char *)(a2 + 19), *(const char **)(a3 + 8)) )
goto LABEL_16;
v4 = *(_DWORD *)(a3 + 16);
if ( v4 != -1 && v4 != *(unsigned __int8 *)(a2 + 18) )
goto LABEL_16;
if ( *(_DWORD *)(a3 + 20) == -1 && *(_QWORD *)(a3 + 24) == -1LL )
goto LABEL_14;
v5 = func8(s, (char *)(a2 + 19));
if ( (unsigned int)func14(v5, &v10) == -1 )
{
perror("lstat");
exit(1);
}
if ( ((v6 = *(_DWORD *)(a3 + 20), v6 == -1) || v6 == v10.st_uid)
&& ((v7 = *(_DWORD *)(a3 + 24), v7 == -1) || v7 == v10.st_gid)
&& ((v8 = *(_DWORD *)(a3 + 28), v8 == -1) || v10.st_size >= v8) )
{
LABEL_14:
result = printf("%s/%s\n", s, (const char *)(a2 + 19));
}
else
{
LABEL_16:
result = 0;
}
return result;
}

original_code:
int filter_files(const char *pathname,
           const struct dirent *entry,
           const FILTER *filter) {
  if (filter->name_len > 0) {
    if (strcmp(entry->d_name, filter->name) != 0) return 0;
  }
  if (filter->d_type != -1) {
    if (entry->d_type != filter->d_type) return 0;
  }
  if (filter->uid != -1 || filter->gid != -1 || filter->size != -1) {
    struct stat file_stat;
    if (lstat(normalize_path(pathname, entry->d_name), &file_stat) == -1) {
      perror("lstat");
      exit(1);
    }
    if ((filter->uid != -1) && (file_stat.st_uid != filter->uid)) return 0;
    if ((filter->gid != -1) && (file_stat.st_gid != filter->gid)) return 0;
    if ((filter->size != -1) && (file_stat.st_size < filter->size)) return 0;
  }
  printf("%s/%s\n", pathname, entry->d_name);
}

canonical_original_code:
int filter_files(char *pathname, struct dirent *entry, struct filters *filter) {
  if (filter->name_len > 0) {
    if (strcmp(entry->d_name, filter->name) != 0) return 0;
  }
  if (filter->d_type != -1) {
    if (entry->d_type != filter->d_type) return 0;
  }
  if (filter->uid != -1 || filter->gid != -1 || filter->size != -1) {
    struct stat file_stat;
    if (lstat(normalize_path(pathname, entry->d_name), &file_stat) == -1) {
      perror("lstat");
      exit(1);
    }
    if ((filter->uid != -1) && (file_stat.st_uid != filter->uid)) return 0;
    if ((filter->gid != -1) && (file_stat.st_gid != filter->gid)) return 0;
    if ((filter->size != -1) && (file_stat.st_size < filter->size)) return 0;
  }
  printf("%s/%s\n", pathname, entry->d_name);
}

variable_types:
pathname
char *

variable_types: struct dirent { unsigned long d_ino; long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; } *

variable_types: struct filters { int name_len; char *name; unsigned int d_type; unsigned int uid; unsigned int gid; unsigned int size; } *

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


decompiled_code:
void __fastcall print_dir_entries(char *a1, __int64 a2, int a3)
{
unsigned int v3; // ebp
int v4; // eax
unsigned __int64 v5; // rbx
unsigned __int64 v6; // r15
struct dirent *v7; // r8
char *v8; // r12
char *v9; // rax
struct dirent **namelist; // [rsp+8h] [rbp-40h] BYREF

if ( a3 )
{
v3 = a3 - (a3 > 0);
v4 = scandir(a1, &namelist, 0LL, alphasort);
if ( v4 < 0 )
{
fprintf(stderr, "scandir process %s error:", a1);
perror("");
}
else
{
if ( v4 )
{
v5 = 8LL * (v4 - 1);
v6 = 8 * (v4 - (unsigned __int64)(unsigned int)(v4 - 1)) - 16;
do
{
v7 = namelist[v5 / 8];
v8 = v7->d_name;
if ( strcmp(v7->d_name, ".") && strcmp(v7->d_name, "..") )
{
filter_files(a1, (__int64)namelist[v5 / 8], a2);
if ( namelist[v5 / 8]->d_type == 4 )
{
v9 = normalize_path(a1, v8);
print_dir_entries(v9, a2, v3);
}
free(namelist[v5 / 8]);
}
v5 -= 8LL;
}
while ( v5 != v6 );
}
free(namelist);
}
}
}

canonical_decompiled_code:
void __fastcall func10(char *a1, __int64 a2, int a3)
{
unsigned int v3; // ebp
int v4; // eax
unsigned __int64 v5; // rbx
unsigned __int64 v6; // r15
struct dirent *v7; // r8
char *v8; // r12
char *v9; // rax
struct dirent **namelist; // [rsp+8h] [rbp-40h] BYREF

if ( a3 )
{
v3 = a3 - (a3 > 0);
v4 = scandir(a1, &namelist, 0LL, alphasort);
if ( v4 < 0 )
{
fprintf(stderr, "scandir process %s error:", a1);
perror("");
}
else
{
if ( v4 )
{
v5 = 8LL * (v4 - 1);
v6 = 8 * (v4 - (unsigned __int64)(unsigned int)(v4 - 1)) - 16;
do
{
v7 = namelist[v5 / 8];
v8 = v7->d_name;
if ( strcmp(v7->d_name, ".") && strcmp(v7->d_name, "..") )
{
func9(a1, (__int64)namelist[v5 / 8], a2);
if ( namelist[v5 / 8]->d_type == 4 )
{
v9 = func8(a1, v8);
func10(v9, a2, v3);
}
free(namelist[v5 / 8]);
}
v5 -= 8LL;
}
while ( v5 != v6 );
}
free(namelist);
}
}
}

original_code:
void print_dir_entries(const char *pathname, FILTER *filter, int level) {
  struct dirent **namelist;
  int number_of_names;
  if (level == 0) return;
  if (level > 0) level--;
  number_of_names = scandir(pathname, &namelist, 0, alphasort);
  if (number_of_names < 0) {
    fprintf(stderr, "scandir process %s error:", pathname);
    perror("");
  }
  else {
    while (number_of_names--) {
      char *entry_name = namelist[number_of_names]->d_name;
      if (strcmp(entry_name, ".") == 0 || strcmp(entry_name, "..") == 0) {
        continue;
      }
      filter_files(pathname, namelist[number_of_names], filter);
      if (namelist[number_of_names]->d_type == DT_DIR) {
        print_dir_entries(normalize_path(pathname, entry_name), filter, level);
      }
      free(namelist[number_of_names]);
    }
    free(namelist);
  }
}

canonical_original_code:
void print_dir_entries(char *pathname, struct filters *filter, int level) {
  struct dirent **namelist;
  int number_of_names;
  if (level == 0) return;
  if (level > 0) level--;
  number_of_names = scandir(pathname, &namelist, 0, alphasort);
  if (number_of_names < 0) {
    fprintf(stderr, "scandir process %s error:", pathname);
    perror("");
  }
  else {
    while (number_of_names--) {
      char *entry_name = namelist[number_of_names]->d_name;
      if (strcmp(entry_name, ".") == 0 || strcmp(entry_name, "..") == 0) {
        continue;
      }
      filter_files(pathname, namelist[number_of_names], filter);
      if (namelist[number_of_names]->d_type == DT_DIR) {
        print_dir_entries(normalize_path(pathname, entry_name), filter, level);
      }
      free(namelist[number_of_names]);
    }
    free(namelist);
  }
}

variable_types: level int

variable_types: struct dirent { unsigned long d_ino; long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; } **

user_defined_types: struct filters { int name_len; char *name; unsigned int d_type; unsigned int uid; unsigned int gid; unsigned int size; }


decompiled_code:
void __noreturn poll_events()
{
struct pollfd *v0; // rbx
__int64 v1; // r12
__int64 v2; // r13
int v3; // ebp
int *v4; // rax
char *v5; // rax
__int16 v6; // ax
ssize_t v7; // r14
ssize_t v8; // r14
int *v9; // rax
char *v10; // rax
char src[312]; // [rsp+0h] [rbp-138h] BYREF

v0 = (struct pollfd *)calloc(1uLL, 8uLL);
v0->events = 1;
v1 = 0LL;
v2 = 0LL;
v3 = 0;
while ( 1 )
{
while ( poll(v0, 1uLL, -1) < 0 )
{
v4 = __errno_location();
v5 = strerror(*v4);
syslog(3, "poll: %s", v5);
}
v6 = v0->revents;
if ( (v6 & 0x18) != 0 )
break;
if ( (v6 & 1) != 0 )
{
v8 = read(v0->fd, &src[v3], 256 - v3);
syslog(6, "Read length: %lu\n", v8);
if ( v8 )
{
if ( v8 < 0 )
{
v9 = __errno_location();
v10 = strerror(*v9);
syslog(3, "read: %s", v10);
}
else
{
v3 += v8;
v2 += v8;
v0->events = (v3 != 256) + 4;
}
}
else
{
if ( v3 <= 0 )
{
syslog(6, "Connection closed! Total read:%lu, write:%lu\n", v2, v1);
exit(0);
}
v0->events = 4;
}
}
else if ( (v6 & 4) != 0 )
{
v7 = write(v0->fd, src, v3);
syslog(6, "Write length: %lu\n", v7);
if ( v3 <= v7 )
{
v1 += v7;
v0->events = 1;
v3 = 0;
}
else if ( v7 > 0 )
{
memmove(src, &src[v7], v3 - v7);
v3 -= v7;
v1 += v7;
v0->events = 5;
}
}
}
syslog(6, "Connection closed! Total read:%lu, write:%lu\n", v2, v1);
exit(0);
}

canonical_decompiled_code:
void __noreturn func7()
{
struct pollfd *v0; // rbx
__int64 v1; // r12
__int64 v2; // r13
int v3; // ebp
int *v4; // rax
char *v5; // rax
__int16 v6; // ax
ssize_t v7; // r14
ssize_t v8; // r14
int *v9; // rax
char *v10; // rax
char src[312]; // [rsp+0h] [rbp-138h] BYREF

v0 = (struct pollfd *)calloc(1uLL, 8uLL);
v0->events = 1;
v1 = 0LL;
v2 = 0LL;
v3 = 0;
while ( 1 )
{
while ( poll(v0, 1uLL, -1) < 0 )
{
v4 = __errno_location();
v5 = strerror(*v4);
syslog(3, "poll: %s", v5);
}
v6 = v0->revents;
if ( (v6 & 0x18) != 0 )
break;
if ( (v6 & 1) != 0 )
{
v8 = read(v0->fd, &src[v3], 256 - v3);
syslog(6, "Read length: %lu\n", v8);
if ( v8 )
{
if ( v8 < 0 )
{
v9 = __errno_location();
v10 = strerror(*v9);
syslog(3, "read: %s", v10);
}
else
{
v3 += v8;
v2 += v8;
v0->events = (v3 != 256) + 4;
}
}
else
{
if ( v3 <= 0 )
{
syslog(6, "Connection closed! Total read:%lu, write:%lu\n", v2, v1);
exit(0);
}
v0->events = 4;
}
}
else if ( (v6 & 4) != 0 )
{
v7 = write(v0->fd, src, v3);
syslog(6, "Write length: %lu\n", v7);
if ( v3 <= v7 )
{
v1 += v7;
v0->events = 1;
v3 = 0;
}
else if ( v7 > 0 )
{
memmove(src, &src[v7], v3 - v7);
v3 -= v7;
v1 += v7;
v0->events = 5;
}
}
}
syslog(6, "Connection closed! Total read:%lu, write:%lu\n", v2, v1);
exit(0);
}

original_code:
void poll_events() {
  int nfds = 1;
  int length = 0;
  struct pollfd *fds;
  char buffer[(1 << 8)];
  ssize_t read_len, write_len;
  ssize_t total_read_len, total_write_len;
  fds = calloc(nfds, sizeof(struct pollfd));
  fds[0].fd = 0;
  fds[0].events = 0x001;
  read_len = 0;
  write_len = 0;
  total_read_len = 0;
  total_write_len = 0;
  while (1) {
    if (poll(fds, nfds, -1) < 0) {
      syslog(3, "poll: %s", strerror((*__errno_location ())));
      continue;
    }
    if ((fds[0].revents & 0x010) || (fds[0].revents & 0x008)) {
      syslog(6, "Connection closed! Total read:%lu, write:%lu\n",
             total_read_len, total_write_len);
      exit(0);
    }
    if (fds[0].revents & 0x001) {
      int available_length = (1 << 8) - length;
      read_len = read(fds[0].fd, buffer+length, available_length);
      syslog(6, "Read length: %lu\n", read_len);
      if (read_len == 0) {
        if (length >0) {
          fds[0].events = 0x004;
        } else {
          syslog(6, "Connection closed! Total read:%lu, write:%lu\n",
                 total_read_len, total_write_len);
          exit(0);
        }
      } else if (read_len < 0) {
        syslog(3, "read: %s", strerror((*__errno_location ())));
      } else {
        length += read_len;
        total_read_len += read_len;
        if (length == (1 << 8)) {
          fds[0].events = 0x004;
        } else {
          fds[0].events = 0x001 | 0x004;
        }
      }
    } else if (fds[0].revents & 0x004) {
      write_len = write(fds[0].fd, buffer, length);
      syslog(6, "Write length: %lu\n", write_len);
      if (write_len < length) {
        if (write_len > 0) {
          memmove(buffer, buffer+write_len, length - write_len);
          length -= write_len;
          total_write_len += write_len;
          fds[0].events = 0x001 | 0x004;
        }
      } else {
        length = 0;
        total_write_len += write_len;
        fds[0].events = 0x001;
      }
    }
  }
}

canonical_original_code:
void poll_events() {
  int nfds = 1;
  int length = 0;
  struct pollfd *fds;
  char buffer[-1];
  long read_len; long write_len;
  long total_read_len; long total_write_len;
  fds = calloc(nfds, sizeof(struct pollfd));
  fds[0].fd = 0;
  fds[0].events = 0x001;
  read_len = 0;
  write_len = 0;
  total_read_len = 0;
  total_write_len = 0;
  while (1) {
    if (poll(fds, nfds, -1) < 0) {
      syslog(3, "poll: %s", strerror((*__errno_location ())));
      continue;
    }
    if ((fds[0].revents & 0x010) || (fds[0].revents & 0x008)) {
      syslog(6, "Connection closed! Total read:%lu, write:%lu\n",
             total_read_len, total_write_len);
      exit(0);
    }
    if (fds[0].revents & 0x001) {
      int available_length = (1 << 8) - length;
      read_len = read(fds[0].fd, buffer+length, available_length);
      syslog(6, "Read length: %lu\n", read_len);
      if (read_len == 0) {
        if (length >0) {
          fds[0].events = 0x004;
        } else {
          syslog(6, "Connection closed! Total read:%lu, write:%lu\n",
                 total_read_len, total_write_len);
          exit(0);
        }
      } else if (read_len < 0) {
        syslog(3, "read: %s", strerror((*__errno_location ())));
      } else {
        length += read_len;
        total_read_len += read_len;
        if (length == (1 << 8)) {
          fds[0].events = 0x004;
        } else {
          fds[0].events = 0x001 | 0x004;
        }
      }
    } else if (fds[0].revents & 0x004) {
      write_len = write(fds[0].fd, buffer, length);
      syslog(6, "Write length: %lu\n", write_len);
      if (write_len < length) {
        if (write_len > 0) {
          memmove(buffer, buffer+write_len, length - write_len);
          length -= write_len;
          total_write_len += write_len;
          fds[0].events = 0x001 | 0x004;
        }
      } else {
        length = 0;
        total_write_len += write_len;
        fds[0].events = 0x001;
      }
    }
  }
}

variable_types: nfds int

variable_types: length int

variable_types: struct pollfd { int fd; short events; short revents; } *

variable_types:
buffer
char [-1]


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__uid_t suid; // [rsp+4h] [rbp-14h] BYREF
__uid_t euid; // [rsp+8h] [rbp-10h] BYREF
__uid_t ruid[3]; // [rsp+Ch] [rbp-Ch] BYREF

openlog("echoserver", 9, 24);
if ( getresuid(ruid, &euid, &suid) == -1 )
syslog(3, "Failed to run getresuid\n");
syslog(6, "Running as real UID=%d, EUID=%d, SUID=%d\n", ruid[0], euid, suid);
syslog(6, "Running as real GID=%d, EGID=%d, SGID=%d\n", 0LL, 0LL, 0LL);
poll_events();
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__uid_t suid; // [rsp+4h] [rbp-14h] BYREF
__uid_t euid; // [rsp+8h] [rbp-10h] BYREF
__uid_t ruid[3]; // [rsp+Ch] [rbp-Ch] BYREF

openlog("echoserver", 9, 24);
if ( getresuid(ruid, &euid, &suid) == -1 )
syslog(3, "Failed to run getresuid\n");
syslog(6, "Running as real UID=%d, EUID=%d, SUID=%d\n", ruid[0], euid, suid);
syslog(6, "Running as real GID=%d, EGID=%d, SGID=%d\n", 0LL, 0LL, 0LL);
func7();
}

original_code:
int main(int argc, char *argv[]) {
  pid_t ruid, euid, suid;
  gid_t rgid, egid, sgid;
  openlog("echoserver", 0x08 | 0x01, (3<<3));
  if (getresuid(&ruid, &euid, &suid) == -1) {
  syslog(3, "Failed to run getresuid\n");
  }
  syslog(6, "Running as real UID=%d, EUID=%d, SUID=%d\n",ruid, euid, suid);
  syslog(6, "Running as real GID=%d, EGID=%d, SGID=%d\n",rgid, egid, sgid);
  poll_events();
}

canonical_original_code:
int main(int argc, char *argv[]) {
  int ruid; int euid; int suid;
  unsigned int rgid; unsigned int egid; unsigned int sgid;
  openlog("echoserver", 0x08 | 0x01, (3<<3));
  if (getresuid(&ruid, &euid, &suid) == -1) {
  syslog(3, "Failed to run getresuid\n");
  }
  syslog(6, "Running as real UID=%d, EUID=%d, SUID=%d\n",ruid, euid, suid);
  syslog(6, "Running as real GID=%d, EGID=%d, SGID=%d\n",rgid, egid, sgid);
  poll_events();
}

variable_types: ruid int

variable_types: euid int

variable_types: suid int

variable_types:
rgid
unsigned int

variable_types:
egid
unsigned int

variable_types:
sgid
unsigned int


original_code:
int apply_filters(const char *pathname, const struct dirent *entry,
                  const FILTER *filter) {
  if (filter->name_len > 0) {
    if (strcmp(entry->d_name, filter->name) != 0) return 0;
  }
  if (filter->d_type != DT_UNKNOWN) {
    if (entry->d_type != filter->d_type) return 0;
  }
  if (filter->uid != -1 || filter->gid != -1 || filter->size != -1) {
    struct stat file_stat;
    if (lstat(normalize_path(pathname, entry->d_name), &file_stat) == -1) {
      perror("lstat");
      return 0;
    }
    if ((filter->uid != -1) && (file_stat.st_uid != filter->uid)) return 0;
    if ((filter->gid != -1) && (file_stat.st_gid != filter->gid)) return 0;
    if ((filter->size != -1) && (file_stat.st_size < filter->size)) return 0;
  }
  printf("%s/%s\n", pathname, entry->d_name);
}

canonical_original_code:
int apply_filters(char *pathname, struct dirent *entry, struct filters *filter) {
  if (filter->name_len > 0) {
    if (strcmp(entry->d_name, filter->name) != 0) return 0;
  }
  if (filter->d_type != DT_UNKNOWN) {
    if (entry->d_type != filter->d_type) return 0;
  }
  if (filter->uid != -1 || filter->gid != -1 || filter->size != -1) {
    struct stat file_stat;
    if (lstat(normalize_path(pathname, entry->d_name), &file_stat) == -1) {
      perror("lstat");
      return 0;
    }
    if ((filter->uid != -1) && (file_stat.st_uid != filter->uid)) return 0;
    if ((filter->gid != -1) && (file_stat.st_gid != filter->gid)) return 0;
    if ((filter->size != -1) && (file_stat.st_size < filter->size)) return 0;
  }
  printf("%s/%s\n", pathname, entry->d_name);
}

variable_types: struct filters { int name_len; char *name; unsigned char d_type; unsigned int uid; unsigned int gid; long size; } *

user_defined_types: struct dirent { unsigned long d_ino; long d_off; unsigned short d_reclen; unsigned char d_type; char d_name[256]; }

user_defined_types: struct timespec { long tv_sec; long tv_nsec; }


decompiled_code:
void __fastcall print_dir_entries(char *a1, __int64 a2, int a3)
{
unsigned int v3; // ebp
int v4; // eax
unsigned __int64 v5; // rbx
unsigned __int64 v6; // r15
struct dirent *v7; // r8
char *v8; // r12
char *v9; // r12
struct dirent **namelist; // [rsp+8h] [rbp-40h] BYREF

if ( a3 )
{
v3 = a3 - (a3 > 0);
v4 = scandir(a1, &namelist, 0LL, alphasort);
if ( v4 < 0 )
{
fprintf(stderr, "scandir process %s error:", a1);
perror("");
}
else
{
if ( v4 )
{
v5 = 8LL * (v4 - 1);
v6 = 8 * (v4 - (unsigned __int64)(unsigned int)(v4 - 1)) - 16;
do
{
v7 = namelist[v5 / 8];
v8 = v7->d_name;
if ( strcmp(v7->d_name, ".") && strcmp(v7->d_name, "..") )
{
apply_filters(a1, (__int64)namelist[v5 / 8], a2);
if ( namelist[v5 / 8]->d_type == 4 )
{
v9 = normalize_path(a1, v8);
print_dir_entries(v9, a2, v3);
free(v9);
}
free(namelist[v5 / 8]);
}
v5 -= 8LL;
}
while ( v5 != v6 );
}
free(namelist);
}
}
}

canonical_decompiled_code:
void __fastcall func10(char *a1, __int64 a2, int a3)
{
unsigned int v3; // ebp
int v4; // eax
unsigned __int64 v5; // rbx
unsigned __int64 v6; // r15
struct dirent *v7; // r8
char *v8; // r12
char *v9; // r12
struct dirent **namelist; // [rsp+8h] [rbp-40h] BYREF

if ( a3 )
{
v3 = a3 - (a3 > 0);
v4 = scandir(a1, &namelist, 0LL, alphasort);
if ( v4 < 0 )
{
fprintf(stderr, "scandir process %s error:", a1);
perror("");
}
else
{
if ( v4 )
{
v5 = 8LL * (v4 - 1);
v6 = 8 * (v4 - (unsigned __int64)(unsigned int)(v4 - 1)) - 16;
do
{
v7 = namelist[v5 / 8];
v8 = v7->d_name;
if ( strcmp(v7->d_name, ".") && strcmp(v7->d_name, "..") )
{
func9(a1, (__int64)namelist[v5 / 8], a2);
if ( namelist[v5 / 8]->d_type == 4 )
{
v9 = func8(a1, v8);
func10(v9, a2, v3);
free(v9);
}
free(namelist[v5 / 8]);
}
v5 -= 8LL;
}
while ( v5 != v6 );
}
free(namelist);
}
}
}

original_code:
void print_dir_entries(const char *pathname, FILTER *filter, int level) {
  struct dirent **namelist;
  int number_of_names;
  if (level == 0) return;
  if (level > 0) level--;
  number_of_names = scandir(pathname, &namelist, 0, alphasort);
  if (number_of_names < 0) {
    fprintf(stderr, "scandir process %s error:", pathname);
    perror("");
  }
  else {
    while (number_of_names--) {
      char *entry_name = namelist[number_of_names]->d_name;
      if (strcmp(entry_name, ".") == 0 || strcmp(entry_name, "..") == 0) {
        continue;
      }
      apply_filters(pathname, namelist[number_of_names], filter);
      if (namelist[number_of_names]->d_type == DT_DIR) {
        char *sub_folder_path = normalize_path(pathname, entry_name);
        print_dir_entries(sub_folder_path, filter, level);
        free(sub_folder_path);
      }
      free(namelist[number_of_names]);
    }
    free(namelist);
  }
}

canonical_original_code:
void print_dir_entries(char *pathname, struct filters *filter, int level) {
  struct dirent **namelist;
  int number_of_names;
  if (level == 0) return;
  if (level > 0) level--;
  number_of_names = scandir(pathname, &namelist, 0, alphasort);
  if (number_of_names < 0) {
    fprintf(stderr, "scandir process %s error:", pathname);
    perror("");
  }
  else {
    while (number_of_names--) {
      char *entry_name = namelist[number_of_names]->d_name;
      if (strcmp(entry_name, ".") == 0 || strcmp(entry_name, "..") == 0) {
        continue;
      }
      apply_filters(pathname, namelist[number_of_names], filter);
      if (namelist[number_of_names]->d_type == DT_DIR) {
        char *sub_folder_path = normalize_path(pathname, entry_name);
        print_dir_entries(sub_folder_path, filter, level);
        free(sub_folder_path);
      }
      free(namelist[number_of_names]);
    }
    free(namelist);
  }
}

variable_types:
pathname
char *

user_defined_types: struct filters { int name_len; char *name; unsigned char d_type; unsigned int uid; unsigned int gid; long size; }


repo: bigjason/numb

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

lineptr = 0LL;
v3 = 1;
while ( getline(&lineptr, &v5, stdin) != -1 )
printf("%04u: %s", v3++, lineptr);
if ( lineptr )
free(lineptr);
return 0;
}

original_code:
int main()
{
    unsigned int position = 1;
    char *line = ((void *)0);
    size_t size;
    while (getline(&line, &size, stdin) != -1) {
        printf("%04u: %s", position++, line);
    }
    if (line) free(line);
    return 0;
}

canonical_original_code:
int main()
{
    unsigned int position = 1;
    char *line = ((void *)0);
    unsigned long size;
    while (getline(&line, &size, stdin) != -1) {
        printf("%04u: %s", position++, line);
    }
    if (line) free(line);
    return 0;
}

variable_types:
position
unsigned int

variable_types:
line
char *

variable_types:
size
unsigned long


repo: bijalthanawala/avl-tree

decompiled_code:
void __fastcall gnrclist_destroy(void *ptr)
{
_QWORD *v1; // rbx
_QWORD *v2; // rbp
void (*v3)(void); // rax

if ( ptr )
{
v1 = (_QWORD *)*((_QWORD *)ptr + 2);
if ( v1 )
{
while ( 1 )
{
v3 = (void (*)(void))*((_QWORD *)ptr + 1);
if ( v3 )
{
if ( *v1 )
v3();
}
v2 = (_QWORD *)v1[1];
free(v1);
if ( !v2 )
break;
v1 = v2;
}
}
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
_QWORD *v1; // rbx
_QWORD *v2; // rbp
void (*v3)(void); // rax

if ( ptr )
{
v1 = (_QWORD *)*((_QWORD *)ptr + 2);
if ( v1 )
{
while ( 1 )
{
v3 = (void (*)(void))*((_QWORD *)ptr + 1);
if ( v3 )
{
if ( *v1 )
v3();
}
v2 = (_QWORD *)v1[1];
free(v1);
if ( !v2 )
break;
v1 = v2;
}
}
free(ptr);
}
}

original_code:
void gnrclist_destroy(struct GNRCLIST *plist)
{
    struct LISTNODE *pnode = ((void *)0);
    struct LISTNODE *pnext = ((void *)0);
    if(plist) {
        pnode = plist->head;
        while(pnode) {
            if(plist->pfndel && pnode->payload) {
                plist->pfndel(pnode->payload);
            }
            pnext = pnode->next;
            free(pnode);
            pnode = pnext;
        }
        free(plist);
    }
}

variable_types: struct GNRCLIST { int count; void (*pfndel)(void *payload); struct LISTNODE { void *payload; struct LISTNODE *next; } *head; struct LISTNODE *tail; struct LISTNODE *iterptr; } *

user_defined_types: struct GNRCLIST { int count; void (*pfndel)(void *payload); struct LISTNODE *head; struct LISTNODE *tail; struct LISTNODE *iterptr; }


original_code:
void gnrclist_walk(struct GNRCLIST *plist,
                   PFN_LISTELEMENT_PROCESS pfnprocessnode,
                   void *pcontext)
{
   struct LISTNODE *pnode = ((void *)0);
   BOOL bcontinue = ((BOOL)1);
   if(!plist) {
    return;
   }
    pnode = plist->head;
    while(pnode && bcontinue) {
       if(pfnprocessnode) {
             bcontinue = (*pfnprocessnode)(pnode->payload,pcontext);
       }
       pnode = pnode->next;
    }
}

canonical_original_code:
void gnrclist_walk(struct GNRCLIST *plist, unsigned char (*pfnprocessnode)(void *payload, void *pcontext), void *pcontext)
{
   struct LISTNODE *pnode = ((void *)0);
   unsigned char bcontinue = ((BOOL)1);
   if(!plist) {
    return;
   }
    pnode = plist->head;
    while(pnode && bcontinue) {
       if(pfnprocessnode) {
             bcontinue = (*pfnprocessnode)(pnode->payload,pcontext);
       }
       pnode = pnode->next;
    }
}

variable_types: unsigned char (*)(void *payload, void *pcontext)

variable_types:
pcontext
void *

variable_types: struct LISTNODE { void *payload; struct LISTNODE *next; } *

variable_types:
bcontinue
unsigned char


decompiled_code:
__int64 __fastcall gnrclist_insert_front(__int64 a1, __int64 a2)
{
__int64 result; // rax
_QWORD *v3; // rax
__int64 v4; // rdx

result = 0LL;
if ( a1 )
{
v3 = gnrclist_genlistnode(a2);
if ( v3 )
{
++*(_DWORD *)a1;
v4 = *(_QWORD *)(a1 + 16);
if ( v4 )
{
v3[1] = v4;
*(_QWORD *)(a1 + 16) = v3;
}
else
{
*(_QWORD *)(a1 + 16) = v3;
*(_QWORD *)(a1 + 32) = v3;
*(_QWORD *)(a1 + 24) = v3;
}
result = 1LL;
}
else
{
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
__int64 result; // rax
_QWORD *v3; // rax
__int64 v4; // rdx

result = 0LL;
if ( a1 )
{
v3 = gnrclist_genlistnode(a2);
if ( v3 )
{
++*(_DWORD *)a1;
v4 = *(_QWORD *)(a1 + 16);
if ( v4 )
{
v3[1] = v4;
*(_QWORD *)(a1 + 16) = v3;
}
else
{
*(_QWORD *)(a1 + 16) = v3;
*(_QWORD *)(a1 + 32) = v3;
*(_QWORD *)(a1 + 24) = v3;
}
result = 1LL;
}
else
{
result = 0LL;
}
}
return result;
}

original_code:
BOOL gnrclist_insert_front(struct GNRCLIST *plist,void *payload)
{
    struct LISTNODE *pnewnode = ((void *)0);
    if(!plist) {
        return ((BOOL)0);
    }
    pnewnode = gnrclist_genlistnode(payload);
    if(!pnewnode) {
        return ((BOOL)0);
    }
    plist->count++;
    if(!plist->head) {
        plist->head = pnewnode;
        plist->iterptr = pnewnode;
        plist->tail = pnewnode;
        return ((BOOL)1);
    }
    pnewnode->next = plist->head;
    plist->head = pnewnode;
    return ((BOOL)1);
}

canonical_original_code:
unsigned char gnrclist_insert_front(struct GNRCLIST *plist, void *payload)
{
    struct LISTNODE *pnewnode = ((void *)0);
    if(!plist) {
        return ((unsigned char)0);
    }
    pnewnode = gnrclist_genlistnode(payload);
    if(!pnewnode) {
        return ((unsigned char)0);
    }
    plist->count++;
    if(!plist->head) {
        plist->head = pnewnode;
        plist->iterptr = pnewnode;
        plist->tail = pnewnode;
        return ((unsigned char)1);
    }
    pnewnode->next = plist->head;
    plist->head = pnewnode;
    return ((unsigned char)1);
}

variable_types:
payload
void *

return_type: unsigned char

user_defined_types: struct LISTNODE { void *payload; struct LISTNODE *next; }


decompiled_code:
__int64 __fastcall gnrclist_insert_back(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax

v2 = gnrclist_genlistnode(a2);
if ( !v2 )
return 0LL;
++*(_DWORD *)a1;
if ( *(_QWORD *)(a1 + 16) )
{
*(_QWORD *)(*(_QWORD *)(a1 + 24) + 8LL) = v2;
}
else
{
*(_QWORD *)(a1 + 16) = v2;
*(_QWORD *)(a1 + 32) = v2;
}
*(_QWORD *)(a1 + 24) = v2;
return 1LL;
}

original_code:
BOOL gnrclist_insert_back(struct GNRCLIST *plist,void *payload)
{
    struct LISTNODE *pnewnode = ((void *)0);
    pnewnode = gnrclist_genlistnode(payload);
    if(!pnewnode) {
        return ((BOOL)0);
    }
    plist->count++;
    if(!plist->head) {
        plist->head = pnewnode;
        plist->iterptr = pnewnode;
        plist->tail = pnewnode;
        return ((BOOL)1);
    }
    plist->tail->next = pnewnode;
    plist->tail = pnewnode;
    return ((BOOL)1);
}

canonical_original_code:
unsigned char gnrclist_insert_back(struct GNRCLIST *plist, void *payload)
{
    struct LISTNODE *pnewnode = ((void *)0);
    pnewnode = gnrclist_genlistnode(payload);
    if(!pnewnode) {
        return ((unsigned char)0);
    }
    plist->count++;
    if(!plist->head) {
        plist->head = pnewnode;
        plist->iterptr = pnewnode;
        plist->tail = pnewnode;
        return ((unsigned char)1);
    }
    plist->tail->next = pnewnode;
    plist->tail = pnewnode;
    return ((unsigned char)1);
}


decompiled_code:
__int64 __fastcall gnrclist_iter_getnext(__int64 a1)
{
__int64 *v1; // rax
__int64 v2; // rdx

if ( !a1 )
return 0LL;
v1 = *(__int64 **)(a1 + 32);
if ( !v1 )
return 0LL;
v2 = *v1;
*(_QWORD *)(a1 + 32) = v1[1];
return v2;
}

original_code:
void *gnrclist_iter_getnext(struct GNRCLIST *plist)
{
   void *curr_payload = ((void *)0);
   if(!plist) {
    return ((void *)0);
   }
   if(plist->iterptr) {
     curr_payload = plist->iterptr->payload;
     plist->iterptr = plist->iterptr->next;
   }
   return curr_payload;
}

return_type: void *


decompiled_code:
__int64 __fastcall gnrclist_iter_getfirst(__int64 a1)
{
if ( !a1 )
return 0LL;
*(_QWORD *)(a1 + 32) = *(_QWORD *)(a1 + 16);
return gnrclist_iter_getnext(a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
if ( !a1 )
return 0LL;
*(_QWORD *)(a1 + 32) = *(_QWORD *)(a1 + 16);
return func4(a1);
}

original_code:
void *gnrclist_iter_getfirst(struct GNRCLIST *plist)
{
    if(!plist) {
        return ((void *)0);
    }
    plist->iterptr = plist->head;
    return gnrclist_iter_getnext(plist);
}


original_code:
void gnrclist_delfirst(struct GNRCLIST *plist)
{
    struct LISTNODE *oldhead = ((void *)0);
    if(!plist || !plist->head) {
        return;
    }
   if(plist->pfndel && plist->head->payload) {
      (*plist->pfndel)(plist->head->payload);
   }
   oldhead = plist->head;
   plist->head = plist->head->next;
   free(oldhead);
}


repo: bill-lonely/sshscan

decompiled_code:
void __fastcall __noreturn thread_routine(void *a1)
{
unsigned int v2; // er12
pthread_mutex_t *v3; // rdi
_QWORD *v4; // r13
_DWORD *v5; // rax

pthread_setcancelstate(0, 0LL);
pthread_setcanceltype(1, 0LL);
v2 = pthread_self();
printf("starting thread 0X%x \n", v2);
while ( 1 )
{
pthread_mutex_lock((pthread_mutex_t *)pool + 1);
v5 = pool;
if ( !*(_QWORD *)pool )
break;
LABEL_2:
if ( v5[4] )
goto LABEL_11;
printf("thread 0x%x is starting to work", v2);
*((_DWORD *)a1 + 2) = 0;
v3 = (pthread_mutex_t *)pool;
v4 = *(_QWORD **)pool;
if ( !*(_QWORD *)pool )
_assert_fail("pool->work_queue_head != NULL", "thread_pool.c", 0x5Cu, "thread_routine");
if ( v4 == *((_QWORD **)pool + 1) )
{
*(_QWORD *)pool = 0LL;
*(&v3->__align + 1) = 0LL;
}
else
{
*(_QWORD *)pool = v4[2];
}
pthread_mutex_unlock(v3 + 1);
((void (__fastcall *)(_QWORD))*v4)(v4[1]);
free(v4);
}
while ( !v5[4] )
{
printf("thread 0x%x is waiting \n", v2);
*((_DWORD *)a1 + 2) = 2;
pthread_cond_wait((pthread_cond_t *)((char *)pool + 80), (pthread_mutex_t *)pool + 1);
v5 = pool;
if ( *(_QWORD *)pool )
goto LABEL_2;
}
LABEL_11:
printf("thread 0x%x will exit.\n", v2);
pthread_mutex_unlock((pthread_mutex_t *)pool + 1);
pthread_exit(0LL);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(void *a1)
{
unsigned int v2; // er12
pthread_mutex_t *v3; // rdi
_QWORD *v4; // r13
_DWORD *v5; // rax

pthread_setcancelstate(0, 0LL);
pthread_setcanceltype(1, 0LL);
v2 = pthread_self();
printf("starting thread 0X%x \n", v2);
while ( 1 )
{
pthread_mutex_lock((pthread_mutex_t *)pool + 1);
v5 = pool;
if ( !*(_QWORD *)pool )
break;
LABEL_2:
if ( v5[4] )
goto LABEL_11;
printf("thread 0x%x is starting to work", v2);
*((_DWORD *)a1 + 2) = 0;
v3 = (pthread_mutex_t *)pool;
v4 = *(_QWORD **)pool;
if ( !*(_QWORD *)pool )
_assert_fail("pool->work_queue_head != NULL", "thread_pool.c", 0x5Cu, "thread_routine");
if ( v4 == *((_QWORD **)pool + 1) )
{
*(_QWORD *)pool = 0LL;
*(&v3->__align + 1) = 0LL;
}
else
{
*(_QWORD *)pool = v4[2];
}
pthread_mutex_unlock(v3 + 1);
((void (__fastcall *)(_QWORD))*v4)(v4[1]);
free(v4);
}
while ( !v5[4] )
{
printf("thread 0x%x is waiting \n", v2);
*((_DWORD *)a1 + 2) = 2;
pthread_cond_wait((pthread_cond_t *)((char *)pool + 80), (pthread_mutex_t *)pool + 1);
v5 = pool;
if ( *(_QWORD *)pool )
goto LABEL_2;
}
LABEL_11:
printf("thread 0x%x will exit.\n", v2);
pthread_mutex_unlock((pthread_mutex_t *)pool + 1);
pthread_exit(0LL);
}

original_code:
void *thread_routine(void *arg)
{
    ThreadUnit *parg = (ThreadUnit *)arg;
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,((void *)0));
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,((void *)0));
    printf("starting thread 0X%x \n", (unsigned int)pthread_self());
    while(1)
    {
        pthread_mutex_lock(&(pool->queue_lock));
        while (pool->work_queue_head == ((void *)0) && !pool->shutdown)
        {
            printf("thread 0x%x is waiting \n", (unsigned int)pthread_self());
   parg->state = 2;
            pthread_cond_wait(&(pool->queue_ready), &(pool->queue_lock));
        }
        if (pool->shutdown)
        {
            printf("thread 0x%x will exit.\n", (unsigned int)pthread_self());
            pthread_mutex_unlock(&(pool->queue_lock));
            pthread_exit(((void *)0));
        }
        printf("thread 0x%x is starting to work", (unsigned int)pthread_self());
        parg->state = 0;
        ((pool->work_queue_head != ((void *)0)) ? (void) (0) : __assert_fail ("pool->work_queue_head != NULL", "thread_pool.c", 92, __extension__ __PRETTY_FUNCTION__));
        CThread_worker *worker;
        worker = pool->work_queue_head;
        if(pool->work_quequ_rear == pool->work_queue_head)
        {
            pool->work_quequ_rear = pool->work_queue_head = ((void *)0);
        }
        else
        {
            pool->work_queue_head = worker->next;
        }
        pthread_mutex_unlock(&(pool->queue_lock));
        (*(worker->process))(worker->arg);
        free(worker);
        worker = ((void *)0);
    }
    pthread_exit(((void *)0));
}

canonical_original_code:
void *thread_routine(void *arg)
{
    struct ThreadUnit *parg = (ThreadUnit *)arg;
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,((void *)0));
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,((void *)0));
    printf("starting thread 0X%x \n", (unsigned int)pthread_self());
    while(1)
    {
        pthread_mutex_lock(&(pool->queue_lock));
        while (pool->work_queue_head == ((void *)0) && !pool->shutdown)
        {
            printf("thread 0x%x is waiting \n", (unsigned int)pthread_self());
   parg->state = 2;
            pthread_cond_wait(&(pool->queue_ready), &(pool->queue_lock));
        }
        if (pool->shutdown)
        {
            printf("thread 0x%x will exit.\n", (unsigned int)pthread_self());
            pthread_mutex_unlock(&(pool->queue_lock));
            pthread_exit(((void *)0));
        }
        printf("thread 0x%x is starting to work", (unsigned int)pthread_self());
        parg->state = 0;
        ((pool->work_queue_head != ((void *)0)) ? (void) (0) : __assert_fail ("pool->work_queue_head != NULL", "thread_pool.c", 92, __extension__ __PRETTY_FUNCTION__));
        struct worker *worker;
        worker = pool->work_queue_head;
        if(pool->work_quequ_rear == pool->work_queue_head)
        {
            pool->work_quequ_rear = pool->work_queue_head = ((void *)0);
        }
        else
        {
            pool->work_queue_head = worker->next;
        }
        pthread_mutex_unlock(&(pool->queue_lock));
        (*(worker->process))(worker->arg);
        free(worker);
        worker = ((void *)0);
    }
    pthread_exit(((void *)0));
}

variable_types:
arg
void *

variable_types: struct ThreadUnit { unsigned long thread_id; int state; } *

variable_types: struct worker { void *(*process)(void *arg); void *arg; struct worker *next; } *


decompiled_code:
int __fastcall pool_init(int a1)
{
char *v1; // rax
char *v2; // rbx
void *v3; // rax
int result; // eax
__int64 v5; // rbx
_QWORD *v6; // rax
char *v7; // rbx

v1 = (char *)malloc(0x80uLL);
pool = v1;
if ( !v1 )
return -1;
v2 = v1;
v3 = malloc(16LL * a1);
*((_QWORD *)v2 + 3) = v3;
if ( v3 )
{
*((_DWORD *)v2 + 8) = a1;
*((_DWORD *)v2 + 4) = 0;
*((_QWORD *)v2 + 1) = 0LL;
*(_QWORD *)v2 = 0LL;
*((_DWORD *)v2 + 9) = 0;
if ( pthread_mutex_init((pthread_mutex_t *)v2 + 1, 0LL) )
{
free(v2);
result = -2;
}
else
{
result = pthread_cond_init((pthread_cond_t *)(v2 + 80), 0LL);
if ( result )
{
pthread_mutex_destroy((pthread_mutex_t *)v2 + 1);
result = -2;
}
else
{
v5 = 0LL;
if ( a1 > 0 )
{
while ( 1 )
{
v6 = pool;
*(_DWORD *)(*((_QWORD *)pool + 3) + v5 + 8) = 1;
result = pthread_create(
(pthread_t *)(v6[3] + v5),
0LL,
(void *(*)(void *))thread_routine,
(void *)(v6[3] + v5));
if ( result )
break;
v5 += 16LL;
if ( v5 == 16LL * (unsigned int)a1 )
return result;
}
v7 = (char *)pool;
pthread_mutex_destroy((pthread_mutex_t *)pool + 1);
pthread_cond_destroy((pthread_cond_t *)(v7 + 80));
free(*((void **)v7 + 3));
free(v7);
result = -3;
}
}
}
}
else
{
free(v2);
result = -1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(int a1)
{
char *v1; // rax
char *v2; // rbx
void *v3; // rax
int result; // eax
__int64 v5; // rbx
_QWORD *v6; // rax
char *v7; // rbx

v1 = (char *)malloc(0x80uLL);
pool = v1;
if ( !v1 )
return -1;
v2 = v1;
v3 = malloc(16LL * a1);
*((_QWORD *)v2 + 3) = v3;
if ( v3 )
{
*((_DWORD *)v2 + 8) = a1;
*((_DWORD *)v2 + 4) = 0;
*((_QWORD *)v2 + 1) = 0LL;
*(_QWORD *)v2 = 0LL;
*((_DWORD *)v2 + 9) = 0;
if ( pthread_mutex_init((pthread_mutex_t *)v2 + 1, 0LL) )
{
free(v2);
result = -2;
}
else
{
result = pthread_cond_init((pthread_cond_t *)(v2 + 80), 0LL);
if ( result )
{
pthread_mutex_destroy((pthread_mutex_t *)v2 + 1);
result = -2;
}
else
{
v5 = 0LL;
if ( a1 > 0 )
{
while ( 1 )
{
v6 = pool;
*(_DWORD *)(*((_QWORD *)pool + 3) + v5 + 8) = 1;
result = pthread_create(
(pthread_t *)(v6[3] + v5),
0LL,
(void *(*)(void *))thread_routine,
(void *)(v6[3] + v5));
if ( result )
break;
v5 += 16LL;
if ( v5 == 16LL * (unsigned int)a1 )
return result;
}
v7 = (char *)pool;
pthread_mutex_destroy((pthread_mutex_t *)pool + 1);
pthread_cond_destroy((pthread_cond_t *)(v7 + 80));
free(*((void **)v7 + 3));
free(v7);
result = -3;
}
}
}
}
else
{
free(v2);
result = -1;
}
return result;
}

original_code:
int pool_init(int max_thread_num)
{
    int i;
    pool = (CThread_pool *) malloc(sizeof(CThread_pool));
    if (pool == ((void *)0))
    {
        return -1;
    }
    pool->thread_unite_list = (ThreadUnit *)malloc(sizeof(ThreadUnit) * max_thread_num);
    if (pool->thread_unite_list == ((void *)0))
    {
        free(pool);
        return -1;
    }
    pool->max_thread_num = max_thread_num;
    pool->shutdown = 0;
    pool->work_quequ_rear = ((void *)0);
    pool->work_queue_head = ((void *)0);
    pool->cur_queue_size = 0;
    if (pthread_mutex_init(&(pool->queue_lock), ((void *)0)) != 0)
    {
        free(pool);
        return -2;
    }
    if (pthread_cond_init(&(pool->queue_ready), ((void *)0)) != 0)
    {
        pthread_mutex_destroy(&(pool->queue_lock));
        return -2;
    }
    for (i = 0; i < max_thread_num; ++i)
    {
 pool->thread_unite_list[i].state = 1;
        if (pthread_create(&(pool->thread_unite_list[i].thread_id), ((void *)0), thread_routine, &pool->thread_unite_list[i]) != 0)
        {
            pthread_mutex_destroy(&(pool->queue_lock));
            pthread_cond_destroy(&(pool->queue_ready));
            free(pool->thread_unite_list);
            free(pool);
            return -3;
        }
    }
    return 0;
}

canonical_original_code:
int pool_init(int max_thread_num)
{
    int i;
    pool = (struct CThread_pool *) malloc(sizeof(CThread_pool));
    if (pool == ((void *)0))
    {
        return -1;
    }
    pool->thread_unite_list = (struct ThreadUnit *)malloc(sizeof(ThreadUnit) * max_thread_num);
    if (pool->thread_unite_list == ((void *)0))
    {
        free(pool);
        return -1;
    }
    pool->max_thread_num = max_thread_num;
    pool->shutdown = 0;
    pool->work_quequ_rear = ((void *)0);
    pool->work_queue_head = ((void *)0);
    pool->cur_queue_size = 0;
    if (pthread_mutex_init(&(pool->queue_lock), ((void *)0)) != 0)
    {
        free(pool);
        return -2;
    }
    if (pthread_cond_init(&(pool->queue_ready), ((void *)0)) != 0)
    {
        pthread_mutex_destroy(&(pool->queue_lock));
        return -2;
    }
    for (i = 0; i < max_thread_num; ++i)
    {
 pool->thread_unite_list[i].state = 1;
        if (pthread_create(&(pool->thread_unite_list[i].thread_id), ((void *)0), thread_routine, &pool->thread_unite_list[i]) != 0)
        {
            pthread_mutex_destroy(&(pool->queue_lock));
            pthread_cond_destroy(&(pool->queue_ready));
            free(pool->thread_unite_list);
            free(pool);
            return -3;
        }
    }
    return 0;
}

variable_types: i int

user_defined_types: struct CThread_pool { struct worker *work_queue_head; struct worker *work_quequ_rear; int shutdown; struct ThreadUnit *thread_unite_list; int max_thread_num; int cur_queue_size; union pthread_mutex_t queue_lock; union pthread_cond_t queue_ready; }

user_defined_types: struct ThreadUnit { unsigned long thread_id; int state; }

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 worker { void *(*process)(void *arg); void *arg; struct worker *next; }

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