main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int64 v4; // rax
_DWORD *v5; // rax
_DWORD *v6; // rbp
__int64 v7; // rax
__int64 v8; // rsi
unsigned __int64 v9; // rbx
__int64 v10; // rax
unsigned __int64 *v11; // r13
unsigned __int64 v12; // rbx
__int64 v14; // rax
__int64 v15; // rax
unsigned __int64 v16; // rbx
unsigned __int64 *v17; // r13
__int64 v18; // rax
unsigned __int64 v19; // rbx
unsigned __int64 *v20; // r13
double v21; // xmm0_8
unsigned __int64 *v22; // r13
__int64 v23; // rax
unsigned __int64 v24; // rbx
unsigned __int64 *v25; // r13
__int64 v26; // rax
unsigned __int64 v28; // [rsp+8h] [rbp-1A0h]
char v29[236]; // [rsp+10h] [rbp-198h] BYREF
int longind; // [rsp+FCh] [rbp-ACh] BYREF
struct option longopts; // [rsp+100h] [rbp-A8h] BYREF
const char *v32; // [rsp+120h] [rbp-88h]
int v33; // [rsp+128h] [rbp-80h]
__int64 v34; // [rsp+130h] [rbp-78h]
int v35; // [rsp+138h] [rbp-70h]
const char *v36; // [rsp+140h] [rbp-68h]
int v37; // [rsp+148h] [rbp-60h]
__int64 v38; // [rsp+150h] [rbp-58h]
int v39; // [rsp+158h] [rbp-50h]
__int64 v40; // [rsp+160h] [rbp-48h]
int v41; // [rsp+168h] [rbp-40h]
__int64 v42; // [rsp+170h] [rbp-38h]
int v43; // [rsp+178h] [rbp-30h]

longopts.name = "help";
longopts.has_arg = 0;
longopts.flag = 0LL;
longopts.val = 104;
v32 = "num_ops";
v33 = 1;
v34 = 0LL;
v35 = 111;
v36 = "num_stores";
v37 = 1;
v38 = 0LL;
v39 = 115;
v40 = 0LL;
v41 = 0;
v42 = 0LL;
v43 = 0;
while ( 1 )
{
longind = 0;
v3 = getopt_long(argc, (char *const *)argv, "ho:s:", &longopts, &longind);
if ( v3 == -1 )
break;
if ( v3 )
{
LABEL_6:
if ( v3 == 111 )
{
num_ops = atoi(optarg);
}
else if ( v3 > 111 )
{
if ( v3 != 115 )
goto LABEL_17;
num_stores = atoi(optarg);
}
else
{
if ( v3 == 104 )
{
puts(
"sspfd_test -- testing sspfd\n"
"\n"
"Usage:\n"
" sspfd_test [options...]\n"
"\n"
"Options:\n"
" -h, --help\n"
" Print this message\n"
" -o, --num_ops <int>\n"
" Test for number of operations (0=infinite, default=100000)\n"
" -s, --num_stores <int>\n"
" number of sspfd stores to be used (default=DEFAULT_DSL_PER_CORE)");
exit(0);
}
if ( v3 > 104 )
goto LABEL_17;
if ( v3 )
{
if ( v3 == 63 )
{
puts("Use -h or --help for help");
exit(0);
}
LABEL_17:
exit(1);
}
}
}
else if ( !*((_QWORD *)&longopts.flag + 4 * longind) )
{
v3 = *(&longopts.val + 8 * longind);
goto LABEL_6;
}
}
if ( !num_stores )
_assert_fail("num_stores > 0", "sspfd_test.c", 0x6Eu, "main");
if ( !num_ops )
_assert_fail("num_ops > 0", "sspfd_test.c", 0x6Fu, "main");
v4 = sspfd_get_id();
printf("[%02lu] ", v4);
printf("* initializing %lu stores of %lu entries", num_stores, num_ops);
putchar(10);
fflush(stdout);
sspfd_store_init(num_stores, num_ops, 0LL);
v5 = malloc(4uLL);
v6 = v5;
if ( !v5 )
_assert_fail("dummy != NULL", "sspfd_test.c", 0x75u, "main");
*v5 = 13;
while ( num_stores-- )
{
v14 = sspfd_get_id();
printf("[%02lu] ", v14);
printf("* testing store %lu", num_stores);
putchar(10);
fflush(stdout);
v12 = num_stores;
if ( num_stores == 2 )
{
v18 = sspfd_get_id();
printf("[%02lu] ", v18);
printf("** asm volatile(\"pause\")");
putchar(10);
fflush(stdout);
v8 = num_ops;
if ( num_ops )
{
v19 = 0LL;
do
{
v20 = (unsigned __int64 *)(sspfd_s + 8 * num_stores);
*v20 = getticks();
_mm_pause();
*(_QWORD *)(*(_QWORD *)(sspfd_store + 8 * num_stores) + 8 * v19++) = getticks()
- (sspfd_correction
+ *(_QWORD *)(sspfd_s + 8 * num_stores));
v8 = num_ops;
}
while ( num_ops > v19 );
}
}
else if ( (unsigned __int64)num_stores > 2 )
{
if ( num_stores == 3 )
{
v7 = sspfd_get_id();
printf("[%02lu] ", v7);
printf("** for (rr = 0; rr < 1e3; rr++);");
putchar(10);
fflush(stdout);
v8 = num_ops;
if ( num_ops )
{
v9 = 0LL;
do
{
v22 = (unsigned __int64 *)(sspfd_s + 8 * num_stores);
*v22 = getticks();
v28 = 0LL;
if ( (double)0 < 1000.0 )
{
do
{
if ( (++v28 & 0x8000000000000000LL) != 0LL )
v21 = (double)(int)(v28 & 1 | (v28 >> 1)) + (double)(int)(v28 & 1 | (v28 >> 1));
else
v21 = (double)(int)v28;
}
while ( v21 < 1000.0 );
}
*(_QWORD *)(*(_QWORD *)(sspfd_store + 8 * num_stores) + 8 * v9++) = getticks()
- (sspfd_correction
+ *(_QWORD *)(sspfd_s + 8 * num_stores));
v8 = num_ops;
}
while ( num_ops > v9 );
}
}
else
{
v23 = sspfd_get_id();
printf("[%02lu] ", v23);
printf("** L1 access");
putchar(10);
fflush(stdout);
v8 = num_ops;
if ( num_ops )
{
v24 = 0LL;
do
{
v25 = (unsigned __int64 *)(sspfd_s + 8 * num_stores);
*v25 = getticks();
LODWORD(v25) = *v6;
_mm_lfence();
*(_QWORD *)(*(_QWORD *)(sspfd_store + 8 * num_stores) + 8 * v24) = getticks()
- (sspfd_correction
+ *(_QWORD *)(sspfd_s + 8 * num_stores));
if ( (_DWORD)v25 != 13 )
{
v26 = sspfd_get_id();
printf("[%02lu] ", v26);
printf("what?");
putchar(10);
fflush(stdout);
}
++v24;
v8 = num_ops;
}
while ( num_ops > v24 );
}
}
}
else if ( num_stores )
{
v15 = sspfd_get_id();
printf("[%02lu] ", v15);
printf("** asm volatile(\"mfence\")");
putchar(10);
fflush(stdout);
v8 = num_ops;
if ( num_ops )
{
v16 = 0LL;
do
{
v17 = (unsigned __int64 *)(sspfd_s + 8 * num_stores);
*v17 = getticks();
_mm_mfence();
*(_QWORD *)(*(_QWORD *)(sspfd_store + 8 * num_stores) + 8 * v16++) = getticks()
- (sspfd_correction
+ *(_QWORD *)(sspfd_s + 8 * num_stores));
v8 = num_ops;
}
while ( num_ops > v16 );
}
}
else
{
v10 = sspfd_get_id();
printf("[%02lu] ", v10);
printf("** asm volatile(\"nop\")");
putchar(10);
fflush(stdout);
v8 = num_ops;
if ( num_ops )
{
do
{
v11 = (unsigned __int64 *)(sspfd_s + 8 * num_stores);
*v11 = getticks();
*(_QWORD *)(*(_QWORD *)(sspfd_store + 8 * num_stores) + 8 * v12++) = getticks()
- (sspfd_correction
+ *(_QWORD *)(sspfd_s + 8 * num_stores));
v8 = num_ops;
}
while ( num_ops > v12 );
}
}
sspfd_get_stats(num_stores, v8, v29);
sspfd_print_stats(v29);
}
sspfd_store_term();
free(v6);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int64 v4; // rax
_DWORD *v5; // rax
_DWORD *v6; // rbp
__int64 v7; // rax
__int64 v8; // rsi
unsigned __int64 v9; // rbx
__int64 v10; // rax
unsigned __int64 *v11; // r13
unsigned __int64 v12; // rbx
__int64 v14; // rax
__int64 v15; // rax
unsigned __int64 v16; // rbx
unsigned __int64 *v17; // r13
__int64 v18; // rax
unsigned __int64 v19; // rbx
unsigned __int64 *v20; // r13
double v21; // xmm0_8
unsigned __int64 *v22; // r13
__int64 v23; // rax
unsigned __int64 v24; // rbx
unsigned __int64 *v25; // r13
__int64 v26; // rax
unsigned __int64 v28; // [rsp+8h] [rbp-1A0h]
char v29[236]; // [rsp+10h] [rbp-198h] BYREF
int longind; // [rsp+FCh] [rbp-ACh] BYREF
struct option longopts; // [rsp+100h] [rbp-A8h] BYREF
const char *v32; // [rsp+120h] [rbp-88h]
int v33; // [rsp+128h] [rbp-80h]
__int64 v34; // [rsp+130h] [rbp-78h]
int v35; // [rsp+138h] [rbp-70h]
const char *v36; // [rsp+140h] [rbp-68h]
int v37; // [rsp+148h] [rbp-60h]
__int64 v38; // [rsp+150h] [rbp-58h]
int v39; // [rsp+158h] [rbp-50h]
__int64 v40; // [rsp+160h] [rbp-48h]
int v41; // [rsp+168h] [rbp-40h]
__int64 v42; // [rsp+170h] [rbp-38h]
int v43; // [rsp+178h] [rbp-30h]

longopts.name = "help";
longopts.has_arg = 0;
longopts.flag = 0LL;
longopts.val = 104;
v32 = "num_ops";
v33 = 1;
v34 = 0LL;
v35 = 111;
v36 = "num_stores";
v37 = 1;
v38 = 0LL;
v39 = 115;
v40 = 0LL;
v41 = 0;
v42 = 0LL;
v43 = 0;
while ( 1 )
{
longind = 0;
v3 = getopt_long(argc, (char *const *)argv, "ho:s:", &longopts, &longind);
if ( v3 == -1 )
break;
if ( v3 )
{
LABEL_6:
if ( v3 == 111 )
{
num_ops = atoi(optarg);
}
else if ( v3 > 111 )
{
if ( v3 != 115 )
goto LABEL_17;
num_stores = atoi(optarg);
}
else
{
if ( v3 == 104 )
{
puts(
"sspfd_test -- testing sspfd\n"
"\n"
"Usage:\n"
" sspfd_test [options...]\n"
"\n"
"Options:\n"
" -h, --help\n"
" Print this message\n"
" -o, --num_ops <int>\n"
" Test for number of operations (0=infinite, default=100000)\n"
" -s, --num_stores <int>\n"
" number of sspfd stores to be used (default=DEFAULT_DSL_PER_CORE)");
exit(0);
}
if ( v3 > 104 )
goto LABEL_17;
if ( v3 )
{
if ( v3 == 63 )
{
puts("Use -h or --help for help");
exit(0);
}
LABEL_17:
exit(1);
}
}
}
else if ( !*((_QWORD *)&longopts.flag + 4 * longind) )
{
v3 = *(&longopts.val + 8 * longind);
goto LABEL_6;
}
}
if ( !num_stores )
_assert_fail("num_stores > 0", "sspfd_test.c", 0x6Eu, "main");
if ( !num_ops )
_assert_fail("num_ops > 0", "sspfd_test.c", 0x6Fu, "main");
v4 = sspfd_get_id();
printf("[%02lu] ", v4);
printf("* initializing %lu stores of %lu entries", num_stores, num_ops);
putchar(10);
fflush(stdout);
sspfd_store_init(num_stores, num_ops, 0LL);
v5 = malloc(4uLL);
v6 = v5;
if ( !v5 )
_assert_fail("dummy != NULL", "sspfd_test.c", 0x75u, "main");
*v5 = 13;
while ( num_stores-- )
{
v14 = sspfd_get_id();
printf("[%02lu] ", v14);
printf("* testing store %lu", num_stores);
putchar(10);
fflush(stdout);
v12 = num_stores;
if ( num_stores == 2 )
{
v18 = sspfd_get_id();
printf("[%02lu] ", v18);
printf("** asm volatile(\"pause\")");
putchar(10);
fflush(stdout);
v8 = num_ops;
if ( num_ops )
{
v19 = 0LL;
do
{
v20 = (unsigned __int64 *)(sspfd_s + 8 * num_stores);
*v20 = func0();
_mm_pause();
*(_QWORD *)(*(_QWORD *)(sspfd_store + 8 * num_stores) + 8 * v19++) = func0()
- (sspfd_correction
+ *(_QWORD *)(sspfd_s + 8 * num_stores));
v8 = num_ops;
}
while ( num_ops > v19 );
}
}
else if ( (unsigned __int64)num_stores > 2 )
{
if ( num_stores == 3 )
{
v7 = sspfd_get_id();
printf("[%02lu] ", v7);
printf("** for (rr = 0; rr < 1e3; rr++);");
putchar(10);
fflush(stdout);
v8 = num_ops;
if ( num_ops )
{
v9 = 0LL;
do
{
v22 = (unsigned __int64 *)(sspfd_s + 8 * num_stores);
*v22 = func0();
v28 = 0LL;
if ( (double)0 < 1000.0 )
{
do
{
if ( (++v28 & 0x8000000000000000LL) != 0LL )
v21 = (double)(int)(v28 & 1 | (v28 >> 1)) + (double)(int)(v28 & 1 | (v28 >> 1));
else
v21 = (double)(int)v28;
}
while ( v21 < 1000.0 );
}
*(_QWORD *)(*(_QWORD *)(sspfd_store + 8 * num_stores) + 8 * v9++) = func0()
- (sspfd_correction
+ *(_QWORD *)(sspfd_s + 8 * num_stores));
v8 = num_ops;
}
while ( num_ops > v9 );
}
}
else
{
v23 = sspfd_get_id();
printf("[%02lu] ", v23);
printf("** L1 access");
putchar(10);
fflush(stdout);
v8 = num_ops;
if ( num_ops )
{
v24 = 0LL;
do
{
v25 = (unsigned __int64 *)(sspfd_s + 8 * num_stores);
*v25 = func0();
LODWORD(v25) = *v6;
_mm_lfence();
*(_QWORD *)(*(_QWORD *)(sspfd_store + 8 * num_stores) + 8 * v24) = func0()
- (sspfd_correction
+ *(_QWORD *)(sspfd_s + 8 * num_stores));
if ( (_DWORD)v25 != 13 )
{
v26 = sspfd_get_id();
printf("[%02lu] ", v26);
printf("what?");
putchar(10);
fflush(stdout);
}
++v24;
v8 = num_ops;
}
while ( num_ops > v24 );
}
}
}
else if ( num_stores )
{
v15 = sspfd_get_id();
printf("[%02lu] ", v15);
printf("** asm volatile(\"mfence\")");
putchar(10);
fflush(stdout);
v8 = num_ops;
if ( num_ops )
{
v16 = 0LL;
do
{
v17 = (unsigned __int64 *)(sspfd_s + 8 * num_stores);
*v17 = func0();
_mm_mfence();
*(_QWORD *)(*(_QWORD *)(sspfd_store + 8 * num_stores) + 8 * v16++) = func0()
- (sspfd_correction
+ *(_QWORD *)(sspfd_s + 8 * num_stores));
v8 = num_ops;
}
while ( num_ops > v16 );
}
}
else
{
v10 = sspfd_get_id();
printf("[%02lu] ", v10);
printf("** asm volatile(\"nop\")");
putchar(10);
fflush(stdout);
v8 = num_ops;
if ( num_ops )
{
do
{
v11 = (unsigned __int64 *)(sspfd_s + 8 * num_stores);
*v11 = func0();
*(_QWORD *)(*(_QWORD *)(sspfd_store + 8 * num_stores) + 8 * v12++) = func0()
- (sspfd_correction
+ *(_QWORD *)(sspfd_s + 8 * num_stores));
v8 = num_ops;
}
while ( num_ops > v12 );
}
}
sspfd_get_stats(num_stores, v8, v29);
sspfd_print_stats(v29);
}
sspfd_store_term();
free(v6);
return 0;
}

