decompiled_code:
__int64 check_apic()
{
_RAX = 1LL;
__asm { cpuid }
return BYTE1(_RDX) & 1;
}

canonical_decompiled_code:
__int64 func0()
{
_RAX = 1LL;
__asm { cpuid }
return BYTE1(_RDX) & 1;
}

original_code:
static uint8_t check_apic()
{
 uint32_t eax, edx;
 cpuid(0x1, &eax, &edx);
 return (edx & 0x100) > 0;
}

canonical_original_code:
static unsigned char check_apic()
{
 unsigned int eax; unsigned int edx;
 cpuid(0x1, &eax, &edx);
 return (edx & 0x100) > 0;
}

variable_types:
eax
unsigned int

variable_types:
edx
unsigned int

return_type: unsigned char


decompiled_code:
__int16 enable_apic()
{
unsigned __int64 v0; // rax

v0 = __readmsr(0x1Bu);
v0 = (unsigned int)v0;
BYTE1(v0) |= 8u;
__writemsr(0x1Bu, v0);
return v0;
}

canonical_decompiled_code:
__int16 func1()
{
unsigned __int64 v0; // rax

v0 = __readmsr(0x1Bu);
v0 = (unsigned int)v0;
BYTE1(v0) |= 8u;
__writemsr(0x1Bu, v0);
return v0;
}

original_code:
static void enable_apic()
{
 uint32_t eax, edx;
 readmsr(0x1B, &eax, &edx);
 eax |= 0x800;
 writemsr(0x1B, eax, edx);
}

canonical_original_code:
static void enable_apic()
{
 unsigned int eax; unsigned int edx;
 readmsr(0x1B, &eax, &edx);
 eax |= 0x800;
 writemsr(0x1B, eax, edx);
}

return_type: void


decompiled_code:
unsigned __int64 apic_base_address()
{
unsigned __int64 v0; // rax

v0 = __readmsr(0x1Bu);
return v0 & 0xFFFFF000;
}

canonical_decompiled_code:
unsigned __int64 func2()
{
unsigned __int64 v0; // rax

v0 = __readmsr(0x1Bu);
return v0 & 0xFFFFF000;
}

original_code:
static uint32_t apic_base_address()
{
 uint32_t eax, edx;
 readmsr(0x1B, &eax, &edx);
 return eax & 0xFFFFF000;
}

canonical_original_code:
static unsigned int apic_base_address()
{
 unsigned int eax; unsigned int edx;
 readmsr(0x1B, &eax, &edx);
 return eax & 0xFFFFF000;
}

return_type: unsigned int


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

__outbyte(0x20u, 0x11u);
__outbyte(0xA0u, 0x11u);
__outbyte(0x21u, 0x20u);
__outbyte(0xA1u, 0x28u);
__outbyte(0x21u, 4u);
__outbyte(0xA1u, 2u);
__outbyte(0x21u, 1u);
__outbyte(0xA1u, 1u);
result = 0LL;
__outbyte(0x21u, 0);
__outbyte(0xA1u, 0);
return result;
}

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

__outbyte(0x20u, 0x11u);
__outbyte(0xA0u, 0x11u);
__outbyte(0x21u, 0x20u);
__outbyte(0xA1u, 0x28u);
__outbyte(0x21u, 4u);
__outbyte(0xA1u, 2u);
__outbyte(0x21u, 1u);
__outbyte(0xA1u, 1u);
result = 0LL;
__outbyte(0x21u, 0);
__outbyte(0xA1u, 0);
return result;
}


decompiled_code:
unsigned __int64 timer_one_ms()
{
return (unsigned int)tsc_per_sec / 0x1F400uLL;
}

canonical_decompiled_code:
unsigned __int64 func4()
{
return (unsigned int)tsc_per_sec / 0x1F400uLL;
}

original_code:
time_t timer_one_ms()
{
 return tsc_per_sec / 1000 / 128;
}

canonical_original_code:
unsigned int timer_one_ms()
{
 return tsc_per_sec / 1000 / 128;
}


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

result = a1;
timer_delay = a1;
return result;
}

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

result = a1;
timer_delay = a1;
return result;
}

original_code:
void timer_set_delay(uint32_t delay)
{
 timer_delay = delay;
}

canonical_original_code:
void timer_set_delay(unsigned int delay)
{
 timer_delay = delay;
}

variable_types:
delay
unsigned int


decompiled_code:
__int64 timer_get_count()
{
return MEMORY[0xFFFFFFFFFFFFF390];
}

canonical_decompiled_code:
__int64 func6()
{
return MEMORY[0xFFFFFFFFFFFFF390];
}

original_code:
time_t timer_get_count()
{
 volatile uint32_t* lapic = (volatile uint32_t*)0xFFFFFFFFFFFFF000;
 return lapic[(0x390/sizeof(uint32_t))];
}

canonical_original_code:
unsigned int timer_get_count()
{
 unsigned int *lapic = (volatile uint32_t*)0xFFFFFFFFFFFFF000;
 return lapic[(0x390/sizeof(unsigned int))];
}

variable_types:
lapic
unsigned int *


decompiled_code:
__int64 timer_get_elapsed()
{
unsigned int v0; // ebx
int v1; // eax

v0 = MEMORY[0xFFFFFFFFFFFFF390];
v1 = timer_delay;
if ( (unsigned int)timer_delay >= MEMORY[0xFFFFFFFFFFFFF390] )
return v1 - v0;
kprintf("PTC: %u - COUNT: %u\n", (unsigned int)timer_delay, MEMORY[0xFFFFFFFFFFFFF390]);
v1 = timer_delay;
if ( timer_delay >= v0 )
return v1 - v0;
panic("ASSERT FAILED: src/arch/x86_64/interrupts/apic.c:156");
return timer_delay - v0;
}

canonical_decompiled_code:
__int64 func7()
{
unsigned int v0; // ebx
int v1; // eax

v0 = MEMORY[0xFFFFFFFFFFFFF390];
v1 = timer_delay;
if ( (unsigned int)timer_delay >= MEMORY[0xFFFFFFFFFFFFF390] )
return v1 - v0;
kprintf("PTC: %u - COUNT: %u\n", (unsigned int)timer_delay, MEMORY[0xFFFFFFFFFFFFF390]);
v1 = timer_delay;
if ( timer_delay >= v0 )
return v1 - v0;
panic("ASSERT FAILED: src/arch/x86_64/interrupts/apic.c:156");
return timer_delay - v0;
}

original_code:
time_t timer_get_elapsed()
{
 volatile uint32_t* lapic = (volatile uint32_t*)0xFFFFFFFFFFFFF000;
 const uint32_t count = lapic[(0x390/sizeof(uint32_t))];
 if (count > timer_delay)
 {
  kprintf("PTC: %u - COUNT: %u\n", timer_delay, count);
 }
 if (!(timer_delay >= count)) panic("ASSERT FAILED: " "src/arch/x86_64/interrupts/apic.c" ":" "156");
 return timer_delay - count;
}

canonical_original_code:
unsigned int timer_get_elapsed()
{
 unsigned int *lapic = (volatile uint32_t*)0xFFFFFFFFFFFFF000;
 unsigned int count = lapic[(0x390/sizeof(uint32_t))];
 if (count > timer_delay)
 {
  kprintf("PTC: %u - COUNT: %u\n", timer_delay, count);
 }
 if (!(timer_delay >= count)) panic("ASSERT FAILED: " "src/arch/x86_64/interrupts/apic.c" ":" "156");
 return timer_delay - count;
}

variable_types:
count
unsigned int


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

result = (unsigned int)timer_delay;
MEMORY[0xFFFFFFFFFFFFF380] = timer_delay;
return result;
}

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

result = (unsigned int)timer_delay;
MEMORY[0xFFFFFFFFFFFFF380] = timer_delay;
return result;
}

original_code:
void timer_start()
{
 volatile uint32_t* lapic = (volatile uint32_t*)0xFFFFFFFFFFFFF000;
 lapic[(0x380/sizeof(uint32_t))] = timer_delay;
}

canonical_original_code:
void timer_start()
{
 unsigned int *lapic = (volatile uint32_t*)0xFFFFFFFFFFFFF000;
 lapic[(0x380/sizeof(unsigned int))] = timer_delay;
}


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

result = MEMORY[0xFFFFFFFFFFFFF390];
MEMORY[0xFFFFFFFFFFFFF380] = MEMORY[0xFFFFFFFFFFFFF390];
return result;
}

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

result = MEMORY[0xFFFFFFFFFFFFF390];
MEMORY[0xFFFFFFFFFFFFF380] = MEMORY[0xFFFFFFFFFFFFF390];
return result;
}

original_code:
void timer_resume()
{
 volatile uint32_t* lapic = (volatile uint32_t*)0xFFFFFFFFFFFFF000;
 lapic[(0x380/sizeof(uint32_t))] = lapic[(0x390/sizeof(uint32_t))];
}

canonical_original_code:
void timer_resume()
{
 unsigned int *lapic = (volatile uint32_t*)0xFFFFFFFFFFFFF000;
 lapic[(0x380/sizeof(unsigned int))] = lapic[(0x390/sizeof(unsigned int))];
}


decompiled_code:
void timer_stop()
{
MEMORY[0xFFFFFFFFFFFFF380] = 0;
}

canonical_decompiled_code:
void func10()
{
MEMORY[0xFFFFFFFFFFFFF380] = 0;
}

original_code:
void timer_stop()
{
 volatile uint32_t* lapic = (volatile uint32_t*)0xFFFFFFFFFFFFF000;
 lapic[(0x380/sizeof(uint32_t))] = 0;
}

canonical_original_code:
void timer_stop()
{
 unsigned int *lapic = (volatile uint32_t*)0xFFFFFFFFFFFFF000;
 lapic[(0x380/sizeof(unsigned int))] = 0;
}


decompiled_code:
void apic_eoi()
{
MEMORY[0xFFFFFFFFFFFFF0B0] = 0;
}

canonical_decompiled_code:
void func11()
{
MEMORY[0xFFFFFFFFFFFFF0B0] = 0;
}

original_code:
void apic_eoi(void)
{
 volatile uint32_t* lapic = (volatile uint32_t*)0xFFFFFFFFFFFFF000;
 lapic[(0xB0/sizeof(uint32_t))] = 0;
}

canonical_original_code:
void apic_eoi()
{
 unsigned int *lapic = (volatile uint32_t*)0xFFFFFFFFFFFFF000;
 lapic[(0xB0/sizeof(unsigned int))] = 0;
}


decompiled_code:
void timer_handler()
{
timer_interrupt();
apic_eoi();
}

canonical_decompiled_code:
void func12()
{
timer_interrupt();
func11();
}

original_code:
void timer_handler(uint64_t vector, uint64_t code)
{
 ((void)vector);
 ((void)code);
 extern void timer_interrupt(void);
 timer_interrupt();
 apic_eoi();
}

canonical_original_code:
void timer_handler(unsigned long vector, unsigned long code)
{
 ((void)vector);
 ((void)code);
 extern void timer_interrupt();
 timer_interrupt();
 apic_eoi();
}

variable_types:
vector
unsigned long

variable_types:
code
unsigned long


decompiled_code:
__int64 apic_init()
{
__int64 v0; // rbx
unsigned __int8 v1; // al
unsigned __int8 v2; // al
unsigned __int8 v3; // al
int v4; // ebx

pic_init();
if ( !(unsigned __int8)check_apic() )
panic("Processor does not have local APIC!");
v0 = (unsigned int)apic_base_address();
kprintf("APIC Location: 0x%x \n", v0);
if ( !(_DWORD)v0 )
panic("No APIC present! \n");
if ( !(unsigned __int8)virt_map_phys(kernel_table, -4096LL, (unsigned int)v0, 26LL, 1LL) )
panic("Failed APIC virtual mapping \n");
kprintf("TIMER VEC: 0x%x \n", MEMORY[0xFFFFFFFFFFFFF320]);
MEMORY[0xFFFFFFFFFFFFF320] = 0x10000;
MEMORY[0xFFFFFFFFFFFFF340] = 1024;
MEMORY[0xFFFFFFFFFFFFF350] = 0x10000;
MEMORY[0xFFFFFFFFFFFFF360] = 0x10000;
MEMORY[0xFFFFFFFFFFFFF080] = 0;
enable_apic();
MEMORY[0xFFFFFFFFFFFFF0F0] = 295;
MEMORY[0xFFFFFFFFFFFFF320] = 32;
MEMORY[0xFFFFFFFFFFFFF3E0] = 10;
v1 = __inbyte(0x61u);
__outbyte(0x61u, v1 & 0xFC | 1);
__outbyte(0x43u, 0xB2u);
__outbyte(0x42u, 0x9Cu);
__outbyte(0x42u, 0x2Eu);
v2 = __inbyte(0x61u);
v2 &= 0xFEu;
__outbyte(0x61u, v2);
__outbyte(0x61u, v2 | 1);
MEMORY[0xFFFFFFFFFFFFF380] = -1;
do
v3 = __inbyte(0x61u);
while ( (v3 & 0x20) == 0 );
MEMORY[0xFFFFFFFFFFFFF380] = 0;
v4 = ~MEMORY[0xFFFFFFFFFFFFF390];
kprintf("Timer Count: 0x%x\n", MEMORY[0xFFFFFFFFFFFFF390]);
tsc_per_sec = 12800 * v4;
__outbyte(0xA1u, 0xFFu);
__outbyte(0x21u, 0xFDu);
MEMORY[0xFFFFFFFFFFFFF350] = 34560;
return interrupts_install_isr(32LL, timer_handler);
}

canonical_decompiled_code:
__int64 func13()
{
__int64 v0; // rbx
unsigned __int8 v1; // al
unsigned __int8 v2; // al
unsigned __int8 v3; // al
int v4; // ebx

func3();
if ( !(unsigned __int8)func0() )
panic("Processor does not have local APIC!");
v0 = (unsigned int)func2();
kprintf("APIC Location: 0x%x \n", v0);
if ( !(_DWORD)v0 )
panic("No APIC present! \n");
if ( !(unsigned __int8)virt_map_phys(kernel_table, -4096LL, (unsigned int)v0, 26LL, 1LL) )
panic("Failed APIC virtual mapping \n");
kprintf("TIMER VEC: 0x%x \n", MEMORY[0xFFFFFFFFFFFFF320]);
MEMORY[0xFFFFFFFFFFFFF320] = 0x10000;
MEMORY[0xFFFFFFFFFFFFF340] = 1024;
MEMORY[0xFFFFFFFFFFFFF350] = 0x10000;
MEMORY[0xFFFFFFFFFFFFF360] = 0x10000;
MEMORY[0xFFFFFFFFFFFFF080] = 0;
func1();
MEMORY[0xFFFFFFFFFFFFF0F0] = 295;
MEMORY[0xFFFFFFFFFFFFF320] = 32;
MEMORY[0xFFFFFFFFFFFFF3E0] = 10;
v1 = __inbyte(0x61u);
__outbyte(0x61u, v1 & 0xFC | 1);
__outbyte(0x43u, 0xB2u);
__outbyte(0x42u, 0x9Cu);
__outbyte(0x42u, 0x2Eu);
v2 = __inbyte(0x61u);
v2 &= 0xFEu;
__outbyte(0x61u, v2);
__outbyte(0x61u, v2 | 1);
MEMORY[0xFFFFFFFFFFFFF380] = -1;
do
v3 = __inbyte(0x61u);
while ( (v3 & 0x20) == 0 );
MEMORY[0xFFFFFFFFFFFFF380] = 0;
v4 = ~MEMORY[0xFFFFFFFFFFFFF390];
kprintf("Timer Count: 0x%x\n", MEMORY[0xFFFFFFFFFFFFF390]);
tsc_per_sec = 12800 * v4;
__outbyte(0xA1u, 0xFFu);
__outbyte(0x21u, 0xFDu);
MEMORY[0xFFFFFFFFFFFFF350] = 34560;
return interrupts_install_isr(32LL, timer_handler);
}

