decompiled_code:
__int64 kma_init()
{
void (__fastcall *v0)(_QWORD *); // rdx
_QWORD *v1; // rax
_QWORD *v2; // rcx

list_init(&kma);
v1 = &kma + 3;
do
v0(v1);
while ( v1 != v2 );
tree_init(&unk_13B8, 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()
{
void (__fastcall *v0)(_QWORD *); // rdx
_QWORD *v1; // rax
_QWORD *v2; // rcx

func0(&kma);
v1 = &kma + 3;
do
v0(v1);
while ( v1 != v2 );
tree_init(&unk_13B8, 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; }


repo: acceso/jOS

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

return_type: long


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

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

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


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

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:
__int64 __fastcall fs_register(__int64 a1)
{
__int64 v1; // rax
unsigned __int8 v2; // r8
__int64 result; // rax

v1 = id_0[0];
if ( id_0[0] )
{
kpanic("Too many registered filesystems!");
v1 = id_0[0];
}
v2 = v1 + 1;
qword_230[v1] = a1;
result = (unsigned int)(v1 + 1);
id_0[0] = v2;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1)
{
__int64 v1; // rax
unsigned __int8 v2; // r8
__int64 result; // rax

v1 = id_0[0];
if ( id_0[0] )
{
kpanic("Too many registered filesystems!");
v1 = id_0[0];
}
v2 = v1 + 1;
qword_230[v1] = a1;
result = (unsigned int)(v1 + 1);
id_0[0] = v2;
return result;
}

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

return_type: 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; } *

return_type: void


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:
void __fastcall inb(unsigned __int16 a1)
{
unsigned __int8 v1; // al

v1 = __inbyte(a1);
iowait();
}

canonical_decompiled_code:
void __fastcall func2(unsigned __int16 a1)
{
unsigned __int8 v1; // al

v1 = __inbyte(a1);
func0();
}

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

return_type: unsigned long


decompiled_code:
__int64 tsc_calibration_withpit()
{
char v0; // al
__int64 v1; // rdx
__int64 v2; // rcx
unsigned __int64 v3; // rbp
__int64 v4; // r8
unsigned __int16 (__fastcall *v5)(__int64, _QWORD, __int64, __int64, __int64); // r9
__int64 v6; // r8
unsigned __int64 v7; // rax
__int64 v8; // rdx
__int64 (__fastcall *v9)(__int64, _QWORD, __int64, unsigned __int64); // r11
char v10; // al
__int64 v11; // r8
_QWORD *v12; // r10
__int64 v13; // rcx

inb(0x61u);
outb(0x61u, v0 & 0xFC | 1);
pit_set_timer_constprop_0(2, 0);
v3 = ticks();
while ( v5(2LL, 0LL, v1, v2, v4) > 0xD163u )
v4 = v6 + 1;
v7 = ticks();
v10 = v9(97LL, 0LL, v8, v7 - v3);
outb(0x61u, v10 & 0xFC);
*v12 = v11;
return 100 * v13;
}

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)
{
__int64 v2; // r9

pit_set_timer_constprop_0(0, a2);
return intr_install_handler(0LL, v2);
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int16 a2)
{
__int64 v2; // r9

func6(0, a2);
return intr_install_handler(0LL, v2);
}

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:
__int64 move_cursor()
{
unsigned __int8 v0; // ch
void (__fastcall *v1)(unsigned __int16, unsigned __int8); // r8
void (__fastcall *v2)(unsigned __int16, unsigned __int8); // r8
unsigned __int8 v3; // cl
__int64 (__fastcall *v4)(__int64, _QWORD); // r8

outb(0x3D4u, 0xEu);
v1(0x3D5u, v0);
v2(0x3D4u, 0xFu);
return v4(981LL, v3);
}

canonical_decompiled_code:
__int64 func2()
{
unsigned __int8 v0; // ch
void (__fastcall *v1)(unsigned __int16, unsigned __int8); // r8
void (__fastcall *v2)(unsigned __int16, unsigned __int8); // r8
unsigned __int8 v3; // cl
__int64 (__fastcall *v4)(__int64, _QWORD); // r8

func1(0x3D4u, 0xEu);
v1(0x3D5u, v0);
v2(0x3D4u, 0xFu);
return v4(981LL, v3);
}

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:
__int64 __fastcall cls(__int64 a1, __int64 a2)
{
unsigned __int64 i; // rax
_DWORD *v3; // rdx

for ( i = 0xFFFF8000000B8000LL; i != 0xFFFF8000000B8FA0LL; i += 4LL )
{
v3 = (_DWORD *)i;
*v3 = 119539488;
}
col = 0;
line = 0;
return move_cursor(a1, a2, v3, 0xFFFF8000000B8FA0LL);
}

variable_types:
p
unsigned int *

variable_types:
blank
unsigned int


decompiled_code:
_WORD *vga_writechar()
{
__int64 v0; // rsi
__int64 v1; // rdi
unsigned __int8 v2; // bp
_WORD *result; // rax
__int64 v4; // rdx
__int64 v5; // rcx
__int64 (__fastcall *v6)(unsigned __int8, unsigned __int8); // r8
int v7; // er10
_BYTE *v8; // r11
_WORD *v9; // r9
unsigned int v10; // eax
__int64 v11; // rcx
char v12; // dl
__int64 v13; // rdx
unsigned __int8 v14; // bp
unsigned __int64 v15; // r9
_WORD *v16; // r10
unsigned __int8 v17; // bp
unsigned __int64 v18; // r9
unsigned __int64 v19; // r10

v0 = col;
v1 = line;
v2 = line;
result = (_WORD *)addrfrompos(line, col);
v9 = result;
if ( (_BYTE)v5 == 8 )
{
if ( (_BYTE)v7 )
{
*(result - 1) = 1824;
*v8 = v7 - 1;
result = (_WORD *)move_cursor(v1, 1824LL, v4, v5);
}
}
else if ( (_BYTE)v5 == 9 )
{
v10 = v7 + 1;
if ( (unsigned __int8)v7 > 0x47u )
{
v14 = v2 + 1;
col = 0;
line = v14;
result = (_WORD *)v6(v14, 0);
v16 = result;
if ( v14 > 0x18u )
result = (_WORD *)scroll(v14, 0LL);
for ( ; (unsigned __int64)v16 > v15; *(_WORD *)(v15 - 2) = 1824 )
v15 += 2LL;
}
else
{
do
{
++v9;
v11 = v10;
*(v9 - 1) = 1824;
v12 = v10++;
v13 = v12 & 7;
}
while ( (_BYTE)v13 );
*v8 = v11;
result = (_WORD *)move_cursor(v1, v0, v13, v11);
}
}
else
{
if ( (_BYTE)v5 == 10 )
{
v17 = v2 + 1;
v0 = 0LL;
col = 0;
v1 = v17;
line = v17;
v19 = v6(v17, 0);
if ( v17 > 0x18u )
scroll(v17, 0LL);
for ( ; v19 > v18; *(_WORD *)(v18 - 2) = 1824 )
v18 += 2LL;
}
else if ( (unsigned __int8)v5 > 0x1Fu )
{
v5 = (unsigned __int8)v5;
BYTE1(v5) = 7;
*result = v5;
*v8 = v7 + 1;
if ( (unsigned __int8)(v7 + 1) > 0x4Eu )
{
if ( v2 > 0x17u )
{
scroll(v1, v0);
v2 = line;
}
col = 0;
line = v2 + 1;
}
}
result = (_WORD *)move_cursor(v1, v0, v4, v5);
}
return result;
}

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

return_type: unsigned short


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; // rcx
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);
v11 = (char *)buf + v10;
if ( v10 <= 0x1FFFFF )
{
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; // rcx
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);
v11 = (char *)buf + v10;
if ( v10 <= 0x1FFFFF )
{
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

return_type: void *


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 ^ 1u);
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 ^ 1u);
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 func11(__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 func12(__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 func13(__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(_QWORD *a1, _QWORD *a2)
{
_QWORD *v2; // r8
__int64 result; // rax
unsigned __int8 v4; // [rsp+7h] [rbp-21h] BYREF
__int64 v5[4]; // [rsp+8h] [rbp-20h] BYREF

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

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

v2 = lookup_node((__int64)a1, (__int64)a2, v5, (char *)&v4);
result = 1LL;
if ( !v2 )
{
result = v5[0];
a2[1] = 0LL;
*a2 = 0LL;
a2[2] = result;
if ( result )
{
*(_QWORD *)(result + 8LL * v4) = a2;
result = 0LL;
}
else
{
*a1 = 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 func11(__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; } *


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[]; }; }


decompiled_code:
__int64 __fastcall cache_get_size(unsigned __int64 a1)
{
__int64 result; // rax
unsigned __int64 v2; // rdx

result = 0x10000LL;
v2 = a1 >> 10;
if ( a1 > 0x23FF )
{
result = 0x20000LL;
if ( v2 > 0x10 )
result = v2 < 0x21 ? 0x40000 : 0x80000;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(unsigned __int64 a1)
{
__int64 result; // rax
unsigned __int64 v2; // rdx

result = 0x10000LL;
v2 = a1 >> 10;
if ( a1 > 0x23FF )
{
result = 0x20000LL;
if ( v2 > 0x10 )
result = v2 < 0x21 ? 0x40000 : 0x80000;
}
return result;
}

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


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

result = *(_QWORD *)(a2 + 8);
if ( a2 == result )
return 0LL;
while ( 1 )
{
v7 = *(_QWORD *)(result + 16);
if ( v7 <= a3 && a3 <= (unsigned int)(*(_DWORD *)(a1 + 72) * *(_DWORD *)(result + 24)) + v7 )
break;
result = *(_QWORD *)(result + 8);
if ( result == a2 )
return 0LL;
}
return result;
}

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

result = *(_QWORD *)(a2 + 8);
if ( a2 == result )
return 0LL;
while ( 1 )
{
v7 = *(_QWORD *)(result + 16);
if ( v7 <= a3 && a3 <= (unsigned int)(*(_DWORD *)(a1 + 72) * *(_DWORD *)(result + 24)) + v7 )
break;
result = *(_QWORD *)(result + 8);
if ( result == a2 )
return 0LL;
}
return result;
}

original_code:
static inline struct slab *kcache_get_slab(struct kcache *cache, struct list_head *l, void *buf)
{
 struct slab *s;
 for (s = (typeof (*s) *)((char *)((l)->next) - __builtin_offsetof (typeof (*s), l)); &((s)->l) != (l); s = (typeof (*s) *)((char *)((s)->l.next) - __builtin_offsetof (typeof (*s), l))) {
  if (buf >= (void *)s->first_element &&
      buf <= (void *)s->first_element + s->objcount * cache->objsz)
   return s;
 }
 return ((void *)0);
}

variable_types: struct kcache { struct list_head slabs_full; struct list_head slabs_notfull; struct slab *first_slab; unsigned long total_alloc; unsigned long total_freed; unsigned long kma_alloc; unsigned long kma_free; unsigned int objsz; unsigned int last_slab; } *

variable_types:
l
struct list_head *

variable_types:
buf
void *

return_type: 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[]; }; } *


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 func15(__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

return_type: char


original_code:
static void kcache_free_slab(struct kcache *cache, struct slab *slab)
{
 if (slab == cache->first_slab)
  return;
 list_del(&slab->l);
 cache->kma_free++;
 kma_free(slab);
}

variable_types: struct kcache { struct list_head { struct list_head *prev; } slabs_full; struct list_head slabs_notfull; struct slab { struct list_head l; void *first_element; unsigned int objcount; unsigned int objused; union { unsigned char usedb[]; unsigned long used[]; }; } *first_slab; unsigned long total_alloc; unsigned long total_freed; unsigned long kma_alloc; unsigned long kma_free; unsigned int objsz; unsigned int last_slab; } *

variable_types:
slab
struct slab *

user_defined_types: struct kcache { struct list_head slabs_full; struct list_head slabs_notfull; struct slab *first_slab; unsigned long total_alloc; unsigned long total_freed; unsigned long kma_alloc; unsigned long kma_free; unsigned int objsz; unsigned int last_slab; }


decompiled_code:
__int64 __fastcall kcache_free(__int64 a1, unsigned __int64 a2)
{
unsigned __int64 v2; // r9
__int64 v3; // r10
__int64 v4; // r11
__int64 v5; // r10
__int64 v6; // r11
int v7; // edx
__int64 result; // rax
_QWORD *v9; // r10

v3 = kcache_get_slab(a1, a1 + 16, a2);
if ( !v3 )
{
v3 = kcache_get_slab(v4, v4, v2);
if ( !v3 )
return 0LL;
}
bitclearptr(v3 + 32, (__int64)(v2 - *(_QWORD *)(v3 + 16)) / *(unsigned int *)(v4 + 72));
v7 = *(_DWORD *)(v5 + 28);
*(_DWORD *)(v5 + 28) = v7 - 1;
++*(_QWORD *)(v6 + 48);
if ( v7 == 1 )
{
kcache_free_slab(v6, v5);
result = 1LL;
}
else
{
result = 1LL;
if ( v7 == *(_DWORD *)(v5 + 24) )
{
list_del_isra_0(*(_QWORD *)v5, *(_QWORD **)(v5 + 8));
list_add(a1 + 16, v9);
result = 1LL;
}
}
return result;
}

original_code:
u8 kcache_free(struct kcache *cache, void *buf)
{
 struct slab *slab;
 slab = kcache_get_slab(cache, &cache->slabs_notfull, buf);
 if (slab == ((void *)0)) {
  slab = kcache_get_slab(cache, &cache->slabs_full, buf);
  if (slab == ((void *)0))
   return 0;
 }
 bitclearptr(slab->usedb, (buf - slab->first_element) / cache->objsz);
 slab->objused--;
 cache->total_freed++;
 if (slab->objused == 0)
  kcache_free_slab(cache, slab);
 else if (slab->objused == slab->objcount - 1) {
  list_del(&slab->l);
  list_add(&cache->slabs_notfull, &slab->l);
 }
 return 1;
}

canonical_original_code:
unsigned char kcache_free(struct kcache *cache, void *buf)
{
 struct slab *slab;
 slab = kcache_get_slab(cache, &cache->slabs_notfull, buf);
 if (slab == ((void *)0)) {
  slab = kcache_get_slab(cache, &cache->slabs_full, buf);
  if (slab == ((void *)0))
   return 0;
 }
 bitclearptr(slab->usedb, (buf - slab->first_element) / cache->objsz);
 slab->objused--;
 cache->total_freed++;
 if (slab->objused == 0)
  kcache_free_slab(cache, slab);
 else if (slab->objused == slab->objcount - 1) {
  list_del(&slab->l);
  list_add(&cache->slabs_notfull, &slab->l);
 }
 return 1;
}

user_defined_types: struct list_head { struct list_head *prev; }


decompiled_code:
__int64 __fastcall get_mm_range(_QWORD *a1, __int16 a2)
{
__int64 v4; // r9
unsigned int *v5; // rdx
unsigned __int64 v6; // rsi
__int16 v7; // cx

v4 = *(unsigned int *)(mbi + 48);
v5 = (unsigned int *)(v4 - 0x800000000000LL);
v6 = v4 + *(unsigned int *)(mbi + 44) - 0x800000000000LL;
if ( v4 - 0x800000000000LL >= v6 )
return 0LL;
v7 = 0;
while ( 1 )
{
while ( v5[5] != 1 )
{
v5 = (unsigned int *)((char *)v5 + *v5 + 4);
if ( (unsigned __int64)v5 >= v6 )
return 0LL;
}
if ( v7 == a2 )
break;
v7 = (unsigned __int8)(v7 + 1);
v5 = (unsigned int *)((char *)v5 + *v5 + 4);
if ( (unsigned __int64)v5 >= v6 )
return 0LL;
}
*a1 = *(_QWORD *)(v5 + 1);
return *(_QWORD *)(v5 + 3);
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1, __int16 a2)
{
__int64 v4; // r9
unsigned int *v5; // rdx
unsigned __int64 v6; // rsi
__int16 v7; // cx

v4 = *(unsigned int *)(mbi + 48);
v5 = (unsigned int *)(v4 - 0x800000000000LL);
v6 = v4 + *(unsigned int *)(mbi + 44) - 0x800000000000LL;
if ( v4 - 0x800000000000LL >= v6 )
return 0LL;
v7 = 0;
while ( 1 )
{
while ( v5[5] != 1 )
{
v5 = (unsigned int *)((char *)v5 + *v5 + 4);
if ( (unsigned __int64)v5 >= v6 )
return 0LL;
}
if ( v7 == a2 )
break;
v7 = (unsigned __int8)(v7 + 1);
v5 = (unsigned int *)((char *)v5 + *v5 + 4);
if ( (unsigned __int64)v5 >= v6 )
return 0LL;
}
*a1 = *(_QWORD *)(v5 + 1);
return *(_QWORD *)(v5 + 3);
}

original_code:
u64 get_mm_range(void **addr, u16 n)
{
 mboot_mmap_info *mmap;
 u8 i = 0;
 mmap = (mboot_mmap_info *)((void *)(((u64)mbi->mmap_addr) + 0xffff800000000000));
 while ((void *)mmap < ((void *)(((u64)mbi->mmap_addr) + 0xffff800000000000)) + mbi->mmap_length) {
  if (mmap->type == 1 && i++ == n) {
   *addr = (void *)mmap->addr;
   return mmap->len;
  }
  mmap = (mboot_mmap_info *)((u64)mmap + mmap->size
   + sizeof(mmap->size));
 }
 return 0;
}

canonical_original_code:
unsigned long get_mm_range(void **addr, unsigned short n)
{
 struct mboot_mmap_info *mmap;
 unsigned char i = 0;
 mmap = (struct mboot_mmap_info *)((void *)(((unsigned long)mbi->mmap_addr) + 0xffff800000000000));
 while ((void *)mmap < ((void *)(((unsigned long)mbi->mmap_addr) + 0xffff800000000000)) + mbi->mmap_length) {
  if (mmap->type == 1 && i++ == n) {
   *addr = (void *)mmap->addr;
   return mmap->len;
  }
  mmap = (struct mboot_mmap_info *)((unsigned long)mmap + mmap->size
   + sizeof(mmap->size));
 }
 return 0;
}

variable_types:
addr
void **

variable_types:
n
unsigned short

variable_types: struct mboot_mmap_info { unsigned int size; unsigned long addr; unsigned long len; unsigned int type; } *

variable_types:
i
unsigned char


oom

decompiled_code:
__int64 __fastcall oom(const char *a1)
{
kprintf("Out of memory: %s\n", a1);
return kpanic(":(");
}

canonical_decompiled_code:
__int64 __fastcall func1(const char *a1)
{
kprintf("Out of memory: %s\n", a1);
return kpanic(":(");
}

original_code:
void oom(const char *str)
{
 kprintf("Out of memory: %s\n", str);
 kpanic(":(");
}

canonical_original_code:
void oom(char *str)
{
 kprintf("Out of memory: %s\n", str);
 kpanic(":(");
}

variable_types:
str
char *


decompiled_code:
__int64 init_memory()
{
__int64 v0; // rax
__int64 v1; // rdx
__int64 v2; // rsi

v0 = *(&mbi32 - 0x200000000000LL) - 0x800000000000LL;
v1 = *(unsigned int *)(v0 + 8);
v2 = *(unsigned int *)(v0 + 4);
mbi = v0;
kprintf("%i lower and %i upper KB detected.\n", v2, v1);
if ( (*(_BYTE *)mbi & 1) == 0 )
kpanic("Can't detect memory!\n");
build_page_frames();
return kma_init();
}

canonical_decompiled_code:
__int64 func2()
{
__int64 v0; // rax
__int64 v1; // rdx
__int64 v2; // rsi

v0 = *(&mbi32 - 0x200000000000LL) - 0x800000000000LL;
v1 = *(unsigned int *)(v0 + 8);
v2 = *(unsigned int *)(v0 + 4);
mbi = v0;
kprintf("%i lower and %i upper KB detected.\n", v2, v1);
if ( (*(_BYTE *)mbi & 1) == 0 )
kpanic("Can't detect memory!\n");
build_page_frames();
return kma_init();
}

original_code:
void init_memory(void)
{
 mbi = (mboot_info *)((void *)(((u64)*(u32 *)((void *)(((u64)&mbi32) + 0xffff800000000000))) + 0xffff800000000000));
 kprintf("%i lower and %i upper KB detected.\n",
  mbi->mem_lower, mbi->mem_upper);
 if ((mbi->flags & (1 << 0)) == 0x0)
  kpanic("Can't detect memory!\n");
 build_page_frames();
 kma_init();
}

canonical_original_code:
void init_memory()
{
 mbi = (struct mboot_info *)((void *)(((u64)*(unsigned int *)((void *)(((u64)&mbi32) + 0xffff800000000000))) + 0xffff800000000000));
 kprintf("%i lower and %i upper KB detected.\n",
  mbi->mem_lower, mbi->mem_upper);
 if ((mbi->flags & (1 << 0)) == 0x0)
  kpanic("Can't detect memory!\n");
 build_page_frames();
 kma_init();
}

user_defined_types: struct mboot_info { unsigned int flags; unsigned int mem_lower; unsigned int mem_upper; unsigned int boot_device; unsigned int cmdline; unsigned int mods_count; unsigned int mods_addr; unsigned int a; unsigned int mmap_length; unsigned int mmap_addr; unsigned int drives_length; unsigned int drives_addr; unsigned int config_table; unsigned int boot_loader_name; unsigned int apm_table; }


iget

original_code:
struct inode *iget(struct super *sb, size_t inum)
{
 struct inode *ino;
 for (ino = (typeof (*ino) *)((char *)((&sb->icache)->next) - __builtin_offsetof (typeof (*ino), l)); &((ino)->l) != (&sb->icache); ino = (typeof (*ino) *)((char *)((ino)->l.next) - __builtin_offsetof (typeof (*ino), l))) {
  if (ino->num == inum) {
   ino->count++;
   return ino;
  }
 }
 ino = xkmallocf(sizeof(struct inode), __func__);
 ino->num = inum;
 ino->count = 0;
 list_add(&sb->icache, &ino->l);
 return ino;
}

canonical_original_code:
struct inode *iget(struct super *sb, unsigned long inum)
{
 struct inode *ino;
 for (ino = (typeof (*ino) *)((char *)((&sb->icache)->next) - __builtin_offsetof (typeof (*ino), l)); &((ino)->l) != (&sb->icache); ino = (typeof (*ino) *)((char *)((ino)->l.next) - __builtin_offsetof (typeof (*ino), l))) {
  if (ino->num == inum) {
   ino->count++;
   return ino;
  }
 }
 ino = xkmallocf(sizeof(struct inode), __func__);
 ino->num = inum;
 ino->count = 0;
 list_add(&sb->icache, &ino->l);
 return ino;
}

variable_types:
sb
struct super *

variable_types:
inum
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; } *


iput

decompiled_code:
__int64 __fastcall iput(__int64 a1)
{
__int64 v1; // r8

list_del_isra_0(*(_QWORD *)(a1 + 80), *(_QWORD **)(a1 + 88));
return kfree(v1);
}

original_code:
void iput(struct inode *inode)
{
 list_del(&inode->l);
 kfree(inode);
}

user_defined_types: struct dev_t { unsigned int major; unsigned int minor; }

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


bmap

original_code:
size_t bmap(struct inode *inode, off_t offset)
{
 return inode->ops->bmap(inode, offset);
}

canonical_original_code:
unsigned long bmap(struct inode *inode, long offset)
{
 return inode->ops->bmap(inode, offset);
}

variable_types: offset long


decompiled_code:
__int64 __fastcall ext2_block_read(_QWORD *a1)
{
__int64 v1; // rax
__int64 result; // rax

v1 = bmap();
if ( v1 )
result = bread(*a1, v1);
else
result = 0LL;
return result;
}

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

v1 = bmap();
if ( v1 )
result = bread(*a1, v1);
else
result = 0LL;
return result;
}

original_code:
static struct bhead *ext2_block_read(struct inode *inode, off_t offset)
{
 size_t blk;
 blk = bmap(inode, offset);
 if (blk == 0)
  return ((void *)0);
 return bread(inode->sb, blk);
}

canonical_original_code:
static struct bhead *ext2_block_read(struct inode *inode, long offset)
{
 unsigned long blk;
 blk = bmap(inode, offset);
 if (blk == 0)
  return ((void *)0);
 return bread(inode->sb, blk);
}

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 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 *(*block_read)(struct inode *, long); } *ops; void *priv; } *

