decompiled_code:
void setup_idt()
{
memset(idt_entries, 0, 0x800uLL);
setup_idt_gate(0, (int)&isr0);
setup_idt_gate(1, (int)&isr1);
setup_idt_gate(2, (int)&isr2);
setup_idt_gate(3, (int)&isr3);
setup_idt_gate(4, (int)&isr4);
setup_idt_gate(5, (int)&isr5);
setup_idt_gate(6, (int)&isr6);
setup_idt_gate(7, (int)&isr7);
setup_idt_gate(8, (int)&isr8);
setup_idt_gate(9, (int)&isr9);
setup_idt_gate(10, (int)&isr10);
setup_idt_gate(11, (int)&isr11);
setup_idt_gate(12, (int)&isr12);
setup_idt_gate(13, (int)&isr13);
setup_idt_gate(14, (int)&isr14);
setup_idt_gate(15, (int)&isr15);
setup_idt_gate(16, (int)&isr16);
setup_idt_gate(17, (int)&isr17);
setup_idt_gate(18, (int)&isr18);
setup_idt_gate(19, (int)&isr19);
setup_idt_gate(20, (int)&isr20);
setup_idt_gate(21, (int)&isr21);
setup_idt_gate(22, (int)&isr22);
setup_idt_gate(23, (int)&isr23);
setup_idt_gate(24, (int)&isr24);
setup_idt_gate(25, (int)&isr25);
setup_idt_gate(26, (int)&isr26);
setup_idt_gate(27, (int)&isr27);
setup_idt_gate(28, (int)&isr28);
setup_idt_gate(29, (int)&isr29);
setup_idt_gate(30, (int)&isr30);
setup_idt_gate(31, (int)&isr31);
}

original_code:
void setup_idt() {
    memset(&idt_entries, 0, sizeof(struct idt_entry) * 256);
    setup_idt_gate(0, &isr0);
    setup_idt_gate(1, &isr1);
    setup_idt_gate(2, &isr2);
    setup_idt_gate(3, &isr3);
    setup_idt_gate(4, &isr4);
    setup_idt_gate(5, &isr5);
    setup_idt_gate(6, &isr6);
    setup_idt_gate(7, &isr7);
    setup_idt_gate(8, &isr8);
    setup_idt_gate(9, &isr9);
    setup_idt_gate(10, &isr10);
    setup_idt_gate(11, &isr11);
    setup_idt_gate(12, &isr12);
    setup_idt_gate(13, &isr13);
    setup_idt_gate(14, &isr14);
    setup_idt_gate(15, &isr15);
    setup_idt_gate(16, &isr16);
    setup_idt_gate(17, &isr17);
    setup_idt_gate(18, &isr18);
    setup_idt_gate(19, &isr19);
    setup_idt_gate(20, &isr20);
    setup_idt_gate(21, &isr21);
    setup_idt_gate(22, &isr22);
    setup_idt_gate(23, &isr23);
    setup_idt_gate(24, &isr24);
    setup_idt_gate(25, &isr25);
    setup_idt_gate(26, &isr26);
    setup_idt_gate(27, &isr27);
    setup_idt_gate(28, &isr28);
    setup_idt_gate(29, &isr29);
    setup_idt_gate(30, &isr30);
    setup_idt_gate(31, &isr31);
}


repo: chicyberwarrior/wos

decompiled_code:
__int64 remap_pic()
{
outportb(32LL, 17LL);
outportb(160LL, 17LL);
outportb(33LL, 32LL);
outportb(161LL, 40LL);
outportb(33LL, 4LL);
outportb(161LL, 2LL);
outportb(33LL, 1LL);
outportb(161LL, 1LL);
outportb(33LL, 252LL);
return outportb(161LL, 255LL);
}

canonical_decompiled_code:
__int64 func0()
{
outportb(32LL, 17LL);
outportb(160LL, 17LL);
outportb(33LL, 32LL);
outportb(161LL, 40LL);
outportb(33LL, 4LL);
outportb(161LL, 2LL);
outportb(33LL, 1LL);
outportb(161LL, 1LL);
outportb(33LL, 252LL);
return outportb(161LL, 255LL);
}

original_code:
void remap_pic() {
    outportb(0x20, 0x11);
    outportb(0xA0, 0x11);
    outportb(0x21, 0x20);
    outportb(0xA1, 0x28);
    outportb(0x21, 0x04);
    outportb(0xA1, 0x02);
    outportb(0x21, 0x01);
    outportb(0xA1, 0x01);
    outportb(0x21,0xfc);
    outportb(0xa1,0xff);
}

return_type: void


decompiled_code:
__int64 setup_irq_gates()
{
setup_idt_gate(32LL, &irq0);
setup_idt_gate(33LL, &irq1);
setup_idt_gate(34LL, &irq2);
setup_idt_gate(35LL, &irq3);
setup_idt_gate(36LL, &irq4);
setup_idt_gate(37LL, &irq5);
setup_idt_gate(38LL, &irq6);
setup_idt_gate(39LL, &irq7);
setup_idt_gate(40LL, &irq8);
setup_idt_gate(41LL, &irq9);
setup_idt_gate(42LL, &irq10);
setup_idt_gate(43LL, &irq11);
setup_idt_gate(44LL, &irq12);
setup_idt_gate(45LL, &irq13);
setup_idt_gate(46LL, &irq14);
return setup_idt_gate(47LL, &irq15);
}

canonical_decompiled_code:
__int64 func1()
{
setup_idt_gate(32LL, &irq0);
setup_idt_gate(33LL, &irq1);
setup_idt_gate(34LL, &irq2);
setup_idt_gate(35LL, &irq3);
setup_idt_gate(36LL, &irq4);
setup_idt_gate(37LL, &irq5);
setup_idt_gate(38LL, &irq6);
setup_idt_gate(39LL, &irq7);
setup_idt_gate(40LL, &irq8);
setup_idt_gate(41LL, &irq9);
setup_idt_gate(42LL, &irq10);
setup_idt_gate(43LL, &irq11);
setup_idt_gate(44LL, &irq12);
setup_idt_gate(45LL, &irq13);
setup_idt_gate(46LL, &irq14);
return setup_idt_gate(47LL, &irq15);
}

original_code:
void setup_irq_gates() {
    setup_idt_gate(32, irq0);
    setup_idt_gate(33, irq1);
    setup_idt_gate(34, irq2);
    setup_idt_gate(35, irq3);
    setup_idt_gate(36, irq4);
    setup_idt_gate(37, irq5);
    setup_idt_gate(38, irq6);
    setup_idt_gate(39, irq7);
    setup_idt_gate(40, irq8);
    setup_idt_gate(41, irq9);
    setup_idt_gate(42, irq10);
    setup_idt_gate(43, irq11);
    setup_idt_gate(44, irq12);
    setup_idt_gate(45, irq13);
    setup_idt_gate(46, irq14);
    setup_idt_gate(47, irq15);
}


decompiled_code:
__int64 __fastcall irq_ack(unsigned int a1)
{
if ( a1 > 0xB )
outportb(160LL, 32LL);
return outportb(32LL, 32LL);
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1)
{
if ( a1 > 0xB )
outportb(160LL, 32LL);
return outportb(32LL, 32LL);
}

original_code:
void irq_ack(unsigned int irq_no) {
    if (irq_no >= 12) {
        outportb(0xA0, 0x20);
    }
    outportb(0x20, 0x20);
}


outportb

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

result = a2;
__outbyte(a1, a2);
return result;
}

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

result = a2;
__outbyte(a1, a2);
return result;
}

original_code:
void outportb(unsigned short port, unsigned char data) {
    asm volatile ("outb %1, %0" : : "dN" (port), "a" (data));
}

variable_types:
port
unsigned short

variable_types:
data
unsigned char


inportb

decompiled_code:
unsigned __int8 __fastcall inportb(unsigned __int16 a1)
{
return __inbyte(a1);
}

canonical_decompiled_code:
unsigned __int8 __fastcall func2(unsigned __int16 a1)
{
return __inbyte(a1);
}

original_code:
unsigned char inportb(unsigned short port) {
    unsigned char rv;
    asm volatile ("inb %1, %0" : "=a" (rv) : "dN" (port));
    return rv;
}

variable_types:
rv
unsigned char

return_type: unsigned char


decompiled_code:
void __noreturn halt_and_catch_fire()
{
printk("SYSTEM HALTED!");
while ( 1 )
;
}

canonical_decompiled_code:
void __noreturn func3()
{
printk("SYSTEM HALTED!");
while ( 1 )
;
}

original_code:
void halt_and_catch_fire() {
    printk("SYSTEM HALTED!");
    for(;;) {}
}


panic

decompiled_code:
void __fastcall __noreturn panic(const char *a1)
{
if ( a1 )
printk("PANIC MESSAGE: %s\n", a1);
halt_and_catch_fire();
}

canonical_decompiled_code:
void __fastcall __noreturn func4(const char *a1)
{
if ( a1 )
printk("PANIC MESSAGE: %s\n", a1);
func3();
}

original_code:
void panic(char * msg) {
    if(msg != 0) {
        printk("PANIC MESSAGE: %s\n", msg);
    }
    halt_and_catch_fire();
}

canonical_original_code:
void panic(char *msg) {
    if(msg != 0) {
        printk("PANIC MESSAGE: %s\n", msg);
    }
    halt_and_catch_fire();
}

variable_types:
msg
char *


