decompiled_code:
__int64 __fastcall thumb2_parse_reg(_DWORD *a1, char a2, unsigned __int16 a3)
{
__int64 result; // rax

result = (unsigned int)(a1[2] - 53);
switch ( a1[2] )
{
case '5':
a1[24] = a3 >> 12;
break;
case '6':
result = a3 >> 12;
a1[24] = result;
a1[25] = HIBYTE(a3) & 0xF;
break;
case '7':
a1[22] = a2 & 0xF;
break;
case '8':
a1[20] = HIBYTE(a3) & 0xF;
break;
case '9':
result = HIBYTE(a3) & 0xF;
a1[20] = result;
a1[22] = a3 & 0xF;
break;
case ':':
a1[21] = a2 & 0xF;
break;
case ';':
a1[21] = a2 & 0xF;
a1[24] = a3 >> 12;
break;
case '<':
a1[21] = a2 & 0xF;
result = a3 >> 12;
a1[24] = result;
a1[25] = HIBYTE(a3) & 0xF;
break;
case '=':
a1[21] = a2 & 0xF;
a1[22] = a3 & 0xF;
break;
case '>':
a1[21] = a2 & 0xF;
result = a3 & 0xF;
a1[22] = result;
a1[24] = a3 >> 12;
break;
case '?':
a1[21] = a2 & 0xF;
a1[20] = HIBYTE(a3) & 0xF;
break;
case '@':
a1[21] = a2 & 0xF;
result = a3 & 0xF;
a1[20] = result;
a1[24] = a3 >> 12;
break;
case 'A':
a1[21] = a2 & 0xF;
a1[20] = a3 & 0xF;
result = a3 >> 12;
a1[24] = result;
a1[25] = HIBYTE(a3) & 0xF;
break;
case 'B':
a1[21] = a2 & 0xF;
result = a3 & 0xF;
a1[22] = result;
a1[20] = HIBYTE(a3) & 0xF;
break;
case 'C':
a1[21] = a2 & 0xF;
a1[22] = a3 & 0xF;
result = HIBYTE(a3) & 0xF;
a1[20] = result;
a1[23] = a3 >> 12;
break;
default:
return result;
}
return result;
}

original_code:
void thumb2_parse_reg(darm_t *d, uint16_t w, uint16_t w2)
{
    switch (d->instr_type) {
    case T_THUMB2_NO_REG:
        break;
    case T_THUMB2_RT_REG:
        d->Rt = (w2 >> 12) & 15;
        break;
    case T_THUMB2_RT_RT2_REG:
        d->Rt = (w2 >> 12) & 15;
        d->Rt2 = (w2 >> 8) & 15;
        break;
    case T_THUMB2_RM_REG:
        d->Rm = (w & 15);
        break;
    case T_THUMB2_RD_REG:
        d->Rd = (w2 >> 8) & 15;
        break;
    case T_THUMB2_RD_RM_REG:
        d->Rd = (w2 >> 8) & 15;
        d->Rm = w2 & 15;
        break;
    case T_THUMB2_RN_REG:
        d->Rn = w & 15;
        break;
    case T_THUMB2_RN_RT_REG:
        d->Rn = w & 15;
        d->Rt = (w2 >> 12) & 15;
        break;
    case T_THUMB2_RN_RT_RT2_REG:
        d->Rn = w & 15;
        d->Rt = (w2 >> 12) & 15;
        d->Rt2 = (w2 >> 8) & 15;
        break;
    case T_THUMB2_RN_RM_REG:
        d->Rn = w & 15;
        d->Rm = w2 & 15;
        break;
    case T_THUMB2_RN_RM_RT_REG:
        d->Rn = w & 15;
        d->Rm = w2 & 15;
        d->Rt = (w2 >> 12) & 15;
        break;
    case T_THUMB2_RN_RD_REG:
        d->Rn = w & 15;
        d->Rd = (w2 >> 8) & 15;
        break;
    case T_THUMB2_RN_RD_RT_REG:
        d->Rn = (w & 15);
        d->Rd = (w2 & 15);
        d->Rt = (w2 >> 12) & 15;
        break;
    case T_THUMB2_RN_RD_RT_RT2_REG:
        d->Rn = (w & 15);
        d->Rd = (w2 & 15);
        d->Rt = (w2 >> 12) & 15;
        d->Rt2 = (w2 >> 8) & 15;
        break;
    case T_THUMB2_RN_RD_RM_REG:
        d->Rn = w & 15;
        d->Rm = w2 & 15;
        d->Rd = (w2 >> 8) & 15;
        break;
    case T_THUMB2_RN_RD_RM_RA_REG:
        d->Rn = w & 15;
        d->Rm = w2 & 15;
        d->Rd = (w2 >> 8) & 15;
        d->Ra = (w2 >> 12) & 15;
        break;
    default:
        break;
    }
}

canonical_original_code:
void thumb2_parse_reg(struct _darm_t *d, unsigned short w, unsigned short w2)
{
    switch (d->instr_type) {
    case T_THUMB2_NO_REG:
        break;
    case T_THUMB2_RT_REG:
        d->Rt = (w2 >> 12) & 15;
        break;
    case T_THUMB2_RT_RT2_REG:
        d->Rt = (w2 >> 12) & 15;
        d->Rt2 = (w2 >> 8) & 15;
        break;
    case T_THUMB2_RM_REG:
        d->Rm = (w & 15);
        break;
    case T_THUMB2_RD_REG:
        d->Rd = (w2 >> 8) & 15;
        break;
    case T_THUMB2_RD_RM_REG:
        d->Rd = (w2 >> 8) & 15;
        d->Rm = w2 & 15;
        break;
    case T_THUMB2_RN_REG:
        d->Rn = w & 15;
        break;
    case T_THUMB2_RN_RT_REG:
        d->Rn = w & 15;
        d->Rt = (w2 >> 12) & 15;
        break;
    case T_THUMB2_RN_RT_RT2_REG:
        d->Rn = w & 15;
        d->Rt = (w2 >> 12) & 15;
        d->Rt2 = (w2 >> 8) & 15;
        break;
    case T_THUMB2_RN_RM_REG:
        d->Rn = w & 15;
        d->Rm = w2 & 15;
        break;
    case T_THUMB2_RN_RM_RT_REG:
        d->Rn = w & 15;
        d->Rm = w2 & 15;
        d->Rt = (w2 >> 12) & 15;
        break;
    case T_THUMB2_RN_RD_REG:
        d->Rn = w & 15;
        d->Rd = (w2 >> 8) & 15;
        break;
    case T_THUMB2_RN_RD_RT_REG:
        d->Rn = (w & 15);
        d->Rd = (w2 & 15);
        d->Rt = (w2 >> 12) & 15;
        break;
    case T_THUMB2_RN_RD_RT_RT2_REG:
        d->Rn = (w & 15);
        d->Rd = (w2 & 15);
        d->Rt = (w2 >> 12) & 15;
        d->Rt2 = (w2 >> 8) & 15;
        break;
    case T_THUMB2_RN_RD_RM_REG:
        d->Rn = w & 15;
        d->Rm = w2 & 15;
        d->Rd = (w2 >> 8) & 15;
        break;
    case T_THUMB2_RN_RD_RM_RA_REG:
        d->Rn = w & 15;
        d->Rm = w2 & 15;
        d->Rd = (w2 >> 8) & 15;
        d->Ra = (w2 >> 12) & 15;
        break;
    default:
        break;
    }
}

variable_types:
w
unsigned short


repo: jbremer/darm

