decompiled_code:
__int64 __fastcall lapic_timer_wait(__int64 a1)
{
__int64 v1; // rbp
unsigned int v2; // eax

idt_intgate((char *)&idt_data + 1024, &lapic_timer_wait_handler, 8LL, 0LL);
v1 = info_cpu;
v2 = lapic_id();
lapic_timer_update(a1 * (unsigned __int64)*(unsigned int *)(v1 + 18LL * v2 + 10) / 0xF4240, 0x40u, 0, 0);
lapic_timer_wait_worker();
return idt_setup_loader();
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
__int64 v1; // rbp
unsigned int v2; // eax

idt_intgate((char *)&idt_data + 1024, &lapic_timer_wait_handler, 8LL, 0LL);
v1 = info_cpu;
v2 = func3();
func6(a1 * (unsigned __int64)*(unsigned int *)(v1 + 18LL * v2 + 10) / 0xF4240, 0x40u, 0, 0);
lapic_timer_wait_worker();
return idt_setup_loader();
}

original_code:
void lapic_timer_wait(uint64_t time)
{
    extern void lapic_timer_wait_handler(void);
    extern void lapic_timer_wait_worker(void);
    uint8_t vector = 0x40;
    idt_intgate(&idt_data[vector], (uintptr_t) &lapic_timer_wait_handler, 0x8, 0x0);
    uint64_t ticks = ((uint64_t) info_cpu[lapic_id()].lapic_timer_freq * time) / 1000000;
    lapic_timer_update(ticks, vector, 0, 0);
    lapic_timer_wait_worker();
    idt_setup_loader();
}

canonical_original_code:
void lapic_timer_wait(unsigned long long time)
{
    extern void lapic_timer_wait_handler();
    extern void lapic_timer_wait_worker();
    unsigned char vector = 0x40;
    idt_intgate(&idt_data[vector], (unsigned long long) &lapic_timer_wait_handler, 0x8, 0x0);
    unsigned long long ticks = ((uint64_t) info_cpu[lapic_id()].lapic_timer_freq * time) / 1000000;
    lapic_timer_update(ticks, vector, 0, 0);
    lapic_timer_wait_worker();
    idt_setup_loader();
}

variable_types:
time
unsigned long long

variable_types:
ticks
unsigned long long


repo: zrho/Hydrogen

decompiled_code:
void __noreturn idt_fault_gp()
{
_disable();
screen_write("PANIC: General Protection fault.", 0LL, 3LL);
while ( 1 )
;
}

canonical_decompiled_code:
void __noreturn func0()
{
_disable();
screen_write("PANIC: General Protection fault.", 0LL, 3LL);
while ( 1 )
;
}

original_code:
static void idt_fault_gp(void)
{
    asm volatile ("cli");
    do { screen_write("PANIC: " "General Protection fault.", 0, 3); while (1); } while (0);;
}

canonical_original_code:
static void idt_fault_gp()
{
    asm volatile ("cli");
    do { screen_write("PANIC: " "General Protection fault.", 0, 3); while (1); } while (0);;
}

return_type: void


decompiled_code:
void __noreturn idt_fault_pf()
{
_disable();
screen_write("PANIC: Page fault.", 0LL, 3LL);
while ( 1 )
;
}

canonical_decompiled_code:
void __noreturn func1()
{
_disable();
screen_write("PANIC: Page fault.", 0LL, 3LL);
while ( 1 )
;
}

original_code:
static void idt_fault_pf(void)
{
    asm volatile ("cli");
    do { screen_write("PANIC: " "Page fault.", 0, 3); while (1); } while (0);;
}

canonical_original_code:
static void idt_fault_pf()
{
    asm volatile ("cli");
    do { screen_write("PANIC: " "Page fault.", 0, 3); while (1); } while (0);;
}


original_code:
void idt_intgate(idt_entry_t *entry, uintptr_t handler, uint16_t cs, uint8_t dpl)
{
    entry->handlerLow = handler & 0xFFFF;
    entry->handlerMiddle = (handler >> 16) & 0xFFFF;
    entry->handlerHigh = (handler >> 32) & 0xFFFFFFFF;
    entry->cs = cs;
    entry->flags = 0xE;
    entry->flags |= (dpl & 0b11) << 5;
    entry->flags |= (1 << 7);
    entry->ist = 0;
    entry->zero = 0;
}

canonical_original_code:
void idt_intgate(struct idt_entry *entry, unsigned long long handler, unsigned short cs, unsigned char dpl)
{
    entry->handlerLow = handler & 0xFFFF;
    entry->handlerMiddle = (handler >> 16) & 0xFFFF;
    entry->handlerHigh = (handler >> 32) & 0xFFFFFFFF;
    entry->cs = cs;
    entry->flags = 0xE;
    entry->flags |= (dpl & 0b11) << 5;
    entry->flags |= (1 << 7);
    entry->ist = 0;
    entry->zero = 0;
}

variable_types: struct idt_entry { unsigned short handlerLow; unsigned short cs; unsigned char ist; unsigned char flags; unsigned short handlerMiddle; unsigned int handlerHigh; unsigned int zero; } *

variable_types:
handler
unsigned long long

variable_types:
cs
unsigned short

variable_types:
dpl
unsigned char


decompiled_code:
unsigned __int64 idt_setup_loader()
{
char *v0; // rbx

v0 = (char *)&idt_data;
do
{
idt_intgate((__int64)v0, (unsigned __int64)&idt_null_handler, 8, 0);
v0 += 16;
}
while ( v0 != (char *)&idt_data + 4096 );
idt_intgate((__int64)&idt_data + 224, (unsigned __int64)idt_fault_pf, 8, 0);
return idt_intgate((__int64)&idt_data + 208, (unsigned __int64)idt_fault_gp, 8, 0);
}

canonical_decompiled_code:
unsigned __int64 func4()
{
char *v0; // rbx

v0 = (char *)&idt_data;
do
{
func3((__int64)v0, (unsigned __int64)&idt_null_handler, 8, 0);
v0 += 16;
}
while ( v0 != (char *)&idt_data + 4096 );
func3((__int64)&idt_data + 224, (unsigned __int64)idt_fault_pf, 8, 0);
return func3((__int64)&idt_data + 208, (unsigned __int64)idt_fault_gp, 8, 0);
}

original_code:
void idt_setup_loader(void)
{
    size_t i;
    for (i = 0; i < 256; ++i) {
        idt_intgate(&idt_data[i], (uintptr_t) &idt_null_handler, 0x8, 0x0);
    }
    idt_intgate(&idt_data[14], (uintptr_t) &idt_fault_pf, 0x8, 0x0);
    idt_intgate(&idt_data[13], (uintptr_t) &idt_fault_gp, 0x8, 0x0);
}

canonical_original_code:
void idt_setup_loader()
{
    unsigned long long i;
    for (i = 0; i < 256; ++i) {
        idt_intgate(&idt_data[i], (unsigned long long) &idt_null_handler, 0x8, 0x0);
    }
    idt_intgate(&idt_data[14], (unsigned long long) &idt_fault_pf, 0x8, 0x0);
    idt_intgate(&idt_data[13], (unsigned long long) &idt_fault_gp, 0x8, 0x0);
}

variable_types:
i
unsigned long long


decompiled_code:
unsigned __int64 idt_setup_kernel()
{
unsigned __int64 v0; // rbp
__int64 i; // rbx
unsigned __int64 result; // rax
unsigned __int64 v3; // rsi

v0 = *(_QWORD *)(kernel_header + 56LL);
for ( i = 0LL; i != 256; ++i )
{
v3 = v0;
if ( v0 )
v3 = *(_QWORD *)(v0 + 8 * i);
result = idt_intgate((__int64)&idt_data + 16 * i, v3, 8, 0);
}
return result;
}

canonical_decompiled_code:
unsigned __int64 func5()
{
unsigned __int64 v0; // rbp
__int64 i; // rbx
unsigned __int64 result; // rax
unsigned __int64 v3; // rsi

v0 = *(_QWORD *)(kernel_header + 56LL);
for ( i = 0LL; i != 256; ++i )
{
v3 = v0;
if ( v0 )
v3 = *(_QWORD *)(v0 + 8 * i);
result = func3((__int64)&idt_data + 16 * i, v3, 8, 0);
}
return result;
}

original_code:
void idt_setup_kernel(void)
{
    bool present = (0 != kernel_header->isr_entry_table);
    uint64_t *table = (uint64_t *) kernel_header->isr_entry_table;
    size_t i;
    for (i = 0; i < 256; ++i) {
        uint64_t handler = (present ? table[i] : 0);
        idt_intgate(&idt_data[i], handler, 0x8, 0x0);
    }
}

canonical_original_code:
void idt_setup_kernel()
{
    char present = (0 != kernel_header->isr_entry_table);
    unsigned long long *table = (uint64_t *) kernel_header->isr_entry_table;
    unsigned long long i;
    for (i = 0; i < 256; ++i) {
        unsigned long long handler = (present ? table[i] : 0);
        idt_intgate(&idt_data[i], handler, 0x8, 0x0);
    }
}

variable_types: present char

variable_types:
table
unsigned long long *


