repo: rohkea/cantonese-tools

decompiled_code:
__int64 __fastcall find_written_form(char *s2, int a2, _DWORD *a3)
{
unsigned int v3; // ebx
int v4; // edx
int v5; // er12
unsigned int v6; // ebp
int v7; // eax
__int64 i; // rbp
int v9; // er12
const char *v10; // r13
int v11; // er15
int v12; // eax

v4 = written_forms_length - 1;
if ( written_forms_length - 1 > 1 )
{
v5 = written_forms_length - 1;
v6 = 0;
while ( 1 )
{
v3 = v6 + v4 / 2;
v7 = strncmp(s2, *((const char **)&written_forms + 2 * (int)v3 + 1), a2);
if ( !v7 )
break;
if ( v7 >= 0 )
v6 = v3;
if ( v7 < 0 )
v5 = v3;
v4 = v5 - v6;
if ( (int)(v5 - v6) <= 1 )
{
for ( i = (int)(((unsigned int)v7 < 0x80000000) + v3 - 1); ; --i )
{
v9 = i;
v3 = i;
v10 = (const char *)*((_QWORD *)&written_forms + 2 * i + 1);
v11 = strlen(v10);
v12 = strncmp(v10, s2, v11);
if ( !v12 )
break;
if ( v9 <= 0 || v12 >= 0 )
return (unsigned int)-1;
}
*a3 = v11;
return v3;
}
}
}
*a3 = strlen(*((const char **)&written_forms + 2 * (int)v3 + 1));
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(char *s2, int a2, _DWORD *a3)
{
unsigned int v3; // ebx
int v4; // edx
int v5; // er12
unsigned int v6; // ebp
int v7; // eax
__int64 i; // rbp
int v9; // er12
const char *v10; // r13
int v11; // er15
int v12; // eax

v4 = written_forms_length - 1;
if ( written_forms_length - 1 > 1 )
{
v5 = written_forms_length - 1;
v6 = 0;
while ( 1 )
{
v3 = v6 + v4 / 2;
v7 = strncmp(s2, *((const char **)&written_forms + 2 * (int)v3 + 1), a2);
if ( !v7 )
break;
if ( v7 >= 0 )
v6 = v3;
if ( v7 < 0 )
v5 = v3;
v4 = v5 - v6;
if ( (int)(v5 - v6) <= 1 )
{
for ( i = (int)(((unsigned int)v7 < 0x80000000) + v3 - 1); ; --i )
{
v9 = i;
v3 = i;
v10 = (const char *)*((_QWORD *)&written_forms + 2 * i + 1);
v11 = strlen(v10);
v12 = strncmp(v10, s2, v11);
if ( !v12 )
break;
if ( v9 <= 0 || v12 >= 0 )
return (unsigned int)-1;
}
*a3 = v11;
return v3;
}
}
}
*a3 = strlen(*((const char **)&written_forms + 2 * (int)v3 + 1));
return v3;
}

original_code:
int
find_written_form(const char *text, int maxlen, int *len) {
 int from, to, id, compared, currlen;
 from = 0;
 to = written_forms_length -1;
 while (to - from > 1) {
  id = from + (to - from) / 2;
  compared = strncmp(text, written_forms[id].text, maxlen);
  if (compared == 0) {
   from = to = id;
  } else if (compared < 0) {
   to = id;
  } else {
   from = id;
  }
 }
 if (compared == 0) {
  *len = strlen(written_forms[id].text);
  return id;
 } else {
  if (compared < 0)
   id--;
  do {
   currlen = strlen(written_forms[id].text);
   compared = strncmp(written_forms[id].text, text, currlen);
   if (compared == 0) {
    *len = currlen;
    return id;
   }
   id--;
  } while (compared < 0 && id >= 0);
 }
 return -1;
}

canonical_original_code:
int find_written_form(char *text, int maxlen, int *len) {
 int from; int to; int id; int compared; int currlen;
 from = 0;
 to = written_forms_length -1;
 while (to - from > 1) {
  id = from + (to - from) / 2;
  compared = strncmp(text, written_forms[id].text, maxlen);
  if (compared == 0) {
   from = to = id;
  } else if (compared < 0) {
   to = id;
  } else {
   from = id;
  }
 }
 if (compared == 0) {
  *len = strlen(written_forms[id].text);
  return id;
 } else {
  if (compared < 0)
   id--;
  do {
   currlen = strlen(written_forms[id].text);
   compared = strncmp(written_forms[id].text, text, currlen);
   if (compared == 0) {
    *len = currlen;
    return id;
   }
   id--;
  } while (compared < 0 && id >= 0);
 }
 return -1;
}

variable_types:
text
char *

variable_types: maxlen int

variable_types:
len
int *

variable_types: from int

variable_types: to int

variable_types: id int

variable_types: compared int

variable_types: currlen int


original_code:
char *
parse_sentence(const char *sent, int maxlen) {
 char *result;
 int id, res_len, res_allocd, curr, unknown_start, unknown_len, known_len;
 res_allocd = 1024;
 res_len = 0;
 result = malloc(res_allocd);
 curr = 0;
 unknown_len = 0;
 if (!maxlen)
  maxlen = strlen(sent);
 if (sent[maxlen -1] == 0x0D)
  maxlen--;
 while (curr < maxlen) {
  id = find_written_form(&sent[curr], maxlen, &known_len);
  if (id == -1) {
   if (unknown_len <= 0) {
    unknown_start = curr;
    unknown_len = 1;
   }
   else {
    if ((sent[unknown_len] == '<' && sent[unknown_len + 1] == '[')
      || (sent[unknown_len] == ']'
       && sent[unknown_len + 1] == '>')) {
     append_to_result("<[", 1, &result, &res_allocd, &res_len);
     append_to_result(&sent[unknown_start], 1, &result, &res_allocd, &res_len);
     append_to_result("]> ", 3, &result, &res_allocd, &res_len);
     unknown_start = curr;
     unknown_len = 1;
    } else {
     ++unknown_len;
    }
   }
   ++curr;
  } else {
   if (unknown_len > 0) {
    append_to_result("<[", 2, &result, &res_allocd, &res_len);
    append_to_result(&sent[unknown_start], unknown_len, &result, &res_allocd, &res_len);
    append_to_result("]> ", 3, &result, &res_allocd, &res_len);
    unknown_len = 0;
   }
   append_to_result("<[", 2, &result, &res_allocd, &res_len);
   append_to_result(&sent[curr], known_len, &result, &res_allocd, &res_len);
   append_to_result("]>::", 4, &result, &res_allocd, &res_len);
   append_to_result(definitions[written_forms[id].def_id].text, 0, &result, &res_allocd, &res_len);
   append_to_result(" ", 1, &result, &res_allocd, &res_len);
   curr += known_len;
  }
 }
 if (unknown_len > 0) {
  append_to_result("<[", 2, &result, &res_allocd, &res_len);
  append_to_result(&sent[unknown_start], unknown_len, &result, &res_allocd, &res_len);
  append_to_result("]> ", 3, &result, &res_allocd, &res_len);
  unknown_len = 0;
 }
 return result;
}

canonical_original_code:
char *parse_sentence(char *sent, int maxlen) {
 char *result;
 int id; int res_len; int res_allocd; int curr; int unknown_start; int unknown_len; int known_len;
 res_allocd = 1024;
 res_len = 0;
 result = malloc(res_allocd);
 curr = 0;
 unknown_len = 0;
 if (!maxlen)
  maxlen = strlen(sent);
 if (sent[maxlen -1] == 0x0D)
  maxlen--;
 while (curr < maxlen) {
  id = find_written_form(&sent[curr], maxlen, &known_len);
  if (id == -1) {
   if (unknown_len <= 0) {
    unknown_start = curr;
    unknown_len = 1;
   }
   else {
    if ((sent[unknown_len] == '<' && sent[unknown_len + 1] == '[')
      || (sent[unknown_len] == ']'
       && sent[unknown_len + 1] == '>')) {
     append_to_result("<[", 1, &result, &res_allocd, &res_len);
     append_to_result(&sent[unknown_start], 1, &result, &res_allocd, &res_len);
     append_to_result("]> ", 3, &result, &res_allocd, &res_len);
     unknown_start = curr;
     unknown_len = 1;
    } else {
     ++unknown_len;
    }
   }
   ++curr;
  } else {
   if (unknown_len > 0) {
    append_to_result("<[", 2, &result, &res_allocd, &res_len);
    append_to_result(&sent[unknown_start], unknown_len, &result, &res_allocd, &res_len);
    append_to_result("]> ", 3, &result, &res_allocd, &res_len);
    unknown_len = 0;
   }
   append_to_result("<[", 2, &result, &res_allocd, &res_len);
   append_to_result(&sent[curr], known_len, &result, &res_allocd, &res_len);
   append_to_result("]>::", 4, &result, &res_allocd, &res_len);
   append_to_result(definitions[written_forms[id].def_id].text, 0, &result, &res_allocd, &res_len);
   append_to_result(" ", 1, &result, &res_allocd, &res_len);
   curr += known_len;
  }
 }
 if (unknown_len > 0) {
  append_to_result("<[", 2, &result, &res_allocd, &res_len);
  append_to_result(&sent[unknown_start], unknown_len, &result, &res_allocd, &res_len);
  append_to_result("]> ", 3, &result, &res_allocd, &res_len);
  unknown_len = 0;
 }
 return result;
}

variable_types:
sent
char *

variable_types:
result
char *

variable_types: curr int

return_type: char *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
char *v4; // rbp
char v6[2072]; // [rsp+0h] [rbp-818h] BYREF

do
{
if ( v6 == fgets(v6, 2048, stdin) )
{
v3 = strlen(v6);
v4 = (char *)parse_sentence(v6, v3 - 1);
puts(v4);
free(v4);
}
}
while ( !feof(stdin) );
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
char *v4; // rbp
char v6[2072]; // [rsp+0h] [rbp-818h] BYREF

do
{
if ( v6 == fgets(v6, 2048, stdin) )
{
v3 = strlen(v6);
v4 = (char *)func2(v6, v3 - 1);
puts(v4);
free(v4);
}
}
while ( !feof(stdin) );
return 0;
}

original_code:
int
main() {
 char buffer[2048], *r, *read;
 do {
  read = fgets(buffer, (sizeof(buffer)/sizeof(buffer[0])), stdin);
  if (read == buffer) {
   r = parse_sentence(buffer, strlen(buffer) -1);
   printf("%s\n", r);
   free(r);
  }
 } while (!feof(stdin));
}

canonical_original_code:
int main() {
 char buffer[2048]; char *r; char *read;
 do {
  read = fgets(buffer, (sizeof(buffer)/sizeof(buffer[0])), stdin);
  if (read == buffer) {
   r = parse_sentence(buffer, strlen(buffer) -1);
   printf("%s\n", r);
   free(r);
  }
 } while (!feof(stdin));
}

variable_types:
buffer
char [2048]

variable_types:
r
char *

variable_types:
read
char *

return_type: int


decompiled_code:
int __fastcall print_syntax(const char *a1)
{
return fprintf(stderr, "Syntax: %s [-b <dtbufsize>] <delay>\n", a1);
}

canonical_decompiled_code:
int __fastcall func7(const char *a1)
{
return fprintf(stderr, "Syntax: %s [-b <dtbufsize>] <delay>\n", a1);
}

original_code:
void print_syntax(char *arg0)
{
    fprintf(stderr, "Syntax: %s [-b <dtbufsize>] <delay>\n", arg0);
}

return_type: void


decompiled_code:
void __fastcall parse_cli(int a1, const char **a2)
{
const void **v3; // rbx
__int64 v4; // r14
int v5; // er12
int v6; // er8
int *v7; // r13
__int64 v8; // rax
const char *v9; // rbp
int *v10; // r13
int v11; // eax
char *endptr[8]; // [rsp+0h] [rbp-40h] BYREF

if ( a1 > 1 )
{
v3 = (const void **)(a2 + 1);
v4 = (__int64)&a2[(unsigned int)(a1 - 2) + 2];
v5 = 0;
v6 = 0;
while ( 1 )
{
v9 = (const char *)*v3;
if ( v6 == 1 )
goto LABEL_3;
if ( memcmp("-b", *v3, 2uLL) )
{
if ( (v5 & 1) != 0 )
{
print_syntax(*a2);
exit(9);
}
v10 = __errno_location();
*v10 = 0;
v11 = strtol(v9, endptr, 0);
delay = v11;
v6 = *v10;
if ( *v10 )
{
perror("strtol()");
exit(10);
}
if ( !strcmp("s", endptr[0]) )
{
delay = 1000 * v11;
}
else if ( !strcmp("m", endptr[0]) )
{
delay = 60000 * v11;
}
else if ( !strcmp("h", endptr[0]) )
{
delay = 3600000 * v11;
}
else if ( *endptr[0] )
{
fprintf(stderr, "delay value contains garbage: %s\n", endptr[0]);
exit(9);
}
v5 |= 1u;
goto LABEL_8;
}
v6 = 1;
if ( v9[2] )
break;
LABEL_8:
if ( ++v3 == (const void **)v4 )
return;
}
v9 += 2;
LABEL_3:
if ( (v5 & 2) != 0 )
{
print_syntax(*a2);
exit(9);
}
v7 = __errno_location();
*v7 = 0;
v8 = strtol(v9, endptr, 0);
dtbufsize = v8;
v6 = *v7;
if ( *v7 )
{
perror("strtol()");
exit(10);
}
if ( !strcmp("k", endptr[0]) )
{
dtbufsize = v8 << 10;
}
else if ( !strcmp("m", endptr[0]) )
{
dtbufsize = v8 << 20;
}
else if ( !strcmp("g", endptr[0]) )
{
dtbufsize = v8 << 30;
}
else if ( *endptr[0] )
{
fprintf(stderr, "dtbufsize value contains garbage: %s\n", endptr[0]);
exit(9);
}
v5 |= 2u;
goto LABEL_8;
}
}

canonical_decompiled_code:
void __fastcall func8(int a1, const char **a2)
{
const void **v3; // rbx
__int64 v4; // r14
int v5; // er12
int v6; // er8
int *v7; // r13
__int64 v8; // rax
const char *v9; // rbp
int *v10; // r13
int v11; // eax
char *endptr[8]; // [rsp+0h] [rbp-40h] BYREF

if ( a1 > 1 )
{
v3 = (const void **)(a2 + 1);
v4 = (__int64)&a2[(unsigned int)(a1 - 2) + 2];
v5 = 0;
v6 = 0;
while ( 1 )
{
v9 = (const char *)*v3;
if ( v6 == 1 )
goto LABEL_3;
if ( memcmp("-b", *v3, 2uLL) )
{
if ( (v5 & 1) != 0 )
{
func7(*a2);
exit(9);
}
v10 = __errno_location();
*v10 = 0;
v11 = strtol(v9, endptr, 0);
delay = v11;
v6 = *v10;
if ( *v10 )
{
perror("strtol()");
exit(10);
}
if ( !strcmp("s", endptr[0]) )
{
delay = 1000 * v11;
}
else if ( !strcmp("m", endptr[0]) )
{
delay = 60000 * v11;
}
else if ( !strcmp("h", endptr[0]) )
{
delay = 3600000 * v11;
}
else if ( *endptr[0] )
{
fprintf(stderr, "delay value contains garbage: %s\n", endptr[0]);
exit(9);
}
v5 |= 1u;
goto LABEL_8;
}
v6 = 1;
if ( v9[2] )
break;
LABEL_8:
if ( ++v3 == (const void **)v4 )
return;
}
v9 += 2;
LABEL_3:
if ( (v5 & 2) != 0 )
{
func7(*a2);
exit(9);
}
v7 = __errno_location();
*v7 = 0;
v8 = strtol(v9, endptr, 0);
dtbufsize = v8;
v6 = *v7;
if ( *v7 )
{
perror("strtol()");
exit(10);
}
if ( !strcmp("k", endptr[0]) )
{
dtbufsize = v8 << 10;
}
else if ( !strcmp("m", endptr[0]) )
{
dtbufsize = v8 << 20;
}
else if ( !strcmp("g", endptr[0]) )
{
dtbufsize = v8 << 30;
}
else if ( *endptr[0] )
{
fprintf(stderr, "dtbufsize value contains garbage: %s\n", endptr[0]);
exit(9);
}
v5 |= 2u;
goto LABEL_8;
}
}

