decompiled_code:
__int64 __fastcall more_heap(__int64 a1)
{
__int64 v1; // rbp
__int64 v2; // rax
__int64 i; // rbx
__int64 v4; // rdx

v1 = heap_top;
v2 = 0LL;
if ( a1 != -24 )
{
for ( i = 0LL; ; ++i )
{
v4 = phys_alloc(1LL);
if ( !v4 )
break;
page_map_4k(&pagedir, heap_top, v4, 3LL);
heap_top += 4096LL;
v2 = i + 1;
if ( (unsigned __int64)(a1 + 24) >> 12 == i )
goto LABEL_8;
}
bug("malloc(): Can't allocate physical pages for kernel heap.");
v2 = i;
}
LABEL_8:
*(_QWORD *)(v1 + 8) = (v2 << 12) - 24;
*(_BYTE *)(v1 + 3) &= 0x7Fu;
*(_DWORD *)v1 = *(_DWORD *)v1 & 0x80000000 | 0x7ABCDEF7;
*(_QWORD *)(v1 + 16) = 0LL;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 v1; // rbp
__int64 v2; // rax
__int64 i; // rbx
__int64 v4; // rdx

v1 = heap_top;
v2 = 0LL;
if ( a1 != -24 )
{
for ( i = 0LL; ; ++i )
{
v4 = phys_alloc(1LL);
if ( !v4 )
break;
page_map_4k(&pagedir, heap_top, v4, 3LL);
heap_top += 4096LL;
v2 = i + 1;
if ( (unsigned __int64)(a1 + 24) >> 12 == i )
goto LABEL_8;
}
bug("malloc(): Can't allocate physical pages for kernel heap.");
v2 = i;
}
LABEL_8:
*(_QWORD *)(v1 + 8) = (v2 << 12) - 24;
*(_BYTE *)(v1 + 3) &= 0x7Fu;
*(_DWORD *)v1 = *(_DWORD *)v1 & 0x80000000 | 0x7ABCDEF7;
*(_QWORD *)(v1 + 16) = 0LL;
return v1;
}

original_code:
static malloc_block_t *more_heap(ulong size)
{
 ulong page, count, i;
 malloc_block_t *original_top;
 original_top = heap_top;
 count = (size + sizeof(malloc_block_t) % 4096 == 0)
   ? (size + sizeof(malloc_block_t)) / 4096
   : (size + sizeof(malloc_block_t)) / 4096 + 1;
 for (i = 0; i < count; ++i)
 {
  page = phys_alloc(PA_KERNEL);
  if (page == 0)
  {
   bug("malloc(): Can't allocate physical pages for kernel heap.");
   break;
  }
  page_map_4k(pagedir, (ulong)heap_top, page, PAGE_WRITE | PAGE_PRESENT);
  heap_top = (malloc_block_t *)((char *)heap_top + 4096);
 }
 original_top->size = 4096 * i - sizeof(malloc_block_t);
 original_top->used = 0;
 original_top->magic = 0x7ABCDEF7;
 original_top->next = 0;
 return original_top;
}

canonical_original_code:
static struct _malloc_block *more_heap(unsigned long size)
{
 unsigned long page; unsigned long count; unsigned long i;
 struct _malloc_block *original_top;
 original_top = heap_top;
 count = (size + sizeof(malloc_block_t) % 4096 == 0)
   ? (size + sizeof(malloc_block_t)) / 4096
   : (size + sizeof(malloc_block_t)) / 4096 + 1;
 for (i = 0; i < count; ++i)
 {
  page = phys_alloc(PA_KERNEL);
  if (page == 0)
  {
   bug("malloc(): Can't allocate physical pages for kernel heap.");
   break;
  }
  page_map_4k(pagedir, (unsigned long)heap_top, page, PAGE_WRITE | PAGE_PRESENT);
  heap_top = (struct _malloc_block *)((char *)heap_top + 4096);
 }
 original_top->size = 4096 * i - sizeof(malloc_block_t);
 original_top->used = 0;
 original_top->magic = 0x7ABCDEF7;
 original_top->next = 0;
 return original_top;
}

variable_types:
page
unsigned long

variable_types:
count
unsigned long

variable_types:
i
unsigned long

user_defined_types: struct _malloc_block { unsigned int magic; unsigned int used; unsigned long size; struct _malloc_block *next; }


repo: martinliptak/kernel-from-scratch

decompiled_code:
__int64 malloc_dump_heap()
{
__int64 v0; // rbx
const char *v1; // rdx
__int64 result; // rax

v0 = qword_4B0;
if ( qword_4B0 )
{
while ( (*(_DWORD *)v0 & 0x7FFFFFFF) == 2059198199 )
{
v1 = "free";
if ( *(char *)(v0 + 3) < 0 )
v1 = "used";
result = printk("size: %6u %s next: %10x\n", *(_QWORD *)(v0 + 8), v1, *(_QWORD *)(v0 + 16));
v0 = *(_QWORD *)(v0 + 16);
if ( !v0 )
return result;
}
result = panic("Heap corrupted");
}
return result;
}

canonical_decompiled_code:
__int64 func3()
{
__int64 v0; // rbx
const char *v1; // rdx
__int64 result; // rax

v0 = qword_4B0;
if ( qword_4B0 )
{
while ( (*(_DWORD *)v0 & 0x7FFFFFFF) == 2059198199 )
{
v1 = "free";
if ( *(char *)(v0 + 3) < 0 )
v1 = "used";
result = printk("size: %6u %s next: %10x\n", *(_QWORD *)(v0 + 8), v1, *(_QWORD *)(v0 + 16));
v0 = *(_QWORD *)(v0 + 16);
if ( !v0 )
return result;
}
result = panic("Heap corrupted");
}
return result;
}

original_code:
void malloc_dump_heap()
{
 malloc_block_t *blk;
 for (blk = block_head.next; blk; blk = blk->next)
 {
  if (blk->magic != 0x7ABCDEF7)
  {
   panic("Heap corrupted");
   return;
  }
  printk("size: %6u %s next: %10x\n", blk->size, blk->used ?
    "used" : "free", blk->next);
 }
}

canonical_original_code:
void malloc_dump_heap()
{
 struct _malloc_block *blk;
 for (blk = block_head.next; blk; blk = blk->next)
 {
  if (blk->magic != 0x7ABCDEF7)
  {
   panic("Heap corrupted");
   return;
  }
  printk("size: %6u %s next: %10x\n", blk->size, blk->used ?
    "used" : "free", blk->next);
 }
}

variable_types: struct _malloc_block { unsigned int magic; unsigned int used; unsigned long size; struct _malloc_block *next; } *

return_type: void


malloc

decompiled_code:
__int64 __fastcall malloc(unsigned __int64 a1)
{
__int64 v1; // r12
char *v2; // rbx
unsigned __int64 v4; // rax
__int64 v5; // rax

v1 = 0LL;
if ( a1 )
{
lock();
if ( qword_4B0 )
{
v2 = (char *)&block_head;
while ( 1 )
{
if ( (*(_DWORD *)v2 & 0x7FFFFFFF) != 2059198199 )
panic("Heap corrupted");
if ( v2[3] >= 0 && *((_QWORD *)v2 + 1) >= a1 )
break;
v2 = (char *)*((_QWORD *)v2 + 2);
if ( !*((_QWORD *)v2 + 2) )
goto LABEL_13;
}
}
else
{
v2 = (char *)&block_head;
LABEL_13:
if ( (*(_DWORD *)v2 & 0x7FFFFFFF) != 2059198199 )
{
panic("Heap corrupted");
v1 = 0LL;
goto LABEL_10;
}
if ( v2[3] < 0 )
{
v5 = more_heap(a1);
*((_QWORD *)v2 + 2) = v5;
v1 = 0LL;
if ( *(_QWORD *)(v5 + 8) >= a1 )
v1 = cut_block(v5, a1);
goto LABEL_10;
}
v4 = *((_QWORD *)v2 + 1);
if ( v4 < a1 )
{
*((_QWORD *)v2 + 2) = more_heap(a1 - 24 - v4);
merge_free_blocks((__int64)v2);
v1 = 0LL;
if ( *((_QWORD *)v2 + 1) < a1 )
{
LABEL_10:
unlock();
return v1;
}
}
}
v1 = cut_block((__int64)v2, a1);
goto LABEL_10;
}
return v1;
}

original_code:
void *malloc(ulong size)
{
 malloc_block_t *blk, *ret;
 ulong page;
 void *retval;
 if (size == 0)
  return 0;
 lock();
 for (blk = &block_head; blk->next; blk = blk->next)
 {
  if (blk->magic != 0x7ABCDEF7)
   panic("Heap corrupted");
  if (!(blk->used || blk->size < size))
  {
   retval = cut_block(blk, size);
   goto ret;
  }
 }
 if (blk->magic != 0x7ABCDEF7)
 {
  panic("Heap corrupted");
  retval = 0;
  goto ret;
 }
 if (!(blk->used || blk->size < size))
 {
  retval = cut_block(blk, size);
  goto ret;
 }
 if (blk->used)
 {
  blk->next = more_heap(size);
  if (blk->next->size < size)
  {
   retval = 0;
  }
  else
  {
   retval = cut_block(blk->next, size);
  }
 }
 else
 {
  blk->next = more_heap(size - blk->size - sizeof(malloc_block_t));
  merge_free_blocks(blk);
  if (blk->size < size)
   retval = 0;
  else
   retval = cut_block(blk, size);
 }
ret:
 unlock();
 return retval;
}

canonical_original_code:
void *malloc(unsigned long size)
{
 struct _malloc_block *blk; struct _malloc_block *ret;
 unsigned long page;
 void *retval;
 if (size == 0)
  return 0;
 lock();
 for (blk = &block_head; blk->next; blk = blk->next)
 {
  if (blk->magic != 0x7ABCDEF7)
   panic("Heap corrupted");
  if (!(blk->used || blk->size < size))
  {
   retval = cut_block(blk, size);
   goto ret;
  }
 }
 if (blk->magic != 0x7ABCDEF7)
 {
  panic("Heap corrupted");
  retval = 0;
  goto ret;
 }
 if (!(blk->used || blk->size < size))
 {
  retval = cut_block(blk, size);
  goto ret;
 }
 if (blk->used)
 {
  blk->next = more_heap(size);
  if (blk->next->size < size)
  {
   retval = 0;
  }
  else
  {
   retval = cut_block(blk->next, size);
  }
 }
 else
 {
  blk->next = more_heap(size - blk->size - sizeof(malloc_block_t));
  merge_free_blocks(blk);
  if (blk->size < size)
   retval = 0;
  else
   retval = cut_block(blk, size);
 }
ret:
 unlock();
 return retval;
}

variable_types:
size
unsigned long

variable_types:
retval
void *

return_type: void *


free

decompiled_code:
void __fastcall free(__int64 a1)
{
__int64 v1; // rdi
__int64 v2; // rax
__int64 i; // rbx

v1 = a1 - 24;
v2 = qword_4B0;
if ( qword_4B0 )
{
while ( 1 )
{
if ( (*(_DWORD *)v2 & 0x7FFFFFFF) != 2059198199 )
{
panic("Heap corrupted");
return;
}
if ( v1 == v2 )
break;
v2 = *(_QWORD *)(v2 + 16);
if ( !v2 )
goto LABEL_5;
}
if ( *(char *)(v2 + 3) >= 0 )
{
bug("free(): Trying to free a block twice");
}
else
{
*(_BYTE *)(v2 + 3) &= 0x7Fu;
for ( i = qword_4B0; i; i = *(_QWORD *)(i + 16) )
merge_free_blocks(i);
}
}
else
{
LABEL_5:
bug("free(): Trying to free a block which is not allocated");
}
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1)
{
__int64 v1; // rdi
__int64 v2; // rax
__int64 i; // rbx

v1 = a1 - 24;
v2 = qword_4B0;
if ( qword_4B0 )
{
while ( 1 )
{
if ( (*(_DWORD *)v2 & 0x7FFFFFFF) != 2059198199 )
{
panic("Heap corrupted");
return;
}
if ( v1 == v2 )
break;
v2 = *(_QWORD *)(v2 + 16);
if ( !v2 )
goto LABEL_5;
}
if ( *(char *)(v2 + 3) >= 0 )
{
bug("free(): Trying to free a block twice");
}
else
{
*(_BYTE *)(v2 + 3) &= 0x7Fu;
for ( i = qword_4B0; i; i = *(_QWORD *)(i + 16) )
func0(i);
}
}
else
{
LABEL_5:
bug("free(): Trying to free a block which is not allocated");
}
}

original_code:
void free(void *addr)
{
 malloc_block_t *blk, *fblk;
 fblk = addr - sizeof(malloc_block_t);
 for (blk = block_head.next; blk; blk = blk->next)
 {
  if (blk->magic != 0x7ABCDEF7)
  {
   panic("Heap corrupted");
   return;
  }
  if (blk == fblk)
   goto found;
 }
 bug("free(): Trying to free a block which is not allocated");
 return;
found:
 if (blk->used == 0)
 {
  bug("free(): Trying to free a block twice");
  return;
 }
 blk->used = 0;
  for (blk = block_head.next; blk; blk = blk->next)
  merge_free_blocks(blk);
}

canonical_original_code:
void free(void *addr)
{
 struct _malloc_block *blk; struct _malloc_block *fblk;
 fblk = addr - sizeof(malloc_block_t);
 for (blk = block_head.next; blk; blk = blk->next)
 {
  if (blk->magic != 0x7ABCDEF7)
  {
   panic("Heap corrupted");
   return;
  }
  if (blk == fblk)
   goto found;
 }
 bug("free(): Trying to free a block which is not allocated");
 return;
found:
 if (blk->used == 0)
 {
  bug("free(): Trying to free a block twice");
  return;
 }
 blk->used = 0;
  for (blk = block_head.next; blk; blk = blk->next)
  merge_free_blocks(blk);
}

variable_types:
addr
void *


realloc

