TextToPhonemes

decompiled_code:
__int64 __fastcall TextToPhonemes(__int64 a1)
{
char v1; // dl
char v2; // dl
char v3; // al
char v4; // dl
unsigned __int8 v5; // cl
unsigned __int8 v6; // al
char v7; // r13
char v9; // dl
char i; // r12
char v11; // al
int v12; // ebx
int v13; // ebp
unsigned __int8 v14; // bp
unsigned __int8 v15; // r12
unsigned __int8 v16; // r15
unsigned __int8 v17; // r12
unsigned __int8 v18; // r13
unsigned __int8 v19; // r12
unsigned __int8 v20; // bp
unsigned __int8 v21; // r12
unsigned __int8 v22; // bl
char v23; // al
char v24; // al
char v25; // al
unsigned __int8 v26; // al
unsigned __int8 v27; // bl
char v28; // al
unsigned __int8 v29; // al
char v30; // dl
char v31; // al
char v32; // al
char v33; // al
unsigned __int16 v34; // [rsp+Eh] [rbp-4Ah]
char v35; // [rsp+10h] [rbp-48h]
char v36; // [rsp+11h] [rbp-47h]
unsigned __int8 v37; // [rsp+12h] [rbp-46h]
unsigned __int8 v38; // [rsp+13h] [rbp-45h]
unsigned __int8 v39; // [rsp+14h] [rbp-44h]
unsigned __int8 v40; // [rsp+17h] [rbp-41h]

inputtemp[0] = 32;
X = 1;
Y = 0;
do
{
v3 = Y;
v4 = *(_BYTE *)(a1 + (unsigned __int8)Y);
v5 = v4 & 0x7F;
if ( (v4 & 0x7Fu) <= 0x6F )
{
v1 = v4 & 0x4F;
if ( v5 < 0x60u )
v1 = *(_BYTE *)(a1 + (unsigned __int8)Y) & 0x7F;
A = v1;
}
else
{
A = v4 & 0x5F;
}
v2 = X;
inputtemp[(unsigned __int8)X] = A;
X = v2 + 1;
Y = v3 + 1;
}
while ( v3 != -2 );
v38 = -1;
byte_A9F = 27;
X = 0;
v6 = inputtemp[0];
if ( inputtemp[0] == 91 )
{
LABEL_11:
X = v38 + 1;
A = -101;
*(_BYTE *)(a1 + (unsigned __int8)(v38 + 1)) = -101;
return 1LL;
}
v7 = -1;
v36 = 0;
while ( v6 == 46 )
{
Y = inputtemp[(unsigned __int8)(v7 + 2)];
if ( (tab36376[(unsigned __int8)Y] & 1) != 0 )
{
v34 = -27995;
goto LABEL_62;
}
*(_BYTE *)(a1 + ++v38) = 46;
v7 = v36;
LABEL_17:
v36 = v7 + 1;
X = v7 + 1;
v6 = inputtemp[(unsigned __int8)(v7 + 1)];
if ( v6 == 91 )
goto LABEL_11;
}
Y = v6;
v9 = tab36376[v6];
if ( (v9 & 2) != 0 )
{
v34 = -27995;
goto LABEL_62;
}
if ( !v9 )
{
inputtemp[(unsigned __int8)X] = 32;
X = ++v38;
if ( v38 > 0x78u )
{
*(_BYTE *)(a1 + v38) = -101;
A = v36;
return 1LL;
}
*(_BYTE *)(a1 + v38) = 32;
v7 = v36;
goto LABEL_17;
}
A = v9 & 0x80;
if ( v9 >= 0 )
return 0LL;
v34 = ((unsigned __int8)tab37515[(unsigned __int8)(v6 - 65)] << 8) | (unsigned __int8)tab37489[(unsigned __int8)(v6 - 65)];
do
LABEL_62:
++v34;
while ( (GetRuleByte(v34, 0) & 0x80u) == 0LL );
Y = 1;
v13 = 1;
for ( i = 0; ; i = 1 )
{
v11 = GetRuleByte(v34, v13);
v12 = v13++;
if ( v11 == 40 )
break;
}
if ( i )
Y = v12;
v14 = Y;
do
v15 = v14++;
while ( (unsigned __int8)GetRuleByte(v34, v14) != 41 );
v37 = v15;
v16 = v14;
do
v17 = v16++;
while ( (GetRuleByte(v34, v16) & 0x7F) != 61 );
v40 = v17;
X = v36;
v18 = v12 + 1;
v19 = v36;
v35 = 0;
v39 = v14;
v20 = v36;
while ( 1 )
{
if ( (unsigned __int8)GetRuleByte(v34, v18) != inputtemp[v19] )
goto LABEL_62;
++v18;
if ( v19 == v37 + v36 - ((_BYTE)v12 + 1) )
break;
v20 = ++v19;
v35 = 1;
}
v7 = v20;
if ( v35 )
X = v37 + v36 - (v12 + 1);
v21 = v36;
while ( 1 )
{
Y = --v12;
v25 = GetRuleByte(v34, v12);
if ( v25 < 0 )
break;
X = v25 & 0x7F;
if ( tab36376[v25 & 0x7F] >= 0 )
{
A = v25;
switch ( v25 )
{
case ' ':
Code37055(v21);
if ( A < 0 )
goto LABEL_62;
LABEL_52:
v21 = X;
continue;
case '#':
Code37055(v21);
if ( (A & 0x40) == 0 )
goto LABEL_62;
goto LABEL_52;
case '.':
Code37055(v21);
if ( (A & 8) == 0 )
goto LABEL_62;
LABEL_56:
v21 = X;
continue;
case '&':
Code37055(v21);
if ( (A & 0x10) == 0 )
{
v23 = X;
if ( inputtemp[(unsigned __int8)X] != 72 )
goto LABEL_62;
--X;
if ( (inputtemp[(unsigned __int8)(v23 - 1)] & 0xEF) != 67 )
goto LABEL_62;
}
goto LABEL_56;
case '@':
Code37055(v21);
if ( (A & 4) == 0 )
goto LABEL_62;
goto LABEL_56;
case '^':
Code37055(v21);
if ( (A & 0x20) == 0 )
goto LABEL_62;
LABEL_68:
v21 = X;
continue;
case '+':
X = v21 - 1;
v24 = inputtemp[(unsigned __int8)(v21 - 1)];
if ( (v24 & 0xEF) != 73 && v24 != 69 )
goto LABEL_62;
goto LABEL_68;
}
if ( v25 != 58 )
return 0LL;
while ( 1 )
{
Code37055(v21);
A &= 0x20u;
if ( !A )
break;
v21 = X;
}
}
else
{
X = --v21;
if ( v25 != inputtemp[v21] )
goto LABEL_62;
}
}
A = v20;
v22 = v37 + 2;
Y = v37 + 2;
if ( v39 == v40 )
{
LABEL_86:
Y = v16;
if ( debug )
PrintRule(v34);
v27 = v38;
while ( 1 )
{
v33 = GetRuleByte(v34, Y);
if ( (v33 & 0x7F) != 61 )
*(_BYTE *)(a1 + ++v27) = v33 & 0x7F;
if ( v33 < 0 )
break;
++Y;
}
v38 = v27;
goto LABEL_17;
}
while ( 2 )
{
v26 = GetRuleByte(v34, v22);
X = v26;
if ( tab36376[v26] < 0 )
{
X = ++v20;
A = inputtemp[v20];
if ( v26 != A )
goto LABEL_62;
goto LABEL_79;
}
A = v26;
switch ( v26 )
{
case ' ':
Code37066(v20);
v31 = A;
A &= 0x80u;
if ( v31 < 0 )
goto LABEL_62;
LABEL_108:
v20 = X;
LABEL_79:
Y = v22 + 1;
if ( v40 == v22 )
goto LABEL_86;
++v22;
continue;
case '#':
Code37066(v20);
A &= 0x40u;
if ( !A )
goto LABEL_62;
goto LABEL_108;
case '.':
Code37066(v20);
A &= 8u;
if ( !A )
goto LABEL_62;
LABEL_112:
v20 = X;
goto LABEL_79;
case '&':
Code37066(v20);
A &= 0x10u;
if ( !A )
{
v32 = X;
if ( inputtemp[(unsigned __int8)X] != 72 )
goto LABEL_62;
++X;
A = inputtemp[(unsigned __int8)(v32 + 1)];
if ( (A & 0xEF) != 67 )
goto LABEL_62;
}
goto LABEL_112;
case '@':
Code37066(v20);
A &= 4u;
if ( !A )
goto LABEL_62;
goto LABEL_112;
case '^':
Code37066(v20);
A &= 0x20u;
if ( !A )
goto LABEL_62;
LABEL_120:
v20 = X;
goto LABEL_79;
case '+':
X = v20 + 1;
A = inputtemp[(unsigned __int8)(v20 + 1)];
if ( (A & 0xEF) != 73 && A != 69 )
goto LABEL_62;
goto LABEL_120;
case ':':
while ( 1 )
{
Code37066(v20);
A &= 0x20u;
if ( !A )
break;
v20 = X;
}
goto LABEL_79;
case '%':
X = v20 + 1;
v28 = inputtemp[(unsigned __int8)(v20 + 1)];
if ( v28 == 69 )
{
v29 = inputtemp[(unsigned __int8)(v20 + 2)];
v30 = tab36376[v29];
A = v30 & 0x80;
if ( v30 < 0 )
{
X = v20 + 2;
A = v29;
if ( (unsigned __int8)(v29 - 82) > 1u && v29 != 68 )
{
if ( v29 == 76 )
{
X = v20 + 3;
A = inputtemp[(unsigned __int8)(v20 + 3)];
if ( A != 89 )
goto LABEL_62;
}
else
{
if ( v29 != 70 )
goto LABEL_62;
if ( inputtemp[(unsigned __int8)(v20 + 3)] != 85 )
goto LABEL_62;
X = v20 + 4;
A = inputtemp[(unsigned __int8)(v20 + 4)];
if ( A != 76 )
goto LABEL_62;
}
}
}
}
else
{
if ( v28 != 73 )
goto LABEL_62;
if ( inputtemp[(unsigned __int8)(v20 + 2)] != 78 )
goto LABEL_62;
X = v20 + 3;
A = inputtemp[(unsigned __int8)(v20 + 3)];
if ( A != 71 )
goto LABEL_62;
}
v20 = X;
goto LABEL_79;
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
char v1; // dl
char v2; // dl
char v3; // al
char v4; // dl
unsigned __int8 v5; // cl
unsigned __int8 v6; // al
char v7; // r13
char v9; // dl
char i; // r12
char v11; // al
int v12; // ebx
int v13; // ebp
unsigned __int8 v14; // bp
unsigned __int8 v15; // r12
unsigned __int8 v16; // r15
unsigned __int8 v17; // r12
unsigned __int8 v18; // r13
unsigned __int8 v19; // r12
unsigned __int8 v20; // bp
unsigned __int8 v21; // r12
unsigned __int8 v22; // bl
char v23; // al
char v24; // al
char v25; // al
unsigned __int8 v26; // al
unsigned __int8 v27; // bl
char v28; // al
unsigned __int8 v29; // al
char v30; // dl
char v31; // al
char v32; // al
char v33; // al
unsigned __int16 v34; // [rsp+Eh] [rbp-4Ah]
char v35; // [rsp+10h] [rbp-48h]
char v36; // [rsp+11h] [rbp-47h]
unsigned __int8 v37; // [rsp+12h] [rbp-46h]
unsigned __int8 v38; // [rsp+13h] [rbp-45h]
unsigned __int8 v39; // [rsp+14h] [rbp-44h]
unsigned __int8 v40; // [rsp+17h] [rbp-41h]

inputtemp[0] = 32;
X = 1;
Y = 0;
do
{
v3 = Y;
v4 = *(_BYTE *)(a1 + (unsigned __int8)Y);
v5 = v4 & 0x7F;
if ( (v4 & 0x7Fu) <= 0x6F )
{
v1 = v4 & 0x4F;
if ( v5 < 0x60u )
v1 = *(_BYTE *)(a1 + (unsigned __int8)Y) & 0x7F;
A = v1;
}
else
{
A = v4 & 0x5F;
}
v2 = X;
inputtemp[(unsigned __int8)X] = A;
X = v2 + 1;
Y = v3 + 1;
}
while ( v3 != -2 );
v38 = -1;
byte_A9F = 27;
X = 0;
v6 = inputtemp[0];
if ( inputtemp[0] == 91 )
{
LABEL_11:
X = v38 + 1;
A = -101;
*(_BYTE *)(a1 + (unsigned __int8)(v38 + 1)) = -101;
return 1LL;
}
v7 = -1;
v36 = 0;
while ( v6 == 46 )
{
Y = inputtemp[(unsigned __int8)(v7 + 2)];
if ( (tab36376[(unsigned __int8)Y] & 1) != 0 )
{
v34 = -27995;
goto LABEL_62;
}
*(_BYTE *)(a1 + ++v38) = 46;
v7 = v36;
LABEL_17:
v36 = v7 + 1;
X = v7 + 1;
v6 = inputtemp[(unsigned __int8)(v7 + 1)];
if ( v6 == 91 )
goto LABEL_11;
}
Y = v6;
v9 = tab36376[v6];
if ( (v9 & 2) != 0 )
{
v34 = -27995;
goto LABEL_62;
}
if ( !v9 )
{
inputtemp[(unsigned __int8)X] = 32;
X = ++v38;
if ( v38 > 0x78u )
{
*(_BYTE *)(a1 + v38) = -101;
A = v36;
return 1LL;
}
*(_BYTE *)(a1 + v38) = 32;
v7 = v36;
goto LABEL_17;
}
A = v9 & 0x80;
if ( v9 >= 0 )
return 0LL;
v34 = ((unsigned __int8)tab37515[(unsigned __int8)(v6 - 65)] << 8) | (unsigned __int8)tab37489[(unsigned __int8)(v6 - 65)];
do
LABEL_62:
++v34;
while ( (func2(v34, 0) & 0x80u) == 0LL );
Y = 1;
v13 = 1;
for ( i = 0; ; i = 1 )
{
v11 = func2(v34, v13);
v12 = v13++;
if ( v11 == 40 )
break;
}
if ( i )
Y = v12;
v14 = Y;
do
v15 = v14++;
while ( (unsigned __int8)func2(v34, v14) != 41 );
v37 = v15;
v16 = v14;
do
v17 = v16++;
while ( (func2(v34, v16) & 0x7F) != 61 );
v40 = v17;
X = v36;
v18 = v12 + 1;
v19 = v36;
v35 = 0;
v39 = v14;
v20 = v36;
while ( 1 )
{
if ( (unsigned __int8)func2(v34, v18) != inputtemp[v19] )
goto LABEL_62;
++v18;
if ( v19 == v37 + v36 - ((_BYTE)v12 + 1) )
break;
v20 = ++v19;
v35 = 1;
}
v7 = v20;
if ( v35 )
X = v37 + v36 - (v12 + 1);
v21 = v36;
while ( 1 )
{
Y = --v12;
v25 = func2(v34, v12);
if ( v25 < 0 )
break;
X = v25 & 0x7F;
if ( tab36376[v25 & 0x7F] >= 0 )
{
A = v25;
switch ( v25 )
{
case ' ':
func0(v21);
if ( A < 0 )
goto LABEL_62;
LABEL_52:
v21 = X;
continue;
case '#':
func0(v21);
if ( (A & 0x40) == 0 )
goto LABEL_62;
goto LABEL_52;
case '.':
func0(v21);
if ( (A & 8) == 0 )
goto LABEL_62;
LABEL_56:
v21 = X;
continue;
case '&':
func0(v21);
if ( (A & 0x10) == 0 )
{
v23 = X;
if ( inputtemp[(unsigned __int8)X] != 72 )
goto LABEL_62;
--X;
if ( (inputtemp[(unsigned __int8)(v23 - 1)] & 0xEF) != 67 )
goto LABEL_62;
}
goto LABEL_56;
case '@':
func0(v21);
if ( (A & 4) == 0 )
goto LABEL_62;
goto LABEL_56;
case '^':
func0(v21);
if ( (A & 0x20) == 0 )
goto LABEL_62;
LABEL_68:
v21 = X;
continue;
case '+':
X = v21 - 1;
v24 = inputtemp[(unsigned __int8)(v21 - 1)];
if ( (v24 & 0xEF) != 73 && v24 != 69 )
goto LABEL_62;
goto LABEL_68;
}
if ( v25 != 58 )
return 0LL;
while ( 1 )
{
func0(v21);
A &= 0x20u;
if ( !A )
break;
v21 = X;
}
}
else
{
X = --v21;
if ( v25 != inputtemp[v21] )
goto LABEL_62;
}
}
A = v20;
v22 = v37 + 2;
Y = v37 + 2;
if ( v39 == v40 )
{
LABEL_86:
Y = v16;
if ( debug )
PrintRule(v34);
v27 = v38;
while ( 1 )
{
v33 = func2(v34, Y);
if ( (v33 & 0x7F) != 61 )
*(_BYTE *)(a1 + ++v27) = v33 & 0x7F;
if ( v33 < 0 )
break;
++Y;
}
v38 = v27;
goto LABEL_17;
}
while ( 2 )
{
v26 = func2(v34, v22);
X = v26;
if ( tab36376[v26] < 0 )
{
X = ++v20;
A = inputtemp[v20];
if ( v26 != A )
goto LABEL_62;
goto LABEL_79;
}
A = v26;
switch ( v26 )
{
case ' ':
func1(v20);
v31 = A;
A &= 0x80u;
if ( v31 < 0 )
goto LABEL_62;
LABEL_108:
v20 = X;
LABEL_79:
Y = v22 + 1;
if ( v40 == v22 )
goto LABEL_86;
++v22;
continue;
case '#':
func1(v20);
A &= 0x40u;
if ( !A )
goto LABEL_62;
goto LABEL_108;
case '.':
func1(v20);
A &= 8u;
if ( !A )
goto LABEL_62;
LABEL_112:
v20 = X;
goto LABEL_79;
case '&':
func1(v20);
A &= 0x10u;
if ( !A )
{
v32 = X;
if ( inputtemp[(unsigned __int8)X] != 72 )
goto LABEL_62;
++X;
A = inputtemp[(unsigned __int8)(v32 + 1)];
if ( (A & 0xEF) != 67 )
goto LABEL_62;
}
goto LABEL_112;
case '@':
func1(v20);
A &= 4u;
if ( !A )
goto LABEL_62;
goto LABEL_112;
case '^':
func1(v20);
A &= 0x20u;
if ( !A )
goto LABEL_62;
LABEL_120:
v20 = X;
goto LABEL_79;
case '+':
X = v20 + 1;
A = inputtemp[(unsigned __int8)(v20 + 1)];
if ( (A & 0xEF) != 73 && A != 69 )
goto LABEL_62;
goto LABEL_120;
case ':':
while ( 1 )
{
func1(v20);
A &= 0x20u;
if ( !A )
break;
v20 = X;
}
goto LABEL_79;
case '%':
X = v20 + 1;
v28 = inputtemp[(unsigned __int8)(v20 + 1)];
if ( v28 == 69 )
{
v29 = inputtemp[(unsigned __int8)(v20 + 2)];
v30 = tab36376[v29];
A = v30 & 0x80;
if ( v30 < 0 )
{
X = v20 + 2;
A = v29;
if ( (unsigned __int8)(v29 - 82) > 1u && v29 != 68 )
{
if ( v29 == 76 )
{
X = v20 + 3;
A = inputtemp[(unsigned __int8)(v20 + 3)];
if ( A != 89 )
goto LABEL_62;
}
else
{
if ( v29 != 70 )
goto LABEL_62;
if ( inputtemp[(unsigned __int8)(v20 + 3)] != 85 )
goto LABEL_62;
X = v20 + 4;
A = inputtemp[(unsigned __int8)(v20 + 4)];
if ( A != 76 )
goto LABEL_62;
}
}
}
}
else
{
if ( v28 != 73 )
goto LABEL_62;
if ( inputtemp[(unsigned __int8)(v20 + 2)] != 78 )
goto LABEL_62;
X = v20 + 3;
A = inputtemp[(unsigned __int8)(v20 + 3)];
if ( A != 71 )
goto LABEL_62;
}
v20 = X;
goto LABEL_79;
}
}
return 0LL;
}

original_code:
int TextToPhonemes(unsigned char *input)
{
    unsigned char mem56;
    unsigned char mem57;
    unsigned char mem58;
    unsigned char mem59;
    unsigned char mem60;
    unsigned char mem61;
    unsigned short mem62;
    unsigned char mem64;
    unsigned char mem65;
    unsigned char mem66;
    unsigned char mem36653;
    inputtemp[0] = 32;
    X = 1;
    Y = 0;
    do
    {
        A = input[Y] & 127;
        if ( A >= 112) A = A & 95;
        else if ( A >= 96) A = A & 79;
        inputtemp[X] = A;
        X++;
        Y++;
    } while (Y != 255);
    X = 255;
    inputtemp[X] = 27;
    mem61 = 255;
pos36550:
    A = 255;
    mem56 = 255;
pos36554:
    while(1)
    {
        mem61++;
        X = mem61;
        A = inputtemp[X];
        mem64 = A;
        if (A == '[')
        {
            mem56++;
            X = mem56;
            A = 155;
            input[X] = 155;
            return 1;
        }
        if (A != '.') break;
        X++;
        Y = inputtemp[X];
        A = tab36376[Y] & 1;
        if(A != 0) break;
        mem56++;
        X = mem56;
        A = '.';
        input[X] = '.';
    }
    A = mem64;
    Y = A;
    A = tab36376[A];
    mem57 = A;
    if((A&2) != 0)
    {
        mem62 = 37541;
        goto pos36700;
    }
    A = mem57;
    if(A != 0) goto pos36677;
    A = 32;
    inputtemp[X] = ' ';
    mem56++;
    X = mem56;
    if (X > 120) goto pos36654;
    input[X] = A;
    goto pos36554;
pos36654:
    input[X] = 155;
    A = mem61;
    mem36653 = A;
    return 1;
    mem61 = mem36653;
    goto pos36550;
pos36677:
    A = mem57 & 128;
    if(A == 0)
    {
        return 0;
    }
    X = mem64 - 'A';
    mem62 = tab37489[X] | (tab37515[X]<<8);
pos36700:
    Y = 0;
    do
    {
        mem62 += 1;
        A = GetRuleByte(mem62, Y);
    } while ((A & 128) == 0);
    Y++;
    while(1)
    {
        A = GetRuleByte(mem62, Y);
        if (A == '(') break;
        Y++;
    }
    mem66 = Y;
    do
    {
        Y++;
        A = GetRuleByte(mem62, Y);
    } while(A != ')');
    mem65 = Y;
    do
    {
        Y++;
        A = GetRuleByte(mem62, Y);
        A = A & 127;
    } while (A != '=');
    mem64 = Y;
    X = mem61;
    mem60 = X;
    Y = mem66;
    Y++;
    while(1)
    {
        mem57 = inputtemp[X];
        A = GetRuleByte(mem62, Y);
        if (A != mem57) goto pos36700;
        Y++;
        if(Y == mem65) break;
        X++;
        mem60 = X;
    }
    A = mem61;
    mem59 = mem61;
pos36791:
    while(1)
    {
        mem66--;
        Y = mem66;
        A = GetRuleByte(mem62, Y);
        mem57 = A;
        if ((A & 128) != 0) goto pos37180;
        X = A & 127;
        A = tab36376[X] & 128;
        if (A == 0) break;
        X = mem59-1;
        A = inputtemp[X];
        if (A != mem57) goto pos36700;
        mem59 = X;
    }
    A = mem57;
    if (A == ' ') goto pos36895;
    if (A == '#') goto pos36910;
    if (A == '.') goto pos36920;
    if (A == '&') goto pos36935;
    if (A == '@') goto pos36967;
    if (A == '^') goto pos37004;
    if (A == '+') goto pos37019;
    if (A == ':') goto pos37040;
    return 0;
pos36895:
    Code37055(mem59);
    A = A & 128;
    if(A != 0) goto pos36700;
pos36905:
    mem59 = X;
    goto pos36791;
pos36910:
    Code37055(mem59);
    A = A & 64;
    if(A != 0) goto pos36905;
    goto pos36700;
pos36920:
    Code37055(mem59);
    A = A & 8;
    if(A == 0) goto pos36700;
pos36930:
    mem59 = X;
    goto pos36791;
pos36935:
    Code37055(mem59);
    A = A & 16;
    if(A != 0) goto pos36930;
    A = inputtemp[X];
    if (A != 72) goto pos36700;
    X--;
    A = inputtemp[X];
    if ((A == 67) || (A == 83)) goto pos36930;
    goto pos36700;
pos36967:
    Code37055(mem59);
    A = A & 4;
    if(A != 0) goto pos36930;
    A = inputtemp[X];
    if (A != 72) goto pos36700;
    if ((A != 84) && (A != 67) && (A != 83)) goto pos36700;
    mem59 = X;
    goto pos36791;
pos37004:
    Code37055(mem59);
    A = A & 32;
    if(A == 0) goto pos36700;
pos37014:
    mem59 = X;
    goto pos36791;
pos37019:
    X = mem59;
    X--;
    A = inputtemp[X];
    if ((A == 'E') || (A == 'I') || (A == 'Y')) goto pos37014;
    goto pos36700;
pos37040:
    Code37055(mem59);
    A = A & 32;
    if(A == 0) goto pos36791;
    mem59 = X;
    goto pos37040;
pos37077:
    X = mem58+1;
    A = inputtemp[X];
    if (A != 'E') goto pos37157;
    X++;
    Y = inputtemp[X];
    X--;
    A = tab36376[Y] & 128;
    if(A == 0) goto pos37108;
    X++;
    A = inputtemp[X];
    if (A != 'R') goto pos37113;
pos37108:
    mem58 = X;
    goto pos37184;
pos37113:
    if ((A == 83) || (A == 68)) goto pos37108;
    if (A != 76) goto pos37135;
    X++;
    A = inputtemp[X];
    if (A != 89) goto pos36700;
    goto pos37108;
pos37135:
    if (A != 70) goto pos36700;
    X++;
    A = inputtemp[X];
    if (A != 85) goto pos36700;
    X++;
    A = inputtemp[X];
    if (A == 76) goto pos37108;
    goto pos36700;
pos37157:
    if (A != 73) goto pos36700;
    X++;
    A = inputtemp[X];
    if (A != 78) goto pos36700;
    X++;
    A = inputtemp[X];
    if (A == 71) goto pos37108;
    goto pos36700;
pos37180:
    A = mem60;
    mem58 = A;
pos37184:
    Y = mem65 + 1;
    if(Y == mem64) goto pos37455;
    mem65 = Y;
    A = GetRuleByte(mem62, Y);
    mem57 = A;
    X = A;
    A = tab36376[X] & 128;
    if(A == 0) goto pos37226;
    X = mem58+1;
    A = inputtemp[X];
    if (A != mem57) goto pos36700;
    mem58 = X;
    goto pos37184;
pos37226:
    A = mem57;
    if (A == 32) goto pos37295;
    if (A == 35) goto pos37310;
    if (A == 46) goto pos37320;
    if (A == 38) goto pos37335;
    if (A == 64) goto pos37367;
    if (A == 94) goto pos37404;
    if (A == 43) goto pos37419;
    if (A == 58) goto pos37440;
    if (A == 37) goto pos37077;
    return 0;
pos37295:
    Code37066(mem58);
    A = A & 128;
    if(A != 0) goto pos36700;
pos37305:
    mem58 = X;
    goto pos37184;
pos37310:
    Code37066(mem58);
    A = A & 64;
    if(A != 0) goto pos37305;
    goto pos36700;
pos37320:
    Code37066(mem58);
    A = A & 8;
    if(A == 0) goto pos36700;
pos37330:
    mem58 = X;
    goto pos37184;
pos37335:
    Code37066(mem58);
    A = A & 16;
    if(A != 0) goto pos37330;
    A = inputtemp[X];
    if (A != 72) goto pos36700;
    X++;
    A = inputtemp[X];
    if ((A == 67) || (A == 83)) goto pos37330;
    goto pos36700;
pos37367:
    Code37066(mem58);
    A = A & 4;
    if(A != 0) goto pos37330;
    A = inputtemp[X];
    if (A != 72) goto pos36700;
    if ((A != 84) && (A != 67) && (A != 83)) goto pos36700;
    mem58 = X;
    goto pos37184;
pos37404:
    Code37066(mem58);
    A = A & 32;
    if(A == 0) goto pos36700;
pos37414:
    mem58 = X;
    goto pos37184;
pos37419:
    X = mem58;
    X++;
    A = inputtemp[X];
    if ((A == 69) || (A == 73) || (A == 89)) goto pos37414;
    goto pos36700;
pos37440:
    Code37066(mem58);
    A = A & 32;
    if(A == 0) goto pos37184;
    mem58 = X;
    goto pos37440;
pos37455:
    Y = mem64;
    mem61 = mem60;
    if (debug)
        PrintRule(mem62);
pos37461:
    A = GetRuleByte(mem62, Y);
    mem57 = A;
    A = A & 127;
    if (A != '=')
    {
        mem56++;
        X = mem56;
        input[X] = A;
    }
    if ((mem57 & 128) == 0) goto pos37485;
    goto pos36554;
pos37485:
    Y++;
    goto pos37461;
}

variable_types:
input
unsigned char *


getContext

repo: s-maruyama/exrepair

decompiled_code:
__int64 __fastcall getContext(int a1, int a2, unsigned int a3, __int64 a4)
{
int v4; // esi
__int64 v7; // rbx
double v8; // xmm0_8
__int64 result; // rax

v4 = a2 - 1;
if ( v4 >= 0 )
{
v7 = v4;
do
{
v8 = pow((double)a1, (double)(int)v7);
result = a3 / (int)v8;
*(_BYTE *)(a4 + v7) = result;
a3 %= (unsigned int)(int)v8;
--v7;
}
while ( (int)v7 >= 0 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1, int a2, unsigned int a3, __int64 a4)
{
int v4; // esi
__int64 v7; // rbx
double v8; // xmm0_8
__int64 result; // rax

v4 = a2 - 1;
if ( v4 >= 0 )
{
v7 = v4;
do
{
v8 = pow((double)a1, (double)(int)v7);
result = a3 / (int)v8;
*(_BYTE *)(a4 + v7) = result;
a3 %= (unsigned int)(int)v8;
--v7;
}
while ( (int)v7 >= 0 );
}
return result;
}

original_code:
void getContext(uint mchar_size, uint cont_len, PCODE id, uchar *context)
{
  int i;
  uint div;
  uint mod;
  for (i = cont_len - 1; i >= 0; i--) {
    div = pow(mchar_size, i);
    mod = id / div;
    context[i] = mod;
    id -= div*mod;
  }
}

canonical_original_code:
void getContext(unsigned int mchar_size, unsigned int cont_len, unsigned int id, unsigned char *context)
{
  int i;
  unsigned int div;
  unsigned int mod;
  for (i = cont_len - 1; i >= 0; i--) {
    div = pow(mchar_size, i);
    mod = id / div;
    context[i] = mod;
    id -= div*mod;
  }
}

variable_types:
id
unsigned int

variable_types:
context
unsigned char *

variable_types: i int

variable_types:
div
unsigned int

variable_types:
mod
unsigned int

return_type: void


getContextBegID

decompiled_code:
__int64 __fastcall getContextBegID(int a1, int a2, __int64 a3, int a4)
{
__int64 v4; // rbx
int v6; // ebp
int v7; // esi
double v9; // [rsp+8h] [rbp-30h]

LODWORD(v4) = a4 - 1;
if ( a4 - 1 < 0 )
{
v6 = 0;
}
else
{
v4 = (int)v4;
v6 = 0;
v7 = a2 - a4;
do
{
v9 = (double)*(unsigned __int8 *)(a3 + v4);
v6 = (int)((double)v6 + pow((double)a1, (double)(v7 + (int)v4--)) * v9);
}
while ( (int)v4 >= 0 );
}
return (unsigned int)v6;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, int a2, __int64 a3, int a4)
{
__int64 v4; // rbx
int v6; // ebp
int v7; // esi
double v9; // [rsp+8h] [rbp-30h]

LODWORD(v4) = a4 - 1;
if ( a4 - 1 < 0 )
{
v6 = 0;
}
else
{
v4 = (int)v4;
v6 = 0;
v7 = a2 - a4;
do
{
v9 = (double)*(unsigned __int8 *)(a3 + v4);
v6 = (int)((double)v6 + pow((double)a1, (double)(v7 + (int)v4--)) * v9);
}
while ( (int)v4 >= 0 );
}
return (unsigned int)v6;
}

original_code:
PCODE getContextBegID(uint mchar_size, uint cont_len,
        uchar *cont_suffix, uint suffix_len)
{
  int i;
  PCODE id = 0;
  for (i = suffix_len-1; i >= 0; i--) {
    id += cont_suffix[i] * pow(mchar_size, i + cont_len - suffix_len);
  }
  return id;
}

canonical_original_code:
unsigned int getContextBegID(unsigned int mchar_size, unsigned int cont_len, unsigned char *cont_suffix, unsigned int suffix_len)
{
  int i;
  unsigned int id = 0;
  for (i = suffix_len-1; i >= 0; i--) {
    id += cont_suffix[i] * pow(mchar_size, i + cont_len - suffix_len);
  }
  return id;
}

return_type: unsigned int


getContextEndID

decompiled_code:
__int64 __fastcall getContextEndID(int a1, int a2, __int64 a3, int a4)
{
__int64 v4; // rbx
int v6; // ebp
int v7; // er14
double v9; // [rsp+0h] [rbp-48h]

LODWORD(v4) = a4 - 1;
if ( a4 - 1 < 0 )
{
v6 = 0;
}
else
{
v4 = (int)v4;
v6 = 0;
v7 = a2 - a4;
do
{
v9 = (double)*(unsigned __int8 *)(a3 + v4);
v6 = (int)((double)v6 + pow((double)a1, (double)(v7 + (int)v4--)) * v9);
}
while ( (int)v4 >= 0 );
}
return (unsigned int)(int)((double)v6 + pow((double)a1, (double)(a2 - a4)) - 1.0);
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, int a2, __int64 a3, int a4)
{
__int64 v4; // rbx
int v6; // ebp
int v7; // er14
double v9; // [rsp+0h] [rbp-48h]

LODWORD(v4) = a4 - 1;
if ( a4 - 1 < 0 )
{
v6 = 0;
}
else
{
v4 = (int)v4;
v6 = 0;
v7 = a2 - a4;
do
{
v9 = (double)*(unsigned __int8 *)(a3 + v4);
v6 = (int)((double)v6 + pow((double)a1, (double)(v7 + (int)v4--)) * v9);
}
while ( (int)v4 >= 0 );
}
return (unsigned int)(int)((double)v6 + pow((double)a1, (double)(a2 - a4)) - 1.0);
}

original_code:
PCODE getContextEndID(uint mchar_size, uint cont_len,
        uchar *cont_suffix, uint suffix_len)
{
  int i;
  PCODE id = 0;
  for (i = suffix_len-1; i >= 0; i--) {
    id += cont_suffix[i] * pow(mchar_size, i + cont_len - suffix_len);
  }
  id += pow(mchar_size, cont_len - suffix_len) - 1;
  return id;
}

canonical_original_code:
unsigned int getContextEndID(unsigned int mchar_size, unsigned int cont_len, unsigned char *cont_suffix, unsigned int suffix_len)
{
  int i;
  unsigned int id = 0;
  for (i = suffix_len-1; i >= 0; i--) {
    id += cont_suffix[i] * pow(mchar_size, i + cont_len - suffix_len);
  }
  id += pow(mchar_size, cont_len - suffix_len) - 1;
  return id;
}


getContextID

decompiled_code:
__int64 __fastcall getContextID(int a1, int a2, __int64 a3)
{
return getContextBegID(a1, a2, a3, a2);
}

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

original_code:
PCODE getContextID(uint mchar_size, uint cont_len, uchar *cont)
{
  return getContextBegID(mchar_size, cont_len, cont, cont_len);
}

canonical_original_code:
unsigned int getContextID(unsigned int mchar_size, unsigned int cont_len, unsigned char *cont)
{
  return getContextBegID(mchar_size, cont_len, cont, cont_len);
}

variable_types:
cont
unsigned char *


getContextRangeSize

decompiled_code:
__int64 __fastcall getContextRangeSize(int a1, int a2, int a3)
{
return (unsigned int)(int)pow((double)a1, (double)(a2 - a3));
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, int a2, int a3)
{
return (unsigned int)(int)pow((double)a1, (double)(a2 - a3));
}

original_code:
uint getContextRangeSize(uint mchar_size, uint cont_len, uint suffix_len)
{
  return (uint)pow(mchar_size, (cont_len - suffix_len));
}

canonical_original_code:
unsigned int getContextRangeSize(unsigned int mchar_size, unsigned int cont_len, unsigned int suffix_len)
{
  return (unsigned int)pow(mchar_size, (cont_len - suffix_len));
}


decompiled_code:
void *__fastcall init_depth_buffer(int a1, int a2)
{
void *result; // rax

depth_buffer_size = a2 * a1;
result = malloc(4LL * a2 * a1);
depth_buffer = result;
return result;
}

canonical_decompiled_code:
void *__fastcall func0(int a1, int a2)
{
void *result; // rax

depth_buffer_size = a2 * a1;
result = malloc(4LL * a2 * a1);
depth_buffer = result;
return result;
}

original_code:
void init_depth_buffer(int width, int height) {
  depth_buffer_size = width*height;
  depth_buffer = (unsigned int*)malloc(depth_buffer_size*sizeof(unsigned int));
}

canonical_original_code:
void init_depth_buffer(int width, int height) {
  depth_buffer_size = width*height;
  depth_buffer = (unsigned int *)malloc(depth_buffer_size*sizeof(unsigned int));
}

variable_types: width int

variable_types: height int


decompiled_code:
void clear_depth_buffer()
{
__int64 v0; // rax

if ( depth_buffer_size > 0 )
{
v0 = 0LL;
do
*((_DWORD *)depth_buffer + v0++) = -1;
while ( depth_buffer_size > (int)v0 );
}
}

canonical_decompiled_code:
void func1()
{
__int64 v0; // rax

if ( depth_buffer_size > 0 )
{
v0 = 0LL;
do
*((_DWORD *)depth_buffer + v0++) = -1;
while ( depth_buffer_size > (int)v0 );
}
}

original_code:
void clear_depth_buffer() {
  int i;
  for(i=0;i<depth_buffer_size; i++) {
    depth_buffer[i] = (0x7fffffff * 2U + 1U);
  }
}


decompiled_code:
void destroy_depth_buffer()
{
free(depth_buffer);
depth_buffer = 0LL;
}

canonical_decompiled_code:
void func2()
{
free(depth_buffer);
depth_buffer = 0LL;
}

original_code:
void destroy_depth_buffer() {
  free(depth_buffer);
  depth_buffer = ((void *)0);
}


decompiled_code:
__int64 __fastcall interp_colors(int a1, int a2, int a3, float a4, float a5, float a6)
{
unsigned __int8 v6; // cl
float v7; // xmm3_4
int v8; // eax
float v9; // xmm3_4
int v10; // esi
float v11; // xmm2_4
int v12; // edx

v6 = a2;
v7 = (float)((float)BYTE2(a3) * a6) + (float)((float)((float)BYTE2(a1) * a4) + (float)((float)BYTE2(a2) * a5));
v8 = 0;
if ( v7 >= 0.0 )
{
v8 = 255;
if ( v7 <= 255.0 )
v8 = (int)v7;
}
v9 = (float)((float)BYTE1(a3) * a6) + (float)((float)((float)BYTE1(a1) * a4) + (float)((float)BYTE1(a2) * a5));
v10 = 0;
if ( v9 >= 0.0 )
{
v10 = 255;
if ( v9 <= 255.0 )
v10 = (int)v9;
}
v11 = (float)((float)((float)(unsigned __int8)a1 * a4) + (float)(a5 * (float)v6))
+ (float)(a6 * (float)(unsigned __int8)a3);
v12 = 0;
if ( v11 >= 0.0 )
{
v12 = 255;
if ( v11 <= 255.0 )
v12 = (int)v11;
}
return (unsigned int)(v12 + (v10 << 8) + (v8 << 16));
}

variable_types: a float

variable_types: b float

variable_types: c float

variable_types: red int

variable_types: green int

variable_types: blue int

variable_types:
interp
unsigned int


decompiled_code:
__int64 __fastcall interp_uint(int a1, int a2, int a3, float a4, float a5, float a6)
{
return (unsigned int)(int)(float)((float)((float)((float)a1 * a4) + (float)((float)a2 * a5)) + (float)((float)a3 * a6));
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, int a2, int a3, float a4, float a5, float a6)
{
return (unsigned int)(int)(float)((float)((float)((float)a1 * a4) + (float)((float)a2 * a5)) + (float)((float)a3 * a6));
}

original_code:
unsigned int interp_uint(unsigned int x, unsigned int y, unsigned int z, float a, float b, float c) {
  return a*x + b*y + c*z;
}

variable_types:
x
unsigned int

variable_types:
y
unsigned int

variable_types:
z
unsigned int


rasterize

decompiled_code:
int __fastcall rasterize(int *a1, __int64 a2, int a3)
{
unsigned int *v3; // rax
int j; // ebx
int v5; // er15
float v6; // xmm1_4
float v7; // xmm2_4
float v8; // xmm6_4
float v9; // xmm0_4
float v10; // xmm1_4
unsigned int v11; // edx
unsigned int v12; // eax
int v13; // er12
double v14; // rbx
double v15; // r14
double v16; // r15
double v17; // xmm1_8
__m128d v18; // xmm0
double v19; // xmm2_8
__m128d v20; // xmm2
double v21; // xmm1_8
double v22; // xmm0_8
__m128d v23; // xmm2
__m128d v24; // xmm1
int v25; // ebp
double v26; // xmm1_8
__m128d v27; // xmm0
double v28; // xmm2_8
__m128d v29; // xmm2
double v30; // xmm1_8
double v31; // xmm0_8
__m128d v32; // xmm2
__m128d v33; // xmm1
float v34; // xmm3_4
float v35; // xmm8_4
float v36; // xmm9_4
float v37; // xmm10_4
float v38; // xmm7_4
double x; // [rsp+0h] [rbp-B8h]
float v41; // [rsp+Ch] [rbp-ACh]
float v42; // [rsp+Ch] [rbp-ACh]
float v43; // [rsp+10h] [rbp-A8h]
float v44; // [rsp+10h] [rbp-A8h]
float v45; // [rsp+14h] [rbp-A4h]
float v46; // [rsp+14h] [rbp-A4h]
float v47; // [rsp+18h] [rbp-A0h]
float v48; // [rsp+18h] [rbp-A0h]
float v49; // [rsp+1Ch] [rbp-9Ch]
float i; // [rsp+1Ch] [rbp-9Ch]
float v51; // [rsp+20h] [rbp-98h]
int v52; // [rsp+38h] [rbp-80h]
int v53; // [rsp+3Ch] [rbp-7Ch]
int v54; // [rsp+58h] [rbp-60h]
int v55; // [rsp+5Ch] [rbp-5Ch]
int v56; // [rsp+60h] [rbp-58h]
int v57; // [rsp+64h] [rbp-54h]
int v58; // [rsp+68h] [rbp-50h]
int v59; // [rsp+6Ch] [rbp-4Ch]
int v61; // [rsp+78h] [rbp-40h]

if ( !depth_buffer )
init_depth_buffer(*a1, a1[1]);
clear_depth_buffer();
fwrite("rasterization stage started...\n", 1uLL, 0x1FuLL, stderr);
LODWORD(v3) = fprintf(stderr, "%d triangles\n", (unsigned int)a3);
if ( a3 > 0 )
{
v57 = 0;
do
{
v49 = *(float *)a2;
v51 = *(float *)(a2 + 4);
v54 = *(_DWORD *)(a2 + 8);
v58 = *(_DWORD *)(a2 + 12);
v43 = *(float *)(a2 + 16);
v41 = *(float *)(a2 + 20);
v55 = *(_DWORD *)(a2 + 24);
v59 = *(_DWORD *)(a2 + 28);
v45 = *(float *)(a2 + 32);
v47 = *(float *)(a2 + 36);
v56 = *(_DWORD *)(a2 + 40);
v61 = *(_DWORD *)(a2 + 44);
v14 = v47;
v15 = v41;
v16 = v51;
x = *(float *)a2;
fprintf(
stderr,
"Triangle %d with vertices: (%f, %f), (%f, %f), (%f, %f)\n",
(unsigned int)++v57,
x,
v51,
v43,
v41,
v45,
v47);
v17 = fmin(v43, v45);
v18.m128d_f64[0] = fmin(x, v17);
v19 = v18.m128d_f64[0];
if ( COERCE_DOUBLE(LC8 & *(_QWORD *)&v18.m128d_f64[0]) < 4.503599627370496e15 )
{
v20.m128d_f64[0] = (double)(int)v18.m128d_f64[0];
*(_QWORD *)&v19 = ~LC8 & *(_QWORD *)&v18.m128d_f64[0] | COERCE_UNSIGNED_INT64(
v20.m128d_f64[0]
- COERCE_DOUBLE(*(_OWORD *)&_mm_cmpgt_sd(v20, v18) & LC7));
}
v52 = (int)v19;
v21 = fmax(v43, v45);
v22 = fmax(x, v21);
v23.m128d_f64[0] = v22;
if ( COERCE_DOUBLE(LC8 & *(_QWORD *)&v22) < 4.503599627370496e15 )
{
v24.m128d_f64[0] = (double)(int)v22;
*(_QWORD *)&v23.m128d_f64[0] = ~LC8 & *(_QWORD *)&v22 | COERCE_UNSIGNED_INT64(
v24.m128d_f64[0]
+ COERCE_DOUBLE(*(_OWORD *)&_mm_cmpgt_sd(v23, v24) & LC7));
}
v25 = (int)v23.m128d_f64[0];
v26 = fmin(v15, v14);
v27.m128d_f64[0] = fmin(v16, v26);
v28 = v27.m128d_f64[0];
if ( COERCE_DOUBLE(LC8 & *(_QWORD *)&v27.m128d_f64[0]) < 4.503599627370496e15 )
{
v29.m128d_f64[0] = (double)(int)v27.m128d_f64[0];
*(_QWORD *)&v28 = ~LC8 & *(_QWORD *)&v27.m128d_f64[0] | COERCE_UNSIGNED_INT64(
v29.m128d_f64[0]
- COERCE_DOUBLE(*(_OWORD *)&_mm_cmpgt_sd(v29, v27) & LC7));
}
v13 = (int)v28;
v30 = fmax(v15, v14);
v31 = fmax(v16, v30);
v32.m128d_f64[0] = v31;
if ( COERCE_DOUBLE(LC8 & *(_QWORD *)&v31) < 4.503599627370496e15 )
{
v33.m128d_f64[0] = (double)(int)v31;
*(_QWORD *)&v32.m128d_f64[0] = ~LC8 & *(_QWORD *)&v31 | COERCE_UNSIGNED_INT64(
v33.m128d_f64[0]
+ COERCE_DOUBLE(*(_OWORD *)&_mm_cmpgt_sd(v32, v33) & LC7));
}
LODWORD(v3) = (int)v32.m128d_f64[0];
v53 = (int)v32.m128d_f64[0];
v34 = v41;
v35 = v47;
v42 = v41 - v47;
v36 = v45;
v37 = v43;
v44 = v45 - v43;
v46 = v37 * v47;
v48 = v34 * v36;
v38 = v49;
for ( i = (float)((float)((float)(v42 * v49) + (float)(v44 * v51)) + v46) - (float)(v34 * v36); v53 >= v13; ++v13 )
{
LODWORD(v3) = v52;
for ( j = v52; v25 >= j; ++j )
{
if ( j >= 0 )
{
v5 = *a1;
if ( v13 >= 0 && v5 > j && a1[1] > v13 )
{
v6 = (float)j;
v7 = (float)v13;
v8 = (float)((float)((float)((float)(v42 * (float)j) + (float)(v44 * (float)v13)) + v46) - v48) / i;
if ( v8 > 0.0 )
{
v9 = (float)((float)((float)((float)((float)(v35 - v51) * v6) + (float)((float)(v38 - v36) * v7))
+ (float)(v51 * v36))
- (float)(v38 * v35))
/ (float)((float)((float)((float)((float)(v35 - v51) * v37) + (float)((float)(v38 - v36) * v34))
+ (float)(v51 * v36))
- (float)(v38 * v35));
if ( v9 > 0.0 )
{
v10 = (float)((float)((float)((float)(v6 * (float)(v51 - v34)) + (float)(v7 * (float)(v37 - v38)))
+ (float)(v38 * v34))
- (float)(v51 * v37))
/ (float)((float)((float)((float)(v36 * (float)(v51 - v34)) + (float)(v35 * (float)(v37 - v38)))
+ (float)(v38 * v34))
- (float)(v51 * v37));
if ( v10 > 0.0 )
{
v11 = interp_uint(v54, v55, v56, v8, v9, v10);
v3 = (unsigned int *)((char *)depth_buffer + 4 * v13 * v5 + 4 * j);
if ( *v3 >= v11 )
{
*v3 = v11;
v12 = interp_colors(
v58,
v59,
v61,
(float)((float)((float)((float)(v42 * (float)j) + (float)(v44 * (float)v13)) + v46) - v48)
/ i,
v9,
v10);
LODWORD(v3) = put_pixel(a1, (unsigned int)j, (unsigned int)v13, v12);
}
}
}
}
}
}
}
}
a2 += 48LL;
}
while ( v57 != a3 );
}
return (int)v3;
}

canonical_decompiled_code:
int __fastcall func5(int *a1, __int64 a2, int a3)
{
unsigned int *v3; // rax
int j; // ebx
int v5; // er15
float v6; // xmm1_4
float v7; // xmm2_4
float v8; // xmm6_4
float v9; // xmm0_4
float v10; // xmm1_4
unsigned int v11; // edx
unsigned int v12; // eax
int v13; // er12
double v14; // rbx
double v15; // r14
double v16; // r15
double v17; // xmm1_8
__m128d v18; // xmm0
double v19; // xmm2_8
__m128d v20; // xmm2
double v21; // xmm1_8
double v22; // xmm0_8
__m128d v23; // xmm2
__m128d v24; // xmm1
int v25; // ebp
double v26; // xmm1_8
__m128d v27; // xmm0
double v28; // xmm2_8
__m128d v29; // xmm2
double v30; // xmm1_8
double v31; // xmm0_8
__m128d v32; // xmm2
__m128d v33; // xmm1
float v34; // xmm3_4
float v35; // xmm8_4
float v36; // xmm9_4
float v37; // xmm10_4
float v38; // xmm7_4
double x; // [rsp+0h] [rbp-B8h]
float v41; // [rsp+Ch] [rbp-ACh]
float v42; // [rsp+Ch] [rbp-ACh]
float v43; // [rsp+10h] [rbp-A8h]
float v44; // [rsp+10h] [rbp-A8h]
float v45; // [rsp+14h] [rbp-A4h]
float v46; // [rsp+14h] [rbp-A4h]
float v47; // [rsp+18h] [rbp-A0h]
float v48; // [rsp+18h] [rbp-A0h]
float v49; // [rsp+1Ch] [rbp-9Ch]
float i; // [rsp+1Ch] [rbp-9Ch]
float v51; // [rsp+20h] [rbp-98h]
int v52; // [rsp+38h] [rbp-80h]
int v53; // [rsp+3Ch] [rbp-7Ch]
int v54; // [rsp+58h] [rbp-60h]
int v55; // [rsp+5Ch] [rbp-5Ch]
int v56; // [rsp+60h] [rbp-58h]
int v57; // [rsp+64h] [rbp-54h]
int v58; // [rsp+68h] [rbp-50h]
int v59; // [rsp+6Ch] [rbp-4Ch]
int v61; // [rsp+78h] [rbp-40h]

if ( !depth_buffer )
func0(*a1, a1[1]);
func1();
fwrite("rasterization stage started...\n", 1uLL, 0x1FuLL, stderr);
LODWORD(v3) = fprintf(stderr, "%d triangles\n", (unsigned int)a3);
if ( a3 > 0 )
{
v57 = 0;
do
{
v49 = *(float *)a2;
v51 = *(float *)(a2 + 4);
v54 = *(_DWORD *)(a2 + 8);
v58 = *(_DWORD *)(a2 + 12);
v43 = *(float *)(a2 + 16);
v41 = *(float *)(a2 + 20);
v55 = *(_DWORD *)(a2 + 24);
v59 = *(_DWORD *)(a2 + 28);
v45 = *(float *)(a2 + 32);
v47 = *(float *)(a2 + 36);
v56 = *(_DWORD *)(a2 + 40);
v61 = *(_DWORD *)(a2 + 44);
v14 = v47;
v15 = v41;
v16 = v51;
x = *(float *)a2;
fprintf(
stderr,
"Triangle %d with vertices: (%f, %f), (%f, %f), (%f, %f)\n",
(unsigned int)++v57,
x,
v51,
v43,
v41,
v45,
v47);
v17 = fmin(v43, v45);
v18.m128d_f64[0] = fmin(x, v17);
v19 = v18.m128d_f64[0];
if ( COERCE_DOUBLE(LC8 & *(_QWORD *)&v18.m128d_f64[0]) < 4.503599627370496e15 )
{
v20.m128d_f64[0] = (double)(int)v18.m128d_f64[0];
*(_QWORD *)&v19 = ~LC8 & *(_QWORD *)&v18.m128d_f64[0] | COERCE_UNSIGNED_INT64(
v20.m128d_f64[0]
- COERCE_DOUBLE(*(_OWORD *)&_mm_cmpgt_sd(v20, v18) & LC7));
}
v52 = (int)v19;
v21 = fmax(v43, v45);
v22 = fmax(x, v21);
v23.m128d_f64[0] = v22;
if ( COERCE_DOUBLE(LC8 & *(_QWORD *)&v22) < 4.503599627370496e15 )
{
v24.m128d_f64[0] = (double)(int)v22;
*(_QWORD *)&v23.m128d_f64[0] = ~LC8 & *(_QWORD *)&v22 | COERCE_UNSIGNED_INT64(
v24.m128d_f64[0]
+ COERCE_DOUBLE(*(_OWORD *)&_mm_cmpgt_sd(v23, v24) & LC7));
}
v25 = (int)v23.m128d_f64[0];
v26 = fmin(v15, v14);
v27.m128d_f64[0] = fmin(v16, v26);
v28 = v27.m128d_f64[0];
if ( COERCE_DOUBLE(LC8 & *(_QWORD *)&v27.m128d_f64[0]) < 4.503599627370496e15 )
{
v29.m128d_f64[0] = (double)(int)v27.m128d_f64[0];
*(_QWORD *)&v28 = ~LC8 & *(_QWORD *)&v27.m128d_f64[0] | COERCE_UNSIGNED_INT64(
v29.m128d_f64[0]
- COERCE_DOUBLE(*(_OWORD *)&_mm_cmpgt_sd(v29, v27) & LC7));
}
v13 = (int)v28;
v30 = fmax(v15, v14);
v31 = fmax(v16, v30);
v32.m128d_f64[0] = v31;
if ( COERCE_DOUBLE(LC8 & *(_QWORD *)&v31) < 4.503599627370496e15 )
{
v33.m128d_f64[0] = (double)(int)v31;
*(_QWORD *)&v32.m128d_f64[0] = ~LC8 & *(_QWORD *)&v31 | COERCE_UNSIGNED_INT64(
v33.m128d_f64[0]
+ COERCE_DOUBLE(*(_OWORD *)&_mm_cmpgt_sd(v32, v33) & LC7));
}
LODWORD(v3) = (int)v32.m128d_f64[0];
v53 = (int)v32.m128d_f64[0];
v34 = v41;
v35 = v47;
v42 = v41 - v47;
v36 = v45;
v37 = v43;
v44 = v45 - v43;
v46 = v37 * v47;
v48 = v34 * v36;
v38 = v49;
for ( i = (float)((float)((float)(v42 * v49) + (float)(v44 * v51)) + v46) - (float)(v34 * v36); v53 >= v13; ++v13 )
{
LODWORD(v3) = v52;
for ( j = v52; v25 >= j; ++j )
{
if ( j >= 0 )
{
v5 = *a1;
if ( v13 >= 0 && v5 > j && a1[1] > v13 )
{
v6 = (float)j;
v7 = (float)v13;
v8 = (float)((float)((float)((float)(v42 * (float)j) + (float)(v44 * (float)v13)) + v46) - v48) / i;
if ( v8 > 0.0 )
{
v9 = (float)((float)((float)((float)((float)(v35 - v51) * v6) + (float)((float)(v38 - v36) * v7))
+ (float)(v51 * v36))
- (float)(v38 * v35))
/ (float)((float)((float)((float)((float)(v35 - v51) * v37) + (float)((float)(v38 - v36) * v34))
+ (float)(v51 * v36))
- (float)(v38 * v35));
if ( v9 > 0.0 )
{
v10 = (float)((float)((float)((float)(v6 * (float)(v51 - v34)) + (float)(v7 * (float)(v37 - v38)))
+ (float)(v38 * v34))
- (float)(v51 * v37))
/ (float)((float)((float)((float)(v36 * (float)(v51 - v34)) + (float)(v35 * (float)(v37 - v38)))
+ (float)(v38 * v34))
- (float)(v51 * v37));
if ( v10 > 0.0 )
{
v11 = func4(v54, v55, v56, v8, v9, v10);
v3 = (unsigned int *)((char *)depth_buffer + 4 * v13 * v5 + 4 * j);
if ( *v3 >= v11 )
{
*v3 = v11;
v12 = func3(
v58,
v59,
v61,
(float)((float)((float)((float)(v42 * (float)j) + (float)(v44 * (float)v13)) + v46) - v48)
/ i,
v9,
v10);
LODWORD(v3) = put_pixel(a1, (unsigned int)j, (unsigned int)v13, v12);
}
}
}
}
}
}
}
}
a2 += 48LL;
}
while ( v57 != a3 );
}
return (int)v3;
}

original_code:
void rasterize(bitmap_t *bmp, triangle2d_t *triangles, int n) {
  if(!depth_buffer) {
    init_depth_buffer(bmp->w, bmp->h);
  }
  clear_depth_buffer();
  int i;
  fprintf(stderr, "rasterization stage started...\n");
  fprintf(stderr, "%d triangles\n", n);
  for(i=0; i<n; i++) {
    triangle2d_t tri = triangles[i];
    fprintf(stderr, "Triangle %d with vertices: (%f, %f), (%f, %f), (%f, %f)\n", (i+1), tri.v1.x, tri.v1.y, tri.v2.x, tri.v2.y, tri.v3.x, tri.v3.y);
    float x0 = tri.v1.x; float y0 = tri.v1.y;
    float x1 = tri.v2.x; float y1 = tri.v2.y;
    float x2 = tri.v3.x; float y2 = tri.v3.y;
    int xmin = floor(fmin(x0, fmin(x1,x2)));
    int xmax = ceil(fmax(x0, fmax(x1,x2)));
    int ymin = floor(fmin(y0, fmin(y1,y2)));
    int ymax = ceil(fmax(y0, fmax(y1,y2)));
    float f_alpha = ((y1-y2)*x0 + (x2-x1)*y0 + x1*y2 - x2*y1);
    float f_beta = ((y2-y0)*x1 + (x0-x2)*y1 + x2*y0 - x0*y2);
    float f_gamma = ((y0-y1)*x2 + (x1-x0)*y2 + x0*y1 - x1*y0);
    int x, y;
    for(y=ymin; y<=ymax; y++) {
      for(x=xmin; x<=xmax; x++) {
        if(x < 0 || x >= bmp->w || y < 0 || y >= bmp->h)
          continue;
        float alpha = ((y1-y2)*x + (x2-x1)*y + x1*y2 - x2*y1)/f_alpha;
        float beta = ((y2-y0)*x + (x0-x2)*y + x2*y0 - x0*y2)/f_beta;
        float gamma = ((y0-y1)*x + (x1-x0)*y + x0*y1 - x1*y0)/f_gamma;
        if(alpha > 0 && beta > 0 && gamma > 0) {
          unsigned int depth_value = interp_uint(tri.v1.depth,
              tri.v2.depth, tri.v3.depth, alpha, beta, gamma);
          if(depth_value > depth_buffer[y*bmp->w + x]) {
            continue;
          }
          depth_buffer[y*bmp->w + x] = depth_value;
          color_t col = interp_colors(tri.v1.color, tri.v2.color, tri.v3.color, alpha, beta, gamma);
          put_pixel(bmp, x, y, col);
        } else {
        }
      }
    }
  }
}

canonical_original_code:
void rasterize(struct bitmap_t *bmp, struct triangle2d_t *triangles, int n) {
  if(!depth_buffer) {
    init_depth_buffer(bmp->w, bmp->h);
  }
  clear_depth_buffer();
  int i;
  fprintf(stderr, "rasterization stage started...\n");
  fprintf(stderr, "%d triangles\n", n);
  for(i=0; i<n; i++) {
    struct triangle2d_t tri = triangles[i];
    fprintf(stderr, "Triangle %d with vertices: (%f, %f), (%f, %f), (%f, %f)\n", (i+1), tri.v1.x, tri.v1.y, tri.v2.x, tri.v2.y, tri.v3.x, tri.v3.y);
    float x0 = tri.v1.x; float y0 = tri.v1.y;
    float x1 = tri.v2.x; float y1 = tri.v2.y;
    float x2 = tri.v3.x; float y2 = tri.v3.y;
    int xmin = floor(fmin(x0, fmin(x1,x2)));
    int xmax = ceil(fmax(x0, fmax(x1,x2)));
    int ymin = floor(fmin(y0, fmin(y1,y2)));
    int ymax = ceil(fmax(y0, fmax(y1,y2)));
    float f_alpha = ((y1-y2)*x0 + (x2-x1)*y0 + x1*y2 - x2*y1);
    float f_beta = ((y2-y0)*x1 + (x0-x2)*y1 + x2*y0 - x0*y2);
    float f_gamma = ((y0-y1)*x2 + (x1-x0)*y2 + x0*y1 - x1*y0);
    int x; int y;
    for(y=ymin; y<=ymax; y++) {
      for(x=xmin; x<=xmax; x++) {
        if(x < 0 || x >= bmp->w || y < 0 || y >= bmp->h)
          continue;
        float alpha = ((y1-y2)*x + (x2-x1)*y + x1*y2 - x2*y1)/f_alpha;
        float beta = ((y2-y0)*x + (x0-x2)*y + x2*y0 - x0*y2)/f_beta;
        float gamma = ((y0-y1)*x + (x1-x0)*y + x0*y1 - x1*y0)/f_gamma;
        if(alpha > 0 && beta > 0 && gamma > 0) {
          unsigned int depth_value = interp_uint(tri.v1.depth,
              tri.v2.depth, tri.v3.depth, alpha, beta, gamma);
          if(depth_value > depth_buffer[y*bmp->w + x]) {
            continue;
          }
          depth_buffer[y*bmp->w + x] = depth_value;
          unsigned int col = interp_colors(tri.v1.color, tri.v2.color, tri.v3.color, alpha, beta, gamma);
          put_pixel(bmp, x, y, col);
        } else {
        }
      }
    }
  }
}

variable_types: struct bitmap_t { int w; int h; unsigned int *data; } *

variable_types: struct triangle2d_t { struct point2d_t { float x; unsigned int depth; unsigned int color; } v1; } *

variable_types: n int

variable_types: xmin int

variable_types: xmax int

variable_types: ymin int

variable_types: ymax int

variable_types: x int

variable_types: y int

variable_types: alpha float

variable_types: beta float

variable_types: gamma float

variable_types:
col
unsigned int

user_defined_types: struct triangle2d_t { struct point2d_t v1; }


decompiled_code:
void rasterizer_free_memory()
{
destroy_depth_buffer();
}

canonical_decompiled_code:
void func6()
{
func2();
}


initMatrixStack

decompiled_code:
__int64 initMatrixStack()
{
_QWORD *v0; // rbx
_QWORD *v1; // rax
__int64 *v2; // rbx
__int64 result; // rax

v0 = malloc(0x10uLL);
head = v0;
v1 = malloc(0x10uLL);
guard = (__int64)v1;
v1[1] = 0LL;
v0[1] = v1;
*v0 = m_get_ident(4LL);
v2 = (__int64 *)guard;
result = m_get_ident(4LL);
*v2 = result;
return result;
}

canonical_decompiled_code:
__int64 func0()
{
_QWORD *v0; // rbx
_QWORD *v1; // rax
__int64 *v2; // rbx
__int64 result; // rax

v0 = malloc(0x10uLL);
head = v0;
v1 = malloc(0x10uLL);
guard = (__int64)v1;
v1[1] = 0LL;
v0[1] = v1;
*v0 = m_get_ident(4LL);
v2 = (__int64 *)guard;
result = m_get_ident(4LL);
*v2 = result;
return result;
}

original_code:
void initMatrixStack() {
  head = (mat_node_t*)malloc(sizeof(mat_node_t));
  guard = (mat_node_t*)malloc(sizeof(mat_node_t));
  guard->prev = ((void *)0);
  head->prev = guard;
  head->mat = m_get_ident(4);
  guard->mat = m_get_ident(4);
}

canonical_original_code:
void initMatrixStack() {
  head = (struct mat_node_t *)malloc(sizeof(mat_node_t));
  guard = (struct mat_node_t *)malloc(sizeof(mat_node_t));
  guard->prev = ((void *)0);
  head->prev = guard;
  head->mat = m_get_ident(4);
  guard->mat = m_get_ident(4);
}

user_defined_types: struct mat_node_t { struct MAT *mat; struct mat_node_t *prev; }

user_defined_types: struct MAT { unsigned int m; float **me; }


topMatrix

decompiled_code:
__int64 topMatrix()
{
return *(_QWORD *)head;
}

canonical_decompiled_code:
__int64 func1()
{
return *(_QWORD *)head;
}

original_code:
MAT* topMatrix() {
  return head->mat;
}

canonical_original_code:
struct MAT *topMatrix() {
  return head->mat;
}

return_type: struct MAT { unsigned int m; float **me; } *


popMatrix

decompiled_code:
__int64 popMatrix()
{
__int64 *v0; // rdi
__int64 v1; // rbx

v0 = (__int64 *)head;
if ( head == (void *)guard )
return *(_QWORD *)guard;
head = (void *)*((_QWORD *)head + 1);
v1 = *v0;
free(v0);
return v1;
}

canonical_decompiled_code:
__int64 func2()
{
__int64 *v0; // rdi
__int64 v1; // rbx

v0 = (__int64 *)head;
if ( head == (void *)guard )
return *(_QWORD *)guard;
head = (void *)*((_QWORD *)head + 1);
v1 = *v0;
free(v0);
return v1;
}

original_code:
MAT* popMatrix() {
  if(head == guard) {
    return guard->mat;
  }
  mat_node_t* last_head = head;
  head = head->prev;
  MAT* mat = last_head->mat;
  free(last_head);
  return mat;
}

canonical_original_code:
struct MAT *popMatrix() {
  if(head == guard) {
    return guard->mat;
  }
  struct mat_node_t *last_head = head;
  head = head->prev;
  struct MAT *mat = last_head->mat;
  free(last_head);
  return mat;
}

variable_types: struct mat_node_t { struct MAT { unsigned int m; float **me; } *mat; struct mat_node_t *prev; } *


pushMatrix

decompiled_code:
__int64 __fastcall pushMatrix(__int64 a1)
{
_QWORD *v1; // rax
__int64 v2; // rax

v1 = malloc(0x10uLL);
*v1 = a1;
v1[1] = head;
head = v1;
fwrite("topMatrix:\n", 1uLL, 0xBuLL, stderr);
v2 = topMatrix();
return print_mat(v2);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
_QWORD *v1; // rax
__int64 v2; // rax

v1 = malloc(0x10uLL);
*v1 = a1;
v1[1] = head;
head = v1;
fwrite("topMatrix:\n", 1uLL, 0xBuLL, stderr);
v2 = func1();
return print_mat(v2);
}

original_code:
void pushMatrix(MAT* matrix) {
  mat_node_t* newHead = (mat_node_t*)malloc(sizeof(mat_node_t));
  newHead->mat = matrix;
  newHead->prev = head;
  head = newHead;
  fprintf(stderr, "topMatrix:\n");
  print_mat(topMatrix());
}

canonical_original_code:
void pushMatrix(struct MAT *matrix) {
  struct mat_node_t *newHead = (mat_node_t*)malloc(sizeof(mat_node_t));
  newHead->mat = matrix;
  newHead->prev = head;
  head = newHead;
  fprintf(stderr, "topMatrix:\n");
  print_mat(topMatrix());
}

variable_types: struct MAT { unsigned int m; float **me; } *


scale

original_code:
MAT* scale(MAT *obj, float s1, float s2, float s3){
 MAT *scaleMatrix;
 scaleMatrix = m_get(4, 4);
 m_zero(scaleMatrix);
 scaleMatrix->me[0][0] = s1;
 scaleMatrix->me[1][1] = s2;
 scaleMatrix->me[2][2] = s3;
 scaleMatrix->me[3][3] = 1;
 m_mlt_self_left(obj, scaleMatrix);
 M_FREE(scaleMatrix);
 return obj;
}

canonical_original_code:
struct MAT *scale(struct MAT *obj, float s1, float s2, float s3){
 struct MAT *scaleMatrix;
 scaleMatrix = m_get(4, 4);
 m_zero(scaleMatrix);
 scaleMatrix->me[0][0] = s1;
 scaleMatrix->me[1][1] = s2;
 scaleMatrix->me[2][2] = s3;
 scaleMatrix->me[3][3] = 1;
 m_mlt_self_left(obj, scaleMatrix);
 M_FREE(scaleMatrix);
 return obj;
}

variable_types:
obj
struct MAT *


rotateZ

decompiled_code:
__int64 __fastcall rotateZ(__int64 a1, float a2)
{
__int64 v2; // rbx
double v3; // xmm0_8
float v4; // xmm1_4
double cosx; // [rsp+10h] [rbp-28h] BYREF
double sinx[4]; // [rsp+18h] [rbp-20h] BYREF

v2 = m_get(4LL, 4LL);
m_zero(v2);
sincos(a2, sinx, &cosx);
v3 = sinx[0];
v4 = cosx;
***(float ***)(v2 + 8) = v4;
*(float *)&v3 = v3;
*(_DWORD *)(**(_QWORD **)(v2 + 8) + 4LL) = LODWORD(v3) ^ LC1;
**(_DWORD **)(*(_QWORD *)(v2 + 8) + 8LL) = LODWORD(v3);
*(float *)(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL) + 4LL) = v4;
*(_DWORD *)(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 16LL) + 8LL) = LC0;
*(_DWORD *)(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 24LL) + 12LL) = LC0;
m_mlt_self_left(a1, v2);
M_FREE(v2);
return a1;
}

original_code:
MAT* rotateZ(MAT *obj, float fi){
 MAT *rotateMatrix = m_get(4, 4);
 m_zero(rotateMatrix);
 rotateMatrix->me[0][0] = cos(fi);
 rotateMatrix->me[0][1] = -sin(fi);
 rotateMatrix->me[1][0] = sin(fi);
 rotateMatrix->me[1][1] = cos(fi);
 rotateMatrix->me[2][2] = 1;
 rotateMatrix->me[3][3] = 1;
 m_mlt_self_left(obj, rotateMatrix);
 M_FREE(rotateMatrix);
 return obj;
}

canonical_original_code:
struct MAT *rotateZ(struct MAT *obj, float fi){
 struct MAT *rotateMatrix = m_get(4, 4);
 m_zero(rotateMatrix);
 rotateMatrix->me[0][0] = cos(fi);
 rotateMatrix->me[0][1] = -sin(fi);
 rotateMatrix->me[1][0] = sin(fi);
 rotateMatrix->me[1][1] = cos(fi);
 rotateMatrix->me[2][2] = 1;
 rotateMatrix->me[3][3] = 1;
 m_mlt_self_left(obj, rotateMatrix);
 M_FREE(rotateMatrix);
 return obj;
}

variable_types: fi float


rotateX

original_code:
MAT* rotateX(MAT *obj, float fi){
 MAT *rotateMatrix = m_get(4,4);
 m_zero(rotateMatrix);
 rotateMatrix->me[0][0] = 1;
 rotateMatrix->me[1][1] = cos(fi);
 rotateMatrix->me[1][2] = -sin(fi);
 rotateMatrix->me[2][1] = sin(fi);
 rotateMatrix->me[2][2] = cos(fi);
 rotateMatrix->me[3][3] = 1;
 m_mlt_self_left(obj, rotateMatrix);
 M_FREE(rotateMatrix);
 return obj;
}

canonical_original_code:
struct MAT *rotateX(struct MAT *obj, float fi){
 struct MAT *rotateMatrix = m_get(4,4);
 m_zero(rotateMatrix);
 rotateMatrix->me[0][0] = 1;
 rotateMatrix->me[1][1] = cos(fi);
 rotateMatrix->me[1][2] = -sin(fi);
 rotateMatrix->me[2][1] = sin(fi);
 rotateMatrix->me[2][2] = cos(fi);
 rotateMatrix->me[3][3] = 1;
 m_mlt_self_left(obj, rotateMatrix);
 M_FREE(rotateMatrix);
 return obj;
}


rotateY

decompiled_code:
__int64 __fastcall rotateY(__int64 a1, float a2)
{
__int64 v2; // rbx
double v3; // xmm0_8
float v4; // xmm2_4
double cosx; // [rsp+10h] [rbp-28h] BYREF
double sinx[4]; // [rsp+18h] [rbp-20h] BYREF

v2 = m_get(4LL, 4LL);
m_zero(v2);
sincos(a2, sinx, &cosx);
v3 = sinx[0];
v4 = cosx;
***(float ***)(v2 + 8) = v4;
*(float *)&v3 = v3;
*(_DWORD *)(**(_QWORD **)(v2 + 8) + 8LL) = LODWORD(v3);
*(_DWORD *)(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 8LL) + 4LL) = LC0;
**(_DWORD **)(*(_QWORD *)(v2 + 8) + 16LL) = LODWORD(v3) ^ LC1;
*(float *)(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 16LL) + 8LL) = v4;
*(_DWORD *)(*(_QWORD *)(*(_QWORD *)(v2 + 8) + 24LL) + 12LL) = LC0;
m_mlt_self_left(a1, v2);
M_FREE(v2);
return a1;
}

