vfprintf

decompiled_code:
__int64 __fastcall vfprintf(FILE *s, __int64 a2, __int64 a3)
{
char *v3; // rbp
size_t v4; // rax

v3 = (char *)vsaprintf(a2, a3);
v4 = strlen(v3);
fwrite(v3, v4, 1uLL, s);
free(v3);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(FILE *s, __int64 a2, __int64 a3)
{
char *v3; // rbp
size_t v4; // rax

v3 = (char *)vsaprintf(a2, a3);
v4 = strlen(v3);
fwrite(v3, v4, 1uLL, s);
free(v3);
return 0LL;
}

original_code:
int vfprintf(FILE *stream, const char *format, va_list args) {
    char *string = vsaprintf(format, args);
    fwrite(string, strlen(string), sizeof(char), stream);
    free(string);
    return 0;
}

canonical_original_code:
int vfprintf(struct __file *stream, char *format, __builtin_va_list args) {
    char *string = vsaprintf(format, args);
    fwrite(string, strlen(string), sizeof(char), stream);
    free(string);
    return 0;
}

variable_types:
string
char *

user_defined_types: struct __file { unsigned long long descriptor; unsigned int lock; unsigned long long position; unsigned char flags; void *buffer; unsigned long long buffer_len; unsigned long long buffer_size; unsigned char buffer_mode; void (*callback_write)(struct __file *, unsigned long long pos, unsigned long long size, void *data); unsigned long long (*callback_read)(struct __file *, unsigned long long pos, unsigned long long size, void *data); unsigned long long (*callback_length)(struct __file *); struct __file *next; }


localeconv

repo: zrho/Carbon

decompiled_code:
__int64 localeconv()
{
return _current_locale;
}

canonical_decompiled_code:
__int64 func0()
{
return _current_locale;
}

original_code:
struct lconv *localeconv(void) {
 return __current_locale;
}

canonical_original_code:
struct lconv *localeconv() {
 return __current_locale;
}

return_type: struct lconv { char *decimal_point; char *grouping; char *thousands_sep; char *currency_symbol; char *int_curr_symbol; char *mon_decimal_point; char *mon_grouping; char *mon_thousands_sep; char *negative_sign; char *positive_sign; char frac_digits; char int_frac_digits; char n_cs_precedes; char n_sep_by_space; char n_sign_posn; char p_cs_precedes; char p_sep_by_space; char p_sign_posn; } *


atexit

decompiled_code:
__int64 __fastcall atexit(__int64 a1)
{
_QWORD *v1; // rax
__int64 v2; // rcx

v1 = malloc(0x10uLL);
v2 = _atexit_func_list;
v1[1] = a1;
*v1 = v2;
_atexit_func_list = v1;
return 0LL;
}

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

v1 = malloc(0x10uLL);
v2 = _atexit_func_list;
v1[1] = a1;
*v1 = v2;
_atexit_func_list = v1;
return 0LL;
}

original_code:
int atexit(void (*function)(void)) {
 struct __atexit_func *func =
   (struct __atexit_func *) malloc(sizeof(struct __atexit_func));
 func->function = function;
 func->next = __atexit_func_list;
 __atexit_func_list = func;
 return 0;
}

canonical_original_code:
int atexit(void (*function)()) {
 struct __atexit_func *func = (struct __atexit_func *) malloc(sizeof(struct __atexit_func));
 func->function = function;
 func->next = __atexit_func_list;
 __atexit_func_list = func;
 return 0;
}

variable_types:
function
void (*)()

variable_types: struct __atexit_func { struct __atexit_func *next; void (*function)(); } *

return_type: int


vsaprintf

decompiled_code:
const void *__fastcall vsaprintf(char *a1, __int64 a2)
{
char v2; // cl
char *v3; // rax
int v4; // er13
char *v5; // r13
char *v7; // [rsp+0h] [rbp-58h] BYREF
const void *v8; // [rsp+8h] [rbp-50h] BYREF
size_t v9; // [rsp+10h] [rbp-48h] BYREF
__int64 v10[8]; // [rsp+18h] [rbp-40h] BYREF

v2 = *a1;
v8 = 0LL;
if ( !v2 )
return 0LL;
v3 = a1;
v4 = 0;
do
{
if ( v2 == 37 )
{
v7 = v3;
v5 = (char *)_format(&v7, a2);
string_adds(&v9, v10, (const char **)&v8, v5);
free(v5);
v4 = (_DWORD)v7 - (_DWORD)a1;
}
else
{
string_addc(&v9, v10, &v8, v2);
}
v3 = &a1[++v4];
v2 = *v3;
}
while ( *v3 );
return v8;
}

canonical_decompiled_code:
const void *__fastcall func1(char *a1, __int64 a2)
{
char v2; // cl
char *v3; // rax
int v4; // er13
char *v5; // r13
char *v7; // [rsp+0h] [rbp-58h] BYREF
const void *v8; // [rsp+8h] [rbp-50h] BYREF
size_t v9; // [rsp+10h] [rbp-48h] BYREF
__int64 v10[8]; // [rsp+18h] [rbp-40h] BYREF

v2 = *a1;
v8 = 0LL;
if ( !v2 )
return 0LL;
v3 = a1;
v4 = 0;
do
{
if ( v2 == 37 )
{
v7 = v3;
v5 = (char *)_format(&v7, a2);
func0(&v9, v10, (const char **)&v8, v5);
free(v5);
v4 = (_DWORD)v7 - (_DWORD)a1;
}
else
{
string_addc(&v9, v10, &v8, v2);
}
v3 = &a1[++v4];
v2 = *v3;
}
while ( *v3 );
return v8;
}

original_code:
char *vsaprintf(const char *format, va_list ap) {
    const char *format_tmp;
    char *temp;
    int i;
    char *string = 0;
    size_t length;
    size_t top;
    for (i = 0; format[i]; i++) {
        if (format[i] == '%') {
            format_tmp = &format[i];
            temp = __format(&format_tmp, ap);
            _string_adds(&length, &top, &string, temp);
            free(temp);
            i = (uintptr_t) format_tmp - (uintptr_t) format;
        }
        else {
            _string_addc(&length, &top, &string, format[i]);
        }
    }
    return string;
}

canonical_original_code:
char *vsaprintf(char *format, __builtin_va_list ap) {
    char *format_tmp;
    char *temp;
    int i;
    char *string = 0;
    unsigned long long length;
    unsigned long long top;
    for (i = 0; format[i]; i++) {
        if (format[i] == '%') {
            format_tmp = &format[i];
            temp = __format(&format_tmp, ap);
            _string_adds(&length, &top, &string, temp);
            free(temp);
            i = (unsigned long long) format_tmp - (unsigned long long) format;
        }
        else {
            _string_addc(&length, &top, &string, format[i]);
        }
    }
    return string;
}

variable_types:
format
char *

variable_types:
temp
char *

variable_types: i int

variable_types:
length
unsigned long long

variable_types:
top
unsigned long long

return_type: char *


strtoll

original_code:
long long int strtoll(const char *nptr, char **endptr, int base) {
 size_t i;
 int64_t sum;
 bool neg;
 i = 0;
 sum = 0;
 while ((_ctype_tbl[(unsigned char) (nptr[i])] & 0x200)) {
  i++;
 }
 if (nptr[i] == '+') {
  neg = 0;
  i++;
 }
 else if (nptr[i] == '-') {
  neg = 1;
  i++;
 }
 if (base == 0) {
  if (nptr[i] == '0') {
   i++;
   if (nptr[i] == 'x') {
    base = 16;
   }
   else if (__isbdigit(nptr[i], 8)) {
    base = 8;
   }
  }
  else if (__isbdigit(nptr[i], 10)) {
   base = 10;
  }
 }
 if (base == 0) {
  if (endptr) {
   *endptr = (char*) &nptr[i];
  }
  return 0;
 }
 while (nptr[i] && __isbdigit(nptr[i], base)) {
  sum *= base;
  sum += __digit(nptr[i], base);
  i++;
 }
 if (endptr) {
  *endptr = (char*) &nptr[i];
 }
 return (neg) ? -sum : sum;
}

canonical_original_code:
long long strtoll(char *nptr, char **endptr, int base) {
 unsigned long long i;
 long long sum;
 unsigned char neg;
 i = 0;
 sum = 0;
 while ((_ctype_tbl[(unsigned char) (nptr[i])] & 0x200)) {
  i++;
 }
 if (nptr[i] == '+') {
  neg = 0;
  i++;
 }
 else if (nptr[i] == '-') {
  neg = 1;
  i++;
 }
 if (base == 0) {
  if (nptr[i] == '0') {
   i++;
   if (nptr[i] == 'x') {
    base = 16;
   }
   else if (__isbdigit(nptr[i], 8)) {
    base = 8;
   }
  }
  else if (__isbdigit(nptr[i], 10)) {
   base = 10;
  }
 }
 if (base == 0) {
  if (endptr) {
   *endptr = (char *) &nptr[i];
  }
  return 0;
 }
 while (nptr[i] && __isbdigit(nptr[i], base)) {
  sum *= base;
  sum += __digit(nptr[i], base);
  i++;
 }
 if (endptr) {
  *endptr = (char *) &nptr[i];
 }
 return (neg) ? -sum : sum;
}

variable_types:
nptr
char *

variable_types:
endptr
char **

variable_types: base int

variable_types:
i
unsigned long long

variable_types:
sum
long long

variable_types:
neg
unsigned char

return_type: long long


fflush

