lxh

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

result = (unsigned __int8)memory[(unsigned __int16)operand];
regxh = memory[(unsigned __int16)operand];
return result;
}

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

result = (unsigned __int8)memory[(unsigned __int16)operand];
regxh = memory[(unsigned __int16)operand];
return result;
}

original_code:
static void lxh() {
   regxh = memory[operand];
}


lxl

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

result = (unsigned __int8)memory[(unsigned __int16)operand];
regxl = memory[(unsigned __int16)operand];
return result;
}

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

result = (unsigned __int8)memory[(unsigned __int16)operand];
regxl = memory[(unsigned __int16)operand];
return result;
}

original_code:
static void lxl() {
   regxl = memory[operand];
}

return_type: void


stb

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

result = (unsigned __int16)operand;
memory[(unsigned __int16)operand] = regb;
return result;
}

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

result = (unsigned __int16)operand;
memory[(unsigned __int16)operand] = regb;
return result;
}

original_code:
static void stb() {
   memory[operand] = regb;
}


stc

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

result = (unsigned __int16)operand;
memory[(unsigned __int16)operand] = regc;
return result;
}

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

result = (unsigned __int16)operand;
memory[(unsigned __int16)operand] = regc;
return result;
}

original_code:
static void stc() {
   memory[operand] = regc;
}


sxh

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

result = (unsigned __int16)operand;
memory[(unsigned __int16)operand] = regxh;
return result;
}

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

result = (unsigned __int16)operand;
memory[(unsigned __int16)operand] = regxh;
return result;
}

original_code:
static void sxh() {
   memory[operand] = regxh;
}


sxl

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

result = (unsigned __int16)operand;
memory[(unsigned __int16)operand] = regxl;
return result;
}

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

result = (unsigned __int16)operand;
memory[(unsigned __int16)operand] = regxl;
return result;
}

original_code:
static void sxl() {
   memory[operand] = regxl;
}


sta

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

result = (unsigned __int16)operand;
memory[(unsigned __int16)operand] = rega;
return result;
}

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

result = (unsigned __int16)operand;
memory[(unsigned __int16)operand] = rega;
return result;
}

original_code:
static void sta() {
   memory[operand] = rega;
}


and

decompiled_code:
char and()
{
char result; // al

rega = regc & regb;
c_flag = 0;
z_flag = ((unsigned __int8)regc & (unsigned __int8)regb) == 0;
result = (unsigned __int8)(regc & regb) >> 7;
n_flag = result;
return result;
}

canonical_decompiled_code:
char func16()
{
char result; // al

rega = regc & regb;
c_flag = 0;
z_flag = ((unsigned __int8)regc & (unsigned __int8)regb) == 0;
result = (unsigned __int8)(regc & regb) >> 7;
n_flag = result;
return result;
}

original_code:
static void and() {
   rega = regb & regc;
   c_flag = 0;
   z_flag = rega == 0;
   n_flag = rega >> 7;
}


or

decompiled_code:
char or()
{
char result; // al

rega = regc | regb;
c_flag = 0;
z_flag = __PAIR16__(regb, regc) == 0;
result = (unsigned __int8)(regc | regb) >> 7;
n_flag = result;
return result;
}

canonical_decompiled_code:
char func17()
{
char result; // al

rega = regc | regb;
c_flag = 0;
z_flag = __PAIR16__(regb, regc) == 0;
result = (unsigned __int8)(regc | regb) >> 7;
n_flag = result;
return result;
}

original_code:
static void or() {
   rega = regb | regc;
   c_flag = 0;
   z_flag = rega == 0;
   n_flag = rega >> 7;
}


shl

decompiled_code:
char shl()
{
char result; // al

rega = 2 * regb;
c_flag = (unsigned __int8)regb >> 7;
z_flag = 2 * regb == 0;
result = (unsigned __int8)(2 * regb) >> 7;
n_flag = result;
return result;
}

canonical_decompiled_code:
char func18()
{
char result; // al

rega = 2 * regb;
c_flag = (unsigned __int8)regb >> 7;
z_flag = 2 * regb == 0;
result = (unsigned __int8)(2 * regb) >> 7;
n_flag = result;
return result;
}

original_code:
static void shl() {
   rega = regb << 1;
   c_flag = regb >> 7;
   z_flag = rega == 0;
   n_flag = rega >> 7;
}


shr

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

rega = (unsigned __int8)regb >> 1;
result = regb & 1;
c_flag = regb & 1;
z_flag = (unsigned __int8)regb >> 1 == 0;
n_flag = 0;
return result;
}

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

rega = (unsigned __int8)regb >> 1;
result = regb & 1;
c_flag = regb & 1;
z_flag = (unsigned __int8)regb >> 1 == 0;
n_flag = 0;
return result;
}

original_code:
static void shr() {
   rega = regb >> 1;
   c_flag = regb & 1;
   z_flag = rega == 0;
   n_flag = rega >> 7;
}


add

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

rega = regb + regc;
result = (unsigned __int8)regc + (unsigned int)(unsigned __int8)regb;
c_flag = (unsigned __int16)((unsigned __int8)regc + (unsigned __int8)regb) > 0xFFu;
z_flag = regb + regc == 0;
n_flag = (unsigned __int8)(regb + regc) >> 7;
return result;
}

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

rega = regb + regc;
result = (unsigned __int8)regc + (unsigned int)(unsigned __int8)regb;
c_flag = (unsigned __int16)((unsigned __int8)regc + (unsigned __int8)regb) > 0xFFu;
z_flag = regb + regc == 0;
n_flag = (unsigned __int8)(regb + regc) >> 7;
return result;
}

original_code:
static void add() {
   unsigned short temp = regb + regc;
   rega = (unsigned char)temp;
   c_flag = temp > 255;
   z_flag = rega == 0;
   n_flag = rega >> 7;
}

variable_types:
temp
unsigned short


inc

decompiled_code:
char inc()
{
char result; // al

rega = regb + 1;
c_flag = regb == -1;
z_flag = regb == -1;
result = (unsigned __int8)(regb + 1) >> 7;
n_flag = result;
return result;
}

canonical_decompiled_code:
char func21()
{
char result; // al

rega = regb + 1;
c_flag = regb == -1;
z_flag = regb == -1;
result = (unsigned __int8)(regb + 1) >> 7;
n_flag = result;
return result;
}

original_code:
static void inc() {
   rega = regb + 1;
   c_flag = regb == 255;
   z_flag = rega == 0;
   n_flag = rega >> 7;
}


dec

decompiled_code:
char dec()
{
char result; // al

rega = regb - 1;
c_flag = regb == 0;
z_flag = regb == 1;
result = (unsigned __int8)(regb - 1) >> 7;
n_flag = result;
return result;
}

canonical_decompiled_code:
char func22()
{
char result; // al

rega = regb - 1;
c_flag = regb == 0;
z_flag = regb == 1;
result = (unsigned __int8)(regb - 1) >> 7;
n_flag = result;
return result;
}

original_code:
static void dec() {
   rega = regb - 1;
   c_flag = regb == 0;
   z_flag = rega == 0;
   n_flag = rega >> 7;
}


not

decompiled_code:
char not()
{
char result; // al

rega = ~regb;
c_flag = 0;
z_flag = regb == -1;
result = (unsigned __int8)~regb >> 7;
n_flag = result;
return result;
}

canonical_decompiled_code:
char func23()
{
char result; // al

rega = ~regb;
c_flag = 0;
z_flag = regb == -1;
result = (unsigned __int8)~regb >> 7;
n_flag = result;
return result;
}

original_code:
static void not() {
   rega = ~regb;
   c_flag = 0;
   z_flag = rega == 0;
   n_flag = rega >> 7;
}


clr

canonical_decompiled_code:
void func24()
{
rega = 0;
c_flag = 0;
z_flag = 1;
n_flag = 0;
}

original_code:
static void clr() {
   rega = 0;
   c_flag = 0;
   z_flag = 1;
   n_flag = 0;
}


mab

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

result = (unsigned __int8)rega;
regb = rega;
return result;
}

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

result = (unsigned __int8)rega;
regb = rega;
return result;
}

original_code:
static void mab() {
   regb = rega;
}

canonical_original_code:
static void mab() {
   regb = rega;
}


mac

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

result = (unsigned __int8)rega;
regc = rega;
return result;
}

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

result = (unsigned __int8)rega;
regc = rega;
return result;
}

original_code:
static void mac() {
   regc = rega;
}

canonical_original_code:
static void mac() {
   regc = rega;
}


sax

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

result = (unsigned __int8)regxl | ((unsigned __int8)regxh << 8);
memory[result] = rega;
return result;
}

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

result = (unsigned __int8)regxl | ((unsigned __int8)regxh << 8);
memory[result] = rega;
return result;
}

original_code:
static void sax() {
   memory[(regxh << 8) | regxl] = rega;
}


sbx

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

result = (unsigned __int8)regxl | ((unsigned __int8)regxh << 8);
memory[result] = regb;
return result;
}

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

result = (unsigned __int8)regxl | ((unsigned __int8)regxh << 8);
memory[result] = regb;
return result;
}

original_code:
static void sbx() {
   memory[(regxh << 8) | regxl] = regb;
}


scx

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

result = (unsigned __int8)regxl | ((unsigned __int8)regxh << 8);
memory[result] = regc;
return result;
}

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

result = (unsigned __int8)regxl | ((unsigned __int8)regxh << 8);
memory[result] = regc;
return result;
}

original_code:
static void scx() {
   memory[(regxh << 8) | regxl] = regc;
}


lbx

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

result = (unsigned __int8)memory[(unsigned __int8)regxl | ((unsigned __int8)regxh << 8)];
regb = memory[(unsigned __int8)regxl | ((unsigned __int8)regxh << 8)];
return result;
}

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

result = (unsigned __int8)memory[(unsigned __int8)regxl | ((unsigned __int8)regxh << 8)];
regb = memory[(unsigned __int8)regxl | ((unsigned __int8)regxh << 8)];
return result;
}

original_code:
static void lbx() {
   regb = memory[(regxh << 8) | regxl];
}


lcx

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

result = (unsigned __int8)memory[(unsigned __int8)regxl | ((unsigned __int8)regxh << 8)];
regc = memory[(unsigned __int8)regxl | ((unsigned __int8)regxh << 8)];
return result;
}

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

result = (unsigned __int8)memory[(unsigned __int8)regxl | ((unsigned __int8)regxh << 8)];
regc = memory[(unsigned __int8)regxl | ((unsigned __int8)regxh << 8)];
return result;
}

original_code:
static void lcx() {
   regc = memory[(regxh << 8) | regxl];
}


ret

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

result = ((unsigned __int8)regxh << 8) | (unsigned int)(unsigned __int8)regxl;
preg = ((unsigned __int8)regxh << 8) | (unsigned __int8)regxl;
return result;
}

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

result = ((unsigned __int8)regxh << 8) | (unsigned int)(unsigned __int8)regxl;
preg = ((unsigned __int8)regxh << 8) | (unsigned __int8)regxl;
return result;
}

original_code:
static void ret() {
   preg = (regxh << 8) | regxl;
}


hlt

decompiled_code:
void hlt()
{
halted = 1;
}

canonical_decompiled_code:
void func33()
{
halted = 1;
}

original_code:
static void hlt() {
   halted = 1;
}

canonical_original_code:
static void hlt() {
   halted = 1;
}


decompiled_code:
__int16 __fastcall j_inst(unsigned __int8 a1)
{
__int16 v1; // ax
__int16 v2; // si
unsigned __int16 v3; // cx
__int16 v4; // si
__int16 result; // ax

v1 = preg;
v2 = (unsigned __int8)memory[(unsigned __int16)preg] << 8;
v3 = preg + 2;
preg += 2;
v4 = (unsigned __int8)memory[(unsigned __int16)(v1 + 1)] | v2;
operand = v4;
result = (n_flag | ((a1 >> 2) ^ 1) & 1) & (unsigned __int8)(z_flag | ((a1 >> 1) ^ 1) & 1);
if ( ((unsigned __int8)(c_flag | (a1 ^ 1) & 1) & (unsigned __int8)result) != 0 )
{
if ( (a1 & 8) != 0 )
{
result = HIBYTE(v3);
regxh = HIBYTE(v3);
regxl = v3;
}
preg = v4;
}
return result;
}

canonical_decompiled_code:
__int16 __fastcall func34(unsigned __int8 a1)
{
__int16 v1; // ax
__int16 v2; // si
unsigned __int16 v3; // cx
__int16 v4; // si
__int16 result; // ax

v1 = preg;
v2 = (unsigned __int8)memory[(unsigned __int16)preg] << 8;
v3 = preg + 2;
preg += 2;
v4 = (unsigned __int8)memory[(unsigned __int16)(v1 + 1)] | v2;
operand = v4;
result = (n_flag | ((a1 >> 2) ^ 1) & 1) & (unsigned __int8)(z_flag | ((a1 >> 1) ^ 1) & 1);
if ( ((unsigned __int8)(c_flag | (a1 ^ 1) & 1) & (unsigned __int8)result) != 0 )
{
if ( (a1 & 8) != 0 )
{
result = HIBYTE(v3);
regxh = HIBYTE(v3);
regxl = v3;
}
preg = v4;
}
return result;
}

original_code:
static void j_inst(unsigned char func) {
   const unsigned char c_func = (func >> 0) & 1;
   const unsigned char z_func = (func >> 1) & 1;
   const unsigned char n_func = (func >> 2) & 1;
   const unsigned char is_call = (func >> 3) & 1;
   operand = (unsigned short)memory[preg++] << 8;
   operand |= memory[preg++];
   if((!c_func | c_flag) & (!z_func | z_flag) & (!n_func | n_flag)) {
      if(is_call) {
         regxh = preg >> 8;
         regxl = preg & 0xFF;
      }
      preg = operand;
   }
}

canonical_original_code:
static void j_inst(unsigned char func) {
   unsigned char c_func = (func >> 0) & 1;
   unsigned char z_func = (func >> 1) & 1;
   unsigned char n_func = (func >> 2) & 1;
   unsigned char is_call = (func >> 3) & 1;
   operand = (unsigned short)memory[preg++] << 8;
   operand |= memory[preg++];
   if((!c_func | c_flag) & (!z_func | z_flag) & (!n_func | n_flag)) {
      if(is_call) {
         regxh = preg >> 8;
         regxl = preg & 0xFF;
      }
      preg = operand;
   }
}