original_code:
MAT* rotateY(MAT *obj, float fi){
 MAT *rotateMatrix = m_get(4,4);
 m_zero(rotateMatrix);
 rotateMatrix->me[0][0] = cos(fi);
 rotateMatrix->me[0][2] = sin(fi);
 rotateMatrix->me[1][1] = 1;
 rotateMatrix->me[2][0] = -sin(fi);
 rotateMatrix->me[2][2] = cos(fi);
 rotateMatrix->me[3][3] = 1;
 m_mlt_self_left(obj, rotateMatrix);
 M_FREE(rotateMatrix);
 return obj;
}

canonical_original_code:
struct MAT *rotateY(struct MAT *obj, float fi){
 struct MAT *rotateMatrix = m_get(4,4);
 m_zero(rotateMatrix);
 rotateMatrix->me[0][0] = cos(fi);
 rotateMatrix->me[0][2] = sin(fi);
 rotateMatrix->me[1][1] = 1;
 rotateMatrix->me[2][0] = -sin(fi);
 rotateMatrix->me[2][2] = cos(fi);
 rotateMatrix->me[3][3] = 1;
 m_mlt_self_left(obj, rotateMatrix);
 M_FREE(rotateMatrix);
 return obj;
}


rotate

original_code:
MAT *rotate(MAT *obj, float degrees, vec3_t axis) {
  float theta = 3.14159265358979323846/180.0 * degrees;
  float s = sin(theta);
  float c = cos(theta);
  float x = axis.x, y=axis.y, z=axis.z;
  MAT *rotationMatrix = m_get_ident(4);
  rotationMatrix->me[0][0] = c + (1-c)*x*x;
  rotationMatrix->me[0][1] = (1-c)*x*y - s*z;
  rotationMatrix->me[0][2] = (1-c)*x*z + s*y;
  rotationMatrix->me[1][0] = (1-c)*x*y + s*z;
  rotationMatrix->me[1][1] = c + (1-c)*y*y;
  rotationMatrix->me[1][2] = (1-c)*y*z - s*x;
  rotationMatrix->me[2][0] = (1-c)*x*z - s*y;
  rotationMatrix->me[2][1] = (1-c)*y*z + s*x;
  rotationMatrix->me[2][2] = c + (1-c)*z*z;
  m_mlt_self_left(obj, rotationMatrix);
  return obj;
}