decompiled_code:
__int64 __fastcall fflush(_QWORD *a1)
{
mutex_lock(a1 + 1);
if ( a1[8] )
_stdio_write(a1, a1[5], a1[4]);
a1[5] = 0LL;
mutex_unlock(a1 + 1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(_QWORD *a1)
{
mutex_lock(a1 + 1);
if ( a1[8] )
_stdio_write(a1, a1[5], a1[4]);
a1[5] = 0LL;
mutex_unlock(a1 + 1);
return 0LL;
}

original_code:
int fflush(FILE *fp) {
    mutex_lock(&fp->lock);
    if (0 == fp->callback_write) {
        fp->buffer_len = 0;
        mutex_unlock(&fp->lock);
        return 0;
    }
    __stdio_write(fp, fp->buffer_len, fp->buffer);
    fp->buffer_len = 0;
    mutex_unlock(&fp->lock);
    return 0;
}

canonical_original_code:
int fflush(struct __file *fp) {
    mutex_lock(&fp->lock);
    if (0 == fp->callback_write) {
        fp->buffer_len = 0;
        mutex_unlock(&fp->lock);
        return 0;
    }
    __stdio_write(fp, fp->buffer_len, fp->buffer);
    fp->buffer_len = 0;
    mutex_unlock(&fp->lock);
    return 0;
}

variable_types: struct __file { unsigned long long descriptor; unsigned int lock; unsigned long long position; unsigned char flags; void *buffer; unsigned long long buffer_len; unsigned long long buffer_size; unsigned char buffer_mode; void (*callback_write)(struct __file *, unsigned long long pos, unsigned long long size, void *data); unsigned long long (*callback_read)(struct __file *, unsigned long long pos, unsigned long long size, void *data); unsigned long long (*callback_length)(struct __file *); struct __file *next; } *


strlcpy

decompiled_code:
__int64 __fastcall strlcpy(_BYTE *a1, _BYTE *a2, __int64 a3)
{
__int64 v3; // rcx
_BYTE *v4; // rax
char v5; // dl
_BYTE *v6; // rdx
__int64 result; // rax

if ( a3 )
{
v3 = (__int64)&a1[a3 - 1];
v4 = a2;
while ( 1 )
{
v6 = v4++;
if ( a1 == (_BYTE *)v3 )
break;
v5 = *(v4 - 1);
*a1++ = v5;
if ( !v5 )
goto LABEL_6;
}
*a1 = 0;
v4 = v6 + 1;
if ( *v6 )
goto LABEL_8;
LABEL_6:
result = v4 - a2 - 1;
}
else
{
v4 = a2;
do
LABEL_8:
++v4;
while ( *(v4 - 1) );
result = v4 - a2 - 1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1, _BYTE *a2, __int64 a3)
{
__int64 v3; // rcx
_BYTE *v4; // rax
char v5; // dl
_BYTE *v6; // rdx
__int64 result; // rax

if ( a3 )
{
v3 = (__int64)&a1[a3 - 1];
v4 = a2;
while ( 1 )
{
v6 = v4++;
if ( a1 == (_BYTE *)v3 )
break;
v5 = *(v4 - 1);
*a1++ = v5;
if ( !v5 )
goto LABEL_6;
}
*a1 = 0;
v4 = v6 + 1;
if ( *v6 )
goto LABEL_8;
LABEL_6:
result = v4 - a2 - 1;
}
else
{
v4 = a2;
do
LABEL_8:
++v4;
while ( *(v4 - 1) );
result = v4 - a2 - 1;
}
return result;
}

original_code:
size_t strlcpy(char *dst, const char *src, size_t siz) {
 char *d = dst;
 const char *s = src;
 size_t n = siz;
 if (n != 0) {
  while (--n != 0) {
   if ((*d++ = *s++) == '\0') {
    break;
   }
  }
 }
 if (n == 0) {
  if (siz != 0) {
   *d = '\0';
  }
  while (*s++);
 }
 return (s - src - 1);
}

canonical_original_code:
unsigned long long strlcpy(char *dst, char *src, unsigned long long siz) {
 char *d = dst;
 char *s = src;
 unsigned long long n = siz;
 if (n != 0) {
  while (--n != 0) {
   if ((*d++ = *s++) == '\0') {
    break;
   }
  }
 }
 if (n == 0) {
  if (siz != 0) {
   *d = '\0';
  }
  while (*s++);
 }
 return (s - src - 1);
}

variable_types:
dst
char *

variable_types:
src
char *

variable_types:
siz
unsigned long long

variable_types:
d
char *

variable_types:
s
char *

variable_types:
n
unsigned long long

return_type: unsigned long long


malloc

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

result = _placement;
_placement = (char *)_placement + a1;
return result;
}

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

result = _placement;
_placement = (char *)_placement + a1;
return result;
}

original_code:
void *malloc(size_t size) {
 uintptr_t addr = __placement;
 __placement += size;
 return (void *) addr;
}

canonical_original_code:
void *malloc(unsigned long long size) {
 unsigned long long addr = __placement;
 __placement += size;
 return (void *) addr;
}

variable_types:
size
unsigned long long

variable_types:
addr
unsigned long long

return_type: void *


abort

decompiled_code:
void __noreturn abort()
{
exit(1);
}

canonical_decompiled_code:
void __noreturn func0()
{
exit(1);
}

original_code:
void abort(void) {
 exit(1);
}

canonical_original_code:
void abort() {
 exit(1);
}

return_type: void


abs

decompiled_code:
__int64 __fastcall abs(int a1)
{
return abs32(a1);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
return abs32(a1);
}

original_code:
int abs(int number) {
 if (number >= 0)
  return number;
 else
  return -number;
}

variable_types: number int


strerror

decompiled_code:
const char *__fastcall strerror(int a1)
{
const char *result; // rax
__int64 v2; // rdi

result = "unknown error";
v2 = (unsigned int)(a1 - 1);
if ( (unsigned int)v2 <= 0x16 )
result = CSWTCH_1[v2];
return result;
}

canonical_decompiled_code:
const char *__fastcall func0(int a1)
{
const char *result; // rax
__int64 v2; // rdi

result = "unknown error";
v2 = (unsigned int)(a1 - 1);
if ( (unsigned int)v2 <= 0x16 )
result = CSWTCH_1[v2];
return result;
}

original_code:
const char *strerror(int errnum) {
 switch (errnum) {
 case 1: return "argument list too long";
 case 2: return "access denied";
 case 3: return "resource unavailable";
 case 4: return "bad message";
 case 5: return "resource busy";
 case 6: return "no child processes";
 case 7: return "argument out of domain";
 case 8: return "file exists";
 case 9: return "illegal byte sequence";
 case 10: return "invalid argument";
 case 11: return "I/O error";
 case 12: return "is a directory";
 case 13: return "invalid path";
 case 14: return "no such file or directory";
 case 15: return "invalid executable";
 case 16: return "out of memory";
 case 17: return "no space left on device";
 case 18: return "function not supported";
 case 19: return "is not a directory";
 case 20: return "operation not permitted";
 case 21: return "result out of range";
 case 23: return "directory not empty";
 case 22:
 default: return "unknown error";
 }
}

variable_types: errnum int


lldiv

decompiled_code:
__int64 __fastcall lldiv(__int64 a1, __int64 a2)
{
return a1 / a2;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
return a1 / a2;
}

original_code:
lldiv_t lldiv(long long int numerator, long long int denominator) {
 lldiv_t result;
 result.quot = numerator / denominator;
 result.rem = numerator % denominator;
 return result;
}

canonical_original_code:
struct lldiv_t lldiv(long long numerator, long long denominator) {
 struct lldiv_t result;
 result.quot = numerator / denominator;
 result.rem = numerator % denominator;
 return result;
}

variable_types:
numerator
long long

variable_types:
denominator
long long

variable_types: struct lldiv_t { long long quot; long long rem; }


atoi

original_code:
int atoi(const char *str) {
 if ('-' == str[0])
  return atoi(&str[1]);
 int i, number;
 for (i = 0, number = 0; (_ctype_tbl[(unsigned char) (str[i])] & 0x010); ++i) {
  int digit = str[i] - '0';
  number = number * 10 + digit;
 }
 return number;
}

canonical_original_code:
int atoi(char *str) {
 if ('-' == str[0])
  return atoi(&str[1]);
 int i; int number;
 for (i = 0, number = 0; (_ctype_tbl[(unsigned char) (str[i])] & 0x010); ++i) {
  int digit = str[i] - '0';
  number = number * 10 + digit;
 }
 return number;
}

variable_types:
str
char *

variable_types: i int

variable_types: digit int


fgets

decompiled_code:
__int64 __fastcall fgets(_BYTE *a1, int a2, FILE *a3)
{
__int64 v3; // r13
_BYTE *v5; // rbx
_BYTE *v6; // rdi
int v7; // eax
__int64 result; // rax

if ( a2 == 1 )
{
*a1 = 0;
result = 0LL;
}
else
{
v3 = (__int64)&a1[a2 - 1];
v5 = a1;
do
{
v7 = fgetc(a3);
v6 = v5;
if ( v7 == -1 )
break;
*v5++ = v7;
v6 = v5;
if ( (_BYTE)v7 == 10 )
break;
}
while ( v5 != (_BYTE *)v3 );
*v6 = 0;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1, int a2, FILE *a3)
{
__int64 v3; // r13
_BYTE *v5; // rbx
_BYTE *v6; // rdi
int v7; // eax
__int64 result; // rax

if ( a2 == 1 )
{
*a1 = 0;
result = 0LL;
}
else
{
v3 = (__int64)&a1[a2 - 1];
v5 = a1;
do
{
v7 = fgetc(a3);
v6 = v5;
if ( v7 == -1 )
break;
*v5++ = v7;
v6 = v5;
if ( (_BYTE)v7 == 10 )
break;
}
while ( v5 != (_BYTE *)v3 );
*v6 = 0;
result = 0LL;
}
return result;
}

original_code:
int fgets(char *string, int length, FILE *stream) {
    size_t offset;
    for (offset = 0; offset < length - 1; ++offset) {
        int c = fgetc(stream);
        if (-1 == c) break;
        string[offset] = (char) c;
        if ('\n' == (char) c) {
            ++offset;
            break;
        }
    }
    string[offset] = 0;
    return 0;
}

canonical_original_code:
int fgets(char *string, int length, struct __file *stream) {
    unsigned long long offset;
    for (offset = 0; offset < length - 1; ++offset) {
        int c = fgetc(stream);
        if (-1 == c) break;
        string[offset] = (char) c;
        if ('\n' == (char) c) {
            ++offset;
            break;
        }
    }
    string[offset] = 0;
    return 0;
}

variable_types:
string
char *

variable_types: length int

variable_types: struct __file { unsigned long long descriptor; unsigned int lock; unsigned long long position; unsigned char flags; void *buffer; unsigned long long buffer_len; unsigned long long buffer_size; unsigned char buffer_mode; void (*callback_write)(struct __file *, unsigned long long pos, unsigned long long size, void *data); unsigned long long (*callback_read)(struct __file *, unsigned long long pos, unsigned long long size, void *data); unsigned long long (*callback_length)(struct __file *); struct __file *next; } *

variable_types:
offset
unsigned long long

variable_types: c int


fgetc

decompiled_code:
__int64 __fastcall fgetc(FILE *stream)
{
__int64 result; // rax
char ptr[9]; // [rsp+Fh] [rbp-9h] BYREF

if ( fread(ptr, 1uLL, 1uLL, stream) == 1 )
result = (unsigned int)ptr[0];
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(FILE *stream)
{
__int64 result; // rax
char ptr[9]; // [rsp+Fh] [rbp-9h] BYREF

if ( fread(ptr, 1uLL, 1uLL, stream) == 1 )
result = (unsigned int)ptr[0];
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int fgetc(FILE *fp) {
    char c;
    if (1 == fread(&c, 1, sizeof(char), fp))
        return c;
    else
        return -1;
}

canonical_original_code:
int fgetc(struct __file *fp) {
    char c;
    if (1 == fread(&c, 1, sizeof(char), fp))
        return c;
    else
        return -1;
}

variable_types: c char


strcoll

decompiled_code:
int __fastcall strcoll(const char *s1, const char *s2)
{
return strcmp(s1, s2);
}

canonical_decompiled_code:
int __fastcall func0(const char *s1, const char *s2)
{
return strcmp(s1, s2);
}

original_code:
int strcoll(const char *s1, const char *s2) {
 return strcmp(s1, s2);
}

canonical_original_code:
int strcoll(char *s1, char *s2) {
 return strcmp(s1, s2);
}


setlocale

decompiled_code:
__int64 setlocale()
{
_errno[(unsigned int)thread_id()] = 18;
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
_errno[(unsigned int)thread_id()] = 18;
return 0LL;
}

original_code:
char *setlocale(int category, const char *locale) {
 (__errno[thread_id()]) = 18;
 return 0;
}

canonical_original_code:
char *setlocale(int category, char *locale) {
 (__errno[thread_id()]) = 18;
 return 0;
}

variable_types: category int

variable_types:
locale
char *


exit

decompiled_code:
void __noreturn exit()
{
__int64 **v0; // rbx

v0 = (__int64 **)_atexit_func_list;
if ( _atexit_func_list )
{
do
{
((void (*)(void))v0[1])();
v0 = (__int64 **)*v0;
}
while ( v0 );
}
process_exit();
JUMPOUT(0x31LL);
}

canonical_decompiled_code:
void __noreturn func0()
{
__int64 **v0; // rbx

v0 = (__int64 **)_atexit_func_list;
if ( _atexit_func_list )
{
do
{
((void (*)(void))v0[1])();
v0 = (__int64 **)*v0;
}
while ( v0 );
}
process_exit();
JUMPOUT(0x31LL);
}

original_code:
void exit(int status) {
 struct __atexit_func *func = __atexit_func_list;
 while (0 != func) {
  func->function();
  func = func->next;
 }
 process_exit();
}

variable_types: status int

variable_types: struct __atexit_func { struct __atexit_func *next; void (*function)(); } *


fputc

decompiled_code:
__int64 __fastcall fputc(int a1, FILE *a2)
{
int ptr[3]; // [rsp+Ch] [rbp-Ch] BYREF

ptr[0] = a1;
fwrite(ptr, 1uLL, 1uLL, a2);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, FILE *a2)
{
int ptr[3]; // [rsp+Ch] [rbp-Ch] BYREF

ptr[0] = a1;
fwrite(ptr, 1uLL, 1uLL, a2);
return 0LL;
}

original_code:
int fputc(int c, FILE *fp) {
    fwrite(&c, sizeof(char), 1, fp);
    return 0;
}

canonical_original_code:
int fputc(int c, struct __file *fp) {
    fwrite(&c, sizeof(char), 1, fp);
    return 0;
}


repo: zrho/Hydrogen

decompiled_code:
__int64 kernel_find()
{
__int64 v0; // r12
unsigned __int16 *v1; // r15
char *v2; // rbx
__int64 result; // rax

if ( !*(_WORD *)(info_root + 156LL) )
{
LABEL_6:
screen_write("PANIC: Could not find kernel binary.", 0LL, 3LL);
while ( 1 )
;
}
v0 = 0LL;
while ( 1 )
{
v1 = (unsigned __int16 *)(info_module + 16 * v0);
v2 = (char *)(info_strings + *v1);
if ( v2 == strstr(v2, "kernel64") )
break;
if ( *(unsigned __int16 *)(info_root + 156LL) <= (unsigned __int64)++v0 )
goto LABEL_6;
}
result = *(_QWORD *)(v1 + 1);
kernel_binary = result;
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // r12
unsigned __int16 *v1; // r15
char *v2; // rbx
__int64 result; // rax

if ( !*(_WORD *)(info_root + 156LL) )
{
LABEL_6:
screen_write("PANIC: Could not find kernel binary.", 0LL, 3LL);
while ( 1 )
;
}
v0 = 0LL;
while ( 1 )
{
v1 = (unsigned __int16 *)(info_module + 16 * v0);
v2 = (char *)(info_strings + *v1);
if ( v2 == strstr(v2, "kernel64") )
break;
if ( *(unsigned __int16 *)(info_root + 156LL) <= (unsigned __int64)++v0 )
goto LABEL_6;
}
result = *(_QWORD *)(v1 + 1);
kernel_binary = result;
return result;
}

original_code:
void kernel_find(void)
{
    size_t i;
    for (i = 0; i < info_root->module_count; ++i) {
        hy_info_module_t *mod = &info_module[i];
        char *name = &info_strings[mod->name];
        if (strstr(name, "kernel64") == name) {
            kernel_binary = (void *) mod->address;
            return;
        }
    }
    do { screen_write("PANIC: " "Could not find kernel binary.", 0, 3); while (1); } while (0);;
}

canonical_original_code:
void kernel_find()
{
    unsigned long long i;
    for (i = 0; i < info_root->module_count; ++i) {
        struct hy_info_module *mod = &info_module[i];
        char *name = &info_strings[mod->name];
        if (strstr(name, "kernel64") == name) {
            kernel_binary = (void *) mod->address;
            return;
        }
    }
    do { screen_write("PANIC: " "Could not find kernel binary.", 0, 3); while (1); } while (0);;
}

variable_types:
i
unsigned long long

variable_types: struct hy_info_module { unsigned short name; unsigned long long address; unsigned int length; unsigned short padding; } *

variable_types:
name
char *


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

result = kernel_binary;
if ( *(_DWORD *)kernel_binary != 1179403647
|| *(_BYTE *)(kernel_binary + 4) != 2
|| *(_WORD *)(kernel_binary + 16) != 2 )
{
screen_write("PANIC: Kernel is not an ELF64 executable.", 0LL, 3LL);
while ( 1 )
;
}
return result;
}

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

result = kernel_binary;
if ( *(_DWORD *)kernel_binary != 1179403647
|| *(_BYTE *)(kernel_binary + 4) != 2
|| *(_WORD *)(kernel_binary + 16) != 2 )
{
screen_write("PANIC: Kernel is not an ELF64 executable.", 0LL, 3LL);
while ( 1 )
;
}
return result;
}

original_code:
void kernel_check(void)
{
    elf64_ehdr_t *ehdr = (elf64_ehdr_t *) kernel_binary;
    if (ehdr->e_ident_mag != 0x464C457F ||
        ehdr->e_ident_class != 2 ||
        ehdr->e_type != 2) {
        do { screen_write("PANIC: " "Kernel is not an ELF64 executable.", 0, 3); while (1); } while (0);;
    }
}

canonical_original_code:
void kernel_check()
{
    struct elf64_ehdr *ehdr = (elf64_ehdr_t *) kernel_binary;
    if (ehdr->e_ident_mag != 0x464C457F ||
        ehdr->e_ident_class != 2 ||
        ehdr->e_type != 2) {
        do { screen_write("PANIC: " "Kernel is not an ELF64 executable.", 0, 3); while (1); } while (0);;
    }
}

variable_types: struct elf64_ehdr { unsigned int e_ident_mag; unsigned char e_ident_class; unsigned char e_ident_data; unsigned char e_ident_version; unsigned char e_ident_osabi; unsigned char e_ident_abiversion; unsigned char e_ident_pad[7]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned long long e_entry; unsigned long long e_phoff; unsigned long long e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; } *


decompiled_code:
_DWORD *kernel_analyze()
{
__int64 v0; // rax
_DWORD *result; // rax

v0 = elf64_sym_find("hydrogen_header", kernel_binary);
if ( !v0 )
{
screen_write("PANIC: The kernel binary does not provide a Hydrogen header.", 0LL, 3LL);
while ( 1 )
;
}
result = *(_DWORD **)(v0 + 8);
kernel_header = (__int64)result;
if ( *result != 1380211016 )
{
screen_write("PANIC: Invalid magic value in kernel header.", 0LL, 3LL);
while ( 1 )
;
}
return result;
}

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

v0 = elf64_sym_find("hydrogen_header", kernel_binary);
if ( !v0 )
{
screen_write("PANIC: The kernel binary does not provide a Hydrogen header.", 0LL, 3LL);
while ( 1 )
;
}
result = *(_DWORD **)(v0 + 8);
kernel_header = (__int64)result;
if ( *result != 1380211016 )
{
screen_write("PANIC: Invalid magic value in kernel header.", 0LL, 3LL);
while ( 1 )
;
}
return result;
}

original_code:
void kernel_analyze(void)
{
    elf64_sym_t *sym = elf64_sym_find("hydrogen_header", kernel_binary);
    if (0 == sym) {
        do { screen_write("PANIC: " "The kernel binary does not provide a Hydrogen header.", 0, 3); while (1); } while (0);;
    }
    kernel_header = (hy_header_root_t *) sym->st_value;
    if (kernel_header->magic != 0x52445948) {
        do { screen_write("PANIC: " "Invalid magic value in kernel header.", 0, 3); while (1); } while (0);;
    }
}

canonical_original_code:
void kernel_analyze()
{
    struct elf64_sym *sym = elf64_sym_find("hydrogen_header", kernel_binary);
    if (0 == sym) {
        do { screen_write("PANIC: " "The kernel binary does not provide a Hydrogen header.", 0, 3); while (1); } while (0);;
    }
    kernel_header = (struct hy_header_root *) sym->st_value;
    if (kernel_header->magic != 0x52445948) {
        do { screen_write("PANIC: " "Invalid magic value in kernel header.", 0, 3); while (1); } while (0);;
    }
}

variable_types: struct elf64_sym { unsigned int st_name; unsigned char st_info; unsigned char st_other; unsigned short st_shndx; unsigned long long st_value; unsigned long long st_size; } *

user_defined_types: struct hy_header_root { unsigned int magic; unsigned int flags; unsigned long long stack_vaddr; unsigned long long info_vaddr; unsigned long long idt_vaddr; unsigned long long gdt_vaddr; unsigned long long ap_entry; unsigned long long syscall_entry; unsigned long long isr_entry_table; struct hy_header_irq irqs[16]; }

user_defined_types: struct hy_header_irq { unsigned char flags; unsigned char vector; }


decompiled_code:
_BYTE *kernel_map_stack()
{
_BYTE *result; // rax
__int64 v1; // rbx
__int64 v2; // rsi
_BYTE v3[24]; // [rsp+0h] [rbp-18h] BYREF

result = (_BYTE *)kernel_header;
v1 = *(_QWORD *)(kernel_header + 8);
if ( v1 )
{
if ( (v1 & 0xFFF) != 0 )
{
screen_write("PANIC: Virtual stack address in kernel header not page-aligned.", 0LL, 3LL);
while ( 1 )
;
}
v2 = v1 + ((unsigned int)lapic_id() << 12);
page_map(v3, v2, 258LL);
result = &v3[v2 - ((unsigned __int64)&v3[-1] & 0xFFFFFFFFFFFFF000LL)];
}
return result;
}

canonical_decompiled_code:
_BYTE *func3()
{
_BYTE *result; // rax
__int64 v1; // rbx
__int64 v2; // rsi
_BYTE v3[24]; // [rsp+0h] [rbp-18h] BYREF

result = (_BYTE *)kernel_header;
v1 = *(_QWORD *)(kernel_header + 8);
if ( v1 )
{
if ( (v1 & 0xFFF) != 0 )
{
screen_write("PANIC: Virtual stack address in kernel header not page-aligned.", 0LL, 3LL);
while ( 1 )
;
}
v2 = v1 + ((unsigned int)lapic_id() << 12);
page_map(v3, v2, 258LL);
result = &v3[v2 - ((unsigned __int64)&v3[-1] & 0xFFFFFFFFFFFFF000LL)];
}
return result;
}

original_code:
void kernel_map_stack(void)
{
    if (0 == kernel_header->stack_vaddr)
        return;
    if (0 != (kernel_header->stack_vaddr & 0xFFF)) {
        do { screen_write("PANIC: " "Virtual stack address in kernel header not page-aligned.", 0, 3); while (1); } while (0);;
    }
    uintptr_t stack_begin, stack_address, stack_offset;
    asm volatile ("mov %%rsp, %0" : "=a" (stack_address));
    stack_begin = (stack_address - 1) & ~0xFFF;
    stack_offset = stack_address - stack_begin;
    uintptr_t stack_target = kernel_header->stack_vaddr + 0x1000 * lapic_id();
    page_map(stack_address, stack_target, (1 << 1) | (1 << 8));
    asm volatile ("mov %0, %%rsp" :: "a" (stack_target + stack_offset));
}

canonical_original_code:
void kernel_map_stack()
{
    if (0 == kernel_header->stack_vaddr)
        return;
    if (0 != (kernel_header->stack_vaddr & 0xFFF)) {
        do { screen_write("PANIC: " "Virtual stack address in kernel header not page-aligned.", 0, 3); while (1); } while (0);;
    }
    unsigned long long stack_begin; unsigned long long stack_address; unsigned long long stack_offset;
    asm volatile ("mov %%rsp, %0" : "=a" (stack_address));
    stack_begin = (stack_address - 1) & ~0xFFF;
    stack_offset = stack_address - stack_begin;
    unsigned long long stack_target = kernel_header->stack_vaddr + 0x1000 * lapic_id();
    page_map(stack_address, stack_target, (1 << 1) | (1 << 8));
    asm volatile ("mov %0, %%rsp" :: "a" (stack_target + stack_offset));
}


decompiled_code:
_QWORD *kernel_map_info()
{
_QWORD *result; // rax
__int64 v1; // rbp
__int64 v2; // r12
unsigned __int64 v3; // r13
unsigned __int64 v4; // rbx
__int64 v5; // rsi
__int64 v6; // rdi

result = (_QWORD *)kernel_header;
v1 = *(_QWORD *)(kernel_header + 16);
if ( v1 )
{
result = &info_root;
v2 = info_root;
v3 = *(unsigned __int16 *)(info_root + 8LL);
if ( *(_WORD *)(info_root + 8LL) )
{
v4 = 0LL;
do
{
v5 = v1 + v4;
v6 = v2 + v4;
v4 += 4096LL;
result = (_QWORD *)page_map(v6, v5, 258LL);
}
while ( v3 > v4 );
}
}
return result;
}

canonical_decompiled_code:
_QWORD *func4()
{
_QWORD *result; // rax
__int64 v1; // rbp
__int64 v2; // r12
unsigned __int64 v3; // r13
unsigned __int64 v4; // rbx
__int64 v5; // rsi
__int64 v6; // rdi

result = (_QWORD *)kernel_header;
v1 = *(_QWORD *)(kernel_header + 16);
if ( v1 )
{
result = &info_root;
v2 = info_root;
v3 = *(unsigned __int16 *)(info_root + 8LL);
if ( *(_WORD *)(info_root + 8LL) )
{
v4 = 0LL;
do
{
v5 = v1 + v4;
v6 = v2 + v4;
v4 += 4096LL;
result = (_QWORD *)page_map(v6, v5, 258LL);
}
while ( v3 > v4 );
}
}
return result;
}

original_code:
void kernel_map_info(void)
{
    if (0 == kernel_header->info_vaddr)
        return;
    size_t length = info_root->length;
    size_t offset;
    uintptr_t physical = (uintptr_t) info_root;
    uintptr_t virtual = kernel_header->info_vaddr;
    for (offset = 0; offset < length; offset += 0x1000) {
        page_map(physical + offset, virtual + offset, (1 << 1) | (1 << 8));
    }
}

canonical_original_code:
void kernel_map_info()
{
    if (0 == kernel_header->info_vaddr)
        return;
    unsigned long long length = info_root->length;
    unsigned long long offset;
    unsigned long long physical = (uintptr_t) info_root;
    unsigned long long virtual = kernel_header->info_vaddr;
    for (offset = 0; offset < length; offset += 0x1000) {
        page_map(physical + offset, virtual + offset, (1 << 1) | (1 << 8));
    }
}

variable_types:
length
unsigned long long

variable_types:
physical
unsigned long long

variable_types:
virtual
unsigned long long


decompiled_code:
__int64 kernel_map_idt()
{
__int64 result; // rax
__int64 v1; // rsi

result = kernel_header;
v1 = *(_QWORD *)(kernel_header + 24);
if ( v1 )
{
page_map(&idt_data, v1, 258LL);
result = *(_QWORD *)(kernel_header + 24);
idt_address = result;
}
return result;
}

canonical_decompiled_code:
__int64 func5()
{
__int64 result; // rax
__int64 v1; // rsi

result = kernel_header;
v1 = *(_QWORD *)(kernel_header + 24);
if ( v1 )
{
page_map(&idt_data, v1, 258LL);
result = *(_QWORD *)(kernel_header + 24);
idt_address = result;
}
return result;
}

original_code:
void kernel_map_idt(void)
{
    if (0 == kernel_header->idt_vaddr)
        return;
    uintptr_t phys = (uintptr_t) &idt_data;
    page_map(
        phys,
        kernel_header->idt_vaddr,
        (1 << 1) | (1 << 8));
    idt_address = kernel_header->idt_vaddr;
}

canonical_original_code:
void kernel_map_idt()
{
    if (0 == kernel_header->idt_vaddr)
        return;
    unsigned long long phys = (uintptr_t) &idt_data;
    page_map(
        phys,
        kernel_header->idt_vaddr,
        (1 << 1) | (1 << 8));
    idt_address = kernel_header->idt_vaddr;
}

variable_types:
phys
unsigned long long


decompiled_code:
__int64 kernel_map_gdt()
{
__int64 result; // rax
__int64 v1; // rsi

result = kernel_header;
v1 = *(_QWORD *)(kernel_header + 32);
if ( v1 )
{
page_map(*(_QWORD *)((char *)&gdt_pointer + 2), v1, 258LL);
result = *(_QWORD *)(kernel_header + 32);
*(_QWORD *)((char *)&gdt_pointer + 2) = result;
}
return result;
}

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

result = kernel_header;
v1 = *(_QWORD *)(kernel_header + 32);
if ( v1 )
{
page_map(*(_QWORD *)((char *)&gdt_pointer + 2), v1, 258LL);
result = *(_QWORD *)(kernel_header + 32);
*(_QWORD *)((char *)&gdt_pointer + 2) = result;
}
return result;
}