variable_types:
func
unsigned char


decompiled_code:
int __fastcall ls_inst(unsigned __int8 a1)
{
__int64 (*v1)(void); // rcx
__int16 v2; // ax
__int16 v3; // dx
int result; // eax

v1 = ls_class[a1];
v2 = preg;
v3 = (unsigned __int8)memory[(unsigned __int16)preg] << 8;
preg += 2;
operand = v3 | (unsigned __int8)memory[(unsigned __int16)(v2 + 1)];
if ( v1 )
result = v1();
else
result = fprintf(stderr, "ERROR: invalid LS instruction: %u\n", a1);
return result;
}

canonical_decompiled_code:
int __fastcall func35(unsigned __int8 a1)
{
__int64 (*v1)(void); // rcx
__int16 v2; // ax
__int16 v3; // dx
int result; // eax

v1 = ls_class[a1];
v2 = preg;
v3 = (unsigned __int8)memory[(unsigned __int16)preg] << 8;
preg += 2;
operand = v3 | (unsigned __int8)memory[(unsigned __int16)(v2 + 1)];
if ( v1 )
result = v1();
else
result = fprintf(stderr, "ERROR: invalid LS instruction: %u\n", a1);
return result;
}

original_code:
static void ls_inst(unsigned char func) {
   void (*inst)() = ls_class[func];
   operand = (unsigned short)memory[preg++] << 8;
   operand |= memory[preg++];
   if(inst) {
      (inst)();
   } else {
      fprintf(stderr, "ERROR: invalid LS instruction: %u\n",
         (unsigned int)func);
   }
}

variable_types:
inst
void (*)()


decompiled_code:
char __fastcall math_inst(unsigned __int8 a1)
{
char (__fastcall *v1)(); // rdx
char result; // al

v1 = (char (__fastcall *)())math_class[a1];
if ( v1 )
result = v1();
else
result = fprintf(stderr, "ERROR: invalid MATH instruction: %u\n", a1);
return result;
}

canonical_decompiled_code:
char __fastcall func36(unsigned __int8 a1)
{
char (__fastcall *v1)(); // rdx
char result; // al

v1 = (char (__fastcall *)())math_class[a1];
if ( v1 )
result = v1();
else
result = fprintf(stderr, "ERROR: invalid MATH instruction: %u\n", a1);
return result;
}

original_code:
static void math_inst(unsigned char func) {
   void (*inst)() = math_class[func];
   if(inst) {
      (inst)();
   } else {
      fprintf(stderr, "ERROR: invalid MATH instruction: %u\n",
         (unsigned int)func);
   }
}


decompiled_code:
int __fastcall misc_inst(unsigned __int8 a1)
{
__int64 (*v1)(void); // rdx
int result; // eax

v1 = misc_class[a1];
if ( v1 )
result = v1();
else
result = fprintf(stderr, "ERROR: invalid MISC instruction: %u\n", a1);
return result;
}

canonical_decompiled_code:
int __fastcall func37(unsigned __int8 a1)
{
__int64 (*v1)(void); // rdx
int result; // eax

v1 = misc_class[a1];
if ( v1 )
result = v1();
else
result = fprintf(stderr, "ERROR: invalid MISC instruction: %u\n", a1);
return result;
}

original_code:
static void misc_inst(unsigned char func) {
   void (*inst)() = misc_class[func];
   if(inst) {
      (inst)();
   } else {
      fprintf(stderr, "ERROR: invalid MISC instruction: %u\n",
         (unsigned int)func);
   }
}


next

decompiled_code:
char next()
{
unsigned __int8 v0; // dl
unsigned __int8 v1; // di
char result; // al

v0 = (unsigned __int8)memory[(unsigned __int16)preg] >> 4;
v1 = memory[(unsigned __int16)preg++] & 0xF;
if ( v0 == 2 )
{
result = math_inst(v1);
clocks += 9;
}
else if ( v0 > 2u )
{
if ( v0 == 3 )
{
result = misc_inst(v1);
clocks += 9;
}
else
{
result = fprintf(stderr, "ERROR: invalid instruction class: %u\n", v0);
}
}
else
{
if ( v0 )
result = ls_inst(v1);
else
result = j_inst(v1);
clocks += 21;
}
return result;
}

canonical_decompiled_code:
char func38()
{
unsigned __int8 v0; // dl
unsigned __int8 v1; // di
char result; // al

v0 = (unsigned __int8)memory[(unsigned __int16)preg] >> 4;
v1 = memory[(unsigned __int16)preg++] & 0xF;
if ( v0 == 2 )
{
result = func36(v1);
clocks += 9;
}
else if ( v0 > 2u )
{
if ( v0 == 3 )
{
result = func37(v1);
clocks += 9;
}
else
{
result = fprintf(stderr, "ERROR: invalid instruction class: %u\n", v0);
}
}
else
{
if ( v0 )
result = func35(v1);
else
result = func34(v1);
clocks += 21;
}
return result;
}

original_code:
static void next() {
   opcode = memory[preg];
   const unsigned char inst_class = opcode >> 4;
   const unsigned char inst_func = opcode & 0x0F;
   ++preg;
   switch(inst_class) {
   case 0:
      j_inst(inst_func);
      clocks += 7 * 3;
      break;
   case 1:
      ls_inst(inst_func);
      clocks += 7 * 3;
      break;
   case 2:
      math_inst(inst_func);
      clocks += 3 * 3;
      break;
   case 3:
      misc_inst(inst_func);
      clocks += 3 * 3;
      break;
   default:
      fprintf(stderr, "ERROR: invalid instruction class: %u\n",
         (unsigned int)inst_class);
      break;
   }
}

canonical_original_code:
static void next() {
   opcode = memory[preg];
   unsigned char inst_class = opcode >> 4;
   unsigned char inst_func = opcode & 0x0F;
   ++preg;
   switch(inst_class) {
   case 0:
      j_inst(inst_func);
      clocks += 7 * 3;
      break;
   case 1:
      ls_inst(inst_func);
      clocks += 7 * 3;
      break;
   case 2:
      math_inst(inst_func);
      clocks += 3 * 3;
      break;
   case 3:
      misc_inst(inst_func);
      clocks += 3 * 3;
      break;
   default:
      fprintf(stderr, "ERROR: invalid instruction class: %u\n",
         (unsigned int)inst_class);
      break;
   }
}


DisplayByte

decompiled_code:
int __fastcall DisplayByte(unsigned __int8 a1)
{
unsigned int v1; // ebx
int v2; // ebp
int result; // eax

v1 = 7;
v2 = a1;
do
{
if ( _bittest(&v2, v1) )
result = putchar(111);
else
result = putchar(45);
--v1;
}
while ( v1 != -1 );
return result;
}

canonical_decompiled_code:
int __fastcall func39(unsigned __int8 a1)
{
unsigned int v1; // ebx
int v2; // ebp
int result; // eax

v1 = 7;
v2 = a1;
do
{
if ( _bittest(&v2, v1) )
result = putchar(111);
else
result = putchar(45);
--v1;
}
while ( v1 != -1 );
return result;
}

original_code:
static void DisplayByte(unsigned char byte) {
   int x;
   for(x = 0; x < 8; x++) {
      if(byte & (1 << (7 - x))) {
         printf("o");
      } else {
         printf("-");
      }
   }
}

variable_types:
byte
unsigned char

variable_types: x int


DisplayWord

decompiled_code:
int __fastcall DisplayWord(__int16 a1)
{
DisplayByte(HIBYTE(a1));
putchar(32);
return DisplayByte(a1);
}

canonical_decompiled_code:
int __fastcall func40(__int16 a1)
{
func39(HIBYTE(a1));
putchar(32);
return func39(a1);
}

original_code:
static void DisplayWord(unsigned short word) {
   DisplayByte((unsigned char)(word >> 8));
   printf(" ");
   DisplayByte((unsigned char)word);
}

variable_types:
word
unsigned short


DisplayState

decompiled_code:
int DisplayState()
{
const char *v0; // rdi
const char *v1; // rdi
const char *v2; // rdi
unsigned int i; // er12
int j; // ebx

printf("\x1B[2J\x1B[;H");
printf("CLOCKS: %u\n", (unsigned int)clocks);
printf("PC: ");
DisplayWord(preg);
printf(" %u\n", (unsigned __int16)preg);
printf("A: ");
DisplayByte(rega);
v0 = " C ";
if ( !c_flag )
v0 = " ";
printf(v0);
v1 = "Z ";
if ( !z_flag )
v1 = " ";
printf(v1);
v2 = "N ";
if ( !n_flag )
v2 = " ";
printf(v2);
printf("%u\n", (unsigned __int8)rega);
printf("B: ");
DisplayByte(regb);
printf(" %u\n", (unsigned __int8)regb);
printf("C: ");
DisplayByte(regc);
printf(" %u\n", (unsigned __int8)regc);
printf("X: ");
DisplayByte(regxh);
putchar(32);
DisplayByte(regxl);
printf(" %u\n", (unsigned __int8)regxl | ((unsigned __int8)regxh << 8));
for ( i = 0; i != 240; i += 16 )
{
printf("%04x:", i);
for ( j = 0; j != 16; ++j )
{
if ( (j & 7) == 0 )
putchar(32);
printf("%02x ", (unsigned __int8)memory[i + j]);
}
putchar(10);
}
return usleep(0x186A0u);
}

canonical_decompiled_code:
int func41()
{
const char *v0; // rdi
const char *v1; // rdi
const char *v2; // rdi
unsigned int i; // er12
int j; // ebx

printf("\x1B[2J\x1B[;H");
printf("CLOCKS: %u\n", (unsigned int)clocks);
printf("PC: ");
func40(preg);
printf(" %u\n", (unsigned __int16)preg);
printf("A: ");
func39(rega);
v0 = " C ";
if ( !c_flag )
v0 = " ";
printf(v0);
v1 = "Z ";
if ( !z_flag )
v1 = " ";
printf(v1);
v2 = "N ";
if ( !n_flag )
v2 = " ";
printf(v2);
printf("%u\n", (unsigned __int8)rega);
printf("B: ");
func39(regb);
printf(" %u\n", (unsigned __int8)regb);
printf("C: ");
func39(regc);
printf(" %u\n", (unsigned __int8)regc);
printf("X: ");
func39(regxh);
putchar(32);
func39(regxl);
printf(" %u\n", (unsigned __int8)regxl | ((unsigned __int8)regxh << 8));
for ( i = 0; i != 240; i += 16 )
{
printf("%04x:", i);
for ( j = 0; j != 16; ++j )
{
if ( (j & 7) == 0 )
putchar(32);
printf("%02x ", (unsigned __int8)memory[i + j]);
}
putchar(10);
}
return usleep(0x186A0u);
}

original_code:
void DisplayState() {
   unsigned int x, y;
   printf("\033[2J\033[;H");
   printf("CLOCKS: %u\n", clocks);
   printf("PC: "); DisplayWord(preg);
   printf("       %u\n", (unsigned int)preg);
   printf("A:           "); DisplayByte(rega);
   printf(c_flag ? " C " : "   ");
   printf(z_flag ? "Z " : "  ");
   printf(n_flag ? "N " : "  ");
   printf("%u\n", (unsigned int)rega);
   printf("B:           "); DisplayByte(regb);
   printf("       %u\n", (unsigned int)regb);
   printf("C:           "); DisplayByte(regc);
   printf("       %u\n", (unsigned int)regc);
   printf("X:  "); DisplayByte(regxh); printf(" ");
                   DisplayByte(regxl);
   printf("       %u\n", ((unsigned int)regxh << 8) | regxl);
   x = 0;
   while(x < (8 * 2) * (24 - 9)) {
      printf("%04x:", x);
      for(y = 0; y < (8 * 2); y++, x++) {
         if((y & 7) == 0) {
            printf(" ");
         }
         printf("%02x ", (unsigned int)memory[x]);
      }
      printf("\n");
   }
   usleep(100000);
}

canonical_original_code:
void DisplayState() {
   unsigned int x; unsigned int y;
   printf("\033[2J\033[;H");
   printf("CLOCKS: %u\n", clocks);
   printf("PC: "); DisplayWord(preg);
   printf("       %u\n", (unsigned int)preg);
   printf("A:           "); DisplayByte(rega);
   printf(c_flag ? " C " : "   ");
   printf(z_flag ? "Z " : "  ");
   printf(n_flag ? "N " : "  ");
   printf("%u\n", (unsigned int)rega);
   printf("B:           "); DisplayByte(regb);
   printf("       %u\n", (unsigned int)regb);
   printf("C:           "); DisplayByte(regc);
   printf("       %u\n", (unsigned int)regc);
   printf("X:  "); DisplayByte(regxh); printf(" ");
                   DisplayByte(regxl);
   printf("       %u\n", ((unsigned int)regxh << 8) | regxl);
   x = 0;
   while(x < (8 * 2) * (24 - 9)) {
      printf("%04x:", x);
      for(y = 0; y < (8 * 2); y++, x++) {
         if((y & 7) == 0) {
            printf(" ");
         }
         printf("%02x ", (unsigned int)memory[x]);
      }
      printf("\n");
   }
   usleep(100000);
}

variable_types:
x
unsigned int

variable_types:
y
unsigned int


ShowUsage

repo: joewing/wmtest

decompiled_code:
int __fastcall ShowUsage(const char *a1)
{
return printf("usage: %s [options]\n", a1);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
return printf("usage: %s [options]\n", a1);
}

original_code:
static void ShowUsage(const char *name)
{
   printf("usage: %s [options]\n", name);
}

canonical_original_code:
static void ShowUsage(char *name)
{
   printf("usage: %s [options]\n", name);
}

variable_types:
name
char *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // r8
unsigned __int64 v4; // r10
unsigned int v5; // edi
char v6; // cf
bool v7; // zf
__int64 v8; // r9
unsigned __int64 v9; // rax
const char *v10; // rdi
__int64 v11; // rcx
int v12; // edx
const char *v13; // rbx
const char *v14; // rdi
Display *v15; // rdx
__int64 v16; // rax