decompiled_code:
__int64 kbd_handler()
{
__int16 v0; // ax
__int16 v1; // si
__int64 result; // rax

v0 = inportb(96LL);
if ( (v0 & 0x80u) != 0 )
{
if ( (unsigned __int16)(*((_WORD *)keymap + ((2 * (v0 - 128)) & 0x1FE)) - 151) > 1u )
return irq_ack(33LL);
shiftkey_0 = 0;
result = irq_ack(33LL);
}
else
{
v1 = *((_WORD *)keymap + 2 * (unsigned __int8)v0 - ((shiftkey_0 == 0) - 1));
if ( (unsigned __int16)(v1 - 151) <= 1u )
{
shiftkey_0 = 1;
result = irq_ack(33LL);
}
else
{
if ( !v1 )
return irq_ack(33LL);
printk(&unk_B24);
result = irq_ack(33LL);
}
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int16 v0; // ax
__int16 v1; // si
__int64 result; // rax

v0 = inportb(96LL);
if ( (v0 & 0x80u) != 0 )
{
if ( (unsigned __int16)(*((_WORD *)keymap + ((2 * (v0 - 128)) & 0x1FE)) - 151) > 1u )
return irq_ack(33LL);
shiftkey_0 = 0;
result = irq_ack(33LL);
}
else
{
v1 = *((_WORD *)keymap + 2 * (unsigned __int8)v0 - ((shiftkey_0 == 0) - 1));
if ( (unsigned __int16)(v1 - 151) <= 1u )
{
shiftkey_0 = 1;
result = irq_ack(33LL);
}
else
{
if ( !v1 )
return irq_ack(33LL);
printk(&unk_B24);
result = irq_ack(33LL);
}
}
return result;
}

original_code:
void kbd_handler() {
    unsigned char inbyte = inportb(0x60);
    int idx = 0;
    static int shiftkey = 0;
    unsigned short key = 0;
    if(inbyte < 0x80) {
        idx = 2 * inbyte;
        if (shiftkey)
            idx++;
        key = keymap[idx];
        if (key == 0x97 || key == 0x98) {
            shiftkey = 1;
        } else if (key != 0) {
            printk("%c", key);
        }
    } else {
        inbyte -= 0x80;
        key = keymap[2 * inbyte];
        if (key == 0x97 || key == 0x98)
            shiftkey = FALSE;
    }
    irq_ack(33);
}

variable_types:
inbyte
unsigned char

variable_types: idx int

variable_types: shiftkey int

variable_types:
key
unsigned short


decompiled_code:
__int64 init_kbd()
{
return setup_idt_gate(33LL, &isrkbd);
}

canonical_decompiled_code:
__int64 func1()
{
return setup_idt_gate(33LL, &isrkbd);
}

original_code:
void init_kbd(void) {
    setup_idt_gate(33, isrkbd);
}

canonical_original_code:
void init_kbd() {
    setup_idt_gate(33, isrkbd);
}


decompiled_code:
__int64 __fastcall show_memory_map(__int64 a1)
{
unsigned __int64 v1; // rdx
__int64 v2; // rbx
__int64 v3; // rcx
unsigned int v4; // er12
_DWORD *v5; // rax
__int64 v6; // r15
__int64 v7; // rbx
__int64 v8; // r15
__int64 v9; // rax
__int64 v10; // rbp
__int64 v11; // rcx
unsigned int v12; // er14
unsigned int v13; // er12
unsigned int v14; // eax
unsigned int v16; // [rsp+Ch] [rbp-3Ch]

printk("Memory map: \n");
v16 = (unsigned int)&kernel_end;
v1 = *(int *)(a1 + 88) / 0x30uLL;
if ( (int)v1 <= 0 )
{
pmm_initialize((unsigned int)&kernel_end, 0LL);
v13 = (unsigned int)&kernel_end;
}
else
{
v2 = (unsigned int)(v1 - 1);
v3 = *(_QWORD *)(a1 + 96);
v4 = 0;
v5 = (_DWORD *)(v3 + 24);
do
{
v4 += *v5;
v5 += 12;
}
while ( (_DWORD *)(v3 + 48 * v2 + 72) != v5 );
v6 = 3 * v2 + 3;
v7 = 0LL;
v8 = 16 * v6;
pmm_initialize(v16, v4);
do
{
v9 = v7 + *(_QWORD *)(a1 + 96);
v10 = *(_QWORD *)(v9 + 8);
v11 = *(_QWORD *)(v9 + 24);
v12 = v10 + v11;
printk(" 0x%x - 0x%x (%d bytes)", v10, v10 + v11, v11);
if ( *(_QWORD *)(*(_QWORD *)(a1 + 96) + v7 + 40) == 1LL )
{
printk(" [usable]");
pmm_add_memory_region((unsigned int)v10, v12);
}
else
{
printk(" [reserved]");
}
v7 += 48LL;
printk("\n");
}
while ( v8 != v7 );
v13 = v16 + (v4 >> 15);
}
pmm_add_memory_region((unsigned int)&kernel_begin, v16);
pmm_add_memory_region(v16, v13);
v14 = pmm_get_memory_size();
return printk("Total found usable memory: %d bytes\n", v14);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
unsigned __int64 v1; // rdx
__int64 v2; // rbx
__int64 v3; // rcx
unsigned int v4; // er12
_DWORD *v5; // rax
__int64 v6; // r15
__int64 v7; // rbx
__int64 v8; // r15
__int64 v9; // rax
__int64 v10; // rbp
__int64 v11; // rcx
unsigned int v12; // er14
unsigned int v13; // er12
unsigned int v14; // eax
unsigned int v16; // [rsp+Ch] [rbp-3Ch]

printk("Memory map: \n");
v16 = (unsigned int)&kernel_end;
v1 = *(int *)(a1 + 88) / 0x30uLL;
if ( (int)v1 <= 0 )
{
pmm_initialize((unsigned int)&kernel_end, 0LL);
v13 = (unsigned int)&kernel_end;
}
else
{
v2 = (unsigned int)(v1 - 1);
v3 = *(_QWORD *)(a1 + 96);
v4 = 0;
v5 = (_DWORD *)(v3 + 24);
do
{
v4 += *v5;
v5 += 12;
}
while ( (_DWORD *)(v3 + 48 * v2 + 72) != v5 );
v6 = 3 * v2 + 3;
v7 = 0LL;
v8 = 16 * v6;
pmm_initialize(v16, v4);
do
{
v9 = v7 + *(_QWORD *)(a1 + 96);
v10 = *(_QWORD *)(v9 + 8);
v11 = *(_QWORD *)(v9 + 24);
v12 = v10 + v11;
printk(" 0x%x - 0x%x (%d bytes)", v10, v10 + v11, v11);
if ( *(_QWORD *)(*(_QWORD *)(a1 + 96) + v7 + 40) == 1LL )
{
printk(" [usable]");
pmm_add_memory_region((unsigned int)v10, v12);
}
else
{
printk(" [reserved]");
}
v7 += 48LL;
printk("\n");
}
while ( v8 != v7 );
v13 = v16 + (v4 >> 15);
}
pmm_add_memory_region((unsigned int)&kernel_begin, v16);
pmm_add_memory_region(v16, v13);
v14 = pmm_get_memory_size();
return printk("Total found usable memory: %d bytes\n", v14);
}

original_code:
void show_memory_map(struct mbootinfo * bootInfo)
{
    int i,parts;
    printk("Memory map: \n");
    parts=((int)bootInfo->mmap_length)/sizeof(memoryMap);
    unsigned int total_size = 0;
    for (i=0;i<parts;i++) {
        unsigned long size = bootInfo->mmap_addr[i].LengthLow;
        total_size += size;
    }
    pmm_initialize((unsigned int) &kernel_end, total_size);
    for (i=0;i<parts;i++) {
        unsigned long start = bootInfo->mmap_addr[i].BaseAddrLow;
        unsigned long size = bootInfo->mmap_addr[i].LengthLow;
        unsigned long end = start + size;
        printk("  0x%x - 0x%x (%d bytes)", start, end, size);
        if(1 == bootInfo->mmap_addr[i].Type) {
            printk(" [usable]");
            pmm_add_memory_region(start, end);
        } else {
            printk(" [reserved]");
        }
        printk("\n");
    }
    pmm_add_memory_region((unsigned int) &kernel_begin, (unsigned int) &kernel_end);
    pmm_add_memory_region((unsigned int) &kernel_end, (unsigned int) &kernel_end + total_size / 4096 / 8);
    printk("Total found usable memory: %d bytes\n", pmm_get_memory_size());
}

canonical_original_code:
void show_memory_map(struct mbootinfo *bootInfo)
{
    int i; int parts;
    printk("Memory map: \n");
    parts=((int)bootInfo->mmap_length)/sizeof(memoryMap);
    unsigned int total_size = 0;
    for (i=0;i<parts;i++) {
        unsigned long size = bootInfo->mmap_addr[i].LengthLow;
        total_size += size;
    }
    pmm_initialize((unsigned int) &kernel_end, total_size);
    for (i=0;i<parts;i++) {
        unsigned long start = bootInfo->mmap_addr[i].BaseAddrLow;
        unsigned long size = bootInfo->mmap_addr[i].LengthLow;
        unsigned long end = start + size;
        printk("  0x%x - 0x%x (%d bytes)", start, end, size);
        if(1 == bootInfo->mmap_addr[i].Type) {
            printk(" [usable]");
            pmm_add_memory_region(start, end);
        } else {
            printk(" [reserved]");
        }
        printk("\n");
    }
    pmm_add_memory_region((unsigned int) &kernel_begin, (unsigned int) &kernel_end);
    pmm_add_memory_region((unsigned int) &kernel_end, (unsigned int) &kernel_end + total_size / 4096 / 8);
    printk("Total found usable memory: %d bytes\n", pmm_get_memory_size());
}

variable_types: struct mbootinfo { unsigned long flags; unsigned long memlow; unsigned long memhigh; unsigned char bootDevice[4]; char *commandLine; unsigned long modulesCount; unsigned int *modules; unsigned long num; unsigned long size; unsigned long addr; unsigned long reserved; unsigned long mmap_length; struct memoryMap { unsigned long Size; unsigned long BaseAddrLow; unsigned long BaseAddrHigh; unsigned long LengthLow; unsigned long LengthHigh; unsigned long Type; } *mmap_addr; unsigned long drives_length; unsigned long drives_addr; unsigned long config_table; unsigned long bootloader_name; } *

variable_types: i int

variable_types: parts int

variable_types:
size
unsigned long

variable_types:
start
unsigned long

variable_types:
end
unsigned long

user_defined_types: struct mbootinfo { unsigned long flags; unsigned long memlow; unsigned long memhigh; unsigned char bootDevice[4]; char *commandLine; unsigned long modulesCount; unsigned int *modules; unsigned long num; unsigned long size; unsigned long addr; unsigned long reserved; unsigned long mmap_length; struct memoryMap *mmap_addr; unsigned long drives_length; unsigned long drives_addr; unsigned long config_table; unsigned long bootloader_name; }


kmain

decompiled_code:
__int64 __fastcall kmain(__int64 a1)
{
console_cls();
cpuid();
printk(
"Dada v0.0.1 | CPU: %s | %u | 0x%x - 0x%x\n",
cpu_name,
(&kernel_end - &kernel_begin) >> 3,
&kernel_begin,
&kernel_end);
show_memory_map(a1);
printk("Stack: 0x%x - 0x%x (%d bytes)\n", &stack_bottom, &stack_top, (&stack_top - &stack_bottom) >> 3);
printk("BIOS reported lower memory: %u KB\n", *(_QWORD *)(a1 + 8));
printk("BIOS reported higher memory: %u KB\n", *(_QWORD *)(a1 + 16));
setup_gdt();
setup_idt();
remap_pic();
setup_irq_gates();
flash_idt();
pmm_print_summary();
unmaskIRQ(1LL);
_enable();
printk("===================================\n");
return printk("===================================\n");
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
console_cls();
cpuid();
printk(
"Dada v0.0.1 | CPU: %s | %u | 0x%x - 0x%x\n",
cpu_name,
(&kernel_end - &kernel_begin) >> 3,
&kernel_begin,
&kernel_end);
func0(a1);
printk("Stack: 0x%x - 0x%x (%d bytes)\n", &stack_bottom, &stack_top, (&stack_top - &stack_bottom) >> 3);
printk("BIOS reported lower memory: %u KB\n", *(_QWORD *)(a1 + 8));
printk("BIOS reported higher memory: %u KB\n", *(_QWORD *)(a1 + 16));
setup_gdt();
setup_idt();
remap_pic();
setup_irq_gates();
flash_idt();
pmm_print_summary();
unmaskIRQ(1LL);
_enable();
printk("===================================\n");
return printk("===================================\n");
}

original_code:
void kmain(int * s)
{
    unsigned int kernel_size = &kernel_end - &kernel_begin;
    struct mbootinfo * bi = (struct mbootinfo *) s;
    console_cls();
    cpuid();
    printk("Dada v0.0.1 | CPU:  %s | %u | 0x%x - 0x%x\n", cpu_name, kernel_size, &kernel_begin, &kernel_end);
    show_memory_map(bi);
    printk("Stack: 0x%x - 0x%x (%d bytes)\n", &_stack_bottom, &_stack_top, &_stack_top - &_stack_bottom);
    printk("BIOS reported lower memory: %u KB\n", bi->memlow );
    printk("BIOS reported higher memory: %u KB\n", bi->memhigh );
    setup_gdt();
    setup_idt();
    remap_pic();
    setup_irq_gates();
    flash_idt();
    pmm_print_summary();
    unmaskIRQ(1);
    asm volatile("sti");
    printk("===================================\n");
    printk("===================================\n");
}

canonical_original_code:
void kmain(int *s)
{
    unsigned int kernel_size = &kernel_end - &kernel_begin;
    struct mbootinfo *bi = (struct mbootinfo *) s;
    console_cls();
    cpuid();
    printk("Dada v0.0.1 | CPU:  %s | %u | 0x%x - 0x%x\n", cpu_name, kernel_size, &kernel_begin, &kernel_end);
    show_memory_map(bi);
    printk("Stack: 0x%x - 0x%x (%d bytes)\n", &_stack_bottom, &_stack_top, &_stack_top - &_stack_bottom);
    printk("BIOS reported lower memory: %u KB\n", bi->memlow );
    printk("BIOS reported higher memory: %u KB\n", bi->memhigh );
    setup_gdt();
    setup_idt();
    remap_pic();
    setup_irq_gates();
    flash_idt();
    pmm_print_summary();
    unmaskIRQ(1);
    asm volatile("sti");
    printk("===================================\n");
    printk("===================================\n");
}

variable_types:
s
int *

user_defined_types: struct memoryMap { unsigned long Size; unsigned long BaseAddrLow; unsigned long BaseAddrHigh; unsigned long LengthLow; unsigned long LengthHigh; unsigned long Type; }


outb

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

result = a2;
__outbyte(a1, a2);
return result;
}

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

result = a2;
__outbyte(a1, a2);
return result;
}

original_code:
static inline void outb(unsigned int port, unsigned char val)
{
    asm volatile("outb %0, %w1" : : "a" (val), "d" (port));
}

variable_types:
port
unsigned int

variable_types:
val
unsigned char


inb

decompiled_code:
unsigned __int8 __fastcall inb(unsigned __int16 a1)
{
return __inbyte(a1);
}

canonical_decompiled_code:
unsigned __int8 __fastcall func1(unsigned __int16 a1)
{
return __inbyte(a1);
}

original_code:
static inline unsigned char inb(int port)
{
    unsigned char ret;
    asm volatile ( "inb %w1, %0" : "=a"(ret) : "d"(port) );
    return ret;
}

variable_types: port int

variable_types:
ret
unsigned char


open

repo: chobits/tinyos

decompiled_code:
// attributes: thunk
__int64 open()
{
return usys_open();
}

canonical_decompiled_code:
__int64 func0()
{
return usys_open();
}

original_code:
int open(char *path, unsigned int mode)
{
 return usys_open(path, mode);
}

variable_types:
path
char *

variable_types:
mode
unsigned int

return_type: int


close

decompiled_code:
// attributes: thunk
__int64 close()
{
return usys_close();
}

canonical_decompiled_code:
__int64 func1()
{
return usys_close();
}

original_code:
int close(int fd)
{
 return usys_close(fd);
}

variable_types: fd int


read

decompiled_code:
// attributes: thunk
__int64 read()
{
return usys_read();
}

canonical_decompiled_code:
__int64 func2()
{
return usys_read();
}

original_code:
int read(int fd, char *buf, size_t size)
{
 return usys_read(fd, buf, size);
}

canonical_original_code:
int read(int fd, char *buf, unsigned int size)
{
 return usys_read(fd, buf, size);
}

variable_types:
buf
char *

variable_types:
size
unsigned int


write

decompiled_code:
// attributes: thunk
__int64 write()
{
return usys_write();
}

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

original_code:
int write(int fd, char *buf, size_t size)
{
 return usys_write(fd, buf, size);
}

canonical_original_code:
int write(int fd, char *buf, unsigned int size)
{
 return usys_write(fd, buf, size);
}


lseek

decompiled_code:
// attributes: thunk
__int64 lseek()
{
return usys_lseek();
}

canonical_decompiled_code:
__int64 func4()
{
return usys_lseek();
}

original_code:
off_t lseek(int fd, off_t offset, int whence)
{
 return usys_lseek(fd, offset, whence);
}

canonical_original_code:
int lseek(int fd, int offset, int whence)
{
 return usys_lseek(fd, offset, whence);
}

variable_types: offset int

variable_types: whence int


fsync

decompiled_code:
// attributes: thunk
__int64 fsync()
{
return usys_fsync();
}

canonical_decompiled_code:
__int64 func5()
{
return usys_fsync();
}

original_code:
int fsync(int fd)
{
 return usys_fsync(fd);
}


fstat

decompiled_code:
// attributes: thunk
__int64 fstat()
{
return usys_fstat();
}

canonical_decompiled_code:
__int64 func6()
{
return usys_fstat();
}

original_code:
int fstat(int fd, struct file_stat *stat)
{
 return usys_fstat(fd, stat);
}

variable_types: struct file_stat { unsigned int size; unsigned int inode; unsigned int mode; unsigned int iref; unsigned int link; } *


fchdir

decompiled_code:
// attributes: thunk
__int64 fchdir()
{
return usys_fchdir();
}

canonical_decompiled_code:
__int64 func7()
{
return usys_fchdir();
}

original_code:
int fchdir(int fd)
{
 return usys_fchdir(fd);
}


chdir

decompiled_code:
__int64 __fastcall chdir(const char *a1)
{
int v1; // eax
unsigned int v2; // ebp
int v3; // er12
_WORD v5[28]; // [rsp+0h] [rbp-38h] BYREF

v1 = open(a1, 0LL);
if ( v1 < 0 )
{
v3 = -1;
printf("Cannot open file: %s\n", a1);
}
else
{
v2 = v1;
v3 = fstat((unsigned int)v1, v5);
if ( v3 < 0 )
{
printf("Cannot get file stat: %s\n", a1);
}
else if ( (v5[4] & 0xF000) == 0x4000 )
{
v3 = fchdir(v2);
if ( v3 < 0 )
puts("error for fchdir");
}
else
{
printf("%s is not dir\n", a1);
}
close(v2);
}
return (unsigned int)v3;
}

canonical_decompiled_code:
__int64 __fastcall func8(const char *a1)
{
int v1; // eax
unsigned int v2; // ebp
int v3; // er12
_WORD v5[28]; // [rsp+0h] [rbp-38h] BYREF

v1 = func0(a1, 0LL);
if ( v1 < 0 )
{
v3 = -1;
printf("Cannot open file: %s\n", a1);
}
else
{
v2 = v1;
v3 = func6((unsigned int)v1, v5);
if ( v3 < 0 )
{
printf("Cannot get file stat: %s\n", a1);
}
else if ( (v5[4] & 0xF000) == 0x4000 )
{
v3 = func7(v2);
if ( v3 < 0 )
puts("error for fchdir");
}
else
{
printf("%s is not dir\n", a1);
}
func1(v2);
}
return (unsigned int)v3;
}

original_code:
int chdir(char *path)
{
 struct file_stat stat;
 int fd, r;
 fd = open(path, 0);
 if (fd < 0) {
  printf("Cannot open file: %s\n", path);
  return -1;
 }
 if ((r = fstat(fd, &stat)) < 0) {
  printf("Cannot get file stat: %s\n", path);
 } else if (!(((stat.mode) & 00170000) == 0040000)) {
  printf("%s is not dir\n", path);
 } else {
  r = fchdir(fd);
  if (r < 0)
   printf("error for fchdir\n");
 }
 close(fd);
 return r;
}

canonical_original_code:
int chdir(char *path)
{
 struct file_stat stat;
 int fd; int r;
 fd = open(path, 0);
 if (fd < 0) {
  printf("Cannot open file: %s\n", path);
  return -1;
 }
 if ((r = fstat(fd, &stat)) < 0) {
  printf("Cannot get file stat: %s\n", path);
 } else if (!(((stat.mode) & 00170000) == 0040000)) {
  printf("%s is not dir\n", path);
 } else {
  r = fchdir(fd);
  if (r < 0)
   printf("error for fchdir\n");
 }
 close(fd);
 return r;
}

variable_types: struct file_stat { unsigned int size; unsigned int inode; unsigned int mode; unsigned int iref; unsigned int link; }

variable_types: r int


fgetdir

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

if ( a2 < 0 || a3 <= 0 )
result = 0xFFFFFFFFLL;
else
result = usys_fgetdir();
return result;
}

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

if ( a2 < 0 || a3 <= 0 )
result = 0xFFFFFFFFLL;
else
result = usys_fgetdir();
return result;
}

original_code:
int fgetdir(int fd, int s, int n, struct dir_stat *ds)
{
 if (s < 0 || n <= 0)
  return -1;
 return usys_fgetdir(fd, s, n, ds);
}

variable_types: s int

variable_types: n int

variable_types: struct dir_stat { char name[32]; int len; unsigned int inode; } *


mkdir

decompiled_code:
// attributes: thunk
__int64 mkdir()
{
return usys_mkdir();
}

canonical_decompiled_code:
__int64 func10()
{
return usys_mkdir();
}

original_code:
int mkdir(char *path, unsigned int mode)
{
 return usys_mkdir(path, mode);
}


rmdir

decompiled_code:
// attributes: thunk
__int64 rmdir()
{
return usys_rmdir();
}

canonical_decompiled_code:
__int64 func11()
{
return usys_rmdir();
}

original_code:
int rmdir(char *path)
{
 return usys_rmdir(path);
}


