decompiled_code:
bool __fastcall dt_is_basic(_QWORD *a1)
{
__int64 v1; // rsi
int v2; // eax

v1 = dt_basic_get_type();
v2 = 0;
if ( a1 )
{
if ( !*a1 || (v2 = 1, *(_QWORD *)*a1 != v1) )
v2 = g_type_check_instance_is_a(a1, v1);
}
return v2 != 0;
}

canonical_decompiled_code:
bool __fastcall func2(_QWORD *a1)
{
__int64 v1; // rsi
int v2; // eax

v1 = func1();
v2 = 0;
if ( a1 )
{
if ( !*a1 || (v2 = 1, *(_QWORD *)*a1 != v1) )
v2 = g_type_check_instance_is_a(a1, v1);
}
return v2 != 0;
}

original_code:
_Bool
dt_is_basic(void *obj)
{
    return ((__extension__ ({ GTypeInstance *__inst = (GTypeInstance*) ((obj)); GType __t = (dt_basic_get_type()); gboolean __r; if (!__inst) __r = (0); else if (__inst->g_class && __inst->g_class->g_type == __t) __r = (!(0)); else __r = g_type_check_instance_is_a (__inst, __t); __r; })));
}

canonical_original_code:
_Bool dt_is_basic(void *obj)
{
    return ((__extension__ ({ struct _GTypeInstance *__inst = (GTypeInstance*) ((obj)); unsigned long __t = (dt_basic_get_type()); int __r; if (!__inst) __r = (0); else if (__inst->g_class && __inst->g_class->g_type == __t) __r = (!(0)); else __r = g_type_check_instance_is_a (__inst, __t); __r; })));
}


repo: lennartblanco/zdc

decompiled_code:
bool __fastcall ir_is_array_literal(_QWORD *a1)
{
__int64 v1; // rsi
int v2; // eax

v1 = ir_array_literal_get_type();
v2 = 0;
if ( a1 )
{
if ( !*a1 || (v2 = 1, *(_QWORD *)*a1 != v1) )
v2 = g_type_check_instance_is_a(a1, v1);
}
return v2 != 0;
}

original_code:
_Bool
ir_is_array_literal(void *obj)
{
    return ((__extension__ ({ GTypeInstance *__inst = (GTypeInstance*) ((obj)); GType __t = (ir_array_literal_get_type()); gboolean __r; if (!__inst) __r = (0); else if (__inst->g_class && __inst->g_class->g_type == __t) __r = (!(0)); else __r = g_type_check_instance_is_a (__inst, __t); __r; })));
}

canonical_original_code:
_Bool ir_is_array_literal(void *obj)
{
    return ((__extension__ ({ struct _GTypeInstance *__inst = (GTypeInstance*) ((obj)); unsigned long __t = (ir_array_literal_get_type()); int __r; if (!__inst) __r = (0); else if (__inst->g_class && __inst->g_class->g_type == __t) __r = (!(0)); else __r = g_type_check_instance_is_a (__inst, __t); __r; })));
}

variable_types:
obj
void *

variable_types: struct _GTypeInstance { struct _GTypeClass { unsigned long g_type; } *g_class; } *

user_defined_types: struct _GTypeInstance { struct _GTypeClass *g_class; }


decompiled_code:
bool __fastcall dt_is_enum(_QWORD *a1)
{
__int64 v1; // rsi
int v2; // eax

v1 = dt_enum_get_type();
v2 = 0;
if ( a1 )
{
if ( !*a1 || (v2 = 1, *(_QWORD *)*a1 != v1) )
v2 = g_type_check_instance_is_a(a1, v1);
}
return v2 != 0;
}

original_code:
_Bool
dt_is_enum(void *obj)
{
    return ((__extension__ ({ GTypeInstance *__inst = (GTypeInstance*) ((obj)); GType __t = (dt_enum_get_type()); gboolean __r; if (!__inst) __r = (0); else if (__inst->g_class && __inst->g_class->g_type == __t) __r = (!(0)); else __r = g_type_check_instance_is_a (__inst, __t); __r; })));
}

canonical_original_code:
_Bool dt_is_enum(void *obj)
{
    return ((__extension__ ({ struct _GTypeInstance *__inst = (GTypeInstance*) ((obj)); unsigned long __t = (dt_enum_get_type()); int __r; if (!__inst) __r = (0); else if (__inst->g_class && __inst->g_class->g_type == __t) __r = (!(0)); else __r = g_type_check_instance_is_a (__inst, __t); __r; })));
}

user_defined_types: struct _GTypeClass { unsigned long g_type; }


decompiled_code:
__int64 ir_linkage_attr_get_type()
{
if ( !linkage_attr_type_11 )
linkage_attr_type_11 = g_enum_register_static("IrLinkageAttrType", &linkage_attr_10);
return linkage_attr_type_11;
}

canonical_decompiled_code:
__int64 func1()
{
if ( !linkage_attr_type_11 )
linkage_attr_type_11 = g_enum_register_static("IrLinkageAttrType", &linkage_attr_10);
return linkage_attr_type_11;
}

original_code:
static GType
ir_linkage_attr_get_type(void)
{
  static GType linkage_attr_type = 0;
  static GEnumValue linkage_attr[] =
  {
      {ir_d_linkage, "D", "D linkage"},
      {ir_c_linkage, "C", "C linkage"},
      {0, ((void *)0), ((void *)0)}
  };
  if (linkage_attr_type == 0)
  {
      linkage_attr_type =
          g_enum_register_static("IrLinkageAttrType", linkage_attr);
  }
  return linkage_attr_type;
}

canonical_original_code:
static unsigned long ir_linkage_attr_get_type()
{
  static unsigned long linkage_attr_type = 0;
  static struct _GEnumValue linkage_attr[] = {
      {ir_d_linkage, "D", "D linkage"},
      {ir_c_linkage, "C", "C linkage"},
      {0, ((void *)0), ((void *)0)}
  };
  if (linkage_attr_type == 0)
  {
      linkage_attr_type =
          g_enum_register_static("IrLinkageAttrType", linkage_attr);
  }
  return linkage_attr_type;
}

variable_types: struct _GEnumValue { int value; char *value_name; char *value_nick; } []

return_type: unsigned long


decompiled_code:
bool __fastcall iml_is_variable(_QWORD *a1, __int64 a2)
{
__int64 v2; // rsi
int v3; // eax

v2 = iml_variable_get_type((__int64)a1, a2);
v3 = 0;
if ( a1 )
{
if ( !*a1 || (v3 = 1, *(_QWORD *)*a1 != v2) )
v3 = g_type_check_instance_is_a(a1, v2);
}
return v3 != 0;
}

canonical_decompiled_code:
bool __fastcall func2(_QWORD *a1, __int64 a2)
{
__int64 v2; // rsi
int v3; // eax

v2 = func1((__int64)a1, a2);
v3 = 0;
if ( a1 )
{
if ( !*a1 || (v3 = 1, *(_QWORD *)*a1 != v2) )
v3 = g_type_check_instance_is_a(a1, v2);
}
return v3 != 0;
}

original_code:
_Bool
iml_is_variable(void *obj)
{
    return ((__extension__ ({ GTypeInstance *__inst = (GTypeInstance*) ((obj)); GType __t = (iml_variable_get_type()); gboolean __r; if (!__inst) __r = (0); else if (__inst->g_class && __inst->g_class->g_type == __t) __r = (!(0)); else __r = g_type_check_instance_is_a (__inst, __t); __r; })));
}

canonical_original_code:
_Bool iml_is_variable(void *obj)
{
    return ((__extension__ ({ struct _GTypeInstance *__inst = (GTypeInstance*) ((obj)); unsigned long __t = (iml_variable_get_type()); int __r; if (!__inst) __r = (0); else if (__inst->g_class && __inst->g_class->g_type == __t) __r = (!(0)); else __r = g_type_check_instance_is_a (__inst, __t); __r; })));
}


decompiled_code:
bool __fastcall iml_is_constant(_QWORD *a1, __int64 a2)
{
__int64 v2; // rsi
int v3; // eax

v2 = iml_constant_get_type((__int64)a1, a2);
v3 = 0;
if ( a1 )
{
if ( !*a1 || (v3 = 1, *(_QWORD *)*a1 != v2) )
v3 = g_type_check_instance_is_a(a1, v2);
}
return v3 != 0;
}

original_code:
_Bool
iml_is_constant(void *obj)
{
    return ((__extension__ ({ GTypeInstance *__inst = (GTypeInstance*) ((obj)); GType __t = (iml_constant_get_type()); gboolean __r; if (!__inst) __r = (0); else if (__inst->g_class && __inst->g_class->g_type == __t) __r = (!(0)); else __r = g_type_check_instance_is_a (__inst, __t); __r; })));
}

canonical_original_code:
_Bool iml_is_constant(void *obj)
{
    return ((__extension__ ({ struct _GTypeInstance *__inst = (GTypeInstance*) ((obj)); unsigned long __t = (iml_constant_get_type()); int __r; if (!__inst) __r = (0); else if (__inst->g_class && __inst->g_class->g_type == __t) __r = (!(0)); else __r = g_type_check_instance_is_a (__inst, __t); __r; })));
}


