decompiled_code:
__int64 __fastcall evg_opencl_program_free(__int64 a1)
{
list_free(*(_QWORD *)(a1 + 24));
if ( *(_QWORD *)(a1 + 16) )
elf_file_free();
evg_opencl_repo_remove_object(*(_QWORD *)(evg_emu + 8LL), a1);
return mhandle_free(a1, "opencl-program.c:63");
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
list_free(*(_QWORD *)(a1 + 24));
if ( *(_QWORD *)(a1 + 16) )
elf_file_free();
evg_opencl_repo_remove_object(*(_QWORD *)(evg_emu + 8LL), a1);
return mhandle_free(a1, "opencl-program.c:63");
}

original_code:
void evg_opencl_program_free(struct evg_opencl_program_t *program)
{
 list_free(program->constant_buffer_list);
 if (program->elf_file)
  elf_file_free(program->elf_file);
 evg_opencl_repo_remove_object(evg_emu->opencl_repo, program);
 (mhandle_free(program, "opencl-program.c" ":" "63"));
}

user_defined_types: struct elf_buffer_t { void *ptr; int size; int pos; }

user_defined_types: struct Elf32_Ehdr { unsigned char e_ident[-1]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned int e_entry; unsigned int e_phoff; unsigned int e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phentsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; }

user_defined_types: struct list_t { int count; int error_code; int size; int head; int tail; void **elem; }

user_defined_types: struct Elf32_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned int sh_flags; unsigned int sh_addr; unsigned int sh_offset; unsigned int sh_size; unsigned int sh_link; unsigned int sh_info; unsigned int sh_addralign; unsigned int sh_entsize; }


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

v1 = *(_QWORD *)(a1 + 16);
if ( !v1 )
_assert_fail("program->elf_file", "opencl-program.c", 0x4Du, "evg_opencl_program_build");
result = *(_QWORD *)(v1 + 24);
if ( *(_WORD *)(result + 18) != 1009 )
result = fatal(
"%s: invalid binary file.\n%s",
"evg_opencl_program_build",
"\tYour application tried to load a pre-compiled OpenCL kernel binary which\n"
"\tdoes not contain code in the Evergreen ISA. Please, check that the off-line\n"
"\tcompilation of your kernel targets this GPU architecture supported by\n"
"\tMulti2Sim.\n");
return result;
}

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

v1 = *(_QWORD *)(a1 + 16);
if ( !v1 )
_assert_fail("program->elf_file", "opencl-program.c", 0x4Du, "evg_opencl_program_build");
result = *(_QWORD *)(v1 + 24);
if ( *(_WORD *)(result + 18) != 1009 )
result = fatal(
"%s: invalid binary file.\n%s",
"evg_opencl_program_build",
"\tYour application tried to load a pre-compiled OpenCL kernel binary which\n"
"\tdoes not contain code in the Evergreen ISA. Please, check that the off-line\n"
"\tcompilation of your kernel targets this GPU architecture supported by\n"
"\tMulti2Sim.\n");
return result;
}

original_code:
void evg_opencl_program_build(struct evg_opencl_program_t *program)
{
 ((void) sizeof ((program->elf_file) ? 1 : 0), __extension__ ({ if (program->elf_file) ; else __assert_fail ("program->elf_file", "opencl-program.c", 77, __extension__ __PRETTY_FUNCTION__); }));
 if (program->elf_file->header->e_machine != 0x3f1)
  fatal("%s: invalid binary file.\n%s", __FUNCTION__,
   err_evg_opencl_evergreen_format);
}

variable_types: struct evg_opencl_program_t { unsigned int id; int ref_count; unsigned int device_id; unsigned int context_id; struct elf_file_t { char *path; struct elf_buffer_t { void *ptr; int size; int pos; } buffer; struct Elf32_Ehdr { unsigned char e_ident[-1]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned int e_entry; unsigned int e_phoff; unsigned int e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phentsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; } *header; struct list_t { int count; int error_code; int size; int head; int tail; void **elem; } *section_list; struct elf_section_t { char *name; struct Elf32_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned int sh_flags; unsigned int sh_addr; unsigned int sh_offset; unsigned int sh_size; unsigned int sh_link; unsigned int sh_info; unsigned int sh_addralign; unsigned int sh_entsize; } *header; struct elf_buffer_t buffer; } *string_table; struct list_t *program_header_list; struct list_t *symbol_table; } *elf_file; struct list_t *constant_buffer_list; } *

return_type: void

user_defined_types: struct evg_opencl_program_t { unsigned int id; int ref_count; unsigned int device_id; unsigned int context_id; struct elf_file_t *elf_file; struct list_t *constant_buffer_list; }

user_defined_types: struct elf_file_t { char *path; struct elf_buffer_t buffer; struct Elf32_Ehdr *header; struct list_t *section_list; struct elf_section_t *string_table; struct list_t *program_header_list; struct list_t *symbol_table; }

user_defined_types: struct elf_section_t { char *name; struct Elf32_Shdr *header; struct elf_buffer_t buffer; }


decompiled_code:
__int64 __fastcall evg_opencl_program_read_symbol(__int64 a1, const char *a2, _QWORD *a3)
{
__int64 v3; // r13
__int64 v5; // rbx
__int64 v6; // rbp
__int64 result; // rax

v3 = *(_QWORD *)(a1 + 16);
if ( !v3 )
_assert_fail("elf_file", "opencl-program.c", 0x6Eu, "evg_opencl_program_read_symbol");
v5 = elf_symbol_get_by_name(*(_QWORD *)(a1 + 16));
if ( !v5 )
fatal(
"%s: ELF symbol '%s' not found.\n%s",
"evg_opencl_program_read_symbol",
a2,
"\tThe ELF file analyzer is trying to find a name in the ELF symbol table.\n"
"\tIf it is not found, it probably means that your application is requesting\n"
"\texecution of a kernel function that is not present in the encoded binary.\n"
"\tPlease, check the parameters passed to the 'clCreateKernel' function in\n"
"\tyour application.\n"
"\tThis could be also a symptom of compiling an OpenCL kernel source on a\n"
"\tmachine with an installation of the APP SDK (using 'm2s-opencl-kc') but\n"
"\twith an incorrect or missing installation of the GPU driver. In this case,\n"
"\tthe tool will still compile the kernel into LLVM, but the ISA section will\n"
"\tbe missing in the kernel binary.\n");
v6 = list_get(*(_QWORD *)(v3 + 32), *(unsigned int *)(v5 + 16));
if ( !v6 )
_assert_fail("section", "opencl-program.c", 0x76u, "evg_opencl_program_read_symbol");
if ( (unsigned int)(*(_DWORD *)(v5 + 8) + *(_DWORD *)(v5 + 12)) > *(_DWORD *)(*(_QWORD *)(v6 + 8) + 20LL) )
fatal(
"%s: ELF symbol '%s' exceeds section '%s' boundaries.\n%s",
"evg_opencl_program_read_symbol",
*(const char **)v5,
*(const char **)v6,
"\tThe ELF file analyzer is trying to find a name in the ELF symbol table.\n"
"\tIf it is not found, it probably means that your application is requesting\n"
"\texecution of a kernel function that is not present in the encoded binary.\n"
"\tPlease, check the parameters passed to the 'clCreateKernel' function in\n"
"\tyour application.\n"
"\tThis could be also a symptom of compiling an OpenCL kernel source on a\n"
"\tmachine with an installation of the APP SDK (using 'm2s-opencl-kc') but\n"
"\twith an incorrect or missing installation of the GPU driver. In this case,\n"
"\tthe tool will still compile the kernel into LLVM, but the ISA section will\n"
"\tbe missing in the kernel binary.\n");
*a3 = *(_QWORD *)(v6 + 16) + *(unsigned int *)(v5 + 8);
result = *(unsigned int *)(v5 + 12);
a3[1] = (unsigned int)result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, const char *a2, _QWORD *a3)
{
__int64 v3; // r13
__int64 v5; // rbx
__int64 v6; // rbp
__int64 result; // rax

v3 = *(_QWORD *)(a1 + 16);
if ( !v3 )
_assert_fail("elf_file", "opencl-program.c", 0x6Eu, "evg_opencl_program_read_symbol");
v5 = elf_symbol_get_by_name(*(_QWORD *)(a1 + 16));
if ( !v5 )
fatal(
"%s: ELF symbol '%s' not found.\n%s",
"evg_opencl_program_read_symbol",
a2,
"\tThe ELF file analyzer is trying to find a name in the ELF symbol table.\n"
"\tIf it is not found, it probably means that your application is requesting\n"
"\texecution of a kernel function that is not present in the encoded binary.\n"
"\tPlease, check the parameters passed to the 'clCreateKernel' function in\n"
"\tyour application.\n"
"\tThis could be also a symptom of compiling an OpenCL kernel source on a\n"
"\tmachine with an installation of the APP SDK (using 'm2s-opencl-kc') but\n"
"\twith an incorrect or missing installation of the GPU driver. In this case,\n"
"\tthe tool will still compile the kernel into LLVM, but the ISA section will\n"
"\tbe missing in the kernel binary.\n");
v6 = list_get(*(_QWORD *)(v3 + 32), *(unsigned int *)(v5 + 16));
if ( !v6 )
_assert_fail("section", "opencl-program.c", 0x76u, "evg_opencl_program_read_symbol");
if ( (unsigned int)(*(_DWORD *)(v5 + 8) + *(_DWORD *)(v5 + 12)) > *(_DWORD *)(*(_QWORD *)(v6 + 8) + 20LL) )
fatal(
"%s: ELF symbol '%s' exceeds section '%s' boundaries.\n%s",
"evg_opencl_program_read_symbol",
*(const char **)v5,
*(const char **)v6,
"\tThe ELF file analyzer is trying to find a name in the ELF symbol table.\n"
"\tIf it is not found, it probably means that your application is requesting\n"
"\texecution of a kernel function that is not present in the encoded binary.\n"
"\tPlease, check the parameters passed to the 'clCreateKernel' function in\n"
"\tyour application.\n"
"\tThis could be also a symptom of compiling an OpenCL kernel source on a\n"
"\tmachine with an installation of the APP SDK (using 'm2s-opencl-kc') but\n"
"\twith an incorrect or missing installation of the GPU driver. In this case,\n"
"\tthe tool will still compile the kernel into LLVM, but the ISA section will\n"
"\tbe missing in the kernel binary.\n");
*a3 = *(_QWORD *)(v6 + 16) + *(unsigned int *)(v5 + 8);
result = *(unsigned int *)(v5 + 12);
a3[1] = (unsigned int)result;
return result;
}

original_code:
void evg_opencl_program_read_symbol(struct evg_opencl_program_t *program, char *symbol_name,
 struct elf_buffer_t *buffer)
{
 struct elf_file_t *elf_file;
 struct elf_symbol_t *symbol;
 struct elf_section_t *section;
 elf_file = program->elf_file;
 ((void) sizeof ((elf_file) ? 1 : 0), __extension__ ({ if (elf_file) ; else __assert_fail ("elf_file", "opencl-program.c", 110, __extension__ __PRETTY_FUNCTION__); }));
 symbol = elf_symbol_get_by_name(elf_file, symbol_name);
 if (!symbol)
  fatal("%s: ELF symbol '%s' not found.\n%s", __FUNCTION__,
   symbol_name, err_evg_opencl_elf_symbol);
 section = list_get(elf_file->section_list, symbol->section);
 ((void) sizeof ((section) ? 1 : 0), __extension__ ({ if (section) ; else __assert_fail ("section", "opencl-program.c", 118, __extension__ __PRETTY_FUNCTION__); }));
 if (symbol->value + symbol->size > section->header->sh_size)
  fatal("%s: ELF symbol '%s' exceeds section '%s' boundaries.\n%s",
   __FUNCTION__, symbol->name, section->name, err_evg_opencl_elf_symbol);
 buffer->ptr = section->buffer.ptr + symbol->value;
 buffer->size = symbol->size;
 buffer->pos = 0;
}

variable_types:
buffer
struct elf_buffer_t *

variable_types: struct elf_file_t { char *path; struct elf_buffer_t { void *ptr; int size; int pos; } buffer; struct Elf32_Ehdr { unsigned char e_ident[-1]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned int e_entry; unsigned int e_phoff; unsigned int e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phentsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; } *header; struct list_t { int count; int error_code; int size; int head; int tail; void **elem; } *section_list; struct elf_section_t { char *name; struct Elf32_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned int sh_flags; unsigned int sh_addr; unsigned int sh_offset; unsigned int sh_size; unsigned int sh_link; unsigned int sh_info; unsigned int sh_addralign; unsigned int sh_entsize; } *header; struct elf_buffer_t buffer; } *string_table; struct list_t *program_header_list; struct list_t *symbol_table; } *

variable_types: struct elf_symbol_t { char *name; unsigned int value; unsigned int size; int section; unsigned char info; unsigned char visibility; } *

variable_types: struct elf_section_t { char *name; struct Elf32_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned int sh_flags; unsigned int sh_addr; unsigned int sh_offset; unsigned int sh_size; unsigned int sh_link; unsigned int sh_info; unsigned int sh_addralign; unsigned int sh_entsize; } *header; struct elf_buffer_t { void *ptr; int size; int pos; } buffer; } *