if ( argc <= 1 )
{
v14 = 0LL;
v15 = XOpenDisplay(0LL);
display = v15;
if ( v15 )
goto LABEL_10;
goto LABEL_16;
}
v3 = argv;
v4 = 2LL * ((unsigned int)(argc - 2) >> 1) + 3;
v5 = argc - 1;
v6 = v5 & 1;
v5 >>= 1;
v7 = v5 == 0;
v8 = 2LL * v5 + 1;
v9 = 1LL;
do
{
argv = (const char **)v3[v9];
v10 = "-display";
v11 = 9LL;
do
{
if ( !v11 )
break;
v6 = *(_BYTE *)argv < *v10;
v7 = *(_BYTE *)argv == *v10;
argv = (const char **)((char *)argv + 1);
++v10;
--v11;
}
while ( v7 );
if ( !(v6 | v7) != v6 || (v12 = v9 + 1, v9 == v8) )
{
ShowUsage(*v3);
exit(-1);
}
v9 += 2LL;
v6 = v9 < v4;
v7 = v9 == v4;
}
while ( v9 != v4 );
v13 = v3[v12];
v14 = v13;
v15 = XOpenDisplay(v13);
display = v15;
if ( !v15 )
{
if ( v13 )
{
printf("error: could not open display %s\n", v13);
goto LABEL_14;
}
LABEL_16:
puts("error: could not open display");
LABEL_14:
exit(-1);
}
LABEL_10:
rootScreen = v15->default_screen;
v16 = (__int64)&v15->screens[(__int64)rootScreen];
rootWindow = *(_QWORD *)(v16 + 16);
rootDepth = *(_DWORD *)(v16 + 56);
TestMap(v14, argv);
TestGravity();
TestDesktop();
TestResize();
XCloseDisplay(display);
printf("Errors: %u\n", (unsigned int)errorCount);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // r8
unsigned __int64 v4; // r10
unsigned int v5; // edi
char v6; // cf
bool v7; // zf
__int64 v8; // r9
unsigned __int64 v9; // rax
const char *v10; // rdi
__int64 v11; // rcx
int v12; // edx
const char *v13; // rbx
const char *v14; // rdi
Display *v15; // rdx
__int64 v16; // rax

if ( argc <= 1 )
{
v14 = 0LL;
v15 = XOpenDisplay(0LL);
display = v15;
if ( v15 )
goto LABEL_10;
goto LABEL_16;
}
v3 = argv;
v4 = 2LL * ((unsigned int)(argc - 2) >> 1) + 3;
v5 = argc - 1;
v6 = v5 & 1;
v5 >>= 1;
v7 = v5 == 0;
v8 = 2LL * v5 + 1;
v9 = 1LL;
do
{
argv = (const char **)v3[v9];
v10 = "-display";
v11 = 9LL;
do
{
if ( !v11 )
break;
v6 = *(_BYTE *)argv < *v10;
v7 = *(_BYTE *)argv == *v10;
argv = (const char **)((char *)argv + 1);
++v10;
--v11;
}
while ( v7 );
if ( !(v6 | v7) != v6 || (v12 = v9 + 1, v9 == v8) )
{
func0(*v3);
exit(-1);
}
v9 += 2LL;
v6 = v9 < v4;
v7 = v9 == v4;
}
while ( v9 != v4 );
v13 = v3[v12];
v14 = v13;
v15 = XOpenDisplay(v13);
display = v15;
if ( !v15 )
{
if ( v13 )
{
printf("error: could not open display %s\n", v13);
goto LABEL_14;
}
LABEL_16:
puts("error: could not open display");
LABEL_14:
exit(-1);
}
LABEL_10:
rootScreen = v15->default_screen;
v16 = (__int64)&v15->screens[(__int64)rootScreen];
rootWindow = *(_QWORD *)(v16 + 16);
rootDepth = *(_DWORD *)(v16 + 56);
TestMap(v14, argv);
TestGravity();
TestDesktop();
TestResize();
XCloseDisplay(display);
printf("Errors: %u\n", (unsigned int)errorCount);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
   const char *displayString = ((void *)0);
   int i;
   for(i = 1; i < argc; i++) {
      if(!strcmp(argv[i], "-display") && i + 1 < argc) {
         i += 1;
         displayString = argv[i];
      } else {
         ShowUsage(argv[0]);
         exit(-1);
      }
   }
   display = XOpenDisplay(displayString);
   if(!display) {
      if(displayString) {
         printf("error: could not open display %s\n", displayString);
      } else {
         printf("error: could not open display\n");
      }
      exit(-1);
   }
   rootScreen = (((_XPrivDisplay)(display))->default_screen);
   rootWindow = ((&((_XPrivDisplay)(display))->screens[rootScreen])->root);
   rootDepth = ((&((_XPrivDisplay)(display))->screens[rootScreen])->root_depth);
   TestMap();
   TestGravity();
   TestDesktop();
   TestResize();
   XCloseDisplay(display);
   printf("Errors: %u\n", errorCount);
   return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
   char *displayString = ((void *)0);
   int i;
   for(i = 1; i < argc; i++) {
      if(!strcmp(argv[i], "-display") && i + 1 < argc) {
         i += 1;
         displayString = argv[i];
      } else {
         ShowUsage(argv[0]);
         exit(-1);
      }
   }
   display = XOpenDisplay(displayString);
   if(!display) {
      if(displayString) {
         printf("error: could not open display %s\n", displayString);
      } else {
         printf("error: could not open display\n");
      }
      exit(-1);
   }
   rootScreen = (((_XPrivDisplay)(display))->default_screen);
   rootWindow = ((&((_XPrivDisplay)(display))->screens[rootScreen])->root);
   rootDepth = ((&((_XPrivDisplay)(display))->screens[rootScreen])->root_depth);
   TestMap();
   TestGravity();
   TestDesktop();
   TestResize();
   XCloseDisplay(display);
   printf("Errors: %u\n", errorCount);
   return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
displayString
char *

variable_types: i int

return_type: int


decompiled_code:
int TestResize2()
{
Window v0; // rax
XSizeHints v2; // [rsp+0h] [rbp-58h] BYREF

v2.flags = 0LL;
v0 = CreateWindow(&v2);
return XDestroyWindow(display, v0);
}

canonical_decompiled_code:
int func2()
{
Window v0; // rax
XSizeHints v2; // [rsp+0h] [rbp-58h] BYREF

v2.flags = 0LL;
v0 = func0(&v2);
return XDestroyWindow(display, v0);
}

original_code:
static void TestResize2()
{
   Window w;
   XSizeHints hints;
   hints.flags = 0;
   w = CreateWindow(&hints);
   XDestroyWindow(display, w);
}

canonical_original_code:
static void TestResize2()
{
   unsigned long w;
   struct XSizeHints hints;
   hints.flags = 0;
   w = CreateWindow(&hints);
   XDestroyWindow(display, w);
}

variable_types:
w
unsigned long

variable_types: struct XSizeHints { long flags; int x; int width; int min_width; int max_width; int width_inc; struct { int x; int y; } min_aspect; int base_width; int win_gravity; }


TestResize

decompiled_code:
int TestResize()
{
TestResize1();
return TestResize2();
}

canonical_decompiled_code:
int func3()
{
func1();
return func2();
}

original_code:
void TestResize()
{
   TestResize1();
   TestResize2();
   TestResize3();
   TestResize4();
}


TestGravity

decompiled_code:
int TestGravity()
{
DoTestGravity(2);
DoTestGravity(8);
DoTestGravity(6);
DoTestGravity(4);
DoTestGravity(3);
DoTestGravity(1);
DoTestGravity(9);
DoTestGravity(7);
DoTestGravity(5);
return DoTestGravity(10);
}

original_code:
void TestGravity()
{
   DoTestGravity(2);
   DoTestGravity(8);
   DoTestGravity(6);
   DoTestGravity(4);
   DoTestGravity(3);
   DoTestGravity(1);
   DoTestGravity(9);
   DoTestGravity(7);
   DoTestGravity(5);
   DoTestGravity(10);
}


decompiled_code:
void __fastcall Tokenize_String_Delete(void *ptr)
{
__int64 v1; // rbx

if ( *(int *)ptr > 0 )
{
v1 = 0LL;
do
free(*(void **)(*((_QWORD *)ptr + 1) + 8 * v1++));
while ( *(_DWORD *)ptr > (int)v1 );
}
free(*((void **)ptr + 1));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
__int64 v1; // rbx

if ( *(int *)ptr > 0 )
{
v1 = 0LL;
do
free(*(void **)(*((_QWORD *)ptr + 1) + 8 * v1++));
while ( *(_DWORD *)ptr > (int)v1 );
}
free(*((void **)ptr + 1));
free(ptr);
}

original_code:
void Tokenize_String_Delete(struct tokenized_string *ts)
{
 int i;
 for (i=0; i<ts->count; i++)
 {
  free(ts->tokens[i]);
 }
 free(ts->tokens);
 free(ts);
}

variable_types: struct tokenized_string { int count; char **tokens; } *


decompiled_code:
char *__fastcall Get_Userinfo_Value_By_Key(const char *a1, const char *a2)
{
char *v2; // rbx
const char *v3; // rdi
const char *v4; // rax
int v5; // esi
char v7[136]; // [rsp+0h] [rbp-88h] BYREF

sprintf(v7, "\\%s\\", a1);
v2 = strstr(a2, v7);
if ( v2 )
{
v3 = &v2[strlen(v7)];
if ( *v3 == 92 || !*v3 )
{
v5 = 0;
}
else
{
v4 = v3;
do
v5 = 1 - (_DWORD)v3 + (_DWORD)v4++;
while ( *v4 != 92 && *v4 );
}
v2 = strndup(v3, v5);
}
return v2;
}

canonical_decompiled_code:
char *__fastcall func0(const char *a1, const char *a2)
{
char *v2; // rbx
const char *v3; // rdi
const char *v4; // rax
int v5; // esi
char v7[136]; // [rsp+0h] [rbp-88h] BYREF

sprintf(v7, "\\%s\\", a1);
v2 = strstr(a2, v7);
if ( v2 )
{
v3 = &v2[strlen(v7)];
if ( *v3 == 92 || !*v3 )
{
v5 = 0;
}
else
{
v4 = v3;
do
v5 = 1 - (_DWORD)v3 + (_DWORD)v4++;
while ( *v4 != 92 && *v4 );
}
v2 = strndup(v3, v5);
}
return v2;
}

original_code:
char *Get_Userinfo_Value_By_Key(char *key, char *userinfo)
{
 char buf[128];
 char *index = ((void *)0);
 char *index_org = ((void *)0);
 int _size = 0;
 char *r;
 sprintf(buf, "\\%s\\", key);
 index = strstr(userinfo, buf);
 if (index == ((void *)0))
  return ((void *)0);
 index += strlen(buf);
 index_org = index;
 while (*index != '\\' && *index != 0)
 {
  _size++;
  index++;
 }
 return strndup(index_org, _size);
}

variable_types:
key
char *

variable_types:
userinfo
char *

variable_types:
buf
char [128]

variable_types:
index
char *

variable_types:
r
char *

return_type: char *


VectorDistance

decompiled_code:
float __fastcall VectorDistance(float *a1, float *a2)
{
double v2; // xmm1_8
double v3; // xmm0_8
float result; // xmm0_4

v2 = (float)(a1[1] - a2[1]);
v3 = (float)(*a1 - *a2) * (float)(*a1 - *a2) + v2 * v2 + (float)(a1[2] - a2[2]) * (float)(a1[2] - a2[2]);
if ( v3 < 0.0 )
result = sqrt(v3);
else
result = sqrt(v3);
return result;
}

canonical_decompiled_code:
float __fastcall func1(float *a1, float *a2)
{
double v2; // xmm1_8
double v3; // xmm0_8
float result; // xmm0_4

v2 = (float)(a1[1] - a2[1]);
v3 = (float)(*a1 - *a2) * (float)(*a1 - *a2) + v2 * v2 + (float)(a1[2] - a2[2]) * (float)(a1[2] - a2[2]);
if ( v3 < 0.0 )
result = sqrt(v3);
else
result = sqrt(v3);
return result;
}

variable_types:
a
float [3]

variable_types:
b
float [3]

variable_types:
c
float [3]

return_type: float


decompiled_code:
_BOOL8 __fastcall is_player(__int64 a1)
{
_BYTE *v1; // rax
_BOOL4 v2; // edx

v1 = *(_BYTE **)(a1 + 8);
if ( !v1 )
return 0;
v2 = 0;
if ( *v1 )
v2 = *(_DWORD *)(a1 + 72) != 1;
return v2;
}

original_code:
int is_player(struct player *p)
{
 if (p->name == ((void *)0))
  return 0;
 if (p->name[0] == '\0')
  return 0;
 if (p->spectator == 1)
  return 0;
 return 1;
}

variable_types: struct player { int userid; char *name; char *name_readable; char *team; char *team_readable; float entertime; int ping; int pl; int frags; int flags; char *userinfo; int spectator; unsigned char topcolor; unsigned char bottomcolor; int stats[32]; float angles[3]; float origin[3]; struct stats { int sg_attacks; int ssg_attacks; int ng_attacks; int sng_attacks; int gl_attacks; int rl_attacks; int lg_attacks; int jumps; int ga; int mh; int quad; int ssg; int deaths; } *statistics; } *

user_defined_types: struct player { int userid; char *name; char *name_readable; char *team; char *team_readable; float entertime; int ping; int pl; int frags; int flags; char *userinfo; int spectator; unsigned char topcolor; unsigned char bottomcolor; int stats[32]; float angles[3]; float origin[3]; struct stats *statistics; }


decompiled_code:
int drop_root()
{
setgid(0x62u);
return setuid(0x63u);
}

canonical_decompiled_code:
int func0()
{
setgid(0x62u);
return setuid(0x63u);
}

original_code:
void drop_root(){
   setgid(98);
   setuid(99);
}


tvdiff

decompiled_code:
__int64 __fastcall tvdiff(int a1, int a2, int a3, int a4)
{
return (unsigned int)(int)(double)(a2 + 1000000 * a1 - (a4 + 1000000 * a3));
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2, int a3, int a4)
{
return (unsigned int)(int)(double)(a2 + 1000000 * a1 - (a4 + 1000000 * a3));
}

original_code:
long tvdiff(struct timeval a, struct timeval b){
   double res;
   res = (a.tv_sec * 1000000 + a.tv_usec) - (b.tv_sec * 1000000 + b.tv_usec);
   return (long) res;
}

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

variable_types:
b
struct timeval

variable_types: res double

return_type: long