original_code:
void apic_init()
{
 pic_init();
 if (!check_apic())
 {
  panic("Processor does not have local APIC!");
 }
 const uint32_t apic_location = apic_base_address();
 kprintf("APIC Location: 0x%x \n", apic_location);
 if (apic_location == 0)
 {
  panic("No APIC present! \n");
 }
 if (!virt_map_phys(kernel_table, 0xFFFFFFFFFFFFF000, apic_location,
    0x2 | 0x8 | 0x10, 0x1))
 {
  panic("Failed APIC virtual mapping \n");
 }
 volatile uint32_t* apic_regs = (volatile uint32_t*)0xFFFFFFFFFFFFF000;
 kprintf("TIMER VEC: 0x%x \n", apic_regs[(0x320/sizeof(uint32_t))]);
 apic_regs[(0x320/sizeof(uint32_t))] = (0x10000);
 apic_regs[(0x340/sizeof(uint32_t))] = (0x400);
 apic_regs[(0x350/sizeof(uint32_t))] = (0x10000);
 apic_regs[(0x360/sizeof(uint32_t))] = (0x10000);
 apic_regs[(0x80/sizeof(uint32_t))] = 0;
 enable_apic();
 apic_regs[(0xF0/sizeof(uint32_t))] = 39 | (0x100);
 apic_regs[(0x320/sizeof(uint32_t))] = 32;
 apic_regs[(0x3E0/sizeof(uint32_t))] = 0xA;
 _outb(0x61, (_inb(0x61) & 0xFD) | 1);
 _outb(0x43, 0x80 | 0x2 | (0x3 << 4));
 _outb(0x42, 0x9C);
 _outb(0x42, 0x2E);
 const uint8_t tmp = _inb(0x61) & 0xFE;
 _outb(0x61, tmp);
 _outb(0x61, tmp|1);
 apic_regs[(0x380/sizeof(uint32_t))] = 0xFFFFFFFF;
 while (!(_inb(0x61) & 0x20));
 const uint32_t count = apic_regs[(0x390/sizeof(uint32_t))];
 apic_regs[(0x380/sizeof(uint32_t))] = 0;
 kprintf("Timer Count: 0x%x\n", count);
 const uint32_t diff = 0xFFFFFFFF - count;
 tsc_per_sec = diff*100*128;
 _outb(0xA1, 0xFF);
 _outb(0x21, 0xFD);
 apic_regs[(0x350/sizeof(uint32_t))] = 0x8700;
 interrupts_install_isr(32, timer_handler);
}

canonical_original_code:
void apic_init()
{
 pic_init();
 if (!check_apic())
 {
  panic("Processor does not have local APIC!");
 }
 unsigned int apic_location = apic_base_address();
 kprintf("APIC Location: 0x%x \n", apic_location);
 if (apic_location == 0)
 {
  panic("No APIC present! \n");
 }
 if (!virt_map_phys(kernel_table, 0xFFFFFFFFFFFFF000, apic_location,
    0x2 | 0x8 | 0x10, 0x1))
 {
  panic("Failed APIC virtual mapping \n");
 }
 unsigned int *apic_regs = (volatile uint32_t*)0xFFFFFFFFFFFFF000;
 kprintf("TIMER VEC: 0x%x \n", apic_regs[(0x320/sizeof(unsigned int))]);
 apic_regs[(0x320/sizeof(unsigned int))] = (0x10000);
 apic_regs[(0x340/sizeof(unsigned int))] = (0x400);
 apic_regs[(0x350/sizeof(unsigned int))] = (0x10000);
 apic_regs[(0x360/sizeof(unsigned int))] = (0x10000);
 apic_regs[(0x80/sizeof(unsigned int))] = 0;
 enable_apic();
 apic_regs[(0xF0/sizeof(unsigned int))] = 39 | (0x100);
 apic_regs[(0x320/sizeof(unsigned int))] = 32;
 apic_regs[(0x3E0/sizeof(unsigned int))] = 0xA;
 _outb(0x61, (_inb(0x61) & 0xFD) | 1);
 _outb(0x43, 0x80 | 0x2 | (0x3 << 4));
 _outb(0x42, 0x9C);
 _outb(0x42, 0x2E);
 unsigned char tmp = _inb(0x61) & 0xFE;
 _outb(0x61, tmp);
 _outb(0x61, tmp|1);
 apic_regs[(0x380/sizeof(unsigned int))] = 0xFFFFFFFF;
 while (!(_inb(0x61) & 0x20));
 unsigned int count = apic_regs[(0x390/sizeof(uint32_t))];
 apic_regs[(0x380/sizeof(unsigned int))] = 0;
 kprintf("Timer Count: 0x%x\n", count);
 unsigned int diff = 0xFFFFFFFF - count;
 tsc_per_sec = diff*100*128;
 _outb(0xA1, 0xFF);
 _outb(0x21, 0xFD);
 apic_regs[(0x350/sizeof(unsigned int))] = 0x8700;
 interrupts_install_isr(32, timer_handler);
}

variable_types:
tmp
unsigned char

variable_types:
diff
unsigned int


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

result = a1;
__writecr3(a1);
return result;
}

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

result = a1;
__writecr3(a1);
return result;
}

original_code:
static inline
void virt_switch_page_table(void* page_table)
{
 __asm__ volatile("movq %%rax, %%cr3" : : "a"((uint64_t)page_table));
}

canonical_original_code:
static inline void virt_switch_page_table(void *page_table)
{
 __asm__ volatile("movq %%rax, %%cr3" : : "a"((unsigned long)page_table));
}


decompiled_code:
__int64 __fastcall sleep_insert(__int64 a1, __int64 a2)
{
unsigned int v2; // eax
unsigned int v3; // edx
unsigned int v4; // edx
__int64 result; // rax

v2 = *(_DWORD *)(a1 + 16);
v3 = *(_DWORD *)(a2 + 16);
if ( v2 > v3 )
{
*(_DWORD *)(a1 + 16) = v2 - v3;
result = 1LL;
}
else
{
v4 = v3 - v2;
result = 0xFFFFFFFFLL;
*(_DWORD *)(a2 + 16) = v4;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
unsigned int v2; // eax
unsigned int v3; // edx
unsigned int v4; // edx
__int64 result; // rax

v2 = *(_DWORD *)(a1 + 16);
v3 = *(_DWORD *)(a2 + 16);
if ( v2 > v3 )
{
*(_DWORD *)(a1 + 16) = v2 - v3;
result = 1LL;
}
else
{
v4 = v3 - v2;
result = 0xFFFFFFFFLL;
*(_DWORD *)(a2 + 16) = v4;
}
return result;
}

original_code:
int8_t sleep_insert(const void* d1, const void* d2)
{
 PCB* pcb1 = (PCB*)d1;
 PCB* pcb2 = (PCB*)d2;
 if (pcb1->sleep_time <= pcb2->sleep_time)
 {
  pcb2->sleep_time -= pcb1->sleep_time;
  return -1;
 }
 else
 {
  pcb1->sleep_time -= pcb2->sleep_time;
  return 1;
 }
}

canonical_original_code:
char sleep_insert(void *d1, void *d2)
{
 struct PCB *pcb1 = (PCB*)d1;
 struct PCB *pcb2 = (PCB*)d2;
 if (pcb1->sleep_time <= pcb2->sleep_time)
 {
  pcb2->sleep_time -= pcb1->sleep_time;
  return -1;
 }
 else
 {
  pcb1->sleep_time -= pcb2->sleep_time;
  return 1;
 }
}

variable_types: struct PCB { struct Context { unsigned long rdi; unsigned long rsi; unsigned long rdx; unsigned long rcx; unsigned long rbx; unsigned long rax; unsigned long rbp; unsigned long r8; unsigned long r9; unsigned long r10; unsigned long r11; unsigned long r12; unsigned long r13; unsigned long r14; unsigned long r15; unsigned long vector; unsigned long error_code; unsigned long rip; unsigned long cs; unsigned long rflags; unsigned long rsp; unsigned long ss; } *context; void *page_table; unsigned int sleep_time; unsigned int pid; unsigned int ppid; enum State { READY=0, RUNNING=1, SLEEPING=2, KILLED=3 } state; enum Priority { HIGH=0, NORMAL=1, LOW=2, IDLE=3 } priority; } *

return_type: char

user_defined_types: struct PCB { struct Context *context; void *page_table; unsigned int sleep_time; unsigned int pid; unsigned int ppid; enum State state; enum Priority priority; }


decompiled_code:
__int64 scheduler_init()
{
__int64 v0; // rax
__int64 v1; // rax

v0 = water_mark_alloc(&kernel_WaterMark, 41008LL);
ba_pcbs = block_init(v0, 41008LL, 40LL);
v1 = water_mark_alloc(&kernel_WaterMark, 32784LL);
ba_qnodes = block_init(v1, 32784LL, 16LL);
queue_init(&queues);
queue_init(&unk_8B8);
queue_init(&unk_8D0);
queue_init(&unk_8E8);
return queue_init(&sleep_queue);
}

canonical_decompiled_code:
__int64 func3()
{
__int64 v0; // rax
__int64 v1; // rax

v0 = water_mark_alloc(&kernel_WaterMark, 41008LL);
ba_pcbs = block_init(v0, 41008LL, 40LL);
v1 = water_mark_alloc(&kernel_WaterMark, 32784LL);
ba_qnodes = block_init(v1, 32784LL, 16LL);
queue_init(&queues);
queue_init(&unk_8B8);
queue_init(&unk_8D0);
queue_init(&unk_8E8);
return queue_init(&sleep_queue);
}

original_code:
void scheduler_init()
{
 const uint64_t pcb_size_needed = sizeof(PCB)*1024 + sizeof(BlockAllocator);
 const void* ba_address = water_mark_alloc(&kernel_WaterMark, pcb_size_needed);
 ba_pcbs = block_init(ba_address, pcb_size_needed, sizeof(PCB));
 const uint64_t queue_node_size_needed = sizeof(QueueNode)*2048 + sizeof(QueueNode);
 const void* qn_address = water_mark_alloc(&kernel_WaterMark, queue_node_size_needed);
 ba_qnodes = block_init(qn_address, queue_node_size_needed, sizeof(QueueNode));
 for (uint64_t i = 0; i < 4; ++i)
 {
  queue_init(&queues[i]);
 }
 queue_init(&sleep_queue);
}

canonical_original_code:
void scheduler_init()
{
 unsigned long pcb_size_needed = sizeof(PCB)*1024 + sizeof(BlockAllocator);
 void *ba_address = water_mark_alloc(&kernel_WaterMark, pcb_size_needed);
 ba_pcbs = block_init(ba_address, pcb_size_needed, sizeof(PCB));
 unsigned long queue_node_size_needed = sizeof(QueueNode)*2048 + sizeof(QueueNode);
 void *qn_address = water_mark_alloc(&kernel_WaterMark, queue_node_size_needed);
 ba_qnodes = block_init(qn_address, queue_node_size_needed, sizeof(QueueNode));
 for (unsigned long i = 0; i < 4; ++i)
 {
  queue_init(&queues[i]);
 }
 queue_init(&sleep_queue);
}

variable_types:
i
unsigned long


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

result = block_alloc(ba_pcbs);
if ( !result )
{
panic("Failed to allocate first PCB");
result = 0LL;
}
*(_QWORD *)(result + 28) = 0x100000000LL;
return result;
}

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

result = block_alloc(ba_pcbs);
if ( !result )
{
panic("Failed to allocate first PCB");
result = 0LL;
}
*(_QWORD *)(result + 28) = 0x100000000LL;
return result;
}

original_code:
PCB* alloc_pcb()
{
 PCB* pcb = (PCB*) block_alloc(ba_pcbs);
 if (pcb == ((void*)0))
 {
  panic("Failed to allocate first PCB");
 }
 ;
 pcb->state = READY;
 pcb->priority = NORMAL;
 return pcb;
}

canonical_original_code:
struct PCB *alloc_pcb()
{
 struct PCB *pcb = (PCB*) block_alloc(ba_pcbs);
 if (pcb == ((void *)0))
 {
  panic("Failed to allocate first PCB");
 }
 ;
 pcb->state = READY;
 pcb->priority = NORMAL;
 return pcb;
}

return_type: struct PCB { struct Context { unsigned long rdi; unsigned long rsi; unsigned long rdx; unsigned long rcx; unsigned long rbx; unsigned long rax; unsigned long rbp; unsigned long r8; unsigned long r9; unsigned long r10; unsigned long r11; unsigned long r12; unsigned long r13; unsigned long r14; unsigned long r15; unsigned long vector; unsigned long error_code; unsigned long rip; unsigned long cs; unsigned long rflags; unsigned long rsp; unsigned long ss; } *context; void *page_table; unsigned int sleep_time; unsigned int pid; unsigned int ppid; enum State { READY=0, RUNNING=1, SLEEPING=2, KILLED=3 } state; enum Priority { HIGH=0, NORMAL=1, LOW=2, IDLE=3 } priority; } *


decompiled_code:
__int64 __fastcall free_pcb(__int64 a1)
{
return block_free(ba_pcbs, a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
return block_free(ba_pcbs, a1);
}

original_code:
void free_pcb(PCB* pcb)
{
 block_free(ba_pcbs, pcb);
}

canonical_original_code:
void free_pcb(struct PCB *pcb)
{
 block_free(ba_pcbs, pcb);
}

user_defined_types: enum State { READY=0, RUNNING=1, SLEEPING=2, KILLED=3 }

user_defined_types: enum Priority { HIGH=0, NORMAL=1, LOW=2, IDLE=3 }

user_defined_types: struct Context { unsigned long rdi; unsigned long rsi; unsigned long rdx; unsigned long rcx; unsigned long rbx; unsigned long rax; unsigned long rbp; unsigned long r8; unsigned long r9; unsigned long r10; unsigned long r11; unsigned long r12; unsigned long r13; unsigned long r14; unsigned long r15; unsigned long vector; unsigned long error_code; unsigned long rip; unsigned long cs; unsigned long rflags; unsigned long rsp; unsigned long ss; }


decompiled_code:
__int64 create_init_process()
{
unsigned __int64 v0; // rdx
__int64 v1; // rax
int v2; // eax

current_pcb = alloc_pcb();
v0 = virt_clone_mapping(kernel_table);
v1 = current_pcb;
*(_QWORD *)(current_pcb + 8LL) = v0;
*(_DWORD *)(v1 + 28) = 0;
virt_switch_page_table(v0);
if ( (unsigned __int8)elf_create_process(current_pcb, (char *)&_KERNEL_END - 0x800000000000LL) )
panic("Failed to load init process!");
v2 = timer_one_ms();
quantum_left = 10;
prev_ticks = 10;
one_ms = v2;
timer_set_delay((unsigned int)(10 * v2));
timer_start();
tss_set_context_stack(33562624LL);
return isr_restore();
}

canonical_decompiled_code:
__int64 func6()
{
unsigned __int64 v0; // rdx
__int64 v1; // rax
int v2; // eax

current_pcb = func4();
v0 = virt_clone_mapping(kernel_table);
v1 = current_pcb;
*(_QWORD *)(current_pcb + 8LL) = v0;
*(_DWORD *)(v1 + 28) = 0;
func0(v0);
if ( (unsigned __int8)elf_create_process(current_pcb, (char *)&_KERNEL_END - 0x800000000000LL) )
panic("Failed to load init process!");
v2 = timer_one_ms();
quantum_left = 10;
prev_ticks = 10;
one_ms = v2;
timer_set_delay((unsigned int)(10 * v2));
timer_start();
tss_set_context_stack(33562624LL);
return isr_restore();
}

original_code:
void create_init_process()
{
 extern uint64_t __KERNEL_END;
 const uint64_t init_location = (const uint64_t)((void*)((uint64_t)(&__KERNEL_END) + 0xFFFF800000000000));
 current_pcb = alloc_pcb();
 ;
 ;
 void* new_page_table = virt_clone_mapping(kernel_table);
 current_pcb->page_table = new_page_table;
 current_pcb->state = READY;
 virt_switch_page_table(new_page_table);
 uint64_t elf_error = 0;
 if ((elf_error = elf_create_process(current_pcb,
    (void*)init_location, new_page_table)) != 0)
 {
  ;
  panic("Failed to load init process!");
 }
 one_ms = timer_one_ms();
 ten_ms = one_ms * 10;
 ;
 prev_ticks = quantum_left = 10;
 timer_set_delay(quantum_left*one_ms);
 timer_start();
 tss_set_context_stack((0x2000000 +0x1000)+0x1000);
 __asm__ volatile("jmp isr_restore");
}

canonical_original_code:
void create_init_process()
{
 extern unsigned long __KERNEL_END;
 unsigned long init_location = (const uint64_t)((void*)((uint64_t)(&__KERNEL_END) + 0xFFFF800000000000));
 current_pcb = alloc_pcb();
 ;
 ;
 void *new_page_table = virt_clone_mapping(kernel_table);
 current_pcb->page_table = new_page_table;
 current_pcb->state = READY;
 virt_switch_page_table(new_page_table);
 unsigned long elf_error = 0;
 if ((elf_error = elf_create_process(current_pcb,
    (void *)init_location, new_page_table)) != 0)
 {
  ;
  panic("Failed to load init process!");
 }
 one_ms = timer_one_ms();
 ten_ms = one_ms * 10;
 ;
 prev_ticks = quantum_left = 10;
 timer_set_delay(quantum_left*one_ms);
 timer_start();
 tss_set_context_stack((0x2000000 +0x1000)+0x1000);
 __asm__ volatile("jmp isr_restore");
}


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

virt_switch_page_table(kernel_table);
result = virt_cleanup_table(*(_QWORD *)(a1 + 8));
*(_DWORD *)(a1 + 28) = 3;
return result;
}

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

