main

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

if ( argc > 1 )
{
v4 = 0LL;
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);
while ( 1 )
{
if ( !v4 )
v4 = (void *)parse_config(argv[1], &v8);
if ( got_a_sighup > 0 )
{
v5 = argv[1];
got_a_sighup = 0;
v6 = parse_config(v5, v9);
verify_inetstructs(v6, v4, v9[0], v8);
free(v4);
v7 = v9[0];
v8 = v9[0];
}
else
{
v7 = v8;
v6 = (__int64)v4;
}
v10 = (struct pollfd)generate_listen_socks(v6, v7);
v4 = (void *)v10;
poll_listen_socks(v10, v8);
}
}
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 *v4; // rbp
const char *v5; // rdi
__int64 v6; // r12
int v7; // esi
unsigned int v8; // [rsp+8h] [rbp-20h] BYREF
unsigned int v9[7]; // [rsp+Ch] [rbp-1Ch] BYREF
struct pollfd v10; // 0:rdi.8

if ( argc > 1 )
{
v4 = 0LL;
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);
while ( 1 )
{
if ( !v4 )
v4 = (void *)parse_config(argv[1], &v8);
if ( got_a_sighup > 0 )
{
v5 = argv[1];
got_a_sighup = 0;
v6 = parse_config(v5, v9);
verify_inetstructs(v6, v4, v9[0], v8);
free(v4);
v7 = v9[0];
v8 = v9[0];
}
else
{
v7 = v8;
v6 = (__int64)v4;
}
v10 = (struct pollfd)func5(v6, v7);
v4 = (void *)v10;
func3(v10, v8);
}
}
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 *


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

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

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

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

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

return_type: 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; // er12
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
{
v1 = -1;
v2 = parse_number(v6, 0LL, 0x7FFFFFFF);
if ( v2 >= 0 )
v1 = v2;
}
return v1;
}

canonical_decompiled_code:
__int64 func2()
{
int v0; // eax
unsigned int v1; // er12
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
{
v1 = -1;
v2 = func0(v6, 0LL, 0x7FFFFFFF);
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()
{
__int64 result; // rax
__pid_t v1; // edi
int v2; // er8

if ( !is_pidfile_existed()
|| (v1 = get_daemon_pid(), v1 < 0)
|| (v2 = kill(v1, 0), result = 1LL, v2) && (result = (unsigned int)*_errno_location(), (_DWORD)result != 1) )
{
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 result; // rax
__pid_t v1; // edi
int v2; // er8

if ( !func1()
|| (v1 = func2(), v1 < 0)
|| (v2 = kill(v1, 0), result = 1LL, v2) && (result = (unsigned int)*_errno_location(), (_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 ())));
    }
  }
}

return_type: void


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 ( (char)((stat_loc & 0x7F) + 1) > 1 )
{
syslog(6, "Child with pid %d killed by signal %d\n", result, stat_loc & 0x7F);
}
else 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 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 ( (char)((stat_loc & 0x7F) + 1) > 1 )
{
syslog(6, "Child with pid %d killed by signal %d\n", result, stat_loc & 0x7F);
}
else 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 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 )
goto LABEL_16;
if ( v1 )
exit(0);
umask(0);
if ( setsid() == -1 )
{
perror("setsid:");
exit(1);
}
v2 = fork();
if ( v2 < 0 )
{
LABEL_16:
perror("fork:");
exit(1);
}
if ( v2 )
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 )
goto LABEL_16;
if ( v1 )
exit(0);
umask(0);
if ( setsid() == -1 )
{
perror("setsid:");
exit(1);
}
v2 = fork();
if ( v2 < 0 )
{
LABEL_16:
perror("fork:");
exit(1);
}
if ( v2 )
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; // rdx
int *v5; // rax
char *v6; // rdx
int *v7; // rax
char *v8; // rdx
int *v9; // rax
char *v10; // rdx

v1 = getpwnam(a1);
if ( !v1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
syslog(3, "getpwnam: %s", v4);
goto LABEL_7;
}
v2 = v1;
if ( initgroups(v1->pw_name, v1->pw_gid) == -1 )
{
v9 = _errno_location();
v10 = strerror(*v9);
syslog(3, "initgroups: %s", v10);
LABEL_7:
exit(1);
}
if ( setgid(v2->pw_gid) == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
syslog(3, "setgid: %s", v8);
goto LABEL_7;
}
syslog(6, "setgid to %d", v2->pw_gid);
if ( setuid(v2->pw_uid) == -1 )
{
v5 = _errno_location();
v6 = strerror(*v5);
syslog(3, "setuid: %s", v6);
goto LABEL_7;
}
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; // rdx
int *v5; // rax
char *v6; // rdx
int *v7; // rax
char *v8; // rdx
int *v9; // rax
char *v10; // rdx

