original_code:
int
scan_scaled(char *scaled, long long *result)
{
 char *p = scaled;
 int sign = 0;
 unsigned int i, ndigits = 0, fract_digits = 0;
 long long scale_fact = 1, whole = 0, fpart = 0;
 while ((((*p) & ~0x7f) == 0) && ((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISspace))
  ++p;
 while (*p == '-' || *p == '+') {
  if (*p == '-') {
   if (sign) {
    (*__errno_location ()) = 22;
    return -1;
   }
   sign = -1;
   ++p;
  } else if (*p == '+') {
   if (sign) {
    (*__errno_location ()) = 22;
    return -1;
   }
   sign = +1;
   ++p;
  }
 }
 for (; (((*p) & ~0x7f) == 0) && (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISdigit) || *p=='.'); ++p) {
  if (*p == '.') {
   if (fract_digits > 0) {
    (*__errno_location ()) = 22;
    return -1;
   }
   fract_digits = 1;
   continue;
  }
  i = (*p) - '0';
  if (fract_digits > 0) {
   if (fract_digits >= ((sizeof(units)/sizeof(units[0])) * 3)-1)
    continue;
   fract_digits++;
   fpart *= 10;
   fpart += i;
  } else {
   if (++ndigits >= ((sizeof(units)/sizeof(units[0])) * 3)) {
    (*__errno_location ()) = 34;
    return -1;
   }
   whole *= 10;
   whole += i;
  }
 }
 if (sign) {
  whole *= sign;
  fpart *= sign;
 }
 if (!*p) {
  *result = whole;
  return 0;
 }
 for (i = 0; i < (sizeof(units)/sizeof(units[0])); i++) {
  if (*p == scale_chars[i] ||
   *p == tolower(scale_chars[i])) {
   if (((*__ctype_b_loc ())[(int) ((*(p+1)))] & (unsigned short int) _ISalnum)) {
    (*__errno_location ()) = 22;
    return -1;
   }
   scale_fact = scale_factors[i];
   whole *= scale_fact;
   while (fpart >= 0x7fffffffffffffffLL / scale_fact) {
    fpart /= 10;
    fract_digits--;
   }
   fpart *= scale_fact;
   if (fract_digits > 0) {
    for (i = 0; i < fract_digits -1; i++)
     fpart /= 10;
   }
   whole += fpart;
   *result = whole;
   return 0;
  }
 }
 (*__errno_location ()) = 34;
 return -1;
}

canonical_original_code:
int scan_scaled(char *scaled, long long *result)
{
 char *p = scaled;
 int sign = 0;
 unsigned int i; unsigned int ndigits = 0; unsigned int fract_digits = 0;
 long long scale_fact = 1; long long whole = 0; long long fpart = 0;
 while ((((*p) & ~0x7f) == 0) && ((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short) _ISspace))
  ++p;
 while (*p == '-' || *p == '+') {
  if (*p == '-') {
   if (sign) {
    (*__errno_location ()) = 22;
    return -1;
   }
   sign = -1;
   ++p;
  } else if (*p == '+') {
   if (sign) {
    (*__errno_location ()) = 22;
    return -1;
   }
   sign = +1;
   ++p;
  }
 }
 for (; (((*p) & ~0x7f) == 0) && (((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short) _ISdigit) || *p=='.'); ++p) {
  if (*p == '.') {
   if (fract_digits > 0) {
    (*__errno_location ()) = 22;
    return -1;
   }
   fract_digits = 1;
   continue;
  }
  i = (*p) - '0';
  if (fract_digits > 0) {
   if (fract_digits >= ((sizeof(units)/sizeof(units[0])) * 3)-1)
    continue;
   fract_digits++;
   fpart *= 10;
   fpart += i;
  } else {
   if (++ndigits >= ((sizeof(units)/sizeof(units[0])) * 3)) {
    (*__errno_location ()) = 34;
    return -1;
   }
   whole *= 10;
   whole += i;
  }
 }
 if (sign) {
  whole *= sign;
  fpart *= sign;
 }
 if (!*p) {
  *result = whole;
  return 0;
 }
 for (i = 0; i < (sizeof(units)/sizeof(units[0])); i++) {
  if (*p == scale_chars[i] ||
   *p == tolower(scale_chars[i])) {
   if (((*__ctype_b_loc ())[(int) ((*(p+1)))] & (unsigned short) _ISalnum)) {
    (*__errno_location ()) = 22;
    return -1;
   }
   scale_fact = scale_factors[i];
   whole *= scale_fact;
   while (fpart >= 0x7fffffffffffffffLL / scale_fact) {
    fpart /= 10;
    fract_digits--;
   }
   fpart *= scale_fact;
   if (fract_digits > 0) {
    for (i = 0; i < fract_digits -1; i++)
     fpart /= 10;
   }
   whole += fpart;
   *result = whole;
   return 0;
  }
 }
 (*__errno_location ()) = 34;
 return -1;
}

variable_types:
scaled
char *

variable_types:
result
long long *

variable_types:
p
char *

variable_types: sign int

variable_types:
i
unsigned int

variable_types:
ndigits
unsigned int

variable_types:
whole
long long

variable_types:
fpart
long long


repo: yzzyx/codewiki

original_code:
int
fmt_scaled(long long number, char *result)
{
 long long abval, fract = 0;
 unsigned int i;
 unit_type unit = NONE;
 abval = llabs(number);
 if (abval < 0 || abval / 1024 >= scale_factors[(sizeof(units)/sizeof(units[0]))-1]) {
  (*__errno_location ()) = 34;
  return -1;
 }
 for (i = 0; i < (sizeof(units)/sizeof(units[0])); i++) {
  if (abval/1024 < scale_factors[i]) {
   unit = units[i];
   fract = (i == 0) ? 0 : abval % scale_factors[i];
   number /= scale_factors[i];
   if (i > 0)
    fract /= scale_factors[i - 1];
   break;
  }
 }
 fract = (10 * fract + 512) / 1024;
 if (fract == 10) {
  if (number >= 0)
   number++;
  else
   number--;
  fract = 0;
 }
 if (number == 0)
  strlcpy(result, "0B", 7);
 else if (unit == NONE || number >= 100 || number <= -100) {
  if (fract >= 5) {
   if (number >= 0)
    number++;
   else
    number--;
  }
  (void)snprintf(result, 7, "%lld%c",
   number, scale_chars[unit]);
 } else
  (void)snprintf(result, 7, "%lld.%1lld%c",
   number, fract, scale_chars[unit]);
 return 0;
}

canonical_original_code:
int fmt_scaled(long long number, char *result)
{
 long long abval; long long fract = 0;
 unsigned int i;
 enum unit_type unit = NONE;
 abval = llabs(number);
 if (abval < 0 || abval / 1024 >= scale_factors[(sizeof(units)/sizeof(units[0]))-1]) {
  (*__errno_location ()) = 34;
  return -1;
 }
 for (i = 0; i < (sizeof(units)/sizeof(units[0])); i++) {
  if (abval/1024 < scale_factors[i]) {
   unit = units[i];
   fract = (i == 0) ? 0 : abval % scale_factors[i];
   number /= scale_factors[i];
   if (i > 0)
    fract /= scale_factors[i - 1];
   break;
  }
 }
 fract = (10 * fract + 512) / 1024;
 if (fract == 10) {
  if (number >= 0)
   number++;
  else
   number--;
  fract = 0;
 }
 if (number == 0)
  strlcpy(result, "0B", 7);
 else if (unit == NONE || number >= 100 || number <= -100) {
  if (fract >= 5) {
   if (number >= 0)
    number++;
   else
    number--;
  }
  (void)snprintf(result, 7, "%lld%c",
   number, scale_chars[unit]);
 } else
  (void)snprintf(result, 7, "%lld.%1lld%c",
   number, fract, scale_chars[unit]);
 return 0;
}

variable_types:
number
long long

variable_types:
result
char *

variable_types:
abval
long long

variable_types:
fract
long long

variable_types: enum unit_type { NONE=0, KILO=1, MEGA=2, GIGA=3, TERA=4, PETA=5, EXA=6 }

return_type: int


decompiled_code:
void *__fastcall mime_get_key(size_t a1, const char *a2)
{
void *v2; // rbp
char v3; // cl
_BYTE *v4; // r12
_BYTE *v5; // rax
int v6; // ebx

while ( 1 )
{
v2 = strstr((const char *)a1, a2);
if ( !v2 )
break;
a1 = (size_t)v2 + strlen(a2);
if ( *(_BYTE *)a1 == 61 )
{
v3 = *(_BYTE *)(a1 + 1);
v4 = (_BYTE *)(a1 + 2);
if ( v3 != 34 )
{
v4 = (_BYTE *)(a1 + 1);
v3 = 0;
}
if ( !*v4 || v3 == *v4 )
{
LODWORD(v5) = (_DWORD)v4;
}
else
{
v5 = v4;
do
++v5;
while ( *v5 && *v5 != v3 );
}
v6 = (_DWORD)v5 - (_DWORD)v4;
v2 = malloc((int)v5 - (int)v4 + 1);
memcpy(v2, v4, v6);
*((_BYTE *)v2 + v6) = 0;
return v2;
}
}
return v2;
}

canonical_decompiled_code:
void *__fastcall func0(size_t a1, const char *a2)
{
void *v2; // rbp
char v3; // cl
_BYTE *v4; // r12
_BYTE *v5; // rax
int v6; // ebx

while ( 1 )
{
v2 = strstr((const char *)a1, a2);
if ( !v2 )
break;
a1 = (size_t)v2 + strlen(a2);
if ( *(_BYTE *)a1 == 61 )
{
v3 = *(_BYTE *)(a1 + 1);
v4 = (_BYTE *)(a1 + 2);
if ( v3 != 34 )
{
v4 = (_BYTE *)(a1 + 1);
v3 = 0;
}
if ( !*v4 || v3 == *v4 )
{
LODWORD(v5) = (_DWORD)v4;
}
else
{
v5 = v4;
do
++v5;
while ( *v5 && *v5 != v3 );
}
v6 = (_DWORD)v5 - (_DWORD)v4;
v2 = malloc((int)v5 - (int)v4 + 1);
memcpy(v2, v4, v6);
*((_BYTE *)v2 + v6) = 0;
return v2;
}
}
return v2;
}

original_code:
char *
mime_get_key(const char *buf, const char *key)
{
 char *ptr, *st_ptr;
 char *value;
 char end_ch;
 int len;
 end_ch = '\0';
 value = ((void *)0);
 ptr = (char *)buf;
 for (;;) {
  ptr = strstr(ptr, key);
  if (ptr == ((void *)0))
   break;
  ptr += strlen(key);
  if (*ptr != '=')
   continue;
  ptr ++;
  if (*ptr == '"') {
   end_ch = '"';
   ptr ++;
  }
  st_ptr = ptr;
  for (; *ptr != end_ch && *ptr != '\0'; ptr ++);
  len = ptr - st_ptr;
  value = malloc(len+1);
  memcpy(value, st_ptr, len);
  value[len] = '\0';
  break;
 }
 return (value);
}

canonical_original_code:
char *mime_get_key(char *buf, char *key)
{
 char *ptr; char *st_ptr;
 char *value;
 char end_ch;
 int len;
 end_ch = '\0';
 value = ((void *)0);
 ptr = (char *)buf;
 for (;;) {
  ptr = strstr(ptr, key);
  if (ptr == ((void *)0))
   break;
  ptr += strlen(key);
  if (*ptr != '=')
   continue;
  ptr ++;
  if (*ptr == '"') {
   end_ch = '"';
   ptr ++;
  }
  st_ptr = ptr;
  for (; *ptr != end_ch && *ptr != '\0'; ptr ++);
  len = ptr - st_ptr;
  value = malloc(len+1);
  memcpy(value, st_ptr, len);
  value[len] = '\0';
  break;
 }
 return (value);
}

variable_types:
buf
char *

variable_types:
key
char *

variable_types:
ptr
char *

variable_types:
value
char *

variable_types: len int

return_type: char *


decompiled_code:
void *mime_getline()
{
void *v0; // r13
__int64 v1; // rbp
int v2; // er14
int v3; // ebx

v0 = malloc(0x400uLL);
v1 = 0LL;
v2 = 1024;
while ( 1 )
{
v3 = webserver_getc();
if ( (unsigned int)webserver_eof() )
{
free(v0);
return 0LL;
}
if ( v3 == 10 || v3 == 13 )
break;
*((_BYTE *)v0 + v1) = v3;
if ( v2 == (_DWORD)v1 + 1 )
{
v2 += 1024;
v0 = realloc(v0, v2);
}
++v1;
}
*((_BYTE *)v0 + (int)v1) = 0;
return v0;
}

canonical_decompiled_code:
void *func1()
{
void *v0; // r13
__int64 v1; // rbp
int v2; // er14
int v3; // ebx

v0 = malloc(0x400uLL);
v1 = 0LL;
v2 = 1024;
while ( 1 )
{
v3 = webserver_getc();
if ( (unsigned int)webserver_eof() )
{
free(v0);
return 0LL;
}
if ( v3 == 10 || v3 == 13 )
break;
*((_BYTE *)v0 + v1) = v3;
if ( v2 == (_DWORD)v1 + 1 )
{
v2 += 1024;
v0 = realloc(v0, v2);
}
++v1;
}
*((_BYTE *)v0 + (int)v1) = 0;
return v0;
}

original_code:
char *
mime_getline()
{
 char *buf;
 int bufsz;
 int ch, nb;
 bufsz = 1024;
 nb = 0;
 buf = malloc(bufsz);
 for (;;) {
  ch = webserver_getc();
  if (webserver_eof()) {
   free(buf);
   return (((void *)0));
  }
  if (ch == '\n' || ch == '\r')
   break;
  buf[nb++] = ch;
  if (nb == bufsz) {
   bufsz += 1024;
   buf = realloc(buf, bufsz);
  }
 }
 buf[nb] = '\0';
 return (buf);
}

canonical_original_code:
char *mime_getline()
{
 char *buf;
 int bufsz;
 int ch; int nb;
 bufsz = 1024;
 nb = 0;
 buf = malloc(bufsz);
 for (;;) {
  ch = webserver_getc();
  if (webserver_eof()) {
   free(buf);
   return (((void *)0));
  }
  if (ch == '\n' || ch == '\r')
   break;
  buf[nb++] = ch;
  if (nb == bufsz) {
   bufsz += 1024;
   buf = realloc(buf, bufsz);
  }
 }
 buf[nb] = '\0';
 return (buf);
}

variable_types: bufsz int

variable_types: ch int

variable_types: nb int


decompiled_code:
int __fastcall mime_getdata(const char *a1, char **a2, _DWORD *a3)
{
int v3; // er15
char *v4; // r13
__int64 v5; // rbx
int v6; // er14
int result; // eax
char v8; // bp
int v9; // er12
const char *v10; // rbp
char s2[128]; // [rsp+20h] [rbp-138h] BYREF
char s[184]; // [rsp+A0h] [rbp-B8h] BYREF

snprintf(s, 0x80uLL, "\r\n%s\r\n", a1);
snprintf(s2, 0x80uLL, "\r\n%s--", a1);
v3 = strlen(s);
v4 = (char *)malloc(0x400uLL);
v5 = 0LL;
v6 = 1024;
while ( 1 )
{
v8 = webserver_getc();
if ( (unsigned int)webserver_eof() )
{
v4[(int)v5] = 0;
printf("buf: %s\n", v4);
free(v4);
return -1;
}
v9 = v5 + 1;
v4[v5] = v8;
if ( v3 < (int)v5 + 1 )
{
v10 = &v4[1LL - v3 + v5];
result = strncmp(v10, s, v3);
if ( !result )
goto LABEL_11;
if ( !strncmp(v10, s2, v3) )
break;
}
if ( v6 == v9 )
{
v6 += 1024;
v4 = (char *)realloc(v4, v6);
}
++v5;
}
result = 1;
LABEL_11:
*v10 = 0;
*a2 = v4;
*a3 = v9;
return result;
}