decompiled_code:
__int64 __fastcall resolve_host(char *name)
{
in_addr_t v1; // eax
__int64 v2; // rdx
struct hostent *v4; // rax

v1 = inet_addr(name);
v2 = v1;
if ( v1 == -1 )
{
v4 = gethostbyname(name);
v2 = 0LL;
if ( v4 )
v2 = **(_QWORD **)v4->h_addr_list;
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(char *name)
{
in_addr_t v1; // eax
__int64 v2; // rdx
struct hostent *v4; // rax

v1 = inet_addr(name);
v2 = v1;
if ( v1 == -1 )
{
v4 = gethostbyname(name);
v2 = 0LL;
if ( v4 )
v2 = **(_QWORD **)v4->h_addr_list;
}
return v2;
}

original_code:
unsigned long resolve_host(char *h){
   struct hostent *host;
   struct in_addr addr;
   if((addr.s_addr = inet_addr(h)) == -1){
       if((host = gethostbyname(h)) == ((void *)0)){
          return 0;
       }
       else return *(unsigned long*)host->h_addr_list[0];
   }
   else return addr.s_addr;
}

canonical_original_code:
unsigned long resolve_host(char *h){
   struct hostent *host;
   struct in_addr addr;
   if((addr.s_addr = inet_addr(h)) == -1){
       if((host = gethostbyname(h)) == ((void *)0)){
          return 0;
       }
       else return *(unsigned long *)host->h_addr_list[0];
   }
   else return addr.s_addr;
}

variable_types:
h
char *

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

variable_types:
addr
struct in_addr { unsigned int s_addr; }

return_type: unsigned long


readudppacket

decompiled_code:
ssize_t __fastcall readudppacket(int fd, void *buf, int a3, int a4, __int64 a5)
{
int v6; // edx
ssize_t result; // rax
struct sockaddr addr; // [rsp+8h] [rbp-C0h] BYREF
socklen_t addr_len; // [rsp+1Ch] [rbp-ACh] BYREF
fd_set readfds; // [rsp+20h] [rbp-A8h] BYREF
struct timeval timeout; // [rsp+A0h] [rbp-28h] BYREF

*(_QWORD *)&addr.sa_family = a5;
addr_len = 16;
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[fd / 64] |= 1LL << (fd % 64);
timeout.tv_sec = a4;
timeout.tv_usec = 0LL;
v6 = select(fd + 1, &readfds, 0LL, 0LL, &timeout);
result = 0LL;
if ( v6 > 0 )
result = recvfrom(fd, buf, a3, 0, &addr, &addr_len);
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func4(int fd, void *buf, int a3, int a4, __int64 a5)
{
int v6; // edx
ssize_t result; // rax
struct sockaddr addr; // [rsp+8h] [rbp-C0h] BYREF
socklen_t addr_len; // [rsp+1Ch] [rbp-ACh] BYREF
fd_set readfds; // [rsp+20h] [rbp-A8h] BYREF
struct timeval timeout; // [rsp+A0h] [rbp-28h] BYREF

*(_QWORD *)&addr.sa_family = a5;
addr_len = 16;
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[fd / 64] |= 1LL << (fd % 64);
timeout.tv_sec = a4;
timeout.tv_usec = 0LL;
v6 = select(fd + 1, &readfds, 0LL, 0LL, &timeout);
result = 0LL;
if ( v6 > 0 )
result = recvfrom(fd, buf, a3, 0, &addr, &addr_len);
return result;
}

original_code:
int readudppacket(int sd, char *buf, int size, int timeout, struct sockaddr *client_addr){
   struct timeval tv;
   fd_set rfds;
   int retval, n, size_of;
   size_of = sizeof(struct sockaddr);
   do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->__fds_bits)[0]) : "memory"); } while (0);
   ((void) (((&rfds)->__fds_bits)[((sd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sd) % (8 * (int) sizeof (__fd_mask)))))));
   tv.tv_sec = timeout;
   tv.tv_usec = 0;
   retval = select(sd+1, &rfds, ((void *)0), ((void *)0), &tv);
   if(retval <= 0)
      return 0;
   n = recvfrom(sd, buf, size, 0, (struct sockaddr *)&client_addr, &size_of);
   return n;
}

canonical_original_code:
int readudppacket(int sd, char *buf, int size, int timeout, struct sockaddr *client_addr){
   struct timeval tv;
   struct fd_set rfds;
   int retval; int n; int size_of;
   size_of = sizeof(struct sockaddr);
   do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&rfds)->__fds_bits)[0]) : "memory"); } while (0);
   ((void) (((&rfds)->__fds_bits)[((sd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((sd) % (8 * (int) sizeof (__fd_mask)))))));
   tv.tv_sec = timeout;
   tv.tv_usec = 0;
   retval = select(sd+1, &rfds, ((void *)0), ((void *)0), &tv);
   if(retval <= 0)
      return 0;
   n = recvfrom(sd, buf, size, 0, (struct sockaddr *)&client_addr, &size_of);
   return n;
}

variable_types: sd int

variable_types:
buf
char *

variable_types: size int

variable_types: timeout int

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

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

variable_types: retval int

variable_types: n int

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


routerConnect

repo: john-kolt/efim

decompiled_code:
__int64 routerConnect()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func7()
{
return 0LL;
}

original_code:
int routerConnect() {
  return(0);
}

canonical_original_code:
int routerConnect() {
  return(0);
}


routerDisconnect

decompiled_code:
__int64 routerDisconnect()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func8()
{
return 0LL;
}

original_code:
int routerDisconnect() {
  return(0);
}

canonical_original_code:
int routerDisconnect() {
  return(0);
}


routerLoadConfig

decompiled_code:
__int64 routerLoadConfig()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func9()
{
return 0LL;
}

original_code:
int routerLoadConfig(char *fileName) {
  return(0);
}

variable_types:
fileName
char *


routerSend

decompiled_code:
__int64 routerSend()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func10()
{
return 0LL;
}

original_code:
int routerSend(tUID toUid, tData data) {
  return(0);
}

canonical_original_code:
int routerSend(char *toUid, char *data) {
  return(0);
}

variable_types:
toUid
char *

variable_types:
data
char *


routerOnReceive

decompiled_code:
__int64 routerOnReceive()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func11()
{
return 0LL;
}

original_code:
void *routerOnReceive(tUID fromUid, tData data) {
  return(0);
}

canonical_original_code:
void *routerOnReceive(char *fromUid, char *data) {
  return(0);
}

variable_types:
fromUid
char *

return_type: void *


listCreate

decompiled_code:
_DWORD *listCreate()
{
_DWORD *result; // rax

result = malloc(0x20uLL);
if ( result )
{
*result = 0;
result[1] = 0;
result[2] = 0;
*((_QWORD *)result + 3) = 0LL;
*((_QWORD *)result + 2) = 0LL;
}
return result;
}

canonical_decompiled_code:
_DWORD *func12()
{
_DWORD *result; // rax

result = malloc(0x20uLL);
if ( result )
{
*result = 0;
result[1] = 0;
result[2] = 0;
*((_QWORD *)result + 3) = 0LL;
*((_QWORD *)result + 2) = 0LL;
}
return result;
}

original_code:
pList listCreate()
{
    pList result = malloc(sizeof(tList));
    if(result != 0)
    {
        result->uid = 0;
        result->data = 0;
        result->level = 0;
        result->left = result->right = 0;
    }
    return result;
}

canonical_original_code:
struct tList *listCreate()
{
    struct tList *result = malloc(sizeof(tList));
    if(result != 0)
    {
        result->uid = 0;
        result->data = 0;
        result->level = 0;
        result->left = result->right = 0;
    }
    return result;
}

variable_types: struct tList { int uid; int data; int level; struct tList *left; struct tList *right; } *


listSwap

decompiled_code:
void listSwap()
{
;
}

canonical_decompiled_code:
void func13()
{
;
}

original_code:
void listSwap(pList from, pList to)
{
    pList tmp = to;
    to = from;
    from = tmp;
}

canonical_original_code:
void listSwap(struct tList *from, struct tList *to)
{
    struct tList *tmp = to;
    to = from;
    from = tmp;
}

variable_types:
to
struct tList *

user_defined_types: struct tList { int uid; int data; int level; struct tList *left; struct tList *right; }


listSkew

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

result = *(_QWORD *)(a1 + 16);
if ( *(_DWORD *)(a1 + 8) == *(_DWORD *)(result + 8) )
{
*(_QWORD *)(result + 16) = *(_QWORD *)(a1 + 24);
*(_QWORD *)(a1 + 24) = result;
}
return result;
}

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

result = *(_QWORD *)(a1 + 16);
if ( *(_DWORD *)(a1 + 8) == *(_DWORD *)(result + 8) )
{
*(_QWORD *)(result + 16) = *(_QWORD *)(a1 + 24);
*(_QWORD *)(a1 + 24) = result;
}
return result;
}

original_code:
void listSkew(pList list)
{
   pList l = list->left;
   if(list->level == l->level)
   {
       listSwap(list, l);
       l->left = list->right;
       list->right = l;
   }
}

canonical_original_code:
void listSkew(struct tList *list)
{
   struct tList *l = list->left;
   if(list->level == l->level)
   {
       listSwap(list, l);
       l->left = list->right;
       list->right = l;
   }
}


listSplit

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

result = *(_QWORD *)(a1 + 24);
if ( *(_DWORD *)(a1 + 8) == *(_DWORD *)(*(_QWORD *)(result + 24) + 8LL) )
{
*(_QWORD *)(result + 24) = *(_QWORD *)(a1 + 16);
*(_QWORD *)(a1 + 16) = result;
++*(_DWORD *)(a1 + 8);
}
return result;
}

original_code:
void listSplit(pList list)
{
    pList r = list->right;
    if(list->level == r->right->level)
    {
        listSwap(list, r);
        r->right = list->left;
        list->left = r;
        list->level += 1;
    }
}

canonical_original_code:
void listSplit(struct tList *list)
{
    struct tList *r = list->right;
    if(list->level == r->right->level)
    {
        listSwap(list, r);
        r->right = list->left;
        list->left = r;
        list->level += 1;
    }
}


listAdd

decompiled_code:
__int64 __fastcall listAdd(__int64 a1, unsigned int a2, unsigned int a3)
{
unsigned int v3; // ebp
_DWORD *v5; // rax
_DWORD *v7; // rax

v3 = a2;
if ( *(_DWORD *)a1 <= (signed int)a2 )
{
if ( *(_DWORD *)a1 >= (signed int)a2 )
{
if ( !*(_DWORD *)(a1 + 8) )
return v3;
v3 = 0;
goto LABEL_5;
}
if ( !*(_QWORD *)(a1 + 24) )
{
v7 = listCreate();
*v7 = a2;
v7[1] = a3;
*(_QWORD *)(a1 + 24) = v7;
++*(_DWORD *)(a1 + 8);
return 1;
}
if ( *(_DWORD *)(a1 + 8) )
{
v3 = listAdd(*(_QWORD *)(a1 + 24), a2, a3);
goto LABEL_5;
}
}
else
{
if ( !*(_QWORD *)(a1 + 16) )
{
v5 = listCreate();
*v5 = a2;
v5[1] = a3;
*(_QWORD *)(a1 + 16) = v5;
++*(_DWORD *)(a1 + 8);
return 1;
}
if ( *(_DWORD *)(a1 + 8) )
{
v3 = listAdd(*(_QWORD *)(a1 + 16), a2, a3);
LABEL_5:
listSkew(a1);
listSplit(a1);
return v3;
}
}
return v3;
}

original_code:
int listAdd(pList list, int uid, tListData data)
{
    if(uid < list->uid && list->left == 0)
    {
        pList newItem = listCreate();
        newItem->uid = uid;
        newItem->data = data;
        list->left = newItem;
        list->level += 1;
        return 1;
    }
    if(uid > list->uid && list->right == 0)
    {
        pList newItem = listCreate();
        newItem->uid = uid;
        newItem->data = data;
        list->right = newItem;
        list->level += 1;
        return 1;
    }
    if(list->level!=0)
    {
        int result = 0;
        if(uid < list->uid)
            result = listAdd(list->left, uid, data);
        else if(uid > list->uid)
            result = listAdd(list->right, uid, data);
        else if(uid == list->uid)
            result = 0;
        listSkew(list);
        listSplit(list);
        return result;
    }
}

canonical_original_code:
int listAdd(struct tList *list, int uid, int data)
{
    if(uid < list->uid && list->left == 0)
    {
        struct tList *newItem = listCreate();
        newItem->uid = uid;
        newItem->data = data;
        list->left = newItem;
        list->level += 1;
        return 1;
    }
    if(uid > list->uid && list->right == 0)
    {
        struct tList *newItem = listCreate();
        newItem->uid = uid;
        newItem->data = data;
        list->right = newItem;
        list->level += 1;
        return 1;
    }
    if(list->level!=0)
    {
        int result = 0;
        if(uid < list->uid)
            result = listAdd(list->left, uid, data);
        else if(uid > list->uid)
            result = listAdd(list->right, uid, data);
        else if(uid == list->uid)
            result = 0;
        listSkew(list);
        listSplit(list);
        return result;
    }
}

variable_types: uid int

variable_types: data int

variable_types: result int


listDel

original_code:
void listDel(pList list, int uid)
{
    pList deleted = 0;
    pList last = 0;
    if(list->level!=0)
    {
        last = list;
        if(uid < list->uid)
            listDel(list->left, uid);
        else
        {
            deleted = list;
            listDel(list->right, uid);
        }
    }
    if(list == last && deleted != 0 && uid == deleted->uid)
    {
        deleted->uid = list->uid;
        deleted = 0;
        list = list->right;
        free(last);
    }
    else if (list->left->level < list->level-1 || list->right->level < list->level-1)
    {
        list->level -= 1;
        if(list->right->level > list->level)
            list->right->level = list->level;
        listSkew(list);
        listSkew(list->right);
        listSkew(list->right->right);
        listSplit(list);
        listSplit(list->right);
    }
}

canonical_original_code:
void listDel(struct tList *list, int uid)
{
    struct tList *deleted = 0;
    struct tList *last = 0;
    if(list->level!=0)
    {
        last = list;
        if(uid < list->uid)
            listDel(list->left, uid);
        else
        {
            deleted = list;
            listDel(list->right, uid);
        }
    }
    if(list == last && deleted != 0 && uid == deleted->uid)
    {
        deleted->uid = list->uid;
        deleted = 0;
        list = list->right;
        free(last);
    }
    else if (list->left->level < list->level-1 || list->right->level < list->level-1)
    {
        list->level -= 1;
        if(list->right->level > list->level)
            list->right->level = list->level;
        listSkew(list);
        listSkew(list->right);
        listSkew(list->right->right);
        listSplit(list);
        listSplit(list->right);
    }
}


