decompiled_code:
unsigned __int64 __fastcall multiboot_align_mmap(unsigned __int64 *a1)
{
unsigned __int64 v1; // rcx
unsigned __int64 v2; // rax
unsigned __int64 v3; // rdx
unsigned __int64 result; // rax

v1 = *a1;
v2 = a1[1];
if ( a1[2] )
{
v3 = (v1 + 4095) & 0xFFFFFFFFFFFFF000LL;
*a1 = v3;
result = (v3 + v2 - v1) & 0xFFFFFFFFFFFFF000LL;
}
else
{
*a1 = v1 & 0xFFFFFFFFFFFFF000LL;
result = ((v1 & 0xFFFFFFFFFFFFF000LL) + v2 + 4095 - v1) & 0xFFFFFFFFFFFFF000LL;
}
a1[1] = result;
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(unsigned __int64 *a1)
{
unsigned __int64 v1; // rcx
unsigned __int64 v2; // rax
unsigned __int64 v3; // rdx
unsigned __int64 result; // rax

v1 = *a1;
v2 = a1[1];
if ( a1[2] )
{
v3 = (v1 + 4095) & 0xFFFFFFFFFFFFF000LL;
*a1 = v3;
result = (v3 + v2 - v1) & 0xFFFFFFFFFFFFF000LL;
}
else
{
*a1 = v1 & 0xFFFFFFFFFFFFF000LL;
result = ((v1 & 0xFFFFFFFFFFFFF000LL) + v2 + 4095 - v1) & 0xFFFFFFFFFFFFF000LL;
}
a1[1] = result;
return result;
}

original_code:
static void multiboot_align_mmap(hy_info_mmap_t *entry)
{
    uintptr_t aligned_addr;
    if (entry->available) {
        aligned_addr = (entry->address + 0xFFF) & ~0xFFF;
        entry->length -= (entry->address - aligned_addr);
        entry->length &= ~0xFFF;
    } else {
        aligned_addr = entry->address & ~0xFFF;
        entry->length += (aligned_addr - entry->address);
        entry->length = (entry->length + 0xFFF) & ~0xFFF;
    }
    entry->address = aligned_addr;
}

canonical_original_code:
static void multiboot_align_mmap(struct hy_info_mmap *entry)
{
    unsigned long long aligned_addr;
    if (entry->available) {
        aligned_addr = (entry->address + 0xFFF) & ~0xFFF;
        entry->length -= (entry->address - aligned_addr);
        entry->length &= ~0xFFF;
    } else {
        aligned_addr = entry->address & ~0xFFF;
        entry->length += (aligned_addr - entry->address);
        entry->length = (entry->length + 0xFFF) & ~0xFFF;
    }
    entry->address = aligned_addr;
}

variable_types: struct hy_info_mmap { unsigned long long address; unsigned long long length; unsigned long long available; unsigned long long padding; } *


repo: zrho/Hydrogen

decompiled_code:
__int64 __fastcall multiboot_parse_mmap(__int64 a1, __int64 a2)
{
_QWORD *v3; // r11
void (__fastcall *v4)(unsigned __int64 *); // r10
__int64 v5; // rdi
__int16 v6; // dx
unsigned __int64 *v7; // rdi
unsigned int *v8; // r8
__int64 v9; // rdx
__int64 v10; // r9
__int64 result; // rax

if ( a2 )
{
v3 = &info_mmap;
v4 = (void (__fastcall *)(unsigned __int64 *))multiboot_align_mmap;
do
{
v5 = *(unsigned __int16 *)(info_root + 154LL);
v6 = v5 + 1;
v7 = (unsigned __int64 *)(*v3 + 32 * v5);
*(_WORD *)(info_root + 154LL) = v6;
*v7 = *(_QWORD *)(a1 + 4);
v7[1] = *(_QWORD *)(a1 + 12);
v7[2] = *(_DWORD *)(a1 + 20) == 1;
v4(v7);
v9 = *v8;
result = v10 - v9;
a1 = (__int64)v8 + v9 + 4;
a2 += v10 - v9;
}
while ( a2 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
_QWORD *v3; // r11
void (__fastcall *v4)(unsigned __int64 *); // r10
__int64 v5; // rdi
__int16 v6; // dx
unsigned __int64 *v7; // rdi
unsigned int *v8; // r8
__int64 v9; // rdx
__int64 v10; // r9
__int64 result; // rax

if ( a2 )
{
v3 = &info_mmap;
v4 = (void (__fastcall *)(unsigned __int64 *))multiboot_align_mmap;
do
{
v5 = *(unsigned __int16 *)(info_root + 154LL);
v6 = v5 + 1;
v7 = (unsigned __int64 *)(*v3 + 32 * v5);
*(_WORD *)(info_root + 154LL) = v6;
*v7 = *(_QWORD *)(a1 + 4);
v7[1] = *(_QWORD *)(a1 + 12);
v7[2] = *(_DWORD *)(a1 + 20) == 1;
v4(v7);
v9 = *v8;
result = v10 - v9;
a1 = (__int64)v8 + v9 + 4;
a2 += v10 - v9;
}
while ( a2 );
}
return result;
}

original_code:
static void multiboot_parse_mmap(multiboot_mmap_t *mmap, size_t length)
{
    while (0 != length) {
        hy_info_mmap_t *hyentry = &info_mmap[info_root->mmap_count++];
        hyentry->address = mmap->address;
        hyentry->length = mmap->length;
        hyentry->available = mmap->type == 1;
        multiboot_align_mmap(hyentry);
        length -= mmap->size + sizeof(uint32_t);
        mmap = (multiboot_mmap_t *) ((uintptr_t) mmap + mmap->size + sizeof(uint32_t));
    }
}

canonical_original_code:
static void multiboot_parse_mmap(struct multiboot_mmap *mmap, unsigned long long length)
{
    while (0 != length) {
        struct hy_info_mmap *hyentry = &info_mmap[info_root->mmap_count++];
        hyentry->address = mmap->address;
        hyentry->length = mmap->length;
        hyentry->available = mmap->type == 1;
        multiboot_align_mmap(hyentry);
        length -= mmap->size + sizeof(unsigned int);
        mmap = (struct multiboot_mmap *) ((unsigned long long) mmap + mmap->size + sizeof(unsigned int));
    }
}

variable_types: struct multiboot_mmap { unsigned int size; unsigned long long address; unsigned long long length; unsigned int type; } *

variable_types:
length
unsigned long long

return_type: void

user_defined_types: struct hy_info_mmap { unsigned long long address; unsigned long long length; unsigned long long available; unsigned long long padding; }


decompiled_code:
void __fastcall multiboot_parse_mods(__int64 a1, __int64 a2)
{
__int64 v2; // rbp
__int64 v3; // rcx
__int64 v4; // r12
__int64 v5; // rdx
const char *v6; // r14
size_t v7; // r15
void *v8; // r13

if ( a2 )
{
v2 = a1;
do
{
v2 += 16LL;
v3 = *(unsigned __int16 *)(info_root + 156LL);
*(_WORD *)(info_root + 156LL) = v3 + 1;
v4 = info_module + 16 * v3;
v5 = *(unsigned int *)(v2 - 16);
*(_QWORD *)(v4 + 2) = v5;
*(_DWORD *)(v4 + 10) = *(_DWORD *)(v2 - 12) - v5;
v6 = (const char *)*(unsigned int *)(v2 - 8);
v7 = strlen(v6);
v8 = (void *)info_string_alloc(v7);
memcpy(v8, v6, v7);
*(_WORD *)v4 = (_WORD)v8 - info_strings;
}
while ( a1 + 16 * a2 != v2 );
}
}

canonical_decompiled_code:
void __fastcall func2(__int64 a1, __int64 a2)
{
__int64 v2; // rbp
__int64 v3; // rcx
__int64 v4; // r12
__int64 v5; // rdx
const char *v6; // r14
size_t v7; // r15
void *v8; // r13

if ( a2 )
{
v2 = a1;
do
{
v2 += 16LL;
v3 = *(unsigned __int16 *)(info_root + 156LL);
*(_WORD *)(info_root + 156LL) = v3 + 1;
v4 = info_module + 16 * v3;
v5 = *(unsigned int *)(v2 - 16);
*(_QWORD *)(v4 + 2) = v5;
*(_DWORD *)(v4 + 10) = *(_DWORD *)(v2 - 12) - v5;
v6 = (const char *)*(unsigned int *)(v2 - 8);
v7 = strlen(v6);
v8 = (void *)info_string_alloc(v7);
memcpy(v8, v6, v7);
*(_WORD *)v4 = (_WORD)v8 - info_strings;
}
while ( a1 + 16 * a2 != v2 );
}
}

original_code:
static void multiboot_parse_mods(multiboot_mod_t *mods, size_t count)
{
    size_t i;
    for (i = 0; i < count; ++i) {
        multiboot_mod_t *mod = &mods[i];
        hy_info_module_t *hymod = &info_module[info_root->module_count++];
        hymod->address = mod->start;
        hymod->length = mod->end - mod->start;
        char *name = (char *) (uintptr_t) mod->cmdline;
        size_t name_len = strlen(name);
        char *name_tbl = info_string_alloc(name_len);
        memcpy(name_tbl, name, name_len);
        hymod->name = ((uintptr_t) name_tbl - (uintptr_t) info_strings);
    }
}

canonical_original_code:
static void multiboot_parse_mods(struct multiboot_mod *mods, unsigned long long count)
{
    unsigned long long i;
    for (i = 0; i < count; ++i) {
        struct multiboot_mod *mod = &mods[i];
        struct hy_info_module *hymod = &info_module[info_root->module_count++];
        hymod->address = mod->start;
        hymod->length = mod->end - mod->start;
        char *name = (char *) (uintptr_t) mod->cmdline;
        unsigned long long name_len = strlen(name);
        char *name_tbl = info_string_alloc(name_len);
        memcpy(name_tbl, name, name_len);
        hymod->name = ((unsigned long long) name_tbl - (unsigned long long) info_strings);
    }
}

variable_types: struct multiboot_mod { unsigned int start; unsigned int end; unsigned int cmdline; unsigned int pad; } *

variable_types:
count
unsigned long long

variable_types:
i
unsigned long long

variable_types: struct hy_info_module { unsigned short name; unsigned long long address; unsigned int length; unsigned short padding; } *

variable_types:
name
char *


decompiled_code:
void multiboot_parse()
{
__int64 v0; // rbp

v0 = *(unsigned int *)(multiboot_info + 24LL);
multiboot_parse_mmap(*(unsigned int *)(multiboot_info + 48LL), *(unsigned int *)(multiboot_info + 44LL));
multiboot_parse_mods(v0, *(unsigned int *)(multiboot_info + 20LL));
}

canonical_decompiled_code:
void func3()
{
__int64 v0; // rbp

v0 = *(unsigned int *)(multiboot_info + 24LL);
func1(*(unsigned int *)(multiboot_info + 48LL), *(unsigned int *)(multiboot_info + 44LL));
func2(v0, *(unsigned int *)(multiboot_info + 20LL));
}

original_code:
void multiboot_parse()
{
    multiboot_mmap_t *mmap = (multiboot_mmap_t *) (uintptr_t) multiboot_info->mmap_addr;
    multiboot_mod_t *mods = (multiboot_mod_t *) (uintptr_t) multiboot_info->mods_addr;
    multiboot_parse_mmap(mmap, multiboot_info->mmap_length);
    multiboot_parse_mods(mods, multiboot_info->mods_count);
}

canonical_original_code:
void multiboot_parse()
{
    struct multiboot_mmap *mmap = (multiboot_mmap_t *) (uintptr_t) multiboot_info->mmap_addr;
    struct multiboot_mod *mods = (multiboot_mod_t *) (uintptr_t) multiboot_info->mods_addr;
    multiboot_parse_mmap(mmap, multiboot_info->mmap_length);
    multiboot_parse_mods(mods, multiboot_info->mods_count);
}

user_defined_types: struct multiboot_mmap { unsigned int size; unsigned long long address; unsigned long long length; unsigned int type; }

user_defined_types: struct multiboot_mod { unsigned int start; unsigned int end; unsigned int cmdline; unsigned int pad; }


decompiled_code:
__int64 lapic_detect()
{
__int64 result; // rax
_BYTE v1[24]; // [rsp+0h] [rbp-18h] BYREF

cpu_cpuid(1LL, v1);
result = *(unsigned int *)(kernel_header + 4LL);
if ( (result & 2) != 0 )
{
if ( (v1[10] & 0x20) != 0 )
{
result = info_root;
*(_DWORD *)(info_root + 4LL) |= 2u;
}
else if ( (result & 4) != 0 )
{
screen_write("PANIC: x2APIC required but not supported.", 0LL, 3LL);
while ( 1 )
;
}
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
_BYTE v1[24]; // [rsp+0h] [rbp-18h] BYREF

cpu_cpuid(1LL, v1);
result = *(unsigned int *)(kernel_header + 4LL);
if ( (result & 2) != 0 )
{
if ( (v1[10] & 0x20) != 0 )
{
result = info_root;
*(_DWORD *)(info_root + 4LL) |= 2u;
}
else if ( (result & 4) != 0 )
{
screen_write("PANIC: x2APIC required but not supported.", 0LL, 3LL);
while ( 1 )
;
}
}
return result;
}

original_code:
void lapic_detect(void)
{
    cpu_cpuid_result_t cpuid;
    cpu_cpuid(0x1, &cpuid);
    bool x2apic_supported = (0 != ((1 << 21) & cpuid.c));
    if (0 != (kernel_header->flags & (1 << 1))) {
        if (x2apic_supported) {
            info_root->flags |= (1 << 1);
        } else if (0 != (kernel_header->flags & (1 << 2))) {
            do { screen_write("PANIC: " "x2APIC required but not supported.", 0, 3); while (1); } while (0);;
        }
    }
}

canonical_original_code:
void lapic_detect()
{
    struct cpu_cpuid_result cpuid;
    cpu_cpuid(0x1, &cpuid);
    char x2apic_supported = (0 != ((1 << 21) & cpuid.c));
    if (0 != (kernel_header->flags & (1 << 1))) {
        if (x2apic_supported) {
            info_root->flags |= (1 << 1);
        } else if (0 != (kernel_header->flags & (1 << 2))) {
            do { screen_write("PANIC: " "x2APIC required but not supported.", 0, 3); while (1); } while (0);;
        }
    }
}

variable_types: struct cpu_cpuid_result { unsigned int a; unsigned int b; unsigned int c; unsigned int d; }


decompiled_code:
__int64 __fastcall lapic_register_read(unsigned __int16 a1)
{
__int64 result; // rax

if ( (*(_BYTE *)(info_root + 4LL) & 2) != 0 )
result = cpu_msr_read((unsigned int)a1 + 2048);
else
result = *(unsigned int *)(*(_QWORD *)(info_root + 10LL) + 16 * a1);
return result;
}

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

if ( (*(_BYTE *)(info_root + 4LL) & 2) != 0 )
result = cpu_msr_read((unsigned int)a1 + 2048);
else
result = *(unsigned int *)(*(_QWORD *)(info_root + 10LL) + 16 * a1);
return result;
}

original_code:
uint32_t lapic_register_read(uint16_t index)
{
    if (!(0 != ((1 << 1) & info_root->flags))) {
        return *((uint32_t *) (info_root->lapic_paddr + index * 0x10));
    } else {
        return cpu_msr_read(0x800 + index);
    }
}

canonical_original_code:
unsigned int lapic_register_read(unsigned short index)
{
    if (!(0 != ((1 << 1) & info_root->flags))) {
        return *((unsigned int *) (info_root->lapic_paddr + index * 0x10));
    } else {
        return cpu_msr_read(0x800 + index);
    }
}

variable_types:
index
unsigned short

return_type: unsigned int


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

if ( (*(_BYTE *)(info_root + 4LL) & 2) != 0 )
return cpu_msr_write((unsigned int)a1 + 2048, a2);
result = *(_QWORD *)(info_root + 10LL);
*(_DWORD *)(result + 16 * a1) = a2;
return result;
}

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

if ( (*(_BYTE *)(info_root + 4LL) & 2) != 0 )
return cpu_msr_write((unsigned int)a1 + 2048, a2);
result = *(_QWORD *)(info_root + 10LL);
*(_DWORD *)(result + 16 * a1) = a2;
return result;
}

original_code:
void lapic_register_write(uint16_t index, uint32_t value)
{
    if (!(0 != ((1 << 1) & info_root->flags))) {
        *((uint32_t *) (info_root->lapic_paddr + index * 0x10)) = value;
    } else {
        cpu_msr_write(0x800 + index, value);
    }
}

canonical_original_code:
void lapic_register_write(unsigned short index, unsigned int value)
{
    if (!(0 != ((1 << 1) & info_root->flags))) {
        *((unsigned int *) (info_root->lapic_paddr + index * 0x10)) = value;
    } else {
        cpu_msr_write(0x800 + index, value);
    }
}

variable_types:
value
unsigned int


decompiled_code:
__int64 lapic_id()
{
unsigned int v0; // er8

v0 = lapic_register_read(2u);
if ( (*(_BYTE *)(info_root + 4LL) & 2) == 0 )
v0 >>= 24;
return v0;
}

canonical_decompiled_code:
__int64 func3()
{
unsigned int v0; // er8

v0 = func1(2u);
if ( (*(_BYTE *)(info_root + 4LL) & 2) == 0 )
v0 >>= 24;
return v0;
}

original_code:
uint32_t lapic_id(void)
{
    uint32_t id = lapic_register_read(0x02);
    return ((0 != ((1 << 1) & info_root->flags))) ? id : (id >> 24);
}

canonical_original_code:
unsigned int lapic_id()
{
    unsigned int id = lapic_register_read(0x02);
    return ((0 != ((1 << 1) & info_root->flags))) ? id : (id >> 24);
}

variable_types:
id
unsigned int


decompiled_code:
__int64 lapic_eoi()
{
return lapic_register_write(0xBu, 0);
}

canonical_decompiled_code:
__int64 func4()
{
return func2(0xBu, 0);
}

original_code:
void lapic_eoi(void)
{
    lapic_register_write(0x0B, 0);
}

canonical_original_code:
void lapic_eoi()
{
    lapic_register_write(0x0B, 0);
}


decompiled_code:
__int64 __fastcall lapic_ipi(unsigned int a1, __int64 a2)
{
if ( (*(_BYTE *)(info_root + 4LL) & 2) != 0 )
return cpu_msr_write(2096LL, a1 | (unsigned __int64)(a2 << 32));
lapic_register_write(0x31u, (_DWORD)a2 << 24);
return lapic_register_write(0x30u, a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1, __int64 a2)
{
if ( (*(_BYTE *)(info_root + 4LL) & 2) != 0 )
return cpu_msr_write(2096LL, a1 | (unsigned __int64)(a2 << 32));
func2(0x31u, (_DWORD)a2 << 24);
return func2(0x30u, a1);
}

original_code:
void lapic_ipi(uint32_t icr_low, uint32_t destination)
{
    if (!(0 != ((1 << 1) & info_root->flags))) {
        lapic_register_write(0x31, (destination & 0xFF) << 24);
        lapic_register_write(0x30, icr_low);
    } else {
        uint64_t icr = icr_low | ((uint64_t) destination << 32);
        cpu_msr_write(0x800 + 0x30, icr);
    }
}

canonical_original_code:
void lapic_ipi(unsigned int icr_low, unsigned int destination)
{
    if (!(0 != ((1 << 1) & info_root->flags))) {
        lapic_register_write(0x31, (destination & 0xFF) << 24);
        lapic_register_write(0x30, icr_low);
    } else {
        unsigned long long icr = icr_low | ((uint64_t) destination << 32);
        cpu_msr_write(0x800 + 0x30, icr);
    }
}

variable_types:
destination
unsigned int

variable_types:
icr
unsigned long long


decompiled_code:
__int64 __fastcall lapic_timer_update(unsigned int a1, unsigned __int8 a2, char a3, char a4)
{
int v5; // ebp

v5 = a4;
lapic_register_write(0x3Eu, 0xBu);
lapic_register_write(0x32u, (v5 << 17) | a2 | (a3 << 16));
return lapic_register_write(0x38u, a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1, unsigned __int8 a2, char a3, char a4)
{
int v5; // ebp

v5 = a4;
func2(0x3Eu, 0xBu);
func2(0x32u, (v5 << 17) | a2 | (a3 << 16));
return func2(0x38u, a1);
}

original_code:
void lapic_timer_update(uint32_t init_count, uint8_t vector, bool mask, bool periodic)
{
    uint32_t lvt = vector | (mask << 16) | (periodic << 17);
    lapic_register_write(0x3E, 0xB);
    lapic_register_write(0x32, lvt);
    lapic_register_write(0x38, init_count);
}

canonical_original_code:
void lapic_timer_update(unsigned int init_count, unsigned char vector, char mask, char periodic)
{
    unsigned int lvt = vector | (mask << 16) | (periodic << 17);
    lapic_register_write(0x3E, 0xB);
    lapic_register_write(0x32, lvt);
    lapic_register_write(0x38, init_count);
}

variable_types:
vector
unsigned char

variable_types: mask char

variable_types: periodic char

variable_types:
lvt
unsigned int


decompiled_code:
__int64 lapic_setup()
{
__int64 v0; // rax
__int64 v1; // rbx
__int64 v2; // r12
char v4; // al

v0 = cpu_msr_read(27LL);
v1 = v0;
v2 = v0 & 0x400;
if ( (v0 & 0x800) == 0 )
{
BYTE1(v1) = BYTE1(v0) | 8;
cpu_msr_write(27LL, v1);
}
if ( (*(_BYTE *)(info_root + 4LL) & 2) != 0 && !v2 )
cpu_msr_write(27LL, v1 | 0x400);
lapic_timer_update(0xFFFFFFFF, 0, 1, 0);
lapic_register_write(8u, 0);
lapic_register_write(0x34u, 0x10000u);
lapic_register_write(0x35u, 0x8700u);
lapic_register_write(0x36u, 0x400u);
lapic_register_write(0x37u, 0x10000u);
if ( (*(_BYTE *)(info_root + 4LL) & 2) == 0 )
{
v4 = lapic_id();
lapic_register_write(0xDu, 1 << (v4 & 7));
}
return lapic_register_write(0xFu, 0x120u);
}

original_code:
void lapic_setup(void)
{
    uint64_t base_msr = cpu_msr_read(0x01B);
    bool en = (0 != (base_msr & (1 << 11)));
    bool extd = (0 != (base_msr & (1 << 10)));
    if (!en) {
        base_msr |= (1 << 11);
        cpu_msr_write(0x01B, base_msr);
    }
    if ((0 != ((1 << 1) & info_root->flags)) && !extd) {
        base_msr |= (1 << 10);
        cpu_msr_write(0x01B, base_msr);
    }
    lapic_timer_update(0xFFFFFFFF, 0, 1, 0);
    lapic_register_write(0x08, 0x0);
    lapic_register_write(0x34, (1 << 16));
    lapic_register_write(0x35, (0b111 << 8) | (1 << 15));
    lapic_register_write(0x36, (0b100 << 8));
    lapic_register_write(0x37, (1 << 16));
    if (!(0 != ((1 << 1) & info_root->flags))) {
        lapic_register_write(0x0D, (1 << (lapic_id() % 8)));
    }
    lapic_register_write(0x0F, 0x20 | (1 << 8));
}

canonical_original_code:
void lapic_setup()
{
    unsigned long long base_msr = cpu_msr_read(0x01B);
    char en = (0 != (base_msr & (1 << 11)));
    char extd = (0 != (base_msr & (1 << 10)));
    if (!en) {
        base_msr |= (1 << 11);
        cpu_msr_write(0x01B, base_msr);
    }
    if ((0 != ((1 << 1) & info_root->flags)) && !extd) {
        base_msr |= (1 << 10);
        cpu_msr_write(0x01B, base_msr);
    }
    lapic_timer_update(0xFFFFFFFF, 0, 1, 0);
    lapic_register_write(0x08, 0x0);
    lapic_register_write(0x34, (1 << 16));
    lapic_register_write(0x35, (0b111 << 8) | (1 << 15));
    lapic_register_write(0x36, (0b100 << 8));
    lapic_register_write(0x37, (1 << 16));
    if (!(0 != ((1 << 1) & info_root->flags))) {
        lapic_register_write(0x0D, (1 << (lapic_id() % 8)));
    }
    lapic_register_write(0x0F, 0x20 | (1 << 8));
}

variable_types: en char

variable_types: extd char


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

idt_intgate((char *)&idt_data + 768, &lapic_timer_calibrate_handler, 8LL, 0LL);
pit_freq_set(100LL);
pit_route();
v0 = lapic_timer_calibrate_worker();
v1 = info_cpu;
*(_DWORD *)(v1 + 18LL * (unsigned int)lapic_id() + 10) = v0;
pit_mask();
return idt_setup_loader();
}

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

idt_intgate((char *)&idt_data + 768, &lapic_timer_calibrate_handler, 8LL, 0LL);
pit_freq_set(100LL);
pit_route();
v0 = lapic_timer_calibrate_worker();
v1 = info_cpu;
*(_DWORD *)(v1 + 18LL * (unsigned int)func3() + 10) = v0;
pit_mask();
return idt_setup_loader();
}

original_code:
void lapic_timer_calibrate(void)
{
    extern uint32_t lapic_timer_calibrate_worker(void);
    extern void lapic_timer_calibrate_handler(void);
    uint8_t vector = 0x30;
    idt_intgate(&idt_data[vector], (uintptr_t) &lapic_timer_calibrate_handler, 0x08, 0x0);
    pit_freq_set(100);
    pit_route();
    uint32_t ticks_per_second = lapic_timer_calibrate_worker();
    info_cpu[lapic_id()].lapic_timer_freq = ticks_per_second;
    pit_mask();
    idt_setup_loader();
}

canonical_original_code:
void lapic_timer_calibrate()
{
    extern unsigned int lapic_timer_calibrate_worker();
    extern void lapic_timer_calibrate_handler();
    unsigned char vector = 0x30;
    idt_intgate(&idt_data[vector], (unsigned long long) &lapic_timer_calibrate_handler, 0x08, 0x0);
    pit_freq_set(100);
    pit_route();
    unsigned int ticks_per_second = lapic_timer_calibrate_worker();
    info_cpu[lapic_id()].lapic_timer_freq = ticks_per_second;
    pit_mask();
    idt_setup_loader();
}


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


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);;
}


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:
__int64 idt_setup_loader()
{
char *v0; // rdi
void *v1; // r10
void (__fastcall *v2)(__int64, unsigned __int64, __int16, char); // r8
__int16 v3; // dx
char *v4; // r9
__int64 v5; // rdx
__int64 (__fastcall *v6)(char *, void (__fastcall __noreturn *)(), __int64, _QWORD); // r8

v0 = idt_data;
v1 = &idt_null_handler;
v2 = (void (__fastcall *)(__int64, unsigned __int64, __int16, char))idt_intgate;
do
{
v2((__int64)v0, (unsigned __int64)v1, 8, 0);
v0 += 16;
}
while ( v0 != v4 );
v2((__int64)&idt_data[224], (unsigned __int64)idt_fault_pf, v3, 0);
return v6(&idt_data[208], idt_fault_gp, v5, 0LL);
}