decompiled_code:
__int64 __fastcall thumb2_parse_imm(_DWORD *a1, __int16 a2, unsigned int a3)
{
__int64 result; // rax
int v5; // edi
int v6; // edi

a1[18] = 1;
result = (unsigned int)(a1[3] - 68);
switch ( a1[3] )
{
case 'D':
a1[18] = 0;
break;
case 'E':
a1[28] = a3 & 0xFFF;
break;
case 'F':
a1[28] = (unsigned __int8)a3;
break;
case 'G':
v5 = ((unsigned __int16)a3 >> 4) & 3;
a1[28] = v5;
a1[32] = v5;
a1[30] = 0;
break;
case 'H':
result = ((unsigned __int16)a3 >> 10) & 0x1Cu | ((unsigned __int8)a3 >> 6);
a1[28] = result;
break;
case 'I':
if ( (a2 & 0x300) == 512 )
{
result = (a3 >> 4) & 0x700 | (unsigned __int8)a3 | (2 * a2) & 0x800;
}
else
{
v6 = (2 * a2) & 0x800 | (a3 >> 4) & 0x700 | (unsigned __int8)a3;
a1[28] = v6;
result = thumb_expand_imm(v6);
}
a1[28] = result;
break;
default:
return result;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(_DWORD *a1, __int16 a2, unsigned int a3)
{
__int64 result; // rax
int v5; // edi
int v6; // edi

a1[18] = 1;
result = (unsigned int)(a1[3] - 68);
switch ( a1[3] )
{
case 'D':
a1[18] = 0;
break;
case 'E':
a1[28] = a3 & 0xFFF;
break;
case 'F':
a1[28] = (unsigned __int8)a3;
break;
case 'G':
v5 = ((unsigned __int16)a3 >> 4) & 3;
a1[28] = v5;
a1[32] = v5;
a1[30] = 0;
break;
case 'H':
result = ((unsigned __int16)a3 >> 10) & 0x1Cu | ((unsigned __int8)a3 >> 6);
a1[28] = result;
break;
case 'I':
if ( (a2 & 0x300) == 512 )
{
result = (a3 >> 4) & 0x700 | (unsigned __int8)a3 | (2 * a2) & 0x800;
}
else
{
v6 = (2 * a2) & 0x800 | (a3 >> 4) & 0x700 | (unsigned __int8)a3;
a1[28] = v6;
result = func0(v6);
}
a1[28] = result;
break;
default:
return result;
}
return result;
}

original_code:
void thumb2_parse_imm(darm_t *d, uint16_t w, uint16_t w2)
{
    d->I = 1;
    switch (d->instr_imm_type) {
    case T_THUMB2_NO_IMM:
        d->I = 0;
        break;
    case T_THUMB2_IMM12:
        d->imm = w2 & 0xfff;
        break;
    case T_THUMB2_IMM8:
        d->imm = w2 & 0xff;
        break;
    case T_THUMB2_IMM2:
        d->imm = (w2 >> 4) & 3;
        d->shift = d->imm;
        d->shift_type = S_LSL;
        break;
    case T_THUMB2_IMM2_IMM3:
        d->imm = ((w2 >> 10) & 28) | ((w2 >> 6) & 3);
        break;
    case T_THUMB2_IMM1_IMM3_IMM8:
        if((w & 0x300) == 0x200) {
            d->imm = ((w & 0x400) << 1) | ((w2 & 0x7000) >> 4) | (w2 & 0xff);
        }
        else {
            d->imm = ((w & 0x400) << 1) | ((w2 & 0x7000) >> 4) | (w2 & 0xff);
            d->imm = thumb_expand_imm(d->imm);
        }
        break;
    default:
        break;
    }
}

canonical_original_code:
void thumb2_parse_imm(struct _darm_t *d, unsigned short w, unsigned short w2)
{
    d->I = 1;
    switch (d->instr_imm_type) {
    case T_THUMB2_NO_IMM:
        d->I = 0;
        break;
    case T_THUMB2_IMM12:
        d->imm = w2 & 0xfff;
        break;
    case T_THUMB2_IMM8:
        d->imm = w2 & 0xff;
        break;
    case T_THUMB2_IMM2:
        d->imm = (w2 >> 4) & 3;
        d->shift = d->imm;
        d->shift_type = S_LSL;
        break;
    case T_THUMB2_IMM2_IMM3:
        d->imm = ((w2 >> 10) & 28) | ((w2 >> 6) & 3);
        break;
    case T_THUMB2_IMM1_IMM3_IMM8:
        if((w & 0x300) == 0x200) {
            d->imm = ((w & 0x400) << 1) | ((w2 & 0x7000) >> 4) | (w2 & 0xff);
        }
        else {
            d->imm = ((w & 0x400) << 1) | ((w2 & 0x7000) >> 4) | (w2 & 0xff);
            d->imm = thumb_expand_imm(d->imm);
        }
        break;
    default:
        break;
    }
}

return_type: void

user_defined_types: struct _darm_t { unsigned int w; enum _darm_instr_t instr; enum _darm_enctype_t instr_type; enum _darm_enctype_t instr_imm_type; enum _darm_enctype_t instr_flag_type; enum _darm_cond_t cond; unsigned int B; unsigned int S; unsigned int E; unsigned int M; unsigned int N; enum _darm_option_t option; unsigned int U; unsigned int H; unsigned int P; unsigned int R; unsigned int T; unsigned int W; unsigned int I; unsigned int rotate; enum _darm_reg_t Rd; enum _darm_reg_t Rn; enum _darm_reg_t Rm; enum _darm_reg_t Ra; enum _darm_reg_t Rt; enum _darm_reg_t Rt2; enum _darm_reg_t RdHi; enum _darm_reg_t RdLo; unsigned int imm; unsigned int sat_imm; enum _darm_shift_type_t shift_type; enum _darm_reg_t Rs; unsigned int shift; unsigned int lsb; unsigned int msb; unsigned int width; unsigned short reglist; unsigned char coproc; unsigned char opc1; unsigned char opc2; enum _darm_reg_t CRd; enum _darm_reg_t CRn; enum _darm_reg_t CRm; unsigned int D; enum _darm_cond_t firstcond; unsigned char mask; }

user_defined_types: enum _darm_enctype_t { T_INVLD=0, T_ARM_ADR=1, T_ARM_UNCOND=2, T_ARM_MUL=3, T_ARM_STACK0=4, T_ARM_STACK1=5, T_ARM_STACK2=6, T_ARM_ARITH_SHIFT=7, T_ARM_ARITH_IMM=8, T_ARM_BITS=9, T_ARM_BRNCHSC=10, T_ARM_BRNCHMISC=11, T_ARM_MOV_IMM=12, T_ARM_CMP_OP=13, T_ARM_CMP_IMM=14, T_ARM_OPLESS=15, T_ARM_DST_SRC=16, T_ARM_LDSTREGS=17, T_ARM_BITREV=18, T_ARM_MISC=19, T_ARM_SM=20, T_ARM_PAS=21, T_ARM_SAT=22, T_ARM_SYNC=23, T_ARM_PUSR=24, T_ARM_MVCR=25, T_ARM_UDF=26, T_THUMB_ONLY_IMM8=27, T_THUMB_COND_BRANCH=28, T_THUMB_UNCOND_BRANCH=29, T_THUMB_SHIFT_IMM=30, T_THUMB_STACK=31, T_THUMB_LDR_PC=32, T_THUMB_GPI=33, T_THUMB_BRANCH_REG=34, T_THUMB_IT_HINTS=35, T_THUMB_HAS_IMM8=36, T_THUMB_EXTEND=37, T_THUMB_MOD_SP_IMM=38, T_THUMB_3REG=39, T_THUMB_2REG_IMM=40, T_THUMB_ADD_SP_IMM=41, T_THUMB_MOV4=42, T_THUMB_RW_MEMI=43, T_THUMB_RW_MEMO=44, T_THUMB_RW_REG=45, T_THUMB_REV=46, T_THUMB_SETEND=47, T_THUMB_PUSHPOP=48, T_THUMB_CMP=49, T_THUMB_MOD_SP_REG=50, T_THUMB_CBZ=51, T_THUMB2_NO_REG=52, T_THUMB2_RT_REG=53, T_THUMB2_RT_RT2_REG=54, T_THUMB2_RM_REG=55, T_THUMB2_RD_REG=56, T_THUMB2_RD_RM_REG=57, T_THUMB2_RN_REG=58, T_THUMB2_RN_RT_REG=59, T_THUMB2_RN_RT_RT2_REG=60, T_THUMB2_RN_RM_REG=61, T_THUMB2_RN_RM_RT_REG=62, T_THUMB2_RN_RD_REG=63, T_THUMB2_RN_RD_RT_REG=64, T_THUMB2_RN_RD_RT_RT2_REG=65, T_THUMB2_RN_RD_RM_REG=66, T_THUMB2_RN_RD_RM_RA_REG=67, T_THUMB2_NO_IMM=68, T_THUMB2_IMM12=69, T_THUMB2_IMM8=70, T_THUMB2_IMM2=71, T_THUMB2_IMM2_IMM3=72, T_THUMB2_IMM1_IMM3_IMM8=73, T_THUMB2_NO_FLAG=74, T_THUMB2_ROTATE_FLAG=75, T_THUMB2_U_FLAG=76, T_THUMB2_WUP_FLAG=77, T_THUMB2_TYPE_FLAG=78, T_THUMB2_REGLIST_FLAG=79, T_THUMB2_WP_REGLIST_FLAG=80, T_THUMB2_S_FLAG=81, T_THUMB2_S_TYPE_FLAG=82 }

user_defined_types: enum _darm_cond_t { C_EQ=0, C_NE=1, C_CS=2, C_CC=3, C_MI=4, C_PL=5, C_VS=6, C_VC=7, C_HI=8, C_LS=9, C_GE=10, C_LT=11, C_GT=12, C_LE=13, C_AL=14, C_HS=expr, C_LO=expr, C_UNCOND=15, C_INVLD=-1 }

user_defined_types: enum _darm_option_t { O_SY=15, O_ST=14, O_ISH=11, O_ISHST=10, O_NSH=7, O_NSHST=6, O_OSH=3, O_OSHST=2, O_INVLD=-1 }

user_defined_types: enum _darm_shift_type_t { S_LSL=0, S_LSR=1, S_ASR=2, S_ROR=3, S_INVLD=-1 }


decompiled_code:
__int64 __fastcall thumb2_parse_flag(__int64 a1, unsigned __int16 a2, unsigned __int16 a3)
{
__int64 result; // rax

result = (unsigned int)(*(_DWORD *)(a1 + 16) - 75);
switch ( *(_DWORD *)(a1 + 16) )
{
case 'K':
*(_DWORD *)(a1 + 76) = (a3 >> 1) & 0x18;
break;
case 'L':
*(_DWORD *)(a1 + 48) = (a2 >> 7) & 1;
break;
case 'M':
*(_DWORD *)(a1 + 68) = HIBYTE(a3) & 1;
result = (a3 >> 9) & 1;
*(_DWORD *)(a1 + 48) = result;
*(_DWORD *)(a1 + 56) = (a3 >> 10) & 1;
break;
case 'N':
result = thumb2_decode_immshift(a1, (a3 >> 4) & 3, *(unsigned __int8 *)(a1 + 112));
break;
case 'O':
*(_WORD *)(a1 + 144) = a3;
break;
case 'P':
*(_WORD *)(a1 + 144) = a3;
*(_DWORD *)(a1 + 68) = (a2 >> 5) & 1;
break;
case 'Q':
*(_DWORD *)(a1 + 28) = (a2 >> 4) & 1;
break;
case 'R':
*(_DWORD *)(a1 + 28) = (a2 >> 4) & 1;
result = thumb2_decode_immshift(a1, (a3 >> 4) & 3, *(unsigned __int8 *)(a1 + 112));
break;
default:
return result;
}
return result;
}

original_code:
void thumb2_parse_flag(darm_t *d, uint16_t w, uint16_t w2)
{
    switch (d->instr_flag_type) {
    case T_THUMB2_NO_FLAG:
        break;
    case T_THUMB2_ROTATE_FLAG:
        d->rotate = (w2 >> 1) & 24;
        break;
    case T_THUMB2_U_FLAG:
        d->U = (w >> 7) & 1 ? 1 : 0;
        break;
    case T_THUMB2_WUP_FLAG:
        d->W = (w2 >> 8) & 1 ? 1 : 0;
        d->U = (w2 >> 9) & 1 ? 1 : 0;
        d->P = (w2 >> 10) & 1 ? 1 : 0;
        break;
    case T_THUMB2_TYPE_FLAG:
        thumb2_decode_immshift(d, (w2 >> 4) & 3, d->imm);
        break;
    case T_THUMB2_REGLIST_FLAG:
        d->reglist = w2 & 0xffff;
        break;
    case T_THUMB2_WP_REGLIST_FLAG:
        d->reglist = w2 & 0xffff;
        d->W = (w >> 5) & 1 ? 1 : 0;
        break;
    case T_THUMB2_S_FLAG:
        d->S = (w >> 4) & 1 ? 1 : 0;
        break;
    case T_THUMB2_S_TYPE_FLAG:
        d->S = (w >> 4) & 1 ? 1 : 0;
        thumb2_decode_immshift(d, (w2 >> 4) & 3, d->imm);
        break;
    default:
        break;
    }
}

canonical_original_code:
void thumb2_parse_flag(struct _darm_t *d, unsigned short w, unsigned short w2)
{
    switch (d->instr_flag_type) {
    case T_THUMB2_NO_FLAG:
        break;
    case T_THUMB2_ROTATE_FLAG:
        d->rotate = (w2 >> 1) & 24;
        break;
    case T_THUMB2_U_FLAG:
        d->U = (w >> 7) & 1 ? 1 : 0;
        break;
    case T_THUMB2_WUP_FLAG:
        d->W = (w2 >> 8) & 1 ? 1 : 0;
        d->U = (w2 >> 9) & 1 ? 1 : 0;
        d->P = (w2 >> 10) & 1 ? 1 : 0;
        break;
    case T_THUMB2_TYPE_FLAG:
        thumb2_decode_immshift(d, (w2 >> 4) & 3, d->imm);
        break;
    case T_THUMB2_REGLIST_FLAG:
        d->reglist = w2 & 0xffff;
        break;
    case T_THUMB2_WP_REGLIST_FLAG:
        d->reglist = w2 & 0xffff;
        d->W = (w >> 5) & 1 ? 1 : 0;
        break;
    case T_THUMB2_S_FLAG:
        d->S = (w >> 4) & 1 ? 1 : 0;
        break;
    case T_THUMB2_S_TYPE_FLAG:
        d->S = (w >> 4) & 1 ? 1 : 0;
        thumb2_decode_immshift(d, (w2 >> 4) & 3, d->imm);
        break;
    default:
        break;
    }
}


decompiled_code:
__int64 __fastcall thumb2_disasm(_DWORD *a1, unsigned __int16 a2, unsigned __int16 a3)
{
unsigned int v3; // er12
int v4; // eax

v3 = a3;
v4 = thumb2_decode_instruction(a1, a2, a3);
a1[1] = v4;
if ( !v4 )
return 0xFFFFFFFFLL;
thumb2_parse_reg(a1, a2, v3);
thumb2_parse_imm(a1, a2, v3);
thumb2_parse_flag((__int64)a1, a2, v3);
thumb2_parse_misc((__int64)a1, a2, v3);
a1[2] = 0;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(_DWORD *a1, unsigned __int16 a2, unsigned __int16 a3)
{
unsigned int v3; // er12
int v4; // eax

v3 = a3;
v4 = thumb2_decode_instruction(a1, a2, a3);
a1[1] = v4;
if ( !v4 )
return 0xFFFFFFFFLL;
func2(a1, a2, v3);
func3(a1, a2, v3);
func4((__int64)a1, a2, v3);
func5((__int64)a1, a2, v3);
a1[2] = 0;
return 0LL;
}

original_code:
static int thumb2_disasm(darm_t *d, uint16_t w, uint16_t w2)
{
    d->instr = thumb2_decode_instruction(d, w, w2);
    if(d->instr == I_INVLD) {
        return -1;
    }
    thumb2_parse_reg(d, w, w2);
    thumb2_parse_imm(d, w, w2);
    thumb2_parse_flag(d, w, w2);
    thumb2_parse_misc(d, w, w2);
    d->instr_type = T_INVLD;
    return 0;
}

canonical_original_code:
static int thumb2_disasm(struct _darm_t *d, unsigned short w, unsigned short w2)
{
    d->instr = thumb2_decode_instruction(d, w, w2);
    if(d->instr == I_INVLD) {
        return -1;
    }
    thumb2_parse_reg(d, w, w2);
    thumb2_parse_imm(d, w, w2);
    thumb2_parse_flag(d, w, w2);
    thumb2_parse_misc(d, w, w2);
    d->instr_type = T_INVLD;
    return 0;
}

return_type: int


decompiled_code:
char *__fastcall darm_thumb2_str(_DWORD *a1)
{
__int64 v1; // rax
int v2; // edx
int v3; // eax
int v4; // ebp
__int64 v5; // rdx
__int64 v6; // rdx
__int64 v7; // rdx

v1 = 0LL;
while ( 1 )
{
v2 = v1;
if ( a1[1] == thumb2_instr_labels[v1] )
break;
if ( ++v1 == 220 )
{
v2 = 0;
break;
}
}
v3 = sprintf(stringbuf_0, "%s", *((const char **)&thumb2_instruction_strings + v2));
v4 = v3;
v5 = (unsigned int)a1[20];
if ( (_DWORD)v5 != -1 )
v4 = sprintf(&stringbuf_0[v3], "rd%i,", v5) + v3;
if ( a1[24] != -1 )
v4 += sprintf(&stringbuf_0[v4], "rt%i,", (unsigned int)a1[25]);
if ( a1[25] != -1 )
v4 += sprintf(&stringbuf_0[v4], "rt2%i,", (unsigned int)a1[24]);
v6 = (unsigned int)a1[21];
if ( (_DWORD)v6 != -1 )
v4 += sprintf(&stringbuf_0[v4], "rn%i,", v6);
v7 = (unsigned int)a1[22];
if ( (_DWORD)v7 != -1 )
v4 += sprintf(&stringbuf_0[v4], "rm%i ", v7);
if ( a1[18] == 1 )
sprintf(&stringbuf_0[v4], "#0x%x", (unsigned int)a1[28]);
return stringbuf_0;
}

canonical_decompiled_code:
char *__fastcall func7(_DWORD *a1)
{
__int64 v1; // rax
int v2; // edx
int v3; // eax
int v4; // ebp
__int64 v5; // rdx
__int64 v6; // rdx
__int64 v7; // rdx

v1 = 0LL;
while ( 1 )
{
v2 = v1;
if ( a1[1] == thumb2_instr_labels[v1] )
break;
if ( ++v1 == 220 )
{
v2 = 0;
break;
}
}
v3 = sprintf(stringbuf_0, "%s", *((const char **)&thumb2_instruction_strings + v2));
v4 = v3;
v5 = (unsigned int)a1[20];
if ( (_DWORD)v5 != -1 )
v4 = sprintf(&stringbuf_0[v3], "rd%i,", v5) + v3;
if ( a1[24] != -1 )
v4 += sprintf(&stringbuf_0[v4], "rt%i,", (unsigned int)a1[25]);
if ( a1[25] != -1 )
v4 += sprintf(&stringbuf_0[v4], "rt2%i,", (unsigned int)a1[24]);
v6 = (unsigned int)a1[21];
if ( (_DWORD)v6 != -1 )
v4 += sprintf(&stringbuf_0[v4], "rn%i,", v6);
v7 = (unsigned int)a1[22];
if ( (_DWORD)v7 != -1 )
v4 += sprintf(&stringbuf_0[v4], "rm%i ", v7);
if ( a1[18] == 1 )
sprintf(&stringbuf_0[v4], "#0x%x", (unsigned int)a1[28]);
return stringbuf_0;
}

original_code:
char *darm_thumb2_str(darm_t *d)
{
    int index=0, offset=0;
    static char stringbuf[512];
    for (int i = 0; i < 220; i++) {
        if(d->instr == thumb2_instr_labels[i]) {
            index = i;
            break;
        }
    }
    offset += sprintf(stringbuf + offset, "%s",
        thumb2_instruction_strings[index]);
    if(d->Rd != R_INVLD) {
        offset += sprintf(stringbuf+offset, "rd%i,", d->Rd);
    }
    if(d->Rt != R_INVLD) {
        offset += sprintf(stringbuf+offset, "rt%i,", d->Rt2);
    }
    if(d->Rt2 != R_INVLD) {
        offset += sprintf(stringbuf+offset, "rt2%i,", d->Rt);
    }
    if(d->Rn != R_INVLD) {
        offset += sprintf(stringbuf+offset, "rn%i,", d->Rn);
    }
    if(d->Rm != R_INVLD) {
        offset += sprintf(stringbuf+offset, "rm%i ", d->Rm);
    }
    if(d->I == 1) {
        offset += sprintf(stringbuf+offset, "#0x%x", d->imm);
    }
    return stringbuf;
}

canonical_original_code:
char *darm_thumb2_str(struct _darm_t *d)
{
    int index = 0; int offset = 0;
    static char stringbuf[512];
    for (int i = 0; i < 220; i++) {
        if(d->instr == thumb2_instr_labels[i]) {
            index = i;
            break;
        }
    }
    offset += sprintf(stringbuf + offset, "%s",
        thumb2_instruction_strings[index]);
    if(d->Rd != R_INVLD) {
        offset += sprintf(stringbuf+offset, "rd%i,", d->Rd);
    }
    if(d->Rt != R_INVLD) {
        offset += sprintf(stringbuf+offset, "rt%i,", d->Rt2);
    }
    if(d->Rt2 != R_INVLD) {
        offset += sprintf(stringbuf+offset, "rt2%i,", d->Rt);
    }
    if(d->Rn != R_INVLD) {
        offset += sprintf(stringbuf+offset, "rn%i,", d->Rn);
    }
    if(d->Rm != R_INVLD) {
        offset += sprintf(stringbuf+offset, "rm%i ", d->Rm);
    }
    if(d->I == 1) {
        offset += sprintf(stringbuf+offset, "#0x%x", d->imm);
    }
    return stringbuf;
}

variable_types: index int

variable_types: offset int

variable_types:
stringbuf
char [512]

variable_types: i int

return_type: char *


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

darm_init();
*a1 = a3 | (a2 << 16);
a1[5] = 14;
if ( (unsigned int)((unsigned __int16)a2 >> 11) - 29 > 2 )
result = 0xFFFFFFFFLL;
else
result = thumb2_disasm(a1, a2, a3);
return result;
}

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

darm_init();
*a1 = a3 | (a2 << 16);
a1[5] = 14;
if ( (unsigned int)((unsigned __int16)a2 >> 11) - 29 > 2 )
result = 0xFFFFFFFFLL;
else
result = func6(a1, a2, a3);
return result;
}

original_code:
int darm_thumb2_disasm(darm_t *d, uint16_t w, uint16_t w2)
{
    darm_init(d);
    d->w = (w << 16) | w2;
    d->cond = C_AL;
    switch (w >> 11) {
    case 29: case 30: case 31:
        return thumb2_disasm(d, w, w2);
    default:
        return -1;
    }
}

canonical_original_code:
int darm_thumb2_disasm(struct _darm_t *d, unsigned short w, unsigned short w2)
{
    darm_init(d);
    d->w = (w << 16) | w2;
    d->cond = C_AL;
    switch (w >> 11) {
    case 29: case 30: case 31:
        return thumb2_disasm(d, w, w2);
    default:
        return -1;
    }
}


original_code:
static int thumb_disasm(darm_t *d, uint16_t w)
{
    d->instr = thumb_instr_labels[w >> 8];
    d->instr_type = thumb_instr_types[w >> 8];
    switch ((uint32_t) d->instr_type) {
    case T_THUMB_ONLY_IMM8:
        d->I = 1;
        d->imm = w & ((1 << 8) - 1);
        return 0;
    case T_THUMB_COND_BRANCH:
        d->cond = (w >> 8) & 15;
        d->I = 1;
        d->imm = (uint32_t)(int8_t)(w & ((1 << 8) - 1)) << 1;
        return 0;
    case T_THUMB_UNCOND_BRANCH:
        d->I = 1;
        d->imm = w & ((1 << 11) - 1);
        if(((d->imm >> 10) & 1) != 0) {
            d->imm |= ~((1 << 11) - 1);
        }
        d->imm <<= 1;
        return 0;
    case T_THUMB_SHIFT_IMM:
        d->Rd = (w >> 0) & 7;
        d->Rm = (w >> 3) & 7;
        d->shift = (w >> 6) & 31;
        if(d->shift == 0 && d->instr == I_LSL) {
            d->instr = I_MOV;
        }
        else {
            switch ((uint32_t) d->instr) {
            case I_ASR:
                d->shift_type = S_ASR;
                break;
            case I_LSL:
                d->shift_type = S_LSL;
                break;
            case I_LSR:
                d->shift_type = S_LSR;
                break;
            }
        }
        return 0;
    case T_THUMB_STACK:
        d->I = 1;
        d->imm = (w & ((1 << 8) - 1)) << 2;
        d->Rn = SP;
        d->Rt = (w >> 8) & 7;
        d->U = 1;
        d->W = 0;
        d->P = 1;
        return 0;
    case T_THUMB_LDR_PC:
        d->I = 1;
        d->imm = (w & ((1 << 8) - 1)) << 2;
        d->Rn = PC;
        d->Rt = (w >> 8) & 7;
        d->U = 1;
        d->W = 0;
        d->P = 1;
        return 0;
    case T_THUMB_GPI:
        d->instr = type_gpi_instr_lookup[(w >> 6) & 15];
        switch ((uint32_t) d->instr) {
        case I_AND: case I_EOR: case I_LSL: case I_LSR:
        case I_ASR: case I_ADC: case I_SBC: case I_ROR:
            d->Rd = d->Rn = w & 7;
            d->Rm = (w >> 3) & 7;
            return 0;
        case I_TST: case I_CMP: case I_CMN:
            d->Rn = w & 7;
            d->Rm = (w >> 3) & 7;
            return 0;
        case I_RSB:
            d->I = 1;
            d->imm = 0;
            d->Rd = w & 7;
            d->Rn = (w >> 3) & 7;
            return 0;
        case I_ORR: case I_BIC:
            d->Rn = w & 7;
        case I_MVN:
            d->Rd = w & 7;
            d->Rm = (w >> 3) & 7;
            return 0;
        case I_MUL:
            d->Rd = d->Rm = w & 7;
            d->Rn = (w >> 3) & 7;
            return 0;
        }
    case T_THUMB_BRANCH_REG:
        d->instr = (w >> 7) & 1 ? I_BLX : I_BX;
        d->Rm = (w >> 3) & 15;
        return 0;
    case T_THUMB_IT_HINTS:
        if((w & 15) == 0) {
            d->instr = type_hints_instr_lookup[(w >> 4) & 7];
            return d->instr == I_INVLD ? -1 : 0;
        }
        d->instr = I_IT;
        d->mask = w & 15;
        d->firstcond = (w >> 4) & 15;
        return 0;
    case T_THUMB_HAS_IMM8:
        d->I = 1;
        d->imm = w & ((1 << 8) - 1);
        switch ((uint32_t) d->instr) {
        case I_ADD: case I_SUB:
            d->Rd = d->Rn = (w >> 8) & 7;
            return 0;
        case I_ADR:
            d->Rn = PC;
            d->U = 1;
            d->imm <<= 2;
        case I_MOV:
            d->Rd = (w >> 8) & 7;
            return 0;
        case I_CMP:
            d->Rn = (w >> 8) & 7;
            return 0;
        }
    case T_THUMB_EXTEND:
        d->instr = type_extend_instr_lookup[(w >> 6) & 3];
        d->Rd = w & 7;
        d->Rm = (w >> 3) & 7;
        return 0;
    case T_THUMB_MOD_SP_IMM:
        d->instr = (w >> 7) & 1 ? I_SUB : I_ADD;
        d->Rd = d->Rn = SP;
        d->I = 1;
        d->imm = (w & 0x7f) << 2;
        return 0;
    case T_THUMB_3REG:
        d->Rd = (w >> 0) & 7;
        d->Rn = (w >> 3) & 7;
        d->Rm = (w >> 6) & 7;
        return 0;
    case T_THUMB_2REG_IMM:
        d->Rd = w & 7;
        d->Rn = (w >> 3) & 7;
        d->I = 1;
        d->imm = (w >> 6) & 7;
        return 0;
    case T_THUMB_ADD_SP_IMM:
        d->I = 1;
        d->imm = (w & ((1 << 8) - 1)) << 2;
        d->Rn = SP;
        d->Rd = (w >> 8) & 7;
        return 0;
    case T_THUMB_MOV4:
        d->Rd = ((w >> 4) & 8) | (w & 7);
        d->Rm = (w >> 3) & 15;
        return 0;
    case T_THUMB_RW_MEMI:
        d->Rt = w & 7;
        d->Rn = (w >> 3) & 7;
        d->I = 1;
        d->imm = (w >> 6) & 31;
        switch ((uint32_t) d->instr) {
        case I_LDR: case I_STR:
            d->imm <<= 2;
            break;
        case I_LDRH: case I_STRH:
            d->imm <<= 1;
            break;
        }
        d->P = 1;
        d->U = 1;
        d->W = 0;
        return 0;
    case T_THUMB_RW_MEMO:
        d->Rt = (w >> 0) & 7;
        d->Rn = (w >> 3) & 7;
        d->Rm = (w >> 6) & 7;
        d->P = 1;
        d->U = 1;
        d->W = 0;
        return 0;
    case T_THUMB_RW_REG:
        d->reglist = w & ((1 << 8) - 1);
        d->Rn = (w >> 8) & 7;
        return 0;
    case T_THUMB_REV:
        d->instr = type_rev_instr_lookup[(w >> 6) & 3];
        if(d->instr == I_INVLD) return -1;
        d->Rd = (w >> 0) & 7;
        d->Rm = (w >> 3) & 7;
        return 0;
    case T_THUMB_SETEND:
        d->E = (w >> 4) & 1;
        return 0;
    case T_THUMB_PUSHPOP:
        d->reglist = w & ((1 << 8) - 1);
        if(d->instr == I_PUSH) {
            d->reglist |= ((w >> 8) & 1) << LR;
        }
        else {
            d->reglist |= ((w >> 8) & 1) << PC;
        }
        return 0;
    case T_THUMB_CMP:
        d->Rn = (w & 7) | ((w >> 4) & 8);
        d->Rm = (w >> 3) & 15;
        return 0;
    case T_THUMB_MOD_SP_REG:
        d->Rd = d->Rn = ((w >> 4) & 8) | (w & 7);
        d->Rm = (w >> 3) & 15;
        if(d->Rm == SP) {
            d->Rd = d->Rm = d->Rn;
            d->Rn = SP;
        }
        return 0;
    case T_THUMB_CBZ:
        d->instr = (w >> 11) & 1 ? I_CBNZ : I_CBZ;
        d->Rn = w & 7;
        d->Rm = PC;
        d->U = 1;
        d->I = 1;
        d->imm = ((w >> 2) & (31 << 1)) | ((w >> 3) & (1 << 6));
        return 0;
    }
    return -1;
}

canonical_original_code:
static int thumb_disasm(struct _darm_t *d, unsigned short w)
{
    d->instr = thumb_instr_labels[w >> 8];
    d->instr_type = thumb_instr_types[w >> 8];
    switch ((unsigned int) d->instr_type) {
    case T_THUMB_ONLY_IMM8:
        d->I = 1;
        d->imm = w & ((1 << 8) - 1);
        return 0;
    case T_THUMB_COND_BRANCH:
        d->cond = (w >> 8) & 15;
        d->I = 1;
        d->imm = (unsigned int)(char)(w & ((1 << 8) - 1)) << 1;
        return 0;
    case T_THUMB_UNCOND_BRANCH:
        d->I = 1;
        d->imm = w & ((1 << 11) - 1);
        if(((d->imm >> 10) & 1) != 0) {
            d->imm |= ~((1 << 11) - 1);
        }
        d->imm <<= 1;
        return 0;
    case T_THUMB_SHIFT_IMM:
        d->Rd = (w >> 0) & 7;
        d->Rm = (w >> 3) & 7;
        d->shift = (w >> 6) & 31;
        if(d->shift == 0 && d->instr == I_LSL) {
            d->instr = I_MOV;
        }
        else {
            switch ((unsigned int) d->instr) {
            case I_ASR:
                d->shift_type = S_ASR;
                break;
            case I_LSL:
                d->shift_type = S_LSL;
                break;
            case I_LSR:
                d->shift_type = S_LSR;
                break;
            }
        }
        return 0;
    case T_THUMB_STACK:
        d->I = 1;
        d->imm = (w & ((1 << 8) - 1)) << 2;
        d->Rn = SP;
        d->Rt = (w >> 8) & 7;
        d->U = 1;
        d->W = 0;
        d->P = 1;
        return 0;
    case T_THUMB_LDR_PC:
        d->I = 1;
        d->imm = (w & ((1 << 8) - 1)) << 2;
        d->Rn = PC;
        d->Rt = (w >> 8) & 7;
        d->U = 1;
        d->W = 0;
        d->P = 1;
        return 0;
    case T_THUMB_GPI:
        d->instr = type_gpi_instr_lookup[(w >> 6) & 15];
        switch ((unsigned int) d->instr) {
        case I_AND: case I_EOR: case I_LSL: case I_LSR:
        case I_ASR: case I_ADC: case I_SBC: case I_ROR:
            d->Rd = d->Rn = w & 7;
            d->Rm = (w >> 3) & 7;
            return 0;
        case I_TST: case I_CMP: case I_CMN:
            d->Rn = w & 7;
            d->Rm = (w >> 3) & 7;
            return 0;
        case I_RSB:
            d->I = 1;
            d->imm = 0;
            d->Rd = w & 7;
            d->Rn = (w >> 3) & 7;
            return 0;
        case I_ORR: case I_BIC:
            d->Rn = w & 7;
        case I_MVN:
            d->Rd = w & 7;
            d->Rm = (w >> 3) & 7;
            return 0;
        case I_MUL:
            d->Rd = d->Rm = w & 7;
            d->Rn = (w >> 3) & 7;
            return 0;
        }
    case T_THUMB_BRANCH_REG:
        d->instr = (w >> 7) & 1 ? I_BLX : I_BX;
        d->Rm = (w >> 3) & 15;
        return 0;
    case T_THUMB_IT_HINTS:
        if((w & 15) == 0) {
            d->instr = type_hints_instr_lookup[(w >> 4) & 7];
            return d->instr == I_INVLD ? -1 : 0;
        }
        d->instr = I_IT;
        d->mask = w & 15;
        d->firstcond = (w >> 4) & 15;
        return 0;
    case T_THUMB_HAS_IMM8:
        d->I = 1;
        d->imm = w & ((1 << 8) - 1);
        switch ((unsigned int) d->instr) {
        case I_ADD: case I_SUB:
            d->Rd = d->Rn = (w >> 8) & 7;
            return 0;
        case I_ADR:
            d->Rn = PC;
            d->U = 1;
            d->imm <<= 2;
        case I_MOV:
            d->Rd = (w >> 8) & 7;
            return 0;
        case I_CMP:
            d->Rn = (w >> 8) & 7;
            return 0;
        }
    case T_THUMB_EXTEND:
        d->instr = type_extend_instr_lookup[(w >> 6) & 3];
        d->Rd = w & 7;
        d->Rm = (w >> 3) & 7;
        return 0;
    case T_THUMB_MOD_SP_IMM:
        d->instr = (w >> 7) & 1 ? I_SUB : I_ADD;
        d->Rd = d->Rn = SP;
        d->I = 1;
        d->imm = (w & 0x7f) << 2;
        return 0;
    case T_THUMB_3REG:
        d->Rd = (w >> 0) & 7;
        d->Rn = (w >> 3) & 7;
        d->Rm = (w >> 6) & 7;
        return 0;
    case T_THUMB_2REG_IMM:
        d->Rd = w & 7;
        d->Rn = (w >> 3) & 7;
        d->I = 1;
        d->imm = (w >> 6) & 7;
        return 0;
    case T_THUMB_ADD_SP_IMM:
        d->I = 1;
        d->imm = (w & ((1 << 8) - 1)) << 2;
        d->Rn = SP;
        d->Rd = (w >> 8) & 7;
        return 0;
    case T_THUMB_MOV4:
        d->Rd = ((w >> 4) & 8) | (w & 7);
        d->Rm = (w >> 3) & 15;
        return 0;
    case T_THUMB_RW_MEMI:
        d->Rt = w & 7;
        d->Rn = (w >> 3) & 7;
        d->I = 1;
        d->imm = (w >> 6) & 31;
        switch ((unsigned int) d->instr) {
        case I_LDR: case I_STR:
            d->imm <<= 2;
            break;
        case I_LDRH: case I_STRH:
            d->imm <<= 1;
            break;
        }
        d->P = 1;
        d->U = 1;
        d->W = 0;
        return 0;
    case T_THUMB_RW_MEMO:
        d->Rt = (w >> 0) & 7;
        d->Rn = (w >> 3) & 7;
        d->Rm = (w >> 6) & 7;
        d->P = 1;
        d->U = 1;
        d->W = 0;
        return 0;
    case T_THUMB_RW_REG:
        d->reglist = w & ((1 << 8) - 1);
        d->Rn = (w >> 8) & 7;
        return 0;
    case T_THUMB_REV:
        d->instr = type_rev_instr_lookup[(w >> 6) & 3];
        if(d->instr == I_INVLD) return -1;
        d->Rd = (w >> 0) & 7;
        d->Rm = (w >> 3) & 7;
        return 0;
    case T_THUMB_SETEND:
        d->E = (w >> 4) & 1;
        return 0;
    case T_THUMB_PUSHPOP:
        d->reglist = w & ((1 << 8) - 1);
        if(d->instr == I_PUSH) {
            d->reglist |= ((w >> 8) & 1) << LR;
        }
        else {
            d->reglist |= ((w >> 8) & 1) << PC;
        }
        return 0;
    case T_THUMB_CMP:
        d->Rn = (w & 7) | ((w >> 4) & 8);
        d->Rm = (w >> 3) & 15;
        return 0;
    case T_THUMB_MOD_SP_REG:
        d->Rd = d->Rn = ((w >> 4) & 8) | (w & 7);
        d->Rm = (w >> 3) & 15;
        if(d->Rm == SP) {
            d->Rd = d->Rm = d->Rn;
            d->Rn = SP;
        }
        return 0;
    case T_THUMB_CBZ:
        d->instr = (w >> 11) & 1 ? I_CBNZ : I_CBZ;
        d->Rn = w & 7;
        d->Rm = PC;
        d->U = 1;
        d->I = 1;
        d->imm = ((w >> 2) & (31 << 1)) | ((w >> 3) & (1 << 6));
        return 0;
    }
    return -1;
}


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

darm_init();
*a1 = a2;
a1[5] = 14;
if ( (unsigned int)(a2 >> 11) - 29 <= 2 )
result = 0xFFFFFFFFLL;
else
result = thumb_disasm((__int64)a1, a2);
return result;
}

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