decompiled_code:
__int64 __fastcall realloc(void *src, unsigned __int64 a2)
{
__int64 v2; // r12
__int64 v3; // rbx
unsigned __int64 v5; // rax
void *v6; // rax

v3 = qword_4B0;
if ( !qword_4B0 )
goto LABEL_5;
while ( 1 )
{
if ( (*(_DWORD *)v3 & 0x7FFFFFFF) != 2059198199 )
{
panic("Heap corrupted");
return v2;
}
if ( (char *)src - 24 == (_BYTE *)v3 )
break;
v3 = *(_QWORD *)(v3 + 16);
if ( !v3 )
goto LABEL_5;
}
if ( *(char *)(v3 + 3) < 0 )
{
if ( a2 )
{
v5 = *(_QWORD *)(v3 + 8);
v2 = v3;
if ( v5 != a2 )
{
if ( v5 <= a2 )
{
free((__int64)src);
v6 = (void *)malloc(a2);
v2 = (__int64)v6;
if ( src != v6 )
memcpy(v6, src, *(_QWORD *)(v3 + 8));
}
else
{
v2 = cut_block(v3, a2);
}
}
}
else
{
free((__int64)src);
v2 = 0LL;
}
}
else
{
LABEL_5:
bug("realloc(): Trying to reallocate a block which is not allocated");
v2 = 0LL;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func6(void *src, unsigned __int64 a2)
{
__int64 v2; // r12
__int64 v3; // rbx
unsigned __int64 v5; // rax
void *v6; // rax

v3 = qword_4B0;
if ( !qword_4B0 )
goto LABEL_5;
while ( 1 )
{
if ( (*(_DWORD *)v3 & 0x7FFFFFFF) != 2059198199 )
{
panic("Heap corrupted");
return v2;
}
if ( (char *)src - 24 == (_BYTE *)v3 )
break;
v3 = *(_QWORD *)(v3 + 16);
if ( !v3 )
goto LABEL_5;
}
if ( *(char *)(v3 + 3) < 0 )
{
if ( a2 )
{
v5 = *(_QWORD *)(v3 + 8);
v2 = v3;
if ( v5 != a2 )
{
if ( v5 <= a2 )
{
func5((__int64)src);
v6 = (void *)func4(a2);
v2 = (__int64)v6;
if ( src != v6 )
memcpy(v6, src, *(_QWORD *)(v3 + 8));
}
else
{
v2 = func1(v3, a2);
}
}
}
else
{
func5((__int64)src);
v2 = 0LL;
}
}
else
{
LABEL_5:
bug("realloc(): Trying to reallocate a block which is not allocated");
v2 = 0LL;
}
return v2;
}

original_code:
void *realloc(void *addr, ulong size)
{
 malloc_block_t *blk, *fblk;
 void *naddr;
 fblk = addr - sizeof(malloc_block_t);
 for (blk = block_head.next; blk; blk = blk->next)
 {
  if (blk->magic != 0x7ABCDEF7)
  {
   panic("Heap corrupted");
   return;
  }
  if (blk == fblk)
   goto found;
 }
 bug("realloc(): Trying to reallocate a block which is not allocated");
 return 0;
found:
 if (blk->used == 0)
 {
  bug("realloc(): Trying to reallocate a block which is not allocated");
  return 0;
 }
 if (size == 0)
 {
  free(addr);
  return 0;
 }
 else if (blk->size == size)
 {
  return blk;
 }
 else if (blk->size > size)
 {
  return cut_block(blk, size);
 }
 else
 {
  free(addr);
  naddr = malloc(size);
  if (addr != naddr)
   memcpy(naddr, addr, blk->size);
  return naddr;
 }
}

canonical_original_code:
void *realloc(void *addr, unsigned long size)
{
 struct _malloc_block *blk; struct _malloc_block *fblk;
 void *naddr;
 fblk = addr - sizeof(malloc_block_t);
 for (blk = block_head.next; blk; blk = blk->next)
 {
  if (blk->magic != 0x7ABCDEF7)
  {
   panic("Heap corrupted");
   return;
  }
  if (blk == fblk)
   goto found;
 }
 bug("realloc(): Trying to reallocate a block which is not allocated");
 return 0;
found:
 if (blk->used == 0)
 {
  bug("realloc(): Trying to reallocate a block which is not allocated");
  return 0;
 }
 if (size == 0)
 {
  free(addr);
  return 0;
 }
 else if (blk->size == size)
 {
  return blk;
 }
 else if (blk->size > size)
 {
  return cut_block(blk, size);
 }
 else
 {
  free(addr);
  naddr = malloc(size);
  if (addr != naddr)
   memcpy(naddr, addr, blk->size);
  return naddr;
 }
}

variable_types:
naddr
void *


putchr

decompiled_code:
__int64 __fastcall putchr(__int64 a1, char a2)
{
return cons_putchar(cons, (unsigned int)a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, char a2)
{
return cons_putchar(cons, (unsigned int)a2);
}

original_code:
static void putchr(void *arg, const char c)
{
 cons_putchar(cons, c);
}

canonical_original_code:
static void putchr(void *arg, char c)
{
 cons_putchar(cons, c);
}

variable_types:
arg
void *

variable_types: c char


vprintk

decompiled_code:
__int64 __fastcall vprintk(_BYTE *a1, __int64 a2)
{
_BYTE *v2; // rdx
unsigned int v3; // ebx

v2 = a1;
if ( *a1 != 60 )
goto LABEL_4;
v3 = 0;
if ( (char)a1[1] - 48 <= opt_verbosity )
{
v2 = a1 + 3;
LABEL_4:
v3 = vxprintf(putchr, 0LL, v2, a2);
cons_update(cons);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(_BYTE *a1, __int64 a2)
{
_BYTE *v2; // rdx
unsigned int v3; // ebx

v2 = a1;
if ( *a1 != 60 )
goto LABEL_4;
v3 = 0;
if ( (char)a1[1] - 48 <= opt_verbosity )
{
v2 = a1 + 3;
LABEL_4:
v3 = vxprintf(putchr, 0LL, v2, a2);
cons_update(cons);
}
return v3;
}

original_code:
int vprintk(const char *format, va_list arg)
{
 int ret;
 if (format[0] == '<')
 {
  if (format[1] - '0' > opt_verbosity)
   return 0;
  format += 3;
 }
 ret = vxprintf(putchr, 0, format, arg);
 cons_update(cons);
 return ret;
}

canonical_original_code:
int vprintk(char *format, __builtin_va_list arg)
{
 int ret;
 if (format[0] == '<')
 {
  if (format[1] - '0' > opt_verbosity)
   return 0;
  format += 3;
 }
 ret = vxprintf(putchr, 0, format, arg);
 cons_update(cons);
 return ret;
}

variable_types:
format
char *

variable_types: ret int

return_type: int


printk

decompiled_code:
__int64 printk(_BYTE *a1, ...)
{
unsigned int v1; // ebx
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

if ( *a1 != 60 )
goto LABEL_4;
v1 = 0;
if ( (char)a1[1] - 48 <= opt_verbosity )
{
a1 += 3;
LABEL_4:
va_start(va, a1);
v1 = vxprintf(putchr, 0LL, a1, va);
cons_update(cons);
}
return v1;
}

canonical_decompiled_code:
__int64 func2(_BYTE *a1, ...)
{
unsigned int v1; // ebx
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

if ( *a1 != 60 )
goto LABEL_4;
v1 = 0;
if ( (char)a1[1] - 48 <= opt_verbosity )
{
a1 += 3;
LABEL_4:
va_start(va, a1);
v1 = vxprintf(putchr, 0LL, a1, va);
cons_update(cons);
}
return v1;
}

original_code:
int printk(const char *format, ...)
{
 va_list arg;
 int ret;
 if (format[0] == '<')
 {
  if (format[1] - '0' > opt_verbosity)
   return 0;
  format += 3;
 }
 __builtin_va_start(arg, format);
 ret = vxprintf(putchr, 0, format, arg);
 __builtin_va_end(arg);
 cons_update(cons);
 return ret;
}

canonical_original_code:
int printk(char *format, ...)
{
 __builtin_va_list arg;
 int ret;
 if (format[0] == '<')
 {
  if (format[1] - '0' > opt_verbosity)
   return 0;
  format += 3;
 }
 __builtin_va_start(arg, format);
 ret = vxprintf(putchr, 0, format, arg);
 __builtin_va_end(arg);
 cons_update(cons);
 return ret;
}


decompiled_code:
__int64 __fastcall buffer_register(__int64 a1, __int64 a2)
{
__int64 v2; // rax
unsigned int v3; // ebx
__int64 v4; // rcx

lock();
v2 = 0LL;
while ( 1 )
{
v3 = v2;
if ( !buffer[2 * v2 + 1] )
break;
if ( ++v2 == 32 )
{
unlock();
bug("Buffer table full");
return 0;
}
}
v4 = 2LL * (int)v2;
buffer[v4 + 1] = a1;
buffer[v4] = a2;
unlock();
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int64 v2; // rax
unsigned int v3; // ebx
__int64 v4; // rcx

lock();
v2 = 0LL;
while ( 1 )
{
v3 = v2;
if ( !buffer[2 * v2 + 1] )
break;
if ( ++v2 == 32 )
{
unlock();
bug("Buffer table full");
return 0;
}
}
v4 = 2LL * (int)v2;
buffer[v4 + 1] = a1;
buffer[v4] = a2;
unlock();
return v3;
}

original_code:
unsigned int buffer_register(buffer_handler_t handler, uint *len)
{
 int i;
 lock();
 for (i = 0; i < sizeof((buffer)) / sizeof((buffer)[0]); ++i)
 {
  if (buffer[i].handler == 0)
  {
   buffer[i].handler = handler;
   buffer[i].len = len;
   unlock();
   return i;
  }
 }
 unlock();
 bug("Buffer table full");
 return 0;
}

canonical_original_code:
unsigned int buffer_register(void (*handler)(unsigned int), unsigned int *len)
{
 int i;
 lock();
 for (i = 0; i < sizeof((buffer)) / sizeof((buffer)[0]); ++i)
 {
  if (buffer[i].handler == 0)
  {
   buffer[i].handler = handler;
   buffer[i].len = len;
   unlock();
   return i;
  }
 }
 unlock();
 bug("Buffer table full");
 return 0;
}

variable_types:
handler
void (*)(unsigned int)

variable_types:
len
unsigned int *

variable_types: i int

return_type: unsigned int


decompiled_code:
_DWORD *buffer_call_handlers()
{
__int64 *v0; // rbx
_DWORD *result; // rax
__int64 (*v2)(void); // rdx

v0 = buffer;
do
{
result = (_DWORD *)*v0;
if ( *v0 )
{
if ( *result )
{
v2 = (__int64 (*)(void))v0[1];
--*result;
result = (_DWORD *)v2();
}
}
v0 += 2;
}
while ( v0 != (__int64 *)(unsigned int)((_DWORD)buffer + 512) );
return result;
}

canonical_decompiled_code:
_DWORD *func1()
{
__int64 *v0; // rbx
_DWORD *result; // rax
__int64 (*v2)(void); // rdx

v0 = buffer;
do
{
result = (_DWORD *)*v0;
if ( *v0 )
{
if ( *result )
{
v2 = (__int64 (*)(void))v0[1];
--*result;
result = (_DWORD *)v2();
}
}
v0 += 2;
}
while ( v0 != (__int64 *)(unsigned int)((_DWORD)buffer + 512) );
return result;
}

original_code:
void buffer_call_handlers()
{
 int i;
 for (i = 0; i < sizeof((buffer)) / sizeof((buffer)[0]); ++i)
 {
  if (buffer[i].len && *buffer[i].len)
  {
   buffer[i].handler(--(*buffer[i].len));
  }
 }
}


init

decompiled_code:
__int64 init()
{
set_gdt();
set_idt();
set_isr_wrappers();
phys_init();
pic_init();
cons_init();
kbd_init();
return sched_init();
}

canonical_decompiled_code:
__int64 func0()
{
set_gdt();
set_idt();
set_isr_wrappers();
phys_init();
pic_init();
cons_init();
kbd_init();
return sched_init();
}

original_code:
static void init()
{
 set_gdt();
 set_idt();
 set_isr_wrappers();
 phys_init();
 pic_init();
 cons_init();
 kbd_init();
 sched_init();
}


test

decompiled_code:
void __noreturn test()
{
kthread_start(test_func1);
while ( 1 )
cons_putchar(cons, 48LL);
}

canonical_decompiled_code:
void __noreturn func1()
{
kthread_start(test_func1);
while ( 1 )
cons_putchar(cons, 48LL);
}

original_code:
static void test()
{
 kthread_start(test_func1);
 while (1)
 {
  cons_putchar(cons, '0');
 }
}


decompiled_code:
void __noreturn test_func1()
{
while ( 1 )
cons_putchar(cons, 49LL);
}

canonical_decompiled_code:
void __noreturn func2()
{
while ( 1 )
cons_putchar(cons, 49LL);
}

original_code:
static void test_func1()
{
 while (1)
 {
  cons_putchar(cons, '1');
 }
}


kmain

decompiled_code:
void __fastcall __noreturn kmain(__int64 a1)
{
lock();
mb_info = a1 + 3221225472LL;
cons_clear(cons);
init();
parse_cmdline(*(_QWORD *)(mb_info + 32) + 3221225472LL);
unlock();
printk("<1>Kernel boot successful\n");
test();
}

canonical_decompiled_code:
void __fastcall __noreturn func3(__int64 a1)
{
lock();
mb_info = a1 + 3221225472LL;
cons_clear(cons);
func0();
parse_cmdline(*(_QWORD *)(mb_info + 32) + 3221225472LL);
unlock();
printk("<1>Kernel boot successful\n");
func1();
}

original_code:
void kmain(multiboot_info_t *mbi)
{
 register int old_ticks = 0;
 lock();
 mb_info = (void *)((char *)(mbi) + 0xc0000000);
 cons_clear(cons);
 init();
 parse_cmdline((void *)((char *)(mb_info->cmdline) + 0xc0000000));
 unlock();
 printk("<1>Kernel boot successful\n");
 test();
 while (1)
 {
  buffer_call_handlers();
  if (timer_ticks != old_ticks && timer_ticks % 18 == 0)
  {
   printk("Seconds: %d\n", timer_ticks / 18);
   old_ticks = timer_ticks;
  }
 }
}

canonical_original_code:
void kmain(struct multiboot_info *mbi)
{
 register int old_ticks = 0;
 lock();
 mb_info = (void *)((char *)(mbi) + 0xc0000000);
 cons_clear(cons);
 init();
 parse_cmdline((void *)((char *)(mb_info->cmdline) + 0xc0000000));
 unlock();
 printk("<1>Kernel boot successful\n");
 test();
 while (1)
 {
  buffer_call_handlers();
  if (timer_ticks != old_ticks && timer_ticks % 18 == 0)
  {
   printk("Seconds: %d\n", timer_ticks / 18);
   old_ticks = timer_ticks;
  }
 }
}

variable_types: struct multiboot_info { unsigned long flags; unsigned long mem_lower; unsigned long mem_upper; unsigned long boot_device; char *cmdline; unsigned long mods_count; unsigned long mods_addr; struct elf_section_header_table { unsigned long num; unsigned long size; unsigned long addr; unsigned long shndx; } elf_sec; unsigned long mmap_length; unsigned long mmap_addr; } *

user_defined_types: struct multiboot_info { unsigned long flags; unsigned long mem_lower; unsigned long mem_upper; unsigned long boot_device; char *cmdline; unsigned long mods_count; unsigned long mods_addr; struct elf_section_header_table elf_sec; unsigned long mmap_length; unsigned long mmap_addr; }


lock

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

if ( !lock_depth )
result = irq_off();
++lock_depth;
return result;
}

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

if ( !lock_depth )
result = irq_off();
++lock_depth;
return result;
}

original_code:
void lock()
{
 if (lock_depth == 0)
  irq_off();
 ++lock_depth;
}


unlock

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

result = (unsigned int)lock_depth;
if ( lock_depth )
{
result = (unsigned int)(lock_depth - 1);
lock_depth = result;
if ( !(_DWORD)result )
result = irq_on();
}
return result;
}

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

result = (unsigned int)lock_depth;
if ( lock_depth )
{
result = (unsigned int)(lock_depth - 1);
lock_depth = result;
if ( !(_DWORD)result )
result = irq_on();
}
return result;
}

original_code:
void unlock()
{
 if (lock_depth == 0)
  return;
 --lock_depth;
 if (lock_depth == 0)
  irq_on();
}


sscanf

decompiled_code:
__int64 sscanf(char *a1, const char *a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
return vsscanf(a1, a2, (unsigned int *)va);
}

canonical_decompiled_code:
__int64 func1(char *a1, const char *a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
return func0(a1, a2, (unsigned int *)va);
}

original_code:
int sscanf(const char *str, const char *format, ...)
{
 va_list arg;
 int ret;
 __builtin_va_start(arg, format);
 ret = vsscanf(str, format, arg);
 __builtin_va_end(arg);
 return ret;
}

canonical_original_code:
int sscanf(char *str, char *format, ...)
{
 __builtin_va_list arg;
 int ret;
 __builtin_va_start(arg, format);
 ret = vsscanf(str, format, arg);
 __builtin_va_end(arg);
 return ret;
}

variable_types:
str
char *


decompiled_code:
void __fastcall dstring_free(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
free(*(void **)ptr);
free(ptr);
}

original_code:
void dstring_free(dstring_t *dstr)
{
 free(dstr->str);
 free(dstr);
}

canonical_original_code:
void dstring_free(struct dstring_t *dstr)
{
 free(dstr->str);
 free(dstr);
}

variable_types: struct dstring_t { char *str; unsigned long len; unsigned long allocated; } *


decompiled_code:
__int64 dstring_printf(__int64 a1, __int64 a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
return vxprintf(dstring_append_char, a1, a2, va);
}

canonical_decompiled_code:
__int64 func3(__int64 a1, __int64 a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
return vxprintf(dstring_append_char, a1, a2, va);
}

original_code:
int dstring_printf(dstring_t *dstr, const char *format, ...)
{
 va_list arg;
 int ret;
 __builtin_va_start(arg, format);
 ret = vxprintf((putchr_t)dstring_append_char, (void *)dstr, format, arg);
 __builtin_va_end(arg);
 return ret;
}

canonical_original_code:
int dstring_printf(struct dstring_t *dstr, char *format, ...)
{
 __builtin_va_list arg;
 int ret;
 __builtin_va_start(arg, format);
 ret = vxprintf((void (*)(void *arg, char c))dstring_append_char, (void *)dstr, format, arg);
 __builtin_va_end(arg);
 return ret;
}

user_defined_types: struct dstring_t { char *str; unsigned long len; unsigned long allocated; }


decompiled_code:
__int64 __fastcall segment_new(int a1, __int64 a2, char a3)
{
unsigned __int64 v4; // rax
int v5; // ebx
unsigned __int64 v6; // rdx

lock();
v4 = (unsigned int)((_DWORD)&gdt + 8);
v5 = 1;
while ( 1 )
{
v6 = v4;
if ( !*(_QWORD *)v4 && !*(_QWORD *)(v4 + 4) )
break;
++v5;
v4 += 8LL;
if ( v5 == 0x2000 )
{
panic("Unable to create a new segment descriptor, GDT is full", a2, v6);
v6 = 0LL;
break;
}
}
*(_WORD *)v6 = a2;
*(_WORD *)(v6 + 2) = a1;
*(_BYTE *)(v6 + 4) = BYTE2(a1);
*(_BYTE *)(v6 + 5) = a3;
*(_BYTE *)(v6 + 6) = BYTE2(a2) & 0xF | 0xC0;
*(_BYTE *)(v6 + 7) = HIBYTE(a1);
unlock();
return (unsigned int)(8 * v5);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, __int64 a2, char a3)
{
unsigned __int64 v4; // rax
int v5; // ebx
unsigned __int64 v6; // rdx

lock();
v4 = (unsigned int)((_DWORD)&gdt + 8);
v5 = 1;
while ( 1 )
{
v6 = v4;
if ( !*(_QWORD *)v4 && !*(_QWORD *)(v4 + 4) )
break;
++v5;
v4 += 8LL;
if ( v5 == 0x2000 )
{
panic("Unable to create a new segment descriptor, GDT is full", a2, v6);
v6 = 0LL;
break;
}
}
*(_WORD *)v6 = a2;
*(_WORD *)(v6 + 2) = a1;
*(_BYTE *)(v6 + 4) = BYTE2(a1);
*(_BYTE *)(v6 + 5) = a3;
*(_BYTE *)(v6 + 6) = BYTE2(a2) & 0xF | 0xC0;
*(_BYTE *)(v6 + 7) = HIBYTE(a1);
unlock();
return (unsigned int)(8 * v5);
}

original_code:
u16 segment_new(u32 base, u32 limit, u8 type)
{
 u8 *desc = 0;
 int i;
 lock();
 for (i = 1; i < 8192; ++i)
  if (*(u32 *)(gdt + i * 8) == 0 && *(u32 *)(gdt + i * 8 + 4) == 0)
  {
   desc = gdt + i * 8;
   break;
  }
 if (desc == 0)
  panic("Unable to create a new segment descriptor, GDT is full");
 ((u16 *)desc)[0] = limit & 0xffff;
 ((u16 *)desc)[1] = base & 0xffff;
 desc[4] = (base >> 16) & 0xff;
 desc[5] = type;
 desc[6] = (limit >> 16) & 0xf;
 desc[6] |= 0xc0;
 desc[7] = (base >> 24) & 0xff;
 unlock();
 return i << 3;
}

canonical_original_code:
unsigned short segment_new(unsigned long base, unsigned long limit, unsigned char type)
{
 unsigned char *desc = 0;
 int i;
 lock();
 for (i = 1; i < 8192; ++i)
  if (*(unsigned long *)(gdt + i * 8) == 0 && *(unsigned long *)(gdt + i * 8 + 4) == 0)
  {
   desc = gdt + i * 8;
   break;
  }
 if (desc == 0)
  panic("Unable to create a new segment descriptor, GDT is full");
 ((unsigned short *)desc)[0] = limit & 0xffff;
 ((unsigned short *)desc)[1] = base & 0xffff;
 desc[4] = (base >> 16) & 0xff;
 desc[5] = type;
 desc[6] = (limit >> 16) & 0xf;
 desc[6] |= 0xc0;
 desc[7] = (base >> 24) & 0xff;
 unlock();
 return i << 3;
}

variable_types:
base
unsigned long

variable_types:
limit
unsigned long

variable_types:
type
unsigned char

variable_types:
desc
unsigned char *

return_type: unsigned short


tagFree

repo: martinwguy/xvi

decompiled_code:
void tagFree()
{
void *v0; // r13
_QWORD **v1; // rbp
char *v2; // r12
_QWORD *v3; // rbx
void *v4; // rdi

v0 = hashtable;
if ( hashtable )
{
v1 = (_QWORD **)hashtable;
v2 = (char *)hashtable + 8072;
do
{
v3 = *v1;
while ( v3 )
{
v4 = v3;
v3 = (_QWORD *)*v3;
free(v4);
}
++v1;
}
while ( v1 != (_QWORD **)v2 );
free(v0);
hashtable = 0LL;
}
}

canonical_decompiled_code:
void func1()
{
void *v0; // r13
_QWORD **v1; // rbp
char *v2; // r12
_QWORD *v3; // rbx
void *v4; // rdi

v0 = hashtable;
if ( hashtable )
{
v1 = (_QWORD **)hashtable;
v2 = (char *)hashtable + 8072;
do
{
v3 = *v1;
while ( v3 )
{
v4 = v3;
v3 = (_QWORD *)*v3;
free(v4);
}
++v1;
}
while ( v1 != (_QWORD **)v2 );
free(v0);
hashtable = 0LL;
}
}

original_code:
static void
tagFree()
{
    int count;
    if (hashtable == ((void *)0)) {
 return;
    }
    for (count = 0; count < hashtabsize; count++) {
 TAG *tp, *tmp;
 for (tp = hashtable[count]; tp != ((void *)0); ) {
     tmp = tp;
     tp = tp->t_next;
     free(tmp);
 }
    }
    free(hashtable);
    hashtable = ((void *)0);
}

canonical_original_code:
static void tagFree()
{
    int count;
    if (hashtable == ((void *)0)) {
 return;
    }
    for (count = 0; count < hashtabsize; count++) {
 struct tag *tp; struct tag *tmp;
 for (tp = hashtable[count]; tp != ((void *)0); ) {
     tmp = tp;
     tp = tp->t_next;
     free(tmp);
 }
    }
    free(hashtable);
    hashtable = ((void *)0);
}

variable_types: count int

variable_types: struct tag { struct tag *t_next; char *t_name; char *t_file; char *t_locator; } *


tagword

decompiled_code:
int tagword()
{
__int64 v0; // rcx
int result; // eax
char i; // bl
const unsigned __int16 **v3; // rdx
char *v4; // rbp
char v5[50]; // [rsp+0h] [rbp-68h] BYREF
char v6[14]; // [rsp+32h] [rbp-36h] BYREF
__int64 v7; // [rsp+40h] [rbp-28h] BYREF
__int64 v8; // [rsp+48h] [rbp-20h]

v0 = *(_QWORD *)(*curwin + 8LL);
v7 = *(_QWORD *)*curwin;
v8 = v0;
result = v0;
for ( i = *(_BYTE *)(*(_QWORD *)(v7 + 16) + (int)v0); i == 32 || i == 9; i = *(_BYTE *)(*(_QWORD *)(v7 + 16) + (int)v8) )
{
result = inc(&v7);
if ( result )
break;
result = v8;
}
if ( i >= 0 && (v3 = _ctype_b_loc(), result = i, ((*v3)[i] & 8) != 0) || i == 95 )
{
v4 = v5;
while ( i >= 0 && ((*_ctype_b_loc())[i] & 8) != 0 || i == 95 )
{
if ( v4 == v6 )
goto LABEL_20;
*v4++ = i;
if ( (unsigned int)inc(&v7) )
break;
i = *(_BYTE *)(*(_QWORD *)(v7 + 16) + (int)v8);
}
if ( v4 < v6 )
{
*v4 = 0;
return exTag(v5, 0, 1u, 1);
}
LABEL_20:
result = beep();
}
return result;
}

canonical_decompiled_code:
int func4()
{
__int64 v0; // rcx
int result; // eax
char i; // bl
const unsigned __int16 **v3; // rdx
char *v4; // rbp
char v5[50]; // [rsp+0h] [rbp-68h] BYREF
char v6[14]; // [rsp+32h] [rbp-36h] BYREF
__int64 v7; // [rsp+40h] [rbp-28h] BYREF
__int64 v8; // [rsp+48h] [rbp-20h]

v0 = *(_QWORD *)(*curwin + 8LL);
v7 = *(_QWORD *)*curwin;
v8 = v0;
result = v0;
for ( i = *(_BYTE *)(*(_QWORD *)(v7 + 16) + (int)v0); i == 32 || i == 9; i = *(_BYTE *)(*(_QWORD *)(v7 + 16) + (int)v8) )
{
result = inc(&v7);
if ( result )
break;
result = v8;
}
if ( i >= 0 && (v3 = _ctype_b_loc(), result = i, ((*v3)[i] & 8) != 0) || i == 95 )
{
v4 = v5;
while ( i >= 0 && ((*_ctype_b_loc())[i] & 8) != 0 || i == 95 )
{
if ( v4 == v6 )
goto LABEL_20;
*v4++ = i;
if ( (unsigned int)inc(&v7) )
break;
i = *(_BYTE *)(*(_QWORD *)(v7 + 16) + (int)v8);
}
if ( v4 < v6 )
{
*v4 = 0;
return func3(v5, 0, 1u, 1);
}
LABEL_20:
result = beep();
}
return result;
}

original_code:
void
tagword()
{
    char ch;
    Posn pos;
    char tagbuf[50];
    char *cp = tagbuf;
    pos = *curwin->w_cursor;
    ch = ((&pos)->p_line->l_text[(&pos)->p_index]);
    while (((ch) == ' ' || (ch) == '\t')) {
 if (inc(&pos) != mv_SAMELINE) {
     break;
 }
 ch = ((&pos)->p_line->l_text[(&pos)->p_index]);
    }
    if (!(((((ch) & ~0x7f) == 0) && ((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short int) _ISalnum)) || (ch) == '_')) {
 return;
    }
    while ((((((ch) & ~0x7f) == 0) && ((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short int) _ISalnum)) || (ch) == '_') && cp < tagbuf + sizeof(tagbuf)) {
 *cp++ = ch;
 if (inc(&pos) != mv_SAMELINE)
     break;
 ch = ((&pos)->p_line->l_text[(&pos)->p_index]);
    }
    if (cp >= tagbuf + sizeof(tagbuf)) {
 beep();
 return;
    }
    *cp = '\0';
    (void) exTag(tagbuf, 0, 1, 1);
}

canonical_original_code:
void tagword()
{
    char ch;
    struct position pos;
    char tagbuf[50];
    char *cp = tagbuf;
    pos = *curwin->w_cursor;
    ch = ((&pos)->p_line->l_text[(&pos)->p_index]);
    while (((ch) == ' ' || (ch) == '\t')) {
 if (inc(&pos) != mv_SAMELINE) {
     break;
 }
 ch = ((&pos)->p_line->l_text[(&pos)->p_index]);
    }
    if (!(((((ch) & ~0x7f) == 0) && ((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short) _ISalnum)) || (ch) == '_')) {
 return;
    }
    while ((((((ch) & ~0x7f) == 0) && ((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short) _ISalnum)) || (ch) == '_') && cp < tagbuf + sizeof(tagbuf)) {
 *cp++ = ch;
 if (inc(&pos) != mv_SAMELINE)
     break;
 ch = ((&pos)->p_line->l_text[(&pos)->p_index]);
    }
    if (cp >= tagbuf + sizeof(tagbuf)) {
 beep();
 return;
    }
    *cp = '\0';
    (void) exTag(tagbuf, 0, 1, 1);
}

variable_types: ch char

variable_types: struct position { struct line { struct line *l_prev; struct line *l_next; char *l_text; int l_size; unsigned long l_number; } *p_line; int p_index; }

variable_types:
tagbuf
char [50]

variable_types:
cp
char *

user_defined_types: struct position { struct line *p_line; int p_index; }


cursupdate

decompiled_code:
__int64 cursupdate()
{
unsigned int *v0; // r12
int v1; // edx
_QWORD **v2; // rax
__int64 v3; // r14
int v4; // er13
unsigned int v5; // ebp
__int64 v6; // rbx
int v7; // eax
int v8; // eax
unsigned int v9; // edi
unsigned int v10; // ecx
unsigned int v11; // edx
int v12; // eax
int v13; // esi
__int64 result; // rax

v0 = (unsigned int *)curwin;
v1 = cntplines(*(_QWORD *)(curwin + 16LL), *(_QWORD *)*curwin);
v2 = (_QWORD **)curwin;
*(_DWORD *)(curwin + 80LL) = v1;
*(_DWORD *)(curwin + 100LL) = plines(**v2);
v3 = *(_QWORD *)(**(_QWORD **)v0 + 16LL);
v4 = *(_DWORD *)(*(_QWORD *)v0 + 8LL);
v5 = params[285];
if ( v5 )
v5 = 8;
if ( v4 < 0 )
{
v7 = 0;
}
else
{
v6 = 0LL;
v7 = 0;
do
{
v5 += v7;
v7 = vischar((unsigned int)*(char *)(v3 + v6++), 0LL);
}
while ( v4 >= (int)v6 );
}
if ( (unsigned int)(State - 2) > 1 && *(_BYTE *)(v3 + v4) )
v5 = v5 + v7 - 1;
v8 = params[285];
if ( v8 )
v8 = 8;
v9 = v5 - v8;
v0[22] = v5 - v8;
v10 = v0[11];
v11 = v5;
if ( v10 <= v5 )
{
v12 = v0[20] + 1;
do
{
v13 = v12;
v11 -= v10;
++v12;
}
while ( v11 >= v10 );
v5 = v11;
v0[20] = v13;
}
v0[21] = v5;
result = v0[10];
if ( v0[20] >= (int)result - 1 )
{
result = (unsigned int)(result - 2);
v0[20] = result;
}
if ( v0[24] )
{
v0[23] = v9;
v0[24] = 0;
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int *v0; // r12
int v1; // edx
_QWORD **v2; // rax
__int64 v3; // r14
int v4; // er13
unsigned int v5; // ebp
__int64 v6; // rbx
int v7; // eax
int v8; // eax
unsigned int v9; // edi
unsigned int v10; // ecx
unsigned int v11; // edx
int v12; // eax
int v13; // esi
__int64 result; // rax

v0 = (unsigned int *)curwin;
v1 = cntplines(*(_QWORD *)(curwin + 16LL), *(_QWORD *)*curwin);
v2 = (_QWORD **)curwin;
*(_DWORD *)(curwin + 80LL) = v1;
*(_DWORD *)(curwin + 100LL) = plines(**v2);
v3 = *(_QWORD *)(**(_QWORD **)v0 + 16LL);
v4 = *(_DWORD *)(*(_QWORD *)v0 + 8LL);
v5 = params[285];
if ( v5 )
v5 = 8;
if ( v4 < 0 )
{
v7 = 0;
}
else
{
v6 = 0LL;
v7 = 0;
do
{
v5 += v7;
v7 = vischar((unsigned int)*(char *)(v3 + v6++), 0LL);
}
while ( v4 >= (int)v6 );
}
if ( (unsigned int)(State - 2) > 1 && *(_BYTE *)(v3 + v4) )
v5 = v5 + v7 - 1;
v8 = params[285];
if ( v8 )
v8 = 8;
v9 = v5 - v8;
v0[22] = v5 - v8;
v10 = v0[11];
v11 = v5;
if ( v10 <= v5 )
{
v12 = v0[20] + 1;
do
{
v13 = v12;
v11 -= v10;
++v12;
}
while ( v11 >= v10 );
v5 = v11;
v0[20] = v13;
}
v0[21] = v5;
result = v0[10];
if ( v0[20] >= (int)result - 1 )
{
result = (unsigned int)(result - 2);
v0[20] = result;
}
if ( v0[24] )
{
v0[23] = v9;
v0[24] = 0;
}
return result;
}

original_code:
void
cursupdate()
{
    Xviwin *win = curwin;
    register int i;
    register char *cltp;
    register int ccol;
    register unsigned width;
    register int index;
    register int c;
    register Posn *curp;
    curwin->w_row = cntplines(curwin->w_topline, curwin->w_cursor->p_line);
    curwin->w_c_line_size = plines(curwin->w_cursor->p_line);
    curp = win->w_cursor;
    cltp = curp->p_line->l_text;
    index = curp->p_index;
    ccol = (params[P_number].p_value) ? 8 : 0;
    for (i = width = 0; i <= index;) {
 ccol += width;
 width = vischar((c = cltp[i++]), (char **) ((void *)0), ccol);
    }
    if (State != INSERT && State != REPLACE && cltp[index] != '\0') {
 ccol += width - 1;
    }
    win->w_virtcol = ccol - ((params[P_number].p_value) ? 8 : 0);
    while (ccol >= win->w_ncols) {
 win->w_row++;
 ccol -= win->w_ncols;
    }
    win->w_col = ccol;
    if (win->w_row >= win->w_nrows - 1) {
 win->w_row = win->w_nrows - 2;
    }
    if (win->w_set_want_col) {
 win->w_curswant = win->w_virtcol;
 win->w_set_want_col = 0;
    }
}

canonical_original_code:
void cursupdate()
{
    struct xviwin *win = curwin;
    register int i;
    register char *cltp;
    register int ccol;
    register unsigned int width;
    register int index;
    register int c;
    register struct position *curp;
    curwin->w_row = cntplines(curwin->w_topline, curwin->w_cursor->p_line);
    curwin->w_c_line_size = plines(curwin->w_cursor->p_line);
    curp = win->w_cursor;
    cltp = curp->p_line->l_text;
    index = curp->p_index;
    ccol = (params[P_number].p_value) ? 8 : 0;
    for (i = width = 0; i <= index;) {
 ccol += width;
 width = vischar((c = cltp[i++]), (char **) ((void *)0), ccol);
    }
    if (State != INSERT && State != REPLACE && cltp[index] != '\0') {
 ccol += width - 1;
    }
    win->w_virtcol = ccol - ((params[P_number].p_value) ? 8 : 0);
    while (ccol >= win->w_ncols) {
 win->w_row++;
 ccol -= win->w_ncols;
    }
    win->w_col = ccol;
    if (win->w_row >= win->w_nrows - 1) {
 win->w_row = win->w_nrows - 2;
    }
    if (win->w_set_want_col) {
 win->w_curswant = win->w_virtcol;
 win->w_set_want_col = 0;
    }
}

variable_types: struct xviwin { struct position { struct line { struct line *l_prev; struct line *l_next; char *l_text; int l_size; unsigned long l_number; } *p_line; int p_index; } *w_cursor; struct buffer { struct line *b_line0; struct line *b_file; struct line *b_lastline; struct line *b_prevline; char *b_Undotext; char *b_filename; char *b_tempfname; unsigned int b_flags; int b_nwindows; struct mark { struct position m_posn; int m_deleted; } b_mlist[26]; struct mark b_pcmark; struct changedata { unsigned int cd_nlevels; struct cmd { struct position cmd_startpos; struct position cmd_target; unsigned char cmd_flags; int cmd_two_char; int cmd_operator; int cmd_ch1; int cmd_ch2; long cmd_prenum; long cmd_opnum; int cmd_got_yp_name; int cmd_yp_name; int cmd_buffer_name; } *cd_vi_cmd; long cd_total_lines; struct change { struct change *c_next; enum c_type_t { C_LINE=0, C_CHAR=1, C_DEL_CHAR=2, C_POSITION=3 } c_type; unsigned long c_lineno; union { struct { long cul_nlines; struct line *cul_lines; } cu_l; struct { int cuc_index; int cuc_nchars; char *cuc_chars; } cu_c; struct { long cup_line; int cup_index; } cu_p; } c_u; } *cd_undo; struct change *cd_redo; } *b_change; } *w_buffer; struct line *w_topline; struct line *w_botline; struct virtscr { void *pv_sys_ptr; unsigned int pv_rows; unsigned int pv_cols; void *pv_window; struct line_struct { char *s_line; unsigned char *s_colour; int s_used; unsigned int s_flags; } *pv_int_lines; struct line_struct *pv_ext_lines; int pv_colours[5]; struct virtscr *(*v_open)(struct virtscr *, void *); void (*v_close)(struct virtscr *); void (*v_clear_all)(struct virtscr *); void (*v_clear_line)(struct virtscr *, int, int); void (*v_goto)(struct virtscr *, int, int); void (*v_write)(struct virtscr *, int, int, char *); void (*v_putc)(struct virtscr *, int, int, int); void (*v_set_colour)(struct virtscr *, int); int (*v_decode_colour)(struct virtscr *, int, char *); void (*v_flush)(struct virtscr *); void (*v_beep)(struct virtscr *); void (*v_insert)(struct virtscr *, int, int, char *); int (*v_scroll)(struct virtscr *, int, int, int); int (*v_can_scroll)(struct virtscr *, int, int, int); } *w_vs; unsigned int w_nrows; unsigned int w_ncols; unsigned int w_winpos; unsigned int w_cmdline; struct Flexbuf { char *fxb_chars; unsigned int fxb_max; unsigned int fxb_rcnt; unsigned int fxb_wcnt; } w_statusline; int w_row; int w_virtcol; int w_curswant; int w_set_want_col; int w_c_line_size; struct xviwin *w_last; struct xviwin *w_next; struct cmd *w_cmd; } *

variable_types:
cltp
char *

variable_types: ccol int

variable_types:
width
unsigned int

variable_types: index int

variable_types: c int

user_defined_types: struct xviwin { struct position *w_cursor; struct buffer *w_buffer; struct line *w_topline; struct line *w_botline; struct virtscr *w_vs; unsigned int w_nrows; unsigned int w_ncols; unsigned int w_winpos; unsigned int w_cmdline; struct Flexbuf w_statusline; int w_row; int w_virtcol; int w_curswant; int w_set_want_col; int w_c_line_size; struct xviwin *w_last; struct xviwin *w_next; struct cmd *w_cmd; }

user_defined_types: struct buffer { struct line *b_line0; struct line *b_file; struct line *b_lastline; struct line *b_prevline; char *b_Undotext; char *b_filename; char *b_tempfname; unsigned int b_flags; int b_nwindows; struct mark b_mlist[26]; struct mark b_pcmark; struct changedata *b_change; }

user_defined_types: struct virtscr { void *pv_sys_ptr; unsigned int pv_rows; unsigned int pv_cols; void *pv_window; struct line_struct *pv_int_lines; struct line_struct *pv_ext_lines; int pv_colours[5]; struct virtscr *(*v_open)(struct virtscr *, void *); void (*v_close)(struct virtscr *); void (*v_clear_all)(struct virtscr *); void (*v_clear_line)(struct virtscr *, int, int); void (*v_goto)(struct virtscr *, int, int); void (*v_write)(struct virtscr *, int, int, char *); void (*v_putc)(struct virtscr *, int, int, int); void (*v_set_colour)(struct virtscr *, int); int (*v_decode_colour)(struct virtscr *, int, char *); void (*v_flush)(struct virtscr *); void (*v_beep)(struct virtscr *); void (*v_insert)(struct virtscr *, int, int, char *); int (*v_scroll)(struct virtscr *, int, int, int); int (*v_can_scroll)(struct virtscr *, int, int, int); }

user_defined_types: struct changedata { unsigned int cd_nlevels; struct cmd *cd_vi_cmd; long cd_total_lines; struct change *cd_undo; struct change *cd_redo; }

user_defined_types: struct change { struct change *c_next; enum c_type_t c_type; unsigned long c_lineno; union { struct { long cul_nlines; struct line *cul_lines; } cu_l; struct { int cuc_index; int cuc_nchars; char *cuc_chars; } cu_c; struct { long cup_line; int cup_index; } cu_p; } c_u; }


decompiled_code:
__int64 cmd_buf_alloc()
{
int v0; // ebp
__int64 v1; // rax
char *v2; // rbx
void *v3; // rax
__int64 result; // rax

v0 = cmdsz + 80;
v1 = re_alloc(inbuf, cmdsz + 80);
if ( v1 )
{
v2 = (char *)v1;
v3 = (void *)re_alloc(colposn, 2LL * v0);
if ( v3 )
{
cmdsz = v0;
inbuf = v2;
colposn = v3;
result = 1LL;
}
else
{
free(v2);
result = 0LL;
}
}
else
{
show_error("Failed to allocate command line inbuf");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
int v0; // ebp
__int64 v1; // rax
char *v2; // rbx
void *v3; // rax
__int64 result; // rax

v0 = cmdsz + 80;
v1 = re_alloc(inbuf, cmdsz + 80);
if ( v1 )
{
v2 = (char *)v1;
v3 = (void *)re_alloc(colposn, 2LL * v0);
if ( v3 )
{
cmdsz = v0;
inbuf = v2;
colposn = v3;
result = 1LL;
}
else
{
free(v2);
result = 0LL;
}
}
else
{
show_error("Failed to allocate command line inbuf");
result = 0LL;
}
return result;
}

original_code:
static bool_t
cmd_buf_alloc()
{
    int new_cmdsz;
    char *new_inbuf;
    unsigned short *new_colposn;
    new_cmdsz = (cmdsz == 0) ? 80 : cmdsz + 80;
    if ((new_inbuf = re_alloc(inbuf, new_cmdsz)) == ((void *)0)) {
 show_error("Failed to allocate command line inbuf");
 return 0;
    }
    if ((new_colposn = re_alloc(colposn, new_cmdsz * sizeof(*colposn))) == ((void *)0)) {
 free(new_inbuf);
 return 0;
    }
    cmdsz = new_cmdsz;
    inbuf = new_inbuf;
    colposn = new_colposn;
    return 1;
}

canonical_original_code:
static int cmd_buf_alloc()
{
    int new_cmdsz;
    char *new_inbuf;
    unsigned short *new_colposn;
    new_cmdsz = (cmdsz == 0) ? 80 : cmdsz + 80;
    if ((new_inbuf = re_alloc(inbuf, new_cmdsz)) == ((void *)0)) {
 show_error("Failed to allocate command line inbuf");
 return 0;
    }
    if ((new_colposn = re_alloc(colposn, new_cmdsz * sizeof(*colposn))) == ((void *)0)) {
 free(new_inbuf);
 return 0;
    }
    cmdsz = new_cmdsz;
    inbuf = new_inbuf;
    colposn = new_colposn;
    return 1;
}


decompiled_code:
char *get_cmd()
{
return inbuf;
}

canonical_decompiled_code:
char *func4()
{
return inbuf;
}

original_code:
char *
get_cmd()
{
    return(inbuf);
}

canonical_original_code:
char *get_cmd()
{
    return(inbuf);
}

return_type: char *


decompiled_code:
__int64 get_pos()
{
return (unsigned int)*((unsigned __int16 *)colposn + (unsigned int)inpos) - literal_next;
}

canonical_decompiled_code:
__int64 func5()
{
return (unsigned int)*((unsigned __int16 *)colposn + (unsigned int)inpos) - literal_next;
}

original_code:
int
get_pos()
{
    return(colposn[inpos] - literal_next);
}


openbwd

decompiled_code:
__int64 openbwd()
{
__int64 v0; // rbp
int v1; // eax
__int64 v2; // rbx
__int64 result; // rax
int v4; // eax

v0 = *(_QWORD *)*curwin;
v1 = strlen(*(const char **)(v0 + 16));
v2 = newline((unsigned int)(v1 + 1));
result = 0LL;
if ( v2 )
{
repllines(v0, 0LL, v2);
if ( params[35] )
{
**(_BYTE **)(v2 + 16) = 0;
v4 = get_indent(v0);
indentchars = set_indent(v2, v4);
}
else
{
indentchars = 0;
}
move_cursor(v2, indentchars);
move_window_to_cursor();
cursupdate();
xvUpdateAllBufferWindows();
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func7()
{
__int64 v0; // rbp
int v1; // eax
__int64 v2; // rbx
__int64 result; // rax
int v4; // eax

v0 = *(_QWORD *)*curwin;
v1 = strlen(*(const char **)(v0 + 16));
v2 = newline((unsigned int)(v1 + 1));
result = 0LL;
if ( v2 )
{
repllines(v0, 0LL, v2);
if ( params[35] )
{
**(_BYTE **)(v2 + 16) = 0;
v4 = func4(v0);
indentchars = func5(v2, v4);
}
else
{
indentchars = 0;
}
move_cursor(v2, indentchars);
move_window_to_cursor();
cursupdate();
xvUpdateAllBufferWindows();
result = 1LL;
}
return result;
}

original_code:
bool_t
openbwd()
{
    Line *l;
    register Line *oldline;
    register char *otext;
    oldline = curwin->w_cursor->p_line;
    otext = oldline->l_text;
    l = newline(strlen(otext) + 1);
    if (l == ((void *)0))
 return(0);
    repllines(oldline, 0L, l);
    if ((params[P_autoindent].p_value)) {
 *l->l_text = '\0';
 indentchars = set_indent(l, get_indent(oldline));
    } else {
 indentchars = 0;
    }
    move_cursor(l, indentchars);
    move_window_to_cursor();
    cursupdate();
    xvUpdateAllBufferWindows();
    return(1);
}

canonical_original_code:
int openbwd()
{
    struct line *l;
    register struct line *oldline;
    register char *otext;
    oldline = curwin->w_cursor->p_line;
    otext = oldline->l_text;
    l = newline(strlen(otext) + 1);
    if (l == ((void *)0))
 return(0);
    repllines(oldline, 0L, l);
    if ((params[P_autoindent].p_value)) {
 *l->l_text = '\0';
 indentchars = set_indent(l, get_indent(oldline));
    } else {
 indentchars = 0;
    }
    move_cursor(l, indentchars);
    move_window_to_cursor();
    cursupdate();
    xvUpdateAllBufferWindows();
    return(1);
}

variable_types: struct line { struct line *l_prev; struct line *l_next; char *l_text; int l_size; unsigned long l_number; } *

variable_types:
otext
char *


xvChangesNotSaved

decompiled_code:
__int64 xvChangesNotSaved()
{
__int64 v0; // rbx
__int64 v2; // rdx
__int64 v3; // rax

v0 = curwin;
while ( (*(_BYTE *)(curbuf + 56LL) & 1) == 0 )
{
v3 = xvNextWindow(curwin);
curwin = v3;
v2 = v3;
if ( v3 )
v2 = *(_QWORD *)(v3 + 8);
curbuf = v2;
if ( v3 == v0 )
return 0LL;
}
curwin = v0;
if ( v0 )
v0 = *(_QWORD *)(v0 + 8);
curbuf = v0;
return 1LL;
}

canonical_decompiled_code:
__int64 func12()
{
__int64 v0; // rbx
__int64 v2; // rdx
__int64 v3; // rax

v0 = curwin;
while ( (*(_BYTE *)(curbuf + 56LL) & 1) == 0 )
{
v3 = xvNextWindow(curwin);
curwin = v3;
v2 = v3;
if ( v3 )
v2 = *(_QWORD *)(v3 + 8);
curbuf = v2;
if ( v3 == v0 )
return 0LL;
}
curwin = v0;
if ( v0 )
v0 = *(_QWORD *)(v0 + 8);
curbuf = v0;
return 1LL;
}

original_code:
bool_t
xvChangesNotSaved()
{
    Xviwin *savecurwin;
    savecurwin = curwin;
    do {
 if (((curbuf)->b_flags & 0x1)) {
     do { curwin=(savecurwin); curbuf = curwin != ((void *)0) ? curwin->w_buffer : ((void *)0); } while(0);
     return(1);
 }
 do { curwin=(xvNextWindow(curwin)); curbuf = curwin != ((void *)0) ? curwin->w_buffer : ((void *)0); } while(0);
    } while (curwin != savecurwin);
    return(0);
}

canonical_original_code:
int xvChangesNotSaved()
{
    struct xviwin *savecurwin;
    savecurwin = curwin;
    do {
 if (((curbuf)->b_flags & 0x1)) {
     do { curwin=(savecurwin); curbuf = curwin != ((void *)0) ? curwin->w_buffer : ((void *)0); } while(0);
     return(1);
 }
 do { curwin=(xvNextWindow(curwin)); curbuf = curwin != ((void *)0) ? curwin->w_buffer : ((void *)0); } while(0);
    } while (curwin != savecurwin);
    return(0);
}

user_defined_types: struct Flexbuf { char *fxb_chars; unsigned int fxb_max; unsigned int fxb_rcnt; unsigned int fxb_wcnt; }

user_defined_types: struct line { struct line *l_prev; struct line *l_next; char *l_text; int l_size; unsigned long l_number; }

user_defined_types: struct mark { struct position m_posn; int m_deleted; }

user_defined_types: struct line_struct { char *s_line; unsigned char *s_colour; int s_used; unsigned int s_flags; }

user_defined_types: struct cmd { struct position cmd_startpos; struct position cmd_target; unsigned char cmd_flags; int cmd_two_char; int cmd_operator; int cmd_ch1; int cmd_ch2; long cmd_prenum; long cmd_opnum; int cmd_got_yp_name; int cmd_yp_name; int cmd_buffer_name; }

user_defined_types: enum c_type_t { C_LINE=0, C_CHAR=1, C_DEL_CHAR=2, C_POSITION=3 }


xvAutoWriteAll

decompiled_code:
void xvAutoWriteAll()
{
__int64 v0; // rbx
__int64 v1; // rax
__int64 v2; // rdx

if ( params[65] )
{
v0 = curwin;
do
{
if ( (*(_BYTE *)(curbuf + 56LL) & 1) != 0 )
exWriteToFile(0LL, 0LL, 0LL, 0LL);
v1 = xvNextWindow(curwin);
curwin = v1;
v2 = v1;
if ( v1 )
v2 = *(_QWORD *)(v1 + 8);
curbuf = v2;
}
while ( v1 != v0 );
}
}

canonical_decompiled_code:
void func13()
{
__int64 v0; // rbx
__int64 v1; // rax
__int64 v2; // rdx

if ( params[65] )
{
v0 = curwin;
do
{
if ( (*(_BYTE *)(curbuf + 56LL) & 1) != 0 )
exWriteToFile(0LL, 0LL, 0LL, 0LL);
v1 = xvNextWindow(curwin);
curwin = v1;
v2 = v1;
if ( v1 )
v2 = *(_QWORD *)(v1 + 8);
curbuf = v2;
}
while ( v1 != v0 );
}
}

original_code:
void
xvAutoWriteAll()
{
    if ((params[P_autowrite].p_value)) {
 Xviwin *savecurwin = curwin;
 do {
     if (((curbuf)->b_flags & 0x1)) {
  (void) exWriteToFile((char *) ((void *)0), (Line *) ((void *)0),
          (Line *) ((void *)0), 0);
     }
     do { curwin=(xvNextWindow(curwin)); curbuf = curwin != ((void *)0) ? curwin->w_buffer : ((void *)0); } while(0);
 } while (curwin != savecurwin);
    }
    return;
}

canonical_original_code:
void xvAutoWriteAll()
{
    if ((params[P_autowrite].p_value)) {
 struct xviwin *savecurwin = curwin;
 do {
     if (((curbuf)->b_flags & 0x1)) {
  (void) exWriteToFile((char *) ((void *)0), (struct line *) ((void *)0),
          (struct line *) ((void *)0), 0);
     }
     do { curwin=(xvNextWindow(curwin)); curbuf = curwin != ((void *)0) ? curwin->w_buffer : ((void *)0); } while(0);
 } while (curwin != savecurwin);
    }
    return;
}


xvAutoWrite

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

result = curbuf;
if ( (*(_BYTE *)(curbuf + 56LL) & 1) != 0 )
{
if ( params[65] )
result = exWriteToFile(0LL, 0LL, 0LL, 0LL);
}
return result;
}

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

result = curbuf;
if ( (*(_BYTE *)(curbuf + 56LL) & 1) != 0 )
{
if ( params[65] )
result = exWriteToFile(0LL, 0LL, 0LL, 0LL);
}
return result;
}

original_code:
void
xvAutoWrite()
{
    if (((curbuf)->b_flags & 0x1) && (params[P_autowrite].p_value)) {
 (void) exWriteToFile((char *) ((void *)0), (Line *) ((void *)0), (Line *) ((void *)0), 0);
    }
}

canonical_original_code:
void xvAutoWrite()
{
    if (((curbuf)->b_flags & 0x1) && (params[P_autowrite].p_value)) {
 (void) exWriteToFile((char *) ((void *)0), (struct line *) ((void *)0), (struct line *) ((void *)0), 0);
    }
}


kbgetc

decompiled_code:
__int64 kbgetc()
{
unsigned __int8 *v0; // rax
int v2; // eax
int v3; // eax
struct timeval v4; // [rsp+0h] [rbp-A8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-98h] BYREF

if ( kbdintr )
return 0xFFFFFFFFLL;
if ( kb_nchars <= 0 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[0] |= 1uLL;
while ( 1 )
{
v4.tv_sec = (__int64)current_timeout / 1000;
v4.tv_usec = 1000 * (__int64)current_timeout % 1000000;
v2 = select(1, &readfds, 0LL, 0LL, &v4);
if ( v2 > 0 )
break;
if ( !v2 || kbdintr )
return 0xFFFFFFFFLL;
}
v3 = read(0, &kbuf_7, 0x30uLL);
if ( v3 <= 0 )
{
SIG_user_disconnected = 1;
return 0xFFFFFFFFLL;
}
kb_nchars = v3;
kbp_6 = (__int64)&kbuf_7;
}
if ( win_size_changed )
return 0xFFFFFFFFLL;
--kb_nchars;
v0 = (unsigned __int8 *)kbp_6++;
return *v0;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned __int8 *v0; // rax
int v2; // eax
int v3; // eax
struct timeval v4; // [rsp+0h] [rbp-A8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-98h] BYREF

if ( kbdintr )
return 0xFFFFFFFFLL;
if ( kb_nchars <= 0 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[0] |= 1uLL;
while ( 1 )
{
v4.tv_sec = (__int64)current_timeout / 1000;
v4.tv_usec = 1000 * (__int64)current_timeout % 1000000;
v2 = select(1, &readfds, 0LL, 0LL, &v4);
if ( v2 > 0 )
break;
if ( !v2 || kbdintr )
return 0xFFFFFFFFLL;
}
v3 = read(0, &kbuf_7, 0x30uLL);
if ( v3 <= 0 )
{
SIG_user_disconnected = 1;
return 0xFFFFFFFFLL;
}
kb_nchars = v3;
kbp_6 = (__int64)&kbuf_7;
}
if ( win_size_changed )
return 0xFFFFFFFFLL;
--kb_nchars;
v0 = (unsigned __int8 *)kbp_6++;
return *v0;
}

original_code:
static int
kbgetc()
{
    static unsigned char kbuf[48];
    static unsigned char *kbp;
    if (kbdintr)
 return (-1);
    if (kb_nchars <= 0) {
        fd_set rfds;
        struct timeval tv;
        int retval;
        int nread;
        do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->__fds_bits)[0]) : "memory"); } while (0);
        ((void) (((&rfds)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
        while (1) {
     tv.tv_sec = (long) (current_timeout / 1000);
     tv.tv_usec = ((long) current_timeout * 1000) % (long) 1000000;
            retval = select(1, &rfds, ((void *)0), ((void *)0), &tv);
            if (retval > 0)
                 break;
            if (retval == 0 || kbdintr)
                return (-1);
        }
 if ((nread = read(0, (char *) kbuf, sizeof kbuf)) <= 0) {
     SIG_user_disconnected = 1;
     return (-1);
 } else {
     kb_nchars = nread;
     kbp = kbuf;
 }
    }
    if (win_size_changed) {
 return((-1));
    }
    --kb_nchars;
    return(*kbp++);
}

canonical_original_code:
static int kbgetc()
{
    static unsigned char kbuf[48];
    static unsigned char *kbp;
    if (kbdintr)
 return (-1);
    if (kb_nchars <= 0) {
        struct fd_set rfds;
        struct timeval tv;
        int retval;
        int nread;
        do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->__fds_bits)[0]) : "memory"); } while (0);
        ((void) (((&rfds)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
        while (1) {
     tv.tv_sec = (long) (current_timeout / 1000);
     tv.tv_usec = ((long) current_timeout * 1000) % (long) 1000000;
            retval = select(1, &rfds, ((void *)0), ((void *)0), &tv);
            if (retval > 0)
                 break;
            if (retval == 0 || kbdintr)
                return (-1);
        }
 if ((nread = read(0, (char *) kbuf, sizeof kbuf)) <= 0) {
     SIG_user_disconnected = 1;
     return (-1);
 } else {
     kb_nchars = nread;
     kbp = kbuf;
 }
    }
    if (win_size_changed) {
 return((-1));
    }
    --kb_nchars;
    return(*kbp++);
}

variable_types:
kbuf
unsigned char [48]

variable_types:
kbp
unsigned char *

variable_types:
rfds
struct fd_set { long __fds_bits[-1]; }

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

variable_types: retval int

variable_types: nread int


decompiled_code:
unsigned int Wait200ms()
{
return sleep(1u);
}

canonical_decompiled_code:
unsigned int func7()
{
return sleep(1u);
}

original_code:
void
Wait200ms()
{
    sleep(1);
}

canonical_original_code:
void Wait200ms()
{
    sleep(1);
}


decompiled_code:
int sys_endv()
{
int result; // eax

if ( curmode )
{
tty_goto((unsigned int)(LI - 1), 0LL);
erase_line();
tty_endv();
fflush(stdout);
result = tcsetattr(0, 1, &cooked_state);
curmode = 0;
}
return result;
}

canonical_decompiled_code:
int func9()
{
int result; // eax

if ( curmode )
{
tty_goto((unsigned int)(LI - 1), 0LL);
erase_line();
tty_endv();
fflush(stdout);
result = tcsetattr(0, 1, &cooked_state);
curmode = 0;
}
return result;
}

original_code:
void
sys_endv()
{
    if (curmode == m_SYS)
 return;
    tty_goto((int) LI - 1, 0);
    erase_line();
    tty_endv();
    (void) fflush(stdout);
    ((void) tcsetattr(0, 1, (&cooked_state)));
    curmode = m_SYS;
}


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

if ( curmode != 1 )
{
tcsetattr(0, 1, &raw_state);
result = tty_startv();
curmode = 1;
}
return result;
}

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