variable_types:
blk
unsigned long

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


decompiled_code:
__int64 __fastcall ext2_bmap(__int64 *a1, unsigned __int64 a2)
{
__int64 v2; // r8
__int64 v3; // rdi
unsigned __int64 v4; // rcx
unsigned __int64 v5; // rax
unsigned __int64 v6; // rdx
unsigned __int64 v7; // rbx
unsigned __int64 v8; // rcx
__int64 v9; // rsi
__int64 result; // rax

v2 = a1[14];
v3 = *a1;
v4 = *(_QWORD *)(v3 + 8);
v5 = a2 / v4;
v6 = a2 % v4;
v7 = a2 / v4;
if ( a2 / v4 <= 0xB )
return *(unsigned int *)(v2 + 4 * v5);
v8 = (v4 >> 2) + 12;
if ( v8 <= v5 )
{
kpanic("No double nor triply indirect block supported yet\n", a2, v6, v8, v2);
result = 0LL;
}
else
{
v9 = *(unsigned int *)(v2 + 48);
result = 0LL;
if ( (_DWORD)v9 )
result = *(unsigned int *)(*(_QWORD *)(bread(v3, v9) + 24) + 4 * v7 - 48);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 *a1, unsigned __int64 a2)
{
__int64 v2; // r8
__int64 v3; // rdi
unsigned __int64 v4; // rcx
unsigned __int64 v5; // rax
unsigned __int64 v6; // rdx
unsigned __int64 v7; // rbx
unsigned __int64 v8; // rcx
__int64 v9; // rsi
__int64 result; // rax

v2 = a1[14];
v3 = *a1;
v4 = *(_QWORD *)(v3 + 8);
v5 = a2 / v4;
v6 = a2 % v4;
v7 = a2 / v4;
if ( a2 / v4 <= 0xB )
return *(unsigned int *)(v2 + 4 * v5);
v8 = (v4 >> 2) + 12;
if ( v8 <= v5 )
{
kpanic("No double nor triply indirect block supported yet\n", a2, v6, v8, v2);
result = 0LL;
}
else
{
v9 = *(unsigned int *)(v2 + 48);
result = 0LL;
if ( (_DWORD)v9 )
result = *(unsigned int *)(*(_QWORD *)(bread(v3, v9) + 24) + 4 * v7 - 48);
}
return result;
}

original_code:
static size_t ext2_bmap(struct inode *inode, off_t offset)
{
 struct ext2_inode_priv *ipriv = inode->priv;
 size_t nblock;
 struct bhead *block;
 nblock = offset / inode->sb->blocksize;
 if (nblock < 12)
  return ipriv->blocks[nblock];
 else if (nblock < 12 + (inode->sb->blocksize >> 2)) {
  if (ipriv->blocks[12] == 0)
   return 0;
  block = bread(inode->sb, ipriv->blocks[12]);
  return *(u32 *)(block->data + 4 * (nblock - 12));
 }
 else
  kpanic("No double nor triply indirect block supported yet\n");
 return 0;
}

canonical_original_code:
static unsigned long ext2_bmap(struct inode *inode, long offset)
{
 struct ext2_inode_priv *ipriv = inode->priv;
 unsigned long nblock;
 struct bhead *block;
 nblock = offset / inode->sb->blocksize;
 if (nblock < 12)
  return ipriv->blocks[nblock];
 else if (nblock < 12 + (inode->sb->blocksize >> 2)) {
  if (ipriv->blocks[12] == 0)
   return 0;
  block = bread(inode->sb, ipriv->blocks[12]);
  return *(unsigned int *)(block->data + 4 * (nblock - 12));
 }
 else
  kpanic("No double nor triply indirect block supported yet\n");
 return 0;
}

variable_types: struct ext2_inode_priv { unsigned int blocks[15]; } *

variable_types:
nblock
unsigned long

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


original_code:
struct inode *ext2_inode_read(struct super *sb, u64 inum)
{
 struct ext2_super_ext2 *sbpriv = sb->priv;
 size_t bgroup;
 struct bhead *buf;
 struct ext2_bg_desc *e2desc;
 size_t iindex;
 struct ext2_inode *e2ino;
 struct inode *inode;
 struct ext2_inode_priv *ipriv;
 u8 i;
 if (inum > sbpriv->inodes_max)
  return ((void *)0);
 bgroup = (inum - 1) / sbpriv->inodes_per_group;
 buf = ext2_bread (sb, sbpriv->bgroup_base
  + bgroup / (sb->blocksize / sizeof(struct ext2_bg_desc)));
 e2desc = (struct ext2_bg_desc *)buf->data;
 e2desc += bgroup;
 iindex = (inum - 1) % sbpriv->inodes_per_group;
 buf = ext2_bread(sb, e2desc->bg_inode_table + (iindex * sbpriv->inode_size) / sb->blocksize);
 e2ino = (struct ext2_inode *)buf->data;
 e2ino += (iindex % (sb->blocksize / sbpriv->inode_size));
 inode = iget(sb, inum);
 if (inode->count > 0)
  return inode;
 inode->count++;
 inode->sb = sb;
 inode->mode = e2ino->i_mode;
 inode->nlinks = e2ino->i_links_count;
 inode->uid = e2ino->i_uid;
 inode->gid = e2ino->i_gid;
 inode->filesize = e2ino->i_size;
 inode->nblocks = e2ino->i_blocks;
 inode->atime = e2ino->i_atime;
 inode->mtime = e2ino->i_mtime;
 inode->ctime = e2ino->i_ctime;
 inode->covered = ((void *)0);
 inode->flags = e2ino->i_flags;
 inode->priv = xkmallocf(sizeof(struct ext2_inode_priv), __func__);
 ipriv = (struct ext2_inode_priv *)inode->priv;
 for (i = 0; i < 15; i++)
  ipriv->blocks[i] = e2ino->i_block[i];
 inode->ops = &ext2_inode_ops;
 return inode;
}

canonical_original_code:
struct inode *ext2_inode_read(struct super *sb, unsigned long inum)
{
 struct ext2_super_ext2 *sbpriv = sb->priv;
 unsigned long bgroup;
 struct bhead *buf;
 struct ext2_bg_desc *e2desc;
 unsigned long iindex;
 struct ext2_inode *e2ino;
 struct inode *inode;
 struct ext2_inode_priv *ipriv;
 unsigned char i;
 if (inum > sbpriv->inodes_max)
  return ((void *)0);
 bgroup = (inum - 1) / sbpriv->inodes_per_group;
 buf = ext2_bread (sb, sbpriv->bgroup_base
  + bgroup / (sb->blocksize / sizeof(struct ext2_bg_desc)));
 e2desc = (struct ext2_bg_desc *)buf->data;
 e2desc += bgroup;
 iindex = (inum - 1) % sbpriv->inodes_per_group;
 buf = ext2_bread(sb, e2desc->bg_inode_table + (iindex * sbpriv->inode_size) / sb->blocksize);
 e2ino = (struct ext2_inode *)buf->data;
 e2ino += (iindex % (sb->blocksize / sbpriv->inode_size));
 inode = iget(sb, inum);
 if (inode->count > 0)
  return inode;
 inode->count++;
 inode->sb = sb;
 inode->mode = e2ino->i_mode;
 inode->nlinks = e2ino->i_links_count;
 inode->uid = e2ino->i_uid;
 inode->gid = e2ino->i_gid;
 inode->filesize = e2ino->i_size;
 inode->nblocks = e2ino->i_blocks;
 inode->atime = e2ino->i_atime;
 inode->mtime = e2ino->i_mtime;
 inode->ctime = e2ino->i_ctime;
 inode->covered = ((void *)0);
 inode->flags = e2ino->i_flags;
 inode->priv = xkmallocf(sizeof(struct ext2_inode_priv), __func__);
 ipriv = (struct ext2_inode_priv *)inode->priv;
 for (i = 0; i < 15; i++)
  ipriv->blocks[i] = e2ino->i_block[i];
 inode->ops = &ext2_inode_ops;
 return inode;
}

variable_types: struct ext2_super_ext2 { unsigned int nblocks; unsigned long bgroup_base; unsigned short inode_size; unsigned short bg_desc_per_block; unsigned int inodes_per_group; unsigned int inodes_max; } *

variable_types:
bgroup
unsigned long

variable_types: struct ext2_bg_desc { unsigned int bg_block_bitmap; unsigned int bg_inode_bitmap; unsigned int bg_inode_table; unsigned short bg_free_blocks_count; unsigned short bg_free_inodes_count; unsigned short bg_used_dirs_count; char pad[14]; } *

variable_types:
iindex
unsigned long

variable_types: struct ext2_inode { unsigned short i_mode; unsigned short i_uid; unsigned int i_size; unsigned int i_atime; unsigned int i_ctime; unsigned int i_mtime; unsigned int i_dtime; unsigned short i_gid; unsigned short i_links_count; unsigned int i_blocks; unsigned int i_flags; unsigned int i_osdl; unsigned int i_block[15]; unsigned int i_generation; unsigned int i_file_acl; unsigned int i_dir_acl; unsigned int i_faddr; char i_osd2[12]; } *

return_type: 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; } *