original_code:
void kernel_map_gdt(void)
{
    if (0 == kernel_header->gdt_vaddr)
        return;
    uintptr_t phys = gdt_pointer.address;
    page_map(
        phys,
        kernel_header->gdt_vaddr,
        (1 << 1) | (1 << 8));
    gdt_pointer.address = kernel_header->gdt_vaddr;
}

canonical_original_code:
void kernel_map_gdt()
{
    if (0 == kernel_header->gdt_vaddr)
        return;
    unsigned long long phys = gdt_pointer.address;
    page_map(
        phys,
        kernel_header->gdt_vaddr,
        (1 << 1) | (1 << 8));
    gdt_pointer.address = kernel_header->gdt_vaddr;
}


decompiled_code:
__int64 kernel_enter_bsp()
{
return kernel_enter(*(_QWORD *)(kernel_binary + 24));
}

canonical_decompiled_code:
__int64 func7()
{
return kernel_enter(*(_QWORD *)(kernel_binary + 24));
}

original_code:
void kernel_enter_bsp(void)
{
    kernel_enter(((elf64_ehdr_t *) kernel_binary)->e_entry);
}

canonical_original_code:
void kernel_enter_bsp()
{
    kernel_enter(((struct elf64_ehdr *) kernel_binary)->e_entry);
}

user_defined_types: struct elf64_ehdr { unsigned int e_ident_mag; unsigned char e_ident_class; unsigned char e_ident_data; unsigned char e_ident_version; unsigned char e_ident_osabi; unsigned char e_ident_abiversion; unsigned char e_ident_pad[7]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned long long e_entry; unsigned long long e_phoff; unsigned long long e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; }


decompiled_code:
__int64 kernel_enter_ap()
{
__int64 v0; // rdi

v0 = *(_QWORD *)(kernel_header + 40);
if ( !v0 )
__halt();
return kernel_enter(v0);
}

canonical_decompiled_code:
__int64 func8()
{
__int64 v0; // rdi

v0 = *(_QWORD *)(kernel_header + 40);
if ( !v0 )
__halt();
return kernel_enter(v0);
}

original_code:
void kernel_enter_ap(void)
{
    if (0 == kernel_header->ap_entry) {
        while (1) { asm volatile ("hlt"); }
    } else {
        kernel_enter(kernel_header->ap_entry);
    }
}

canonical_original_code:
void kernel_enter_ap()
{
    if (0 == kernel_header->ap_entry) {
        while (1) { asm volatile ("hlt"); }
    } else {
        kernel_enter(kernel_header->ap_entry);
    }
}


decompiled_code:
__int64 main_bsp()
{
__int64 v0; // rbp
unsigned int v1; // eax

screen_write("Hydrogen v0.2b - http://github.com/farok/H2", 0LL, 0LL);
screen_write("Copyright (c) 2012 by Lukas Heidemann", 0LL, 1LL);
screen_write("-------------------------------------------------", 0LL, 2LL);
idt_load(idt_address, 4096LL);
idt_setup_loader();
info_init();
multiboot_parse();
heap_init();
acpi_parse();
ioapic_analyze();
kernel_find();
kernel_check();
elf64_load(kernel_binary);
kernel_analyze();
lapic_detect();
lapic_setup();
ioapic_setup_loader();
pic_setup();
lapic_timer_calibrate();
v0 = info_cpu;
v1 = lapic_id();
*(_WORD *)(info_cpu + 18LL * v1 + 8) = *(_WORD *)(v0 + 18LL * v1 + 8) | 2;
smp_setup();
idt_setup_kernel();
ioapic_setup_kernel();
syscall_init();
kernel_map_info();
kernel_map_stack();
kernel_map_idt();
kernel_map_gdt();
*(_QWORD *)(info_root + 50LL) = (heap_top + 4095LL) & 0xFFFFFFFFFFFFF000LL;
main_entry_barrier = 0;
return kernel_enter_bsp();
}

canonical_decompiled_code:
__int64 func0()
{
__int64 v0; // rbp
unsigned int v1; // eax

screen_write("Hydrogen v0.2b - http://github.com/farok/H2", 0LL, 0LL);
screen_write("Copyright (c) 2012 by Lukas Heidemann", 0LL, 1LL);
screen_write("-------------------------------------------------", 0LL, 2LL);
idt_load(idt_address, 4096LL);
idt_setup_loader();
info_init();
multiboot_parse();
heap_init();
acpi_parse();
ioapic_analyze();
kernel_find();
kernel_check();
elf64_load(kernel_binary);
kernel_analyze();
lapic_detect();
lapic_setup();
ioapic_setup_loader();
pic_setup();
lapic_timer_calibrate();
v0 = info_cpu;
v1 = lapic_id();
*(_WORD *)(info_cpu + 18LL * v1 + 8) = *(_WORD *)(v0 + 18LL * v1 + 8) | 2;
smp_setup();
idt_setup_kernel();
ioapic_setup_kernel();
syscall_init();
kernel_map_info();
kernel_map_stack();
kernel_map_idt();
kernel_map_gdt();
*(_QWORD *)(info_root + 50LL) = (heap_top + 4095LL) & 0xFFFFFFFFFFFFF000LL;
main_entry_barrier = 0;
return kernel_enter_bsp();
}