listSearch

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

result = 0LL;
if ( *(_DWORD *)(a1 + 8) )
{
if ( *(_DWORD *)a1 > a2 )
{
result = listSearch(*(_QWORD *)(a1 + 16));
}
else if ( *(_DWORD *)a1 < a2 )
{
result = listSearch(*(_QWORD *)(a1 + 24));
}
else
{
result = 0LL;
if ( *(_DWORD *)a1 == a2 )
result = a1;
}
}
return result;
}

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

result = 0LL;
if ( *(_DWORD *)(a1 + 8) )
{
if ( *(_DWORD *)a1 > a2 )
{
result = func18(*(_QWORD *)(a1 + 16));
}
else if ( *(_DWORD *)a1 < a2 )
{
result = func18(*(_QWORD *)(a1 + 24));
}
else
{
result = 0LL;
if ( *(_DWORD *)a1 == a2 )
result = a1;
}
}
return result;
}

original_code:
pList listSearch(pList list, int uid)
{
    if(list->level==0)
        return 0;
    else
    {
        pList result = 0;
        if(uid < list->uid)
            result = listSearch(list->left, uid);
        else if(uid > list->uid)
            result = listSearch(list->right, uid);
        else if(uid == list->uid)
            result = list;
        return result;
    }
}

canonical_original_code:
struct tList *listSearch(struct tList *list, int uid)
{
    if(list->level==0)
        return 0;
    else
    {
        struct tList *result = 0;
        if(uid < list->uid)
            result = listSearch(list->left, uid);
        else if(uid > list->uid)
            result = listSearch(list->right, uid);
        else if(uid == list->uid)
            result = list;
        return result;
    }
}

variable_types:
list
struct tList *

return_type: struct tList { int uid; int data; int level; struct tList *left; struct tList *right; } *


listPrint

decompiled_code:
int __fastcall listPrint(int *a1)
{
fprintf((FILE *)&unk_402004, (const char *)*a1);
if ( *((_QWORD *)a1 + 2) )
listPrint();
if ( *((_QWORD *)a1 + 3) )
listPrint();
return puts(&s);
}

canonical_decompiled_code:
int __fastcall func19(int *a1)
{
fprintf((FILE *)&unk_402004, (const char *)*a1);
if ( *((_QWORD *)a1 + 2) )
func19();
if ( *((_QWORD *)a1 + 3) )
func19();
return puts(&s);
}

original_code:
void listPrint(pList list)
{
    fprintf("%d ", list->uid);
    if(list->left!=0)
        listPrint(list->left);
    if(list->right!=0)
        listPrint(list->right);
    puts("");
}

canonical_original_code:
void listPrint(struct tList *list)
{
    fprintf("%d ", list->uid);
    if(list->left!=0)
        listPrint(list->left);
    if(list->right!=0)
        listPrint(list->right);
    puts("");
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int *v3; // rbp
int v4; // ebx
int v5; // eax

puts("Hello World!");
v3 = listCreate();
*v3 = rand() % 256;
v4 = 10;
do
{
v5 = rand();
listAdd((__int64)v3, v5 % 256, 0);
--v4;
}
while ( v4 );
listPrint(v3);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int *v3; // rbp
int v4; // ebx
int v5; // eax

puts("Hello World!");
v3 = func12();
*v3 = rand() % 256;
v4 = 10;
do
{
v5 = rand();
func16((__int64)v3, v5 % 256, 0);
--v4;
}
while ( v4 );
func19(v3);
return 0;
}

original_code:
int main(void)
{
    printf("Hello World!\n");
    pList list = listCreate();
    list->uid = rand()%256;
    for(int i=0; i<10; i++)
        listAdd(list, rand()%256, 0);
    listPrint(list);
    return 0;
}

canonical_original_code:
int main()
{
    printf("Hello World!\n");
    struct tList *list = listCreate();
    list->uid = rand()%256;
    for(int i = 0; i<10; i++)
        listAdd(list, rand()%256, 0);
    listPrint(list);
    return 0;
}

variable_types: i int


decompiled_code:
__int64 init_maestro()
{
unsigned int v0; // ebx
int v2; // [rsp+0h] [rbp-58h] BYREF
int v3; // [rsp+4h] [rbp-54h] BYREF
int v4; // [rsp+8h] [rbp-50h] BYREF
int buf; // [rsp+Ch] [rbp-4Ch] BYREF
struct termios termios_p; // [rsp+10h] [rbp-48h] BYREF

v0 = open("/dev/ttyACM0", 258);
tcgetattr(v0, &termios_p);
termios_p.c_lflag &= 0xFFFF7FB4;
termios_p.c_oflag &= 0xFFFFFFF3;
tcsetattr(v0, 0, &termios_p);
buf = 135;
if ( write(v0, &buf, 4uLL) == -1 )
{
perror("error writing");
v0 = -1;
}
else
{
v4 = 391;
if ( write(v0, &v4, 4uLL) == -1 )
{
perror("error writing");
v0 = -1;
}
else
{
v3 = 137;
if ( write(v0, &v3, 4uLL) == -1 )
{
perror("error writing");
v0 = -1;
}
else
{
v2 = 393;
if ( write(v0, &v2, 4uLL) == -1 )
{
perror("error writing");
v0 = -1;
}
}
}
}
return v0;
}

canonical_decompiled_code:
__int64 func0()
{
unsigned int v0; // ebx
int v2; // [rsp+0h] [rbp-58h] BYREF
int v3; // [rsp+4h] [rbp-54h] BYREF
int v4; // [rsp+8h] [rbp-50h] BYREF
int buf; // [rsp+Ch] [rbp-4Ch] BYREF
struct termios termios_p; // [rsp+10h] [rbp-48h] BYREF

v0 = open("/dev/ttyACM0", 258);
tcgetattr(v0, &termios_p);
termios_p.c_lflag &= 0xFFFF7FB4;
termios_p.c_oflag &= 0xFFFFFFF3;
tcsetattr(v0, 0, &termios_p);
buf = 135;
if ( write(v0, &buf, 4uLL) == -1 )
{
perror("error writing");
v0 = -1;
}
else
{
v4 = 391;
if ( write(v0, &v4, 4uLL) == -1 )
{
perror("error writing");
v0 = -1;
}
else
{
v3 = 137;
if ( write(v0, &v3, 4uLL) == -1 )
{
perror("error writing");
v0 = -1;
}
else
{
v2 = 393;
if ( write(v0, &v2, 4uLL) == -1 )
{
perror("error writing");
v0 = -1;
}
}
}
}
return v0;
}

original_code:
int init_maestro()
{
 const char * device = "/dev/ttyACM0";
 int fd = open(device, 02 | 0400);
 struct termios options;
 tcgetattr(fd, &options);
 options.c_lflag &= ~(0000010 | 0000100 | 0000002 | 0000001 | 0100000);
 options.c_oflag &= ~(0000004 | 0000010);
 tcsetattr(fd, 0, &options);
 unsigned char command[] = {0x87, 0, 0 & 0x7F, 0 >> 7 & 0x7F};
 if (write(fd, command, sizeof(command)) == -1)
 {
  perror("error writing");
  return -1;
 }
 unsigned char command2[] = {0x87, 1, 0 & 0x7F, 0 >> 7 & 0x7F};
 if (write(fd, command2, sizeof(command)) == -1)
 {
  perror("error writing");
  return -1;
 }
 unsigned char command3[] = {0x89, 0, 0 & 0x7F, 0 >> 7 & 0x7F};
 if (write(fd, command3, sizeof(command)) == -1)
 {
  perror("error writing");
  return -1;
 }
 unsigned char command4[] = {0x89, 1, 0 & 0x7F, 0 >> 7 & 0x7F};
 if (write(fd, command4, sizeof(command)) == -1)
 {
  perror("error writing");
  return -1;
 }
 return fd;
}

canonical_original_code:
int init_maestro()
{
 char *device = "/dev/ttyACM0";
 int fd = open(device, 02 | 0400);
 struct termios options;
 tcgetattr(fd, &options);
 options.c_lflag &= ~(0000010 | 0000100 | 0000002 | 0000001 | 0100000);
 options.c_oflag &= ~(0000004 | 0000010);
 tcsetattr(fd, 0, &options);
 unsigned char command[] = {0x87, 0, 0 & 0x7F, 0 >> 7 & 0x7F};
 if (write(fd, command, sizeof(command)) == -1)
 {
  perror("error writing");
  return -1;
 }
 unsigned char command2[] = {0x87, 1, 0 & 0x7F, 0 >> 7 & 0x7F};
 if (write(fd, command2, sizeof(command)) == -1)
 {
  perror("error writing");
  return -1;
 }
 unsigned char command3[] = {0x89, 0, 0 & 0x7F, 0 >> 7 & 0x7F};
 if (write(fd, command3, sizeof(command)) == -1)
 {
  perror("error writing");
  return -1;
 }
 unsigned char command4[] = {0x89, 1, 0 & 0x7F, 0 >> 7 & 0x7F};
 if (write(fd, command4, sizeof(command)) == -1)
 {
  perror("error writing");
  return -1;
 }
 return fd;
}

variable_types:
device
char *

variable_types: fd int

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }

variable_types:
command
unsigned char []


maestroSetTarget

decompiled_code:
__int64 __fastcall maestroSetTarget(int a1, char a2, unsigned __int16 a3)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = -124;
buf[1] = a2;
buf[2] = a3 & 0x7F;
buf[3] = (a3 >> 7) & 0x7F;
if ( write(a1, buf, 4uLL) != -1 )
return 0LL;
perror("error writing");
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, char a2, unsigned __int16 a3)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = -124;
buf[1] = a2;
buf[2] = a3 & 0x7F;
buf[3] = (a3 >> 7) & 0x7F;
if ( write(a1, buf, 4uLL) != -1 )
return 0LL;
perror("error writing");
return 0xFFFFFFFFLL;
}

original_code:
int maestroSetTarget(int fd, unsigned char channel, unsigned short target)
{
  unsigned char command[] = {0x84, channel, target & 0x7F, target >> 7 & 0x7F};
  if (write(fd, command, sizeof(command)) == -1)
  {
    perror("error writing");
    return -1;
  }
  return 0;
}

variable_types:
channel
unsigned char

variable_types:
target
unsigned short


maestroGetPosition

decompiled_code:
__int64 __fastcall maestroGetPosition(int fd, char a2)
{
__int64 result; // rax
char v3[2]; // [rsp+Ch] [rbp-Ch] BYREF
char buf[10]; // [rsp+Eh] [rbp-Ah] BYREF

buf[0] = -112;
buf[1] = a2;
if ( write(fd, buf, 2uLL) == -1 )
{
perror("error writing");
result = 0xFFFFFFFFLL;
}
else if ( read(fd, v3, 2uLL) == 2 )
{
result = (unsigned __int8)v3[0] + ((unsigned __int8)v3[1] << 8);
}
else
{
perror("error reading");
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd, char a2)
{
__int64 result; // rax
char v3[2]; // [rsp+Ch] [rbp-Ch] BYREF
char buf[10]; // [rsp+Eh] [rbp-Ah] BYREF

buf[0] = -112;
buf[1] = a2;
if ( write(fd, buf, 2uLL) == -1 )
{
perror("error writing");
result = 0xFFFFFFFFLL;
}
else if ( read(fd, v3, 2uLL) == 2 )
{
result = (unsigned __int8)v3[0] + ((unsigned __int8)v3[1] << 8);
}
else
{
perror("error reading");
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
int maestroGetPosition(int fd, unsigned char channel)
{
  unsigned char command[] = {0x90, channel};
  if(write(fd, command, sizeof(command)) == -1)
  {
    perror("error writing");
    return -1;
  }
  unsigned char response[2];
  if(read(fd,response,2) != 2)
  {
    perror("error reading");
    return -1;
  }
  return response[0] + 256*response[1];
}

variable_types:
response
unsigned char [2]


decompiled_code:
int __fastcall close_maestro(int a1)
{
return close(a1);
}

canonical_decompiled_code:
int __fastcall func3(int a1)
{
return close(a1);
}

original_code:
void close_maestro(int fd)
{
 close(fd);
}


maestroSetSpeed

decompiled_code:
void __fastcall maestroSetSpeed(int fd, int a2)
{
char v2[4]; // [rsp+8h] [rbp-20h] BYREF
char buf[28]; // [rsp+Ch] [rbp-1Ch] BYREF

buf[0] = -121;
buf[1] = 0;
buf[2] = a2 & 0x7F;
buf[3] = (a2 >> 7) & 0x7F;
if ( write(fd, buf, 4uLL) == -1 )
{
perror("error writing");
}
else
{
v2[0] = -121;
v2[1] = 1;
v2[2] = a2 & 0x7F;
v2[3] = (a2 >> 7) & 0x7F;
if ( write(fd, v2, 4uLL) == -1 )
perror("error writing");
}
}

canonical_decompiled_code:
void __fastcall func4(int fd, int a2)
{
char v2[4]; // [rsp+8h] [rbp-20h] BYREF
char buf[28]; // [rsp+Ch] [rbp-1Ch] BYREF

buf[0] = -121;
buf[1] = 0;
buf[2] = a2 & 0x7F;
buf[3] = (a2 >> 7) & 0x7F;
if ( write(fd, buf, 4uLL) == -1 )
{
perror("error writing");
}
else
{
v2[0] = -121;
v2[1] = 1;
v2[2] = a2 & 0x7F;
v2[3] = (a2 >> 7) & 0x7F;
if ( write(fd, v2, 4uLL) == -1 )
perror("error writing");
}
}

original_code:
void maestroSetSpeed(int fd, int speed)
{
 unsigned char command[] = {0x87, 0, speed & 0x7F, speed >> 7 & 0x7F};
 if (write(fd, command, sizeof(command)) == -1)
 {
  perror("error writing");
  return;
 }
 unsigned char command2[] = {0x87, 1, speed & 0x7F, speed >> 7 & 0x7F};
 if (write(fd, command2, sizeof(command)) == -1)
 {
  perror("error writing");
  return;
 }
}

variable_types: speed int


repo: johncausey/game

decompiled_code:
__int64 setup_sdl()
{
__int64 result; // rax
const char *v1; // rax
const char *v2; // rax
const char *v3; // rax

if ( (int)SDL_Init() < 0 )
{
v1 = (const char *)SDL_GetError();
fprintf(_bss_start, "Couldn't initialise SDL: %s\n", v1);
exit(1);
}
atexit(SDL_Quit);
if ( !SDL_GetVideoInfo() )
{
v2 = (const char *)SDL_GetError();
fprintf(_bss_start, "Couldn't get video information %s\n", v2);
exit(1);
}
SDL_GL_SetAttribute();
SDL_GL_SetAttribute();
SDL_GL_SetAttribute();
SDL_GL_SetAttribute();
SDL_GL_SetAttribute();
SDL_WM_SetCaption();
SDL_EnableKeyRepeat();
result = SDL_SetVideoMode();
if ( !result )
{
v3 = (const char *)SDL_GetError();
fprintf(_bss_start, "Couldn't set video mode: %s\n", v3);
exit(1);
}
return result;
}

canonical_decompiled_code:
__int64 func7()
{
__int64 result; // rax
const char *v1; // rax
const char *v2; // rax
const char *v3; // rax

if ( (int)SDL_Init() < 0 )
{
v1 = (const char *)SDL_GetError();
fprintf(_bss_start, "Couldn't initialise SDL: %s\n", v1);
exit(1);
}
func13(SDL_Quit);
if ( !SDL_GetVideoInfo() )
{
v2 = (const char *)SDL_GetError();
fprintf(_bss_start, "Couldn't get video information %s\n", v2);
exit(1);
}
SDL_GL_SetAttribute();
SDL_GL_SetAttribute();
SDL_GL_SetAttribute();
SDL_GL_SetAttribute();
SDL_GL_SetAttribute();
SDL_WM_SetCaption();
SDL_EnableKeyRepeat();
result = SDL_SetVideoMode();
if ( !result )
{
v3 = (const char *)SDL_GetError();
fprintf(_bss_start, "Couldn't set video mode: %s\n", v3);
exit(1);
}
return result;
}

original_code:
static void setup_sdl() {
 const SDL_VideoInfo* video;
 if ( SDL_Init(0x00000020) < 0 ) {
  fprintf(stderr, "Couldn't initialise SDL: %s\n", SDL_GetError());
  exit(1);
 }
 atexit(SDL_Quit);
 video = SDL_GetVideoInfo();
 if (video == ((void *)0)) {
  fprintf(stderr, "Couldn't get video information %s\n", SDL_GetError());
  exit(1);
 }
 SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5);
 SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5);
 SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5);
 SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16);
 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1);
 SDL_WM_SetCaption( "Totally Bananas?", ((void *)0) );
 SDL_EnableKeyRepeat(70, 70);
 if (SDL_SetVideoMode(1920, 1080, 32, 0x00000002 | 0x80000000) == 0) {
  fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
  exit(1);
 }
}

