decompiled_code:
int check_stdin_ready()
{
struct timeval v1; // [rsp+0h] [rbp-98h] BYREF
fd_set readfds; // [rsp+10h] [rbp-88h] BYREF

memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[0] |= 1uLL;
v1.tv_sec = 0LL;
v1.tv_usec = 0LL;
return select(1, &readfds, 0LL, 0LL, &v1);
}

canonical_decompiled_code:
int func1()
{
struct timeval v1; // [rsp+0h] [rbp-98h] BYREF
fd_set readfds; // [rsp+10h] [rbp-88h] BYREF

memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[0] |= 1uLL;
v1.tv_sec = 0LL;
v1.tv_usec = 0LL;
return select(1, &readfds, 0LL, 0LL, &v1);
}

original_code:
static int check_stdin_ready()
{
 fd_set fds;
 struct timeval timeout;
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&fds)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
 timeout.tv_sec = 0;
 timeout.tv_usec = 0;
 return select(0 + 1, &fds, ((void *)0), ((void *)0), &timeout);
}

canonical_original_code:
static int check_stdin_ready()
{
 struct fd_set fds;
 struct timeval timeout;
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&fds)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
 timeout.tv_sec = 0;
 timeout.tv_usec = 0;
 return select(0 + 1, &fds, ((void *)0), ((void *)0), &timeout);
}

variable_types:
fds
struct fd_set { long __fds_bits[-1]; }

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


repo: vicnils/tetrominic

decompiled_code:
__int64 __fastcall read_into_buffer(__int64 a1, __int64 a2)
{
_BYTE *v2; // r12
int v3; // ebx
__int64 result; // rax
int v5; // eax

v2 = (_BYTE *)(a2 + a1 + 16);
v3 = check_stdin_ready();
memset(v2, 0, 16 - a2);
if ( v3 > 0 )
{
v3 = read(0, v2, 15 - a2);
if ( !v3 )
{
*v2 = 4;
return 1LL;
}
}
else if ( !v3 )
{
if ( a2 )
{
v3 = read(0, v2, 15 - a2);
if ( !v3 )
return 1LL;
}
}
result = v3;
if ( v3 < 0 )
{
v5 = *_errno_location();
if ( v5 != 4 && v5 != 11 )
{
print_error("Error reading from stdin");
restore_terminal();
exit(1);
}
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
_BYTE *v2; // r12
int v3; // ebx
__int64 result; // rax
int v5; // eax

v2 = (_BYTE *)(a2 + a1 + 16);
v3 = func1();
memset(v2, 0, 16 - a2);
if ( v3 > 0 )
{
v3 = read(0, v2, 15 - a2);
if ( !v3 )
{
*v2 = 4;
return 1LL;
}
}
else if ( !v3 )
{
if ( a2 )
{
v3 = read(0, v2, 15 - a2);
if ( !v3 )
return 1LL;
}
}
result = v3;
if ( v3 < 0 )
{
v5 = *_errno_location();
if ( v5 != 4 && v5 != 11 )
{
print_error("Error reading from stdin");
restore_terminal();
exit(1);
}
result = 0LL;
}
return result;
}

original_code:
static size_t read_into_buffer(struct terminal_input *inp, size_t len)
{
 char *buf = inp->next.s + len;
 size_t nbytes = sizeof(inp->next.s) - len;
 int ret = check_stdin_ready();
 memset(buf, '\0', nbytes);
 if (ret > 0) {
  ret = read(0, buf, nbytes - 1);
  if (ret == 0) {
   buf[0] = '\004';
   return 1;
  }
 } else if (ret == 0 && len > 0) {
  ret = read(0, buf, nbytes - 1);
  if (ret == 0) {
   return 1;
  }
 }
 if (ret >= 0) {
  return ret;
 }
 if ((*__errno_location ()) != 4 && (*__errno_location ()) != 11 && (*__errno_location ()) != 11) {
  print_error("Error reading from stdin");
  restore_terminal();
  exit(1);
 }
 return 0;
}

canonical_original_code:
static unsigned long read_into_buffer(struct terminal_input *inp, unsigned long len)
{
 char *buf = inp->next.s + len;
 unsigned long nbytes = sizeof(inp->next.s) - len;
 int ret = check_stdin_ready();
 memset(buf, '\0', nbytes);
 if (ret > 0) {
  ret = read(0, buf, nbytes - 1);
  if (ret == 0) {
   buf[0] = '\004';
   return 1;
  }
 } else if (ret == 0 && len > 0) {
  ret = read(0, buf, nbytes - 1);
  if (ret == 0) {
   return 1;
  }
 }
 if (ret >= 0) {
  return ret;
 }
 if ((*__errno_location ()) != 4 && (*__errno_location ()) != 11 && (*__errno_location ()) != 11) {
  print_error("Error reading from stdin");
  restore_terminal();
  exit(1);
 }
 return 0;
}

variable_types: struct terminal_input { struct terminal_seq_buffer { char s[16]; } current; unsigned long next_length; } *

variable_types:
len
unsigned long

variable_types:
buf
char *

variable_types:
nbytes
unsigned long

variable_types: ret int

return_type: unsigned long

user_defined_types: struct terminal_input { struct terminal_seq_buffer current; unsigned long next_length; }


original_code:
size_t read_terminal_seq(struct terminal_input *inp)
{
 char *buf = inp->next.s;
 size_t len = get_seq_len(buf, inp->next_length);
 if (len == 0) {
  inp->next_length = truncate_seq(buf, inp->next_length, 8);
  len = read_into_buffer(inp, inp->next_length);
  if (len > 0) {
   inp->next_length += len;
   len = get_seq_len(buf, inp->next_length);
  }
 }
 if (len > 0) {
  inp->current = inp->next;
  if (len < inp->next_length) {
   inp->next_length -= len;
  } else {
   inp->next_length = 0;
  }
  memcpy(buf, inp->current.s + len, inp->next_length + 1);
 }
 inp->current.s[len] = '\0';
 return len;
}

canonical_original_code:
unsigned long read_terminal_seq(struct terminal_input *inp)
{
 char *buf = inp->next.s;
 unsigned long len = get_seq_len(buf, inp->next_length);
 if (len == 0) {
  inp->next_length = truncate_seq(buf, inp->next_length, 8);
  len = read_into_buffer(inp, inp->next_length);
  if (len > 0) {
   inp->next_length += len;
   len = get_seq_len(buf, inp->next_length);
  }
 }
 if (len > 0) {
  inp->current = inp->next;
  if (len < inp->next_length) {
   inp->next_length -= len;
  } else {
   inp->next_length = 0;
  }
  memcpy(buf, inp->current.s + len, inp->next_length + 1);
 }
 inp->current.s[len] = '\0';
 return len;
}

user_defined_types: struct terminal_seq_buffer { char s[16]; }


decompiled_code:
const char *__fastcall read_terminal_keypress(__m128i *a1)
{
unsigned __int64 v1; // rdx
const char *result; // rax

v1 = read_terminal_seq(a1);
result = (const char *)a1;
if ( a1->m128i_i8[0] == 32 && !a1->m128i_i8[1] )
return "space";
if ( v1 == 3 && a1->m128i_i8[0] == 27 && a1->m128i_i8[1] == 91 && (unsigned __int8)(a1->m128i_i8[2] - 65) <= 3u )
result = CSWTCH_6[(unsigned __int8)(a1->m128i_i8[2] - 65)];
return result;
}

original_code:
const char *read_terminal_keypress(struct terminal_input *inp)
{
 size_t len = read_terminal_seq(inp);
 const char *s = inp->current.s;
 if (!strcmp(s, " ")) {
  return "space";
 }
 if (len == 3 && s[0] == '\033' && s[1] == '[') {
  switch (s[2]) {
  case 'A': return "up";
  case 'B': return "down";
  case 'C': return "right";
  case 'D': return "left";
  }
 }
 return s;
}

canonical_original_code:
char *read_terminal_keypress(struct terminal_input *inp)
{
 unsigned long len = read_terminal_seq(inp);
 char *s = inp->current.s;
 if (!strcmp(s, " ")) {
  return "space";
 }
 if (len == 3 && s[0] == '\033' && s[1] == '[') {
  switch (s[2]) {
  case 'A': return "up";
  case 'B': return "down";
  case 'C': return "right";
  case 'D': return "left";
  }
 }
 return s;
}

variable_types:
s
char *

return_type: char *


original_code:
static int is_empty_row(const struct tetgrid *grid, int row)
{
 return (grid->blocks[row] & grid->blocks[0]) == 0;
}

canonical_original_code:
static int is_empty_row(struct tetgrid *grid, int row)
{
 return (grid->blocks[row] & grid->blocks[0]) == 0;
}

variable_types: struct tetgrid { int cols; int clearing; int delay; unsigned short blocks[24]; } *

variable_types: row int

return_type: int


decompiled_code:
__int64 __fastcall invert_blocks(__int64 a1, int a2)
{
return ~(*(unsigned __int16 *)(a1 + 2LL * a2 + 12) | 1u);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, int a2)
{
return ~(*(unsigned __int16 *)(a1 + 2LL * a2 + 12) | 1u);
}

original_code:
static blocks_row invert_blocks(const struct tetgrid *grid, int row)
{
 blocks_row b = -1;
 return b ^ (grid->blocks[row] | 1);
}

canonical_original_code:
static unsigned short invert_blocks(struct tetgrid *grid, int row)
{
 unsigned short b = -1;
 return b ^ (grid->blocks[row] | 1);
}

variable_types:
b
unsigned short

return_type: unsigned short

user_defined_types: struct tetgrid { int cols; int clearing; int delay; unsigned short blocks[24]; }


original_code:
static blocks_row grow_cleared_blocks(struct tetgrid *grid, int row)
{
 blocks_row b = invert_blocks(grid, row);
 if (b)
  grid->blocks[row] &= ~((b << 1) | (b >> 1));
 else {
  b = (grid->cols & 1) ? 1 : 3;
  grid->blocks[row] ^= b << (grid->cols/2 + 3 - 1);
 }
 return (b ^ invert_blocks(grid, row)) >> 3;
}

canonical_original_code:
static unsigned short grow_cleared_blocks(struct tetgrid *grid, int row)
{
 unsigned short b = invert_blocks(grid, row);
 if (b)
  grid->blocks[row] &= ~((b << 1) | (b >> 1));
 else {
  b = (grid->cols & 1) ? 1 : 3;
  grid->blocks[row] ^= b << (grid->cols/2 + 3 - 1);
 }
 return (b ^ invert_blocks(grid, row)) >> 3;
}


original_code:
int update_line_clears(struct tetgrid *grid)
{
 int cleared = 0;
 int clearing = grid->clearing;
 int i;
 if (grid->delay > 0) {
  grid->delay--;
  return 0;
 }
 for (i=1; clearing > 0 && i < 24; i++) {
  if (!(grid->blocks[i] & 1)) {
   cleared = i;
   clearing--;
   grid->delay = 5;
  }
 }
 for (i = cleared + 1; i + 1 < 24; i++) {
  if (is_empty_row(grid, i) && !is_empty_row(grid, i + 1)) {
   grid->blocks[i] ^= 1;
   grid->clearing++;
  }
 }
 return cleared;
}

variable_types: cleared int

variable_types: clearing int

variable_types: i int


decompiled_code:
__int16 __fastcall shift_cleared_blocks(_DWORD *a1, int a2)
{
int v2; // er8

if ( !is_empty_row((__int64)a1, a2) )
return grow_cleared_blocks(a1, v2);
if ( v2 <= 22 )
memmove((char *)a1 + 2 * v2 + 12, (char *)a1 + 2 * v2 + 14, 2LL * (unsigned int)(23 - v2));
--a1[1];
return 0;
}

canonical_decompiled_code:
__int16 __fastcall func4(_DWORD *a1, int a2)
{
int v2; // er8

if ( !func0((__int64)a1, a2) )
return func2(a1, v2);
if ( v2 <= 22 )
memmove((char *)a1 + 2 * v2 + 12, (char *)a1 + 2 * v2 + 14, 2LL * (unsigned int)(23 - v2));
--a1[1];
return 0;
}

original_code:
blocks_row shift_cleared_blocks(struct tetgrid *grid, int row)
{
 if (is_empty_row(grid, row)) {
  for (; row + 1 < 24; row++) {
   grid->blocks[row] = grid->blocks[row + 1];
  }
  grid->clearing--;
  return 0;
 }
 return grow_cleared_blocks(grid, row);
}

canonical_original_code:
unsigned short shift_cleared_blocks(struct tetgrid *grid, int row)
{
 if (is_empty_row(grid, row)) {
  for (; row + 1 < 24; row++) {
   grid->blocks[row] = grid->blocks[row + 1];
  }
  grid->clearing--;
  return 0;
 }
 return grow_cleared_blocks(grid, row);
}


decompiled_code:
__int64 __fastcall next_cleared_row(__int64 a1, int a2)
{
__int64 v2; // rsi
__int16 v3; // ax
unsigned int v4; // er8

v2 = a2 - 1;
do
{
v3 = *(_WORD *)(a1 + 2 * v2 + 12);
v4 = v2--;
}
while ( (v3 & 1) != 0 );
return v4;
}

original_code:
int next_cleared_row(const struct tetgrid *grid, int row)
{
 do row--;
 while (grid->blocks[row] & 1);
 return row;
}

canonical_original_code:
int next_cleared_row(struct tetgrid *grid, int row)
{
 do row--;
 while (grid->blocks[row] & 1);
 return row;
}


decompiled_code:
__int64 __fastcall update_removable_rows(const __m128i *a1, __int64 a2)
{
__m128i v2; // xmm0
__m128i v3; // xmm1
__m128i v4; // xmm2
int v5; // eax
__int64 result; // rax
int v7; // ebp
__int128 v8[3]; // [rsp+0h] [rbp-58h] BYREF
__int64 v9; // [rsp+30h] [rbp-28h]
int v10; // [rsp+38h] [rbp-20h]

v2 = _mm_loadu_si128(a1);
v3 = _mm_loadu_si128(a1 + 1);
v4 = _mm_loadu_si128(a1 + 2);
v9 = a1[3].m128i_i64[0];
v5 = a1[3].m128i_i32[2];
v8[0] = (__int128)v2;
v10 = v5;
v8[1] = (__int128)v3;
v8[2] = (__int128)v4;
result = update_line_clears((__int64)v8);
v7 = result;
if ( (int)result > 0 )
{
while ( shift_cleared_blocks(v8, v7) || (unsigned int)can_move_tetmino(a2, (char *)v8 + 12, 0LL) )
{
result = next_cleared_row((__int64)a1, v7);
v7 = result;
if ( (int)result <= 0 )
return result;
}
do
{
if ( is_empty_row((__int64)a1, v7) )
{
a1->m128i_i16[v7 + 6] |= 1u;
--a1->m128i_i32[1];
}
result = next_cleared_row((__int64)a1, v7);
v7 = result;
}
while ( (int)result > 0 );
}
return result;
}

original_code:
void update_removable_rows(struct tetgrid *grid, const struct tetmino *t)
{
 struct tetgrid grid2 = *grid;
 int row = update_line_clears(&grid2);
 while (row > 0 && (shift_cleared_blocks(&grid2, row) || can_move_tetmino(t, grid2.blocks, 0))) {
  row = next_cleared_row(grid, row);
 }
 while (row > 0) {
  if (is_empty_row(grid, row)) {
   grid->blocks[row] |= 1;
   grid->clearing--;
  }
  row = next_cleared_row(grid, row);
 }
}

canonical_original_code:
void update_removable_rows(struct tetgrid *grid, struct tetmino *t)
{
 struct tetgrid grid2 = *grid;
 int row = update_line_clears(&grid2);
 while (row > 0 && (shift_cleared_blocks(&grid2, row) || can_move_tetmino(t, grid2.blocks, 0))) {
  row = next_cleared_row(grid, row);
 }
 while (row > 0) {
  if (is_empty_row(grid, row)) {
   grid->blocks[row] |= 1;
   grid->clearing--;
  }
  row = next_cleared_row(grid, row);
 }
}

variable_types: struct tetmino { unsigned int shape; int piece; int row; int falling; int lock_delay_move; int lock_delay_step; int climbed; } *

variable_types: struct tetgrid { int cols; int clearing; int delay; unsigned short blocks[24]; }

return_type: void


decompiled_code:
unsigned __int64 __fastcall render_piece(__int64 *a1, int *a2, char a3)
{
unsigned __int64 result; // rax
__int64 v4; // r12
int v5; // ebp
int v6; // er10
int v7; // er9
__int64 v9; // r11
int v11; // er13
_BYTE *v12; // rdi
__int64 v13; // r11
_BYTE *v14; // rdx
char v15; // cl

result = (unsigned int)a2[1];
v4 = *a1;
v5 = *a2;
v6 = a2[2];
v7 = a2[3];
if ( (int)result > 0 )
{
v9 = (unsigned int)(v5 - 1);
v11 = v7 + 4 * result;
v12 = (char *)a2 + v7 + v9 + 17;
v13 = ~v9;
do
{
result = (unsigned __int64)&v12[v13];
v14 = (_BYTE *)(v4 + v6);
if ( v5 > 0 )
{
do
{
v15 = *(_BYTE *)result;
*(_BYTE *)result = *v14;
if ( v15 )
*v14 = a3 & v15;
++result;
++v14;
}
while ( (_BYTE *)result != v12 );
}
v7 += 4;
v6 += *((_DWORD *)a1 + 2);
v12 += 4;
}
while ( v7 != v11 );
}
return result;
}

original_code:
static void render_piece(struct bitmap *bitmap, struct piece *p, int mask)
{
 unsigned char *bmp = bitmap->bitmap;
 struct copyrect r = p->r;
 for (; r.height > 0; r.height--) {
  int i;
  for (i=0; i < r.width; i++) {
   int block = p->piece[r.si + i];
   p->piece[r.si + i] = bmp[r.di + i];
   if (block)
    bmp[r.di + i] = block & mask;
  }
  r.di += bitmap->width;
  r.si += 4;
 }
}

variable_types: struct bitmap { unsigned char *bitmap; int width; int height; } *

variable_types: struct piece { struct copyrect { int width; int di; } r; unsigned char piece[-1]; struct piece *prev; } *

variable_types: mask int

variable_types:
bmp
unsigned char *

variable_types:
r
struct copyrect { int width; int di; }

variable_types: block int

user_defined_types: struct piece { struct copyrect r; unsigned char piece[-1]; struct piece *prev; }


original_code:
void render_tetmino_piece(struct bitmap *bitmap, struct piece *p, const struct tetmino *t, int mask)
{
 int w = bitmap->width;
 int h = bitmap->height;
 p->r = cliprect(
  w, h, 4, 4,
  t->col - 3, h - (4 - 1) - t->row
 );
 make_piece(p->piece, t->shape);
 mask &= 0x88 | (0x11 * (t->piece + 1));
 render_piece(bitmap, p, mask);
}

canonical_original_code:
void render_tetmino_piece(struct bitmap *bitmap, struct piece *p, struct tetmino *t, int mask)
{
 int w = bitmap->width;
 int h = bitmap->height;
 p->r = cliprect(
  w, h, 4, 4,
  t->col - 3, h - (4 - 1) - t->row
 );
 make_piece(p->piece, t->shape);
 mask &= 0x88 | (0x11 * (t->piece + 1));
 render_piece(bitmap, p, mask);
}

variable_types: w int

variable_types: h int

user_defined_types: struct bitmap { unsigned char *bitmap; int width; int height; }

user_defined_types: struct tetmino { unsigned int shape; int piece; int row; int falling; int lock_delay_move; int lock_delay_step; int climbed; }


original_code:
static struct piece *render_tetmino_pieces(struct bitmap *bitmap, struct tetmino_piece *tp)
{
 struct piece *piece = ((void *)0);
 while (tp) {
  render_tetmino_piece(bitmap, &tp->piece, tp->tetmino, tp->mask);
  tp->piece.prev = piece;
  piece = &tp->piece;
  tp = tp->succ;
 }
 return piece;
}

variable_types: struct tetmino_piece { struct piece piece; struct tetmino { unsigned int shape; int piece; int row; int falling; int lock_delay_move; int lock_delay_step; int climbed; } *tetmino; int mask; struct tetmino_piece *succ; } *

return_type: struct piece { struct copyrect { int width; int di; } r; unsigned char piece[-1]; struct piece *prev; } *

user_defined_types: struct tetmino_piece { struct piece piece; struct tetmino *tetmino; int mask; struct tetmino_piece *succ; }


decompiled_code:
__int64 __fastcall init_tetmino_piece(__int64 a1, __int64 a2, int a3, __int64 a4)
{
__int64 result; // rax

*(_QWORD *)(a1 + 40) = a2;
result = a1;
*(_DWORD *)(a1 + 48) = a3;
*(_QWORD *)(a1 + 56) = a4;
return result;
}

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

*(_QWORD *)(a1 + 40) = a2;
result = a1;
*(_DWORD *)(a1 + 48) = a3;
*(_QWORD *)(a1 + 56) = a4;
return result;
}