original_code:
void main_bsp(void)
{
    screen_write("Hydrogen v0.2b - http://github.com/farok/H2", 0, 0);
    screen_write("Copyright (c) 2012 by Lukas Heidemann", 0, 1);
    screen_write("-------------------------------------------------", 0, 2);
    idt_load(idt_address, 0x1000);
    idt_setup_loader();
    info_init();
    multiboot_parse();
    heap_init();
    acpi_parse();
    ioapic_analyze();
    kernel_find();
    kernel_check();
    elf64_load(kernel_binary);
    kernel_analyze();
    lapic_detect();
    lapic_setup();
    ioapic_setup_loader();
    pic_setup();
    lapic_timer_calibrate();
    info_cpu[lapic_id()].flags |= (1 << 1);
    smp_setup();
    idt_setup_kernel();
    ioapic_setup_kernel();
    syscall_init();
    kernel_map_info();
    kernel_map_stack();
    kernel_map_idt();
    kernel_map_gdt();
    info_root->free_paddr = (heap_top + 0xFFF) & ~0xFFF;
    main_entry_barrier = 0;
    kernel_enter_bsp();
}

canonical_original_code:
void main_bsp()
{
    screen_write("Hydrogen v0.2b - http://github.com/farok/H2", 0, 0);
    screen_write("Copyright (c) 2012 by Lukas Heidemann", 0, 1);
    screen_write("-------------------------------------------------", 0, 2);
    idt_load(idt_address, 0x1000);
    idt_setup_loader();
    info_init();
    multiboot_parse();
    heap_init();
    acpi_parse();
    ioapic_analyze();
    kernel_find();
    kernel_check();
    elf64_load(kernel_binary);
    kernel_analyze();
    lapic_detect();
    lapic_setup();
    ioapic_setup_loader();
    pic_setup();
    lapic_timer_calibrate();
    info_cpu[lapic_id()].flags |= (1 << 1);
    smp_setup();
    idt_setup_kernel();
    ioapic_setup_kernel();
    syscall_init();
    kernel_map_info();
    kernel_map_stack();
    kernel_map_idt();
    kernel_map_gdt();
    info_root->free_paddr = (heap_top + 0xFFF) & ~0xFFF;
    main_entry_barrier = 0;
    kernel_enter_bsp();
}


decompiled_code:
__int64 main_ap()
{
idt_load(&idt_data, 4096LL);
lapic_setup();
lapic_timer_calibrate();
kernel_map_stack();
syscall_init();
++smp_ready_count;
while ( main_entry_barrier == 1 )
;
return kernel_enter_ap(&idt_data, 4096LL, &main_entry_barrier);
}

canonical_decompiled_code:
__int64 func1()
{
idt_load(&idt_data, 4096LL);
lapic_setup();
lapic_timer_calibrate();
kernel_map_stack();
syscall_init();
++smp_ready_count;
while ( main_entry_barrier == 1 )
;
return kernel_enter_ap(&idt_data, 4096LL, &main_entry_barrier);
}

original_code:
void main_ap(void)
{
    idt_load((uintptr_t) &idt_data, 0x1000);
    lapic_setup();
    lapic_timer_calibrate();
    kernel_map_stack();
    syscall_init();
    ++smp_ready_count;
    while (main_entry_barrier == 1);
    kernel_enter_ap();
}

canonical_original_code:
void main_ap()
{
    idt_load((unsigned long long) &idt_data, 0x1000);
    lapic_setup();
    lapic_timer_calibrate();
    kernel_map_stack();
    syscall_init();
    ++smp_ready_count;
    while (main_entry_barrier == 1);
    kernel_enter_ap();
}


original_code:
void syscall_init(void)
{
    cpu_msr_write(0xC0000081, (((uint64_t) 0x8 << 32) | ((uint64_t) 0x18 << 48)));
    cpu_msr_write(0xC0000084, (1 << 9));
    cpu_msr_write(0xC0000083, 0);
    cpu_msr_write(0xC0000082, kernel_header->syscall_entry);
}

canonical_original_code:
void syscall_init()
{
    cpu_msr_write(0xC0000081, (((unsigned long long) 0x8 << 32) | ((unsigned long long) 0x18 << 48)));
    cpu_msr_write(0xC0000084, (1 << 9));
    cpu_msr_write(0xC0000083, 0);
    cpu_msr_write(0xC0000082, kernel_header->syscall_entry);
}


decompiled_code:
__int64 __fastcall ioapic_irq_by_gsi(int a1)
{
__int64 v1; // rdx

v1 = 0LL;
while ( *(_DWORD *)(info_root + 4 * v1 + 58) != a1 )
{
if ( ++v1 == 16 )
return 0xFFFFFFFFLL;
}
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
__int64 v1; // rdx

v1 = 0LL;
while ( *(_DWORD *)(info_root + 4 * v1 + 58) != a1 )
{
if ( ++v1 == 16 )
return 0xFFFFFFFFLL;
}
return (unsigned int)v1;
}

original_code:
static int8_t ioapic_irq_by_gsi(uint32_t gsi)
{
    size_t irq;
    for (irq = 0; irq < 16; ++irq) {
        if (info_root->irq_gsi[irq] == gsi) {
            return irq;
        }
    }
    return -1;
}

canonical_original_code:
static char ioapic_irq_by_gsi(unsigned int gsi)
{
    unsigned long long irq;
    for (irq = 0; irq < 16; ++irq) {
        if (info_root->irq_gsi[irq] == gsi) {
            return irq;
        }
    }
    return -1;
}

variable_types:
gsi
unsigned int

variable_types:
irq
unsigned long long

return_type: char


decompiled_code:
__int64 __fastcall ioapic_get_by_gsi(unsigned int a1)
{
__int64 result; // rax
__int64 v2; // rcx
unsigned int v3; // esi

if ( !*(_WORD *)(info_root + 152LL) )
return 0LL;
result = info_ioapic;
v2 = 0LL;
while ( 1 )
{
v3 = *(_DWORD *)(result + 2);
if ( v3 + *(unsigned __int16 *)(result + 6) > a1 && v3 <= a1 )
break;
++v2;
result += 16LL;
if ( v2 == *(unsigned __int16 *)(info_root + 152LL) )
return 0LL;
}
return result;
}

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

if ( !*(_WORD *)(info_root + 152LL) )
return 0LL;
result = info_ioapic;
v2 = 0LL;
while ( 1 )
{
v3 = *(_DWORD *)(result + 2);
if ( v3 + *(unsigned __int16 *)(result + 6) > a1 && v3 <= a1 )
break;
++v2;
result += 16LL;
if ( v2 == *(unsigned __int16 *)(info_root + 152LL) )
return 0LL;
}
return result;
}

original_code:
hy_info_ioapic_t *ioapic_get_by_gsi(uint32_t gsi)
{
    size_t i;
    for (i = 0; i < info_root->ioapic_count; ++i) {
        hy_info_ioapic_t *ioapic = &info_ioapic[i];
        uint32_t start = ioapic->gsi_base;
        uint32_t end = start + ioapic->gsi_count;
        if (gsi >= start && gsi < end)
            return ioapic;
    }
    return 0;
}

canonical_original_code:
struct hy_info_ioapic *ioapic_get_by_gsi(unsigned int gsi)
{
    unsigned long long i;
    for (i = 0; i < info_root->ioapic_count; ++i) {
        struct hy_info_ioapic *ioapic = &info_ioapic[i];
        unsigned int start = ioapic->gsi_base;
        unsigned int end = start + ioapic->gsi_count;
        if (gsi >= start && gsi < end)
            return ioapic;
    }
    return 0;
}

variable_types: struct hy_info_ioapic { unsigned char apic_id; unsigned char version; unsigned int gsi_base; unsigned short gsi_count; unsigned long long mmio_paddr; } *

variable_types:
start
unsigned int

variable_types:
end
unsigned int


decompiled_code:
__int64 __fastcall ioapic_register_read(unsigned __int16 a1, __int64 a2)
{
_DWORD *v2; // rax

v2 = *(_DWORD **)(a2 + 8);
*v2 = a1;
return (unsigned int)v2[4];
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int16 a1, __int64 a2)
{
_DWORD *v2; // rax

v2 = *(_DWORD **)(a2 + 8);
*v2 = a1;
return (unsigned int)v2[4];
}

original_code:
uint32_t ioapic_register_read(uint16_t index, hy_info_ioapic_t *ioapic)
{
    *(uint32_t *) ((ioapic)->mmio_paddr + 0x0) = index;
    return *(uint32_t *)((ioapic)->mmio_paddr + 0x10);
}

canonical_original_code:
unsigned int ioapic_register_read(unsigned short index, struct hy_info_ioapic *ioapic)
{
    *(unsigned int *) ((ioapic)->mmio_paddr + 0x0) = index;
    return *(unsigned int *)((ioapic)->mmio_paddr + 0x10);
}

variable_types:
index
unsigned short

return_type: unsigned int

user_defined_types: struct hy_info_ioapic { unsigned char apic_id; unsigned char version; unsigned int gsi_base; unsigned short gsi_count; unsigned long long mmio_paddr; }


decompiled_code:
__int64 __fastcall ioapic_register_write(unsigned __int16 a1, int a2, __int64 a3)
{
_DWORD *v3; // rax
unsigned int v4; // er8

v3 = *(_DWORD **)(a3 + 8);
v4 = v3[4];
*v3 = a1;
v3[4] = a2;
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int16 a1, int a2, __int64 a3)
{
_DWORD *v3; // rax
unsigned int v4; // er8

v3 = *(_DWORD **)(a3 + 8);
v4 = v3[4];
*v3 = a1;
v3[4] = a2;
return v4;
}

original_code:
uint32_t ioapic_register_write(uint16_t index, uint32_t value, hy_info_ioapic_t *ioapic)
{
    *(uint32_t *) ((ioapic)->mmio_paddr + 0x0) = index;
    uint32_t old = *(uint32_t *)((ioapic)->mmio_paddr + 0x10);
    *(uint32_t *)((ioapic)->mmio_paddr + 0x10) = value;
    return old;
}

canonical_original_code:
unsigned int ioapic_register_write(unsigned short index, unsigned int value, struct hy_info_ioapic *ioapic)
{
    *(unsigned int *) ((ioapic)->mmio_paddr + 0x0) = index;
    unsigned int old = *(uint32_t *)((ioapic)->mmio_paddr + 0x10);
    *(unsigned int *)((ioapic)->mmio_paddr + 0x10) = value;
    return old;
}

variable_types:
value
unsigned int

variable_types:
old
unsigned int


decompiled_code:
__int64 __fastcall ioapic_redir_get(__int16 a1, __int64 a2)
{
__int16 v2; // dx
__int64 (__fastcall *v3)(unsigned __int16, __int64); // r8
__int64 v4; // rax
__int64 v5; // rcx

ioapic_register_read(2 * a1 + 16, a2);
v4 = v3(v2 + 1, a2);
return v5 | (v4 << 32);
}

original_code:
uint64_t ioapic_redir_get(uint16_t index, hy_info_ioapic_t *ioapic)
{
    uint16_t register_index = 0x10 + index * 2;
    uint32_t low = ioapic_register_read(register_index, ioapic);
    uint32_t high = ioapic_register_read(register_index + 1, ioapic);
    return low | ((uint64_t) high << 32);
}

canonical_original_code:
unsigned long long ioapic_redir_get(unsigned short index, struct hy_info_ioapic *ioapic)
{
    unsigned short register_index = 0x10 + index * 2;
    unsigned int low = ioapic_register_read(register_index, ioapic);
    unsigned int high = ioapic_register_read(register_index + 1, ioapic);
    return low | ((unsigned long long) high << 32);
}

variable_types:
low
unsigned int

variable_types:
high
unsigned int


decompiled_code:
__int64 __fastcall ioapic_redir_set(__int16 a1, unsigned __int64 a2, __int64 a3)
{
__int16 v3; // cx
__int64 (__fastcall *v4)(_QWORD, unsigned __int64); // r10

ioapic_register_write(2 * a1 + 16, a2, a3);
return v4((unsigned __int16)(v3 + 1), HIDWORD(a2));
}

canonical_decompiled_code:
__int64 __fastcall func5(__int16 a1, unsigned __int64 a2, __int64 a3)
{
__int16 v3; // cx
__int64 (__fastcall *v4)(_QWORD, unsigned __int64); // r10

func3(2 * a1 + 16, a2, a3);
return v4((unsigned __int16)(v3 + 1), HIDWORD(a2));
}

original_code:
void ioapic_redir_set(uint16_t index, uint64_t redir, hy_info_ioapic_t *ioapic)
{
    uint16_t register_index = 0x10 + index * 2;
    ioapic_register_write(register_index, redir, ioapic);
    ioapic_register_write(register_index + 1, redir >> 32, ioapic);
}

canonical_original_code:
void ioapic_redir_set(unsigned short index, unsigned long long redir, struct hy_info_ioapic *ioapic)
{
    unsigned short register_index = 0x10 + index * 2;
    ioapic_register_write(register_index, redir, ioapic);
    ioapic_register_write(register_index + 1, redir >> 32, ioapic);
}

variable_types:
redir
unsigned long long


decompiled_code:
unsigned __int64 ioapic_analyze()
{
unsigned __int64 result; // rax
_QWORD *v1; // r10
unsigned __int64 v2; // rcx
__int64 (__fastcall *v3)(unsigned __int16, __int64); // r9
__int64 v4; // rax
__int64 v5; // rdx
__int64 v6; // r8

result = info_root;
if ( *(_WORD *)(info_root + 152LL) )
{
v1 = &info_ioapic;
v2 = 0LL;
v3 = ioapic_register_read;
do
{
v4 = v3(1u, *v1 + 16 * v2);
*(_BYTE *)(v5 + 1) = v4;
*(_WORD *)(v5 + 6) = BYTE2(v4) + 1;
result = *(unsigned __int16 *)(*(_QWORD *)v6 + 152LL);
}
while ( result > v2 );
}
return result;
}

canonical_decompiled_code:
unsigned __int64 func6()
{
unsigned __int64 result; // rax
_QWORD *v1; // r10
unsigned __int64 v2; // rcx
__int64 (__fastcall *v3)(unsigned __int16, __int64); // r9
__int64 v4; // rax
__int64 v5; // rdx
__int64 v6; // r8

result = info_root;
if ( *(_WORD *)(info_root + 152LL) )
{
v1 = &info_ioapic;
v2 = 0LL;
v3 = ioapic_register_read;
do
{
v4 = v3(1u, *v1 + 16 * v2);
*(_BYTE *)(v5 + 1) = v4;
*(_WORD *)(v5 + 6) = BYTE2(v4) + 1;
result = *(unsigned __int16 *)(*(_QWORD *)v6 + 152LL);
}
while ( result > v2 );
}
return result;
}

original_code:
void ioapic_analyze(void)
{
    size_t i;
    for (i = 0; i < info_root->ioapic_count; ++i) {
        hy_info_ioapic_t *ioapic = &info_ioapic[i];
        uint32_t version_reg = ioapic_register_read(0x01, ioapic);
        uint8_t version = version_reg & 0xFF;
        uint8_t max_entry = (version_reg >> 16) & 0xFF;
        ioapic->version = version;
        ioapic->gsi_count = max_entry + 1;
    }
}