decompiled_code:
__int64 __fastcall evg_opencl_program_initialize_constant_buffers(__int64 a1)
{
__int64 v1; // r13
unsigned int i; // ebp
_DWORD *v3; // rbx
int v4; // eax
__int64 v5; // rcx
int v6; // edx
__int64 result; // rax
const char **v8; // rbx
char v9[208]; // [rsp+0h] [rbp-108h] BYREF
__int64 v10; // [rsp+D0h] [rbp-38h] BYREF
int v11; // [rsp+D8h] [rbp-30h]

v1 = *(_QWORD *)(a1 + 16);
for ( i = 2; i != 25; ++i )
{
sprintf(v9, "__OpenCL_%d_global", i);
result = elf_symbol_get_by_name(v1);
v8 = (const char **)result;
if ( !result )
break;
if ( evg_opencl_debug_category )
_debug(
evg_opencl_debug_category,
" constant buffer '%s' found with size %d\n",
*(const char **)result,
*(unsigned int *)(result + 12));
evg_opencl_program_read_symbol(a1, *v8, &v10);
v3 = (_DWORD *)evg_opencl_mem_create();
v3[2] = 0;
v4 = v11;
v3[3] = v11;
v3[10] = 0;
v3[11] = 0;
v5 = evg_emu;
v6 = *(_DWORD *)(evg_emu + 288LL);
v3[12] = v6;
*(_DWORD *)(v5 + 288) = v6 + v4;
mem_write(*(_QWORD *)(v5 + 280), (unsigned int)v3[12], (unsigned int)v3[3], v10);
result = list_set(*(_QWORD *)(a1 + 24), i, v3);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
__int64 v1; // r13
unsigned int i; // ebp
_DWORD *v3; // rbx
int v4; // eax
__int64 v5; // rcx
int v6; // edx
__int64 result; // rax
const char **v8; // rbx
char v9[208]; // [rsp+0h] [rbp-108h] BYREF
__int64 v10; // [rsp+D0h] [rbp-38h] BYREF
int v11; // [rsp+D8h] [rbp-30h]

v1 = *(_QWORD *)(a1 + 16);
for ( i = 2; i != 25; ++i )
{
sprintf(v9, "__OpenCL_%d_global", i);
result = elf_symbol_get_by_name(v1);
v8 = (const char **)result;
if ( !result )
break;
if ( evg_opencl_debug_category )
_debug(
evg_opencl_debug_category,
" constant buffer '%s' found with size %d\n",
*(const char **)result,
*(unsigned int *)(result + 12));
func3(a1, *v8, &v10);
v3 = (_DWORD *)evg_opencl_mem_create();
v3[2] = 0;
v4 = v11;
v3[3] = v11;
v3[10] = 0;
v3[11] = 0;
v5 = evg_emu;
v6 = *(_DWORD *)(evg_emu + 288LL);
v3[12] = v6;
*(_DWORD *)(v5 + 288) = v6 + v4;
mem_write(*(_QWORD *)(v5 + 280), (unsigned int)v3[12], (unsigned int)v3[3], v10);
result = list_set(*(_QWORD *)(a1 + 24), i, v3);
}
return result;
}

original_code:
void evg_opencl_program_initialize_constant_buffers(struct evg_opencl_program_t *program)
{
 struct elf_file_t *elf_file;
 struct elf_symbol_t *elf_symbol;
 struct elf_buffer_t elf_buffer;
 struct evg_opencl_mem_t *mem;
 char symbol_name[200];
 int i;
 elf_file = program->elf_file;
 for (i = 2; i < 25; i++)
 {
  sprintf(symbol_name, "__OpenCL_%d_global", i);
  elf_symbol = elf_symbol_get_by_name(elf_file, symbol_name);
                if (elf_symbol == ((void *)0)) {
   break;
  }
  ((evg_opencl_debug_category) ? __debug((evg_opencl_debug_category), "  constant buffer '%s' found with size %d\n", elf_symbol->name, elf_symbol->size) : (void) 0);
  evg_opencl_program_read_symbol(program, elf_symbol->name, &elf_buffer);
  mem = evg_opencl_mem_create();
  mem->type = 0;
  mem->size = elf_buffer.size;
  mem->flags = 0;
  mem->host_ptr = 0;
  mem->device_ptr = evg_emu->global_mem_top;
  evg_emu->global_mem_top += mem->size;
  mem_write(evg_emu->global_mem, mem->device_ptr, mem->size, elf_buffer.ptr);
  list_set(program->constant_buffer_list, i, mem);
 }
}

variable_types: struct elf_buffer_t { void *ptr; int size; int pos; }

variable_types: struct evg_opencl_mem_t { unsigned int id; int ref_count; unsigned int type; unsigned int size; unsigned int height; unsigned int width; unsigned int depth; unsigned int num_pixels; unsigned int pixel_size; unsigned int num_channels_per_pixel; unsigned int flags; unsigned int host_ptr; unsigned int device_ptr; } *

variable_types: i int

user_defined_types: struct elf_symbol_t { char *name; unsigned int value; unsigned int size; int section; unsigned char info; unsigned char visibility; }


decompiled_code:
void evg_periodic_report_init()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void evg_periodic_report_init(void)
{
}

canonical_original_code:
void evg_periodic_report_init()
{
}


decompiled_code:
__int64 evg_periodic_report_done()
{
return str_free(evg_periodic_report_file_name);
}

canonical_decompiled_code:
__int64 func1()
{
return str_free(evg_periodic_report_file_name);
}

original_code:
void evg_periodic_report_done(void)
{
 str_free(evg_periodic_report_file_name);
}

canonical_original_code:
void evg_periodic_report_done()
{
 str_free(evg_periodic_report_file_name);
}


decompiled_code:
__int64 evg_emu_done()
{
__int64 i; // rax

if ( evg_emu_report_file )
fclose(evg_emu_report_file);
for ( i = evg_emu; *(_DWORD *)(evg_emu + 48); i = evg_emu )
evg_ndrange_free(*(_QWORD *)(i + 32));
evg_opencl_repo_free_all_objects(*(_QWORD *)(i + 8));
evg_opencl_repo_free(*(_QWORD *)(evg_emu + 8));
evg_disasm_done();
evg_isa_done();
mem_free(*(_QWORD *)(evg_emu + 128));
mem_free(*(_QWORD *)(evg_emu + 280));
m2s_timer_free(*(_QWORD *)evg_emu);
return mhandle_free(evg_emu, "emu.c:118");
}

canonical_decompiled_code:
__int64 func1()
{
__int64 i; // rax

if ( evg_emu_report_file )
fclose(evg_emu_report_file);
for ( i = evg_emu; *(_DWORD *)(evg_emu + 48); i = evg_emu )
evg_ndrange_free(*(_QWORD *)(i + 32));
evg_opencl_repo_free_all_objects(*(_QWORD *)(i + 8));
evg_opencl_repo_free(*(_QWORD *)(evg_emu + 8));
evg_disasm_done();
evg_isa_done();
mem_free(*(_QWORD *)(evg_emu + 128));
mem_free(*(_QWORD *)(evg_emu + 280));
m2s_timer_free(*(_QWORD *)evg_emu);
return mhandle_free(evg_emu, "emu.c:118");
}

original_code:
void evg_emu_done()
{
 if (evg_emu_report_file)
  fclose(evg_emu_report_file);
 while (evg_emu->ndrange_list_count)
  evg_ndrange_free(evg_emu->ndrange_list_head);
 evg_opencl_repo_free_all_objects(evg_emu->opencl_repo);
 evg_opencl_repo_free(evg_emu->opencl_repo);
 evg_disasm_done();
 evg_isa_done();
 mem_free(evg_emu->const_mem);
 mem_free(evg_emu->global_mem);
 m2s_timer_free(evg_emu->timer);
 (mhandle_free(evg_emu, "emu.c" ":" "118"));
}


decompiled_code:
__int64 __fastcall evg_emu_libopencl_failed(unsigned int a1)
{
return warning(
"context %d finished after failing to access OpenCL library.\n"
"\tMulti2Sim has detected several attempts to access 'libm2s-opencl.so' by your\n"
"\tapplication's dynamic linker. Please, make sure that this file is available\n"
"\teither in any shared library path, in the current working directory, or in\n"
"\tany directory pointed by the environment variable LD_LIBRARY_PATH. See the\n"
"\tMulti2Sim Guide for further details (www.multi2sim.org).\n",
a1);
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1)
{
return warning(
"context %d finished after failing to access OpenCL library.\n"
"\tMulti2Sim has detected several attempts to access 'libm2s-opencl.so' by your\n"
"\tapplication's dynamic linker. Please, make sure that this file is available\n"
"\teither in any shared library path, in the current working directory, or in\n"
"\tany directory pointed by the environment variable LD_LIBRARY_PATH. See the\n"
"\tMulti2Sim Guide for further details (www.multi2sim.org).\n",
a1);
}

original_code:
void evg_emu_libopencl_failed(int pid)
{
 warning("context %d finished after failing to access OpenCL library.\n"
  "\tMulti2Sim has detected several attempts to access 'libm2s-opencl.so' by your\n"
  "\tapplication's dynamic linker. Please, make sure that this file is available\n"
  "\teither in any shared library path, in the current working directory, or in\n"
  "\tany directory pointed by the environment variable LD_LIBRARY_PATH. See the\n"
  "\tMulti2Sim Guide for further details (www.multi2sim.org).\n",
  pid);
}

variable_types: pid int


decompiled_code:
void __fastcall __noreturn evg_emu_disasm(__int64 a1)
{
__int64 v1; // r13
int i; // ebp
const char **v3; // rbx
__int64 v4; // r12
const char *v5; // r14
int v6; // eax
__int64 v7; // rbx
char v8[248]; // [rsp+0h] [rbp-F8h] BYREF

evg_disasm_init();
v1 = elf_file_create_from_path(a1);
for ( i = 0; (int)list_count(*(_QWORD *)(v1 + 56)) > i; ++i )
{
v3 = (const char **)list_get(*(_QWORD *)(v1 + 56), (unsigned int)i);
v4 = list_get(*(_QWORD *)(v1 + 32), *((unsigned int *)v3 + 4));
if ( v4 && (unsigned int)str_prefix(*v3, "__OpenCL_") )
{
if ( (unsigned int)str_suffix(*v3, "_kernel") )
{
v5 = *v3;
v6 = strlen(*v3);
str_substr(v8, 200LL, v5, 9LL, (unsigned int)(v6 - 16));
v7 = evg_bin_file_create(*(_QWORD *)(v4 + 16) + *((unsigned int *)v3 + 2), *((unsigned int *)v3 + 3), v8);
printf("**\n** Disassembly for '__kernel %s'\n**\n\n", v8);
evg_disasm_buffer(*(_QWORD *)(v7 + 16) + 40LL, stdout);
puts("\n\n");
evg_bin_file_free(v7);
}
}
}
elf_file_free(v1);
evg_disasm_done();
_mhandle_done();
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func5(__int64 a1)
{
__int64 v1; // r13
int i; // ebp
const char **v3; // rbx
__int64 v4; // r12
const char *v5; // r14
int v6; // eax
__int64 v7; // rbx
char v8[248]; // [rsp+0h] [rbp-F8h] BYREF

evg_disasm_init();
v1 = elf_file_create_from_path(a1);
for ( i = 0; (int)list_count(*(_QWORD *)(v1 + 56)) > i; ++i )
{
v3 = (const char **)list_get(*(_QWORD *)(v1 + 56), (unsigned int)i);
v4 = list_get(*(_QWORD *)(v1 + 32), *((unsigned int *)v3 + 4));
if ( v4 && (unsigned int)str_prefix(*v3, "__OpenCL_") )
{
if ( (unsigned int)str_suffix(*v3, "_kernel") )
{
v5 = *v3;
v6 = strlen(*v3);
str_substr(v8, 200LL, v5, 9LL, (unsigned int)(v6 - 16));
v7 = evg_bin_file_create(*(_QWORD *)(v4 + 16) + *((unsigned int *)v3 + 2), *((unsigned int *)v3 + 3), v8);
printf("**\n** Disassembly for '__kernel %s'\n**\n\n", v8);
evg_disasm_buffer(*(_QWORD *)(v7 + 16) + 40LL, stdout);
puts("\n\n");
evg_bin_file_free(v7);
}
}
}
elf_file_free(v1);
evg_disasm_done();
_mhandle_done();
exit(0);
}

original_code:
void evg_emu_disasm(char *path)
{
 struct elf_file_t *elf_file;
 struct elf_symbol_t *symbol;
 struct elf_section_t *section;
 struct evg_bin_file_t *amd_bin;
 char kernel_name[200];
 int i;
 evg_disasm_init();
 elf_file = elf_file_create_from_path(path);
 for (i = 0; i < list_count(elf_file->symbol_table); i++)
 {
  symbol = list_get(elf_file->symbol_table, i);
  section = list_get(elf_file->section_list, symbol->section);
  if (!section)
   continue;
  if (str_prefix(symbol->name, "__OpenCL_") && str_suffix(symbol->name, "_kernel"))
  {
   str_substr(kernel_name, sizeof(kernel_name), symbol->name, 9, strlen(symbol->name) - 16);
   amd_bin = evg_bin_file_create(section->buffer.ptr + symbol->value, symbol->size, kernel_name);
   printf("**\n** Disassembly for '__kernel %s'\n**\n\n", kernel_name);
   evg_disasm_buffer(&amd_bin->enc_dict_entry_evergreen->sec_text_buffer, stdout);
   printf("\n\n\n");
   evg_bin_file_free(amd_bin);
  }
 }
 elf_file_free(elf_file);
 evg_disasm_done();
 __mhandle_done();
 exit(0);
}

variable_types:
path
char *

variable_types: struct evg_bin_file_t { struct elf_file_t { char *path; struct elf_buffer_t { void *ptr; int size; int pos; } buffer; struct Elf32_Ehdr { unsigned char e_ident[-1]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned int e_entry; unsigned int e_phoff; unsigned int e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phentsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; } *header; struct list_t { int count; int error_code; int size; int head; int tail; void **elem; } *section_list; struct elf_section_t { char *name; struct Elf32_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned int sh_flags; unsigned int sh_addr; unsigned int sh_offset; unsigned int sh_size; unsigned int sh_link; unsigned int sh_info; unsigned int sh_addralign; unsigned int sh_entsize; } *header; struct elf_buffer_t buffer; } *string_table; struct list_t *program_header_list; struct list_t *symbol_table; } *elf_file; struct list_t *enc_dict; struct evg_bin_enc_dict_entry_t { struct evg_bin_enc_dict_entry_header_t { unsigned int d_machine; unsigned int d_type; unsigned int d_offset; unsigned int d_size; unsigned int d_flags; } *header; struct elf_buffer_t pt_load_buffer; struct elf_buffer_t pt_note_buffer; struct elf_buffer_t sec_text_buffer; struct elf_buffer_t sec_data_buffer; struct elf_buffer_t sec_symtab_buffer; struct elf_buffer_t sec_strtab_buffer; struct evg_bin_enc_dict_entry_consts_t { float float_consts[256][4]; unsigned int int_consts[32][4]; unsigned int bool_consts[32]; } *consts; int num_gpr_used; int lds_size_used; int stack_size_used; } *enc_dict_entry_evergreen; } *

user_defined_types: struct evg_bin_file_t { struct elf_file_t *elf_file; struct list_t *enc_dict; struct evg_bin_enc_dict_entry_t *enc_dict_entry_evergreen; }

user_defined_types: struct evg_bin_enc_dict_entry_t { struct evg_bin_enc_dict_entry_header_t *header; struct elf_buffer_t pt_load_buffer; struct elf_buffer_t pt_note_buffer; struct elf_buffer_t sec_text_buffer; struct elf_buffer_t sec_data_buffer; struct elf_buffer_t sec_symtab_buffer; struct elf_buffer_t sec_strtab_buffer; struct evg_bin_enc_dict_entry_consts_t *consts; int num_gpr_used; int lds_size_used; int stack_size_used; }


decompiled_code:
void __fastcall __noreturn evg_emu_opengl_disasm(const char *a1, int a2)
{
__int64 v2; // r12
__int64 v3; // rbx
unsigned int v4; // eax
int v5; // eax
unsigned int v6; // eax
__int64 v7; // r12
unsigned int v8[7]; // [rsp+Ch] [rbp-1Ch] BYREF

evg_disasm_init();
v2 = read_buffer(a1, v8);
if ( !v2 )
fatal("%s:Invalid file!", a1);
v3 = evg_opengl_bin_file_create(v2, v8[0], a1);
free_buffer(v2);
v4 = list_count(*(_QWORD *)(v3 + 8));
printf("This shader binary contains %d shaders\n\n", v4);
v5 = list_count(*(_QWORD *)(v3 + 8));
if ( a2 <= 0 || v5 < a2 )
{
v6 = list_count(*(_QWORD *)(v3 + 8));
fatal("Shader index out of range! Please choose <index> from 1 ~ %d", v6);
}
v7 = list_get(*(_QWORD *)(v3 + 8), (unsigned int)(a2 - 1));
printf("**\n** Disassembly for shader %d\n**\n\n", (unsigned int)a2);
evg_disasm_buffer(v7 + 24, stdout);
puts("\n\n");
evg_opengl_bin_file_free(v3);
evg_disasm_done();
_mhandle_done();
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func6(const char *a1, int a2)
{
__int64 v2; // r12
__int64 v3; // rbx
unsigned int v4; // eax
int v5; // eax
unsigned int v6; // eax
__int64 v7; // r12
unsigned int v8[7]; // [rsp+Ch] [rbp-1Ch] BYREF

evg_disasm_init();
v2 = read_buffer(a1, v8);
if ( !v2 )
fatal("%s:Invalid file!", a1);
v3 = evg_opengl_bin_file_create(v2, v8[0], a1);
free_buffer(v2);
v4 = list_count(*(_QWORD *)(v3 + 8));
printf("This shader binary contains %d shaders\n\n", v4);
v5 = list_count(*(_QWORD *)(v3 + 8));
if ( a2 <= 0 || v5 < a2 )
{
v6 = list_count(*(_QWORD *)(v3 + 8));
fatal("Shader index out of range! Please choose <index> from 1 ~ %d", v6);
}
v7 = list_get(*(_QWORD *)(v3 + 8), (unsigned int)(a2 - 1));
printf("**\n** Disassembly for shader %d\n**\n\n", (unsigned int)a2);
evg_disasm_buffer(v7 + 24, stdout);
puts("\n\n");
evg_opengl_bin_file_free(v3);
evg_disasm_done();
_mhandle_done();
exit(0);
}

original_code:
void evg_emu_opengl_disasm(char *path, int opengl_shader_index)
{
 void *file_buffer;
 int file_size;
 struct evg_opengl_bin_file_t *amd_opengl_bin;
 struct evg_opengl_shader_t *amd_opengl_shader;
 evg_disasm_init();
 file_buffer = read_buffer(path, &file_size);
 if(!file_buffer)
  fatal("%s:Invalid file!", path);
 amd_opengl_bin = evg_opengl_bin_file_create(file_buffer, file_size, path);
 free_buffer(file_buffer);
 printf("This shader binary contains %d shaders\n\n", list_count(amd_opengl_bin->shader_list));
 if (opengl_shader_index > list_count(amd_opengl_bin->shader_list) || opengl_shader_index <= 0 )
 {
  fatal("Shader index out of range! Please choose <index> from 1 ~ %d", list_count(amd_opengl_bin->shader_list));
 }
 amd_opengl_shader = list_get(amd_opengl_bin->shader_list, opengl_shader_index -1 );
 printf("**\n** Disassembly for shader %d\n**\n\n", opengl_shader_index);
 evg_disasm_buffer(&amd_opengl_shader->isa_buffer, stdout);
 printf("\n\n\n");
 evg_opengl_bin_file_free(amd_opengl_bin);
 evg_disasm_done();
 __mhandle_done();
 exit(0);
}

variable_types: struct evg_opengl_bin_file_t { char *name; struct list_t { int count; int error_code; int size; int head; int tail; void **elem; } *shader_list; } *

variable_types: struct evg_opengl_shader_t { enum evg_opengl_shader_kind_t { EVG_OPENGL_SHADER_VERTEX=0, EVG_OPENGL_SHADER_FRAGMENT=1, EVG_OPENGL_SHADER_GEOMETRY=2, EVG_OPENGL_SHADER_EVALUATION=3, EVG_OPENGL_SHADER_CONTROL=4 } shader_kind; struct elf_file_t { char *path; struct elf_buffer_t { void *ptr; int size; int pos; } buffer; struct Elf32_Ehdr { unsigned char e_ident[-1]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned int e_entry; unsigned int e_phoff; unsigned int e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phentsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; } *header; struct list_t { int count; int error_code; int size; int head; int tail; void **elem; } *section_list; struct elf_section_t { char *name; struct Elf32_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned int sh_flags; unsigned int sh_addr; unsigned int sh_offset; unsigned int sh_size; unsigned int sh_link; unsigned int sh_info; unsigned int sh_addralign; unsigned int sh_entsize; } *header; struct elf_buffer_t buffer; } *string_table; struct list_t *program_header_list; struct list_t *symbol_table; } *external_elf_file; struct elf_file_t *internal_elf_file; struct elf_buffer_t isa_buffer; } *

user_defined_types: struct evg_opengl_bin_file_t { char *name; struct list_t *shader_list; }

user_defined_types: struct evg_opengl_shader_t { enum evg_opengl_shader_kind_t shader_kind; struct elf_file_t *external_elf_file; struct elf_file_t *internal_elf_file; struct elf_buffer_t isa_buffer; }


decompiled_code:
__int64 si_uop_done()
{
return repos_free(gpu_uop_repos);
}

canonical_decompiled_code:
__int64 func1()
{
return repos_free(gpu_uop_repos);
}

original_code:
void si_uop_done()
{
 repos_free(gpu_uop_repos);
}


decompiled_code:
__int64 __fastcall x86_cpu_decode_core(unsigned int a1)
{
signed int v1; // ebx
__int64 result; // rax

if ( x86_cpu_num_threads > 0 )
{
v1 = 0;
do
result = x86_cpu_decode_thread(a1, v1++);
while ( x86_cpu_num_threads > v1 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
signed int v1; // ebx
__int64 result; // rax

if ( x86_cpu_num_threads > 0 )
{
v1 = 0;
do
result = func0(a1, v1++);
while ( x86_cpu_num_threads > v1 );
}
return result;
}

original_code:
static void x86_cpu_decode_core(int core)
{
 int thread;
 for (thread = 0; thread < x86_cpu_num_threads; thread++)
  x86_cpu_decode_thread(core, thread);
}

variable_types: core int

variable_types: thread int


decompiled_code:
__int64 x86_cpu_decode()
{
__int64 result; // rax
signed int v1; // ebx

result = x86_cpu;
*(_QWORD *)(x86_cpu + 32LL) = "decode";
if ( x86_cpu_num_cores > 0 )
{
v1 = 0;
do
result = x86_cpu_decode_core(v1++);
while ( x86_cpu_num_cores > v1 );
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 result; // rax
signed int v1; // ebx

result = x86_cpu;
*(_QWORD *)(x86_cpu + 32LL) = "decode";
if ( x86_cpu_num_cores > 0 )
{
v1 = 0;
do
result = func1(v1++);
while ( x86_cpu_num_cores > v1 );
}
return result;
}

original_code:
void x86_cpu_decode()
{
 int core;
 x86_cpu->stage = "decode";
 for (core = 0; core < x86_cpu_num_cores; core++)
  x86_cpu_decode_core(core);
}


decompiled_code:
__int64 evg_uop_done()
{
return repos_free(gpu_uop_repos);
}

canonical_decompiled_code:
__int64 func2()
{
return repos_free(gpu_uop_repos);
}

original_code:
void evg_uop_done()
{
 repos_free(gpu_uop_repos);
}


decompiled_code:
__int64 __fastcall evg_uop_dump_dep_list(__int64 a1, int a2, _DWORD *a3, int a4)
{
_DWORD *v6; // rbx
__int64 v7; // r12
const char *v8; // rbp
const char *v9; // rax
unsigned int v10; // eax
int v12; // [rsp+4h] [rbp-F4h] BYREF
__int64 v13; // [rsp+8h] [rbp-F0h] BYREF
char dest[232]; // [rsp+10h] [rbp-E8h] BYREF

v13 = a1;
v12 = a2;
str_printf(&v13, &v12, "{");
if ( a4 > 0 )
{
v6 = a3;
v7 = (__int64)&a3[a4 - 1 + 1];
v8 = (const char *)&unk_72C;
do
{
v10 = *v6 - 1;
if ( v10 > 0x7F )
{
v9 = (const char *)map_value(&gpu_uop_dep_map_2);
strcpy(dest, v9);
}
else
{
sprintf(dest, "R%d", v10);
}
str_printf(&v13, &v12, "%s%s", v8, dest);
++v6;
v8 = (const char *)asc_738;
}
while ( v6 != (_DWORD *)v7 );
}
return str_printf(&v13, &v12, (const char *)&asc_738[1]);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, int a2, _DWORD *a3, int a4)
{
_DWORD *v6; // rbx
__int64 v7; // r12
const char *v8; // rbp
const char *v9; // rax
unsigned int v10; // eax
int v12; // [rsp+4h] [rbp-F4h] BYREF
__int64 v13; // [rsp+8h] [rbp-F0h] BYREF
char dest[232]; // [rsp+10h] [rbp-E8h] BYREF

v13 = a1;
v12 = a2;
str_printf(&v13, &v12, "{");
if ( a4 > 0 )
{
v6 = a3;
v7 = (__int64)&a3[a4 - 1 + 1];
v8 = (const char *)&unk_72C;
do
{
v10 = *v6 - 1;
if ( v10 > 0x7F )
{
v9 = (const char *)map_value(&gpu_uop_dep_map_2);
strcpy(dest, v9);
}
else
{
sprintf(dest, "R%d", v10);
}
str_printf(&v13, &v12, "%s%s", v8, dest);
++v6;
v8 = (const char *)asc_738;
}
while ( v6 != (_DWORD *)v7 );
}
return str_printf(&v13, &v12, (const char *)&asc_738[1]);
}

original_code:
void evg_uop_dump_dep_list(char *buf, int size, int *dep_list, int dep_count)
{
 static struct string_map_t gpu_uop_dep_map = {
  4, {
   { "none", 0 },
   { "LDS", 131 },
   { "PS", 130 },
   { "PV", 129 }
  }
 };
 char *comma = "";
 char str[200];
 int i;
 str_printf(&buf, &size, "{");
 for (i = 0; i < dep_count; i++)
 {
  if (((dep_list[i])>=(1)&&(dep_list[i])<=(128)))
   sprintf(str, "R%d", dep_list[i] - 1);
  else
   strcpy(str, map_value(&gpu_uop_dep_map, dep_list[i]));
  str_printf(&buf, &size, "%s%s", comma, str);
  comma = ",";
 }
 str_printf(&buf, &size, "}");
}

variable_types:
buf
char *

variable_types: size int

variable_types: struct string_map_t { int count; struct { char *string; int value; } map[]; }

variable_types:
comma
char *

variable_types:
str
char [200]


decompiled_code:
_DWORD *evg_opencl_mem_create()
{
_DWORD *v0; // rbx

v0 = (_DWORD *)mhandle_calloc(1LL, 52LL, "opencl-mem.c:34");
if ( !v0 )
fatal("%s: out of memory", "evg_opencl_mem_create");
*v0 = evg_opencl_repo_new_object_id(*(_QWORD *)(evg_emu + 8LL), 7LL);
v0[1] = 1;
evg_opencl_repo_add_object(*(_QWORD *)(evg_emu + 8LL), v0);
return v0;
}

canonical_decompiled_code:
_DWORD *func0()
{
_DWORD *v0; // rbx

v0 = (_DWORD *)mhandle_calloc(1LL, 52LL, "opencl-mem.c:34");
if ( !v0 )
fatal("%s: out of memory", "evg_opencl_mem_create");
*v0 = evg_opencl_repo_new_object_id(*(_QWORD *)(evg_emu + 8LL), 7LL);
v0[1] = 1;
evg_opencl_repo_add_object(*(_QWORD *)(evg_emu + 8LL), v0);
return v0;
}

original_code:
struct evg_opencl_mem_t *evg_opencl_mem_create()
{
 struct evg_opencl_mem_t *mem;
 mem = (mhandle_calloc(1, sizeof(struct evg_opencl_mem_t), "opencl-mem.c" ":" "34"));
 if (!mem)
  fatal("%s: out of memory", __FUNCTION__);
 mem->id = evg_opencl_repo_new_object_id(evg_emu->opencl_repo,
  evg_opencl_object_mem);
 mem->ref_count = 1;
 evg_opencl_repo_add_object(evg_emu->opencl_repo, mem);
 return mem;
}

return_type: struct evg_opencl_mem_t { unsigned int id; int ref_count; unsigned int type; unsigned int size; unsigned int height; unsigned int width; unsigned int depth; unsigned int num_pixels; unsigned int pixel_size; unsigned int num_channels_per_pixel; unsigned int flags; unsigned int host_ptr; unsigned int device_ptr; } *


decompiled_code:
__int64 __fastcall evg_opencl_mem_free(__int64 a1)
{
evg_opencl_repo_remove_object(*(_QWORD *)(evg_emu + 8LL), a1);
return mhandle_free(a1, "opencl-mem.c:52");
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
evg_opencl_repo_remove_object(*(_QWORD *)(evg_emu + 8LL), a1);
return mhandle_free(a1, "opencl-mem.c:52");
}

original_code:
void evg_opencl_mem_free(struct evg_opencl_mem_t *mem)
{
 evg_opencl_repo_remove_object(evg_emu->opencl_repo, mem);
 (mhandle_free(mem, "opencl-mem.c" ":" "52"));
}

user_defined_types: struct evg_opencl_mem_t { unsigned int id; int ref_count; unsigned int type; unsigned int size; unsigned int height; unsigned int width; unsigned int depth; unsigned int num_pixels; unsigned int pixel_size; unsigned int num_channels_per_pixel; unsigned int flags; unsigned int host_ptr; unsigned int device_ptr; }


decompiled_code:
__int64 __fastcall matrix_free(__int64 a1)
{
__int64 v2; // rdi

v2 = *(_QWORD *)(a1 + 8);
if ( v2 )
mhandle_free(v2, "matrix.c:69");
return mhandle_free(a1, "matrix.c:70");
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 v2; // rdi

v2 = *(_QWORD *)(a1 + 8);
if ( v2 )
mhandle_free(v2, "matrix.c:69");
return mhandle_free(a1, "matrix.c:70");
}

original_code:
void matrix_free(struct matrix_t *matrix)
{
 if (matrix->elem)
  (mhandle_free(matrix->elem, "matrix.c" ":" "69"));
 (mhandle_free(matrix, "matrix.c" ":" "70"));
}

variable_types: struct matrix_t { int num_rows; int num_cols; void **elem; } *


original_code:
void matrix_set(struct matrix_t *matrix, int row, int col, void *value)
{
 if (row < 0 || row >= matrix->num_rows)
  return;
 if (col < 0 || col >= matrix->num_cols)
  return;
 matrix->elem[row * matrix->num_cols + col] = value;
}

variable_types: row int

variable_types: col int

variable_types:
value
void *

user_defined_types: struct matrix_t { int num_rows; int num_cols; void **elem; }


decompiled_code:
__int64 __fastcall matrix_get(__int64 a1, int a2, int a3)
{
int v3; // eax
__int64 result; // rax

if ( a2 < 0 )
return 0LL;
if ( *(_DWORD *)a1 <= a2 || a3 < 0 )
return 0LL;
v3 = *(_DWORD *)(a1 + 4);
if ( v3 <= a3 )
result = 0LL;
else
result = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * (v3 * a2 + a3));
return result;
}

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

if ( a2 < 0 )
return 0LL;
if ( *(_DWORD *)a1 <= a2 || a3 < 0 )
return 0LL;
v3 = *(_DWORD *)(a1 + 4);
if ( v3 <= a3 )
result = 0LL;
else
result = *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * (v3 * a2 + a3));
return result;
}

original_code:
void *matrix_get(struct matrix_t *matrix, int row, int col)
{
 if (row < 0 || row >= matrix->num_rows)
  return ((void *)0);
 if (col < 0 || col >= matrix->num_cols)
  return ((void *)0);
 return matrix->elem[row * matrix->num_cols + col];
}

return_type: void *


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

result = *(void **)(a1 + 8);
if ( result )
result = memset(*(void **)(a1 + 8), 0, 8LL * *(_DWORD *)(a1 + 4) * *(_DWORD *)a1);
return result;
}

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