canonical_original_code:
struct MAT *rotate(struct MAT *obj, float degrees, struct vec3_t axis) {
  float theta = 3.14159265358979323846/180.0 * degrees;
  float s = sin(theta);
  float c = cos(theta);
  float x = axis.x; float y = axis.y; float z = axis.z;
  struct MAT *rotationMatrix = m_get_ident(4);
  rotationMatrix->me[0][0] = c + (1-c)*x*x;
  rotationMatrix->me[0][1] = (1-c)*x*y - s*z;
  rotationMatrix->me[0][2] = (1-c)*x*z + s*y;
  rotationMatrix->me[1][0] = (1-c)*x*y + s*z;
  rotationMatrix->me[1][1] = c + (1-c)*y*y;
  rotationMatrix->me[1][2] = (1-c)*y*z - s*x;
  rotationMatrix->me[2][0] = (1-c)*x*z - s*y;
  rotationMatrix->me[2][1] = (1-c)*y*z + s*x;
  rotationMatrix->me[2][2] = c + (1-c)*z*z;
  m_mlt_self_left(obj, rotationMatrix);
  return obj;
}

variable_types: degrees float

variable_types:
axis
struct vec3_t { float x; }

variable_types: theta float

variable_types: s float

variable_types: c float

variable_types: x float