v1 = getpwnam(a1);
if ( !v1 )
{
v3 = _errno_location();
v4 = strerror(*v3);
syslog(3, "getpwnam: %s", v4);
goto LABEL_7;
}
v2 = v1;
if ( initgroups(v1->pw_name, v1->pw_gid) == -1 )
{
v9 = _errno_location();
v10 = strerror(*v9);
syslog(3, "initgroups: %s", v10);
LABEL_7:
exit(1);
}
if ( setgid(v2->pw_gid) == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
syslog(3, "setgid: %s", v8);
goto LABEL_7;
}
syslog(6, "setgid to %d", v2->pw_gid);
if ( setuid(v2->pw_uid) == -1 )
{
v5 = _errno_location();
v6 = strerror(*v5);
syslog(3, "setuid: %s", v6);
goto LABEL_7;
}
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:
_BOOL8 __fastcall select_even(_DWORD *a1)
{
return (*a1 & 1) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func7(_DWORD *a1)
{
return (*a1 & 1) == 0;
}

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

canonical_name: main

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

src[0] = 0x300000001LL;
src[1] = 0x600000007LL;
src[2] = 0xD00000009LL;
src[3] = 0x140000000FLL;
src[4] = 0x2800000025LL;
filter(src, 4uLL);
printf("%d even numbers:\n", (unsigned int)v6);
if ( v6 )
{
for ( i = 0LL; i < v6; ++i )
{
v4 = *(unsigned int *)(4 * i);
printf("%d ", v4);
}
}
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(void *src, __int64 a2, __int64 a3)
{
int v4; // eax
int result; // eax
char *v6; // r12
char *v7; // rax
int v8; // eax
int v9; // eax
__off_t v10; // rax
struct stat v11; // [rsp+0h] [rbp-A8h] BYREF

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

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

if ( *(int *)a3 > 0 && strcmp((const char *)(a2 + 19), *(const char **)(a3 + 8)) )
goto LABEL_5;
v4 = *(_DWORD *)(a3 + 16);
if ( v4 != -1 && v4 != *(unsigned __int8 *)(a2 + 18) )
goto LABEL_5;
v6 = (char *)(a2 + 19);
if ( *(_DWORD *)(a3 + 20) == -1 && *(_QWORD *)(a3 + 24) == -1LL )
goto LABEL_15;
v7 = func8(src, v6);
if ( (unsigned int)func14(v7, &v11) == -1 )
{
perror("lstat");
exit(1);
}
if ( ((v8 = *(_DWORD *)(a3 + 20), v8 == -1) || v8 == v11.st_uid)
&& ((v9 = *(_DWORD *)(a3 + 24), v9 == -1) || v9 == v11.st_gid)
&& ((v10 = *(unsigned int *)(a3 + 28), (_DWORD)v10 == -1) || v11.st_size >= v10) )
{
LABEL_15:
result = printf("%s/%s\n", (const char *)src, v6);
}
else
{
LABEL_5:
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 v5; // ebp
int v6; // eax
struct dirent **v7; // rdi
int v8; // edx
unsigned __int64 v9; // rbx
unsigned __int64 v10; // r14
char *v11; // rsi
struct dirent *v12; // rdi
char *v13; // rax
struct dirent **namelist; // [rsp+8h] [rbp-40h] BYREF

if ( a3 )
{
v5 = a3 - (a3 > 0);
v6 = scandir(a1, &namelist, 0LL, alphasort);
if ( v6 < 0 )
{
fprintf(stderr, "scandir process %s error:", a1);
perror("");
}
else
{
v7 = namelist;
v8 = v6 - 1;
if ( v6 )
{
v9 = 8LL * v8;
v10 = 8 * (v6 - (unsigned __int64)(unsigned int)v8) - 16;
do
{
v11 = (char *)v7[v9 / 8];
if ( (v11[19] != 46 || v11[20]) && (v11[19] != 46 || v11[20] != 46 || v11[21]) )
{
filter_files(a1, (__int64)v11, a2);
v12 = namelist[v9 / 8];
if ( v12->d_type == 4 )
{
v13 = normalize_path(a1, v11 + 19);
print_dir_entries(v13, a2, v5);
v12 = namelist[v9 / 8];
}
free(v12);
v7 = namelist;
}
v9 -= 8LL;
}
while ( v10 != v9 );
}
free(v7);
}
}
}

canonical_decompiled_code:
void __fastcall func10(char *a1, __int64 a2, int a3)
{
unsigned int v5; // ebp
int v6; // eax
struct dirent **v7; // rdi
int v8; // edx
unsigned __int64 v9; // rbx
unsigned __int64 v10; // r14
char *v11; // rsi
struct dirent *v12; // rdi
char *v13; // rax
struct dirent **namelist; // [rsp+8h] [rbp-40h] BYREF

if ( a3 )
{
v5 = a3 - (a3 > 0);
v6 = scandir(a1, &namelist, 0LL, alphasort);
if ( v6 < 0 )
{
fprintf(stderr, "scandir process %s error:", a1);
perror("");
}
else
{
v7 = namelist;
v8 = v6 - 1;
if ( v6 )
{
v9 = 8LL * v8;
v10 = 8 * (v6 - (unsigned __int64)(unsigned int)v8) - 16;
do
{
v11 = (char *)v7[v9 / 8];
if ( (v11[19] != 46 || v11[20]) && (v11[19] != 46 || v11[20] != 46 || v11[21]) )
{
func9(a1, (__int64)v11, a2);
v12 = namelist[v9 / 8];
if ( v12->d_type == 4 )
{
v13 = func8(a1, v11 + 19);
func10(v13, a2, v5);
v12 = namelist[v9 / 8];
}
free(v12);
v7 = namelist;
}
v9 -= 8LL;
}
while ( v10 != v9 );
}
free(v7);
}
}
}

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()
{
__int64 v0; // r13
__int64 v1; // r12
int v2; // ebp
struct pollfd *v3; // rax
struct pollfd *v4; // rbx
__int16 v5; // ax
__int64 v6; // r15
ssize_t v7; // r14
ssize_t v8; // r14
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
char src[312]; // [rsp+0h] [rbp-138h] BYREF

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

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

v0 = 0LL;
v1 = 0LL;
v2 = 0;
v3 = (struct pollfd *)calloc(1uLL, 8uLL);
v3->events = 1;
v4 = v3;
while ( 1 )
{
while ( poll(v4, 1uLL, -1) < 0 )
{
v9 = __errno_location();
v10 = strerror(*v9);
syslog(3, "poll: %s", v10);
}
v5 = v4->revents;
if ( (v5 & 0x18) != 0 )
break;
if ( (v5 & 1) != 0 )
{
v8 = read(v4->fd, &src[v2], 256 - v2);
syslog(6, "Read length: %lu\n", v8);
if ( v8 )
{
if ( v8 < 0 )
{
v11 = __errno_location();
v12 = strerror(*v11);
syslog(3, "read: %s", v12);
}
else
{
v2 += v8;
v0 += v8;
v4->events = (v2 != 256) + 4;
}
}
else
{
if ( v2 <= 0 )
break;
v4->events = 4;
}
}
else if ( (v5 & 4) != 0 )
{
v6 = v2;
v7 = write(v4->fd, src, v2);
syslog(6, "Write length: %lu\n", v7);
if ( v2 <= v7 )
{
v1 += v7;
v2 = 0;
v4->events = 1;
}
else if ( v7 > 0 )
{
v2 -= v7;
v1 += v7;
memmove(src, &src[v7], v6 - v7);
v4->events = 5;
}
}
}
syslog(6, "Connection closed! Total read:%lu, write:%lu\n", v0, 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 ruid; // [rsp+4h] [rbp-14h] BYREF
__uid_t euid; // [rsp+8h] [rbp-10h] BYREF
__uid_t suid[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, euid, suid[0]);
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 ruid; // [rsp+4h] [rbp-14h] BYREF
__uid_t euid; // [rsp+8h] [rbp-10h] BYREF
__uid_t suid[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, euid, suid[0]);
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 v5; // ebp
int v6; // eax
struct dirent **v7; // rdi
int v8; // edx
unsigned __int64 v9; // rbx
unsigned __int64 v10; // r14
char *v11; // rsi
struct dirent *v12; // rdi
char *v13; // r15
struct dirent **namelist; // [rsp+8h] [rbp-40h] BYREF

if ( a3 )
{
v5 = a3 - (a3 > 0);
v6 = scandir(a1, &namelist, 0LL, alphasort);
if ( v6 < 0 )
{
fprintf(stderr, "scandir process %s error:", a1);
perror("");
}
else
{
v7 = namelist;
v8 = v6 - 1;
if ( v6 )
{
v9 = 8LL * v8;
v10 = 8 * (v6 - (unsigned __int64)(unsigned int)v8) - 16;
do
{
v11 = (char *)v7[v9 / 8];
if ( (v11[19] != 46 || v11[20]) && (v11[19] != 46 || v11[20] != 46 || v11[21]) )
{
apply_filters(a1, (__int64)v11, a2);
v12 = namelist[v9 / 8];
if ( v12->d_type == 4 )
{
v13 = normalize_path(a1, v11 + 19);
print_dir_entries(v13, a2, v5);
free(v13);
v12 = namelist[v9 / 8];
}
free(v12);
v7 = namelist;
}
v9 -= 8LL;
}
while ( v10 != v9 );
}
free(v7);
}
}
}

canonical_decompiled_code:
void __fastcall func10(char *a1, __int64 a2, int a3)
{
unsigned int v5; // ebp
int v6; // eax
struct dirent **v7; // rdi
int v8; // edx
unsigned __int64 v9; // rbx
unsigned __int64 v10; // r14
char *v11; // rsi
struct dirent *v12; // rdi
char *v13; // r15
struct dirent **namelist; // [rsp+8h] [rbp-40h] BYREF

if ( a3 )
{
v5 = a3 - (a3 > 0);
v6 = scandir(a1, &namelist, 0LL, alphasort);
if ( v6 < 0 )
{
fprintf(stderr, "scandir process %s error:", a1);
perror("");
}
else
{
v7 = namelist;
v8 = v6 - 1;
if ( v6 )
{
v9 = 8LL * v8;
v10 = 8 * (v6 - (unsigned __int64)(unsigned int)v8) - 16;
do
{
v11 = (char *)v7[v9 / 8];
if ( (v11[19] != 46 || v11[20]) && (v11[19] != 46 || v11[20] != 46 || v11[21]) )
{
func9(a1, (__int64)v11, a2);
v12 = namelist[v9 / 8];
if ( v12->d_type == 4 )
{
v13 = func8(a1, v11 + 19);
func10(v13, a2, v5);
free(v13);
v12 = namelist[v9 / 8];
}
free(v12);
v7 = namelist;
}
v9 -= 8LL;
}
while ( v10 != v9 );
}
free(v7);
}
}
}

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; // ebp
__int64 v4; // rsi
void *ptr; // [rsp+0h] [rbp-48h] BYREF
size_t n[8]; // [rsp+8h] [rbp-40h] BYREF

v3 = 1;
for ( ptr = 0LL; getline((char **)&ptr, n, stdin) != -1; printf("%04u: %s", v4, (const char *)ptr) )
v4 = v3++;
if ( ptr )
free(ptr);
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; // rbp
void (*v2)(void); // rax
_QWORD *v3; // rbx

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

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

if ( ptr )
{
v1 = (_QWORD *)*((_QWORD *)ptr + 2);
if ( v1 )
{
while ( 1 )
{
v2 = (void (*)(void))*((_QWORD *)ptr + 1);
if ( v2 )
{
if ( *v1 )
v2();
}
v3 = (_QWORD *)v1[1];
free(v1);
if ( !v3 )
break;
v1 = v3;
}
}
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)
{
_QWORD *v2; // rax
__int64 v3; // rdx

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

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

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
_DWORD *v3; // rax
char *v4; // rdi
pthread_mutex_t *v5; // rdi
_QWORD *v6; // r13

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);
v3 = pool;
if ( !*(_QWORD *)pool )
break;
LABEL_7:
if ( v3[4] )
goto LABEL_6;
printf("thread 0x%x is starting to work", v2);
v5 = (pthread_mutex_t *)pool;
*((_DWORD *)a1 + 2) = 0;
v6 = (_QWORD *)v5->__align;
if ( !v5->__align )
_assert_fail("pool->work_queue_head != NULL", "thread_pool.c", 0x5Cu, "thread_routine");
if ( v6 == *((_QWORD **)&v5->__align + 1) )
{
v5->__align = 0LL;
*(&v5->__align + 1) = 0LL;
}
else
{
v5->__align = v6[2];
}
pthread_mutex_unlock(v5 + 1);
((void (__fastcall *)(_QWORD))*v6)(v6[1]);
free(v6);
}
while ( !v3[4] )
{
printf("thread 0x%x is waiting \n", v2);
v4 = (char *)pool;
*((_DWORD *)a1 + 2) = 2;
pthread_cond_wait((pthread_cond_t *)(v4 + 80), (pthread_mutex_t *)v4 + 1);
v3 = pool;
if ( *(_QWORD *)pool )
goto LABEL_7;
}
LABEL_6:
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
_DWORD *v3; // rax
char *v4; // rdi
pthread_mutex_t *v5; // rdi
_QWORD *v6; // r13

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);
v3 = pool;
if ( !*(_QWORD *)pool )
break;
LABEL_7:
if ( v3[4] )
goto LABEL_6;
printf("thread 0x%x is starting to work", v2);
v5 = (pthread_mutex_t *)pool;
*((_DWORD *)a1 + 2) = 0;
v6 = (_QWORD *)v5->__align;
if ( !v5->__align )
_assert_fail("pool->work_queue_head != NULL", "thread_pool.c", 0x5Cu, "thread_routine");
if ( v6 == *((_QWORD **)&v5->__align + 1) )
{
v5->__align = 0LL;
*(&v5->__align + 1) = 0LL;
}
else
{
v5->__align = v6[2];
}
pthread_mutex_unlock(v5 + 1);
((void (__fastcall *)(_QWORD))*v6)(v6[1]);
free(v6);
}
while ( !v3[4] )
{
printf("thread 0x%x is waiting \n", v2);
v4 = (char *)pool;
*((_DWORD *)a1 + 2) = 2;
pthread_cond_wait((pthread_cond_t *)(v4 + 80), (pthread_mutex_t *)v4 + 1);
v3 = pool;
if ( *(_QWORD *)pool )
goto LABEL_7;
}
LABEL_6:
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:
__int64 __fastcall pool_init(int a1)
{
char *v1; // rax
char *v2; // rbp
void *v3; // rax
unsigned int v4; // er13
__int64 v5; // rbx
__int64 v6; // r12
__int64 v7; // rdi
char *v8; // rbp

v1 = (char *)malloc(0x80uLL);
pool = v1;
if ( !v1 )
return (unsigned int)-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) )
{
v4 = -2;
free(v2);
}
else
{
v4 = pthread_cond_init((pthread_cond_t *)(v2 + 80), 0LL);
if ( v4 )
{
v4 = -2;
pthread_mutex_destroy((pthread_mutex_t *)v2 + 1);
}
else if ( a1 > 0 )
{
v5 = 0LL;
v6 = 16LL * (unsigned int)(a1 - 1);
while ( 1 )
{
v7 = v5 + *((_QWORD *)v2 + 3);
*(_DWORD *)(v7 + 8) = 1;
if ( pthread_create((pthread_t *)v7, 0LL, (void *(*)(void *))thread_routine, (void *)v7) )
break;
if ( v5 == v6 )
return v4;
v2 = (char *)pool;
v5 += 16LL;
}
v8 = (char *)pool;
v4 = -3;
pthread_mutex_destroy((pthread_mutex_t *)pool + 1);
pthread_cond_destroy((pthread_cond_t *)(v8 + 80));
free(*((void **)v8 + 3));
free(v8);
}
}
}
else
{
v4 = -1;
free(v2);
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
char *v1; // rax
char *v2; // rbp
void *v3; // rax
unsigned int v4; // er13
__int64 v5; // rbx
__int64 v6; // r12
__int64 v7; // rdi
char *v8; // rbp

v1 = (char *)malloc(0x80uLL);
pool = v1;
if ( !v1 )
return (unsigned int)-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) )
{
v4 = -2;
free(v2);
}
else
{
v4 = pthread_cond_init((pthread_cond_t *)(v2 + 80), 0LL);
if ( v4 )
{
v4 = -2;
pthread_mutex_destroy((pthread_mutex_t *)v2 + 1);
}
else if ( a1 > 0 )
{
v5 = 0LL;
v6 = 16LL * (unsigned int)(a1 - 1);
while ( 1 )
{
v7 = v5 + *((_QWORD *)v2 + 3);
*(_DWORD *)(v7 + 8) = 1;
if ( pthread_create((pthread_t *)v7, 0LL, (void *(*)(void *))thread_routine, (void *)v7) )
break;
if ( v5 == v6 )
return v4;
v2 = (char *)pool;
v5 += 16LL;
}
v8 = (char *)pool;
v4 = -3;
pthread_mutex_destroy((pthread_mutex_t *)pool + 1);
pthread_cond_destroy((pthread_cond_t *)(v8 + 80));
free(*((void **)v8 + 3));
free(v8);
}
}
}
else
{
v4 = -1;
free(v2);
}
return v4;
}

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


