decompiled_code:
__int64 __fastcall slq_dequeue(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rdx

if ( !(unsigned int)slq_ready((unsigned int *)a1) )
return 0LL;
result = *(_QWORD *)(a1 + 16);
v2 = *(_QWORD *)(result + 24);
*(_QWORD *)(a1 + 16) = v2;
if ( !v2 )
*(_QWORD *)(a1 + 24) = 0LL;
*(_QWORD *)(result + 24) = 0LL;
--*(_DWORD *)(a1 + 4);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
__int64 result; // rax
__int64 v2; // rdx

if ( !(unsigned int)func11((unsigned int *)a1) )
return 0LL;
result = *(_QWORD *)(a1 + 16);
v2 = *(_QWORD *)(result + 24);
*(_QWORD *)(a1 + 16) = v2;
if ( !v2 )
*(_QWORD *)(a1 + 24) = 0LL;
*(_QWORD *)(result + 24) = 0LL;
--*(_DWORD *)(a1 + 4);
return result;
}

original_code:
SLQline* slq_dequeue(SLQueue *slq)
{
 SLQline *slql;
 if (slq_ready(slq) == FALSE)
 {
  return ((void *)0);
 }
 slql = slq->tail;
 slq->tail = slq->tail->next;
 if (slq->tail == ((void *)0))
 {
  slq->head = ((void *)0);
 }
 slql->next = ((void *)0);
 slq->count--;
 return slql;
}

canonical_original_code:
struct SLQline_s *slq_dequeue(struct SLQueue_s *slq)
{
 struct SLQline_s *slql;
 if (slq_ready(slq) == FALSE)
 {
  return ((void *)0);
 }
 slql = slq->tail;
 slq->tail = slq->tail->next;
 if (slq->tail == ((void *)0))
 {
  slq->head = ((void *)0);
 }
 slql->next = ((void *)0);
 slq->count--;
 return slql;
}

variable_types: struct SLQueue_s { int finished; int count; int span; struct SLQline_s *tail; struct SLQline_s *head; } *

return_type: struct SLQline_s { int slid; int dim; int width; struct Symbol_s { unsigned short dim; float vec[1]; } **line; struct SLQline_s *next; } *


repo: psilord/manifold

decompiled_code:
unsigned __int16 *__fastcall input_init(unsigned __int16 a1)
{
unsigned __int16 *v1; // r12
const unsigned __int16 **v2; // r14
const unsigned __int16 *v3; // rax
int v4; // er13
__int64 v5; // rbx
int v6; // er15
int i; // ebp
__int64 v8; // rax
float v9; // xmm0_4

v1 = (unsigned __int16 *)xmalloc(4104LL);
*(_QWORD *)v1 = a1;
v2 = _ctype_b_loc();
v3 = *v2;
v4 = 0;
do
v4 -= ((*v3++ & 0x4000) == 0) - 1;
while ( v3 != *v2 + 256 );
v5 = 0LL;
v6 = 0;
for ( i = 0; i != 256; ++i )
{
LOBYTE(v1[2 * v5 + 4]) = i;
if ( ((*v2)[v5] & 0x4000) != 0 )
{
v8 = symbol_init(*v1);
*(_QWORD *)&v1[2 * v5 + 8] = v8;
v9 = ((float)v6 + 1.0) / (float)v4;
symbol_set_all(v8, v9);
++*((_DWORD *)v1 + 1);
++v6;
}
else
{
*(_QWORD *)&v1[2 * v5 + 8] = 0LL;
}
++v5;
}
printf("Initialized %d printable characters.\n", *((unsigned int *)v1 + 1));
return v1;
}

canonical_decompiled_code:
unsigned __int16 *__fastcall func0(unsigned __int16 a1)
{
unsigned __int16 *v1; // r12
const unsigned __int16 **v2; // r14
const unsigned __int16 *v3; // rax
int v4; // er13
__int64 v5; // rbx
int v6; // er15
int i; // ebp
__int64 v8; // rax
float v9; // xmm0_4

v1 = (unsigned __int16 *)xmalloc(4104LL);
*(_QWORD *)v1 = a1;
v2 = _ctype_b_loc();
v3 = *v2;
v4 = 0;
do
v4 -= ((*v3++ & 0x4000) == 0) - 1;
while ( v3 != *v2 + 256 );
v5 = 0LL;
v6 = 0;
for ( i = 0; i != 256; ++i )
{
LOBYTE(v1[2 * v5 + 4]) = i;
if ( ((*v2)[v5] & 0x4000) != 0 )
{
v8 = symbol_init(*v1);
*(_QWORD *)&v1[2 * v5 + 8] = v8;
v9 = ((float)v6 + 1.0) / (float)v4;
symbol_set_all(v8, v9);
++*((_DWORD *)v1 + 1);
++v6;
}
else
{
*(_QWORD *)&v1[2 * v5 + 8] = 0LL;
}
++v5;
}
printf("Initialized %d printable characters.\n", *((unsigned int *)v1 + 1));
return v1;
}

original_code:
Input* input_init(unsigned short dimension)
{
 int i;
 Input *inp;
 int sum;
 int count;
 inp = (Input*)xmalloc(sizeof(Input) * 1);
 inp->dim = dimension;
 inp->printable = 0;
 sum = 0;
 for(i = 0; i < 256; i++)
 {
  if (((*__ctype_b_loc ())[(int) ((i))] & (unsigned short int) _ISprint))
  {
   sum++;
  }
 }
 count = 0;
 for(i = 0; i < 256; i++)
 {
  inp->xlt[i].c = i;
  if (((*__ctype_b_loc ())[(int) ((i))] & (unsigned short int) _ISprint))
  {
   inp->xlt[i].sym = symbol_init(inp->dim);
   symbol_set_all(inp->xlt[i].sym,
    ((float)count+1.0) / (float)sum);
   inp->printable++;
   count++;
  }
  else
  {
   inp->xlt[i].sym = ((void *)0);
  }
 }
 printf("Initialized %d printable characters.\n", inp->printable);
 return inp;
}

canonical_original_code:
struct Input_s *input_init(unsigned short dimension)
{
 int i;
 struct Input_s *inp;
 int sum;
 int count;
 inp = (struct Input_s *)xmalloc(sizeof(Input) * 1);
 inp->dim = dimension;
 inp->printable = 0;
 sum = 0;
 for(i = 0; i < 256; i++)
 {
  if (((*__ctype_b_loc ())[(int) ((i))] & (unsigned short) _ISprint))
  {
   sum++;
  }
 }
 count = 0;
 for(i = 0; i < 256; i++)
 {
  inp->xlt[i].c = i;
  if (((*__ctype_b_loc ())[(int) ((i))] & (unsigned short) _ISprint))
  {
   inp->xlt[i].sym = symbol_init(inp->dim);
   symbol_set_all(inp->xlt[i].sym,
    ((float)count+1.0) / (float)sum);
   inp->printable++;
   count++;
  }
  else
  {
   inp->xlt[i].sym = ((void *)0);
  }
 }
 printf("Initialized %d printable characters.\n", inp->printable);
 return inp;
}

variable_types:
dimension
unsigned short

variable_types: i int

variable_types: struct Input_s { int dim; int printable; struct XlateTable_s { char c; struct Symbol_s { unsigned short dim; float vec[1]; } *sym; } xlt[256]; } *

variable_types: sum int

variable_types: count int

user_defined_types: struct Input_s { int dim; int printable; struct XlateTable_s xlt[256]; }

user_defined_types: struct XlateTable_s { char c; struct Symbol_s *sym; }