original_code:
struct tetmino_piece *init_tetmino_piece(struct tetmino_piece *tp,
 const struct tetmino *t, int mask, struct tetmino_piece *succ)
{
 tp->tetmino = t;
 tp->mask = mask;
 tp->succ = succ;
 return tp;
}

canonical_original_code:
struct tetmino_piece *init_tetmino_piece(struct tetmino_piece *tp, struct tetmino *t, int mask, struct tetmino_piece *succ)
{
 tp->tetmino = t;
 tp->mask = mask;
 tp->succ = succ;
 return tp;
}

variable_types:
tp
struct tetmino_piece *

variable_types:
t
struct tetmino *

variable_types:
succ
struct tetmino_piece *

return_type: struct tetmino_piece { struct piece { struct copyrect { int width; int di; } r; unsigned char piece[-1]; struct piece *prev; } piece; struct tetmino { unsigned int shape; int piece; int row; int falling; int lock_delay_move; int lock_delay_step; int climbed; } *tetmino; int mask; struct tetmino_piece *succ; } *


original_code:
void render_cleared_blocks(struct bitmap *bitmap, int row, blocks_row mask)
{
 unsigned char *bmp = bitmap->bitmap;
 int w = bitmap->width;
 int h = bitmap->height;
 int i = 0;
 if (mask) {
  for (; mask; mask >>= 1, i++) {
   if (mask & 1)
    bmp[w * (h - row) + i] = 0x80;
  }
 } else {
  memmove(bmp + w, bmp, w * (h - row));
  memset(bmp, 0, w);
  while (memchr(bmp + w, 0, w)) {
   bmp += w;
  }
  for (; i < w; i++) {
   if (!bmp[i]) {
    bmp[i] = 0x80;
   }
  }
 }
}

canonical_original_code:
void render_cleared_blocks(struct bitmap *bitmap, int row, unsigned short mask)
{
 unsigned char *bmp = bitmap->bitmap;
 int w = bitmap->width;
 int h = bitmap->height;
 int i = 0;
 if (mask) {
  for (; mask; mask >>= 1, i++) {
   if (mask & 1)
    bmp[w * (h - row) + i] = 0x80;
  }
 } else {
  memmove(bmp + w, bmp, w * (h - row));
  memset(bmp, 0, w);
  while (memchr(bmp + w, 0, w)) {
   bmp += w;
  }
  for (; i < w; i++) {
   if (!bmp[i]) {
    bmp[i] = 0x80;
   }
  }
 }
}

variable_types: row int

variable_types:
mask
unsigned short


decompiled_code:
__int64 __fastcall get_blocks_box(unsigned __int16 *a1, char a2)
{
unsigned int v3; // edx
unsigned __int16 *v4; // rsi
int v5; // eax

v3 = 0;
v4 = a1 + 4;
do
{
v5 = *a1++;
v3 = (v5 >> a2) & 0xF | (16 * v3);
}
while ( v4 != a1 );
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int16 *a1, char a2)
{
unsigned int v3; // edx
unsigned __int16 *v4; // rsi
int v5; // eax

v3 = 0;
v4 = a1 + 4;
do
{
v5 = *a1++;
v3 = (v5 >> a2) & 0xF | (16 * v3);
}
while ( v4 != a1 );
return v3;
}

original_code:
static unsigned get_blocks_box(const blocks_row *blocks, int col)
{
 unsigned box = 0, row_mask = (1 << 4) - 1;
 int i;
 for (i=0; i < 4; i++)
  box = (box << 4) | ((blocks[i] >> col) & row_mask);
 return box;
}

canonical_original_code:
static unsigned int get_blocks_box(unsigned short *blocks, int col)
{
 unsigned int box = 0; unsigned int row_mask = (1 << 4) - 1;
 int i;
 for (i=0; i < 4; i++)
  box = (box << 4) | ((blocks[i] >> col) & row_mask);
 return box;
}

variable_types:
blocks
unsigned short *

variable_types: col int

variable_types:
box
unsigned int

variable_types: i int

return_type: unsigned int


decompiled_code:
__int64 __fastcall floor_kick(_DWORD *a1, unsigned int a2, int a3)
{
__int64 result; // rax
int v4; // edx

result = 0LL;
if ( (a3 & a2) >> 12 && (a2 & (16 * a3)) == 0 && !a1[7] )
{
v4 = a1[4];
++a1[2];
result = 1LL;
a1[7] = 1;
if ( v4 )
a1[4] = 1;
}
return result;
}

original_code:
static int floor_kick(struct tetmino *t, unsigned rotated, unsigned box)
{
 if ((rotated & box) >> (4 -1) * 4 &&
     !(rotated & (box << 4)) && !t->climbed) {
  t->climbed = 1;
  t->row++;
  if (t->falling) {
   t->falling = 1;
  }
  return 1;
 }
 return 0;
}

canonical_original_code:
static int floor_kick(struct tetmino *t, unsigned int rotated, unsigned int box)
{
 if ((rotated & box) >> (4 -1) * 4 &&
     !(rotated & (box << 4)) && !t->climbed) {
  t->climbed = 1;
  t->row++;
  if (t->falling) {
   t->falling = 1;
  }
  return 1;
 }
 return 0;
}

variable_types: struct tetmino { unsigned int shape; int piece; int row; int falling; int lock_delay_move; int lock_delay_step; int climbed; } *

variable_types:
rotated
unsigned int


original_code:
static int wall_kick(struct tetmino *t, unsigned rotated, unsigned box,
       const blocks_row *blocks)
{
 unsigned overlap = rotated & box,
   mask1 = 0x2222 >> 1,
   mask3 = 0x2222 << 1;
 int n = 0;
 if (rotated == 0x0F00)
  n = (t->shape == 0x2222) ? -1 : 1;
 else {
  if (overlap == (overlap & mask1))
   n = 1;
  if (overlap == (overlap & mask3))
   n = -1;
 }
 if (n && !(rotated & get_blocks_box(blocks, t->col + n))) {
  t->col += n;
  return n;
 }
 return 0;
}

canonical_original_code:
static int wall_kick(struct tetmino *t, unsigned int rotated, unsigned int box, unsigned short *blocks)
{
 unsigned int overlap = rotated & box; unsigned int mask1 = 0x2222 >> 1; unsigned int mask3 = 0x2222 << 1;
 int n = 0;
 if (rotated == 0x0F00)
  n = (t->shape == 0x2222) ? -1 : 1;
 else {
  if (overlap == (overlap & mask1))
   n = 1;
  if (overlap == (overlap & mask3))
   n = -1;
 }
 if (n && !(rotated & get_blocks_box(blocks, t->col + n))) {
  t->col += n;
  return n;
 }
 return 0;
}

variable_types:
overlap
unsigned int

variable_types: n int


rotate

original_code:
static int rotate(struct tetmino *t, const blocks_row *blocks, int n)
{
 const unsigned short *rot_states = tetmino_shapes[t->piece];
 unsigned box = get_blocks_box(blocks, t->col);
 int kick = 1;
 int i;
 if (rot_states[0] == t->shape &&
     rot_states[2] == t->shape && (rot_states[n] & box)) {
  n += 2;
  kick = 0;
 }
 for (i=0; i<4; i++) {
  if (rot_states[i] == t->shape) {
   unsigned rotated = rot_states[(i+n) & 3];
   if (rotated & box) {
    if (!kick)
     return 0;
    if (i == 2 && floor_kick(t, rotated, box))
     ;
    else if (!wall_kick(t, rotated, box, blocks))
     return 0;
   }
   t->shape = rotated;
   return rotated ^ 0x6600;
  }
 }
 return 0;
}

canonical_original_code:
static int rotate(struct tetmino *t, unsigned short *blocks, int n)
{
 unsigned short *rot_states = tetmino_shapes[t->piece];
 unsigned int box = get_blocks_box(blocks, t->col);
 int kick = 1;
 int i;
 if (rot_states[0] == t->shape &&
     rot_states[2] == t->shape && (rot_states[n] & box)) {
  n += 2;
  kick = 0;
 }
 for (i=0; i<4; i++) {
  if (rot_states[i] == t->shape) {
   unsigned int rotated = rot_states[(i+n) & 3];
   if (rotated & box) {
    if (!kick)
     return 0;
    if (i == 2 && floor_kick(t, rotated, box))
     ;
    else if (!wall_kick(t, rotated, box, blocks))
     return 0;
   }
   t->shape = rotated;
   return rotated ^ 0x6600;
  }
 }
 return 0;
}

variable_types: kick int


decompiled_code:
__int64 __fastcall init_lock_delay(__int64 a1, int a2)
{
__int64 result; // rax

*(_DWORD *)(a1 + 24) = 30;
result = (unsigned int)(20 - a2);
if ( *(_DWORD *)(a1 + 20) < (int)result )
*(_DWORD *)(a1 + 20) = result;
return result;
}

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