user_defined_types: struct ext2_inode_priv { unsigned int blocks[15]; }


original_code:
static struct inode *ext2_lookup(struct inode *inode, const char *name, u8 len)
{
 size_t block = 0;
 size_t offset = 0;
 struct bhead *buffer;
 struct ext2_dirent *dirent;
 if (!(((inode->mode) & 0170000) == 0040000))
  return ((void *)0);
 while (offset <= inode->filesize) {
  buffer = ext2_block_read(inode, block);
  if (buffer == ((void *)0))
   break;
  dirent = buffer->data;
  while ((void *)dirent < buffer->data + inode->sb->blocksize) {
   if (len == dirent->name_len &&
    strncmp(name, dirent->name, len) == 0)
    return ext2_inode_read(inode->sb, dirent->inode);
   dirent = (void *)dirent + dirent->rec_len;
  }
  offset += inode->sb->blocksize;
  block++;
 }
 return ((void *)0);
}

canonical_original_code:
static struct inode *ext2_lookup(struct inode *inode, char *name, unsigned char len)
{
 unsigned long block = 0;
 unsigned long offset = 0;
 struct bhead *buffer;
 struct ext2_dirent *dirent;
 if (!(((inode->mode) & 0170000) == 0040000))
  return ((void *)0);
 while (offset <= inode->filesize) {
  buffer = ext2_block_read(inode, block);
  if (buffer == ((void *)0))
   break;
  dirent = buffer->data;
  while ((void *)dirent < buffer->data + inode->sb->blocksize) {
   if (len == dirent->name_len &&
    strncmp(name, dirent->name, len) == 0)
    return ext2_inode_read(inode->sb, dirent->inode);
   dirent = (void *)dirent + dirent->rec_len;
  }
  offset += inode->sb->blocksize;
  block++;
 }
 return ((void *)0);
}