if ( curmode != 1 )
{
tcsetattr(0, 1, &raw_state);
result = tty_startv();
curmode = 1;
}
return result;
}

original_code:
void
sys_startv()
{
    if (curmode == m_VI)
 return;
    ((void) tcsetattr(0, 1, (&raw_state)));
    tty_startv();
    curmode = m_VI;
}


decompiled_code:
__int64 sys_init()
{
__m128i v0; // xmm0
__m128i v1; // xmm1
__int64 result; // rax
int v3; // [rsp+8h] [rbp-10h] BYREF
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

v4[0] = 0;
v3 = 0;
tcgetattr(0, &cooked_state);
v0 = _mm_load_si128((const __m128i *)&cooked_state);
*(__m128i *)&raw_state.c_iflag = v0;
v1 = _mm_load_si128((const __m128i *)&cooked_state.c_line);
*(__m128i *)&raw_state.c_line = v1;
*(__m128i *)&raw_state.c_cc[15] = _mm_load_si128((const __m128i *)&cooked_state.c_cc[15]);
*(_QWORD *)&raw_state.c_cc[31] = *(_QWORD *)&cooked_state.c_cc[31];
raw_state.c_ospeed = cooked_state.c_ospeed;
raw_state.c_oflag = v0.m128i_i32[1] & 0xFFFFE7FA;
raw_state.c_lflag = v0.m128i_i32[3] & 0xFFFFF181;
raw_state.c_iflag = v0.m128i_i32[0] & 0xFFFFFE3F;
raw_state.c_cc[6] = 1;
raw_state.c_cc[5] = 0;
raw_state.c_cc[13] = 0;
raw_state.c_cc[10] = 0;
kbdintr_ch = v1.m128i_i8[1];
setvbuf(stdout, outbuffer, 0, 0x80uLL);
LOWORD(ospeed) = cfgetospeed(&cooked_state);
getScreenSize(v4, &v3);
tty_open(v4, &v3);
result = sys_startv();
subshells = 1;
return result;
}