*(_DWORD *)(a1 + 24) = 30;
result = (unsigned int)(20 - a2);
if ( *(_DWORD *)(a1 + 20) < (int)result )
*(_DWORD *)(a1 + 20) = result;
return result;
}

original_code:
static void init_lock_delay(struct tetmino *t, int gravity)
{
 int lock_delay = 20 - gravity;
 t->lock_delay_step = 30;
 if (lock_delay > t->lock_delay_move)
  t->lock_delay_move = lock_delay;
}

variable_types: gravity int


decompiled_code:
__int64 __fastcall tetmino_has_row(unsigned int a1, char a2)
{
return (a1 >> (4 * (3 - a2))) & 0xF;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1, char a2)
{
return (a1 >> (4 * (3 - a2))) & 0xF;
}

original_code:
int tetmino_has_row(unsigned shape, int i)
{
 int shift = (4 - 1 - i) * 4;
 return (shape >> shift) % (1 << 4);
}

canonical_original_code:
int tetmino_has_row(unsigned int shape, int i)
{
 int shift = (4 - 1 - i) * 4;
 return (shape >> shift) % (1 << 4);
}

variable_types:
shape
unsigned int

variable_types: shift int


decompiled_code:
_BOOL8 __fastcall can_move_tetmino(__int64 a1, __int64 a2, char a3)
{
int v3; // eax
_DWORD *v4; // r8

v3 = get_blocks_box((unsigned __int16 *)(a2 + 2LL * *(int *)(a1 + 8)), *(_BYTE *)(a1 + 12) + a3);
return (*v4 & v3) == 0;
}

original_code:
int can_move_tetmino(const struct tetmino *t, const blocks_row *blocks, int d)
{
 return !(t->shape & get_blocks_box(blocks + t->row, t->col + d));
}

canonical_original_code:
int can_move_tetmino(struct tetmino *t, unsigned short *blocks, int d)
{
 return !(t->shape & get_blocks_box(blocks + t->row, t->col + d));
}

variable_types: d int


original_code:
int drop_height(const struct tetmino *t, const blocks_row *blocks, int max)
{
 int h = 0;
 if (max > t->row)
  max = t->row;
 if (max > 0) {
  unsigned box = get_blocks_box(blocks += t->row - 1, t->col);
  while (!(t->shape & box)) {
   h++;
   if (h == max)
    break;
   box = (box >> 4) |
         (blocks[-h] >> t->col
          << (4 -1) * 4);
   box &= (1U << 4 * 4) - 1;
  }
 }
 return h;
}

canonical_original_code:
int drop_height(struct tetmino *t, unsigned short *blocks, int max)
{
 int h = 0;
 if (max > t->row)
  max = t->row;
 if (max > 0) {
  unsigned int box = get_blocks_box(blocks += t->row - 1, t->col);
  while (!(t->shape & box)) {
   h++;
   if (h == max)
    break;
   box = (box >> 4) |
         (blocks[-h] >> t->col
          << (4 -1) * 4);
   box &= (1U << 4 * 4) - 1;
  }
 }
 return h;
}

variable_types: max int

variable_types: h int


drop

decompiled_code:
__int64 __fastcall drop(__int64 a1, __int64 a2, int a3)
{
__int64 result; // rax
int v4; // edx
int v5; // edx

result = drop_height(a1, a2, a3);
v4 = *(_DWORD *)(a1 + 28);
*(_DWORD *)(a1 + 8) -= result;
v5 = v4 - result;
if ( v5 < 0 )
v5 = 0;
*(_DWORD *)(a1 + 28) = v5;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, __int64 a2, int a3)
{
__int64 result; // rax
int v4; // edx
int v5; // edx

result = func8(a1, a2, a3);
v4 = *(_DWORD *)(a1 + 28);
*(_DWORD *)(a1 + 8) -= result;
v5 = v4 - result;
if ( v5 < 0 )
v5 = 0;
*(_DWORD *)(a1 + 28) = v5;
return result;
}

original_code:
static int drop(struct tetmino *t, const blocks_row *blocks, int n)
{
 int h = drop_height(t, blocks, n);
 t->row -= h;
 t->climbed -= h;
 if (t->climbed < 0)
  t->climbed = 0;
 return h;
}

canonical_original_code:
static int drop(struct tetmino *t, unsigned short *blocks, int n)
{
 int h = drop_height(t, blocks, n);
 t->row -= h;
 t->climbed -= h;
 if (t->climbed < 0)
  t->climbed = 0;
 return h;
}


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

switch ( a3 )
{
case 0:
return 0LL;
case 1:
case 2:
result = rotate((int *)a1, (unsigned __int16 *)(a2 + 2LL * *(int *)(a1 + 8)));
goto LABEL_5;
case 3:
result = can_move_tetmino(a1, a2, 1);
*(_DWORD *)(a1 + 12) += result;
goto LABEL_5;
case 4:
result = can_move_tetmino(a1, a2, -1);
*(_DWORD *)(a1 + 12) -= result;
LABEL_5:
if ( (_DWORD)result )
*(_DWORD *)(a1 + 20) = 21;
return result;
case 5:
drop(a1, a2, *(_DWORD *)(a1 + 8));
goto LABEL_10;
case 6:
result = drop(a1, a2, 1);
if ( (_DWORD)result )
return result;
LABEL_10:
*(_QWORD *)(a1 + 16) = 0LL;
result = 1LL;
*(_DWORD *)(a1 + 24) = 0;
break;
default:
result = 0LL;
break;
}
return result;
}

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

switch ( a3 )
{
case 0:
return 0LL;
case 1:
case 2:
result = func3((int *)a1, (unsigned __int16 *)(a2 + 2LL * *(int *)(a1 + 8)));
goto LABEL_5;
case 3:
result = func7(a1, a2, 1);
*(_DWORD *)(a1 + 12) += result;
goto LABEL_5;
case 4:
result = func7(a1, a2, -1);
*(_DWORD *)(a1 + 12) -= result;
LABEL_5:
if ( (_DWORD)result )
*(_DWORD *)(a1 + 20) = 21;
return result;
case 5:
func9(a1, a2, *(_DWORD *)(a1 + 8));
goto LABEL_10;
case 6:
result = func9(a1, a2, 1);
if ( (_DWORD)result )
return result;
LABEL_10:
*(_QWORD *)(a1 + 16) = 0LL;
result = 1LL;
*(_DWORD *)(a1 + 24) = 0;
break;
default:
result = 0LL;
break;
}
return result;
}

original_code:
int control_tetmino(struct tetmino *t, const blocks_row *blocks, enum action a)
{
 int moved = 0, dropped = 0, lock = 0;
 switch (a) {
 case ROTATE_CW:
  moved = rotate(t, blocks + t->row, 1);
  break;
 case ROTATE_CCW:
  moved = rotate(t, blocks + t->row, 3);
  break;
 case MOVE_RIGHT:
  t->col += (moved = can_move_tetmino(t, blocks, 1));
  break;
 case MOVE_LEFT:
  t->col -= (moved = can_move_tetmino(t, blocks, -1));
  break;
 case HARDDROP:
  dropped = drop(t, blocks, t->row);
  lock = 1;
  break;
 case SOFTDROP:
  dropped = drop(t, blocks, 1);
  lock = !dropped;
 default:
  break;
 }
 if (lock) {
  t->falling = 0;
  t->lock_delay_move = 0;
  t->lock_delay_step = 0;
  return lock;
 }
 if (moved) {
  t->lock_delay_move = 20 + 1;
 }
 return moved | dropped;
}

canonical_original_code:
int control_tetmino(struct tetmino *t, unsigned short *blocks, enum action a)
{
 int moved = 0; int dropped = 0; int lock = 0;
 switch (a) {
 case ROTATE_CW:
  moved = rotate(t, blocks + t->row, 1);
  break;
 case ROTATE_CCW:
  moved = rotate(t, blocks + t->row, 3);
  break;
 case MOVE_RIGHT:
  t->col += (moved = can_move_tetmino(t, blocks, 1));
  break;
 case MOVE_LEFT:
  t->col -= (moved = can_move_tetmino(t, blocks, -1));
  break;
 case HARDDROP:
  dropped = drop(t, blocks, t->row);
  lock = 1;
  break;
 case SOFTDROP:
  dropped = drop(t, blocks, 1);
  lock = !dropped;
 default:
  break;
 }
 if (lock) {
  t->falling = 0;
  t->lock_delay_move = 0;
  t->lock_delay_step = 0;
  return lock;
 }
 if (moved) {
  t->lock_delay_move = 20 + 1;
 }
 return moved | dropped;
}

variable_types: enum action { NO_ACTION=0, ROTATE_CW=1, ROTATE_CCW=2, MOVE_RIGHT=3, MOVE_LEFT=4, HARDDROP=5, SOFTDROP=6, END_ACTION=7 }

variable_types: moved int

variable_types: dropped int

variable_types: lock int


decompiled_code:
__int64 __fastcall unfloat_tetmino(__int64 a1, __int64 a2)
{
__int64 result; // rax

result = *(unsigned int *)(a1 + 16);
if ( !(_DWORD)result )
{
drop(a1, a2, *(_DWORD *)(a1 + 28));
result = drop_height(a1, a2, 1);
*(_DWORD *)(a1 + 16) = result;
}
return result;
}

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

result = *(unsigned int *)(a1 + 16);
if ( !(_DWORD)result )
{
func9(a1, a2, *(_DWORD *)(a1 + 28));
result = func8(a1, a2, 1);
*(_DWORD *)(a1 + 16) = result;
}
return result;
}

original_code:
void unfloat_tetmino(struct tetmino *t, const blocks_row *blocks)
{
 if (!t->falling) {
  drop(t, blocks, t->climbed);
  t->falling = drop_height(t, blocks, 1);
 }
}

canonical_original_code:
void unfloat_tetmino(struct tetmino *t, unsigned short *blocks)
{
 if (!t->falling) {
  drop(t, blocks, t->climbed);
  t->falling = drop_height(t, blocks, 1);
 }
}

variable_types:
blocks
unsigned short *