result = *(void **)(a1 + 8);
if ( result )
result = memset(*(void **)(a1 + 8), 0, 8LL * *(_DWORD *)(a1 + 4) * *(_DWORD *)a1);
return result;
}

original_code:
void matrix_clear(struct matrix_t *matrix)
{
 if (matrix->elem)
  memset(matrix->elem, 0, matrix->num_rows * matrix->num_cols * sizeof(void *));
}


decompiled_code:
__int64 __fastcall matrix_get_num_rows(unsigned int *a1)
{
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int *a1)
{
return *a1;
}

original_code:
int matrix_get_num_rows(struct matrix_t *matrix)
{
 return matrix->num_rows;
}

return_type: int


decompiled_code:
__int64 __fastcall matrix_get_num_cols(__int64 a1)
{
return *(unsigned int *)(a1 + 4);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return *(unsigned int *)(a1 + 4);
}

original_code:
int matrix_get_num_cols(struct matrix_t *matrix)
{
 return matrix->num_cols;
}


grow

original_code:
static int grow(struct buffer_t *buffer, int size)
{
 void *data;
 if (size < buffer->size)
  return 0;
 data = (mhandle_calloc(1, size, "buffer.c" ":" "47"));
 if (!data)
  return 0;
 memcpy(data, buffer->data + buffer->head, buffer->size - buffer->head);
 memcpy(data + buffer->size - buffer->head, buffer->data, buffer->head);
 (mhandle_free(buffer->data, "buffer.c" ":" "56"));
 buffer->data = data;
 buffer->size = size;
 buffer->head = 0;
 buffer->tail = buffer->count;
 return 1;
}