original_code:
void parse_cli(int argc, char *argv[])
{
    int ctx = 0;
    char *arg;
    int args_handled = 0;
    char *garbage;
    int i;
    for (i = 1; i < argc; i++) {
        arg = argv[i];
 handle_arg:
        if (ctx == 1) {
            if (args_handled & (1 << 1)) {
                print_syntax(argv[0]);
                exit(9);
            }
            (*__errno_location ()) = 0;
            dtbufsize = strtol(arg, &garbage, 0);
            if ((*__errno_location ())) {
                perror("strtol()");
                exit(10);
            }
            if (strcmp("k", garbage) == 0) {
                dtbufsize <<= 10;
            } else if (strcmp("m", garbage) == 0) {
                dtbufsize <<= 20;
            } else if (strcmp("g", garbage) == 0) {
                dtbufsize <<= 30;
            } else if (garbage[0] != '\0') {
                fprintf(stderr, "dtbufsize value contains garbage: %s\n",
                        garbage);
                exit(9);
            }
            args_handled |= (1 << 1);
            ctx = 0;
        } else if (strncmp("-b", arg, 2) == 0) {
            ctx = 1;
            if (strlen(&arg[2]) > 0) {
                arg = &arg[2];
                goto handle_arg;
            }
        } else {
            if (args_handled & 1) {
                print_syntax(argv[0]);
                exit(9);
            }
            (*__errno_location ()) = 0;
            delay = strtol(arg, &garbage, 0);
            if ((*__errno_location ())) {
                perror("strtol()");
                exit(10);
            }
            if (strcmp("s", garbage) == 0) {
                delay *= 1000;
            } else if (strcmp("m", garbage) == 0) {
                delay *= 60 * 1000;
            } else if (strcmp("h", garbage) == 0) {
                delay *= 60 * 60 * 1000;
            } else if (garbage[0] != '\0') {
                fprintf(stderr, "delay value contains garbage: %s\n", garbage);
                exit(9);
            }
            args_handled |= 1;
        }
    }
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: ctx int

variable_types:
arg
char *

variable_types:
garbage
char *

variable_types: i int


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_BOOL4 v3; // er12
_BOOL4 v4; // er15
__int64 i; // r14
int v6; // eax
__int64 v7; // rax
int v9; // edx
int v10; // esi
__int16 v11; // ax
int v12; // eax
_BOOL4 v13; // ebx
int v14; // eax
__int64 v15; // rbx
_BOOL4 v16; // ebp

parse_cli(argc, argv);
if ( (unsigned int)dtbuf_init(&dtbuf, dtbufsize) )
{
fwrite("dtbuf initialization failed\n", 1uLL, 0x1CuLL, stderr);
exit(1);
}
v3 = 0;
v4 = 0;
for ( i = 0LL; ; i = dtbuf_next_timestamp(&dtbuf) )
{
while ( 1 )
{
v15 = get_time_ms();
v16 = 0;
if ( !v3 )
v16 = dtbuf_is_full(&dtbuf) == 0;
if ( !v4 )
{
v9 = -1;
LABEL_17:
v10 = v16 + 1;
v11 = 0;
goto LABEL_18;
}
v9 = i + delay - v15;
if ( i + delay - v15 > 0 )
goto LABEL_17;
v10 = v16 + 1;
v9 = -1;
v11 = 4;
LABEL_18:
fds_all.events = v11;
v12 = poll(&fds_all, v10, v9);
v13 = v12;
if ( v12 == -1 )
{
perror("poll()");
exit(3);
}
if ( !v12 )
break;
v13 = 0;
if ( fds_all.revents )
{
v13 = 1;
if ( (fds_all.revents & 0x14) != 0 )
v13 = dtbuf_read_chunk(&dtbuf, 1LL) <= 0;
}
if ( v16 && word_4040AE )
{
if ( (word_4040AE & 0x11) != 0 )
{
v7 = get_time_ms();
if ( dtbuf_write_chunk(&dtbuf, 0LL, v7) <= 0 )
v3 = v16;
}
else
{
v3 = v16;
}
}
v14 = dtbuf_is_empty(&dtbuf);
v4 = v14 == 0;
if ( v14 )
{
LABEL_28:
if ( v13 || v3 )
goto LABEL_15;
v4 = 0;
}
else
{
i = dtbuf_next_timestamp(&dtbuf);
if ( v13 )
goto LABEL_15;
}
}
if ( dtbuf_read_chunk(&dtbuf, 1LL) <= 0 )
break;
v6 = dtbuf_is_empty(&dtbuf);
v4 = v6 == 0;
if ( v6 )
goto LABEL_28;
}
if ( !(unsigned int)dtbuf_is_empty(&dtbuf) )
dtbuf_next_timestamp(&dtbuf);
LABEL_15:
dtbuf_free(&dtbuf);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
_BOOL4 v3; // er12
_BOOL4 v4; // er15
__int64 i; // r14
int v6; // eax
__int64 v7; // rax
int v9; // edx
int v10; // esi
__int16 v11; // ax
int v12; // eax
_BOOL4 v13; // ebx
int v14; // eax
__int64 v15; // rbx
_BOOL4 v16; // ebp

func8(argc, argv);
if ( (unsigned int)func9(&dtbuf, dtbufsize) )
{
fwrite("dtbuf initialization failed\n", 1uLL, 0x1CuLL, stderr);
exit(1);
}
v3 = 0;
v4 = 0;
for ( i = 0LL; ; i = func13(&dtbuf) )
{
while ( 1 )
{
v15 = func16();
v16 = 0;
if ( !v3 )
v16 = func12(&dtbuf) == 0;
if ( !v4 )
{
v9 = -1;
LABEL_17:
v10 = v16 + 1;
v11 = 0;
goto LABEL_18;
}
v9 = i + delay - v15;
if ( i + delay - v15 > 0 )
goto LABEL_17;
v10 = v16 + 1;
v9 = -1;
v11 = 4;
LABEL_18:
fds_all.events = v11;
v12 = poll(&fds_all, v10, v9);
v13 = v12;
if ( v12 == -1 )
{
perror("poll()");
exit(3);
}
if ( !v12 )
break;
v13 = 0;
if ( fds_all.revents )
{
v13 = 1;
if ( (fds_all.revents & 0x14) != 0 )
v13 = func15(&dtbuf, 1LL) <= 0;
}
if ( v16 && word_4040AE )
{
if ( (word_4040AE & 0x11) != 0 )
{
v7 = func16();
if ( func14(&dtbuf, 0LL, v7) <= 0 )
v3 = v16;
}
else
{
v3 = v16;
}
}
v14 = func11(&dtbuf);
v4 = v14 == 0;
if ( v14 )
{
LABEL_28:
if ( v13 || v3 )
goto LABEL_15;
v4 = 0;
}
else
{
i = func13(&dtbuf);
if ( v13 )
goto LABEL_15;
}
}
if ( func15(&dtbuf, 1LL) <= 0 )
break;
v6 = func11(&dtbuf);
v4 = v6 == 0;
if ( v6 )
goto LABEL_28;
}
if ( !(unsigned int)func11(&dtbuf) )
func13(&dtbuf);
LABEL_15:
func10(&dtbuf);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
    int r;
    int fds_count;
    int has_next_chunk = 0;
    time_ms next_chunk_timestamp = 0;
    time_ms now;
    time_ms wait_delay;
    int poll_stdin;
    int pollout_stdout;
    int timeout;
    int in_closed = 0;
    int out_closed = 0;
    parse_cli(argc, argv);
    if (dtbuf_init(&dtbuf, dtbufsize)) {
        fprintf(stderr, "dtbuf initialization failed\n");
        exit(1);
    }
    do {
        now = get_time_ms();
        poll_stdin = !in_closed && !dtbuf_is_full(&dtbuf);
        if (has_next_chunk) {
            wait_delay = next_chunk_timestamp + delay - now;
            if (wait_delay <= 0) {
                pollout_stdout = 1;
                timeout = -1;
            } else {
                pollout_stdout = 0;
                timeout = wait_delay;
            }
        } else {
            pollout_stdout = 0;
            timeout = -1;
        }
        fds_count = poll_stdin ? 2 : 1;
        fds_all[0].events = pollout_stdout ? 0x004 : 0;
        if ((r = poll(fds_all, fds_count, timeout)) == -1) {
            perror("poll()");
            exit(3);
        }
        if (r == 0) {
            if (dtbuf_read_chunk(&dtbuf, 1 ) <= 0) {
                out_closed = 1;
            }
        } else {
            if (fds_all[0].revents) {
                if (fds_all[0].revents & (0x004 | 0x010)) {
                    if (dtbuf_read_chunk(&dtbuf, 1 ) <= 0) {
                        out_closed = 1;
                    }
                } else {
                    out_closed = 1;
                }
            }
            if (poll_stdin && fds_all[1].revents) {
                if (fds_all[1].revents & (0x001 | 0x010)) {
                    now = get_time_ms();
                    if (dtbuf_write_chunk(&dtbuf, 0 , now) <= 0) {
                        in_closed = 1;
                    }
                } else {
                    in_closed = 1;
                }
            }
        }
        has_next_chunk = !dtbuf_is_empty(&dtbuf);
        if (has_next_chunk) {
            next_chunk_timestamp = dtbuf_next_timestamp(&dtbuf);
        }
    } while (!out_closed && (!in_closed || has_next_chunk));
    dtbuf_free(&dtbuf);
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int r;
    int fds_count;
    int has_next_chunk = 0;
    long next_chunk_timestamp = 0;
    long now;
    long wait_delay;
    int poll_stdin;
    int pollout_stdout;
    int timeout;
    int in_closed = 0;
    int out_closed = 0;
    parse_cli(argc, argv);
    if (dtbuf_init(&dtbuf, dtbufsize)) {
        fprintf(stderr, "dtbuf initialization failed\n");
        exit(1);
    }
    do {
        now = get_time_ms();
        poll_stdin = !in_closed && !dtbuf_is_full(&dtbuf);
        if (has_next_chunk) {
            wait_delay = next_chunk_timestamp + delay - now;
            if (wait_delay <= 0) {
                pollout_stdout = 1;
                timeout = -1;
            } else {
                pollout_stdout = 0;
                timeout = wait_delay;
            }
        } else {
            pollout_stdout = 0;
            timeout = -1;
        }
        fds_count = poll_stdin ? 2 : 1;
        fds_all[0].events = pollout_stdout ? 0x004 : 0;
        if ((r = poll(fds_all, fds_count, timeout)) == -1) {
            perror("poll()");
            exit(3);
        }
        if (r == 0) {
            if (dtbuf_read_chunk(&dtbuf, 1 ) <= 0) {
                out_closed = 1;
            }
        } else {
            if (fds_all[0].revents) {
                if (fds_all[0].revents & (0x004 | 0x010)) {
                    if (dtbuf_read_chunk(&dtbuf, 1 ) <= 0) {
                        out_closed = 1;
                    }
                } else {
                    out_closed = 1;
                }
            }
            if (poll_stdin && fds_all[1].revents) {
                if (fds_all[1].revents & (0x001 | 0x010)) {
                    now = get_time_ms();
                    if (dtbuf_write_chunk(&dtbuf, 0 , now) <= 0) {
                        in_closed = 1;
                    }
                } else {
                    in_closed = 1;
                }
            }
        }
        has_next_chunk = !dtbuf_is_empty(&dtbuf);
        if (has_next_chunk) {
            next_chunk_timestamp = dtbuf_next_timestamp(&dtbuf);
        }
    } while (!out_closed && (!in_closed || has_next_chunk));
    dtbuf_free(&dtbuf);
    return 0;
}

variable_types: r int

variable_types: now long

variable_types: timeout int


original_code:
int dtbuf_init(struct dtbuf *dtbuf, size_t capacity)
{
    dtbuf->real_capacity = capacity + (sizeof (struct header) + 4000) - 1;
    if (!(dtbuf->data = malloc(dtbuf->real_capacity))) {
        return 1;
    }
    dtbuf->capacity = capacity;
    dtbuf->head = 0;
    dtbuf->tail = 0;
    return 0;
}

canonical_original_code:
int dtbuf_init(struct dtbuf *dtbuf, unsigned long capacity)
{
    dtbuf->real_capacity = capacity + (sizeof (struct header) + 4000) - 1;
    if (!(dtbuf->data = malloc(dtbuf->real_capacity))) {
        return 1;
    }
    dtbuf->capacity = capacity;
    dtbuf->head = 0;
    dtbuf->tail = 0;
    return 0;
}

variable_types: struct dtbuf { char *data; unsigned long capacity; unsigned long real_capacity; int head; int tail; } *

variable_types:
capacity
unsigned long

user_defined_types: struct header { long timestamp; unsigned short data_length; }


decompiled_code:
void __fastcall dtbuf_free(void **a1)
{
free(*a1);
}

canonical_decompiled_code:
void __fastcall func10(void **a1)
{
free(*a1);
}

original_code:
void dtbuf_free(struct dtbuf *dtbuf)
{
    free(dtbuf->data);
}

user_defined_types: struct dtbuf { char *data; unsigned long capacity; unsigned long real_capacity; int head; int tail; }


decompiled_code:
_BOOL8 __fastcall dtbuf_is_empty(__int64 a1)
{
return *(_DWORD *)(a1 + 24) == *(_DWORD *)(a1 + 28);
}

original_code:
int dtbuf_is_empty(struct dtbuf *dtbuf)
{
    return dtbuf->head == dtbuf->tail;
}