variable_types:
inode
struct inode *

variable_types:
name
char *

variable_types:
len
unsigned char

variable_types:
block
unsigned long

variable_types:
offset
unsigned long

variable_types: struct ext2_dirent { unsigned int inode; unsigned short rec_len; unsigned char name_len; unsigned char file_type; char name[256]; } *


getblk

original_code:
struct bhead *getblk(struct super *sb, size_t block)
{
 struct bhead *bh;
 for (bh = (typeof (*bh) *)((char *)((&sb->bcache)->next) - __builtin_offsetof (typeof (*bh), l)); &((bh)->l) != (&sb->bcache); bh = (typeof (*bh) *)((char *)((bh)->l.next) - __builtin_offsetof (typeof (*bh), l))) {
  if (bh->bnum == block) {
   bh->count++;
   return bh;
  }
 }
 bh = xkmallocf(sizeof(struct bhead), __func__);
 bh->bnum = block;
 bh->data = xkmallocf(sb->blocksize, __func__);
 if (bh->data == ((void *)0)) {
  kfree(bh);
  return ((void *)0);
 }
 bh->count = 0;
 list_add(&sb->bcache, &bh->l);
 return bh;
}

canonical_original_code:
struct bhead *getblk(struct super *sb, unsigned long block)
{
 struct bhead *bh;
 for (bh = (typeof (*bh) *)((char *)((&sb->bcache)->next) - __builtin_offsetof (typeof (*bh), l)); &((bh)->l) != (&sb->bcache); bh = (typeof (*bh) *)((char *)((bh)->l.next) - __builtin_offsetof (typeof (*bh), l))) {
  if (bh->bnum == block) {
   bh->count++;
   return bh;
  }
 }
 bh = xkmallocf(sizeof(struct bhead), __func__);
 bh->bnum = block;
 bh->data = xkmallocf(sb->blocksize, __func__);
 if (bh->data == ((void *)0)) {
  kfree(bh);
  return ((void *)0);
 }
 bh->count = 0;
 list_add(&sb->bcache, &bh->l);
 return bh;
}