variable_types: y float

variable_types: z float


translate

original_code:
MAT* translate(MAT* obj, float xt, float yt, float zt){
    MAT *translationMatrix = m_get_ident(4);
    translationMatrix->me[0][3] = xt;
    translationMatrix->me[1][3] = yt;
    translationMatrix->me[2][3] = zt;
    print_mat(translationMatrix);
    m_mlt_self_left(obj, translationMatrix);
   M_FREE(translationMatrix);
   return obj;
}

canonical_original_code:
struct MAT *translate(struct MAT *obj, float xt, float yt, float zt){
    struct MAT *translationMatrix = m_get_ident(4);
    translationMatrix->me[0][3] = xt;
    translationMatrix->me[1][3] = yt;
    translationMatrix->me[2][3] = zt;
    print_mat(translationMatrix);
    m_mlt_self_left(obj, translationMatrix);
   M_FREE(translationMatrix);
   return obj;
}

variable_types: xt float

variable_types: yt float

variable_types: zt float


decompiled_code:
__m128i __fastcall v3_init(float a1, float a2)
{
__m128i v3; // [rsp+0h] [rbp-14h] BYREF

v3.m128i_i64[0] = __PAIR64__(LODWORD(a2), LODWORD(a1));
return _mm_loadl_epi64(&v3);
}