decompiled_code:
__int64 __fastcall elf64_hash(char *a1)
{
char v1; // dl
__int64 result; // rax
__int64 v3; // rax

v1 = *a1;
if ( !*a1 )
return 0LL;
result = 0LL;
do
{
++a1;
v3 = v1 + 16 * result;
if ( (v3 & 0xF0000000) != 0 )
v3 ^= (unsigned __int64)((unsigned int)v3 & 0xF0000000) >> 24;
result = v3 & 0xFFFFFFF;
v1 = *a1;
}
while ( *a1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1)
{
char v1; // dl
__int64 result; // rax
__int64 v3; // rax

v1 = *a1;
if ( !*a1 )
return 0LL;
result = 0LL;
do
{
++a1;
v3 = v1 + 16 * result;
if ( (v3 & 0xF0000000) != 0 )
v3 ^= (unsigned __int64)((unsigned int)v3 & 0xF0000000) >> 24;
result = v3 & 0xFFFFFFF;
v1 = *a1;
}
while ( *a1 );
return result;
}

original_code:
uint64_t elf64_hash(const char *name)
{
    uint64_t h = 0, g;
    while (*name) {
        h = (h << 4) + *name++;
        g = h & 0xf0000000;
        if (g != 0)
            h ^= g >> 24;
        h &= 0x0fffffff;
    }
    return h;
}

canonical_original_code:
unsigned long long elf64_hash(char *name)
{
    unsigned long long h = 0; unsigned long long g;
    while (*name) {
        h = (h << 4) + *name++;
        g = h & 0xf0000000;
        if (g != 0)
            h ^= g >> 24;
        h &= 0x0fffffff;
    }
    return h;
}

variable_types:
name
char *

variable_types:
h
unsigned long long

variable_types:
g
unsigned long long

return_type: unsigned long long


original_code:
elf64_shdr_t *elf64_shdr_find(uint32_t type, void *binary)
{
    elf64_ehdr_t *ehdr = (elf64_ehdr_t *) binary;
    elf64_shdr_t *shdr_list = (elf64_shdr_t *) ((uintptr_t) binary + ehdr->e_shoff);
    size_t i;
    for (i = 0; i < ehdr->e_shnum; ++i) {
        elf64_shdr_t *shdr = &shdr_list[i];
        if (type == shdr->sh_type) {
            return shdr;
        }
    }
    return 0;
}

canonical_original_code:
struct elf64_shdr *elf64_shdr_find(unsigned int type, void *binary)
{
    struct elf64_ehdr *ehdr = (elf64_ehdr_t *) binary;
    struct elf64_shdr *shdr_list = (elf64_shdr_t *) ((uintptr_t) binary + ehdr->e_shoff);
    unsigned long long i;
    for (i = 0; i < ehdr->e_shnum; ++i) {
        struct elf64_shdr *shdr = &shdr_list[i];
        if (type == shdr->sh_type) {
            return shdr;
        }
    }
    return 0;
}

variable_types:
type
unsigned int

variable_types:
binary
void *

variable_types: struct elf64_ehdr { unsigned int e_ident_mag; unsigned char e_ident_class; unsigned char e_ident_data; unsigned char e_ident_version; unsigned char e_ident_osabi; unsigned char e_ident_abiversion; unsigned char e_ident_pad[7]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned long long e_entry; unsigned long long e_phoff; unsigned long long e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; } *

variable_types: struct elf64_shdr { unsigned int sh_name; unsigned int sh_type; unsigned long long sh_flags; unsigned long long sh_addr; unsigned long long sh_offset; unsigned long long sh_size; unsigned int sh_link; unsigned int sh_info; unsigned long long sh_addralign; unsigned long long sh_entsize; } *


decompiled_code:
unsigned int *__fastcall elf64_sym_find(char *s2, __int64 a2)
{
__int64 v2; // rax
__int64 v3; // r12
unsigned __int64 v4; // rcx
unsigned __int64 v5; // r15
__int64 v6; // rbp
unsigned int *v7; // rbx
__int64 v9; // [rsp+0h] [rbp-48h]
__int64 v10; // [rsp+8h] [rbp-40h]

v2 = elf64_shdr_find(2, a2);
v3 = v2;
if ( !v2 )
return 0LL;
v10 = *(_QWORD *)(*(_QWORD *)(a2 + 40) + a2 + *(_DWORD *)(v2 + 40) * (unsigned int)*(unsigned __int16 *)(a2 + 58) + 24)
+ a2;
v9 = *(_QWORD *)(v2 + 24) + a2;
v4 = *(_QWORD *)(v2 + 56);
v5 = *(_QWORD *)(v2 + 32) / v4;
if ( *(_QWORD *)(v2 + 32) < v4 )
return 0LL;
v6 = 0LL;
while ( 1 )
{
v7 = (unsigned int *)(v9 + *(_QWORD *)(v3 + 56) * v6);
if ( (unsigned __int8)strcmp((const char *)(v10 + *v7), s2) )
break;
if ( v5 <= ++v6 )
return 0LL;
}
return v7;
}

original_code:
elf64_sym_t *elf64_sym_find(const char *name, void *binary)
{
    elf64_ehdr_t *ehdr = (elf64_ehdr_t *) binary;
    elf64_shdr_t *symtab_hdr = elf64_shdr_find(2, binary);
    if (0 == symtab_hdr) {
        return 0;
    }
    elf64_shdr_t *strtab_hdr = (elf64_shdr_t *) (
            ehdr->e_shoff +
            symtab_hdr->sh_link * ehdr->e_shentsize +
            (uintptr_t) binary);
    char *strtab = (char *) (strtab_hdr->sh_offset + (uintptr_t) binary);
    elf64_sym_t *symtab = (elf64_sym_t *) (symtab_hdr->sh_offset + (uintptr_t) binary);
    size_t i;
    size_t symbol_count = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
    for (i = 0; i < symbol_count; ++i) {
        elf64_sym_t *symbol = (elf64_sym_t *) ((uintptr_t) symtab + i * symtab_hdr->sh_entsize);
        char *symname = &strtab[symbol->st_name];
        if (strcmp(symname, name)) {
            return symbol;
        }
    }
    return 0;
}

canonical_original_code:
struct elf64_sym *elf64_sym_find(char *name, void *binary)
{
    struct elf64_ehdr *ehdr = (elf64_ehdr_t *) binary;
    struct elf64_shdr *symtab_hdr = elf64_shdr_find(2, binary);
    if (0 == symtab_hdr) {
        return 0;
    }
    struct elf64_shdr *strtab_hdr = (elf64_shdr_t *) (
            ehdr->e_shoff +
            symtab_hdr->sh_link * ehdr->e_shentsize +
            (uintptr_t) binary);
    char *strtab = (char *) (strtab_hdr->sh_offset + (uintptr_t) binary);
    struct elf64_sym *symtab = (elf64_sym_t *) (symtab_hdr->sh_offset + (uintptr_t) binary);
    unsigned long long i;
    unsigned long long symbol_count = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
    for (i = 0; i < symbol_count; ++i) {
        struct elf64_sym *symbol = (elf64_sym_t *) ((uintptr_t) symtab + i * symtab_hdr->sh_entsize);
        char *symname = &strtab[symbol->st_name];
        if (strcmp(symname, name)) {
            return symbol;
        }
    }
    return 0;
}

variable_types:
strtab
char *

variable_types: struct elf64_sym { unsigned int st_name; unsigned char st_info; unsigned char st_other; unsigned short st_shndx; unsigned long long st_value; unsigned long long st_size; } *

variable_types:
symname
char *

user_defined_types: struct elf64_ehdr { unsigned int e_ident_mag; unsigned char e_ident_class; unsigned char e_ident_data; unsigned char e_ident_version; unsigned char e_ident_osabi; unsigned char e_ident_abiversion; unsigned char e_ident_pad[7]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned long long e_entry; unsigned long long e_phoff; unsigned long long e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; }

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


original_code:
void elf64_load(void *binary)
{
    elf64_ehdr_t *ehdr = (elf64_ehdr_t *) binary;
    size_t i;
    for (i = 0; i < ehdr->e_phnum; ++i) {
        elf64_phdr_t *phdr = (elf64_phdr_t *) ((uintptr_t) binary + ehdr->e_phoff + i * ehdr->e_phsize);
        if (1 != phdr->p_type)
            continue;
        uintptr_t source = (uintptr_t) binary + phdr->p_offset;
        uintptr_t target = (uintptr_t) heap_alloc(phdr->p_memsz);
        memcpy((void *) target, (void *) source, phdr->p_filesz);
        memset((void *) (target + phdr->p_filesz), 0, phdr->p_memsz - phdr->p_filesz);
        size_t offset;
        for (offset = 0; offset < phdr->p_memsz; offset += 0x1000) {
            page_map(target + offset, phdr->p_vaddr + offset, (1 << 1) | (1 << 8));
        }
    }
}

canonical_original_code:
void elf64_load(void *binary)
{
    struct elf64_ehdr *ehdr = (elf64_ehdr_t *) binary;
    unsigned long long i;
    for (i = 0; i < ehdr->e_phnum; ++i) {
        struct elf64_phdr *phdr = (elf64_phdr_t *) ((uintptr_t) binary + ehdr->e_phoff + i * ehdr->e_phsize);
        if (1 != phdr->p_type)
            continue;
        unsigned long long source = (uintptr_t) binary + phdr->p_offset;
        unsigned long long target = (uintptr_t) heap_alloc(phdr->p_memsz);
        memcpy((void *) target, (void *) source, phdr->p_filesz);
        memset((void *) (target + phdr->p_filesz), 0, phdr->p_memsz - phdr->p_filesz);
        unsigned long long offset;
        for (offset = 0; offset < phdr->p_memsz; offset += 0x1000) {
            page_map(target + offset, phdr->p_vaddr + offset, (1 << 1) | (1 << 8));
        }
    }
}

variable_types: struct elf64_phdr { unsigned int p_type; unsigned int p_flags; unsigned long long p_offset; unsigned long long p_vaddr; unsigned long long p_paddr; unsigned long long p_filesz; unsigned long long p_memsz; unsigned long long p_align; } *

variable_types:
source
unsigned long long

variable_types:
target
unsigned long long

variable_types:
offset
unsigned long long


decompiled_code:
char *__fastcall ui_get_line(char *a1, unsigned __int64 a2)
{
char v2; // al
char *v3; // rdi
unsigned __int64 v4; // rdx
char *v5; // rcx

v2 = *a1;
if ( !a2 || !v2 )
return a1;
v3 = a1 + 1;
v4 = 0LL;
do
{
v4 += v2 == 10;
v5 = v3;
v2 = *v3++;
}
while ( v4 < a2 && v2 );
return v5;
}

canonical_decompiled_code:
char *__fastcall func4(char *a1, unsigned __int64 a2)
{
char v2; // al
char *v3; // rdi
unsigned __int64 v4; // rdx
char *v5; // rcx

v2 = *a1;
if ( !a2 || !v2 )
return a1;
v3 = a1 + 1;
v4 = 0LL;
do
{
v4 += v2 == 10;
v5 = v3;
v2 = *v3++;
}
while ( v4 < a2 && v2 );
return v5;
}

original_code:
static char *ui_get_line(char *string, size_t line) {
    size_t line_cur = 0;
    size_t i;
    for (i = 0; '\0' != string[i] && line_cur < line; ++i) {
        if ('\n' == string[i]) {
            ++line_cur;
        }
    }
    return &string[i];
}

canonical_original_code:
static char *ui_get_line(char *string, unsigned long long line) {
    unsigned long long line_cur = 0;
    unsigned long long i;
    for (i = 0; '\0' != string[i] && line_cur < line; ++i) {
        if ('\n' == string[i]) {
            ++line_cur;
        }
    }
    return &string[i];
}

variable_types:
string
char *

variable_types:
line
unsigned long long

variable_types:
i
unsigned long long

return_type: char *


variable_types: struct ui_page { char *title; char *body; } *


decompiled_code:
__int64 __fastcall ui_display(unsigned __int64 a1, unsigned __int64 a2)
{
unsigned __int8 **v2; // rbx

v2 = (unsigned __int8 **)((char *)&ui_pages
+ 16
* (a1
- (a1 / 5
+ (((0xCCCCCCCCCCCCCCCDLL * (unsigned __int128)a1) >> 64) & 0xFFFFFFFFFFFFFFFCLL))));
screen_clear();
ui_display_header(v2);
return ui_display_body((__int64)v2, a2);
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned __int64 a1, unsigned __int64 a2)
{
unsigned __int8 **v2; // rbx

v2 = (unsigned __int8 **)((char *)&ui_pages
+ 16
* (a1
- (a1 / 5
+ (((0xCCCCCCCCCCCCCCCDLL * (unsigned __int128)a1) >> 64) & 0xFFFFFFFFFFFFFFFCLL))));
func3();
func5(v2);
return func6((__int64)v2, a2);
}

original_code:
void ui_display(size_t page_nr, size_t line)
{
    ui_page_t *page = &ui_pages[page_nr % 5];
    screen_clear();
    ui_display_header(page);
    ui_display_body(page, line);
}

canonical_original_code:
void ui_display(unsigned long long page_nr, unsigned long long line)
{
    struct ui_page *page = &ui_pages[page_nr % 5];
    screen_clear();
    ui_display_header(page);
    ui_display_body(page, line);
}

user_defined_types: struct ui_page { char *title; char *body; }


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

result = ui_page_current;
if ( ui_page_current )
{
ui_line_current = 0LL;
result = ui_display(--ui_page_current, 0LL);
}
return result;
}

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

result = ui_page_current;
if ( ui_page_current )
{
ui_line_current = 0LL;
result = func7(--ui_page_current, 0LL);
}
return result;
}

original_code:
void ui_switch_left(void)
{
    if (0 != ui_page_current) {
        ui_line_current = 0;
        --ui_page_current;
        ui_display(ui_page_current, ui_line_current);
    }
}

canonical_original_code:
void ui_switch_left()
{
    if (0 != ui_page_current) {
        ui_line_current = 0;
        --ui_page_current;
        ui_display(ui_page_current, ui_line_current);
    }
}


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

result = ui_page_current;
if ( (unsigned __int64)ui_page_current <= 3 )
{
ui_line_current = 0LL;
result = ui_display(++ui_page_current, 0LL);
}
return result;
}

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

result = ui_page_current;
if ( (unsigned __int64)ui_page_current <= 3 )
{
ui_line_current = 0LL;
result = func7(++ui_page_current, 0LL);
}
return result;
}

original_code:
void ui_switch_right(void)
{
    if (ui_page_current < 5 - 1) {
        ui_line_current = 0;
        ++ui_page_current;
        ui_display(ui_page_current, ui_line_current);
    }
}

canonical_original_code:
void ui_switch_right()
{
    if (ui_page_current < 5 - 1) {
        ui_line_current = 0;
        ++ui_page_current;
        ui_display(ui_page_current, ui_line_current);
    }
}


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

result = ui_line_current;
if ( ui_line_current )
result = ui_display(ui_page_current, --ui_line_current);
return result;
}

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

result = ui_line_current;
if ( ui_line_current )
result = func7(ui_page_current, --ui_line_current);
return result;
}

original_code:
void ui_scroll_up(void)
{
    if (0 != ui_line_current) {
        ui_display(ui_page_current, --ui_line_current);
    }
}

canonical_original_code:
void ui_scroll_up()
{
    if (0 != ui_line_current) {
        ui_display(ui_page_current, --ui_line_current);
    }
}


decompiled_code:
__int64 ui_scroll_down()
{
return ui_display(ui_page_current, ++ui_line_current);
}

canonical_decompiled_code:
__int64 func11()
{
return func7(ui_page_current, ++ui_line_current);
}

original_code:
void ui_scroll_down(void)
{
    ui_display(ui_page_current, ++ui_line_current);
}

canonical_original_code:
void ui_scroll_down()
{
    ui_display(ui_page_current, ++ui_line_current);
}


decompiled_code:
__int64 info_init()
{
char *v0; // rdx
__int64 result; // rax

*(_DWORD *)info_root = 1380211016;
v0 = (char *)info_root;
*((_WORD *)info_root + 4) = 20480;
*(_QWORD *)(v0 + 26) = &idt_data;
*(_QWORD *)(v0 + 34) = &gdt_data;
*((_WORD *)v0 + 71) = (_WORD)info_mmap - (_WORD)v0;
*((_WORD *)v0 + 72) = (_WORD)info_module - (_WORD)v0;
*((_WORD *)v0 + 73) = (_WORD)info_strings - (_WORD)v0;
*((_WORD *)v0 + 70) = (_WORD)info_ioapic - (_WORD)v0;
for ( result = 0LL; result != 16; ++result )
*(_DWORD *)&v0[4 * result + 58] = result;
return result;
}

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

*(_DWORD *)info_root = 1380211016;
v0 = (char *)info_root;
*((_WORD *)info_root + 4) = 20480;
*(_QWORD *)(v0 + 26) = &idt_data;
*(_QWORD *)(v0 + 34) = &gdt_data;
*((_WORD *)v0 + 71) = (_WORD)info_mmap - (_WORD)v0;
*((_WORD *)v0 + 72) = (_WORD)info_module - (_WORD)v0;
*((_WORD *)v0 + 73) = (_WORD)info_strings - (_WORD)v0;
*((_WORD *)v0 + 70) = (_WORD)info_ioapic - (_WORD)v0;
for ( result = 0LL; result != 16; ++result )
*(_DWORD *)&v0[4 * result + 58] = result;
return result;
}

original_code:
void info_init(void)
{
    info_root->magic = 0x52445948;
    info_root->length = 0x5000;
    info_root->idt_paddr = (uintptr_t) &idt_data;
    info_root->gdt_paddr = (uintptr_t) &gdt_data;
    info_root->mmap_offset = ((uintptr_t) info_mmap - (uintptr_t) info_root);
    info_root->module_offset = ((uintptr_t) info_module - (uintptr_t) info_root);
    info_root->string_offset = ((uintptr_t) info_strings - (uintptr_t) info_root);
    info_root->ioapic_offset = ((uintptr_t) info_ioapic - (uintptr_t) info_root);
    size_t i;
    for (i = 0; i < 16; ++i) {
        info_root->irq_gsi[i] = i;
    }
}

canonical_original_code:
void info_init()
{
    info_root->magic = 0x52445948;
    info_root->length = 0x5000;
    info_root->idt_paddr = (unsigned long long) &idt_data;
    info_root->gdt_paddr = (unsigned long long) &gdt_data;
    info_root->mmap_offset = ((unsigned long long) info_mmap - (unsigned long long) info_root);
    info_root->module_offset = ((unsigned long long) info_module - (unsigned long long) info_root);
    info_root->string_offset = ((unsigned long long) info_strings - (unsigned long long) info_root);
    info_root->ioapic_offset = ((unsigned long long) info_ioapic - (unsigned long long) info_root);
    unsigned long long i;
    for (i = 0; i < 16; ++i) {
        info_root->irq_gsi[i] = i;
    }
}


decompiled_code:
void *__fastcall info_string_alloc(__int64 a1)
{
__int64 v1; // rdi
void *result; // rax

v1 = a1 + 1;
result = info_strings_next;
info_strings_next = (char *)info_strings_next + v1;
info_string_space -= v1;
return result;
}

canonical_decompiled_code:
void *__fastcall func1(__int64 a1)
{
__int64 v1; // rdi
void *result; // rax

v1 = a1 + 1;
result = info_strings_next;
info_strings_next = (char *)info_strings_next + v1;
info_string_space -= v1;
return result;
}

original_code:
char *info_string_alloc(size_t length)
{
    ++length;
    if (length > info_string_space) {
    }
    char *allocated = info_strings_next;
    info_strings_next = &info_strings_next[length];
    info_string_space -= length;
    return allocated;
}

canonical_original_code:
char *info_string_alloc(unsigned long long length)
{
    ++length;
    if (length > info_string_space) {
    }
    char *allocated = info_strings_next;
    info_strings_next = &info_strings_next[length];
    info_string_space -= length;
    return allocated;
}

variable_types:
length
unsigned long long

variable_types:
allocated
char *


decompiled_code:
__int64 __fastcall pit_set_redir(__int64 a1)
{
unsigned int v1; // ebx
__int64 v2; // rax

v1 = *(_DWORD *)(info_root + 8LL * ((*(_BYTE *)(info_root + 4LL) & 1) != 0) + 58);
v2 = ioapic_get_by_gsi(v1);
return ioapic_redir_set((unsigned __int16)(v1 - *(_WORD *)(v2 + 2)), a1, v2);
}

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

v1 = *(_DWORD *)(info_root + 8LL * ((*(_BYTE *)(info_root + 4LL) & 1) != 0) + 58);
v2 = ioapic_get_by_gsi(v1);
return ioapic_redir_set((unsigned __int16)(v1 - *(_WORD *)(v2 + 2)), a1, v2);
}

original_code:
static void pit_set_redir(uint64_t redir)
{
    uint8_t irq = (0 == (info_root->flags & (1 << 0))) ? 0x0 : 0x2;
    uint32_t gsi = info_root->irq_gsi[irq];
    hy_info_ioapic_t *ioapic = ioapic_get_by_gsi(gsi);
    uint32_t index = gsi - ioapic->gsi_base;
    ioapic_redir_set(index, redir, ioapic);
}

