read

decompiled_code:
unsigned __int64 __fastcall read(int a1, char *a2, unsigned __int64 a3)
{
unsigned __int64 v3; // rbp
__int64 v4; // rax
_QWORD *v5; // r13
unsigned __int64 result; // rax
__int64 v8; // rcx
unsigned __int64 v9; // rbx
unsigned __int64 v10; // rdx
unsigned __int64 v11; // rbx
unsigned __int64 v12; // [rsp+0h] [rbp-48h]

v3 = a3;
v4 = *(_QWORD *)(current + 8LL * a1 + 8);
v12 = *(_QWORD *)(v4 + 16);
v5 = *(_QWORD **)v4;
result = 0LL;
if ( v12 <= v5[4] )
{
if ( a3 )
{
do
{
v8 = (*(__int64 (__fastcall **)(_QWORD *, _QWORD))(v5[13] + 16LL))(
v5,
*(_QWORD *)(*(_QWORD *)(current + 8LL * a1 + 8) + 16LL));
if ( !v8 )
break;
v9 = *(_QWORD *)(*v5 + 8LL);
v10 = *(_QWORD *)(*(_QWORD *)(current + 8LL * a1 + 8) + 16LL) % v9;
v11 = v9 - v10;
if ( v11 > v3 )
LOWORD(v11) = v3;
memcpy(a2, (const void *)(*(_QWORD *)(v8 + 24) + v10), (unsigned __int16)v11);
*(_QWORD *)(*(_QWORD *)(current + 8LL * a1 + 8) + 16LL) += (unsigned __int16)v11;
a2 += (unsigned __int16)v11;
v3 -= (unsigned __int16)v11;
}
while ( v3 );
}
result = *(_QWORD *)(*(_QWORD *)(current + 8LL * a1 + 8) + 16LL) - v12;
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func1(int a1, char *a2, unsigned __int64 a3)
{
unsigned __int64 v3; // rbp
__int64 v4; // rax
_QWORD *v5; // r13
unsigned __int64 result; // rax
__int64 v8; // rcx
unsigned __int64 v9; // rbx
unsigned __int64 v10; // rdx
unsigned __int64 v11; // rbx
unsigned __int64 v12; // [rsp+0h] [rbp-48h]

v3 = a3;
v4 = *(_QWORD *)(current + 8LL * a1 + 8);
v12 = *(_QWORD *)(v4 + 16);
v5 = *(_QWORD **)v4;
result = 0LL;
if ( v12 <= v5[4] )
{
if ( a3 )
{
do
{
v8 = (*(__int64 (__fastcall **)(_QWORD *, _QWORD))(v5[13] + 16LL))(
v5,
*(_QWORD *)(*(_QWORD *)(current + 8LL * a1 + 8) + 16LL));
if ( !v8 )
break;
v9 = *(_QWORD *)(*v5 + 8LL);
v10 = *(_QWORD *)(*(_QWORD *)(current + 8LL * a1 + 8) + 16LL) % v9;
v11 = v9 - v10;
if ( v11 > v3 )
LOWORD(v11) = v3;
memcpy(a2, (const void *)(*(_QWORD *)(v8 + 24) + v10), (unsigned __int16)v11);
*(_QWORD *)(*(_QWORD *)(current + 8LL * a1 + 8) + 16LL) += (unsigned __int16)v11;
a2 += (unsigned __int16)v11;
v3 -= (unsigned __int16)v11;
}
while ( v3 );
}
result = *(_QWORD *)(*(_QWORD *)(current + 8LL * a1 + 8) + 16LL) - v12;
}
return result;
}

original_code:
size_t read(s32 fd, void *buf, size_t count)
{
 struct inode *inode;
 struct bhead *block;
 size_t rbytes;
 off_t offset;
 u16 len;
 if (current->fds[fd]->pos > current->fds[fd]->inode->filesize)
  return 0;
 inode = current->fds[fd]->inode;
 rbytes = current->fds[fd]->pos;
 while (count) {
  block = inode->ops->block_read(inode, current->fds[fd]->pos);
  if (block == ((void *)0))
   break;
  offset = current->fds[fd]->pos % inode->sb->blocksize;
  len = ((inode->sb->blocksize - offset) < (count) ? (inode->sb->blocksize - offset) : (count));
  memcpy(buf, block->data + offset, len);
  current->fds[fd]->pos += len;
  count -= len;
  buf += len;
 }
 return current->fds[fd]->pos - rbytes;
}

canonical_original_code:
unsigned long read(int fd, void *buf, unsigned long count)
{
 struct inode *inode;
 struct bhead *block;
 unsigned long rbytes;
 long offset;
 unsigned short len;
 if (current->fds[fd]->pos > current->fds[fd]->inode->filesize)
  return 0;
 inode = current->fds[fd]->inode;
 rbytes = current->fds[fd]->pos;
 while (count) {
  block = inode->ops->block_read(inode, current->fds[fd]->pos);
  if (block == ((void *)0))
   break;
  offset = current->fds[fd]->pos % inode->sb->blocksize;
  len = ((inode->sb->blocksize - offset) < (count) ? (inode->sb->blocksize - offset) : (count));
  memcpy(buf, block->data + offset, len);
  current->fds[fd]->pos += len;
  count -= len;
  buf += len;
 }
 return current->fds[fd]->pos - rbytes;
}

variable_types:
buf
void *

variable_types:
count
unsigned long

variable_types: struct inode { struct super { struct dev_t { unsigned int major; unsigned int minor; } dev; unsigned long blocksize; unsigned long blocksizephys; struct list_head { struct list_head *prev; } bcache; struct list_head icache; struct super_ops { struct super *(*super_read)(struct dev_t *dev); struct inode *(*inode_read)(struct super *sb, unsigned long inum); } *ops; unsigned long magic; unsigned long time; struct inode *mounted; unsigned int flags; void *priv; } *sb; unsigned long num; unsigned short mode; unsigned char nlinks; unsigned int uid; unsigned int gid; unsigned long filesize; unsigned long nblocks; unsigned long atime; unsigned long mtime; unsigned long ctime; unsigned int flags; unsigned int count; struct list_head l; struct super *covered; struct inode_ops { struct inode *(*lookup)(struct inode *, char *, unsigned char); unsigned long (*bmap)(struct inode *, long); struct bhead { struct list_head l; unsigned long bnum; void *data; unsigned char count; } *(*block_read)(struct inode *, long); } *ops; void *priv; } *

variable_types: struct bhead { struct list_head { struct list_head *prev; } l; unsigned long bnum; void *data; unsigned char count; } *

variable_types:
rbytes
unsigned long

variable_types:
len
unsigned short


open

repo: acceso/jOS

decompiled_code:
__int64 __fastcall open(__int64 a1, __int16 a2, __int16 a3)
{
__int64 v4; // r13
_QWORD *v5; // rax
unsigned int v6; // ebx
__int64 v8; // rax
__int64 v9; // rdx

v4 = current;
v5 = (_QWORD *)(current + 8LL);
v6 = 0;
while ( *v5 )
{
++v6;
++v5;
if ( v6 == 32 )
return 0xFFFFFFFFLL;
}
if ( v6 > 0x1E )
return 0xFFFFFFFFLL;
*(_QWORD *)(v4 + 8LL * v6 + 8) = file_get();
v8 = current + 8LL * v6;
v9 = *(_QWORD *)(v8 + 8);
if ( !v9 )
return 0xFFFFFFFFLL;
*(_WORD *)(v9 + 10) = a3;
*(_WORD *)(*(_QWORD *)(v8 + 8) + 12LL) = a2;
*(_QWORD *)(*(_QWORD *)(v8 + 8) + 16LL) = 0LL;
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int16 a2, __int16 a3)
{
__int64 v4; // r13
_QWORD *v5; // rax
unsigned int v6; // ebx
__int64 v8; // rax
__int64 v9; // rdx

v4 = current;
v5 = (_QWORD *)(current + 8LL);
v6 = 0;
while ( *v5 )
{
++v6;
++v5;
if ( v6 == 32 )
return 0xFFFFFFFFLL;
}
if ( v6 > 0x1E )
return 0xFFFFFFFFLL;
*(_QWORD *)(v4 + 8LL * v6 + 8) = file_get();
v8 = current + 8LL * v6;
v9 = *(_QWORD *)(v8 + 8);
if ( !v9 )
return 0xFFFFFFFFLL;
*(_WORD *)(v9 + 10) = a3;
*(_WORD *)(*(_QWORD *)(v8 + 8) + 12LL) = a2;
*(_QWORD *)(*(_QWORD *)(v8 + 8) + 16LL) = 0LL;
return v6;
}

original_code:
s8 open(const char *path, u16 flags, u16 mode)
{
 u32 fd;
 for (fd = 0; fd < 32; fd++)
  if (current->fds[fd] == ((void *)0))
   break;
 if (fd >= 32 - 1)
  return -1;
 current->fds[fd] = file_get(path);
 if (current->fds[fd] == ((void *)0))
  return -1;
 current->fds[fd]->mode = mode;
 current->fds[fd]->flags = flags;
 current->fds[fd]->pos = 0;
 return fd;
}

canonical_original_code:
char open(char *path, unsigned short flags, unsigned short mode)
{
 unsigned int fd;
 for (fd = 0; fd < 32; fd++)
  if (current->fds[fd] == ((void *)0))
   break;
 if (fd >= 32 - 1)
  return -1;
 current->fds[fd] = file_get(path);
 if (current->fds[fd] == ((void *)0))
  return -1;
 current->fds[fd]->mode = mode;
 current->fds[fd]->flags = flags;
 current->fds[fd]->pos = 0;
 return fd;
}

variable_types:
path
char *

variable_types:
flags
unsigned short

variable_types:
mode
unsigned short

variable_types:
fd
unsigned int

return_type: char


bitset

decompiled_code:
void __fastcall bitset(signed __int64 *a1, unsigned __int64 a2)
{
_bittestandset64(a1, a2);
}

canonical_decompiled_code:
void __fastcall func0(signed __int64 *a1, unsigned __int64 a2)
{
_bittestandset64(a1, a2);
}

original_code:
inline static void bitset(u64 *word, u64 pos)
{
 asm volatile(
  "bts %[bit], %[reg]\n"
  : [reg] "+m"(*word)
  : [bit] "ir" (pos)
  : "cc" );
}

canonical_original_code:
inline static void bitset(unsigned long *word, unsigned long pos)
{
 asm volatile(
  "bts %[bit], %[reg]\n"
  : [reg] "+m"(*word)
  : [bit] "ir" (pos)
  : "cc" );
}

variable_types:
word
unsigned long *

variable_types:
pos
unsigned long

return_type: void


bitclear

decompiled_code:
void __fastcall bitclear(signed __int64 *a1, unsigned __int64 a2)
{
_bittestandreset64(a1, a2);
}

canonical_decompiled_code:
void __fastcall func1(signed __int64 *a1, unsigned __int64 a2)
{
_bittestandreset64(a1, a2);
}

original_code:
inline static void bitclear(u64 *word, u64 pos)
{
 asm volatile(
  "btr %[bit], %[reg]\n"
  : [reg] "+m"(*word)
  : [bit] "ir" (pos)
  : "cc" );
}

canonical_original_code:
inline static void bitclear(unsigned long *word, unsigned long pos)
{
 asm volatile(
  "btr %[bit], %[reg]\n"
  : [reg] "+m"(*word)
  : [bit] "ir" (pos)
  : "cc" );
}


decompiled_code:
__int64 __fastcall pfn_to_addr(__int64 a1)
{
return a1 << 21;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return a1 << 21;
}

original_code:
static void *pfn_to_addr(u64 pfn)
{
 return (void *)(pfn * 0x200000);
}

canonical_original_code:
static void *pfn_to_addr(unsigned long pfn)
{
 return (void *)(pfn * 0x200000);
}

variable_types:
pfn
unsigned long

return_type: void *


decompiled_code:
unsigned __int64 __fastcall addr_to_pfn(unsigned __int64 a1)
{
return a1 >> 21;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func3(unsigned __int64 a1)
{
return a1 >> 21;
}

original_code:
static inline u64 addr_to_pfn(void *addr)
{
 return (u64)addr >> 21;
}

canonical_original_code:
static inline unsigned long addr_to_pfn(void *addr)
{
 return (unsigned long)addr >> 21;
}

variable_types:
addr
void *

return_type: unsigned long


decompiled_code:
void __fastcall pf_mark_free_addr(unsigned __int64 a1)
{
unsigned __int64 v1; // rax

v1 = addr_to_pfn(a1);
if ( v1 <= 0x83F )
bitclear(&zone[v1 >> 6], v1 & 0x3F);
}

canonical_decompiled_code:
void __fastcall func4(unsigned __int64 a1)
{
unsigned __int64 v1; // rax

v1 = func3(a1);
if ( v1 <= 0x83F )
func1(&zone[v1 >> 6], v1 & 0x3F);
}

original_code:
static void pf_mark_free_addr(void *addr)
{
 u64 idx = addr_to_pfn(addr) >> 6;
 if (idx > 32)
  return;
 bitclear(&zone.memmap[idx], (u64)addr_to_pfn(addr) & 0b111111);
}

canonical_original_code:
static void pf_mark_free_addr(void *addr)
{
 unsigned long idx = addr_to_pfn(addr) >> 6;
 if (idx > 32)
  return;
 bitclear(&zone.memmap[idx], (unsigned long)addr_to_pfn(addr) & 0b111111);
}

variable_types:
idx
unsigned long


decompiled_code:
__int64 __fastcall mem_usable(unsigned __int64 a1)
{
unsigned __int64 v1; // rdx
__int64 result; // rax
int v3; // eax

v1 = a1 + 0x200000;
if ( a1 + 0x200000 < (unsigned __int64)&sok || (result = 0LL, a1 >= (unsigned __int64)&eok + 0x800000000000LL) )
{
v3 = -2;
LOBYTE(v3) = a1 <= 0xFFFFFFFE;
LOBYTE(v1) = v1 > 0xFEBFFFFF;
result = (unsigned int)v1 & v3 ^ 1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned __int64 a1)
{
unsigned __int64 v1; // rdx
__int64 result; // rax
int v3; // eax

v1 = a1 + 0x200000;
if ( a1 + 0x200000 < (unsigned __int64)&sok || (result = 0LL, a1 >= (unsigned __int64)&eok + 0x800000000000LL) )
{
v3 = -2;
LOBYTE(v3) = a1 <= 0xFFFFFFFE;
LOBYTE(v1) = v1 > 0xFEBFFFFF;
result = (unsigned int)v1 & v3 ^ 1;
}
return result;
}

original_code:
static s8 mem_usable(void *addr)
{
 if (addr + 0x200000 >= (void *)&sok &&
  addr < (void *)((void *)(((u64)(void *)&eok) - 0xffff800000000000)))
   return 0;
 if (addr + 0x200000 >= (void *)0xfec00000 &&
  addr < (void *)0xffffffff)
   return 0;
 return 1;
}

canonical_original_code:
static char mem_usable(void *addr)
{
 if (addr + 0x200000 >= (void *)&sok &&
  addr < (void *)((void *)(((unsigned long)(void *)&eok) - 0xffff800000000000)))
   return 0;
 if (addr + 0x200000 >= (void *)0xfec00000 &&
  addr < (void *)0xffffffff)
   return 0;
 return 1;
}


decompiled_code:
__int64 __fastcall mm_make_range_usable(__int64 a1, unsigned __int64 a2)
{
__int64 result; // rax
__int64 v3; // rax
unsigned __int64 v4; // rsi
unsigned __int64 v5; // rbx
unsigned __int64 v6; // r13
__int64 v7; // rbp

result = 0LL;
if ( a2 > 0x1FFFFF )
{
v3 = align_to(a1, 0x200000LL);
v4 = a1 + a2;
if ( v4 < v3 + 0x200000 )
{
v7 = 0LL;
}
else
{
v5 = v3;
v6 = v3 + ((v4 - 0x200000 - v3) & 0xFFFFFFFFFFE00000LL) + 0x200000;
v7 = 0LL;
do
{
if ( (unsigned __int8)mem_usable(v5) )
{
pf_mark_free_addr(v5);
++v7;
}
v5 += 0x200000LL;
}
while ( v5 != v6 );
}
result = v7;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, unsigned __int64 a2)
{
__int64 result; // rax
__int64 v3; // rax
unsigned __int64 v4; // rsi
unsigned __int64 v5; // rbx
unsigned __int64 v6; // r13
__int64 v7; // rbp

result = 0LL;
if ( a2 > 0x1FFFFF )
{
v3 = align_to(a1, 0x200000LL);
v4 = a1 + a2;
if ( v4 < v3 + 0x200000 )
{
v7 = 0LL;
}
else
{
v5 = v3;
v6 = v3 + ((v4 - 0x200000 - v3) & 0xFFFFFFFFFFE00000LL) + 0x200000;
v7 = 0LL;
do
{
if ( (unsigned __int8)func5(v5) )
{
func4(v5);
++v7;
}
v5 += 0x200000LL;
}
while ( v5 != v6 );
}
result = v7;
}
return result;
}

original_code:
static s64 mm_make_range_usable(void *addr, u64 len)
{
 void *p;
 u64 npages = 0;
 if (len < 0x200000)
  return 0;
 p = align_to(addr, 0x200000);
 do {
  if (p + 0x200000 > addr + len)
   return npages;
  if (mem_usable(p)) {
   pf_mark_free_addr(p);
   npages++;
  }
  p += 0x200000;
 } while (p <= addr + len);
 return npages;
}

canonical_original_code:
static long mm_make_range_usable(void *addr, unsigned long len)
{
 void *p;
 unsigned long npages = 0;
 if (len < 0x200000)
  return 0;
 p = align_to(addr, 0x200000);
 do {
  if (p + 0x200000 > addr + len)
   return npages;
  if (mem_usable(p)) {
   pf_mark_free_addr(p);
   npages++;
  }
  p += 0x200000;
 } while (p <= addr + len);
 return npages;
}

variable_types:
len
unsigned long

variable_types:
p
void *

variable_types:
npages
unsigned long

return_type: long


decompiled_code:
__int64 build_page_frames()
{
signed __int64 *v0; // rax
int i; // esi
int v2; // ebx
unsigned __int64 v3; // rsi
__int64 v5[6]; // [rsp+8h] [rbp-30h] BYREF

v5[0] = 0LL;
v0 = zone;
do
*v0++ = -1LL;
while ( v0 != &zone[32] );
for ( i = 0; ; i = v2 )
{
v2 = i + 1;
v3 = get_mm_range(v5, (unsigned __int8)i);
if ( !v3 )
break;
zone[32] += mm_make_range_usable(v5[0], v3);
}
return kprintf(" %d free page frames\n", unk_420);
}

canonical_decompiled_code:
__int64 func8()
{
signed __int64 *v0; // rax
int i; // esi
int v2; // ebx
unsigned __int64 v3; // rsi
__int64 v5[6]; // [rsp+8h] [rbp-30h] BYREF

v5[0] = 0LL;
v0 = zone;
do
*v0++ = -1LL;
while ( v0 != &zone[32] );
for ( i = 0; ; i = v2 )
{
v2 = i + 1;
v3 = get_mm_range(v5, (unsigned __int8)i);
if ( !v3 )
break;
zone[32] += func7(v5[0], v3);
}
return kprintf(" %d free page frames\n", unk_420);
}

original_code:
void build_page_frames(void)
{
 u8 i;
 void *addr = 0;
 u64 len;
 for (i = 0; i < 32; i++)
  zone.memmap[i] = ~0L;
 i = 0;
 do {
  len = get_mm_range(&addr, i++);
  if (len == 0)
   break;
  zone.npages += mm_make_range_usable(addr, len);
 } while (1);
 kprintf("  %d free page frames\n", zone.npages);
}

canonical_original_code:
void build_page_frames()
{
 unsigned char i;
 void *addr = 0;
 unsigned long len;
 for (i = 0; i < 32; i++)
  zone.memmap[i] = ~0L;
 i = 0;
 do {
  len = get_mm_range(&addr, i++);
  if (len == 0)
   break;
  zone.npages += mm_make_range_usable(addr, len);
 } while (1);
 kprintf("  %d free page frames\n", zone.npages);
}

variable_types:
i
unsigned char


decompiled_code:
__int64 __fastcall get_pages(unsigned int a1)
{
int v1; // ebx

if ( a1 )
kpanic("Not yet implemented!\n");
v1 = find_free_pages(a1);
if ( v1 < 0 )
oom("get_pages");
return pfn_to_addr(v1) - 0x800000000000LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int a1)
{
int v1; // ebx

if ( a1 )
kpanic("Not yet implemented!\n");
v1 = func6(a1);
if ( v1 < 0 )
oom("get_pages");
return func2(v1) - 0x800000000000LL;
}

original_code:
void *get_pages(u32 order)
{
 s32 pfn;
 if (order > 0)
  kpanic("Not yet implemented!\n");
 pfn = find_free_pages(order);
 if (pfn < 0)
  oom(__func__);
 return (void *)((void *)(((u64)pfn_to_addr(pfn)) + 0xffff800000000000));
}

canonical_original_code:
void *get_pages(unsigned int order)
{
 int pfn;
 if (order > 0)
  kpanic("Not yet implemented!\n");
 pfn = find_free_pages(order);
 if (pfn < 0)
  oom(__func__);
 return (void *)((void *)(((unsigned long)pfn_to_addr(pfn)) + 0xffff800000000000));
}

variable_types:
order
unsigned int

variable_types: pfn int


decompiled_code:
__int64 get_one_page()
{
return get_pages(0);
}

canonical_decompiled_code:
__int64 func10()
{
return func9(0);
}

original_code:
void *get_one_page(void)
{
 return get_pages(0);
}

canonical_original_code:
void *get_one_page()
{
 return get_pages(0);
}


decompiled_code:
void __fastcall free_page(unsigned __int64 a1)
{
pf_mark_free_addr(a1);
}

canonical_decompiled_code:
void __fastcall func11(unsigned __int64 a1)
{
func4(a1);
}

original_code:
void free_page(u64 *addr)
{
 pf_mark_free_addr(addr);
}

canonical_original_code:
void free_page(unsigned long *addr)
{
 pf_mark_free_addr(addr);
}

variable_types:
addr
unsigned long *


decompiled_code:
void __fastcall free_page_pfn(__int64 a1)
{
__int64 v1; // rax

v1 = pfn_to_addr(a1);
pf_mark_free_addr(v1);
}

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

v1 = func2(a1);
func4(v1);
}

original_code:
void free_page_pfn(u64 pfn)
{
 pf_mark_free_addr((u64 *)pfn_to_addr(pfn));
}

canonical_original_code:
void free_page_pfn(unsigned long pfn)
{
 pf_mark_free_addr((unsigned long *)pfn_to_addr(pfn));
}


decompiled_code:
void __fastcall cr3_write(unsigned __int64 a1)
{
__writecr3(a1);
}

canonical_decompiled_code:
void __fastcall func0(unsigned __int64 a1)
{
__writecr3(a1);
}

original_code:
static inline void
cr3_write(u64 val)
{
 asm volatile(
  "mov %0,%%cr3"
  : : "r" (val));
}

canonical_original_code:
static inline void cr3_write(unsigned long val)
{
 asm volatile(
  "mov %0,%%cr3"
  : : "r" (val));
}

variable_types:
val
unsigned long


original_code:
static void *pte_set(void *src, void *dst, u8 user_readable, u8 last)
{
 *(u64 *)src = (u64)((void *)(((u64)dst) - 0xffff800000000000))
  | (last << 7)
  | (user_readable << 2)
  | 0b10
  | 0b1;
 return dst;
}

canonical_original_code:
static void *pte_set(void *src, void *dst, unsigned char user_readable, unsigned char last)
{
 *(unsigned long *)src = (u64)((void *)(((unsigned long)dst) - 0xffff800000000000))
  | (last << 7)
  | (user_readable << 2)
  | 0b10
  | 0b1;
 return dst;
}

variable_types:
src
void *

variable_types:
dst
void *

variable_types:
last
unsigned char


decompiled_code:
__int64 __fastcall pml4_walk(unsigned __int64 a1)
{
return *(_QWORD *)(*(_QWORD *)(*(_QWORD *)(current + 264LL) + 16LL) + 8 * ((a1 >> 39) & 0x1FF)) & 0xFFFFFFFFFF000LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int64 a1)
{
return *(_QWORD *)(*(_QWORD *)(*(_QWORD *)(current + 264LL) + 16LL) + 8 * ((a1 >> 39) & 0x1FF)) & 0xFFFFFFFFFF000LL;
}

original_code:
static void *pml4_walk(const void *addr)
{
 return ((void *)((*(u64 *)(current->mm->pgd + ((u64)(addr) >> 39 & 0x1ff))) & 0xffffffffff000L));
}

canonical_original_code:
static void *pml4_walk(void *addr)
{
 return ((void *)((*(unsigned long *)(current->mm->pgd + ((u64)(addr) >> 39 & 0x1ff))) & 0xffffffffff000L));
}


decompiled_code:
__int64 __fastcall pdpe_walk(__int64 a1, unsigned __int64 a2)
{
return *(_QWORD *)(a1 + 8 * ((a2 >> 30) & 0x1FF)) & 0xFFFFFFFFFF000LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, unsigned __int64 a2)
{
return *(_QWORD *)(a1 + 8 * ((a2 >> 30) & 0x1FF)) & 0xFFFFFFFFFF000LL;
}

original_code:
static void *pdpe_walk(const u64 *pdpe, const void *addr)
{
 return ((void *)((*(u64 *)(pdpe + ((u64)(addr) >> 30 & 0x1ff))) & 0xffffffffff000L));
}

canonical_original_code:
static void *pdpe_walk(unsigned long *pdpe, void *addr)
{
 return ((void *)((*(unsigned long *)(pdpe + ((u64)(addr) >> 30 & 0x1ff))) & 0xffffffffff000L));
}

variable_types:
pdpe
unsigned long *


decompiled_code:
__int64 __fastcall pde_walk(__int64 a1, unsigned __int64 a2)
{
return (a2 & 0xFFFFF) + (*(_QWORD *)(a1 + 8 * ((a2 >> 21) & 0x1FF)) & 0xFFFFFFFFFF000LL);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, unsigned __int64 a2)
{
return (a2 & 0xFFFFF) + (*(_QWORD *)(a1 + 8 * ((a2 >> 21) & 0x1FF)) & 0xFFFFFFFFFF000LL);
}

original_code:
static void *pde_walk(const u64 *pde, const void *addr)
{
 return ((void *)((*(u64 *)(pde + ((u64)(addr) >> 21 & 0x1ff))) & 0xffffffffff000L)) + ((u64)addr & 0xfffff);
}

canonical_original_code:
static void *pde_walk(unsigned long *pde, void *addr)
{
 return ((void *)((*(unsigned long *)(pde + ((u64)(addr) >> 21 & 0x1ff))) & 0xffffffffff000L)) + ((unsigned long)addr & 0xfffff);
}

variable_types:
pde
unsigned long *


decompiled_code:
__int64 __fastcall is_canonical(unsigned __int64 a1, __int64 a2, int a3)
{
unsigned int v3; // eax
unsigned __int64 v4; // rdi

v4 = a1 >> 47;
LOBYTE(v3) = v4 == 0;
LOBYTE(a3) = v4 == 0x1FFFF;
return a3 | v3;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned __int64 a1, __int64 a2, int a3)
{
unsigned int v3; // eax
unsigned __int64 v4; // rdi

v4 = a1 >> 47;
LOBYTE(v3) = v4 == 0;
LOBYTE(a3) = v4 == 0x1FFFF;
return a3 | v3;
}

original_code:
static u8 is_canonical(const void *addr)
{
 if ((u64)addr >> 47 == 0 || (u64)addr >> 47 == 0x1ffff)
  return 1;
 return 0;
}

canonical_original_code:
static unsigned char is_canonical(void *addr)
{
 if ((unsigned long)addr >> 47 == 0 || (unsigned long)addr >> 47 == 0x1ffff)
  return 1;
 return 0;
}

return_type: unsigned char


decompiled_code:
__int64 pte_get_empty()
{
return xkmallocf0(4096LL, "pte_get_empty");
}

canonical_decompiled_code:
__int64 func6()
{
return xkmallocf0(4096LL, "pte_get_empty");
}

original_code:
static void *pte_get_empty(void)
{
 return xkmallocf0(4096, __func__);
}

canonical_original_code:
static void *pte_get_empty()
{
 return xkmallocf0(4096, __func__);
}


decompiled_code:
void __fastcall cr3_load(unsigned __int64 a1)
{
if ( (a1 & 0x7FF8000000000FFFLL) != 0 )
kpanic("Invalid cr3 location!\n");
cr3_write(a1);
}

canonical_decompiled_code:
void __fastcall func7(unsigned __int64 a1)
{
if ( (a1 & 0x7FF8000000000FFFLL) != 0 )
kpanic("Invalid cr3 location!\n");
func0(a1);
}

original_code:
static void cr3_load(void *pml4)
{
 if (((u64)pml4 & 0x7ff8000000000fffL) != 0)
  kpanic("Invalid cr3 location!\n");
 cr3_write((u64)pml4);
}

canonical_original_code:
static void cr3_load(void *pml4)
{
 if (((unsigned long)pml4 & 0x7ff8000000000fffL) != 0)
  kpanic("Invalid cr3 location!\n");
 cr3_write((unsigned long)pml4);
}


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

if ( !(unsigned __int8)is_canonical(a1, a2, a3) )
return 0LL;
result = pml4_walk(a1);
if ( result )
{
result = pdpe_walk(result - 0x800000000000LL, a1);
if ( result )
result = pde_walk(result - 0x800000000000LL, a1);
}
return result;
}

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

if ( !(unsigned __int8)func5(a1, a2, a3) )
return 0LL;
result = func2(a1);
if ( result )
{
result = func3(result - 0x800000000000LL, a1);
if ( result )
result = func4(result - 0x800000000000LL, a1);
}
return result;
}

original_code:
void *virt2phys(const void *vaddr)
{
 u64 *pdpe;
 u64 *pde;
 if (!is_canonical(vaddr))
  return ((void *)0);
 pdpe = pml4_walk(vaddr);
 if (pdpe == ((void *)0))
  return ((void *)0);
 pde = pdpe_walk(((void *)(((u64)pdpe) + 0xffff800000000000)), vaddr);
 if (pde == ((void *)0))
  return ((void *)0);
 return pde_walk(((void *)(((u64)pde) + 0xffff800000000000)), vaddr);
}

canonical_original_code:
void *virt2phys(void *vaddr)
{
 unsigned long *pdpe;
 unsigned long *pde;
 if (!is_canonical(vaddr))
  return ((void *)0);
 pdpe = pml4_walk(vaddr);
 if (pdpe == ((void *)0))
  return ((void *)0);
 pde = pdpe_walk(((void *)(((unsigned long)pdpe) + 0xffff800000000000)), vaddr);
 if (pde == ((void *)0))
  return ((void *)0);
 return pde_walk(((void *)(((unsigned long)pde) + 0xffff800000000000)), vaddr);
}

variable_types:
vaddr
void *


decompiled_code:
__int64 __fastcall map_one_page(unsigned __int64 a1, unsigned __int64 a2, int a3)
{
__int64 result; // rax
int v4; // edx
__int64 v5; // r12
__int64 v6; // rax
__int64 v7; // rax
__int64 v8; // rax

result = is_canonical(a2, a2, a3);
if ( (_BYTE)result )
{
result = is_canonical(a1, a2, v4);
if ( (_BYTE)result )
{
v5 = pml4_walk(a2);
if ( !v5 )
{
v7 = pte_get_empty();
v5 = pte_set(
(unsigned __int64 *)(*(_QWORD *)(*(_QWORD *)(current + 264LL) + 16LL) + ((a2 >> 36) & 0xFF8)),
v7,
1,
0);
}
v6 = pdpe_walk(v5, a2);
if ( !v6 )
{
v8 = pte_get_empty();
v6 = pte_set((unsigned __int64 *)(v5 + ((a2 >> 27) & 0xFF8)), v8, 1, 0);
}
pte_set((unsigned __int64 *)(v6 + ((a2 >> 18) & 0xFF8)), (a1 & 0xFFFFFFFFFFE00000LL) - 0x800000000000LL, 1, 1);
result = 1LL;
}
}
return result;
}

original_code:
u8 map_one_page(void *phys, void *vaddr)
{
 u64 *pdpe;
 u64 *pde;
 if (!is_canonical(vaddr) || !is_canonical(phys))
  return 0;
 pdpe = pml4_walk(vaddr);
 if (pdpe == ((void *)0))
  pdpe = pte_set(current->mm->pgd + ((u64)(vaddr) >> 39 & 0x1ff), pte_get_empty(), 1, 0);
 pde = pdpe_walk(pdpe, vaddr);
 if (pde == ((void *)0))
  pde = pte_set(pdpe + ((u64)(vaddr) >> 30 & 0x1ff), pte_get_empty(), 1, 0);
 pte_set(pde + ((u64)(vaddr) >> 21 & 0x1ff), ((void *)(((u64)phys & ~(0x200000 - 1)) + 0xffff800000000000)), 1, 1);
 return 1;
}

canonical_original_code:
unsigned char map_one_page(void *phys, void *vaddr)
{
 unsigned long *pdpe;
 unsigned long *pde;
 if (!is_canonical(vaddr) || !is_canonical(phys))
  return 0;
 pdpe = pml4_walk(vaddr);
 if (pdpe == ((void *)0))
  pdpe = pte_set(current->mm->pgd + ((u64)(vaddr) >> 39 & 0x1ff), pte_get_empty(), 1, 0);
 pde = pdpe_walk(pdpe, vaddr);
 if (pde == ((void *)0))
  pde = pte_set(pdpe + ((u64)(vaddr) >> 30 & 0x1ff), pte_get_empty(), 1, 0);
 pte_set(pde + ((u64)(vaddr) >> 21 & 0x1ff), ((void *)(((unsigned long)phys & ~(0x200000 - 1)) + 0xffff800000000000)), 1, 1);
 return 1;
}

variable_types:
phys
void *


original_code:
void load_init_page_tables(struct task *task)
{
 u16 i;
 u64 *p;
 u64 *pa;
 u64 *pd;
 task->mm = xkmallocf0(sizeof(struct mm), __func__);
 init_mm(task->mm, pte_get_empty());
 p = pte_set(task->mm->pgd + 256, pte_get_empty(), 0, 0);
 pa = pte_set(p + 0, pte_get_empty(), 0, 0);
 pd = pte_set(p + 3, pte_get_empty(), 0, 0);
 for (i = 0; i < 512; i++) {
  *(pa + i) = 0x87 + i * 0x200000;
  *(pd + i) = 0xc0000087 + i * 0x200000;
 }
 cr3_load(((void *)(((u64)task->mm->pgd) - 0xffff800000000000)));
}

canonical_original_code:
void load_init_page_tables(struct task *task)
{
 unsigned short i;
 unsigned long *p;
 unsigned long *pa;
 unsigned long *pd;
 task->mm = xkmallocf0(sizeof(struct mm), __func__);
 init_mm(task->mm, pte_get_empty());
 p = pte_set(task->mm->pgd + 256, pte_get_empty(), 0, 0);
 pa = pte_set(p + 0, pte_get_empty(), 0, 0);
 pd = pte_set(p + 3, pte_get_empty(), 0, 0);
 for (i = 0; i < 512; i++) {
  *(pa + i) = 0x87 + i * 0x200000;
  *(pd + i) = 0xc0000087 + i * 0x200000;
 }
 cr3_load(((void *)(((unsigned long)task->mm->pgd) - 0xffff800000000000)));
}

variable_types: struct task { unsigned char state; struct file { struct inode { struct super { struct dev_t { unsigned int major; unsigned int minor; } dev; unsigned long blocksize; unsigned long blocksizephys; struct list_head { struct list_head *prev; } bcache; struct list_head icache; struct super_ops { struct super *(*super_read)(struct dev_t *dev); struct inode *(*inode_read)(struct super *sb, unsigned long inum); } *ops; unsigned long magic; unsigned long time; struct inode *mounted; unsigned int flags; void *priv; } *sb; unsigned long num; unsigned short mode; unsigned char nlinks; unsigned int uid; unsigned int gid; unsigned long filesize; unsigned long nblocks; unsigned long atime; unsigned long mtime; unsigned long ctime; unsigned int flags; unsigned int count; struct list_head l; struct super *covered; struct inode_ops { struct inode *(*lookup)(struct inode *, char *, unsigned char); unsigned long (*bmap)(struct inode *, long); struct bhead { struct list_head l; unsigned long bnum; void *data; unsigned char count; } *(*block_read)(struct inode *, long); } *ops; void *priv; } *inode; unsigned short count; unsigned short mode; unsigned short flags; long pos; } *fds[32]; struct mm { struct list_head l; unsigned long *pgd; unsigned char count; void *code_start; void *code_end; void *data_start; void *data_end; void *heap_start; void *heap_end; void *stack_start; unsigned short used_pages; } *mm; } *

variable_types:
i
unsigned short

variable_types:
p
unsigned long *

variable_types:
pa
unsigned long *

variable_types:
pd
unsigned long *

user_defined_types: struct task { unsigned char state; struct file *fds[32]; struct mm *mm; }

user_defined_types: struct file { struct inode *inode; unsigned short count; unsigned short mode; unsigned short flags; long pos; }

user_defined_types: struct inode { struct super *sb; unsigned long num; unsigned short mode; unsigned char nlinks; unsigned int uid; unsigned int gid; unsigned long filesize; unsigned long nblocks; unsigned long atime; unsigned long mtime; unsigned long ctime; unsigned int flags; unsigned int count; struct list_head l; struct super *covered; struct inode_ops *ops; void *priv; }

user_defined_types: struct super { struct dev_t dev; unsigned long blocksize; unsigned long blocksizephys; struct list_head bcache; struct list_head icache; struct super_ops *ops; unsigned long magic; unsigned long time; struct inode *mounted; unsigned int flags; void *priv; }

user_defined_types: struct inode_ops { struct inode *(*lookup)(struct inode *, char *, unsigned char); unsigned long (*bmap)(struct inode *, long); struct bhead *(*block_read)(struct inode *, long); }


decompiled_code:
void __fastcall syscall_dispatch_real()
{
unsigned __int64 v0; // rax

if ( v0 < 0x3D )
syscall_table[v0]();
__asm { sysret }
}

canonical_decompiled_code:
void __fastcall func0()
{
unsigned __int64 v0; // rax

if ( v0 < 0x3D )
syscall_table[v0]();
__asm { sysret }
}

original_code:
void syscall_dispatch_real(void)
{
 asm volatile(
  "nop; nop; nop; nop;"
  "nop; nop; nop; nop;"
  "nop; nop; nop; nop;"
  "nop; nop; nop; nop;\n\t"
  "cmpq $" "61" ", %rax\n\t"
  "jae 1f\n\t"
  "cmpq $" "60" ", %rax\n\t"
  "je 2f\n\t"
  "pushq %rax\n\t" "pushq %rcx\n\t" "pushq %rdx\n\t" "pushq %rbx\n\t" "pushq %rsi\n\t" "pushq %rdi\n\t" "pushq %r8\n\t" "pushq %r9\n\t" "pushq %r10\n\t" "pushq %r11\n\t"
  "2:\n\t"
 );
 asm volatile(
  "shl $3, %%rax\n\t"
  "addq %0, %%rax\n\t"
  "call *(%%rax)\n\t"
  : : "c"(syscall_table)
 );
 asm volatile(
  "popq %r11\n\t" "popq %r10\n\t" "popq %r9\n\t" "popq %r8\n\t" "popq %rdi\n\t" "popq %rsi\n\t" "popq %rbx\n\t" "popq %rdx\n\t" "popq %rcx\n\t" "popq %rax\n\t"
  "1:\n\t"
  "sysretq\n\t"
 );
 kernel_idle();
}

canonical_original_code:
void syscall_dispatch_real()
{
 asm volatile(
  "nop; nop; nop; nop;"
  "nop; nop; nop; nop;"
  "nop; nop; nop; nop;"
  "nop; nop; nop; nop;\n\t"
  "cmpq $" "61" ", %rax\n\t"
  "jae 1f\n\t"
  "cmpq $" "60" ", %rax\n\t"
  "je 2f\n\t"
  "pushq %rax\n\t" "pushq %rcx\n\t" "pushq %rdx\n\t" "pushq %rbx\n\t" "pushq %rsi\n\t" "pushq %rdi\n\t" "pushq %r8\n\t" "pushq %r9\n\t" "pushq %r10\n\t" "pushq %r11\n\t"
  "2:\n\t"
 );
 asm volatile(
  "shl $3, %%rax\n\t"
  "addq %0, %%rax\n\t"
  "call *(%%rax)\n\t"
  : : "c"(syscall_table)
 );
 asm volatile(
  "popq %r11\n\t" "popq %r10\n\t" "popq %r9\n\t" "popq %r8\n\t" "popq %rdi\n\t" "popq %rsi\n\t" "popq %rbx\n\t" "popq %rdx\n\t" "popq %rcx\n\t" "popq %rax\n\t"
  "1:\n\t"
  "sysretq\n\t"
 );
 kernel_idle();
}


decompiled_code:
__int64 __fastcall syscall_register(unsigned __int16 a1, void (*a2)(void))
{
if ( a1 > 0x3Cu )
return 0xFFFFFFFFLL;
syscall_table[a1] = a2;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int16 a1, void (*a2)(void))
{
if ( a1 > 0x3Cu )
return 0xFFFFFFFFLL;
syscall_table[a1] = a2;
return 0LL;
}

original_code:
s8 syscall_register(u16 num, void *handler)
{
 if (num >= 61)
  return -1;
 syscall_table[num] = handler;
 return 0;
}

canonical_original_code:
char syscall_register(unsigned short num, void *handler)
{
 if (num >= 61)
  return -1;
 syscall_table[num] = handler;
 return 0;
}

variable_types:
num
unsigned short

variable_types:
handler
void *


decompiled_code:
void *init_syscall()
{
void *result; // rax

result = &loc_10;
syscall_dispatch = (__int64)&loc_10;
return result;
}

canonical_decompiled_code:
void *func2()
{
void *result; // rax

result = &loc_10;
syscall_dispatch = (__int64)&loc_10;
return result;
}

original_code:
void init_syscall(void)
{
 syscall_dispatch = syscall_dispatch_real + 16;
}

canonical_original_code:
void init_syscall()
{
 syscall_dispatch = syscall_dispatch_real + 16;
}


decompiled_code:
__int64 elf_get_machine()
{
return 62LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 62LL;
}

original_code:
static u16 elf_get_machine(void)
{
 return 0x3e;
}

canonical_original_code:
static unsigned short elf_get_machine()
{
 return 0x3e;
}

return_type: unsigned short


decompiled_code:
__int64 __fastcall elf_load_section(int fd, __int64 a2)
{
if ( *(_QWORD *)(a2 + 16) % *(_QWORD *)(a2 + 48) )
return 0xFFFFFFFFLL;
mmap(*(void **)(a2 + 16), *(_QWORD *)(a2 + 40), 4, 17, fd, *(_QWORD *)(a2 + 8));
return 0LL;
}

original_code:
static s8 elf_load_section(s32 fd, struct elf_ph_entry *ph)
{
 if (ph->ph_vaddr % ph->ph_align > 0)
  return -1;
 mmap((void *)ph->ph_vaddr, ph->ph_memsz, 0x4,
  0x01 | 0x10, fd, ph->ph_offset);
 return 0;
}

canonical_original_code:
static char elf_load_section(int fd, struct elf_ph_entry *ph)
{
 if (ph->ph_vaddr % ph->ph_align > 0)
  return -1;
 mmap((void *)ph->ph_vaddr, ph->ph_memsz, 0x4,
  0x01 | 0x10, fd, ph->ph_offset);
 return 0;
}

variable_types: fd int

variable_types: struct elf_ph_entry { unsigned int ph_type; unsigned int ph_flags; unsigned long ph_offset; unsigned long ph_vaddr; unsigned long ph_paddr; unsigned long ph_filesz; unsigned long ph_memsz; unsigned long ph_align; } *


decompiled_code:
__int64 lapic_eoi()
{
return lapic_write(0xB0u, 0);
}

canonical_decompiled_code:
__int64 func1()
{
return func0(0xB0u, 0);
}

original_code:
static inline void lapic_eoi(void)
{
 lapic_write(0xb0, 0);
}

canonical_original_code:
static inline void lapic_eoi()
{
 lapic_write(0xb0, 0);
}


decompiled_code:
void do_pit()
{
++jiffies;
lapic_eoi();
__asm { iretq }
}

canonical_decompiled_code:
void func2()
{
++jiffies;
func1();
__asm { iretq }
}

original_code:
__attribute__((aligned(16))) static void do_pit(struct intr_frame r)
{
 asm volatile( "nop; nop; nop; nop;" "nop; nop; nop; nop;" "nop; nop; nop; nop;" "nop; nop; nop; nop;" "pushq $0\n\t" "pushq %rax\n\t" "pushq %rcx\n\t" "pushq %rdx\n\t" "pushq %rbx\n\t" "pushq %rsi\n\t" "pushq %rdi\n\t" "pushq %r8\n\t" "pushq %r9\n\t" "pushq %r10\n\t" "pushq %r11\n\t" "subq $16, %rsp\n\t" "cld\n\t" );
 jiffies++;
 lapic_eoi();
 asm volatile( "addq $16, %rsp\n\t" "popq %r11\n\t" "popq %r10\n\t" "popq %r9\n\t" "popq %r8\n\t" "popq %rdi\n\t" "popq %rsi\n\t" "popq %rbx\n\t" "popq %rdx\n\t" "popq %rcx\n\t" "popq %rax\n\t" "addq $8, %rsp\n\t" "iretq\n\t" );
}

variable_types: struct intr_frame { unsigned long r11; unsigned long r10; unsigned long r9; unsigned long r8; unsigned long rdi; unsigned long rsi; unsigned long rbx; unsigned long rdx; unsigned long rcx; unsigned long rax; unsigned int ecode; unsigned int pad4; unsigned long rip; unsigned char cs; unsigned char pad0; unsigned int pad1; unsigned long rflags; unsigned long rsp; unsigned char ss; unsigned char pad2; unsigned int pad3; }


decompiled_code:
void do_lapictim()
{
lapic_eoi();
__asm { iretq }
}

canonical_decompiled_code:
void func3()
{
func1();
__asm { iretq }
}

original_code:
__attribute__((aligned(16))) static void do_lapictim(struct intr_frame r)
{
 asm volatile( "nop; nop; nop; nop;" "nop; nop; nop; nop;" "nop; nop; nop; nop;" "nop; nop; nop; nop;" "pushq $0\n\t" "pushq %rax\n\t" "pushq %rcx\n\t" "pushq %rdx\n\t" "pushq %rbx\n\t" "pushq %rsi\n\t" "pushq %rdi\n\t" "pushq %r8\n\t" "pushq %r9\n\t" "pushq %r10\n\t" "pushq %r11\n\t" "subq $16, %rsp\n\t" "cld\n\t" );
 lapic_eoi();
 asm volatile( "addq $16, %rsp\n\t" "popq %r11\n\t" "popq %r10\n\t" "popq %r9\n\t" "popq %r8\n\t" "popq %rdi\n\t" "popq %rsi\n\t" "popq %rbx\n\t" "popq %rdx\n\t" "popq %rcx\n\t" "popq %rax\n\t" "addq $8, %rsp\n\t" "iretq\n\t" );
}

user_defined_types: struct intr_frame { unsigned long r11; unsigned long r10; unsigned long r9; unsigned long r8; unsigned long rdi; unsigned long rsi; unsigned long rbx; unsigned long rdx; unsigned long rcx; unsigned long rax; unsigned int ecode; unsigned int pad4; unsigned long rip; unsigned char cs; unsigned char pad0; unsigned int pad1; unsigned long rflags; unsigned long rsp; unsigned char ss; unsigned char pad2; unsigned int pad3; }


decompiled_code:
__int64 tsc_calibration()
{
char v0; // bl
unsigned __int64 v1; // rax
unsigned __int64 v2; // rbx
unsigned __int64 v4[6]; // [rsp+8h] [rbp-30h] BYREF

sys[2] = -1LL;
loops_per_jiffy = -1LL;
v0 = 4;
do
{
v1 = tsc_calibration_withpit(v4);
if ( sys[2] > v1 )
sys[2] = v1;
if ( v4[0] < loops_per_jiffy )
loops_per_jiffy = v4[0];
--v0;
}
while ( v0 );
v2 = sys[2] / 0xF4240uLL;
if ( sys[2] <= 0xF423FuLL )
kpanic("Can't calibrate the cpu!!");
return kprintf("Cpu speed calibrated to: %ldMhz, %ld BogoMIPS\n", v2, loops_per_jiffy);
}

canonical_decompiled_code:
__int64 func4()
{
char v0; // bl
unsigned __int64 v1; // rax
unsigned __int64 v2; // rbx
unsigned __int64 v4[6]; // [rsp+8h] [rbp-30h] BYREF

sys[2] = -1LL;
loops_per_jiffy = -1LL;
v0 = 4;
do
{
v1 = tsc_calibration_withpit(v4);
if ( sys[2] > v1 )
sys[2] = v1;
if ( v4[0] < loops_per_jiffy )
loops_per_jiffy = v4[0];
--v0;
}
while ( v0 );
v2 = sys[2] / 0xF4240uLL;
if ( sys[2] <= 0xF423FuLL )
kpanic("Can't calibrate the cpu!!");
return kprintf("Cpu speed calibrated to: %ldMhz, %ld BogoMIPS\n", v2, loops_per_jiffy);
}

original_code:
static void tsc_calibration(void)
{
 u64 hz, mhz, loops;
 u8 i;
 sys.cpu[0].hz = (u64)-1;
 loops_per_jiffy = (u64)-1;
 i = 4;
 while (i--) {
  hz = tsc_calibration_withpit(&loops);
  if (hz < sys.cpu[0].hz)
   sys.cpu[0].hz = hz;
  if (loops < loops_per_jiffy)
   loops_per_jiffy = loops;
 }
 mhz = sys.cpu[0].hz / 1000000;
 if (mhz == 0)
  kpanic("Can't calibrate the cpu!!");
 kprintf("Cpu speed calibrated to: %ldMhz, %ld BogoMIPS\n",
  mhz, loops_per_jiffy);
}

canonical_original_code:
static void tsc_calibration()
{
 unsigned long hz; unsigned long mhz; unsigned long loops;
 unsigned char i;
 sys.cpu[0].hz = (u64)-1;
 loops_per_jiffy = (u64)-1;
 i = 4;
 while (i--) {
  hz = tsc_calibration_withpit(&loops);
  if (hz < sys.cpu[0].hz)
   sys.cpu[0].hz = hz;
  if (loops < loops_per_jiffy)
   loops_per_jiffy = loops;
 }
 mhz = sys.cpu[0].hz / 1000000;
 if (mhz == 0)
  kpanic("Can't calibrate the cpu!!");
 kprintf("Cpu speed calibrated to: %ldMhz, %ld BogoMIPS\n",
  mhz, loops_per_jiffy);
}

variable_types:
hz
unsigned long

variable_types:
mhz
unsigned long

variable_types:
loops
unsigned long

variable_types:
i
unsigned char


decompiled_code:
__int64 init_timers()
{
tsc_calibration();
init_pit(do_pit, 100LL);
init_lapic_timer(do_lapictim, 100LL);
return init_rtc();
}

canonical_decompiled_code:
__int64 func5()
{
func4();
init_pit(do_pit, 100LL);
init_lapic_timer(do_lapictim, 100LL);
return init_rtc();
}

original_code:
void init_timers(void)
{
 tsc_calibration();
 init_pit(do_pit, 100);
 init_lapic_timer(do_lapictim, 100);
 init_rtc();
 return;
}

canonical_original_code:
void init_timers()
{
 tsc_calibration();
 init_pit(do_pit, 100);
 init_lapic_timer(do_lapictim, 100);
 init_rtc();
 return;
}


decompiled_code:
char __fastcall acpi_csum(_BYTE *a1, unsigned __int8 a2)
{
_BYTE *v2; // rsi
char result; // al

v2 = &a1[a2];
if ( a1 >= v2 )
return 0;
result = 0;
do
result += *a1++;
while ( v2 != a1 );
return result;
}

canonical_decompiled_code:
char __fastcall func0(_BYTE *a1, unsigned __int8 a2)
{
_BYTE *v2; // rsi
char result; // al

v2 = &a1[a2];
if ( a1 >= v2 )
return 0;
result = 0;
do
result += *a1++;
while ( v2 != a1 );
return result;
}

original_code:
static inline u8 acpi_csum(void *p, u8 bytes)
{
 u8 csum = 0;
 u8 *pp = (u8 *)p;
 while ((u64)pp < (u64)p + bytes)
  csum += *pp++;
 return csum;
}

canonical_original_code:
static inline unsigned char acpi_csum(void *p, unsigned char bytes)
{
 unsigned char csum = 0;
 unsigned char *pp = (u8 *)p;
 while ((unsigned long)pp < (unsigned long)p + bytes)
  csum += *pp++;
 return csum;
}

variable_types:
p
void *

variable_types:
bytes
unsigned char

variable_types:
csum
unsigned char

variable_types:
pp
unsigned char *


decompiled_code:
unsigned __int64 __fastcall acpi_walk_madt(unsigned int *a1, int a2)
{
char *v2; // rax
int v3; // edx
unsigned __int64 result; // rax
int v5; // ebp
unsigned int *i; // rbx
char v7; // al
__int64 v8; // rsi

v2 = (char *)&ioapic + 10;
v3 = 0;
do
{
*v2 = v3;
v2[1] = 1;
v2[2] = 1;
++v3;
v2 += 3;
}
while ( (_BYTE)v3 != 16 );
result = (unsigned __int64)&lapic;
*((_QWORD *)&sys + 1) = &lapic;
lapic = *a1 - 0x800000000000LL;
if ( (a1[1] & 1) != 0 )
{
result = (unsigned __int64)&sys + 32;
*((_BYTE *)&sys + 32) = 1;
}
v5 = a2 - 8;
for ( i = a1 + 2; v5 > 0; i = (unsigned int *)((char *)i + result) )
{
if ( !*((_BYTE *)i + 1) )
break;
v7 = *(_BYTE *)i;
if ( *(_BYTE *)i == 1 )
{
*((_BYTE *)&ioapic + 8) = *((_BYTE *)i + 2);
ioapic = i[1] - 0x800000000000LL;
}
else if ( v7 == 2 )
{
LOBYTE(v8) = *((_BYTE *)i + 3);
if ( (unsigned __int8)v8 <= 0x10u && i[1] <= 0x10 )
{
v8 = (unsigned __int8)v8;
kprintf(" PIC Redirect %d -> %d", (unsigned __int8)v8);
*((_BYTE *)&ioapic + 3 * *((unsigned __int8 *)i + 3) + 10) = i[1];
if ( (((unsigned __int8)*((_WORD *)i + 4) ^ 0xC) & 0xC) != 0 )
{
kprintf(" (edge, ", v8);
}
else
{
*((_BYTE *)&ioapic + 3 * i[1] + 11) = 0;
kprintf(" (level, ", v8);
}
if ( (i[2] & 3) == 1 )
{
kprintf("high)\n", v8);
}
else
{
*((_BYTE *)&ioapic + 3 * i[1] + 12) = 0;
kprintf("low)\n", v8);
}
}
}
else if ( !v7 )
{
*((_BYTE *)&lapic + 8) = *((_BYTE *)i + 3);
sys = *((_BYTE *)i + 2);
}
v5 -= *((unsigned __int8 *)i + 1);
result = *((unsigned __int8 *)i + 1);
}
return result;
}

original_code:
static void acpi_walk_madt(void *t, s32 l)
{
 struct acpi_apic *a;
 u8 i;
 for (i = 0; i < 16; i++) {
  ioapic[0].pic[i].dest = i;
  ioapic[0].pic[i].edge = 1;
  ioapic[0].pic[i].active_high = 1;
 }
 sys.cpu[0].lapic = &lapic[0];
 lapic[0].base = (void *)((void *)(((u64)*(u32 *)(void *)(t + 0)) + 0xffff800000000000));
 if (*(u32 *)(t + 4) & 0x1)
  sys.has8259 = 1;
 l = l - 8;
 a = (struct acpi_apic *)(t + 8);
 while (l > 0) {
  if (a->l == 0)
   break;
  switch (a->type) {
  case 0:
   lapic[0].id = a->lapicid;
   sys.cpu[0].id = a->cpuid;
   break;
  case 1:
   ioapic[0].id = a->ioapicid;
   ioapic[0].base = (void *)((void *)(((u64)a->ioapicaddr) + 0xffff800000000000));
   break;
  case 2:
   if (a->source > 16 || a->gsi > 16)
    break;
   kprintf("  PIC Redirect %d -> %d",
    a->source, a->gsi);
   ioapic[0].pic[a->source].dest = a->gsi;
   if (((a->flags2 >> 2) & 0x3) == 0x3) {
    ioapic[0].pic[a->gsi].edge = 0;
    kprintf(" (level, ");
   } else
    kprintf(" (edge, ");
   if ((a->flags2 & 0x3) != 0x1) {
    ioapic[0].pic[a->gsi].active_high = 0;
    kprintf("low)\n");
   } else
    kprintf("high)\n");
   break;
  default:
   break;
  }
  l -= a->l;
  a = (struct acpi_apic *)((u64)a + a->l);
 }
 return;
}

canonical_original_code:
static void acpi_walk_madt(void *t, int l)
{
 struct acpi_apic *a;
 unsigned char i;
 for (i = 0; i < 16; i++) {
  ioapic[0].pic[i].dest = i;
  ioapic[0].pic[i].edge = 1;
  ioapic[0].pic[i].active_high = 1;
 }
 sys.cpu[0].lapic = &lapic[0];
 lapic[0].base = (void *)((void *)(((u64)*(unsigned int *)(void *)(t + 0)) + 0xffff800000000000));
 if (*(unsigned int *)(t + 4) & 0x1)
  sys.has8259 = 1;
 l = l - 8;
 a = (struct acpi_apic *)(t + 8);
 while (l > 0) {
  if (a->l == 0)
   break;
  switch (a->type) {
  case 0:
   lapic[0].id = a->lapicid;
   sys.cpu[0].id = a->cpuid;
   break;
  case 1:
   ioapic[0].id = a->ioapicid;
   ioapic[0].base = (void *)((void *)(((unsigned long)a->ioapicaddr) + 0xffff800000000000));
   break;
  case 2:
   if (a->source > 16 || a->gsi > 16)
    break;
   kprintf("  PIC Redirect %d -> %d",
    a->source, a->gsi);
   ioapic[0].pic[a->source].dest = a->gsi;
   if (((a->flags2 >> 2) & 0x3) == 0x3) {
    ioapic[0].pic[a->gsi].edge = 0;
    kprintf(" (level, ");
   } else
    kprintf(" (edge, ");
   if ((a->flags2 & 0x3) != 0x1) {
    ioapic[0].pic[a->gsi].active_high = 0;
    kprintf("low)\n");
   } else
    kprintf("high)\n");
   break;
  default:
   break;
  }
  l -= a->l;
  a = (struct acpi_apic *)((unsigned long)a + a->l);
 }
 return;
}

variable_types:
t
void *

variable_types: l int

variable_types: struct acpi_apic { unsigned char type; unsigned char l; union { struct { unsigned char cpuid; unsigned char lapicid; unsigned int flags0; }; struct { unsigned char ioapicid; unsigned char reserved1; unsigned int ioapicaddr; unsigned int gsib; }; struct { unsigned char bus; unsigned char source; unsigned int gsi; unsigned short flags2; }; }; } *


decompiled_code:
unsigned __int64 __fastcall init_acpi(__int64 a1, __int64 a2)
{
char *v2; // rax
char *v3; // rbx
_BYTE *v4; // r12
unsigned int *v5; // rbp
unsigned __int64 result; // rax
__int64 v7; // rbx
int v8; // er13
char dest[63]; // [rsp+9h] [rbp-3Fh] BYREF

kprintf("Parsing ACPI tables:\n", a2);
v2 = (char *)acpi_search_table(0x2052545020445352LL);
v3 = v2;
if ( !v2 || !*((_DWORD *)v2 + 4) )
kpanic("No valid ACPI tables.");
if ( acpi_csum(v3, 0x14u) )
kpanic("Wrong checksum in rsdp\n");
strncpy(dest, v3 + 9, 6uLL);
dest[6] = 0;
kprintf(" ACPI table v%d by %s\n", (unsigned __int8)v3[15]);
v4 = (_BYTE *)(*((unsigned int *)v3 + 4) - 0x800000000000LL);
if ( *(_DWORD *)v4 != 1413763922 )
kpanic("Invalid RSDT in ACPI table\n");
if ( acpi_csum(v4, v4[4]) )
kpanic("Wrong checksum in rsdt\n");
v5 = (unsigned int *)(v4 + 36);
result = (unsigned __int64)&v4[*((unsigned int *)v4 + 1)];
if ( (unsigned __int64)(v4 + 36) < result )
{
do
{
v7 = *v5 - 0x800000000000LL;
v8 = *(_DWORD *)(v7 + 4);
if ( !acpi_csum((_BYTE *)v7, v8) && *(_DWORD *)v7 == 1128878145 )
acpi_walk_madt((unsigned int *)(v7 + 36), v8 - 44);
++v5;
result = (unsigned __int64)&v4[*((unsigned int *)v4 + 1)];
}
while ( (unsigned __int64)v5 < result );
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(__int64 a1, __int64 a2)
{
char *v2; // rax
char *v3; // rbx
_BYTE *v4; // r12
unsigned int *v5; // rbp
unsigned __int64 result; // rax
__int64 v7; // rbx
int v8; // er13
char dest[63]; // [rsp+9h] [rbp-3Fh] BYREF

kprintf("Parsing ACPI tables:\n", a2);
v2 = (char *)acpi_search_table(0x2052545020445352LL);
v3 = v2;
if ( !v2 || !*((_DWORD *)v2 + 4) )
kpanic("No valid ACPI tables.");
if ( func0(v3, 0x14u) )
kpanic("Wrong checksum in rsdp\n");
strncpy(dest, v3 + 9, 6uLL);
dest[6] = 0;
kprintf(" ACPI table v%d by %s\n", (unsigned __int8)v3[15]);
v4 = (_BYTE *)(*((unsigned int *)v3 + 4) - 0x800000000000LL);
if ( *(_DWORD *)v4 != 1413763922 )
kpanic("Invalid RSDT in ACPI table\n");
if ( func0(v4, v4[4]) )
kpanic("Wrong checksum in rsdt\n");
v5 = (unsigned int *)(v4 + 36);
result = (unsigned __int64)&v4[*((unsigned int *)v4 + 1)];
if ( (unsigned __int64)(v4 + 36) < result )
{
do
{
v7 = *v5 - 0x800000000000LL;
v8 = *(_DWORD *)(v7 + 4);
if ( !func0((_BYTE *)v7, v8) && *(_DWORD *)v7 == 1128878145 )
func1((unsigned int *)(v7 + 36), v8 - 44);
++v5;
result = (unsigned __int64)&v4[*((unsigned int *)v4 + 1)];
}
while ( (unsigned __int64)v5 < result );
}
return result;
}

original_code:
void init_acpi(void)
{
 struct rsdp *rsdpp = ((void *)0);
 struct acpi_header *h;
 char oem[7];
 u32 *p;
 kprintf("Parsing ACPI tables:\n");
 rsdpp = (struct rsdp *)acpi_search_table(0x2052545020445352);
 if (rsdpp == ((void *)0) || rsdpp->rsdt == 0)
  kpanic("No valid ACPI tables.");
 if (acpi_csum(rsdpp, 20) != 0)
  kpanic("Wrong checksum in rsdp\n");
 strncpy(oem, rsdpp->oemid, 6);
 oem[6] = '\0';
 kprintf("  ACPI table v%d by %s\n", rsdpp->rev, oem);
 h = (struct acpi_header *)((void *)(((u64)rsdpp->rsdt) + 0xffff800000000000));
 if (h == ((void *)0) || h->sig != 0x54445352)
  kpanic("Invalid RSDT in ACPI table\n");
 if (acpi_csum(h, h->l) != 0)
  kpanic("Wrong checksum in rsdt\n");
 for (p = (u32 *)&h->entry; p < (u32 *)((u64)h + h->l); p++) {
  struct acpi_header *h2 = (struct acpi_header *)((void *)(((u64)*p) + 0xffff800000000000));
  if (acpi_csum(h2, h2->l) != 0)
   continue;
  switch (h2->sig) {
  case 0x43495041:
   acpi_walk_madt(&h2->entry,
    h2->l - sizeof(struct acpi_header) + 4
    );
   break;
  case 0x50434146:
   acpi_walk_fadt(&h2->entry);
   break;
  case 0x54445353:
   acpi_walk_ssdt(&h2->entry);
   break;
  case 0x54455048:
   break;
  default:
   break;
  }
 }
}

canonical_original_code:
void init_acpi()
{
 struct rsdp *rsdpp = ((void *)0);
 struct acpi_header *h;
 char oem[7];
 unsigned int *p;
 kprintf("Parsing ACPI tables:\n");
 rsdpp = (struct rsdp *)acpi_search_table(0x2052545020445352);
 if (rsdpp == ((void *)0) || rsdpp->rsdt == 0)
  kpanic("No valid ACPI tables.");
 if (acpi_csum(rsdpp, 20) != 0)
  kpanic("Wrong checksum in rsdp\n");
 strncpy(oem, rsdpp->oemid, 6);
 oem[6] = '\0';
 kprintf("  ACPI table v%d by %s\n", rsdpp->rev, oem);
 h = (struct acpi_header *)((void *)(((unsigned long)rsdpp->rsdt) + 0xffff800000000000));
 if (h == ((void *)0) || h->sig != 0x54445352)
  kpanic("Invalid RSDT in ACPI table\n");
 if (acpi_csum(h, h->l) != 0)
  kpanic("Wrong checksum in rsdt\n");
 for (p = (unsigned int *)&h->entry; p < (unsigned int *)((unsigned long)h + h->l); p++) {
  struct acpi_header *h2 = (struct acpi_header *)((void *)(((u64)*p) + 0xffff800000000000));
  if (acpi_csum(h2, h2->l) != 0)
   continue;
  switch (h2->sig) {
  case 0x43495041:
   acpi_walk_madt(&h2->entry,
    h2->l - sizeof(struct acpi_header) + 4
    );
   break;
  case 0x50434146:
   acpi_walk_fadt(&h2->entry);
   break;
  case 0x54445353:
   acpi_walk_ssdt(&h2->entry);
   break;
  case 0x54455048:
   break;
  default:
   break;
  }
 }
}

variable_types: struct rsdp { unsigned long sig; unsigned char csum; char oemid[6]; unsigned char rev; unsigned int rsdt; } *

variable_types: struct acpi_header { unsigned int sig; unsigned int l; unsigned char rev; unsigned char csum; char oemid[6]; char oemtableid[8]; unsigned int oemrev; char creatorid[4]; unsigned int creatorrev; unsigned int entry; } *

variable_types:
oem
char [7]

variable_types:
p
unsigned int *


iowait

decompiled_code:
void iowait()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
static inline void iowait(void)
{
 asm volatile(
  "jmp 1f\n\t"
  "1:\n\t"
  "jmp 1f\n\t"
  "1:\n\t"
  );
}

canonical_original_code:
static inline void iowait()
{
 asm volatile(
  "jmp 1f\n\t"
  "1:\n\t"
  "jmp 1f\n\t"
  "1:\n\t"
  );
}


yield

decompiled_code:
void yield()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
static inline void yield(void)
{
 asm volatile("nop\n\t");
}

canonical_original_code:
static inline void yield()
{
 asm volatile("nop\n\t");
}


outb

decompiled_code:
void __fastcall outb(unsigned __int16 a1, unsigned __int8 a2)
{
__outbyte(a1, a2);
iowait();
}

canonical_decompiled_code:
void __fastcall func2(unsigned __int16 a1, unsigned __int8 a2)
{
__outbyte(a1, a2);
func0();
}

original_code:
static inline void outb(u16 port, u8 val)
{
 asm volatile("outb %1, %0" : : "dN"(port), "a"(val));
 iowait();
}

canonical_original_code:
static inline void outb(unsigned short port, unsigned char val)
{
 asm volatile("outb %1, %0" : : "dN"(port), "a"(val));
 iowait();
}

variable_types:
port
unsigned short

variable_types:
val
unsigned char


canonical_decompiled_code:
__int64 func5()
{
return func4(0xB0u, 0);
}


decompiled_code:
void interrupts_disable()
{
_disable();
}

canonical_decompiled_code:
void func6()
{
_disable();
}

original_code:
static inline void interrupts_disable(void)
{
 asm volatile("cli\n\t");
}

canonical_original_code:
static inline void interrupts_disable()
{
 asm volatile("cli\n\t");
}


decompiled_code:
void interrupts_enable()
{
_enable();
}

canonical_decompiled_code:
void func7()
{
_enable();
}

original_code:
static inline void interrupts_enable(void)
{
 asm volatile("sti\n\t");
}

canonical_original_code:
static inline void interrupts_enable()
{
 asm volatile("sti\n\t");
}


ticks

decompiled_code:
unsigned __int64 ticks()
{
return __rdtsc();
}

canonical_decompiled_code:
unsigned __int64 func8()
{
return __rdtsc();
}

original_code:
static inline u64 ticks(void)
{
 u32 a;
 u32 d;
 asm volatile(
  "rdtsc\n\t"
  : "=a"(a), "=d"(d));
 return ((u64)d << 32) | a;
}

canonical_original_code:
static inline unsigned long ticks()
{
 unsigned int a;
 unsigned int d;
 asm volatile(
  "rdtsc\n\t"
  : "=a"(a), "=d"(d));
 return ((unsigned long)d << 32) | a;
}

variable_types:
a
unsigned int

variable_types:
d
unsigned int


decompiled_code:
__int64 __fastcall rtc_read(char a1)
{
outb(0x70u, a1 | 0x80);
return inb(0x71u);
}

canonical_decompiled_code:
__int64 __fastcall func11(char a1)
{
func2(0x70u, a1 | 0x80);
return func3(0x71u);
}

original_code:
static inline u8 rtc_read(u8 port)
{
 outb(0x70, port | (1 << 7));
 return inb(0x71);
}

canonical_original_code:
static inline unsigned char rtc_read(unsigned char port)
{
 outb(0x70, port | (1 << 7));
 return inb(0x71);
}

variable_types:
port
unsigned char


decompiled_code:
// positive sp value has been detected, the output may be wrong!
void __fastcall do_rtc(const char *a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
__int64 v6; // rax
__int64 v7; // r10
__int64 v8; // r11
struct tm v9; // [rsp-90h] [rbp-B8h] BYREF
__int64 v10; // [rsp-58h] [rbp-80h]
__int64 v11; // [rsp-48h] [rbp-70h]
__int64 v12; // [rsp-40h] [rbp-68h]
__int64 v13; // [rsp-38h] [rbp-60h]
__int64 v14; // [rsp-30h] [rbp-58h]

v14 = 0LL;
v13 = v6;
v12 = a4;
v11 = a3;
v10 = a2;
v9.tm_zone = a1;
v9.tm_gmtoff = a5;
*(_QWORD *)&v9.tm_isdst = a6;
*(_QWORD *)&v9.tm_wday = v7;
*(_QWORD *)&v9.tm_mon = v8;
rtc_read(12);
if ( ++hznum == 1024 )
{
hznum = 0;
++unixtime;
if ( unixtime != 3600 * (unixtime / 0xE10uLL) )
{
rtc_get_tm(&v9);
unixtime = mktime(&v9);
}
}
lapic_eoi();
__asm { iretq }
JUMPOUT(0x2AALL);
}

original_code:
__attribute__((aligned(16))) static void do_rtc(struct intr_frame r)
{
 asm volatile( "nop; nop; nop; nop;" "nop; nop; nop; nop;" "nop; nop; nop; nop;" "nop; nop; nop; nop;" "pushq $0\n\t" "pushq %rax\n\t" "pushq %rcx\n\t" "pushq %rdx\n\t" "pushq %rbx\n\t" "pushq %rsi\n\t" "pushq %rdi\n\t" "pushq %r8\n\t" "pushq %r9\n\t" "pushq %r10\n\t" "pushq %r11\n\t" "subq $16, %rsp\n\t" "cld\n\t" );
 struct tm tim;
 rtc_read(0xc);
 hznum++;
 if (hznum == 1024) {
  hznum = 0;
  unixtime++;
  if (unixtime % 3600) {
   rtc_get_tm(&tim);
   unixtime = mktime(&tim);
  }
 }
 lapic_eoi();
 asm volatile( "addq $16, %rsp\n\t" "popq %r11\n\t" "popq %r10\n\t" "popq %r9\n\t" "popq %r8\n\t" "popq %rdi\n\t" "popq %rsi\n\t" "popq %rbx\n\t" "popq %rdx\n\t" "popq %rcx\n\t" "popq %rax\n\t" "addq $8, %rsp\n\t" "iretq\n\t" );
}

variable_types: struct intr_frame { unsigned long r11; unsigned long r10; unsigned long r9; unsigned long r8; unsigned long rdi; unsigned long rsi; unsigned long rbx; unsigned long rdx; unsigned long rcx; unsigned long rax; unsigned int ecode; unsigned int pad4; unsigned long rip; unsigned char cs; unsigned char pad0; unsigned int pad1; unsigned long rflags; unsigned long rsp; unsigned char ss; unsigned char pad2; unsigned int pad3; }

variable_types: struct tm { short tm_sec; short tm_min; short tm_hour; short tm_mday; short tm_mon; short tm_year; short tm_wday; short tm_yday; short tm_isdst; }


decompiled_code:
__int64 init_rtc()
{
char v0; // al
__int16 v2[20]; // [rsp+0h] [rbp-28h] BYREF

intr_install_handler(8LL, do_rtc);
interrupts_disable();
v0 = rtc_read(11);
rtc_write(11, v0 | 0x46);
interrupts_enable();
msleep(0x1F4u);
rtc_get_tm(v2);
unixtime = mktime((struct tm *)v2);
return kprintf(
"System clock set to: %d-%d-%d %d:%d:%d (unix: %lld)\n",
(unsigned int)v2[5],
(unsigned int)v2[4],
(unsigned int)v2[3],
(unsigned int)v2[2],
(unsigned int)v2[1],
(unsigned int)v2[0],
unixtime);
}

canonical_decompiled_code:
__int64 func15()
{
char v0; // al
__int16 v2[20]; // [rsp+0h] [rbp-28h] BYREF

intr_install_handler(8LL, do_rtc);
func6();
v0 = func11(11);
func12(11, v0 | 0x46);
func7();
func10(0x1F4u);
func13(v2);
unixtime = mktime((struct tm *)v2);
return kprintf(
"System clock set to: %d-%d-%d %d:%d:%d (unix: %lld)\n",
(unsigned int)v2[5],
(unsigned int)v2[4],
(unsigned int)v2[3],
(unsigned int)v2[2],
(unsigned int)v2[1],
(unsigned int)v2[0],
unixtime);
}

original_code:
void init_rtc(void)
{
 struct tm tim;
 intr_install_handler(8, do_rtc);
 interrupts_disable();
 rtc_write(0xb, rtc_read(0xb) | 0b1000110);
 interrupts_enable();
 msleep(500);
 rtc_get_tm(&tim);
 unixtime = mktime(&tim);
 kprintf("System clock set to: %d-%d-%d %d:%d:%d (unix: %lld)\n",
  tim.tm_year, tim.tm_mon, tim.tm_mday, tim.tm_hour, tim.tm_min,
  tim.tm_sec, unixtime);
 return;
}

canonical_original_code:
void init_rtc()
{
 struct tm tim;
 intr_install_handler(8, do_rtc);
 interrupts_disable();
 rtc_write(0xb, rtc_read(0xb) | 0b1000110);
 interrupts_enable();
 msleep(500);
 rtc_get_tm(&tim);
 unixtime = mktime(&tim);
 kprintf("System clock set to: %d-%d-%d %d:%d:%d (unix: %lld)\n",
  tim.tm_year, tim.tm_mon, tim.tm_mday, tim.tm_hour, tim.tm_min,
  tim.tm_sec, unixtime);
 return;
}

user_defined_types: struct tm { short tm_sec; short tm_min; short tm_hour; short tm_mday; short tm_mon; short tm_year; short tm_wday; short tm_yday; short tm_isdst; }


decompiled_code:
bool __fastcall list_empty(__int64 a1)
{
return *(_QWORD *)(a1 + 8) == a1;
}

canonical_decompiled_code:
bool __fastcall func4(__int64 a1)
{
return *(_QWORD *)(a1 + 8) == a1;
}

original_code:
static inline u8 list_empty(const struct list_head *head)
{
 return head->next == head;
}

canonical_original_code:
static inline unsigned char list_empty(struct list_head *head)
{
 return head->next == head;
}

variable_types: struct list_head { struct list_head *prev; } *


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

result = 0LL;
if ( a1 > 1 )
{
v2 = (a1 - 1) >> 1;
if ( v2 )
{
LODWORD(result) = 1;
do
{
LODWORD(result) = result + 1;
v2 >>= 1;
}
while ( v2 );
}
else
{
LODWORD(result) = 1;
}
result = (unsigned int)result;
}
return result;
}

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

result = 0LL;
if ( a1 > 1 )
{
v2 = (a1 - 1) >> 1;
if ( v2 )
{
LODWORD(result) = 1;
do
{
LODWORD(result) = result + 1;
v2 >>= 1;
}
while ( v2 );
}
else
{
LODWORD(result) = 1;
}
result = (unsigned int)result;
}
return result;
}

original_code:
static inline u64 num2power(u64 x)
{
 unsigned power = 1;
 if (x == 0 || x == 1)
  return 0;
 x--;
 while (x >>= 1)
  power++;
 return power;
}

canonical_original_code:
static inline unsigned long num2power(unsigned long x)
{
 unsigned int power = 1;
 if (x == 0 || x == 1)
  return 0;
 x--;
 while (x >>= 1)
  power++;
 return power;
}

variable_types:
x
unsigned long

variable_types:
power
unsigned int


decompiled_code:
__int64 __fastcall kma_blocksizes_idx(char a1)
{
unsigned int v1; // edx

if ( 0x4000u >> a1 )
return 0;
v1 = 0;
do
++v1;
while ( !(blocksizes[(char)v1] >> a1) );
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func6(char a1)
{
unsigned int v1; // edx

if ( 0x4000u >> a1 )
return 0;
v1 = 0;
do
++v1;
while ( !(blocksizes[(char)v1] >> a1) );
return v1;
}

original_code:
static u8 kma_blocksizes_idx(u8 power)
{
 s8 i = 0;
 while (blocksizes[i] < (1 << power))
  i++;
 return i;
}

canonical_original_code:
static unsigned char kma_blocksizes_idx(unsigned char power)
{
 char i = 0;
 while (blocksizes[i] < (1 << power))
  i++;
 return i;
}

variable_types:
power
unsigned char

variable_types: i char


decompiled_code:
__int64 kma_free_page()
{
--kma[8];
return free_page();
}

canonical_decompiled_code:
__int64 func8()
{
--kma[8];
return free_page();
}

original_code:
static inline void kma_free_page(void *addr)
{
 kma.npages--;
 free_page(addr);
}

variable_types:
addr
void *


decompiled_code:
__int64 __fastcall bitscan_right(__int64 _RDI)
{
__int64 result; // rax

if ( !_RDI )
return 0xFFFFFFFFLL;
__asm { tzcnt rax, rdi }
return result;
}

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

if ( !_RDI )
return 0xFFFFFFFFLL;
__asm { tzcnt rax, rdi }
return result;
}

original_code:
inline static s8 bitscan_right(u64 word)
{
 if (word == 0)
  return -1;
 return __builtin_ctzl(word);
}

canonical_original_code:
inline static char bitscan_right(unsigned long word)
{
 if (word == 0)
  return -1;
 return __builtin_ctzl(word);
}

variable_types:
word
unsigned long


decompiled_code:
char *kma_early_alloc_next_busy()
{
char v0; // bl

if ( kma_early_init_done )
return (char *)kcache_alloc(busy_block_cache);
v0 = bitscan_right(~bblock_bmask);
if ( v0 == -1 )
kpanic("Oops! kma memory exhausted :(\n");
bitset(&bblock_bmask, v0);
return (char *)&bblock + 40 * v0;
}

canonical_decompiled_code:
char *func17()
{
char v0; // bl

if ( kma_early_init_done )
return (char *)kcache_alloc(busy_block_cache);
v0 = func16(~bblock_bmask);
if ( v0 == -1 )
kpanic("Oops! kma memory exhausted :(\n");
func0(&bblock_bmask, v0);
return (char *)&bblock + 40 * v0;
}

original_code:
static void *kma_early_alloc_next_busy(void)
{
 s8 bit;
 if (kma_early_init_done)
  return kcache_alloc(busy_block_cache);
 bit = bitscan_right(~bblock_bmask);
 if (bit == -1)
  kpanic("Oops! kma memory exhausted :(\n");
 bitset(&bblock_bmask, bit);
 return &bblock[bit];
}

canonical_original_code:
static void *kma_early_alloc_next_busy()
{
 char bit;
 if (kma_early_init_done)
  return kcache_alloc(busy_block_cache);
 bit = bitscan_right(~bblock_bmask);
 if (bit == -1)
  kpanic("Oops! kma memory exhausted :(\n");
 bitset(&bblock_bmask, bit);
 return &bblock[bit];
}

variable_types: bit char


decompiled_code:
__int64 kma_init()
{
_QWORD *v0; // rbx

list_init(kma);
v0 = &unk_1238;
do
{
list_init(v0);
v0 += 2;
}
while ( v0 != (_QWORD *)((char *)&unk_1238 + 128) );
tree_init(&unk_12B8, kma_busy_cmp);
kma_init_done = 1;
busy_block_cache = kcache_create("kma", 40LL);
kma_early_init_done = 1;
return init_kmalloc();
}

canonical_decompiled_code:
__int64 func19()
{
_QWORD *v0; // rbx

func2(kma);
v0 = &unk_1238;
do
{
func2(v0);
v0 += 2;
}
while ( v0 != (_QWORD *)((char *)&unk_1238 + 128) );
tree_init(&unk_12B8, kma_busy_cmp);
kma_init_done = 1;
busy_block_cache = kcache_create("kma", 40LL);
kma_early_init_done = 1;
return init_kmalloc();
}

original_code:
void kma_init(void)
{
 u8 i;
 list_init(&kma.pages);
 for (i = 0; i < (sizeof(blocksizes) / sizeof(typeof (blocksizes[0]))); i++)
  list_init(&kma.free[i].l);
 tree_init(&kma.used, kma_busy_cmp);
 kma_init_done = 1;
 busy_block_cache = kcache_create("kma", sizeof(struct busy_block));
 kma_early_init_done = 1;
 init_kmalloc();
 return;
}

canonical_original_code:
void kma_init()
{
 unsigned char i;
 list_init(&kma.pages);
 for (i = 0; i < (sizeof(blocksizes) / sizeof(typeof (blocksizes[0]))); i++)
  list_init(&kma.free[i].l);
 tree_init(&kma.used, kma_busy_cmp);
 kma_init_done = 1;
 busy_block_cache = kcache_create("kma", sizeof(struct busy_block));
 kma_early_init_done = 1;
 init_kmalloc();
 return;
}

variable_types:
i
unsigned char

user_defined_types: struct busy_block { struct tnode t; void *addr; unsigned char size_idx; }

user_defined_types: struct tnode { union { struct { struct tnode *l; }; struct tnode *n[2]; }; struct tnode *parent; }


decompiled_code:
__int64 sys_read()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 0LL;
}

original_code:
s64 sys_read()
{
 return 0;
}

canonical_original_code:
long sys_read()
{
 return 0;
}


decompiled_code:
__int64 sys_open()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
return 0LL;
}