variable_types: struct super { struct dev_t { unsigned int major; unsigned int minor; } dev; unsigned long blocksize; unsigned long blocksizephys; struct list_head 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 *(*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; } *


bread

original_code:
struct bhead *bread(struct super *sb, size_t block)
{
 struct bhead *bh;
 bh = getblk(sb, block);
 if (bh == ((void *)0))
  return ((void *)0);
 if (bh->count > 0)
  return bh;
 bh->count++;
 if (breadu(&sb->dev, block * sb->blocksize / sb->blocksizephys,
  bh->data, sb->blocksize) == 0)
  return ((void *)0);
 return bh;
}

canonical_original_code:
struct bhead *bread(struct super *sb, unsigned long block)
{
 struct bhead *bh;
 bh = getblk(sb, block);
 if (bh == ((void *)0))
  return ((void *)0);
 if (bh->count > 0)
  return bh;
 bh->count++;
 if (breadu(&sb->dev, block * sb->blocksize / sb->blocksizephys,
  bh->data, sb->blocksize) == 0)
  return ((void *)0);
 return bh;
}


decompiled_code:
__int64 init_ext2()
{
return fs_register(ext2);
}

canonical_decompiled_code:
__int64 func1()
{
return fs_register(ext2);
}

original_code:
void init_ext2(void)
{
 fs_register(&ext2);
}

canonical_original_code:
void init_ext2()
{
 fs_register(&ext2);
}


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


decompiled_code:
__int64 lapic_eoi()
{
return lapic_write_constprop_0();
}

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

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:
// positive sp value has been detected, the output may be wrong!
void do_keyboard()
{
unsigned __int8 v0; // al
char v1; // al
__int64 v2; // rdx

inb_constprop_0();
if ( v0 != 0xAA )
{
if ( v0 <= 0xAAu )
{
if ( v0 == 42 || v0 == 54 )
{
keyb |= 1u;
}
else if ( (v0 & 0x80u) == 0 )
{
if ( (keyb & 1) != 0 )
v1 = kb_en_caps[v0];
else
v1 = kb_en[v0];
v2 = kbdbptr;
if ( v1 == 8 )
{
--kbdbptr;
((void (__fastcall *)(void *, __int64))kprintf)(&unk_4F0, 8LL);
}
else
{
if ( kbdbptr - (__int64)&kbdb <= 255 )
{
*(_BYTE *)kbdbptr = v1;
kbdbptr = v2 + 1;
}
((void (__fastcall *)(void *, _QWORD))kprintf)(&unk_4F0, (unsigned int)v1);
}
}
LABEL_6:
lapic_eoi();
__asm { iretq }
JUMPOUT(0xC4LL);
}
if ( v0 != 0xB6 )
goto LABEL_6;
}
keyb &= 0xFEu;
goto LABEL_6;
}

canonical_decompiled_code:
void func4()
{
unsigned __int8 v0; // al
char v1; // al
__int64 v2; // rdx

func3();
if ( v0 != 0xAA )
{
if ( v0 <= 0xAAu )
{
if ( v0 == 42 || v0 == 54 )
{
keyb |= 1u;
}
else if ( (v0 & 0x80u) == 0 )
{
if ( (keyb & 1) != 0 )
v1 = kb_en_caps[v0];
else
v1 = kb_en[v0];
v2 = kbdbptr;
if ( v1 == 8 )
{
--kbdbptr;
((void (__fastcall *)(void *, __int64))kprintf)(&unk_4F0, 8LL);
}
else
{
if ( kbdbptr - (__int64)&kbdb <= 255 )
{
*(_BYTE *)kbdbptr = v1;
kbdbptr = v2 + 1;
}
((void (__fastcall *)(void *, _QWORD))kprintf)(&unk_4F0, (unsigned int)v1);
}
}
LABEL_6:
func2();
__asm { iretq }
JUMPOUT(0xC4LL);
}
if ( v0 != 0xB6 )
goto LABEL_6;
}
keyb &= 0xFEu;
goto LABEL_6;
}

original_code:
__attribute__((aligned(16))) static void do_keyboard(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" );
 u8 scancode = inb(0x60);
 switch (scancode) {
 case 0x2a:
 case 0x36:
  keyb.shift = 1;
  break;
 case 0xaa:
 case 0xb6:
  keyb.shift = 0;
  break;
 default: {
  char c;
  if (scancode & 0x80)
   break;
  if (keyb.shift)
   c = kb_en_caps[scancode];
  else
   c = kb_en[scancode];
  switch (c) {
  case '\b':
   kbdbptr--;
   kprintf("%c", c);
   break;
  default:
   if (kbdbptr - kbdb < 256)
    *kbdbptr++ = c;
   kprintf("%c", c);
  }
 }
 }
 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" );
}

canonical_original_code:
static void do_keyboard(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" );
 unsigned char scancode = inb(0x60);
 switch (scancode) {
 case 0x2a:
 case 0x36:
  keyb.shift = 1;
  break;
 case 0xaa:
 case 0xb6:
  keyb.shift = 0;
  break;
 default: {
  char c;
  if (scancode & 0x80)
   break;
  if (keyb.shift)
   c = kb_en_caps[scancode];
  else
   c = kb_en[scancode];
  switch (c) {
  case '\b':
   kbdbptr--;
   kprintf("%c", c);
   break;
  default:
   if (kbdbptr - kbdb < 256)
    *kbdbptr++ = c;
   kprintf("%c", c);
  }
 }
 }
 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:
scancode
unsigned char

variable_types: c char


decompiled_code:
__int64 init_keyboard()
{
kbdbptr = (__int64)&kbdb;
return intr_install_handler(1LL, do_keyboard);
}

canonical_decompiled_code:
__int64 func5()
{
kbdbptr = (__int64)&kbdb;
return intr_install_handler(1LL, do_keyboard);
}

original_code:
void init_keyboard(void)
{
 kbdbptr = kbdb;
 intr_install_handler(1, do_keyboard);
 return;
}

canonical_original_code:
void init_keyboard()
{
 kbdbptr = kbdb;
 intr_install_handler(1, do_keyboard);
 return;
}


div

decompiled_code:
__int64 __fastcall div(unsigned int a1, unsigned int a2)
{
return a1 / a2;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, unsigned int a2)
{
return a1 / a2;
}

canonical_original_code:
inline static unsigned long div(unsigned long n, unsigned int d)
{
 unsigned int q; unsigned int r;
 asm (
  "divl %[n]\n\t"
  : "=d" (r), "=a" (q)
  : "0" (n >> 32), "1" (n), [n] "rm" (d));
 return q;
}

variable_types:
n
unsigned long

variable_types:
d
unsigned int

variable_types:
q
unsigned int

variable_types:
r
unsigned int


yield

decompiled_code:
void yield()
{
;
}

canonical_decompiled_code:
void func2()
{
;
}

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

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


ticks

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

canonical_decompiled_code:
unsigned __int64 func3()
{
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


decompiled_code:
__int64 __fastcall init_lapic_timer(__int64 a1, unsigned int a2)
{
void (__fastcall *v2)(unsigned int, int); // r9
void (__fastcall *v3)(unsigned int, int); // r9
void (*v4)(void); // r10
__int64 (__fastcall *v5)(__int64, _QWORD); // r10
int v6; // eax
int v7; // ecx
__int64 v8; // r8
__int64 v9; // rax
void (__fastcall *v10)(__int64, __int64); // r9
__int64 (__fastcall *v11)(__int64, __int64); // rcx
__int64 v12; // r8
__int64 v13; // rax

intr_install_handler(66LL, a1);
lapic_write(0x3E0u, 11);
v2(0x320u, 131138);
v3(0x380u, -1);
v4();
msleep_constprop_0();
v6 = v5(896LL, a2);
v8 = (unsigned int)(v7 - v6);
sys[3] = v8;
v9 = div(1000 * (int)v8, a2);
v10(896LL, v9);
v13 = v11(v12, 1000LL);
return kprintf("External clock: %ldMhz\n", v13);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, unsigned int a2)
{
void (__fastcall *v2)(unsigned int, int); // r9
void (__fastcall *v3)(unsigned int, int); // r9
void (*v4)(void); // r10
__int64 (__fastcall *v5)(__int64, _QWORD); // r10
int v6; // eax
int v7; // ecx
__int64 v8; // r8
__int64 v9; // rax
void (__fastcall *v10)(__int64, __int64); // r9
__int64 (__fastcall *v11)(__int64, __int64); // rcx
__int64 v12; // r8
__int64 v13; // rax

intr_install_handler(66LL, a1);
func0(0x3E0u, 11);
v2(0x320u, 131138);
v3(0x380u, -1);
v4();
func5();
v6 = v5(896LL, a2);
v8 = (unsigned int)(v7 - v6);
sys[3] = v8;
v9 = func1(1000 * (int)v8, a2);
v10(896LL, v9);
v13 = v11(v12, 1000LL);
return kprintf("External clock: %ldMhz\n", v13);
}

original_code:
void init_lapic_timer(void *intr_handler, u32 hz)
{
 u32 lapictic;
 intr_install_handler(66, intr_handler);
 lapic_write(0x3e0, 0xb);
 lapic_write(0x320, (1 << 17) | 66);
 lapic_write(0x380, (u32)-1);
 lapictic = lapic_get_current_count();
 msleep(1);
 lapictic -= lapic_get_current_count();
 sys.busspeed = lapictic;
 lapic_write(0x380, div(lapictic * 1000, hz));
 kprintf("External clock: %ldMhz\n", div(lapictic, 1000));
 return;
}

canonical_original_code:
void init_lapic_timer(void *intr_handler, unsigned int hz)
{
 unsigned int lapictic;
 intr_install_handler(66, intr_handler);
 lapic_write(0x3e0, 0xb);
 lapic_write(0x320, (1 << 17) | 66);
 lapic_write(0x380, (u32)-1);
 lapictic = lapic_get_current_count();
 msleep(1);
 lapictic -= lapic_get_current_count();
 sys.busspeed = lapictic;
 lapic_write(0x380, div(lapictic * 1000, hz));
 kprintf("External clock: %ldMhz\n", div(lapictic, 1000));
 return;
}

variable_types:
hz
unsigned int

variable_types:
lapictic
unsigned int


repo: xant/wurfld

decompiled_code:
__int64 __fastcall open_socket(char *src, int a2)
{
int v2; // eax
unsigned int v3; // er12
int optval; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF

optval = 1;
*_errno_location() = 22;
if ( !src )
return (unsigned int)-1;
if ( !*src )
return (unsigned int)-1;
if ( !a2 )
return (unsigned int)-1;
v2 = socket(2, 1, 6);
v3 = v2;
if ( v2 == -1 )
return (unsigned int)-1;
setsockopt(v2, 1, 2, &optval, 4u);
setsockopt(v3, 6, 1, &optval, 4u);
if ( (unsigned int)string2sockaddr(src, a2, (__int64)&addr) == -1 || bind(v3, &addr, 0x10u) == -1 )
{
shutdown(v3, 2);
close(v3);
return (unsigned int)-1;
}
listen(v3, -1);
fcntl(v3, 2, 1LL);
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *src, int a2)
{
int v2; // eax
unsigned int v3; // er12
int optval; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF

optval = 1;
*_errno_location() = 22;
if ( !src )
return (unsigned int)-1;
if ( !*src )
return (unsigned int)-1;
if ( !a2 )
return (unsigned int)-1;
v2 = socket(2, 1, 6);
v3 = v2;
if ( v2 == -1 )
return (unsigned int)-1;
setsockopt(v2, 1, 2, &optval, 4u);
setsockopt(v3, 6, 1, &optval, 4u);
if ( (unsigned int)func2(src, a2, (__int64)&addr) == -1 || bind(v3, &addr, 0x10u) == -1 )
{
shutdown(v3, 2);
close(v3);
return (unsigned int)-1;
}
listen(v3, -1);
fcntl(v3, 2, 1LL);
return v3;
}

original_code:
int
open_socket(const char *host, int port)
{
    int val = 1;
    struct sockaddr_in sockaddr;
    int sock;
    (*__errno_location ()) = 22;
    if (host == ((void *)0) || strlen(host) == 0 || port == 0)
 return -1;
    sock = socket(2, SOCK_STREAM, IPPROTO_TCP);
    if (sock == -1)
 return -1;
    setsockopt(sock, 1, 2, &val, sizeof(val));
    setsockopt(sock, IPPROTO_TCP, 1, &val, sizeof(val));
    if (string2sockaddr(host, port, &sockaddr) == -1
 || bind(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1) {
 shutdown(sock, SHUT_RDWR);
 close(sock);
 return -1;
    }
    listen(sock, -1);
    fcntl(sock, 2, 1);
    return sock;
}

canonical_original_code:
int open_socket(char *host, int port)
{
    int val = 1;
    struct sockaddr_in sockaddr;
    int sock;
    (*__errno_location ()) = 22;
    if (host == ((void *)0) || strlen(host) == 0 || port == 0)
 return -1;
    sock = socket(2, SOCK_STREAM, IPPROTO_TCP);
    if (sock == -1)
 return -1;
    setsockopt(sock, 1, 2, &val, sizeof(val));
    setsockopt(sock, IPPROTO_TCP, 1, &val, sizeof(val));
    if (string2sockaddr(host, port, &sockaddr) == -1
 || bind(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1) {
 shutdown(sock, SHUT_RDWR);
 close(sock);
 return -1;
    }
    listen(sock, -1);
    fcntl(sock, 2, 1);
    return sock;
}

variable_types:
host
char *

variable_types: port int

variable_types: val int

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

variable_types: sock int

return_type: int

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


decompiled_code:
__int64 __fastcall write_socket(int fd, __int64 a2, int a3)
{
int v3; // er13
int v4; // er12
int v6; // eax
int *v7; // rax
char *v8; // rax

v3 = 0;
v4 = 0;
do
{
v3 += v4;
a3 -= v4;
v6 = write(fd, (const void *)(a2 + v3), a3);
v4 = v6;
if ( v6 == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
log_message(5LL, 0LL, "write on fd %d failed: %s", (unsigned int)fd, v8);
return 0xFFFFFFFFLL;
}
}
while ( v6 && a3 != v6 );
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(int fd, __int64 a2, int a3)
{
int v3; // er13
int v4; // er12
int v6; // eax
int *v7; // rax
char *v8; // rax

v3 = 0;
v4 = 0;
do
{
v3 += v4;
a3 -= v4;
v6 = write(fd, (const void *)(a2 + v3), a3);
v4 = v6;
if ( v6 == -1 )
{
v7 = _errno_location();
v8 = strerror(*v7);
log_message(5LL, 0LL, "write on fd %d failed: %s", (unsigned int)fd, v8);
return 0xFFFFFFFFLL;
}
}
while ( v6 && a3 != v6 );
return 0LL;
}

original_code:
int write_socket(int fd, char *buf, int len) {
    int wb = 0;
    int ofx = 0;
    do {
        len -= wb;
        ofx += wb;
        wb = write(fd, buf+ofx, len);
        if (wb == -1) {
            if ((*__errno_location ()) != 4 || (*__errno_location ()) != 11) {
                do { log_message(5, 0, "write on fd %d failed: %s", fd, strerror((*__errno_location ()))); } while (0);
                return -1;
            }
            wb = 0;
        } else if (wb == 0) {
            break;
        }
    } while (wb != len);
    return 0;
}

variable_types: fd int

variable_types:
buf
char *

variable_types: len int

variable_types: wb int

variable_types: ofx int


decompiled_code:
__int64 __fastcall read_socket(unsigned int a1, void *a2, int *a3)
{
int v4; // eax
unsigned int v5; // er13
int *v7; // rax
char *v8; // rax

v4 = read(a1, a2, *a3);
if ( v4 == -1 )
{
v5 = -1;
v7 = _errno_location();
v8 = strerror(*v7);
log_message(5LL, 0LL, "Read on fd %d failed: %s", a1, v8);
}
else
{
*a3 = v4;
v5 = 0;
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1, void *a2, int *a3)
{
int v4; // eax
unsigned int v5; // er13
int *v7; // rax
char *v8; // rax

v4 = read(a1, a2, *a3);
if ( v4 == -1 )
{
v5 = -1;
v7 = _errno_location();
v8 = strerror(*v7);
log_message(5LL, 0LL, "Read on fd %d failed: %s", a1, v8);
}
else
{
*a3 = v4;
v5 = 0;
}
return v5;
}

original_code:
int read_socket(int fd, char *buf, int *len) {
    int rb = 0;
    int to_read = *len;
    rb = read(fd, buf, to_read);
    if (rb == -1 && ((*__errno_location ()) != 4 || (*__errno_location ()) != 11)) {
        do { log_message(5, 0, "Read on fd %d failed: %s", fd, strerror((*__errno_location ()))); } while (0);
        return -1;
    }
    *len = rb;
    return 0;
}

variable_types:
len
int *

variable_types: rb int


decompiled_code:
__int64 __fastcall open_connection(char *src, unsigned int a2, unsigned int a3)
{
int v4; // eax
unsigned int v5; // er12
int optval; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF

optval = 1;
*_errno_location() = 22;
if ( !src )
return (unsigned int)-1;
if ( !*src )
return (unsigned int)-1;
if ( !a2 )
return (unsigned int)-1;
v4 = socket(2, 1, 6);
v5 = v4;
if ( v4 == -1 )
return (unsigned int)-1;
setsockopt(v4, 6, 1, &optval, 4u);
if ( a3 )
{
*(_QWORD *)&addr.sa_family = a3;
*(_QWORD *)&addr.sa_data[6] = 0LL;
if ( setsockopt(v5, 1, 21, &addr, 0x10u) == -1 || setsockopt(v5, 1, 20, &addr, 0x10u) == -1 )
log_message(6LL, 0LL, "%s:%d: Failed to set timeout to %d\n", src, a2, a3);
}
if ( (unsigned int)string2sockaddr(src, a2, (__int64)&addr) == -1 || connect(v5, &addr, 0x10u) == -1 )
{
shutdown(v5, 2);
close(v5);
return (unsigned int)-1;
}
fcntl(v5, 2, 1LL);
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *src, unsigned int a2, unsigned int a3)
{
int v4; // eax
unsigned int v5; // er12
int optval; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF

optval = 1;
*_errno_location() = 22;
if ( !src )
return (unsigned int)-1;
if ( !*src )
return (unsigned int)-1;
if ( !a2 )
return (unsigned int)-1;
v4 = socket(2, 1, 6);
v5 = v4;
if ( v4 == -1 )
return (unsigned int)-1;
setsockopt(v4, 6, 1, &optval, 4u);
if ( a3 )
{
*(_QWORD *)&addr.sa_family = a3;
*(_QWORD *)&addr.sa_data[6] = 0LL;
if ( setsockopt(v5, 1, 21, &addr, 0x10u) == -1 || setsockopt(v5, 1, 20, &addr, 0x10u) == -1 )
log_message(6LL, 0LL, "%s:%d: Failed to set timeout to %d\n", src, a2, a3);
}
if ( (unsigned int)func2(src, a2, (__int64)&addr) == -1 || connect(v5, &addr, 0x10u) == -1 )
{
shutdown(v5, 2);
close(v5);
return (unsigned int)-1;
}
fcntl(v5, 2, 1LL);
return v5;
}

original_code:
int
open_connection(const char *host, int port, unsigned int timeout)
{
    int val = 1;
    struct sockaddr_in sockaddr;
    int sock;
    (*__errno_location ()) = 22;
    if (host == ((void *)0) || strlen(host) == 0 || port == 0)
 return -1;
    sock = socket(2, SOCK_STREAM, IPPROTO_TCP);
    if (sock == -1)
 return -1;
    setsockopt(sock, IPPROTO_TCP, 1, &val, sizeof(val));
    if (timeout > 0) {
 struct timeval tv = { timeout, 0 };
 if (setsockopt(sock, 1, 21, &tv, sizeof(tv)) == -1
     || setsockopt(sock, 1, 20, &tv, sizeof(tv)) == -1)
     do { log_message(6, 0, "%s:%d: Failed to set timeout to %d\n", host, port, timeout); } while (0);
    }
    if (string2sockaddr(host, port, &sockaddr) == -1
 || connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1) {
 shutdown(sock, SHUT_RDWR);
 close(sock);
 return -1;
    }
    fcntl(sock, 2, 1);
    return sock;
}

canonical_original_code:
int open_connection(char *host, int port, unsigned int timeout)
{
    int val = 1;
    struct sockaddr_in sockaddr;
    int sock;
    (*__errno_location ()) = 22;
    if (host == ((void *)0) || strlen(host) == 0 || port == 0)
 return -1;
    sock = socket(2, SOCK_STREAM, IPPROTO_TCP);
    if (sock == -1)
 return -1;
    setsockopt(sock, IPPROTO_TCP, 1, &val, sizeof(val));
    if (timeout > 0) {
 struct timeval tv = { timeout, 0 };
 if (setsockopt(sock, 1, 21, &tv, sizeof(tv)) == -1
     || setsockopt(sock, 1, 20, &tv, sizeof(tv)) == -1)
     do { log_message(6, 0, "%s:%d: Failed to set timeout to %d\n", host, port, timeout); } while (0);
    }
    if (string2sockaddr(host, port, &sockaddr) == -1
 || connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1) {
 shutdown(sock, SHUT_RDWR);
 close(sock);
 return -1;
    }
    fcntl(sock, 2, 1);
    return sock;
}

variable_types:
timeout
unsigned int

variable_types: struct timeval { long tv_sec; long tv_usec; }


decompiled_code:
__int64 __fastcall open_lsocket(char *name)
{
unsigned int v1; // er12
struct sockaddr v3[8]; // [rsp+0h] [rbp-88h] BYREF

*_errno_location() = 22;
if ( !name )
return (unsigned int)-1;
if ( !*name )
return (unsigned int)-1;
v1 = socket(1, 1, 0);
if ( v1 == -1 )
return (unsigned int)-1;
unlink(name);
v3[0].sa_family = 1;
strncpy(v3[0].sa_data, name, 0x6CuLL);
if ( bind(v1, v3, 0x6Eu) == -1 || listen(v1, -1) == -1 )
{
shutdown(v1, 2);
close(v1);
return (unsigned int)-1;
}
fcntl(v1, 2, 1LL);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *name)
{
unsigned int v1; // er12
struct sockaddr v3[8]; // [rsp+0h] [rbp-88h] BYREF

*_errno_location() = 22;
if ( !name )
return (unsigned int)-1;
if ( !*name )
return (unsigned int)-1;
v1 = socket(1, 1, 0);
if ( v1 == -1 )
return (unsigned int)-1;
unlink(name);
v3[0].sa_family = 1;
strncpy(v3[0].sa_data, name, 0x6CuLL);
if ( bind(v1, v3, 0x6Eu) == -1 || listen(v1, -1) == -1 )
{
shutdown(v1, 2);
close(v1);
return (unsigned int)-1;
}
fcntl(v1, 2, 1LL);
return v1;
}

original_code:
int
open_lsocket(const char *filename)
{
    struct sockaddr_un sockaddr;
    int sock;
    (*__errno_location ()) = 22;
    if (filename == ((void *)0) || strlen(filename) == 0)
 return -1;
    sock = socket(1, SOCK_STREAM, 0);
    if (sock == -1)
 return -1;
    unlink(filename);
    sockaddr.sun_family = 1;
    strncpy(sockaddr.sun_path, filename, sizeof(sockaddr.sun_path));
    if (bind(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1
 || listen(sock, -1) == -1) {
 shutdown(sock, SHUT_RDWR);
 close(sock);
 return -1;
    }
    fcntl(sock, 2, 1);
    return sock;
}

canonical_original_code:
int open_lsocket(char *filename)
{
    struct sockaddr_un sockaddr;
    int sock;
    (*__errno_location ()) = 22;
    if (filename == ((void *)0) || strlen(filename) == 0)
 return -1;
    sock = socket(1, SOCK_STREAM, 0);
    if (sock == -1)
 return -1;
    unlink(filename);
    sockaddr.sun_family = 1;
    strncpy(sockaddr.sun_path, filename, sizeof(sockaddr.sun_path));
    if (bind(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1
 || listen(sock, -1) == -1) {
 shutdown(sock, SHUT_RDWR);
 close(sock);
 return -1;
    }
    fcntl(sock, 2, 1);
    return sock;
}

variable_types:
filename
char *

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }


decompiled_code:
__int64 __fastcall open_fifo(char *file)
{
int *v1; // rbx
unsigned int v2; // er12
int v3; // eax
struct stat v5; // [rsp+0h] [rbp-A8h] BYREF

if ( !mkfifo(file, 0x1180u) )
goto LABEL_5;
v1 = _errno_location();
if ( *v1 != 17 )
return (unsigned int)-1;
v2 = stat(file, &v5);
if ( v2 == -1 )
{
*v1 = 17;
return v2;
}
if ( (v5.st_mode & 0xF000) != 4096 )
return (unsigned int)-1;
LABEL_5:
v3 = open(file, 2178);
v2 = v3;
if ( v3 != -1 )
fcntl(v3, 2, 1LL);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(char *file)
{
int *v1; // rbx
unsigned int v2; // er12
int v3; // eax
struct stat v5; // [rsp+0h] [rbp-A8h] BYREF

if ( !mkfifo(file, 0x1180u) )
goto LABEL_5;
v1 = _errno_location();
if ( *v1 != 17 )
return (unsigned int)-1;
v2 = stat(file, &v5);
if ( v2 == -1 )
{
*v1 = 17;
return v2;
}
if ( (v5.st_mode & 0xF000) != 4096 )
return (unsigned int)-1;
LABEL_5:
v3 = open(file, 2178);
v2 = v3;
if ( v3 != -1 )
fcntl(v3, 2, 1LL);
return v2;
}

original_code:
int
open_fifo(const char *filename)
{
    struct stat sb;
    int fd;
    if (mkfifo(filename, 0010000 | 0600) != 0) {
 if ((*__errno_location ()) == 17) {
     if (stat(filename, &sb) == -1) {
  (*__errno_location ()) = 17;
  return -1;
     } else if (!((((sb.st_mode)) & 0170000) == (0010000))) {
  return -1;
     }
 } else {
     return -1;
 }
    }
    fd = open(filename, 02|0200|04000);
    if (fd == -1)
 return -1;
    fcntl(fd, 2, 1);
    return fd;
}

canonical_original_code:
int open_fifo(char *filename)
{
    struct stat sb;
    int fd;
    if (mkfifo(filename, 0010000 | 0600) != 0) {
 if ((*__errno_location ()) == 17) {
     if (stat(filename, &sb) == -1) {
  (*__errno_location ()) = 17;
  return -1;
     } else if (!((((sb.st_mode)) & 0170000) == (0010000))) {
  return -1;
     }
 } else {
     return -1;
 }
    }
    fd = open(filename, 02|0200|04000);
    if (fd == -1)
 return -1;
    fcntl(fd, 2, 1);
    return fd;
}

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

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


decompiled_code:
void *__fastcall fbuf_swap(__m128i *a1, __m128i *a2)
{
void *result; // rax
__m128i v3; // xmm3
__m128i v4; // [rsp+0h] [rbp-28h] BYREF
__m128i v5; // [rsp+10h] [rbp-18h] BYREF

v4 = _mm_loadu_si128(a1);
v5 = _mm_loadu_si128(a1 + 1);
result = memmove(a1, a2, 0x20uLL);
v3 = _mm_loadu_si128(&v5);
*a2 = _mm_loadu_si128(&v4);
a2[1] = v3;
return result;
}

canonical_decompiled_code:
void *__fastcall func0(__m128i *a1, __m128i *a2)
{
void *result; // rax
__m128i v3; // xmm3
__m128i v4; // [rsp+0h] [rbp-28h] BYREF
__m128i v5; // [rsp+10h] [rbp-18h] BYREF

v4 = _mm_loadu_si128(a1);
v5 = _mm_loadu_si128(a1 + 1);
result = memmove(a1, a2, 0x20uLL);
v3 = _mm_loadu_si128(&v5);
*a2 = _mm_loadu_si128(&v4);
a2[1] = v3;
return result;
}

original_code:
void
fbuf_swap(fbuf_t *fbuf1, fbuf_t *fbuf2)
{
    fbuf_t fbuf3;
    ;
    ;
    bcopy(fbuf1, &fbuf3, sizeof(fbuf_t));
    bcopy(fbuf2, fbuf1, sizeof(fbuf_t));
    bcopy(&fbuf3, fbuf2, sizeof(fbuf_t));
}

canonical_original_code:
void fbuf_swap(struct __fbuf *fbuf1, struct __fbuf *fbuf2)
{
    struct __fbuf fbuf3;
    ;
    ;
    bcopy(fbuf1, &fbuf3, sizeof(fbuf_t));
    bcopy(fbuf2, fbuf1, sizeof(fbuf_t));
    bcopy(&fbuf3, fbuf2, sizeof(fbuf_t));
}

variable_types: struct __fbuf { unsigned int id; char *data; unsigned int len; unsigned int prefmaxlen; unsigned int used; } *


decompiled_code:
__int64 __fastcall fbuf_extended(__int64 a1, int a2)
{
__int64 result; // rax
unsigned int v3; // esi
unsigned int v4; // ecx
unsigned int v6; // edx
bool v7; // di
_BYTE *v8; // rax

result = *(unsigned int *)(a1 + 16);
v3 = a2 + 1;
if ( (unsigned int)result >= v3 )
return result;
v4 = *(_DWORD *)(a1 + 20);
if ( (unsigned int)result > v4 && v4 )
{
LABEL_7:
*_errno_location() = 12;
return 0LL;
}
v6 = globalMaxLen;
v7 = globalMaxLen != 0;
if ( globalMaxLen < v3 && globalMaxLen )
{
if ( !v4 )
exit(99);
goto LABEL_7;
}
do
{
while ( 1 )
{
if ( !(_DWORD)result )
{
*(_DWORD *)(a1 + 16) = 0x2000;
LODWORD(result) = 0x2000;
if ( v3 <= 0x2000 )
goto LABEL_13;
goto LABEL_9;
}
if ( (unsigned int)result > 0xFFFF )
break;
LABEL_9:
LODWORD(result) = 2 * result;
*(_DWORD *)(a1 + 16) = result;
if ( v3 <= (unsigned int)result )
goto LABEL_13;
}
LODWORD(result) = result + 0x10000;
*(_DWORD *)(a1 + 16) = result;
}
while ( v3 > (unsigned int)result );
LABEL_13:
if ( v6 < (unsigned int)result && v7 )
{
*(_DWORD *)(a1 + 16) = v6;
LODWORD(result) = v6;
}
v8 = realloc(*(void **)(a1 + 8), (unsigned int)result);
if ( !v8 )
return 0LL;
if ( !*(_QWORD *)(a1 + 8) )
*v8 = 0;
*(_QWORD *)(a1 + 8) = v8;
return *(unsigned int *)(a1 + 16);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2)
{
__int64 result; // rax
unsigned int v3; // esi
unsigned int v4; // ecx
unsigned int v6; // edx
bool v7; // di
_BYTE *v8; // rax

result = *(unsigned int *)(a1 + 16);
v3 = a2 + 1;
if ( (unsigned int)result >= v3 )
return result;
v4 = *(_DWORD *)(a1 + 20);
if ( (unsigned int)result > v4 && v4 )
{
LABEL_7:
*_errno_location() = 12;
return 0LL;
}
v6 = globalMaxLen;
v7 = globalMaxLen != 0;
if ( globalMaxLen < v3 && globalMaxLen )
{
if ( !v4 )
exit(99);
goto LABEL_7;
}
do
{
while ( 1 )
{
if ( !(_DWORD)result )
{
*(_DWORD *)(a1 + 16) = 0x2000;
LODWORD(result) = 0x2000;
if ( v3 <= 0x2000 )
goto LABEL_13;
goto LABEL_9;
}
if ( (unsigned int)result > 0xFFFF )
break;
LABEL_9:
LODWORD(result) = 2 * result;
*(_DWORD *)(a1 + 16) = result;
if ( v3 <= (unsigned int)result )
goto LABEL_13;
}
LODWORD(result) = result + 0x10000;
*(_DWORD *)(a1 + 16) = result;
}
while ( v3 > (unsigned int)result );
LABEL_13:
if ( v6 < (unsigned int)result && v7 )
{
*(_DWORD *)(a1 + 16) = v6;
LODWORD(result) = v6;
}
v8 = realloc(*(void **)(a1 + 8), (unsigned int)result);
if ( !v8 )
return 0LL;
if ( !*(_QWORD *)(a1 + 8) )
*v8 = 0;
*(_QWORD *)(a1 + 8) = v8;
return *(unsigned int *)(a1 + 16);
}

original_code:
unsigned int
fbuf_extended(fbuf_t *fbuf, unsigned int newlen)
{
    char *p;
    newlen++;
    if (newlen <= fbuf->len)
 return fbuf->len;
    if (fbuf->prefmaxlen != 0 && fbuf->len > fbuf->prefmaxlen) {
 (*__errno_location ()) = 12;
        return 0;
    }
    if (globalMaxLen && newlen > globalMaxLen) {
 if (fbuf->prefmaxlen != 0) {
     (*__errno_location ()) = 12;
     return 0;
 } else {
     exit(99);
 }
    }
    while (newlen > fbuf->len) {
 if (fbuf->len == 0)
     fbuf->len = 8192;
 else if (fbuf->len < 64*1024)
     fbuf->len *= 2;
 else
     fbuf->len += 64*1024;
    }
    if (globalMaxLen && fbuf->len > globalMaxLen)
 fbuf->len = globalMaxLen;
    p = (char *)realloc(fbuf->data, fbuf->len);
    if (p) {
 if (!fbuf->data)
     p[0] = '\0';
 fbuf->data = p;
 ;
 return fbuf->len;
    } else {
 return 0;
    }
}

canonical_original_code:
unsigned int fbuf_extended(struct __fbuf *fbuf, unsigned int newlen)
{
    char *p;
    newlen++;
    if (newlen <= fbuf->len)
 return fbuf->len;
    if (fbuf->prefmaxlen != 0 && fbuf->len > fbuf->prefmaxlen) {
 (*__errno_location ()) = 12;
        return 0;
    }
    if (globalMaxLen && newlen > globalMaxLen) {
 if (fbuf->prefmaxlen != 0) {
     (*__errno_location ()) = 12;
     return 0;
 } else {
     exit(99);
 }
    }
    while (newlen > fbuf->len) {
 if (fbuf->len == 0)
     fbuf->len = 8192;
 else if (fbuf->len < 64*1024)
     fbuf->len *= 2;
 else
     fbuf->len += 64*1024;
    }
    if (globalMaxLen && fbuf->len > globalMaxLen)
 fbuf->len = globalMaxLen;
    p = (char *)realloc(fbuf->data, fbuf->len);
    if (p) {
 if (!fbuf->data)
     p[0] = '\0';
 fbuf->data = p;
 ;
 return fbuf->len;
    } else {
 return 0;
    }
}

variable_types:
newlen
unsigned int

variable_types:
p
char *

user_defined_types: struct __fbuf { unsigned int id; char *data; unsigned int len; unsigned int prefmaxlen; unsigned int used; }


decompiled_code:
__int64 __fastcall fbuf_shrink(__int64 a1)
{
int v1; // ecx
unsigned int v2; // er8
unsigned int v3; // ebx
unsigned int v4; // ecx
unsigned int v5; // eax
void *v6; // rax

v1 = *(_DWORD *)(a1 + 24);
v2 = *(_DWORD *)(a1 + 16);
if ( v1 )
{
v3 = *(_DWORD *)(a1 + 16);
v4 = v1 + 1;
while ( v3 > 0x2000 )
{
v5 = v3 - 0x10000;
if ( v3 <= 0x10000 )
v5 = v3 >> 1;
if ( v4 > v5 )
{
if ( v3 == v2 )
return v2;
LABEL_9:
v6 = realloc(*(void **)(a1 + 8), v3);
if ( !v6 )
return *(unsigned int *)(a1 + 16);
*(_QWORD *)(a1 + 8) = v6;
v2 = v3;
*(_DWORD *)(a1 + 16) = v3;
return v2;
}
v3 = v5;
}
if ( v3 == v2 )
return v2;
if ( v3 )
goto LABEL_9;
}
else if ( !v2 )
{
return v2;
}
free(*(void **)(a1 + 8));
*(_QWORD *)(a1 + 8) = 0LL;
*(_DWORD *)(a1 + 16) = 0;
return 0LL;
}

original_code:
unsigned int
fbuf_shrink(fbuf_t *fbuf)
{
    unsigned int newlen, len = fbuf->len;
    char *p;
    if (fbuf->used == 0)
 len = 0;
    do {
 newlen = len;
 if (len <= 8192)
     break;
 else if (len <= 64*1024)
     len /= 2;
 else
     len -= 64*1024;
    } while (len >= fbuf->used+1);
    if (newlen == fbuf->len)
 return fbuf->len;
    if (newlen == 0) {
 free(fbuf->data);
 fbuf->data = ((void *)0);
 fbuf->len = 0;
    } else if ((p = (char *)realloc(fbuf->data, newlen)) != ((void *)0)) {
 fbuf->data = p;
 fbuf->len = newlen;
    }
    ;
    return fbuf->len;
}

canonical_original_code:
unsigned int fbuf_shrink(struct __fbuf *fbuf)
{
    unsigned int newlen; unsigned int len = fbuf->len;
    char *p;
    if (fbuf->used == 0)
 len = 0;
    do {
 newlen = len;
 if (len <= 8192)
     break;
 else if (len <= 64*1024)
     len /= 2;
 else
     len -= 64*1024;
    } while (len >= fbuf->used+1);
    if (newlen == fbuf->len)
 return fbuf->len;
    if (newlen == 0) {
 free(fbuf->data);
 fbuf->data = ((void *)0);
 fbuf->len = 0;
    } else if ((p = (char *)realloc(fbuf->data, newlen)) != ((void *)0)) {
 fbuf->data = p;
 fbuf->len = newlen;
    }
    ;
    return fbuf->len;
}

variable_types:
len
unsigned int


original_code:
void
fbuf_destroy(fbuf_t *fbuf)
{
    if (fbuf->data)
 free(fbuf->data);
    fbuf->data = ((void *)0);
    fbuf->used = fbuf->len = 0;
    ;
}

canonical_original_code:
void fbuf_destroy(struct __fbuf *fbuf)
{
    if (fbuf->data)
 free(fbuf->data);
    fbuf->data = ((void *)0);
    fbuf->used = fbuf->len = 0;
    ;
}


decompiled_code:
void __fastcall fbuf_free(void *ptr)
{
void *v2; // rdi

v2 = (void *)*((_QWORD *)ptr + 1);
if ( v2 )
free(v2);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func5(void *ptr)
{
void *v2; // rdi

v2 = (void *)*((_QWORD *)ptr + 1);
if ( v2 )
free(v2);
free(ptr);
}

original_code:
void
fbuf_free(fbuf_t *fbuf)
{
    if (fbuf->data)
 free(fbuf->data);
    ;
    free(fbuf);
}

canonical_original_code:
void fbuf_free(struct __fbuf *fbuf)
{
    if (fbuf->data)
 free(fbuf->data);
    ;
    free(fbuf);
}