canonical_original_code:
static void pit_set_redir(unsigned long long redir)
{
    unsigned char irq = (0 == (info_root->flags & (1 << 0))) ? 0x0 : 0x2;
    unsigned int gsi = info_root->irq_gsi[irq];
    struct hy_info_ioapic *ioapic = ioapic_get_by_gsi(gsi);
    unsigned int index = gsi - ioapic->gsi_base;
    ioapic_redir_set(index, redir, ioapic);
}

variable_types:
redir
unsigned long long

variable_types:
irq
unsigned char

variable_types:
gsi
unsigned int

variable_types: struct hy_info_ioapic { unsigned char apic_id; unsigned char version; unsigned int gsi_base; unsigned short gsi_count; unsigned long long mmio_paddr; } *

variable_types:
index
unsigned int


decompiled_code:
void __fastcall pit_freq_set(unsigned __int16 a1)
{
outb(0x40u, (unsigned __int8)(1193180 / a1));
outb(0x40u, (unsigned __int16)(1193180 / a1) >> 8);
}

canonical_decompiled_code:
void __fastcall func1(unsigned __int16 a1)
{
outb(0x40u, (unsigned __int8)(1193180 / a1));
outb(0x40u, (unsigned __int16)(1193180 / a1) >> 8);
}

original_code:
void pit_freq_set(uint16_t freq)
{
    uint16_t divisor = 1193180 / freq;
    outb(0x40, divisor);
    outb(0x40, divisor >> 8);
}

canonical_original_code:
void pit_freq_set(unsigned short freq)
{
    unsigned short divisor = 1193180 / freq;
    outb(0x40, divisor);
    outb(0x40, divisor >> 8);
}

variable_types:
freq
unsigned short

variable_types:
divisor
unsigned short


decompiled_code:
__int64 pit_route()
{
__int64 v0; // rax

v0 = lapic_id();
return pit_set_redir((v0 << 56) | 0x30);
}

original_code:
void pit_route(void)
{
    uint64_t redir = 0x30 | ((uint64_t) lapic_id() << 56);
    pit_set_redir(redir);
}

canonical_original_code:
void pit_route()
{
    unsigned long long redir = 0x30 | ((uint64_t) lapic_id() << 56);
    pit_set_redir(redir);
}


decompiled_code:
__int64 pit_mask()
{
return pit_set_redir(0x10000LL);
}

canonical_decompiled_code:
__int64 func3()
{
return func0(0x10000LL);
}

original_code:
void pit_mask(void)
{
    uint64_t redir = (1 << 16);
    pit_set_redir(redir);
}

canonical_original_code:
void pit_mask()
{
    unsigned long long redir = (1 << 16);
    pit_set_redir(redir);
}


decompiled_code:
__int64 __fastcall screen_write(unsigned __int8 *a1, unsigned __int16 a2, unsigned __int16 a3)
{
__int64 v3; // rsi
__int64 result; // rax
_BYTE *v5; // rdx
unsigned __int8 *v6; // rdi

v3 = a2 + 80 * a3;
result = *a1;
if ( (_BYTE)result )
{
v5 = (_BYTE *)(2 * v3 + 753664);
v6 = a1 + 1;
do
{
*v5 = result;
result = *v6;
v5 += 2;
++v6;
}
while ( (_BYTE)result );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 *a1, unsigned __int16 a2, unsigned __int16 a3)
{
__int64 v3; // rsi
__int64 result; // rax
_BYTE *v5; // rdx
unsigned __int8 *v6; // rdi

v3 = a2 + 80 * a3;
result = *a1;
if ( (_BYTE)result )
{
v5 = (_BYTE *)(2 * v3 + 753664);
v6 = a1 + 1;
do
{
*v5 = result;
result = *v6;
v5 += 2;
++v6;
}
while ( (_BYTE)result );
}
return result;
}

original_code:
void screen_write(const char *string, uint16_t x, uint16_t y)
{
    char *video = (char *) 0xB8000;
    size_t screen_max = 80 * 25;
    size_t pos_screen = x + y * 80;
    size_t pos_str;
    for (pos_str = 0; '\0' != string[pos_str] && pos_screen < screen_max; ++pos_str) {
        if ('\n' == string[pos_str]) {
            pos_screen = ((pos_screen / 80) + 1) * 80;
        } else {
            video[pos_screen * 2 ] = string[pos_str];
            video[pos_screen * 2 + 1] = 0x0F;
            ++pos_screen;
        }
    }
}

canonical_original_code:
void screen_write(char *string, unsigned short x, unsigned short y)
{
    char *video = (char *) 0xB8000;
    unsigned long long screen_max = 80 * 25;
    unsigned long long pos_screen = x + y * 80;
    unsigned long long pos_str;
    for (pos_str = 0; '\0' != string[pos_str] && pos_screen < screen_max; ++pos_str) {
        if ('\n' == string[pos_str]) {
            pos_screen = ((pos_screen / 80) + 1) * 80;
        } else {
            video[pos_screen * 2 ] = string[pos_str];
            video[pos_screen * 2 + 1] = 0x0F;
            ++pos_screen;
        }
    }
}

variable_types:
x
unsigned short

variable_types:
y
unsigned short

variable_types:
video
char *


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

for ( result = 753664LL; result != 757664; result += 2LL )
{
*(_BYTE *)(result + 1) = 15;
*(_BYTE *)result = 32;
}
return result;
}

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

for ( result = 753664LL; result != 757664; result += 2LL )
{
*(_BYTE *)(result + 1) = 15;
*(_BYTE *)result = 32;
}
return result;
}

original_code:
void screen_clear(void)
{
    char *video = (char *) 0xB8000;
    size_t screen_max = 80 * 25;
    size_t i;
    for (i = 0; i < screen_max; ++i) {
        video[i * 2 ] = ' ';
        video[i * 2 + 1] = 0x0F;
    }
}

canonical_original_code:
void screen_clear()
{
    char *video = (char *) 0xB8000;
    unsigned long long screen_max = 80 * 25;
    unsigned long long i;
    for (i = 0; i < screen_max; ++i) {
        video[i * 2 ] = ' ';
        video[i * 2 + 1] = 0x0F;
    }
}


maf

original_code:
void maf(char* data){
    if (data != ((void *)0) && strlen(data) > 4) {
        char* value = data+4;
        char A[2];
        A[0] = value[0];
        A[1] = value[1];
        char B[2];
        B[0] = value[2];
        B[1] = value[3];
        int iA = _hex_to_int(A);
        int iB = _hex_to_int(B);
        int result = ((iA*256)+iB)/100;
        printf("%d\n", result);
    }
}

canonical_original_code:
void maf(char *data){
    if (data != ((void *)0) && strlen(data) > 4) {
        char *value = data+4;
        char A[2];
        A[0] = value[0];
        A[1] = value[1];
        char B[2];
        B[0] = value[2];
        B[1] = value[3];
        int iA = _hex_to_int(A);
        int iB = _hex_to_int(B);
        int result = ((iA*256)+iB)/100;
        printf("%d\n", result);
    }
}

variable_types:
data
char *

variable_types:
value
char *

variable_types:
A
char [2]

variable_types:
B
char [2]

variable_types: iA int

variable_types: iB int

variable_types: result int


decompiled_code:
int __fastcall throttle_position(char *s)
{
size_t v1; // rax
int v2; // eax
char v4[10]; // [rsp+Eh] [rbp-Ah] BYREF

LODWORD(v1) = printf("-%s#\n", s);
if ( s )
{
v1 = strlen(s);
if ( v1 > 2 )
{
v4[0] = s[2];
v4[1] = s[3];
v2 = 100 * hex_to_int(v4);
LODWORD(v1) = printf("%d\n", (unsigned int)(v2 / 255));
}
}
return v1;
}

canonical_decompiled_code:
int __fastcall func2(char *s)
{
size_t v1; // rax
int v2; // eax
char v4[10]; // [rsp+Eh] [rbp-Ah] BYREF

LODWORD(v1) = printf("-%s#\n", s);
if ( s )
{
v1 = strlen(s);
if ( v1 > 2 )
{
v4[0] = s[2];
v4[1] = s[3];
v2 = 100 * func0(v4);
LODWORD(v1) = printf("%d\n", (unsigned int)(v2 / 255));
}
}
return v1;
}

original_code:
void throttle_position(char* data){
    printf("-%s#\n",data);
    if(data != ((void *)0) && strlen(data) > 2){
        char* value = data+2;
        char A[2];
        A[0] = value[0];
        A[1] = value[1];
        int iA = _hex_to_int(A);
        int result = (iA*100)/255;
        printf("%d\n", result);
    }
}

canonical_original_code:
void throttle_position(char *data){
    printf("-%s#\n",data);
    if(data != ((void *)0) && strlen(data) > 2){
        char *value = data+2;
        char A[2];
        A[0] = value[0];
        A[1] = value[1];
        int iA = _hex_to_int(A);
        int result = (iA*100)/255;
        printf("%d\n", result);
    }
}


decompiled_code:
double __fastcall intake_pressure(const char *a1)
{
return (double)(int)hex_to_int(a1) / 0.14504;
}

canonical_decompiled_code:
double __fastcall func3(const char *a1)
{
return (double)(int)func0(a1) / 0.14504;
}

original_code:
double intake_pressure(char* data){
  return _hex_to_int(data)/ 0.14504;
}

canonical_original_code:
double intake_pressure(char *data){
  return _hex_to_int(data)/ 0.14504;
}

return_type: double


rpm

original_code:
void rpm(char* data){
    if (data != ((void *)0) && strlen(data) > 4) {
        char* value = data+4;
        char A[2];
        A[0] = value[0];
        A[1] = value[1];
        char B[2];
        B[0] = value[2];
        B[1] = value[3];
        int iA = _hex_to_int(A);
        int iB = _hex_to_int(B);
        int result = ((iA*256)+iB)/4;
        printf("%d\n", result);
    }
}

canonical_original_code:
void rpm(char *data){
    if (data != ((void *)0) && strlen(data) > 4) {
        char *value = data+4;
        char A[2];
        A[0] = value[0];
        A[1] = value[1];
        char B[2];
        B[0] = value[2];
        B[1] = value[3];
        int iA = _hex_to_int(A);
        int iB = _hex_to_int(B);
        int result = ((iA*256)+iB)/4;
        printf("%d\n", result);
    }
}


decompiled_code:
void __fastcall engine_oil_temp(__int64 a1)
{
int v1; // eax
char v2[10]; // [rsp+0h] [rbp-Ah] BYREF

if ( a1 )
{
if ( strlen((const char *)a1) > 4 )
{
v2[0] = *(_BYTE *)(a1 + 4);
v2[1] = *(_BYTE *)(a1 + 5);
v1 = hex_to_int(v2);
printf("%d\n", (unsigned int)(v1 - 40));
}
}
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1)
{
int v1; // eax
char v2[10]; // [rsp+0h] [rbp-Ah] BYREF

if ( a1 )
{
if ( strlen((const char *)a1) > 4 )
{
v2[0] = *(_BYTE *)(a1 + 4);
v2[1] = *(_BYTE *)(a1 + 5);
v1 = func0(v2);
printf("%d\n", (unsigned int)(v1 - 40));
}
}
}

original_code:
void engine_oil_temp(char* data){
    if (data != ((void *)0) && strlen(data) > 4) {
        char* value = data+4;
        char A[2];
        A[0] = value[0];
        A[1] = value[1];
        int iA = _hex_to_int(A);
        int result = iA-40;
        printf("%d\n", result);
    }
}

canonical_original_code:
void engine_oil_temp(char *data){
    if (data != ((void *)0) && strlen(data) > 4) {
        char *value = data+4;
        char A[2];
        A[0] = value[0];
        A[1] = value[1];
        int iA = _hex_to_int(A);
        int result = iA-40;
        printf("%d\n", result);
    }
}


decompiled_code:
void __fastcall engine_coolant_temp(__int64 a1)
{
int v1; // eax
char v2[10]; // [rsp+0h] [rbp-Ah] BYREF

if ( a1 )
{
if ( strlen((const char *)a1) > 4 )
{
v2[0] = *(_BYTE *)(a1 + 4);
v2[1] = *(_BYTE *)(a1 + 5);
v1 = hex_to_int(v2);
printf("%d\n", (unsigned int)(v1 - 40));
}
}
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1)
{
int v1; // eax
char v2[10]; // [rsp+0h] [rbp-Ah] BYREF

if ( a1 )
{
if ( strlen((const char *)a1) > 4 )
{
v2[0] = *(_BYTE *)(a1 + 4);
v2[1] = *(_BYTE *)(a1 + 5);
v1 = func0(v2);
printf("%d\n", (unsigned int)(v1 - 40));
}
}
}

original_code:
void engine_coolant_temp(char* data){
    if (data != ((void *)0) && strlen(data) > 4) {
        char* value = data+4;
        char A[2];
        A[0] = value[0];
        A[1] = value[1];
        int iA = _hex_to_int(A);
        int result = iA-40;
        printf("%d\n", result);
    }
}

canonical_original_code:
void engine_coolant_temp(char *data){
    if (data != ((void *)0) && strlen(data) > 4) {
        char *value = data+4;
        char A[2];
        A[0] = value[0];
        A[1] = value[1];
        int iA = _hex_to_int(A);
        int result = iA-40;
        printf("%d\n", result);
    }
}


decompiled_code:
void __fastcall engine_intake_temp(__int64 a1)
{
int v1; // eax
char v2[10]; // [rsp+0h] [rbp-Ah] BYREF

if ( a1 )
{
if ( strlen((const char *)a1) > 4 )
{
v2[0] = *(_BYTE *)(a1 + 4);
v2[1] = *(_BYTE *)(a1 + 5);
v1 = hex_to_int(v2);
printf("%d\n", (unsigned int)(v1 - 40));
}
}
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1)
{
int v1; // eax
char v2[10]; // [rsp+0h] [rbp-Ah] BYREF

if ( a1 )
{
if ( strlen((const char *)a1) > 4 )
{
v2[0] = *(_BYTE *)(a1 + 4);
v2[1] = *(_BYTE *)(a1 + 5);
v1 = func0(v2);
printf("%d\n", (unsigned int)(v1 - 40));
}
}
}

original_code:
void engine_intake_temp(char* data){
    if (data != ((void *)0) && strlen(data) > 4) {
        char* value = data+4;
        char A[2];
        A[0] = value[0];
        A[1] = value[1];
        int iA = _hex_to_int(A);
        int result = iA-40;
        printf("%d\n", result);
    }
}

canonical_original_code:
void engine_intake_temp(char *data){
    if (data != ((void *)0) && strlen(data) > 4) {
        char *value = data+4;
        char A[2];
        A[0] = value[0];
        A[1] = value[1];
        int iA = _hex_to_int(A);
        int result = iA-40;
        printf("%d\n", result);
    }
}


load

decompiled_code:
void __fastcall load(__int64 a1)
{
int v1; // eax
char v2[10]; // [rsp+0h] [rbp-Ah] BYREF

if ( a1 )
{
if ( strlen((const char *)a1) > 4 )
{
v2[0] = *(_BYTE *)(a1 + 4);
v2[1] = *(_BYTE *)(a1 + 5);
v1 = 100 * hex_to_int(v2);
printf("%d\n", (unsigned int)(v1 / 255));
}
}
}

original_code:
void load(char* data){
    if (data != ((void *)0) && strlen(data) > 4) {
        char* value = data+4;
        char A[2];
        A[0] = value[0];
        A[1] = value[1];
        int iA = _hex_to_int(A);
        int result = (iA*100)/255;
        printf("%d\n", result);
    }
}