darm_init();
*a1 = a2;
a1[5] = 14;
if ( (unsigned int)(a2 >> 11) - 29 <= 2 )
result = 0xFFFFFFFFLL;
else
result = func0((__int64)a1, a2);
return result;
}

original_code:
int darm_thumb_disasm(darm_t *d, uint16_t w)
{
    darm_init(d);
    d->w = w;
    d->cond = C_AL;
    switch (w >> 11) {
    case 29: case 30: case 31:
        return -1;
    default:
        return thumb_disasm(d, w);
    }
}

canonical_original_code:
int darm_thumb_disasm(struct _darm_t *d, unsigned short w)
{
    darm_init(d);
    d->w = w;
    d->cond = C_AL;
    switch (w >> 11) {
    case 29: case 30: case 31:
        return -1;
    default:
        return thumb_disasm(d, w);
    }
}


main

canonical_name: main

repo: jbruchon/alterego

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v3; // rax
const char *v4; // rbp
__uid_t v5; // eax
int v6; // ebx
char v7[4120]; // [rsp+0h] [rbp-1018h] BYREF

if ( !strcmp(__xpg_basename(v7), "alterego") )
{
if ( argc <= 1 )
{
fwrite("No parameters specified.\n", 1uLL, 0x19uLL, _bss_start);
v6 = 255;
goto LABEL_19;
}
v3 = argv[1];
if ( *v3 == 45 )
{
if ( v3[1] != 114 )
{
fwrite("You must specify a valid option after the '-'\n", 1uLL, 0x2EuLL, _bss_start);
v6 = 255;
goto LABEL_19;
}
if ( argc <= 3 )
{
fwrite("You must specify the root after the -r switch\n", 1uLL, 0x2EuLL, _bss_start);
v6 = 255;
goto LABEL_19;
}
strncpy(rootdir_0, argv[2], 0x1000uLL);
}
}
v4 = *argv;
if ( strchr(*argv, 47) )
{
getcwd(v7, 0x1000uLL);
*(_WORD *)&v7[strlen(v7)] = 47;
strcat(v7, *argv);
}
else
{
strcpy(v7, v4);
}
if ( chdir(rootdir_0) )
{
fprintf(_bss_start, "Change directory to %s failed.\n", rootdir_0);
v6 = 1;
}
else if ( chroot(rootdir_0) )
{
fprintf(_bss_start, "Change root to %s failed.\n", rootdir_0);
v6 = 2;
}
else
{
v5 = getuid();
if ( seteuid(v5) )
{
fwrite("seteuid() to the current user ID failed.\n", 1uLL, 0x29uLL, _bss_start);
v6 = 3;
}
else if ( execvp(v7, (char *const *)argv) )
{
fprintf(_bss_start, "Could not execute %s in %s\n", v7, rootdir_0);
v6 = 4;
}
else
{
fwrite("This message indicated a bug, please report it.\n", 1uLL, 0x30uLL, _bss_start);
v6 = 128;
}
}
LABEL_19:
printf("\nAlterEgo version %s by Jody Lee Bruchon <jody@jodybruchon.com>\n", "1.2");
printf("This copy uses the following default root path: %s\n\n", "/i386");
puts(
"This program attempts to execute a program with the same name\n"
"as itself under a hard-coded chroot, for example to run a 32-bit\n"
"userland on a 64-bit machine. It was written to allow seamless\n"
"mixing of two potentially incompatible userlands.\n"
"\n"
"For example, if the machine is x86_64 and you create a complete\n"
"i386 system under /i386, and you install a 32-bit Firefox under\n"
"/i386/usr/bin/firefox, any x86_64 program that attempts to run\n"
"/usr/bin/firefox (or just firefox) will run the 32-bit Firefox\n"
"transparently with no extra configuration. Simply symlink to\n"
"AlterEgo, i.e. ln -s /usr/bin/alterego /usr/bin/firefox\n");
exit(v6);
}