decompiled_code:
__int64 __fastcall input_random_choice(__int64 a1)
{
int v2; // edi
__int64 v3; // rcx
__int64 *v4; // rax
int v5; // edx
float v7; // [rsp+Ch] [rbp-Ch]

v7 = (float)*(int *)(a1 + 4);
v2 = (int)((float)((float)rand() * v7) * 4.656612873077393e-10);
if ( v2 < 0 || *(_DWORD *)(a1 + 4) <= v2 )
{
printf(
"input_random_choice(): What!?! Why isn't %d in the range of 0 to %d\n",
(unsigned int)v2,
*(unsigned int *)(a1 + 4));
exit(1);
}
v3 = *(_QWORD *)(a1 + 16);
if ( v2 || !v3 )
{
v4 = (__int64 *)(a1 + 32);
v5 = 0;
while ( 1 )
{
v5 -= (v3 == 0) - 1;
if ( v4 == (__int64 *)(a1 + 4112) )
break;
v3 = *v4;
v4 += 2;
if ( v5 == v2 && v3 )
return v3;
}
puts("input_random_choice(): never supposed to return NULL!");
fflush(0LL);
v3 = 0LL;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
int v2; // edi
__int64 v3; // rcx
__int64 *v4; // rax
int v5; // edx
float v7; // [rsp+Ch] [rbp-Ch]

v7 = (float)*(int *)(a1 + 4);
v2 = (int)((float)((float)rand() * v7) * 4.656612873077393e-10);
if ( v2 < 0 || *(_DWORD *)(a1 + 4) <= v2 )
{
printf(
"input_random_choice(): What!?! Why isn't %d in the range of 0 to %d\n",
(unsigned int)v2,
*(unsigned int *)(a1 + 4));
exit(1);
}
v3 = *(_QWORD *)(a1 + 16);
if ( v2 || !v3 )
{
v4 = (__int64 *)(a1 + 32);
v5 = 0;
while ( 1 )
{
v5 -= (v3 == 0) - 1;
if ( v4 == (__int64 *)(a1 + 4112) )
break;
v3 = *v4;
v4 += 2;
if ( v5 == v2 && v3 )
return v3;
}
puts("input_random_choice(): never supposed to return NULL!");
fflush(0LL);
v3 = 0LL;
}
return v3;
}

original_code:
Symbol* input_random_choice(Input *inp)
{
 int i, count;
 int choice;
 choice = (int)(((float)inp->printable*rand())/(2147483647 +1.0));
 if (choice < 0 || choice >= inp->printable)
 {
  printf("input_random_choice(): What!?! Why isn't %d in the "
   "range of 0 to %d\n", choice, inp->printable);
  exit(1);
 }
 count = 0;
 for (i = 0; i < 256; i++)
 {
  if (inp->xlt[i].sym != ((void *)0) && count == choice)
  {
   return inp->xlt[i].sym;
  }
  if (inp->xlt[i].sym != ((void *)0))
  {
   count++;
  }
 }
 printf("input_random_choice(): never supposed to return NULL!\n");
 fflush(((void *)0));
 return ((void *)0);
}

canonical_original_code:
struct Symbol_s *input_random_choice(struct Input_s *inp)
{
 int i; int count;
 int choice;
 choice = (int)(((float)inp->printable*rand())/(2147483647 +1.0));
 if (choice < 0 || choice >= inp->printable)
 {
  printf("input_random_choice(): What!?! Why isn't %d in the "
   "range of 0 to %d\n", choice, inp->printable);
  exit(1);
 }
 count = 0;
 for (i = 0; i < 256; i++)
 {
  if (inp->xlt[i].sym != ((void *)0) && count == choice)
  {
   return inp->xlt[i].sym;
  }
  if (inp->xlt[i].sym != ((void *)0))
  {
   count++;
  }
 }
 printf("input_random_choice(): never supposed to return NULL!\n");
 fflush(((void *)0));
 return ((void *)0);
}

variable_types: struct Input_s { int dim; int printable; struct XlateTable_s { char c; struct Symbol_s *sym; } xlt[256]; } *

variable_types: choice int

return_type: struct Symbol_s { unsigned short dim; float vec[1]; } *


decompiled_code:
__int64 __fastcall input_random_choice_special(__int64 a1, int a2)
{
__int64 v3; // rsi

if ( a2 > 25 )
{
puts("input_random_choice_special(): num must be less than 26!");
exit(1);
}
v3 = (unsigned int)(int)((float)((float)a2 * (float)rand()) * 4.656612873077393e-10);
if ( (int)v3 < 0 || a2 <= (int)v3 )
{
printf("input_random_choice(): What!?! Why isn't %d in the range of 0 to %d\n", v3, (unsigned int)a2);
exit(1);
}
return *(_QWORD *)(a1 + 16LL * ((int)v3 + 97) + 16);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2)
{
__int64 v3; // rsi

if ( a2 > 25 )
{
puts("input_random_choice_special(): num must be less than 26!");
exit(1);
}
v3 = (unsigned int)(int)((float)((float)a2 * (float)rand()) * 4.656612873077393e-10);
if ( (int)v3 < 0 || a2 <= (int)v3 )
{
printf("input_random_choice(): What!?! Why isn't %d in the range of 0 to %d\n", v3, (unsigned int)a2);
exit(1);
}
return *(_QWORD *)(a1 + 16LL * ((int)v3 + 97) + 16);
}

original_code:
Symbol* input_random_choice_special(Input *inp, int num)
{
 int choice;
 if (num >= 26)
 {
  printf("input_random_choice_special(): num must be less than 26!\n");
  exit(1);
 }
 choice = (int)(((float)num*rand())/(2147483647 +1.0));
 if (choice < 0 || choice >= num)
 {
  printf("input_random_choice(): What!?! Why isn't %d in the "
   "range of 0 to %d\n", choice, num);
  exit(1);
 }
 return inp->xlt[((int)'a') + choice].sym;
}

canonical_original_code:
struct Symbol_s *input_random_choice_special(struct Input_s *inp, int num)
{
 int choice;
 if (num >= 26)
 {
  printf("input_random_choice_special(): num must be less than 26!\n");
  exit(1);
 }
 choice = (int)(((float)num*rand())/(2147483647 +1.0));
 if (choice < 0 || choice >= num)
 {
  printf("input_random_choice(): What!?! Why isn't %d in the "
   "range of 0 to %d\n", choice, num);
  exit(1);
 }
 return inp->xlt[((int)'a') + choice].sym;
}

variable_types: num int

user_defined_types: struct Symbol_s { unsigned short dim; float vec[1]; }


decompiled_code:
__int64 __fastcall input_xlate_char(__int64 a1, char a2)
{
__int64 result; // rax

result = *(_QWORD *)(16LL * a2 + a1 + 16);
if ( !result )
{
printf("input_xlate_char(): passed a nonprintable character: %c!\n", (unsigned int)a2);
exit(1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, char a2)
{
__int64 result; // rax

result = *(_QWORD *)(16LL * a2 + a1 + 16);
if ( !result )
{
printf("input_xlate_char(): passed a nonprintable character: %c!\n", (unsigned int)a2);
exit(1);
}
return result;
}

original_code:
Symbol* input_xlate_char(Input *inp, char c)
{
 if (inp->xlt[(int)c].sym == ((void *)0))
 {
  printf("input_xlate_char(): passed a nonprintable character: %c!\n", c);
  exit(1);
 }
 return inp->xlt[(int)c].sym;
}

canonical_original_code:
struct Symbol_s *input_xlate_char(struct Input_s *inp, char c)
{
 if (inp->xlt[(int)c].sym == ((void *)0))
 {
  printf("input_xlate_char(): passed a nonprintable character: %c!\n", c);
  exit(1);
 }
 return inp->xlt[(int)c].sym;
}

variable_types: c char


original_code:
Symbol* input_decode_to_sym(Input *inp, Symbol *unk, double *error)
{
 double min_dist = 99999, max_dist = 0;
 double dist;
 int i;
 int loc = 0;
 for (i = 0; i < 256; i++)
 {
  if (inp->xlt[i].sym != ((void *)0))
  {
   dist = symbol_dist(unk, inp->xlt[i].sym);
   if (dist < min_dist)
   {
    min_dist = dist;
    loc = i;
   }
   if (dist > max_dist)
   {
    max_dist = dist;
   }
  }
 }
 *error = 1.0 - ((max_dist - min_dist) / max_dist);
 return inp->xlt[loc].sym;
}

canonical_original_code:
struct Symbol_s *input_decode_to_sym(struct Input_s *inp, struct Symbol_s *unk, double *error)
{
 double min_dist = 99999; double max_dist = 0;
 double dist;
 int i;
 int loc = 0;
 for (i = 0; i < 256; i++)
 {
  if (inp->xlt[i].sym != ((void *)0))
  {
   dist = symbol_dist(unk, inp->xlt[i].sym);
   if (dist < min_dist)
   {
    min_dist = dist;
    loc = i;
   }
   if (dist > max_dist)
   {
    max_dist = dist;
   }
  }
 }
 *error = 1.0 - ((max_dist - min_dist) / max_dist);
 return inp->xlt[loc].sym;
}

variable_types:
unk
struct Symbol_s *

variable_types:
error
double *

variable_types: dist double

variable_types: loc int


original_code:
char input_decode_to_char(Input *inp, Symbol *unk, double *error)
{
 double min_dist = 99999, max_dist = 0;
 double dist;
 int i;
 int loc = 0;
 for (i = 0; i < 256; i++)
 {
  if (inp->xlt[i].sym != ((void *)0))
  {
   dist = symbol_dist(unk, inp->xlt[i].sym);
   if (dist < min_dist)
   {
    min_dist = dist;
    loc = i;
   }
   if (dist > max_dist)
   {
    max_dist = dist;
   }
  }
 }
 *error = 1.0 - ((max_dist - min_dist) / max_dist);
 return inp->xlt[loc].c;
}

canonical_original_code:
char input_decode_to_char(struct Input_s *inp, struct Symbol_s *unk, double *error)
{
 double min_dist = 99999; double max_dist = 0;
 double dist;
 int i;
 int loc = 0;
 for (i = 0; i < 256; i++)
 {
  if (inp->xlt[i].sym != ((void *)0))
  {
   dist = symbol_dist(unk, inp->xlt[i].sym);
   if (dist < min_dist)
   {
    min_dist = dist;
    loc = i;
   }
   if (dist > max_dist)
   {
    max_dist = dist;
   }
  }
 }
 *error = 1.0 - ((max_dist - min_dist) / max_dist);
 return inp->xlt[loc].c;
}

return_type: char


decompiled_code:
void __fastcall input_free(void *ptr)
{
_QWORD *v1; // rbx

v1 = (char *)ptr + 16;
do
{
if ( *v1 )
symbol_free();
v1 += 2;
}
while ( v1 != (_QWORD *)((char *)ptr + 4112) );
free(ptr);
}

canonical_decompiled_code:
void __fastcall func6(void *ptr)
{
_QWORD *v1; // rbx

v1 = (char *)ptr + 16;
do
{
if ( *v1 )
symbol_free();
v1 += 2;
}
while ( v1 != (_QWORD *)((char *)ptr + 4112) );
free(ptr);
}

original_code:
void input_free(Input *inp)
{
 int i;
 for(i = 0; i < 256; i++)
 {
  if (inp->xlt[i].sym != ((void *)0))
  {
   symbol_free(inp->xlt[i].sym);
  }
 }
 free(inp);
}

canonical_original_code:
void input_free(struct Input_s *inp)
{
 int i;
 for(i = 0; i < 256; i++)
 {
  if (inp->xlt[i].sym != ((void *)0))
  {
   symbol_free(inp->xlt[i].sym);
  }
 }
 free(inp);
}

return_type: void


xmalloc

decompiled_code:
void *__fastcall xmalloc(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
{
puts("Out of memory!");
exit(1);
}
return result;
}

canonical_decompiled_code:
void *__fastcall func0(size_t a1)
{
void *result; // rax

result = malloc(a1);
if ( !result )
{
puts("Out of memory!");
exit(1);
}
return result;
}

original_code:
void* xmalloc(unsigned long size)
{
 void *space;
 space = malloc(size);
 if (space == ((void *)0))
 {
  printf("Out of memory!\n");
  exit(1);
 }
 return space;
}

canonical_original_code:
void *xmalloc(unsigned long size)
{
 void *space;
 space = malloc(size);
 if (space == ((void *)0))
 {
  printf("Out of memory!\n");
  exit(1);
 }
 return space;
}

variable_types:
size
unsigned long

variable_types:
space
void *

return_type: void *


readn

decompiled_code:
size_t __fastcall readn(int fd, void *buf, size_t nbytes)
{
size_t v3; // r13
size_t v5; // rbx
ssize_t v6; // rax

v3 = nbytes;
if ( nbytes )
{
v5 = nbytes;
while ( 1 )
{
v6 = read(fd, buf, v5);
if ( v6 < 0 )
{
if ( *_errno_location() != 4 )
return -1LL;
v6 = 0LL;
}
else if ( !v6 )
{
return v3 - v5;
}
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3 - v5;
}
}
v5 = 0LL;
return v3 - v5;
}

canonical_decompiled_code:
size_t __fastcall func1(int fd, void *buf, size_t nbytes)
{
size_t v3; // r13
size_t v5; // rbx
ssize_t v6; // rax

v3 = nbytes;
if ( nbytes )
{
v5 = nbytes;
while ( 1 )
{
v6 = read(fd, buf, v5);
if ( v6 < 0 )
{
if ( *_errno_location() != 4 )
return -1LL;
v6 = 0LL;
}
else if ( !v6 )
{
return v3 - v5;
}
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3 - v5;
}
}
v5 = 0LL;
return v3 - v5;
}

original_code:
ssize_t readn(int fd, void *vptr, size_t n)
{
 size_t nleft;
 ssize_t nread;
 char *ptr = ((void *)0);
 ptr = vptr;
 nleft = n;
 while (nleft > 0) {
  if ((nread = read(fd, ptr, nleft)) < 0) {
   if ((*__errno_location ()) == 4) {
    nread = 0;
   } else {
    return -1;
   }
  } else if (nread == 0) {
   break;
  }
  nleft -= nread;
  ptr += nread;
 }
 return n - nleft;
}

canonical_original_code:
long readn(int fd, void *vptr, unsigned long n)
{
 unsigned long nleft;
 long nread;
 char *ptr = ((void *)0);
 ptr = vptr;
 nleft = n;
 while (nleft > 0) {
  if ((nread = read(fd, ptr, nleft)) < 0) {
   if ((*__errno_location ()) == 4) {
    nread = 0;
   } else {
    return -1;
   }
  } else if (nread == 0) {
   break;
  }
  nleft -= nread;
  ptr += nread;
 }
 return n - nleft;
}

variable_types: fd int

variable_types:
vptr
void *

variable_types:
n
unsigned long

variable_types:
nleft
unsigned long

variable_types: nread long

variable_types:
ptr
char *

return_type: long


original_code:
Conv* conv_init(int style, double param)
{
 int i, j;
 Conv *c = ((void *)0);
 c = (Conv*)xmalloc(sizeof(Conv) * 1);
 switch(style)
 {
  case CONV_BLUR_SMEAR:
   c->desc = "All Edge Sensitive";
   c->type = style;
   c->rows = 3;
   c->cols = 3;
   c->coef = (double*)xmalloc(sizeof(double) *
       (c->rows*c->cols));
   for (i = 0; i < c->rows; i++)
   {
    for (j = 0; j < c->cols; j++)
    {
     c->coef[(((i) * ((c)->cols)) + (j))] = param;
    }
   }
   c->coef[(((c->rows / 2) * ((c)->cols)) + (c->cols / 2))] = 1;
   break;
  case CONV_BLUR_LIGHT:
   c->desc = "Simple Blur";
   c->type = style;
   c->rows = 3;
   c->cols = 3;
   c->coef = (double*)xmalloc(sizeof(double) *
       (c->rows*c->cols));
   c->coef[(((0) * ((c)->cols)) + (0))] = param / sqrt(2.0);
   c->coef[(((0) * ((c)->cols)) + (1))] = param;
   c->coef[(((0) * ((c)->cols)) + (2))] = param / sqrt(2.0);
   c->coef[(((1) * ((c)->cols)) + (0))] = param;
   c->coef[(((1) * ((c)->cols)) + (1))] = param;
   c->coef[(((1) * ((c)->cols)) + (2))] = param;
   c->coef[(((2) * ((c)->cols)) + (0))] = param / sqrt(2.0);
   c->coef[(((2) * ((c)->cols)) + (1))] = param;
   c->coef[(((2) * ((c)->cols)) + (2))] = param / sqrt(2.0);
   break;
  case CONV_ENHANCED_DETAIL:
   c->desc = "Enhanced Detail";
   c->type = style;
   c->rows = 3;
   c->cols = 3;
   c->coef = (double*)xmalloc(sizeof(double) *
       (c->rows*c->cols));
   c->coef[(((0) * ((c)->cols)) + (0))] = 0;
   c->coef[(((0) * ((c)->cols)) + (1))] = -1;
   c->coef[(((0) * ((c)->cols)) + (2))] = 0;
   c->coef[(((1) * ((c)->cols)) + (0))] = -1;
   c->coef[(((1) * ((c)->cols)) + (1))] = param;
   c->coef[(((1) * ((c)->cols)) + (2))] = -1;
   c->coef[(((2) * ((c)->cols)) + (0))] = 0;
   c->coef[(((2) * ((c)->cols)) + (1))] = -1;
   c->coef[(((2) * ((c)->cols)) + (2))] = 0;
   break;
  case CONV_SOFTEN:
   c->desc = "Soften";
   c->type = style;
   c->rows = 3;
   c->cols = 3;
   c->coef = (double*)xmalloc(sizeof(double) *
       (c->rows*c->cols));
   c->coef[(((0) * ((c)->cols)) + (0))] = param;
   c->coef[(((0) * ((c)->cols)) + (1))] = param;
   c->coef[(((0) * ((c)->cols)) + (2))] = param;
   c->coef[(((1) * ((c)->cols)) + (0))] = param;
   c->coef[(((1) * ((c)->cols)) + (1))] = param;
   c->coef[(((1) * ((c)->cols)) + (2))] = param;
   c->coef[(((2) * ((c)->cols)) + (0))] = param;
   c->coef[(((2) * ((c)->cols)) + (1))] = param;
   c->coef[(((2) * ((c)->cols)) + (2))] = param;
   break;
  case CONV_LAPLACIAN:
   c->desc = "Laplacian";
   c->type = style;
   c->rows = 3;
   c->cols = 3;
   c->coef = (double*)xmalloc(sizeof(double) *
       (c->rows*c->cols));
   c->coef[(((0) * ((c)->cols)) + (0))] = -1;
   c->coef[(((0) * ((c)->cols)) + (1))] = -1;
   c->coef[(((0) * ((c)->cols)) + (2))] = -1;
   c->coef[(((1) * ((c)->cols)) + (0))] = -1;
   c->coef[(((1) * ((c)->cols)) + (1))] = param;
   c->coef[(((1) * ((c)->cols)) + (2))] = -1;
   c->coef[(((2) * ((c)->cols)) + (0))] = -1;
   c->coef[(((2) * ((c)->cols)) + (1))] = -1;
   c->coef[(((2) * ((c)->cols)) + (2))] = -1;
   break;
  case CONV_USER_DEF:
   c->desc = "User Defined";
   c->type = style;
   c->rows = 3;
   c->cols = 3;
   c->coef = (double*)xmalloc(sizeof(double) *
       (c->rows*c->cols));
   c->coef[(((0) * ((c)->cols)) + (0))] = 0;
   c->coef[(((0) * ((c)->cols)) + (1))] = 0;
   c->coef[(((0) * ((c)->cols)) + (2))] = 0;
   c->coef[(((1) * ((c)->cols)) + (0))] = 0;
   c->coef[(((1) * ((c)->cols)) + (1))] = 1;
   c->coef[(((1) * ((c)->cols)) + (2))] = 0;
   c->coef[(((2) * ((c)->cols)) + (0))] = 0;
   c->coef[(((2) * ((c)->cols)) + (1))] = 0;
   c->coef[(((2) * ((c)->cols)) + (2))] = 0;
   break;
  default:
   printf("convolution_init(): Unknown convolution style!\n");
   exit(1);
   break;
 };
 return c;
}

canonical_original_code:
struct Conv_s *conv_init(int style, double param)
{
 int i; int j;
 struct Conv_s *c = ((void *)0);
 c = (struct Conv_s *)xmalloc(sizeof(Conv) * 1);
 switch(style)
 {
  case CONV_BLUR_SMEAR:
   c->desc = "All Edge Sensitive";
   c->type = style;
   c->rows = 3;
   c->cols = 3;
   c->coef = (double *)xmalloc(sizeof(double) *
       (c->rows*c->cols));
   for (i = 0; i < c->rows; i++)
   {
    for (j = 0; j < c->cols; j++)
    {
     c->coef[(((i) * ((c)->cols)) + (j))] = param;
    }
   }
   c->coef[(((c->rows / 2) * ((c)->cols)) + (c->cols / 2))] = 1;
   break;
  case CONV_BLUR_LIGHT:
   c->desc = "Simple Blur";
   c->type = style;
   c->rows = 3;
   c->cols = 3;
   c->coef = (double *)xmalloc(sizeof(double) *
       (c->rows*c->cols));
   c->coef[(((0) * ((c)->cols)) + (0))] = param / sqrt(2.0);
   c->coef[(((0) * ((c)->cols)) + (1))] = param;
   c->coef[(((0) * ((c)->cols)) + (2))] = param / sqrt(2.0);
   c->coef[(((1) * ((c)->cols)) + (0))] = param;
   c->coef[(((1) * ((c)->cols)) + (1))] = param;
   c->coef[(((1) * ((c)->cols)) + (2))] = param;
   c->coef[(((2) * ((c)->cols)) + (0))] = param / sqrt(2.0);
   c->coef[(((2) * ((c)->cols)) + (1))] = param;
   c->coef[(((2) * ((c)->cols)) + (2))] = param / sqrt(2.0);
   break;
  case CONV_ENHANCED_DETAIL:
   c->desc = "Enhanced Detail";
   c->type = style;
   c->rows = 3;
   c->cols = 3;
   c->coef = (double *)xmalloc(sizeof(double) *
       (c->rows*c->cols));
   c->coef[(((0) * ((c)->cols)) + (0))] = 0;
   c->coef[(((0) * ((c)->cols)) + (1))] = -1;
   c->coef[(((0) * ((c)->cols)) + (2))] = 0;
   c->coef[(((1) * ((c)->cols)) + (0))] = -1;
   c->coef[(((1) * ((c)->cols)) + (1))] = param;
   c->coef[(((1) * ((c)->cols)) + (2))] = -1;
   c->coef[(((2) * ((c)->cols)) + (0))] = 0;
   c->coef[(((2) * ((c)->cols)) + (1))] = -1;
   c->coef[(((2) * ((c)->cols)) + (2))] = 0;
   break;
  case CONV_SOFTEN:
   c->desc = "Soften";
   c->type = style;
   c->rows = 3;
   c->cols = 3;
   c->coef = (double *)xmalloc(sizeof(double) *
       (c->rows*c->cols));
   c->coef[(((0) * ((c)->cols)) + (0))] = param;
   c->coef[(((0) * ((c)->cols)) + (1))] = param;
   c->coef[(((0) * ((c)->cols)) + (2))] = param;
   c->coef[(((1) * ((c)->cols)) + (0))] = param;
   c->coef[(((1) * ((c)->cols)) + (1))] = param;
   c->coef[(((1) * ((c)->cols)) + (2))] = param;
   c->coef[(((2) * ((c)->cols)) + (0))] = param;
   c->coef[(((2) * ((c)->cols)) + (1))] = param;
   c->coef[(((2) * ((c)->cols)) + (2))] = param;
   break;
  case CONV_LAPLACIAN:
   c->desc = "Laplacian";
   c->type = style;
   c->rows = 3;
   c->cols = 3;
   c->coef = (double *)xmalloc(sizeof(double) *
       (c->rows*c->cols));
   c->coef[(((0) * ((c)->cols)) + (0))] = -1;
   c->coef[(((0) * ((c)->cols)) + (1))] = -1;
   c->coef[(((0) * ((c)->cols)) + (2))] = -1;
   c->coef[(((1) * ((c)->cols)) + (0))] = -1;
   c->coef[(((1) * ((c)->cols)) + (1))] = param;
   c->coef[(((1) * ((c)->cols)) + (2))] = -1;
   c->coef[(((2) * ((c)->cols)) + (0))] = -1;
   c->coef[(((2) * ((c)->cols)) + (1))] = -1;
   c->coef[(((2) * ((c)->cols)) + (2))] = -1;
   break;
  case CONV_USER_DEF:
   c->desc = "User Defined";
   c->type = style;
   c->rows = 3;
   c->cols = 3;
   c->coef = (double *)xmalloc(sizeof(double) *
       (c->rows*c->cols));
   c->coef[(((0) * ((c)->cols)) + (0))] = 0;
   c->coef[(((0) * ((c)->cols)) + (1))] = 0;
   c->coef[(((0) * ((c)->cols)) + (2))] = 0;
   c->coef[(((1) * ((c)->cols)) + (0))] = 0;
   c->coef[(((1) * ((c)->cols)) + (1))] = 1;
   c->coef[(((1) * ((c)->cols)) + (2))] = 0;
   c->coef[(((2) * ((c)->cols)) + (0))] = 0;
   c->coef[(((2) * ((c)->cols)) + (1))] = 0;
   c->coef[(((2) * ((c)->cols)) + (2))] = 0;
   break;
  default:
   printf("convolution_init(): Unknown convolution style!\n");
   exit(1);
   break;
 };
 return c;
}

variable_types: style int

variable_types: param double

variable_types: i int

variable_types: j int

variable_types: struct Conv_s { char *desc; int type; int rows; double *coef; } *


decompiled_code:
void __fastcall conv_free(void *ptr)
{
free(*((void **)ptr + 3));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
free(*((void **)ptr + 3));
free(ptr);
}

original_code:
void conv_free(Conv *c)
{
 free(c->coef);
 free(c);
}

canonical_original_code:
void conv_free(struct Conv_s *c)
{
 free(c->coef);
 free(c);
}

user_defined_types: struct Conv_s { char *desc; int type; int rows; double *coef; }


decompiled_code:
double __fastcall conv_get_rows(__int64 a1)
{
return (double)*(int *)(a1 + 12);
}

canonical_decompiled_code:
double __fastcall func2(__int64 a1)
{
return (double)*(int *)(a1 + 12);
}

original_code:
double conv_get_rows(Conv *c)
{
 return c->rows;
}

canonical_original_code:
double conv_get_rows(struct Conv_s *c)
{
 return c->rows;
}

return_type: double


decompiled_code:
double __fastcall conv_get_cols(__int64 a1)
{
return (double)*(int *)(a1 + 16);
}

canonical_decompiled_code:
double __fastcall func3(__int64 a1)
{
return (double)*(int *)(a1 + 16);
}

original_code:
double conv_get_cols(Conv *c)
{
 return c->cols;
}

canonical_original_code:
double conv_get_cols(struct Conv_s *c)
{
 return c->cols;
}


decompiled_code:
double __fastcall conv_get_coef(int a1, int a2, __int64 a3)
{
return *(double *)(*(_QWORD *)(a3 + 24) + 8LL * (a2 + *(_DWORD *)(a3 + 16) * a1));
}

canonical_decompiled_code:
double __fastcall func4(int a1, int a2, __int64 a3)
{
return *(double *)(*(_QWORD *)(a3 + 24) + 8LL * (a2 + *(_DWORD *)(a3 + 16) * a1));
}

original_code:
double conv_get_coef(int row, int col, Conv *c)
{
 return c->coef[(((row) * ((c)->cols)) + (col))];
}

canonical_original_code:
double conv_get_coef(int row, int col, struct Conv_s *c)
{
 return c->coef[(((row) * ((c)->cols)) + (col))];
}

variable_types: row int

variable_types: col int


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

result = *(_QWORD *)(a3 + 24);
*(double *)(result + 8LL * (a2 + *(_DWORD *)(a3 + 16) * a1)) = a4;
return result;
}

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

result = *(_QWORD *)(a3 + 24);
*(double *)(result + 8LL * (a2 + *(_DWORD *)(a3 + 16) * a1)) = a4;
return result;
}

original_code:
void conv_set_coef(int row, int col, Conv *c, double param)
{
 c->coef[(((row) * ((c)->cols)) + (col))] = param;
}

canonical_original_code:
void conv_set_coef(int row, int col, struct Conv_s *c, double param)
{
 c->coef[(((row) * ((c)->cols)) + (col))] = param;
}


original_code:
void conv_apply(SOM *s, Conv *c, double thld)
{
 int row, col;
 Symbol *sum;
 Symbol *mul;
 Symbol *candidate;
 double divisor;
 SLQueue *slq;
 SLQline *slql;
 Symbol **data;
 int i;
 int slid;
 int xrow, xcol;
 int cr, cc;
 double coef;
 double dist;
 slq = slq_init(((int)(conv_get_rows(c) / 2.0)) + 1);
 sum = symbol_init(s->sd.dim);
 mul = symbol_init(s->sd.dim);
 for(row = 0; row < s->sd.rows; row++)
 {
  slql = slqline_init(row, s->sd.cols, s->sd.dim);
  data = slqline_line(slql);
  for (col = 0; col < s->sd.cols; col++)
  {
   symbol_one(mul);
   divisor = 0;
   symbol_zero(sum);
   for (cr = 0; cr < conv_get_rows(c); cr++)
   {
    for (cc = 0; cc < conv_get_cols(c); cc++)
    {
     xrow = row - (int)(conv_get_rows(c) / 2) + cr;
     xcol = col - (int)(conv_get_cols(c) / 2) + cc;
     if ((xrow >= 0 && xrow < s->sd.rows) &&
      (xcol >= 0 && xcol < s->sd.cols))
     {
      dist = symbol_dist(
       som_symbol_ref(s, row, col),
       som_symbol_ref(s, xrow, xcol));
      if (dist <= thld)
      {
       coef = conv_get_coef(cr, cc, c);
       symbol_move(mul, som_symbol_ref(s, xrow, xcol));
       symbol_mul(mul, coef);
       symbol_add(sum, sum, mul);
       divisor += coef;
      }
     }
    }
   }
   if ( !(fabs(divisor) < (1e-13)) )
   {
    symbol_div(sum, divisor);
   }
   symbol_move(data[col], sum);
  }
  slq_enqueue(slq, slql);
  slql = ((void *)0);
  data = ((void *)0);
  while(slq_ready(slq) == TRUE)
  {
   slql = slq_dequeue(slq);
   if (slql == ((void *)0))
   {
    printf("slql shouldn't be null here!\n");
    exit(1);
   }
   data = slqline_line(slql);
   slid = slqline_get_slid(slql);
   for (i = 0; i < slqline_get_width(slql); i++)
   {
    candidate = som_symbol_ref(s, slid, i);
    symbol_move(candidate, data[i]);
   }
   slqline_free(slql);
   slql = ((void *)0);
  }
 }
 slq_set_finished(slq, TRUE);
 while(slq_ready(slq) == TRUE)
 {
  slql = slq_dequeue(slq);
  if (slql == ((void *)0))
  {
   printf("slql shouldn't be null here either!\n");
   exit(1);
  }
  data = slqline_line(slql);
  slid = slqline_get_slid(slql);
  for (i = 0; i < slqline_get_width(slql); i++)
  {
   candidate = som_symbol_ref(s, slid, i);
   symbol_move(candidate, data[i]);
  }
  slqline_free(slql);
  slql = ((void *)0);
 }
 slq_free(slq);
 symbol_free(sum);
 symbol_free(mul);
}

canonical_original_code:
void conv_apply(struct SOM_s *s, struct Conv_s *c, double thld)
{
 int row; int col;
 struct Symbol_s *sum;
 struct Symbol_s *mul;
 struct Symbol_s *candidate;
 double divisor;
 struct SLQueue_s *slq;
 struct SLQline_s *slql;
 struct Symbol_s **data;
 int i;
 int slid;
 int xrow; int xcol;
 int cr; int cc;
 double coef;
 double dist;
 slq = slq_init(((int)(conv_get_rows(c) / 2.0)) + 1);
 sum = symbol_init(s->sd.dim);
 mul = symbol_init(s->sd.dim);
 for(row = 0; row < s->sd.rows; row++)
 {
  slql = slqline_init(row, s->sd.cols, s->sd.dim);
  data = slqline_line(slql);
  for (col = 0; col < s->sd.cols; col++)
  {
   symbol_one(mul);
   divisor = 0;
   symbol_zero(sum);
   for (cr = 0; cr < conv_get_rows(c); cr++)
   {
    for (cc = 0; cc < conv_get_cols(c); cc++)
    {
     xrow = row - (int)(conv_get_rows(c) / 2) + cr;
     xcol = col - (int)(conv_get_cols(c) / 2) + cc;
     if ((xrow >= 0 && xrow < s->sd.rows) &&
      (xcol >= 0 && xcol < s->sd.cols))
     {
      dist = symbol_dist(
       som_symbol_ref(s, row, col),
       som_symbol_ref(s, xrow, xcol));
      if (dist <= thld)
      {
       coef = conv_get_coef(cr, cc, c);
       symbol_move(mul, som_symbol_ref(s, xrow, xcol));
       symbol_mul(mul, coef);
       symbol_add(sum, sum, mul);
       divisor += coef;
      }
     }
    }
   }
   if ( !(fabs(divisor) < (1e-13)) )
   {
    symbol_div(sum, divisor);
   }
   symbol_move(data[col], sum);
  }
  slq_enqueue(slq, slql);
  slql = ((void *)0);
  data = ((void *)0);
  while(slq_ready(slq) == TRUE)
  {
   slql = slq_dequeue(slq);
   if (slql == ((void *)0))
   {
    printf("slql shouldn't be null here!\n");
    exit(1);
   }
   data = slqline_line(slql);
   slid = slqline_get_slid(slql);
   for (i = 0; i < slqline_get_width(slql); i++)
   {
    candidate = som_symbol_ref(s, slid, i);
    symbol_move(candidate, data[i]);
   }
   slqline_free(slql);
   slql = ((void *)0);
  }
 }
 slq_set_finished(slq, TRUE);
 while(slq_ready(slq) == TRUE)
 {
  slql = slq_dequeue(slq);
  if (slql == ((void *)0))
  {
   printf("slql shouldn't be null here either!\n");
   exit(1);
  }
  data = slqline_line(slql);
  slid = slqline_get_slid(slql);
  for (i = 0; i < slqline_get_width(slql); i++)
  {
   candidate = som_symbol_ref(s, slid, i);
   symbol_move(candidate, data[i]);
  }
  slqline_free(slql);
  slql = ((void *)0);
 }
 slq_free(slq);
 symbol_free(sum);
 symbol_free(mul);
}

variable_types: struct SOM_s { int mode; int bmu_row; struct SOMDesc_s { unsigned short dim; unsigned int train_iter; unsigned int rows; unsigned int cols; float (*radius_func)(struct SOM_s *s); } sd; int current_iter; float initial_radius; float half_life; struct Symbol_s { unsigned short dim; float vec[1]; } **neuron; float final_computation; float max_dist; float *qmap; } *

variable_types: thld double

variable_types: divisor double

variable_types: struct SLQueue_s { int finished; int count; int span; struct SLQline_s { int slid; int dim; int width; struct Symbol_s { unsigned short dim; float vec[1]; } **line; struct SLQline_s *next; } *tail; struct SLQline_s *head; } *

variable_types: slid int

variable_types: xrow int

variable_types: xcol int

variable_types: cr int

variable_types: cc int

variable_types: coef double

variable_types: dist double

user_defined_types: struct SOM_s { int mode; int bmu_row; struct SOMDesc_s sd; int current_iter; float initial_radius; float half_life; struct Symbol_s **neuron; float final_computation; float max_dist; float *qmap; }

user_defined_types: struct SLQueue_s { int finished; int count; int span; struct SLQline_s *tail; struct SLQline_s *head; }

user_defined_types: struct SLQline_s { int slid; int dim; int width; struct Symbol_s **line; struct SLQline_s *next; }


decompiled_code:
void __fastcall conv_apply_easy(__int64 a1, int a2, double a3, double a4)
{
void *v4; // rbx

v4 = (void *)conv_init(a2, a3);
conv_apply(a1, (__int64)v4, a4);
conv_free(v4);
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1, int a2, double a3, double a4)
{
void *v4; // rbx

v4 = (void *)func0(a2, a3);
func6(a1, (__int64)v4, a4);
func1(v4);
}

original_code:
void conv_apply_easy(SOM *som, int style, double param, double thld)
{
 Conv *c;
 c = conv_init(style, param);
 conv_apply(som, c, thld);
 conv_free(c);
}

canonical_original_code:
void conv_apply_easy(struct SOM_s *som, int style, double param, double thld)
{
 struct Conv_s *c;
 c = conv_init(style, param);
 conv_apply(som, c, thld);
 conv_free(c);
}

variable_types: style int

variable_types: param double

user_defined_types: struct SOMDesc_s { unsigned short dim; unsigned int train_iter; unsigned int rows; unsigned int cols; float (*radius_func)(struct SOM_s *s); }


original_code:
TuringMachine* turing_machine_init(int iterations)
{
 int i;
 TuringMachine *tmach = ((void *)0);
 tmach = (TuringMachine*)xmalloc(sizeof(TuringMachine) * 1);
 tmach->perceptions = xmalloc(sizeof(Symbol*) * 6);
 tmach->perceptions[0] = symbol_init(2);
 symbol_set_2(tmach->perceptions[0], 0.0, 1.0);
 tmach->perceptions[1] = symbol_init(2);
 symbol_set_2(tmach->perceptions[1], 0.0, 0.0);
 tmach->perceptions[2] = symbol_init(2);
 symbol_set_2(tmach->perceptions[2], 0.0, 0.5);
 tmach->perceptions[3] = symbol_init(2);
 symbol_set_2(tmach->perceptions[3], 1.0, 0.0);
 tmach->perceptions[4] = symbol_init(2);
 symbol_set_2(tmach->perceptions[4], 1.0, 1.0);
 tmach->perceptions[5] = symbol_init(2);
 symbol_set_2(tmach->perceptions[5], 1.0, 0.5);
 tmach->actions = xmalloc(sizeof(Symbol*) * 6);
 tmach->actions[0] = symbol_init(3);
 symbol_set_3(tmach->actions[0], 0.0, 1.0, 0.0);
 tmach->actions[1] = symbol_init(3);
 symbol_set_3(tmach->actions[1], 0.0, 0.0, 0.0);
 tmach->actions[2] = symbol_init(3);
 symbol_set_3(tmach->actions[2], 1.0, 0.5, 1.0);
 tmach->actions[3] = symbol_init(3);
 symbol_set_3(tmach->actions[3], 0.0, 1.0, 0.0);
 tmach->actions[4] = symbol_init(3);
 symbol_set_3(tmach->actions[4], 1.0, 0.0, 1.0);
 tmach->actions[5] = symbol_init(3);
 symbol_set_3(tmach->actions[5], 0.0, 1.0, 0.0);
 tmach->perception = som_init(2, iterations, 128, 128, ((void *)0));
 tmach->action = som_init(3, iterations, 128, 128, ((void *)0));
 tmach->symdex = 0;
 tmach->state = 0;
 tmach->head = (70 / 2);
 for (i = 0; i < 70; i++) {
  tmach->tape[i] = 0.5;
 }
 tmach->step = 0;
 tmach->valid = TRUE;
 return tmach;
}

canonical_original_code:
struct TuringMachine_s *turing_machine_init(int iterations)
{
 int i;
 struct TuringMachine_s *tmach = ((void *)0);
 tmach = (struct TuringMachine_s *)xmalloc(sizeof(TuringMachine) * 1);
 tmach->perceptions = xmalloc(sizeof(struct Symbol_s *) * 6);
 tmach->perceptions[0] = symbol_init(2);
 symbol_set_2(tmach->perceptions[0], 0.0, 1.0);
 tmach->perceptions[1] = symbol_init(2);
 symbol_set_2(tmach->perceptions[1], 0.0, 0.0);
 tmach->perceptions[2] = symbol_init(2);
 symbol_set_2(tmach->perceptions[2], 0.0, 0.5);
 tmach->perceptions[3] = symbol_init(2);
 symbol_set_2(tmach->perceptions[3], 1.0, 0.0);
 tmach->perceptions[4] = symbol_init(2);
 symbol_set_2(tmach->perceptions[4], 1.0, 1.0);
 tmach->perceptions[5] = symbol_init(2);
 symbol_set_2(tmach->perceptions[5], 1.0, 0.5);
 tmach->actions = xmalloc(sizeof(struct Symbol_s *) * 6);
 tmach->actions[0] = symbol_init(3);
 symbol_set_3(tmach->actions[0], 0.0, 1.0, 0.0);
 tmach->actions[1] = symbol_init(3);
 symbol_set_3(tmach->actions[1], 0.0, 0.0, 0.0);
 tmach->actions[2] = symbol_init(3);
 symbol_set_3(tmach->actions[2], 1.0, 0.5, 1.0);
 tmach->actions[3] = symbol_init(3);
 symbol_set_3(tmach->actions[3], 0.0, 1.0, 0.0);
 tmach->actions[4] = symbol_init(3);
 symbol_set_3(tmach->actions[4], 1.0, 0.0, 1.0);
 tmach->actions[5] = symbol_init(3);
 symbol_set_3(tmach->actions[5], 0.0, 1.0, 0.0);
 tmach->perception = som_init(2, iterations, 128, 128, ((void *)0));
 tmach->action = som_init(3, iterations, 128, 128, ((void *)0));
 tmach->symdex = 0;
 tmach->state = 0;
 tmach->head = (70 / 2);
 for (i = 0; i < 70; i++) {
  tmach->tape[i] = 0.5;
 }
 tmach->step = 0;
 tmach->valid = TRUE;
 return tmach;
}

variable_types: iterations int

variable_types: i int

variable_types: struct TuringMachine_s { struct Symbol_s { unsigned short dim; float vec[1]; } **perceptions; struct Symbol_s **actions; int symdex; float state; int head; float tape[70]; struct SOM_s { int mode; int bmu_row; struct SOMDesc_s { unsigned short dim; unsigned int train_iter; unsigned int rows; unsigned int cols; float (*radius_func)(struct SOM_s *s); } sd; int current_iter; float initial_radius; float half_life; struct Symbol_s **neuron; float final_computation; float max_dist; float *qmap; } *perception; struct SOM_s *action; int step; int valid; } *

user_defined_types: struct TuringMachine_s { struct Symbol_s **perceptions; struct Symbol_s **actions; int symdex; float state; int head; float tape[70]; struct SOM_s *perception; struct SOM_s *action; int step; int valid; }


decompiled_code:
void __fastcall symbol_corrupt(__int64 a1, float a2, float a3)
{
float v3; // xmm0_4
float v4; // xmm0_4
int i; // ebx
float v6; // xmm3_4
float v8[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

v3 = drand48();
if ( a2 > v3 )
{
for ( i = 0; (unsigned __int16)symbol_get_dim(a1) > i; ++i )
{
v6 = a3 * drand48();
symbol_get_index(a1, v8, (unsigned int)i);
if ( drand48() >= 0.5 )
v4 = (1.0 - v8[0]) * v6 + v8[0];
else
v4 = v8[0] - (float)(v6 * v8[0]);
v8[0] = v4;
symbol_set_index(a1, (unsigned int)i);
}
}
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1, float a2, float a3)
{
float v3; // xmm0_4
float v4; // xmm0_4
int i; // ebx
float v6; // xmm3_4
float v8[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

v3 = drand48();
if ( a2 > v3 )
{
for ( i = 0; (unsigned __int16)symbol_get_dim(a1) > i; ++i )
{
v6 = a3 * drand48();
symbol_get_index(a1, v8, (unsigned int)i);
if ( drand48() >= 0.5 )
v4 = (1.0 - v8[0]) * v6 + v8[0];
else
v4 = v8[0] - (float)(v6 * v8[0]);
v8[0] = v4;
symbol_set_index(a1, (unsigned int)i);
}
}
}

original_code:
void symbol_corrupt(float per_chance, float per_range, Symbol *sym)
{
 int i;
 float rnd;
 float val;
 rnd = drand48();
 if (rnd < per_chance) {
  for (i = 0; i < symbol_get_dim(sym); i++) {
   rnd = drand48() * per_range;
   symbol_get_index(sym, &val, i);
   if (drand48() < .5) {
    val = val - val * rnd;
   } else {
    val = val + (1.0 - val) * rnd;
   }
   symbol_set_index(sym, val, i);
  }
 }
}

canonical_original_code:
void symbol_corrupt(float per_chance, float per_range, struct Symbol_s *sym)
{
 int i;
 float rnd;
 float val;
 rnd = drand48();
 if (rnd < per_chance) {
  for (i = 0; i < symbol_get_dim(sym); i++) {
   rnd = drand48() * per_range;
   symbol_get_index(sym, &val, i);
   if (drand48() < .5) {
    val = val - val * rnd;
   } else {
    val = val + (1.0 - val) * rnd;
   }
   symbol_set_index(sym, val, i);
  }
 }
}

variable_types: struct Symbol_s { unsigned short dim; float vec[1]; } *

variable_types: rnd float

variable_types: val float


original_code:
int turing_machine_train(TuringMachine *tmach)
{
 int state;
 int bmu_row, bmu_col;
 Symbol *corrupted_precept = symbol_copy(tmach->perceptions[tmach->symdex]);
 Symbol *corrupted_action = symbol_copy(tmach->actions[tmach->symdex]);
 symbol_corrupt(0.10, 0.2, corrupted_precept);
 symbol_corrupt(0.10, 0.2, corrupted_action);
 som_learn(tmach->perception, corrupted_precept,
  &bmu_row, &bmu_col, 0, 128, SOM_REQUEST_LEARN, FALSE);
 state = som_learn(tmach->action, corrupted_action,
  &bmu_row, &bmu_col, 128, 128, SOM_REQUEST_LEARN, TRUE);
 tmach->symdex++;
 tmach->symdex %= 6;
 symbol_free(corrupted_precept);
 symbol_free(corrupted_action);
 return state;
}

canonical_original_code:
int turing_machine_train(struct TuringMachine_s *tmach)
{
 int state;
 int bmu_row; int bmu_col;
 struct Symbol_s *corrupted_precept = symbol_copy(tmach->perceptions[tmach->symdex]);
 struct Symbol_s *corrupted_action = symbol_copy(tmach->actions[tmach->symdex]);
 symbol_corrupt(0.10, 0.2, corrupted_precept);
 symbol_corrupt(0.10, 0.2, corrupted_action);
 som_learn(tmach->perception, corrupted_precept,
  &bmu_row, &bmu_col, 0, 128, SOM_REQUEST_LEARN, FALSE);
 state = som_learn(tmach->action, corrupted_action,
  &bmu_row, &bmu_col, 128, 128, SOM_REQUEST_LEARN, TRUE);
 tmach->symdex++;
 tmach->symdex %= 6;
 symbol_free(corrupted_precept);
 symbol_free(corrupted_action);
 return state;
}

variable_types: state int

return_type: int


decompiled_code:
void __fastcall turing_machine_step(__int64 a1)
{
__int64 v1; // rbp
__int64 v2; // r12
int v3; // eax
int v4; // eax
int v5; // eax
float v6; // [rsp+0h] [rbp-2Ch] BYREF
int v7; // [rsp+4h] [rbp-28h] BYREF
int v8; // [rsp+8h] [rbp-24h] BYREF
unsigned int v9; // [rsp+Ch] [rbp-20h] BYREF
unsigned int v10[7]; // [rsp+10h] [rbp-1Ch] BYREF

if ( *(_DWORD *)(a1 + 332) )
{
puts("Execute a step.");
v1 = symbol_init(2LL);
symbol_set_2(
v1,
COERCE_DOUBLE((unsigned __int64)*(_DWORD *)(a1 + 20)),
*(float *)(a1 + 4LL * *(int *)(a1 + 24) + 28));
printf("Precept: ");
symbol_stdout(v1);
som_learn(*(_QWORD *)(a1 + 312), v1, v10, &v9, 0LL, 128LL, 1LL, 0LL);
v2 = som_symbol_ref(*(_QWORD *)(a1 + 320), v10[0], v9);
symbol_get_3(v2, &v8, &v7, &v6);
printf("Action: ");
symbol_stdout(v2);
*(_DWORD *)(a1 + 20) = v8;
v3 = *(_DWORD *)(a1 + 24);
*(_DWORD *)(a1 + 4LL * v3 + 28) = v7;
if ( v6 >= 0.5 )
{
v5 = v3 - 1;
*(_DWORD *)(a1 + 24) = v5;
if ( v5 >= 0 )
goto LABEL_5;
}
else
{
v4 = v3 + 1;
*(_DWORD *)(a1 + 24) = v4;
if ( v4 != 70 )
{
LABEL_5:
symbol_free(v1);
++*(_DWORD *)(a1 + 328);
return;
}
}
puts("Turing machine off tape boundary.");
*(_DWORD *)(a1 + 332) = 0;
goto LABEL_5;
}
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1)
{
__int64 v1; // rbp
__int64 v2; // r12
int v3; // eax
int v4; // eax
int v5; // eax
float v6; // [rsp+0h] [rbp-2Ch] BYREF
int v7; // [rsp+4h] [rbp-28h] BYREF
int v8; // [rsp+8h] [rbp-24h] BYREF
unsigned int v9; // [rsp+Ch] [rbp-20h] BYREF
unsigned int v10[7]; // [rsp+10h] [rbp-1Ch] BYREF

if ( *(_DWORD *)(a1 + 332) )
{
puts("Execute a step.");
v1 = symbol_init(2LL);
symbol_set_2(
v1,
COERCE_DOUBLE((unsigned __int64)*(_DWORD *)(a1 + 20)),
*(float *)(a1 + 4LL * *(int *)(a1 + 24) + 28));
printf("Precept: ");
symbol_stdout(v1);
som_learn(*(_QWORD *)(a1 + 312), v1, v10, &v9, 0LL, 128LL, 1LL, 0LL);
v2 = som_symbol_ref(*(_QWORD *)(a1 + 320), v10[0], v9);
symbol_get_3(v2, &v8, &v7, &v6);
printf("Action: ");
symbol_stdout(v2);
*(_DWORD *)(a1 + 20) = v8;
v3 = *(_DWORD *)(a1 + 24);
*(_DWORD *)(a1 + 4LL * v3 + 28) = v7;
if ( v6 >= 0.5 )
{
v5 = v3 - 1;
*(_DWORD *)(a1 + 24) = v5;
if ( v5 >= 0 )
goto LABEL_5;
}
else
{
v4 = v3 + 1;
*(_DWORD *)(a1 + 24) = v4;
if ( v4 != 70 )
{
LABEL_5:
symbol_free(v1);
++*(_DWORD *)(a1 + 328);
return;
}
}
puts("Turing machine off tape boundary.");
*(_DWORD *)(a1 + 332) = 0;
goto LABEL_5;
}
}

original_code:
void turing_machine_step(TuringMachine *tmach)
{
 int bmu_row, bmu_col;
 Symbol *precept = ((void *)0);
 Symbol *act = ((void *)0);
 float new_state, write_value, head_move;
 if (tmach->valid == FALSE) {
  return;
 }
 printf("Execute a step.\n");
 precept = symbol_init(2);
 symbol_set_2(precept, tmach->state, tmach->tape[tmach->head]);
 printf("Precept: ");
 symbol_stdout(precept);
 som_learn(tmach->perception, precept,
  &bmu_row, &bmu_col, 0, 128, SOM_REQUEST_CLASSIFY, FALSE);
 act = som_symbol_ref(tmach->action, bmu_row, bmu_col);
 symbol_get_3(act, &new_state, &write_value, &head_move);
 printf("Action: ");
 symbol_stdout(act);
 tmach->state = new_state;
 tmach->tape[tmach->head] = write_value;
 if (head_move < .5) {
  tmach->head++;
  if (tmach->head == 70) {
   printf("Turing machine off tape boundary.\n");
   tmach->valid = FALSE;
  }
 } else {
  tmach->head--;
  if (tmach->head < 0) {
   printf("Turing machine off tape boundary.\n");
   tmach->valid = FALSE;
  }
 }
 symbol_free(precept);
 tmach->step++;
}

canonical_original_code:
void turing_machine_step(struct TuringMachine_s *tmach)
{
 int bmu_row; int bmu_col;
 struct Symbol_s *precept = ((void *)0);
 struct Symbol_s *act = ((void *)0);
 float new_state; float write_value; float head_move;
 if (tmach->valid == FALSE) {
  return;
 }
 printf("Execute a step.\n");
 precept = symbol_init(2);
 symbol_set_2(precept, tmach->state, tmach->tape[tmach->head]);
 printf("Precept: ");
 symbol_stdout(precept);
 som_learn(tmach->perception, precept,
  &bmu_row, &bmu_col, 0, 128, SOM_REQUEST_CLASSIFY, FALSE);
 act = som_symbol_ref(tmach->action, bmu_row, bmu_col);
 symbol_get_3(act, &new_state, &write_value, &head_move);
 printf("Action: ");
 symbol_stdout(act);
 tmach->state = new_state;
 tmach->tape[tmach->head] = write_value;
 if (head_move < .5) {
  tmach->head++;
  if (tmach->head == 70) {
   printf("Turing machine off tape boundary.\n");
   tmach->valid = FALSE;
  }
 } else {
  tmach->head--;
  if (tmach->head < 0) {
   printf("Turing machine off tape boundary.\n");
   tmach->valid = FALSE;
  }
 }
 symbol_free(precept);
 tmach->step++;
}


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

for ( i = 0LL; i != 48; i += 8LL )
{
symbol_free(*(_QWORD *)(*(_QWORD *)ptr + i));
symbol_free(*(_QWORD *)(*((_QWORD *)ptr + 1) + i));
}
free(*(void **)ptr);
free(*((void **)ptr + 1));
som_free(*((_QWORD *)ptr + 39));
som_free(*((_QWORD *)ptr + 40));
free(ptr);
}

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

for ( i = 0LL; i != 48; i += 8LL )
{
symbol_free(*(_QWORD *)(*(_QWORD *)ptr + i));
symbol_free(*(_QWORD *)(*((_QWORD *)ptr + 1) + i));
}
free(*(void **)ptr);
free(*((void **)ptr + 1));
som_free(*((_QWORD *)ptr + 39));
som_free(*((_QWORD *)ptr + 40));
free(ptr);
}

original_code:
void turing_machine_free(TuringMachine *tmach)
{
 int i;
 for (i = 0; i < 6; i++) {
  symbol_free(tmach->perceptions[i]);
  symbol_free(tmach->actions[i]);
 }
 free(tmach->perceptions);
 free(tmach->actions);
 som_free(tmach->perception);
 som_free(tmach->action);
 free(tmach);
}

canonical_original_code:
void turing_machine_free(struct TuringMachine_s *tmach)
{
 int i;
 for (i = 0; i < 6; i++) {
  symbol_free(tmach->perceptions[i]);
  symbol_free(tmach->actions[i]);
 }
 free(tmach->perceptions);
 free(tmach->actions);
 som_free(tmach->perception);
 som_free(tmach->action);
 free(tmach);
}


original_code:
void turing_machine_stdout(TuringMachine *tmach)
{
 int i;
 printf("---] Step %d\n", tmach->step);
 printf("State: %f\n", tmach->state);
 for (i = 0; i < 70; i++) {
  if (tmach->tape[i] >= 0 && tmach->tape[i] < .25) {
   printf("0");
  }
  if (tmach->tape[i] >= .25 && tmach->tape[i] < .75) {
   printf(".");
  }
  if (tmach->tape[i] >= .75) {
   printf("1");
  }
 }
 printf("\n");
 for (i = 0; i < 70; i++) {
  if (i == tmach->head) {
   printf("^");
  } else {
   printf(" ");
  }
 }
 printf("\n");
}

canonical_original_code:
void turing_machine_stdout(struct TuringMachine_s *tmach)
{
 int i;
 printf("---] Step %d\n", tmach->step);
 printf("State: %f\n", tmach->state);
 for (i = 0; i < 70; i++) {
  if (tmach->tape[i] >= 0 && tmach->tape[i] < .25) {
   printf("0");
  }
  if (tmach->tape[i] >= .25 && tmach->tape[i] < .75) {
   printf(".");
  }
  if (tmach->tape[i] >= .75) {
   printf("1");
  }
 }
 printf("\n");
 for (i = 0; i < 70; i++) {
  if (i == tmach->head) {
   printf("^");
  } else {
   printf(" ");
  }
 }
 printf("\n");
}


decompiled_code:
void test_turing_machine()
{
__int64 v0; // r13
int v1; // ebp
int v2; // er14
unsigned int v3; // er15
int v4; // er12
__int64 v5; // rax
__int64 v6; // rax
__int64 v7; // rax
unsigned int v8; // eax
const char *v9; // rax
int v10; // eax
int v11; // ebx
float v12; // [rsp+Ch] [rbp-4Ch] BYREF
float v13; // [rsp+10h] [rbp-48h] BYREF
float v14; // [rsp+14h] [rbp-44h] BYREF
unsigned int v15; // [rsp+18h] [rbp-40h] BYREF
unsigned int v16[15]; // [rsp+1Ch] [rbp-3Ch] BYREF

v0 = turing_machine_init(0x186A0u);
v1 = SDL_GetTicks();
v2 = v1 - 1;
turing_machine_stdout(v0);
v3 = 0;
v4 = 0;
do
{
if ( v2 < v1 || v4 == 1 )
glClear(16640LL);
v10 = process_events(v16, &v15);
v11 = v10;
if ( v10 == 2 )
{
v3 = 0;
LABEL_18:
v4 = turing_machine_train(v0);
goto LABEL_19;
}
if ( v10 != 3 )
{
if ( v10 == 1 )
v3 = 1;
goto LABEL_18;
}
printf("Clicked: %d, %d\n", v15, v16[0]);
v4 = turing_machine_train(v0);
if ( v15 <= 0x7F )
{
if ( v16[0] > 0x7F )
goto LABEL_19;
printf("Perception symbol: ");
v6 = som_symbol_ref(*(_QWORD *)(v0 + 312), v16[0], v15);
symbol_get_2(v6, &v14, &v13);
printf("<state=%f, head=%f>\n", v14, v13);
printf(" Associated request: ");
v7 = som_symbol_ref(*(_QWORD *)(v0 + 320), v16[0], v15 + 128);
symbol_get_3(v7, &v14, &v13, &v12);
printf("<state=%f, write=%f, move=%f>\n", v14, v13, v12);
}
if ( v15 - 128 <= 0x7F && v16[0] <= 0x7F )
{
printf("Action symbol: ");
v5 = som_symbol_ref(*(_QWORD *)(v0 + 320), v16[0], v15 - 128);
symbol_get_3(v5, &v14, &v13, &v12);
printf("<state=%f, write=%f, move=%f>\n", v14, v13, v12);
}
LABEL_19:
if ( v4 == 1 )
{
turing_machine_step(v0);
if ( *(_DWORD *)(v0 + 332) == 1 )
turing_machine_stdout(v0);
usleep(0x1F4u);
}
else if ( v2 >= v1 )
{
goto LABEL_21;
}
som_draw(*(_QWORD *)(v0 + 312), v3, 0LL, 0LL);
som_draw(*(_QWORD *)(v0 + 320), v3, 128LL, 0LL);
v8 = glGetError();
if ( v8 )
{
v9 = (const char *)gluErrorString(v8);
printf("Opengl Error: %s\n", v9);
}
SDL_GL_SwapBuffers();
v2 = v1 + 1000;
LABEL_21:
v1 = SDL_GetTicks();
}
while ( v11 != 4 );
turing_machine_free((void *)v0);
}

canonical_decompiled_code:
void func6()
{
__int64 v0; // r13
int v1; // ebp
int v2; // er14
unsigned int v3; // er15
int v4; // er12
__int64 v5; // rax
__int64 v6; // rax
__int64 v7; // rax
unsigned int v8; // eax
const char *v9; // rax
int v10; // eax
int v11; // ebx
float v12; // [rsp+Ch] [rbp-4Ch] BYREF
float v13; // [rsp+10h] [rbp-48h] BYREF
float v14; // [rsp+14h] [rbp-44h] BYREF
unsigned int v15; // [rsp+18h] [rbp-40h] BYREF
unsigned int v16[15]; // [rsp+1Ch] [rbp-3Ch] BYREF

v0 = func0(0x186A0u);
v1 = SDL_GetTicks();
v2 = v1 - 1;
func5(v0);
v3 = 0;
v4 = 0;
do
{
if ( v2 < v1 || v4 == 1 )
glClear(16640LL);
v10 = process_events(v16, &v15);
v11 = v10;
if ( v10 == 2 )
{
v3 = 0;
LABEL_18:
v4 = func2(v0);
goto LABEL_19;
}
if ( v10 != 3 )
{
if ( v10 == 1 )
v3 = 1;
goto LABEL_18;
}
printf("Clicked: %d, %d\n", v15, v16[0]);
v4 = func2(v0);
if ( v15 <= 0x7F )
{
if ( v16[0] > 0x7F )
goto LABEL_19;
printf("Perception symbol: ");
v6 = som_symbol_ref(*(_QWORD *)(v0 + 312), v16[0], v15);
symbol_get_2(v6, &v14, &v13);
printf("<state=%f, head=%f>\n", v14, v13);
printf(" Associated request: ");
v7 = som_symbol_ref(*(_QWORD *)(v0 + 320), v16[0], v15 + 128);
symbol_get_3(v7, &v14, &v13, &v12);
printf("<state=%f, write=%f, move=%f>\n", v14, v13, v12);
}
if ( v15 - 128 <= 0x7F && v16[0] <= 0x7F )
{
printf("Action symbol: ");
v5 = som_symbol_ref(*(_QWORD *)(v0 + 320), v16[0], v15 - 128);
symbol_get_3(v5, &v14, &v13, &v12);
printf("<state=%f, write=%f, move=%f>\n", v14, v13, v12);
}
LABEL_19:
if ( v4 == 1 )
{
func3(v0);
if ( *(_DWORD *)(v0 + 332) == 1 )
func5(v0);
usleep(0x1F4u);
}
else if ( v2 >= v1 )
{
goto LABEL_21;
}
som_draw(*(_QWORD *)(v0 + 312), v3, 0LL, 0LL);
som_draw(*(_QWORD *)(v0 + 320), v3, 128LL, 0LL);
v8 = glGetError();
if ( v8 )
{
v9 = (const char *)gluErrorString(v8);
printf("Opengl Error: %s\n", v9);
}
SDL_GL_SwapBuffers();
v2 = v1 + 1000;
LABEL_21:
v1 = SDL_GetTicks();
}
while ( v11 != 4 );
func4((void *)v0);
}

original_code:
void test_turing_machine(void)
{
 int state = STATE_RUNNING;
 int tmach_state = SOM_LEARNING;
 int drawing_mode = SOM_STYLE_ACTUAL;
 int sample, now, incr = 1000;
 int mr, mc;
 int iter;
 int glerr;
 TuringMachine *tmach = turing_machine_init(100000);
 Symbol *lookup = ((void *)0);
 float val0, val1, val2;
 now = SDL_GetTicks();
 sample = now - 1;
 iter = 0;
 turing_machine_stdout(tmach);
 while(state != STATE_EXIT)
 {
  if (now > sample || tmach_state == SOM_CLASSIFYING)
  {
   glClear(0x00004000 | 0x00000100);
  }
  state = process_events(&mr, &mc);
  switch (state)
  {
   case STATE_MOUSE_DOWN:
    printf("Clicked: %d, %d\n", mc, mr);
    break;
   case STATE_QUALITY:
    drawing_mode = SOM_STYLE_QUALITY;
    break;
   case STATE_ACTUAL:
    drawing_mode = SOM_STYLE_ACTUAL;
    break;
   default:
    ;
  }
  tmach_state = turing_machine_train(tmach);
  if (state == STATE_MOUSE_DOWN) {
   if (mc >= 0 && mc < 128 && mr >= 0 && mr < 128) {
    printf("Perception symbol: ");
    lookup = som_symbol_ref(tmach->perception, mr, mc);
    symbol_get_2(lookup, &val0, &val1);
    printf("<state=%f, head=%f>\n", val0, val1);
    printf("  Associated request: ");
    lookup = som_symbol_ref(tmach->action, mr, mc + 128);
    symbol_get_3(lookup, &val0, &val1, &val2);
    printf("<state=%f, write=%f, move=%f>\n", val0, val1, val2);
   }
   if (mc >= 128 && mc < 256 && mr >= 0 && mr < 128) {
    printf("Action symbol: ");
    lookup = som_symbol_ref(tmach->action, mr, mc - 128);
    symbol_get_3(lookup, &val0, &val1, &val2);
    printf("<state=%f, write=%f, move=%f>\n", val0, val1, val2);
   }
  }
  if (tmach_state == SOM_CLASSIFYING) {
   turing_machine_step(tmach);
   if (tmach->valid == TRUE) {
    turing_machine_stdout(tmach);
   }
   usleep(500);
  }
  if (now > sample || tmach_state == SOM_CLASSIFYING)
  {
   som_draw(tmach->perception, drawing_mode, 0, 0);
   som_draw(tmach->action, drawing_mode, 128, 0);
   glerr = glGetError();
   if (glerr != 0) {
    printf("Opengl Error: %s\n", gluErrorString(glerr));
   }
   SDL_GL_SwapBuffers();
   sample = now + incr;
   iter = 0;
  }
  now = SDL_GetTicks();
  iter++;
 }
 turing_machine_free(tmach);
}

canonical_original_code:
void test_turing_machine()
{
 int state = STATE_RUNNING;
 int tmach_state = SOM_LEARNING;
 int drawing_mode = SOM_STYLE_ACTUAL;
 int sample; int now; int incr = 1000;
 int mr; int mc;
 int iter;
 int glerr;
 struct TuringMachine_s *tmach = turing_machine_init(100000);
 struct Symbol_s *lookup = ((void *)0);
 float val0; float val1; float val2;
 now = SDL_GetTicks();
 sample = now - 1;
 iter = 0;
 turing_machine_stdout(tmach);
 while(state != STATE_EXIT)
 {
  if (now > sample || tmach_state == SOM_CLASSIFYING)
  {
   glClear(0x00004000 | 0x00000100);
  }
  state = process_events(&mr, &mc);
  switch (state)
  {
   case STATE_MOUSE_DOWN:
    printf("Clicked: %d, %d\n", mc, mr);
    break;
   case STATE_QUALITY:
    drawing_mode = SOM_STYLE_QUALITY;
    break;
   case STATE_ACTUAL:
    drawing_mode = SOM_STYLE_ACTUAL;
    break;
   default:
    ;
  }
  tmach_state = turing_machine_train(tmach);
  if (state == STATE_MOUSE_DOWN) {
   if (mc >= 0 && mc < 128 && mr >= 0 && mr < 128) {
    printf("Perception symbol: ");
    lookup = som_symbol_ref(tmach->perception, mr, mc);
    symbol_get_2(lookup, &val0, &val1);
    printf("<state=%f, head=%f>\n", val0, val1);
    printf("  Associated request: ");
    lookup = som_symbol_ref(tmach->action, mr, mc + 128);
    symbol_get_3(lookup, &val0, &val1, &val2);
    printf("<state=%f, write=%f, move=%f>\n", val0, val1, val2);
   }
   if (mc >= 128 && mc < 256 && mr >= 0 && mr < 128) {
    printf("Action symbol: ");
    lookup = som_symbol_ref(tmach->action, mr, mc - 128);
    symbol_get_3(lookup, &val0, &val1, &val2);
    printf("<state=%f, write=%f, move=%f>\n", val0, val1, val2);
   }
  }
  if (tmach_state == SOM_CLASSIFYING) {
   turing_machine_step(tmach);
   if (tmach->valid == TRUE) {
    turing_machine_stdout(tmach);
   }
   usleep(500);
  }
  if (now > sample || tmach_state == SOM_CLASSIFYING)
  {
   som_draw(tmach->perception, drawing_mode, 0, 0);
   som_draw(tmach->action, drawing_mode, 128, 0);
   glerr = glGetError();
   if (glerr != 0) {
    printf("Opengl Error: %s\n", gluErrorString(glerr));
   }
   SDL_GL_SwapBuffers();
   sample = now + incr;
   iter = 0;
  }
  now = SDL_GetTicks();
  iter++;
 }
 turing_machine_free(tmach);
}

variable_types: sample int

variable_types: now int

variable_types: incr int

variable_types: mr int

variable_types: mc int

variable_types: iter int

variable_types: glerr int


pochisq

variable_types: ax double

variable_types: df int

variable_types: x double

variable_types: a double

variable_types: y double

variable_types: s double

variable_types: e double

variable_types: c double

variable_types: z double

variable_types: even int


decompiled_code:
double __fastcall rt_log2(double a1)
{
return log10(a1) * 3.321928094887362;
}

canonical_decompiled_code:
double __fastcall func0(double a1)
{
return log10(a1) * 3.321928094887362;
}

original_code:
static double rt_log2(double x)
{
    return 3.32192809488736234787 * log10(x);
}


decompiled_code:
int *__fastcall rt_init(int a1)
{
int *result; // rax

binary = a1;
ent = 0LL;
chisq = 0LL;
datasum = 0LL;
mp = 0;
mcount = 0LL;
inmont = 0LL;
sccfirst = 1;
scct3 = 0LL;
scct2 = 0LL;
scct1 = 0LL;
incirc = LC2;
result = (int *)&ccount;
do
{
*(_QWORD *)result = 0LL;
result += 2;
}
while ( result != &binary );
totalc = 0LL;
return result;
}

canonical_decompiled_code:
int *__fastcall func1(int a1)
{
int *result; // rax

binary = a1;
ent = 0LL;
chisq = 0LL;
datasum = 0LL;
mp = 0;
mcount = 0LL;
inmont = 0LL;
sccfirst = 1;
scct3 = 0LL;
scct2 = 0LL;
scct1 = 0LL;
incirc = LC2;
result = (int *)&ccount;
do
{
*(_QWORD *)result = 0LL;
result += 2;
}
while ( result != &binary );
totalc = 0LL;
return result;
}

original_code:
void rt_init(int binmode)
{
    int i;
    binary = binmode;
    ent = 0.0;
    chisq = 0.0;
    datasum = 0.0;
    mp = 0;
    mcount = 0;
    inmont = 0;
    incirc = 65535.0 * 65535.0;
    sccfirst = 1;
    scct1 = scct2 = scct3 = 0.0;
    incirc = pow(pow(256.0, (double) (6 / 2)) - 1, 2.0);
    for (i = 0; i < 256; i++) {
 ccount[i] = 0;
    }
    totalc = 0;
}

variable_types: binmode int

variable_types: i int


original_code:
void rt_add(void *buf, int bufl)
{
    unsigned char *bp = buf;
    int oc, c, bean;
    while (bean = 0, (bufl-- > 0)) {
       oc = *bp++;
       do {
   if (binary) {
      c = !!(oc & 0x80);
   } else {
      c = oc;
   }
   ccount[c]++;
   totalc++;
   if (bean == 0) {
       monte[mp++] = oc;
       if (mp >= 6) {
   int mj;
   mp = 0;
   mcount++;
   montex = montey = 0;
   for (mj = 0; mj < 6 / 2; mj++) {
      montex = (montex * 256.0) + monte[mj];
      montey = (montey * 256.0) + monte[(6 / 2) + mj];
   }
   if ((montex * montex + montey * montey) <= incirc) {
      inmont++;
   }
       }
   }
   sccun = c;
   if (sccfirst) {
      sccfirst = 0;
      scclast = 0;
      sccu0 = sccun;
   } else {
      scct1 = scct1 + scclast * sccun;
   }
   scct2 = scct2 + sccun;
   scct3 = scct3 + (sccun * sccun);
   scclast = sccun;
   oc <<= 1;
       } while (binary && (++bean < 8));
    }
}

canonical_original_code:
void rt_add(void *buf, int bufl)
{
    unsigned char *bp = buf;
    int oc; int c; int bean;
    while (bean = 0, (bufl-- > 0)) {
       oc = *bp++;
       do {
   if (binary) {
      c = !!(oc & 0x80);
   } else {
      c = oc;
   }
   ccount[c]++;
   totalc++;
   if (bean == 0) {
       monte[mp++] = oc;
       if (mp >= 6) {
   int mj;
   mp = 0;
   mcount++;
   montex = montey = 0;
   for (mj = 0; mj < 6 / 2; mj++) {
      montex = (montex * 256.0) + monte[mj];
      montey = (montey * 256.0) + monte[(6 / 2) + mj];
   }
   if ((montex * montex + montey * montey) <= incirc) {
      inmont++;
   }
       }
   }
   sccun = c;
   if (sccfirst) {
      sccfirst = 0;
      scclast = 0;
      sccu0 = sccun;
   } else {
      scct1 = scct1 + scclast * sccun;
   }
   scct2 = scct2 + sccun;
   scct3 = scct3 + (sccun * sccun);
   scclast = sccun;
   oc <<= 1;
       } while (binary && (++bean < 8));
    }
}

variable_types:
buf
void *

variable_types: bufl int

variable_types:
bp
unsigned char *

variable_types: oc int

variable_types: c int

variable_types: bean int

variable_types: mj int


help

decompiled_code:
int help()
{
printf("ent -- Calculate entropy of file. Call");
printf("\n with ent [options] [input-file]");
putchar(10);
printf("\n Options: -b Treat input as a stream of bits");
printf("\n -c Print occurrence counts");
printf("\n -f Fold upper to lower case letters");
printf("\n -t Terse output in CSV format");
puts("\n -u Print this message");
printf("\nBy John Walker");
printf("\n http://www.fourmilab.ch/");
return printf("\n %s\n", "January 28th, 2008");
}

canonical_decompiled_code:
int func0()
{
printf("ent -- Calculate entropy of file. Call");
printf("\n with ent [options] [input-file]");
putchar(10);
printf("\n Options: -b Treat input as a stream of bits");
printf("\n -c Print occurrence counts");
printf("\n -f Fold upper to lower case letters");
printf("\n -t Terse output in CSV format");
puts("\n -u Print this message");
printf("\nBy John Walker");
printf("\n http://www.fourmilab.ch/");
return printf("\n %s\n", "January 28th, 2008");
}

original_code:
static void help(void)
{
        printf("ent --  Calculate entropy of file.  Call");
        printf("\n        with ent [options] [input-file]");
        printf("\n");
        printf("\n        Options:   -b   Treat input as a stream of bits");
        printf("\n                   -c   Print occurrence counts");
        printf("\n                   -f   Fold upper to lower case letters");
        printf("\n                   -t   Terse output in CSV format");
        printf("\n                   -u   Print this message\n");
        printf("\nBy John Walker");
        printf("\n   http://www.fourmilab.ch/");
        printf("\n   %s\n", "January 28th, 2008");
}

canonical_original_code:
static void help()
{
        printf("ent --  Calculate entropy of file.  Call");
        printf("\n        with ent [options] [input-file]");
        printf("\n");
        printf("\n        Options:   -b   Treat input as a stream of bits");
        printf("\n                   -c   Print occurrence counts");
        printf("\n                   -f   Fold upper to lower case letters");
        printf("\n                   -t   Terse output in CSV format");
        printf("\n                   -u   Print this message\n");
        printf("\nBy John Walker");
        printf("\n   http://www.fourmilab.ch/");
        printf("\n   %s\n", "January 28th, 2008");
}


repo: pstiasny/eiti-tin-derpftp

decompiled_code:
__int64 get_free_handle()
{
__int64 v0; // rax
unsigned int v1; // edx

v0 = 0LL;
while ( 1 )
{
v1 = v0;
if ( !servers[2 * v0] )
break;
if ( ++v0 == 32 )
return (unsigned int)-1;
}
return v1;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // rax
unsigned int v1; // edx

v0 = 0LL;
while ( 1 )
{
v1 = v0;
if ( !servers[2 * v0] )
break;
if ( ++v0 == 32 )
return (unsigned int)-1;
}
return v1;
}

original_code:
static int get_free_handle()
{
    int i;
    for (i = 0; i < 32; ++i)
        if (!servers[i].in_use)
            return i;
    return -1;
}


decompiled_code:
__int64 __fastcall read_response(int fd, void *buf)
{
size_t v3; // rbx
ssize_t v4; // rax

v3 = 8LL;
while ( 1 )
{
v4 = read(fd, buf, v3);
if ( v4 == -1 )
break;
buf = (char *)buf + v4;
v3 -= v4;
if ( !v3 )
return 0LL;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, void *buf)
{
size_t v3; // rbx
ssize_t v4; // rax

v3 = 8LL;
while ( 1 )
{
v4 = read(fd, buf, v3);
if ( v4 == -1 )
break;
buf = (char *)buf + v4;
v3 -= v4;
if ( !v3 )
return 0LL;
}
return 0xFFFFFFFFLL;
}

original_code:
static int read_response(int sock, struct fs_response *buf)
{
    size_t len = sizeof(struct fs_response);
    void *bufp = buf;
    ssize_t got;
    while (len) {
        got = read(sock, bufp, len);
        if (got == -1)
            return -1;
        len -= got;
        bufp += got;
    }
    return 0;
}

canonical_original_code:
static int read_response(int sock, struct fs_response *buf)
{
    unsigned long len = sizeof(struct fs_response);
    void *bufp = buf;
    long got;
    while (len) {
        got = read(sock, bufp, len);
        if (got == -1)
            return -1;
        len -= got;
        bufp += got;
    }
    return 0;
}

variable_types: sock int

variable_types: struct fs_response { int status; int val; } *

variable_types:
len
unsigned long

variable_types:
bufp
void *

variable_types: got long


decompiled_code:
__int64 __fastcall process_response(int *a1)
{
int v1; // ebx

if ( !*a1 )
return (unsigned int)a1[1];
v1 = *a1;
*_errno_location() = v1;
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func4(int *a1)
{
int v1; // ebx

if ( !*a1 )
return (unsigned int)a1[1];
v1 = *a1;
*_errno_location() = v1;
return 0xFFFFFFFFLL;
}

original_code:
static int process_response(struct fs_response *res)
{
    if (res->status) {
        (*__errno_location ()) = res->status;
        return (-1);
    } else {
        return res->val;
    }
}

user_defined_types: struct fs_response { int status; int val; }


decompiled_code:
ssize_t __fastcall write_command(int fd, char a2, int a3, int a4, int a5)
{
size_t v5; // rbp
char *i; // rbx
ssize_t result; // rax
char v8; // [rsp+0h] [rbp-28h] BYREF
int v9; // [rsp+4h] [rbp-24h]
int v10; // [rsp+8h] [rbp-20h]
int v11; // [rsp+Ch] [rbp-1Ch]

v8 = a2;
v9 = a3;
v10 = a4;
v11 = a5;
v5 = 16LL;
for ( i = &v8; ; i += result )
{
result = send(fd, i, v5, 0);
if ( !result )
break;
if ( result == -1 )
return 0xFFFFFFFFLL;
v5 -= result;
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func5(int fd, char a2, int a3, int a4, int a5)
{
size_t v5; // rbp
char *i; // rbx
ssize_t result; // rax
char v8; // [rsp+0h] [rbp-28h] BYREF
int v9; // [rsp+4h] [rbp-24h]
int v10; // [rsp+8h] [rbp-20h]
int v11; // [rsp+Ch] [rbp-1Ch]

v8 = a2;
v9 = a3;
v10 = a4;
v11 = a5;
v5 = 16LL;
for ( i = &v8; ; i += result )
{
result = send(fd, i, v5, 0);
if ( !result )
break;
if ( result == -1 )
return 0xFFFFFFFFLL;
v5 -= result;
}
return result;
}

original_code:
static int write_command(int sock, enum FSMSG_TYPE type, int32_t fd, int32_t arg1, int32_t arg2)
{
    struct fs_command cmd = { type, fd, arg1, arg2 };
    void *cmdp = &cmd;
    size_t sent, length = sizeof(cmd);
    while (0 != (sent = send(sock, cmdp, length, 0))) {
        if (sent == -1)
            return -1;
        length -= sent;
        cmdp += sent;
    }
    return 0;
}

canonical_original_code:
static int write_command(int sock, enum FSMSG_TYPE type, int fd, int arg1, int arg2)
{
    struct fs_command cmd = { type, fd, arg1, arg2 };
    void *cmdp = &cmd;
    unsigned long sent; unsigned long length = sizeof(cmd);
    while (0 != (sent = send(sock, cmdp, length, 0))) {
        if (sent == -1)
            return -1;
        length -= sent;
        cmdp += sent;
    }
    return 0;
}

variable_types: enum FSMSG_TYPE { FSMSG_OPEN=0, FSMSG_WRITE=1, FSMSG_READ=2, FSMSG_LSEEK=3, FSMSG_CLOSE=4, FSMSG_STAT=5 }

variable_types: fd int

variable_types: struct fs_command { char type; int fd; }

variable_types:
cmdp
void *

variable_types:
sent
unsigned long

variable_types:
length
unsigned long


decompiled_code:
__int64 __fastcall fs_open_server(char *src)
{
unsigned int v1; // eax
unsigned int v2; // ebx
char *v3; // rax
char *v4; // rbp
__int16 v5; // r12
struct hostent *v6; // rax
int v7; // edi
char v9[1024]; // [rsp+0h] [rbp-438h] BYREF
struct sockaddr addr; // [rsp+400h] [rbp-38h] BYREF

v1 = get_free_handle();
if ( v1 == -1 )
return (unsigned int)-2;
v2 = v1;
v3 = strchr(src, 58);
v4 = v3;
if ( v3 )
{
v5 = atoi(v3 + 1);
strncpy(v9, src, v4 - src);
}
else
{
strncpy(v9, src, 0x400uLL);
v5 = 1337;
}
v6 = gethostbyname(v9);
if ( !v6 )
return (unsigned int)-4;
memcpy(&addr.sa_data[2], *(const void **)v6->h_addr_list, v6->h_length);
addr.sa_family = 2;
*(_WORD *)addr.sa_data = _bswap_16(v5);
v7 = socket(2, 1, 0);
servers[2 * v2 + 1] = v7;
if ( v7 < 0 )
return (unsigned int)-3;
if ( connect(v7, &addr, 0x10u) == -1 )
return (unsigned int)-3;
servers[2 * v2] = 1;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *src)
{
unsigned int v1; // eax
unsigned int v2; // ebx
char *v3; // rax
char *v4; // rbp
__int16 v5; // r12
struct hostent *v6; // rax
int v7; // edi
char v9[1024]; // [rsp+0h] [rbp-438h] BYREF
struct sockaddr addr; // [rsp+400h] [rbp-38h] BYREF

v1 = func0();
if ( v1 == -1 )
return (unsigned int)-2;
v2 = v1;
v3 = strchr(src, 58);
v4 = v3;
if ( v3 )
{
v5 = atoi(v3 + 1);
strncpy(v9, src, v4 - src);
}
else
{
strncpy(v9, src, 0x400uLL);
v5 = 1337;
}
v6 = gethostbyname(v9);
if ( !v6 )
return (unsigned int)-4;
memcpy(&addr.sa_data[2], *(const void **)v6->h_addr_list, v6->h_length);
addr.sa_family = 2;
*(_WORD *)addr.sa_data = func1(v5);
v7 = socket(2, 1, 0);
servers[2 * v2 + 1] = v7;
if ( v7 < 0 )
return (unsigned int)-3;
if ( connect(v7, &addr, 0x10u) == -1 )
return (unsigned int)-3;
servers[2 * v2] = 1;
return v2;
}

original_code:
int fs_open_server(const char *server_addr)
{
    int server_handle, port = 1337;
    struct serverd *sd;
    struct sockaddr_in addr;
    struct hostent *hp;
    char host_part[1024], *port_part;
    if (-1 == (server_handle = get_free_handle()))
        return (-2);
    sd = &servers[server_handle];
    if (0 != (port_part = strchr(server_addr, ':'))) {
        port = atoi(port_part + 1);
        strncpy(host_part, server_addr, port_part - server_addr);
    } else {
        strncpy(host_part, server_addr, sizeof(host_part));
    }
    hp = gethostbyname(host_part);
    if (hp == 0)
        return (-4);
    memcpy((char*)&addr.sin_addr, (char*)hp->h_addr_list[0], hp->h_length);
    addr.sin_family = 2;
    addr.sin_port = htons(port);
    sd->sock = socket(2, SOCK_STREAM, 0);
    if (sd->sock < 0)
        return (-3);
    if (-1 == connect(sd->sock, (struct sockaddr*)&addr, sizeof(addr)))
        return (-3);
    sd->in_use = 1;
    return server_handle;
}

canonical_original_code:
int fs_open_server(char *server_addr)
{
    int server_handle; int port = 1337;
    struct serverd *sd;
    struct sockaddr_in addr;
    struct hostent *hp;
    char host_part[1024]; char *port_part;
    if (-1 == (server_handle = get_free_handle()))
        return (-2);
    sd = &servers[server_handle];
    if (0 != (port_part = strchr(server_addr, ':'))) {
        port = atoi(port_part + 1);
        strncpy(host_part, server_addr, port_part - server_addr);
    } else {
        strncpy(host_part, server_addr, sizeof(host_part));
    }
    hp = gethostbyname(host_part);
    if (hp == 0)
        return (-4);
    memcpy((char *)&addr.sin_addr, (char *)hp->h_addr_list[0], hp->h_length);
    addr.sin_family = 2;
    addr.sin_port = htons(port);
    sd->sock = socket(2, SOCK_STREAM, 0);
    if (sd->sock < 0)
        return (-3);
    if (-1 == connect(sd->sock, (struct sockaddr *)&addr, sizeof(addr)))
        return (-3);
    sd->in_use = 1;
    return server_handle;
}

variable_types: port int

variable_types: struct serverd { int in_use; int sock; } *

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

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

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

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


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

if ( a1 > 0x1F )
return -5;
if ( !servers[2 * a1] )
return -5;
servers[2 * a1] = 0;
result = close(servers[2 * a1 + 1]);
if ( result )
result = -3;
return result;
}

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

if ( a1 > 0x1F )
return -5;
if ( !servers[2 * a1] )
return -5;
servers[2 * a1] = 0;
result = close(servers[2 * a1 + 1]);
if ( result )
result = -3;
return result;
}

original_code:
int fs_close_server(int server_handle)
{
    if (((server_handle < 0) || (server_handle >= 32) || !servers[server_handle].in_use))
        return (-5);
    servers[server_handle].in_use = 0;
    if (0 == close(servers[server_handle].sock))
        return (0);
    else
        return (-3);
}


decompiled_code:
__int64 __fastcall fs_open(unsigned int a1, const char *a2, int a3)
{
int v4; // [rsp+0h] [rbp-130h] BYREF
int buf[74]; // [rsp+8h] [rbp-128h] BYREF

if ( a1 > 0x1F )
return 4294967291LL;
if ( !servers[2 * a1] )
return 4294967291LL;
memset(buf, 0, 0x110uLL);
buf[2] = a3;
strncpy((char *)&buf[4], a2, 0x100uLL);
if ( write(servers[2 * a1 + 1], buf, 0x110uLL) != 272 )
return 4294967293LL;
if ( (unsigned int)read_response(servers[2 * a1 + 1], &v4) == -1 )
return 4294967293LL;
return process_response(&v4);
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1, const char *a2, int a3)
{
int v4; // [rsp+0h] [rbp-130h] BYREF
int buf[74]; // [rsp+8h] [rbp-128h] BYREF

if ( a1 > 0x1F )
return 4294967291LL;
if ( !servers[2 * a1] )
return 4294967291LL;
memset(buf, 0, 0x110uLL);
buf[2] = a3;
strncpy((char *)&buf[4], a2, 0x100uLL);
if ( write(servers[2 * a1 + 1], buf, 0x110uLL) != 272 )
return 4294967293LL;
if ( (unsigned int)func2(servers[2 * a1 + 1], &v4) == -1 )
return 4294967293LL;
return func4(&v4);
}

original_code:
int fs_open(int server_handle, const char *name, int flags)
{
    struct fs_open_command cmd;
    struct fs_response res;
    if (((server_handle < 0) || (server_handle >= 32) || !servers[server_handle].in_use))
        return (-5);
    memset(&cmd, 0, sizeof(cmd));
    cmd.base_command.type = FSMSG_OPEN;
    cmd.base_command.arg1 = flags;
    strncpy(cmd.filename, name, 256);
    if (sizeof(cmd) != write(servers[server_handle].sock, &cmd, sizeof(cmd)))
        return (-3);
    if (-1 == read_response(servers[server_handle].sock, &res))
        return (-3);
    return process_response(&res);
}

canonical_original_code:
int fs_open(int server_handle, char *name, int flags)
{
    struct fs_open_command cmd;
    struct fs_response res;
    if (((server_handle < 0) || (server_handle >= 32) || !servers[server_handle].in_use))
        return (-5);
    memset(&cmd, 0, sizeof(cmd));
    cmd.base_command.type = FSMSG_OPEN;
    cmd.base_command.arg1 = flags;
    strncpy(cmd.filename, name, 256);
    if (sizeof(cmd) != write(servers[server_handle].sock, &cmd, sizeof(cmd)))
        return (-3);
    if (-1 == read_response(servers[server_handle].sock, &res))
        return (-3);
    return process_response(&res);
}

variable_types:
name
char *

variable_types: flags int

variable_types: struct fs_open_command { struct fs_command { char type; int fd; } base_command; char filename[256]; }

variable_types: struct fs_response { int status; int val; }

user_defined_types: struct fs_open_command { struct fs_command base_command; char filename[256]; }


decompiled_code:
__int64 __fastcall fs_write(unsigned int a1, int a2, char *a3, size_t a4)
{
size_t v5; // rbx
int v6; // er12
ssize_t v7; // rax
__int64 result; // rax
int buf[8]; // [rsp+0h] [rbp-20h] BYREF

if ( a1 > 0x1F )
return 4294967291LL;
v5 = a4;
if ( !servers[2 * a1] )
return 4294967291LL;
v6 = servers[2 * a1 + 1];
if ( (unsigned int)write_command(v6, 1, a2, a4, 0) == -1 )
return 4294967293LL;
if ( v5 )
{
while ( 1 )
{
v7 = send(v6, a3, v5, 0);
if ( v7 == -1 )
break;
a3 += v7;
v5 -= v7;
if ( !v5 )
goto LABEL_7;
}
result = 4294967293LL;
}
else
{
LABEL_7:
if ( (unsigned int)read_response(v6, buf) == -1 )
result = 4294967293LL;
else
result = process_response(buf);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int a1, int a2, char *a3, size_t a4)
{
size_t v5; // rbx
int v6; // er12
ssize_t v7; // rax
__int64 result; // rax
int buf[8]; // [rsp+0h] [rbp-20h] BYREF

if ( a1 > 0x1F )
return 4294967291LL;
v5 = a4;
if ( !servers[2 * a1] )
return 4294967291LL;
v6 = servers[2 * a1 + 1];
if ( (unsigned int)func5(v6, 1, a2, a4, 0) == -1 )
return 4294967293LL;
if ( v5 )
{
while ( 1 )
{
v7 = send(v6, a3, v5, 0);
if ( v7 == -1 )
break;
a3 += v7;
v5 -= v7;
if ( !v5 )
goto LABEL_7;
}
result = 4294967293LL;
}
else
{
LABEL_7:
if ( (unsigned int)func2(v6, buf) == -1 )
result = 4294967293LL;
else
result = func4(buf);
}
return result;
}

original_code:
int fs_write(int server_handle, int fd, void *buf, size_t len)
{
    struct fs_response res;
    size_t sent = 0;
    int sock;
    if (((server_handle < 0) || (server_handle >= 32) || !servers[server_handle].in_use))
        return (-5);
    sock = servers[server_handle].sock;
    if (-1 == write_command(sock, FSMSG_WRITE, fd, len, 0))
        return (-3);
    for (; len > 0; len -= sent) {
        sent = send(sock, buf, len, 0);
        if (sent == -1)
            return (-3);
        buf += sent;
    }
    if (-1 == read_response(sock, &res))
        return (-3);
    return process_response(&res);
}

canonical_original_code:
int fs_write(int server_handle, int fd, void *buf, unsigned long len)
{
    struct fs_response res;
    unsigned long sent = 0;
    int sock;
    if (((server_handle < 0) || (server_handle >= 32) || !servers[server_handle].in_use))
        return (-5);
    sock = servers[server_handle].sock;
    if (-1 == write_command(sock, FSMSG_WRITE, fd, len, 0))
        return (-3);
    for (; len > 0; len -= sent) {
        sent = send(sock, buf, len, 0);
        if (sent == -1)
            return (-3);
        buf += sent;
    }
    if (-1 == read_response(sock, &res))
        return (-3);
    return process_response(&res);
}

variable_types:
buf
void *

variable_types:
len
unsigned long

variable_types: sock int


decompiled_code:
__int64 __fastcall fs_read(unsigned int a1, int a2, char *a3, unsigned __int64 a4)
{
int v6; // er12
int v7; // ebx
int v8; // ebx
int v9; // eax
__int64 result; // rax
int buf; // [rsp+0h] [rbp-30h] BYREF
unsigned int v12; // [rsp+4h] [rbp-2Ch]

if ( a1 > 0x1F )
return 4294967291LL;
if ( !servers[2 * a1] )
return 4294967291LL;
v6 = servers[2 * a1 + 1];
if ( (unsigned int)write_command(v6, 2, a2, a4, 0) == -1 )
return 4294967293LL;
if ( (unsigned int)read_response(v6, &buf) == -1 )
return 4294967293LL;
v7 = buf;
if ( buf )
{
*_errno_location() = v7;
result = 0xFFFFFFFFLL;
}
else
{
v8 = v12;
if ( (int)v12 > a4 )
{
result = 4294967290LL;
}
else if ( (int)v12 <= 0 )
{
LABEL_10:
result = v12;
}
else
{
while ( 1 )
{
v9 = recv(v6, a3, v8, 0);
if ( v9 <= 0 )
break;
a3 += v9;
v8 -= v9;
if ( v8 <= 0 )
goto LABEL_10;
}
result = 4294967293LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned int a1, int a2, char *a3, unsigned __int64 a4)
{
int v6; // er12
int v7; // ebx
int v8; // ebx
int v9; // eax
__int64 result; // rax
int buf; // [rsp+0h] [rbp-30h] BYREF
unsigned int v12; // [rsp+4h] [rbp-2Ch]

if ( a1 > 0x1F )
return 4294967291LL;
if ( !servers[2 * a1] )
return 4294967291LL;
v6 = servers[2 * a1 + 1];
if ( (unsigned int)func5(v6, 2, a2, a4, 0) == -1 )
return 4294967293LL;
if ( (unsigned int)func2(v6, &buf) == -1 )
return 4294967293LL;
v7 = buf;
if ( buf )
{
*_errno_location() = v7;
result = 0xFFFFFFFFLL;
}
else
{
v8 = v12;
if ( (int)v12 > a4 )
{
result = 4294967290LL;
}
else if ( (int)v12 <= 0 )
{
LABEL_10:
result = v12;
}
else
{
while ( 1 )
{
v9 = recv(v6, a3, v8, 0);
if ( v9 <= 0 )
break;
a3 += v9;
v8 -= v9;
if ( v8 <= 0 )
goto LABEL_10;
}
result = 4294967293LL;
}
}
return result;
}

original_code:
int fs_read(int server_handle, int fd, void *buf, size_t len)
{
    struct fs_response res;
    int sock, rcvd, response_len;
    if (((server_handle < 0) || (server_handle >= 32) || !servers[server_handle].in_use))
        return (-5);
    sock = servers[server_handle].sock;
    if (-1 == write_command(sock, FSMSG_READ, fd, len, 0))
        return (-3);
    if (-1 == read_response(sock, &res))
        return (-3);
    if (res.status) {
        (*__errno_location ()) = res.status;
        return (-1);
    }
    response_len = res.val;
    if (response_len > len)
        return (-6);
    for (; response_len > 0; response_len -= rcvd) {
        rcvd = recv(sock, buf, response_len, 0);
        if (rcvd <= 0)
            return (-3);
        buf += rcvd;
    }
    return res.val;
}

canonical_original_code:
int fs_read(int server_handle, int fd, void *buf, unsigned long len)
{
    struct fs_response res;
    int sock; int rcvd; int response_len;
    if (((server_handle < 0) || (server_handle >= 32) || !servers[server_handle].in_use))
        return (-5);
    sock = servers[server_handle].sock;
    if (-1 == write_command(sock, FSMSG_READ, fd, len, 0))
        return (-3);
    if (-1 == read_response(sock, &res))
        return (-3);
    if (res.status) {
        (*__errno_location ()) = res.status;
        return (-1);
    }
    response_len = res.val;
    if (response_len > len)
        return (-6);
    for (; response_len > 0; response_len -= rcvd) {
        rcvd = recv(sock, buf, response_len, 0);
        if (rcvd <= 0)
            return (-3);
        buf += rcvd;
    }
    return res.val;
}

variable_types: rcvd int


decompiled_code:
__int64 __fastcall fs_lseek(unsigned int a1, int a2, int a3, int a4)
{
int v4; // ebp
int buf[8]; // [rsp+0h] [rbp-20h] BYREF

if ( a1 > 0x1F )
return 4294967291LL;
if ( !servers[2 * a1] )
return 4294967291LL;
v4 = servers[2 * a1 + 1];
write_command(v4, 3, a2, a3, a4);
read_response(v4, buf);
return process_response(buf);
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned int a1, int a2, int a3, int a4)
{
int v4; // ebp
int buf[8]; // [rsp+0h] [rbp-20h] BYREF

if ( a1 > 0x1F )
return 4294967291LL;
if ( !servers[2 * a1] )
return 4294967291LL;
v4 = servers[2 * a1 + 1];
func5(v4, 3, a2, a3, a4);
func2(v4, buf);
return func4(buf);
}

original_code:
int fs_lseek(int server_handle, int fd, long offset, int whence)
{
    struct fs_response res;
    int sock;
    if (((server_handle < 0) || (server_handle >= 32) || !servers[server_handle].in_use))
        return (-5);
    sock = servers[server_handle].sock;
    write_command(sock, FSMSG_LSEEK, fd, offset, whence);
    read_response(sock, &res);
    return process_response(&res);
}

variable_types: fd int

variable_types: offset long

variable_types: whence int


decompiled_code:
__int64 __fastcall fs_close(unsigned int a1, int a2)
{
int v2; // ebp
int buf[8]; // [rsp+0h] [rbp-20h] BYREF

if ( a1 > 0x1F )
return 4294967291LL;
if ( !servers[2 * a1] )
return 4294967291LL;
v2 = servers[2 * a1 + 1];
write_command(v2, 4, a2, 0, 0);
read_response(v2, buf);
return process_response(buf);
}

canonical_decompiled_code:
__int64 __fastcall func12(unsigned int a1, int a2)
{
int v2; // ebp
int buf[8]; // [rsp+0h] [rbp-20h] BYREF

if ( a1 > 0x1F )
return 4294967291LL;
if ( !servers[2 * a1] )
return 4294967291LL;
v2 = servers[2 * a1 + 1];
func5(v2, 4, a2, 0, 0);
func2(v2, buf);
return func4(buf);
}

original_code:
int fs_close(int server_handle, int fd)
{
    struct fs_response res;
    int sock;
    if (((server_handle < 0) || (server_handle >= 32) || !servers[server_handle].in_use))
        return (-5);
    sock = servers[server_handle].sock;
    write_command(sock, FSMSG_CLOSE, fd, 0, 0);
    read_response(sock, &res);
    return process_response(&res);
}


decompiled_code:
__int64 __fastcall fs_fstat(unsigned int a1, int a2, __m128i *a3)
{
int v4; // er12
int v6; // [rsp-8h] [rbp-48h] BYREF
__m128i v7; // [rsp+0h] [rbp-40h] BYREF
__int64 v8; // [rsp+10h] [rbp-30h]
int v9; // [rsp+18h] [rbp-28h]

if ( a1 > 0x1F )
return 4294967291LL;
if ( !servers[2 * a1] )
return 4294967291LL;
v4 = servers[2 * a1 + 1];
write_command(v4, 5, a2, 0, 0);
read_stat_response(v4, &v6);
*a3 = _mm_loadu_si128(&v7);
a3[1].m128i_i64[0] = v8;
a3[1].m128i_i32[2] = v9;
return process_response(&v6);
}

original_code:
int fs_fstat(int server_handle, int fd, struct fs_stat *buf)
{
    int sock;
    struct fs_stat_response resp;
    if (((server_handle < 0) || (server_handle >= 32) || !servers[server_handle].in_use))
        return (-5);
    sock = servers[server_handle].sock;
    write_command(sock, FSMSG_STAT, fd, 0, 0);
    read_stat_response(sock, &resp);
    memcpy(buf, &(resp.stat), sizeof(struct fs_stat));
    return process_response(&resp.base_response);
}

variable_types: struct fs_stat { int st_size; int st_atim; int st_mtim; int st_ctim; int st_blocks; int st_blksize; short st_mode; } *

variable_types: struct fs_stat_response { struct fs_response { int status; int val; } base_response; struct fs_stat { int st_size; int st_atim; int st_mtim; int st_ctim; int st_blocks; int st_blksize; short st_mode; } stat; }

user_defined_types: struct fs_stat_response { struct fs_response base_response; struct fs_stat stat; }


decompiled_code:
__int64 __fastcall send_reponse(int a1, int a2, int a3)
{
int buf[4]; // [rsp+8h] [rbp-10h] BYREF

buf[0] = a2;
buf[1] = a3;
send(a1, buf, 8uLL, 0);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, int a2, int a3)
{
int buf[4]; // [rsp+8h] [rbp-10h] BYREF

buf[0] = a2;
buf[1] = a3;
send(a1, buf, 8uLL, 0);
return 0LL;
}

original_code:
int send_reponse(int sock, int status, int value) {
    struct fs_response resp = {status, value};
    send(sock, &resp, sizeof(resp), 0);
    return 0;
}

variable_types: status int

variable_types: value int


original_code:
int send_stat_reponse(int sock, int status, int value, struct stat *st) {
    struct fs_stat_response resp;
    resp.base_response.status = status;
    resp.base_response.val = value;
    resp.stat.st_mode = st->st_mode;
    resp.stat.st_size = st->st_size;
    resp.stat.st_atim = st->st_atim.tv_sec;
    resp.stat.st_mtim = st->st_mtim.tv_sec;
    resp.stat.st_ctim = st->st_ctim.tv_sec;
    resp.stat.st_blocks = st->st_blocks;
    resp.stat.st_blksize = st->st_blksize;
    send(sock, &resp, sizeof(resp), 0);
    return 0;
}

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

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

user_defined_types: struct fs_stat { int st_size; int st_atim; int st_mtim; int st_ctim; int st_blocks; int st_blksize; short st_mode; }


decompiled_code:
__int64 __fastcall handle_connection(int fd)
{
int v1; // ebp
int *v2; // rax
int v3; // ebp
int v4; // edx
int v5; // eax
int *v6; // rax
int v7; // edx
int v8; // ebp
int *v9; // rax
char *v10; // r12
int v11; // eax
int v12; // ebp
int *v13; // rax
int v14; // ebp
int *v15; // rax
int v16; // ebp
int *v17; // rax
__int64 result; // rax
struct stat v19; // [rsp+0h] [rbp-29D8h] BYREF
char v20[10240]; // [rsp+90h] [rbp-2948h] BYREF
char v21[4]; // [rsp+2890h] [rbp-148h] BYREF
int fda; // [rsp+2894h] [rbp-144h]
int v23; // [rsp+2898h] [rbp-140h]
int whence; // [rsp+289Ch] [rbp-13Ch]
char v25[8]; // [rsp+28A0h] [rbp-138h] BYREF
int oflag; // [rsp+28A8h] [rbp-130h]
char file[271]; // [rsp+28B0h] [rbp-128h] BYREF
char buf[25]; // [rsp+29BFh] [rbp-19h] BYREF

while ( 2 )
{
if ( recv(fd, buf, 1uLL, 2) == 1 )
{
switch ( buf[0] )
{
case 0:
recv(fd, v25, 0x110uLL, 256);
file[255] = 0;
printf("received FSMSG_OPEN, filename = %s\n", file);
v1 = open(file, oflag, 432LL);
v2 = __errno_location();
send_reponse(fd, *v2, v1);
continue;
case 1:
recv(fd, v21, 0x10uLL, 256);
printf("received FSMSG_WRITE, fd = %i\n", (unsigned int)fda);
v3 = 0;
while ( 1 )
{
v4 = 10240;
if ( v23 <= 10240 )
v4 = v23;
v5 = read(fd, v20, v4);
if ( v5 <= 0 )
break;
v3 += v5;
v23 -= v5;
write(fda, v20, v5);
}
v6 = __errno_location();
send_reponse(fd, *v6, v3);
continue;
case 2:
recv(fd, v21, 0x10uLL, 256);
printf("received FSMSG_READ, fd = %i\n", (unsigned int)fda);
v7 = 10240;
if ( v23 <= 10240 )
v7 = v23;
v8 = read(fda, v20, v7);
v9 = __errno_location();
send_reponse(fd, *v9, v8);
if ( v8 )
{
v10 = v20;
do
{
v11 = write(fd, v10, v8);
v10 += v11;
v8 -= v11;
}
while ( v8 );
}
continue;
case 3:
recv(fd, v21, 0x10uLL, 256);
printf("received FSMSG_LSEEK, fd = %i\n", (unsigned int)fda);
v12 = lseek(fda, v23, whence);
v13 = __errno_location();
send_reponse(fd, *v13, v12);
continue;
case 4:
recv(fd, v21, 0x10uLL, 256);
printf("received FSMSG_CLOSE, fd = %i\n", (unsigned int)fda);
v14 = close(fda);
v15 = __errno_location();
send_reponse(fd, *v15, v14);
continue;
case 5:
recv(fd, v21, 0x10uLL, 256);
printf("received FSMSG_STAT, fd = %i\n", (unsigned int)fda);
v16 = fstat(fda, &v19);
v17 = __errno_location();
send_stat_reponse(fd, *v17, v16, (__int64)&v19);
continue;
default:
printf("unknown msg type %d\n", (unsigned int)buf[0]);
result = 1LL;
break;
}
}
else
{
puts("subprocess exiting");
result = 0LL;
}
return result;
}
}

canonical_decompiled_code:
__int64 __fastcall func10(int fd)
{
int v1; // ebp
int *v2; // rax
int v3; // ebp
int v4; // edx
int v5; // eax
int *v6; // rax
int v7; // edx
int v8; // ebp
int *v9; // rax
char *v10; // r12
int v11; // eax
int v12; // ebp
int *v13; // rax
int v14; // ebp
int *v15; // rax
int v16; // ebp
int *v17; // rax
__int64 result; // rax
struct stat v19; // [rsp+0h] [rbp-29D8h] BYREF
char v20[10240]; // [rsp+90h] [rbp-2948h] BYREF
char v21[4]; // [rsp+2890h] [rbp-148h] BYREF
int fda; // [rsp+2894h] [rbp-144h]
int v23; // [rsp+2898h] [rbp-140h]
int whence; // [rsp+289Ch] [rbp-13Ch]
char v25[8]; // [rsp+28A0h] [rbp-138h] BYREF
int oflag; // [rsp+28A8h] [rbp-130h]
char file[271]; // [rsp+28B0h] [rbp-128h] BYREF
char buf[25]; // [rsp+29BFh] [rbp-19h] BYREF

while ( 2 )
{
if ( recv(fd, buf, 1uLL, 2) == 1 )
{
switch ( buf[0] )
{
case 0:
recv(fd, v25, 0x110uLL, 256);
file[255] = 0;
printf("received FSMSG_OPEN, filename = %s\n", file);
v1 = open(file, oflag, 432LL);
v2 = __errno_location();
func8(fd, *v2, v1);
continue;
case 1:
recv(fd, v21, 0x10uLL, 256);
printf("received FSMSG_WRITE, fd = %i\n", (unsigned int)fda);
v3 = 0;
while ( 1 )
{
v4 = 10240;
if ( v23 <= 10240 )
v4 = v23;
v5 = read(fd, v20, v4);
if ( v5 <= 0 )
break;
v3 += v5;
v23 -= v5;
write(fda, v20, v5);
}
v6 = __errno_location();
func8(fd, *v6, v3);
continue;
case 2:
recv(fd, v21, 0x10uLL, 256);
printf("received FSMSG_READ, fd = %i\n", (unsigned int)fda);
v7 = 10240;
if ( v23 <= 10240 )
v7 = v23;
v8 = read(fda, v20, v7);
v9 = __errno_location();
func8(fd, *v9, v8);
if ( v8 )
{
v10 = v20;
do
{
v11 = write(fd, v10, v8);
v10 += v11;
v8 -= v11;
}
while ( v8 );
}
continue;
case 3:
recv(fd, v21, 0x10uLL, 256);
printf("received FSMSG_LSEEK, fd = %i\n", (unsigned int)fda);
v12 = lseek(fda, v23, whence);
v13 = __errno_location();
func8(fd, *v13, v12);
continue;
case 4:
recv(fd, v21, 0x10uLL, 256);
printf("received FSMSG_CLOSE, fd = %i\n", (unsigned int)fda);
v14 = close(fda);
v15 = __errno_location();
func8(fd, *v15, v14);
continue;
case 5:
recv(fd, v21, 0x10uLL, 256);
printf("received FSMSG_STAT, fd = %i\n", (unsigned int)fda);
v16 = func13(fda, &v19);
v17 = __errno_location();
func9(fd, *v17, v16, (__int64)&v19);
continue;
default:
printf("unknown msg type %d\n", (unsigned int)buf[0]);
result = 1LL;
break;
}
}
else
{
puts("subprocess exiting");
result = 0LL;
}
return result;
}
}

original_code:
int handle_connection(int sock, struct sockaddr_in *addr)
{
    int8_t cmd_type;
    struct fs_open_command cmd_open;
    struct fs_command cmd;
    int read_bytes, written_bytes, ret, file;
    char buffer[10240], *bufp = 0;
    while (1 == recv(sock, &cmd_type, sizeof(cmd_type), MSG_PEEK)) {
        switch (cmd_type) {
        case FSMSG_OPEN:
            recv(sock, &cmd_open, sizeof(cmd_open), MSG_WAITALL);
            cmd_open.filename[255] = 0;
            printf("received FSMSG_OPEN, filename = %s\n", cmd_open.filename);
            file = open(cmd_open.filename, cmd_open.base_command.arg1, 0660);
            send_reponse(sock, (*__errno_location ()), file);
            break;
        case FSMSG_WRITE:
            recv(sock, &cmd, sizeof(cmd), MSG_WAITALL);
            printf("received FSMSG_WRITE, fd = %i\n", cmd.fd);
            ret = 0;
            while (0 < (read_bytes = read(sock, buffer, ( ((10240) < (cmd.arg1)) ? (10240) : (cmd.arg1) )))) {
                ret += read_bytes;
                cmd.arg1 -= read_bytes;
                write(cmd.fd, buffer, read_bytes);
            }
            send_reponse(sock, (*__errno_location ()), ret);
            break;
        case FSMSG_READ:
            recv(sock, &cmd, sizeof(cmd), MSG_WAITALL);
            printf("received FSMSG_READ, fd = %i\n", cmd.fd);
            read_bytes = read(cmd.fd, buffer, ( ((10240) < (cmd.arg1)) ? (10240) : (cmd.arg1) ));
            send_reponse(sock, (*__errno_location ()), read_bytes);
            bufp = buffer;
            while (read_bytes) {
                written_bytes = write(sock, bufp, read_bytes);
                read_bytes -= written_bytes;
                bufp += written_bytes;
            }
            break;
        case FSMSG_LSEEK:
            recv(sock, &cmd, sizeof(cmd), MSG_WAITALL);
            printf("received FSMSG_LSEEK, fd = %i\n", cmd.fd);
            ret = lseek(cmd.fd, cmd.arg1, cmd.arg2);
            send_reponse(sock, (*__errno_location ()), ret);
            break;
        case FSMSG_CLOSE:
            recv(sock, &cmd, sizeof(cmd), MSG_WAITALL);
            printf("received FSMSG_CLOSE, fd = %i\n", cmd.fd);
            ret = close(cmd.fd);
            send_reponse(sock, (*__errno_location ()), ret);
            break;
        case FSMSG_STAT:
            recv(sock, &cmd, sizeof(cmd), MSG_WAITALL);
            printf("received FSMSG_STAT, fd = %i\n", cmd.fd);
            struct stat sb;
            ret = fstat(cmd.fd, &sb);
            send_stat_reponse(sock, (*__errno_location ()), ret, &sb);
            break;
        default:
            printf("unknown msg type %d\n", cmd_type);
            return 1;
        }
    }
    printf("subprocess exiting\n");
    return 0;
}

canonical_original_code:
int handle_connection(int sock, struct sockaddr_in *addr)
{
    char cmd_type;
    struct fs_open_command cmd_open;
    struct fs_command cmd;
    int read_bytes; int written_bytes; int ret; int file;
    char buffer[10240]; char *bufp = 0;
    while (1 == recv(sock, &cmd_type, sizeof(cmd_type), MSG_PEEK)) {
        switch (cmd_type) {
        case FSMSG_OPEN:
            recv(sock, &cmd_open, sizeof(cmd_open), MSG_WAITALL);
            cmd_open.filename[255] = 0;
            printf("received FSMSG_OPEN, filename = %s\n", cmd_open.filename);
            file = open(cmd_open.filename, cmd_open.base_command.arg1, 0660);
            send_reponse(sock, (*__errno_location ()), file);
            break;
        case FSMSG_WRITE:
            recv(sock, &cmd, sizeof(cmd), MSG_WAITALL);
            printf("received FSMSG_WRITE, fd = %i\n", cmd.fd);
            ret = 0;
            while (0 < (read_bytes = read(sock, buffer, ( ((10240) < (cmd.arg1)) ? (10240) : (cmd.arg1) )))) {
                ret += read_bytes;
                cmd.arg1 -= read_bytes;
                write(cmd.fd, buffer, read_bytes);
            }
            send_reponse(sock, (*__errno_location ()), ret);
            break;
        case FSMSG_READ:
            recv(sock, &cmd, sizeof(cmd), MSG_WAITALL);
            printf("received FSMSG_READ, fd = %i\n", cmd.fd);
            read_bytes = read(cmd.fd, buffer, ( ((10240) < (cmd.arg1)) ? (10240) : (cmd.arg1) ));
            send_reponse(sock, (*__errno_location ()), read_bytes);
            bufp = buffer;
            while (read_bytes) {
                written_bytes = write(sock, bufp, read_bytes);
                read_bytes -= written_bytes;
                bufp += written_bytes;
            }
            break;
        case FSMSG_LSEEK:
            recv(sock, &cmd, sizeof(cmd), MSG_WAITALL);
            printf("received FSMSG_LSEEK, fd = %i\n", cmd.fd);
            ret = lseek(cmd.fd, cmd.arg1, cmd.arg2);
            send_reponse(sock, (*__errno_location ()), ret);
            break;
        case FSMSG_CLOSE:
            recv(sock, &cmd, sizeof(cmd), MSG_WAITALL);
            printf("received FSMSG_CLOSE, fd = %i\n", cmd.fd);
            ret = close(cmd.fd);
            send_reponse(sock, (*__errno_location ()), ret);
            break;
        case FSMSG_STAT:
            recv(sock, &cmd, sizeof(cmd), MSG_WAITALL);
            printf("received FSMSG_STAT, fd = %i\n", cmd.fd);
            struct stat sb;
            ret = fstat(cmd.fd, &sb);
            send_stat_reponse(sock, (*__errno_location ()), ret, &sb);
            break;
        default:
            printf("unknown msg type %d\n", cmd_type);
            return 1;
        }
    }
    printf("subprocess exiting\n");
    return 0;
}

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

variable_types: struct fs_command { char type; int fd; }

variable_types: ret int

variable_types: file int

variable_types:
buffer
char [10240]

variable_types:
bufp
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]; }