variable_types: struct buffer_t { void *data; int count; int head; } *

variable_types:
data
void *


decompiled_code:
__int64 __fastcall buffer_free(_QWORD *a1)
{
mhandle_free(*a1, "buffer.c:88");
return mhandle_free(a1, "buffer.c:89");
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1)
{
mhandle_free(*a1, "buffer.c:88");
return mhandle_free(a1, "buffer.c:89");
}

original_code:
void buffer_free(struct buffer_t *buffer)
{
 (mhandle_free(buffer->data, "buffer.c" ":" "88"));
 (mhandle_free(buffer, "buffer.c" ":" "89"));
}

user_defined_types: struct buffer_t { void *data; int count; int head; }


original_code:
int buffer_read(struct buffer_t *buffer, void *dest, int size)
{
 int right, left;
 size = ((size)<(buffer->count)?(size):(buffer->count));
 right = ((buffer->size - buffer->head)<(size)?(buffer->size - buffer->head):(size));
 left = size - right;
 memcpy(dest, buffer->data + buffer->head, right);
 memcpy(dest + right, buffer->data, left);
 buffer->head = (buffer->head + size) % buffer->size;
 buffer->count -= size;
 return size;
}

canonical_original_code:
int buffer_read(struct buffer_t *buffer, void *dest, int size)
{
 int right; int left;
 size = ((size)<(buffer->count)?(size):(buffer->count));
 right = ((buffer->size - buffer->head)<(size)?(buffer->size - buffer->head):(size));
 left = size - right;
 memcpy(dest, buffer->data + buffer->head, right);
 memcpy(dest + right, buffer->data, left);
 buffer->head = (buffer->head + size) % buffer->size;
 buffer->count -= size;
 return size;
}

variable_types:
dest
void *

variable_types: right int

variable_types: left int


original_code:
int buffer_write(struct buffer_t *buffer, void *dest, int size)
{
 int right, left;
 while (buffer->count + size > buffer->size)
  if (!grow(buffer, ((buffer->count + size)>(buffer->size * 2)?(buffer->count + size):(buffer->size * 2))))
   return 0;
 right = ((buffer->size - buffer->tail)<(size)?(buffer->size - buffer->tail):(size));
 left = size - right;
 memcpy(buffer->data + buffer->tail, dest, right);
 memcpy(buffer->data, dest + right, left);
 buffer->tail = (buffer->tail + size) % buffer->size;
 buffer->count += size;
 return size;
}

canonical_original_code:
int buffer_write(struct buffer_t *buffer, void *dest, int size)
{
 int right; int left;
 while (buffer->count + size > buffer->size)
  if (!grow(buffer, ((buffer->count + size)>(buffer->size * 2)?(buffer->count + size):(buffer->size * 2))))
   return 0;
 right = ((buffer->size - buffer->tail)<(size)?(buffer->size - buffer->tail):(size));
 left = size - right;
 memcpy(buffer->data + buffer->tail, dest, right);
 memcpy(buffer->data, dest + right, left);
 buffer->tail = (buffer->tail + size) % buffer->size;
 buffer->count += size;
 return size;
}


decompiled_code:
__int64 __fastcall buffer_count(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return *(unsigned int *)(a1 + 8);
}

original_code:
int buffer_count(struct buffer_t *buffer)
{
 return buffer->count;
}


decompiled_code:
_DWORD *si_opencl_mem_create()
{
_DWORD *v0; // rbx

v0 = (_DWORD *)mhandle_calloc(1LL, 64LL, "opencl-mem.c:34");
if ( !v0 )
fatal("%s: out of memory", "si_opencl_mem_create");
*v0 = si_opencl_repo_new_object_id(*(_QWORD *)(si_emu + 8LL), 7LL);
v0[1] = 1;
si_opencl_repo_add_object(*(_QWORD *)(si_emu + 8LL), v0);
return v0;
}

canonical_decompiled_code:
_DWORD *func0()
{
_DWORD *v0; // rbx

v0 = (_DWORD *)mhandle_calloc(1LL, 64LL, "opencl-mem.c:34");
if ( !v0 )
fatal("%s: out of memory", "si_opencl_mem_create");
*v0 = si_opencl_repo_new_object_id(*(_QWORD *)(si_emu + 8LL), 7LL);
v0[1] = 1;
si_opencl_repo_add_object(*(_QWORD *)(si_emu + 8LL), v0);
return v0;
}

original_code:
struct si_opencl_mem_t *si_opencl_mem_create()
{
 struct si_opencl_mem_t *mem;
 mem = (mhandle_calloc(1, sizeof(struct si_opencl_mem_t), "opencl-mem.c" ":" "34"));
 if (!mem)
  fatal("%s: out of memory", __FUNCTION__);
 mem->id = si_opencl_repo_new_object_id(si_emu->opencl_repo,
  si_opencl_object_mem);
 mem->ref_count = 1;
 si_opencl_repo_add_object(si_emu->opencl_repo, mem);
 return mem;
}

variable_types: struct si_opencl_mem_t { unsigned int id; int ref_count; unsigned int type; unsigned int size; unsigned int height; unsigned int width; unsigned int depth; unsigned int num_pixels; unsigned int pixel_size; unsigned int num_channels_per_pixel; unsigned int map_flags; unsigned int map_offset; unsigned int map_cb; unsigned int flags; unsigned int host_ptr; unsigned int device_ptr; } *


decompiled_code:
__int64 __fastcall si_opencl_mem_free(__int64 a1)
{
si_opencl_repo_remove_object(*(_QWORD *)(si_emu + 8LL), a1);
return mhandle_free(a1, "opencl-mem.c:52");
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
si_opencl_repo_remove_object(*(_QWORD *)(si_emu + 8LL), a1);
return mhandle_free(a1, "opencl-mem.c:52");
}

original_code:
void si_opencl_mem_free(struct si_opencl_mem_t *mem)
{
 si_opencl_repo_remove_object(si_emu->opencl_repo, mem);
 (mhandle_free(mem, "opencl-mem.c" ":" "52"));
}

user_defined_types: struct si_opencl_mem_t { unsigned int id; int ref_count; unsigned int type; unsigned int size; unsigned int height; unsigned int width; unsigned int depth; unsigned int num_pixels; unsigned int pixel_size; unsigned int num_channels_per_pixel; unsigned int map_flags; unsigned int map_offset; unsigned int map_cb; unsigned int flags; unsigned int host_ptr; unsigned int device_ptr; }


decompiled_code:
__int64 frm_emu_done()
{
frm_cuda_object_free_all();
linked_list_free(frm_cuda_object_list);
frm_isa_done();
mem_free(*(_QWORD *)(frm_emu + 96));
mem_free(*(_QWORD *)(frm_emu + 232));
return mhandle_free(frm_emu, "emu.c:85");
}

canonical_decompiled_code:
__int64 func1()
{
frm_cuda_object_free_all();
linked_list_free(frm_cuda_object_list);
frm_isa_done();
mem_free(*(_QWORD *)(frm_emu + 96));
mem_free(*(_QWORD *)(frm_emu + 232));
return mhandle_free(frm_emu, "emu.c:85");
}

original_code:
void frm_emu_done(void)
{
 frm_cuda_object_free_all();
 linked_list_free(frm_cuda_object_list);
 frm_isa_done();
        mem_free(frm_emu->const_mem);
        mem_free(frm_emu->global_mem);
        (mhandle_free(frm_emu, "emu.c" ":" "85"));
}

canonical_original_code:
void frm_emu_done()
{
 frm_cuda_object_free_all();
 linked_list_free(frm_cuda_object_list);
 frm_isa_done();
        mem_free(frm_emu->const_mem);
        mem_free(frm_emu->global_mem);
        (mhandle_free(frm_emu, "emu.c" ":" "85"));
}


decompiled_code:
__int64 __fastcall x86_cpu_pipeline_empty(int a1, int a2)
{
__int64 v2; // rdx
unsigned int v3; // er12

v2 = *(_QWORD *)(*x86_cpu + 1824LL * a1) + 1904LL * a2;
v3 = *(_DWORD *)(v2 + 12);
if ( v3 )
return 0;
if ( !(unsigned int)list_count(*(_QWORD *)(v2 + 56)) )
v3 = list_count(*(_QWORD *)(*(_QWORD *)(*x86_cpu + 1824LL * a1) + 1904LL * a2 + 64)) == 0;
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2)
{
__int64 v2; // rdx
unsigned int v3; // er12

v2 = *(_QWORD *)(*x86_cpu + 1824LL * a1) + 1904LL * a2;
v3 = *(_DWORD *)(v2 + 12);
if ( v3 )
return 0;
if ( !(unsigned int)list_count(*(_QWORD *)(v2 + 56)) )
v3 = list_count(*(_QWORD *)(*(_QWORD *)(*x86_cpu + 1824LL * a1) + 1904LL * a2 + 64)) == 0;
return v3;
}