canonical_decompiled_code:
__int64 func11()
{
__m128i v0; // xmm0
__m128i v1; // xmm1
__int64 result; // rax
int v3; // [rsp+8h] [rbp-10h] BYREF
int v4[3]; // [rsp+Ch] [rbp-Ch] BYREF

v4[0] = 0;
v3 = 0;
tcgetattr(0, &cooked_state);
v0 = _mm_load_si128((const __m128i *)&cooked_state);
*(__m128i *)&raw_state.c_iflag = v0;
v1 = _mm_load_si128((const __m128i *)&cooked_state.c_line);
*(__m128i *)&raw_state.c_line = v1;
*(__m128i *)&raw_state.c_cc[15] = _mm_load_si128((const __m128i *)&cooked_state.c_cc[15]);
*(_QWORD *)&raw_state.c_cc[31] = *(_QWORD *)&cooked_state.c_cc[31];
raw_state.c_ospeed = cooked_state.c_ospeed;
raw_state.c_oflag = v0.m128i_i32[1] & 0xFFFFE7FA;
raw_state.c_lflag = v0.m128i_i32[3] & 0xFFFFF181;
raw_state.c_iflag = v0.m128i_i32[0] & 0xFFFFFE3F;
raw_state.c_cc[6] = 1;
raw_state.c_cc[5] = 0;
raw_state.c_cc[13] = 0;
raw_state.c_cc[10] = 0;
kbdintr_ch = v1.m128i_i8[1];
setvbuf(stdout, outbuffer, 0, 0x80uLL);
LOWORD(ospeed) = cfgetospeed(&cooked_state);
func8(v4, &v3);
tty_open(v4, &v3);
result = func10();
subshells = 1;
return result;
}