canonical_decompiled_code:
int __fastcall func2(const char *a1, char **a2, _DWORD *a3)
{
int v3; // er15
char *v4; // r13
__int64 v5; // rbx
int v6; // er14
int result; // eax
char v8; // bp
int v9; // er12
const char *v10; // rbp
char s2[128]; // [rsp+20h] [rbp-138h] BYREF
char s[184]; // [rsp+A0h] [rbp-B8h] BYREF

snprintf(s, 0x80uLL, "\r\n%s\r\n", a1);
snprintf(s2, 0x80uLL, "\r\n%s--", a1);
v3 = strlen(s);
v4 = (char *)malloc(0x400uLL);
v5 = 0LL;
v6 = 1024;
while ( 1 )
{
v8 = webserver_getc();
if ( (unsigned int)webserver_eof() )
{
v4[(int)v5] = 0;
printf("buf: %s\n", v4);
free(v4);
return -1;
}
v9 = v5 + 1;
v4[v5] = v8;
if ( v3 < (int)v5 + 1 )
{
v10 = &v4[1LL - v3 + v5];
result = strncmp(v10, s, v3);
if ( !result )
goto LABEL_11;
if ( !strncmp(v10, s2, v3) )
break;
}
if ( v6 == v9 )
{
v6 += 1024;
v4 = (char *)realloc(v4, v6);
}
++v5;
}
result = 1;
LABEL_11:
*v10 = 0;
*a2 = v4;
*a3 = v9;
return result;
}

original_code:
int
mime_getdata(const char *boundary, char **result, int *result_sz)
{
 char *buf;
 int bufsz;
 char next_boundary[128];
 char end_boundary[128];
 int boundary_len;
 int nb, ch, retval;
 snprintf(next_boundary, sizeof next_boundary, "\r\n%s\r\n", boundary);
 snprintf(end_boundary, sizeof end_boundary, "\r\n%s--", boundary);
 bufsz = 1024;
 nb = 0;
 boundary_len = strlen(next_boundary);
 buf = malloc(bufsz);
 for (;;) {
  ch = webserver_getc();
  if (webserver_eof()) {
   buf[nb] = '\0';
   printf("buf: %s\n",buf);
   free(buf);
   return (-1);
  }
  buf[nb++] = ch;
  if (nb > boundary_len) {
   if (strncmp(buf + nb - boundary_len,
    next_boundary, boundary_len) == 0) {
    retval = 0;
    break;
   } else if (strncmp(buf + nb - boundary_len,
     end_boundary, boundary_len) == 0) {
    retval = 1;
    break;
   }
  }
  if (nb == bufsz) {
   bufsz += 1024;
   buf = realloc(buf, bufsz);
  }
 }
 buf[nb - boundary_len] = '\0';
 *result = buf;
 *result_sz = nb;
 return (retval);
}

canonical_original_code:
int mime_getdata(char *boundary, char **result, int *result_sz)
{
 char *buf;
 int bufsz;
 char next_boundary[128];
 char end_boundary[128];
 int boundary_len;
 int nb; int ch; int retval;
 snprintf(next_boundary, sizeof next_boundary, "\r\n%s\r\n", boundary);
 snprintf(end_boundary, sizeof end_boundary, "\r\n%s--", boundary);
 bufsz = 1024;
 nb = 0;
 boundary_len = strlen(next_boundary);
 buf = malloc(bufsz);
 for (;;) {
  ch = webserver_getc();
  if (webserver_eof()) {
   buf[nb] = '\0';
   printf("buf: %s\n",buf);
   free(buf);
   return (-1);
  }
  buf[nb++] = ch;
  if (nb > boundary_len) {
   if (strncmp(buf + nb - boundary_len,
    next_boundary, boundary_len) == 0) {
    retval = 0;
    break;
   } else if (strncmp(buf + nb - boundary_len,
     end_boundary, boundary_len) == 0) {
    retval = 1;
    break;
   }
  }
  if (nb == bufsz) {
   bufsz += 1024;
   buf = realloc(buf, bufsz);
  }
 }
 buf[nb - boundary_len] = '\0';
 *result = buf;
 *result_sz = nb;
 return (retval);
}

variable_types:
boundary
char *

variable_types:
result
char **

variable_types: retval int


decompiled_code:
__int64 __fastcall mime_parse(_QWORD *a1, const char *a2)
{
void *v2; // rsp
unsigned int v4; // ebx
_QWORD *v5; // rcx
char *v6; // rax
const char *v7; // r12
char *v8; // r14
size_t v9; // rbx
char *v10; // r13
char *v11; // rax
char *v12; // rbx
const char *v13; // rbx
int v14; // er13
char *v15; // r15
_QWORD *v16; // rax
__int64 v17; // rdx
const char *v18; // [rsp+0h] [rbp-60h] BYREF
_QWORD *v19; // [rsp+8h] [rbp-58h]
char *v20; // [rsp+18h] [rbp-48h] BYREF
char *haystack; // [rsp+20h] [rbp-40h] BYREF
int v22[13]; // [rsp+2Ch] [rbp-34h] BYREF

v19 = a1;
v2 = alloca(strlen(a2) + 11);
v18 = (const char *)&v18;
sprintf((char *)&v18, "--%s", a2);
do
{
v4 = mime_getdata((const char *)&unk_4EF, &haystack, v22);
if ( v4 == -1 )
{
perror("mime_parse()");
return v4;
}
printf("Headers: %s\n", haystack);
v6 = strstr(haystack, "Content-Disposition: ");
v7 = v6;
v8 = v6;
if ( v6 )
{
v9 = (size_t)(v6 + 21);
v7 = (const char *)mime_get_key((size_t)(v6 + 21), "name");
v8 = (char *)mime_get_key(v9, "filename");
printf("name: %s\n", v7);
printf("filename: %s\n", v8);
}
v10 = haystack;
v11 = strstr(haystack, "Content-Type: ");
v12 = v11;
if ( v11 )
{
v13 = v11 + 14;
v22[0] = (unsigned int)strstr(v11 + 14, "\r\n") - ((_DWORD)v11 + 14);
if ( v22[0] < 0 )
v22[0] = strlen(v13);
v12 = strndup(v13, v22[0]);
}
free(v10);
puts("End of headers");
v14 = mime_getdata(v18, &v20, v22);
if ( v14 == -1 )
{
perror("mime_getdata():");
return 0;
}
printf("data: %s\n", v20);
v15 = v20;
if ( v20 )
{
v16 = malloc(0x28uLL);
v16[2] = v7;
v16[3] = v15;
*((_DWORD *)v16 + 8) = v22[0];
v17 = *v19;
*v16 = *v19;
if ( v17 )
*(_QWORD *)(v17 + 8) = v16;
v5 = v19;
*v19 = v16;
v16[1] = v5;
}
if ( v8 )
free(v8);
if ( v12 )
free(v12);
}
while ( v14 != 1 );
return 0;
}

canonical_decompiled_code:
__int64 __fastcall func3(_QWORD *a1, const char *a2)
{
void *v2; // rsp
unsigned int v4; // ebx
_QWORD *v5; // rcx
char *v6; // rax
const char *v7; // r12
char *v8; // r14
size_t v9; // rbx
char *v10; // r13
char *v11; // rax
char *v12; // rbx
const char *v13; // rbx
int v14; // er13
char *v15; // r15
_QWORD *v16; // rax
__int64 v17; // rdx
const char *v18; // [rsp+0h] [rbp-60h] BYREF
_QWORD *v19; // [rsp+8h] [rbp-58h]
char *v20; // [rsp+18h] [rbp-48h] BYREF
char *haystack; // [rsp+20h] [rbp-40h] BYREF
int v22[13]; // [rsp+2Ch] [rbp-34h] BYREF

v19 = a1;
v2 = alloca(strlen(a2) + 11);
v18 = (const char *)&v18;
sprintf((char *)&v18, "--%s", a2);
do
{
v4 = func2((const char *)&unk_4EF, &haystack, v22);
if ( v4 == -1 )
{
perror("mime_parse()");
return v4;
}
printf("Headers: %s\n", haystack);
v6 = strstr(haystack, "Content-Disposition: ");
v7 = v6;
v8 = v6;
if ( v6 )
{
v9 = (size_t)(v6 + 21);
v7 = (const char *)func0((size_t)(v6 + 21), "name");
v8 = (char *)func0(v9, "filename");
printf("name: %s\n", v7);
printf("filename: %s\n", v8);
}
v10 = haystack;
v11 = strstr(haystack, "Content-Type: ");
v12 = v11;
if ( v11 )
{
v13 = v11 + 14;
v22[0] = (unsigned int)strstr(v11 + 14, "\r\n") - ((_DWORD)v11 + 14);
if ( v22[0] < 0 )
v22[0] = strlen(v13);
v12 = strndup(v13, v22[0]);
}
free(v10);
puts("End of headers");
v14 = func2(v18, &v20, v22);
if ( v14 == -1 )
{
perror("mime_getdata():");
return 0;
}
printf("data: %s\n", v20);
v15 = v20;
if ( v20 )
{
v16 = malloc(0x28uLL);
v16[2] = v7;
v16[3] = v15;
*((_DWORD *)v16 + 8) = v22[0];
v17 = *v19;
*v16 = *v19;
if ( v17 )
*(_QWORD *)(v17 + 8) = v16;
v5 = v19;
*v19 = v16;
v16[1] = v5;
}
if ( v8 )
free(v8);
if ( v12 )
free(v12);
}
while ( v14 != 1 );
return 0;
}

original_code:
int
mime_parse(struct cgi_var_list *cgi_list, const char *boundary)
{
 char *part_name,
    *part_filename,
    *part_type;
 int len;
 struct cgi_var *cv;
 char *headers;
 char *ptr;
 char *data;
 int ret;
 char *actual_boundary;
 actual_boundary = __builtin_alloca (strlen(boundary)+3);
 sprintf(actual_boundary, "--%s", boundary);
 for (;;) {
  part_name = ((void *)0);
  part_filename = ((void *)0);
  part_type = ((void *)0);
  ret = mime_getdata("", &headers, &len);
  if (ret == -1) {
   perror("mime_parse()");
   return (-1);
  }
  printf("Headers: %s\n", headers);
  if ((ptr = strstr(headers, "Content-Disposition: ")) != ((void *)0)) {
   ptr += strlen("Content-Disposition: ");
   part_name = mime_get_key(ptr, "name");
   part_filename = mime_get_key(ptr, "filename");
   printf("name: %s\n", part_name);
   printf("filename: %s\n", part_filename);
  }
  if ((ptr = strstr(headers, "Content-Type: ")) != ((void *)0)) {
   ptr += strlen("Content-Type: ");
   len = strstr(ptr, "\r\n") - ptr;
   if (len < 0)
    len = strlen(ptr);
   part_type = strndup(ptr, len);
  }
  free(headers);
  printf("End of headers\n");
  ret = mime_getdata(actual_boundary, &data, &len);
  if (ret == -1) {
   perror("mime_getdata():");
   return (0);
  }
  printf("data: %s\n", data);
  if (data != ((void *)0)) {
   cv = malloc(sizeof *cv);
   cv->name = part_name;
   cv->value = data;
   cv->value_len = len;
   do { if (((cv)->entry.le_next = (cgi_list)->lh_first) != ((void *)0)) (cgi_list)->lh_first->entry.le_prev = &(cv)->entry.le_next; (cgi_list)->lh_first = (cv); (cv)->entry.le_prev = &(cgi_list)->lh_first; } while ( 0);
  }
  if (part_filename)
   free(part_filename);
  if (part_type)
   free(part_type);
  if (ret == 1)
   break;
 }
 return (0);
}

canonical_original_code:
int mime_parse(struct cgi_var_list *cgi_list, char *boundary)
{
 char *part_name; char *part_filename; char *part_type;
 int len;
 struct cgi_var *cv;
 char *headers;
 char *ptr;
 char *data;
 int ret;
 char *actual_boundary;
 actual_boundary = __builtin_alloca (strlen(boundary)+3);
 sprintf(actual_boundary, "--%s", boundary);
 for (;;) {
  part_name = ((void *)0);
  part_filename = ((void *)0);
  part_type = ((void *)0);
  ret = mime_getdata("", &headers, &len);
  if (ret == -1) {
   perror("mime_parse()");
   return (-1);
  }
  printf("Headers: %s\n", headers);
  if ((ptr = strstr(headers, "Content-Disposition: ")) != ((void *)0)) {
   ptr += strlen("Content-Disposition: ");
   part_name = mime_get_key(ptr, "name");
   part_filename = mime_get_key(ptr, "filename");
   printf("name: %s\n", part_name);
   printf("filename: %s\n", part_filename);
  }
  if ((ptr = strstr(headers, "Content-Type: ")) != ((void *)0)) {
   ptr += strlen("Content-Type: ");
   len = strstr(ptr, "\r\n") - ptr;
   if (len < 0)
    len = strlen(ptr);
   part_type = strndup(ptr, len);
  }
  free(headers);
  printf("End of headers\n");
  ret = mime_getdata(actual_boundary, &data, &len);
  if (ret == -1) {
   perror("mime_getdata():");
   return (0);
  }
  printf("data: %s\n", data);
  if (data != ((void *)0)) {
   cv = malloc(sizeof *cv);
   cv->name = part_name;
   cv->value = data;
   cv->value_len = len;
   do { if (((cv)->entry.le_next = (cgi_list)->lh_first) != ((void *)0)) (cgi_list)->lh_first->entry.le_prev = &(cv)->entry.le_next; (cgi_list)->lh_first = (cv); (cv)->entry.le_prev = &(cgi_list)->lh_first; } while ( 0);
  }
  if (part_filename)
   free(part_filename);
  if (part_type)
   free(part_type);
  if (ret == 1)
   break;
 }
 return (0);
}

variable_types: struct cgi_var_list { struct cgi_var { struct { struct cgi_var *le_next; struct cgi_var **le_prev; } entry; char *name; char *value; int value_len; } *lh_first; } *

variable_types:
headers
char *

variable_types:
data
char *

variable_types: ret int

user_defined_types: struct cgi_var_list { struct cgi_var *lh_first; }


repo: zPatrickz/Game

decompiled_code:
__int64 init_game()
{
__int64 result; // rax

result = (unsigned int)((_DWORD)pool + 40);
do
{
*(_QWORD *)(result - 8) = result;
result += 40LL;
}
while ( result != (_DWORD)pool + 40000 );
pool[4999] = 0LL;
top = (__int64)pool;
active = 0LL;
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 result; // rax

result = (unsigned int)((_DWORD)pool + 40);
do
{
*(_QWORD *)(result - 8) = result;
result += 40LL;
}
while ( result != (_DWORD)pool + 40000 );
pool[4999] = 0LL;
top = (__int64)pool;
active = 0LL;
return result;
}

original_code:
void
init_game(void) {
 int i;
 for (i = 0; i < 1000 - 1; i ++) {
  pool[i].next = pool + (i + 1);
 }
 pool[1000 - 1].next = ((void*)0);
 top = pool;
 active = ((void*)0);
}

canonical_original_code:
void init_game() {
 int i;
 for (i = 0; i < 1000 - 1; i ++) {
  pool[i].next = pool + (i + 1);
 }
 pool[1000 - 1].next = ((void *)0);
 top = pool;
 active = ((void *)0);
}

variable_types: i int

return_type: void


original_code:
void
key_stroke(int code) {
 struct Firework *ptr;
 int i;
 int x = rand() % 100 + 50;
 int y = rand() % 160 + 80;
 int color = rand() % 256 & 0x6c;
 if (code > 128) return;
 for (i = 0; i < 10; i ++) {
  if (top == ((void*)0)) {
   return;
  }
  ptr = top;
  top = top->next;
  ptr->x = x;
  ptr->y = y;
  ptr->vx = vx[i];
  ptr->vy = vy[i];
  ptr->color = color;
  ptr->timestamp = 0;
  ptr->next = active;
  ptr->prev = ((void*)0);
  if (active != ((void*)0)) active->prev = ptr;
  active = ptr;
 }
}

canonical_original_code:
void key_stroke(int code) {
 struct Firework *ptr;
 int i;
 int x = rand() % 100 + 50;
 int y = rand() % 160 + 80;
 int color = rand() % 256 & 0x6c;
 if (code > 128) return;
 for (i = 0; i < 10; i ++) {
  if (top == ((void *)0)) {
   return;
  }
  ptr = top;
  top = top->next;
  ptr->x = x;
  ptr->y = y;
  ptr->vx = vx[i];
  ptr->vy = vy[i];
  ptr->color = color;
  ptr->timestamp = 0;
  ptr->next = active;
  ptr->prev = ((void *)0);
  if (active != ((void *)0)) active->prev = ptr;
  active = ptr;
 }
}

