decompiled_code:
__int64 __fastcall parse_type(const char *a1, __int64 a2)
{
size_t v2; // rdx
__int64 result; // rax

v2 = strlen(a1);
result = 0xFFFFFFFFLL;
if ( v2 == 1 )
{
if ( (unsigned __int8)(*a1 - 73) <= 1u || (result = 0xFFFFFFFFLL, *a1 == 82) )
{
result = _isoc99_sscanf(a1, &unk_2C2, a2);
if ( (_DWORD)result != 1 )
result = 0xFFFFFFFFLL;
}
}
return result;
}

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

v2 = strlen(a1);
result = 0xFFFFFFFFLL;
if ( v2 == 1 )
{
if ( (unsigned __int8)(*a1 - 73) <= 1u || (result = 0xFFFFFFFFLL, *a1 == 82) )
{
result = _isoc99_sscanf(a1, &unk_2C2, a2);
if ( (_DWORD)result != 1 )
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int parse_type(char* str, char* type)
{
 if (strlen(str) != 1)
  return -1;
 if (*str != 'I' && *str != 'J' && *str != 'R')
  return -1;
 if (sscanf(str, "%s", type) != 1)
  return -1;
 return 1;
}

canonical_original_code:
int parse_type(char *str, char *type)
{
 if (strlen(str) != 1)
  return -1;
 if (*str != 'I' && *str != 'J' && *str != 'R')
  return -1;
 if (sscanf(str, "%s", type) != 1)
  return -1;
 return 1;
}

variable_types:
str
char *

variable_types:
type
char *


repo: jeanbroid/mips

decompiled_code:
__int64 __fastcall parse_name(const char *a1, __int64 a2)
{
size_t v2; // r13
unsigned int i; // ebp
__int64 result; // rax

v2 = strlen(a1);
for ( i = 0; i < v2; ++i )
{
if ( ((*_ctype_b_loc())[a1[i]] & 0x400) == 0 )
return 0xFFFFFFFFLL;
}
result = _isoc99_sscanf(a1, &unk_2C2, a2);
if ( (_DWORD)result != 1 )
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, __int64 a2)
{
size_t v2; // r13
unsigned int i; // ebp
__int64 result; // rax

v2 = strlen(a1);
for ( i = 0; i < v2; ++i )
{
if ( ((*_ctype_b_loc())[a1[i]] & 0x400) == 0 )
return 0xFFFFFFFFLL;
}
result = _isoc99_sscanf(a1, &unk_2C2, a2);
if ( (_DWORD)result != 1 )
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int parse_name(char* str, char* name)
{
 for (uint i = 0; i < strlen(str); i++)
  if (!((*__ctype_b_loc ())[(int) ((*(str+i)))] & (unsigned short int) _ISalpha))
   return -1;
 if (sscanf(str, "%s", name) != 1)
  return -1;
 return 1;
}

canonical_original_code:
int parse_name(char *str, char *name)
{
 for (unsigned int i = 0; i < strlen(str); i++)
  if (!((*__ctype_b_loc ())[(int) ((*(str+i)))] & (unsigned short) _ISalpha))
   return -1;
 if (sscanf(str, "%s", name) != 1)
  return -1;
 return 1;
}

variable_types:
name
char *

variable_types:
i
unsigned int

return_type: int


decompiled_code:
__int64 __fastcall parse_opcode(const char *a1, _DWORD *a2)
{
size_t v2; // rdx
__int64 result; // rax
const unsigned __int16 *v4; // rcx
const char *v5; // rax

v2 = strlen(a1);
result = 0xFFFFFFFFLL;
if ( v2 == 6 )
{
v4 = *_ctype_b_loc();
v5 = a1;
while ( (v4[*v5] & 0x800) != 0 )
{
if ( ++v5 == a1 + 6 )
{
*a2 = (char)bin_to_dec(a1);
return 1LL;
}
}
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1, _DWORD *a2)
{
size_t v2; // rdx
__int64 result; // rax
const unsigned __int16 *v4; // rcx
const char *v5; // rax

v2 = strlen(a1);
result = 0xFFFFFFFFLL;
if ( v2 == 6 )
{
v4 = *_ctype_b_loc();
v5 = a1;
while ( (v4[*v5] & 0x800) != 0 )
{
if ( ++v5 == a1 + 6 )
{
*a2 = (char)bin_to_dec(a1);
return 1LL;
}
}
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int parse_opcode(char* str, uint* opcode)
{
 if (strlen(str) != 6)
  return -1;
 for (uint i = 0; i < 6; i++)
  if (!((*__ctype_b_loc ())[(int) ((*(str+i)))] & (unsigned short int) _ISdigit))
   return -1;
 *opcode = bin_to_dec(str);
 return 1;
}

canonical_original_code:
int parse_opcode(char *str, unsigned int *opcode)
{
 if (strlen(str) != 6)
  return -1;
 for (unsigned int i = 0; i < 6; i++)
  if (!((*__ctype_b_loc ())[(int) ((*(str+i)))] & (unsigned short) _ISdigit))
   return -1;
 *opcode = bin_to_dec(str);
 return 1;
}

variable_types:
opcode
unsigned int *


decompiled_code:
__int64 __fastcall parse_function(const char *a1, _DWORD *a2)
{
return parse_opcode(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1, _DWORD *a2)
{
return func2(a1, a2);
}

original_code:
int parse_function(char* str, uint* function)
{
 return parse_opcode(str, function);
}

canonical_original_code:
int parse_function(char *str, unsigned int *function)
{
 return parse_opcode(str, function);
}

variable_types:
function
unsigned int *


decompiled_code:
_BOOL8 __fastcall is_desc_file(const char *a1)
{
char *v1; // rax
_BOOL4 v2; // edx

v1 = strrchr(a1, 46);
v2 = 1;
if ( v1 )
v2 = strcmp(v1 + 1, "desc") == 0;
return v2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func5(const char *a1)
{
char *v1; // rax
_BOOL4 v2; // edx

v1 = strrchr(a1, 46);
v2 = 1;
if ( v1 )
v2 = strcmp(v1 + 1, "desc") == 0;
return v2;
}

original_code:
int is_desc_file(char* filename)
{
    char* ext;
    ext = strrchr(filename, '.');
    if (ext) {
        if (strcmp(ext + 1, "desc"))
            return 0;
    }
    return 1;
}

canonical_original_code:
int is_desc_file(char *filename)
{
    char *ext;
    ext = strrchr(filename, '.');
    if (ext) {
        if (strcmp(ext + 1, "desc"))
            return 0;
    }
    return 1;
}

variable_types:
filename
char *

variable_types:
ext
char *


decompiled_code:
_BOOL8 __fastcall is_list_empty(__int64 a1)
{
return a1 == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func0(__int64 a1)
{
return a1 == 0;
}

original_code:
int is_list_empty(list_t l)
{
 return !l;
}

canonical_original_code:
int is_list_empty(struct list_elt *l)
{
 return !l;
}

variable_types: struct list_elt { void *val; struct list_elt *next; } *


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

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

original_code:
list_t init_list(void)
{
 return ((void *)0);
}

canonical_original_code:
struct list_elt *init_list()
{
 return ((void *)0);
}

return_type: struct list_elt { void *val; struct list_elt *next; } *


decompiled_code:
int __fastcall display_list(unsigned int **a1)
{
unsigned int **i; // rbx

for ( i = a1; !is_list_empty((__int64)i); i = (unsigned int **)i[1] )
printf("0x%08x ", **i);
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func2(unsigned int **a1)
{
unsigned int **i; // rbx

for ( i = a1; !func0((__int64)i); i = (unsigned int **)i[1] )
printf("0x%08x ", **i);
return putchar(10);
}

original_code:
void display_list(list_t list)
{
 list_t l;
 if (!is_list_empty(list)) {
  for (l = list; !is_list_empty(l); l = l->next) {
   printf("0x%08x ", *(uint32_t*) l->val);
  }
 }
 printf("\n");
}

canonical_original_code:
void display_list(struct list_elt *list)
{
 struct list_elt *l;
 if (!is_list_empty(list)) {
  for (l = list; !is_list_empty(l); l = l->next) {
   printf("0x%08x ", *(unsigned int *) l->val);
  }
 }
 printf("\n");
}

return_type: void

user_defined_types: struct list_elt { void *val; struct list_elt *next; }


decompiled_code:
__int64 __fastcall cmp_val_int(_DWORD *a1, _DWORD *a2)
{
__int64 result; // rax

result = 1LL;
if ( *a1 >= *a2 )
result = 2 * (unsigned int)(*a1 == *a2);
return result;
}

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

result = 1LL;
if ( *a1 >= *a2 )
result = 2 * (unsigned int)(*a1 == *a2);
return result;
}

original_code:
int cmp_val_int(void* elt_1, void* elt_2)
{
 if (*(uint32_t*) elt_1 < *(uint32_t*) elt_2)
  return 1;
 else if (*(uint32_t*) elt_1 == *(uint32_t*) elt_2)
  return 2;
 return 0;
}

canonical_original_code:
int cmp_val_int(void *elt_1, void *elt_2)
{
 if (*(unsigned int *) elt_1 < *(unsigned int *) elt_2)
  return 1;
 else if (*(unsigned int *) elt_1 == *(unsigned int *) elt_2)
  return 2;
 return 0;
}


decompiled_code:
__int64 __fastcall find_elt_int(_DWORD *a1, __int64 a2)
{
__int64 v2; // rbx
unsigned int v3; // ebp

v2 = a2;
v3 = is_list_empty(a2);
if ( v3 )
return (unsigned int)-1;
while ( (unsigned int)cmp_val_int(a1, *(_DWORD **)v2) != 2 )
{
++v3;
v2 = *(_QWORD *)(v2 + 8);
if ( is_list_empty(v2) )
return (unsigned int)-1;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func4(_DWORD *a1, __int64 a2)
{
__int64 v2; // rbx
unsigned int v3; // ebp

v2 = a2;
v3 = func0(a2);
if ( v3 )
return (unsigned int)-1;
while ( (unsigned int)func3(a1, *(_DWORD **)v2) != 2 )
{
++v3;
v2 = *(_QWORD *)(v2 + 8);
if ( func0(v2) )
return (unsigned int)-1;
}
return v3;
}

original_code:
int find_elt_int(void* elt, list_t list)
{
 int i = 0;
 if (is_list_empty(list))
  return -1;
 for (list_t l = list; !is_list_empty(l); l = l->next) {
  if (cmp_val_int(elt, l->val) == 2)
   return i;
  i++;
 }
 return -1;
}

canonical_original_code:
int find_elt_int(void *elt, struct list_elt *list)
{
 int i = 0;
 if (is_list_empty(list))
  return -1;
 for (struct list_elt *l = list; !is_list_empty(l); l = l->next) {
  if (cmp_val_int(elt, l->val) == 2)
   return i;
  i++;
 }
 return -1;
}

variable_types:
elt
void *

variable_types: i int


decompiled_code:
void __fastcall free_elt(void *ptr)
{
if ( ptr )
{
free(*(void **)ptr);
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func5(void *ptr)
{
if ( ptr )
{
free(*(void **)ptr);
free(ptr);
}
}

original_code:
void free_elt(list_t list)
{
 if (list) {
  free(list->val);
  free(list);
 }
}

canonical_original_code:
void free_elt(struct list_elt *list)
{
 if (list) {
  free(list->val);
  free(list);
 }
}


decompiled_code:
__int64 __fastcall del_head(void *ptr)
{
__int64 v1; // rbp

v1 = 0LL;
if ( !is_list_empty((__int64)ptr) )
{
v1 = *((_QWORD *)ptr + 1);
free_elt(ptr);
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func6(void *ptr)
{
__int64 v1; // rbp

v1 = 0LL;
if ( !func0((__int64)ptr) )
{
v1 = *((_QWORD *)ptr + 1);
func5(ptr);
}
return v1;
}

original_code:
list_t del_head(list_t head)
{
 list_t new;
 if (is_list_empty(head))
  return ((void *)0);
 new = head->next;
 free_elt(head);
 return new;
}

canonical_original_code:
struct list_elt *del_head(struct list_elt *head)
{
 struct list_elt *new;
 if (is_list_empty(head))
  return ((void *)0);
 new = head->next;
 free_elt(head);
 return new;
}

variable_types:
head
struct list_elt *


decompiled_code:
void *__fastcall del_elt_n(void *ptr, int a2)
{
void *v2; // r13
_QWORD *v3; // rbx
int i; // ebp
_QWORD *v6; // rdi

v2 = ptr;
if ( !a2 )
return (void *)del_head(ptr);
v3 = ptr;
for ( i = 0; !is_list_empty((__int64)v3) && i != a2 - 1; ++i )
v3 = (_QWORD *)v3[1];
v6 = (_QWORD *)v3[1];
v3[1] = v6[1];
free_elt(v6);
return v2;
}

canonical_decompiled_code:
void *__fastcall func7(void *ptr, int a2)
{
void *v2; // r13
_QWORD *v3; // rbx
int i; // ebp
_QWORD *v6; // rdi

v2 = ptr;
if ( !a2 )
return (void *)func6(ptr);
v3 = ptr;
for ( i = 0; !func0((__int64)v3) && i != a2 - 1; ++i )
v3 = (_QWORD *)v3[1];
v6 = (_QWORD *)v3[1];
v3[1] = v6[1];
func5(v6);
return v2;
}

original_code:
list_t del_elt_n(list_t list, int n)
{
 list_t l, tmp;
 int i = 0;
 if (n == 0)
  return del_head(list);
 for (l = list; !is_list_empty(l); l = l->next) {
  if (i == n - 1)
   break;
  i++;
 }
 tmp = l->next;
 l->next = tmp->next;
 free_elt(tmp);
 return list;
}

canonical_original_code:
struct list_elt *del_elt_n(struct list_elt *list, int n)
{
 struct list_elt *l; struct list_elt *tmp;
 int i = 0;
 if (n == 0)
  return del_head(list);
 for (l = list; !is_list_empty(l); l = l->next) {
  if (i == n - 1)
   break;
  i++;
 }
 tmp = l->next;
 l->next = tmp->next;
 free_elt(tmp);
 return list;
}

variable_types:
list
struct list_elt *

variable_types: n int


decompiled_code:
_BOOL8 __fastcall free_list(void *ptr)
{
_BOOL8 result; // rax

while ( 1 )
{
result = is_list_empty((__int64)ptr);
if ( result )
break;
ptr = (void *)del_head(ptr);
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func8(void *ptr)
{
_BOOL8 result; // rax

while ( 1 )
{
result = func0((__int64)ptr);
if ( result )
break;
ptr = (void *)func6(ptr);
}
return result;
}

original_code:
void free_list(list_t list)
{
 for (list_t l = list; !is_list_empty(l);)
  l = del_head(l);
}

canonical_original_code:
void free_list(struct list_elt *list)
{
 for (struct list_elt *l = list; !is_list_empty(l);)
  l = del_head(l);
}


decompiled_code:
int __fastcall load_function(void *a1, void **a2, const char *a3)
{
char *v3; // rax

*a2 = dlsym(a1, a3);
v3 = dlerror();
if ( v3 )
LODWORD(v3) = fprintf(stderr, "%s\n", v3);
return (int)v3;
}

canonical_decompiled_code:
int __fastcall func0(void *a1, void **a2, const char *a3)
{
char *v3; // rax

*a2 = dlsym(a1, a3);
v3 = dlerror();
if ( v3 )
LODWORD(v3) = fprintf(stderr, "%s\n", v3);
return (int)v3;
}

original_code:
void load_function(void* plugin, void** f, char* f_name)
{
 char* error;
 *f = (void*) dlsym(plugin, f_name);
 if ((error = dlerror()) != ((void *)0))
  fprintf(stderr, "%s\n", error);
}

canonical_original_code:
void load_function(void *plugin, void **f, char *f_name)
{
 char *error;
 *f = (void *) dlsym(plugin, f_name);
 if ((error = dlerror()) != ((void *)0))
  fprintf(stderr, "%s\n", error);
}

variable_types:
plugin
void *

variable_types:
f
void **

variable_types:
error
char *


decompiled_code:
__int64 __fastcall load_desc_so(const char *a1, _QWORD *a2)
{
void *v2; // rax
void *v3; // rbx
__int64 result; // rax
char *v5; // rax
void *v6; // [rsp+0h] [rbp-28h] BYREF
void *v7; // [rsp+8h] [rbp-20h] BYREF

v2 = dlopen(a1, 1);
a2[5] = v2;
if ( v2 )
{
v3 = v2;
load_function(v2, &v6, "display");
a2[3] = v6;
load_function(v3, &v7, "execute");
a2[4] = v7;
result = 1LL;
}
else
{
v5 = dlerror();
fprintf(stderr, "%s\n", v5);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1, _QWORD *a2)
{
void *v2; // rax
void *v3; // rbx
__int64 result; // rax
char *v5; // rax
void *v6; // [rsp+0h] [rbp-28h] BYREF
void *v7; // [rsp+8h] [rbp-20h] BYREF

v2 = dlopen(a1, 1);
a2[5] = v2;
if ( v2 )
{
v3 = v2;
func0(v2, &v6, "display");
a2[3] = v6;
func0(v3, &v7, "execute");
a2[4] = v7;
result = 1LL;
}
else
{
v5 = dlerror();
fprintf(stderr, "%s\n", v5);
result = 0LL;
}
return result;
}

original_code:
int load_desc_so(char* plugin_filename, DESC* desc)
{
 void* plugin;
 execute_f execute;
 display_f display;
 plugin = dlopen(plugin_filename, 0x00001);
 desc->plugin = plugin;
 if (!plugin) {
  fprintf(stderr, "%s\n", dlerror());
  return 0;
 }
 else {
  load_function(plugin, (void **) &display, "display");
  desc->display = display;
  load_function(plugin, (void **) &execute, "execute");
  desc->execute = execute;
 }
 return 1;
}

canonical_original_code:
int load_desc_so(char *plugin_filename, struct DESC *desc)
{
 void *plugin;
 void (*execute)();
 void (*display)();
 plugin = dlopen(plugin_filename, 0x00001);
 desc->plugin = plugin;
 if (!plugin) {
  fprintf(stderr, "%s\n", dlerror());
  return 0;
 }
 else {
  load_function(plugin, (void **) &display, "display");
  desc->display = display;
  load_function(plugin, (void **) &execute, "execute");
  desc->execute = execute;
 }
 return 1;
}

variable_types: struct DESC { char name[8]; char type; unsigned int opcode; unsigned int function; void (*display)(); void (*execute)(); void *plugin; } *

variable_types:
execute
void (*)()

variable_types:
display
void (*)()


decompiled_code:
int __fastcall display_desc(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, char a7)
{
return fprintf(stderr, "%-8s", &a7);
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, char a7)
{
return fprintf(stderr, "%-8s", &a7);
}

original_code:
void display_desc(DESC desc)
{
 fprintf(stderr, "%-8s", desc.name);
}

canonical_original_code:
void display_desc(struct DESC desc)
{
 fprintf(stderr, "%-8s", desc.name);
}

variable_types: struct DESC { char name[8]; char type; unsigned int opcode; unsigned int function; void (*display)(); void (*execute)(); void *plugin; }


decompiled_code:
int __fastcall display_desc_array(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
int result; // eax
__int64 v7; // rdx
__int64 v8; // rcx
__int64 v9; // r8
__int64 v10; // r9
__int64 v11; // rbx
int v12; // ebp

if ( DESC_ARRAY_LENGTH <= 0 )
return putchar(10);
result = display_desc(a1, a2, a3, a4, a5, a6, *DESC_ARRAY);
v11 = 48LL;
v12 = 1;
while ( DESC_ARRAY_LENGTH > v12 )
{
result = display_desc(a1, a2, v7, v8, v9, v10, *(_QWORD *)(DESC_ARRAY + v11));
if ( (++v12 & 3) == 0 )
{
a1 = 10LL;
result = putchar(10);
}
v11 += 48LL;
}
if ( (((_BYTE)v12 + 1) & 3) != 0 )
return putchar(10);
return result;
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
int result; // eax
__int64 v7; // rdx
__int64 v8; // rcx
__int64 v9; // r8
__int64 v10; // r9
__int64 v11; // rbx
int v12; // ebp

if ( DESC_ARRAY_LENGTH <= 0 )
return putchar(10);
result = func2(a1, a2, a3, a4, a5, a6, *DESC_ARRAY);
v11 = 48LL;
v12 = 1;
while ( DESC_ARRAY_LENGTH > v12 )
{
result = func2(a1, a2, v7, v8, v9, v10, *(_QWORD *)(DESC_ARRAY + v11));
if ( (++v12 & 3) == 0 )
{
a1 = 10LL;
result = putchar(10);
}
v11 += 48LL;
}
if ( (((_BYTE)v12 + 1) & 3) != 0 )
return putchar(10);
return result;
}

original_code:
void display_desc_array(void)
{
 int i;
 for (i = 0; i < DESC_ARRAY_LENGTH; i++) {
  display_desc(DESC_ARRAY[i]);
  if ((i+1)%4 == 0)
   printf("\n");
 }
 if ((i+1)%4 != 0)
  printf("\n");
}

canonical_original_code:
void display_desc_array()
{
 int i;
 for (i = 0; i < DESC_ARRAY_LENGTH; i++) {
  display_desc(DESC_ARRAY[i]);
  if ((i+1)%4 == 0)
   printf("\n");
 }
 if ((i+1)%4 != 0)
  printf("\n");
}


decompiled_code:
__int64 __fastcall parse_hex_value(char *s, __int64 a2, unsigned int a3, unsigned int a4)
{
char *v4; // rbx
int v7; // edx
__int64 result; // rax
unsigned int v9; // ebp
size_t v10; // rcx
size_t v11; // r14
__int64 v12; // r12
const unsigned __int16 *v13; // rdx

v4 = s;
v7 = _isoc99_sscanf(s, &unk_3B7, s);
result = 0LL;
if ( v7 != 1 )
return result;
if ( strlen(s) <= 1 || *s != 48 )
goto LABEL_4;
if ( s[1] == 120 )
{
v4 = s + 2;
LABEL_4:
v9 = 0;
if ( *v4 == 45 )
{
result = a4;
if ( !a4 )
return result;
v9 = 1;
}
v10 = strlen(v4);
v11 = v10;
result = 0LL;
if ( v10 > a3 )
return result;
v12 = v9;
if ( v10 <= v9 )
return (unsigned int)_isoc99_sscanf(v4, &unk_3BA, a2) == 1;
goto LABEL_7;
}
v11 = strlen(s);
result = 0LL;
if ( v11 > a3 )
return result;
v12 = 0LL;
v9 = 0;
LABEL_7:
v13 = *_ctype_b_loc();
while ( (v13[v4[v12]] & 0x1000) != 0 )
{
v12 = ++v9;
if ( v9 >= v11 )
return (unsigned int)_isoc99_sscanf(v4, &unk_3BA, a2) == 1;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *s, __int64 a2, unsigned int a3, unsigned int a4)
{
char *v4; // rbx
int v7; // edx
__int64 result; // rax
unsigned int v9; // ebp
size_t v10; // rcx
size_t v11; // r14
__int64 v12; // r12
const unsigned __int16 *v13; // rdx

v4 = s;
v7 = _isoc99_sscanf(s, &unk_3B7, s);
result = 0LL;
if ( v7 != 1 )
return result;
if ( strlen(s) <= 1 || *s != 48 )
goto LABEL_4;
if ( s[1] == 120 )
{
v4 = s + 2;
LABEL_4:
v9 = 0;
if ( *v4 == 45 )
{
result = a4;
if ( !a4 )
return result;
v9 = 1;
}
v10 = strlen(v4);
v11 = v10;
result = 0LL;
if ( v10 > a3 )
return result;
v12 = v9;
if ( v10 <= v9 )
return (unsigned int)_isoc99_sscanf(v4, &unk_3BA, a2) == 1;
goto LABEL_7;
}
v11 = strlen(s);
result = 0LL;
if ( v11 > a3 )
return result;
v12 = 0LL;
v9 = 0;
LABEL_7:
v13 = *_ctype_b_loc();
while ( (v13[v4[v12]] & 0x1000) != 0 )
{
v12 = ++v9;
if ( v9 >= v11 )
return (unsigned int)_isoc99_sscanf(v4, &unk_3BA, a2) == 1;
}
return 0LL;
}

original_code:
int parse_hex_value(char* hex_str, uint* hex_value, uint hex_leng, int neg_flag)
{
 uint i;
 if (sscanf(hex_str, "%s", hex_str) != 1)
  return 0;
 if (strlen(hex_str) > 1) {
  if (*(hex_str) == '0' && *(hex_str+1) == 'x')
   hex_str += 2;
 }
 if (*(hex_str) == '-') {
  if (!neg_flag)
   return 0;
  else
   i = 1;
 } else {
  i = 0;
 }
 if (strlen(hex_str) > hex_leng)
  return 0;
 for (; i < strlen(hex_str); i++) {
  if (!((*__ctype_b_loc ())[(int) ((*(hex_str+i)))] & (unsigned short int) _ISxdigit))
   return 0;
 }
 if (sscanf(hex_str, "%x", hex_value) != 1)
  return 0;
 return 1;
}

canonical_original_code:
int parse_hex_value(char *hex_str, unsigned int *hex_value, unsigned int hex_leng, int neg_flag)
{
 unsigned int i;
 if (sscanf(hex_str, "%s", hex_str) != 1)
  return 0;
 if (strlen(hex_str) > 1) {
  if (*(hex_str) == '0' && *(hex_str+1) == 'x')
   hex_str += 2;
 }
 if (*(hex_str) == '-') {
  if (!neg_flag)
   return 0;
  else
   i = 1;
 } else {
  i = 0;
 }
 if (strlen(hex_str) > hex_leng)
  return 0;
 for (; i < strlen(hex_str); i++) {
  if (!((*__ctype_b_loc ())[(int) ((*(hex_str+i)))] & (unsigned short) _ISxdigit))
   return 0;
 }
 if (sscanf(hex_str, "%x", hex_value) != 1)
  return 0;
 return 1;
}

variable_types:
i
unsigned int


decompiled_code:
__int64 __fastcall parse_addr(char *a1, __int64 a2)
{
return parse_hex_value(a1, a2, 8u, 0);
}

canonical_decompiled_code:
__int64 __fastcall func1(char *a1, __int64 a2)
{
return func0(a1, a2, 8u, 0);
}

original_code:
int parse_addr(char* addr_str, uint* addr)
{
 return parse_hex_value(addr_str, addr, 8, 0);
}

canonical_original_code:
int parse_addr(char *addr_str, unsigned int *addr)
{
 return parse_hex_value(addr_str, addr, 8, 0);
}

variable_types:
addr
unsigned int *


decompiled_code:
__int64 __fastcall parse_reg_value(char *a1, __int64 a2)
{
return parse_hex_value(a1, a2, 8u, 1u);
}

canonical_decompiled_code:
__int64 __fastcall func2(char *a1, __int64 a2)
{
return func0(a1, a2, 8u, 1u);
}

original_code:
int parse_reg_value(char* reg_val_str, uint* reg_value)
{
 return parse_hex_value(reg_val_str, reg_value, 8, 1);
}

canonical_original_code:
int parse_reg_value(char *reg_val_str, unsigned int *reg_value)
{
 return parse_hex_value(reg_val_str, reg_value, 8, 1);
}


decompiled_code:
__int64 __fastcall parse_addr_value(char *a1, __int64 a2)
{
return parse_addr(a1, a2);
}

canonical_decompiled_code:
__int64 __fastcall func3(char *a1, __int64 a2)
{
return func1(a1, a2);
}

original_code:
int parse_addr_value(char* addr_value_str, uint* addr_value)
{
 return parse_addr(addr_value_str, addr_value);
}

canonical_original_code:
int parse_addr_value(char *addr_value_str, unsigned int *addr_value)
{
 return parse_addr(addr_value_str, addr_value);
}


decompiled_code:
__int64 __fastcall parse_register(_BYTE *a1)
{
unsigned int v1; // er12
const char *v2; // rbp
size_t v3; // rax
__int64 v4; // rbx
const unsigned __int16 *v6; // rax

v1 = 0;
if ( (unsigned int)_isoc99_sscanf(a1, &unk_3B7, a1) == 1 )
{
v2 = &a1[*a1 == 36];
v3 = strlen(v2);
if ( v3 == 1 )
{
v4 = 0LL;
if ( ((*_ctype_b_loc())[*v2] & 0x800) == 0 )
goto LABEL_4;
v1 = atoi(v2);
}
else
{
v4 = 0LL;
if ( v3 != 2 )
goto LABEL_4;
v6 = *_ctype_b_loc();
if ( (v6[*v2] & 0x800) == 0 )
{
v4 = 0LL;
goto LABEL_4;
}
if ( (v6[v2[1]] & 0x800) != 0 )
{
v1 = atoi(v2);
if ( v1 > 0x23 )
v1 = -1;
}
else
{
v4 = 0LL;
LABEL_4:
while ( 1 )
{
v1 = v4;
if ( !strcmp(*((const char **)&REG_NAMES + v4), v2) )
break;
if ( ++v4 == 36 )
return (unsigned int)-1;
}
}
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func4(_BYTE *a1)
{
unsigned int v1; // er12
const char *v2; // rbp
size_t v3; // rax
__int64 v4; // rbx
const unsigned __int16 *v6; // rax

v1 = 0;
if ( (unsigned int)_isoc99_sscanf(a1, &unk_3B7, a1) == 1 )
{
v2 = &a1[*a1 == 36];
v3 = strlen(v2);
if ( v3 == 1 )
{
v4 = 0LL;
if ( ((*_ctype_b_loc())[*v2] & 0x800) == 0 )
goto LABEL_4;
v1 = atoi(v2);
}
else
{
v4 = 0LL;
if ( v3 != 2 )
goto LABEL_4;
v6 = *_ctype_b_loc();
if ( (v6[*v2] & 0x800) == 0 )
{
v4 = 0LL;
goto LABEL_4;
}
if ( (v6[v2[1]] & 0x800) != 0 )
{
v1 = atoi(v2);
if ( v1 > 0x23 )
v1 = -1;
}
else
{
v4 = 0LL;
LABEL_4:
while ( 1 )
{
v1 = v4;
if ( !strcmp(*((const char **)&REG_NAMES + v4), v2) )
break;
if ( ++v4 == 36 )
return (unsigned int)-1;
}
}
}
}
return v1;
}

original_code:
int parse_register(char* reg_str)
{
 int reg_index = -1;
 if (sscanf(reg_str, "%s", reg_str) != 1)
  return 0;
 if (*reg_str == '$') {
  reg_str++;
 }
 if (strlen(reg_str) == 1 && ((*__ctype_b_loc ())[(int) ((*reg_str))] & (unsigned short int) _ISdigit)) {
  return atoi(reg_str);
 }
 else if (strlen(reg_str) == 2 && ((*__ctype_b_loc ())[(int) ((*reg_str))] & (unsigned short int) _ISdigit) && ((*__ctype_b_loc ())[(int) ((*(reg_str+1)))] & (unsigned short int) _ISdigit)) {
  reg_index = atoi(reg_str);
  if (reg_index < 36 && reg_index >= 0)
   return atoi(reg_str);
  else
   return -1;
 }
 else {
  for (int i = 0; i < 36; i++) {
   if (strcmp(*(REG_NAMES+i), reg_str) == 0)
    return i;
  }
 }
 return reg_index;
}

canonical_original_code:
int parse_register(char *reg_str)
{
 int reg_index = -1;
 if (sscanf(reg_str, "%s", reg_str) != 1)
  return 0;
 if (*reg_str == '$') {
  reg_str++;
 }
 if (strlen(reg_str) == 1 && ((*__ctype_b_loc ())[(int) ((*reg_str))] & (unsigned short) _ISdigit)) {
  return atoi(reg_str);
 }
 else if (strlen(reg_str) == 2 && ((*__ctype_b_loc ())[(int) ((*reg_str))] & (unsigned short) _ISdigit) && ((*__ctype_b_loc ())[(int) ((*(reg_str+1)))] & (unsigned short) _ISdigit)) {
  reg_index = atoi(reg_str);
  if (reg_index < 36 && reg_index >= 0)
   return atoi(reg_str);
  else
   return -1;
 }
 else {
  for (int i = 0; i < 36; i++) {
   if (strcmp(*(REG_NAMES+i), reg_str) == 0)
    return i;
  }
 }
 return reg_index;
}


decompiled_code:
__int64 __fastcall parse_token(char *delim, char *a2, __int64 a3, int *a4)
{
char *v8; // rax

while ( 1 )
{
v8 = strtok(a2, delim);
if ( !v8 )
break;
*(_QWORD *)(a3 + 8LL * *a4) = v8;
*(_QWORD *)(a3 + 8LL * (*a4)++ + 8) = delim;
a2 = 0LL;
}
return swap_str(a3 + 8LL * *a4 - 8, a3 + 8LL * *a4);
}

canonical_decompiled_code:
__int64 __fastcall func5(char *delim, char *a2, __int64 a3, int *a4)
{
char *v8; // rax

while ( 1 )
{
v8 = strtok(a2, delim);
if ( !v8 )
break;
*(_QWORD *)(a3 + 8LL * *a4) = v8;
*(_QWORD *)(a3 + 8LL * (*a4)++ + 8) = delim;
a2 = 0LL;
}
return swap_str(a3 + 8LL * *a4 - 8, a3 + 8LL * *a4);
}

original_code:
void parse_token(char* delim, char* token, char** args, int* i)
{
 char* str2;
 char* subtoken;
 for (str2 = token; ; str2 = ((void *)0), (*i)++) {
  if (!(subtoken = strtok(str2, delim)))
    break;
  *(args+*i) = subtoken;
  *(args+*i+1) = delim;
 }
 swap_str(args+*i-1, args+*i);
}

canonical_original_code:
void parse_token(char *delim, char *token, char **args, int *i)
{
 char *str2;
 char *subtoken;
 for (str2 = token; ; str2 = ((void *)0), (*i)++) {
  if (!(subtoken = strtok(str2, delim)))
    break;
  *(args+*i) = subtoken;
  *(args+*i+1) = delim;
 }
 swap_str(args+*i-1, args+*i);
}

variable_types:
delim
char *

variable_types:
token
char *

variable_types:
args
char **

variable_types:
i
int *

variable_types:
subtoken
char *


original_code:
int parse_args(struct command* cmd, char* str_arg, char** args)
{
 char* str1;
 char* token;
 char* found_colon, *found_tild;
 char* delim = " ";
 int i;
 for (i = 0, str1 = str_arg; i < 32 - 1; str1 = ((void *)0), i++) {
  if (!(token = strtok(str1, delim)))
   break;
  *(args+i) = token;
  if (*(args+i)) {
   if (strcmp("\n", *(args+i)) == 0) {
    *(args+i) = ((void *)0);
    break;
   }
  }
  if ((found_colon = strchr(token, ':')) && strlen(token) > 1)
   parse_token(":", token, args, &i);
  if ((found_tild = strchr(token, '~')) && strlen(token) > 1)
   parse_token("~", token, args, &i);
 }
 if (i < cmd->min || ( i > cmd->max && cmd->max != -1))
  return 0;
 return 1;
}

canonical_original_code:
int parse_args(struct command *cmd, char *str_arg, char **args)
{
 char *str1;
 char *token;
 char *found_colon; char *found_tild;
 char *delim = " ";
 int i;
 for (i = 0, str1 = str_arg; i < 32 - 1; str1 = ((void *)0), i++) {
  if (!(token = strtok(str1, delim)))
   break;
  *(args+i) = token;
  if (*(args+i)) {
   if (strcmp("\n", *(args+i)) == 0) {
    *(args+i) = ((void *)0);
    break;
   }
  }
  if ((found_colon = strchr(token, ':')) && strlen(token) > 1)
   parse_token(":", token, args, &i);
  if ((found_tild = strchr(token, '~')) && strlen(token) > 1)
   parse_token("~", token, args, &i);
 }
 if (i < cmd->min || ( i > cmd->max && cmd->max != -1))
  return 0;
 return 1;
}

variable_types: struct command { char *command; int (*ptr)(); char *usage; char *help; int min; } *

variable_types: i int


decompiled_code:
void free_desc_array()
{
__int64 v0; // rbp
int v1; // ebx

if ( DESC_ARRAY_LENGTH > 0 )
{
v0 = 0LL;
v1 = 0;
do
{
dlclose(*(void **)((char *)DESC_ARRAY + v0 + 40));
++v1;
v0 += 48LL;
}
while ( DESC_ARRAY_LENGTH > v1 );
}
free(DESC_ARRAY);
}

canonical_decompiled_code:
void func0()
{
__int64 v0; // rbp
int v1; // ebx

if ( DESC_ARRAY_LENGTH > 0 )
{
v0 = 0LL;
v1 = 0;
do
{
dlclose(*(void **)((char *)DESC_ARRAY + v0 + 40));
++v1;
v0 += 48LL;
}
while ( DESC_ARRAY_LENGTH > v1 );
}
free(DESC_ARRAY);
}

original_code:
void free_desc_array(void)
{
 for (int i = 0; i < DESC_ARRAY_LENGTH; i++)
  dlclose(DESC_ARRAY[i].plugin);
 free(DESC_ARRAY);
}

canonical_original_code:
void free_desc_array()
{
 for (int i = 0; i < DESC_ARRAY_LENGTH; i++)
  dlclose(DESC_ARRAY[i].plugin);
 free(DESC_ARRAY);
}


original_code:
void init_stack(ARCH arch)
{
 arch->sections[3].start_addr = arch->sections[2].start_addr + 0x1000;
 arch->sections[3].size = 0x1000;
 arch->sections[3].data = (uchar*) calloc(0x1000, sizeof(uchar));
 arch->sections[3].name = (char*) calloc(256, sizeof(char));
 strcpy(arch->sections[3].name, ".stack");
 set_register(arch, 29, arch->sections[3].start_addr + arch->sections[3].size - 4);
}

canonical_original_code:
void init_stack(struct { unsigned int registers[36]; struct section_t sections[4]; enum state_t state; struct list_elt *breakpoints; struct Elf32_Sym *symbols; unsigned int symbols_num; char *symbols_names; } *arch)
{
 arch->sections[3].start_addr = arch->sections[2].start_addr + 0x1000;
 arch->sections[3].size = 0x1000;
 arch->sections[3].data = (unsigned char *) calloc(0x1000, sizeof(uchar));
 arch->sections[3].name = (char *) calloc(256, sizeof(char));
 strcpy(arch->sections[3].name, ".stack");
 set_register(arch, 29, arch->sections[3].start_addr + arch->sections[3].size - 4);
}

variable_types: struct { unsigned int registers[36]; struct section_t { char *name; unsigned char *data; unsigned int start_addr; unsigned int size; } sections[4]; enum state_t { NOT_LOADED=0, PAUSED=1, FINISHED=2 } state; struct list_elt { void *val; struct list_elt *next; } *breakpoints; struct Elf32_Sym { unsigned int st_name; unsigned int st_value; unsigned int st_size; unsigned char st_info; unsigned char st_other; unsigned short st_shndx; } *symbols; unsigned int symbols_num; char *symbols_names; } *


original_code:
ARCH init_simu(void)
{
 section_t* section_ptr;
 ARCH arch;
 arch = (ARCH) calloc(1, sizeof(*arch));
 if (arch == ((void *)0))
  return ((void *)0);
 arch->sections[0].start_addr = 0x400;
 arch->sections[2].start_addr = 0xc00;
 arch->sections[1].start_addr = 0x1400;
 for (int i = 0; i < 3 ; i++) {
  section_ptr = &(arch->sections[i]);
  section_ptr->size = 1024;
  section_ptr->data = (uchar*) calloc((section_ptr->size)*1024, sizeof(uchar));
  if (section_ptr->data == ((void *)0))
   return ((void *)0);
 }
 init_stack(arch);
 arch->state = NOT_LOADED;
 arch->breakpoints = init_list();
 return arch;
}

canonical_original_code:
struct { unsigned int registers[36]; struct section_t sections[4]; enum state_t state; struct list_elt *breakpoints; struct Elf32_Sym *symbols; unsigned int symbols_num; char *symbols_names; } *init_simu()
{
 struct section_t *section_ptr;
 struct { unsigned int registers[36]; struct section_t sections[4]; enum state_t state; struct list_elt *breakpoints; struct Elf32_Sym *symbols; unsigned int symbols_num; char *symbols_names; } *arch;
 arch = (struct { unsigned int registers[36]; struct section_t sections[4]; enum state_t state; struct list_elt *breakpoints; struct Elf32_Sym *symbols; unsigned int symbols_num; char *symbols_names; } *) calloc(1, sizeof(*arch));
 if (arch == ((void *)0))
  return ((void *)0);
 arch->sections[0].start_addr = 0x400;
 arch->sections[2].start_addr = 0xc00;
 arch->sections[1].start_addr = 0x1400;
 for (int i = 0; i < 3 ; i++) {
  section_ptr = &(arch->sections[i]);
  section_ptr->size = 1024;
  section_ptr->data = (unsigned char *) calloc((section_ptr->size)*1024, sizeof(uchar));
  if (section_ptr->data == ((void *)0))
   return ((void *)0);
 }
 init_stack(arch);
 arch->state = NOT_LOADED;
 arch->breakpoints = init_list();
 return arch;
}

variable_types: struct section_t { char *name; unsigned char *data; unsigned int start_addr; unsigned int size; } *

return_type: struct { unsigned int registers[36]; struct section_t { char *name; unsigned char *data; unsigned int start_addr; unsigned int size; } sections[4]; enum state_t { NOT_LOADED=0, PAUSED=1, FINISHED=2 } state; struct list_elt { void *val; struct list_elt *next; } *breakpoints; struct Elf32_Sym { unsigned int st_name; unsigned int st_value; unsigned int st_size; unsigned char st_info; unsigned char st_other; unsigned short st_shndx; } *symbols; unsigned int symbols_num; char *symbols_names; } *


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

v1 = (void **)((char *)ptr + 144);
do
{
free(*v1);
free(v1[1]);
v1 += 3;
}
while ( v1 != (void **)((char *)ptr + 240) );
free(*((void **)ptr + 32));
if ( !(unsigned int)is_list_empty(*((_QWORD *)ptr + 31)) )
free_list(*((_QWORD *)ptr + 31));
free(*((void **)ptr + 34));
free_desc_array();
free(ptr);
}

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

v1 = (void **)((char *)ptr + 144);
do
{
free(*v1);
free(v1[1]);
v1 += 3;
}
while ( v1 != (void **)((char *)ptr + 240) );
free(*((void **)ptr + 32));
if ( !(unsigned int)is_list_empty(*((_QWORD *)ptr + 31)) )
free_list(*((_QWORD *)ptr + 31));
free(*((void **)ptr + 34));
free_desc_array();
free(ptr);
}

original_code:
void free_arch(ARCH arch)
{
 for (int i = 0; i < 4 ; i++) {
  free((arch->sections[i]).name);
  free((arch->sections[i]).data);
 }
 free(arch->symbols);
 if (!is_list_empty(arch->breakpoints))
  free_list(arch->breakpoints);
 free(arch->symbols_names);
 free_desc_array();
 free(arch);
}

canonical_original_code:
void free_arch(struct { unsigned int registers[36]; struct section_t sections[4]; enum state_t state; struct list_elt *breakpoints; struct Elf32_Sym *symbols; unsigned int symbols_num; char *symbols_names; } *arch)
{
 for (int i = 0; i < 4 ; i++) {
  free((arch->sections[i]).name);
  free((arch->sections[i]).data);
 }
 free(arch->symbols);
 if (!is_list_empty(arch->breakpoints))
  free_list(arch->breakpoints);
 free(arch->symbols_names);
 free_desc_array();
 free(arch);
}

user_defined_types: enum state_t { NOT_LOADED=0, PAUSED=1, FINISHED=2 }

user_defined_types: struct section_t { char *name; unsigned char *data; unsigned int start_addr; unsigned int size; }

user_defined_types: struct Elf32_Sym { unsigned int st_name; unsigned int st_value; unsigned int st_size; unsigned char st_info; unsigned char st_other; unsigned short st_shndx; }


decompiled_code:
__int64 __fastcall parser_instr(int a1, unsigned __int8 a2, unsigned __int8 a3)
{
int v3; // ecx
unsigned int v4; // eax

if ( a2 > (int)a3 )
{
v4 = 0;
}
else
{
v3 = a2;
v4 = 0;
do
v4 |= 1 << v3++;
while ( v3 != a3 + 1 );
}
return (a1 & v4) >> a2;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, unsigned __int8 a2, unsigned __int8 a3)
{
int v3; // ecx
unsigned int v4; // eax

if ( a2 > (int)a3 )
{
v4 = 0;
}
else
{
v3 = a2;
v4 = 0;
do
v4 |= 1 << v3++;
while ( v3 != a3 + 1 );
}
return (a1 & v4) >> a2;
}

original_code:
uint parser_instr(uint val, uchar start, uchar stop)
{
    int i;
    uint isol = 0;
    uint res;
    for (i=start; i<stop+1; i++){
        isol |= (1<<i) ;
    }
    res = val & isol;
    res = res >> start;
    return res;
}

canonical_original_code:
unsigned int parser_instr(unsigned int val, unsigned char start, unsigned char stop)
{
    int i;
    unsigned int isol = 0;
    unsigned int res;
    for (i=start; i<stop+1; i++){
        isol |= (1<<i) ;
    }
    res = val & isol;
    res = res >> start;
    return res;
}

variable_types:
val
unsigned int

variable_types:
start
unsigned char

variable_types:
stop
unsigned char

variable_types:
isol
unsigned int

variable_types:
res
unsigned int

return_type: unsigned int


decompiled_code:
__int64 __fastcall parser_typeR(int a1, _DWORD *a2, _DWORD *a3, _DWORD *a4, _DWORD *a5)
{
*a2 = parser_instr(a1, 0x15u, 0x19u);
*a3 = parser_instr(a1, 0x10u, 0x14u);
*a4 = parser_instr(a1, 0xBu, 0xFu);
*a5 = parser_instr(a1, 6u, 0xAu);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, _DWORD *a2, _DWORD *a3, _DWORD *a4, _DWORD *a5)
{
*a2 = func0(a1, 0x15u, 0x19u);
*a3 = func0(a1, 0x10u, 0x14u);
*a4 = func0(a1, 0xBu, 0xFu);
*a5 = func0(a1, 6u, 0xAu);
return 1LL;
}

original_code:
int parser_typeR(uint val , uint *rs, uint *rt, uint *rd, uint *sa)
{
    *rs = parser_instr(val, 21, 25 );
    *rt = parser_instr(val, 16, 20 );
    *rd = parser_instr(val, 11, 15 );
    *sa = parser_instr(val, 6, 10 );
    return 1;
}

canonical_original_code:
int parser_typeR(unsigned int val, unsigned int *rs, unsigned int *rt, unsigned int *rd, unsigned int *sa)
{
    *rs = parser_instr(val, 21, 25 );
    *rt = parser_instr(val, 16, 20 );
    *rd = parser_instr(val, 11, 15 );
    *sa = parser_instr(val, 6, 10 );
    return 1;
}

variable_types:
rs
unsigned int *

variable_types:
rt
unsigned int *

variable_types:
rd
unsigned int *

variable_types:
sa
unsigned int *


decompiled_code:
__int64 __fastcall parser_typeI(int a1, _DWORD *a2, _DWORD *a3, _DWORD *a4)
{
*a2 = parser_instr(a1, 0x15u, 0x19u);
*a3 = parser_instr(a1, 0x10u, 0x14u);
*a4 = parser_instr(a1, 0, 0xFu);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, _DWORD *a2, _DWORD *a3, _DWORD *a4)
{
*a2 = func0(a1, 0x15u, 0x19u);
*a3 = func0(a1, 0x10u, 0x14u);
*a4 = func0(a1, 0, 0xFu);
return 1LL;
}

original_code:
int parser_typeI(uint val, uint *rs, uint *rt, uint *immediate)
{
    *rs = parser_instr(val, 21, 25 );
    *rt = parser_instr(val, 16, 20 );
    *immediate = parser_instr(val, 0, 15);
    return 1;
}

canonical_original_code:
int parser_typeI(unsigned int val, unsigned int *rs, unsigned int *rt, unsigned int *immediate)
{
    *rs = parser_instr(val, 21, 25 );
    *rt = parser_instr(val, 16, 20 );
    *immediate = parser_instr(val, 0, 15);
    return 1;
}

variable_types:
immediate
unsigned int *


decompiled_code:
__int64 __fastcall parser_typeJ(int a1, _DWORD *a2)
{
*a2 = parser_instr(a1, 0, 0x19u);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, _DWORD *a2)
{
*a2 = func0(a1, 0, 0x19u);
return 1LL;
}

original_code:
int parser_typeJ(uint val, uint *target)
{
    *target = parser_instr(val, 0, 25 );
    return 1;
}

canonical_original_code:
int parser_typeJ(unsigned int val, unsigned int *target)
{
    *target = parser_instr(val, 0, 25 );
    return 1;
}

variable_types:
target
unsigned int *


decompiled_code:
char **__fastcall find_cmd(char *s1)
{
const char *v1; // rsi
__int64 v2; // rbx
int v3; // ebp

v1 = cmd_table;
if ( !cmd_table )
return (char **)cmd_table;
v2 = (unsigned int)((_DWORD)&cmd_table + 40);
v3 = 0;
while ( strcmp(s1, v1) )
{
++v3;
v2 += 40LL;
v1 = *(const char **)(v2 - 40);
if ( !v1 )
return (char **)v1;
}
return &(&cmd_table)[5 * v3];
}

canonical_decompiled_code:
char **__fastcall func0(char *s1)
{
const char *v1; // rsi
__int64 v2; // rbx
int v3; // ebp

v1 = cmd_table;
if ( !cmd_table )
return (char **)cmd_table;
v2 = (unsigned int)((_DWORD)&cmd_table + 40);
v3 = 0;
while ( strcmp(s1, v1) )
{
++v3;
v2 += 40LL;
v1 = *(const char **)(v2 - 40);
if ( !v1 )
return (char **)v1;
}
return &(&cmd_table)[5 * v3];
}

original_code:
struct command* find_cmd(char* cmd)
{
 for (int i = 0; cmd_table[i].command != ((void *)0); i++) {
  if (strcmp(cmd, cmd_table[i].command) == 0)
   return cmd_table+i;
 }
 return ((void *)0);
}

canonical_original_code:
struct command *find_cmd(char *cmd)
{
 for (int i = 0; cmd_table[i].command != ((void *)0); i++) {
  if (strcmp(cmd, cmd_table[i].command) == 0)
   return cmd_table+i;
 }
 return ((void *)0);
}

variable_types:
cmd
char *

return_type: struct command { char *command; int (*ptr)(); char *usage; char *help; int min; } *


decompiled_code:
int __fastcall print_usage(const char **a1)
{
const char *v1; // rdx
int result; // eax

fwrite("Usage: ", 1uLL, 7uLL, stderr);
v1 = a1[2];
if ( *v1 )
result = fprintf(stderr, "%s\n", v1);
else
result = fprintf(stderr, "%s\n", *a1);
return result;
}

canonical_decompiled_code:
int __fastcall func1(const char **a1)
{
const char *v1; // rdx
int result; // eax

fwrite("Usage: ", 1uLL, 7uLL, stderr);
v1 = a1[2];
if ( *v1 )
result = fprintf(stderr, "%s\n", v1);
else
result = fprintf(stderr, "%s\n", *a1);
return result;
}

original_code:
void print_usage(struct command* command)
{
 fprintf(stderr, "Usage: ");
 if (strcmp(command->usage, "") != 0)
  fprintf(stderr, "%s\n", command->usage);
 else
  fprintf(stderr, "%s\n", command->command);
}

canonical_original_code:
void print_usage(struct command *command)
{
 fprintf(stderr, "Usage: ");
 if (strcmp(command->usage, "") != 0)
  fprintf(stderr, "%s\n", command->usage);
 else
  fprintf(stderr, "%s\n", command->command);
}

user_defined_types: struct command { char *command; int (*ptr)(); char *usage; char *help; int min; }


decompiled_code:
int __fastcall print_help(const char **a1)
{
fprintf(stderr, "Name: %s\n", *a1);
print_usage(a1);
return fprintf(stderr, "Help: %s\n", a1[3]);
}

canonical_decompiled_code:
int __fastcall func2(const char **a1)
{
fprintf(stderr, "Name: %s\n", *a1);
func1(a1);
return fprintf(stderr, "Help: %s\n", a1[3]);
}

original_code:
void print_help(struct command* command)
{
 fprintf(stderr, "Name: %s\n", command->command);
 print_usage(command);
 fprintf(stderr, "Help: %s\n", command->help);
}

canonical_original_code:
void print_help(struct command *command)
{
 fprintf(stderr, "Name: %s\n", command->command);
 print_usage(command);
 fprintf(stderr, "Help: %s\n", command->help);
}


decompiled_code:
int print_help_all()
{
const char **v0; // rbp
int v1; // ebx
int result; // eax

if ( cmd_table )
{
fprintf(stderr, "%-8s", cmd_table);
v0 = (const char **)(unsigned int)((_DWORD)&cmd_table + 40);
v1 = 1;
while ( *v0 )
{
fprintf(stderr, "%-8s", *v0);
++v1;
if ( v1 == 5 * (v1 / 5) )
putchar(10);
v0 += 5;
}
result = 5 * (v1 / 5);
if ( v1 != result )
result = putchar(10);
}
return result;
}

canonical_decompiled_code:
int func3()
{
const char **v0; // rbp
int v1; // ebx
int result; // eax

if ( cmd_table )
{
fprintf(stderr, "%-8s", cmd_table);
v0 = (const char **)(unsigned int)((_DWORD)&cmd_table + 40);
v1 = 1;
while ( *v0 )
{
fprintf(stderr, "%-8s", *v0);
++v1;
if ( v1 == 5 * (v1 / 5) )
putchar(10);
v0 += 5;
}
result = 5 * (v1 / 5);
if ( v1 != result )
result = putchar(10);
}
return result;
}

original_code:
void print_help_all(void)
{
 int i;
 int n = 5;
 for (i = 0; cmd_table[i].command != ((void *)0); i++) {
  fprintf(stderr, "%-8s", (cmd_table+i)->command);
  if ((i+1)%n == 0)
   printf("\n");
 }
 if ((i)%n != 0)
  printf("\n");
}

canonical_original_code:
void print_help_all()
{
 int i;
 int n = 5;
 for (i = 0; cmd_table[i].command != ((void *)0); i++) {
  fprintf(stderr, "%-8s", (cmd_table+i)->command);
  if ((i+1)%n == 0)
   printf("\n");
 }
 if ((i)%n != 0)
  printf("\n");
}

variable_types: n int


decompiled_code:
__int64 __fastcall execute_cmd(__int64 a1, char *a2, __int64 a3)
{
void *v4; // rbp
char **v5; // rax
char **v6; // rbx
unsigned int v7; // ebx

v4 = calloc(0x20uLL, 8uLL);
v5 = find_cmd(a2);
if ( !v5 )
return (unsigned int)-4;
v6 = v5;
if ( (unsigned int)parse_args(v5, a3, v4) )
{
v7 = ((__int64 (__fastcall *)(__int64, void *))v6[1])(a1, v4);
free(v4);
}
else
{
print_error("usage error");
print_usage((const char **)v6);
v7 = -1;
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, char *a2, __int64 a3)
{
void *v4; // rbp
char **v5; // rax
char **v6; // rbx
unsigned int v7; // ebx

v4 = calloc(0x20uLL, 8uLL);
v5 = func0(a2);
if ( !v5 )
return (unsigned int)-4;
v6 = v5;
if ( (unsigned int)parse_args(v5, a3, v4) )
{
v7 = ((__int64 (__fastcall *)(__int64, void *))v6[1])(a1, v4);
free(v4);
}
else
{
print_error("usage error");
func1((const char **)v6);
v7 = -1;
}
return v7;
}

original_code:
int execute_cmd(ARCH arch, char* cmd, char* str_arg)
{
 int res;
 struct command* command;
 char** args = (char**) calloc(32, sizeof(*args));
 if ((command = find_cmd(cmd)) == ((void *)0))
  return -4;
 if (!parse_args(command, str_arg, args)) {
  print_error("usage error");
  print_usage(command);
  return -1;
 }
 res = command->ptr(arch, args);
 free(args);
 return res;
}

canonical_original_code:
int execute_cmd(struct { unsigned int registers[36]; struct section_t sections[4]; enum state_t state; struct list_elt *breakpoints; struct Elf32_Sym *symbols; unsigned int symbols_num; char *symbols_names; } *arch, char *cmd, char *str_arg)
{
 int res;
 struct command *command;
 char **args = (char**) calloc(32, sizeof(*args));
 if ((command = find_cmd(cmd)) == ((void *)0))
  return -4;
 if (!parse_args(command, str_arg, args)) {
  print_error("usage error");
  print_usage(command);
  return -1;
 }
 res = command->ptr(arch, args);
 free(args);
 return res;
}

variable_types: res int

variable_types: struct command { char *command; int (*ptr)(); char *usage; char *help; int min; } *

variable_types:
args
char **


decompiled_code:
__int64 __fastcall parse_interpreter(__int64 a1)
{
__int64 result; // rax
unsigned int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = 1;
do
{
switch_return_code(a1, stdin, v2);
result = v2[0];
}
while ( v2[0] && v2[0] != 11 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
__int64 result; // rax
unsigned int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

v2[0] = 1;
do
{
func6(a1, stdin, v2);
result = v2[0];
}
while ( v2[0] && v2[0] != 11 );
return result;
}

original_code:
void parse_interpreter(ARCH arch)
{
 int res = 1;
 while (res != 0 && res != 11)
  switch_return_code(arch, stdin, &res);
}

canonical_original_code:
void parse_interpreter(struct { unsigned int registers[36]; struct section_t sections[4]; enum state_t state; struct list_elt *breakpoints; struct Elf32_Sym *symbols; unsigned int symbols_num; char *symbols_names; } *arch)
{
 int res = 1;
 while (res != 0 && res != 11)
  switch_return_code(arch, stdin, &res);
}

variable_types: struct { unsigned int registers[36]; struct section_t { char *name; unsigned char *data; unsigned int start_addr; unsigned int size; } sections[4]; enum state_t { NOT_LOADED=0, PAUSED=1, FINISHED=2 } state; struct list_elt { void *val; struct list_elt *next; } *breakpoints; struct Elf32_Sym { unsigned int st_name; unsigned int st_value; unsigned int st_size; unsigned char st_info; unsigned char st_other; unsigned short st_shndx; } *symbols; unsigned int symbols_num; char *symbols_names; } *


main

canonical_name: main

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

print_logo(argc, argv, envp);
print_info("System init");
v3 = init_simu();
init_desc_array();
if ( !v3 )
{
print_error("allocation error");
die(0LL);
}
if ( argc == 1 )
{
print_info("Interactive mode");
parse_interpreter(v3);
}
else
{
if ( argc != 2 )
{
usage(*argv);
exit(1);
}
print_info("Script mode");
parse_file(v3, (__int64)argv[1]);
}
print_info("System exit");
free_arch(v3);
exit(0);
}

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

print_logo(argc, argv, envp);
print_info("System init");
v3 = init_simu();
init_desc_array();
if ( !v3 )
{
print_error("allocation error");
die(0LL);
}
if ( argc == 1 )
{
print_info("Interactive mode");
func8(v3);
}
else
{
if ( argc != 2 )
{
usage(*argv);
exit(1);
}
print_info("Script mode");
func7(v3, (__int64)argv[1]);
}
print_info("System exit");
free_arch(v3);
exit(0);
}

original_code:
int main(int argc, char* argv[])
{
 ARCH arch = ((void *)0);
 print_logo();
 print_info("System init");
 arch = init_simu();
 init_desc_array();
 if (arch == ((void *)0)) {
  print_error("allocation error");
  die(arch);
 }
 switch(argc) {
  case 1:
   print_info("Interactive mode");
   parse_interpreter(arch);
   break;
  case 2:
   print_info("Script mode");
   parse_file(arch, argv[1]);
   break;
  default:
   usage(argv[0]);
   exit(1);
   break;
 }
 print_info("System exit");
 free_arch(arch);
 exit(0);
}

canonical_original_code:
int main(int argc, char *argv[])
{
 struct { unsigned int registers[36]; struct section_t sections[4]; enum state_t state; struct list_elt *breakpoints; struct Elf32_Sym *symbols; unsigned int symbols_num; char *symbols_names; } *arch = ((void *)0);
 print_logo();
 print_info("System init");
 arch = init_simu();
 init_desc_array();
 if (arch == ((void *)0)) {
  print_error("allocation error");
  die(arch);
 }
 switch(argc) {
  case 1:
   print_info("Interactive mode");
   parse_interpreter(arch);
   break;
  case 2:
   print_info("Script mode");
   parse_file(arch, argv[1]);
   break;
  default:
   usage(argv[0]);
   exit(1);
   break;
 }
 print_info("System exit");
 free_arch(arch);
 exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]


decompiled_code:
int __fastcall display_reg(__int64 a1, int a2)
{
return fprintf(stdout, "%4s : %08x ", (&REG_NAMES)[a2], *(unsigned int *)(a1 + 4LL * a2));
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1, int a2)
{
return fprintf(stdout, "%4s : %08x ", (&REG_NAMES)[a2], *(unsigned int *)(a1 + 4LL * a2));
}

original_code:
void display_reg(ARCH arch, int reg_index)
{
 char *reg_name = REG_NAMES[reg_index];
 uint reg_value = (arch->registers)[reg_index];
 fprintf(stdout, "%4s : %08x   ", reg_name, reg_value);
}

canonical_original_code:
void display_reg(struct { unsigned int registers[36]; struct section_t sections[4]; enum state_t state; struct list_elt *breakpoints; struct Elf32_Sym *symbols; unsigned int symbols_num; char *symbols_names; } *arch, int reg_index)
{
 char *reg_name = REG_NAMES[reg_index];
 unsigned int reg_value = (arch->registers)[reg_index];
 fprintf(stdout, "%4s : %08x   ", reg_name, reg_value);
}


decompiled_code:
int __fastcall display_reg_all(__int64 a1)
{
int v1; // ebx
int result; // eax

display_reg(a1, 0);
v1 = 1;
do
{
result = display_reg(a1, v1++);
if ( (v1 & 3) == 0 )
result = putchar(10);
}
while ( v1 != 36 );
return result;
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1)
{
int v1; // ebx
int result; // eax

func0(a1, 0);
v1 = 1;
do
{
result = func0(a1, v1++);
if ( (v1 & 3) == 0 )
result = putchar(10);
}
while ( v1 != 36 );
return result;
}

original_code:
void display_reg_all(ARCH arch)
{
 for (int i = 0; i < 36; i++) {
  display_reg(arch, i);
  if ((i+1) % 4 == 0)
   printf("\n");
 }
}

canonical_original_code:
void display_reg_all(struct { unsigned int registers[36]; struct section_t sections[4]; enum state_t state; struct list_elt *breakpoints; struct Elf32_Sym *symbols; unsigned int symbols_num; char *symbols_names; } *arch)
{
 for (int i = 0; i < 36; i++) {
  display_reg(arch, i);
  if ((i+1) % 4 == 0)
   printf("\n");
 }
}

variable_types: i int


matchJI

decompiled_code:
__int64 __fastcall matchJI(int a1)
{
_DWORD *v1; // rax
unsigned int v2; // edx

if ( DESC_ARRAY_LENGTH <= 0 )
return (unsigned int)-1;
v1 = (_DWORD *)(DESC_ARRAY + 12);
v2 = 0;
while ( *v1 != a1 )
{
++v2;
v1 += 12;
if ( v2 == DESC_ARRAY_LENGTH )
return (unsigned int)-1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
_DWORD *v1; // rax
unsigned int v2; // edx

if ( DESC_ARRAY_LENGTH <= 0 )
return (unsigned int)-1;
v1 = (_DWORD *)(DESC_ARRAY + 12);
v2 = 0;
while ( *v1 != a1 )
{
++v2;
v1 += 12;
if ( v2 == DESC_ARRAY_LENGTH )
return (unsigned int)-1;
}
return v2;
}

original_code:
int matchJI(uint opcode)
{
    for (int i = 0 ; i < DESC_ARRAY_LENGTH; i++) {
        if ((DESC_ARRAY+i)->opcode == opcode)
            return i;
    }
    return -1;
}

canonical_original_code:
int matchJI(unsigned int opcode)
{
    for (int i = 0; i < DESC_ARRAY_LENGTH; i++) {
        if ((DESC_ARRAY+i)->opcode == opcode)
            return i;
    }
    return -1;
}

variable_types:
opcode
unsigned int


matchR

decompiled_code:
__int64 __fastcall matchR(int a1)
{
_DWORD *v1; // rax
unsigned int v2; // edx

if ( DESC_ARRAY_LENGTH <= 0 )
return (unsigned int)-1;
v1 = (_DWORD *)(DESC_ARRAY + 16);
v2 = 0;
while ( *v1 != a1 )
{
++v2;
v1 += 12;
if ( v2 == DESC_ARRAY_LENGTH )
return (unsigned int)-1;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1)
{
_DWORD *v1; // rax
unsigned int v2; // edx

if ( DESC_ARRAY_LENGTH <= 0 )
return (unsigned int)-1;
v1 = (_DWORD *)(DESC_ARRAY + 16);
v2 = 0;
while ( *v1 != a1 )
{
++v2;
v1 += 12;
if ( v2 == DESC_ARRAY_LENGTH )
return (unsigned int)-1;
}
return v2;
}

original_code:
int matchR(uint function)
{
 for (int i = 0 ; i < DESC_ARRAY_LENGTH; i++) {
  if ((DESC_ARRAY+i)->function == function)
            return i;
    }
    return -1;
}

canonical_original_code:
int matchR(unsigned int function)
{
 for (int i = 0; i < DESC_ARRAY_LENGTH; i++) {
  if ((DESC_ARRAY+i)->function == function)
            return i;
    }
    return -1;
}

variable_types:
function
unsigned int


decompiled_code:
__int64 __fastcall get_instr(__int64 a1)
{
int v1; // eax
int v3; // ebp
int v4; // eax

v1 = parser_instr(a1, 26LL, 31LL);
if ( v1 )
return matchJI(v1);
v3 = parser_instr((unsigned int)a1, 0LL, 5LL);
v4 = parser_instr((unsigned int)a1, 21LL, 21LL);
if ( v3 != 2 )
return matchR(v3);
if ( v4 == 1 )
return get_desc_id("ROTR");
return get_desc_id("SRL");
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
int v1; // eax
int v3; // ebp
int v4; // eax

v1 = parser_instr(a1, 26LL, 31LL);
if ( v1 )
return func0(v1);
v3 = parser_instr((unsigned int)a1, 0LL, 5LL);
v4 = parser_instr((unsigned int)a1, 21LL, 21LL);
if ( v3 != 2 )
return func1(v3);
if ( v4 == 1 )
return get_desc_id("ROTR");
return get_desc_id("SRL");
}

original_code:
int get_instr(uint val)
{
    uint opcode, function;
    int c, R;
    opcode = parser_instr(val, 26, 31);
    if (opcode == 0) {
        function = parser_instr(val, 0, 5);
        R = parser_instr(val, 21, 21);
        c = matchR(function);
        if (function == 2) {
            if (R == 1)
                return get_desc_id("ROTR");
            else
                return get_desc_id("SRL");
        }
        if (c == -1)
            return -1;
    }
    else {
        c = matchJI(opcode);
        if (c == -1)
            return -1;
    }
    return c;
}

canonical_original_code:
int get_instr(unsigned int val)
{
    unsigned int opcode; unsigned int function;
    int c; int R;
    opcode = parser_instr(val, 26, 31);
    if (opcode == 0) {
        function = parser_instr(val, 0, 5);
        R = parser_instr(val, 21, 21);
        c = matchR(function);
        if (function == 2) {
            if (R == 1)
                return get_desc_id("ROTR");
            else
                return get_desc_id("SRL");
        }
        if (c == -1)
            return -1;
    }
    else {
        c = matchJI(opcode);
        if (c == -1)
            return -1;
    }
    return c;
}

variable_types:
val
unsigned int

variable_types: c int

variable_types: R int


decompiled_code:
__int64 __fastcall get_section(__int64 a1, unsigned int a2)
{
unsigned int *v2; // rdi
__int64 result; // rax

v2 = (unsigned int *)(a1 + 160);
result = 0LL;
while ( *v2 > a2 || v2[1] + *v2 <= a2 )
{
result = (unsigned int)(result + 1);
v2 += 6;
if ( (_DWORD)result == 3 )
return 0xFFFFFFFFLL;
}
return result;
}

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

v2 = (unsigned int *)(a1 + 160);
result = 0LL;
while ( *v2 > a2 || v2[1] + *v2 <= a2 )
{
result = (unsigned int)(result + 1);
v2 += 6;
if ( (_DWORD)result == 3 )
return 0xFFFFFFFFLL;
}
return result;
}

original_code:
int get_section(ARCH arch, uint addr)
{
 section_t* section_ptr;
 for (int i = 0; i < 3; i++) {
  section_ptr = &(arch->sections[i]);
  if ( addr >= section_ptr->start_addr
   && addr < section_ptr->start_addr + section_ptr->size)
   return i;
 }
 return -1;
}

canonical_original_code:
int get_section(struct { unsigned int registers[36]; struct section_t sections[4]; enum state_t state; struct list_elt *breakpoints; struct Elf32_Sym *symbols; unsigned int symbols_num; char *symbols_names; } *arch, unsigned int addr)
{
 struct section_t *section_ptr;
 for (int i = 0; i < 3; i++) {
  section_ptr = &(arch->sections[i]);
  if ( addr >= section_ptr->start_addr
   && addr < section_ptr->start_addr + section_ptr->size)
   return i;
 }
 return -1;
}

variable_types:
addr
unsigned int

variable_types: struct section_t { char *name; unsigned char *data; unsigned int start_addr; unsigned int size; } *


decompiled_code:
__int64 __fastcall get_offset(__int64 a1, int a2, int a3)
{
return (unsigned int)(a2 - *(_DWORD *)(a1 + 24LL * a3 + 160));
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2, int a3)
{
return (unsigned int)(a2 - *(_DWORD *)(a1 + 24LL * a3 + 160));
}

original_code:
int get_offset(ARCH arch, uint addr, int section_index)
{
 return addr - (arch->sections)[section_index].start_addr;
}

canonical_original_code:
int get_offset(struct { unsigned int registers[36]; struct section_t sections[4]; enum state_t state; struct list_elt *breakpoints; struct Elf32_Sym *symbols; unsigned int symbols_num; char *symbols_names; } *arch, unsigned int addr, int section_index)
{
 return addr - (arch->sections)[section_index].start_addr;
}


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

result = get_instr(a2);
if ( (_DWORD)result != -1 )
{
(*(void (__fastcall **)(__int64, _QWORD))(DESC_ARRAY + 48LL * (int)result + 32))(a1, a2);
result = 1LL;
}
return result;
}

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

result = get_instr(a2);
if ( (_DWORD)result != -1 )
{
(*(void (__fastcall **)(__int64, _QWORD))(DESC_ARRAY + 48LL * (int)result + 32))(a1, a2);
result = 1LL;
}
return result;
}

original_code:
int execute_instruction(ARCH arch, uint word)
{
    int c;
    if ((c = get_instr(word)) == -1)
        return -1;
    DESC_ARRAY[c].execute(arch, word);
    return 1;
}

canonical_original_code:
int execute_instruction(struct { unsigned int registers[36]; struct section_t sections[4]; enum state_t state; struct list_elt *breakpoints; struct Elf32_Sym *symbols; unsigned int symbols_num; char *symbols_names; } *arch, unsigned int word)
{
    int c;
    if ((c = get_instr(word)) == -1)
        return -1;
    DESC_ARRAY[c].execute(arch, word);
    return 1;
}

variable_types:
word
unsigned int


run

decompiled_code:
__int64 __fastcall run(__int64 a1, int a2)
{
unsigned int v2; // ebp
unsigned int v3; // eax
unsigned int v4; // ebp
int v5; // eax
unsigned int v6; // eax
unsigned int v7; // eax
int v8; // ebp
__int64 result; // rax

if ( *(_DWORD *)(a1 + 240) == 2 )
reset_registers();
do
{
v2 = get_register(a1, 35LL);
if ( v2 >= (unsigned int)get_section_end(a1, 0LL) )
break;
reset_register(a1, 34LL);
v3 = get_register(a1, 35LL);
v4 = get_instr_from_addr(a1, v3);
v5 = get_register(a1, 35LL);
set_register(a1, 35LL, (unsigned int)(v5 + 4));
display_instruction(a1, v4, stdout);
execute_instruction(a1, v4);
v6 = get_register(a1, 35LL);
if ( (unsigned int)get_breakpoint_id(a1, v6) != -1 || (unsigned int)get_register_bit(a1, 34LL, 2LL) )
{
*(_DWORD *)(a1 + 240) = 1;
v7 = get_register(a1, 35LL);
del_breakpoint_by_addr(a1, v7);
break;
}
}
while ( !a2 && !(unsigned int)get_register_bit(a1, 34LL, 1LL) );
v8 = get_register(a1, 35LL);
result = get_section_end(a1, 0LL);
if ( v8 == (_DWORD)result )
*(_DWORD *)(a1 + 240) = 2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, int a2)
{
unsigned int v2; // ebp
unsigned int v3; // eax
unsigned int v4; // ebp
int v5; // eax
unsigned int v6; // eax
unsigned int v7; // eax
int v8; // ebp
__int64 result; // rax

if ( *(_DWORD *)(a1 + 240) == 2 )
reset_registers();
do
{
v2 = get_register(a1, 35LL);
if ( v2 >= (unsigned int)get_section_end(a1, 0LL) )
break;
reset_register(a1, 34LL);
v3 = get_register(a1, 35LL);
v4 = get_instr_from_addr(a1, v3);
v5 = get_register(a1, 35LL);
set_register(a1, 35LL, (unsigned int)(v5 + 4));
display_instruction(a1, v4, stdout);
execute_instruction(a1, v4);
v6 = get_register(a1, 35LL);
if ( (unsigned int)get_breakpoint_id(a1, v6) != -1 || (unsigned int)get_register_bit(a1, 34LL, 2LL) )
{
*(_DWORD *)(a1 + 240) = 1;
v7 = get_register(a1, 35LL);
del_breakpoint_by_addr(a1, v7);
break;
}
}
while ( !a2 && !(unsigned int)get_register_bit(a1, 34LL, 1LL) );
v8 = get_register(a1, 35LL);
result = get_section_end(a1, 0LL);
if ( v8 == (_DWORD)result )
*(_DWORD *)(a1 + 240) = 2;
return result;
}

original_code:
void run(ARCH arch, int flag)
{
 ;
 INSTR instr;
 if (arch->state == FINISHED)
  reset_registers(arch);
 while (get_register(arch, 35) < get_section_end(arch, 0)) {
  reset_register(arch, 34);
  instr = get_instr_from_addr(arch, get_register(arch, 35));
  set_register(arch, 35, get_register(arch, 35) + 4);
  display_instruction(arch, instr.word, stdout);
  execute_instruction(arch, instr.word);
  if ((get_breakpoint_id(arch, get_register(arch, 35)) != -1)
    || (get_register_bit(arch, 34, 2))) {
   arch->state = PAUSED;
   ;
   del_breakpoint_by_addr(arch, get_register(arch, 35));
   break;
  }
  if (flag)
   break;
  if (get_register_bit(arch, 34, 1))
   break;
 }
 if (get_register(arch, 35) == get_section_end(arch, 0)) {
  ;
  arch->state = FINISHED;
 }
 ;
}

canonical_original_code:
void run(struct { unsigned int registers[36]; struct section_t sections[4]; enum state_t state; struct list_elt *breakpoints; struct Elf32_Sym *symbols; unsigned int symbols_num; char *symbols_names; } *arch, int flag)
{
 ;
 union INSTR instr;
 if (arch->state == FINISHED)
  reset_registers(arch);
 while (get_register(arch, 35) < get_section_end(arch, 0)) {
  reset_register(arch, 34);
  instr = get_instr_from_addr(arch, get_register(arch, 35));
  set_register(arch, 35, get_register(arch, 35) + 4);
  display_instruction(arch, instr.word, stdout);
  execute_instruction(arch, instr.word);
  if ((get_breakpoint_id(arch, get_register(arch, 35)) != -1)
    || (get_register_bit(arch, 34, 2))) {
   arch->state = PAUSED;
   ;
   del_breakpoint_by_addr(arch, get_register(arch, 35));
   break;
  }
  if (flag)
   break;
  if (get_register_bit(arch, 34, 1))
   break;
 }
 if (get_register(arch, 35) == get_section_end(arch, 0)) {
  ;
  arch->state = FINISHED;
 }
 ;
}

variable_types: flag int

variable_types: union INSTR { unsigned int word; unsigned char bytes[4]; }


foo

repo: jedbrown/pami

decompiled_code:
void foo()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code: void foo(){;}

canonical_original_code: void foo(){;}


threadCtrl

repo: jedivind/gtthreads

decompiled_code:
void __fastcall threadCtrl(__int64 (__fastcall *a1)(__int64), __int64 a2)
{
char *v2; // rbx
__int64 v3; // rdx

v2 = (char *)&threadList[250 * currentThread];
*(_DWORD *)v2 = 1;
*((_QWORD *)v2 + 124) = a1(a2);
v3 = 250LL * currentThread;
if ( !threadList[v3 + 2] )
{
threadList[250 * currentThread + 1] = 1;
threadList[v3] = 0;
}
setcontext(&scheduler_context);
}

canonical_decompiled_code:
void __fastcall func0(__int64 (__fastcall *a1)(__int64), __int64 a2)
{
char *v2; // rbx
__int64 v3; // rdx

v2 = (char *)&threadList[250 * currentThread];
*(_DWORD *)v2 = 1;
*((_QWORD *)v2 + 124) = a1(a2);
v3 = 250LL * currentThread;
if ( !threadList[v3 + 2] )
{
threadList[250 * currentThread + 1] = 1;
threadList[v3] = 0;
}
setcontext(&scheduler_context);
}

original_code:
void threadCtrl( void *func(), void *arg ) {
 threadList[currentThread].active = true;
 threadList[currentThread].returnval = func(arg);
 if( !threadList[currentThread].exited ){
  threadList[currentThread].finished = true;
  threadList[currentThread].active = false;
 }
 setcontext( &scheduler_context );
 return;
}

canonical_original_code:
void threadCtrl(void *func(), void *arg) {
 threadList[currentThread].active = true;
 threadList[currentThread].returnval = func(arg);
 if( !threadList[currentThread].exited ){
  threadList[currentThread].finished = true;
  threadList[currentThread].active = false;
 }
 setcontext( &scheduler_context );
 return;
}

variable_types:
func
void *()

variable_types:
arg
void *


scheduler

decompiled_code:
void scheduler(void)
{
__int64 v0; // rdx

while ( !allthreadsfinished )
{
if ( numThreadsCreated )
{
atomic_increment(&currentThread);
atomic_modulus(&currentThread, &numThreadsCreated);
}
if ( !activeThreadCount )
allthreadsfinished = 1;
if ( threadList[250 * currentThread + 1] || (v0 = 250LL * currentThread, threadList[v0 + 2]) )
gtthread_cancel(*(_QWORD *)&threadList[250 * currentThread + 4]);
else
swapcontext(&scheduler_context, (const ucontext_t *)&threadList[v0 + 6]);
}
}

canonical_decompiled_code:
void func1(void)
{
__int64 v0; // rdx

while ( !allthreadsfinished )
{
if ( numThreadsCreated )
{
atomic_increment(&currentThread);
atomic_modulus(&currentThread, &numThreadsCreated);
}
if ( !activeThreadCount )
allthreadsfinished = 1;
if ( threadList[250 * currentThread + 1] || (v0 = 250LL * currentThread, threadList[v0 + 2]) )
gtthread_cancel(*(_QWORD *)&threadList[250 * currentThread + 4]);
else
swapcontext(&scheduler_context, (const ucontext_t *)&threadList[v0 + 6]);
}
}

original_code:
void scheduler() {
 while( !allthreadsfinished ){
  if( numThreadsCreated != 0 ){
     atomic_increment( &currentThread );
     atomic_modulus( &currentThread, &numThreadsCreated );
  }
  if( activeThreadCount==0 )
   allthreadsfinished = true;
  if( !threadList[currentThread].finished && !threadList[currentThread].exited ){
   swapcontext( &scheduler_context, &threadList[currentThread].context );
  }
  else{
   gtthread_cancel(threadList[currentThread].threadid);
  }
 }
}


decompiled_code:
_DWORD *init_thread_structs()
{
_DWORD *result; // rax

result = threadList;
do
{
*result = 0;
result[1] = 0;
result[3] = 0;
result[2] = 0;
*((_QWORD *)result + 2) = -1LL;
result += 250;
}
while ( result != (_DWORD *)(unsigned int)((_DWORD)threadList + 20000) );
return result;
}

canonical_decompiled_code:
_DWORD *func2()
{
_DWORD *result; // rax

result = threadList;
do
{
*result = 0;
result[1] = 0;
result[3] = 0;
result[2] = 0;
*((_QWORD *)result + 2) = -1LL;
result += 250;
}
while ( result != (_DWORD *)(unsigned int)((_DWORD)threadList + 20000) );
return result;
}

original_code:
void init_thread_structs(){
 int i;
 for( i=0; i<20; i++ ){
  threadList[i].active = false;
  threadList[i].finished = false;
  threadList[i].deleted = false;
  threadList[i].exited = false;
  threadList[i].threadid = -1;
 }
 return;
}


decompiled_code:
void make_sched_context()
{
if ( !getcontext(&scheduler_context) )
{
scheduler_context.uc_stack.ss_sp = malloc((size_t)&unk_10000);
scheduler_context.uc_stack.ss_size = (size_t)&unk_10000;
scheduler_context.uc_stack.ss_flags = 0;
scheduler_context.uc_link = 0LL;
makecontext(&scheduler_context, scheduler, 0);
}
}

canonical_decompiled_code:
void func3()
{
if ( !getcontext(&scheduler_context) )
{
scheduler_context.uc_stack.ss_sp = malloc((size_t)&unk_10000);
scheduler_context.uc_stack.ss_size = (size_t)&unk_10000;
scheduler_context.uc_stack.ss_flags = 0;
scheduler_context.uc_link = 0LL;
makecontext(&scheduler_context, scheduler, 0);
}
}

original_code:
void make_sched_context(){
 if( !getcontext(&scheduler_context) ){
  scheduler_context.uc_stack.ss_sp = malloc( 65536 );
  scheduler_context.uc_stack.ss_size = 65536;
  scheduler_context.uc_stack.ss_flags = 0;
  scheduler_context.uc_link = ((void *)0);
  makecontext( &scheduler_context, (void(*)(void)) scheduler, 0 );
 }
 return;
}

canonical_original_code:
void make_sched_context(){
 if( !getcontext(&scheduler_context) ){
  scheduler_context.uc_stack.ss_sp = malloc( 65536 );
  scheduler_context.uc_stack.ss_size = 65536;
  scheduler_context.uc_stack.ss_flags = 0;
  scheduler_context.uc_link = ((void *)0);
  makecontext( &scheduler_context, (void (*)()) scheduler, 0 );
 }
 return;
}


decompiled_code:
int make_main_context()
{
void *v0; // rax

LODWORD(v0) = getcontext(&main_context);
if ( !(_DWORD)v0 )
{
main_context.uc_link = 0LL;
v0 = malloc((size_t)&unk_10000);
main_context.uc_stack.ss_sp = v0;
main_context.uc_stack.ss_size = (size_t)&unk_10000;
main_context.uc_stack.ss_flags = 0;
}
if ( !main_context.uc_stack.ss_sp )
{
printf("Error: Could not allocate stack");
exit(-1);
}
return (int)v0;
}

canonical_decompiled_code:
int func4()
{
void *v0; // rax

LODWORD(v0) = getcontext(&main_context);
if ( !(_DWORD)v0 )
{
main_context.uc_link = 0LL;
v0 = malloc((size_t)&unk_10000);
main_context.uc_stack.ss_sp = v0;
main_context.uc_stack.ss_size = (size_t)&unk_10000;
main_context.uc_stack.ss_flags = 0;
}
if ( !main_context.uc_stack.ss_sp )
{
printf("Error: Could not allocate stack");
exit(-1);
}
return (int)v0;
}

original_code:
void make_main_context(){
 if( !getcontext( &main_context ) ){
  main_context.uc_link = ((void *)0);
  main_context.uc_stack.ss_sp = malloc(65536);
  main_context.uc_stack.ss_size = 65536;
  main_context.uc_stack.ss_flags = 0;
 }
 if( main_context.uc_stack.ss_sp == 0 ){
  printf( "Error: Could not allocate stack" );
  exit(-1);
 }
 return;
}


decompiled_code:
__int64 __fastcall generate_thread_id(_QWORD *a1)
{
unsigned __int64 v1; // rbx
__int64 result; // rax

do
{
*a1 = rand();
v1 = (unsigned int)((_DWORD)threadList + 16);
while ( 1 )
{
result = gtthread_equal(*(_QWORD *)v1, *a1);
if ( (_DWORD)result )
break;
unique_0 = 1;
v1 += 1000LL;
if ( v1 == (_DWORD)threadList + 20016 )
return result;
}
}
while ( !unique_0 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(_QWORD *a1)
{
unsigned __int64 v1; // rbx
__int64 result; // rax

do
{
*a1 = rand();
v1 = (unsigned int)((_DWORD)threadList + 16);
while ( 1 )
{
result = gtthread_equal(*(_QWORD *)v1, *a1);
if ( (_DWORD)result )
break;
unique_0 = 1;
v1 += 1000LL;
if ( v1 == (_DWORD)threadList + 20016 )
return result;
}
}
while ( !unique_0 );
return result;
}

original_code:
void generate_thread_id( unsigned long int *thread ){
 int i;
 static bool unique = false;
 do{
  *thread = rand();
  for( i=0; i<20; i++ ){
   if( gtthread_equal(threadList[i].threadid, *thread ) ){
    break;
   }
   else unique = true;
  }
 }while(!unique);
 return;
}

canonical_original_code:
void generate_thread_id(unsigned long *thread){
 int i;
 static int unique = false;
 do{
  *thread = rand();
  for( i=0; i<20; i++ ){
   if( gtthread_equal(threadList[i].threadid, *thread ) ){
    break;
   }
   else unique = true;
  }
 }while(!unique);
 return;
}

variable_types:
thread
unsigned long *

variable_types: unique int


decompiled_code:
__int64 __fastcall make_thread_context(_QWORD *a1, __int64 a2, __int64 a3)
{
__int64 v3; // rbx
int v4; // edi
__int64 v5; // rax
__int64 result; // rax

generate_thread_id(a1);
getcontext((ucontext_t *)&threadList[250 * threadCreateIndex + 6]);
v3 = 250LL * threadCreateIndex;
*(_QWORD *)&threadList[v3 + 4] = *a1;
*(_QWORD *)&threadList[v3 + 8] = &scheduler_context;
*(_QWORD *)&threadList[v3 + 10] = malloc((size_t)&unk_10000);
v4 = threadCreateIndex;
v5 = 250LL * threadCreateIndex;
*(_QWORD *)&threadList[v5 + 14] = &unk_10000;
threadList[v5 + 12] = 0;
if ( *(_QWORD *)&threadList[v5 + 10] )
{
makecontext((ucontext_t *)&threadList[250 * v4 + 6], (void (*)(void))threadCtrl, 2, a2, a3);
result = 0LL;
}
else
{
printf("Error: Could not allocate stack.");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD *a1, __int64 a2, __int64 a3)
{
__int64 v3; // rbx
int v4; // edi
__int64 v5; // rax
__int64 result; // rax

func5(a1);
getcontext((ucontext_t *)&threadList[250 * threadCreateIndex + 6]);
v3 = 250LL * threadCreateIndex;
*(_QWORD *)&threadList[v3 + 4] = *a1;
*(_QWORD *)&threadList[v3 + 8] = &scheduler_context;
*(_QWORD *)&threadList[v3 + 10] = malloc((size_t)&unk_10000);
v4 = threadCreateIndex;
v5 = 250LL * threadCreateIndex;
*(_QWORD *)&threadList[v5 + 14] = &unk_10000;
threadList[v5 + 12] = 0;
if ( *(_QWORD *)&threadList[v5 + 10] )
{
makecontext((ucontext_t *)&threadList[250 * v4 + 6], (void (*)(void))threadCtrl, 2, a2, a3);
result = 0LL;
}
else
{
printf("Error: Could not allocate stack.");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int make_thread_context( unsigned long int *thread, void *(*start_routine)(void *), void *arg ){
 generate_thread_id( thread );
 getcontext( &threadList[threadCreateIndex].context );
 threadList[threadCreateIndex].threadid = *thread;
 threadList[threadCreateIndex].context.uc_link = &scheduler_context;
 threadList[threadCreateIndex].context.uc_stack.ss_sp = malloc(65536);
 threadList[threadCreateIndex].context.uc_stack.ss_size = 65536;
 threadList[threadCreateIndex].context.uc_stack.ss_flags = 0;
 if( threadList[threadCreateIndex].context.uc_stack.ss_sp == 0 ) {
  printf( "Error: Could not allocate stack.");
  return -1;
 }
 makecontext( &threadList[threadCreateIndex].context, (void(*)(void)) threadCtrl, 2, start_routine, arg );
 return 0;
}

canonical_original_code:
int make_thread_context(unsigned long *thread, void *(*start_routine)(void *), void *arg){
 generate_thread_id( thread );
 getcontext( &threadList[threadCreateIndex].context );
 threadList[threadCreateIndex].threadid = *thread;
 threadList[threadCreateIndex].context.uc_link = &scheduler_context;
 threadList[threadCreateIndex].context.uc_stack.ss_sp = malloc(65536);
 threadList[threadCreateIndex].context.uc_stack.ss_size = 65536;
 threadList[threadCreateIndex].context.uc_stack.ss_flags = 0;
 if( threadList[threadCreateIndex].context.uc_stack.ss_sp == 0 ) {
  printf( "Error: Could not allocate stack.");
  return -1;
 }
 makecontext( &threadList[threadCreateIndex].context, (void (*)()) threadCtrl, 2, start_routine, arg );
 return 0;
}


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

init_thread_structs();
make_sched_context();
init_itimerval(a1);
init_sched_preempt_handler();
make_main_context();
result = init_sigtimer();
initialized = 1;
return result;
}

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

init_thread_structs();
make_sched_context();
init_itimerval(a1);
init_sched_preempt_handler();
make_main_context();
result = init_sigtimer();
initialized = 1;
return result;
}

original_code:
void gtthread_init( long period ){
 init_thread_structs();
 make_sched_context();
 init_itimerval( period );
 init_sched_preempt_handler();
 make_main_context();
 init_sigtimer();
 initialized = true;
 return;
}

canonical_original_code:
void gtthread_init(long period){
 init_thread_structs();
 make_sched_context();
 init_itimerval( period );
 init_sched_preempt_handler();
 make_main_context();
 init_sigtimer();
 initialized = true;
 return;
}

variable_types: period long


decompiled_code:
__int64 gtthread_create()
{
unsigned int v0; // ebx

if ( !initialized )
{
printf("Error: gtthread_init() not called! Exiting.");
exit(-1);
}
if ( numThreadsCreated > 19 )
return (unsigned int)-1;
v0 = make_thread_context();
atomic_increment(&threadCreateIndex);
atomic_increment(&numThreadsCreated);
atomic_increment(&activeThreadCount);
return v0;
}

canonical_decompiled_code:
__int64 func1()
{
unsigned int v0; // ebx

if ( !initialized )
{
printf("Error: gtthread_init() not called! Exiting.");
exit(-1);
}
if ( numThreadsCreated > 19 )
return (unsigned int)-1;
v0 = make_thread_context();
atomic_increment(&threadCreateIndex);
atomic_increment(&numThreadsCreated);
atomic_increment(&activeThreadCount);
return v0;
}

original_code:
int gtthread_create( unsigned long int *thread, void *(*start_routine)(void *), void *arg ){
 if( !initialized ) {
  printf("Error: gtthread_init() not called! Exiting.");
  exit(-1);
 }
 if( numThreadsCreated >= 20 )
  return -1;
 int status = make_thread_context( thread, start_routine, arg );
 atomic_increment( &threadCreateIndex );
 atomic_increment( &numThreadsCreated );
 atomic_increment( &activeThreadCount );
 return status;
}

canonical_original_code:
int gtthread_create(unsigned long *thread, void *(*start_routine)(void *), void *arg){
 if( !initialized ) {
  printf("Error: gtthread_init() not called! Exiting.");
  exit(-1);
 }
 if( numThreadsCreated >= 20 )
  return -1;
 int status = make_thread_context( thread, start_routine, arg );
 atomic_increment( &threadCreateIndex );
 atomic_increment( &numThreadsCreated );
 atomic_increment( &activeThreadCount );
 return status;
}

variable_types: status int


decompiled_code:
int gtthread_yield()
{
return swapcontext((ucontext_t *)&threadList[125 * currentThread + 3], &scheduler_context);
}

canonical_decompiled_code:
int func2()
{
return swapcontext((ucontext_t *)&threadList[125 * currentThread + 3], &scheduler_context);
}

original_code:
void gtthread_yield( void ){
 swapcontext( &threadList[currentThread].context, &scheduler_context);
 return;
}

canonical_original_code:
void gtthread_yield(){
 swapcontext( &threadList[currentThread].context, &scheduler_context);
 return;
}


decompiled_code:
__int64 __fastcall gtthread_join(__int64 a1, _QWORD *a2)
{
unsigned __int64 v2; // rax
int v3; // ebx
__int64 v4; // rax
__int64 *v5; // rbp

if ( numThreadsCreated <= 0 )
{
v3 = 0;
}
else
{
v2 = (unsigned int)((_DWORD)threadList + 16);
v3 = 0;
while ( *(_QWORD *)v2 != a1 )
{
++v3;
v2 += 1000LL;
if ( v3 == numThreadsCreated )
{
v3 = numThreadsCreated;
break;
}
}
}
v4 = 125LL * v3;
if ( !LODWORD(threadList[v4 + 1]) )
{
v5 = &threadList[v4];
do
{
if ( *((_DWORD *)v5 + 1) )
break;
gtthread_yield();
}
while ( !*((_DWORD *)v5 + 2) );
}
if ( !a2 )
return 0xFFFFFFFFLL;
*a2 = threadList[125 * v3 + 124];
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, _QWORD *a2)
{
unsigned __int64 v2; // rax
int v3; // ebx
__int64 v4; // rax
__int64 *v5; // rbp

if ( numThreadsCreated <= 0 )
{
v3 = 0;
}
else
{
v2 = (unsigned int)((_DWORD)threadList + 16);
v3 = 0;
while ( *(_QWORD *)v2 != a1 )
{
++v3;
v2 += 1000LL;
if ( v3 == numThreadsCreated )
{
v3 = numThreadsCreated;
break;
}
}
}
v4 = 125LL * v3;
if ( !LODWORD(threadList[v4 + 1]) )
{
v5 = &threadList[v4];
do
{
if ( *((_DWORD *)v5 + 1) )
break;
func2();
}
while ( !*((_DWORD *)v5 + 2) );
}
if ( !a2 )
return 0xFFFFFFFFLL;
*a2 = threadList[125 * v3 + 124];
return 0LL;
}

original_code:
int gtthread_join( unsigned long int thread, void **status ){
 int i;
 for( i=0; i<numThreadsCreated; i++ )
  if(threadList[i].threadid == thread) break;
 while( !threadList[i].exited && !threadList[i].finished ){
  gtthread_yield();
 }
 if( ( threadList[i].exited || threadList[i].finished ) && status ){
  *status = (void *)threadList[i].returnval;
  return 0;
 }
return -1;
}

canonical_original_code:
int gtthread_join(unsigned long thread, void **status){
 int i;
 for( i=0; i<numThreadsCreated; i++ )
  if(threadList[i].threadid == thread) break;
 while( !threadList[i].exited && !threadList[i].finished ){
  gtthread_yield();
 }
 if( ( threadList[i].exited || threadList[i].finished ) && status ){
  *status = (void *)threadList[i].returnval;
  return 0;
 }
return -1;
}

variable_types:
thread
unsigned long

variable_types:
status
void **

variable_types: i int


decompiled_code:
_BOOL8 __fastcall gtthread_equal(__int64 a1, __int64 a2)
{
return a1 == a2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(__int64 a1, __int64 a2)
{
return a1 == a2;
}

original_code:
int gtthread_equal( unsigned long int t1, unsigned long int t2 ){
 if( t1 == t2 ) return 1;
 else return 0;
}

canonical_original_code:
int gtthread_equal(unsigned long t1, unsigned long t2){
 if( t1 == t2 ) return 1;
 else return 0;
}


decompiled_code:
__int64 __fastcall gtthread_cancel(__int64 a1)
{
__int64 *v1; // rbx
__int64 v2; // rbp

if ( numThreadsCreated <= 0 )
return 0LL;
v1 = threadList;
v2 = 0LL;
do
{
if ( gtthread_equal(v1[2], a1) && !*((_DWORD *)v1 + 3) )
{
free((void *)v1[5]);
*(_DWORD *)v1 = 0;
threadidarray[v2] = -1LL;
atomic_decrement(&activeThreadCount);
*((_DWORD *)v1 + 3) = 1;
}
++v2;
v1 += 125;
}
while ( numThreadsCreated > (int)v2 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
__int64 *v1; // rbx
__int64 v2; // rbp

if ( numThreadsCreated <= 0 )
return 0LL;
v1 = threadList;
v2 = 0LL;
do
{
if ( func4(v1[2], a1) && !*((_DWORD *)v1 + 3) )
{
free((void *)v1[5]);
*(_DWORD *)v1 = 0;
threadidarray[v2] = -1LL;
atomic_decrement(&activeThreadCount);
*((_DWORD *)v1 + 3) = 1;
}
++v2;
v1 += 125;
}
while ( numThreadsCreated > (int)v2 );
return 0LL;
}

original_code:
int gtthread_cancel( unsigned long int thread ){
 int i;
 for( i=0; i<numThreadsCreated; i++ ) {
  if( gtthread_equal( threadList[i].threadid, thread ) && !threadList[i].deleted ){
   free(threadList[i].context.uc_stack.ss_sp);
   threadList[i].active = false;
   threadidarray[i] = -1;
   atomic_decrement( &activeThreadCount );
   threadList[i].deleted = true;
  }
 }
 return 0;
}

canonical_original_code:
int gtthread_cancel(unsigned long thread){
 int i;
 for( i=0; i<numThreadsCreated; i++ ) {
  if( gtthread_equal( threadList[i].threadid, thread ) && !threadList[i].deleted ){
   free(threadList[i].context.uc_stack.ss_sp);
   threadList[i].active = false;
   threadidarray[i] = -1;
   atomic_decrement( &activeThreadCount );
   threadList[i].deleted = true;
  }
 }
 return 0;
}


decompiled_code:
int __fastcall gtthread_exit(__int64 a1)
{
__int64 v1; // rax

v1 = 125LL * currentThread;
threadList[v1 + 124] = a1;
LODWORD(threadList[v1 + 1]) = 1;
gtthread_cancel(threadList[v1 + 2]);
return setcontext(&scheduler_context);
}

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

v1 = 125LL * currentThread;
threadList[v1 + 124] = a1;
LODWORD(threadList[v1 + 1]) = 1;
func5(threadList[v1 + 2]);
return setcontext(&scheduler_context);
}

original_code:
void gtthread_exit( void *retval ){
  threadList[currentThread].returnval = retval;
  threadList[currentThread].exited = true;
  gtthread_cancel( threadList[currentThread].threadid );
 setcontext( &scheduler_context );
 return;
}

canonical_original_code:
void gtthread_exit(void *retval){
  threadList[currentThread].returnval = retval;
  threadList[currentThread].exited = true;
  gtthread_cancel( threadList[currentThread].threadid );
 setcontext( &scheduler_context );
 return;
}

variable_types:
retval
void *


decompiled_code:
__int64 gtthread_self()
{
return threadList[125 * currentThread + 2];
}

canonical_decompiled_code:
__int64 func7()
{
return threadList[125 * currentThread + 2];
}

original_code:
unsigned long int gtthread_self( void ){
 return threadList[currentThread].threadid;
}

canonical_original_code:
unsigned long gtthread_self(){
 return threadList[currentThread].threadid;
}

return_type: unsigned long


decompiled_code:
__int64 __fastcall gtthread_mutex_init(_QWORD *a1)
{
if ( *a1 == 1LL )
return 0xFFFFFFFFLL;
*a1 = 0LL;
a1[1] = -1LL;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1)
{
if ( *a1 == 1LL )
return 0xFFFFFFFFLL;
*a1 = 0LL;
a1[1] = -1LL;
return 0LL;
}

original_code:
int gtthread_mutex_init( gtthread_mutex_t *mutex ) {
 if( mutex->lock == 1 ) return -1;
 __asm__ __volatile__ (
       "mov $0x00, %%rax\n\t"
       "xchg %%rax, %0\n\t"
       "mov $0xFFFFFFFFFFFFFFFF, %%rax\n\t"
       "xchg %%rax, %1\n\t"
       :"=r" (mutex->lock), "=r" (mutex->owner)
       :
       :"%rax"
       );
 return 0;
}

canonical_original_code:
int gtthread_mutex_init(struct gtthread_mutex_t *mutex) {
 if( mutex->lock == 1 ) return -1;
 __asm__ __volatile__ (
       "mov $0x00, %%rax\n\t"
       "xchg %%rax, %0\n\t"
       "mov $0xFFFFFFFFFFFFFFFF, %%rax\n\t"
       "xchg %%rax, %1\n\t"
       :"=r" (mutex->lock), "=r" (mutex->owner)
       :
       :"%rax"
       );
 return 0;
}

variable_types: struct gtthread_mutex_t { long lock; unsigned long owner; } *


decompiled_code:
__int64 __fastcall gtthread_mutex_lock(_QWORD *a1)
{
if ( a1[1] == threadList[125 * currentThread + 2] )
return 0xFFFFFFFFLL;
if ( *a1 )
{
do
gtthread_yield();
while ( *a1 && a1[1] != threadList[125 * currentThread + 2] );
}
*a1 = 1LL;
a1[1] = 1LL;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1)
{
if ( a1[1] == threadList[125 * currentThread + 2] )
return 0xFFFFFFFFLL;
if ( *a1 )
{
do
gtthread_yield();
while ( *a1 && a1[1] != threadList[125 * currentThread + 2] );
}
*a1 = 1LL;
a1[1] = 1LL;
return 0LL;
}

original_code:
int gtthread_mutex_lock( gtthread_mutex_t *mutex ){
 if( ( mutex->owner ) == threadList[currentThread].threadid ){
  return -1;
 }
 while( mutex->lock !=0 && mutex->owner != threadList[currentThread].threadid )
 gtthread_yield();
 __asm__ __volatile__(
      "mov $0x01, %%rax\n\t"
      "xchg %%rax, %0\n\t"
      "mov %2, %%rax\n\t"
      "xchg %%rax, %1\n\t"
      :"=r" (mutex->lock), "=r" (mutex->owner)
      :"r" (threadList[currentThread].threadid)
      :"%rax"
      );
 return 0;
}

canonical_original_code:
int gtthread_mutex_lock(struct gtthread_mutex_t *mutex){
 if( ( mutex->owner ) == threadList[currentThread].threadid ){
  return -1;
 }
 while( mutex->lock !=0 && mutex->owner != threadList[currentThread].threadid )
 gtthread_yield();
 __asm__ __volatile__(
      "mov $0x01, %%rax\n\t"
      "xchg %%rax, %0\n\t"
      "mov %2, %%rax\n\t"
      "xchg %%rax, %1\n\t"
      :"=r" (mutex->lock), "=r" (mutex->owner)
      :"r" (threadList[currentThread].threadid)
      :"%rax"
      );
 return 0;
}

user_defined_types: struct gtthread_mutex_t { long lock; unsigned long owner; }


decompiled_code:
__int64 __fastcall gtthread_mutex_unlock(_QWORD *a1)
{
if ( *a1 != 1LL )
return 0xFFFFFFFFLL;
if ( a1[1] != threadList[125 * currentThread + 2] )
return 0xFFFFFFFFLL;
*a1 = 0LL;
a1[1] = -1LL;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(_QWORD *a1)
{
if ( *a1 != 1LL )
return 0xFFFFFFFFLL;
if ( a1[1] != threadList[125 * currentThread + 2] )
return 0xFFFFFFFFLL;
*a1 = 0LL;
a1[1] = -1LL;
return 0LL;
}

original_code:
int gtthread_mutex_unlock( gtthread_mutex_t *mutex ){
 if( mutex->lock == 1 && mutex->owner == threadList[currentThread].threadid ){
  __asm__ __volatile__ (
        "mov $0x00, %%rax\n\t"
        "xchg %%rax, %0\n\t"
        "mov $0xFFFFFFFFFFFFFFFF, %%rax\n\t"
        "xchg %%rax, %1\n\t"
        :"=r" (mutex->lock), "=r" (mutex->owner)
         :
         :"%rax"
        );
  return 0;
 }
 return -1;
}

canonical_original_code:
int gtthread_mutex_unlock(struct gtthread_mutex_t *mutex){
 if( mutex->lock == 1 && mutex->owner == threadList[currentThread].threadid ){
  __asm__ __volatile__ (
        "mov $0x00, %%rax\n\t"
        "xchg %%rax, %0\n\t"
        "mov $0xFFFFFFFFFFFFFFFF, %%rax\n\t"
        "xchg %%rax, %1\n\t"
        :"=r" (mutex->lock), "=r" (mutex->owner)
         :
         :"%rax"
        );
  return 0;
 }
 return -1;
}


sighandler

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

if ( a1 == 14 )
result = swapcontext((ucontext_t *)((char *)&threadList + 1000 * currentThread + 24), &scheduler_context);
return result;
}

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

if ( a1 == 14 )
result = swapcontext((ucontext_t *)((char *)&threadList + 1000 * currentThread + 24), &scheduler_context);
return result;
}

original_code:
void sighandler( int sig_nr, siginfo_t* info, void *old_context ) {
 if ( sig_nr == 14 )
  swapcontext( &threadList[currentThread].context, &scheduler_context );
 else return;
}

canonical_original_code:
void sighandler(int sig_nr, struct siginfo_t *info, void *old_context) {
 if ( sig_nr == 14 )
  swapcontext( &threadList[currentThread].context, &scheduler_context );
 else return;
}

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

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


decompiled_code:
int __fastcall gtthread_block_signal(int signo)
{
sigset_t v2; // [rsp+0h] [rbp-88h] BYREF

sigemptyset(&v2);
sigaddset(&v2, signo);
return sigprocmask(0, &v2, 0LL);
}

canonical_decompiled_code:
int __fastcall func1(int signo)
{
sigset_t v2; // [rsp+0h] [rbp-88h] BYREF

sigemptyset(&v2);
sigaddset(&v2, signo);
return sigprocmask(0, &v2, 0LL);
}

original_code:
void gtthread_block_signal( int signo ){
 sigset_t set;
 sigemptyset(&set);
 sigaddset(&set, signo);
 sigprocmask(0, &set, ((void *)0));
return;
}

canonical_original_code:
void gtthread_block_signal(int signo){
 struct __sigset_t set;
 sigemptyset(&set);
 sigaddset(&set, signo);
 sigprocmask(0, &set, ((void *)0));
return;
}

variable_types: signo int

variable_types: struct __sigset_t { unsigned long __val[-1]; }


decompiled_code:
int __fastcall gtthread_unblock_signal(int signo)
{
sigset_t v2; // [rsp+0h] [rbp-88h] BYREF

sigemptyset(&v2);
sigaddset(&v2, signo);
return sigprocmask(0, &v2, 0LL);
}

canonical_decompiled_code:
int __fastcall func2(int signo)
{
sigset_t v2; // [rsp+0h] [rbp-88h] BYREF

sigemptyset(&v2);
sigaddset(&v2, signo);
return sigprocmask(0, &v2, 0LL);
}

original_code:
void gtthread_unblock_signal( int signo ){
 sigset_t set;
 sigemptyset(&set);
 sigaddset(&set, signo);
 sigprocmask(0, &set, ((void *)0));
return;
}

canonical_original_code:
void gtthread_unblock_signal(int signo){
 struct __sigset_t set;
 sigemptyset(&set);
 sigaddset(&set, signo);
 sigprocmask(0, &set, ((void *)0));
return;
}

user_defined_types: struct __sigset_t { unsigned long __val[-1]; }


decompiled_code:
void __fastcall init_itimerval(__suseconds_t a1)
{
timequant.it_value.tv_sec = 0LL;
timequant.it_value.tv_usec = a1;
timequant.it_interval = (timeval)_mm_load_si128((const __m128i *)&timequant.it_value);
}

canonical_decompiled_code:
void __fastcall func3(__suseconds_t a1)
{
timequant.it_value.tv_sec = 0LL;
timequant.it_value.tv_usec = a1;
timequant.it_interval = (timeval)_mm_load_si128((const __m128i *)&timequant.it_value);
}

original_code:
void init_itimerval( long period ){
 timequant.it_value.tv_sec = 0;
 timequant.it_value.tv_usec = (long) period;
 timequant.it_interval = timequant.it_value;
}

canonical_original_code:
void init_itimerval(long period){
 timequant.it_value.tv_sec = 0;
 timequant.it_value.tv_usec = (long) period;
 timequant.it_interval = timequant.it_value;
}


decompiled_code:
int init_sigtimer()
{
int result; // eax

result = setitimer(ITIMER_REAL, &timequant, 0LL);
if ( result )
{
puts("\n\tAn error occurred while initializing timer. Please check the value of period to gtthread_init()");
exit(-1);
}
return result;
}

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

result = setitimer(ITIMER_REAL, &timequant, 0LL);
if ( result )
{
puts("\n\tAn error occurred while initializing timer. Please check the value of period to gtthread_init()");
exit(-1);
}
return result;
}

original_code:
void init_sigtimer(){
 if( setitimer( ITIMER_REAL, &timequant, ((void *)0) ) == 0) {
 }
 else {
  printf("\n\tAn error occurred while initializing timer. Please check the value of period to gtthread_init()\n");
  exit(-1);
 }
 return;
}


decompiled_code:
int init_sched_preempt_handler()
{
int result; // eax

scheduling_preemption_handler.__val[0] = (unsigned __int64)sighandler;
*((_DWORD *)&scheduling_preemption_handler + 34) = 268435460;
sigemptyset((sigset_t *)(unsigned int)((_DWORD)&scheduling_preemption_handler + 8));
result = sigaction(14, (const struct sigaction *)&scheduling_preemption_handler, 0LL);
if ( result == -1 )
{
puts("\nAn error occurred while initializing the signal handler for swapping to the scheduler context");
exit(-1);
}
return result;
}

canonical_decompiled_code:
int func5()
{
int result; // eax

scheduling_preemption_handler.__val[0] = (unsigned __int64)sighandler;
*((_DWORD *)&scheduling_preemption_handler + 34) = 268435460;
sigemptyset((sigset_t *)(unsigned int)((_DWORD)&scheduling_preemption_handler + 8));
result = sigaction(14, (const struct sigaction *)&scheduling_preemption_handler, 0LL);
if ( result == -1 )
{
puts("\nAn error occurred while initializing the signal handler for swapping to the scheduler context");
exit(-1);
}
return result;
}

original_code:
void init_sched_preempt_handler(){
 scheduling_preemption_handler.__sigaction_handler.sa_sigaction = sighandler;
 scheduling_preemption_handler.sa_flags = 0x10000000 | 4;
 sigemptyset( &scheduling_preemption_handler.sa_mask );
 if ( sigaction( 14, &scheduling_preemption_handler, ((void *)0) ) == -1 ) {
  printf("\nAn error occurred while initializing the signal handler for swapping to the scheduler context\n");
  exit(-1);
 }
 return;
}


repo: jeffboody/loax

decompiled_code:
char *__fastcall loax_eventkey_keystring(int *a1)
{
int v1; // eax
const char *v2; // rdx

v1 = *a1;
if ( (unsigned int)*a1 <= 0x7F )
return LOAX_KEYSTRING[v1];
v2 = "UP";
if ( v1 != 256 )
{
v2 = "DOWN";
if ( v1 != 257 )
{
v2 = "LEFT";
if ( v1 != 258 )
{
v2 = "RIGHT";
if ( v1 != 259 )
{
v2 = "HOME";
if ( v1 != 260 )
{
v2 = "END";
if ( v1 != 261 )
{
v2 = "PGUP";
if ( v1 != 262 )
{
v2 = "PGDOWN";
if ( v1 != 263 )
{
v2 = "INSERT";
if ( v1 != 264 )
v2 = "UNKNOWN";
}
}
}
}
}
}
}
}
return (char *)v2;
}

original_code:
const char* loax_eventkey_keystring(loax_eventkey_t* self)
{
 int k = self->keycode;
 if((k >= 0) && (k < 128))
 {
  return LOAX_KEYSTRING[k];
 }
 else if(k == 0x100)
 {
  return "UP";
 }
 else if(k == 0x101)
 {
  return "DOWN";
 }
 else if(k == 0x102)
 {
  return "LEFT";
 }
 else if(k == 0x103)
 {
  return "RIGHT";
 }
 else if(k == 0x104)
 {
  return "HOME";
 }
 else if(k == 0x105)
 {
  return "END";
 }
 else if(k == 0x106)
 {
  return "PGUP";
 }
 else if(k == 0x107)
 {
  return "PGDOWN";
 }
 else if(k == 0x108)
 {
  return "INSERT";
 }
 return LOAX_STRING_UNKNOWN;
}

canonical_original_code:
char *loax_eventkey_keystring(struct loax_eventkey_t *self)
{
 int k = self->keycode;
 if((k >= 0) && (k < 128))
 {
  return LOAX_KEYSTRING[k];
 }
 else if(k == 0x100)
 {
  return "UP";
 }
 else if(k == 0x101)
 {
  return "DOWN";
 }
 else if(k == 0x102)
 {
  return "LEFT";
 }
 else if(k == 0x103)
 {
  return "RIGHT";
 }
 else if(k == 0x104)
 {
  return "HOME";
 }
 else if(k == 0x105)
 {
  return "END";
 }
 else if(k == 0x106)
 {
  return "PGUP";
 }
 else if(k == 0x107)
 {
  return "PGDOWN";
 }
 else if(k == 0x108)
 {
  return "INSERT";
 }
 return LOAX_STRING_UNKNOWN;
}

variable_types: struct loax_eventkey_t { int keycode; int meta; } *

variable_types: k int

return_type: char *