func0(kernel_table);
result = virt_cleanup_table(*(_QWORD *)(a1 + 8));
*(_DWORD *)(a1 + 28) = 3;
return result;
}

original_code:
void cleanup_pcb(PCB* pcb)
{
 virt_switch_page_table(kernel_table);
 virt_cleanup_table(pcb->page_table);
 pcb->state = KILLED;
}

canonical_original_code:
void cleanup_pcb(struct PCB *pcb)
{
 virt_switch_page_table(kernel_table);
 virt_cleanup_table(pcb->page_table);
 pcb->state = KILLED;
}


schedule

decompiled_code:
__int64 __fastcall schedule(__int64 a1)
{
int v1; // eax
__int64 result; // rax
__int64 v3; // rbp
__int64 v4; // rax
__int64 v5; // rsi

v1 = *(_DWORD *)(a1 + 28);
if ( v1 )
{
if ( v1 == 2 )
{
v5 = block_alloc(ba_qnodes);
if ( !v5 )
{
panic("ASSERT FAILED: src/kernel/scheduler/scheduler.c:168");
v5 = 0LL;
}
*(_QWORD *)(v5 + 8) = a1;
queue_enqueue_prio(&sleep_queue, v5, sleep_insert);
result = 1LL;
}
else
{
panic("Scheduler: Unhandled PCB state");
result = 1LL;
}
}
else
{
v3 = block_alloc(ba_qnodes);
if ( !v3 )
panic("ASSERT FAILED: src/kernel/scheduler/scheduler.c:158");
v4 = *(unsigned int *)(a1 + 32);
if ( (unsigned int)v4 > 3 )
{
panic("ASSERT FAILED: src/kernel/scheduler/scheduler.c:159");
v4 = *(unsigned int *)(a1 + 32);
}
*(_QWORD *)(v3 + 8) = a1;
queue_enqueue((char *)&queues + 24 * v4, v3);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1)
{
int v1; // eax
__int64 result; // rax
__int64 v3; // rbp
__int64 v4; // rax
__int64 v5; // rsi

v1 = *(_DWORD *)(a1 + 28);
if ( v1 )
{
if ( v1 == 2 )
{
v5 = block_alloc(ba_qnodes);
if ( !v5 )
{
panic("ASSERT FAILED: src/kernel/scheduler/scheduler.c:168");
v5 = 0LL;
}
*(_QWORD *)(v5 + 8) = a1;
queue_enqueue_prio(&sleep_queue, v5, sleep_insert);
result = 1LL;
}
else
{
panic("Scheduler: Unhandled PCB state");
result = 1LL;
}
}
else
{
v3 = block_alloc(ba_qnodes);
if ( !v3 )
panic("ASSERT FAILED: src/kernel/scheduler/scheduler.c:158");
v4 = *(unsigned int *)(a1 + 32);
if ( (unsigned int)v4 > 3 )
{
panic("ASSERT FAILED: src/kernel/scheduler/scheduler.c:159");
v4 = *(unsigned int *)(a1 + 32);
}
*(_QWORD *)(v3 + 8) = a1;
queue_enqueue((char *)&queues + 24 * v4, v3);
result = 1LL;
}
return result;
}

original_code:
uint8_t schedule(PCB* pcb)
{
 switch (pcb->state)
 {
  case READY:
   {
    QueueNode* node = (QueueNode*)block_alloc(ba_qnodes);
    if (!(node != ((void*)0))) panic("ASSERT FAILED: " "src/kernel/scheduler/scheduler.c" ":" "158");
    if (!(pcb->priority < 4)) panic("ASSERT FAILED: " "src/kernel/scheduler/scheduler.c" ":" "159");
    node->data = pcb;
    queue_enqueue(&queues[pcb->priority], node);
   }
   break;
  case SLEEPING:
   {
    QueueNode* node = (QueueNode*)block_alloc(ba_qnodes);
    if (!(node != ((void*)0))) panic("ASSERT FAILED: " "src/kernel/scheduler/scheduler.c" ":" "168");
    node->data = pcb;
    queue_enqueue_prio(&sleep_queue, node, sleep_insert);
   }
   break;
  default:
   {
    panic("Scheduler: Unhandled PCB state");
   }
   break;
 }
 return 1;
}

canonical_original_code:
unsigned char schedule(struct PCB *pcb)
{
 switch (pcb->state)
 {
  case READY:
   {
    struct _QueueNode *node = (QueueNode*)block_alloc(ba_qnodes);
    if (!(node != ((void *)0))) panic("ASSERT FAILED: " "src/kernel/scheduler/scheduler.c" ":" "158");
    if (!(pcb->priority < 4)) panic("ASSERT FAILED: " "src/kernel/scheduler/scheduler.c" ":" "159");
    node->data = pcb;
    queue_enqueue(&queues[pcb->priority], node);
   }
   break;
  case SLEEPING:
   {
    struct _QueueNode *node = (QueueNode*)block_alloc(ba_qnodes);
    if (!(node != ((void *)0))) panic("ASSERT FAILED: " "src/kernel/scheduler/scheduler.c" ":" "168");
    node->data = pcb;
    queue_enqueue_prio(&sleep_queue, node, sleep_insert);
   }
   break;
  default:
   {
    panic("Scheduler: Unhandled PCB state");
   }
   break;
 }
 return 1;
}

variable_types: struct _QueueNode { struct _QueueNode *next; void *data; } *


decompiled_code:
__int64 __fastcall get_next_sleep(unsigned int a1)
{
__int64 v2; // rdi
unsigned int v3; // eax
__int64 result; // rax
__int64 v5; // rsi

if ( (unsigned __int8)queue_empty(&sleep_queue) )
goto LABEL_5;
v2 = *(_QWORD *)(queue_peek_constprop_0() + 8);
v3 = *(_DWORD *)(v2 + 16);
if ( v3 > a1 )
{
result = v3 - a1;
*(_DWORD *)(v2 + 16) = result;
return result;
}
*(_DWORD *)(v2 + 28) = 0;
*(_DWORD *)(v2 + 16) = 0;
schedule(v2);
v5 = queue_dequeue(&sleep_queue);
block_free(ba_qnodes, v5);
if ( (unsigned __int8)queue_empty(&sleep_queue) )
LABEL_5:
result = 0LL;
else
result = *(unsigned int *)(*(_QWORD *)(queue_peek_constprop_0() + 8) + 16LL);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int a1)
{
__int64 v2; // rdi
unsigned int v3; // eax
__int64 result; // rax
__int64 v5; // rsi

if ( (unsigned __int8)queue_empty(&sleep_queue) )
goto LABEL_5;
v2 = *(_QWORD *)(func2() + 8);
v3 = *(_DWORD *)(v2 + 16);
if ( v3 > a1 )
{
result = v3 - a1;
*(_DWORD *)(v2 + 16) = result;
return result;
}
*(_DWORD *)(v2 + 28) = 0;
*(_DWORD *)(v2 + 16) = 0;
func8(v2);
v5 = queue_dequeue(&sleep_queue);
block_free(ba_qnodes, v5);
if ( (unsigned __int8)queue_empty(&sleep_queue) )
LABEL_5:
result = 0LL;
else
result = *(unsigned int *)(*(_QWORD *)(func2() + 8) + 16LL);
return result;
}

original_code:
uint32_t get_next_sleep(const uint32_t tick_span)
{
 if (queue_empty(&sleep_queue))
 {
  return 0;
 }
 QueueNode* peek = queue_peek(&sleep_queue);
 PCB* pcb = (PCB*)peek->data;
 if (pcb->sleep_time <= tick_span)
 {
  pcb->state = READY;
  pcb->sleep_time = 0;
  schedule(pcb);
  block_free(ba_qnodes, queue_dequeue(&sleep_queue));
  if (!queue_empty(&sleep_queue))
  {
   peek = queue_peek(&sleep_queue);
   pcb = (PCB*)peek->data;
   return pcb->sleep_time;
  }
  return 0;
 }
 else
 {
  pcb->sleep_time -= tick_span;
  return pcb->sleep_time;
 }
}

canonical_original_code:
unsigned int get_next_sleep(unsigned int tick_span)
{
 if (queue_empty(&sleep_queue))
 {
  return 0;
 }
 struct _QueueNode *peek = queue_peek(&sleep_queue);
 struct PCB *pcb = (PCB*)peek->data;
 if (pcb->sleep_time <= tick_span)
 {
  pcb->state = READY;
  pcb->sleep_time = 0;
  schedule(pcb);
  block_free(ba_qnodes, queue_dequeue(&sleep_queue));
  if (!queue_empty(&sleep_queue))
  {
   peek = queue_peek(&sleep_queue);
   pcb = (struct PCB *)peek->data;
   return pcb->sleep_time;
  }
  return 0;
 }
 else
 {
  pcb->sleep_time -= tick_span;
  return pcb->sleep_time;
 }
}

user_defined_types: struct _QueueNode { struct _QueueNode *next; void *data; }


dispatch

decompiled_code:
__int64 dispatch()
{
unsigned int v0; // er13
__int64 v1; // rdi
int v2; // eax
unsigned int v3; // eax
__int64 v4; // rdi
int v6; // edx
__int64 *v7; // r13
__int64 v8; // rsi
__int64 v9; // r15
int v10; // eax
unsigned __int64 v11; // rdi

v0 = (unsigned int)timer_get_elapsed() / one_ms;
if ( quantum_left < v0 )
{
quantum_left = 0;
v1 = current_pcb;
if ( *(_DWORD *)(current_pcb + 28LL) != 3 )
{
LABEL_3:
schedule(v1);
LABEL_4:
current_pcb = 0LL;
quantum_left = 10;
v2 = 10;
goto LABEL_5;
}
LABEL_25:
cleanup_pcb(v1);
goto LABEL_4;
}
v2 = quantum_left - v0;
v1 = current_pcb;
quantum_left -= v0;
v6 = *(_DWORD *)(current_pcb + 28LL);
if ( v6 == 3 )
goto LABEL_25;
if ( v6 == 2 || !v2 )
goto LABEL_3;
LABEL_5:
prev_ticks = v2;
v3 = get_next_sleep(v0);
if ( v3 && quantum_left > v3 )
{
prev_ticks = v3;
if ( current_pcb )
{
LABEL_8:
v4 = one_ms * v3;
goto LABEL_9;
}
}
else
{
if ( current_pcb )
{
if ( !quantum_left )
panic("ASSERT FAILED: src/kernel/scheduler/scheduler.c:287");
v3 = prev_ticks;
goto LABEL_8;
}
if ( !prev_ticks )
panic("ASSERT FAILED: src/kernel/scheduler/scheduler.c:294");
}
v7 = (__int64 *)&queues;
LABEL_17:
while ( (unsigned __int8)queue_empty(v7) )
{
LABEL_23:
v7 += 3;
if ( v7 == &ba_qnodes )
return panic("Dispatch: Nothing left to run!");
}
while ( 1 )
{
v8 = queue_dequeue(v7);
if ( !v8 )
{
panic("ASSERT FAILED: src/kernel/scheduler/scheduler.c:302");
v8 = 0LL;
}
v9 = *(_QWORD *)(v8 + 8);
block_free(ba_qnodes, v8);
v10 = *(_DWORD *)(v9 + 28);
if ( !v10 )
break;
if ( v10 != 3 )
{
panic("Scheduler: Unhandled case!");
goto LABEL_17;
}
free_pcb(v9);
if ( (unsigned __int8)queue_empty(v7) )
goto LABEL_23;
}
v11 = *(_QWORD *)(v9 + 8);
current_pcb = v9;
virt_switch_page_table(v11);
v4 = (unsigned int)(one_ms * prev_ticks);
LABEL_9:
timer_set_delay(v4);
return timer_start();
}

canonical_decompiled_code:
__int64 func10()
{
unsigned int v0; // er13
__int64 v1; // rdi
int v2; // eax
unsigned int v3; // eax
__int64 v4; // rdi
int v6; // edx
__int64 *v7; // r13
__int64 v8; // rsi
__int64 v9; // r15
int v10; // eax
unsigned __int64 v11; // rdi

v0 = (unsigned int)timer_get_elapsed() / one_ms;
if ( quantum_left < v0 )
{
quantum_left = 0;
v1 = current_pcb;
if ( *(_DWORD *)(current_pcb + 28LL) != 3 )
{
LABEL_3:
func8(v1);
LABEL_4:
current_pcb = 0LL;
quantum_left = 10;
v2 = 10;
goto LABEL_5;
}
LABEL_25:
func7(v1);
goto LABEL_4;
}
v2 = quantum_left - v0;
v1 = current_pcb;
quantum_left -= v0;
v6 = *(_DWORD *)(current_pcb + 28LL);
if ( v6 == 3 )
goto LABEL_25;
if ( v6 == 2 || !v2 )
goto LABEL_3;
LABEL_5:
prev_ticks = v2;
v3 = func9(v0);
if ( v3 && quantum_left > v3 )
{
prev_ticks = v3;
if ( current_pcb )
{
LABEL_8:
v4 = one_ms * v3;
goto LABEL_9;
}
}
else
{
if ( current_pcb )
{
if ( !quantum_left )
panic("ASSERT FAILED: src/kernel/scheduler/scheduler.c:287");
v3 = prev_ticks;
goto LABEL_8;
}
if ( !prev_ticks )
panic("ASSERT FAILED: src/kernel/scheduler/scheduler.c:294");
}
v7 = (__int64 *)&queues;
LABEL_17:
while ( (unsigned __int8)queue_empty(v7) )
{
LABEL_23:
v7 += 3;
if ( v7 == &ba_qnodes )
return panic("Dispatch: Nothing left to run!");
}
while ( 1 )
{
v8 = queue_dequeue(v7);
if ( !v8 )
{
panic("ASSERT FAILED: src/kernel/scheduler/scheduler.c:302");
v8 = 0LL;
}
v9 = *(_QWORD *)(v8 + 8);
block_free(ba_qnodes, v8);
v10 = *(_DWORD *)(v9 + 28);
if ( !v10 )
break;
if ( v10 != 3 )
{
panic("Scheduler: Unhandled case!");
goto LABEL_17;
}
func5(v9);
if ( (unsigned __int8)queue_empty(v7) )
goto LABEL_23;
}
v11 = *(_QWORD *)(v9 + 8);
current_pcb = v9;
func0(v11);
v4 = (unsigned int)(one_ms * prev_ticks);
LABEL_9:
timer_set_delay(v4);
return timer_start();
}

original_code:
void dispatch()
{
 const uint32_t elapsed = (timer_get_elapsed() / one_ms);
 const uint32_t tick_span = elapsed;
 ;
 ;
 if (tick_span > quantum_left) { quantum_left = 0; }
 else { quantum_left -= tick_span; }
 ;
 if (current_pcb->state == KILLED)
 {
  cleanup_pcb(current_pcb);
  current_pcb = ((void*)0);
  quantum_left = 10;
 }
 else if (current_pcb->state == SLEEPING || quantum_left == 0)
 {
  if (current_pcb->state == SLEEPING) { ; }
  else { ; }
  schedule(current_pcb);
  current_pcb = ((void*)0);
  quantum_left = 10;
 }
 prev_ticks = quantum_left;
 const uint32_t next_wakeup = get_next_sleep(tick_span);
 if (next_wakeup > 0 && next_wakeup < quantum_left)
 {
  prev_ticks = next_wakeup;
 }
 if (current_pcb != ((void*)0))
 {
  ;
  if (!(quantum_left != 0)) panic("ASSERT FAILED: " "src/kernel/scheduler/scheduler.c" ":" "287");
  timer_set_delay(prev_ticks*one_ms);
  timer_start();
  return;
 }
 ;
 if (!(prev_ticks > 0)) panic("ASSERT FAILED: " "src/kernel/scheduler/scheduler.c" ":" "294");
 for (uint64_t i = 0; i < 4; ++i)
 {
  while (!queue_empty(&queues[i]))
  {
   QueueNode* node = queue_dequeue(&queues[i]);
   if (!(node != ((void*)0))) panic("ASSERT FAILED: " "src/kernel/scheduler/scheduler.c" ":" "302");
   PCB* next = (PCB*)node->data;
   block_free(ba_qnodes, node);
   switch (next->state)
   {
    case KILLED:
     free_pcb(next);
     continue;
    case READY:
     {
      ;
      current_pcb = next;
      virt_switch_page_table(current_pcb->page_table);
      timer_set_delay(prev_ticks*one_ms);
      timer_start();
     }
     return;
    default:
     panic("Scheduler: Unhandled case!");
     break;
   }
  }
 }
 panic("Dispatch: Nothing left to run!");
}

canonical_original_code:
void dispatch()
{
 unsigned int elapsed = (timer_get_elapsed() / one_ms);
 unsigned int tick_span = elapsed;
 ;
 ;
 if (tick_span > quantum_left) { quantum_left = 0; }
 else { quantum_left -= tick_span; }
 ;
 if (current_pcb->state == KILLED)
 {
  cleanup_pcb(current_pcb);
  current_pcb = ((void *)0);
  quantum_left = 10;
 }
 else if (current_pcb->state == SLEEPING || quantum_left == 0)
 {
  if (current_pcb->state == SLEEPING) { ; }
  else { ; }
  schedule(current_pcb);
  current_pcb = ((void *)0);
  quantum_left = 10;
 }
 prev_ticks = quantum_left;
 unsigned int next_wakeup = get_next_sleep(tick_span);
 if (next_wakeup > 0 && next_wakeup < quantum_left)
 {
  prev_ticks = next_wakeup;
 }
 if (current_pcb != ((void *)0))
 {
  ;
  if (!(quantum_left != 0)) panic("ASSERT FAILED: " "src/kernel/scheduler/scheduler.c" ":" "287");
  timer_set_delay(prev_ticks*one_ms);
  timer_start();
  return;
 }
 ;
 if (!(prev_ticks > 0)) panic("ASSERT FAILED: " "src/kernel/scheduler/scheduler.c" ":" "294");
 for (unsigned long i = 0; i < 4; ++i)
 {
  while (!queue_empty(&queues[i]))
  {
   struct _QueueNode *node = queue_dequeue(&queues[i]);
   if (!(node != ((void *)0))) panic("ASSERT FAILED: " "src/kernel/scheduler/scheduler.c" ":" "302");
   struct PCB *next = (PCB*)node->data;
   block_free(ba_qnodes, node);
   switch (next->state)
   {
    case KILLED:
     free_pcb(next);
     continue;
    case READY:
     {
      ;
      current_pcb = next;
      virt_switch_page_table(current_pcb->page_table);
      timer_set_delay(prev_ticks*one_ms);
      timer_start();
     }
     return;
    default:
     panic("Scheduler: Unhandled case!");
     break;
   }
  }
 }
 panic("Dispatch: Nothing left to run!");
}

variable_types:
elapsed
unsigned int


decompiled_code:
__int64 __fastcall sleep_pcb(__int64 a1, int a2)
{
if ( a2 )
{
*(_DWORD *)(a1 + 28) = 2;
*(_DWORD *)(a1 + 16) = a2;
}
return dispatch();
}

original_code:
void sleep_pcb(PCB* pcb, time_t time)
{
 if (time != 0)
 {
  ;
  pcb->state = SLEEPING;
  pcb->sleep_time = time;
  ;
 }
 dispatch();
}

canonical_original_code:
void sleep_pcb(struct PCB *pcb, unsigned int time)
{
 if (time != 0)
 {
  ;
  pcb->state = SLEEPING;
  pcb->sleep_time = time;
  ;
 }
 dispatch();
}

variable_types:
time
unsigned int


decompiled_code:
__int64 timer_interrupt()
{
return dispatch();
}

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

original_code:
void timer_interrupt()
{
 ;
 dispatch();
}


decompiled_code:
__int64 __fastcall fancy_display_char(char a1)
{
unsigned __int16 v2; // bx
unsigned __int16 v3; // ax
int v4; // edx
int v5; // ebx
unsigned __int64 v6; // rax
_BYTE *v7; // rsi
__int64 v8; // [rsp+0h] [rbp-48h]

if ( a1 != 32 )
{
v2 = text_get_y();
v3 = text_get_x();
v4 = v2;
v5 = 10;
v8 = 2LL * (v3 + 80 * v4);
do
{
LODWORD(v6) = rand();
v7 = (_BYTE *)(video + v8);
*v7 = aAbcdefghijklmn[v6 % 0x2C];
v7[1] = text_color;
msleep(20LL);
--v5;
}
while ( v5 );
}
return text_mode_char((unsigned int)a1);
}

canonical_decompiled_code:
__int64 __fastcall func0(char a1)
{
unsigned __int16 v2; // bx
unsigned __int16 v3; // ax
int v4; // edx
int v5; // ebx
unsigned __int64 v6; // rax
_BYTE *v7; // rsi
__int64 v8; // [rsp+0h] [rbp-48h]

if ( a1 != 32 )
{
v2 = text_get_y();
v3 = text_get_x();
v4 = v2;
v5 = 10;
v8 = 2LL * (v3 + 80 * v4);
do
{
LODWORD(v6) = rand();
v7 = (_BYTE *)(video + v8);
*v7 = aAbcdefghijklmn[v6 % 0x2C];
v7[1] = text_color;
msleep(20LL);
--v5;
}
while ( v5 );
}
return text_mode_char((unsigned int)a1);
}

original_code:
static void fancy_display_char(const char c)
{
 if (c != ' ')
 {
  const uint32_t offset = text_get_y()*80 + text_get_x();
  for (uint32_t i = 0; i < 10; ++i)
  {
   const uint32_t str_offset = rand() % 44;
   video[offset].c = str[str_offset];
   video[offset].color = text_color;
   msleep(20);
  }
 }
 text_mode_char(c);
}

canonical_original_code:
static void fancy_display_char(char c)
{
 if (c != ' ')
 {
  unsigned int offset = text_get_y()*80 + text_get_x();
  for (unsigned int i = 0; i < 10; ++i)
  {
   unsigned int str_offset = rand() % 44;
   video[offset].c = str[str_offset];
   video[offset].color = text_color;
   msleep(20);
  }
 }
 text_mode_char(c);
}

variable_types: c char

variable_types:
offset
unsigned int

variable_types:
i
unsigned int


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

v1 = a1;
for ( i = *a1; i; ++v1 )
{
result = write_char(i);
i = v1[1];
}
return result;
}

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