original_code:
void
sys_init()
{
    unsigned int rows = 0;
    unsigned int columns = 0;
    ((void) tcgetattr(0, (&cooked_state)));
    raw_state = cooked_state;
    raw_state.c_oflag &= ~(0
   | 0000001
   | 0000004
   | 0014000
   | 0014000
    );
    raw_state.c_lflag &= ~(0000002 | 0000010 | 0000020 | 0000040 | 0000100
   | 0000004
   | 0001000
   | 0002000
   | 0004000
    );
    raw_state.c_iflag &= ~(0000400 | 0000200 | 0000100);
    raw_state.c_cc[6] = 1;
    raw_state.c_cc[5] = 0;
     raw_state.c_cc[13] = 0;
 raw_state.c_cc[10] = 0;
    kbdintr_ch = raw_state.c_cc[0];
    (void) setvbuf(stdout, outbuffer, 0, sizeof(outbuffer));
 ospeed = cfgetospeed(&cooked_state);
    getScreenSize(&rows, &columns);
    tty_open(&rows, &columns);
    sys_startv();
    subshells = 1;
}

variable_types:
rows
unsigned int

variable_types:
columns
unsigned int


setpcmark

decompiled_code:
__int64 setpcmark()
{
__int64 v0; // rdi
__int64 result; // rax

v0 = *(_QWORD *)(*curwin + 8LL);
result = curbuf;
*(_QWORD *)(curbuf + 688LL) = *(_QWORD *)*curwin;
*(_QWORD *)(result + 696) = v0;
return result;
}

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

v0 = *(_QWORD *)(*curwin + 8LL);
result = curbuf;
*(_QWORD *)(curbuf + 688LL) = *(_QWORD *)*curwin;
*(_QWORD *)(result + 696) = v0;
return result;
}

original_code:
void
setpcmark()
{
    curbuf->b_pcmark.m_posn = *(curwin->w_cursor);
}


