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

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

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 }


repo: zpconn/Simple

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

return_type: int

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 *v4; // r14
char *i; // rbx
unsigned int v6; // eax
unsigned int v7; // eax
unsigned int v8; // er12
unsigned int v9; // eax
unsigned int v10; // eax
int v11; // ebx
unsigned int v12; // ebp
int v13; // eax
unsigned int v14; // eax
unsigned int v15; // eax
int v16; // ebx
int v17; // eax
unsigned int v18; // eax
int v19; // ebx
unsigned int v20; // eax
int v21; // ebp
_QWORD *v23; // [rsp+0h] [rbp-58h] BYREF
int *v24; // [rsp+8h] [rbp-50h] BYREF
__int64 v25; // [rsp+10h] [rbp-48h] BYREF
__int64 v26[8]; // [rsp+18h] [rbp-40h] BYREF

v26[0] = 0LL;
v25 = 0LL;
v24 = 0LL;
v23 = 0LL;
v4 = pad_string(a2);
for ( i = strtok(v4, " "); i; i = strtok(0LL, " ") )
{
if ( (unsigned int)is_integer(i) )
{
v6 = atoi(i);
enqueue(v26, &v25, v6, 0LL);
}
else
{
v7 = token_determine_op(i);
v8 = v7;
if ( v7 == 7 )
{
if ( (unsigned int)check_variable_name_no_error(i) )
{
v14 = simple_lookup_variable(a3, i, a1);
enqueue(v26, &v25, v14, 0LL);
}
else
{
simple_error(0LL, a1, "invalid or unrecognizable symbol is in the arithmetic expression");
}
}
else if ( v7 )
{
if ( v7 == 1 )
{
if ( (unsigned int)stack_empty(v24) )
simple_error(
0LL,
a1,
"incorrect matching of parentheses in arithmetic expression; inappropriate right parenthesis");
while ( *v24 )
{
if ( (unsigned int)stack_empty(v24) )
simple_error(0LL, a1, "incorrect matching of parentheses in arithmetic expression");
v9 = stack_pop(&v24);
enqueue(v26, &v25, v9, 1LL);
if ( (unsigned int)stack_empty(v24) )
simple_error(0LL, a1, "incorrect matching of parentheses in arithmetic expression");
}
stack_pop(&v24);
}
else
{
while ( !(unsigned int)stack_empty(v24) )
{
v11 = op_precedence(v8);
v12 = *v24;
v13 = op_precedence(*v24);
if ( v12 <= 1 || v11 > v13 )
break;
v10 = stack_pop(&v24);
enqueue(v26, &v25, v10, 1LL);
}
stack_push(&v24, v8);
}
}
else
{
stack_push(&v24, 0LL);
}
}
}
while ( !(unsigned int)stack_empty(v24) )
{
if ( (unsigned int)*v24 <= 1 )
simple_error(0LL, a1, "incorrect matching of parentheses in arithmetic expression");
v15 = stack_pop(&v24);
enqueue(v26, &v25, v15, 1LL);
}
while ( !(unsigned int)queue_empty(v26[0]) )
{
v19 = *(_DWORD *)(v26[0] + 4);
v20 = dequeue(v26, &v25);
v21 = v20;
if ( v19 == 1 )
{
if ( (unsigned int)stack_empty(v23) )
simple_error(0LL, a1, "incorrect syntax for arithmetic expression");
v16 = stack_pop(&v23);
if ( (unsigned int)stack_empty(v23) )
simple_error(0LL, a1, "incorrect syntax for arithmetic expression");
v17 = stack_pop(&v23);
v18 = evaluate_operator(v21, v17, v16);
stack_push(&v23, v18);
}
else if ( !v19 )
{
stack_push(&v23, v20);
}
}
if ( (unsigned int)stack_empty(v23) || v23[1] )
simple_error(0LL, a1, "incorrect syntax for arithmetic expression");
free(v4);
return *(unsigned int *)v23;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, const char *a2, __int64 a3)
{
char *v4; // r14
char *i; // rbx
unsigned int v6; // eax
unsigned int v7; // eax
unsigned int v8; // er12
unsigned int v9; // eax
unsigned int v10; // eax
int v11; // ebx
unsigned int v12; // ebp
int v13; // eax
unsigned int v14; // eax
unsigned int v15; // eax
int v16; // ebx
int v17; // eax
unsigned int v18; // eax
int v19; // ebx
unsigned int v20; // eax
int v21; // ebp
_QWORD *v23; // [rsp+0h] [rbp-58h] BYREF
int *v24; // [rsp+8h] [rbp-50h] BYREF
__int64 v25; // [rsp+10h] [rbp-48h] BYREF
__int64 v26[8]; // [rsp+18h] [rbp-40h] BYREF

v26[0] = 0LL;
v25 = 0LL;
v24 = 0LL;
v23 = 0LL;
v4 = pad_string(a2);
for ( i = strtok(v4, " "); i; i = strtok(0LL, " ") )
{
if ( (unsigned int)is_integer(i) )
{
v6 = atoi(i);
enqueue(v26, &v25, v6, 0LL);
}
else
{
v7 = func0(i);
v8 = v7;
if ( v7 == 7 )
{
if ( (unsigned int)check_variable_name_no_error(i) )
{
v14 = simple_lookup_variable(a3, i, a1);
enqueue(v26, &v25, v14, 0LL);
}
else
{
simple_error(0LL, a1, "invalid or unrecognizable symbol is in the arithmetic expression");
}
}
else if ( v7 )
{
if ( v7 == 1 )
{
if ( (unsigned int)stack_empty(v24) )
simple_error(
0LL,
a1,
"incorrect matching of parentheses in arithmetic expression; inappropriate right parenthesis");
while ( *v24 )
{
if ( (unsigned int)stack_empty(v24) )
simple_error(0LL, a1, "incorrect matching of parentheses in arithmetic expression");
v9 = stack_pop(&v24);
enqueue(v26, &v25, v9, 1LL);
if ( (unsigned int)stack_empty(v24) )
simple_error(0LL, a1, "incorrect matching of parentheses in arithmetic expression");
}
stack_pop(&v24);
}
else
{
while ( !(unsigned int)stack_empty(v24) )
{
v11 = func2(v8);
v12 = *v24;
v13 = func2(*v24);
if ( v12 <= 1 || v11 > v13 )
break;
v10 = stack_pop(&v24);
enqueue(v26, &v25, v10, 1LL);
}
stack_push(&v24, v8);
}
}
else
{
stack_push(&v24, 0LL);
}
}
}
while ( !(unsigned int)stack_empty(v24) )
{
if ( (unsigned int)*v24 <= 1 )
simple_error(0LL, a1, "incorrect matching of parentheses in arithmetic expression");
v15 = stack_pop(&v24);
enqueue(v26, &v25, v15, 1LL);
}
while ( !(unsigned int)queue_empty(v26[0]) )
{
v19 = *(_DWORD *)(v26[0] + 4);
v20 = dequeue(v26, &v25);
v21 = v20;
if ( v19 == 1 )
{
if ( (unsigned int)stack_empty(v23) )
simple_error(0LL, a1, "incorrect syntax for arithmetic expression");
v16 = stack_pop(&v23);
if ( (unsigned int)stack_empty(v23) )
simple_error(0LL, a1, "incorrect syntax for arithmetic expression");
v17 = stack_pop(&v23);
v18 = func1(v21, v17, v16);
stack_push(&v23, v18);
}
else if ( !v19 )
{
stack_push(&v23, v20);
}
}
if ( (unsigned int)stack_empty(v23) || v23[1] )
simple_error(0LL, a1, "incorrect syntax for arithmetic expression");
free(v4);
return *(unsigned int *)v23;
}

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: 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:
expression
char *

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

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:
token
char *