v1 = a1;
for ( i = *a1; i; ++v1 )
{
result = func1(i);
i = v1[1];
}
return result;
}

original_code:
static void write_string(const char* str)
{
 while (*str)
 {
  char c = *str;
  ++str;
  write_char(c);
 }
}

canonical_original_code:
static void write_string(char *str)
{
 while (*str)
 {
  char c = *str;
  ++str;
  write_char(c);
 }
}

variable_types:
str
char *


decompiled_code:
__int64 read_command()
{
char v0; // al
unsigned int v1; // edx
__int64 v2; // rax

write_string(">");
command_length[0] = 0;
do
{
while ( 1 )
{
v0 = read_key();
if ( v0 == 10 )
break;
v1 = command_length[0];
if ( v0 == 8 )
{
if ( command_length[0] )
{
--command_length[0];
goto LABEL_5;
}
}
else
{
command[command_length[0]] = v0;
command_length[0] = v1 + 1;
LABEL_5:
write_char(v0);
v2 = command_length[0];
if ( command_length[0] > 0xFE )
goto LABEL_6;
}
}
v2 = command_length[0];
}
while ( !command_length[0] );
LABEL_6:
command[v2] = 0;
return write_string("\n");
}

canonical_decompiled_code:
__int64 func3()
{
char v0; // al
unsigned int v1; // edx
__int64 v2; // rax

func2(">");
command_length[0] = 0;
do
{
while ( 1 )
{
v0 = read_key();
if ( v0 == 10 )
break;
v1 = command_length[0];
if ( v0 == 8 )
{
if ( command_length[0] )
{
--command_length[0];
goto LABEL_5;
}
}
else
{
command[command_length[0]] = v0;
command_length[0] = v1 + 1;
LABEL_5:
func1(v0);
v2 = command_length[0];
if ( command_length[0] > 0xFE )
goto LABEL_6;
}
}
v2 = command_length[0];
}
while ( !command_length[0] );
LABEL_6:
command[v2] = 0;
return func2("\n");
}

original_code:
static void read_command()
{
 write_string(">");
 command_length = 0;
 while (command_length < 256 -1)
 {
  uint8_t key = read_key();
  if (key == '\n')
  {
   if (command_length > 0)
   {
    break;
   }
   continue;
  }
  if (key == '\b')
  {
   if (command_length > 0)
   {
    --command_length;
   }
   else
   {
    continue;
   }
  }
  else
  {
   command[command_length] = key;
   ++command_length;
  }
  write_char(key);
 }
 command[command_length] = 0;
 write_string("\n");
}

canonical_original_code:
static void read_command()
{
 write_string(">");
 command_length = 0;
 while (command_length < 256 -1)
 {
  unsigned char key = read_key();
  if (key == '\n')
  {
   if (command_length > 0)
   {
    break;
   }
   continue;
  }
  if (key == '\b')
  {
   if (command_length > 0)
   {
    --command_length;
   }
   else
   {
    continue;
   }
  }
  else
  {
   command[command_length] = key;
   ++command_length;
  }
  write_char(key);
 }
 command[command_length] = 0;
 write_string("\n");
}

variable_types:
key
unsigned char


decompiled_code:
char check_command()
{
char result; // al

if ( (unsigned __int8)streq("help", command) )
{
write_string("Commands:\n");
write_string(" help - Display this text\n");
result = write_string(" tetris - Play tetris\n");
}
else if ( (unsigned __int8)streq("tetris", command) )
{
tetris();
clear_screen();
text_set_pos(0LL, 0LL);
result = text_color;
LOBYTE(text_mode_info[2]) = text_color;
}
else
{
result = write_string("Unrecognized command\n");
}
return result;
}

canonical_decompiled_code:
char func4()
{
char result; // al

if ( (unsigned __int8)streq("help", command) )
{
func2("Commands:\n");
func2(" help - Display this text\n");
result = func2(" tetris - Play tetris\n");
}
else if ( (unsigned __int8)streq("tetris", command) )
{
tetris();
clear_screen();
text_set_pos(0LL, 0LL);
result = text_color;
LOBYTE(text_mode_info[2]) = text_color;
}
else
{
result = func2("Unrecognized command\n");
}
return result;
}

original_code:
static void check_command()
{
 if (streq("help", command))
 {
  write_string("Commands:\n");
  write_string(" help - Display this text\n");
  write_string(" tetris - Play tetris\n");
 }
 else if (streq("tetris", command))
 {
  tetris();
  clear_screen();
  text_set_pos(0, 0);
  text_mode_info.color = text_color;
 }
 else
 {
  write_string("Unrecognized command\n");
 }
}


decompiled_code:
__int64 display_banner()
{
return fancy_display_string_constprop_0();
}

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

original_code:
static void display_banner()
{
 fancy_display_string("Welcome to Bikeshed64!");
}


decompiled_code:
void __noreturn shell_loop()
{
text_set_pos(0LL, 0LL);
clear_screen();
display_banner();
text_set_pos(0LL, 1LL);
while ( 1 )
{
read_command();
check_command();
}
}

canonical_decompiled_code:
void __noreturn func7()
{
text_set_pos(0LL, 0LL);
clear_screen();
func6();
text_set_pos(0LL, 1LL);
while ( 1 )
{
func3();
func4();
}
}

original_code:
void shell_loop()
{
 text_set_pos(0, 0);
 clear_screen();
 display_banner();
 text_set_pos(0, 1);
 while (1)
 {
  read_command();
  check_command();
 }
}


decompiled_code:
__int64 __fastcall write_char(char a1)
{
if ( !(text_mode_info[0] | text_mode_info[1]) )
clear_screen();
return text_mode_char((unsigned int)a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(char a1)
{
if ( !(text_mode_info[0] | text_mode_info[1]) )
clear_screen();
return text_mode_char((unsigned int)a1);
}

original_code:
static void write_char(const char c)
{
 if (text_mode_info.y == 0 && text_mode_info.x == 0)
 {
  clear_screen();
 }
 text_mode_char(c);
}

canonical_original_code:
static void write_char(char c)
{
 if (text_mode_info.y == 0 && text_mode_info.x == 0)
 {
  clear_screen();
 }
 text_mode_char(c);
}


exec

decompiled_code:
void exec()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void exec(PCB* pcb)
{
 ((void)pcb);
}

canonical_original_code:
void exec(struct PCB *pcb)
{
 ((void)pcb);
}


decompiled_code:
__int64 __fastcall syscall_interrupt(unsigned __int64 a1)
{
__int64 v2; // rdi
unsigned __int64 v3; // rax
__int64 result; // rax

timer_stop();
v2 = current_pcb;
if ( !current_pcb )
{
panic("SYSCALL: Current PCB is NULL!");
v2 = current_pcb;
}
v3 = *(_QWORD *)(*(_QWORD *)v2 + 72LL);
if ( v3 <= 6 )
syscall_functions[v3]();
else
qword_370();
result = a1 - 32;
if ( a1 - 32 <= 0xF )
{
result = 32LL;
__outbyte(0x20u, 0x20u);
if ( a1 > 0x28 )
__outbyte(0xA0u, 0x20u);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int64 a1)
{
__int64 v2; // rdi
unsigned __int64 v3; // rax
__int64 result; // rax

timer_stop();
v2 = current_pcb;
if ( !current_pcb )
{
panic("SYSCALL: Current PCB is NULL!");
v2 = current_pcb;
}
v3 = *(_QWORD *)(*(_QWORD *)v2 + 72LL);
if ( v3 <= 6 )
syscall_functions[v3]();
else
qword_370();
result = a1 - 32;
if ( a1 - 32 <= 0xF )
{
result = 32LL;
__outbyte(0x20u, 0x20u);
if ( a1 > 0x28 )
__outbyte(0xA0u, 0x20u);
}
return result;
}

original_code:
void syscall_interrupt(uint64_t vector, uint64_t error)
{
 ((void)error);
 timer_stop();
 ;
 ;
 if (current_pcb == ((void*)0))
 {
  panic("SYSCALL: Current PCB is NULL!");
 }
 uint64_t syscall_num = current_pcb->context->r10;
 ;
 ;
 if (syscall_num >= 7)
 {
  ;
  syscall_functions[2](current_pcb);
 }
 else
 {
  syscall_functions[syscall_num](current_pcb);
 }
 ;
 pic_acknowledge(vector);
}

canonical_original_code:
void syscall_interrupt(unsigned long vector, unsigned long error)
{
 ((void)error);
 timer_stop();
 ;
 ;
 if (current_pcb == ((void *)0))
 {
  panic("SYSCALL: Current PCB is NULL!");
 }
 unsigned long syscall_num = current_pcb->context->r10;
 ;
 ;
 if (syscall_num >= 7)
 {
  ;
  syscall_functions[2](current_pcb);
 }
 else
 {
  syscall_functions[syscall_num](current_pcb);
 }
 ;
 pic_acknowledge(vector);
}

variable_types:
error
unsigned long


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

result = (unsigned __int8)keyboard_get_char();
*(_QWORD *)(*(_QWORD *)a1 + 40LL) = (unsigned __int8)result;
return result;
}

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

result = (unsigned __int8)keyboard_get_char();
*(_QWORD *)(*(_QWORD *)a1 + 40LL) = (unsigned __int8)result;
return result;
}

original_code:
void get_key(PCB* pcb)
{
 pcb->context->rax = keyboard_get_char();
}

canonical_original_code:
void get_key(struct PCB *pcb)
{
 pcb->context->rax = keyboard_get_char();
}


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

result = (unsigned __int8)keyboard_char_available();
*(_QWORD *)(*(_QWORD *)a1 + 40LL) = (unsigned __int8)result;
return result;
}

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