decompiled_code:
__int64 __fastcall pool_add_worker(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
_QWORD *v3; // rbx
pthread_mutex_t *v4; // rdi
__int64 v5; // rax
bool v6; // zf

v2 = malloc(0x18uLL);
if ( !v2 )
return 0xFFFFFFFFLL;
*v2 = a1;
v3 = v2;
v2[1] = a2;
v2[2] = 0LL;
pthread_mutex_lock((pthread_mutex_t *)pool + 1);
v4 = (pthread_mutex_t *)pool;
v5 = *((_QWORD *)pool + 1);
if ( v5 )
{
v6 = *(_QWORD *)pool == 0LL;
*(_QWORD *)(v5 + 16) = v3;
*(&v4->__align + 1) = (__int64)v3;
if ( v6 )
_assert_fail("pool->work_queue_head != NULL", "thread_pool.c", 0x8Bu, "pool_add_worker");
}
else
{
*((_QWORD *)pool + 1) = v3;
v4->__align = (__int64)v3;
}
pthread_mutex_unlock(v4 + 1);
pthread_cond_signal((pthread_cond_t *)((char *)pool + 80));
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
_QWORD *v3; // rbx
pthread_mutex_t *v4; // rdi
__int64 v5; // rax
bool v6; // zf

v2 = malloc(0x18uLL);
if ( !v2 )
return 0xFFFFFFFFLL;
*v2 = a1;
v3 = v2;
v2[1] = a2;
v2[2] = 0LL;
pthread_mutex_lock((pthread_mutex_t *)pool + 1);
v4 = (pthread_mutex_t *)pool;
v5 = *((_QWORD *)pool + 1);
if ( v5 )
{
v6 = *(_QWORD *)pool == 0LL;
*(_QWORD *)(v5 + 16) = v3;
*(&v4->__align + 1) = (__int64)v3;
if ( v6 )
_assert_fail("pool->work_queue_head != NULL", "thread_pool.c", 0x8Bu, "pool_add_worker");
}
else
{
*((_QWORD *)pool + 1) = v3;
v4->__align = (__int64)v3;
}
pthread_mutex_unlock(v4 + 1);
pthread_cond_signal((pthread_cond_t *)((char *)pool + 80));
return 0LL;
}