rm

decompiled_code:
// attributes: thunk
__int64 rm()
{
return usys_rm();
}

canonical_decompiled_code:
__int64 func12()
{
return usys_rm();
}

original_code:
int rm(char *path)
{
 return usys_rm(path);
}


sync

decompiled_code:
// attributes: thunk
__int64 sync()
{
return usys_sync();
}

canonical_decompiled_code:
__int64 func13()
{
return usys_sync();
}

original_code:
void sync(void)
{
 usys_sync();
}

canonical_original_code:
void sync()
{
 usys_sync();
}


truncate

decompiled_code:
// attributes: thunk
__int64 truncate()
{
return usys_truncate();
}

canonical_decompiled_code:
__int64 func14()
{
return usys_truncate();
}

original_code:
int truncate(int fd)
{
 return usys_truncate(fd);
}


getcwd

decompiled_code:
__int64 __fastcall getcwd(__int64 a1)
{
if ( (unsigned int)usys_getcwd() )
a1 = 0LL;
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1)
{
if ( (unsigned int)usys_getcwd() )
a1 = 0LL;
return a1;
}

original_code:
char *getcwd(char *buf, size_t size)
{
 if (usys_getcwd(buf, size))
  return ((void *)0);
 return buf;
}

canonical_original_code:
char *getcwd(char *buf, unsigned int size)
{
 if (usys_getcwd(buf, size))
  return ((void *)0);
 return buf;
}

return_type: char *


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

result = run_builtin_command();
if ( (int)result < 0 )
result = run_process_command(a1, a2);
return result;
}

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

result = func7();
if ( (int)result < 0 )
result = func8(a1, a2);
return result;
}

original_code:
static void run_command(int argc, char **argv)
{
 if (run_builtin_command(argc, argv) < 0)
  run_process_command(argc, argv);
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
void __fastcall __noreturn shell_run(char *a1)
{
unsigned int v1; // eax
signed int v2; // eax
_BYTE v3[128]; // [rsp+0h] [rbp-108h] BYREF
char v4[136]; // [rsp+80h] [rbp-88h] BYREF

if ( a1 && *a1 )
prompt = a1;
while ( 1 )
{
printf("%s: ", prompt);
v1 = get_line(v3, 128LL);
v2 = parse_line(v3, v1, v4);
if ( v2 <= 0 )
{
if ( v2 )
puts("-shell: too many arguments");
}
else
{
run_command(v2, (__int64)v4);
}
}
}

canonical_decompiled_code:
void __fastcall __noreturn func1(char *a1)
{
unsigned int v1; // eax
signed int v2; // eax
_BYTE v3[128]; // [rsp+0h] [rbp-108h] BYREF
char v4[136]; // [rsp+80h] [rbp-88h] BYREF

if ( a1 && *a1 )
prompt = a1;
while ( 1 )
{
printf("%s: ", prompt);
v1 = func10(v3, 128LL);
v2 = func11(v3, v1, v4);
if ( v2 <= 0 )
{
if ( v2 )
puts("-shell: too many arguments");
}
else
{
func0(v2, (__int64)v4);
}
}
}

original_code:
void shell_run(char *prompt_str)
{
 char linebuf[128];
 int linelen;
 char *argv[16];
 int argc;
 if (prompt_str && *prompt_str)
  prompt = prompt_str;
 while (1) {
  print_prompt();
  linelen = get_line(linebuf, 128);
  argc = parse_line(linebuf, linelen, argv);
  if (argc > 0)
   run_command(argc, argv);
  else if (argc < 0)
   printf("-shell: too many arguments\n");
 }
}

variable_types:
linebuf
char [128]

variable_types: linelen int

variable_types:
argv
char *[16]


decompiled_code:
void __noreturn builtin_exit()
{
puts("exit");
exit(0);
}

canonical_decompiled_code:
void __noreturn func2()
{
puts("exit");
exit(0);
}

original_code:
static void builtin_exit(int argc, char **argv)
{
 printf("exit\n");
 exit(0);
}


decompiled_code:
int builtin_pid()
{
unsigned int v0; // eax

v0 = getpid();
return printf("%d\n", v0);
}

canonical_decompiled_code:
int func3()
{
unsigned int v0; // eax

v0 = getpid();
return printf("%d\n", v0);
}

original_code:
static void builtin_pid(int argc, char **argv)
{
 printf("%d\n", getpid());
}


decompiled_code:
int builtin_help()
{
const char *v0; // rsi
__int64 v1; // rbx
const char *v2; // rdx
int result; // eax

v0 = "help";
v1 = 0LL;
v2 = "display manual of this shell";
while ( 1 )
{
v1 += 3LL;
result = printf("%-8s - %s\n", v0, v2);
if ( v1 == 15 )
break;
v2 = (&off_408)[v1];
v0 = cmds[v1];
}
return result;
}

canonical_decompiled_code:
int func4()
{
const char *v0; // rsi
__int64 v1; // rbx
const char *v2; // rdx
int result; // eax

v0 = "help";
v1 = 0LL;
v2 = "display manual of this shell";
while ( 1 )
{
v1 += 3LL;
result = printf("%-8s - %s\n", v0, v2);
if ( v1 == 15 )
break;
v2 = (&off_408)[v1];
v0 = cmds[v1];
}
return result;
}

original_code:
static void builtin_help(int argc, char **argv)
{
 int i;
 for (i = 0; i < (sizeof(cmds) / sizeof(cmds[0])); i++)
  printf("%-8s - %s\n", cmds[i].cmd, cmds[i].info);
}

variable_types: i int


decompiled_code:
int __fastcall builtin_echo(int a1, __int64 a2)
{
__int64 v2; // rbp
const char **v3; // rbx
const char *v4; // rsi

if ( a1 > 1 )
{
v2 = a2 + 8LL * (unsigned int)(a1 - 2) + 16;
v3 = (const char **)(a2 + 8);
do
{
v4 = *v3++;
printf("%s ", v4);
}
while ( v3 != (const char **)v2 );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func5(int a1, __int64 a2)
{
__int64 v2; // rbp
const char **v3; // rbx
const char *v4; // rsi

if ( a1 > 1 )
{
v2 = a2 + 8LL * (unsigned int)(a1 - 2) + 16;
v3 = (const char **)(a2 + 8);
do
{
v4 = *v3++;
printf("%s ", v4);
}
while ( v3 != (const char **)v2 );
}
return putchar(10);
}

original_code:
static void builtin_echo(int argc, char **argv)
{
 int i;
 for (i = 1; i < argc; i++)
  printf("%s ", argv[i]);
 printf("\n");
}


decompiled_code:
int __fastcall builtin_cd(int a1, __int64 a2)
{
bool v2; // zf
const char *v3; // rdi

v2 = a1 == 2;
if ( a1 > 2 )
return puts("too many arguments for cd");
v3 = "/";
if ( v2 )
v3 = *(const char **)(a2 + 8);
return chdir(v3);
}

canonical_decompiled_code:
int __fastcall func6(int a1, __int64 a2)
{
bool v2; // zf
const char *v3; // rdi

v2 = a1 == 2;
if ( a1 > 2 )
return puts("too many arguments for cd");
v3 = "/";
if ( v2 )
v3 = *(const char **)(a2 + 8);
return chdir(v3);
}

original_code:
static void builtin_cd(int argc, char **argv)
{
 char *dir = "No dir";
 if (argc > 2) {
  printf("too many arguments for cd\n");
  return;
 }
 if (argc == 2) {
  dir = argv[1];
 } else {
  dir = "/";
 }
 chdir(dir);
}

variable_types:
dir
char *


decompiled_code:
__int64 __fastcall run_builtin_command(__int64 a1, const char **a2)
{
const char **v2; // r14
int v3; // ebx
const char *v4; // r13
int v5; // eax
__int64 result; // rax
unsigned int v7; // [rsp+Ch] [rbp-2Ch]

v2 = (const char **)cmds;
v3 = 0;
v4 = *a2;
v5 = strcmp(*a2, "help");
if ( v5 )
{
while ( 1 )
{
++v3;
v2 += 3;
if ( v3 == 5 )
break;
v5 = strcmp(v4, *v2);
if ( !v5 )
goto LABEL_4;
}
result = 0xFFFFFFFFLL;
}
else
{
LABEL_4:
v7 = v5;
(*(&funcs_22A + 3 * v3))();
result = v7;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, const char **a2)
{
const char **v2; // r14
int v3; // ebx
const char *v4; // r13
int v5; // eax
__int64 result; // rax
unsigned int v7; // [rsp+Ch] [rbp-2Ch]

v2 = (const char **)cmds;
v3 = 0;
v4 = *a2;
v5 = strcmp(*a2, "help");
if ( v5 )
{
while ( 1 )
{
++v3;
v2 += 3;
if ( v3 == 5 )
break;
v5 = strcmp(v4, *v2);
if ( !v5 )
goto LABEL_4;
}
result = 0xFFFFFFFFLL;
}
else
{
LABEL_4:
v7 = v5;
(*(&funcs_22A + 3 * v3))();
result = v7;
}
return result;
}

original_code:
int run_builtin_command(int argc, char **argv)
{
 int i;
 for (i = 0; i < (sizeof(cmds) / sizeof(cmds[0])); i++)
  if (strcmp(argv[0], cmds[i].cmd) == 0)
   goto found;
 return -1;
found:
 cmds[i].func(argc, argv);
 return 0;
}


decompiled_code:
unsigned int __fastcall run_process_command(unsigned int a1, const char **a2)
{
__pid_t v2; // eax
__pid_t v3; // ebx
unsigned int result; // eax
__WAIT_STATUS stat_loc; // [rsp+Ch] [rbp-1Ch] BYREF

LODWORD(stat_loc.__uptr) = 0;
v2 = fork();
if ( v2 < 0 )
return puts("fork error");
if ( !v2 )
{
execute(*a2, a1, a2);
printf("execute %s error\n", *a2);
exit(-1);
}
v3 = v2;
result = wait((__WAIT_STATUS)&stat_loc);
if ( v3 != result )
result = printf("wait error pid: %d return %d\n", result, LODWORD(stat_loc.__uptr));
return result;
}

canonical_decompiled_code:
unsigned int __fastcall func8(unsigned int a1, const char **a2)
{
__pid_t v2; // eax
__pid_t v3; // ebx
unsigned int result; // eax
__WAIT_STATUS stat_loc; // [rsp+Ch] [rbp-1Ch] BYREF

LODWORD(stat_loc.__uptr) = 0;
v2 = fork();
if ( v2 < 0 )
return puts("fork error");
if ( !v2 )
{
execute(*a2, a1, a2);
printf("execute %s error\n", *a2);
exit(-1);
}
v3 = v2;
result = wait((__WAIT_STATUS)&stat_loc);
if ( v3 != result )
result = printf("wait error pid: %d return %d\n", result, LODWORD(stat_loc.__uptr));
return result;
}

original_code:
void run_process_command(int argc, char **argv)
{
 int pid;
 int rpid, rv = 0;
 pid = fork();
 if (pid < 0) {
  printf("fork error\n");
  return;
 }
 if (pid == 0) {
  execute(argv[0], argc, argv);
  printf("execute %s error\n", argv[0]);
  exit(-1);
 }
 rpid = wait(&rv);
 if (rpid != pid)
  printf("wait error pid: %d return %d\n", rpid, rv);
}

canonical_original_code:
void run_process_command(int argc, char **argv)
{
 int pid;
 int rpid; int rv = 0;
 pid = fork();
 if (pid < 0) {
  printf("fork error\n");
  return;
 }
 if (pid == 0) {
  execute(argv[0], argc, argv);
  printf("execute %s error\n", argv[0]);
  exit(-1);
 }
 rpid = wait(&rv);
 if (rpid != pid)
  printf("wait error pid: %d return %d\n", rpid, rv);
}

variable_types: pid int

variable_types: rpid int

variable_types: rv int


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

result = gets(a1, (unsigned int)(a2 - 1));
*(_BYTE *)(a1 + (int)result) = 0;
if ( (int)result > 0 )
{
v3 = (_BYTE *)(a1 + (int)result - 1);
if ( *v3 == 10 )
{
*v3 = 0;
result = (unsigned int)(result - 1);
}
}
return result;
}

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

result = gets(a1, (unsigned int)(a2 - 1));
*(_BYTE *)(a1 + (int)result) = 0;
if ( (int)result > 0 )
{
v3 = (_BYTE *)(a1 + (int)result - 1);
if ( *v3 == 10 )
{
*v3 = 0;
result = (unsigned int)(result - 1);
}
}
return result;
}

original_code:
int get_line(char *buf, int bufsz)
{
 int len;
 len = gets(buf, bufsz - 1);
 buf[len] = '\0';
 if (len > 0 && buf[len - 1] == '\n') {
  buf[len - 1] = '\0';
  len--;
 }
 return len;
}

variable_types:
buf
char *

variable_types: bufsz int

variable_types: len int


decompiled_code:
__int64 __fastcall parse_line(char *a1, int a2)
{
char *v2; // rax
__int64 v3; // r8
__int64 v4; // r9
char *v6; // [rsp+0h] [rbp-8h] BYREF

if ( !a2 )
return 0LL;
v6 = a1;
while ( 1 )
{
v2 = get_arg(&v6);
if ( !v2 )
break;
*(_QWORD *)(v4 + 8 * v3) = v2;
}
return (unsigned int)v3;
}

canonical_decompiled_code:
__int64 __fastcall func11(char *a1, int a2)
{
char *v2; // rax
__int64 v3; // r8
__int64 v4; // r9
char *v6; // [rsp+0h] [rbp-8h] BYREF

if ( !a2 )
return 0LL;
v6 = a1;
while ( 1 )
{
v2 = func9(&v6);
if ( !v2 )
break;
*(_QWORD *)(v4 + 8 * v3) = v2;
}
return (unsigned int)v3;
}

original_code:
int parse_line(char *line, int len, char **argv)
{
 int argc;
 char *p, *pp;
 if (len == 0)
  return 0;
 p = pp = line;
 argc = 0;
 while ((p = get_arg(&pp)) != ((void *)0))
  argv[argc++] = p;
 return argc;
}

canonical_original_code:
int parse_line(char *line, int len, char **argv)
{
 int argc;
 char *p; char *pp;
 if (len == 0)
  return 0;
 p = pp = line;
 argc = 0;
 while ((p = get_arg(&pp)) != ((void *)0))
  argv[argc++] = p;
 return argc;
}

variable_types:
line
char *

variable_types:
p
char *

variable_types:
pp
char *


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
shell_run(0LL);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
func1(0LL);
}

original_code:
int main(int argc, char **argv)
{
 shell_run(((void *)0));
 return 0;
}


usyscall

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

result = a1;
__asm { int 30h; (NOT A VECTOR!) FAR JMP instruction for CP/M-style calls }
return result;
}

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

result = a1;
__asm { int 30h; (NOT A VECTOR!) FAR JMP instruction for CP/M-style calls }
return result;
}

canonical_original_code:
unsigned int usyscall(int num, unsigned int a1, unsigned int a2, unsigned int a3, unsigned int a4, unsigned int a5)
{
 unsigned int ret;
 asm volatile (
  "int %1;"
  : "=a" (ret)
  : "i" (48),
    "a" (num),
    "b"(a1),
    "c"(a2),
    "d"(a3),
    "D"(a4),
    "S"(a5)
  : "cc", "memory");
 return ret;
}

variable_types: num int

variable_types:
ret
unsigned int

return_type: unsigned int


decompiled_code:
__int64 usys_puts()
{
return usyscall(1u);
}

canonical_decompiled_code:
__int64 func1()
{
return func0(1u);
}

original_code:
int usys_puts(char *str)
{
 return usyscall(1, (u32)str, 0, 0, 0, 0);
}

canonical_original_code:
int usys_puts(char *str)
{
 return usyscall(1, (unsigned int)str, 0, 0, 0, 0);
}

variable_types:
str
char *


decompiled_code:
__int64 usys_gets()
{
return usyscall(2u);
}