canonical_original_code:
void ioapic_analyze()
{
    unsigned long long i;
    for (i = 0; i < info_root->ioapic_count; ++i) {
        struct hy_info_ioapic *ioapic = &info_ioapic[i];
        unsigned int version_reg = ioapic_register_read(0x01, ioapic);
        unsigned char version = version_reg & 0xFF;
        unsigned char max_entry = (version_reg >> 16) & 0xFF;
        ioapic->version = version;
        ioapic->gsi_count = max_entry + 1;
    }
}

variable_types:
i
unsigned long long

variable_types:
version
unsigned char


decompiled_code:
__int64 __fastcall ioapic_redir_forge(int a1, char a2)
{
__int64 v2; // rcx
unsigned __int64 v3; // rdx
char v4; // al
__int64 result; // rax

v2 = (char)ioapic_irq_by_gsi(a1);
if ( a2 )
{
v3 = 0xFF00000000000900LL;
if ( (*(_BYTE *)(kernel_header + 4LL) & 1) != 0 )
v3 = 0LL;
if ( (v2 & 0x80u) != 0LL )
{
BYTE1(v3) |= 0xA0u;
return v3 | 0x10000;
}
}
else
{
v3 = 0x10000LL;
if ( (v2 & 0x80u) != 0LL )
return 106496LL;
}
v4 = *(_BYTE *)(info_root + v2 + 122);
if ( (v4 & 1) != 0 )
v3 |= 0x2000uLL;
if ( (v4 & 2) != 0 )
v3 |= 0x8000uLL;
if ( a2 )
result = *(unsigned __int8 *)(kernel_header + 2 * (v2 + 32) + 1) | v3 | ((unsigned __int64)(*(_BYTE *)(kernel_header + 2 * (v2 + 32)) & 1) << 16);
else
result = v3 | 0x10000;
return result;
}

original_code:
uint64_t ioapic_redir_forge(uint32_t gsi, bool kernel)
{
    uint64_t redir = ( (0b000 << 8) | (1 << 16) | (0 << 11) );
    if (kernel) {
        redir = (0 == (kernel_header->flags & (1 << 0)))
                ? ( (0b001 << 8) | (1 << 11) | ((uint64_t) 0xFF << 56) ) : ( (0b000 << 8) | (0 << 11) );
    }
    int8_t irq = ioapic_irq_by_gsi(gsi);
    if (irq >= 0) {
        uint8_t flags = info_root->irq_flags[(size_t) irq];
        if (0 != ((1 << 0) & flags)) {
            redir |= (1 << 13);
        } else {
            redir |= (0 << 13);
        }
        redir |= (0 << 13);
        if (0 != ((1 << 1) & flags)) {
            redir |= (1 << 15);
        } else {
            redir |= (0 << 15);
        }
    } else {
        redir |= (1 << 15);
        redir |= (1 << 13);
    }
    if (kernel) {
        bool mask = 1;
        uint8_t vector = 0;
        if (irq >= 0) {
            hy_header_irq_t *config = &kernel_header->irqs[(size_t) irq];
            mask = (0 != (config->flags & (1 << 0)));
            vector = config->vector;
        }
        redir |= ((uint64_t) mask << 16);
        redir |= vector;
    } else {
        redir |= (1 << 16);
    }
    return redir;
}

canonical_original_code:
unsigned long long ioapic_redir_forge(unsigned int gsi, char kernel)
{
    unsigned long long redir = ( (0b000 << 8) | (1 << 16) | (0 << 11) );
    if (kernel) {
        redir = (0 == (kernel_header->flags & (1 << 0)))
                ? ( (0b001 << 8) | (1 << 11) | ((unsigned long long) 0xFF << 56) ) : ( (0b000 << 8) | (0 << 11) );
    }
    char irq = ioapic_irq_by_gsi(gsi);
    if (irq >= 0) {
        unsigned char flags = info_root->irq_flags[(size_t) irq];
        if (0 != ((1 << 0) & flags)) {
            redir |= (1 << 13);
        } else {
            redir |= (0 << 13);
        }
        redir |= (0 << 13);
        if (0 != ((1 << 1) & flags)) {
            redir |= (1 << 15);
        } else {
            redir |= (0 << 15);
        }
    } else {
        redir |= (1 << 15);
        redir |= (1 << 13);
    }
    if (kernel) {
        char mask = 1;
        unsigned char vector = 0;
        if (irq >= 0) {
            struct hy_header_irq *config = &kernel_header->irqs[(size_t) irq];
            mask = (0 != (config->flags & (1 << 0)));
            vector = config->vector;
        }
        redir |= ((unsigned long long) mask << 16);
        redir |= vector;
    } else {
        redir |= (1 << 16);
    }
    return redir;
}

variable_types: kernel char

variable_types: irq char

variable_types:
flags
unsigned char

variable_types: mask char

variable_types:
vector
unsigned char

variable_types: struct hy_header_irq { unsigned char flags; unsigned char vector; } *


decompiled_code:
unsigned __int64 __fastcall ioapic_setup(char a1)
{
unsigned __int64 result; // rax
unsigned __int64 v2; // r13
unsigned __int64 v3; // r9
__int64 v4; // r11
__int64 v5; // rax
__int16 v6; // r9
__int64 v7; // r11

result = info_root;
if ( *(_WORD *)(info_root + 152LL) )
{
v2 = 0LL;
do
{
LODWORD(v3) = 0;
v4 = info_ioapic + 16 * v2;
if ( *(_WORD *)(v4 + 6) )
{
do
{
v5 = ioapic_redir_forge((int)v3 + *(_DWORD *)(v4 + 2), a1);
ioapic_redir_set(v6, v5, v7);
}
while ( *(unsigned __int16 *)(v4 + 6) > v3 );
}
++v2;
result = *(unsigned __int16 *)(info_root + 152LL);
}
while ( result > v2 );
}
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func8(char a1)
{
unsigned __int64 result; // rax
unsigned __int64 v2; // r13
unsigned __int64 v3; // r9
__int64 v4; // r11
__int64 v5; // rax
__int16 v6; // r9
__int64 v7; // r11

result = info_root;
if ( *(_WORD *)(info_root + 152LL) )
{
v2 = 0LL;
do
{
LODWORD(v3) = 0;
v4 = info_ioapic + 16 * v2;
if ( *(_WORD *)(v4 + 6) )
{
do
{
v5 = func7((int)v3 + *(_DWORD *)(v4 + 2), a1);
func5(v6, v5, v7);
}
while ( *(unsigned __int16 *)(v4 + 6) > v3 );
}
++v2;
result = *(unsigned __int16 *)(info_root + 152LL);
}
while ( result > v2 );
}
return result;
}

original_code:
static void ioapic_setup(bool kernel)
{
    size_t i, j;
    for (i = 0; i < info_root->ioapic_count; ++i) {
        hy_info_ioapic_t *ioapic = &info_ioapic[i];
        for (j = 0; j < ioapic->gsi_count; ++j) {
            uint32_t gsi = ioapic->gsi_base + j;
            ioapic_redir_set(j, ioapic_redir_forge(gsi, kernel), ioapic);
        }
    }
}

canonical_original_code:
static void ioapic_setup(char kernel)
{
    unsigned long long i; unsigned long long j;
    for (i = 0; i < info_root->ioapic_count; ++i) {
        struct hy_info_ioapic *ioapic = &info_ioapic[i];
        for (j = 0; j < ioapic->gsi_count; ++j) {
            unsigned int gsi = ioapic->gsi_base + j;
            ioapic_redir_set(j, ioapic_redir_forge(gsi, kernel), ioapic);
        }
    }
}

variable_types:
j
unsigned long long

variable_types: struct hy_info_ioapic { unsigned char apic_id; unsigned char version; unsigned int gsi_base; unsigned short gsi_count; unsigned long long mmio_paddr; } *

variable_types:
gsi
unsigned int


decompiled_code:
unsigned __int64 ioapic_setup_kernel()
{
return ioapic_setup(1);
}

canonical_decompiled_code:
unsigned __int64 func9()
{
return func8(1);
}

original_code:
void ioapic_setup_kernel(void)
{
    ioapic_setup(1);
}

canonical_original_code:
void ioapic_setup_kernel()
{
    ioapic_setup(1);
}


decompiled_code:
unsigned __int64 ioapic_setup_loader()
{
return ioapic_setup(0);
}

canonical_decompiled_code:
unsigned __int64 func10()
{
return func8(0);
}

original_code:
void ioapic_setup_loader(void)
{
    ioapic_setup(0);
}

canonical_original_code:
void ioapic_setup_loader()
{
    ioapic_setup(0);
}


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

result = info_root;
if ( *(_WORD *)(info_root + 156LL) > 1u )
{
while ( 1 )
;
}
return result;
}

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

result = info_root;
if ( *(_WORD *)(info_root + 156LL) > 1u )
{
while ( 1 )
;
}
return result;
}

original_code:
static void heap_modules_sort(void)
{
    size_t count = info_root->module_count;
    size_t i, j;
    if (count > 1) {
        for (i = 0; i < count - 1; ++i) {
            uintptr_t min_addr = 0;
            size_t min_idx = -1;
            for (j = i; i < count; ++j) {
                hy_info_module_t *mod = &info_module[j];
                if (mod->address < min_addr) {
                    min_idx = j;
                    min_addr = mod->address;
                }
            }
            if (i != min_idx) {
                hy_info_module_t tmp;
                memcpy(&tmp, &info_module[i], sizeof(hy_info_module_t));
                memcpy(&info_module[i], &info_module[min_idx], sizeof(hy_info_module_t));
                memcpy(&info_module[min_idx], &tmp, sizeof(hy_info_module_t));
            }
        }
    }
}

canonical_original_code:
static void heap_modules_sort()
{
    unsigned long long count = info_root->module_count;
    unsigned long long i; unsigned long long j;
    if (count > 1) {
        for (i = 0; i < count - 1; ++i) {
            unsigned long long min_addr = 0;
            unsigned long long min_idx = -1;
            for (j = i; i < count; ++j) {
                struct hy_info_module *mod = &info_module[j];
                if (mod->address < min_addr) {
                    min_idx = j;
                    min_addr = mod->address;
                }
            }
            if (i != min_idx) {
                struct hy_info_module tmp;
                memcpy(&tmp, &info_module[i], sizeof(hy_info_module_t));
                memcpy(&info_module[i], &info_module[min_idx], sizeof(hy_info_module_t));
                memcpy(&info_module[min_idx], &tmp, sizeof(hy_info_module_t));
            }
        }
    }
}

variable_types:
count
unsigned long long

variable_types: struct hy_info_module { unsigned short name; unsigned long long address; unsigned int length; unsigned short padding; } *


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

result = heap_top;
heap_top += (a1 + 4095) & 0xFFFFFFFFFFFFF000LL;
return result;
}

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

result = heap_top;
heap_top += (a1 + 4095) & 0xFFFFFFFFFFFFF000LL;
return result;
}

original_code:
void *heap_alloc(size_t size)
{
 size = (size + 0xFFF) & ~0xFFF;
 uintptr_t chunk = heap_top;
 heap_top += size;
 return (void *) chunk;
}

canonical_original_code:
void *heap_alloc(unsigned long long size)
{
 size = (size + 0xFFF) & ~0xFFF;
 unsigned long long chunk = heap_top;
 heap_top += size;
 return (void *) chunk;
}

variable_types:
size
unsigned long long

variable_types:
chunk
unsigned long long


decompiled_code:
unsigned __int64 heap_modules_move()
{
unsigned __int64 result; // rax
unsigned __int64 v1; // r15
__int64 v2; // rbx
__int64 v3; // rbx
void *v4; // rbp
__int64 v5; // rax

result = info_root;
if ( *(_WORD *)(info_root + 156LL) )
{
v1 = 0LL;
do
{
v2 = v1++;
v3 = info_module + 16 * v2;
v4 = (void *)heap_alloc(*(unsigned int *)(v3 + 10));
memcpy(v4, *(const void **)(v3 + 2), *(unsigned int *)(v3 + 10));
v5 = info_root;
*(_QWORD *)(v3 + 2) = v4;
result = *(unsigned __int16 *)(v5 + 156);
}
while ( result > v1 );
}
return result;
}

canonical_decompiled_code:
unsigned __int64 func2()
{
unsigned __int64 result; // rax
unsigned __int64 v1; // r15
__int64 v2; // rbx
__int64 v3; // rbx
void *v4; // rbp
__int64 v5; // rax

result = info_root;
if ( *(_WORD *)(info_root + 156LL) )
{
v1 = 0LL;
do
{
v2 = v1++;
v3 = info_module + 16 * v2;
v4 = (void *)func1(*(unsigned int *)(v3 + 10));
memcpy(v4, *(const void **)(v3 + 2), *(unsigned int *)(v3 + 10));
v5 = info_root;
*(_QWORD *)(v3 + 2) = v4;
result = *(unsigned __int16 *)(v5 + 156);
}
while ( result > v1 );
}
return result;
}

original_code:
static void heap_modules_move(void)
{
    size_t i;
    for (i = 0; i < info_root->module_count; ++i) {
        hy_info_module_t *mod = &info_module[i];
        void *buffer = heap_alloc(mod->length);
        memcpy((void *) buffer, (void *) mod->address, mod->length);
        mod->address = (uintptr_t) buffer;
    }
}

canonical_original_code:
static void heap_modules_move()
{
    unsigned long long i;
    for (i = 0; i < info_root->module_count; ++i) {
        struct hy_info_module *mod = &info_module[i];
        void *buffer = heap_alloc(mod->length);
        memcpy((void *) buffer, (void *) mod->address, mod->length);
        mod->address = (unsigned long long) buffer;
    }
}

variable_types:
buffer
void *

user_defined_types: struct hy_info_module { unsigned short name; unsigned long long address; unsigned int length; unsigned short padding; }


decompiled_code:
unsigned __int64 heap_init()
{
heap_top = (__int64)&heap_mark;
heap_modules_sort();
return heap_modules_move();
}

canonical_decompiled_code:
unsigned __int64 func3()
{
heap_top = (__int64)&heap_mark;
func0();
return func2();
}

original_code:
void heap_init(void)
{
 extern uint8_t heap_mark;
 heap_top = (uintptr_t) &heap_mark;
 heap_modules_sort();
 heap_modules_move();
}

canonical_original_code:
void heap_init()
{
 extern unsigned char heap_mark;
 heap_top = (unsigned long long) &heap_mark;
 heap_modules_sort();
 heap_modules_move();
}


decompiled_code:
__int64 __fastcall acpi_add_cpu(unsigned int a1, int a2, char a3)
{
int v3; // er8
__int64 v4; // rdx
__int64 result; // rax
__int16 v6; // cx

v3 = a3 & 1;
v4 = info_cpu + 18LL * a1;
result = info_root;
*(_DWORD *)(v4 + 4) = a2;
v6 = *(_WORD *)(v4 + 8);
*(_DWORD *)v4 = a1;
*(_DWORD *)(v4 + 14) = 0;
if ( v3 )
{
*(_WORD *)(v4 + 8) = v6 | 1;
++*(_WORD *)(result + 148);
}
if ( *(unsigned __int16 *)(result + 150) < (unsigned __int64)(a1 + 1) )
*(_WORD *)(result + 150) = a1 + 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1, int a2, char a3)
{
int v3; // er8
__int64 v4; // rdx
__int64 result; // rax
__int16 v6; // cx

v3 = a3 & 1;
v4 = info_cpu + 18LL * a1;
result = info_root;
*(_DWORD *)(v4 + 4) = a2;
v6 = *(_WORD *)(v4 + 8);
*(_DWORD *)v4 = a1;
*(_DWORD *)(v4 + 14) = 0;
if ( v3 )
{
*(_WORD *)(v4 + 8) = v6 | 1;
++*(_WORD *)(result + 148);
}
if ( *(unsigned __int16 *)(result + 150) < (unsigned __int64)(a1 + 1) )
*(_WORD *)(result + 150) = a1 + 1;
return result;
}