canonical_original_code:
static void setup_sdl() {
 struct SDL_VideoInfo *video;
 if ( SDL_Init(0x00000020) < 0 ) {
  fprintf(stderr, "Couldn't initialise SDL: %s\n", SDL_GetError());
  exit(1);
 }
 atexit(SDL_Quit);
 video = SDL_GetVideoInfo();
 if (video == ((void *)0)) {
  fprintf(stderr, "Couldn't get video information %s\n", SDL_GetError());
  exit(1);
 }
 SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5);
 SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5);
 SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5);
 SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16);
 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1);
 SDL_WM_SetCaption( "Totally Bananas?", ((void *)0) );
 SDL_EnableKeyRepeat(70, 70);
 if (SDL_SetVideoMode(1920, 1080, 32, 0x00000002 | 0x80000000) == 0) {
  fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
  exit(1);
 }
}

variable_types: struct SDL_VideoInfo { unsigned int hw_available; unsigned int wm_available; unsigned int UnusedBits1; unsigned int UnusedBits2; unsigned int blit_hw; unsigned int blit_hw_CC; unsigned int blit_hw_A; unsigned int blit_sw; unsigned int blit_sw_CC; unsigned int blit_sw_A; unsigned int blit_fill; unsigned int UnusedBits3; unsigned int video_mem; struct SDL_PixelFormat { struct SDL_Palette { int ncolors; struct SDL_Color { unsigned char r; unsigned char g; unsigned char b; unsigned char unused; } *colors; } *palette; unsigned char BitsPerPixel; unsigned char BytesPerPixel; unsigned char Rloss; unsigned char Gloss; unsigned char Bloss; unsigned char Aloss; unsigned char Rshift; unsigned char Gshift; unsigned char Bshift; unsigned char Ashift; unsigned int Rmask; unsigned int Gmask; unsigned int Bmask; unsigned int Amask; unsigned int colorkey; unsigned char alpha; } *vfmt; int current_w; int current_h; } *

user_defined_types: struct SDL_VideoInfo { unsigned int hw_available; unsigned int wm_available; unsigned int UnusedBits1; unsigned int UnusedBits2; unsigned int blit_hw; unsigned int blit_hw_CC; unsigned int blit_hw_A; unsigned int blit_sw; unsigned int blit_sw_CC; unsigned int blit_sw_A; unsigned int blit_fill; unsigned int UnusedBits3; unsigned int video_mem; struct SDL_PixelFormat *vfmt; int current_w; int current_h; }

user_defined_types: struct SDL_PixelFormat { struct SDL_Palette *palette; unsigned char BitsPerPixel; unsigned char BytesPerPixel; unsigned char Rloss; unsigned char Gloss; unsigned char Bloss; unsigned char Aloss; unsigned char Rshift; unsigned char Gshift; unsigned char Bshift; unsigned char Ashift; unsigned int Rmask; unsigned int Gmask; unsigned int Bmask; unsigned int Amask; unsigned int colorkey; unsigned char alpha; }

user_defined_types: struct SDL_Palette { int ncolors; struct SDL_Color *colors; }


decompiled_code:
__int64 setup_opengl()
{
glViewport();
glMatrixMode();
glClearColor();
return glDisable();
}

canonical_decompiled_code:
__int64 func8()
{
glViewport();
glMatrixMode();
glClearColor();
return glDisable();
}

original_code:
static void setup_opengl() {
 float aspect = (float)1920 / (float)1080;
 glViewport(0, 0, 1920, 1080);
 glMatrixMode(0x1701);
 glClearColor(0.5, 0.5, 0.5, 0);
 glDisable(0x0B71);
}

variable_types: aspect float


repaint

decompiled_code:
__int64 repaint()
{
glClear();
glLoadIdentity();
glEnd();
return SDL_GL_SwapBuffers();
}

canonical_decompiled_code:
__int64 func9()
{
glClear();
glLoadIdentity();
glEnd();
return SDL_GL_SwapBuffers();
}

original_code:
static void repaint() {
 glClear(0x00004000 | 0x00000100);
 glLoadIdentity();
 glEnd();
 SDL_GL_SwapBuffers();
}


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
setup_sdl(argc, argv, envp);
setup_opengl();
main_loop();
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
func7(argc, argv, envp);
func8();
func10();
}

original_code:
int main(int argc, char ** argv) {
 setup_sdl();
 setup_opengl();
 main_loop();
 return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
 setup_sdl();
 setup_opengl();
 main_loop();
 return 0;
}

variable_types: argc int

variable_types:
argv
char **


urand

repo: johncheetham/gse

original_code:
unsigned int
urand(void)
{
    next *= 1103515245;
    next += 12345;
    return ((unsigned int) (next >> 16) & 0xFFFF);
}

canonical_original_code:
unsigned int urand()
{
    next *= 1103515245;
    next += 12345;
    return ((unsigned int) (next >> 16) & 0xFFFF);
}

return_type: unsigned int


gsrand

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

result = a1;
next = a1;
return result;
}

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

result = a1;
next = a1;
return result;
}

original_code:
void
gsrand(unsigned int seed)
{
    next = seed;
}

variable_types:
seed
unsigned int


TimeCalc

decompiled_code:
void TimeCalc()
{
int v0; // er11
int v1; // er8
__int64 v2; // rsi
__int64 v3; // r10
__int64 v4; // rdi
int v5; // eax

if ( GameCnt > 7 )
{
v0 = (timeopp[3] + timeopp[2] + timeopp[0] + timeopp[1]) / 400;
v1 = (timecomp[3] + timecomp[2] + timecomp[0] + timecomp[1]) / 400;
v2 = *((_QWORD *)&TimeControl + computer + 1) / 100LL;
v3 = *((_QWORD *)&TimeControl + opponent + 1);
v4 = v3 / 100;
if ( (int)v2 >= (int)(v3 / 100) )
{
v5 = 0;
}
else
{
if ( (int)v4 - (int)v2 > 60 )
{
v5 = 3;
if ( v0 < v1 )
{
v5 = 3;
goto LABEL_7;
}
LABEL_8:
if ( !TCadd && v5 > 0 )
v5 += 2;
if ( !(_DWORD)v2 && v5 > 0 )
LOWORD(v5) = v5 + 2;
goto LABEL_14;
}
if ( (int)v2 <= 119 )
{
v5 = 3;
if ( v0 < v1 )
{
v5 = 3;
LABEL_7:
v5 += 2;
goto LABEL_8;
}
LABEL_21:
if ( (int)v2 > (int)v4 && v0 > v1 )
{
LOWORD(v5) = -1;
LABEL_14:
*((_WORD *)&TimeControl + computer) += v5;
return;
}
goto LABEL_8;
}
v5 = 2;
}
if ( v0 < v1 )
goto LABEL_7;
if ( *((__int16 *)&TimeControl + computer) <= 14 && !v5 )
{
v5 = 1;
goto LABEL_8;
}
goto LABEL_21;
}
}

canonical_decompiled_code:
void func2()
{
int v0; // er11
int v1; // er8
__int64 v2; // rsi
__int64 v3; // r10
__int64 v4; // rdi
int v5; // eax

if ( GameCnt > 7 )
{
v0 = (timeopp[3] + timeopp[2] + timeopp[0] + timeopp[1]) / 400;
v1 = (timecomp[3] + timecomp[2] + timecomp[0] + timecomp[1]) / 400;
v2 = *((_QWORD *)&TimeControl + computer + 1) / 100LL;
v3 = *((_QWORD *)&TimeControl + opponent + 1);
v4 = v3 / 100;
if ( (int)v2 >= (int)(v3 / 100) )
{
v5 = 0;
}
else
{
if ( (int)v4 - (int)v2 > 60 )
{
v5 = 3;
if ( v0 < v1 )
{
v5 = 3;
goto LABEL_7;
}
LABEL_8:
if ( !TCadd && v5 > 0 )
v5 += 2;
if ( !(_DWORD)v2 && v5 > 0 )
LOWORD(v5) = v5 + 2;
goto LABEL_14;
}
if ( (int)v2 <= 119 )
{
v5 = 3;
if ( v0 < v1 )
{
v5 = 3;
LABEL_7:
v5 += 2;
goto LABEL_8;
}
LABEL_21:
if ( (int)v2 > (int)v4 && v0 > v1 )
{
LOWORD(v5) = -1;
LABEL_14:
*((_WORD *)&TimeControl + computer) += v5;
return;
}
goto LABEL_8;
}
v5 = 2;
}
if ( v0 < v1 )
goto LABEL_7;
if ( *((__int16 *)&TimeControl + computer) <= 14 && !v5 )
{
v5 = 1;
goto LABEL_8;
}
goto LABEL_21;
}
}

original_code:
void
TimeCalc()
{
    int increment = 0;
    int topsum = 0;
    int tcompsum = 0;
    int me, him;
    int i;
    if (GameCnt < (4 * 2))
        return;
    for (i = 0; i < 4; i++)
    {
        tcompsum += timecomp[i];
        topsum += timeopp[i];
    }
    topsum /= (100 * 4);
    tcompsum /= (100 * 4);
    me = TimeControl.clock[computer] / 100;
    him = TimeControl.clock[opponent] / 100;
    if (me < him)
        increment += 2;
    if (((him - me) > 60) || ((me < him) && (me < 120)))
        increment++;
    if (tcompsum > topsum)
    {
        increment += 2;
    }
    else if ((TimeControl.moves[computer] < 15) && !increment)
    {
        increment++;
    }
    else if ((me > him) && (tcompsum < topsum))
    {
        increment = -1;
    }
    if ((TCadd == 0) && (increment > 0))
        increment += 2;
    if ((me == 0) && (increment > 0))
        increment += 2;
    TimeControl.moves[computer] += increment;
}

canonical_original_code:
void TimeCalc()
{
    int increment = 0;
    int topsum = 0;
    int tcompsum = 0;
    int me; int him;
    int i;
    if (GameCnt < (4 * 2))
        return;
    for (i = 0; i < 4; i++)
    {
        tcompsum += timecomp[i];
        topsum += timeopp[i];
    }
    topsum /= (100 * 4);
    tcompsum /= (100 * 4);
    me = TimeControl.clock[computer] / 100;
    him = TimeControl.clock[opponent] / 100;
    if (me < him)
        increment += 2;
    if (((him - me) > 60) || ((me < him) && (me < 120)))
        increment++;
    if (tcompsum > topsum)
    {
        increment += 2;
    }
    else if ((TimeControl.moves[computer] < 15) && !increment)
    {
        increment++;
    }
    else if ((me > him) && (tcompsum < topsum))
    {
        increment = -1;
    }
    if ((TCadd == 0) && (increment > 0))
        increment += 2;
    if ((me == 0) && (increment > 0))
        increment += 2;
    TimeControl.moves[computer] += increment;
}

variable_types: increment int

variable_types: topsum int

variable_types: tcompsum int

variable_types: me int

variable_types: him int


SetResponseTime