decompiled_code:
_QWORD *move_window_to_cursor()
{
__int64 v0; // rbx
_QWORD *v1; // rbp
int v2; // er12
__int64 v3; // r15
unsigned int v4; // er14
__int64 v5; // r13
__int64 v6; // rax
_QWORD *result; // rax
int v8; // er12
__int64 v9; // rdx
int v10; // eax
_QWORD *v11; // r12
__int64 v12; // r14
__int64 v13; // rax
__int64 v14; // r13
int v15; // eax
unsigned int (__fastcall *v16)(__int64, _QWORD, _QWORD); // rax
unsigned int (__fastcall *v17)(__int64, _QWORD, _QWORD, _QWORD); // rax
unsigned int (__fastcall *v18)(__int64, _QWORD, _QWORD, _QWORD); // rax
unsigned int (__fastcall *v19)(__int64, _QWORD, _QWORD, _QWORD); // rax
__int64 v20; // rax
unsigned int v21; // [rsp+0h] [rbp-48h]
unsigned int v22; // [rsp+4h] [rbp-44h]
unsigned int v23; // [rsp+8h] [rbp-40h]

v0 = curwin;
v1 = *(_QWORD **)*curwin;
v2 = *(_DWORD *)(curwin + 40LL);
v3 = *(_QWORD *)(curwin + 32LL);
v21 = *(_DWORD *)(curwin + 48LL);
v4 = *(_DWORD *)(curwin + 52LL);
v5 = *(_QWORD *)(curwin + 16LL);
if ( (unsigned int)bufempty() )
{
v6 = *(_QWORD *)(*(_QWORD *)(v0 + 8) + 8LL);
*(_QWORD *)(v0 + 16) = v6;
**(_QWORD **)v0 = v6;
result = *(_QWORD **)v0;
*(_DWORD *)(*(_QWORD *)v0 + 8LL) = 0;
return result;
}
v8 = (unsigned int)(v2 - 1) >> 1;
v22 = v4 - 1;
result = (_QWORD *)v1[4];
if ( (unsigned __int64)result < *(_QWORD *)(v5 + 32) )
{
v14 = cntplines(v1, v5);
if ( v8 < v14 )
return (_QWORD *)jump(v1, v8);
v15 = params[205];
if ( v15 == 2 )
return (_QWORD *)jump(v1, v8);
if ( v15 == 1 )
{
v16 = *(unsigned int (__fastcall **)(__int64, _QWORD, _QWORD))(v3 + 168);
if ( v16 )
{
if ( !v16(v3, v21, v22) )
{
v17 = *(unsigned int (__fastcall **)(__int64, _QWORD, _QWORD, _QWORD))(v3 + 168);
if ( v17 )
{
if ( !v17(v3, v21, v4, (unsigned int)-(int)v14) )
return (_QWORD *)jump(v1, v8);
}
else if ( !*(_QWORD *)(v3 + 160) )
{
return (_QWORD *)jump(v1, v8);
}
}
}
else if ( !*(_QWORD *)(v3 + 160) )
{
return (_QWORD *)jump(v1, v8);
}
}
s_ins(0LL, (unsigned int)v14);
*(_QWORD *)(v0 + 16) = v1;
return (_QWORD *)redraw_window(0LL);
}
if ( (unsigned __int64)result < *(_QWORD *)(**(_QWORD **)(v0 + 24) + 32LL) )
return result;
if ( plines(v1) == 1 )
{
result = *(_QWORD **)(*(_QWORD *)(v0 + 24) + 32LL);
if ( v1[4] < (unsigned __int64)result )
return result;
}
result = (_QWORD *)cntplines(v5, v1[1]);
v9 = (unsigned int)(*(_DWORD *)(v0 + 40) - 1);
if ( v9 >= (__int64)result )
return result;
v23 = (_DWORD)result - v9;
if ( v8 < (__int64)result - v9 )
return (_QWORD *)jump(v1, v8);
v10 = params[205];
if ( v10 == 2 )
return (_QWORD *)jump(v1, v8);
if ( v10 != 1 )
goto LABEL_12;
v18 = *(unsigned int (__fastcall **)(__int64, _QWORD, _QWORD, _QWORD))(v3 + 168);
if ( v18 )
{
if ( !v18(v3, v21, v22, v23) )
{
v19 = *(unsigned int (__fastcall **)(__int64, _QWORD, _QWORD, _QWORD))(v3 + 168);
if ( v19 )
{
if ( !v19(v3, v21, v4, v23) )
return (_QWORD *)jump(v1, v8);
}
else if ( !*(_QWORD *)(v3 + 160) )
{
return (_QWORD *)jump(v1, v8);
}
}
}
else if ( !*(_QWORD *)(v3 + 160) )
{
return (_QWORD *)jump(v1, v8);
}
LABEL_12:
v11 = v1;
v12 = 0LL;
while ( 1 )
{
v12 += plines(v11);
v13 = (unsigned int)(*(_DWORD *)(v0 + 40) - 1);
if ( v13 <= v12 || *(_QWORD **)(*(_QWORD *)(v0 + 8) + 8LL) == v11 )
break;
v11 = (_QWORD *)*v11;
}
if ( v11 != v1 && v13 < v12 )
{
do
{
v12 -= plines(v11);
v11 = (_QWORD *)v11[1];
}
while ( (unsigned int)(*(_DWORD *)(v0 + 40) - 1) < v12 && v1 != v11 );
}
if ( *(_QWORD *)(v5 + 32) < v11[4] )
{
v20 = cntplines(v5, v11);
if ( v20 )
s_del(0LL, (unsigned int)v20);
}
*(_QWORD *)(v0 + 16) = v11;
return (_QWORD *)redraw_window(0LL);
}

canonical_decompiled_code:
_QWORD *func16()
{
__int64 v0; // rbx
_QWORD *v1; // rbp
int v2; // er12
__int64 v3; // r15
unsigned int v4; // er14
__int64 v5; // r13
__int64 v6; // rax
_QWORD *result; // rax
int v8; // er12
__int64 v9; // rdx
int v10; // eax
_QWORD *v11; // r12
__int64 v12; // r14
__int64 v13; // rax
__int64 v14; // r13
int v15; // eax
unsigned int (__fastcall *v16)(__int64, _QWORD, _QWORD); // rax
unsigned int (__fastcall *v17)(__int64, _QWORD, _QWORD, _QWORD); // rax
unsigned int (__fastcall *v18)(__int64, _QWORD, _QWORD, _QWORD); // rax
unsigned int (__fastcall *v19)(__int64, _QWORD, _QWORD, _QWORD); // rax
__int64 v20; // rax
unsigned int v21; // [rsp+0h] [rbp-48h]
unsigned int v22; // [rsp+4h] [rbp-44h]
unsigned int v23; // [rsp+8h] [rbp-40h]

v0 = curwin;
v1 = *(_QWORD **)*curwin;
v2 = *(_DWORD *)(curwin + 40LL);
v3 = *(_QWORD *)(curwin + 32LL);
v21 = *(_DWORD *)(curwin + 48LL);
v4 = *(_DWORD *)(curwin + 52LL);
v5 = *(_QWORD *)(curwin + 16LL);
if ( (unsigned int)bufempty() )
{
v6 = *(_QWORD *)(*(_QWORD *)(v0 + 8) + 8LL);
*(_QWORD *)(v0 + 16) = v6;
**(_QWORD **)v0 = v6;
result = *(_QWORD **)v0;
*(_DWORD *)(*(_QWORD *)v0 + 8LL) = 0;
return result;
}
v8 = (unsigned int)(v2 - 1) >> 1;
v22 = v4 - 1;
result = (_QWORD *)v1[4];
if ( (unsigned __int64)result < *(_QWORD *)(v5 + 32) )
{
v14 = cntplines(v1, v5);
if ( v8 < v14 )
return (_QWORD *)func0(v1, v8);
v15 = params[205];
if ( v15 == 2 )
return (_QWORD *)func0(v1, v8);
if ( v15 == 1 )
{
v16 = *(unsigned int (__fastcall **)(__int64, _QWORD, _QWORD))(v3 + 168);
if ( v16 )
{
if ( !v16(v3, v21, v22) )
{
v17 = *(unsigned int (__fastcall **)(__int64, _QWORD, _QWORD, _QWORD))(v3 + 168);
if ( v17 )
{
if ( !v17(v3, v21, v4, (unsigned int)-(int)v14) )
return (_QWORD *)func0(v1, v8);
}
else if ( !*(_QWORD *)(v3 + 160) )
{
return (_QWORD *)func0(v1, v8);
}
}
}
else if ( !*(_QWORD *)(v3 + 160) )
{
return (_QWORD *)func0(v1, v8);
}
}
s_ins(0LL, (unsigned int)v14);
*(_QWORD *)(v0 + 16) = v1;
return (_QWORD *)redraw_window(0LL);
}
if ( (unsigned __int64)result < *(_QWORD *)(**(_QWORD **)(v0 + 24) + 32LL) )
return result;
if ( plines(v1) == 1 )
{
result = *(_QWORD **)(*(_QWORD *)(v0 + 24) + 32LL);
if ( v1[4] < (unsigned __int64)result )
return result;
}
result = (_QWORD *)cntplines(v5, v1[1]);
v9 = (unsigned int)(*(_DWORD *)(v0 + 40) - 1);
if ( v9 >= (__int64)result )
return result;
v23 = (_DWORD)result - v9;
if ( v8 < (__int64)result - v9 )
return (_QWORD *)func0(v1, v8);
v10 = params[205];
if ( v10 == 2 )
return (_QWORD *)func0(v1, v8);
if ( v10 != 1 )
goto LABEL_12;
v18 = *(unsigned int (__fastcall **)(__int64, _QWORD, _QWORD, _QWORD))(v3 + 168);
if ( v18 )
{
if ( !v18(v3, v21, v22, v23) )
{
v19 = *(unsigned int (__fastcall **)(__int64, _QWORD, _QWORD, _QWORD))(v3 + 168);
if ( v19 )
{
if ( !v19(v3, v21, v4, v23) )
return (_QWORD *)func0(v1, v8);
}
else if ( !*(_QWORD *)(v3 + 160) )
{
return (_QWORD *)func0(v1, v8);
}
}
}
else if ( !*(_QWORD *)(v3 + 160) )
{
return (_QWORD *)func0(v1, v8);
}
LABEL_12:
v11 = v1;
v12 = 0LL;
while ( 1 )
{
v12 += plines(v11);
v13 = (unsigned int)(*(_DWORD *)(v0 + 40) - 1);
if ( v13 <= v12 || *(_QWORD **)(*(_QWORD *)(v0 + 8) + 8LL) == v11 )
break;
v11 = (_QWORD *)*v11;
}
if ( v11 != v1 && v13 < v12 )
{
do
{
v12 -= plines(v11);
v11 = (_QWORD *)v11[1];
}
while ( (unsigned int)(*(_DWORD *)(v0 + 40) - 1) < v12 && v1 != v11 );
}
if ( *(_QWORD *)(v5 + 32) < v11[4] )
{
v20 = cntplines(v5, v11);
if ( v20 )
s_del(0LL, (unsigned int)v20);
}
*(_QWORD *)(v0 + 16) = v11;
return (_QWORD *)redraw_window(0LL);
}

original_code:
void
move_window_to_cursor()
{
    register Line *currline;
    int halfwinsize;
    long distance;
    VirtScr *vsp;
    int start_row;
    int end_row;
    Line *topline;
    Xviwin *win = curwin;
    currline = win->w_cursor->p_line;
    halfwinsize = (win->w_nrows - 1) / 2;
    vsp = win->w_vs;
    start_row = win->w_winpos;
    end_row = win->w_cmdline - 1;
    topline = win->w_topline;
    if (bufempty()) {
 win->w_topline = win->w_buffer->b_file;
 win->w_cursor->p_line = win->w_buffer->b_file;
 win->w_cursor->p_index = 0;
    } else if (((currline)->l_number < (topline)->l_number)) {
 long nlines;
 nlines = cntplines(currline, topline);
 if (
     nlines > halfwinsize
     ||
     (params[P_jumpscroll].p_value) == 2
     ||
     (
  (params[P_jumpscroll].p_value) == 1
  &&
  ! ( (vsp->v_can_scroll != ((void *)0)) ? (*(vsp->v_can_scroll))(vsp, start_row, end_row, (int) -nlines) : (vsp->v_scroll != ((void *)0)) )
  &&
  ! ( (vsp->v_can_scroll != ((void *)0)) ? (*(vsp->v_can_scroll))(vsp, start_row, end_row + 1, (int) -nlines) : (vsp->v_scroll != ((void *)0)) )
     )
 ) {
     jump(currline, halfwinsize);
 } else {
     s_ins(0, (int) nlines);
     win->w_topline = currline;
     redraw_window(0);
 }
    } else {
 long nlines;
 if (((currline)->l_number < (win->w_botline->l_prev)->l_number) ||
       (plines(currline) == 1 &&
        ((currline)->l_number < (win->w_botline)->l_number))) {
     return;
 }
 distance = cntplines(topline, currline->l_next);
 if (distance <= win->w_nrows - 1) {
     return;
 }
 nlines = distance - (win->w_nrows - 1);
 if (
     nlines > halfwinsize
     ||
     (params[P_jumpscroll].p_value) == 2
     ||
     (
  (params[P_jumpscroll].p_value) == 1
  &&
  ! ( (vsp->v_can_scroll != ((void *)0)) ? (*(vsp->v_can_scroll))(vsp, start_row, end_row, (int) nlines) : (vsp->v_scroll != ((void *)0)) )
  &&
  ! ( (vsp->v_can_scroll != ((void *)0)) ? (*(vsp->v_can_scroll))(vsp, start_row, end_row + 1, (int) nlines) : (vsp->v_scroll != ((void *)0)) )
     )
 ) {
     jump(currline, halfwinsize);
 } else {
     long done = 0;
     Line *l;
     Line *newtopline;
     for (l = currline;; l = l->l_prev) {
  done += plines(l);
  if (done >= win->w_nrows - 1)
      break;
  if (l == win->w_buffer->b_file)
      break;
     }
     while (done > win->w_nrows - 1 && l != currline) {
  done -= plines(l);
  l = l->l_next;
     }
     newtopline = l;
     if (((topline)->l_number < (newtopline)->l_number)) {
  done = cntplines(topline, newtopline);
  if (done != 0) {
      s_del(0, (int) done);
  }
     }
     win->w_topline = newtopline;
     redraw_window(0);
 }
    }
}

canonical_original_code:
void move_window_to_cursor()
{
    register struct line *currline;
    int halfwinsize;
    long distance;
    struct virtscr *vsp;
    int start_row;
    int end_row;
    struct line *topline;
    struct xviwin *win = curwin;
    currline = win->w_cursor->p_line;
    halfwinsize = (win->w_nrows - 1) / 2;
    vsp = win->w_vs;
    start_row = win->w_winpos;
    end_row = win->w_cmdline - 1;
    topline = win->w_topline;
    if (bufempty()) {
 win->w_topline = win->w_buffer->b_file;
 win->w_cursor->p_line = win->w_buffer->b_file;
 win->w_cursor->p_index = 0;
    } else if (((currline)->l_number < (topline)->l_number)) {
 long nlines;
 nlines = cntplines(currline, topline);
 if (
     nlines > halfwinsize
     ||
     (params[P_jumpscroll].p_value) == 2
     ||
     (
  (params[P_jumpscroll].p_value) == 1
  &&
  ! ( (vsp->v_can_scroll != ((void *)0)) ? (*(vsp->v_can_scroll))(vsp, start_row, end_row, (int) -nlines) : (vsp->v_scroll != ((void *)0)) )
  &&
  ! ( (vsp->v_can_scroll != ((void *)0)) ? (*(vsp->v_can_scroll))(vsp, start_row, end_row + 1, (int) -nlines) : (vsp->v_scroll != ((void *)0)) )
     )
 ) {
     jump(currline, halfwinsize);
 } else {
     s_ins(0, (int) nlines);
     win->w_topline = currline;
     redraw_window(0);
 }
    } else {
 long nlines;
 if (((currline)->l_number < (win->w_botline->l_prev)->l_number) ||
       (plines(currline) == 1 &&
        ((currline)->l_number < (win->w_botline)->l_number))) {
     return;
 }
 distance = cntplines(topline, currline->l_next);
 if (distance <= win->w_nrows - 1) {
     return;
 }
 nlines = distance - (win->w_nrows - 1);
 if (
     nlines > halfwinsize
     ||
     (params[P_jumpscroll].p_value) == 2
     ||
     (
  (params[P_jumpscroll].p_value) == 1
  &&
  ! ( (vsp->v_can_scroll != ((void *)0)) ? (*(vsp->v_can_scroll))(vsp, start_row, end_row, (int) nlines) : (vsp->v_scroll != ((void *)0)) )
  &&
  ! ( (vsp->v_can_scroll != ((void *)0)) ? (*(vsp->v_can_scroll))(vsp, start_row, end_row + 1, (int) nlines) : (vsp->v_scroll != ((void *)0)) )
     )
 ) {
     jump(currline, halfwinsize);
 } else {
     long done = 0;
     struct line *l;
     struct line *newtopline;
     for (l = currline;; l = l->l_prev) {
  done += plines(l);
  if (done >= win->w_nrows - 1)
      break;
  if (l == win->w_buffer->b_file)
      break;
     }
     while (done > win->w_nrows - 1 && l != currline) {
  done -= plines(l);
  l = l->l_next;
     }
     newtopline = l;
     if (((topline)->l_number < (newtopline)->l_number)) {
  done = cntplines(topline, newtopline);
  if (done != 0) {
      s_del(0, (int) done);
  }
     }
     win->w_topline = newtopline;
     redraw_window(0);
 }
    }
}