original_code:
s64 sys_open(const char *path, int flags, mode_t mode)
{
 return 0;
}

canonical_original_code:
long sys_open(char *path, int flags, unsigned long mode)
{
 return 0;
}

variable_types:
path
char *

variable_types: flags int

variable_types:
mode
unsigned long


decompiled_code:
__int64 sys_close()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func2()
{
return 0LL;
}

original_code:
s64 sys_close()
{
 return 0;
}

canonical_original_code:
long sys_close()
{
 return 0;
}


decompiled_code:
__int64 __fastcall sys_write(int a1, __int64 a2)
{
if ( (unsigned int)(a1 - 1) <= 1 )
kprintf(a2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, __int64 a2)
{
if ( (unsigned int)(a1 - 1) <= 1 )
kprintf(a2);
return 0LL;
}

original_code:
s64 sys_write(int fd, const void *buf, size_t count)
{
 if (fd < 1 || fd > 2)
  return 0;
 kprintf(buf);
 return 0;
}

canonical_original_code:
long sys_write(int fd, void *buf, unsigned long count)
{
 if (fd < 1 || fd > 2)
  return 0;
 kprintf(buf);
 return 0;
}

variable_types: fd int

variable_types:
buf
void *

variable_types:
count
unsigned long


decompiled_code:
__int64 __fastcall fs_mount(__int64 a1, _BYTE *a2)
{
__int64 result; // rax
__int64 v3; // rbp

result = qword_1D8;
if ( qword_1D8 )
{
v3 = (*(__int64 (**)(void))(qword_1D8 + 16))();
if ( !v3 )
kpanic("Can't mount root!");
if ( *a2 == 47 && !a2[1] )
{
result = (*(__int64 (__fastcall **)(__int64, __int64))(*(_QWORD *)(v3 + 56) + 8LL))(v3, 2LL);
}
else
{
result = namei(a2);
if ( result )
{
if ( (*(_WORD *)(result + 16) & 0xF000) == 0x4000 )
{
if ( *(_QWORD *)(result + 96) )
result = 0LL;
}
else
{
result = 0LL;
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, _BYTE *a2)
{
__int64 result; // rax
__int64 v3; // rbp

result = qword_1D8;
if ( qword_1D8 )
{
v3 = (*(__int64 (**)(void))(qword_1D8 + 16))();
if ( !v3 )
kpanic("Can't mount root!");
if ( *a2 == 47 && !a2[1] )
{
result = (*(__int64 (__fastcall **)(__int64, __int64))(*(_QWORD *)(v3 + 56) + 8LL))(v3, 2LL);
}
else
{
result = namei(a2);
if ( result )
{
if ( (*(_WORD *)(result + 16) & 0xF000) == 0x4000 )
{
if ( *(_QWORD *)(result + 96) )
result = 0LL;
}
else
{
result = 0LL;
}
}
}
}
return result;
}

original_code:
struct inode *fs_mount(dev_t *dev, const char *dir)
{
 struct inode *inode;
 struct super *sb;
 u8 i;
 for (i = 0; i < 1; i++) {
  if (fs[i] == ((void *)0))
   return ((void *)0);
  sb = fs[i]->prepare_mount(dev);
  if (sb == ((void *)0))
   continue;
  break;
 }
 if (sb == ((void *)0))
  kpanic("Can't mount root!");
 if (*dir == '/' && *(dir + 1) == '\0')
  return sb->ops->inode_read(sb, 2);
 inode = namei(dir);
 if (inode == 0 || !(((inode->mode) & 0170000) == 0040000) || inode->covered)
  return ((void *)0);
 return inode;
}

canonical_original_code:
struct inode *fs_mount(struct dev_t *dev, char *dir)
{
 struct inode *inode;
 struct super *sb;
 unsigned char i;
 for (i = 0; i < 1; i++) {
  if (fs[i] == ((void *)0))
   return ((void *)0);
  sb = fs[i]->prepare_mount(dev);
  if (sb == ((void *)0))
   continue;
  break;
 }
 if (sb == ((void *)0))
  kpanic("Can't mount root!");
 if (*dir == '/' && *(dir + 1) == '\0')
  return sb->ops->inode_read(sb, 2);
 inode = namei(dir);
 if (inode == 0 || !(((inode->mode) & 0170000) == 0040000) || inode->covered)
  return ((void *)0);
 return inode;
}

variable_types:
dev
struct dev_t *

variable_types:
dir
char *

variable_types: struct inode { struct super { struct dev_t { unsigned int major; unsigned int minor; } dev; unsigned long blocksize; unsigned long blocksizephys; struct list_head { struct list_head *prev; } bcache; struct list_head icache; struct super_ops { struct super *(*super_read)(struct dev_t *dev); struct inode *(*inode_read)(struct super *sb, unsigned long inum); } *ops; unsigned long magic; unsigned long time; struct inode *mounted; unsigned int flags; void *priv; } *sb; unsigned long num; unsigned short mode; unsigned char nlinks; unsigned int uid; unsigned int gid; unsigned long filesize; unsigned long nblocks; unsigned long atime; unsigned long mtime; unsigned long ctime; unsigned int flags; unsigned int count; struct list_head l; struct super *covered; struct inode_ops { struct inode *(*lookup)(struct inode *, char *, unsigned char); unsigned long (*bmap)(struct inode *, long); struct bhead { struct list_head l; unsigned long bnum; void *data; unsigned char count; } *(*block_read)(struct inode *, long); } *ops; void *priv; } *

variable_types: struct super { struct dev_t { unsigned int major; unsigned int minor; } dev; unsigned long blocksize; unsigned long blocksizephys; struct list_head { struct list_head *prev; } bcache; struct list_head icache; struct super_ops { struct super *(*super_read)(struct dev_t *dev); struct inode { struct super *sb; unsigned long num; unsigned short mode; unsigned char nlinks; unsigned int uid; unsigned int gid; unsigned long filesize; unsigned long nblocks; unsigned long atime; unsigned long mtime; unsigned long ctime; unsigned int flags; unsigned int count; struct list_head l; struct super *covered; struct inode_ops { struct inode *(*lookup)(struct inode *, char *, unsigned char); unsigned long (*bmap)(struct inode *, long); struct bhead { struct list_head l; unsigned long bnum; void *data; unsigned char count; } *(*block_read)(struct inode *, long); } *ops; void *priv; } *(*inode_read)(struct super *sb, unsigned long inum); } *ops; unsigned long magic; unsigned long time; struct inode *mounted; unsigned int flags; void *priv; } *


decompiled_code:
__int64 __fastcall fs_register(__int64 a1)
{
unsigned __int8 v1; // al
unsigned int v2; // edx

if ( id_0[0] )
kpanic("Too many registered filesystems!");
v1 = id_0[0];
v2 = ++id_0[0];
qword_1D8[v1] = a1;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
unsigned __int8 v1; // al
unsigned int v2; // edx

if ( id_0[0] )
kpanic("Too many registered filesystems!");
v1 = id_0[0];
v2 = ++id_0[0];
qword_1D8[v1] = a1;
return v2;
}

original_code:
u8 fs_register(struct fs *fsn)
{
 static u8 id;
 if (id >= 1)
  kpanic("Too many registered filesystems!");
 fs[id++] = fsn;
 return id;
}

canonical_original_code:
unsigned char fs_register(struct fs *fsn)
{
 static unsigned char id;
 if (id >= 1)
  kpanic("Too many registered filesystems!");
 fs[id++] = fsn;
 return id;
}

variable_types: struct fs { char name[10]; struct super { struct dev_t { unsigned int major; unsigned int minor; } dev; unsigned long blocksize; unsigned long blocksizephys; struct list_head { struct list_head *prev; } bcache; struct list_head icache; struct super_ops { struct super *(*super_read)(struct dev_t *dev); struct inode { struct super *sb; unsigned long num; unsigned short mode; unsigned char nlinks; unsigned int uid; unsigned int gid; unsigned long filesize; unsigned long nblocks; unsigned long atime; unsigned long mtime; unsigned long ctime; unsigned int flags; unsigned int count; struct list_head l; struct super *covered; struct inode_ops { struct inode *(*lookup)(struct inode *, char *, unsigned char); unsigned long (*bmap)(struct inode *, long); struct bhead { struct list_head l; unsigned long bnum; void *data; unsigned char count; } *(*block_read)(struct inode *, long); } *ops; void *priv; } *(*inode_read)(struct super *sb, unsigned long inum); } *ops; unsigned long magic; unsigned long time; struct inode *mounted; unsigned int flags; void *priv; } *(*prepare_mount)(struct dev_t *dev); } *

variable_types:
id
unsigned char

user_defined_types: struct fs { char name[10]; struct super *(*prepare_mount)(struct dev_t *dev); }

user_defined_types: struct super_ops { struct super *(*super_read)(struct dev_t *dev); struct inode *(*inode_read)(struct super *sb, unsigned long inum); }


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

init_files();
init_ext2();
syscall_register(0LL, sys_read);
syscall_register(1LL, sys_write);
syscall_register(2LL, sys_open);
syscall_register(3LL, sys_close);
result = fs_mount(a1, "/");
root = result;
return result;
}

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

init_files();
init_ext2();
syscall_register(0LL, sys_read);
syscall_register(1LL, sys_write);
syscall_register(2LL, sys_open);
syscall_register(3LL, sys_close);
result = func4(a1, "/");
root = result;
return result;
}

original_code:
void init_fs(dev_t *rdev)
{
 init_files();
 init_ext2();
 syscall_register(0, sys_read);
 syscall_register(1, sys_write);
 syscall_register(2, sys_open);
 syscall_register(3, sys_close);
 root = fs_mount(rdev, "/");
}

canonical_original_code:
void init_fs(struct dev_t *rdev)
{
 init_files();
 init_ext2();
 syscall_register(0, sys_read);
 syscall_register(1, sys_write);
 syscall_register(2, sys_open);
 syscall_register(3, sys_close);
 root = fs_mount(rdev, "/");
}

variable_types: struct dev_t { unsigned int major; unsigned int minor; } *


iowait

decompiled_code:
void iowait()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
static inline void iowait(void)
{
 asm volatile(
  "jmp 1f\n\t"
  "1:\n\t"
  "jmp 1f\n\t"
  "1:\n\t"
  );
}

canonical_original_code:
static inline void iowait()
{
 asm volatile(
  "jmp 1f\n\t"
  "1:\n\t"
  "jmp 1f\n\t"
  "1:\n\t"
  );
}


outb

decompiled_code:
void __fastcall outb(unsigned __int16 a1, unsigned __int8 a2)
{
__outbyte(a1, a2);
iowait();
}

canonical_decompiled_code:
void __fastcall func1(unsigned __int16 a1, unsigned __int8 a2)
{
__outbyte(a1, a2);
func0();
}

original_code:
static inline void outb(u16 port, u8 val)
{
 asm volatile("outb %1, %0" : : "dN"(port), "a"(val));
 iowait();
}

canonical_original_code:
static inline void outb(unsigned short port, unsigned char val)
{
 asm volatile("outb %1, %0" : : "dN"(port), "a"(val));
 iowait();
}

variable_types:
port
unsigned short

variable_types:
val
unsigned char


inb

decompiled_code:
__int64 __fastcall inb(unsigned __int16 a1)
{
unsigned int v1; // eax
unsigned int v2; // ebx

LOBYTE(v1) = __inbyte(a1);
v2 = v1;
iowait();
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int16 a1)
{
unsigned int v1; // eax
unsigned int v2; // ebx

LOBYTE(v1) = __inbyte(a1);
v2 = v1;
func0();
return v2;
}

original_code:
static inline u8 inb(u16 port)
{
 u8 ret;
 asm volatile("inb %1, %0" : "=a"(ret) : "dN"(port));
 iowait();
 return ret;
}

canonical_original_code:
static inline unsigned char inb(unsigned short port)
{
 unsigned char ret;
 asm volatile("inb %1, %0" : "=a"(ret) : "dN"(port));
 iowait();
 return ret;
}

variable_types:
ret
unsigned char


decompiled_code:
void interrupts_disable()
{
_disable();
}

canonical_decompiled_code:
void func3()
{
_disable();
}

original_code:
static inline void interrupts_disable(void)
{
 asm volatile("cli\n\t");
}

canonical_original_code:
static inline void interrupts_disable()
{
 asm volatile("cli\n\t");
}


decompiled_code:
void interrupts_enable()
{
_enable();
}

canonical_decompiled_code:
void func4()
{
_enable();
}

original_code:
static inline void interrupts_enable(void)
{
 asm volatile("sti\n\t");
}

canonical_original_code:
static inline void interrupts_enable()
{
 asm volatile("sti\n\t");
}


ticks

decompiled_code:
unsigned __int64 ticks()
{
return __rdtsc();
}

canonical_decompiled_code:
unsigned __int64 func5()
{
return __rdtsc();
}

original_code:
static inline u64 ticks(void)
{
 u32 a;
 u32 d;
 asm volatile(
  "rdtsc\n\t"
  : "=a"(a), "=d"(d));
 return ((u64)d << 32) | a;
}

canonical_original_code:
static inline unsigned long ticks()
{
 unsigned int a;
 unsigned int d;
 asm volatile(
  "rdtsc\n\t"
  : "=a"(a), "=d"(d));
 return ((unsigned long)d << 32) | a;
}

variable_types:
a
unsigned int

variable_types:
d
unsigned int


decompiled_code:
__int64 __fastcall tsc_calibration_withpit(_QWORD *a1)
{
char v1; // al
unsigned __int64 v2; // r13
__int64 v3; // rbx
unsigned __int64 v4; // rbp
char v5; // al

v1 = inb(0x61u);
outb(0x61u, v1 & 0xFC | 1);
pit_set_timer(2u, 3, 2, 0);
v2 = ticks();
v3 = 0LL;
while ( (unsigned __int16)pit_read_count(2u) > 0xD163u )
++v3;
v4 = ticks() - v2;
v5 = inb(0x61u);
outb(0x61u, v5 & 0xFC);
*a1 = v3;
return 100 * v4;
}

canonical_decompiled_code:
__int64 __fastcall func8(_QWORD *a1)
{
char v1; // al
unsigned __int64 v2; // r13
__int64 v3; // rbx
unsigned __int64 v4; // rbp
char v5; // al

v1 = func2(0x61u);
func1(0x61u, v1 & 0xFC | 1);
func7(2u, 3, 2, 0);
v2 = func5();
v3 = 0LL;
while ( (unsigned __int16)func6(2u) > 0xD163u )
++v3;
v4 = func5() - v2;
v5 = func2(0x61u);
func1(0x61u, v5 & 0xFC);
*a1 = v3;
return 100 * v4;
}

original_code:
u64 tsc_calibration_withpit(u64 *loops)
{
 u64 cpu_hz, l = 0;
 outb(0x61, (inb(0x61) & ~0b10) | 0b1);
 pit_set_timer(2, 0b11, 0b010, 0);
 cpu_hz = ticks();
 while (pit_read_count(2) > (u16)-1 - 11932)
  l++;
 cpu_hz = (ticks() - cpu_hz) * 100;
 outb(0x61, inb(0x61) & ~0b11);
 *loops = l;
 return cpu_hz;
}

canonical_original_code:
unsigned long tsc_calibration_withpit(unsigned long *loops)
{
 unsigned long cpu_hz; unsigned long l = 0;
 outb(0x61, (inb(0x61) & ~0b10) | 0b1);
 pit_set_timer(2, 0b11, 0b010, 0);
 cpu_hz = ticks();
 while (pit_read_count(2) > (u16)-1 - 11932)
  l++;
 cpu_hz = (ticks() - cpu_hz) * 100;
 outb(0x61, inb(0x61) & ~0b11);
 *loops = l;
 return cpu_hz;
}

variable_types:
loops
unsigned long *

variable_types:
l
unsigned long


decompiled_code:
__int64 __fastcall init_pit(__int64 a1, __int16 a2)
{
pit_set_timer(0, 3, 2, a2);
return intr_install_handler(0LL, a1);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int16 a2)
{
func7(0, 3, 2, a2);
return intr_install_handler(0LL, a1);
}

original_code:
void init_pit(void *intr_handler, u32 hz)
{
 pit_set_timer(0, 0b11, 0b010, hz);
 intr_install_handler(0, intr_handler);
}

canonical_original_code:
void init_pit(void *intr_handler, unsigned int hz)
{
 pit_set_timer(0, 0b11, 0b010, hz);
 intr_install_handler(0, intr_handler);
}

variable_types:
hz
unsigned int


decompiled_code:
void move_cursor()
{
__int16 v0; // bx

v0 = (unsigned __int8)col + 80 * (unsigned __int8)line;
outb(0x3D4u, 0xEu);
outb(0x3D5u, HIBYTE(v0));
outb(0x3D4u, 0xFu);
outb(0x3D5u, v0);
}

canonical_decompiled_code:
void func2()
{
__int16 v0; // bx

v0 = (unsigned __int8)col + 80 * (unsigned __int8)line;
func1(0x3D4u, 0xEu);
func1(0x3D5u, HIBYTE(v0));
func1(0x3D4u, 0xFu);
func1(0x3D5u, v0);
}

original_code:
static inline void move_cursor(void)
{
 u16 loc = line * 80 + col;
 outb(0x3D4, 14);
 outb(0x3D5, loc >> 8);
 outb(0x3D4, 15);
 outb(0x3D5, loc);
}

canonical_original_code:
static inline void move_cursor()
{
 unsigned short loc = line * 80 + col;
 outb(0x3D4, 14);
 outb(0x3D5, loc >> 8);
 outb(0x3D4, 15);
 outb(0x3D5, loc);
}

variable_types:
loc
unsigned short


addrfrompos

decompiled_code:
__int64 __fastcall addrfrompos(unsigned __int8 a1, unsigned __int8 a2)
{
return 2 * (a2 + (__int64)(80 * a1)) - 0x7FFFFFF48000LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int8 a1, unsigned __int8 a2)
{
return 2 * (a2 + (__int64)(80 * a1)) - 0x7FFFFFF48000LL;
}

canonical_original_code:
static inline unsigned short *addrfrompos(unsigned char l, unsigned char c)
{
 return (((unsigned short *)((void *)(((unsigned long)0xb8000) + 0xffff800000000000))) + l * 80 + c);
}

variable_types:
l
unsigned char

variable_types:
c
unsigned char

return_type: unsigned short *


cls

decompiled_code:
void cls()
{
unsigned __int64 i; // rax
_DWORD *v1; // rdx

for ( i = 0xFFFF8000000B8000LL; i != 0xFFFF8000000B8FA0LL; i += 4LL )
{
v1 = (_DWORD *)i;
*v1 = 119539488;
}
col = 0;
line = 0;
move_cursor();
}

variable_types:
p
unsigned int *

variable_types:
blank
unsigned int


decompiled_code:
void __fastcall vga_writechar(unsigned __int8 a1)
{
int v1; // er12
char v2; // r13
_WORD *v3; // rax
_WORD *v4; // rbx
int v5; // edx
char v6; // cl
char v7; // al
unsigned __int8 v8; // r13
unsigned __int64 v9; // rbp
unsigned __int8 v10; // r13
unsigned __int64 v11; // rbp

LOBYTE(v1) = col;
v2 = line;
v3 = (_WORD *)addrfrompos(line, col);
v4 = v3;
if ( a1 == 8 )
{
if ( !(_BYTE)v1 )
return;
*(v3 - 1) = 1824;
col = v1 - 1;
goto LABEL_11;
}
if ( a1 != 9 )
{
if ( a1 == 10 )
{
col = 0;
v10 = v2 + 1;
line = v10;
v11 = addrfrompos(v10, 0);
if ( v10 > 0x18u )
scroll();
for ( ; v11 > (unsigned __int64)v4; *(v4 - 1) = 1824 )
++v4;
}
else if ( a1 > 0x1Fu )
{
*v3 = a1 | 0x700;
col = v1 + 1;
if ( (unsigned __int8)(v1 + 1) > 0x4Eu )
{
if ( (unsigned __int8)v2 > 0x17u )
scroll();
col = 0;
++line;
}
}
goto LABEL_11;
}
v5 = v1 + 1;
if ( (unsigned __int8)v1 <= 0x47u )
{
do
{
*v4++ = 1824;
v6 = v5 & 7;
v7 = v5++;
}
while ( v6 );
col = v7;
LABEL_11:
move_cursor();
return;
}
col = 0;
v8 = v2 + 1;
line = v8;
v9 = addrfrompos(v8, 0);
if ( v8 > 0x18u )
scroll();
for ( ; v9 > (unsigned __int64)v4; *(v4 - 1) = 1824 )
++v4;
}

canonical_original_code:
void vga_writechar(unsigned char c)
{
 unsigned short *p = addrfrompos(line, col);
 if (c == '\b') {
  if (col == 0)
   return;
  *(p - 1) = ((' ') | (((c_front) | (c_back) << 4) << 8));
  col--;
 } else if (c == '\t') {
  if (col >= 80 - 8) {
   unsigned short *p2 = addrfrompos(++line, col = 0);
   if (line >= 25)
    scroll();
   while (p < p2)
    *p++ = ((' ') | (((c_front) | (c_back) << 4) << 8));
   return;
  }
  do {
   *p++ = ((' ') | (((c_front) | (c_back) << 4) << 8));
  } while (++col % 8 != 0);
 } else if (c == '\n') {
  unsigned short *p2 = addrfrompos(++line, col = 0);
  if (line >= 25)
   scroll();
  while (p < p2)
   *p++ = ((' ') | (((c_front) | (c_back) << 4) << 8));
 } else if (c >= ' ') {
  *p = ((c) | (((c_front) | (c_back) << 4) << 8));
  col++;
  if (col >= 80 - 1) {
   if (line >= 25 - 1)
    scroll();
   col = 0;
   line++;
  }
 }
 move_cursor();
 return;
}

variable_types:
p
unsigned short *


canonical_decompiled_code:
void func0()
{
_enable();
}


kmain

decompiled_code:
__int64 kmain()
{
cls();
kputs("Booting jOS kernel 0.0.3!\n");
init_acpi();
init_exceptions();
init_interrupts();
init_memory();
init_cpu();
interrupts_enable();
init_timers();
init_keyboard();
init_disks();
init_fs(&root);
init_syscall();
current = init_task();
exec("/sbin/init");
kernel_idle();
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
cls();
kputs("Booting jOS kernel 0.0.3!\n");
init_acpi();
init_exceptions();
init_interrupts();
init_memory();
init_cpu();
func0();
init_timers();
init_keyboard();
init_disks();
init_fs(&root);
init_syscall();
current = init_task();
exec("/sbin/init");
kernel_idle();
return 0LL;
}

original_code:
u16 kmain(void)
{
 cls();
 kputs("Booting jOS kernel " "0.0.3" "!\n");
 init_acpi();
 init_exceptions();
 init_interrupts();
 init_memory();
 init_cpu();
 interrupts_enable();
 init_timers();
 init_keyboard();
 init_disks();
 init_fs(&root);
 init_syscall();
 current = init_task();
 exec("/sbin/init");
 kernel_idle();
 return 0;
}

canonical_original_code:
unsigned short kmain()
{
 cls();
 kputs("Booting jOS kernel " "0.0.3" "!\n");
 init_acpi();
 init_exceptions();
 init_interrupts();
 init_memory();
 init_cpu();
 interrupts_enable();
 init_timers();
 init_keyboard();
 init_disks();
 init_fs(&root);
 init_syscall();
 current = init_task();
 exec("/sbin/init");
 kernel_idle();
 return 0;
}


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

if ( a2 > 2 )
result = kpanic("CDEV: too big major number registered.");
return result;
}

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

if ( a2 > 2 )
result = kpanic("CDEV: too big major number registered.");
return result;
}

original_code:
void cdev_register_dev(struct cdevsw *cdev, u32 major)
{
 if (major > 2)
  kpanic("CDEV: too big major number registered.");
 cdevsw[major] = cdev;
}

canonical_original_code:
void cdev_register_dev(struct cdevsw *cdev, unsigned int major)
{
 if (major > 2)
  kpanic("CDEV: too big major number registered.");
 cdevsw[major] = cdev;
}

variable_types: struct cdevsw { void (*open)(); void (*close)(); void (*read)(); void (*write)(); void (*ioctl)(); void (*mmap)(); void (*segmap)(); void (*poll)(); void (*halt)(); } *

variable_types:
major
unsigned int


decompiled_code:
void init_cdev()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void init_cdev(void)
{
}

canonical_original_code:
void init_cdev()
{
}


mmap

decompiled_code:
void *__fastcall mmap(void *buf, size_t nbytes, __int64 a3, char a4, int a5, __off_t a6)
{
void *result; // rax
unsigned __int64 v10; // rax
char *v11; // rax

result = (void *)virt2phys();
if ( nbytes > 0x200000 || result )
return 0LL;
if ( (a4 & 0x20) == 0 )
{
map_one_page(buf, buf);
lseek(a5, a6, 0);
v10 = read(a5, buf, nbytes);
if ( v10 > 0x1FFFFF )
{
result = buf;
}
else
{
v11 = (char *)buf + v10;
do
*v11++ = 0;
while ( v11 != (char *)buf + 0x200000 );
result = buf;
}
}
return result;
}

canonical_decompiled_code:
void *__fastcall func0(void *buf, size_t nbytes, __int64 a3, char a4, int a5, __off_t a6)
{
void *result; // rax
unsigned __int64 v10; // rax
char *v11; // rax

result = (void *)virt2phys();
if ( nbytes > 0x200000 || result )
return 0LL;
if ( (a4 & 0x20) == 0 )
{
map_one_page(buf, buf);
lseek(a5, a6, 0);
v10 = read(a5, buf, nbytes);
if ( v10 > 0x1FFFFF )
{
result = buf;
}
else
{
v11 = (char *)buf + v10;
do
*v11++ = 0;
while ( v11 != (char *)buf + 0x200000 );
result = buf;
}
}
return result;
}

original_code:
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
{
 size_t bytes;
 if (virt2phys(addr) != ((void *)0))
  return ((void *)0);
 if (length > 0x200000)
  return ((void *)0);
 if ((flags & 0x20) == 0x20)
  return ((void *)0);
 map_one_page(addr, addr);
 lseek(fd, offset, 0);
 bytes = read(fd, addr, length);
 while (bytes < 0x200000)
  *((u8 *)addr + bytes++) = '\0';
 return addr;
}

canonical_original_code:
void *mmap(void *addr, unsigned long length, int prot, int flags, int fd, long offset)
{
 unsigned long bytes;
 if (virt2phys(addr) != ((void *)0))
  return ((void *)0);
 if (length > 0x200000)
  return ((void *)0);
 if ((flags & 0x20) == 0x20)
  return ((void *)0);
 map_one_page(addr, addr);
 lseek(fd, offset, 0);
 bytes = read(fd, addr, length);
 while (bytes < 0x200000)
  *((unsigned char *)addr + bytes++) = '\0';
 return addr;
}

variable_types:
addr
void *

variable_types:
length
unsigned long

variable_types: prot int

variable_types: flags int

variable_types: fd int

variable_types: offset long

variable_types:
bytes
unsigned long


decompiled_code:
__int64 __fastcall tree_first_last(__int64 a1, unsigned __int8 a2)
{
__int64 result; // rax

do
{
result = a1;
a1 = *(_QWORD *)(a1 + 8LL * a2);
}
while ( a1 );
return result;
}

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

do
{
result = a1;
a1 = *(_QWORD *)(a1 + 8LL * a2);
}
while ( a1 );
return result;
}