decompiled_code:
_BOOL8 __fastcall dtbuf_is_full(__int64 a1)
{
int v1; // edx
_BOOL8 result; // rax
int v3; // ecx

v1 = *(_DWORD *)(a1 + 24);
result = 1LL;
if ( (unsigned __int64)v1 < *(_QWORD *)(a1 + 8) )
{
v3 = *(_DWORD *)(a1 + 28);
result = 0LL;
if ( v1 < v3 )
result = (unsigned int)(v3 - v1) <= 0xFB0;
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func12(__int64 a1)
{
int v1; // edx
_BOOL8 result; // rax
int v3; // ecx

v1 = *(_DWORD *)(a1 + 24);
result = 1LL;
if ( (unsigned __int64)v1 < *(_QWORD *)(a1 + 8) )
{
v3 = *(_DWORD *)(a1 + 28);
result = 0LL;
if ( v1 < v3 )
result = (unsigned int)(v3 - v1) <= 0xFB0;
}
return result;
}

original_code:
int dtbuf_is_full(struct dtbuf *dtbuf)
{
    return dtbuf->head >= dtbuf->capacity || (dtbuf->head < dtbuf->tail
                                              && dtbuf->tail - dtbuf->head <=
                                              (sizeof (struct header) + 4000));
}


decompiled_code:
__int64 __fastcall dtbuf_next_timestamp(__int64 a1)
{
return *(_QWORD *)(*(_QWORD *)a1 + *(int *)(a1 + 28));
}

original_code:
time_ms dtbuf_next_timestamp(struct dtbuf *dtbuf)
{
    struct header *header = (struct header *) &dtbuf->data[dtbuf->tail];
    return header->timestamp;
}

canonical_original_code:
long dtbuf_next_timestamp(struct dtbuf *dtbuf)
{
    struct header *header = (struct header *) &dtbuf->data[dtbuf->tail];
    return header->timestamp;
}

variable_types: struct header { long timestamp; unsigned short data_length; } *

return_type: long


original_code:
ssize_t dtbuf_write_chunk(struct dtbuf *dtbuf, int fd_in, time_ms timestamp)
{
    ssize_t r;
    struct header header;
    int payload_index = dtbuf->head + sizeof(struct header);
    if ((r = read(fd_in, &dtbuf->data[payload_index],
                  4000)) > 0) {
        header.timestamp = timestamp;
        header.data_length = (chunk_length) r;
        memcpy(&dtbuf->data[dtbuf->head], &header, sizeof(header));
        dtbuf->head = payload_index + r;
        if (dtbuf->head >= dtbuf->capacity && dtbuf->tail >= (sizeof (struct header) + 4000)) {
            dtbuf->head = 0;
        }
    } else if (r == -1) {
        perror("read()");
    }
    return r;
}

canonical_original_code:
long dtbuf_write_chunk(struct dtbuf *dtbuf, int fd_in, long timestamp)
{
    long r;
    struct header header;
    int payload_index = dtbuf->head + sizeof(struct header);
    if ((r = read(fd_in, &dtbuf->data[payload_index],
                  4000)) > 0) {
        header.timestamp = timestamp;
        header.data_length = (unsigned short) r;
        memcpy(&dtbuf->data[dtbuf->head], &header, sizeof(header));
        dtbuf->head = payload_index + r;
        if (dtbuf->head >= dtbuf->capacity && dtbuf->tail >= (sizeof (struct header) + 4000)) {
            dtbuf->head = 0;
        }
    } else if (r == -1) {
        perror("read()");
    }
    return r;
}

variable_types: timestamp long

variable_types: r long

variable_types: struct header { long timestamp; unsigned short data_length; }


original_code:
ssize_t dtbuf_read_chunk(struct dtbuf *dtbuf, int fd_out)
{
    ssize_t w;
    struct header *pheader = (struct header *) &dtbuf->data[dtbuf->tail];
    struct header header;
    chunk_length length = pheader->data_length;
    int payload_index = dtbuf->tail + sizeof(struct header);
    if ((w = write(fd_out, &dtbuf->data[payload_index], length)) > 0) {
        if (w == length) {
            dtbuf->tail = payload_index + w;
            if (dtbuf->tail >= dtbuf->capacity) {
                dtbuf->tail = 0;
                if (dtbuf->head >= dtbuf->capacity) {
                    dtbuf->head = 0;
                }
            }
        } else {
            dtbuf->tail += w;
            header.timestamp = pheader->timestamp;
            header.data_length = length - w;
            memcpy(&dtbuf->data[dtbuf->tail], &header, sizeof(header));
        }
        if (dtbuf->head >= dtbuf->capacity && dtbuf->tail >= (sizeof (struct header) + 4000)) {
            dtbuf->head = 0;
        }
    } else if (w == -1) {
        perror("write()");
    }
    return w;
}

canonical_original_code:
long dtbuf_read_chunk(struct dtbuf *dtbuf, int fd_out)
{
    long w;
    struct header *pheader = (struct header *) &dtbuf->data[dtbuf->tail];
    struct header header;
    unsigned short length = pheader->data_length;
    int payload_index = dtbuf->tail + sizeof(struct header);
    if ((w = write(fd_out, &dtbuf->data[payload_index], length)) > 0) {
        if (w == length) {
            dtbuf->tail = payload_index + w;
            if (dtbuf->tail >= dtbuf->capacity) {
                dtbuf->tail = 0;
                if (dtbuf->head >= dtbuf->capacity) {
                    dtbuf->head = 0;
                }
            }
        } else {
            dtbuf->tail += w;
            header.timestamp = pheader->timestamp;
            header.data_length = length - w;
            memcpy(&dtbuf->data[dtbuf->tail], &header, sizeof(header));
        }
        if (dtbuf->head >= dtbuf->capacity && dtbuf->tail >= (sizeof (struct header) + 4000)) {
            dtbuf->head = 0;
        }
    } else if (w == -1) {
        perror("write()");
    }
    return w;
}

variable_types: w long

variable_types:
length
unsigned short


decompiled_code:
__time_t get_time_ms()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

if ( gettimeofday(&v1, 0LL) )
{
perror("gettimeofday()");
exit(42);
}
return 1000 * v1.tv_sec + v1.tv_usec / 1000;
}

canonical_decompiled_code:
__time_t func16()
{
struct timeval v1; // [rsp+0h] [rbp-18h] BYREF

if ( gettimeofday(&v1, 0LL) )
{
perror("gettimeofday()");
exit(42);
}
return 1000 * v1.tv_sec + v1.tv_usec / 1000;
}

original_code:
time_ms get_time_ms()
{
    struct timeval timeval;
    if (gettimeofday(&timeval, ((void *)0))) {
        perror("gettimeofday()");
        exit(42);
    }
    return timeval.tv_sec * 1000 + timeval.tv_usec / 1000;
}

canonical_original_code:
long get_time_ms()
{
    struct timeval timeval;
    if (gettimeofday(&timeval, ((void *)0))) {
        perror("gettimeofday()");
        exit(42);
    }
    return timeval.tv_sec * 1000 + timeval.tv_usec / 1000;
}

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


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const void *v3; // r15
int *v4; // rbp
__int64 v5; // rbx
char *v6; // rbp
int v7; // er12
int v8; // ebp
int v9; // eax
int v10; // ebx
__int64 v11; // r14
int v12; // eax
int v13; // er13
void *v14; // rax
int v15; // eax
__int64 v17; // [rsp+0h] [rbp-1068h] BYREF
char *endptr; // [rsp+8h] [rbp-1060h] BYREF
char src[4096]; // [rsp+10h] [rbp-1058h] BYREF
char v20[88]; // [rsp+1010h] [rbp-58h] BYREF

if ( argc == 1 )
{
v5 = 10485760LL;
}
else
{
if ( argc != 2 )
{
fprintf(stderr, "Syntax: %s [cbufsize in kb]\n", *argv);
exit(1);
}
v4 = __errno_location();
*v4 = 0;
v5 = strtol(argv[1], &endptr, 0);
if ( *v4 )
{
perror("Cannot parse cbufsize argument");
exit(1);
}
v6 = endptr;
if ( !strcmp("k", endptr) )
{
v5 <<= 10;
}
else if ( !strcmp("m", endptr) )
{
v5 <<= 20;
}
else if ( *v6 )
{
fprintf(stderr, "cbufsize contains garbage: %s\n", v6);
exit(1);
}
}
v7 = cbuf_init(v20, v5);
if ( v7 )
{
fwrite("Out of memory", 1uLL, 0xDuLL, stderr);
exit(2);
}
naldetector_init(&v17, 0LL);
v8 = 3;
LABEL_8:
while ( !feof(stdin) && !ferror(stdin) && v8 > 0 )
{
v9 = fread(&src[v7], 1uLL, 32 - v7, stdin);
v10 = v9;
if ( v9 && v9 > 0 )
{
v11 = v7 + 1;
while ( 1 )
{
--v10;
v7 = v11;
v12 = naldetector_eat(&v17, (unsigned int)*((char *)&endptr + v11 + 7));
v13 = v12;
if ( v12 )
{
if ( !--v8 )
break;
}
++v11;
if ( v10 <= 0 || v8 <= 0 )
goto LABEL_8;
}
v7 = v11 - v12;
v14 = malloc((int)v11 - v12);
v3 = v14;
if ( !v14 )
{
perror("Cannot allocate headers buffer");
exit(2);
}
memcpy(v14, src, v7);
cbuf_write(v20, &src[v7], v10 + v13);
}
}
if ( !v8 )
{
while ( !feof(stdin) && !ferror(stdin) )
{
v15 = fread(src, 1uLL, 0x1000uLL, stdin);
if ( v15 )
cbuf_write(v20, src, v15);
}
fwrite(v3, 1uLL, v7, stdout);
cbuf_print_nals(v20, stdout);
}
cbuf_free(v20);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const void *v3; // r15
int *v4; // rbp
__int64 v5; // rbx
char *v6; // rbp
int v7; // er12
int v8; // ebp
int v9; // eax
int v10; // ebx
__int64 v11; // r14
int v12; // eax
int v13; // er13
void *v14; // rax
int v15; // eax
__int64 v17; // [rsp+0h] [rbp-1068h] BYREF
char *endptr; // [rsp+8h] [rbp-1060h] BYREF
char src[4096]; // [rsp+10h] [rbp-1058h] BYREF
char v20[88]; // [rsp+1010h] [rbp-58h] BYREF

if ( argc == 1 )
{
v5 = 10485760LL;
}
else
{
if ( argc != 2 )
{
fprintf(stderr, "Syntax: %s [cbufsize in kb]\n", *argv);
exit(1);
}
v4 = __errno_location();
*v4 = 0;
v5 = strtol(argv[1], &endptr, 0);
if ( *v4 )
{
perror("Cannot parse cbufsize argument");
exit(1);
}
v6 = endptr;
if ( !strcmp("k", endptr) )
{
v5 <<= 10;
}
else if ( !strcmp("m", endptr) )
{
v5 <<= 20;
}
else if ( *v6 )
{
fprintf(stderr, "cbufsize contains garbage: %s\n", v6);
exit(1);
}
}
v7 = func7(v20, v5);
if ( v7 )
{
fwrite("Out of memory", 1uLL, 0xDuLL, stderr);
exit(2);
}
func11(&v17, 0LL);
v8 = 3;
LABEL_8:
while ( !feof(stdin) && !ferror(stdin) && v8 > 0 )
{
v9 = fread(&src[v7], 1uLL, 32 - v7, stdin);
v10 = v9;
if ( v9 && v9 > 0 )
{
v11 = v7 + 1;
while ( 1 )
{
--v10;
v7 = v11;
v12 = func12(&v17, (unsigned int)*((char *)&endptr + v11 + 7));
v13 = v12;
if ( v12 )
{
if ( !--v8 )
break;
}
++v11;
if ( v10 <= 0 || v8 <= 0 )
goto LABEL_8;
}
v7 = v11 - v12;
v14 = malloc((int)v11 - v12);
v3 = v14;
if ( !v14 )
{
perror("Cannot allocate headers buffer");
exit(2);
}
memcpy(v14, src, v7);
func9(v20, &src[v7], v10 + v13);
}
}
if ( !v8 )
{
while ( !feof(stdin) && !ferror(stdin) )
{
v15 = fread(src, 1uLL, 0x1000uLL, stdin);
if ( v15 )
func9(v20, src, v15);
}
fwrite(v3, 1uLL, v7, stdout);
func10(v20, stdout);
}
func8(v20);
return 0;
}

