decompiled_code:
char *snake_update()
{
int v0; // edi
int v1; // ecx
char *result; // rax
int v3; // eax
__int64 i; // rcx
__int64 j; // rax
__int64 v6; // rsi
int k; // ecx
int *v8; // rdx
__int64 v9; // rax
__int64 v10; // rdx
char v11; // si
int v12; // eax
unsigned int v13; // edi
int v14; // esi
_QWORD *v15; // rax
int v16; // ebx
_QWORD *v17; // rbp
_QWORD *v18; // rax
int v19; // eax
unsigned int v20; // edi
int v21; // esi

v0 = HIDWORD(snake_head);
v1 = snake_head;
if ( snake_direction == 2 )
{
v1 = snake_head - 1;
LODWORD(snake_head) = snake_head - 1;
}
else if ( snake_direction > 2 )
{
if ( snake_direction == 3 )
{
v1 = snake_head + 1;
LODWORD(snake_head) = snake_head + 1;
}
}
else if ( snake_direction )
{
if ( snake_direction == 1 )
v0 = ++HIDWORD(snake_head);
}
else
{
v0 = --HIDWORD(snake_head);
}
if ( snake_len <= 1 )
{
LABEL_14:
if ( (unsigned int)v0 <= 0x13 )
{
if ( (unsigned int)v1 <= 0x1D )
{
LABEL_20:
if ( __PAIR64__(HIDWORD(snake_head), v1) == food )
{
++score;
v15 = realloc(snake_body, 8LL * (snake_len + 1));
v16 = snake_len;
v17 = v15;
snake_body = v15;
v18 = malloc(8uLL);
v17[v16] = v18;
*v18 = -1LL;
snake_len = v16 + 1;
do
{
do
{
LODWORD(food) = snake_rand(0, 29);
v19 = snake_rand(0, 19);
v20 = food;
v21 = v19;
HIDWORD(food) = v19;
}
while ( (unsigned int)in_snake(food, v19) );
v3 = bonus;
}
while ( __PAIR64__(v21, v20) == bonus );
v1 = snake_head;
}
else
{
v3 = bonus;
}
if ( v1 == v3 && HIDWORD(snake_head) == HIDWORD(bonus) )
{
score += 10;
bonus = -1LL;
timer = -1;
}
else if ( v3 >= 0 && bonus >= 0 )
{
if ( !--timer )
{
bonus = -1LL;
timer = -1;
}
goto LABEL_27;
}
if ( (unsigned int)snake_rand(0, 400) == 200 )
{
do
{
do
{
LODWORD(bonus) = snake_rand(0, 29);
v12 = snake_rand(0, 19);
v13 = bonus;
v14 = v12;
HIDWORD(bonus) = v12;
}
while ( (unsigned int)in_snake(bonus, v12) );
}
while ( __PAIR64__(v14, v13) == food );
timer = snake_rand(30, 120);
shift_snake();
i = 0LL;
goto LABEL_28;
}
LABEL_27:
shift_snake();
for ( i = 0LL; i != 160; i += 8LL )
{
LABEL_28:
for ( j = 0LL; j != 30; ++j )
*(_BYTE *)(*(_QWORD *)((char *)game_state + i) + j) = 32;
}
v6 = 8LL;
for ( k = 1; snake_len > k; v6 += 8LL )
{
v8 = *(int **)((char *)snake_body + v6);
v9 = *v8;
v10 = v8[1];
if ( (int)v9 >= 0 && (int)v10 >= 0 )
*(_BYTE *)(*((_QWORD *)game_state + v10) + v9) = 42;
++k;
}
if ( (int)food >= 0 && food >= 0 )
*(_BYTE *)(*((_QWORD *)game_state + SHIDWORD(food)) + (int)food) = 64;
if ( (int)bonus >= 0 && bonus >= 0 )
*(_BYTE *)(*((_QWORD *)game_state + SHIDWORD(bonus)) + (int)bonus) = 36;
result = (char *)(int)snake_head;
if ( (int)snake_head >= 0 && snake_head >= 0 )
{
v11 = 88;
if ( (unsigned int)snake_direction <= 3 )
v11 = CSWTCH_87[snake_direction];
*(_BYTE *)(*((_QWORD *)game_state + SHIDWORD(snake_head)) + (int)snake_head) = v11;
}
return result;
}
if ( v1 >= 0 )
{
v1 %= 30;
goto LABEL_19;
}
v1 = 29;
}
else
{
if ( v1 < 0 )
v1 = 29;
else
v1 %= 30;
if ( v0 < 0 )
{
v0 = 19;
LABEL_19:
LODWORD(snake_head) = v1;
HIDWORD(snake_head) = v0;
goto LABEL_20;
}
}
v0 %= 20;
goto LABEL_19;
}
result = (char *)snake_body + 8;
while ( **(_DWORD **)result != v1 || *(_DWORD *)(*(_QWORD *)result + 4LL) != v0 )
{
result += 8;
if ( (char *)snake_body + 8 * (unsigned int)(snake_len - 2) + 16 == result )
goto LABEL_14;
}
quit = 1;
return result;
}

canonical_decompiled_code:
char *func13()
{
int v0; // edi
int v1; // ecx
char *result; // rax
int v3; // eax
__int64 i; // rcx
__int64 j; // rax
__int64 v6; // rsi
int k; // ecx
int *v8; // rdx
__int64 v9; // rax
__int64 v10; // rdx
char v11; // si
int v12; // eax
unsigned int v13; // edi
int v14; // esi
_QWORD *v15; // rax
int v16; // ebx
_QWORD *v17; // rbp
_QWORD *v18; // rax
int v19; // eax
unsigned int v20; // edi
int v21; // esi

v0 = HIDWORD(snake_head);
v1 = snake_head;
if ( snake_direction == 2 )
{
v1 = snake_head - 1;
LODWORD(snake_head) = snake_head - 1;
}
else if ( snake_direction > 2 )
{
if ( snake_direction == 3 )
{
v1 = snake_head + 1;
LODWORD(snake_head) = snake_head + 1;
}
}
else if ( snake_direction )
{
if ( snake_direction == 1 )
v0 = ++HIDWORD(snake_head);
}
else
{
v0 = --HIDWORD(snake_head);
}
if ( snake_len <= 1 )
{
LABEL_14:
if ( (unsigned int)v0 <= 0x13 )
{
if ( (unsigned int)v1 <= 0x1D )
{
LABEL_20:
if ( __PAIR64__(HIDWORD(snake_head), v1) == food )
{
++score;
v15 = realloc(snake_body, 8LL * (snake_len + 1));
v16 = snake_len;
v17 = v15;
snake_body = v15;
v18 = malloc(8uLL);
v17[v16] = v18;
*v18 = -1LL;
snake_len = v16 + 1;
do
{
do
{
LODWORD(food) = func8(0, 29);
v19 = func8(0, 19);
v20 = food;
v21 = v19;
HIDWORD(food) = v19;
}
while ( (unsigned int)func12(food, v19) );
v3 = bonus;
}
while ( __PAIR64__(v21, v20) == bonus );
v1 = snake_head;
}
else
{
v3 = bonus;
}
if ( v1 == v3 && HIDWORD(snake_head) == HIDWORD(bonus) )
{
score += 10;
bonus = -1LL;
timer = -1;
}
else if ( v3 >= 0 && bonus >= 0 )
{
if ( !--timer )
{
bonus = -1LL;
timer = -1;
}
goto LABEL_27;
}
if ( (unsigned int)func8(0, 400) == 200 )
{
do
{
do
{
LODWORD(bonus) = func8(0, 29);
v12 = func8(0, 19);
v13 = bonus;
v14 = v12;
HIDWORD(bonus) = v12;
}
while ( (unsigned int)func12(bonus, v12) );
}
while ( __PAIR64__(v14, v13) == food );
timer = func8(30, 120);
func11();
i = 0LL;
goto LABEL_28;
}
LABEL_27:
func11();
for ( i = 0LL; i != 160; i += 8LL )
{
LABEL_28:
for ( j = 0LL; j != 30; ++j )
*(_BYTE *)(*(_QWORD *)((char *)game_state + i) + j) = 32;
}
v6 = 8LL;
for ( k = 1; snake_len > k; v6 += 8LL )
{
v8 = *(int **)((char *)snake_body + v6);
v9 = *v8;
v10 = v8[1];
if ( (int)v9 >= 0 && (int)v10 >= 0 )
*(_BYTE *)(*((_QWORD *)game_state + v10) + v9) = 42;
++k;
}
if ( (int)food >= 0 && food >= 0 )
*(_BYTE *)(*((_QWORD *)game_state + SHIDWORD(food)) + (int)food) = 64;
if ( (int)bonus >= 0 && bonus >= 0 )
*(_BYTE *)(*((_QWORD *)game_state + SHIDWORD(bonus)) + (int)bonus) = 36;
result = (char *)(int)snake_head;
if ( (int)snake_head >= 0 && snake_head >= 0 )
{
v11 = 88;
if ( (unsigned int)snake_direction <= 3 )
v11 = CSWTCH_87[snake_direction];
*(_BYTE *)(*((_QWORD *)game_state + SHIDWORD(snake_head)) + (int)snake_head) = v11;
}
return result;
}
if ( v1 >= 0 )
{
v1 %= 30;
goto LABEL_19;
}
v1 = 29;
}
else
{
if ( v1 < 0 )
v1 = 29;
else
v1 %= 30;
if ( v0 < 0 )
{
v0 = 19;
LABEL_19:
LODWORD(snake_head) = v1;
HIDWORD(snake_head) = v0;
goto LABEL_20;
}
}
v0 %= 20;
goto LABEL_19;
}
result = (char *)snake_body + 8;
while ( **(_DWORD **)result != v1 || *(_DWORD *)(*(_QWORD *)result + 4LL) != v0 )
{
result += 8;
if ( (char *)snake_body + 8 * (unsigned int)(snake_len - 2) + 16 == result )
goto LABEL_14;
}
quit = 1;
return result;
}

original_code:
void snake_update(void) {
 switch(snake_direction) {
  case up:
   snake_head[1]--;
   break;
  case down:
   snake_head[1]++;
   break;
  case left:
   snake_head[0]--;
   break;
  case right:
   snake_head[0]++;
   break;
 }
 int i;
 for(i = 1; i < snake_len; i++) {
  if(snake_head[0] == snake_body[i][0] && snake_head[1] == snake_body[i][1]) {
   quit = 1;
   return;
  }
 }
 if(snake_head[1] < 0 || snake_head[1] >= 20 ||
    snake_head[0] < 0 || snake_head[0] >= 30) {
  if(1) {
   snake_head[0] = (snake_head[0] < 0) ? 30 - 1 : snake_head[0];
   snake_head[1] = (snake_head[1] < 0) ? 20 - 1 : snake_head[1];
   snake_head[0] %= 30;
   snake_head[1] %= 20;
  } else {
   quit = 1;
   return;
  }
 }
 if(snake_head[0] == food[0] && snake_head[1] == food[1]) {
  score += 1;
  snake_body = realloc(snake_body, (snake_len + 1) * sizeof(int *));
  snake_body[snake_len] = malloc(2 * sizeof(int));
  snake_body[snake_len][0] = -1;
  snake_body[snake_len][1] = -1;
  snake_len++;
  do {
   food[0] = snake_rand(0, 30 - 1);
   food[1] = snake_rand(0, 20 - 1);
  } while(in_snake(food[0], food[1]) || (food[0] == bonus[0] && food[1] == bonus[1]));
 }
 if(snake_head[0] == bonus[0] && snake_head[1] == bonus[1]) {
  score += 10;
  bonus[0] = -1;
  bonus[1] = -1;
  timer = -1;
 }
 if(bonus[0] >= 0 && bonus[1] >= 0) {
  timer--;
  if(!timer) {
   bonus[0] = -1;
   bonus[1] = -1;
   timer = -1;
  }
 } else {
  if(snake_rand(0, 400) == 400 / 2) {
   do {
    bonus[0] = snake_rand(0, 30 - 1);
    bonus[1] = snake_rand(0, 20 - 1);
   } while(in_snake(bonus[0], bonus[1]) || (bonus[0] == food[0] && bonus[1] == food[1]));
   timer = snake_rand(30, 120);
  }
 }
 shift_snake();
 int x, y;
 for(y = 0; y < 20; y++) {
  for(x = 0; x < 30; x++) {
   game_state[y][x] = ' ';
  }
 }
 for(i = 1; i < snake_len; i++) {
  x = snake_body[i][0];
  y = snake_body[i][1];
  if(x < 0 || y < 0)
   continue;
  game_state[y][x] = '*';
 }
 x = food[0];
 y = food[1];
 if(x >= 0 && y >= 0)
  game_state[y][x] = '@';
 x = bonus[0];
 y = bonus[1];
 if(x >= 0 && y >= 0)
  game_state[y][x] = '$';
 x = snake_head[0];
 y = snake_head[1];
 if(x >= 0 && y >= 0) {
  char head = 'X';
  switch(snake_direction) {
   case up:
    head = 'v';
    break;
   case down:
    head = '^';
    break;
   case left:
    head = '>';
    break;
   case right:
    head = '<';
    break;
  }
  game_state[y][x] = head;
 }
}

canonical_original_code:
void snake_update() {
 switch(snake_direction) {
  case up:
   snake_head[1]--;
   break;
  case down:
   snake_head[1]++;
   break;
  case left:
   snake_head[0]--;
   break;
  case right:
   snake_head[0]++;
   break;
 }
 int i;
 for(i = 1; i < snake_len; i++) {
  if(snake_head[0] == snake_body[i][0] && snake_head[1] == snake_body[i][1]) {
   quit = 1;
   return;
  }
 }
 if(snake_head[1] < 0 || snake_head[1] >= 20 ||
    snake_head[0] < 0 || snake_head[0] >= 30) {
  if(1) {
   snake_head[0] = (snake_head[0] < 0) ? 30 - 1 : snake_head[0];
   snake_head[1] = (snake_head[1] < 0) ? 20 - 1 : snake_head[1];
   snake_head[0] %= 30;
   snake_head[1] %= 20;
  } else {
   quit = 1;
   return;
  }
 }
 if(snake_head[0] == food[0] && snake_head[1] == food[1]) {
  score += 1;
  snake_body = realloc(snake_body, (snake_len + 1) * sizeof(int *));
  snake_body[snake_len] = malloc(2 * sizeof(int));
  snake_body[snake_len][0] = -1;
  snake_body[snake_len][1] = -1;
  snake_len++;
  do {
   food[0] = snake_rand(0, 30 - 1);
   food[1] = snake_rand(0, 20 - 1);
  } while(in_snake(food[0], food[1]) || (food[0] == bonus[0] && food[1] == bonus[1]));
 }
 if(snake_head[0] == bonus[0] && snake_head[1] == bonus[1]) {
  score += 10;
  bonus[0] = -1;
  bonus[1] = -1;
  timer = -1;
 }
 if(bonus[0] >= 0 && bonus[1] >= 0) {
  timer--;
  if(!timer) {
   bonus[0] = -1;
   bonus[1] = -1;
   timer = -1;
  }
 } else {
  if(snake_rand(0, 400) == 400 / 2) {
   do {
    bonus[0] = snake_rand(0, 30 - 1);
    bonus[1] = snake_rand(0, 20 - 1);
   } while(in_snake(bonus[0], bonus[1]) || (bonus[0] == food[0] && bonus[1] == food[1]));
   timer = snake_rand(30, 120);
  }
 }
 shift_snake();
 int x; int y;
 for(y = 0; y < 20; y++) {
  for(x = 0; x < 30; x++) {
   game_state[y][x] = ' ';
  }
 }
 for(i = 1; i < snake_len; i++) {
  x = snake_body[i][0];
  y = snake_body[i][1];
  if(x < 0 || y < 0)
   continue;
  game_state[y][x] = '*';
 }
 x = food[0];
 y = food[1];
 if(x >= 0 && y >= 0)
  game_state[y][x] = '@';
 x = bonus[0];
 y = bonus[1];
 if(x >= 0 && y >= 0)
  game_state[y][x] = '$';
 x = snake_head[0];
 y = snake_head[1];
 if(x >= 0 && y >= 0) {
  char head = 'X';
  switch(snake_direction) {
   case up:
    head = 'v';
    break;
   case down:
    head = '^';
    break;
   case left:
    head = '>';
    break;
   case right:
    head = '<';
    break;
  }
  game_state[y][x] = head;
 }
}