variable_types: code int

variable_types: struct Firework { int x; float vx; int color; int timestamp; struct Firework *prev; } *

variable_types: x int

variable_types: y int

variable_types: color int


original_code:
void
start_frame(void) {
 static int count = 0;
 struct Firework *ptr;
 int i;
 count = (count + 1) % 50;
 if (count == 0) {
  key_stroke(0);
 }
 for (ptr = active; ptr != ((void*)0); ptr = ptr->next) {
  for (i = ptr->timestamp; i >= 0 && i >= ptr->timestamp - (15 - 1); i --) {
   draw_pixel( get_x(ptr, i), get_y(ptr, i), 0 );
  }
 }
 ptr = active;
 while (ptr != ((void*)0)) {
  struct Firework *prev = ptr->prev, *next = ptr->next;
  ptr->timestamp ++;
  if (ptr->timestamp > 200) {
   if (prev != ((void*)0)) prev->next = next;
   if (next != ((void*)0)) next->prev = prev;
   if (prev == ((void*)0) && next == ((void*)0)) active = ((void*)0);
   ptr->next = top;
   ptr->prev = ((void*)0);
   top = ptr;
  } else {
   for (i = ptr->timestamp; i >= 0 && i >= ptr->timestamp - (15 - 1); i --) {
    draw_pixel( get_x(ptr, i), get_y(ptr, i), ptr->color );
   }
  }
  ptr = next;
 }
}

canonical_original_code:
void start_frame() {
 static int count = 0;
 struct Firework *ptr;
 int i;
 count = (count + 1) % 50;
 if (count == 0) {
  key_stroke(0);
 }
 for (ptr = active; ptr != ((void *)0); ptr = ptr->next) {
  for (i = ptr->timestamp; i >= 0 && i >= ptr->timestamp - (15 - 1); i --) {
   draw_pixel( get_x(ptr, i), get_y(ptr, i), 0 );
  }
 }
 ptr = active;
 while (ptr != ((void *)0)) {
  struct Firework *prev = ptr->prev; struct Firework *next = ptr->next;
  ptr->timestamp ++;
  if (ptr->timestamp > 200) {
   if (prev != ((void *)0)) prev->next = next;
   if (next != ((void *)0)) next->prev = prev;
   if (prev == ((void *)0) && next == ((void *)0)) active = ((void *)0);
   ptr->next = top;
   ptr->prev = ((void *)0);
   top = ptr;
  } else {
   for (i = ptr->timestamp; i >= 0 && i >= ptr->timestamp - (15 - 1); i --) {
    draw_pixel( get_x(ptr, i), get_y(ptr, i), ptr->color );
   }
  }
  ptr = next;
 }
}

variable_types: count int

user_defined_types: struct Firework { int x; float vx; int color; int timestamp; struct Firework *prev; }


decompiled_code:
__int16 *init_idt()
{
char *v0; // rbx

v0 = (char *)&idt;
do
{
set_trap((__int64)v0, 1, (int)&irq_empty, 0);
v0 += 8;
}
while ( v0 != (char *)(unsigned int)((_DWORD)&idt + 2048) );
set_trap((__int64)&idt, 1, (int)&vec0, 0);
set_trap((unsigned int)((_DWORD)&idt + 8), 1, (int)&vec1, 0);
set_trap((unsigned int)((_DWORD)&idt + 16), 1, (int)&vec2, 0);
set_trap((unsigned int)((_DWORD)&idt + 24), 1, (int)&vec3, 0);
set_trap((unsigned int)((_DWORD)&idt + 32), 1, (int)&vec4, 0);
set_trap((unsigned int)((_DWORD)&idt + 40), 1, (int)&vec5, 0);
set_trap((unsigned int)((_DWORD)&idt + 48), 1, (int)&vec6, 0);
set_trap((unsigned int)((_DWORD)&idt + 56), 1, (int)&vec7, 0);
set_trap((unsigned int)((_DWORD)&idt + 64), 1, (int)&vec8, 0);
set_trap((unsigned int)((_DWORD)&idt + 72), 1, (int)&vec9, 0);
set_trap((unsigned int)((_DWORD)&idt + 80), 1, (int)&vec10, 0);
set_trap((unsigned int)((_DWORD)&idt + 88), 1, (int)&vec11, 0);
set_trap((unsigned int)((_DWORD)&idt + 96), 1, (int)&vec12, 0);
set_trap((unsigned int)((_DWORD)&idt + 104), 1, (int)&vec13, 0);
set_intr((unsigned int)((_DWORD)&idt + 256), 1, (int)&irq0, 0);
set_intr((unsigned int)((_DWORD)&idt + 264), 1, (int)&irq1, 0);
return save_idt((int)&idt, 2048);
}

canonical_decompiled_code:
__int16 *func2()
{
char *v0; // rbx

v0 = (char *)&idt;
do
{
func1((__int64)v0, 1, (int)&irq_empty, 0);
v0 += 8;
}
while ( v0 != (char *)(unsigned int)((_DWORD)&idt + 2048) );
func1((__int64)&idt, 1, (int)&vec0, 0);
func1((unsigned int)((_DWORD)&idt + 8), 1, (int)&vec1, 0);
func1((unsigned int)((_DWORD)&idt + 16), 1, (int)&vec2, 0);
func1((unsigned int)((_DWORD)&idt + 24), 1, (int)&vec3, 0);
func1((unsigned int)((_DWORD)&idt + 32), 1, (int)&vec4, 0);
func1((unsigned int)((_DWORD)&idt + 40), 1, (int)&vec5, 0);
func1((unsigned int)((_DWORD)&idt + 48), 1, (int)&vec6, 0);
func1((unsigned int)((_DWORD)&idt + 56), 1, (int)&vec7, 0);
func1((unsigned int)((_DWORD)&idt + 64), 1, (int)&vec8, 0);
func1((unsigned int)((_DWORD)&idt + 72), 1, (int)&vec9, 0);
func1((unsigned int)((_DWORD)&idt + 80), 1, (int)&vec10, 0);
func1((unsigned int)((_DWORD)&idt + 88), 1, (int)&vec11, 0);
func1((unsigned int)((_DWORD)&idt + 96), 1, (int)&vec12, 0);
func1((unsigned int)((_DWORD)&idt + 104), 1, (int)&vec13, 0);
func0((unsigned int)((_DWORD)&idt + 256), 1, (int)&irq0, 0);
func0((unsigned int)((_DWORD)&idt + 264), 1, (int)&irq1, 0);
return save_idt((int)&idt, 2048);
}

original_code:
void init_idt() {
 int i;
 for (i = 0; i < 256; i ++) {
  set_trap(idt + i, 1, (uint_32)irq_empty, 0);
 }
 set_trap(idt + 0, 1, (uint_32)vec0, 0);
 set_trap(idt + 1, 1, (uint_32)vec1, 0);
 set_trap(idt + 2, 1, (uint_32)vec2, 0);
 set_trap(idt + 3, 1, (uint_32)vec3, 0);
 set_trap(idt + 4, 1, (uint_32)vec4, 0);
 set_trap(idt + 5, 1, (uint_32)vec5, 0);
 set_trap(idt + 6, 1, (uint_32)vec6, 0);
 set_trap(idt + 7, 1, (uint_32)vec7, 0);
 set_trap(idt + 8, 1, (uint_32)vec8, 0);
 set_trap(idt + 9, 1, (uint_32)vec9, 0);
 set_trap(idt + 10, 1, (uint_32)vec10, 0);
 set_trap(idt + 11, 1, (uint_32)vec11, 0);
 set_trap(idt + 12, 1, (uint_32)vec12, 0);
 set_trap(idt + 13, 1, (uint_32)vec13, 0);
 set_intr(idt + 32, 1, (uint_32)irq0, 0);
 set_intr(idt + 33, 1, (uint_32)irq1, 0);
 save_idt(idt, sizeof(idt));
}

canonical_original_code:
void init_idt() {
 int i;
 for (i = 0; i < 256; i ++) {
  set_trap(idt + i, 1, (unsigned int)irq_empty, 0);
 }
 set_trap(idt + 0, 1, (unsigned int)vec0, 0);
 set_trap(idt + 1, 1, (unsigned int)vec1, 0);
 set_trap(idt + 2, 1, (unsigned int)vec2, 0);
 set_trap(idt + 3, 1, (unsigned int)vec3, 0);
 set_trap(idt + 4, 1, (unsigned int)vec4, 0);
 set_trap(idt + 5, 1, (unsigned int)vec5, 0);
 set_trap(idt + 6, 1, (unsigned int)vec6, 0);
 set_trap(idt + 7, 1, (unsigned int)vec7, 0);
 set_trap(idt + 8, 1, (unsigned int)vec8, 0);
 set_trap(idt + 9, 1, (unsigned int)vec9, 0);
 set_trap(idt + 10, 1, (unsigned int)vec10, 0);
 set_trap(idt + 11, 1, (unsigned int)vec11, 0);
 set_trap(idt + 12, 1, (unsigned int)vec12, 0);
 set_trap(idt + 13, 1, (unsigned int)vec13, 0);
 set_intr(idt + 32, 1, (unsigned int)irq0, 0);
 set_intr(idt + 33, 1, (unsigned int)irq1, 0);
 save_idt(idt, sizeof(idt));
}


decompiled_code:
void __noreturn idle_cpu()
{
__halt();
}

canonical_decompiled_code:
void __noreturn func0()
{
__halt();
}

original_code:
static inline void
idle_cpu() {
 asm volatile("hlt");
}


decompiled_code:
void enable_interrupt()
{
_enable();
}

canonical_decompiled_code:
void func1()
{
_enable();
}

original_code:
static inline void
enable_interrupt(void) {
 asm volatile("sti");
}

canonical_original_code:
static inline void enable_interrupt() {
 asm volatile("sti");
}


decompiled_code:
void __noreturn game_init()
{
init_100hz_timer();
init_idt();
init_intr();
set_timer_intr_handler(&start_frame);
set_keyboard_intr_handler(&key_stroke);
init_game();
enable_interrupt();
idle_cpu();
}

canonical_decompiled_code:
void __noreturn func2()
{
init_100hz_timer();
init_idt();
init_intr();
set_timer_intr_handler(&start_frame);
set_keyboard_intr_handler(&key_stroke);
init_game();
func1();
func0();
}

original_code:
void
game_init(void) {
 init_100hz_timer();
 init_idt();
 init_intr();
 set_timer_intr_handler(start_frame);
 set_keyboard_intr_handler(key_stroke);
 init_game();
 enable_interrupt();
 while (1) {
  idle_cpu();
 }
}

canonical_original_code:
void game_init() {
 init_100hz_timer();
 init_idt();
 init_intr();
 set_timer_intr_handler(start_frame);
 set_keyboard_intr_handler(key_stroke);
 init_game();
 enable_interrupt();
 while (1) {
  idle_cpu();
 }
}


waitdisk

decompiled_code:
__int64 waitdisk()
{
__int64 result; // rax

do
result = in_byte(0x1F7u) & 0xC0;
while ( (_DWORD)result != 64 );
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 result; // rax

do
result = func0(0x1F7u) & 0xC0;
while ( (_DWORD)result != 64 );
return result;
}

original_code:
void
waitdisk(void) {
 while((in_byte(0x1F7) & 0xC0) != 0x40);
}

canonical_original_code:
void waitdisk() {
 while((in_byte(0x1F7) & 0xC0) != 0x40);
}


readsect

decompiled_code:
unsigned __int32 __fastcall readsect(unsigned __int32 *a1, int a2)
{
unsigned __int32 *v2; // rbx
unsigned __int32 result; // eax

waitdisk();
out_byte(0x1F2u, 1u);
out_byte(0x1F3u, (char)a2);
out_byte(0x1F4u, SBYTE1(a2));
out_byte(0x1F5u, SBYTE2(a2));
out_byte(0x1F6u, (char)(HIBYTE(a2) | 0xE0));
out_byte(0x1F7u, 0x20u);
waitdisk();
v2 = a1;
do
{
result = in_long(0x1F0u);
*v2++ = result;
}
while ( v2 != a1 + 128 );
return result;
}

canonical_decompiled_code:
unsigned __int32 __fastcall func4(unsigned __int32 *a1, int a2)
{
unsigned __int32 *v2; // rbx
unsigned __int32 result; // eax

func3();
func2(0x1F2u, 1u);
func2(0x1F3u, (char)a2);
func2(0x1F4u, SBYTE1(a2));
func2(0x1F5u, SBYTE2(a2));
func2(0x1F6u, (char)(HIBYTE(a2) | 0xE0));
func2(0x1F7u, 0x20u);
func3();
v2 = a1;
do
{
result = func1(0x1F0u);
*v2++ = result;
}
while ( v2 != a1 + 128 );
return result;
}

original_code:
void
readsect(void *dst, int offset) {
 int i;
 waitdisk();
 out_byte(0x1F2, 1);
 out_byte(0x1F3, offset);
 out_byte(0x1F4, offset >> 8);
 out_byte(0x1F5, offset >> 16);
 out_byte(0x1F6, (offset >> 24) | 0xE0);
 out_byte(0x1F7, 0x20);
 waitdisk();
 for (i = 0; i < 512 / 4; i ++) {
  ((int *)dst)[i] = in_long(0x1F0);
 }
}

variable_types:
dst
void *

variable_types: offset int


readseg

decompiled_code:
int __fastcall readseg(__int64 a1, int a2, int a3)
{
unsigned __int64 v3; // r12
__int64 v4; // rax
unsigned __int32 *v5; // rbx
int v6; // ebp

v3 = a1 + a2;
v4 = a3 % 512;
v5 = (unsigned __int32 *)(a1 - v4);
v6 = a3 / 512 + 1;
if ( v3 > a1 - v4 )
{
do
{
LODWORD(v4) = readsect(v5, v6);
v5 += 128;
++v6;
}
while ( v3 > (unsigned __int64)v5 );
}
return v4;
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1, int a2, int a3)
{
unsigned __int64 v3; // r12
__int64 v4; // rax
unsigned __int32 *v5; // rbx
int v6; // ebp

v3 = a1 + a2;
v4 = a3 % 512;
v5 = (unsigned __int32 *)(a1 - v4);
v6 = a3 / 512 + 1;
if ( v3 > a1 - v4 )
{
do
{
LODWORD(v4) = func4(v5, v6);
v5 += 128;
++v6;
}
while ( v3 > (unsigned __int64)v5 );
}
return v4;
}

original_code:
void
readseg(unsigned char *pa, int count, int offset) {
 unsigned char *epa;
 epa = pa + count;
 pa -= offset % 512;
 offset = (offset / 512) + 1;
 for(; pa < epa; pa += 512, offset ++)
  readsect(pa, offset);
}

variable_types:
pa
unsigned char *

variable_types:
epa
unsigned char *


bootmain

original_code:
void
bootmain(void) {
 struct ELFHeader *elf;
 struct ProgramHeader *ph, *eph;
 unsigned char* pa, *i;
 void (*entry)(void);
 elf = (struct ELFHeader*)0x8000;
 readseg((unsigned char*)elf, 4096, 0);
 ph = (struct ProgramHeader*)((char *)elf + elf->phoff);
 eph = ph + elf->phnum;
 for(; ph < eph; ph ++) {
  pa = (unsigned char*)ph->paddr;
  for (i = pa; i != pa + ph->memsz; *i ++ = 0);
  readseg(pa, ph->filesz, ph->off);
 }
 entry = (void(*)(void))(elf->entry);
 entry();
}

canonical_original_code:
void bootmain() {
 struct ELFHeader *elf;
 struct ProgramHeader *ph; struct ProgramHeader *eph;
 unsigned char *pa; unsigned char *i;
 void (*entry)();
 elf = (struct ELFHeader *)0x8000;
 readseg((unsigned char *)elf, 4096, 0);
 ph = (struct ProgramHeader *)((char *)elf + elf->phoff);
 eph = ph + elf->phnum;
 for(; ph < eph; ph ++) {
  pa = (unsigned char *)ph->paddr;
  for (i = pa; i != pa + ph->memsz; *i ++ = 0);
  readseg(pa, ph->filesz, ph->off);
 }
 entry = (void (*)())(elf->entry);
 entry();
}