canonical_decompiled_code:
__int64 func4()
{
char *v0; // rdi
void *v1; // r10
void (__fastcall *v2)(__int64, unsigned __int64, __int16, char); // r8
__int16 v3; // dx
char *v4; // r9
__int64 v5; // rdx
__int64 (__fastcall *v6)(char *, void (__fastcall __noreturn *)(), __int64, _QWORD); // r8

v0 = idt_data;
v1 = &idt_null_handler;
v2 = (void (__fastcall *)(__int64, unsigned __int64, __int16, char))idt_intgate;
do
{
v2((__int64)v0, (unsigned __int64)v1, 8, 0);
v0 += 16;
}
while ( v0 != v4 );
v2((__int64)&idt_data[224], (unsigned __int64)idt_fault_pf, v3, 0);
return v6(&idt_data[208], idt_fault_gp, v5, 0LL);
}

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()
{
__int64 v0; // r8
char *v1; // r11
unsigned __int64 (__fastcall *v2)(__int64, unsigned __int64, __int16, char); // r10
__int64 v3; // r9
unsigned __int64 v4; // rsi
unsigned __int64 result; // rax

v0 = 0LL;
v1 = idt_data;
v2 = idt_intgate;
v3 = *(_QWORD *)(kernel_header + 56LL);
do
{
v4 = 0LL;
if ( v3 )
v4 = *(_QWORD *)(v3 + 8 * v0);
result = v2((__int64)&v1[16 * v0], v4, 8, 0);
}
while ( v0 != 256 );
return result;
}