original_code:
static void acpi_add_cpu(uint32_t apic_id, uint32_t acpi_id, uint32_t flags)
{
    hy_info_cpu_t *cpu = &info_cpu[apic_id];
    cpu->acpi_id = acpi_id;
    cpu->apic_id = apic_id;
    cpu->flags |= 0;
    cpu->domain = 0;
    if (0 != (flags & (1 << 0))) {
        cpu->flags |= (1 << 0);
        ++info_root->cpu_count_active;
    }
    size_t new_count = apic_id + 1;
    if (info_root->cpu_count < new_count) {
        info_root->cpu_count = new_count;
    }
}

canonical_original_code:
static void acpi_add_cpu(unsigned int apic_id, unsigned int acpi_id, unsigned int flags)
{
    struct hy_info_cpu *cpu = &info_cpu[apic_id];
    cpu->acpi_id = acpi_id;
    cpu->apic_id = apic_id;
    cpu->flags |= 0;
    cpu->domain = 0;
    if (0 != (flags & (1 << 0))) {
        cpu->flags |= (1 << 0);
        ++info_root->cpu_count_active;
    }
    unsigned long long new_count = apic_id + 1;
    if (info_root->cpu_count < new_count) {
        info_root->cpu_count = new_count;
    }
}

variable_types:
flags
unsigned int

variable_types: struct hy_info_cpu { unsigned int apic_id; unsigned int acpi_id; unsigned short flags; unsigned int lapic_timer_freq; unsigned int domain; } *


decompiled_code:
__int64 __fastcall acpi_parse_madt_x2lapic(_DWORD *a1)
{
return acpi_add_cpu(a1[1], a1[3], a1[2]);
}

canonical_decompiled_code:
__int64 __fastcall func1(_DWORD *a1)
{
return func0(a1[1], a1[3], a1[2]);
}

original_code:
static void acpi_parse_madt_x2lapic(acpi_madt_x2lapic_t *entry)
{
    acpi_add_cpu(entry->x2apic_id, entry->acpi_id, entry->flags);
}

canonical_original_code:
static void acpi_parse_madt_x2lapic(struct apci_madt_x2lapic *entry)
{
    acpi_add_cpu(entry->x2apic_id, entry->acpi_id, entry->flags);
}

variable_types: struct apci_madt_x2lapic { struct acpi_madt_entry { unsigned char type; unsigned char length; } header; unsigned short reserved; unsigned int x2apic_id; unsigned int flags; unsigned int acpi_id; } *

user_defined_types: struct apci_madt_x2lapic { struct acpi_madt_entry header; unsigned short reserved; unsigned int x2apic_id; unsigned int flags; unsigned int acpi_id; }


decompiled_code:
__int64 __fastcall acpi_parse_madt_lapic(__int64 a1)
{
return acpi_add_cpu(*(unsigned __int8 *)(a1 + 3), *(unsigned __int8 *)(a1 + 2), *(_DWORD *)(a1 + 4));
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
return func0(*(unsigned __int8 *)(a1 + 3), *(unsigned __int8 *)(a1 + 2), *(_DWORD *)(a1 + 4));
}

original_code:
static void acpi_parse_madt_lapic(acpi_madt_lapic_t *entry)
{
    acpi_add_cpu(entry->apic_id, entry->acpi_id, entry->flags);
}

canonical_original_code:
static void acpi_parse_madt_lapic(struct acpi_madt_lapic *entry)
{
    acpi_add_cpu(entry->apic_id, entry->acpi_id, entry->flags);
}

variable_types: struct acpi_madt_lapic { struct acpi_madt_entry { unsigned char type; unsigned char length; } header; unsigned char acpi_id; unsigned char apic_id; unsigned int flags; } *

user_defined_types: struct acpi_madt_lapic { struct acpi_madt_entry header; unsigned char acpi_id; unsigned char apic_id; unsigned int flags; }


decompiled_code:
__int64 __fastcall acpi_parse_madt_ioapic(__int64 a1)
{
__int64 v1; // rdx
__int64 v2; // rdx
__int64 result; // rax

v1 = *(unsigned __int16 *)(info_root + 152LL);
*(_WORD *)(info_root + 152LL) = v1 + 1;
v2 = info_ioapic + 16 * v1;
*(_BYTE *)v2 = *(_BYTE *)(a1 + 2);
*(_QWORD *)(v2 + 8) = *(unsigned int *)(a1 + 4);
result = *(unsigned int *)(a1 + 8);
*(_DWORD *)(v2 + 2) = result;
return result;
}

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

v1 = *(unsigned __int16 *)(info_root + 152LL);
*(_WORD *)(info_root + 152LL) = v1 + 1;
v2 = info_ioapic + 16 * v1;
*(_BYTE *)v2 = *(_BYTE *)(a1 + 2);
*(_QWORD *)(v2 + 8) = *(unsigned int *)(a1 + 4);
result = *(unsigned int *)(a1 + 8);
*(_DWORD *)(v2 + 2) = result;
return result;
}

original_code:
static void acpi_parse_madt_ioapic(acpi_madt_ioapic_t *entry)
{
    hy_info_ioapic_t *ioapic = &info_ioapic[info_root->ioapic_count++];
    ioapic->apic_id = entry->apic_id;
    ioapic->mmio_paddr = entry->mmio_addr;
    ioapic->gsi_base = entry->gsi_base;
}

canonical_original_code:
static void acpi_parse_madt_ioapic(struct acpi_madt_ioapic *entry)
{
    struct hy_info_ioapic *ioapic = &info_ioapic[info_root->ioapic_count++];
    ioapic->apic_id = entry->apic_id;
    ioapic->mmio_paddr = entry->mmio_addr;
    ioapic->gsi_base = entry->gsi_base;
}

variable_types: struct acpi_madt_ioapic { struct acpi_madt_entry { unsigned char type; unsigned char length; } header; unsigned char apic_id; unsigned char reserved; unsigned int mmio_addr; unsigned int gsi_base; } *

user_defined_types: struct acpi_madt_ioapic { struct acpi_madt_entry header; unsigned char apic_id; unsigned char reserved; unsigned int mmio_addr; unsigned int gsi_base; }


original_code:
static void acpi_parse_madt_iso(acpi_madt_iso_t *iso)
{
    if (iso->bus != 1 || iso->irq >= 16)
        return;
    uint8_t irq = iso->irq;
    uint16_t polarity = ((iso->flags >> 0) & 0b11);
    uint16_t trigger = ((iso->flags >> 2) & 0b11);
    info_root->irq_gsi[irq] = iso->gsi;
    if (polarity == (0b11)) {
        info_root->irq_flags[irq] |= (1 << 0);
    }
    if (trigger == (0b11)) {
        info_root->irq_flags[irq] |= (1 << 1);
    }
}

canonical_original_code:
static void acpi_parse_madt_iso(struct acpi_madt_iso *iso)
{
    if (iso->bus != 1 || iso->irq >= 16)
        return;
    unsigned char irq = iso->irq;
    unsigned short polarity = ((iso->flags >> 0) & 0b11);
    unsigned short trigger = ((iso->flags >> 2) & 0b11);
    info_root->irq_gsi[irq] = iso->gsi;
    if (polarity == (0b11)) {
        info_root->irq_flags[irq] |= (1 << 0);
    }
    if (trigger == (0b11)) {
        info_root->irq_flags[irq] |= (1 << 1);
    }
}

variable_types: struct acpi_madt_iso { struct acpi_madt_entry { unsigned char type; unsigned char length; } header; unsigned char bus; unsigned char irq; unsigned int gsi; unsigned short flags; } *

variable_types:
irq
unsigned char

variable_types:
polarity
unsigned short

variable_types:
trigger
unsigned short

user_defined_types: struct acpi_madt_iso { struct acpi_madt_entry header; unsigned char bus; unsigned char irq; unsigned int gsi; unsigned short flags; }


decompiled_code:
__int64 __fastcall acpi_parse_madt(__int64 a1)
{
__int64 *v1; // r11
__int64 v2; // rdx
__int64 *v3; // r10
__int64 v4; // r14
__int64 v5; // r9
__int64 v6; // rax
__int64 v7; // r14
__int64 v8; // rdx
unsigned __int8 v9; // al
__int64 v10; // r9
unsigned __int64 v11; // rcx
__int64 result; // rax

v1 = (__int64 *)&info_root;
v2 = info_root;
*(_QWORD *)(info_root + 10LL) = *(unsigned int *)(a1 + 36);
if ( (*(_BYTE *)(a1 + 40) & 1) != 0 )
*(_DWORD *)(v2 + 4) |= 1u;
v3 = &heap_top;
v4 = *(unsigned int *)(a1 + 4);
v5 = a1 + 44;
v6 = heap_top;
info_cpu = heap_top;
*(_WORD *)(v2 + 138) = heap_top - v2;
v7 = v4 - 44;
if ( v7 )
{
while ( 1 )
{
v8 = *(unsigned __int8 *)(v5 + 1);
v9 = *(_BYTE *)v5;
v7 -= v8;
if ( *(_BYTE *)v5 == 2 )
break;
if ( v9 > 2u )
{
if ( v9 == 9 )
{
acpi_parse_madt_x2lapic((_DWORD *)v5);
v8 = *(unsigned __int8 *)(v5 + 1);
}
goto LABEL_6;
}
if ( v9 )
{
acpi_parse_madt_ioapic(v5);
v8 = *(unsigned __int8 *)(v5 + 1);
LABEL_6:
v5 += v8;
if ( !v7 )
goto LABEL_11;
}
else
{
acpi_parse_madt_lapic(v5);
v5 = *(unsigned __int8 *)(v10 + 1) + v10;
if ( !v7 )
{
LABEL_11:
v2 = *v1;
v6 = *v3;
goto LABEL_12;
}
}
}
acpi_parse_madt_iso(v5);
v8 = *(unsigned __int8 *)(v5 + 1);
goto LABEL_6;
}
LABEL_12:
v11 = (18LL * *(unsigned __int16 *)(v2 + 150) + 4095) & 0xFFFFFFFFFFFFF000LL;
*(_WORD *)(v2 + 8) += v11;
result = v11 + v6;
*v3 = result;
return result;
}

original_code:
static void acpi_parse_madt(acpi_madt_t *madt)
{
    info_root->lapic_paddr = madt->lapic_paddr;
    if ((madt->flags & (1 << 0)) != 0) {
        info_root->flags |= (1 << 0);
    }
    acpi_madt_entry_t *entry = (acpi_madt_entry_t *) ((uintptr_t) madt + sizeof (acpi_madt_t));
    size_t size_left = madt->header.length - sizeof (acpi_madt_t);
    info_root->cpu_offset = heap_top - (uintptr_t) info_root;
    info_cpu = (hy_info_cpu_t *) heap_top;
    while (size_left > 0) {
        size_left -= entry->length;
        switch (entry->type) {
        case 0:
            acpi_parse_madt_lapic((acpi_madt_lapic_t *) entry);
            break;
        case 9:
            acpi_parse_madt_x2lapic((acpi_madt_x2lapic_t *) entry);
            break;
        case 1:
            acpi_parse_madt_ioapic((acpi_madt_ioapic_t *) entry);
            break;
        case 2:
            acpi_parse_madt_iso((acpi_madt_iso_t *) entry);
            break;
        }
        entry = (acpi_madt_entry_t *) ((uintptr_t) entry + entry->length);
    }
    size_t cpu_length = sizeof(hy_info_cpu_t) * info_root->cpu_count;
    cpu_length = (cpu_length + 0xFFF) & ~0xFFF;
    heap_top += cpu_length;
    info_root->length += cpu_length;
}

canonical_original_code:
static void acpi_parse_madt(struct acpi_madt *madt)
{
    info_root->lapic_paddr = madt->lapic_paddr;
    if ((madt->flags & (1 << 0)) != 0) {
        info_root->flags |= (1 << 0);
    }
    struct acpi_madt_entry *entry = (acpi_madt_entry_t *) ((uintptr_t) madt + sizeof (acpi_madt_t));
    unsigned long long size_left = madt->header.length - sizeof (acpi_madt_t);
    info_root->cpu_offset = heap_top - (unsigned long long) info_root;
    info_cpu = (struct hy_info_cpu *) heap_top;
    while (size_left > 0) {
        size_left -= entry->length;
        switch (entry->type) {
        case 0:
            acpi_parse_madt_lapic((struct acpi_madt_lapic *) entry);
            break;
        case 9:
            acpi_parse_madt_x2lapic((struct apci_madt_x2lapic *) entry);
            break;
        case 1:
            acpi_parse_madt_ioapic((struct acpi_madt_ioapic *) entry);
            break;
        case 2:
            acpi_parse_madt_iso((struct acpi_madt_iso *) entry);
            break;
        }
        entry = (struct acpi_madt_entry *) ((unsigned long long) entry + entry->length);
    }
    unsigned long long cpu_length = sizeof(hy_info_cpu_t) * info_root->cpu_count;
    cpu_length = (cpu_length + 0xFFF) & ~0xFFF;
    heap_top += cpu_length;
    info_root->length += cpu_length;
}

variable_types: struct acpi_madt { struct acpi_sdt_header { unsigned int signature; unsigned int length; unsigned char revision; unsigned char checksum; unsigned char oemid[6]; unsigned char oem_tbl_id[8]; unsigned int oem_rev; unsigned int creator_id; unsigned int creator_rev; } header; unsigned int lapic_paddr; unsigned int flags; } *

variable_types: struct acpi_madt_entry { unsigned char type; unsigned char length; } *

user_defined_types: struct acpi_madt { struct acpi_sdt_header header; unsigned int lapic_paddr; unsigned int flags; }

user_defined_types: struct hy_info_cpu { unsigned int apic_id; unsigned int acpi_id; unsigned short flags; unsigned int lapic_timer_freq; unsigned int domain; }


original_code:
static void acpi_parse_srat_lapic(acpi_srat_lapic_t *entry)
{
    if (0 == (entry->flags & (1 << 0)))
        return;
    uint32_t domain = entry->domain_low;
    domain |= entry->domain_high[0] << 8;
    domain |= entry->domain_high[1] << 16;
    domain |= entry->domain_high[2] << 24;
    info_cpu[entry->apic_id].domain = domain;
}