canonical_decompiled_code:
__m128i __fastcall func0(float a1, float a2)
{
__m128i v3; // [rsp+0h] [rbp-14h] BYREF

v3.m128i_i64[0] = __PAIR64__(LODWORD(a2), LODWORD(a1));
return _mm_loadl_epi64(&v3);
}

original_code:
vec3_t v3_init(float x, float y, float z) {
  vec3_t v;
  v.x = x; v.y = y; v.z = z;
  return v;
}

canonical_original_code:
struct vec3_t v3_init(float x, float y, float z) {
  struct vec3_t v;
  v.x = x; v.y = y; v.z = z;
  return v;
}

variable_types:
v
struct vec3_t { float x; }

return_type: struct vec3_t { float x; }


decompiled_code:
float __fastcall v3_dot(double a1, float a2, double a3, float a4)
{
return (float)((float)(*(float *)&a1 * *(float *)&a3) + (float)(*((float *)&a1 + 1) * *((float *)&a3 + 1)))
+ (float)(a2 * a4);
}

canonical_decompiled_code:
float __fastcall func1(double a1, float a2, double a3, float a4)
{
return (float)((float)(*(float *)&a1 * *(float *)&a3) + (float)(*((float *)&a1 + 1) * *((float *)&a3 + 1)))
+ (float)(a2 * a4);
}

original_code:
float v3_dot(vec3_t v, vec3_t w) {
  return v.x*w.x + v.y*w.y + v.z*w.z;
}

canonical_original_code:
float v3_dot(struct vec3_t v, struct vec3_t w) {
  return v.x*w.x + v.y*w.y + v.z*w.z;
}

variable_types:
w
struct vec3_t

return_type: float


decompiled_code:
__m128i __fastcall v3_cross(double a1, float a2, double a3, float a4)
{
__m128i v5; // [rsp+24h] [rbp-14h] BYREF

*(float *)v5.m128i_i32 = (float)(*((float *)&a1 + 1) * a4) - (float)(a2 * *((float *)&a3 + 1));
*(float *)&v5.m128i_i32[1] = (float)(a2 * *(float *)&a3) - (float)(a4 * *(float *)&a1);
return _mm_loadl_epi64(&v5);
}

canonical_decompiled_code:
__m128i __fastcall func2(double a1, float a2, double a3, float a4)
{
__m128i v5; // [rsp+24h] [rbp-14h] BYREF

*(float *)v5.m128i_i32 = (float)(*((float *)&a1 + 1) * a4) - (float)(a2 * *((float *)&a3 + 1));
*(float *)&v5.m128i_i32[1] = (float)(a2 * *(float *)&a3) - (float)(a4 * *(float *)&a1);
return _mm_loadl_epi64(&v5);
}

original_code:
vec3_t v3_cross(vec3_t lhs, vec3_t rhs) {
  vec3_t result;
  result.x = lhs.y*rhs.z - lhs.z*rhs.y;
  result.y = lhs.z*rhs.x - lhs.x*rhs.z;
  result.z = lhs.x*rhs.y - lhs.y*rhs.x;
  return result;
}

canonical_original_code:
struct vec3_t v3_cross(struct vec3_t lhs, struct vec3_t rhs) {
  struct vec3_t result;
  result.x = lhs.y*rhs.z - lhs.z*rhs.y;
  result.y = lhs.z*rhs.x - lhs.x*rhs.z;
  result.z = lhs.x*rhs.y - lhs.y*rhs.x;
  return result;
}

variable_types:
lhs
struct vec3_t

variable_types:
rhs
struct vec3_t

variable_types:
result
struct vec3_t { float x; }


decompiled_code:
float __fastcall v3_length(double a1, float a2)
{
float v2; // xmm0_4
float result; // xmm0_4

v2 = (float)((float)(*(float *)&a1 * *(float *)&a1) + (float)(*((float *)&a1 + 1) * *((float *)&a1 + 1)))
+ (float)(a2 * a2);
if ( v2 < 0.0 )
result = sqrtf(v2);
else
result = fsqrt(v2);
return result;
}

canonical_decompiled_code:
float __fastcall func3(double a1, float a2)
{
float v2; // xmm0_4
float result; // xmm0_4

v2 = (float)((float)(*(float *)&a1 * *(float *)&a1) + (float)(*((float *)&a1 + 1) * *((float *)&a1 + 1)))
+ (float)(a2 * a2);
if ( v2 < 0.0 )
result = sqrtf(v2);
else
result = fsqrt(v2);
return result;
}

original_code:
float v3_length(vec3_t v) {
  return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
}

canonical_original_code:
float v3_length(struct vec3_t v) {
  return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
}


original_code:
vec3_t v3_sub(vec3_t lhs, vec3_t rhs) {
  vec3_t result;
  result.x = lhs.x - rhs.x;
  result.y = lhs.y - rhs.y;
  result.z = lhs.z - rhs.z;
  return result;
}

canonical_original_code:
struct vec3_t v3_sub(struct vec3_t lhs, struct vec3_t rhs) {
  struct vec3_t result;
  result.x = lhs.x - rhs.x;
  result.y = lhs.y - rhs.y;
  result.z = lhs.z - rhs.z;
  return result;
}


decompiled_code:
__m128i __fastcall v3_normalize(double a1, float a2)
{
float v2; // xmm0_4
__m128i v5; // [rsp+24h] [rbp-14h] BYREF

v2 = v3_length(a1, a2);
*(float *)v5.m128i_i32 = *(float *)&a1 / v2;
*(float *)&v5.m128i_i32[1] = *((float *)&a1 + 1) / v2;
return _mm_loadl_epi64(&v5);
}