original_code:
int pool_add_worker(void *(*process)(void *arg), void *arg )
{
    CThread_worker *newworker = (CThread_worker *)malloc(sizeof(CThread_worker));
    if (newworker == ((void *)0))
    {
        return -1;
    }
    newworker->process = process;
    newworker->arg = arg;
    newworker->next = ((void *)0);
    pthread_mutex_lock(&(pool->queue_lock));
    if (pool->work_quequ_rear == ((void *)0))
    {
        pool->work_queue_head = pool->work_quequ_rear = newworker;
    }
    else
    {
        pool->work_quequ_rear->next = newworker;
        pool->work_quequ_rear = newworker;
    }
    ((pool->work_queue_head != ((void *)0)) ? (void) (0) : __assert_fail ("pool->work_queue_head != NULL", "thread_pool.c", 139, __extension__ __PRETTY_FUNCTION__));
    pthread_mutex_unlock(&(pool->queue_lock));
    pthread_cond_signal(&(pool->queue_ready));
    return 0;
}

canonical_original_code:
int pool_add_worker(void *(*process)(void *arg), void *arg)
{
    struct worker *newworker = (CThread_worker *)malloc(sizeof(CThread_worker));
    if (newworker == ((void *)0))
    {
        return -1;
    }
    newworker->process = process;
    newworker->arg = arg;
    newworker->next = ((void *)0);
    pthread_mutex_lock(&(pool->queue_lock));
    if (pool->work_quequ_rear == ((void *)0))
    {
        pool->work_queue_head = pool->work_quequ_rear = newworker;
    }
    else
    {
        pool->work_quequ_rear->next = newworker;
        pool->work_quequ_rear = newworker;
    }
    ((pool->work_queue_head != ((void *)0)) ? (void) (0) : __assert_fail ("pool->work_queue_head != NULL", "thread_pool.c", 139, __extension__ __PRETTY_FUNCTION__));
    pthread_mutex_unlock(&(pool->queue_lock));
    pthread_cond_signal(&(pool->queue_ready));
    return 0;
}

variable_types:
process
void *(*)(void *arg)


decompiled_code:
__int64 pool_destroy()
{
pthread_cond_t *v0; // rdi
char *v1; // rbp
__int64 v2; // rbx
__int64 v3; // rax
__int64 result; // rax

if ( *((_DWORD *)pool + 4) == 1 )
return 0xFFFFFFFFLL;
v0 = (pthread_cond_t *)((char *)pool + 80);
*((_DWORD *)pool + 4) = 1;
pthread_cond_broadcast(v0);
v1 = (char *)pool;
if ( *((int *)pool + 8) > 0 )
{
v2 = 0LL;
do
{
v3 = v2++;
pthread_join(*(_QWORD *)(*((_QWORD *)v1 + 3) + 16 * v3), 0LL);
v1 = (char *)pool;
}
while ( *((_DWORD *)pool + 8) > (int)v2 );
}
free(*((void **)v1 + 3));
*((_QWORD *)v1 + 3) = 0LL;
pthread_mutex_destroy((pthread_mutex_t *)v1 + 1);
pthread_cond_destroy((pthread_cond_t *)(v1 + 80));
free(v1);
result = 0LL;
pool = 0LL;
return result;
}

canonical_decompiled_code:
__int64 func3()
{
pthread_cond_t *v0; // rdi
char *v1; // rbp
__int64 v2; // rbx
__int64 v3; // rax
__int64 result; // rax

if ( *((_DWORD *)pool + 4) == 1 )
return 0xFFFFFFFFLL;
v0 = (pthread_cond_t *)((char *)pool + 80);
*((_DWORD *)pool + 4) = 1;
pthread_cond_broadcast(v0);
v1 = (char *)pool;
if ( *((int *)pool + 8) > 0 )
{
v2 = 0LL;
do
{
v3 = v2++;
pthread_join(*(_QWORD *)(*((_QWORD *)v1 + 3) + 16 * v3), 0LL);
v1 = (char *)pool;
}
while ( *((_DWORD *)pool + 8) > (int)v2 );
}
free(*((void **)v1 + 3));
*((_QWORD *)v1 + 3) = 0LL;
pthread_mutex_destroy((pthread_mutex_t *)v1 + 1);
pthread_cond_destroy((pthread_cond_t *)(v1 + 80));
free(v1);
result = 0LL;
pool = 0LL;
return result;
}

original_code:
int pool_destroy(void)
{
    int i;
    if (pool->shutdown == 1)
    {
        return -1;
    }
    pool->shutdown = 1;
    pthread_cond_broadcast(&(pool->queue_ready));
    for (i = 0; i < pool->max_thread_num; ++i)
    {
        pthread_join(pool->thread_unite_list[i].thread_id, ((void *)0));
    }
    free(pool->thread_unite_list);
    pool->thread_unite_list = ((void *)0);
    pthread_mutex_destroy(&(pool->queue_lock));
    pthread_cond_destroy(&(pool->queue_ready));
    free(pool);
    pool = ((void *)0);
    return 0;
}

canonical_original_code:
int pool_destroy()
{
    int i;
    if (pool->shutdown == 1)
    {
        return -1;
    }
    pool->shutdown = 1;
    pthread_cond_broadcast(&(pool->queue_ready));
    for (i = 0; i < pool->max_thread_num; ++i)
    {
        pthread_join(pool->thread_unite_list[i].thread_id, ((void *)0));
    }
    free(pool->thread_unite_list);
    pool->thread_unite_list = ((void *)0);
    pthread_mutex_destroy(&(pool->queue_lock));
    pthread_cond_destroy(&(pool->queue_ready));
    free(pool);
    pool = ((void *)0);
    return 0;
}