result = (unsigned __int8)keyboard_char_available();
*(_QWORD *)(*(_QWORD *)a1 + 40LL) = (unsigned __int8)result;
return result;
}

original_code:
void key_avail(PCB* pcb)
{
 pcb->context->rax = keyboard_char_available();
}

canonical_original_code:
void key_avail(struct PCB *pcb)
{
 pcb->context->rax = keyboard_char_available();
}


fork

decompiled_code:
__int64 __fastcall fork(void *src)
{
_QWORD *v1; // rax
_QWORD *v2; // rbp
__int64 v3; // rax
__int64 v4; // rsi
__int64 v5; // r12
__int64 v6; // rdx
__int64 v7; // rax
__int64 v8; // rsi
__int64 result; // rax
__int64 v10; // [rsp+0h] [rbp-38h] BYREF
__int64 v11[6]; // [rsp+8h] [rbp-30h] BYREF

v1 = (_QWORD *)alloc_pcb();
if ( v1 )
{
v2 = v1;
memcpy(v1, src, 0x28uLL);
v3 = virt_clone_mapping(*((_QWORD *)src + 1));
v10 = 0LL;
v4 = *(_QWORD *)src;
v2[1] = v3;
v5 = v3;
if ( !(unsigned __int8)virt_lookup_phys(v3, v4, &v10) )
panic("Fork: failed to lookup context location, very bad!");
v6 = *(_QWORD *)src;
v7 = v10 - 0x800000000000LL;
v8 = *(_QWORD *)(v10 - 0x800000000000LL);
*v2 = *(_QWORD *)src;
*(_QWORD *)(v6 + 40) = 0LL;
**(_DWORD **)v6 = 0;
*(_QWORD *)(v7 + 40) = 0LL;
v11[0] = 0LL;
if ( !(unsigned __int8)virt_lookup_phys(v5, v8, v11) )
panic("Fork: failed to lookup parameter location!");
*(_DWORD *)(v11[0] - 0x800000000000LL) = 1;
schedule(v2);
result = dispatch();
}
else
{
result = *(_QWORD *)src;
*(_QWORD *)(*(_QWORD *)src + 40LL) = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(void *src)
{
_QWORD *v1; // rax
_QWORD *v2; // rbp
__int64 v3; // rax
__int64 v4; // rsi
__int64 v5; // r12
__int64 v6; // rdx
__int64 v7; // rax
__int64 v8; // rsi
__int64 result; // rax
__int64 v10; // [rsp+0h] [rbp-38h] BYREF
__int64 v11[6]; // [rsp+8h] [rbp-30h] BYREF

v1 = (_QWORD *)alloc_pcb();
if ( v1 )
{
v2 = v1;
memcpy(v1, src, 0x28uLL);
v3 = virt_clone_mapping(*((_QWORD *)src + 1));
v10 = 0LL;
v4 = *(_QWORD *)src;
v2[1] = v3;
v5 = v3;
if ( !(unsigned __int8)virt_lookup_phys(v3, v4, &v10) )
panic("Fork: failed to lookup context location, very bad!");
v6 = *(_QWORD *)src;
v7 = v10 - 0x800000000000LL;
v8 = *(_QWORD *)(v10 - 0x800000000000LL);
*v2 = *(_QWORD *)src;
*(_QWORD *)(v6 + 40) = 0LL;
**(_DWORD **)v6 = 0;
*(_QWORD *)(v7 + 40) = 0LL;
v11[0] = 0LL;
if ( !(unsigned __int8)virt_lookup_phys(v5, v8, v11) )
panic("Fork: failed to lookup parameter location!");
*(_DWORD *)(v11[0] - 0x800000000000LL) = 1;
schedule(v2);
result = dispatch();
}
else
{
result = *(_QWORD *)src;
*(_QWORD *)(*(_QWORD *)src + 40LL) = 1LL;
}
return result;
}

original_code:
void fork(PCB* pcb)
{
 ;
 PCB* new_pcb = alloc_pcb();
 if (new_pcb == ((void*)0))
 {
  pcb->context->rax = FAILURE;
  return;
 }
 memcpy(new_pcb, pcb, sizeof(PCB));
 ;
 ;
 void* new_page_table = virt_clone_mapping(pcb->page_table);
 new_pcb->page_table = new_page_table;
 ;
 uint64_t new_context_addr = 0;
 if (!virt_lookup_phys(new_page_table, (uint64_t)pcb->context, &new_context_addr))
 {
  panic("Fork: failed to lookup context location, very bad!");
 }
 ;
 Context* new_context = (Context*)((void*)((uint64_t)(new_context_addr) + 0xFFFF800000000000));
 ;
 new_pcb->context = pcb->context;
 pcb->context->rax = SUCCESS;
 *((Pid*)pcb->context->rdi) = 0;
 new_context->rax = SUCCESS;
 ;
 ;
 uint64_t pid_param_location = 0;
 if (!virt_lookup_phys(new_page_table, (uint64_t)new_context->rdi, &pid_param_location))
 {
  panic("Fork: failed to lookup parameter location!");
 }
 ;
 ;
 Pid* param_pid = (Pid*)((void*)((uint64_t)(pid_param_location) + 0xFFFF800000000000));
 *param_pid = 1;
 ;
 ;
 schedule(new_pcb);
 ;
 dispatch();
}

canonical_original_code:
void fork(struct PCB *pcb)
{
 ;
 struct PCB *new_pcb = alloc_pcb();
 if (new_pcb == ((void *)0))
 {
  pcb->context->rax = FAILURE;
  return;
 }
 memcpy(new_pcb, pcb, sizeof(PCB));
 ;
 ;
 void *new_page_table = virt_clone_mapping(pcb->page_table);
 new_pcb->page_table = new_page_table;
 ;
 unsigned long new_context_addr = 0;
 if (!virt_lookup_phys(new_page_table, (unsigned long)pcb->context, &new_context_addr))
 {
  panic("Fork: failed to lookup context location, very bad!");
 }
 ;
 struct Context *new_context = (Context*)((void*)((uint64_t)(new_context_addr) + 0xFFFF800000000000));
 ;
 new_pcb->context = pcb->context;
 pcb->context->rax = SUCCESS;
 *((unsigned int *)pcb->context->rdi) = 0;
 new_context->rax = SUCCESS;
 ;
 ;
 unsigned long pid_param_location = 0;
 if (!virt_lookup_phys(new_page_table, (unsigned long)new_context->rdi, &pid_param_location))
 {
  panic("Fork: failed to lookup parameter location!");
 }
 ;
 ;
 unsigned int *param_pid = (Pid*)((void*)((uint64_t)(pid_param_location) + 0xFFFF800000000000));
 *param_pid = 1;
 ;
 ;
 schedule(new_pcb);
 ;
 dispatch();
}

variable_types: struct Context { unsigned long rdi; unsigned long rsi; unsigned long rdx; unsigned long rcx; unsigned long rbx; unsigned long rax; unsigned long rbp; unsigned long r8; unsigned long r9; unsigned long r10; unsigned long r11; unsigned long r12; unsigned long r13; unsigned long r14; unsigned long r15; unsigned long vector; unsigned long error_code; unsigned long rip; unsigned long cs; unsigned long rflags; unsigned long rsp; unsigned long ss; } *


decompiled_code:
__int64 syscalls_init()
{
syscall_functions[0] = (void (*)(void))fork;
syscall_functions[1] = exec;
syscall_functions[2] = (void (*)(void))exit;
syscall_functions[3] = (void (*)(void))msleep;
syscall_functions[6] = (void (*)(void))get_key;
syscall_functions[4] = (void (*)(void))set_priority;
syscall_functions[5] = (void (*)(void))key_avail;
return interrupts_install_isr(128LL, syscall_interrupt);
}

canonical_decompiled_code:
__int64 func8()
{
syscall_functions[0] = (void (*)(void))fork;
syscall_functions[1] = exec;
syscall_functions[2] = (void (*)(void))exit;
syscall_functions[3] = (void (*)(void))msleep;
syscall_functions[6] = (void (*)(void))get_key;
syscall_functions[4] = (void (*)(void))set_priority;
syscall_functions[5] = (void (*)(void))key_avail;
return interrupts_install_isr(128LL, syscall_interrupt);
}

original_code:
void syscalls_init()
{
 syscall_functions[0] = fork;
 syscall_functions[1] = exec;
 syscall_functions[2] = exit;
 syscall_functions[3] = msleep;
 syscall_functions[4] = set_priority;
 syscall_functions[5] = key_avail;
 syscall_functions[6] = get_key;
 interrupts_install_isr(0x80, syscall_interrupt);
}


decompiled_code:
unsigned __int64 *__fastcall set_priority(__int64 a1)
{
unsigned __int64 *result; // rax
unsigned __int64 v2; // rbp

result = *(unsigned __int64 **)a1;
v2 = **(_QWORD **)a1;
if ( v2 > 3 )
result = (unsigned __int64 *)panic("ASSERT FAILED: src/kernel/syscalls/syscalls.c:159");
*(_DWORD *)(a1 + 32) = v2;
return result;
}

canonical_decompiled_code:
unsigned __int64 *__fastcall func2(__int64 a1)
{
unsigned __int64 *result; // rax
unsigned __int64 v2; // rbp

result = *(unsigned __int64 **)a1;
v2 = **(_QWORD **)a1;
if ( v2 > 3 )
result = (unsigned __int64 *)panic("ASSERT FAILED: src/kernel/syscalls/syscalls.c:159");
*(_DWORD *)(a1 + 32) = v2;
return result;
}

original_code:
void set_priority(PCB* pcb)
{
 const uint64_t priority = pcb->context->rdi;
 if (!(priority <= IDLE)) panic("ASSERT FAILED: " "src/kernel/syscalls/syscalls.c" ":" "159");
 ;
 pcb->priority = (Priority)priority;
}

canonical_original_code:
void set_priority(struct PCB *pcb)
{
 unsigned long priority = pcb->context->rdi;
 if (!(priority <= IDLE)) panic("ASSERT FAILED: " "src/kernel/syscalls/syscalls.c" ":" "159");
 ;
 pcb->priority = (enum Priority)priority;
}

variable_types:
priority
unsigned long


msleep

decompiled_code:
__int64 __fastcall msleep(_QWORD **a1)
{
return sleep_pcb(a1, **a1);
}

canonical_decompiled_code:
__int64 __fastcall func5(_QWORD **a1)
{
return sleep_pcb(a1, **a1);
}

original_code:
void msleep(PCB* pcb)
{
 const time_t sleep_time = pcb->context->rdi;
 sleep_pcb(pcb, sleep_time);
}

canonical_original_code:
void msleep(struct PCB *pcb)
{
 unsigned int sleep_time = pcb->context->rdi;
 sleep_pcb(pcb, sleep_time);
}


exit

decompiled_code:
void __noreturn exit()
{
cleanup_pcb();
dispatch();
}

canonical_decompiled_code:
void __noreturn func6()
{
cleanup_pcb();
dispatch();
}

original_code:
void exit(PCB* pcb)
{
 cleanup_pcb(pcb);
 dispatch();
}

canonical_original_code:
void exit(struct PCB *pcb)
{
 cleanup_pcb(pcb);
 dispatch();
}


decompiled_code:
unsigned __int64 setup_kernel_tss()
{
char *v0; // rax
unsigned __int64 result; // rax

v0 = (char *)&tss_seg_64 - 0x800000000000LL;
tss = (__int64)v0;
*(_WORD *)v0 = 104;
*((_WORD *)v0 + 1) = (unsigned __int16)&kernel_TSS;
v0[4] = (unsigned int)&kernel_TSS >> 16;
*(_WORD *)(v0 + 5) = 137;
v0[7] = (unsigned int)&kernel_TSS >> 24;
*((_QWORD *)v0 + 1) = (unsigned __int64)&kernel_TSS >> 32;
memclr(&kernel_TSS, 104LL);
result = 0xFFFF8000001FFFFCLL;
*((_WORD *)&kernel_TSS + 51) = 104;
*(_QWORD *)((char *)&kernel_TSS + 4) = 0xFFFF8000001FFFFCLL;
*(_QWORD *)((char *)&kernel_TSS + 36) = 0xFFFF8000001FFFFCLL;
return result;
}

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

v0 = (char *)&tss_seg_64 - 0x800000000000LL;
tss = (__int64)v0;
*(_WORD *)v0 = 104;
*((_WORD *)v0 + 1) = (unsigned __int16)&kernel_TSS;
v0[4] = (unsigned int)&kernel_TSS >> 16;
*(_WORD *)(v0 + 5) = 137;
v0[7] = (unsigned int)&kernel_TSS >> 24;
*((_QWORD *)v0 + 1) = (unsigned __int64)&kernel_TSS >> 32;
memclr(&kernel_TSS, 104LL);
result = 0xFFFF8000001FFFFCLL;
*((_WORD *)&kernel_TSS + 51) = 104;
*(_QWORD *)((char *)&kernel_TSS + 4) = 0xFFFF8000001FFFFCLL;
*(_QWORD *)((char *)&kernel_TSS + 36) = 0xFFFF8000001FFFFCLL;
return result;
}

original_code:
static
void setup_kernel_tss()
{
 const uint64_t tss_base = (uint64_t)&kernel_TSS;
 const uint64_t tss_limit = sizeof(TSS);
 tss = ((void*)((uint64_t)(&tss_seg_64) + 0xFFFF800000000000));
 tss->limit = tss_limit & 0xFF;
 tss->base1 = tss_base & 0xFFFF;
 tss->base2 = (tss_base & 0xFF0000) >> 16;
 tss->flags = 0x80 | (0x00 << 1) | 0x9;
 tss->limit2 = (tss_limit & 0xF0000) >> 16;
 tss->base3 = (tss_base & 0xFF000000) >> 24;
 tss->base4 = (tss_base & 0xFFFFFFFF00000000) >> 32;
 tss->reserved = 0;
 memclr(&kernel_TSS, sizeof(kernel_TSS));
 kernel_TSS.io_map_base = 104;
 kernel_TSS.rsp[0] = 0xFFFF800000000000 + (0x200000 - 0x4);
 kernel_TSS.ist[0] = 0xFFFF800000000000 + (0x200000 - 0x4);
}

canonical_original_code:
static void setup_kernel_tss()
{
 unsigned long tss_base = (uint64_t)&kernel_TSS;
 unsigned long tss_limit = sizeof(TSS);
 tss = ((void *)((unsigned long)(&tss_seg_64) + 0xFFFF800000000000));
 tss->limit = tss_limit & 0xFF;
 tss->base1 = tss_base & 0xFFFF;
 tss->base2 = (tss_base & 0xFF0000) >> 16;
 tss->flags = 0x80 | (0x00 << 1) | 0x9;
 tss->limit2 = (tss_limit & 0xF0000) >> 16;
 tss->base3 = (tss_base & 0xFF000000) >> 24;
 tss->base4 = (tss_base & 0xFFFFFFFF00000000) >> 32;
 tss->reserved = 0;
 memclr(&kernel_TSS, sizeof(kernel_TSS));
 kernel_TSS.io_map_base = 104;
 kernel_TSS.rsp[0] = 0xFFFF800000000000 + (0x200000 - 0x4);
 kernel_TSS.ist[0] = 0xFFFF800000000000 + (0x200000 - 0x4);
}


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

result = a1;
qword_E4 = a1;
return result;
}

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