original_code:
int x86_cpu_pipeline_empty(int core, int thread)
{
 return !(x86_cpu->core[core].thread[thread]).rob_count && !list_count((x86_cpu->core[core].thread[thread]).fetch_queue) &&
  !list_count((x86_cpu->core[core].thread[thread]).uop_queue);
}

variable_types: core int


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

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

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

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

original_code:
static int frm_warp_divergence_compare(const void *elem1, const void *elem2)
{
 const int count1 = * (const int *) elem1;
 const int count2 = * (const int *) elem2;
 if (count1 < count2)
  return 1;
 else if (count1 > count2)
  return -1;
 return 0;
}

canonical_original_code:
static int frm_warp_divergence_compare(void *elem1, void *elem2)
{
 int count1 = * (const int *) elem1;
 int count2 = * (const int *) elem2;
 if (count1 < count2)
  return 1;
 else if (count1 > count2)
  return -1;
 return 0;
}


repo: raphui/rnk

decompiled_code:
char *__fastcall elf_get_section(int a1)
{
return (char *)buff + section_size * a1 + (unsigned __int64)*(unsigned int *)(ehdr + 32);
}

canonical_decompiled_code:
char *__fastcall func7(int a1)
{
return (char *)buff + section_size * a1 + (unsigned __int64)*(unsigned int *)(ehdr + 32);
}

original_code:
static Elf32_Shdr *elf_get_section(int num)
{
 return (Elf32_Shdr *)(buff + ehdr->e_shoff + num * section_size);
}

canonical_original_code:
static struct Elf32_Shdr *elf_get_section(int num)
{
 return (struct Elf32_Shdr *)(buff + ehdr->e_shoff + num * section_size);
}

variable_types: num int

return_type: struct Elf32_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned int sh_flags; unsigned int sh_addr; unsigned int sh_offset; unsigned int sh_size; unsigned int sh_link; unsigned int sh_info; unsigned int sh_addralign; unsigned int sh_entsize; } *


decompiled_code:
__int64 __fastcall hash_str(char *a1)
{
char *v1; // rsi
char v2; // al
unsigned int v3; // ecx

v1 = a1 + 1;
v2 = *a1;
if ( !*a1 )
return 5381;
v3 = 5381;
do
{
v3 += 32 * v3 + v2;
v2 = *v1++;
}
while ( v2 );
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *a1)
{
char *v1; // rsi
char v2; // al
unsigned int v3; // ecx

v1 = a1 + 1;
v2 = *a1;
if ( !*a1 )
return 5381;
v3 = 5381;
do
{
v3 += 32 * v3 + v2;
v2 = *v1++;
}
while ( v2 );
return v3;
}

original_code:
static unsigned int hash_str(char *str)
{
 unsigned int hash = 5381;
 int c;
 while ((c = *str++))
  hash = ((hash << 5) + hash) + c;
 return hash;
}

variable_types:
str
char *

variable_types:
hash
unsigned int

variable_types: c int

return_type: unsigned int


cmpasc

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

result = 0xFFFFFFFFLL;
if ( *a1 >= *a2 )
result = *a1 > *a2;
return result;
}

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

result = 0xFFFFFFFFLL;
if ( *a1 >= *a2 )
result = *a1 > *a2;
return result;
}

original_code:
int cmpasc(const void *input1, const void *input2)
{
        const int elem1 = *(int *) input1;
        const int elem2 = *(int *) input2;
        if(elem1 < elem2)
                return -1;
        if(elem1 > elem2)
                return 1;
        return 0;
}

canonical_original_code:
int cmpasc(void *input1, void *input2)
{
        int elem1 = *(int *) input1;
        int elem2 = *(int *) input2;
        if(elem1 < elem2)
                return -1;
        if(elem1 > elem2)
                return 1;
        return 0;
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rbx
char *v4; // rbp
char *v5; // rbp
_BYTE v7[80]; // [rsp+0h] [rbp-68h] BYREF
char v8[24]; // [rsp+50h] [rbp-18h] BYREF

v3 = v7;
v4 = v7;
do
{
*(_DWORD *)v4 = rand() % 20;
v4 += 4;
}
while ( v4 != v8 );
printf("array: ");
v5 = v7;
do
{
printf("%d, ", *(unsigned int *)v5);
v5 += 4;
}
while ( v5 != v8 );
putchar(10);
hsort(v7, 20LL, 4LL, cmpasc);
printf("sorted output: ");
do
{
printf("%d, ", *(unsigned int *)v3);
v3 += 4;
}
while ( v3 != v8 );
putchar(10);
return 0;
}

original_code:
int main()
{
 block blk[20];
 int arr[20];
 int i;
 int output[20];
 int limit = 20 - 1, outcntr = 0;
 for(i = 0; i < 20; i++) {
  blk[i].id = 20 - i;
  arr[i] = rand() % 20;
 }
 printf("array: ");
 for(i = 0; i < 20; i++)
  printf("%d, ", arr[i]);
 printf("\n");
 hsort((void *) &arr, 20, sizeof(int), cmpasc);
 printf("sorted output: ");
 for(i = 0; i < 20; i++)
  printf("%d, ", arr[i]);
 printf("\n");
 return 0;
}

canonical_original_code:
int main()
{
 struct block blk[20];
 int arr[20];
 int i;
 int output[20];
 int limit = 20 - 1; int outcntr = 0;
 for(i = 0; i < 20; i++) {
  blk[i].id = 20 - i;
  arr[i] = rand() % 20;
 }
 printf("array: ");
 for(i = 0; i < 20; i++)
  printf("%d, ", arr[i]);
 printf("\n");
 hsort((void *) &arr, 20, sizeof(int), cmpasc);
 printf("sorted output: ");
 for(i = 0; i < 20; i++)
  printf("%d, ", arr[i]);
 printf("\n");
 return 0;
}

variable_types: struct block { int id; char name[20]; } [20]

variable_types:
arr
int [20]

variable_types: i int

variable_types:
output
int [20]

variable_types: limit int

variable_types: outcntr int


error

decompiled_code:
void __fastcall __noreturn error(const char *a1)
{
perror(a1);
putchar(10);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func2(const char *a1)
{
perror(a1);
putchar(10);
exit(1);
}

original_code:
void error(char *msg)
{
   perror(msg);
   printf("\n");
   exit(1);
}

variable_types:
msg
char *


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

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

original_code:
void close_sock(int sock)
{
   close(sock);
}

variable_types: sock int


swap

original_code:
static void swap(char *num1, char *num2, unsigned width) {
 char tmp;
 if (num1 != num2) {
  while (width--) {
   tmp = *num1;
   *num1++ = *num2;
   *num2++ = tmp;
      }
   }
}

canonical_original_code:
static void swap(char *num1, char *num2, unsigned int width) {
 char tmp;
 if (num1 != num2) {
  while (width--) {
   tmp = *num1;
   *num1++ = *num2;
   *num2++ = tmp;
      }
   }
}

variable_types:
width
unsigned int

variable_types: tmp char


sift

decompiled_code:
void __fastcall sift(__int64 a1, unsigned int a2, unsigned int a3, int a4, int (__fastcall *a5)(__int64, __int64))
{
unsigned int v5; // ebx
char *v8; // r14
unsigned int v9; // ebp
unsigned int v10; // er12
unsigned int v12; // [rsp+Ch] [rbp-3Ch]

v5 = a2;
v12 = a3 >> 1;
if ( a2 < a3 >> 1 )
{
do
{
v9 = 2 * v5 + 2;
v10 = 2 * v5 + 1;
if ( v9 < a3 && a5(a1 + a4 * v9 - a4, a1 + a4 * v9) < 0 )
v10 = 2 * v5 + 2;
v8 = (char *)(a1 + a4 * v5);
v5 = v10;
if ( a5((__int64)v8, a1 + a4 * v10) > 0 )
break;
swap(v8, (char *)(a1 + a4 * v10), a4);
}
while ( v10 < v12 );
}
}

original_code:
static void sift(void *base, unsigned mid, unsigned num, unsigned width, int (*comp)(const void *, const void *))
{
 int left, right;
 int small;
 char *rchild, *lchild;
 char *midchild, *smallchild;
 while(mid < floor(num / 2)) {
  left = (mid + 1) * 2 - 1;
  right = left + 1;
  lchild = (char *) base + left * width;
  rchild = (char *) base + right * width;
  if(right < num && comp(lchild, rchild) < 0)
   small = right;
  else
   small = left;
  midchild = (char *) base + mid * width;
  smallchild = (char *) base + small * width;
  if(comp(midchild, smallchild) > 0)
   return;
  swap(midchild, smallchild, width);
  mid = small;
 }
}

canonical_original_code:
static void sift(void *base, unsigned int mid, unsigned int num, unsigned int width, int (*comp)(void *, void *))
{
 int left; int right;
 int small;
 char *rchild; char *lchild;
 char *midchild; char *smallchild;
 while(mid < floor(num / 2)) {
  left = (mid + 1) * 2 - 1;
  right = left + 1;
  lchild = (char *) base + left * width;
  rchild = (char *) base + right * width;
  if(right < num && comp(lchild, rchild) < 0)
   small = right;
  else
   small = left;
  midchild = (char *) base + mid * width;
  smallchild = (char *) base + small * width;
  if(comp(midchild, smallchild) > 0)
   return;
  swap(midchild, smallchild, width);
  mid = small;
 }
}

variable_types:
base
void *

variable_types:
mid
unsigned int

variable_types:
num
unsigned int

variable_types:
comp
int (*)(void *, void *)

variable_types: left int

variable_types: right int

variable_types: small int

variable_types:
rchild
char *

variable_types:
lchild
char *

variable_types:
midchild
char *

variable_types:
smallchild
char *


heapify

decompiled_code:
void __fastcall heapify(__int64 a1, unsigned int a2, int a3, int (__fastcall *a4)(__int64, __int64))
{
int v6; // ebx

v6 = a2 >> 1;
do
sift(a1, --v6, a2, a3, a4);
while ( v6 >= 0 );
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, unsigned int a2, int a3, int (__fastcall *a4)(__int64, __int64))
{
int v6; // ebx

v6 = a2 >> 1;
do
func1(a1, --v6, a2, a3, a4);
while ( v6 >= 0 );
}

original_code:
static void heapify(void *base, unsigned num, unsigned width, int (*comp)(const void *, const void *))
{
 int mid;
 mid = floor(num / 2);
 while(mid >= 0) {
  sift(base, mid - 1, num, width, comp);
  mid--;
 }
}

canonical_original_code:
static void heapify(void *base, unsigned int num, unsigned int width, int (*comp)(void *, void *))
{
 int mid;
 mid = floor(num / 2);
 while(mid >= 0) {
  sift(base, mid - 1, num, width, comp);
  mid--;
 }
}

variable_types: mid int


hsort

decompiled_code:
void __fastcall hsort(char *a1, unsigned int a2, int a3, int (__fastcall *a4)(__int64, __int64))
{
unsigned int v4; // ebx
unsigned int v7; // er12

if ( a2 )
{
v4 = a2;
heapify((__int64)a1, a2, a3, a4);
v7 = a3 * (a2 - 1);
while ( 1 )
{
--v4;
swap(a1, &a1[v7], a3);
v7 -= a3;
if ( !v4 )
break;
sift((__int64)a1, 0, v4, a3, a4);
}
}
}

canonical_decompiled_code:
void __fastcall func3(char *a1, unsigned int a2, int a3, int (__fastcall *a4)(__int64, __int64))
{
unsigned int v4; // ebx
unsigned int v7; // er12

if ( a2 )
{
v4 = a2;
func2((__int64)a1, a2, a3, a4);
v7 = a3 * (a2 - 1);
while ( 1 )
{
--v4;
func0(a1, &a1[v7], a3);
v7 -= a3;
if ( !v4 )
break;
func1((__int64)a1, 0, v4, a3, a4);
}
}
}

original_code:
void hsort(void *base, unsigned num, unsigned width, int (*comp)(const void *, const void *))
{
 char *next;
 char *last;
 int i;
 if(num == 0)
  return;
 heapify(base, num, width, comp);
 do
 {
  next = base;
  last = (char *) base + (num - 1) * width;
  swap(next, last, width);
  num--;
  if(num > 0) {
   sift(base, 0, num, width, comp);
  }
 }while(num > 0);
}

canonical_original_code:
void hsort(void *base, unsigned int num, unsigned int width, int (*comp)(void *, void *))
{
 char *next;
 char *last;
 int i;
 if(num == 0)
  return;
 heapify(base, num, width, comp);
 do
 {
  next = base;
  last = (char *) base + (num - 1) * width;
  swap(next, last, width);
  num--;
  if(num > 0) {
   sift(base, 0, num, width, comp);
  }
 }while(num > 0);
}

variable_types:
next
char *

variable_types:
last
char *

variable_types: i int


decompiled_code:
__int64 __fastcall connect_to(int fd, char *name, __int16 a3)
{
const char *v4; // rdx
struct hostent *v5; // rbp
struct sockaddr dest[2]; // [rsp+0h] [rbp-28h] BYREF

v5 = gethostbyname(name);
if ( !v5 )
error((int)"No such host", (int)name, v4);
*(_QWORD *)&dest[0].sa_family = 2LL;
*(_QWORD *)&dest[0].sa_data[6] = 0LL;
memmove(&dest[0].sa_data[2], *(const void **)v5->h_addr_list, v5->h_length);
*(_WORD *)dest[0].sa_data = _bswap_16(a3);
return (unsigned int)(connect(fd, dest, 0x10u) >> 31);
}

canonical_decompiled_code:
__int64 __fastcall func1(int fd, char *name, __int16 a3)
{
const char *v4; // rdx
struct hostent *v5; // rbp
struct sockaddr dest[2]; // [rsp+0h] [rbp-28h] BYREF

v5 = gethostbyname(name);
if ( !v5 )
error((int)"No such host", (int)name, v4);
*(_QWORD *)&dest[0].sa_family = 2LL;
*(_QWORD *)&dest[0].sa_data[6] = 0LL;
memmove(&dest[0].sa_data[2], *(const void **)v5->h_addr_list, v5->h_length);
*(_WORD *)dest[0].sa_data = func0(a3);
return (unsigned int)(connect(fd, dest, 0x10u) >> 31);
}

original_code:
int connect_to(int sock, char *server_addr, int port)
{
 struct sockaddr_in sin_serversock;
 struct hostent *h_server;
  h_server = gethostbyname(server_addr);
  if(h_server == ((void *)0))
  {
    error("No such host");
  }
  bzero((char *) &sin_serversock, sizeof(sin_serversock));
  sin_serversock.sin_family = 2;
  bcopy((char *) h_server->h_addr_list[0],
    (char *) &sin_serversock.sin_addr.s_addr,
    h_server->h_length);
  sin_serversock.sin_port = htons(port);
  if(connect(sock, (struct sockaddr *) &sin_serversock, sizeof(sin_serversock)) < 0)
  {
  return -1;
  }
  return 0;
}

variable_types: sock int

variable_types: port int

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

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

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

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


decompiled_code:
int __fastcall read_my_ip(char *s, char *src)
{
int v2; // ebx
char v4[16]; // [rsp+0h] [rbp-48h] BYREF
__int16 v5; // [rsp+10h] [rbp-38h]
unsigned int v6; // [rsp+14h] [rbp-34h]

v2 = socket(2, 1, 0);
v5 = 2;
strncpy(v4, src, 0xFuLL);
ioctl(v2, 0x8915uLL, v4);
close(v2);
return sprintf(s, "%d.%d.%d.%d", (unsigned __int8)v6, BYTE1(v6), BYTE2(v6), HIBYTE(v6));
}

canonical_decompiled_code:
int __fastcall func2(char *s, char *src)
{
int v2; // ebx
char v4[16]; // [rsp+0h] [rbp-48h] BYREF
__int16 v5; // [rsp+10h] [rbp-38h]
unsigned int v6; // [rsp+14h] [rbp-34h]

v2 = socket(2, 1, 0);
v5 = 2;
strncpy(v4, src, 0xFuLL);
ioctl(v2, 0x8915uLL, v4);
close(v2);
return sprintf(s, "%d.%d.%d.%d", (unsigned __int8)v6, BYTE1(v6), BYTE2(v6), HIBYTE(v6));
}

original_code:
void read_my_ip(char ip[50], char *interface)
{
  int fd;
  struct ifreq ifr;
  struct sockaddr_in *client;
  fd = socket(2, SOCK_STREAM, 0);
  ifr.ifr_ifru.ifru_addr.sa_family = 2;
  strncpy(ifr.ifr_ifrn.ifrn_name, interface, 16 -1);
  ioctl(fd, 0x8915, &ifr);
  close(fd);
  client = (struct sockaddr_in *) &ifr.ifr_ifru.ifru_addr;
  sprintf(ip, "%d.%d.%d.%d", (int)(client->sin_addr.s_addr&0xFF),
     (int)((client->sin_addr.s_addr&0xFF00)>>8),
     (int)((client->sin_addr.s_addr&0xFF0000)>>16),
     (int)((client->sin_addr.s_addr&0xFF000000)>>24));
}

variable_types:
ip
char [50]

variable_types:
interface
char *

variable_types: fd int

variable_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }

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