original_code:
static inline struct tnode *tree_first_last(struct tnode *node, u8 dir)
{
 while (node->n[dir])
  node = node->n[dir];
 return node;
}

canonical_original_code:
static inline struct tnode *tree_first_last(struct tnode *node, unsigned char dir)
{
 while (node->n[dir])
  node = node->n[dir];
 return node;
}

variable_types:
node
struct tnode *

variable_types:
dir
unsigned char

return_type: struct tnode { union { struct { struct tnode *l; }; struct tnode *n[2]; }; struct tnode *parent; } *


decompiled_code:
__int64 __fastcall tree_prev_next(__int64 a1, unsigned __int8 a2)
{
__int64 result; // rax
__int64 v3; // rdi
__int64 v4; // rdx

result = a1;
v3 = *(_QWORD *)(a1 + 8LL * a2);
if ( v3 )
return tree_first_last(v3, a2 == 0);
do
{
v4 = result;
result = *(_QWORD *)(result + 16);
}
while ( result && *(_QWORD *)(result + 8LL * a2) == v4 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, unsigned __int8 a2)
{
__int64 result; // rax
__int64 v3; // rdi
__int64 v4; // rdx

result = a1;
v3 = *(_QWORD *)(a1 + 8LL * a2);
if ( v3 )
return func7(v3, a2 == 0);
do
{
v4 = result;
result = *(_QWORD *)(result + 16);
}
while ( result && *(_QWORD *)(result + 8LL * a2) == v4 );
return result;
}

original_code:
static inline struct tnode *tree_prev_next(struct tnode *node, u8 dir)
{
 if (node->n[dir])
  return tree_first_last(node->n[dir], !dir);
 while (node->parent && node->parent->n[dir] == node) {
  if (node->parent == ((void *)0))
   return ((void *)0);
  node = node->parent;
 }
 return node->parent;
}

canonical_original_code:
static inline struct tnode *tree_prev_next(struct tnode *node, unsigned char dir)
{
 if (node->n[dir])
  return tree_first_last(node->n[dir], !dir);
 while (node->parent && node->parent->n[dir] == node) {
  if (node->parent == ((void *)0))
   return ((void *)0);
  node = node->parent;
 }
 return node->parent;
}


decompiled_code:
__int64 __fastcall tree_last(__int64 *a1)
{
return tree_first_last(*a1, 1u);
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 *a1)
{
return func7(*a1, 1u);
}

original_code:
struct tnode *tree_last(struct tree *tree)
{
 return tree_first_last(tree->root, 1);
}

variable_types: struct tree { struct tnode *root; char (*f_cmp)(struct tnode *, struct tnode *); } *


decompiled_code:
__int64 __fastcall tree_prev(__int64 a1)
{
return tree_prev_next(a1, 0);
}

canonical_decompiled_code:
__int64 __fastcall func14(__int64 a1)
{
return func8(a1, 0);
}

original_code:
struct tnode *tree_prev(struct tnode *node)
{
 return tree_prev_next(node, 0);
}


decompiled_code:
__int64 __fastcall tree_next(__int64 a1)
{
return tree_prev_next(a1, 1u);
}

canonical_decompiled_code:
__int64 __fastcall func15(__int64 a1)
{
return func8(a1, 1u);
}

original_code:
struct tnode *tree_next(struct tnode *node)
{
 return tree_prev_next(node, 1);
}


original_code:
u8 tree_detach(struct tree *tree, struct tnode *node)
{
 struct tnode *n;
 u8 pos;
 n = lookup_node(tree, node, ((void *)0), &pos);
 if (n == ((void *)0))
  return 0;
 if (is_leaf_node(n))
  link_children_to_parent(tree, n, pos);
 else if (n->l == ((void *)0) || n->r == ((void *)0))
  link_children_to_parent(tree, n, n->l == ((void *)0));
 else if (n->l->r == ((void *)0) || n->r->l == ((void *)0)) {
  u8 pos = (n->l->r != ((void *)0));
  link_children_to_parent(tree, n, pos);
  n->n[!pos]->parent = n->n[pos];
  n->n[pos]->n[!pos] = n->n[!pos];
 } else if (n->parent && n->parent->n[!pos] == ((void *)0)) {
  link_children_to_parent(tree, n, pos);
 } else {
  u8 pos = ((u64)n >> 6) & 1;
  struct tnode *new = tree_prev_next(n, pos);
  new->parent->n[!pos] = new->n[pos];
  new->l = n->l;
  new->r = n->r;
  new->parent = n->parent;
  n->l->parent = n->r->parent = new;
  if (n == tree->root)
   tree->root = new;
 }
 node->parent = node->l = node->r = ((void *)0);
 return 1;
}

canonical_original_code:
unsigned char tree_detach(struct tree *tree, struct tnode *node)
{
 struct tnode *n;
 unsigned char pos;
 n = lookup_node(tree, node, ((void *)0), &pos);
 if (n == ((void *)0))
  return 0;
 if (is_leaf_node(n))
  link_children_to_parent(tree, n, pos);
 else if (n->l == ((void *)0) || n->r == ((void *)0))
  link_children_to_parent(tree, n, n->l == ((void *)0));
 else if (n->l->r == ((void *)0) || n->r->l == ((void *)0)) {
  unsigned char pos = (n->l->r != ((void *)0));
  link_children_to_parent(tree, n, pos);
  n->n[!pos]->parent = n->n[pos];
  n->n[pos]->n[!pos] = n->n[!pos];
 } else if (n->parent && n->parent->n[!pos] == ((void *)0)) {
  link_children_to_parent(tree, n, pos);
 } else {
  unsigned char pos = ((u64)n >> 6) & 1;
  struct tnode *new = tree_prev_next(n, pos);
  new->parent->n[!pos] = new->n[pos];
  new->l = n->l;
  new->r = n->r;
  new->parent = n->parent;
  n->l->parent = n->r->parent = new;
  if (n == tree->root)
   tree->root = new;
 }
 node->parent = node->l = node->r = ((void *)0);
 return 1;
}

variable_types: struct tree { struct tnode { union { struct { struct tnode *l; }; struct tnode *n[2]; }; struct tnode *parent; } *root; char (*f_cmp)(struct tnode *, struct tnode *); } *

variable_types:
pos
unsigned char

user_defined_types: struct tree { struct tnode *root; char (*f_cmp)(struct tnode *, struct tnode *); }


decompiled_code:
__int64 __fastcall tree_attach(__int64 *a1, _QWORD *a2)
{
__int64 v2; // rdx
__int64 result; // rax
unsigned __int8 v4; // [rsp+7h] [rbp-21h] BYREF
__int64 v5[4]; // [rsp+8h] [rbp-20h] BYREF

v2 = lookup_node(a1, (__int64)a2, v5, (char *)&v4);
result = 1LL;
if ( !v2 )
{
a2[1] = 0LL;
*a2 = 0LL;
result = v5[0];
a2[2] = v5[0];
if ( result )
{
*(_QWORD *)(result + 8LL * v4) = a2;
result = 0LL;
}
else
{
*a1 = (__int64)a2;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func18(__int64 *a1, _QWORD *a2)
{
__int64 v2; // rdx
__int64 result; // rax
unsigned __int8 v4; // [rsp+7h] [rbp-21h] BYREF
__int64 v5[4]; // [rsp+8h] [rbp-20h] BYREF

v2 = func9(a1, (__int64)a2, v5, (char *)&v4);
result = 1LL;
if ( !v2 )
{
a2[1] = 0LL;
*a2 = 0LL;
result = v5[0];
a2[2] = v5[0];
if ( result )
{
*(_QWORD *)(result + 8LL * v4) = a2;
result = 0LL;
}
else
{
*a1 = (__int64)a2;
}
}
return result;
}

original_code:
u8 tree_attach(struct tree *tree, struct tnode *node)
{
 struct tnode *n, *p;
 u8 pos;
 n = lookup_node(tree, node, &p, &pos);
 if (n != ((void *)0))
  return 1;
 node->l = node->r = ((void *)0);
 node->parent = p;
 if (p == ((void *)0))
  tree->root = node;
 else
  p->n[pos] = node;
 return 0;
}

canonical_original_code:
unsigned char tree_attach(struct tree *tree, struct tnode *node)
{
 struct tnode *n; struct tnode *p;
 unsigned char pos;
 n = lookup_node(tree, node, &p, &pos);
 if (n != ((void *)0))
  return 1;
 node->l = node->r = ((void *)0);
 node->parent = p;
 if (p == ((void *)0))
  tree->root = node;
 else
  p->n[pos] = node;
 return 0;
}

variable_types: struct tnode { union { struct { struct tnode *l; }; struct tnode *n[2]; }; struct tnode *parent; } *


bitset

decompiled_code:
void __fastcall bitset(signed __int64 *a1, unsigned __int64 a2)
{
_bittestandset64(a1, a2);
}

canonical_decompiled_code:
void __fastcall func7(signed __int64 *a1, unsigned __int64 a2)
{
_bittestandset64(a1, a2);
}

original_code:
inline static void bitset(u64 *word, u64 pos)
{
 asm volatile(
  "bts %[bit], %[reg]\n"
  : [reg] "+m"(*word)
  : [bit] "ir" (pos)
  : "cc" );
}

canonical_original_code:
inline static void bitset(unsigned long *word, unsigned long pos)
{
 asm volatile(
  "bts %[bit], %[reg]\n"
  : [reg] "+m"(*word)
  : [bit] "ir" (pos)
  : "cc" );
}

variable_types:
word
unsigned long *

variable_types:
pos
unsigned long


bitclear

decompiled_code:
void __fastcall bitclear(signed __int64 *a1, unsigned __int64 a2)
{
_bittestandreset64(a1, a2);
}

canonical_decompiled_code:
void __fastcall func8(signed __int64 *a1, unsigned __int64 a2)
{
_bittestandreset64(a1, a2);
}

original_code:
inline static void bitclear(u64 *word, u64 pos)
{
 asm volatile(
  "btr %[bit], %[reg]\n"
  : [reg] "+m"(*word)
  : [bit] "ir" (pos)
  : "cc" );
}

canonical_original_code:
inline static void bitclear(unsigned long *word, unsigned long pos)
{
 asm volatile(
  "btr %[bit], %[reg]\n"
  : [reg] "+m"(*word)
  : [bit] "ir" (pos)
  : "cc" );
}


bitclearptr

decompiled_code:
void __fastcall bitclearptr(__int64 a1, unsigned __int16 a2)
{
bitclear((signed __int64 *)(((a2 >> 3) & 0x1FF8) + a1), a2 & 0x3F);
}

canonical_decompiled_code:
void __fastcall func9(__int64 a1, unsigned __int16 a2)
{
func8((signed __int64 *)(((a2 >> 3) & 0x1FF8) + a1), a2 & 0x3F);
}

original_code:
inline static void bitclearptr(void *ptr, u16 pos)
{
 bitclear(ptr + ((pos >> 3) & ~0b111), pos & 0b111111);
}

canonical_original_code:
inline static void bitclearptr(void *ptr, unsigned short pos)
{
 bitclear(ptr + ((pos >> 3) & ~0b111), pos & 0b111111);
}

variable_types:
ptr
void *

variable_types:
pos
unsigned short


decompiled_code:
bool __fastcall list_empty(__int64 a1)
{
return *(_QWORD *)(a1 + 8) == a1;
}

canonical_decompiled_code:
bool __fastcall func12(__int64 a1)
{
return *(_QWORD *)(a1 + 8) == a1;
}

original_code:
static inline u8 list_empty(const struct list_head *head)
{
 return head->next == head;
}

canonical_original_code:
static inline unsigned char list_empty(struct list_head *head)
{
 return head->next == head;
}

variable_types: struct list_head { struct list_head *prev; } *


decompiled_code:
__int64 __fastcall slab_init(__int64 a1, __int64 a2, unsigned __int64 a3)
{
unsigned int v3; // eax
unsigned int v4; // eax
unsigned __int16 v5; // dx
unsigned __int16 v6; // cx
__int64 result; // rax

v3 = (a2 - 32 - (unsigned __int64)(unsigned int)(((a2 - 32 - (((a2 - 32) / a3) >> 3)) / a3) >> 3)) / a3;
*(_DWORD *)(a1 + 24) = v3;
v4 = v3 >> 6;
if ( v4 )
{
v5 = 0;
do
{
v6 = v5++;
*(_QWORD *)(a1 + 8LL * v6 + 32) = 0LL;
}
while ( v5 < v4 );
}
else
{
v5 = 0;
}
result = a1 + 8LL * v5 + 40;
*(_QWORD *)(a1 + 16) = result;
*(_DWORD *)(a1 + 28) = 0;
return result;
}

original_code:
static void slab_init(void *addr, size_t sz, size_t osz)
{
 struct slab *slab;
 u32 bmasksize;
 u16 i = 0;
 slab = addr;
 bmasksize = ((sz - sizeof(struct slab) - (((sz - sizeof(struct slab)) / osz) >> 3)) / osz) >> 3;
 slab->objcount = (sz - sizeof(struct slab) - bmasksize) / osz;
 while (i < (slab->objcount >> 6))
  slab->used[i++] = 0;
 slab->first_element = slab->used + i + 1;
 slab->objused = 0;
 return;
}

canonical_original_code:
static void slab_init(void *addr, unsigned long sz, unsigned long osz)
{
 struct slab *slab;
 unsigned int bmasksize;
 unsigned short i = 0;
 slab = addr;
 bmasksize = ((sz - sizeof(struct slab) - (((sz - sizeof(struct slab)) / osz) >> 3)) / osz) >> 3;
 slab->objcount = (sz - sizeof(struct slab) - bmasksize) / osz;
 while (i < (slab->objcount >> 6))
  slab->used[i++] = 0;
 slab->first_element = slab->used + i + 1;
 slab->objused = 0;
 return;
}

variable_types:
addr
void *

variable_types:
sz
unsigned long

variable_types:
osz
unsigned long

variable_types: struct slab { struct list_head { struct list_head *prev; } l; void *first_element; unsigned int objcount; unsigned int objused; union { unsigned char usedb[]; unsigned long used[]; }; } *

variable_types:
bmasksize
unsigned int

variable_types:
i
unsigned short

user_defined_types: struct slab { struct list_head l; void *first_element; unsigned int objcount; unsigned int objused; union { unsigned char usedb[]; unsigned long used[]; }; }


original_code:
static u32 cache_get_size(size_t objsz)
{
 u16 sz;
 objsz >>= 10;
 if (objsz <= 8)
  sz = 64;
 else if (objsz <= 16)
  sz = 128;
 else if (objsz <= 32)
  sz = 256;
 else
  sz = 512;
 return sz << 10;
}

canonical_original_code:
static unsigned int cache_get_size(unsigned long objsz)
{
 unsigned short sz;
 objsz >>= 10;
 if (objsz <= 8)
  sz = 64;
 else if (objsz <= 16)
  sz = 128;
 else if (objsz <= 32)
  sz = 256;
 else
  sz = 512;
 return sz << 10;
}

variable_types:
objsz
unsigned long

variable_types:
sz
unsigned short

return_type: unsigned int