main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
float v3; // xmm7_4
float v4; // xmm3_4
float v5; // xmm5_4
float v6; // xmm3_4
float v7; // xmm5_4
unsigned int *v8; // r13
float v9; // xmm2_4
float v10; // xmm1_4
__int64 v11; // r14
__syscall_slong_t v12; // r15
FILE *v13; // r12
unsigned int v14; // ebp
double v15; // xmm0_8
float v16; // xmm3_4
double v18; // [rsp+0h] [rbp-98h]
double v19; // [rsp+0h] [rbp-98h]
double v20; // [rsp+8h] [rbp-90h]
float v21; // [rsp+10h] [rbp-88h]
float v22; // [rsp+14h] [rbp-84h]
float v23; // [rsp+18h] [rbp-80h]
float v24; // [rsp+1Ch] [rbp-7Ch]
float v25; // [rsp+20h] [rbp-78h]
int v26; // [rsp+24h] [rbp-74h]
double v27; // [rsp+28h] [rbp-70h]
__int64 v28; // [rsp+38h] [rbp-60h] BYREF
char s[10]; // [rsp+46h] [rbp-52h] BYREF
struct timespec tp; // [rsp+50h] [rbp-48h] BYREF

if ( argc != 9 )
{
fwrite("[application] usage:\n", 1uLL, 0x15uLL, stderr);
fwrite("<a> sl a_cpu b_cpu a_mem b_mem eps weight deadline\n", 1uLL, 0x33uLL, stderr);
exit(-1);
}
v3 = atof(argv[1]);
v21 = v3;
v4 = atof(argv[2]);
v22 = v4;
v5 = atof(argv[3]);
v24 = v5;
v6 = atof(argv[4]);
v23 = v6;
v7 = atof(argv[5]);
v25 = v7;
v27 = atof(argv[6]);
v20 = atof(argv[7]);
v18 = atof(argv[8]);
v8 = (unsigned int *)jobsignaler_registration();
v28 = (unsigned int)(int)(v18 * 1000000000.0);
jobsignaler_set(v8, 1LL, &v28);
set_weight(*v8, v20);
v26 = 100;
v19 = v3;
do
{
v14 = jobsignaler_signalstart(v8, 0LL);
v15 = get_performance_number(v8, 0LL);
if ( v15 == 0.0 )
{
v9 = v22 * v21;
v10 = v23 * v21;
}
else
{
v16 = v19 * v15 * v27 + v19;
v21 = v16;
v19 = v16;
if ( v16 >= 0.0001 )
{
v9 = v22 * v16;
v10 = v23 * v16;
}
else
{
v21 = 0.000099999997;
v9 = 0.000099999997 * v22;
v19 = 0.000099999997;
v10 = 0.000099999997 * v23;
}
}
v11 = (unsigned int)(int)(float)(v9 + v24);
clock_gettime(0, &tp);
v12 = tp.tv_nsec + 1000000000 * tp.tv_sec;
sprintf(s, "%u.log", *v8);
v13 = fopen(s, "a+");
fprintf(v13, "%lld, %f, %f, %lld, %lld, %u\n", v12, v15, v19, v11, (unsigned int)(int)(float)(v10 + v25), v14);
fclose(v13);
do_work(v11, 0.0);
jobsignaler_signalend(v8, v14);
--v26;
}
while ( v26 );
jobsignaler_terminate(v8);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
float v3; // xmm7_4
float v4; // xmm3_4
float v5; // xmm5_4
float v6; // xmm3_4
float v7; // xmm5_4
unsigned int *v8; // r13
float v9; // xmm2_4
float v10; // xmm1_4
__int64 v11; // r14
__syscall_slong_t v12; // r15
FILE *v13; // r12
unsigned int v14; // ebp
double v15; // xmm0_8
float v16; // xmm3_4
double v18; // [rsp+0h] [rbp-98h]
double v19; // [rsp+0h] [rbp-98h]
double v20; // [rsp+8h] [rbp-90h]
float v21; // [rsp+10h] [rbp-88h]
float v22; // [rsp+14h] [rbp-84h]
float v23; // [rsp+18h] [rbp-80h]
float v24; // [rsp+1Ch] [rbp-7Ch]
float v25; // [rsp+20h] [rbp-78h]
int v26; // [rsp+24h] [rbp-74h]
double v27; // [rsp+28h] [rbp-70h]
__int64 v28; // [rsp+38h] [rbp-60h] BYREF
char s[10]; // [rsp+46h] [rbp-52h] BYREF
struct timespec tp; // [rsp+50h] [rbp-48h] BYREF

if ( argc != 9 )
{
fwrite("[application] usage:\n", 1uLL, 0x15uLL, stderr);
fwrite("<a> sl a_cpu b_cpu a_mem b_mem eps weight deadline\n", 1uLL, 0x33uLL, stderr);
exit(-1);
}
v3 = atof(argv[1]);
v21 = v3;
v4 = atof(argv[2]);
v22 = v4;
v5 = atof(argv[3]);
v24 = v5;
v6 = atof(argv[4]);
v23 = v6;
v7 = atof(argv[5]);
v25 = v7;
v27 = atof(argv[6]);
v20 = atof(argv[7]);
v18 = atof(argv[8]);
v8 = (unsigned int *)jobsignaler_registration();
v28 = (unsigned int)(int)(v18 * 1000000000.0);
jobsignaler_set(v8, 1LL, &v28);
set_weight(*v8, v20);
v26 = 100;
v19 = v3;
do
{
v14 = jobsignaler_signalstart(v8, 0LL);
v15 = get_performance_number(v8, 0LL);
if ( v15 == 0.0 )
{
v9 = v22 * v21;
v10 = v23 * v21;
}
else
{
v16 = v19 * v15 * v27 + v19;
v21 = v16;
v19 = v16;
if ( v16 >= 0.0001 )
{
v9 = v22 * v16;
v10 = v23 * v16;
}
else
{
v21 = 0.000099999997;
v9 = 0.000099999997 * v22;
v19 = 0.000099999997;
v10 = 0.000099999997 * v23;
}
}
v11 = (unsigned int)(int)(float)(v9 + v24);
clock_gettime(0, &tp);
v12 = tp.tv_nsec + 1000000000 * tp.tv_sec;
sprintf(s, "%u.log", *v8);
v13 = fopen(s, "a+");
fprintf(v13, "%lld, %f, %f, %lld, %lld, %u\n", v12, v15, v19, v11, (unsigned int)(int)(float)(v10 + v25), v14);
fclose(v13);
func1(v11, 0.0);
jobsignaler_signalend(v8, v14);
--v26;
}
while ( v26 );
jobsignaler_terminate(v8);
return 0;
}