user_defined_types: struct ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }


decompiled_code:
int __fastcall listen_sock(int a1)
{
return listen(a1, 5);
}

canonical_decompiled_code:
int __fastcall func2(int a1)
{
return listen(a1, 5);
}

original_code:
void listen_sock(int sock)
{
  listen(sock, 5);
}


decompiled_code:
__int64 __fastcall accept_con(int a1)
{
const char *v1; // rdx
int v2; // ebx
socklen_t addr_len; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

addr_len = 16;
v2 = accept(a1, &addr, &addr_len);
if ( v2 < 0 )
error((int)"error while accepting connection", (int)&addr, v1);
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1)
{
const char *v1; // rdx
int v2; // ebx
socklen_t addr_len; // [rsp+Ch] [rbp-1Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-18h] BYREF

addr_len = 16;
v2 = accept(a1, &addr, &addr_len);
if ( v2 < 0 )
error((int)"error while accepting connection", (int)&addr, v1);
return (unsigned int)v2;
}

original_code:
int accept_con(int sock)
{
  struct sockaddr_in client;
  int client_len = sizeof(client);
  int csock;
  char req_from[50];
  csock = accept(sock, (struct sockaddr *) &client, &client_len);
  if(csock < 0)
     error("error while accepting connection");
  else
  {
  }
  return csock;
}

variable_types: csock int


decompiled_code:
__int64 __fastcall my_add(unsigned int a1, int a2)
{
__int64 result; // rax
int i; // esi
int v4; // edx

result = a2 ^ a1;
for ( i = a1 & a2; i; result = v4 ^ (unsigned int)result )
{
v4 = 2 * i;
i = result & (2 * i);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int a1, int a2)
{
__int64 result; // rax
int i; // esi
int v4; // edx

result = a2 ^ a1;
for ( i = a1 & a2; i; result = v4 ^ (unsigned int)result )
{
v4 = 2 * i;
i = result & (2 * i);
}
return result;
}

original_code:
unsigned int my_add(unsigned int a, unsigned int b)
{
    int mask = 0, result = 0;
    result = a ^ b;
    mask = a & b;
    while (mask!=0) {
        int shifted = mask << 1;
        mask = result & shifted;
        result ^= shifted;
    }
    return result;
}

canonical_original_code:
unsigned int my_add(unsigned int a, unsigned int b)
{
    int mask = 0; int result = 0;
    result = a ^ b;
    mask = a & b;
    while (mask!=0) {
        int shifted = mask << 1;
        mask = result & shifted;
        result ^= shifted;
    }
    return result;
}

variable_types:
a
unsigned int

variable_types:
b
unsigned int

variable_types: mask int

variable_types: result int

variable_types: shifted int


binaryCount

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

if ( a1 > 1 || (a1 & 1) != 0 )
{
binaryCount(a1 >> 1);
result = printf("%d", a1 & 1);
}
return result;
}

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

if ( a1 > 1 || (a1 & 1) != 0 )
{
func8(a1 >> 1);
result = printf("%d", a1 & 1);
}
return result;
}

original_code:
void binaryCount(unsigned int n){
    if (n<2 && n%2==0) {
        ;
    }
    else{
        binaryCount(n/2);
        printf("%d",n%2);
    }
}

variable_types:
n
unsigned int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
unsigned int v5; // [rsp+8h] [rbp-20h]
unsigned int v6; // [rsp+Ch] [rbp-1Ch]

printf("Please enter two numbers to sum:");
while ( (unsigned int)__isoc99_scanf() != -1 )
{
v3 = (unsigned int)my_add(v6, v5);
printf("\n%d + %d (base 10) = %d\n", v6, v5, v3);
printf("%d + %d (base 2) = ", v6, v5);
binaryCount(v3);
putchar(10);
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
unsigned int v5; // [rsp+8h] [rbp-20h]
unsigned int v6; // [rsp+Ch] [rbp-1Ch]

printf("Please enter two numbers to sum:");
while ( (unsigned int)__isoc99_scanf() != -1 )
{
v3 = (unsigned int)func7(v6, v5);
printf("\n%d + %d (base 10) = %d\n", v6, v5, v3);
printf("%d + %d (base 2) = ", v6, v5);
func8(v3);
putchar(10);
}
return 0;
}

original_code:
int main()
{
    unsigned int a, b;
    printf("Please enter two numbers to sum:");
    while (scanf("%u%u",&a, &b)!=(-1))
    {
    int result = my_add(a, b);
    printf("\n%d + %d (base 10) = %d\n", a, b, result);
    printf("%d + %d (base 2) = ", a, b);
    binaryCount(result);
    printf("\n");
    }
    return 0;
}

canonical_original_code:
int main()
{
    unsigned int a; unsigned int b;
    printf("Please enter two numbers to sum:");
    while (scanf("%u%u",&a, &b)!=(-1))
    {
    int result = my_add(a, b);
    printf("\n%d + %d (base 10) = %d\n", a, b, result);
    printf("%d + %d (base 2) = ", a, b);
    binaryCount(result);
    printf("\n");
    }
    return 0;
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
char v5[1016]; // [rsp+0h] [rbp-3F8h] BYREF

puts("\nEnter A String:");
while ( 1 )
{
v3 = fgets(v5, 1000, _bss_start);
if ( !v3 )
break;
printArray(v5);
}
return (int)v3;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v3; // rax
char v5[1016]; // [rsp+0h] [rbp-3F8h] BYREF

puts("\nEnter A String:");
while ( 1 )
{
v3 = fgets(v5, 1000, _bss_start);
if ( !v3 )
break;
func7(v5);
}
return (int)v3;
}

original_code:
int main()
{
 printf("\nEnter A String:\n");
 char a [1000];
 while(fgets(a, 1000, stdin))
  printArray(a);
 return 0;
}

canonical_original_code:
int main()
{
 printf("\nEnter A String:\n");
 char a[1000];
 while(fgets(a, 1000, stdin))
  printArray(a);
 return 0;
}


printArray

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

if ( a2 != a3 )
{
printf("%d ", *(unsigned int *)(a1 + 4LL * a2));
result = printArray(a1, (unsigned int)(a2 + 1), a3);
}
return result;
}

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

if ( a2 != a3 )
{
printf("%d ", *(unsigned int *)(a1 + 4LL * a2));
result = func7(a1, (unsigned int)(a2 + 1), a3);
}
return result;
}

original_code:
void printArray(int array[], int from, int to){
 if(from==to){
  return;
 }
 else{
  printf("%d ", array[from]);
  printArray(array, from+1, to);
 }
}

variable_types:
array
int []

variable_types: from int

variable_types: to int


summary

decompiled_code:
void __fastcall summary(__int64 a1, signed int a2)
{
_DWORD *v2; // rax
_DWORD *v3; // rbx
__int64 v4; // rax
int v5; // ecx

v2 = malloc(4LL * a2 + 1);
if ( !v2 )
{
printf("memory allocation failed!");
exit(0);
}
v3 = v2;
v4 = 0LL;
v5 = 0;
if ( a2 > 0 )
{
do
{
v5 += *(_DWORD *)(a1 + 4 * v4);
v3[v4++] = v5;
}
while ( v4 != a2 );
}
v3[a2] = 0;
printArray((__int64)v3, 0, a2);
free(v3);
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1, signed int a2)
{
_DWORD *v2; // rax
_DWORD *v3; // rbx
__int64 v4; // rax
int v5; // ecx

v2 = malloc(4LL * a2 + 1);
if ( !v2 )
{
printf("memory allocation failed!");
exit(0);
}
v3 = v2;
v4 = 0LL;
v5 = 0;
if ( a2 > 0 )
{
do
{
v5 += *(_DWORD *)(a1 + 4 * v4);
v3[v4++] = v5;
}
while ( v4 != a2 );
}
v3[a2] = 0;
func7((__int64)v3, 0, a2);
free(v3);
}

original_code:
void summary(int a[], int length){
 int i;
 int counter = 0;
 int *p;
 p = (int *) malloc(length * sizeof(int) + 1);
 if(!p){
  printf("memory allocation failed!");
  exit(0);
 }
 for(i=0 ; i<length ; i++){
  counter+=a[i];
  p[i] = counter;
 }
 p[length] = '\0';
 printArray(p, 0, length);
 free(p);
}

variable_types:
a
int []

variable_types: length int

variable_types: i int

variable_types: counter int

variable_types:
p
int *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_DWORD *v3; // rbx
signed int v4; // ebp
_BYTE v6[204]; // [rsp+0h] [rbp-E8h] BYREF
int v7[7]; // [rsp+CCh] [rbp-1Ch] BYREF

puts("Enter up to 50 numbers to get a summary of them:");
LABEL_2:
v3 = v6;
v4 = 0;
while ( scanf("%d", v7) == 1 )
{
*v3 = v7[0];
++v4;
++v3;
if ( getchar() == 10 )
{
summary((__int64)v6, v4);
putchar(10);
goto LABEL_2;
}
}
summary((__int64)v6, v4);
putchar(10);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_DWORD *v3; // rbx
signed int v4; // ebp
_BYTE v6[204]; // [rsp+0h] [rbp-E8h] BYREF
int v7[7]; // [rsp+CCh] [rbp-1Ch] BYREF

puts("Enter up to 50 numbers to get a summary of them:");
LABEL_2:
v3 = v6;
v4 = 0;
while ( scanf("%d", v7) == 1 )
{
*v3 = v7[0];
++v4;
++v3;
if ( getchar() == 10 )
{
func8((__int64)v6, v4);
putchar(10);
goto LABEL_2;
}
}
func8((__int64)v6, v4);
putchar(10);
return 0;
}

original_code:
int main(){
 int k;
 int length=0;
 int userArray[50];
 printf("Enter up to 50 numbers to get a summary of them:\n");
 while((scanf("%d", &k) ==1)){
  userArray[length] = k;
  length++;
  if(getchar() == '\n'){
   summary(userArray, length);
   length = 0;
   printf("\n");
  }
 }
 summary(userArray, length);
 printf("\n");
 return 0;
}

canonical_original_code:
int main(){
 int k;
 int length = 0;
 int userArray[50];
 printf("Enter up to 50 numbers to get a summary of them:\n");
 while((scanf("%d", &k) ==1)){
  userArray[length] = k;
  length++;
  if(getchar() == '\n'){
   summary(userArray, length);
   length = 0;
   printf("\n");
  }
 }
 summary(userArray, length);
 printf("\n");
 return 0;
}

variable_types: k int

variable_types:
userArray
int [50]


repo: raspberry-alpha-omega/corn-mainline

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