original_code:
int
main(int argc, char **argv)
{
  struct option long_options[] =
    {
      {"help", 0, ((void *)0), 'h'},
      {"num_ops", 1, ((void *)0), 'o'},
      {"num_stores", 1, ((void *)0), 's'},
      {((void *)0), 0, ((void *)0), 0}
    };
  int i, c;
  while(1)
    {
      i = 0;
      c = getopt_long(argc, argv, "ho:s:", long_options, &i);
      if(c == -1)
 break;
      if(c == 0 && long_options[i].flag == 0)
 c = long_options[i].val;
      switch(c) {
      case 0:
 break;
      case 'h':
 printf("sspfd_test -- testing sspfd\n"
        "\n"
        "Usage:\n"
        "  sspfd_test [options...]\n"
        "\n"
        "Options:\n"
        "  -h, --help\n"
        "        Print this message\n"
        "  -o, --num_ops <int>\n"
        "        Test for number of operations (0=infinite, default=" "100000" ")\n"
        "  -s, --num_stores <int>\n"
        "        number of sspfd stores to be used (default=" "DEFAULT_DSL_PER_CORE" ")\n"
        );
 exit(0);
      case 'o':
 num_ops = atoi(optarg);
 break;
      case 's':
 num_stores = atoi(optarg);
 break;
      case '?':
 printf("Use -h or --help for help\n");
 exit(0);
      default:
 exit(1);
      }
    }
  ((void) sizeof ((num_stores > 0) ? 1 : 0), __extension__ ({ if (num_stores > 0) ; else __assert_fail ("num_stores > 0", "sspfd_test.c", 110, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((num_ops > 0) ? 1 : 0), __extension__ ({ if (num_ops > 0) ; else __assert_fail ("num_ops > 0", "sspfd_test.c", 111, __extension__ __PRETTY_FUNCTION__); }));
  printf("[%02lu] ", sspfd_get_id()); printf("* initializing %lu stores of %lu entries", num_stores, num_ops); printf("\n"); fflush(stdout);
  sspfd_store_init(num_stores, num_ops, 0);
  volatile int* dummy = (volatile int*) malloc(sizeof(int));
  ((void) sizeof ((dummy != ((void *)0)) ? 1 : 0), __extension__ ({ if (dummy != ((void *)0)) ; else __assert_fail ("dummy != NULL", "sspfd_test.c", 117, __extension__ __PRETTY_FUNCTION__); }));
  *dummy = 13;
  while (num_stores-- > 0)
    {
      printf("[%02lu] ", sspfd_get_id()); printf("* testing store %lu", num_stores); printf("\n"); fflush(stdout);
      switch(num_stores)
 {
 case 0:
   {
     printf("[%02lu] ", sspfd_get_id()); printf("** asm volatile(\"nop\")"); printf("\n"); fflush(stdout);
     size_t r;
     for (r = 0; r < num_ops; r++)
       {
  { asm volatile (""); _sspfd_s[num_stores] = getticks();;
  asm volatile ("nop");
  asm volatile (""); sspfd_store[num_stores][r] = getticks() - _sspfd_s[num_stores] - sspfd_correction; };
       }
     sspfd_stats_t stats;
     sspfd_get_stats(num_stores, num_ops, &stats);
     sspfd_print_stats(&stats);
   }
   break;
 case 1:
   {
     printf("[%02lu] ", sspfd_get_id()); printf("** asm volatile(\"mfence\")"); printf("\n"); fflush(stdout);
     size_t r;
     for (r = 0; r < num_ops; r++)
       {
  { asm volatile (""); _sspfd_s[num_stores] = getticks();;
  asm volatile ("mfence");
  asm volatile (""); sspfd_store[num_stores][r] = getticks() - _sspfd_s[num_stores] - sspfd_correction; };
       }
     sspfd_stats_t stats;
     sspfd_get_stats(num_stores, num_ops, &stats);
     sspfd_print_stats(&stats);
   }
   break;
 case 2:
   {
     printf("[%02lu] ", sspfd_get_id()); printf("** asm volatile(\"pause\")"); printf("\n"); fflush(stdout);
     size_t r;
     for (r = 0; r < num_ops; r++)
       {
  { asm volatile (""); _sspfd_s[num_stores] = getticks();;
  asm volatile ("pause");
  asm volatile (""); sspfd_store[num_stores][r] = getticks() - _sspfd_s[num_stores] - sspfd_correction; };
       }
     sspfd_stats_t stats;
     sspfd_get_stats(num_stores, num_ops, &stats);
     sspfd_print_stats(&stats);
   }
   break;
 case 3:
   {
     printf("[%02lu] ", sspfd_get_id()); printf("** for (rr = 0; rr < 1e3; rr++);"); printf("\n"); fflush(stdout);
     size_t r;
     for (r = 0; r < num_ops; r++)
       {
  { asm volatile (""); _sspfd_s[num_stores] = getticks();;
  volatile size_t rr;
  for (rr = 0; rr < 1e3; rr++);
  asm volatile (""); sspfd_store[num_stores][r] = getticks() - _sspfd_s[num_stores] - sspfd_correction; };
       }
     sspfd_stats_t stats;
     sspfd_get_stats(num_stores, num_ops, &stats);
     sspfd_print_stats(&stats);
   }
   break;
 default:
   {
     printf("[%02lu] ", sspfd_get_id()); printf("** L1 access"); printf("\n"); fflush(stdout);
     size_t r;
     for (r = 0; r < num_ops; r++)
       {
  int tmp = 0;
  { asm volatile (""); _sspfd_s[num_stores] = getticks();;
  tmp = *dummy;
  asm volatile ("lfence");
  asm volatile (""); sspfd_store[num_stores][r] = getticks() - _sspfd_s[num_stores] - sspfd_correction; };
  if (tmp != 13)
    {
      printf("[%02lu] ", sspfd_get_id()); printf("what?"); printf("\n"); fflush(stdout);
    }
       }
     sspfd_stats_t stats;
     sspfd_get_stats(num_stores, num_ops, &stats);
     sspfd_print_stats(&stats);
   }
   break;
 }
    }
  sspfd_store_term();
  free((void*) dummy);
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  struct option long_options[] = {
      {"help", 0, ((void *)0), 'h'},
      {"num_ops", 1, ((void *)0), 'o'},
      {"num_stores", 1, ((void *)0), 's'},
      {((void *)0), 0, ((void *)0), 0}
    };
  int i; int c;
  while(1)
    {
      i = 0;
      c = getopt_long(argc, argv, "ho:s:", long_options, &i);
      if(c == -1)
 break;
      if(c == 0 && long_options[i].flag == 0)
 c = long_options[i].val;
      switch(c) {
      case 0:
 break;
      case 'h':
 printf("sspfd_test -- testing sspfd\n"
        "\n"
        "Usage:\n"
        "  sspfd_test [options...]\n"
        "\n"
        "Options:\n"
        "  -h, --help\n"
        "        Print this message\n"
        "  -o, --num_ops <int>\n"
        "        Test for number of operations (0=infinite, default=" "100000" ")\n"
        "  -s, --num_stores <int>\n"
        "        number of sspfd stores to be used (default=" "DEFAULT_DSL_PER_CORE" ")\n"
        );
 exit(0);
      case 'o':
 num_ops = atoi(optarg);
 break;
      case 's':
 num_stores = atoi(optarg);
 break;
      case '?':
 printf("Use -h or --help for help\n");
 exit(0);
      default:
 exit(1);
      }
    }
  ((void) sizeof ((num_stores > 0) ? 1 : 0), __extension__ ({ if (num_stores > 0) ; else __assert_fail ("num_stores > 0", "sspfd_test.c", 110, __extension__ __PRETTY_FUNCTION__); }));
  ((void) sizeof ((num_ops > 0) ? 1 : 0), __extension__ ({ if (num_ops > 0) ; else __assert_fail ("num_ops > 0", "sspfd_test.c", 111, __extension__ __PRETTY_FUNCTION__); }));
  printf("[%02lu] ", sspfd_get_id()); printf("* initializing %lu stores of %lu entries", num_stores, num_ops); printf("\n"); fflush(stdout);
  sspfd_store_init(num_stores, num_ops, 0);
  int *dummy = (volatile int*) malloc(sizeof(int));
  ((void) sizeof ((dummy != ((void *)0)) ? 1 : 0), __extension__ ({ if (dummy != ((void *)0)) ; else __assert_fail ("dummy != NULL", "sspfd_test.c", 117, __extension__ __PRETTY_FUNCTION__); }));
  *dummy = 13;
  while (num_stores-- > 0)
    {
      printf("[%02lu] ", sspfd_get_id()); printf("* testing store %lu", num_stores); printf("\n"); fflush(stdout);
      switch(num_stores)
 {
 case 0:
   {
     printf("[%02lu] ", sspfd_get_id()); printf("** asm volatile(\"nop\")"); printf("\n"); fflush(stdout);
     unsigned long r;
     for (r = 0; r < num_ops; r++)
       {
  { asm volatile (""); _sspfd_s[num_stores] = getticks();;
  asm volatile ("nop");
  asm volatile (""); sspfd_store[num_stores][r] = getticks() - _sspfd_s[num_stores] - sspfd_correction; };
       }
     struct sspfd_stats stats;
     sspfd_get_stats(num_stores, num_ops, &stats);
     sspfd_print_stats(&stats);
   }
   break;
 case 1:
   {
     printf("[%02lu] ", sspfd_get_id()); printf("** asm volatile(\"mfence\")"); printf("\n"); fflush(stdout);
     unsigned long r;
     for (r = 0; r < num_ops; r++)
       {
  { asm volatile (""); _sspfd_s[num_stores] = getticks();;
  asm volatile ("mfence");
  asm volatile (""); sspfd_store[num_stores][r] = getticks() - _sspfd_s[num_stores] - sspfd_correction; };
       }
     struct sspfd_stats stats;
     sspfd_get_stats(num_stores, num_ops, &stats);
     sspfd_print_stats(&stats);
   }
   break;
 case 2:
   {
     printf("[%02lu] ", sspfd_get_id()); printf("** asm volatile(\"pause\")"); printf("\n"); fflush(stdout);
     unsigned long r;
     for (r = 0; r < num_ops; r++)
       {
  { asm volatile (""); _sspfd_s[num_stores] = getticks();;
  asm volatile ("pause");
  asm volatile (""); sspfd_store[num_stores][r] = getticks() - _sspfd_s[num_stores] - sspfd_correction; };
       }
     struct sspfd_stats stats;
     sspfd_get_stats(num_stores, num_ops, &stats);
     sspfd_print_stats(&stats);
   }
   break;
 case 3:
   {
     printf("[%02lu] ", sspfd_get_id()); printf("** for (rr = 0; rr < 1e3; rr++);"); printf("\n"); fflush(stdout);
     unsigned long r;
     for (r = 0; r < num_ops; r++)
       {
  { asm volatile (""); _sspfd_s[num_stores] = getticks();;
  unsigned long rr;
  for (rr = 0; rr < 1e3; rr++);
  asm volatile (""); sspfd_store[num_stores][r] = getticks() - _sspfd_s[num_stores] - sspfd_correction; };
       }
     struct sspfd_stats stats;
     sspfd_get_stats(num_stores, num_ops, &stats);
     sspfd_print_stats(&stats);
   }
   break;
 default:
   {
     printf("[%02lu] ", sspfd_get_id()); printf("** L1 access"); printf("\n"); fflush(stdout);
     unsigned long r;
     for (r = 0; r < num_ops; r++)
       {
  int tmp = 0;
  { asm volatile (""); _sspfd_s[num_stores] = getticks();;
  tmp = *dummy;
  asm volatile ("lfence");
  asm volatile (""); sspfd_store[num_stores][r] = getticks() - _sspfd_s[num_stores] - sspfd_correction; };
  if (tmp != 13)
    {
      printf("[%02lu] ", sspfd_get_id()); printf("what?"); printf("\n"); fflush(stdout);
    }
       }
     struct sspfd_stats stats;
     sspfd_get_stats(num_stores, num_ops, &stats);
     sspfd_print_stats(&stats);
   }
   break;
 }
    }
  sspfd_store_term();
  free((void *) dummy);
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

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

variable_types: c int

variable_types:
dummy
int *

variable_types:
r
unsigned long

variable_types: struct sspfd_stats { unsigned long num_vals; double avg; double avg_10p; double avg_25p; double avg_50p; double avg_75p; double avg_rst; double abs_dev_10p; double abs_dev_25p; double abs_dev_50p; double abs_dev_75p; double abs_dev_rst; double abs_dev; double abs_dev_perc; double std_dev_10p; double std_dev_25p; double std_dev_50p; double std_dev_75p; double std_dev_rst; double std_dev; double std_dev_perc; double min_val; unsigned long min_val_idx; double max_val; unsigned long max_val_idx; unsigned int num_dev_10p; unsigned int num_dev_25p; unsigned int num_dev_50p; unsigned int num_dev_75p; unsigned int num_dev_rst; }

variable_types:
rr
unsigned long

variable_types: tmp int


decompiled_code:
void __fastcall tm2c_shmalloc_set(__int64 a1)
{
tm2c_app_mem = a1;
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1)
{
tm2c_app_mem = a1;
}

original_code:
void
tm2c_shmalloc_set(void* mem)
{
  tm2c_app_mem = mem;
}

canonical_original_code:
void tm2c_shmalloc_set(void *mem)
{
  tm2c_app_mem = mem;
}

variable_types:
mem
void *

return_type: void


decompiled_code:
void *__fastcall tm2c_shmalloc_init(__off_t length)
{
int v1; // eax
int v2; // ebx
void *result; // rax
char v4[136]; // [rsp+0h] [rbp-88h] BYREF

strcpy(v4, "/tm2c_mem2");
v1 = shm_open(v4, 194, 0x1F8u);
if ( v1 >= 0 )
{
v2 = v1;
ftruncate(v1, length);
}
else
{
if ( *_errno_location() != 17 )
{
perror("In shm_open");
exit(1);
}
v2 = shm_open(v4, 66, 0x1F8u);
if ( v2 < 0 )
{
perror("In shm_open");
exit(1);
}
}
result = mmap(0LL, length, 3, 1, v2, 0LL);
if ( !result )
_assert_fail("mem != NULL", "src/tm2c_malloc.c", 0x63u, "tm2c_shmalloc_init");
tm2c_app_mem = (__int64)result;
return result;
}

canonical_decompiled_code:
void *__fastcall func1(__off_t length)
{
int v1; // eax
int v2; // ebx
void *result; // rax
char v4[136]; // [rsp+0h] [rbp-88h] BYREF

strcpy(v4, "/tm2c_mem2");
v1 = shm_open(v4, 194, 0x1F8u);
if ( v1 >= 0 )
{
v2 = v1;
ftruncate(v1, length);
}
else
{
if ( *_errno_location() != 17 )
{
perror("In shm_open");
exit(1);
}
v2 = shm_open(v4, 66, 0x1F8u);
if ( v2 < 0 )
{
perror("In shm_open");
exit(1);
}
}
result = mmap(0LL, length, 3, 1, v2, 0LL);
if ( !result )
_assert_fail("mem != NULL", "src/tm2c_malloc.c", 0x63u, "tm2c_shmalloc_init");
tm2c_app_mem = (__int64)result;
return result;
}

original_code:
void
tm2c_shmalloc_init(size_t size)
{
   char keyF[100];
   sprintf(keyF,"/tm2c_mem2");
   int shmfd = shm_open(keyF, 0100 | 0200 | 02, (0400|0200|0100) | ((0400|0200|0100) >> 3));
   if (shmfd<0)
   {
      if ((*__errno_location ()) != 17)
      {
         perror("In shm_open");
         exit(1);
      }
      shmfd = shm_open(keyF, 0100 | 02, (0400|0200|0100) | ((0400|0200|0100) >> 3));
      if (shmfd<0)
      {
         perror("In shm_open");
         exit(1);
      }
   }
   else
   {
     if (!ftruncate(shmfd,size))
       {
       }
   }
  void* mem = (void*) mmap(((void *)0), size, 0x1 | 0x2, 0x01, shmfd, 0);
  ((void) sizeof ((mem != ((void *)0)) ? 1 : 0), __extension__ ({ if (mem != ((void *)0)) ; else __assert_fail ("mem != NULL", "src/tm2c_malloc.c", 99, __extension__ __PRETTY_FUNCTION__); }));
  tm2c_app_mem = (void*) mem;
}

canonical_original_code:
void tm2c_shmalloc_init(unsigned long size)
{
   char keyF[100];
   sprintf(keyF,"/tm2c_mem2");
   int shmfd = shm_open(keyF, 0100 | 0200 | 02, (0400|0200|0100) | ((0400|0200|0100) >> 3));
   if (shmfd<0)
   {
      if ((*__errno_location ()) != 17)
      {
         perror("In shm_open");
         exit(1);
      }
      shmfd = shm_open(keyF, 0100 | 02, (0400|0200|0100) | ((0400|0200|0100) >> 3));
      if (shmfd<0)
      {
         perror("In shm_open");
         exit(1);
      }
   }
   else
   {
     if (!ftruncate(shmfd,size))
       {
       }
   }
  void *mem = (void*) mmap(((void *)0), size, 0x1 | 0x2, 0x01, shmfd, 0);
  ((void) sizeof ((mem != ((void *)0)) ? 1 : 0), __extension__ ({ if (mem != ((void *)0)) ; else __assert_fail ("mem != NULL", "src/tm2c_malloc.c", 99, __extension__ __PRETTY_FUNCTION__); }));
  tm2c_app_mem = (void *) mem;
}

variable_types:
size
unsigned long

variable_types:
keyF
char [100]

variable_types: shmfd int


decompiled_code:
int tm2c_shmalloc_term()
{
char v1[120]; // [rsp+0h] [rbp-78h] BYREF

strcpy(v1, "/tm2c_mem2");
return shm_unlink(v1);
}

canonical_decompiled_code:
int func2()
{
char v1[120]; // [rsp+0h] [rbp-78h] BYREF

strcpy(v1, "/tm2c_mem2");
return shm_unlink(v1);
}

original_code:
void
tm2c_shmalloc_term()
{
   char keyF[100];
   sprintf(keyF,"/tm2c_mem2");
   shm_unlink(keyF);
}


decompiled_code:
__int64 __fastcall tm2c_shmalloc(__int64 a1)
{
__int64 v1; // rdx

if ( (unsigned __int8)tm2c_free_num > 2u )
return tm2c_free_list[(unsigned __int8)(tm2c_free_cur - tm2c_free_num--)];
v1 = tm2c_app_mem + alloc_next;
alloc_next += a1;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 v1; // rdx

if ( (unsigned __int8)tm2c_free_num > 2u )
return tm2c_free_list[(unsigned __int8)(tm2c_free_cur - tm2c_free_num--)];
v1 = tm2c_app_mem + alloc_next;
alloc_next += a1;
return v1;
}

original_code:
void*
tm2c_shmalloc(size_t size)
{
  void *ret;
  if (tm2c_free_num > 2)
    {
      uint8_t spot = tm2c_free_cur - tm2c_free_num;
      ret = tm2c_free_list[spot];
      tm2c_free_num--;
    }
  else
    {
      ret = tm2c_app_mem + alloc_next;
      alloc_next += size;
    }
  return (void*) ret;
}

canonical_original_code:
void *tm2c_shmalloc(unsigned long size)
{
  void *ret;
  if (tm2c_free_num > 2)
    {
      unsigned char spot = tm2c_free_cur - tm2c_free_num;
      ret = tm2c_free_list[spot];
      tm2c_free_num--;
    }
  else
    {
      ret = tm2c_app_mem + alloc_next;
      alloc_next += size;
    }
  return (void *) ret;
}

variable_types:
ret
void *

variable_types:
spot
unsigned char

return_type: void *


decompiled_code:
__int64 __fastcall tm2c_shfree(__int64 a1)
{
__int64 result; // rax

++tm2c_free_num;
LOBYTE(result) = tm2c_free_cur++;
result = (unsigned __int8)result;
tm2c_free_list[(unsigned __int8)result] = a1;
return result;
}

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

++tm2c_free_num;
LOBYTE(result) = tm2c_free_cur++;
result = (unsigned __int8)result;
tm2c_free_list[(unsigned __int8)result] = a1;
return result;
}

original_code:
void
tm2c_shfree(void* ptr)
{
  tm2c_free_num++;
  tm2c_free_list[tm2c_free_cur++] = (void*) ptr;
}

canonical_original_code:
void tm2c_shfree(void *ptr)
{
  tm2c_free_num++;
  tm2c_free_list[tm2c_free_cur++] = (void *) ptr;
}

variable_types:
ptr
void *


repo: triposorbust/fractal-graph

decompiled_code:
__int64 __fastcall new_node(double a1, double a2)
{
unsigned int v2; // edx
__int64 v3; // rax

v2 = index_0;
v3 = 24LL * index_0;
*(double *)((char *)NODES + v3) = a1;
*(double *)((char *)NODES + v3 + 8) = a2;
index_0 = v2 + 1;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(double a1, double a2)
{
unsigned int v2; // edx
__int64 v3; // rax

v2 = index_0;
v3 = 24LL * index_0;
*(double *)((char *)NODES + v3) = a1;
*(double *)((char *)NODES + v3 + 8) = a2;
index_0 = v2 + 1;
return v2;
}

original_code:
int new_node(double x, double y)
{
  static int index = 0;
  NODES[index].x = x;
  NODES[index].y = y;
  return index++;
}

variable_types: x double

variable_types: y double

variable_types: index int

return_type: int


decompiled_code:
__int64 __fastcall set_up(int a1, int a2)
{
__int64 v2; // rbx
char *v3; // rbp
__int64 i; // rax
_DWORD *v5; // rbx
_DWORD *v6; // rbx
_DWORD *v7; // rbx
__int64 result; // rax

LATTICE = malloc(12LL * a1);
NODES = malloc(24LL * a2);
if ( a2 > 0 )
{
v2 = 0LL;
do
{
v3 = (char *)NODES + v2;
*((_QWORD *)v3 + 2) = malloc(0x18uLL);
for ( i = 0LL; i != 24; i += 4LL )
*(_DWORD *)(*(_QWORD *)((char *)NODES + v2 + 16) + i) = -1;
v2 += 24LL;
}
while ( v2 != 24LL * (unsigned int)a2 );
}
v5 = LATTICE;
*v5 = new_node(0.0, 1.0);
v6 = LATTICE;
v6[1] = new_node(0.866, 0.5);
v7 = LATTICE;
result = new_node(-0.866, 0.5);
v7[2] = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, int a2)
{
__int64 v2; // rbx
char *v3; // rbp
__int64 i; // rax
_DWORD *v5; // rbx
_DWORD *v6; // rbx
_DWORD *v7; // rbx
__int64 result; // rax

LATTICE = malloc(12LL * a1);
NODES = malloc(24LL * a2);
if ( a2 > 0 )
{
v2 = 0LL;
do
{
v3 = (char *)NODES + v2;
*((_QWORD *)v3 + 2) = malloc(0x18uLL);
for ( i = 0LL; i != 24; i += 4LL )
*(_DWORD *)(*(_QWORD *)((char *)NODES + v2 + 16) + i) = -1;
v2 += 24LL;
}
while ( v2 != 24LL * (unsigned int)a2 );
}
v5 = LATTICE;
*v5 = func1(0.0, 1.0);
v6 = LATTICE;
v6[1] = func1(0.866, 0.5);
v7 = LATTICE;
result = func1(-0.866, 0.5);
v7[2] = result;
return result;
}

original_code:
void set_up(int n_triangles, int n_points)
{
  int i,j;
  LATTICE = (triangle *) malloc(n_triangles * sizeof(triangle));
  NODES = (node *) malloc(n_points * sizeof(node));
  for (i=0; i<n_points; i++) {
    NODES[i].edges = (int *) malloc(6 * sizeof(int));
    for (j=0; j<6; j++)
      NODES[i].edges[j] = -1;
  }
  LATTICE[0].a = new_node( 0.0, 1.0);
  LATTICE[0].b = new_node( 0.866, 0.5);
  LATTICE[0].c = new_node(-0.866, 0.5);
}

canonical_original_code:
void set_up(int n_triangles, int n_points)
{
  int i; int j;
  LATTICE = (struct Triangle *) malloc(n_triangles * sizeof(triangle));
  NODES = (struct Node *) malloc(n_points * sizeof(node));
  for (i=0; i<n_points; i++) {
    NODES[i].edges = (int *) malloc(6 * sizeof(int));
    for (j=0; j<6; j++)
      NODES[i].edges[j] = -1;
  }
  LATTICE[0].a = new_node( 0.0, 1.0);
  LATTICE[0].b = new_node( 0.866, 0.5);
  LATTICE[0].c = new_node(-0.866, 0.5);
}

variable_types: i int

variable_types: j int

user_defined_types:
struct Triangle { int a; }
struct Node { double x; int *edges; }


original_code:
void add_edge(int src, int dest)
{
  int j=0,k=0;
  while (NODES[src].edges[j] != -1) j++;
  while (NODES[dest].edges[k] != -1) k++;
  NODES[src].edges[j] = dest;
  NODES[dest].edges[k] = src;
}

canonical_original_code:
void add_edge(int src, int dest)
{
  int j = 0; int k = 0;
  while (NODES[src].edges[j] != -1) j++;
  while (NODES[dest].edges[k] != -1) k++;
  NODES[src].edges[j] = dest;
  NODES[dest].edges[k] = src;
}

variable_types: src int

variable_types: dest int

variable_types: k int


decompiled_code:
__int64 __fastcall read_lattice(int a1)
{
__int64 v1; // rbx
int v2; // er13
int v3; // ebp
int v4; // er12
__int64 result; // rax

if ( a1 > 0 )
{
v1 = 0LL;
do
{
v2 = *(_DWORD *)((char *)LATTICE + v1);
v3 = *(_DWORD *)((char *)LATTICE + v1 + 4);
v4 = *(_DWORD *)((char *)LATTICE + v1 + 8);
add_edge(v2, v3);
add_edge(v2, v4);
result = add_edge(v3, v4);
v1 += 12LL;
}
while ( v1 != 12LL * (unsigned int)a1 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
__int64 v1; // rbx
int v2; // er13
int v3; // ebp
int v4; // er12
__int64 result; // rax

if ( a1 > 0 )
{
v1 = 0LL;
do
{
v2 = *(_DWORD *)((char *)LATTICE + v1);
v3 = *(_DWORD *)((char *)LATTICE + v1 + 4);
v4 = *(_DWORD *)((char *)LATTICE + v1 + 8);
func3(v2, v3);
func3(v2, v4);
result = func3(v3, v4);
v1 += 12LL;
}
while ( v1 != 12LL * (unsigned int)a1 );
}
return result;
}

original_code:
void read_lattice(int end)
{
  int i,a,b,c;
  for (i=0; i<end; i++) {
    a = LATTICE[i].a;
    b = LATTICE[i].b;
    c = LATTICE[i].c;
    add_edge(a,b);
    add_edge(a,c);
    add_edge(b,c);
  }
}

canonical_original_code:
void read_lattice(int end)
{
  int i; int a; int b; int c;
  for (i=0; i<end; i++) {
    a = LATTICE[i].a;
    b = LATTICE[i].b;
    c = LATTICE[i].c;
    add_edge(a,b);
    add_edge(a,c);
    add_edge(b,c);
  }
}

variable_types: end int

variable_types: a int

variable_types: b int

variable_types: c int


decompiled_code:
__int64 __fastcall node_between(int a1, int a2)
{
return new_node(
(*((double *)NODES + 3 * a1) + *((double *)NODES + 3 * a2)) * 0.5,
(*((double *)NODES + 3 * a1 + 1) + *((double *)NODES + 3 * a2 + 1)) * 0.5);
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1, int a2)
{
return func1(
(*((double *)NODES + 3 * a1) + *((double *)NODES + 3 * a2)) * 0.5,
(*((double *)NODES + 3 * a1 + 1) + *((double *)NODES + 3 * a2 + 1)) * 0.5);
}

original_code:
int node_between(int a_idx, int b_idx)
{
  double mx,my;
  mx = (NODES[a_idx].x + NODES[b_idx].x)/2.0;
  my = (NODES[a_idx].y + NODES[b_idx].y)/2.0;
  return new_node(mx, my);
}

canonical_original_code:
int node_between(int a_idx, int b_idx)
{
  double mx; double my;
  mx = (NODES[a_idx].x + NODES[b_idx].x)/2.0;
  my = (NODES[a_idx].y + NODES[b_idx].y)/2.0;
  return new_node(mx, my);
}

variable_types: mx double

variable_types: my double


decompiled_code:
void __fastcall build_lattice(int a1, unsigned int a2, int a3)
{
void *v4; // rbp
__int64 v5; // rbx

if ( a1 )
{
v4 = malloc(12LL * (int)a2);
if ( a3 > 0 )
{
v5 = 0LL;
do
{
build_inner_triangles(
*(_QWORD *)((char *)LATTICE + 12 * v5),
*((_DWORD *)LATTICE + 3 * v5 + 2),
v5,
(__int64)v4);
++v5;
}
while ( v5 != a3 );
}
free(LATTICE);
LATTICE = v4;
build_lattice((unsigned int)(a1 - 1), a2, (unsigned int)(3 * a3));
}
}

canonical_decompiled_code:
void __fastcall func6(int a1, unsigned int a2, int a3)
{
void *v4; // rbp
__int64 v5; // rbx

if ( a1 )
{
v4 = malloc(12LL * (int)a2);
if ( a3 > 0 )
{
v5 = 0LL;
do
{
build_inner_triangles(
*(_QWORD *)((char *)LATTICE + 12 * v5),
*((_DWORD *)LATTICE + 3 * v5 + 2),
v5,
(__int64)v4);
++v5;
}
while ( v5 != a3 );
}
free(LATTICE);
LATTICE = v4;
func6((unsigned int)(a1 - 1), a2, (unsigned int)(3 * a3));
}
}

original_code:
void build_lattice(int depth, int n_triangles, int end_idx)
{
  if (depth == 0)
    return;
  int i=0;
  triangle *nlattice = (triangle *) malloc(n_triangles * sizeof(triangle));
  for (i=0; i<end_idx; i++)
    build_inner_triangles(LATTICE[i], i, nlattice);
  free(LATTICE);
  LATTICE = nlattice;
  build_lattice(depth-1, n_triangles, 3*end_idx);
}

canonical_original_code:
void build_lattice(int depth, int n_triangles, int end_idx)
{
  if (depth == 0)
    return;
  int i = 0;
  struct Triangle *nlattice = (triangle *) malloc(n_triangles * sizeof(triangle));
  for (i=0; i<end_idx; i++)
    build_inner_triangles(LATTICE[i], i, nlattice);
  free(LATTICE);
  LATTICE = nlattice;
  build_lattice(depth-1, n_triangles, 3*end_idx);
}

variable_types: depth int

variable_types:
nlattice
struct Triangle { int a; } *


decompiled_code:
void free_graph()
{
free(NODES);
}

canonical_decompiled_code:
void func7()
{
free(NODES);
}

original_code:
void free_graph(void)
{
  int i, n_points = sizeof(NODES) / sizeof(node);
  for (i=0; i<n_points; i++)
    free(NODES[i].edges);
  free(NODES);
}

canonical_original_code:
void free_graph()
{
  int i; int n_points = sizeof(NODES) / sizeof(node);
  for (i=0; i<n_points; i++)
    free(NODES[i].edges);
  free(NODES);
}


decompiled_code:
__int64 __fastcall build_graph(int a1)
{
unsigned int v2; // [rsp+8h] [rbp-10h] BYREF
unsigned int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

compute_dimensions(a1, v3, (int *)&v2);
set_up(v3[0], v2);
build_lattice(a1, v3[0], 1);
read_lattice(v3[0]);
free(LATTICE);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1)
{
unsigned int v2; // [rsp+8h] [rbp-10h] BYREF
unsigned int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

func0(a1, v3, (int *)&v2);
func2(v3[0], v2);
func6(a1, v3[0], 1);
func4(v3[0]);
free(LATTICE);
return v2;
}

original_code:
int build_graph(int depth)
{
  int n_triangles, n_points;
  compute_dimensions(depth, &n_triangles, &n_points);
  set_up(n_triangles, n_points);
  build_lattice(depth, n_triangles, 1);
  read_lattice(n_triangles);
  free(LATTICE);
  return n_points;
}

canonical_original_code:
int build_graph(int depth)
{
  int n_triangles; int n_points;
  compute_dimensions(depth, &n_triangles, &n_points);
  set_up(n_triangles, n_points);
  build_lattice(depth, n_triangles, 1);
  read_lattice(n_triangles);
  free(LATTICE);
  return n_points;
}


original_code:
void distribute_densities(int n_nodes, double *densities, node *nodes)
{
  int i,j,n_edges;
  double amount;
  double *udensities = (double *) malloc(n_nodes * sizeof(double));
  for (i=0; i<n_nodes; i++)
    udensities[i] = 0.0;
  for (n_edges=0, i=0; i<n_nodes; i++) {
    while (nodes[i].edges[n_edges] != -1) n_edges++;
    amount = densities[i] / (float) n_edges;
    for (j=0; j<n_edges; j++)
      udensities[ nodes[i].edges[j] ] += amount;
  }
  for (i=0; i<n_nodes; i++)
    densities[i] = udensities[i];
  free(udensities);
  return;
}

canonical_original_code:
void distribute_densities(int n_nodes, double *densities, struct Node *nodes)
{
  int i; int j; int n_edges;
  double amount;
  double *udensities = (double *) malloc(n_nodes * sizeof(double));
  for (i=0; i<n_nodes; i++)
    udensities[i] = 0.0;
  for (n_edges=0, i=0; i<n_nodes; i++) {
    while (nodes[i].edges[n_edges] != -1) n_edges++;
    amount = densities[i] / (float) n_edges;
    for (j=0; j<n_edges; j++)
      udensities[ nodes[i].edges[j] ] += amount;
  }
  for (i=0; i<n_nodes; i++)
    densities[i] = udensities[i];
  free(udensities);
  return;
}

variable_types:
densities
double *

variable_types:
nodes
struct Node { double x; int *edges; } *

variable_types: amount double

variable_types:
udensities
double *


decompiled_code:
void __fastcall compute_densities(double *a1, int a2, int a3, int a4)
{
double *v6; // rax
int i; // er14
int v8; // er12
double *v9; // rax

if ( a2 > 0 )
{
v6 = a1;
do
*v6++ = 0.0;
while ( v6 != &a1[(unsigned int)(a2 - 1) + 1] );
}
if ( a2 < a4 )
_assert_fail("number <= n_nodes", "densities.c", 0x25u, "compute_densities");
for ( i = 0; a4 > i; ++i )
{
v9 = &a1[rand() % a2];
if ( 1.0 == *v9 )
--i;
else
*(_QWORD *)v9 = LC3;
}
if ( a3 > 0 )
{
v8 = 0;
do
{
distribute_densities(a2, a1, NODES);
++v8;
}
while ( a3 != v8 );
}
}

canonical_decompiled_code:
void __fastcall func1(double *a1, int a2, int a3, int a4)
{
double *v6; // rax
int i; // er14
int v8; // er12
double *v9; // rax

if ( a2 > 0 )
{
v6 = a1;
do
*v6++ = 0.0;
while ( v6 != &a1[(unsigned int)(a2 - 1) + 1] );
}
if ( a2 < a4 )
_assert_fail("number <= n_nodes", "densities.c", 0x25u, "compute_densities");
for ( i = 0; a4 > i; ++i )
{
v9 = &a1[rand() % a2];
if ( 1.0 == *v9 )
--i;
else
*(_QWORD *)v9 = LC3;
}
if ( a3 > 0 )
{
v8 = 0;
do
{
func0(a2, a1, NODES);
++v8;
}
while ( a3 != v8 );
}
}

original_code:
void compute_densities(double *densities, int n_nodes, int steps, int number)
{
  int i, step, temp;
  for (i=0; i<n_nodes; i++)
    densities[i] = 0.0;
  ((void) sizeof ((number <= n_nodes) ? 1 : 0), __extension__ ({ if (number <= n_nodes) ; else __assert_fail ("number <= n_nodes", "densities.c", 37, __extension__ __PRETTY_FUNCTION__); }));
  for (i=0; i<number; i++) {
    temp = rand()%n_nodes;
    if (densities[temp] == 1.0) {
      i--;
      continue;
    }
    densities[temp] = 1.0;
  }
  for (step=0; step<steps; step++)
    distribute_densities(n_nodes, densities, NODES);
  return;
}

canonical_original_code:
void compute_densities(double *densities, int n_nodes, int steps, int number)
{
  int i; int step; int temp;
  for (i=0; i<n_nodes; i++)
    densities[i] = 0.0;
  ((void) sizeof ((number <= n_nodes) ? 1 : 0), __extension__ ({ if (number <= n_nodes) ; else __assert_fail ("number <= n_nodes", "densities.c", 37, __extension__ __PRETTY_FUNCTION__); }));
  for (i=0; i<number; i++) {
    temp = rand()%n_nodes;
    if (densities[temp] == 1.0) {
      i--;
      continue;
    }
    densities[temp] = 1.0;
  }
  for (step=0; step<steps; step++)
    distribute_densities(n_nodes, densities, NODES);
  return;
}

variable_types: steps int

variable_types: number int

variable_types: i int

variable_types: step int

variable_types: temp int


decompiled_code:
int print_help()
{
__int64 i; // rbx

puts("\nProgram for executing random walk on a lattice. Options:\n");
for ( i = 0LL; i != 5; ++i )
printf("-%c\t\t%s\n", (unsigned int)LETTERS[i], (&DESCRIPTIONS)[i]);
return puts("\nHow you use it is up to you!\n");
}

canonical_decompiled_code:
int func0()
{
__int64 i; // rbx

puts("\nProgram for executing random walk on a lattice. Options:\n");
for ( i = 0LL; i != 5; ++i )
printf("-%c\t\t%s\n", (unsigned int)LETTERS[i], (&DESCRIPTIONS)[i]);
return puts("\nHow you use it is up to you!\n");
}

original_code:
void print_help(void)
{
  int i;
  printf("\nProgram for executing random walk on a lattice. Options:\n\n");
  for (i=0; i<5; i++) {
    printf("-%c\t\t%s\n",
           LETTERS[i],
           DESCRIPTIONS[i]);
  }
  printf("\nHow you use it is up to you!\n\n");
  return;
}

canonical_original_code:
void print_help()
{
  int i;
  printf("\nProgram for executing random walk on a lattice. Options:\n\n");
  for (i=0; i<5; i++) {
    printf("-%c\t\t%s\n",
           LETTERS[i],
           DESCRIPTIONS[i]);
  }
  printf("\nHow you use it is up to you!\n\n");
  return;
}


original_code:
void random_walk(walk *walks, int end)
{
  int i,j,next_node;
  node anode;
  for (i=0; i<end; ++i) {
    anode = NODES[(*(walks+i)).loc];
    next_node = anode.edges[0];
    for (j=0; anode.edges[j] != -1; ++j)
      next_node = (rand() % (j+1) == 0) ? anode.edges[j] : next_node;
    (*(walks+i)).loc = next_node;
    (*(walks+i)).step += 1;
    (*(walks+i)).x = NODES[(*(walks+i)).loc].x;
    (*(walks+i)).y = NODES[(*(walks+i)).loc].y;
  }
}

canonical_original_code:
void random_walk(struct Walk *walks, int end)
{
  int i; int j; int next_node;
  struct Node anode;
  for (i=0; i<end; ++i) {
    anode = NODES[(*(walks+i)).loc];
    next_node = anode.edges[0];
    for (j=0; anode.edges[j] != -1; ++j)
      next_node = (rand() % (j+1) == 0) ? anode.edges[j] : next_node;
    (*(walks+i)).loc = next_node;
    (*(walks+i)).step += 1;
    (*(walks+i)).x = NODES[(*(walks+i)).loc].x;
    (*(walks+i)).y = NODES[(*(walks+i)).loc].y;
  }
}

variable_types: struct Walk { int index; double x; int step; int loc; } *

variable_types: end int

variable_types: j int

variable_types:
anode
struct Node { double x; int *edges; }


decompiled_code:
size_t __fastcall get_datetime_string(char *s, int a2)
{
struct tm *v2; // rax
time_t timer; // [rsp+8h] [rbp-20h] BYREF

time(&timer);
v2 = localtime(&timer);
return strftime(s, a2, "%Y-%m-%d-%H%M", v2);
}

canonical_decompiled_code:
size_t __fastcall func1(char *s, int a2)
{
struct tm *v2; // rax
time_t timer; // [rsp+8h] [rbp-20h] BYREF

time(&timer);
v2 = localtime(&timer);
return strftime(s, a2, "%Y-%m-%d-%H%M", v2);
}

original_code:
void get_datetime_string(char *s, int max_chars)
{
  time_t rawtime;
  struct tm *timeinfo;
  time(&rawtime);
  timeinfo = localtime(&rawtime);
  strftime(s, max_chars, "%Y-%m-%d-%H%M", timeinfo);
}

canonical_original_code:
void get_datetime_string(char *s, int max_chars)
{
  long rawtime;
  struct tm *timeinfo;
  time(&rawtime);
  timeinfo = localtime(&rawtime);
  strftime(s, max_chars, "%Y-%m-%d-%H%M", timeinfo);
}

variable_types:
s
char *

variable_types: rawtime long

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; } *


original_code:
void initialize_walks(walk *walks, int n_walks, int n_nodes)
{
  int i;
  for (i=0; i<n_walks; i++) {
    (walks+i)->index = i;
    (walks+i)->step = 0;
    (walks+i)->loc = rand() % n_nodes;
    (walks+i)->x = NODES[walks[i].loc].x;
    (walks+i)->y = NODES[walks[i].loc].y;
  }
}

canonical_original_code:
void initialize_walks(struct Walk *walks, int n_walks, int n_nodes)
{
  int i;
  for (i=0; i<n_walks; i++) {
    (walks+i)->index = i;
    (walks+i)->step = 0;
    (walks+i)->loc = rand() % n_nodes;
    (walks+i)->x = NODES[walks[i].loc].x;
    (walks+i)->y = NODES[walks[i].loc].y;
  }
}

user_defined_types: struct Walk { int index; double x; int step; int loc; }


canonical_name: main

repo: triposorbust/zerochat

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v4; // [rsp+4h] [rbp-14h]
unsigned int v5; // [rsp+8h] [rbp-10h]
unsigned int v6; // [rsp+Ch] [rbp-Ch]

zmq_version();
printf("ZeroMQ v%d.%d.%d\n", v6, v5, v4);
return 0;
}

original_code:
int main(void)
{
  int major, minor, patch;
  zmq_version (&major, &minor, &patch);
  printf ("ZeroMQ v%d.%d.%d\n", major, minor, patch);
  return 0;
}

canonical_original_code:
int main()
{
  int major; int minor; int patch;
  zmq_version (&major, &minor, &patch);
  printf ("ZeroMQ v%d.%d.%d\n", major, minor, patch);
  return 0;
}

variable_types: major int

variable_types: minor int

variable_types: patch int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
double v4[2]; // [rsp+8h] [rbp-10h] BYREF

result = read_i2c("/dev/i2c-0", 72LL, v4);
if ( !result )
{
printf("%.1f", v4[0]);
result = 0;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int result; // eax
double v4[2]; // [rsp+8h] [rbp-10h] BYREF

result = func7("/dev/i2c-0", 72LL, v4);
if ( !result )
{
printf("%.1f", v4[0]);
result = 0;
}
return result;
}

original_code:
int main(int argc, char **argv)
{
    double temp;
    if(read_i2c("/dev/i2c-0", 0x48, &temp))
  return;
    printf("%.1f", temp);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: temp double


decompiled_code:
__int64 __fastcall read_i2c(const char *a1, unsigned int a2, double *a3)
{
int v4; // eax
int v5; // ebx
int *v6; // rax
char *v7; // rax
__int64 result; // rax
unsigned __int8 v9; // bp
int v10; // eax
int v11; // edx
int *v12; // rax
char *v13; // rax
char buf[34]; // [rsp+6h] [rbp-22h] BYREF

v4 = open(a1, 2);
if ( v4 < 0 )
{
fwrite("Failed to open i2c port\n", 1uLL, 0x18uLL, stderr);
result = 1LL;
}
else
{
v5 = v4;
if ( ioctl(v4, 0x703uLL, a2) < 0 )
{
fwrite("Unable to get bus access to talk to slave\n", 1uLL, 0x2AuLL, stderr);
result = 1LL;
}
else
{
buf[0] = 0;
if ( write(v5, buf, 1uLL) == 1 )
{
if ( read(v5, buf, 1uLL) == 1 && (v9 = buf[0], read(v5, buf, 1uLL) == 1) )
{
v10 = ((unsigned __int8)buf[0] | (v9 << 8)) >> 4;
v11 = v10;
if ( (v10 & 0x800) != 0 )
{
BYTE1(v11) = BYTE1(v10) | 0xF8;
v10 = v11;
}
*a3 = (double)v10 * 0.0625;
result = 0LL;
}
else
{
v12 = __errno_location();
v13 = strerror(*v12);
fprintf(stderr, "Unable to read from slave, %s\n", v13);
result = 1LL;
}
}
else
{
v6 = __errno_location();
v7 = strerror(*v6);
fprintf(stderr, "Error writing to i2c slave, %s\n", v7);
result = 1LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(const char *a1, unsigned int a2, double *a3)
{
int v4; // eax
int v5; // ebx
int *v6; // rax
char *v7; // rax
__int64 result; // rax
unsigned __int8 v9; // bp
int v10; // eax
int v11; // edx
int *v12; // rax
char *v13; // rax
char buf[34]; // [rsp+6h] [rbp-22h] BYREF

v4 = open(a1, 2);
if ( v4 < 0 )
{
fwrite("Failed to open i2c port\n", 1uLL, 0x18uLL, stderr);
result = 1LL;
}
else
{
v5 = v4;
if ( ioctl(v4, 0x703uLL, a2) < 0 )
{
fwrite("Unable to get bus access to talk to slave\n", 1uLL, 0x2AuLL, stderr);
result = 1LL;
}
else
{
buf[0] = 0;
if ( write(v5, buf, 1uLL) == 1 )
{
if ( read(v5, buf, 1uLL) == 1 && (v9 = buf[0], read(v5, buf, 1uLL) == 1) )
{
v10 = ((unsigned __int8)buf[0] | (v9 << 8)) >> 4;
v11 = v10;
if ( (v10 & 0x800) != 0 )
{
BYTE1(v11) = BYTE1(v10) | 0xF8;
v10 = v11;
}
*a3 = (double)v10 * 0.0625;
result = 0LL;
}
else
{
v12 = __errno_location();
v13 = strerror(*v12);
fprintf(stderr, "Unable to read from slave, %s\n", v13);
result = 1LL;
}
}
else
{
v6 = __errno_location();
v7 = strerror(*v6);
fprintf(stderr, "Error writing to i2c slave, %s\n", v7);
result = 1LL;
}
}
}
return result;
}

original_code:
int read_i2c(char* filename, int address, double* temp)
{
    unsigned char msb, lsb;
    int fd;
    unsigned char buf[10];
    if ((fd = open(filename, 02)) < 0)
    {
            fprintf(stderr,"Failed to open i2c port\n");
            return 1;
    }
    if (ioctl(fd, 0x0703, address) < 0)
    {
            fprintf(stderr,"Unable to get bus access to talk to slave\n");
            return 1;
    }
    buf[0] = 0;
    if ((write(fd, buf, 1)) != 1)
    {
            fprintf(stderr, "Error writing to i2c slave, %s\n", strerror((*__errno_location ())));
            return 1;
    }
    if ((read(fd, buf, 1)) != 1)
    {
            fprintf(stderr, "Unable to read from slave, %s\n", strerror((*__errno_location ())));
            return 1;
    }
    else
    {
            msb = buf[0];
            if (read(fd, buf, 1) != 1)
            {
                    fprintf(stderr, "Unable to read from slave, %s\n", strerror((*__errno_location ())));
                    return 1;
            }
            else
            {
                    int res;
                    lsb = buf[0];
                    res = (msb<<8) | lsb;
                    res >>= 4;
     if(res & (1<<11))
      res |= 0xF800;
                    *temp = res*0.0625;
            }
    }
    return 0;
}

canonical_original_code:
int read_i2c(char *filename, int address, double *temp)
{
    unsigned char msb; unsigned char lsb;
    int fd;
    unsigned char buf[10];
    if ((fd = open(filename, 02)) < 0)
    {
            fprintf(stderr,"Failed to open i2c port\n");
            return 1;
    }
    if (ioctl(fd, 0x0703, address) < 0)
    {
            fprintf(stderr,"Unable to get bus access to talk to slave\n");
            return 1;
    }
    buf[0] = 0;
    if ((write(fd, buf, 1)) != 1)
    {
            fprintf(stderr, "Error writing to i2c slave, %s\n", strerror((*__errno_location ())));
            return 1;
    }
    if ((read(fd, buf, 1)) != 1)
    {
            fprintf(stderr, "Unable to read from slave, %s\n", strerror((*__errno_location ())));
            return 1;
    }
    else
    {
            msb = buf[0];
            if (read(fd, buf, 1) != 1)
            {
                    fprintf(stderr, "Unable to read from slave, %s\n", strerror((*__errno_location ())));
                    return 1;
            }
            else
            {
                    int res;
                    lsb = buf[0];
                    res = (msb<<8) | lsb;
                    res >>= 4;
     if(res & (1<<11))
      res |= 0xF800;
                    *temp = res*0.0625;
            }
    }
    return 0;
}

variable_types:
filename
char *

variable_types: address int

variable_types:
temp
double *

variable_types:
msb
unsigned char

variable_types:
lsb
unsigned char

variable_types: fd int

variable_types:
buf
unsigned char [10]

variable_types: res int


intHandler

repo: tristanseifert/WSServer

decompiled_code:
void __fastcall __noreturn intHandler(int a1)
{
signal(a1, (__sighandler_t)((char *)_bswap_16 + 1));
printf("\x1B[1m\x1B[31m\nReceived Ctrl^C!\n\x1B[0m");
socket_handler_die();
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(int a1)
{
signal(a1, (__sighandler_t)((char *)_bswap_16 + 1));
printf("\x1B[1m\x1B[31m\nReceived Ctrl^C!\n\x1B[0m");
socket_handler_die();
exit(0);
}

original_code:
void intHandler(int sig) {
    signal(sig, ((__sighandler_t) 1));
    printf("\x1b[1m" "\x1b[31m" "\nReceived Ctrl^C!\n" "\x1b[0m");
 socket_handler_die();
    exit(0);
}

canonical_original_code:
void intHandler(int sig) {
    signal(sig, ((void (*)(int)) 1));
    printf("\x1b[1m" "\x1b[31m" "\nReceived Ctrl^C!\n" "\x1b[0m");
 socket_handler_die();
    exit(0);
}

variable_types: sig int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
__int64 v4; // rax
struct hostent *v5; // rax
__int16 v6; // ax
int *v7; // rbx
int v8; // edi
int v9; // eax
const char **v10; // rbx
int result; // eax
struct sockaddr v12; // [rsp+0h] [rbp-428h] BYREF
char name[1048]; // [rsp+10h] [rbp-418h] BYREF

config_parse("./config.cfg", argv, envp);
v3 = plugin_manager_new();
printf("\x1B[1mDiscovering plugins...\n\x1B[0m");
v4 = config_get_value("PluginDirectory");
plugin_discover(v4, v3);
connection = (__int64)malloc(4uLL);
*(_DWORD *)connection = 0;
*(_QWORD *)&v12.sa_family = 0LL;
*(_QWORD *)&v12.sa_data[6] = 0LL;
gethostname(name, 0x400uLL);
v5 = gethostbyname(name);
if ( v5 )
{
v12.sa_family = v5->h_addrtype;
v6 = config_get_number("WS_Port");
*(_WORD *)v12.sa_data = _bswap_16(v6);
v7 = (int *)connection;
v8 = socket(2, 1, 0);
*v7 = v8;
if ( v8 < 0 )
{
perror("Creating listening socket");
result = 255;
}
else
{
if ( bind(v8, &v12, 0x10u) >= 0 )
{
v9 = config_get_number("WS_ListenBacklog");
listen(*(_DWORD *)connection, v9);
printf("\x1B[1m\x1B[32m\nSocket connected!\n\n\x1B[0m");
signal(2, (__sighandler_t)intHandler);
socket_handler_init(connection);
puts("Server is ready. Waiting for connections.");
v10 = (const char **)malloc(0x38uLL);
http_parse_headers(
"GET /mychat HTTP/1.1\r\n"
"Host: server.example.com\r\n"
"Upgrade: websocket\r\n"
"Connection: Upgrade\r\n"
"Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n"
"Sec-WebSocket-Protocol: chat\r\n"
"Sec-WebSocket-Version: 13\r\n"
"Origin: http://example.com\r\n",
v10);
ws_calculate_accept(v10);
printf("Accept: %s\n", v10[3]);
while ( 1 )
sleep(5u);
}
close(*v7);
perror("Binding listening socket");
result = 512;
}
}
else
{
perror("Finding hostname");
result = -1;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
__int64 v4; // rax
struct hostent *v5; // rax
__int16 v6; // ax
int *v7; // rbx
int v8; // edi
int v9; // eax
const char **v10; // rbx
int result; // eax
struct sockaddr v12; // [rsp+0h] [rbp-428h] BYREF
char name[1048]; // [rsp+10h] [rbp-418h] BYREF

config_parse("./config.cfg", argv, envp);
v3 = plugin_manager_new();
printf("\x1B[1mDiscovering plugins...\n\x1B[0m");
v4 = config_get_value("PluginDirectory");
plugin_discover(v4, v3);
connection = (__int64)malloc(4uLL);
*(_DWORD *)connection = 0;
*(_QWORD *)&v12.sa_family = 0LL;
*(_QWORD *)&v12.sa_data[6] = 0LL;
gethostname(name, 0x400uLL);
v5 = gethostbyname(name);
if ( v5 )
{
v12.sa_family = v5->h_addrtype;
v6 = config_get_number("WS_Port");
*(_WORD *)v12.sa_data = func0(v6);
v7 = (int *)connection;
v8 = socket(2, 1, 0);
*v7 = v8;
if ( v8 < 0 )
{
perror("Creating listening socket");
result = 255;
}
else
{
if ( bind(v8, &v12, 0x10u) >= 0 )
{
v9 = config_get_number("WS_ListenBacklog");
listen(*(_DWORD *)connection, v9);
printf("\x1B[1m\x1B[32m\nSocket connected!\n\n\x1B[0m");
signal(2, (__sighandler_t)intHandler);
socket_handler_init(connection);
puts("Server is ready. Waiting for connections.");
v10 = (const char **)malloc(0x38uLL);
http_parse_headers(
"GET /mychat HTTP/1.1\r\n"
"Host: server.example.com\r\n"
"Upgrade: websocket\r\n"
"Connection: Upgrade\r\n"
"Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n"
"Sec-WebSocket-Protocol: chat\r\n"
"Sec-WebSocket-Version: 13\r\n"
"Origin: http://example.com\r\n",
v10);
ws_calculate_accept(v10);
printf("Accept: %s\n", v10[3]);
while ( 1 )
sleep(5u);
}
close(*v7);
perror("Binding listening socket");
result = 512;
}
}
else
{
perror("Finding hostname");
result = -1;
}
return result;
}

original_code:
int main(int argc, const char* argv[]) {
 config_parse("./config.cfg");
 Plugin_Manager *pm = plugin_manager_new();
 printf("\x1b[1m" "Discovering plugins...\n" "\x1b[0m");
    void* pdstate = plugin_discover(config_get_value("PluginDirectory"), pm);
    connection = malloc(sizeof(*connection));
    memset(connection, 0x00, sizeof(*connection));
    char ourname[1024 +1];
    struct sockaddr_in sa;
    struct hostent *hp;
    memset(&sa, 0, sizeof(struct sockaddr_in));
    gethostname(ourname, 1024);
    hp = gethostbyname(ourname);
    if(hp == ((void *)0)) {
        perror("Finding hostname");
        return -1;
    }
    sa.sin_family = hp->h_addrtype;
    sa.sin_port = htons(config_get_number("WS_Port"));
    if((connection->socket = socket(2, SOCK_STREAM, 0)) < 0) {
        perror("Creating listening socket");
        return 255;
    }
    if(bind(connection->socket, (const struct sockaddr *) &sa, sizeof(struct sockaddr_in)) < 0) {
        close(connection->socket);
        perror("Binding listening socket");
        return 512;
    }
    listen(connection->socket, (int) config_get_number("WS_ListenBacklog"));
    printf("\x1b[1m" "\x1b[32m" "\nSocket connected!\n\n" "\x1b[0m");
 signal(2, intHandler);
    socket_handler_init(connection);
    printf("Server is ready. Waiting for connections.\n");
 WS_Client *client = malloc(sizeof(*client));
    char* test_headers = "GET /mychat HTTP/1.1\r\nHost: server.example.com\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\nSec-WebSocket-Protocol: chat\r\nSec-WebSocket-Version: 13\r\nOrigin: http://example.com\r\n";
    http_parse_headers(test_headers, client);
 ws_calculate_accept(client);
 printf("Accept: %s\n", client->ws_accept);
    while(1) {
        sleep(5);
    }
}

canonical_original_code:
int main(int argc, char *argv[]) {
 config_parse("./config.cfg");
 struct Plugin_Manager *pm = plugin_manager_new();
 printf("\x1b[1m" "Discovering plugins...\n" "\x1b[0m");
    void *pdstate = plugin_discover(config_get_value("PluginDirectory"), pm);
    connection = malloc(sizeof(*connection));
    memset(connection, 0x00, sizeof(*connection));
    char ourname[-1];
    struct sockaddr_in sa;
    struct hostent *hp;
    memset(&sa, 0, sizeof(struct sockaddr_in));
    gethostname(ourname, 1024);
    hp = gethostbyname(ourname);
    if(hp == ((void *)0)) {
        perror("Finding hostname");
        return -1;
    }
    sa.sin_family = hp->h_addrtype;
    sa.sin_port = htons(config_get_number("WS_Port"));
    if((connection->socket = socket(2, SOCK_STREAM, 0)) < 0) {
        perror("Creating listening socket");
        return 255;
    }
    if(bind(connection->socket, (struct sockaddr *) &sa, sizeof(struct sockaddr_in)) < 0) {
        close(connection->socket);
        perror("Binding listening socket");
        return 512;
    }
    listen(connection->socket, (int) config_get_number("WS_ListenBacklog"));
    printf("\x1b[1m" "\x1b[32m" "\nSocket connected!\n\n" "\x1b[0m");
 signal(2, intHandler);
    socket_handler_init(connection);
    printf("Server is ready. Waiting for connections.\n");
 struct WS_Client *client = malloc(sizeof(*client));
    char *test_headers = "GET /mychat HTTP/1.1\r\nHost: server.example.com\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\nSec-WebSocket-Protocol: chat\r\nSec-WebSocket-Version: 13\r\nOrigin: http://example.com\r\n";
    http_parse_headers(test_headers, client);
 ws_calculate_accept(client);
 printf("Accept: %s\n", client->ws_accept);
    while(1) {
        sleep(5);
    }
}

variable_types:
argv
char *[]

variable_types: struct Plugin_Manager { int num_plugins_loaded; struct Plugin_Endpoint_Registry { void *next; char *endpoint; int (*callback)(struct WS_Client { int read_socket; char *endpoint; char *ws_key; char *ws_accept; char *ws_protocol; int ws_version; void *headers; } *, char *, unsigned int); } *plugin_endpoint_map; struct Plugin_Endpoint_Registry *plugin_endpoint_map_last; } *

variable_types:
pdstate
void *

variable_types:
ourname
char [-1]

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 hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

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 Plugin_Manager { int num_plugins_loaded; struct Plugin_Endpoint_Registry *plugin_endpoint_map; struct Plugin_Endpoint_Registry *plugin_endpoint_map_last; }

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

user_defined_types: struct Plugin_Endpoint_Registry { void *next; char *endpoint; int (*callback)(struct WS_Client *, char *, unsigned int); }


decompiled_code:
void *__fastcall plugin_get_name(char *src)
{
char *v1; // rax
char *v2; // rbp
char *v3; // rax
void *v4; // rbx
size_t v5; // r12
size_t v6; // r13

v1 = strrchr(src, 47);
v2 = v1 + 1;
if ( !v1 )
v2 = src;
v3 = strrchr(src, 46);
v4 = v3;
if ( v3 )
{
if ( !strcmp(v3, ".so") )
{
v5 = v3 - v2;
v6 = v3 - v2 + 2;
v4 = malloc(v6);
memset(v4, 0, v6);
strncpy((char *)v4, v2, v5);
}
else
{
v4 = 0LL;
}
}
return v4;
}

canonical_decompiled_code:
void *__fastcall func0(char *src)
{
char *v1; // rax
char *v2; // rbp
char *v3; // rax
void *v4; // rbx
size_t v5; // r12
size_t v6; // r13

v1 = strrchr(src, 47);
v2 = v1 + 1;
if ( !v1 )
v2 = src;
v3 = strrchr(src, 46);
v4 = v3;
if ( v3 )
{
if ( !strcmp(v3, ".so") )
{
v5 = v3 - v2;
v6 = v3 - v2 + 2;
v4 = malloc(v6);
memset(v4, 0, v6);
strncpy((char *)v4, v2, v5);
}
else
{
v4 = 0LL;
}
}
return v4;
}

original_code:
static char* plugin_get_name(char* filename) {
 char* last_slash = strrchr(filename, '/');
 char* name_start = last_slash ? last_slash + 1 : filename;
 char* last_dot = strrchr(filename, '.');
 if (!last_dot || strcmp(last_dot, ".so")) {
  return ((void *)0);
 }
 char* dest = malloc((last_dot - name_start) + 2);
 memset(dest, 0x00, ((last_dot - name_start) + 2));
 strncpy(dest, name_start, last_dot - name_start);
 return dest;
}

canonical_original_code:
static char *plugin_get_name(char *filename) {
 char *last_slash = strrchr(filename, '/');
 char *name_start = last_slash ? last_slash + 1 : filename;
 char *last_dot = strrchr(filename, '.');
 if (!last_dot || strcmp(last_dot, ".so")) {
  return ((void *)0);
 }
 char *dest = malloc((last_dot - name_start) + 2);
 memset(dest, 0x00, ((last_dot - name_start) + 2));
 strncpy(dest, name_start, last_dot - name_start);
 return dest;
}

variable_types:
dest
char *

return_type: char *


decompiled_code:
void *__fastcall plugin_load(char *s, char *a2, __int64 a3)
{
size_t v4; // rax
void *v5; // rbp
size_t v6; // rax
size_t v7; // rax
void *v8; // r13
size_t v9; // rax
void *v10; // rbp
size_t v11; // rax
size_t v12; // rax
__int64 (__fastcall *v13)(__int64); // r12
int v14; // eax
char *v16; // rax
char *v17; // rax

v4 = strlen(a2);
v5 = malloc(v4 + 8);
v6 = strlen(a2);
memset(v5, 0, v6 + 8);
v7 = strlen(a2);
snprintf((char *)v5, v7 + 8, "./%s", a2);
v8 = dlopen((const char *)v5, 258);
free(v5);
if ( v8 )
{
v9 = strlen(s);
v10 = malloc(v9 + 8);
v11 = strlen(s);
memset(v10, 0, v11 + 8);
v12 = strlen(s);
snprintf((char *)v10, v12 + 8, "init_%s", s);
v13 = (__int64 (__fastcall *)(__int64))dlsym(v8, (const char *)v10);
free(v10);
if ( v13 )
{
v14 = v13(a3);
if ( v14 < 0 )
{
printf("Error: Plugin init function returned %d\n", (unsigned int)v14);
dlclose(v8);
v8 = 0LL;
}
else
{
printf("\x1B[1m\x1B[32mLoaded plugin from: \"%s\"\n\x1B[0m", a2);
}
}
else
{
v17 = dlerror();
printf("Error loading init function: %s\n", v17);
dlclose(v8);
v8 = 0LL;
}
}
else
{
v16 = dlerror();
printf("Error loading DSO: %s\n", v16);
}
return v8;
}

canonical_decompiled_code:
void *__fastcall func1(char *s, char *a2, __int64 a3)
{
size_t v4; // rax
void *v5; // rbp
size_t v6; // rax
size_t v7; // rax
void *v8; // r13
size_t v9; // rax
void *v10; // rbp
size_t v11; // rax
size_t v12; // rax
__int64 (__fastcall *v13)(__int64); // r12
int v14; // eax
char *v16; // rax
char *v17; // rax

v4 = strlen(a2);
v5 = malloc(v4 + 8);
v6 = strlen(a2);
memset(v5, 0, v6 + 8);
v7 = strlen(a2);
snprintf((char *)v5, v7 + 8, "./%s", a2);
v8 = dlopen((const char *)v5, 258);
free(v5);
if ( v8 )
{
v9 = strlen(s);
v10 = malloc(v9 + 8);
v11 = strlen(s);
memset(v10, 0, v11 + 8);
v12 = strlen(s);
snprintf((char *)v10, v12 + 8, "init_%s", s);
v13 = (__int64 (__fastcall *)(__int64))dlsym(v8, (const char *)v10);
free(v10);
if ( v13 )
{
v14 = v13(a3);
if ( v14 < 0 )
{
printf("Error: Plugin init function returned %d\n", (unsigned int)v14);
dlclose(v8);
v8 = 0LL;
}
else
{
printf("\x1B[1m\x1B[32mLoaded plugin from: \"%s\"\n\x1B[0m", a2);
}
}
else
{
v17 = dlerror();
printf("Error loading init function: %s\n", v17);
dlclose(v8);
v8 = 0LL;
}
}
else
{
v16 = dlerror();
printf("Error loading DSO: %s\n", v16);
}
return v8;
}

original_code:
static void* plugin_load(char* name, char* fullpath, Plugin_Manager* pm) {
 char *slashedpath = malloc(strlen(fullpath) + 8);
 memset(slashedpath, 0x00, (strlen(fullpath) + 8));
 snprintf(slashedpath, (strlen(fullpath) + 8), "./%s", fullpath);
 void* libhandle = dlopen(slashedpath, 0x00002 | 0x00100);
 free(slashedpath);
 if (!libhandle) {
  printf("Error loading DSO: %s\n", dlerror());
  return ((void *)0);
 }
 char *initfunc_name = malloc(strlen(name) + 8);
 memset(initfunc_name, 0x00, (strlen(name) + 8));
 snprintf(initfunc_name, (strlen(name) + 8), "init_%s", name);
 Plugin_InitFunc initfunc = (Plugin_InitFunc) (intptr_t) dlsym(libhandle, initfunc_name);
 free(initfunc_name);
 if (!initfunc) {
  printf("Error loading init function: %s\n", dlerror());
  dlclose(libhandle);
  return ((void *)0);
 }
 int rc = initfunc(pm);
 if (rc < 0) {
  printf("Error: Plugin init function returned %d\n", rc);
  dlclose(libhandle);
  return ((void *)0);
 }
 printf("\x1b[1m" "\x1b[32m" "Loaded plugin from: \"%s\"\n" "\x1b[0m", fullpath);
 return libhandle;
}

canonical_original_code:
static void *plugin_load(char *name, char *fullpath, struct Plugin_Manager *pm) {
 char *slashedpath = malloc(strlen(fullpath) + 8);
 memset(slashedpath, 0x00, (strlen(fullpath) + 8));
 snprintf(slashedpath, (strlen(fullpath) + 8), "./%s", fullpath);
 void *libhandle = dlopen(slashedpath, 0x00002 | 0x00100);
 free(slashedpath);
 if (!libhandle) {
  printf("Error loading DSO: %s\n", dlerror());
  return ((void *)0);
 }
 char *initfunc_name = malloc(strlen(name) + 8);
 memset(initfunc_name, 0x00, (strlen(name) + 8));
 snprintf(initfunc_name, (strlen(name) + 8), "init_%s", name);
 int (*initfunc)(struct Plugin_Manager *) = (Plugin_InitFunc) (intptr_t) dlsym(libhandle, initfunc_name);
 free(initfunc_name);
 if (!initfunc) {
  printf("Error loading init function: %s\n", dlerror());
  dlclose(libhandle);
  return ((void *)0);
 }
 int rc = initfunc(pm);
 if (rc < 0) {
  printf("Error: Plugin init function returned %d\n", rc);
  dlclose(libhandle);
  return ((void *)0);
 }
 printf("\x1b[1m" "\x1b[32m" "Loaded plugin from: \"%s\"\n" "\x1b[0m", fullpath);
 return libhandle;
}

variable_types:
name
char *

variable_types:
fullpath
char *

variable_types:
slashedpath
char *

variable_types:
libhandle
void *

variable_types: int (*)(struct Plugin_Manager { int num_plugins_loaded; struct Plugin_Endpoint_Registry { void *next; char *endpoint; int (*callback)(struct WS_Client { int read_socket; char *endpoint; char *ws_key; char *ws_accept; char *ws_protocol; int ws_version; void *headers; } *, char *, unsigned int); } *plugin_endpoint_map; struct Plugin_Endpoint_Registry *plugin_endpoint_map_last; } *)

variable_types: rc int


decompiled_code:
void __fastcall plugin_cleanup(void *ptr)
{
__int64 v1; // rbx
void **v2; // rbp

v1 = *(_QWORD *)ptr;
while ( v1 )
{
v2 = (void **)v1;
v1 = *(_QWORD *)(v1 + 8);
dlclose(*v2);
free(v2);
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
__int64 v1; // rbx
void **v2; // rbp

v1 = *(_QWORD *)ptr;
while ( v1 )
{
v2 = (void **)v1;
v1 = *(_QWORD *)(v1 + 8);
dlclose(*v2);
free(v2);
}
free(ptr);
}

original_code:
void plugin_cleanup(void* vpds) {
 Plugin_DiscoveryState* pds = (Plugin_DiscoveryState*)vpds;
 Plugin_HandleList* node = pds->handle_list;
 while(node) {
  Plugin_HandleList* next = node->next;
  dlclose(node->handle);
  free(node);
  node = next;
 }
 free(pds);
}

canonical_original_code:
void plugin_cleanup(void *vpds) {
 struct Plugin_DiscoveryState_t *pds = (Plugin_DiscoveryState*)vpds;
 struct Plugin_HandleList_t *node = pds->handle_list;
 while(node) {
  struct Plugin_HandleList_t *next = node->next;
  dlclose(node->handle);
  free(node);
  node = next;
 }
 free(pds);
}

variable_types:
vpds
void *

variable_types: struct Plugin_DiscoveryState_t { struct Plugin_HandleList_t { void *handle; struct Plugin_HandleList_t *next; } *handle_list; } *

user_defined_types: struct Plugin_DiscoveryState_t { struct Plugin_HandleList_t *handle_list; }


decompiled_code:
void *__fastcall socket_handler_connection_handler(void *a1)
{
if ( a1 )
pthread_exit(0LL);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func0(void *a1)
{
if ( a1 )
pthread_exit(0LL);
return 0LL;
}

original_code:
void *socket_handler_connection_handler(void *connection) {
    if(connection == ((void *)0)) return ((void *)0);
    int sock = *((int *) connection);
    pthread_exit(((void *)0));
}

variable_types:
connection
void *

variable_types: sock int


decompiled_code:
__int64 __fastcall socket_handler_init(void *arg)
{
unsigned int v1; // ebx

socket_listener_should_run = 1;
v1 = pthread_create(&socket_listener_thread, 0LL, socket_handler_listening_thread, arg);
if ( v1 )
{
socket_listener_should_run = 0;
close(*(_DWORD *)arg);
perror("Creating listening thread");
BYTE1(v1) |= 0x80u;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *arg)
{
unsigned int v1; // ebx

socket_listener_should_run = 1;
v1 = pthread_create(&socket_listener_thread, 0LL, socket_handler_listening_thread, arg);
if ( v1 )
{
socket_listener_should_run = 0;
close(*(_DWORD *)arg);
perror("Creating listening thread");
BYTE1(v1) |= 0x80u;
}
return v1;
}

original_code:
int socket_handler_init(WS_Connection *connection) {
 socket_listener_should_run = 1;
    int error = pthread_create(&socket_listener_thread, ((void *)0), socket_handler_listening_thread, connection);
    if(error) {
        socket_listener_should_run = 0;
        close(connection->socket);
        perror("Creating listening thread");
        return error | 0x8000;
    }
    return 0;
}

canonical_original_code:
int socket_handler_init(struct WS_Connection *connection) {
 socket_listener_should_run = 1;
    int error = pthread_create(&socket_listener_thread, ((void *)0), socket_handler_listening_thread, connection);
    if(error) {
        socket_listener_should_run = 0;
        close(connection->socket);
        perror("Creating listening thread");
        return error | 0x8000;
    }
    return 0;
}

variable_types:
connection
struct WS_Connection { int socket; } *

variable_types: error int


decompiled_code:
void socket_handler_die()
{
socket_listener_should_run = 0;
}

canonical_decompiled_code:
void func2()
{
socket_listener_should_run = 0;
}


decompiled_code:
int __fastcall socket_handler_get_connection(int a1)
{
int result; // eax

result = accept(a1, 0LL, 0LL);
if ( result < 0 )
result = -1;
return result;
}

canonical_decompiled_code:
int __fastcall func3(int a1)
{
int result; // eax

result = accept(a1, 0LL, 0LL);
if ( result < 0 )
result = -1;
return result;
}

original_code:
int socket_handler_get_connection(int sock) {
    int t;
    if((t = accept(sock, ((void *)0), ((void *)0))) < 0) {
        return -1;
    }
    return t;
}

variable_types: t int


decompiled_code:
void __fastcall __noreturn socket_handler_listening_thread(void *a1)
{
__int64 v1; // rax
pthread_t v2; // [rsp+0h] [rbp-18h] BYREF
int arg[3]; // [rsp+Ch] [rbp-Ch] BYREF

v1 = config_get_number("WS_Port");
printf("Client listening thread active. Listening for clients on port %lli...\n", v1);
fflush(stdout);
while ( socket_listener_should_run == 1 )
{
arg[0] = socket_handler_get_connection(*(_DWORD *)a1);
if ( arg[0] >= 0 )
{
pthread_create(&v2, 0LL, socket_handler_connection_handler, arg);
}
else if ( *_errno_location() != 4 )
{
perror("Accept client connection");
}
}
close(*(_DWORD *)a1);
pthread_exit(0LL);
}

canonical_decompiled_code:
void __fastcall __noreturn func4(void *a1)
{
__int64 v1; // rax
pthread_t v2; // [rsp+0h] [rbp-18h] BYREF
int arg[3]; // [rsp+Ch] [rbp-Ch] BYREF

v1 = config_get_number("WS_Port");
printf("Client listening thread active. Listening for clients on port %lli...\n", v1);
fflush(stdout);
while ( socket_listener_should_run == 1 )
{
arg[0] = func3(*(_DWORD *)a1);
if ( arg[0] >= 0 )
{
pthread_create(&v2, 0LL, socket_handler_connection_handler, arg);
}
else if ( *_errno_location() != 4 )
{
perror("Accept client connection");
}
}
close(*(_DWORD *)a1);
pthread_exit(0LL);
}

original_code:
void *socket_handler_listening_thread(void *param) {
    int t;
    WS_Connection *conn = (WS_Connection *) param;
    printf("Client listening thread active. Listening for clients on port %lli...\n"
           , config_get_number("WS_Port"));
    fflush(stdout);
    while(socket_listener_should_run == 1) {
        if((t = socket_handler_get_connection(conn->socket)) < 0) {
            if((*__errno_location ()) == 4) {
                continue;
            }
            perror("Accept client connection");
        } else {
            pthread_t thread;
            pthread_create(&thread, ((void *)0), socket_handler_connection_handler, (void *) &t);
        }
    }
    close(conn->socket);
    pthread_exit(((void *)0));
}

canonical_original_code:
void *socket_handler_listening_thread(void *param) {
    int t;
    struct WS_Connection *conn = (WS_Connection *) param;
    printf("Client listening thread active. Listening for clients on port %lli...\n"
           , config_get_number("WS_Port"));
    fflush(stdout);
    while(socket_listener_should_run == 1) {
        if((t = socket_handler_get_connection(conn->socket)) < 0) {
            if((*__errno_location ()) == 4) {
                continue;
            }
            perror("Accept client connection");
        } else {
            unsigned long thread;
            pthread_create(&thread, ((void *)0), socket_handler_connection_handler, (void *) &t);
        }
    }
    close(conn->socket);
    pthread_exit(((void *)0));
}

variable_types:
param
void *

variable_types:
conn
struct WS_Connection { int socket; } *

variable_types:
thread
unsigned long


decompiled_code:
__int64 __fastcall util_string_trim(void *src)
{
char v1; // bl
const unsigned __int16 *v2; // rax
const char *v3; // r12
size_t v4; // rbx
__int64 result; // rax
_BYTE *v6; // rbx
const unsigned __int16 *v7; // rdx

v1 = *(_BYTE *)src;
if ( *(_BYTE *)src )
{
v2 = *_ctype_b_loc();
v3 = (const char *)src;
do
{
if ( (v2[v1] & 0x2000) == 0 )
break;
v1 = *++v3;
}
while ( *v3 );
}
else
{
v3 = (const char *)src;
}
v4 = strlen(v3) + 1;
result = (__int64)memmove(src, v3, v4);
v6 = (char *)src + v4;
if ( src < v6 )
{
v7 = *_ctype_b_loc();
do
{
result = (char)*v6;
if ( (v7[result] & 0x2000) == 0 )
break;
--v6;
}
while ( src != v6 );
}
*v6 = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(void *src)
{
char v1; // bl
const unsigned __int16 *v2; // rax
const char *v3; // r12
size_t v4; // rbx
__int64 result; // rax
_BYTE *v6; // rbx
const unsigned __int16 *v7; // rdx

v1 = *(_BYTE *)src;
if ( *(_BYTE *)src )
{
v2 = *_ctype_b_loc();
v3 = (const char *)src;
do
{
if ( (v2[v1] & 0x2000) == 0 )
break;
v1 = *++v3;
}
while ( *v3 );
}
else
{
v3 = (const char *)src;
}
v4 = strlen(v3) + 1;
result = (__int64)memmove(src, v3, v4);
v6 = (char *)src + v4;
if ( src < v6 )
{
v7 = *_ctype_b_loc();
do
{
result = (char)*v6;
if ( (v7[result] & 0x2000) == 0 )
break;
--v6;
}
while ( src != v6 );
}
*v6 = 0;
return result;
}

original_code:
void util_string_trim(char* string) {
 const char* firstNonSpace = string;
 while(*firstNonSpace != '\0' && ((*__ctype_b_loc ())[(int) ((*firstNonSpace))] & (unsigned short int) _ISspace)) {
  ++firstNonSpace;
 }
 size_t len = strlen(firstNonSpace)+1;
 memmove(string, firstNonSpace, len);
 char* endOfString = string + len;
 while(string < endOfString && ((*__ctype_b_loc ())[(int) ((*endOfString))] & (unsigned short int) _ISspace)) {
  --endOfString;
 }
 *endOfString = '\0';
}

canonical_original_code:
void util_string_trim(char *string) {
 char *firstNonSpace = string;
 while(*firstNonSpace != '\0' && ((*__ctype_b_loc ())[(int) ((*firstNonSpace))] & (unsigned short) _ISspace)) {
  ++firstNonSpace;
 }
 unsigned long len = strlen(firstNonSpace)+1;
 memmove(string, firstNonSpace, len);
 char *endOfString = string + len;
 while(string < endOfString && ((*__ctype_b_loc ())[(int) ((*endOfString))] & (unsigned short) _ISspace)) {
  --endOfString;
 }
 *endOfString = '\0';
}

variable_types:
string
char *

variable_types:
firstNonSpace
char *

variable_types:
len
unsigned long

variable_types:
endOfString
char *


decompiled_code:
char *__fastcall util_config_strip_quotes(char *src)
{
size_t v1; // rbp
char *v2; // rbx

v1 = (int)strlen(src);
v2 = (char *)malloc(v1);
strcpy(v2, src);
v2[v1 - 1] = 0;
return v2 + 1;
}

canonical_decompiled_code:
char *__fastcall func1(char *src)
{
size_t v1; // rbp
char *v2; // rbx

v1 = (int)strlen(src);
v2 = (char *)malloc(v1);
strcpy(v2, src);
v2[v1 - 1] = 0;
return v2 + 1;
}

original_code:
char *util_config_strip_quotes(char *string) {
 int length = (int) strlen(string);
 char *newString = malloc(length);
 strcpy(newString, string);
 char *strPtr = newString;
 strPtr += length - 1;
 *strPtr = 0x00;
 return newString + 1;
}

variable_types: length int

variable_types:
newString
char *

variable_types:
strPtr
char *


decompiled_code:
_QWORD *plugin_manager_new()
{
_QWORD *v0; // rbx
void *v1; // rax

v0 = malloc(0x18uLL);
v1 = malloc(0x18uLL);
v0[1] = v1;
v0[2] = v1;
return v0;
}

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *v0; // rbx
void *v1; // rax

v0 = malloc(0x18uLL);
v1 = malloc(0x18uLL);
v0[1] = v1;
v0[2] = v1;
return v0;
}

original_code:
Plugin_Manager* plugin_manager_new() {
 Plugin_Manager* pm = malloc(sizeof(*pm));
 pm->plugin_endpoint_map = malloc(sizeof(Plugin_Endpoint_Registry));
 pm->plugin_endpoint_map_last = pm->plugin_endpoint_map;
 return pm;
}

canonical_original_code:
struct Plugin_Manager *plugin_manager_new() {
 struct Plugin_Manager *pm = malloc(sizeof(*pm));
 pm->plugin_endpoint_map = malloc(sizeof(Plugin_Endpoint_Registry));
 pm->plugin_endpoint_map_last = pm->plugin_endpoint_map;
 return pm;
}

variable_types: struct Plugin_Manager { int num_plugins_loaded; struct Plugin_Endpoint_Registry { void *next; char *endpoint; int (*callback)(struct WS_Client { int read_socket; char *endpoint; char *ws_key; char *ws_accept; char *ws_protocol; int ws_version; void *headers; } *, char *, unsigned int); } *plugin_endpoint_map; struct Plugin_Endpoint_Registry *plugin_endpoint_map_last; } *


decompiled_code:
void __fastcall plugin_manager_free(void *ptr)
{
void **i; // rbx

for ( i = (void **)*((_QWORD *)ptr + 1); i; i = (void **)*i )
{
free(i);
free(i[1]);
}
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
void **i; // rbx

for ( i = (void **)*((_QWORD *)ptr + 1); i; i = (void **)*i )
{
free(i);
free(i[1]);
}
free(ptr);
}

original_code:
void plugin_manager_free(Plugin_Manager* pm) {
 Plugin_Endpoint_Registry* entry = (Plugin_Endpoint_Registry*) pm->plugin_endpoint_map;
 while(entry) {
  free(entry);
  free(entry->endpoint);
  entry = entry->next;
 }
 free(pm);
}

canonical_original_code:
void plugin_manager_free(struct Plugin_Manager *pm) {
 struct Plugin_Endpoint_Registry *entry = (Plugin_Endpoint_Registry*) pm->plugin_endpoint_map;
 while(entry) {
  free(entry);
  free(entry->endpoint);
  entry = entry->next;
 }
 free(pm);
}

variable_types: struct Plugin_Endpoint_Registry { void *next; char *endpoint; int (*callback)(struct WS_Client { int read_socket; char *endpoint; char *ws_key; char *ws_accept; char *ws_protocol; int ws_version; void *headers; } *, char *, unsigned int); } *


decompiled_code:
int __fastcall plugin_register_handler(const char *a1, __int64 a2, __int64 a3)
{
_QWORD *v4; // r13
_QWORD *v5; // rax

v4 = *(_QWORD **)(a3 + 16);
v5 = malloc(0x18uLL);
*v5 = 0LL;
v5[1] = a1;
v5[2] = a2;
*v4 = v5;
*(_QWORD *)(a3 + 16) = v5;
return printf("Registered plugin for endpoint \"%s\" with callback %X\n", a1, a2);
}

canonical_decompiled_code:
int __fastcall func2(const char *a1, __int64 a2, __int64 a3)
{
_QWORD *v4; // r13
_QWORD *v5; // rax

v4 = *(_QWORD **)(a3 + 16);
v5 = malloc(0x18uLL);
*v5 = 0LL;
v5[1] = a1;
v5[2] = a2;
*v4 = v5;
*(_QWORD *)(a3 + 16) = v5;
return printf("Registered plugin for endpoint \"%s\" with callback %X\n", a1, a2);
}

original_code:
void plugin_register_handler(char* endpointPath, Plugin_EndpointCallback function, Plugin_Manager* pm) {
 Plugin_Endpoint_Registry *lastEntry = pm->plugin_endpoint_map_last;
 Plugin_Endpoint_Registry *newEntry = malloc(sizeof(*newEntry));
 memset(newEntry, 0x00, sizeof(*newEntry));
 newEntry->endpoint = endpointPath;
 newEntry->callback = function;
 lastEntry->next = newEntry;
 pm->plugin_endpoint_map_last = newEntry;
 printf("Registered plugin for endpoint \"%s\" with callback %X\n", endpointPath, function);
}

canonical_original_code:
void plugin_register_handler(char *endpointPath, int (*function)(struct WS_Client *, char *, unsigned int), struct Plugin_Manager *pm) {
 struct Plugin_Endpoint_Registry *lastEntry = pm->plugin_endpoint_map_last;
 struct Plugin_Endpoint_Registry *newEntry = malloc(sizeof(*newEntry));
 memset(newEntry, 0x00, sizeof(*newEntry));
 newEntry->endpoint = endpointPath;
 newEntry->callback = function;
 lastEntry->next = newEntry;
 pm->plugin_endpoint_map_last = newEntry;
 printf("Registered plugin for endpoint \"%s\" with callback %X\n", endpointPath, function);
}

variable_types:
endpointPath
char *

variable_types: int (*)(struct WS_Client { int read_socket; char *endpoint; char *ws_key; char *ws_accept; char *ws_protocol; int ws_version; void *headers; } *, char *, unsigned int)

variable_types: struct Plugin_Manager { int num_plugins_loaded; struct Plugin_Endpoint_Registry { void *next; char *endpoint; int (*callback)(struct WS_Client *, char *, unsigned int); } *plugin_endpoint_map; struct Plugin_Endpoint_Registry *plugin_endpoint_map_last; } *


decompiled_code:
__int64 __fastcall config_get_value(char *s1)
{
__int64 v1; // rbx
__int64 v2; // rbp

v1 = config_list;
while ( 1 )
{
v2 = v1;
v1 = *(_QWORD *)(v1 + 24);
if ( !v1 )
break;
if ( !strcmp(s1, *(const char **)v2) )
return *(_QWORD *)(v2 + 8);
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *s1)
{
__int64 v1; // rbx
__int64 v2; // rbp

v1 = config_list;
while ( 1 )
{
v2 = v1;
v1 = *(_QWORD *)(v1 + 24);
if ( !v1 )
break;
if ( !strcmp(s1, *(const char **)v2) )
return *(_QWORD *)(v2 + 8);
}
return v1;
}

original_code:
void *config_get_value(char * key) {
    config_entry *entry = config_list;
    void *value = ((void *)0);
    while(entry->next_entry != ((void *)0)) {
        if(strcmp(key, entry->key) == 0) {
            value = entry->data;
            break;
        }
        entry = (config_entry *) entry->next_entry;
    }
    return value;
}

canonical_original_code:
void *config_get_value(char *key) {
    struct config_entry *entry = config_list;
    void *value = ((void *)0);
    while(entry->next_entry != ((void *)0)) {
        if(strcmp(key, entry->key) == 0) {
            value = entry->data;
            break;
        }
        entry = (struct config_entry *) entry->next_entry;
    }
    return value;
}

variable_types:
key
char *

variable_types: struct config_entry { char *key; void *data; enum config_type { kConfigValueString=0, kConfigValueNumber=1 } type; struct config_entry *next_entry; } *

variable_types:
value
void *

user_defined_types: struct config_entry { char *key; void *data; enum config_type type; struct config_entry *next_entry; }


decompiled_code:
__int64 __fastcall config_get_number(char *s1)
{
__int64 v1; // rbx
__int64 v2; // rbp

v1 = config_list;
while ( 1 )
{
v2 = v1;
v1 = *(_QWORD *)(v1 + 24);
if ( !v1 )
break;
if ( !strcmp(s1, *(const char **)v2) )
return *(_QWORD *)(v2 + 8);
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(char *s1)
{
__int64 v1; // rbx
__int64 v2; // rbp

v1 = config_list;
while ( 1 )
{
v2 = v1;
v1 = *(_QWORD *)(v1 + 24);
if ( !v1 )
break;
if ( !strcmp(s1, *(const char **)v2) )
return *(_QWORD *)(v2 + 8);
}
return v1;
}

original_code:
long long config_get_number(char * key) {
    config_entry *entry = config_list;
    void *value = ((void *)0);
    while(entry->next_entry != ((void *)0)) {
        if(strcmp(key, entry->key) == 0) {
            value = entry->data;
            break;
        }
        entry = (config_entry *) entry->next_entry;
    }
    return (long long) value;
}

canonical_original_code:
long long config_get_number(char *key) {
    struct config_entry *entry = config_list;
    void *value = ((void *)0);
    while(entry->next_entry != ((void *)0)) {
        if(strcmp(key, entry->key) == 0) {
            value = entry->data;
            break;
        }
        entry = (struct config_entry *) entry->next_entry;
    }
    return (long long) value;
}

return_type: long long

user_defined_types: enum config_type { kConfigValueString=0, kConfigValueNumber=1 }


decompiled_code:
__int64 config_find_last_entry()
{
__int64 v0; // rax
__int64 v1; // rdx

v0 = config_list;
if ( !config_list )
return config_list;
do
{
v1 = v0;
v0 = *(_QWORD *)(v0 + 24);
}
while ( v0 );
return v1;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // rax
__int64 v1; // rdx

v0 = config_list;
if ( !config_list )
return config_list;
do
{
v1 = v0;
v0 = *(_QWORD *)(v0 + 24);
}
while ( v0 );
return v1;
}

original_code:
inline config_entry *config_find_last_entry() {
    if(config_list == ((void *)0)) {
        return config_list;
    }
    config_entry *list_read_ptr = config_list;
    while(list_read_ptr != ((void *)0)) {
        config_entry *entry = list_read_ptr;
        if(entry->next_entry != ((void *)0)) {
            list_read_ptr = (config_entry *) entry->next_entry;
        } else {
            break;
        }
    }
    return list_read_ptr;
}

canonical_original_code:
inline struct config_entry *config_find_last_entry() {
    if(config_list == ((void *)0)) {
        return config_list;
    }
    struct config_entry *list_read_ptr = config_list;
    while(list_read_ptr != ((void *)0)) {
        struct config_entry *entry = list_read_ptr;
        if(entry->next_entry != ((void *)0)) {
            list_read_ptr = (struct config_entry *) entry->next_entry;
        } else {
            break;
        }
    }
    return list_read_ptr;
}

return_type: struct config_entry { char *key; void *data; enum config_type { kConfigValueString=0, kConfigValueNumber=1 } type; struct config_entry *next_entry; } *


decompiled_code:
int config_print_pretty_rep()
{
const char **v0; // rbx
unsigned int v1; // ebp
int result; // eax

v0 = (const char **)config_list;
if ( config_list )
{
v1 = 0;
do
{
printf(
"Item %.4i: 0x%0.8X, points to 0x%.8X, content at 0x%.8X\n",
v1,
(unsigned int)v0,
*((unsigned int *)v0 + 6),
*((unsigned int *)v0 + 2));
if ( *((_DWORD *)v0 + 4) )
result = printf("\t %s: %lli\n", *v0, v0[1]);
else
result = printf("\t %s: %s\n", *v0, v0[1]);
++v1;
v0 = (const char **)v0[3];
}
while ( v0 );
}
return result;
}

canonical_decompiled_code:
int func5()
{
const char **v0; // rbx
unsigned int v1; // ebp
int result; // eax

v0 = (const char **)config_list;
if ( config_list )
{
v1 = 0;
do
{
printf(
"Item %.4i: 0x%0.8X, points to 0x%.8X, content at 0x%.8X\n",
v1,
(unsigned int)v0,
*((unsigned int *)v0 + 6),
*((unsigned int *)v0 + 2));
if ( *((_DWORD *)v0 + 4) )
result = printf("\t %s: %lli\n", *v0, v0[1]);
else
result = printf("\t %s: %s\n", *v0, v0[1]);
++v1;
v0 = (const char **)v0[3];
}
while ( v0 );
}
return result;
}

original_code:
void config_print_pretty_rep() {
    config_entry *entry = config_list;
    int i = 0;
    while(entry != ((void *)0)) {
        printf("Item %.4i: 0x%0.8X, points to 0x%.8X, content at 0x%.8X\n", i, (int) entry, (int) entry->next_entry, (int)entry->data);
        if(entry->type == kConfigValueString) {
            printf("\t   %s: %s\n", entry->key, entry->data);
        } else {
            printf("\t   %s: %lli\n", entry->key, (long long) entry->data);
        }
        i++;
        entry = (config_entry *) entry->next_entry;
    }
}

canonical_original_code:
void config_print_pretty_rep() {
    struct config_entry *entry = config_list;
    int i = 0;
    while(entry != ((void *)0)) {
        printf("Item %.4i: 0x%0.8X, points to 0x%.8X, content at 0x%.8X\n", i, (int) entry, (int) entry->next_entry, (int)entry->data);
        if(entry->type == kConfigValueString) {
            printf("\t   %s: %s\n", entry->key, entry->data);
        } else {
            printf("\t   %s: %lli\n", entry->key, (long long) entry->data);
        }
        i++;
        entry = (struct config_entry *) entry->next_entry;
    }
}

variable_types: i int


decompiled_code:
void __fastcall ws_calculate_accept(__int64 a1)
{
const char *v1; // r13
size_t v2; // rbp
void *v3; // rbx
size_t v4; // rax
void *v5; // rbp
__int64 v6; // r13

v1 = *(const char **)(a1 + 16);
v2 = (int)(strlen(v1) + 38);
v3 = malloc(v2);
memset(v3, 0, v2);
strcpy((char *)v3, v1);
strcpy((char *)v3 + strlen(v1), "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
v4 = strlen((const char *)v3);
v5 = (void *)util_sha1(v3, v4);
v6 = util_base64_encode(v5, 20LL);
free(v3);
free(v5);
*(_QWORD *)(a1 + 24) = v6;
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1)
{
const char *v1; // r13
size_t v2; // rbp
void *v3; // rbx
size_t v4; // rax
void *v5; // rbp
__int64 v6; // r13

v1 = *(const char **)(a1 + 16);
v2 = (int)(strlen(v1) + 38);
v3 = malloc(v2);
memset(v3, 0, v2);
strcpy((char *)v3, v1);
strcpy((char *)v3 + strlen(v1), "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
v4 = strlen((const char *)v3);
v5 = (void *)util_sha1(v3, v4);
v6 = util_base64_encode(v5, 20LL);
free(v3);
free(v5);
*(_QWORD *)(a1 + 24) = v6;
}

original_code:
void ws_calculate_accept(WS_Client *client) {
 int bufSize = strlen(client->ws_key) + strlen("258EAFA5-E914-47DA-95CA-C5AB0DC85B11") + 2;
 char *accept_buf = malloc(bufSize);
 memset(accept_buf, 0x00, bufSize);
 strcpy(accept_buf, client->ws_key);
 strcpy(accept_buf+strlen(client->ws_key), "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
 char *sha1_accept_buf = util_sha1(accept_buf, strlen(accept_buf));
 char *base64_accept_header = util_base64_encode(sha1_accept_buf, 20);
 free(accept_buf);
 free(sha1_accept_buf);
 client->ws_accept = base64_accept_header;
}

canonical_original_code:
void ws_calculate_accept(struct WS_Client *client) {
 int bufSize = strlen(client->ws_key) + strlen("258EAFA5-E914-47DA-95CA-C5AB0DC85B11") + 2;
 char *accept_buf = malloc(bufSize);
 memset(accept_buf, 0x00, bufSize);
 strcpy(accept_buf, client->ws_key);
 strcpy(accept_buf+strlen(client->ws_key), "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
 char *sha1_accept_buf = util_sha1(accept_buf, strlen(accept_buf));
 char *base64_accept_header = util_base64_encode(sha1_accept_buf, 20);
 free(accept_buf);
 free(sha1_accept_buf);
 client->ws_accept = base64_accept_header;
}

variable_types: struct WS_Client { int read_socket; char *endpoint; char *ws_key; char *ws_accept; char *ws_protocol; int ws_version; void *headers; } *

variable_types: bufSize int