variable_types: head char


repo: cyphar/ascii-snake

decompiled_code:
int clr_line()
{
printf("\x1B[s");
printf("%*s", 30, "");
printf("\x1B[u");
return fflush(stdout);
}

canonical_decompiled_code:
int func14()
{
printf("\x1B[s");
printf("%*s", 30, "");
printf("\x1B[u");
return fflush(stdout);
}

original_code:
void clr_line(void) {
 printf("\x1b[s");
 printf("%*s", 30, "");
 printf("\x1b[u");
 fflush(stdout);
}

canonical_original_code:
void clr_line() {
 printf("\x1b[s");
 printf("%*s", 30, "");
 printf("\x1b[u");
 fflush(stdout);
}

return_type: void


decompiled_code:
int snake_redraw()
{
__int64 v0; // rbx
const char *v1; // rdx

v0 = 0LL;
printf("\x1B[H");
fflush(stdout);
puts(border);
do
{
v1 = *(const char **)((char *)game_state + v0);
v0 += 8LL;
printf("%c%s%c\n", 124LL, v1, 124LL);
}
while ( v0 != 160 );
puts(border);
clr_line();
printf("\nScore: \x1B[1;34m%d\x1B[0m\n", (unsigned int)score);
if ( timer >= 0 )
{
clr_line();
printf("Timer: \x1B[1;33m%d\x1B[0m\n", (unsigned int)timer);
}
return clr_line();
}

canonical_decompiled_code:
int func15()
{
__int64 v0; // rbx
const char *v1; // rdx

v0 = 0LL;
printf("\x1B[H");
fflush(stdout);
puts(border);
do
{
v1 = *(const char **)((char *)game_state + v0);
v0 += 8LL;
printf("%c%s%c\n", 124LL, v1, 124LL);
}
while ( v0 != 160 );
puts(border);
func14();
printf("\nScore: \x1B[1;34m%d\x1B[0m\n", (unsigned int)score);
if ( timer >= 0 )
{
func14();
printf("Timer: \x1B[1;33m%d\x1B[0m\n", (unsigned int)timer);
}
return func14();
}

original_code:
void snake_redraw(void) {
 printf("\x1b[H");
 fflush(stdout);
 printf("%s\n", border);
 int i;
 for(i = 0; i < 20; i++)
  printf("%c%s%c\n", '|', game_state[i], '|');
 printf("%s\n", border);
 clr_line();
 printf("\n" "Score: " "\x1b[1;34m" "%d" "\x1b[0m" "\n", score);
 if(timer >= 0) {
  clr_line();
  printf("Timer: " "\x1b[1;33m" "%d" "\x1b[0m" "\n", timer);
 }
 clr_line();
}

canonical_original_code:
void snake_redraw() {
 printf("\x1b[H");
 fflush(stdout);
 printf("%s\n", border);
 int i;
 for(i = 0; i < 20; i++)
  printf("%c%s%c\n", '|', game_state[i], '|');
 printf("%s\n", border);
 clr_line();
 printf("\n" "Score: " "\x1b[1;34m" "%d" "\x1b[0m" "\n", score);
 if(timer >= 0) {
  clr_line();
  printf("Timer: " "\x1b[1;33m" "%d" "\x1b[0m" "\n", timer);
 }
 clr_line();
}

variable_types: i int


original_code:
int snake_end_state(void) {
 int x, y;
 for(y = 0; y < 20; y++)
  for(x = 0; x < 30; x++)
   if(!(game_state[y][x] == '*' || game_state[y][x] == 'v' || game_state[y][x] == '^' || game_state[y][x] == '>' || game_state[y][x] == '<'))
    return loss;
 return win;
}

canonical_original_code:
int snake_end_state() {
 int x; int y;
 for(y = 0; y < 20; y++)
  for(x = 0; x < 30; x++)
   if(!(game_state[y][x] == '*' || game_state[y][x] == 'v' || game_state[y][x] == '^' || game_state[y][x] == '>' || game_state[y][x] == '<'))
    return loss;
 return win;
}

variable_types: x int

variable_types: y int

return_type: int


decompiled_code:
int __fastcall move_cursor(unsigned int a1, unsigned int a2)
{
printf("\x1B[%d;%df", a1, a2);
return fflush(stdout);
}

canonical_decompiled_code:
int __fastcall func17(unsigned int a1, unsigned int a2)
{
printf("\x1B[%d;%df", a1, a2);
return fflush(stdout);
}

original_code:
void move_cursor(int row, int column) {
 printf("\x1b[%d;%df", row, column);
 fflush(stdout);
}

variable_types: row int

variable_types: column int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
int v5; // [rsp+8h] [rbp-10h] BYREF
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

con_init(argc, argv, envp);
con_hideCursor();
init_colors();
con_getMaxXY(&v5, v6);
field_x = 5;
field_y = 3;
field_width = v5 - 8;
field_height = v6[0] - 6;
if ( v5 - 8 <= 2 )
__assert_fail("field_width > 2", "example.c", 0x79u, "main");
if ( v6[0] - 6 <= 2 )
__assert_fail("field_height > 2", "example.c", 0x7Au, "main");
initial_draw();
do
{
while ( !(unsigned int)con_keyPressed() )
;
v3 = con_getKey();
}
while ( !(unsigned int)process_key(v3) );
con_clearScr();
con_deinit();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
int v5; // [rsp+8h] [rbp-10h] BYREF
int v6[3]; // [rsp+Ch] [rbp-Ch] BYREF

func13(argc, argv, envp);
func14();
func9();
func17(&v5, v6);
field_x = 5;
field_y = 3;
field_width = v5 - 8;
field_height = v6[0] - 6;
if ( v5 - 8 <= 2 )
__assert_fail("field_width > 2", "example.c", 0x79u, "main");
if ( v6[0] - 6 <= 2 )
__assert_fail("field_height > 2", "example.c", 0x7Au, "main");
func8();
do
{
while ( !(unsigned int)func20() )
;
v3 = func21();
}
while ( !(unsigned int)func10(v3) );
func18();
func11();
return 0;
}