decompiled_code:
__int64 __fastcall update_tetmino(unsigned int *a1, __int64 a2, int a3)
{
__int64 result; // rax
int v5; // eax
int v6; // edx
int v7; // edx

unfloat_tetmino((__int64)a1, a2);
result = a1[4];
if ( (_DWORD)result )
{
if ( a3 <= (int)result )
LODWORD(result) = a3;
v5 = result - 1;
a1[4] = v5;
if ( v5 > 0 )
return 0LL;
if ( (unsigned int)drop((__int64)a1, a2, 1) )
{
a1[4] = a3;
result = 1LL;
}
else
{
a1[4] = 0;
init_lock_delay((__int64)a1, a3);
result = 0LL;
}
}
else
{
v6 = a1[5];
if ( v6 > 0 )
a1[5] = v6 - 1;
v7 = a1[6];
if ( v7 <= 0 )
return 0LL;
a1[6] = v7 - 1;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(unsigned int *a1, __int64 a2, int a3)
{
__int64 result; // rax
int v5; // eax
int v6; // edx
int v7; // edx

func11((__int64)a1, a2);
result = a1[4];
if ( (_DWORD)result )
{
if ( a3 <= (int)result )
LODWORD(result) = a3;
v5 = result - 1;
a1[4] = v5;
if ( v5 > 0 )
return 0LL;
if ( (unsigned int)func9((__int64)a1, a2, 1) )
{
a1[4] = a3;
result = 1LL;
}
else
{
a1[4] = 0;
func4((__int64)a1, a3);
result = 0LL;
}
}
else
{
v6 = a1[5];
if ( v6 > 0 )
a1[5] = v6 - 1;
v7 = a1[6];
if ( v7 <= 0 )
return 0LL;
a1[6] = v7 - 1;
}
return result;
}

original_code:
int update_tetmino(struct tetmino *t, const blocks_row *blocks, int gravity)
{
 unfloat_tetmino(t, blocks);
 if (!t->falling) {
  if (t->lock_delay_move > 0)
   t->lock_delay_move--;
  if (t->lock_delay_step > 0)
   t->lock_delay_step--;
  return 0;
 }
 if (t->falling > gravity)
  t->falling = gravity;
 t->falling--;
 if (t->falling > 0)
  return 0;
 if (drop(t, blocks, 1)) {
  t->falling = gravity;
  return 1;
 } else {
  t->falling = 0;
  init_lock_delay(t, gravity);
  return 0;
 }
}

canonical_original_code:
int update_tetmino(struct tetmino *t, unsigned short *blocks, int gravity)
{
 unfloat_tetmino(t, blocks);
 if (!t->falling) {
  if (t->lock_delay_move > 0)
   t->lock_delay_move--;
  if (t->lock_delay_step > 0)
   t->lock_delay_step--;
  return 0;
 }
 if (t->falling > gravity)
  t->falling = gravity;
 t->falling--;
 if (t->falling > 0)
  return 0;
 if (drop(t, blocks, 1)) {
  t->falling = gravity;
  return 1;
 } else {
  t->falling = 0;
  init_lock_delay(t, gravity);
  return 0;
 }
}


decompiled_code:
__int16 __fastcall to_wide(unsigned __int8 a1)
{
__int16 result; // ax
unsigned int v2; // edx

result = a1;
if ( (a1 & 0x80) != 0 )
{
v2 = a1 - 176;
result = -1;
if ( v2 <= 0x2F )
{
LOBYTE(result) = extended_b0_0[v2];
HIBYTE(result) = 37;
}
}
return result;
}

canonical_decompiled_code:
__int16 __fastcall func0(unsigned __int8 a1)
{
__int16 result; // ax
unsigned int v2; // edx

result = a1;
if ( (a1 & 0x80) != 0 )
{
v2 = a1 - 176;
result = -1;
if ( v2 <= 0x2F )
{
LOBYTE(result) = extended_b0_0[v2];
HIBYTE(result) = 37;
}
}
return result;
}

canonical_original_code:
static unsigned int to_wide(int c)
{
 static unsigned char extended_b0[48] = {
  0x91, 0x92, 0x93, 0x02, 0x24, 0x61, 0x62, 0x56,
  0x55, 0x63, 0x51, 0x57, 0x5d, 0x5c, 0x5b, 0x10,
  0x14, 0x34, 0x2c, 0x1c, 0x00, 0x3c, 0x5e, 0x5f,
  0x5a, 0x54, 0x69, 0x66, 0x60, 0x50, 0x6c, 0x67,
  0x68, 0x64, 0x65, 0x59, 0x58, 0x52, 0x53, 0x6b,
  0x6a, 0x18, 0x0c, 0x88, 0x84, 0x8c, 0x90, 0x80
 };
 int codepoint = (unsigned char) c;
 if (codepoint < 128)
  return codepoint;
 if (codepoint >= 0xb0 && codepoint < 0xe0)
  return extended_b0[codepoint - 0xb0] | 0x2500;
 return (0xffffffffu);
}

variable_types: c int

variable_types: codepoint int


decompiled_code:
void __fastcall puttext_unicode(unsigned __int8 *a1)
{
unsigned __int8 *v1; // rbx
char v2[40]; // [rsp-28h] [rbp-28h] BYREF

if ( *a1 )
{
v1 = a1;
do
{
v1 += (int)convert_text_constprop_0(v1, v2);
fputs(v2, stdout);
}
while ( *v1 );
}
}

canonical_decompiled_code:
void __fastcall func2(unsigned __int8 *a1)
{
unsigned __int8 *v1; // rbx
char v2[40]; // [rsp-28h] [rbp-28h] BYREF

if ( *a1 )
{
v1 = a1;
do
{
v1 += (int)func1(v1, v2);
fputs(v2, stdout);
}
while ( *v1 );
}
}

original_code:
void puttext_unicode(const char *s)
{
 char buf[20];
 while (*s) {
  s += convert_text(s, buf, sizeof(buf));
  fputs(buf, stdout);
 }
}

canonical_original_code:
void puttext_unicode(char *s)
{
 char buf[20];
 while (*s) {
  s += convert_text(s, buf, sizeof(buf));
  fputs(buf, stdout);
 }
}

variable_types:
s
char *

variable_types:
buf
char [20]


decompiled_code:
void __fastcall catch_signal(int a1)
{
signal_caught = a1;
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
signal_caught = a1;
}

original_code:
static void catch_signal(int sig)
{
 signal_caught = sig;
}

variable_types: sig int


decompiled_code:
_BOOL8 is_linux_console()
{
char *v0; // rsi
_BOOL8 result; // rax

v0 = getenv("TERM");
result = 0LL;
if ( v0 )
result = strcmp(v0, "linux") == 0;
return result;
}

canonical_decompiled_code:
_BOOL8 func1()
{
char *v0; // rsi
_BOOL8 result; // rax

v0 = getenv("TERM");
result = 0LL;
if ( v0 )
result = strcmp(v0, "linux") == 0;
return result;
}

original_code:
static int is_linux_console()
{
 char *s = getenv("TERM");
 return s && !strcmp(s, "linux");
}


decompiled_code:
bool detect_charset()
{
bool v0; // zf
_BOOL4 v1; // eax

v0 = strcmp(nl_langinfo(14), "UTF-8") == 0;
LOBYTE(v1) = !v0;
if ( v0 )
{
terminal[2] = &puttext_unicode;
terminal[1] = &putacs_text;
}
else
{
v1 = is_linux_console();
if ( v1 )
terminal[1] = &putacs_text;
}
return v1;
}

canonical_decompiled_code:
bool func2()
{
bool v0; // zf
_BOOL4 v1; // eax

v0 = strcmp(nl_langinfo(14), "UTF-8") == 0;
LOBYTE(v1) = !v0;
if ( v0 )
{
terminal[2] = &puttext_unicode;
terminal[1] = &putacs_text;
}
else
{
v1 = func1();
if ( v1 )
terminal[1] = &putacs_text;
}
return v1;
}

original_code:
static void detect_charset()
{
 if (!strcmp(nl_langinfo(CODESET), "UTF-8")) {
  terminal.puttext = puttext_unicode;
  terminal.putacs = putacs_text;
 } else if (is_linux_console()) {
  terminal.putacs = putacs_text;
 }
}


decompiled_code:
int wait_one_frame()
{
int *v0; // rax
struct timeval v2; // [rsp+0h] [rbp-98h] BYREF
fd_set readfds; // [rsp+10h] [rbp-88h] BYREF

v2.tv_sec = 0LL;
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[0] |= 1uLL;
v2.tv_usec = 16666LL;
LODWORD(v0) = select(1, &readfds, 0LL, 0LL, &v2);
if ( (int)v0 < 0 )
{
v0 = _errno_location();
if ( *v0 != 4 )
{
print_error("select");
restore_terminal("select");
exit(1);
}
}
return (int)v0;
}

canonical_decompiled_code:
int func3()
{
int *v0; // rax
struct timeval v2; // [rsp+0h] [rbp-98h] BYREF
fd_set readfds; // [rsp+10h] [rbp-88h] BYREF

v2.tv_sec = 0LL;
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[0] |= 1uLL;
v2.tv_usec = 16666LL;
LODWORD(v0) = select(1, &readfds, 0LL, 0LL, &v2);
if ( (int)v0 < 0 )
{
v0 = _errno_location();
if ( *v0 != 4 )
{
print_error("select");
restore_terminal("select");
exit(1);
}
}
return (int)v0;
}

original_code:
static void wait_one_frame()
{
 struct timeval timeout = {0};
 fd_set fds;
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&fds)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
 timeout.tv_usec = (1000000L / 60);
 if (select(0 + 1, &fds, ((void *)0), ((void *)0), &timeout) < 0) {
  if ((*__errno_location ()) != 4) {
   print_error("select");
   restore_terminal();
   exit(1);
  }
 }
}

canonical_original_code:
static void wait_one_frame()
{
 struct timeval timeout = {0};
 struct fd_set fds;
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&fds)->__fds_bits)[((0) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((0) % (8 * (int) sizeof (__fd_mask)))))));
 timeout.tv_usec = (1000000L / 60);
 if (select(0 + 1, &fds, ((void *)0), ((void *)0), &timeout) < 0) {
  if ((*__errno_location ()) != 4) {
   print_error("select");
   restore_terminal();
   exit(1);
  }
 }
}

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

variable_types:
fds
struct fd_set { long __fds_bits[-1]; }


decompiled_code:
__int64 run_game_loop()
{
__int128 *v0; // rbp
__int64 result; // rax
__int128 v2[2]; // [rsp+0h] [rbp-388h] BYREF
__int64 v3; // [rsp+20h] [rbp-368h]
char v4[856]; // [rsp+30h] [rbp-358h] BYREF

v2[0] = 0LL;
v0 = v2;
v2[1] = 0LL;
v3 = 0LL;
init_game(v4, 20LL);
signal(2, catch_signal);
signal(15, catch_signal);
signal(18, catch_signal);
signal(20, catch_signal);
signal(28, catch_signal);
while ( 1 )
{
result = update_game(v4, v0);
if ( !(_DWORD)result )
break;
wait_one_frame();
process_signal_isra_0((__int64)v4);
v0 = (__int128 *)read_terminal_keypress(v2);
process_signal_isra_0((__int64)v2);
}
return result;
}

canonical_decompiled_code:
__int64 func5()
{
__int128 *v0; // rbp
__int64 result; // rax
__int128 v2[2]; // [rsp+0h] [rbp-388h] BYREF
__int64 v3; // [rsp+20h] [rbp-368h]
char v4[856]; // [rsp+30h] [rbp-358h] BYREF

v2[0] = 0LL;
v0 = v2;
v2[1] = 0LL;
v3 = 0LL;
init_game(v4, 20LL);
signal(2, catch_signal);
signal(15, catch_signal);
signal(18, catch_signal);
signal(20, catch_signal);
signal(28, catch_signal);
while ( 1 )
{
result = update_game(v4, v0);
if ( !(_DWORD)result )
break;
func3();
func4((__int64)v4);
v0 = (__int128 *)read_terminal_keypress(v2);
func4((__int64)v2);
}
return result;
}

original_code:
static void run_game_loop()
{
 struct terminal_input input = {{""}};
 const char *keypress = input.current.s;
 struct game game;
 init_game(&game, 20);
 signal(2, catch_signal);
 signal(15, catch_signal);
 signal(18, catch_signal);
 signal(20, catch_signal);
 signal(28, catch_signal);
 while (update_game(&game, keypress)) {
  wait_one_frame();
  process_signal();
  keypress = read_terminal_keypress(&input);
  process_signal();
 }
}

canonical_original_code:
static void run_game_loop()
{
 struct terminal_input input = {{""}};
 char *keypress = input.current.s;
 struct game game;
 init_game(&game, 20);
 signal(2, catch_signal);
 signal(15, catch_signal);
 signal(18, catch_signal);
 signal(20, catch_signal);
 signal(28, catch_signal);
 while (update_game(&game, keypress)) {
  wait_one_frame();
  process_signal();
  keypress = read_terminal_keypress(&input);
  process_signal();
 }
}

variable_types: struct terminal_input { struct terminal_seq_buffer { char s[16]; } current; unsigned long next_length; }

variable_types:
keypress
char *

variable_types: struct game { struct tetgrid { int cols; int clearing; int delay; unsigned short blocks[24]; } tetgrid; struct tetfield { struct tetmino { unsigned int shape; int piece; int row; int falling; int lock_delay_move; int lock_delay_step; int climbed; } mino; struct tetgrid *grid; int gravity; enum { TETFIELD_SPAWN=0, TETFIELD_MOVE=1, TETFIELD_PLACED=2, TETFIELD_TOP_OUT=3 } state; enum action { NO_ACTION=0, ROTATE_CW=1, ROTATE_CCW=2, MOVE_RIGHT=3, MOVE_LEFT=4, HARDDROP=5, SOFTDROP=6, END_ACTION=7 } last_action; unsigned char timeout[-1]; unsigned long score; unsigned int pieces; } tetfield; struct tetmino next_tetmino; struct tetmino_piece { struct piece { struct copyrect { int width; int di; } r; unsigned char piece[-1]; struct piece *prev; } piece; struct tetmino *tetmino; int mask; struct tetmino_piece *succ; } piece; struct blocks { struct bitmap { unsigned char *bitmap; int width; int height; } bitmap; struct tg_tiles { char chars[16]; unsigned short attrs[16]; } tiles; int x; int rendered; } blocks; unsigned char bitmap[-1]; }

user_defined_types: struct game { struct tetgrid tetgrid; struct tetfield tetfield; struct tetmino next_tetmino; struct tetmino_piece piece; struct blocks blocks; unsigned char bitmap[-1]; }

user_defined_types: struct tetfield { struct tetmino mino; struct tetgrid *grid; int gravity; enum { TETFIELD_SPAWN=0, TETFIELD_MOVE=1, TETFIELD_PLACED=2, TETFIELD_TOP_OUT=3 } state; enum action last_action; unsigned char timeout[-1]; unsigned long score; unsigned int pieces; }

user_defined_types: struct blocks { struct bitmap bitmap; struct tg_tiles tiles; int x; int rendered; }


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er8
int result; // eax
time_t v5; // rdi

setlocale(0, &locale);
v3 = init_terminal();
result = 1;
if ( v3 )
{
detect_charset();
v5 = time(0LL);
srandom(v5);
run_game_loop();
restore_terminal(v5);
result = 0;
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er8
int result; // eax
time_t v5; // rdi

setlocale(0, &locale);
v3 = init_terminal();
result = 1;
if ( v3 )
{
func2();
v5 = time(0LL);
srandom(v5);
func5();
restore_terminal(v5);
result = 0;
}
return result;
}

original_code:
int main(int argc, char **argv)
{
 setlocale(0, "");
 if (!init_terminal()) {
  return 1;
 }
 detect_charset();
 srandom(time(((void *)0)));
 run_game_loop();
 restore_terminal();
 return 0;
}

variable_types: argc int

variable_types:
argv
char **


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

switch ( a1 )
{
case 'a':
result = 4294967217LL;
break;
case 'h':
result = 4294967216LL;
break;
case 'j':
result = 4294967257LL;
break;
case 'k':
result = 4294967231LL;
break;
case 'l':
result = 4294967258LL;
break;
case 'm':
result = 4294967232LL;
break;
case 'n':
result = 4294967237LL;
break;
case 'q':
result = 4294967236LL;
break;
case 't':
result = 4294967235LL;
break;
case 'u':
result = 4294967220LL;
break;
case 'v':
result = 4294967233LL;
break;
case 'w':
result = 4294967234LL;
break;
case 'x':
result = 4294967219LL;
break;
default:
result = a1;
break;
}
return result;
}

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

switch ( a1 )
{
case 'a':
result = 4294967217LL;
break;
case 'h':
result = 4294967216LL;
break;
case 'j':
result = 4294967257LL;
break;
case 'k':
result = 4294967231LL;
break;
case 'l':
result = 4294967258LL;
break;
case 'm':
result = 4294967232LL;
break;
case 'n':
result = 4294967237LL;
break;
case 'q':
result = 4294967236LL;
break;
case 't':
result = 4294967235LL;
break;
case 'u':
result = 4294967220LL;
break;
case 'v':
result = 4294967233LL;
break;
case 'w':
result = 4294967234LL;
break;
case 'x':
result = 4294967219LL;
break;
default:
result = a1;
break;
}
return result;
}

original_code:
static int acs_to_text(int c)
{
 switch (c) {
 case 'a': return TEXT_MEDIUM_SHADE;
 case 'h': return TEXT_LIGHT_SHADE;
 case 'j': return TEXT_LOWER_RIGHT_CORNER;
 case 'k': return TEXT_UPPER_RIGHT_CORNER;
 case 'l': return TEXT_UPPER_LEFT_CORNER;
 case 'm': return TEXT_LOWER_LEFT_CORNER;
 case 'n': return TEXT_CROSS;
 case 'q': return TEXT_HORIZONTAL_LINE;
 case 't': return TEXT_LEFT_T;
 case 'u': return TEXT_RIGHT_T;
 case 'v': return TEXT_BOTTOM_T;
 case 'w': return TEXT_TOP_T;
 case 'x': return TEXT_VERTICAL_LINE;
 }
 return c;
}


decompiled_code:
void __fastcall putacs_text(char *a1)
{
char *v1; // rbx
_BYTE v2[40]; // [rsp-28h] [rbp-28h] BYREF

if ( *a1 )
{
v1 = a1;
do
{
v1 += (int)convert_chars_constprop_0(v1, v2);
terminal[2](v2);
}
while ( *v1 );
}
}

canonical_decompiled_code:
void __fastcall func2(char *a1)
{
char *v1; // rbx
_BYTE v2[40]; // [rsp-28h] [rbp-28h] BYREF

if ( *a1 )
{
v1 = a1;
do
{
v1 += (int)func1(v1, v2);
terminal[2](v2);
}
while ( *v1 );
}
}

original_code:
void putacs_text(const char *s)
{
 char buf[20];
 while (*s) {
  s += convert_chars(s, buf, sizeof(buf));
  terminal.puttext(buf);
 }
}

canonical_original_code:
void putacs_text(char *s)
{
 char buf[20];
 while (*s) {
  s += convert_chars(s, buf, sizeof(buf));
  terminal.puttext(buf);
 }
}


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

do
result = random() / 306783378;
while ( (int)result > 6 );
return result;
}

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

do
result = random() / 306783378;
while ( (int)result > 6 );
return result;
}

variable_types: x int