decompiled_code:
__int64 __fastcall left_strip_index(char *a1)
{
char v1; // dl
__int64 v2; // rax
unsigned int v3; // ecx

v1 = *a1;
if ( !*a1 )
return (unsigned int)-1;
v2 = 0LL;
while ( 1 )
{
v3 = v2;
if ( v1 != 32 && v1 != 9 )
break;
v1 = a1[++v2];
if ( !v1 )
return (unsigned int)-1;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1)
{
char v1; // dl
__int64 v2; // rax
unsigned int v3; // ecx

v1 = *a1;
if ( !*a1 )
return (unsigned int)-1;
v2 = 0LL;
while ( 1 )
{
v3 = v2;
if ( v1 != 32 && v1 != 9 )
break;
v1 = a1[++v2];
if ( !v1 )
return (unsigned int)-1;
}
return v3;
}

original_code:
static int left_strip_index(char const *s) {
 int i;
 for (i = 0; s[i]; ++i)
  if (!((s[i]) == ' ' || (s[i]) == '\t'))
   return i;
 return -1;
}

canonical_original_code:
static int left_strip_index(char *s) {
 int i;
 for (i = 0; s[i]; ++i)
  if (!((s[i]) == ' ' || (s[i]) == '\t'))
   return i;
 return -1;
}

variable_types:
s
char *

variable_types: i int

return_type: int


decompiled_code:
char *__fastcall hp_get_tweakable_var(char *s2, int a2)
{
char *i; // rbx

for ( i = hp_tweakable_vars; i; i = (char *)*((_QWORD *)i + 34) )
{
if ( !strcmp(i, s2) && *((_DWORD *)i + 64) == a2 )
break;
}
return i;
}

canonical_decompiled_code:
char *__fastcall func2(char *s2, int a2)
{
char *i; // rbx

for ( i = hp_tweakable_vars; i; i = (char *)*((_QWORD *)i + 34) )
{
if ( !strcmp(i, s2) && *((_DWORD *)i + 64) == a2 )
break;
}
return i;
}

original_code:
static hp_vars_list_t *hp_get_tweakable_var(char const *path, hp_id_t id) {
 ;
 hp_vars_list_t *o;
 for (o = hp_tweakable_vars; o; o = o->next) {
  ;
  if (!strcmp(o->path, path)
   && o->id == id)
   return o;
 }
 ;
 return ((void *)0);
}

canonical_original_code:
static struct hp_vars_list_s *hp_get_tweakable_var(char *path, unsigned int id) {
 ;
 struct hp_vars_list_s *o;
 for (o = hp_tweakable_vars; o; o = o->next) {
  ;
  if (!strcmp(o->path, path)
   && o->id == id)
   return o;
 }
 ;
 return ((void *)0);
}

variable_types:
path
char *

variable_types:
id
unsigned int

variable_types: struct hp_vars_list_s { char path[256]; unsigned int id; struct hp_value_s { enum eType { TYPE_UNKNOWN=-1, TYPE_INT=0, TYPE_FLOAT=1 } type; union { int integer; float floating_integer; } value; } value; struct hp_vars_list_s *next; } *

user_defined_types: struct hp_vars_list_s { char path[256]; unsigned int id; struct hp_value_s value; struct hp_vars_list_s *next; }

user_defined_types: struct hp_value_s { enum eType type; union { int integer; float floating_integer; } value; }


decompiled_code:
char *__fastcall hp_get_tweakable_file(char *s2)
{
char *i; // rbx

for ( i = hp_tweakable_files; i; i = (char *)*((_QWORD *)i + 33) )
{
if ( !strcmp(i, s2) )
break;
}
return i;
}

canonical_decompiled_code:
char *__fastcall func3(char *s2)
{
char *i; // rbx

for ( i = hp_tweakable_files; i; i = (char *)*((_QWORD *)i + 33) )
{
if ( !strcmp(i, s2) )
break;
}
return i;
}

original_code:
static hp_files_list_t *hp_get_tweakable_file(char const *path) {
 hp_files_list_t *o;
 for (o = hp_tweakable_files; o; o = o->next)
  if (!strcmp(o->path, path))
   return o;
 return ((void *)0);
}

canonical_original_code:
static struct hp_files_list_s *hp_get_tweakable_file(char *path) {
 struct hp_files_list_s *o;
 for (o = hp_tweakable_files; o; o = o->next)
  if (!strcmp(o->path, path))
   return o;
 return ((void *)0);
}

variable_types: struct hp_files_list_s { char path[256]; unsigned int mod_timestamp; struct hp_files_list_s *next; } *


decompiled_code:
__int64 __fastcall get_modification_time(const char *a1)
{
bool v1; // sf
__int64 result; // rax
struct stat v3; // [rsp+0h] [rbp-98h] BYREF

v1 = stat(a1, &v3) < 0;
result = 0LL;
if ( !v1 )
result = LODWORD(v3.st_mtim.tv_sec);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1)
{
bool v1; // sf
__int64 result; // rax
struct stat v3; // [rsp+0h] [rbp-98h] BYREF

v1 = stat(a1, &v3) < 0;
result = 0LL;
if ( !v1 )
result = LODWORD(v3.st_mtim.tv_sec);
return result;
}

original_code:
static unsigned int get_modification_time(char const *path) {
 struct stat st;
 if (stat(path, &st) < 0)
  return 0;
 return st.st_mtim.tv_sec;
}

canonical_original_code:
static unsigned int get_modification_time(char *path) {
 struct stat st;
 if (stat(path, &st) < 0)
  return 0;
 return st.st_mtim.tv_sec;
}

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

return_type: unsigned int

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


decompiled_code:
char *__fastcall hp_reg_file(char *src)
{
char *v1; // rbx
char *v3; // rax

v1 = hp_get_tweakable_file(src);
if ( !v1 )
{
v3 = (char *)malloc(0x110uLL);
v1 = v3;
if ( v3 )
{
strncpy(v3, src, 0x100uLL);
*((_DWORD *)v1 + 64) = get_modification_time(src);
*((_QWORD *)v1 + 33) = hp_tweakable_files;
hp_tweakable_files = v1;
}
}
return v1;
}

canonical_decompiled_code:
char *__fastcall func5(char *src)
{
char *v1; // rbx
char *v3; // rax

v1 = func3(src);
if ( !v1 )
{
v3 = (char *)malloc(0x110uLL);
v1 = v3;
if ( v3 )
{
strncpy(v3, src, 0x100uLL);
*((_DWORD *)v1 + 64) = func4(src);
*((_QWORD *)v1 + 33) = hp_tweakable_files;
hp_tweakable_files = v1;
}
}
return v1;
}

original_code:
static hp_files_list_t *hp_reg_file(char const *path) {
 hp_files_list_t *new = hp_get_tweakable_file(path);
 if (new)
  return new;
 if (!(new = malloc(sizeof(hp_files_list_t))))
  return ((void *)0);
 strncpy(new->path, path, 256);
 new->mod_timestamp = get_modification_time(path);
 new->next = hp_tweakable_files;
 hp_tweakable_files = new;
 ;
 return new;
}

canonical_original_code:
static struct hp_files_list_s *hp_reg_file(char *path) {
 struct hp_files_list_s *new = hp_get_tweakable_file(path);
 if (new)
  return new;
 if (!(new = malloc(sizeof(hp_files_list_t))))
  return ((void *)0);
 strncpy(new->path, path, 256);
 new->mod_timestamp = get_modification_time(path);
 new->next = hp_tweakable_files;
 hp_tweakable_files = new;
 ;
 return new;
}

return_type: struct hp_files_list_s { char path[256]; unsigned int mod_timestamp; struct hp_files_list_s *next; } *


decompiled_code:
char *__fastcall hp_reg_val(char *src, int a2, int a3, _DWORD *a4)
{
char *v6; // rax
char *v7; // rbx

v6 = (char *)malloc(0x118uLL);
v7 = v6;
if ( v6 )
{
strncpy(v6, src, 0x100uLL);
*((_DWORD *)v7 + 64) = a2;
*((_DWORD *)v7 + 65) = a3;
if ( a3 )
{
if ( a3 != 1 )
{
free(v7);
return 0LL;
}
*((_DWORD *)v7 + 66) = *a4;
}
else
{
*((_DWORD *)v7 + 66) = *a4;
}
*((_QWORD *)v7 + 34) = hp_tweakable_vars;
hp_tweakable_vars = v7;
}
return v7;
}

canonical_decompiled_code:
char *__fastcall func6(char *src, int a2, int a3, _DWORD *a4)
{
char *v6; // rax
char *v7; // rbx

v6 = (char *)malloc(0x118uLL);
v7 = v6;
if ( v6 )
{
strncpy(v6, src, 0x100uLL);
*((_DWORD *)v7 + 64) = a2;
*((_DWORD *)v7 + 65) = a3;
if ( a3 )
{
if ( a3 != 1 )
{
free(v7);
return 0LL;
}
*((_DWORD *)v7 + 66) = *a4;
}
else
{
*((_DWORD *)v7 + 66) = *a4;
}
*((_QWORD *)v7 + 34) = hp_tweakable_vars;
hp_tweakable_vars = v7;
}
return v7;
}

original_code:
static hp_vars_list_t *hp_reg_val(char const *path, hp_id_t id, eType type, void *value) {
 hp_vars_list_t *new;
 if (!(new = malloc(sizeof(hp_vars_list_t))))
  return ((void *)0);
 strncpy(new->path, path, 256);
 new->id = id;
 new->value.type = type;
 if (type == TYPE_INT)
  memcpy(&new->value.value.integer, value, sizeof(int));
 else if (type == TYPE_FLOAT)
  memcpy(&new->value.value.floating_integer, value, sizeof(float));
 else {
  ;
  free(new);
  return ((void *)0);
 }
 ;
 new->next = hp_tweakable_vars;
 hp_tweakable_vars = new;
 return new;
}

canonical_original_code:
static struct hp_vars_list_s *hp_reg_val(char *path, unsigned int id, enum eType type, void *value) {
 struct hp_vars_list_s *new;
 if (!(new = malloc(sizeof(hp_vars_list_t))))
  return ((void *)0);
 strncpy(new->path, path, 256);
 new->id = id;
 new->value.type = type;
 if (type == TYPE_INT)
  memcpy(&new->value.value.integer, value, sizeof(int));
 else if (type == TYPE_FLOAT)
  memcpy(&new->value.value.floating_integer, value, sizeof(float));
 else {
  ;
  free(new);
  return ((void *)0);
 }
 ;
 new->next = hp_tweakable_vars;
 hp_tweakable_vars = new;
 return new;
}

variable_types:
value
void *

return_type: struct hp_vars_list_s { char path[256]; unsigned int id; struct hp_value_s { enum eType { TYPE_UNKNOWN=-1, TYPE_INT=0, TYPE_FLOAT=1 } type; union { int integer; float floating_integer; } value; } value; struct hp_vars_list_s *next; } *


decompiled_code:
__int64 __fastcall hp_value_from_str(unsigned int a1, const char *a2)
{
__int64 v2; // rax
__m128i v4; // xmm1

if ( a1 )
{
v2 = 0LL;
if ( a1 == 1 )
{
v4 = 0LL;
*(float *)v4.m128i_i32 = atof(a2);
v2 = (unsigned int)_mm_cvtsi128_si32(v4);
}
}
else
{
LODWORD(v2) = atoi(a2);
}
return (v2 << 32) | a1;
}

canonical_decompiled_code:
__int64 __fastcall func8(unsigned int a1, const char *a2)
{
__int64 v2; // rax
__m128i v4; // xmm1

if ( a1 )
{
v2 = 0LL;
if ( a1 == 1 )
{
v4 = 0LL;
*(float *)v4.m128i_i32 = atof(a2);
v2 = (unsigned int)_mm_cvtsi128_si32(v4);
}
}
else
{
LODWORD(v2) = atoi(a2);
}
return (v2 << 32) | a1;
}

original_code:
static hp_value_t hp_value_from_str(eType type, char *s) {
 hp_value_t value;
 value.type = type;
 if (type == TYPE_INT)
  value.value.integer = atoi(s);
 else if (type == TYPE_FLOAT)
  value.value.floating_integer = (float)atof(s);
 else
  value.value.integer = 0;
 return value;
}

canonical_original_code:
static struct hp_value_s hp_value_from_str(enum eType type, char *s) {
 struct hp_value_s value;
 value.type = type;
 if (type == TYPE_INT)
  value.value.integer = atoi(s);
 else if (type == TYPE_FLOAT)
  value.value.floating_integer = (float)atof(s);
 else
  value.value.integer = 0;
 return value;
}

variable_types: struct hp_value_s { enum eType { TYPE_UNKNOWN=-1, TYPE_INT=0, TYPE_FLOAT=1 } type; union { int integer; float floating_integer; } value; }


decompiled_code:
char *__fastcall hp_get_tweakable_int(char *src, int a2, int a3)
{
char *v3; // rbp
char *result; // rax
int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v5[0] = a3;
v3 = hp_get_tweakable_var(src, a2);
result = hp_reg_file(src);
if ( result )
{
if ( v3 )
return v3 + 264;
result = hp_reg_val(src, a2, 0, v5);
if ( result )
{
v3 = result;
return v3 + 264;
}
}
return result;
}

canonical_decompiled_code:
char *__fastcall func10(char *src, int a2, int a3)
{
char *v3; // rbp
char *result; // rax
int v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v5[0] = a3;
v3 = func2(src, a2);
result = func5(src);
if ( result )
{
if ( v3 )
return v3 + 264;
result = func6(src, a2, 0, v5);
if ( result )
{
v3 = result;
return v3 + 264;
}
}
return result;
}

original_code:
int *hp_get_tweakable_int(char const *path, hp_id_t id, int value) {
 hp_vars_list_t *var = hp_get_tweakable_var(path, id);
 if (!hp_reg_file(path)) {
  ;
  return ((void *)0);
 }
 if (!var) {
  ;
  var = hp_reg_val(path, id, TYPE_INT, &value);
 }
 return (var ? &var->value.value.integer : ((void *)0));
}

canonical_original_code:
int *hp_get_tweakable_int(char *path, unsigned int id, int value) {
 struct hp_vars_list_s *var = hp_get_tweakable_var(path, id);
 if (!hp_reg_file(path)) {
  ;
  return ((void *)0);
 }
 if (!var) {
  ;
  var = hp_reg_val(path, id, TYPE_INT, &value);
 }
 return (var ? &var->value.value.integer : ((void *)0));
}

variable_types: value int

return_type: int *

user_defined_types: enum eType { TYPE_UNKNOWN=-1, TYPE_INT=0, TYPE_FLOAT=1 }


decompiled_code:
char *__fastcall hp_get_tweakable_float(char *src, int a2, float a3)
{
char *v3; // rbp
char *result; // rax
float v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v5[0] = a3;
v3 = hp_get_tweakable_var(src, a2);
result = hp_reg_file(src);
if ( result )
{
if ( v3 )
return v3 + 264;
result = hp_reg_val(src, a2, 1, v5);
if ( result )
{
v3 = result;
return v3 + 264;
}
}
return result;
}

canonical_decompiled_code:
char *__fastcall func11(char *src, int a2, float a3)
{
char *v3; // rbp
char *result; // rax
float v5[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v5[0] = a3;
v3 = func2(src, a2);
result = func5(src);
if ( result )
{
if ( v3 )
return v3 + 264;
result = func6(src, a2, 1, v5);
if ( result )
{
v3 = result;
return v3 + 264;
}
}
return result;
}

original_code:
float *hp_get_tweakable_float(char const *path, hp_id_t id, float value) {
 hp_vars_list_t *var = hp_get_tweakable_var(path, id);
 if (!hp_reg_file(path)) {
  ;
  return ((void *)0);
 }
 if (!var) {
  ;
  var = hp_reg_val(path, id, TYPE_FLOAT, &value);
 }
 return (var ? &var->value.value.floating_integer : ((void *)0));
}

canonical_original_code:
float *hp_get_tweakable_float(char *path, unsigned int id, float value) {
 struct hp_vars_list_s *var = hp_get_tweakable_var(path, id);
 if (!hp_reg_file(path)) {
  ;
  return ((void *)0);
 }
 if (!var) {
  ;
  var = hp_reg_val(path, id, TYPE_FLOAT, &value);
 }
 return (var ? &var->value.value.floating_integer : ((void *)0));
}

variable_types: value float

return_type: float *


decompiled_code:
__int64 hp_refresh_tweakable_vars()
{
char *v0; // rbx
unsigned int v1; // ebp

v0 = hp_tweakable_files;
if ( !hp_tweakable_files )
return 0;
v1 = 0;
do
{
v1 -= ((unsigned int)hp_refresh_file(v0) == 0) - 1;
v0 = (char *)*((_QWORD *)v0 + 33);
}
while ( v0 );
return v1;
}

canonical_decompiled_code:
__int64 func12()
{
char *v0; // rbx
unsigned int v1; // ebp

v0 = hp_tweakable_files;
if ( !hp_tweakable_files )
return 0;
v1 = 0;
do
{
v1 -= ((unsigned int)func9(v0) == 0) - 1;
v0 = (char *)*((_QWORD *)v0 + 33);
}
while ( v0 );
return v1;
}

original_code:
unsigned int hp_refresh_tweakable_vars() {
 hp_files_list_t *file;
 unsigned int nb_errors = 0;
 for (file = hp_tweakable_files; file; file = file->next)
  if (hp_refresh_file(file))
   ++nb_errors;
 return nb_errors;
}

canonical_original_code:
unsigned int hp_refresh_tweakable_vars() {
 struct hp_files_list_s *file;
 unsigned int nb_errors = 0;
 for (file = hp_tweakable_files; file; file = file->next)
  if (hp_refresh_file(file))
   ++nb_errors;
 return nb_errors;
}

user_defined_types: struct hp_files_list_s { char path[256]; unsigned int mod_timestamp; struct hp_files_list_s *next; }


decompiled_code:
__int64 __fastcall hp_refresh_tweakable_file(char *a1)
{
char *v1; // rdi
__int64 result; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
v1 = hp_get_tweakable_file(a1);
result = 1LL;
if ( v1 )
result = hp_refresh_file(v1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(char *a1)
{
char *v1; // rdi
__int64 result; // rax

if ( !a1 )
return 0xFFFFFFFFLL;
v1 = func3(a1);
result = 1LL;
if ( v1 )
result = func9(v1);
return result;
}

original_code:
int hp_refresh_tweakable_file(char const *path) {
 hp_files_list_t *file;
 if (!path)
  return -1;
 if (!(file = hp_get_tweakable_file(path)))
  return 1;
 return hp_refresh_file(file);
}

canonical_original_code:
int hp_refresh_tweakable_file(char *path) {
 struct hp_files_list_s *file;
 if (!path)
  return -1;
 if (!(file = hp_get_tweakable_file(path)))
  return 1;
 return hp_refresh_file(file);
}


repo: leo-bogert/accuraterip-checksum

decompiled_code:
bool __fastcall check_fileformat(_DWORD *a1)
{
bool result; // al

result = 0;
if ( a1[3] == 2 && (a1[4] & 0x10002) == 65538 )
result = a1[2] == 44100;
return result;
}

canonical_decompiled_code:
bool __fastcall func7(_DWORD *a1)
{
bool result; // al

result = 0;
if ( a1[3] == 2 && (a1[4] & 0x10002) == 65538 )
result = a1[2] == 44100;
return result;
}

original_code:
_Bool check_fileformat(const SF_INFO* sfinfo) {
 if(sfinfo->channels != 2) return 0;
 if((sfinfo->format & SF_FORMAT_TYPEMASK & SF_FORMAT_WAV) != SF_FORMAT_WAV) return 0;
 if((sfinfo->format & SF_FORMAT_SUBMASK & SF_FORMAT_PCM_16) != SF_FORMAT_PCM_16) return 0;
 if(sfinfo->samplerate != 44100) return 0;
 return 1;
}

canonical_original_code:
_Bool check_fileformat(struct SF_INFO *sfinfo) {
 if(sfinfo->channels != 2) return 0;
 if((sfinfo->format & SF_FORMAT_TYPEMASK & SF_FORMAT_WAV) != SF_FORMAT_WAV) return 0;
 if((sfinfo->format & SF_FORMAT_SUBMASK & SF_FORMAT_PCM_16) != SF_FORMAT_PCM_16) return 0;
 if(sfinfo->samplerate != 44100) return 0;
 return 1;
}

variable_types: struct SF_INFO { long frames; int samplerate; int channels; int format; int sections; int seekable; } *


decompiled_code:
__int64 __fastcall get_full_audiodata_size(__int64 a1)
{
return 2 * *(_QWORD *)a1 * *(int *)(a1 + 12);
}

original_code:
size_t get_full_audiodata_size(const SF_INFO* sfinfo) {
 return sfinfo->frames * sfinfo->channels * (16 / 8);
}

canonical_original_code:
unsigned long get_full_audiodata_size(struct SF_INFO *sfinfo) {
 return sfinfo->frames * sfinfo->channels * (16 / 8);
}

user_defined_types: struct SF_INFO { long frames; int samplerate; int channels; int format; int sections; int seekable; }


decompiled_code:
__int64 __fastcall compute_v1_checksum(__int64 a1, int a2, int a3, int a4)
{
unsigned __int64 v6; // rsi
unsigned int v7; // ecx
unsigned int v8; // er8
__int64 v9; // rsi
__int64 v10; // rax
unsigned int v11; // edi

v6 = (unsigned __int64)a2 >> 2;
v7 = 0;
if ( a3 == 1 )
v7 = 2940;
v8 = v6 - 2940;
if ( a3 != a4 )
v8 = v6;
if ( (int)v6 <= 0 )
return 0;
v9 = (unsigned int)(v6 - 1) + 2LL;
v10 = 1LL;
v11 = 0;
do
{
if ( v7 <= (unsigned int)v10 && v8 >= (unsigned int)v10 )
v11 += *(_DWORD *)(a1 + 4 * v10 - 4) * v10;
++v10;
}
while ( v10 != v9 );
return v11;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, int a2, int a3, int a4)
{
unsigned __int64 v6; // rsi
unsigned int v7; // ecx
unsigned int v8; // er8
__int64 v9; // rsi
__int64 v10; // rax
unsigned int v11; // edi

v6 = (unsigned __int64)a2 >> 2;
v7 = 0;
if ( a3 == 1 )
v7 = 2940;
v8 = v6 - 2940;
if ( a3 != a4 )
v8 = v6;
if ( (int)v6 <= 0 )
return 0;
v9 = (unsigned int)(v6 - 1) + 2LL;
v10 = 1LL;
v11 = 0;
do
{
if ( v7 <= (unsigned int)v10 && v8 >= (unsigned int)v10 )
v11 += *(_DWORD *)(a1 + 4 * v10 - 4) * v10;
++v10;
}
while ( v10 != v9 );
return v11;
}

original_code:
uint32_t compute_v1_checksum(const uint32_t* audio_data, const size_t audio_data_size, const int track_number, const int total_tracks) {
 const uint32_t *pAudioData = audio_data;
 int DataSize = audio_data_size;
 int TrackNumber = track_number;
 int AudioTrackCount = total_tracks;
 uint32_t AR_CRC = 0, AR_CRCPosMulti = 1;
 uint32_t AR_CRCPosCheckFrom = 0;
 uint32_t AR_CRCPosCheckTo = DataSize / sizeof(uint32_t);
 if (TrackNumber == 1)
  AR_CRCPosCheckFrom+= ((2352 * 5) / sizeof(uint32_t));
 if (TrackNumber == AudioTrackCount)
  AR_CRCPosCheckTo-=((2352 * 5) / sizeof(uint32_t));
 int DataDWORDSize = DataSize / sizeof(uint32_t);
 for (int i = 0; i < DataDWORDSize; i++)
 {
  if (AR_CRCPosMulti >= AR_CRCPosCheckFrom && AR_CRCPosMulti <= AR_CRCPosCheckTo)
   AR_CRC+=(AR_CRCPosMulti * pAudioData[i]);
  AR_CRCPosMulti++;
 }
 return AR_CRC;
}

canonical_original_code:
unsigned int compute_v1_checksum(unsigned int *audio_data, unsigned long audio_data_size, int track_number, int total_tracks) {
 unsigned int *pAudioData = audio_data;
 int DataSize = audio_data_size;
 int TrackNumber = track_number;
 int AudioTrackCount = total_tracks;
 unsigned int AR_CRC = 0; unsigned int AR_CRCPosMulti = 1;
 unsigned int AR_CRCPosCheckFrom = 0;
 unsigned int AR_CRCPosCheckTo = DataSize / sizeof(uint32_t);
 if (TrackNumber == 1)
  AR_CRCPosCheckFrom+= ((2352 * 5) / sizeof(unsigned int));
 if (TrackNumber == AudioTrackCount)
  AR_CRCPosCheckTo-=((2352 * 5) / sizeof(unsigned int));
 int DataDWORDSize = DataSize / sizeof(uint32_t);
 for (int i = 0; i < DataDWORDSize; i++)
 {
  if (AR_CRCPosMulti >= AR_CRCPosCheckFrom && AR_CRCPosMulti <= AR_CRCPosCheckTo)
   AR_CRC+=(AR_CRCPosMulti * pAudioData[i]);
  AR_CRCPosMulti++;
 }
 return AR_CRC;
}

variable_types:
pAudioData
unsigned int *

variable_types: DataSize int

variable_types: TrackNumber int

variable_types: AudioTrackCount int

variable_types: DataDWORDSize int


decompiled_code:
__int64 __fastcall compute_v2_checksum(__int64 a1, int a2, int a3, int a4)
{
unsigned __int64 v5; // rax
unsigned int v6; // esi
unsigned int v7; // er8
__int64 v8; // rdi
__int64 v9; // rax
unsigned int v10; // ecx
__int64 v11; // rdx

v5 = (unsigned __int64)a2 >> 2;
v6 = 0;
if ( a3 == 1 )
v6 = 2940;
v7 = v5 - 2940;
if ( a3 != a4 )
v7 = v5;
if ( (int)v5 <= 0 )
return 0;
v8 = (unsigned int)(v5 - 1) + 2LL;
v9 = 1LL;
v10 = 0;
do
{
if ( v6 <= (unsigned int)v9 && v7 >= (unsigned int)v9 )
{
v11 = v9 * *(unsigned int *)(a1 + 4 * v9 - 4);
v10 += HIDWORD(v11) + v11;
}
++v9;
}
while ( v9 != v8 );
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, int a2, int a3, int a4)
{
unsigned __int64 v5; // rax
unsigned int v6; // esi
unsigned int v7; // er8
__int64 v8; // rdi
__int64 v9; // rax
unsigned int v10; // ecx
__int64 v11; // rdx

v5 = (unsigned __int64)a2 >> 2;
v6 = 0;
if ( a3 == 1 )
v6 = 2940;
v7 = v5 - 2940;
if ( a3 != a4 )
v7 = v5;
if ( (int)v5 <= 0 )
return 0;
v8 = (unsigned int)(v5 - 1) + 2LL;
v9 = 1LL;
v10 = 0;
do
{
if ( v6 <= (unsigned int)v9 && v7 >= (unsigned int)v9 )
{
v11 = v9 * *(unsigned int *)(a1 + 4 * v9 - 4);
v10 += HIDWORD(v11) + v11;
}
++v9;
}
while ( v9 != v8 );
return v10;
}

original_code:
uint32_t compute_v2_checksum(const uint32_t* audio_data, const size_t audio_data_size, const int track_number, const int total_tracks) {
 const uint32_t *pAudioData = audio_data;
 int DataSize = audio_data_size;
 int TrackNumber = track_number;
 int AudioTrackCount = total_tracks;
 uint32_t AR_CRCPosCheckFrom = 0;
 uint32_t AR_CRCPosCheckTo = DataSize / sizeof(uint32_t);
 if (TrackNumber == 1)
  AR_CRCPosCheckFrom+= ((2352 * 5) / sizeof(uint32_t));
 if (TrackNumber == AudioTrackCount)
  AR_CRCPosCheckTo-=((2352 * 5) / sizeof(uint32_t));
 int DataDWORDSize = DataSize / sizeof(uint32_t);
    uint32_t AC_CRCNEW = 0;
 uint32_t MulBy = 1;
 for (int i = 0; i < DataDWORDSize; i++)
 {
  if (MulBy >= AR_CRCPosCheckFrom && MulBy <= AR_CRCPosCheckTo)
  {
   uint32_t Value = pAudioData[i];
   uint64_t CalcCRCNEW = (uint64_t)Value * (uint64_t)MulBy;
   uint32_t LOCalcCRCNEW = (uint32_t)(CalcCRCNEW & (uint64_t)0xFFFFFFFF);
   uint32_t HICalcCRCNEW = (uint32_t)(CalcCRCNEW / (uint64_t)0x100000000);
   AC_CRCNEW+=HICalcCRCNEW;
   AC_CRCNEW+=LOCalcCRCNEW;
  }
        MulBy++;
 }
 return AC_CRCNEW;
}

canonical_original_code:
unsigned int compute_v2_checksum(unsigned int *audio_data, unsigned long audio_data_size, int track_number, int total_tracks) {
 unsigned int *pAudioData = audio_data;
 int DataSize = audio_data_size;
 int TrackNumber = track_number;
 int AudioTrackCount = total_tracks;
 unsigned int AR_CRCPosCheckFrom = 0;
 unsigned int AR_CRCPosCheckTo = DataSize / sizeof(uint32_t);
 if (TrackNumber == 1)
  AR_CRCPosCheckFrom+= ((2352 * 5) / sizeof(unsigned int));
 if (TrackNumber == AudioTrackCount)
  AR_CRCPosCheckTo-=((2352 * 5) / sizeof(unsigned int));
 int DataDWORDSize = DataSize / sizeof(uint32_t);
    unsigned int AC_CRCNEW = 0;
 unsigned int MulBy = 1;
 for (int i = 0; i < DataDWORDSize; i++)
 {
  if (MulBy >= AR_CRCPosCheckFrom && MulBy <= AR_CRCPosCheckTo)
  {
   unsigned int Value = pAudioData[i];
   unsigned long CalcCRCNEW = (uint64_t)Value * (uint64_t)MulBy;
   unsigned int LOCalcCRCNEW = (uint32_t)(CalcCRCNEW & (uint64_t)0xFFFFFFFF);
   unsigned int HICalcCRCNEW = (uint32_t)(CalcCRCNEW / (uint64_t)0x100000000);
   AC_CRCNEW+=HICalcCRCNEW;
   AC_CRCNEW+=LOCalcCRCNEW;
  }
        MulBy++;
 }
 return AC_CRCNEW;
}

variable_types:
MulBy
unsigned int

variable_types:
Value
unsigned int

variable_types:
CalcCRCNEW
unsigned long

variable_types:
LOCalcCRCNEW
unsigned int

variable_types:
HICalcCRCNEW
unsigned int


decompiled_code:
size_t print_syntax_to_stderr()
{
return fwrite(
"Syntax: accuraterip-checksum [--version / --accuraterip-v1 / --accuraterip-v2 (default)] filename track_numbe"
"r total_tracks\n",
1uLL,
0x7CuLL,
_bss_start);
}

canonical_decompiled_code:
size_t func12()
{
return fwrite(
"Syntax: accuraterip-checksum [--version / --accuraterip-v1 / --accuraterip-v2 (default)] filename track_numbe"
"r total_tracks\n",
1uLL,
0x7CuLL,
_bss_start);
}

original_code:
void print_syntax_to_stderr() {
 fprintf(stderr, "Syntax: accuraterip-checksum [--version / --accuraterip-v1 / --accuraterip-v2 (default)] filename track_number total_tracks\n");
}

return_type: void


repo: leobispo/kermit

decompiled_code:
__int64 __fastcall print_usage(const char *a1)
{
fprintf(
stderr,
"Usage: %s [options]\n"
"Kermit Server Implementation\n"
"Options:\n"
"\t-p --port=<port>\n"
"\t-s --server\n"
"\t-c --client=<server_address>\n"
"\t-d --directory=<directory>\n"
"\t-D --daemon\n",
a1);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
fprintf(
stderr,
"Usage: %s [options]\n"
"Kermit Server Implementation\n"
"Options:\n"
"\t-p --port=<port>\n"
"\t-s --server\n"
"\t-c --client=<server_address>\n"
"\t-d --directory=<directory>\n"
"\t-D --daemon\n",
a1);
return 1LL;
}

original_code:
static int
print_usage(const char * const app_name)
{
  fprintf(stderr, "Usage: %s [options]\n"
    "Kermit Server Implementation\n"
    "Options:\n"
    "\t-p --port=<port>\n"
    "\t-s --server\n"
    "\t-c --client=<server_address>\n"
    "\t-d --directory=<directory>\n"
    "\t-D --daemon\n",
    app_name
  );
  return 1;
}

canonical_original_code:
static int print_usage(char *app_name)
{
  fprintf(stderr, "Usage: %s [options]\n"
    "Kermit Server Implementation\n"
    "Options:\n"
    "\t-p --port=<port>\n"
    "\t-s --server\n"
    "\t-c --client=<server_address>\n"
    "\t-d --directory=<directory>\n"
    "\t-D --daemon\n",
    app_name
  );
  return 1;
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // r12
char *v4; // r13
char *v5; // r15
unsigned __int16 v6; // r14
int v7; // eax
int v8; // ebx
unsigned __int8 v10; // [rsp+Fh] [rbp-F9h]
struct option longopts; // [rsp+10h] [rbp-F8h] BYREF
const char *v12; // [rsp+30h] [rbp-D8h]
int v13; // [rsp+38h] [rbp-D0h]
__int64 v14; // [rsp+40h] [rbp-C8h]
int v15; // [rsp+48h] [rbp-C0h]
const char *v16; // [rsp+50h] [rbp-B8h]
int v17; // [rsp+58h] [rbp-B0h]
__int64 v18; // [rsp+60h] [rbp-A8h]
int v19; // [rsp+68h] [rbp-A0h]
const char *v20; // [rsp+70h] [rbp-98h]
int v21; // [rsp+78h] [rbp-90h]
__int64 v22; // [rsp+80h] [rbp-88h]
int v23; // [rsp+88h] [rbp-80h]
const char *v24; // [rsp+90h] [rbp-78h]
int v25; // [rsp+98h] [rbp-70h]
__int64 v26; // [rsp+A0h] [rbp-68h]
int v27; // [rsp+A8h] [rbp-60h]
__int64 v28; // [rsp+B0h] [rbp-58h]
int v29; // [rsp+B8h] [rbp-50h]
__int64 v30; // [rsp+C0h] [rbp-48h]
int v31; // [rsp+C8h] [rbp-40h]

longopts.name = "port";
longopts.has_arg = 1;
longopts.flag = 0LL;
longopts.val = 112;
v12 = "directory";
v13 = 1;
v14 = 0LL;
v15 = 100;
v16 = "client";
v17 = 1;
v18 = 0LL;
v19 = 99;
v20 = "daemon";
v21 = 0;
v22 = 0LL;
v23 = 68;
v24 = "server";
v25 = 0;
v26 = 0LL;
v27 = 115;
v28 = 0LL;
v29 = 0;
v30 = 0LL;
v31 = 0;
v3 = 0;
v4 = 0LL;
v10 = 0;
v5 = 0LL;
v6 = 0;
while ( 1 )
{
v7 = getopt_long(argc, (char *const *)argv, "p:d:c:Ds", &longopts, 0LL);
if ( v7 == -1 )
break;
if ( v7 == 100 )
{
v5 = strdup(optarg);
}
else if ( v7 > 100 )
{
if ( v7 == 112 )
{
v6 = atoi(optarg);
}
else
{
if ( v7 != 115 )
return print_usage(*argv);
v3 = 1;
}
}
else if ( v7 == 68 )
{
v10 = 1;
}
else
{
if ( v7 != 99 )
return print_usage(*argv);
v4 = strdup(optarg);
}
}
if ( !v6 )
return print_usage(*argv);
if ( v4 && v3 )
{
LABEL_23:
v8 = print_usage(*argv);
goto LABEL_21;
}
if ( !v4 )
{
if ( v5 && v3 )
{
v8 = start_server(v5, v6, v10);
goto LABEL_21;
}
goto LABEL_23;
}
v8 = start_client(v4, v6);
LABEL_21:
free(v5);
free(v4);
return v8;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char v3; // r12
char *v4; // r13
char *v5; // r15
unsigned __int16 v6; // r14
int v7; // eax
int v8; // ebx
unsigned __int8 v10; // [rsp+Fh] [rbp-F9h]
struct option longopts; // [rsp+10h] [rbp-F8h] BYREF
const char *v12; // [rsp+30h] [rbp-D8h]
int v13; // [rsp+38h] [rbp-D0h]
__int64 v14; // [rsp+40h] [rbp-C8h]
int v15; // [rsp+48h] [rbp-C0h]
const char *v16; // [rsp+50h] [rbp-B8h]
int v17; // [rsp+58h] [rbp-B0h]
__int64 v18; // [rsp+60h] [rbp-A8h]
int v19; // [rsp+68h] [rbp-A0h]
const char *v20; // [rsp+70h] [rbp-98h]
int v21; // [rsp+78h] [rbp-90h]
__int64 v22; // [rsp+80h] [rbp-88h]
int v23; // [rsp+88h] [rbp-80h]
const char *v24; // [rsp+90h] [rbp-78h]
int v25; // [rsp+98h] [rbp-70h]
__int64 v26; // [rsp+A0h] [rbp-68h]
int v27; // [rsp+A8h] [rbp-60h]
__int64 v28; // [rsp+B0h] [rbp-58h]
int v29; // [rsp+B8h] [rbp-50h]
__int64 v30; // [rsp+C0h] [rbp-48h]
int v31; // [rsp+C8h] [rbp-40h]

longopts.name = "port";
longopts.has_arg = 1;
longopts.flag = 0LL;
longopts.val = 112;
v12 = "directory";
v13 = 1;
v14 = 0LL;
v15 = 100;
v16 = "client";
v17 = 1;
v18 = 0LL;
v19 = 99;
v20 = "daemon";
v21 = 0;
v22 = 0LL;
v23 = 68;
v24 = "server";
v25 = 0;
v26 = 0LL;
v27 = 115;
v28 = 0LL;
v29 = 0;
v30 = 0LL;
v31 = 0;
v3 = 0;
v4 = 0LL;
v10 = 0;
v5 = 0LL;
v6 = 0;
while ( 1 )
{
v7 = getopt_long(argc, (char *const *)argv, "p:d:c:Ds", &longopts, 0LL);
if ( v7 == -1 )
break;
if ( v7 == 100 )
{
v5 = strdup(optarg);
}
else if ( v7 > 100 )
{
if ( v7 == 112 )
{
v6 = atoi(optarg);
}
else
{
if ( v7 != 115 )
return func0(*argv);
v3 = 1;
}
}
else if ( v7 == 68 )
{
v10 = 1;
}
else
{
if ( v7 != 99 )
return func0(*argv);
v4 = strdup(optarg);
}
}
if ( !v6 )
return func0(*argv);
if ( v4 && v3 )
{
LABEL_23:
v8 = func0(*argv);
goto LABEL_21;
}
if ( !v4 )
{
if ( v5 && v3 )
{
v8 = start_server(v5, v6, v10);
goto LABEL_21;
}
goto LABEL_23;
}
v8 = start_client(v4, v6);
LABEL_21:
free(v5);
free(v4);
return v8;
}

original_code:
int main(int argc, char **argv)
{
  int ret = 0;
  uint16_t port = 0;
  char *directory = ((void *)0);
  _Bool is_daemon = 0;
  char *server_addr = ((void *)0);
  _Bool is_server = 0;
  struct option opts[] = {
    { "port" , 1, 0, 'p' },
    { "directory", 1, 0, 'd' },
    { "client" , 1, 0, 'c' },
    { "daemon" , 0, 0, 'D' },
    { "server" , 0, 0, 's' },
    { ((void *)0) , 0, 0, 0 }
  };
  int opt;
  while ((opt = getopt_long(argc, argv, "p:d:c:Ds", opts, ((void *)0))) != -1) {
    switch(opt) {
      case 'p':
        port = atoi(optarg);
      break;
      case 'd':
        directory = strdup(optarg);
      break;
      case 'c':
        server_addr = strdup(optarg);
      break;
      case 's':
        is_server = 1;
      break;
      case 'D':
        is_daemon = 1;
      break;
      default:
        return print_usage(argv[0]);
    }
  }
  if (port == 0)
    return print_usage(argv[0]);
  if (server_addr && is_server)
    ret = print_usage(argv[0]);
  else if (server_addr)
    ret = start_client(server_addr, port);
  else if (is_server && directory)
    ret = start_server(directory, port, is_daemon);
  else
    ret = print_usage(argv[0]);
  free(directory);
  free(server_addr);
  return ret;
}

canonical_original_code:
int main(int argc, char **argv)
{
  int ret = 0;
  unsigned short port = 0;
  char *directory = ((void *)0);
  _Bool is_daemon = 0;
  char *server_addr = ((void *)0);
  _Bool is_server = 0;
  struct option opts[] = {
    { "port" , 1, 0, 'p' },
    { "directory", 1, 0, 'd' },
    { "client" , 1, 0, 'c' },
    { "daemon" , 0, 0, 'D' },
    { "server" , 0, 0, 's' },
    { ((void *)0) , 0, 0, 0 }
  };
  int opt;
  while ((opt = getopt_long(argc, argv, "p:d:c:Ds", opts, ((void *)0))) != -1) {
    switch(opt) {
      case 'p':
        port = atoi(optarg);
      break;
      case 'd':
        directory = strdup(optarg);
      break;
      case 'c':
        server_addr = strdup(optarg);
      break;
      case 's':
        is_server = 1;
      break;
      case 'D':
        is_daemon = 1;
      break;
      default:
        return print_usage(argv[0]);
    }
  }
  if (port == 0)
    return print_usage(argv[0]);
  if (server_addr && is_server)
    ret = print_usage(argv[0]);
  else if (server_addr)
    ret = start_client(server_addr, port);
  else if (is_server && directory)
    ret = start_server(directory, port, is_daemon);
  else
    ret = print_usage(argv[0]);
  free(directory);
  free(server_addr);
  return ret;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: ret int

variable_types:
port
unsigned short

variable_types:
directory
char *

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

variable_types: opt int


decompiled_code:
__int64 __fastcall calculate_crc(_BYTE *a1, unsigned int a2)
{
__int64 v2; // r8
__int64 result; // rax
char v4; // cl
unsigned int v5; // esi
bool v6; // sf

if ( !(_BYTE)a2 )
return a2;
v2 = (__int64)&a1[(unsigned __int8)(a2 - 1) + 1];
LODWORD(result) = 0;
do
{
LOBYTE(result) = *a1 ^ result;
v4 = 8;
do
{
v5 = 2 * result;
v6 = (result & 0x80u) != 0LL;
result = (2 * (_DWORD)result) ^ 7u;
if ( !v6 )
result = v5;
--v4;
}
while ( v4 );
++a1;
}
while ( a1 != (_BYTE *)v2 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1, unsigned int a2)
{
__int64 v2; // r8
__int64 result; // rax
char v4; // cl
unsigned int v5; // esi
bool v6; // sf

if ( !(_BYTE)a2 )
return a2;
v2 = (__int64)&a1[(unsigned __int8)(a2 - 1) + 1];
LODWORD(result) = 0;
do
{
LOBYTE(result) = *a1 ^ result;
v4 = 8;
do
{
v5 = 2 * result;
v6 = (result & 0x80u) != 0LL;
result = (2 * (_DWORD)result) ^ 7u;
if ( !v6 )
result = v5;
--v4;
}
while ( v4 );
++a1;
}
while ( a1 != (_BYTE *)v2 );
return result;
}

original_code:
static uint8_t
calculate_crc(const uint8_t *payload, uint8_t size)
{
  uint8_t remainder = 0x00;
  uint8_t byte, bit;
  for (byte = 0; byte < size; ++byte) {
    remainder ^= payload[byte];
    for (bit = 8; bit > 0 ; --bit) {
      if (remainder & 0x80)
        remainder = (remainder << 1) ^ 0x07;
      else
        remainder <<= 1;
      remainder = remainder & 0xFF;
    }
  }
  return remainder;
}

canonical_original_code:
static unsigned char calculate_crc(unsigned char *payload, unsigned char size)
{
  unsigned char remainder = 0x00;
  unsigned char byte; unsigned char bit;
  for (byte = 0; byte < size; ++byte) {
    remainder ^= payload[byte];
    for (bit = 8; bit > 0 ; --bit) {
      if (remainder & 0x80)
        remainder = (remainder << 1) ^ 0x07;
      else
        remainder <<= 1;
      remainder = remainder & 0xFF;
    }
  }
  return remainder;
}

variable_types:
payload
unsigned char *

variable_types:
size
unsigned char

variable_types:
remainder
unsigned char

variable_types:
byte
unsigned char

variable_types:
bit
unsigned char

return_type: unsigned char


decompiled_code:
void __fastcall register_decode_callback(unsigned __int8 a1, __int64 (__fastcall *a2)())
{
cmd_callbacks[a1] = a2;
}

canonical_decompiled_code:
void __fastcall func1(unsigned __int8 a1, __int64 (__fastcall *a2)())
{
cmd_callbacks[a1] = a2;
}

original_code:
void
register_decode_callback(uint8_t cmd, void (*callback)(void *, uint8_t, const uint8_t * const, uint8_t))
{
  _cmd_callbacks[cmd].callback = callback;
}

canonical_original_code:
void register_decode_callback(unsigned char cmd, void (*callback)(void *, unsigned char, unsigned char *, unsigned char))
{
  _cmd_callbacks[cmd].callback = callback;
}

variable_types:
cmd
unsigned char

variable_types: void (*)(void *, unsigned char, unsigned char *, unsigned char)


original_code:
uint8_t
encode_msg(uint8_t *ret, uint8_t size, uint8_t sequence, uint8_t type, const uint8_t * const payload)
{
  struct header *h = (struct header *) ret;
  uint8_t s = 0x1e;
  h->mark = s;
  h->size = size;
  h->sequence = sequence;
  h->type = type;
  (*(uint16_t *) ret) = ntohs(*((uint16_t *) ret));
  memcpy(ret + 2, payload, size);
  ret[2 + size] = calculate_crc(payload, size);
  return size + 3;
}

canonical_original_code:
unsigned char encode_msg(unsigned char *ret, unsigned char size, unsigned char sequence, unsigned char type, unsigned char *payload)
{
  struct header *h = (struct header *) ret;
  unsigned char s = 0x1e;
  h->mark = s;
  h->size = size;
  h->sequence = sequence;
  h->type = type;
  (*(unsigned short *) ret) = ntohs(*((unsigned short *) ret));
  memcpy(ret + 2, payload, size);
  ret[2 + size] = calculate_crc(payload, size);
  return size + 3;
}

variable_types:
ret
unsigned char *

variable_types:
sequence
unsigned char

variable_types:
type
unsigned char

variable_types: struct header { unsigned short type; unsigned short sequence; unsigned short size; unsigned short mark; } *

variable_types:
s
unsigned char


decompiled_code:
__int64 __fastcall decode_msg(__int64 a1, uint16_t *a2)
{
_BYTE *v2; // rbx
uint16_t v3; // ax
__int64 (*v4)(void); // rax
unsigned int v5; // edx
unsigned int v7; // er12

v2 = a2;
v3 = ntohs(*a2);
*a2 = v3;
if ( (HIBYTE(v3) & 0xFC) == 120 )
{
v5 = 1;
if ( (v3 & 0xF) != 7 )
{
v7 = (*a2 >> 6) & 0xF;
a2 = (uint16_t *)v7;
if ( (unsigned __int8)calculate_crc(v2 + 2, v7) == v2[v7 + 2] )
{
cmd_callbacks[*v2 & 0xF]();
return 1;
}
v4 = cmd_error_callback;
v5 = 0;
if ( cmd_error_callback )
goto LABEL_3;
}
}
else
{
v4 = cmd_error_callback;
v5 = 0;
if ( cmd_error_callback )
{
LABEL_3:
((void (__fastcall *)(__int64, uint16_t *, _QWORD))v4)(a1, a2, 0LL);
return 0;
}
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, uint16_t *a2)
{
_BYTE *v2; // rbx
uint16_t v3; // ax
__int64 (*v4)(void); // rax
unsigned int v5; // edx
unsigned int v7; // er12

v2 = a2;
v3 = ntohs(*a2);
*a2 = v3;
if ( (HIBYTE(v3) & 0xFC) == 120 )
{
v5 = 1;
if ( (v3 & 0xF) != 7 )
{
v7 = (*a2 >> 6) & 0xF;
a2 = (uint16_t *)v7;
if ( (unsigned __int8)func0(v2 + 2, v7) == v2[v7 + 2] )
{
cmd_callbacks[*v2 & 0xF]();
return 1;
}
v4 = cmd_error_callback;
v5 = 0;
if ( cmd_error_callback )
goto LABEL_3;
}
}
else
{
v4 = cmd_error_callback;
v5 = 0;
if ( cmd_error_callback )
{
LABEL_3:
((void (__fastcall *)(__int64, uint16_t *, _QWORD))v4)(a1, a2, 0LL);
return 0;
}
}
return v5;
}

original_code:
_Bool
decode_msg(void *ctx, const uint8_t * const msg)
{
  uint8_t crc;
  (*(uint16_t *) msg) = ntohs(*((uint16_t *) msg));
  struct header *h = (struct header *) msg;
  if (h->mark != 0x1e) {
    if (_cmd_error_callback)
      _cmd_error_callback(ctx);
    return 0;
  }
  if (h->type == 0x7)
    return 1;
  crc = msg[2 + h->size];
  const uint8_t * payload = msg + 2;
  uint8_t sequence = h->sequence;
  if (calculate_crc(payload, h->size) == crc) {
    _cmd_callbacks[h->type].callback(ctx, sequence, payload, h->size);
    return 1;
  }
  if (_cmd_error_callback)
    _cmd_error_callback(ctx);
  return 0;
}

canonical_original_code:
_Bool decode_msg(void *ctx, unsigned char *msg)
{
  unsigned char crc;
  (*(unsigned short *) msg) = ntohs(*((unsigned short *) msg));
  struct header *h = (struct header *) msg;
  if (h->mark != 0x1e) {
    if (_cmd_error_callback)
      _cmd_error_callback(ctx);
    return 0;
  }
  if (h->type == 0x7)
    return 1;
  crc = msg[2 + h->size];
  unsigned char *payload = msg + 2;
  unsigned char sequence = h->sequence;
  if (calculate_crc(payload, h->size) == crc) {
    _cmd_callbacks[h->type].callback(ctx, sequence, payload, h->size);
    return 1;
  }
  if (_cmd_error_callback)
    _cmd_error_callback(ctx);
  return 0;
}

variable_types:
ctx
void *

variable_types:
msg
unsigned char *

variable_types:
crc
unsigned char

user_defined_types: struct header { unsigned short type; unsigned short sequence; unsigned short size; unsigned short mark; }


decompiled_code:
__int64 __fastcall decode_msg_size_by_header(uint16_t a1)
{
__int64 result; // rax
uint16_t v2; // [rsp+Ch] [rbp-Ch]

v2 = ntohs(a1);
if ( (HIBYTE(v2) & 0xFC) == 120 )
result = ((v2 >> 6) & 0xF) + 3LL;
else
result = -1LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(uint16_t a1)
{
__int64 result; // rax
uint16_t v2; // [rsp+Ch] [rbp-Ch]

v2 = ntohs(a1);
if ( (HIBYTE(v2) & 0xFC) == 120 )
result = ((v2 >> 6) & 0xF) + 3LL;
else
result = -1LL;
return result;
}

original_code:
ssize_t
decode_msg_size_by_header(uint16_t _header)
{
  _header = ntohs(_header);
  struct header *h = (struct header *) &_header;
  if (h->mark != 0x1e)
    return -1;
  return 3 + h->size;
}

canonical_original_code:
long decode_msg_size_by_header(unsigned short _header)
{
  _header = ntohs(_header);
  struct header *h = (struct header *) &_header;
  if (h->mark != 0x1e)
    return -1;
  return 3 + h->size;
}

return_type: long


repo: leonshao/network

decompiled_code:
unsigned int sigchld_handler()
{
__pid_t v0; // eax
int v1; // edi
char *v2; // rax
int stat_loc[3]; // [rsp+Ch] [rbp-Ch] BYREF

while ( 1 )
{
v0 = waitpid(-1, stat_loc, 0);
if ( v0 <= 0 )
break;
printf("Child process %d is reaped, status: %d\n", (unsigned int)v0, (unsigned int)stat_loc[0]);
}
v1 = *_errno_location();
if ( v1 != 10 )
{
v2 = strerror(v1);
fprintf(stdout, "waitpid error, %s", v2);
exit(0);
}
return sleep(2u);
}

canonical_decompiled_code:
unsigned int func0()
{
__pid_t v0; // eax
int v1; // edi
char *v2; // rax
int stat_loc[3]; // [rsp+Ch] [rbp-Ch] BYREF

while ( 1 )
{
v0 = waitpid(-1, stat_loc, 0);
if ( v0 <= 0 )
break;
printf("Child process %d is reaped, status: %d\n", (unsigned int)v0, (unsigned int)stat_loc[0]);
}
v1 = *_errno_location();
if ( v1 != 10 )
{
v2 = strerror(v1);
fprintf(stdout, "waitpid error, %s", v2);
exit(0);
}
return sleep(2u);
}

original_code:
void sigchld_handler(int sig) {
 pid_t pid;
 int status;
 while ((pid = waitpid(-1, &status, 0)) > 0) {
  printf("Child process %d is reaped, status: %d\n", (int)pid, status);
 }
 if ((*__errno_location ()) != 10) {
  fprintf(stdout, "waitpid error, %s", strerror((*__errno_location ())));
  exit(0);
 }
 sleep(2);
 return;
}

canonical_original_code:
void sigchld_handler(int sig) {
 int pid;
 int status;
 while ((pid = waitpid(-1, &status, 0)) > 0) {
  printf("Child process %d is reaped, status: %d\n", (int)pid, status);
 }
 if ((*__errno_location ()) != 10) {
  fprintf(stdout, "waitpid error, %s", strerror((*__errno_location ())));
  exit(0);
 }
 sleep(2);
 return;
}

variable_types: sig int

variable_types: pid int

variable_types: status int


decompiled_code:
int sigpipe_handler()
{
return puts("write to closed conn");
}

canonical_decompiled_code:
int func1()
{
return puts("write to closed conn");
}

original_code:
void sigpipe_handler(int sig) {
 printf("write to closed conn\n");
}


decompiled_code:
int __fastcall print_header_200(char *a1)
{
strcpy(a1, "HTTP/1.1 200 OK\r\n");
return sprintf(a1, "%sServer: My Web Server\r\n", a1);
}

canonical_decompiled_code:
int __fastcall func2(char *a1)
{
strcpy(a1, "HTTP/1.1 200 OK\r\n");
return sprintf(a1, "%sServer: My Web Server\r\n", a1);
}

original_code:
void print_header_200(char *buf) {
 sprintf(buf, "HTTP/1.1 200 OK\r\n");
 sprintf(buf, "%sServer: My Web Server\r\n", buf);
}

variable_types:
buf
char *


decompiled_code:
__int64 read_req_header()
{
return io_print_buf();
}

canonical_decompiled_code:
__int64 func3()
{
return io_print_buf();
}

original_code:
void read_req_header(io_t *io_buf) {
 io_print_buf(io_buf);
 return;
}

canonical_original_code:
void read_req_header(struct io_t *io_buf) {
 io_print_buf(io_buf);
 return;
}

variable_types: struct io_t { int io_fd; int io_cnt; char *io_bufptr; char io_buf[8192]; } *


decompiled_code:
__int64 __fastcall error_to_client(unsigned int a1, const char *a2, const char *a3, const char *a4, const char *a5)
{
size_t v8; // rax
char v10[8192]; // [rsp+0h] [rbp-4028h] BYREF
char s[8232]; // [rsp+2000h] [rbp-2028h] BYREF

strcpy(s, "<title>Tiny Error</title>");
sprintf(s, "%s\r\n", s);
sprintf(s, "%s%s: %s\r\n", s, a3, a4);
sprintf(s, "%s
%s: %s\r\n", s, a5, a2);
sprintf(s, "%s
The Tiny Web Server\r\n", s);
LODWORD(a5) = strlen(s);
sprintf(v10, "HTTP/1.1 %s %s\r\n", a3, a4);
sprintf(v10, "%sContent-type: text/html\r\n", v10);
sprintf(v10, "%sContent-length: %d\r\n\r\n", v10, (unsigned int)a5);
v8 = strlen(v10);
io_writen(a1, v10, v8);
return io_writen(a1, s, (int)a5);
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1, const char *a2, const char *a3, const char *a4, const char *a5)
{
size_t v8; // rax
char v10[8192]; // [rsp+0h] [rbp-4028h] BYREF
char s[8232]; // [rsp+2000h] [rbp-2028h] BYREF

strcpy(s, "<title>Tiny Error</title>");
sprintf(s, "%s\r\n", s);
sprintf(s, "%s%s: %s\r\n", s, a3, a4);
sprintf(s, "%s
%s: %s\r\n", s, a5, a2);
sprintf(s, "%s
The Tiny Web Server\r\n", s);
LODWORD(a5) = strlen(s);
sprintf(v10, "HTTP/1.1 %s %s\r\n", a3, a4);
sprintf(v10, "%sContent-type: text/html\r\n", v10);
sprintf(v10, "%sContent-length: %d\r\n\r\n", v10, (unsigned int)a5);
v8 = strlen(v10);
io_writen(a1, v10, v8);
return io_writen(a1, s, (int)a5);
}

original_code:
void error_to_client(int connfd, char *cause, char *errnum, char *shortmsg, char *longmsg){
 char body[8192], buf[8192];
 int body_size = 0;
 sprintf(body, "<title>Tiny Error</title>");
 sprintf(body, "%s\r\n", body);
 sprintf(body, "%s%s: %s\r\n", body, errnum, shortmsg);
 sprintf(body, "%s
%s: %s\r\n", body, longmsg, cause);
 sprintf(body, "%s
The Tiny Web Server\r\n", body);
 body_size = (int)strlen(body);
 sprintf(buf, "HTTP/1.1 %s %s\r\n", errnum, shortmsg);
 sprintf(buf, "%sContent-type: text/html\r\n", buf);
 sprintf(buf, "%sContent-length: %d\r\n\r\n", buf, body_size);
 io_writen(connfd, buf, strlen(buf));
 io_writen(connfd, body, body_size);
}

canonical_original_code:
void error_to_client(int connfd, char *cause, char *errnum, char *shortmsg, char *longmsg){
 char body[8192]; char buf[8192];
 int body_size = 0;
 sprintf(body, "<title>Tiny Error</title>");
 sprintf(body, "%s\r\n", body);
 sprintf(body, "%s%s: %s\r\n", body, errnum, shortmsg);
 sprintf(body, "%s
%s: %s\r\n", body, longmsg, cause);
 sprintf(body, "%s
The Tiny Web Server\r\n", body);
 body_size = (int)strlen(body);
 sprintf(buf, "HTTP/1.1 %s %s\r\n", errnum, shortmsg);
 sprintf(buf, "%sContent-type: text/html\r\n", buf);
 sprintf(buf, "%sContent-length: %d\r\n\r\n", buf, body_size);
 io_writen(connfd, buf, strlen(buf));
 io_writen(connfd, body, body_size);
}

variable_types: connfd int

variable_types:
cause
char *

variable_types:
errnum
char *

variable_types:
shortmsg
char *

variable_types:
longmsg
char *

variable_types:
body
char [8192]

variable_types:
buf
char [8192]


decompiled_code:
__int64 __fastcall get_post_content(__int64 a1, __int64 a2)
{
int v2; // er13
char *v3; // rax
char v5[8232]; // [rsp+0h] [rbp-2028h] BYREF

io_readlineb(a1, v5, 0x2000LL);
v2 = 0;
while ( strcmp(v5, "\r\n") )
{
v3 = strchr(v5, 32);
if ( v3 )
v2 = atoi(v3 + 1);
io_readlineb(a1, v5, 0x2000LL);
}
return io_readnb(a1, a2, v2);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
int v2; // er13
char *v3; // rax
char v5[8232]; // [rsp+0h] [rbp-2028h] BYREF

io_readlineb(a1, v5, 0x2000LL);
v2 = 0;
while ( strcmp(v5, "\r\n") )
{
v3 = strchr(v5, 32);
if ( v3 )
v2 = atoi(v3 + 1);
io_readlineb(a1, v5, 0x2000LL);
}
return io_readnb(a1, a2, v2);
}

original_code:
void get_post_content(io_t *io_buf, char *cgi_args) {
 char buf[8192];
 char *ptr;
 int content_len = 0;
 io_readlineb(io_buf, buf, 8192);
 while (strcmp(buf, "\r\n")) {
  ptr = strchr(buf, ' ');
  if (ptr) {
   content_len = atoi(ptr + 1);
  }
  io_readlineb(io_buf, buf, 8192);
 }
 io_readnb(io_buf, cgi_args, content_len);
}

canonical_original_code:
void get_post_content(struct io_t *io_buf, char *cgi_args) {
 char buf[8192];
 char *ptr;
 int content_len = 0;
 io_readlineb(io_buf, buf, 8192);
 while (strcmp(buf, "\r\n")) {
  ptr = strchr(buf, ' ');
  if (ptr) {
   content_len = atoi(ptr + 1);
  }
  io_readlineb(io_buf, buf, 8192);
 }
 io_readnb(io_buf, cgi_args, content_len);
}

variable_types:
ptr
char *

user_defined_types: struct io_t { int io_fd; int io_cnt; char *io_bufptr; char io_buf[8192]; }


decompiled_code:
__int64 __fastcall parse_uri(char *src, __int64 a2, char *a3, char *a4)
{
char *v6; // rdx
__int64 result; // rax
char *v8; // rax
char *v9; // r14
size_t v10; // rdx

v6 = strstr(src, "header");
result = 2LL;
if ( !v6 )
{
if ( strstr(src, "cgi-bin") )
{
v8 = index(src, 63);
v9 = v8;
if ( v8 )
{
strcpy(a4, v8 + 1);
*v9 = 0;
}
else
{
get_post_content(a2, (__int64)a4);
}
*(_WORD *)a3 = 46;
strcat(a3, src);
result = 0LL;
}
else
{
*a4 = 0;
*(_WORD *)a3 = 46;
strcat(a3, src);
v10 = strlen(src);
result = 1LL;
if ( src[v10 - 1] == 47 )
{
strcat(a3, "home.html");
result = 1LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *src, __int64 a2, char *a3, char *a4)
{
char *v6; // rdx
__int64 result; // rax
char *v8; // rax
char *v9; // r14
size_t v10; // rdx

v6 = strstr(src, "header");
result = 2LL;
if ( !v6 )
{
if ( strstr(src, "cgi-bin") )
{
v8 = index(src, 63);
v9 = v8;
if ( v8 )
{
strcpy(a4, v8 + 1);
*v9 = 0;
}
else
{
func5(a2, (__int64)a4);
}
*(_WORD *)a3 = 46;
strcat(a3, src);
result = 0LL;
}
else
{
*a4 = 0;
*(_WORD *)a3 = 46;
strcat(a3, src);
v10 = strlen(src);
result = 1LL;
if ( src[v10 - 1] == 47 )
{
strcat(a3, "home.html");
result = 1LL;
}
}
}
return result;
}

original_code:
int parse_uri(char *uri, io_t *io_buf, char *filename, char *cgi_args) {
 char * ptr;
 if (strstr(uri, "header")) {
  return 2;
 }
 else if (!strstr(uri, "cgi-bin")) {
  strcpy(cgi_args, "");
  strcpy(filename, ".");
  strcat(filename, uri);
  if (uri[strlen(uri)-1] == '/')
   strcat(filename, "home.html");
  return 1;
 }
 else {
  ptr = index(uri, '?');
  if (ptr) {
   strcpy(cgi_args, ptr+1);
   *ptr = '\0';
  }
  else {
   get_post_content(io_buf, cgi_args);
  }
  strcpy(filename, ".");
  strcat(filename, uri);
  return 0;
 }
}

canonical_original_code:
int parse_uri(char *uri, struct io_t *io_buf, char *filename, char *cgi_args) {
 char *ptr;
 if (strstr(uri, "header")) {
  return 2;
 }
 else if (!strstr(uri, "cgi-bin")) {
  strcpy(cgi_args, "");
  strcpy(filename, ".");
  strcat(filename, uri);
  if (uri[strlen(uri)-1] == '/')
   strcat(filename, "home.html");
  return 1;
 }
 else {
  ptr = index(uri, '?');
  if (ptr) {
   strcpy(cgi_args, ptr+1);
   *ptr = '\0';
  }
  else {
   get_post_content(io_buf, cgi_args);
  }
  strcpy(filename, ".");
  strcat(filename, uri);
  return 0;
 }
}

variable_types:
uri
char *

variable_types:
filename
char *


decompiled_code:
__int64 __fastcall get_file_type(char *haystack, char *a2)
{
__int64 result; // rax

if ( strstr(haystack, ".html") )
{
result = 0x6D74682F74786574LL;
strcpy(a2, "text/html");
}
else if ( strstr(haystack, ".jpg") )
{
result = 0x706A2F6567616D69LL;
strcpy(a2, "image/jpeg");
}
else if ( strstr(haystack, ".gif") )
{
result = 0x69672F6567616D69LL;
strcpy(a2, "image/gif");
}
else
{
result = 0x616C702F74786574LL;
strcpy(a2, "text/plain");
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *haystack, char *a2)
{
__int64 result; // rax

if ( strstr(haystack, ".html") )
{
result = 0x6D74682F74786574LL;
strcpy(a2, "text/html");
}
else if ( strstr(haystack, ".jpg") )
{
result = 0x706A2F6567616D69LL;
strcpy(a2, "image/jpeg");
}
else if ( strstr(haystack, ".gif") )
{
result = 0x69672F6567616D69LL;
strcpy(a2, "image/gif");
}
else
{
result = 0x616C702F74786574LL;
strcpy(a2, "text/plain");
}
return result;
}

original_code:
void get_file_type(char *filename, char *filetype) {
 if (strstr(filename, ".html"))
  strcpy(filetype, "text/html");
 else if (strstr(filename, ".jpg"))
  strcpy(filetype, "image/jpeg");
 else if (strstr(filename, ".gif"))
  strcpy(filetype, "image/gif");
 else
  strcpy(filetype, "text/plain");
}

variable_types:
filetype
char *


decompiled_code:
int __fastcall serve_static(unsigned int a1, char *a2, unsigned int a3)
{
size_t v4; // rax
unsigned int v5; // er12
void *v6; // rbp
char v8[8192]; // [rsp+0h] [rbp-4028h] BYREF
char s[8232]; // [rsp+2000h] [rbp-2028h] BYREF

get_file_type(a2, v8);
print_header_200(s);
sprintf(s, "%sContent-type: %s\r\n", s, v8);
sprintf(s, "%sContent-length: %d\r\n\r\n", s, a3);
v4 = strlen(s);
io_writen(a1, s, v4);
v5 = open(a2, 0, 0LL);
v6 = malloc((int)a3);
io_readn(v5, v6, (int)a3);
io_writen(a1, v6, (int)a3);
free(v6);
return close(v5);
}

canonical_decompiled_code:
int __fastcall func8(unsigned int a1, char *a2, unsigned int a3)
{
size_t v4; // rax
unsigned int v5; // er12
void *v6; // rbp
char v8[8192]; // [rsp+0h] [rbp-4028h] BYREF
char s[8232]; // [rsp+2000h] [rbp-2028h] BYREF

func7(a2, v8);
func2(s);
sprintf(s, "%sContent-type: %s\r\n", s, v8);
sprintf(s, "%sContent-length: %d\r\n\r\n", s, a3);
v4 = strlen(s);
io_writen(a1, s, v4);
v5 = open(a2, 0, 0LL);
v6 = malloc((int)a3);
io_readn(v5, v6, (int)a3);
io_writen(a1, v6, (int)a3);
free(v6);
return close(v5);
}

original_code:
void serve_static(int connfd, char *filename, int filesize) {
 char buf[8192], filetype[8192];
 char * srcp;
 int filefd;
 get_file_type(filename, filetype);
 print_header_200(buf);
 sprintf(buf, "%sContent-type: %s\r\n", buf, filetype);
 sprintf(buf, "%sContent-length: %d\r\n\r\n", buf, filesize);
 io_writen(connfd, buf, strlen(buf));
 filefd = open(filename, 00, 0);
 srcp = malloc(filesize);
 io_readn(filefd, srcp, filesize);
 io_writen(connfd, srcp, filesize);
 free(srcp);
 close(filefd);
}

canonical_original_code:
void serve_static(int connfd, char *filename, int filesize) {
 char buf[8192]; char filetype[8192];
 char *srcp;
 int filefd;
 get_file_type(filename, filetype);
 print_header_200(buf);
 sprintf(buf, "%sContent-type: %s\r\n", buf, filetype);
 sprintf(buf, "%sContent-length: %d\r\n\r\n", buf, filesize);
 io_writen(connfd, buf, strlen(buf));
 filefd = open(filename, 00, 0);
 srcp = malloc(filesize);
 io_readn(filefd, srcp, filesize);
 io_writen(connfd, srcp, filesize);
 free(srcp);
 close(filefd);
}

variable_types: filesize int

variable_types:
filetype
char [8192]

variable_types:
srcp
char *

variable_types: filefd int


decompiled_code:
__pid_t __fastcall serve_dynamic(int fd, char *path, char *value)
{
size_t v4; // rax
__pid_t result; // eax
char *argv; // [rsp+8h] [rbp-2020h] BYREF
char s[8216]; // [rsp+10h] [rbp-2018h] BYREF

argv = 0LL;
print_header_200(s);
v4 = strlen(s);
io_writen((unsigned int)fd, s, v4);
result = fork();
if ( !result )
{
setenv("QUERY_STRING", value, 1);
dup2(fd, 1);
result = execve(path, &argv, _environ);
}
return result;
}

canonical_decompiled_code:
__pid_t __fastcall func9(int fd, char *path, char *value)
{
size_t v4; // rax
__pid_t result; // eax
char *argv; // [rsp+8h] [rbp-2020h] BYREF
char s[8216]; // [rsp+10h] [rbp-2018h] BYREF

argv = 0LL;
func2(s);
v4 = strlen(s);
io_writen((unsigned int)fd, s, v4);
result = fork();
if ( !result )
{
setenv("QUERY_STRING", value, 1);
dup2(fd, 1);
result = execve(path, &argv, _environ);
}
return result;
}

original_code:
void serve_dynamic(int connfd, char *filename, char *cgi_args) {
 char buf[8192], *emptylist[] = { ((void *)0) };
 print_header_200(buf);
 io_writen(connfd, buf, strlen(buf));
 if (fork() == 0) {
  setenv("QUERY_STRING", cgi_args, 1);
  dup2(connfd, 1);
  execve(filename, emptylist, __environ);
 }
}

canonical_original_code:
void serve_dynamic(int connfd, char *filename, char *cgi_args) {
 char buf[8192]; char *emptylist[] = { ((void *)0) };
 print_header_200(buf);
 io_writen(connfd, buf, strlen(buf));
 if (fork() == 0) {
  setenv("QUERY_STRING", cgi_args, 1);
  dup2(connfd, 1);
  execve(filename, emptylist, __environ);
 }
}

variable_types:
emptylist
char *[]


decompiled_code:
__int64 __fastcall serve_header(unsigned int a1, __int64 a2)
{
const char *v2; // rbp
char *v3; // rax
char *v4; // rbx
unsigned int v5; // ebx
size_t v6; // rax
size_t v7; // rax
char v9[8192]; // [rsp+0h] [rbp-4018h] BYREF
char s[8216]; // [rsp+2000h] [rbp-2018h] BYREF

v2 = (const char *)(a2 + 16);
v9[0] = 0;
while ( 1 )
{
v3 = strchr(v2, 10);
v4 = v3;
if ( !v3 )
break;
*v3 = 0;
sprintf(v9, "%s<p>%s", v9, v2);
v2 = v4 + 1;
}
v5 = strlen(v9);
print_header_200(s);
sprintf(s, "%sContent-type: text/html\r\n", s);
sprintf(s, "%sContent-length: %d\r\n\r\n", s, v5);
v6 = strlen(s);
io_writen(a1, s, v6);
v7 = strlen(v9);
return io_writen(a1, v9, v7);
}

canonical_decompiled_code:
__int64 __fastcall func10(unsigned int a1, __int64 a2)
{
const char *v2; // rbp
char *v3; // rax
char *v4; // rbx
unsigned int v5; // ebx
size_t v6; // rax
size_t v7; // rax
char v9[8192]; // [rsp+0h] [rbp-4018h] BYREF
char s[8216]; // [rsp+2000h] [rbp-2018h] BYREF

v2 = (const char *)(a2 + 16);
v9[0] = 0;
while ( 1 )
{
v3 = strchr(v2, 10);
v4 = v3;
if ( !v3 )
break;
*v3 = 0;
sprintf(v9, "%s<p>%s", v9, v2);
v2 = v4 + 1;
}
v5 = strlen(v9);
func2(s);
sprintf(s, "%sContent-type: text/html\r\n", s);
sprintf(s, "%sContent-length: %d\r\n\r\n", s, v5);
v6 = strlen(s);
io_writen(a1, s, v6);
v7 = strlen(v9);
return io_writen(a1, v9, v7);
}

original_code:
void serve_header(int connfd, io_t *io_buf) {
 char buf[8192], content[8192];
 char *ptr_end, *ptr_start = io_buf->io_buf;
 int header_size = 0;
 content[0] = 0;
 while((ptr_end = strchr(ptr_start, '\n')) != ((void *)0)) {
  *ptr_end = 0;
  sprintf(content, "%s<p>%s", content, ptr_start);
  ptr_start = ptr_end + 1;
 }
 header_size = strlen(content);
 print_header_200(buf);
 sprintf(buf, "%sContent-type: text/html\r\n", buf);
 sprintf(buf, "%sContent-length: %d\r\n\r\n", buf, header_size);
 io_writen(connfd, buf, strlen(buf));
 io_writen(connfd, content, strlen(content));
}

canonical_original_code:
void serve_header(int connfd, struct io_t *io_buf) {
 char buf[8192]; char content[8192];
 char *ptr_end; char *ptr_start = io_buf->io_buf;
 int header_size = 0;
 content[0] = 0;
 while((ptr_end = strchr(ptr_start, '\n')) != ((void *)0)) {
  *ptr_end = 0;
  sprintf(content, "%s<p>%s", content, ptr_start);
  ptr_start = ptr_end + 1;
 }
 header_size = strlen(content);
 print_header_200(buf);
 sprintf(buf, "%sContent-type: text/html\r\n", buf);
 sprintf(buf, "%sContent-length: %d\r\n\r\n", buf, header_size);
 io_writen(connfd, buf, strlen(buf));
 io_writen(connfd, content, strlen(content));
}

variable_types: struct io_t { int io_fd; int io_cnt; char *io_bufptr; char io_buf[8192]; } *

variable_types:
content
char [8192]


decompiled_code:
int __fastcall check_req(__int64 a1, __int64 a2)
{
int result; // eax

_isoc99_sscanf(a1, "%s %s %s", a2 + 1024, a2, a2 + 1034);
result = strcasecmp((const char *)(a2 + 1024), "POST");
if ( result )
result = -(strcasecmp((const char *)(a2 + 1024), "GET") != 0);
return result;
}

canonical_decompiled_code:
int __fastcall func11(__int64 a1, __int64 a2)
{
int result; // eax

_isoc99_sscanf(a1, "%s %s %s", a2 + 1024, a2, a2 + 1034);
result = strcasecmp((const char *)(a2 + 1024), "POST");
if ( result )
result = -(strcasecmp((const char *)(a2 + 1024), "GET") != 0);
return result;
}

original_code:
int check_req(char * req_line, req_line_t * req_line_ptr) {
 char * method_p = (char *)&req_line_ptr->method;
 char * uri_p = (char *)&req_line_ptr->uri;
 char * version_p = (char *)&req_line_ptr->version;
 sscanf(req_line, "%s %s %s", method_p, uri_p, version_p);
 if (!strcasecmp(method_p, "POST")) {
 }
 else if (strcasecmp(method_p, "GET")) {
  return -1;
 }
 return 0;
}

canonical_original_code:
int check_req(char *req_line, struct req_line_t *req_line_ptr) {
 char *method_p = (char *)&req_line_ptr->method;
 char *uri_p = (char *)&req_line_ptr->uri;
 char *version_p = (char *)&req_line_ptr->version;
 sscanf(req_line, "%s %s %s", method_p, uri_p, version_p);
 if (!strcasecmp(method_p, "POST")) {
 }
 else if (strcasecmp(method_p, "GET")) {
  return -1;
 }
 return 0;
}

variable_types: struct req_line_t { char uri[1024]; char method[10]; char version[10]; } *


decompiled_code:
int __fastcall print_req(const char *a1)
{
return printf("%s %s %s\n", a1 + 1024, a1, a1 + 1034);
}

canonical_decompiled_code:
int __fastcall func12(const char *a1)
{
return printf("%s %s %s\n", a1 + 1024, a1, a1 + 1034);
}

original_code:
void print_req(req_line_t * req_ptr) {
 printf("%s %s %s\n", req_ptr->method, req_ptr->uri, req_ptr->version);
}

canonical_original_code:
void print_req(struct req_line_t *req_ptr) {
 printf("%s %s %s\n", req_ptr->method, req_ptr->uri, req_ptr->version);
}

user_defined_types: struct req_line_t { char uri[1024]; char method[10]; char version[10]; }


decompiled_code:
int __fastcall process_req(int fd)
{
int result; // eax
struct stat v2; // [rsp+0h] [rbp-84C8h] BYREF
char value[8192]; // [rsp+90h] [rbp-8438h] BYREF
char file[8192]; // [rsp+2090h] [rbp-6438h] BYREF
char v5[8192]; // [rsp+4090h] [rbp-4438h] BYREF
char src[1024]; // [rsp+6090h] [rbp-2438h] BYREF
char v7[32]; // [rsp+6490h] [rbp-2038h] BYREF
char v8[8216]; // [rsp+64B0h] [rbp-2018h] BYREF

io_initbuf(v8, (unsigned int)fd);
io_readlineb(v8, v5, 0x2000LL);
if ( check_req((__int64)v5, (__int64)src) )
return error_to_client(fd, v7, "501", "Not Implemented", "Server does not implement this method");
result = parse_uri(src, (__int64)v8, file, value);
switch ( result )
{
case 1:
if ( !stat(file, &v2) )
{
if ( (v2.st_mode & 0xF100) == 33024 )
result = serve_static(fd, file, v2.st_size);
else
result = error_to_client(fd, file, "403", "Forbidden", "File can't read");
return result;
}
return error_to_client(fd, file, "404", "Not Found", "File not found");
case 2:
return serve_header(fd, (__int64)v8);
case 0:
if ( !stat(file, &v2) )
{
if ( (v2.st_mode & 0xF040) == 32832 )
result = serve_dynamic(fd, file, value);
else
result = error_to_client(fd, file, "403", "Forbidden", "CGI program can't run");
return result;
}
return error_to_client(fd, file, "404", "Not Found", "File not found");
}
return result;
}

canonical_decompiled_code:
int __fastcall func13(int fd)
{
int result; // eax
struct stat v2; // [rsp+0h] [rbp-84C8h] BYREF
char value[8192]; // [rsp+90h] [rbp-8438h] BYREF
char file[8192]; // [rsp+2090h] [rbp-6438h] BYREF
char v5[8192]; // [rsp+4090h] [rbp-4438h] BYREF
char src[1024]; // [rsp+6090h] [rbp-2438h] BYREF
char v7[32]; // [rsp+6490h] [rbp-2038h] BYREF
char v8[8216]; // [rsp+64B0h] [rbp-2018h] BYREF

io_initbuf(v8, (unsigned int)fd);
io_readlineb(v8, v5, 0x2000LL);
if ( func11((__int64)v5, (__int64)src) )
return func4(fd, v7, "501", "Not Implemented", "Server does not implement this method");
result = func6(src, (__int64)v8, file, value);
switch ( result )
{
case 1:
if ( !stat(file, &v2) )
{
if ( (v2.st_mode & 0xF100) == 33024 )
result = func8(fd, file, v2.st_size);
else
result = func4(fd, file, "403", "Forbidden", "File can't read");
return result;
}
return func4(fd, file, "404", "Not Found", "File not found");
case 2:
return func10(fd, (__int64)v8);
case 0:
if ( !stat(file, &v2) )
{
if ( (v2.st_mode & 0xF040) == 32832 )
result = func9(fd, file, value);
else
result = func4(fd, file, "403", "Forbidden", "CGI program can't run");
return result;
}
return func4(fd, file, "404", "Not Found", "File not found");
}
return result;
}

original_code:
void process_req(int connfd) {
 int req_type;
 io_t io_buf;
 req_line_t req_line;
 char buf[8192];
 char filename[8192], cgi_args[8192];
 struct stat file_stat;
 io_initbuf(&io_buf, connfd);
 io_readlineb(&io_buf, buf, 8192);
 if (check_req((char *)&buf, &req_line)) {
  error_to_client(connfd, (char *)&(req_line.method), "501", "Not Implemented",
    "Server does not implement this method");
  return;
 }
 req_type = parse_uri((char *)&req_line.uri, &io_buf, filename, cgi_args);
 switch(req_type) {
 case 0:
  if (stat(filename, &file_stat)) {
   error_to_client(connfd, filename, "404", "Not Found",
     "File not found");
   return;
  }
  if (!(((((file_stat.st_mode)) & 0170000) == (0100000))) || !(0100 & file_stat.st_mode)) {
   error_to_client(connfd, filename, "403", "Forbidden",
     "CGI program can't run");
   return;
  }
  serve_dynamic(connfd, filename, cgi_args);
  break;
 case 1:
  if (stat(filename, &file_stat)) {
   error_to_client(connfd, filename, "404", "Not Found",
     "File not found");
   return;
  }
  if (!(((((file_stat.st_mode)) & 0170000) == (0100000))) || !(0400 & file_stat.st_mode)) {
   error_to_client(connfd, filename, "403", "Forbidden",
     "File can't read");
   return;
  }
  serve_static(connfd, filename, file_stat.st_size);
  break;
 case 2:
  serve_header(connfd, &io_buf);
  break;
 }
}

canonical_original_code:
void process_req(int connfd) {
 int req_type;
 struct io_t io_buf;
 struct req_line_t req_line;
 char buf[8192];
 char filename[8192]; char cgi_args[8192];
 struct stat file_stat;
 io_initbuf(&io_buf, connfd);
 io_readlineb(&io_buf, buf, 8192);
 if (check_req((char *)&buf, &req_line)) {
  error_to_client(connfd, (char *)&(req_line.method), "501", "Not Implemented",
    "Server does not implement this method");
  return;
 }
 req_type = parse_uri((char *)&req_line.uri, &io_buf, filename, cgi_args);
 switch(req_type) {
 case 0:
  if (stat(filename, &file_stat)) {
   error_to_client(connfd, filename, "404", "Not Found",
     "File not found");
   return;
  }
  if (!(((((file_stat.st_mode)) & 0170000) == (0100000))) || !(0100 & file_stat.st_mode)) {
   error_to_client(connfd, filename, "403", "Forbidden",
     "CGI program can't run");
   return;
  }
  serve_dynamic(connfd, filename, cgi_args);
  break;
 case 1:
  if (stat(filename, &file_stat)) {
   error_to_client(connfd, filename, "404", "Not Found",
     "File not found");
   return;
  }
  if (!(((((file_stat.st_mode)) & 0170000) == (0100000))) || !(0400 & file_stat.st_mode)) {
   error_to_client(connfd, filename, "403", "Forbidden",
     "File can't read");
   return;
  }
  serve_static(connfd, filename, file_stat.st_size);
  break;
 case 2:
  serve_header(connfd, &io_buf);
  break;
 }
}

variable_types: connfd int

variable_types: struct io_t { int io_fd; int io_cnt; char *io_bufptr; char io_buf[8192]; }

variable_types: struct req_line_t { char uri[1024]; char method[10]; char version[10]; }

variable_types:
filename
char [8192]

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


decompiled_code:
char *__fastcall get_arg_value(const char *a1)
{
char *v1; // rax

v1 = strchr(a1, 61);
return &v1[-(v1 == 0LL) + 1];
}

canonical_decompiled_code:
char *__fastcall func7(const char *a1)
{
char *v1; // rax

v1 = strchr(a1, 61);
return &v1[-(v1 == 0LL) + 1];
}

original_code:
char * get_arg_value(char * arg) {
 char * ptr = ((void *)0);
 if ((ptr = strchr(arg, '=')) != ((void *)0)) {
  return ptr + 1;
 }
 return ((void *)0);
}

canonical_original_code:
char *get_arg_value(char *arg) {
 char *ptr = ((void *)0);
 if ((ptr = strchr(arg, '=')) != ((void *)0)) {
  return ptr + 1;
 }
 return ((void *)0);
}

variable_types:
arg
char *

variable_types:
ptr
char *

return_type: char *


decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
const char *v4; // rbx
char *v5; // rbp
char *v6; // rax
char *v7; // rax
unsigned int v8; // ebx
__int64 v9; // rcx
size_t v10; // rax
char v11[8192]; // [rsp+0h] [rbp-2818h] BYREF
char nptr[1024]; // [rsp+2000h] [rbp-818h] BYREF
char dest[1048]; // [rsp+2400h] [rbp-418h] BYREF

v3 = getenv("QUERY_STRING");
if ( v3 )
{
v4 = v3;
v5 = strchr(v3, 38);
*v5 = 0;
v6 = get_arg_value(v4);
strcpy(dest, v6);
v7 = get_arg_value(v5 + 1);
strcpy(nptr, v7);
v8 = atoi(dest);
v9 = (unsigned int)atoi(nptr);
}
else
{
v9 = 0LL;
v8 = 0;
}
sprintf(v11, "<p>The answer is : %d + %d = %d\r\n", v8, v9, v8 + (unsigned int)v9);
puts("Content-type: text/html\r");
v10 = strlen(v11);
printf("Content-length: %d\r\n\r\n", v10);
printf("%s", v11);
fflush(_bss_start);
exit(0);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
const char *v4; // rbx
char *v5; // rbp
char *v6; // rax
char *v7; // rax
unsigned int v8; // ebx
__int64 v9; // rcx
size_t v10; // rax
char v11[8192]; // [rsp+0h] [rbp-2818h] BYREF
char nptr[1024]; // [rsp+2000h] [rbp-818h] BYREF
char dest[1048]; // [rsp+2400h] [rbp-418h] BYREF

v3 = getenv("QUERY_STRING");
if ( v3 )
{
v4 = v3;
v5 = strchr(v3, 38);
*v5 = 0;
v6 = func7(v4);
strcpy(dest, v6);
v7 = func7(v5 + 1);
strcpy(nptr, v7);
v8 = atoi(dest);
v9 = (unsigned int)atoi(nptr);
}
else
{
v9 = 0LL;
v8 = 0;
}
sprintf(v11, "<p>The answer is : %d + %d = %d\r\n", v8, v9, v8 + (unsigned int)v9);
puts("Content-type: text/html\r");
v10 = strlen(v11);
printf("Content-length: %d\r\n\r\n", v10);
printf("%s", v11);
fflush(_bss_start);
exit(0);
}

original_code:
int main(void) {
 char *buf, *p;
 char arg1[1024], arg2[1024], content[8192];
 int n1=0, n2=0;
 if ((buf = getenv("QUERY_STRING")) != ((void *)0)) {
  p = strchr(buf, '&');
  *p = '\0';
  strcpy(arg1, get_arg_value(buf));
  strcpy(arg2, get_arg_value(p+1));
  n1 = atoi(arg1);
  n2 = atoi(arg2);
 }
 sprintf(content, "<p>The answer is : %d + %d = %d\r\n", n1, n2, n1 + n2);
 printf("Content-type: text/html\r\n");
 printf("Content-length: %d\r\n\r\n", (int)strlen(content));
 printf("%s", content);
 fflush(stdout);
 exit(0);
}

canonical_original_code:
int main() {
 char *buf; char *p;
 char arg1[1024]; char arg2[1024]; char content[8192];
 int n1 = 0; int n2 = 0;
 if ((buf = getenv("QUERY_STRING")) != ((void *)0)) {
  p = strchr(buf, '&');
  *p = '\0';
  strcpy(arg1, get_arg_value(buf));
  strcpy(arg2, get_arg_value(p+1));
  n1 = atoi(arg1);
  n2 = atoi(arg2);
 }
 sprintf(content, "<p>The answer is : %d + %d = %d\r\n", n1, n2, n1 + n2);
 printf("Content-type: text/html\r\n");
 printf("Content-length: %d\r\n\r\n", (int)strlen(content));
 printf("%s", content);
 fflush(stdout);
 exit(0);
}

variable_types:
buf
char *

variable_types:
p
char *


decompiled_code:
__int64 __fastcall io_read(__int64 a1, void *a2, unsigned __int64 a3)
{
int v4; // er14
void *v5; // rbp
int v6; // eax
__int64 result; // rax

v4 = *(_DWORD *)(a1 + 4);
if ( v4 > 0 )
{
LABEL_7:
if ( v4 >= a3 )
v4 = a3;
memcpy(a2, *(const void **)(a1 + 8), v4);
*(_QWORD *)(a1 + 8) += v4;
*(_DWORD *)(a1 + 4) -= v4;
result = v4;
}
else
{
v5 = (void *)(a1 + 16);
while ( 1 )
{
v6 = read(*(_DWORD *)a1, v5, 0x2000uLL);
v4 = v6;
*(_DWORD *)(a1 + 4) = v6;
if ( v6 >= 0 )
break;
if ( *_errno_location() != 4 )
return -1LL;
}
if ( v6 )
{
*(_QWORD *)(a1 + 8) = v5;
goto LABEL_7;
}
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, void *a2, unsigned __int64 a3)
{
int v4; // er14
void *v5; // rbp
int v6; // eax
__int64 result; // rax

v4 = *(_DWORD *)(a1 + 4);
if ( v4 > 0 )
{
LABEL_7:
if ( v4 >= a3 )
v4 = a3;
memcpy(a2, *(const void **)(a1 + 8), v4);
*(_QWORD *)(a1 + 8) += v4;
*(_DWORD *)(a1 + 4) -= v4;
result = v4;
}
else
{
v5 = (void *)(a1 + 16);
while ( 1 )
{
v6 = read(*(_DWORD *)a1, v5, 0x2000uLL);
v4 = v6;
*(_DWORD *)(a1 + 4) = v6;
if ( v6 >= 0 )
break;
if ( *_errno_location() != 4 )
return -1LL;
}
if ( v6 )
{
*(_QWORD *)(a1 + 8) = v5;
goto LABEL_7;
}
result = 0LL;
}
return result;
}

original_code:
static ssize_t io_read(io_t *rp, char *usrbuf, size_t n) {
 int cnt;
 while (rp->io_cnt <= 0) {
  rp->io_cnt = read(rp->io_fd, rp->io_buf, sizeof(rp->io_buf));
  if (rp->io_cnt < 0) {
   if ((*__errno_location ()) != 4)
    return -1;
  }
  else if (rp->io_cnt == 0)
   return 0;
  else
   rp->io_bufptr = rp->io_buf;
 }
 cnt = n;
 if (rp->io_cnt < n)
  cnt = rp->io_cnt;
 memcpy(usrbuf, rp->io_bufptr, cnt);
 rp->io_bufptr += cnt;
 rp->io_cnt -= cnt;
 return cnt;
}

canonical_original_code:
static long io_read(struct io_t *rp, char *usrbuf, unsigned long n) {
 int cnt;
 while (rp->io_cnt <= 0) {
  rp->io_cnt = read(rp->io_fd, rp->io_buf, sizeof(rp->io_buf));
  if (rp->io_cnt < 0) {
   if ((*__errno_location ()) != 4)
    return -1;
  }
  else if (rp->io_cnt == 0)
   return 0;
  else
   rp->io_bufptr = rp->io_buf;
 }
 cnt = n;
 if (rp->io_cnt < n)
  cnt = rp->io_cnt;
 memcpy(usrbuf, rp->io_bufptr, cnt);
 rp->io_bufptr += cnt;
 rp->io_cnt -= cnt;
 return cnt;
}

variable_types:
usrbuf
char *

variable_types:
n
unsigned long

variable_types: cnt int


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

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

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

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

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

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

variable_types: fd int

variable_types:
usrbuf
void *

variable_types:
nleft
unsigned long

variable_types: nread long

variable_types:
bufp
char *


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

v3 = n;
if ( !n )
return v3;
v5 = n;
while ( 1 )
{
v6 = write(fd, buf, v5);
if ( v6 <= 0 )
break;
LABEL_3:
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3;
}
if ( *_errno_location() == 4 )
{
v6 = 0LL;
goto LABEL_3;
}
return -1LL;
}

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

v3 = n;
if ( !n )
return v3;
v5 = n;
while ( 1 )
{
v6 = write(fd, buf, v5);
if ( v6 <= 0 )
break;
LABEL_3:
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3;
}
if ( *_errno_location() == 4 )
{
v6 = 0LL;
goto LABEL_3;
}
return -1LL;
}

original_code:
ssize_t io_writen(int fd, void *usrbuf, size_t n){
 size_t nleft = n;
 ssize_t nwritten;
 char *bufp = usrbuf;
 while (nleft > 0) {
  if ((nwritten = write(fd, bufp, nleft)) <= 0) {
   if ((*__errno_location ()) == 4)
    nwritten = 0;
   else
    return -1;
  }
  nleft -= nwritten;
  bufp += nwritten;
 }
 return n;
}

canonical_original_code:
long io_writen(int fd, void *usrbuf, unsigned long n){
 unsigned long nleft = n;
 long nwritten;
 char *bufp = usrbuf;
 while (nleft > 0) {
  if ((nwritten = write(fd, bufp, nleft)) <= 0) {
   if ((*__errno_location ()) == 4)
    nwritten = 0;
   else
    return -1;
  }
  nleft -= nwritten;
  bufp += nwritten;
 }
 return n;
}

variable_types: nwritten long


decompiled_code:
__int64 __fastcall io_initbuf(__int64 a1, int a2)
{
__int64 result; // rax

*(_DWORD *)a1 = a2;
*(_DWORD *)(a1 + 4) = 0;
result = a1 + 16;
*(_QWORD *)(a1 + 8) = a1 + 16;
return result;
}

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

*(_DWORD *)a1 = a2;
*(_DWORD *)(a1 + 4) = 0;
result = a1 + 16;
*(_QWORD *)(a1 + 8) = a1 + 16;
return result;
}

original_code:
void io_initbuf(io_t *iop, int fd) {
 iop->io_fd = fd;
 iop->io_cnt = 0;
 iop->io_bufptr = iop->io_buf;
}

canonical_original_code:
void io_initbuf(struct io_t *iop, int fd) {
 iop->io_fd = fd;
 iop->io_cnt = 0;
 iop->io_bufptr = iop->io_buf;
}

variable_types: struct io_t { int io_fd; int io_cnt; char *io_bufptr; char io_buf[8192]; } *


decompiled_code:
__int64 __fastcall io_readlineb(__int64 a1, _BYTE *a2, unsigned __int64 a3)
{
_BYTE *v3; // rbx
__int64 v4; // r13
int v5; // ebp
int v6; // eax
char v7; // al
__int64 result; // rax
char v9[41]; // [rsp+Fh] [rbp-29h] BYREF

v3 = a2;
if ( a3 <= 1 )
{
v5 = 1;
goto LABEL_7;
}
v4 = (__int64)&a2[a3 - 1];
v5 = 1;
while ( 1 )
{
v6 = io_read(a1, v9, 1uLL);
if ( v6 != 1 )
break;
++v3;
v7 = v9[0];
*(v3 - 1) = v9[0];
if ( v7 == 10 )
goto LABEL_7;
++v5;
if ( v3 == (_BYTE *)v4 )
{
v3 = (_BYTE *)v4;
goto LABEL_7;
}
}
if ( v6 )
return -1LL;
result = 0LL;
if ( v5 != 1 )
{
LABEL_7:
*v3 = 0;
result = v5;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, _BYTE *a2, unsigned __int64 a3)
{
_BYTE *v3; // rbx
__int64 v4; // r13
int v5; // ebp
int v6; // eax
char v7; // al
__int64 result; // rax
char v9[41]; // [rsp+Fh] [rbp-29h] BYREF

v3 = a2;
if ( a3 <= 1 )
{
v5 = 1;
goto LABEL_7;
}
v4 = (__int64)&a2[a3 - 1];
v5 = 1;
while ( 1 )
{
v6 = func0(a1, v9, 1uLL);
if ( v6 != 1 )
break;
++v3;
v7 = v9[0];
*(v3 - 1) = v9[0];
if ( v7 == 10 )
goto LABEL_7;
++v5;
if ( v3 == (_BYTE *)v4 )
{
v3 = (_BYTE *)v4;
goto LABEL_7;
}
}
if ( v6 )
return -1LL;
result = 0LL;
if ( v5 != 1 )
{
LABEL_7:
*v3 = 0;
result = v5;
}
return result;
}

original_code:
ssize_t io_readlineb(io_t *rp, void *usrbuf, size_t maxlen) {
 int n, rc;
 char c, *bufp = usrbuf;
 for (n = 1; n < maxlen; n++) {
  if ((rc = io_read(rp, &c, 1)) == 1) {
   *bufp++ = c;
   if (c == '\n') {
    break;
   }
  }
  else if (rc == 0) {
   if (n == 1)
    return 0;
   else
    break;
  }
  else
   return -1;
 }
 *bufp = 0;
 return n;
}

canonical_original_code:
long io_readlineb(struct io_t *rp, void *usrbuf, unsigned long maxlen) {
 int n; int rc;
 char c; char *bufp = usrbuf;
 for (n = 1; n < maxlen; n++) {
  if ((rc = io_read(rp, &c, 1)) == 1) {
   *bufp++ = c;
   if (c == '\n') {
    break;
   }
  }
  else if (rc == 0) {
   if (n == 1)
    return 0;
   else
    break;
  }
  else
   return -1;
 }
 *bufp = 0;
 return n;
}

variable_types:
maxlen
unsigned long

variable_types: n int

variable_types: rc int

variable_types: c char


decompiled_code:
__int64 __fastcall io_readnb(__int64 a1, char *a2, unsigned __int64 a3)
{
unsigned __int64 v3; // r13
unsigned __int64 v5; // rbx
__int64 v6; // rax

v3 = a3;
if ( a3 )
{
v5 = a3;
while ( 1 )
{
v6 = io_read(a1, a2, v5);
if ( v6 < 0 )
{
if ( *_errno_location() != 4 )
return -1LL;
v6 = 0LL;
}
else if ( !v6 )
{
return v3 - v5;
}
a2 += v6;
v5 -= v6;
if ( !v5 )
return v3 - v5;
}
}
v5 = 0LL;
return v3 - v5;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, char *a2, unsigned __int64 a3)
{
unsigned __int64 v3; // r13
unsigned __int64 v5; // rbx
__int64 v6; // rax

v3 = a3;
if ( a3 )
{
v5 = a3;
while ( 1 )
{
v6 = func0(a1, a2, v5);
if ( v6 < 0 )
{
if ( *_errno_location() != 4 )
return -1LL;
v6 = 0LL;
}
else if ( !v6 )
{
return v3 - v5;
}
a2 += v6;
v5 -= v6;
if ( !v5 )
return v3 - v5;
}
}
v5 = 0LL;
return v3 - v5;
}

original_code:
ssize_t io_readnb(io_t *rp, void *usrbuf, size_t n){
 size_t nleft = n;
 ssize_t nread;
 char *bufp = usrbuf;
 while (nleft > 0) {
  if ((nread = io_read(rp, bufp, nleft)) < 0) {
   if ((*__errno_location ()) == 4)
    nread = 0;
   else
    return -1;
  }
  else if (nread == 0)
   break;
  nleft -= nread;
  bufp += nread;
 }
 return (n - nleft);
}

canonical_original_code:
long io_readnb(struct io_t *rp, void *usrbuf, unsigned long n){
 unsigned long nleft = n;
 long nread;
 char *bufp = usrbuf;
 while (nleft > 0) {
  if ((nread = io_read(rp, bufp, nleft)) < 0) {
   if ((*__errno_location ()) == 4)
    nread = 0;
   else
    return -1;
  }
  else if (nread == 0)
   break;
  nleft -= nread;
  bufp += nread;
 }
 return (n - nleft);
}


logging

decompiled_code:
int __fastcall logging(__int64 a1)
{
int v1; // ebp
size_t v2; // rax

v1 = open("log.txt", 1090, 420LL);
v2 = strlen((const char *)(a1 + 16));
io_writen(v1, (void *)(a1 + 16), v2);
return close(v1);
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1)
{
int v1; // ebp
size_t v2; // rax

v1 = open("log.txt", 1090, 420LL);
v2 = strlen((const char *)(a1 + 16));
func2(v1, (void *)(a1 + 16), v2);
return close(v1);
}

original_code:
void logging(io_t *rp) {
 int filefd;
 filefd = open("log.txt", 02|0100|02000, 0400|0200|(0400 >> 3)|((0400 >> 3) >> 3));
 io_writen(filefd, rp->io_buf, strlen(rp->io_buf));
 close(filefd);
}

canonical_original_code:
void logging(struct io_t *rp) {
 int filefd;
 filefd = open("log.txt", 02|0100|02000, 0400|0200|(0400 >> 3)|((0400 >> 3) >> 3));
 io_writen(filefd, rp->io_buf, strlen(rp->io_buf));
 close(filefd);
}

variable_types: filefd int


decompiled_code:
int __fastcall io_print_buf(__int64 a1)
{
puts((const char *)(a1 + 16));
return logging(a1);
}

canonical_decompiled_code:
int __fastcall func7(__int64 a1)
{
puts((const char *)(a1 + 16));
return func6(a1);
}

original_code:
void io_print_buf(io_t *rp) {
 printf("%s\n", rp->io_buf);
 logging(rp);
}

canonical_original_code:
void io_print_buf(struct io_t *rp) {
 printf("%s\n", rp->io_buf);
 logging(rp);
}


decompiled_code:
int __fastcall io_dump_buf(const char **a1)
{
printf("io_fd: %d\n", *(unsigned int *)a1);
printf("io_cnt: %d\n", *((unsigned int *)a1 + 1));
printf("io_bufptr: %s\n", a1[1]);
return printf("io_buf: %s\n", (const char *)a1 + 16);
}

canonical_decompiled_code:
int __fastcall func8(const char **a1)
{
printf("io_fd: %d\n", *(unsigned int *)a1);
printf("io_cnt: %d\n", *((unsigned int *)a1 + 1));
printf("io_bufptr: %s\n", a1[1]);
return printf("io_buf: %s\n", (const char *)a1 + 16);
}

original_code:
void io_dump_buf(io_t *iop){
 printf("io_fd: %d\n", iop->io_fd);
 printf("io_cnt: %d\n", iop->io_cnt);
 printf("io_bufptr: %s\n", iop->io_bufptr);
 printf("io_buf: %s\n", iop->io_buf);
}

canonical_original_code:
void io_dump_buf(struct io_t *iop){
 printf("io_fd: %d\n", iop->io_fd);
 printf("io_cnt: %d\n", iop->io_cnt);
 printf("io_bufptr: %s\n", iop->io_bufptr);
 printf("io_buf: %s\n", iop->io_buf);
}


decompiled_code:
__int64 __fastcall open_clientfd(char *name, __int16 a2)
{
int v2; // eax
unsigned int v3; // ebx
struct hostent *v4; // rax
struct in_addr inp; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr dest[2]; // [rsp+10h] [rbp-28h] BYREF

v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
perror("socket fail");
v3 = -1;
}
else
{
v3 = v2;
if ( inet_aton(name, &inp) )
v4 = gethostbyaddr(&inp, 4u, 2);
else
v4 = gethostbyname(name);
if ( v4 )
{
*(_QWORD *)&dest[0].sa_family = 2LL;
*(_QWORD *)&dest[0].sa_data[6] = 0LL;
memmove(&dest[0].sa_data[2], *(const void **)v4->h_addr_list, v4->h_length);
*(_WORD *)dest[0].sa_data = _bswap_16(a2);
if ( connect(v3, dest, 0x10u) < 0 )
{
puts("connect fail!");
v3 = -3;
}
}
else
{
puts("get host fail");
v3 = -2;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(char *name, __int16 a2)
{
int v2; // eax
unsigned int v3; // ebx
struct hostent *v4; // rax
struct in_addr inp; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr dest[2]; // [rsp+10h] [rbp-28h] BYREF

v2 = socket(2, 1, 0);
if ( v2 < 0 )
{
perror("socket fail");
v3 = -1;
}
else
{
v3 = v2;
if ( inet_aton(name, &inp) )
v4 = gethostbyaddr(&inp, 4u, 2);
else
v4 = gethostbyname(name);
if ( v4 )
{
*(_QWORD *)&dest[0].sa_family = 2LL;
*(_QWORD *)&dest[0].sa_data[6] = 0LL;
memmove(&dest[0].sa_data[2], *(const void **)v4->h_addr_list, v4->h_length);
*(_WORD *)dest[0].sa_data = func0(a2);
if ( connect(v3, dest, 0x10u) < 0 )
{
puts("connect fail!");
v3 = -3;
}
}
else
{
puts("get host fail");
v3 = -2;
}
}
return v3;
}

original_code:
int open_clientfd(char *hostname, int port) {
 int clientfd;
 struct hostent *hostp;
 struct sockaddr_in serveraddr;
 struct in_addr addr;
 if ((clientfd = socket(2, SOCK_STREAM, 0)) < 0) {
  perror("socket fail");
  return -1;
 }
 if (inet_aton(hostname, &addr) != 0) {
  hostp = gethostbyaddr((char *)&addr,sizeof(addr),2);
 }
 else {
  hostp = gethostbyname(hostname);
 }
 if (hostp == ((void *)0)) {
  printf("get host fail\n");
  return -2;
 }
 bzero((char *)&serveraddr, sizeof(serveraddr));
 serveraddr.sin_family = 2;
 bcopy((char *)hostp->h_addr_list[0],
   (char *)&serveraddr.sin_addr.s_addr,
   hostp->h_length);
 serveraddr.sin_port = htons((uint16_t)port);
 if (connect(clientfd, (SA *)&serveraddr, sizeof(serveraddr)) < 0) {
  printf("connect fail!\n");
  return -3;
 }
 return clientfd;
}

canonical_original_code:
int open_clientfd(char *hostname, int port) {
 int clientfd;
 struct hostent *hostp;
 struct sockaddr_in serveraddr;
 struct in_addr addr;
 if ((clientfd = socket(2, SOCK_STREAM, 0)) < 0) {
  perror("socket fail");
  return -1;
 }
 if (inet_aton(hostname, &addr) != 0) {
  hostp = gethostbyaddr((char *)&addr,sizeof(addr),2);
 }
 else {
  hostp = gethostbyname(hostname);
 }
 if (hostp == ((void *)0)) {
  printf("get host fail\n");
  return -2;
 }
 bzero((char *)&serveraddr, sizeof(serveraddr));
 serveraddr.sin_family = 2;
 bcopy((char *)hostp->h_addr_list[0],
   (char *)&serveraddr.sin_addr.s_addr,
   hostp->h_length);
 serveraddr.sin_port = htons((unsigned short)port);
 if (connect(clientfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0) {
  printf("connect fail!\n");
  return -3;
 }
 return clientfd;
}

variable_types:
hostname
char *

variable_types: port int

variable_types: clientfd int

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

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

variable_types:
addr
struct in_addr { unsigned int s_addr; }

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

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


decompiled_code:
__int64 __fastcall open_listenfd(unsigned int a1)
{
int v1; // eax
unsigned int v2; // ebx
char *v3; // rax
struct sockaddr v5; // [rsp+0h] [rbp-38h] BYREF
int optval[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

optval[0] = 1;
v1 = socket(2, 1, 0);
if ( v1 < 0 )
{
puts("socket fail!");
v2 = -1;
}
else
{
v2 = v1;
if ( setsockopt(v1, 1, 2, optval, 4u) < 0 )
{
puts("setsockopt fail!");
v2 = -1;
}
else
{
*(_DWORD *)&v5.sa_data[10] = 0;
v5.sa_family = 2;
*(_QWORD *)&v5.sa_data[2] = (unsigned int)_bswap_32(0);
*(_WORD *)v5.sa_data = _bswap_16(a1);
if ( bind(v2, &v5, 0x10u) < 0 )
{
puts("bind fail!");
v2 = -2;
}
else if ( listen(v2, 512) < 0 )
{
puts("listen fail!");
v2 = -3;
}
else
{
v3 = inet_ntoa(*(struct in_addr *)&v5.sa_data[2]);
printf("server starts at %s:%d\n", v3, a1);
}
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
int v1; // eax
unsigned int v2; // ebx
char *v3; // rax
struct sockaddr v5; // [rsp+0h] [rbp-38h] BYREF
int optval[7]; // [rsp+1Ch] [rbp-1Ch] BYREF

optval[0] = 1;
v1 = socket(2, 1, 0);
if ( v1 < 0 )
{
puts("socket fail!");
v2 = -1;
}
else
{
v2 = v1;
if ( setsockopt(v1, 1, 2, optval, 4u) < 0 )
{
puts("setsockopt fail!");
v2 = -1;
}
else
{
*(_DWORD *)&v5.sa_data[10] = 0;
v5.sa_family = 2;
*(_QWORD *)&v5.sa_data[2] = (unsigned int)func1(0);
*(_WORD *)v5.sa_data = func0(a1);
if ( bind(v2, &v5, 0x10u) < 0 )
{
puts("bind fail!");
v2 = -2;
}
else if ( listen(v2, 512) < 0 )
{
puts("listen fail!");
v2 = -3;
}
else
{
v3 = inet_ntoa(*(struct in_addr *)&v5.sa_data[2]);
printf("server starts at %s:%d\n", v3, a1);
}
}
}
return v2;
}

original_code:
int open_listenfd(int port) {
 int listenfd, optval=1;
 struct sockaddr_in serveraddr;
 if ((listenfd = socket(2, SOCK_STREAM, 0)) < 0) {
  printf("socket fail!\n");
  return -1;
 }
 if (setsockopt(listenfd, 1, 2,
   (const void *)&optval, sizeof(int)) < 0) {
  printf("setsockopt fail!\n");
  return -1;
 }
 bzero((char *)&serveraddr, sizeof(serveraddr));
 serveraddr.sin_family = 2;
 serveraddr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
 serveraddr.sin_port = htons((uint16_t)port);
 if (bind(listenfd, (SA *)&serveraddr, sizeof(serveraddr)) < 0) {
  printf("bind fail!\n");
  return -2;
 }
 if (listen(listenfd, 512) < 0) {
  printf("listen fail!\n");
  return -3;
 }
 printf("server starts at %s:%d\n", inet_ntoa(serveraddr.sin_addr), port);
 return listenfd;
}

canonical_original_code:
int open_listenfd(int port) {
 int listenfd; int optval = 1;
 struct sockaddr_in serveraddr;
 if ((listenfd = socket(2, SOCK_STREAM, 0)) < 0) {
  printf("socket fail!\n");
  return -1;
 }
 if (setsockopt(listenfd, 1, 2,
   (void *)&optval, sizeof(int)) < 0) {
  printf("setsockopt fail!\n");
  return -1;
 }
 bzero((char *)&serveraddr, sizeof(serveraddr));
 serveraddr.sin_family = 2;
 serveraddr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
 serveraddr.sin_port = htons((unsigned short)port);
 if (bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0) {
  printf("bind fail!\n");
  return -2;
 }
 if (listen(listenfd, 512) < 0) {
  printf("listen fail!\n");
  return -3;
 }
 printf("server starts at %s:%d\n", inet_ntoa(serveraddr.sin_addr), port);
 return listenfd;
}

variable_types: listenfd int

variable_types: optval int


decompiled_code:
struct hostent *__fastcall check_client_addr(__int64 a1)
{
struct hostent *v1; // rbx
unsigned __int16 v2; // r12
char *v3; // rax

v1 = gethostbyaddr((const void *)(a1 + 4), 4u, 2);
if ( v1 )
{
v2 = _bswap_16(*(_WORD *)(a1 + 2));
v3 = inet_ntoa(*(struct in_addr *)(a1 + 4));
printf("Get connection from %s (%s:%d)\n", v1->h_name, v3, v2);
}
return v1;
}

canonical_decompiled_code:
struct hostent *__fastcall func4(__int64 a1)
{
struct hostent *v1; // rbx
unsigned __int16 v2; // r12
char *v3; // rax

v1 = gethostbyaddr((const void *)(a1 + 4), 4u, 2);
if ( v1 )
{
v2 = func0(*(_WORD *)(a1 + 2));
v3 = inet_ntoa(*(struct in_addr *)(a1 + 4));
printf("Get connection from %s (%s:%d)\n", v1->h_name, v3, v2);
}
return v1;
}

original_code:
struct hostent *check_client_addr(struct sockaddr_in *clientaddr){
 struct hostent * hostp;
 hostp = gethostbyaddr((const char *)&clientaddr->sin_addr.s_addr,
   sizeof(clientaddr->sin_addr.s_addr), 2);
 if (hostp != ((void *)0) ) {
  printf("Get connection from %s (%s:%d)\n", hostp->h_name,
    inet_ntoa(clientaddr->sin_addr), ntohs(clientaddr->sin_port));
  return hostp;
 }
 return ((void *)0);
}

canonical_original_code:
struct hostent *check_client_addr(struct sockaddr_in *clientaddr){
 struct hostent *hostp;
 hostp = gethostbyaddr((char *)&clientaddr->sin_addr.s_addr,
   sizeof(clientaddr->sin_addr.s_addr), 2);
 if (hostp != ((void *)0) ) {
  printf("Get connection from %s (%s:%d)\n", hostp->h_name,
    inet_ntoa(clientaddr->sin_addr), ntohs(clientaddr->sin_port));
  return hostp;
 }
 return ((void *)0);
}

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

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


thread

decompiled_code:
void *__fastcall thread(void *a1)
{
pthread_t v1; // rax

v1 = pthread_self();
printf("Hello world.\tby tid: %ld\n", v1);
return 0LL;
}

canonical_decompiled_code:
void *__fastcall func7(void *a1)
{
pthread_t v1; // rax

v1 = pthread_self();
printf("Hello world.\tby tid: %ld\n", v1);
return 0LL;
}

original_code:
void *thread(void *vargp) {
 printf("Hello world.\tby tid: %ld\n", pthread_self());
 return ((void *)0);
}

variable_types:
vargp
void *

return_type: void *