result = a1;
qword_E4 = a1;
return result;
}

original_code:
void tss_set_context_stack(const uint64_t location)
{
 kernel_TSS.rsp[0] = location;
}

canonical_original_code:
void tss_set_context_stack(unsigned long location)
{
 kernel_TSS.rsp[0] = location;
}

variable_types:
location
unsigned long


decompiled_code:
__int16 setup_tss_descriptor()
{
__int16 result; // ax

setup_kernel_tss();
result = 80;
__asm { ltr ax }
return result;
}

canonical_decompiled_code:
__int16 func2()
{
__int16 result; // ax

func0();
result = 80;
__asm { ltr ax }
return result;
}

original_code:
void setup_tss_descriptor()
{
 setup_kernel_tss();
 __asm__ volatile ("movw $" "0x50" ", %ax");
 __asm__ volatile ("ltr %ax");
}


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

if ( !stack_empty_isra_0(*(_QWORD *)(a1 + 32)) )
return stack_pop(v1 + 4);
v2 = v1[2];
result = 0LL;
if ( v2 < v1[1] )
{
result = v1[2];
v1[2] = v1[3] + v2;
}
return result;
}

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

if ( !func0(*(_QWORD *)(a1 + 32)) )
return stack_pop(v1 + 4);
v2 = v1[2];
result = 0LL;
if ( v2 < v1[1] )
{
result = v1[2];
v1[2] = v1[3] + v2;
}
return result;
}

original_code:
void* block_alloc(BlockAllocator* ba)
{
 if (!stack_empty(&ba->free_stack))
 {
  return stack_pop(&ba->free_stack);
 }
 else if (ba->implicit_next < ba->max_address)
 {
  void* returnVal = (void*)ba->implicit_next;
  ba->implicit_next += ba->block_size;
  return returnVal;
 }
 return ((void*)0);
}

canonical_original_code:
void *block_alloc(struct BlockAllocator *ba)
{
 if (!stack_empty(&ba->free_stack))
 {
  return stack_pop(&ba->free_stack);
 }
 else if (ba->implicit_next < ba->max_address)
 {
  void *returnVal = (void*)ba->implicit_next;
  ba->implicit_next += ba->block_size;
  return returnVal;
 }
 return ((void *)0);
}

variable_types: struct BlockAllocator { unsigned long base; unsigned long max_address; unsigned long implicit_next; unsigned long block_size; struct Stack { unsigned long size; struct _StackNode { struct _StackNode *next; } *top; } free_stack; } *

variable_types:
returnVal
void *

return_type: void *

user_defined_types: struct BlockAllocator { unsigned long base; unsigned long max_address; unsigned long implicit_next; unsigned long block_size; struct Stack free_stack; }

user_defined_types: struct Stack { unsigned long size; struct _StackNode *top; }


decompiled_code:
__int64 __fastcall block_free(__int64 a1, unsigned __int64 a2)
{
if ( a1 + 48 > a2 || a2 >= *(_QWORD *)(a1 + 8) )
panic("ASSERT FAILED: src/kernel/data_structures/block.c:56");
return stack_push(a1 + 32, a2);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned __int64 a2)
{
if ( a1 + 48 > a2 || a2 >= *(_QWORD *)(a1 + 8) )
panic("ASSERT FAILED: src/kernel/data_structures/block.c:56");
return stack_push(a1 + 32, a2);
}

original_code:
void block_free(BlockAllocator* ba, void* ptr)
{
 if (!(((uint64_t)ptr) >= ((uint64_t)ba+sizeof(BlockAllocator)) && (uint64_t)ptr < ba->max_address)) panic("ASSERT FAILED: " "src/kernel/data_structures/block.c" ":" "56");
 stack_push(&ba->free_stack, ptr);
}

canonical_original_code:
void block_free(struct BlockAllocator *ba, void *ptr)
{
 if (!(((unsigned long)ptr) >= ((unsigned long)ba+sizeof(BlockAllocator)) && (unsigned long)ptr < ba->max_address)) panic("ASSERT FAILED: " "src/kernel/data_structures/block.c" ":" "56");
 stack_push(&ba->free_stack, ptr);
}

variable_types:
ptr
void *

user_defined_types: struct _StackNode { struct _StackNode *next; }


decompiled_code:
__int64 alloc_init()
{
return water_mark_init(&kernel_WaterMark, -2097152LL, 0x400000LL);
}

canonical_decompiled_code:
__int64 func0()
{
return water_mark_init(&kernel_WaterMark, -2097152LL, 0x400000LL);
}

original_code:
void alloc_init()
{
 water_mark_init(&kernel_WaterMark, (void*)0xFFFFFFFFFFE00000, 0x400000);
}

canonical_original_code:
void alloc_init()
{
 water_mark_init(&kernel_WaterMark, (void *)0xFFFFFFFFFFE00000, 0x400000);
}


kmain

decompiled_code:
void __noreturn kmain()
{
virt_memory_init();
alloc_init();
interrupts_init();
keyboard_init();
scheduler_init();
syscalls_init();
create_init_process();
_enable();
__halt();
}

canonical_decompiled_code:
void __noreturn func0()
{
virt_memory_init();
alloc_init();
interrupts_init();
keyboard_init();
scheduler_init();
syscalls_init();
create_init_process();
_enable();
__halt();
}

original_code:
void kmain(void)
{
 virt_memory_init();
 alloc_init();
 interrupts_init();
 keyboard_init();
 scheduler_init();
 syscalls_init();
 create_init_process();
 while (1) {
  __asm__ volatile ("sti");
  __asm__ volatile ("hlt");
 }
}

canonical_original_code:
void kmain()
{
 virt_memory_init();
 alloc_init();
 interrupts_init();
 keyboard_init();
 scheduler_init();
 syscalls_init();
 create_init_process();
 while (1) {
  __asm__ volatile ("sti");
  __asm__ volatile ("hlt");
 }
}


original_code:
static void pool_init(Pool* pool)
{
 stack_init(&pool->free_stack);
 pool->implicit_next = (void*) ((uint64_t)pool + (4*1024ULL));
 pool->max_address = (void*) ((uint64_t)pool + (2ULL*(1024ULL*1024ULL)));
 pool->next = ((void*)0);
 pool->prev = ((void*)0);
 pool->on_list = 0;
 ;
 ;
 ;
 ;
 ;
 ;
}

canonical_original_code:
static void pool_init(struct _Pool *pool)
{
 stack_init(&pool->free_stack);
 pool->implicit_next = (void *) ((unsigned long)pool + (4*1024ULL));
 pool->max_address = (void *) ((unsigned long)pool + (2ULL*(1024ULL*1024ULL)));
 pool->next = ((void *)0);
 pool->prev = ((void *)0);
 pool->on_list = 0;
 ;
 ;
 ;
 ;
 ;
 ;
}

variable_types: struct _Pool { struct Stack { unsigned long size; struct _StackNode { struct _StackNode *next; } *top; } free_stack; void *implicit_next; void *max_address; struct _Pool *next; struct _Pool *prev; unsigned char on_list; } *

user_defined_types: struct _Pool { struct Stack free_stack; void *implicit_next; void *max_address; struct _Pool *next; struct _Pool *prev; unsigned char on_list; }


decompiled_code:
__int64 __fastcall pool_free(unsigned __int64 a1, unsigned __int64 a2)
{
if ( a2 <= a1 || *(_QWORD *)(a1 + 24) <= a2 )
panic("Pool freeing bad ptr");
return stack_push(a1, a2 & 0xFFFFFFFFFFFFF000LL);
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int64 a1, unsigned __int64 a2)
{
if ( a2 <= a1 || *(_QWORD *)(a1 + 24) <= a2 )
panic("Pool freeing bad ptr");
return stack_push(a1, a2 & 0xFFFFFFFFFFFFF000LL);
}

original_code:
static void pool_free(Pool* pool, void* ptr)
{
 if (ptr <= (void*)pool || ptr >= pool->max_address)
 {
  ;
  panic("Pool freeing bad ptr");
 }
 stack_push(&pool->free_stack, (void*) (((uint64_t)ptr) & 0xFFFFFFFFFFFFF000));
}

canonical_original_code:
static void pool_free(struct _Pool *pool, void *ptr)
{
 if (ptr <= (void *)pool || ptr >= pool->max_address)
 {
  ;
  panic("Pool freeing bad ptr");
 }
 stack_push(&pool->free_stack, (void *) (((unsigned long)ptr) & 0xFFFFFFFFFFFFF000));
}


decompiled_code:
bool __fastcall pool_full(__int64 *a1)
{
__int64 v1; // rax

v1 = stack_size_isra_0(*a1);
if ( v1 != 511 || a1[2] >= (unsigned __int64)a1[3] )
return v1 == 511;
panic("ASSERT FAILED: src/arch/x86_64/virt_memory/phys_alloc.c:209");
return stack_size_isra_0(*a1) == 511;
}

canonical_decompiled_code:
bool __fastcall func3(__int64 *a1)
{
__int64 v1; // rax

v1 = func2(*a1);
if ( v1 != 511 || a1[2] >= (unsigned __int64)a1[3] )
return v1 == 511;
panic("ASSERT FAILED: src/arch/x86_64/virt_memory/phys_alloc.c:209");
return func2(*a1) == 511;
}

original_code:
static uint8_t pool_full(Pool* pool)
{
 const uint64_t entries_free = stack_size(&pool->free_stack);
 if (entries_free == 511)
 {
  if (!(pool->implicit_next >= pool->max_address)) panic("ASSERT FAILED: " "src/arch/x86_64/virt_memory/phys_alloc.c" ":" "209");
 }
 return stack_size(&pool->free_stack) == 511;
}

canonical_original_code:
static unsigned char pool_full(struct _Pool *pool)
{
 unsigned long entries_free = stack_size(&pool->free_stack);
 if (entries_free == 511)
 {
  if (!(pool->implicit_next >= pool->max_address)) panic("ASSERT FAILED: " "src/arch/x86_64/virt_memory/phys_alloc.c" ":" "209");
 }
 return stack_size(&pool->free_stack) == 511;
}


decompiled_code:
bool __fastcall pool_empty(__int64 *a1)
{
bool result; // al
__int64 v2; // rdx

result = stack_empty_isra_0(*a1);
if ( result )
result = *(_QWORD *)(v2 + 16) >= *(_QWORD *)(v2 + 24);
return result;
}

canonical_decompiled_code:
bool __fastcall func5(__int64 *a1)
{
bool result; // al
__int64 v2; // rdx

result = func4(*a1);
if ( result )
result = *(_QWORD *)(v2 + 16) >= *(_QWORD *)(v2 + 24);
return result;
}

original_code:
static uint8_t pool_empty(Pool* pool)
{
 return stack_empty(&pool->free_stack)
  && pool->implicit_next >= pool->max_address;
}

canonical_original_code:
static unsigned char pool_empty(struct _Pool *pool)
{
 return stack_empty(&pool->free_stack)
  && pool->implicit_next >= pool->max_address;
}


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

if ( pool_empty(a1) )
panic("Trying to allocate from empty pool!");
if ( !stack_empty_isra_0(*a1) )
return stack_pop(a1);
result = a1[2];
a1[2] = result + 4096;
return result;
}

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

if ( func5(a1) )
panic("Trying to allocate from empty pool!");
if ( !func4(*a1) )
return stack_pop(a1);
result = a1[2];
a1[2] = result + 4096;
return result;
}

original_code:
static void* pool_alloc(Pool* pool)
{
 if (pool_empty(pool))
 {
  panic("Trying to allocate from empty pool!");
 }
 if (!stack_empty(&pool->free_stack))
 {
  return stack_pop(&pool->free_stack);
 }
 else
 {
  void* retVal = pool->implicit_next;
  pool->implicit_next = (void*) ((uint64_t)pool->implicit_next + (4*1024ULL));
  return retVal;
 }
}

canonical_original_code:
static void *pool_alloc(struct _Pool *pool)
{
 if (pool_empty(pool))
 {
  panic("Trying to allocate from empty pool!");
 }
 if (!stack_empty(&pool->free_stack))
 {
  return stack_pop(&pool->free_stack);
 }
 else
 {
  void *retVal = pool->implicit_next;
  pool->implicit_next = (void *) ((unsigned long)pool->implicit_next + (4*1024ULL));
  return retVal;
 }
}

variable_types:
retVal
void *


decompiled_code:
__int64 setup_physical_allocator()
{
int v0; // ebx
__int64 result; // rax
__int64 v2; // r14
__int64 v3; // r13
unsigned __int64 v4; // rcx
unsigned __int64 v5; // rbx
unsigned __int64 v6; // rcx
unsigned __int64 v7; // r15
unsigned __int64 v8; // rbx
unsigned __int64 v9; // rsi

v0 = MEMORY[0x2D00];
stack_init(&stack_2MIB);
result = (__int64)pool_4KIB;
pool_4KIB[0] = 0LL;
if ( v0 )
{
v2 = 11524LL;
result = 3LL * (unsigned int)(v0 - 1);
v3 = 24LL * (unsigned int)(v0 - 1) + 11548;
do
{
while ( 1 )
{
if ( *(_DWORD *)(v2 + 16) == 1 )
{
v4 = *(_QWORD *)(v2 + 8);
if ( v4 > 0x1FFFFF )
{
result = *(_QWORD *)v2;
v5 = *(_QWORD *)v2 & 0xFFFFFFFFFFE00000LL;
if ( (*(_QWORD *)v2 & 0x1FFFFF) != 0 )
v5 += 0x200000LL;
v6 = result + v4 - v5;
if ( v6 > 0x1FFFFF )
break;
}
}
v2 += 24LL;
if ( v3 == v2 )
return result;
}
v7 = v5 - 0x800000000000LL;
v8 = ((v6 - 0x200000) & 0xFFFFFFFFFFE00000LL) + v5 - 0x7FFFFFE00000LL;
do
{
v9 = v7;
v7 += 0x200000LL;
result = stack_push(&stack_2MIB, v9);
}
while ( v7 != v8 );
v2 += 24LL;
}
while ( v3 != v2 );
}
return result;
}

