createAudioBuffer

decompiled_code:
__int64 __fastcall createAudioBuffer(unsigned int a1)
{
__int64 v1; // r12
__int64 v2; // rax
__int64 result; // rax

v1 = safe_malloc(24LL);
v2 = safe_malloc(a1);
*(_DWORD *)(v1 + 8) = a1;
*(_QWORD *)v1 = v2;
result = v1;
*(_DWORD *)(v1 + 12) = 0;
*(_BYTE *)(v1 + 16) = 0;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
__int64 v1; // r12
__int64 v2; // rax
__int64 result; // rax

v1 = safe_malloc(24LL);
v2 = safe_malloc(a1);
*(_DWORD *)(v1 + 8) = a1;
*(_QWORD *)v1 = v2;
result = v1;
*(_DWORD *)(v1 + 12) = 0;
*(_BYTE *)(v1 + 16) = 0;
return result;
}

original_code:
AudioBuffer *createAudioBuffer(uint size)
{
  AudioBuffer *buffer = ((void *)0);
  buffer = safe_malloc(sizeof(AudioBuffer));
  buffer->data = safe_malloc(size);
  buffer->capacity = size;
  buffer->count = 0;
  buffer->full = 0;
  return buffer;
}

canonical_original_code:
struct AudioBuffer *createAudioBuffer(unsigned int size)
{
  struct AudioBuffer *buffer = ((void *)0);
  buffer = safe_malloc(sizeof(AudioBuffer));
  buffer->data = safe_malloc(size);
  buffer->capacity = size;
  buffer->count = 0;
  buffer->full = 0;
  return buffer;
}

variable_types:
size
unsigned int

return_type: struct AudioBuffer { unsigned char *data; unsigned int capacity; unsigned int count; _Bool full; } *


error

repo: trewtzu/JAC-RMIT-OSP

decompiled_code:
void __fastcall __noreturn error(const char *a1)
{
perror(a1);
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(const char *a1)
{
perror(a1);
exit(0);
}

original_code:
void error(const char *msg)
{
 perror(msg);
 exit(0);
}

canonical_original_code:
void error(char *msg)
{
 perror(msg);
 exit(0);
}

variable_types:
msg
char *

return_type: void


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int16 v3; // r12
int v4; // ebp
struct hostent *v5; // rax
size_t v6; // rdx
const void **v7; // rax
const void *v8; // rsi
socklen_t v9; // edx
pthread_t newthread; // [rsp+8h] [rbp-30h] BYREF
struct sockaddr v12[2]; // [rsp+10h] [rbp-28h] BYREF

if ( argc <= 2 )
{
fprintf(stderr, "usage %s hostname port\n", *argv);
exit(0);
}
v3 = atoi(argv[2]);
v4 = socket(2, 1, 0);
if ( v4 < 0 )
error("ERROR opening socket");
v5 = gethostbyname(argv[1]);
if ( !v5 )
{
fwrite("ERROR, no such host\n", 1uLL, 0x14uLL, stderr);
exit(0);
}
v6 = v5->h_length;
v7 = (const void **)v5->h_addr_list;
v12[0] = 0LL;
v8 = *v7;
v12[0].sa_family = 2;
memmove(&v12[0].sa_data[2], v8, v6);
*(_WORD *)v12[0].sa_data = _bswap_16(v3);
if ( connect(v4, v12, v9) < 0 )
error("ERROR connecting");
pthread_create(&newthread, 0LL, (void *(*)(void *))beginPlayback, 0LL);
begin((unsigned int)v4);
close(v4);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int16 v3; // r12
int v4; // ebp
struct hostent *v5; // rax
size_t v6; // rdx
const void **v7; // rax
const void *v8; // rsi
socklen_t v9; // edx
pthread_t newthread; // [rsp+8h] [rbp-30h] BYREF
struct sockaddr v12[2]; // [rsp+10h] [rbp-28h] BYREF

if ( argc <= 2 )
{
fprintf(stderr, "usage %s hostname port\n", *argv);
exit(0);
}
v3 = atoi(argv[2]);
v4 = socket(2, 1, 0);
if ( v4 < 0 )
func1("ERROR opening socket");
v5 = gethostbyname(argv[1]);
if ( !v5 )
{
fwrite("ERROR, no such host\n", 1uLL, 0x14uLL, stderr);
exit(0);
}
v6 = v5->h_length;
v7 = (const void **)v5->h_addr_list;
v12[0] = 0LL;
v8 = *v7;
v12[0].sa_family = 2;
memmove(&v12[0].sa_data[2], v8, v6);
*(_WORD *)v12[0].sa_data = func0(v3);
if ( connect(v4, v12, v9) < 0 )
func1("ERROR connecting");
pthread_create(&newthread, 0LL, (void *(*)(void *))beginPlayback, 0LL);
begin((unsigned int)v4);
close(v4);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 int sockfd, portno, n;
 struct sockaddr_in serv_addr;
 struct hostent *server;
 if (argc < 3) {
  fprintf(stderr,"usage %s hostname port\n", argv[0]);
  exit(0);
 }
 portno = atoi(argv[2]);
 sockfd = socket(2, SOCK_STREAM, 0);
 if (sockfd < 0)
  error("ERROR opening socket");
 server = gethostbyname(argv[1]);
 if (server == ((void *)0)) {
  fprintf(stderr,"ERROR, no such host\n");
  exit(0);
 }
 (memset(((char *) &serv_addr), '\0', (sizeof(serv_addr))), (void) 0);
 serv_addr.sin_family = 2;
 bcopy((char *)server->h_addr_list[0],
   (char *)&serv_addr.sin_addr.s_addr,
   server->h_length);
 serv_addr.sin_port = htons(portno);
 if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
  error("ERROR connecting");
 pthread_t playbackThread;
 pthread_create(&playbackThread, ((void *)0), beginPlayback, ((void *)0));
 begin(sockfd);
 close(sockfd);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
 int sockfd; int portno; int n;
 struct sockaddr_in serv_addr;
 struct hostent *server;
 if (argc < 3) {
  fprintf(stderr,"usage %s hostname port\n", argv[0]);
  exit(0);
 }
 portno = atoi(argv[2]);
 sockfd = socket(2, SOCK_STREAM, 0);
 if (sockfd < 0)
  error("ERROR opening socket");
 server = gethostbyname(argv[1]);
 if (server == ((void *)0)) {
  fprintf(stderr,"ERROR, no such host\n");
  exit(0);
 }
 (memset(((char *) &serv_addr), '\0', (sizeof(serv_addr))), (void) 0);
 serv_addr.sin_family = 2;
 bcopy((char *)server->h_addr_list[0],
   (char *)&serv_addr.sin_addr.s_addr,
   server->h_length);
 serv_addr.sin_port = htons(portno);
 if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
  error("ERROR connecting");
 unsigned long playbackThread;
 pthread_create(&playbackThread, ((void *)0), beginPlayback, ((void *)0));
 begin(sockfd);
 close(sockfd);
 return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: sockfd int

variable_types: portno int

variable_types: n int

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

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

variable_types:
playbackThread
unsigned long

return_type: int

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

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


repo: trezor/trezor-qrenc

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebp
size_t v4; // rax
__int64 v5; // rbx
int v6; // ebp
int v7; // eax
int v8; // er14
int v9; // ebp
int v10; // er13
int v11; // er15
const char *v12; // rdi
__int64 v13; // rdx
int v14; // ebp
int v15; // eax
int v17; // [rsp+Ch] [rbp-178Ch]
char s[2048]; // [rsp+10h] [rbp-1788h] BYREF
char v19[3976]; // [rsp+810h] [rbp-F88h] BYREF

printf("ECC Level [LMQH]: ");
if ( fgets(s, 2048, stdin) )
{
switch ( s[0] )
{
case 'H':
case 'h':
v3 = 3;
break;
case 'L':
case 'l':
v3 = 0;
break;
case 'Q':
case 'q':
v3 = 2;
break;
default:
v3 = 1;
break;
}
printf("Enter string: ");
if ( fgets(s, 2048, stdin) )
{
v4 = strlen(s);
if ( *((_BYTE *)&v17 + v4 + 3) == 10 )
*((_BYTE *)&v17 + v4 + 3) = 0;
v5 = (unsigned int)qr_encode(v3, 0LL, s, 0LL, v19);
printf("side: %d\n", v5);
v17 = v5 + 1;
if ( (int)v5 + 1 < 0 )
{
putchar(10);
if ( (int)v5 <= 0 )
{
LABEL_21:
putchar(10);
return 0;
}
}
else
{
v6 = 0;
do
{
printf(&byte_6);
v7 = v6++;
}
while ( (int)v5 >= v7 );
putchar(10);
if ( (int)v5 <= 0 )
{
LABEL_19:
v14 = 0;
do
{
printf(&byte_6);
v15 = v14++;
}
while ( (int)v5 >= v15 );
goto LABEL_21;
}
}
v8 = -1;
v9 = 0;
v10 = 0;
do
{
v11 = v8;
printf(&byte_6);
while ( 1 )
{
v12 = &byte_6;
if ( (((int)(unsigned __int8)v19[~v11 >> 3] >> (v11 & 7)) & 1) != 0 )
v12 = " ";
printf(v12);
if ( (int)v5 <= v9 - v11 )
break;
--v11;
}
++v10;
v9 -= v5;
printf(&byte_6, v5, v13, (unsigned int)(v11 - 1));
v8 -= v5;
putchar(10);
}
while ( (int)v5 > v10 );
if ( v17 < 0 )
goto LABEL_21;
goto LABEL_19;
}
}
return 1;
}