decompiled_code:
__int64 pool_check_state()
{
int v0; // edx
__int64 v1; // rcx
_DWORD *v2; // rax
__int64 v3; // rdx
__int64 result; // rax

pthread_mutex_lock((pthread_mutex_t *)pool + 1);
v0 = *((_DWORD *)pool + 8);
if ( v0 <= 0 )
{
LABEL_6:
pthread_mutex_unlock((pthread_mutex_t *)pool + 1);
result = 0LL;
}
else
{
v1 = *((_QWORD *)pool + 3);
v2 = (_DWORD *)(v1 + 8);
v3 = v1 + 16LL * (unsigned int)(v0 - 1) + 24;
while ( *v2 )
{
v2 += 4;
if ( v2 == (_DWORD *)v3 )
goto LABEL_6;
}
pthread_mutex_unlock((pthread_mutex_t *)pool + 1);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func4()
{
int v0; // edx
__int64 v1; // rcx
_DWORD *v2; // rax
__int64 v3; // rdx
__int64 result; // rax

pthread_mutex_lock((pthread_mutex_t *)pool + 1);
v0 = *((_DWORD *)pool + 8);
if ( v0 <= 0 )
{
LABEL_6:
pthread_mutex_unlock((pthread_mutex_t *)pool + 1);
result = 0LL;
}
else
{
v1 = *((_QWORD *)pool + 3);
v2 = (_DWORD *)(v1 + 8);
v3 = v1 + 16LL * (unsigned int)(v0 - 1) + 24;
while ( *v2 )
{
v2 += 4;
if ( v2 == (_DWORD *)v3 )
goto LABEL_6;
}
pthread_mutex_unlock((pthread_mutex_t *)pool + 1);
result = 1LL;
}
return result;
}

original_code:
int pool_check_state(void)
{
 int i;
 pthread_mutex_lock(&(pool->queue_lock));
 for (i = 0; i < pool->max_thread_num; ++i)
 {
  if (pool->thread_unite_list[i].state == 0)
  {
   pthread_mutex_unlock(&(pool->queue_lock));
   return 1;
  }
 }
 pthread_mutex_unlock(&(pool->queue_lock));
 return 0;
}

canonical_original_code:
int pool_check_state()
{
 int i;
 pthread_mutex_lock(&(pool->queue_lock));
 for (i = 0; i < pool->max_thread_num; ++i)
 {
  if (pool->thread_unite_list[i].state == 0)
  {
   pthread_mutex_unlock(&(pool->queue_lock));
   return 1;
  }
 }
 pthread_mutex_unlock(&(pool->queue_lock));
 return 0;
}


decompiled_code:
__int64 pool_destroy_force()
{
pthread_cond_t *v0; // rdi
char *v1; // rbp
__int64 v2; // rbx
__int64 v3; // r12
__int64 result; // rax

if ( *((_DWORD *)pool + 4) == 1 )
return 0xFFFFFFFFLL;
v0 = (pthread_cond_t *)((char *)pool + 80);
*((_DWORD *)pool + 4) = 1;
pthread_cond_broadcast(v0);
sleep(2u);
v1 = (char *)pool;
if ( *((int *)pool + 8) > 0 )
{
v2 = 0LL;
do
{
v3 = v2++;
v3 *= 16LL;
pthread_cancel(*(_QWORD *)(*((_QWORD *)v1 + 3) + v3));
pthread_join(*(_QWORD *)(*((_QWORD *)pool + 3) + v3), 0LL);
v1 = (char *)pool;
}
while ( *((_DWORD *)pool + 8) > (int)v2 );
}
free(*((void **)v1 + 3));
*((_QWORD *)v1 + 3) = 0LL;
pthread_mutex_destroy((pthread_mutex_t *)v1 + 1);
pthread_cond_destroy((pthread_cond_t *)(v1 + 80));
free(v1);
result = 0LL;
pool = 0LL;
return result;
}

canonical_decompiled_code:
__int64 func5()
{
pthread_cond_t *v0; // rdi
char *v1; // rbp
__int64 v2; // rbx
__int64 v3; // r12
__int64 result; // rax

if ( *((_DWORD *)pool + 4) == 1 )
return 0xFFFFFFFFLL;
v0 = (pthread_cond_t *)((char *)pool + 80);
*((_DWORD *)pool + 4) = 1;
pthread_cond_broadcast(v0);
sleep(2u);
v1 = (char *)pool;
if ( *((int *)pool + 8) > 0 )
{
v2 = 0LL;
do
{
v3 = v2++;
v3 *= 16LL;
pthread_cancel(*(_QWORD *)(*((_QWORD *)v1 + 3) + v3));
pthread_join(*(_QWORD *)(*((_QWORD *)pool + 3) + v3), 0LL);
v1 = (char *)pool;
}
while ( *((_DWORD *)pool + 8) > (int)v2 );
}
free(*((void **)v1 + 3));
*((_QWORD *)v1 + 3) = 0LL;
pthread_mutex_destroy((pthread_mutex_t *)v1 + 1);
pthread_cond_destroy((pthread_cond_t *)(v1 + 80));
free(v1);
result = 0LL;
pool = 0LL;
return result;
}

original_code:
int pool_destroy_force(void)
{
    int i;
    if (pool->shutdown == 1)
    {
        return -1;
    }
    pool->shutdown = 1;
    pthread_cond_broadcast(&(pool->queue_ready));
    sleep(2);
    for (i = 0; i < pool->max_thread_num; ++i)
    {
        pthread_cancel(pool->thread_unite_list[i].thread_id);
        pthread_join(pool->thread_unite_list[i].thread_id, ((void *)0));
    }
    free(pool->thread_unite_list);
    pool->thread_unite_list = ((void *)0);
    pthread_mutex_destroy(&(pool->queue_lock));
    pthread_cond_destroy(&(pool->queue_ready));
    free(pool);
    pool = ((void *)0);
    return 0;
}

canonical_original_code:
int pool_destroy_force()
{
    int i;
    if (pool->shutdown == 1)
    {
        return -1;
    }
    pool->shutdown = 1;
    pthread_cond_broadcast(&(pool->queue_ready));
    sleep(2);
    for (i = 0; i < pool->max_thread_num; ++i)
    {
        pthread_cancel(pool->thread_unite_list[i].thread_id);
        pthread_join(pool->thread_unite_list[i].thread_id, ((void *)0));
    }
    free(pool->thread_unite_list);
    pool->thread_unite_list = ((void *)0);
    pthread_mutex_destroy(&(pool->queue_lock));
    pthread_cond_destroy(&(pool->queue_ready));
    free(pool);
    pool = ((void *)0);
    return 0;
}


ouch

decompiled_code:
void __noreturn ouch()
{
recive = 1;
_sysv_signal(2, 0LL);
puts("Recived SIGINT");
pool_destroy_force();
libssh2_exit();
free_ip_list(&IP_List);
free_user_list(&User_List);
free_password_list(&Pwd_List);
free_setting(&setting);
sleep(2u);
puts("END! BYE.......");
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
recive = 1;
_sysv_signal(2, 0LL);
puts("Recived SIGINT");
pool_destroy_force();
libssh2_exit();
free_ip_list(&IP_List);
free_user_list(&User_List);
free_password_list(&Pwd_List);
free_setting(&setting);
sleep(2u);
puts("END! BYE.......");
exit(0);
}

original_code:
void ouch(int sig)
{
    recive = 1;
    (void) signal(2, ((__sighandler_t) 0));
    printf("Recived SIGINT\n");
    pool_destroy_force();
    libssh2_exit();
    free_ip_list(&IP_List);
    free_user_list(&User_List);
    free_password_list(&Pwd_List);
    free_setting(&setting);
    sleep(2);
    printf("END! BYE.......\n");
    exit(0);
}

canonical_original_code:
void ouch(int sig)
{
    recive = 1;
    (void) signal(2, ((void (*)(int)) 0));
    printf("Recived SIGINT\n");
    pool_destroy_force();
    libssh2_exit();
    free_ip_list(&IP_List);
    free_user_list(&User_List);
    free_password_list(&Pwd_List);
    free_setting(&setting);
    sleep(2);
    printf("END! BYE.......\n");
    exit(0);
}

variable_types: sig int


decompiled_code:
int show_help()
{
puts(aHIp);
puts(aHIp_0);
puts(aU);
puts(aU_0);
puts(aP);
puts(aP_0);
puts(aT);
puts(aD);
puts(aT_0);
return puts(aN);
}

canonical_decompiled_code:
int func1()
{
puts(aHIp);
puts(aHIp_0);
puts(aU);
puts(aU_0);
puts(aP);
puts(aP_0);
puts(aT);
puts(aD);
puts(aT_0);
return puts(aN);
}

original_code:
void show_help(void)
{
 printf("-h [ip地址]\n");
 printf("-H [ip列表文件]\n");
 printf("-u [用户名]\n");
 printf("-U [用户名列表文件]\n");
 printf("-p [密码]\n");
 printf("-P [密码列表文件]\n");
 printf("-t [端口]  默认22\n");
 printf("-D [日志文件目录] 默认在HOME\n");
 printf("-T [线程数]  默认是6\n");
 printf("-N [密码分组数] 默认是4\n");
}

canonical_original_code:
void show_help()
{
 printf("-h [ip地址]\n");
 printf("-H [ip列表文件]\n");
 printf("-u [用户名]\n");
 printf("-U [用户名列表文件]\n");
 printf("-p [密码]\n");
 printf("-P [密码列表文件]\n");
 printf("-t [端口]  默认22\n");
 printf("-D [日志文件目录] 默认在HOME\n");
 printf("-T [线程数]  默认是6\n");
 printf("-N [密码分组数] 默认是4\n");
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
bool v3; // zf
int v4; // er12
unsigned int v5; // er12
__m128i *v6; // rbx
_DWORD *v7; // rax
_DWORD *v8; // rbp
__int64 v9; // rax
const __m128i *v10; // rbp
__m128i v11; // xmm2
__m128i *v12; // rsi
__m128i v13; // xmm0
__m128i v14; // xmm1
__int64 v16; // rdx

v3 = strcmp(argv[1], "-help") == 0;
v4 = !v3;
if ( v3 )
{
show_help();
}
else
{
if ( (int)checkSetting((unsigned int)argc, argv, &setting) < 0 )
{
v4 = -1;
fwrite("Options Error \n", 1uLL, 0xFuLL, stderr);
return v4;
}
if ( (int)analysisSetting(&setting, &IP_List, &User_List, &Pwd_List) < 0 )
return -1;
if ( *((int *)&IP_List + 2) <= 0 )
goto LABEL_18;
if ( (int)pool_init(*((unsigned int *)&setting + 1065)) < 0 )
{
v4 = -2;
free_ip_list(&IP_List);
free_user_list(&User_List);
free_password_list(&Pwd_List);
free_setting(&setting);
fwrite("Init thread pool error!!\n", 1uLL, 0x19uLL, stderr);
return v4;
}
_sysv_signal(2, (__sighandler_t)ouch);
gcrypt_fix();
v5 = libssh2_init(0LL);
if ( v5 )
{
free_ip_list(&IP_List);
free_user_list(&User_List);
free_password_list(&Pwd_List);
free_setting(&setting);
v16 = v5;
v4 = -1;
fprintf(stderr, "libssh2 initialization failed (%d)\n", v16);
return v4;
}
v6 = (__m128i *)malloc((__int64)*((int *)&IP_List + 2) << 6);
v7 = malloc(0x18uLL);
v8 = v7;
if ( !v7 )
{
LABEL_18:
v4 = -1;
free_ip_list(&IP_List);
free_user_list(&User_List);
free_password_list(&Pwd_List);
free_setting(&setting);
return v4;
}
v7[2] = 1;
*(_QWORD *)v7 = v6;
pthread_mutex_lock((pthread_mutex_t *)(unsigned int)((_DWORD)&setting + 4192));
v9 = *((_QWORD *)&setting + 531);
*((_QWORD *)&setting + 531) = v8;
*((_QWORD *)v8 + 2) = v9;
pthread_mutex_unlock((pthread_mutex_t *)(unsigned int)((_DWORD)&setting + 4192));
v10 = (const __m128i *)IP_List;
if ( *((int *)&IP_List + 2) > 0 )
{
do
{
v11 = _mm_loadu_si128(v10);
v12 = v6;
v13 = _mm_load_si128((const __m128i *)&User_List);
v14 = _mm_load_si128((const __m128i *)&Pwd_List);
++v5;
v6 += 4;
v6[-1].m128i_i16[2] = *((_WORD *)&setting + 24);
v6[-3] = v13;
v6[-2] = v14;
v6[-4] = v11;
v6[-1].m128i_i64[1] = (__int64)&setting;
pool_add_worker(&ssh_test, v12);
v10 = (const __m128i *)v10->m128i_i64[1];
}
while ( *((_DWORD *)&IP_List + 2) > (signed int)v5 );
}
sleep(2u);
while ( 1 )
{
v4 = pool_check_state();
if ( !v4 )
break;
sleep(6u);
}
if ( !recive )
{
pool_destroy();
libssh2_exit();
free_ip_list(&IP_List);
free_user_list(&User_List);
free_password_list(&Pwd_List);
free_setting(&setting);
}
}
return v4;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
bool v3; // zf
int v4; // er12
unsigned int v5; // er12
__m128i *v6; // rbx
_DWORD *v7; // rax
_DWORD *v8; // rbp
__int64 v9; // rax
const __m128i *v10; // rbp
__m128i v11; // xmm2
__m128i *v12; // rsi
__m128i v13; // xmm0
__m128i v14; // xmm1
__int64 v16; // rdx

v3 = strcmp(argv[1], "-help") == 0;
v4 = !v3;
if ( v3 )
{
func1();
}
else
{
if ( (int)checkSetting((unsigned int)argc, argv, &setting) < 0 )
{
v4 = -1;
fwrite("Options Error \n", 1uLL, 0xFuLL, stderr);
return v4;
}
if ( (int)analysisSetting(&setting, &IP_List, &User_List, &Pwd_List) < 0 )
return -1;
if ( *((int *)&IP_List + 2) <= 0 )
goto LABEL_18;
if ( (int)pool_init(*((unsigned int *)&setting + 1065)) < 0 )
{
v4 = -2;
free_ip_list(&IP_List);
free_user_list(&User_List);
free_password_list(&Pwd_List);
free_setting(&setting);
fwrite("Init thread pool error!!\n", 1uLL, 0x19uLL, stderr);
return v4;
}
_sysv_signal(2, (__sighandler_t)ouch);
gcrypt_fix();
v5 = libssh2_init(0LL);
if ( v5 )
{
free_ip_list(&IP_List);
free_user_list(&User_List);
free_password_list(&Pwd_List);
free_setting(&setting);
v16 = v5;
v4 = -1;
fprintf(stderr, "libssh2 initialization failed (%d)\n", v16);
return v4;
}
v6 = (__m128i *)malloc((__int64)*((int *)&IP_List + 2) << 6);
v7 = malloc(0x18uLL);
v8 = v7;
if ( !v7 )
{
LABEL_18:
v4 = -1;
free_ip_list(&IP_List);
free_user_list(&User_List);
free_password_list(&Pwd_List);
free_setting(&setting);
return v4;
}
v7[2] = 1;
*(_QWORD *)v7 = v6;
pthread_mutex_lock((pthread_mutex_t *)(unsigned int)((_DWORD)&setting + 4192));
v9 = *((_QWORD *)&setting + 531);
*((_QWORD *)&setting + 531) = v8;
*((_QWORD *)v8 + 2) = v9;
pthread_mutex_unlock((pthread_mutex_t *)(unsigned int)((_DWORD)&setting + 4192));
v10 = (const __m128i *)IP_List;
if ( *((int *)&IP_List + 2) > 0 )
{
do
{
v11 = _mm_loadu_si128(v10);
v12 = v6;
v13 = _mm_load_si128((const __m128i *)&User_List);
v14 = _mm_load_si128((const __m128i *)&Pwd_List);
++v5;
v6 += 4;
v6[-1].m128i_i16[2] = *((_WORD *)&setting + 24);
v6[-3] = v13;
v6[-2] = v14;
v6[-4] = v11;
v6[-1].m128i_i64[1] = (__int64)&setting;
pool_add_worker(&ssh_test, v12);
v10 = (const __m128i *)v10->m128i_i64[1];
}
while ( *((_DWORD *)&IP_List + 2) > (signed int)v5 );
}
sleep(2u);
while ( 1 )
{
v4 = pool_check_state();
if ( !v4 )
break;
sleep(6u);
}
if ( !recive )
{
pool_destroy();
libssh2_exit();
free_ip_list(&IP_List);
free_user_list(&User_List);
free_password_list(&Pwd_List);
free_setting(&setting);
}
}
return v4;
}

original_code:
int main(int argc, char **argv)
{
    int i, rc;
    struct Test_ssh_Arg_by_IP *workarg = ((void *)0);
    struct ip_node *pIP = ((void *)0);
    struct workarg_queue *record_node = ((void *)0);
    if (strcmp(argv[1],"-help") == 0)
    {
 show_help();
 return 0;
    }
    if (checkSetting(argc, argv, &setting) < 0)
    {
        fprintf(stderr, "Options Error \n");
        return -1;
    }
    if (analysisSetting(&setting, &IP_List, &User_List, &Pwd_List) < 0)
    {
        return -1;
    }
    if (IP_List.count <= 0)
    {
        free_ip_list(&IP_List);
        free_user_list(&User_List);
        free_password_list(&Pwd_List);
        free_setting(&setting);
        return -1;
    }
    if (pool_init(setting.thread_num) < 0)
    {
 free_ip_list(&IP_List);
        free_user_list(&User_List);
        free_password_list(&Pwd_List);
        free_setting(&setting);
  fprintf(stderr,"Init thread pool error!!\n");
  return -2;
    }
    (void)signal(2, ouch);
    gcrypt_fix();
    rc = libssh2_init (0);
    if (rc != 0) {
        free_ip_list(&IP_List);
        free_user_list(&User_List);
        free_password_list(&Pwd_List);
        free_setting(&setting);
        fprintf (stderr, "libssh2 initialization failed (%d)\n", rc);
        return -1;
    }
    workarg = (struct Test_ssh_Arg_by_IP *)malloc(sizeof(struct Test_ssh_Arg_by_IP) * IP_List.count);
    record_node = (struct workarg_queue *)malloc(sizeof(struct workarg_queue));
    if (record_node == ((void *)0))
    {
        free_ip_list(&IP_List);
        free_user_list(&User_List);
        free_password_list(&Pwd_List);
        free_setting(&setting);;
        return -1;
    }
    record_node->level = 1;
    record_node->point = (void *)workarg;
    pthread_mutex_lock(&(setting.setting_mutex));
    record_node->next = setting.workarg_list_head;
    setting.workarg_list_head = record_node;
    pthread_mutex_unlock(&(setting.setting_mutex));
    pIP = IP_List.head;
    for (i = 0; i < IP_List.count; ++i)
    {
        workarg[i].port = setting.port;
        workarg[i].users = User_List;
        workarg[i].passwords = Pwd_List;
        workarg[i].ip = *pIP;
        workarg[i].setting = &setting;
        pool_add_worker (ssh_test,(void *)(&workarg[i]));
        pIP = pIP->next;
    }
    sleep(2);
    while (pool_check_state())
    {
        sleep(6);
    }
    if (recive == 0)
    {
        pool_destroy();
        libssh2_exit();
        free_ip_list(&IP_List);
        free_user_list(&User_List);
        free_password_list(&Pwd_List);
        free_setting(&setting);
    }
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    int i; int rc;
    struct Test_ssh_Arg_by_IP *workarg = ((void *)0);
    struct ip_node *pIP = ((void *)0);
    struct workarg_queue *record_node = ((void *)0);
    if (strcmp(argv[1],"-help") == 0)
    {
 show_help();
 return 0;
    }
    if (checkSetting(argc, argv, &setting) < 0)
    {
        fprintf(stderr, "Options Error \n");
        return -1;
    }
    if (analysisSetting(&setting, &IP_List, &User_List, &Pwd_List) < 0)
    {
        return -1;
    }
    if (IP_List.count <= 0)
    {
        free_ip_list(&IP_List);
        free_user_list(&User_List);
        free_password_list(&Pwd_List);
        free_setting(&setting);
        return -1;
    }
    if (pool_init(setting.thread_num) < 0)
    {
 free_ip_list(&IP_List);
        free_user_list(&User_List);
        free_password_list(&Pwd_List);
        free_setting(&setting);
  fprintf(stderr,"Init thread pool error!!\n");
  return -2;
    }
    (void)signal(2, ouch);
    gcrypt_fix();
    rc = libssh2_init (0);
    if (rc != 0) {
        free_ip_list(&IP_List);
        free_user_list(&User_List);
        free_password_list(&Pwd_List);
        free_setting(&setting);
        fprintf (stderr, "libssh2 initialization failed (%d)\n", rc);
        return -1;
    }
    workarg = (struct Test_ssh_Arg_by_IP *)malloc(sizeof(struct Test_ssh_Arg_by_IP) * IP_List.count);
    record_node = (struct workarg_queue *)malloc(sizeof(struct workarg_queue));
    if (record_node == ((void *)0))
    {
        free_ip_list(&IP_List);
        free_user_list(&User_List);
        free_password_list(&Pwd_List);
        free_setting(&setting);;
        return -1;
    }
    record_node->level = 1;
    record_node->point = (void *)workarg;
    pthread_mutex_lock(&(setting.setting_mutex));
    record_node->next = setting.workarg_list_head;
    setting.workarg_list_head = record_node;
    pthread_mutex_unlock(&(setting.setting_mutex));
    pIP = IP_List.head;
    for (i = 0; i < IP_List.count; ++i)
    {
        workarg[i].port = setting.port;
        workarg[i].users = User_List;
        workarg[i].passwords = Pwd_List;
        workarg[i].ip = *pIP;
        workarg[i].setting = &setting;
        pool_add_worker (ssh_test,(void *)(&workarg[i]));
        pIP = pIP->next;
    }
    sleep(2);
    while (pool_check_state())
    {
        sleep(6);
    }
    if (recive == 0)
    {
        pool_destroy();
        libssh2_exit();
        free_ip_list(&IP_List);
        free_user_list(&User_List);
        free_password_list(&Pwd_List);
        free_setting(&setting);
    }
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: rc int

variable_types: struct Test_ssh_Arg_by_IP { struct ip_node { char *ip; struct ip_node *next; } ip; struct user_list { struct user_node { char *user; struct user_node *next; } *head; int count; } users; struct password_list { struct password_node { char *password; struct password_node *next; } *head; int count; } passwords; int ret; short port; struct _Setting { char *Host_IP; char *Host_File; char *Username; char *User_File; char *Password; char *Password_File; short port; int connect_test_count; char success_log_filename[2048]; char path_log[2048]; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } success_log_mutex; union pthread_mutex_t setting_mutex; struct password_node **pwd_groups; int per_pwd_num; struct workarg_queue { void *point; int level; struct workarg_queue *next; } *workarg_list_head; int pwd_group_num; int thread_num; } *setting; } *

variable_types: struct ip_node { char *ip; struct ip_node *next; } *

user_defined_types: struct Test_ssh_Arg_by_IP { struct ip_node ip; struct user_list users; struct password_list passwords; int ret; short port; struct _Setting *setting; }

user_defined_types: struct user_list { struct user_node *head; int count; }

user_defined_types: struct password_list { struct password_node *head; int count; }

user_defined_types: struct _Setting { char *Host_IP; char *Host_File; char *Username; char *User_File; char *Password; char *Password_File; short port; int connect_test_count; char success_log_filename[2048]; char path_log[2048]; union pthread_mutex_t success_log_mutex; union pthread_mutex_t setting_mutex; struct password_node **pwd_groups; int per_pwd_num; struct workarg_queue *workarg_list_head; int pwd_group_num; int thread_num; }


decompiled_code:
__int64 gcrypt_fix()
{
return gcry_control(47LL, &gcry_threads_pthread);
}

canonical_decompiled_code:
__int64 func0()
{
return gcry_control(47LL, &gcry_threads_pthread);
}

original_code:
void gcrypt_fix() {
   gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
}


repo: billvb/core-flight-executive

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
int v5; // eax
int v6; // ebp
unsigned int v7; // er12
unsigned int v8; // er13
__int64 v9; // rsi
unsigned int v10; // eax
_BYTE v12[152]; // [rsp+0h] [rbp-98h] BYREF

if ( argc != 2 || (v3 = argv[1], !strcmp(v3, "-help")) )
{
printf("\ncFE TS CRC calculator for LRO files.");
puts("\nUsage: cfe_ts_crc [filename]");
exit(0);
}
v5 = open(v3, 0);
v6 = v5;
if ( v5 < 0 )
{
puts("\ncfe_ts_crc error: can't open input file!");
exit(0);
}
lseek(v5, 116LL, 0);
v7 = 0;
v8 = 0;
do
{
v9 = (unsigned int)read(v6, v12, 0x64uLL);
v10 = CFE_ES_CalculateCRC(v12, v9, v7, 2LL);
v8 += v9;
v7 = v10;
}
while ( (_DWORD)v9 == 100 );
printf(
"\nTable File Name: %s\nTable Size: %d Bytes\nExpected TS Validation CRC: 0x%08X\n\n",
argv[1],
v8,
v10);
return v7;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbp
int v5; // eax
int v6; // ebp
unsigned int v7; // er12
unsigned int v8; // er13
__int64 v9; // rsi
unsigned int v10; // eax
_BYTE v12[152]; // [rsp+0h] [rbp-98h] BYREF

if ( argc != 2 || (v3 = argv[1], !strcmp(v3, "-help")) )
{
printf("\ncFE TS CRC calculator for LRO files.");
puts("\nUsage: cfe_ts_crc [filename]");
exit(0);
}
v5 = open(v3, 0);
v6 = v5;
if ( v5 < 0 )
{
puts("\ncfe_ts_crc error: can't open input file!");
exit(0);
}
lseek(v5, 116LL, 0);
v7 = 0;
v8 = 0;
do
{
v9 = (unsigned int)read(v6, v12, 0x64uLL);
v10 = func7(v12, v9, v7, 2LL);
v8 += v9;
v7 = v10;
}
while ( (_DWORD)v9 == 100 );
printf(
"\nTable File Name: %s\nTable Size: %d Bytes\nExpected TS Validation CRC: 0x%08X\n\n",
argv[1],
v8,
v10);
return v7;
}

original_code:
int main( int argc, char **argv )
{
 int readSize;
 int skipSize = 0;
 int fileSize = 0;
 uint32 fileCRC = 0;
 int fd;
 int done = 0;
 char buffer[100];
 if ( ( argc != 2 ) || ( strncmp(argv[1], "-help", 100) == 0 ) )
 {
     printf("\ncFE TS CRC calculator for LRO files.");
     printf("\nUsage: cfe_ts_crc [filename]\n");
        exit(0);
    }
 skipSize = 116;
 fd = open( argv[1], 00 );
 if ( fd < 0 )
 {
  printf("\ncfe_ts_crc error: can't open input file!\n");
  exit(0);
 }
 lseek( fd, skipSize, 0 );
 while ( done == 0 )
 {
  readSize = read(fd, buffer, 100);
  fileCRC = CFE_ES_CalculateCRC(buffer, readSize, fileCRC, 2);
  fileSize += readSize;
  if (readSize != 100) done=1;
 }
     printf("\nTable File Name:            %s\nTable Size:                 %d Bytes\nExpected TS Validation CRC: 0x%08X\n\n", argv[1], fileSize, fileCRC);
     return(fileCRC);
}

canonical_original_code:
int main(int argc, char **argv)
{
 int readSize;
 int skipSize = 0;
 int fileSize = 0;
 unsigned int fileCRC = 0;
 int fd;
 int done = 0;
 char buffer[100];
 if ( ( argc != 2 ) || ( strncmp(argv[1], "-help", 100) == 0 ) )
 {
     printf("\ncFE TS CRC calculator for LRO files.");
     printf("\nUsage: cfe_ts_crc [filename]\n");
        exit(0);
    }
 skipSize = 116;
 fd = open( argv[1], 00 );
 if ( fd < 0 )
 {
  printf("\ncfe_ts_crc error: can't open input file!\n");
  exit(0);
 }
 lseek( fd, skipSize, 0 );
 while ( done == 0 )
 {
  readSize = read(fd, buffer, 100);
  fileCRC = CFE_ES_CalculateCRC(buffer, readSize, fileCRC, 2);
  fileSize += readSize;
  if (readSize != 100) done=1;
 }
     printf("\nTable File Name:            %s\nTable Size:                 %d Bytes\nExpected TS Validation CRC: 0x%08X\n\n", argv[1], fileSize, fileCRC);
     return(fileCRC);
}

variable_types: readSize int

variable_types: skipSize int

variable_types: fileSize int

variable_types:
fileCRC
unsigned int

variable_types: fd int

variable_types: done int

variable_types:
buffer
char [100]


decompiled_code:
__int64 __fastcall CFE_ES_CalculateCRC(_BYTE *a1, int a2, __int16 a3, int a4)
{
__int64 result; // rax
_BYTE *v5; // rcx
unsigned __int16 v6; // dx
unsigned __int8 v7; // al

result = 0LL;
if ( a4 == 2 )
{
LOWORD(result) = a3;
v5 = &a1[a2];
if ( a2 )
{
do
{
v6 = result;
v7 = *a1++ ^ result;
result = (unsigned int)(CrcTable_0[v7] ^ HIBYTE(v6));
}
while ( a1 != v5 );
}
else
{
result = (unsigned int)a3;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_BYTE *a1, int a2, __int16 a3, int a4)
{
__int64 result; // rax
_BYTE *v5; // rcx
unsigned __int16 v6; // dx
unsigned __int8 v7; // al

result = 0LL;
if ( a4 == 2 )
{
LOWORD(result) = a3;
v5 = &a1[a2];
if ( a2 )
{
do
{
v6 = result;
v7 = *a1++ ^ result;
result = (unsigned int)(CrcTable_0[v7] ^ HIBYTE(v6));
}
while ( a1 != v5 );
}
else
{
result = (unsigned int)a3;
}
}
return result;
}

variable_types:
DataPtr
void *

variable_types:
DataLength
unsigned int

variable_types:
InputCRC
unsigned int

variable_types:
TypeCRC
unsigned int

variable_types: Index short

variable_types: Crc short

variable_types:
BufPtr
unsigned char *

variable_types:
CrcTable
unsigned short [256]

return_type: unsigned int