backtrack

original_code:
int
backtrack(Prog *prog, char *input, char **subp, int nsubp)
{
 enum { MAX = 1000 };
 Thread ready[MAX];
 int i, nready;
 Inst *pc;
 char *sp;
 Sub *sub;
 sub = newsub(nsubp);
 for(i=0; i<nsubp; i++)
  sub->sub[i] = ((void*)0);
 ready[0] = thread(prog->start, input, sub);
 nready = 1;
 while(nready > 0) {
  --nready;
  pc = ready[nready].pc;
  sp = ready[nready].sp;
  sub = ready[nready].sub;
  ((void) sizeof ((sub->ref > 0) ? 1 : 0), __extension__ ({ if (sub->ref > 0) ; else __assert_fail ("sub->ref > 0", "backtrack.c", 45, __extension__ __PRETTY_FUNCTION__); }));
  for(;;) {
   switch(pc->opcode) {
   case Char:
    if(*sp != pc->c)
     goto Dead;
    pc++;
    sp++;
    continue;
   case Any:
    if(*sp == 0)
     goto Dead;
    pc++;
    sp++;
    continue;
   case Match:
    for(i=0; i<nsubp; i++)
     subp[i] = sub->sub[i];
    decref(sub);
    return 1;
   case Jmp:
    pc = pc->x;
    continue;
   case Split:
    if(nready >= MAX)
     fatal("backtrack overflow");
    ready[nready++] = thread(pc->y, sp, incref(sub));
    pc = pc->x;
    continue;
   case Save:
    sub = update(sub, pc->n, sp);
    pc++;
    continue;
   }
  }
 Dead:
  decref(sub);
 }
 return 0;
}

canonical_original_code:
int backtrack(struct Prog *prog, char *input, char **subp, int nsubp)
{
 enum { MAX = 1000 };
 struct Thread ready[-1];
 int i; int nready;
 struct Inst *pc;
 char *sp;
 struct Sub *sub;
 sub = newsub(nsubp);
 for(i=0; i<nsubp; i++)
  sub->sub[i] = ((void *)0);
 ready[0] = thread(prog->start, input, sub);
 nready = 1;
 while(nready > 0) {
  --nready;
  pc = ready[nready].pc;
  sp = ready[nready].sp;
  sub = ready[nready].sub;
  ((void) sizeof ((sub->ref > 0) ? 1 : 0), __extension__ ({ if (sub->ref > 0) ; else __assert_fail ("sub->ref > 0", "backtrack.c", 45, __extension__ __PRETTY_FUNCTION__); }));
  for(;;) {
   switch(pc->opcode) {
   case Char:
    if(*sp != pc->c)
     goto Dead;
    pc++;
    sp++;
    continue;
   case Any:
    if(*sp == 0)
     goto Dead;
    pc++;
    sp++;
    continue;
   case Match:
    for(i=0; i<nsubp; i++)
     subp[i] = sub->sub[i];
    decref(sub);
    return 1;
   case Jmp:
    pc = pc->x;
    continue;
   case Split:
    if(nready >= MAX)
     fatal("backtrack overflow");
    ready[nready++] = thread(pc->y, sp, incref(sub));
    pc = pc->x;
    continue;
   case Save:
    sub = update(sub, pc->n, sp);
    pc++;
    continue;
   }
  }
 Dead:
  decref(sub);
 }
 return 0;
}

variable_types: struct Thread { struct Inst { int opcode; int c; int n; struct Inst *x; struct Inst *y; int gen; } *pc; char *sp; struct Sub { int ref; int nsub; char *sub[-1]; } *sub; } [-1]

variable_types: nready int

user_defined_types: struct Thread { struct Inst *pc; char *sp; struct Sub *sub; }


rdtsc

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

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

original_code:
static __inline__ unsigned long long rdtsc(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 long rdtsc()
{
  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 long


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // rax
FILE *v4; // rbx
FILE *v5; // rax
FILE *v6; // rbx
int v7; // er12
unsigned __int64 v8; // rbp
int v9; // ebx
unsigned __int64 v10; // rax
FILE *v11; // rax
FILE *v12; // rbx

v3 = fopen("/proc/sysmon_toggle", "w");
if ( v3 )
{
v4 = v3;
fprintf(v3, "1");
fclose(v4);
v5 = fopen("/proc/sysmon_uid", "w");
v6 = v5;
if ( !v5 )
{
puts("sysmon_uid not open");
return 1;
}
fprintf(v5, "396531");
fclose(v6);
puts("getuid, ");
v7 = 100;
do
{
v8 = rdtsc();
v9 = 1024;
do
{
syscall(102LL);
--v9;
}
while ( v9 );
v10 = rdtsc();
printf("%llu, \n", v10 - v8);
--v7;
}
while ( v7 );
putchar(10);
v11 = fopen("/proc/sysmon_toggle", "w");
v12 = v11;
if ( v11 )
{
fprintf(v11, "0");
fclose(v12);
return 0;
}
}
puts("sysmon_toggle not open");
return 1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
FILE *v3; // rax
FILE *v4; // rbx
FILE *v5; // rax
FILE *v6; // rbx
int v7; // er12
unsigned __int64 v8; // rbp
int v9; // ebx
unsigned __int64 v10; // rax
FILE *v11; // rax
FILE *v12; // rbx

v3 = fopen("/proc/sysmon_toggle", "w");
if ( v3 )
{
v4 = v3;
fprintf(v3, "1");
fclose(v4);
v5 = fopen("/proc/sysmon_uid", "w");
v6 = v5;
if ( !v5 )
{
puts("sysmon_uid not open");
return 1;
}
fprintf(v5, "396531");
fclose(v6);
puts("getuid, ");
v7 = 100;
do
{
v8 = func7();
v9 = 1024;
do
{
syscall(102LL);
--v9;
}
while ( v9 );
v10 = func7();
printf("%llu, \n", v10 - v8);
--v7;
}
while ( v7 );
putchar(10);
v11 = fopen("/proc/sysmon_toggle", "w");
v12 = v11;
if ( v11 )
{
fprintf(v11, "0");
fclose(v12);
return 0;
}
}
puts("sysmon_toggle not open");
return 1;
}

original_code:
int main(void)
{
 unsigned int syscall_number = 1024;
 unsigned int epoch = 100;
 unsigned int index_syscall = 0;
 unsigned int index_epoch = 0;
 unsigned long long start, end, total;
 char buf[40];
 char buf_write[20] = "hello\n";
 printf("getuid, \n");
 for(index_epoch = 0; index_epoch < epoch; index_epoch++){
  start = rdtsc();
  for(index_syscall = 0; index_syscall < syscall_number; index_syscall++){
   syscall(102);
  }
  end = rdtsc();
  total = end - start;
  printf("%llu, \n", total);
 }
 printf("\n");
 return 0;
}

canonical_original_code:
int main()
{
 unsigned int syscall_number = 1024;
 unsigned int epoch = 100;
 unsigned int index_syscall = 0;
 unsigned int index_epoch = 0;
 unsigned long long start; unsigned long long end; unsigned long long total;
 char buf[40];
 char buf_write[20] = "hello\n";
 printf("getuid, \n");
 for(index_epoch = 0; index_epoch < epoch; index_epoch++){
  start = rdtsc();
  for(index_syscall = 0; index_syscall < syscall_number; index_syscall++){
   syscall(102);
  }
  end = rdtsc();
  total = end - start;
  printf("%llu, \n", total);
 }
 printf("\n");
 return 0;
}

variable_types:
epoch
unsigned int

variable_types:
start
unsigned long long

variable_types:
end
unsigned long long

variable_types:
total
unsigned long long

variable_types:
buf
char [40]

return_type: int


repo: agrimaldi/pmap

decompiled_code:
double u_wseconds()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return (double)SLODWORD(v1.tv_usec) / 1000000.0 + (double)SLODWORD(v1.tv_sec);
}

canonical_decompiled_code:
double func7()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

gettimeofday(&v1, 0LL);
return (double)SLODWORD(v1.tv_usec) / 1000000.0 + (double)SLODWORD(v1.tv_sec);
}

original_code:
double u_wseconds(void) {
    struct timeval tp;
    gettimeofday(&tp, ((void *)0));
    return (double) tp.tv_sec + (double) tp.tv_usec / 1000000.0;
}

canonical_original_code:
double u_wseconds() {
    struct timeval tp;
    gettimeofday(&tp, ((void *)0));
    return (double) tp.tv_sec + (double) tp.tv_usec / 1000000.0;
}

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

return_type: double


printList

repo: agusk/sorting

decompiled_code:
int __fastcall printList(const char *a1)
{
char *v1; // rbx

printf("%s :", a1);
v1 = (char *)&list;
do
{
printf("%ld ", *(_QWORD *)v1);
v1 += 8;
}
while ( v1 != &_bss_start );
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func19(const char *a1)
{
char *v1; // rbx

printf("%s :", a1);
v1 = (char *)&list;
do
{
printf("%ld ", *(_QWORD *)v1);
v1 += 8;
}
while ( v1 != &_bss_start );
return putchar(10);
}

original_code:
void printList(char* title) {
  int i;
  printf("%s :",title);
  for(i=0;i<10;i++)
    printf("%ld ", list[i]);
  printf("\n");
}

canonical_original_code:
void printList(char *title) {
  int i;
  printf("%s :",title);
  for(i=0;i<10;i++)
    printf("%ld ", list[i]);
  printf("\n");
}

variable_types:
title
char *

variable_types: i int

return_type: void


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
printList("data");
bubbleSort(&list, 10LL);
printList("bubblesort");
resetList();
insertionSort((__int64)&list, 10LL);
printList("insertionsort");
resetList();
selectionSort((__int64)&list, 10LL);
printList("selectionsort");
resetList();
heapSort(&list, 10LL);
printList("heapsort");
resetList();
quickSort((__int64)&list, 0LL, 9LL);
printList("quicksort");
resetList();
mergeSort((__int64)&list, 0LL, 9LL, 10LL);
printList("mergesort");
resetList();
radixSort(&list, 10LL);
printList("radixsort");
resetList();
gnomeSort((__int64)&list, 10LL);
printList("gnomesort");
resetList();
shellSort((__int64)&list, 10LL);
printList("shellsort");
resetList();
cycleSort((__int64)&list, 10LL);
printList("cyclesort");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func19("data");
func7(&list, 10LL);
func19("bubblesort");
func18();
func8((__int64)&list, 10LL);
func19("insertionsort");
func18();
func9((__int64)&list, 10LL);
func19("selectionsort");
func18();
func10(&list, 10LL);
func19("heapsort");
func18();
func11((__int64)&list, 0LL, 9LL);
func19("quicksort");
func18();
func13((__int64)&list, 0LL, 9LL, 10LL);
func19("mergesort");
func18();
func14(&list, 10LL);
func19("radixsort");
func18();
func15((__int64)&list, 10LL);
func19("gnomesort");
func18();
func16((__int64)&list, 10LL);
func19("shellsort");
func18();
func17((__int64)&list, 10LL);
func19("cyclesort");
return 0;
}

original_code:
int main() {
  printList("data");
  bubbleSort(list,10);
  printList("bubblesort");
  resetList();
  insertionSort(list,10);
  printList("insertionsort");
  resetList();
  selectionSort(list,10);
  printList("selectionsort");
  resetList();
  heapSort(list,10);
  printList("heapsort");
  resetList();
  quickSort(list,0,9);
  printList("quicksort");
  resetList();
  mergeSort(list,0,9,10);
  printList("mergesort");
  resetList();
  radixSort(list,10);
  printList("radixsort");
  resetList();
  gnomeSort(list,10);
  printList("gnomesort");
  resetList();
  shellSort(list,10);
  printList("shellsort");
  resetList();
  cycleSort(list,10);
  printList("cyclesort");
}


repo: agustingianni/StreamingProject

decompiled_code:
__int64 socket_error()
{
return (unsigned int)*_errno_location();
}

canonical_decompiled_code:
__int64 func1()
{
return (unsigned int)*_errno_location();
}

original_code:
int
socket_error(void)
{
 return (*__errno_location ());
}

canonical_original_code:
int socket_error()
{
 return (*__errno_location ());
}