original_code:
vec3_t v3_normalize(vec3_t v) {
  vec3_t result;
  float length = v3_length(v);
  result.x = v.x / length;
  result.y = v.y / length;
  result.z = v.z / length;
  return result;
}

canonical_original_code:
struct vec3_t v3_normalize(struct vec3_t v) {
  struct vec3_t result;
  float length = v3_length(v);
  result.x = v.x / length;
  result.y = v.y / length;
  result.z = v.z / length;
  return result;
}

variable_types:
v
struct vec3_t

variable_types: length float


decompiled_code:
__m128i __fastcall v3_up(double a1, float a2, double a3, float a4)
{
double v4; // xmm0_8
double v5; // xmm0_8

*(_QWORD *)&v4 = v3_cross(a1, a2, a3, a4).m128i_u64[0];
*(_QWORD *)&v5 = v3_cross(a3, a4, v4, a2).m128i_u64[0];
return v3_normalize(v5, a4);
}

canonical_decompiled_code:
__m128i __fastcall func6(double a1, float a2, double a3, float a4)
{
double v4; // xmm0_8
double v5; // xmm0_8

*(_QWORD *)&v4 = func2(a1, a2, a3, a4).m128i_u64[0];
*(_QWORD *)&v5 = func2(a3, a4, v4, a2).m128i_u64[0];
return func5(v5, a4);
}

original_code:
vec3_t v3_up(vec3_t up, vec3_t zvec) {
  vec3_t x = v3_cross(up, zvec);
  vec3_t y = v3_cross(zvec, x);
  return v3_normalize(y);
}

canonical_original_code:
struct vec3_t v3_up(struct vec3_t up, struct vec3_t zvec) {
  struct vec3_t x = v3_cross(up, zvec);
  struct vec3_t y = v3_cross(zvec, x);
  return v3_normalize(y);
}

variable_types:
up
struct vec3_t

variable_types:
zvec
struct vec3_t

variable_types:
x
struct vec3_t { float x; }

variable_types:
y
struct vec3_t { float x; }


computeColor

original_code:
color_t computeColor(fcolor_t fcolor) {
  color_t col;
  int r = floor(255 * fcolor.r);
  int g = floor(255 * fcolor.g);
  int b = floor(255 * fcolor.b);
  col = (r << 16) + (g << 8) + b;
  return col;
}

canonical_original_code:
unsigned int computeColor(struct fcolor_t fcolor) {
  unsigned int col;
  int r = floor(255 * fcolor.r);
  int g = floor(255 * fcolor.g);
  int b = floor(255 * fcolor.b);
  col = (r << 16) + (g << 8) + b;
  return col;
}

variable_types:
fcolor
struct fcolor_t { float r; }

variable_types:
col
unsigned int

variable_types: r int

variable_types: g int

variable_types: b int


computeDepth

decompiled_code:
__int64 __fastcall computeDepth(__int64 a1, float a2)
{
fprintf(stderr, "Depth, near, far = %f, %f, %f\n", a2, *(float *)(a1 + 52), *(float *)(a1 + 56));
fprintf(
stderr,
"Depth value: %u\n",
(unsigned int)(int)(float)((float)((float)(a2 - *(float *)(a1 + 52))
/ (float)(*(float *)(a1 + 56) - *(float *)(a1 + 52)))
* 4294967300.0));
return (unsigned int)(int)(float)((float)((float)(a2 - *(float *)(a1 + 52))
/ (float)(*(float *)(a1 + 56) - *(float *)(a1 + 52)))
* 4294967300.0);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, float a2)
{
fprintf(stderr, "Depth, near, far = %f, %f, %f\n", a2, *(float *)(a1 + 52), *(float *)(a1 + 56));
fprintf(
stderr,
"Depth value: %u\n",
(unsigned int)(int)(float)((float)((float)(a2 - *(float *)(a1 + 52))
/ (float)(*(float *)(a1 + 56) - *(float *)(a1 + 52)))
* 4294967300.0));
return (unsigned int)(int)(float)((float)((float)(a2 - *(float *)(a1 + 52))
/ (float)(*(float *)(a1 + 56) - *(float *)(a1 + 52)))
* 4294967300.0);
}

original_code:
unsigned int computeDepth(scene_t *scene, float depth) {
  fprintf(stderr, "Depth, near, far = %f, %f, %f\n", depth, scene->near, scene->far);
  fprintf(stderr, "Depth value: %u\n", (unsigned int)((depth - scene->near)/(scene->far - scene->near) * (0x7fffffff * 2U + 1U)));
  return (depth - scene->near)/(scene->far - scene->near) * (0x7fffffff * 2U + 1U);
}

canonical_original_code:
unsigned int computeDepth(struct scene_t *scene, float depth) {
  fprintf(stderr, "Depth, near, far = %f, %f, %f\n", depth, scene->near, scene->far);
  fprintf(stderr, "Depth value: %u\n", (unsigned int)((depth - scene->near)/(scene->far - scene->near) * (0x7fffffff * 2U + 1U)));
  return (depth - scene->near)/(scene->far - scene->near) * (0x7fffffff * 2U + 1U);
}

variable_types: struct scene_t { struct mesh_t { struct triangle_t { struct vec4_t { float x; struct fcolor_t { float r; } color; } verts[3]; } *tris; int n; struct MAT { unsigned int m; float **me; } *transform; } *meshes; int n; struct camera_t { struct MAT *transform; } *camera; struct MAT *M_viewport; struct MAT *M_proj; struct MAT *M_cam; int total_tris; float near; float far; } *

variable_types: depth float

user_defined_types: struct scene_t { struct mesh_t *meshes; int n; struct camera_t *camera; struct MAT *M_viewport; struct MAT *M_proj; struct MAT *M_cam; int total_tris; float near; float far; }

user_defined_types: struct mesh_t { struct triangle_t *tris; int n; struct MAT *transform; }

user_defined_types: struct triangle_t { struct vec4_t verts[3]; }

user_defined_types: struct vec4_t { float x; struct fcolor_t color; }


decompiled_code:
char *__fastcall scene_compute_tris_to_rasterize(__int64 a1)
{
char *v1; // r14
const __m128i *v2; // r15
__m128i *v3; // rbp
float *v4; // r14
int i; // er13
__int64 v6; // r12
float **v7; // rax
float *v8; // rdx
float v9; // xmm1_4
const char *v10; // rsi
struct _IO_FILE *v11; // rdi
__int64 v12; // rcx
__int64 v13; // rax
int v14; // ebx
__int64 v15; // rbp
__int64 v17; // [rsp+20h] [rbp-128h]
__int64 v18; // [rsp+28h] [rbp-120h]
__int64 v20; // [rsp+38h] [rbp-110h]
__int64 v21; // [rsp+40h] [rbp-108h]
__m128i *v22; // [rsp+48h] [rbp-100h]
__int64 v23; // [rsp+60h] [rbp-E8h]
int v24; // [rsp+6Ch] [rbp-DCh]
char *v25; // [rsp+70h] [rbp-D8h]
char *v26; // [rsp+78h] [rbp-D0h]
__m128i v27; // [rsp+80h] [rbp-C8h] BYREF
__m128i v28; // [rsp+90h] [rbp-B8h] BYREF
__m128i v29; // [rsp+A0h] [rbp-A8h] BYREF
__m128i v30; // [rsp+B0h] [rbp-98h] BYREF
__int128 v31[4]; // [rsp+C0h] [rbp-88h]
int v32; // [rsp+100h] [rbp-48h]

v21 = m_get_ident(4LL);
m_mlt_self_left(v21, *(_QWORD *)(a1 + 40));
fwrite("Camera:\n", 1uLL, 8uLL, stderr);
print_mat(v21);
fwrite("viewport:\n", 1uLL, 0xAuLL, stderr);
print_mat(*(_QWORD *)(a1 + 24));
fwrite("Projection:\n", 1uLL, 0xCuLL, stderr);
print_mat(*(_QWORD *)(a1 + 32));
fwrite("Camera:\n", 1uLL, 8uLL, stderr);
m_mlt_self_left(v21, *(_QWORD *)(a1 + 32));
fwrite("Projection*Camera:\n", 1uLL, 0x13uLL, stderr);
print_mat(v21);
m_mlt_self_left(v21, *(_QWORD *)(a1 + 24));
fwrite("Viewport*Projection*Camera:\n", 1uLL, 0x1CuLL, stderr);
print_mat(v21);
v1 = (char *)malloc(48LL * *(int *)(a1 + 48));
v25 = v1;
fprintf(stderr, "Scene has %d meshes\n", *(unsigned int *)(a1 + 8));
if ( *(int *)(a1 + 8) > 0 )
{
v23 = 0LL;
v24 = 0;
v26 = v1 + 48;
do
{
v13 = *(_QWORD *)a1 + 24 * v23;
v2 = *(const __m128i **)v13;
v14 = *(_DWORD *)(v13 + 8);
v15 = *(_QWORD *)(v13 + 16);
v20 = v15;
fwrite("Mesh modelview matrix:\n", 1uLL, 0x17uLL, stderr);
print_mat(v15);
v10 = "Mesh %d has %d triangles\n";
v11 = stderr;
fprintf(stderr, "Mesh %d has %d triangles\n", (unsigned int)v23, (unsigned int)v14);
if ( v14 > 0 )
{
v22 = (__m128i *)&v25[48 * v24];
do
{
v30 = _mm_loadu_si128(v2);
v31[0] = (__int128)_mm_loadu_si128(v2 + 1);
v31[1] = (__int128)_mm_loadu_si128(v2 + 2);
v31[2] = (__int128)_mm_loadu_si128(v2 + 3);
v31[3] = (__int128)_mm_loadu_si128(v2 + 4);
v32 = v2[5].m128i_i32[0];
v3 = &v27;
v4 = (float *)&v30;
for ( i = 0; i != 3; ++i )
{
v17 = vec_to_mat(v11, v10, (char *)&v30 + 28 * i, v12);
v6 = m_get(4LL, 1LL);
v18 = m_get(4LL, 4LL);
m_mlt(v21, v20, v18);
m_mlt(v18, v17, v6);
v7 = *(float ***)(v6 + 8);
v8 = *v7;
v9 = *v7[3];
*(float *)&v17 = **v7 / v9;
v3->m128i_i32[0] = v17;
*(float *)&v18 = v8[1] / v9;
v3->m128i_i32[1] = v18;
v3->m128i_i32[2] = computeDepth(a1, *v7[2] / v9);
v3->m128i_i32[3] = computeColor(*(double *)((char *)v31 + 28 * i), *((float *)v31 + 7 * i + 2));
fprintf(stderr, "Point in modelview coords: %f %f %f %f\n", *v4, v4[1], v4[2], v4[3]);
fprintf(
stderr,
"Point after transforms: %f %f %f %f\n",
***(float ***)(v6 + 8),
**(float **)(*(_QWORD *)(v6 + 8) + 8LL),
**(float **)(*(_QWORD *)(v6 + 8) + 16LL),
**(float **)(*(_QWORD *)(v6 + 8) + 24LL));
v10 = "Point projected to: %f %f\n";
v11 = stderr;
fprintf(stderr, "Point projected to: %f %f\n", *(float *)&v17, *(float *)&v18);
++v3;
v4 += 7;
}
*v22 = _mm_loadu_si128(&v27);
v22[1] = _mm_loadu_si128(&v28);
v22[2] = _mm_loadu_si128(&v29);
v2 = (const __m128i *)((char *)v2 + 84);
v22 += 3;
}
while ( v22 != (__m128i *)&v26[48 * v24 + 48 * (unsigned __int64)(unsigned int)(v14 - 1)] );
v24 += v14;
}
++v23;
}
while ( *(_DWORD *)(a1 + 8) > (int)v23 );
}
M_FREE(v21);
return v25;
}

canonical_decompiled_code:
char *__fastcall func2(__int64 a1)
{
char *v1; // r14
const __m128i *v2; // r15
__m128i *v3; // rbp
float *v4; // r14
int i; // er13
__int64 v6; // r12
float **v7; // rax
float *v8; // rdx
float v9; // xmm1_4
const char *v10; // rsi
struct _IO_FILE *v11; // rdi
__int64 v12; // rcx
__int64 v13; // rax
int v14; // ebx
__int64 v15; // rbp
__int64 v17; // [rsp+20h] [rbp-128h]
__int64 v18; // [rsp+28h] [rbp-120h]
__int64 v20; // [rsp+38h] [rbp-110h]
__int64 v21; // [rsp+40h] [rbp-108h]
__m128i *v22; // [rsp+48h] [rbp-100h]
__int64 v23; // [rsp+60h] [rbp-E8h]
int v24; // [rsp+6Ch] [rbp-DCh]
char *v25; // [rsp+70h] [rbp-D8h]
char *v26; // [rsp+78h] [rbp-D0h]
__m128i v27; // [rsp+80h] [rbp-C8h] BYREF
__m128i v28; // [rsp+90h] [rbp-B8h] BYREF
__m128i v29; // [rsp+A0h] [rbp-A8h] BYREF
__m128i v30; // [rsp+B0h] [rbp-98h] BYREF
__int128 v31[4]; // [rsp+C0h] [rbp-88h]
int v32; // [rsp+100h] [rbp-48h]

v21 = m_get_ident(4LL);
m_mlt_self_left(v21, *(_QWORD *)(a1 + 40));
fwrite("Camera:\n", 1uLL, 8uLL, stderr);
print_mat(v21);
fwrite("viewport:\n", 1uLL, 0xAuLL, stderr);
print_mat(*(_QWORD *)(a1 + 24));
fwrite("Projection:\n", 1uLL, 0xCuLL, stderr);
print_mat(*(_QWORD *)(a1 + 32));
fwrite("Camera:\n", 1uLL, 8uLL, stderr);
m_mlt_self_left(v21, *(_QWORD *)(a1 + 32));
fwrite("Projection*Camera:\n", 1uLL, 0x13uLL, stderr);
print_mat(v21);
m_mlt_self_left(v21, *(_QWORD *)(a1 + 24));
fwrite("Viewport*Projection*Camera:\n", 1uLL, 0x1CuLL, stderr);
print_mat(v21);
v1 = (char *)malloc(48LL * *(int *)(a1 + 48));
v25 = v1;
fprintf(stderr, "Scene has %d meshes\n", *(unsigned int *)(a1 + 8));
if ( *(int *)(a1 + 8) > 0 )
{
v23 = 0LL;
v24 = 0;
v26 = v1 + 48;
do
{
v13 = *(_QWORD *)a1 + 24 * v23;
v2 = *(const __m128i **)v13;
v14 = *(_DWORD *)(v13 + 8);
v15 = *(_QWORD *)(v13 + 16);
v20 = v15;
fwrite("Mesh modelview matrix:\n", 1uLL, 0x17uLL, stderr);
print_mat(v15);
v10 = "Mesh %d has %d triangles\n";
v11 = stderr;
fprintf(stderr, "Mesh %d has %d triangles\n", (unsigned int)v23, (unsigned int)v14);
if ( v14 > 0 )
{
v22 = (__m128i *)&v25[48 * v24];
do
{
v30 = _mm_loadu_si128(v2);
v31[0] = (__int128)_mm_loadu_si128(v2 + 1);
v31[1] = (__int128)_mm_loadu_si128(v2 + 2);
v31[2] = (__int128)_mm_loadu_si128(v2 + 3);
v31[3] = (__int128)_mm_loadu_si128(v2 + 4);
v32 = v2[5].m128i_i32[0];
v3 = &v27;
v4 = (float *)&v30;
for ( i = 0; i != 3; ++i )
{
v17 = vec_to_mat(v11, v10, (char *)&v30 + 28 * i, v12);
v6 = m_get(4LL, 1LL);
v18 = m_get(4LL, 4LL);
m_mlt(v21, v20, v18);
m_mlt(v18, v17, v6);
v7 = *(float ***)(v6 + 8);
v8 = *v7;
v9 = *v7[3];
*(float *)&v17 = **v7 / v9;
v3->m128i_i32[0] = v17;
*(float *)&v18 = v8[1] / v9;
v3->m128i_i32[1] = v18;
v3->m128i_i32[2] = func1(a1, *v7[2] / v9);
v3->m128i_i32[3] = func0(*(double *)((char *)v31 + 28 * i), *((float *)v31 + 7 * i + 2));
fprintf(stderr, "Point in modelview coords: %f %f %f %f\n", *v4, v4[1], v4[2], v4[3]);
fprintf(
stderr,
"Point after transforms: %f %f %f %f\n",
***(float ***)(v6 + 8),
**(float **)(*(_QWORD *)(v6 + 8) + 8LL),
**(float **)(*(_QWORD *)(v6 + 8) + 16LL),
**(float **)(*(_QWORD *)(v6 + 8) + 24LL));
v10 = "Point projected to: %f %f\n";
v11 = stderr;
fprintf(stderr, "Point projected to: %f %f\n", *(float *)&v17, *(float *)&v18);
++v3;
v4 += 7;
}
*v22 = _mm_loadu_si128(&v27);
v22[1] = _mm_loadu_si128(&v28);
v22[2] = _mm_loadu_si128(&v29);
v2 = (const __m128i *)((char *)v2 + 84);
v22 += 3;
}
while ( v22 != (__m128i *)&v26[48 * v24 + 48 * (unsigned __int64)(unsigned int)(v14 - 1)] );
v24 += v14;
}
++v23;
}
while ( *(_DWORD *)(a1 + 8) > (int)v23 );
}
M_FREE(v21);
return v25;
}