canonical_original_code:
void load(char *data){
    if (data != ((void *)0) && strlen(data) > 4) {
        char *value = data+4;
        char A[2];
        A[0] = value[0];
        A[1] = value[1];
        int iA = _hex_to_int(A);
        int result = (iA*100)/255;
        printf("%d\n", result);
    }
}


speed

decompiled_code:
void __fastcall speed(__int64 a1)
{
unsigned int v1; // eax
char v2[10]; // [rsp+0h] [rbp-Ah] BYREF

if ( a1 )
{
if ( strlen((const char *)a1) > 4 )
{
v2[0] = *(_BYTE *)(a1 + 4);
v2[1] = *(_BYTE *)(a1 + 5);
v1 = hex_to_int(v2);
printf("%d\n", v1);
}
}
}

canonical_decompiled_code:
void __fastcall func9(__int64 a1)
{
unsigned int v1; // eax
char v2[10]; // [rsp+0h] [rbp-Ah] BYREF

if ( a1 )
{
if ( strlen((const char *)a1) > 4 )
{
v2[0] = *(_BYTE *)(a1 + 4);
v2[1] = *(_BYTE *)(a1 + 5);
v1 = func0(v2);
printf("%d\n", v1);
}
}
}

original_code:
void speed(char* data){
    if (data != ((void *)0) && strlen(data) > 4) {
        char* value = data+4;
        char A[2];
        A[0] = value[0];
        A[1] = value[1];
        int result = _hex_to_int(A);
        printf("%d\n", result);
    }
}

canonical_original_code:
void speed(char *data){
    if (data != ((void *)0) && strlen(data) > 4) {
        char *value = data+4;
        char A[2];
        A[0] = value[0];
        A[1] = value[1];
        int result = _hex_to_int(A);
        printf("%d\n", result);
    }
}


decompiled_code:
double __fastcall percent_scale(const char *a1)
{
return (double)(int)hex_to_int(a1) * 100.0 / 255.0;
}

canonical_decompiled_code:
double __fastcall func10(const char *a1)
{
return (double)(int)func0(a1) * 100.0 / 255.0;
}

original_code:
double percent_scale(char* data){
  return _hex_to_int(data)*100./255.;
}

canonical_original_code:
double percent_scale(char *data){
  return _hex_to_int(data)*100./255.;
}


decompiled_code:
double __fastcall ignition_timming_advance(const char *a1)
{
return ((double)(int)hex_to_int(a1) - 128.0) * 0.5;
}

canonical_decompiled_code:
double __fastcall func11(const char *a1)
{
return ((double)(int)func0(a1) - 128.0) * 0.5;
}

original_code:
double ignition_timming_advance(char* data){
  return (_hex_to_int(data)-128.)/2.;
}

canonical_original_code:
double ignition_timming_advance(char *data){
  return (_hex_to_int(data)-128.)/2.;
}


decompiled_code:
double __fastcall fuel_trim_percent(const char *a1)
{
return ((double)(int)hex_to_int(a1) - 128.0) * 100.0 * 0.0078125;
}

canonical_decompiled_code:
double __fastcall func12(const char *a1)
{
return ((double)(int)func0(a1) - 128.0) * 100.0 * 0.0078125;
}

original_code:
double fuel_trim_percent(char* data){
  return (_hex_to_int(data)-128.)*100./128.;
}

canonical_original_code:
double fuel_trim_percent(char *data){
  return (_hex_to_int(data)-128.)*100./128.;
}


decompiled_code:
__int64 __fastcall convert_to_min(const char *a1)
{
return (unsigned int)((int)hex_to_int(a1) / 60);
}

canonical_decompiled_code:
__int64 __fastcall func13(const char *a1)
{
return (unsigned int)((int)func0(a1) / 60);
}

original_code:
int convert_to_min(char* data){
  return _hex_to_int(data)/60;
}

canonical_original_code:
int convert_to_min(char *data){
  return _hex_to_int(data)/60;
}

return_type: int


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

result = a1;
if ( a2 != 1 )
{
v3 = msb_to_lsb(a1 >> (a2 >> 1), a2 >> 1);
result = v3 | ((unsigned int)msb_to_lsb(a1 & ((1 << (a2 >> 1)) - 1), a2 >> 1) << (a2 >> 1));
}
return result;
}

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

result = a1;
if ( a2 != 1 )
{
v3 = func1(a1 >> (a2 >> 1), a2 >> 1);
result = v3 | ((unsigned int)func1(a1 & ((1 << (a2 >> 1)) - 1), a2 >> 1) << (a2 >> 1));
}
return result;
}

original_code:
int msb_to_lsb(unsigned int num, unsigned int numBits)
{
    unsigned int reversedNum;
    unsigned int mask = 0;
    mask = (0x1 << (numBits/2)) - 1;
    if (numBits == 1){
        return num;
    }
    reversedNum = msb_to_lsb(num >> numBits/2, numBits/2) | msb_to_lsb((num & mask), numBits/2) << numBits/2;
    return reversedNum;
}

variable_types:
num
unsigned int

variable_types:
numBits
unsigned int

variable_types:
reversedNum
unsigned int

variable_types:
mask
unsigned int


repo: zvxryb/Binary-Static-Mesh

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

result = a3 & 3;
if ( (a3 & 3) != 0 )
_assert_fail("bytes % 4 == 0", "bsm.c", 0x23u, "reordercpy32");
if ( a3 > 3 )
{
do
{
*(_DWORD *)(a1 + 4 * result) = *(_DWORD *)(a2 + 4 * result);
++result;
}
while ( result < a3 >> 2 );
}
return result;
}

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

result = a3 & 3;
if ( (a3 & 3) != 0 )
_assert_fail("bytes % 4 == 0", "bsm.c", 0x23u, "reordercpy32");
if ( a3 > 3 )
{
do
{
*(_DWORD *)(a1 + 4 * result) = *(_DWORD *)(a2 + 4 * result);
++result;
}
while ( result < a3 >> 2 );
}
return result;
}

original_code:
static void reordercpy32(void *dst, const void *src, size_t bytes) {
  ((bytes % 4 == 0) ? (void) (0) : __assert_fail ("bytes % 4 == 0", "bsm.c", 35, __extension__ __PRETTY_FUNCTION__));
  uint32_t *src32 = src;
  uint32_t *dst32 = dst;
  uint32_t x;
  for (int i = 0; i < bytes / 4; i++) {
    memcpy(&x, &src32[i], 4);
    x = (x);
    dst32[i] = x;
  }
}

canonical_original_code:
static void reordercpy32(void *dst, void *src, unsigned long bytes) {
  ((bytes % 4 == 0) ? (void) (0) : __assert_fail ("bytes % 4 == 0", "bsm.c", 35, __extension__ __PRETTY_FUNCTION__));
  unsigned int *src32 = src;
  unsigned int *dst32 = dst;
  unsigned int x;
  for (int i = 0; i < bytes / 4; i++) {
    memcpy(&x, &src32[i], 4);
    x = (x);
    dst32[i] = x;
  }
}

variable_types:
dst
void *

variable_types:
src
void *

variable_types:
bytes
unsigned long

variable_types:
x
unsigned int

variable_types: i int


original_code:
static void normalize_normal(bsm_normal_t *normal) {
  float m = sqrtf(normal->x * normal->x + normal->y * normal->y + normal->z * normal->z);
  normal->x /= m;
  normal->y /= m;
  normal->z /= m;
}

canonical_original_code:
static void normalize_normal(struct bsm_normal *normal) {
  float m = sqrtf(normal->x * normal->x + normal->y * normal->y + normal->z * normal->z);
  normal->x /= m;
  normal->y /= m;
  normal->z /= m;
}

variable_types:
normal
struct bsm_normal { float x; } *

variable_types: m float


original_code:
static void normalize_tangent(bsm_tangent_t *tangent) {
  float m = sqrtf(tangent->x * tangent->x + tangent->y * tangent->y + tangent->z * tangent->z);
  tangent->x /= m;
  tangent->y /= m;
  tangent->z /= m;
  tangent->handedness = tangent->handedness >= 0.0f ? 1.0f : -1.0f;
}

canonical_original_code:
static void normalize_tangent(struct bsm_tangent *tangent) {
  float m = sqrtf(tangent->x * tangent->x + tangent->y * tangent->y + tangent->z * tangent->z);
  tangent->x /= m;
  tangent->y /= m;
  tangent->z /= m;
  tangent->handedness = tangent->handedness >= 0.0f ? 1.0f : -1.0f;
}

variable_types:
tangent
struct bsm_tangent { float x; } *


original_code:
_Bool bsm_read_header_v1(uint8_t *data, size_t n, bsm_header_v1_t *header) {
  ((sizeof(bsm_header_v1_t) == bsm_header_v1_size) ? (void) (0) : __assert_fail ("sizeof(bsm_header_v1_t) == bsm_header_v1_size", "bsm.c", 63, __extension__ __PRETTY_FUNCTION__));;
  if (n < sizeof(bsm_header_v1_t)) return 0;
  reordercpy32(header, data, sizeof(bsm_header_v1_t));
  if (header->magic[0] != bsm_magic[0]) return 0;
  if (header->magic[1] != bsm_magic[1]) return 0;
  if (header->magic[2] != bsm_magic[2]) return 0;
  if (header->magic[3] != bsm_magic[3]) return 0;
  if (header->num_verts < 0) return 0;
  if (header->offs_positions < 0) return 0;
  if (header->offs_texcoords < 0) return 0;
  if (header->offs_normals < 0) return 0;
  if (header->offs_tangents < 0) return 0;
  if (header->num_tris < 0) return 0;
  if (header->offs_tris < 0) return 0;
  if (header->num_meshes < 0) return 0;
  if (header->offs_meshes < 0) return 0;
  if (header->num_hullverts < 0) return 0;
  if (header->offs_hullverts < 0) return 0;
  if (header->num_hulls < 0) return 0;
  if (header->offs_hulls < 0) return 0;
  if (header->num_visverts < 0) return 0;
  if (header->offs_visverts < 0) return 0;
  if (header->num_vistris < 0) return 0;
  if (header->offs_vistris < 0) return 0;
  if (header->num_verts * sizeof(bsm_position_t) + header->offs_positions > n) return 0;
  if (header->num_verts * sizeof(bsm_texcoord_t) + header->offs_texcoords > n) return 0;
  if (header->num_verts * sizeof(bsm_normal_t) + header->offs_normals > n) return 0;
  if (header->num_verts * sizeof(bsm_tangent_t) + header->offs_tangents > n) return 0;
  if (header->num_tris * sizeof(bsm_triangle_t) + header->offs_tris > n) return 0;
  if (header->num_meshes * sizeof(bsm_mesh_t) + header->offs_meshes > n) return 0;
  if (header->num_hullverts * sizeof(bsm_hullvert_t) + header->offs_hullverts > n) return 0;
  if (header->num_hulls * sizeof(bsm_hull_t) + header->offs_hulls > n) return 0;
  if (header->num_visverts * sizeof(bsm_visvert_t) + header->offs_visverts > n) return 0;
  if (header->num_vistris * sizeof(bsm_vistri_t) + header->offs_vistris > n) return 0;
  return 1;
}

canonical_original_code:
_Bool bsm_read_header_v1(unsigned char *data, unsigned long n, struct bsm_header_v1 *header) {
  ((sizeof(bsm_header_v1_t) == bsm_header_v1_size) ? (void) (0) : __assert_fail ("sizeof(bsm_header_v1_t) == bsm_header_v1_size", "bsm.c", 63, __extension__ __PRETTY_FUNCTION__));;
  if (n < sizeof(bsm_header_v1_t)) return 0;
  reordercpy32(header, data, sizeof(bsm_header_v1_t));
  if (header->magic[0] != bsm_magic[0]) return 0;
  if (header->magic[1] != bsm_magic[1]) return 0;
  if (header->magic[2] != bsm_magic[2]) return 0;
  if (header->magic[3] != bsm_magic[3]) return 0;
  if (header->num_verts < 0) return 0;
  if (header->offs_positions < 0) return 0;
  if (header->offs_texcoords < 0) return 0;
  if (header->offs_normals < 0) return 0;
  if (header->offs_tangents < 0) return 0;
  if (header->num_tris < 0) return 0;
  if (header->offs_tris < 0) return 0;
  if (header->num_meshes < 0) return 0;
  if (header->offs_meshes < 0) return 0;
  if (header->num_hullverts < 0) return 0;
  if (header->offs_hullverts < 0) return 0;
  if (header->num_hulls < 0) return 0;
  if (header->offs_hulls < 0) return 0;
  if (header->num_visverts < 0) return 0;
  if (header->offs_visverts < 0) return 0;
  if (header->num_vistris < 0) return 0;
  if (header->offs_vistris < 0) return 0;
  if (header->num_verts * sizeof(bsm_position_t) + header->offs_positions > n) return 0;
  if (header->num_verts * sizeof(bsm_texcoord_t) + header->offs_texcoords > n) return 0;
  if (header->num_verts * sizeof(bsm_normal_t) + header->offs_normals > n) return 0;
  if (header->num_verts * sizeof(bsm_tangent_t) + header->offs_tangents > n) return 0;
  if (header->num_tris * sizeof(bsm_triangle_t) + header->offs_tris > n) return 0;
  if (header->num_meshes * sizeof(bsm_mesh_t) + header->offs_meshes > n) return 0;
  if (header->num_hullverts * sizeof(bsm_hullvert_t) + header->offs_hullverts > n) return 0;
  if (header->num_hulls * sizeof(bsm_hull_t) + header->offs_hulls > n) return 0;
  if (header->num_visverts * sizeof(bsm_visvert_t) + header->offs_visverts > n) return 0;
  if (header->num_vistris * sizeof(bsm_vistri_t) + header->offs_vistris > n) return 0;
  return 1;
}

variable_types:
data
unsigned char *

variable_types:
n
unsigned long

variable_types: struct bsm_header_v1 { int magic[4]; int version; int extension; struct bsm_bsphere { float x; } bsphere; struct bsm_bbox { float x0; float x1; } bbox; int num_verts; int offs_positions; int offs_texcoords; int offs_normals; int offs_tangents; int num_tris; int offs_tris; int num_meshes; int offs_meshes; int num_hullverts; int offs_hullverts; int num_hulls; int offs_hulls; int num_visverts; int offs_visverts; int num_vistris; int offs_vistris; } *

user_defined_types: struct bsm_header_v1 { int magic[4]; int version; int extension; struct bsm_bsphere bsphere; struct bsm_bbox bbox; int num_verts; int offs_positions; int offs_texcoords; int offs_normals; int offs_tangents; int num_tris; int offs_tris; int num_meshes; int offs_meshes; int num_hullverts; int offs_hullverts; int num_hulls; int offs_hulls; int num_visverts; int offs_visverts; int num_vistris; int offs_vistris; }


decompiled_code:
__int64 __fastcall bsm_texcoords_bytes(__int64 a1)
{
return 8LL * *(int *)(a1 + 64);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return 8LL * *(int *)(a1 + 64);
}