canonical_decompiled_code:
__int64 func2()
{
return func0(2u);
}

original_code:
int usys_gets(char *str, int size)
{
 return usyscall(2, (u32)str, (u32)size, 0, 0, 0);
}

canonical_original_code:
int usys_gets(char *str, int size)
{
 return usyscall(2, (unsigned int)str, (unsigned int)size, 0, 0, 0);
}

variable_types: size int


decompiled_code:
__int64 usys_open()
{
return usyscall(3u);
}

canonical_decompiled_code:
__int64 func3()
{
return func0(3u);
}

original_code:
int usys_open(char *path, unsigned int flags)
{
 return usyscall(3, (u32)path, (u32)flags, 0, 0, 0);
}

canonical_original_code:
int usys_open(char *path, unsigned int flags)
{
 return usyscall(3, (unsigned int)path, (unsigned int)flags, 0, 0, 0);
}

variable_types:
path
char *

variable_types:
flags
unsigned int


decompiled_code:
__int64 usys_close()
{
return usyscall(8u);
}

canonical_decompiled_code:
__int64 func4()
{
return func0(8u);
}

original_code:
int usys_close(int fd)
{
 return usyscall(8, (u32)fd, 0, 0, 0, 0);
}

canonical_original_code:
int usys_close(int fd)
{
 return usyscall(8, (unsigned int)fd, 0, 0, 0, 0);
}

variable_types: fd int


decompiled_code:
__int64 usys_fsync()
{
return usyscall(6u);
}

canonical_decompiled_code:
__int64 func5()
{
return func0(6u);
}

original_code:
int usys_fsync(int fd)
{
 return usyscall(6, (u32)fd, 0, 0, 0, 0);
}

canonical_original_code:
int usys_fsync(int fd)
{
 return usyscall(6, (unsigned int)fd, 0, 0, 0, 0);
}


decompiled_code:
__int64 usys_read()
{
return usyscall(4u);
}

canonical_decompiled_code:
__int64 func7()
{
return func0(4u);
}

original_code:
int usys_read(int fd, char *buf, size_t size)
{
 return usyscall(4, (u32)fd, (u32)buf, (u32)size, 0, 0);
}

canonical_original_code:
int usys_read(int fd, char *buf, unsigned int size)
{
 return usyscall(4, (unsigned int)fd, (unsigned int)buf, (unsigned int)size, 0, 0);
}

variable_types:
size
unsigned int


decompiled_code:
__int64 usys_write()
{
return usyscall(5u);
}

canonical_decompiled_code:
__int64 func8()
{
return func0(5u);
}

original_code:
int usys_write(int fd, char *buf, size_t size)
{
 return usyscall(5, (u32)fd, (u32)buf, (u32)size, 0, 0);
}

canonical_original_code:
int usys_write(int fd, char *buf, unsigned int size)
{
 return usyscall(5, (unsigned int)fd, (unsigned int)buf, (unsigned int)size, 0, 0);
}


decompiled_code:
__int64 usys_lseek()
{
return usyscall(9u);
}

canonical_decompiled_code:
__int64 func9()
{
return func0(9u);
}

original_code:
off_t usys_lseek(int fd, off_t offset, unsigned int set)
{
 return usyscall(9, (u32)fd, (u32)offset, (u32)set, 0, 0);
}

canonical_original_code:
int usys_lseek(int fd, int offset, unsigned int set)
{
 return usyscall(9, (unsigned int)fd, (unsigned int)offset, (unsigned int)set, 0, 0);
}

variable_types: offset int

variable_types:
set
unsigned int


decompiled_code:
__int64 usys_fork()
{
return usyscall(0xAu);
}

canonical_decompiled_code:
__int64 func10()
{
return func0(0xAu);
}

original_code:
int usys_fork(void)
{
 return usyscall(10, 0, 0, 0, 0, 0);
}

canonical_original_code:
int usys_fork()
{
 return usyscall(10, 0, 0, 0, 0, 0);
}


decompiled_code:
__int64 usys_yield()
{
return usyscall(0xBu);
}

canonical_decompiled_code:
__int64 func11()
{
return func0(0xBu);
}

original_code:
void usys_yield(void)
{
 usyscall(11, 0, 0, 0, 0, 0);
}

canonical_original_code:
void usys_yield()
{
 usyscall(11, 0, 0, 0, 0, 0);
}


decompiled_code:
__int64 usys_getpid()
{
return usyscall(0xCu);
}

canonical_decompiled_code:
__int64 func12()
{
return func0(0xCu);
}

original_code:
int usys_getpid(void)
{
 return usyscall(12, 0, 0, 0, 0, 0);
}

canonical_original_code:
int usys_getpid()
{
 return usyscall(12, 0, 0, 0, 0, 0);
}


decompiled_code:
__int64 usys_exit()
{
return usyscall(0xDu);
}

canonical_decompiled_code:
__int64 func13()
{
return func0(0xDu);
}

original_code:
void usys_exit(int status)
{
 usyscall(13, (u32)status, 0, 0, 0, 0);
}

canonical_original_code:
void usys_exit(int status)
{
 usyscall(13, (unsigned int)status, 0, 0, 0, 0);
}

variable_types: status int


decompiled_code:
__int64 usys_wait()
{
return usyscall(0xEu);
}

canonical_decompiled_code:
__int64 func14()
{
return func0(0xEu);
}

original_code:
int usys_wait(int *status)
{
 return usyscall(14, (u32)status, 0, 0, 0, 0);
}

canonical_original_code:
int usys_wait(int *status)
{
 return usyscall(14, (unsigned int)status, 0, 0, 0, 0);
}

variable_types:
status
int *


decompiled_code:
__int64 usys_execute()
{
return usyscall(0xFu);
}

canonical_decompiled_code:
__int64 func15()
{
return func0(0xFu);
}

original_code:
int usys_execute(char *path, int argc, char **argv)
{
 return usyscall(15, (u32)path, (u32)argc, (u32)argv, 0, 0);
}

canonical_original_code:
int usys_execute(char *path, int argc, char **argv)
{
 return usyscall(15, (unsigned int)path, (unsigned int)argc, (unsigned int)argv, 0, 0);
}

variable_types: argc int

variable_types:
argv
char **


decompiled_code:
__int64 usys_fchdir()
{
return usyscall(0x10u);
}

canonical_decompiled_code:
__int64 func16()
{
return func0(0x10u);
}

original_code:
int usys_fchdir(int fd)
{
 return usyscall(16, (u32)fd, 0, 0, 0, 0);
}

canonical_original_code:
int usys_fchdir(int fd)
{
 return usyscall(16, (unsigned int)fd, 0, 0, 0, 0);
}


decompiled_code:
__int64 usys_mkdir()
{
return usyscall(0x12u);
}

canonical_decompiled_code:
__int64 func18()
{
return func0(0x12u);
}

original_code:
int usys_mkdir(char *path, unsigned int mode)
{
 return usyscall(18, (u32)path, (u32)mode, 0, 0, 0);
}

canonical_original_code:
int usys_mkdir(char *path, unsigned int mode)
{
 return usyscall(18, (unsigned int)path, (unsigned int)mode, 0, 0, 0);
}

variable_types:
mode
unsigned int


decompiled_code:
__int64 usys_sync()
{
return usyscall(0x13u);
}

canonical_decompiled_code:
__int64 func19()
{
return func0(0x13u);
}

original_code:
void usys_sync(void)
{
 usyscall(19, 0, 0, 0, 0, 0);
}

canonical_original_code:
void usys_sync()
{
 usyscall(19, 0, 0, 0, 0, 0);
}


decompiled_code:
__int64 usys_rmdir()
{
return usyscall(0x14u);
}

canonical_decompiled_code:
__int64 func20()
{
return func0(0x14u);
}

original_code:
int usys_rmdir(char *path)
{
 return usyscall(20, (u32)path, 0, 0, 0, 0);
}

canonical_original_code:
int usys_rmdir(char *path)
{
 return usyscall(20, (unsigned int)path, 0, 0, 0, 0);
}


decompiled_code:
__int64 usys_rm()
{
return usyscall(0x15u);
}

canonical_decompiled_code:
__int64 func21()
{
return func0(0x15u);
}

original_code:
int usys_rm(char *path)
{
 return usyscall(21, (u32)path, 0, 0, 0, 0);
}

canonical_original_code:
int usys_rm(char *path)
{
 return usyscall(21, (unsigned int)path, 0, 0, 0, 0);
}


decompiled_code:
__int64 usys_truncate()
{
return usyscall(0x16u);
}

canonical_decompiled_code:
__int64 func22()
{
return func0(0x16u);
}

original_code:
int usys_truncate(int fd)
{
 return usyscall(22, (u32)fd, 0, 0, 0, 0);
}

canonical_original_code:
int usys_truncate(int fd)
{
 return usyscall(22, (unsigned int)fd, 0, 0, 0, 0);
}


decompiled_code:
__int64 usys_getcwd()
{
return usyscall(0x17u);
}

canonical_decompiled_code:
__int64 func23()
{
return func0(0x17u);
}

original_code:
int usys_getcwd(char *buf, size_t size)
{
 return usyscall(23, (u32)buf, (u32)size, 0, 0, 0);
}

canonical_original_code:
int usys_getcwd(char *buf, unsigned int size)
{
 return usyscall(23, (unsigned int)buf, (unsigned int)size, 0, 0, 0);
}

variable_types:
buf
char *


searchzerobit

decompiled_code:
__int64 __fastcall searchzerobit(unsigned int *a1, int a2, int a3)
{
int v3; // eax
int v6; // edi
int v7; // ecx
unsigned int v8; // edx
int v9; // eax
unsigned int v11; // edx
int v12; // esi

v3 = a2 + 31;
if ( a2 >= 0 )
v3 = a2;
v6 = 0;
v7 = v3 >> 5;
if ( a2 > 31 )
{
LABEL_4:
v8 = *a1;
v9 = 0;
while ( (v8 & 1) != 0 )
{
++v9;
v8 >>= 1;
if ( v9 == 32 )
{
++v6;
++a1;
if ( v7 > v6 )
goto LABEL_4;
goto LABEL_11;
}
}
goto LABEL_7;
}
LABEL_11:
v11 = *a1;
v12 = a2 & 0x1F;
if ( !v12 )
return 0xFFFFFFFFLL;
v9 = 0;
while ( (v11 & 1) != 0 )
{
++v9;
v11 >>= 1;
if ( v12 == v9 )
return 0xFFFFFFFFLL;
}
LABEL_7:
if ( a3 )
*((_BYTE *)a1 + (v9 >> 3)) |= 1 << (v9 & 7);
return (unsigned int)(32 * v6 + v9);
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int *a1, int a2, int a3)
{
int v3; // eax
int v6; // edi
int v7; // ecx
unsigned int v8; // edx
int v9; // eax
unsigned int v11; // edx
int v12; // esi

v3 = a2 + 31;
if ( a2 >= 0 )
v3 = a2;
v6 = 0;
v7 = v3 >> 5;
if ( a2 > 31 )
{
LABEL_4:
v8 = *a1;
v9 = 0;
while ( (v8 & 1) != 0 )
{
++v9;
v8 >>= 1;
if ( v9 == 32 )
{
++v6;
++a1;
if ( v7 > v6 )
goto LABEL_4;
goto LABEL_11;
}
}
goto LABEL_7;
}
LABEL_11:
v11 = *a1;
v12 = a2 & 0x1F;
if ( !v12 )
return 0xFFFFFFFFLL;
v9 = 0;
while ( (v11 & 1) != 0 )
{
++v9;
v11 >>= 1;
if ( v12 == v9 )
return 0xFFFFFFFFLL;
}
LABEL_7:
if ( a3 )
*((_BYTE *)a1 + (v9 >> 3)) |= 1 << (v9 & 7);
return (unsigned int)(32 * v6 + v9);
}

original_code:
int searchzerobit(void *src, int bits, int set)
{
 unsigned int bit32;
 int i, k, off;
 off = bits & 31;
 bits /= 32;
 for (i = 0; i < bits; i++) {
  bit32 = *((unsigned int *)src);
  for (k = 0; k < 32; k++) {
   if (!(bit32 & 1))
    goto found;
   bit32 >>= 1;
  }
  src += 4;
 }
 bit32 = *((unsigned int *)src);
 for (k = 0; k < off; k++) {
  if (!(bit32 & 1))
   goto found;
  bit32 >>= 1;
 }
 return -1;
found:
 if (set)
  ((unsigned char *)src)[k >> 3] |= (1 << (k & 7));
 return i * 32 + k;
}

canonical_original_code:
int searchzerobit(void *src, int bits, int set)
{
 unsigned int bit32;
 int i; int k; int off;
 off = bits & 31;
 bits /= 32;
 for (i = 0; i < bits; i++) {
  bit32 = *((unsigned int *)src);
  for (k = 0; k < 32; k++) {
   if (!(bit32 & 1))
    goto found;
   bit32 >>= 1;
  }
  src += 4;
 }
 bit32 = *((unsigned int *)src);
 for (k = 0; k < off; k++) {
  if (!(bit32 & 1))
   goto found;
  bit32 >>= 1;
 }
 return -1;
found:
 if (set)
  ((unsigned char *)src)[k >> 3] |= (1 << (k & 7));
 return i * 32 + k;
}

variable_types:
src
void *

variable_types: bits int

variable_types: set int

variable_types: i int

variable_types: k int

variable_types: off int


testbit

decompiled_code:
__int64 __fastcall testbit(__int64 a1, int a2)
{
return *(_DWORD *)(a1 + 4LL * (a2 / 32)) & (unsigned int)(1 << a2);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2)
{
return *(_DWORD *)(a1 + 4LL * (a2 / 32)) & (unsigned int)(1 << a2);
}

original_code:
int testbit(void *src, int bits)
{
 u32 bit32;
 bit32 = ((u32 *)src)[bits / 32];
 return bit32 & (1 << (bits & 31));
}

canonical_original_code:
int testbit(void *src, int bits)
{
 unsigned int bit32;
 bit32 = ((unsigned int *)src)[bits / 32];
 return bit32 & (1 << (bits & 31));
}


setbit

decompiled_code:
_BOOL8 __fastcall setbit(__int64 a1, int a2)
{
int v2; // edx
unsigned __int8 *v3; // rax
unsigned __int8 v4; // si

v2 = 1 << (a2 & 7);
v3 = (unsigned __int8 *)(a1 + a2 / 8);
v4 = *v3;
*v3 |= v2;
return ((unsigned __int8)v2 & v4) != 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(__int64 a1, int a2)
{
int v2; // edx
unsigned __int8 *v3; // rax
unsigned __int8 v4; // si

v2 = 1 << (a2 & 7);
v3 = (unsigned __int8 *)(a1 + a2 / 8);
v4 = *v3;
*v3 |= v2;
return ((unsigned __int8)v2 & v4) != 0;
}

original_code:
int setbit(void *src, int bits)
{
 u8 *bit8, r;
 bit8 = (u8 *)src + (bits / 8);
 r = *bit8 & 1 << (bits & 7);
 *bit8 = (*bit8) | (1 << (bits & 7));
 return !!r;
}

canonical_original_code:
int setbit(void *src, int bits)
{
 unsigned char *bit8; unsigned char r;
 bit8 = (unsigned char *)src + (bits / 8);
 r = *bit8 & 1 << (bits & 7);
 *bit8 = (*bit8) | (1 << (bits & 7));
 return !!r;
}

variable_types:
r
unsigned char


memmove

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

result = a2 + a3;
if ( a1 < result && a2 <= a1 )
{
v4 = a3 - 1;
if ( v4 >= 0 )
{
result = v4;
do
{
*(_BYTE *)(a1 + result) = *(_BYTE *)(a2 + result);
--result;
}
while ( (result & 0x80000000) == 0LL );
}
}
else
{
result = 0LL;
if ( a3 > 0 )
{
do
{
*(_BYTE *)(a1 + result) = *(_BYTE *)(a2 + result);
++result;
}
while ( result != a3 );
}
}
return result;
}

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

