decompiled_code:
__int64 __fastcall conditional_command(__int64 a1, __int64 a2, __int64 a3)
{
const char *v5; // r13
size_t v6; // r12
void *v7; // rax
char *v8; // rax
char *v9; // r12
__int64 v10; // r13
char *v11; // rax
bool v12; // dl
char *v13; // r14
char *v14; // rax
unsigned __int8 *v15; // r15
char *v16; // r13
int v17; // er12
bool v18; // cf
bool v19; // zf
int v20; // er13
const char *v21; // rsi
const char *v22; // rdi
__int64 v23; // rcx
__int64 result; // rax
int v25; // eax
int v26; // er12
int v27; // [rsp+Ch] [rbp-7Ch]
__int64 v28; // [rsp+10h] [rbp-78h]
__int64 v29[2]; // [rsp+18h] [rbp-70h]
unsigned __int8 *v30; // [rsp+28h] [rbp-60h]
char *nptr; // [rsp+30h] [rbp-58h]
__int64 v32; // [rsp+38h] [rbp-50h]
char *v33; // [rsp+40h] [rbp-48h]

v5 = *(const char **)(a2 + 8);
v29[0] = a3;
v30 = 0LL;
nptr = 0LL;
v32 = 0LL;
v33 = 0LL;
v6 = strlen(v5) + 1;
v7 = malloc(v6);
v8 = (char *)memcpy(v7, v5, v6);
v9 = strtok(v8, " ");
if ( v9 )
{
v10 = 2LL;
do
{
v11 = strtok(0LL, " ");
v12 = (int)v10 <= 4;
v29[v10++] = (__int64)v11;
}
while ( v11 != 0LL && v12 );
v13 = v33;
if ( v33 )
{
v14 = strtok(0LL, " ");
v15 = v30;
v16 = nptr;
v28 = v32;
if ( !v14 )
goto LABEL_7;
}
else
{
v15 = v30;
v16 = nptr;
v28 = v32;
}
}
else
{
v28 = 0LL;
v16 = 0LL;
v15 = 0LL;
v13 = 0LL;
}
simple_error(0LL, a2, "the conditional statement isn't well-formed");
LABEL_7:
if ( (unsigned int)check_variable_name_no_error(v9) )
{
v27 = simple_lookup_variable(v29[0], v9, a2);
v17 = determine_conditional(v15);
if ( v17 != 5 )
goto LABEL_9;
LABEL_23:
simple_error(0LL, a2, "invalid or unrecognizable conditional provided; valid choices are <, <=, ==, >=, and >");
if ( (unsigned int)check_variable_name_no_error(v16) )
goto LABEL_10;
goto LABEL_24;
}
if ( !(unsigned int)is_integer(v9) )
simple_error(0LL, a2, "the operands of the comparison should be variable names or integer literals");
v27 = atoi(v9);
v17 = determine_conditional(v15);
if ( v17 == 5 )
goto LABEL_23;
LABEL_9:
if ( (unsigned int)check_variable_name_no_error(v16) )
{
LABEL_10:
v20 = simple_lookup_variable(v29[0], v16, a2);
goto LABEL_11;
}
LABEL_24:
if ( !(unsigned int)is_integer(v16) )
simple_error(0LL, a2, "the operands of the comparison should be variable names or integer literals");
v20 = atoi(v16);
LABEL_11:
v21 = (const char *)v28;
v22 = "goto";
v23 = 5LL;
do
{
if ( !v23 )
break;
v18 = *v21 < (unsigned int)*v22;
v19 = *v21++ == *v22++;
--v23;
}
while ( v19 );
if ( (!v18 && !v19) != v18 )
{
v21 = (const char *)a2;
simple_error(0LL, a2, "the conditional statement isn't well-formed; is there a typo in the 'goto' token?");
}
if ( !(unsigned int)is_number(v13, v21) )
simple_error(
0LL,
a2,
"the final token of the conditional should be a positive integer indicating the line number to jump to");
result = perform_comparison(a2, v27, v20, v17);
if ( (_DWORD)result )
{
v25 = atoi(v13);
result = find_line_index((__int64 *)a1, v25);
v26 = result;
if ( (int)result < 0 )
result = simple_error(0LL, a2, "no line with the provided number is present in the file");
*(_DWORD *)(a1 + 16) = v26;
}
else
{
++*(_DWORD *)(a1 + 16);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, __int64 a3)
{
const char *v5; // r13
size_t v6; // r12
void *v7; // rax
char *v8; // rax
char *v9; // r12
__int64 v10; // r13
char *v11; // rax
bool v12; // dl
char *v13; // r14
char *v14; // rax
unsigned __int8 *v15; // r15
char *v16; // r13
int v17; // er12
bool v18; // cf
bool v19; // zf
int v20; // er13
const char *v21; // rsi
const char *v22; // rdi
__int64 v23; // rcx
__int64 result; // rax
int v25; // eax
int v26; // er12
int v27; // [rsp+Ch] [rbp-7Ch]
__int64 v28; // [rsp+10h] [rbp-78h]
__int64 v29[2]; // [rsp+18h] [rbp-70h]
unsigned __int8 *v30; // [rsp+28h] [rbp-60h]
char *nptr; // [rsp+30h] [rbp-58h]
__int64 v32; // [rsp+38h] [rbp-50h]
char *v33; // [rsp+40h] [rbp-48h]

v5 = *(const char **)(a2 + 8);
v29[0] = a3;
v30 = 0LL;
nptr = 0LL;
v32 = 0LL;
v33 = 0LL;
v6 = strlen(v5) + 1;
v7 = malloc(v6);
v8 = (char *)memcpy(v7, v5, v6);
v9 = strtok(v8, " ");
if ( v9 )
{
v10 = 2LL;
do
{
v11 = strtok(0LL, " ");
v12 = (int)v10 <= 4;
v29[v10++] = (__int64)v11;
}
while ( v11 != 0LL && v12 );
v13 = v33;
if ( v33 )
{
v14 = strtok(0LL, " ");
v15 = v30;
v16 = nptr;
v28 = v32;
if ( !v14 )
goto LABEL_7;
}
else
{
v15 = v30;
v16 = nptr;
v28 = v32;
}
}
else
{
v28 = 0LL;
v16 = 0LL;
v15 = 0LL;
v13 = 0LL;
}
simple_error(0LL, a2, "the conditional statement isn't well-formed");
LABEL_7:
if ( (unsigned int)check_variable_name_no_error(v9) )
{
v27 = simple_lookup_variable(v29[0], v9, a2);
v17 = func1(v15);
if ( v17 != 5 )
goto LABEL_9;
LABEL_23:
simple_error(0LL, a2, "invalid or unrecognizable conditional provided; valid choices are <, <=, ==, >=, and >");
if ( (unsigned int)check_variable_name_no_error(v16) )
goto LABEL_10;
goto LABEL_24;
}
if ( !(unsigned int)is_integer(v9) )
simple_error(0LL, a2, "the operands of the comparison should be variable names or integer literals");
v27 = atoi(v9);
v17 = func1(v15);
if ( v17 == 5 )
goto LABEL_23;
LABEL_9:
if ( (unsigned int)check_variable_name_no_error(v16) )
{
LABEL_10:
v20 = simple_lookup_variable(v29[0], v16, a2);
goto LABEL_11;
}
LABEL_24:
if ( !(unsigned int)is_integer(v16) )
simple_error(0LL, a2, "the operands of the comparison should be variable names or integer literals");
v20 = atoi(v16);
LABEL_11:
v21 = (const char *)v28;
v22 = "goto";
v23 = 5LL;
do
{
if ( !v23 )
break;
v18 = *v21 < (unsigned int)*v22;
v19 = *v21++ == *v22++;
--v23;
}
while ( v19 );
if ( (!v18 && !v19) != v18 )
{
v21 = (const char *)a2;
simple_error(0LL, a2, "the conditional statement isn't well-formed; is there a typo in the 'goto' token?");
}
if ( !(unsigned int)is_number(v13, v21) )
simple_error(
0LL,
a2,
"the final token of the conditional should be a positive integer indicating the line number to jump to");
result = func2(a2, v27, v20, v17);
if ( (_DWORD)result )
{
v25 = atoi(v13);
result = func0((__int64 *)a1, v25);
v26 = result;
if ( (int)result < 0 )
result = simple_error(0LL, a2, "no line with the provided number is present in the file");
*(_DWORD *)(a1 + 16) = v26;
}
else
{
++*(_DWORD *)(a1 + 16);
}
return result;
}

original_code:
void conditional_command(simple_file_t * file, simple_line_t * line, simple_variable_t variable_map[26])
{
 int a, b;
 int i = 0;
 char * token = ((void *)0);
 char * tokens[5] = {((void *)0), ((void *)0), ((void *)0), ((void *)0), ((void *)0)};
 enum SIMPLE_CONDITIONAL conditional = NO_MATCH;
 int comparison_value = 0;
 int goto_destination = -1;
 char * copy = malloc(strlen(line->rest) + 1);
 strcpy(copy, line->rest);
 token = strtok(copy, " ");
 tokens[0] = token;
 ++i;
 while (i < 5 && token != ((void *)0))
 {
  token = strtok(((void *)0), " ");
  tokens[i] = token;
  ++i;
 }
 if (tokens[4] == ((void *)0) || strtok(((void *)0), " ") != ((void *)0))
 {
  simple_error(SYNTAX_ERROR, line, "the conditional statement isn't well-formed");
 }
 if (check_variable_name_no_error(tokens[0]))
 {
  a = simple_lookup_variable(variable_map, tokens[0], line);
 }
 else
 {
  if (!is_integer(tokens[0]))
  {
   simple_error(SYNTAX_ERROR, line, "the operands of the comparison should be variable names or integer literals");
  }
  a = atoi(tokens[0]);
 }
 conditional = determine_conditional(tokens[1]);
 if (conditional == INVALID_CONDITIONAL)
 {
  simple_error(SYNTAX_ERROR, line, "invalid or unrecognizable conditional provided; valid choices are <, <=, ==, >=, and >");
 }
 if (check_variable_name_no_error(tokens[2]))
 {
  b = simple_lookup_variable(variable_map, tokens[2], line);
 }
 else
 {
  if (!is_integer(tokens[2]))
  {
   simple_error(SYNTAX_ERROR, line, "the operands of the comparison should be variable names or integer literals");
  }
  b = atoi(tokens[2]);
 }
 if (strcmp(tokens[3], "goto") != 0)
 {
  simple_error(SYNTAX_ERROR, line, "the conditional statement isn't well-formed; is there a typo in the 'goto' token?");
 }
 if (!is_number(tokens[4]))
 {
  simple_error(SYNTAX_ERROR, line, "the final token of the conditional should be a positive integer indicating the line number to jump to");
 }
 comparison_value = perform_comparison(line, a, b, conditional);
 if (comparison_value)
 {
  goto_destination = find_line_index(file, atoi(tokens[4]));
  if (goto_destination < 0)
  {
   simple_error(SYNTAX_ERROR, line, "no line with the provided number is present in the file");
  }
  file->next_line = goto_destination;
 }
 else
 {
  ++file->next_line;
 }
}

canonical_original_code:
void conditional_command(struct simple_file *file, struct simple_line *line, struct simple_variable variable_map[26])
{
 int a; int b;
 int i = 0;
 char *token = ((void *)0);
 char *tokens[5] = {((void *)0), ((void *)0), ((void *)0), ((void *)0), ((void *)0)};
 enum SIMPLE_CONDITIONAL conditional = NO_MATCH;
 int comparison_value = 0;
 int goto_destination = -1;
 char *copy = malloc(strlen(line->rest) + 1);
 strcpy(copy, line->rest);
 token = strtok(copy, " ");
 tokens[0] = token;
 ++i;
 while (i < 5 && token != ((void *)0))
 {
  token = strtok(((void *)0), " ");
  tokens[i] = token;
  ++i;
 }
 if (tokens[4] == ((void *)0) || strtok(((void *)0), " ") != ((void *)0))
 {
  simple_error(SYNTAX_ERROR, line, "the conditional statement isn't well-formed");
 }
 if (check_variable_name_no_error(tokens[0]))
 {
  a = simple_lookup_variable(variable_map, tokens[0], line);
 }
 else
 {
  if (!is_integer(tokens[0]))
  {
   simple_error(SYNTAX_ERROR, line, "the operands of the comparison should be variable names or integer literals");
  }
  a = atoi(tokens[0]);
 }
 conditional = determine_conditional(tokens[1]);
 if (conditional == INVALID_CONDITIONAL)
 {
  simple_error(SYNTAX_ERROR, line, "invalid or unrecognizable conditional provided; valid choices are <, <=, ==, >=, and >");
 }
 if (check_variable_name_no_error(tokens[2]))
 {
  b = simple_lookup_variable(variable_map, tokens[2], line);
 }
 else
 {
  if (!is_integer(tokens[2]))
  {
   simple_error(SYNTAX_ERROR, line, "the operands of the comparison should be variable names or integer literals");
  }
  b = atoi(tokens[2]);
 }
 if (strcmp(tokens[3], "goto") != 0)
 {
  simple_error(SYNTAX_ERROR, line, "the conditional statement isn't well-formed; is there a typo in the 'goto' token?");
 }
 if (!is_number(tokens[4]))
 {
  simple_error(SYNTAX_ERROR, line, "the final token of the conditional should be a positive integer indicating the line number to jump to");
 }
 comparison_value = perform_comparison(line, a, b, conditional);
 if (comparison_value)
 {
  goto_destination = find_line_index(file, atoi(tokens[4]));
  if (goto_destination < 0)
  {
   simple_error(SYNTAX_ERROR, line, "no line with the provided number is present in the file");
  }
  file->next_line = goto_destination;
 }
 else
 {
  ++file->next_line;
 }
}

variable_types:
line
struct simple_line *

variable_types: i int

variable_types:
token
char *

variable_types:
tokens
char *[5]

variable_types:
copy
char *

user_defined_types: enum SIMPLE_CONDITIONAL { LESS_THAN_STRICT=0, LESS_THAN=1, EQUALS=2, GREATER_THAN=3, GREATER_THAN_STRICT=4, INVALID_CONDITIONAL=5 }


repo: zpconn/Simple

decompiled_code:
int __fastcall print_command(__int64 a1, __int64 a2)
{
unsigned int v2; // eax

v2 = simple_lookup_variable(a2, *(_QWORD *)(a1 + 8), a1);
return printf("%s = %d\n", *(const char **)(a1 + 8), v2);
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1, __int64 a2)
{
unsigned int v2; // eax

v2 = simple_lookup_variable(a2, *(_QWORD *)(a1 + 8), a1);
return printf("%s = %d\n", *(const char **)(a1 + 8), v2);
}

original_code:
void print_command(simple_line_t * line, simple_variable_t variable_map[26])
{
 printf("%s = %d\n", line->rest, simple_lookup_variable(variable_map, line->rest, line));
}

canonical_original_code:
void print_command(struct simple_line *line, struct simple_variable variable_map[26])
{
 printf("%s = %d\n", line->rest, simple_lookup_variable(variable_map, line->rest, line));
}

variable_types: struct simple_line { int number; enum SIMPLE_COMMAND { REM=0, INPUT=1, LET=2, PRINT=3, GOTO=4, IF=5, END=6, NO_MATCH=7 } command; char *rest; } *

variable_types: struct simple_variable { int initialized; int value; } [26]

return_type: void

user_defined_types: struct simple_line { int number; enum SIMPLE_COMMAND command; char *rest; }


decompiled_code:
__int64 __fastcall goto_command(__int64 a1, __int64 a2)
{
int v2; // eax
__int64 result; // rax
int v4; // er12

if ( !(unsigned int)is_number(*(_QWORD *)(a2 + 8), a2) )
simple_error(0LL, a2, "the goto destination is not a positive integer");
v2 = atoi(*(const char **)(a2 + 8));
result = find_line_index((__int64 *)a1, v2);
v4 = result;
if ( (int)result < 0 )
result = simple_error(0LL, a2, "no line with the provided number is present in the file");
*(_DWORD *)(a1 + 16) = v4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2)
{
int v2; // eax
__int64 result; // rax
int v4; // er12

if ( !(unsigned int)is_number(*(_QWORD *)(a2 + 8), a2) )
simple_error(0LL, a2, "the goto destination is not a positive integer");
v2 = atoi(*(const char **)(a2 + 8));
result = func0((__int64 *)a1, v2);
v4 = result;
if ( (int)result < 0 )
result = simple_error(0LL, a2, "no line with the provided number is present in the file");
*(_DWORD *)(a1 + 16) = v4;
return result;
}

original_code:
void goto_command(simple_file_t * file, simple_line_t * line, simple_variable_t variable_map[26])
{
 int goto_destination = -1;
 if (!is_number(line->rest))
 {
  simple_error(SYNTAX_ERROR, line, "the goto destination is not a positive integer");
 }
 goto_destination = find_line_index(file, atoi(line->rest));
 if (goto_destination < 0)
 {
  simple_error(SYNTAX_ERROR, line, "no line with the provided number is present in the file");
 }
 file->next_line = goto_destination;
}

canonical_original_code:
void goto_command(struct simple_file *file, struct simple_line *line, struct simple_variable variable_map[26])
{
 int goto_destination = -1;
 if (!is_number(line->rest))
 {
  simple_error(SYNTAX_ERROR, line, "the goto destination is not a positive integer");
 }
 goto_destination = find_line_index(file, atoi(line->rest));
 if (goto_destination < 0)
 {
  simple_error(SYNTAX_ERROR, line, "no line with the provided number is present in the file");
 }
 file->next_line = goto_destination;
}

variable_types: struct simple_file { struct simple_line { int number; enum SIMPLE_COMMAND { REM=0, INPUT=1, LET=2, PRINT=3, GOTO=4, IF=5, END=6, NO_MATCH=7 } command; char *rest; } **lines; long line_count; int next_line; } *

user_defined_types: struct simple_file { struct simple_line **lines; long line_count; int next_line; }

user_defined_types: struct simple_variable { int initialized; int value; }


decompiled_code:
__int64 __fastcall let_command(__int64 a1, __int64 a2)
{
const char *v2; // r14
size_t v3; // r13
char *v4; // rbx
char *v5; // r13
char *v6; // rax
const char *v7; // rbx
size_t v8; // rax
const char *v9; // r14
unsigned int v10; // eax

v2 = *(const char **)(a1 + 8);
v3 = strlen(v2) + 1;
v4 = (char *)malloc(v3);
memcpy(v4, v2, v3);
v5 = strtok(v4, " ");
check_variable_name(v5, a1);
v6 = strtok(0LL, " ");
if ( *v6 != 61 || v6[1] )
simple_error(0LL, a1, "invalid syntax for assignment statement; are you missing an = sign?");
v7 = &v4[strlen(v4) + 1];
v8 = strlen(v7);
v9 = &v7[v8 + 1];
if ( *v9 == 10 && !v7[v8 + 2] || !*v9 )
simple_error(0LL, a1, "it appears you're missing an expression to assign to the provided variable");
v10 = exp_evaluate(a1, v9, a2);
return simple_assign_variable(a2, v5, v10, a1);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2)
{
const char *v2; // r14
size_t v3; // r13
char *v4; // rbx
char *v5; // r13
char *v6; // rax
const char *v7; // rbx
size_t v8; // rax
const char *v9; // r14
unsigned int v10; // eax

v2 = *(const char **)(a1 + 8);
v3 = strlen(v2) + 1;
v4 = (char *)malloc(v3);
memcpy(v4, v2, v3);
v5 = strtok(v4, " ");
check_variable_name(v5, a1);
v6 = strtok(0LL, " ");
if ( *v6 != 61 || v6[1] )
simple_error(0LL, a1, "invalid syntax for assignment statement; are you missing an = sign?");
v7 = &v4[strlen(v4) + 1];
v8 = strlen(v7);
v9 = &v7[v8 + 1];
if ( *v9 == 10 && !v7[v8 + 2] || !*v9 )
simple_error(0LL, a1, "it appears you're missing an expression to assign to the provided variable");
v10 = exp_evaluate(a1, v9, a2);
return simple_assign_variable(a2, v5, v10, a1);
}

original_code:
void let_command(simple_line_t * line, simple_variable_t variable_map[26])
{
 char * tokens[3] = {((void *)0), ((void *)0), ((void *)0)};
 int result = 0;
 char * str = malloc(strlen(line->rest) + 1);
 strcpy(str, line->rest);
 tokens[0] = strtok(str, " ");
 check_variable_name(tokens[0], line);
 tokens[1] = strtok(((void *)0), " ");
 if (strcmp(tokens[1], "=") != 0)
 {
  simple_error(SYNTAX_ERROR, line, "invalid syntax for assignment statement; are you missing an = sign?");
 }
 tokens[2] = strchr(strchr(str, 0) + 1, 0) + 1;
 if (tokens[2] == ((void *)0) || strcmp(tokens[2], "\n") == 0 || strcmp(tokens[2], "\0") == 0)
 {
  simple_error(SYNTAX_ERROR, line, "it appears you're missing an expression to assign to the provided variable");
 }
 result = exp_evaluate(line, tokens[2], variable_map);
 simple_assign_variable(variable_map, tokens[0], result, line);
}

canonical_original_code:
void let_command(struct simple_line *line, struct simple_variable variable_map[26])
{
 char *tokens[3] = {((void *)0), ((void *)0), ((void *)0)};
 int result = 0;
 char *str = malloc(strlen(line->rest) + 1);
 strcpy(str, line->rest);
 tokens[0] = strtok(str, " ");
 check_variable_name(tokens[0], line);
 tokens[1] = strtok(((void *)0), " ");
 if (strcmp(tokens[1], "=") != 0)
 {
  simple_error(SYNTAX_ERROR, line, "invalid syntax for assignment statement; are you missing an = sign?");
 }
 tokens[2] = strchr(strchr(str, 0) + 1, 0) + 1;
 if (tokens[2] == ((void *)0) || strcmp(tokens[2], "\n") == 0 || strcmp(tokens[2], "\0") == 0)
 {
  simple_error(SYNTAX_ERROR, line, "it appears you're missing an expression to assign to the provided variable");
 }
 result = exp_evaluate(line, tokens[2], variable_map);
 simple_assign_variable(variable_map, tokens[0], result, line);
}

variable_types:
tokens
char *[3]

variable_types: result int

variable_types:
str
char *

user_defined_types: enum SIMPLE_COMMAND { REM=0, INPUT=1, LET=2, PRINT=3, GOTO=4, IF=5, END=6, NO_MATCH=7 }


original_code:
enum SIMPLE_OPERATOR token_determine_op(char * token)
{
 if (strcmp(token, "(") == 0)
 {
  return LEFT_PARENTHESIS;
 }
 else if (strcmp(token, ")") == 0)
 {
  return RIGHT_PARENTHESIS;
 }
 else if (strcmp(token, "+") == 0)
 {
  return PLUS;
 }
 else if (strcmp(token, "-") == 0)
 {
  return MINUS;
 }
 else if (strcmp(token, "*") == 0)
 {
  return TIMES;
 }
 else if (strcmp(token, "/") == 0)
 {
  return DIVIDE;
 }
 else if (strcmp(token, "%") == 0)
 {
  return MODULUS;
 }
 else
 {
  return OP_NO_MATCH;
 }
}

canonical_original_code:
enum SIMPLE_OPERATOR token_determine_op(char *token)
{
 if (strcmp(token, "(") == 0)
 {
  return LEFT_PARENTHESIS;
 }
 else if (strcmp(token, ")") == 0)
 {
  return RIGHT_PARENTHESIS;
 }
 else if (strcmp(token, "+") == 0)
 {
  return PLUS;
 }
 else if (strcmp(token, "-") == 0)
 {
  return MINUS;
 }
 else if (strcmp(token, "*") == 0)
 {
  return TIMES;
 }
 else if (strcmp(token, "/") == 0)
 {
  return DIVIDE;
 }
 else if (strcmp(token, "%") == 0)
 {
  return MODULUS;
 }
 else
 {
  return OP_NO_MATCH;
 }
}

return_type: enum SIMPLE_OPERATOR { LEFT_PARENTHESIS=0, RIGHT_PARENTHESIS=1, PLUS=2, MINUS=3, TIMES=4, DIVIDE=5, MODULUS=6, OP_NO_MATCH=7 }


decompiled_code:
__int64 __fastcall evaluate_operator(int a1, int a2, int a3)
{
unsigned int v3; // er8

v3 = a2 + a3;
switch ( a1 )
{
case 2:
return v3;
case 3:
return (unsigned int)(a2 - a3);
case 4:
return (unsigned int)(a3 * a2);
}
if ( a1 != 5 )
{
v3 = 0;
if ( a1 == 6 )
v3 = a2 % a3;
return v3;
}
return (unsigned int)(a2 / a3);
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2, int a3)
{
unsigned int v3; // er8

v3 = a2 + a3;
switch ( a1 )
{
case 2:
return v3;
case 3:
return (unsigned int)(a2 - a3);
case 4:
return (unsigned int)(a3 * a2);
}
if ( a1 != 5 )
{
v3 = 0;
if ( a1 == 6 )
v3 = a2 % a3;
return v3;
}
return (unsigned int)(a2 / a3);
}

original_code:
int evaluate_operator(enum SIMPLE_OPERATOR op, int operand1, int operand2)
{
 if (op == PLUS)
 {
  return operand1 + operand2;
 }
 else if (op == MINUS)
 {
  return operand1 - operand2;
 }
 else if (op == TIMES)
 {
  return operand1 * operand2;
 }
 else if (op == DIVIDE)
 {
  return operand1 / operand2;
 }
 else if (op == MODULUS)
 {
  return operand1 % operand2;
 }
 return 0;
}

variable_types: enum SIMPLE_OPERATOR { LEFT_PARENTHESIS=0, RIGHT_PARENTHESIS=1, PLUS=2, MINUS=3, TIMES=4, DIVIDE=5, MODULUS=6, OP_NO_MATCH=7 }

return_type: int


decompiled_code:
__int64 __fastcall op_precedence(int a1)
{
return (unsigned int)(a1 - 4) < 3 ? 0xA : 0;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1)
{
return (unsigned int)(a1 - 4) < 3 ? 0xA : 0;
}

original_code:
int op_precedence(enum SIMPLE_OPERATOR op_type)
{
 if (op_type == TIMES || op_type == DIVIDE || op_type == MODULUS)
 {
  return 10;
 }
 else
 {
  return 0;
 }
}

user_defined_types: enum SIMPLE_OPERATOR { LEFT_PARENTHESIS=0, RIGHT_PARENTHESIS=1, PLUS=2, MINUS=3, TIMES=4, DIVIDE=5, MODULUS=6, OP_NO_MATCH=7 }


decompiled_code:
__int64 __fastcall exp_evaluate(__int64 a1, const char *a2, __int64 a3)
{
char *v5; // r14
char *v6; // rax
char *v7; // r12
unsigned __int8 *v8; // rdi
unsigned int v9; // eax
unsigned int v10; // ebp
unsigned int v11; // eax
int v12; // eax
int v13; // edx
unsigned int v14; // ecx
char *v15; // rax
unsigned int i; // eax
unsigned int v17; // eax
int v18; // ebx
unsigned int v19; // eax
int v20; // ebp
int v22; // er12
int v23; // eax
unsigned int v24; // eax
unsigned int v26; // eax
__int64 v27; // [rsp+0h] [rbp-48h] BYREF
__int64 v28; // [rsp+8h] [rbp-40h] BYREF
int *v29; // [rsp+10h] [rbp-38h] BYREF
__int64 v30[6]; // [rsp+18h] [rbp-30h] BYREF

v27 = 0LL;
v28 = 0LL;
v29 = 0LL;
v30[0] = 0LL;
v5 = pad_string(a2);
v6 = strtok(v5, " ");
if ( v6 )
{
v7 = v6;
v8 = (unsigned __int8 *)v6;
if ( (unsigned int)is_integer(v6) )
goto LABEL_14;
LABEL_3:
v9 = token_determine_op(v8);
v10 = v9;
if ( v9 != 7 )
{
if ( v9 )
{
if ( v9 == 1 )
{
if ( (unsigned int)stack_empty(v29) )
simple_error(
0LL,
a1,
"incorrect matching of parentheses in arithmetic expression; inappropriate right parenthesis");
while ( *v29 )
{
if ( (unsigned int)stack_empty(v29) )
simple_error(0LL, a1, "incorrect matching of parentheses in arithmetic expression");
v26 = stack_pop(&v29);
enqueue(&v27, &v28, v26, 1LL);
if ( (unsigned int)stack_empty(v29) )
simple_error(0LL, a1, "incorrect matching of parentheses in arithmetic expression");
}
stack_pop(&v29);
}
else
{
while ( !(unsigned int)stack_empty(v29) )
{
op_precedence(v10);
v12 = op_precedence(*v29);
if ( v14 <= 1 || v13 > v12 )
break;
v11 = stack_pop(&v29);
enqueue(&v27, &v28, v11, 1LL);
}
stack_push(&v29, v10);
}
}
else
{
stack_push(&v29, 0LL);
}
goto LABEL_12;
}
if ( !(unsigned int)check_variable_name_no_error() )
{
simple_error(0LL, a1, "invalid or unrecognizable symbol is in the arithmetic expression");
goto LABEL_12;
}
for ( i = simple_lookup_variable(a3, v7, a1); ; i = atoi((const char *)v8) )
{
enqueue(&v27, &v28, i, 0LL);
LABEL_12:
v15 = strtok(0LL, " ");
v7 = v15;
if ( !v15 )
break;
v8 = (unsigned __int8 *)v15;
if ( !(unsigned int)is_integer(v15) )
goto LABEL_3;
LABEL_14:
;
}
}
while ( !(unsigned int)stack_empty(v29) )
{
if ( (unsigned int)*v29 <= 1 )
simple_error(0LL, a1, "incorrect matching of parentheses in arithmetic expression");
v17 = stack_pop(&v29);
enqueue(&v27, &v28, v17, 1LL);
}
LABEL_21:
while ( !(unsigned int)queue_empty(v27) )
{
while ( 1 )
{
v18 = *(_DWORD *)(v27 + 4);
v19 = dequeue(&v27, &v28);
v20 = v19;
if ( v18 == 1 )
break;
if ( !v18 )
{
stack_push(v30, v19);
goto LABEL_21;
}
if ( (unsigned int)queue_empty(v27) )
goto LABEL_25;
}
if ( (unsigned int)stack_empty(v30[0]) )
simple_error(0LL, a1, "incorrect syntax for arithmetic expression");
v22 = stack_pop(v30);
if ( (unsigned int)stack_empty(v30[0]) )
simple_error(0LL, a1, "incorrect syntax for arithmetic expression");
v23 = stack_pop(v30);
v24 = evaluate_operator(v20, v23, v22);
stack_push(v30, v24);
}
LABEL_25:
if ( (unsigned int)stack_empty(v30[0]) || *(_QWORD *)(v30[0] + 8) )
simple_error(0LL, a1, "incorrect syntax for arithmetic expression");
free(v5);
return *(unsigned int *)v30[0];
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, const char *a2, __int64 a3)
{
char *v5; // r14
char *v6; // rax
char *v7; // r12
unsigned __int8 *v8; // rdi
unsigned int v9; // eax
unsigned int v10; // ebp
unsigned int v11; // eax
int v12; // eax
int v13; // edx
unsigned int v14; // ecx
char *v15; // rax
unsigned int i; // eax
unsigned int v17; // eax
int v18; // ebx
unsigned int v19; // eax
int v20; // ebp
int v22; // er12
int v23; // eax
unsigned int v24; // eax
unsigned int v26; // eax
__int64 v27; // [rsp+0h] [rbp-48h] BYREF
__int64 v28; // [rsp+8h] [rbp-40h] BYREF
int *v29; // [rsp+10h] [rbp-38h] BYREF
__int64 v30[6]; // [rsp+18h] [rbp-30h] BYREF

v27 = 0LL;
v28 = 0LL;
v29 = 0LL;
v30[0] = 0LL;
v5 = pad_string(a2);
v6 = strtok(v5, " ");
if ( v6 )
{
v7 = v6;
v8 = (unsigned __int8 *)v6;
if ( (unsigned int)is_integer(v6) )
goto LABEL_14;
LABEL_3:
v9 = func0(v8);
v10 = v9;
if ( v9 != 7 )
{
if ( v9 )
{
if ( v9 == 1 )
{
if ( (unsigned int)stack_empty(v29) )
simple_error(
0LL,
a1,
"incorrect matching of parentheses in arithmetic expression; inappropriate right parenthesis");
while ( *v29 )
{
if ( (unsigned int)stack_empty(v29) )
simple_error(0LL, a1, "incorrect matching of parentheses in arithmetic expression");
v26 = stack_pop(&v29);
enqueue(&v27, &v28, v26, 1LL);
if ( (unsigned int)stack_empty(v29) )
simple_error(0LL, a1, "incorrect matching of parentheses in arithmetic expression");
}
stack_pop(&v29);
}
else
{
while ( !(unsigned int)stack_empty(v29) )
{
func2(v10);
v12 = func2(*v29);
if ( v14 <= 1 || v13 > v12 )
break;
v11 = stack_pop(&v29);
enqueue(&v27, &v28, v11, 1LL);
}
stack_push(&v29, v10);
}
}
else
{
stack_push(&v29, 0LL);
}
goto LABEL_12;
}
if ( !(unsigned int)check_variable_name_no_error() )
{
simple_error(0LL, a1, "invalid or unrecognizable symbol is in the arithmetic expression");
goto LABEL_12;
}
for ( i = simple_lookup_variable(a3, v7, a1); ; i = atoi((const char *)v8) )
{
enqueue(&v27, &v28, i, 0LL);
LABEL_12:
v15 = strtok(0LL, " ");
v7 = v15;
if ( !v15 )
break;
v8 = (unsigned __int8 *)v15;
if ( !(unsigned int)is_integer(v15) )
goto LABEL_3;
LABEL_14:
;
}
}
while ( !(unsigned int)stack_empty(v29) )
{
if ( (unsigned int)*v29 <= 1 )
simple_error(0LL, a1, "incorrect matching of parentheses in arithmetic expression");
v17 = stack_pop(&v29);
enqueue(&v27, &v28, v17, 1LL);
}
LABEL_21:
while ( !(unsigned int)queue_empty(v27) )
{
while ( 1 )
{
v18 = *(_DWORD *)(v27 + 4);
v19 = dequeue(&v27, &v28);
v20 = v19;
if ( v18 == 1 )
break;
if ( !v18 )
{
stack_push(v30, v19);
goto LABEL_21;
}
if ( (unsigned int)queue_empty(v27) )
goto LABEL_25;
}
if ( (unsigned int)stack_empty(v30[0]) )
simple_error(0LL, a1, "incorrect syntax for arithmetic expression");
v22 = stack_pop(v30);
if ( (unsigned int)stack_empty(v30[0]) )
simple_error(0LL, a1, "incorrect syntax for arithmetic expression");
v23 = stack_pop(v30);
v24 = func1(v20, v23, v22);
stack_push(v30, v24);
}
LABEL_25:
if ( (unsigned int)stack_empty(v30[0]) || *(_QWORD *)(v30[0] + 8) )
simple_error(0LL, a1, "incorrect syntax for arithmetic expression");
free(v5);
return *(unsigned int *)v30[0];
}

original_code:
int exp_evaluate(simple_line_t * line, char * expression, simple_variable_t variable_map[26])
{
 queue_t * output_head = ((void *)0), * output_tail = ((void *)0);
 stack_struct_t * operators = ((void *)0);
 stack_struct_t * eval_stack = ((void *)0);
 char * token;
 char * copy = pad_string(expression);
 token = strtok(copy, " ");
 for (; token != ((void *)0); token = strtok(((void *)0), " "))
 {
  if (is_integer(token))
  {
   enqueue(&output_head, &output_tail, atoi(token), NUMBER);
  }
  else if (token_determine_op(token) != OP_NO_MATCH)
  {
   enum SIMPLE_OPERATOR op_type = token_determine_op(token);
   if (op_type == LEFT_PARENTHESIS)
   {
    stack_push(&operators, LEFT_PARENTHESIS);
   }
   else if (op_type == RIGHT_PARENTHESIS)
   {
    if (stack_empty(operators))
    {
     simple_error(SYNTAX_ERROR, line, "incorrect matching of parentheses in arithmetic expression; inappropriate right parenthesis");
    }
    while (operators->data != LEFT_PARENTHESIS)
    {
     if (stack_empty(operators))
     {
      simple_error(SYNTAX_ERROR, line, "incorrect matching of parentheses in arithmetic expression");
     }
     enqueue(&output_head, &output_tail, stack_pop(&operators), OPERATOR);
     if (stack_empty(operators))
     {
      simple_error(SYNTAX_ERROR, line, "incorrect matching of parentheses in arithmetic expression");
     }
    }
    stack_pop(&operators);
   }
   else
   {
    while (!stack_empty(operators) && op_precedence(op_type) <= op_precedence((enum SIMPLE_OPERATOR)operators->data)
           && operators->data != LEFT_PARENTHESIS && operators->data != RIGHT_PARENTHESIS)
    {
     enqueue(&output_head, &output_tail, stack_pop(&operators), OPERATOR);
    }
    stack_push(&operators, op_type);
   }
  }
  else if (check_variable_name_no_error(token))
  {
   enqueue(&output_head, &output_tail, simple_lookup_variable(variable_map, token, line), NUMBER);
  }
  else
  {
   simple_error(SYNTAX_ERROR, line, "invalid or unrecognizable symbol is in the arithmetic expression");
  }
 }
 while (!stack_empty(operators))
 {
  if (operators->data == LEFT_PARENTHESIS || operators->data == RIGHT_PARENTHESIS)
  {
   simple_error(SYNTAX_ERROR, line, "incorrect matching of parentheses in arithmetic expression");
  }
  enqueue(&output_head, &output_tail, stack_pop(&operators), OPERATOR);
 }
 while (!queue_empty(output_head))
 {
  int operand1 = 0, operand2 = 0;
  enum QUEUE_DATA_TYPE type = output_head->type;
  int value = dequeue(&output_head, &output_tail);
  if (type == OPERATOR)
  {
   if (stack_empty(eval_stack))
   {
    simple_error(SYNTAX_ERROR, line, "incorrect syntax for arithmetic expression");
   }
   operand2 = stack_pop(&eval_stack);
   if (stack_empty(eval_stack))
   {
    simple_error(SYNTAX_ERROR, line, "incorrect syntax for arithmetic expression");
   }
   operand1 = stack_pop(&eval_stack);
   stack_push(&eval_stack, evaluate_operator((enum SIMPLE_OPERATOR)value, operand1, operand2));
  }
  else if (type == NUMBER)
  {
   stack_push(&eval_stack, value);
  }
 }
 if (stack_empty(eval_stack) || eval_stack->next != ((void *)0))
 {
  simple_error(SYNTAX_ERROR, line, "incorrect syntax for arithmetic expression");
 }
 free(copy);
 return eval_stack->data;
}

canonical_original_code:
int exp_evaluate(struct simple_line *line, char *expression, struct simple_variable variable_map[26])
{
 struct queue *output_head = ((void *)0); struct queue *output_tail = ((void *)0);
 struct stack *operators = ((void *)0);
 struct stack *eval_stack = ((void *)0);
 char *token;
 char *copy = pad_string(expression);
 token = strtok(copy, " ");
 for (; token != ((void *)0); token = strtok(((void *)0), " "))
 {
  if (is_integer(token))
  {
   enqueue(&output_head, &output_tail, atoi(token), NUMBER);
  }
  else if (token_determine_op(token) != OP_NO_MATCH)
  {
   enum SIMPLE_OPERATOR op_type = token_determine_op(token);
   if (op_type == LEFT_PARENTHESIS)
   {
    stack_push(&operators, LEFT_PARENTHESIS);
   }
   else if (op_type == RIGHT_PARENTHESIS)
   {
    if (stack_empty(operators))
    {
     simple_error(SYNTAX_ERROR, line, "incorrect matching of parentheses in arithmetic expression; inappropriate right parenthesis");
    }
    while (operators->data != LEFT_PARENTHESIS)
    {
     if (stack_empty(operators))
     {
      simple_error(SYNTAX_ERROR, line, "incorrect matching of parentheses in arithmetic expression");
     }
     enqueue(&output_head, &output_tail, stack_pop(&operators), OPERATOR);
     if (stack_empty(operators))
     {
      simple_error(SYNTAX_ERROR, line, "incorrect matching of parentheses in arithmetic expression");
     }
    }
    stack_pop(&operators);
   }
   else
   {
    while (!stack_empty(operators) && op_precedence(op_type) <= op_precedence((enum SIMPLE_OPERATOR)operators->data)
           && operators->data != LEFT_PARENTHESIS && operators->data != RIGHT_PARENTHESIS)
    {
     enqueue(&output_head, &output_tail, stack_pop(&operators), OPERATOR);
    }
    stack_push(&operators, op_type);
   }
  }
  else if (check_variable_name_no_error(token))
  {
   enqueue(&output_head, &output_tail, simple_lookup_variable(variable_map, token, line), NUMBER);
  }
  else
  {
   simple_error(SYNTAX_ERROR, line, "invalid or unrecognizable symbol is in the arithmetic expression");
  }
 }
 while (!stack_empty(operators))
 {
  if (operators->data == LEFT_PARENTHESIS || operators->data == RIGHT_PARENTHESIS)
  {
   simple_error(SYNTAX_ERROR, line, "incorrect matching of parentheses in arithmetic expression");
  }
  enqueue(&output_head, &output_tail, stack_pop(&operators), OPERATOR);
 }
 while (!queue_empty(output_head))
 {
  int operand1 = 0; int operand2 = 0;
  enum QUEUE_DATA_TYPE type = output_head->type;
  int value = dequeue(&output_head, &output_tail);
  if (type == OPERATOR)
  {
   if (stack_empty(eval_stack))
   {
    simple_error(SYNTAX_ERROR, line, "incorrect syntax for arithmetic expression");
   }
   operand2 = stack_pop(&eval_stack);
   if (stack_empty(eval_stack))
   {
    simple_error(SYNTAX_ERROR, line, "incorrect syntax for arithmetic expression");
   }
   operand1 = stack_pop(&eval_stack);
   stack_push(&eval_stack, evaluate_operator((enum SIMPLE_OPERATOR)value, operand1, operand2));
  }
  else if (type == NUMBER)
  {
   stack_push(&eval_stack, value);
  }
 }
 if (stack_empty(eval_stack) || eval_stack->next != ((void *)0))
 {
  simple_error(SYNTAX_ERROR, line, "incorrect syntax for arithmetic expression");
 }
 free(copy);
 return eval_stack->data;
}

variable_types:
expression
char *

variable_types: struct queue { int data; enum QUEUE_DATA_TYPE { NUMBER=0, OPERATOR=1 } type; struct queue *next; } *

variable_types: struct stack { int data; struct stack *next; } *

variable_types: value int

user_defined_types: struct queue { int data; enum QUEUE_DATA_TYPE type; struct queue *next; }


decompiled_code:
__int64 __fastcall simple_execute_line(__int64 a1, __int64 a2, int a3)
{
__int64 v4; // rbx
__int64 v5; // rdi
__int64 result; // rax

v4 = a3;
if ( a3 < 0 || a3 >= *(_QWORD *)(a1 + 8) )
simple_error(5LL, 0LL, "the program flow is trying to go beyond available lines; is an 'end' command missing?");
v5 = *(_QWORD *)(*(_QWORD *)a1 + 8 * v4);
result = *(unsigned int *)(v5 + 4);
switch ( *(_DWORD *)(v5 + 4) )
{
case 0:
goto LABEL_6;
case 1:
result = input_command(v5, a2);
++*(_DWORD *)(a1 + 16);
break;
case 2:
result = let_command(v5, a2);
++*(_DWORD *)(a1 + 16);
break;
case 3:
result = print_command(v5, a2);
LABEL_6:
++*(_DWORD *)(a1 + 16);
break;
case 4:
result = goto_command(a1, *(_QWORD *)(*(_QWORD *)a1 + 8 * v4), a2);
break;
case 5:
result = conditional_command(a1, *(_QWORD *)(*(_QWORD *)a1 + 8 * v4), a2);
break;
case 6:
*(_DWORD *)(a1 + 16) = -1;
break;
default:
result = simple_error(5LL, v5, "tried to execute a line whose command is NO_MATCH");
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2, int a3)
{
__int64 v4; // rbx
__int64 v5; // rdi
__int64 result; // rax

v4 = a3;
if ( a3 < 0 || a3 >= *(_QWORD *)(a1 + 8) )
simple_error(5LL, 0LL, "the program flow is trying to go beyond available lines; is an 'end' command missing?");
v5 = *(_QWORD *)(*(_QWORD *)a1 + 8 * v4);
result = *(unsigned int *)(v5 + 4);
switch ( *(_DWORD *)(v5 + 4) )
{
case 0:
goto LABEL_6;
case 1:
result = input_command(v5, a2);
++*(_DWORD *)(a1 + 16);
break;
case 2:
result = let_command(v5, a2);
++*(_DWORD *)(a1 + 16);
break;
case 3:
result = print_command(v5, a2);
LABEL_6:
++*(_DWORD *)(a1 + 16);
break;
case 4:
result = goto_command(a1, *(_QWORD *)(*(_QWORD *)a1 + 8 * v4), a2);
break;
case 5:
result = conditional_command(a1, *(_QWORD *)(*(_QWORD *)a1 + 8 * v4), a2);
break;
case 6:
*(_DWORD *)(a1 + 16) = -1;
break;
default:
result = simple_error(5LL, v5, "tried to execute a line whose command is NO_MATCH");
break;
}
return result;
}

original_code:
void simple_execute_line(simple_file_t * file, simple_variable_t variable_map[26], int line_index)
{
 simple_line_t * line = ((void *)0);
 if (line_index < 0 || line_index >= file->line_count)
 {
  simple_error(MYSTERY, ((void *)0), "the program flow is trying to go beyond available lines; is an 'end' command missing?");
 }
 line = file->lines[line_index];
 switch (line->command)
 {
  case REM:
   ++file->next_line;
   break;
  case INPUT:
   input_command(line, variable_map);
   ++file->next_line;
   break;
  case LET:
   let_command(line, variable_map);
   ++file->next_line;
   break;
  case PRINT:
   print_command(line, variable_map);
   ++file->next_line;
   break;
  case GOTO:
   goto_command(file, line, variable_map);
   break;
  case IF:
   conditional_command(file, line, variable_map);
   break;
  case END:
   file->next_line = -1;
   break;
  default:
  case NO_MATCH:
   simple_error(MYSTERY, line, "tried to execute a line whose command is NO_MATCH");
   break;
 }
}

canonical_original_code:
void simple_execute_line(struct simple_file *file, struct simple_variable variable_map[26], int line_index)
{
 struct simple_line *line = ((void *)0);
 if (line_index < 0 || line_index >= file->line_count)
 {
  simple_error(MYSTERY, ((void *)0), "the program flow is trying to go beyond available lines; is an 'end' command missing?");
 }
 line = file->lines[line_index];
 switch (line->command)
 {
  case REM:
   ++file->next_line;
   break;
  case INPUT:
   input_command(line, variable_map);
   ++file->next_line;
   break;
  case LET:
   let_command(line, variable_map);
   ++file->next_line;
   break;
  case PRINT:
   print_command(line, variable_map);
   ++file->next_line;
   break;
  case GOTO:
   goto_command(file, line, variable_map);
   break;
  case IF:
   conditional_command(file, line, variable_map);
   break;
  case END:
   file->next_line = -1;
   break;
  default:
  case NO_MATCH:
   simple_error(MYSTERY, line, "tried to execute a line whose command is NO_MATCH");
   break;
 }
}


decompiled_code:
__int64 __fastcall simple_execute(__int64 a1, __int64 a2)
{
int i; // edx
__int64 result; // rax

for ( i = *(_DWORD *)(a1 + 16); i >= 0; i = *(_DWORD *)(a1 + 16) )
result = simple_execute_line(a1, a2, i);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
int i; // edx
__int64 result; // rax

for ( i = *(_DWORD *)(a1 + 16); i >= 0; i = *(_DWORD *)(a1 + 16) )
result = func0(a1, a2, i);
return result;
}

original_code:
void simple_execute(simple_file_t * file, simple_variable_t variable_map[26])
{
 while (file->next_line >= 0)
 {
  simple_execute_line(file, variable_map, file->next_line);
 }
}

canonical_original_code:
void simple_execute(struct simple_file *file, struct simple_variable variable_map[26])
{
 while (file->next_line >= 0)
 {
  simple_execute_line(file, variable_map, file->next_line);
 }
}


decompiled_code:
__int64 __fastcall determine_command(__int64 a1)
{
unsigned int v1; // er8
__int64 result; // rax

v1 = 0;
if ( !strcmp((const char *)a1, "rem")
|| (v1 = 1, !strcmp((const char *)a1, "input"))
|| (v1 = 2, !strcmp((const char *)a1, "let"))
|| (v1 = 3, !strcmp((const char *)a1, "print"))
|| (v1 = 4, !strcmp((const char *)a1, "goto"))
|| *(_BYTE *)a1 == 105 && *(_BYTE *)(a1 + 1) == 102 && (v1 = 5, !*(_BYTE *)(a1 + 2)) )
{
result = v1;
}
else
{
result = 6 - ((unsigned int)(strcmp((const char *)a1, "end") == 0) - 1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
unsigned int v1; // er8
__int64 result; // rax

v1 = 0;
if ( !strcmp((const char *)a1, "rem")
|| (v1 = 1, !strcmp((const char *)a1, "input"))
|| (v1 = 2, !strcmp((const char *)a1, "let"))
|| (v1 = 3, !strcmp((const char *)a1, "print"))
|| (v1 = 4, !strcmp((const char *)a1, "goto"))
|| *(_BYTE *)a1 == 105 && *(_BYTE *)(a1 + 1) == 102 && (v1 = 5, !*(_BYTE *)(a1 + 2)) )
{
result = v1;
}
else
{
result = 6 - ((unsigned int)(strcmp((const char *)a1, "end") == 0) - 1);
}
return result;
}

original_code:
enum SIMPLE_COMMAND determine_command(char * command_str)
{
 if (strcmp(command_str, "rem") == 0)
  return REM;
 else if (strcmp(command_str, "input") == 0)
  return INPUT;
 else if (strcmp(command_str, "let") == 0)
  return LET;
 else if (strcmp(command_str, "print") == 0)
  return PRINT;
 else if (strcmp(command_str, "goto") == 0)
  return GOTO;
 else if (strcmp(command_str, "if") == 0)
  return IF;
 else if (strcmp(command_str, "end") == 0)
  return END;
 else
  return NO_MATCH;
}

canonical_original_code:
enum SIMPLE_COMMAND determine_command(char *command_str)
{
 if (strcmp(command_str, "rem") == 0)
  return REM;
 else if (strcmp(command_str, "input") == 0)
  return INPUT;
 else if (strcmp(command_str, "let") == 0)
  return LET;
 else if (strcmp(command_str, "print") == 0)
  return PRINT;
 else if (strcmp(command_str, "goto") == 0)
  return GOTO;
 else if (strcmp(command_str, "if") == 0)
  return IF;
 else if (strcmp(command_str, "end") == 0)
  return END;
 else
  return NO_MATCH;
}

return_type: enum SIMPLE_COMMAND { REM=0, INPUT=1, LET=2, PRINT=3, GOTO=4, IF=5, END=6, NO_MATCH=7 }


decompiled_code:
__int64 __fastcall simple_parse_line(__int64 a1)
{
const char *v1; // rbp
size_t v2; // r13
char *v3; // r12
char *v4; // rbp
char *v5; // r13
__int64 result; // rax
int v7; // ebp
const char *v8; // r13
const char *v9; // r12
char delim[2]; // [rsp+Eh] [rbp-2Ah] BYREF

v1 = *(const char **)(a1 + 8);
*(_WORD *)delim = 2336;
v2 = strlen(v1) + 1;
v3 = (char *)malloc(v2);
memcpy(v3, v1, v2);
v4 = strtok(v3, delim);
if ( !v4 )
simple_error(0LL, 0LL, "empty/blank line; is this a blank line at the end? ");
if ( !(unsigned int)is_number(v4) )
simple_error(0LL, 0LL, "line does not start with positive integer");
*(_DWORD *)a1 = atoi(v4);
v5 = strtok(0LL, delim);
if ( !v5 )
simple_error(0LL, 0LL, "line does not contain enough tokens to be well-defined");
result = determine_command((__int64)v5);
*(_DWORD *)(a1 + 4) = result;
v7 = result;
if ( (_DWORD)result == 7 )
{
result = simple_error(2LL, 0LL, v5);
v7 = *(_DWORD *)(a1 + 4);
}
v8 = 0LL;
if ( v7 == 6 )
goto LABEL_13;
v9 = &v3[strlen(v3) + 1];
result = strlen(v9);
v8 = &v9[result + 1];
if ( *v8 != 10 || v9[result + 2] )
{
if ( *v8 || !v7 )
goto LABEL_13;
}
else if ( !v7 )
{
LABEL_13:
*(_QWORD *)(a1 + 8) = v8;
return result;
}
result = simple_error(0LL, 0LL, "a command other than 'rem' and 'end' was encountered with no arguments");
*(_QWORD *)(a1 + 8) = v8;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
const char *v1; // rbp
size_t v2; // r13
char *v3; // r12
char *v4; // rbp
char *v5; // r13
__int64 result; // rax
int v7; // ebp
const char *v8; // r13
const char *v9; // r12
char delim[2]; // [rsp+Eh] [rbp-2Ah] BYREF

v1 = *(const char **)(a1 + 8);
*(_WORD *)delim = 2336;
v2 = strlen(v1) + 1;
v3 = (char *)malloc(v2);
memcpy(v3, v1, v2);
v4 = strtok(v3, delim);
if ( !v4 )
simple_error(0LL, 0LL, "empty/blank line; is this a blank line at the end? ");
if ( !(unsigned int)is_number(v4) )
simple_error(0LL, 0LL, "line does not start with positive integer");
*(_DWORD *)a1 = atoi(v4);
v5 = strtok(0LL, delim);
if ( !v5 )
simple_error(0LL, 0LL, "line does not contain enough tokens to be well-defined");
result = func0((__int64)v5);
*(_DWORD *)(a1 + 4) = result;
v7 = result;
if ( (_DWORD)result == 7 )
{
result = simple_error(2LL, 0LL, v5);
v7 = *(_DWORD *)(a1 + 4);
}
v8 = 0LL;
if ( v7 == 6 )
goto LABEL_13;
v9 = &v3[strlen(v3) + 1];
result = strlen(v9);
v8 = &v9[result + 1];
if ( *v8 != 10 || v9[result + 2] )
{
if ( *v8 || !v7 )
goto LABEL_13;
}
else if ( !v7 )
{
LABEL_13:
*(_QWORD *)(a1 + 8) = v8;
return result;
}
result = simple_error(0LL, 0LL, "a command other than 'rem' and 'end' was encountered with no arguments");
*(_QWORD *)(a1 + 8) = v8;
return result;
}

original_code:
void simple_parse_line(simple_line_t * line)
{
 char delimiters[2] = " \t";
 char * tokens[3] = {((void *)0), ((void *)0), ((void *)0)};
 char * str = malloc(strlen(line->rest) + 1);
 strcpy(str, line->rest);
 tokens[0] = strtok(str, delimiters);
 if (tokens[0] == ((void *)0))
 {
  simple_error(SYNTAX_ERROR, ((void *)0), "empty/blank line; is this a blank line at the end? ");
 }
 if (!is_number(tokens[0]))
 {
  simple_error(SYNTAX_ERROR, ((void *)0), "line does not start with positive integer");
 }
 line->number = atoi(tokens[0]);
 tokens[1] = strtok(((void *)0), delimiters);
 if (tokens[1] == ((void *)0))
 {
  simple_error(SYNTAX_ERROR, ((void *)0), "line does not contain enough tokens to be well-defined");
 }
 line->command = determine_command(tokens[1]);
 if (line->command == NO_MATCH)
 {
  simple_error(UNRECOGNIZED_COMMAND, ((void *)0), tokens[1]);
 }
 if (line->command == END)
 {
  tokens[2] = ((void *)0);
 }
 else
 {
  tokens[2] = strchr(strchr(str, 0) + 1, 0) + 1;
  if ((tokens[2] == ((void *)0) || strcmp(tokens[2], "\n") == 0 || strcmp(tokens[2], "\0") == 0) && line->command != REM)
  {
   simple_error(SYNTAX_ERROR, ((void *)0), "a command other than 'rem' and 'end' was encountered with no arguments");
  }
 }
 line->rest = tokens[2];
}

canonical_original_code:
void simple_parse_line(struct simple_line *line)
{
 char delimiters[2] = " \t";
 char *tokens[3] = {((void *)0), ((void *)0), ((void *)0)};
 char *str = malloc(strlen(line->rest) + 1);
 strcpy(str, line->rest);
 tokens[0] = strtok(str, delimiters);
 if (tokens[0] == ((void *)0))
 {
  simple_error(SYNTAX_ERROR, ((void *)0), "empty/blank line; is this a blank line at the end? ");
 }
 if (!is_number(tokens[0]))
 {
  simple_error(SYNTAX_ERROR, ((void *)0), "line does not start with positive integer");
 }
 line->number = atoi(tokens[0]);
 tokens[1] = strtok(((void *)0), delimiters);
 if (tokens[1] == ((void *)0))
 {
  simple_error(SYNTAX_ERROR, ((void *)0), "line does not contain enough tokens to be well-defined");
 }
 line->command = determine_command(tokens[1]);
 if (line->command == NO_MATCH)
 {
  simple_error(UNRECOGNIZED_COMMAND, ((void *)0), tokens[1]);
 }
 if (line->command == END)
 {
  tokens[2] = ((void *)0);
 }
 else
 {
  tokens[2] = strchr(strchr(str, 0) + 1, 0) + 1;
  if ((tokens[2] == ((void *)0) || strcmp(tokens[2], "\n") == 0 || strcmp(tokens[2], "\0") == 0) && line->command != REM)
  {
   simple_error(SYNTAX_ERROR, ((void *)0), "a command other than 'rem' and 'end' was encountered with no arguments");
  }
 }
 line->rest = tokens[2];
}

variable_types:
delimiters
char [2]


repo: zrafa/paralab

decompiled_code:
__suseconds_t tor_getclock()
{
if ( gettimeofday(&tv_0, 0LL) < 0 )
{
perror("getclock");
exit(1);
}
return tv_0.tv_usec + 1000000 * tv_0.tv_sec;
}

canonical_decompiled_code:
__suseconds_t func0()
{
if ( gettimeofday(&tv_0, 0LL) < 0 )
{
perror("getclock");
exit(1);
}
return tv_0.tv_usec + 1000000 * tv_0.tv_sec;
}

original_code:
suseconds_t tor_getclock()
{
 static struct timeval tv;
 if(gettimeofday(&tv, (struct timezone *)((void *)0)) < 0) {
  perror("getclock");
  exit(1);
 }
 return (suseconds_t)tv.tv_sec * 1000000 + tv.tv_usec;
}

canonical_original_code:
long tor_getclock()
{
 static struct timeval tv;
 if(gettimeofday(&tv, (struct timezone *)((void *)0)) < 0) {
  perror("getclock");
  exit(1);
 }
 return (long)tv.tv_sec * 1000000 + tv.tv_usec;
}

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

return_type: long

user_defined_types: struct timezone { int tz_minuteswest; int tz_dsttime; }


decompiled_code:
__suseconds_t tor_setclock()
{
__suseconds_t result; // rax

result = tor_getclock();
tor_clock = result;
return result;
}

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

result = func0();
tor_clock = result;
return result;
}

original_code:
void tor_setclock()
{
 tor_clock = tor_getclock();
}


decompiled_code:
__suseconds_t tor_getelapsed()
{
__suseconds_t v0; // r12

v0 = tor_getclock() - tor_clock;
tor_setclock();
return v0;
}

canonical_decompiled_code:
__suseconds_t func2()
{
__suseconds_t v0; // r12

v0 = func0() - tor_clock;
func1();
return v0;
}

original_code:
suseconds_t tor_getelapsed()
{
 suseconds_t elapsed;
 elapsed = tor_getclock() - tor_clock;
 tor_setclock();
 return elapsed;
}

canonical_original_code:
long tor_getelapsed()
{
 long elapsed;
 elapsed = tor_getclock() - tor_clock;
 tor_setclock();
 return elapsed;
}

variable_types: elapsed long


strtoul

repo: zrho/Carbon

decompiled_code:
unsigned __int64 __fastcall strtoul(const char *nptr, char **endptr, int base)
{
return strtoull(nptr, endptr, base);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(const char *nptr, char **endptr, int base)
{
return strtoull(nptr, endptr, base);
}

original_code:
unsigned long int strtoul(const char *ptr, char **endptr, int base) {
 return strtoull(ptr, endptr, base);
}

canonical_original_code:
unsigned long strtoul(char *ptr, char **endptr, int base) {
 return strtoull(ptr, endptr, base);
}

variable_types:
ptr
char *

variable_types:
endptr
char **

variable_types: base int

return_type: unsigned long


decompiled_code:
__int64 __fastcall thread_get(unsigned int *a1, unsigned int a2)
{
__int64 result; // rax

if ( a2 > 0x3FF )
result = 0LL;
else
result = *(_QWORD *)(8 * (((unsigned __int64)*a1 << 10) + a2) - 0x8000207000LL);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int *a1, unsigned int a2)
{
__int64 result; // rax

if ( a2 > 0x3FF )
result = 0LL;
else
result = *(_QWORD *)(8 * (((unsigned __int64)*a1 << 10) + a2) - 0x8000207000LL);
return result;
}

original_code:
thread_t *thread_get(process_t *process, uint32_t tid) {
    if (tid >= 1024)
        return 0;
    return ((thread_t **) ((0xFFFFFF8000000000 - 0x207000) + (process->pid * (1024 * sizeof(uintptr_t)))))[tid];
}

canonical_original_code:
struct thread_t *thread_get(struct process_t *process, unsigned int tid) {
    if (tid >= 1024)
        return 0;
    return ((struct thread_t **) ((0xFFFFFF8000000000 - 0x207000) + (process->pid * (1024 * sizeof(unsigned long long)))))[tid];
}

variable_types: struct process_t { unsigned int pid; struct process_t *parent; unsigned short order; unsigned long long message_handler; unsigned char term_implicit; struct thread_t *threads; unsigned long long addr_space; unsigned long long stack_offset; struct process_t *next; } *

variable_types:
tid
unsigned int

return_type: struct thread_t { unsigned int tid; unsigned int pid; unsigned char frozen; unsigned char ttl; unsigned char role; void *role_ctx; unsigned char sleep_mode; void *sleep_ctx; void *result_ptr; unsigned int ipc_buffer_sz[3]; unsigned char flags; unsigned long long entry_point; void *fx_data; struct cpu_int_state_t { unsigned long long ds; struct cpu_state_t { unsigned long long r15; unsigned long long r14; unsigned long long r13; unsigned long long r12; unsigned long long r11; unsigned long long r10; unsigned long long r9; unsigned long long r8; unsigned long long rsi; unsigned long long rdi; unsigned long long rbp; unsigned long long rdx; unsigned long long rcx; unsigned long long rbx; unsigned long long rax; } state; unsigned long long vector; unsigned long long error_code; unsigned long long rip; unsigned long long cs; unsigned long long flags; unsigned long long rsp; unsigned long long ss; } state; struct stack_t { unsigned long long address; unsigned long long length; } stack; struct thread_t *next_sched; struct thread_t *next; } *

user_defined_types: struct thread_t { unsigned int tid; unsigned int pid; unsigned char frozen; unsigned char ttl; unsigned char role; void *role_ctx; unsigned char sleep_mode; void *sleep_ctx; void *result_ptr; unsigned int ipc_buffer_sz[3]; unsigned char flags; unsigned long long entry_point; void *fx_data; struct cpu_int_state_t state; struct stack_t stack; struct thread_t *next_sched; struct thread_t *next; }

user_defined_types: struct cpu_int_state_t { unsigned long long ds; struct cpu_state_t state; unsigned long long vector; unsigned long long error_code; unsigned long long rip; unsigned long long cs; unsigned long long flags; unsigned long long rsp; unsigned long long ss; }


original_code:
void thread_free(process_t *process, uint32_t tid) {
    thread_t *thread = process->threads;
    thread_t *thread_prev = 0;
    while (0 != thread) {
        if (tid == thread->tid)
            break;
        thread_prev = thread;
        thread = thread->next;
    }
    if (0 == thread || 0 == (thread->flags & (1 << 0)))
        return;
    ((thread_t **) ((0xFFFFFF8000000000 - 0x207000) + (process->pid * (1024 * sizeof(uintptr_t)))))[tid] = 0;
    if (0 == thread_prev)
        process->threads = thread->next;
    else
        thread_prev->next = thread->next;
    heap_free(thread);
}

canonical_original_code:
void thread_free(struct process_t *process, unsigned int tid) {
    struct thread_t *thread = process->threads;
    struct thread_t *thread_prev = 0;
    while (0 != thread) {
        if (tid == thread->tid)
            break;
        thread_prev = thread;
        thread = thread->next;
    }
    if (0 == thread || 0 == (thread->flags & (1 << 0)))
        return;
    ((struct thread_t **) ((0xFFFFFF8000000000 - 0x207000) + (process->pid * (1024 * sizeof(unsigned long long)))))[tid] = 0;
    if (0 == thread_prev)
        process->threads = thread->next;
    else
        thread_prev->next = thread->next;
    heap_free(thread);
}

variable_types: struct process_t { unsigned int pid; struct process_t *parent; unsigned short order; unsigned long long message_handler; unsigned char term_implicit; struct thread_t { unsigned int tid; unsigned int pid; unsigned char frozen; unsigned char ttl; unsigned char role; void *role_ctx; unsigned char sleep_mode; void *sleep_ctx; void *result_ptr; unsigned int ipc_buffer_sz[3]; unsigned char flags; unsigned long long entry_point; void *fx_data; struct cpu_int_state_t { unsigned long long ds; struct cpu_state_t { unsigned long long r15; unsigned long long r14; unsigned long long r13; unsigned long long r12; unsigned long long r11; unsigned long long r10; unsigned long long r9; unsigned long long r8; unsigned long long rsi; unsigned long long rdi; unsigned long long rbp; unsigned long long rdx; unsigned long long rcx; unsigned long long rbx; unsigned long long rax; } state; unsigned long long vector; unsigned long long error_code; unsigned long long rip; unsigned long long cs; unsigned long long flags; unsigned long long rsp; unsigned long long ss; } state; struct stack_t { unsigned long long address; unsigned long long length; } stack; struct thread_t *next_sched; struct thread_t *next; } *threads; unsigned long long addr_space; unsigned long long stack_offset; struct process_t *next; } *

user_defined_types: struct process_t { unsigned int pid; struct process_t *parent; unsigned short order; unsigned long long message_handler; unsigned char term_implicit; struct thread_t *threads; unsigned long long addr_space; unsigned long long stack_offset; struct process_t *next; }


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

result = *(unsigned __int8 *)(a1 + 8);
*(_BYTE *)(a1 + 8) = result + 1;
if ( !(_BYTE)result )
result = scheduler_remove();
return result;
}

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

result = *(unsigned __int8 *)(a1 + 8);
*(_BYTE *)(a1 + 8) = result + 1;
if ( !(_BYTE)result )
result = scheduler_remove();
return result;
}

original_code:
void thread_freeze(thread_t *thread) {
    if (0 == thread->frozen++)
        scheduler_remove(thread);
}

canonical_original_code:
void thread_freeze(struct thread_t *thread) {
    if (0 == thread->frozen++)
        scheduler_remove(thread);
}

variable_types: struct thread_t { unsigned int tid; unsigned int pid; unsigned char frozen; unsigned char ttl; unsigned char role; void *role_ctx; unsigned char sleep_mode; void *sleep_ctx; void *result_ptr; unsigned int ipc_buffer_sz[3]; unsigned char flags; unsigned long long entry_point; void *fx_data; struct cpu_int_state_t { unsigned long long ds; struct cpu_state_t { unsigned long long r15; unsigned long long r14; unsigned long long r13; unsigned long long r12; unsigned long long r11; unsigned long long r10; unsigned long long r9; unsigned long long r8; unsigned long long rsi; unsigned long long rdi; unsigned long long rbp; unsigned long long rdx; unsigned long long rcx; unsigned long long rbx; unsigned long long rax; } state; unsigned long long vector; unsigned long long error_code; unsigned long long rip; unsigned long long cs; unsigned long long flags; unsigned long long rsp; unsigned long long ss; } state; struct stack_t { unsigned long long address; unsigned long long length; } stack; struct thread_t *next_sched; struct thread_t *next; } *


decompiled_code:
__int64 __fastcall thread_thaw(__int64 a1, unsigned __int8 a2)
{
__int64 result; // rax

result = *(unsigned __int8 *)(a1 + 8);
if ( (_BYTE)result )
{
result = (unsigned int)(result - 1);
*(_BYTE *)(a1 + 8) = result;
if ( !(_BYTE)result )
result = scheduler_add(a1, a2 | 1u);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, unsigned __int8 a2)
{
__int64 result; // rax

result = *(unsigned __int8 *)(a1 + 8);
if ( (_BYTE)result )
{
result = (unsigned int)(result - 1);
*(_BYTE *)(a1 + 8) = result;
if ( !(_BYTE)result )
result = scheduler_add(a1, a2 | 1u);
}
return result;
}

original_code:
void thread_thaw(thread_t *thread, uint8_t flags) {
    if (0 == thread->frozen)
        return;
    if (0 == --thread->frozen)
        scheduler_add(thread, (1 << 0) | flags);
}

canonical_original_code:
void thread_thaw(struct thread_t *thread, unsigned char flags) {
    if (0 == thread->frozen)
        return;
    if (0 == --thread->frozen)
        scheduler_add(thread, (1 << 0) | flags);
}

variable_types:
flags
unsigned char

user_defined_types: struct stack_t { unsigned long long address; unsigned long long length; }

user_defined_types: struct cpu_state_t { unsigned long long r15; unsigned long long r14; unsigned long long r13; unsigned long long r12; unsigned long long r11; unsigned long long r10; unsigned long long r9; unsigned long long r8; unsigned long long rsi; unsigned long long rdi; unsigned long long rbp; unsigned long long rdx; unsigned long long rcx; unsigned long long rbx; unsigned long long rax; }


decompiled_code:
__int64 __fastcall thread_join_sleep(__int64 a1, int *a2)
{
unsigned int v2; // er8
_DWORD *v3; // rax
int v4; // edx

if ( *(_DWORD *)(a1 + 4) != a2[1] )
{
console_print("PANIC: ");
console_print("Trying to join with thread of foreign process.");
console_print("\n");
while ( 1 )
;
}
v2 = a2[15] & 2;
if ( (a2[15] & 2) != 0 )
{
console_print("PANIC: ");
console_print("Trying to join with detached thread.");
console_print("\n");
while ( 1 )
;
}
if ( (a2[15] & 1) == 0 )
{
thread_freeze(a1);
*(_BYTE *)(a1 + 24) = 1;
v3 = (_DWORD *)heap_alloc(4LL);
v4 = *a2;
v2 = 1;
*(_QWORD *)(a1 + 32) = v3;
*v3 = v4;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, int *a2)
{
unsigned int v2; // er8
_DWORD *v3; // rax
int v4; // edx

if ( *(_DWORD *)(a1 + 4) != a2[1] )
{
console_print("PANIC: ");
console_print("Trying to join with thread of foreign process.");
console_print("\n");
while ( 1 )
;
}
v2 = a2[15] & 2;
if ( (a2[15] & 2) != 0 )
{
console_print("PANIC: ");
console_print("Trying to join with detached thread.");
console_print("\n");
while ( 1 )
;
}
if ( (a2[15] & 1) == 0 )
{
func4(a1);
*(_BYTE *)(a1 + 24) = 1;
v3 = (_DWORD *)heap_alloc(4LL);
v4 = *a2;
v2 = 1;
*(_QWORD *)(a1 + 32) = v3;
*v3 = v4;
}
return v2;
}

original_code:
bool thread_join_sleep(thread_t *thread, thread_t *wait_for) {
    if (__builtin_expect(!!(thread->pid != wait_for->pid), 0))
        { console_print("PANIC: "); console_print("Trying to join with thread of foreign process."); console_print("\n"); while (1); };
    if (__builtin_expect(!!(0 != (wait_for->flags & (1 << 1))), 0))
        { console_print("PANIC: "); console_print("Trying to join with detached thread."); console_print("\n"); while (1); };
    if (__builtin_expect(!!(0 != (wait_for->flags & (1 << 0))), 0))
        return 0;
    thread_freeze(thread);
    thread->sleep_mode = 1;
    thread->sleep_ctx = heap_alloc(sizeof(uint32_t));
    *((uint32_t *) thread->sleep_ctx) = wait_for->tid;
    return 1;
}

canonical_original_code:
unsigned char thread_join_sleep(struct thread_t *thread, struct thread_t *wait_for) {
    if (__builtin_expect(!!(thread->pid != wait_for->pid), 0))
        { console_print("PANIC: "); console_print("Trying to join with thread of foreign process."); console_print("\n"); while (1); };
    if (__builtin_expect(!!(0 != (wait_for->flags & (1 << 1))), 0))
        { console_print("PANIC: "); console_print("Trying to join with detached thread."); console_print("\n"); while (1); };
    if (__builtin_expect(!!(0 != (wait_for->flags & (1 << 0))), 0))
        return 0;
    thread_freeze(thread);
    thread->sleep_mode = 1;
    thread->sleep_ctx = heap_alloc(sizeof(unsigned int));
    *((unsigned int *) thread->sleep_ctx) = wait_for->tid;
    return 1;
}

return_type: unsigned char


decompiled_code:
__int64 __fastcall thread_join_awake(__int64 a1, __int64 a2)
{
if ( *(_BYTE *)(a1 + 24) != 1 )
{
console_print("PANIC: ");
console_print("Trying to awake non-joining thread from join-trigged sleep.");
console_print("\n");
while ( 1 )
;
}
*(_BYTE *)(a1 + 24) = 0;
heap_free(*(_QWORD *)(a1 + 32));
*(_QWORD *)(a1 + 192) = *(_QWORD *)(a2 + 40);
return thread_thaw(a1, 0);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2)
{
if ( *(_BYTE *)(a1 + 24) != 1 )
{
console_print("PANIC: ");
console_print("Trying to awake non-joining thread from join-trigged sleep.");
console_print("\n");
while ( 1 )
;
}
*(_BYTE *)(a1 + 24) = 0;
heap_free(*(_QWORD *)(a1 + 32));
*(_QWORD *)(a1 + 192) = *(_QWORD *)(a2 + 40);
return func5(a1, 0);
}

original_code:
void thread_join_awake(thread_t *thread, thread_t *wait_for) {
    if (__builtin_expect(!!(1 != thread->sleep_mode), 0))
        { console_print("PANIC: "); console_print("Trying to awake non-joining thread from join-trigged sleep."); console_print("\n"); while (1); };
    thread->sleep_mode = 0;
    heap_free(thread->sleep_ctx);
    thread->state.state.rbx = (uintptr_t) wait_for->result_ptr;
    thread_thaw(thread, 0);
}

canonical_original_code:
void thread_join_awake(struct thread_t *thread, struct thread_t *wait_for) {
    if (__builtin_expect(!!(1 != thread->sleep_mode), 0))
        { console_print("PANIC: "); console_print("Trying to awake non-joining thread from join-trigged sleep."); console_print("\n"); while (1); };
    thread->sleep_mode = 0;
    heap_free(thread->sleep_ctx);
    thread->state.state.rbx = (unsigned long long) wait_for->result_ptr;
    thread_thaw(thread, 0);
}


decompiled_code:
void __fastcall thread_dispose_all(__int64 a1)
{
__int64 v1; // rbx
__int64 v2; // rsi
char v3; // al

v1 = *(_QWORD *)(a1 + 40);
while ( v1 )
{
while ( 1 )
{
v2 = v1;
v1 = *(_QWORD *)(v1 + 288);
v3 = *(_BYTE *)(v2 + 60);
if ( (v3 & 1) != 0 )
break;
*(_BYTE *)(v2 + 60) = v3 | 2;
thread_stop((unsigned int *)a1, v2);
if ( !v1 )
return;
}
thread_free((unsigned int *)a1, *(_DWORD *)v2);
}
}

original_code:
void thread_dispose_all(process_t *process) {
    thread_t *current = process->threads;
    while (0 != current) {
        thread_t *next = current->next;
        if (0 == (current->flags & (1 << 0))) {
            current->flags |= (1 << 1);
            thread_stop(process, current);
        } else {
            thread_free(process, current->tid);
        }
        current = next;
    }
}

canonical_original_code:
void thread_dispose_all(struct process_t *process) {
    struct thread_t *current = process->threads;
    while (0 != current) {
        struct thread_t *next = current->next;
        if (0 == (current->flags & (1 << 0))) {
            current->flags |= (1 << 1);
            thread_stop(process, current);
        } else {
            thread_free(process, current->tid);
        }
        current = next;
    }
}


decompiled_code:
void *__fastcall thread_switch(__int64 a1, void *a2)
{
__int64 v3; // rdi
void *result; // rax

if ( thread_current )
{
memcpy((void *)(thread_current + 80LL), a2, 0xB8uLL);
fpu_save(*(_QWORD *)(thread_current + 72LL));
}
if ( !a1 )
return thread_idle(a2);
v3 = *(unsigned int *)(a1 + 4);
if ( !process_current || *process_current != (_DWORD)v3 )
process_current = (_DWORD *)process_get(v3);
thread_current = a1;
if ( memory_space_get() != *((_QWORD *)process_current + 6) )
memory_space_switch();
memcpy(a2, (const void *)(a1 + 80), 0xB8uLL);
result = (void *)fpu_load(*(_QWORD *)(thread_current + 72LL));
if ( (*(_BYTE *)(a1 + 60) & 4) == 0 )
{
result = (void *)fpu_prepare();
*(_BYTE *)(a1 + 60) |= 4u;
}
return result;
}

canonical_decompiled_code:
void *__fastcall func10(__int64 a1, void *a2)
{
__int64 v3; // rdi
void *result; // rax

if ( thread_current )
{
memcpy((void *)(thread_current + 80LL), a2, 0xB8uLL);
fpu_save(*(_QWORD *)(thread_current + 72LL));
}
if ( !a1 )
return func1(a2);
v3 = *(unsigned int *)(a1 + 4);
if ( !process_current || *process_current != (_DWORD)v3 )
process_current = (_DWORD *)process_get(v3);
thread_current = a1;
if ( memory_space_get() != *((_QWORD *)process_current + 6) )
memory_space_switch();
memcpy(a2, (const void *)(a1 + 80), 0xB8uLL);
result = (void *)fpu_load(*(_QWORD *)(thread_current + 72LL));
if ( (*(_BYTE *)(a1 + 60) & 4) == 0 )
{
result = (void *)fpu_prepare();
*(_BYTE *)(a1 + 60) |= 4u;
}
return result;
}

original_code:
void thread_switch(thread_t *thread, cpu_int_state_t *state) {
    if (0 != thread_current) {
        memcpy(&thread_current->state, state, sizeof(cpu_int_state_t));
        fpu_save(thread_current->fx_data);
    }
    if (0 == thread) {
        _thread_idle(state);
    } else {
        if (0 == process_current || process_current->pid != thread->pid)
            process_current = process_get(thread->pid);
        thread_current = thread;
        if (memory_space_get() != process_current->addr_space)
            memory_space_switch(process_current->addr_space);
        memcpy(state, &thread->state, sizeof(cpu_int_state_t));
        fpu_load(thread_current->fx_data);
        if (0 == (thread->flags & (1 << 2))) {
            fpu_prepare();
            thread->flags |= (1 << 2);
        }
    }
}

canonical_original_code:
void thread_switch(struct thread_t *thread, struct cpu_int_state_t *state) {
    if (0 != thread_current) {
        memcpy(&thread_current->state, state, sizeof(cpu_int_state_t));
        fpu_save(thread_current->fx_data);
    }
    if (0 == thread) {
        _thread_idle(state);
    } else {
        if (0 == process_current || process_current->pid != thread->pid)
            process_current = process_get(thread->pid);
        thread_current = thread;
        if (memory_space_get() != process_current->addr_space)
            memory_space_switch(process_current->addr_space);
        memcpy(state, &thread->state, sizeof(cpu_int_state_t));
        fpu_load(thread_current->fx_data);
        if (0 == (thread->flags & (1 << 2))) {
            fpu_prepare();
            thread->flags |= (1 << 2);
        }
    }
}

variable_types:
state
struct cpu_int_state_t *


qsort

decompiled_code:
void __fastcall qsort(__int64 a1, unsigned __int64 a2, size_t a3, int (__fastcall *a4)(size_t, void *))
{
unsigned __int64 v6; // r13
unsigned __int64 v7; // rbx
void *v8; // r12
char *v9; // r14
unsigned __int64 i; // [rsp+10h] [rbp-58h]
__int64 v12; // [rsp+18h] [rbp-50h]
void *dest; // [rsp+20h] [rbp-48h]
void *src; // [rsp+28h] [rbp-40h]

for ( i = a2; i > 1; a1 += a3 * v12 )
{
v6 = 0LL;
v7 = i - 1;
v8 = malloc(a3);
dest = malloc(a3);
memcpy(v8, (const void *)(a1 + a3 * ((i - 1) >> 1)), a3);
v12 = 1LL;
while ( 1 )
{
while ( v6 < i && a4(a1 + a3 * v6, v8) < 0 )
LABEL_11:
v6 = v12++;
v9 = (char *)(a1 + a3 * v7);
while ( a4((size_t)v9, v8) > 0 )
{
v9 -= a3;
if ( !--v7 )
goto LABEL_12;
}
if ( v6 >= v7 )
break;
src = (void *)(a1 + a3 * v6);
if ( !a4((size_t)src, v9) )
goto LABEL_11;
memcpy(dest, v9, a3);
memcpy(v9, src, a3);
memcpy(src, dest, a3);
}
LABEL_12:
free(v8);
free(dest);
qsort(a1, v6, a3, a4);
i += ~v6;
}
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1, unsigned __int64 a2, size_t a3, int (__fastcall *a4)(size_t, void *))
{
unsigned __int64 v6; // r13
unsigned __int64 v7; // rbx
void *v8; // r12
char *v9; // r14
unsigned __int64 i; // [rsp+10h] [rbp-58h]
__int64 v12; // [rsp+18h] [rbp-50h]
void *dest; // [rsp+20h] [rbp-48h]
void *src; // [rsp+28h] [rbp-40h]

for ( i = a2; i > 1; a1 += a3 * v12 )
{
v6 = 0LL;
v7 = i - 1;
v8 = malloc(a3);
dest = malloc(a3);
memcpy(v8, (const void *)(a1 + a3 * ((i - 1) >> 1)), a3);
v12 = 1LL;
while ( 1 )
{
while ( v6 < i && a4(a1 + a3 * v6, v8) < 0 )
LABEL_11:
v6 = v12++;
v9 = (char *)(a1 + a3 * v7);
while ( a4((size_t)v9, v8) > 0 )
{
v9 -= a3;
if ( !--v7 )
goto LABEL_12;
}
if ( v6 >= v7 )
break;
src = (void *)(a1 + a3 * v6);
if ( !a4((size_t)src, v9) )
goto LABEL_11;
memcpy(dest, v9, a3);
memcpy(v9, src, a3);
memcpy(src, dest, a3);
}
LABEL_12:
free(v8);
free(dest);
func0(a1, v6, a3, a4);
i += ~v6;
}
}

original_code:
void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void*)) {
 if (nmemb <= 1) {
  return;
 }
 size_t left = 0;
 size_t right = nmemb - 1;
 void *pivot = malloc(size);
 void *t = malloc(size);
 memcpy(pivot, (uint8_t*) base + (left + right) / 2 * size, size);
 while (1) {
  while (left < nmemb && compar((uint8_t*) base + left * size, pivot) < 0) {
   left++;
  }
  while (right > 0 && compar((uint8_t*) base + right * size, pivot) > 0) {
   right--;
  }
  if (left >= right) {
   break;
  }
  if (compar((uint8_t*) base + left * size, (uint8_t*) base + right * size) == 0) {
   left++;
   continue;
  }
  memcpy(t, (uint8_t*) base + right * size, size);
  memcpy((uint8_t*) base + right * size, (uint8_t*) base + left * size, size);
  memcpy((uint8_t*) base + left * size, t, size);
 }
 free(pivot);
 free(t);
 qsort(base, left, size, compar);
 qsort((uint8_t*) base + (left + 1) * size, nmemb - left - 1, size, compar);
}

canonical_original_code:
void qsort(void *base, unsigned long long nmemb, unsigned long long size, int (*compar)(void *, void *)) {
 if (nmemb <= 1) {
  return;
 }
 unsigned long long left = 0;
 unsigned long long right = nmemb - 1;
 void *pivot = malloc(size);
 void *t = malloc(size);
 memcpy(pivot, (unsigned char *) base + (left + right) / 2 * size, size);
 while (1) {
  while (left < nmemb && compar((unsigned char *) base + left * size, pivot) < 0) {
   left++;
  }
  while (right > 0 && compar((unsigned char *) base + right * size, pivot) > 0) {
   right--;
  }
  if (left >= right) {
   break;
  }
  if (compar((unsigned char *) base + left * size, (unsigned char *) base + right * size) == 0) {
   left++;
   continue;
  }
  memcpy(t, (unsigned char *) base + right * size, size);
  memcpy((unsigned char *) base + right * size, (unsigned char *) base + left * size, size);
  memcpy((unsigned char *) base + left * size, t, size);
 }
 free(pivot);
 free(t);
 qsort(base, left, size, compar);
 qsort((unsigned char *) base + (left + 1) * size, nmemb - left - 1, size, compar);
}

variable_types:
base
void *

variable_types:
nmemb
unsigned long long

variable_types:
size
unsigned long long

variable_types:
compar
int (*)(void *, void *)

variable_types:
left
unsigned long long

variable_types:
right
unsigned long long

variable_types:
pivot
void *

variable_types:
t
void *


strxfrm

decompiled_code:
size_t __fastcall strxfrm(char *a1, const char *a2, size_t a3)
{
strncpy(a1, a2, a3);
return a3;
}

canonical_decompiled_code:
size_t __fastcall func0(char *a1, const char *a2, size_t a3)
{
strncpy(a1, a2, a3);
return a3;
}

original_code:
size_t strxfrm(char *dest, const char *src, size_t n) {
 strncpy(dest, src, n);
 return n;
}

canonical_original_code:
unsigned long long strxfrm(char *dest, char *src, unsigned long long n) {
 strncpy(dest, src, n);
 return n;
}

variable_types:
dest
char *

variable_types:
src
char *

variable_types:
n
unsigned long long

return_type: unsigned long long


sqrtl

decompiled_code:
long double __fastcall sqrtl(long double a1)
{
long double result; // fst7
float v8; // xmm0_4
long double v9; // fst7
long double v10; // fst7
long double v11; // fst4
bool v12; // zf
unsigned int v13; // [rsp+8h] [rbp-10h]

if ( a1 < 0.0 )
{
_errno[(unsigned int)thread_id()] = 7;
result = 0.0 / 0.0;
}
else if ( 1.0 / 0.0 == a1 )
{
result = 1.0 / 0.0;
}
else if ( -(1.0 / 0.0) == a1 )
{
result = 1.0 / 0.0;
}
else
{
result = 0.0;
if ( a1 != 0.0 )
{
*(float *)&v13 = a1;
v8 = *(float *)_sqrt_approx((__m128i)v13).m128i_i32;
v9 = (v8 + a1 / v8) * 0.5;
v10 = ((v9 + a1 / v9) * 0.5 + a1 / ((v9 + a1 / v9) * 0.5)) * 0.5;
result = (v10 + a1 / v10) * 0.5;
if ( (a1 - 1.0 > result * result || result * result > a1 + 1.0) && -1.0 != result )
{
do
{
v11 = (a1 / result + result) * 0.5;
v12 = v11 == result;
result = v11;
}
while ( !v12 );
}
}
}
return result;
}

canonical_decompiled_code:
long double __fastcall func2(long double a1)
{
long double result; // fst7
float v8; // xmm0_4
long double v9; // fst7
long double v10; // fst7
long double v11; // fst4
bool v12; // zf
unsigned int v13; // [rsp+8h] [rbp-10h]

if ( a1 < 0.0 )
{
_errno[(unsigned int)thread_id()] = 7;
result = 0.0 / 0.0;
}
else if ( 1.0 / 0.0 == a1 )
{
result = 1.0 / 0.0;
}
else if ( -(1.0 / 0.0) == a1 )
{
result = 1.0 / 0.0;
}
else
{
result = 0.0;
if ( a1 != 0.0 )
{
*(float *)&v13 = a1;
v8 = *(float *)func0((__m128i)v13).m128i_i32;
v9 = (v8 + a1 / v8) * 0.5;
v10 = ((v9 + a1 / v9) * 0.5 + a1 / ((v9 + a1 / v9) * 0.5)) * 0.5;
result = (v10 + a1 / v10) * 0.5;
if ( (a1 - 1.0 > result * result || result * result > a1 + 1.0) && -1.0 != result )
{
do
{
v11 = (a1 / result + result) * 0.5;
v12 = v11 == result;
result = v11;
}
while ( !v12 );
}
}
}
return result;
}

variable_types:
x
long double

variable_types:
y
long double

return_type: long double


decompiled_code:
__int64 __fastcall syscall_mutex_lock(__int64 a1)
{
unsigned __int64 v1; // rbp
__int64 result; // rax
__int64 v3; // rax
__int64 v4; // rax

v1 = *(_QWORD *)(a1 + 80);
result = memory_user_accessible(v1 & 0xFFFFFFFFFFFFF000LL);
if ( (_BYTE)result )
{
if ( *(_BYTE *)v1 )
{
thread_freeze(thread_current);
v3 = thread_current;
*(_QWORD *)(thread_current + 32LL) = v1;
*(_BYTE *)(v3 + 24) = 2;
v4 = scheduler_next();
result = thread_switch(v4, a1);
}
else
{
*(_BYTE *)v1 = 1;
}
*(_QWORD *)(a1 + 120) = 0LL;
}
else
{
*(_QWORD *)(a1 + 120) = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
unsigned __int64 v1; // rbp
__int64 result; // rax
__int64 v3; // rax
__int64 v4; // rax

v1 = *(_QWORD *)(a1 + 80);
result = memory_user_accessible(v1 & 0xFFFFFFFFFFFFF000LL);
if ( (_BYTE)result )
{
if ( *(_BYTE *)v1 )
{
thread_freeze(thread_current);
v3 = thread_current;
*(_QWORD *)(thread_current + 32LL) = v1;
*(_BYTE *)(v3 + 24) = 2;
v4 = scheduler_next();
result = thread_switch(v4, a1);
}
else
{
*(_BYTE *)v1 = 1;
}
*(_QWORD *)(a1 + 120) = 0LL;
}
else
{
*(_QWORD *)(a1 + 120) = 1LL;
}
return result;
}

original_code:
void syscall_mutex_lock(cpu_int_state_t *state) {
 uintptr_t mutex_vaddr = state->state.rdi;
 if (!memory_user_accessible(mutex_vaddr & ~0xFFF)) { state->state.rax = 1; return; };;
 uint8_t *mutex_ptr = (uint8_t *) mutex_vaddr;
 if (0 == *mutex_ptr) {
  *mutex_ptr = 1;
  { state->state.rax = 0; return; };
 }
 thread_freeze(thread_current);
 thread_current->sleep_mode = 2;
 thread_current->sleep_ctx = mutex_ptr;
 thread_switch(scheduler_next(), state);
 { state->state.rax = 0; return; };
}

canonical_original_code:
void syscall_mutex_lock(struct cpu_int_state_t *state) {
 unsigned long long mutex_vaddr = state->state.rdi;
 if (!memory_user_accessible(mutex_vaddr & ~0xFFF)) { state->state.rax = 1; return; };;
 unsigned char *mutex_ptr = (uint8_t *) mutex_vaddr;
 if (0 == *mutex_ptr) {
  *mutex_ptr = 1;
  { state->state.rax = 0; return; };
 }
 thread_freeze(thread_current);
 thread_current->sleep_mode = 2;
 thread_current->sleep_ctx = mutex_ptr;
 thread_switch(scheduler_next(), state);
 { state->state.rax = 0; return; };
}

variable_types: struct cpu_int_state_t { unsigned long long ds; struct cpu_state_t { unsigned long long r15; unsigned long long r14; unsigned long long r13; unsigned long long r12; unsigned long long r11; unsigned long long r10; unsigned long long r9; unsigned long long r8; unsigned long long rsi; unsigned long long rdi; unsigned long long rbp; unsigned long long rdx; unsigned long long rcx; unsigned long long rbx; unsigned long long rax; } state; unsigned long long vector; unsigned long long error_code; unsigned long long rip; unsigned long long cs; unsigned long long flags; unsigned long long rsp; unsigned long long ss; } *


decompiled_code:
__int64 __fastcall syscall_mutex_unlock(__int64 a1)
{
unsigned __int64 v2; // rbp
__int64 result; // rax
__int64 v4; // rdi

v2 = *(_QWORD *)(a1 + 80);
result = memory_user_accessible(v2 & 0xFFFFFFFFFFFFF000LL);
if ( (_BYTE)result )
{
if ( !*(_BYTE *)v2 )
{
LABEL_10:
*(_QWORD *)(a1 + 120) = 0LL;
return result;
}
result = process_current;
v4 = *(_QWORD *)(process_current + 40);
if ( !v4 )
{
LABEL_9:
*(_BYTE *)v2 = 0;
goto LABEL_10;
}
while ( *(_BYTE *)(v4 + 24) != 2 || *(_QWORD *)(v4 + 32) != v2 )
{
v4 = *(_QWORD *)(v4 + 288);
if ( !v4 )
goto LABEL_9;
}
*(_BYTE *)(v4 + 24) = 0;
*(_QWORD *)(v4 + 32) = 0LL;
result = thread_thaw(v4, 0LL);
*(_QWORD *)(a1 + 120) = 0LL;
}
else
{
*(_QWORD *)(a1 + 120) = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
unsigned __int64 v2; // rbp
__int64 result; // rax
__int64 v4; // rdi

v2 = *(_QWORD *)(a1 + 80);
result = memory_user_accessible(v2 & 0xFFFFFFFFFFFFF000LL);
if ( (_BYTE)result )
{
if ( !*(_BYTE *)v2 )
{
LABEL_10:
*(_QWORD *)(a1 + 120) = 0LL;
return result;
}
result = process_current;
v4 = *(_QWORD *)(process_current + 40);
if ( !v4 )
{
LABEL_9:
*(_BYTE *)v2 = 0;
goto LABEL_10;
}
while ( *(_BYTE *)(v4 + 24) != 2 || *(_QWORD *)(v4 + 32) != v2 )
{
v4 = *(_QWORD *)(v4 + 288);
if ( !v4 )
goto LABEL_9;
}
*(_BYTE *)(v4 + 24) = 0;
*(_QWORD *)(v4 + 32) = 0LL;
result = thread_thaw(v4, 0LL);
*(_QWORD *)(a1 + 120) = 0LL;
}
else
{
*(_QWORD *)(a1 + 120) = 1LL;
}
return result;
}

original_code:
void syscall_mutex_unlock(cpu_int_state_t *state) {
 uintptr_t mutex_vaddr = state->state.rdi;
 if (!memory_user_accessible(mutex_vaddr & ~0xFFF)) { state->state.rax = 1; return; };;
 uint8_t *mutex_ptr = (uint8_t *) mutex_vaddr;
 if (0 == *mutex_ptr) {
  { state->state.rax = 0; return; };
 }
 thread_t *thread = process_current->threads;
 while (0 != thread) {
  if (2 == thread->sleep_mode &&
   mutex_ptr == thread->sleep_ctx) {
   thread->sleep_mode = 0;
   thread->sleep_ctx = 0;
   thread_thaw(thread, 0);
   { state->state.rax = 0; return; };
  }
  thread = thread->next;
 }
 *mutex_ptr = 0;
 { state->state.rax = 0; return; };
}

canonical_original_code:
void syscall_mutex_unlock(struct cpu_int_state_t *state) {
 unsigned long long mutex_vaddr = state->state.rdi;
 if (!memory_user_accessible(mutex_vaddr & ~0xFFF)) { state->state.rax = 1; return; };;
 unsigned char *mutex_ptr = (uint8_t *) mutex_vaddr;
 if (0 == *mutex_ptr) {
  { state->state.rax = 0; return; };
 }
 struct thread_t *thread = process_current->threads;
 while (0 != thread) {
  if (2 == thread->sleep_mode &&
   mutex_ptr == thread->sleep_ctx) {
   thread->sleep_mode = 0;
   thread->sleep_ctx = 0;
   thread_thaw(thread, 0);
   { state->state.rax = 0; return; };
  }
  thread = thread->next;
 }
 *mutex_ptr = 0;
 { state->state.rax = 0; return; };
}

variable_types: struct thread_t { unsigned int tid; unsigned int pid; unsigned char frozen; unsigned char ttl; unsigned char role; void *role_ctx; unsigned char sleep_mode; void *sleep_ctx; void *result_ptr; unsigned int ipc_buffer_sz[3]; unsigned char flags; unsigned long long entry_point; void *fx_data; struct cpu_int_state_t { unsigned long long ds; struct cpu_state_t { unsigned long long r15; unsigned long long r14; unsigned long long r13; unsigned long long r12; unsigned long long r11; unsigned long long r10; unsigned long long r9; unsigned long long r8; unsigned long long rsi; unsigned long long rdi; unsigned long long rbp; unsigned long long rdx; unsigned long long rcx; unsigned long long rbx; unsigned long long rax; } state; unsigned long long vector; unsigned long long error_code; unsigned long long rip; unsigned long long cs; unsigned long long flags; unsigned long long rsp; unsigned long long ss; } state; struct stack_t { unsigned long long address; unsigned long long length; } stack; struct thread_t *next_sched; struct thread_t *next; } *


memmove

decompiled_code:
unsigned __int64 __fastcall memmove(unsigned __int64 a1, unsigned __int64 a2, __int64 a3)
{
__int64 i; // rax
__int64 v5; // rax

if ( a1 == a2 )
return a1;
if ( a1 < a2 )
{
if ( a3 )
{
for ( i = 0LL; i != a3; ++i )
*(_BYTE *)(a1 + i) = *(_BYTE *)(a2 + i);
}
return a1;
}
if ( a1 <= a2 || !a3 )
return a1;
v5 = a3 - 1;
do
*(_BYTE *)(a1 + v5) = *(_BYTE *)(a2 + v5);
while ( v5-- != 0 );
return a1;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(unsigned __int64 a1, unsigned __int64 a2, __int64 a3)
{
__int64 i; // rax
__int64 v5; // rax

if ( a1 == a2 )
return a1;
if ( a1 < a2 )
{
if ( a3 )
{
for ( i = 0LL; i != a3; ++i )
*(_BYTE *)(a1 + i) = *(_BYTE *)(a2 + i);
}
return a1;
}
if ( a1 <= a2 || !a3 )
return a1;
v5 = a3 - 1;
do
*(_BYTE *)(a1 + v5) = *(_BYTE *)(a2 + v5);
while ( v5-- != 0 );
return a1;
}

original_code:
void *memmove(void *d, const void *s, size_t n) {
 size_t i;
 uint8_t *dst = (uint8_t*) d;
 uint8_t *src = (uint8_t*) s;
 if (src == dst) {
  return dst;
 }
 else if (src > dst) {
  for (i = 0; i < n; i++) {
   dst[i] = src[i];
  }
 }
 else if (src < dst) {
  for (i = 0; i < n; i++) {
   dst[n - i - 1] = src[n - i - 1];
  }
 }
 return dst;
}

canonical_original_code:
void *memmove(void *d, void *s, unsigned long long n) {
 unsigned long long i;
 unsigned char *dst = (uint8_t*) d;
 unsigned char *src = (uint8_t*) s;
 if (src == dst) {
  return dst;
 }
 else if (src > dst) {
  for (i = 0; i < n; i++) {
   dst[i] = src[i];
  }
 }
 else if (src < dst) {
  for (i = 0; i < n; i++) {
   dst[n - i - 1] = src[n - i - 1];
  }
 }
 return dst;
}

variable_types:
d
void *

variable_types:
s
void *

variable_types:
i
unsigned long long

variable_types:
dst
unsigned char *

variable_types:
src
unsigned char *

return_type: void *


strcmp

original_code:
int strcmp(const char *s1, const char *s2) {
 size_t i;
 for (i = 0;; i++) {
  if (s1[i] == s2[i]) {
   if (s1[i] == '\0') return 0;
   continue;
  }
  if (s1[i] == '\0') return -1;
  if (s2[i] == '\0') return 1;
  if (s1[i] < s2[i]) return -1;
  else return 1;
 }
}

canonical_original_code:
int strcmp(char *s1, char *s2) {
 unsigned long long i;
 for (i = 0;; i++) {
  if (s1[i] == s2[i]) {
   if (s1[i] == '\0') return 0;
   continue;
  }
  if (s1[i] == '\0') return -1;
  if (s2[i] == '\0') return 1;
  if (s1[i] < s2[i]) return -1;
  else return 1;
 }
}


fseek

decompiled_code:
__int64 __fastcall fseek(__int64 a1, __int64 a2, int a3)
{
__int64 v3; // r12
__int64 (__fastcall *v5)(__int64); // rax
__int64 result; // rax

v3 = a1 + 8;
mutex_lock(a1 + 8);
switch ( a3 )
{
case 0:
goto LABEL_6;
case 1:
a2 += *(_QWORD *)(a1 + 16);
goto LABEL_6;
case 2:
v5 = *(__int64 (__fastcall **)(__int64))(a1 + 80);
if ( v5 )
{
a2 += v5(a1);
LABEL_6:
*(_BYTE *)(a1 + 24) &= 0xFEu;
*(_QWORD *)(a1 + 16) = a2;
mutex_unlock(v3);
return 0LL;
}
result = 0xFFFFFFFFLL;
break;
default:
mutex_unlock(v3);
result = 0xFFFFFFFFLL;
break;
}
return result;
}

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

v3 = a1 + 8;
mutex_lock(a1 + 8);
switch ( a3 )
{
case 0:
goto LABEL_6;
case 1:
a2 += *(_QWORD *)(a1 + 16);
goto LABEL_6;
case 2:
v5 = *(__int64 (__fastcall **)(__int64))(a1 + 80);
if ( v5 )
{
a2 += v5(a1);
LABEL_6:
*(_BYTE *)(a1 + 24) &= 0xFEu;
*(_QWORD *)(a1 + 16) = a2;
mutex_unlock(v3);
return 0LL;
}
result = 0xFFFFFFFFLL;
break;
default:
mutex_unlock(v3);
result = 0xFFFFFFFFLL;
break;
}
return result;
}

original_code:
int fseek(FILE *stream, long offset, int origin) {
    mutex_lock(&stream->lock);
    fpos_t position;
    if (0 == origin)
        position = (fpos_t) offset;
    else if (1 == origin)
        position = (fpos_t) ((long) stream->position + offset);
    else if (2 == origin) {
        if (0 == stream->callback_length)
            return -1;
        size_t length = stream->callback_length(stream);
        position = (fpos_t) ((long) length + offset);
    } else {
        mutex_unlock(&stream->lock);
        return -1;
    }
    stream->position = position;
    stream->flags &= ~(1 << 0);
    mutex_unlock(&stream->lock);
    return 0;
}

canonical_original_code:
int fseek(struct __file *stream, long offset, int origin) {
    mutex_lock(&stream->lock);
    unsigned long long position;
    if (0 == origin)
        position = (unsigned long long) offset;
    else if (1 == origin)
        position = (fpos_t) ((long) stream->position + offset);
    else if (2 == origin) {
        if (0 == stream->callback_length)
            return -1;
        unsigned long long length = stream->callback_length(stream);
        position = (fpos_t) ((long) length + offset);
    } else {
        mutex_unlock(&stream->lock);
        return -1;
    }
    stream->position = position;
    stream->flags &= ~(1 << 0);
    mutex_unlock(&stream->lock);
    return 0;
}

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

variable_types: offset long

variable_types: origin int

variable_types:
position
unsigned long long

variable_types:
length
unsigned long long


decompiled_code:
__int64 kmain_header()
{
console_print("---------------------------------------\n");
console_print("Carbon AMD64 Microkernel 0.1\n");
console_print("Copyright (c) 2011 by Lukas Heidemann\n");
console_print("---------------------------------------\n");
console_print("Loader: ");
console_print(*(_QWORD *)info);
console_print("\nEntry point: ");
console_print_hex(*(_QWORD *)(info + 16));
console_print("\nEnd of allocated memory: ");
console_print_hex(*(_QWORD *)(info + 24));
return console_print("\n");
}

canonical_decompiled_code:
__int64 func0()
{
console_print("---------------------------------------\n");
console_print("Carbon AMD64 Microkernel 0.1\n");
console_print("Copyright (c) 2011 by Lukas Heidemann\n");
console_print("---------------------------------------\n");
console_print("Loader: ");
console_print(*(_QWORD *)info);
console_print("\nEntry point: ");
console_print_hex(*(_QWORD *)(info + 16));
console_print("\nEnd of allocated memory: ");
console_print_hex(*(_QWORD *)(info + 24));
return console_print("\n");
}

original_code:
static void kmain_header() {
    console_print("---------------------------------------\n");
    console_print("Carbon AMD64 Microkernel 0.1\n");
    console_print("Copyright (c) 2011 by Lukas Heidemann\n");
    console_print("---------------------------------------\n");
    console_print("Loader: ");
    console_print(info->loader_name);
    console_print("\nEntry point: ");
    console_print_hex(info->entry_point);
    console_print("\nEnd of allocated memory: ");
    console_print_hex(info->free_mem_begin);
    console_print("\n");
}


decompiled_code:
__int64 kmain_init()
{
console_print("---------------------------------------\n");
console_print("Initializing virtual memory management...\n");
memory_space_initial = memory_space_get();
console_print("Initializing physical memory management...\n");
frame_init(info);
console_print("Initializing interrupt management...\n");
irq_pic_init();
cpu_int_init();
cpu_int_enable();
cpu_tss_create();
fault_install();
console_print("Initializing floating point unit...\n");
fpu_init();
console_print("Initializing multitasking...\n");
process_init();
return cpu_int_register(128LL, &syscall_handler_int);
}

canonical_decompiled_code:
__int64 func1()
{
console_print("---------------------------------------\n");
console_print("Initializing virtual memory management...\n");
memory_space_initial = memory_space_get();
console_print("Initializing physical memory management...\n");
frame_init(info);
console_print("Initializing interrupt management...\n");
irq_pic_init();
cpu_int_init();
cpu_int_enable();
cpu_tss_create();
fault_install();
console_print("Initializing floating point unit...\n");
fpu_init();
console_print("Initializing multitasking...\n");
process_init();
return cpu_int_register(128LL, &syscall_handler_int);
}

original_code:
static void kmain_init() {
    console_print("---------------------------------------\n");
    console_print("Initializing virtual memory management...\n");
    memory_space_initial = memory_space_get();
    console_print("Initializing physical memory management...\n");
    frame_init(info);
    console_print("Initializing interrupt management...\n");
    irq_pic_init();
    cpu_int_init();
    cpu_int_enable();
    cpu_tss_create();
    fault_install();
    console_print("Initializing floating point unit...\n");
    fpu_init();
    console_print("Initializing multitasking...\n");
    process_init();
    cpu_int_register(0x80, &syscall_handler_int);
}


decompiled_code:
__int64 kmain_root()
{
__int64 v0; // rax
__int64 v1; // rbp
__int64 v2; // rax
__int64 v3; // rax
__int64 v4; // r12
__int64 v5; // rbp
__int64 v6; // rax

console_print("---------------------------------------\n");
console_print("Searching for root module...\n");
v0 = find_root_mod(info);
if ( !v0 )
{
console_print("PANIC: ");
console_print(
"Could not find root binary module.\n"
"Please make sure to attach the root binary as a module\n"
"with name '/boot/root.bin'.");
console_print("\n");
while ( 1 )
;
}
v1 = v0;
v2 = memory_space_create();
memory_space_switch(v2);
console_print("Creating process...\n");
v3 = memory_space_get();
v4 = process_spawn(v3, 0LL);
console_print("Loading binary...\n");
v5 = binary_load_elf64(*(_QWORD *)(v1 + 8), 4LL);
console_print("Starting thread...\n");
v6 = thread_spawn(v4, v5);
thread_thaw(v6, 0LL);
console_print("Passing control to root process...\n");
console_print("---------------------------------------\n");
return irq_pit_init();
}

canonical_decompiled_code:
__int64 func3()
{
__int64 v0; // rax
__int64 v1; // rbp
__int64 v2; // rax
__int64 v3; // rax
__int64 v4; // r12
__int64 v5; // rbp
__int64 v6; // rax

console_print("---------------------------------------\n");
console_print("Searching for root module...\n");
v0 = func2(info);
if ( !v0 )
{
console_print("PANIC: ");
console_print(
"Could not find root binary module.\n"
"Please make sure to attach the root binary as a module\n"
"with name '/boot/root.bin'.");
console_print("\n");
while ( 1 )
;
}
v1 = v0;
v2 = memory_space_create();
memory_space_switch(v2);
console_print("Creating process...\n");
v3 = memory_space_get();
v4 = process_spawn(v3, 0LL);
console_print("Loading binary...\n");
v5 = binary_load_elf64(*(_QWORD *)(v1 + 8), 4LL);
console_print("Starting thread...\n");
v6 = thread_spawn(v4, v5);
thread_thaw(v6, 0LL);
console_print("Passing control to root process...\n");
console_print("---------------------------------------\n");
return irq_pit_init();
}

original_code:
static void kmain_root() {
    console_print("---------------------------------------\n");
    console_print("Searching for root module...\n");
    boot_info_mod_t *root_mod = _find_root_mod(info);
    if (0 == root_mod)
 { console_print("PANIC: "); console_print("Could not find root binary module.\n" "Please make sure to attach the root binary as a module\n" "with name '/boot/root.bin'."); console_print("\n"); while (1); };
    memory_space_switch(memory_space_create());
    uint16_t pflags = (1 << 2);
    console_print("Creating process...\n");
    process_t *proc = process_spawn(memory_space_get(), 0);
    console_print("Loading binary...\n");
    uintptr_t entry_addr = binary_load_elf64((void *) root_mod->mapping, pflags);
    console_print("Starting thread...\n");
    thread_thaw(thread_spawn(proc, entry_addr), 0);
    console_print("Passing control to root process...\n");
    console_print("---------------------------------------\n");
    irq_pit_init();
}

canonical_original_code:
static void kmain_root() {
    console_print("---------------------------------------\n");
    console_print("Searching for root module...\n");
    struct boot_info_mod_t *root_mod = _find_root_mod(info);
    if (0 == root_mod)
 { console_print("PANIC: "); console_print("Could not find root binary module.\n" "Please make sure to attach the root binary as a module\n" "with name '/boot/root.bin'."); console_print("\n"); while (1); };
    memory_space_switch(memory_space_create());
    unsigned short pflags = (1 << 2);
    console_print("Creating process...\n");
    struct process_t *proc = process_spawn(memory_space_get(), 0);
    console_print("Loading binary...\n");
    unsigned long long entry_addr = binary_load_elf64((void *) root_mod->mapping, pflags);
    console_print("Starting thread...\n");
    thread_thaw(thread_spawn(proc, entry_addr), 0);
    console_print("Passing control to root process...\n");
    console_print("---------------------------------------\n");
    irq_pit_init();
}

variable_types: struct boot_info_mod_t { unsigned long long address; unsigned long long mapping; unsigned long long length; unsigned long long name; struct boot_info_mod_t *next; } *

variable_types:
pflags
unsigned short

variable_types: struct process_t { unsigned int pid; struct process_t *parent; unsigned short order; unsigned long long message_handler; unsigned char term_implicit; struct thread_t { unsigned int tid; unsigned int pid; unsigned char frozen; unsigned char ttl; unsigned char role; void *role_ctx; unsigned char sleep_mode; void *sleep_ctx; void *result_ptr; unsigned int ipc_buffer_sz[3]; unsigned char flags; unsigned long long entry_point; void *fx_data; struct cpu_int_state_t { unsigned long long ds; struct cpu_state_t { unsigned long long r15; unsigned long long r14; unsigned long long r13; unsigned long long r12; unsigned long long r11; unsigned long long r10; unsigned long long r9; unsigned long long r8; unsigned long long rsi; unsigned long long rdi; unsigned long long rbp; unsigned long long rdx; unsigned long long rcx; unsigned long long rbx; unsigned long long rax; } state; unsigned long long vector; unsigned long long error_code; unsigned long long rip; unsigned long long cs; unsigned long long flags; unsigned long long rsp; unsigned long long ss; } state; struct stack_t { unsigned long long address; unsigned long long length; } stack; struct thread_t *next_sched; struct thread_t *next; } *threads; unsigned long long addr_space; unsigned long long stack_offset; struct process_t *next; } *


kmain

decompiled_code:
__int64 kmain()
{
console_videomem = 0xFFFFFF7FFFFFE000LL;
console_clear();
info = 0xFFFFFF7FFFFFF000LL;
kmain_header();
kmain_init();
return kmain_root();
}

canonical_decompiled_code:
__int64 func4()
{
console_videomem = 0xFFFFFF7FFFFFE000LL;
console_clear();
info = 0xFFFFFF7FFFFFF000LL;
func0();
func1();
return func3();
}

original_code:
void kmain() {
    console_videomem = (uint16_t *) (0xFFFFFF8000000000 - 0x002000);
    console_clear();
    info = (boot_info_t *) ((0xFFFFFF8000000000 - 0x001000));
    kmain_header();
    kmain_init();
    kmain_root();
}

canonical_original_code:
void kmain() {
    console_videomem = (unsigned short *) (0xFFFFFF8000000000 - 0x002000);
    console_clear();
    info = (struct boot_info_t *) ((0xFFFFFF8000000000 - 0x001000));
    kmain_header();
    kmain_init();
    kmain_root();
}

user_defined_types: struct boot_info_t { char *loader_name; char *args; unsigned long long entry_point; unsigned long long free_mem_begin; struct boot_info_mmap_t *mmap; struct boot_info_mod_t *mods; }

user_defined_types: struct boot_info_mmap_t { unsigned long long address; unsigned long long length; unsigned char available; struct boot_info_mmap_t *next; }

user_defined_types: struct boot_info_mod_t { unsigned long long address; unsigned long long mapping; unsigned long long length; unsigned long long name; struct boot_info_mod_t *next; }