original_code:
int main(void)
{
 int side, i, j, a, ecclevel;
 uint8_t bitdata[(((40 * 4 + 17) * (40 * 4 + 17) + 7) / 8)];
 char str[2048];
 printf("ECC Level [LMQH]: ");
 if (!fgets(str, sizeof(str), stdin)) {
  return 1;
 }
 switch (str[0]) {
 case 'l':
 case 'L':
  ecclevel = 0;
  break;
 case 'm':
 case 'M':
 default:
  ecclevel = 1;
  break;
 case 'q':
 case 'Q':
  ecclevel = 2;
  break;
 case 'h':
 case 'H':
  ecclevel = 3;
  break;
 }
 printf("Enter string: ");
 if (!fgets(str, sizeof(str), stdin)) {
  return 1;
 }
 if (str[strlen(str) - 1] == '\n') {
  str[strlen(str) - 1] = 0;
 }
 side = qr_encode(ecclevel, 0, str, 0, bitdata);
 printf("side: %d\n", side);
 for (i = 0; i < side + 2; i++) printf("██");
 printf("\n");
 for (i = 0; i < side; i++) {
  printf("██");
  for (j = 0; j < side; j++) {
   a = i * side + j;
   printf((bitdata[a / 8] & (1 << (7 - a % 8))) ? "  " : "██");
  }
  printf("██");
  printf("\n");
 }
 for (i = 0; i < side + 2; i++) printf("██");
 printf("\n");
 return 0;
}

canonical_original_code:
int main()
{
 int side; int i; int j; int a; int ecclevel;
 unsigned char bitdata[-1];
 char str[2048];
 printf("ECC Level [LMQH]: ");
 if (!fgets(str, sizeof(str), stdin)) {
  return 1;
 }
 switch (str[0]) {
 case 'l':
 case 'L':
  ecclevel = 0;
  break;
 case 'm':
 case 'M':
 default:
  ecclevel = 1;
  break;
 case 'q':
 case 'Q':
  ecclevel = 2;
  break;
 case 'h':
 case 'H':
  ecclevel = 3;
  break;
 }
 printf("Enter string: ");
 if (!fgets(str, sizeof(str), stdin)) {
  return 1;
 }
 if (str[strlen(str) - 1] == '\n') {
  str[strlen(str) - 1] = 0;
 }
 side = qr_encode(ecclevel, 0, str, 0, bitdata);
 printf("side: %d\n", side);
 for (i = 0; i < side + 2; i++) printf("██");
 printf("\n");
 for (i = 0; i < side; i++) {
  printf("██");
  for (j = 0; j < side; j++) {
   a = i * side + j;
   printf((bitdata[a / 8] & (1 << (7 - a % 8))) ? "  " : "██");
  }
  printf("██");
  printf("\n");
 }
 for (i = 0; i < side + 2; i++) printf("██");
 printf("\n");
 return 0;
}