decompiled_code:
_BOOL8 __fastcall socket_recoverable(int a1)
{
_BOOL8 result; // rax

if ( a1 > 11 )
{
result = 1LL;
if ( a1 != 85 )
result = a1 == 115;
}
else
{
result = 0LL;
if ( a1 >= 0 )
result = ((1LL << a1) & 0x811) != 0;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(int a1)
{
_BOOL8 result; // rax

if ( a1 > 11 )
{
result = 1LL;
if ( a1 != 85 )
result = a1 == 115;
}
else
{
result = 0LL;
if ( a1 >= 0 )
result = ((1LL << a1) & 0x811) != 0;
}
return result;
}

original_code:
int
socket_recoverable(int error)
{
 switch (error)
 {
  case 0:
  case 11:
  case 4:
  case 115:
  case 85:
   return 1;
  default:
   return 0;
 }
}

variable_types: error int


decompiled_code:
__int64 __fastcall socket_stalled(int a1)
{
if ( a1 == 85 )
return 1LL;
if ( a1 > 85 )
return (unsigned int)(a1 - 114) <= 1;
return a1 == 11;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
if ( a1 == 85 )
return 1LL;
if ( a1 > 85 )
return (unsigned int)(a1 - 114) <= 1;
return a1 == 11;
}

original_code:
int
socket_stalled(int error)
{
 switch (error)
 {
  case 11:
  case 115:
  case 114:
  case 85:
   return 1;
  default:
   return 0;
 }
}


decompiled_code:
__int64 __fastcall socket_active(int a1)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

recv(a1, buf, 1uLL, 2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

recv(a1, buf, 1uLL, 2);
return 0LL;
}

original_code:
int
socket_active(int sock)
{
 char c;
 int l;
 l = recv(sock, &c, 1, MSG_PEEK);
 if (l == 0)
  return 0;
 return 0;
}

variable_types: sock int

variable_types: c char

variable_types: l int


decompiled_code:
int __fastcall socket_set_blocking(int a1)
{
return fcntl(a1, 4, 0LL);
}

canonical_decompiled_code:
int __fastcall func5(int a1)
{
return fcntl(a1, 4, 0LL);
}

original_code:
int
socket_set_blocking(int sock)
{
 return fcntl(sock, 4, 0);
}


decompiled_code:
int __fastcall socket_set_nonblocking(int a1)
{
return fcntl(a1, 4, 2048LL);
}

canonical_decompiled_code:
int __fastcall func6(int a1)
{
return fcntl(a1, 4, 2048LL);
}

original_code:
int
socket_set_nonblocking(int sock)
{
 return fcntl(sock, 4, 04000);
}


decompiled_code:
int __fastcall socket_set_no_linger(int a1)
{
int optval[4]; // [rsp+8h] [rbp-10h] BYREF

optval[0] = 0;
optval[1] = 0;
return setsockopt(a1, 1, 13, optval, 8u);
}

canonical_decompiled_code:
int __fastcall func7(int a1)
{
int optval[4]; // [rsp+8h] [rbp-10h] BYREF

optval[0] = 0;
optval[1] = 0;
return setsockopt(a1, 1, 13, optval, 8u);
}

original_code:
int
socket_set_no_linger(int sock)
{
 struct linger lin = { 0, 0 };
 return setsockopt(sock, 1, 13, (void *) &lin,
   sizeof(struct linger));
}

variable_types: struct linger { int l_onoff; int l_linger; }


decompiled_code:
int __fastcall socket_set_nodelay(int a1)
{
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

optval[0] = 1;
return setsockopt(a1, 6, 1, optval, 4u);
}

canonical_decompiled_code:
int __fastcall func8(int a1)
{
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

optval[0] = 1;
return setsockopt(a1, 6, 1, optval, 4u);
}

original_code:
int
socket_set_nodelay(int sock)
{
 int nodelay = 1;
 return setsockopt(sock, IPPROTO_TCP, 1, (void *) &nodelay,
   sizeof(int));
}

variable_types: nodelay int


decompiled_code:
int __fastcall socket_set_keepalive(int a1)
{
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

optval[0] = 1;
return setsockopt(a1, 1, 9, optval, 4u);
}

canonical_decompiled_code:
int __fastcall func9(int a1)
{
int optval[3]; // [rsp+Ch] [rbp-Ch] BYREF

optval[0] = 1;
return setsockopt(a1, 1, 9, optval, 4u);
}

original_code:
int
socket_set_keepalive(int sock)
{
 int keepalive = 1;
 return setsockopt(sock, 1, 9, (void *) &keepalive,
   sizeof(int));
}

variable_types: keepalive int


decompiled_code:
int __fastcall socket_close(int a1)
{
return close(a1);
}

canonical_decompiled_code:
int __fastcall func10(int a1)
{
return close(a1);
}

original_code:
int
socket_close(int sock)
{
 return close(sock);
}


decompiled_code:
ssize_t __fastcall socket_write(int a1, const void *a2, size_t a3)
{
return send(a1, a2, a3, 0);
}

canonical_decompiled_code:
ssize_t __fastcall func11(int a1, const void *a2, size_t a3)
{
return send(a1, a2, a3, 0);
}

original_code:
int
socket_write(int sock, const void *buff, size_t len)
{
 return send(sock, buff, len, 0);
}

canonical_original_code:
int socket_write(int sock, void *buff, unsigned long len)
{
 return send(sock, buff, len, 0);
}

variable_types:
buff
void *

variable_types:
len
unsigned long


decompiled_code:
__int64 __fastcall socket_write_all(int a1, char *a2, size_t a3)
{
unsigned int v3; // er13
size_t v5; // rbx
__int64 v6; // rax

v3 = a3;
if ( !a3 )
return v3;
v5 = a3;
while ( 1 )
{
v6 = (int)socket_write(a1, a2, v5);
if ( (int)v6 <= 0 )
break;
LABEL_3:
a2 += v6;
v5 -= v6;
if ( !v5 )
return v3;
}
if ( (int)v6 >= 0LL )
return 0xFFFFFFFFLL;
if ( *_errno_location() == 4 )
{
v6 = 0LL;
goto LABEL_3;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1, char *a2, size_t a3)
{
unsigned int v3; // er13
size_t v5; // rbx
__int64 v6; // rax

v3 = a3;
if ( !a3 )
return v3;
v5 = a3;
while ( 1 )
{
v6 = (int)func11(a1, a2, v5);
if ( (int)v6 <= 0 )
break;
LABEL_3:
a2 += v6;
v5 -= v6;
if ( !v5 )
return v3;
}
if ( (int)v6 >= 0LL )
return 0xFFFFFFFFLL;
if ( *_errno_location() == 4 )
{
v6 = 0LL;
goto LABEL_3;
}
return 0xFFFFFFFFLL;
}

original_code:
int
socket_write_all(int sock, const void *buff, size_t len)
{
 size_t nleft;
 ssize_t nwritten;
 const char *ptr;
 ptr = buff;
 nleft = len;
 while (nleft > 0)
 {
  if ( (nwritten = socket_write(sock, ptr, nleft)) <= 0)
  {
   if (nwritten < 0 && (*__errno_location ()) == 4)
    nwritten = 0;
   else
    return (-1);
  }
  nleft -= nwritten;
  ptr += nwritten;
 }
 return len;
}

canonical_original_code:
int socket_write_all(int sock, void *buff, unsigned long len)
{
 unsigned long nleft;
 long nwritten;
 char *ptr;
 ptr = buff;
 nleft = len;
 while (nleft > 0)
 {
  if ( (nwritten = socket_write(sock, ptr, nleft)) <= 0)
  {
   if (nwritten < 0 && (*__errno_location ()) == 4)
    nwritten = 0;
   else
    return (-1);
  }
  nleft -= nwritten;
  ptr += nwritten;
 }
 return len;
}

variable_types:
nleft
unsigned long

variable_types: nwritten long

variable_types:
ptr
char *


decompiled_code:
ssize_t __fastcall socket_read(int a1, void *a2, size_t a3)
{
ssize_t result; // rax

result = 0LL;
if ( a2 )
result = recv(a1, a2, a3, 0);
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func13(int a1, void *a2, size_t a3)
{
ssize_t result; // rax

result = 0LL;
if ( a2 )
result = recv(a1, a2, a3, 0);
return result;
}

original_code:
int
socket_read(int sock, char *buff, size_t len)
{
 if (!buff)
  return 0;
 return recv(sock, buff, len, 0);
}

canonical_original_code:
int socket_read(int sock, char *buff, unsigned long len)
{
 if (!buff)
  return 0;
 return recv(sock, buff, len, 0);
}

variable_types:
buff
char *


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

if ( !a2 )
return 0LL;
LODWORD(result) = recv(a1, a2, a3 - 1, 0);
if ( (_DWORD)result != -1 )
*((_BYTE *)a2 + (int)result) = 0;
return (unsigned int)result;
}

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

if ( !a2 )
return 0LL;
LODWORD(result) = recv(a1, a2, a3 - 1, 0);
if ( (_DWORD)result != -1 )
*((_BYTE *)a2 + (int)result) = 0;
return (unsigned int)result;
}

original_code:
int
socket_read_string(int sock, char *string_buffer, size_t len)
{
 if (!string_buffer)
  return 0;
 int ret = recv(sock, string_buffer, len-1, 0);
 if(ret != -1)
  string_buffer[ret] = '\0';
 return ret;
}

canonical_original_code:
int socket_read_string(int sock, char *string_buffer, unsigned long len)
{
 if (!string_buffer)
  return 0;
 int ret = recv(sock, string_buffer, len-1, 0);
 if(ret != -1)
  string_buffer[ret] = '\0';
 return ret;
}

variable_types: ret int


decompiled_code:
__int64 __fastcall socket_tcp_server(int a1)
{
int v1; // eax
unsigned int v2; // ebx
int optval; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

if ( a1 < 0 )
return (unsigned int)-1;
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = _bswap_16(a1);
v1 = socket(2, 1, 6);
v2 = v1;
if ( v1 != -1 )
{
optval = 1;
setsockopt(v1, 1, 2, &optval, 4u);
if ( bind(v2, &addr, 0x10u) == -1 )
v2 = -1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func15(int a1)
{
int v1; // eax
unsigned int v2; // ebx
int optval; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

if ( a1 < 0 )
return (unsigned int)-1;
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = func0(a1);
v1 = socket(2, 1, 6);
v2 = v1;
if ( v1 != -1 )
{
optval = 1;
setsockopt(v1, 1, 2, &optval, 4u);
if ( bind(v2, &addr, 0x10u) == -1 )
v2 = -1;
}
return v2;
}

original_code:
int
socket_tcp_server(int port)
{
 struct sockaddr_in sa;
 int opt;
 int sock;
 if (port < 0)
  return -1;
 memset(&sa, 0, sizeof(sa));
 sa.sin_addr.s_addr = ((in_addr_t) 0x00000000);
 sa.sin_family = 2;
 sa.sin_port = htons(port);
 if ((sock = socket(2, SOCK_STREAM,
   IPPROTO_TCP)) == -1)
  return -1;
 opt = 1;
 setsockopt(sock, 1, 2,
   (const void *) &opt, sizeof(int));
 if(bind(sock, (struct sockaddr *) &sa,
   sizeof(struct sockaddr_in)) == -1)
  return -1;
 return sock;
}

canonical_original_code:
int socket_tcp_server(int port)
{
 struct sockaddr_in sa;
 int opt;
 int sock;
 if (port < 0)
  return -1;
 memset(&sa, 0, sizeof(sa));
 sa.sin_addr.s_addr = ((unsigned int) 0x00000000);
 sa.sin_family = 2;
 sa.sin_port = htons(port);
 if ((sock = socket(2, SOCK_STREAM,
   IPPROTO_TCP)) == -1)
  return -1;
 opt = 1;
 setsockopt(sock, 1, 2,
   (void *) &opt, sizeof(int));
 if(bind(sock, (struct sockaddr *) &sa,
   sizeof(struct sockaddr_in)) == -1)
  return -1;
 return sock;
}

variable_types: port 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: opt int

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

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


decompiled_code:
__int64 __fastcall socket_udp_server(int a1)
{
int v1; // eax
unsigned int v2; // ebx
int optval; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

if ( a1 < 0 )
return (unsigned int)-1;
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = _bswap_16(a1);
v1 = socket(2, 2, 17);
v2 = v1;
if ( v1 != -1 )
{
optval = 1;
setsockopt(v1, 1, 2, &optval, 4u);
if ( bind(v2, &addr, 0x10u) == -1 )
v2 = -1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func16(int a1)
{
int v1; // eax
unsigned int v2; // ebx
int optval; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

if ( a1 < 0 )
return (unsigned int)-1;
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = func0(a1);
v1 = socket(2, 2, 17);
v2 = v1;
if ( v1 != -1 )
{
optval = 1;
setsockopt(v1, 1, 2, &optval, 4u);
if ( bind(v2, &addr, 0x10u) == -1 )
v2 = -1;
}
return v2;
}

original_code:
int
socket_udp_server(int port)
{
 struct sockaddr_in sa;
 int opt;
 int sock;
 if (port < 0)
  return -1;
 memset(&sa, 0, sizeof(sa));
 sa.sin_addr.s_addr = ((in_addr_t) 0x00000000);
 sa.sin_family = 2;
 sa.sin_port = htons(port);
 if ((sock = socket(2, SOCK_DGRAM,
   IPPROTO_UDP)) == -1)
  return -1;
 opt = 1;
 setsockopt(sock, 1, 2,
   (const void *) &opt, sizeof(int));
 if(bind(sock, (struct sockaddr *) &sa,
   sizeof(struct sockaddr_in)) == -1)
  return -1;
 return sock;
}

canonical_original_code:
int socket_udp_server(int port)
{
 struct sockaddr_in sa;
 int opt;
 int sock;
 if (port < 0)
  return -1;
 memset(&sa, 0, sizeof(sa));
 sa.sin_addr.s_addr = ((unsigned int) 0x00000000);
 sa.sin_family = 2;
 sa.sin_port = htons(port);
 if ((sock = socket(2, SOCK_DGRAM,
   IPPROTO_UDP)) == -1)
  return -1;
 opt = 1;
 setsockopt(sock, 1, 2,
   (void *) &opt, sizeof(int));
 if(bind(sock, (struct sockaddr *) &sa,
   sizeof(struct sockaddr_in)) == -1)
  return -1;
 return sock;
}


decompiled_code:
int __fastcall socket_listen(int a1)
{
return listen(a1, 128);
}

canonical_decompiled_code:
int __fastcall func17(int a1)
{
return listen(a1, 128);
}

original_code:
int
socket_listen(int serversock)
{
 return listen(serversock, 128);
}

variable_types: serversock int


decompiled_code:
__int64 __fastcall socket_accept(int a1)
{
unsigned int v1; // ebx
socklen_t addr_len; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

addr_len = 16;
v1 = accept(a1, &addr, &addr_len);
socket_set_no_linger(v1);
socket_set_keepalive(v1);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func18(int a1)
{
unsigned int v1; // ebx
socklen_t addr_len; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

addr_len = 16;
v1 = accept(a1, &addr, &addr_len);
func7(v1);
func9(v1);
return v1;
}

original_code:
int
socket_accept(int serversock)
{
 int ret;
 struct sockaddr sa;
 socklen_t slen = sizeof(struct sockaddr);
 ret = accept(serversock, (struct sockaddr *) &sa, &slen);
 socket_set_no_linger(ret);
 socket_set_keepalive(ret);
 return ret;
}

canonical_original_code:
int socket_accept(int serversock)
{
 int ret;
 struct sockaddr sa;
 unsigned int slen = sizeof(struct sockaddr);
 ret = accept(serversock, (struct sockaddr *) &sa, &slen);
 socket_set_no_linger(ret);
 socket_set_keepalive(ret);
 return ret;
}

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

variable_types:
slen
unsigned int


original_code:
static void
main_initialize(void)
{
 server.name = "KENNY Streaming Server";
 server.status = 2;
 server.clients = 0;
 server.bitrate = 128;
 server.buffer_size = 1024;
 server.timeout = -1;
 config.max_clients = 1024 - 24;
 config.sockets_number = 1;
 config.streaming_file = "test.mp3";
 config.mode = 1;
 config.port = 1337;
 config.interface = "lo";
}

canonical_original_code:
static void main_initialize()
{
 server.name = "KENNY Streaming Server";
 server.status = 2;
 server.clients = 0;
 server.bitrate = 128;
 server.buffer_size = 1024;
 server.timeout = -1;
 config.max_clients = 1024 - 24;
 config.sockets_number = 1;
 config.streaming_file = "test.mp3";
 config.mode = 1;
 config.port = 1337;
 config.interface = "lo";
}


decompiled_code:
__int64 __fastcall parse_arguments(int argc, char **argv)
{
int v2; // eax
unsigned __int16 v3; // ax
char *v4; // rax

while ( 1 )
{
v2 = getopt(argc, argv, "hf:dp:");
if ( v2 == -1 )
break;
if ( v2 == 102 )
{
v4 = strdup(optarg);
*((_QWORD *)&config + 1) = v4;
fprintf(stdout, "[*] Archivo a servir: %s\n", v4);
}
else if ( v2 > 102 )
{
if ( v2 == 104 )
show_help((const char **)argv);
if ( v2 == 112 )
{
v3 = atoi(optarg);
*((_WORD *)&config + 8) = v3;
fprintf(stdout, "[*] Escuchando en el puerto %d\n", v3);
}
}
else
{
if ( v2 == 63 )
{
fwrite("[!] Opcion invalida\n", 1uLL, 0x14uLL, stderr);
return 0xFFFFFFFFLL;
}
if ( v2 == 100 )
{
*(&config + 8) = 0;
fwrite("[*] Corriendo en modo daemon\n", 1uLL, 0x1DuLL, stdout);
}
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int argc, char **argv)
{
int v2; // eax
unsigned __int16 v3; // ax
char *v4; // rax

while ( 1 )
{
v2 = getopt(argc, argv, "hf:dp:");
if ( v2 == -1 )
break;
if ( v2 == 102 )
{
v4 = strdup(optarg);
*((_QWORD *)&config + 1) = v4;
fprintf(stdout, "[*] Archivo a servir: %s\n", v4);
}
else if ( v2 > 102 )
{
if ( v2 == 104 )
func1((const char **)argv);
if ( v2 == 112 )
{
v3 = atoi(optarg);
*((_WORD *)&config + 8) = v3;
fprintf(stdout, "[*] Escuchando en el puerto %d\n", v3);
}
}
else
{
if ( v2 == 63 )
{
fwrite("[!] Opcion invalida\n", 1uLL, 0x14uLL, stderr);
return 0xFFFFFFFFLL;
}
if ( v2 == 100 )
{
*(&config + 8) = 0;
fwrite("[*] Corriendo en modo daemon\n", 1uLL, 0x1DuLL, stdout);
}
}
}
return 0LL;
}

original_code:
static int
parse_arguments(int argc, char **argv)
{
 int c;
 while((c = getopt(argc, argv, "hf:dp:")) != -1)
 {
  switch(c)
  {
   case 'h':
    show_help(argv);
    break;
   case 'f':
    config.streaming_file = strdup(optarg);
    fprintf(stdout, "[*] Archivo a servir: %s\n", config.streaming_file);
    break;
   case 'd':
    config.mode = 0;
    fprintf(stdout, "[*] Corriendo en modo daemon\n");
    break;
   case 'p':
    config.port = (unsigned short) atoi(optarg);
    fprintf(stdout, "[*] Escuchando en el puerto %d\n", config.port);
    break;
   case '?':
    fprintf(stderr, "[!] Opcion invalida\n");
    return -1;
   default:
    break;
  }
 }
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: c int


decompiled_code:
__int64 main_daemonize()
{
__pid_t v0; // eax
unsigned int v1; // er12
int v2; // ebx
int v3; // ebp

v0 = fork();
v1 = v0;
if ( v0 == -1 )
{
perror("main_daemonize():");
}
else
{
v2 = v0;
if ( v0 )
exit(0);
if ( setsid() == -1 )
{
perror("main_daemonize():");
v1 = -1;
}
else
{
chdir("/");
v3 = sysconf(4);
if ( v3 > 0 )
{
do
close(v2++);
while ( v3 != v2 );
}
umask(0x3Fu);
open("/dev/null", 0);
open("/dev/null", 2);
open("/dev/null", 2);
}
}
return v1;
}

canonical_decompiled_code:
__int64 func3()
{
__pid_t v0; // eax
unsigned int v1; // er12
int v2; // ebx
int v3; // ebp

v0 = fork();
v1 = v0;
if ( v0 == -1 )
{
perror("main_daemonize():");
}
else
{
v2 = v0;
if ( v0 )
exit(0);
if ( setsid() == -1 )
{
perror("main_daemonize():");
v1 = -1;
}
else
{
chdir("/");
v3 = sysconf(4);
if ( v3 > 0 )
{
do
close(v2++);
while ( v3 != v2 );
}
umask(0x3Fu);
open("/dev/null", 0);
open("/dev/null", 2);
open("/dev/null", 2);
}
}
return v1;
}

original_code:
int
main_daemonize(void)
{
 pid_t pid;
 int i;
 pid = fork();
 if(pid == -1)
 {
  perror("main_daemonize():");
  return -1;
 }
 else if(pid)
 {
  _exit(0);
 }
 if(setsid() == -1)
 {
  perror("main_daemonize():");
  return -1;
 }
 chdir("/");
 int maxfd = sysconf(_SC_OPEN_MAX);
 for (i = 0; i < maxfd; i++)
   close(i);
 umask(077);
 open("/dev/null", 00);
 open("/dev/null", 02);
 open("/dev/null", 02);
 return 0;
}

canonical_original_code:
int main_daemonize()
{
 int pid;
 int i;
 pid = fork();
 if(pid == -1)
 {
  perror("main_daemonize():");
  return -1;
 }
 else if(pid)
 {
  _exit(0);
 }
 if(setsid() == -1)
 {
  perror("main_daemonize():");
  return -1;
 }
 chdir("/");
 int maxfd = sysconf(_SC_OPEN_MAX);
 for (i = 0; i < maxfd; i++)
   close(i);
 umask(077);
 open("/dev/null", 00);
 open("/dev/null", 02);
 open("/dev/null", 02);
 return 0;
}

variable_types: pid int

variable_types: i int

variable_types: maxfd int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx

main_initialize();
if ( (unsigned int)parse_arguments(argc, (char **)argv) == -1 )
{
v3 = -1;
fwrite("[!] Error parseando argumentos.\n", 1uLL, 0x20uLL, stderr);
}
else if ( !*(&config + 8) && (v3 = main_daemonize(), v3 == -1) )
{
fwrite("[!] No se pudo inicializar el servidor como demonio\n", 1uLL, 0x34uLL, stderr);
}
else
{
v3 = signal_initialize();
if ( v3 == -1 )
{
fwrite("[!] Error seteando manejadores de seniales\n", 1uLL, 0x2BuLL, stderr);
}
else
{
fwrite("[*] Inicializando el servidor\n", 1uLL, 0x1EuLL, stdout);
v3 = stream_create((unsigned int)((_DWORD)server + 24), *((_QWORD *)&config + 1));
if ( v3 == -1 )
{
fwrite("[!] Error inicializando el stream de bytes\n", 1uLL, 0x2BuLL, stderr);
}
else
{
v3 = server_initialize();
if ( v3 == -1 )
{
fwrite("[!] No se pudo inicializar el servidor.\n", 1uLL, 0x28uLL, stderr);
stream_destroy(server[3]);
}
else
{
v3 = client_initialize((unsigned int)config);
if ( v3 == -1 )
{
fwrite("[!] No se pudo inicializar el subsistema de clientes.\n", 1uLL, 0x36uLL, stderr);
stream_destroy(server[3]);
server_shutdown();
}
else
{
fwrite("[*] Entrando en el loop de eventos\n", 1uLL, 0x23uLL, stdout);
v3 = server_enter_event_loop();
if ( v3 == -1 )
{
fwrite("[!] No se pudo entrar en el loop de eventos.\n", 1uLL, 0x2DuLL, stderr);
stream_destroy(server[3]);
server_shutdown();
}
else
{
fwrite("[*] Cerrando el servidor\n", 1uLL, 0x19uLL, stdout);
stream_destroy(server[3]);
v3 = server_shutdown();
if ( v3 == -1 )
fwrite("[!] Cierre inconcluso del servidor, verifique los logs.\n", 1uLL, 0x38uLL, stderr);
else
v3 = 0;
}
}
}
}
}
}
return v3;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx

func0();
if ( (unsigned int)func2(argc, (char **)argv) == -1 )
{
v3 = -1;
fwrite("[!] Error parseando argumentos.\n", 1uLL, 0x20uLL, stderr);
}
else if ( !*(&config + 8) && (v3 = func3(), v3 == -1) )
{
fwrite("[!] No se pudo inicializar el servidor como demonio\n", 1uLL, 0x34uLL, stderr);
}
else
{
v3 = signal_initialize();
if ( v3 == -1 )
{
fwrite("[!] Error seteando manejadores de seniales\n", 1uLL, 0x2BuLL, stderr);
}
else
{
fwrite("[*] Inicializando el servidor\n", 1uLL, 0x1EuLL, stdout);
v3 = stream_create((unsigned int)((_DWORD)server + 24), *((_QWORD *)&config + 1));
if ( v3 == -1 )
{
fwrite("[!] Error inicializando el stream de bytes\n", 1uLL, 0x2BuLL, stderr);
}
else
{
v3 = server_initialize();
if ( v3 == -1 )
{
fwrite("[!] No se pudo inicializar el servidor.\n", 1uLL, 0x28uLL, stderr);
stream_destroy(server[3]);
}
else
{
v3 = client_initialize((unsigned int)config);
if ( v3 == -1 )
{
fwrite("[!] No se pudo inicializar el subsistema de clientes.\n", 1uLL, 0x36uLL, stderr);
stream_destroy(server[3]);
server_shutdown();
}
else
{
fwrite("[*] Entrando en el loop de eventos\n", 1uLL, 0x23uLL, stdout);
v3 = server_enter_event_loop();
if ( v3 == -1 )
{
fwrite("[!] No se pudo entrar en el loop de eventos.\n", 1uLL, 0x2DuLL, stderr);
stream_destroy(server[3]);
server_shutdown();
}
else
{
fwrite("[*] Cerrando el servidor\n", 1uLL, 0x19uLL, stdout);
stream_destroy(server[3]);
v3 = server_shutdown();
if ( v3 == -1 )
fwrite("[!] Cierre inconcluso del servidor, verifique los logs.\n", 1uLL, 0x38uLL, stderr);
else
v3 = 0;
}
}
}
}
}
}
return v3;
}

original_code:
int
main(int argc, char **argv)
{
 main_initialize();
 if(parse_arguments(argc, argv) == -1)
 {
  fprintf(stderr, "[!] Error parseando argumentos.\n");
  return -1;
 }
 if(config.mode == 0)
 {
  if(main_daemonize() == -1)
  {
   fprintf(stderr, "[!] No se pudo inicializar el servidor como demonio\n");
   return -1;
  }
 }
 if(signal_initialize() == -1)
 {
  fprintf(stderr, "[!] Error seteando manejadores de seniales\n");
  return -1;
 }
 fprintf(stdout, "[*] Inicializando el servidor\n");
 if(stream_create(&server.stream, config.streaming_file) == -1)
 {
  fprintf(stderr, "[!] Error inicializando el stream de bytes\n");
  return -1;
 }
 if(server_initialize() == -1)
 {
  fprintf(stderr, "[!] No se pudo inicializar el servidor.\n");
  stream_destroy(server.stream);
  return -1;
 }
 if(client_initialize(config.max_clients) == -1)
 {
  fprintf(stderr, "[!] No se pudo inicializar el subsistema de clientes.\n");
  stream_destroy(server.stream);
  server_shutdown();
  return -1;
 }
 fprintf(stdout, "[*] Entrando en el loop de eventos\n");
 if(server_enter_event_loop() == -1)
 {
  fprintf(stderr, "[!] No se pudo entrar en el loop de eventos.\n");
  stream_destroy(server.stream);
  server_shutdown();
  return -1;
 }
 fprintf(stdout, "[*] Cerrando el servidor\n");
 stream_destroy(server.stream);
 if(server_shutdown() == -1)
 {
  fprintf(stderr, "[!] Cierre inconcluso del servidor, verifique los logs.\n");
  return -1;
 }
 return 0;
}


decompiled_code:
__int64 signal_handler()
{
return server_leave_event_loop();
}

canonical_decompiled_code:
__int64 func0()
{
return server_leave_event_loop();
}

original_code:
void
signal_handler(__attribute__ ((__unused__)) int sig)
{
 server_leave_event_loop();
}

canonical_original_code:
void signal_handler(int sig)
{
 server_leave_event_loop();
}

variable_types: sig int


decompiled_code:
__int64 signal_initialize()
{
void (*v0)(int); // rax
unsigned int v1; // ebx
struct sigaction v3; // [rsp+0h] [rbp-A8h] BYREF

sigemptyset(&v3.sa_mask);
v3.sa_flags = 0;
v0 = (void (*)(int))signal_handler;
if ( config[8] != 1 )
v0 = (void (*)(int))(signal_handler + 1);
v3.sa_handler = v0;
if ( sigaction(2, &v3, 0LL) == -1 )
{
v1 = -1;
perror("signal_initialize():");
}
else
{
v3.sa_handler = (__sighandler_t)signal_handler;
v1 = sigaction(15, &v3, 0LL);
if ( v1 == -1
|| (v3.sa_handler = (__sighandler_t)(signal_handler + 1), v1 = sigaction(1, &v3, 0LL), v1 == -1)
|| (v1 = sigaction(3, &v3, 0LL), v1 == -1) )
{
perror("signal_initialize():");
}
else
{
v1 = 0;
}
}
return v1;
}

canonical_decompiled_code:
__int64 func1()
{
void (*v0)(int); // rax
unsigned int v1; // ebx
struct sigaction v3; // [rsp+0h] [rbp-A8h] BYREF

sigemptyset(&v3.sa_mask);
v3.sa_flags = 0;
v0 = (void (*)(int))signal_handler;
if ( config[8] != 1 )
v0 = (void (*)(int))(signal_handler + 1);
v3.sa_handler = v0;
if ( sigaction(2, &v3, 0LL) == -1 )
{
v1 = -1;
perror("signal_initialize():");
}
else
{
v3.sa_handler = (__sighandler_t)signal_handler;
v1 = sigaction(15, &v3, 0LL);
if ( v1 == -1
|| (v3.sa_handler = (__sighandler_t)(signal_handler + 1), v1 = sigaction(1, &v3, 0LL), v1 == -1)
|| (v1 = sigaction(3, &v3, 0LL), v1 == -1) )
{
perror("signal_initialize():");
}
else
{
v1 = 0;
}
}
return v1;
}

original_code:
int
signal_initialize(void)
{
 struct sigaction action;
 sigemptyset (&action.sa_mask);
 action.sa_flags = 0;
 action.__sigaction_handler.sa_handler = (config.mode == 1) ? &signal_handler : ((__sighandler_t) 1);
 if(sigaction (2, &action, ((void *)0)) == -1)
 {
  perror("signal_initialize():");
  return -1;
 }
 action.__sigaction_handler.sa_handler = &signal_handler;
 if(sigaction (15, &action, ((void *)0)) == -1)
 {
  perror("signal_initialize():");
  return -1;
 }
 action.__sigaction_handler.sa_handler = ((__sighandler_t) 1);
 if(sigaction (1, &action, ((void *)0)) == -1)
 {
  perror("signal_initialize():");
  return -1;
 }
 if(sigaction (3, &action, ((void *)0)) == -1)
 {
  perror("signal_initialize():");
  return -1;
 }
 return 0;
}

canonical_original_code:
int signal_initialize()
{
 struct sigaction action;
 sigemptyset (&action.sa_mask);
 action.sa_flags = 0;
 action.__sigaction_handler.sa_handler = (config.mode == 1) ? &signal_handler : ((void (*)(int)) 1);
 if(sigaction (2, &action, ((void *)0)) == -1)
 {
  perror("signal_initialize():");
  return -1;
 }
 action.__sigaction_handler.sa_handler = &signal_handler;
 if(sigaction (15, &action, ((void *)0)) == -1)
 {
  perror("signal_initialize():");
  return -1;
 }
 action.__sigaction_handler.sa_handler = ((void (*)(int)) 1);
 if(sigaction (1, &action, ((void *)0)) == -1)
 {
  perror("signal_initialize():");
  return -1;
 }
 if(sigaction (3, &action, ((void *)0)) == -1)
 {
  perror("signal_initialize():");
  return -1;
 }
 return 0;
}

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

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

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


decompiled_code:
_BOOL8 server_running()
{
return server[2] == 1;
}

canonical_decompiled_code:
_BOOL8 func0()
{
return server[2] == 1;
}

original_code:
static int
server_running(void)
{
 return server.status == 1;
}

canonical_original_code:
static int server_running()
{
 return server.status == 1;
}


decompiled_code:
__int64 __fastcall connection_accept_handler(__int64 a1, unsigned int *a2)
{
unsigned int v2; // ebp
unsigned int v3; // ebx
__int64 v5[4]; // [rsp+8h] [rbp-20h] BYREF

v2 = socket_accept(*a2);
if ( v2 == -1 )
{
perror("connection_accept_handler():");
v3 = -1;
}
else if ( server[3] == config )
{
fwrite("connection_accept_handler(): Guarda! Se llego al limite de clientes\n", 1uLL, 0x44uLL, stdout);
socket_close(v2);
v3 = 0;
}
else
{
v3 = client_create(v5, v2);
if ( v3 == -1 )
{
fwrite("connection_accept_handler(): Error creando cliente\n", 1uLL, 0x33uLL, stderr);
socket_close(v2);
}
else
{
v3 = event_add(a1, v2, connection_client_handler, 3LL);
if ( v3 == -1 )
{
fwrite("connection_accept_handler(): Error seteando el event handler\n", 1uLL, 0x3DuLL, stderr);
client_destroy(v5[0]);
socket_close(v2);
--server[3];
}
else
{
++server[3];
v3 = 0;
}
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned int *a2)
{
unsigned int v2; // ebp
unsigned int v3; // ebx
__int64 v5[4]; // [rsp+8h] [rbp-20h] BYREF

v2 = socket_accept(*a2);
if ( v2 == -1 )
{
perror("connection_accept_handler():");
v3 = -1;
}
else if ( server[3] == config )
{
fwrite("connection_accept_handler(): Guarda! Se llego al limite de clientes\n", 1uLL, 0x44uLL, stdout);
socket_close(v2);
v3 = 0;
}
else
{
v3 = client_create(v5, v2);
if ( v3 == -1 )
{
fwrite("connection_accept_handler(): Error creando cliente\n", 1uLL, 0x33uLL, stderr);
socket_close(v2);
}
else
{
v3 = event_add(a1, v2, connection_client_handler, 3LL);
if ( v3 == -1 )
{
fwrite("connection_accept_handler(): Error seteando el event handler\n", 1uLL, 0x3DuLL, stderr);
client_destroy(v5[0]);
socket_close(v2);
--server[3];
}
else
{
++server[3];
v3 = 0;
}
}
}
return v3;
}

original_code:
int
connection_accept_handler(events_t *ev, event_t *event)
{
 int client_fd;
 client_t *c;
 if((client_fd = socket_accept(event->fd)) == -1)
 {
  perror("connection_accept_handler():");
  return -1;
 }
 if(server.clients == config.max_clients)
 {
  fprintf(stdout, "connection_accept_handler(): Guarda! Se llego al limite de clientes\n");
  socket_close(client_fd);
  return 0;
 }
 if(client_create(&c, client_fd) == -1)
 {
  fprintf(stderr, "connection_accept_handler(): Error creando cliente\n");
  socket_close(client_fd);
  return -1;
 }
 if(event_add(ev, client_fd, &connection_client_handler,
   1 | 2) == -1)
 {
  fprintf(stderr, "connection_accept_handler(): Error seteando el event handler\n");
  client_destroy(c);
  socket_close(client_fd);
  server.clients--;
  return -1;
 }
 server.clients++;
 return 0;
}

canonical_original_code:
int connection_accept_handler(struct events *ev, struct event *event)
{
 int client_fd;
 struct client_t *c;
 if((client_fd = socket_accept(event->fd)) == -1)
 {
  perror("connection_accept_handler():");
  return -1;
 }
 if(server.clients == config.max_clients)
 {
  fprintf(stdout, "connection_accept_handler(): Guarda! Se llego al limite de clientes\n");
  socket_close(client_fd);
  return 0;
 }
 if(client_create(&c, client_fd) == -1)
 {
  fprintf(stderr, "connection_accept_handler(): Error creando cliente\n");
  socket_close(client_fd);
  return -1;
 }
 if(event_add(ev, client_fd, &connection_client_handler,
   1 | 2) == -1)
 {
  fprintf(stderr, "connection_accept_handler(): Error seteando el event handler\n");
  client_destroy(c);
  socket_close(client_fd);
  server.clients--;
  return -1;
 }
 server.clients++;
 return 0;
}

variable_types: struct events { int (**handlers)(struct events *ev, struct event { int fd; int (*handler)(struct events *ev, struct event *event); int event_mask; } *event); unsigned long maxfds; int currfd; struct fd_set { long fds_bits[-1]; } set_read_copy; struct fd_set set_write_copy; struct fd_set set_error_copy; struct fd_set set_read; struct fd_set set_write; struct fd_set set_error; int max_fd; int nevents; int has_timeout; struct timeval { long tv_sec; long tv_usec; } tv; } *

variable_types:
event
struct event *

variable_types: struct client_t { int connection_fd; char *name; unsigned int id; unsigned int offset; int state; } *

user_defined_types: struct events { int (**handlers)(struct events *ev, struct event *event); unsigned long maxfds; int currfd; struct fd_set set_read_copy; struct fd_set set_write_copy; struct fd_set set_error_copy; struct fd_set set_read; struct fd_set set_write; struct fd_set set_error; int max_fd; int nevents; int has_timeout; struct timeval tv; }


decompiled_code:
__int64 server_enter_event_loop()
{
unsigned int v0; // ebx
int v2; // eax
int v3; // ebx
_QWORD v4[5]; // [rsp+0h] [rbp-28h] BYREF

server[2] = 1;
if ( (unsigned int)event_initialize((unsigned int)((_DWORD)server + 32)) == -1 )
{
v0 = -1;
fwrite("server_enter_event_loop(): Error inicializando el monitor de eventos\n", 1uLL, 0x45uLL, stderr);
}
else
{
v0 = event_add((unsigned int)((_DWORD)server + 32), (unsigned int)server[4], connection_accept_handler, 1LL);
if ( v0 == -1 )
{
fwrite("server_enter_event_loop(): Error seteando el event handler\n", 1uLL, 0x3BuLL, stderr);
event_shutdown((unsigned int)((_DWORD)server + 32));
}
else
{
while ( server_running() )
{
v2 = event_wait((unsigned int)((_DWORD)server + 32), (unsigned int)server[216]);
v3 = v2;
if ( v2 == -1 )
{
fwrite("server_enter_event_loop(): Error esperando por eventos\n", 1uLL, 0x37uLL, stderr);
break;
}
if ( v2 > 0 )
{
do
{
v3 -= event_next((unsigned int)((_DWORD)server + 32), v4);
((void (__fastcall *)(_QWORD, _QWORD *))v4[1])((unsigned int)((_DWORD)server + 32), v4);
}
while ( v3 > 0 );
}
fprintf(stdout, "\r[*] Numero de clientes = %d", (unsigned int)server[3]);
}
putchar(10);
event_shutdown((unsigned int)((_DWORD)server + 32));
v0 = 0;
}
}
return v0;
}

canonical_decompiled_code:
__int64 func3()
{
unsigned int v0; // ebx
int v2; // eax
int v3; // ebx
_QWORD v4[5]; // [rsp+0h] [rbp-28h] BYREF

server[2] = 1;
if ( (unsigned int)event_initialize((unsigned int)((_DWORD)server + 32)) == -1 )
{
v0 = -1;
fwrite("server_enter_event_loop(): Error inicializando el monitor de eventos\n", 1uLL, 0x45uLL, stderr);
}
else
{
v0 = event_add((unsigned int)((_DWORD)server + 32), (unsigned int)server[4], connection_accept_handler, 1LL);
if ( v0 == -1 )
{
fwrite("server_enter_event_loop(): Error seteando el event handler\n", 1uLL, 0x3BuLL, stderr);
event_shutdown((unsigned int)((_DWORD)server + 32));
}
else
{
while ( func0() )
{
v2 = event_wait((unsigned int)((_DWORD)server + 32), (unsigned int)server[216]);
v3 = v2;
if ( v2 == -1 )
{
fwrite("server_enter_event_loop(): Error esperando por eventos\n", 1uLL, 0x37uLL, stderr);
break;
}
if ( v2 > 0 )
{
do
{
v3 -= event_next((unsigned int)((_DWORD)server + 32), v4);
((void (__fastcall *)(_QWORD, _QWORD *))v4[1])((unsigned int)((_DWORD)server + 32), v4);
}
while ( v3 > 0 );
}
fprintf(stdout, "\r[*] Numero de clientes = %d", (unsigned int)server[3]);
}
putchar(10);
event_shutdown((unsigned int)((_DWORD)server + 32));
v0 = 0;
}
}
return v0;
}

original_code:
int
server_enter_event_loop(void)
{
 int nevents;
 event_t event;
 server.status = 1;
 if(event_initialize(&server.ev) == -1)
 {
  fprintf(stderr, "server_enter_event_loop(): Error inicializando el monitor de eventos\n");
  return -1;
 }
 if(event_add(&server.ev, server.server_fd,
   &connection_accept_handler, 1) == -1)
 {
  fprintf(stderr, "server_enter_event_loop(): Error seteando el event handler\n");
  event_shutdown(&server.ev);
  return -1;
 }
 while(server_running())
 {
     nevents = event_wait(&server.ev, server.timeout);
     if(nevents == -1)
     {
      fprintf(stderr, "server_enter_event_loop(): Error esperando por eventos\n");
      break;
     }
     while(nevents > 0)
     {
      nevents -= event_next(&server.ev, &event);
      event.handler(&server.ev, &event);
     }
     fprintf(stdout, "\r[*] Numero de clientes = %d", server.clients);
 }
 printf("\n");
 event_shutdown(&server.ev);
 return 0;
}

canonical_original_code:
int server_enter_event_loop()
{
 int nevents;
 struct event event;
 server.status = 1;
 if(event_initialize(&server.ev) == -1)
 {
  fprintf(stderr, "server_enter_event_loop(): Error inicializando el monitor de eventos\n");
  return -1;
 }
 if(event_add(&server.ev, server.server_fd,
   &connection_accept_handler, 1) == -1)
 {
  fprintf(stderr, "server_enter_event_loop(): Error seteando el event handler\n");
  event_shutdown(&server.ev);
  return -1;
 }
 while(server_running())
 {
     nevents = event_wait(&server.ev, server.timeout);
     if(nevents == -1)
     {
      fprintf(stderr, "server_enter_event_loop(): Error esperando por eventos\n");
      break;
     }
     while(nevents > 0)
     {
      nevents -= event_next(&server.ev, &event);
      event.handler(&server.ev, &event);
     }
     fprintf(stdout, "\r[*] Numero de clientes = %d", server.clients);
 }
 printf("\n");
 event_shutdown(&server.ev);
 return 0;
}

variable_types: nevents int

variable_types: struct event { int fd; int (*handler)(struct events { int (**handlers)(struct events *ev, struct event *event); unsigned long maxfds; int currfd; struct fd_set { long fds_bits[-1]; } set_read_copy; struct fd_set set_write_copy; struct fd_set set_error_copy; struct fd_set set_read; struct fd_set set_write; struct fd_set set_error; int max_fd; int nevents; int has_timeout; struct timeval { long tv_sec; long tv_usec; } tv; } *ev, struct event *event); int event_mask; }

user_defined_types: struct event { int fd; int (*handler)(struct events *ev, struct event *event); int event_mask; }


decompiled_code:
__int64 server_leave_event_loop()
{
server[2] = 0;
return 0LL;
}

canonical_decompiled_code:
__int64 func4()
{
server[2] = 0;
return 0LL;
}

original_code:
int
server_leave_event_loop(void)
{
 server.status = 0;
 return 0;
}

canonical_original_code:
int server_leave_event_loop()
{
 server.status = 0;
 return 0;
}


decompiled_code:
__int64 server_initialize()
{
unsigned int v0; // eax
unsigned int v1; // ebx

v0 = socket_tcp_server(config[8]);
v1 = v0;
if ( v0 == -1 )
{
perror("server_initialize():");
}
else
{
if ( (unsigned int)socket_set_nonblocking(v0) )
perror("server_initialize():");
if ( (unsigned int)socket_listen(v1) == -1 )
{
perror("server_initialize():");
socket_close(v1);
v1 = -1;
}
else
{
server[4] = v1;
v1 = 0;
}
}
return v1;
}

canonical_decompiled_code:
__int64 func5()
{
unsigned int v0; // eax
unsigned int v1; // ebx

v0 = socket_tcp_server(config[8]);
v1 = v0;
if ( v0 == -1 )
{
perror("server_initialize():");
}
else
{
if ( (unsigned int)socket_set_nonblocking(v0) )
perror("server_initialize():");
if ( (unsigned int)socket_listen(v1) == -1 )
{
perror("server_initialize():");
socket_close(v1);
v1 = -1;
}
else
{
server[4] = v1;
v1 = 0;
}
}
return v1;
}

original_code:
int
server_initialize(void)
{
 int server_fd;
 server_fd = socket_tcp_server(config.port);
 if(server_fd == -1)
 {
  perror("server_initialize():");
  return -1;
 }
 if(socket_set_nonblocking(server_fd))
 {
  perror("server_initialize():");
 }
 if(socket_listen(server_fd) == -1)
 {
  perror("server_initialize():");
  socket_close(server_fd);
  return -1;
 }
 server.server_fd = server_fd;
 return 0;
}

canonical_original_code:
int server_initialize()
{
 int server_fd;
 server_fd = socket_tcp_server(config.port);
 if(server_fd == -1)
 {
  perror("server_initialize():");
  return -1;
 }
 if(socket_set_nonblocking(server_fd))
 {
  perror("server_initialize():");
 }
 if(socket_listen(server_fd) == -1)
 {
  perror("server_initialize():");
  socket_close(server_fd);
  return -1;
 }
 server.server_fd = server_fd;
 return 0;
}


decompiled_code:
__int64 server_shutdown()
{
socket_close((unsigned int)server[4]);
server[4] = -1;
return 0LL;
}

canonical_decompiled_code:
__int64 func6()
{
socket_close((unsigned int)server[4]);
server[4] = -1;
return 0LL;
}

original_code:
int
server_shutdown(void)
{
 socket_close(server.server_fd);
 server.server_fd = -1;
 return 0;
}

canonical_original_code:
int server_shutdown()
{
 socket_close(server.server_fd);
 server.server_fd = -1;
 return 0;
}


decompiled_code:
__int64 __fastcall event_reset(_DWORD *a1)
{
__int64 result; // rax

result = 0LL;
memset(a1 + 102, 0, 0x80uLL);
memset(a1 + 134, 0, 0x80uLL);
memset(a1 + 166, 0, 0x80uLL);
a1[198] = -1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_DWORD *a1)
{
__int64 result; // rax

result = 0LL;
memset(a1 + 102, 0, 0x80uLL);
memset(a1 + 134, 0, 0x80uLL);
memset(a1 + 166, 0, 0x80uLL);
a1[198] = -1;
return result;
}

variable_types: struct events { int (**handlers)(struct events *ev, struct event { int fd; int (*handler)(struct events *ev, struct event *event); int event_mask; } *event); unsigned long maxfds; int currfd; struct fd_set { long __fds_bits[-1]; } set_read_copy; struct fd_set set_write_copy; struct fd_set set_error_copy; struct fd_set set_read; struct fd_set set_write; struct fd_set set_error; int max_fd; int nevents; int has_timeout; struct timeval { long tv_sec; long tv_usec; } tv; } *


decompiled_code:
__int64 __fastcall event_initialize(__int64 a1)
{
void *v1; // rax
__int64 result; // rax

*(_DWORD *)(a1 + 16) = -1;
*(_QWORD *)(a1 + 8) = 1000LL;
*(_DWORD *)(a1 + 792) = -1;
*(_DWORD *)(a1 + 796) = 0;
v1 = calloc(0x3E8uLL, 8uLL);
*(_QWORD *)a1 = v1;
if ( v1 )
{
event_reset((_DWORD *)a1);
result = 0LL;
}
else
{
perror("event_initialize():");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
void *v1; // rax
__int64 result; // rax

*(_DWORD *)(a1 + 16) = -1;
*(_QWORD *)(a1 + 8) = 1000LL;
*(_DWORD *)(a1 + 792) = -1;
*(_DWORD *)(a1 + 796) = 0;
v1 = calloc(0x3E8uLL, 8uLL);
*(_QWORD *)a1 = v1;
if ( v1 )
{
func0((_DWORD *)a1);
result = 0LL;
}
else
{
perror("event_initialize():");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int
event_initialize(events_t *ev)
{
 ev->currfd = -1;
 ev->maxfds = 1024 - 24;
 ev->max_fd = -1;
 ev->nevents = 0;
 ev->handlers = (handler_t *) calloc(ev->maxfds, sizeof(handler_t));
 if(ev->handlers == ((void *)0))
 {
  perror("event_initialize():");
  return -1;
 }
 event_reset(ev);
 return 0;
}

canonical_original_code:
int event_initialize(struct events *ev)
{
 ev->currfd = -1;
 ev->maxfds = 1024 - 24;
 ev->max_fd = -1;
 ev->nevents = 0;
 ev->handlers = (int (**)(struct events *ev, struct event *event)) calloc(ev->maxfds, sizeof(handler_t));
 if(ev->handlers == ((void *)0))
 {
  perror("event_initialize():");
  return -1;
 }
 event_reset(ev);
 return 0;
}

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


decompiled_code:
__int64 __fastcall event_shutdown(_DWORD *a1)
{
void *v2; // rdi

v2 = *(void **)a1;
if ( v2 )
free(v2);
event_reset(a1);
a1[4] = -1;
a1[198] = -1;
a1[199] = 0;
return 0LL;
}

original_code:
int
event_shutdown(events_t *ev)
{
 if(ev->handlers)
  free(ev->handlers);
 event_reset(ev);
 ev->currfd = -1;
 ev->max_fd = -1;
 ev->nevents = 0;
 return 0;
}

canonical_original_code:
int event_shutdown(struct events *ev)
{
 if(ev->handlers)
  free(ev->handlers);
 event_reset(ev);
 ev->currfd = -1;
 ev->max_fd = -1;
 ev->nevents = 0;
 return 0;
}


original_code:
void
event_set_mask(events_t *ev, int fd, int event_mask)
{
 ((void) (((&(ev->set_read))->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 if (event_mask & 1)
  ((void) (((&(ev->set_read))->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 ((void) (((&(ev->set_write))->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 if (event_mask & 2)
  ((void) (((&(ev->set_write))->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 ((void) (((&(ev->set_error))->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 if (event_mask & 4)
  ((void) (((&(ev->set_error))->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
}

canonical_original_code:
void event_set_mask(struct events *ev, int fd, int event_mask)
{
 ((void) (((&(ev->set_read))->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 if (event_mask & 1)
  ((void) (((&(ev->set_read))->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 ((void) (((&(ev->set_write))->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 if (event_mask & 2)
  ((void) (((&(ev->set_write))->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 ((void) (((&(ev->set_error))->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 if (event_mask & 4)
  ((void) (((&(ev->set_error))->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
}

variable_types: fd int


decompiled_code:
__int64 __fastcall event_del(__int64 a1, int a2)
{
_QWORD *v2; // rax
__int64 v3; // rdx
int v4; // esi
__int64 v5; // rcx
__int64 v6; // rax
__int64 result; // rax

if ( a2 < 0 || (unsigned __int64)(unsigned int)a2 >= *(_QWORD *)(a1 + 8) )
{
fprintf(stderr, "event_del(fd=%d): fd out of range\n", (unsigned int)a2);
result = 0xFFFFFFFFLL;
}
else if ( *(_QWORD *)(*(_QWORD *)a1 + 8LL * a2) )
{
event_set_mask(a1, a2, 0);
v2 = (_QWORD *)(a1 + 8LL * (a2 / 64));
v3 = __ROL8__(-2LL, a2 % 64);
v2[3] &= v3;
v2[19] &= v3;
v2[35] &= v3;
*(_QWORD *)(*(_QWORD *)a1 + 8LL * a2) = 0LL;
v4 = *(_DWORD *)(a1 + 792);
if ( v4 < 0 )
{
result = 0LL;
}
else
{
v5 = *(_QWORD *)a1;
v6 = v4 - 1;
while ( !*(_QWORD *)(v5 + 8 * v6 + 8) )
{
*(_DWORD *)(a1 + 792) = v6--;
if ( v6 == v4 - 2LL - (unsigned int)v4 )
return 0LL;
}
result = 0LL;
}
}
else
{
fprintf(stderr, "event_del(fd=%d): not monitoring that fd!\n", (unsigned int)a2);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2)
{
_QWORD *v2; // rax
__int64 v3; // rdx
int v4; // esi
__int64 v5; // rcx
__int64 v6; // rax
__int64 result; // rax

if ( a2 < 0 || (unsigned __int64)(unsigned int)a2 >= *(_QWORD *)(a1 + 8) )
{
fprintf(stderr, "event_del(fd=%d): fd out of range\n", (unsigned int)a2);
result = 0xFFFFFFFFLL;
}
else if ( *(_QWORD *)(*(_QWORD *)a1 + 8LL * a2) )
{
func3(a1, a2, 0);
v2 = (_QWORD *)(a1 + 8LL * (a2 / 64));
v3 = __ROL8__(-2LL, a2 % 64);
v2[3] &= v3;
v2[19] &= v3;
v2[35] &= v3;
*(_QWORD *)(*(_QWORD *)a1 + 8LL * a2) = 0LL;
v4 = *(_DWORD *)(a1 + 792);
if ( v4 < 0 )
{
result = 0LL;
}
else
{
v5 = *(_QWORD *)a1;
v6 = v4 - 1;
while ( !*(_QWORD *)(v5 + 8 * v6 + 8) )
{
*(_DWORD *)(a1 + 792) = v6--;
if ( v6 == v4 - 2LL - (unsigned int)v4 )
return 0LL;
}
result = 0LL;
}
}
else
{
fprintf(stderr, "event_del(fd=%d): not monitoring that fd!\n", (unsigned int)a2);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int
event_del(events_t *ev, int fd)
{
 if ((fd < 0) || ((unsigned) fd >= ev->maxfds))
 {
  fprintf(stderr, "event_del(fd=%d): fd out of range\n", fd);
  return -1;
 }
 if (ev->handlers[fd] == ((void *)0))
 {
  fprintf(stderr, "event_del(fd=%d): not monitoring that fd!\n", fd);
  return -1;
 }
 event_set_mask(ev, fd, 0);
 ((void) (((&(ev->set_read_copy))->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 ((void) (((&(ev->set_write_copy))->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 ((void) (((&(ev->set_error_copy))->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 ev->handlers[fd] = ((void *)0);
 while ((ev->max_fd >= 0) && (ev->handlers[ev->max_fd] == ((void *)0)))
  ev->max_fd--;
 return 0;
}

canonical_original_code:
int event_del(struct events *ev, int fd)
{
 if ((fd < 0) || ((unsigned int) fd >= ev->maxfds))
 {
  fprintf(stderr, "event_del(fd=%d): fd out of range\n", fd);
  return -1;
 }
 if (ev->handlers[fd] == ((void *)0))
 {
  fprintf(stderr, "event_del(fd=%d): not monitoring that fd!\n", fd);
  return -1;
 }
 event_set_mask(ev, fd, 0);
 ((void) (((&(ev->set_read_copy))->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 ((void) (((&(ev->set_write_copy))->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 ((void) (((&(ev->set_error_copy))->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
 ev->handlers[fd] = ((void *)0);
 while ((ev->max_fd >= 0) && (ev->handlers[ev->max_fd] == ((void *)0)))
  ev->max_fd--;
 return 0;
}


decompiled_code:
__int64 __fastcall event_add(__int64 a1, int a2, __int64 a3, char a4)
{
_QWORD *v6; // rcx
__int64 result; // rax

if ( a2 < 0 || (unsigned __int64)(unsigned int)a2 >= *(_QWORD *)(a1 + 8) )
{
fprintf(stderr, "event_add(fd=%d): fd out of range\n", (unsigned int)a2);
result = 0xFFFFFFFFLL;
}
else
{
v6 = (_QWORD *)(*(_QWORD *)a1 + 8LL * a2);
if ( *v6 )
{
fprintf(stderr, "event_add(fd=%d): already monitoring that fd!\n", (unsigned int)a2);
result = 0xFFFFFFFFLL;
}
else
{
if ( *(_DWORD *)(a1 + 792) < a2 )
*(_DWORD *)(a1 + 792) = a2;
*v6 = a3;
event_set_mask(a1, a2, a4);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, int a2, __int64 a3, char a4)
{
_QWORD *v6; // rcx
__int64 result; // rax

if ( a2 < 0 || (unsigned __int64)(unsigned int)a2 >= *(_QWORD *)(a1 + 8) )
{
fprintf(stderr, "event_add(fd=%d): fd out of range\n", (unsigned int)a2);
result = 0xFFFFFFFFLL;
}
else
{
v6 = (_QWORD *)(*(_QWORD *)a1 + 8LL * a2);
if ( *v6 )
{
fprintf(stderr, "event_add(fd=%d): already monitoring that fd!\n", (unsigned int)a2);
result = 0xFFFFFFFFLL;
}
else
{
if ( *(_DWORD *)(a1 + 792) < a2 )
*(_DWORD *)(a1 + 792) = a2;
*v6 = a3;
func3(a1, a2, a4);
result = 0LL;
}
}
return result;
}

original_code:
int
event_add(events_t *ev, int fd, handler_t handler, int event_mask)
{
 if ((fd < 0) || ((unsigned) fd >= ev->maxfds))
 {
  fprintf(stderr, "event_add(fd=%d): fd out of range\n", fd);
  return -1;
 }
 if (ev->handlers[fd])
 {
  fprintf(stderr, "event_add(fd=%d): already monitoring that fd!\n", fd);
  return -1;
 }
 if (fd > ev->max_fd)
  ev->max_fd = fd;
 ev->handlers[fd] = handler;
 event_set_mask(ev, fd, event_mask);
 return 0;
}

canonical_original_code:
int event_add(struct events *ev, int fd, int (*handler)(struct events *ev, struct event *event), int event_mask)
{
 if ((fd < 0) || ((unsigned int) fd >= ev->maxfds))
 {
  fprintf(stderr, "event_add(fd=%d): fd out of range\n", fd);
  return -1;
 }
 if (ev->handlers[fd])
 {
  fprintf(stderr, "event_add(fd=%d): already monitoring that fd!\n", fd);
  return -1;
 }
 if (fd > ev->max_fd)
  ev->max_fd = fd;
 ev->handlers[fd] = handler;
 event_set_mask(ev, fd, event_mask);
 return 0;
}

variable_types: int (*)(struct events *ev, struct event *event)


original_code:
int
event_wait(events_t *ev, int microseconds)
{
 struct timeval *t = ((void *)0);
 if(microseconds != -1)
 {
  t = &ev->tv;
  t->tv_sec = 0;
  t->tv_usec = microseconds;
 }
 ev->set_read_copy = ev->set_read;
 ev->set_write_copy = ev->set_write;
 ev->set_error_copy = ev->set_error;
 ev->currfd = -1;
 ev->nevents = select(ev->max_fd + 1, &(ev->set_read_copy),
   &(ev->set_write_copy), &(ev->set_error_copy), t);
 if(ev->nevents == -1)
 {
  if((*__errno_location ()) == 4)
   return 0;
  perror("event_wait():");
  return -1;
 }
 return ev->nevents;
}

canonical_original_code:
int event_wait(struct events *ev, int microseconds)
{
 struct timeval *t = ((void *)0);
 if(microseconds != -1)
 {
  t = &ev->tv;
  t->tv_sec = 0;
  t->tv_usec = microseconds;
 }
 ev->set_read_copy = ev->set_read;
 ev->set_write_copy = ev->set_write;
 ev->set_error_copy = ev->set_error;
 ev->currfd = -1;
 ev->nevents = select(ev->max_fd + 1, &(ev->set_read_copy),
   &(ev->set_write_copy), &(ev->set_error_copy), t);
 if(ev->nevents == -1)
 {
  if((*__errno_location ()) == 4)
   return 0;
  perror("event_wait():");
  return -1;
 }
 return ev->nevents;
}

variable_types: microseconds int

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


original_code:
int
event_next(events_t *ev, event_t *event)
{
 ((void) sizeof ((event != ((void *)0)) ? 1 : 0), __extension__ ({ if (event != ((void *)0)) ; else __assert_fail ("event != NULL", "./event/event.c", 179, __extension__ __PRETTY_FUNCTION__); }));
 int ret =0;
 event->event_mask = 0;
 for (; ev->currfd++ < ev->max_fd;)
 {
  if (((((&(ev->set_read_copy))->__fds_bits)[((ev->currfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ev->currfd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  {
   event->event_mask |= 1;
   ev->nevents--;
   ret++;
  }
  if (((((&(ev->set_write_copy))->__fds_bits)[((ev->currfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ev->currfd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  {
   event->event_mask |= 2;
   ev->nevents--;
   ret++;
  }
  if (((((&(ev->set_error_copy))->__fds_bits)[((ev->currfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ev->currfd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  {
   event->event_mask |= 4;
   ev->nevents--;
   ret++;
  }
  if(event->event_mask)
   break;
 }
 event->fd = ev->currfd;
 event->handler = ev->handlers[ev->currfd];
 ((void) sizeof ((event->handler != ((void *)0)) ? 1 : 0), __extension__ ({ if (event->handler != ((void *)0)) ; else __assert_fail ("event->handler != NULL", "./event/event.c", 214, __extension__ __PRETTY_FUNCTION__); }));
 return ret;
}

canonical_original_code:
int event_next(struct events *ev, struct event *event)
{
 ((void) sizeof ((event != ((void *)0)) ? 1 : 0), __extension__ ({ if (event != ((void *)0)) ; else __assert_fail ("event != NULL", "./event/event.c", 179, __extension__ __PRETTY_FUNCTION__); }));
 int ret = 0;
 event->event_mask = 0;
 for (; ev->currfd++ < ev->max_fd;)
 {
  if (((((&(ev->set_read_copy))->__fds_bits)[((ev->currfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ev->currfd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  {
   event->event_mask |= 1;
   ev->nevents--;
   ret++;
  }
  if (((((&(ev->set_write_copy))->__fds_bits)[((ev->currfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ev->currfd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  {
   event->event_mask |= 2;
   ev->nevents--;
   ret++;
  }
  if (((((&(ev->set_error_copy))->__fds_bits)[((ev->currfd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((ev->currfd) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  {
   event->event_mask |= 4;
   ev->nevents--;
   ret++;
  }
  if(event->event_mask)
   break;
 }
 event->fd = ev->currfd;
 event->handler = ev->handlers[ev->currfd];
 ((void) sizeof ((event->handler != ((void *)0)) ? 1 : 0), __extension__ ({ if (event->handler != ((void *)0)) ; else __assert_fail ("event->handler != NULL", "./event/event.c", 214, __extension__ __PRETTY_FUNCTION__); }));
 return ret;
}

variable_types: ret int


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

clients = (__int64)calloc(a1, 8uLL);
if ( clients )
{
client_limit = a1;
result = 0LL;
}
else
{
perror("client_initialize():");
result = 0xFFFFFFFFLL;
}
return result;
}

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

clients = (__int64)calloc(a1, 8uLL);
if ( clients )
{
client_limit = a1;
result = 0LL;
}
else
{
perror("client_initialize():");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int
client_initialize(size_t max_clients)
{
 clients = (client_t **) calloc(max_clients, sizeof(client_t *));
 if(clients == ((void *)0))
 {
  perror("client_initialize():");
  return -1;
 }
 client_limit = max_clients;
 return 0;
}

canonical_original_code:
int client_initialize(unsigned long max_clients)
{
 clients = (struct client_t **) calloc(max_clients, sizeof(struct client_t *));
 if(clients == ((void *)0))
 {
  perror("client_initialize():");
  return -1;
 }
 client_limit = max_clients;
 return 0;
}

user_defined_types: struct client_t { int connection_fd; char *name; unsigned int id; unsigned int offset; int state; }


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

result = 0LL;
if ( a1 <= (unsigned __int64)client_limit )
result = *(_QWORD *)(clients + 8LL * a1);
return result;
}

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

result = 0LL;
if ( a1 <= (unsigned __int64)client_limit )
result = *(_QWORD *)(clients + 8LL * a1);
return result;
}

original_code:
client_t *
client_get(int fd)
{
 return ((size_t) fd > client_limit) ? ((void *)0) : clients[fd];
}

canonical_original_code:
struct client_t *client_get(int fd)
{
 return ((unsigned long) fd > client_limit) ? ((void *)0) : clients[fd];
}

return_type: struct client_t { int connection_fd; char *name; unsigned int id; unsigned int offset; int state; } *


decompiled_code:
__int64 __fastcall client_create(_QWORD *a1, int a2)
{
_DWORD *v2; // rax
__int64 result; // rax

if ( current_clients == client_limit )
{
fwrite("client_create(): Se llego al limite de clientes\n", 1uLL, 0x30uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
++current_clients;
v2 = malloc(0x20uLL);
if ( v2 )
{
*v2 = a2;
v2[4] = a2;
*((_QWORD *)v2 + 1) = "1337 Client";
v2[5] = 0;
v2[6] = 0;
*a1 = v2;
result = client_add(v2);
}
else
{
perror("client_create():");
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(_QWORD *a1, int a2)
{
_DWORD *v2; // rax
__int64 result; // rax

if ( current_clients == client_limit )
{
fwrite("client_create(): Se llego al limite de clientes\n", 1uLL, 0x30uLL, stderr);
result = 0xFFFFFFFFLL;
}
else
{
++current_clients;
v2 = malloc(0x20uLL);
if ( v2 )
{
*v2 = a2;
v2[4] = a2;
*((_QWORD *)v2 + 1) = "1337 Client";
v2[5] = 0;
v2[6] = 0;
*a1 = v2;
result = func0(v2);
}
else
{
perror("client_create():");
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int
client_create(client_t **client, int connection)
{
 if(current_clients == client_limit)
 {
  fprintf(stderr, "client_create(): Se llego al limite de clientes\n");
  return -1;
 }
 current_clients++;
 client_t *tmp = (client_t *) malloc(sizeof(client_t));
 if(!tmp)
 {
  perror("client_create():");
  return -1;
 }
 tmp->connection_fd = connection;
 tmp->id = connection;
 tmp->name = "1337 Client";
 tmp->offset = 0;
 tmp->state = 0;
 *client = tmp;
 return client_add(*client);
}

canonical_original_code:
int client_create(struct client_t **client, int connection)
{
 if(current_clients == client_limit)
 {
  fprintf(stderr, "client_create(): Se llego al limite de clientes\n");
  return -1;
 }
 current_clients++;
 struct client_t *tmp = (client_t *) malloc(sizeof(client_t));
 if(!tmp)
 {
  perror("client_create():");
  return -1;
 }
 tmp->connection_fd = connection;
 tmp->id = connection;
 tmp->name = "1337 Client";
 tmp->offset = 0;
 tmp->state = 0;
 *client = tmp;
 return client_add(*client);
}

variable_types: struct client_t { int connection_fd; char *name; unsigned int id; unsigned int offset; int state; } **

variable_types: connection int


decompiled_code:
__int64 __fastcall client_destroy(void *ptr)
{
unsigned int v1; // eax
unsigned int v2; // ebp

v1 = client_remove((unsigned int *)ptr);
if ( !ptr )
_assert_fail("client != NULL", "./client/client.c", 0x87u, "client_destroy");
v2 = v1;
free(ptr);
--current_clients;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func5(void *ptr)
{
unsigned int v1; // eax
unsigned int v2; // ebp

v1 = func1((unsigned int *)ptr);
if ( !ptr )
_assert_fail("client != NULL", "./client/client.c", 0x87u, "client_destroy");
v2 = v1;
free(ptr);
--current_clients;
return v2;
}

original_code:
int
client_destroy(client_t * client)
{
 int ret = client_remove(client);
 ((void) sizeof ((client != ((void *)0)) ? 1 : 0), __extension__ ({ if (client != ((void *)0)) ; else __assert_fail ("client != NULL", "./client/client.c", 135, __extension__ __PRETTY_FUNCTION__); }));
 free(client);
 current_clients--;
 return ret;
}

canonical_original_code:
int client_destroy(struct client_t *client)
{
 int ret = client_remove(client);
 ((void) sizeof ((client != ((void *)0)) ? 1 : 0), __extension__ ({ if (client != ((void *)0)) ; else __assert_fail ("client != NULL", "./client/client.c", 135, __extension__ __PRETTY_FUNCTION__); }));
 free(client);
 current_clients--;
 return ret;
}


decompiled_code:
__int64 __fastcall stream_create(_QWORD *a1, const char *a2)
{
unsigned int v2; // ebx
_QWORD *v3; // rbp
void *v4; // r13
struct stat v6; // [rsp+0h] [rbp-B8h] BYREF

v2 = open(a2, 0);
if ( v2 == -1 )
{
perror("stream_create():");
}
else
{
v3 = malloc(0x20uLL);
if ( v3 )
{
if ( fstat(v2, &v6) == -1 || (v4 = mmap(0LL, v6.st_size, 1, 2, v2, 0LL), v4 == (void *)-1LL) )
{
perror("stream_create():");
free(v3);
*a1 = 0LL;
close(v2);
v2 = -1;
}
else
{
*v3 = strdup(a2);
*((_DWORD *)v3 + 2) = 0;
v3[2] = v6.st_size;
v3[3] = v4;
*a1 = v3;
close(v2);
v2 = 0;
}
}
else
{
perror("stream_create():");
*a1 = 0LL;
close(v2);
v2 = -1;
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1, const char *a2)
{
unsigned int v2; // ebx
_QWORD *v3; // rbp
void *v4; // r13
struct stat v6; // [rsp+0h] [rbp-B8h] BYREF

v2 = open(a2, 0);
if ( v2 == -1 )
{
perror("stream_create():");
}
else
{
v3 = malloc(0x20uLL);
if ( v3 )
{
if ( fstat(v2, &v6) == -1 || (v4 = mmap(0LL, v6.st_size, 1, 2, v2, 0LL), v4 == (void *)-1LL) )
{
perror("stream_create():");
free(v3);
*a1 = 0LL;
close(v2);
v2 = -1;
}
else
{
*v3 = strdup(a2);
*((_DWORD *)v3 + 2) = 0;
v3[2] = v6.st_size;
v3[3] = v4;
*a1 = v3;
close(v2);
v2 = 0;
}
}
else
{
perror("stream_create():");
*a1 = 0LL;
close(v2);
v2 = -1;
}
}
return v2;
}

original_code:
int
stream_create(stream_t **stream, const char *filename)
{
 int fd;
 stream_t *tmp;
 if((fd = open(filename, 00)) == -1)
 {
  perror("stream_create():");
  return -1;
 }
 tmp = (stream_t *) malloc(sizeof(stream_t));
 if(!tmp)
 {
  perror("stream_create():");
  *stream = ((void *)0);
  close(fd);
  return -1;
 }
 struct stat sb;
 if (fstat(fd, &sb) == -1)
 {
  perror("stream_create():");
  free(tmp);
  *stream = ((void *)0);
  close(fd);
  return -1;
 }
 void *addr;
 addr = mmap(((void *)0), sb.st_size, 0x1, 0x02, fd, 0);
 if(addr == ((void *) -1))
 {
  perror("stream_create():");
  free(tmp);
  *stream = ((void *)0);
  close(fd);
  return -1;
 }
 tmp->filename = strdup(filename);
 tmp->id = 0;
 tmp->size = sb.st_size;
 tmp->bytes = (char *) addr;
 *stream = tmp;
 close(fd);
 return 0;
}

canonical_original_code:
int stream_create(struct stream_t **stream, char *filename)
{
 int fd;
 struct stream_t *tmp;
 if((fd = open(filename, 00)) == -1)
 {
  perror("stream_create():");
  return -1;
 }
 tmp = (struct stream_t *) malloc(sizeof(stream_t));
 if(!tmp)
 {
  perror("stream_create():");
  *stream = ((void *)0);
  close(fd);
  return -1;
 }
 struct stat sb;
 if (fstat(fd, &sb) == -1)
 {
  perror("stream_create():");
  free(tmp);
  *stream = ((void *)0);
  close(fd);
  return -1;
 }
 void *addr;
 addr = mmap(((void *)0), sb.st_size, 0x1, 0x02, fd, 0);
 if(addr == ((void *) -1))
 {
  perror("stream_create():");
  free(tmp);
  *stream = ((void *)0);
  close(fd);
  return -1;
 }
 tmp->filename = strdup(filename);
 tmp->id = 0;
 tmp->size = sb.st_size;
 tmp->bytes = (char *) addr;
 *stream = tmp;
 close(fd);
 return 0;
}

variable_types: struct stream_t { char *filename; int id; unsigned long size; char *bytes; } **

variable_types:
filename
char *

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

variable_types:
addr
void *

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


decompiled_code:
__int64 __fastcall stream_destroy(void *ptr)
{
unsigned int v1; // ebp

if ( !ptr )
_assert_fail("stream != NULL", "./stream/stream.c", 0x4Fu, "stream_destroy");
v1 = munmap(*((void **)ptr + 3), *((_QWORD *)ptr + 2));
if ( v1 == -1 )
{
perror("stream_destroy():");
}
else
{
free(*(void **)ptr);
free(ptr);
v1 = 0;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(void *ptr)
{
unsigned int v1; // ebp

if ( !ptr )
_assert_fail("stream != NULL", "./stream/stream.c", 0x4Fu, "stream_destroy");
v1 = munmap(*((void **)ptr + 3), *((_QWORD *)ptr + 2));
if ( v1 == -1 )
{
perror("stream_destroy():");
}
else
{
free(*(void **)ptr);
free(ptr);
v1 = 0;
}
return v1;
}

original_code:
int
stream_destroy(stream_t *stream)
{
 ((void) sizeof ((stream != ((void *)0)) ? 1 : 0), __extension__ ({ if (stream != ((void *)0)) ; else __assert_fail ("stream != NULL", "./stream/stream.c", 79, __extension__ __PRETTY_FUNCTION__); }));
 if(munmap(stream->bytes, stream->size) == -1)
 {
  perror("stream_destroy():");
  return -1;
 }
 free(stream->filename);
 free(stream);
 return 0;
}

canonical_original_code:
int stream_destroy(struct stream_t *stream)
{
 ((void) sizeof ((stream != ((void *)0)) ? 1 : 0), __extension__ ({ if (stream != ((void *)0)) ; else __assert_fail ("stream != NULL", "./stream/stream.c", 79, __extension__ __PRETTY_FUNCTION__); }));
 if(munmap(stream->bytes, stream->size) == -1)
 {
  perror("stream_destroy():");
  return -1;
 }
 free(stream->filename);
 free(stream);
 return 0;
}

variable_types: struct stream_t { char *filename; int id; unsigned long size; char *bytes; } *


decompiled_code:
__int64 __fastcall stream_read(__int64 a1, __int64 a2, __int64 a3, _QWORD *a4)
{
__int64 result; // rax

if ( !a4 )
_assert_fail("buffer != NULL", "./stream/stream.c", 0x60u, "stream_read");
if ( !a3 )
_assert_fail("size != 0", "./stream/stream.c", 0x61u, "stream_read");
if ( (unsigned __int64)(a2 + a3) > *(_QWORD *)(a1 + 16) )
_assert_fail("offset+size <= stream->size", "./stream/stream.c", 0x62u, "stream_read");
*a4 = *(_QWORD *)(a1 + 24) + a2;
result = (unsigned int)*(_QWORD *)(a1 + 16) - (unsigned int)a2;
if ( (unsigned __int64)(a2 + a3) <= *(_QWORD *)(a1 + 16) )
result = (unsigned int)a3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, __int64 a3, _QWORD *a4)
{
__int64 result; // rax

if ( !a4 )
_assert_fail("buffer != NULL", "./stream/stream.c", 0x60u, "stream_read");
if ( !a3 )
_assert_fail("size != 0", "./stream/stream.c", 0x61u, "stream_read");
if ( (unsigned __int64)(a2 + a3) > *(_QWORD *)(a1 + 16) )
_assert_fail("offset+size <= stream->size", "./stream/stream.c", 0x62u, "stream_read");
*a4 = *(_QWORD *)(a1 + 24) + a2;
result = (unsigned int)*(_QWORD *)(a1 + 16) - (unsigned int)a2;
if ( (unsigned __int64)(a2 + a3) <= *(_QWORD *)(a1 + 16) )
result = (unsigned int)a3;
return result;
}

original_code:
int
stream_read(stream_t *stream, off_t offset, size_t size, char **buffer)
{
 ((void) sizeof ((buffer != ((void *)0)) ? 1 : 0), __extension__ ({ if (buffer != ((void *)0)) ; else __assert_fail ("buffer != NULL", "./stream/stream.c", 96, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((size != 0) ? 1 : 0), __extension__ ({ if (size != 0) ; else __assert_fail ("size != 0", "./stream/stream.c", 97, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((offset+size <= stream->size) ? 1 : 0), __extension__ ({ if (offset+size <= stream->size) ; else __assert_fail ("offset+size <= stream->size", "./stream/stream.c", 98, __extension__ __PRETTY_FUNCTION__); }));
 *buffer = stream->bytes + offset;
 return (offset + size <= stream->size) ? size : stream->size - offset;
}

canonical_original_code:
int stream_read(struct stream_t *stream, long offset, unsigned long size, char **buffer)
{
 ((void) sizeof ((buffer != ((void *)0)) ? 1 : 0), __extension__ ({ if (buffer != ((void *)0)) ; else __assert_fail ("buffer != NULL", "./stream/stream.c", 96, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((size != 0) ? 1 : 0), __extension__ ({ if (size != 0) ; else __assert_fail ("size != 0", "./stream/stream.c", 97, __extension__ __PRETTY_FUNCTION__); }));
 ((void) sizeof ((offset+size <= stream->size) ? 1 : 0), __extension__ ({ if (offset+size <= stream->size) ; else __assert_fail ("offset+size <= stream->size", "./stream/stream.c", 98, __extension__ __PRETTY_FUNCTION__); }));
 *buffer = stream->bytes + offset;
 return (offset + size <= stream->size) ? size : stream->size - offset;
}

variable_types: offset long

variable_types:
size
unsigned long

variable_types:
buffer
char **

user_defined_types: struct stream_t { char *filename; int id; unsigned long size; char *bytes; }


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

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

original_code:
int go_controller_init() {
  return 0;
}

canonical_original_code:
int go_controller_init() {
  return 0;
}


decompiled_code:
_DWORD *go_controller_launch_engine()
{
_DWORD *result; // rax
int *v1; // rbx
char *v2; // rax
int *v3; // rbx
char *v4; // rax
int *v5; // rbx
char *v6; // rax
int fd; // [rsp+0h] [rbp-18h] BYREF
int v8; // [rsp+4h] [rbp-14h]
int pipedes[2]; // [rsp+8h] [rbp-10h] BYREF

if ( pipe(pipedes) )
{
v1 = __errno_location();
v2 = strerror(*v1);
printf("pipe() failed with error %i: %s\n", (unsigned int)*v1, v2);
result = 0LL;
}
else if ( pipe(&fd) )
{
v3 = __errno_location();
v4 = strerror(*v3);
printf("pipe() failed with error %i: %s\n", (unsigned int)*v3, v4);
result = 0LL;
}
else
{
if ( !fork() )
{
close(0);
close(1);
close(2);
dup2(fd, 0);
dup2(pipedes[1], 1);
dup2(pipedes[1], 2);
close(v8);
close(fd);
close(pipedes[1]);
close(pipedes[0]);
if ( execl("gnugo", "/usr/bin/gnugo", "--mode", "gtp", 0LL) == -1 )
{
v5 = __errno_location();
v6 = strerror(*v5);
printf("gnugo has finished with err: %i: %i: %s\n", 0xFFFFFFFFLL, (unsigned int)*v5, v6);
}
exit(1);
}
close(fd);
close(pipedes[1]);
result = malloc(8uLL);
*result = v8;
result[1] = pipedes[0];
}
return result;
}

canonical_decompiled_code:
_DWORD *func8()
{
_DWORD *result; // rax
int *v1; // rbx
char *v2; // rax
int *v3; // rbx
char *v4; // rax
int *v5; // rbx
char *v6; // rax
int fd; // [rsp+0h] [rbp-18h] BYREF
int v8; // [rsp+4h] [rbp-14h]
int pipedes[2]; // [rsp+8h] [rbp-10h] BYREF

if ( pipe(pipedes) )
{
v1 = __errno_location();
v2 = strerror(*v1);
printf("pipe() failed with error %i: %s\n", (unsigned int)*v1, v2);
result = 0LL;
}
else if ( pipe(&fd) )
{
v3 = __errno_location();
v4 = strerror(*v3);
printf("pipe() failed with error %i: %s\n", (unsigned int)*v3, v4);
result = 0LL;
}
else
{
if ( !fork() )
{
close(0);
close(1);
close(2);
dup2(fd, 0);
dup2(pipedes[1], 1);
dup2(pipedes[1], 2);
close(v8);
close(fd);
close(pipedes[1]);
close(pipedes[0]);
if ( execl("gnugo", "/usr/bin/gnugo", "--mode", "gtp", 0LL) == -1 )
{
v5 = __errno_location();
v6 = strerror(*v5);
printf("gnugo has finished with err: %i: %i: %s\n", 0xFFFFFFFFLL, (unsigned int)*v5, v6);
}
exit(1);
}
close(fd);
close(pipedes[1]);
result = malloc(8uLL);
*result = v8;
result[1] = pipedes[0];
}
return result;
}

original_code:
go_controller_context* go_controller_launch_engine(const go_controller_setup* const setup) {
  int r_pipes[2];
  int w_pipes[2];
  int err;
  pid_t pid;
  go_controller_context* context = ((void *)0);
  err = pipe(r_pipes);
  if (err != 0) {
    printf("pipe() failed with error %i: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
    return ((void *)0);
  }
  err = pipe(w_pipes);
  if (err != 0) {
    printf("pipe() failed with error %i: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
    return ((void *)0);
  }
  pid = fork();
  if (pid == 0) {
    close(0);
    close(1);
    close(2);
    dup2(w_pipes[0], 0);
    dup2(r_pipes[1], 1);
    dup2(r_pipes[1], 2);
    close(w_pipes[1]);
    close(w_pipes[0]);
    close(r_pipes[1]);
    close(r_pipes[0]);
    int err = execl("gnugo", "/usr/bin/gnugo", "--mode", "gtp", ((void *)0));
    if (err == -1)
      printf("gnugo has finished with err: %i: %i: %s\n", err, (*__errno_location ()), strerror((*__errno_location ())));
    exit(1);
  }
  close(w_pipes[0]);
  close(r_pipes[1]);
  context = malloc(sizeof(go_controller_context));
  context->input_handle = w_pipes[1];
  context->output_handle = r_pipes[0];
  return context;
}

canonical_original_code:
struct go_controller_context *go_controller_launch_engine(struct go_controller_setup *setup) {
  int r_pipes[2];
  int w_pipes[2];
  int err;
  int pid;
  struct go_controller_context *context = ((void *)0);
  err = pipe(r_pipes);
  if (err != 0) {
    printf("pipe() failed with error %i: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
    return ((void *)0);
  }
  err = pipe(w_pipes);
  if (err != 0) {
    printf("pipe() failed with error %i: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
    return ((void *)0);
  }
  pid = fork();
  if (pid == 0) {
    close(0);
    close(1);
    close(2);
    dup2(w_pipes[0], 0);
    dup2(r_pipes[1], 1);
    dup2(r_pipes[1], 2);
    close(w_pipes[1]);
    close(w_pipes[0]);
    close(r_pipes[1]);
    close(r_pipes[0]);
    int err = execl("gnugo", "/usr/bin/gnugo", "--mode", "gtp", ((void *)0));
    if (err == -1)
      printf("gnugo has finished with err: %i: %i: %s\n", err, (*__errno_location ()), strerror((*__errno_location ())));
    exit(1);
  }
  close(w_pipes[0]);
  close(r_pipes[1]);
  context = malloc(sizeof(go_controller_context));
  context->input_handle = w_pipes[1];
  context->output_handle = r_pipes[0];
  return context;
}

variable_types: struct go_controller_setup { int level; } *

variable_types: err int

variable_types: pid int

variable_types: struct go_controller_context { int input_handle; int output_handle; } *


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

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

original_code:
int go_controller_close() {
  return 0;
}

canonical_original_code:
int go_controller_close() {
  return 0;
}


decompiled_code:
__int64 __fastcall send_command(char *src, int *a2)
{
int v2; // ebx
void *v3; // rsp
size_t v4; // rax
ssize_t v5; // rdx
__int64 result; // rax
int *v7; // rbx
char *v8; // rax
char v9[32]; // [rsp+0h] [rbp-20h] BYREF

v2 = strlen(src);
v3 = alloca(v2 + 2);
strncpy(v9, src, v2);
v9[v2] = 10;
v9[v2 + 1] = 0;
v4 = strlen(v9);
v5 = write(*a2, v9, v4);
result = 0LL;
if ( v5 < 0 )
{
v7 = __errno_location();
v8 = strerror(*v7);
printf("write failed with %i: %s\n", (unsigned int)*v7, v8);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(char *src, int *a2)
{
int v2; // ebx
void *v3; // rsp
size_t v4; // rax
ssize_t v5; // rdx
__int64 result; // rax
int *v7; // rbx
char *v8; // rax
char v9[32]; // [rsp+0h] [rbp-20h] BYREF

v2 = strlen(src);
v3 = alloca(v2 + 2);
strncpy(v9, src, v2);
v9[v2] = 10;
v9[v2 + 1] = 0;
v4 = strlen(v9);
v5 = write(*a2, v9, v4);
result = 0LL;
if ( v5 < 0 )
{
v7 = __errno_location();
v8 = strerror(*v7);
printf("write failed with %i: %s\n", (unsigned int)*v7, v8);
result = 1LL;
}
return result;
}

original_code:
int send_command(char* command, go_controller_context* context) {
  ssize_t wc;
  int len;
  len = strlen(command);
  char cmd[len + 2];
  strncpy(cmd, command, len);
  cmd[len] = '\n';
  cmd[len + 1] = '\0';
  wc = write(context->input_handle, cmd, strlen(cmd));
  if (wc < 0) {
    printf("write failed with %i: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
    return 1;
  }
  return 0;
}

canonical_original_code:
int send_command(char *command, struct go_controller_context *context) {
  long wc;
  int len;
  len = strlen(command);
  char cmd[-1];
  strncpy(cmd, command, len);
  cmd[len] = '\n';
  cmd[len + 1] = '\0';
  wc = write(context->input_handle, cmd, strlen(cmd));
  if (wc < 0) {
    printf("write failed with %i: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
    return 1;
  }
  return 0;
}

variable_types:
command
char *

variable_types: wc long

variable_types: len int

variable_types:
cmd
char [-1]

user_defined_types: struct go_controller_context { int input_handle; int output_handle; }


decompiled_code:
__int64 __fastcall go_controller_set_board_size(unsigned int a1, int *a2)
{
__int64 result; // rax
char v3[136]; // [rsp+0h] [rbp-88h] BYREF

if ( a1 - 3 > 0x16 )
{
printf("Invalid size. Must be between %i and %i\n", 3LL, 25LL);
result = 1LL;
}
else
{
sprintf(v3, "boardsize %i", a1);
result = send_command(v3, a2);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned int a1, int *a2)
{
__int64 result; // rax
char v3[136]; // [rsp+0h] [rbp-88h] BYREF

if ( a1 - 3 > 0x16 )
{
printf("Invalid size. Must be between %i and %i\n", 3LL, 25LL);
result = 1LL;
}
else
{
sprintf(v3, "boardsize %i", a1);
result = func10(v3, a2);
}
return result;
}

original_code:
int go_controller_set_board_size(int size, go_controller_context* context) {
  const int min_size = 3;
  const int max_size = 25;
  const size_t buf_size = 128;
  char cmd[buf_size];
  int err;
  if (size < 3 || size > 25) {
    printf("Invalid size. Must be between %i and %i\n", min_size, max_size);
    return 1;
  }
  sprintf(cmd, "boardsize %i", size);
  err = send_command(cmd, context);
  return err;
}

canonical_original_code:
int go_controller_set_board_size(int size, struct go_controller_context *context) {
  int min_size = 3;
  int max_size = 25;
  unsigned long buf_size = 128;
  char cmd[-1];
  int err;
  if (size < 3 || size > 25) {
    printf("Invalid size. Must be between %i and %i\n", min_size, max_size);
    return 1;
  }
  sprintf(cmd, "boardsize %i", size);
  err = send_command(cmd, context);
  return err;
}

variable_types: size int


decompiled_code:
__int64 __fastcall go_controller_quit_engine(void *ptr)
{
unsigned int v1; // ebp

v1 = send_command("quit", (int *)ptr);
close(*(_DWORD *)ptr);
close(*((_DWORD *)ptr + 1));
free(ptr);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func12(void *ptr)
{
unsigned int v1; // ebp

v1 = func10("quit", (int *)ptr);
close(*(_DWORD *)ptr);
close(*((_DWORD *)ptr + 1));
free(ptr);
return v1;
}

original_code:
int go_controller_quit_engine(go_controller_context* context) {
  int err = send_command("quit", context);
  close(context->input_handle);
  close(context->output_handle);
  free(context);
  return err;
}

canonical_original_code:
int go_controller_quit_engine(struct go_controller_context *context) {
  int err = send_command("quit", context);
  close(context->input_handle);
  close(context->output_handle);
  free(context);
  return err;
}


decompiled_code:
__int64 __fastcall wait_for_activity(int a1)
{
int v1; // ebx
int *v3; // rbp
char *v4; // rax
fd_set v5; // [rsp+0h] [rbp-98h] BYREF

memset(&v5, 0, sizeof(v5));
v5.fds_bits[a1 / 64] |= 1LL << (a1 % 64);
v1 = select(a1 + 1, &v5, 0LL, 0LL, 0LL);
if ( v1 < 0 )
{
v3 = __errno_location();
v4 = strerror(*v3);
printf("Error occurred: %i: %s\n", (unsigned int)*v3, v4);
}
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func13(int a1)
{
int v1; // ebx
int *v3; // rbp
char *v4; // rax
fd_set v5; // [rsp+0h] [rbp-98h] BYREF

memset(&v5, 0, sizeof(v5));
v5.fds_bits[a1 / 64] |= 1LL << (a1 % 64);
v1 = select(a1 + 1, &v5, 0LL, 0LL, 0LL);
if ( v1 < 0 )
{
v3 = __errno_location();
v4 = strerror(*v3);
printf("Error occurred: %i: %s\n", (unsigned int)*v3, v4);
}
return (unsigned int)v1;
}

original_code:
int wait_for_activity(int read_handle) {
  fd_set read_fds;
  int err;
  do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&read_fds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&read_fds)->__fds_bits)[((read_handle) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((read_handle) % (8 * (int) sizeof (__fd_mask)))))));
  err = select(read_handle + 1, &read_fds, ((void *)0), ((void *)0), ((void *)0));
  if (err < 0) {
    printf("Error occurred: %i: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
  }
  return err;
}

canonical_original_code:
int wait_for_activity(int read_handle) {
  struct fd_set read_fds;
  int err;
  do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&read_fds)->__fds_bits)[0]) : "memory"); } while (0);
  ((void) (((&read_fds)->__fds_bits)[((read_handle) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((read_handle) % (8 * (int) sizeof (__fd_mask)))))));
  err = select(read_handle + 1, &read_fds, ((void *)0), ((void *)0), ((void *)0));
  if (err < 0) {
    printf("Error occurred: %i: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
  }
  return err;
}


decompiled_code:
__int64 __fastcall get_response(char *a1, char *a2, size_t a3, int *a4)
{
unsigned int v6; // ebx
signed int v8; // eax
size_t v9; // rax
int *v10; // rbx
char *v11; // rax

v6 = send_command(a1, a4);
if ( !v6 )
{
v8 = wait_for_activity(a4[1]);
if ( v8 < 0LL )
{
v6 = v8;
}
else if ( v8 )
{
memset(a2, 0, a3);
if ( read(a4[1], a2, a3) < 0 )
{
v10 = __errno_location();
v11 = strerror(*v10);
printf("read failed with %i: %s\n", (unsigned int)*v10, v11);
v6 = 1;
}
else if ( (*a2 & 0xFD) == 61 )
{
v9 = strlen(a2);
strncpy(a2, a2 + 2, v9);
}
}
else
{
puts("wait_for_activity returned zero");
}
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func14(char *a1, char *a2, size_t a3, int *a4)
{
unsigned int v6; // ebx
signed int v8; // eax
size_t v9; // rax
int *v10; // rbx
char *v11; // rax

v6 = func10(a1, a4);
if ( !v6 )
{
v8 = func13(a4[1]);
if ( v8 < 0LL )
{
v6 = v8;
}
else if ( v8 )
{
memset(a2, 0, a3);
if ( read(a4[1], a2, a3) < 0 )
{
v10 = __errno_location();
v11 = strerror(*v10);
printf("read failed with %i: %s\n", (unsigned int)*v10, v11);
v6 = 1;
}
else if ( (*a2 & 0xFD) == 61 )
{
v9 = strlen(a2);
strncpy(a2, a2 + 2, v9);
}
}
else
{
puts("wait_for_activity returned zero");
}
}
return v6;
}

original_code:
int get_response(char* command, char* response, size_t response_buf_size, go_controller_context* context) {
  ssize_t rc;
  int err;
  err = send_command(command, context);
  if (err != 0)
    return err;
  rc = wait_for_activity(context->output_handle);
  if (rc < 0)
    return rc;
  else if (rc == 0) {
    printf("wait_for_activity returned zero\n");
  }
  else {
    memset(response, 0, response_buf_size);
    rc = read(context->output_handle, response, response_buf_size);
    if (rc < 0) {
      printf("read failed with %i: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
      return 1;
    }
    else {
      if (response[0] == '?') {
      }
      if (response[0] == '=' || response[0] == '?') {
        strncpy(response, &response[2], strlen(response));
      }
    }
  }
  return 0;
}

canonical_original_code:
int get_response(char *command, char *response, unsigned long response_buf_size, struct go_controller_context *context) {
  long rc;
  int err;
  err = send_command(command, context);
  if (err != 0)
    return err;
  rc = wait_for_activity(context->output_handle);
  if (rc < 0)
    return rc;
  else if (rc == 0) {
    printf("wait_for_activity returned zero\n");
  }
  else {
    memset(response, 0, response_buf_size);
    rc = read(context->output_handle, response, response_buf_size);
    if (rc < 0) {
      printf("read failed with %i: %s\n", (*__errno_location ()), strerror((*__errno_location ())));
      return 1;
    }
    else {
      if (response[0] == '?') {
      }
      if (response[0] == '=' || response[0] == '?') {
        strncpy(response, &response[2], strlen(response));
      }
    }
  }
  return 0;
}

variable_types:
response
char *

variable_types: rc long


decompiled_code:
__int64 __fastcall go_controller_genmove(int a1, char *a2, size_t a3, int *a4)
{
__int64 result; // rax
char *v5; // rdi

if ( a1 )
{
result = 1LL;
if ( a1 != 1 )
return result;
v5 = "genmove white";
}
else
{
v5 = "genmove black";
}
return get_response(v5, a2, a3, a4);
}

canonical_decompiled_code:
__int64 __fastcall func15(int a1, char *a2, size_t a3, int *a4)
{
__int64 result; // rax
char *v5; // rdi

if ( a1 )
{
result = 1LL;
if ( a1 != 1 )
return result;
v5 = "genmove white";
}
else
{
v5 = "genmove black";
}
return func14(v5, a2, a3, a4);
}

original_code:
int go_controller_genmove(go_controller_colour colour, char* move, size_t move_size, go_controller_context* context) {
  char* command;
  int err;
  switch (colour) {
    case BLACK:
      command = "genmove black";
      break;
    case WHITE:
      command = "genmove white";
      break;
    default:
      return 1;
  }
  err = get_response(command, move, move_size, context);
  return err;
}

canonical_original_code:
int go_controller_genmove(enum go_controller_colour colour, char *move, unsigned long move_size, struct go_controller_context *context) {
  char *command;
  int err;
  switch (colour) {
    case BLACK:
      command = "genmove black";
      break;
    case WHITE:
      command = "genmove white";
      break;
    default:
      return 1;
  }
  err = get_response(command, move, move_size, context);
  return err;
}

variable_types: enum go_controller_colour { BLACK=0, WHITE=1 }

variable_types:
move
char *


decompiled_code:
__int64 __fastcall go_controller_play(int a1, const char *a2, int *a3)
{
char v5[128]; // [rsp+0h] [rbp-108h] BYREF
char dest[136]; // [rsp+80h] [rbp-88h] BYREF

if ( a1 )
{
if ( a1 == 1 )
strcpy(dest, "play white ");
}
else
{
strcpy(dest, "play black ");
}
strcat(dest, a2);
return get_response(dest, v5, 0x80uLL, a3);
}

canonical_decompiled_code:
__int64 __fastcall func16(int a1, const char *a2, int *a3)
{
char v5[128]; // [rsp+0h] [rbp-108h] BYREF
char dest[136]; // [rsp+80h] [rbp-88h] BYREF

if ( a1 )
{
if ( a1 == 1 )
strcpy(dest, "play white ");
}
else
{
strcpy(dest, "play black ");
}
strcat(dest, a2);
return func14(dest, v5, 0x80uLL, a3);
}

original_code:
int go_controller_play(go_controller_colour colour, char* move, go_controller_context* context) {
  const size_t buf_size = 128;
  char response_buf[buf_size];
  char command[buf_size];
  int err;
  switch (colour) {
    case BLACK:
      strcpy(command, "play black ");
      break;
    case WHITE:
      strcpy(command, "play white ");
      break;
  }
  strcat(command, move);
  err = get_response(command, response_buf, buf_size, context);
  return err;
}

canonical_original_code:
int go_controller_play(enum go_controller_colour colour, char *move, struct go_controller_context *context) {
  unsigned long buf_size = 128;
  char response_buf[-1];
  char command[-1];
  int err;
  switch (colour) {
    case BLACK:
      strcpy(command, "play black ");
      break;
    case WHITE:
      strcpy(command, "play white ");
      break;
  }
  strcat(command, move);
  err = get_response(command, response_buf, buf_size, context);
  return err;
}

variable_types:
command
char [-1]

user_defined_types: enum go_controller_colour { BLACK=0, WHITE=1 }