original_code:
triangle2d_t* scene_compute_tris_to_rasterize(scene_t *scene) {
  int mesh_num;
  MAT *M_vpproj = m_get_ident(4);
  m_mlt_self_left(M_vpproj, scene->M_cam);
  fprintf(stderr, "Camera:\n");
  print_mat(M_vpproj);
  fprintf(stderr, "viewport:\n");
  print_mat(scene->M_viewport);
  fprintf(stderr, "Projection:\n");
  print_mat(scene->M_proj);
  fprintf(stderr, "Camera:\n");
  m_mlt_self_left(M_vpproj, scene->M_proj);
  fprintf(stderr, "Projection*Camera:\n");
  print_mat(M_vpproj);
  m_mlt_self_left(M_vpproj, scene->M_viewport);
  fprintf(stderr, "Viewport*Projection*Camera:\n");
  print_mat(M_vpproj);
  int tris_computed = 0;
  triangle2d_t *projected_tris = (triangle2d_t*)malloc(sizeof(triangle2d_t) * scene->total_tris);
  fprintf(stderr, "Scene has %d meshes\n", scene->n);
  for(mesh_num=0; mesh_num<scene->n; mesh_num++) {
    int tri_num;
    mesh_t current_mesh = scene->meshes[mesh_num];
    fprintf(stderr, "Mesh modelview matrix:\n");
    print_mat(current_mesh.transform);
    fprintf(stderr, "Mesh %d has %d triangles\n", mesh_num, current_mesh.n);
    for(tri_num=0; tri_num<current_mesh.n; tri_num++) {
      triangle_t current_tri = current_mesh.tris[tri_num];
      int v;
      triangle2d_t *projectedTri = &projected_tris[tris_computed];
      point2d_t projectedPoints[3];
      for(v=0; v<3; v++) {
        MAT *currentPoint = vec_to_mat(current_tri.verts[v]);
        MAT *proj_point = m_get(4,1);
        MAT *transform = m_get(4,4);
        m_mlt(M_vpproj, current_mesh.transform, transform);
        m_mlt(transform, currentPoint, proj_point);
        projectedPoints[v].x = proj_point->me[0][0]/ proj_point->me[3][0];
        projectedPoints[v].y = proj_point->me[0][1]/ proj_point->me[3][0];
        projectedPoints[v].depth = computeDepth(scene, proj_point->me[2][0] / proj_point->me[3][0]);
        projectedPoints[v].color = computeColor(current_tri.verts[v].color);
        fprintf(stderr, "Point in modelview coords: %f %f %f %f\n", current_tri.verts[v].x, current_tri.verts[v].y, current_tri.verts[v].z, current_tri.verts[v].t);
        fprintf(stderr, "Point after transforms: %f %f %f %f\n", proj_point->me[0][0], proj_point->me[1][0], proj_point->me[2][0], proj_point->me[3][0]);
        fprintf(stderr, "Point projected to: %f %f\n", projectedPoints[v].x, projectedPoints[v].y);
      }
      projectedTri->v1 = projectedPoints[0];
      projectedTri->v2 = projectedPoints[1];
      projectedTri->v3 = projectedPoints[2];
      tris_computed++;
    }
  }
  M_FREE(M_vpproj);
  return projected_tris;
}

canonical_original_code:
struct triangle2d_t *scene_compute_tris_to_rasterize(struct scene_t *scene) {
  int mesh_num;
  struct MAT *M_vpproj = m_get_ident(4);
  m_mlt_self_left(M_vpproj, scene->M_cam);
  fprintf(stderr, "Camera:\n");
  print_mat(M_vpproj);
  fprintf(stderr, "viewport:\n");
  print_mat(scene->M_viewport);
  fprintf(stderr, "Projection:\n");
  print_mat(scene->M_proj);
  fprintf(stderr, "Camera:\n");
  m_mlt_self_left(M_vpproj, scene->M_proj);
  fprintf(stderr, "Projection*Camera:\n");
  print_mat(M_vpproj);
  m_mlt_self_left(M_vpproj, scene->M_viewport);
  fprintf(stderr, "Viewport*Projection*Camera:\n");
  print_mat(M_vpproj);
  int tris_computed = 0;
  struct triangle2d_t *projected_tris = (triangle2d_t*)malloc(sizeof(triangle2d_t) * scene->total_tris);
  fprintf(stderr, "Scene has %d meshes\n", scene->n);
  for(mesh_num=0; mesh_num<scene->n; mesh_num++) {
    int tri_num;
    struct mesh_t current_mesh = scene->meshes[mesh_num];
    fprintf(stderr, "Mesh modelview matrix:\n");
    print_mat(current_mesh.transform);
    fprintf(stderr, "Mesh %d has %d triangles\n", mesh_num, current_mesh.n);
    for(tri_num=0; tri_num<current_mesh.n; tri_num++) {
      struct triangle_t current_tri = current_mesh.tris[tri_num];
      int v;
      struct triangle2d_t *projectedTri = &projected_tris[tris_computed];
      struct point2d_t projectedPoints[3];
      for(v=0; v<3; v++) {
        struct MAT *currentPoint = vec_to_mat(current_tri.verts[v]);
        struct MAT *proj_point = m_get(4,1);
        struct MAT *transform = m_get(4,4);
        m_mlt(M_vpproj, current_mesh.transform, transform);
        m_mlt(transform, currentPoint, proj_point);
        projectedPoints[v].x = proj_point->me[0][0]/ proj_point->me[3][0];
        projectedPoints[v].y = proj_point->me[0][1]/ proj_point->me[3][0];
        projectedPoints[v].depth = computeDepth(scene, proj_point->me[2][0] / proj_point->me[3][0]);
        projectedPoints[v].color = computeColor(current_tri.verts[v].color);
        fprintf(stderr, "Point in modelview coords: %f %f %f %f\n", current_tri.verts[v].x, current_tri.verts[v].y, current_tri.verts[v].z, current_tri.verts[v].t);
        fprintf(stderr, "Point after transforms: %f %f %f %f\n", proj_point->me[0][0], proj_point->me[1][0], proj_point->me[2][0], proj_point->me[3][0]);
        fprintf(stderr, "Point projected to: %f %f\n", projectedPoints[v].x, projectedPoints[v].y);
      }
      projectedTri->v1 = projectedPoints[0];
      projectedTri->v2 = projectedPoints[1];
      projectedTri->v3 = projectedPoints[2];
      tris_computed++;
    }
  }
  M_FREE(M_vpproj);
  return projected_tris;
}

variable_types: struct triangle2d_t { struct point2d_t { float x; unsigned int depth; unsigned int color; } v1; } *

variable_types: struct mesh_t { struct triangle_t { struct vec4_t { float x; struct fcolor_t { float r; } color; } verts[3]; } *tris; int n; struct MAT { unsigned int m; float **me; } *transform; }

variable_types: v int

variable_types: struct point2d_t { float x; unsigned int depth; unsigned int color; } [3]

user_defined_types: struct camera_t { struct MAT *transform; }


decompiled_code:
void __fastcall scene_draw(__int64 a1, __int64 a2)
{
char *v2; // rbp

v2 = scene_compute_tris_to_rasterize(a1);
fprintf(stderr, "Total triangles on the scene: %d\n", *(unsigned int *)(a1 + 48));
rasterize(a2, v2, *(unsigned int *)(a1 + 48));
free(v2);
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1, __int64 a2)
{
char *v2; // rbp

v2 = func2(a1);
fprintf(stderr, "Total triangles on the scene: %d\n", *(unsigned int *)(a1 + 48));
rasterize(a2, v2, *(unsigned int *)(a1 + 48));
free(v2);
}

original_code:
void scene_draw(scene_t *scene, bitmap_t* bmp) {
  triangle2d_t *projected_tris = scene_compute_tris_to_rasterize(scene);
  fprintf(stderr, "Total triangles on the scene: %d\n", scene->total_tris);
  rasterize(bmp, projected_tris, scene->total_tris);
  free(projected_tris);
}

canonical_original_code:
void scene_draw(struct scene_t *scene, struct bitmap_t *bmp) {
  struct triangle2d_t *projected_tris = scene_compute_tris_to_rasterize(scene);
  fprintf(stderr, "Total triangles on the scene: %d\n", scene->total_tris);
  rasterize(bmp, projected_tris, scene->total_tris);
  free(projected_tris);
}

variable_types: struct bitmap_t { int w; int h; unsigned int *data; } *

user_defined_types: struct point2d_t { float x; unsigned int depth; unsigned int color; }


getViewportTransformation

original_code:
MAT* getViewportTransformation(float nx,float ny) {
    MAT* transMat = m_get(4,4);
    m_zero(transMat);
    transMat->me[0][0] = nx/2;
    transMat->me[0][3] = (nx-1)/2;
    transMat->me[1][1] = ny/2;
    transMat->me[1][3] = (ny-1)/2;
    transMat->me[2][2] = 1;
    transMat->me[3][3] = 1;
    return transMat;
}

canonical_original_code:
struct MAT *getViewportTransformation(float nx, float ny) {
    struct MAT *transMat = m_get(4,4);
    m_zero(transMat);
    transMat->me[0][0] = nx/2;
    transMat->me[0][3] = (nx-1)/2;
    transMat->me[1][1] = ny/2;
    transMat->me[1][3] = (ny-1)/2;
    transMat->me[2][2] = 1;
    transMat->me[3][3] = 1;
    return transMat;
}

variable_types: nx float

variable_types: ny float

variable_types: struct MAT { unsigned int m; float **me; } *


getOrthographicProjection

decompiled_code:
__int64 __fastcall getOrthographicProjection(float a1, float a2, float a3, float a4, float a5, float a6)
{
__int64 v6; // rbx

v6 = m_get(4LL, 4LL);
m_zero(v6);
***(float ***)(v6 + 8) = 2.0 / (float)(a2 - a1);
*(float *)(*(_QWORD *)(*(_QWORD *)(v6 + 8) + 8LL) + 4LL) = 2.0 / (float)(a4 - a3);
*(float *)(*(_QWORD *)(*(_QWORD *)(v6 + 8) + 16LL) + 8LL) = 2.0 / (float)(a5 - a6);
*(float *)(**(_QWORD **)(v6 + 8) + 12LL) = COERCE_FLOAT(COERCE_UNSIGNED_INT(a2 + a1) ^ LC3) / (float)(a2 - a1);
*(float *)(*(_QWORD *)(*(_QWORD *)(v6 + 8) + 8LL) + 12LL) = COERCE_FLOAT(COERCE_UNSIGNED_INT(a4 + a3) ^ LC3)
/ (float)(a4 - a3);
*(float *)(*(_QWORD *)(*(_QWORD *)(v6 + 8) + 16LL) + 12LL) = COERCE_FLOAT(COERCE_UNSIGNED_INT(a5 + a6) ^ LC3)
/ (float)(a5 - a6);
*(_DWORD *)(*(_QWORD *)(*(_QWORD *)(v6 + 8) + 24LL) + 12LL) = 1065353216;
return v6;
}

original_code:
MAT* getOrthographicProjection(float l,float r,float b,float t,float n,float f){
    MAT* transMat = m_get(4,4);
    m_zero(transMat);
    transMat->me[0][0] = 2/(r-l);
    transMat->me[1][1] = 2/(t-b);
    transMat->me[2][2] = 2/(n-f);
    transMat->me[0][3] = -(r+l)/(r-l);
    transMat->me[1][3] = -(t+b)/(t-b);
    transMat->me[2][3] = -(n+f)/(n-f);
    transMat->me[3][3] = 1;
    return transMat;
}

canonical_original_code:
struct MAT *getOrthographicProjection(float l, float r, float b, float t, float n, float f){
    struct MAT *transMat = m_get(4,4);
    m_zero(transMat);
    transMat->me[0][0] = 2/(r-l);
    transMat->me[1][1] = 2/(t-b);
    transMat->me[2][2] = 2/(n-f);
    transMat->me[0][3] = -(r+l)/(r-l);
    transMat->me[1][3] = -(t+b)/(t-b);
    transMat->me[2][3] = -(n+f)/(n-f);
    transMat->me[3][3] = 1;
    return transMat;
}

variable_types: l float

variable_types: r float

variable_types: b float

variable_types: t float

variable_types: n float

variable_types: f float


lookAt

original_code:
MAT* lookAt(vec3_t eye, vec3_t center, vec3_t up) {
    vec3_t w = v3_normalize( v3_sub(eye, center) );
    vec3_t u = v3_cross( up, w );
    u = v3_normalize(u);
    vec3_t v = v3_cross(w, u);
    vec3_t t = v3_init( v3_dot(u,eye), v3_dot(v, eye), v3_dot(w, eye) );
    MAT* mat = m_get(4,4);
    m_zero(mat);
    mat->me[0][0] = u.x; mat->me[0][1] = u.y; mat->me[0][2] = u.z; mat->me[0][3] = -t.x;
    mat->me[1][0] = v.x; mat->me[1][1] = v.y; mat->me[1][2] = v.z; mat->me[1][3] = -t.y;
    mat->me[2][0] = w.x; mat->me[2][1] = w.y; mat->me[2][2] = w.z; mat->me[2][3] = -t.z;
    mat->me[3][3] = 1;
    return mat;
}

canonical_original_code:
struct MAT *lookAt(struct vec3_t eye, struct vec3_t center, struct vec3_t up) {
    struct vec3_t w = v3_normalize( v3_sub(eye, center) );
    struct vec3_t u = v3_cross( up, w );
    u = v3_normalize(u);
    struct vec3_t v = v3_cross(w, u);
    struct vec3_t t = v3_init( v3_dot(u,eye), v3_dot(v, eye), v3_dot(w, eye) );
    struct MAT *mat = m_get(4,4);
    m_zero(mat);
    mat->me[0][0] = u.x; mat->me[0][1] = u.y; mat->me[0][2] = u.z; mat->me[0][3] = -t.x;
    mat->me[1][0] = v.x; mat->me[1][1] = v.y; mat->me[1][2] = v.z; mat->me[1][3] = -t.y;
    mat->me[2][0] = w.x; mat->me[2][1] = w.y; mat->me[2][2] = w.z; mat->me[2][3] = -t.z;
    mat->me[3][3] = 1;
    return mat;
}

variable_types:
eye
struct vec3_t { float x; }

variable_types:
center
struct vec3_t

variable_types:
up
struct vec3_t

variable_types:
w
struct vec3_t { float x; }

variable_types:
u
struct vec3_t { float x; }

variable_types:
v
struct vec3_t { float x; }

variable_types:
t
struct vec3_t { float x; }


getPerspectiveProjection

original_code:
MAT* getPerspectiveProjection(float n, float r, float l, float t, float b, float f){
    MAT* transMat = m_get(4,4);
    m_zero(transMat);
    transMat->me[0][0] = 2*n/(r-l);
    transMat->me[0][2] = (l+r)/(l-r);
    transMat->me[1][1] = 2*n/(t-b);
    transMat->me[1][2] = (b+t)/(b-t);
    transMat->me[2][2] = (f+n)/(n-f);
    transMat->me[2][3] = 2*f*n/(f-n);
    transMat->me[3][2] = 1;
    return transMat;
}

canonical_original_code:
struct MAT *getPerspectiveProjection(float n, float r, float l, float t, float b, float f){
    struct MAT *transMat = m_get(4,4);
    m_zero(transMat);
    transMat->me[0][0] = 2*n/(r-l);
    transMat->me[0][2] = (l+r)/(l-r);
    transMat->me[1][1] = 2*n/(t-b);
    transMat->me[1][2] = (b+t)/(b-t);
    transMat->me[2][2] = (f+n)/(n-f);
    transMat->me[2][3] = 2*f*n/(f-n);
    transMat->me[3][2] = 1;
    return transMat;
}


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

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

original_code:
void M_FREE(MAT *A){
    free(A->me);
    free(A);
}

canonical_original_code:
void M_FREE(struct MAT *A){
    free(A->me);
    free(A);
}


canonical_original_code:
struct MAT *m_zero(struct MAT *A)
{
 int i; int j; int A_m; int A_n;
 A_m = A->m; A_n = A->n;
 for ( i = 0; i < A_m; i++ ){
  for ( j = 0; j < A_n; j++ ){
   A->me[i][j] = 0.0;
     }
    }
 return A;
}

variable_types:
A
struct MAT *

variable_types: i int

variable_types: j int


decompiled_code:
unsigned int *__fastcall m_get_ident(int a1)
{
unsigned int *v1; // rbp
__int64 v2; // rax

v1 = m_get(a1, a1);
m_zero(v1);
if ( a1 > 0 )
{
v2 = 0LL;
do
{
*(_DWORD *)(*(_QWORD *)(*((_QWORD *)v1 + 1) + 2 * v2) + v2) = LC3;
v2 += 4LL;
}
while ( v2 != 4LL * (unsigned int)a1 );
}
return v1;
}