result = raspi_set_gpio_level(17LL, a1);
p17 = a1;
return result;
}

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

result = raspi_set_gpio_level(17LL, a1);
p17 = a1;
return result;
}

original_code:
void pin17(enum gpio_level level) {
  raspi_set_gpio_level(17, level);
  p17 = level;
}

variable_types: enum gpio_level { GPIO_LOW=0, GPIO_HIGH=1 }


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

result = raspi_set_gpio_level(21LL, a1);
p21 = a1;
return result;
}

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

result = raspi_set_gpio_level(21LL, a1);
p21 = a1;
return result;
}

original_code:
void pin21(enum gpio_level level) {
  raspi_set_gpio_level(21, level);
  p21 = level;
}

user_defined_types: enum gpio_level { GPIO_LOW=0, GPIO_HIGH=1 }


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

result = raspi_set_gpio_level(22LL, a1);
p22 = a1;
return result;
}

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

result = raspi_set_gpio_level(22LL, a1);
p22 = a1;
return result;
}

original_code:
void pin22(enum gpio_level level) {
  raspi_set_gpio_level(22, level);
  p22 = level;
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
unsigned int v4; // ebp
unsigned int v5; // er12
unsigned int v6; // er13

puts("tests go here!");
pwm_init(15LL, argv);
pwm_set_slot(0LL, pin17);
pwm_set_cycle(0LL, 8LL);
pwm_set_slot(1LL, pin21);
pwm_set_cycle(1LL, 4LL);
pwm_set_slot(2LL, pin22);
pwm_set_cycle(2LL, 2LL);
v3 = 16;
v4 = 0;
v5 = 0;
v6 = 0;
do
{
pwm_tick();
v6 += p17 == 1;
v5 += p21 == 1;
v4 += p22 == 1;
--v3;
}
while ( v3 );
printf("p17, cycle=%x total=%d\n", 8LL, v6);
printf("p21, cycle=%x total=%d\n", 4LL, v5);
printf("p22, cycle=%x total=%d\n", 2LL, v4);
return 0;
}

original_code:
int main() {
  puts("tests go here!");
  pwm_init(0b1111);
  pwm_set_slot(0, &pin17);
  pwm_set_cycle(0, 0b1000);
  pwm_set_slot(1, &pin21);
  pwm_set_cycle(1, 0b0100);
  pwm_set_slot(2, &pin22);
  pwm_set_cycle(2, 0b0010);
  int p17_total = 0;
  int p21_total = 0;
  int p22_total = 0;
  for (int i = 0; i < 16; ++i) {
    pwm_tick();
    if (p17 == GPIO_HIGH) ++ p17_total;
    if (p21 == GPIO_HIGH) ++ p21_total;
    if (p22 == GPIO_HIGH) ++ p22_total;
  }
  printf("p17, cycle=%x total=%d\n", 0b1000, p17_total);
  printf("p21, cycle=%x total=%d\n", 0b0100, p21_total);
  printf("p22, cycle=%x total=%d\n", 0b0010, p22_total);
}


decompiled_code:
void __fastcall morse_set_switch(void *a1)
{
switcher = a1;
}

canonical_decompiled_code:
void __fastcall func0(void *a1)
{
switcher = a1;
}

original_code:
void morse_set_switch(switch_fn fn) {
  switcher = fn;
}

canonical_original_code:
void morse_set_switch(void (*fn)(enum gpio_level)) {
  switcher = fn;
}

variable_types: void (*)(enum gpio_level { GPIO_LOW=0, GPIO_HIGH=1 })


decompiled_code:
__int64 __fastcall switch_off(unsigned int a1)
{
switcher(0LL);
return raspi_timer_wait(a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
switcher(0LL);
return raspi_timer_wait(a1);
}

original_code:
void switch_off(int duration) {
  switcher(GPIO_LOW);
  raspi_timer_wait(duration);
}

variable_types: duration int


decompiled_code:
__int64 __fastcall switch_on(unsigned int a1)
{
switcher(1LL);
return raspi_timer_wait(a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
switcher(1LL);
return raspi_timer_wait(a1);
}

original_code:
void switch_on(int duration) {
  switcher(GPIO_HIGH);
  raspi_timer_wait(duration);
}


decompiled_code:
__int64 __fastcall morse_glyph(char a1)
{
if ( a1 == 45 )
{
switch_on(0x6DDD0u);
}
else if ( a1 == 46 )
{
switch_on(0x249F0u);
}
else
{
raspi_timer_wait(1050000LL);
}
return switch_off(0x249F0u);
}

canonical_decompiled_code:
__int64 __fastcall func3(char a1)
{
if ( a1 == 45 )
{
func2(0x6DDD0u);
}
else if ( a1 == 46 )
{
func2(0x249F0u);
}
else
{
raspi_timer_wait(1050000LL);
}
return func1(0x249F0u);
}

original_code:
void morse_glyph(char c) {
  switch(c) {
  case '.':
    switch_on(150000);
    break;
  case '-':
    switch_on((150000 * 3));
    break;
  default:
    raspi_timer_wait((150000 * 7));
    break;
  }
  switch_off(150000);
}

variable_types: c char


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

result = 0LL;
while ( LOBYTE(code[2 * result]) != a1 )
{
if ( ++result == 37 )
return result;
}
return morse_symbol((char *)code[2 * (int)result + 1]);
}

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

result = 0LL;
while ( LOBYTE(code[2 * result]) != a1 )
{
if ( ++result == 37 )
return result;
}
return func4((char *)code[2 * (int)result + 1]);
}

original_code:
void morse_char(char c) {
  for (int i = 0; i < nchars; ++i) {
    if (c == code[i].letter) {
      morse_symbol(code[i].symbol);
      return;
    }
  }
}


decompiled_code:
__int64 __fastcall morse_string(char *a1)
{
char *v1; // rbx
char v2; // di
__int64 result; // rax

v1 = a1;
v2 = *a1;
if ( v2 )
{
do
{
result = morse_char(v2);
v2 = *++v1;
}
while ( *v1 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *a1)
{
char *v1; // rbx
char v2; // di
__int64 result; // rax

v1 = a1;
v2 = *a1;
if ( v2 )
{
do
{
result = func5(v2);
v2 = *++v1;
}
while ( *v1 );
}
return result;
}

original_code:
void morse_string(const char* message) {
  for (char c = *message; c != 0; c = *++message) {
    morse_char(c);
  }
}

canonical_original_code:
void morse_string(char *message) {
  for (char c = *message; c != 0; c = *++message) {
    morse_char(c);
  }
}

variable_types:
message
char *


decompiled_code:
__int64 __fastcall flash_dots(int a1)
{
int i; // ebx

if ( a1 > 0 )
{
for ( i = 0; i != a1; ++i )
morse_glyph(46);
}
return raspi_timer_wait(450000LL);
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
int i; // ebx

if ( a1 > 0 )
{
for ( i = 0; i != a1; ++i )
func3(46);
}
return raspi_timer_wait(450000LL);
}

original_code:
void flash_dots(int ndots) {
  for (int i = 0; i < ndots; ++i) {
    morse_glyph('.');
  }
  raspi_timer_wait((150000 * 3));
}

variable_types: ndots int


halt

decompiled_code:
void __fastcall __noreturn halt(char *a1)
{
while ( 1 )
morse_string(a1);
}

canonical_decompiled_code:
void __fastcall __noreturn func8(char *a1)
{
while ( 1 )
func6(a1);
}

original_code:
void halt(const char* message) {
  for (;;) {
    morse_string(message);
  }
}

canonical_original_code:
void halt(char *message) {
  for (;;) {
    morse_string(message);
  }
}


decompiled_code:
int __fastcall raspi_set_gpio_direction(unsigned int a1, unsigned int a2)
{
return printf("%s: pin=%d dir=%d\n", "raspi_set_gpio_direction", a1, a2);
}

canonical_decompiled_code:
int __fastcall func0(unsigned int a1, unsigned int a2)
{
return printf("%s: pin=%d dir=%d\n", "raspi_set_gpio_direction", a1, a2);
}

original_code:
void raspi_set_gpio_direction(int pin, enum gpio_direction direction) {
  printf("%s: pin=%d dir=%d\n", __FUNCTION__, pin, direction);
}

variable_types: pin int

variable_types: enum gpio_direction { GPIO_INPUT=0, GPIO_OUTPUT=1 }


decompiled_code:
int __fastcall raspi_set_gpio_level(unsigned int a1, unsigned int a2)
{
return printf("%s: pin=%d level=%d\n", "raspi_set_gpio_level", a1, a2);
}

canonical_decompiled_code:
int __fastcall func1(unsigned int a1, unsigned int a2)
{
return printf("%s: pin=%d level=%d\n", "raspi_set_gpio_level", a1, a2);
}

original_code:
void raspi_set_gpio_level(int pin, enum gpio_level level) {
  printf("%s: pin=%d level=%d\n", __FUNCTION__, pin, level);
}


decompiled_code:
__int64 __fastcall raspi_get_gpio_level(unsigned int a1)
{
printf("%s: pin=%d\n", "raspi_get_gpio_level", a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
printf("%s: pin=%d\n", "raspi_get_gpio_level", a1);
return 0LL;
}

original_code:
enum gpio_level raspi_get_gpio_level(int pin) {
  printf("%s: pin=%d\n", __FUNCTION__, pin);
  return GPIO_LOW;
}

return_type: enum gpio_level { GPIO_LOW=0, GPIO_HIGH=1 }


decompiled_code:
int __fastcall raspi_okled_set(unsigned int a1)
{
return printf("%s: level=%d\n", "raspi_okled_set", a1);
}

canonical_decompiled_code:
int __fastcall func3(unsigned int a1)
{
return printf("%s: level=%d\n", "raspi_okled_set", a1);
}

original_code:
void raspi_okled_set(enum gpio_level level) {
  printf("%s: level=%d\n", __FUNCTION__, level);
}


decompiled_code:
int __fastcall raspi_timer_wait(unsigned int a1)
{
return printf("%s: usec=%d\n", "raspi_timer_wait", a1);
}

canonical_decompiled_code:
int __fastcall func4(unsigned int a1)
{
return printf("%s: usec=%d\n", "raspi_timer_wait", a1);
}

original_code:
void raspi_timer_wait(int usec) {
  printf("%s: usec=%d\n", __FUNCTION__, usec);
}

variable_types: usec int


decompiled_code:
int __fastcall raspi_mini_uart_send_char(char a1)
{
return printf("%s: c=%c\n", "raspi_mini_uart_send_char", (unsigned int)a1);
}

canonical_decompiled_code:
int __fastcall func5(char a1)
{
return printf("%s: c=%c\n", "raspi_mini_uart_send_char", (unsigned int)a1);
}

original_code:
void raspi_mini_uart_send_char(char c) {
  printf("%s: c=%c\n", __FUNCTION__, c);
}


decompiled_code:
int __fastcall raspi_mini_uart_send_string(__int64 a1)
{
return printf("%s: s=[%c]\n", "raspi_mini_uart_send_string", a1);
}

canonical_decompiled_code:
int __fastcall func6(__int64 a1)
{
return printf("%s: s=[%c]\n", "raspi_mini_uart_send_string", a1);
}

original_code:
void raspi_mini_uart_send_string(const char* s) {
  printf("%s: s=[%c]\n", __FUNCTION__, s);
}

canonical_original_code:
void raspi_mini_uart_send_string(char *s) {
  printf("%s: s=[%c]\n", __FUNCTION__, s);
}

variable_types:
s
char *


decompiled_code:
int __fastcall raspi_mini_uart_send_hex(unsigned int a1)
{
return printf("%s: hex=[%08x]\n", "raspi_mini_uart_send_hex", a1);
}

canonical_decompiled_code:
int __fastcall func7(unsigned int a1)
{
return printf("%s: hex=[%08x]\n", "raspi_mini_uart_send_hex", a1);
}

original_code:
void raspi_mini_uart_send_hex(uint32_t n) {
  printf("%s: hex=[%08x]\n", __FUNCTION__, n);
}

canonical_original_code:
void raspi_mini_uart_send_hex(unsigned int n) {
  printf("%s: hex=[%08x]\n", __FUNCTION__, n);
}

variable_types:
n
unsigned int


decompiled_code:
int raspi_mini_uart_send_newline()
{
return printf("%s:\n", "raspi_mini_uart_send_newline");
}

canonical_decompiled_code:
int func8()
{
return printf("%s:\n", "raspi_mini_uart_send_newline");
}

original_code:
void raspi_mini_uart_send_newline(void) {
  printf("%s:\n", __FUNCTION__);
}

canonical_original_code:
void raspi_mini_uart_send_newline() {
  printf("%s:\n", __FUNCTION__);
}


decompiled_code:
int raspi_okled_init()
{
return printf("%s:\n", "raspi_okled_init");
}

canonical_decompiled_code:
int func9()
{
return printf("%s:\n", "raspi_okled_init");
}

original_code:
void raspi_okled_init() {
  printf("%s:\n", __FUNCTION__);
}


decompiled_code:
int raspi_mini_uart_init()
{
return printf("%s:\n", "raspi_mini_uart_init");
}

canonical_decompiled_code:
int func10()
{
return printf("%s:\n", "raspi_mini_uart_init");
}

original_code:
void raspi_mini_uart_init() {
  printf("%s:\n", __FUNCTION__);
}


decompiled_code:
__int64 *pwm_init()
{
__int64 *result; // rax

result = &slots;
do
{
*result = 0LL;
*((_DWORD *)result + 2) = 0;
*((_DWORD *)result + 3) = 0;
result += 2;
}
while ( result != (__int64 *)&unk_180 );
pwm_clock = 0;
pwm_mask = 255;
return result;
}

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

result = &slots;
do
{
*result = 0LL;
*((_DWORD *)result + 2) = 0;
*((_DWORD *)result + 3) = 0;
result += 2;
}
while ( result != (__int64 *)&unk_180 );
pwm_clock = 0;
pwm_mask = 255;
return result;
}

original_code:
void pwm_init(uint32_t mask) {
  for (int i = 0; i < 8; ++i) {
    slots[i].fn = 0;
    slots[i].cycle = 0;
    slots[i].state = 0;
  }
  pwm_clock = 0;
  pwm_mask = 0xFF;
}

canonical_original_code:
void pwm_init(unsigned int mask) {
  for (int i = 0; i < 8; ++i) {
    slots[i].fn = 0;
    slots[i].cycle = 0;
    slots[i].state = 0;
  }
  pwm_clock = 0;
  pwm_mask = 0xFF;
}

variable_types:
mask
unsigned int


decompiled_code:
void __fastcall pwm_set_slot(int a1, __int64 a2)
{
slots[2 * a1] = a2;
}

canonical_decompiled_code:
void __fastcall func1(int a1, __int64 a2)
{
slots[2 * a1] = a2;
}

original_code:
void pwm_set_slot(int slot, switch_fn fn) {
  slots[slot].fn = fn;
}

canonical_original_code:
void pwm_set_slot(int slot, void (*fn)(enum gpio_level)) {
  slots[slot].fn = fn;
}

variable_types: slot int


decompiled_code:
void __fastcall pwm_set_cycle(int a1, int a2)
{
dword_108[4 * a1] = a2;
}

canonical_decompiled_code:
void __fastcall func2(int a1, int a2)
{
dword_108[4 * a1] = a2;
}

original_code:
void pwm_set_cycle(int slot, uint32_t bits) {
  slots[slot].cycle = bits;
}

canonical_original_code:
void pwm_set_cycle(int slot, unsigned int bits) {
  slots[slot].cycle = bits;
}

variable_types:
bits
unsigned int


RegRead

repo: raspberry-alpha-omega/ledborg-source

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

if ( *(_QWORD *)a1 == -1LL )
{
printf("RegRead failed due to an unmapped base (%s)!\n", (const char *)(a1 + 16));
result = 0LL;
}
else if ( *(_DWORD *)(a1 + 12) <= a2 )
{
printf("RegRead failed due to address out of bounds (%s, 0x%08X)!\n", (const char *)(a1 + 16), a2);
result = 0LL;
}
else
{
result = *(unsigned int *)(*(_QWORD *)a1 + a2);
}
return result;
}

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

if ( *(_QWORD *)a1 == -1LL )
{
printf("RegRead failed due to an unmapped base (%s)!\n", (const char *)(a1 + 16));
result = 0LL;
}
else if ( *(_DWORD *)(a1 + 12) <= a2 )
{
printf("RegRead failed due to address out of bounds (%s, 0x%08X)!\n", (const char *)(a1 + 16), a2);
result = 0LL;
}
else
{
result = *(unsigned int *)(*(_QWORD *)a1 + a2);
}
return result;
}