result = a2 + a3;
if ( a1 < result && a2 <= a1 )
{
v4 = a3 - 1;
if ( v4 >= 0 )
{
result = v4;
do
{
*(_BYTE *)(a1 + result) = *(_BYTE *)(a2 + result);
--result;
}
while ( (result & 0x80000000) == 0LL );
}
}
else
{
result = 0LL;
if ( a3 > 0 )
{
do
{
*(_BYTE *)(a1 + result) = *(_BYTE *)(a2 + result);
++result;
}
while ( result != a3 );
}
}
return result;
}

original_code:
void memmove(void *dst, void *src, int n)
{
 if (src + n > dst && src <= dst) {
  while (--n >= 0)
   *((u8 *)dst + n) = *((u8 *)src + n);
 } else {
  int i;
  for (i = 0; i < n; i++)
   *((u8 *)dst + i) = *((u8 *)src + i);
 }
}

canonical_original_code:
void memmove(void *dst, void *src, int n)
{
 if (src + n > dst && src <= dst) {
  while (--n >= 0)
   *((unsigned char *)dst + n) = *((unsigned char *)src + n);
 } else {
  int i;
  for (i = 0; i < n; i++)
   *((unsigned char *)dst + i) = *((unsigned char *)src + i);
 }
}

variable_types:
dst
void *

variable_types: n int


memcpy

original_code:
void memcpy(void *dst, void *src, int n)
{
 int i;
 for (i = 0; i < n; i++)
  *((u8 *)dst + i) = *((u8 *)src + i);
}

canonical_original_code:
void memcpy(void *dst, void *src, int n)
{
 int i;
 for (i = 0; i < n; i++)
  *((unsigned char *)dst + i) = *((unsigned char *)src + i);
}


memset

decompiled_code:
void __fastcall memset(__int64 a1, __int64 a2, int a3)
{
while ( a3 > 0 )
;
}

canonical_decompiled_code:
void __fastcall func6(__int64 a1, __int64 a2, int a3)
{
while ( a3 > 0 )
;
}

original_code:
void memset(void *dst, int c, int n)
{
 int i;
 for (i = 0; i < n; i++)
  *((u8 *)dst + i) = c;
}

canonical_original_code:
void memset(void *dst, int c, int n)
{
 int i;
 for (i = 0; i < n; i++)
  *((unsigned char *)dst + i) = c;
}

variable_types: c int


wmemset

original_code:
void wmemset(void *dst, int c, int n)
{
 int i;
 for (i = 0; i < n; i++)
  *((u16 *)dst + i) = c;
}

canonical_original_code:
void wmemset(void *dst, int c, int n)
{
 int i;
 for (i = 0; i < n; i++)
  *((unsigned short *)dst + i) = c;
}


strncpy