decompiled_code:
void __fastcall SetResponseTime(__int16 a1)
{
int v2; // ecx
_BOOL4 v3; // er10
__int64 v4; // r9
__int16 v5; // r11
__int16 v6; // di
int v7; // er8
__int64 v8; // rax
__int64 v9; // r11
__int64 v10; // rax
int v11; // er9

if ( !TCflag )
{
TCleft = 0LL;
ResponseTime = MaxResponseTime;
ElapsedTime(1LL);
LABEL_23:
if ( TCleft )
{
v10 = (int)(*((_QWORD *)&TimeControl + a1 + 1) - ResponseTime) / 2 / (__int64)TCleft;
if ( (int)v10 <= 0 )
{
TCcount = 10;
}
else
{
v11 = 0;
if ( (int)v10 <= 10 )
v11 = 10 - v10;
TCcount = v11;
}
}
else
{
TCcount = 10;
}
goto LABEL_30;
}
TCcount = 0;
v2 = a1;
if ( *((__int16 *)&TimeControl + a1) <= 0 )
*((_WORD *)&TimeControl + a1) = 1;
if ( flag[20] )
{
ResponseTime = *((_QWORD *)&TimeControl + a1 + 1) - 100LL;
TCleft = 0LL;
v3 = 1;
}
else
{
v4 = *((_QWORD *)&TimeControl + a1 + 1);
if ( v4 <= 0 )
{
ResponseTime = 0LL;
TCleft = 10LL;
}
else
{
if ( flag[22] )
{
v5 = 2;
v6 = 4;
}
else if ( stage <= 32 )
{
v5 = 8;
v6 = 2;
}
else
{
v5 = 2;
v6 = 2 * (stage >= 33) + 2;
}
v7 = TCadd;
v8 = v4 + TCadd;
*((_QWORD *)&TimeControl + v2 + 1) = v8;
v9 = v8 / (v5 * *((__int16 *)&TimeControl + v2) + 1);
TCleft = v9 / v6;
ResponseTime = v7 / 2 + v9;
}
v3 = *((_WORD *)&TimeControl + v2) > 4;
}
if ( ResponseTime <= 99 )
{
ResponseTime = 100LL;
if ( hard_time_limit )
goto LABEL_30;
goto LABEL_8;
}
if ( hard_time_limit || ResponseTime > 199 )
{
if ( !v3 )
return;
goto LABEL_23;
}
LABEL_8:
TCcount = 0;
LABEL_30:
if ( ResponseTime <= 99 )
{
ResponseTime = 100LL;
if ( TCcount > 10 )
_assert_fail("TCcount <= MAXTCCOUNTX", "tcontrl.c", 0x142u, "SetResponseTime");
}
}

canonical_decompiled_code:
void __fastcall func3(__int16 a1)
{
int v2; // ecx
_BOOL4 v3; // er10
__int64 v4; // r9
__int16 v5; // r11
__int16 v6; // di
int v7; // er8
__int64 v8; // rax
__int64 v9; // r11
__int64 v10; // rax
int v11; // er9

if ( !TCflag )
{
TCleft = 0LL;
ResponseTime = MaxResponseTime;
ElapsedTime(1LL);
LABEL_23:
if ( TCleft )
{
v10 = (int)(*((_QWORD *)&TimeControl + a1 + 1) - ResponseTime) / 2 / (__int64)TCleft;
if ( (int)v10 <= 0 )
{
TCcount = 10;
}
else
{
v11 = 0;
if ( (int)v10 <= 10 )
v11 = 10 - v10;
TCcount = v11;
}
}
else
{
TCcount = 10;
}
goto LABEL_30;
}
TCcount = 0;
v2 = a1;
if ( *((__int16 *)&TimeControl + a1) <= 0 )
*((_WORD *)&TimeControl + a1) = 1;
if ( flag[20] )
{
ResponseTime = *((_QWORD *)&TimeControl + a1 + 1) - 100LL;
TCleft = 0LL;
v3 = 1;
}
else
{
v4 = *((_QWORD *)&TimeControl + a1 + 1);
if ( v4 <= 0 )
{
ResponseTime = 0LL;
TCleft = 10LL;
}
else
{
if ( flag[22] )
{
v5 = 2;
v6 = 4;
}
else if ( stage <= 32 )
{
v5 = 8;
v6 = 2;
}
else
{
v5 = 2;
v6 = 2 * (stage >= 33) + 2;
}
v7 = TCadd;
v8 = v4 + TCadd;
*((_QWORD *)&TimeControl + v2 + 1) = v8;
v9 = v8 / (v5 * *((__int16 *)&TimeControl + v2) + 1);
TCleft = v9 / v6;
ResponseTime = v7 / 2 + v9;
}
v3 = *((_WORD *)&TimeControl + v2) > 4;
}
if ( ResponseTime <= 99 )
{
ResponseTime = 100LL;
if ( hard_time_limit )
goto LABEL_30;
goto LABEL_8;
}
if ( hard_time_limit || ResponseTime > 199 )
{
if ( !v3 )
return;
goto LABEL_23;
}
LABEL_8:
TCcount = 0;
LABEL_30:
if ( ResponseTime <= 99 )
{
ResponseTime = 100LL;
if ( TCcount > 10 )
_assert_fail("TCcount <= MAXTCCOUNTX", "tcontrl.c", 0x142u, "SetResponseTime");
}
}

original_code:
void SetResponseTime(short side)
{
    int DetermineTCcount = 1;
    if (TCflag)
    {
        TCcount = 0;
        if (TimeControl.moves[side] < 1)
            TimeControl.moves[side] = 1;
        if (flag.onemove)
        {
            ResponseTime = TimeControl.clock[side] - 100;
            TCleft = 0;
        }
        else
        {
            if (TimeControl.clock[side] <= 0)
            {
                ResponseTime = 0;
                TCleft = (long)100 / 10;
            }
            else
            {
                short rtf = (!flag.tsume && (stage < 33)) ? 8 : 2;
                short tcq = (!flag.tsume && (stage < 33)) ? 2 : 4;
                TimeControl.clock[side] += TCadd;
                ResponseTime = (TimeControl.clock[side])
                    / (((TimeControl.moves[side]) * rtf) + 1);
                TCleft = (long)ResponseTime / tcq;
                ResponseTime += TCadd / 2;
            }
            if (TimeControl.moves[side] < 5)
            {
                TCcount = 10 - 10;
                if (TCcount < 0)
                    TCcount = 0;
                DetermineTCcount = 0;
            }
        }
        if (ResponseTime < 100)
        {
            ResponseTime = 100;
            TCcount = 10 - 10;
            if (TCcount < 0)
                TCcount = 0;
            DetermineTCcount = 0;
        }
        if (!hard_time_limit && (ResponseTime < 2 * 100))
        {
            TCcount = 10 - 10;
            if (TCcount < 0)
                TCcount = 0;
            DetermineTCcount = 0;
        }
    }
    else
    {
        TCleft = 0;
        ResponseTime = MaxResponseTime;
        ElapsedTime(COMPUTE_AND_INIT_MODE);
    }
    if (DetermineTCcount)
    {
        if (TCleft )
        {
            int AllowedCounts
                = ((int)((TimeControl.clock[side] - ResponseTime)) / 2)
                / TCleft;
            if (AllowedCounts <= 0)
                TCcount = 10;
            else if (AllowedCounts > 10)
                TCcount = 0;
            else
                TCcount = 10 - AllowedCounts;
        }
        else
        {
            TCcount = 10;
        }
    }
    if (ResponseTime < 100)
        ResponseTime = 100;
    ((TCcount <= 10) ? (void) (0) : __assert_fail ("TCcount <= MAXTCCOUNTX", "tcontrl.c", 322, __extension__ __PRETTY_FUNCTION__));
}

variable_types: side short

variable_types: DetermineTCcount int

variable_types: rtf short

variable_types: tcq short

variable_types: AllowedCounts int


CheckForTimeout

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

if ( flag[11] || Sdepth >= MaxSearchDepth )
{
flag[10] = 1;
}
else if ( TCflag )
{
if ( Sdepth > 1 )
{
v5 = TCcount;
if ( TCcount <= 3 )
{
if ( killr0[1] == PrVar[1] || (v5 = TCcount + 1, TCcount = v5, ExtraTime += TCleft, v5 <= 3) )
{
if ( (int)abs32(a1 - a2) / Sdepth > 10 )
{
TCcount = v5 + 1;
ExtraTime += TCleft;
}
}
}
}
}
if ( a3 - a4 < a1 && *(__int16 *)(Tree + 14LL) + 250 < a1 )
ExtraTime = 0LL;
result = ElapsedTime(2LL);
if ( (*(_BYTE *)(root + 10LL) & 0x40) != 0 )
{
flag[10] = 1;
return ShowMessage("timeout");
}
if ( Sdepth > 1 )
{
if ( TCflag )
{
result = ExtraTime;
if ( 4LL * et > ExtraTime + 2LL * ResponseTime )
{
flag[10] = 1;
return ShowMessage("timeout");
}
}
}
if ( flag[10] )
return ShowMessage("timeout");
return result;
}

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

if ( flag[11] || Sdepth >= MaxSearchDepth )
{
flag[10] = 1;
}
else if ( TCflag )
{
if ( Sdepth > 1 )
{
v5 = TCcount;
if ( TCcount <= 3 )
{
if ( killr0[1] == PrVar[1] || (v5 = TCcount + 1, TCcount = v5, ExtraTime += TCleft, v5 <= 3) )
{
if ( (int)abs32(a1 - a2) / Sdepth > 10 )
{
TCcount = v5 + 1;
ExtraTime += TCleft;
}
}
}
}
}
if ( a3 - a4 < a1 && *(__int16 *)(Tree + 14LL) + 250 < a1 )
ExtraTime = 0LL;
result = ElapsedTime(2LL);
if ( (*(_BYTE *)(root + 10LL) & 0x40) != 0 )
{
flag[10] = 1;
return ShowMessage("timeout");
}
if ( Sdepth > 1 )
{
if ( TCflag )
{
result = ExtraTime;
if ( 4LL * et > ExtraTime + 2LL * ResponseTime )
{
flag[10] = 1;
return ShowMessage("timeout");
}
}
}
if ( flag[10] )
return ShowMessage("timeout");
return result;
}

original_code:
void
CheckForTimeout(int score, int globalscore, int Jscore, int zwndw)
{
    if (flag.musttimeout || (Sdepth >= MaxSearchDepth))
        flag.timeout = 1;
    else if (TCflag && (Sdepth > (2 - 1)) && (TCcount < 4))
    {
        if (killr0[1] != PrVar[1] )
        {
            TCcount++;
            ExtraTime += TCleft;
        }
        if ((TCcount < 4)
            && (abs(score - globalscore) / Sdepth) > 10)
        {
            TCcount++;
            ExtraTime += TCleft;
        }
    }
    if ((score > (Jscore - zwndw)) && (score > (Tree[1].score + 250)))
        ExtraTime = 0;
    ElapsedTime(COMPUTE_MODE);
    if (root->flags & 0x0040)
        flag.timeout = 1;
    else if (!(Sdepth < 2)
             && TCflag
             && ((4 * et) > (2*ResponseTime + ExtraTime)))
        flag.timeout = 1;
    if (flag.timeout)
        ShowMessage("timeout");
}

variable_types: score int

variable_types: globalscore int

variable_types: Jscore int

variable_types: zwndw int


SqAttacked

original_code:
int
SqAttacked(short square, short side, short *blockable)
{
    unsigned char *ppos, *pdir;
    short u, ptyp;
    if (((threats_signature[side].hashbd == hashbd) && (threats_signature[side].hashkey == hashkey)))
    {
        *blockable = 1;
        return (attack[side][square] != 0);
    }
    *blockable = 0;
    ptyp = ptype[0][14];
    pdir = (*nextdir[ptyp])[square];
    u = pdir[square];
    do
    {
        if (color[u] == side)
        {
            if (piece_distance(side, board[u], u, square) == 1)
                return 1;
        }
        u = pdir[u];
    }
    while (u != square);
    ptyp = ptype[side ^ 1][3];
    pdir = (*nextdir[ptyp])[square];
    u = pdir[square];
    do
    {
        if (color[u] == side && board[u] == 3)
            return 1;
        u = pdir[u];
    }
    while (u != square);
    *blockable = 1;
    ptyp = ptype[0][6];
    ppos = (*nextpos[ptyp])[square];
    pdir = (*nextdir[ptyp])[square];
    u = ppos[square];
    do
    {
        if (color[u] == 2)
        u = ppos[u];
        else
        {
            if (color[u] == side && (unpromoted[board[u]] == 6))
                return 1;
            u = pdir[u];
        }
    }
    while (u != square);
    ptyp = ptype[0][7];
    ppos = (*nextpos[ptyp])[square];
    pdir = (*nextdir[ptyp])[square];
    u = ppos[square];
    do
    {
        if (color[u] == 2)
        {
            u = ppos[u];
        }
        else
        {
            if (color[u] == side && (unpromoted[board[u]] == 7))
                return 1;
            u = pdir[u];
        }
    }
    while (u != square);
    ptyp = ptype[side ^ 1][2];
    ppos = (*nextpos[ptyp])[square];
    u = ppos[square];
    do
    {
        if (color[u] == 2)
        {
            u = ppos[u];
        }
        else
        {
            if ((color[u] == side) && (board[u] == 2))
                return 1;
            u = square;
        }
    }
    while (u != square);
    return 0;
}

canonical_original_code:
int SqAttacked(short square, short side, short *blockable)
{
    unsigned char *ppos; unsigned char *pdir;
    short u; short ptyp;
    if (((threats_signature[side].hashbd == hashbd) && (threats_signature[side].hashkey == hashkey)))
    {
        *blockable = 1;
        return (attack[side][square] != 0);
    }
    *blockable = 0;
    ptyp = ptype[0][14];
    pdir = (*nextdir[ptyp])[square];
    u = pdir[square];
    do
    {
        if (color[u] == side)
        {
            if (piece_distance(side, board[u], u, square) == 1)
                return 1;
        }
        u = pdir[u];
    }
    while (u != square);
    ptyp = ptype[side ^ 1][3];
    pdir = (*nextdir[ptyp])[square];
    u = pdir[square];
    do
    {
        if (color[u] == side && board[u] == 3)
            return 1;
        u = pdir[u];
    }
    while (u != square);
    *blockable = 1;
    ptyp = ptype[0][6];
    ppos = (*nextpos[ptyp])[square];
    pdir = (*nextdir[ptyp])[square];
    u = ppos[square];
    do
    {
        if (color[u] == 2)
        u = ppos[u];
        else
        {
            if (color[u] == side && (unpromoted[board[u]] == 6))
                return 1;
            u = pdir[u];
        }
    }
    while (u != square);
    ptyp = ptype[0][7];
    ppos = (*nextpos[ptyp])[square];
    pdir = (*nextdir[ptyp])[square];
    u = ppos[square];
    do
    {
        if (color[u] == 2)
        {
            u = ppos[u];
        }
        else
        {
            if (color[u] == side && (unpromoted[board[u]] == 7))
                return 1;
            u = pdir[u];
        }
    }
    while (u != square);
    ptyp = ptype[side ^ 1][2];
    ppos = (*nextpos[ptyp])[square];
    u = ppos[square];
    do
    {
        if (color[u] == 2)
        {
            u = ppos[u];
        }
        else
        {
            if ((color[u] == side) && (board[u] == 2))
                return 1;
            u = square;
        }
    }
    while (u != square);
    return 0;
}