canonical_decompiled_code:
__int64 func7()
{
int v0; // ebx
__int64 result; // rax
__int64 v2; // r14
__int64 v3; // r13
unsigned __int64 v4; // rcx
unsigned __int64 v5; // rbx
unsigned __int64 v6; // rcx
unsigned __int64 v7; // r15
unsigned __int64 v8; // rbx
unsigned __int64 v9; // rsi

v0 = MEMORY[0x2D00];
stack_init(&stack_2MIB);
result = (__int64)pool_4KIB;
pool_4KIB[0] = 0LL;
if ( v0 )
{
v2 = 11524LL;
result = 3LL * (unsigned int)(v0 - 1);
v3 = 24LL * (unsigned int)(v0 - 1) + 11548;
do
{
while ( 1 )
{
if ( *(_DWORD *)(v2 + 16) == 1 )
{
v4 = *(_QWORD *)(v2 + 8);
if ( v4 > 0x1FFFFF )
{
result = *(_QWORD *)v2;
v5 = *(_QWORD *)v2 & 0xFFFFFFFFFFE00000LL;
if ( (*(_QWORD *)v2 & 0x1FFFFF) != 0 )
v5 += 0x200000LL;
v6 = result + v4 - v5;
if ( v6 > 0x1FFFFF )
break;
}
}
v2 += 24LL;
if ( v3 == v2 )
return result;
}
v7 = v5 - 0x800000000000LL;
v8 = ((v6 - 0x200000) & 0xFFFFFFFFFFE00000LL) + v5 - 0x7FFFFFE00000LL;
do
{
v9 = v7;
v7 += 0x200000LL;
result = stack_push(&stack_2MIB, v9);
}
while ( v7 != v8 );
v2 += 24LL;
}
while ( v3 != v2 );
}
return result;
}

original_code:
void setup_physical_allocator()
{
 const uint32_t mmap_size = *((uint32_t*) 0x2D00);
 MMapEntry* mmap_array = (MMapEntry*) 0x2D04;
 stack_init(&stack_2MIB);
 pool_4KIB = ((void*)0);
 uint64_t wasted_ram = 0;
 uint64_t allocatable_ram = 0;
 uint32_t usable = 0;
 for (uint32_t i = 0; i < mmap_size; ++i)
 {
  if (mmap_array[i].type != 1)
  {
   continue;
  }
  const uint64_t base_orig = mmap_array[i].base;
  const uint64_t length_orig = mmap_array[i].length;
  if (length_orig < (2ULL*(1024ULL*1024ULL)))
  {
   wasted_ram += length_orig;
   continue;
  }
  uint64_t base = (((base_orig) & 0xFFFFFFFFFFE00000) + ((((base_orig) & 0x1FFFFF) > 0) * (2ULL*(1024ULL*1024ULL))));
  uint64_t length = length_orig - (base - base_orig);
  if (length < (2ULL*(1024ULL*1024ULL)))
  {
   wasted_ram += length_orig;
   continue;
  }
  uint64_t count = 0;
  do
  {
   StackNode* node = (StackNode*) (base + 0xFFFF800000000000);
   stack_push(&stack_2MIB, node);
   ++count;
   allocatable_ram += (2ULL*(1024ULL*1024ULL));
   base += (2ULL*(1024ULL*1024ULL));
   length -= (2ULL*(1024ULL*1024ULL));
  } while (length >= (2ULL*(1024ULL*1024ULL)));
  ++usable;
  wasted_ram += length;
 }
}

canonical_original_code:
void setup_physical_allocator()
{
 unsigned int mmap_size = *((uint32_t*) 0x2D00);
 struct MMapEntry *mmap_array = (MMapEntry*) 0x2D04;
 stack_init(&stack_2MIB);
 pool_4KIB = ((void *)0);
 unsigned long wasted_ram = 0;
 unsigned long allocatable_ram = 0;
 unsigned int usable = 0;
 for (unsigned int i = 0; i < mmap_size; ++i)
 {
  if (mmap_array[i].type != 1)
  {
   continue;
  }
  unsigned long base_orig = mmap_array[i].base;
  unsigned long length_orig = mmap_array[i].length;
  if (length_orig < (2ULL*(1024ULL*1024ULL)))
  {
   wasted_ram += length_orig;
   continue;
  }
  unsigned long base = (((base_orig) & 0xFFFFFFFFFFE00000) + ((((base_orig) & 0x1FFFFF) > 0) * (2ULL*(1024ULL*1024ULL))));
  unsigned long length = length_orig - (base - base_orig);
  if (length < (2ULL*(1024ULL*1024ULL)))
  {
   wasted_ram += length_orig;
   continue;
  }
  unsigned long count = 0;
  do
  {
   struct _StackNode *node = (StackNode*) (base + 0xFFFF800000000000);
   stack_push(&stack_2MIB, node);
   ++count;
   allocatable_ram += (2ULL*(1024ULL*1024ULL));
   base += (2ULL*(1024ULL*1024ULL));
   length -= (2ULL*(1024ULL*1024ULL));
  } while (length >= (2ULL*(1024ULL*1024ULL)));
  ++usable;
  wasted_ram += length;
 }
}

variable_types: struct MMapEntry { unsigned long base; unsigned long length; unsigned int type; unsigned int ACPI; } *

variable_types:
usable
unsigned int

variable_types:
base
unsigned long

variable_types:
length
unsigned long

variable_types:
count
unsigned long

variable_types: struct _StackNode { struct _StackNode *next; } *


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

result = stack_pop(&stack_2MIB);
if ( result )
result += 0x800000000000LL;
return result;
}

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

result = stack_pop(&stack_2MIB);
if ( result )
result += 0x800000000000LL;
return result;
}

original_code:
void* phys_alloc_2MIB()
{
 void* retVal = stack_pop(&stack_2MIB);
 if (retVal == 0)
 {
  return ((void*)0);
 }
 void* final_value = ((void*)((uint64_t)(retVal) - 0xFFFF800000000000));
 ;
 return final_value;
}

canonical_original_code:
void *phys_alloc_2MIB()
{
 void *retVal = stack_pop(&stack_2MIB);
 if (retVal == 0)
 {
  return ((void *)0);
 }
 void *final_value = ((void*)((uint64_t)(retVal) - 0xFFFF800000000000));
 ;
 return final_value;
}


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

result = phys_alloc_2MIB();
if ( !result )
{
panic(a1);
result = 0LL;
}
return result;
}

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

result = func8();
if ( !result )
{
panic(a1);
result = 0LL;
}
return result;
}

original_code:
void* phys_alloc_2MIB_safe(const char* error)
{
 void* alloc_ptr = phys_alloc_2MIB();
 if (alloc_ptr == ((void*)0))
 {
  panic(error);
 }
 return alloc_ptr;
}

canonical_original_code:
void *phys_alloc_2MIB_safe(char *error)
{
 void *alloc_ptr = phys_alloc_2MIB();
 if (alloc_ptr == ((void *)0))
 {
  panic(error);
 }
 return alloc_ptr;
}

variable_types:
error
char *


decompiled_code:
__int64 __fastcall phys_free_2MIB(__int64 a1)
{
return stack_push(&stack_2MIB, (a1 - 0x800000000000LL) & 0xFFFFFFFFFFE00000LL);
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
return stack_push(&stack_2MIB, (a1 - 0x800000000000LL) & 0xFFFFFFFFFFE00000LL);
}

original_code:
void phys_free_2MIB(void* ptr)
{
 const uint64_t address = (uint64_t)((void*)((uint64_t)(ptr) + 0xFFFF800000000000));
 stack_push(&stack_2MIB, (void*)(((uint64_t)address) & 0xFFFFFFFFFFE00000));
}

canonical_original_code:
void phys_free_2MIB(void *ptr)
{
 unsigned long address = (uint64_t)((void*)((uint64_t)(ptr) + 0xFFFF800000000000));
 stack_push(&stack_2MIB, (void *)(((unsigned long)address) & 0xFFFFFFFFFFE00000));
}

variable_types:
address
unsigned long


decompiled_code:
__int64 phys_alloc_4KIB()
{
__int64 *v0; // rdi
__int64 v1; // rsi
__int64 v2; // rcx
__int64 v3; // rax
__int64 result; // rax

v0 = (__int64 *)pool_4KIB[0];
if ( !pool_4KIB[0] )
{
result = phys_alloc_2MIB();
pool_4KIB[0] = result;
if ( !result )
{
panic("ASSERT FAILED: src/arch/x86_64/virt_memory/phys_alloc.c:251");
result = pool_4KIB[0];
if ( !pool_4KIB[0] )
return result;
}
pool_4KIB[0] = result - 0x800000000000LL;
pool_init(result - 0x800000000000LL);
v0 = (__int64 *)pool_4KIB[0];
*(_BYTE *)(pool_4KIB[0] + 48LL) = 1;
}
pool_alloc(v0);
v1 = pool_4KIB[0];
if ( pool_empty((__int64 *)pool_4KIB[0]) )
{
v3 = *(_QWORD *)(v1 + 32);
*(_BYTE *)(v1 + 48) = 0;
*(_QWORD *)(v1 + 32) = 0LL;
*(_QWORD *)(v1 + 40) = 0LL;
pool_4KIB[0] = v3;
}
return v2 + 0x800000000000LL;
}

original_code:
void* phys_alloc_4KIB()
{
 if (pool_4KIB == ((void*)0))
 {
  pool_4KIB = (Pool*) phys_alloc_2MIB();
  if (!(pool_4KIB != ((void*)0))) panic("ASSERT FAILED: " "src/arch/x86_64/virt_memory/phys_alloc.c" ":" "251");
  if (pool_4KIB == ((void*)0))
  {
   return ((void*)0);
  }
  pool_4KIB = (Pool*) ((void*)((uint64_t)(pool_4KIB) + 0xFFFF800000000000));
  pool_init(pool_4KIB);
  pool_4KIB->on_list = 1;
 }
 uint64_t retVal = (uint64_t) pool_alloc(pool_4KIB);
 if (pool_empty(pool_4KIB))
 {
  Pool* p_next = pool_4KIB->next;
  pool_4KIB->on_list = 0;
  pool_4KIB->next = ((void*)0);
  pool_4KIB->prev = ((void*)0);
  pool_4KIB = p_next;
 }
 void* final_value = ((void*)((uint64_t)(retVal) - 0xFFFF800000000000));
 ;
 return final_value;
}

canonical_original_code:
void *phys_alloc_4KIB()
{
 if (pool_4KIB == ((void *)0))
 {
  pool_4KIB = (struct _Pool *) phys_alloc_2MIB();
  if (!(pool_4KIB != ((void *)0))) panic("ASSERT FAILED: " "src/arch/x86_64/virt_memory/phys_alloc.c" ":" "251");
  if (pool_4KIB == ((void *)0))
  {
   return ((void *)0);
  }
  pool_4KIB = (struct _Pool *) ((void *)((unsigned long)(pool_4KIB) + 0xFFFF800000000000));
  pool_init(pool_4KIB);
  pool_4KIB->on_list = 1;
 }
 unsigned long retVal = (uint64_t) pool_alloc(pool_4KIB);
 if (pool_empty(pool_4KIB))
 {
  struct _Pool *p_next = pool_4KIB->next;
  pool_4KIB->on_list = 0;
  pool_4KIB->next = ((void *)0);
  pool_4KIB->prev = ((void *)0);
  pool_4KIB = p_next;
 }
 void *final_value = ((void*)((uint64_t)(retVal) - 0xFFFF800000000000));
 ;
 return final_value;
}

variable_types:
retVal
unsigned long


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

result = phys_alloc_4KIB();
if ( !result )
{
panic(a1);
result = 0LL;
}
return result;
}

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

result = func11();
if ( !result )
{
panic(a1);
result = 0LL;
}
return result;
}

original_code:
void* phys_alloc_4KIB_safe(const char* error)
{
 void* alloc_ptr = phys_alloc_4KIB();
 if (alloc_ptr == ((void*)0))
 {
  panic(error);
 }
 return alloc_ptr;
}

canonical_original_code:
void *phys_alloc_4KIB_safe(char *error)
{
 void *alloc_ptr = phys_alloc_4KIB();
 if (alloc_ptr == ((void *)0))
 {
  panic(error);
 }
 return alloc_ptr;
}


original_code:
void phys_free_4KIB(void* ptr)
{
 const uint64_t address = (uint64_t)((void*)((uint64_t)(ptr) + 0xFFFF800000000000));
 Pool* pool = (Pool*) (((uint64_t)address) & 0xFFFFFFFFFFE00000);
 pool_free(pool, (void*)address);
 if (pool_4KIB == ((void*)0))
 {
  panic("phys_free_4KIB bad free");
 }
 if (pool->on_list && pool_full(pool))
 {
  if (pool_4KIB == pool)
  {
   pool_4KIB = pool_4KIB->next;
   if (pool_4KIB != ((void*)0))
   {
    pool_4KIB->prev = ((void*)0);
   }
  }
  else
  {
   Pool* p_next = pool->next;
   Pool* p_prev = pool->prev;
   if (p_next != ((void*)0))
   {
    p_next->prev = p_prev;
   }
   if (p_prev != ((void*)0))
   {
    p_prev->next = p_next;
   }
  }
  pool_init(pool);
  phys_free_2MIB(pool);
 }
 else if (!pool->on_list)
 {
  pool_4KIB->prev = pool;
  pool->next = pool_4KIB;
  pool->prev = ((void*)0);
  pool_4KIB = pool;
  pool_4KIB->on_list = 1;
 }
}

canonical_original_code:
void phys_free_4KIB(void *ptr)
{
 unsigned long address = (uint64_t)((void*)((uint64_t)(ptr) + 0xFFFF800000000000));
 struct _Pool *pool = (Pool*) (((uint64_t)address) & 0xFFFFFFFFFFE00000);
 pool_free(pool, (void *)address);
 if (pool_4KIB == ((void *)0))
 {
  panic("phys_free_4KIB bad free");
 }
 if (pool->on_list && pool_full(pool))
 {
  if (pool_4KIB == pool)
  {
   pool_4KIB = pool_4KIB->next;
   if (pool_4KIB != ((void *)0))
   {
    pool_4KIB->prev = ((void *)0);
   }
  }
  else
  {
   struct _Pool *p_next = pool->next;
   struct _Pool *p_prev = pool->prev;
   if (p_next != ((void *)0))
   {
    p_next->prev = p_prev;
   }
   if (p_prev != ((void *)0))
   {
    p_prev->next = p_next;
   }
  }
  pool_init(pool);
  phys_free_2MIB(pool);
 }
 else if (!pool->on_list)
 {
  pool_4KIB->prev = pool;
  pool->next = pool_4KIB;
  pool->prev = ((void *)0);
  pool_4KIB = pool;
  pool_4KIB->on_list = 1;
 }
}


original_code:
void list_init(linked_list_t* list, void* (*ram_alloc)(uint64_t), void (*destroy)(void*))
{
 list->size = 0;
 list->destroy = destroy;
 list->ram_alloc = ram_alloc;
 list->head = ((void*)0);
 list->tail = ((void*)0);
}

canonical_original_code:
void list_init(struct LinkedList *list, void *(*ram_alloc)(unsigned long), void (*destroy)(void *))
{
 list->size = 0;
 list->destroy = destroy;
 list->ram_alloc = ram_alloc;
 list->head = ((void *)0);
 list->tail = ((void *)0);
}

variable_types: struct LinkedList { int size; void (*destroy)(void *data); void *(*ram_alloc)(unsigned long size); struct ListElement { void *data; struct ListElement *next; struct ListElement *prev; } *head; struct ListElement *tail; } *

variable_types:
destroy
void (*)(void *)

user_defined_types: struct LinkedList { int size; void (*destroy)(void *data); void *(*ram_alloc)(unsigned long size); struct ListElement *head; struct ListElement *tail; }


original_code:
int32_t list_insert_next(linked_list_t* list, list_element_t* element, const void* data)
{
 list_element_t* new_element;
 if ((new_element = (list_element_t *)list->ram_alloc(sizeof(list_element_t))) == ((void*)0))
 {
  return -1;
 }
 new_element->data = (void *)data;
 if (element == ((void*)0))
 {
  if (((list)->size) == 0)
  {
   list->tail = new_element;
  }
  if (list->head != ((void*)0))
   list->head->prev = new_element;
  new_element->next = list->head;
  list->head = new_element;
  new_element->prev = ((void*)0);
 } else {
  if (element->next == ((void*)0))
  {
   list->tail = new_element;
  }
  new_element->prev = element;
  element->next->prev = new_element;
  new_element->next = element->next;
  element->next = new_element;
 }
 list->size++;
 return 0;
}