decompiled_code:
__int64 __fastcall strncpy(_BYTE *a1, char *a2, int a3)
{
char v3; // cl
__int64 i; // rax
unsigned int v5; // er12

v3 = *a2;
if ( *a2 )
{
if ( a3 <= 0 )
return 0;
for ( i = 1LL; ; ++i )
{
*a1 = v3;
v3 = a2[i];
++a1;
--a3;
v5 = i;
if ( !v3 )
break;
if ( !a3 )
return v5;
}
}
else
{
v5 = 0;
}
if ( a3 > 0 )
memset((__int64)a1, 0LL, a3);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func9(_BYTE *a1, char *a2, int a3)
{
char v3; // cl
__int64 i; // rax
unsigned int v5; // er12

v3 = *a2;
if ( *a2 )
{
if ( a3 <= 0 )
return 0;
for ( i = 1LL; ; ++i )
{
*a1 = v3;
v3 = a2[i];
++a1;
--a3;
v5 = i;
if ( !v3 )
break;
if ( !a3 )
return v5;
}
}
else
{
v5 = 0;
}
if ( a3 > 0 )
func6((__int64)a1, 0LL, a3);
return v5;
}

original_code:
int strncpy(char *dst, char *src, int n)
{
 int i = 0;
 while (*src && n > 0) {
  *dst++ = *src++;
  n--;
  i++;
 }
 while (n > 0) {
  *dst++ = '\0';
  n--;
 }
 return i;
}

variable_types:
dst
char *

variable_types:
src
char *


strnncpy

decompiled_code:
__int64 __fastcall strnncpy(__int64 a1, char *a2, int a3)
{
char v4; // dl
__int64 v5; // rax
int v6; // er9
unsigned int v7; // er8
int v8; // ecx

v4 = *a2;
if ( a3 <= 0 || !v4 )
return 0LL;
v5 = 0LL;
v6 = a3 - 1;
do
{
*(_BYTE *)(a1 + v5) = v4;
v7 = v5 + 1;
v4 = a2[v5 + 1];
v8 = v6 - v5++;
}
while ( v8 > 0 && v4 );
return v7;
}

original_code:
int strnncpy(char *dst, char *src, int n)
{
 int i = 0;
 while (*src && n > 0) {
  *dst++ = *src++;
  n--;
  i++;
 }
 return i;
}


strcmp

decompiled_code:
__int64 __fastcall strcmp(_BYTE *a1, char *a2)
{
int v2; // eax
int v3; // edx

v2 = (char)*a1;
v3 = *a2;
if ( *a1 )
{
while ( 1 )
{
if ( *a2 != (_BYTE)v2 )
return (unsigned int)(v2 - *a2);
v2 = (char)*++a1;
if ( !(_BYTE)v2 )
break;
++a2;
}
v3 = a2[1];
}
return (unsigned int)-v3;
}

canonical_decompiled_code:
__int64 __fastcall func11(_BYTE *a1, char *a2)
{
int v2; // eax
int v3; // edx

v2 = (char)*a1;
v3 = *a2;
if ( *a1 )
{
while ( 1 )
{
if ( *a2 != (_BYTE)v2 )
return (unsigned int)(v2 - *a2);
v2 = (char)*++a1;
if ( !(_BYTE)v2 )
break;
++a2;
}
v3 = a2[1];
}
return (unsigned int)-v3;
}

original_code:
int strcmp(char *s1, char *s2)
{
 while (*s1 && *s1 == *s2) {
  s1++;
  s2++;
 }
 return *s1 - *s2;
}


strncmp

decompiled_code:
__int64 __fastcall strncmp(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 v4; // rsi
__int64 v5; // rax
char v6; // dl
int v7; // ecx

if ( !a3 )
return 0LL;
v4 = a3;
v5 = 0LL;
while ( 1 )
{
v6 = *(_BYTE *)(a1 + v5);
v7 = *(char *)(a2 + v5);
if ( !v6 || v6 != (_BYTE)v7 )
break;
if ( v4 == ++v5 )
return 0LL;
}
return (unsigned int)(v6 - v7);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 v4; // rsi
__int64 v5; // rax
char v6; // dl
int v7; // ecx

if ( !a3 )
return 0LL;
v4 = a3;
v5 = 0LL;
while ( 1 )
{
v6 = *(_BYTE *)(a1 + v5);
v7 = *(char *)(a2 + v5);
if ( !v6 || v6 != (_BYTE)v7 )
break;
if ( v4 == ++v5 )
return 0LL;
}
return (unsigned int)(v6 - v7);
}

variable_types:
n
unsigned int


utostr

decompiled_code:
__int64 __fastcall utostr(unsigned int a1, char *a2, unsigned int a3)
{
unsigned int v5; // er8
char *v6; // r8
unsigned int v7; // er9
char *v8; // rcx
int v9; // et2
char v10; // dl
unsigned int v11; // edi
char v12; // al

v5 = 0;
if ( a3 - 2 > 0x22 )
return v5;
v6 = a2;
if ( a1 )
{
do
{
v7 = a1;
v8 = v6;
v9 = a1 % a3;
a1 /= a3;
++v6;
v10 = byte_570[v9];
*(v6 - 1) = v10;
}
while ( a3 <= v7 );
*v6 = 0;
v11 = (_DWORD)v6 - (_DWORD)a2;
if ( a2 < v8 )
{
while ( 1 )
{
v12 = *a2;
--v8;
++a2;
v8[1] = v12;
*(a2 - 1) = v10;
if ( a2 >= v8 )
break;
v10 = *v8;
}
}
return v11;
}
*(_WORD *)a2 = 48;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func13(unsigned int a1, char *a2, unsigned int a3)
{
unsigned int v5; // er8
char *v6; // r8
unsigned int v7; // er9
char *v8; // rcx
int v9; // et2
char v10; // dl
unsigned int v11; // edi
char v12; // al

v5 = 0;
if ( a3 - 2 > 0x22 )
return v5;
v6 = a2;
if ( a1 )
{
do
{
v7 = a1;
v8 = v6;
v9 = a1 % a3;
a1 /= a3;
++v6;
v10 = byte_570[v9];
*(v6 - 1) = v10;
}
while ( a3 <= v7 );
*v6 = 0;
v11 = (_DWORD)v6 - (_DWORD)a2;
if ( a2 < v8 )
{
while ( 1 )
{
v12 = *a2;
--v8;
++a2;
v8[1] = v12;
*(a2 - 1) = v10;
if ( a2 >= v8 )
break;
v10 = *v8;
}
}
return v11;
}
*(_WORD *)a2 = 48;
return 1LL;
}

original_code:
int utostr(unsigned int n, char *nptr, int base)
{
 int i;
 char *p = nptr;
 if (base < 2 || base > ('z' - 'a' + 11))
  return 0;
 if (n == 0) {
  nptr[0] = '0';
  nptr[1] = '\0';
  return 1;
 }
 while (n) {
  i = n % base;
  n /= base;
  *p++ = "0123456789abcdefghijklmnopqrstuvwxyz"[i];
 }
 n = p - nptr;
 *p-- = '\0';
 while (nptr < p) {
  i = *p;
  *p = *nptr;
  *nptr = i;
  nptr++;
  p--;
 }
 return n;
}

variable_types:
nptr
char *

variable_types: base int

variable_types:
p
char *


itostr

original_code:
int itostr(int n, char *nptr, int base)
{
 int i;
 char *p = nptr;
 if (base < 2 || base > ('z' - 'a' + 11))
  return 0;
 if (n == 0) {
  nptr[0] = '0';
  nptr[1] = '\0';
  return 1;
 }
 if (n < 0) {
  if (n == ((~0U >> 1) + 1))
   return 0;
  n = -n;
  *p++ = '-';
 }
 while (n) {
  i = n % base;
  n /= base;
  *p++ = "0123456789abcdefghijklmnopqrstuvwxyz"[i];
 }
 n = p - nptr;
 *p-- = '\0';
 if (*nptr == '-')
  nptr++;
 while (nptr < p) {
  i = *p;
  *p = *nptr;
  *nptr = i;
  nptr++;
  p--;
 }
 return n;
}


strtoi

decompiled_code:
__int64 __fastcall strtoi(_BYTE *a1, _QWORD *a2, int a3)
{
unsigned int v3; // er8
int v4; // eax
int v5; // eax

v3 = 0;
if ( (unsigned int)(a3 - 2) <= 0x22 )
{
while ( 1 )
{
v5 = (char)*a1;
if ( !*a1 )
break;
if ( (unsigned int)(v5 - 48) <= 9 )
{
v4 = v5 - 48;
}
else if ( (unsigned int)(v5 - 65) > 0x19 )
{
if ( (unsigned int)(v5 - 97) > 0x19 )
break;
v4 = v5 - 87;
}
else
{
v4 = v5 - 55;
}
if ( v4 >= a3 )
break;
++a1;
v3 = v4 + a3 * v3;
}
if ( a2 )
*a2 = a1;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func15(_BYTE *a1, _QWORD *a2, int a3)
{
unsigned int v3; // er8
int v4; // eax
int v5; // eax

v3 = 0;
if ( (unsigned int)(a3 - 2) <= 0x22 )
{
while ( 1 )
{
v5 = (char)*a1;
if ( !*a1 )
break;
if ( (unsigned int)(v5 - 48) <= 9 )
{
v4 = v5 - 48;
}
else if ( (unsigned int)(v5 - 65) > 0x19 )
{
if ( (unsigned int)(v5 - 97) > 0x19 )
break;
v4 = v5 - 87;
}
else
{
v4 = v5 - 55;
}
if ( v4 >= a3 )
break;
++a1;
v3 = v4 + a3 * v3;
}
if ( a2 )
*a2 = a1;
}
return v3;
}

original_code:
int strtoi(char *nptr, char **endptr, int base)
{
 int num = 0;
 int n;
 if (base < 2 || base > ('z' - 'a' + 11))
  return 0;
 while (*nptr) {
  n = *nptr;
  if (n >= '0' && n <= '9')
   n -= '0';
  else if (n >= 'A' && n <= 'Z')
   n -= ('A' - 10);
  else if (n >= 'a' && n <= 'z')
   n -= ('a' - 10);
  else
   n = base;
  if (n >= base)
   break;
  num = num * base + n;
  nptr++;
 }
 if (endptr)
  *endptr = nptr;
 return num;
}

variable_types:
endptr
char **

variable_types: num int


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

result = 0xFFFFFFFFLL;
if ( a1 > 0 )
{
do
{
result = (unsigned int)(result + 1);
a1 >>= 1;
}
while ( a1 );
}
return result;
}

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

result = 0xFFFFFFFFLL;
if ( a1 > 0 )
{
do
{
result = (unsigned int)(result + 1);
a1 >>= 1;
}
while ( a1 );
}
return result;
}

original_code:
int log2(int n)
{
 int i = -1;
 while (n > 0) {
  n >>= 1;
  i++;
 }
 return i;
}


decompiled_code:
__int64 __fastcall log2up(int a1)
{
__int64 result; // rax
int i; // edi

if ( a1 <= 0 )
return 0xFFFFFFFFLL;
result = 0LL;
for ( i = a1 - 1; i; i >>= 1 )
result = (unsigned int)(result + 1);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func17(int a1)
{
__int64 result; // rax
int i; // edi

if ( a1 <= 0 )
return 0xFFFFFFFFLL;
result = 0LL;
for ( i = a1 - 1; i; i >>= 1 )
result = (unsigned int)(result + 1);
return result;
}

original_code:
int log2up(int n)
{
 int i = 0;
 if (n <= 0)
  return -1;
 n--;
 while (n > 0) {
  n >>= 1;
  i++;
 }
 return i;
}


decompiled_code:
__int64 __fastcall minix_check_super(unsigned __int16 *a1)
{
__int64 v1; // rsi
__int64 v2; // rdx
__int64 v3; // rsi
__int64 v4; // rsi
int v5; // edx
int v6; // esi

if ( a1[10] != 4991 )
panic("Unknown fs type, not minix");
v1 = a1[1];
v2 = (unsigned int)(*(_DWORD *)(minix_sb + 20) - dword_248);
if ( (int)v1 > (int)v2 )
panic("zones %d is larger then %d usable blocks", v1, v2);
v3 = *(_QWORD *)(a1 + 6);
if ( v3 != 268966912 )
panic("Invalid max size %d", v3);
v4 = a1[5];
if ( (_WORD)v4 )
panic("Invalid log zone size %d", v4);
v5 = *a1;
v6 = a1[2];
if ( a1[4] != v6 + a1[3] + 2 + (v5 + 24) / 0x19u )
{
panic("Invalid first data zone %d", a1[4]);
v6 = a1[2];
v5 = *a1;
}
if ( (unsigned int)(v5 + 0x1FFF) >> 13 != v6 )
panic("Invalid imap blocks");
if ( a1[3] != (a1[1] - (unsigned int)a1[4] + 0x1FFF) >> 13 )
panic("Invalid zmap blocks");
return printk("The super block of minix filesystem is ok!\n");
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int16 *a1)
{
__int64 v1; // rsi
__int64 v2; // rdx
__int64 v3; // rsi
__int64 v4; // rsi
int v5; // edx
int v6; // esi

if ( a1[10] != 4991 )
panic("Unknown fs type, not minix");
v1 = a1[1];
v2 = (unsigned int)(*(_DWORD *)(minix_sb + 20) - dword_248);
if ( (int)v1 > (int)v2 )
panic("zones %d is larger then %d usable blocks", v1, v2);
v3 = *(_QWORD *)(a1 + 6);
if ( v3 != 268966912 )
panic("Invalid max size %d", v3);
v4 = a1[5];
if ( (_WORD)v4 )
panic("Invalid log zone size %d", v4);
v5 = *a1;
v6 = a1[2];
if ( a1[4] != v6 + a1[3] + 2 + (v5 + 24) / 0x19u )
{
panic("Invalid first data zone %d", a1[4]);
v6 = a1[2];
v5 = *a1;
}
if ( (unsigned int)(v5 + 0x1FFF) >> 13 != v6 )
panic("Invalid imap blocks");
if ( a1[3] != (a1[1] - (unsigned int)a1[4] + 0x1FFF) >> 13 )
panic("Invalid zmap blocks");
return printk("The super block of minix filesystem is ok!\n");
}

original_code:
static void minix_check_super(struct minix_d_super_block *msb)
{
 if (msb->s_magic != 0x137F)
  panic("Unknown fs type, not minix");
 if (msb->s_nzones > minix_sb.s_bdev->blocks - minix_sb.s_start)
  panic("zones %d is larger then %d usable blocks",
   msb->s_nzones, minix_sb.s_bdev->blocks - minix_sb.s_start);
 if (msb->s_max_size != ((7 + 512 + 512 * 512) * (1 << 10)))
  panic("Invalid max size %d", msb->s_max_size);
 if (msb->s_log_zone_size != 0)
  panic("Invalid log zone size %d", msb->s_log_zone_size);
 if (msb->s_firstdatazone != (2 + (((u32)((msb)->s_ninodes) + (u32)((((1 << 10)) / (sizeof(struct minix_d_inode)))) - 1) / (u32)(((1 << 10)) / (sizeof(struct minix_d_inode)))) + (msb)->s_imap_blocks + (msb)->s_zmap_blocks))
  panic("Invalid first data zone %d", msb->s_firstdatazone);
 if (msb->s_imap_blocks != (((u32)((msb)->s_ninodes) + (u32)((8 * (1 << 10))) - 1) / (u32)(8 * (1 << 10))))
  panic("Invalid imap blocks");
 if (msb->s_zmap_blocks != (((u32)((msb)->s_nzones - (msb)->s_firstdatazone) + (u32)((8 * (1 << 10))) - 1) / (u32)(8 * (1 << 10))))
  panic("Invalid zmap blocks");
 printk("The super block of minix filesystem is ok!\n");
}

variable_types: struct minix_d_super_block { unsigned short s_ninodes; unsigned short s_nzones; unsigned short s_imap_blocks; unsigned short s_zmap_blocks; unsigned short s_firstdatazone; unsigned short s_log_zone_size; unsigned long s_max_size; unsigned short s_magic; } *

user_defined_types: struct minix_d_inode { unsigned short i_mode; unsigned short i_uid; unsigned long i_size; unsigned long i_time; unsigned char i_gid; unsigned char i_nlinks; unsigned short i_zone[9]; }


decompiled_code:
__int64 __fastcall minix_get_block(__int64 a1, int a2)
{
return get_block(*(_QWORD *)a1, (unsigned int)(*(_DWORD *)(a1 + 8) + a2));
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2)
{
return get_block(*(_QWORD *)a1, (unsigned int)(*(_DWORD *)(a1 + 8) + a2));
}

original_code:
struct block *minix_get_block(struct super_block *s, int blk)
{
 return get_block(s->s_bdev, s->s_start + blk);
}

variable_types: struct super_block { struct block_device *s_bdev; int s_start; struct block *s_block; } *

variable_types: blk int

return_type: struct block { struct hlist_node { struct hlist_node *next; struct hlist_node **pprev; } b_hnode; struct list_head { struct list_head *prev; struct list_head *next; } b_list; int b_nr; int b_sects; int b_size; int b_refcnt; void *b_data; struct block_device { struct list_head block_list; int size; int blocks; int no; struct bdev_ops { int (*read)(struct block *); int (*write)(struct block *); int (*set_block_size)(struct block_device *, int); } *ops; } *b_dev; unsigned int b_dirty; unsigned int b_private; } *

user_defined_types: struct block { struct hlist_node b_hnode; struct list_head b_list; int b_nr; int b_sects; int b_size; int b_refcnt; void *b_data; struct block_device *b_dev; unsigned int b_dirty; unsigned int b_private; }

user_defined_types: struct block_device { struct list_head block_list; int size; int blocks; int no; struct bdev_ops *ops; }


decompiled_code:
__int64 minix_fs_init()
{
__int64 v0; // rdx
__int64 v1; // rax
__int64 v2; // rbx
__int64 v3; // rbx
__int64 v4; // rdi
__int64 result; // rax

minix_sb = (__int64)&hd_bdev;
set_block_size(&hd_bdev, 1024LL);
v0 = *(unsigned int *)(minix_sb + 20);
dword_248 = MEMORY[0xC0007DFA];
if ( MEMORY[0xC0007DFA] >= (int)v0 )
panic("minixfs starts from %d block over %d blocks of device", MEMORY[0xC0007DFA], v0);
v1 = minix_get_block((__int64)&minix_sb, 1);
qword_250 = v1;
if ( !v1 )
{
panic("Cannot get minixfs super block");
v1 = qword_250;
}
minix_check_super(*(unsigned __int16 **)(v1 + 48));
minix_inode_init();
v2 = ctask;
*(_QWORD *)(v2 + 200) = minix_get_inode(&minix_sb, 1LL);
v3 = ctask;
v4 = *(_QWORD *)(ctask + 200LL);
if ( v4 )
{
result = get_inode_ref(v4);
}
else
{
panic("Cannot get minix root dir");
v3 = ctask;
result = get_inode_ref(*(_QWORD *)(ctask + 200LL));
}
*(_QWORD *)(v3 + 192) = result;
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // rdx
__int64 v1; // rax
__int64 v2; // rbx
__int64 v3; // rbx
__int64 v4; // rdi
__int64 result; // rax

minix_sb = (__int64)&hd_bdev;
set_block_size(&hd_bdev, 1024LL);
v0 = *(unsigned int *)(minix_sb + 20);
dword_248 = MEMORY[0xC0007DFA];
if ( MEMORY[0xC0007DFA] >= (int)v0 )
panic("minixfs starts from %d block over %d blocks of device", MEMORY[0xC0007DFA], v0);
v1 = func1((__int64)&minix_sb, 1);
qword_250 = v1;
if ( !v1 )
{
panic("Cannot get minixfs super block");
v1 = qword_250;
}
func0(*(unsigned __int16 **)(v1 + 48));
minix_inode_init();
v2 = ctask;
*(_QWORD *)(v2 + 200) = minix_get_inode(&minix_sb, 1LL);
v3 = ctask;
v4 = *(_QWORD *)(ctask + 200LL);
if ( v4 )
{
result = get_inode_ref(v4);
}
else
{
panic("Cannot get minix root dir");
v3 = ctask;
result = get_inode_ref(*(_QWORD *)(ctask + 200LL));
}
*(_QWORD *)(v3 + 192) = result;
return result;
}

original_code:
void minix_fs_init(void)
{
 minix_sb.s_bdev = &hd_bdev;
 set_block_size(minix_sb.s_bdev, (1 << 10));
 minix_sb.s_start = *(unsigned short *)((unsigned int)(0x7c00 + 506) + 0xc0000000);
 if (minix_sb.s_start >= minix_sb.s_bdev->blocks)
  panic("minixfs starts from %d block over %d blocks of device",
   minix_sb.s_start, minix_sb.s_bdev->blocks);
 minix_sb.s_block = minix_get_block(&minix_sb, 1);
 if (!minix_sb.s_block)
  panic("Cannot get minixfs super block");
 minix_check_super(((struct minix_d_super_block *)((&minix_sb)->s_block->b_data)));
 minix_inode_init();
 ctask->fs.root_dir = minix_get_inode(&minix_sb, 1);
 if (!ctask->fs.root_dir)
  panic("Cannot get minix root dir");
 ctask->fs.current_dir = get_inode_ref(ctask->fs.root_dir);
}

canonical_original_code:
void minix_fs_init()
{
 minix_sb.s_bdev = &hd_bdev;
 set_block_size(minix_sb.s_bdev, (1 << 10));
 minix_sb.s_start = *(unsigned short *)((unsigned int)(0x7c00 + 506) + 0xc0000000);
 if (minix_sb.s_start >= minix_sb.s_bdev->blocks)
  panic("minixfs starts from %d block over %d blocks of device",
   minix_sb.s_start, minix_sb.s_bdev->blocks);
 minix_sb.s_block = minix_get_block(&minix_sb, 1);
 if (!minix_sb.s_block)
  panic("Cannot get minixfs super block");
 minix_check_super(((struct minix_d_super_block *)((&minix_sb)->s_block->b_data)));
 minix_inode_init();
 ctask->fs.root_dir = minix_get_inode(&minix_sb, 1);
 if (!ctask->fs.root_dir)
  panic("Cannot get minix root dir");
 ctask->fs.current_dir = get_inode_ref(ctask->fs.root_dir);
}

user_defined_types: struct minix_d_super_block { unsigned short s_ninodes; unsigned short s_nzones; unsigned short s_imap_blocks; unsigned short s_zmap_blocks; unsigned short s_firstdatazone; unsigned short s_log_zone_size; unsigned long s_max_size; unsigned short s_magic; }


decompiled_code:
__int64 __fastcall imap_create(__int64 a1)
{
int v1; // er13
int v2; // er13
unsigned int v3; // ebx
__int64 v4; // rbp
int v5; // eax
int v6; // er12

v1 = *(unsigned __int16 *)(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 48LL) + 4LL);
if ( (_WORD)v1 )
{
v2 = v1 + 2;
v3 = 2;
while ( 1 )
{
v4 = minix_get_block(a1, v3);
v5 = searchzerobit(*(_QWORD *)(v4 + 48), 0x2000LL, 1LL);
*(_BYTE *)(v4 + 64) |= 1u;
v6 = v5;
put_block(v4);
if ( v6 >= 0 )
break;
if ( ++v3 == v2 )
return (unsigned int)v6;
}
v6 += (v3 - 2) << 13;
}
else
{
v6 = 0;
}
return (unsigned int)v6;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
int v1; // er13
int v2; // er13
unsigned int v3; // ebx
__int64 v4; // rbp
int v5; // eax
int v6; // er12

v1 = *(unsigned __int16 *)(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 48LL) + 4LL);
if ( (_WORD)v1 )
{
v2 = v1 + 2;
v3 = 2;
while ( 1 )
{
v4 = minix_get_block(a1, v3);
v5 = searchzerobit(*(_QWORD *)(v4 + 48), 0x2000LL, 1LL);
*(_BYTE *)(v4 + 64) |= 1u;
v6 = v5;
put_block(v4);
if ( v6 >= 0 )
break;
if ( ++v3 == v2 )
return (unsigned int)v6;
}
v6 += (v3 - 2) << 13;
}
else
{
v6 = 0;
}
return (unsigned int)v6;
}

original_code:
static int imap_create(struct super_block *sb)
{
 struct block *block;
 int i, n, r;
 i = (2 + (0) / (8 * (1 << 10)));
 n = (2 + (0) / (8 * (1 << 10))) + ((struct minix_d_super_block *)((sb)->s_block->b_data))->s_imap_blocks;
 while (i < n) {
  block = minix_get_block(sb, i);
  r = searchzerobit(block->b_data, (8 * (1 << 10)), 1);
  block->b_dirty = 1;
  put_block(block);
  if (r >= 0) {
   r = (i - (2 + (0) / (8 * (1 << 10)))) * (8 * (1 << 10)) + r;
   break;
  }
  i++;
 }
 return r;
}

canonical_original_code:
static int imap_create(struct super_block *sb)
{
 struct block *block;
 int i; int n; int r;
 i = (2 + (0) / (8 * (1 << 10)));
 n = (2 + (0) / (8 * (1 << 10))) + ((struct minix_d_super_block *)((sb)->s_block->b_data))->s_imap_blocks;
 while (i < n) {
  block = minix_get_block(sb, i);
  r = searchzerobit(block->b_data, (8 * (1 << 10)), 1);
  block->b_dirty = 1;
  put_block(block);
  if (r >= 0) {
   r = (i - (2 + (0) / (8 * (1 << 10)))) * (8 * (1 << 10)) + r;
   break;
  }
  i++;
 }
 return r;
}

variable_types: struct super_block { struct block_device { struct list_head { struct list_head *prev; struct list_head *next; } block_list; int size; int blocks; int no; struct bdev_ops { int (*read)(struct block { struct hlist_node { struct hlist_node *next; struct hlist_node **pprev; } b_hnode; struct list_head b_list; int b_nr; int b_sects; int b_size; int b_refcnt; void *b_data; struct block_device *b_dev; unsigned int b_dirty; unsigned int b_private; } *); int (*write)(struct block *); int (*set_block_size)(struct block_device *, int); } *ops; } *s_bdev; int s_start; struct block *s_block; } *

variable_types: struct block { struct hlist_node { struct hlist_node *next; struct hlist_node **pprev; } b_hnode; struct list_head { struct list_head *prev; struct list_head *next; } b_list; int b_nr; int b_sects; int b_size; int b_refcnt; void *b_data; struct block_device { struct list_head block_list; int size; int blocks; int no; struct bdev_ops { int (*read)(struct block *); int (*write)(struct block *); int (*set_block_size)(struct block_device *, int); } *ops; } *b_dev; unsigned int b_dirty; unsigned int b_private; } *

variable_types: i int

variable_types: r int

user_defined_types: struct super_block { struct block_device *s_bdev; int s_start; struct block *s_block; }


decompiled_code:
__int64 __fastcall bmap_put_block(__int64 a1, int a2)
{
__int64 v2; // rcx
int v3; // eax
int v4; // edx
int v5; // eax
__int64 v6; // rbp

v2 = *(_QWORD *)(*(_QWORD *)(a1 + 16) + 48LL);
v3 = a2 - *(unsigned __int16 *)(v2 + 8);
v4 = v3 + 0x2000;
v5 = v3 + 1;
if ( v5 < 0 )
v5 = v4;
v6 = minix_get_block(a1, (v5 >> 13) + (unsigned int)*(unsigned __int16 *)(v2 + 4) + 2);
if ( !(unsigned int)clearbit(
*(_QWORD *)(v6 + 48),
((_WORD)a2 - *(_WORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 48LL) + 8LL) + 1) & 0x1FFF) )
panic("Clear unexist-zone bitmap");
*(_BYTE *)(v6 + 64) |= 1u;
return put_block(v6);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2)
{
__int64 v2; // rcx
int v3; // eax
int v4; // edx
int v5; // eax
__int64 v6; // rbp

v2 = *(_QWORD *)(*(_QWORD *)(a1 + 16) + 48LL);
v3 = a2 - *(unsigned __int16 *)(v2 + 8);
v4 = v3 + 0x2000;
v5 = v3 + 1;
if ( v5 < 0 )
v5 = v4;
v6 = minix_get_block(a1, (v5 >> 13) + (unsigned int)*(unsigned __int16 *)(v2 + 4) + 2);
if ( !(unsigned int)clearbit(
*(_QWORD *)(v6 + 48),
((_WORD)a2 - *(_WORD *)(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 48LL) + 8LL) + 1) & 0x1FFF) )
panic("Clear unexist-zone bitmap");
*(_BYTE *)(v6 + 64) |= 1u;
return put_block(v6);
}

original_code:
static void bmap_put_block(struct super_block *sb, int blk)
{
 struct block *block;
 block = minix_get_block(sb, (((((struct minix_d_super_block *)((sb)->s_block->b_data)))->s_imap_blocks + 2) + ((((blk) - (((struct minix_d_super_block *)((sb)->s_block->b_data)))->s_firstdatazone + 1)) / (8 * (1 << 10)))));
 if (!clearbit(block->b_data, (((blk) - (((struct minix_d_super_block *)((sb)->s_block->b_data)))->s_firstdatazone + 1) & ((8 * (1 << 10)) - 1))))
  panic("Clear unexist-zone bitmap");
 block->b_dirty = 1;
 put_block(block);
}