original_code:
int centre_text(const char *s, int w, int x, int y)
{
 char buf[80] = " ";
 int len = strcspn(strncat(buf, s, 72), "\n\t");
 buf[len++] = ' ';
 buf[len] = '\0';
 if (w - len > 0)
  x += (w - len)/2;
 moveto(x, y);
 terminal.puttext(buf);
 terminal.cursor_x += len;
 return len - 2;
}

canonical_original_code:
int centre_text(char *s, int w, int x, int y)
{
 char buf[80] = " ";
 int len = strcspn(strncat(buf, s, 72), "\n\t");
 buf[len++] = ' ';
 buf[len] = '\0';
 if (w - len > 0)
  x += (w - len)/2;
 moveto(x, y);
 terminal.puttext(buf);
 terminal.cursor_x += len;
 return len - 2;
}

variable_types: w int

variable_types: y int

variable_types:
buf
char [80]

variable_types: len int


repo: vieux/dcpu-vv

decompiled_code:
bool __fastcall basic_operation(__int64 errnum, unsigned __int16 a2, const char *a3, unsigned __int16 *a4)
{
_WORD *v4; // rbx
__int64 v5; // r8
int v7; // er8

v4 = a4;
v5 = *a4;
*(_QWORD *)(errnum + 131096) += *((unsigned __int8 *)&basic_opcodes + 4 * a2 + 3);
LOWORD(a4) = (_WORD)a3;
switch ( a2 )
{
case 1u:
goto LABEL_3;
case 2u:
LOWORD(a4) = v5 + (_WORD)a3;
*(_WORD *)(errnum + 131094) = ((unsigned __int16)a3 + (unsigned int)(unsigned __int16)v5) >> 16;
goto LABEL_3;
case 3u:
LOWORD(a4) = v5 - (_WORD)a3;
*(_WORD *)(errnum + 131094) = -(((unsigned __int16)v5 - (unsigned int)(unsigned __int16)a3) >> 16 != 0);
goto LABEL_3;
case 4u:
LOWORD(a4) = (_WORD)a3 * v5;
*(_WORD *)(errnum + 131094) = ((unsigned __int16)a3 * (unsigned int)(unsigned __int16)v5) >> 16;
goto LABEL_3;
case 5u:
if ( (_WORD)a3 )
{
*(_WORD *)(errnum + 131094) = ((_DWORD)v5 << 16) / (int)(unsigned __int16)a3;
LOWORD(a4) = (unsigned __int16)v5 / (unsigned __int16)a3;
}
else
{
*(_WORD *)(errnum + 131094) = 0;
}
goto LABEL_3;
case 6u:
if ( (_WORD)a3 )
LOWORD(a4) = (unsigned __int16)v5 % (unsigned __int16)a3;
goto LABEL_3;
case 7u:
v7 = (_DWORD)v5 << (char)a3;
LOWORD(a4) = v7;
*(_WORD *)(errnum + 131094) = HIWORD(v7);
goto LABEL_3;
case 8u:
*(_WORD *)(errnum + 131094) = (int)((_DWORD)v5 << 16) >> (char)a3;
LODWORD(a4) = (int)(unsigned __int16)v5 >> (char)a3;
goto LABEL_3;
case 9u:
LOWORD(a4) = (unsigned __int16)a3 & v5;
goto LABEL_3;
case 0xAu:
LOWORD(a4) = (unsigned __int16)a3 | v5;
goto LABEL_3;
case 0xBu:
LOWORD(a4) = (unsigned __int16)a3 ^ v5;
goto LABEL_3;
case 0xCu:
return (_WORD)v5 != (unsigned __int16)a3;
case 0xDu:
return (_WORD)v5 == (unsigned __int16)a3;
case 0xEu:
return (unsigned __int16)v5 <= (unsigned __int16)a3;
case 0xFu:
return ((unsigned __int16)a3 & (unsigned __int16)v5) == 0;
default:
error((int)"Invalide Opcode", errnum, a3, (unsigned int)a3, v5);
LOWORD(a4) = 0;
LABEL_3:
*v4 = (_WORD)a4;
return 0;
}
}

canonical_decompiled_code:
bool __fastcall func0(__int64 errnum, unsigned __int16 a2, const char *a3, unsigned __int16 *a4)
{
_WORD *v4; // rbx
__int64 v5; // r8
int v7; // er8

v4 = a4;
v5 = *a4;
*(_QWORD *)(errnum + 131096) += *((unsigned __int8 *)&basic_opcodes + 4 * a2 + 3);
LOWORD(a4) = (_WORD)a3;
switch ( a2 )
{
case 1u:
goto LABEL_3;
case 2u:
LOWORD(a4) = v5 + (_WORD)a3;
*(_WORD *)(errnum + 131094) = ((unsigned __int16)a3 + (unsigned int)(unsigned __int16)v5) >> 16;
goto LABEL_3;
case 3u:
LOWORD(a4) = v5 - (_WORD)a3;
*(_WORD *)(errnum + 131094) = -(((unsigned __int16)v5 - (unsigned int)(unsigned __int16)a3) >> 16 != 0);
goto LABEL_3;
case 4u:
LOWORD(a4) = (_WORD)a3 * v5;
*(_WORD *)(errnum + 131094) = ((unsigned __int16)a3 * (unsigned int)(unsigned __int16)v5) >> 16;
goto LABEL_3;
case 5u:
if ( (_WORD)a3 )
{
*(_WORD *)(errnum + 131094) = ((_DWORD)v5 << 16) / (int)(unsigned __int16)a3;
LOWORD(a4) = (unsigned __int16)v5 / (unsigned __int16)a3;
}
else
{
*(_WORD *)(errnum + 131094) = 0;
}
goto LABEL_3;
case 6u:
if ( (_WORD)a3 )
LOWORD(a4) = (unsigned __int16)v5 % (unsigned __int16)a3;
goto LABEL_3;
case 7u:
v7 = (_DWORD)v5 << (char)a3;
LOWORD(a4) = v7;
*(_WORD *)(errnum + 131094) = HIWORD(v7);
goto LABEL_3;
case 8u:
*(_WORD *)(errnum + 131094) = (int)((_DWORD)v5 << 16) >> (char)a3;
LODWORD(a4) = (int)(unsigned __int16)v5 >> (char)a3;
goto LABEL_3;
case 9u:
LOWORD(a4) = (unsigned __int16)a3 & v5;
goto LABEL_3;
case 0xAu:
LOWORD(a4) = (unsigned __int16)a3 | v5;
goto LABEL_3;
case 0xBu:
LOWORD(a4) = (unsigned __int16)a3 ^ v5;
goto LABEL_3;
case 0xCu:
return (_WORD)v5 != (unsigned __int16)a3;
case 0xDu:
return (_WORD)v5 == (unsigned __int16)a3;
case 0xEu:
return (unsigned __int16)v5 <= (unsigned __int16)a3;
case 0xFu:
return ((unsigned __int16)a3 & (unsigned __int16)v5) == 0;
default:
error((int)"Invalide Opcode", errnum, a3, (unsigned int)a3, v5);
LOWORD(a4) = 0;
LABEL_3:
*v4 = (_WORD)a4;
return 0;
}
}

original_code:
_Bool basic_operation(dcpu16 *cpu, u16 code , u16 b, u16 *toSet)
{
  u16 a = *toSet;
  u32 result;
  cpu->cycle += basic_opcodes[code].cycle_cost;
  switch (code)
    {
    case 0x1: result = b; break;
    case 0x2: result = a+b; cpu->o = (result >> 16 ? 0x0001 : 0); break;
    case 0x3: result = a-b; cpu->o = (result >> 16 ? 0xFFFF : 0); break;
    case 0x4: result = a*b; cpu->o = (result >> 16); break;
    case 0x5: if (b) {result = a/b; cpu->o = (a << 16) / b; } else {result = cpu->o = 0;} break;
    case 0x6: if (b) {result = a%b;} else {result = 0;} break;
    case 0x7: result = a<<b; cpu->o = result >> 16; break;
    case 0x8: result = a>>b; cpu->o = ((a<<16)>>b); break;
    case 0x9: result = a&b; break;
    case 0xa: result = a|b; break;
    case 0xb: result = a^b; break;
    case 0xc: return (a != b);
    case 0xd: return (a == b);
    case 0xe: return (a <= b);
    case 0xf: return ((a&b) == 0);
    default:
      error("Invalide Opcode", cpu);
    }
  *toSet = result;
  return 0;
}

canonical_original_code:
_Bool basic_operation(struct dcpu16 *cpu, unsigned short code, unsigned short b, unsigned short *toSet)
{
  unsigned short a = *toSet;
  unsigned int result;
  cpu->cycle += basic_opcodes[code].cycle_cost;
  switch (code)
    {
    case 0x1: result = b; break;
    case 0x2: result = a+b; cpu->o = (result >> 16 ? 0x0001 : 0); break;
    case 0x3: result = a-b; cpu->o = (result >> 16 ? 0xFFFF : 0); break;
    case 0x4: result = a*b; cpu->o = (result >> 16); break;
    case 0x5: if (b) {result = a/b; cpu->o = (a << 16) / b; } else {result = cpu->o = 0;} break;
    case 0x6: if (b) {result = a%b;} else {result = 0;} break;
    case 0x7: result = a<<b; cpu->o = result >> 16; break;
    case 0x8: result = a>>b; cpu->o = ((a<<16)>>b); break;
    case 0x9: result = a&b; break;
    case 0xa: result = a|b; break;
    case 0xb: result = a^b; break;
    case 0xc: return (a != b);
    case 0xd: return (a == b);
    case 0xe: return (a <= b);
    case 0xf: return ((a&b) == 0);
    default:
      error("Invalide Opcode", cpu);
    }
  *toSet = result;
  return 0;
}

variable_types: struct dcpu16 { unsigned short unused; unsigned short ram[65536]; unsigned short r[8]; unsigned short pc; unsigned short sp; unsigned short o; long cycle; char *program_name; } *

variable_types:
code
unsigned short

variable_types:
b
unsigned short

variable_types:
toSet
unsigned short *

variable_types:
a
unsigned short

variable_types:
result
unsigned int


decompiled_code:
__int64 __fastcall extended_operation(__int64 errnum, unsigned __int16 a2, const char *a3)
{
__int16 v3; // cx
unsigned __int16 v4; // ax

if ( a2 == 1 )
{
v3 = *(_WORD *)(errnum + 131090);
v4 = *(_WORD *)(errnum + 131092) - 1;
*(_WORD *)(errnum + 131092) = v4;
*(_WORD *)(errnum + 2LL * v4 + 2) = v3;
*(_WORD *)(errnum + 131090) = (_WORD)a3;
}
else
{
error((int)"Invalide Opcode", errnum, a3);
}
*(_QWORD *)(errnum + 131096) += *((unsigned __int8 *)&non_basic_opcodes + 4 * a2 + 3);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 errnum, unsigned __int16 a2, const char *a3)
{
__int16 v3; // cx
unsigned __int16 v4; // ax

if ( a2 == 1 )
{
v3 = *(_WORD *)(errnum + 131090);
v4 = *(_WORD *)(errnum + 131092) - 1;
*(_WORD *)(errnum + 131092) = v4;
*(_WORD *)(errnum + 2LL * v4 + 2) = v3;
*(_WORD *)(errnum + 131090) = (_WORD)a3;
}
else
{
error((int)"Invalide Opcode", errnum, a3);
}
*(_QWORD *)(errnum + 131096) += *((unsigned __int8 *)&non_basic_opcodes + 4 * a2 + 3);
return 0LL;
}

original_code:
_Bool extended_operation(dcpu16 *cpu, u16 code , u16 a)
{
  switch (code)
    {
    case 0x1: cpu->ram[--(cpu->sp)] = cpu->pc; cpu->pc = a; break;
    default: error("Invalide Opcode", cpu);
    }
  cpu->cycle += non_basic_opcodes[code].cycle_cost;
  return 0;
}

canonical_original_code:
_Bool extended_operation(struct dcpu16 *cpu, unsigned short code, unsigned short a)
{
  switch (code)
    {
    case 0x1: cpu->ram[--(cpu->sp)] = cpu->pc; cpu->pc = a; break;
    default: error("Invalide Opcode", cpu);
    }
  cpu->cycle += non_basic_opcodes[code].cycle_cost;
  return 0;
}

user_defined_types: struct dcpu16 { unsigned short unused; unsigned short ram[65536]; unsigned short r[8]; unsigned short pc; unsigned short sp; unsigned short o; long cycle; char *program_name; }


error