variable_types: struct ELFHeader { unsigned int magic; unsigned char elf[12]; unsigned short type; unsigned short machine; unsigned int version; unsigned int entry; unsigned int phoff; unsigned int shoff; unsigned int flags; unsigned short ehsize; unsigned short phentsize; unsigned short phnum; unsigned short shentsize; unsigned short shnum; unsigned short shstrndx; } *

variable_types: struct ProgramHeader { unsigned int type; unsigned int off; unsigned int vaddr; unsigned int paddr; unsigned int filesz; unsigned int memsz; unsigned int flags; unsigned int align; } *

variable_types:
i
unsigned char *

variable_types:
entry
void (*)()


decompiled_code:
__int64 __fastcall out_byte(unsigned __int16 a1, unsigned int a2)
{
__int64 result; // rax

result = a2;
__outbyte(a1, a2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int16 a1, unsigned int a2)
{
__int64 result; // rax

result = a2;
__outbyte(a1, a2);
return result;
}

original_code:
static inline void
out_byte(short port, char data) {
 asm volatile("out %0,%1" : : "a" (data), "d" (port));
}

variable_types: port short

variable_types: data char


decompiled_code:
void __fastcall set_timer_intr_handler(__int64 a1)
{
do_timer = a1;
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1)
{
do_timer = a1;
}

original_code:
void
set_timer_intr_handler( void (*ptr)(void) ) {
 do_timer = ptr;
}

canonical_original_code:
void set_timer_intr_handler(void (*ptr)()) {
 do_timer = ptr;
}

variable_types:
ptr
void (*)()


decompiled_code:
void __fastcall set_keyboard_intr_handler(__int64 a1)
{
do_keyboard = a1;
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1)
{
do_keyboard = a1;
}

original_code:
void
set_keyboard_intr_handler( void (*ptr)(int) ) {
 do_keyboard = ptr;
}

canonical_original_code:
void set_keyboard_intr_handler(void (*ptr)(int)) {
 do_keyboard = ptr;
}

variable_types:
ptr
void (*)(int)


decompiled_code:
__int64 __fastcall irq_handle(__int64 a1)
{
__int64 result; // rax
unsigned __int8 v2; // bp
unsigned __int8 v3; // bl

result = *(unsigned int *)(a1 + 32);
if ( (_DWORD)result == 1000 )
return do_timer();
if ( (_DWORD)result == 1001 )
{
v2 = in_byte(0x60u);
v3 = in_byte(0x61u);
out_byte(0x61u, v3 | 0x80);
out_byte(0x61u, v3);
result = do_keyboard(v2);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 result; // rax
unsigned __int8 v2; // bp
unsigned __int8 v3; // bl

result = *(unsigned int *)(a1 + 32);
if ( (_DWORD)result == 1000 )
return do_timer();
if ( (_DWORD)result == 1001 )
{
v2 = func0(0x60u);
v3 = func0(0x61u);
func1(0x61u, v3 | 0x80);
func1(0x61u, v3);
result = do_keyboard(v2);
}
return result;
}

original_code:
__attribute__((regparm(0))) void
irq_handle(struct TrapFrame *tf) {
 if (tf->irq == 1000) {
  do_timer();
 } else if (tf->irq == 1001) {
  uint_32 code = in_byte(0x60);
  uint_32 val = in_byte(0x61);
  out_byte(0x61, val | 0x80);
  out_byte(0x61, val);
  do_keyboard(code);
 }
}

canonical_original_code:
void irq_handle(struct TrapFrame *tf) {
 if (tf->irq == 1000) {
  do_timer();
 } else if (tf->irq == 1001) {
  unsigned int code = in_byte(0x60);
  unsigned int val = in_byte(0x61);
  out_byte(0x61, val | 0x80);
  out_byte(0x61, val);
  do_keyboard(code);
 }
}

variable_types: struct TrapFrame { unsigned int edi; int irq; } *

variable_types:
code
unsigned int

variable_types:
val
unsigned int


decompiled_code:
unsigned __int8 __fastcall in_byte(unsigned __int16 a1)
{
return __inbyte(a1);
}

canonical_decompiled_code:
unsigned __int8 __fastcall func0(unsigned __int16 a1)
{
return __inbyte(a1);
}

original_code:
static inline uint_8
in_byte(uint_16 port) {
 uint_8 data;
 asm volatile("in %1, %0" : "=a"(data) : "d"(port));
 return data;
}

canonical_original_code:
static inline unsigned char in_byte(unsigned short port) {
 unsigned char data;
 asm volatile("in %1, %0" : "=a"(data) : "d"(port));
 return data;
}

variable_types:
port
unsigned short

variable_types:
data
unsigned char

return_type: unsigned char


decompiled_code:
__int64 init_100hz_timer()
{
out_byte(0x43u, 0x34u);
out_byte(0x40u, 0x9Cu);
return out_byte(0x40u, 0x2Eu);
}


canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int16 a1, unsigned int a2)
{
__int64 result; // rax

result = a2;
__outbyte(a1, a2);
return result;
}

original_code:
static inline void
out_byte(uint_16 port, uint_8 data) {
 asm volatile("out %%al, %%dx" : : "a"(data), "d"(port));
}

canonical_original_code:
static inline void out_byte(unsigned short port, unsigned char data) {
 asm volatile("out %%al, %%dx" : : "a"(data), "d"(port));
}


decompiled_code:
__int64 init_intr()
{
out_byte(0x21u, 0xFFu);
out_byte(0xA1u, 0xFFu);
out_byte(0x20u, 0x11u);
out_byte(0x21u, 0x20u);
out_byte(0x21u, 4u);
out_byte(0x21u, 3u);
out_byte(0xA0u, 0x11u);
out_byte(0xA1u, 0x28u);
out_byte(0xA1u, 2u);
out_byte(0xA1u, 3u);
out_byte(0x20u, 0x68u);
out_byte(0x20u, 0xAu);
out_byte(0xA0u, 0x68u);
return out_byte(0xA0u, 0xAu);
}


decompiled_code:
__int64 __fastcall cmp_int_ascend(_DWORD *a1, _DWORD *a2)
{
return (unsigned int)(*a1 - *a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(_DWORD *a1, _DWORD *a2)
{
return (unsigned int)(*a1 - *a2);
}

original_code:
int cmp_int_ascend( const void *a, const void *b ) {
    return (*(int*)a - *(int*)b);
}

canonical_original_code:
int cmp_int_ascend(void *a, void *b) {
    return (*(int *)a - *(int *)b);
}

variable_types:
a
void *

variable_types:
b
void *


printInt

decompiled_code:
char *__fastcall printInt(unsigned int *a1)
{
sprintf(result, "%d", *a1);
return result;
}

canonical_decompiled_code:
char *__fastcall func1(unsigned int *a1)
{
sprintf(result, "%d", *a1);
return result;
}

original_code:
char* printInt(const void* element) {
    sprintf(result, "%d", *(int*)element);
    return result;
}

canonical_original_code:
char *printInt(void *element) {
    sprintf(result, "%d", *(int *)element);
    return result;
}

variable_types:
element
void *


decompiled_code:
__int64 __fastcall cmp_int_descend(_DWORD *a1, _DWORD *a2)
{
return (unsigned int)(*a2 - *a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD *a1, _DWORD *a2)
{
return (unsigned int)(*a2 - *a1);
}

original_code:
int cmp_int_descend( const void *a, const void *b ) {
    return (*(int*)b - *(int*)a);
}

canonical_original_code:
int cmp_int_descend(void *a, void *b) {
    return (*(int *)b - *(int *)a);
}


printFloat

decompiled_code:
char *__fastcall printFloat(float *a1)
{
sprintf(result, "%lf", *a1);
return result;
}

canonical_decompiled_code:
char *__fastcall func1(float *a1)
{
sprintf(result, "%lf", *a1);
return result;
}

original_code:
char* printFloat(const void* element) {
    sprintf(result, "%lf", *(float*)element);
    return result;
}

canonical_original_code:
char *printFloat(void *element) {
    sprintf(result, "%lf", *(float *)element);
    return result;
}


ascend

decompiled_code:
__int64 __fastcall ascend(float *a1, float *a2)
{
return (unsigned int)(int)(float)(*a1 - *a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(float *a1, float *a2)
{
return (unsigned int)(int)(float)(*a1 - *a2);
}

original_code:
int ascend( const void *a, const void *b ) {
    return (*(float*)a - *(float*)b);
}

canonical_original_code:
int ascend(void *a, void *b) {
    return (*(float *)a - *(float *)b);
}


descend

decompiled_code:
__int64 __fastcall descend(float *a1, float *a2)
{
return (unsigned int)(int)(float)(*a2 - *a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(float *a1, float *a2)
{
return (unsigned int)(int)(float)(*a2 - *a1);
}

original_code:
int descend( const void *a, const void *b ) {
    return (*(float*)b - *(float*)a);
}

canonical_original_code:
int descend(void *a, void *b) {
    return (*(float *)b - *(float *)a);
}


main

canonical_name: main

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

v3 = makeList(argc, argv, envp);
setPrintFun(v3, printFloat);
printList(v3);
v4 = 9;
do
{
append(v3, 5.0);
--v4;
}
while ( v4 );
append(v3, 1.5);
append(v3, 9.3000002);
append(v3, 3.5);
printList(v3);
setCmpFun(v3, ascend);
sort(v3);
printList(v3);
cleanupList(v3);
return 0;
}

original_code:
int main() {
 List* list = makeList();
 setPrintFun(list, printFloat);
 printList(list);
 for (int i = 0; i < 9; ++i) {
  append(list, 5.0);
 }
 append(list, 1.5);
 append(list, 9.3);
 append(list, 3.5);
 printList(list);
 setCmpFun(list, ascend);
 sort(list);
 printList(list);
 cleanupList(list);
 return 0;
}

canonical_original_code:
int main() {
 struct List *list = makeList();
 setPrintFun(list, printFloat);
 printList(list);
 for (int i = 0; i < 9; ++i) {
  append(list, 5.0);
 }
 append(list, 1.5);
 append(list, 9.3);
 append(list, 3.5);
 printList(list);
 setCmpFun(list, ascend);
 sort(list);
 printList(list);
 cleanupList(list);
 return 0;
}

variable_types: struct List { int size; int capacity; float *data; int (*cmp)(void *, void *); char *(*print)(void *element); } *

variable_types: i int


ascend

decompiled_code:
__int64 __fastcall ascend(_DWORD **a1, _DWORD **a2)
{
return (unsigned int)(**a1 - **a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(_DWORD **a1, _DWORD **a2)
{
return (unsigned int)(**a1 - **a2);
}

original_code:
int ascend( const void *a, const void *b ) {
    int p = ((myStruct*) (*(myStruct**) a))->x;
    int q = ((myStruct*) (*(myStruct**) b))->x;
    return p - q;
}

canonical_original_code:
int ascend(void *a, void *b) {
    int p = ((myStruct*) (*(myStruct**) a))->x;
    int q = ((myStruct*) (*(myStruct**) b))->x;
    return p - q;
}

variable_types: p int

variable_types: q int


printStruct

decompiled_code:
char *__fastcall printStruct(unsigned int **a1)
{
sprintf(result, "{%d, %f}", **a1, *((float *)*a1 + 1));
return result;
}

canonical_decompiled_code:
char *__fastcall func1(unsigned int **a1)
{
sprintf(result, "{%d, %f}", **a1, *((float *)*a1 + 1));
return result;
}

original_code:
char* printStruct(const void* element) {
    sprintf(result, "{%d, %f}", (*(myStruct**)element)->x, (*(myStruct**)element)->y);
    return result;
}

canonical_original_code:
char *printStruct(void *element) {
    sprintf(result, "{%d, %f}", (*(struct ListStruct **)element)->x, (*(struct ListStruct **)element)->y);
    return result;
}


descend

decompiled_code:
__int64 __fastcall descend(_DWORD **a1, _DWORD **a2)
{
return (unsigned int)(**a2 - **a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(_DWORD **a1, _DWORD **a2)
{
return (unsigned int)(**a2 - **a1);
}

original_code:
int descend( const void *a, const void *b ) {
    return ((*(myStruct**)b)->x - (*(myStruct**)a)->x);
}

canonical_original_code:
int descend(void *a, void *b) {
    return ((*(struct ListStruct **)b)->x - (*(struct ListStruct **)a)->x);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
int i; // ebx
__m128i v5; // xmm0
_DWORD *v6; // rax
_DWORD *v7; // r13
_DWORD *v8; // r12
_DWORD *v9; // rbx

v3 = makeList(argc, argv, envp);
setPrintFun(v3, printStruct);
printList(v3);
for ( i = 0; i != 10; ++i )
{
v5 = 0LL;
*(double *)v5.m128i_i64 = (double)i * 5.0;
*(float *)v5.m128i_i32 = *(double *)v5.m128i_i64;
v6 = mkMyStruct(i, v5);
append(v3, v6);
}
v7 = mkMyStruct(5, (__m128i)(unsigned int)LC2);
v8 = mkMyStruct(8, (__m128i)(unsigned int)LC3);
v9 = mkMyStruct(15, (__m128i)(unsigned int)LC4);
append(v3, v7);
append(v3, v8);
append(v3, v9);
printList(v3);
setCmpFun(v3, ascend);
puts("Removing {15, 3.5}");
removeElement(v3, v9);
printList(v3);
puts("Removing index 0");
removeIndex(v3, 0LL);
printList(v3);
printList(v3);
sort(v3);
printList(v3);
cleanupList(v3);
return 0;
}

original_code:
int main() {
 List* list = makeList();
 setPrintFun(list, printStruct);
 printList(list);
 for (int i = 0; i < 10; ++i) {
  myStruct* el = mkMyStruct(i, i * 5.0);
  append(list, el);
 }
 myStruct* a = mkMyStruct(5, 1.5);
 myStruct* b = mkMyStruct(8, 9.3);
 myStruct* c = mkMyStruct(15, 3.5);
 append(list, a);
 append(list, b);
 append(list, c);
 printList(list);
 setCmpFun(list, ascend);
 printf("Removing {15, 3.5}\n");
 removeElement(list, c);
 printList(list);
 printf("Removing index 0\n");
 removeIndex(list, 0);
 printList(list);
 printList(list);
 sort(list);
 printList(list);
 cleanupList(list);
 return 0;
}

canonical_original_code:
int main() {
 struct List *list = makeList();
 setPrintFun(list, printStruct);
 printList(list);
 for (int i = 0; i < 10; ++i) {
  struct ListStruct *el = mkMyStruct(i, i * 5.0);
  append(list, el);
 }
 struct ListStruct *a = mkMyStruct(5, 1.5);
 struct ListStruct *b = mkMyStruct(8, 9.3);
 struct ListStruct *c = mkMyStruct(15, 3.5);
 append(list, a);
 append(list, b);
 append(list, c);
 printList(list);
 setCmpFun(list, ascend);
 printf("Removing {15, 3.5}\n");
 removeElement(list, c);
 printList(list);
 printf("Removing index 0\n");
 removeIndex(list, 0);
 printList(list);
 printList(list);
 sort(list);
 printList(list);
 cleanupList(list);
 return 0;
}

variable_types: struct List { int size; int capacity; struct ListStruct { int x; float y; } **data; int (*cmp)(void *, void *); char *(*print)(void *element); } *

variable_types:
el
struct ListStruct { int x; float y; } *

variable_types:
a
struct ListStruct { int x; float y; } *

variable_types:
b
struct ListStruct { int x; float y; } *

variable_types:
c
struct ListStruct { int x; float y; } *

user_defined_types: struct List { int size; int capacity; struct ListStruct **data; int (*cmp)(void *, void *); char *(*print)(void *element); }


printString

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

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

original_code:
char* printString(const void* element) {
    return *(char**) element;
}

canonical_original_code:
char *printString(void *element) {
    return *(char **) element;
}


ascend

decompiled_code:
int __fastcall ascend(const char *a1, const char *a2)
{
return strcmp(a1, a2);
}

canonical_decompiled_code:
int __fastcall func1(const char *a1, const char *a2)
{
return strcmp(a1, a2);
}

original_code:
int ascend( const void *a, const void *b ) {
    return strcmp((char*) a, (char*) b);
}

canonical_original_code:
int ascend(void *a, void *b) {
    return strcmp((char *) a, (char *) b);
}


descend

decompiled_code:
__int64 __fastcall descend(const char *a1, const char *a2)
{
return (unsigned int)-strcmp(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1, const char *a2)
{
return (unsigned int)-strcmp(a1, a2);
}

original_code:
int descend( const void *a, const void *b ) {
    return -1 * strcmp((char*) a, (char*) b);
}

canonical_original_code:
int descend(void *a, void *b) {
    return -1 * strcmp((char *) a, (char *) b);
}


main

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

v3 = makeList(argc, argv, envp);
setPrintFun(v3, printString);
printList(v3);
v4 = 9;
do
{
append(v3, "hello");
--v4;
}
while ( v4 );
append(v3, "aaa");
append(v3, "zzz");
append(v3, "lists!");
printList(v3);
setCmpFun(v3, ascend);
sort(v3);
printList(v3);
cleanupList(v3);
return 0;
}

original_code:
int main() {
 List* list = makeList();
 setPrintFun(list, printString);
 printList(list);
 for (int i = 0; i < 9; ++i) {
  append(list, "hello");
 }
 append(list, "aaa");
 append(list, "zzz");
 append(list, "lists!");
 printList(list);
 setCmpFun(list, ascend);
 sort(list);
 printList(list);
 cleanupList(list);
 return 0;
}

canonical_original_code:
int main() {
 struct List *list = makeList();
 setPrintFun(list, printString);
 printList(list);
 for (int i = 0; i < 9; ++i) {
  append(list, "hello");
 }
 append(list, "aaa");
 append(list, "zzz");
 append(list, "lists!");
 printList(list);
 setCmpFun(list, ascend);
 sort(list);
 printList(list);
 cleanupList(list);
 return 0;
}

variable_types: struct List { int size; int capacity; char **data; int (*cmp)(void *, void *); char *(*print)(void *element); } *


append

repo: zachbornheimer/morpheme-extraction

decompiled_code:
const char *__fastcall append(char *src, char *a2)
{
size_t v2; // rbx
size_t v3; // rax
char *v4; // rax
char *v5; // rbx

v2 = strlen(src);
v3 = strlen(a2);
v4 = (char *)malloc(v2 + v3 + 1);
if ( !v4 )
return "ERROR IN MALLOC";
v5 = v4;
strcpy(v4, src);
strcat(v5, a2);
return v5;
}

canonical_decompiled_code:
const char *__fastcall func0(char *src, char *a2)
{
size_t v2; // rbx
size_t v3; // rax
char *v4; // rax
char *v5; // rbx

v2 = strlen(src);
v3 = strlen(a2);
v4 = (char *)malloc(v2 + v3 + 1);
if ( !v4 )
return "ERROR IN MALLOC";
v5 = v4;
strcpy(v4, src);
strcat(v5, a2);
return v5;
}

original_code:
char* append(char* one, char* two)
{
 char *temp = malloc(sizeof(char) * (strlen(one) + 1 + strlen(two)));
 if (temp == ((void *)0))
  return "ERROR IN MALLOC";
 strcpy(temp, one);
 strcat(temp, two);
 return temp;
}

canonical_original_code:
char *append(char *one, char *two)
{
 char *temp = malloc(sizeof(char) * (strlen(one) + 1 + strlen(two)));
 if (temp == ((void *)0))
  return "ERROR IN MALLOC";
 strcpy(temp, one);
 strcat(temp, two);
 return temp;
}

variable_types:
one
char *

variable_types:
two
char *

variable_types:
temp
char *


decompiled_code:
__int64 __fastcall in_array(int a1, __int64 *a2, int a3)
{
__int64 v3; // r8
__int64 v4; // rax
unsigned int v5; // esi

if ( a3 <= 0 )
return (unsigned int)-1;
v3 = *a2;
v4 = 0LL;
while ( 1 )
{
v5 = v4;
if ( *(char *)(v3 + v4) == a1 )
break;
if ( ++v4 == a3 )
return (unsigned int)-1;
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, __int64 *a2, int a3)
{
__int64 v3; // r8
__int64 v4; // rax
unsigned int v5; // esi

if ( a3 <= 0 )
return (unsigned int)-1;
v3 = *a2;
v4 = 0LL;
while ( 1 )
{
v5 = v4;
if ( *(char *)(v3 + v4) == a1 )
break;
if ( ++v4 == a3 )
return (unsigned int)-1;
}
return v5;
}

original_code:
int in_array(const int c, char **uniq, const int size)
{
 int i;
 for (i = 0; i < size; ++i)
  if (c == (*uniq)[i])
   return i;
 return -1;
}

canonical_original_code:
int in_array(int c, char **uniq, int size)
{
 int i;
 for (i = 0; i < size; ++i)
  if (c == (*uniq)[i])
   return i;
 return -1;
}

variable_types: c int

variable_types:
uniq
char **

variable_types: size int


decompiled_code:
__int64 __fastcall in_char_array(char *s1, __int64 a2, int a3)
{
__int64 v3; // rbp
__int64 v4; // rbx
unsigned int v5; // er14

if ( a3 <= 0 )
return (unsigned int)-1;
v3 = (unsigned int)a3;
v4 = 0LL;
while ( 1 )
{
v5 = v4;
if ( !strcmp(s1, *(const char **)(a2 + 8 * v4)) )
break;
if ( ++v4 == v3 )
return (unsigned int)-1;
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *s1, __int64 a2, int a3)
{
__int64 v3; // rbp
__int64 v4; // rbx
unsigned int v5; // er14

if ( a3 <= 0 )
return (unsigned int)-1;
v3 = (unsigned int)a3;
v4 = 0LL;
while ( 1 )
{
v5 = v4;
if ( !strcmp(s1, *(const char **)(a2 + 8 * v4)) )
break;
if ( ++v4 == v3 )
return (unsigned int)-1;
}
return v5;
}

original_code:
int in_char_array(const char *w, char *uniq[], const int size)
{
 int i;
 for (i = 0; i < size; ++i)
  if (strcmp(w, uniq[i]) == 0)
   return i;
 return -1;
}

canonical_original_code:
int in_char_array(char *w, char *uniq[], int size)
{
 int i;
 for (i = 0; i < size; ++i)
  if (strcmp(w, uniq[i]) == 0)
   return i;
 return -1;
}

variable_types:
w
char *

variable_types:
uniq
char *[]


uniq

decompiled_code:
__int64 __fastcall uniq(const char **a1, _QWORD *a2)
{
const char *v2; // r13
int v3; // er15
_BYTE *v4; // r14
__int64 v5; // rbx
int v6; // ebp
char v7; // r12
__int64 v9[8]; // [rsp+18h] [rbp-40h] BYREF

if ( !a1 )
return 0xFFFFFFFFLL;
v2 = *a1;
if ( !*a1 )
return 0xFFFFFFFFLL;
v3 = strlen(*a1);
v4 = malloc(v3 + 2);
v9[0] = (__int64)v4;
if ( v3 < 0 )
{
v6 = 0;
}
else
{
v5 = 0LL;
v6 = 0;
do
{
v7 = v2[v5];
if ( !v7 )
break;
if ( !v6 || (unsigned int)in_array(v7, v9, v6) == -1 )
v4[v6++] = v7;
++v5;
}
while ( v3 >= (int)v5 );
}
*a2 = v4;
return (unsigned int)(v6 - 1);
}

canonical_decompiled_code:
__int64 __fastcall func3(const char **a1, _QWORD *a2)
{
const char *v2; // r13
int v3; // er15
_BYTE *v4; // r14
__int64 v5; // rbx
int v6; // ebp
char v7; // r12
__int64 v9[8]; // [rsp+18h] [rbp-40h] BYREF

if ( !a1 )
return 0xFFFFFFFFLL;
v2 = *a1;
if ( !*a1 )
return 0xFFFFFFFFLL;
v3 = strlen(*a1);
v4 = malloc(v3 + 2);
v9[0] = (__int64)v4;
if ( v3 < 0 )
{
v6 = 0;
}
else
{
v5 = 0LL;
v6 = 0;
do
{
v7 = v2[v5];
if ( !v7 )
break;
if ( !v6 || (unsigned int)func1(v7, v9, v6) == -1 )
v4[v6++] = v7;
++v5;
}
while ( v3 >= (int)v5 );
}
*a2 = v4;
return (unsigned int)(v6 - 1);
}

original_code:
int uniq(char **chars, char **ret_addr)
{
 if (chars == ((void *)0) || *chars == ((void *)0))
  return -1;
 int len = strlen(*chars);
 char *u = malloc((len+2) * sizeof(char));
 int i = 0, j=0;
 for (j = 0; j <= len; ++j) {
  if ((*chars)[j] != 0) {
   if (i == 0 || in_array((*chars)[j], &u, i)==-1)
    u[i++] = (*chars)[j];
  } else {
   break;
  }
 }
 *ret_addr = u;
 return i-1;
}

canonical_original_code:
int uniq(char **chars, char **ret_addr)
{
 if (chars == ((void *)0) || *chars == ((void *)0))
  return -1;
 int len = strlen(*chars);
 char *u = malloc((len+2) * sizeof(char));
 int i = 0; int j = 0;
 for (j = 0; j <= len; ++j) {
  if ((*chars)[j] != 0) {
   if (i == 0 || in_array((*chars)[j], &u, i)==-1)
    u[i++] = (*chars)[j];
  } else {
   break;
  }
 }
 *ret_addr = u;
 return i-1;
}

variable_types:
chars
char **

variable_types: len int

variable_types:
u
char *

variable_types: i int

variable_types: j int


decompiled_code:
__int64 __fastcall uniq_words(char **a1, int a2)
{
_QWORD *v2; // r12
char **v3; // rbx
int v4; // ebp

v2 = malloc(8LL * a2);
if ( a2 <= 0 )
{
v4 = 0;
}
else
{
v3 = a1;
v4 = 0;
do
{
if ( !v4 || (unsigned int)in_char_array(*v3, (__int64)v2, v4) == -1 )
v2[v4++] = *v3;
++v3;
}
while ( v3 != &a1[(unsigned int)(a2 - 1) + 1] );
}
free(v2);
return (unsigned int)(v4 - 1);
}

canonical_decompiled_code:
__int64 __fastcall func4(char **a1, int a2)
{
_QWORD *v2; // r12
char **v3; // rbx
int v4; // ebp

v2 = malloc(8LL * a2);
if ( a2 <= 0 )
{
v4 = 0;
}
else
{
v3 = a1;
v4 = 0;
do
{
if ( !v4 || (unsigned int)func2(*v3, (__int64)v2, v4) == -1 )
v2[v4++] = *v3;
++v3;
}
while ( v3 != &a1[(unsigned int)(a2 - 1) + 1] );
}
free(v2);
return (unsigned int)(v4 - 1);
}

original_code:
int uniq_words(char **arr, int size)
{
 char **u = malloc(sizeof(char*) * size);
 int i = 0, j=0;
 for (j = 0; j < size; ++j) {
  if (i == 0 || in_char_array(arr[j], u, i)==-1) {
   u[i] = arr[j];
   ++i;
  }
 }
 free(u);
 return i-1;
}

canonical_original_code:
int uniq_words(char **arr, int size)
{
 char **u = malloc(sizeof(char*) * size);
 int i = 0; int j = 0;
 for (j = 0; j < size; ++j) {
  if (i == 0 || in_char_array(arr[j], u, i)==-1) {
   u[i] = arr[j];
   ++i;
  }
 }
 free(u);
 return i-1;
}

variable_types:
arr
char **

variable_types:
u
char **


decompiled_code:
__int64 __fastcall explode_sansnull_str(__int64 a1, __int64 a2, const char **a3)
{
__int64 v5[3]; // [rsp+0h] [rbp-38h] BYREF
unsigned int v6; // [rsp+18h] [rbp-20h]
int v7; // [rsp+1Ch] [rbp-1Ch]

v7 = strlen(*a3);
v5[2] = (__int64)a3;
v5[1] = a2;
v5[0] = a1;
v6 = -1;
GOMP_parallel(explode_sansnull_str__omp_fn_0, v5, 0LL, 0LL);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, const char **a3)
{
__int64 v5[3]; // [rsp+0h] [rbp-38h] BYREF
unsigned int v6; // [rsp+18h] [rbp-20h]
int v7; // [rsp+1Ch] [rbp-1Ch]

v7 = strlen(*a3);
v5[2] = (__int64)a3;
v5[1] = a2;
v5[0] = a1;
v6 = -1;
GOMP_parallel(explode_sansnull_str__omp_fn_0, v5, 0LL, 0LL);
return v6;
}

original_code:
int explode_sansnull_str(char ***arr_ptr, const char *str, const char **delimiter)
{
 int i, size = -1, len = strlen(*delimiter);

#pragma omp parallel for
 for (i = 0; i < len; i++) {
  size = explode_sansnull(arr_ptr, str, *delimiter);
 }
 return size;
}

canonical_original_code:
int explode_sansnull_str(char ***arr_ptr, char *str, char **delimiter)
{
 int i; int size = -1; int len = strlen(*delimiter);

#pragma omp parallel for
 for (i = 0; i < len; i++) {
  size = explode_sansnull(arr_ptr, str, *delimiter);
 }
 return size;
}

variable_types:
str
char *

variable_types:
delimiter
char **


decompiled_code:
__int64 __fastcall explode_sansnull(_QWORD *a1, const char *a2, const char *a3)
{
int v4; // er14
char *v5; // rbx
char *v6; // r12
char v7; // al
char *v8; // rsi
int v9; // ebp
char *v10; // rbx
_QWORD *v11; // r14
__int64 v12; // rbp
int v13; // er15

v4 = strlen(a2) + 1;
v5 = strdup(a2);
v6 = strdup(a3);
v7 = *a2;
if ( *a2 )
{
v8 = (char *)(a2 + 1);
v9 = 0;
do
{
v9 += *a3 == (unsigned __int8)v7;
v7 = *v8++;
}
while ( v7 );
}
else
{
v9 = 0;
}
v10 = strtok(v5, v6);
v11 = malloc(v4 + v9 + 1 + 8 * v9);
if ( v10 )
{
v12 = 1LL;
do
{
v13 = v12;
v11[v12 - 1] = v10;
v10 = strtok(0LL, v6);
++v12;
}
while ( v10 );
}
else
{
v13 = 0;
}
free(v6);
*a1 = v11;
return (unsigned int)(v13 - 1);
}

original_code:
int explode_sansnull(char ***a, const char *str, const char *delimiter)
{
 int wc, size = strlen(str)+1;
 int count = 0, char_count = 0, i = 0, j;
 char *token;
 char *s = strdup(str);
 char *t = strdup(delimiter);
 for (j = 0; str[j] != '\0'; (str[j++] == *delimiter) ? ++(count) : 0);
 wc = count+1;
 count = 0;
 token = strtok(s, t);
 int malloc_size = (sizeof(char*) * (wc-1)) + size + wc;
 char **arr = malloc(malloc_size);
 while (token != ((void *)0)) {
  char_count += strlen(token)+1;
  if (token != ((void *)0))
   arr[i++] = token;
  token = strtok(((void *)0), t);
  ++count;
 }
 free(t);
 *a = arr;
 return i-1;
}

canonical_original_code:
int explode_sansnull(char ***a, char *str, char *delimiter)
{
 int wc; int size = strlen(str)+1;
 int count = 0; int char_count = 0; int i = 0; int j;
 char *token;
 char *s = strdup(str);
 char *t = strdup(delimiter);
 for (j = 0; str[j] != '\0'; (str[j++] == *delimiter) ? ++(count) : 0);
 wc = count+1;
 count = 0;
 token = strtok(s, t);
 int malloc_size = (sizeof(char*) * (wc-1)) + size + wc;
 char **arr = malloc(malloc_size);
 while (token != ((void *)0)) {
  char_count += strlen(token)+1;
  if (token != ((void *)0))
   arr[i++] = token;
  token = strtok(((void *)0), t);
  ++count;
 }
 free(t);
 *a = arr;
 return i-1;
}

variable_types:
a
char ***

variable_types:
delimiter
char *

variable_types: wc int

variable_types: count int

variable_types:
token
char *

variable_types:
s
char *

variable_types:
t
char *


original_code:
int move_char(int *index, char **in)
{
 if ((*in)[*index + 1] == '\0')
  *index = 0;
 int j, f = *index;
 char temp[strlen(*in)+1];
 for (j = 0; j < f; ++j)
  temp[j] = (*in)[j];
 temp[j] = (*in)[j+1];
 temp[j+1] = (*in)[j];
 for (j = j+2; j <= (int) strlen(*in); ++j)
  temp[j] = (*in)[j];
 strcpy(*in,temp);
 ++(*index);
 return 1;
}

canonical_original_code:
int move_char(int *index, char **in)
{
 if ((*in)[*index + 1] == '\0')
  *index = 0;
 int j; int f = *index;
 char temp[-1];
 for (j = 0; j < f; ++j)
  temp[j] = (*in)[j];
 temp[j] = (*in)[j+1];
 temp[j+1] = (*in)[j];
 for (j = j+2; j <= (int) strlen(*in); ++j)
  temp[j] = (*in)[j];
 strcpy(*in,temp);
 ++(*index);
 return 1;
}

variable_types:
index
int *

variable_types:
in
char **

variable_types: f int

variable_types:
temp
char [-1]


permute

decompiled_code:
const char *__fastcall permute(const char **a1, int *a2)
{
if ( *a2 >= (int)strlen(*a1) )
*a2 = 0;
if ( !(unsigned int)move_char(a2, a1) )
puts("Could not move char.");
return *a1;
}

canonical_decompiled_code:
const char *__fastcall func9(const char **a1, int *a2)
{
if ( *a2 >= (int)strlen(*a1) )
*a2 = 0;
if ( !(unsigned int)func8(a2, a1) )
puts("Could not move char.");
return *a1;
}

original_code:
char* permute(char **string, int *i)
{
 if (*i >= (int) strlen(*string))
  *i = 0;
 if (!move_char(i, string))
  printf("Could not move char.\n");
 return *string;
}

canonical_original_code:
char *permute(char **string, int *i)
{
 if (*i >= (int) strlen(*string))
  *i = 0;
 if (!move_char(i, string))
  printf("Could not move char.\n");
 return *string;
}

variable_types:
string
char **

variable_types:
i
int *


reverse

decompiled_code:
const char *__fastcall reverse(const char *a1)
{
__int64 v1; // rax
__int64 v2; // rdx
char v3; // cl

LODWORD(v1) = strlen(a1) - 1;
if ( (int)v1 > 0 )
{
v1 = (int)v1;
v2 = 0LL;
do
{
v3 = a1[v2];
a1[v2] = a1[v1];
a1[v1] = v3;
++v2;
--v1;
}
while ( (int)v1 > (int)v2 );
}
return a1;
}

canonical_decompiled_code:
const char *__fastcall func10(const char *a1)
{
__int64 v1; // rax
__int64 v2; // rdx
char v3; // cl

LODWORD(v1) = strlen(a1) - 1;
if ( (int)v1 > 0 )
{
v1 = (int)v1;
v2 = 0LL;
do
{
v3 = a1[v2];
a1[v2] = a1[v1];
a1[v1] = v3;
++v2;
--v1;
}
while ( (int)v1 > (int)v2 );
}
return a1;
}

original_code:
char* reverse(char *string)
{
 char temp;
 int i = 0, len = strlen(string)-1;
 if (len < 1)
  return string;
 char *str = string;
 while (i < (len-i)) {
  temp = str[i];
  str[i] = str[len-i];
  str[len-i] = temp;
  ++i;
 }
 return str;
}

canonical_original_code:
char *reverse(char *string)
{
 char temp;
 int i = 0; int len = strlen(string)-1;
 if (len < 1)
  return string;
 char *str = string;
 while (i < (len-i)) {
  temp = str[i];
  str[i] = str[len-i];
  str[len-i] = temp;
  ++i;
 }
 return str;
}

variable_types:
string
char *

variable_types: temp char

variable_types: i int

variable_types: len int


decompiled_code:
void swap_mem()
{
;
}

canonical_decompiled_code:
void func11()
{
;
}

original_code:
void swap_mem(void *one, void *two)
{
 void *temp = one;
 one = two;
 two = temp;
}

variable_types:
one
void *

variable_types:
two
void *

variable_types:
temp
void *


decompiled_code:
__int64 __fastcall add_ngram_element(void ***a1)
{
int v1; // esi
void **v2; // rbp

v1 = *((_DWORD *)*a1 + 2);
*((_DWORD *)*a1 + 2) = v1 + 1;
v2 = *a1;
*v2 = realloc(**a1, (unsigned int)(16 * (v1 + 2)));
if ( !**a1 )
{
printf("Assertion: '%s' failed.\n", "((*at_index)->elems != NULL)");
exit(9);
}
return (unsigned int)(*((_DWORD *)*a1 + 2) - 1);
}

canonical_decompiled_code:
__int64 __fastcall func2(void ***a1)
{
int v1; // esi
void **v2; // rbp

v1 = *((_DWORD *)*a1 + 2);
*((_DWORD *)*a1 + 2) = v1 + 1;
v2 = *a1;
*v2 = realloc(**a1, (unsigned int)(16 * (v1 + 2)));
if ( !**a1 )
{
printf("Assertion: '%s' failed.\n", "((*at_index)->elems != NULL)");
exit(9);
}
return (unsigned int)(*((_DWORD *)*a1 + 2) - 1);
}

original_code:
int add_ngram_element(struct word_t_array **at_index, int index)
{
 unsigned int size = sizeof(struct word_t) * (++((*at_index)->count) + 1);
 (*at_index)->elems = realloc((*at_index)->elems, size);
 if (((*at_index)->elems != ((void *)0)) == 0) { printf("Assertion: '%s' failed.\n", "((*at_index)->elems != NULL)"); exit(9); };;
 return (*at_index)->count - 1;
}

variable_types: struct word_t_array { struct word_t { unsigned int i; int freq; char *word; } *elems; unsigned int count; } **

variable_types: index int

variable_types:
size
unsigned int

user_defined_types: struct word_t_array { struct word_t *elems; unsigned int count; }


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

v1 = (void ***)((char *)ptr + 40);
do
{
free(*v1[5]);
free(**v1);
free(v1[5]);
free(*v1--);
}
while ( v1 != (void ***)((char *)ptr + 8) );
free(ptr);
}

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

v1 = (void ***)((char *)ptr + 40);
do
{
free(*v1[5]);
free(**v1);
free(v1[5]);
free(*v1--);
}
while ( v1 != (void ***)((char *)ptr + 8) );
free(ptr);
}

original_code:
void free_ngram(struct ngram_t *ng)
{
 int index;
 for (index = ((9/2) - 1); index >= 0; --index) {
  free(ng->after.at[index]->elems);
  free(ng->before.at[index]->elems);
  free(ng->after.at[index]);
  free(ng->before.at[index]);
 }
 free(ng);
}

variable_types: struct ngram_t { struct word_t { unsigned int i; int freq; char *word; } word; struct ngram_half_array { struct word_t_array { struct word_t *elems; unsigned int count; } *at[-1]; } before; struct ngram_half_array after; int refs_count; struct ngram_t **refs; } *

user_defined_types: struct ngram_t { struct word_t word; struct ngram_half_array before; struct ngram_half_array after; int refs_count; struct ngram_t **refs; }

user_defined_types: struct ngram_half_array { struct word_t_array *at[-1]; }


uniqify

original_code:
void uniqify(struct ngram_half_array *bora)
{
 int i, j, k;
 for (i = 0; i < (9/2); ++i) {
  int count = bora->at[i]->count;
  for (j = 0; j < count; ++j) {
   struct word_t *elems = malloc(sizeof(struct word_t) * (bora->at[i]->count + 1));
   int elems_count = 0;
   elems[elems_count++] = bora->at[i]->elems[j];
   struct word_t elem = elems[elems_count-1];

#pragma omp parallel for
   for (k = j+1; k < count - 1; ++k) {
    struct word_t comp = bora->at[i]->elems[k];
    if (comp.freq != 1)
     continue;
    if (strcmp(elem.word, comp.word) == 0)
     elem.freq += bora->at[i]->elems[k].freq;
    else
     elems[elems_count++] = bora->at[i]->elems[k];
   }
   bora->at[i]->elems = elems;
  }
 }
}

canonical_original_code:
void uniqify(struct ngram_half_array *bora)
{
 int i; int j; int k;
 for (i = 0; i < (9/2); ++i) {
  int count = bora->at[i]->count;
  for (j = 0; j < count; ++j) {
   struct word_t *elems = malloc(sizeof(struct word_t) * (bora->at[i]->count + 1));
   int elems_count = 0;
   elems[elems_count++] = bora->at[i]->elems[j];
   struct word_t elem = elems[elems_count-1];

#pragma omp parallel for
   for (k = j+1; k < count - 1; ++k) {
    struct word_t comp = bora->at[i]->elems[k];
    if (comp.freq != 1)
     continue;
    if (strcmp(elem.word, comp.word) == 0)
     elem.freq += bora->at[i]->elems[k].freq;
    else
     elems[elems_count++] = bora->at[i]->elems[k];
   }
   bora->at[i]->elems = elems;
  }
 }
}

variable_types: struct ngram_half_array { struct word_t_array { struct word_t { unsigned int i; int freq; char *word; } *elems; unsigned int count; } *at[-1]; } *

variable_types: j int

variable_types: k int

variable_types: count int


original_code:
int ngrams_similar(struct ngram_t a, struct ngram_t b)
{
 register int percent_similar = 0;
 register int elems_count = 0;
 register int arr_count = 0;
 register int count = 0;
 register int i, j;
 for (i = 0; i < (9/2); ++i)
  elems_count += a.before.at[i]->count;
 for (i = 0; i < (9/2); ++i)
  elems_count += a.after.at[i]->count;
 for (i = 0; i < (9/2); ++i)
  elems_count += b.before.at[i]->count;
 for (i = 0; i < (9/2); ++i)
  elems_count += b.after.at[i]->count;
 register char **arr = malloc(sizeof(char*) * elems_count);
 for (i = 0; i < (9/2); ++i)
  for (j = 0; j < a.before.at[i]->count; ++j)
   if (strcmp(a.before.at[i]->elems[j].word, "") != 0)
    arr[arr_count++] = a.before.at[i]->elems[j].word;
 for (i = 0; i < (9/2); ++i)
  for (j = 0; j < a.after.at[i]->count; ++j)
   if (strcmp(a.after.at[i]->elems[j].word, "") != 0)
    arr[arr_count++] = a.after.at[i]->elems[j].word;
 for (i = 0; i < (9/2); ++i)
  for (j = 0; j < b.before.at[i]->count; ++j)
   if (strcmp(b.before.at[i]->elems[j].word, "") != 0)
    arr[arr_count++] = b.before.at[i]->elems[j].word;
 for (i = 0; i < (9/2); ++i)
  for (j = 0; j < b.after.at[i]->count; ++j)
   if (strcmp(b.after.at[i]->elems[j].word, "") != 0)
    arr[arr_count++] = b.after.at[i]->elems[j].word;
        count = uniq_words(arr, elems_count);
 free(arr);
 arr_count -= 1;
 percent_similar = (double) 100.00 * (((double)arr_count - (double)count)/(double)arr_count);
 if (percent_similar >= 12.5)
  return 1;
 else
  return 0;
}

canonical_original_code:
int ngrams_similar(struct ngram_t a, struct ngram_t b)
{
 register int percent_similar = 0;
 register int elems_count = 0;
 register int arr_count = 0;
 register int count = 0;
 register int i; register int j;
 for (i = 0; i < (9/2); ++i)
  elems_count += a.before.at[i]->count;
 for (i = 0; i < (9/2); ++i)
  elems_count += a.after.at[i]->count;
 for (i = 0; i < (9/2); ++i)
  elems_count += b.before.at[i]->count;
 for (i = 0; i < (9/2); ++i)
  elems_count += b.after.at[i]->count;
 register char **arr = malloc(sizeof(char*) * elems_count);
 for (i = 0; i < (9/2); ++i)
  for (j = 0; j < a.before.at[i]->count; ++j)
   if (strcmp(a.before.at[i]->elems[j].word, "") != 0)
    arr[arr_count++] = a.before.at[i]->elems[j].word;
 for (i = 0; i < (9/2); ++i)
  for (j = 0; j < a.after.at[i]->count; ++j)
   if (strcmp(a.after.at[i]->elems[j].word, "") != 0)
    arr[arr_count++] = a.after.at[i]->elems[j].word;
 for (i = 0; i < (9/2); ++i)
  for (j = 0; j < b.before.at[i]->count; ++j)
   if (strcmp(b.before.at[i]->elems[j].word, "") != 0)
    arr[arr_count++] = b.before.at[i]->elems[j].word;
 for (i = 0; i < (9/2); ++i)
  for (j = 0; j < b.after.at[i]->count; ++j)
   if (strcmp(b.after.at[i]->elems[j].word, "") != 0)
    arr[arr_count++] = b.after.at[i]->elems[j].word;
        count = uniq_words(arr, elems_count);
 free(arr);
 arr_count -= 1;
 percent_similar = (double) 100.00 * (((double)arr_count - (double)count)/(double)arr_count);
 if (percent_similar >= 12.5)
  return 1;
 else
  return 0;
}

variable_types: struct ngram_t { struct word_t { unsigned int i; int freq; char *word; } word; struct ngram_half_array { struct word_t_array { struct word_t *elems; unsigned int count; } *at[-1]; } before; struct ngram_half_array after; int refs_count; struct ngram_t **refs; }

variable_types:
b
struct ngram_t

variable_types:
arr
char **


original_code:
void add_similar_ngram_ref(struct ngram_t **ngram, struct ngram_t **ngram_ref)
{
 int num_refs = (*ngram)->refs_count;
 int memsize = (sizeof(struct ngram**) * (num_refs+1));
 if (num_refs == 0) {
  (*ngram)->refs = malloc(memsize);
 } else {
  (*ngram)->refs = realloc((*ngram)->refs, memsize);
  if (((*ngram)->refs != ((void *)0)) == 0) { printf("Assertion: '%s' failed.\n", "((*ngram)->refs != NULL)"); exit(9); };;
 }
 (*ngram)->refs[(*ngram)->refs_count] = *ngram_ref;
 ++(*ngram)->refs_count;
}

variable_types: struct ngram_t { struct word_t { unsigned int i; int freq; char *word; } word; struct ngram_half_array { struct word_t_array { struct word_t *elems; unsigned int count; } *at[-1]; } before; struct ngram_half_array after; int refs_count; struct ngram_t **refs; } **

variable_types: memsize int


setword

decompiled_code:
char *__fastcall setword(__int64 a1, const char *a2)
{
char *result; // rax

*(_DWORD *)a1 = strlen(a2);
*(_DWORD *)(a1 + 4) = 1;
result = strdup(a2);
*(_QWORD *)(a1 + 8) = result;
return result;
}

canonical_decompiled_code:
char *__fastcall func0(__int64 a1, const char *a2)
{
char *result; // rax

*(_DWORD *)a1 = strlen(a2);
*(_DWORD *)(a1 + 4) = 1;
result = strdup(a2);
*(_QWORD *)(a1 + 8) = result;
return result;
}

original_code:
void setword(struct word_t *word, char *w)
{
 word->i = strlen(w);
 word->freq = 1;
 word->word = strdup(w);
}

variable_types: struct word_t { unsigned int i; int freq; char *word; } *

variable_types:
w
char *


word

decompiled_code:
__int64 __fastcall word(const char *a1)
{
__int64 v2[3]; // [rsp+0h] [rbp-18h] BYREF

v2[0] = 0LL;
v2[1] = 0LL;
setword((__int64)v2, a1);
return v2[0];
}

original_code:
struct word_t word(char *w)
{
 struct word_t a = {.i = 0};
 setword(&a, w);
 return a;
}

variable_types: struct word_t { unsigned int i; int freq; char *word; }


decompiled_code:
__int64 __fastcall reverse_word(__int64 a1, __int64 a2)
{
reverse(a2);
return (a1 >> 32 << 32) | (unsigned int)a1;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
reverse(a2);
return (a1 >> 32 << 32) | (unsigned int)a1;
}

original_code:
struct word_t reverse_word(struct word_t w)
{
 struct word_t temp;
 temp.i = w.i;
 temp.freq = w.freq;
 temp.word = reverse(w.word);
 return temp;
}

variable_types:
w
struct word_t

return_type: struct word_t { unsigned int i; int freq; char *word; }


decompiled_code:
__int64 __fastcall remove_first_char(__int64 a1, const char *a2)
{
char *v2; // rax
const char *v3; // rbx

v2 = strdup(a2);
v3 = (const char *)reverse(v2);
v3[strlen(v3) - 1] = 0;
return reverse(v3);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, const char *a2)
{
char *v2; // rax
const char *v3; // rbx

v2 = strdup(a2);
v3 = (const char *)reverse(v2);
v3[strlen(v3) - 1] = 0;
return reverse(v3);
}

original_code:
char* remove_first_char(struct word_t w)
{
 char *word = strdup(w.word);
 char *finalized = reverse(word);
 finalized[strlen(finalized)-1] = '\0';
 return reverse(finalized);
}

canonical_original_code:
char *remove_first_char(struct word_t w)
{
 char *word = strdup(w.word);
 char *finalized = reverse(word);
 finalized[strlen(finalized)-1] = '\0';
 return reverse(finalized);
}

variable_types:
word
char *

variable_types:
finalized
char *

user_defined_types: struct word_t { unsigned int i; int freq; char *word; }


decompiled_code:
void *ask_for_file()
{
int v0; // eax
void *i; // [rsp+0h] [rbp-18h] BYREF
int v3; // [rsp+8h] [rbp-10h] BYREF
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

v4[0] = 0;
v3 = 1;
for ( i = malloc(1uLL); ; read_by_char(v0, &i, v4, &v3) )
{
v0 = getchar();
if ( v0 == 10 || v0 == -1 )
break;
}
return i;
}

canonical_decompiled_code:
void *func1()
{
int v0; // eax
void *i; // [rsp+0h] [rbp-18h] BYREF
int v3; // [rsp+8h] [rbp-10h] BYREF
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

v4[0] = 0;
v3 = 1;
for ( i = malloc(1uLL); ; func0(v0, &i, v4, &v3) )
{
v0 = getchar();
if ( v0 == 10 || v0 == -1 )
break;
}
return i;
}

original_code:
char* ask_for_file(void)
{
 int c, charCount = 0, bytes = 1;
 char *filename;
 filename = malloc(sizeof(char) * bytes);
 while ((c = getchar()) != (-1) && c != '\n')
  read_by_char(c, &filename, &charCount, &bytes);
 return filename;
}

canonical_original_code:
char *ask_for_file()
{
 int c; int charCount = 0; int bytes = 1;
 char *filename;
 filename = malloc(sizeof(char) * bytes);
 while ((c = getchar()) != (-1) && c != '\n')
  read_by_char(c, &filename, &charCount, &bytes);
 return filename;
}

variable_types: c int

variable_types: charCount int

variable_types: bytes int

variable_types:
filename
char *


decompiled_code:
char *__fastcall find_word_delimiter(_QWORD *a1)
{
__int64 v1; // rdx
void *v2; // rsp
__int64 v3; // rbx
char *result; // rax
int v5; // eax
__int64 v6; // rbx
int v7; // er15
int v8; // er13
int v9; // eax
char *v10; // rbx
size_t v11; // rax
const char *v12; // rax
_BYTE v13[15]; // [rsp+0h] [rbp-70h] BYREF
char v14; // [rsp+Fh] [rbp-61h]
char *v15; // [rsp+10h] [rbp-60h] BYREF
void *v16; // [rsp+18h] [rbp-58h] BYREF
char *s; // [rsp+20h] [rbp-50h] BYREF
int v18; // [rsp+2Ch] [rbp-44h] BYREF
void *ptr; // [rsp+30h] [rbp-40h] BYREF
void *v20; // [rsp+38h] [rbp-38h] BYREF

if ( verbose_mode == 1 )
puts("Creating List of Uniq Chars.");
v18 = uniq(a1, &v20);
if ( v18 == -1 )
{
*_errno_location() = 6;
result = (char *)&unk_29B;
}
else
{
if ( verbose_mode == 1 )
puts("Constructing Word Delimiter Structures.");
v1 = (unsigned int)v18;
v2 = alloca(8LL * v18);
if ( v18 >= 0 )
{
v3 = 0LL;
do
{
if ( verbose_mode == 1 )
printf("Working on character %d/%d...\r", (unsigned int)v3, v1);
LOWORD(v15) = *((unsigned __int8 *)v20 + v3);
v5 = explode_sansnull(&ptr, *a1, &v15);
v13[8 * v3] = *((_BYTE *)v20 + v3);
*(_DWORD *)&v13[8 * v3 + 4] = v5;
free(ptr);
v1 = (unsigned int)v18;
++v3;
}
while ( v18 >= (int)v3 );
}
free(v20);
if ( verbose_mode == 1 )
puts("Initial Word Delimiter Structures Constructed.");
s = (char *)malloc(2uLL);
if ( v18 >= 0 )
{
v6 = 0LL;
v7 = 0;
v8 = 0;
do
{
v9 = *(_DWORD *)&v13[8 * v6 + 4];
if ( v9 <= v8 )
{
if ( v9 == v8 )
{
v14 = v13[8 * v6];
if ( !(unsigned int)in_array((unsigned int)v14, &s, (unsigned int)v7) )
{
s[v7++] = v14;
s[v7] = 0;
s = (char *)realloc(s, v7 + 2LL);
if ( !s )
{
printf("Assertion: '%s' failed.\n", "(wd_real != NULL)");
exit(9);
}
uniq(&s, &v15);
free(s);
s = v15;
}
}
}
else
{
*(_WORD *)s = (unsigned __int8)v13[8 * v6];
v8 = v9;
v7 = 1;
}
++v6;
}
while ( v18 >= (int)v6 );
}
if ( verbose_mode == 1 )
puts("Word Delimiter Candidates Identified.");
v10 = s;
v11 = strlen(s);
v16 = malloc(v11 + 1);
strcpy((char *)v16, v10);
v18 = 0;
if ( strlen(v10) > 1 )
{
while ( 1 )
{
v12 = (const char *)permute(&v16, &v18);
if ( !strcmp(s, v12) )
break;
v15 = (char *)v16;
explode_sansnull_str(&ptr, *a1, &v15);
}
}
free(v16);
result = s;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func0(_QWORD *a1)
{
__int64 v1; // rdx
void *v2; // rsp
__int64 v3; // rbx
char *result; // rax
int v5; // eax
__int64 v6; // rbx
int v7; // er15
int v8; // er13
int v9; // eax
char *v10; // rbx
size_t v11; // rax
const char *v12; // rax
_BYTE v13[15]; // [rsp+0h] [rbp-70h] BYREF
char v14; // [rsp+Fh] [rbp-61h]
char *v15; // [rsp+10h] [rbp-60h] BYREF
void *v16; // [rsp+18h] [rbp-58h] BYREF
char *s; // [rsp+20h] [rbp-50h] BYREF
int v18; // [rsp+2Ch] [rbp-44h] BYREF
void *ptr; // [rsp+30h] [rbp-40h] BYREF
void *v20; // [rsp+38h] [rbp-38h] BYREF

if ( verbose_mode == 1 )
puts("Creating List of Uniq Chars.");
v18 = uniq(a1, &v20);
if ( v18 == -1 )
{
*_errno_location() = 6;
result = (char *)&unk_29B;
}
else
{
if ( verbose_mode == 1 )
puts("Constructing Word Delimiter Structures.");
v1 = (unsigned int)v18;
v2 = alloca(8LL * v18);
if ( v18 >= 0 )
{
v3 = 0LL;
do
{
if ( verbose_mode == 1 )
printf("Working on character %d/%d...\r", (unsigned int)v3, v1);
LOWORD(v15) = *((unsigned __int8 *)v20 + v3);
v5 = explode_sansnull(&ptr, *a1, &v15);
v13[8 * v3] = *((_BYTE *)v20 + v3);
*(_DWORD *)&v13[8 * v3 + 4] = v5;
free(ptr);
v1 = (unsigned int)v18;
++v3;
}
while ( v18 >= (int)v3 );
}
free(v20);
if ( verbose_mode == 1 )
puts("Initial Word Delimiter Structures Constructed.");
s = (char *)malloc(2uLL);
if ( v18 >= 0 )
{
v6 = 0LL;
v7 = 0;
v8 = 0;
do
{
v9 = *(_DWORD *)&v13[8 * v6 + 4];
if ( v9 <= v8 )
{
if ( v9 == v8 )
{
v14 = v13[8 * v6];
if ( !(unsigned int)in_array((unsigned int)v14, &s, (unsigned int)v7) )
{
s[v7++] = v14;
s[v7] = 0;
s = (char *)realloc(s, v7 + 2LL);
if ( !s )
{
printf("Assertion: '%s' failed.\n", "(wd_real != NULL)");
exit(9);
}
uniq(&s, &v15);
free(s);
s = v15;
}
}
}
else
{
*(_WORD *)s = (unsigned __int8)v13[8 * v6];
v8 = v9;
v7 = 1;
}
++v6;
}
while ( v18 >= (int)v6 );
}
if ( verbose_mode == 1 )
puts("Word Delimiter Candidates Identified.");
v10 = s;
v11 = strlen(s);
v16 = malloc(v11 + 1);
strcpy((char *)v16, v10);
v18 = 0;
if ( strlen(v10) > 1 )
{
while ( 1 )
{
v12 = (const char *)permute(&v16, &v18);
if ( !strcmp(s, v12) )
break;
v15 = (char *)v16;
explode_sansnull_str(&ptr, *a1, &v15);
}
}
free(v16);
result = s;
}
return result;
}

original_code:
char* find_word_delimiter(char **file)
{
 char *ret, **arr;
 if (verbose_mode == 1) printf("%s\n", "Creating List of Uniq Chars.");;
 int j, i = uniq(file, &ret);
 if (i == -1) {
  (*__errno_location ()) = 6;
  return "";
 }
 if (verbose_mode == 1) printf("%s\n", "Constructing Word Delimiter Structures.");;
 struct char_doubleton wd[i];
 for (j = 0; j <= i; ++j) {
  if (verbose_mode == 1)
   printf("Working on character %d/%d...\r", j, i);
  const char c[2] = {ret[j], '\0'};
  int freq = explode_sansnull(&arr, *file, c);
  wd[j].c = ret[j];
  wd[j].freq = freq;
  free(arr);
 }
 free(ret);
 if (verbose_mode == 1) printf("%s\n", "Initial Word Delimiter Structures Constructed.");;
 int max_size = 0, index = 0;
 char *wd_real = malloc(sizeof(char) * 2);
 for (j = 0; j <= i; ++j) {
  if (wd[j].freq > max_size) {
   index = 0;
   wd_real[index] = wd[j].c;
   wd_real[++index] = '\0';
   max_size = wd[j].freq;
  }
  else if (wd[j].freq == max_size && !in_array((wd[j].c), &wd_real, index)) {
   wd_real[index] = wd[j].c;
   wd_real[++index] = '\0';
   wd_real = realloc(wd_real, sizeof(char) * index+2);
   if ((wd_real != ((void *)0)) == 0) { printf("Assertion: '%s' failed.\n", "(wd_real != NULL)"); exit(9); };;
   char *uniqd;
   uniq(&wd_real, &uniqd);
   free(wd_real);
   wd_real = uniqd;
  }
 }
 if (verbose_mode == 1) printf("%s\n", "Word Delimiter Candidates Identified.");;
 char *wd_final;
 char *wd_permuted = malloc(strlen(wd_real) * sizeof(char) + 1);
 strcpy(wd_permuted, wd_real);
 int wd_final_freq = 0, size;
 i = 0;
 if(strlen(wd_real) > 1)
  while(strcmp(wd_real, permute(&wd_permuted, &i)) != 0) {
   const char *wd_testing = wd_permuted;
   if ((size = explode_sansnull_str(&arr, *file, &wd_testing)) > wd_final_freq) {
    size = wd_final_freq;
    wd_final = wd_permuted;
   } else if (size == wd_final_freq && size != 0) {
    printf("This implementation is not designed to handle multiple word delimiters.\nSkipping this file...\n");
    (*__errno_location ()) = 8;
   }
  }
 free(wd_permuted);
 if (wd_final_freq == 0)
  return wd_real;
 else
  return wd_final;
}

canonical_original_code:
char *find_word_delimiter(char **file)
{
 char *ret; char **arr;
 if (verbose_mode == 1) printf("%s\n", "Creating List of Uniq Chars.");;
 int j; int i = uniq(file, &ret);
 if (i == -1) {
  (*__errno_location ()) = 6;
  return "";
 }
 if (verbose_mode == 1) printf("%s\n", "Constructing Word Delimiter Structures.");;
 struct char_doubleton wd[-1];
 for (j = 0; j <= i; ++j) {
  if (verbose_mode == 1)
   printf("Working on character %d/%d...\r", j, i);
  char c[2] = {ret[j], '\0'};
  int freq = explode_sansnull(&arr, *file, c);
  wd[j].c = ret[j];
  wd[j].freq = freq;
  free(arr);
 }
 free(ret);
 if (verbose_mode == 1) printf("%s\n", "Initial Word Delimiter Structures Constructed.");;
 int max_size = 0; int index = 0;
 char *wd_real = malloc(sizeof(char) * 2);
 for (j = 0; j <= i; ++j) {
  if (wd[j].freq > max_size) {
   index = 0;
   wd_real[index] = wd[j].c;
   wd_real[++index] = '\0';
   max_size = wd[j].freq;
  }
  else if (wd[j].freq == max_size && !in_array((wd[j].c), &wd_real, index)) {
   wd_real[index] = wd[j].c;
   wd_real[++index] = '\0';
   wd_real = realloc(wd_real, sizeof(char) * index+2);
   if ((wd_real != ((void *)0)) == 0) { printf("Assertion: '%s' failed.\n", "(wd_real != NULL)"); exit(9); };;
   char *uniqd;
   uniq(&wd_real, &uniqd);
   free(wd_real);
   wd_real = uniqd;
  }
 }
 if (verbose_mode == 1) printf("%s\n", "Word Delimiter Candidates Identified.");;
 char *wd_final;
 char *wd_permuted = malloc(strlen(wd_real) * sizeof(char) + 1);
 strcpy(wd_permuted, wd_real);
 int wd_final_freq = 0; int size;
 i = 0;
 if(strlen(wd_real) > 1)
  while(strcmp(wd_real, permute(&wd_permuted, &i)) != 0) {
   char *wd_testing = wd_permuted;
   if ((size = explode_sansnull_str(&arr, *file, &wd_testing)) > wd_final_freq) {
    size = wd_final_freq;
    wd_final = wd_permuted;
   } else if (size == wd_final_freq && size != 0) {
    printf("This implementation is not designed to handle multiple word delimiters.\nSkipping this file...\n");
    (*__errno_location ()) = 8;
   }
  }
 free(wd_permuted);
 if (wd_final_freq == 0)
  return wd_real;
 else
  return wd_final;
}

variable_types:
file
char **

variable_types:
ret
char *

variable_types: j int

variable_types: i int

variable_types: struct char_doubleton { char c; int freq; } [-1]

variable_types:
c
char [2]

variable_types: freq int

variable_types: index int

variable_types:
uniqd
char *

variable_types: size int


getfiles

decompiled_code:
__int64 __fastcall getfiles(_DWORD *a1, char **a2)
{
int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = 0;
return _gf(corpus_dir, a1, v3, a2);
}

canonical_decompiled_code:
__int64 __fastcall func1(_DWORD *a1, char **a2)
{
int v3[3]; // [rsp+Ch] [rbp-Ch] BYREF

v3[0] = 0;
return func0(corpus_dir, a1, v3, a2);
}

original_code:
char* getfiles(int *index, char **header)
{
 int count = 0;
 return __gf(corpus_dir, index, &count, header);
}

canonical_original_code:
char *getfiles(int *index, char **header)
{
 int count = 0;
 return __gf(corpus_dir, index, &count, header);
}

variable_types:
index
int *

variable_types:
header
char **

variable_types: count int


repo: zackw/moeller-ref

decompiled_code:
__int64 report_failure(__int64 a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

if ( !set_failure )
{
fwrite("FAIL\n", 1uLL, 5uLL, stdout);
set_failure = 1;
failure = 1;
}
va_start(va, a1);
_vprintf_chk(1LL, a1, va);
putchar(10);
return ERR_print_errors_fp(stdout);
}

canonical_decompiled_code:
__int64 func0(__int64 a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

if ( !set_failure )
{
fwrite("FAIL\n", 1uLL, 5uLL, stdout);
set_failure = 1;
failure = 1;
}
va_start(va, a1);
_vprintf_chk(1LL, a1, va);
putchar(10);
return ERR_print_errors_fp(stdout);
}

original_code:
static void
report_failure(const char *label, ...)
{
  va_list ap;
  if (!set_failure) {
    fputs("FAIL\n", stdout);
    set_failure = 1;
    failure = 1;
  }
  __builtin_va_start(ap,label);
  vprintf(label, ap);
  __builtin_va_end(ap);
  putchar('\n');
  ERR_print_errors_fp(stdout);
}

canonical_original_code:
static void report_failure(char *label, ...)
{
  __builtin_va_list ap;
  if (!set_failure) {
    fputs("FAIL\n", stdout);
    set_failure = 1;
    failure = 1;
  }
  __builtin_va_start(ap,label);
  vprintf(label, ap);
  __builtin_va_end(ap);
  putchar('\n');
  ERR_print_errors_fp(stdout);
}

variable_types:
label
char *


decompiled_code:
int __fastcall print_hexarray(unsigned __int8 *a1, __int64 a2, const char *a3)
{
unsigned __int8 *v3; // rbx
int result; // eax

v3 = a1;
result = fputs(a3, stdout);
if ( a1 < &a1[a2] )
{
do
result = _printf_chk(1LL, "%02x", *v3++);
while ( &a1[a2] != v3 );
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(unsigned __int8 *a1, __int64 a2, const char *a3)
{
unsigned __int8 *v3; // rbx
int result; // eax

v3 = a1;
result = fputs(a3, stdout);
if ( a1 < &a1[a2] )
{
do
result = _printf_chk(1LL, "%02x", *v3++);
while ( &a1[a2] != v3 );
}
return result;
}

original_code:
static void
print_hexarray(const uint8_t *b, size_t n, const char *prefix)
{
  const uint8_t *limit = b + n;
  fputs(prefix, stdout);
  while (b < limit)
    printf("%02x", *b++);
}

canonical_original_code:
static void print_hexarray(unsigned char *b, unsigned long n, char *prefix)
{
  unsigned char *limit = b + n;
  fputs(prefix, stdout);
  while (b < limit)
    printf("%02x", *b++);
}

variable_types:
b
unsigned char *

variable_types:
n
unsigned long

variable_types:
prefix
char *

variable_types:
limit
unsigned char *


decompiled_code:
int __fastcall compare_failure(unsigned __int8 *a1, unsigned __int8 *a2, __int64 a3, __int64 a4)
{
report_failure(a4);
print_hexarray(a2, a3, " expected: ");
putchar(10);
print_hexarray(a1, a3, " got: ");
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func3(unsigned __int8 *a1, unsigned __int8 *a2, __int64 a3, __int64 a4)
{
func0(a4);
func2(a2, a3, " expected: ");
putchar(10);
func2(a1, a3, " got: ");
return putchar(10);
}

original_code:
static void
compare_failure(const uint8_t *got, const uint8_t *exp, size_t len,
                const char *label)
{
  report_failure(label);
  print_hexarray(exp, len, " expected: ");
  putchar('\n');
  print_hexarray(got, len, "      got: ");
  putchar('\n');
}

canonical_original_code:
static void compare_failure(unsigned char *got, unsigned char *exp, unsigned long len, char *label)
{
  report_failure(label);
  print_hexarray(exp, len, " expected: ");
  putchar('\n');
  print_hexarray(got, len, "      got: ");
  putchar('\n');
}

variable_types:
got
unsigned char *

variable_types:
exp
unsigned char *

variable_types:
len
unsigned long


repo: zaitcev/ruat

decompiled_code:
__int64 __fastcall upd_init(__int64 a1, int a2)
{
void *v2; // rax

v2 = malloc(4LL * a2);
if ( !v2 )
return 0xFFFFFFFFLL;
*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
*(_DWORD *)(a1 + 12) = a2;
*(_QWORD *)a1 = v2;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
void *v2; // rax

v2 = malloc(4LL * a2);
if ( !v2 )
return 0xFFFFFFFFLL;
*(_QWORD *)(a1 + 8) = 0LL;
*(_QWORD *)(a1 + 16) = 0LL;
*(_DWORD *)(a1 + 12) = a2;
*(_QWORD *)a1 = v2;
return 0LL;
}

original_code:
int upd_init(struct upd *up, int length)
{
 void *p;
 p = malloc(length * sizeof(int));
 if (!p)
  return -1;
 memset(up, 0, sizeof(struct upd));
 up->len = length;
 up->vec = p;
 return 0;
}

variable_types: struct upd { int *vec; int cur; int len; unsigned int x; } *

variable_types: length int

variable_types:
p
void *


original_code:
int upd_ate(struct upd *up, int p)
{
 int sub;
 unsigned int x;
 x = up->x;
 sub = up->vec[x];
 up->vec[x] = p;
 up->x = (x + 1) % up->len;
 up->cur -= sub;
 up->cur += p;
 return up->cur / up->len;
}

variable_types: p int

variable_types: sub int

variable_types:
x
unsigned int

user_defined_types: struct upd { int *vec; int cur; int len; unsigned int x; }


decompiled_code:
void __fastcall upd_fini(void **a1)
{
free(*a1);
*a1 = 0LL;
}

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

original_code:
void upd_fini(struct upd *up)
{
 free(up->vec);
 up->vec = ((void *)0);
}


decompiled_code:
__int64 test_gen_uat()
{
unsigned int v0; // eax
_BYTE v2[24]; // [rsp+0h] [rbp-18h] BYREF

v0 = gf_init(v2, 391LL);
if ( v0 )
{
fprintf(stderr, "tester: gf_init(0x%x) error: %d\n", 391LL, v0);
exit(1);
}
return gf_fin(v2);
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // eax
_BYTE v2[24]; // [rsp+0h] [rbp-18h] BYREF

v0 = gf_init(v2, 391LL);
if ( v0 )
{
fprintf(stderr, "tester: gf_init(0x%x) error: %d\n", 391LL, v0);
exit(1);
}
return gf_fin(v2);
}

original_code:
static void test_gen_uat(void)
{
 struct gf field;
 int rc;
 rc = gf_init(&field, 0x187);
 if (rc != 0) {
  fprintf(stderr, "tester" ": gf_init(0x%x) error: %d\n",
      0x187, rc);
  exit(1);
 }
 gf_fin(&field);
}

canonical_original_code:
static void test_gen_uat()
{
 struct gf field;
 int rc;
 rc = gf_init(&field, 0x187);
 if (rc != 0) {
  fprintf(stderr, "tester" ": gf_init(0x%x) error: %d\n",
      0x187, rc);
  exit(1);
 }
 gf_fin(&field);
}

variable_types: struct gf { unsigned char *field; unsigned char *index; }

variable_types: rc int


original_code:
static void test_gen_lc(void)
{
 unsigned char sample[256];
 struct gf field;
 char *p;
 unsigned int v;
 int i, j;
 int error;
 int rc;
 for (i = 0; i < 256; i++) {
  p = gf256_8_4_3_2_0[i];
  if (p[0] != 0) {
   v = 0;
   for (j = 8; j-- != 0;) {
    v <<= 1;
    v |= p[j] & 1;
   }
   sample[i] = v;
  } else {
   sample[i] = 0xee;
  }
 }
 rc = gf_init(&field, 0x11d);
 if (rc != 0) {
  fprintf(stderr, "tester" ": gf_init(0x%x) error: %d\n",
      0x11d, rc);
  exit(1);
 }
 error = 0;
 for (i = 0; i < 256; i++) {
  if (gf256_8_4_3_2_0[i][0] != 0) {
   if (sample[i] != field.field[i]) {
    fprintf(stderr, "tester" ": "
        "mismatch [%d] 0x%02x 0x%02x\n",
        i, sample[i], field.field[i]);
    error = 1;
   }
  }
 }
 if (error)
  exit(1);
 test_field_lc(&field);
 gf_fin(&field);
}

canonical_original_code:
static void test_gen_lc()
{
 unsigned char sample[256];
 struct gf field;
 char *p;
 unsigned int v;
 int i; int j;
 int error;
 int rc;
 for (i = 0; i < 256; i++) {
  p = gf256_8_4_3_2_0[i];
  if (p[0] != 0) {
   v = 0;
   for (j = 8; j-- != 0;) {
    v <<= 1;
    v |= p[j] & 1;
   }
   sample[i] = v;
  } else {
   sample[i] = 0xee;
  }
 }
 rc = gf_init(&field, 0x11d);
 if (rc != 0) {
  fprintf(stderr, "tester" ": gf_init(0x%x) error: %d\n",
      0x11d, rc);
  exit(1);
 }
 error = 0;
 for (i = 0; i < 256; i++) {
  if (gf256_8_4_3_2_0[i][0] != 0) {
   if (sample[i] != field.field[i]) {
    fprintf(stderr, "tester" ": "
        "mismatch [%d] 0x%02x 0x%02x\n",
        i, sample[i], field.field[i]);
    error = 1;
   }
  }
 }
 if (error)
  exit(1);
 test_field_lc(&field);
 gf_fin(&field);
}

variable_types:
sample
unsigned char [256]

variable_types:
p
char *

variable_types:
v
unsigned int

variable_types: i int

variable_types: j int

variable_types: error int

user_defined_types: struct gf { unsigned char *field; unsigned char *index; }


decompiled_code:
void __fastcall test_gen_gen(unsigned int a1, unsigned int a2, int a3, const void *a4)
{
unsigned int v5; // er15
size_t v6; // r13
_BYTE *v7; // rax
_BYTE *v8; // rbp
unsigned int v9; // eax
unsigned int v10; // eax
char v12[72]; // [rsp+10h] [rbp-48h] BYREF

v5 = a2 + a3 - 1;
v6 = a3 + 2;
v7 = malloc(v6);
if ( !v7 )
{
fwrite("tester: No core\n", 1uLL, 0x10uLL, stderr);
exit(1);
}
v8 = v7;
v9 = gf_init(v12, (unsigned __int16)a1);
if ( v9 )
{
fprintf(stderr, "tester: gf_init(0x%x) error: %d\n", a1, v9);
exit(1);
}
memset(v8, 229, v6);
v10 = p_gen_gen(v12, v8 + 1, a2, v5);
if ( v10 )
{
fprintf(stderr, "tester: p_gen_gen(0x%x,%d,%d) error: %d\n", a1, a2, v5, v10);
exit(1);
}
if ( *v8 != 0xE5 || v8[a3 + 1] != 0xE5 )
{
fprintf(stderr, "tester: p_gen_gen(0x%x,%d,%d) destination overflow\n", a1, a2, v5);
exit(1);
}
if ( memcmp(v8 + 1, a4, a3) )
{
fprintf(stderr, "tester: p_gen_gen(0x%x,%d,%d) sample mismatch\n", a1, a2, v5);
exit(1);
}
gf_fin(v12);
free(v8);
}

canonical_decompiled_code:
void __fastcall func3(unsigned int a1, unsigned int a2, int a3, const void *a4)
{
unsigned int v5; // er15
size_t v6; // r13
_BYTE *v7; // rax
_BYTE *v8; // rbp
unsigned int v9; // eax
unsigned int v10; // eax
char v12[72]; // [rsp+10h] [rbp-48h] BYREF

v5 = a2 + a3 - 1;
v6 = a3 + 2;
v7 = malloc(v6);
if ( !v7 )
{
fwrite("tester: No core\n", 1uLL, 0x10uLL, stderr);
exit(1);
}
v8 = v7;
v9 = gf_init(v12, (unsigned __int16)a1);
if ( v9 )
{
fprintf(stderr, "tester: gf_init(0x%x) error: %d\n", a1, v9);
exit(1);
}
memset(v8, 229, v6);
v10 = p_gen_gen(v12, v8 + 1, a2, v5);
if ( v10 )
{
fprintf(stderr, "tester: p_gen_gen(0x%x,%d,%d) error: %d\n", a1, a2, v5, v10);
exit(1);
}
if ( *v8 != 0xE5 || v8[a3 + 1] != 0xE5 )
{
fprintf(stderr, "tester: p_gen_gen(0x%x,%d,%d) destination overflow\n", a1, a2, v5);
exit(1);
}
if ( memcmp(v8 + 1, a4, a3) )
{
fprintf(stderr, "tester: p_gen_gen(0x%x,%d,%d) sample mismatch\n", a1, a2, v5);
exit(1);
}
gf_fin(v12);
free(v8);
}

original_code:
static void test_gen_gen(unsigned int poly, int rpow, int len,
    const unsigned char *sample)
{
 int rpow_end = rpow + len - 1;
 struct gf field;
 unsigned char *buf;
 int rc;
 buf = malloc(len + 2);
 if (!buf) {
  fprintf(stderr, "tester" ": No core\n");
  exit(1);
 }
 rc = gf_init(&field, poly);
 if (rc != 0) {
  fprintf(stderr, "tester" ": gf_init(0x%x) error: %d\n", poly, rc);
  exit(1);
 }
 memset(buf, 0xe5, len+2);
 rc = p_gen_gen(&field, buf+1, rpow, rpow_end);
 if (rc != 0) {
  fprintf(stderr, "tester" ": p_gen_gen(0x%x,%d,%d) error: %d\n",
      poly, rpow, rpow_end, rc);
  exit(1);
 }
 if (buf[0] != 0xe5 || buf[len+1] != 0xe5) {
  fprintf(stderr, "tester" ": "
      "p_gen_gen(0x%x,%d,%d) destination overflow\n",
      poly, rpow, rpow_end);
  exit(1);
 }
 if (memcmp(buf+1, sample, len) != 0) {
  fprintf(stderr, "tester" ": "
      "p_gen_gen(0x%x,%d,%d) sample mismatch\n",
      poly, rpow, rpow_end);
  exit(1);
 }
 gf_fin(&field);
 free(buf);
}

canonical_original_code:
static void test_gen_gen(unsigned int poly, int rpow, int len, unsigned char *sample)
{
 int rpow_end = rpow + len - 1;
 struct gf field;
 unsigned char *buf;
 int rc;
 buf = malloc(len + 2);
 if (!buf) {
  fprintf(stderr, "tester" ": No core\n");
  exit(1);
 }
 rc = gf_init(&field, poly);
 if (rc != 0) {
  fprintf(stderr, "tester" ": gf_init(0x%x) error: %d\n", poly, rc);
  exit(1);
 }
 memset(buf, 0xe5, len+2);
 rc = p_gen_gen(&field, buf+1, rpow, rpow_end);
 if (rc != 0) {
  fprintf(stderr, "tester" ": p_gen_gen(0x%x,%d,%d) error: %d\n",
      poly, rpow, rpow_end, rc);
  exit(1);
 }
 if (buf[0] != 0xe5 || buf[len+1] != 0xe5) {
  fprintf(stderr, "tester" ": "
      "p_gen_gen(0x%x,%d,%d) destination overflow\n",
      poly, rpow, rpow_end);
  exit(1);
 }
 if (memcmp(buf+1, sample, len) != 0) {
  fprintf(stderr, "tester" ": "
      "p_gen_gen(0x%x,%d,%d) sample mismatch\n",
      poly, rpow, rpow_end);
  exit(1);
 }
 gf_fin(&field);
 free(buf);
}

variable_types:
poly
unsigned int

variable_types: rpow int

variable_types: len int

variable_types:
sample
unsigned char *

variable_types:
buf
unsigned char *