variable_types: halfwinsize int

variable_types: distance long

variable_types: struct virtscr { void *pv_sys_ptr; unsigned int pv_rows; unsigned int pv_cols; void *pv_window; struct line_struct { char *s_line; unsigned char *s_colour; int s_used; unsigned int s_flags; } *pv_int_lines; struct line_struct *pv_ext_lines; int pv_colours[5]; struct virtscr *(*v_open)(struct virtscr *, void *); void (*v_close)(struct virtscr *); void (*v_clear_all)(struct virtscr *); void (*v_clear_line)(struct virtscr *, int, int); void (*v_goto)(struct virtscr *, int, int); void (*v_write)(struct virtscr *, int, int, char *); void (*v_putc)(struct virtscr *, int, int, int); void (*v_set_colour)(struct virtscr *, int); int (*v_decode_colour)(struct virtscr *, int, char *); void (*v_flush)(struct virtscr *); void (*v_beep)(struct virtscr *); void (*v_insert)(struct virtscr *, int, int, char *); int (*v_scroll)(struct virtscr *, int, int, int); int (*v_can_scroll)(struct virtscr *, int, int, int); } *

variable_types: nlines long

variable_types: done long


decompiled_code:
unsigned __int64 move_cursor_to_window()
{
__int64 v0; // rbx
_QWORD *v1; // rdx
unsigned __int64 v2; // rcx
__int64 v3; // rsi
unsigned __int64 result; // rax
_QWORD *v5; // rdi
unsigned __int64 v6; // rsi

v0 = curwin;
v1 = (_QWORD *)*curwin;
v2 = *(_QWORD *)(*(_QWORD *)*curwin + 32LL);
v3 = *(_QWORD *)(curwin + 16LL);
result = *(_QWORD *)(v3 + 32);
if ( v2 < result )
{
*v1 = v3;
info_update();
result = xvMoveToColumn(*(_DWORD **)v0, *(_DWORD *)(v0 + 92));
}
else
{
v5 = *(_QWORD **)(curwin + 24LL);
v6 = v5[4];
if ( result < v6 && v2 >= v6 )
{
*v1 = *v5;
info_update();
result = xvMoveToColumn(*(_DWORD **)v0, *(_DWORD *)(v0 + 92));
}
}
return result;
}

canonical_decompiled_code:
unsigned __int64 func17()
{
__int64 v0; // rbx
_QWORD *v1; // rdx
unsigned __int64 v2; // rcx
__int64 v3; // rsi
unsigned __int64 result; // rax
_QWORD *v5; // rdi
unsigned __int64 v6; // rsi

v0 = curwin;
v1 = (_QWORD *)*curwin;
v2 = *(_QWORD *)(*(_QWORD *)*curwin + 32LL);
v3 = *(_QWORD *)(curwin + 16LL);
result = *(_QWORD *)(v3 + 32);
if ( v2 < result )
{
*v1 = v3;
info_update();
result = func13(*(_DWORD **)v0, *(_DWORD *)(v0 + 92));
}
else
{
v5 = *(_QWORD **)(curwin + 24LL);
v6 = v5[4];
if ( result < v6 && v2 >= v6 )
{
*v1 = *v5;
info_update();
result = func13(*(_DWORD **)v0, *(_DWORD *)(v0 + 92));
}
}
return result;
}

original_code:
void
move_cursor_to_window()
{
    Xviwin *win = curwin;
    Posn *cp;
    cp = win->w_cursor;
    if (((cp->p_line)->l_number < (win->w_topline)->l_number)) {
 cp->p_line = win->w_topline;
 info_update();
 xvMoveToColumn(win->w_cursor, win->w_curswant);
    } else if (!((cp->p_line)->l_number < (win->w_botline)->l_number)
        && ((win->w_topline)->l_number < (win->w_botline)->l_number)) {
 cp->p_line = win->w_botline->l_prev;
 info_update();
 xvMoveToColumn(win->w_cursor, win->w_curswant);
    }
}

canonical_original_code:
void move_cursor_to_window()
{
    struct xviwin *win = curwin;
    struct position *cp;
    cp = win->w_cursor;
    if (((cp->p_line)->l_number < (win->w_topline)->l_number)) {
 cp->p_line = win->w_topline;
 info_update();
 xvMoveToColumn(win->w_cursor, win->w_curswant);
    } else if (!((cp->p_line)->l_number < (win->w_botline)->l_number)
        && ((win->w_topline)->l_number < (win->w_botline)->l_number)) {
 cp->p_line = win->w_botline->l_prev;
 info_update();
 xvMoveToColumn(win->w_cursor, win->w_curswant);
    }
}

variable_types: struct xviwin { struct position { struct line { struct line *l_prev; struct line *l_next; char *l_text; int l_size; unsigned long l_number; } *p_line; int p_index; } *w_cursor; struct buffer { struct line *b_line0; struct line *b_file; struct line *b_lastline; struct line *b_prevline; char *b_Undotext; char *b_filename; char *b_tempfname; unsigned int b_flags; int b_nwindows; struct mark { struct position m_posn; int m_deleted; } b_mlist[26]; struct mark b_pcmark; struct changedata { unsigned int cd_nlevels; struct cmd { struct position cmd_startpos; struct position cmd_target; unsigned char cmd_flags; int cmd_two_char; int cmd_operator; int cmd_ch1; int cmd_ch2; long cmd_prenum; long cmd_opnum; int cmd_got_yp_name; int cmd_yp_name; int cmd_buffer_name; } *cd_vi_cmd; long cd_total_lines; struct change { struct change *c_next; enum c_type_t { C_LINE=0, C_CHAR=1, C_DEL_CHAR=2, C_POSITION=3 } c_type; unsigned long c_lineno; union { struct { long cul_nlines; struct line *cul_lines; } cu_l; struct { int cuc_index; int cuc_nchars; char *cuc_chars; } cu_c; struct { long cup_line; int cup_index; } cu_p; } c_u; } *cd_undo; struct change *cd_redo; } *b_change; } *w_buffer; struct line *w_topline; struct line *w_botline; struct virtscr { void *pv_sys_ptr; unsigned int pv_rows; unsigned int pv_cols; void *pv_window; struct line_struct { char *s_line; unsigned char *s_colour; int s_used; unsigned int s_flags; } *pv_int_lines; struct line_struct *pv_ext_lines; int pv_colours[5]; struct virtscr *(*v_open)(struct virtscr *, void *); void (*v_close)(struct virtscr *); void (*v_clear_all)(struct virtscr *); void (*v_clear_line)(struct virtscr *, int, int); void (*v_goto)(struct virtscr *, int, int); void (*v_write)(struct virtscr *, int, int, char *); void (*v_putc)(struct virtscr *, int, int, int); void (*v_set_colour)(struct virtscr *, int); int (*v_decode_colour)(struct virtscr *, int, char *); void (*v_flush)(struct virtscr *); void (*v_beep)(struct virtscr *); void (*v_insert)(struct virtscr *, int, int, char *); int (*v_scroll)(struct virtscr *, int, int, int); int (*v_can_scroll)(struct virtscr *, int, int, int); } *w_vs; unsigned int w_nrows; unsigned int w_ncols; unsigned int w_winpos; unsigned int w_cmdline; struct Flexbuf { char *fxb_chars; unsigned int fxb_max; unsigned int fxb_rcnt; unsigned int fxb_wcnt; } w_statusline; int w_row; int w_virtcol; int w_curswant; int w_set_want_col; int w_c_line_size; struct xviwin *w_last; struct xviwin *w_next; struct cmd *w_cmd; } *


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

result = curmap;
if ( curmap )
{
dword_A90 = 0;
dword_A8C = 0;
lformat(&buf_0, "%-18.18s %-s", *(_QWORD *)(curmap + 8));
curmap = *(_QWORD *)curmap;
result = flexgetstr(&buf_0);
}
return result;
}

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

result = curmap;
if ( curmap )
{
dword_A90 = 0;
dword_A8C = 0;
lformat(&buf_0, "%-18.18s %-s", *(_QWORD *)(curmap + 8));
curmap = *(_QWORD *)curmap;
result = flexgetstr(&buf_0);
}
return result;
}

original_code:
static char *
show_map()
{
    static Flexbuf buf;
    if (curmap == ((void *)0)) {
 return(((void *)0));
    }
    ((&buf)->fxb_rcnt = (&buf)->fxb_wcnt = 0);
    (void) lformat(&buf, "%-18.18s %-s", curmap->m_lhs, curmap->m_rhs);
    curmap = curmap->m_next;
    return flexgetstr(&buf);
}

canonical_original_code:
static char *show_map()
{
    static struct Flexbuf buf;
    if (curmap == ((void *)0)) {
 return(((void *)0));
    }
    ((&buf)->fxb_rcnt = (&buf)->fxb_wcnt = 0);
    (void) lformat(&buf, "%-18.18s %-s", curmap->m_lhs, curmap->m_rhs);
    curmap = curmap->m_next;
    return flexgetstr(&buf);
}

variable_types: struct Flexbuf { char *fxb_chars; unsigned int fxb_max; unsigned int fxb_rcnt; unsigned int fxb_wcnt; }


stuff

decompiled_code:
__int64 stuff(__int64 a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
return vformat(off_A78, a1, va);
}

canonical_decompiled_code:
__int64 func8(__int64 a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
return vformat(off_A78, a1, va);
}

original_code:
void
stuff
    (char *format, ...)
{
    va_list argp;
    __builtin_va_start(argp,format);
    (void) vformat(npos.mp_dest, format, argp);
    __builtin_va_end(argp);
}

canonical_original_code:
void stuff(char *format, ...)
{
    __builtin_va_list argp;
    __builtin_va_start(argp,format);
    (void) vformat(npos.mp_dest, format, argp);
    __builtin_va_end(argp);
}

variable_types:
format
char *


unstuff

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

*((_DWORD *)off_A78 + 4) = 0;
*((_DWORD *)off_A78 + 3) = 0;
*((_DWORD *)off_A70 + 4) = 0;
result = off_A70;
*((_DWORD *)off_A70 + 3) = 0;
return result;
}

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

*((_DWORD *)off_A78 + 4) = 0;
*((_DWORD *)off_A78 + 3) = 0;
*((_DWORD *)off_A70 + 4) = 0;
result = off_A70;
*((_DWORD *)off_A70 + 3) = 0;
return result;
}

original_code:
void
unstuff()
{
 ((npos.mp_dest)->fxb_rcnt = (npos.mp_dest)->fxb_wcnt = 0);
 ((npos.mp_src)->fxb_rcnt = (npos.mp_src)->fxb_wcnt = 0);
}


decompiled_code:
__int64 map_getc()
{
void *v0; // rdi
unsigned int v1; // ecx
unsigned int v2; // edx
__int64 v4; // rsi

v0 = off_A78;
v1 = *((_DWORD *)off_A78 + 3);
v2 = *((_DWORD *)off_A78 + 4);
if ( v1 < v2 )
return flexpopch(v0);
while ( 1 )
{
if ( kbdintr )
{
if ( !literal_next )
{
dword_AF0 = 0;
dword_AEC = 0;
imessage = 1;
}
return (unsigned int)kbdintr_ch;
}
if ( *((_DWORD *)off_A70 + 3) >= *((_DWORD *)off_A70 + 4) )
break;
if ( literal_next )
return flexpopch(off_A70);
v4 = cmd_map;
if ( State )
{
v4 = 0LL;
if ( (unsigned int)(State - 2) < 3 )
v4 = ins_map;
}
mapthrough((__int64)&npos, v4);
LABEL_8:
v0 = off_A78;
v1 = *((_DWORD *)off_A78 + 3);
v2 = *((_DWORD *)off_A78 + 4);
if ( v1 < v2 )
return flexpopch(v0);
}
if ( *((_DWORD *)off_A50 + 3) < *((_DWORD *)off_A50 + 4) )
{
mapthrough((__int64)&kpos, key_map);
goto LABEL_8;
}
if ( v1 < v2 )
return flexpopch(v0);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func12()
{
void *v0; // rdi
unsigned int v1; // ecx
unsigned int v2; // edx
__int64 v4; // rsi

v0 = off_A78;
v1 = *((_DWORD *)off_A78 + 3);
v2 = *((_DWORD *)off_A78 + 4);
if ( v1 < v2 )
return flexpopch(v0);
while ( 1 )
{
if ( kbdintr )
{
if ( !literal_next )
{
dword_AF0 = 0;
dword_AEC = 0;
imessage = 1;
}
return (unsigned int)kbdintr_ch;
}
if ( *((_DWORD *)off_A70 + 3) >= *((_DWORD *)off_A70 + 4) )
break;
if ( literal_next )
return flexpopch(off_A70);
v4 = cmd_map;
if ( State )
{
v4 = 0LL;
if ( (unsigned int)(State - 2) < 3 )
v4 = ins_map;
}
func3((__int64)&npos, v4);
LABEL_8:
v0 = off_A78;
v1 = *((_DWORD *)off_A78 + 3);
v2 = *((_DWORD *)off_A78 + 4);
if ( v1 < v2 )
return flexpopch(v0);
}
if ( *((_DWORD *)off_A50 + 3) < *((_DWORD *)off_A50 + 4) )
{
func3((__int64)&kpos, key_map);
goto LABEL_8;
}
if ( v1 < v2 )
return flexpopch(v0);
return 0xFFFFFFFFLL;
}

original_code:
int
map_getc()
{
    while (((npos.mp_dest)->fxb_rcnt >= (npos.mp_dest)->fxb_wcnt)) {
 if (kbdintr) {
     if (!literal_next) {
  ((&canon_queue)->fxb_rcnt = (&canon_queue)->fxb_wcnt = 0);
  imessage = 1;
     } else {
     }
     return(kbdintr_ch);
 }
 if (!((npos.mp_src)->fxb_rcnt >= (npos.mp_src)->fxb_wcnt)) {
     if (literal_next) {
  return(flexpopch(npos.mp_src));
     }
     mapthrough(&npos,
   (State == NORMAL) ? cmd_map :
   (State == INSERT ||
    State == REPLACE ||
    State == CMDLINE) ? ins_map :
   ((void *)0));
 } else if (!((kpos.mp_src)->fxb_rcnt >= (kpos.mp_src)->fxb_wcnt)) {
     mapthrough(&kpos, key_map);
 } else {
     break;
 }
    }
    return(((npos.mp_dest)->fxb_rcnt >= (npos.mp_dest)->fxb_wcnt) ? (-1) : flexpopch(npos.mp_dest));
}


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

if ( kpos )
result = map_failed((__int64)&kpos);
else
result = map_failed((__int64)&npos);
return result;
}

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

if ( kpos )
result = func1((__int64)&kpos);
else
result = func1((__int64)&npos);
return result;
}

original_code:
void
map_timeout()
{
    if (kpos.mp_map != ((void *)0)) {
 map_failed(&kpos);
    } else {
 map_failed(&npos);
    }
}