canonical_decompiled_code:
unsigned __int64 func5()
{
__int64 v0; // r8
char *v1; // r11
unsigned __int64 (__fastcall *v2)(__int64, unsigned __int64, __int16, char); // r10
__int64 v3; // r9
unsigned __int64 v4; // rsi
unsigned __int64 result; // rax

v0 = 0LL;
v1 = idt_data;
v2 = idt_intgate;
v3 = *(_QWORD *)(kernel_header + 56LL);
do
{
v4 = 0LL;
if ( v3 )
v4 = *(_QWORD *)(v3 + 8 * v0);
result = v2((__int64)&v1[16 * v0], v4, 8, 0);
}
while ( v0 != 256 );
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)
{
__int64 v1; // rdx
__int64 result; // rax
__int64 v3; // rax

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

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

v1 = *a1;
for ( result = 0LL; (_BYTE)v1; result = v3 & 0xFFFFFFF )
{
++a1;
v3 = v1 + 16 * result;
if ( (v3 & 0xF0000000) != 0 )
v3 ^= (unsigned __int64)((unsigned int)v3 & 0xF0000000) >> 24;
v1 = *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


decompiled_code:
__int64 __fastcall elf64_shdr_find(int a1, __int64 a2)
{
__int64 result; // rax
__int64 v3; // rdx

result = a2 + *(_QWORD *)(a2 + 40);
if ( !*(_WORD *)(a2 + 60) )
return 0LL;
v3 = 0LL;
while ( *(_DWORD *)(result + 4) != a1 )
{
++v3;
result += 64LL;
if ( v3 == *(unsigned __int16 *)(a2 + 60) )
return 0LL;
}
return result;
}

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

result = a2 + *(_QWORD *)(a2 + 40);
if ( !*(_WORD *)(a2 + 60) )
return 0LL;
v3 = 0LL;
while ( *(_DWORD *)(result + 4) != a1 )
{
++v3;
result += 64LL;
if ( v3 == *(unsigned __int16 *)(a2 + 60) )
return 0LL;
}
return result;
}

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; // rbp
unsigned __int64 v4; // rcx
__int64 v5; // rbx
__int64 v6; // r13
__int64 v7; // r14
unsigned int *v8; // r15
unsigned __int64 v10; // [rsp+8h] [rbp-40h]

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

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
_BYTE *v3; // rdi
unsigned __int64 v4; // rdx
_BYTE *v5; // r8

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

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

v2 = *a1;
if ( !a2 || !v2 )
return a1;
v3 = a1 + 1;
v4 = 0LL;
do
{
v5 = v3++;
v4 += v2 == 10;
v2 = *(v3 - 1);
}
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)
{
__int64 *v2; // rbx

v2 = &ui_pages[2 * (a1 - (a1 / 5 + (((0xCCCCCCCCCCCCCCCDLL * (unsigned __int128)a1) >> 64) & 0xFFFFFFFFFFFFFFFCLL)))];
screen_clear();
ui_display_header((unsigned __int8 **)v2);
return ui_display_body_isra_0((char *)v2[1], a2);
}

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