variable_types: side int

variable_types: i int

variable_types: j int

variable_types: a int

variable_types: ecclevel int

variable_types:
bitdata
unsigned char [-1]

variable_types:
str
char [2048]


repo: trigonak/ccbench

decompiled_code:
__int64 color_all()
{
return 1LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 1LL;
}

original_code:
int color_all(int id)
{
  return 1;
}

canonical_original_code:
int color_all(int id)
{
  return 1;
}

variable_types: id int


decompiled_code:
void __fastcall barrier_init(unsigned int a1, __int64 a2, unsigned int (__fastcall *a3)(__int64), int a4)
{
__int64 v5; // r14
_QWORD *v7; // rax
__int64 v8; // rbp
unsigned int i; // ebx
__int64 v10; // rdi

if ( a1 <= 0xF )
{
v5 = 40LL * a1;
v7 = (_QWORD *)(v5 + barriers);
v7[1] = 0LL;
v7[4] = a3;
v7[2] = 0LL;
v7[3] = 0LL;
if ( a4 )
{
LODWORD(v8) = 0;
for ( i = 0; i != a4; ++i )
{
v10 = i;
v8 = a3(v10) + (unsigned int)v8;
}
v7 = (_QWORD *)(v5 + barriers);
}
else
{
v8 = 0LL;
}
*v7 = v8;
}
}

canonical_decompiled_code:
void __fastcall func1(unsigned int a1, __int64 a2, unsigned int (__fastcall *a3)(__int64), int a4)
{
__int64 v5; // r14
_QWORD *v7; // rax
__int64 v8; // rbp
unsigned int i; // ebx
__int64 v10; // rdi

if ( a1 <= 0xF )
{
v5 = 40LL * a1;
v7 = (_QWORD *)(v5 + barriers);
v7[1] = 0LL;
v7[4] = a3;
v7[2] = 0LL;
v7[3] = 0LL;
if ( a4 )
{
LODWORD(v8) = 0;
for ( i = 0; i != a4; ++i )
{
v10 = i;
v8 = a3(v10) + (unsigned int)v8;
}
v7 = (_QWORD *)(v5 + barriers);
}
else
{
v8 = 0LL;
}
*v7 = v8;
}
}

original_code:
void
barrier_init(const uint32_t barrier_num, const uint64_t participants, int (*color)(int),
      const uint32_t total_cores)
{
  if (barrier_num >= 16)
    {
      return;
    }
  barriers[barrier_num].num_crossing1 = 0;
  barriers[barrier_num].num_crossing2 = 0;
  barriers[barrier_num].num_crossing3 = 0;
  barriers[barrier_num].color = color;
  uint32_t ue, num_parts = 0;
  for (ue = 0; ue < total_cores; ue++)
    {
      num_parts += color(ue);
    }
  barriers[barrier_num].num_participants = num_parts;
}

canonical_original_code:
void barrier_init(unsigned int barrier_num, unsigned long participants, int (*color)(int), unsigned int total_cores)
{
  if (barrier_num >= 16)
    {
      return;
    }
  barriers[barrier_num].num_crossing1 = 0;
  barriers[barrier_num].num_crossing2 = 0;
  barriers[barrier_num].num_crossing3 = 0;
  barriers[barrier_num].color = color;
  unsigned int ue; unsigned int num_parts = 0;
  for (ue = 0; ue < total_cores; ue++)
    {
      num_parts += color(ue);
    }
  barriers[barrier_num].num_participants = num_parts;
}

variable_types:
participants
unsigned long

variable_types:
color
int (*)(int)

variable_types:
ue
unsigned int


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

if ( !a1 )
result = shm_unlink("/barrier_mem");
return result;
}

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

if ( !a1 )
result = shm_unlink("/barrier_mem");
return result;
}

original_code:
void
barriers_term(const uint32_t id)
{
  if (id == 0)
    {
      shm_unlink("/barrier_mem");
    }
}

canonical_original_code:
void barriers_term(unsigned int id)
{
  if (id == 0)
    {
      shm_unlink("/barrier_mem");
    }
}

variable_types:
id
unsigned int


getticks

repo: trigonak/sspfd

decompiled_code:
unsigned __int64 getticks()
{
return __rdtsc();
}

canonical_decompiled_code:
unsigned __int64 func0()
{
return __rdtsc();
}

original_code:
static inline ticks
getticks(void)
{
  unsigned hi, lo;
  __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
  return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}

canonical_original_code:
static inline unsigned long getticks()
{
  unsigned int hi; unsigned int lo;
  __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
  return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}

variable_types:
hi
unsigned int

variable_types:
lo
unsigned int

return_type: unsigned long


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
__int64 v4; // rax
int *v5; // rax
int *v6; // r12
__int64 v8; // rax
unsigned __int64 v9; // r14
__int64 v10; // rax
__int64 v11; // rsi
unsigned __int64 *v12; // rsi
unsigned __int64 v13; // rax
__int64 v14; // rcx
__int64 v15; // rax
unsigned __int64 *v16; // rsi
int v17; // eax
unsigned __int64 v18; // rax
__int64 v19; // rcx
__int64 v20; // rax
unsigned __int64 v21; // r14
unsigned __int64 v22; // rax
unsigned __int64 *v23; // rcx
int v24; // esi
__int64 v25; // rax
__int64 v26; // rax
unsigned __int64 v27; // rax
unsigned __int64 *v28; // rcx
unsigned __int64 v29; // rax
_QWORD *v30; // rcx
__int64 v31; // rax
unsigned __int64 *v32; // rsi
unsigned __int64 v33; // rax
__int64 v34; // rcx
int longind; // [rsp+4h] [rbp-194h] BYREF
unsigned __int64 v37; // [rsp+8h] [rbp-190h]
struct option longopts; // [rsp+10h] [rbp-188h] BYREF
const char *v39; // [rsp+30h] [rbp-168h]
int v40; // [rsp+38h] [rbp-160h]
__int64 v41; // [rsp+40h] [rbp-158h]
int v42; // [rsp+48h] [rbp-150h]
const char *v43; // [rsp+50h] [rbp-148h]
int v44; // [rsp+58h] [rbp-140h]
__int64 v45; // [rsp+60h] [rbp-138h]
int v46; // [rsp+68h] [rbp-130h]
__int64 v47; // [rsp+70h] [rbp-128h]
int v48; // [rsp+78h] [rbp-120h]
__int64 v49; // [rsp+80h] [rbp-118h]
int v50; // [rsp+88h] [rbp-110h]
char v51[264]; // [rsp+90h] [rbp-108h] BYREF