original_code:
unsigned int RegRead(raw_memory *base, unsigned int address) {
 if (base->accessor == ((void *) -1)) {
  printf("RegRead failed due to an unmapped base (%s)!\n", base->name);
  return 0;
 } else if (address >= base->length) {
  printf("RegRead failed due to address out of bounds (%s, 0x%08X)!\n", base->name, address);
  return 0;
 } else {
  unsigned int value = *((unsigned int *)(base->accessor + address));
  return value;
 }
}

canonical_original_code:
unsigned int RegRead(struct raw_memory *base, unsigned int address) {
 if (base->accessor == ((void *) -1)) {
  printf("RegRead failed due to an unmapped base (%s)!\n", base->name);
  return 0;
 } else if (address >= base->length) {
  printf("RegRead failed due to address out of bounds (%s, 0x%08X)!\n", base->name, address);
  return 0;
 } else {
  unsigned int value = *((unsigned int *)(base->accessor + address));
  return value;
 }
}

variable_types: struct raw_memory { void *accessor; unsigned int offset; unsigned int length; char name[-1]; } *

variable_types:
address
unsigned int

variable_types:
value
unsigned int


RegWrite

decompiled_code:
int __fastcall RegWrite(__int64 a1, unsigned int a2, int a3, int a4)
{
__int64 v6; // rax
int v7; // eax

if ( *(_QWORD *)a1 == -1LL )
{
LODWORD(v6) = printf("RegWrite failed due to an unmapped base (%s)!\n", (const char *)(a1 + 16));
}
else if ( *(_DWORD *)(a1 + 12) <= a2 )
{
LODWORD(v6) = printf("RegWrite failed due to address out of bounds (%s, 0x%08X)!\n", (const char *)(a1 + 16), a2);
}
else
{
if ( a3 != -1 )
{
v7 = RegRead(a1, a2);
a4 = a3 & (v7 ^ a4) ^ v7;
}
v6 = *(_QWORD *)a1 + *(unsigned int *)(a1 + 8);
*(_DWORD *)(v6 + a2) = a4;
}
return v6;
}

canonical_decompiled_code:
int __fastcall func1(__int64 a1, unsigned int a2, int a3, int a4)
{
__int64 v6; // rax
int v7; // eax

if ( *(_QWORD *)a1 == -1LL )
{
LODWORD(v6) = printf("RegWrite failed due to an unmapped base (%s)!\n", (const char *)(a1 + 16));
}
else if ( *(_DWORD *)(a1 + 12) <= a2 )
{
LODWORD(v6) = printf("RegWrite failed due to address out of bounds (%s, 0x%08X)!\n", (const char *)(a1 + 16), a2);
}
else
{
if ( a3 != -1 )
{
v7 = func0(a1, a2);
a4 = a3 & (v7 ^ a4) ^ v7;
}
v6 = *(_QWORD *)a1 + *(unsigned int *)(a1 + 8);
*(_DWORD *)(v6 + a2) = a4;
}
return v6;
}

original_code:
void RegWrite(raw_memory *base, unsigned int address, unsigned int mask, unsigned int value) {
 if (base->accessor == ((void *) -1)) {
  printf("RegWrite failed due to an unmapped base (%s)!\n", base->name);
 } else if (address >= base->length) {
  printf("RegWrite failed due to address out of bounds (%s, 0x%08X)!\n", base->name, address);
 } else {
  if (mask != ((0x7fffffff * 2U + 1U))) {
   unsigned int read = RegRead(base, address);
   value &= mask;
   value |= (~mask) & read;
  }
  *((unsigned int *)(base->accessor + base->offset + address)) = value;
 }
}

canonical_original_code:
void RegWrite(struct raw_memory *base, unsigned int address, unsigned int mask, unsigned int value) {
 if (base->accessor == ((void *) -1)) {
  printf("RegWrite failed due to an unmapped base (%s)!\n", base->name);
 } else if (address >= base->length) {
  printf("RegWrite failed due to address out of bounds (%s, 0x%08X)!\n", base->name, address);
 } else {
  if (mask != ((0x7fffffff * 2U + 1U))) {
   unsigned int read = RegRead(base, address);
   value &= mask;
   value |= (~mask) & read;
  }
  *((unsigned int *)(base->accessor + base->offset + address)) = value;
 }
}

variable_types:
read
unsigned int

user_defined_types: struct raw_memory { void *accessor; unsigned int offset; unsigned int length; char name[-1]; }


UnmapMemory

decompiled_code:
int __fastcall UnmapMemory(__int64 a1)
{
void *v2; // rdi
int result; // eax

v2 = *(void **)a1;
if ( v2 != (void *)-1LL )
{
result = munmap(v2, (unsigned int)(*(_DWORD *)(a1 + 8) + *(_DWORD *)(a1 + 12)));
*(_QWORD *)a1 = -1LL;
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(__int64 a1)
{
void *v2; // rdi
int result; // eax

v2 = *(void **)a1;
if ( v2 != (void *)-1LL )
{
result = munmap(v2, (unsigned int)(*(_DWORD *)(a1 + 8) + *(_DWORD *)(a1 + 12)));
*(_QWORD *)a1 = -1LL;
}
return result;
}

original_code:
void UnmapMemory(raw_memory *base) {
 if (base->accessor != ((void *) -1)) {
  munmap(base->accessor, base->offset + base->length);
  base->accessor = ((void *) -1);
 } else {
 }
}

canonical_original_code:
void UnmapMemory(struct raw_memory *base) {
 if (base->accessor != ((void *) -1)) {
  munmap(base->accessor, base->offset + base->length);
  base->accessor = ((void *) -1);
 } else {
 }
}


shutdownGPIO

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

if ( rawMemGPIO != -1 )
result = UnmapMemory((__int64)&rawMemGPIO);
if ( memFile )
{
result = close(memFile);
memFile = 0;
}
return result;
}

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

if ( rawMemGPIO != -1 )
result = func2((__int64)&rawMemGPIO);
if ( memFile )
{
result = close(memFile);
memFile = 0;
}
return result;
}

original_code:
void shutdownGPIO(void) {
 if (rawMemGPIO.accessor != ((void *) -1)) {
  UnmapMemory(&rawMemGPIO);
 }
 if (memFile != ((int)0)) {
  close(memFile);
  memFile = ((int)0);
 }
}

canonical_original_code:
void shutdownGPIO() {
 if (rawMemGPIO.accessor != ((void *) -1)) {
  UnmapMemory(&rawMemGPIO);
 }
 if (memFile != ((int)0)) {
  close(memFile);
  memFile = ((int)0);
 }
}


MapMemory

decompiled_code:
__int64 __fastcall MapMemory(__int64 a1, int a2, unsigned int a3, __int64 a4)
{
unsigned int v6; // eax
unsigned int v7; // er9
void *v8; // rax
unsigned int v9; // edx

if ( *(_QWORD *)a1 != -1LL )
UnmapMemory(a1);
v6 = getpagesize();
v7 = a3 / v6 * v6;
*(_DWORD *)(a1 + 8) = v7;
*(_DWORD *)(a1 + 12) = a4;
v8 = mmap(0LL, a3 % v6 + a4, 3, 1, a2, v7);
*(_QWORD *)a1 = v8;
v9 = 0;
if ( v8 != (void *)-1LL )
{
*(_DWORD *)(a1 + 8) = a3 - *(_DWORD *)(a1 + 8);
v9 = 1;
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, int a2, unsigned int a3, __int64 a4)
{
unsigned int v6; // eax
unsigned int v7; // er9
void *v8; // rax
unsigned int v9; // edx

if ( *(_QWORD *)a1 != -1LL )
func2(a1);
v6 = getpagesize();
v7 = a3 / v6 * v6;
*(_DWORD *)(a1 + 8) = v7;
*(_DWORD *)(a1 + 12) = a4;
v8 = mmap(0LL, a3 % v6 + a4, 3, 1, a2, v7);
*(_QWORD *)a1 = v8;
v9 = 0;
if ( v8 != (void *)-1LL )
{
*(_DWORD *)(a1 + 8) = a3 - *(_DWORD *)(a1 + 8);
v9 = 1;
}
return v9;
}

original_code:
BOOL MapMemory(raw_memory *base, int device, unsigned int startAddress, size_t size) {
 if (base->accessor != ((void *) -1)) {
  UnmapMemory(base);
 }
 base->offset = (unsigned int)(startAddress / getpagesize()) * getpagesize();
 base->length = (unsigned int)size;
 size += (size_t)(startAddress - base->offset);
 base->accessor = mmap(((void *)0), size, 0x1|0x2, 0x01, device, base->offset);
 if (base->accessor == ((void *) -1)) {
  return (0);
 } else {
  base->offset = startAddress - base->offset;
  return (!(0));
 }
}

canonical_original_code:
unsigned char MapMemory(struct raw_memory *base, int device, unsigned int startAddress, unsigned long size) {
 if (base->accessor != ((void *) -1)) {
  UnmapMemory(base);
 }
 base->offset = (unsigned int)(startAddress / getpagesize()) * getpagesize();
 base->length = (unsigned int)size;
 size += (unsigned long)(startAddress - base->offset);
 base->accessor = mmap(((void *)0), size, 0x1|0x2, 0x01, device, base->offset);
 if (base->accessor == ((void *) -1)) {
  return (0);
 } else {
  base->offset = startAddress - base->offset;
  return (!(0));
 }
}

variable_types: device int

variable_types:
startAddress
unsigned int

variable_types:
size
unsigned long

return_type: unsigned char


initialiseGPIO

decompiled_code:
__int64 initialiseGPIO()
{
int v0; // esi
unsigned int v1; // ebx

v0 = memFile;
if ( memFile || (v0 = open("/dev/mem", 1052674), (memFile = v0) != 0) )
{
v1 = MapMemory((__int64)&rawMemGPIO, v0, 0x20200000u, 180LL);
if ( (_BYTE)v1 )
v1 = 1;
else
puts("Failed to map GPIO device memory (are we root?) !");
}
else
{
puts("Failed to open /dev/mem (are we root?) !");
v1 = 0;
}
return v1;
}

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

v0 = memFile;
if ( memFile || (v0 = open("/dev/mem", 1052674), (memFile = v0) != 0) )
{
v1 = func4((__int64)&rawMemGPIO, v0, 0x20200000u, 180LL);
if ( (_BYTE)v1 )
v1 = 1;
else
puts("Failed to map GPIO device memory (are we root?) !");
}
else
{
puts("Failed to open /dev/mem (are we root?) !");
v1 = 0;
}
return v1;
}

original_code:
BOOL initialiseGPIO(void)
{
 if (memFile == ((int)0)) memFile = open("/dev/mem", 02 | 04010000);
 if (memFile == ((int)0)) {
  printf("Failed to open /dev/mem (are we root?) !\n");
  return (0);
 }
 if (!MapMemory(&rawMemGPIO, memFile, (0x20200000), (180))) {
  printf("Failed to map GPIO device memory (are we root?) !\n");
  return (0);
 }
 return (!(0));
}

canonical_original_code:
unsigned char initialiseGPIO()
{
 if (memFile == ((int)0)) memFile = open("/dev/mem", 02 | 04010000);
 if (memFile == ((int)0)) {
  printf("Failed to open /dev/mem (are we root?) !\n");
  return (0);
 }
 if (!MapMemory(&rawMemGPIO, memFile, (0x20200000), (180))) {
  printf("Failed to map GPIO device memory (are we root?) !\n");
  return (0);
 }
 return (!(0));
}