decompiled_code:
_BOOL8 map_waiting()
{
_BOOL8 result; // rax

result = 1LL;
if ( !kpos )
result = npos != 0;
return result;
}

canonical_decompiled_code:
_BOOL8 func14()
{
_BOOL8 result; // rax

result = 1LL;
if ( !kpos )
result = npos != 0;
return result;
}

original_code:
bool_t
map_waiting()
{
    return(kpos.mp_map != ((void *)0) || npos.mp_map != ((void *)0));
}

canonical_original_code:
int map_waiting()
{
    return(kpos.mp_map != ((void *)0) || npos.mp_map != ((void *)0));
}


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

result = curwin;
*(_DWORD *)(curwin + 72LL) = 0;
*(_DWORD *)(result + 68) = 0;
return result;
}

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

result = curwin;
*(_DWORD *)(curwin + 72LL) = 0;
*(_DWORD *)(result + 68) = 0;
return result;
}

original_code:
void
init_sline()
{
    ((&curwin->w_statusline)->fxb_rcnt = (&curwin->w_statusline)->fxb_wcnt = 0);
}


decompiled_code:
__int64 show_message(__int64 a1, ...)
{
__int64 v2; // rdi
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
v2 = curwin;
*(_DWORD *)(curwin + 72LL) = 0;
*(_DWORD *)(v2 + 68) = 0;
vformat(v2 + 56, a1, va);
return update_sline();
}

canonical_decompiled_code:
__int64 func1(__int64 a1, ...)
{
__int64 v2; // rdi
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
v2 = curwin;
*(_DWORD *)(curwin + 72LL) = 0;
*(_DWORD *)(v2 + 68) = 0;
vformat(v2 + 56, a1, va);
return update_sline();
}

original_code:
void
show_message
    (char *format, ...)
{
    va_list argp;
    __builtin_va_start(argp,format);
    ((&curwin->w_statusline)->fxb_rcnt = (&curwin->w_statusline)->fxb_wcnt = 0);
    (void) vformat(&curwin->w_statusline, format, argp);
    __builtin_va_end(argp);
    update_sline();
}

canonical_original_code:
void show_message(char *format, ...)
{
    __builtin_va_list argp;
    __builtin_va_start(argp,format);
    ((&curwin->w_statusline)->fxb_rcnt = (&curwin->w_statusline)->fxb_wcnt = 0);
    (void) vformat(&curwin->w_statusline, format, argp);
    __builtin_va_end(argp);
    update_sline();
}


decompiled_code:
__int64 show_error(__int64 a1, ...)
{
__int64 v2; // rdi
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

if ( params[125] )
beep();
va_start(va, a1);
v2 = curwin;
*(_DWORD *)(curwin + 72LL) = 0;
*(_DWORD *)(v2 + 68) = 0;
vformat(v2 + 56, a1, va);
return update_sline();
}

canonical_decompiled_code:
__int64 func2(__int64 a1, ...)
{
__int64 v2; // rdi
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

if ( params[125] )
beep();
va_start(va, a1);
v2 = curwin;
*(_DWORD *)(curwin + 72LL) = 0;
*(_DWORD *)(v2 + 68) = 0;
vformat(v2 + 56, a1, va);
return update_sline();
}

original_code:
void
show_error
    (char *format, ...)
{
    va_list argp;
    if ((params[P_errorbells].p_value)) {
 beep();
    }
    __builtin_va_start(argp,format);
    ((&curwin->w_statusline)->fxb_rcnt = (&curwin->w_statusline)->fxb_wcnt = 0);
    (void) vformat(&curwin->w_statusline, format, argp);
    __builtin_va_end(argp);
    update_sline();
}

canonical_original_code:
void show_error(char *format, ...)
{
    __builtin_va_list argp;
    if ((params[P_errorbells].p_value)) {
 beep();
    }
    __builtin_va_start(argp,format);
    ((&curwin->w_statusline)->fxb_rcnt = (&curwin->w_statusline)->fxb_wcnt = 0);
    (void) vformat(&curwin->w_statusline, format, argp);
    __builtin_va_end(argp);
    update_sline();
}


decompiled_code:
__int64 info_update()
{
return show_file_info(params[195] == 1);
}

canonical_decompiled_code:
__int64 func4()
{
return func3(params[195] == 1);
}

original_code:
void
info_update()
{
    show_file_info((params[P_infoupdate].p_value) == 1);
}


WarnUnSaved

decompiled_code:
void WarnUnSaved()
{
if ( params[635] )
{
if ( (unsigned int)xvChangesNotSaved() )
{
fwrite("[No write since last change]\r\n", 1uLL, 0x1EuLL, stdout);
fflush(stdout);
}
}
}

canonical_decompiled_code:
void func0()
{
if ( params[635] )
{
if ( (unsigned int)xvChangesNotSaved() )
{
fwrite("[No write since last change]\r\n", 1uLL, 0x1EuLL, stdout);
fflush(stdout);
}
}
}

original_code:
static void
WarnUnSaved()
{
    if ((params[P_warn].p_value) && xvChangesNotSaved()) {
 fputs("[No write since last change]\r\n", stdout);
 fflush(stdout);
    }
}


exHelp

decompiled_code:
__int64 exHelp()
{
int v0; // ebx
__int64 result; // rax
__int64 v2; // rdi
__int64 v3; // rax

v0 = (int)echo;
result = (unsigned int)echo & 0xFFFFFFF1;
LODWORD(echo) = (unsigned int)echo & 0xFFFFFFF1;
v2 = params[89];
if ( v2 )
{
v3 = fexpand(v2, 0LL);
result = exNewBuffer(v3, 0LL);
if ( (_DWORD)result )
{
*(_DWORD *)(curbuf + 56LL) |= 2u;
move_window_to_cursor();
show_file_info(1LL);
result = redraw_window(0LL);
}
}
LODWORD(echo) = v0;
return result;
}

canonical_decompiled_code:
__int64 func5()
{
int v0; // ebx
__int64 result; // rax
__int64 v2; // rdi
__int64 v3; // rax

v0 = (int)echo;
result = (unsigned int)echo & 0xFFFFFFF1;
LODWORD(echo) = (unsigned int)echo & 0xFFFFFFF1;
v2 = params[89];
if ( v2 )
{
v3 = fexpand(v2, 0LL);
result = exNewBuffer(v3, 0LL);
if ( (_DWORD)result )
{
*(_DWORD *)(curbuf + 56LL) |= 2u;
move_window_to_cursor();
show_file_info(1LL);
result = redraw_window(0LL);
}
}
LODWORD(echo) = v0;
return result;
}

original_code:
void
exHelp()
{
    unsigned savecho;
    savecho = echo;
    echo &= ~(2 | 4 | 010);
    if ((params[P_helpfile].pu.pu_str) != ((void *)0) &&
      exNewBuffer(fexpand((params[P_helpfile].pu.pu_str), 0), 0)) {
 curbuf->b_flags |= 0x2;
 move_window_to_cursor();
 show_file_info(1);
 redraw_window(0);
    }
    echo = savecho;
}

canonical_original_code:
void exHelp()
{
    unsigned int savecho;
    savecho = echo;
    echo &= ~(2 | 4 | 010);
    if ((params[P_helpfile].pu.pu_str) != ((void *)0) &&
      exNewBuffer(fexpand((params[P_helpfile].pu.pu_str), 0), 0)) {
 curbuf->b_flags |= 0x2;
 move_window_to_cursor();
 show_file_info(1);
 redraw_window(0);
    }
    echo = savecho;
}

variable_types:
savecho
unsigned int


decompiled_code:
__int64 alt_file_name()
{
__int64 result; // rax
__int64 v1; // rax

result = 0LL;
if ( n_elements )
{
v1 = top - 1;
if ( top <= 0 )
v1 = 31LL;
result = stack[2 * v1];
}
return result;
}

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

result = 0LL;
if ( n_elements )
{
v1 = top - 1;
if ( top <= 0 )
v1 = 31LL;
result = stack[2 * v1];
}
return result;
}

original_code:
char *
alt_file_name()
{
    if ((!n_elements)) {
 return(((void *)0));
    } else {
 return(stack[(top > 0 ? top - 1 : (sizeof(stack) / sizeof(stack[0])) - 1)].filename);
    }
}

canonical_original_code:
char *alt_file_name()
{
    if ((!n_elements)) {
 return(((void *)0));
    } else {
 return(stack[(top > 0 ? top - 1 : (sizeof(stack) / sizeof(stack[0])) - 1)].filename);
    }
}


xvClear

decompiled_code:
int xvClear()
{
__int64 v0; // rbx
int v1; // ebp
int result; // eax
unsigned int i; // ebx

v0 = *(_QWORD *)(curwin + 32LL);
v1 = *(_DWORD *)(v0 + 8);
(*(void (__fastcall **)(__int64, _QWORD))(v0 + 120))(v0, *(unsigned int *)(v0 + 40));
result = (*(__int64 (__fastcall **)(__int64))(v0 + 80))(v0);
if ( v1 )
{
for ( i = 0; i != v1; ++i )
result = xvClearLine(i);
}
return result;
}

canonical_decompiled_code:
int func5()
{
__int64 v0; // rbx
int v1; // ebp
int result; // eax
unsigned int i; // ebx

v0 = *(_QWORD *)(curwin + 32LL);
v1 = *(_DWORD *)(v0 + 8);
(*(void (__fastcall **)(__int64, _QWORD))(v0 + 120))(v0, *(unsigned int *)(v0 + 40));
result = (*(__int64 (__fastcall **)(__int64))(v0 + 80))(v0);
if ( v1 )
{
for ( i = 0; i != v1; ++i )
result = func4(i);
}
return result;
}

original_code:
void
xvClear()
{
    VirtScr *vs = curwin->w_vs;
    register unsigned row;
    register unsigned nrows;
    nrows = (vs->pv_rows);
    ((*((vs)->v_set_colour))(vs, (vs)->pv_colours[0]));
    ((*(vs->v_clear_all))(vs));
    for (row = 0; row < nrows; row++) {
 xvClearLine(row);
    }
}

canonical_original_code:
void xvClear()
{
    struct virtscr *vs = curwin->w_vs;
    register unsigned int row;
    register unsigned int nrows;
    nrows = (vs->pv_rows);
    ((*((vs)->v_set_colour))(vs, (vs)->pv_colours[0]));
    ((*(vs->v_clear_all))(vs));
    for (row = 0; row < nrows; row++) {
 xvClearLine(row);
    }
}

variable_types:
row
unsigned int

variable_types:
nrows
unsigned int


initline

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

result = newline(81LL);
if ( !result )
result = newline(1LL);
return result;
}

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

result = newline(81LL);
if ( !result )
result = newline(1LL);
return result;
}

original_code:
static Line *
initline()
{
    Line *lp;
    if ((lp = newline(81)) == ((void *)0)) {
 lp = newline(1);
    }
    return lp;
}

canonical_original_code:
static struct line *initline()
{
    struct line *lp;
    if ((lp = newline(81)) == ((void *)0)) {
 lp = newline(1);
    }
    return lp;
}

variable_types: struct line { struct line *l_prev; struct line *l_next; char *l_text; int l_size; unsigned long l_number; } *


decompiled_code:
__sighandler_t catch_signals()
{
setsigcatcher(2, (void (*)(int))int_handler);
setsigcatcher(20, (void (*)(int))tstp_handler);
setsigcatcher(1, (void (*)(int))hup_handler);
return setsigcatcher(15, (void (*)(int))term_handler);
}

canonical_decompiled_code:
__sighandler_t func5()
{
func3(2, (void (*)(int))int_handler);
func3(20, (void (*)(int))tstp_handler);
func3(1, (void (*)(int))hup_handler);
return func3(15, (void (*)(int))term_handler);
}

original_code:
void
catch_signals()
{
    setsigcatcher(2, int_handler);
    setsigcatcher(20, tstp_handler);
    setsigcatcher(1, hup_handler);
    setsigcatcher(15, term_handler);
}


ralloc

decompiled_code:
void *ralloc()
{
void *result; // rax
int v1; // ebx
int v2; // eax
void *v3; // rdi

result = (void *)reuselist;
if ( reuselist )
{
reuselist = *(_QWORD *)reuselist;
}
else
{
v1 = (int)echo;
v2 = (int)echo;
LOBYTE(v2) = (unsigned __int8)echo & 0x7F;
LODWORD(echo) = v2;
v3 = alloc(0x280uLL);
if ( v3 )
usemem((__int64)v3, 0x280u);
LODWORD(echo) = v1;
result = (void *)reuselist;
if ( reuselist )
reuselist = *(_QWORD *)reuselist;
else
result = alloc(0x28uLL);
}
return result;
}

canonical_decompiled_code:
void *func3()
{
void *result; // rax
int v1; // ebx
int v2; // eax
void *v3; // rdi

result = (void *)reuselist;
if ( reuselist )
{
reuselist = *(_QWORD *)reuselist;
}
else
{
v1 = (int)echo;
v2 = (int)echo;
LOBYTE(v2) = (unsigned __int8)echo & 0x7F;
LODWORD(echo) = v2;
v3 = func2(0x280uLL);
if ( v3 )
func0((__int64)v3, 0x280u);
LODWORD(echo) = v1;
result = (void *)reuselist;
if ( reuselist )
reuselist = *(_QWORD *)reuselist;
else
result = func2(0x28uLL);
}
return result;
}

original_code:
static Reusable *
ralloc()
{
    Reusable *p;
    if (reuselist == ((void *)0)) {
 unsigned savecho = echo;
 echo &= ~0200;
 if ((p = alloc((16 * sizeof (Reusable)))) != ((void *)0)) {
     usemem((char *) p, (16 * sizeof (Reusable)));
 }
 echo = savecho;
    }
    if (reuselist) {
 p = reuselist;
 reuselist = p->ru_next;
 return p;
    }
    return alloc(sizeof(Reusable));
}

canonical_original_code:
static union reusable *ralloc()
{
    union reusable *p;
    if (reuselist == ((void *)0)) {
 unsigned int savecho = echo;
 echo &= ~0200;
 if ((p = alloc((16 * sizeof (Reusable)))) != ((void *)0)) {
     usemem((char *) p, (16 * sizeof (Reusable)));
 }
 echo = savecho;
    }
    if (reuselist) {
 p = reuselist;
 reuselist = p->ru_next;
 return p;
    }
    return alloc(sizeof(Reusable));
}

variable_types: union reusable { struct line { struct line *l_prev; struct line *l_next; char *l_text; int l_size; unsigned long l_number; } ru_l; struct change { struct change *c_next; enum c_type_t { C_LINE=0, C_CHAR=1, C_DEL_CHAR=2, C_POSITION=3 } c_type; unsigned long c_lineno; union { struct { long cul_nlines; struct line *cul_lines; } cu_l; struct { int cuc_index; int cuc_nchars; char *cuc_chars; } cu_c; struct { long cup_line; int cup_index; } cu_p; } c_u; } ru_c; union reusable *ru_next; } *

user_defined_types: union reusable { struct line ru_l; struct change ru_c; union reusable *ru_next; }


challoc

decompiled_code:
void *challoc()
{
return ralloc();
}

canonical_decompiled_code:
void *func4()
{
return func3();
}

original_code:
Change *
challoc()
{
    return (Change *) ralloc();
}

canonical_original_code:
struct change *challoc()
{
    return (struct change *) ralloc();
}

return_type: struct change { struct change *c_next; enum c_type_t { C_LINE=0, C_CHAR=1, C_DEL_CHAR=2, C_POSITION=3 } c_type; unsigned long c_lineno; union { struct { long cul_nlines; struct line { struct line *l_prev; struct line *l_next; char *l_text; int l_size; unsigned long l_number; } *cul_lines; } cu_l; struct { int cuc_index; int cuc_nchars; char *cuc_chars; } cu_c; struct { long cup_line; int cup_index; } cu_p; } c_u; } *


bufempty

original_code:
bool_t
bufempty()
{
    return(curbuf->b_file->l_next == curbuf->b_lastline &&
    curbuf->b_file->l_text[0] == '\0');
}

canonical_original_code:
int bufempty()
{
    return(curbuf->b_file->l_next == curbuf->b_lastline &&
    curbuf->b_file->l_text[0] == '\0');
}