longopts.name = "help";
longopts.has_arg = 0;
longopts.flag = 0LL;
longopts.val = 104;
v39 = "num_ops";
v40 = 1;
v41 = 0LL;
v42 = 111;
v43 = "num_stores";
v44 = 1;
v45 = 0LL;
v46 = 115;
v47 = 0LL;
v48 = 0;
v49 = 0LL;
v50 = 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 == 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 )
{
if ( v3 )
{
if ( v3 == 63 )
{
puts("Use -h or --help for help");
exit(0);
}
LABEL_48:
exit(1);
}
}
else if ( v3 == 111 )
{
num_ops = atoi(optarg);
}
else
{
if ( v3 != 115 )
goto LABEL_48;
num_stores = atoi(optarg);
}
}
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 = (int *)malloc(4uLL);
v6 = v5;
if ( !v5 )
_assert_fail("dummy != NULL", "sspfd_test.c", 0x75u, "main");
*v5 = 13;
while ( num_stores-- )
{
v8 = sspfd_get_id();
printf("[%02lu] ", v8);
printf("* testing store %lu", num_stores);
putchar(10);
fflush(stdout);
v9 = num_stores;
if ( num_stores == 2 )
{
v31 = sspfd_get_id();
printf("[%02lu] ", v31);
printf("** asm volatile(\"pause\")");
putchar(10);
fflush(stdout);
v11 = num_ops;
if ( num_ops )
{
do
{
v32 = (unsigned __int64 *)(sspfd_s + 8 * num_stores);
*v32 = getticks();
_mm_pause();
v33 = getticks();
*(_QWORD *)(*(_QWORD *)(sspfd_store + 8 * num_stores) + 8 * v34) = v33
- (sspfd_correction
+ *(_QWORD *)(sspfd_s + 8 * num_stores));
v11 = num_ops;
}
while ( num_ops > (unsigned __int64)(v34 + 1) );
}
}
else if ( (unsigned __int64)num_stores > 2 )
{
if ( num_stores == 3 )
{
v15 = sspfd_get_id();
printf("[%02lu] ", v15);
printf("** for (rr = 0; rr < 1e3; rr++);");
putchar(10);
fflush(stdout);
v11 = num_ops;
if ( num_ops )
{
do
{
v16 = (unsigned __int64 *)(sspfd_s + 8 * num_stores);
*v16 = getticks();
v37 = 0LL;
v17 = 0;
while ( (double)v17 < 1000.0 )
{
while ( 1 )
{
v17 = ++v37;
if ( (v37 & 0x8000000000000000LL) == 0LL )
break;
if ( (double)(int)(v37 & 1 | (v37 >> 1)) + (double)(int)(v37 & 1 | (v37 >> 1)) >= 1000.0 )
goto LABEL_33;
}
}
LABEL_33:
v18 = getticks();
*(_QWORD *)(*(_QWORD *)(sspfd_store + 8 * num_stores) + 8 * v19) = v18
- (sspfd_correction
+ *(_QWORD *)(sspfd_s + 8 * num_stores));
v11 = num_ops;
}
while ( num_ops > (unsigned __int64)(v19 + 1) );
}
}
else
{
v20 = sspfd_get_id();
printf("[%02lu] ", v20);
printf("** L1 access");
putchar(10);
fflush(stdout);
v11 = num_ops;
if ( num_ops )
{
v21 = 0LL;
do
{
v22 = getticks();
*v23 = v22;
v24 = *v6;
_mm_lfence();
*(_QWORD *)(*(_QWORD *)(sspfd_store + 8 * num_stores) + 8 * v21) = getticks()
- (sspfd_correction
+ *(_QWORD *)(sspfd_s + 8 * num_stores));
if ( v24 != 13 )
{
v25 = sspfd_get_id();
printf("[%02lu] ", v25);
printf("what?");
putchar(10);
fflush(stdout);
}
v11 = num_ops;
++v21;
}
while ( num_ops > v21 );
}
}
}
else if ( num_stores )
{
v10 = sspfd_get_id();
printf("[%02lu] ", v10);
printf("** asm volatile(\"mfence\")");
putchar(10);
fflush(stdout);
v11 = num_ops;
if ( num_ops )
{
do
{
v12 = (unsigned __int64 *)(sspfd_s + 8 * num_stores);
*v12 = getticks();
_mm_mfence();
v13 = getticks();
*(_QWORD *)(*(_QWORD *)(sspfd_store + 8 * num_stores) + 8 * v14) = v13
- (sspfd_correction
+ *(_QWORD *)(sspfd_s + 8 * num_stores));
v11 = num_ops;
}
while ( num_ops > (unsigned __int64)(v14 + 1) );
}
}
else
{
v26 = sspfd_get_id();
printf("[%02lu] ", v26);
printf("** asm volatile(\"nop\")");
putchar(10);
fflush(stdout);
v11 = num_ops;
if ( num_ops )
{
do
{
v27 = getticks();
*v28 = v27;
v29 = getticks();
v30 = (_QWORD *)(*(_QWORD *)(sspfd_store + 8 * num_stores) + 8 * v9++);
*v30 = v29 - (sspfd_correction + *(_QWORD *)(sspfd_s + 8 * num_stores));
v11 = num_ops;
}
while ( num_ops > v9 );
}
}
sspfd_get_stats(num_stores, v11, v51);
sspfd_print_stats(v51);
}
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
int *v5; // rax
int *v6; // r12
__int64 v8; // rax
unsigned __int64 v9; // r14
__int64 v10; // rax
__int64 v11; // rsi
unsigned __int64 *v12; // rsi
unsigned __int64 v13; // rax
__int64 v14; // rcx
__int64 v15; // rax
unsigned __int64 *v16; // rsi
int v17; // eax
unsigned __int64 v18; // rax
__int64 v19; // rcx
__int64 v20; // rax
unsigned __int64 v21; // r14
unsigned __int64 v22; // rax
unsigned __int64 *v23; // rcx
int v24; // esi
__int64 v25; // rax
__int64 v26; // rax
unsigned __int64 v27; // rax
unsigned __int64 *v28; // rcx
unsigned __int64 v29; // rax
_QWORD *v30; // rcx
__int64 v31; // rax
unsigned __int64 *v32; // rsi
unsigned __int64 v33; // rax
__int64 v34; // rcx
int longind; // [rsp+4h] [rbp-194h] BYREF
unsigned __int64 v37; // [rsp+8h] [rbp-190h]
struct option longopts; // [rsp+10h] [rbp-188h] BYREF
const char *v39; // [rsp+30h] [rbp-168h]
int v40; // [rsp+38h] [rbp-160h]
__int64 v41; // [rsp+40h] [rbp-158h]
int v42; // [rsp+48h] [rbp-150h]
const char *v43; // [rsp+50h] [rbp-148h]
int v44; // [rsp+58h] [rbp-140h]
__int64 v45; // [rsp+60h] [rbp-138h]
int v46; // [rsp+68h] [rbp-130h]
__int64 v47; // [rsp+70h] [rbp-128h]
int v48; // [rsp+78h] [rbp-120h]
__int64 v49; // [rsp+80h] [rbp-118h]
int v50; // [rsp+88h] [rbp-110h]
char v51[264]; // [rsp+90h] [rbp-108h] BYREF