original_code:
int main() {
    int quit = 0;
    int max_x, max_y;
    con_init();
    con_hideCursor();
    init_colors();
    con_getMaxXY(&max_x, &max_y);
    field_x = 3 + 1 + 1;
    field_y = 3;
    field_width = max_x - field_x - 3;
    field_height = max_y - field_y - 3;
    ((void) sizeof ((field_width > 2) ? 1 : 0), __extension__ ({ if (field_width > 2) ; else __assert_fail ("field_width > 2", "example.c", 121, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((field_height > 2) ? 1 : 0), __extension__ ({ if (field_height > 2) ; else __assert_fail ("field_height > 2", "example.c", 122, __extension__ __PRETTY_FUNCTION__); }));
    initial_draw();
    while (!quit) {
        if (con_keyPressed()) {
            if (process_key(con_getKey())) {
                quit = 1;
            }
        }
    }
    con_clearScr();
    con_deinit();
    return 0;
}

canonical_original_code:
int main() {
    int quit = 0;
    int max_x; int max_y;
    con_init();
    con_hideCursor();
    init_colors();
    con_getMaxXY(&max_x, &max_y);
    field_x = 3 + 1 + 1;
    field_y = 3;
    field_width = max_x - field_x - 3;
    field_height = max_y - field_y - 3;
    ((void) sizeof ((field_width > 2) ? 1 : 0), __extension__ ({ if (field_width > 2) ; else __assert_fail ("field_width > 2", "example.c", 121, __extension__ __PRETTY_FUNCTION__); }));
    ((void) sizeof ((field_height > 2) ? 1 : 0), __extension__ ({ if (field_height > 2) ; else __assert_fail ("field_height > 2", "example.c", 122, __extension__ __PRETTY_FUNCTION__); }));
    initial_draw();
    while (!quit) {
        if (con_keyPressed()) {
            if (process_key(con_getKey())) {
                quit = 1;
            }
        }
    }
    con_clearScr();
    con_deinit();
    return 0;
}

variable_types: quit int


decompiled_code:
__int64 __fastcall con_charAt(__int64 a1, unsigned int a2, int a3, unsigned int a4, __int64 a5, __int64 a6, char a7)
{
__int64 v8; // rsi

v8 = a4;
con_gotoXY(a3);
con_setColor(a2, v8);
return con_outTxt("%c", a7);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, unsigned int a2, int a3, unsigned int a4, __int64 a5, __int64 a6, char a7)
{
__int64 v8; // rsi

v8 = a4;
func15(a3);
func23(a2, v8);
return func19("%c", a7);
}

original_code:
static void con_charAt(int ch, int color, int x, int y) {
    con_gotoXY(x, y);
    con_setColor(color);
    con_outTxt("%c", ch);
}

variable_types: ch int

variable_types: color int


decompiled_code:
__int64 __fastcall initial_draw(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, char a7)
{
__int64 v7; // r8
__int64 v8; // r9
int v9; // edx
int v10; // eax
int v11; // ebp
int v12; // ebx
int v13; // eax
unsigned int v14; // esi
__int64 v15; // rdi
unsigned int v16; // ecx
char v18; // [rsp+0h] [rbp-18h]
char v19; // [rsp+0h] [rbp-18h]

con_clearScr();
con_gotoXY(3);
con_outTxt("Use arrows to move point, use Esc to exit.", v18);
v9 = field_width;
v10 = field_height;
if ( field_width > 0 )
{
v11 = 0;
do
{
v12 = 0;
if ( v10 > 0 )
{
do
{
if ( v11 && field_width - 1 != v11 && v12 )
{
v13 = v10 - 1;
v14 = (v13 != v12) + 1;
v15 = 3 * (unsigned int)(v13 == v12) + 32;
}
else
{
v14 = 1;
v15 = 35LL;
}
v16 = v12 + field_y;
++v12;
con_charAt(v15, v14, v11 + field_x, v16, v7, v8, v19);
v10 = field_height;
}
while ( field_height > v12 );
v9 = field_width;
}
++v11;
}
while ( v9 > v11 );
}
point_x = field_x + v9 / 2;
point_y = field_y + v10 / 2;
return con_charAt(64LL, 3u, point_x, field_y + v10 / 2, v7, v8, a7);
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6, char a7)
{
__int64 v7; // r8
__int64 v8; // r9
int v9; // edx
int v10; // eax
int v11; // ebp
int v12; // ebx
int v13; // eax
unsigned int v14; // esi
__int64 v15; // rdi
unsigned int v16; // ecx
char v18; // [rsp+0h] [rbp-18h]
char v19; // [rsp+0h] [rbp-18h]

func18();
func15(3);
func19("Use arrows to move point, use Esc to exit.", v18);
v9 = field_width;
v10 = field_height;
if ( field_width > 0 )
{
v11 = 0;
do
{
v12 = 0;
if ( v10 > 0 )
{
do
{
if ( v11 && field_width - 1 != v11 && v12 )
{
v13 = v10 - 1;
v14 = (v13 != v12) + 1;
v15 = 3 * (unsigned int)(v13 == v12) + 32;
}
else
{
v14 = 1;
v15 = 35LL;
}
v16 = v12 + field_y;
++v12;
func7(v15, v14, v11 + field_x, v16, v7, v8, v19);
v10 = field_height;
}
while ( field_height > v12 );
v9 = field_width;
}
++v11;
}
while ( v9 > v11 );
}
point_x = field_x + v9 / 2;
point_y = field_y + v10 / 2;
return func7(64LL, 3u, point_x, field_y + v10 / 2, v7, v8, a7);
}

original_code:
static void initial_draw() {
    con_clearScr();
    con_gotoXY(3, 1);
    con_outTxt("Use arrows to move point, use Esc to exit.");
    {
        int i, j;
        for (i = 0; i < field_width; ++i) {
            for (j = 0; j < field_height; ++j) {
                int ch;
                int color;
                if (i == 0 || i == field_width-1 || j == 0 || j == field_height-1) {
                    ch = '#';
                    color = 1;
                } else {
                    ch = ' ';
                    color = 2;
                }
                con_charAt(ch, color, field_x + i, field_y + j);
            }
        }
    }
    point_x = field_x + field_width/2;
    point_y = field_y + field_height/2;
    con_charAt('@', 3, point_x, point_y);
}

canonical_original_code:
static void initial_draw() {
    con_clearScr();
    con_gotoXY(3, 1);
    con_outTxt("Use arrows to move point, use Esc to exit.");
    {
        int i; int j;
        for (i = 0; i < field_width; ++i) {
            for (j = 0; j < field_height; ++j) {
                int ch;
                int color;
                if (i == 0 || i == field_width-1 || j == 0 || j == field_height-1) {
                    ch = '#';
                    color = 1;
                } else {
                    ch = ' ';
                    color = 2;
                }
                con_charAt(ch, color, field_x + i, field_y + j);
            }
        }
    }
    point_x = field_x + field_width/2;
    point_y = field_y + field_height/2;
    con_charAt('@', 3, point_x, point_y);
}

variable_types: j int


decompiled_code:
__int64 init_colors()
{
con_initPair(1LL, 0LL, 4LL);
con_initPair(2LL, 2LL, 2LL);
return con_initPair(3LL, 1LL, 2LL);
}

canonical_decompiled_code:
__int64 func9()
{
func22(1LL, 0LL, 4LL);
func22(2LL, 2LL, 2LL);
return func22(3LL, 1LL, 2LL);
}

original_code:
static void init_colors() {
    con_initPair(1, 0, 4);
    con_initPair(2, 2, 2);
    con_initPair(3, 1, 2);
}


decompiled_code:
__int64 __fastcall process_key(int a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
int v6; // edx
__int64 result; // rax
unsigned int v8; // ecx
int v9; // ebp
int v10; // ebx
__int64 v11; // r8
__int64 v12; // r9
char v13; // [rsp+0h] [rbp-18h]
char v14; // [rsp+0h] [rbp-18h]

if ( a1 == 259 )
{
v8 = point_y;
if ( point_y - 1 <= field_y )
return 0LL;
v6 = point_x;
v9 = -1;
v10 = 0;
goto LABEL_11;
}
if ( a1 <= 259 )
{
result = 1LL;
if ( a1 == 27 )
return result;
if ( a1 != 258 )
return 0LL;
v8 = point_y;
result = 0LL;
if ( point_y + 1 >= field_y + field_height - 1 )
return result;
v6 = point_x;
v9 = 1;
v10 = 0;
goto LABEL_11;
}
if ( a1 != 260 )
{
if ( a1 != 261 )
return 0LL;
v6 = point_x;
result = 0LL;
if ( point_x + 1 >= field_x + field_width - 1 )
return result;
v8 = point_y;
v9 = 0;
v10 = 1;
LABEL_11:
con_charAt(32LL, 2u, v6, v8, a5, a6, v13);
point_x += v10;
point_y += v9;
con_charAt(64LL, 3u, point_x, point_y, v11, v12, v14);
return 0LL;
}
v6 = point_x;
result = 0LL;
if ( point_x - 1 > field_x )
{
v8 = point_y;
v9 = 0;
v10 = -1;
goto LABEL_11;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func10(int a1, __int64 a2, __int64 a3, __int64 a4, __int64 a5, __int64 a6)
{
int v6; // edx
__int64 result; // rax
unsigned int v8; // ecx
int v9; // ebp
int v10; // ebx
__int64 v11; // r8
__int64 v12; // r9
char v13; // [rsp+0h] [rbp-18h]
char v14; // [rsp+0h] [rbp-18h]

if ( a1 == 259 )
{
v8 = point_y;
if ( point_y - 1 <= field_y )
return 0LL;
v6 = point_x;
v9 = -1;
v10 = 0;
goto LABEL_11;
}
if ( a1 <= 259 )
{
result = 1LL;
if ( a1 == 27 )
return result;
if ( a1 != 258 )
return 0LL;
v8 = point_y;
result = 0LL;
if ( point_y + 1 >= field_y + field_height - 1 )
return result;
v6 = point_x;
v9 = 1;
v10 = 0;
goto LABEL_11;
}
if ( a1 != 260 )
{
if ( a1 != 261 )
return 0LL;
v6 = point_x;
result = 0LL;
if ( point_x + 1 >= field_x + field_width - 1 )
return result;
v8 = point_y;
v9 = 0;
v10 = 1;
LABEL_11:
func7(32LL, 2u, v6, v8, a5, a6, v13);
point_x += v10;
point_y += v9;
func7(64LL, 3u, point_x, point_y, v11, v12, v14);
return 0LL;
}
v6 = point_x;
result = 0LL;
if ( point_x - 1 > field_x )
{
v8 = point_y;
v9 = 0;
v10 = -1;
goto LABEL_11;
}
return result;
}

original_code:
int process_key(int key) {
    int dx = 0;
    int dy = 0;
    switch (key) {
        case 27:
            return 1;
        case 0403:
            if (point_y - 1 > field_y) {
                dy = -1;
            }
            break;
        case 0402:
            if (point_y + 1 < field_y + field_height - 1) {
                dy = 1;
            }
            break;
        case 0404:
            if (point_x - 1 > field_x) {
                dx = -1;
            }
            break;
        case 0405:
            if (point_x + 1 < field_x + field_width - 1) {
                dx = 1;
            }
            break;
    }
    if (dx != 0 || dy != 0) {
        con_charAt(' ', 2, point_x, point_y);
        point_x += dx;
        point_y += dy;
        con_charAt('@', 3, point_x, point_y);
    }
    return 0;
}

variable_types: key int

variable_types: dx int

variable_types: dy int


decompiled_code:
// attributes: thunk
int con_deinit(void)
{
return endwin();
}

canonical_decompiled_code:
int func11(void)
{
return endwin();
}

original_code:
void con_deinit()
{
    endwin();
}

canonical_original_code:
void con_deinit()
{
    endwin();
}


decompiled_code:
int con_showCursor()
{
return curs_set(1);
}

canonical_decompiled_code:
int func12()
{
return curs_set(1);
}

original_code:
void con_showCursor()
{
    curs_set(1);
}

canonical_original_code:
void con_showCursor()
{
    curs_set(1);
}


decompiled_code:
int con_init()
{
initscr();
noecho();
cbreak();
nodelay(stdscr__NCURSES6_TINFO_5_0_19991023, 1);
keypad(stdscr__NCURSES6_TINFO_5_0_19991023, 1);
start_color();
wattr_on(stdscr__NCURSES6_TINFO_5_0_19991023, 0x200000uLL, 0LL);
return con_showCursor();
}

canonical_decompiled_code:
int func13()
{
initscr();
noecho();
cbreak();
nodelay(stdscr__NCURSES6_TINFO_5_0_19991023, 1);
keypad(stdscr__NCURSES6_TINFO_5_0_19991023, 1);
start_color();
wattr_on(stdscr__NCURSES6_TINFO_5_0_19991023, 0x200000uLL, 0LL);
return func12();
}

original_code:
void con_init()
{
    initscr();
    noecho();
    cbreak();
    nodelay(stdscr, 1);
    keypad(stdscr, 1);
    start_color();
    wattr_on(stdscr, (attr_t)((((chtype)((1U)) << ((13) + 8)))), ((void *)0));
    con_showCursor();
}


decompiled_code:
int con_hideCursor()
{
return curs_set(0);
}

canonical_decompiled_code:
int func14()
{
return curs_set(0);
}

original_code:
void con_hideCursor()
{
    curs_set(0);
}

canonical_original_code:
void con_hideCursor()
{
    curs_set(0);
}


decompiled_code:
int __fastcall con_gotoXY(int a1, int a2)
{
return wmove(stdscr__NCURSES6_TINFO_5_0_19991023, a2, a1);
}

canonical_decompiled_code:
int __fastcall func15(int a1, int a2)
{
return wmove(stdscr__NCURSES6_TINFO_5_0_19991023, a2, a1);
}

original_code:
int con_gotoXY(int x, int y)
{
    return wmove(stdscr,(y),(x));
}


decompiled_code:
__int64 __fastcall con_getXY(_DWORD *a1, _DWORD *a2)
{
__int64 result; // rax

if ( stdscr__NCURSES6_TINFO_5_0_19991023 )
{
result = (unsigned int)stdscr__NCURSES6_TINFO_5_0_19991023->_curx;
*a2 = stdscr__NCURSES6_TINFO_5_0_19991023->_cury;
}
else
{
*a2 = -1;
result = 0xFFFFFFFFLL;
}
*a1 = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func16(_DWORD *a1, _DWORD *a2)
{
__int64 result; // rax

if ( stdscr__NCURSES6_TINFO_5_0_19991023 )
{
result = (unsigned int)stdscr__NCURSES6_TINFO_5_0_19991023->_curx;
*a2 = stdscr__NCURSES6_TINFO_5_0_19991023->_cury;
}
else
{
*a2 = -1;
result = 0xFFFFFFFFLL;
}
*a1 = result;
return result;
}

original_code:
int con_getXY(int *px, int *py)
{
    return (*py = ((0 != (const void *)((stdscr))) ? (stdscr)->_cury : (-1)), *px = ((0 != (const void *)((stdscr))) ? (stdscr)->_curx : (-1)));
}

canonical_original_code:
int con_getXY(int *px, int *py)
{
    return (*py = ((0 != (void *)((stdscr))) ? (stdscr)->_cury : (-1)), *px = ((0 != (void *)((stdscr))) ? (stdscr)->_curx : (-1)));
}

variable_types:
px
int *

variable_types:
py
int *


decompiled_code:
__int64 __fastcall con_getMaxXY(_DWORD *a1, _DWORD *a2)
{
__int64 result; // rax

if ( stdscr__NCURSES6_TINFO_5_0_19991023 )
{
result = (unsigned int)(stdscr__NCURSES6_TINFO_5_0_19991023->_maxx + 1);
*a2 = stdscr__NCURSES6_TINFO_5_0_19991023->_maxy + 1;
}
else
{
*a2 = -1;
result = 0xFFFFFFFFLL;
}
*a1 = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func17(_DWORD *a1, _DWORD *a2)
{
__int64 result; // rax

if ( stdscr__NCURSES6_TINFO_5_0_19991023 )
{
result = (unsigned int)(stdscr__NCURSES6_TINFO_5_0_19991023->_maxx + 1);
*a2 = stdscr__NCURSES6_TINFO_5_0_19991023->_maxy + 1;
}
else
{
*a2 = -1;
result = 0xFFFFFFFFLL;
}
*a1 = result;
return result;
}

original_code:
int con_getMaxXY(int *px, int *py)
{
    return (*py = ((0 != (const void *)((stdscr))) ? ((stdscr)->_maxy + 1) : (-1)), *px = ((0 != (const void *)((stdscr))) ? ((stdscr)->_maxx + 1) : (-1)));
}

canonical_original_code:
int con_getMaxXY(int *px, int *py)
{
    return (*py = ((0 != (void *)((stdscr))) ? ((stdscr)->_maxy + 1) : (-1)), *px = ((0 != (void *)((stdscr))) ? ((stdscr)->_maxx + 1) : (-1)));
}


decompiled_code:
int con_clearScr()
{
wclear(stdscr__NCURSES6_TINFO_5_0_19991023);
return wrefresh(stdscr__NCURSES6_TINFO_5_0_19991023);
}

canonical_decompiled_code:
int func18()
{
wclear(stdscr__NCURSES6_TINFO_5_0_19991023);
return wrefresh(stdscr__NCURSES6_TINFO_5_0_19991023);
}

original_code:
int con_clearScr()
{
    wclear(stdscr);
    return wrefresh(stdscr);
}


decompiled_code:
__int64 con_outTxt(char *format, ...)
{
unsigned int v1; // er12
gcc_va_list arg; // [rsp+8h] [rbp-4D0h] BYREF
char s[1024]; // [rsp+20h] [rbp-4B8h] BYREF

va_start(arg, format);
v1 = vsnprintf(s, 0x3FFuLL, format, arg);
if ( printw(s) == -1 )
v1 = -1;
wrefresh(stdscr__NCURSES6_TINFO_5_0_19991023);
return v1;
}

canonical_decompiled_code:
__int64 func19(char *format, ...)
{
unsigned int v1; // er12
gcc_va_list arg; // [rsp+8h] [rbp-4D0h] BYREF
char s[1024]; // [rsp+20h] [rbp-4B8h] BYREF

va_start(arg, format);
v1 = vsnprintf(s, 0x3FFuLL, format, arg);
if ( printw(s) == -1 )
v1 = -1;
wrefresh(stdscr__NCURSES6_TINFO_5_0_19991023);
return v1;
}

original_code:
int con_outTxt(const char *format, ...)
{
    va_list arglist;
    char buffer[1024];
    int len;
    __builtin_va_start(arglist,format);
    len = vsnprintf(buffer, sizeof(buffer)-1, format, arglist);
    len = (printw(buffer) != (-1)) ? len : -1;
    wrefresh(stdscr);
    return len;
}

canonical_original_code:
int con_outTxt(char *format, ...)
{
    __builtin_va_list arglist;
    char buffer[1024];
    int len;
    __builtin_va_start(arglist,format);
    len = vsnprintf(buffer, sizeof(buffer)-1, format, arglist);
    len = (printw(buffer) != (-1)) ? len : -1;
    wrefresh(stdscr);
    return len;
}

variable_types:
format
char *

variable_types:
buffer
char [1024]

variable_types: len int


decompiled_code:
__int64 con_keyPressed()
{
int v0; // edi
__int64 result; // rax

v0 = wgetch(stdscr__NCURSES6_TINFO_5_0_19991023);
result = 0LL;
if ( v0 != -1 )
{
ungetch(v0);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func20()
{
int v0; // edi
__int64 result; // rax

v0 = wgetch(stdscr__NCURSES6_TINFO_5_0_19991023);
result = 0LL;
if ( v0 != -1 )
{
ungetch(v0);
result = 1LL;
}
return result;
}

original_code:
int con_keyPressed()
{
    int ch = wgetch(stdscr);
    if (ch != (-1))
    {
        ungetch(ch);
        return 1;
    }
    return 0;
}


decompiled_code:
int con_getKey()
{
return wgetch(stdscr__NCURSES6_TINFO_5_0_19991023);
}

canonical_decompiled_code:
int func21()
{
return wgetch(stdscr__NCURSES6_TINFO_5_0_19991023);
}

original_code:
int con_getKey()
{
    return wgetch(stdscr);
}


decompiled_code:
int __fastcall con_initPair(__int16 a1, __int16 a2, __int16 a3)
{
return init_pair(a1, a2, a3);
}

canonical_decompiled_code:
int __fastcall func22(__int16 a1, __int16 a2, __int16 a3)
{
return init_pair(a1, a2, a3);
}

original_code:
int con_initPair(short n, short fg, short bg)
{
    return init_pair(n, fg, bg);
}

variable_types: n short

variable_types: fg short

variable_types: bg short


decompiled_code:
int __fastcall con_setColor(__int16 a1)
{
return wattr_on(stdscr__NCURSES6_TINFO_5_0_19991023, (unsigned __int16)(a1 << 8), 0LL);
}

canonical_decompiled_code:
int __fastcall func23(__int16 a1)
{
return wattr_on(stdscr__NCURSES6_TINFO_5_0_19991023, (unsigned __int16)(a1 << 8), 0LL);
}


limitValue

decompiled_code:
__int64 __fastcall limitValue(signed int a1, unsigned int a2, unsigned int a3)
{
__int64 result; // rax

result = a2;
if ( a1 >= (int)a2 )
{
result = a3;
if ( a1 <= (int)a3 )
result = (unsigned int)a1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(signed int a1, unsigned int a2, unsigned int a3)
{
__int64 result; // rax

result = a2;
if ( a1 >= (int)a2 )
{
result = a3;
if ( a1 <= (int)a3 )
result = (unsigned int)a1;
}
return result;
}

original_code:
static int limitValue(int x, int min, int max) {
    return (x < min) ? min : ((x > max) ? max : x);
}

variable_types: x int

variable_types: min int

variable_types: max int


checkSDLResult

decompiled_code:
void __fastcall checkSDLResult(int a1)
{
const char *v1; // rax

if ( !a1 )
{
v1 = (const char *)SDL_GetError();
fprintf(stderr, "Error: %s\n", v1);
exit(1);
}
}

canonical_decompiled_code:
void __fastcall func10(int a1)
{
const char *v1; // rax

if ( !a1 )
{
v1 = (const char *)SDL_GetError();
fprintf(stderr, "Error: %s\n", v1);
exit(1);
}
}

original_code:
static void checkSDLResult(int result) {
    if (!result) {
        fprintf(stderr, "Error: %s\n", SDL_GetError());
        exit(1);
    }
}

variable_types: result int


absentDeLigne

repo: cyprieng/Sudoku

decompiled_code:
__int64 __fastcall absentDeLigne(int a1, __int64 a2, int a3)
{
_DWORD *v3; // rax
_DWORD *v4; // rdx

v3 = (_DWORD *)(a2 + 36LL * a3);
v4 = v3 + 9;
while ( *v3 != a1 )
{
if ( ++v3 == v4 )
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, __int64 a2, int a3)
{
_DWORD *v3; // rax
_DWORD *v4; // rdx

v3 = (_DWORD *)(a2 + 36LL * a3);
v4 = v3 + 9;
while ( *v3 != a1 )
{
if ( ++v3 == v4 )
return 1LL;
}
return 0LL;
}

original_code:
int absentDeLigne(int k, int grille[9][9], int i){
    int j;
    for(j=0; j < 9; j++){
        if(grille[i][j] == k) return 0;
    }
    return 1;
}

variable_types: k int

variable_types:
grille
int [9][9]

variable_types: i int


absentDeColonne

decompiled_code:
__int64 __fastcall absentDeColonne(int a1, __int64 a2, int a3)
{
_DWORD *v3; // rax
_DWORD *v4; // rdx

v3 = (_DWORD *)(a2 + 4LL * a3);
v4 = v3 + 81;
while ( *v3 != a1 )
{
v3 += 9;
if ( v3 == v4 )
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, __int64 a2, int a3)
{
_DWORD *v3; // rax
_DWORD *v4; // rdx

v3 = (_DWORD *)(a2 + 4LL * a3);
v4 = v3 + 81;
while ( *v3 != a1 )
{
v3 += 9;
if ( v3 == v4 )
return 1LL;
}
return 0LL;
}

original_code:
int absentDeColonne(int k, int grille[9][9], int j){
    int i;
    for (i=0; i < 9; i++){
        if(grille[i][j] == k) return 0;
    }
    return 1;
}

variable_types: j int


absentDeBloc

original_code:
int absentDeBloc (int k, int grille[9][9], int i, int j){
    int _i = i-(i%3), _j = j-(j%3);
    for (i=_i; i < _i+3; i++){
        for (j=_j; j < _j+3; j++){
            if (grille[i][j] == k) return 0;
        }
    }
    return 1;
}

canonical_original_code:
int absentDeBloc(int k, int grille[9][9], int i, int j){
    int _i = i-(i%3); int _j = j-(j%3);
    for (i=_i; i < _i+3; i++){
        for (j=_j; j < _j+3; j++){
            if (grille[i][j] == k) return 0;
        }
    }
    return 1;
}


resolve

decompiled_code:
__int64 __fastcall resolve(__int64 a1, int a2, unsigned int a3)
{
int v4; // ebx
int v5; // er15
int v6; // ebp
unsigned int v7; // er12
int v8; // er14
unsigned int v9; // ebx
int v10; // er14
unsigned int v11; // ebx
unsigned int v12; // eax
int v13; // edi
__int64 v15; // [rsp+8h] [rbp-50h]

v4 = a2;
if ( a2 != 81 )
{
while ( 1 )
{
v5 = v4 / 9;
v6 = v4 % 9;
v7 = *(_DWORD *)(a1 + 36LL * (v4 / 9) + 4LL * (v4 % 9));
v15 = a1 + 36LL * (v4 / 9);
if ( !v7 )
break;
if ( ++v4 == 81 )
return 1;
}
if ( a3 )
{
v10 = 20;
v11 = v4 + 1;
v12 = time(0LL);
srand(v12);
while ( 1 )
{
v13 = rand() % 9 + 1;
if ( (unsigned int)absentDeLigne(v13, a1, v5) )
{
if ( (unsigned int)absentDeColonne(v13, a1, v6) )
{
if ( (unsigned int)absentDeBloc(v13, a1, v5, v6) )
{
*(_DWORD *)(v15 + 4LL * v6) = v13;
if ( (unsigned int)resolve(a1, v11, a3) )
break;
}
}
}
if ( !--v10 )
{
LABEL_11:
*(_DWORD *)(v15 + 4LL * v6) = 0;
return v7;
}
}
}
else
{
v8 = 1;
v9 = v4 + 1;
while ( 1 )
{
if ( (unsigned int)absentDeLigne(v8, a1, v5) )
{
if ( (unsigned int)absentDeColonne(v8, a1, v6) )
{
if ( (unsigned int)absentDeBloc(v8, a1, v5, v6) )
{
*(_DWORD *)(v15 + 4LL * v6) = v8;
if ( (unsigned int)resolve(a1, v9, 0LL) )
break;
}
}
}
if ( ++v8 == 10 )
goto LABEL_11;
}
}
}
return 1;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2, unsigned int a3)
{
int v4; // ebx
int v5; // er15
int v6; // ebp
unsigned int v7; // er12
int v8; // er14
unsigned int v9; // ebx
int v10; // er14
unsigned int v11; // ebx
unsigned int v12; // eax
int v13; // edi
__int64 v15; // [rsp+8h] [rbp-50h]

v4 = a2;
if ( a2 != 81 )
{
while ( 1 )
{
v5 = v4 / 9;
v6 = v4 % 9;
v7 = *(_DWORD *)(a1 + 36LL * (v4 / 9) + 4LL * (v4 % 9));
v15 = a1 + 36LL * (v4 / 9);
if ( !v7 )
break;
if ( ++v4 == 81 )
return 1;
}
if ( a3 )
{
v10 = 20;
v11 = v4 + 1;
v12 = time(0LL);
srand(v12);
while ( 1 )
{
v13 = rand() % 9 + 1;
if ( (unsigned int)func0(v13, a1, v5) )
{
if ( (unsigned int)func1(v13, a1, v6) )
{
if ( (unsigned int)func2(v13, a1, v5, v6) )
{
*(_DWORD *)(v15 + 4LL * v6) = v13;
if ( (unsigned int)func3(a1, v11, a3) )
break;
}
}
}
if ( !--v10 )
{
LABEL_11:
*(_DWORD *)(v15 + 4LL * v6) = 0;
return v7;
}
}
}
else
{
v8 = 1;
v9 = v4 + 1;
while ( 1 )
{
if ( (unsigned int)func0(v8, a1, v5) )
{
if ( (unsigned int)func1(v8, a1, v6) )
{
if ( (unsigned int)func2(v8, a1, v5, v6) )
{
*(_DWORD *)(v15 + 4LL * v6) = v8;
if ( (unsigned int)func3(a1, v9, 0LL) )
break;
}
}
}
if ( ++v8 == 10 )
goto LABEL_11;
}
}
}
return 1;
}

original_code:
int resolve(int grille[9][9], int position, int generate){
    int k;
    if(position == 9*9) return 1;
    int i = position/9, j = position%9;
    if(grille[i][j] != 0)
        return resolve(grille, position+1, generate);
    if(!generate){
        for(k=1; k <= 9; k++){
            if(absentDeLigne(k,grille,i) && absentDeColonne(k,grille,j) && absentDeBloc(k,grille,i,j)){
                grille[i][j] = k;
                if(resolve(grille, position+1, generate)) return 1;
            }
        }
    }
    else{
        int nb;
        srand(time(((void *)0)));
        for(k=1; k <= 20; k++){
            nb = rand()%9 + 1;
            if (absentDeLigne(nb,grille,i) && absentDeColonne(nb,grille,j) && absentDeBloc(nb,grille,i,j)){
                grille[i][j] = nb;
                if(resolve(grille, position+1, generate)) return 1;
            }
        }
    }
    grille[i][j] = 0;
    return 0;
}

canonical_original_code:
int resolve(int grille[9][9], int position, int generate){
    int k;
    if(position == 9*9) return 1;
    int i = position/9; int j = position%9;
    if(grille[i][j] != 0)
        return resolve(grille, position+1, generate);
    if(!generate){
        for(k=1; k <= 9; k++){
            if(absentDeLigne(k,grille,i) && absentDeColonne(k,grille,j) && absentDeBloc(k,grille,i,j)){
                grille[i][j] = k;
                if(resolve(grille, position+1, generate)) return 1;
            }
        }
    }
    else{
        int nb;
        srand(time(((void *)0)));
        for(k=1; k <= 20; k++){
            nb = rand()%9 + 1;
            if (absentDeLigne(nb,grille,i) && absentDeColonne(nb,grille,j) && absentDeBloc(nb,grille,i,j)){
                grille[i][j] = nb;
                if(resolve(grille, position+1, generate)) return 1;
            }
        }
    }
    grille[i][j] = 0;
    return 0;
}

variable_types: position int

variable_types: generate int

variable_types: nb int


isUnique

original_code:
int isUnique(int grid[][9]){
    int grid_copy[9][9] = {{0}};
    int grid_to_resolve[9][9] = {{0}};
    int grid_resolve[9][9] = {{0}};
    int grid_fixes[9][9] = {{0}};
    int i = 0, k = 0;
    int row,col;
    int value;
    for(i=0;i<=80;i++){
        row = floor(i/9);
        col = i%9;
        if(grid[col][row] != 0){grid_fixes[col][row] = 1;}
    }
    memcpy(grid_to_resolve, grid, sizeof(grid_to_resolve));
    memcpy(grid_copy, grid, sizeof(grid_copy));
    resolve(grid_copy, 0, 0);
     for(i=0;i<=80;i++){
        row = floor(i/9);
        col = i%9;
        if(grid_fixes[col][row] == 1)continue;
        value = grid_to_resolve[col][row];
        for(k=1;k<=9;k++){
            grid_to_resolve[col][row] = k;
            if(!is_grid_valid(grid_to_resolve)) continue;
            memcpy(grid_resolve, grid_to_resolve, sizeof(grid_resolve));
            if(!resolve(grid_resolve, 0, 0)) continue;
            if(!equal_grid(grid_copy, grid_resolve))return 0;
        }
        grid_to_resolve[col][row] = value;
    }
 return 1;
}

canonical_original_code:
int isUnique(int grid[][9]){
    int grid_copy[9][9] = {{0}};
    int grid_to_resolve[9][9] = {{0}};
    int grid_resolve[9][9] = {{0}};
    int grid_fixes[9][9] = {{0}};
    int i = 0; int k = 0;
    int row; int col;
    int value;
    for(i=0;i<=80;i++){
        row = floor(i/9);
        col = i%9;
        if(grid[col][row] != 0){grid_fixes[col][row] = 1;}
    }
    memcpy(grid_to_resolve, grid, sizeof(grid_to_resolve));
    memcpy(grid_copy, grid, sizeof(grid_copy));
    resolve(grid_copy, 0, 0);
     for(i=0;i<=80;i++){
        row = floor(i/9);
        col = i%9;
        if(grid_fixes[col][row] == 1)continue;
        value = grid_to_resolve[col][row];
        for(k=1;k<=9;k++){
            grid_to_resolve[col][row] = k;
            if(!is_grid_valid(grid_to_resolve)) continue;
            memcpy(grid_resolve, grid_to_resolve, sizeof(grid_resolve));
            if(!resolve(grid_resolve, 0, 0)) continue;
            if(!equal_grid(grid_copy, grid_resolve))return 0;
        }
        grid_to_resolve[col][row] = value;
    }
 return 1;
}

variable_types:
grid
int [][9]

variable_types: row int

variable_types: col int

variable_types: value int


generate

decompiled_code:
void __fastcall generate(_DWORD *a1, int a2)
{
int v2; // ebp
unsigned int v3; // eax
int v4; // eax
__int64 v5; // r13
_DWORD *v6; // r14
int v7; // er15

v2 = 0;
resolve((__int64)a1, 0, 1u);
v3 = time(0LL);
srand(v3);
if ( a2 > 0 )
{
do
{
while ( 1 )
{
do
{
v4 = rand() % 81;
v5 = v4 / 9;
v6 = &a1[9 * (v4 % 9)];
v7 = v6[v5];
}
while ( !v7 );
v6[v4 / 9] = 0;
if ( (unsigned int)isUnique(a1) )
break;
v6[v5] = v7;
}
++v2;
}
while ( a2 != v2 );
}
}

original_code:
void generate(int grid[][9], int type){
    int i, valide, position, row, col, inter;
    resolve(grid, 0, 1);
    srand(time(((void *)0)));
    for(i=0;i<type;i++){
        valide = 0;
        do{
            position = rand() % 81;
            row = floor(position/9);
            col = position%9;
            if(grid[col][row] == 0) continue;
            inter = grid[col][row];
            grid[col][row] = 0;
            if(isUnique(grid)){valide = 1;}
            else{grid[col][row] = inter;}
        }while(!valide);
    }
}

canonical_original_code:
void generate(int grid[][9], int type){
    int i; int valide; int position; int row; int col; int inter;
    resolve(grid, 0, 1);
    srand(time(((void *)0)));
    for(i=0;i<type;i++){
        valide = 0;
        do{
            position = rand() % 81;
            row = floor(position/9);
            col = position%9;
            if(grid[col][row] == 0) continue;
            inter = grid[col][row];
            grid[col][row] = 0;
            if(isUnique(grid)){valide = 1;}
            else{grid[col][row] = inter;}
        }while(!valide);
    }
}

variable_types: type int

variable_types: valide int

variable_types: inter int


decompiled_code:
__int64 __fastcall is_grid_valid(__int64 a1)
{
unsigned int i; // ebp
unsigned int v2; // er12
unsigned int *v3; // r13
unsigned int v4; // ebx

for ( i = 0; i != 81; ++i )
{
v2 = i / 9;
v3 = (unsigned int *)(a1 + 36LL * (i % 9) + 4LL * (i / 9));
v4 = *v3;
*v3 = 0;
if ( (unsigned int)absentDeBloc(v4, a1, i % 9, i / 9) && (unsigned int)absentDeColonne(v4, a1, v2) )
{
if ( !(unsigned int)absentDeLigne(v4, a1, i % 9) && v4 )
{
LABEL_8:
*(_DWORD *)(a1 + 36LL * (i % 9) + 4LL * (int)v2) = v4;
return 0LL;
}
}
else if ( v4 )
{
goto LABEL_8;
}
*v3 = v4;
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
unsigned int i; // ebp
unsigned int v2; // er12
unsigned int *v3; // r13
unsigned int v4; // ebx

for ( i = 0; i != 81; ++i )
{
v2 = i / 9;
v3 = (unsigned int *)(a1 + 36LL * (i % 9) + 4LL * (i / 9));
v4 = *v3;
*v3 = 0;
if ( (unsigned int)absentDeBloc(v4, a1, i % 9, i / 9) && (unsigned int)absentDeColonne(v4, a1, v2) )
{
if ( !(unsigned int)absentDeLigne(v4, a1, i % 9) && v4 )
{
LABEL_8:
*(_DWORD *)(a1 + 36LL * (i % 9) + 4LL * (int)v2) = v4;
return 0LL;
}
}
else if ( v4 )
{
goto LABEL_8;
}
*v3 = v4;
}
return 1LL;
}

original_code:
int is_grid_valid(int grid[9][9]){
    int i = 0;
    int row,col;
    int value;
     for(i=0;i<=80;i++){
        row = floor(i/9);
        col = i%9;
        value = grid[col][row];
        grid[col][row] = 0;
        if((!absentDeBloc(value, grid, col, row) || !absentDeColonne(value, grid, row) || !absentDeLigne(value, grid, col)) && value != 0){
            grid[col][row] = value;
            return 0;
        }
        grid[col][row] = value;
    }
    return 1;
}

canonical_original_code:
int is_grid_valid(int grid[9][9]){
    int i = 0;
    int row; int col;
    int value;
     for(i=0;i<=80;i++){
        row = floor(i/9);
        col = i%9;
        value = grid[col][row];
        grid[col][row] = 0;
        if((!absentDeBloc(value, grid, col, row) || !absentDeColonne(value, grid, row) || !absentDeLigne(value, grid, col)) && value != 0){
            grid[col][row] = value;
            return 0;
        }
        grid[col][row] = value;
    }
    return 1;
}

variable_types:
grid
int [9][9]


original_code:
int equal_grid(int grid1[9][9], int grid2[9][9]){
    int i = 0;
    int row,col;
     for(i=0;i<=80;i++){
        row = floor(i/9);
        col = i%9;
        if(grid1[col][row] != grid2[col][row]){
            return 0;
        }
    }
    return 1;
}

canonical_original_code:
int equal_grid(int grid1[9][9], int grid2[9][9]){
    int i = 0;
    int row; int col;
     for(i=0;i<=80;i++){
        row = floor(i/9);
        col = i%9;
        if(grid1[col][row] != grid2[col][row]){
            return 0;
        }
    }
    return 1;
}


repo: cyrillos/compel

decompiled_code:
__int64 __fastcall task_in_compat_mode(unsigned int a1)
{
int *v1; // rax
int *v2; // rbx
__int64 v3; // rax
__int64 v4; // r12
__int64 v5; // rdx
__int64 result; // rax

v1 = _errno_location();
*v1 = 0;
v2 = v1;
v3 = ptrace(PTRACE_PEEKUSER, a1, 136LL, 0LL);
if ( *v2 )
{
print_on_level(1LL, "Error (%s:%d): Can't get CS register for %d: %m\n", "src/lib/arch/x86/ptrace.c", 29LL, a1);
result = 1LL;
}
else
{
v4 = v3;
v5 = ptrace(PTRACE_PEEKUSER, a1, 184LL, 0LL);
if ( *v2 )
{
print_on_level(1LL, "Error (%s:%d): Can't get DS register for %d: %m\n", "src/lib/arch/x86/ptrace.c", 36LL, a1);
result = 1LL;
}
else
{
LOBYTE(v5) = v5 == 43;
result = (unsigned int)v5 | (v4 != 51);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1)
{
int *v1; // rax
int *v2; // rbx
__int64 v3; // rax
__int64 v4; // r12
__int64 v5; // rdx
__int64 result; // rax

v1 = _errno_location();
*v1 = 0;
v2 = v1;
v3 = ptrace(PTRACE_PEEKUSER, a1, 136LL, 0LL);
if ( *v2 )
{
print_on_level(1LL, "Error (%s:%d): Can't get CS register for %d: %m\n", "src/lib/arch/x86/ptrace.c", 29LL, a1);
result = 1LL;
}
else
{
v4 = v3;
v5 = ptrace(PTRACE_PEEKUSER, a1, 184LL, 0LL);
if ( *v2 )
{
print_on_level(1LL, "Error (%s:%d): Can't get DS register for %d: %m\n", "src/lib/arch/x86/ptrace.c", 36LL, a1);
result = 1LL;
}
else
{
LOBYTE(v5) = v5 == 43;
result = (unsigned int)v5 | (v4 != 51);
}
}
return result;
}

original_code:
_Bool task_in_compat_mode(pid_t pid)
{
 unsigned long cs, ds;
 (*__errno_location ()) = 0;
 cs = ptrace(PTRACE_PEEKUSER, pid, ((size_t) &((user_regs_struct_t *)0)->cs), 0);
 if ((*__errno_location ()) != 0) {
  print_on_level((1), "Error (%s:%d): " "Can't get CS register for %d" ": %m\n", "src/lib/arch/x86/ptrace.c", 29, pid);
  return -1;
 }
 (*__errno_location ()) = 0;
 ds = ptrace(PTRACE_PEEKUSER, pid, ((size_t) &((user_regs_struct_t *)0)->ds), 0);
 if ((*__errno_location ()) != 0) {
  print_on_level((1), "Error (%s:%d): " "Can't get DS register for %d" ": %m\n", "src/lib/arch/x86/ptrace.c", 36, pid);
  return -1;
 }
 return cs != 0x33 || ds == 0x2b;
}

canonical_original_code:
_Bool task_in_compat_mode(int pid)
{
 unsigned long cs; unsigned long ds;
 (*__errno_location ()) = 0;
 cs = ptrace(PTRACE_PEEKUSER, pid, ((unsigned long) &((struct user_regs_struct_t *)0)->cs), 0);
 if ((*__errno_location ()) != 0) {
  print_on_level((1), "Error (%s:%d): " "Can't get CS register for %d" ": %m\n", "src/lib/arch/x86/ptrace.c", 29, pid);
  return -1;
 }
 (*__errno_location ()) = 0;
 ds = ptrace(PTRACE_PEEKUSER, pid, ((unsigned long) &((struct user_regs_struct_t *)0)->ds), 0);
 if ((*__errno_location ()) != 0) {
  print_on_level((1), "Error (%s:%d): " "Can't get DS register for %d" ": %m\n", "src/lib/arch/x86/ptrace.c", 36, pid);
  return -1;
 }
 return cs != 0x33 || ds == 0x2b;
}

variable_types: pid int

variable_types:
cs
unsigned long

variable_types:
ds
unsigned long

user_defined_types: struct user_regs_struct_t { unsigned long r15; unsigned long r14; unsigned long r13; unsigned long r12; unsigned long bp; unsigned long bx; unsigned long r11; unsigned long r10; unsigned long r9; unsigned long r8; unsigned long ax; unsigned long cx; unsigned long dx; unsigned long si; unsigned long di; unsigned long orig_ax; unsigned long ip; unsigned long cs; unsigned long flags; unsigned long sp; unsigned long ss; unsigned long fs_base; unsigned long gs_base; unsigned long ds; unsigned long es; unsigned long fs; unsigned long gs; }


decompiled_code:
__int64 __fastcall ptrace_get_fpu(unsigned int a1, __int64 a2)
{
__int64 result; // rax
__int64 v3[5]; // [rsp+0h] [rbp-28h] BYREF

v3[0] = a2;
v3[1] = 832LL;
if ( ptrace(PTRACE_GETREGSET, a1, 514LL, v3) < 0 )
{
if ( *_errno_location() == 19 && ptrace(PTRACE_GETFPREGS, a1, 0LL, a2) >= 0 )
{
*(_BYTE *)(a2 + 896) = 0;
result = 0LL;
}
else
{
print_on_level(
1LL,
"Error (%s:%d): Can't obtain FPU registers for %d: %m\n",
"src/lib/arch/x86/ptrace.c",
64LL,
a1);
result = 0xFFFFFFFFLL;
}
}
else
{
*(_BYTE *)(a2 + 896) = 1;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1, __int64 a2)
{
__int64 result; // rax
__int64 v3[5]; // [rsp+0h] [rbp-28h] BYREF

v3[0] = a2;
v3[1] = 832LL;
if ( ptrace(PTRACE_GETREGSET, a1, 514LL, v3) < 0 )
{
if ( *_errno_location() == 19 && ptrace(PTRACE_GETFPREGS, a1, 0LL, a2) >= 0 )
{
*(_BYTE *)(a2 + 896) = 0;
result = 0LL;
}
else
{
print_on_level(
1LL,
"Error (%s:%d): Can't obtain FPU registers for %d: %m\n",
"src/lib/arch/x86/ptrace.c",
64LL,
a1);
result = 0xFFFFFFFFLL;
}
}
else
{
*(_BYTE *)(a2 + 896) = 1;
result = 0LL;
}
return result;
}

original_code:
int ptrace_get_fpu(pid_t pid, fpu_state_t *fpu)
{
 struct iovec iov = {
  .iov_base = &fpu->xsave,
  .iov_len = sizeof(fpu->xsave),
 };
 if (ptrace(PTRACE_GETREGSET, pid, (unsigned int)0x202, &iov) < 0) {
  if ((*__errno_location ()) != 19)
   goto fpuerr;
  else {
   if (ptrace(PTRACE_GETFPREGS, pid, ((void *)0), &fpu->xsave) < 0)
    goto fpuerr;
   fpu->has_xsave = 0;
  }
 } else
  fpu->has_xsave = 1;
 return 0;
fpuerr:
 print_on_level((1), "Error (%s:%d): " "Can't obtain FPU registers for %d" ": %m\n", "src/lib/arch/x86/ptrace.c", 64, pid);
 return -1;
}

canonical_original_code:
int ptrace_get_fpu(int pid, struct fpu_state_t *fpu)
{
 struct iovec iov = {
  .iov_base = &fpu->xsave,
  .iov_len = sizeof(fpu->xsave),
 };
 if (ptrace(PTRACE_GETREGSET, pid, (unsigned int)0x202, &iov) < 0) {
  if ((*__errno_location ()) != 19)
   goto fpuerr;
  else {
   if (ptrace(PTRACE_GETFPREGS, pid, ((void *)0), &fpu->xsave) < 0)
    goto fpuerr;
   fpu->has_xsave = 0;
  }
 } else
  fpu->has_xsave = 1;
 return 0;
fpuerr:
 print_on_level((1), "Error (%s:%d): " "Can't obtain FPU registers for %d" ": %m\n", "src/lib/arch/x86/ptrace.c", 64, pid);
 return -1;
}

variable_types: struct fpu_state_t { union { struct xsave_struct { struct i387_fxsave_struct { unsigned short cwd; unsigned short swd; unsigned short twd; unsigned short fop; union { struct { unsigned long rip; unsigned long rdp; }; struct { unsigned int fip; unsigned int fcs; unsigned int foo; unsigned int fos; }; }; unsigned int mxcsr; unsigned int mxcsr_mask; unsigned int st_space[32]; unsigned int xmm_space[64]; unsigned int padding[12]; union { unsigned int padding1[12]; unsigned int sw_reserved[12]; }; } i387; struct xsave_hdr_struct { unsigned long xstate_bv; unsigned long reserved1[2]; unsigned long reserved2[5]; } xsave_hdr; struct ymmh_struct { unsigned int ymmh_space[64]; } ymmh; } xsave; unsigned char __pad[-1]; }; _Bool has_xsave; } *

variable_types: struct iovec { void *iov_base; unsigned long iov_len; }

user_defined_types: struct fpu_state_t { union { struct xsave_struct xsave; unsigned char __pad[-1]; }; _Bool has_xsave; }

user_defined_types: struct xsave_struct { struct i387_fxsave_struct i387; struct xsave_hdr_struct xsave_hdr; struct ymmh_struct ymmh; }


decompiled_code:
__int64 __fastcall ptrace_set_fpu(unsigned int a1, __int64 a2)
{
bool v2; // zf
__int64 v4[3]; // [rsp+0h] [rbp-18h] BYREF

v2 = *(_BYTE *)(a2 + 896) == 0;
v4[0] = a2;
v4[1] = 832LL;
if ( v2 )
{
if ( ptrace(PTRACE_SETFPREGS, a1, 0LL, a2) >= 0 )
return 0LL;
}
else if ( ptrace(PTRACE_SETREGSET, a1, 514LL, v4) >= 0 )
{
return 0LL;
}
print_on_level(1LL, "Error (%s:%d): Can't set FPU registers for %d: %m\n", "src/lib/arch/x86/ptrace.c", 85LL, a1);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, __int64 a2)
{
bool v2; // zf
__int64 v4[3]; // [rsp+0h] [rbp-18h] BYREF

v2 = *(_BYTE *)(a2 + 896) == 0;
v4[0] = a2;
v4[1] = 832LL;
if ( v2 )
{
if ( ptrace(PTRACE_SETFPREGS, a1, 0LL, a2) >= 0 )
return 0LL;
}
else if ( ptrace(PTRACE_SETREGSET, a1, 514LL, v4) >= 0 )
{
return 0LL;
}
print_on_level(1LL, "Error (%s:%d): Can't set FPU registers for %d: %m\n", "src/lib/arch/x86/ptrace.c", 85LL, a1);
return 0xFFFFFFFFLL;
}

original_code:
int ptrace_set_fpu(pid_t pid, fpu_state_t *fpu)
{
 struct iovec iov = {
  .iov_base = &fpu->xsave,
  .iov_len = sizeof(fpu->xsave),
 };
 if (fpu->has_xsave) {
  if (ptrace(PTRACE_SETREGSET, pid, (unsigned int)0x202, &iov) < 0)
   goto fpuerr;
 } else {
  if (ptrace(PTRACE_SETFPREGS, pid, ((void *)0), &fpu->xsave) < 0)
   goto fpuerr;
 }
 return 0;
fpuerr:
 print_on_level((1), "Error (%s:%d): " "Can't set FPU registers for %d" ": %m\n", "src/lib/arch/x86/ptrace.c", 85, pid);
 return -1;
}

canonical_original_code:
int ptrace_set_fpu(int pid, struct fpu_state_t *fpu)
{
 struct iovec iov = {
  .iov_base = &fpu->xsave,
  .iov_len = sizeof(fpu->xsave),
 };
 if (fpu->has_xsave) {
  if (ptrace(PTRACE_SETREGSET, pid, (unsigned int)0x202, &iov) < 0)
   goto fpuerr;
 } else {
  if (ptrace(PTRACE_SETFPREGS, pid, ((void *)0), &fpu->xsave) < 0)
   goto fpuerr;
 }
 return 0;
fpuerr:
 print_on_level((1), "Error (%s:%d): " "Can't set FPU registers for %d" ": %m\n", "src/lib/arch/x86/ptrace.c", 85, pid);
 return -1;
}

user_defined_types: struct iovec { void *iov_base; unsigned long iov_len; }

user_defined_types: struct i387_fxsave_struct { unsigned short cwd; unsigned short swd; unsigned short twd; unsigned short fop; union { struct { unsigned long rip; unsigned long rdp; }; struct { unsigned int fip; unsigned int fcs; unsigned int foo; unsigned int fos; }; }; unsigned int mxcsr; unsigned int mxcsr_mask; unsigned int st_space[32]; unsigned int xmm_space[64]; unsigned int padding[12]; union { unsigned int padding1[12]; unsigned int sw_reserved[12]; }; }

user_defined_types: struct xsave_hdr_struct { unsigned long xstate_bv; unsigned long reserved1[2]; unsigned long reserved2[5]; }

user_defined_types: struct ymmh_struct { unsigned int ymmh_space[64]; }


original_code:
static int setup_load_headers(load_info_t *info)
{
 unsigned int i;
 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
 info->secstrings= (void *)info->hdr + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
 for (i = 1; i < info->hdr->e_shnum; i++) {
  if (info->sechdrs[i].sh_type == 2) {
   info->index.sym = i;
   info->index.str = info->sechdrs[i].sh_link;
   info->strtab = (char *)info->hdr + info->sechdrs[info->index.str].sh_offset;
   break;
  } else if (info->sechdrs[i].sh_type == 1) {
   if (!strcmp(&info->secstrings[info->sechdrs[i].sh_name], ".compel.init")) {
    info->index.init = i;
   }
  }
 }
 if (info->index.sym == 0) {
  print_on_level((2), "Warn  (%s:%d): " "elf: " "Plugin has no symbols (stripped?)\n", "src/lib/arch/x86/elf.c", 287);
  return -8;
 }
 return 0;
}

canonical_original_code:
static int setup_load_headers(struct load_info_s *info)
{
 unsigned int i;
 info->sechdrs = (void *)info->hdr + info->hdr->e_shoff;
 info->secstrings= (void *)info->hdr + info->sechdrs[info->hdr->e_shstrndx].sh_offset;
 for (i = 1; i < info->hdr->e_shnum; i++) {
  if (info->sechdrs[i].sh_type == 2) {
   info->index.sym = i;
   info->index.str = info->sechdrs[i].sh_link;
   info->strtab = (char *)info->hdr + info->sechdrs[info->index.str].sh_offset;
   break;
  } else if (info->sechdrs[i].sh_type == 1) {
   if (!strcmp(&info->secstrings[info->sechdrs[i].sh_name], ".compel.init")) {
    info->index.init = i;
   }
  }
 }
 if (info->index.sym == 0) {
  print_on_level((2), "Warn  (%s:%d): " "elf: " "Plugin has no symbols (stripped?)\n", "src/lib/arch/x86/elf.c", 287);
  return -8;
 }
 return 0;
}

variable_types: struct load_info_s { long addr_delta; struct Elf64_Ehdr { unsigned char e_ident[-1]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned long e_entry; unsigned long e_phoff; unsigned long e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phentsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; } *hdr; unsigned long len; struct Elf64_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned long sh_flags; unsigned long sh_addr; unsigned long sh_offset; unsigned long sh_size; unsigned int sh_link; unsigned int sh_info; unsigned long sh_addralign; unsigned long sh_entsize; } *sechdrs; char *secstrings; char *strtab; struct { unsigned int sym; unsigned int str; unsigned int symtab; unsigned int symtab_link; unsigned int init; } index; } *

variable_types:
i
unsigned int

user_defined_types: struct load_info_s { long addr_delta; struct Elf64_Ehdr *hdr; unsigned long len; struct Elf64_Shdr *sechdrs; char *secstrings; char *strtab; struct { unsigned int sym; unsigned int str; unsigned int symtab; unsigned int symtab_link; unsigned int init; } index; }


original_code:
static int rewrite_section_headers(load_info_t *info)
{
 unsigned int i;
 info->sechdrs[0].sh_addr = 0;
 for (i = 1; i < info->hdr->e_shnum; i++) {
  Elf64_Shdr *shdr = &info->sechdrs[i];
  if (shdr->sh_type != 8 &&
      info->len < shdr->sh_offset + shdr->sh_size) {
   print_on_level((1), "Error (%s:%d): " "elf: " "Plugin len %lu truncated\n", "src/lib/arch/x86/elf.c", 64, info->len);
   return -8;
  }
  shdr->sh_addr = (size_t)info->hdr + shdr->sh_offset;
 }
 return 0;
}

canonical_original_code:
static int rewrite_section_headers(struct load_info_s *info)
{
 unsigned int i;
 info->sechdrs[0].sh_addr = 0;
 for (i = 1; i < info->hdr->e_shnum; i++) {
  struct Elf64_Shdr *shdr = &info->sechdrs[i];
  if (shdr->sh_type != 8 &&
      info->len < shdr->sh_offset + shdr->sh_size) {
   print_on_level((1), "Error (%s:%d): " "elf: " "Plugin len %lu truncated\n", "src/lib/arch/x86/elf.c", 64, info->len);
   return -8;
  }
  shdr->sh_addr = (unsigned long)info->hdr + shdr->sh_offset;
 }
 return 0;
}

variable_types: struct Elf64_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned long sh_flags; unsigned long sh_addr; unsigned long sh_offset; unsigned long sh_size; unsigned int sh_link; unsigned int sh_info; unsigned long sh_addralign; unsigned long sh_entsize; } *

user_defined_types: struct Elf64_Ehdr { unsigned char e_ident[-1]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned long e_entry; unsigned long e_phoff; unsigned long e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phentsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; }


decompiled_code:
__int64 __fastcall simplify_symbols(__int64 a1)
{
unsigned __int64 v1; // r15
__int64 v2; // rbx
unsigned int v3; // er13
__int64 v4; // rcx
unsigned int *v5; // r9
__int64 v6; // rax
const char *v7; // rdx
__int64 v8; // rcx
__int64 v9; // r8
const char *v10; // rsi
__int64 v11; // r9

v1 = *(_QWORD *)(a1 + 24) + ((unsigned __int64)*(unsigned int *)(a1 + 48) << 6);
v2 = *(_QWORD *)(v1 + 16);
if ( *(_QWORD *)(v1 + 32) > 0x2FuLL )
{
v3 = 2;
v4 = 1LL;
while ( 1 )
{
v5 = (unsigned int *)(v2 + 24 * v4);
v6 = *((unsigned __int16 *)v5 + 3);
v7 = (const char *)(*(_QWORD *)(a1 + 40) + *v5);
if ( (_WORD)v6 == 0xFFF1 )
{
print_on_level(4LL, "elf: Absolute symbol: %32s 0x%08lx\n", v7, *((_QWORD *)v5 + 1));
}
else
{
if ( (_WORD)v6 == 0xFFF2 )
{
print_on_level(4LL, "elf: Common symbol: %s\n", v7);
print_on_level(1LL, "Error (%s:%d): elf: Please compile with -fno-common\n", "src/lib/arch/x86/elf.c", 88LL);
return 4294967288LL;
}
if ( (_WORD)v6 )
{
v8 = *((_QWORD *)v5 + 1);
v9 = v8 + *(_QWORD *)(*(_QWORD *)(a1 + 24) + (v6 << 6) + 16);
*((_QWORD *)v5 + 1) = v9;
print_on_level(4LL, "elf: sym %32s %8lx -> %8lx\n", v7, v8, v9);
}
else
{
v10 = (const char *)(*(_QWORD *)(a1 + 40) + *v5);
v11 = *((_QWORD *)v5 + 1);
if ( strcmp(v10, "_GLOBAL_OFFSET_TABLE_") )
{
print_on_level(
1LL,
"Error (%s:%d): elf: Unknown symbol: %32s 0x%08lx\n",
"src/lib/arch/x86/elf.c",
96LL,
v7,
v11);
return 4294967294LL;
}
print_on_level(4LL, "elf: Undef symbol: %32s 0x%08lx\n", v7, v11);
}
}
v4 = v3;
if ( v3 >= *(_QWORD *)(v1 + 32) / 0x18uLL )
return 0LL;
++v3;
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
unsigned __int64 v1; // r15
__int64 v2; // rbx
unsigned int v3; // er13
__int64 v4; // rcx
unsigned int *v5; // r9
__int64 v6; // rax
const char *v7; // rdx
__int64 v8; // rcx
__int64 v9; // r8
const char *v10; // rsi
__int64 v11; // r9

v1 = *(_QWORD *)(a1 + 24) + ((unsigned __int64)*(unsigned int *)(a1 + 48) << 6);
v2 = *(_QWORD *)(v1 + 16);
if ( *(_QWORD *)(v1 + 32) > 0x2FuLL )
{
v3 = 2;
v4 = 1LL;
while ( 1 )
{
v5 = (unsigned int *)(v2 + 24 * v4);
v6 = *((unsigned __int16 *)v5 + 3);
v7 = (const char *)(*(_QWORD *)(a1 + 40) + *v5);
if ( (_WORD)v6 == 0xFFF1 )
{
print_on_level(4LL, "elf: Absolute symbol: %32s 0x%08lx\n", v7, *((_QWORD *)v5 + 1));
}
else
{
if ( (_WORD)v6 == 0xFFF2 )
{
print_on_level(4LL, "elf: Common symbol: %s\n", v7);
print_on_level(1LL, "Error (%s:%d): elf: Please compile with -fno-common\n", "src/lib/arch/x86/elf.c", 88LL);
return 4294967288LL;
}
if ( (_WORD)v6 )
{
v8 = *((_QWORD *)v5 + 1);
v9 = v8 + *(_QWORD *)(*(_QWORD *)(a1 + 24) + (v6 << 6) + 16);
*((_QWORD *)v5 + 1) = v9;
print_on_level(4LL, "elf: sym %32s %8lx -> %8lx\n", v7, v8, v9);
}
else
{
v10 = (const char *)(*(_QWORD *)(a1 + 40) + *v5);
v11 = *((_QWORD *)v5 + 1);
if ( strcmp(v10, "_GLOBAL_OFFSET_TABLE_") )
{
print_on_level(
1LL,
"Error (%s:%d): elf: Unknown symbol: %32s 0x%08lx\n",
"src/lib/arch/x86/elf.c",
96LL,
v7,
v11);
return 4294967294LL;
}
print_on_level(4LL, "elf: Undef symbol: %32s 0x%08lx\n", v7, v11);
}
}
v4 = v3;
if ( v3 >= *(_QWORD *)(v1 + 32) / 0x18uLL )
return 0LL;
++v3;
}
}
return 0LL;
}

original_code:
static int simplify_symbols(load_info_t *info)
{
 Elf64_Shdr *symsec = &info->sechdrs[info->index.sym];
 Elf64_Sym *sym = (void *)symsec->sh_addr;
 unsigned long secbase;
 unsigned int i;
 int ret = 0;
 for (i = 1; i < symsec->sh_size / sizeof(Elf64_Sym) && ret == 0; i++) {
  const char *name = info->strtab + sym[i].st_name;
  switch (sym[i].st_shndx) {
  case 0xfff2:
   print_on_level((4), "elf: " "Common symbol: %s\n", name);
   print_on_level((1), "Error (%s:%d): " "elf: " "Please compile with -fno-common\n", "src/lib/arch/x86/elf.c", 88);
   ret = -8;
   break;
  case 0xfff1:
   print_on_level((4), "elf: " "Absolute symbol: %32s 0x%08lx\n", name, (long)sym[i].st_value);
   break;
  case 0:
   if (strcmp(name, "_GLOBAL_OFFSET_TABLE_")) {
    print_on_level((1), "Error (%s:%d): " "elf: " "Unknown symbol: %32s 0x%08lx\n", "src/lib/arch/x86/elf.c", 96, name, (long)sym[i].st_value);
    ret = -2;
   } else
    print_on_level((4), "elf: " "Undef symbol: %32s 0x%08lx\n", name, (long)sym[i].st_value);
   break;
  default:
   secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
   sym[i].st_value += secbase;
   print_on_level((4), "elf: " "sym %32s %8lx -> %8lx\n", name, sym[i].st_value - secbase, sym[i].st_value);
   break;
  }
 }
 return ret;
}

canonical_original_code:
static int simplify_symbols(struct load_info_s *info)
{
 struct Elf64_Shdr *symsec = &info->sechdrs[info->index.sym];
 struct Elf64_Sym *sym = (void *)symsec->sh_addr;
 unsigned long secbase;
 unsigned int i;
 int ret = 0;
 for (i = 1; i < symsec->sh_size / sizeof(Elf64_Sym) && ret == 0; i++) {
  char *name = info->strtab + sym[i].st_name;
  switch (sym[i].st_shndx) {
  case 0xfff2:
   print_on_level((4), "elf: " "Common symbol: %s\n", name);
   print_on_level((1), "Error (%s:%d): " "elf: " "Please compile with -fno-common\n", "src/lib/arch/x86/elf.c", 88);
   ret = -8;
   break;
  case 0xfff1:
   print_on_level((4), "elf: " "Absolute symbol: %32s 0x%08lx\n", name, (long)sym[i].st_value);
   break;
  case 0:
   if (strcmp(name, "_GLOBAL_OFFSET_TABLE_")) {
    print_on_level((1), "Error (%s:%d): " "elf: " "Unknown symbol: %32s 0x%08lx\n", "src/lib/arch/x86/elf.c", 96, name, (long)sym[i].st_value);
    ret = -2;
   } else
    print_on_level((4), "elf: " "Undef symbol: %32s 0x%08lx\n", name, (long)sym[i].st_value);
   break;
  default:
   secbase = info->sechdrs[sym[i].st_shndx].sh_addr;
   sym[i].st_value += secbase;
   print_on_level((4), "elf: " "sym %32s %8lx -> %8lx\n", name, sym[i].st_value - secbase, sym[i].st_value);
   break;
  }
 }
 return ret;
}

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

variable_types:
secbase
unsigned long

variable_types: ret int

variable_types:
name
char *

user_defined_types: struct Elf64_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned long sh_flags; unsigned long sh_addr; unsigned long sh_offset; unsigned long sh_size; unsigned int sh_link; unsigned int sh_info; unsigned long sh_addralign; unsigned long sh_entsize; }


decompiled_code:
__int64 __fastcall apply_relocations(__int64 a1)
{
__int64 v1; // rsi
unsigned int v2; // edx
unsigned int v3; // er8
__int64 v5; // rbp
unsigned int v6; // ebx
__int64 v7; // rdi
__int64 v8; // rax
int v9; // eax

v1 = *(_QWORD *)(a1 + 8);
v2 = *(unsigned __int16 *)(v1 + 60);
if ( v2 > 1 )
{
v3 = 0;
v5 = 64LL;
v6 = 1;
while ( 1 )
{
v7 = *(_QWORD *)(a1 + 24);
v8 = *(unsigned int *)(v7 + v5 + 44);
if ( v2 <= (unsigned int)v8 || (*(_BYTE *)(v7 + (v8 << 6) + 8) & 2) == 0 )
goto LABEL_3;
v9 = *(_DWORD *)(v7 + v5 + 4);
if ( v9 == 9 )
{
print_on_level(1LL, "Error (%s:%d): elf: SHT_REL unsupported\n", "src/lib/arch/x86/elf.c", 207LL);
return 4294967274LL;
}
if ( v9 == 4 )
{
v3 = apply_relocate_add_constprop_0(v7, *(_DWORD *)(a1 + 48), v6, *(_QWORD *)a1);
if ( (v3 & 0x80000000) != 0 )
return v3;
v1 = *(_QWORD *)(a1 + 8);
++v6;
v5 += 64LL;
v2 = *(unsigned __int16 *)(v1 + 60);
if ( v2 <= v6 )
return v3;
}
else
{
LABEL_3:
v2 = *(unsigned __int16 *)(v1 + 60);
++v6;
v5 += 64LL;
if ( v2 <= v6 )
return v3;
}
}
}
return 0LL;
}

original_code:
static int apply_relocations(const load_info_t *info)
{
 unsigned int i;
 int err = 0;
 for (i = 1; i < info->hdr->e_shnum; i++) {
  unsigned int infosec = info->sechdrs[i].sh_info;
  if (infosec >= info->hdr->e_shnum)
   continue;
  if (!(info->sechdrs[infosec].sh_flags & (1 << 1)))
   continue;
  if (info->sechdrs[i].sh_type == 9) {
   print_on_level((1), "Error (%s:%d): " "elf: " "SHT_REL unsupported\n", "src/lib/arch/x86/elf.c", 207);
   err = -22;
  } else if (info->sechdrs[i].sh_type == 4)
   err = apply_relocate_add(info->sechdrs, info->strtab,
       info->index.sym, i,
       info->addr_delta);
  if (err < 0)
   break;
 }
 return err;
}

canonical_original_code:
static int apply_relocations(struct load_info_s *info)
{
 unsigned int i;
 int err = 0;
 for (i = 1; i < info->hdr->e_shnum; i++) {
  unsigned int infosec = info->sechdrs[i].sh_info;
  if (infosec >= info->hdr->e_shnum)
   continue;
  if (!(info->sechdrs[infosec].sh_flags & (1 << 1)))
   continue;
  if (info->sechdrs[i].sh_type == 9) {
   print_on_level((1), "Error (%s:%d): " "elf: " "SHT_REL unsupported\n", "src/lib/arch/x86/elf.c", 207);
   err = -22;
  } else if (info->sechdrs[i].sh_type == 4)
   err = apply_relocate_add(info->sechdrs, info->strtab,
       info->index.sym, i,
       info->addr_delta);
  if (err < 0)
   break;
 }
 return err;
}

variable_types: err int

variable_types:
infosec
unsigned int


decompiled_code:
__int64 __fastcall load_elf_headers(_QWORD *a1)
{
__int64 result; // rax

result = elf_header_check_isra_0(a1[1], a1[2]);
if ( !(_DWORD)result )
{
result = setup_load_headers((__int64)a1);
if ( !(_DWORD)result )
{
result = rewrite_section_headers(a1);
if ( !(_DWORD)result )
result = simplify_symbols((__int64)a1);
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(_QWORD *a1)
{
__int64 result; // rax

result = func5(a1[1], a1[2]);
if ( !(_DWORD)result )
{
result = func0((__int64)a1);
if ( !(_DWORD)result )
{
result = func1(a1);
if ( !(_DWORD)result )
result = func2((__int64)a1);
}
}
return result;
}

original_code:
static int load_elf_headers(load_info_t *info)
{
 int err = 0;
 if (!err)
  err = elf_header_check(info);
 if (!err)
  err = setup_load_headers(info);
 if (!err)
  err = rewrite_section_headers(info);
 if (!err)
  err = simplify_symbols(info);
 return err;
}

canonical_original_code:
static int load_elf_headers(struct load_info_s *info)
{
 int err = 0;
 if (!err)
  err = elf_header_check(info);
 if (!err)
  err = setup_load_headers(info);
 if (!err)
  err = rewrite_section_headers(info);
 if (!err)
  err = simplify_symbols(info);
 return err;
}


decompiled_code:
__int64 __fastcall lookup_elf_plugin_symbol(__int64 a1, const char *a2, char a3)
{
unsigned __int64 v3; // rax
__int64 v4; // r12
__int64 v5; // r13
unsigned __int64 v6; // rbx
unsigned int v7; // er14
__int64 v8; // rax
unsigned int *v9; // r15
__int64 result; // rax

v3 = *(_QWORD *)(a1 + 24) + ((unsigned __int64)*(unsigned int *)(a1 + 48) << 6);
v4 = *(_QWORD *)(v3 + 16);
if ( *(_QWORD *)(v3 + 32) <= 0x2FuLL )
return 0LL;
v5 = *(_QWORD *)(a1 + 40);
v6 = *(_QWORD *)(v3 + 32) / 0x18uLL;
v7 = 1;
v8 = 1LL;
while ( 1 )
{
v9 = (unsigned int *)(v4 + 24 * v8);
if ( !strcmp((const char *)(v5 + *v9), a2) )
break;
v8 = ++v7;
if ( v7 >= v6 )
return 0LL;
}
result = *((_QWORD *)v9 + 1);
if ( !a3 )
result += *(_QWORD *)a1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, const char *a2, char a3)
{
unsigned __int64 v3; // rax
__int64 v4; // r12
__int64 v5; // r13
unsigned __int64 v6; // rbx
unsigned int v7; // er14
__int64 v8; // rax
unsigned int *v9; // r15
__int64 result; // rax

v3 = *(_QWORD *)(a1 + 24) + ((unsigned __int64)*(unsigned int *)(a1 + 48) << 6);
v4 = *(_QWORD *)(v3 + 16);
if ( *(_QWORD *)(v3 + 32) <= 0x2FuLL )
return 0LL;
v5 = *(_QWORD *)(a1 + 40);
v6 = *(_QWORD *)(v3 + 32) / 0x18uLL;
v7 = 1;
v8 = 1LL;
while ( 1 )
{
v9 = (unsigned int *)(v4 + 24 * v8);
if ( !strcmp((const char *)(v5 + *v9), a2) )
break;
v8 = ++v7;
if ( v7 >= v6 )
return 0LL;
}
result = *((_QWORD *)v9 + 1);
if ( !a3 )
result += *(_QWORD *)a1;
return result;
}

original_code:
unsigned long lookup_elf_plugin_symbol(load_info_t *info,
           const char *symbol_name,
           _Bool local)
{
 Elf64_Shdr *symsec = &info->sechdrs[info->index.sym];
 Elf64_Sym *sym = (void *)symsec->sh_addr;
 unsigned int i;
 for (i = 1; i < symsec->sh_size / sizeof(Elf64_Sym); i++) {
  const char *name = info->strtab + sym[i].st_name;
  if (!strcmp(name, symbol_name)) {
   if (local)
    return sym[i].st_value;
   else
    return sym[i].st_value + info->addr_delta;
  }
 }
 return 0;
}

canonical_original_code:
unsigned long lookup_elf_plugin_symbol(struct load_info_s *info, char *symbol_name, _Bool local)
{
 struct Elf64_Shdr *symsec = &info->sechdrs[info->index.sym];
 struct Elf64_Sym *sym = (void *)symsec->sh_addr;
 unsigned int i;
 for (i = 1; i < symsec->sh_size / sizeof(Elf64_Sym); i++) {
  char *name = info->strtab + sym[i].st_name;
  if (!strcmp(name, symbol_name)) {
   if (local)
    return sym[i].st_value;
   else
    return sym[i].st_value + info->addr_delta;
  }
 }
 return 0;
}

return_type: unsigned long

user_defined_types: struct Elf64_Sym { unsigned int st_name; unsigned char st_info; unsigned char st_other; unsigned short st_shndx; unsigned long st_value; unsigned long st_size; }


decompiled_code:
__int64 __fastcall fixup_init(__int64 a1)
{
__int64 v1; // rax
__int64 v2; // rbx
_QWORD *v3; // rax
_QWORD *v4; // rax

v1 = *(unsigned int *)(a1 + 64);
v2 = *(_QWORD *)(a1 + 24) + (v1 << 6);
if ( !(_DWORD)v1 )
return 4294967294LL;
v3 = (_QWORD *)lookup_elf_plugin_symbol(a1, "__export_std_plugin_begin", 1);
if ( !v3 )
return 4294967294LL;
*v3 = *(_QWORD *)(v2 + 16) + *(_QWORD *)a1;
v4 = (_QWORD *)lookup_elf_plugin_symbol(a1, "__export_std_plugin_size", 1);
if ( !v4 )
return 4294967294LL;
*v4 = *(_QWORD *)(v2 + 32);
return 0LL;
}

original_code:
static int fixup_init(load_info_t *info)
{
 Elf64_Shdr *initsec = &info->sechdrs[info->index.init];
 unsigned long *where;
 if (!info->index.init)
  return -2;
 where = (void *)lookup_elf_plugin_symbol(info, "__export_std_plugin_begin", 1);
 if (!where)
  return -2;
 *where = (unsigned long)initsec->sh_addr + info->addr_delta;
 where = (void *)lookup_elf_plugin_symbol(info, "__export_std_plugin_size", 1);
 if (!where)
  return -2;
 *where = (unsigned long)initsec->sh_size;
 return 0;
}

canonical_original_code:
static int fixup_init(struct load_info_s *info)
{
 struct Elf64_Shdr *initsec = &info->sechdrs[info->index.init];
 unsigned long *where;
 if (!info->index.init)
  return -2;
 where = (void *)lookup_elf_plugin_symbol(info, "__export_std_plugin_begin", 1);
 if (!where)
  return -2;
 *where = (unsigned long)initsec->sh_addr + info->addr_delta;
 where = (void *)lookup_elf_plugin_symbol(info, "__export_std_plugin_size", 1);
 if (!where)
  return -2;
 *where = (unsigned long)initsec->sh_size;
 return 0;
}

variable_types:
where
unsigned long *


decompiled_code:
__int64 __fastcall load_elf_plugin(_QWORD *a1)
{
__int64 result; // rax

result = load_elf_headers(a1);
if ( !(_DWORD)result )
{
result = apply_relocations((__int64)a1);
if ( !(_DWORD)result )
result = fixup_init((__int64)a1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(_QWORD *a1)
{
__int64 result; // rax

result = func6(a1);
if ( !(_DWORD)result )
{
result = func4((__int64)a1);
if ( !(_DWORD)result )
result = func8((__int64)a1);
}
return result;
}

original_code:
int load_elf_plugin(load_info_t *info)
{
 int err = 0;
 if (!err)
  err = load_elf_headers(info);
 if (!err)
  err = apply_relocations(info);
 if (!err)
  err = fixup_init(info);
 return err;
}

canonical_original_code:
int load_elf_plugin(struct load_info_s *info)
{
 int err = 0;
 if (!err)
  err = load_elf_headers(info);
 if (!err)
  err = apply_relocations(info);
 if (!err)
  err = fixup_init(info);
 return err;
}


decompiled_code:
__int64 __fastcall verify_elf_packed(const char *a1)
{
int v1; // eax
int v2; // ebp
unsigned int v3; // er12
void *addr[2]; // [rsp+0h] [rbp-F8h] BYREF
size_t len[2]; // [rsp+10h] [rbp-E8h]
__int128 v7; // [rsp+20h] [rbp-D8h]
__int128 v8; // [rsp+30h] [rbp-C8h]
__int64 v9; // [rsp+40h] [rbp-B8h]
struct stat64 buf; // [rsp+50h] [rbp-A8h] BYREF

v9 = 0LL;
*(_OWORD *)addr = 0LL;
*(_OWORD *)len = 0LL;
v7 = 0LL;
v8 = 0LL;
v1 = open64(a1, 0);
if ( v1 < 0 )
{
v3 = -1;
print_on_level(1LL, "Error (%s:%d): elf: Can't open %s: %m\n", "src/lib/arch/x86/elf.c", 340LL, a1);
}
else
{
v2 = v1;
if ( fstat64(v1, &buf) )
{
v3 = -1;
print_on_level(
1LL,
"Error (%s:%d): elf: Failed to obtain statistics on %s: %m\n",
"src/lib/arch/x86/elf.c",
345LL,
a1);
}
else
{
len[0] = buf.st_size;
addr[1] = mmap64(0LL, buf.st_size, 3, 2, v2, 0LL);
if ( addr[1] == (void *)-1LL )
{
v3 = -1;
print_on_level(1LL, "Error (%s:%d): elf: Can't map %s: %m\n", "src/lib/arch/x86/elf.c", 353LL, a1);
}
else
{
v3 = load_elf_headers(addr);
}
}
munmap(addr[1], len[0]);
close(v2);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func10(const char *a1)
{
int v1; // eax
int v2; // ebp
unsigned int v3; // er12
void *addr[2]; // [rsp+0h] [rbp-F8h] BYREF
size_t len[2]; // [rsp+10h] [rbp-E8h]
__int128 v7; // [rsp+20h] [rbp-D8h]
__int128 v8; // [rsp+30h] [rbp-C8h]
__int64 v9; // [rsp+40h] [rbp-B8h]
struct stat64 buf; // [rsp+50h] [rbp-A8h] BYREF

v9 = 0LL;
*(_OWORD *)addr = 0LL;
*(_OWORD *)len = 0LL;
v7 = 0LL;
v8 = 0LL;
v1 = open64(a1, 0);
if ( v1 < 0 )
{
v3 = -1;
print_on_level(1LL, "Error (%s:%d): elf: Can't open %s: %m\n", "src/lib/arch/x86/elf.c", 340LL, a1);
}
else
{
v2 = v1;
if ( fstat64(v1, &buf) )
{
v3 = -1;
print_on_level(
1LL,
"Error (%s:%d): elf: Failed to obtain statistics on %s: %m\n",
"src/lib/arch/x86/elf.c",
345LL,
a1);
}
else
{
len[0] = buf.st_size;
addr[1] = mmap64(0LL, buf.st_size, 3, 2, v2, 0LL);
if ( addr[1] == (void *)-1LL )
{
v3 = -1;
print_on_level(1LL, "Error (%s:%d): elf: Can't map %s: %m\n", "src/lib/arch/x86/elf.c", 353LL, a1);
}
else
{
v3 = func6(addr);
}
}
munmap(addr[1], len[0]);
close(v2);
}
return v3;
}

original_code:
int verify_elf_packed(char *path)
{
 int plugin_fd, ret = -1;
 load_info_t info = { };
 struct stat plugin_st;
 plugin_fd = open(path, 00);
 if (plugin_fd < 0) {
  print_on_level((1), "Error (%s:%d): " "elf: " "Can't open %s" ": %m\n", "src/lib/arch/x86/elf.c", 340, path);
  return -1;
 }
 if (fstat(plugin_fd, &plugin_st)) {
  print_on_level((1), "Error (%s:%d): " "elf: " "Failed to obtain statistics on %s" ": %m\n", "src/lib/arch/x86/elf.c", 345, path);
  goto err;
 }
 info.len = plugin_st.st_size;
 info.hdr = mmap(((void *)0), plugin_st.st_size, 0x1 | 0x2,
   0x02 | 0, plugin_fd, 0);
 if ((void *)info.hdr == ((void *) -1)) {
  print_on_level((1), "Error (%s:%d): " "elf: " "Can't map %s" ": %m\n", "src/lib/arch/x86/elf.c", 353, path);
  goto err;
 }
 ret = load_elf_headers(&info);
 if (ret)
  goto err;
err:
 munmap((void *)info.hdr, info.len);
 close(plugin_fd);
 return ret;
}

canonical_original_code:
int verify_elf_packed(char *path)
{
 int plugin_fd; int ret = -1;
 struct load_info_s info = { };
 struct stat plugin_st;
 plugin_fd = open(path, 00);
 if (plugin_fd < 0) {
  print_on_level((1), "Error (%s:%d): " "elf: " "Can't open %s" ": %m\n", "src/lib/arch/x86/elf.c", 340, path);
  return -1;
 }
 if (fstat(plugin_fd, &plugin_st)) {
  print_on_level((1), "Error (%s:%d): " "elf: " "Failed to obtain statistics on %s" ": %m\n", "src/lib/arch/x86/elf.c", 345, path);
  goto err;
 }
 info.len = plugin_st.st_size;
 info.hdr = mmap(((void *)0), plugin_st.st_size, 0x1 | 0x2,
   0x02 | 0, plugin_fd, 0);
 if ((void *)info.hdr == ((void *) -1)) {
  print_on_level((1), "Error (%s:%d): " "elf: " "Can't map %s" ": %m\n", "src/lib/arch/x86/elf.c", 353, path);
  goto err;
 }
 ret = load_elf_headers(&info);
 if (ret)
  goto err;
err:
 munmap((void *)info.hdr, info.len);
 close(plugin_fd);
 return ret;
}

variable_types:
path
char *

variable_types: struct load_info_s { long addr_delta; struct Elf64_Ehdr { unsigned char e_ident[-1]; unsigned short e_type; unsigned short e_machine; unsigned int e_version; unsigned long e_entry; unsigned long e_phoff; unsigned long e_shoff; unsigned int e_flags; unsigned short e_ehsize; unsigned short e_phentsize; unsigned short e_phnum; unsigned short e_shentsize; unsigned short e_shnum; unsigned short e_shstrndx; } *hdr; unsigned long len; struct Elf64_Shdr { unsigned int sh_name; unsigned int sh_type; unsigned long sh_flags; unsigned long sh_addr; unsigned long sh_offset; unsigned long sh_size; unsigned int sh_link; unsigned int sh_info; unsigned long sh_addralign; unsigned long sh_entsize; } *sechdrs; char *secstrings; char *strtab; struct { unsigned int sym; unsigned int str; unsigned int symtab; unsigned int symtab_link; unsigned int init; } index; }

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


decompiled_code:
__int64 __fastcall init_socket(struct sockaddr *a1)
{
__int64 result; // rax

result = sys_socket(1, 5, 0);
ctl_socket = result;
if ( (int)result >= 0 )
{
result = sys_connect(result, a1, *(_DWORD *)&a1[7].sa_family);
if ( (int)result > 0 )
result = 0LL;
}
return result;
}

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

result = sys_socket(1, 5, 0);
ctl_socket = result;
if ( (int)result >= 0 )
{
result = sys_connect(result, a1, *(_DWORD *)&a1[7].sa_family);
if ( (int)result > 0 )
result = 0LL;
}
return result;
}

original_code:
static int init_socket(struct prologue_init_args *args)
{
 int ret;
 ctl_socket = sys_socket(1, SOCK_SEQPACKET, 0);
 if (ctl_socket < 0)
  return ctl_socket;
 ret = sys_connect(ctl_socket, (struct sockaddr *)&args->ctl_sock_addr, args->ctl_sock_addr_len);
 if (ret < 0)
  return ret;
 return 0;
}

variable_types: struct prologue_init_args { struct sockaddr_un { unsigned short sun_family; char sun_path[108]; } ctl_sock_addr; unsigned int ctl_sock_addr_len; unsigned int arg_s; void *arg_p; void *sigframe; } *

user_defined_types: struct prologue_init_args { struct sockaddr_un ctl_sock_addr; unsigned int ctl_sock_addr_len; unsigned int arg_s; void *arg_p; void *sigframe; }

user_defined_types: struct sockaddr { unsigned short sa_family; char sa_data[14]; }


decompiled_code:
__int64 std_ctl_sock()
{
return ctl_socket;
}

canonical_decompiled_code:
__int64 func2()
{
return ctl_socket;
}

original_code:
int std_ctl_sock(void)
{
 return ctl_socket;
}

canonical_original_code:
int std_ctl_sock()
{
 return ctl_socket;
}


original_code:
int parasite_syscall_seized(parasite_ctl_t *ctl,
       int nr, unsigned long *ret,
       unsigned long arg1,
       unsigned long arg2,
       unsigned long arg3,
       unsigned long arg4,
       unsigned long arg5,
       unsigned long arg6)
{
 user_regs_struct_t regs = ctl->thread_ctx_orig.regs;
 int err;
 regs.ax = (unsigned long)nr;
 regs.di = arg1;
 regs.si = arg2;
 regs.dx = arg3;
 regs.r10 = arg4;
 regs.r8 = arg5;
 regs.r9 = arg6;
 err = parasite_execute_syscall_trap(ctl, &regs);
 *ret = regs.ax;
 return err;
}

canonical_original_code:
int parasite_syscall_seized(struct parasite_ctl_s *ctl, int nr, unsigned long *ret, unsigned long arg1, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5, unsigned long arg6)
{
 struct user_regs_struct_t regs = ctl->thread_ctx_orig.regs;
 int err;
 regs.ax = (unsigned long)nr;
 regs.di = arg1;
 regs.si = arg2;
 regs.dx = arg3;
 regs.r10 = arg4;
 regs.r8 = arg5;
 regs.r9 = arg6;
 err = parasite_execute_syscall_trap(ctl, &regs);
 *ret = regs.ax;
 return err;
}

variable_types: struct parasite_ctl_s { int pid; int task_state; unsigned long syscall_ip; unsigned long entry_ip; int ctl_sock; int ctl_accepted; struct thread_ctx_t { struct k_rtsigset_t { unsigned long sig[-1]; } sigmask; struct user_regs_struct_t { unsigned long r15; unsigned long r14; unsigned long r13; unsigned long r12; unsigned long bp; unsigned long bx; unsigned long r11; unsigned long r10; unsigned long r9; unsigned long r8; unsigned long ax; unsigned long cx; unsigned long dx; unsigned long si; unsigned long di; unsigned long orig_ax; unsigned long ip; unsigned long cs; unsigned long flags; unsigned long sp; unsigned long ss; unsigned long fs_base; unsigned long gs_base; unsigned long ds; unsigned long es; unsigned long fs; unsigned long gs; } regs; struct fpu_state_t { union { struct xsave_struct { struct i387_fxsave_struct { unsigned short cwd; unsigned short swd; unsigned short twd; unsigned short fop; union { struct { unsigned long rip; unsigned long rdp; }; struct { unsigned int fip; unsigned int fcs; unsigned int foo; unsigned int fos; }; }; unsigned int mxcsr; unsigned int mxcsr_mask; unsigned int st_space[32]; unsigned int xmm_space[64]; unsigned int padding[12]; union { unsigned int padding1[12]; unsigned int sw_reserved[12]; }; } i387; struct xsave_hdr_struct { unsigned long xstate_bv; unsigned long reserved1[2]; unsigned long reserved2[5]; } xsave_hdr; struct ymmh_struct { unsigned int ymmh_space[64]; } ymmh; } xsave; unsigned char __pad[-1]; }; _Bool has_xsave; } fpu; } thread_ctx_orig; unsigned char code_orig[8]; void *remote_map; void *local_map; unsigned long map_length; } *

variable_types: nr int

variable_types:
ret
unsigned long *

user_defined_types: struct parasite_ctl_s { int pid; int task_state; unsigned long syscall_ip; unsigned long entry_ip; int ctl_sock; int ctl_accepted; struct thread_ctx_t thread_ctx_orig; unsigned char code_orig[8]; void *remote_map; void *local_map; unsigned long map_length; }

user_defined_types: struct thread_ctx_t { struct k_rtsigset_t sigmask; struct user_regs_struct_t regs; struct fpu_state_t fpu; }


original_code:
void parasite_fixup_thread_ctx(thread_ctx_t *ctx)
{
 if ((int)ctx->regs.orig_ax >= 0) {
  switch ((int)ctx->regs.ax) {
  case -514:
  case -512:
  case -513:
   ctx->regs.ax = ctx->regs.orig_ax;
   ctx->regs.ip -= 2;
   break;
  case -516:
   ctx->regs.ax = 219;
   ctx->regs.ip -= 2;
   break;
  }
 }
}

canonical_original_code:
void parasite_fixup_thread_ctx(struct thread_ctx_t *ctx)
{
 if ((int)ctx->regs.orig_ax >= 0) {
  switch ((int)ctx->regs.ax) {
  case -514:
  case -512:
  case -513:
   ctx->regs.ax = ctx->regs.orig_ax;
   ctx->regs.ip -= 2;
   break;
  case -516:
   ctx->regs.ax = 219;
   ctx->regs.ip -= 2;
   break;
  }
 }
}

variable_types: struct thread_ctx_t { struct k_rtsigset_t { unsigned long sig[-1]; } sigmask; struct user_regs_struct_t { unsigned long r15; unsigned long r14; unsigned long r13; unsigned long r12; unsigned long bp; unsigned long bx; unsigned long r11; unsigned long r10; unsigned long r9; unsigned long r8; unsigned long ax; unsigned long cx; unsigned long dx; unsigned long si; unsigned long di; unsigned long orig_ax; unsigned long ip; unsigned long cs; unsigned long flags; unsigned long sp; unsigned long ss; unsigned long fs_base; unsigned long gs_base; unsigned long ds; unsigned long es; unsigned long fs; unsigned long gs; } regs; struct fpu_state_t { union { struct xsave_struct { struct i387_fxsave_struct { unsigned short cwd; unsigned short swd; unsigned short twd; unsigned short fop; union { struct { unsigned long rip; unsigned long rdp; }; struct { unsigned int fip; unsigned int fcs; unsigned int foo; unsigned int fos; }; }; unsigned int mxcsr; unsigned int mxcsr_mask; unsigned int st_space[32]; unsigned int xmm_space[64]; unsigned int padding[12]; union { unsigned int padding1[12]; unsigned int sw_reserved[12]; }; } i387; struct xsave_hdr_struct { unsigned long xstate_bv; unsigned long reserved1[2]; unsigned long reserved2[5]; } xsave_hdr; struct ymmh_struct { unsigned int ymmh_space[64]; } ymmh; } xsave; unsigned char __pad[-1]; }; _Bool has_xsave; } fpu; } *


original_code:
void parasite_prepare_sigframe(parasite_ctl_t *c, unsigned long sigframe_at)
{
 rt_sigframe_t *sigframe;
 sigframe = (void *)sigframe_at;
 memset(sigframe, 0, sizeof(*sigframe));
 sigframe->uc.uc_mcontext.r8 = c->thread_ctx_orig.regs.r8;
 sigframe->uc.uc_mcontext.r9 = c->thread_ctx_orig.regs.r9;
 sigframe->uc.uc_mcontext.r10 = c->thread_ctx_orig.regs.r10;
 sigframe->uc.uc_mcontext.r11 = c->thread_ctx_orig.regs.r11;
 sigframe->uc.uc_mcontext.r12 = c->thread_ctx_orig.regs.r12;
 sigframe->uc.uc_mcontext.r13 = c->thread_ctx_orig.regs.r13;
 sigframe->uc.uc_mcontext.r14 = c->thread_ctx_orig.regs.r14;
 sigframe->uc.uc_mcontext.r15 = c->thread_ctx_orig.regs.r15;
 sigframe->uc.uc_mcontext.rdi = c->thread_ctx_orig.regs.di;
 sigframe->uc.uc_mcontext.rsi = c->thread_ctx_orig.regs.si;
 sigframe->uc.uc_mcontext.rbp = c->thread_ctx_orig.regs.bp;
 sigframe->uc.uc_mcontext.rbx = c->thread_ctx_orig.regs.bx;
 sigframe->uc.uc_mcontext.rdx = c->thread_ctx_orig.regs.dx;
 sigframe->uc.uc_mcontext.rax = c->thread_ctx_orig.regs.ax;
 sigframe->uc.uc_mcontext.rcx = c->thread_ctx_orig.regs.cx;
 sigframe->uc.uc_mcontext.rsp = c->thread_ctx_orig.regs.sp;
 sigframe->uc.uc_mcontext.rip = c->thread_ctx_orig.regs.ip;
 sigframe->uc.uc_mcontext.eflags = c->thread_ctx_orig.regs.flags;
 sigframe->uc.uc_mcontext.cs = c->thread_ctx_orig.regs.cs;
 sigframe->uc.uc_mcontext.gs = c->thread_ctx_orig.regs.gs;
 sigframe->uc.uc_mcontext.fs = c->thread_ctx_orig.regs.fs;
 sigframe->uc.uc_sigmask = c->thread_ctx_orig.sigmask;
 sigframe->fpu_state = c->thread_ctx_orig.fpu;
}

canonical_original_code:
void parasite_prepare_sigframe(struct parasite_ctl_s *c, unsigned long sigframe_at)
{
 struct rt_sigframe *sigframe;
 sigframe = (void *)sigframe_at;
 memset(sigframe, 0, sizeof(*sigframe));
 sigframe->uc.uc_mcontext.r8 = c->thread_ctx_orig.regs.r8;
 sigframe->uc.uc_mcontext.r9 = c->thread_ctx_orig.regs.r9;
 sigframe->uc.uc_mcontext.r10 = c->thread_ctx_orig.regs.r10;
 sigframe->uc.uc_mcontext.r11 = c->thread_ctx_orig.regs.r11;
 sigframe->uc.uc_mcontext.r12 = c->thread_ctx_orig.regs.r12;
 sigframe->uc.uc_mcontext.r13 = c->thread_ctx_orig.regs.r13;
 sigframe->uc.uc_mcontext.r14 = c->thread_ctx_orig.regs.r14;
 sigframe->uc.uc_mcontext.r15 = c->thread_ctx_orig.regs.r15;
 sigframe->uc.uc_mcontext.rdi = c->thread_ctx_orig.regs.di;
 sigframe->uc.uc_mcontext.rsi = c->thread_ctx_orig.regs.si;
 sigframe->uc.uc_mcontext.rbp = c->thread_ctx_orig.regs.bp;
 sigframe->uc.uc_mcontext.rbx = c->thread_ctx_orig.regs.bx;
 sigframe->uc.uc_mcontext.rdx = c->thread_ctx_orig.regs.dx;
 sigframe->uc.uc_mcontext.rax = c->thread_ctx_orig.regs.ax;
 sigframe->uc.uc_mcontext.rcx = c->thread_ctx_orig.regs.cx;
 sigframe->uc.uc_mcontext.rsp = c->thread_ctx_orig.regs.sp;
 sigframe->uc.uc_mcontext.rip = c->thread_ctx_orig.regs.ip;
 sigframe->uc.uc_mcontext.eflags = c->thread_ctx_orig.regs.flags;
 sigframe->uc.uc_mcontext.cs = c->thread_ctx_orig.regs.cs;
 sigframe->uc.uc_mcontext.gs = c->thread_ctx_orig.regs.gs;
 sigframe->uc.uc_mcontext.fs = c->thread_ctx_orig.regs.fs;
 sigframe->uc.uc_sigmask = c->thread_ctx_orig.sigmask;
 sigframe->fpu_state = c->thread_ctx_orig.fpu;
}

variable_types: struct rt_sigframe { char *pretcode; struct rt_ucontext { unsigned long uc_flags; struct rt_ucontext *uc_link; struct rt_sigaltstack { void *ss_sp; int ss_flags; unsigned long ss_size; } uc_stack; struct rt_sigcontext { unsigned long r8; unsigned long r9; unsigned long r10; unsigned long r11; unsigned long r12; unsigned long r13; unsigned long r14; unsigned long r15; unsigned long rdi; unsigned long rsi; unsigned long rbp; unsigned long rbx; unsigned long rdx; unsigned long rax; unsigned long rcx; unsigned long rsp; unsigned long rip; unsigned long eflags; unsigned short cs; unsigned short gs; unsigned short fs; unsigned short __pad0; unsigned long err; unsigned long trapno; unsigned long oldmask; unsigned long cr2; void *fpstate; unsigned long reserved1[8]; } uc_mcontext; struct k_rtsigset_t { unsigned long sig[-1]; } uc_sigmask; int __unused[-1]; unsigned long uc_regspace[128]; } uc; struct rt_siginfo { int si_signo; int si_errno; int si_code; int _pad[-1]; } info; struct fpu_state_t { union { struct xsave_struct { struct i387_fxsave_struct { unsigned short cwd; unsigned short swd; unsigned short twd; unsigned short fop; union { struct { unsigned long rip; unsigned long rdp; }; struct { unsigned int fip; unsigned int fcs; unsigned int foo; unsigned int fos; }; }; unsigned int mxcsr; unsigned int mxcsr_mask; unsigned int st_space[32]; unsigned int xmm_space[64]; unsigned int padding[12]; union { unsigned int padding1[12]; unsigned int sw_reserved[12]; }; } i387; struct xsave_hdr_struct { unsigned long xstate_bv; unsigned long reserved1[2]; unsigned long reserved2[5]; } xsave_hdr; struct ymmh_struct { unsigned int ymmh_space[64]; } ymmh; } xsave; unsigned char __pad[-1]; }; _Bool has_xsave; } fpu_state; } *

user_defined_types: struct rt_sigframe { char *pretcode; struct rt_ucontext uc; struct rt_siginfo info; struct fpu_state_t fpu_state; }

user_defined_types: struct rt_ucontext { unsigned long uc_flags; struct rt_ucontext *uc_link; struct rt_sigaltstack uc_stack; struct rt_sigcontext uc_mcontext; struct k_rtsigset_t uc_sigmask; int __unused[-1]; unsigned long uc_regspace[128]; }


original_code:
unsigned long std_strtoul(const char *nptr, char **endptr, int base)
{
 const char *s = nptr;
 _Bool neg = 0;
 unsigned int v;
 long num = 0;
 if (base < 0 || base == 1 || base > 36)
  goto fin;
 while (__isspace(*s))
  s++;
 if (!*s)
  goto fin;
 if (*s == '-')
  neg = 1, s++;
 if (base == 0) {
  if (s[0] == '0') {
   unsigned char p = __tolower(s[1]);
   switch (p) {
   case 'b':
    base = 2, s += 2;
    break;
   case 'x':
    base = 16, s += 2;
    break;
   default:
    base = 8, s += 1;
    break;
   }
  } else
   base = 10;
 } else if (base == 16) {
  if (s[0] == '0' && __tolower(s[1]) == 'x')
   s += 2;
 }
 for (; *s; s++) {
  if (__isspace(*s))
   continue;
  if (!__isalnum(*s))
      goto fin;
  v = __conv_val(*s);
  if (v == -1u || v > base)
   goto fin;
  num *= base;
  num += v;
 }
fin:
 if (endptr)
  *endptr = (char *)s;
 return neg ? (unsigned long)-num : (unsigned long)num;
}

canonical_original_code:
unsigned long std_strtoul(char *nptr, char **endptr, int base)
{
 char *s = nptr;
 _Bool neg = 0;
 unsigned int v;
 long num = 0;
 if (base < 0 || base == 1 || base > 36)
  goto fin;
 while (__isspace(*s))
  s++;
 if (!*s)
  goto fin;
 if (*s == '-')
  neg = 1, s++;
 if (base == 0) {
  if (s[0] == '0') {
   unsigned char p = __tolower(s[1]);
   switch (p) {
   case 'b':
    base = 2, s += 2;
    break;
   case 'x':
    base = 16, s += 2;
    break;
   default:
    base = 8, s += 1;
    break;
   }
  } else
   base = 10;
 } else if (base == 16) {
  if (s[0] == '0' && __tolower(s[1]) == 'x')
   s += 2;
 }
 for (; *s; s++) {
  if (__isspace(*s))
   continue;
  if (!__isalnum(*s))
      goto fin;
  v = __conv_val(*s);
  if (v == -1u || v > base)
   goto fin;
  num *= base;
  num += v;
 }
fin:
 if (endptr)
  *endptr = (char *)s;
 return neg ? (unsigned long)-num : (unsigned long)num;
}

variable_types:
nptr
char *

variable_types:
endptr
char **

variable_types: base int

variable_types:
s
char *

variable_types:
v
unsigned int

variable_types: num long

variable_types:
p
unsigned char


decompiled_code:
__int64 __fastcall std_memcmp(__int64 a1, __int64 a2, __int64 a3)
{
__int64 i; // rax
unsigned int v6; // edx

if ( !a3 )
return 0;
for ( i = 0LL; i != a3; ++i )
{
v6 = *(unsigned __int8 *)(a1 + i) - *(unsigned __int8 *)(a2 + i);
if ( v6 )
break;
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2, __int64 a3)
{
__int64 i; // rax
unsigned int v6; // edx

if ( !a3 )
return 0;
for ( i = 0LL; i != a3; ++i )
{
v6 = *(unsigned __int8 *)(a1 + i) - *(unsigned __int8 *)(a2 + i);
if ( v6 )
break;
}
return v6;
}

original_code:
int std_memcmp(const void *cs, const void *ct, size_t count)
{
 const unsigned char *su1, *su2;
 int res = 0;
 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  if ((res = *su1 - *su2) != 0)
   break;
 return res;
}

canonical_original_code:
int std_memcmp(void *cs, void *ct, unsigned long count)
{
 unsigned char *su1; unsigned char *su2;
 int res = 0;
 for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
  if ((res = *su1 - *su2) != 0)
   break;
 return res;
}

variable_types:
cs
void *

variable_types:
ct
void *

variable_types:
count
unsigned long

variable_types: res int


original_code:
int std_strcmp(const char *cs, const char *ct)
{
 unsigned char c1, c2;
 while (1) {
  c1 = *cs++;
  c2 = *ct++;
  if (c1 != c2)
   return c1 < c2 ? -1 : 1;
  if (!c1)
   break;
 }
 return 0;
}

canonical_original_code:
int std_strcmp(char *cs, char *ct)
{
 unsigned char c1; unsigned char c2;
 while (1) {
  c1 = *cs++;
  c2 = *ct++;
  if (c1 != c2)
   return c1 < c2 ? -1 : 1;
  if (!c1)
   break;
 }
 return 0;
}

variable_types:
cs
char *

variable_types:
ct
char *