original_code:
int main(int argc, char **argv) {
 char prog[4096];
 static char rootdir[4096] = "/i386";
 char *p;
 int status = 0;
 if (strncmp(__xpg_basename(prog), "alterego", 4096) == 0) {
  if (argc < 2) {
   fprintf(stderr, "No parameters specified.\n");
   status = 255; goto usage;
  }
  if (*argv[1] == '-') {
   p = argv[1];
   p++;
   switch (*p) {
   default:
   case '\0':
    fprintf(stderr, "You must specify a valid option after the '-'\n");
    status = 255; goto usage;
   case 'r':
    if (argc < 4) {
     fprintf(stderr, "You must specify the root after the -r switch\n");
     status = 255; goto usage;
    }
    strncpy(rootdir, argv[2], 4096);
    break;
   }
  }
 }
 if (strchr(argv[0], '/') != ((void *)0)) {
  getcwd(prog, 4096);
  strcat(prog, "/");
  strcat(prog, argv[0]);
 } else {
  strcpy(prog, argv[0]);
 }
 if (chdir(rootdir)) {
  fprintf(stderr, "Change directory to %s failed.\n", rootdir);
  status = 1; goto usage;
 }
 if (chroot(rootdir)) {
  fprintf(stderr, "Change root to %s failed.\n", rootdir);
  status = 2; goto usage;
 }
 if (seteuid(getuid())) {
  fprintf(stderr, "seteuid() to the current user ID failed.\n");
  status = 3; goto usage;
 }
 if (execvp(prog, argv)) {
  fprintf(stderr, "Could not execute %s in %s\n", prog, rootdir);
  status = 4; goto usage;
 }
 fprintf(stderr, "This message indicated a bug, please report it.\n");
 status = 128;
usage:
 printf("\nAlterEgo version %s by Jody Lee Bruchon <jody@jodybruchon.com>\n", program_version);
 printf("This copy uses the following default root path: %s\n\n", "/i386");
 printf(
 "This program attempts to execute a program with the same name\n"
 "as itself under a hard-coded chroot, for example to run a 32-bit\n"
 "userland on a 64-bit machine. It was written to allow seamless\n"
 "mixing of two potentially incompatible userlands.\n\n"
 "For example, if the machine is x86_64 and you create a complete\n"
 "i386 system under /i386, and you install a 32-bit Firefox under\n"
 "/i386/usr/bin/firefox, any x86_64 program that attempts to run\n"
 "/usr/bin/firefox (or just firefox) will run the 32-bit Firefox\n"
 "transparently with no extra configuration. Simply symlink to\n"
 "AlterEgo, i.e. ln -s /usr/bin/alterego /usr/bin/firefox\n\n"
 );
 exit(status);
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
prog
char [4096]

variable_types:
rootdir
char [4096]

variable_types:
p
char *

variable_types: status int


unique

decompiled_code:
__int64 __fastcall unique(char *s)
{
int v1; // eax
char *v2; // rdx
__int64 v3; // rcx
int v5[258]; // [rsp+0h] [rbp-408h] BYREF

memset(v5, 0, 0x400uLL);
v1 = strlen(s);
if ( v1 <= 0 )
return 1LL;
v2 = s;
while ( 1 )
{
v3 = *v2;
if ( v5[v3] == 1 )
break;
v5[v3] = 1;
if ( ++v2 == &s[v1 - 1 + 1] )
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(char *s)
{
int v1; // eax
char *v2; // rdx
__int64 v3; // rcx
int v5[258]; // [rsp+0h] [rbp-408h] BYREF

memset(v5, 0, 0x400uLL);
v1 = strlen(s);
if ( v1 <= 0 )
return 1LL;
v2 = s;
while ( 1 )
{
v3 = *v2;
if ( v5[v3] == 1 )
break;
v5[v3] = 1;
if ( ++v2 == &s[v1 - 1 + 1] )
return 1LL;
}
return 0LL;
}

original_code:
int unique(char str[]) {
  int characters[256] = {0};
  int length = strlen(str);
  int i;
  for(i = 0; i < length; i++) {
    int c = str[i];
    if(characters[c] == 1)
      return 0;
    else
      characters[c] = 1;
  }
  return 1;
}

variable_types:
str
char []

variable_types:
characters
int [256]

variable_types: length int

variable_types: c int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( (unsigned int)unique((char *)argv[1]) )
puts("Characters are unique");
else
puts("Characters are NOT unique");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( (unsigned int)func7((char *)argv[1]) )
puts("Characters are unique");
else
puts("Characters are NOT unique");
return 0;
}

original_code:
int main(int argc, void *argv[]) {
  int result = unique(argv[1]);
  if(result)
    printf("Characters are unique\n");
  else
    printf("Characters are NOT unique\n");
  return 0;
}

variable_types:
argv
void *[]

variable_types: result int


variable_types:
matcher
char *

variable_types:
type
int *

variable_types:
param
char **

variable_types:
s
int *

variable_types:
e
char *


original_code:
int enum_fields(const char *fv, const char **m, int *s) {
 const char *f, *b;
 enum state {
  start,
  skip_space,
  scan,
  scan_quote,
  scan_back,
  done,
  end
 } cur_state = start;
 do {
  switch (cur_state) {
   case start:
    f = fv;
    cur_state = skip_space;
    break;
   case skip_space:
    if (*f == '\0')
     cur_state = end;
    else if (*f == ' ' || *f == ',')
     f++;
    else if (*f == '"') {
     b = f;
     cur_state = scan_quote;
    } else {
     b = f;
     cur_state = scan;
    }
    break;
   case scan:
    b++;
    if (*b == '\0')
     cur_state = scan_back;
    else if (*b == '"')
     cur_state = scan_quote;
    else if (*b == ',')
     cur_state = scan_back;
    break;
   case scan_quote:
    b++;
    if (*b == '\0')
     cur_state = scan_back;
    else if (*b == '\\' && b[1] != '\0')
     b++;
    else if (*b == '"')
     cur_state = scan;
    break;
   case scan_back:
    b--;
    if (*b != ' ')
     cur_state = done;
    break;
   case done:
    *m = f;
    *s = (int)(b-f)+1;
    return b - fv + 1;
    break;
  }
 } while (cur_state != end);
 return 0;
}

canonical_original_code:
int enum_fields(char *fv, char **m, int *s) {
 char *f; char *b;
 enum state cur_state = start;
 do {
  switch (cur_state) {
   case start:
    f = fv;
    cur_state = skip_space;
    break;
   case skip_space:
    if (*f == '\0')
     cur_state = end;
    else if (*f == ' ' || *f == ',')
     f++;
    else if (*f == '"') {
     b = f;
     cur_state = scan_quote;
    } else {
     b = f;
     cur_state = scan;
    }
    break;
   case scan:
    b++;
    if (*b == '\0')
     cur_state = scan_back;
    else if (*b == '"')
     cur_state = scan_quote;
    else if (*b == ',')
     cur_state = scan_back;
    break;
   case scan_quote:
    b++;
    if (*b == '\0')
     cur_state = scan_back;
    else if (*b == '\\' && b[1] != '\0')
     b++;
    else if (*b == '"')
     cur_state = scan;
    break;
   case scan_back:
    b--;
    if (*b != ' ')
     cur_state = done;
    break;
   case done:
    *m = f;
    *s = (int)(b-f)+1;
    return b - fv + 1;
    break;
  }
 } while (cur_state != end);
 return 0;
}

variable_types:
fv
char *

variable_types:
m
char **

variable_types:
f
char *

variable_types:
b
char *

variable_types: enum state { start=0, skip_space=1, scan=2, scan_quote=3, scan_back=4, done=5, end=6 }


decompiled_code:
int __fastcall cmp_func(const char *a1, const char *a2, int a3, int a4)
{
int result; // eax

if ( a4 == 1 )
result = strncmp(a1, a2, a3);
else
result = strncasecmp(a1, a2, a3);
return result;
}

canonical_decompiled_code:
int __fastcall func2(const char *a1, const char *a2, int a3, int a4)
{
int result; // eax

if ( a4 == 1 )
result = strncmp(a1, a2, a3);
else
result = strncasecmp(a1, a2, a3);
return result;
}

original_code:
int cmp_func(const char *str1, const char *str2, int size, int case_sensitive) {
      if (case_sensitive == 1)
     return strncmp(str1, str2, size);
      else
     return strncasecmp(str1, str2, size);
}

canonical_original_code:
int cmp_func(char *str1, char *str2, int size, int case_sensitive) {
      if (case_sensitive == 1)
     return strncmp(str1, str2, size);
      else
     return strncasecmp(str1, str2, size);
}

variable_types: size int


decompiled_code:
__int64 __fastcall word_matcher(const char *a1, __int64 a2, int a3, _BYTE *a4, _BYTE *a5)
{
int i; // ebp
unsigned int v7; // ebx
int v9; // [rsp+Ch] [rbp-4Ch]
const char *v10; // [rsp+10h] [rbp-48h] BYREF
int v11[15]; // [rsp+1Ch] [rbp-3Ch] BYREF

v9 = (int)a4;
for ( i = 0; ; i += v7 )
{
v7 = enum_fields((_BYTE *)(a2 + i), &v10, v11, a4, a5);
if ( !v7 )
break;
if ( v11[0] == a3 && !cmp_func(v10, a1, v11[0], v9) )
return 1;
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func3(const char *a1, __int64 a2, int a3, _BYTE *a4, _BYTE *a5)
{
int i; // ebp
unsigned int v7; // ebx
int v9; // [rsp+Ch] [rbp-4Ch]
const char *v10; // [rsp+10h] [rbp-48h] BYREF
int v11[15]; // [rsp+1Ch] [rbp-3Ch] BYREF

v9 = (int)a4;
for ( i = 0; ; i += v7 )
{
v7 = func1((_BYTE *)(a2 + i), &v10, v11, a4, a5);
if ( !v7 )
break;
if ( v11[0] == a3 && !func2(v10, a1, v11[0], v9) )
return 1;
}
return v7;
}

original_code:
int word_matcher(const char *p, const char *fv, int ps, int case_sensitive) {
 int read, size, offset = 0;
 const char *match;
 while (read = enum_fields(fv + offset, &match, &size)) {
  if (ps == size) {
   if (cmp_func(match, p, size, case_sensitive) == 0) {
    return 1;
   }
  }
  offset += read;
 }
 return 0;
}

canonical_original_code:
int word_matcher(char *p, char *fv, int ps, int case_sensitive) {
 int read; int size; int offset = 0;
 char *match;
 while (read = enum_fields(fv + offset, &match, &size)) {
  if (ps == size) {
   if (cmp_func(match, p, size, case_sensitive) == 0) {
    return 1;
   }
  }
  offset += read;
 }
 return 0;
}

variable_types:
p
char *

variable_types: ps int

variable_types: read int

variable_types: offset int

variable_types:
match
char *


decompiled_code:
__int64 __fastcall substring_matcher(char *s, _BYTE *a2, int a3, _BYTE *a4, _BYTE *a5)
{
int v6; // er15
int v7; // er14
unsigned int i; // eax
unsigned int v9; // ebp
size_t v10; // rax
_BYTE *v11; // rcx
_BYTE *v12; // r8
const char *v13; // rbx
const char *v15; // [rsp+20h] [rbp-48h] BYREF
int v16[15]; // [rsp+2Ch] [rbp-3Ch] BYREF

v6 = (int)a4;
v7 = 0;
for ( i = enum_fields(a2, &v15, v16, a4, a5); ; i = enum_fields(&a2[v7], &v15, v16, v11, v12) )
{
v9 = i;
if ( !i )
break;
v10 = strlen(s);
if ( v10 <= v16[0] )
{
v13 = v15;
v11 = (_BYTE *)a3;
if ( !__CFADD__(v15, v16[0] - (__int64)a3) )
{
while ( cmp_func(v13, s, a3, v6) )
{
if ( ++v13 > &v15[v16[0] - (__int64)a3] )
goto LABEL_2;
}
return 1;
}
}
LABEL_2:
v7 += v9;
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *s, _BYTE *a2, int a3, _BYTE *a4, _BYTE *a5)
{
int v6; // er15
int v7; // er14
unsigned int i; // eax
unsigned int v9; // ebp
size_t v10; // rax
_BYTE *v11; // rcx
_BYTE *v12; // r8
const char *v13; // rbx
const char *v15; // [rsp+20h] [rbp-48h] BYREF
int v16[15]; // [rsp+2Ch] [rbp-3Ch] BYREF

v6 = (int)a4;
v7 = 0;
for ( i = func1(a2, &v15, v16, a4, a5); ; i = func1(&a2[v7], &v15, v16, v11, v12) )
{
v9 = i;
if ( !i )
break;
v10 = strlen(s);
if ( v10 <= v16[0] )
{
v13 = v15;
v11 = (_BYTE *)a3;
if ( !__CFADD__(v15, v16[0] - (__int64)a3) )
{
while ( func2(v13, s, a3, v6) )
{
if ( ++v13 > &v15[v16[0] - (__int64)a3] )
goto LABEL_2;
}
return 1;
}
}
LABEL_2:
v7 += v9;
}
return v9;
}

original_code:
int substring_matcher(const char *p, const char *fv, int ps, int case_sensitive) {
 int read, size, offset = 0;
 const char *match;
 while (read = enum_fields(fv + offset, &match, &size)) {
  if (strlen(p) <= size) {
   const char *s;
   for (s = match; s <= match + size - ps; s++) {
    if (cmp_func(s, p, ps, case_sensitive) == 0) {
     return 1;
    }
   }
  }
  offset += read;
 }
 return 0;
}

canonical_original_code:
int substring_matcher(char *p, char *fv, int ps, int case_sensitive) {
 int read; int size; int offset = 0;
 char *match;
 while (read = enum_fields(fv + offset, &match, &size)) {
  if (strlen(p) <= size) {
   char *s;
   for (s = match; s <= match + size - ps; s++) {
    if (cmp_func(s, p, ps, case_sensitive) == 0) {
     return 1;
    }
   }
  }
  offset += read;
 }
 return 0;
}

variable_types:
s
char *


decompiled_code:
__int64 __fastcall beginning_substring_matcher(const char *a1, __int64 a2, int a3, _BYTE *a4, _BYTE *a5)
{
int v6; // er14
int i; // ebp
unsigned int v8; // ebx
const char *v10; // [rsp+10h] [rbp-48h] BYREF
int v11[15]; // [rsp+1Ch] [rbp-3Ch] BYREF

v6 = (int)a4;
for ( i = 0; ; i += v8 )
{
v8 = enum_fields((_BYTE *)(a2 + i), &v10, v11, a4, a5);
if ( !v8 )
break;
if ( v11[0] >= a3 && !cmp_func(v10, a1, a3, v6) )
return 1;
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func5(const char *a1, __int64 a2, int a3, _BYTE *a4, _BYTE *a5)
{
int v6; // er14
int i; // ebp
unsigned int v8; // ebx
const char *v10; // [rsp+10h] [rbp-48h] BYREF
int v11[15]; // [rsp+1Ch] [rbp-3Ch] BYREF

v6 = (int)a4;
for ( i = 0; ; i += v8 )
{
v8 = func1((_BYTE *)(a2 + i), &v10, v11, a4, a5);
if ( !v8 )
break;
if ( v11[0] >= a3 && !func2(v10, a1, a3, v6) )
return 1;
}
return v8;
}

original_code:
int beginning_substring_matcher(const char *p, const char *fv, int ps, int case_sensitive) {
 int read, size, offset = 0;
 const char *match;
 while (read = enum_fields(fv + offset, &match, &size)) {
  if (ps <= size) {
   if (cmp_func(match, p, ps, case_sensitive) == 0) {
    return 1;
   }
  }
  offset += read;
 }
 return 0;
}

canonical_original_code:
int beginning_substring_matcher(char *p, char *fv, int ps, int case_sensitive) {
 int read; int size; int offset = 0;
 char *match;
 while (read = enum_fields(fv + offset, &match, &size)) {
  if (ps <= size) {
   if (cmp_func(match, p, ps, case_sensitive) == 0) {
    return 1;
   }
  }
  offset += read;
 }
 return 0;
}


decompiled_code:
__int64 __fastcall parameter_prefix_matcher(const char *a1, __int64 a2, int a3, _BYTE *a4, _BYTE *a5)
{
int v6; // er14
int i; // ebp
const char *v8; // rax
unsigned int v9; // ebx
const char *v10; // rdx
const char *v12; // [rsp+10h] [rbp-48h] BYREF
int v13[15]; // [rsp+1Ch] [rbp-3Ch] BYREF

v6 = (int)a4;
for ( i = 0; ; i += v9 )
{
v9 = enum_fields((_BYTE *)(a2 + i), &v12, v13, a4, a5);
if ( !v9 )
break;
if ( v13[0] >= a3 && !cmp_func(v12, a1, a3, v6) )
{
v8 = &v12[a3];
v10 = &v12[v13[0]];
if ( v8 < v10 )
{
while ( *v8 == 32 )
{
if ( ++v8 == v10 )
return 1;
}
}
if ( *v8 == 59 || v10 == v8 )
return 1;
}
}
return v9;
}

original_code:
int parameter_prefix_matcher(const char *p, const char *fv, int ps, int case_sensitive) {
 int read, size, offset = 0;
 const char *match;
 while (read = enum_fields(fv + offset, &match, &size)) {
  if (ps <= size) {
   if (cmp_func(match, p, ps, case_sensitive) == 0) {
    const char *r;
    for (r = match + ps; r < match + size && *r == ' '; r++)
     continue;
    if (*r == ';' || r == match + size)
     return 1;
   }
  }
  offset += read;
 }
 return 0;
}

canonical_original_code:
int parameter_prefix_matcher(char *p, char *fv, int ps, int case_sensitive) {
 int read; int size; int offset = 0;
 char *match;
 while (read = enum_fields(fv + offset, &match, &size)) {
  if (ps <= size) {
   if (cmp_func(match, p, ps, case_sensitive) == 0) {
    char *r;
    for (r = match + ps; r < match + size && *r == ' '; r++)
     continue;
    if (*r == ';' || r == match + size)
     return 1;
   }
  }
  offset += read;
 }
 return 0;
}

variable_types:
r
char *


original_code:
int parameter_range_matcher(const char *p, const char *fv, int ps, int case_sensitive) {
 case_sensitive = 0;
 int read, size, offset = 0;
 const char *match;
 int min = 99, max = 0;
 int has_min = 1, has_max = 1;
 char *x = p;
 for (x = p; *x && *x != '['; x++)
  continue;
 char *prefix = x - 1;
 ps = (int)(prefix - p) + 1;
 if (strncmp(x, "[:", 2)) {
  if (!sscanf(x, "[%d:", &min)) {
   return 0;
  } else
   has_min = 0;
 }
 for (; *x && *x != ':'; x++)
  continue;
 if (strncmp(x, ":]", 2)) {
  if (!sscanf(x, ":%d]", &max)) {
   return 0;
  } else
   has_max = 0;
 }
 while (read = enum_fields(fv + offset, &match, &size)) {
  if (ps <= size) {
   if (cmp_func(match, p, ps, case_sensitive) == 0) {
    if (match[ps] == '=') {
     int value;
     if (sscanf(match + ps + 1, "%d", &value)) {
         if ((has_min || (value >= min)) && (has_max || (value <= max)))
          return 1;
     }
    }
   }
  }
  offset += read;
 }
 return 0;
}

canonical_original_code:
int parameter_range_matcher(char *p, char *fv, int ps, int case_sensitive) {
 case_sensitive = 0;
 int read; int size; int offset = 0;
 char *match;
 int min = 99; int max = 0;
 int has_min = 1; int has_max = 1;
 char *x = p;
 for (x = p; *x && *x != '['; x++)
  continue;
 char *prefix = x - 1;
 ps = (int)(prefix - p) + 1;
 if (strncmp(x, "[:", 2)) {
  if (!sscanf(x, "[%d:", &min)) {
   return 0;
  } else
   has_min = 0;
 }
 for (; *x && *x != ':'; x++)
  continue;
 if (strncmp(x, ":]", 2)) {
  if (!sscanf(x, ":%d]", &max)) {
   return 0;
  } else
   has_max = 0;
 }
 while (read = enum_fields(fv + offset, &match, &size)) {
  if (ps <= size) {
   if (cmp_func(match, p, ps, case_sensitive) == 0) {
    if (match[ps] == '=') {
     int value;
     if (sscanf(match + ps + 1, "%d", &value)) {
         if ((has_min || (value >= min)) && (has_max || (value <= max)))
          return 1;
     }
    }
   }
  }
  offset += read;
 }
 return 0;
}

variable_types: min int

variable_types: max int

variable_types:
x
char *

variable_types:
prefix
char *

variable_types: value int


unescape

original_code:
int unescape(char *dst, const char *src, int len) {
 int i, j;
 i = j = 0;
 while (i < len) {
  switch (src[i]) {
   case '\\':
    switch(src[++i]) {
     case '\"':
      dst[j++] = '\"';
      i++;
      break;
     default:
      dst[j++] = '\\';
      dst[j] = src[i];
      break;
    }
    break;
   default:
    dst[j] = src[i];
    i++;
    j++;
    break;
  }
 }
 return j;
}

canonical_original_code:
int unescape(char *dst, char *src, int len) {
 int i; int j;
 i = j = 0;
 while (i < len) {
  switch (src[i]) {
   case '\\':
    switch(src[++i]) {
     case '\"':
      dst[j++] = '\"';
      i++;
      break;
     default:
      dst[j++] = '\\';
      dst[j] = src[i];
      break;
    }
    break;
   default:
    dst[j] = src[i];
    i++;
    j++;
    break;
  }
 }
 return j;
}

variable_types:
dst
char *

variable_types:
src
char *

variable_types: len int

variable_types: i int

variable_types: j int


matcher

decompiled_code:
_BOOL8 __fastcall matcher(__int64 a1, _BYTE *a2)
{
int v2; // er15
int v3; // er12
_BOOL4 v4; // er13
int v5; // ebp
int v6; // er13
char *v7; // rbx
unsigned int v8; // eax
_BYTE *v9; // r8
unsigned int v11; // [rsp+14h] [rbp-54h]
__int64 v12; // [rsp+20h] [rbp-48h] BYREF
int v13; // [rsp+28h] [rbp-40h] BYREF
int v14[15]; // [rsp+2Ch] [rbp-3Ch] BYREF

v11 = 0;
v2 = 0;
v3 = 0;
while ( 1 )
{
v5 = enum_matchers((_BYTE *)(a1 + v3), &v13, &v12, v14);
if ( !v5 )
break;
v6 = v14[0];
v7 = (char *)malloc(v14[0] + 1);
v8 = unescape(v7, v12, v6);
v14[0] = v8;
switch ( v13 )
{
case 1:
v4 = word_matcher(v7, (__int64)a2, v8, (_BYTE *)v11, v9) != v2;
goto LABEL_4;
case 2:
v4 = substring_matcher(v7, a2, v8, (_BYTE *)v11, v9) != v2;
goto LABEL_4;
case 3:
v4 = beginning_substring_matcher(v7, (__int64)a2, v8, (_BYTE *)v11, v9) != v2;
goto LABEL_4;
case 4:
v11 = 1;
goto LABEL_15;
case 5:
v2 = 1;
LABEL_15:
free(v7);
goto LABEL_5;
case 7:
v4 = parameter_range_matcher(v7, (__int64)a2, v8, v11, v9) != v2;
goto LABEL_4;
default:
v4 = 1;
LABEL_4:
free(v7);
if ( !v4 )
return v4;
LABEL_5:
v3 += v5;
break;
}
}
return 1;
}

canonical_decompiled_code:
_BOOL8 __fastcall func9(__int64 a1, _BYTE *a2)
{
int v2; // er15
int v3; // er12
_BOOL4 v4; // er13
int v5; // ebp
int v6; // er13
char *v7; // rbx
unsigned int v8; // eax
_BYTE *v9; // r8
unsigned int v11; // [rsp+14h] [rbp-54h]
__int64 v12; // [rsp+20h] [rbp-48h] BYREF
int v13; // [rsp+28h] [rbp-40h] BYREF
int v14[15]; // [rsp+2Ch] [rbp-3Ch] BYREF

v11 = 0;
v2 = 0;
v3 = 0;
while ( 1 )
{
v5 = func0((_BYTE *)(a1 + v3), &v13, &v12, v14);
if ( !v5 )
break;
v6 = v14[0];
v7 = (char *)malloc(v14[0] + 1);
v8 = func8(v7, v12, v6);
v14[0] = v8;
switch ( v13 )
{
case 1:
v4 = func3(v7, (__int64)a2, v8, (_BYTE *)v11, v9) != v2;
goto LABEL_4;
case 2:
v4 = func4(v7, a2, v8, (_BYTE *)v11, v9) != v2;
goto LABEL_4;
case 3:
v4 = func5(v7, (__int64)a2, v8, (_BYTE *)v11, v9) != v2;
goto LABEL_4;
case 4:
v11 = 1;
goto LABEL_15;
case 5:
v2 = 1;
LABEL_15:
free(v7);
goto LABEL_5;
case 7:
v4 = func7(v7, (__int64)a2, v8, v11, v9) != v2;
goto LABEL_4;
default:
v4 = 1;
LABEL_4:
free(v7);
if ( !v4 )
return v4;
LABEL_5:
v3 += v5;
break;
}
}
return 1;
}

original_code:
int matcher(const char *m, const char *f) {
    int mr, fr;
    int mo = 0, fo = 0;
    int fs, ms;
    int type;
    const char *match, *field;
    int not_flag = 0;
    int case_flag = 0;
    while (mr = enum_matchers(m + mo, &type, &match, &ms)) {
 int matched = 1;
 char *m = malloc(ms+1);
 ms = unescape(m,match,ms);
 switch(type) {
     case 1:
  if (!(word_matcher(m, f, ms, case_flag) ^ not_flag))
      matched = 0;
  break;
     case 2:
  if (!(substring_matcher(m, f, ms, case_flag) ^ not_flag))
      matched = 0;
  break;
     case 3:
  if (!(beginning_substring_matcher(m, f, ms, case_flag) ^ not_flag))
      matched = 0;
  break;
     case 7:
  if (!(parameter_range_matcher(m, f, ms, case_flag) ^ not_flag))
      matched = 0;
  break;
     case 4:
  case_flag = 1;
  break;
     case 5:
  not_flag = 1;
  break;
 }
 free(m);
 if (!matched)
     return 0;
 mo += mr;
    }
    return 1;
}

canonical_original_code:
int matcher(char *m, char *f) {
    int mr; int fr;
    int mo = 0; int fo = 0;
    int fs; int ms;
    int type;
    char *match; char *field;
    int not_flag = 0;
    int case_flag = 0;
    while (mr = enum_matchers(m + mo, &type, &match, &ms)) {
 int matched = 1;
 char *m = malloc(ms+1);
 ms = unescape(m,match,ms);
 switch(type) {
     case 1:
  if (!(word_matcher(m, f, ms, case_flag) ^ not_flag))
      matched = 0;
  break;
     case 2:
  if (!(substring_matcher(m, f, ms, case_flag) ^ not_flag))
      matched = 0;
  break;
     case 3:
  if (!(beginning_substring_matcher(m, f, ms, case_flag) ^ not_flag))
      matched = 0;
  break;
     case 7:
  if (!(parameter_range_matcher(m, f, ms, case_flag) ^ not_flag))
      matched = 0;
  break;
     case 4:
  case_flag = 1;
  break;
     case 5:
  not_flag = 1;
  break;
 }
 free(m);
 if (!matched)
     return 0;
 mo += mr;
    }
    return 1;
}

variable_types:
m
char *

variable_types:
f
char *

variable_types: mr int

variable_types: fr int

variable_types: mo int

variable_types: fo int

variable_types: fs int

variable_types: ms int

variable_types: type int

variable_types:
field
char *

variable_types: matched int


repo: jbussdieker/game

decompiled_code:
void *char_create()
{
void *result; // rax

result = (void *)sprite_create(20LL, 20LL, 128LL);
hero = result;
return result;
}

canonical_decompiled_code:
void *func17()
{
void *result; // rax

result = (void *)sprite_create(20LL, 20LL, 128LL);
hero = result;
return result;
}

original_code:
void char_create() {
  hero = sprite_create(20, 20, 0x00000080);
}


decompiled_code:
__int64 char_render()
{
return opengl_draw_sprite((unsigned int)hero_x, (unsigned int)hero_y, (__int64)hero);
}

canonical_decompiled_code:
__int64 func18()
{
return func9((unsigned int)hero_x, (unsigned int)hero_y, (__int64)hero);
}

original_code:
void char_render() {
  opengl_draw_sprite(hero_x, hero_y, hero);
}


decompiled_code:
__int64 char_delete()
{
return sprite_delete(hero);
}

canonical_decompiled_code:
__int64 func19()
{
return func21(hero);
}

original_code:
void char_delete() {
  sprite_delete(hero);
}


decompiled_code:
void __fastcall char_key_press(char a1)
{
switch ( a1 )
{
case 'w':
hero_y -= 5;
break;
case 'a':
hero_x -= 5;
return;
case 's':
hero_y += 5;
return;
case 'd':
hero_x += 5;
break;
}
}

canonical_decompiled_code:
void __fastcall func20(char a1)
{
switch ( a1 )
{
case 'w':
hero_y -= 5;
break;
case 'a':
hero_x -= 5;
return;
case 's':
hero_y += 5;
return;
case 'd':
hero_x += 5;
break;
}
}

original_code:
void char_key_press(unsigned char key, int x, int y) {
  if (key == 'w') {
    hero_y -= 1 * 5;
  }
  if (key == 'a') {
    hero_x -= 1 * 5;
  }
  if (key == 's') {
    hero_y += 1 * 5;
  }
  if (key == 'd') {
    hero_x += 1 * 5;
  }
}

variable_types:
key
unsigned char

variable_types: x int

variable_types: y int


decompiled_code:
void __fastcall sprite_delete(void *ptr)
{
free(*((void **)ptr + 1));
free(ptr);
}

canonical_decompiled_code:
void __fastcall func21(void *ptr)
{
free(*((void **)ptr + 1));
free(ptr);
}

original_code:
void sprite_delete(sprite *sprite) {
  free(sprite->data);
  free(sprite);
}

canonical_original_code:
void sprite_delete(struct sprite *sprite) {
  free(sprite->data);
  free(sprite);
}

variable_types: struct sprite { int width; int height; void *data; } *


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

tiles = sprite_create(32, 32, 8421504);
result = sprite_create(32, 32, 15790320);
ptr = result;
return result;
}

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

tiles = sprite_create(32, 32, 8421504);
result = sprite_create(32, 32, 15790320);
ptr = result;
return result;
}

original_code:
void map_create() {
  tiles[0] = sprite_create(32, 32, 0x00808080);
  tiles[1] = sprite_create(32, 32, 0x00F0F0F0);
}


decompiled_code:
__int64 map_render()
{
int *v0; // r13
unsigned int v1; // er14
int *v2; // rbp
unsigned int i; // ebx
__int64 result; // rax

v0 = (int *)↦
v1 = 0;
do
{
v2 = v0;
for ( i = 0; i != 640; i += 32 )
result = opengl_draw_sprite(i, v1, (__int64)*(&tiles + *v2++));
v0 += 20;
v1 += 32;
}
while ( v0 != (int *)&_GNU_EH_FRAME_HDR );
return result;
}

canonical_decompiled_code:
__int64 func23()
{
int *v0; // r13
unsigned int v1; // er14
int *v2; // rbp
unsigned int i; // ebx
__int64 result; // rax

v0 = (int *)↦
v1 = 0;
do
{
v2 = v0;
for ( i = 0; i != 640; i += 32 )
result = func9(i, v1, (__int64)*(&tiles + *v2++));
v0 += 20;
v1 += 32;
}
while ( v0 != (int *)&_GNU_EH_FRAME_HDR );
return result;
}

original_code:
void map_render() {
  int x, y;
  for (y = 0; y < 15; y++) {
    for (x = 0; x < 20; x++) {
      opengl_draw_sprite(x*32, y*32, tiles[map[y][x]]);
    }
  }
}

canonical_original_code:
void map_render() {
  int x; int y;
  for (y = 0; y < 15; y++) {
    for (x = 0; x < 20; x++) {
      opengl_draw_sprite(x*32, y*32, tiles[map[y][x]]);
    }
  }
}


decompiled_code:
void map_delete()
{
sprite_delete(tiles);
sprite_delete(ptr);
}

canonical_decompiled_code:
void func24()
{
func21(tiles);
func21(ptr);
}

original_code:
void map_delete() {
  sprite_delete(tiles[0]);
  sprite_delete(tiles[1]);
}


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax

SDL_GetTicks(argc, argv, envp);
init("Shreaders' Game: Under Development");
atexit((void (*)(void))&cleanup);
stewie[3] = loadImage("data/world/stewie_world.png");
stewie[4] = loadImage("data/battle/monsters/stewie.gif");
player[4] = loadImage("data/battle/norrisBattle.gif");
lv1Goop[4] = loadImage("data/battle/monsters/goopMonster.gif");
lv2Lizard[4] = loadImage("data/battle/monsters/lizardMonster.gif");
lv3Multi[4] = loadImage("data/battle/monsters/multiheadedBeast.gif");
backgroundImage = loadImage("data/world/background.png");
battleDungeonImage = loadImage("data/battle/dungeonFight.png");
DUN_TILE = loadImage("data/world/DUN_TILE.png");
WAT_TILE = loadImage("data/world/WAT_TILE.png");
GRA_TILE = loadImage("data/world/GRA_TILE.png");
SNO_TILE = loadImage("data/world/SNO_TILE.png");
RED_TILE = loadImage("data/world/GRA_TILE.png");
DUN_WAT_R_TILE = loadImage("data/world/DUN_WAT_R_TILE.png");
DUN_WAT_L_TILE = loadImage("data/world/DUN_WAT_L_TILE.png");
DUN_WAT_T_TILE = loadImage("data/world/DUN_WAT_T_TILE.png");
DUN_WAT_B_TILE = loadImage("data/world/DUN_WAT_B_TILE.png");
DUN_WAT_TR_TILE = loadImage("data/world/DUN_WAT_TR_TILE.png");
DUN_WAT_TL_TILE = loadImage("data/world/DUN_WAT_TL_TILE.png");
DUN_WAT_BR_TILE = loadImage("data/world/DUN_WAT_BR_TILE.png");
DUN_WAT_BL_TILE = loadImage("data/world/DUN_WAT_BL_TILE.png");
DUN_EMP_R_TILE = loadImage("data/world/DUN_EMP_R_TILE.png");
DUN_EMP_L_TILE = loadImage("data/world/DUN_EMP_L_TILE.png");
DUN_EMP_T_TILE = loadImage("data/world/DUN_EMP_T_TILE.png");
DUN_EMP_TR_TILE = loadImage("data/world/DUN_EMP_TR_TILE.png");
DUN_EMP_TL_TILE = loadImage("data/world/DUN_EMP_TL_TILE.png");
TRE_OPN_TILE = loadImage("data/world/TRE_OPN_TILE.png");
TRE_CLD_TILE = loadImage("data/world/TRE_CLD_TILE.png");
baseatk = (__int64)malloc(5uLL);
curatk = (__int64)malloc(5uLL);
basedef = (__int64)malloc(5uLL);
curdef = (__int64)malloc(5uLL);
basespd = (__int64)malloc(5uLL);
curspd = (__int64)malloc(5uLL);
maxhp = (__int64)malloc(5uLL);
curhp = (__int64)malloc(5uLL);
curexp = (__int64)malloc(5uLL);
curlvl = (__int64)malloc(5uLL);
gold = (__int64)malloc(5uLL);
dmgdisp1 = (__int64)malloc(5uLL);
dmgdisp2 = (__int64)malloc(5uLL);
potOne = (__int64)malloc(5uLL);
potTwo = (__int64)malloc(5uLL);
potThree = (__int64)malloc(5uLL);
potFour = (__int64)malloc(5uLL);
potOneI = (__int64)malloc(5uLL);
potTwoI = (__int64)malloc(5uLL);
potThreeI = (__int64)malloc(5uLL);
potFourI = (__int64)malloc(5uLL);
swordOneI = (__int64)malloc(5uLL);
swordTwoI = (__int64)malloc(5uLL);
swordThreeI = (__int64)malloc(5uLL);
swordFourI = (__int64)malloc(5uLL);
armorOneI = (__int64)malloc(5uLL);
armorTwoI = (__int64)malloc(5uLL);
armorThreeI = (__int64)malloc(5uLL);
armorFourI = (__int64)malloc(5uLL);
expGained = (__int64)malloc(5uLL);
goldGained = (__int64)malloc(5uLL);
loadMap("data/maps/map01.dat");
loadAllSprites();
loadAnimation("data/anim/playerdownanim.dat", &playerAnimDown);
loadAnimation("data/anim/playerupanim.dat", &playerAnimUp);
loadAnimation("data/anim/playerleftanim.dat", &playerAnimLeft);
loadAnimation("data/anim/playerrightanim.dat", &playerAnimRight);
loadAnimation("data/anim/slashone.dat", &slashOne);
loadAnimation("data/anim/slashtwo.dat", &slashTwo);
displayer[1] = loadFont("data/font/blackWolf.ttf", 40LL);
displayer[2] = loadFont("data/font/blackWolf.ttf", 20LL);
initPlayer();
v3 = Mix_LoadMUS("data/sounds/bg.wav");
music = v3;
if ( v3 )
{
Mix_PlayMusic(v3, 0xFFFFFFFFLL);
while ( 1 )
{
getInput();
doPlayer();
draw();
SDL_Delay(16LL);
}
}
printf("Main: Problem loading music: 'bg.wav'");
exit(4);
}

original_code:
int main(int argc, char *argv[])
{
 unsigned int frameLimit = SDL_GetTicks() + 16;
 int go;
 init("Shreaders' Game: Under Development");
 atexit(cleanup);
 go = 1;
 stewie.sprite = loadImage("data/world/stewie_world.png");
 stewie.battleSprite = loadImage("data/battle/monsters/stewie.gif");
 player.battleSprite = loadImage("data/battle/norrisBattle.gif");
 lv1Goop.battleSprite = loadImage("data/battle/monsters/goopMonster.gif");
 lv2Lizard.battleSprite = loadImage("data/battle/monsters/lizardMonster.gif");
 lv3Multi.battleSprite = loadImage("data/battle/monsters/multiheadedBeast.gif");
 backgroundImage = loadImage("data/world/background.png");
 battleDungeonImage = loadImage("data/battle/dungeonFight.png");
 DUN_TILE = loadImage("data/world/DUN_TILE.png");
 WAT_TILE = loadImage("data/world/WAT_TILE.png");
 GRA_TILE = loadImage("data/world/GRA_TILE.png");
 SNO_TILE = loadImage("data/world/SNO_TILE.png");
 RED_TILE = loadImage("data/world/GRA_TILE.png");
 DUN_WAT_R_TILE = loadImage("data/world/DUN_WAT_R_TILE.png");
 DUN_WAT_L_TILE = loadImage("data/world/DUN_WAT_L_TILE.png");
 DUN_WAT_T_TILE = loadImage("data/world/DUN_WAT_T_TILE.png");
 DUN_WAT_B_TILE = loadImage("data/world/DUN_WAT_B_TILE.png");
 DUN_WAT_TR_TILE = loadImage("data/world/DUN_WAT_TR_TILE.png");
 DUN_WAT_TL_TILE = loadImage("data/world/DUN_WAT_TL_TILE.png");
 DUN_WAT_BR_TILE = loadImage("data/world/DUN_WAT_BR_TILE.png");
 DUN_WAT_BL_TILE = loadImage("data/world/DUN_WAT_BL_TILE.png");
 DUN_EMP_R_TILE = loadImage("data/world/DUN_EMP_R_TILE.png");
 DUN_EMP_L_TILE = loadImage("data/world/DUN_EMP_L_TILE.png");
 DUN_EMP_T_TILE = loadImage("data/world/DUN_EMP_T_TILE.png");
 DUN_EMP_TR_TILE = loadImage("data/world/DUN_EMP_TR_TILE.png");
 DUN_EMP_TL_TILE = loadImage("data/world/DUN_EMP_TL_TILE.png");
 TRE_OPN_TILE = loadImage("data/world/TRE_OPN_TILE.png");
 TRE_CLD_TILE = loadImage("data/world/TRE_CLD_TILE.png");
 baseatk = (char*)malloc(5 * sizeof(char));
 curatk= (char*)malloc(5 * sizeof(char));
 basedef= (char*)malloc(5 * sizeof(char));
 curdef= (char*)malloc(5 * sizeof(char));
 basespd= (char*)malloc(5 * sizeof(char));
 curspd= (char*)malloc(5 * sizeof(char));
 maxhp= (char*)malloc(5 * sizeof(char));
 curhp= (char*)malloc(5 * sizeof(char));
 curexp= (char*)malloc(5 * sizeof(char));
 curlvl= (char*)malloc(5 * sizeof(char));
 gold= (char*)malloc(5 * sizeof(char));
 dmgdisp1= (char*)malloc(5 * sizeof(char));
 dmgdisp2= (char*)malloc(5 * sizeof(char));
 potOne = (char*)malloc(5 * sizeof(char));
 potTwo = (char*)malloc(5 * sizeof(char));
 potThree = (char*)malloc(5 * sizeof(char));
 potFour = (char*)malloc(5 * sizeof(char));
  potOneI = (char*)malloc(5 * sizeof(char));
 potTwoI = (char*)malloc(5 * sizeof(char));
 potThreeI = (char*)malloc(5 * sizeof(char));
 potFourI = (char*)malloc(5 * sizeof(char));
  swordOneI = (char*)malloc(5 * sizeof(char));
  swordTwoI = (char*)malloc(5 * sizeof(char));
 swordThreeI = (char*)malloc(5 * sizeof(char));
 swordFourI = (char*)malloc(5 * sizeof(char));
  armorOneI = (char*)malloc(5 * sizeof(char));
 armorTwoI = (char*)malloc(5 * sizeof(char));
 armorThreeI = (char*)malloc(5 * sizeof(char));
 armorFourI = (char*)malloc(5 * sizeof(char));
 expGained = (char*)malloc(5 * sizeof(char));
 goldGained = (char*)malloc(5 * sizeof(char));
 loadMap("data/maps/map01.dat");
 loadAllSprites();
 loadAnimation("data/anim/playerdownanim.dat", &playerAnimDown);
 loadAnimation("data/anim/playerupanim.dat", &playerAnimUp);
 loadAnimation("data/anim/playerleftanim.dat", &playerAnimLeft);
 loadAnimation("data/anim/playerrightanim.dat", &playerAnimRight);
 loadAnimation("data/anim/slashone.dat", &slashOne);
 loadAnimation("data/anim/slashtwo.dat", &slashTwo);
 displayer.font = loadFont("data/font/blackWolf.ttf", 40);
 displayer.smallfont = loadFont("data/font/blackWolf.ttf", 20);
 initPlayer();
 music = Mix_LoadMUS( "data/sounds/bg.wav" );
 if(music == ((void *)0))
 {
  printf("Main: Problem loading music: 'bg.wav'");
  exit(4);
 }
 Mix_PlayMusic( music, -1 );
 while (go == 1)
 {
  getInput();
  doPlayer();
  draw();
  SDL_Delay(16);
 }
 exit(0);
}

canonical_original_code:
int main(int argc, char *argv[])
{
 unsigned int frameLimit = SDL_GetTicks() + 16;
 int go;
 init("Shreaders' Game: Under Development");
 atexit(cleanup);
 go = 1;
 stewie.sprite = loadImage("data/world/stewie_world.png");
 stewie.battleSprite = loadImage("data/battle/monsters/stewie.gif");
 player.battleSprite = loadImage("data/battle/norrisBattle.gif");
 lv1Goop.battleSprite = loadImage("data/battle/monsters/goopMonster.gif");
 lv2Lizard.battleSprite = loadImage("data/battle/monsters/lizardMonster.gif");
 lv3Multi.battleSprite = loadImage("data/battle/monsters/multiheadedBeast.gif");
 backgroundImage = loadImage("data/world/background.png");
 battleDungeonImage = loadImage("data/battle/dungeonFight.png");
 DUN_TILE = loadImage("data/world/DUN_TILE.png");
 WAT_TILE = loadImage("data/world/WAT_TILE.png");
 GRA_TILE = loadImage("data/world/GRA_TILE.png");
 SNO_TILE = loadImage("data/world/SNO_TILE.png");
 RED_TILE = loadImage("data/world/GRA_TILE.png");
 DUN_WAT_R_TILE = loadImage("data/world/DUN_WAT_R_TILE.png");
 DUN_WAT_L_TILE = loadImage("data/world/DUN_WAT_L_TILE.png");
 DUN_WAT_T_TILE = loadImage("data/world/DUN_WAT_T_TILE.png");
 DUN_WAT_B_TILE = loadImage("data/world/DUN_WAT_B_TILE.png");
 DUN_WAT_TR_TILE = loadImage("data/world/DUN_WAT_TR_TILE.png");
 DUN_WAT_TL_TILE = loadImage("data/world/DUN_WAT_TL_TILE.png");
 DUN_WAT_BR_TILE = loadImage("data/world/DUN_WAT_BR_TILE.png");
 DUN_WAT_BL_TILE = loadImage("data/world/DUN_WAT_BL_TILE.png");
 DUN_EMP_R_TILE = loadImage("data/world/DUN_EMP_R_TILE.png");
 DUN_EMP_L_TILE = loadImage("data/world/DUN_EMP_L_TILE.png");
 DUN_EMP_T_TILE = loadImage("data/world/DUN_EMP_T_TILE.png");
 DUN_EMP_TR_TILE = loadImage("data/world/DUN_EMP_TR_TILE.png");
 DUN_EMP_TL_TILE = loadImage("data/world/DUN_EMP_TL_TILE.png");
 TRE_OPN_TILE = loadImage("data/world/TRE_OPN_TILE.png");
 TRE_CLD_TILE = loadImage("data/world/TRE_CLD_TILE.png");
 baseatk = (char *)malloc(5 * sizeof(char));
 curatk= (char *)malloc(5 * sizeof(char));
 basedef= (char *)malloc(5 * sizeof(char));
 curdef= (char *)malloc(5 * sizeof(char));
 basespd= (char *)malloc(5 * sizeof(char));
 curspd= (char *)malloc(5 * sizeof(char));
 maxhp= (char *)malloc(5 * sizeof(char));
 curhp= (char *)malloc(5 * sizeof(char));
 curexp= (char *)malloc(5 * sizeof(char));
 curlvl= (char *)malloc(5 * sizeof(char));
 gold= (char *)malloc(5 * sizeof(char));
 dmgdisp1= (char *)malloc(5 * sizeof(char));
 dmgdisp2= (char *)malloc(5 * sizeof(char));
 potOne = (char *)malloc(5 * sizeof(char));
 potTwo = (char *)malloc(5 * sizeof(char));
 potThree = (char *)malloc(5 * sizeof(char));
 potFour = (char *)malloc(5 * sizeof(char));
  potOneI = (char *)malloc(5 * sizeof(char));
 potTwoI = (char *)malloc(5 * sizeof(char));
 potThreeI = (char *)malloc(5 * sizeof(char));
 potFourI = (char *)malloc(5 * sizeof(char));
  swordOneI = (char *)malloc(5 * sizeof(char));
  swordTwoI = (char *)malloc(5 * sizeof(char));
 swordThreeI = (char *)malloc(5 * sizeof(char));
 swordFourI = (char *)malloc(5 * sizeof(char));
  armorOneI = (char *)malloc(5 * sizeof(char));
 armorTwoI = (char *)malloc(5 * sizeof(char));
 armorThreeI = (char *)malloc(5 * sizeof(char));
 armorFourI = (char *)malloc(5 * sizeof(char));
 expGained = (char *)malloc(5 * sizeof(char));
 goldGained = (char *)malloc(5 * sizeof(char));
 loadMap("data/maps/map01.dat");
 loadAllSprites();
 loadAnimation("data/anim/playerdownanim.dat", &playerAnimDown);
 loadAnimation("data/anim/playerupanim.dat", &playerAnimUp);
 loadAnimation("data/anim/playerleftanim.dat", &playerAnimLeft);
 loadAnimation("data/anim/playerrightanim.dat", &playerAnimRight);
 loadAnimation("data/anim/slashone.dat", &slashOne);
 loadAnimation("data/anim/slashtwo.dat", &slashTwo);
 displayer.font = loadFont("data/font/blackWolf.ttf", 40);
 displayer.smallfont = loadFont("data/font/blackWolf.ttf", 20);
 initPlayer();
 music = Mix_LoadMUS( "data/sounds/bg.wav" );
 if(music == ((void *)0))
 {
  printf("Main: Problem loading music: 'bg.wav'");
  exit(4);
 }
 Mix_PlayMusic( music, -1 );
 while (go == 1)
 {
  getInput();
  doPlayer();
  draw();
  SDL_Delay(16);
 }
 exit(0);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
frameLimit
unsigned int

variable_types: go int


initPlayer

decompiled_code:
__int64 initPlayer()
{
__int64 v0; // rax
__int64 result; // rax

*((_QWORD *)&player + 3) = getSprite(0LL);
loadEntityPos("data/charPositionData.dat", &player, &stewie);
v0 = *((_QWORD *)&player + 3);
*(&player + 4) = *(_DWORD *)(v0 + 16);
result = *(unsigned int *)(v0 + 20);
*(&player + 5) = result;
*(&player + 2) = 0;
*(&player + 3) = 0;
return result;
}

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

*((_QWORD *)&player + 3) = getSprite(0LL);
loadEntityPos("data/charPositionData.dat", &player, &stewie);
v0 = *((_QWORD *)&player + 3);
*(&player + 4) = *(_DWORD *)(v0 + 16);
result = *(unsigned int *)(v0 + 20);
*(&player + 5) = result;
*(&player + 2) = 0;
*(&player + 3) = 0;
return result;
}

original_code:
void initPlayer()
{
 player.sprite = getSprite(0);
 loadEntityPos("data/charPositionData.dat", &player, &stewie);
 player.w = player.sprite->w;
 player.h = player.sprite->h;
 player.dirX = 0;
 player.dirY = 0;
}


doPlayer

decompiled_code:
__int64 doPlayer()
{
unsigned int v1; // eax
unsigned int v2; // eax
unsigned int v3; // eax
unsigned int v4; // eax

*(&player + 2) = 0;
*(&player + 3) = 0;
if ( input[2] == 1 && dontDisplayMap != 1 )
{
if ( showTitleScreen == 1 )
{
if ( input[3] != 1 )
{
if ( input[0] == 1 )
{
LABEL_20:
if ( showTitleScreen == 1 )
goto LABEL_5;
*(&player + 2) -= 5;
directionDetermine = 2;
lastFacing = 2;
v3 = queryMap((unsigned int)player, (unsigned int)*(&player + 1));
randomBattleCheck(v3);
goto LABEL_4;
}
goto LABEL_4;
}
LABEL_25:
if ( input[0] == 1 )
goto LABEL_5;
goto LABEL_4;
}
*(&player + 3) = -5;
directionDetermine = 0;
lastFacing = 0;
v1 = queryMap((unsigned int)player, (unsigned int)*(&player + 1));
randomBattleCheck(v1);
}
if ( input[3] == 1 )
{
if ( dontDisplayMap == 1 )
{
if ( input[0] == 1 )
goto LABEL_5;
goto LABEL_4;
}
if ( showTitleScreen == 1 )
goto LABEL_25;
*(&player + 3) += 5;
directionDetermine = 1;
lastFacing = 1;
v2 = queryMap((unsigned int)player, (unsigned int)*(&player + 1));
randomBattleCheck(v2);
}
if ( input[0] == 1 )
{
if ( dontDisplayMap == 1 )
goto LABEL_5;
goto LABEL_20;
}
LABEL_4:
if ( input[1] == 1 && dontDisplayMap != 1 && showTitleScreen != 1 )
{
*(&player + 2) += 5;
directionDetermine = 3;
lastFacing = 3;
v4 = queryMap((unsigned int)player, (unsigned int)*(&player + 1));
randomBattleCheck(v4);
}
LABEL_5:
if ( input[4] == 1 )
{
saveCharData("char.dat");
saveTreasureChestData("TreasureChest.dat");
}
if ( input[5] == 1 )
loadCharData("char.dat");
checkToMap(&player);
return centerEntityOnMap(&player);
}

canonical_decompiled_code:
__int64 func1()
{
unsigned int v1; // eax
unsigned int v2; // eax
unsigned int v3; // eax
unsigned int v4; // eax

*(&player + 2) = 0;
*(&player + 3) = 0;
if ( input[2] == 1 && dontDisplayMap != 1 )
{
if ( showTitleScreen == 1 )
{
if ( input[3] != 1 )
{
if ( input[0] == 1 )
{
LABEL_20:
if ( showTitleScreen == 1 )
goto LABEL_5;
*(&player + 2) -= 5;
directionDetermine = 2;
lastFacing = 2;
v3 = queryMap((unsigned int)player, (unsigned int)*(&player + 1));
randomBattleCheck(v3);
goto LABEL_4;
}
goto LABEL_4;
}
LABEL_25:
if ( input[0] == 1 )
goto LABEL_5;
goto LABEL_4;
}
*(&player + 3) = -5;
directionDetermine = 0;
lastFacing = 0;
v1 = queryMap((unsigned int)player, (unsigned int)*(&player + 1));
randomBattleCheck(v1);
}
if ( input[3] == 1 )
{
if ( dontDisplayMap == 1 )
{
if ( input[0] == 1 )
goto LABEL_5;
goto LABEL_4;
}
if ( showTitleScreen == 1 )
goto LABEL_25;
*(&player + 3) += 5;
directionDetermine = 1;
lastFacing = 1;
v2 = queryMap((unsigned int)player, (unsigned int)*(&player + 1));
randomBattleCheck(v2);
}
if ( input[0] == 1 )
{
if ( dontDisplayMap == 1 )
goto LABEL_5;
goto LABEL_20;
}
LABEL_4:
if ( input[1] == 1 && dontDisplayMap != 1 && showTitleScreen != 1 )
{
*(&player + 2) += 5;
directionDetermine = 3;
lastFacing = 3;
v4 = queryMap((unsigned int)player, (unsigned int)*(&player + 1));
randomBattleCheck(v4);
}
LABEL_5:
if ( input[4] == 1 )
{
saveCharData("char.dat");
saveTreasureChestData("TreasureChest.dat");
}
if ( input[5] == 1 )
loadCharData("char.dat");
checkToMap(&player);
return centerEntityOnMap(&player);
}

original_code:
void doPlayer()
{
 player.dirX = 0;
 player.dirY = 0;
 if (input.up == 1)
 {
  if(dontDisplayMap != 1 && showTitleScreen != 1){
   player.dirY -= 5;
   directionDetermine = 0;
   lastFacing = 0;
   randomBattleCheck( queryMap(player.x, player.y));
  }
 }
 if (input.down == 1)
 {
  if(dontDisplayMap != 1 && showTitleScreen != 1){
   player.dirY += 5;
   directionDetermine = 1;
   lastFacing = 1;
   randomBattleCheck( queryMap(player.x, player.y));
  }
 }
 if (input.left == 1)
 {
  if(dontDisplayMap != 1 && showTitleScreen != 1){
   player.dirX -= 5;
   directionDetermine = 2;
   lastFacing = 2;
   randomBattleCheck( queryMap(player.x, player.y));
  }
 }
 if (input.right == 1)
 {
  if(dontDisplayMap != 1 && showTitleScreen != 1){
   player.dirX += 5;
   directionDetermine = 3;
   lastFacing = 3;
   randomBattleCheck( queryMap(player.x, player.y));
  }
 }
 if (input.save == 1) {
  saveCharData("char.dat");
  saveTreasureChestData("TreasureChest.dat");
 }
 if (input.load == 1) {
  loadCharData("char.dat");
 }
 checkToMap(&player);
 centerEntityOnMap(&player);
}


drawPlayer

decompiled_code:
__int64 drawPlayer()
{
__int64 result; // rax
__int64 v1; // rax
__int64 v2; // rax
__int64 v3; // rax
__int64 v4; // rax

if ( directionDetermine )
{
switch ( directionDetermine )
{
case 1:
doAnimation(&playerAnimDown);
result = drawAnimation(
&playerAnimDown,
(unsigned int)(player - samplemap),
(unsigned int)(*(&player + 1) - *((_DWORD *)&samplemap + 1)));
directionDetermine = -1;
break;
case 2:
doAnimation(&playerAnimLeft);
result = drawAnimation(
&playerAnimLeft,
(unsigned int)(player - samplemap),
(unsigned int)(*(&player + 1) - *((_DWORD *)&samplemap + 1)));
directionDetermine = -1;
break;
case 3:
doAnimation(&playerAnimRight);
result = drawAnimation(
&playerAnimRight,
(unsigned int)(player - samplemap),
(unsigned int)(*(&player + 1) - *((_DWORD *)&samplemap + 1)));
directionDetermine = -1;
break;
default:
result = lastFacing;
if ( ((lastFacing + 1) & 0xFFFFFFFD) != 0 )
{
if ( lastFacing )
{
if ( lastFacing == 2 )
{
v4 = getSprite(6LL);
*((_QWORD *)&player + 3) = v4;
result = drawImage(
v4,
(unsigned int)(player - samplemap),
(unsigned int)(*(&player + 1) - *((_DWORD *)&samplemap + 1)));
}
else if ( lastFacing == 3 )
{
v1 = getSprite(4LL);
*((_QWORD *)&player + 3) = v1;
result = drawImage(
v1,
(unsigned int)(player - samplemap),
(unsigned int)(*(&player + 1) - *((_DWORD *)&samplemap + 1)));
}
}
else
{
v3 = getSprite(2LL);
*((_QWORD *)&player + 3) = v3;
result = drawImage(
v3,
(unsigned int)(player - samplemap),
(unsigned int)(*(&player + 1) - *((_DWORD *)&samplemap + 1)));
}
}
else
{
v2 = getSprite(0LL);
*((_QWORD *)&player + 3) = v2;
result = drawImage(
v2,
(unsigned int)(player - samplemap),
(unsigned int)(*(&player + 1) - *((_DWORD *)&samplemap + 1)));
}
break;
}
}
else
{
doAnimation(&playerAnimUp);
result = drawAnimation(
&playerAnimUp,
(unsigned int)(player - samplemap),
(unsigned int)(*(&player + 1) - *((_DWORD *)&samplemap + 1)));
directionDetermine = -1;
}
return result;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 result; // rax
__int64 v1; // rax
__int64 v2; // rax
__int64 v3; // rax
__int64 v4; // rax

if ( directionDetermine )
{
switch ( directionDetermine )
{
case 1:
doAnimation(&playerAnimDown);
result = drawAnimation(
&playerAnimDown,
(unsigned int)(player - samplemap),
(unsigned int)(*(&player + 1) - *((_DWORD *)&samplemap + 1)));
directionDetermine = -1;
break;
case 2:
doAnimation(&playerAnimLeft);
result = drawAnimation(
&playerAnimLeft,
(unsigned int)(player - samplemap),
(unsigned int)(*(&player + 1) - *((_DWORD *)&samplemap + 1)));
directionDetermine = -1;
break;
case 3:
doAnimation(&playerAnimRight);
result = drawAnimation(
&playerAnimRight,
(unsigned int)(player - samplemap),
(unsigned int)(*(&player + 1) - *((_DWORD *)&samplemap + 1)));
directionDetermine = -1;
break;
default:
result = lastFacing;
if ( ((lastFacing + 1) & 0xFFFFFFFD) != 0 )
{
if ( lastFacing )
{
if ( lastFacing == 2 )
{
v4 = getSprite(6LL);
*((_QWORD *)&player + 3) = v4;
result = drawImage(
v4,
(unsigned int)(player - samplemap),
(unsigned int)(*(&player + 1) - *((_DWORD *)&samplemap + 1)));
}
else if ( lastFacing == 3 )
{
v1 = getSprite(4LL);
*((_QWORD *)&player + 3) = v1;
result = drawImage(
v1,
(unsigned int)(player - samplemap),
(unsigned int)(*(&player + 1) - *((_DWORD *)&samplemap + 1)));
}
}
else
{
v3 = getSprite(2LL);
*((_QWORD *)&player + 3) = v3;
result = drawImage(
v3,
(unsigned int)(player - samplemap),
(unsigned int)(*(&player + 1) - *((_DWORD *)&samplemap + 1)));
}
}
else
{
v2 = getSprite(0LL);
*((_QWORD *)&player + 3) = v2;
result = drawImage(
v2,
(unsigned int)(player - samplemap),
(unsigned int)(*(&player + 1) - *((_DWORD *)&samplemap + 1)));
}
break;
}
}
else
{
doAnimation(&playerAnimUp);
result = drawAnimation(
&playerAnimUp,
(unsigned int)(player - samplemap),
(unsigned int)(*(&player + 1) - *((_DWORD *)&samplemap + 1)));
directionDetermine = -1;
}
return result;
}

original_code:
void drawPlayer()
{
 if(directionDetermine == 0){
  doAnimation(&playerAnimUp);
  drawAnimation(&playerAnimUp, player.x - samplemap.startX, player.y - samplemap.startY);
  directionDetermine = -1;
 }
 else if(directionDetermine == 1){
  doAnimation(&playerAnimDown);
  drawAnimation(&playerAnimDown, player.x - samplemap.startX, player.y - samplemap.startY);
  directionDetermine = -1;
 }
 else if(directionDetermine ==2 ){
  doAnimation(&playerAnimLeft);
  drawAnimation(&playerAnimLeft, player.x - samplemap.startX, player.y - samplemap.startY);
  directionDetermine = -1;
 }
 else if(directionDetermine == 3){
  doAnimation(&playerAnimRight);
  drawAnimation(&playerAnimRight, player.x - samplemap.startX, player.y - samplemap.startY);
  directionDetermine = -1;
 }
 else{
  if(lastFacing == 1 || lastFacing == -1){
   player.sprite = getSprite(0);
   drawImage(player.sprite, player.x - samplemap.startX, player.y - samplemap.startY);
  }
  else if(lastFacing == 0){
   player.sprite = getSprite(2);
   drawImage(player.sprite, player.x - samplemap.startX, player.y - samplemap.startY);
  }
  else if(lastFacing == 2){
   player.sprite = getSprite(6);
   drawImage(player.sprite, player.x - samplemap.startX, player.y - samplemap.startY);
  }
  else if(lastFacing == 3){
   player.sprite = getSprite(4);
   drawImage(player.sprite, player.x - samplemap.startX, player.y - samplemap.startY);
  }
 }
}


loadSprite

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

if ( a1 > 7 )
{
printf("Invalid index for sprite! Index: %d Maximum: %d\n", a1, 8LL);
exit(1);
}
result = loadImage(a2);
*((_QWORD *)&sprite + (int)a1) = result;
if ( !result )
exit(1);
return result;
}

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

if ( a1 > 7 )
{
printf("Invalid index for sprite! Index: %d Maximum: %d\n", a1, 8LL);
exit(1);
}
result = func0(a2);
*((_QWORD *)&sprite + (int)a1) = result;
if ( !result )
exit(1);
return result;
}

original_code:
void loadSprite(int index, char * name){
 if (index >= 8 || index < 0)
 {
  printf("Invalid index for sprite! Index: %d Maximum: %d\n", index, 8);
  exit(1);
 }
 sprite[index].image = loadImage(name);
 if (sprite[index].image == ((void *)0))
 {
  exit(1);
 }
}

canonical_original_code:
void loadSprite(int index, char *name){
 if (index >= 8 || index < 0)
 {
  printf("Invalid index for sprite! Index: %d Maximum: %d\n", index, 8);
  exit(1);
 }
 sprite[index].image = loadImage(name);
 if (sprite[index].image == ((void *)0))
 {
  exit(1);
 }
}

variable_types: index int

variable_types:
name
char *


freeSprites

decompiled_code:
__int64 freeSprites()
{
_QWORD *v0; // rbx
__int64 result; // rax

v0 = &sprite;
do
{
if ( *v0 )
result = SDL_FreeSurface(*v0);
++v0;
}
while ( v0 != (_QWORD *)(unsigned int)((_DWORD)&sprite + 64) );
return result;
}

canonical_decompiled_code:
__int64 func4()
{
_QWORD *v0; // rbx
__int64 result; // rax

v0 = &sprite;
do
{
if ( *v0 )
result = SDL_FreeSurface(*v0);
++v0;
}
while ( v0 != (_QWORD *)(unsigned int)((_DWORD)&sprite + 64) );
return result;
}

original_code:
void freeSprites()
{
 int i;
 for (i=0;i<8;i++) {
  if (sprite[i].image != ((void *)0)) {
   SDL_FreeSurface(sprite[i].image);
  }
 }
}

variable_types: i int


loadAllSprites

decompiled_code:
__int64 loadAllSprites()
{
loadSprite(0, "data/world/player/playerDown.gif");
loadSprite(1u, "data/world/player/playerDownWalk1.gif");
loadSprite(2u, "data/world/player/playerUp.gif");
loadSprite(3u, "data/world/player/playerUpWalk1.gif");
loadSprite(4u, "data/world/player/playerRight.gif");
loadSprite(5u, "data/world/player/playerRightWalk.gif");
loadSprite(6u, "data/world/player/playerLeft.gif");
return loadSprite(7u, "data/world/player/playerLeftWalk.gif");
}

canonical_decompiled_code:
__int64 func5()
{
func2(0, "data/world/player/playerDown.gif");
func2(1u, "data/world/player/playerDownWalk1.gif");
func2(2u, "data/world/player/playerUp.gif");
func2(3u, "data/world/player/playerUpWalk1.gif");
func2(4u, "data/world/player/playerRight.gif");
func2(5u, "data/world/player/playerRightWalk.gif");
func2(6u, "data/world/player/playerLeft.gif");
return func2(7u, "data/world/player/playerLeftWalk.gif");
}

original_code:
void loadAllSprites()
{
 loadSprite(0, "data/world/player/playerDown.gif");
 loadSprite(1, "data/world/player/playerDownWalk1.gif");
 loadSprite(2, "data/world/player/playerUp.gif");
 loadSprite(3, "data/world/player/playerUpWalk1.gif");
 loadSprite(4, "data/world/player/playerRight.gif");
 loadSprite(5, "data/world/player/playerRightWalk.gif");
 loadSprite(6, "data/world/player/playerLeft.gif");
 loadSprite(7, "data/world/player/playerLeftWalk.gif");
}


updateScreen

decompiled_code:
__int64 updateScreen()
{
SDL_FillRect(screen, 0LL, 0LL);
SDL_Flip(screen);
return SDL_Delay(1LL);
}

canonical_decompiled_code:
__int64 func6()
{
SDL_FillRect(screen, 0LL, 0LL);
SDL_Flip(screen);
return SDL_Delay(1LL);
}

original_code:
void updateScreen()
{
 SDL_FillRect(screen, ((void *)0), 0);
 SDL_Flip(screen);
 SDL_Delay(1);
}


init

decompiled_code:
__int64 __fastcall init(__int64 a1)
{
unsigned int v1; // eax
__int64 result; // rax
const char *v3; // rax
const char *v4; // rax
const char *v5; // rax
const char *v6; // rax

v1 = time(0LL);
srand(v1);
setDefaultGenieStock();
if ( (unsigned int)threadInit() == 1 )
exit(4);
if ( (int)SDL_Init(32LL) < 0 )
{
v3 = (const char *)SDL_GetError();
printf("Could not initialize SDL: %s\n", v3);
exit(1);
}
screen = SDL_SetVideoMode(800LL, 600LL, 0LL, 1610612736LL);
if ( !screen )
{
v4 = (const char *)SDL_GetError();
printf("Couldn't set screen mode to 800 x 600: %s\n", v4);
exit(2);
}
SDL_WM_SetCaption(a1, 0LL);
if ( (unsigned int)TTF_Init() == -1 )
{
v5 = (const char *)SDL_GetError();
printf("SDL Initalization Error\nTTF_Init: %s", v5);
exit(3);
}
result = Mix_OpenAudio(22050LL, 32784LL, 2LL, 4096LL);
if ( (_DWORD)result == -1 )
{
v6 = (const char *)SDL_GetError();
printf("SDL Initialization Error\nMixer Failure: %s\n", v6);
exit(4);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
unsigned int v1; // eax
__int64 result; // rax
const char *v3; // rax
const char *v4; // rax
const char *v5; // rax
const char *v6; // rax

v1 = time(0LL);
srand(v1);
setDefaultGenieStock();
if ( (unsigned int)threadInit() == 1 )
exit(4);
if ( (int)SDL_Init(32LL) < 0 )
{
v3 = (const char *)SDL_GetError();
printf("Could not initialize SDL: %s\n", v3);
exit(1);
}
screen = SDL_SetVideoMode(800LL, 600LL, 0LL, 1610612736LL);
if ( !screen )
{
v4 = (const char *)SDL_GetError();
printf("Couldn't set screen mode to 800 x 600: %s\n", v4);
exit(2);
}
SDL_WM_SetCaption(a1, 0LL);
if ( (unsigned int)TTF_Init() == -1 )
{
v5 = (const char *)SDL_GetError();
printf("SDL Initalization Error\nTTF_Init: %s", v5);
exit(3);
}
result = Mix_OpenAudio(22050LL, 32784LL, 2LL, 4096LL);
if ( (_DWORD)result == -1 )
{
v6 = (const char *)SDL_GetError();
printf("SDL Initialization Error\nMixer Failure: %s\n", v6);
exit(4);
}
return result;
}

original_code:
void init(char *title)
{
 srand( time(((void *)0)));
 setDefaultGenieStock();
 if( threadInit() == 1) {
  exit(4);
 }
 if (SDL_Init(0x00000020) < 0)
 {
  printf("Could not initialize SDL: %s\n", SDL_GetError());
  exit(1);
 }
 screen = SDL_SetVideoMode(800, 600, 0, 0x20000000 | 0x40000000);
 if (screen == ((void *)0))
 {
  printf("Couldn't set screen mode to 800 x 600: %s\n", SDL_GetError());
  exit(2);
 }
 SDL_WM_SetCaption(title, ((void *)0));
 if(TTF_Init() == -1) {
  printf("SDL Initalization Error\nTTF_Init: %s", SDL_GetError());
  exit(3);
 }
 if( Mix_OpenAudio( 22050, 0x8010, 2, 4096 ) == -1 )
 {
  printf("SDL Initialization Error\nMixer Failure: %s\n", SDL_GetError());
  exit(4);
 }
}

variable_types:
title
char *


resetGame

decompiled_code:
__int64 resetGame()
{
setDefaultGenieStock();
initializePlayerStats();
initializeInventory();
return loadEntityPos("data/charPositionData.dat", &player, &stewie);
}

canonical_decompiled_code:
__int64 func1()
{
setDefaultGenieStock();
initializePlayerStats();
initializeInventory();
return loadEntityPos("data/charPositionData.dat", &player, &stewie);
}

original_code:
void resetGame() {
 setDefaultGenieStock();
 initializePlayerStats();
 initializeInventory();
 loadEntityPos("data/charPositionData.dat", &player, &stewie);
}


cleanup

decompiled_code:
__int64 cleanup()
{
threadCleanup();
if ( DUN_TILE )
SDL_FreeSurface();
if ( WAT_TILE )
SDL_FreeSurface();
if ( GRA_TILE )
SDL_FreeSurface();
if ( SNO_TILE )
SDL_FreeSurface();
if ( RED_TILE )
SDL_FreeSurface();
if ( DUN_WAT_R_TILE )
SDL_FreeSurface();
if ( DUN_WAT_L_TILE )
SDL_FreeSurface();
if ( DUN_WAT_T_TILE )
SDL_FreeSurface();
if ( DUN_WAT_B_TILE )
SDL_FreeSurface();
if ( DUN_WAT_TR_TILE )
SDL_FreeSurface();
if ( DUN_WAT_TL_TILE )
SDL_FreeSurface();
if ( DUN_WAT_BR_TILE )
SDL_FreeSurface();
if ( DUN_WAT_BL_TILE )
SDL_FreeSurface();
if ( DUN_EMP_R_TILE )
SDL_FreeSurface();
if ( DUN_EMP_L_TILE )
SDL_FreeSurface();
if ( DUN_EMP_T_TILE )
SDL_FreeSurface();
if ( DUN_EMP_TR_TILE )
SDL_FreeSurface();
if ( DUN_EMP_TL_TILE )
SDL_FreeSurface();
if ( TRE_OPN_TILE )
SDL_FreeSurface();
if ( TRE_CLD_TILE )
SDL_FreeSurface();
if ( stewie[3] )
SDL_FreeSurface();
if ( backgroundImage )
SDL_FreeSurface();
TTF_Quit();
freeAnimation(&playerAnimUp);
freeAnimation(&playerAnimDown);
freeAnimation(&playerAnimLeft);
freeAnimation(&playerAnimRight);
freeAnimation(&slashOne);
freeAnimation(&slashTwo);
free(potOne);
free(potTwo);
free(potThree);
free(potFour);
free(baseatk);
free(curatk);
free(basedef);
free(curdef);
free(basespd);
free(curspd);
free(maxhp);
free(curhp);
free(curexp);
free(curlvl);
free(gold);
free(dmgdisp1);
free(dmgdisp2);
free(potOneI);
free(potTwoI);
free(potThreeI);
free(potFourI);
free(swordOneI);
free(swordTwoI);
free(swordThreeI);
free(swordFourI);
free(armorOneI);
free(armorTwoI);
free(armorThreeI);
free(armorFourI);
if ( music )
Mix_FreeMusic();
Mix_CloseAudio();
return SDL_Quit();
}

canonical_decompiled_code:
__int64 func2()
{
threadCleanup();
if ( DUN_TILE )
SDL_FreeSurface();
if ( WAT_TILE )
SDL_FreeSurface();
if ( GRA_TILE )
SDL_FreeSurface();
if ( SNO_TILE )
SDL_FreeSurface();
if ( RED_TILE )
SDL_FreeSurface();
if ( DUN_WAT_R_TILE )
SDL_FreeSurface();
if ( DUN_WAT_L_TILE )
SDL_FreeSurface();
if ( DUN_WAT_T_TILE )
SDL_FreeSurface();
if ( DUN_WAT_B_TILE )
SDL_FreeSurface();
if ( DUN_WAT_TR_TILE )
SDL_FreeSurface();
if ( DUN_WAT_TL_TILE )
SDL_FreeSurface();
if ( DUN_WAT_BR_TILE )
SDL_FreeSurface();
if ( DUN_WAT_BL_TILE )
SDL_FreeSurface();
if ( DUN_EMP_R_TILE )
SDL_FreeSurface();
if ( DUN_EMP_L_TILE )
SDL_FreeSurface();
if ( DUN_EMP_T_TILE )
SDL_FreeSurface();
if ( DUN_EMP_TR_TILE )
SDL_FreeSurface();
if ( DUN_EMP_TL_TILE )
SDL_FreeSurface();
if ( TRE_OPN_TILE )
SDL_FreeSurface();
if ( TRE_CLD_TILE )
SDL_FreeSurface();
if ( stewie[3] )
SDL_FreeSurface();
if ( backgroundImage )
SDL_FreeSurface();
TTF_Quit();
freeAnimation(&playerAnimUp);
freeAnimation(&playerAnimDown);
freeAnimation(&playerAnimLeft);
freeAnimation(&playerAnimRight);
freeAnimation(&slashOne);
freeAnimation(&slashTwo);
free(potOne);
free(potTwo);
free(potThree);
free(potFour);
free(baseatk);
free(curatk);
free(basedef);
free(curdef);
free(basespd);
free(curspd);
free(maxhp);
free(curhp);
free(curexp);
free(curlvl);
free(gold);
free(dmgdisp1);
free(dmgdisp2);
free(potOneI);
free(potTwoI);
free(potThreeI);
free(potFourI);
free(swordOneI);
free(swordTwoI);
free(swordThreeI);
free(swordFourI);
free(armorOneI);
free(armorTwoI);
free(armorThreeI);
free(armorFourI);
if ( music )
Mix_FreeMusic();
Mix_CloseAudio();
return SDL_Quit();
}

original_code:
void cleanup()
{
 threadCleanup();
 if (DUN_TILE != ((void *)0)){
  SDL_FreeSurface(DUN_TILE);
 }
 if (WAT_TILE != ((void *)0)){
  SDL_FreeSurface(WAT_TILE);
 }
 if (GRA_TILE != ((void *)0)){
  SDL_FreeSurface(GRA_TILE);
 }
 if (SNO_TILE != ((void *)0)){
  SDL_FreeSurface(SNO_TILE);
 }
 if (RED_TILE != ((void *)0)){
  SDL_FreeSurface(RED_TILE);
 }
 if (DUN_WAT_R_TILE != ((void *)0)){
  SDL_FreeSurface(DUN_WAT_R_TILE);
 }
 if (DUN_WAT_L_TILE != ((void *)0)){
  SDL_FreeSurface(DUN_WAT_L_TILE);
 }
 if (DUN_WAT_T_TILE != ((void *)0)){
  SDL_FreeSurface(DUN_WAT_T_TILE);
 }
 if (DUN_WAT_B_TILE != ((void *)0)){
  SDL_FreeSurface(DUN_WAT_B_TILE);
 }
 if (DUN_WAT_TR_TILE != ((void *)0)){
  SDL_FreeSurface(DUN_WAT_TR_TILE);
 }
 if (DUN_WAT_TL_TILE != ((void *)0)){
  SDL_FreeSurface(DUN_WAT_TL_TILE);
 }
 if (DUN_WAT_BR_TILE != ((void *)0)){
  SDL_FreeSurface(DUN_WAT_BR_TILE);
 }
 if (DUN_WAT_BL_TILE != ((void *)0)){
  SDL_FreeSurface(DUN_WAT_BL_TILE);
 }
 if (DUN_EMP_R_TILE != ((void *)0)){
  SDL_FreeSurface(DUN_EMP_R_TILE);
 }
 if (DUN_EMP_L_TILE != ((void *)0)){
  SDL_FreeSurface(DUN_EMP_L_TILE);
 }
 if (DUN_EMP_T_TILE != ((void *)0)){
  SDL_FreeSurface(DUN_EMP_T_TILE);
 }
 if (DUN_EMP_TR_TILE != ((void *)0)){
  SDL_FreeSurface(DUN_EMP_TR_TILE);
 }
 if (DUN_EMP_TL_TILE != ((void *)0)){
  SDL_FreeSurface(DUN_EMP_TL_TILE);
 }
 if (TRE_OPN_TILE != ((void *)0)){
  SDL_FreeSurface(TRE_OPN_TILE);
 }
 if (TRE_CLD_TILE != ((void *)0)){
  SDL_FreeSurface(TRE_CLD_TILE);
 }
 if (stewie.sprite != ((void *)0)){
  SDL_FreeSurface(stewie.sprite);
 }
 if (backgroundImage != ((void *)0)){
  SDL_FreeSurface(backgroundImage);
 }
 TTF_Quit();
 freeAnimation(&playerAnimUp);
 freeAnimation(&playerAnimDown);
 freeAnimation(&playerAnimLeft);
 freeAnimation(&playerAnimRight);
 freeAnimation(&slashOne);
 freeAnimation(&slashTwo);
 free(potOne);
 free(potTwo);
 free(potThree);
 free(potFour);
 free(baseatk);
 free(curatk);
 free(basedef);
 free(curdef);
 free(basespd);
 free(curspd);
 free(maxhp);
 free(curhp);
 free(curexp);
 free(curlvl);
 free(gold);
 free(dmgdisp1);
 free(dmgdisp2);
  free(potOneI);
 free(potTwoI);
 free(potThreeI);
 free(potFourI);
  free(swordOneI);
  free(swordTwoI);
 free(swordThreeI);
 free(swordFourI);
  free(armorOneI);
 free(armorTwoI);
 free(armorThreeI);
 free(armorFourI);
 if(music != ((void *)0))
 {
  Mix_FreeMusic(music);
 }
 Mix_CloseAudio();
 SDL_Quit();
}