decompiled_code:
void __fastcall __noreturn error(const char *a1, __int64 a2)
{
if ( a2 )
{
fprintf(stderr, "[ERROR] %s as 0x%x, generating core file...\n", a1, *(unsigned __int16 *)(a2 + 131090));
core(a1, a2);
}
else
{
fprintf(stderr, "[ERROR] %s, generating core file...\n", a1);
}
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(const char *a1, __int64 a2)
{
if ( a2 )
{
fprintf(stderr, "[ERROR] %s as 0x%x, generating core file...\n", a1, *(unsigned __int16 *)(a2 + 131090));
func0(a1, a2);
}
else
{
fprintf(stderr, "[ERROR] %s, generating core file...\n", a1);
}
exit(1);
}

original_code:
void error(char *str, dcpu16 *cpu)
{
  if (cpu != ((void *)0))
    {
      fprintf(stderr, "[ERROR] %s as 0x%x, generating core file...\n", str, cpu->pc);
      core(str, cpu);
    }
  else
    fprintf(stderr, "[ERROR] %s, generating core file...\n", str);
  exit(1);
}

canonical_original_code:
void error(char *str, struct dcpu16 *cpu)
{
  if (cpu != ((void *)0))
    {
      fprintf(stderr, "[ERROR] %s as 0x%x, generating core file...\n", str, cpu->pc);
      core(str, cpu);
    }
  else
    fprintf(stderr, "[ERROR] %s, generating core file...\n", str);
  exit(1);
}

variable_types:
str
char *


skipInstruction

decompiled_code:
__int64 __fastcall skipInstruction(__int64 a1)
{
__int64 v1; // rax
unsigned __int16 v2; // bp
__int64 result; // rax

v1 = *(unsigned __int16 *)(a1 + 131090);
v2 = *(_WORD *)(a1 + 2 * v1 + 2);
*(_WORD *)(a1 + 131090) = v1 + 1;
*(_WORD *)(a1 + 131090) += (unsigned __int8)opcode_pc_cost((v2 >> 4) & 0x3F);
LOBYTE(result) = opcode_pc_cost(v2 >> 10);
++*(_QWORD *)(a1 + 131096);
result = (unsigned __int8)result;
*(_WORD *)(a1 + 131090) += (unsigned __int8)result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
__int64 v1; // rax
unsigned __int16 v2; // bp
__int64 result; // rax

v1 = *(unsigned __int16 *)(a1 + 131090);
v2 = *(_WORD *)(a1 + 2 * v1 + 2);
*(_WORD *)(a1 + 131090) = v1 + 1;
*(_WORD *)(a1 + 131090) += (unsigned __int8)opcode_pc_cost((v2 >> 4) & 0x3F);
LOBYTE(result) = opcode_pc_cost(v2 >> 10);
++*(_QWORD *)(a1 + 131096);
result = (unsigned __int8)result;
*(_WORD *)(a1 + 131090) += (unsigned __int8)result;
return result;
}

original_code:
static inline void skipInstruction(dcpu16 *cpu)
{
  instruction i = {.value = cpu->ram[cpu->pc++]};
  cpu->pc += opcode_pc_cost(i.value_a);
  cpu->pc += opcode_pc_cost(i.value_b);
  cpu->cycle++;
}

canonical_original_code:
static inline void skipInstruction(struct dcpu16 *cpu)
{
  union instruction i = {.value = cpu->ram[cpu->pc++]};
  cpu->pc += opcode_pc_cost(i.value_a);
  cpu->pc += opcode_pc_cost(i.value_b);
  cpu->cycle++;
}

variable_types: union instruction { unsigned short value; struct { unsigned short opcode; unsigned short value_a; unsigned short value_b; }; }


handleInstruction

decompiled_code:
__int64 __fastcall handleInstruction(__int64 a1)
{
__int64 v1; // rax
unsigned __int16 v2; // bx
unsigned __int16 *v3; // rax
__int64 v5; // r13
unsigned __int16 *v6; // rax

v1 = *(unsigned __int16 *)(a1 + 131090);
v2 = *(_WORD *)(a1 + 2 * v1 + 2);
*(_WORD *)(a1 + 131090) = v1 + 1;
if ( (v2 & 0xF) == 0 )
{
v3 = (unsigned __int16 *)access_opcode(a1, v2 >> 10);
extended_operation(a1, (v2 >> 4) & 0x3F, *v3);
return 1LL;
}
v5 = access_opcode(a1, (v2 >> 4) & 0x3F);
v6 = (unsigned __int16 *)access_opcode(a1, v2 >> 10);
if ( !(unsigned __int8)basic_operation(a1, v2 & 0xF, *v6, v5) )
return 1LL;
skipInstruction(a1);
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
__int64 v1; // rax
unsigned __int16 v2; // bx
unsigned __int16 *v3; // rax
__int64 v5; // r13
unsigned __int16 *v6; // rax

v1 = *(unsigned __int16 *)(a1 + 131090);
v2 = *(_WORD *)(a1 + 2 * v1 + 2);
*(_WORD *)(a1 + 131090) = v1 + 1;
if ( (v2 & 0xF) == 0 )
{
v3 = (unsigned __int16 *)access_opcode(a1, v2 >> 10);
extended_operation(a1, (v2 >> 4) & 0x3F, *v3);
return 1LL;
}
v5 = access_opcode(a1, (v2 >> 4) & 0x3F);
v6 = (unsigned __int16 *)access_opcode(a1, v2 >> 10);
if ( !(unsigned __int8)basic_operation(a1, v2 & 0xF, *v6, v5) )
return 1LL;
func0(a1);
return 1LL;
}

original_code:
_Bool handleInstruction(dcpu16 *cpu)
{
  instruction i = {.value = cpu->ram[cpu->pc++]};
  if (i.opcode == 0) {
    extended_operation(cpu, i.value_a, *access_opcode(cpu, i.value_b));
  } else {
    if (basic_operation(cpu, i.opcode, *access_opcode(cpu, i.value_b), access_opcode(cpu, i.value_a)))
      skipInstruction(cpu);
  }
  return 1;
}

canonical_original_code:
_Bool handleInstruction(struct dcpu16 *cpu)
{
  union instruction i = {.value = cpu->ram[cpu->pc++]};
  if (i.opcode == 0) {
    extended_operation(cpu, i.value_a, *access_opcode(cpu, i.value_b));
  } else {
    if (basic_operation(cpu, i.opcode, *access_opcode(cpu, i.value_b), access_opcode(cpu, i.value_a)))
      skipInstruction(cpu);
  }
  return 1;
}

user_defined_types: union instruction { unsigned short value; struct { unsigned short opcode; unsigned short value_a; unsigned short value_b; }; }


decompiled_code:
__int64 __fastcall dcpu_start(__int64 a1)
{
const char *v1; // rdx

if ( !(unsigned __int8)loadFile() )
error((int)"Can't open file", 0, v1);
while ( (unsigned __int8)handleInstruction(a1) )
usleep(0x64u);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
const char *v1; // rdx

if ( !(unsigned __int8)loadFile() )
error((int)"Can't open file", 0, v1);
while ( (unsigned __int8)func1(a1) )
usleep(0x64u);
return 0LL;
}

original_code:
void* dcpu_start(void *arg)
{
  dcpu16 *cpu = arg;
  if (!loadFile(cpu))
    error("Can't open file", ((void *)0));
  while (handleInstruction(cpu))
    {
      usleep(100);
    }
  return ((void *)0);
}

canonical_original_code:
void *dcpu_start(void *arg)
{
  struct dcpu16 *cpu = arg;
  if (!loadFile(cpu))
    error("Can't open file", ((void *)0));
  while (handleInstruction(cpu))
    {
      usleep(100);
    }
  return ((void *)0);
}

variable_types:
arg
void *

return_type: void *


usage

decompiled_code:
int __fastcall usage(const char **a1)
{
return fprintf(stderr, "%s: [file.hex|file.bin ....]\n", *a1);
}

canonical_decompiled_code:
int __fastcall func0(const char **a1)
{
return fprintf(stderr, "%s: [file.hex|file.bin ....]\n", *a1);
}

original_code:
void usage(char **av)
{
  fprintf(stderr, "%s: [file.hex|file.bin ....]\n", av[0]);
}

variable_types:
av
char **


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

if ( a1 == 4 )
return 1LL;
if ( a1 > 4 )
{
result = 3LL;
if ( a1 != 6 )
result = (unsigned int)a1;
}
else
{
result = 4LL;
if ( a1 != 1 )
{
result = 6LL;
if ( a1 != 3 )
result = (unsigned int)a1;
}
}
return result;
}

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

if ( a1 == 4 )
return 1LL;
if ( a1 > 4 )
{
result = 3LL;
if ( a1 != 6 )
result = (unsigned int)a1;
}
else
{
result = 4LL;
if ( a1 != 1 )
{
result = 6LL;
if ( a1 != 3 )
result = (unsigned int)a1;
}
}
return result;
}

original_code:
int to_ncurses_color(int c)
{
  switch(c)
    {
    case 1: return 4;
    case 3: return 6;
    case 4: return 1;
    case 6: return 3;
    default: return c;
    }
}

variable_types: c int


repo: vigetlabs/bode

decompiled_code:
char *__fastcall file_get_extension(char *a1)
{
char *v1; // rax
char *v2; // r12
char *stringp; // [rsp+8h] [rbp-10h] BYREF

stringp = a1;
do
{
v2 = v1;
v1 = strsep(&stringp, ".");
}
while ( v1 );
return v2;
}

canonical_decompiled_code:
char *__fastcall func0(char *a1)
{
char *v1; // rax
char *v2; // r12
char *stringp; // [rsp+8h] [rbp-10h] BYREF

stringp = a1;
do
{
v2 = v1;
v1 = strsep(&stringp, ".");
}
while ( v1 );
return v2;
}

original_code:
char *
file_get_extension(char *filename)
{
    const char *d = ".";
    char *token,
         *ext,
         *fp = filename;
    while ((token = strsep(&fp, d)) != ((void *)0)) {
        ext = token;
    }
    return ext;
}

canonical_original_code:
char *file_get_extension(char *filename)
{
    char *d = ".";
    char *token; char *ext; char *fp = filename;
    while ((token = strsep(&fp, d)) != ((void *)0)) {
        ext = token;
    }
    return ext;
}

variable_types:
filename
char *

variable_types:
d
char *

variable_types:
token
char *

variable_types:
ext
char *

variable_types:
fp
char *


decompiled_code:
__int64 __fastcall file_content_type_for(__int64 a1, char *a2)
{
char *v2; // rax

v2 = file_get_extension(a2);
return mime_types_find_content_type(a1, v2);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, char *a2)
{
char *v2; // rax

v2 = func0(a2);
return mime_types_find_content_type(a1, v2);
}

original_code:
char *
file_content_type_for(MimeTypes *mime_types, char *filename)
{
    char *ext = file_get_extension(filename);
    return mime_types_find_content_type(mime_types, ext);
}

canonical_original_code:
char *file_content_type_for(struct MimeTypes *mime_types, char *filename)
{
    char *ext = file_get_extension(filename);
    return mime_types_find_content_type(mime_types, ext);
}

variable_types: struct MimeTypes { struct MimeType { char *content_type; char *extension; } **types; int count; int allocated; } *

user_defined_types: struct MimeTypes { struct MimeType **types; int count; int allocated; }


decompiled_code:
_BOOL8 __fastcall ends_with(const char *a1, unsigned __int8 a2)
{
return a1[(int)strlen(a1)] == a2;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(const char *a1, unsigned __int8 a2)
{
return a1[(int)strlen(a1)] == a2;
}

original_code:
int
ends_with(char *path, char end)
{
    int index = strlen(path);
    return path[index] == end;
}

variable_types:
path
char *

variable_types: end char

variable_types: index int


decompiled_code:
char *__fastcall path_append(const char *a1, __int64 a2)
{
char *v2; // rbp
char *ptr; // [rsp+8h] [rbp-20h] BYREF

v2 = (char *)calloc(6uLL, 1uLL);
if ( ends_with(a1, 0x2Fu) )
{
*(_DWORD *)v2 = 1931834149;
}
else
{
*(_DWORD *)v2 = 623866661;
v2[4] = 115;
}
asprintf(&ptr, v2, a1, a2);
free(v2);
return ptr;
}

canonical_decompiled_code:
char *__fastcall func3(const char *a1, __int64 a2)
{
char *v2; // rbp
char *ptr; // [rsp+8h] [rbp-20h] BYREF

v2 = (char *)calloc(6uLL, 1uLL);
if ( func2(a1, 0x2Fu) )
{
*(_DWORD *)v2 = 1931834149;
}
else
{
*(_DWORD *)v2 = 623866661;
v2[4] = 115;
}
asprintf(&ptr, v2, a1, a2);
free(v2);
return ptr;
}

original_code:
char *
path_append(char *path, const char *filename)
{
    char *full_path;
    char *format = calloc(6, sizeof(char));
    if (ends_with(path, '/')) {
        strncpy(format, "%s%s", 4);
    } else {
        strncpy(format, "%s/%s", 5);
    }
    asprintf(&full_path, format, path, filename);
    free(format);
    return full_path;
}

canonical_original_code:
char *path_append(char *path, char *filename)
{
    char *full_path;
    char *format = calloc(6, sizeof(char));
    if (ends_with(path, '/')) {
        strncpy(format, "%s%s", 4);
    } else {
        strncpy(format, "%s/%s", 5);
    }
    asprintf(&full_path, format, path, filename);
    free(format);
    return full_path;
}

variable_types:
format
char *


decompiled_code:
_BOOL8 __fastcall file_is_type(const char *a1, int a2)
{
int v2; // er8
_BOOL8 result; // rax
struct stat v4; // [rsp+0h] [rbp-98h] BYREF

v2 = stat(a1, &v4);
result = 0LL;
if ( !v2 )
result = (a2 & v4.st_mode) == a2;
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func4(const char *a1, int a2)
{
int v2; // er8
_BOOL8 result; // rax
struct stat v4; // [rsp+0h] [rbp-98h] BYREF

v2 = stat(a1, &v4);
result = 0LL;
if ( !v2 )
result = (a2 & v4.st_mode) == a2;
return result;
}

original_code:
int
file_is_type(char *filename, int type)
{
    struct stat fs;
    if (stat(filename, &fs) == 0) {
        return (fs.st_mode & type) == type;
    }
    return 0;
}

variable_types: type int

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:
_BOOL8 __fastcall is_file(const char *a1)
{
return file_is_type(a1, 0x8000);
}

canonical_decompiled_code:
_BOOL8 __fastcall func5(const char *a1)
{
return func4(a1, 0x8000);
}

original_code:
int
is_file(char *filename)
{
    return file_is_type(filename, 0100000);
}


decompiled_code:
_BOOL8 __fastcall is_dir(const char *a1)
{
return file_is_type(a1, 0x4000);
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(const char *a1)
{
return func4(a1, 0x4000);
}

original_code:
int
is_dir(char *filename)
{
    return file_is_type(filename, 0040000);
}


decompiled_code:
char *__fastcall file_path_for(char *s)
{
if ( is_file(s) )
return strdup(s);
if ( is_dir(s) )
return path_append(s, (__int64)"index.html");
return 0LL;
}

canonical_decompiled_code:
char *__fastcall func7(char *s)
{
if ( func5(s) )
return strdup(s);
if ( func6(s) )
return func3(s, (__int64)"index.html");
return 0LL;
}

original_code:
char *
file_path_for(char *requested_filename)
{
    char *filename = ((void *)0);
    if (is_file(requested_filename)) {
        filename = strdup(requested_filename);
    } else if (is_dir(requested_filename)) {
        filename = path_append(requested_filename, "index.html");
    }
    return filename;
}

canonical_original_code:
char *file_path_for(char *requested_filename)
{
    char *filename = ((void *)0);
    if (is_file(requested_filename)) {
        filename = strdup(requested_filename);
    } else if (is_dir(requested_filename)) {
        filename = path_append(requested_filename, "index.html");
    }
    return filename;
}


decompiled_code:
const char *__fastcall mime_types_find_content_type(__int64 **a1, const char *a2)
{
int v2; // eax
__int64 *v3; // rbx
__int64 v4; // r13
__int64 v5; // rbp

v2 = *((_DWORD *)a1 + 2);
if ( v2 <= 0 )
return "application/octet-stream";
v3 = *a1;
v4 = (__int64)&(*a1)[(unsigned int)(v2 - 1) + 1];
do
{
v5 = *v3;
if ( !strcmp(a2, *(const char **)(*v3 + 8)) )
return *(const char **)v5;
++v3;
}
while ( v3 != (__int64 *)v4 );
return "application/octet-stream";
}

canonical_decompiled_code:
const char *__fastcall func0(__int64 **a1, const char *a2)
{
int v2; // eax
__int64 *v3; // rbx
__int64 v4; // r13
__int64 v5; // rbp

v2 = *((_DWORD *)a1 + 2);
if ( v2 <= 0 )
return "application/octet-stream";
v3 = *a1;
v4 = (__int64)&(*a1)[(unsigned int)(v2 - 1) + 1];
do
{
v5 = *v3;
if ( !strcmp(a2, *(const char **)(*v3 + 8)) )
return *(const char **)v5;
++v3;
}
while ( v3 != (__int64 *)v4 );
return "application/octet-stream";
}

original_code:
char *
mime_types_find_content_type(MimeTypes *mime_types, char *ext)
{
    int i = 0;
    for (i = 0; i < mime_types->count; i++) {
        if (strcmp(ext, mime_types->types[i]->extension) == 0) {
            return mime_types->types[i]->content_type;
        }
    }
    return "application/octet-stream";
}

canonical_original_code:
char *mime_types_find_content_type(struct MimeTypes *mime_types, char *ext)
{
    int i = 0;
    for (i = 0; i < mime_types->count; i++) {
        if (strcmp(ext, mime_types->types[i]->extension) == 0) {
            return mime_types->types[i]->content_type;
        }
    }
    return "application/octet-stream";
}

variable_types: i int

user_defined_types: struct MimeType { char *content_type; char *extension; }


decompiled_code:
void __fastcall mime_type_free(void *ptr)
{
void *v2; // rdi
void *v3; // rdi

v2 = *(void **)ptr;
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 1);
if ( v3 )
free(v3);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
void *v2; // rdi
void *v3; // rdi

v2 = *(void **)ptr;
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 1);
if ( v3 )
free(v3);
free(ptr);
}

original_code:
void
mime_type_free(MimeType *mime_type)
{
    if (mime_type->content_type) { free(mime_type->content_type); }
    if (mime_type->extension) { free(mime_type->extension); }
    free(mime_type);
}

canonical_original_code:
void mime_type_free(struct MimeType *mime_type)
{
    if (mime_type->content_type) { free(mime_type->content_type); }
    if (mime_type->extension) { free(mime_type->extension); }
    free(mime_type);
}

variable_types: struct MimeType { char *content_type; char *extension; } *


decompiled_code:
void __fastcall mime_types_free(void *ptr)
{
__int64 v2; // rbx
void *v3; // rdi

if ( *((int *)ptr + 2) > 0 )
{
v2 = 0LL;
do
{
v3 = *(void **)(*(_QWORD *)ptr + 8 * v2++);
mime_type_free(v3);
}
while ( *((_DWORD *)ptr + 2) > (int)v2 );
}
free(*(void **)ptr);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
__int64 v2; // rbx
void *v3; // rdi

if ( *((int *)ptr + 2) > 0 )
{
v2 = 0LL;
do
{
v3 = *(void **)(*(_QWORD *)ptr + 8 * v2++);
func1(v3);
}
while ( *((_DWORD *)ptr + 2) > (int)v2 );
}
free(*(void **)ptr);
free(ptr);
}

original_code:
void
mime_types_free(MimeTypes *mime_types)
{
    int i = 0;
    for (i = 0; i < mime_types->count; i++) {
        mime_type_free(mime_types->types[i]);
    }
    free(mime_types->types);
    free(mime_types);
}

canonical_original_code:
void mime_types_free(struct MimeTypes *mime_types)
{
    int i = 0;
    for (i = 0; i < mime_types->count; i++) {
        mime_type_free(mime_types->types[i]);
    }
    free(mime_types->types);
    free(mime_types);
}


decompiled_code:
void __fastcall config_free(void *ptr)
{
void *v2; // rdi
void *v3; // rdi

v2 = *(void **)ptr;
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 1);
if ( v3 )
free(v3);
free(ptr);
}