canonical_decompiled_code:
unsigned int *__fastcall func2(int a1)
{
unsigned int *v1; // rbp
__int64 v2; // rax

v1 = m_get(a1, a1);
func1(v1);
if ( a1 > 0 )
{
v2 = 0LL;
do
{
*(_DWORD *)(*(_QWORD *)(*((_QWORD *)v1 + 1) + 2 * v2) + v2) = LC3;
v2 += 4LL;
}
while ( v2 != 4LL * (unsigned int)a1 );
}
return v1;
}

original_code:
MAT *m_get_ident(int m){
    MAT* new = m_get(m, m);
    m_zero(new);
    int i;
    for(i = 0; i<m; ++i){
        new -> me[i][i] = 1;
    }
    return new;
}

canonical_original_code:
struct MAT *m_get_ident(int m){
    struct MAT *new = m_get(m, m);
    m_zero(new);
    int i;
    for(i = 0; i<m; ++i){
        new -> me[i][i] = 1;
    }
    return new;
}

variable_types: m int

variable_types: struct MAT { unsigned int m; float **me; } *


original_code:
MAT *m_mlt(const MAT *A, const MAT *B, MAT *OUT)
{
 unsigned int i, j, k, m, n, p;
 float **A_v, **B_v, sum;
 if ( A==(MAT *)((void *)0) || B==(MAT *)((void *)0) )
  perror("Matrixes cannot be null");
 if ( A->n != B->m )
  perror("Sizes do not match");
 if ( A == OUT || B == OUT )
  perror("Sizes do not match");
 m = A->m;
 n = A->n;
 p = B->n;
 A_v = A->me;
 B_v = B->me;
 for ( i=0; i<m; i++ )
  for ( j=0; j<p; j++ )
  {
   sum = 0.0;
   for ( k=0; k<n; k++ )
    sum += A_v[i][k]*B_v[k][j];
   OUT->me[i][j] = sum;
  }
 return OUT;
}

canonical_original_code:
struct MAT *m_mlt(struct MAT *A, struct MAT *B, struct MAT *OUT)
{
 unsigned int i; unsigned int j; unsigned int k; unsigned int m; unsigned int n; unsigned int p;
 float **A_v; float **B_v; float sum;
 if ( A==(struct MAT *)((void *)0) || B==(struct MAT *)((void *)0) )
  perror("Matrixes cannot be null");
 if ( A->n != B->m )
  perror("Sizes do not match");
 if ( A == OUT || B == OUT )
  perror("Sizes do not match");
 m = A->m;
 n = A->n;
 p = B->n;
 A_v = A->me;
 B_v = B->me;
 for ( i=0; i<m; i++ )
  for ( j=0; j<p; j++ )
  {
   sum = 0.0;
   for ( k=0; k<n; k++ )
    sum += A_v[i][k]*B_v[k][j];
   OUT->me[i][j] = sum;
  }
 return OUT;
}

variable_types:
B
struct MAT *

variable_types:
OUT
struct MAT *

variable_types:
i
unsigned int

variable_types:
j
unsigned int

variable_types:
k
unsigned int

variable_types:
m
unsigned int

variable_types:
n
unsigned int

variable_types:
p
unsigned int

variable_types: sum float


decompiled_code:
__int64 __fastcall m_mlt_self_left(__int64 a1, unsigned int *a2)
{
_DWORD *v2; // rax

v2 = m_copy(a1);
m_mlt(a2, (__int64)v2, a1);
return a1;
}

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

v2 = m_copy(a1);
func3(a2, (__int64)v2, a1);
return a1;
}

original_code:
MAT *m_mlt_self_left(MAT *A, const MAT *B) {
  MAT *old = m_copy(A);
  m_mlt(B, old, A);
  return A;
}

canonical_original_code:
struct MAT *m_mlt_self_left(struct MAT *A, struct MAT *B) {
  struct MAT *old = m_copy(A);
  m_mlt(B, old, A);
  return A;
}


decompiled_code:
__int64 __fastcall test_rw()
{
char *v0; // rbp
unsigned int v1; // er12
__pid_t v2; // eax
struct timespec v4; // [rsp+0h] [rbp-68h] BYREF
__int64 v5; // [rsp+20h] [rbp-48h]

if ( (unsigned int)mq_open() == -1 )
{
perror("Failed to open message queue");
v1 = -1;
}
else
{
v1 = mq_getattr();
if ( v1 == -1 )
{
perror("Failed to get msq attr");
}
else
{
printf("Max message size: %ld\n", v5);
v0 = (char *)malloc(v5 + 1);
if ( v0 )
{
clock_gettime(0, &v4);
++v4.tv_sec;
v2 = fork();
if ( v2 < 0 )
{
perror("Failed to spawn process");
v1 = -1;
}
else
{
if ( v2 <= 0 )
{
mq_close();
if ( (unsigned int)mq_open() != -1 )
{
strcpy(v0, "shit");
if ( (unsigned int)mq_timedsend() != -1 )
{
puts("Message sent");
free(v0);
mq_close();
exit(0);
}
perror("mq_timedsend failed");
free(v0);
mq_close();
exit(1);
}
perror("Failed to open message queue in write-only mode");
free(v0);
exit(1);
}
if ( mq_timedreceive() == -1 )
{
perror("mq_timedreceive failed");
v1 = -1;
}
else
{
v0[4] = 0;
printf("Message received: %s\n", v0);
v1 = 0;
}
}
}
else
{
perror("malloc failed");
v1 = -1;
}
}
free(v0);
mq_close();
mq_unlink();
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func7()
{
char *v0; // rbp
unsigned int v1; // er12
__pid_t v2; // eax
struct timespec v4; // [rsp+0h] [rbp-68h] BYREF
__int64 v5; // [rsp+20h] [rbp-48h]

if ( (unsigned int)mq_open() == -1 )
{
perror("Failed to open message queue");
v1 = -1;
}
else
{
v1 = mq_getattr();
if ( v1 == -1 )
{
perror("Failed to get msq attr");
}
else
{
printf("Max message size: %ld\n", v5);
v0 = (char *)malloc(v5 + 1);
if ( v0 )
{
clock_gettime(0, &v4);
++v4.tv_sec;
v2 = fork();
if ( v2 < 0 )
{
perror("Failed to spawn process");
v1 = -1;
}
else
{
if ( v2 <= 0 )
{
mq_close();
if ( (unsigned int)mq_open() != -1 )
{
strcpy(v0, "shit");
if ( (unsigned int)mq_timedsend() != -1 )
{
puts("Message sent");
free(v0);
mq_close();
exit(0);
}
perror("mq_timedsend failed");
free(v0);
mq_close();
exit(1);
}
perror("Failed to open message queue in write-only mode");
free(v0);
exit(1);
}
if ( mq_timedreceive() == -1 )
{
perror("mq_timedreceive failed");
v1 = -1;
}
else
{
v0[4] = 0;
printf("Message received: %s\n", v0);
v1 = 0;
}
}
}
else
{
perror("malloc failed");
v1 = -1;
}
}
free(v0);
mq_close();
mq_unlink();
}
return v1;
}

original_code:
int test_rw()
{
 struct mq_attr qattr;
 char *msgbuf;
 mqd_t mqid;
 pid_t pid;
 struct timespec t;
 int ret = 0;
 mqid = mq_open("/msq_test", 00 | 0100 | 0200, 0600, ((void *)0));
 if (mqid == (mqd_t) -1) {
  perror("Failed to open message queue");
  return -1;
 }
 if (mq_getattr(mqid, &qattr) == -1) {
  perror("Failed to get msq attr");
  ret = -1;
  goto finish;
 }
 printf("Max message size: %ld\n", qattr.mq_msgsize);
 msgbuf = (char *)malloc(qattr.mq_msgsize + 1);
 if (!msgbuf) {
  perror("malloc failed");
  ret = -1;
  goto finish;
 }
 clock_gettime(0, &t);
 t.tv_sec++;
 pid = fork();
 if (pid < 0) {
  perror("Failed to spawn process");
  ret = -1;
  goto finish;
 }
 if (pid > 0) {
  if (mq_timedreceive(mqid, msgbuf, qattr.mq_msgsize + 1, ((void *)0), &t) == -1) {
   perror("mq_timedreceive failed");
   ret = -1;
   goto finish;
  }
  msgbuf[4] = '\0';
  printf("Message received: %s\n", msgbuf);
 }
 else {
  mq_close(mqid);
  mqid = mq_open("/msq_test", 01);
  if (mqid == (mqd_t) -1) {
   perror("Failed to open message queue in write-only mode");
   free(msgbuf);
   exit(1);
  }
  memcpy(msgbuf, "shit\0", 5);
  if (mq_timedsend(mqid, msgbuf, 5, 0, &t) == -1) {
   perror("mq_timedsend failed");
   free(msgbuf);
   mq_close(mqid);
   exit(1);
  }
  printf("Message sent\n");
  free(msgbuf);
  mq_close(mqid);
  exit(0);
 }
finish:
 free(msgbuf);
 mq_close(mqid);
 mq_unlink("/msq_test");
 return ret;
}

canonical_original_code:
int test_rw()
{
 struct mq_attr qattr;
 char *msgbuf;
 int mqid;
 int pid;
 struct timespec t;
 int ret = 0;
 mqid = mq_open("/msq_test", 00 | 0100 | 0200, 0600, ((void *)0));
 if (mqid == (mqd_t) -1) {
  perror("Failed to open message queue");
  return -1;
 }
 if (mq_getattr(mqid, &qattr) == -1) {
  perror("Failed to get msq attr");
  ret = -1;
  goto finish;
 }
 printf("Max message size: %ld\n", qattr.mq_msgsize);
 msgbuf = (char *)malloc(qattr.mq_msgsize + 1);
 if (!msgbuf) {
  perror("malloc failed");
  ret = -1;
  goto finish;
 }
 clock_gettime(0, &t);
 t.tv_sec++;
 pid = fork();
 if (pid < 0) {
  perror("Failed to spawn process");
  ret = -1;
  goto finish;
 }
 if (pid > 0) {
  if (mq_timedreceive(mqid, msgbuf, qattr.mq_msgsize + 1, ((void *)0), &t) == -1) {
   perror("mq_timedreceive failed");
   ret = -1;
   goto finish;
  }
  msgbuf[4] = '\0';
  printf("Message received: %s\n", msgbuf);
 }
 else {
  mq_close(mqid);
  mqid = mq_open("/msq_test", 01);
  if (mqid == (mqd_t) -1) {
   perror("Failed to open message queue in write-only mode");
   free(msgbuf);
   exit(1);
  }
  memcpy(msgbuf, "shit\0", 5);
  if (mq_timedsend(mqid, msgbuf, 5, 0, &t) == -1) {
   perror("mq_timedsend failed");
   free(msgbuf);
   mq_close(mqid);
   exit(1);
  }
  printf("Message sent\n");
  free(msgbuf);
  mq_close(mqid);
  exit(0);
 }
finish:
 free(msgbuf);
 mq_close(mqid);
 mq_unlink("/msq_test");
 return ret;
}

variable_types: struct mq_attr { long mq_flags; long mq_maxmsg; long mq_msgsize; long mq_curmsgs; long __pad[4]; }

variable_types:
msgbuf
char *

variable_types: mqid int

variable_types: pid int

variable_types: struct timespec { long tv_sec; long tv_nsec; }

variable_types: ret int

return_type: int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
return test_rw(argc, argv, envp);
}

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

original_code:
int main()
{
 int ret = 0;
 ret |= test_rw();
 return ret;
}


messageToValue

decompiled_code:
__int64 __fastcall messageToValue(char *a1, __int64 a2, __int64 a3)
{
char *v3; // r12
char i; // bl

v3 = a1;
for ( i = *a1; *v3; i = *v3 )
{
_gmpz_mul_ui(a2, a2, 256LL);
_gmpz_add_ui(a2, a2, (unsigned int)i);
++v3;
}
return _gmpz_get_str(a3, 10LL, a2);
}

canonical_decompiled_code:
__int64 __fastcall func0(char *a1, __int64 a2, __int64 a3)
{
char *v3; // r12
char i; // bl

v3 = a1;
for ( i = *a1; *v3; i = *v3 )
{
_gmpz_mul_ui(a2, a2, 256LL);
_gmpz_add_ui(a2, a2, (unsigned int)i);
++v3;
}
return _gmpz_get_str(a3, 10LL, a2);
}

original_code:
void messageToValue(void *message, mpz_t message_mpz, char *m){
  char* c = ((void *)0);
  unsigned int value = 0, size = 0;
  c = message;
  while(*c != '\0'){
   value = (unsigned int) *c;
 __gmpz_mul_ui(message_mpz, message_mpz, 256);
   __gmpz_add_ui(message_mpz, message_mpz, value);
 c += 1;
  }
  __gmpz_get_str(m, 10, message_mpz);
}

canonical_original_code:
void messageToValue(void *message, struct __mpz_struct message_mpz[1], char *m){
  char *c = ((void *)0);
  unsigned int value = 0; unsigned int size = 0;
  c = message;
  while(*c != '\0'){
   value = (unsigned int) *c;
 __gmpz_mul_ui(message_mpz, message_mpz, 256);
   __gmpz_add_ui(message_mpz, message_mpz, value);
 c += 1;
  }
  __gmpz_get_str(m, 10, message_mpz);
}

variable_types:
message
void *

variable_types: struct __mpz_struct { int _mp_alloc; int _mp_size; unsigned long *_mp_d; } [1]

variable_types:
m
char *

variable_types:
c
char *

variable_types:
value
unsigned int

variable_types:
size
unsigned int


strAppend

decompiled_code:
char *__fastcall strAppend(void *ptr, char *src)
{
size_t v2; // r12
size_t v3; // rax
char *v4; // rax

v2 = strlen((const char *)ptr);
v3 = strlen(src);
v4 = (char *)realloc(ptr, v2 + v3 + 50);
return strcat(v4, src);
}

canonical_decompiled_code:
char *__fastcall func7(void *ptr, char *src)
{
size_t v2; // r12
size_t v3; // rax
char *v4; // rax

v2 = strlen((const char *)ptr);
v3 = strlen(src);
v4 = (char *)realloc(ptr, v2 + v3 + 50);
return strcat(v4, src);
}

original_code:
char* strAppend(char* str1, const char* str2) {
    str1 = realloc(str1, (strlen(str1) + strlen(str2) + 50) * sizeof(char));
    strcat(str1, str2);
    return str1;
}

canonical_original_code:
char *strAppend(char *str1, char *str2) {
    str1 = realloc(str1, (strlen(str1) + strlen(str2) + 50) * sizeof(char));
    strcat(str1, str2);
    return str1;
}

return_type: char *


searchmem

decompiled_code:
__int64 __fastcall searchmem(void *s1, unsigned __int64 a2, const void *a3, size_t a4)
{
__int64 result; // rax
char *v5; // rbx
size_t v8; // rsi

result = 1LL;
if ( a4 )
{
v5 = (char *)s1;
result = 0LL;
if ( a2 >= a4 )
{
v8 = a2 - a4;
if ( s1 >= (char *)s1 + v8 )
{
result = 0LL;
}
else
{
while ( memcmp(v5, a3, a4) )
{
if ( (char *)s1 + v8 == ++v5 )
return 0LL;
}
result = 1LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(void *s1, unsigned __int64 a2, const void *a3, size_t a4)
{
__int64 result; // rax
char *v5; // rbx
size_t v8; // rsi

result = 1LL;
if ( a4 )
{
v5 = (char *)s1;
result = 0LL;
if ( a2 >= a4 )
{
v8 = a2 - a4;
if ( s1 >= (char *)s1 + v8 )
{
result = 0LL;
}
else
{
while ( memcmp(v5, a3, a4) )
{
if ( (char *)s1 + v8 == ++v5 )
return 0LL;
}
result = 1LL;
}
}
}
return result;
}

original_code:
int searchmem(char* haystack, size_t haystackLength, char* needle, size_t needleLength){
    char* curpos = haystack;
    char* lastpos = haystack + haystackLength - needleLength;
    if (needleLength == 0){
        return 1;
    }
    if (haystackLength < needleLength){
        return 0;
    }
    while (curpos < lastpos){
        if (!memcmp(curpos,needle,needleLength)){
            return 1;
        }
  curpos++;
    }
    return 0;
}

canonical_original_code:
int searchmem(char *haystack, unsigned long haystackLength, char *needle, unsigned long needleLength){
    char *curpos = haystack;
    char *lastpos = haystack + haystackLength - needleLength;
    if (needleLength == 0){
        return 1;
    }
    if (haystackLength < needleLength){
        return 0;
    }
    while (curpos < lastpos){
        if (!memcmp(curpos,needle,needleLength)){
            return 1;
        }
  curpos++;
    }
    return 0;
}

variable_types:
haystack
char *

variable_types:
haystackLength
unsigned long

variable_types:
needle
char *

variable_types:
needleLength
unsigned long

variable_types:
curpos
char *

variable_types:
lastpos
char *