original_code:
size_t bsm_texcoords_bytes(bsm_header_v1_t *header) {
  ((sizeof(bsm_texcoord_t) == bsm_texcoord_size) ? (void) (0) : __assert_fail ("sizeof(bsm_texcoord_t) == bsm_texcoord_size", "bsm.c", 110, __extension__ __PRETTY_FUNCTION__));;
  return header->num_verts * sizeof(bsm_texcoord_t);
}

canonical_original_code:
unsigned long bsm_texcoords_bytes(struct bsm_header_v1 *header) {
  ((sizeof(bsm_texcoord_t) == bsm_texcoord_size) ? (void) (0) : __assert_fail ("sizeof(bsm_texcoord_t) == bsm_texcoord_size", "bsm.c", 110, __extension__ __PRETTY_FUNCTION__));;
  return header->num_verts * sizeof(bsm_texcoord_t);
}

return_type: unsigned long


decompiled_code:
__int64 __fastcall bsm_normals_bytes(__int64 a1)
{
return 12LL * *(int *)(a1 + 64);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return 12LL * *(int *)(a1 + 64);
}

original_code:
size_t bsm_normals_bytes(bsm_header_v1_t *header) {
  ((sizeof(bsm_normal_t) == bsm_normal_size) ? (void) (0) : __assert_fail ("sizeof(bsm_normal_t) == bsm_normal_size", "bsm.c", 115, __extension__ __PRETTY_FUNCTION__));;
  return header->num_verts * sizeof(bsm_normal_t);
}

canonical_original_code:
unsigned long bsm_normals_bytes(struct bsm_header_v1 *header) {
  ((sizeof(bsm_normal_t) == bsm_normal_size) ? (void) (0) : __assert_fail ("sizeof(bsm_normal_t) == bsm_normal_size", "bsm.c", 115, __extension__ __PRETTY_FUNCTION__));;
  return header->num_verts * sizeof(bsm_normal_t);
}


decompiled_code:
__int64 __fastcall bsm_tangents_bytes(__int64 a1)
{
return 16LL * *(int *)(a1 + 64);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return 16LL * *(int *)(a1 + 64);
}

original_code:
size_t bsm_tangents_bytes(bsm_header_v1_t *header) {
  ((sizeof(bsm_tangent_t) == bsm_tangent_size) ? (void) (0) : __assert_fail ("sizeof(bsm_tangent_t) == bsm_tangent_size", "bsm.c", 120, __extension__ __PRETTY_FUNCTION__));;
  return header->num_verts * sizeof(bsm_tangent_t);
}

canonical_original_code:
unsigned long bsm_tangents_bytes(struct bsm_header_v1 *header) {
  ((sizeof(bsm_tangent_t) == bsm_tangent_size) ? (void) (0) : __assert_fail ("sizeof(bsm_tangent_t) == bsm_tangent_size", "bsm.c", 120, __extension__ __PRETTY_FUNCTION__));;
  return header->num_verts * sizeof(bsm_tangent_t);
}