canonical_original_code:
int list_insert_next(struct LinkedList *list, struct ListElement *element, void *data)
{
 struct ListElement *new_element;
 if ((new_element = (struct ListElement *)list->ram_alloc(sizeof(list_element_t))) == ((void *)0))
 {
  return -1;
 }
 new_element->data = (void *)data;
 if (element == ((void *)0))
 {
  if (((list)->size) == 0)
  {
   list->tail = new_element;
  }
  if (list->head != ((void *)0))
   list->head->prev = new_element;
  new_element->next = list->head;
  list->head = new_element;
  new_element->prev = ((void *)0);
 } else {
  if (element->next == ((void *)0))
  {
   list->tail = new_element;
  }
  new_element->prev = element;
  element->next->prev = new_element;
  new_element->next = element->next;
  element->next = new_element;
 }
 list->size++;
 return 0;
}

variable_types:
element
struct ListElement *

variable_types:
data
void *

variable_types: struct ListElement { void *data; struct ListElement *next; struct ListElement *prev; } *

return_type: int


original_code:
int32_t list_remove_next(linked_list_t* list, list_element_t* element, void** data)
{
 list_element_t* old_element;
 if (((list)->size) == 0)
 {
  return -1;
 }
 if (element == ((void*)0))
 {
  if (data != ((void*)0))
   *data = list->head->data;
  old_element = list->head;
  list->head = list->head->next;
  if (((list)->size) == 1)
  {
   list->tail = ((void*)0);
  }
 } else {
  if (element->next == ((void*)0))
  {
   return -1;
  }
  if (data != ((void*)0))
   *data = element->next->data;
  old_element = element->next;
  element->next = element->next->next;
  if (element->next == ((void*)0))
  {
   list->tail = element;
  }
 }
 list->destroy(old_element);
 list->size--;
 return 0;
}

canonical_original_code:
int list_remove_next(struct LinkedList *list, struct ListElement *element, void **data)
{
 struct ListElement *old_element;
 if (((list)->size) == 0)
 {
  return -1;
 }
 if (element == ((void *)0))
 {
  if (data != ((void *)0))
   *data = list->head->data;
  old_element = list->head;
  list->head = list->head->next;
  if (((list)->size) == 1)
  {
   list->tail = ((void *)0);
  }
 } else {
  if (element->next == ((void *)0))
  {
   return -1;
  }
  if (data != ((void *)0))
   *data = element->next->data;
  old_element = element->next;
  element->next = element->next->next;
  if (element->next == ((void *)0))
  {
   list->tail = element;
  }
 }
 list->destroy(old_element);
 list->size--;
 return 0;
}

variable_types:
data
void **

user_defined_types: struct ListElement { void *data; struct ListElement *next; struct ListElement *prev; }


original_code:
void list_destroy(linked_list_t* list)
{
 void* data;
 while (((list)->size) > 0)
 {
  if (list_remove_next(list, ((void*)0), (void **)&data) == 0 && list->destroy != ((void*)0))
  {
   list->destroy(data);
  }
 }
 list->size = 0;
 list->destroy = ((void*)0);
 list->head = ((void*)0);
 list->tail = ((void*)0);
}

canonical_original_code:
void list_destroy(struct LinkedList *list)
{
 void *data;
 while (((list)->size) > 0)
 {
  if (list_remove_next(list, ((void *)0), (void **)&data) == 0 && list->destroy != ((void *)0))
  {
   list->destroy(data);
  }
 }
 list->size = 0;
 list->destroy = ((void *)0);
 list->head = ((void *)0);
 list->tail = ((void *)0);
}


decompiled_code:
bool __fastcall queue_empty(_QWORD *a1)
{
return *a1 == 0LL;
}

canonical_decompiled_code:
bool __fastcall func3(_QWORD *a1)
{
return *a1 == 0LL;
}

original_code:
uint8_t queue_empty(const Queue* q)
{
 return q->size == 0;
}

canonical_original_code:
unsigned char queue_empty(struct Queue *q)
{
 return q->size == 0;
}

variable_types: struct Queue { unsigned long size; struct _QueueNode { struct _QueueNode *next; void *data; } *head; struct _QueueNode *tail; } *

user_defined_types: struct Queue { unsigned long size; struct _QueueNode *head; struct _QueueNode *tail; }


decompiled_code:
__int64 __fastcall queue_size(__int64 a1)
{
return *(_QWORD *)a1;
}

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

original_code:
uint64_t queue_size(const Queue* q)
{
 return q->size;
}

canonical_original_code:
unsigned long queue_size(struct Queue *q)
{
 return q->size;
}

return_type: unsigned long


decompiled_code:
__int16 text_get_x()
{
return text_mode_info;
}

canonical_decompiled_code:
__int16 func1()
{
return text_mode_info;
}

original_code:
uint16_t text_get_x()
{
 return text_mode_info.x;
}

canonical_original_code:
unsigned short text_get_x()
{
 return text_mode_info.x;
}

return_type: unsigned short


decompiled_code:
__int16 text_get_y()
{
return *(&text_mode_info + 1);
}

canonical_decompiled_code:
__int16 func2()
{
return *(&text_mode_info + 1);
}

original_code:
uint16_t text_get_y()
{
 return text_mode_info.y;
}

canonical_original_code:
unsigned short text_get_y()
{
 return text_mode_info.y;
}


decompiled_code:
char __fastcall text_mode_string(char *a1)
{
char v1; // di
char (__fastcall *v2)(char); // r10
char result; // al
__int64 v4; // r9

v1 = *a1;
if ( v1 )
{
v2 = text_mode_char;
do
{
result = v2(v1);
v1 = *(_BYTE *)(v4 + 1);
}
while ( v1 );
}
return result;
}

canonical_decompiled_code:
char __fastcall func4(char *a1)
{
char v1; // di
char (__fastcall *v2)(char); // r10
char result; // al
__int64 v4; // r9

v1 = *a1;
if ( v1 )
{
v2 = text_mode_char;
do
{
result = v2(v1);
v1 = *(_BYTE *)(v4 + 1);
}
while ( v1 );
}
return result;
}

original_code:
void text_mode_string(const char* str)
{
 while (*str != '\0')
 {
  text_mode_char(*str);
  ++str;
 }
}

canonical_original_code:
void text_mode_string(char *str)
{
 while (*str != '\0')
 {
  text_mode_char(*str);
  ++str;
 }
}

variable_types:
str
char *


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

for ( result = -4198400LL; result != -4194400; *(_WORD *)(result - 2) = 0 )
result += 2LL;
return result;
}

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

for ( result = -4198400LL; result != -4194400; *(_WORD *)(result - 2) = 0 )
result += 2LL;
return result;
}

original_code:
void clear_screen()
{
 uint16_t* video = (uint16_t*)0xFFFFFFFFFFBFF000;
 for (int i = 0; i < 80*25; ++i)
 {
  video[i] = 0;
 }
}

canonical_original_code:
void clear_screen()
{
 unsigned short *video = (uint16_t*)0xFFFFFFFFFFBFF000;
 for (int i = 0; i < 80*25; ++i)
 {
  video[i] = 0;
 }
}

variable_types:
video
unsigned short *

variable_types: i int


decompiled_code:
char __fastcall text_mode_char(char a1)
{
unsigned __int16 v1; // dx
__int16 v2; // r8
_BYTE *v3; // rcx
__int16 *v4; // rax
int v5; // ecx
_BYTE *v6; // rdx

v1 = *(&text_mode_info + 1);
if ( a1 == 10 || a1 == 13 )
{
LABEL_5:
text_mode_info = 0;
LOBYTE(v4) = v1 + 1;
*(&text_mode_info + 1) = v1 + 1;
if ( (unsigned __int16)(v1 + 1) > 0x18u )
{
v4 = &text_mode_info + 1;
*(&text_mode_info + 1) = 0;
}
return (char)v4;
}
if ( a1 != 8 )
{
v2 = text_mode_info + 1;
v3 = (_BYTE *)(video + 2LL * ((unsigned __int16)text_mode_info + 80 * v1));
*v3 = a1;
LOBYTE(v4) = *((_BYTE *)&text_mode_info + 4);
v3[1] = (_BYTE)v4;
if ( (unsigned __int16)v2 <= 0x4Fu )
{
text_mode_info = v2;
return (char)v4;
}
goto LABEL_5;
}
if ( text_mode_info )
{
LOWORD(v4) = text_mode_info - 1;
v5 = (unsigned __int16)(text_mode_info - 1);
}
else
{
if ( v1 )
{
--v1;
v5 = 79;
*(&text_mode_info + 1) = v1;
}
else
{
v5 = 79;
}
LOWORD(v4) = 79;
}
text_mode_info = (__int16)v4;
v6 = (_BYTE *)(video + 2LL * (v5 + 80 * v1));
*v6 = 32;
LOBYTE(v4) = *((_BYTE *)&text_mode_info + 4);
v6[1] = (_BYTE)v4;
return (char)v4;
}

canonical_decompiled_code:
char __fastcall func3(char a1)
{
unsigned __int16 v1; // dx
__int16 v2; // r8
_BYTE *v3; // rcx
__int16 *v4; // rax
int v5; // ecx
_BYTE *v6; // rdx

v1 = *(&text_mode_info + 1);
if ( a1 == 10 || a1 == 13 )
{
LABEL_5:
text_mode_info = 0;
LOBYTE(v4) = v1 + 1;
*(&text_mode_info + 1) = v1 + 1;
if ( (unsigned __int16)(v1 + 1) > 0x18u )
{
v4 = &text_mode_info + 1;
*(&text_mode_info + 1) = 0;
}
return (char)v4;
}
if ( a1 != 8 )
{
v2 = text_mode_info + 1;
v3 = (_BYTE *)(video + 2LL * ((unsigned __int16)text_mode_info + 80 * v1));
*v3 = a1;
LOBYTE(v4) = *((_BYTE *)&text_mode_info + 4);
v3[1] = (_BYTE)v4;
if ( (unsigned __int16)v2 <= 0x4Fu )
{
text_mode_info = v2;
return (char)v4;
}
goto LABEL_5;
}
if ( text_mode_info )
{
LOWORD(v4) = text_mode_info - 1;
v5 = (unsigned __int16)(text_mode_info - 1);
}
else
{
if ( v1 )
{
--v1;
v5 = 79;
*(&text_mode_info + 1) = v1;
}
else
{
v5 = 79;
}
LOWORD(v4) = 79;
}
text_mode_info = (__int16)v4;
v6 = (_BYTE *)(video + 2LL * (v5 + 80 * v1));
*v6 = 32;
LOBYTE(v4) = *((_BYTE *)&text_mode_info + 4);
v6[1] = (_BYTE)v4;
return (char)v4;
}

original_code:
void text_mode_char(const char c)
{
 switch (c)
 {
  case '\r':
  case '\n':
   {
    text_mode_info.x = 0;
    ++text_mode_info.y;
    if (text_mode_info.y >= 25)
     text_mode_info.y = 0;
   }
   return;
  case '\b':
   {
    if (text_mode_info.x == 0)
    {
     text_mode_info.x = 80;
     if (text_mode_info.y > 0)
     {
      --text_mode_info.y;
     }
    }
    --text_mode_info.x;
    const uint32_t offset = text_mode_info.y*80 + text_mode_info.x;
    video[offset].c = ' ';
    video[offset].color = text_mode_info.color;
   }
   return;
 }
 const uint32_t offset = text_mode_info.y*80 + text_mode_info.x;
 video[offset].c = c;
 video[offset].color = text_mode_info.color;
 ++text_mode_info.x;
 if (text_mode_info.x >= 80)
 {
  text_mode_info.x = 0;
  ++text_mode_info.y;
  if (text_mode_info.y >= 25)
  {
   text_mode_info.y = 0;
  }
 }
}

canonical_original_code:
void text_mode_char(char c)
{
 switch (c)
 {
  case '\r':
  case '\n':
   {
    text_mode_info.x = 0;
    ++text_mode_info.y;
    if (text_mode_info.y >= 25)
     text_mode_info.y = 0;
   }
   return;
  case '\b':
   {
    if (text_mode_info.x == 0)
    {
     text_mode_info.x = 80;
     if (text_mode_info.y > 0)
     {
      --text_mode_info.y;
     }
    }
    --text_mode_info.x;
    unsigned int offset = text_mode_info.y*80 + text_mode_info.x;
    video[offset].c = ' ';
    video[offset].color = text_mode_info.color;
   }
   return;
 }
 unsigned int offset = text_mode_info.y*80 + text_mode_info.x;
 video[offset].c = c;
 video[offset].color = text_mode_info.color;
 ++text_mode_info.x;
 if (text_mode_info.x >= 80)
 {
  text_mode_info.x = 0;
  ++text_mode_info.y;
  if (text_mode_info.y >= 25)
  {
   text_mode_info.y = 0;
  }
 }
}

variable_types: c char

variable_types:
offset
unsigned int


strlen

decompiled_code:
__int64 __fastcall strlen(_BYTE *a1)
{
__int64 result; // rax

result = 0LL;
if ( *a1 )
{
do
++result;
while ( a1[result] );
}
return result;
}

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

result = 0LL;
if ( *a1 )
{
do
++result;
while ( a1[result] );
}
return result;
}

original_code:
uint64_t strlen(const char* str)
{
 uint64_t length = 0;
 while (*str != 0)
 {
  ++length;
  ++str;
 }
 return length;
}

canonical_original_code:
unsigned long strlen(char *str)
{
 unsigned long length = 0;
 while (*str != 0)
 {
  ++length;
  ++str;
 }
 return length;
}

variable_types:
length
unsigned long


streq

decompiled_code:
bool __fastcall streq(_BYTE *a1, _BYTE *a2)
{
while ( 1 )
{
if ( !*a1 )
return *a2 == 0;
if ( !*a2 || *a2 != *a1 )
break;
++a1;
++a2;
}
return 0;
}

canonical_decompiled_code:
bool __fastcall func1(_BYTE *a1, _BYTE *a2)
{
while ( 1 )
{
if ( !*a1 )
return *a2 == 0;
if ( !*a2 || *a2 != *a1 )
break;
++a1;
++a2;
}
return 0;
}

original_code:
uint8_t streq(const char* str1, const char* str2)
{
 while (*str1 != 0 && *str2 != 0)
 {
  const char c1 = *str1;
  const char c2 = *str2;
  if (c1 != c2)
  {
   return 0;
  }
  ++str1;
  ++str2;
 }
 return *str1 == 0 && *str2 == 0;
}

canonical_original_code:
unsigned char streq(char *str1, char *str2)
{
 while (*str1 != 0 && *str2 != 0)
 {
  char c1 = *str1;
  char c2 = *str2;
  if (c1 != c2)
  {
   return 0;
  }
  ++str1;
  ++str2;
 }
 return *str1 == 0 && *str2 == 0;
}


decompiled_code:
void serial_handler()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
static void serial_handler(uint64_t vector, uint64_t code)
{
 ((void)vector);
 ((void)code);
}

canonical_original_code:
static void serial_handler(unsigned long vector, unsigned long code)
{
 ((void)vector);
 ((void)code);
}

variable_types:
vector
unsigned long

variable_types:
code
unsigned long


decompiled_code:
void __noreturn spurious_handler()
{
kprintf("SPURIOUS\n");
__halt();
}

canonical_decompiled_code:
void __noreturn func1()
{
kprintf("SPURIOUS\n");
__halt();
}

original_code:
static void spurious_handler(uint64_t vector, uint64_t code)
{
 ((void)vector);
 ((void)code);
 kprintf("SPURIOUS\n");
 __asm__ volatile("hlt");
 pic_acknowledge(vector);
}

canonical_original_code:
static void spurious_handler(unsigned long vector, unsigned long code)
{
 ((void)vector);
 ((void)code);
 kprintf("SPURIOUS\n");
 __asm__ volatile("hlt");
 pic_acknowledge(vector);
}