user_defined_types: struct hlist_node { struct hlist_node *next; struct hlist_node **pprev; }

user_defined_types: struct list_head { struct list_head *prev; struct list_head *next; }

user_defined_types: struct bdev_ops { int (*read)(struct block *); int (*write)(struct block *); int (*set_block_size)(struct block_device *, int); }


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

v2 = minix_get_block(a1, (a2 >> 13) + 2);
v3 = testbit(*(_QWORD *)(v2 + 48), a2 & 0x1FFF);
put_block(v2);
return v3;
}

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

v2 = minix_get_block(a1, (a2 >> 13) + 2);
v3 = testbit(*(_QWORD *)(v2 + 48), a2 & 0x1FFF);
put_block(v2);
return v3;
}

original_code:
static int imap_lookup(struct super_block *sb, unsigned int ino)
{
 struct block *block;
 int r;
 block = minix_get_block(sb, (2 + (ino) / (8 * (1 << 10))));
 r = testbit(block->b_data, ino & ((8 * (1 << 10)) - 1));
 put_block(block);
 return r;
}

variable_types:
ino
unsigned int


decompiled_code:
__int64 __fastcall minix_new_block_nr(__int64 a1)
{
unsigned __int16 *v1; // r14
int v2; // ebx
int v3; // er12
int v4; // eax
__int64 v5; // rax
__int64 v6; // rbp
int v8; // [rsp+Ch] [rbp-2Ch]

v1 = *(unsigned __int16 **)(*(_QWORD *)(a1 + 16) + 48LL);
if ( v1[3] )
{
v2 = 0;
v3 = v1[2] + 2;
do
{
v5 = minix_get_block(a1, (unsigned int)(v3 + v2));
v6 = v5;
if ( !v5 )
break;
v4 = searchzerobit(*(_QWORD *)(v5 + 48), 0x2000LL, 1LL);
if ( v4 > 0 )
{
*(_BYTE *)(v6 + 64) |= 1u;
v8 = v4;
put_block(v6);
return v8 + (unsigned int)v1[4] + (v2 << 13) - 1;
}
++v2;
put_block(v6);
}
while ( v1[3] > v2 );
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
unsigned __int16 *v1; // r14
int v2; // ebx
int v3; // er12
int v4; // eax
__int64 v5; // rax
__int64 v6; // rbp
int v8; // [rsp+Ch] [rbp-2Ch]

v1 = *(unsigned __int16 **)(*(_QWORD *)(a1 + 16) + 48LL);
if ( v1[3] )
{
v2 = 0;
v3 = v1[2] + 2;
do
{
v5 = minix_get_block(a1, (unsigned int)(v3 + v2));
v6 = v5;
if ( !v5 )
break;
v4 = searchzerobit(*(_QWORD *)(v5 + 48), 0x2000LL, 1LL);
if ( v4 > 0 )
{
*(_BYTE *)(v6 + 64) |= 1u;
v8 = v4;
put_block(v6);
return v8 + (unsigned int)v1[4] + (v2 << 13) - 1;
}
++v2;
put_block(v6);
}
while ( v1[3] > v2 );
}
return 0xFFFFFFFFLL;
}

original_code:
int minix_new_block_nr(struct super_block *sb)
{
 struct minix_d_super_block *msb = ((struct minix_d_super_block *)((sb)->s_block->b_data));
 int i, r, blk = ((msb)->s_imap_blocks + 2);
 struct block *block;
 for (i = 0; i < msb->s_zmap_blocks; i++) {
  block = minix_get_block(sb, blk);
  if (!block)
   return -1;
  r = searchzerobit(block->b_data, (8 * (1 << 10)), 1);
  if (r > 0) {
   block->b_dirty = 1;
   put_block(block);
   return (msb->s_firstdatazone + i * (8 * (1 << 10)) + r - 1);
  }
  put_block(block);
  blk++;
 }
 return -1;
}

canonical_original_code:
int minix_new_block_nr(struct super_block *sb)
{
 struct minix_d_super_block *msb = ((struct minix_d_super_block *)((sb)->s_block->b_data));
 int i; int r; int blk = ((msb)->s_imap_blocks + 2);
 struct block *block;
 for (i = 0; i < msb->s_zmap_blocks; i++) {
  block = minix_get_block(sb, blk);
  if (!block)
   return -1;
  r = searchzerobit(block->b_data, (8 * (1 << 10)), 1);
  if (r > 0) {
   block->b_dirty = 1;
   put_block(block);
   return (msb->s_firstdatazone + i * (8 * (1 << 10)) + r - 1);
  }
  put_block(block);
  blk++;
 }
 return -1;
}


decompiled_code:
__int64 __fastcall minix_new_block(__int64 a1, _WORD *a2)
{
int v2; // eax
__int64 v3; // rax
__int64 v4; // r12

v2 = minix_new_block_nr(a1);
if ( v2 < 0 )
return 0LL;
if ( a2 )
*a2 = v2;
v3 = minix_get_block(a1, (unsigned int)v2);
v4 = v3;
if ( v3 )
memset(*(void **)(v3 + 48), 0, *(unsigned int *)(v3 + 40));
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, _WORD *a2)
{
int v2; // eax
__int64 v3; // rax
__int64 v4; // r12

v2 = func5(a1);
if ( v2 < 0 )
return 0LL;
if ( a2 )
*a2 = v2;
v3 = minix_get_block(a1, (unsigned int)v2);
v4 = v3;
if ( v3 )
memset(*(void **)(v3 + 48), 0, *(unsigned int *)(v3 + 40));
return v4;
}

original_code:
struct block *minix_new_block(struct super_block *sb, unsigned short *data)
{
 struct block *block;
 int blk = minix_new_block_nr(sb);
 if (blk < 0)
  return ((void *)0);
 if (data)
  *data = blk;
 block = minix_get_block(sb, blk);
 if (block)
  memset(block->b_data, 0x0, block->b_size);
 return block;
}

variable_types:
data
unsigned short *


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

if ( (unsigned int)imap_lookup(a1, a2) )
result = minix_get_block(
a1,
a2 / 0x19
+ *(unsigned __int16 *)(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 48LL) + 4LL)
+ *(unsigned __int16 *)(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 48LL) + 6LL)
+ 2);
else
result = 0LL;
return result;
}

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

if ( (unsigned int)func2(a1, a2) )
result = minix_get_block(
a1,
a2 / 0x19
+ *(unsigned __int16 *)(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 48LL) + 4LL)
+ *(unsigned __int16 *)(*(_QWORD *)(*(_QWORD *)(a1 + 16) + 48LL) + 6LL)
+ 2);
else
result = 0LL;
return result;
}

original_code:
struct block *imap_block(struct super_block *sb, unsigned int ino)
{
 if (imap_lookup(sb, ino) == 0)
  return ((void *)0);
 return minix_get_block(sb, ((2 + (((struct minix_d_super_block *)((sb)->s_block->b_data)))->s_imap_blocks + (((struct minix_d_super_block *)((sb)->s_block->b_data)))->s_zmap_blocks) + (ino) / (((1 << 10)) / (sizeof(struct minix_d_inode)))));
}


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

result = imap_block(a1, a2);
if ( result )
{
if ( a3 )
*a3 = result;
result = *(_QWORD *)(result + 48) + 40LL * ((a2 - 1) % 0x19);
}
return result;
}

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

result = func10(a1, a2);
if ( result )
{
if ( a3 )
*a3 = result;
result = *(_QWORD *)(result + 48) + 40LL * ((a2 - 1) % 0x19);
}
return result;
}

original_code:
struct minix_d_inode *imap_get_inode(struct super_block *sb, unsigned int ino,
  struct block **b)
{
 struct block *block = imap_block(sb, ino);
 if (!block)
  return ((void *)0);
 if (b)
  *b = block;
 return ((struct minix_d_inode *)(block)->b_data + ((ino) - 1) % (((1 << 10)) / (sizeof(struct minix_d_inode))));
}

variable_types:
b
struct block **

return_type: struct minix_d_inode { unsigned short i_mode; unsigned short i_uid; unsigned long i_size; unsigned long i_time; unsigned char i_gid; unsigned char i_nlinks; unsigned short i_zone[9]; } *


decompiled_code:
void __fastcall __noreturn per_exit(char *s)
{
if ( *__errno_location() )
perror(s);
else
fprintf(stderr, "ERROR: %s\n", s);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func7(char *s)
{
if ( *__errno_location() )
perror(s);
else
fprintf(stderr, "ERROR: %s\n", s);
exit(1);
}

original_code:
void per_exit(char *str)
{
 if ((*__errno_location ()))
  perror(str);
 else
  fprintf(stderr, "ERROR: %s\n", str);
 exit(1);
}

variable_types:
str
char *


decompiled_code:
__int64 open_set_param()
{
int v0; // eax
int v1; // ebp
__int64 result; // rax
unsigned __int64 v3; // [rsp+8h] [rbp-A0h] BYREF
struct stat stat_buf; // [rsp+10h] [rbp-98h] BYREF

v0 = open(devfile, 2);
devfd = v0;
if ( v0 == -1 )
per_exit("open devfile");
v1 = v0;
if ( (unsigned int)fstat(v0, &stat_buf) == -1 )
per_exit("fstat");
if ( (stat_buf.st_mode & 0xF000) == 24576 )
{
if ( ioctl(v1, 0x1260uLL, &v3) == -1 )
per_exit("ioctl");
blocks = v3 >> 1;
}
else
{
blocks = stat_buf.st_size / 1024;
}
if ( (unsigned int)lseek(v1, 506LL, 0) != 506 )
per_exit("lseek");
if ( (unsigned int)read(devfd, &blkoff, 2uLL) != 2 )
per_exit("read");
blocks -= blkoff;
printf(" <Minix fs starts from %dth block with %d blocks>\n", (unsigned int)blkoff, (unsigned int)blocks);
result = (unsigned int)(blocks - 10);
if ( (unsigned int)result > 0xFFF5 )
per_exit("invalid block number");
return result;
}

canonical_decompiled_code:
__int64 func8()
{
int v0; // eax
int v1; // ebp
__int64 result; // rax
unsigned __int64 v3; // [rsp+8h] [rbp-A0h] BYREF
struct stat stat_buf; // [rsp+10h] [rbp-98h] BYREF

v0 = open(devfile, 2);
devfd = v0;
if ( v0 == -1 )
func7("open devfile");
v1 = v0;
if ( (unsigned int)func16(v0, &stat_buf) == -1 )
func7("fstat");
if ( (stat_buf.st_mode & 0xF000) == 24576 )
{
if ( ioctl(v1, 0x1260uLL, &v3) == -1 )
func7("ioctl");
blocks = v3 >> 1;
}
else
{
blocks = stat_buf.st_size / 1024;
}
if ( (unsigned int)lseek(v1, 506LL, 0) != 506 )
func7("lseek");
if ( (unsigned int)read(devfd, &blkoff, 2uLL) != 2 )
func7("read");
blocks -= blkoff;
printf(" <Minix fs starts from %dth block with %d blocks>\n", (unsigned int)blkoff, (unsigned int)blocks);
result = (unsigned int)(blocks - 10);
if ( (unsigned int)result > 0xFFF5 )
func7("invalid block number");
return result;
}

original_code:
void open_set_param(void)
{
 struct stat sbuf;
 unsigned long size;
 devfd = open(devfile, 02);
 if (devfd == -1)
  per_exit("open devfile");
 if (fstat(devfd, &sbuf) == -1)
  per_exit("fstat");
 if (!((((sbuf.st_mode)) & 0170000) == (0060000))) {
  ;
  blocks = sbuf.st_size / 1024;
 } else {
  if (ioctl(devfd, 0x00001260, &size) == -1)
   per_exit("ioctl");
  blocks = size / (1024 / 512);
 }
 if (lseek(devfd, 506, 0) != 506)
  per_exit("lseek");
 if (read(devfd, &blkoff, 2) != 2)
  per_exit("read");
 blocks -= blkoff;
 printf(" <Minix fs starts from %dth block with %d blocks>\n",
  blkoff, blocks);
 if (blocks < 10 || blocks > 65535)
  per_exit("invalid block number");
}

canonical_original_code:
void open_set_param()
{
 struct stat sbuf;
 unsigned long size;
 devfd = open(devfile, 02);
 if (devfd == -1)
  per_exit("open devfile");
 if (fstat(devfd, &sbuf) == -1)
  per_exit("fstat");
 if (!((((sbuf.st_mode)) & 0170000) == (0060000))) {
  ;
  blocks = sbuf.st_size / 1024;
 } else {
  if (ioctl(devfd, 0x00001260, &size) == -1)
   per_exit("ioctl");
  blocks = size / (1024 / 512);
 }
 if (lseek(devfd, 506, 0) != 506)
  per_exit("lseek");
 if (read(devfd, &blkoff, 2) != 2)
  per_exit("read");
 blocks -= blkoff;
 printf(" <Minix fs starts from %dth block with %d blocks>\n",
  blkoff, blocks);
 if (blocks < 10 || blocks > 65535)
  per_exit("invalid block number");
}

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

variable_types:
size
unsigned long

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


decompiled_code:
ssize_t clean_boot()
{
ssize_t result; // rax

memset(&block_buffer, 0, 0x400uLL);
if ( (unsigned int)lseek(devfd, (unsigned int)(blkoff << 10), 0) == -1 )
per_exit("lseek");
result = write(devfd, &block_buffer, 0x400uLL);
if ( (_DWORD)result != 1024 )
per_exit("write: boot block");
return result;
}

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

memset(&block_buffer, 0, 0x400uLL);
if ( (unsigned int)lseek(devfd, (unsigned int)(blkoff << 10), 0) == -1 )
func7("lseek");
result = write(devfd, &block_buffer, 0x400uLL);
if ( (_DWORD)result != 1024 )
func7("write: boot block");
return result;
}

original_code:
void clean_boot(void)
{
 memset(block_buffer, 0x0, 1024);
 do { if (lseek(devfd, ((0) + blkoff) * 1024, 0) == -1) per_exit("lseek"); if (1024 != write(devfd, block_buffer, 1024)) { per_exit("write: ""boot block"); } } while (0);
}

canonical_original_code:
void clean_boot()
{
 memset(block_buffer, 0x0, 1024);
 do { if (lseek(devfd, ((0) + blkoff) * 1024, 0) == -1) per_exit("lseek"); if (1024 != write(devfd, block_buffer, 1024)) { per_exit("write: ""boot block"); } } while (0);
}


decompiled_code:
ssize_t write_super_block()
{
int v0; // edi
__int64 v1; // rdx
unsigned __int64 v2; // rcx
unsigned __int64 v3; // rsi
unsigned __int64 v4; // rax
__int64 v5; // rdx
__int16 v6; // r8
__int16 v7; // ax
ssize_t result; // rax

memset(&super_block, 0, 0x400uLL);
qword_4040D0 = 268966912LL;
word_4040D8 = 4991;
word_4040C2 = blocks;
super_block = blocks / 3u;
v0 = ((unsigned __int16)super_block + 8190) >> 13;
v1 = (0x8618618618618619LL * (unsigned __int128)((unsigned __int64)(unsigned __int16)super_block + 20)) >> 64;
word_4040C4 = v0;
v2 = (v1 + (((unsigned __int64)(unsigned __int16)super_block + 20 - v1) >> 1)) >> 4;
v3 = (unsigned int)blocks + 8189LL - (v2 + v0);
v4 = v3 >> 13;
if ( v3 >> 13 )
{
do
{
v5 = (unsigned __int16)v4;
v6 = v4;
v4 = (v3 - (unsigned __int16)v4) >> 13;
}
while ( v5 != v4 );
word_4040C6 = v6;
v7 = v6;
}
else
{
v7 = 0;
}
word_4040C8 = v7 + v0 + v2 + 2;
if ( (unsigned int)lseek(devfd, (unsigned int)((blkoff + 1) << 10), 0) == -1 )
per_exit("lseek");
result = write(devfd, &super_block, 0x400uLL);
if ( (_DWORD)result != 1024 )
per_exit("write: super block");
return result;
}