decompiled_code:
__int64 __fastcall bsm_read_positions(__int64 a1, unsigned __int64 a2, __int64 a3, __int64 a4)
{
__int64 v7; // rdx
__int64 v8; // rsi
__int64 result; // rax

v7 = bsm_positions_bytes(a3);
v8 = *(int *)(a3 + 68);
result = 0LL;
if ( v7 + v8 <= a2 )
{
reordercpy32(a4, a1 + v8, v7);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, unsigned __int64 a2, __int64 a3, __int64 a4)
{
__int64 v7; // rdx
__int64 v8; // rsi
__int64 result; // rax

v7 = func4(a3);
v8 = *(int *)(a3 + 68);
result = 0LL;
if ( v7 + v8 <= a2 )
{
func0(a4, a1 + v8, v7);
result = 1LL;
}
return result;
}

original_code:
_Bool bsm_read_positions(uint8_t *data, size_t n, bsm_header_v1_t *header, bsm_position_t *positions) {
  size_t bytes = bsm_positions_bytes(header);
  size_t offs = header->offs_positions;
  if (offs + bytes > n) return 0;
  reordercpy32(positions, data + offs, bytes);
  return 1;
}

canonical_original_code:
_Bool bsm_read_positions(unsigned char *data, unsigned long n, struct bsm_header_v1 *header, struct bsm_position *positions) {
  unsigned long bytes = bsm_positions_bytes(header);
  unsigned long offs = header->offs_positions;
  if (offs + bytes > n) return 0;
  reordercpy32(positions, data + offs, bytes);
  return 1;
}

variable_types:
positions
struct bsm_position { float x; } *

variable_types:
offs
unsigned long


decompiled_code:
__int64 __fastcall bsm_read_texcoords(__int64 a1, unsigned __int64 a2, __int64 a3, __int64 a4)
{
__int64 v7; // rdx
__int64 v8; // rsi
__int64 result; // rax

v7 = bsm_texcoords_bytes(a3);
v8 = *(int *)(a3 + 72);
result = 0LL;
if ( v7 + v8 <= a2 )
{
reordercpy32(a4, a1 + v8, v7);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, unsigned __int64 a2, __int64 a3, __int64 a4)
{
__int64 v7; // rdx
__int64 v8; // rsi
__int64 result; // rax

v7 = func5(a3);
v8 = *(int *)(a3 + 72);
result = 0LL;
if ( v7 + v8 <= a2 )
{
func0(a4, a1 + v8, v7);
result = 1LL;
}
return result;
}

original_code:
_Bool bsm_read_texcoords(uint8_t *data, size_t n, bsm_header_v1_t *header, bsm_texcoord_t *texcoords) {
  size_t bytes = bsm_texcoords_bytes(header);
  size_t offs = header->offs_texcoords;
  if (offs + bytes > n) return 0;
  reordercpy32(texcoords, data + offs, bytes);
  return 1;
}

canonical_original_code:
_Bool bsm_read_texcoords(unsigned char *data, unsigned long n, struct bsm_header_v1 *header, struct bsm_texcoord *texcoords) {
  unsigned long bytes = bsm_texcoords_bytes(header);
  unsigned long offs = header->offs_texcoords;
  if (offs + bytes > n) return 0;
  reordercpy32(texcoords, data + offs, bytes);
  return 1;
}

variable_types:
texcoords
struct bsm_texcoord { float u; } *


decompiled_code:
__int64 __fastcall bsm_read_normals(__int64 a1, unsigned __int64 a2, __int64 a3, float *a4)
{
__int64 v7; // rdx
__int64 v8; // rsi
__int64 result; // rax
int v10; // ebp

v7 = bsm_normals_bytes(a3);
v8 = *(int *)(a3 + 76);
result = 0LL;
if ( v7 + v8 <= a2 )
{
reordercpy32((__int64)a4, a1 + v8, v7);
if ( *(int *)(a3 + 64) <= 0 )
{
result = 1LL;
}
else
{
v10 = 0;
do
{
normalize_normal(a4);
++v10;
a4 += 3;
}
while ( *(_DWORD *)(a3 + 64) > v10 );
result = 1LL;
}
}
return result;
}

original_code:
_Bool bsm_read_normals(uint8_t *data, size_t n, bsm_header_v1_t *header, bsm_normal_t *normals) {
  size_t bytes = bsm_normals_bytes(header);
  size_t offs = header->offs_normals;
  if (offs + bytes > n) return 0;
  reordercpy32(normals, data + offs, bytes);
  for (int i = 0; i < header->num_verts; i++) {
    normalize_normal(&normals[i]);
  }
  return 1;
}

canonical_original_code:
_Bool bsm_read_normals(unsigned char *data, unsigned long n, struct bsm_header_v1 *header, struct bsm_normal *normals) {
  unsigned long bytes = bsm_normals_bytes(header);
  unsigned long offs = header->offs_normals;
  if (offs + bytes > n) return 0;
  reordercpy32(normals, data + offs, bytes);
  for (int i = 0; i < header->num_verts; i++) {
    normalize_normal(&normals[i]);
  }
  return 1;
}

variable_types:
normals
struct bsm_normal { float x; } *


decompiled_code:
__int64 __fastcall bsm_read_tangents(__int64 a1, unsigned __int64 a2, __int64 a3, float *a4)
{
__int64 v7; // rdx
__int64 v8; // rsi
__int64 result; // rax
int v10; // ebp

v7 = bsm_tangents_bytes(a3);
v8 = *(int *)(a3 + 80);
result = 0LL;
if ( v7 + v8 <= a2 )
{
reordercpy32((__int64)a4, a1 + v8, v7);
if ( *(int *)(a3 + 64) <= 0 )
{
result = 1LL;
}
else
{
v10 = 0;
do
{
normalize_tangent(a4);
++v10;
a4 += 4;
}
while ( *(_DWORD *)(a3 + 64) > v10 );
result = 1LL;
}
}
return result;
}

original_code:
_Bool bsm_read_tangents(uint8_t *data, size_t n, bsm_header_v1_t *header, bsm_tangent_t *tangents) {
  size_t bytes = bsm_tangents_bytes(header);
  size_t offs = header->offs_tangents;
  if (offs + bytes > n) return 0;
  reordercpy32(tangents, data + offs, bytes);
  for (int i = 0; i < header->num_verts; i++) {
    normalize_tangent(&tangents[i]);
  }
  return 1;
}

canonical_original_code:
_Bool bsm_read_tangents(unsigned char *data, unsigned long n, struct bsm_header_v1 *header, struct bsm_tangent *tangents) {
  unsigned long bytes = bsm_tangents_bytes(header);
  unsigned long offs = header->offs_tangents;
  if (offs + bytes > n) return 0;
  reordercpy32(tangents, data + offs, bytes);
  for (int i = 0; i < header->num_verts; i++) {
    normalize_tangent(&tangents[i]);
  }
  return 1;
}

variable_types:
tangents
struct bsm_tangent { float x; } *


decompiled_code:
__int64 __fastcall bsm_tris_bytes(__int64 a1)
{
return 12LL * *(int *)(a1 + 84);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
return 12LL * *(int *)(a1 + 84);
}

original_code:
size_t bsm_tris_bytes(bsm_header_v1_t *header) {
  ((sizeof(bsm_triangle_t) == bsm_triangle_size) ? (void) (0) : __assert_fail ("sizeof(bsm_triangle_t) == bsm_triangle_size", "bsm.c", 167, __extension__ __PRETTY_FUNCTION__));;
  return header->num_tris * sizeof(bsm_triangle_t);
}

canonical_original_code:
unsigned long bsm_tris_bytes(struct bsm_header_v1 *header) {
  ((sizeof(bsm_triangle_t) == bsm_triangle_size) ? (void) (0) : __assert_fail ("sizeof(bsm_triangle_t) == bsm_triangle_size", "bsm.c", 167, __extension__ __PRETTY_FUNCTION__));;
  return header->num_tris * sizeof(bsm_triangle_t);
}


decompiled_code:
__int64 __fastcall bsm_read_tris(__int64 a1, unsigned __int64 a2, __int64 a3, __int64 a4)
{
__int64 v7; // rdx
__int64 v8; // rsi
__int64 result; // rax

v7 = bsm_tris_bytes(a3);
v8 = *(int *)(a3 + 88);
result = 0LL;
if ( v7 + v8 <= a2 )
{
reordercpy32(a4, a1 + v8, v7);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, unsigned __int64 a2, __int64 a3, __int64 a4)
{
__int64 v7; // rdx
__int64 v8; // rsi
__int64 result; // rax

v7 = func12(a3);
v8 = *(int *)(a3 + 88);
result = 0LL;
if ( v7 + v8 <= a2 )
{
func0(a4, a1 + v8, v7);
result = 1LL;
}
return result;
}

original_code:
_Bool bsm_read_tris(uint8_t *data, size_t n, bsm_header_v1_t *header, bsm_triangle_t *tris) {
  size_t bytes = bsm_tris_bytes(header);
  size_t offs = header->offs_tris;
  if (offs + bytes > n) return 0;
  reordercpy32(tris, data + offs, bytes);
  return 1;
}

canonical_original_code:
_Bool bsm_read_tris(unsigned char *data, unsigned long n, struct bsm_header_v1 *header, struct bsm_triangle *tris) {
  unsigned long bytes = bsm_tris_bytes(header);
  unsigned long offs = header->offs_tris;
  if (offs + bytes > n) return 0;
  reordercpy32(tris, data + offs, bytes);
  return 1;
}

variable_types:
tris
struct bsm_triangle { int index[3]; } *


decompiled_code:
__int64 __fastcall bsm_meshes_bytes(__int64 a1)
{
return 264LL * *(int *)(a1 + 92);
}

original_code:
size_t bsm_meshes_bytes(bsm_header_v1_t *header) {
  ((sizeof(bsm_mesh_t) == bsm_mesh_size) ? (void) (0) : __assert_fail ("sizeof(bsm_mesh_t) == bsm_mesh_size", "bsm.c", 181, __extension__ __PRETTY_FUNCTION__));;
  return header->num_meshes * sizeof(bsm_mesh_t);
}

canonical_original_code:
unsigned long bsm_meshes_bytes(struct bsm_header_v1 *header) {
  ((sizeof(bsm_mesh_t) == bsm_mesh_size) ? (void) (0) : __assert_fail ("sizeof(bsm_mesh_t) == bsm_mesh_size", "bsm.c", 181, __extension__ __PRETTY_FUNCTION__));;
  return header->num_meshes * sizeof(bsm_mesh_t);
}


decompiled_code:
__int64 __fastcall bsm_read_meshes(__int64 a1, unsigned __int64 a2, __int64 a3, __int64 a4)
{
__int64 v7; // rdx
__int64 v8; // rsi
__int64 result; // rax

v7 = bsm_meshes_bytes(a3);
v8 = *(int *)(a3 + 96);
result = 0LL;
if ( v7 + v8 <= a2 )
{
reordercpy32(a4, a1 + v8, v7);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1, unsigned __int64 a2, __int64 a3, __int64 a4)
{
__int64 v7; // rdx
__int64 v8; // rsi
__int64 result; // rax

v7 = func14(a3);
v8 = *(int *)(a3 + 96);
result = 0LL;
if ( v7 + v8 <= a2 )
{
func0(a4, a1 + v8, v7);
result = 1LL;
}
return result;
}

original_code:
_Bool bsm_read_meshes(uint8_t *data, size_t n, bsm_header_v1_t *header, bsm_mesh_t *meshes) {
  size_t bytes = bsm_meshes_bytes(header);
  size_t offs = header->offs_meshes;
  if (offs + bytes > n) return 0;
  reordercpy32(meshes, data + offs, bytes);
  return 1;
}

canonical_original_code:
_Bool bsm_read_meshes(unsigned char *data, unsigned long n, struct bsm_header_v1 *header, struct bsm_mesh *meshes) {
  unsigned long bytes = bsm_meshes_bytes(header);
  unsigned long offs = header->offs_meshes;
  if (offs + bytes > n) return 0;
  reordercpy32(meshes, data + offs, bytes);
  return 1;
}

variable_types: struct bsm_mesh { int idx_tris; int num_tris; unsigned char material[256]; } *


decompiled_code:
__int64 __fastcall bsm_hullverts_bytes(__int64 a1)
{
return 12LL * *(int *)(a1 + 100);
}

original_code:
size_t bsm_hullverts_bytes(bsm_header_v1_t *header) {
  ((sizeof(bsm_hullvert_t) == bsm_hullvert_size) ? (void) (0) : __assert_fail ("sizeof(bsm_hullvert_t) == bsm_hullvert_size", "bsm.c", 195, __extension__ __PRETTY_FUNCTION__));;
  return header->num_hullverts * sizeof(bsm_hullvert_t);
}

canonical_original_code:
unsigned long bsm_hullverts_bytes(struct bsm_header_v1 *header) {
  ((sizeof(bsm_hullvert_t) == bsm_hullvert_size) ? (void) (0) : __assert_fail ("sizeof(bsm_hullvert_t) == bsm_hullvert_size", "bsm.c", 195, __extension__ __PRETTY_FUNCTION__));;
  return header->num_hullverts * sizeof(bsm_hullvert_t);
}


decompiled_code:
__int64 __fastcall bsm_hulls_bytes(__int64 a1)
{
return 8LL * *(int *)(a1 + 108);
}

canonical_decompiled_code:
__int64 __fastcall func17(__int64 a1)
{
return 8LL * *(int *)(a1 + 108);
}

original_code:
size_t bsm_hulls_bytes(bsm_header_v1_t *header) {
  ((sizeof(bsm_hull_t) == bsm_hull_size) ? (void) (0) : __assert_fail ("sizeof(bsm_hull_t) == bsm_hull_size", "bsm.c", 200, __extension__ __PRETTY_FUNCTION__));;
  return header->num_hulls * sizeof(bsm_hull_t);
}

canonical_original_code:
unsigned long bsm_hulls_bytes(struct bsm_header_v1 *header) {
  ((sizeof(bsm_hull_t) == bsm_hull_size) ? (void) (0) : __assert_fail ("sizeof(bsm_hull_t) == bsm_hull_size", "bsm.c", 200, __extension__ __PRETTY_FUNCTION__));;
  return header->num_hulls * sizeof(bsm_hull_t);
}


decompiled_code:
__int64 __fastcall bsm_visverts_bytes(__int64 a1)
{
return 12LL * *(int *)(a1 + 116);
}

original_code:
size_t bsm_visverts_bytes(bsm_header_v1_t *header) {
  ((sizeof(bsm_visvert_t) == bsm_visvert_size) ? (void) (0) : __assert_fail ("sizeof(bsm_visvert_t) == bsm_visvert_size", "bsm.c", 205, __extension__ __PRETTY_FUNCTION__));;
  return header->num_visverts * sizeof(bsm_visvert_t);
}

canonical_original_code:
unsigned long bsm_visverts_bytes(struct bsm_header_v1 *header) {
  ((sizeof(bsm_visvert_t) == bsm_visvert_size) ? (void) (0) : __assert_fail ("sizeof(bsm_visvert_t) == bsm_visvert_size", "bsm.c", 205, __extension__ __PRETTY_FUNCTION__));;
  return header->num_visverts * sizeof(bsm_visvert_t);
}


decompiled_code:
__int64 __fastcall bsm_vistris_bytes(__int64 a1)
{
return 12LL * *(int *)(a1 + 124);
}

original_code:
size_t bsm_vistris_bytes(bsm_header_v1_t *header) {
  ((sizeof(bsm_vistri_t) == bsm_vistri_size) ? (void) (0) : __assert_fail ("sizeof(bsm_vistri_t) == bsm_vistri_size", "bsm.c", 210, __extension__ __PRETTY_FUNCTION__));;
  return header->num_vistris * sizeof(bsm_vistri_t);
}

canonical_original_code:
unsigned long bsm_vistris_bytes(struct bsm_header_v1 *header) {
  ((sizeof(bsm_vistri_t) == bsm_vistri_size) ? (void) (0) : __assert_fail ("sizeof(bsm_vistri_t) == bsm_vistri_size", "bsm.c", 210, __extension__ __PRETTY_FUNCTION__));;
  return header->num_vistris * sizeof(bsm_vistri_t);
}


decompiled_code:
__int64 __fastcall bsm_read_hullverts(__int64 a1, unsigned __int64 a2, __int64 a3, __int64 a4)
{
__int64 v7; // rdx
__int64 v8; // rsi
__int64 result; // rax

v7 = bsm_hullverts_bytes(a3);
v8 = *(int *)(a3 + 104);
result = 0LL;
if ( v7 + v8 <= a2 )
{
reordercpy32(a4, a1 + v8, v7);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func20(__int64 a1, unsigned __int64 a2, __int64 a3, __int64 a4)
{
__int64 v7; // rdx
__int64 v8; // rsi
__int64 result; // rax

v7 = func16(a3);
v8 = *(int *)(a3 + 104);
result = 0LL;
if ( v7 + v8 <= a2 )
{
func0(a4, a1 + v8, v7);
result = 1LL;
}
return result;
}

original_code:
_Bool bsm_read_hullverts(uint8_t *data, size_t n, bsm_header_v1_t *header, bsm_hullvert_t *hullverts) {
  size_t bytes = bsm_hullverts_bytes(header);
  size_t offs = header->offs_hullverts;
  if (offs + bytes > n) return 0;
  reordercpy32(hullverts, data + offs, bytes);
  return 1;
}

canonical_original_code:
_Bool bsm_read_hullverts(unsigned char *data, unsigned long n, struct bsm_header_v1 *header, struct bsm_hullvert *hullverts) {
  unsigned long bytes = bsm_hullverts_bytes(header);
  unsigned long offs = header->offs_hullverts;
  if (offs + bytes > n) return 0;
  reordercpy32(hullverts, data + offs, bytes);
  return 1;
}

variable_types:
hullverts
struct bsm_hullvert { float x; } *


decompiled_code:
__int64 __fastcall bsm_read_hulls(__int64 a1, unsigned __int64 a2, __int64 a3, __int64 a4)
{
__int64 v7; // rdx
__int64 v8; // rsi
__int64 result; // rax

v7 = bsm_hulls_bytes(a3);
v8 = *(int *)(a3 + 112);
result = 0LL;
if ( v7 + v8 <= a2 )
{
reordercpy32(a4, a1 + v8, v7);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func21(__int64 a1, unsigned __int64 a2, __int64 a3, __int64 a4)
{
__int64 v7; // rdx
__int64 v8; // rsi
__int64 result; // rax

v7 = func17(a3);
v8 = *(int *)(a3 + 112);
result = 0LL;
if ( v7 + v8 <= a2 )
{
func0(a4, a1 + v8, v7);
result = 1LL;
}
return result;
}

original_code:
_Bool bsm_read_hulls(uint8_t *data, size_t n, bsm_header_v1_t *header, bsm_hull_t *hulls) {
  size_t bytes = bsm_hulls_bytes(header);
  size_t offs = header->offs_hulls;
  if (offs + bytes > n) return 0;
  reordercpy32(hulls, data + offs, bytes);
  return 1;
}

canonical_original_code:
_Bool bsm_read_hulls(unsigned char *data, unsigned long n, struct bsm_header_v1 *header, struct bsm_hull *hulls) {
  unsigned long bytes = bsm_hulls_bytes(header);
  unsigned long offs = header->offs_hulls;
  if (offs + bytes > n) return 0;
  reordercpy32(hulls, data + offs, bytes);
  return 1;
}

variable_types: struct bsm_hull { int idx_vert; int num_vert; } *

variable_types:
bytes
unsigned long


decompiled_code:
__int64 __fastcall bsm_read_visverts(__int64 a1, unsigned __int64 a2, __int64 a3, __int64 a4)
{
__int64 v7; // rdx
__int64 v8; // rsi
__int64 result; // rax

v7 = bsm_visverts_bytes(a3);
v8 = *(int *)(a3 + 120);
result = 0LL;
if ( v7 + v8 <= a2 )
{
reordercpy32(a4, a1 + v8, v7);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func22(__int64 a1, unsigned __int64 a2, __int64 a3, __int64 a4)
{
__int64 v7; // rdx
__int64 v8; // rsi
__int64 result; // rax

v7 = func18(a3);
v8 = *(int *)(a3 + 120);
result = 0LL;
if ( v7 + v8 <= a2 )
{
func0(a4, a1 + v8, v7);
result = 1LL;
}
return result;
}

original_code:
_Bool bsm_read_visverts(uint8_t *data, size_t n, bsm_header_v1_t *header, bsm_visvert_t *visverts) {
  size_t bytes = bsm_visverts_bytes(header);
  size_t offs = header->offs_visverts;
  if (offs + bytes > n) return 0;
  reordercpy32(visverts, data + offs, bytes);
  return 1;
}

canonical_original_code:
_Bool bsm_read_visverts(unsigned char *data, unsigned long n, struct bsm_header_v1 *header, struct bsm_visvert *visverts) {
  unsigned long bytes = bsm_visverts_bytes(header);
  unsigned long offs = header->offs_visverts;
  if (offs + bytes > n) return 0;
  reordercpy32(visverts, data + offs, bytes);
  return 1;
}

variable_types:
visverts
struct bsm_visvert { float x; } *


decompiled_code:
__int64 __fastcall bsm_read_vistris(__int64 a1, unsigned __int64 a2, __int64 a3, __int64 a4)
{
__int64 v7; // rdx
__int64 v8; // rsi
__int64 result; // rax

v7 = bsm_vistris_bytes(a3);
v8 = *(int *)(a3 + 128);
result = 0LL;
if ( v7 + v8 <= a2 )
{
reordercpy32(a4, a1 + v8, v7);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func23(__int64 a1, unsigned __int64 a2, __int64 a3, __int64 a4)
{
__int64 v7; // rdx
__int64 v8; // rsi
__int64 result; // rax

v7 = func19(a3);
v8 = *(int *)(a3 + 128);
result = 0LL;
if ( v7 + v8 <= a2 )
{
func0(a4, a1 + v8, v7);
result = 1LL;
}
return result;
}

original_code:
_Bool bsm_read_vistris(uint8_t *data, size_t n, bsm_header_v1_t *header, bsm_vistri_t *vistris) {
  size_t bytes = bsm_vistris_bytes(header);
  size_t offs = header->offs_vistris;
  if (offs + bytes > n) return 0;
  reordercpy32(vistris, data + offs, bytes);
  return 1;
}

canonical_original_code:
_Bool bsm_read_vistris(unsigned char *data, unsigned long n, struct bsm_header_v1 *header, struct bsm_vistri *vistris) {
  unsigned long bytes = bsm_vistris_bytes(header);
  unsigned long offs = header->offs_vistris;
  if (offs + bytes > n) return 0;
  reordercpy32(vistris, data + offs, bytes);
  return 1;
}

variable_types:
vistris
struct bsm_vistri { int index[3]; } *


repo: zvxryb/kludge-engine

original_code:
static kl_bvh_branch_t* branch_new(kl_bvh_node_t *left, kl_bvh_node_t *right) {
  kl_bvh_branch_t *branch = malloc(sizeof(kl_bvh_branch_t));
  kl_sphere_t bounds;
  kl_sphere_merge(&bounds, &left->header.bounds, &right->header.bounds);
  *branch = (kl_bvh_branch_t){
    .header = {
      .type = 0x02,
      .bounds = bounds
    },
    .children = { left, right }
  };
  return branch;
}

canonical_original_code:
static struct kl_bvh_branch *branch_new(union kl_bvh_node *left, union kl_bvh_node *right) {
  struct kl_bvh_branch *branch = malloc(sizeof(kl_bvh_branch_t));
  struct kl_sphere bounds;
  kl_sphere_merge(&bounds, &left->header.bounds, &right->header.bounds);
  *branch = (struct kl_bvh_branch){
    .header = {
      .type = 0x02,
      .bounds = bounds
    },
    .children = { left, right }
  };
  return branch;
}

variable_types:
left
union kl_bvh_node *

variable_types:
right
union kl_bvh_node *

variable_types: struct kl_bvh_branch { struct kl_bvh_header { int type; struct kl_sphere { struct kl_vec3f { float x; } center; float radius; } bounds; } header; union kl_bvh_node { struct kl_bvh_header header; struct kl_bvh_branch branch; struct kl_bvh_leaf { struct kl_bvh_header header; void *item; } leaf; } *children[2]; } *

user_defined_types: struct kl_sphere { struct kl_vec3f center; float radius; }

user_defined_types: struct kl_bvh_branch { struct kl_bvh_header header; union kl_bvh_node *children[2]; }

user_defined_types: struct kl_bvh_header { int type; struct kl_sphere bounds; }

user_defined_types: union kl_bvh_node { struct kl_bvh_header header; struct kl_bvh_branch branch; struct kl_bvh_leaf leaf; }


decompiled_code:
float __fastcall node_dist(__int64 a1, __int64 a2)
{
return kl_vec3f_dist((float *)(a1 + 4), (float *)(a2 + 4));
}

canonical_decompiled_code:
float __fastcall func4(__int64 a1, __int64 a2)
{
return func3((float *)(a1 + 4), (float *)(a2 + 4));
}

original_code:
static float node_dist(kl_bvh_node_t *s1, kl_bvh_node_t *s2) {
  return kl_vec3f_dist(&s1->header.bounds.center, &s2->header.bounds.center);
}

canonical_original_code:
static float node_dist(union kl_bvh_node *s1, union kl_bvh_node *s2) {
  return kl_vec3f_dist(&s1->header.bounds.center, &s2->header.bounds.center);
}

variable_types: union kl_bvh_node { struct kl_bvh_header { int type; struct kl_sphere { struct kl_vec3f { float x; } center; float radius; } bounds; } header; struct kl_bvh_branch { struct kl_bvh_header header; union kl_bvh_node *children[2]; } branch; struct kl_bvh_leaf { struct kl_bvh_header header; void *item; } leaf; } *

return_type: float


decompiled_code:
char *__fastcall leaf_insert(__int64 a1, __int64 a2)
{
__int64 v3; // r14
__int64 v4; // r13
float v5; // [rsp+Ch] [rbp-24h]

if ( !a1 )
return (char *)a2;
if ( *(_DWORD *)a1 == 1 )
return branch_new(a1, a2);
if ( *(_DWORD *)a1 != 2 )
return 0LL;
v3 = *(_QWORD *)(a1 + 24);
v4 = *(_QWORD *)(a1 + 32);
v5 = node_dist(a2, v3);
if ( node_dist(a2, v4) <= v5 )
v4 = leaf_insert(v4, a2);
else
v3 = leaf_insert(v3, a2);
*(_QWORD *)(a1 + 24) = v3;
*(_QWORD *)(a1 + 32) = v4;
kl_sphere_merge(a1 + 4, v3 + 4, v4 + 4);
return (char *)a1;
}

original_code:
static kl_bvh_node_t* leaf_insert(kl_bvh_node_t *curr, kl_bvh_leaf_t *leaf) {
  if (curr == ((void *)0)) return (kl_bvh_node_t*)leaf;
  kl_bvh_node_t *l;
  kl_bvh_node_t *r;
  switch (curr->header.type) {
    case 0x01:
      return (kl_bvh_node_t*)branch_new(curr, (kl_bvh_node_t*)leaf);
    case 0x02:
      l = curr->branch.children[0];
      r = curr->branch.children[1];
      float dl = node_dist((kl_bvh_node_t*)leaf, l);
      float dr = node_dist((kl_bvh_node_t*)leaf, r);
      if (dl < dr) {
        l = leaf_insert(l, leaf);
      } else {
        r = leaf_insert(r, leaf);
      }
      curr->branch.children[0] = l;
      curr->branch.children[1] = r;
      kl_sphere_merge(&curr->header.bounds, &l->header.bounds, &r->header.bounds);
      return curr;
  }
  return ((void *)0);
}

canonical_original_code:
static union kl_bvh_node *leaf_insert(union kl_bvh_node *curr, struct kl_bvh_leaf *leaf) {
  if (curr == ((void *)0)) return (union kl_bvh_node *)leaf;
  union kl_bvh_node *l;
  union kl_bvh_node *r;
  switch (curr->header.type) {
    case 0x01:
      return (union kl_bvh_node *)branch_new(curr, (union kl_bvh_node *)leaf);
    case 0x02:
      l = curr->branch.children[0];
      r = curr->branch.children[1];
      float dl = node_dist((kl_bvh_node_t*)leaf, l);
      float dr = node_dist((kl_bvh_node_t*)leaf, r);
      if (dl < dr) {
        l = leaf_insert(l, leaf);
      } else {
        r = leaf_insert(r, leaf);
      }
      curr->branch.children[0] = l;
      curr->branch.children[1] = r;
      kl_sphere_merge(&curr->header.bounds, &l->header.bounds, &r->header.bounds);
      return curr;
  }
  return ((void *)0);
}

variable_types:
curr
union kl_bvh_node *

variable_types:
leaf
struct kl_bvh_leaf *

variable_types: dl float

variable_types: dr float

return_type: union kl_bvh_node { struct kl_bvh_header { int type; struct kl_sphere { struct kl_vec3f { float x; } center; float radius; } bounds; } header; struct kl_bvh_branch { struct kl_bvh_header header; union kl_bvh_node *children[2]; } branch; struct kl_bvh_leaf { struct kl_bvh_header header; void *item; } leaf; } *


decompiled_code:
char *__fastcall kl_bvh_insert(__int64 *a1, int *a2, __int64 a3)
{
_DWORD *v3; // rax
char *result; // rax

v3 = leaf_new(a2, a3);
result = leaf_insert(*a1, (__int64)v3);
*a1 = (__int64)result;
return result;
}

canonical_decompiled_code:
char *__fastcall func6(__int64 *a1, int *a2, __int64 a3)
{
_DWORD *v3; // rax
char *result; // rax

v3 = leaf_new(a2, a3);
result = func5(*a1, (__int64)v3);
*a1 = (__int64)result;
return result;
}

original_code:
void kl_bvh_insert(kl_bvh_node_t **root, kl_sphere_t *bounds, void *item) {
  kl_bvh_leaf_t *leaf = leaf_new(bounds, item);
  *root = leaf_insert(*root, leaf);
}

canonical_original_code:
void kl_bvh_insert(union kl_bvh_node **root, struct kl_sphere *bounds, void *item) {
  struct kl_bvh_leaf *leaf = leaf_new(bounds, item);
  *root = leaf_insert(*root, leaf);
}

variable_types: union kl_bvh_node { struct kl_bvh_header { int type; struct kl_sphere { struct kl_vec3f { float x; } center; float radius; } bounds; } header; struct kl_bvh_branch { struct kl_bvh_header header; union kl_bvh_node *children[2]; } branch; struct kl_bvh_leaf { struct kl_bvh_header header; void *item; } leaf; } **

variable_types:
bounds
struct kl_sphere *

variable_types:
item
void *

variable_types: struct kl_bvh_leaf { struct kl_bvh_header { int type; struct kl_sphere { struct kl_vec3f { float x; } center; float radius; } bounds; } header; void *item; } *


decompiled_code:
void __fastcall kl_bvh_search(__int64 a1, unsigned int (__fastcall *a2)(__int64, __int64), __int64 a3, __int64 a4)
{
if ( a1 && a2(a1 + 4, a3) )
{
if ( *(_DWORD *)a1 == 1 )
{
kl_array_push(a4, a1 + 24);
}
else if ( *(_DWORD *)a1 == 2 )
{
kl_bvh_search(*(_QWORD *)(a1 + 24), a2, a3, a4);
kl_bvh_search(*(_QWORD *)(a1 + 32), a2, a3, a4);
}
}
}

original_code:
void kl_bvh_search(kl_bvh_node_t *root, kl_bvh_filter_cb filtercb, void *filter_data, kl_array_t *results) {
  if (root == ((void *)0)) return;
  if (!filtercb(&root->header.bounds, filter_data)) return;
  switch (root->header.type) {
    case 0x01:
      kl_array_push(results, &root->leaf.item);
      return;
    case 0x02:
      kl_bvh_search(root->branch.children[0], filtercb, filter_data, results);
      kl_bvh_search(root->branch.children[1], filtercb, filter_data, results);
      return;
  }
}

canonical_original_code:
void kl_bvh_search(union kl_bvh_node *root, int (*filtercb)(struct kl_sphere *, void *), void *filter_data, struct kl_array *results) {
  if (root == ((void *)0)) return;
  if (!filtercb(&root->header.bounds, filter_data)) return;
  switch (root->header.type) {
    case 0x01:
      kl_array_push(results, &root->leaf.item);
      return;
    case 0x02:
      kl_bvh_search(root->branch.children[0], filtercb, filter_data, results);
      kl_bvh_search(root->branch.children[1], filtercb, filter_data, results);
      return;
  }
}

variable_types:
filtercb
int (*)(struct kl_sphere *, void *)

variable_types: struct kl_array { unsigned char *data; int size; int item_size; int num_items; } *

user_defined_types: struct kl_bvh_leaf { struct kl_bvh_header header; void *item; }


decompiled_code:
__int64 __fastcall kl_bvh_debug(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // [rsp+8h] [rbp-18h] BYREF

v3 = a1;
if ( a1 )
{
kl_array_push(a2, &v3);
result = v3;
if ( *(_DWORD *)v3 == 2 )
{
kl_bvh_debug(*(_QWORD *)(v3 + 24), a2);
result = kl_bvh_debug(*(_QWORD *)(v3 + 32), a2);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // [rsp+8h] [rbp-18h] BYREF

v3 = a1;
if ( a1 )
{
kl_array_push(a2, &v3);
result = v3;
if ( *(_DWORD *)v3 == 2 )
{
func8(*(_QWORD *)(v3 + 24), a2);
result = func8(*(_QWORD *)(v3 + 32), a2);
}
}
return result;
}

original_code:
void kl_bvh_debug(kl_bvh_node_t* root, kl_array_t *results) {
  if (root == ((void *)0)) return;
  kl_array_push(results, &root);
  if (root->header.type == 0x02) {
      kl_bvh_debug(root->branch.children[0], results);
      kl_bvh_debug(root->branch.children[1], results);
  }
}

canonical_original_code:
void kl_bvh_debug(union kl_bvh_node *root, struct kl_array *results) {
  if (root == ((void *)0)) return;
  kl_array_push(results, &root);
  if (root->header.type == 0x02) {
      kl_bvh_debug(root->branch.children[0], results);
      kl_bvh_debug(root->branch.children[1], results);
  }
}

user_defined_types: struct kl_array { unsigned char *data; int size; int item_size; int num_items; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
__int64 v4; // rax
__int64 v5; // rax
char v6; // r15
char v7; // r14
int result; // eax
float v9; // xmm0_4
float v10; // xmm1_4
float v11; // xmm0_4
float v12; // xmm1_4
int v13; // ebx
int v14; // er12
char v15; // [rsp+Ah] [rbp-F6h]
char v16; // [rsp+Bh] [rbp-F5h]
float v17; // [rsp+Ch] [rbp-F4h]
int v18[3]; // [rsp+1Ch] [rbp-E4h] BYREF
float v19; // [rsp+28h] [rbp-D8h] BYREF
float v20; // [rsp+2Ch] [rbp-D4h]
int v21; // [rsp+30h] [rbp-D0h]
int v22[2]; // [rsp+34h] [rbp-CCh] BYREF
int v23[3]; // [rsp+3Ch] [rbp-C4h] BYREF
int v24[3]; // [rsp+48h] [rbp-B8h] BYREF
int v25[3]; // [rsp+54h] [rbp-ACh] BYREF
int v26[18]; // [rsp+60h] [rbp-A0h] BYREF
int v27; // [rsp+A8h] [rbp-58h] BYREF
int v28; // [rsp+ACh] [rbp-54h] BYREF
int v29[2]; // [rsp+B0h] [rbp-50h] BYREF
char v30; // [rsp+B8h] [rbp-48h]
int v31; // [rsp+BCh] [rbp-44h]
int v32; // [rsp+C0h] [rbp-40h]

kl_resource_add_dir("./test_assets/", &unk_505, envp);
if ( (int)kl_vid_init() < 0 )
return -1;
if ( (int)kl_input_init() < 0 )
return -1;
if ( (int)kl_render_init() < 0 )
return -1;
kl_vid_size(&v28, &v27);
v26[0] = 0;
v26[1] = 0;
v26[2] = 0;
v26[3] = LC0;
v26[4] = 0;
v26[5] = 0;
v26[6] = 0;
*(float *)&v26[7] = (float)v28 / (float)v27;
v26[8] = 1065747481;
v26[9] = 0;
v26[10] = 0;
*(float *)&v26[11] = (float)v28;
*(float *)&v26[12] = (float)v27;
v26[13] = LC0;
v26[14] = 1176256512;
kl_terrain_testsphere();
v3 = kl_model_load("test_assets/test.iqm");
kl_render_add_model(v3);
v4 = kl_model_load("test_assets/sponza.obj");
kl_render_add_model(v4);
v5 = kl_model_load("test_assets/colorcalibration.obj");
kl_render_add_model(v5);
v25[0] = 1140457472;
v25[1] = LC10;
v25[2] = 0;
kl_render_add_light(v25, *(double *)_mm_cvtsi32_si128(LC0).m128i_i64, 0.80000001, 0.60000002, 25000.0);
v24[0] = -1007026176;
v24[1] = LC10;
v24[2] = 0;
kl_render_add_light(v24, *(double *)_mm_cvtsi32_si128(LC0).m128i_i64, 0.80000001, 0.60000002, 25000.0);
v23[0] = LC15;
v23[1] = LC15;
v23[2] = LC0;
kl_render_set_envlight(
v23,
0.80000001,
0.80000001,
*(double *)_mm_cvtsi32_si128(LC0).m128i_i64,
0.0,
*(double *)_mm_cvtsi32_si128(LC0).m128i_i64,
0.89999998,
0.60000002);
v22[0] = -1082130432;
v22[1] = 0;
v19 = 0.0;
v20 = 0.0;
v21 = 0;
v6 = 0;
v7 = 0;
v15 = 0;
v16 = 0;
LABEL_30:
v17 = kl_timer_tick(v22);
kl_input_tick();
v13 = 0;
v14 = 0;
while ( 1 )
{
LABEL_31:
if ( !(unsigned __int8)kl_input_poll(v29) )
{
v20 = COERCE_FLOAT(COERCE_UNSIGNED_INT((float)v14) ^ LC18) / 200.0;
v19 = COERCE_FLOAT(COERCE_UNSIGNED_INT((float)v13) ^ LC18) / 200.0;
kl_camera_local_rotate(v26, &v19);
v9 = *(float *)&LC0;
if ( !v6 )
v9 = 0.0;
v10 = *(float *)&LC0;
if ( !v7 )
v10 = 0.0;
*(float *)v18 = v9 - v10;
v18[1] = 0;
v11 = *(float *)&LC0;
if ( !v15 )
v11 = 0.0;
v12 = *(float *)&LC0;
if ( !v16 )
v12 = 0.0;
*(float *)&v18[2] = v11 - v12;
kl_vec3f_scale((float *)v18, (float *)v18, v17 * 320.0);
kl_camera_local_move(v26, v18);
kl_render_draw(v26);
kl_vid_swap();
goto LABEL_30;
}
if ( v29[0] == 1 )
break;
if ( v29[0] == 2 )
{
v14 += v31;
v13 += v32;
}
}
switch ( v29[1] )
{
case 1:
if ( !v30 )
goto LABEL_31;
result = 0;
break;
case 0x21:
kl_render_set_debug(0LL);
goto LABEL_31;
case 0x22:
kl_render_set_debug(1LL);
goto LABEL_31;
case 0x23:
kl_render_set_debug(2LL);
goto LABEL_31;
case 0x24:
kl_render_set_debug(3LL);
goto LABEL_31;
case 0x25:
kl_render_set_debug(4LL);
goto LABEL_31;
case 0x26:
kl_render_set_debug(5LL);
goto LABEL_31;
case 0x27:
kl_render_set_debug(6LL);
goto LABEL_31;
case 0x28:
kl_render_set_debug(7LL);
goto LABEL_31;
case 0x29:
kl_render_set_debug(8LL);
goto LABEL_31;
case 0x30:
v7 = v30;
goto LABEL_31;
case 0x33:
v6 = v30;
goto LABEL_31;
case 0x42:
v15 = v30;
goto LABEL_31;
case 0x46:
v16 = v30;
goto LABEL_31;
default:
goto LABEL_31;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
__int64 v4; // rax
__int64 v5; // rax
char v6; // r15
char v7; // r14
int result; // eax
float v9; // xmm0_4
float v10; // xmm1_4
float v11; // xmm0_4
float v12; // xmm1_4
int v13; // ebx
int v14; // er12
char v15; // [rsp+Ah] [rbp-F6h]
char v16; // [rsp+Bh] [rbp-F5h]
float v17; // [rsp+Ch] [rbp-F4h]
int v18[3]; // [rsp+1Ch] [rbp-E4h] BYREF
float v19; // [rsp+28h] [rbp-D8h] BYREF
float v20; // [rsp+2Ch] [rbp-D4h]
int v21; // [rsp+30h] [rbp-D0h]
int v22[2]; // [rsp+34h] [rbp-CCh] BYREF
int v23[3]; // [rsp+3Ch] [rbp-C4h] BYREF
int v24[3]; // [rsp+48h] [rbp-B8h] BYREF
int v25[3]; // [rsp+54h] [rbp-ACh] BYREF
int v26[18]; // [rsp+60h] [rbp-A0h] BYREF
int v27; // [rsp+A8h] [rbp-58h] BYREF
int v28; // [rsp+ACh] [rbp-54h] BYREF
int v29[2]; // [rsp+B0h] [rbp-50h] BYREF
char v30; // [rsp+B8h] [rbp-48h]
int v31; // [rsp+BCh] [rbp-44h]
int v32; // [rsp+C0h] [rbp-40h]

kl_resource_add_dir("./test_assets/", &unk_505, envp);
if ( (int)kl_vid_init() < 0 )
return -1;
if ( (int)kl_input_init() < 0 )
return -1;
if ( (int)kl_render_init() < 0 )
return -1;
kl_vid_size(&v28, &v27);
v26[0] = 0;
v26[1] = 0;
v26[2] = 0;
v26[3] = LC0;
v26[4] = 0;
v26[5] = 0;
v26[6] = 0;
*(float *)&v26[7] = (float)v28 / (float)v27;
v26[8] = 1065747481;
v26[9] = 0;
v26[10] = 0;
*(float *)&v26[11] = (float)v28;
*(float *)&v26[12] = (float)v27;
v26[13] = LC0;
v26[14] = 1176256512;
kl_terrain_testsphere();
v3 = kl_model_load("test_assets/test.iqm");
kl_render_add_model(v3);
v4 = kl_model_load("test_assets/sponza.obj");
kl_render_add_model(v4);
v5 = kl_model_load("test_assets/colorcalibration.obj");
kl_render_add_model(v5);
v25[0] = 1140457472;
v25[1] = LC10;
v25[2] = 0;
kl_render_add_light(v25, *(double *)_mm_cvtsi32_si128(LC0).m128i_i64, 0.80000001, 0.60000002, 25000.0);
v24[0] = -1007026176;
v24[1] = LC10;
v24[2] = 0;
kl_render_add_light(v24, *(double *)_mm_cvtsi32_si128(LC0).m128i_i64, 0.80000001, 0.60000002, 25000.0);
v23[0] = LC15;
v23[1] = LC15;
v23[2] = LC0;
kl_render_set_envlight(
v23,
0.80000001,
0.80000001,
*(double *)_mm_cvtsi32_si128(LC0).m128i_i64,
0.0,
*(double *)_mm_cvtsi32_si128(LC0).m128i_i64,
0.89999998,
0.60000002);
v22[0] = -1082130432;
v22[1] = 0;
v19 = 0.0;
v20 = 0.0;
v21 = 0;
v6 = 0;
v7 = 0;
v15 = 0;
v16 = 0;
LABEL_30:
v17 = kl_timer_tick(v22);
kl_input_tick();
v13 = 0;
v14 = 0;
while ( 1 )
{
LABEL_31:
if ( !(unsigned __int8)kl_input_poll(v29) )
{
v20 = COERCE_FLOAT(COERCE_UNSIGNED_INT((float)v14) ^ LC18) / 200.0;
v19 = COERCE_FLOAT(COERCE_UNSIGNED_INT((float)v13) ^ LC18) / 200.0;
kl_camera_local_rotate(v26, &v19);
v9 = *(float *)&LC0;
if ( !v6 )
v9 = 0.0;
v10 = *(float *)&LC0;
if ( !v7 )
v10 = 0.0;
*(float *)v18 = v9 - v10;
v18[1] = 0;
v11 = *(float *)&LC0;
if ( !v15 )
v11 = 0.0;
v12 = *(float *)&LC0;
if ( !v16 )
v12 = 0.0;
*(float *)&v18[2] = v11 - v12;
func0((float *)v18, (float *)v18, v17 * 320.0);
kl_camera_local_move(v26, v18);
kl_render_draw(v26);
kl_vid_swap();
goto LABEL_30;
}
if ( v29[0] == 1 )
break;
if ( v29[0] == 2 )
{
v14 += v31;
v13 += v32;
}
}
switch ( v29[1] )
{
case 1:
if ( !v30 )
goto LABEL_31;
result = 0;
break;
case 0x21:
kl_render_set_debug(0LL);
goto LABEL_31;
case 0x22:
kl_render_set_debug(1LL);
goto LABEL_31;
case 0x23:
kl_render_set_debug(2LL);
goto LABEL_31;
case 0x24:
kl_render_set_debug(3LL);
goto LABEL_31;
case 0x25:
kl_render_set_debug(4LL);
goto LABEL_31;
case 0x26:
kl_render_set_debug(5LL);
goto LABEL_31;
case 0x27:
kl_render_set_debug(6LL);
goto LABEL_31;
case 0x28:
kl_render_set_debug(7LL);
goto LABEL_31;
case 0x29:
kl_render_set_debug(8LL);
goto LABEL_31;
case 0x30:
v7 = v30;
goto LABEL_31;
case 0x33:
v6 = v30;
goto LABEL_31;
case 0x42:
v15 = v30;
goto LABEL_31;
case 0x46:
v16 = v30;
goto LABEL_31;
default:
goto LABEL_31;
}
return result;
}

original_code:
int main(int argc, char **argv) {
  kl_evt_generic_t evt;
  kl_resource_add_dir("./test_assets/", "");
  if (kl_vid_init() < 0) return -1;
  if (kl_input_init() < 0) return -1;
  if (kl_render_init() < 0) return -1;
  int w, h;
  kl_vid_size(&w, &h);
  kl_camera_t cam = {
    .position = { .x = 0.0f, .y = 0.0f, .z = 0.0f },
    .orientation = { .r = 1.0f, .i = 0.0f, .j = 0.0f, .k = 0.0f },
    .aspect = (float)w/(float)h,
    .fov = 1.047f,
 .viewport = { .x = 0, .y = 0, .z = w, .w = h },
    .near = 1.0f,
    .far = 10000.0f
  };
  kl_terrain_testsphere();
  kl_model_t *model1 = kl_model_load("test_assets/test.iqm");
  kl_render_add_model(model1);
  kl_model_t *model2 = kl_model_load("test_assets/sponza.obj");
  kl_render_add_model(model2);
  kl_model_t *model3 = kl_model_load("test_assets/colorcalibration.obj");
  kl_render_add_model(model3);
  kl_vec3f_t light1_pos = { .x = 500.0f, .y = 400.0f, .z = 0.0f };
  kl_render_add_light(&light1_pos, 1.0f, 0.8f, 0.6f, 25000.0f);
  kl_vec3f_t light2_pos = { .x = -500.0f, .y = 400.0f, .z = 0.0f };
  kl_render_add_light(&light2_pos, 1.0f, 0.8f, 0.6f, 25000.0f);
  kl_vec3f_t light3_pos = { .x = 0.0f, .y = 300.0f, .z = 0.0f };
  kl_vec3f_t envlight_dir = { -2.0f, -2.0f, 1.0f };
  kl_render_set_envlight(&envlight_dir, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f, 0.9f, 0.6f, 0.0f);
  kl_timer_t timer = { .time = -1.0f, .dt = 0.0f};
  _Bool move_f = 0;
  _Bool move_b = 0;
  _Bool move_l = 0;
  _Bool move_r = 0;
  kl_vec3f_t mouseangle = { .x = 0.0f, .y = 0.0f, .z = 0.0f };
  for (;;) {
    float dt = kl_timer_tick(&timer);
    kl_input_tick();
    int dx = 0; int dy = 0;
    while (kl_input_poll(&evt)) {
      switch (evt.event.type) {
        case 0x01:
          switch (evt.button.code) {
            case 0x01:
              if (evt.button.isdown) return 0;
              break;
            case 0x46:
              move_f = evt.button.isdown;
              break;
            case 0x30:
              move_l = evt.button.isdown;
              break;
            case 0x42:
              move_b = evt.button.isdown;
              break;
            case 0x33:
              move_r = evt.button.isdown;
              break;
            case 0x21:
              kl_render_set_debug(0);
              break;
            case 0x22:
              kl_render_set_debug(1);
              break;
            case 0x23:
              kl_render_set_debug(2);
              break;
            case 0x24:
              kl_render_set_debug(3);
              break;
            case 0x25:
              kl_render_set_debug(4);
              break;
            case 0x26:
              kl_render_set_debug(5);
              break;
            case 0x27:
              kl_render_set_debug(6);
              break;
            case 0x28:
              kl_render_set_debug(7);
              break;
            case 0x29:
              kl_render_set_debug(8);
              break;
          }
          break;
        case 0x02:
          dx += evt.mouse.dx;
          dy += evt.mouse.dy;
      }
    }
    mouseangle.y = -(float)dx/200.0f;
    mouseangle.x = -(float)dy/200.0f;
    kl_camera_local_rotate(&cam, &mouseangle);
    kl_vec3f_t offset = {
      .x = (move_r ? 1.0f : 0.0f) - (move_l ? 1.0f : 0.0f),
      .y = 0.0f,
      .z = (move_b ? 1.0f : 0.0f) - (move_f ? 1.0f : 0.0f)
    };
    kl_vec3f_scale(&offset, &offset, 320.0f * dt);
    kl_camera_local_move(&cam, &offset);
    kl_render_draw(&cam);
    kl_vid_swap();
  }
}

canonical_original_code:
int main(int argc, char **argv) {
  union kl_evt_generic evt;
  kl_resource_add_dir("./test_assets/", "");
  if (kl_vid_init() < 0) return -1;
  if (kl_input_init() < 0) return -1;
  if (kl_render_init() < 0) return -1;
  int w; int h;
  kl_vid_size(&w, &h);
  struct kl_camera cam = {
    .position = { .x = 0.0f, .y = 0.0f, .z = 0.0f },
    .orientation = { .r = 1.0f, .i = 0.0f, .j = 0.0f, .k = 0.0f },
    .aspect = (float)w/(float)h,
    .fov = 1.047f,
 .viewport = { .x = 0, .y = 0, .z = w, .w = h },
    .near = 1.0f,
    .far = 10000.0f
  };
  kl_terrain_testsphere();
  struct kl_model *model1 = kl_model_load("test_assets/test.iqm");
  kl_render_add_model(model1);
  struct kl_model *model2 = kl_model_load("test_assets/sponza.obj");
  kl_render_add_model(model2);
  struct kl_model *model3 = kl_model_load("test_assets/colorcalibration.obj");
  kl_render_add_model(model3);
  struct kl_vec3f light1_pos = { .x = 500.0f, .y = 400.0f, .z = 0.0f };
  kl_render_add_light(&light1_pos, 1.0f, 0.8f, 0.6f, 25000.0f);
  struct kl_vec3f light2_pos = { .x = -500.0f, .y = 400.0f, .z = 0.0f };
  kl_render_add_light(&light2_pos, 1.0f, 0.8f, 0.6f, 25000.0f);
  struct kl_vec3f light3_pos = { .x = 0.0f, .y = 300.0f, .z = 0.0f };
  struct kl_vec3f envlight_dir = { -2.0f, -2.0f, 1.0f };
  kl_render_set_envlight(&envlight_dir, 0.8f, 0.8f, 1.0f, 0.0f, 1.0f, 0.9f, 0.6f, 0.0f);
  struct kl_timer timer = { .time = -1.0f, .dt = 0.0f};
  _Bool move_f = 0;
  _Bool move_b = 0;
  _Bool move_l = 0;
  _Bool move_r = 0;
  struct kl_vec3f mouseangle = { .x = 0.0f, .y = 0.0f, .z = 0.0f };
  for (;;) {
    float dt = kl_timer_tick(&timer);
    kl_input_tick();
    int dx = 0; int dy = 0;
    while (kl_input_poll(&evt)) {
      switch (evt.event.type) {
        case 0x01:
          switch (evt.button.code) {
            case 0x01:
              if (evt.button.isdown) return 0;
              break;
            case 0x46:
              move_f = evt.button.isdown;
              break;
            case 0x30:
              move_l = evt.button.isdown;
              break;
            case 0x42:
              move_b = evt.button.isdown;
              break;
            case 0x33:
              move_r = evt.button.isdown;
              break;
            case 0x21:
              kl_render_set_debug(0);
              break;
            case 0x22:
              kl_render_set_debug(1);
              break;
            case 0x23:
              kl_render_set_debug(2);
              break;
            case 0x24:
              kl_render_set_debug(3);
              break;
            case 0x25:
              kl_render_set_debug(4);
              break;
            case 0x26:
              kl_render_set_debug(5);
              break;
            case 0x27:
              kl_render_set_debug(6);
              break;
            case 0x28:
              kl_render_set_debug(7);
              break;
            case 0x29:
              kl_render_set_debug(8);
              break;
          }
          break;
        case 0x02:
          dx += evt.mouse.dx;
          dy += evt.mouse.dy;
      }
    }
    mouseangle.y = -(float)dx/200.0f;
    mouseangle.x = -(float)dy/200.0f;
    kl_camera_local_rotate(&cam, &mouseangle);
    struct kl_vec3f offset = {
      .x = (move_r ? 1.0f : 0.0f) - (move_l ? 1.0f : 0.0f),
      .y = 0.0f,
      .z = (move_b ? 1.0f : 0.0f) - (move_f ? 1.0f : 0.0f)
    };
    kl_vec3f_scale(&offset, &offset, 320.0f * dt);
    kl_camera_local_move(&cam, &offset);
    kl_render_draw(&cam);
    kl_vid_swap();
  }
}

variable_types: argc int

variable_types:
argv
char **

variable_types: union kl_evt_generic { struct kl_evt { int type; } event; struct kl_evt_button { struct kl_evt evt; int code; _Bool isdown; } button; struct kl_evt_mouse { struct kl_evt evt; int x; int y; int dx; int dy; } mouse; }

variable_types: w int

variable_types: h int

variable_types: struct kl_camera { struct kl_vec3f { float x; } position; struct kl_quat { float r; } orientation; float aspect; float fov; struct kl_vec4f { float x; } viewport; float near; }

variable_types: struct kl_model { int type; struct kl_sphere { struct kl_vec3f { float x; } center; float radius; } bounds; int winding; union kl_model_bufs { struct kl_model_bufs_prop { unsigned int position; unsigned int texcoord; unsigned int normal; unsigned int tangent; } prop; struct kl_model_bufs_actor { unsigned int position; unsigned int texcoord; unsigned int normal; unsigned int tangent; unsigned int blendidx; unsigned int blendwt; } actor; struct kl_model_bufs_terrain { unsigned int position; unsigned int normal; unsigned int texweight; } terrain; } bufs; unsigned int tris; unsigned int attribs; unsigned int mesh_n; struct kl_mesh { struct kl_material { char path[256]; struct kl_texture_t { char path[256]; unsigned int w; unsigned int id; } *diffuse; struct kl_texture_t *normal; struct kl_texture_t *specular; struct kl_texture_t *emissive; } *material; unsigned int tris_i; } mesh[]; } *

variable_types:
timer
struct kl_timer { float time; }

variable_types:
mouseangle
struct kl_vec3f { float x; }

variable_types: dt float

variable_types: dx int

variable_types: dy int

variable_types:
offset
struct kl_vec3f { float x; }

user_defined_types: union kl_evt_generic { struct kl_evt event; struct kl_evt_button button; struct kl_evt_mouse mouse; }

user_defined_types: struct kl_camera { struct kl_vec3f position; struct kl_quat orientation; float aspect; float fov; struct kl_vec4f viewport; float near; }

user_defined_types: struct kl_model { int type; struct kl_sphere bounds; int winding; union kl_model_bufs bufs; unsigned int tris; unsigned int attribs; unsigned int mesh_n; struct kl_mesh mesh[]; }

user_defined_types: union kl_model_bufs { struct kl_model_bufs_prop prop; struct kl_model_bufs_actor actor; struct kl_model_bufs_terrain terrain; }

user_defined_types: struct kl_mesh { struct kl_material *material; unsigned int tris_i; }

user_defined_types: struct kl_material { char path[256]; struct kl_texture_t *diffuse; struct kl_texture_t *normal; struct kl_texture_t *specular; struct kl_texture_t *emissive; }


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

result = realloc(*(void **)a1, *(_DWORD *)(a1 + 12) * a2);
*(_QWORD *)a1 = result;
*(_DWORD *)(a1 + 8) = a2;
return result;
}

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

result = realloc(*(void **)a1, *(_DWORD *)(a1 + 12) * a2);
*(_QWORD *)a1 = result;
*(_DWORD *)(a1 + 8) = a2;
return result;
}

original_code:
static void array_resize(kl_array_t *array, int size) {
  array->data = realloc(array->data, size * array->item_size);
  array->size = size;
}

canonical_original_code:
static void array_resize(struct kl_array *array, int size) {
  array->data = realloc(array->data, size * array->item_size);
  array->size = size;
}

variable_types: size int