variable_types:
copy
char *

variable_types: value int

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

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 v5; // rdi
__int64 result; // rax

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 + 8LL * a3);
result = *(unsigned int *)(v5 + 4);
switch ( *(_DWORD *)(v5 + 4) )
{
case 0:
++*(_DWORD *)(a1 + 16);
break;
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);
++*(_DWORD *)(a1 + 16);
break;
case 4:
result = goto_command(a1, *(_QWORD *)(*(_QWORD *)a1 + 8LL * a3), a2);
break;
case 5:
result = conditional_command(a1, *(_QWORD *)(*(_QWORD *)a1 + 8LL * a3), a2);
break;
case 6:
*(_DWORD *)(a1 + 16) = -1;
break;
default:
result = simple_error(
5LL,
*(_QWORD *)(*(_QWORD *)a1 + 8LL * a3),
"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 v5; // rdi
__int64 result; // rax

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 + 8LL * a3);
result = *(unsigned int *)(v5 + 4);
switch ( *(_DWORD *)(v5 + 4) )
{
case 0:
++*(_DWORD *)(a1 + 16);
break;
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);
++*(_DWORD *)(a1 + 16);
break;
case 4:
result = goto_command(a1, *(_QWORD *)(*(_QWORD *)a1 + 8LL * a3), a2);
break;
case 5:
result = conditional_command(a1, *(_QWORD *)(*(_QWORD *)a1 + 8LL * a3), a2);
break;
case 6:
*(_DWORD *)(a1 + 16) = -1;
break;
default:
result = simple_error(
5LL,
*(_QWORD *)(*(_QWORD *)a1 + 8LL * a3),
"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;
 }
}

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

return_type: void

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

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


decompiled_code:
__int64 __fastcall determine_command(const char *a1)
{
unsigned int v1; // edx

v1 = 0;
if ( strcmp(a1, "rem") )
{
v1 = 1;
if ( strcmp(a1, "input") )
{
v1 = 2;
if ( strcmp(a1, "let") )
{
v1 = 3;
if ( strcmp(a1, "print") )
{
v1 = 4;
if ( strcmp(a1, "goto") )
{
v1 = 5;
if ( strcmp(a1, "if") )
v1 = 6 - ((strcmp(a1, "end") == 0) - 1);
}
}
}
}
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
unsigned int v1; // edx

v1 = 0;
if ( strcmp(a1, "rem") )
{
v1 = 1;
if ( strcmp(a1, "input") )
{
v1 = 2;
if ( strcmp(a1, "let") )
{
v1 = 3;
if ( strcmp(a1, "print") )
{
v1 = 4;
if ( strcmp(a1, "goto") )
{
v1 = 5;
if ( strcmp(a1, "if") )
v1 = 6 - ((strcmp(a1, "end") == 0) - 1);
}
}
}
}
}
return v1;
}

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:
char __fastcall simple_parse_line(__int64 a1)
{
const char *v1; // r12
size_t v2; // rax
char *v3; // rbp
char *v4; // r12
char *v5; // r12
int v6; // eax
int v7; // er13
const char *v8; // r12
const char *v9; // rbp
bool v10; // zf
char delim[2]; // [rsp+Eh] [rbp-2Ah] BYREF

*(_WORD *)delim = 2336;
v1 = *(const char **)(a1 + 8);
v2 = strlen(v1);
v3 = (char *)malloc(v2 + 1);
strcpy(v3, v1);
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");
v6 = determine_command(v5);
*(_DWORD *)(a1 + 4) = v6;
if ( v6 == 7 )
LOBYTE(v6) = simple_error(2LL, 0LL, v5);
v7 = *(_DWORD *)(a1 + 4);
v8 = 0LL;
if ( v7 != 6 )
{
v9 = &v3[strlen(v3) + 1];
v8 = &v9[strlen(v9) + 1];
v10 = strcmp(v8, "\n") == 0;
LOBYTE(v6) = !v10;
if ( v10 || !*v8 )
{
if ( v7 )
LOBYTE(v6) = simple_error(0LL, 0LL, "a command other than 'rem' and 'end' was encountered with no arguments");
}
}
*(_QWORD *)(a1 + 8) = v8;
return v6;
}

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

*(_WORD *)delim = 2336;
v1 = *(const char **)(a1 + 8);
v2 = strlen(v1);
v3 = (char *)malloc(v2 + 1);
strcpy(v3, v1);
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");
v6 = func0(v5);
*(_DWORD *)(a1 + 4) = v6;
if ( v6 == 7 )
LOBYTE(v6) = simple_error(2LL, 0LL, v5);
v7 = *(_DWORD *)(a1 + 4);
v8 = 0LL;
if ( v7 != 6 )
{
v9 = &v3[strlen(v3) + 1];
v8 = &v9[strlen(v9) + 1];
v10 = strcmp(v8, "\n") == 0;
LOBYTE(v6) = !v10;
if ( v10 || !*v8 )
{
if ( v7 )
LOBYTE(v6) = simple_error(0LL, 0LL, "a command other than 'rem' and 'end' was encountered with no arguments");
}
}
*(_QWORD *)(a1 + 8) = v8;
return v6;
}

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]