canonical_decompiled_code:
ssize_t func10()
{
int v0; // edi
__int64 v1; // rdx
unsigned __int64 v2; // rcx
unsigned __int64 v3; // rsi
unsigned __int64 v4; // rax
__int64 v5; // rdx
__int16 v6; // r8
__int16 v7; // ax
ssize_t result; // rax

memset(&super_block, 0, 0x400uLL);
qword_4040D0 = 268966912LL;
word_4040D8 = 4991;
word_4040C2 = blocks;
super_block = blocks / 3u;
v0 = ((unsigned __int16)super_block + 8190) >> 13;
v1 = (0x8618618618618619LL * (unsigned __int128)((unsigned __int64)(unsigned __int16)super_block + 20)) >> 64;
word_4040C4 = v0;
v2 = (v1 + (((unsigned __int64)(unsigned __int16)super_block + 20 - v1) >> 1)) >> 4;
v3 = (unsigned int)blocks + 8189LL - (v2 + v0);
v4 = v3 >> 13;
if ( v3 >> 13 )
{
do
{
v5 = (unsigned __int16)v4;
v6 = v4;
v4 = (v3 - (unsigned __int16)v4) >> 13;
}
while ( v5 != v4 );
word_4040C6 = v6;
v7 = v6;
}
else
{
v7 = 0;
}
word_4040C8 = v7 + v0 + v2 + 2;
if ( (unsigned int)lseek(devfd, (unsigned int)((blkoff + 1) << 10), 0) == -1 )
func7("lseek");
result = write(devfd, &super_block, 0x400uLL);
if ( (_DWORD)result != 1024 )
func7("write: super block");
return result;
}

original_code:
void write_super_block(void)
{
 memset(super_block, 0x0, 1024);
 sb->s_log_zone_size = 0;
 sb->s_max_size = (7 + 512 + 512 * 512) * 1024;
 sb->s_magic = 0x137F;
 sb->s_ninodes = blocks / 3;
 sb->s_nzones = blocks;
 sb->s_imap_blocks = ((sb->s_ninodes - 1 + ((8 * 1024) - 1)) / (8 * 1024));
 sb->s_zmap_blocks = 0;
 while (sb->s_zmap_blocks != ((blocks - (2 + (((sb)->s_ninodes + ((((1024)/ (sizeof(struct d_inode)))) - 1)) / (((1024)/ (sizeof(struct d_inode))))) + (sb)->s_imap_blocks + (sb)->s_zmap_blocks) + ((8 * 1024) - 1)) / (8 * 1024)))
  sb->s_zmap_blocks = ((blocks - (2 + (((sb)->s_ninodes + ((((1024)/ (sizeof(struct d_inode)))) - 1)) / (((1024)/ (sizeof(struct d_inode))))) + (sb)->s_imap_blocks + (sb)->s_zmap_blocks) + ((8 * 1024) - 1)) / (8 * 1024));
 sb->s_firstdatazone = (2 + (((sb)->s_ninodes + ((((1024)/ (sizeof(struct d_inode)))) - 1)) / (((1024)/ (sizeof(struct d_inode))))) + (sb)->s_imap_blocks + (sb)->s_zmap_blocks);
 do { if (lseek(devfd, ((1) + blkoff) * 1024, 0) == -1) per_exit("lseek"); if (1024 != write(devfd, super_block, 1024)) { per_exit("write: ""super block"); } } while (0);
 ;
 ;
}

canonical_original_code:
void write_super_block()
{
 memset(super_block, 0x0, 1024);
 sb->s_log_zone_size = 0;
 sb->s_max_size = (7 + 512 + 512 * 512) * 1024;
 sb->s_magic = 0x137F;
 sb->s_ninodes = blocks / 3;
 sb->s_nzones = blocks;
 sb->s_imap_blocks = ((sb->s_ninodes - 1 + ((8 * 1024) - 1)) / (8 * 1024));
 sb->s_zmap_blocks = 0;
 while (sb->s_zmap_blocks != ((blocks - (2 + (((sb)->s_ninodes + ((((1024)/ (sizeof(struct d_inode)))) - 1)) / (((1024)/ (sizeof(struct d_inode))))) + (sb)->s_imap_blocks + (sb)->s_zmap_blocks) + ((8 * 1024) - 1)) / (8 * 1024)))
  sb->s_zmap_blocks = ((blocks - (2 + (((sb)->s_ninodes + ((((1024)/ (sizeof(struct d_inode)))) - 1)) / (((1024)/ (sizeof(struct d_inode))))) + (sb)->s_imap_blocks + (sb)->s_zmap_blocks) + ((8 * 1024) - 1)) / (8 * 1024));
 sb->s_firstdatazone = (2 + (((sb)->s_ninodes + ((((1024)/ (sizeof(struct d_inode)))) - 1)) / (((1024)/ (sizeof(struct d_inode))))) + (sb)->s_imap_blocks + (sb)->s_zmap_blocks);
 do { if (lseek(devfd, ((1) + blkoff) * 1024, 0) == -1) per_exit("lseek"); if (1024 != write(devfd, super_block, 1024)) { per_exit("write: ""super block"); } } while (0);
 ;
 ;
}

user_defined_types: struct d_inode { unsigned short i_mode; unsigned short i_uid; unsigned long i_size; unsigned long i_time; unsigned char i_gid; unsigned char i_nlinks; unsigned short i_zone[9]; }


decompiled_code:
__int64 write_maps()
{
int v0; // ebx
__int64 result; // rax

memset(&block_buffer, 0, 0x400uLL);
if ( (unsigned int)lseek(devfd, (unsigned int)((blkoff + 2) << 10), 0) == -1 )
per_exit("lseek");
v0 = 0;
result = (unsigned __int16)word_4040C6 + (unsigned int)(unsigned __int16)word_4040C4;
if ( (unsigned __int16)word_4040C6 + (unsigned __int16)word_4040C4 )
{
do
{
if ( (unsigned int)write(devfd, &block_buffer, 0x400uLL) != 1024 )
per_exit("write map_blks");
++v0;
result = (unsigned __int16)word_4040C6 + (unsigned int)(unsigned __int16)word_4040C4;
}
while ( (int)result > v0 );
}
return result;
}

canonical_decompiled_code:
__int64 func11()
{
int v0; // ebx
__int64 result; // rax

memset(&block_buffer, 0, 0x400uLL);
if ( (unsigned int)lseek(devfd, (unsigned int)((blkoff + 2) << 10), 0) == -1 )
func7("lseek");
v0 = 0;
result = (unsigned __int16)word_4040C6 + (unsigned int)(unsigned __int16)word_4040C4;
if ( (unsigned __int16)word_4040C6 + (unsigned __int16)word_4040C4 )
{
do
{
if ( (unsigned int)write(devfd, &block_buffer, 0x400uLL) != 1024 )
func7("write map_blks");
++v0;
result = (unsigned __int16)word_4040C6 + (unsigned int)(unsigned __int16)word_4040C4;
}
while ( (int)result > v0 );
}
return result;
}

original_code:
void write_maps(void)
{
 int i;
 memset(block_buffer, 0x0, 1024);
 if (lseek(devfd, (2 + blkoff) * 1024, 0) == -1)
  per_exit("lseek");
 for (i = 0; i < sb->s_imap_blocks + sb->s_zmap_blocks; i++)
  if (1024 != write(devfd, map_blk, 1024))
   per_exit("write map_blks");
}

canonical_original_code:
void write_maps()
{
 int i;
 memset(block_buffer, 0x0, 1024);
 if (lseek(devfd, (2 + blkoff) * 1024, 0) == -1)
  per_exit("lseek");
 for (i = 0; i < sb->s_imap_blocks + sb->s_zmap_blocks; i++)
  if (1024 != write(devfd, map_blk, 1024))
   per_exit("write map_blks");
}


decompiled_code:
ssize_t write_root()
{
__int64 *v0; // r12
int v1; // eax
int v2; // edx
int v3; // edi
ssize_t result; // rax

inode_blk = &block_buffer;
memset(&block_buffer, 0, 0x400uLL);
LOBYTE(block_buffer) = 3;
if ( (unsigned int)lseek(devfd, (unsigned int)((blkoff + 2) << 10), 0) == -1 )
goto LABEL_10;
if ( (unsigned int)write(devfd, &block_buffer, 0x400uLL) != 1024 )
goto LABEL_11;
LOBYTE(block_buffer) = 3;
if ( (unsigned int)lseek(devfd, ((unsigned int)(unsigned __int16)word_4040C4 + blkoff + 2) << 10, 0) == -1 )
goto LABEL_10;
if ( (unsigned int)write(devfd, &block_buffer, 0x400uLL) != 1024 )
LABEL_11:
per_exit("write: root inode map");
v0 = inode_blk;
*(_WORD *)inode_blk = 16877;
v0[1] = 32LL;
v1 = time(0LL);
v2 = (unsigned __int16)word_4040C4;
*((_BYTE *)v0 + 25) = 2;
v3 = devfd;
v0[2] = v1;
*((_WORD *)v0 + 13) = word_4040C8;
if ( (unsigned int)lseek(v3, (blkoff + v2 + (unsigned int)(unsigned __int16)word_4040C6 + 2) << 10, 0) == -1 )
goto LABEL_10;
if ( (unsigned int)write(devfd, &block_buffer, 0x400uLL) != 1024 )
per_exit("write: root inode");
memset(&block_buffer, 0, 0x400uLL);
block_buffer = 3014657LL;
qword_4044C8 = 0LL;
qword_4044D0 = 774766593LL;
qword_4044D8 = 0LL;
if ( (unsigned int)lseek(devfd, (blkoff + (unsigned int)(unsigned __int16)word_4040C8) << 10, 0) == -1 )
LABEL_10:
per_exit("lseek");
result = write(devfd, &block_buffer, 0x400uLL);
if ( (_DWORD)result != 1024 )
per_exit("write: root data block(dentry)");
return result;
}

canonical_decompiled_code:
ssize_t func12()
{
__int64 *v0; // r12
int v1; // eax
int v2; // edx
int v3; // edi
ssize_t result; // rax

inode_blk = &block_buffer;
memset(&block_buffer, 0, 0x400uLL);
LOBYTE(block_buffer) = 3;
if ( (unsigned int)lseek(devfd, (unsigned int)((blkoff + 2) << 10), 0) == -1 )
goto LABEL_10;
if ( (unsigned int)write(devfd, &block_buffer, 0x400uLL) != 1024 )
goto LABEL_11;
LOBYTE(block_buffer) = 3;
if ( (unsigned int)lseek(devfd, ((unsigned int)(unsigned __int16)word_4040C4 + blkoff + 2) << 10, 0) == -1 )
goto LABEL_10;
if ( (unsigned int)write(devfd, &block_buffer, 0x400uLL) != 1024 )
LABEL_11:
func7("write: root inode map");
v0 = inode_blk;
*(_WORD *)inode_blk = 16877;
v0[1] = 32LL;
v1 = time(0LL);
v2 = (unsigned __int16)word_4040C4;
*((_BYTE *)v0 + 25) = 2;
v3 = devfd;
v0[2] = v1;
*((_WORD *)v0 + 13) = word_4040C8;
if ( (unsigned int)lseek(v3, (blkoff + v2 + (unsigned int)(unsigned __int16)word_4040C6 + 2) << 10, 0) == -1 )
goto LABEL_10;
if ( (unsigned int)write(devfd, &block_buffer, 0x400uLL) != 1024 )
func7("write: root inode");
memset(&block_buffer, 0, 0x400uLL);
block_buffer = 3014657LL;
qword_4044C8 = 0LL;
qword_4044D0 = 774766593LL;
qword_4044D8 = 0LL;
if ( (unsigned int)lseek(devfd, (blkoff + (unsigned int)(unsigned __int16)word_4040C8) << 10, 0) == -1 )
LABEL_10:
func7("lseek");
result = write(devfd, &block_buffer, 0x400uLL);
if ( (_DWORD)result != 1024 )
func7("write: root data block(dentry)");
return result;
}

original_code:
void write_root(void)
{
 int i;
 struct d_inode *root;
 inode_blk = (unsigned char *)block_buffer;
 memset(block_buffer, 0x0, 1024);
 map_blk[0] = 0x03;
 do { if (lseek(devfd, ((2) + blkoff) * 1024, 0) == -1) per_exit("lseek"); if (1024 != write(devfd, block_buffer, 1024)) { per_exit("write: ""root inode map"); } } while (0);
 map_blk[0] = 0x03;
 do { if (lseek(devfd, ((2 + sb->s_imap_blocks) + blkoff) * 1024, 0) == -1) per_exit("lseek"); if (1024 != write(devfd, block_buffer, 1024)) { per_exit("write: ""root inode map"); } } while (0);
 root = (struct d_inode *)inode_blk;
 root->i_mode = 0040000 | 0755;
 root->i_size = (2 * sizeof(struct dir_entry));
 root->i_time = time(((void *)0));
 root->i_zone[0] = sb->s_firstdatazone;
 root->i_nlinks = 2;
 do { if (lseek(devfd, (((2 + (sb)->s_imap_blocks + (sb)->s_zmap_blocks)) + blkoff) * 1024, 0) == -1) per_exit("lseek"); if (1024 != write(devfd, block_buffer, 1024)) { per_exit("write: ""root inode"); } } while (0);
 memset(block_buffer, 0x0, 1024);
 memcpy(block_buffer, "\x1\x0.\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0" "\x1\x0..\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0", (2 * sizeof(struct dir_entry)));
 do { if (lseek(devfd, ((sb->s_firstdatazone) + blkoff) * 1024, 0) == -1) per_exit("lseek"); if (1024 != write(devfd, block_buffer, 1024)) { per_exit("write: ""root data block(dentry)"); } } while (0);
}

canonical_original_code:
void write_root()
{
 int i;
 struct d_inode *root;
 inode_blk = (unsigned char *)block_buffer;
 memset(block_buffer, 0x0, 1024);
 map_blk[0] = 0x03;
 do { if (lseek(devfd, ((2) + blkoff) * 1024, 0) == -1) per_exit("lseek"); if (1024 != write(devfd, block_buffer, 1024)) { per_exit("write: ""root inode map"); } } while (0);
 map_blk[0] = 0x03;
 do { if (lseek(devfd, ((2 + sb->s_imap_blocks) + blkoff) * 1024, 0) == -1) per_exit("lseek"); if (1024 != write(devfd, block_buffer, 1024)) { per_exit("write: ""root inode map"); } } while (0);
 root = (struct d_inode *)inode_blk;
 root->i_mode = 0040000 | 0755;
 root->i_size = (2 * sizeof(struct dir_entry));
 root->i_time = time(((void *)0));
 root->i_zone[0] = sb->s_firstdatazone;
 root->i_nlinks = 2;
 do { if (lseek(devfd, (((2 + (sb)->s_imap_blocks + (sb)->s_zmap_blocks)) + blkoff) * 1024, 0) == -1) per_exit("lseek"); if (1024 != write(devfd, block_buffer, 1024)) { per_exit("write: ""root inode"); } } while (0);
 memset(block_buffer, 0x0, 1024);
 memcpy(block_buffer, "\x1\x0.\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0" "\x1\x0..\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0", (2 * sizeof(struct dir_entry)));
 do { if (lseek(devfd, ((sb->s_firstdatazone) + blkoff) * 1024, 0) == -1) per_exit("lseek"); if (1024 != write(devfd, block_buffer, 1024)) { per_exit("write: ""root data block(dentry)"); } } while (0);
}

variable_types: struct d_inode { unsigned short i_mode; unsigned short i_uid; unsigned long i_size; unsigned long i_time; unsigned char i_gid; unsigned char i_nlinks; unsigned short i_zone[9]; } *

user_defined_types: struct dir_entry { unsigned short inode; char name[14]; }