longopts.name = "help";
longopts.has_arg = 0;
longopts.flag = 0LL;
longopts.val = 104;
v39 = "num_ops";
v40 = 1;
v41 = 0LL;
v42 = 111;
v43 = "num_stores";
v44 = 1;
v45 = 0LL;
v46 = 115;
v47 = 0LL;
v48 = 0;
v49 = 0LL;
v50 = 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 == 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 )
{
if ( v3 )
{
if ( v3 == 63 )
{
puts("Use -h or --help for help");
exit(0);
}
LABEL_48:
exit(1);
}
}
else if ( v3 == 111 )
{
num_ops = atoi(optarg);
}
else
{
if ( v3 != 115 )
goto LABEL_48;
num_stores = atoi(optarg);
}
}
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 = (int *)malloc(4uLL);
v6 = v5;
if ( !v5 )
_assert_fail("dummy != NULL", "sspfd_test.c", 0x75u, "main");
*v5 = 13;
while ( num_stores-- )
{
v8 = sspfd_get_id();
printf("[%02lu] ", v8);
printf("* testing store %lu", num_stores);
putchar(10);
fflush(stdout);
v9 = num_stores;
if ( num_stores == 2 )
{
v31 = sspfd_get_id();
printf("[%02lu] ", v31);
printf("** asm volatile(\"pause\")");
putchar(10);
fflush(stdout);
v11 = num_ops;
if ( num_ops )
{
do
{
v32 = (unsigned __int64 *)(sspfd_s + 8 * num_stores);
*v32 = func0();
_mm_pause();
v33 = func0();
*(_QWORD *)(*(_QWORD *)(sspfd_store + 8 * num_stores) + 8 * v34) = v33
- (sspfd_correction
+ *(_QWORD *)(sspfd_s + 8 * num_stores));
v11 = num_ops;
}
while ( num_ops > (unsigned __int64)(v34 + 1) );
}
}
else if ( (unsigned __int64)num_stores > 2 )
{
if ( num_stores == 3 )
{
v15 = sspfd_get_id();
printf("[%02lu] ", v15);
printf("** for (rr = 0; rr < 1e3; rr++);");
putchar(10);
fflush(stdout);
v11 = num_ops;
if ( num_ops )
{
do
{
v16 = (unsigned __int64 *)(sspfd_s + 8 * num_stores);
*v16 = func0();
v37 = 0LL;
v17 = 0;
while ( (double)v17 < 1000.0 )
{
while ( 1 )
{
v17 = ++v37;
if ( (v37 & 0x8000000000000000LL) == 0LL )
break;
if ( (double)(int)(v37 & 1 | (v37 >> 1)) + (double)(int)(v37 & 1 | (v37 >> 1)) >= 1000.0 )
goto LABEL_33;
}
}
LABEL_33:
v18 = func0();
*(_QWORD *)(*(_QWORD *)(sspfd_store + 8 * num_stores) + 8 * v19) = v18
- (sspfd_correction
+ *(_QWORD *)(sspfd_s + 8 * num_stores));
v11 = num_ops;
}
while ( num_ops > (unsigned __int64)(v19 + 1) );
}
}
else
{
v20 = sspfd_get_id();
printf("[%02lu] ", v20);
printf("** L1 access");
putchar(10);
fflush(stdout);
v11 = num_ops;
if ( num_ops )
{
v21 = 0LL;
do
{
v22 = func0();
*v23 = v22;
v24 = *v6;
_mm_lfence();
*(_QWORD *)(*(_QWORD *)(sspfd_store + 8 * num_stores) + 8 * v21) = func0()
- (sspfd_correction
+ *(_QWORD *)(sspfd_s + 8 * num_stores));
if ( v24 != 13 )
{
v25 = sspfd_get_id();
printf("[%02lu] ", v25);
printf("what?");
putchar(10);
fflush(stdout);
}
v11 = num_ops;
++v21;
}
while ( num_ops > v21 );
}
}
}
else if ( num_stores )
{
v10 = sspfd_get_id();
printf("[%02lu] ", v10);
printf("** asm volatile(\"mfence\")");
putchar(10);
fflush(stdout);
v11 = num_ops;
if ( num_ops )
{
do
{
v12 = (unsigned __int64 *)(sspfd_s + 8 * num_stores);
*v12 = func0();
_mm_mfence();
v13 = func0();
*(_QWORD *)(*(_QWORD *)(sspfd_store + 8 * num_stores) + 8 * v14) = v13
- (sspfd_correction
+ *(_QWORD *)(sspfd_s + 8 * num_stores));
v11 = num_ops;
}
while ( num_ops > (unsigned __int64)(v14 + 1) );
}
}
else
{
v26 = sspfd_get_id();
printf("[%02lu] ", v26);
printf("** asm volatile(\"nop\")");
putchar(10);
fflush(stdout);
v11 = num_ops;
if ( num_ops )
{
do
{
v27 = func0();
*v28 = v27;
v29 = func0();
v30 = (_QWORD *)(*(_QWORD *)(sspfd_store + 8 * num_stores) + 8 * v9++);
*v30 = v29 - (sspfd_correction + *(_QWORD *)(sspfd_s + 8 * num_stores));
v11 = num_ops;
}
while ( num_ops > v9 );
}
}
sspfd_get_stats(num_stores, v11, v51);
sspfd_print_stats(v51);
}
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 *


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 || (v2 = shm_open(v4, 66, 0x1F8u), 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 || (v2 = shm_open(v4, 66, 0x1F8u), 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)
{
char v1; // al
__int64 result; // rax
__int64 v3; // r8

v1 = tm2c_free_num;
if ( (unsigned __int8)tm2c_free_num <= 2u )
{
v3 = alloc_next + tm2c_app_mem;
alloc_next += a1;
result = v3;
}
else
{
--tm2c_free_num;
result = tm2c_free_list[(unsigned __int8)(tm2c_free_cur - v1)];
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
char v1; // al
__int64 result; // rax
__int64 v3; // r8

v1 = tm2c_free_num;
if ( (unsigned __int8)tm2c_free_num <= 2u )
{
v3 = alloc_next + tm2c_app_mem;
alloc_next += a1;
result = v3;
}
else
{
--tm2c_free_num;
result = tm2c_free_list[(unsigned __int8)(tm2c_free_cur - v1)];
}
return result;
}

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
char v2; // dl

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

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

result = (unsigned __int8)tm2c_free_cur;
++tm2c_free_num;
v2 = tm2c_free_cur + 1;
tm2c_free_list[(unsigned __int8)tm2c_free_cur] = a1;
tm2c_free_cur = v2;
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)
{
__int64 result; // rax
double *v3; // rdx

result = index_0;
v3 = (double *)((char *)NODES + 24 * index_0);
*v3 = a1;
v3[1] = a2;
index_0 = result + 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(double a1, double a2)
{
__int64 result; // rax
double *v3; // rdx

result = index_0;
v3 = (double *)((char *)NODES + 24 * index_0);
*v3 = a1;
v3[1] = a2;
index_0 = result + 1;
return result;
}

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


decompiled_code:
__int64 __fastcall set_up(int a1, int a2)
{
_DWORD *v2; // rbp
_QWORD *v3; // rax
_QWORD *v4; // r12
__int64 v5; // rbx
_DWORD *v6; // rax
_DWORD *v7; // rdx
int v8; // eax
_DWORD *v9; // rsi
_DWORD *v10; // rsi
__int64 result; // rax

LATTICE = malloc(12LL * a1);
v2 = LATTICE;
v3 = malloc(24LL * a2);
NODES = v3;
if ( a2 > 0 )
{
v4 = v3 + 2;
v5 = (__int64)&v3[3 * (unsigned int)(a2 - 1) + 5];
do
{
v6 = malloc(0x18uLL);
*v4 = v6;
v7 = v6 + 6;
do
*v6++ = -1;
while ( v6 != v7 );
v4 += 3;
}
while ( v4 != (_QWORD *)v5 );
}
v8 = new_node(0.0, 1.0);
v9 = LATTICE;
*v2 = v8;
v9[1] = new_node(0.866, 0.5);
v10 = LATTICE;
result = new_node(-0.866, 0.5);
v10[2] = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, int a2)
{
_DWORD *v2; // rbp
_QWORD *v3; // rax
_QWORD *v4; // r12
__int64 v5; // rbx
_DWORD *v6; // rax
_DWORD *v7; // rdx
int v8; // eax
_DWORD *v9; // rsi
_DWORD *v10; // rsi
__int64 result; // rax

LATTICE = malloc(12LL * a1);
v2 = LATTICE;
v3 = malloc(24LL * a2);
NODES = v3;
if ( a2 > 0 )
{
v4 = v3 + 2;
v5 = (__int64)&v3[3 * (unsigned int)(a2 - 1) + 5];
do
{
v6 = malloc(0x18uLL);
*v4 = v6;
v7 = v6 + 6;
do
*v6++ = -1;
while ( v6 != v7 );
v4 += 3;
}
while ( v4 != (_QWORD *)v5 );
}
v8 = func1(0.0, 1.0);
v9 = LATTICE;
*v2 = v8;
v9[1] = func1(0.866, 0.5);
v10 = LATTICE;
result = func1(-0.866, 0.5);
v10[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; }


decompiled_code:
__int64 __fastcall add_edge(int a1, int a2)
{
_DWORD *v2; // rcx
_DWORD *v3; // rax
__int64 result; // rax
_DWORD *v5; // rdx

v2 = (_DWORD *)*((_QWORD *)NODES + 3 * a1 + 2);
if ( *v2 != -1 )
{
v3 = v2 + 1;
do
v2 = v3++;
while ( *(v3 - 1) != -1 );
}
result = 3LL * a2;
v5 = (_DWORD *)*((_QWORD *)NODES + 3 * a2 + 2);
if ( *v5 != -1 )
{
result = (__int64)(v5 + 1);
do
{
v5 = (_DWORD *)result;
result += 4LL;
}
while ( *(_DWORD *)(result - 4) != -1 );
}
*v2 = a2;
*v5 = a1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, int a2)
{
_DWORD *v2; // rcx
_DWORD *v3; // rax
__int64 result; // rax
_DWORD *v5; // rdx

v2 = (_DWORD *)*((_QWORD *)NODES + 3 * a1 + 2);
if ( *v2 != -1 )
{
v3 = v2 + 1;
do
v2 = v3++;
while ( *(v3 - 1) != -1 );
}
result = 3LL * a2;
v5 = (_DWORD *)*((_QWORD *)NODES + 3 * a2 + 2);
if ( *v5 != -1 )
{
result = (__int64)(v5 + 1);
do
{
v5 = (_DWORD *)result;
result += 4LL;
}
while ( *(_DWORD *)(result - 4) != -1 );
}
*v2 = a2;
*v5 = a1;
return result;
}

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; // r8
int v2; // edi
int v3; // er10
int v4; // esi
int v5; // er9
__int64 result; // rax
__int64 v7; // r11

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

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
__int64 v1; // r8
int v2; // edi
int v3; // er10
int v4; // esi
int v5; // er9
__int64 result; // rax
__int64 v7; // r11

if ( a1 > 0 )
{
v1 = 0LL;
do
{
v2 = *(_DWORD *)((char *)LATTICE + v1);
func3(v2, *(_DWORD *)((char *)LATTICE + v1 + 4));
v4 = v3;
func3(v2, v3);
result = func3(v5, v4);
}
while ( v1 != v7 );
}
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, int a2, int a3)
{
int v3; // er14
char *v5; // rbp
char *v6; // r15
__int64 v7; // rbx
__int64 v8; // rax

if ( a1 )
{
v3 = a1;
v5 = (char *)LATTICE;
do
{
v6 = v5;
v5 = (char *)malloc(12LL * a2);
if ( a3 > 0 )
{
v7 = 0LL;
do
{
v8 = 3 * v7++;
build_inner_triangles(*(_QWORD *)&v6[4 * v8]);
v6 = (char *)LATTICE;
}
while ( v7 != a3 );
}
a3 *= 3;
free(v6);
LATTICE = v5;
--v3;
}
while ( v3 );
}
}

canonical_decompiled_code:
void __fastcall func6(int a1, int a2, int a3)
{
int v3; // er14
char *v5; // rbp
char *v6; // r15
__int64 v7; // rbx
__int64 v8; // rax

if ( a1 )
{
v3 = a1;
v5 = (char *)LATTICE;
do
{
v6 = v5;
v5 = (char *)malloc(12LL * a2);
if ( a3 > 0 )
{
v7 = 0LL;
do
{
v8 = 3 * v7++;
build_inner_triangles(*(_QWORD *)&v6[4 * v8]);
v6 = (char *)LATTICE;
}
while ( v7 != a3 );
}
a3 *= 3;
free(v6);
LATTICE = v5;
--v3;
}
while ( v3 );
}
}

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)
{
int v2; // [rsp+8h] [rbp-10h] BYREF
unsigned int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

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

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

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

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(char *a1, int a2, int a3, int a4)
{
int v6; // er14
double *v7; // rax
int v8; // er12

if ( a2 > 0 )
memset(a1, 0, 8LL * a2);
if ( a2 < a4 )
_assert_fail("number <= n_nodes", "densities.c", 0x25u, "compute_densities");
v6 = 0;
while ( v6 < a4 )
{
while ( 1 )
{
v7 = (double *)&a1[8 * (rand() % a2)];
if ( 1.0 == *v7 )
break;
++v6;
*(_QWORD *)v7 = LC2;
if ( v6 >= a4 )
goto LABEL_9;
}
}
LABEL_9:
v8 = 0;
if ( a3 > 0 )
{
do
{
++v8;
distribute_densities(a2, a1, NODES);
}
while ( a3 != v8 );
}
}

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

if ( a2 > 0 )
memset(a1, 0, 8LL * a2);
if ( a2 < a4 )
_assert_fail("number <= n_nodes", "densities.c", 0x25u, "compute_densities");
v6 = 0;
while ( v6 < a4 )
{
while ( 1 )
{
v7 = (double *)&a1[8 * (rand() % a2)];
if ( 1.0 == *v7 )
break;
++v6;
*(_QWORD *)v7 = LC2;
if ( v6 >= a4 )
goto LABEL_9;
}
}
LABEL_9:
v8 = 0;
if ( a3 > 0 )
{
do
{
++v8;
func0(a2, a1, NODES);
}
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 v0; // rbx
__int64 i; // rsi
const char *v2; // rdx

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

canonical_decompiled_code:
int func0()
{
__int64 v0; // rbx
__int64 i; // rsi
const char *v2; // rdx

v0 = 0LL;
puts("\nProgram for executing random walk on a lattice. Options:\n");
for ( i = 104LL; ; i = (unsigned int)LETTERS[v0] )
{
v2 = (&DESCRIPTIONS)[v0++];
printf("-%c\t\t%s\n", i, v2);
if ( v0 == 5 )
break;
}
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; }


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", v4, v5, v6);
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; // ebp
int *v6; // rax
char *v7; // rax
__int64 result; // rax
unsigned __int8 v9; // r12
int v10; // eax
int v11; // edx
double v12; // xmm0_8
int *v13; // rax
char *v14; // 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;
}
v12 = (double)v10 * 0.0625;
result = 0LL;
*a3 = v12;
}
else
{
v13 = __errno_location();
v14 = strerror(*v13);
fprintf(stderr, "Unable to read from slave, %s\n", v14);
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; // ebp
int *v6; // rax
char *v7; // rax
__int64 result; // rax
unsigned __int8 v9; // r12
int v10; // eax
int v11; // edx
double v12; // xmm0_8
int *v13; // rax
char *v14; // 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;
}
v12 = (double)v10 * 0.0625;
result = 0LL;
*a3 = v12;
}
else
{
v13 = __errno_location();
v14 = strerror(*v13);
fprintf(stderr, "Unable to read from slave, %s\n", v14);
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; // rbp
__int64 v4; // rax
struct hostent *v5; // rax
__int16 v6; // ax
__int16 v7; // ax
int *v8; // rbp
int v9; // edx
int v10; // eax
int v11; // eax
const char **v12; // rbp
int result; // eax
struct sockaddr v14; // [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)calloc(4uLL, 1uLL);
v14 = 0LL;
gethostname(name, 0x400uLL);
v5 = gethostbyname(name);
if ( v5 )
{
v14.sa_family = v5->h_addrtype;
v6 = config_get_number("WS_Port");
v7 = _bswap_16(v6);
v8 = (int *)connection;
*(_WORD *)v14.sa_data = v7;
v10 = socket(2, 1, v9);
*v8 = v10;
if ( v10 < 0 )
{
perror("Creating listening socket");
result = 255;
}
else
{
if ( bind(v10, &v14, 0x10u) >= 0 )
{
v11 = config_get_number("WS_ListenBacklog");
listen(*(_DWORD *)connection, v11);
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.");
v12 = (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",
v12);
ws_calculate_accept(v12);
printf("Accept: %s\n", v12[3]);
while ( 1 )
sleep(5u);
}
close(*v8);
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; // rbp
__int64 v4; // rax
struct hostent *v5; // rax
__int16 v6; // ax
__int16 v7; // ax
int *v8; // rbp
int v9; // edx
int v10; // eax
int v11; // eax
const char **v12; // rbp
int result; // eax
struct sockaddr v14; // [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)calloc(4uLL, 1uLL);
v14 = 0LL;
gethostname(name, 0x400uLL);
v5 = gethostbyname(name);
if ( v5 )
{
v14.sa_family = v5->h_addrtype;
v6 = config_get_number("WS_Port");
v7 = func0(v6);
v8 = (int *)connection;
*(_WORD *)v14.sa_data = v7;
v10 = socket(2, 1, v9);
*v8 = v10;
if ( v10 < 0 )
{
perror("Creating listening socket");
result = 255;
}
else
{
if ( bind(v10, &v14, 0x10u) >= 0 )
{
v11 = config_get_number("WS_ListenBacklog");
listen(*(_DWORD *)connection, v11);
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.");
v12 = (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",
v12);
ws_calculate_accept(v12);
printf("Accept: %s\n", v12[3]);
while ( 1 )
sleep(5u);
}
close(*v8);
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 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 Plugin_Endpoint_Registry { void *next; char *endpoint; int (*callback)(struct WS_Client *, char *, unsigned int); }


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

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

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

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

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; // r14
void *v5; // rbp
void *v6; // r14
size_t v7; // rbx
void *v8; // rbp
__int64 (__fastcall *v9)(__int64); // rbx
int v10; // eax
void *v12; // rdi
char *v13; // rax
char *v14; // rax
void *v15; // rdi

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

canonical_decompiled_code:
void *__fastcall func1(char *s, char *a2, __int64 a3)
{
size_t v4; // r14
void *v5; // rbp
void *v6; // r14
size_t v7; // rbx
void *v8; // rbp
__int64 (__fastcall *v9)(__int64); // rbx
int v10; // eax
void *v12; // rdi
char *v13; // rax
char *v14; // rax
void *v15; // rdi

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

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)
{
int v1; // er12
int v3; // edi

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

canonical_decompiled_code:
__int64 __fastcall func1(void *arg)
{
int v1; // er12
int v3; // edi

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

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 v2; // rax
int v3; // edi
int arg; // [rsp+4h] [rbp-14h] BYREF
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

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

canonical_decompiled_code:
void __fastcall __noreturn func4(void *a1)
{
__int64 v2; // rax
int v3; // edi
int arg; // [rsp+4h] [rbp-14h] BYREF
pthread_t newthread[2]; // [rsp+8h] [rbp-10h] BYREF

v2 = config_get_number("WS_Port");
printf("Client listening thread active. Listening for clients on port %lli...\n", v2);
fflush(stdout);
LABEL_2:
while ( 1 )
{
v3 = *(_DWORD *)a1;
if ( socket_listener_should_run != 1 )
break;
while ( 1 )
{
arg = func3(v3);
if ( arg >= 0 )
break;
if ( *_errno_location() == 4 )
goto LABEL_2;
perror("Accept client connection");
v3 = *(_DWORD *)a1;
if ( socket_listener_should_run != 1 )
goto LABEL_6;
}
pthread_create(newthread, 0LL, socket_handler_connection_handler, &arg);
}
LABEL_6:
close(v3);
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 *dest)
{
__int64 v1; // rbx
const char *v2; // rbp
const unsigned __int16 *v3; // rax
size_t v4; // rax
_BYTE *v5; // rbx
__int64 result; // rax
const unsigned __int16 *v7; // rdx

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

canonical_decompiled_code:
__int64 __fastcall func0(void *dest)
{
__int64 v1; // rbx
const char *v2; // rbp
const unsigned __int16 *v3; // rax
size_t v4; // rax
_BYTE *v5; // rbx
__int64 result; // rax
const unsigned __int16 *v7; // rdx

v1 = *(char *)dest;
if ( (_BYTE)v1 )
{
v2 = (const char *)dest;
v3 = *_ctype_b_loc();
do
{
if ( (v3[v1] & 0x2000) == 0 )
break;
v1 = *++v2;
}
while ( (_BYTE)v1 );
}
else
{
v2 = (const char *)dest;
}
v4 = strlen(v2);
v5 = (char *)dest + v4 + 1;
result = (__int64)memmove(dest, v2, v4 + 1);
if ( dest < v5 )
{
v7 = *_ctype_b_loc();
do
{
result = (char)*v5;
if ( (v7[result] & 0x2000) == 0 )
break;
--v5;
}
while ( dest != v5 );
}
*v5 = 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(void *src)
{
size_t v1; // rax
__int64 v2; // r12
size_t v3; // rbx
void *v4; // rax
char *v5; // rax

v1 = strlen((const char *)src);
v2 = (int)v1;
v3 = v1;
v4 = malloc((int)v1);
v5 = (char *)memcpy(v4, src, v3 + 1);
v5[v2 - 1] = 0;
return v5 + 1;
}

canonical_decompiled_code:
char *__fastcall func1(void *src)
{
size_t v1; // rax
__int64 v2; // r12
size_t v3; // rbx
void *v4; // rax
char *v5; // rax

v1 = strlen((const char *)src);
v2 = (int)v1;
v3 = v1;
v4 = malloc((int)v1);
v5 = (char *)memcpy(v4, src, v3 + 1);
v5[v2 - 1] = 0;
return v5 + 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; // r12
void *v1; // rax

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

canonical_decompiled_code:
_QWORD *func0()
{
_QWORD *v0; // r12
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 = calloc(0x18uLL, 1uLL);
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 = calloc(0x18uLL, 1uLL);
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; } *