canonical_original_code:
static void acpi_parse_srat_lapic(struct acpi_srat_lapic *entry)
{
    if (0 == (entry->flags & (1 << 0)))
        return;
    unsigned int domain = entry->domain_low;
    domain |= entry->domain_high[0] << 8;
    domain |= entry->domain_high[1] << 16;
    domain |= entry->domain_high[2] << 24;
    info_cpu[entry->apic_id].domain = domain;
}

variable_types: struct acpi_srat_lapic { struct acpi_srat_entry { unsigned char type; unsigned char length; } header; unsigned char domain_low; unsigned char apic_id; unsigned int flags; unsigned char sapic_eid; unsigned char domain_high[3]; unsigned int clock_domain; } *

variable_types:
domain
unsigned int

user_defined_types: struct acpi_srat_lapic { struct acpi_srat_entry header; unsigned char domain_low; unsigned char apic_id; unsigned int flags; unsigned char sapic_eid; unsigned char domain_high[3]; unsigned int clock_domain; }


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

if ( (*(_BYTE *)(a1 + 12) & 1) != 0 )
{
result = info_cpu;
*(_DWORD *)(info_cpu + 18LL * *(unsigned int *)(a1 + 8) + 14) = *(_DWORD *)(a1 + 4);
}
return result;
}

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

if ( (*(_BYTE *)(a1 + 12) & 1) != 0 )
{
result = info_cpu;
*(_DWORD *)(info_cpu + 18LL * *(unsigned int *)(a1 + 8) + 14) = *(_DWORD *)(a1 + 4);
}
return result;
}

original_code:
static void acpi_parse_srat_x2lapic(acpi_srat_x2lapic_t *entry)
{
    if (0 == (entry->flags & (1 << 0)))
        return;
    info_cpu[entry->x2apic_id].domain = entry->domain;
}

canonical_original_code:
static void acpi_parse_srat_x2lapic(struct acpi_srat_x2lapic *entry)
{
    if (0 == (entry->flags & (1 << 0)))
        return;
    info_cpu[entry->x2apic_id].domain = entry->domain;
}

variable_types: struct acpi_srat_x2lapic { struct acpi_srat_entry { unsigned char type; unsigned char length; } header; unsigned short reserved0; unsigned int domain; unsigned int x2apic_id; unsigned int flags; unsigned int clock_domain; unsigned int reserved1; } *

user_defined_types: struct acpi_srat_x2lapic { struct acpi_srat_entry header; unsigned short reserved0; unsigned int domain; unsigned int x2apic_id; unsigned int flags; unsigned int clock_domain; unsigned int reserved1; }


decompiled_code:
void __fastcall acpi_parse_srat(__int64 a1)
{
unsigned __int8 *v1; // rsi
void (__fastcall *v2)(__int64); // r10
void (__fastcall *v3)(unsigned __int8 *); // r9
__int64 i; // r8
unsigned __int8 v5; // al
unsigned __int8 *v6; // rdi
__int64 v7; // rdi

v1 = (unsigned __int8 *)(a1 + 48);
v2 = (void (__fastcall *)(__int64))acpi_parse_srat_x2lapic;
v3 = (void (__fastcall *)(unsigned __int8 *))acpi_parse_srat_lapic;
LABEL_2:
for ( i = *(unsigned int *)(a1 + 4) - 48LL; i; i -= 40LL )
{
while ( 1 )
{
v5 = *v1;
if ( *v1 == 1 )
break;
if ( v5 == 2 )
{
v7 = (__int64)v1;
v1 += 24;
v2(v7);
goto LABEL_2;
}
if ( v5 )
goto LABEL_2;
v6 = v1;
v1 += 16;
v3(v6);
if ( !i )
return;
}
v1 += 40;
}
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1)
{
unsigned __int8 *v1; // rsi
void (__fastcall *v2)(__int64); // r10
void (__fastcall *v3)(unsigned __int8 *); // r9
__int64 i; // r8
unsigned __int8 v5; // al
unsigned __int8 *v6; // rdi
__int64 v7; // rdi

v1 = (unsigned __int8 *)(a1 + 48);
v2 = (void (__fastcall *)(__int64))acpi_parse_srat_x2lapic;
v3 = (void (__fastcall *)(unsigned __int8 *))acpi_parse_srat_lapic;
LABEL_2:
for ( i = *(unsigned int *)(a1 + 4) - 48LL; i; i -= 40LL )
{
while ( 1 )
{
v5 = *v1;
if ( *v1 == 1 )
break;
if ( v5 == 2 )
{
v7 = (__int64)v1;
v1 += 24;
v2(v7);
goto LABEL_2;
}
if ( v5 )
goto LABEL_2;
v6 = v1;
v1 += 16;
v3(v6);
if ( !i )
return;
}
v1 += 40;
}
}

original_code:
static void acpi_parse_srat(acpi_srat_t *srat)
{
    acpi_srat_entry_t *entry = (acpi_srat_entry_t *) ((uintptr_t) srat + sizeof(acpi_srat_t));
    size_t length_remaining = srat->header.length - sizeof(acpi_srat_t);
    while (length_remaining > 0) {
        switch (entry->type) {
        case 0:
            acpi_parse_srat_lapic((acpi_srat_lapic_t *) entry);
            length_remaining -= sizeof(acpi_srat_lapic_t);
            entry = (acpi_srat_entry_t *)((char *)entry + sizeof(acpi_srat_lapic_t));
            break;
        case 1:
            length_remaining -= sizeof(acpi_srat_memory_t);
            entry = (acpi_srat_entry_t *)((char *)entry + sizeof(acpi_srat_memory_t));
            break;
        case 2:
            acpi_parse_srat_x2lapic((acpi_srat_x2lapic_t *) entry);
            length_remaining -= sizeof(acpi_srat_x2lapic_t);
            entry = (acpi_srat_entry_t *)((char *)entry + sizeof(acpi_srat_x2lapic_t));
            break;
        }
    }
}

canonical_original_code:
static void acpi_parse_srat(struct acpi_srat *srat)
{
    struct acpi_srat_entry *entry = (acpi_srat_entry_t *) ((uintptr_t) srat + sizeof(acpi_srat_t));
    unsigned long long length_remaining = srat->header.length - sizeof(acpi_srat_t);
    while (length_remaining > 0) {
        switch (entry->type) {
        case 0:
            acpi_parse_srat_lapic((struct acpi_srat_lapic *) entry);
            length_remaining -= sizeof(acpi_srat_lapic_t);
            entry = (struct acpi_srat_entry *)((char *)entry + sizeof(acpi_srat_lapic_t));
            break;
        case 1:
            length_remaining -= sizeof(acpi_srat_memory_t);
            entry = (struct acpi_srat_entry *)((char *)entry + sizeof(acpi_srat_memory_t));
            break;
        case 2:
            acpi_parse_srat_x2lapic((struct acpi_srat_x2lapic *) entry);
            length_remaining -= sizeof(acpi_srat_x2lapic_t);
            entry = (struct acpi_srat_entry *)((char *)entry + sizeof(acpi_srat_x2lapic_t));
            break;
        }
    }
}

variable_types: struct acpi_srat { struct acpi_sdt_header { unsigned int signature; unsigned int length; unsigned char revision; unsigned char checksum; unsigned char oemid[6]; unsigned char oem_tbl_id[8]; unsigned int oem_rev; unsigned int creator_id; unsigned int creator_rev; } header; unsigned int reserved0; unsigned long long reserved1; } *

variable_types: struct acpi_srat_entry { unsigned char type; unsigned char length; } *

user_defined_types: struct acpi_srat { struct acpi_sdt_header header; unsigned int reserved0; unsigned long long reserved1; }