variable_types:
tokens
char *[3]

variable_types:
str
char *


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

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

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

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 *a1, char **a2, int a3)
{
return strtoull(a1, a2, a3);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(const char *a1, char **a2, int a3)
{
return strtoull(a1, a2, a3);
}

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, __int64 a2)
{
unsigned int v2; // edx
_DWORD *v3; // rax

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

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

if ( *(_DWORD *)(a1 + 4) != *(_DWORD *)(a2 + 4) )
{
console_print("PANIC: ");
console_print("Trying to join with thread of foreign process.");
console_print("\n");
while ( 1 )
;
}
v2 = *(_BYTE *)(a2 + 60) & 2;
if ( (*(_BYTE *)(a2 + 60) & 2) != 0 )
{
console_print("PANIC: ");
console_print("Trying to join with detached thread.");
console_print("\n");
while ( 1 )
;
}
if ( (*(_BYTE *)(a2 + 60) & 1) == 0 )
{
func4(a1);
*(_BYTE *)(a1 + 24) = 1;
v3 = (_DWORD *)heap_alloc(4LL);
*(_QWORD *)(a1 + 32) = v3;
*v3 = *(_DWORD *)a2;
v2 = 1;
}
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 )
{
v2 = v1;
v1 = *(_QWORD *)(v1 + 288);
v3 = *(_BYTE *)(v2 + 60);
if ( (v3 & 1) != 0 )
{
thread_free((unsigned int *)a1, *(_DWORD *)v2);
}
else
{
*(_BYTE *)(v2 + 60) = v3 | 2;
thread_stop((unsigned int *)a1, 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)
{
void *result; // rax

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

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

if ( thread_current )
{
memcpy((void *)(thread_current + 80), a2, 0xB8uLL);
fpu_save(*(_QWORD *)(thread_current + 72));
}
if ( !a1 )
return func1(a2);
if ( !process_current || *(_DWORD *)process_current != *(_DWORD *)(a1 + 4) )
process_current = process_get(*(unsigned int *)(a1 + 4));
thread_current = a1;
if ( memory_space_get() != *(_QWORD *)(process_current + 48) )
memory_space_switch();
memcpy(a2, (const void *)(a1 + 80), 0xB8uLL);
result = (void *)fpu_load(*(_QWORD *)(thread_current + 72));
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 *))
{
void *v6; // r13
unsigned __int64 v7; // rbx
unsigned __int64 v8; // r14
size_t v9; // r15
void *src; // [rsp+10h] [rbp-58h]
void *ptr; // [rsp+20h] [rbp-48h]

if ( a2 > 1 )
{
v6 = malloc(a3);
ptr = malloc(a3);
memcpy(v6, (const void *)(a1 + a3 * ((a2 - 1) >> 1)), a3);
v7 = a2 - 1;
v8 = 0LL;
while ( 1 )
{
while ( v8 < a2 && a4(a1 + a3 * v8, v6) < 0 )
++v8;
if ( !v7 )
break;
v9 = a1 + a3 * v7;
while ( a4(v9, v6) > 0 )
{
v9 -= a3;
if ( !--v7 )
goto LABEL_13;
}
if ( v8 >= v7 )
break;
src = (void *)(a1 + a3 * v8);
if ( a4((size_t)src, (void *)(a1 + v7 * a3)) )
{
memcpy(ptr, (const void *)(a1 + v7 * a3), a3);
memcpy((void *)(a1 + v7 * a3), src, a3);
memcpy(src, ptr, a3);
}
else
{
++v8;
}
}
LABEL_13:
free(v6);
free(ptr);
qsort(a1, v8, a3, a4);
qsort(a3 * (v8 + 1) + a1, a2 - 1 - v8, a3, a4);
}
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1, unsigned __int64 a2, size_t a3, int (__fastcall *a4)(size_t, void *))
{
void *v6; // r13
unsigned __int64 v7; // rbx
unsigned __int64 v8; // r14
size_t v9; // r15
void *src; // [rsp+10h] [rbp-58h]
void *ptr; // [rsp+20h] [rbp-48h]

if ( a2 > 1 )
{
v6 = malloc(a3);
ptr = malloc(a3);
memcpy(v6, (const void *)(a1 + a3 * ((a2 - 1) >> 1)), a3);
v7 = a2 - 1;
v8 = 0LL;
while ( 1 )
{
while ( v8 < a2 && a4(a1 + a3 * v8, v6) < 0 )
++v8;
if ( !v7 )
break;
v9 = a1 + a3 * v7;
while ( a4(v9, v6) > 0 )
{
v9 -= a3;
if ( !--v7 )
goto LABEL_13;
}
if ( v8 >= v7 )
break;
src = (void *)(a1 + a3 * v8);
if ( a4((size_t)src, (void *)(a1 + v7 * a3)) )
{
memcpy(ptr, (const void *)(a1 + v7 * a3), a3);
memcpy((void *)(a1 + v7 * a3), src, a3);
memcpy(src, ptr, a3);
}
else
{
++v8;
}
}
LABEL_13:
free(v6);
free(ptr);
func0(a1, v8, a3, a4);
func0(a3 * (v8 + 1) + a1, a2 - 1 - v8, a3, a4);
}
}

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


sqrt

decompiled_code:
double __fastcall sqrt(double x)
{
double result; // xmm0_8
__m128i v2; // xmm0
float v3; // xmm0_4
long double v4; // fst7
long double v5; // fst7
long double v6; // fst7
long double v7; // fst4
bool v8; // zf

if ( x < 0.0 )
{
_errno[(unsigned int)thread_id()] = 7;
result = 0.0 / 0.0;
}
else
{
result = 1.0 / 0.0;
if ( result != x && -result != x )
{
if ( x == 0.0 )
{
result = 0.0;
}
else
{
v2 = 0LL;
*(float *)v2.m128i_i32 = x;
v3 = *(float *)_sqrt_approx(v2).m128i_i32;
v4 = (v3 + x / v3) * 0.5;
v5 = ((v4 + x / v4) * 0.5 + x / ((v4 + x / v4) * 0.5)) * 0.5;
v6 = (v5 + x / v5) * 0.5;
if ( (x - 1.0 > v6 * v6 || v6 * v6 > x + 1.0) && -1.0 != v6 )
{
do
{
v7 = (x / v6 + v6) * 0.5;
v8 = v7 == v6;
v6 = v7;
}
while ( !v8 );
}
result = v6;
}
}
}
return result;
}

variable_types: x double

variable_types:
y
long double

return_type: double


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; // fst5
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
{
result = 1.0 / 0.0;
if ( a1 != result && -(1.0 / 0.0) != a1 )
{
if ( a1 == 0.0 )
{
result = 0.0;
}
else
{
*(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 func3(long double a1)
{
long double result; // fst7
float v8; // xmm0_4
long double v9; // fst7
long double v10; // fst7
long double v11; // fst5
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
{
result = 1.0 / 0.0;
if ( a1 != result && -(1.0 / 0.0) != a1 )
{
if ( a1 == 0.0 )
{
result = 0.0;
}
else
{
*(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

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;
*(_BYTE *)(thread_current + 24LL) = 2;
*(_QWORD *)(v3 + 32) = v1;
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;
*(_BYTE *)(thread_current + 24LL) = 2;
*(_QWORD *)(v3 + 32) = v1;
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 )
{
result = process_current;
v4 = *(_QWORD *)(process_current + 40);
if ( v4 )
{
while ( *(_BYTE *)(v4 + 24) != 2 || *(_QWORD *)(v4 + 32) != v2 )
{
v4 = *(_QWORD *)(v4 + 288);
if ( !v4 )
goto LABEL_4;
}
*(_BYTE *)(v4 + 24) = 0;
*(_QWORD *)(v4 + 32) = 0LL;
result = thread_thaw(v4, 0LL);
*(_QWORD *)(a1 + 120) = 0LL;
}
else
{
LABEL_4:
*(_BYTE *)v2 = 0;
*(_QWORD *)(a1 + 120) = 0LL;
}
}
else
{
*(_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 )
{
result = process_current;
v4 = *(_QWORD *)(process_current + 40);
if ( v4 )
{
while ( *(_BYTE *)(v4 + 24) != 2 || *(_QWORD *)(v4 + 32) != v2 )
{
v4 = *(_QWORD *)(v4 + 288);
if ( !v4 )
goto LABEL_4;
}
*(_BYTE *)(v4 + 24) = 0;
*(_QWORD *)(v4 + 32) = 0LL;
result = thread_thaw(v4, 0LL);
*(_QWORD *)(a1 + 120) = 0LL;
}
else
{
LABEL_4:
*(_BYTE *)v2 = 0;
*(_QWORD *)(a1 + 120) = 0LL;
}
}
else
{
*(_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

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

decompiled_code:
__int64 __fastcall strcmp(char *a1, char *a2)
{
char v2; // al
char v3; // cl
__int64 v4; // rdx
unsigned int v5; // edx

v2 = *a1;
v3 = *a2;
if ( *a1 == *a2 )
{
v4 = 0LL;
while ( v2 )
{
v2 = a1[++v4];
v3 = a2[v4];
if ( v2 != v3 )
goto LABEL_7;
}
v5 = 0;
}
else
{
LABEL_7:
v5 = -1;
if ( v2 && (v2 >= v3 || !v3) )
v5 = 1;
}
return v5;
}

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 v4; // r12
__int64 (__fastcall *v5)(__int64); // rax
__int64 result; // rax

v4 = 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:
*(_QWORD *)(a1 + 16) = a2;
*(_BYTE *)(a1 + 24) &= 0xFEu;
mutex_unlock(v4);
return 0LL;
}
result = 0xFFFFFFFFLL;
break;
default:
mutex_unlock(v4);
result = 0xFFFFFFFFLL;
break;
}
return result;
}

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

v4 = 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:
*(_QWORD *)(a1 + 16) = a2;
*(_BYTE *)(a1 + 24) &= 0xFEu;
mutex_unlock(v4);
return 0LL;
}
result = 0xFFFFFFFFLL;
break;
default:
mutex_unlock(v4);
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; // rbx
__int64 v2; // rax
__int64 v3; // rax
__int64 v4; // r12
__int64 v5; // rbx
__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; // rbx
__int64 v2; // rax
__int64 v3; // rax
__int64 v4; // r12
__int64 v5; // rbx
__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; }


decompiled_code:
__int64 __fastcall console_put(char a1)
{
__int64 v1; // r8
__int64 v2; // rsi
__int64 result; // rax
__int64 v4; // rbp
__int64 v5; // rbx

if ( a1 == 8 )
{
if ( console_cursor_x )
{
--console_cursor_x;
result = console_put(32LL);
--console_cursor_x;
return result;
}
goto LABEL_4;
}
if ( a1 != 9 )
{
if ( a1 == 10 )
{
console_cursor_x = 0LL;
++console_cursor_y;
LABEL_6:
console_scroll();
return console_update_cursor();
}
LABEL_4:
v1 = console_cursor_y;
v2 = console_cursor_x;
*(_WORD *)(console_videomem + 2 * (console_cursor_x + 80 * console_cursor_y)) = ((console_color_fg & 0xF | (unsigned __int16)(16 * (unsigned __int8)console_color_bg)) << 8) | a1;
if ( (unsigned __int64)(v2 + 1) > 0x4F )
{
console_cursor_x = 0LL;
console_cursor_y = v1 + 1;
}
else
{
console_cursor_x = v2 + 1;
}
goto LABEL_6;
}
result = console_cursor_x & 7;
v4 = result;
if ( (console_cursor_x & 7) != 0 )
{
v5 = 0LL;
do
{
result = console_put(32LL);
++v5;
}
while ( v4 != v5 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(char a1)
{
__int64 v1; // r8
__int64 v2; // rsi
__int64 result; // rax
__int64 v4; // rbp
__int64 v5; // rbx

if ( a1 == 8 )
{
if ( console_cursor_x )
{
--console_cursor_x;
result = func2(32LL);
--console_cursor_x;
return result;
}
goto LABEL_4;
}
if ( a1 != 9 )
{
if ( a1 == 10 )
{
console_cursor_x = 0LL;
++console_cursor_y;
LABEL_6:
func0();
return func1();
}
LABEL_4:
v1 = console_cursor_y;
v2 = console_cursor_x;
*(_WORD *)(console_videomem + 2 * (console_cursor_x + 80 * console_cursor_y)) = ((console_color_fg & 0xF | (unsigned __int16)(16 * (unsigned __int8)console_color_bg)) << 8) | a1;
if ( (unsigned __int64)(v2 + 1) > 0x4F )
{
console_cursor_x = 0LL;
console_cursor_y = v1 + 1;
}
else
{
console_cursor_x = v2 + 1;
}
goto LABEL_6;
}
result = console_cursor_x & 7;
v4 = result;
if ( (console_cursor_x & 7) != 0 )
{
v5 = 0LL;
do
{
result = func2(32LL);
++v5;
}
while ( v4 != v5 );
}
return result;
}

original_code:
void console_put(char c)
{
    if (__builtin_expect(!!(c == 0x08 && console_cursor_x > 0), 0))
    {
        --console_cursor_x;
        console_put(' ');
        --console_cursor_x;
        return;
    }
    else if (__builtin_expect(!!(c == '\t'), 0))
    {
        size_t chars = console_cursor_x % 8;
        size_t i;
        for (i = 0; i < chars; ++i)
            console_put(' ');
        return;
    }
    else if (__builtin_expect(!!(c == '\n'), 0))
    {
        console_cursor_x = 0;
        ++console_cursor_y;
    }
    else
    {
        console_videomem[(console_cursor_y * 80 + console_cursor_x)] =
            (c | (((console_color_bg << 4) | (console_color_fg & 0x0F)) << 8));
        ++console_cursor_x;
        if (console_cursor_x >= 80)
        {
            console_cursor_x = 0;
            ++console_cursor_y;
        }
    }
    _console_scroll();
    _console_update_cursor();
}

canonical_original_code:
void console_put(char c)
{
    if (__builtin_expect(!!(c == 0x08 && console_cursor_x > 0), 0))
    {
        --console_cursor_x;
        console_put(' ');
        --console_cursor_x;
        return;
    }
    else if (__builtin_expect(!!(c == '\t'), 0))
    {
        unsigned long long chars = console_cursor_x % 8;
        unsigned long long i;
        for (i = 0; i < chars; ++i)
            console_put(' ');
        return;
    }
    else if (__builtin_expect(!!(c == '\n'), 0))
    {
        console_cursor_x = 0;
        ++console_cursor_y;
    }
    else
    {
        console_videomem[(console_cursor_y * 80 + console_cursor_x)] =
            (c | (((console_color_bg << 4) | (console_color_fg & 0x0F)) << 8));
        ++console_cursor_x;
        if (console_cursor_x >= 80)
        {
            console_cursor_x = 0;
            ++console_cursor_y;
        }
    }
    _console_scroll();
    _console_update_cursor();
}

variable_types: c char

variable_types:
chars
unsigned long long


decompiled_code:
__int64 __fastcall console_clear(__int64 a1)
{
__int16 v1; // cx
__int64 i; // rax

v1 = ((console_color_fg & 0xF | (unsigned __int16)(16 * (unsigned __int8)console_color_bg)) << 8) | 0x20;
for ( i = 0LL; i != 4000; i += 2LL )
*(_WORD *)(console_videomem + i) = v1;
console_cursor_y = 0LL;
console_cursor_x = 0LL;
return console_update_cursor(a1, &console_videomem);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int16 v1; // cx
__int64 i; // rax

v1 = ((console_color_fg & 0xF | (unsigned __int16)(16 * (unsigned __int8)console_color_bg)) << 8) | 0x20;
for ( i = 0LL; i != 4000; i += 2LL )
*(_WORD *)(console_videomem + i) = v1;
console_cursor_y = 0LL;
console_cursor_x = 0LL;
return func1(a1, &console_videomem);
}

original_code:
void console_clear()
{
    uint16_t cell = (' ' | (((console_color_bg << 4) | (console_color_fg & 0x0F)) << 8));
    size_t i;
    for (i = 0; i < 80 * 25; ++i)
        console_videomem[i] = cell;
    console_cursor_x = console_cursor_y = 0;
    _console_update_cursor();
}

canonical_original_code:
void console_clear()
{
    unsigned short cell = (' ' | (((console_color_bg << 4) | (console_color_fg & 0x0F)) << 8));
    unsigned long long i;
    for (i = 0; i < 80 * 25; ++i)
        console_videomem[i] = cell;
    console_cursor_x = console_cursor_y = 0;
    _console_update_cursor();
}

variable_types:
cell
unsigned short


decompiled_code:
__int64 __fastcall console_set_foreground(unsigned int a1)
{
__int64 result; // rax

result = a1;
console_color_fg = a1;
return result;
}

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

result = a1;
console_color_fg = a1;
return result;
}

original_code:
void console_set_foreground(uint8_t color)
{
    console_color_fg = color;
}

canonical_original_code:
void console_set_foreground(unsigned char color)
{
    console_color_fg = color;
}

variable_types:
color
unsigned char


decompiled_code:
__int64 __fastcall console_set_background(unsigned int a1)
{
__int64 result; // rax

result = a1;
console_color_bg = a1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1)
{
__int64 result; // rax

result = a1;
console_color_bg = a1;
return result;
}

original_code:
void console_set_background(uint8_t color)
{
    console_color_bg = color;
}

canonical_original_code:
void console_set_background(unsigned char color)
{
    console_color_bg = color;
}


canonical_decompiled_code:
char func6()
{
return console_color_fg;
}

original_code:
uint8_t console_get_foreground()
{
    return console_color_fg;
}

canonical_original_code:
unsigned char console_get_foreground()
{
    return console_color_fg;
}


canonical_decompiled_code:
char func7()
{
return console_color_bg;
}

original_code:
uint8_t console_get_background()
{
    return console_color_bg;
}

canonical_original_code:
unsigned char console_get_background()
{
    return console_color_bg;
}


decompiled_code:
__int64 console_get_x()
{
return console_cursor_x;
}

canonical_decompiled_code:
__int64 func8()
{
return console_cursor_x;
}

original_code:
size_t console_get_x()
{
    return console_cursor_x;
}

canonical_original_code:
unsigned long long console_get_x()
{
    return console_cursor_x;
}


decompiled_code:
__int64 console_get_y()
{
return console_cursor_y;
}

canonical_decompiled_code:
__int64 func9()
{
return console_cursor_y;
}

original_code:
size_t console_get_y()
{
    return console_cursor_y;
}

canonical_original_code:
unsigned long long console_get_y()
{
    return console_cursor_y;
}


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

result = a1;
console_cursor_x = a1;
return result;
}

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

result = a1;
console_cursor_x = a1;
return result;
}

original_code:
void console_set_x(size_t x)
{
    console_cursor_x = x;
}

canonical_original_code:
void console_set_x(unsigned long long x)
{
    console_cursor_x = x;
}

variable_types:
x
unsigned long long


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

result = a1;
console_cursor_y = a1;
return result;
}

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

result = a1;
console_cursor_y = a1;
return result;
}

original_code:
void console_set_y(size_t y)
{
    console_cursor_y = y;
}

canonical_original_code:
void console_set_y(unsigned long long y)
{
    console_cursor_y = y;
}

variable_types:
y
unsigned long long


decompiled_code:
__int64 console_get_width()
{
return 80LL;
}

canonical_decompiled_code:
__int64 func12()
{
return 80LL;
}

original_code:
size_t console_get_width()
{
    return 80;
}

canonical_original_code:
unsigned long long console_get_width()
{
    return 80;
}


decompiled_code:
__int64 console_get_height()
{
return 25LL;
}

canonical_decompiled_code:
__int64 func13()
{
return 25LL;
}

original_code:
size_t console_get_height()
{
    return 25;
}

canonical_original_code:
unsigned long long console_get_height()
{
    return 25;
}


decompiled_code:
__int64 __fastcall console_print(char *a1)
{
char *v1; // rbx
char v2; // di
__int64 result; // rax

v1 = a1;
v2 = *a1;
if ( v2 )
{
do
{
result = console_put(v2);
v2 = *++v1;
}
while ( *v1 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func14(char *a1)
{
char *v1; // rbx
char v2; // di
__int64 result; // rax

v1 = a1;
v2 = *a1;
if ( v2 )
{
do
{
result = func2(v2);
v2 = *++v1;
}
while ( *v1 );
}
return result;
}

original_code:
void console_print(const char *str)
{
    char *ptr;
    for (ptr = (char *) str; *ptr; ++ptr)
        console_put(*ptr);
}

canonical_original_code:
void console_print(char *str)
{
    char *ptr;
    for (ptr = (char *) str; *ptr; ++ptr)
        console_put(*ptr);
}

variable_types:
str
char *

variable_types:
ptr
char *


decompiled_code:
__int64 __fastcall console_print_hex(unsigned __int64 a1)
{
unsigned __int64 v1; // rbx
__int64 result; // rax
unsigned int v3; // ecx
char v4; // dl
char *v5; // rbx
char *v6; // rbp
char v7[40]; // [rsp+0h] [rbp-28h] BYREF

v1 = a1;
console_put(48);
console_put(120);
result = 15LL;
if ( !a1 )
return console_put(48);
while ( 1 )
{
v3 = v1 & 0xF;
v4 = v3 + 48;
if ( v3 >= 0xA )
v4 = v3 + 55;
v7[result] = v4;
v1 >>= 4;
if ( !v1 )
break;
--result;
}
if ( (int)result <= 15 )
{
v5 = &v7[(int)result];
v6 = &v7[(int)result + 1 + (unsigned int)(15 - result)];
do
result = console_put(*v5++);
while ( v5 != v6 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func15(unsigned __int64 a1)
{
unsigned __int64 v1; // rbx
__int64 result; // rax
unsigned int v3; // ecx
char v4; // dl
char *v5; // rbx
char *v6; // rbp
char v7[40]; // [rsp+0h] [rbp-28h] BYREF

v1 = a1;
func2(48);
func2(120);
result = 15LL;
if ( !a1 )
return func2(48);
while ( 1 )
{
v3 = v1 & 0xF;
v4 = v3 + 48;
if ( v3 >= 0xA )
v4 = v3 + 55;
v7[result] = v4;
v1 >>= 4;
if ( !v1 )
break;
--result;
}
if ( (int)result <= 15 )
{
v5 = &v7[(int)result];
v6 = &v7[(int)result + 1 + (unsigned int)(15 - result)];
do
result = func2(*v5++);
while ( v5 != v6 );
}
return result;
}

original_code:
void console_print_hex(uintptr_t number)
{
    console_put('0');
    console_put('x');
    if (__builtin_expect(!!(0 == number), 0)) {
        console_put('0');
        return;
    }
    int32_t i = 0;
    int8_t out[16];
    for (i = 15; number != 0; --i) {
        uint32_t digit = number & 0xF;
        number = number >> 4;
        if (digit > 9) {
            out[i] = (int8_t) ('A' + digit - 0xA);
        } else
            out[i] = (int8_t) ('0' + digit);
    }
    for (++i; i < 16; ++i)
        console_put(out[i]);
}

canonical_original_code:
void console_print_hex(unsigned long long number)
{
    console_put('0');
    console_put('x');
    if (__builtin_expect(!!(0 == number), 0)) {
        console_put('0');
        return;
    }
    int i = 0;
    char out[16];
    for (i = 15; number != 0; --i) {
        unsigned int digit = number & 0xF;
        number = number >> 4;
        if (digit > 9) {
            out[i] = (char) ('A' + digit - 0xA);
        } else
            out[i] = (char) ('0' + digit);
    }
    for (++i; i < 16; ++i)
        console_put(out[i]);
}

variable_types:
number
unsigned long long

variable_types: i int

variable_types:
out
char [16]

variable_types:
digit
unsigned int


decompiled_code:
__int64 __fastcall console_print_dec(__int64 a1)
{
__int64 v1; // rbx
unsigned int v2; // ecx
__int64 v3; // rdx
__int64 result; // rax
char *v5; // rbx
char *v6; // rbp
char v7[40]; // [rsp+0h] [rbp-28h] BYREF

if ( !a1 )
return console_put(48);
v1 = a1;
if ( a1 < 0 )
{
console_put(45);
v1 = -a1;
}
v2 = 15;
do
{
v3 = v1 / 10;
v1 %= 10LL;
v7[v2] = v3 + 48;
result = v2--;
}
while ( v1 );
if ( (unsigned int)result <= 0xF )
{
v5 = &v7[(unsigned int)result];
v6 = &v7[(unsigned int)result + 1 + (unsigned int)(15 - result)];
do
result = console_put(*v5++);
while ( v6 != v5 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(__int64 a1)
{
__int64 v1; // rbx
unsigned int v2; // ecx
__int64 v3; // rdx
__int64 result; // rax
char *v5; // rbx
char *v6; // rbp
char v7[40]; // [rsp+0h] [rbp-28h] BYREF

if ( !a1 )
return func2(48);
v1 = a1;
if ( a1 < 0 )
{
func2(45);
v1 = -a1;
}
v2 = 15;
do
{
v3 = v1 / 10;
v1 %= 10LL;
v7[v2] = v3 + 48;
result = v2--;
}
while ( v1 );
if ( (unsigned int)result <= 0xF )
{
v5 = &v7[(unsigned int)result];
v6 = &v7[(unsigned int)result + 1 + (unsigned int)(15 - result)];
do
result = func2(*v5++);
while ( v6 != v5 );
}
return result;
}

original_code:
void console_print_dec(intptr_t number)
{
    if (__builtin_expect(!!(0 == number), 0)) {
        console_put('0');
        return;
    }
    if (__builtin_expect(!!(number < 0), 0)) {
        console_put('-');
        number = -number;
    }
    uint32_t i = 0;
    uint8_t out[16];
    for (i = 15; number != 0; --i) {
        uint32_t digit = number / 10;
        number = number % 10;
        out[i] = (int8_t) ('0' + digit);
    }
    for (++i; i < 16; ++i)
        console_put(out[i]);
}

canonical_original_code:
void console_print_dec(long long number)
{
    if (__builtin_expect(!!(0 == number), 0)) {
        console_put('0');
        return;
    }
    if (__builtin_expect(!!(number < 0), 0)) {
        console_put('-');
        number = -number;
    }
    unsigned int i = 0;
    unsigned char out[16];
    for (i = 15; number != 0; --i) {
        unsigned int digit = number / 10;
        number = number % 10;
        out[i] = (char) ('0' + digit);
    }
    for (++i; i < 16; ++i)
        console_put(out[i]);
}

variable_types:
number
long long

variable_types:
i
unsigned int

variable_types:
out
unsigned char [16]


decompiled_code:
unsigned __int64 __fastcall stack_create(_QWORD *a1, __int64 a2)
{
__int64 v2; // r12
unsigned __int64 result; // rax

*a1 = *(_QWORD *)(a2 + 56) - 0x17FFFE01000LL;
*(_QWORD *)(a2 + 56) += 2093056LL;
a1[1] = 0LL;
if ( *a1 > 0xFFFFFEFFFFFFFFFFLL )
{
console_print("PANIC: ");
console_print("Exceeded maximum number of stacks per process.");
console_print("\n");
while ( 1 )
;
}
v2 = memory_space_get();
if ( *(_QWORD *)(a2 + 48) != v2 )
((void (*)(void))memory_space_switch)();
result = stack_resize(a1, 0x1000uLL, a2);
if ( *(_QWORD *)(a2 + 48) != v2 )
result = memory_space_switch(v2);
return result;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func1(_QWORD *a1, __int64 a2)
{
__int64 v2; // r12
unsigned __int64 result; // rax

*a1 = *(_QWORD *)(a2 + 56) - 0x17FFFE01000LL;
*(_QWORD *)(a2 + 56) += 2093056LL;
a1[1] = 0LL;
if ( *a1 > 0xFFFFFEFFFFFFFFFFLL )
{
console_print("PANIC: ");
console_print("Exceeded maximum number of stacks per process.");
console_print("\n");
while ( 1 )
;
}
v2 = memory_space_get();
if ( *(_QWORD *)(a2 + 48) != v2 )
((void (*)(void))memory_space_switch)();
result = func0(a1, 0x1000uLL, a2);
if ( *(_QWORD *)(a2 + 48) != v2 )
result = memory_space_switch(v2);
return result;
}

original_code:
void stack_create(stack_t *stack, process_t *process) {
    stack->address = 0xFFFFFE8000000000 + process->stack_offset + (0x200000 - 0x1000);
    process->stack_offset += (0x200000 - 0x1000);
    stack->length = 0;
    if (__builtin_expect(!!(stack->address >= 0xFFFFFE8000000000 + 0x8000000000), 0))
        { console_print("PANIC: "); console_print("Exceeded maximum number of stacks per process."); console_print("\n"); while (1); };
    uintptr_t old_addr_space = memory_space_get();
    if (old_addr_space != process->addr_space)
        memory_space_switch(process->addr_space);
    stack_resize(stack, 0x1000, process);
    if (old_addr_space != process->addr_space)
        memory_space_switch(old_addr_space);
}

canonical_original_code:
void stack_create(struct stack_t *stack, struct process_t *process) {
    stack->address = 0xFFFFFE8000000000 + process->stack_offset + (0x200000 - 0x1000);
    process->stack_offset += (0x200000 - 0x1000);
    stack->length = 0;
    if (__builtin_expect(!!(stack->address >= 0xFFFFFE8000000000 + 0x8000000000), 0))
        { console_print("PANIC: "); console_print("Exceeded maximum number of stacks per process."); console_print("\n"); while (1); };
    unsigned long long old_addr_space = memory_space_get();
    if (old_addr_space != process->addr_space)
        memory_space_switch(process->addr_space);
    stack_resize(stack, 0x1000, process);
    if (old_addr_space != process->addr_space)
        memory_space_switch(old_addr_space);
}

variable_types: struct stack_t { unsigned long long address; unsigned long long length; } *

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


decompiled_code:
unsigned __int64 __fastcall stack_dispose(_QWORD *a1, __int64 a2)
{
return stack_resize(a1, 0LL, a2);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func2(_QWORD *a1, __int64 a2)
{
return func0(a1, 0LL, a2);
}

original_code:
void stack_dispose(stack_t *stack, process_t *process) {
    stack_resize(stack, 0, process);
}

canonical_original_code:
void stack_dispose(struct stack_t *stack, struct process_t *process) {
    stack_resize(stack, 0, process);
}


fread

decompiled_code:
unsigned __int64 __fastcall fread(__int64 a1, unsigned __int64 a2, __int64 a3, __int64 a4)
{
__int64 v7; // r13
unsigned __int64 v8; // rsi
unsigned __int64 v9; // rax
unsigned __int64 v10; // rdx
unsigned __int64 v11; // r12

v7 = a4 + 8;
mutex_lock(a4 + 8);
v8 = a2 * a3;
v9 = _stdio_read(a4, v8, a1);
v10 = v9 % a2;
v11 = v9 / a2;
if ( v8 > v9 )
*(_BYTE *)(a4 + 24) |= 1u;
mutex_unlock(v7, v8, v10);
return v11;
}

canonical_decompiled_code:
unsigned __int64 __fastcall func0(__int64 a1, unsigned __int64 a2, __int64 a3, __int64 a4)
{
__int64 v7; // r13
unsigned __int64 v8; // rsi
unsigned __int64 v9; // rax
unsigned __int64 v10; // rdx
unsigned __int64 v11; // r12

v7 = a4 + 8;
mutex_lock(a4 + 8);
v8 = a2 * a3;
v9 = _stdio_read(a4, v8, a1);
v10 = v9 % a2;
v11 = v9 / a2;
if ( v8 > v9 )
*(_BYTE *)(a4 + 24) |= 1u;
mutex_unlock(v7, v8, v10);
return v11;
}

original_code:
size_t fread(void *ptr, size_t size, size_t count, FILE *stream) {
    mutex_lock(&stream->lock);
    size_t length = size * count;
    size_t bytes_read = __stdio_read(stream, length, ptr);
    size_t blocks_read = bytes_read / size;
    if (bytes_read < length)
        stream->flags |= (1 << 0);
    mutex_unlock(&stream->lock);
    return blocks_read;
}

canonical_original_code:
unsigned long long fread(void *ptr, unsigned long long size, unsigned long long count, struct __file *stream) {
    mutex_lock(&stream->lock);
    unsigned long long length = size * count;
    unsigned long long bytes_read = __stdio_read(stream, length, ptr);
    unsigned long long blocks_read = bytes_read / size;
    if (bytes_read < length)
        stream->flags |= (1 << 0);
    mutex_unlock(&stream->lock);
    return blocks_read;
}

variable_types:
ptr
void *

variable_types:
size
unsigned long long

variable_types:
count
unsigned long long

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:
length
unsigned long long