original_code:
int main(int argc, char *argv[]) {
  struct cbuf cbuf;
  char buf[4096];
  long cbufsize;
  int read;
  char *garbage;
  char *headers;
  int headers_size;
  struct naldetector detector;
  int detected_rewind;
  int remaining_start_prefixes;
  if (argc == 1) {
    cbufsize = 10 * 1024 * 1024;
  } else if (argc == 2) {
    (*__errno_location ()) = 0;
    cbufsize = strtol(argv[1], &garbage, 0);
    if ((*__errno_location ())) {
      perror("Cannot parse cbufsize argument");
      exit(1);
    }
    if (strcmp("k", garbage) == 0) {
      cbufsize *= 1024;
    } else if (strcmp("m", garbage) == 0) {
      cbufsize *= 1024 * 1024;
    } else if (garbage[0] != '\0') {
      fprintf(stderr, "cbufsize contains garbage: %s\n", garbage);
      exit(1);
    }
  } else {
    fprintf(stderr, "Syntax: %s [cbufsize in kb]\n", argv[0]);
    exit(1);
  }
  if (cbuf_init(&cbuf, cbufsize)) {
    fprintf(stderr, "Out of memory");
    exit(2);
  }
  naldetector_init(&detector, 0 );
  headers_size = 0;
  remaining_start_prefixes = 3;
  while (!feof(stdin) && !ferror(stdin) && remaining_start_prefixes > 0) {
    if ((read = fread(&buf[headers_size], 1, 32 - headers_size, stdin))) {
      while (read > 0 && remaining_start_prefixes > 0) {
        read--;
        headers_size++;
        if ((detected_rewind = naldetector_eat(&detector, buf[headers_size - 1]))) {
          if (--remaining_start_prefixes == 0) {
            headers_size -= detected_rewind;
            if (!(headers = malloc(headers_size))) {
              perror("Cannot allocate headers buffer");
              exit(2);
            }
            memcpy(headers, buf, headers_size);
            cbuf_write(&cbuf, &buf[headers_size], read + detected_rewind);
          }
        }
      }
    }
  }
  if (remaining_start_prefixes == 0) {
    while (!feof(stdin) && !ferror(stdin)) {
      if ((read = fread(buf, 1, 4096, stdin))) {
        cbuf_write(&cbuf, buf, read);
      }
    }
    fwrite(headers, 1, headers_size, stdout);
    cbuf_print_nals(&cbuf, stdout);
  }
  cbuf_free(&cbuf);
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct cbuf { char *data; unsigned long capacity; int head; unsigned long length; }

variable_types:
buf
char [4096]

variable_types: cbufsize long

variable_types: read int

variable_types:
headers
char *

variable_types: struct naldetector { int pattern_bytes_ok; int detect_idr_only; }


decompiled_code:
__int64 __fastcall cbuf_init(__int64 a1, size_t a2)
{
void *v2; // rax

v2 = malloc(a2);
*(_QWORD *)a1 = v2;
if ( !v2 )
return 1LL;
*(_QWORD *)(a1 + 8) = a2;
*(_DWORD *)(a1 + 16) = 0;
*(_QWORD *)(a1 + 24) = 0LL;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, size_t a2)
{
void *v2; // rax

v2 = malloc(a2);
*(_QWORD *)a1 = v2;
if ( !v2 )
return 1LL;
*(_QWORD *)(a1 + 8) = a2;
*(_DWORD *)(a1 + 16) = 0;
*(_QWORD *)(a1 + 24) = 0LL;
return 0LL;
}

original_code:
int cbuf_init(struct cbuf *cbuf, size_t capacity) {
  cbuf->data = (char *) malloc(capacity);
  if (!cbuf->data) {
    return 1;
  }
  cbuf->capacity = capacity;
  cbuf->head = 0;
  cbuf->length = 0;
  return 0;
}

canonical_original_code:
int cbuf_init(struct cbuf *cbuf, unsigned long capacity) {
  cbuf->data = (char *) malloc(capacity);
  if (!cbuf->data) {
    return 1;
  }
  cbuf->capacity = capacity;
  cbuf->head = 0;
  cbuf->length = 0;
  return 0;
}

variable_types: struct cbuf { char *data; unsigned long capacity; int head; unsigned long length; } *


decompiled_code:
void __fastcall cbuf_free(void **a1)
{
free(*a1);
}

canonical_decompiled_code:
void __fastcall func8(void **a1)
{
free(*a1);
}

original_code:
void cbuf_free(struct cbuf *cbuf) {
  free(cbuf->data);
}

user_defined_types: struct cbuf { char *data; unsigned long capacity; int head; unsigned long length; }


original_code:
size_t cbuf_write(struct cbuf *cbuf, char *data, size_t length) {
  size_t chunk;
  if (length > cbuf->capacity) {
    memcpy(cbuf->data, &data[length - cbuf->capacity], cbuf->capacity);
    cbuf->head = 0;
    cbuf->length = cbuf->capacity;
    return length;
  }
  cbuf->length += length;
  if (cbuf->length > cbuf->capacity) {
    cbuf->length = cbuf->capacity;
  }
  if (cbuf->head + length > cbuf->capacity) {
    chunk = cbuf->capacity - cbuf->head;
    memcpy(&cbuf->data[cbuf->head], data, chunk);
    memcpy(cbuf->data, data, length - chunk);
    cbuf->head = length - chunk;
  } else {
    memcpy(&cbuf->data[cbuf->head], data, length);
    cbuf->head += length;
    if (cbuf->head == cbuf->capacity) {
      cbuf->head = 0;
    }
  }
  return length;
}

canonical_original_code:
unsigned long cbuf_write(struct cbuf *cbuf, char *data, unsigned long length) {
  unsigned long chunk;
  if (length > cbuf->capacity) {
    memcpy(cbuf->data, &data[length - cbuf->capacity], cbuf->capacity);
    cbuf->head = 0;
    cbuf->length = cbuf->capacity;
    return length;
  }
  cbuf->length += length;
  if (cbuf->length > cbuf->capacity) {
    cbuf->length = cbuf->capacity;
  }
  if (cbuf->head + length > cbuf->capacity) {
    chunk = cbuf->capacity - cbuf->head;
    memcpy(&cbuf->data[cbuf->head], data, chunk);
    memcpy(cbuf->data, data, length - chunk);
    cbuf->head = length - chunk;
  } else {
    memcpy(&cbuf->data[cbuf->head], data, length);
    cbuf->head += length;
    if (cbuf->head == cbuf->capacity) {
      cbuf->head = 0;
    }
  }
  return length;
}

variable_types:
data
char *

variable_types:
length
unsigned long

variable_types:
chunk
unsigned long

return_type: unsigned long


decompiled_code:
__int64 __fastcall naldetector_eat(int *a1, char a2)
{
int v2; // eax
__int64 result; // rax

v2 = *a1;
if ( *a1 == 3 )
{
if ( a2 == 1 )
{
result = 4LL;
if ( a1[1] )
{
*a1 = 4;
result = 0LL;
}
}
else
{
result = 0LL;
if ( a2 )
*a1 = 0;
}
}
else if ( v2 == 4 )
{
if ( !a1[1] )
fwrite("Warning: this naldetector must be reinitialized before detecting again", 1uLL, 0x46uLL, stderr);
result = 5LL;
if ( (a2 & 0x1F) != 5 )
{
*a1 = 0;
result = 0LL;
}
}
else
{
if ( a2 )
*a1 = 0;
else
*a1 = v2 + 1;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(int *a1, char a2)
{
int v2; // eax
__int64 result; // rax

v2 = *a1;
if ( *a1 == 3 )
{
if ( a2 == 1 )
{
result = 4LL;
if ( a1[1] )
{
*a1 = 4;
result = 0LL;
}
}
else
{
result = 0LL;
if ( a2 )
*a1 = 0;
}
}
else if ( v2 == 4 )
{
if ( !a1[1] )
fwrite("Warning: this naldetector must be reinitialized before detecting again", 1uLL, 0x46uLL, stderr);
result = 5LL;
if ( (a2 & 0x1F) != 5 )
{
*a1 = 0;
result = 0LL;
}
}
else
{
if ( a2 )
*a1 = 0;
else
*a1 = v2 + 1;
result = 0LL;
}
return result;
}

original_code:
int naldetector_eat(struct naldetector *detector, char c) {
  if (detector->pattern_bytes_ok == 4 - 1) {
    if (c == 0x01) {
      if (detector->detect_idr_only) {
        detector->pattern_bytes_ok = 4;
        return 0;
      }
      return 4;
    }
    if (c != 0x00) {
      detector->pattern_bytes_ok = 0;
    }
  } else if (detector->pattern_bytes_ok == 4) {
    if (!detector->detect_idr_only) {
      fprintf(stderr, "Warning: this naldetector must be reinitialized before detecting again");
    }
    if ((c & 0x1f) == 5) {
      return 4 + 1;
    }
    detector->pattern_bytes_ok = 0;
    return 0;
  } else if (c == 0x00) {
    detector->pattern_bytes_ok++;
  } else {
    detector->pattern_bytes_ok = 0;
  }
  return 0;
}

variable_types: struct naldetector { int pattern_bytes_ok; int detect_idr_only; } *

variable_types: c char


usage

decompiled_code:
int usage()
{
puts("Usage: gameOfLife [options...]");
puts(" --size -s size of the squared grid (default: 30)");
puts(" --delay -d time between two iterations in ms (default: 100)");
puts(" --cell -c character used to draw a cell (default: '#')");
puts(" --nbiter -n number of iterations (default: 0 (infinite))");
puts(" --border -b border of the grid (0 = no border (default), 1 = fixed border)");
puts(" --pattern -p path to the pattern file");
return puts(" --help display this help");
}

canonical_decompiled_code:
int func0()
{
puts("Usage: gameOfLife [options...]");
puts(" --size -s size of the squared grid (default: 30)");
puts(" --delay -d time between two iterations in ms (default: 100)");
puts(" --cell -c character used to draw a cell (default: '#')");
puts(" --nbiter -n number of iterations (default: 0 (infinite))");
puts(" --border -b border of the grid (0 = no border (default), 1 = fixed border)");
puts(" --pattern -p path to the pattern file");
return puts(" --help display this help");
}

original_code:
void usage(void) {
    printf("Usage: gameOfLife [options...]\n");
    printf("   --size       -s size of the squared grid (default: 30)\n");
    printf("   --delay      -d time between two iterations in ms (default: 100)\n");
    printf("   --cell       -c character used to draw a cell (default: '#')\n");
    printf("   --nbiter     -n number of iterations (default: 0 (infinite))\n");
    printf("   --border     -b border of the grid (0 = no border (default), 1 = fixed border)\n");
    printf("   --pattern    -p path to the pattern file\n");
    printf("   --help          display this help\n");
}

canonical_original_code:
void usage() {
    printf("Usage: gameOfLife [options...]\n");
    printf("   --size       -s size of the squared grid (default: 30)\n");
    printf("   --delay      -d time between two iterations in ms (default: 100)\n");
    printf("   --cell       -c character used to draw a cell (default: '#')\n");
    printf("   --nbiter     -n number of iterations (default: 0 (infinite))\n");
    printf("   --border     -b border of the grid (0 = no border (default), 1 = fixed border)\n");
    printf("   --pattern    -p path to the pattern file\n");
    printf("   --help          display this help\n");
}


initParams

original_code:
void initParams(Params *params) {
    params->size = -1;
    params->sizex = -1;
    params->sizey = -1;
    params->delay = 0;
    params->cell = '#';
    params->nbiter = 0;
    params->filename[0] = '\0';
}

canonical_original_code:
void initParams(struct Params *params) {
    params->size = -1;
    params->sizex = -1;
    params->sizey = -1;
    params->delay = 0;
    params->cell = '#';
    params->nbiter = 0;
    params->filename[0] = '\0';
}

variable_types: struct Params { int size; int sizex; int sizey; int border; long delay; char cell; unsigned int nbiter; int graphmode; char filename[1024]; } *


parseOptions

decompiled_code:
int __fastcall parseOptions(int argc, char **argv)
{
int result; // eax
int longind[7]; // [rsp+Ch] [rbp-1Ch] BYREF

while ( 1 )
{
longind[0] = 0;
result = getopt_long(argc, argv, "s:d:c:n:b:p:g:h:", &long_options_0, longind);
if ( result == -1 )
break;
if ( !result )
{
usage();
exit(0);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(int argc, char **argv)
{
int result; // eax
int longind[7]; // [rsp+Ch] [rbp-1Ch] BYREF

while ( 1 )
{
longind[0] = 0;
result = getopt_long(argc, argv, "s:d:c:n:b:p:g:h:", &long_options_0, longind);
if ( result == -1 )
break;
if ( !result )
{
func0();
exit(0);
}
}
return result;
}

original_code:
void parseOptions(int argc, char **argv, Params *params) {
    int c;
    while(1) {
        static struct option long_options[] =
        {
            {"help", 0 , &help_tag, 0 },
            {"size", 1, 0, 's'},
            {"delay", 1, 0, 'd'},
            {"cell", 1, 0, 'c'},
            {"nbiter", 1, 0, 'n'},
            {"border", 1, 0, 'b'},
            {"pattern", 1, 0, 'p'},
            {0,0,0,0}
        };
        int option_index = 0;
        c = getopt_long(argc, argv, "s:d:c:n:b:p:g:h:", long_options, &option_index);
        if (c == -1)
            break;
        switch(c) {
          case 0:
            usage();
            exit(0);
          case 's':
            params->size = atoi(optarg);
            break;
          case 'p':
            strcpy(params->filename, optarg);
            break;
          case 'd':
            params->delay = atol(optarg);
            break;
          case 'c':
            params->cell = optarg[0];
            break;
          case 'b':
            params->border = atoi(optarg);
            break;
          case 'n':
            params->nbiter = atoi(optarg);
            break;
        }
    }
}

canonical_original_code:
void parseOptions(int argc, char **argv, struct Params *params) {
    int c;
    while(1) {
        static struct option long_options[] = {
            {"help", 0 , &help_tag, 0 },
            {"size", 1, 0, 's'},
            {"delay", 1, 0, 'd'},
            {"cell", 1, 0, 'c'},
            {"nbiter", 1, 0, 'n'},
            {"border", 1, 0, 'b'},
            {"pattern", 1, 0, 'p'},
            {0,0,0,0}
        };
        int option_index = 0;
        c = getopt_long(argc, argv, "s:d:c:n:b:p:g:h:", long_options, &option_index);
        if (c == -1)
            break;
        switch(c) {
          case 0:
            usage();
            exit(0);
          case 's':
            params->size = atoi(optarg);
            break;
          case 'p':
            strcpy(params->filename, optarg);
            break;
          case 'd':
            params->delay = atol(optarg);
            break;
          case 'c':
            params->cell = optarg[0];
            break;
          case 'b':
            params->border = atoi(optarg);
            break;
          case 'n':
            params->nbiter = atoi(optarg);
            break;
        }
    }
}

variable_types:
argv
char **

variable_types: c int

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []

user_defined_types: struct Params { int size; int sizex; int sizey; int border; long delay; char cell; unsigned int nbiter; int graphmode; char filename[1024]; }


checkRuleFixedBorder

canonical_original_code:
int checkRuleFixedBorder(struct Grid *grid, int x, int y){
    int **g = grid->g;
    int sizex = grid->sizex;
    int sizey = grid->sizey;
    if ( x > 0 && x < sizex-1 && y > 0 && y < sizey-1) {
 return g[x-1][y-1]
             + g[x-1][y]
             + g[x-1][y+1]
      + g[x][y-1]
             + g[x][y+1]
      + g[x+1][y-1]
             + g[x+1][y]
             + g [x+1][y+1];
    }
    else if (x == 0) {
       if (y == 0) {
                  return g[0][1]
                       + g[1][1]
                       + g[1][0];
              }
              else if (y == sizey-1) {
    return g[0][sizey-2]
                       + g[1][sizey-1]
                       + g[1][sizey-2];
              }
       else return g[0][y-1]
                        + g[1][y-1]
          + g[1][y] + g[1][y+1]
          + g[0][y+1];
    }
    else if (x == sizex-1) {
       if (y == sizey-1) {
    return g[sizex-1][sizey-2]
         + g[sizex-2][sizey-2]
         + g[sizex-2][sizey-1];
              }
              else if (y == 0) {
    return g[sizex-1][1]
         + g[sizex-2][0]
         + g[sizex-2][1];
              }
              else return g[sizex-1][y-1]
                        + g[sizex-1][y+1]
                        + g[sizex-2][y-1]
                        + g[sizex-2][y]
                        + g[sizex-2][y+1];
    }
    else if (y == sizey-1) {
 return g[x-1][sizey-1]
             + g[x-1][sizey-2]
      + g[x][sizey-2]
             + g[x+1][sizey-2]
      + g[x+1][sizey-1];
    }
    else return g[x-1][0]
              + g[x-1][1]
              + g[x][1]
              + g[x+1][1]
              + g[x+1][0];
}

variable_types: struct Grid { int **g; int sizex; int sizey; } *

variable_types: x int

variable_types: y int

variable_types:
g
int **

variable_types: sizex int

variable_types: sizey int


checkRuleNoBorder

original_code:
int checkRuleNoBorder(Grid *grid, int x, int y){
    int **g = grid->g;
    int sizex = grid->sizex;
    int sizey = grid->sizey;
    int xm1 = (x-1) > -1 ? x-1 : sizex-1;
    int ym1 = (y-1) > -1 ? y-1 : sizey-1;
    int xp1 = (x+1) > sizex-1 ? 0 : x+1;
    int yp1 = (y-1) > sizey-1 ? 0 : y+1;
    return g[xm1][ym1]
         + g[xm1][y]
         + g[xm1][yp1]
         + g[x][ym1]
         + g[x][yp1]
         + g[xp1][ym1]
         + g[xp1][y]
         + g[xp1][yp1];
}

canonical_original_code:
int checkRuleNoBorder(struct Grid *grid, int x, int y){
    int **g = grid->g;
    int sizex = grid->sizex;
    int sizey = grid->sizey;
    int xm1 = (x-1) > -1 ? x-1 : sizex-1;
    int ym1 = (y-1) > -1 ? y-1 : sizey-1;
    int xp1 = (x+1) > sizex-1 ? 0 : x+1;
    int yp1 = (y-1) > sizey-1 ? 0 : y+1;
    return g[xm1][ym1]
         + g[xm1][y]
         + g[xm1][yp1]
         + g[x][ym1]
         + g[x][yp1]
         + g[xp1][ym1]
         + g[xp1][y]
         + g[xp1][yp1];
}

user_defined_types: struct Grid { int **g; int sizex; int sizey; }


update

original_code:
void update(Grid *from, Grid *to, int (*checkRule)(Grid*, int, int)) {
    int i,j,res;
    int sizex = from->sizex;
    int sizey = from->sizey;
    for ( i = 0 ; i < sizex ; i++)
        for ( j = 0 ; j < sizey ; j++) {
     res = checkRule(from, i, j);
     switch(res){
  case 2 :
      if (from->g[i][j] == 0) to->g[i][j] = 0;
      else to->g[i][j] = 1;
      break;
  case 3 :
      to->g[i][j] = 1;
      break;
  default :
      to->g[i][j] = 0;
      break;
     }
 }
}

canonical_original_code:
void update(struct Grid *from, struct Grid *to, int (*checkRule)(struct Grid *, int, int)) {
    int i; int j; int res;
    int sizex = from->sizex;
    int sizey = from->sizey;
    for ( i = 0 ; i < sizex ; i++)
        for ( j = 0 ; j < sizey ; j++) {
     res = checkRule(from, i, j);
     switch(res){
  case 2 :
      if (from->g[i][j] == 0) to->g[i][j] = 0;
      else to->g[i][j] = 1;
      break;
  case 3 :
      to->g[i][j] = 1;
      break;
  default :
      to->g[i][j] = 0;
      break;
     }
 }
}

variable_types:
to
struct Grid *

variable_types:
checkRule
int (*)(struct Grid *, int, int)

variable_types: i int

variable_types: j int

variable_types: res int


initGrid

decompiled_code:
void *__fastcall initGrid(__int64 a1, int a2, int a3)
{
void *result; // rax
__int64 v5; // rbx
_QWORD *v6; // r12

*(_DWORD *)(a1 + 8) = a2;
*(_DWORD *)(a1 + 12) = a3;
result = malloc(8LL * a2);
*(_QWORD *)a1 = result;
if ( a2 > 0 )
{
v5 = 0LL;
do
{
v6 = (_QWORD *)(*(_QWORD *)a1 + v5);
result = calloc(a3, 4uLL);
*v6 = result;
v5 += 8LL;
}
while ( v5 != 8LL * (unsigned int)a2 );
}
return result;
}

canonical_decompiled_code:
void *__fastcall func0(__int64 a1, int a2, int a3)
{
void *result; // rax
__int64 v5; // rbx
_QWORD *v6; // r12

*(_DWORD *)(a1 + 8) = a2;
*(_DWORD *)(a1 + 12) = a3;
result = malloc(8LL * a2);
*(_QWORD *)a1 = result;
if ( a2 > 0 )
{
v5 = 0LL;
do
{
v6 = (_QWORD *)(*(_QWORD *)a1 + v5);
result = calloc(a3, 4uLL);
*v6 = result;
v5 += 8LL;
}
while ( v5 != 8LL * (unsigned int)a2 );
}
return result;
}

original_code:
void initGrid(Grid *grid, int sizex, int sizey) {
    grid->sizex = sizex;
    grid->sizey = sizey;
    grid->g = (int**)malloc(sizex * sizeof(int*));
    for(int i = 0; i < sizex ; i++)
      grid->g[i] = (int*)calloc(sizey,sizeof(int));
}

canonical_original_code:
void initGrid(struct Grid *grid, int sizex, int sizey) {
    grid->sizex = sizex;
    grid->sizey = sizey;
    grid->g = (int **)malloc(sizex * sizeof(int *));
    for(int i = 0; i < sizex ; i++)
      grid->g[i] = (int *)calloc(sizey,sizeof(int));
}


loadPatternToGrid

decompiled_code:
__int64 __fastcall loadPatternToGrid(__int64 a1, __int64 a2, int a3, int a4)
{
int v6; // esi
int v7; // edx
int v8; // er8
__int64 result; // rax
__int64 v10; // r11
__int64 v11; // rdx

v6 = *(_DWORD *)(a1 + 8);
v7 = *(_DWORD *)(a2 + 8);
if ( v6 > v7 || (v8 = *(_DWORD *)(a1 + 12), result = *(unsigned int *)(a2 + 12), v8 > (int)result) )
{
fwrite("ERROR: the pattern size is larger than the grid size. \n", 1uLL, 0x47uLL, stderr);
exit(1);
}
if ( (int)result <= v8 + a4 || v7 <= v6 + a3 )
{
fwrite("ERROR: the offset is too large for the grid.\n", 1uLL, 0x2DuLL, stderr);
exit(1);
}
if ( a3 < 0 || a4 < 0 )
{
a3 = (v7 - v6) / 2;
result = (unsigned int)(result - v8);
a4 = (int)result / 2;
}
if ( v6 > 0 )
{
v10 = 0LL;
do
{
if ( *(int *)(a1 + 12) > 0 )
{
v11 = 4LL * a4;
result = 0LL;
do
{
*(_DWORD *)(*(_QWORD *)(*(_QWORD *)a2 + 8LL * (a3 + (int)v10)) + v11) = *(_DWORD *)(*(_QWORD *)(*(_QWORD *)a1 + 8 * v10)
+ 4 * result++);
v11 += 4LL;
}
while ( *(_DWORD *)(a1 + 12) > (int)result );
}
++v10;
}
while ( *(_DWORD *)(a1 + 8) > (int)v10 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, int a3, int a4)
{
int v6; // esi
int v7; // edx
int v8; // er8
__int64 result; // rax
__int64 v10; // r11
__int64 v11; // rdx

v6 = *(_DWORD *)(a1 + 8);
v7 = *(_DWORD *)(a2 + 8);
if ( v6 > v7 || (v8 = *(_DWORD *)(a1 + 12), result = *(unsigned int *)(a2 + 12), v8 > (int)result) )
{
fwrite("ERROR: the pattern size is larger than the grid size. \n", 1uLL, 0x47uLL, stderr);
exit(1);
}
if ( (int)result <= v8 + a4 || v7 <= v6 + a3 )
{
fwrite("ERROR: the offset is too large for the grid.\n", 1uLL, 0x2DuLL, stderr);
exit(1);
}
if ( a3 < 0 || a4 < 0 )
{
a3 = (v7 - v6) / 2;
result = (unsigned int)(result - v8);
a4 = (int)result / 2;
}
if ( v6 > 0 )
{
v10 = 0LL;
do
{
if ( *(int *)(a1 + 12) > 0 )
{
v11 = 4LL * a4;
result = 0LL;
do
{
*(_DWORD *)(*(_QWORD *)(*(_QWORD *)a2 + 8LL * (a3 + (int)v10)) + v11) = *(_DWORD *)(*(_QWORD *)(*(_QWORD *)a1 + 8 * v10)
+ 4 * result++);
v11 += 4LL;
}
while ( *(_DWORD *)(a1 + 12) > (int)result );
}
++v10;
}
while ( *(_DWORD *)(a1 + 8) > (int)v10 );
}
return result;
}

original_code:
void loadPatternToGrid(Grid *pattern, Grid *grid, int x, int y) {
    if (pattern->sizex > grid->sizex || pattern->sizey > grid->sizey) {
        fprintf(stderr, "ERROR: the pattern size is larger than the grid size.                 \n");
        exit(1);
    }
    if (y+pattern->sizey > grid->sizey-1 || x+pattern->sizex > grid->sizex-1) {
        fprintf(stderr, "ERROR: the offset is too large for the grid.\n");
        exit(1);
    }
    int x_offset = x, y_offset = y;
    if (x < 0 || y < 0) {
        x_offset = (grid->sizex - pattern->sizex) / 2;
        y_offset = (grid->sizey - pattern->sizey) / 2;
    }
    for(int i = 0; i < pattern->sizex; i++)
        for(int j = 0; j < pattern->sizey; j++)
            grid->g[x_offset+i][y_offset+j] = pattern->g[i][j];
}

canonical_original_code:
void loadPatternToGrid(struct Grid *pattern, struct Grid *grid, int x, int y) {
    if (pattern->sizex > grid->sizex || pattern->sizey > grid->sizey) {
        fprintf(stderr, "ERROR: the pattern size is larger than the grid size.                 \n");
        exit(1);
    }
    if (y+pattern->sizey > grid->sizey-1 || x+pattern->sizex > grid->sizex-1) {
        fprintf(stderr, "ERROR: the offset is too large for the grid.\n");
        exit(1);
    }
    int x_offset = x; int y_offset = y;
    if (x < 0 || y < 0) {
        x_offset = (grid->sizex - pattern->sizex) / 2;
        y_offset = (grid->sizey - pattern->sizey) / 2;
    }
    for(int i = 0; i < pattern->sizex; i++)
        for(int j = 0; j < pattern->sizey; j++)
            grid->g[x_offset+i][y_offset+j] = pattern->g[i][j];
}

variable_types:
grid
struct Grid *


generateRandomPatternOnGrid

decompiled_code:
void __fastcall generateRandomPatternOnGrid(__int64 a1, double a2)
{
double v2; // r13
unsigned int v3; // eax
__int64 i; // r14
__int64 j; // rbx

v2 = a2;
v3 = time(0LL);
srand(v3);
if ( a2 <= 0.0 )
{
v2 = *(double *)&LC7;
}
else if ( a2 >= 1.0 )
{
v2 = *(double *)&LC7;
}
for ( i = 0LL; *(_DWORD *)(a1 + 8) > (int)i; ++i )
{
for ( j = 0LL; *(_DWORD *)(a1 + 12) > (int)j; ++j )
*(_DWORD *)(*(_QWORD *)(*(_QWORD *)a1 + 8 * i) + 4 * j) = v2 >= (double)rand() / 2147483647.0;
}
}

original_code:
void generateRandomPatternOnGrid(Grid *grid, double live) {
    double l = live;
    srand(time(((void *)0)));
    if ( l <= 0 || l >=1 )
        l = 0.5;
    for (int w = 0; w < grid->sizex ; w++)
        for (int h = 0; h < grid->sizey ; h++) {
            if((double)rand()/(double)2147483647 <= l)
                grid->g[w][h] = 1;
            else grid->g[w][h] = 0;
        }
}

canonical_original_code:
void generateRandomPatternOnGrid(struct Grid *grid, double live) {
    double l = live;
    srand(time(((void *)0)));
    if ( l <= 0 || l >=1 )
        l = 0.5;
    for (int w = 0; w < grid->sizex ; w++)
        for (int h = 0; h < grid->sizey ; h++) {
            if((double)rand()/(double)2147483647 <= l)
                grid->g[w][h] = 1;
            else grid->g[w][h] = 0;
        }
}

variable_types: live double

variable_types: l double

variable_types: w int

variable_types: h int


freeGrid

original_code:
void freeGrid(Grid *pattern) {
    for (int i = 0; i < pattern->sizey; i++)
        free(pattern->g[i]);
    free(pattern->g);
}

canonical_original_code:
void freeGrid(struct Grid *pattern) {
    for (int i = 0; i < pattern->sizey; i++)
        free(pattern->g[i]);
    free(pattern->g);
}


donothing

repo: romiras/BlackBox-linux

decompiled_code:
void donothing()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
int donothing(char* fmt, ...)
{
}

canonical_original_code:
int donothing(char *fmt, ...)
{
}

variable_types:
fmt
char *


DumpMod

decompiled_code:
void DumpMod()
{
;
}

canonical_decompiled_code:
void func1()
{
;
}

original_code:
void DumpMod()
{
  donothing("\n\n---- Mod info:\n");
  donothing("        hs, ms, ds, cs, vs = %d, %d, %d, %d, %d\n",
  mod.hs, mod.ms, mod.ds, mod.cs, mod.vs);
  donothing("        mad, dad = %d, %d\n\n", mod.mad, mod.dad);
}


RegisterModule

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

result = *((int *)&mod + 73);
*(_QWORD *)result = modlist;
modlist = result;
return result;
}

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

result = *((int *)&mod + 73);
*(_QWORD *)result = modlist;
modlist = result;
return result;
}

original_code:
void RegisterModule()
{
  Module* m;
  m = (Module*)mod.dad;
  m->next = modlist;
  modlist = m;
  donothing("Registred module %s\n", mod.name);
}

canonical_original_code:
void RegisterModule()
{
  struct Module *m;
  m = (struct Module *)mod.dad;
  m->next = modlist;
  modlist = m;
  donothing("Registred module %s\n", mod.name);
}

variable_types: struct Module { struct Module *next; int opts; int refcnt; short compTime[6]; int ext; int term; int nofimps; int csize; int code; int procBase; char *names; int *ptrs; struct Module *imports; struct Directory { int num; struct Object { int fprint; int offs; int id; struct Type { int size; struct Module *mod; int id; int base[16]; int fields; int ptroffs[1000000]; } *ostruct; } obj[1000000]; } *export; char name[256]; } *

user_defined_types: struct Module { struct Module *next; int opts; int refcnt; short compTime[6]; int ext; int term; int nofimps; int csize; int code; int procBase; char *names; int *ptrs; struct Module *imports; struct Directory *export; char name[256]; }

user_defined_types: struct Directory { int num; struct Object obj[1000000]; }

user_defined_types: struct Object { int fprint; int offs; int id; struct Type *ostruct; }


PrintMods

decompiled_code:
_QWORD *PrintMods()
{
_QWORD *result; // rax

result = (_QWORD *)modlist;
if ( modlist )
{
do
result = (_QWORD *)*result;
while ( result );
}
return result;
}

canonical_decompiled_code:
_QWORD *func3()
{
_QWORD *result; // rax

result = (_QWORD *)modlist;
if ( modlist )
{
do
result = (_QWORD *)*result;
while ( result );
}
return result;
}

original_code:
void PrintMods()
{
  Module* ml;
  ml = modlist;
  donothing("Loaded Modules\n");
  while (ml != ((void *)0)){
    donothing("mod name: %s\n", ml->name);
    ml = ml->next;
  }
  donothing("end of list\n");
}

canonical_original_code:
void PrintMods()
{
  struct Module *ml;
  ml = modlist;
  donothing("Loaded Modules\n");
  while (ml != ((void *)0)){
    donothing("mod name: %s\n", ml->name);
    ml = ml->next;
  }
  donothing("end of list\n");
}

user_defined_types: struct Type { int size; struct Module *mod; int id; int base[16]; int fields; int ptroffs[1000000]; }


ThisModule

decompiled_code:
__int64 __fastcall ThisModule(char *s2)
{
__int64 i; // rbx

for ( i = modlist; i; i = *(_QWORD *)i )
{
if ( !strcmp((const char *)(i + 120), s2) )
break;
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *s2)
{
__int64 i; // rbx

for ( i = modlist; i; i = *(_QWORD *)i )
{
if ( !strcmp((const char *)(i + 120), s2) )
break;
}
return i;
}

original_code:
Module* ThisModule(char* name)
{
  Module* ml;
  ml = modlist;
  while ((ml != ((void *)0)) && (strcmp(ml->name, name) != 0)){
    ml = ml->next;
  }
  return ml;
}

canonical_original_code:
struct Module *ThisModule(char *name)
{
  struct Module *ml;
  ml = modlist;
  while ((ml != ((void *)0)) && (strcmp(ml->name, name) != 0)){
    ml = ml->next;
  }
  return ml;
}

variable_types:
name
char *

return_type: struct Module { struct Module *next; int opts; int refcnt; short compTime[6]; int ext; int term; int nofimps; int csize; int code; int procBase; char *names; int *ptrs; struct Module *imports; struct Directory { int num; struct Object { int fprint; int offs; int id; struct Type { int size; struct Module *mod; int id; int base[16]; int fields; int ptroffs[1000000]; } *ostruct; } obj[1000000]; } *export; char name[256]; } *


ThisObject

original_code:
Object* ThisObject(Module* mod, char* name)
{
  int l, r, m;
  char* p;
  l = 0; r = mod->export->num;
  while (l < r){
    m = (l + r) / 2;
    p = (char*) &(mod->names[mod->export->obj[m].id / 256]);
    if (strcmp(p, name) == 0)
      return (Object*)&(mod->export->obj[m]);
    if (strcmp(p, name) < 0)
      l = m + 1;
    else
      r = m;
  }
  return ((void *)0);
}

canonical_original_code:
struct Object *ThisObject(struct Module *mod, char *name)
{
  int l; int r; int m;
  char *p;
  l = 0; r = mod->export->num;
  while (l < r){
    m = (l + r) / 2;
    p = (char *) &(mod->names[mod->export->obj[m].id / 256]);
    if (strcmp(p, name) == 0)
      return (struct Object *)&(mod->export->obj[m]);
    if (strcmp(p, name) < 0)
      l = m + 1;
    else
      r = m;
  }
  return ((void *)0);
}

variable_types:
mod
struct Module *

variable_types: l int

variable_types: r int

variable_types: m int

variable_types:
p
char *

return_type: struct Object { int fprint; int offs; int id; struct Type { int size; struct Module { struct Module *next; int opts; int refcnt; short compTime[6]; int ext; int term; int nofimps; int csize; int code; int procBase; char *names; int *ptrs; struct Module *imports; struct Directory { int num; struct Object obj[1000000]; } *export; char name[256]; } *mod; int id; int base[16]; int fields; int ptroffs[1000000]; } *ostruct; } *


ThisDesc

original_code:
Object* ThisDesc(Module* mod, int fprint)
{
  int i, n;
  i = 0; n = mod->export->num;
  while ((i < n) && (mod->export->obj[i].id / 256 == 0))
    {
      if (mod->export->obj[i].offs == fprint)
 return (Object*)&(mod->export->obj[i]);
      i++;
    }
  return ((void *)0);
}

canonical_original_code:
struct Object *ThisDesc(struct Module *mod, int fprint)
{
  int i; int n;
  i = 0; n = mod->export->num;
  while ((i < n) && (mod->export->obj[i].id / 256 == 0))
    {
      if (mod->export->obj[i].offs == fprint)
 return (struct Object *)&(mod->export->obj[i]);
      i++;
    }
  return ((void *)0);
}

variable_types: fprint int

variable_types: n int


LoadDll

decompiled_code:
__int64 __fastcall LoadDll(const char *a1)
{
char *v2; // rax

if ( !dlopen(a1, 257) )
{
printf("LoadDll: failed to load lib %s\n", a1);
v2 = dlerror();
printf(" - dlerror: %s\n", v2);
exit(-1);
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(const char *a1)
{
char *v2; // rax

if ( !dlopen(a1, 257) )
{
printf("LoadDll: failed to load lib %s\n", a1);
v2 = dlerror();
printf(" - dlerror: %s\n", v2);
exit(-1);
}
return 1LL;
}

original_code:
int LoadDll (char* name)
{
  void *handle;
  donothing("loading: %s\n", name);
  if ((handle = dlopen(name, 0x00001 + 0x00100)) == ((void *)0)) {
    printf("LoadDll: failed to load lib %s\n", name);
    printf(" - dlerror: %s\n", dlerror());
    exit(-1);
  }
  return handle != ((void *)0);
}

canonical_original_code:
int LoadDll(char *name)
{
  void *handle;
  donothing("loading: %s\n", name);
  if ((handle = dlopen(name, 0x00001 + 0x00100)) == ((void *)0)) {
    printf("LoadDll: failed to load lib %s\n", name);
    printf(" - dlerror: %s\n", dlerror());
    exit(-1);
  }
  return handle != ((void *)0);
}

variable_types:
handle
void *


ThisDllObj

decompiled_code:
void *__fastcall ThisDllObj(int a1, __int64 a2, const char *a3, const char *a4)
{
void *result; // rax
void *v7; // rdi
char *v8; // rax

result = 0LL;
if ( (unsigned int)(a1 - 3) <= 1 )
{
v7 = dlopen(a3, 257);
if ( !v7 )
{
printf("ThisDllObj: lib %s not found\n", a3);
v8 = dlerror();
printf(" - dlerror: %s\n", v8);
exit(-1);
}
result = dlsym(v7, a4);
if ( !(_DWORD)result )
{
printf("ThisDllObj: symbol %s not found\n", a4);
exit(-1);
}
}
return result;
}

canonical_decompiled_code:
void *__fastcall func8(int a1, __int64 a2, const char *a3, const char *a4)
{
void *result; // rax
void *v7; // rdi
char *v8; // rax

result = 0LL;
if ( (unsigned int)(a1 - 3) <= 1 )
{
v7 = dlopen(a3, 257);
if ( !v7 )
{
printf("ThisDllObj: lib %s not found\n", a3);
v8 = dlerror();
printf(" - dlerror: %s\n", v8);
exit(-1);
}
result = dlsym(v7, a4);
if ( !(_DWORD)result )
{
printf("ThisDllObj: symbol %s not found\n", a4);
exit(-1);
}
}
return result;
}

original_code:
int ThisDllObj (int mode, int fprint, char* dll, char* name)
{
  void *handle;
  int ad = 0;
  if ((mode == 0x3) || (mode == 0x4)){
    if ((handle = dlopen(dll, 0x00001 + 0x00100)) == ((void *)0)) {
      printf("ThisDllObj: lib %s not found\n", dll);
      printf(" - dlerror: %s\n", dlerror());
      exit(-1);
    } else {
      ad = (int)dlsym((void *) handle, name);
      if (ad == 0) {
 printf("ThisDllObj: symbol %s not found\n", name); exit(-1);
      }
    }
  }
  return ad;
 }

canonical_original_code:
int ThisDllObj(int mode, int fprint, char *dll, char *name)
{
  void *handle;
  int ad = 0;
  if ((mode == 0x3) || (mode == 0x4)){
    if ((handle = dlopen(dll, 0x00001 + 0x00100)) == ((void *)0)) {
      printf("ThisDllObj: lib %s not found\n", dll);
      printf(" - dlerror: %s\n", dlerror());
      exit(-1);
    } else {
      ad = (int)dlsym((void *) handle, name);
      if (ad == 0) {
 printf("ThisDllObj: symbol %s not found\n", name); exit(-1);
      }
    }
  }
  return ad;
 }

variable_types: mode int

variable_types:
dll
char *

variable_types: ad int


decompiled_code:
__int64 Read4()
{
unsigned __int8 v0; // bp
int v1; // ebp
int v2; // ebx

v0 = fgetc(f);
v1 = ((unsigned __int8)fgetc(f) << 8) + v0;
v2 = v1 + ((unsigned __int8)fgetc(f) << 16);
return (unsigned int)(v2 + (fgetc(f) << 24));
}

canonical_decompiled_code:
__int64 func9()
{
unsigned __int8 v0; // bp
int v1; // ebp
int v2; // ebx

v0 = fgetc(f);
v1 = ((unsigned __int8)fgetc(f) << 8) + v0;
v2 = v1 + ((unsigned __int8)fgetc(f) << 16);
return (unsigned int)(v2 + (fgetc(f) << 24));
}

original_code:
int Read4 ()
{
  unsigned char b;
  int w;
  b = fgetc(f); w = b % 256;
  b = fgetc(f); w = w + 0x100 * (b % 256);
  b = fgetc(f); w = w + 0x10000 * (b % 256);
  b = fgetc(f); w = w + 0x1000000 * b;
  return w;
}

canonical_original_code:
int Read4()
{
  unsigned char b;
  int w;
  b = fgetc(f); w = b % 256;
  b = fgetc(f); w = w + 0x100 * (b % 256);
  b = fgetc(f); w = w + 0x10000 * (b % 256);
  b = fgetc(f); w = w + 0x1000000 * b;
  return w;
}

variable_types:
b
unsigned char


RNum

variable_types: b char

variable_types: s int

variable_types: y int


Fixup

decompiled_code:
int __fastcall Fixup(int a1)
{
int result; // eax
int v2; // ebx
unsigned int v3; // er13
int v4; // er14
__int64 v5; // rsi
int v6; // eax
__int64 v7; // rcx
unsigned int v8; // ebp
unsigned int *v9; // r12
__int64 v10; // r8

result = RNum();
if ( result )
{
v2 = result;
do
{
v3 = RNum();
if ( v2 )
{
v4 = a1 + v3;
while ( 2 )
{
if ( v2 <= 0 )
{
v5 = (unsigned int)-v2;
v6 = *((_DWORD *)&mod + 68);
if ( v6 <= (int)v5 )
v7 = (unsigned int)(*((_DWORD *)&mod + 73) - v2 - v6);
else
v7 = (unsigned int)(*((_DWORD *)&mod + 72) - v2);
}
else
{
v7 = (unsigned int)(v2 + *((_DWORD *)&mod + 72) + *((_DWORD *)&mod + 68));
v5 = (unsigned int)v2;
}
v9 = (unsigned int *)(int)v7;
v8 = *(_DWORD *)(int)v7;
v10 = (unsigned int)((int)v8 / 0x1000000);
v2 = (int)(v8 + 0x800000) % 0x1000000 - 0x800000;
switch ( (int)v10 )
{
case 'd':
v8 = a1 + v3;
goto LABEL_9;
case 'e':
v8 = v4 - v7 - 4;
goto LABEL_9;
case 'f':
v8 = *(_DWORD *)v4;
goto LABEL_9;
case 'g':
v8 = a1 + v2;
v2 = v5 + 4;
goto LABEL_9;
case 'h':
*(_DWORD *)(int)v7 = a1 + v2;
break;
case 'i':
v8 = *(_DWORD *)(a1 + 2) + v3;
goto LABEL_9;
case 'j':
puts("fixup: halfword not implemented");
LABEL_9:
*v9 = v8;
if ( v2 )
continue;
goto LABEL_17;
default:
return printf("fixup error(link=%d, offset=%d, linkadr=%d, t=%d, x=%d)\n", v5, v3, v7, v10, v8);
}
break;
}
}
LABEL_17:
result = RNum();
v2 = result;
}
while ( result );
}
return result;
}

canonical_decompiled_code:
int __fastcall func12(int a1)
{
int result; // eax
int v2; // ebx
unsigned int v3; // er13
int v4; // er14
__int64 v5; // rsi
int v6; // eax
__int64 v7; // rcx
unsigned int v8; // ebp
unsigned int *v9; // r12
__int64 v10; // r8

result = func10();
if ( result )
{
v2 = result;
do
{
v3 = func10();
if ( v2 )
{
v4 = a1 + v3;
while ( 2 )
{
if ( v2 <= 0 )
{
v5 = (unsigned int)-v2;
v6 = *((_DWORD *)&mod + 68);
if ( v6 <= (int)v5 )
v7 = (unsigned int)(*((_DWORD *)&mod + 73) - v2 - v6);
else
v7 = (unsigned int)(*((_DWORD *)&mod + 72) - v2);
}
else
{
v7 = (unsigned int)(v2 + *((_DWORD *)&mod + 72) + *((_DWORD *)&mod + 68));
v5 = (unsigned int)v2;
}
v9 = (unsigned int *)(int)v7;
v8 = *(_DWORD *)(int)v7;
v10 = (unsigned int)((int)v8 / 0x1000000);
v2 = (int)(v8 + 0x800000) % 0x1000000 - 0x800000;
switch ( (int)v10 )
{
case 'd':
v8 = a1 + v3;
goto LABEL_9;
case 'e':
v8 = v4 - v7 - 4;
goto LABEL_9;
case 'f':
v8 = *(_DWORD *)v4;
goto LABEL_9;
case 'g':
v8 = a1 + v2;
v2 = v5 + 4;
goto LABEL_9;
case 'h':
*(_DWORD *)(int)v7 = a1 + v2;
break;
case 'i':
v8 = *(_DWORD *)(a1 + 2) + v3;
goto LABEL_9;
case 'j':
puts("fixup: halfword not implemented");
LABEL_9:
*v9 = v8;
if ( v2 )
continue;
goto LABEL_17;
default:
return printf("fixup error(link=%d, offset=%d, linkadr=%d, t=%d, x=%d)\n", v5, v3, v7, v10, v8);
}
break;
}
}
LABEL_17:
result = func10();
v2 = result;
}
while ( result );
}
return result;
}

original_code:
void Fixup (int adr)
{
  int link, offset, linkadr, n, x, t;
  donothing("fixup: %X ", adr);
  link = RNum();
  while (link != 0)
    {
      offset = RNum();
      donothing("+%d: ", offset);
      while (link != 0)
 {
   if (link > 0)
     {
       donothing("c");
       linkadr = mod.mad + mod.ms + link;
     }
   else
     {
       donothing("d");
       link = -link;
       if (link < mod.ms)
  linkadr = mod.mad + link;
       else
  linkadr = mod.dad + link - mod.ms;
     }
   donothing("%X ", link);
   x = *(int*)linkadr;
   t = x / 0x1000000;
   n = (x + 0x800000) % 0x1000000 - 0x800000;
   switch (t) {
   case 100: x = adr + offset; break;
   case 101: x = adr + offset - linkadr -4; break;
   case 102: x = *(int*)(adr + offset); break;
   case 103: x = adr + n; n = link + 4; break;
   case 104: x = adr + n; n = 0; break;
   case 105: x = *(int*)(adr + 2); x = x + offset; break;
   case 106:
     printf("fixup: halfword not implemented\n");
     break;
   default:
     printf("fixup error(link=%d, offset=%d, linkadr=%d, t=%d, x=%d)\n",
     link, offset, linkadr, t, x);
     return;
   }
   *(int*)linkadr = x;
   link = n;
 }
      link = RNum();
    }
  donothing("\n");
}

canonical_original_code:
void Fixup(int adr)
{
  int link; int offset; int linkadr; int n; int x; int t;
  donothing("fixup: %X ", adr);
  link = RNum();
  while (link != 0)
    {
      offset = RNum();
      donothing("+%d: ", offset);
      while (link != 0)
 {
   if (link > 0)
     {
       donothing("c");
       linkadr = mod.mad + mod.ms + link;
     }
   else
     {
       donothing("d");
       link = -link;
       if (link < mod.ms)
  linkadr = mod.mad + link;
       else
  linkadr = mod.dad + link - mod.ms;
     }
   donothing("%X ", link);
   x = *(int *)linkadr;
   t = x / 0x1000000;
   n = (x + 0x800000) % 0x1000000 - 0x800000;
   switch (t) {
   case 100: x = adr + offset; break;
   case 101: x = adr + offset - linkadr -4; break;
   case 102: x = *(int *)(adr + offset); break;
   case 103: x = adr + n; n = link + 4; break;
   case 104: x = adr + n; n = 0; break;
   case 105: x = *(int *)(adr + 2); x = x + offset; break;
   case 106:
     printf("fixup: halfword not implemented\n");
     break;
   default:
     printf("fixup error(link=%d, offset=%d, linkadr=%d, t=%d, x=%d)\n",
     link, offset, linkadr, t, x);
     return;
   }
   *(int *)linkadr = x;
   link = n;
 }
      link = RNum();
    }
  donothing("\n");
}

variable_types: adr int

variable_types: link int

variable_types: offset int

variable_types: linkadr int

variable_types: x int

variable_types: t int


ReadBootHeader

decompiled_code:
__int64 ReadBootHeader()
{
int v0; // ebx
int v1; // eax

fseek(f, 21399LL, 0);
v0 = Read4();
v1 = Read4();
if ( v0 != 978017389 || v1 )
return 0LL;
nofMods = Read4();
ReadName((__int64)kernel);
ReadName((__int64)mainmod);
newRecFP = Read4();
newRecAdr = 0;
newArrFP = Read4();
newArrAdr = 0;
*((_DWORD *)&mod + 66) = ftell(f);
return 1LL;
}

canonical_decompiled_code:
__int64 func13()
{
int v0; // ebx
int v1; // eax

fseek(f, 21399LL, 0);
v0 = func9();
v1 = func9();
if ( v0 != 978017389 || v1 )
return 0LL;
nofMods = func9();
func11((__int64)kernel);
func11((__int64)mainmod);
newRecFP = func9();
newRecAdr = 0;
newArrFP = func9();
newArrAdr = 0;
*((_DWORD *)&mod + 66) = ftell(f);
return 1LL;
}

original_code:
int ReadBootHeader()
{
  int tag, version;
  fseek(f, 21399, 0);
  tag = Read4();
  version = Read4();
  if ((tag != 0x3A4B5C6D) || (version != 0))
    {
      return 0;
    }
  nofMods = Read4();
  donothing("Linked modules: %d\n", nofMods);
  ReadName(kernel);
  donothing("kernel: %s\n", kernel);
  ReadName(mainmod);
  donothing("main: %s\n", mainmod);
  newRecFP = Read4(); newRecAdr = 0;
  newArrFP = Read4(); newArrAdr = 0;
  mod.start = ftell(f);
  return 1;
}

canonical_original_code:
int ReadBootHeader()
{
  int tag; int version;
  fseek(f, 21399, 0);
  tag = Read4();
  version = Read4();
  if ((tag != 0x3A4B5C6D) || (version != 0))
    {
      return 0;
    }
  nofMods = Read4();
  donothing("Linked modules: %d\n", nofMods);
  ReadName(kernel);
  donothing("kernel: %s\n", kernel);
  ReadName(mainmod);
  donothing("main: %s\n", mainmod);
  newRecFP = Read4(); newRecAdr = 0;
  newArrFP = Read4(); newArrAdr = 0;
  mod.start = ftell(f);
  return 1;
}

variable_types: tag int

variable_types: version int


ReadHeader

decompiled_code:
__int64 __fastcall ReadHeader()
{
char *v0; // rbx
unsigned int v1; // ebp
int v3; // er14
int v4; // er12
_QWORD *v5; // r13

if ( (unsigned int)Read4() == 1867465542 )
{
Read4();
*((_DWORD *)&mod + 67) = Read4();
*((_DWORD *)&mod + 68) = Read4();
*((_DWORD *)&mod + 69) = Read4();
*((_DWORD *)&mod + 70) = Read4();
*((_DWORD *)&mod + 71) = Read4();
v3 = RNum();
ReadName((unsigned int)((_DWORD)&mod + 8));
mod = 0LL;
if ( v3 <= 0 )
{
LABEL_15:
ftell(f);
v1 = 1;
}
else
{
v4 = 0;
while ( 1 )
{
v5 = v0;
v0 = (char *)calloc(1uLL, 0x108uLL);
ReadName((__int64)(v0 + 8));
if ( mod )
*v5 = v0;
else
mod = (__int64)v0;
*(_QWORD *)v0 = 0LL;
if ( *((_WORD *)v0 + 4) == 9252 )
strcpy(v0 + 8, "Kernel");
if ( v0[8] == 36 )
{
v1 = LoadDll(v0 + 9);
if ( !v1 )
break;
}
if ( v3 == ++v4 )
goto LABEL_15;
}
printf("Could not load lib: %s\n", (const char *)(unsigned int)v0[9]);
}
}
else
{
puts("wrong object file version");
v1 = 0;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func14()
{
char *v0; // rbx
unsigned int v1; // ebp
int v3; // er14
int v4; // er12
_QWORD *v5; // r13

if ( (unsigned int)func9() == 1867465542 )
{
func9();
*((_DWORD *)&mod + 67) = func9();
*((_DWORD *)&mod + 68) = func9();
*((_DWORD *)&mod + 69) = func9();
*((_DWORD *)&mod + 70) = func9();
*((_DWORD *)&mod + 71) = func9();
v3 = func10();
func11((unsigned int)((_DWORD)&mod + 8));
mod = 0LL;
if ( v3 <= 0 )
{
LABEL_15:
ftell(f);
v1 = 1;
}
else
{
v4 = 0;
while ( 1 )
{
v5 = v0;
v0 = (char *)calloc(1uLL, 0x108uLL);
func11((__int64)(v0 + 8));
if ( mod )
*v5 = v0;
else
mod = (__int64)v0;
*(_QWORD *)v0 = 0LL;
if ( *((_WORD *)v0 + 4) == 9252 )
strcpy(v0 + 8, "Kernel");
if ( v0[8] == 36 )
{
v1 = func7(v0 + 9);
if ( !v1 )
break;
}
if ( v3 == ++v4 )
goto LABEL_15;
}
printf("Could not load lib: %s\n", (const char *)(unsigned int)v0[9]);
}
}
else
{
puts("wrong object file version");
v1 = 0;
}
return v1;
}

original_code:
int ReadHeader ()
{
  int ofTag, i, nofImps, processor;
  char str[80];
  ImpList *imp, *last;
  char* n;
  ofTag = Read4();
  if (ofTag != 0x6F4F4346)
    {
      printf("wrong object file version\n");
      return 0;
    }
  processor = Read4();
  mod.hs = Read4();
  mod.ms = Read4();
  mod.ds = Read4();
  mod.cs = Read4();
  mod.vs = Read4();
  donothing("File tag: %d ", ofTag); donothing("Processor: %d\n", processor);
  donothing("Header size: %d ", mod.hs);
  donothing("Meta size: %d ", mod.ms);
  donothing("Desc size: %d ", mod.ds );
  donothing("Code size: %d ", mod.cs);
  donothing("Data size: %d\n", mod.vs);
  nofImps = RNum(); donothing("Nof imports: %d\n", nofImps);
  ReadName(mod.name); donothing("Module name: %s\n", mod.name);
  mod.imp = ((void *)0);
  for (i = 0; i < nofImps; i++)
    {
      imp = (ImpList*)calloc(1, sizeof(ImpList));
      ReadName(imp->name);
      if (mod.imp == ((void *)0))
 mod.imp = imp;
      else
 last->next = imp;
      last = imp;
      last->next = ((void *)0);
      donothing("Import %d: %s\n", i, imp->name);
      if ((imp->name[0] == '$') && (imp->name[1] == '$'))
 strcpy(imp->name, "Kernel");
      if (imp->name[0] == '$'){
 n = imp->name;
 n++;
 if (!LoadDll(n)){
   printf("Could not load lib: %s\n", imp->name[1]);
   return 0;
 }
      }
    }
  donothing("Pos: %d\n", ftell(f));
  return 1;
}

canonical_original_code:
int ReadHeader()
{
  int ofTag; int i; int nofImps; int processor;
  char str[80];
  struct ImpList *imp; struct ImpList *last;
  char *n;
  ofTag = Read4();
  if (ofTag != 0x6F4F4346)
    {
      printf("wrong object file version\n");
      return 0;
    }
  processor = Read4();
  mod.hs = Read4();
  mod.ms = Read4();
  mod.ds = Read4();
  mod.cs = Read4();
  mod.vs = Read4();
  donothing("File tag: %d ", ofTag); donothing("Processor: %d\n", processor);
  donothing("Header size: %d ", mod.hs);
  donothing("Meta size: %d ", mod.ms);
  donothing("Desc size: %d ", mod.ds );
  donothing("Code size: %d ", mod.cs);
  donothing("Data size: %d\n", mod.vs);
  nofImps = RNum(); donothing("Nof imports: %d\n", nofImps);
  ReadName(mod.name); donothing("Module name: %s\n", mod.name);
  mod.imp = ((void *)0);
  for (i = 0; i < nofImps; i++)
    {
      imp = (struct ImpList *)calloc(1, sizeof(ImpList));
      ReadName(imp->name);
      if (mod.imp == ((void *)0))
 mod.imp = imp;
      else
 last->next = imp;
      last = imp;
      last->next = ((void *)0);
      donothing("Import %d: %s\n", i, imp->name);
      if ((imp->name[0] == '$') && (imp->name[1] == '$'))
 strcpy(imp->name, "Kernel");
      if (imp->name[0] == '$'){
 n = imp->name;
 n++;
 if (!LoadDll(n)){
   printf("Could not load lib: %s\n", imp->name[1]);
   return 0;
 }
      }
    }
  donothing("Pos: %d\n", ftell(f));
  return 1;
}

variable_types: ofTag int

variable_types: i int

variable_types: nofImps int

variable_types: processor int

variable_types:
str
char [80]

variable_types: struct ImpList { struct ImpList *next; char name[256]; } *

variable_types:
n
char *


ReadModule

decompiled_code:
__int64 __fastcall ReadModule()
{
__int64 v0; // r15
signed int v1; // ebx
unsigned int v2; // eax
signed int v3; // ebp
__int64 v4; // rax
__int64 v5; // rbx
__int64 v6; // rax
__int64 v7; // rax
unsigned int v8; // ebx
unsigned int v9; // er13
__int64 v10; // r12
unsigned int v11; // eax
unsigned int v12; // ebp
int v13; // er14
int v14; // eax
int v16; // eax
__int64 v17; // [rsp+0h] [rbp-148h]
_DWORD *v18; // [rsp+8h] [rbp-140h]
char v19[312]; // [rsp+10h] [rbp-138h] BYREF

v1 = (unsigned int)calloc(1uLL, *((int *)&mod + 69));
*((_DWORD *)&mod + 73) = v1;
v2 = (unsigned int)calloc(1uLL, *((_DWORD *)&mod + 71) + *((_DWORD *)&mod + 68) + *((_DWORD *)&mod + 70));
*((_DWORD *)&mod + 72) = v2;
if ( v2 && (v3 = v2, v1) )
{
fseek(f, *((_DWORD *)&mod + 66) + *((_DWORD *)&mod + 67), 0);
ftell(f);
fread((void *)v3, 1uLL, *((int *)&mod + 68), f);
ftell(f);
fread((void *)v1, 1uLL, *((int *)&mod + 69), f);
ftell(f);
fread((void *)(*((_DWORD *)&mod + 72) + *((_DWORD *)&mod + 68)), 1uLL, *((int *)&mod + 70), f);
ftell(f);
ftell(f);
if ( !newRecAdr || !newArrAdr )
{
v4 = ThisModule(kernel);
v5 = v4;
if ( v4 )
{
v6 = ThisObject(v4, "NewRec");
if ( v6 )
newRecAdr = *(_DWORD *)(v5 + 80) + *(_DWORD *)(v6 + 4);
v7 = ThisObject(v5, "NewArr");
if ( v7 )
newArrAdr = *(_DWORD *)(v5 + 80) + *(_DWORD *)(v7 + 4);
}
}
Fixup(newRecAdr);
Fixup(newArrAdr);
Fixup(*((_DWORD *)&mod + 72));
Fixup(*((_DWORD *)&mod + 73));
Fixup(*((_DWORD *)&mod + 72) + *((_DWORD *)&mod + 68));
Fixup(*((_DWORD *)&mod + 70) + *((_DWORD *)&mod + 72) + *((_DWORD *)&mod + 68));
ftell(f);
v17 = mod;
if ( mod )
{
v18 = (_DWORD *)(int)*(_QWORD *)((char *)ThisObject + *((int *)&mod + 73) + 1);
do
{
v8 = RNum();
if ( *(_WORD *)(v17 + 8) == 9252 )
printf("should be Kerneln");
v9 = 1;
if ( *(_BYTE *)(v17 + 8) != 36 )
{
v0 = ThisModule((char *)(v17 + 8));
if ( !v0 )
{
puts("invalid import list");
return 0;
}
v9 = 0;
}
for ( ; v8; v8 = RNum() )
{
ReadName((__int64)v19);
v11 = RNum();
v12 = v11;
if ( v9 )
{
if ( v8 - 3 <= 1 )
{
v16 = (unsigned int)ThisDllObj(v8, v11, (const char *)(v17 + 9), v19);
if ( !v16 )
goto LABEL_48;
Fixup(v16);
}
else if ( v8 == 2 )
{
RNum();
if ( (unsigned int)RNum() )
{
LABEL_48:
printf("ReadModule: Object not found: %s\n", v19);
return 0;
}
}
}
else
{
if ( v19[0] )
v10 = ThisObject(v0, v19);
else
v10 = ThisDesc(v0, v11);
if ( !v10 )
{
puts("obj == NULL");
LABEL_43:
printf("descriptor not found (%s, x: %d, id: %d)\n", v19, v8, *(unsigned int *)(v10 + 8));
return v9;
}
if ( *(_DWORD *)(v10 + 8) % 16 != v8 )
goto LABEL_43;
v13 = *(_DWORD *)v10;
switch ( v8 )
{
case 3u:
Fixup(*(_DWORD *)(v0 + 84) + *(_DWORD *)(v10 + 4));
break;
case 4u:
Fixup(*(_DWORD *)(v0 + 80) + *(_DWORD *)(v10 + 4));
break;
case 2u:
v14 = RNum();
if ( v14 % 2 == 1 )
v13 = *(_DWORD *)(v10 + 4);
if ( v14 > 1
&& ((((unsigned int)(v14 >> 31) >> 28) + (unsigned __int8)(*(_DWORD *)(v10 + 8) / 16)) & 0xF)
- ((unsigned int)(v14 >> 31) >> 28) != 4 )
{
printf("object not found (%s)\n", (const char *)(v17 + 8));
return v9;
}
Fixup(*(_QWORD *)(v10 + 16));
break;
}
if ( v13 != v12 )
{
printf("illigal foot print (%s)\n", (const char *)(v17 + 8));
return v9;
}
}
}
*v18 = v0;
v17 = *(_QWORD *)v17;
++v18;
}
while ( v17 );
}
*((_DWORD *)&mod + 66) = ftell(f);
v9 = 1;
}
else
{
puts("BootLoader: Couldn't initalize heap");
free((void *)*((int *)&mod + 73));
free((void *)*((int *)&mod + 72));
v9 = 0;
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func15()
{
__int64 v0; // r15
signed int v1; // ebx
unsigned int v2; // eax
signed int v3; // ebp
__int64 v4; // rax
__int64 v5; // rbx
__int64 v6; // rax
__int64 v7; // rax
unsigned int v8; // ebx
unsigned int v9; // er13
__int64 v10; // r12
unsigned int v11; // eax
unsigned int v12; // ebp
int v13; // er14
int v14; // eax
int v16; // eax
__int64 v17; // [rsp+0h] [rbp-148h]
_DWORD *v18; // [rsp+8h] [rbp-140h]
char v19[312]; // [rsp+10h] [rbp-138h] BYREF

v1 = (unsigned int)calloc(1uLL, *((int *)&mod + 69));
*((_DWORD *)&mod + 73) = v1;
v2 = (unsigned int)calloc(1uLL, *((_DWORD *)&mod + 71) + *((_DWORD *)&mod + 68) + *((_DWORD *)&mod + 70));
*((_DWORD *)&mod + 72) = v2;
if ( v2 && (v3 = v2, v1) )
{
fseek(f, *((_DWORD *)&mod + 66) + *((_DWORD *)&mod + 67), 0);
ftell(f);
fread((void *)v3, 1uLL, *((int *)&mod + 68), f);
ftell(f);
fread((void *)v1, 1uLL, *((int *)&mod + 69), f);
ftell(f);
fread((void *)(*((_DWORD *)&mod + 72) + *((_DWORD *)&mod + 68)), 1uLL, *((int *)&mod + 70), f);
ftell(f);
ftell(f);
if ( !newRecAdr || !newArrAdr )
{
v4 = func4(kernel);
v5 = v4;
if ( v4 )
{
v6 = func5(v4, "NewRec");
if ( v6 )
newRecAdr = *(_DWORD *)(v5 + 80) + *(_DWORD *)(v6 + 4);
v7 = func5(v5, "NewArr");
if ( v7 )
newArrAdr = *(_DWORD *)(v5 + 80) + *(_DWORD *)(v7 + 4);
}
}
func12(newRecAdr);
func12(newArrAdr);
func12(*((_DWORD *)&mod + 72));
func12(*((_DWORD *)&mod + 73));
func12(*((_DWORD *)&mod + 72) + *((_DWORD *)&mod + 68));
func12(*((_DWORD *)&mod + 70) + *((_DWORD *)&mod + 72) + *((_DWORD *)&mod + 68));
ftell(f);
v17 = mod;
if ( mod )
{
v18 = (_DWORD *)(int)*(_QWORD *)((char *)ThisObject + *((int *)&mod + 73) + 1);
do
{
v8 = func10();
if ( *(_WORD *)(v17 + 8) == 9252 )
printf("should be Kerneln");
v9 = 1;
if ( *(_BYTE *)(v17 + 8) != 36 )
{
v0 = func4((char *)(v17 + 8));
if ( !v0 )
{
puts("invalid import list");
return 0;
}
v9 = 0;
}
for ( ; v8; v8 = func10() )
{
func11((__int64)v19);
v11 = func10();
v12 = v11;
if ( v9 )
{
if ( v8 - 3 <= 1 )
{
v16 = (unsigned int)func8(v8, v11, (const char *)(v17 + 9), v19);
if ( !v16 )
goto LABEL_48;
func12(v16);
}
else if ( v8 == 2 )
{
func10();
if ( (unsigned int)func10() )
{
LABEL_48:
printf("ReadModule: Object not found: %s\n", v19);
return 0;
}
}
}
else
{
if ( v19[0] )
v10 = func5(v0, v19);
else
v10 = func6(v0, v11);
if ( !v10 )
{
puts("obj == NULL");
LABEL_43:
printf("descriptor not found (%s, x: %d, id: %d)\n", v19, v8, *(unsigned int *)(v10 + 8));
return v9;
}
if ( *(_DWORD *)(v10 + 8) % 16 != v8 )
goto LABEL_43;
v13 = *(_DWORD *)v10;
switch ( v8 )
{
case 3u:
func12(*(_DWORD *)(v0 + 84) + *(_DWORD *)(v10 + 4));
break;
case 4u:
func12(*(_DWORD *)(v0 + 80) + *(_DWORD *)(v10 + 4));
break;
case 2u:
v14 = func10();
if ( v14 % 2 == 1 )
v13 = *(_DWORD *)(v10 + 4);
if ( v14 > 1
&& ((((unsigned int)(v14 >> 31) >> 28) + (unsigned __int8)(*(_DWORD *)(v10 + 8) / 16)) & 0xF)
- ((unsigned int)(v14 >> 31) >> 28) != 4 )
{
printf("object not found (%s)\n", (const char *)(v17 + 8));
return v9;
}
func12(*(_QWORD *)(v10 + 16));
break;
}
if ( v13 != v12 )
{
printf("illigal foot print (%s)\n", (const char *)(v17 + 8));
return v9;
}
}
}
*v18 = v0;
v17 = *(_QWORD *)v17;
++v18;
}
while ( v17 );
}
*((_DWORD *)&mod + 66) = ftell(f);
v9 = 1;
}
else
{
puts("BootLoader: Couldn't initalize heap");
free((void *)*((int *)&mod + 73));
free((void *)*((int *)&mod + 72));
v9 = 0;
}
return v9;
}

original_code:
int ReadModule ()
{
  char *dp, *mp;
  unsigned int cnt;
  ImpList* imp;
  int x, fp, opt, link, ofp, imptab, a;
  Module *desc, *k;
  String name;
  Object* obj;
  int isLib;
  char* im;
  mod.dad = (int) calloc(1, mod.ds);
  mod.mad = (int) calloc(1, mod.ms + mod.cs + mod.vs);
  if ((mod.dad == 0) || (mod.mad == 0))
    {
      printf("BootLoader: Couldn't initalize heap\n");
      free((void*)mod.dad);
      free((void*)mod.mad);
      return 0;
    }
  dp = (char*) mod.dad;
  mp = (char*) mod.mad;
  fseek(f, mod.start + mod.hs, 0);
  donothing("ReadModule after fseek pos: %d\n", ftell(f));
  cnt = fread(mp, 1, mod.ms, f);
  donothing("Read meta bulk (%d bytes. New pos: %d)\n", cnt, ftell(f));
  cnt = fread(dp, 1, mod.ds, f);
  donothing("Read desc bulk (%d bytes. New pos: %d)\n", cnt, ftell(f));
  mp = (char*)(mod.mad + mod.ms);
  cnt = fread(mp, 1, mod.cs, f);
  donothing("Read code bulk (%d bytes. New pos: %d)\n", cnt, ftell(f));
  DumpMod();
  donothing("before fixup: pos = %d\n", ftell(f));
  if ((!newRecAdr) || (!newArrAdr)){
    k = ThisModule(kernel);
    if (k != ((void *)0)){
      obj = ThisObject(k, "NewRec");
      if (obj != ((void *)0))
 newRecAdr = k->procBase + obj->offs;
      obj = ThisObject(k, "NewArr");
      if (obj != ((void *)0))
 newArrAdr = k->procBase + obj->offs;
      donothing("newRecFP: %X  newArrFP: %X\n", newRecFP, newArrFP);
      donothing("newRecAdr: %X  newArrAdr: %X\n", newRecAdr, newArrAdr);
    } else {
      donothing("no kernel before %s.\n", mod.name);
    }
  }
  Fixup(newRecAdr);
  Fixup(newArrAdr);
  Fixup(mod.mad);
  Fixup(mod.dad);
  Fixup(mod.mad + mod.ms);
  Fixup(mod.mad + mod.ms + mod.cs);
  donothing("after fixup: pos = %d\n", ftell(f));
  imp = mod.imp; imptab = (int)((Module*)(mod.dad))->imports;
  while (imp != ((void *)0)){
    x = RNum();
    if ((imp->name[0] == '$') && (imp->name[1] == '$'))
      printf("should be Kerneln");
    if (imp->name[0] == '$')
      isLib = 1;
    else{
      isLib = 0;
      desc = ThisModule(imp->name);
      if (desc == ((void *)0)){
 printf("invalid import list\n");
 return 0;
      }
    }
    while (x != 0) {
      ReadName(name); fp = RNum(); opt = 0;
      if (!isLib) {
 if (name[0] == 0)
   obj = ThisDesc(desc, fp);
 else
   obj = ThisObject(desc, name);
 if ((obj != ((void *)0)) && (obj->id % 16 == x)){
   ofp = obj->fprint;
   switch (x){
   case 0x2:
     opt = RNum();
     if (opt % 2 == 1) ofp = obj->offs;
     if ((opt > 1) && ((obj->id / 16) % 16 != 4)){
       printf("object not found (%s)\n", imp->name);
       return 0;
  }
     Fixup((int)obj->ostruct);
     break;
   case 0x3:
     Fixup(desc->varBase + obj->offs);
     break;
   case 0x4:
     Fixup(desc->procBase + obj->offs);
   }
   if (ofp != fp){
     printf("illigal foot print (%s)\n", imp->name);
     return 0;
   }
 } else {
   if (obj == ((void *)0)) printf("obj == NULL\n");
   printf("descriptor not found (%s, x: %d, id: %d)\n", name, x, obj->id);
   return 0;
 }
      }else{
 if ((x == 0x3) || (x == 0x4)){
   im = imp->name;
   im++;
   a = ThisDllObj(x, fp, im, name);
   if (a != 0)
     Fixup(a);
   else{
     printf("ReadModule: Object not found: %s\n", name);
     return 0;
   }
 } else {
   if (x == 0x2) {
     opt = RNum();
     x = RNum();
     if (x != 0) {
       printf("ReadModule: Object not found: %s\n", name);
       return 0;
     }
   }
 }
      }
      x = RNum();
    }
    *(int*)imptab = (int)desc; imptab += 4;
    imp = imp->next;
  }
  mod.start = ftell(f);
  return 1;
}

canonical_original_code:
int ReadModule()
{
  char *dp; char *mp;
  unsigned int cnt;
  struct ImpList *imp;
  int x; int fp; int opt; int link; int ofp; int imptab; int a;
  struct Module *desc; struct Module *k;
  char name[256];
  struct Object *obj;
  int isLib;
  char *im;
  mod.dad = (int) calloc(1, mod.ds);
  mod.mad = (int) calloc(1, mod.ms + mod.cs + mod.vs);
  if ((mod.dad == 0) || (mod.mad == 0))
    {
      printf("BootLoader: Couldn't initalize heap\n");
      free((void *)mod.dad);
      free((void *)mod.mad);
      return 0;
    }
  dp = (char *) mod.dad;
  mp = (char *) mod.mad;
  fseek(f, mod.start + mod.hs, 0);
  donothing("ReadModule after fseek pos: %d\n", ftell(f));
  cnt = fread(mp, 1, mod.ms, f);
  donothing("Read meta bulk (%d bytes. New pos: %d)\n", cnt, ftell(f));
  cnt = fread(dp, 1, mod.ds, f);
  donothing("Read desc bulk (%d bytes. New pos: %d)\n", cnt, ftell(f));
  mp = (char *)(mod.mad + mod.ms);
  cnt = fread(mp, 1, mod.cs, f);
  donothing("Read code bulk (%d bytes. New pos: %d)\n", cnt, ftell(f));
  DumpMod();
  donothing("before fixup: pos = %d\n", ftell(f));
  if ((!newRecAdr) || (!newArrAdr)){
    k = ThisModule(kernel);
    if (k != ((void *)0)){
      obj = ThisObject(k, "NewRec");
      if (obj != ((void *)0))
 newRecAdr = k->procBase + obj->offs;
      obj = ThisObject(k, "NewArr");
      if (obj != ((void *)0))
 newArrAdr = k->procBase + obj->offs;
      donothing("newRecFP: %X  newArrFP: %X\n", newRecFP, newArrFP);
      donothing("newRecAdr: %X  newArrAdr: %X\n", newRecAdr, newArrAdr);
    } else {
      donothing("no kernel before %s.\n", mod.name);
    }
  }
  Fixup(newRecAdr);
  Fixup(newArrAdr);
  Fixup(mod.mad);
  Fixup(mod.dad);
  Fixup(mod.mad + mod.ms);
  Fixup(mod.mad + mod.ms + mod.cs);
  donothing("after fixup: pos = %d\n", ftell(f));
  imp = mod.imp; imptab = (int)((struct Module *)(mod.dad))->imports;
  while (imp != ((void *)0)){
    x = RNum();
    if ((imp->name[0] == '$') && (imp->name[1] == '$'))
      printf("should be Kerneln");
    if (imp->name[0] == '$')
      isLib = 1;
    else{
      isLib = 0;
      desc = ThisModule(imp->name);
      if (desc == ((void *)0)){
 printf("invalid import list\n");
 return 0;
      }
    }
    while (x != 0) {
      ReadName(name); fp = RNum(); opt = 0;
      if (!isLib) {
 if (name[0] == 0)
   obj = ThisDesc(desc, fp);
 else
   obj = ThisObject(desc, name);
 if ((obj != ((void *)0)) && (obj->id % 16 == x)){
   ofp = obj->fprint;
   switch (x){
   case 0x2:
     opt = RNum();
     if (opt % 2 == 1) ofp = obj->offs;
     if ((opt > 1) && ((obj->id / 16) % 16 != 4)){
       printf("object not found (%s)\n", imp->name);
       return 0;
  }
     Fixup((int)obj->ostruct);
     break;
   case 0x3:
     Fixup(desc->varBase + obj->offs);
     break;
   case 0x4:
     Fixup(desc->procBase + obj->offs);
   }
   if (ofp != fp){
     printf("illigal foot print (%s)\n", imp->name);
     return 0;
   }
 } else {
   if (obj == ((void *)0)) printf("obj == NULL\n");
   printf("descriptor not found (%s, x: %d, id: %d)\n", name, x, obj->id);
   return 0;
 }
      }else{
 if ((x == 0x3) || (x == 0x4)){
   im = imp->name;
   im++;
   a = ThisDllObj(x, fp, im, name);
   if (a != 0)
     Fixup(a);
   else{
     printf("ReadModule: Object not found: %s\n", name);
     return 0;
   }
 } else {
   if (x == 0x2) {
     opt = RNum();
     x = RNum();
     if (x != 0) {
       printf("ReadModule: Object not found: %s\n", name);
       return 0;
     }
   }
 }
      }
      x = RNum();
    }
    *(int *)imptab = (int)desc; imptab += 4;
    imp = imp->next;
  }
  mod.start = ftell(f);
  return 1;
}

variable_types:
dp
char *

variable_types:
mp
char *

variable_types:
cnt
unsigned int

variable_types: fp int

variable_types: opt int

variable_types: ofp int

variable_types: imptab int

variable_types: a int

variable_types: struct Module { struct Module *next; int opts; int refcnt; short compTime[6]; int ext; int term; int nofimps; int csize; int code; int procBase; char *names; int *ptrs; struct Module *imports; struct Directory { int num; struct Object { int fprint; int offs; int id; struct Type { int size; struct Module *mod; int id; int base[16]; int fields; int ptroffs[1000000]; } *ostruct; } obj[1000000]; } *export; char name[256]; } *

variable_types:
name
char [256]

variable_types: struct Object { int fprint; int offs; int id; struct Type { int size; struct Module { struct Module *next; int opts; int refcnt; short compTime[6]; int ext; int term; int nofimps; int csize; int code; int procBase; char *names; int *ptrs; struct Module *imports; struct Directory { int num; struct Object obj[1000000]; } *export; char name[256]; } *mod; int id; int base[16]; int fields; int ptroffs[1000000]; } *ostruct; } *

variable_types: isLib int

variable_types:
im
char *

user_defined_types: struct ImpList { struct ImpList *next; char name[256]; }