canonical_decompiled_code:
void __fastcall func0(void *ptr)
{
void *v2; // rdi
void *v3; // rdi

v2 = *(void **)ptr;
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 1);
if ( v3 )
free(v3);
free(ptr);
}

original_code:
void
config_free(Config *config)
{
    if (config->document_root) { free(config->document_root); }
    if (config->config_dir) { free(config->config_dir); }
    free(config);
}

canonical_original_code:
void config_free(struct Config *config)
{
    if (config->document_root) { free(config->document_root); }
    if (config->config_dir) { free(config->config_dir); }
    free(config);
}

variable_types: struct Config { char *document_root; char *config_dir; int port; } *


decompiled_code:
__int64 __fastcall write_value(void **a1, const char *a2, int a3)
{
char *v4; // rax

v4 = (char *)realloc(*a1, 8LL * a3);
*a1 = v4;
strncpy(v4, a2, a3 - 1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(void **a1, const char *a2, int a3)
{
char *v4; // rax

v4 = (char *)realloc(*a1, 8LL * a3);
*a1 = v4;
strncpy(v4, a2, a3 - 1);
return 0LL;
}

original_code:
int
write_value(char **target, char *value, int buf_length)
{
    *target = realloc(*target, buf_length * sizeof(char *));
    strncpy(*target, value, buf_length - 1);
    return 0;
}

variable_types:
target
char **

variable_types:
value
char *


decompiled_code:
void **config_create()
{
void **v0; // rax
void **v1; // r12
void **result; // rax

v0 = (void **)malloc(0x18uLL);
*v0 = 0LL;
v1 = v0;
v0[1] = 0LL;
write_value(v0, "root", 1024);
write_value(v1 + 1, "config", 1024);
result = v1;
*((_DWORD *)v1 + 4) = 8080;
return result;
}

canonical_decompiled_code:
void **func2()
{
void **v0; // rax
void **v1; // r12
void **result; // rax

v0 = (void **)malloc(0x18uLL);
*v0 = 0LL;
v1 = v0;
v0[1] = 0LL;
func1(v0, "root", 1024);
func1(v1 + 1, "config", 1024);
result = v1;
*((_DWORD *)v1 + 4) = 8080;
return result;
}

original_code:
Config *
config_create()
{
    Config *config = malloc(sizeof(Config));
    config->document_root = ((void *)0);
    config->config_dir = ((void *)0);
    write_value(&config->document_root, "root", 1024);
    write_value(&config->config_dir, "config", 1024);
    config->port = 8080;
    return config;
}

canonical_original_code:
struct Config *config_create()
{
    struct Config *config = malloc(sizeof(Config));
    config->document_root = ((void *)0);
    config->config_dir = ((void *)0);
    write_value(&config->document_root, "root", 1024);
    write_value(&config->config_dir, "config", 1024);
    config->port = 8080;
    return config;
}

return_type: struct Config { char *document_root; char *config_dir; int port; } *


decompiled_code:
__int64 __fastcall config_set(__int64 a1, int a2, const char *a3)
{
__int64 result; // rax

switch ( a2 )
{
case 2:
a1 += 8LL;
return write_value((void **)a1, a3, 1024);
case 3:
result = *(unsigned int *)a3;
*(_DWORD *)(a1 + 16) = result;
return result;
case 1:
return write_value((void **)a1, a3, 1024);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2, const char *a3)
{
__int64 result; // rax

switch ( a2 )
{
case 2:
a1 += 8LL;
return func1((void **)a1, a3, 1024);
case 3:
result = *(unsigned int *)a3;
*(_DWORD *)(a1 + 16) = result;
return result;
case 1:
return func1((void **)a1, a3, 1024);
}
return result;
}

original_code:
void
config_set(Config *config, int attr, void *value)
{
    switch (attr) {
        case 1:
            write_value(&config->document_root, (char *)value, 1024);
            break;
        case 2:
            write_value(&config->config_dir, (char *)value, 1024);
            break;
        case 3:
            config->port = *(int *)value;
            break;
        default:
            break;
    }
}

canonical_original_code:
void config_set(struct Config *config, int attr, void *value)
{
    switch (attr) {
        case 1:
            write_value(&config->document_root, (char *)value, 1024);
            break;
        case 2:
            write_value(&config->config_dir, (char *)value, 1024);
            break;
        case 3:
            config->port = *(int *)value;
            break;
        default:
            break;
    }
}

variable_types: attr int

variable_types:
value
void *

user_defined_types: struct Config { char *document_root; char *config_dir; int port; }


decompiled_code:
void __fastcall header_free(void *ptr)
{
void *v2; // rdi
void *v3; // rdi

v2 = *(void **)ptr;
if ( v2 )
free(v2);
v3 = (void *)*((_QWORD *)ptr + 1);
if ( v3 )
free(v3);
free(ptr);
}

original_code:
void
header_free(Header *header)
{
    if (header->key) { free(header->key); }
    if (header->value) { free(header->value); }
    free(header);
}

canonical_original_code:
void header_free(struct Header *header)
{
    if (header->key) { free(header->key); }
    if (header->value) { free(header->value); }
    free(header);
}

variable_types: struct Header { char *key; char *value; } *


repo: vighneshbirodkar/vigredis

original_code:
int string_get_bit(vr_string* s,int n)
{
    int index,offset;
    char mask;
    char *str = s->string;
    if(n > s->len*8)
    {
        return 0;
    }
    else
    {
        index = n/8;
        offset = (n%8);
        mask = 0x80 >> offset;
        if(str[index] & mask)
            return 1;
        else
            return 0;
    }
}

canonical_original_code:
int string_get_bit(struct vr_string *s, int n)
{
    int index; int offset;
    char mask;
    char *str = s->string;
    if(n > s->len*8)
    {
        return 0;
    }
    else
    {
        index = n/8;
        offset = (n%8);
        mask = 0x80 >> offset;
        if(str[index] & mask)
            return 1;
        else
            return 0;
    }
}

variable_types: struct vr_string { int len; char *string; } *

variable_types: n int

variable_types: offset int

variable_types: mask char

variable_types:
str
char *


original_code:
int string_set_bit(vr_string* s,int n,char bit)
{
    int index,offset;
    char mask,value;
    char* str = s->string;
    char *new_str;
    int old_val;
    if ( n < s->len*8)
    {
        index = n/8;
        offset = (n%8);
        mask = 0x80 >> offset;
        value = (bit*0x80) >> offset;
        old_val = str[index] & mask;
        str[index] = (str[index] & ~mask) | (value & mask) ;
    }
    else
    {
        index = n/8;
        new_str = (char*)malloc(index+1);
        bzero(new_str,index+1);
        strncpy(new_str,str,s->len);
        if(str != ((void *)0))
            free(str);
        s->string = new_str;
        s->len = index + 1;
        str = s->string;
        index = n/8;
        offset = (n%8);
        mask = 0x80 >> offset;
        value = (bit*0x80) >> offset;
        old_val = str[index] & mask;
        str[index] = (str[index] & ~mask) | (value & mask) ;
    }
    if (old_val)
        return 1;
    else
        return 0;
}

canonical_original_code:
int string_set_bit(struct vr_string *s, int n, char bit)
{
    int index; int offset;
    char mask; char value;
    char *str = s->string;
    char *new_str;
    int old_val;
    if ( n < s->len*8)
    {
        index = n/8;
        offset = (n%8);
        mask = 0x80 >> offset;
        value = (bit*0x80) >> offset;
        old_val = str[index] & mask;
        str[index] = (str[index] & ~mask) | (value & mask) ;
    }
    else
    {
        index = n/8;
        new_str = (char *)malloc(index+1);
        bzero(new_str,index+1);
        strncpy(new_str,str,s->len);
        if(str != ((void *)0))
            free(str);
        s->string = new_str;
        s->len = index + 1;
        str = s->string;
        index = n/8;
        offset = (n%8);
        mask = 0x80 >> offset;
        value = (bit*0x80) >> offset;
        old_val = str[index] & mask;
        str[index] = (str[index] & ~mask) | (value & mask) ;
    }
    if (old_val)
        return 1;
    else
        return 0;
}

variable_types: bit char

variable_types: index int

variable_types: value char

user_defined_types: struct vr_string { int len; char *string; }


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

gettimeofday(&v1, 0LL);
return (double)SLODWORD(v1.tv_usec) / 1000.0 / 1000.0 / 1000.0 + (double)SLODWORD(v1.tv_sec);
}

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

gettimeofday(&v1, 0LL);
return (double)SLODWORD(v1.tv_usec) / 1000.0 / 1000.0 / 1000.0 + (double)SLODWORD(v1.tv_sec);
}

original_code:
double get_time_ms()
{
    struct timeval now;
    double d;
    gettimeofday(&now, ((void *)0));
    d = now.tv_sec + ((double)now.tv_usec)/1000/1000/1000;
    return d;
}

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

variable_types: d double

return_type: double


decompiled_code:
void __fastcall str_lower(char *a1)
{
char *v1; // rbp
__int64 v2; // rbx
const __int32_t **v3; // rdx

if ( a1 )
{
v1 = a1;
v2 = *a1;
if ( (_BYTE)v2 )
{
v3 = _ctype_tolower_loc();
do
{
*v1++ = (*v3)[v2];
v2 = *v1;
}
while ( (_BYTE)v2 );
}
}
}

canonical_decompiled_code:
void __fastcall func2(char *a1)
{
char *v1; // rbp
__int64 v2; // rbx
const __int32_t **v3; // rdx

if ( a1 )
{
v1 = a1;
v2 = *a1;
if ( (_BYTE)v2 )
{
v3 = _ctype_tolower_loc();
do
{
*v1++ = (*v3)[v2];
v2 = *v1;
}
while ( (_BYTE)v2 );
}
}
}

original_code:
void str_lower( char* s )
{
    int i=0;
    if (s == ((void *)0))
        return;
    while(s[i])
    {
        s[i] = tolower(s[i]);
        i++;
    }
}

canonical_original_code:
void str_lower(char *s)
{
    int i = 0;
    if (s == ((void *)0))
        return;
    while(s[i])
    {
        s[i] = tolower(s[i]);
        i++;
    }
}

variable_types:
s
char *

variable_types: i int


rstrip

decompiled_code:
char *__fastcall rstrip(const char *a1)
{
char *result; // rax
int v3; // ebp
int v4; // er12
const unsigned __int16 **v5; // rsi
__int64 v6; // rdi

result = (char *)strlen(a1);
v3 = (_DWORD)result - 1;
if ( (int)result - 1 > 0 )
{
v4 = (int)result;
v5 = _ctype_b_loc();
v6 = (__int64)&a1[v3 - 1 - (v4 - 2)];
result = (char *)&a1[v3];
do
{
if ( ((*v5)[*result] & 0x2000) == 0 )
break;
*result-- = 0;
}
while ( result != (char *)v6 );
}
return result;
}

canonical_decompiled_code:
char *__fastcall func3(const char *a1)
{
char *result; // rax
int v3; // ebp
int v4; // er12
const unsigned __int16 **v5; // rsi
__int64 v6; // rdi

result = (char *)strlen(a1);
v3 = (_DWORD)result - 1;
if ( (int)result - 1 > 0 )
{
v4 = (int)result;
v5 = _ctype_b_loc();
v6 = (__int64)&a1[v3 - 1 - (v4 - 2)];
result = (char *)&a1[v3];
do
{
if ( ((*v5)[*result] & 0x2000) == 0 )
break;
*result-- = 0;
}
while ( result != (char *)v6 );
}
return result;
}

original_code:
void rstrip(char* s)
{
    int i = strlen(s)-1;
    if (s == ((void *)0))
        return;
    while(i > 0)
    {
        if(((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short int) _ISspace))
            s[i] = 0;
        else
            break;
        i--;
    }
}

canonical_original_code:
void rstrip(char *s)
{
    int i = strlen(s)-1;
    if (s == ((void *)0))
        return;
    while(i > 0)
    {
        if(((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short) _ISspace))
            s[i] = 0;
        else
            break;
        i--;
    }
}


isint

decompiled_code:
__int64 __fastcall isint(char *a1)
{
__int64 v1; // rbx
char *v2; // rdi
const unsigned __int16 *v3; // rax

v1 = *a1;
if ( !(_BYTE)v1 )
return 1LL;
v2 = a1 + 1;
v3 = *_ctype_b_loc();
while ( (v3[v1] & 0x800) != 0 )
{
v1 = *v2++;
if ( !(_BYTE)v1 )
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *a1)
{
__int64 v1; // rbx
char *v2; // rdi
const unsigned __int16 *v3; // rax

v1 = *a1;
if ( !(_BYTE)v1 )
return 1LL;
v2 = a1 + 1;
v3 = *_ctype_b_loc();
while ( (v3[v1] & 0x800) != 0 )
{
v1 = *v2++;
if ( !(_BYTE)v1 )
return 1LL;
}
return 0LL;
}

original_code:
int isint(char *s)
{
    int i=0;
    while(s[i])
    {
        if(!((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short int) _ISdigit))
            return 0;
        i++;
    }
    return 1;
}

canonical_original_code:
int isint(char *s)
{
    int i = 0;
    while(s[i])
    {
        if(!((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short) _ISdigit))
            return 0;
        i++;
    }
    return 1;
}


decompiled_code:
__int64 __fastcall isint_neg(char *a1)
{
char v1; // bl
char *v2; // rdi
const unsigned __int16 *v3; // rdx

v1 = *a1;
if ( !*a1 )
return 1LL;
v2 = a1 + 1;
v3 = *_ctype_b_loc();
while ( (v3[v1] & 0x800) != 0 || v1 == 45 )
{
v1 = *v2++;
if ( !v1 )
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(char *a1)
{
char v1; // bl
char *v2; // rdi
const unsigned __int16 *v3; // rdx

v1 = *a1;
if ( !*a1 )
return 1LL;
v2 = a1 + 1;
v3 = *_ctype_b_loc();
while ( (v3[v1] & 0x800) != 0 || v1 == 45 )
{
v1 = *v2++;
if ( !v1 )
return 1LL;
}
return 0LL;
}

original_code:
int isint_neg(char *s)
{
    int i=0;
    while(s[i])
    {
        if(!(((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short int) _ISdigit) || s[i] == '-'))
            return 0;
        i++;
    }
    return 1;
}

canonical_original_code:
int isint_neg(char *s)
{
    int i = 0;
    while(s[i])
    {
        if(!(((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short) _ISdigit) || s[i] == '-'))
            return 0;
        i++;
    }
    return 1;
}


isdouble

decompiled_code:
__int64 __fastcall isdouble(char *a1)
{
char v1; // bl
char *v2; // rdi
const unsigned __int16 *v3; // rdx

v1 = *a1;
if ( !*a1 )
return 1LL;
v2 = a1 + 1;
v3 = *_ctype_b_loc();
while ( (v3[v1] & 0x800) != 0 || (unsigned __int8)(v1 - 45) <= 1u )
{
v1 = *v2++;
if ( !v1 )
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *a1)
{
char v1; // bl
char *v2; // rdi
const unsigned __int16 *v3; // rdx

v1 = *a1;
if ( !*a1 )
return 1LL;
v2 = a1 + 1;
v3 = *_ctype_b_loc();
while ( (v3[v1] & 0x800) != 0 || (unsigned __int8)(v1 - 45) <= 1u )
{
v1 = *v2++;
if ( !v1 )
return 1LL;
}
return 0LL;
}

original_code:
int isdouble(char *s)
{
    int i=0;
    while(s[i])
    {
        if(! (((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short int) _ISdigit) || (s[i] == '.') || (s[i] == '-') ) )
            return 0;
        i++;
    }
    return 1;
}

canonical_original_code:
int isdouble(char *s)
{
    int i = 0;
    while(s[i])
    {
        if(! (((*__ctype_b_loc ())[(int) ((s[i]))] & (unsigned short) _ISdigit) || (s[i] == '.') || (s[i] == '-') ) )
            return 0;
        i++;
    }
    return 1;
}


decompiled_code:
__int64 __fastcall dict_init(__int64 a1, char a2)
{
unsigned int v3; // ebx
char *v4; // rax
__int64 v5; // rdi
__int64 result; // rax

v3 = 0;
*(_QWORD *)a1 = 0x800000000LL;
v4 = (char *)malloc(0x80uLL);
*(_BYTE *)(a1 + 8) = a2;
for ( *(_QWORD *)(a1 + 16) = v4; ; v4 = *(char **)(a1 + 16) )
{
v5 = v3++;
result = list_init(&v4[16 * v5], (unsigned int)a2);
if ( *(_DWORD *)(a1 + 4) <= v3 )
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, char a2)
{
unsigned int v3; // ebx
char *v4; // rax
__int64 v5; // rdi
__int64 result; // rax

v3 = 0;
*(_QWORD *)a1 = 0x800000000LL;
v4 = (char *)malloc(0x80uLL);
*(_BYTE *)(a1 + 8) = a2;
for ( *(_QWORD *)(a1 + 16) = v4; ; v4 = *(char **)(a1 + 16) )
{
v5 = v3++;
result = list_init(&v4[16 * v5], (unsigned int)a2);
if ( *(_DWORD *)(a1 + 4) <= v3 )
break;
}
return result;
}

original_code:
void dict_init(dict* d,char type)
{
    uint32_t i;
    d->len = 0;
    d->size = 8;
    d->table = (list*)malloc(sizeof(list)*8);
    d->type = type;
    for(i=0;i < d->size ;i ++)
        list_init(&d->table[i],type);
}

canonical_original_code:
void dict_init(struct dict_ *d, char type)
{
    unsigned int i;
    d->len = 0;
    d->size = 8;
    d->table = (struct list *)malloc(sizeof(list)*8);
    d->type = type;
    for(i=0;i < d->size ;i ++)
        list_init(&d->table[i],type);
}

variable_types: struct dict_ { unsigned int len; unsigned int size; char type; struct list { char type; unsigned int len; struct list_node { struct list_node *next; int klen; char *key; union vr_object { double value; struct set { struct dict_ set_dict; struct skip_list { struct skip_list_node { double score; struct skip_list_node **next; char *key; int klen; } *header; int level; } set_list; int len; } *obj_set; struct vr_string { int len; char *string; } string; } object; double expiry; } *root; } *table; } *

variable_types: type char

variable_types:
i
unsigned int

user_defined_types: struct dict_ { unsigned int len; unsigned int size; char type; struct list *table; }

user_defined_types: struct list { char type; unsigned int len; struct list_node *root; }

user_defined_types: struct list_node { struct list_node *next; int klen; char *key; union vr_object object; double expiry; }

user_defined_types: union vr_object { double value; struct set *obj_set; struct vr_string string; }

user_defined_types: struct set { struct dict_ set_dict; struct skip_list set_list; int len; }

user_defined_types: struct skip_list { struct skip_list_node *header; int level; }


decompiled_code:
__int64 __fastcall dict_get_bit(__int64 a1, __int64 a2, unsigned int a3, unsigned int a4, _QWORD *a5)
{
unsigned int v8; // edx
__int64 v9; // rax
__int64 result; // rax

v8 = (unsigned int)hash_string_32(a2, a3) % *(_DWORD *)(a1 + 4);
if ( *(_BYTE *)(a1 + 8) == 1 )
{
v9 = list_find(*(_QWORD *)(a1 + 16) + 16LL * v8, a2, a3);
if ( v9 )
{
*a5 = *(_QWORD *)(v9 + 40);
result = string_get_bit(v9 + 24, a4);
}
else
{
result = 0LL;
}
}
else
{
puts("Incompatible Dict Operation");
result = 4294967293LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2, unsigned int a3, unsigned int a4, _QWORD *a5)
{
unsigned int v8; // edx
__int64 v9; // rax
__int64 result; // rax

v8 = (unsigned int)hash_string_32(a2, a3) % *(_DWORD *)(a1 + 4);
if ( *(_BYTE *)(a1 + 8) == 1 )
{
v9 = list_find(*(_QWORD *)(a1 + 16) + 16LL * v8, a2, a3);
if ( v9 )
{
*a5 = *(_QWORD *)(v9 + 40);
result = string_get_bit(v9 + 24, a4);
}
else
{
result = 0LL;
}
}
else
{
puts("Incompatible Dict Operation");
result = 4294967293LL;
}
return result;
}

original_code:
int dict_get_bit(dict *d,char* key,int klen,int n,double* expiry)
{
    list_node* tmp;
    uint32_t hash = hash_string_32(key,klen);
    uint32_t index = hash % d->size;
    if(d->type != 1)
    {
        printf("Incompatible Dict Operation\n");
        return -3;
    }
    tmp = list_find(&d->table[index],key,klen);
    if(tmp)
    {
        *expiry = tmp->expiry;
        return string_get_bit(&tmp->object.string,n);
    }
    else
        return 0;
}

canonical_original_code:
int dict_get_bit(struct dict_ *d, char *key, int klen, int n, double *expiry)
{
    struct list_node *tmp;
    unsigned int hash = hash_string_32(key,klen);
    unsigned int index = hash % d->size;
    if(d->type != 1)
    {
        printf("Incompatible Dict Operation\n");
        return -3;
    }
    tmp = list_find(&d->table[index],key,klen);
    if(tmp)
    {
        *expiry = tmp->expiry;
        return string_get_bit(&tmp->object.string,n);
    }
    else
        return 0;
}

variable_types:
key
char *

variable_types: klen int

variable_types:
expiry
double *

variable_types: struct list_node { struct list_node *next; int klen; char *key; union vr_object { double value; struct set { struct dict_ { unsigned int len; unsigned int size; char type; struct list { char type; unsigned int len; struct list_node *root; } *table; } set_dict; struct skip_list { struct skip_list_node { double score; struct skip_list_node **next; char *key; int klen; } *header; int level; } set_list; int len; } *obj_set; struct vr_string { int len; char *string; } string; } object; double expiry; } *

variable_types:
hash
unsigned int

variable_types:
index
unsigned int


original_code:
void dict_expand(dict *d)
{
    list* old_table = d->table;
    uint32_t old_size = d->size;
    uint32_t i;
    list_node *tmp,*current;
    d->table = (list*)malloc(sizeof(list)*old_size*2);
    d->size = old_size*2;
    for(i=0;i < d->size ;i ++)
        list_init(&d->table[i],d->type);
    d->len = 0;
    for(i=0;i < old_size;i++)
    {
        current = old_table[i].root;
        while(current != ((void *)0))
        {
            tmp = current;
            current = current->next;
            dict_add_object(d,tmp->key,tmp->klen,tmp->object,0,tmp->expiry);
            free(tmp->key);
            free(tmp);
        }
    }
    free(old_table);
}

canonical_original_code:
void dict_expand(struct dict_ *d)
{
    struct list *old_table = d->table;
    unsigned int old_size = d->size;
    unsigned int i;
    struct list_node *tmp; struct list_node *current;
    d->table = (struct list *)malloc(sizeof(list)*old_size*2);
    d->size = old_size*2;
    for(i=0;i < d->size ;i ++)
        list_init(&d->table[i],d->type);
    d->len = 0;
    for(i=0;i < old_size;i++)
    {
        current = old_table[i].root;
        while(current != ((void *)0))
        {
            tmp = current;
            current = current->next;
            dict_add_object(d,tmp->key,tmp->klen,tmp->object,0,tmp->expiry);
            free(tmp->key);
            free(tmp);
        }
    }
    free(old_table);
}

variable_types: struct list { char type; unsigned int len; struct list_node { struct list_node *next; int klen; char *key; union vr_object { double value; struct set { struct dict_ { unsigned int len; unsigned int size; char type; struct list *table; } set_dict; struct skip_list { struct skip_list_node { double score; struct skip_list_node **next; char *key; int klen; } *header; int level; } set_list; int len; } *obj_set; struct vr_string { int len; char *string; } string; } object; double expiry; } *root; } *


decompiled_code:
__int64 __fastcall dict_add_object(__int64 a1, __int64 a2, unsigned int a3, __int64 a4, __int64 a5, unsigned int a6, double a7)
{
unsigned int v11; // eax
unsigned int v12; // eax
bool v13; // zf
unsigned int v14; // er12
unsigned int v15; // eax

v11 = hash_string_32(a2, a3);
v12 = list_add_object(16LL * (v11 % *(_DWORD *)(a1 + 4)) + *(_QWORD *)(a1 + 16), a2, a3, a4, a5, a6, a7);
v13 = v12 == 0;
v14 = v12;
v15 = *(_DWORD *)a1;
if ( v13 )
*(_DWORD *)a1 = ++v15;
if ( *(_DWORD *)(a1 + 4) < v15 )
dict_expand(a1);
return v14;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2, unsigned int a3, __int64 a4, __int64 a5, unsigned int a6, double a7)
{
unsigned int v11; // eax
unsigned int v12; // eax
bool v13; // zf
unsigned int v14; // er12
unsigned int v15; // eax

v11 = hash_string_32(a2, a3);
v12 = list_add_object(16LL * (v11 % *(_DWORD *)(a1 + 4)) + *(_QWORD *)(a1 + 16), a2, a3, a4, a5, a6, a7);
v13 = v12 == 0;
v14 = v12;
v15 = *(_DWORD *)a1;
if ( v13 )
*(_DWORD *)a1 = ++v15;
if ( *(_DWORD *)(a1 + 4) < v15 )
func2(a1);
return v14;
}

original_code:
int dict_add_object(dict *d,char* key,int klen,vr_object object,int flag,double expiry)
{
    uint32_t hash = hash_string_32(key,klen);
    uint32_t index = hash % d->size;
    int ret_val;
    ret_val = list_add_object(&d->table[index],key,klen,object,flag,expiry);
    if( ret_val == 0)
        d->len++;
    if(d->len > d->size)
        dict_expand(d);
    return ret_val;
}

canonical_original_code:
int dict_add_object(struct dict_ *d, char *key, int klen, union vr_object object, int flag, double expiry)
{
    unsigned int hash = hash_string_32(key,klen);
    unsigned int index = hash % d->size;
    int ret_val;
    ret_val = list_add_object(&d->table[index],key,klen,object,flag,expiry);
    if( ret_val == 0)
        d->len++;
    if(d->len > d->size)
        dict_expand(d);
    return ret_val;
}

variable_types:
object
union vr_object

variable_types: flag int

variable_types: expiry double

user_defined_types: struct skip_list_node { double score; struct skip_list_node **next; char *key; int klen; }