decompiled_code:
bool __fastcall acpi_check(_BYTE *a1, __int64 a2)
{
_BYTE *v2; // rsi
char v3; // al

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

canonical_decompiled_code:
bool __fastcall func9(_BYTE *a1, __int64 a2)
{
_BYTE *v2; // rsi
char v3; // al

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

original_code:
bool acpi_check(void *table, size_t length)
{
    uint8_t *bytes = (uint8_t *) table;
    uint8_t sum = 0;
    size_t i;
    for (i = 0; i < length; ++i) {
        sum += bytes[i];
    }
    return (0 == sum);
}

canonical_original_code:
char acpi_check(void *table, unsigned long long length)
{
    unsigned char *bytes = (uint8_t *) table;
    unsigned char sum = 0;
    unsigned long long i;
    for (i = 0; i < length; ++i) {
        sum += bytes[i];
    }
    return (0 == sum);
}

variable_types:
table
void *

variable_types:
length
unsigned long long

variable_types:
bytes
unsigned char *

variable_types:
sum
unsigned char

variable_types:
i
unsigned long long


decompiled_code:
char __fastcall acpi_parse_table(void *s1)
{
char result; // al

result = acpi_check(s1, *((unsigned int *)s1 + 1));
if ( result )
{
if ( !(unsigned __int8)memcmp(s1, "MADT", 4uLL) && !(unsigned __int8)memcmp(s1, "APIC", 4uLL) )
{
result = memcmp(s1, "SRAT", 4uLL);
if ( result )
{
result = (char)s1;
acpi_srat = (__int64)s1;
}
}
else
{
result = (char)s1;
acpi_madt = (__int64)s1;
}
}
return result;
}

canonical_decompiled_code:
char __fastcall func10(void *s1)
{
char result; // al

result = func9(s1, *((unsigned int *)s1 + 1));
if ( result )
{
if ( !(unsigned __int8)memcmp(s1, "MADT", 4uLL) && !(unsigned __int8)memcmp(s1, "APIC", 4uLL) )
{
result = memcmp(s1, "SRAT", 4uLL);
if ( result )
{
result = (char)s1;
acpi_srat = (__int64)s1;
}
}
else
{
result = (char)s1;
acpi_madt = (__int64)s1;
}
}
return result;
}

original_code:
static void acpi_parse_table(acpi_sdt_header_t *table)
{
    if (!acpi_check(table, table->length))
        return;
    if (memcmp(&table->signature, "MADT", 4) ||
            memcmp(&table->signature, "APIC", 4)) {
        acpi_madt = (acpi_madt_t *) table;
    } else if (memcmp(&table->signature, "SRAT", 4)) {
        acpi_srat = (acpi_srat_t *) table;
    }
}

canonical_original_code:
static void acpi_parse_table(struct acpi_sdt_header *table)
{
    if (!acpi_check(table, table->length))
        return;
    if (memcmp(&table->signature, "MADT", 4) ||
            memcmp(&table->signature, "APIC", 4)) {
        acpi_madt = (struct acpi_madt *) table;
    } else if (memcmp(&table->signature, "SRAT", 4)) {
        acpi_srat = (struct acpi_srat *) table;
    }
}

variable_types: struct acpi_sdt_header { unsigned int signature; unsigned int length; unsigned char revision; unsigned char checksum; unsigned char oemid[6]; unsigned char oem_tbl_id[8]; unsigned int oem_rev; unsigned int creator_id; unsigned int creator_rev; } *


decompiled_code:
char __fastcall acpi_parse_xsdt(__int64 a1)
{
char result; // al
__int64 v2; // rdx
void **v3; // rbx
unsigned __int64 v4; // rdx
void **v5; // r12
void *v6; // rdi

result = acpi_check((_BYTE *)a1, *(unsigned int *)(a1 + 4));
if ( !result )
{
screen_write("PANIC: ACPI: XSDT is invalid.", 0LL, 3LL);
while ( 1 )
;
}
v3 = (void **)(a1 + 36);
v4 = (unsigned __int64)(v2 - 36) >> 3;
if ( v4 )
{
v5 = &v3[v4];
do
{
v6 = *v3++;
result = acpi_parse_table(v6);
}
while ( v3 != v5 );
}
return result;
}

canonical_decompiled_code:
char __fastcall func11(__int64 a1)
{
char result; // al
__int64 v2; // rdx
void **v3; // rbx
unsigned __int64 v4; // rdx
void **v5; // r12
void *v6; // rdi

result = func9((_BYTE *)a1, *(unsigned int *)(a1 + 4));
if ( !result )
{
screen_write("PANIC: ACPI: XSDT is invalid.", 0LL, 3LL);
while ( 1 )
;
}
v3 = (void **)(a1 + 36);
v4 = (unsigned __int64)(v2 - 36) >> 3;
if ( v4 )
{
v5 = &v3[v4];
do
{
v6 = *v3++;
result = func10(v6);
}
while ( v3 != v5 );
}
return result;
}

original_code:
static void acpi_parse_xsdt(acpi_sdt_header_t *xsdt)
{
    if (!acpi_check(xsdt, xsdt->length)) {
        do { screen_write("PANIC: " "ACPI: XSDT is invalid.", 0, 3); while (1); } while (0);;
    }
    size_t count = (xsdt->length - sizeof (acpi_sdt_header_t)) / 8;
    uint64_t *entry = (uint64_t *) ((uintptr_t) xsdt + sizeof (acpi_sdt_header_t));
    size_t i;
    for (i = 0; i < count; ++i) {
        acpi_parse_table((acpi_sdt_header_t *) entry[i]);
    }
}

canonical_original_code:
static void acpi_parse_xsdt(struct acpi_sdt_header *xsdt)
{
    if (!acpi_check(xsdt, xsdt->length)) {
        do { screen_write("PANIC: " "ACPI: XSDT is invalid.", 0, 3); while (1); } while (0);;
    }
    unsigned long long count = (xsdt->length - sizeof (acpi_sdt_header_t)) / 8;
    unsigned long long *entry = (uint64_t *) ((uintptr_t) xsdt + sizeof (acpi_sdt_header_t));
    unsigned long long i;
    for (i = 0; i < count; ++i) {
        acpi_parse_table((struct acpi_sdt_header *) entry[i]);
    }
}

variable_types:
count
unsigned long long

variable_types:
entry
unsigned long long *

user_defined_types: struct acpi_sdt_header { unsigned int signature; unsigned int length; unsigned char revision; unsigned char checksum; unsigned char oemid[6]; unsigned char oem_tbl_id[8]; unsigned int oem_rev; unsigned int creator_id; unsigned int creator_rev; }


decompiled_code:
char __fastcall acpi_parse_rsdt(__int64 a1)
{
char result; // al
__int64 v2; // rdx
unsigned int *v3; // rbx
unsigned __int64 v4; // rdx
unsigned int *v5; // r12
void *v6; // rdi

result = acpi_check((_BYTE *)a1, *(unsigned int *)(a1 + 4));
if ( !result )
{
screen_write("PANIC: ACPI: RSDT is invalid.", 0LL, 3LL);
while ( 1 )
;
}
v3 = (unsigned int *)(a1 + 36);
v4 = (unsigned __int64)(v2 - 36) >> 2;
if ( v4 )
{
v5 = &v3[v4];
do
{
v6 = (void *)*v3++;
result = acpi_parse_table(v6);
}
while ( v3 != v5 );
}
return result;
}

canonical_decompiled_code:
char __fastcall func12(__int64 a1)
{
char result; // al
__int64 v2; // rdx
unsigned int *v3; // rbx
unsigned __int64 v4; // rdx
unsigned int *v5; // r12
void *v6; // rdi

result = func9((_BYTE *)a1, *(unsigned int *)(a1 + 4));
if ( !result )
{
screen_write("PANIC: ACPI: RSDT is invalid.", 0LL, 3LL);
while ( 1 )
;
}
v3 = (unsigned int *)(a1 + 36);
v4 = (unsigned __int64)(v2 - 36) >> 2;
if ( v4 )
{
v5 = &v3[v4];
do
{
v6 = (void *)*v3++;
result = func10(v6);
}
while ( v3 != v5 );
}
return result;
}

original_code:
static void acpi_parse_rsdt(acpi_sdt_header_t *rsdt)
{
    if (!acpi_check(rsdt, rsdt->length)) {
        do { screen_write("PANIC: " "ACPI: RSDT is invalid.", 0, 3); while (1); } while (0);;
    }
    size_t count = (rsdt->length - sizeof (acpi_sdt_header_t)) / 4;
    uint32_t *entry = (uint32_t *) ((uintptr_t) rsdt + sizeof (acpi_sdt_header_t));
    size_t i;
    for (i = 0; i < count; ++i) {
        acpi_parse_table((acpi_sdt_header_t *) (uintptr_t) entry[i]);
    }
}

canonical_original_code:
static void acpi_parse_rsdt(struct acpi_sdt_header *rsdt)
{
    if (!acpi_check(rsdt, rsdt->length)) {
        do { screen_write("PANIC: " "ACPI: RSDT is invalid.", 0, 3); while (1); } while (0);;
    }
    unsigned long long count = (rsdt->length - sizeof (acpi_sdt_header_t)) / 4;
    unsigned int *entry = (uint32_t *) ((uintptr_t) rsdt + sizeof (acpi_sdt_header_t));
    unsigned long long i;
    for (i = 0; i < count; ++i) {
        acpi_parse_table((struct acpi_sdt_header *) (unsigned long long) entry[i]);
    }
}

variable_types:
entry
unsigned int *


decompiled_code:
char __fastcall acpi_parse_rsdp(__int64 a1)
{
char result; // al

if ( *(_BYTE *)(a1 + 15) )
result = acpi_parse_xsdt(*(_QWORD *)(a1 + 24));
else
result = acpi_parse_rsdt(*(unsigned int *)(a1 + 16));
return result;
}

canonical_decompiled_code:
char __fastcall func13(__int64 a1)
{
char result; // al

if ( *(_BYTE *)(a1 + 15) )
result = func11(*(_QWORD *)(a1 + 24));
else
result = func12(*(unsigned int *)(a1 + 16));
return result;
}

original_code:
static void acpi_parse_rsdp(acpi_rsdp_t *rsdp)
{
    if (rsdp->revision > 0) {
        acpi_parse_xsdt((acpi_sdt_header_t *) rsdp->xsdt_addr);
    } else {
        acpi_parse_rsdt((acpi_sdt_header_t *) (uintptr_t) rsdp->rsdt_addr);
    }
}

canonical_original_code:
static void acpi_parse_rsdp(struct acpi_rsdp *rsdp)
{
    if (rsdp->revision > 0) {
        acpi_parse_xsdt((struct acpi_sdt_header *) rsdp->xsdt_addr);
    } else {
        acpi_parse_rsdt((struct acpi_sdt_header *) (unsigned long long) rsdp->rsdt_addr);
    }
}

variable_types: struct acpi_rsdp { unsigned char signature[8]; unsigned char checksum; unsigned char oemid[6]; unsigned char revision; unsigned int rsdt_addr; unsigned int length; unsigned long long xsdt_addr; unsigned char xchecksum; unsigned char reserved[3]; } *


decompiled_code:
unsigned __int64 __fastcall acpi_find_rsdp(__int64 a1, __int64 a2)
{
unsigned __int64 v2; // r13
unsigned __int64 v3; // rbp
unsigned __int64 v4; // r12

v2 = (a1 + 15) & 0xFFFFFFFFFFFFFFF0LL;
if ( a1 + a2 == v2 )
return 0LL;
v3 = 0LL;
while ( 1 )
{
v4 = v2 + v3;
if ( (unsigned __int8)memcmp((const void *)(v2 + v3), "RSD PTR ", 8uLL) )
{
if ( acpi_check((_BYTE *)(v2 + v3), 20LL)
&& (!*(_BYTE *)(v4 + 15) || acpi_check((_BYTE *)(v2 + v3), *(unsigned int *)(v4 + 20))) )
{
break;
}
}
v3 += 16LL;
if ( a1 + a2 - v2 <= v3 )
return 0LL;
}
return v4;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func14(__int64 a1, __int64 a2)
{
unsigned __int64 v2; // r13
unsigned __int64 v3; // rbp
unsigned __int64 v4; // r12

v2 = (a1 + 15) & 0xFFFFFFFFFFFFFFF0LL;
if ( a1 + a2 == v2 )
return 0LL;
v3 = 0LL;
while ( 1 )
{
v4 = v2 + v3;
if ( (unsigned __int8)memcmp((const void *)(v2 + v3), "RSD PTR ", 8uLL) )
{
if ( func9((_BYTE *)(v2 + v3), 20LL)
&& (!*(_BYTE *)(v4 + 15) || func9((_BYTE *)(v2 + v3), *(unsigned int *)(v4 + 20))) )
{
break;
}
}
v3 += 16LL;
if ( a1 + a2 - v2 <= v3 )
return 0LL;
}
return v4;
}

original_code:
acpi_rsdp_t *acpi_find_rsdp(uintptr_t begin, size_t length)
{
    uintptr_t begin_aligned = (begin + 15) & ~15;
    length -= begin_aligned - begin;
    begin = begin_aligned;
    size_t offset;
    for (offset = 0; offset < length; offset += 16) {
        uintptr_t address = begin + offset;
        uint64_t *signature = (uint64_t *) address;
        if (!memcmp(signature, "RSD PTR ", 8))
            continue;
        acpi_rsdp_t *rsdp = (acpi_rsdp_t *) address;
        if (!acpi_check(rsdp, 20))
            continue;
        if (rsdp->revision >= 1) {
            if (!acpi_check(rsdp, rsdp->length))
                continue;
        }
        return rsdp;
    }
    return 0;
}

canonical_original_code:
struct acpi_rsdp *acpi_find_rsdp(unsigned long long begin, unsigned long long length)
{
    unsigned long long begin_aligned = (begin + 15) & ~15;
    length -= begin_aligned - begin;
    begin = begin_aligned;
    unsigned long long offset;
    for (offset = 0; offset < length; offset += 16) {
        unsigned long long address = begin + offset;
        unsigned long long *signature = (uint64_t *) address;
        if (!memcmp(signature, "RSD PTR ", 8))
            continue;
        struct acpi_rsdp *rsdp = (acpi_rsdp_t *) address;
        if (!acpi_check(rsdp, 20))
            continue;
        if (rsdp->revision >= 1) {
            if (!acpi_check(rsdp, rsdp->length))
                continue;
        }
        return rsdp;
    }
    return 0;
}

variable_types:
begin
unsigned long long

variable_types:
offset
unsigned long long

variable_types:
address
unsigned long long

variable_types:
signature
unsigned long long *

return_type: struct acpi_rsdp { unsigned char signature[8]; unsigned char checksum; unsigned char oemid[6]; unsigned char revision; unsigned int rsdt_addr; unsigned int length; unsigned long long xsdt_addr; unsigned char xchecksum; unsigned char reserved[3]; } *


decompiled_code:
__int64 acpi_parse()
{
__int64 v0; // rax
__int64 result; // rax

v0 = acpi_find_rsdp(917504LL, 0x20000LL);
if ( !v0 )
{
screen_write("PANIC: ACPI: Could not find RSDP.", 0LL, 3LL);
while ( 1 )
;
}
*(_QWORD *)(info_root + 18LL) = v0;
acpi_parse_rsdp(v0);
if ( !acpi_madt )
{
screen_write("PANIC: No MADT ACPI table found.", 0LL, 3LL);
while ( 1 )
;
}
acpi_parse_madt(acpi_madt);
if ( acpi_srat )
acpi_parse_srat(acpi_srat);
result = info_root;
if ( !*(_WORD *)(info_root + 150LL) )
{
screen_write("PANIC: No CPU information in ACPI tables.", 0LL, 3LL);
while ( 1 )
;
}
if ( !*(_WORD *)(info_root + 152LL) )
{
screen_write("PANIC: No I/O APIC found in ACPI tables.", 0LL, 3LL);
while ( 1 )
;
}
return result;
}

canonical_decompiled_code:
__int64 func15()
{
__int64 v0; // rax
__int64 result; // rax

v0 = func14(917504LL, 0x20000LL);
if ( !v0 )
{
screen_write("PANIC: ACPI: Could not find RSDP.", 0LL, 3LL);
while ( 1 )
;
}
*(_QWORD *)(info_root + 18LL) = v0;
func13(v0);
if ( !acpi_madt )
{
screen_write("PANIC: No MADT ACPI table found.", 0LL, 3LL);
while ( 1 )
;
}
func5(acpi_madt);
if ( acpi_srat )
func8(acpi_srat);
result = info_root;
if ( !*(_WORD *)(info_root + 150LL) )
{
screen_write("PANIC: No CPU information in ACPI tables.", 0LL, 3LL);
while ( 1 )
;
}
if ( !*(_WORD *)(info_root + 152LL) )
{
screen_write("PANIC: No I/O APIC found in ACPI tables.", 0LL, 3LL);
while ( 1 )
;
}
return result;
}

original_code:
void acpi_parse(void)
{
    acpi_rsdp_t *rsdp = acpi_find_rsdp(0xE0000, 0x100000 - 0xE0000);
    if (0 == rsdp) {
        do { screen_write("PANIC: " "ACPI: Could not find RSDP.", 0, 3); while (1); } while (0);;
    }
    info_root->rsdp_paddr = (uintptr_t) rsdp;
    acpi_parse_rsdp(rsdp);
    if (0 == acpi_madt) {
        do { screen_write("PANIC: " "No MADT ACPI table found.", 0, 3); while (1); } while (0);;
    }
    acpi_parse_madt(acpi_madt);
    if (0 != acpi_srat) {
        acpi_parse_srat(acpi_srat);
    }
    if (0 == info_root->cpu_count) {
        do { screen_write("PANIC: " "No CPU information in ACPI tables.", 0, 3); while (1); } while (0);;
    }
    if (0 == info_root->ioapic_count) {
        do { screen_write("PANIC: " "No I/O APIC found in ACPI tables.", 0, 3); while (1); } while (0);;
    }
}

canonical_original_code:
void acpi_parse()
{
    struct acpi_rsdp *rsdp = acpi_find_rsdp(0xE0000, 0x100000 - 0xE0000);
    if (0 == rsdp) {
        do { screen_write("PANIC: " "ACPI: Could not find RSDP.", 0, 3); while (1); } while (0);;
    }
    info_root->rsdp_paddr = (unsigned long long) rsdp;
    acpi_parse_rsdp(rsdp);
    if (0 == acpi_madt) {
        do { screen_write("PANIC: " "No MADT ACPI table found.", 0, 3); while (1); } while (0);;
    }
    acpi_parse_madt(acpi_madt);
    if (0 != acpi_srat) {
        acpi_parse_srat(acpi_srat);
    }
    if (0 == info_root->cpu_count) {
        do { screen_write("PANIC: " "No CPU information in ACPI tables.", 0, 3); while (1); } while (0);;
    }
    if (0 == info_root->ioapic_count) {
        do { screen_write("PANIC: " "No I/O APIC found in ACPI tables.", 0, 3); while (1); } while (0);;
    }
}

user_defined_types: struct acpi_rsdp { unsigned char signature[8]; unsigned char checksum; unsigned char oemid[6]; unsigned char revision; unsigned int rsdt_addr; unsigned int length; unsigned long long xsdt_addr; unsigned char xchecksum; unsigned char reserved[3]; }


decompiled_code:
void *smp_prepare_boot16()
{
return memcpy((void *)0x1000, &boot16_begin, &boot16_end - &boot16_begin);
}

canonical_decompiled_code:
void *func0()
{
return memcpy((void *)0x1000, &boot16_begin, &boot16_end - &boot16_begin);
}

original_code:
static void smp_prepare_boot16(void)
{
    extern uint8_t boot16_begin;
    extern uint8_t boot16_end;
    size_t length = ((uintptr_t) &boot16_end - (uintptr_t) &boot16_begin);
    memcpy((void *) 0x1000, &boot16_begin, length);
}

canonical_original_code:
static void smp_prepare_boot16()
{
    extern unsigned char boot16_begin;
    extern unsigned char boot16_end;
    unsigned long long length = ((uintptr_t) &boot16_end - (uintptr_t) &boot16_begin);
    memcpy((void *) 0x1000, &boot16_begin, length);
}


decompiled_code:
unsigned __int64 smp_setup()
{
unsigned __int64 result; // rax
__int64 v1; // rbp
unsigned __int64 v2; // rbx
__int16 v3; // ax

smp_prepare_boot16();
result = info_root;
if ( *(_WORD *)(info_root + 150LL) )
{
v1 = 0LL;
v2 = 0LL;
do
{
v3 = *(_WORD *)(v1 + info_cpu + 8);
if ( (v3 & 1) != 0 && (v3 & 2) == 0 )
smp_boot((unsigned int *)(v1 + info_cpu));
++v2;
v1 += 18LL;
result = *(unsigned __int16 *)(info_root + 150LL);
}
while ( result > v2 );
}
return result;
}

canonical_decompiled_code:
unsigned __int64 func2()
{
unsigned __int64 result; // rax
__int64 v1; // rbp
unsigned __int64 v2; // rbx
__int16 v3; // ax

func0();
result = info_root;
if ( *(_WORD *)(info_root + 150LL) )
{
v1 = 0LL;
v2 = 0LL;
do
{
v3 = *(_WORD *)(v1 + info_cpu + 8);
if ( (v3 & 1) != 0 && (v3 & 2) == 0 )
func1((unsigned int *)(v1 + info_cpu));
++v2;
v1 += 18LL;
result = *(unsigned __int16 *)(info_root + 150LL);
}
while ( result > v2 );
}
return result;
}

original_code:
void smp_setup(void)
{
    smp_prepare_boot16();
    size_t i;
    for (i = 0; i < info_root->cpu_count; ++i) {
        hy_info_cpu_t *cpu = &info_cpu[i];
        if (0 == (cpu->flags & (1 << 0)))
            continue;
        if (0 != (cpu->flags & (1 << 1)))
            continue;
        smp_boot(cpu);
    }
}

canonical_original_code:
void smp_setup()
{
    smp_prepare_boot16();
    unsigned long long i;
    for (i = 0; i < info_root->cpu_count; ++i) {
        struct hy_info_cpu *cpu = &info_cpu[i];
        if (0 == (cpu->flags & (1 << 0)))
            continue;
        if (0 != (cpu->flags & (1 << 1)))
            continue;
        smp_boot(cpu);
    }
}

variable_types: struct hy_info_cpu { unsigned int apic_id; unsigned int acpi_id; unsigned short flags; unsigned int lapic_timer_freq; unsigned int domain; } *