original_code:
int main(int argc, char* argv[]) {
  if (argc != 3) {
    exit(-1);
  }
  double c1 = atof(argv[1]);
  double c2 = atof(argv[2]);
  gtrm_init();
  _rm_application_h apps[100];
  int i = 0;
  int prev_num_apps = 0;
  while(1) {
    int num_apps = update_applications(apps);
    if (num_apps > 0) {
      compute_virtual_platforms(apps, num_apps, i, num_apps != prev_num_apps, c1, c2);
      rescale_virtual_platforms(apps, num_apps);
      rmset_performance_multiplier(apps, num_apps);
      write_log(apps, num_apps);
      apply_virtual_platforms(apps, num_apps);
      prev_num_apps = num_apps;
      ++i;
    }
  }
  ({ fprintf(stderr, "Fatal: "); fprintf(stderr, "unreachable"); fprintf(stderr, "\n"); exit(1); });;
}

canonical_original_code:
int main(int argc, char *argv[]) {
  if (argc != 3) {
    exit(-1);
  }
  double c1 = atof(argv[1]);
  double c2 = atof(argv[2]);
  gtrm_init();
  struct _rm_application_h apps[100];
  int i = 0;
  int prev_num_apps = 0;
  while(1) {
    int num_apps = update_applications(apps);
    if (num_apps > 0) {
      compute_virtual_platforms(apps, num_apps, i, num_apps != prev_num_apps, c1, c2);
      rescale_virtual_platforms(apps, num_apps);
      rmset_performance_multiplier(apps, num_apps);
      write_log(apps, num_apps);
      apply_virtual_platforms(apps, num_apps);
      prev_num_apps = num_apps;
      ++i;
    }
  }
  ({ fprintf(stderr, "Fatal: "); fprintf(stderr, "unreachable"); fprintf(stderr, "\n"); exit(1); });;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct _rm_application_h { int tid; float vp; float vp_old; float performance; float weight; } [100]

variable_types: i int


repo: martinamaggio/gtrm

decompiled_code:
int __fastcall cg_destroy(const char *a1)
{
int result; // eax
char v2[4104]; // [rsp+0h] [rbp-1008h] BYREF

if ( !a1 )
_assert_fail("group != NULL", "src/cg.c", 0x36u, "cg_destroy");
cg_detach_all(a1);
snprintf(v2, 0x1000uLL, "/sys/fs/cgroup/%s", a1);
result = rmdir(v2);
if ( result )
_assert_fail("retval == 0", "src/cg.c", 0x3Eu, "cg_destroy");
return result;
}

canonical_decompiled_code:
int __fastcall func5(const char *a1)
{
int result; // eax
char v2[4104]; // [rsp+0h] [rbp-1008h] BYREF

if ( !a1 )
_assert_fail("group != NULL", "src/cg.c", 0x36u, "cg_destroy");
func4(a1);
snprintf(v2, 0x1000uLL, "/sys/fs/cgroup/%s", a1);
result = rmdir(v2);
if ( result )
_assert_fail("retval == 0", "src/cg.c", 0x3Eu, "cg_destroy");
return result;
}

original_code:
void cg_destroy(const char *group)
{
	assert(group != NULL);

	cg_detach_all(group);

	char cg[PATH_SIZE_MAX];
	snprintf(cg, PATH_SIZE_MAX, MOUNT_POINT "/%s", group);

	int retval = rmdir(cg);
	assert(retval == 0);
}

canonical_original_code:
void cg_destroy(char *group)
{
	assert(group != NULL);

	cg_detach_all(group);

	char cg[-1];
	snprintf(cg, PATH_SIZE_MAX, MOUNT_POINT "/%s", group);

	int retval = rmdir(cg);
	assert(retval == 0);
}

variable_types:
group
char *

variable_types:
cg
char [-1]

variable_types: retval int

return_type: void


repo: martinliptak/kernel-from-scratch

decompiled_code:
void kthread_exit()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
void kthread_exit()
{
}

canonical_original_code:
void kthread_exit()
{
}


original_code:
process_t *kthread_start(void (*func)())
{
 ulong thr_pgdir_page, thr_stack_page;
 ulong *thr_pgdir, *thr_stack, *thr_stack_top;
 process_t *thr;
 process_queue_t *qthr;
 thr_pgdir_page = phys_alloc(PA_KERNEL);
 thr_stack_page = phys_alloc(PA_KERNEL);
 thr_pgdir = kaddr_alloc(1);
 thr_stack = kaddr_alloc(1);
 page_map_4k(pagedir, (ulong)thr_pgdir, thr_pgdir_page, PAGE_PRESENT | PAGE_WRITE);
 page_map_4k(pagedir, (ulong)thr_stack, thr_stack_page, PAGE_PRESENT | PAGE_WRITE);
 thr_stack_top = thr_stack + 1024;
 *--thr_stack_top = (ulong)kthread_exit;
 *--thr_stack_top = 0x246;
 *--thr_stack_top = 0x8;
 *--thr_stack_top = (ulong)func;
 *--thr_stack_top = 0;
 *--thr_stack_top = 0;
 *--thr_stack_top = 0;
 *--thr_stack_top = 0;
 --thr_stack_top;
        *thr_stack_top = (ulong)(thr_stack_top + 5);
 *--thr_stack_top = 0;
 *--thr_stack_top = 0;
 *--thr_stack_top = 0;
 memcpy(thr_pgdir, pagedir, 4096);
 thr_pgdir[1022] = thr_pgdir_page | PAGE_WRITE | PAGE_PRESENT;
 page_map_4k(thr_pgdir, 0xc0000000 - 4096, thr_stack_page,
   PAGE_PRESENT | PAGE_WRITE);
 thr = malloc(sizeof(process_t));
 memset(thr, 0, sizeof(thr));
 thr->esp = (ulong)0xc0000000 - ((ulong)thr_stack + 4096
  - (ulong)thr_stack_top);
 thr->ss = 0x10;
 thr->cr3 = thr_pgdir_page;
 qthr = malloc(sizeof(process_queue_t));
 qthr->proc = thr;
 lock();
 qthr->next = run_queue->next;
 run_queue->next = qthr;
 unlock();
}

canonical_original_code:
struct process *kthread_start(void (*func)())
{
 unsigned long thr_pgdir_page; unsigned long thr_stack_page;
 unsigned long *thr_pgdir; unsigned long *thr_stack; unsigned long *thr_stack_top;
 struct process *thr;
 struct process_queue *qthr;
 thr_pgdir_page = phys_alloc(PA_KERNEL);
 thr_stack_page = phys_alloc(PA_KERNEL);
 thr_pgdir = kaddr_alloc(1);
 thr_stack = kaddr_alloc(1);
 page_map_4k(pagedir, (unsigned long)thr_pgdir, thr_pgdir_page, PAGE_PRESENT | PAGE_WRITE);
 page_map_4k(pagedir, (unsigned long)thr_stack, thr_stack_page, PAGE_PRESENT | PAGE_WRITE);
 thr_stack_top = thr_stack + 1024;
 *--thr_stack_top = (unsigned long)kthread_exit;
 *--thr_stack_top = 0x246;
 *--thr_stack_top = 0x8;
 *--thr_stack_top = (unsigned long)func;
 *--thr_stack_top = 0;
 *--thr_stack_top = 0;
 *--thr_stack_top = 0;
 *--thr_stack_top = 0;
 --thr_stack_top;
        *thr_stack_top = (ulong)(thr_stack_top + 5);
 *--thr_stack_top = 0;
 *--thr_stack_top = 0;
 *--thr_stack_top = 0;
 memcpy(thr_pgdir, pagedir, 4096);
 thr_pgdir[1022] = thr_pgdir_page | PAGE_WRITE | PAGE_PRESENT;
 page_map_4k(thr_pgdir, 0xc0000000 - 4096, thr_stack_page,
   PAGE_PRESENT | PAGE_WRITE);
 thr = malloc(sizeof(process_t));
 memset(thr, 0, sizeof(thr));
 thr->esp = (unsigned long)0xc0000000 - ((unsigned long)thr_stack + 4096
  - (unsigned long)thr_stack_top);
 thr->ss = 0x10;
 thr->cr3 = thr_pgdir_page;
 qthr = malloc(sizeof(process_queue_t));
 qthr->proc = thr;
 lock();
 qthr->next = run_queue->next;
 run_queue->next = qthr;
 unlock();
}

variable_types:
func
void (*)()

variable_types: struct process_queue { struct process { unsigned long esp; unsigned long ss; unsigned long cr3; } *proc; struct process_queue *next; } *

user_defined_types: struct process_queue { struct process *proc; struct process_queue *next; }


strcmp

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

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

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

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

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

return_type: int


strlen

decompiled_code:
__int64 __fastcall strlen(_BYTE *a1)
{
_BYTE *v1; // rax
unsigned int v2; // er8

v1 = a1 + 1;
if ( !*a1 )
return 0LL;
do
v2 = (_DWORD)v1++ - (_DWORD)a1;
while ( *(v1 - 1) );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(_BYTE *a1)
{
_BYTE *v1; // rax
unsigned int v2; // er8

v1 = a1 + 1;
if ( !*a1 )
return 0LL;
do
v2 = (_DWORD)v1++ - (_DWORD)a1;
while ( *(v1 - 1) );
return v2;
}

original_code:
int strlen(const char *s)
{
 int len = 0;
 while (*s++)
  ++len;
 return len;
}

canonical_original_code:
int strlen(char *s)
{
 int len = 0;
 while (*s++)
  ++len;
 return len;
}

variable_types:
s
char *

variable_types: len int


decompiled_code:
int __fastcall parse_option(char *s2, char *s)
{
int v2; // eax
__int64 v3; // rbx
char **v4; // rdx
char *v5; // rax
int v6; // eax

if ( !options )
goto LABEL_9;
v2 = strcmp(options, s2);
if ( !v2 )
goto LABEL_5;
if ( !*(&options + 3) )
{
LABEL_9:
LODWORD(v5) = panic("Bad option name '%s'", s2);
return (int)v5;
}
v2 = 1;
LABEL_5:
v3 = v2;
v4 = &(&options)[3 * v2];
LODWORD(v5) = *((_DWORD *)v4 + 2);
switch ( (_DWORD)v5 )
{
case 1:
if ( !s )
panic("Option '%s' requires a string argument", s2);
LODWORD(v5) = (unsigned int)strcpy((&options)[3 * v3 + 2], s);
break;
case 2:
if ( !s || (LODWORD(v5) = sscanf(s, "%i", v4[2]), !(_DWORD)v5) )
LODWORD(v5) = panic("Option '%s' requires a signed numurical argument", s2);
break;
case 0:
if ( s )
{
if ( *s == 110 && s[1] == 111 && !s[2] || (v6 = (unsigned __int8)*s, v6 == 48) && !s[1] )
{
v5 = (&options)[3 * v3 + 2];
*(_DWORD *)v5 = 0;
}
else if ( !strcmp(s, "yes") || v6 == 49 && !s[1] )
{
v5 = (&options)[3 * v3 + 2];
*(_DWORD *)v5 = 1;
}
else
{
LODWORD(v5) = panic("Option '%s' requires a boolean argument (yes | no, 1 | 0)", s2);
}
}
else
{
v5 = v4[2];
*(_DWORD *)v5 = 1;
}
break;
}
return (int)v5;
}

canonical_decompiled_code:
int __fastcall func0(char *s2, char *s)
{
int v2; // eax
__int64 v3; // rbx
char **v4; // rdx
char *v5; // rax
int v6; // eax

if ( !options )
goto LABEL_9;
v2 = strcmp(options, s2);
if ( !v2 )
goto LABEL_5;
if ( !*(&options + 3) )
{
LABEL_9:
LODWORD(v5) = panic("Bad option name '%s'", s2);
return (int)v5;
}
v2 = 1;
LABEL_5:
v3 = v2;
v4 = &(&options)[3 * v2];
LODWORD(v5) = *((_DWORD *)v4 + 2);
switch ( (_DWORD)v5 )
{
case 1:
if ( !s )
panic("Option '%s' requires a string argument", s2);
LODWORD(v5) = (unsigned int)strcpy((&options)[3 * v3 + 2], s);
break;
case 2:
if ( !s || (LODWORD(v5) = sscanf(s, "%i", v4[2]), !(_DWORD)v5) )
LODWORD(v5) = panic("Option '%s' requires a signed numurical argument", s2);
break;
case 0:
if ( s )
{
if ( *s == 110 && s[1] == 111 && !s[2] || (v6 = (unsigned __int8)*s, v6 == 48) && !s[1] )
{
v5 = (&options)[3 * v3 + 2];
*(_DWORD *)v5 = 0;
}
else if ( !strcmp(s, "yes") || v6 == 49 && !s[1] )
{
v5 = (&options)[3 * v3 + 2];
*(_DWORD *)v5 = 1;
}
else
{
LODWORD(v5) = panic("Option '%s' requires a boolean argument (yes | no, 1 | 0)", s2);
}
}
else
{
v5 = v4[2];
*(_DWORD *)v5 = 1;
}
break;
}
return (int)v5;
}

original_code:
static void parse_option(const char *opt, const char *arg)
{
 int i;
 int sn;
 for (i = 0; options[i].name; ++i)
 {
  if (strcmp(options[i].name, opt) == 0)
  {
   switch (options[i].type)
   {
   case OPT_TYPE_BOOL:
    if (arg)
    {
     if (strcmp(arg, "no") == 0 || strcmp(arg, "0") == 0)
      *(bool *)options[i].value = 0;
     else if (strcmp(arg, "yes") == 0
       || strcmp(arg, "1") == 0)
      *(bool *)options[i].value = 1;
     else
      panic("Option \'%s\' requires a boolean "
        "argument (yes | no, 1 | 0)",
        opt);
    }
    else
     *(bool *)options[i].value = 1;
    break;
   case OPT_TYPE_STRING:
    if (!arg)
     panic("Option \'%s\' requires a string argument",
       opt);
    strcpy((char *)options[i].value, arg);
    break;
   case OPT_TYPE_SIGNED:
    if (!arg || sscanf(arg, "%i", (int *)options[i].value) == 0)
     panic("Option \'%s\' requires a signed numurical "
       "argument", opt);
    break;
   }
   return;
  }
 }
 panic("Bad option name \'%s\'", opt);
}

canonical_original_code:
static void parse_option(char *opt, char *arg)
{
 int i;
 int sn;
 for (i = 0; options[i].name; ++i)
 {
  if (strcmp(options[i].name, opt) == 0)
  {
   switch (options[i].type)
   {
   case OPT_TYPE_BOOL:
    if (arg)
    {
     if (strcmp(arg, "no") == 0 || strcmp(arg, "0") == 0)
      *(unsigned int *)options[i].value = 0;
     else if (strcmp(arg, "yes") == 0
       || strcmp(arg, "1") == 0)
      *(unsigned int *)options[i].value = 1;
     else
      panic("Option \'%s\' requires a boolean "
        "argument (yes | no, 1 | 0)",
        opt);
    }
    else
     *(unsigned int *)options[i].value = 1;
    break;
   case OPT_TYPE_STRING:
    if (!arg)
     panic("Option \'%s\' requires a string argument",
       opt);
    strcpy((char *)options[i].value, arg);
    break;
   case OPT_TYPE_SIGNED:
    if (!arg || sscanf(arg, "%i", (int *)options[i].value) == 0)
     panic("Option \'%s\' requires a signed numurical "
       "argument", opt);
    break;
   }
   return;
  }
 }
 panic("Bad option name \'%s\'", opt);
}

variable_types:
opt
char *

variable_types:
arg
char *

variable_types: sn int


decompiled_code:
int __fastcall option_found(char *a1)
{
char v1; // dl
__int64 v2; // rax

v1 = *a1;
if ( !*a1 )
return parse_option(a1, 0LL);
v2 = 0LL;
while ( v1 != 61 )
{
v1 = a1[++v2];
if ( !v1 )
return parse_option(a1, 0LL);
}
a1[v2] = 0;
return parse_option(a1, &a1[v2 + 1]);
}

canonical_decompiled_code:
int __fastcall func1(char *a1)
{
char v1; // dl
__int64 v2; // rax

v1 = *a1;
if ( !*a1 )
return func0(a1, 0LL);
v2 = 0LL;
while ( v1 != 61 )
{
v1 = a1[++v2];
if ( !v1 )
return func0(a1, 0LL);
}
a1[v2] = 0;
return func0(a1, &a1[v2 + 1]);
}

original_code:
static void option_found(char *opt)
{
 int i;
 const char *arg;
 for (i = 0, arg = 0; opt[i]; ++i)
  if (opt[i] == '=')
  {
   arg = opt + i + 1;
   opt[i] = 0;
   break;
  }
 parse_option(opt, arg);
}

canonical_original_code:
static void option_found(char *opt)
{
 int i;
 char *arg;
 for (i = 0, arg = 0; opt[i]; ++i)
  if (opt[i] == '=')
  {
   arg = opt + i + 1;
   opt[i] = 0;
   break;
  }
 parse_option(opt, arg);
}


decompiled_code:
__int64 __fastcall parse_cmdline(const char *a1)
{
const char *v1; // rbx
char **j; // rbp
char i; // al
int v4; // edi
const char *v5; // rax

v1 = a1;
j = (char **)dstring_new(16LL);
printk("<1>Command line: %s\n", a1);
for ( i = *a1; (i & 0xDF) != 0; ++v1 )
i = v1[1];
if ( i )
{
while ( 1 )
{
v4 = *v1;
if ( *v1 != 32 )
break;
++v1;
}
if ( (_BYTE)v4 )
{
do
{
if ( isblank(v4) && !isblank(v1[1]) )
{
dstring_append_char(j, 0LL);
option_found(*j);
dstring_free(j);
for ( j = (char **)dstring_new(16LL); *v1 == 32; ++v1 )
;
v5 = v1;
}
else
{
dstring_append_char(j, (unsigned int)*v1);
v5 = v1;
}
v4 = v5[1];
++v1;
}
while ( v5[1] );
}
dstring_append_char(j, 0LL);
option_found(*j);
}
return dstring_free(j);
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1)
{
const char *v1; // rbx
char **j; // rbp
char i; // al
int v4; // edi
const char *v5; // rax

v1 = a1;
j = (char **)dstring_new(16LL);
printk("<1>Command line: %s\n", a1);
for ( i = *a1; (i & 0xDF) != 0; ++v1 )
i = v1[1];
if ( i )
{
while ( 1 )
{
v4 = *v1;
if ( *v1 != 32 )
break;
++v1;
}
if ( (_BYTE)v4 )
{
do
{
if ( isblank(v4) && !isblank(v1[1]) )
{
dstring_append_char(j, 0LL);
func1(*j);
dstring_free(j);
for ( j = (char **)dstring_new(16LL); *v1 == 32; ++v1 )
;
v5 = v1;
}
else
{
dstring_append_char(j, (unsigned int)*v1);
v5 = v1;
}
v4 = v5[1];
++v1;
}
while ( v5[1] );
}
dstring_append_char(j, 0LL);
func1(*j);
}
return dstring_free(j);
}

original_code:
void parse_cmdline(const char *cmdline)
{
 const char *c;
 dstring_t *opt = dstring_new(16);
 printk("<1>Command line: %s\n", cmdline);
 for (c = cmdline; *c != ' ' && *c != 0; ++c)
  ;
 if (*c == 0)
 {
  dstring_free(opt);
  return;
 }
 while (*c == ' ')
  ++c;
 for (; *c; ++c)
 {
  if (isblank(*c) && !isblank(c[1]))
  {
   dstring_append_char(opt, 0);
   option_found(opt->str);
   dstring_free(opt);
   opt = dstring_new(16);
   while (*c == ' ')
    ++c;
  }
  else
   dstring_append_char(opt, *c);
 }
 dstring_append_char(opt, *c);
 option_found(opt->str);
 dstring_free(opt);
}

canonical_original_code:
void parse_cmdline(char *cmdline)
{
 char *c;
 struct dstring_t *opt = dstring_new(16);
 printk("<1>Command line: %s\n", cmdline);
 for (c = cmdline; *c != ' ' && *c != 0; ++c)
  ;
 if (*c == 0)
 {
  dstring_free(opt);
  return;
 }
 while (*c == ' ')
  ++c;
 for (; *c; ++c)
 {
  if (isblank(*c) && !isblank(c[1]))
  {
   dstring_append_char(opt, 0);
   option_found(opt->str);
   dstring_free(opt);
   opt = dstring_new(16);
   while (*c == ' ')
    ++c;
  }
  else
   dstring_append_char(opt, *c);
 }
 dstring_append_char(opt, *c);
 option_found(opt->str);
 dstring_free(opt);
}

variable_types:
cmdline
char *

variable_types:
c
char *

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


spaces

decompiled_code:
__int64 __fastcall spaces(__int64 (__fastcall *a1)(__int64, __int64), __int64 a2, int a3)
{
int v3; // ebx
__int64 result; // rax

if ( a3 > 0 )
{
v3 = a3 - 1;
do
result = a1(a2, 32LL);
while ( v3-- != 0 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 (__fastcall *a1)(__int64, __int64), __int64 a2, int a3)
{
int v3; // ebx
__int64 result; // rax

if ( a3 > 0 )
{
v3 = a3 - 1;
do
result = a1(a2, 32LL);
while ( v3-- != 0 );
}
return result;
}

original_code:
static void spaces(putchr_t putchr, void *putchr_arg, int n)
{
 while (n-- > 0)
  putchr(putchr_arg, ' ');
}

canonical_original_code:
static void spaces(void (*putchr)(void *arg, char c), void *putchr_arg, int n)
{
 while (n-- > 0)
  putchr(putchr_arg, ' ');
}

variable_types:
putchr
void (*)(void *arg, char c)

variable_types: n int


putstr

decompiled_code:
__int64 __fastcall putstr(__int64 (__fastcall *a1)(__int64), __int64 a2, _BYTE *a3)
{
_BYTE *v3; // rbx
__int64 result; // rax

if ( *a3 )
{
v3 = a3;
do
{
++v3;
result = a1(a2);
}
while ( *v3 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 (__fastcall *a1)(__int64), __int64 a2, _BYTE *a3)
{
_BYTE *v3; // rbx
__int64 result; // rax

if ( *a3 )
{
v3 = a3;
do
{
++v3;
result = a1(a2);
}
while ( *v3 );
}
return result;
}

original_code:
static void putstr(putchr_t putchr, void *putchr_arg, const char *str)
{
 while (*str)
  putchr(putchr_arg, *str++);
}

canonical_original_code:
static void putstr(void (*putchr)(void *arg, char c), void *putchr_arg, char *str)
{
 while (*str)
  putchr(putchr_arg, *str++);
}

variable_types:
str
char *


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

lock();
v1 = &kaddr_block_head;
do
{
v1 = (_QWORD *)v1[3];
if ( !v1 )
{
bug("kaddr_free(): Trying to free a block which is not allocated.");
return unlock();
}
}
while ( *v1 != a1 );
if ( *((_DWORD *)v1 + 4) )
*((_DWORD *)v1 + 4) = 0;
else
bug("kaddr_free(): Trying to free a block twice.");
return unlock();
}

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

lock();
v1 = &kaddr_block_head;
do
{
v1 = (_QWORD *)v1[3];
if ( !v1 )
{
bug("kaddr_free(): Trying to free a block which is not allocated.");
return unlock();
}
}
while ( *v1 != a1 );
if ( *((_DWORD *)v1 + 4) )
*((_DWORD *)v1 + 4) = 0;
else
bug("kaddr_free(): Trying to free a block twice.");
return unlock();
}

original_code:
void kaddr_free(void *base)
{
 kaddr_block_t *block;
 bool found;
 lock();
 for (found = 0, block = &kaddr_block_head; block->next; block = block->next)
 {
  if (block->next->base == base)
  {
   found = 1;
   break;
  }
 }
 if (found)
 {
  if (block->next->used == 0)
   bug("kaddr_free(): Trying to free a block twice.");
  else
   block->next->used = 0;
 }
 else
  bug("kaddr_free(): Trying to free a block which is not allocated.");
 unlock();
}

canonical_original_code:
void kaddr_free(void *base)
{
 struct _kaddr_block *block;
 unsigned int found;
 lock();
 for (found = 0, block = &kaddr_block_head; block->next; block = block->next)
 {
  if (block->next->base == base)
  {
   found = 1;
   break;
  }
 }
 if (found)
 {
  if (block->next->used == 0)
   bug("kaddr_free(): Trying to free a block twice.");
  else
   block->next->used = 0;
 }
 else
  bug("kaddr_free(): Trying to free a block which is not allocated.");
 unlock();
}

variable_types:
base
void *

variable_types: struct _kaddr_block { void *base; unsigned long pages; unsigned int used; struct _kaddr_block *next; } *

variable_types:
found
unsigned int


decompiled_code:
__int64 sched_init()
{
_QWORD *v0; // rbx
_QWORD *v1; // rax

v0 = malloc(0x10uLL);
run_queue = (__int64)v0;
v1 = malloc(0x18uLL);
v0[1] = v0;
*v1 = 0LL;
*v0 = v1;
current = (__int64)v1;
return pic_unmask(0LL);
}

canonical_decompiled_code:
__int64 func0()
{
_QWORD *v0; // rbx
_QWORD *v1; // rax

v0 = malloc(0x10uLL);
run_queue = (__int64)v0;
v1 = malloc(0x18uLL);
v0[1] = v0;
*v1 = 0LL;
*v0 = v1;
current = (__int64)v1;
return pic_unmask(0LL);
}

original_code:
void sched_init()
{
 run_queue = malloc(sizeof(process_queue_t));
 run_queue[0].proc = malloc(sizeof(process_t));
 memset(run_queue[0].proc, 0, sizeof(run_queue[0].proc));
 run_queue[0].next = run_queue;
 current = run_queue->proc;
 pic_unmask(0);
}


original_code:
static inline void merge_free_blocks(malloc_block_t *blk)
{
 while (blk->used == 0 && blk->next && blk->next->used == 0)
 {
  if (blk->magic != 0x7ABCDEF7)
  {
   panic("Heap corrupted");
   return;
  }
  blk->size += blk->next->size + sizeof(malloc_block_t);
  blk->next = blk->next->next;
 }
}

canonical_original_code:
static inline void merge_free_blocks(struct _malloc_block *blk)
{
 while (blk->used == 0 && blk->next && blk->next->used == 0)
 {
  if (blk->magic != 0x7ABCDEF7)
  {
   panic("Heap corrupted");
   return;
  }
  blk->size += blk->next->size + sizeof(malloc_block_t);
  blk->next = blk->next->next;
 }
}

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


original_code:
static malloc_block_t *cut_block(malloc_block_t *blk, ulong size)
{
 malloc_block_t *nblk;
 if (blk->size > size + sizeof(malloc_block_t))
 {
  nblk = (malloc_block_t *)((char *)blk + sizeof(malloc_block_t) + size);
  nblk->size = blk->size - size - sizeof(malloc_block_t);
  nblk->used = 0;
  nblk->magic = 0x7ABCDEF7;
  nblk->next = blk->next;
  merge_free_blocks(nblk);
  blk->size = size;
  blk->used = 1;
  blk->next = nblk;
  return (malloc_block_t *)((char *)blk + sizeof(malloc_block_t));
 }
 else if (blk->size >= size && blk->size <= size + sizeof(malloc_block_t))
 {
  blk->used = 1;
  return (malloc_block_t *)((char *)blk + sizeof(malloc_block_t));
 }
 else
  return 0;
}

canonical_original_code:
static struct _malloc_block *cut_block(struct _malloc_block *blk, unsigned long size)
{
 struct _malloc_block *nblk;
 if (blk->size > size + sizeof(malloc_block_t))
 {
  nblk = (struct _malloc_block *)((char *)blk + sizeof(malloc_block_t) + size);
  nblk->size = blk->size - size - sizeof(malloc_block_t);
  nblk->used = 0;
  nblk->magic = 0x7ABCDEF7;
  nblk->next = blk->next;
  merge_free_blocks(nblk);
  blk->size = size;
  blk->used = 1;
  blk->next = nblk;
  return (struct _malloc_block *)((char *)blk + sizeof(malloc_block_t));
 }
 else if (blk->size >= size && blk->size <= size + sizeof(malloc_block_t))
 {
  blk->used = 1;
  return (struct _malloc_block *)((char *)blk + sizeof(malloc_block_t));
 }
 else
  return 0;
}

variable_types:
blk
struct _malloc_block *

variable_types:
size
unsigned long

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


decompiled_code:
__int64 __fastcall more_heap(__int64 a1)
{
__int64 v1; // r12
__int64 i; // rbx
__int64 v3; // rdx
__int64 result; // rax

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

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
__int64 v1; // r12
__int64 i; // rbx
__int64 v3; // rdx
__int64 result; // rax

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

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


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


malloc

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

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

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:
retval
void *

return_type: void *


free

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

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

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

v1 = qword_4B0;
v2 = a1 - 24;
if ( qword_4B0 )
{
v3 = qword_4B0;
while ( 1 )
{
if ( (*(_DWORD *)v3 & 0x7FFFFFFF) != 2059198199 )
{
panic("Heap corrupted");
return;
}
if ( v2 == v3 )
break;
v3 = *(_QWORD *)(v3 + 16);
if ( !v3 )
goto LABEL_7;
}
if ( *(char *)(v2 + 3) >= 0 )
{
bug("free(): Trying to free a block twice");
}
else
{
*(_BYTE *)(v2 + 3) &= 0x7Fu;
do
{
func0(v1);
v1 = *(_QWORD *)(v1 + 16);
}
while ( v1 );
}
}
else
{
LABEL_7:
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:
char *__fastcall realloc(void *src, unsigned __int64 a2)
{
__int64 v2; // rax
char *v3; // rbp
void *v4; // r8
void *v6; // rax

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

canonical_decompiled_code:
char *__fastcall func6(void *src, unsigned __int64 a2)
{
__int64 v2; // rax
char *v3; // rbp
void *v4; // r8
void *v6; // rax

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

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; // er12

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; // er12

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


printk

decompiled_code:
__int64 printk(_BYTE *a1, ...)
{
unsigned int v1; // er12
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; // er12
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 i; // rax
unsigned int v3; // er12
__int64 v4; // rax

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

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
__int64 i; // rax
unsigned int v3; // er12
__int64 v4; // rax

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

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 != &buffer[64] );
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 != &buffer[64] );
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()
{
if ( !lock_depth )
irq_off();
return (unsigned int)++lock_depth;
}

canonical_decompiled_code:
__int64 func0()
{
if ( !lock_depth )
irq_off();
return (unsigned int)++lock_depth;
}

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, int a2, char a3)
{
int v4; // ebx
__int64 i; // rax

v4 = 1;
lock();
for ( i = (unsigned int)((_DWORD)&gdt + 8); *(_QWORD *)i || *(_QWORD *)(i + 4); i += 8LL )
{
if ( ++v4 == 0x2000 )
segment_new_cold();
}
*(_WORD *)i = a2;
*(_WORD *)(i + 2) = a1;
*(_BYTE *)(i + 5) = a3;
*(_BYTE *)(i + 6) = BYTE2(a2) & 0xF | 0xC0;
*(_BYTE *)(i + 7) = HIBYTE(a1);
*(_BYTE *)(i + 4) = BYTE2(a1);
unlock();
return (unsigned int)(8 * v4);
}

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 ( v2 != (char *)v1 );
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 ( v2 != (char *)v1 );
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()
{
int result; // eax
char v1; // bl
const unsigned __int16 **v2; // rax
const unsigned __int16 *v3; // rdx
const unsigned __int16 **v4; // r13
char *v5; // rbp
__m128i v6; // [rsp+0h] [rbp-78h] BYREF
char src[50]; // [rsp+10h] [rbp-68h] BYREF
char v8[54]; // [rsp+42h] [rbp-36h] BYREF

v6 = _mm_loadu_si128((const __m128i *)*curwin);
result = v6.m128i_i32[2];
v1 = *(_BYTE *)(*(_QWORD *)(v6.m128i_i64[0] + 16) + v6.m128i_i32[2]);
if ( v1 != 32 )
goto LABEL_4;
while ( 1 )
{
result = inc(&v6);
if ( result )
break;
result = v6.m128i_i32[2];
v1 = *(_BYTE *)(*(_QWORD *)(v6.m128i_i64[0] + 16) + v6.m128i_i32[2]);
if ( v1 != 32 )
{
LABEL_4:
if ( v1 != 9 )
break;
}
}
if ( v1 >= 0 )
{
v2 = _ctype_b_loc();
v3 = *v2;
v4 = v2;
result = (*v2)[v1];
if ( (result & 8) == 0 )
{
if ( v1 != 95 )
return result;
LOWORD(result) = v3[95];
}
v5 = src;
while ( (result & 8) != 0 || v1 == 95 )
{
if ( v5 == v8 )
goto LABEL_19;
*v5++ = v1;
if ( (unsigned int)inc(&v6) )
break;
v1 = *(_BYTE *)(*(_QWORD *)(v6.m128i_i64[0] + 16) + v6.m128i_i32[2]);
if ( v1 < 0 )
break;
LOWORD(result) = (*v4)[v1];
}
if ( v5 < v8 )
{
*v5 = 0;
return exTag(src, 0, 1, 1);
}
LABEL_19:
result = beep();
}
return result;
}

canonical_decompiled_code:
int func4()
{
int result; // eax
char v1; // bl
const unsigned __int16 **v2; // rax
const unsigned __int16 *v3; // rdx
const unsigned __int16 **v4; // r13
char *v5; // rbp
__m128i v6; // [rsp+0h] [rbp-78h] BYREF
char src[50]; // [rsp+10h] [rbp-68h] BYREF
char v8[54]; // [rsp+42h] [rbp-36h] BYREF

v6 = _mm_loadu_si128((const __m128i *)*curwin);
result = v6.m128i_i32[2];
v1 = *(_BYTE *)(*(_QWORD *)(v6.m128i_i64[0] + 16) + v6.m128i_i32[2]);
if ( v1 != 32 )
goto LABEL_4;
while ( 1 )
{
result = inc(&v6);
if ( result )
break;
result = v6.m128i_i32[2];
v1 = *(_BYTE *)(*(_QWORD *)(v6.m128i_i64[0] + 16) + v6.m128i_i32[2]);
if ( v1 != 32 )
{
LABEL_4:
if ( v1 != 9 )
break;
}
}
if ( v1 >= 0 )
{
v2 = _ctype_b_loc();
v3 = *v2;
v4 = v2;
result = (*v2)[v1];
if ( (result & 8) == 0 )
{
if ( v1 != 95 )
return result;
LOWORD(result) = v3[95];
}
v5 = src;
while ( (result & 8) != 0 || v1 == 95 )
{
if ( v5 == v8 )
goto LABEL_19;
*v5++ = v1;
if ( (unsigned int)inc(&v6) )
break;
v1 = *(_BYTE *)(*(_QWORD *)(v6.m128i_i64[0] + 16) + v6.m128i_i32[2]);
if ( v1 < 0 )
break;
LOWORD(result) = (*v4)[v1];
}
if ( v5 < v8 )
{
*v5 = 0;
return func3(src, 0, 1, 1);
}
LABEL_19:
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

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; // ebx
__int64 v1; // rax
char *v2; // rbp
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; // ebx
__int64 v1; // rax
char *v2; // rbp
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; // r12
int v1; // eax
__int64 v2; // rbp
__int64 result; // rax
__int64 v4; // rsi
int v5; // eax

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

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

v0 = *(_QWORD *)*curwin;
v1 = strlen(*(const char **)(v0 + 16));
v2 = newline((unsigned int)(v1 + 1));
result = 0LL;
if ( v2 )
{
repllines(v0, 0LL, v2);
v4 = (unsigned int)params[35];
if ( (_DWORD)v4 )
{
**(_BYTE **)(v2 + 16) = 0;
v5 = func4(v0);
indentchars = func5(v2, v5);
v4 = indentchars;
}
else
{
indentchars = 0;
}
move_cursor(v2, v4);
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; // rdx
__int64 v1; // rbx
__int64 v2; // rax

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

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

v0 = curbuf;
v1 = curwin;
while ( (*(_BYTE *)(v0 + 56) & 1) == 0 )
{
v2 = xvNextWindow(curwin);
v0 = 0LL;
curwin = v2;
if ( v2 )
v0 = *(_QWORD *)(v2 + 8);
curbuf = v0;
if ( v2 == v1 )
return 0LL;
}
curwin = v1;
if ( v1 )
v1 = *(_QWORD *)(v1 + 8);
curbuf = v1;
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:
__int64 xvAutoWriteAll()
{
__int64 result; // rax
__int64 v1; // rdx
__int64 v2; // rbx

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

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

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

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 )
{
result = (unsigned int)params[65];
if ( (_DWORD)result )
result = exWriteToFile(0LL, 0LL, 0LL, 0LL);
}
return result;
}

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

result = curbuf;
if ( (*(_BYTE *)(curbuf + 56LL) & 1) != 0 )
{
result = (unsigned int)params[65];
if ( (_DWORD)result )
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()
{
int v1; // eax
int v2; // eax
struct timeval v3; // [rsp+0h] [rbp-A8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-98h] BYREF

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

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

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

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

result = curmode;
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

result = curmode;
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

v3 = 0;
v4[0] = 0;
tcgetattr(0, &cooked_state);
v0 = _mm_load_si128((const __m128i *)&cooked_state);
v1 = _mm_load_si128((const __m128i *)&cooked_state.c_line);
*(_QWORD *)&raw_state.c_cc[31] = *(_QWORD *)&cooked_state.c_cc[31];
*(__m128i *)&raw_state.c_iflag = v0;
raw_state.c_ospeed = cooked_state.c_ospeed;
*(_QWORD *)&raw_state.c_iflag = v0.m128i_i64[0] & 0xFFFFE7FAFFFFFE3FLL;
*(__m128i *)&raw_state.c_line = v1;
*(_WORD *)&raw_state.c_cc[5] = 256;
*(__m128i *)&raw_state.c_cc[15] = _mm_load_si128((const __m128i *)&cooked_state.c_cc[15]);
raw_state.c_lflag = v0.m128i_i32[3] & 0xFFFFF181;
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(&v3, v4);
tty_open(&v3, v4);
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

v3 = 0;
v4[0] = 0;
tcgetattr(0, &cooked_state);
v0 = _mm_load_si128((const __m128i *)&cooked_state);
v1 = _mm_load_si128((const __m128i *)&cooked_state.c_line);
*(_QWORD *)&raw_state.c_cc[31] = *(_QWORD *)&cooked_state.c_cc[31];
*(__m128i *)&raw_state.c_iflag = v0;
raw_state.c_ospeed = cooked_state.c_ospeed;
*(_QWORD *)&raw_state.c_iflag = v0.m128i_i64[0] & 0xFFFFE7FAFFFFFE3FLL;
*(__m128i *)&raw_state.c_line = v1;
*(_WORD *)&raw_state.c_cc[5] = 256;
*(__m128i *)&raw_state.c_cc[15] = _mm_load_si128((const __m128i *)&cooked_state.c_cc[15]);
raw_state.c_lflag = v0.m128i_i32[3] & 0xFFFFF181;
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(&v3, v4);
tty_open(&v3, v4);
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 result; // rax

result = curbuf;
*(__m128i *)(curbuf + 688LL) = _mm_loadu_si128((const __m128i *)*curwin);
return result;
}

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

result = curbuf;
*(__m128i *)(curbuf + 688LL) = _mm_loadu_si128((const __m128i *)*curwin);
return result;
}

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


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

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

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

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

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