v2 = &ui_pages[2 * (a1 - (a1 / 5 + (((0xCCCCCCCCCCCCCCCDLL * (unsigned __int128)a1) >> 64) & 0xFFFFFFFFFFFFFFFCLL)))];
func3();
func5((unsigned __int8 **)v2);
return func6((char *)v2[1], 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:
unsigned __int64 *ui_switch_left()
{
unsigned __int64 *result; // rax

result = ui_page_current;
if ( ui_page_current[0] )
{
--ui_page_current[0];
ui_line_current = 0LL;
result = (unsigned __int64 *)ui_display(ui_page_current[0], 0LL);
}
return result;
}

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

result = ui_page_current;
if ( ui_page_current[0] )
{
--ui_page_current[0];
ui_line_current = 0LL;
result = (unsigned __int64 *)func7(ui_page_current[0], 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:
unsigned __int64 *ui_switch_right()
{
unsigned __int64 *result; // rax

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

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

result = ui_page_current;
if ( ui_page_current[0] <= 3 )
{
++ui_page_current[0];
ui_line_current = 0LL;
result = (unsigned __int64 *)func7(ui_page_current[0], 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:
unsigned __int64 *ui_scroll_up()
{
unsigned __int64 *result; // rax

result = &ui_line_current;
if ( ui_line_current )
result = (unsigned __int64 *)ui_display(ui_page_current[0], --ui_line_current);
return result;
}

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

result = &ui_line_current;
if ( ui_line_current )
result = (unsigned __int64 *)func7(ui_page_current[0], --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[0], ++ui_line_current);
}

canonical_decompiled_code:
__int64 func11()
{
return func7(ui_page_current[0], ++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
__int16 v1; // ax
__int64 result; // rax

v0 = (char *)info_root;
*((_WORD *)info_root + 4) = 20480;
*(_QWORD *)(v0 + 26) = &idt_data;
*(_QWORD *)(v0 + 34) = &gdt_data;
v1 = (__int16)info_mmap;
*(_DWORD *)v0 = 1380211016;
*((_WORD *)v0 + 71) = v1 - (_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
__int16 v1; // ax
__int64 result; // rax

v0 = (char *)info_root;
*((_WORD *)info_root + 4) = 20480;
*(_QWORD *)(v0 + 26) = &idt_data;
*(_QWORD *)(v0 + 34) = &gdt_data;
v1 = (__int16)info_mmap;
*(_DWORD *)v0 = 1380211016;
*((_WORD *)v0 + 71) = v1 - (_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_string_space -= v1;
info_strings_next = (char *)info_strings_next + 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_string_space -= v1;
info_strings_next = (char *)info_strings_next + 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; *(_WORD *)(result - 2) = 3872 )
result += 2LL;
return result;
}

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

for ( result = 753664LL; result != 757664; *(_WORD *)(result - 2) = 3872 )
result += 2LL;
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

decompiled_code:
void __fastcall maf(__int64 a1)
{
int v1; // ebx
int v2; // eax
__int16 v3; // [rsp+0h] [rbp-Ch] BYREF
__int16 v4[5]; // [rsp+2h] [rbp-Ah] BYREF

if ( a1 )
{
if ( strlen((const char *)a1) > 4 )
{
v3 = *(_WORD *)(a1 + 4);
v4[0] = *(_WORD *)(a1 + 6);
v1 = hex_to_int((const char *)&v3);
v2 = hex_to_int((const char *)v4);
printf("%d\n", (unsigned int)(((v1 << 8) + v2) / 100));
}
}
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1)
{
int v1; // ebx
int v2; // eax
__int16 v3; // [rsp+0h] [rbp-Ch] BYREF
__int16 v4[5]; // [rsp+2h] [rbp-Ah] BYREF

if ( a1 )
{
if ( strlen((const char *)a1) > 4 )
{
v3 = *(_WORD *)(a1 + 4);
v4[0] = *(_WORD *)(a1 + 6);
v1 = func0((const char *)&v3);
v2 = func0((const char *)v4);
printf("%d\n", (unsigned int)(((v1 << 8) + v2) / 100));
}
}
}

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
__int16 v4[5]; // [rsp+Eh] [rbp-Ah] BYREF

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

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

LODWORD(v1) = printf("-%s#\n", s);
if ( s )
{
v1 = strlen(s);
if ( v1 > 2 )
{
v4[0] = *((_WORD *)s + 1);
v2 = func0((const char *)v4);
LODWORD(v1) = printf("%d\n", (unsigned int)(100 * 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

decompiled_code:
void __fastcall rpm(__int64 a1)
{
int v1; // ebx
int v2; // eax
bool v3; // sf
int v4; // eax
int v5; // esi
__int16 v6; // [rsp+0h] [rbp-Ch] BYREF
__int16 v7[5]; // [rsp+2h] [rbp-Ah] BYREF

if ( a1 && strlen((const char *)a1) > 4 )
{
v6 = *(_WORD *)(a1 + 4);
v7[0] = *(_WORD *)(a1 + 6);
v1 = hex_to_int((const char *)&v6);
v2 = hex_to_int((const char *)v7);
v1 <<= 8;
v3 = v1 + v2 < 0;
v4 = v1 + v2;
v5 = v4 + 3;
if ( !v3 )
v5 = v4;
printf("%d\n", (unsigned int)(v5 >> 2));
}
}

canonical_decompiled_code:
void __fastcall func4(__int64 a1)
{
int v1; // ebx
int v2; // eax
bool v3; // sf
int v4; // eax
int v5; // esi
__int16 v6; // [rsp+0h] [rbp-Ch] BYREF
__int16 v7[5]; // [rsp+2h] [rbp-Ah] BYREF

if ( a1 && strlen((const char *)a1) > 4 )
{
v6 = *(_WORD *)(a1 + 4);
v7[0] = *(_WORD *)(a1 + 6);
v1 = func0((const char *)&v6);
v2 = func0((const char *)v7);
v1 <<= 8;
v3 = v1 + v2 < 0;
v4 = v1 + v2;
v5 = v4 + 3;
if ( !v3 )
v5 = v4;
printf("%d\n", (unsigned int)(v5 >> 2));
}
}

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
__int16 v2[5]; // [rsp+0h] [rbp-Ah] BYREF

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

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

if ( a1 )
{
if ( strlen((const char *)a1) > 4 )
{
v2[0] = *(_WORD *)(a1 + 4);
v1 = func0((const char *)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:
// attributes: thunk
void __fastcall engine_coolant_temp(__int64 a1)
{
engine_oil_temp(a1);
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1)
{
func5(a1);
}

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:
// attributes: thunk
void __fastcall engine_intake_temp(__int64 a1)
{
engine_oil_temp(a1);
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1)
{
func5(a1);
}

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
__int16 v2[5]; // [rsp+0h] [rbp-Ah] BYREF

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

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

if ( a1 )
{
if ( strlen((const char *)a1) > 4 )
{
v2[0] = *(_WORD *)(a1 + 4);
v1 = func0((const char *)v2);
printf("%d\n", (unsigned int)(100 * 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
__int16 v2[5]; // [rsp+0h] [rbp-Ah] BYREF

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

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

if ( a1 )
{
if ( strlen((const char *)a1) > 4 )
{
v2[0] = *(_WORD *)(a1 + 4);
v1 = func0((const char *)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; // ebp

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; // ebp

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:
__int64 __fastcall reordercpy32(__int64 a1, __int64 a2, unsigned __int64 a3)
{
__int64 result; // rax

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

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

result = a3 & 3;
if ( (a3 & 3) != 0 )
_assert_fail("bytes % 4 == 0", "bsm.c", 0x23u, "reordercpy32");
if ( a3 >> 2 )
{
do
{
*(_DWORD *)(a1 + result) = *(_DWORD *)(a2 + result);
result += 4LL;
}
while ( (a3 & 0xFFFFFFFFFFFFFFFCLL) != result );
}
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:
// attributes: thunk
__int64 __fastcall bsm_normals_bytes(__int64 a1)
{
return bsm_positions_bytes_localalias(a1);
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
return func4(a1);
}

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; // rax
__int64 v8; // rsi

v7 = bsm_positions_bytes_localalias(a3);
v8 = *(int *)(a3 + 68);
if ( v7 + v8 > a2 )
return 0LL;
reordercpy32(a4, a1 + v8, v7);
return 1LL;
}

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

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

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; // rax
__int64 v8; // rsi

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

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

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

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, __int64 a4)
{
__int64 v7; // rax
__int64 v8; // rsi
int v10; // ebp
float *v11; // rdi

v7 = bsm_normals_bytes(a3);
v8 = *(int *)(a3 + 76);
if ( v7 + v8 > a2 )
return 0LL;
v10 = 0;
reordercpy32(a4, a1 + v8, v7);
if ( *(int *)(a3 + 64) > 0 )
{
do
{
v11 = (float *)a4;
++v10;
a4 += 12LL;
normalize_normal(v11);
}
while ( *(_DWORD *)(a3 + 64) > v10 );
}
return 1LL;
}

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, __int64 a4)
{
__int64 v7; // rax
__int64 v8; // rsi
int v10; // ebp
float *v11; // rdi

v7 = bsm_tangents_bytes(a3);
v8 = *(int *)(a3 + 80);
if ( v7 + v8 > a2 )
return 0LL;
v10 = 0;
reordercpy32(a4, a1 + v8, v7);
if ( *(int *)(a3 + 64) > 0 )
{
do
{
v11 = (float *)a4;
++v10;
a4 += 16LL;
normalize_tangent(v11);
}
while ( *(_DWORD *)(a3 + 64) > v10 );
}
return 1LL;
}

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; // rax
__int64 v8; // rsi

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

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; // rax
__int64 v8; // rsi

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

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; // rax
__int64 v8; // rsi

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

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; // rax
__int64 v8; // rsi

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

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; // rax
__int64 v8; // rsi

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

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; // rax
__int64 v8; // rsi

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

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

decompiled_code:
char *__fastcall branch_new(__int64 a1, __int64 a2)
{
char *v2; // r12
__m128i v3; // xmm0
char *result; // rax
__m128i v5[3]; // [rsp+0h] [rbp-30h] BYREF

v2 = (char *)malloc(0x28uLL);
kl_sphere_merge(v5, a1 + 4, a2 + 4);
v3 = _mm_loadu_si128(v5);
result = v2;
*((_QWORD *)v2 + 3) = a1;
*((_QWORD *)v2 + 4) = a2;
*(_DWORD *)v2 = 2;
*(__m128i *)(v2 + 4) = v3;
return result;
}

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:
void __fastcall node_dist(float *a1, float *a2)
{
kl_vec3f_dist_isra_0(a1[1], a1[2], a1[3], a2[1], a2[2], a2[3]);
}

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, float *a2)
{
float *v3; // r14
float *v4; // r15
__int64 v5; // r15

if ( !a1 )
return (char *)a2;
if ( *(_DWORD *)a1 == 1 )
return branch_new(a1, (__int64)a2);
if ( *(_DWORD *)a1 != 2 )
return 0LL;
v3 = *(float **)(a1 + 24);
v4 = *(float **)(a1 + 32);
node_dist(a2, v3);
node_dist(a2, v4);
v5 = leaf_insert(v4, a2);
*(_QWORD *)(a1 + 24) = v3;
*(_QWORD *)(a1 + 32) = v5;
kl_sphere_merge(a1 + 4, v3 + 1, v5 + 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; } *