variable_types: square short

variable_types:
blockable
short *

variable_types:
ppos
unsigned char *

variable_types:
pdir
unsigned char *

variable_types: u short

variable_types: ptyp short


decompiled_code:
time_t __fastcall ElapsedTime_NOFIONREAD(int a1)
{
time_t v1; // rdx
time_t result; // rax
__int64 v3; // rsi

v1 = time(0LL);
result = v1 - time0;
ETnodes = NodeCnt + znodes;
v3 = 100 * (v1 - time0);
if ( v3 < 0 )
v3 = 0LL;
et = v3;
if ( a1 == 1 )
{
if ( ResponseTime + ExtraTime < et && Sdepth > 2 )
flag[10] = 1;
time0 = v1;
}
return result;
}

canonical_decompiled_code:
time_t __fastcall func0(int a1)
{
time_t v1; // rdx
time_t result; // rax
__int64 v3; // rsi

v1 = time(0LL);
result = v1 - time0;
ETnodes = NodeCnt + znodes;
v3 = 100 * (v1 - time0);
if ( v3 < 0 )
v3 = 0LL;
et = v3;
if ( a1 == 1 )
{
if ( ResponseTime + ExtraTime < et && Sdepth > 2 )
flag[10] = 1;
time0 = v1;
}
return result;
}

original_code:
void
ElapsedTime_NOFIONREAD(ElapsedTime_mode iop)
{
    long current_time;
    et = ((current_time = time((long *) 0)) - time0) * 100;
    {
        ETnodes = NodeCnt + znodes;
        if (et < 0)
        {
            et = 0;
        }
        if (iop == COMPUTE_AND_INIT_MODE)
        {
            if ((et > (ResponseTime + ExtraTime)) && (Sdepth > 2))
                flag.timeout = 1;
            time0 = current_time;
        }
    }
}

canonical_original_code:
void ElapsedTime_NOFIONREAD(enum ElapsedTime_mode iop)
{
    long current_time;
    et = ((current_time = time((long *) 0)) - time0) * 100;
    {
        ETnodes = NodeCnt + znodes;
        if (et < 0)
        {
            et = 0;
        }
        if (iop == COMPUTE_AND_INIT_MODE)
        {
            if ((et > (ResponseTime + ExtraTime)) && (Sdepth > 2))
                flag.timeout = 1;
            time0 = current_time;
        }
    }
}

variable_types: enum ElapsedTime_mode { COMPUTE_AND_INIT_MODE=1, COMPUTE_MODE=2 }


ElapsedTime

decompiled_code:
time_t __fastcall ElapsedTime(int a1)
{
return ElapsedTime_NOFIONREAD(a1);
}

canonical_decompiled_code:
time_t __fastcall func1(int a1)
{
return func0(a1);
}

original_code:
void
ElapsedTime(ElapsedTime_mode iop)
{
    ElapsedTime_NOFIONREAD(iop);
}

canonical_original_code:
void ElapsedTime(enum ElapsedTime_mode iop)
{
    ElapsedTime_NOFIONREAD(iop);
}

user_defined_types: enum ElapsedTime_mode { COMPUTE_AND_INIT_MODE=1, COMPUTE_MODE=2 }


main

repo: johndgiese/c-bootstrap

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
float v3; // xmm0_4
float v4; // xmm0_4
float v6; // [rsp+8h] [rbp-10h] BYREF
float v7; // [rsp+Ch] [rbp-Ch]

printf("Enter a number: ");
__isoc99_scanf();
printf("Enter a number: ");
__isoc99_scanf();
v3 = add("%f", &v6, COERCE_DOUBLE((unsigned __int64)LODWORD(v7)), COERCE_DOUBLE((unsigned __int64)LODWORD(v6)));
printf("Sum: %f\n", v3);
v4 = subtract(v7, v6);
printf("Difference: %f\n", v4);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
float v3; // xmm0_4
float v4; // xmm0_4
float v6; // [rsp+8h] [rbp-10h] BYREF
float v7; // [rsp+Ch] [rbp-Ch]

printf("Enter a number: ");
__isoc99_scanf();
printf("Enter a number: ");
__isoc99_scanf();
v3 = func7("%f", &v6, COERCE_DOUBLE((unsigned __int64)LODWORD(v7)), COERCE_DOUBLE((unsigned __int64)LODWORD(v6)));
printf("Sum: %f\n", v3);
v4 = func8(v7, v6);
printf("Difference: %f\n", v4);
return 0;
}

original_code:
int main(int argc, const char *argv[]) {
    float a, b;
    printf("Enter a number: ");
    scanf("%f", &a);
    printf("Enter a number: ");
    scanf("%f", &b);
    printf("Sum: %f\n", add(a, b));
    printf("Difference: %f\n", subtract(a, b));
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[]) {
    float a; float b;
    printf("Enter a number: ");
    scanf("%f", &a);
    printf("Enter a number: ");
    scanf("%f", &b);
    printf("Sum: %f\n", add(a, b));
    printf("Difference: %f\n", subtract(a, b));
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: a float

variable_types: b float


problem

repo: johndgiese/knking-c

decompiled_code:
int __fastcall problem(unsigned int a1, unsigned int a2)
{
return printf("\nPROBLEM %2d.%-4d\n", a1, a2);
}

canonical_decompiled_code:
int __fastcall func7(unsigned int a1, unsigned int a2)
{
return printf("\nPROBLEM %2d.%-4d\n", a1, a2);
}

original_code:
void problem(int chapter, int number) {
    printf("\nPROBLEM %2d.%-4d\n", chapter, number);
}

variable_types: chapter int

variable_types: number int


main

variable_types: a int

variable_types: b int

variable_types: c int

variable_types: d int

variable_types: aa float

variable_types: bb float

variable_types: cc float

variable_types: dd float


decompiled_code:
__int64 grab_single_number()
{
__isoc99_scanf();
return 1234LL;
}

canonical_decompiled_code:
__int64 func9()
{
__isoc99_scanf();
return 1234LL;
}

original_code:
unsigned int grab_single_number(unsigned int *number_p) {
    int status = scanf("%u", number_p);
    return 1234;
}

variable_types: status int


decompiled_code:
__int64 prompt_for_amount()
{
unsigned int v1[3]; // [rsp+Ch] [rbp-Ch] BYREF

while ( 1 )
{
printf("Enter a dollar amount: ");
if ( !(unsigned int)grab_single_number(v1) )
break;
puts("Values must be whole numbers.");
}
return v1[0];
}

canonical_decompiled_code:
__int64 func10()
{
unsigned int v1[3]; // [rsp+Ch] [rbp-Ch] BYREF

while ( 1 )
{
printf("Enter a dollar amount: ");
if ( !(unsigned int)func9(v1) )
break;
puts("Values must be whole numbers.");
}
return v1[0];
}

original_code:
unsigned int prompt_for_amount() {
    unsigned int amount;
    while (1) {
        printf("Enter a dollar amount: ");
        int status = grab_single_number(&amount);
        if (status) {
            printf("Values must be whole numbers.\n");
        } else {
            break;
        }
    }
    return amount;
}

variable_types:
amount
unsigned int


decompiled_code:
unsigned __int64 __fastcall breakdown_amount(int a1)
{
return (unsigned int)(a1 % 20 % 10 % 5) | ((unsigned __int64)(unsigned int)(a1 % 20 % 10 / 5) << 32);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func11(int a1)
{
return (unsigned int)(a1 % 20 % 10 % 5) | ((unsigned __int64)(unsigned int)(a1 % 20 % 10 / 5) << 32);
}

original_code:
billbreakdown breakdown_amount(int amount) {
    billbreakdown b;
    b.twenties = amount/20;
    amount = amount - 20*b.twenties;
    b.tens = amount/10;
    amount = amount - 10*b.tens;
    b.fives = amount/5;
    amount = amount - 5*b.fives;
    b.ones = amount;
    return b;
}

canonical_original_code:
struct billbreakdown breakdown_amount(int amount) {
    struct billbreakdown b;
    b.twenties = amount/20;
    amount = amount - 20*b.twenties;
    b.tens = amount/10;
    amount = amount - 10*b.tens;
    b.fives = amount/5;
    amount = amount - 5*b.fives;
    b.ones = amount;
    return b;
}

variable_types: amount int

variable_types: struct billbreakdown { int ones; int fives; int tens; int twenties; }


decompiled_code:
int __fastcall print_breakdown(__int64 a1, __int64 a2)
{
printf("$20 bills: %d\n", a2 >> 32);
printf("$10 bills: %d\n", (unsigned int)a2);
printf(" $5 bills: %d\n", a1 >> 32);
return printf(" $1 bills: %d\n", (unsigned int)a1);
}

canonical_decompiled_code:
int __fastcall func12(__int64 a1, __int64 a2)
{
printf("$20 bills: %d\n", a2 >> 32);
printf("$10 bills: %d\n", (unsigned int)a2);
printf(" $5 bills: %d\n", a1 >> 32);
return printf(" $1 bills: %d\n", (unsigned int)a1);
}

original_code:
void print_breakdown(billbreakdown b) {
    printf("$20 bills: %d\n", b.twenties);
    printf("$10 bills: %d\n", b.tens);
    printf(" $5 bills: %d\n", b.fives);
    printf(" $1 bills: %d\n", b.ones);
}

canonical_original_code:
void print_breakdown(struct billbreakdown b) {
    printf("$20 bills: %d\n", b.twenties);
    printf("$10 bills: %d\n", b.tens);
    printf(" $5 bills: %d\n", b.fives);
    printf(" $1 bills: %d\n", b.ones);
}

user_defined_types: struct billbreakdown { int ones; int fives; int tens; int twenties; }


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

result = a1;
if ( (int)a2 >= (int)a1 )
result = a2;
return result;
}

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

result = a1;
if ( (int)a2 >= (int)a1 )
result = a2;
return result;
}

original_code: int int_max(int x, int y) { return x > y ? x : y; }

variable_types: x int

variable_types: y int


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

result = a1;
if ( a2 >= a1 )
result = a2;
return result;
}

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

result = a1;
if ( a2 >= a1 )
result = a2;
return result;
}

original_code: unsigned long ul_max(unsigned long x, unsigned long y) { return x > y ? x : y; }

variable_types:
x
unsigned long

variable_types:
y
unsigned long


decompiled_code:
__int64 __fastcall dummy_gettext(__int64 a1)
{
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return a1;
}

original_code: static char * dummy_gettext (const char *msgid) { return (char *) (msgid); }

canonical_original_code: static char *dummy_gettext(char *msgid) { return (char *) (msgid); }

variable_types:
msgid
char *


decompiled_code:
__int64 __fastcall dummy_dgettext(__int64 a1, __int64 a2)
{
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
return a2;
}

original_code: static char * dummy_dgettext (const char *domainname, const char *msgid) { return (char *) (msgid); }

canonical_original_code: static char *dummy_dgettext(char *domainname, char *msgid) { return (char *) (msgid); }

variable_types:
domainname
char *


decompiled_code:
__int64 __fastcall dummy_dcgettext(__int64 a1, __int64 a2)
{
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2)
{
return a2;
}

original_code: static char * dummy_dcgettext (const char *domainname, const char *msgid, int category) { return (char *) (msgid); }

canonical_original_code: static char *dummy_dcgettext(char *domainname, char *msgid, int category) { return (char *) (msgid); }

variable_types: category int


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

result = a2;
if ( a3 == 1 )
result = a1;
return result;
}

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

result = a2;
if ( a3 == 1 )
result = a1;
return result;
}

original_code: static char * dummy_ngettext (const char *msgid1, const char *msgid2, unsigned long int n) { return (char *) (n == 1 ? msgid1 : msgid2); }

canonical_original_code: static char *dummy_ngettext(char *msgid1, char *msgid2, unsigned long n) { return (char *) (n == 1 ? msgid1 : msgid2); }

variable_types:
n
unsigned long


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

result = a3;
if ( a4 == 1 )
result = a2;
return result;
}

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

result = a3;
if ( a4 == 1 )
result = a2;
return result;
}

original_code: static char * dummy_dngettext (const char *domainname, const char *msgid1, const char *msgid2, unsigned long int n) { return (char *) (n == 1 ? msgid1 : msgid2); }

canonical_original_code: static char *dummy_dngettext(char *domainname, char *msgid1, char *msgid2, unsigned long n) { return (char *) (n == 1 ? msgid1 : msgid2); }


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

result = a3;
if ( a4 == 1 )
result = a2;
return result;
}

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

result = a3;
if ( a4 == 1 )
result = a2;
return result;
}

original_code: static char * dummy_dcngettext (const char *domainname, const char *msgid1, const char *msgid2, unsigned long int n, int category) { return (char *) (n == 1 ? msgid1 : msgid2); }

canonical_original_code: static char *dummy_dcngettext(char *domainname, char *msgid1, char *msgid2, unsigned long n, int category) { return (char *) (n == 1 ? msgid1 : msgid2); }


decompiled_code:
const char *dummy_bindtextdomain()
{
return "/dummy";
}

canonical_decompiled_code:
const char *func6()
{
return "/dummy";
}

original_code: static char * dummy_bindtextdomain (const char *domainname, const char *dirname) { return (char *) ("/dummy"); }

canonical_original_code: static char *dummy_bindtextdomain(char *domainname, char *dirname) { return (char *) ("/dummy"); }

variable_types:
dirname
char *


decompiled_code:
__int64 __fastcall dummy_bind_textdomain_codeset(__int64 a1, __int64 a2)
{
return a2;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, __int64 a2)
{
return a2;
}

original_code: static char * dummy_bind_textdomain_codeset (const char *domainname, const char *codeset) { return (char *) (codeset); }

canonical_original_code: static char *dummy_bind_textdomain_codeset(char *domainname, char *codeset) { return (char *) (codeset); }

variable_types:
codeset
char *