compilesong

decompiled_code:
int *__fastcall compilesong(__int64 a1, int a2)
{
__int64 v3; // rax
int *v4; // r12
int v5; // ebx
int v6; // eax
unsigned __int8 *v7; // rdx
int v8; // ecx
int v9; // er15
int *v10; // rax
int *v11; // rdi
int v12; // edx
__int64 v13; // r13
_QWORD *v14; // rax
_QWORD *v15; // rbp
void *v16; // rax
int v17; // ebx
int v18; // eax
_DWORD *v19; // rdx
bool v20; // zf
__int64 v21; // rcx
int *v23; // rdi
__int64 v24; // rax
char v25; // cl
int v26; // eax
int v27; // [rsp+4h] [rbp-44h] BYREF
int v28; // [rsp+8h] [rbp-40h] BYREF
int v29[15]; // [rsp+Ch] [rbp-3Ch] BYREF

v3 = initsong();
if ( !v3 )
return 0LL;
v4 = (int *)v3;
chartolower(a1, (unsigned int)a2);
v27 = 0;
if ( a2 > 0 )
{
v5 = 0;
v6 = 0;
while ( 1 )
{
v29[0] = 0;
v28 = 0;
v7 = (unsigned __int8 *)(a1 + v6);
if ( v6 >= a2 - 1 )
{
LABEL_12:
switch ( *v7 )
{
case '!':
v29[0] = 11;
v9 = 1;
v28 = 767;
goto LABEL_14;
case '&':
v24 = v27;
v25 = 0;
break;
case '*':
v12 = v6 + 1;
if ( v6 + 1 < a2 )
{
v27 = v6 + 1;
switch ( *(_BYTE *)(a1 + v12) )
{
case ':':
v26 = v5;
v9 = 14;
++v5;
v26 <<= 8;
LOBYTE(v26) = -1;
v28 = v26;
goto LABEL_14;
case '>':
if ( v6 + 2 >= a2 )
return 0LL;
v9 = !islabel(a1, &v27) ? 10 : 20;
goto LABEL_76;
case 'a':
if ( (unsigned int)read2arg(&v27, &v28, v29, a2, a1) == 1 )
return 0LL;
v9 = 2;
goto LABEL_14;
case 'b':
v29[0] = 1;
goto LABEL_99;
case 'c':
if ( (unsigned int)read2arg(&v27, &v28, v29, a2, a1) == 1 )
return 0LL;
v9 = 4;
goto LABEL_14;
case 'd':
v29[0] = 3;
LABEL_99:
if ( (unsigned int)read1arg(&v27, &v28, a2, a1) == 1 )
return 0LL;
goto LABEL_88;
case 'e':
if ( v6 + 2 >= a2 )
return 0LL;
v9 = !islabel(a1, &v27) ? 6 : 16;
goto LABEL_76;
case 'f':
v9 = 12;
goto LABEL_14;
case 'g':
if ( v6 + 2 >= a2 )
return 0LL;
v9 = !islabel(a1, &v27) ? 8 : 18;
goto LABEL_76;
case 'h':
v9 = 13;
goto LABEL_14;
case 'i':
if ( v6 + 2 >= a2 )
return 0LL;
v9 = !islabel(a1, &v27) ? 5 : 15;
goto LABEL_76;
case 'j':
if ( v6 + 2 >= a2 )
return 0LL;
v9 = !islabel(a1, &v27) ? 9 : 19;
goto LABEL_76;
case 'l':
if ( v6 + 2 >= a2 )
return 0LL;
v9 = !islabel(a1, &v27) ? 7 : 17;
LABEL_76:
if ( (unsigned int)read1arg(&v27, &v28, a2, a1) == 1 )
return 0LL;
goto LABEL_14;
case 'm':
if ( (unsigned int)read2arg(&v27, &v28, v29, a2, a1) == 1 )
return 0LL;
LABEL_88:
v9 = 1;
goto LABEL_14;
case 'n':
if ( (unsigned int)read1arg(&v27, &v28, a2, a1) == 1 )
return 0LL;
v9 = 0;
goto LABEL_14;
case 'r':
v9 = 11;
goto LABEL_14;
case 's':
if ( (unsigned int)read2arg(&v27, &v28, v29, a2, a1) == 1 )
return 0LL;
v9 = 3;
goto LABEL_14;
default:
goto LABEL_17;
}
}
goto LABEL_16;
case '+':
v29[0] = 14;
v9 = 2;
v28 = 511;
goto LABEL_14;
case '-':
v29[0] = 14;
v9 = 3;
v28 = 511;
goto LABEL_14;
case '.':
v29[0] = 11;
v9 = 1;
v28 = 511;
goto LABEL_14;
case '0':
v29[0] = 14;
v9 = 1;
v28 = 255;
goto LABEL_14;
case '1':
v29[0] = 14;
v9 = 1;
v28 = 511;
goto LABEL_14;
case '2':
v29[0] = 14;
v9 = 1;
v28 = 767;
goto LABEL_14;
case '3':
v29[0] = 14;
v9 = 1;
v28 = 1023;
goto LABEL_14;
case '4':
v29[0] = 14;
v9 = 1;
v28 = 1279;
goto LABEL_14;
case '5':
v29[0] = 14;
v9 = 1;
v28 = 1535;
goto LABEL_14;
case '6':
v29[0] = 14;
v9 = 1;
v28 = 1791;
goto LABEL_14;
case 'a':
v28 = 2815;
v9 = 0;
goto LABEL_14;
case 'b':
v28 = 3327;
v9 = 0;
goto LABEL_14;
case 'c':
v28 = 511;
v9 = 0;
goto LABEL_14;
case 'd':
v28 = 1023;
v9 = 0;
goto LABEL_14;
case 'e':
v28 = 1535;
v9 = 0;
goto LABEL_14;
case 'f':
v28 = 1791;
v9 = 0;
goto LABEL_14;
case 'g':
v28 = 2303;
v9 = 0;
goto LABEL_14;
case 'h':
v29[0] = 3;
v9 = 1;
v28 = 767;
goto LABEL_14;
case 'i':
v29[0] = 3;
v9 = 1;
v28 = 2303;
goto LABEL_14;
case 'q':
v29[0] = 3;
v9 = 1;
v28 = 1279;
goto LABEL_14;
case 's':
v29[0] = 3;
v9 = 1;
v28 = 4351;
goto LABEL_14;
case 't':
v29[0] = 3;
v9 = 1;
v28 = 8447;
goto LABEL_14;
case 'w':
v29[0] = 3;
v9 = 1;
v28 = 511;
goto LABEL_14;
case 'x':
v28 = 255;
v9 = 0;
v10 = (int *)initcommand();
v11 = v10;
if ( !v10 )
goto LABEL_29;
goto LABEL_15;
case 'z':
v29[0] = 3;
v9 = 1;
v28 = 16639;
goto LABEL_14;
default:
goto LABEL_16;
}
while ( 1 )
{
v12 = v24;
if ( a2 <= (int)v24 )
break;
v12 = ++v24;
v25 = 1;
if ( *(_BYTE *)(a1 + v24) == 38 )
goto LABEL_17;
}
if ( !v25 )
v12 = v27;
goto LABEL_17;
}
v8 = *v7;
if ( v8 != 99 )
goto LABEL_6;
if ( v7[1] == 35 )
goto LABEL_63;
if ( v7[1] == 36 )
{
v28 = 3839;
}
else
{
LABEL_6:
if ( v8 == 100 )
{
if ( v7[1] == 35 )
goto LABEL_66;
if ( v7[1] == 36 )
{
LABEL_63:
v28 = 767;
goto LABEL_64;
}
}
if ( v8 == 101 )
{
if ( v7[1] == 35 )
{
v28 = 1791;
goto LABEL_64;
}
if ( v7[1] == 36 )
{
LABEL_66:
v28 = 1279;
goto LABEL_64;
}
}
if ( v8 != 102 )
goto LABEL_69;
if ( v7[1] == 35 )
goto LABEL_10;
if ( v7[1] != 36 )
{
LABEL_69:
if ( v8 == 103 )
{
if ( v7[1] == 35 )
goto LABEL_82;
if ( v7[1] == 36 )
{
LABEL_10:
v28 = 2047;
goto LABEL_64;
}
}
if ( v8 == 97 )
{
if ( v7[1] != 35 )
{
if ( v7[1] == 36 )
{
LABEL_82:
v28 = 2559;
goto LABEL_64;
}
goto LABEL_71;
}
}
else
{
LABEL_71:
if ( v8 != 98 )
goto LABEL_12;
if ( v7[1] == 35 )
{
v28 = 3583;
goto LABEL_64;
}
if ( v7[1] != 36 )
goto LABEL_12;
}
v28 = 3071;
goto LABEL_64;
}
v28 = 1535;
}
LABEL_64:
v9 = 0;
v27 = v6 + 1;
LABEL_14:
v10 = (int *)initcommand();
v11 = v10;
if ( !v10 )
{
LABEL_29:
v23 = v4;
v4 = 0LL;
freesong(v23);
return v4;
}
LABEL_15:
*v10 = v9;
v11[1] = v28;
v11[2] = v29[0];
addcmd(v11, v4);
LABEL_16:
v12 = v27;
LABEL_17:
v6 = v12 + 1;
v27 = v12 + 1;
if ( v12 + 1 >= a2 )
goto LABEL_18;
}
}
v5 = 0;
LABEL_18:
if ( !*((_QWORD *)v4 + 1) )
{
freesong(v4);
return 0LL;
}
rewindsong(v4);
v13 = (unsigned int)*v4;
v14 = malloc(8 * v13);
*((_QWORD *)v4 + 4) = v14;
v15 = v14;
if ( !v14 )
return 0LL;
v16 = malloc(4LL * v5);
*((_QWORD *)v4 + 6) = v16;
if ( !v16 )
return 0LL;
v27 = 0;
v4[10] = v5 - 1;
if ( (_DWORD)v13 )
{
v17 = 0;
v18 = 0;
while ( 1 )
{
v19 = (_DWORD *)*((_QWORD *)v4 + 3);
v20 = *v19 == 14;
v15[v18] = v19;
if ( v20 )
{
v21 = v17++;
*(_DWORD *)(*((_QWORD *)v4 + 6) + 4 * v21) = v18;
}
nextcommand(v4);
v18 = v27 + 1;
v27 = v18;
if ( *v4 <= (unsigned int)v18 )
break;
v15 = (_QWORD *)*((_QWORD *)v4 + 4);
}
}
return v4;
}

canonical_decompiled_code:
int *__fastcall func3(__int64 a1, int a2)
{
__int64 v3; // rax
int *v4; // r12
int v5; // ebx
int v6; // eax
unsigned __int8 *v7; // rdx
int v8; // ecx
int v9; // er15
int *v10; // rax
int *v11; // rdi
int v12; // edx
__int64 v13; // r13
_QWORD *v14; // rax
_QWORD *v15; // rbp
void *v16; // rax
int v17; // ebx
int v18; // eax
_DWORD *v19; // rdx
bool v20; // zf
__int64 v21; // rcx
int *v23; // rdi
__int64 v24; // rax
char v25; // cl
int v26; // eax
int v27; // [rsp+4h] [rbp-44h] BYREF
int v28; // [rsp+8h] [rbp-40h] BYREF
int v29[15]; // [rsp+Ch] [rbp-3Ch] BYREF

v3 = initsong();
if ( !v3 )
return 0LL;
v4 = (int *)v3;
chartolower(a1, (unsigned int)a2);
v27 = 0;
if ( a2 > 0 )
{
v5 = 0;
v6 = 0;
while ( 1 )
{
v29[0] = 0;
v28 = 0;
v7 = (unsigned __int8 *)(a1 + v6);
if ( v6 >= a2 - 1 )
{
LABEL_12:
switch ( *v7 )
{
case '!':
v29[0] = 11;
v9 = 1;
v28 = 767;
goto LABEL_14;
case '&':
v24 = v27;
v25 = 0;
break;
case '*':
v12 = v6 + 1;
if ( v6 + 1 < a2 )
{
v27 = v6 + 1;
switch ( *(_BYTE *)(a1 + v12) )
{
case ':':
v26 = v5;
v9 = 14;
++v5;
v26 <<= 8;
LOBYTE(v26) = -1;
v28 = v26;
goto LABEL_14;
case '>':
if ( v6 + 2 >= a2 )
return 0LL;
v9 = !func2(a1, &v27) ? 10 : 20;
goto LABEL_76;
case 'a':
if ( (unsigned int)func1(&v27, &v28, v29, a2, a1) == 1 )
return 0LL;
v9 = 2;
goto LABEL_14;
case 'b':
v29[0] = 1;
goto LABEL_99;
case 'c':
if ( (unsigned int)func1(&v27, &v28, v29, a2, a1) == 1 )
return 0LL;
v9 = 4;
goto LABEL_14;
case 'd':
v29[0] = 3;
LABEL_99:
if ( (unsigned int)func0(&v27, &v28, a2, a1) == 1 )
return 0LL;
goto LABEL_88;
case 'e':
if ( v6 + 2 >= a2 )
return 0LL;
v9 = !func2(a1, &v27) ? 6 : 16;
goto LABEL_76;
case 'f':
v9 = 12;
goto LABEL_14;
case 'g':
if ( v6 + 2 >= a2 )
return 0LL;
v9 = !func2(a1, &v27) ? 8 : 18;
goto LABEL_76;
case 'h':
v9 = 13;
goto LABEL_14;
case 'i':
if ( v6 + 2 >= a2 )
return 0LL;
v9 = !func2(a1, &v27) ? 5 : 15;
goto LABEL_76;
case 'j':
if ( v6 + 2 >= a2 )
return 0LL;
v9 = !func2(a1, &v27) ? 9 : 19;
goto LABEL_76;
case 'l':
if ( v6 + 2 >= a2 )
return 0LL;
v9 = !func2(a1, &v27) ? 7 : 17;
LABEL_76:
if ( (unsigned int)func0(&v27, &v28, a2, a1) == 1 )
return 0LL;
goto LABEL_14;
case 'm':
if ( (unsigned int)func1(&v27, &v28, v29, a2, a1) == 1 )
return 0LL;
LABEL_88:
v9 = 1;
goto LABEL_14;
case 'n':
if ( (unsigned int)func0(&v27, &v28, a2, a1) == 1 )
return 0LL;
v9 = 0;
goto LABEL_14;
case 'r':
v9 = 11;
goto LABEL_14;
case 's':
if ( (unsigned int)func1(&v27, &v28, v29, a2, a1) == 1 )
return 0LL;
v9 = 3;
goto LABEL_14;
default:
goto LABEL_17;
}
}
goto LABEL_16;
case '+':
v29[0] = 14;
v9 = 2;
v28 = 511;
goto LABEL_14;
case '-':
v29[0] = 14;
v9 = 3;
v28 = 511;
goto LABEL_14;
case '.':
v29[0] = 11;
v9 = 1;
v28 = 511;
goto LABEL_14;
case '0':
v29[0] = 14;
v9 = 1;
v28 = 255;
goto LABEL_14;
case '1':
v29[0] = 14;
v9 = 1;
v28 = 511;
goto LABEL_14;
case '2':
v29[0] = 14;
v9 = 1;
v28 = 767;
goto LABEL_14;
case '3':
v29[0] = 14;
v9 = 1;
v28 = 1023;
goto LABEL_14;
case '4':
v29[0] = 14;
v9 = 1;
v28 = 1279;
goto LABEL_14;
case '5':
v29[0] = 14;
v9 = 1;
v28 = 1535;
goto LABEL_14;
case '6':
v29[0] = 14;
v9 = 1;
v28 = 1791;
goto LABEL_14;
case 'a':
v28 = 2815;
v9 = 0;
goto LABEL_14;
case 'b':
v28 = 3327;
v9 = 0;
goto LABEL_14;
case 'c':
v28 = 511;
v9 = 0;
goto LABEL_14;
case 'd':
v28 = 1023;
v9 = 0;
goto LABEL_14;
case 'e':
v28 = 1535;
v9 = 0;
goto LABEL_14;
case 'f':
v28 = 1791;
v9 = 0;
goto LABEL_14;
case 'g':
v28 = 2303;
v9 = 0;
goto LABEL_14;
case 'h':
v29[0] = 3;
v9 = 1;
v28 = 767;
goto LABEL_14;
case 'i':
v29[0] = 3;
v9 = 1;
v28 = 2303;
goto LABEL_14;
case 'q':
v29[0] = 3;
v9 = 1;
v28 = 1279;
goto LABEL_14;
case 's':
v29[0] = 3;
v9 = 1;
v28 = 4351;
goto LABEL_14;
case 't':
v29[0] = 3;
v9 = 1;
v28 = 8447;
goto LABEL_14;
case 'w':
v29[0] = 3;
v9 = 1;
v28 = 511;
goto LABEL_14;
case 'x':
v28 = 255;
v9 = 0;
v10 = (int *)initcommand();
v11 = v10;
if ( !v10 )
goto LABEL_29;
goto LABEL_15;
case 'z':
v29[0] = 3;
v9 = 1;
v28 = 16639;
goto LABEL_14;
default:
goto LABEL_16;
}
while ( 1 )
{
v12 = v24;
if ( a2 <= (int)v24 )
break;
v12 = ++v24;
v25 = 1;
if ( *(_BYTE *)(a1 + v24) == 38 )
goto LABEL_17;
}
if ( !v25 )
v12 = v27;
goto LABEL_17;
}
v8 = *v7;
if ( v8 != 99 )
goto LABEL_6;
if ( v7[1] == 35 )
goto LABEL_63;
if ( v7[1] == 36 )
{
v28 = 3839;
}
else
{
LABEL_6:
if ( v8 == 100 )
{
if ( v7[1] == 35 )
goto LABEL_66;
if ( v7[1] == 36 )
{
LABEL_63:
v28 = 767;
goto LABEL_64;
}
}
if ( v8 == 101 )
{
if ( v7[1] == 35 )
{
v28 = 1791;
goto LABEL_64;
}
if ( v7[1] == 36 )
{
LABEL_66:
v28 = 1279;
goto LABEL_64;
}
}
if ( v8 != 102 )
goto LABEL_69;
if ( v7[1] == 35 )
goto LABEL_10;
if ( v7[1] != 36 )
{
LABEL_69:
if ( v8 == 103 )
{
if ( v7[1] == 35 )
goto LABEL_82;
if ( v7[1] == 36 )
{
LABEL_10:
v28 = 2047;
goto LABEL_64;
}
}
if ( v8 == 97 )
{
if ( v7[1] != 35 )
{
if ( v7[1] == 36 )
{
LABEL_82:
v28 = 2559;
goto LABEL_64;
}
goto LABEL_71;
}
}
else
{
LABEL_71:
if ( v8 != 98 )
goto LABEL_12;
if ( v7[1] == 35 )
{
v28 = 3583;
goto LABEL_64;
}
if ( v7[1] != 36 )
goto LABEL_12;
}
v28 = 3071;
goto LABEL_64;
}
v28 = 1535;
}
LABEL_64:
v9 = 0;
v27 = v6 + 1;
LABEL_14:
v10 = (int *)initcommand();
v11 = v10;
if ( !v10 )
{
LABEL_29:
v23 = v4;
v4 = 0LL;
freesong(v23);
return v4;
}
LABEL_15:
*v10 = v9;
v11[1] = v28;
v11[2] = v29[0];
addcmd(v11, v4);
LABEL_16:
v12 = v27;
LABEL_17:
v6 = v12 + 1;
v27 = v12 + 1;
if ( v12 + 1 >= a2 )
goto LABEL_18;
}
}
v5 = 0;
LABEL_18:
if ( !*((_QWORD *)v4 + 1) )
{
freesong(v4);
return 0LL;
}
rewindsong(v4);
v13 = (unsigned int)*v4;
v14 = malloc(8 * v13);
*((_QWORD *)v4 + 4) = v14;
v15 = v14;
if ( !v14 )
return 0LL;
v16 = malloc(4LL * v5);
*((_QWORD *)v4 + 6) = v16;
if ( !v16 )
return 0LL;
v27 = 0;
v4[10] = v5 - 1;
if ( (_DWORD)v13 )
{
v17 = 0;
v18 = 0;
while ( 1 )
{
v19 = (_DWORD *)*((_QWORD *)v4 + 3);
v20 = *v19 == 14;
v15[v18] = v19;
if ( v20 )
{
v21 = v17++;
*(_DWORD *)(*((_QWORD *)v4 + 6) + 4 * v21) = v18;
}
nextcommand(v4);
v18 = v27 + 1;
v27 = v18;
if ( *v4 <= (unsigned int)v18 )
break;
v15 = (_QWORD *)*((_QWORD *)v4 + 4);
}
}
return v4;
}

original_code:
song *compilesong(char *songstr, int length) {
 char cur;
 int pos;
 int labels;
 song *s;
 command *c;
 int addnew;
 int cmd;
 int data;
 int reg;
 s = initsong();
 if(s == ((void *)0))
  return(((void *)0));
 labels = 0;
 chartolower(songstr, length);
 for(pos = 0; pos < length; pos++) {
  addnew = 0;
  cmd = 0;
  reg = 0;
  data = 0;
  cur = songstr[pos];
  if(pos < length - 1) {
   if(strncmp(&(songstr[pos]), "c#", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((2) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "c$", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((14) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "d#", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((4) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "d$", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((2) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "e#", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((6) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "e$", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((4) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "f#", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((7) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "f$", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((5) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "g#", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((9) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "g$", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((7) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "a#", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((11) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "a$", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((9) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "b#", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((13) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "b$", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((11) << 8 | 0xFF);
   }
  }
  if(addnew == 1) {
   pos++;
  } else {
   switch(songstr[pos]) {
   case 'c':
    addnew = 1;
    cmd = NOTE;
    data = ((1) << 8 | 0xFF);
    break;
   case 'd':
    addnew = 1;
    cmd = NOTE;
    data = ((3) << 8 | 0xFF);
    break;
   case 'e':
    addnew = 1;
    cmd = NOTE;
    data = ((5) << 8 | 0xFF);
    break;
   case 'f':
    addnew = 1;
    cmd = NOTE;
    data = ((6) << 8 | 0xFF);
    break;
   case 'g':
    addnew = 1;
    cmd = NOTE;
    data = ((8) << 8 | 0xFF);
    break;
   case 'a':
    addnew = 1;
    cmd = NOTE;
    data = ((10) << 8 | 0xFF);
    break;
   case 'b':
    addnew = 1;
    cmd = NOTE;
    data = ((12) << 8 | 0xFF);
    break;
   case 'x':
    addnew = 1;
    cmd = NOTE;
    data = ((0) << 8 | 0xFF);
    break;
   case '0':
    addnew = 1;
    cmd = MOV;
    reg = (14);
    data = (0 << 8 | 0xFF);
    break;
   case '1':
    addnew = 1;
    cmd = MOV;
    reg = (14);
    data = (1 << 8 | 0xFF);
    break;
   case '2':
    addnew = 1;
    cmd = MOV;
    reg = (14);
    data = (2 << 8 | 0xFF);
    break;
   case '3':
    addnew = 1;
    cmd = MOV;
    reg = (14);
    data = (3 << 8 | 0xFF);
    break;
   case '4':
    addnew = 1;
    cmd = MOV;
    reg = (14);
    data = (4 << 8 | 0xFF);
    break;
   case '5':
    addnew = 1;
    cmd = MOV;
    reg = (14);
    data = (5 << 8 | 0xFF);
    break;
   case '6':
    addnew = 1;
    cmd = MOV;
    reg = (14);
    data = (6 << 8 | 0xFF);
    break;
   case '-':
    addnew = 1;
    cmd = SUB;
    reg = (14);
    data = (1 << 8 | 0xFF);
    break;
   case '+':
    addnew = 1;
    cmd = ADD;
    reg = (14);
    data = (1 << 8 | 0xFF);
    break;
   case 'z':
    addnew = 1;
    cmd = MOV;
    reg = (3);
    data = (64 << 8 | 0xFF);
    break;
   case 't':
    addnew = 1;
    cmd = MOV;
    reg = (3);
    data = (32 << 8 | 0xFF);
    break;
   case 's':
    addnew = 1;
    cmd = MOV;
    reg = (3);
    data = (16 << 8 | 0xFF);
    break;
   case 'i':
    addnew = 1;
    cmd = MOV;
    reg = (3);
    data = (8 << 8 | 0xFF);
    break;
   case 'q':
    addnew = 1;
    cmd = MOV;
    reg = (3);
    data = (4 << 8 | 0xFF);
    break;
   case 'h':
    addnew = 1;
    cmd = MOV;
    reg = (3);
    data = (2 << 8 | 0xFF);
    break;
   case 'w':
    addnew = 1;
    cmd = MOV;
    reg = (3);
    data = (1 << 8 | 0xFF);
    break;
   case '.':
    addnew = 1;
    cmd = MOV;
    reg = (11);
    data = ((1) << 8 | 0xFF);
    break;
   case '!':
    addnew = 1;
    cmd = MOV;
    reg = (11);
    data = ((2) << 8 | 0xFF);
    break;
   case '&':
    while(pos < length) {
     pos++;
     if(songstr[pos] == '&') {
      break;
     }
    }
    break;
   case '*':
    if(pos + 1 < length) {
     pos++;
     switch(songstr[pos]) {
      case 'b':
       addnew = 1;
       cmd = MOV;
       reg = (1);
       if(read1arg(&pos, &data, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'd':
       addnew = 1;
       cmd = MOV;
       reg = (3);
       if(read1arg(&pos, &data, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'n':
       addnew = 1;
       cmd = NOTE;
       if(read1arg(&pos, &data, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'm':
       addnew = 1;
       cmd = MOV;
       if(read2arg(&pos, &data, &reg, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'a':
       addnew = 1;
       cmd = ADD;
       if(read2arg(&pos, &data, &reg, length, songstr) == 1)
        return(((void *)0));
       break;
      case 's':
       addnew = 1;
       cmd = SUB;
       if(read2arg(&pos, &data, &reg, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'c':
       addnew = 1;
       cmd = CMP;
       if(read2arg(&pos, &data, &reg, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'i':
       addnew = 1;
       cmd = JNE;
       if(pos + 1 >= length)
        return(((void *)0));
       if(islabel(songstr, &pos))
        cmd = JNEL;
       if(read1arg(&pos, &data, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'e':
       addnew = 1;
       cmd = JE;
       if(pos + 1 >= length)
        return(((void *)0));
       if(islabel(songstr, &pos))
        cmd = JEL;
       if(read1arg(&pos, &data, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'g':
       addnew = 1;
       cmd = JG;
       if(pos + 1 >= length)
        return(((void *)0));
       if(islabel(songstr, &pos))
        cmd = JGL;
       if(read1arg(&pos, &data, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'l':
       addnew = 1;
       cmd = JL;
       if(pos + 1 >= length)
        return(((void *)0));
       if(islabel(songstr, &pos))
        cmd = JLL;
       if(read1arg(&pos, &data, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'j':
       addnew = 1;
       cmd = JMP;
       if(pos + 1 >= length)
        return(((void *)0));
       if(islabel(songstr, &pos))
        cmd = JMPL;
       if(read1arg(&pos, &data, length, songstr) == 1)
        return(((void *)0));
       break;
      case '>':
       addnew = 1;
       cmd = BRA;
       if(pos + 1 >= length)
        return(((void *)0));
       if(islabel(songstr, &pos))
        cmd = BRAL;
       if(read1arg(&pos, &data, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'r':
       addnew = 1;
       cmd = RET;
       break;
      case 'f':
       addnew = 1;
       cmd = CFL;
       break;
      case 'h':
       addnew = 1;
       cmd = HALT;
       break;
      case ':':
       addnew = 1;
       cmd = LABEL;
       data = (labels << 8 | 0xFF);
       labels++;
       break;
      default:
       break;
     }
    }
    break;
   default:
    break;
   }
  }
  if(addnew == 1) {
   c = initcommand();
   if(c == ((void *)0)) {
    freesong(s);
    return(((void *)0));
   }
   c->cmd = cmd;
   c->data = data;
   c->reg = reg;
   addcmd(c, s);
  }
 }
 if(s->first == ((void *)0)) {
  freesong(s);
  return(((void *)0));
 }
 rewindsong(s);
 s->offsets = malloc(sizeof(command *) * s->count);
 if(s->offsets == ((void *)0))
  return(((void *)0));
 s->labels = malloc(sizeof(int) * labels);
 if(s->labels == ((void *)0))
  return(((void *)0));
 s->nlabels = labels - 1;
 labels = 0;
 for(pos = 0; pos < s->count; pos++) {
  s->offsets[pos] = s->current;
  if(s->offsets[pos]->cmd == LABEL) {
   s->labels[labels] = pos;
   labels++;
  }
  nextcommand(s);
 }
 return(s);
}

canonical_original_code:
struct song *compilesong(char *songstr, int length) {
 char cur;
 int pos;
 int labels;
 struct song *s;
 struct command *c;
 int addnew;
 int cmd;
 int data;
 int reg;
 s = initsong();
 if(s == ((void *)0))
  return(((void *)0));
 labels = 0;
 chartolower(songstr, length);
 for(pos = 0; pos < length; pos++) {
  addnew = 0;
  cmd = 0;
  reg = 0;
  data = 0;
  cur = songstr[pos];
  if(pos < length - 1) {
   if(strncmp(&(songstr[pos]), "c#", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((2) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "c$", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((14) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "d#", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((4) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "d$", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((2) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "e#", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((6) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "e$", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((4) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "f#", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((7) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "f$", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((5) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "g#", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((9) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "g$", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((7) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "a#", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((11) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "a$", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((9) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "b#", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((13) << 8 | 0xFF);
   } else if(strncmp(&(songstr[pos]), "b$", 2) == 0) {
    addnew = 1;
    cmd = NOTE;
    data = ((11) << 8 | 0xFF);
   }
  }
  if(addnew == 1) {
   pos++;
  } else {
   switch(songstr[pos]) {
   case 'c':
    addnew = 1;
    cmd = NOTE;
    data = ((1) << 8 | 0xFF);
    break;
   case 'd':
    addnew = 1;
    cmd = NOTE;
    data = ((3) << 8 | 0xFF);
    break;
   case 'e':
    addnew = 1;
    cmd = NOTE;
    data = ((5) << 8 | 0xFF);
    break;
   case 'f':
    addnew = 1;
    cmd = NOTE;
    data = ((6) << 8 | 0xFF);
    break;
   case 'g':
    addnew = 1;
    cmd = NOTE;
    data = ((8) << 8 | 0xFF);
    break;
   case 'a':
    addnew = 1;
    cmd = NOTE;
    data = ((10) << 8 | 0xFF);
    break;
   case 'b':
    addnew = 1;
    cmd = NOTE;
    data = ((12) << 8 | 0xFF);
    break;
   case 'x':
    addnew = 1;
    cmd = NOTE;
    data = ((0) << 8 | 0xFF);
    break;
   case '0':
    addnew = 1;
    cmd = MOV;
    reg = (14);
    data = (0 << 8 | 0xFF);
    break;
   case '1':
    addnew = 1;
    cmd = MOV;
    reg = (14);
    data = (1 << 8 | 0xFF);
    break;
   case '2':
    addnew = 1;
    cmd = MOV;
    reg = (14);
    data = (2 << 8 | 0xFF);
    break;
   case '3':
    addnew = 1;
    cmd = MOV;
    reg = (14);
    data = (3 << 8 | 0xFF);
    break;
   case '4':
    addnew = 1;
    cmd = MOV;
    reg = (14);
    data = (4 << 8 | 0xFF);
    break;
   case '5':
    addnew = 1;
    cmd = MOV;
    reg = (14);
    data = (5 << 8 | 0xFF);
    break;
   case '6':
    addnew = 1;
    cmd = MOV;
    reg = (14);
    data = (6 << 8 | 0xFF);
    break;
   case '-':
    addnew = 1;
    cmd = SUB;
    reg = (14);
    data = (1 << 8 | 0xFF);
    break;
   case '+':
    addnew = 1;
    cmd = ADD;
    reg = (14);
    data = (1 << 8 | 0xFF);
    break;
   case 'z':
    addnew = 1;
    cmd = MOV;
    reg = (3);
    data = (64 << 8 | 0xFF);
    break;
   case 't':
    addnew = 1;
    cmd = MOV;
    reg = (3);
    data = (32 << 8 | 0xFF);
    break;
   case 's':
    addnew = 1;
    cmd = MOV;
    reg = (3);
    data = (16 << 8 | 0xFF);
    break;
   case 'i':
    addnew = 1;
    cmd = MOV;
    reg = (3);
    data = (8 << 8 | 0xFF);
    break;
   case 'q':
    addnew = 1;
    cmd = MOV;
    reg = (3);
    data = (4 << 8 | 0xFF);
    break;
   case 'h':
    addnew = 1;
    cmd = MOV;
    reg = (3);
    data = (2 << 8 | 0xFF);
    break;
   case 'w':
    addnew = 1;
    cmd = MOV;
    reg = (3);
    data = (1 << 8 | 0xFF);
    break;
   case '.':
    addnew = 1;
    cmd = MOV;
    reg = (11);
    data = ((1) << 8 | 0xFF);
    break;
   case '!':
    addnew = 1;
    cmd = MOV;
    reg = (11);
    data = ((2) << 8 | 0xFF);
    break;
   case '&':
    while(pos < length) {
     pos++;
     if(songstr[pos] == '&') {
      break;
     }
    }
    break;
   case '*':
    if(pos + 1 < length) {
     pos++;
     switch(songstr[pos]) {
      case 'b':
       addnew = 1;
       cmd = MOV;
       reg = (1);
       if(read1arg(&pos, &data, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'd':
       addnew = 1;
       cmd = MOV;
       reg = (3);
       if(read1arg(&pos, &data, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'n':
       addnew = 1;
       cmd = NOTE;
       if(read1arg(&pos, &data, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'm':
       addnew = 1;
       cmd = MOV;
       if(read2arg(&pos, &data, &reg, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'a':
       addnew = 1;
       cmd = ADD;
       if(read2arg(&pos, &data, &reg, length, songstr) == 1)
        return(((void *)0));
       break;
      case 's':
       addnew = 1;
       cmd = SUB;
       if(read2arg(&pos, &data, &reg, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'c':
       addnew = 1;
       cmd = CMP;
       if(read2arg(&pos, &data, &reg, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'i':
       addnew = 1;
       cmd = JNE;
       if(pos + 1 >= length)
        return(((void *)0));
       if(islabel(songstr, &pos))
        cmd = JNEL;
       if(read1arg(&pos, &data, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'e':
       addnew = 1;
       cmd = JE;
       if(pos + 1 >= length)
        return(((void *)0));
       if(islabel(songstr, &pos))
        cmd = JEL;
       if(read1arg(&pos, &data, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'g':
       addnew = 1;
       cmd = JG;
       if(pos + 1 >= length)
        return(((void *)0));
       if(islabel(songstr, &pos))
        cmd = JGL;
       if(read1arg(&pos, &data, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'l':
       addnew = 1;
       cmd = JL;
       if(pos + 1 >= length)
        return(((void *)0));
       if(islabel(songstr, &pos))
        cmd = JLL;
       if(read1arg(&pos, &data, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'j':
       addnew = 1;
       cmd = JMP;
       if(pos + 1 >= length)
        return(((void *)0));
       if(islabel(songstr, &pos))
        cmd = JMPL;
       if(read1arg(&pos, &data, length, songstr) == 1)
        return(((void *)0));
       break;
      case '>':
       addnew = 1;
       cmd = BRA;
       if(pos + 1 >= length)
        return(((void *)0));
       if(islabel(songstr, &pos))
        cmd = BRAL;
       if(read1arg(&pos, &data, length, songstr) == 1)
        return(((void *)0));
       break;
      case 'r':
       addnew = 1;
       cmd = RET;
       break;
      case 'f':
       addnew = 1;
       cmd = CFL;
       break;
      case 'h':
       addnew = 1;
       cmd = HALT;
       break;
      case ':':
       addnew = 1;
       cmd = LABEL;
       data = (labels << 8 | 0xFF);
       labels++;
       break;
      default:
       break;
     }
    }
    break;
   default:
    break;
   }
  }
  if(addnew == 1) {
   c = initcommand();
   if(c == ((void *)0)) {
    freesong(s);
    return(((void *)0));
   }
   c->cmd = cmd;
   c->data = data;
   c->reg = reg;
   addcmd(c, s);
  }
 }
 if(s->first == ((void *)0)) {
  freesong(s);
  return(((void *)0));
 }
 rewindsong(s);
 s->offsets = malloc(sizeof(struct command *) * s->count);
 if(s->offsets == ((void *)0))
  return(((void *)0));
 s->labels = malloc(sizeof(int) * labels);
 if(s->labels == ((void *)0))
  return(((void *)0));
 s->nlabels = labels - 1;
 labels = 0;
 for(pos = 0; pos < s->count; pos++) {
  s->offsets[pos] = s->current;
  if(s->offsets[pos]->cmd == LABEL) {
   s->labels[labels] = pos;
   labels++;
  }
  nextcommand(s);
 }
 return(s);
}

variable_types: cur char

variable_types: pos int

variable_types: labels int

variable_types: struct song { unsigned int count; struct command { enum instruction { NOTE=0, MOV=1, ADD=2, SUB=3, CMP=4, JNE=5, JE=6, JL=7, JG=8, JMP=9, BRA=10, RET=11, CFL=12, HALT=13, LABEL=14, JNEL=15, JEL=16, JLL=17, JGL=18, JMPL=19, BRAL=20, NOP=21 } cmd; unsigned int data; unsigned int reg; struct command *next; struct command *prev; } *first; struct command *last; struct command *current; struct command **offsets; int nlabels; int *labels; } *

variable_types: addnew int

variable_types: cmd int

variable_types: data int

variable_types: reg int


repo: paulguy/piccrap

decompiled_code:
__int64 __fastcall analog_decode(int *a1, __int64 a2, __int64 a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, __int64 a12, __int64 a13, int a14)
{
int v14; // er10
__int64 v15; // r15
double v16; // xmm8_8
int v17; // er8
double v18; // xmm5_8
double v19; // xmm4_8
int v20; // eax
int v21; // ebx
int v24; // er11
int v25; // er13
double v26; // xmm3_8
__int64 v28; // rcx
int v29; // esi
int v30; // eax
double *v31; // rdi
int i; // edx
int v33; // eax
int v34; // edx
int v35; // er9
int v36; // eax
__int64 v37; // rsi
int v38; // eax
double *v39; // rcx
int v40; // ebx
double j; // xmm0_8
double v42; // xmm4_8
double v43; // xmm0_8
int v44; // ebx
int v45; // er8
double *v46; // rax
double v47; // xmm0_8
double v48; // xmm3_8
double v49; // xmm1_8
int v50; // esi
int v51; // er14
int v52; // edx
int v53; // ecx
int v54; // edx
double *v55; // rdi
double v56; // xmm1_8
int v57; // eax
int v58; // edx
int v59; // eax
int v60; // edi
int v61; // eax
double *v62; // rsi
int v63; // ecx
int v64; // er14
__int64 v65; // rcx
double v66; // xmm3_8
double v67; // xmm4_8
int v68; // ecx
int v69; // ecx
double v70; // xmm1_8
int v71; // er8
double *v72; // rdx
int v73; // eax
int v74; // ebx
double v75; // xmm0_8
int v76; // ebx
__int64 v77; // rcx
unsigned int v79; // [rsp+Ch] [rbp-8Ch]
int v80; // [rsp+Ch] [rbp-8Ch]
double v81; // [rsp+10h] [rbp-88h]
double v82; // [rsp+18h] [rbp-80h]
double v83; // [rsp+18h] [rbp-80h]
double v84; // [rsp+20h] [rbp-78h]
double v85; // [rsp+20h] [rbp-78h]
double v86; // [rsp+20h] [rbp-78h]
double v87; // [rsp+28h] [rbp-70h]
int v88; // [rsp+28h] [rbp-70h]
double v89; // [rsp+28h] [rbp-70h]
int v90; // [rsp+28h] [rbp-70h]
double v91; // [rsp+30h] [rbp-68h]
double v92; // [rsp+38h] [rbp-60h]
double v93; // [rsp+50h] [rbp-48h]
int v94; // [rsp+58h] [rbp-40h]
int v95; // [rsp+58h] [rbp-40h]
int v96; // [rsp+58h] [rbp-40h]
int v97; // [rsp+5Ch] [rbp-3Ch]

v17 = *(_DWORD *)(a3 + 32);
v18 = 1.0 - *(double *)(a3 + 16);
if ( a1[2] <= 0 )
{
v25 = 0;
goto LABEL_19;
}
v19 = *(double *)&LC0;
v20 = 0;
v21 = 0;
v24 = 0;
v25 = 0;
v26 = *(double *)&LC1;
while ( 2 )
{
if ( v25 <= 8 )
{
if ( v20 )
return 0xFFFFFFFFLL;
v29 = a1[2];
LABEL_50:
v54 = v21 + 1;
v55 = (double *)(*(_QWORD *)a1 + 8LL * v21);
v56 = *(double *)a3 * v26;
while ( 1 )
{
v57 = v54 - 1;
if ( v56 <= *v55 )
{
v66 = v26 * *(double *)(a3 + 16);
if ( v17 < *(_DWORD *)(a3 + 32) )
v17 = *(_DWORD *)(a3 + 32);
v26 = v66 + *v55 * v18;
if ( v54 >= v29 )
goto LABEL_19;
v56 = *(double *)a3 * v26;
}
else
{
if ( !v17 )
{
v58 = *(_DWORD *)(a3 + 32);
v59 = v57 - v58;
v60 = v59 + 1;
if ( v59 + 1 >= v29 )
goto LABEL_19;
v61 = v59 + 2;
v62 = (double *)(*(_QWORD *)a1 + 8LL * v60);
v63 = *(_DWORD *)(a3 + 32);
while ( 1 )
{
if ( *v62 <= v56 )
{
v67 = v19 * *(double *)(a3 + 16);
if ( v63 < v58 )
v63 = *(_DWORD *)(a3 + 32);
v19 = v67 + *v62 * v18;
if ( v61 >= a1[2] )
goto LABEL_19;
}
else
{
if ( !v63 )
{
v64 = v61 - 1 - v58;
v65 = (unsigned int)(v64 - v24);
if ( v25 )
{
v79 += v65;
v89 = v18;
v85 = v19;
fprintf(
stderr,
"%d %d %d %d %f %f\n",
(unsigned int)v25,
v65,
v79,
(unsigned int)((int)v79 / (v25 + 1)),
v26,
v19);
v29 = a1[2];
v30 = v64 + 1;
v17 = *(_DWORD *)(a3 + 32);
if ( v64 + 1 >= v29 )
{
++v25;
goto LABEL_19;
}
v19 = v85;
v18 = v89;
if ( v25 == 8 )
{
v28 = *(_QWORD *)a1;
v24 = v64;
v25 = 9;
goto LABEL_8;
}
v68 = v79;
v21 = v64 + 1;
++v25;
}
else
{
v80 = v64 - v24;
v86 = v18;
v21 = v61 - v58;
v25 = 1;
v82 = v19;
fprintf(stderr, "%d %d %d %d %f %f\n", 0LL, v65, (unsigned int)v65, (unsigned int)v65, v26, v19);
v29 = a1[2];
v17 = *(_DWORD *)(a3 + 32);
v68 = v80;
v19 = v82;
v18 = v86;
if ( v29 <= v64 + 1 )
goto LABEL_19;
}
v79 = v68;
v24 = v64;
goto LABEL_50;
}
--v63;
if ( a1[2] <= v61 )
goto LABEL_19;
}
++v62;
++v61;
}
}
--v17;
if ( v54 >= v29 )
goto LABEL_19;
}
++v55;
++v54;
}
}
if ( v20 == 2 )
goto LABEL_22;
v28 = *(_QWORD *)a1;
v29 = a1[2];
v30 = v21;
LABEL_8:
v31 = (double *)(v28 + 8LL * v30);
for ( i = v30 + 1; ; ++i )
{
v33 = i - 1;
if ( v26 > *v31 )
break;
v48 = v26 * *(double *)(a3 + 16);
if ( v17 < *(_DWORD *)(a3 + 32) )
v17 = *(_DWORD *)(a3 + 32);
v26 = v48 + *v31 * v18;
if ( v29 <= i )
goto LABEL_19;
LABEL_29:
++v31;
}
if ( v17 )
{
--v17;
if ( v29 <= i )
goto LABEL_19;
goto LABEL_29;
}
v34 = *(_DWORD *)(a3 + 32);
v35 = v34;
v36 = v33 - v34 + 1;
if ( v29 <= v36 )
goto LABEL_19;
v37 = v36;
v38 = v36 + 1;
v39 = (double *)(v28 + 8 * v37);
v40 = v38 - 1;
for ( j = *v39; v19 <= j; v40 = v38 - 1 )
{
v42 = v19 * *(double *)(a3 + 16);
v43 = j * v18;
if ( v35 < v34 )
v35 = *(_DWORD *)(a3 + 32);
v19 = v42 + v43;
if ( v38 >= a1[2] )
goto LABEL_19;
LABEL_15:
j = v39[1];
++v39;
++v38;
}
if ( v35 )
{
--v35;
if ( v38 < a1[2] )
goto LABEL_15;
goto LABEL_19;
}
v44 = v40 - *(_DWORD *)(a3 + 32);
v45 = v44 - v24;
v21 = v44 + 1;
v94 = v24;
v92 = v18;
v81 = v19;
v87 = (double)*(int *)a2 / ((double)((int)v79 / v25) - (double)v45 - (double)v45 * 0.0);
fprintf(stderr, "%d %f ", (unsigned int)v45, v87);
fprintf(stderr, "%f %f %f\n", v81, 0.9333333333333333 * v81, 0.1666666666666667 * v81);
v17 = *(_DWORD *)(a3 + 32);
if ( v21 >= a1[2] )
{
LABEL_96:
LODWORD(v15) = 0;
goto LABEL_19;
}
a14 = 0;
v24 = v94;
v14 = 0;
LODWORD(v15) = 0;
v18 = v92;
a10 = 0.0;
a11 = v87;
a6 = 0.1666666666666667 * v81;
v16 = 0.9333333333333333 * v81;
v19 = v81;
LABEL_22:
v46 = (double *)(*(_QWORD *)a1 + 8LL * v21);
while ( 2 )
{
v47 = *v46;
if ( *v46 <= *(double *)a3 * v26 )
{
v17 = *(_DWORD *)(a3 + 32);
goto LABEL_31;
}
if ( v17 )
{
--v17;
LABEL_31:
a10 = a10 + a11;
v49 = (double)a14;
v50 = v14 + 1;
v51 = v21 + 1;
v52 = (int)a10;
if ( a6 <= v47 )
{
if ( v47 <= v16 )
{
v20 = 2;
goto LABEL_77;
}
a14 = (int)(v49 + v16);
if ( v52 > (int)v15 )
{
LABEL_33:
v53 = v15;
v20 = 2;
v15 = (unsigned int)(v15 + 1);
goto LABEL_34;
}
}
else
{
a14 = (int)(v49 + a6);
if ( v52 > (int)v15 )
goto LABEL_33;
}
if ( v51 >= a1[2] )
goto LABEL_19;
++v46;
++v14;
++v21;
continue;
}
break;
}
v95 = v14;
v21 -= *(_DWORD *)(a3 + 32);
v97 = *(_DWORD *)(a3 + 32);
v88 = a14;
v51 = v21 + 1;
v79 += v21 - v24;
v93 = v18;
v84 = v19;
fprintf(stderr, "%d %d %d %f %f\n", (unsigned int)(v25 + 1), 0LL, v79, v26, v19);
fprintf(stderr, "Line %d ended at %d due to hsync reached.\n", (unsigned int)(v25 + 1), (unsigned int)v21);
v28 = *(_QWORD *)a1;
v47 = *(double *)(*(_QWORD *)a1 + 8LL * v21);
v30 = v21 + 1;
v19 = v84;
v50 = v95 + 1;
v18 = v93;
if ( a6 <= v47 )
{
if ( v47 <= v16 )
{
a10 = a10 + a11;
v17 = v97;
LODWORD(v15) = 0;
v49 = (double)v88;
v24 = v21;
++v25;
v20 = 0;
LABEL_77:
v14 = v50;
a14 = (int)(v47 + v49);
if ( (int)a10 <= (int)v15 )
goto LABEL_36;
v53 = v15;
v15 = (unsigned int)(v15 + 1);
}
else
{
a10 = a10 + a11;
if ( (int)a10 <= 0 )
goto LABEL_93;
v17 = v97;
v53 = 0;
v24 = v21;
++v25;
v15 = 1LL;
v20 = 0;
a14 = (int)((double)v88 + v16);
}
LABEL_34:
*(_BYTE *)(*(_QWORD *)(a2 + 8) + v53 + *(_DWORD *)a2 * (v25 - 9)) = (int)(255.0
- ((double)a14 / (double)v50 - a6)
* (255.0
/ (v16 - a6)));
if ( *(_DWORD *)a2 <= (int)v15 )
goto LABEL_80;
v14 = 0;
a14 = 0;
LABEL_36:
v21 = v51;
if ( a1[2] > v51 )
continue;
goto LABEL_19;
}
break;
}
a10 = a10 + a11;
if ( (int)a10 <= 0 )
{
LABEL_93:
v29 = a1[2];
LODWORD(v15) = 0;
if ( v29 > v51 )
{
v17 = v97;
v24 = v21;
++v25;
goto LABEL_8;
}
++v25;
goto LABEL_19;
}
*(_BYTE *)(*(_QWORD *)(a2 + 8) + *(_DWORD *)a2 * (v25 - 8)) = (int)(255.0
- ((double)(int)((double)v88 + a6) / (double)v50 - a6)
* (255.0
/ (v16 - a6)));
if ( *(int *)a2 <= 1 )
{
v17 = v97;
v24 = v21;
++v25;
v15 = 1LL;
LABEL_80:
v96 = v17;
v90 = v24;
v91 = v19;
v83 = v18;
fprintf(stderr, "Ran out of framebuffer on line %d.\n", (unsigned int)v25);
v69 = a1[2];
if ( v69 <= v51 )
goto LABEL_19;
v70 = *(double *)a3 * v26;
v71 = v96;
v72 = (double *)(*(_QWORD *)a1 + 8LL * v51);
v73 = v21 + 2;
v74 = v21 + 1;
if ( *v72 <= v70 )
{
do
{
v71 = *(_DWORD *)(a3 + 32);
if ( v69 <= v73 )
goto LABEL_19;
LABEL_84:
v75 = v72[1];
++v72;
v74 = v73++;
}
while ( v75 <= v70 );
}
if ( v71 )
{
--v71;
if ( v73 < v69 )
goto LABEL_84;
goto LABEL_19;
}
v76 = v74 - *(_DWORD *)(a3 + 32);
++v25;
v77 = (unsigned int)(v76 - v90);
v79 += v77;
fprintf(
stderr,
"%d %d %d %d %f %d %f\n",
(unsigned int)v25,
v77,
v79,
(unsigned int)((int)v79 / v25),
a10,
v15,
v26);
v29 = a1[2];
v30 = v76 + 1;
v17 = *(_DWORD *)(a3 + 32);
if ( v76 + 1 >= v29 )
goto LABEL_96;
v28 = *(_QWORD *)a1;
LODWORD(v15) = 0;
v24 = v76;
v19 = v91;
v18 = v83;
goto LABEL_8;
}
v29 = a1[2];
if ( v29 > v51 )
{
v28 = *(_QWORD *)a1;
v17 = v97;
v24 = v21;
++v25;
LODWORD(v15) = 1;
goto LABEL_8;
}
++v25;
LODWORD(v15) = 1;
LABEL_19:
fprintf(stderr, "Read ended at %d, %d.\n", (unsigned int)v15, (unsigned int)v25);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int *a1, __int64 a2, __int64 a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11, __int64 a12, __int64 a13, int a14)
{
int v14; // er10
__int64 v15; // r15
double v16; // xmm8_8
int v17; // er8
double v18; // xmm5_8
double v19; // xmm4_8
int v20; // eax
int v21; // ebx
int v24; // er11
int v25; // er13
double v26; // xmm3_8
__int64 v28; // rcx
int v29; // esi
int v30; // eax
double *v31; // rdi
int i; // edx
int v33; // eax
int v34; // edx
int v35; // er9
int v36; // eax
__int64 v37; // rsi
int v38; // eax
double *v39; // rcx
int v40; // ebx
double j; // xmm0_8
double v42; // xmm4_8
double v43; // xmm0_8
int v44; // ebx
int v45; // er8
double *v46; // rax
double v47; // xmm0_8
double v48; // xmm3_8
double v49; // xmm1_8
int v50; // esi
int v51; // er14
int v52; // edx
int v53; // ecx
int v54; // edx
double *v55; // rdi
double v56; // xmm1_8
int v57; // eax
int v58; // edx
int v59; // eax
int v60; // edi
int v61; // eax
double *v62; // rsi
int v63; // ecx
int v64; // er14
__int64 v65; // rcx
double v66; // xmm3_8
double v67; // xmm4_8
int v68; // ecx
int v69; // ecx
double v70; // xmm1_8
int v71; // er8
double *v72; // rdx
int v73; // eax
int v74; // ebx
double v75; // xmm0_8
int v76; // ebx
__int64 v77; // rcx
unsigned int v79; // [rsp+Ch] [rbp-8Ch]
int v80; // [rsp+Ch] [rbp-8Ch]
double v81; // [rsp+10h] [rbp-88h]
double v82; // [rsp+18h] [rbp-80h]
double v83; // [rsp+18h] [rbp-80h]
double v84; // [rsp+20h] [rbp-78h]
double v85; // [rsp+20h] [rbp-78h]
double v86; // [rsp+20h] [rbp-78h]
double v87; // [rsp+28h] [rbp-70h]
int v88; // [rsp+28h] [rbp-70h]
double v89; // [rsp+28h] [rbp-70h]
int v90; // [rsp+28h] [rbp-70h]
double v91; // [rsp+30h] [rbp-68h]
double v92; // [rsp+38h] [rbp-60h]
double v93; // [rsp+50h] [rbp-48h]
int v94; // [rsp+58h] [rbp-40h]
int v95; // [rsp+58h] [rbp-40h]
int v96; // [rsp+58h] [rbp-40h]
int v97; // [rsp+5Ch] [rbp-3Ch]

v17 = *(_DWORD *)(a3 + 32);
v18 = 1.0 - *(double *)(a3 + 16);
if ( a1[2] <= 0 )
{
v25 = 0;
goto LABEL_19;
}
v19 = *(double *)&LC0;
v20 = 0;
v21 = 0;
v24 = 0;
v25 = 0;
v26 = *(double *)&LC1;
while ( 2 )
{
if ( v25 <= 8 )
{
if ( v20 )
return 0xFFFFFFFFLL;
v29 = a1[2];
LABEL_50:
v54 = v21 + 1;
v55 = (double *)(*(_QWORD *)a1 + 8LL * v21);
v56 = *(double *)a3 * v26;
while ( 1 )
{
v57 = v54 - 1;
if ( v56 <= *v55 )
{
v66 = v26 * *(double *)(a3 + 16);
if ( v17 < *(_DWORD *)(a3 + 32) )
v17 = *(_DWORD *)(a3 + 32);
v26 = v66 + *v55 * v18;
if ( v54 >= v29 )
goto LABEL_19;
v56 = *(double *)a3 * v26;
}
else
{
if ( !v17 )
{
v58 = *(_DWORD *)(a3 + 32);
v59 = v57 - v58;
v60 = v59 + 1;
if ( v59 + 1 >= v29 )
goto LABEL_19;
v61 = v59 + 2;
v62 = (double *)(*(_QWORD *)a1 + 8LL * v60);
v63 = *(_DWORD *)(a3 + 32);
while ( 1 )
{
if ( *v62 <= v56 )
{
v67 = v19 * *(double *)(a3 + 16);
if ( v63 < v58 )
v63 = *(_DWORD *)(a3 + 32);
v19 = v67 + *v62 * v18;
if ( v61 >= a1[2] )
goto LABEL_19;
}
else
{
if ( !v63 )
{
v64 = v61 - 1 - v58;
v65 = (unsigned int)(v64 - v24);
if ( v25 )
{
v79 += v65;
v89 = v18;
v85 = v19;
fprintf(
stderr,
"%d %d %d %d %f %f\n",
(unsigned int)v25,
v65,
v79,
(unsigned int)((int)v79 / (v25 + 1)),
v26,
v19);
v29 = a1[2];
v30 = v64 + 1;
v17 = *(_DWORD *)(a3 + 32);
if ( v64 + 1 >= v29 )
{
++v25;
goto LABEL_19;
}
v19 = v85;
v18 = v89;
if ( v25 == 8 )
{
v28 = *(_QWORD *)a1;
v24 = v64;
v25 = 9;
goto LABEL_8;
}
v68 = v79;
v21 = v64 + 1;
++v25;
}
else
{
v80 = v64 - v24;
v86 = v18;
v21 = v61 - v58;
v25 = 1;
v82 = v19;
fprintf(stderr, "%d %d %d %d %f %f\n", 0LL, v65, (unsigned int)v65, (unsigned int)v65, v26, v19);
v29 = a1[2];
v17 = *(_DWORD *)(a3 + 32);
v68 = v80;
v19 = v82;
v18 = v86;
if ( v29 <= v64 + 1 )
goto LABEL_19;
}
v79 = v68;
v24 = v64;
goto LABEL_50;
}
--v63;
if ( a1[2] <= v61 )
goto LABEL_19;
}
++v62;
++v61;
}
}
--v17;
if ( v54 >= v29 )
goto LABEL_19;
}
++v55;
++v54;
}
}
if ( v20 == 2 )
goto LABEL_22;
v28 = *(_QWORD *)a1;
v29 = a1[2];
v30 = v21;
LABEL_8:
v31 = (double *)(v28 + 8LL * v30);
for ( i = v30 + 1; ; ++i )
{
v33 = i - 1;
if ( v26 > *v31 )
break;
v48 = v26 * *(double *)(a3 + 16);
if ( v17 < *(_DWORD *)(a3 + 32) )
v17 = *(_DWORD *)(a3 + 32);
v26 = v48 + *v31 * v18;
if ( v29 <= i )
goto LABEL_19;
LABEL_29:
++v31;
}
if ( v17 )
{
--v17;
if ( v29 <= i )
goto LABEL_19;
goto LABEL_29;
}
v34 = *(_DWORD *)(a3 + 32);
v35 = v34;
v36 = v33 - v34 + 1;
if ( v29 <= v36 )
goto LABEL_19;
v37 = v36;
v38 = v36 + 1;
v39 = (double *)(v28 + 8 * v37);
v40 = v38 - 1;
for ( j = *v39; v19 <= j; v40 = v38 - 1 )
{
v42 = v19 * *(double *)(a3 + 16);
v43 = j * v18;
if ( v35 < v34 )
v35 = *(_DWORD *)(a3 + 32);
v19 = v42 + v43;
if ( v38 >= a1[2] )
goto LABEL_19;
LABEL_15:
j = v39[1];
++v39;
++v38;
}
if ( v35 )
{
--v35;
if ( v38 < a1[2] )
goto LABEL_15;
goto LABEL_19;
}
v44 = v40 - *(_DWORD *)(a3 + 32);
v45 = v44 - v24;
v21 = v44 + 1;
v94 = v24;
v92 = v18;
v81 = v19;
v87 = (double)*(int *)a2 / ((double)((int)v79 / v25) - (double)v45 - (double)v45 * 0.0);
fprintf(stderr, "%d %f ", (unsigned int)v45, v87);
fprintf(stderr, "%f %f %f\n", v81, 0.9333333333333333 * v81, 0.1666666666666667 * v81);
v17 = *(_DWORD *)(a3 + 32);
if ( v21 >= a1[2] )
{
LABEL_96:
LODWORD(v15) = 0;
goto LABEL_19;
}
a14 = 0;
v24 = v94;
v14 = 0;
LODWORD(v15) = 0;
v18 = v92;
a10 = 0.0;
a11 = v87;
a6 = 0.1666666666666667 * v81;
v16 = 0.9333333333333333 * v81;
v19 = v81;
LABEL_22:
v46 = (double *)(*(_QWORD *)a1 + 8LL * v21);
while ( 2 )
{
v47 = *v46;
if ( *v46 <= *(double *)a3 * v26 )
{
v17 = *(_DWORD *)(a3 + 32);
goto LABEL_31;
}
if ( v17 )
{
--v17;
LABEL_31:
a10 = a10 + a11;
v49 = (double)a14;
v50 = v14 + 1;
v51 = v21 + 1;
v52 = (int)a10;
if ( a6 <= v47 )
{
if ( v47 <= v16 )
{
v20 = 2;
goto LABEL_77;
}
a14 = (int)(v49 + v16);
if ( v52 > (int)v15 )
{
LABEL_33:
v53 = v15;
v20 = 2;
v15 = (unsigned int)(v15 + 1);
goto LABEL_34;
}
}
else
{
a14 = (int)(v49 + a6);
if ( v52 > (int)v15 )
goto LABEL_33;
}
if ( v51 >= a1[2] )
goto LABEL_19;
++v46;
++v14;
++v21;
continue;
}
break;
}
v95 = v14;
v21 -= *(_DWORD *)(a3 + 32);
v97 = *(_DWORD *)(a3 + 32);
v88 = a14;
v51 = v21 + 1;
v79 += v21 - v24;
v93 = v18;
v84 = v19;
fprintf(stderr, "%d %d %d %f %f\n", (unsigned int)(v25 + 1), 0LL, v79, v26, v19);
fprintf(stderr, "Line %d ended at %d due to hsync reached.\n", (unsigned int)(v25 + 1), (unsigned int)v21);
v28 = *(_QWORD *)a1;
v47 = *(double *)(*(_QWORD *)a1 + 8LL * v21);
v30 = v21 + 1;
v19 = v84;
v50 = v95 + 1;
v18 = v93;
if ( a6 <= v47 )
{
if ( v47 <= v16 )
{
a10 = a10 + a11;
v17 = v97;
LODWORD(v15) = 0;
v49 = (double)v88;
v24 = v21;
++v25;
v20 = 0;
LABEL_77:
v14 = v50;
a14 = (int)(v47 + v49);
if ( (int)a10 <= (int)v15 )
goto LABEL_36;
v53 = v15;
v15 = (unsigned int)(v15 + 1);
}
else
{
a10 = a10 + a11;
if ( (int)a10 <= 0 )
goto LABEL_93;
v17 = v97;
v53 = 0;
v24 = v21;
++v25;
v15 = 1LL;
v20 = 0;
a14 = (int)((double)v88 + v16);
}
LABEL_34:
*(_BYTE *)(*(_QWORD *)(a2 + 8) + v53 + *(_DWORD *)a2 * (v25 - 9)) = (int)(255.0
- ((double)a14 / (double)v50 - a6)
* (255.0
/ (v16 - a6)));
if ( *(_DWORD *)a2 <= (int)v15 )
goto LABEL_80;
v14 = 0;
a14 = 0;
LABEL_36:
v21 = v51;
if ( a1[2] > v51 )
continue;
goto LABEL_19;
}
break;
}
a10 = a10 + a11;
if ( (int)a10 <= 0 )
{
LABEL_93:
v29 = a1[2];
LODWORD(v15) = 0;
if ( v29 > v51 )
{
v17 = v97;
v24 = v21;
++v25;
goto LABEL_8;
}
++v25;
goto LABEL_19;
}
*(_BYTE *)(*(_QWORD *)(a2 + 8) + *(_DWORD *)a2 * (v25 - 8)) = (int)(255.0
- ((double)(int)((double)v88 + a6) / (double)v50 - a6)
* (255.0
/ (v16 - a6)));
if ( *(int *)a2 <= 1 )
{
v17 = v97;
v24 = v21;
++v25;
v15 = 1LL;
LABEL_80:
v96 = v17;
v90 = v24;
v91 = v19;
v83 = v18;
fprintf(stderr, "Ran out of framebuffer on line %d.\n", (unsigned int)v25);
v69 = a1[2];
if ( v69 <= v51 )
goto LABEL_19;
v70 = *(double *)a3 * v26;
v71 = v96;
v72 = (double *)(*(_QWORD *)a1 + 8LL * v51);
v73 = v21 + 2;
v74 = v21 + 1;
if ( *v72 <= v70 )
{
do
{
v71 = *(_DWORD *)(a3 + 32);
if ( v69 <= v73 )
goto LABEL_19;
LABEL_84:
v75 = v72[1];
++v72;
v74 = v73++;
}
while ( v75 <= v70 );
}
if ( v71 )
{
--v71;
if ( v73 < v69 )
goto LABEL_84;
goto LABEL_19;
}
v76 = v74 - *(_DWORD *)(a3 + 32);
++v25;
v77 = (unsigned int)(v76 - v90);
v79 += v77;
fprintf(
stderr,
"%d %d %d %d %f %d %f\n",
(unsigned int)v25,
v77,
v79,
(unsigned int)((int)v79 / v25),
a10,
v15,
v26);
v29 = a1[2];
v30 = v76 + 1;
v17 = *(_DWORD *)(a3 + 32);
if ( v76 + 1 >= v29 )
goto LABEL_96;
v28 = *(_QWORD *)a1;
LODWORD(v15) = 0;
v24 = v76;
v19 = v91;
v18 = v83;
goto LABEL_8;
}
v29 = a1[2];
if ( v29 > v51 )
{
v28 = *(_QWORD *)a1;
v17 = v97;
v24 = v21;
++v25;
LODWORD(v15) = 1;
goto LABEL_8;
}
++v25;
LODWORD(v15) = 1;
LABEL_19:
fprintf(stderr, "Read ended at %d, %d.\n", (unsigned int)v15, (unsigned int)v25);
return 0LL;
}

original_code:
int analog_decode(Connection *connection, Surface *surface, DecodeParams *params) {
 double invautolevel = 1.0 - params->autolevel;
 int i;
 int currentline, linestart;
 int xpos, avgsample, samples;
 double xposd;
 int runningstride;
 int activestart;
 int fuzz;
 double sampleperiod;
 SyncMode syncing;
 double synclevel, blanklevel;
 double whitelevel, blacklevel;
 currentline = 0;
 syncing = SYNC_SYNC;
 synclevel = (100.0);
 blanklevel = (75.0);
 fuzz = params->fuzzyness;
 activestart = 0;
 linestart = 0;
 for(i = 0; i < connection->length; i++) {
    if(currentline < (9)) {
   switch (syncing) {
    case SYNC_SYNC:
     if(connection->stream[i] < (synclevel * params->syncthresh)) {
      if(fuzz == 0) {
       i -= params->fuzzyness;
       fuzz = params->fuzzyness;
       syncing = SYNC_BLANK;
      } else {
       fuzz--;
      }
     } else {
      synclevel = synclevel*params->autolevel + connection->stream[i]*invautolevel;
      if(fuzz < params->fuzzyness) {
       fuzz = params->fuzzyness;
      }
     }
     break;
    case SYNC_BLANK:
     if(connection->stream[i] > (synclevel * params->syncthresh)) {
      if(fuzz == 0) {
       i -= params->fuzzyness;
       if(currentline == 0) {
        runningstride = i - linestart;
       } else {
        runningstride += i - linestart;
       }
fprintf(stderr, "%d %d %d %d %f %f\n", currentline, i - linestart, runningstride, runningstride / (currentline + 1), synclevel, blanklevel);
       linestart = i;
       currentline++;
       fuzz = params->fuzzyness;
       syncing = SYNC_SYNC;
      } else {
       fuzz--;
      }
     } else {
      blanklevel = blanklevel*params->autolevel + connection->stream[i]*invautolevel;
      if(fuzz < params->fuzzyness) {
       fuzz = params->fuzzyness;
      }
     }
     break;
    default:
     return(-1);
   }
  } else {
   switch (syncing) {
    case SYNC_SYNC:
     if(connection->stream[i] < synclevel) {
      if(fuzz == 0) {
       i -= params->fuzzyness;
       fuzz = params->fuzzyness;
       syncing = SYNC_BLANK;
      } else {
       fuzz--;
      }
     } else {
      synclevel = synclevel*params->autolevel + connection->stream[i]*invautolevel;
      if(fuzz < params->fuzzyness) {
       activestart += params->fuzzyness - fuzz;
       fuzz = params->fuzzyness;
      }
     }
     break;
    case SYNC_BLANK:
     if(connection->stream[i] < blanklevel) {
      if(fuzz == 0) {
       i -= params->fuzzyness;
       activestart = i - linestart;
       sampleperiod = (double)(surface->width) / ((double)(runningstride / currentline) -
           (double)activestart -
           (((12) / ((44) + (48))) * (double)activestart));
fprintf(stderr, "%d %f ", activestart, sampleperiod);
       activestart = 0;
       xpos = 0;
       xposd = 0.0;
       samples = 0;
       avgsample = 0;
       whitelevel = blanklevel * ((12.5) / (75.0));
       blacklevel = blanklevel * ((70.0) / (75.0));
fprintf(stderr, "%f %f %f\n", blanklevel, blacklevel, whitelevel);
       fuzz = params->fuzzyness;
       syncing = SYNC_ACTIVE;
      } else {
       fuzz--;
      }
     } else {
      blanklevel = blanklevel*params->autolevel + connection->stream[i]*invautolevel;
      if(fuzz < params->fuzzyness) {
       activestart += params->fuzzyness - fuzz;
       fuzz = params->fuzzyness;
      }
     }
     break;
    case SYNC_ACTIVE:
     if(connection->stream[i] > (synclevel * params->syncthresh)) {
      if(fuzz == 0) {
       i -= params->fuzzyness;
       runningstride += i - linestart;
       linestart = i;
       xpos = 0;
       currentline++;
       fuzz = params->fuzzyness;
       syncing = SYNC_SYNC;
fprintf(stderr, "%d %d %d %f %f\n", currentline, i - linestart, runningstride, synclevel, blanklevel);
fprintf(stderr, "Line %d ended at %d due to hsync reached.\n", currentline, i);
      } else {
       fuzz--;
      }
     } else {
      fuzz = params->fuzzyness;
     }
     if(connection->stream[i] < whitelevel) {
      avgsample += whitelevel;
     } else if(connection->stream[i] > blacklevel) {
      avgsample += blacklevel;
     } else {
      avgsample += connection->stream[i];
     }
     samples++;
     xposd += sampleperiod;
     if((int)xposd > xpos) {
      surface->luma[(currentline - (9)) * surface->width + xpos] = (int)(255.0 - ((avgsample / (double)samples) - whitelevel) * (255.0 / (blacklevel - whitelevel)));
      avgsample = 0;
      samples = 0;
      xpos++;
      if(xpos >= surface->width) {
fprintf(stderr, "Ran out of framebuffer on line %d.\n", currentline);
       syncing = SYNC_END;
      }
     }
     break;
    case SYNC_END:
     if(connection->stream[i] > (synclevel * params->syncthresh)) {
      if(fuzz == 0) {
       i -= params->fuzzyness;
       runningstride += i - linestart;
       currentline++;
fprintf(stderr, "%d %d %d %d %f %d %f\n", currentline, i - linestart, runningstride, runningstride / currentline, xposd, xpos, synclevel);
       linestart = i;
       xpos = 0;
       fuzz = params->fuzzyness;
       syncing = SYNC_SYNC;
      } else {
       fuzz--;
      }
     } else {
      fuzz = params->fuzzyness;
     }
     break;
   }
  }
 }
fprintf(stderr, "Read ended at %d, %d.\n", xpos, currentline);
 return(0);
}

canonical_original_code:
int analog_decode(struct Connection *connection, struct Surface *surface, struct DecodeParams *params) {
 double invautolevel = 1.0 - params->autolevel;
 int i;
 int currentline; int linestart;
 int xpos; int avgsample; int samples;
 double xposd;
 int runningstride;
 int activestart;
 int fuzz;
 double sampleperiod;
 enum SyncMode syncing;
 double synclevel; double blanklevel;
 double whitelevel; double blacklevel;
 currentline = 0;
 syncing = SYNC_SYNC;
 synclevel = (100.0);
 blanklevel = (75.0);
 fuzz = params->fuzzyness;
 activestart = 0;
 linestart = 0;
 for(i = 0; i < connection->length; i++) {
    if(currentline < (9)) {
   switch (syncing) {
    case SYNC_SYNC:
     if(connection->stream[i] < (synclevel * params->syncthresh)) {
      if(fuzz == 0) {
       i -= params->fuzzyness;
       fuzz = params->fuzzyness;
       syncing = SYNC_BLANK;
      } else {
       fuzz--;
      }
     } else {
      synclevel = synclevel*params->autolevel + connection->stream[i]*invautolevel;
      if(fuzz < params->fuzzyness) {
       fuzz = params->fuzzyness;
      }
     }
     break;
    case SYNC_BLANK:
     if(connection->stream[i] > (synclevel * params->syncthresh)) {
      if(fuzz == 0) {
       i -= params->fuzzyness;
       if(currentline == 0) {
        runningstride = i - linestart;
       } else {
        runningstride += i - linestart;
       }
fprintf(stderr, "%d %d %d %d %f %f\n", currentline, i - linestart, runningstride, runningstride / (currentline + 1), synclevel, blanklevel);
       linestart = i;
       currentline++;
       fuzz = params->fuzzyness;
       syncing = SYNC_SYNC;
      } else {
       fuzz--;
      }
     } else {
      blanklevel = blanklevel*params->autolevel + connection->stream[i]*invautolevel;
      if(fuzz < params->fuzzyness) {
       fuzz = params->fuzzyness;
      }
     }
     break;
    default:
     return(-1);
   }
  } else {
   switch (syncing) {
    case SYNC_SYNC:
     if(connection->stream[i] < synclevel) {
      if(fuzz == 0) {
       i -= params->fuzzyness;
       fuzz = params->fuzzyness;
       syncing = SYNC_BLANK;
      } else {
       fuzz--;
      }
     } else {
      synclevel = synclevel*params->autolevel + connection->stream[i]*invautolevel;
      if(fuzz < params->fuzzyness) {
       activestart += params->fuzzyness - fuzz;
       fuzz = params->fuzzyness;
      }
     }
     break;
    case SYNC_BLANK:
     if(connection->stream[i] < blanklevel) {
      if(fuzz == 0) {
       i -= params->fuzzyness;
       activestart = i - linestart;
       sampleperiod = (double)(surface->width) / ((double)(runningstride / currentline) -
           (double)activestart -
           (((12) / ((44) + (48))) * (double)activestart));
fprintf(stderr, "%d %f ", activestart, sampleperiod);
       activestart = 0;
       xpos = 0;
       xposd = 0.0;
       samples = 0;
       avgsample = 0;
       whitelevel = blanklevel * ((12.5) / (75.0));
       blacklevel = blanklevel * ((70.0) / (75.0));
fprintf(stderr, "%f %f %f\n", blanklevel, blacklevel, whitelevel);
       fuzz = params->fuzzyness;
       syncing = SYNC_ACTIVE;
      } else {
       fuzz--;
      }
     } else {
      blanklevel = blanklevel*params->autolevel + connection->stream[i]*invautolevel;
      if(fuzz < params->fuzzyness) {
       activestart += params->fuzzyness - fuzz;
       fuzz = params->fuzzyness;
      }
     }
     break;
    case SYNC_ACTIVE:
     if(connection->stream[i] > (synclevel * params->syncthresh)) {
      if(fuzz == 0) {
       i -= params->fuzzyness;
       runningstride += i - linestart;
       linestart = i;
       xpos = 0;
       currentline++;
       fuzz = params->fuzzyness;
       syncing = SYNC_SYNC;
fprintf(stderr, "%d %d %d %f %f\n", currentline, i - linestart, runningstride, synclevel, blanklevel);
fprintf(stderr, "Line %d ended at %d due to hsync reached.\n", currentline, i);
      } else {
       fuzz--;
      }
     } else {
      fuzz = params->fuzzyness;
     }
     if(connection->stream[i] < whitelevel) {
      avgsample += whitelevel;
     } else if(connection->stream[i] > blacklevel) {
      avgsample += blacklevel;
     } else {
      avgsample += connection->stream[i];
     }
     samples++;
     xposd += sampleperiod;
     if((int)xposd > xpos) {
      surface->luma[(currentline - (9)) * surface->width + xpos] = (int)(255.0 - ((avgsample / (double)samples) - whitelevel) * (255.0 / (blacklevel - whitelevel)));
      avgsample = 0;
      samples = 0;
      xpos++;
      if(xpos >= surface->width) {
fprintf(stderr, "Ran out of framebuffer on line %d.\n", currentline);
       syncing = SYNC_END;
      }
     }
     break;
    case SYNC_END:
     if(connection->stream[i] > (synclevel * params->syncthresh)) {
      if(fuzz == 0) {
       i -= params->fuzzyness;
       runningstride += i - linestart;
       currentline++;
fprintf(stderr, "%d %d %d %d %f %d %f\n", currentline, i - linestart, runningstride, runningstride / currentline, xposd, xpos, synclevel);
       linestart = i;
       xpos = 0;
       fuzz = params->fuzzyness;
       syncing = SYNC_SYNC;
      } else {
       fuzz--;
      }
     } else {
      fuzz = params->fuzzyness;
     }
     break;
   }
  }
 }
fprintf(stderr, "Read ended at %d, %d.\n", xpos, currentline);
 return(0);
}

variable_types: struct Connection { double *stream; int length; } *

variable_types: struct Surface { int width; char *luma; } *

variable_types: struct DecodeParams { double syncthresh; double blankthresh; double autolevel; double autostride; int fuzzyness; } *

variable_types: invautolevel double

variable_types: i int

variable_types: currentline int

variable_types: linestart int

variable_types: xpos int

variable_types: avgsample int

variable_types: samples int

variable_types: xposd double

variable_types: runningstride int

variable_types: activestart int

variable_types: fuzz int

variable_types: sampleperiod double

variable_types: enum SyncMode { SYNC_SYNC=0, SYNC_BLANK=1, SYNC_ACTIVE=2, SYNC_END=3 }

variable_types: synclevel double

variable_types: blanklevel double

variable_types: whitelevel double

variable_types: blacklevel double

return_type: int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__pid_t v3; // eax
int v4; // eax
unsigned int v5; // er12
socklen_t v6; // edx
__pid_t v7; // eax
int v8; // ebp
int *v10; // rax
char *v11; // rax
socklen_t addr_len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

if ( (unsigned int)getcryptid(argc, argv, envp) - 1 > 5 )
ragequit("The crypt id found in /etc/shadow is invalid.");
v3 = fork();
if ( v3 < 0 )
ragequit("fork failed!\n");
if ( v3 )
{
printf("pid of child process %d \n", (unsigned int)v3);
exit(0);
}
umask(6u);
if ( setsid() < 0 )
ragequit("Couldn't get session id.\n");
close(0);
close(1);
v4 = socket(2, 1, 0);
v5 = v4;
if ( v4 < 0 )
ragequit("can't create socket");
fcntl(v4, 2, 1LL);
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = inet_addr("127.0.0.1");
*(_WORD *)addr.sa_data = _bswap_16_constprop_0(v5, &addr, 16LL);
if ( bind(v5, &addr, v6) < 0 )
{
v10 = __errno_location();
v11 = strerror(*v10);
fprintf(stderr, "WARNING: cannot bind to socket %s (%s), exiting\n", "127.0.0.1", v11);
exit(-1);
}
if ( listen(v5, 1) )
ragequit("pald failed to listen on open sock\n");
addr_len = 16;
while ( 1 )
{
v8 = accept(v5, &addr, &addr_len);
if ( v8 < 0 )
break;
v7 = fork();
if ( v7 < 0 )
ragequit("fork");
if ( !v7 )
check_creds(v8);
close(v8);
}
close(v5);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__pid_t v3; // eax
int v4; // eax
unsigned int v5; // er12
socklen_t v6; // edx
__pid_t v7; // eax
int v8; // ebp
int *v10; // rax
char *v11; // rax
socklen_t addr_len; // [rsp+Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-28h] BYREF

if ( (unsigned int)func10(argc, argv, envp) - 1 > 5 )
func8("The crypt id found in /etc/shadow is invalid.");
v3 = fork();
if ( v3 < 0 )
func8("fork failed!\n");
if ( v3 )
{
printf("pid of child process %d \n", (unsigned int)v3);
exit(0);
}
umask(6u);
if ( setsid() < 0 )
func8("Couldn't get session id.\n");
close(0);
close(1);
v4 = socket(2, 1, 0);
v5 = v4;
if ( v4 < 0 )
func8("can't create socket");
fcntl(v4, 2, 1LL);
addr.sa_family = 2;
*(_DWORD *)&addr.sa_data[2] = inet_addr("127.0.0.1");
*(_WORD *)addr.sa_data = func7(v5, &addr, 16LL);
if ( bind(v5, &addr, v6) < 0 )
{
v10 = __errno_location();
v11 = strerror(*v10);
fprintf(stderr, "WARNING: cannot bind to socket %s (%s), exiting\n", "127.0.0.1", v11);
exit(-1);
}
if ( listen(v5, 1) )
func8("pald failed to listen on open sock\n");
addr_len = 16;
while ( 1 )
{
v8 = accept(v5, &addr, &addr_len);
if ( v8 < 0 )
break;
v7 = fork();
if ( v7 < 0 )
func8("fork");
if ( !v7 )
func12(v8);
close(v8);
}
close(v5);
return 0;
}

original_code:
int main(int argc, char* argv[])
{
  int crypt_id=getcryptid();
  if(crypt_id>6 || crypt_id<1)
    ragequit("The crypt id found in /etc/shadow is invalid.");
  pid_t pid = 0;
  pid_t sid = 0;
  pid = fork();
  if (pid < 0)
    ragequit("fork failed!\n");
  if (pid > 0)
    {
      printf("pid of child process %d \n", pid);
      exit(0);
    }
  umask(((0400 >> 3) >> 3)|((0200 >> 3) >> 3));
  sid = setsid();
  if(sid < 0)
    {
      ragequit("Couldn't get session id.\n");
    }
  close(0);
  close(1);
  int infd=socket(2, SOCK_STREAM, 0);
  if (infd < 0)
    ragequit("can't create socket");
  fcntl(infd, 2, 1);
  struct sockaddr_in addr;
  addr.sin_family = 2;
  addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  addr.sin_port = htons(7331);
  if (bind(infd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
    fprintf(stderr,"WARNING: cannot bind to socket %s (%s), exiting\n",
     "127.0.0.1", strerror((*__errno_location ())));
    exit(-1);
  }
  if(listen(infd,1)!=0)
    ragequit("pald failed to listen on open sock\n");
  int connection_fd;
  socklen_t address_length=(socklen_t)sizeof(struct sockaddr);
  while((connection_fd = accept(infd, (struct sockaddr *) &addr, &address_length )) > -1){
    pid_t child = fork();
    if(child<0)
      ragequit("fork");
    else if(child == 0)
      return check_creds(connection_fd, crypt_id);
    else
      close(connection_fd);
  }
  close(infd);
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  int crypt_id = getcryptid();
  if(crypt_id>6 || crypt_id<1)
    ragequit("The crypt id found in /etc/shadow is invalid.");
  int pid = 0;
  int sid = 0;
  pid = fork();
  if (pid < 0)
    ragequit("fork failed!\n");
  if (pid > 0)
    {
      printf("pid of child process %d \n", pid);
      exit(0);
    }
  umask(((0400 >> 3) >> 3)|((0200 >> 3) >> 3));
  sid = setsid();
  if(sid < 0)
    {
      ragequit("Couldn't get session id.\n");
    }
  close(0);
  close(1);
  int infd = socket(2, SOCK_STREAM, 0);
  if (infd < 0)
    ragequit("can't create socket");
  fcntl(infd, 2, 1);
  struct sockaddr_in addr;
  addr.sin_family = 2;
  addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  addr.sin_port = htons(7331);
  if (bind(infd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
    fprintf(stderr,"WARNING: cannot bind to socket %s (%s), exiting\n",
     "127.0.0.1", strerror((*__errno_location ())));
    exit(-1);
  }
  if(listen(infd,1)!=0)
    ragequit("pald failed to listen on open sock\n");
  int connection_fd;
  unsigned int address_length = (socklen_t)sizeof(struct sockaddr);
  while((connection_fd = accept(infd, (struct sockaddr *) &addr, &address_length )) > -1){
    int child = fork();
    if(child<0)
      ragequit("fork");
    else if(child == 0)
      return check_creds(connection_fd, crypt_id);
    else
      close(connection_fd);
  }
  close(infd);
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: pid int

variable_types: sid int

variable_types: infd int

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

variable_types: child int

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

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


ragequit

decompiled_code:
void __fastcall __noreturn ragequit(const char *a1)
{
syslog(27, "%s", a1);
exit(-1);
}

canonical_decompiled_code:
void __fastcall __noreturn func8(const char *a1)
{
syslog(27, "%s", a1);
exit(-1);
}

original_code:
void ragequit(const char *msg){
  syslog((3<<3)|3,"%s", msg);
  exit(-1);
}

canonical_original_code:
void ragequit(char *msg){
  syslog((3<<3)|3,"%s", msg);
  exit(-1);
}

variable_types:
msg
char *

return_type: void


isnumber

decompiled_code:
__int16 __fastcall isnumber(char *a1)
{
__int64 v1; // rbx
char *v2; // rbp
const unsigned __int16 *v3; // rdx
__int16 result; // ax

v1 = *a1;
if ( !(_BYTE)v1 || (_BYTE)v1 == 58 )
return 1;
v2 = a1;
v3 = *__ctype_b_loc();
while ( 1 )
{
result = v3[v1] & 0x800;
if ( !result )
break;
v1 = *++v2;
if ( !(_BYTE)v1 || (_BYTE)v1 == 58 )
return 1;
}
return result;
}

canonical_decompiled_code:
__int16 __fastcall func9(char *a1)
{
__int64 v1; // rbx
char *v2; // rbp
const unsigned __int16 *v3; // rdx
__int16 result; // ax

v1 = *a1;
if ( !(_BYTE)v1 || (_BYTE)v1 == 58 )
return 1;
v2 = a1;
v3 = *__ctype_b_loc();
while ( 1 )
{
result = v3[v1] & 0x800;
if ( !result )
break;
v1 = *++v2;
if ( !(_BYTE)v1 || (_BYTE)v1 == 58 )
return 1;
}
return result;
}

original_code:
unsigned short int isnumber(char *buf){
  while(*buf != '\0' && *buf != ':'){
    if(!((*__ctype_b_loc ())[(int) ((*buf))] & (unsigned short int) _ISdigit))
      return 0;
    buf++;
  }
  return 1;
}

canonical_original_code:
unsigned short isnumber(char *buf){
  while(*buf != '\0' && *buf != ':'){
    if(!((*__ctype_b_loc ())[(int) ((*buf))] & (unsigned short) _ISdigit))
      return 0;
    buf++;
  }
  return 1;
}

variable_types:
buf
char *

return_type: unsigned short


mstrcmp

decompiled_code:
__int64 __fastcall mstrcmp(__int64 a1, __int64 a2)
{
__int64 v2; // rax
char v3; // dl

v2 = 0LL;
do
v3 = *(_BYTE *)(a1 + v2++);
while ( v3 == *(_BYTE *)(a2 + v2 - 1) );
return 1LL;
}


dispose

repo: pawel-smolenski/MyFS

decompiled_code:
__int64 __fastcall dispose(unsigned int a1)
{
fseek(partitionFile, 0LL, 0);
fwrite(superblock, 0x1CuLL, 1uLL, partitionFile);
fwrite(iNodeTable, 0x20uLL, iNodeTableSize, partitionFile);
free(filename);
fclose(partitionFile);
free(superblock);
free(iNodeTable);
return a1;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1)
{
fseek(partitionFile, 0LL, 0);
fwrite(superblock, 0x1CuLL, 1uLL, partitionFile);
fwrite(iNodeTable, 0x20uLL, iNodeTableSize, partitionFile);
free(filename);
fclose(partitionFile);
free(superblock);
free(iNodeTable);
return a1;
}

original_code:
int dispose(int returnCode)
{
 fseek(partitionFile, 0, 0);
 fwrite(superblock, sizeof(struct SUPERBLOCK), 1, partitionFile);
 fwrite(iNodeTable, sizeof(struct INODE), iNodeTableSize, partitionFile);
 free(filename);
 fclose(partitionFile);
 free(superblock);
 free(iNodeTable);
 return returnCode;
}

variable_types: returnCode int

user_defined_types: struct SUPERBLOCK { int partitionSize; int blockSize; int dataBlockOffset; int firstFile; int lastFile; int firstFree; int lastFree; }

user_defined_types: struct INODE { char fileName[16]; int fileSize; int blockAddres; int nextBlock; int nextFile; }


decompiled_code:
__int64 print_help()
{
puts("My File System");
puts(" create SIZE BLOCKSIZE - Tworzy nowa partycje o rozmiarze SIZE bajtow");
puts(" delete - Usuwa partycje");
puts(" map - Wyswietla mape partycji");
puts(aLsWyswietlaZaw);
puts(aCptoSourceFile);
puts(" cpfrom FILENAME DEST - Kopiuje plik z partycji na lokalny dysk");
puts(" rm FILENAME - Usuwa plik z partycji");
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
puts("My File System");
puts(" create SIZE BLOCKSIZE - Tworzy nowa partycje o rozmiarze SIZE bajtow");
puts(" delete - Usuwa partycje");
puts(" map - Wyswietla mape partycji");
puts(aLsWyswietlaZaw);
puts(aCptoSourceFile);
puts(" cpfrom FILENAME DEST - Kopiuje plik z partycji na lokalny dysk");
puts(" rm FILENAME - Usuwa plik z partycji");
return 0LL;
}

original_code:
int print_help(void)
{
 printf("My File System\n");
 printf("  create SIZE BLOCKSIZE - Tworzy nowa partycje o rozmiarze SIZE bajtow\n");
 printf("  delete - Usuwa partycje\n");
 printf("  map - Wyswietla mape partycji\n");
 printf("  ls - Wyswietla zawartość katalogu glownego\n");
 printf("  cpto SOURCE FILENAME - Kopiuje plik z lokalnego dysku na partycję\n");
 printf("  cpfrom FILENAME DEST - Kopiuje plik z partycji na lokalny dysk\n");
 printf("  rm FILENAME - Usuwa plik z partycji\n");
 return 0;
}

canonical_original_code:
int print_help()
{
 printf("My File System\n");
 printf("  create SIZE BLOCKSIZE - Tworzy nowa partycje o rozmiarze SIZE bajtow\n");
 printf("  delete - Usuwa partycje\n");
 printf("  map - Wyswietla mape partycji\n");
 printf("  ls - Wyswietla zawartość katalogu glownego\n");
 printf("  cpto SOURCE FILENAME - Kopiuje plik z lokalnego dysku na partycję\n");
 printf("  cpfrom FILENAME DEST - Kopiuje plik z partycji na lokalny dysk\n");
 printf("  rm FILENAME - Usuwa plik z partycji\n");
 return 0;
}


getFilename

decompiled_code:
char *getFilename()
{
char *result; // rax

result = (char *)calloc(0x10uLL, 1uLL);
strcpy(result, "./partition.mfs");
return result;
}

canonical_decompiled_code:
char *func2()
{
char *result; // rax

result = (char *)calloc(0x10uLL, 1uLL);
strcpy(result, "./partition.mfs");
return result;
}

original_code:
char *getFilename(void)
{
 char *filename = calloc(strlen("partition.mfs") + 3, sizeof(char));
 strcpy(filename, "./");
 strcat(filename, "partition.mfs");
 return filename;
}

canonical_original_code:
char *getFilename()
{
 char *filename = calloc(strlen("partition.mfs") + 3, sizeof(char));
 strcpy(filename, "./");
 strcat(filename, "partition.mfs");
 return filename;
}

variable_types:
filename
char *

return_type: char *


calculateINodeTableSize

decompiled_code:
unsigned __int64 __fastcall calculateINodeTableSize(int a1, int a2)
{
return a1 / (unsigned __int64)(a2 + 32LL);
}

canonical_decompiled_code:
unsigned __int64 __fastcall func3(int a1, int a2)
{
return a1 / (unsigned __int64)(a2 + 32LL);
}

original_code:
int calculateINodeTableSize(int size, int blockSize)
{
 return size / (blockSize + sizeof(struct INODE));
}

variable_types: size int

variable_types: blockSize int


init

decompiled_code:
void init()
{
FILE *v0; // rax
FILE *v1; // r12
int v2; // eax
size_t v3; // r12

filename = getFilename();
v0 = fopen(filename, "r+");
partitionFile = v0;
if ( v0 )
{
v1 = v0;
superblock = calloc(1uLL, 0x1CuLL);
fread(superblock, 0x1CuLL, 1uLL, v1);
v2 = calculateINodeTableSize(*(_DWORD *)superblock, *((_DWORD *)superblock + 1));
v3 = v2;
iNodeTableSize = v2;
iNodeTable = calloc(v2, 0x20uLL);
fread(iNodeTable, 0x20uLL, v3, partitionFile);
}
else
{
puts("Partycja nie istnieje");
free(filename);
}
}

canonical_decompiled_code:
void func4()
{
FILE *v0; // rax
FILE *v1; // r12
int v2; // eax
size_t v3; // r12

filename = func2();
v0 = fopen(filename, "r+");
partitionFile = v0;
if ( v0 )
{
v1 = v0;
superblock = calloc(1uLL, 0x1CuLL);
fread(superblock, 0x1CuLL, 1uLL, v1);
v2 = func3(*(_DWORD *)superblock, *((_DWORD *)superblock + 1));
v3 = v2;
iNodeTableSize = v2;
iNodeTable = calloc(v2, 0x20uLL);
fread(iNodeTable, 0x20uLL, v3, partitionFile);
}
else
{
puts("Partycja nie istnieje");
free(filename);
}
}

original_code:
void init(void)
{
 filename = getFilename();
 partitionFile = fopen(filename, "r+");
 if (partitionFile == ((void *)0))
 {
  printf("Partycja nie istnieje\n");
  free(filename);
  return;
 }
 superblock = calloc(1, sizeof(struct SUPERBLOCK));
 fread(superblock, sizeof(struct SUPERBLOCK), 1, partitionFile);
 iNodeTableSize = calculateINodeTableSize(superblock->partitionSize, superblock->blockSize);
 iNodeTable = (struct INODE *)calloc(iNodeTableSize, sizeof(struct INODE));
 fread(iNodeTable, sizeof(struct INODE), iNodeTableSize, partitionFile);
}

canonical_original_code:
void init()
{
 filename = getFilename();
 partitionFile = fopen(filename, "r+");
 if (partitionFile == ((void *)0))
 {
  printf("Partycja nie istnieje\n");
  free(filename);
  return;
 }
 superblock = calloc(1, sizeof(struct SUPERBLOCK));
 fread(superblock, sizeof(struct SUPERBLOCK), 1, partitionFile);
 iNodeTableSize = calculateINodeTableSize(superblock->partitionSize, superblock->blockSize);
 iNodeTable = (struct INODE *)calloc(iNodeTableSize, sizeof(struct INODE));
 fread(iNodeTable, sizeof(struct INODE), iNodeTableSize, partitionFile);
}


filenameExists

decompiled_code:
int __fastcall filenameExists(char *s2)
{
__int64 v1; // rax
char *v2; // r12
const char *v3; // rbx
int result; // eax

v1 = *((int *)superblock + 3);
if ( (int)v1 < 0 )
return -1;
v2 = (char *)iNodeTable;
while ( 1 )
{
v3 = &v2[32 * v1];
result = strcmp(v3, s2);
if ( !result )
break;
v1 = *((int *)v3 + 7);
if ( (int)v1 < 0 )
return -1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func5(char *s2)
{
__int64 v1; // rax
char *v2; // r12
const char *v3; // rbx
int result; // eax

v1 = *((int *)superblock + 3);
if ( (int)v1 < 0 )
return -1;
v2 = (char *)iNodeTable;
while ( 1 )
{
v3 = &v2[32 * v1];
result = strcmp(v3, s2);
if ( !result )
break;
v1 = *((int *)v3 + 7);
if ( (int)v1 < 0 )
return -1;
}
return result;
}

original_code:
int filenameExists(char* filename)
{
 int fileAddress;
 for(fileAddress = superblock->firstFile; fileAddress >= 0; fileAddress = iNodeTable[fileAddress].nextFile)
 {
  if(strcmp(iNodeTable[fileAddress].fileName, filename) == 0)
  {
   return 0;
  }
 }
 return -1;
}

canonical_original_code:
int filenameExists(char *filename)
{
 int fileAddress;
 for(fileAddress = superblock->firstFile; fileAddress >= 0; fileAddress = iNodeTable[fileAddress].nextFile)
 {
  if(strcmp(iNodeTable[fileAddress].fileName, filename) == 0)
  {
   return 0;
  }
 }
 return -1;
}

variable_types: fileAddress int


hasEnoughFreeSpace

decompiled_code:
__int64 __fastcall hasEnoughFreeSpace(int a1)
{
__int64 result; // rax
int v2; // edx

result = *((int *)superblock + 5);
if ( a1 > 0 )
{
if ( (_DWORD)result == -1 )
return result;
v2 = 0;
while ( 1 )
{
v2 += *((_DWORD *)superblock + 1);
result = *((int *)iNodeTable + 8 * result + 6);
if ( a1 <= v2 )
break;
if ( (_DWORD)result == -1 )
return result;
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1)
{
__int64 result; // rax
int v2; // edx

result = *((int *)superblock + 5);
if ( a1 > 0 )
{
if ( (_DWORD)result == -1 )
return result;
v2 = 0;
while ( 1 )
{
v2 += *((_DWORD *)superblock + 1);
result = *((int *)iNodeTable + 8 * result + 6);
if ( a1 <= v2 )
break;
if ( (_DWORD)result == -1 )
return result;
}
}
return 0LL;
}

original_code:
int hasEnoughFreeSpace(int size)
{
 int freeBlockAddr;
 int freeSpace = 0;
 for(freeBlockAddr=superblock->firstFree; freeSpace < size; freeBlockAddr = iNodeTable[freeBlockAddr].nextBlock)
 {
  if(freeBlockAddr == -1)
  {
   return -1;
  }
  freeSpace += superblock->blockSize;
 }
 return 0;
}

variable_types: freeBlockAddr int

variable_types: freeSpace int


format

decompiled_code:
__int64 __fastcall format(int a1, int a2)
{
_DWORD *v2; // rax
int v3; // eax
int v4; // ebx
__int64 v5; // rcx
_DWORD *v6; // rax
int v7; // edx
char ptr[25]; // [rsp+Fh] [rbp-19h] BYREF

ptr[0] = 0;
v2 = calloc(1uLL, 0x1CuLL);
*v2 = a1;
v2[1] = a2;
v2[3] = -1;
superblock = v2;
*((_QWORD *)v2 + 2) = 0xFFFFFFFFLL;
v3 = calculateINodeTableSize(a1, a2);
iNodeTableSize = v3;
v4 = v3;
*(_DWORD *)(v5 + 24) = v3 - 1;
*(_DWORD *)(v5 + 8) = 32 * v3 + 28;
v6 = calloc(v3, 0x20uLL);
iNodeTable = v6;
if ( v4 > 0 )
{
v7 = 0;
do
{
while ( 1 )
{
v6[5] = v7++;
*(_BYTE *)v6 = 0;
v6[4] = 0;
v6[7] = -1;
if ( v4 <= v7 )
break;
v6[6] = v7;
v6 += 8;
}
v6[6] = -1;
v6 += 8;
}
while ( v4 != v7 );
}
fseek(partitionFile, 0LL, 0);
fwrite(superblock, 0x1CuLL, 1uLL, partitionFile);
fwrite(iNodeTable, 0x20uLL, iNodeTableSize, partitionFile);
fseek(partitionFile, a1, 0);
fwrite(ptr, 1uLL, 1uLL, partitionFile);
free(iNodeTable);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2)
{
_DWORD *v2; // rax
int v3; // eax
int v4; // ebx
__int64 v5; // rcx
_DWORD *v6; // rax
int v7; // edx
char ptr[25]; // [rsp+Fh] [rbp-19h] BYREF

ptr[0] = 0;
v2 = calloc(1uLL, 0x1CuLL);
*v2 = a1;
v2[1] = a2;
v2[3] = -1;
superblock = v2;
*((_QWORD *)v2 + 2) = 0xFFFFFFFFLL;
v3 = func3(a1, a2);
iNodeTableSize = v3;
v4 = v3;
*(_DWORD *)(v5 + 24) = v3 - 1;
*(_DWORD *)(v5 + 8) = 32 * v3 + 28;
v6 = calloc(v3, 0x20uLL);
iNodeTable = v6;
if ( v4 > 0 )
{
v7 = 0;
do
{
while ( 1 )
{
v6[5] = v7++;
*(_BYTE *)v6 = 0;
v6[4] = 0;
v6[7] = -1;
if ( v4 <= v7 )
break;
v6[6] = v7;
v6 += 8;
}
v6[6] = -1;
v6 += 8;
}
while ( v4 != v7 );
}
fseek(partitionFile, 0LL, 0);
fwrite(superblock, 0x1CuLL, 1uLL, partitionFile);
fwrite(iNodeTable, 0x20uLL, iNodeTableSize, partitionFile);
fseek(partitionFile, a1, 0);
fwrite(ptr, 1uLL, 1uLL, partitionFile);
free(iNodeTable);
return 0LL;
}

original_code:
int format(int size, int blockSize)
{
 int i;
 char buffer[1];
 buffer[0] = '\0';
 superblock = calloc(1, sizeof(struct SUPERBLOCK));
 superblock->partitionSize = size;
 superblock->blockSize = blockSize;
 superblock->firstFile = -1;
 superblock->lastFile = -1;
 superblock->firstFree = 0;
 iNodeTableSize = calculateINodeTableSize(size, blockSize);
 superblock->lastFree = iNodeTableSize-1;
 superblock->dataBlockOffset = sizeof(struct SUPERBLOCK) + sizeof(struct INODE)*iNodeTableSize;
 iNodeTable = (struct INODE*) calloc(iNodeTableSize, sizeof(struct INODE));
 for (i = 0; i < iNodeTableSize; i++)
 {
  iNodeTable[i].fileName[0] = '\0';
  iNodeTable[i].fileSize = 0;
  iNodeTable[i].blockAddres = i;
  iNodeTable[i].nextFile = -1;
  if(i+1 < iNodeTableSize)
  {
   iNodeTable[i].nextBlock = i+1;
  }
  else
  {
   iNodeTable[i].nextBlock = -1;
  }
 }
 fseek(partitionFile, 0, 0);
 fwrite(superblock, sizeof(struct SUPERBLOCK), 1, partitionFile);
 fwrite(iNodeTable, sizeof(struct INODE), iNodeTableSize, partitionFile);
 fseek(partitionFile, size, 0);
 fwrite(buffer, sizeof(char), 1, partitionFile);
 free(iNodeTable);
 return 0;
}

canonical_original_code:
int format(int size, int blockSize)
{
 int i;
 char buffer[1];
 buffer[0] = '\0';
 superblock = calloc(1, sizeof(struct SUPERBLOCK));
 superblock->partitionSize = size;
 superblock->blockSize = blockSize;
 superblock->firstFile = -1;
 superblock->lastFile = -1;
 superblock->firstFree = 0;
 iNodeTableSize = calculateINodeTableSize(size, blockSize);
 superblock->lastFree = iNodeTableSize-1;
 superblock->dataBlockOffset = sizeof(struct SUPERBLOCK) + sizeof(struct INODE)*iNodeTableSize;
 iNodeTable = (struct INODE *) calloc(iNodeTableSize, sizeof(struct INODE));
 for (i = 0; i < iNodeTableSize; i++)
 {
  iNodeTable[i].fileName[0] = '\0';
  iNodeTable[i].fileSize = 0;
  iNodeTable[i].blockAddres = i;
  iNodeTable[i].nextFile = -1;
  if(i+1 < iNodeTableSize)
  {
   iNodeTable[i].nextBlock = i+1;
  }
  else
  {
   iNodeTable[i].nextBlock = -1;
  }
 }
 fseek(partitionFile, 0, 0);
 fwrite(superblock, sizeof(struct SUPERBLOCK), 1, partitionFile);
 fwrite(iNodeTable, sizeof(struct INODE), iNodeTableSize, partitionFile);
 fseek(partitionFile, size, 0);
 fwrite(buffer, sizeof(char), 1, partitionFile);
 free(iNodeTable);
 return 0;
}

variable_types: i int

variable_types:
buffer
char [1]


create

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

filename = getFilename();
if ( a1 < (unsigned __int64)(10 * a2 + 60LL) )
{
puts("Rozmiar partycji jest nieprawidlowy");
result = 1LL;
}
else
{
partitionFile = fopen(filename, "wb");
format(a1, a2);
fclose(partitionFile);
free(filename);
result = 0LL;
}
return result;
}

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

filename = func2();
if ( a1 < (unsigned __int64)(10 * a2 + 60LL) )
{
puts("Rozmiar partycji jest nieprawidlowy");
result = 1LL;
}
else
{
partitionFile = fopen(filename, "wb");
func7(a1, a2);
fclose(partitionFile);
free(filename);
result = 0LL;
}
return result;
}

original_code:
int create(int size, int blockSize)
{
 filename = getFilename();
 if (size < (sizeof(struct SUPERBLOCK) + sizeof(struct INODE) + 10 * blockSize))
 {
  printf("Rozmiar partycji jest nieprawidlowy\n");
  return 1;
 }
 partitionFile = fopen(filename, "wb");
 format(size, blockSize);
 fclose(partitionFile);
 free(filename);
 return 0;
}


delete

decompiled_code:
__int64 delete()
{
FILE *v0; // rax
const char *v1; // r12
size_t v2; // rax
char *v3; // rax
char *v4; // rbp
__int64 result; // rax
char v6[25]; // [rsp+Fh] [rbp-19h] BYREF

filename = getFilename();
v0 = fopen(filename, "r");
partitionFile = v0;
if ( v0 )
{
fclose(v0);
printf("Czy na pewno usunac partycje? y/n: ");
_isoc99_scanf("%c", v6);
if ( v6[0] == 110 )
{
puts("Usuniecie partycji anulowane");
}
else
{
v1 = filename;
v2 = strlen(filename);
v3 = (char *)calloc(v2 + 4, 1uLL);
*(_DWORD *)v3 = 2125170;
v4 = v3;
strcpy(v3 + 3, v1);
system(v4);
puts("Partycja zostala usunieta");
free(v4);
free(filename);
}
result = 0LL;
}
else
{
puts("Partycja nie istnieje");
free(filename);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func9()
{
FILE *v0; // rax
const char *v1; // r12
size_t v2; // rax
char *v3; // rax
char *v4; // rbp
__int64 result; // rax
char v6[25]; // [rsp+Fh] [rbp-19h] BYREF

filename = func2();
v0 = fopen(filename, "r");
partitionFile = v0;
if ( v0 )
{
fclose(v0);
printf("Czy na pewno usunac partycje? y/n: ");
_isoc99_scanf("%c", v6);
if ( v6[0] == 110 )
{
puts("Usuniecie partycji anulowane");
}
else
{
v1 = filename;
v2 = strlen(filename);
v3 = (char *)calloc(v2 + 4, 1uLL);
*(_DWORD *)v3 = 2125170;
v4 = v3;
strcpy(v3 + 3, v1);
system(v4);
puts("Partycja zostala usunieta");
free(v4);
free(filename);
}
result = 0LL;
}
else
{
puts("Partycja nie istnieje");
free(filename);
result = 1LL;
}
return result;
}

original_code:
int delete(void)
{
 char *command;
 char decision;
 filename = getFilename();
 partitionFile = fopen(filename, "r");
 if (partitionFile == ((void *)0))
 {
  printf("Partycja nie istnieje\n");
  free(filename);
  return 1;
 }
 fclose(partitionFile);
 printf("Czy na pewno usunac partycje? y/n: ");
 scanf("%c", &decision);
 if (decision == 'n')
 {
  printf("Usuniecie partycji anulowane\n");
  return 0;
 }
 command = calloc(strlen(filename) + 4, sizeof(char));
 strcpy(command, "rm ");
 strcat(command, filename);
 system(command);
 printf("Partycja zostala usunieta\n");
 free(command);
 free(filename);
 return 0;
}

canonical_original_code:
int delete()
{
 char *command;
 char decision;
 filename = getFilename();
 partitionFile = fopen(filename, "r");
 if (partitionFile == ((void *)0))
 {
  printf("Partycja nie istnieje\n");
  free(filename);
  return 1;
 }
 fclose(partitionFile);
 printf("Czy na pewno usunac partycje? y/n: ");
 scanf("%c", &decision);
 if (decision == 'n')
 {
  printf("Usuniecie partycji anulowane\n");
  return 0;
 }
 command = calloc(strlen(filename) + 4, sizeof(char));
 strcpy(command, "rm ");
 strcat(command, filename);
 system(command);
 printf("Partycja zostala usunieta\n");
 free(command);
 free(filename);
 return 0;
}

variable_types:
command
char *

variable_types: decision char


map

decompiled_code:
__int64 map()
{
__int64 v0; // rbx
__int64 v1; // rbp

printf(
"Size: %d Block: %d DataOffset: %d FirstFile: %d LastFile: %d FirstFree: %d LastFree: %d\n",
*(unsigned int *)superblock,
*((unsigned int *)superblock + 1),
*((unsigned int *)superblock + 2),
*((unsigned int *)superblock + 3),
*((unsigned int *)superblock + 4),
*((unsigned int *)superblock + 5),
*((unsigned int *)superblock + 6));
puts("ID\tFilename\tFileSize\tNextFile\tNextBlock");
if ( iNodeTableSize > 0 )
{
v0 = 0LL;
do
{
printf("%3d", (unsigned int)v0);
v1 = 32 * v0;
if ( *((_BYTE *)iNodeTable + 32 * v0) )
{
printf("\t%15s", (const char *)iNodeTable + 32 * v0);
printf("\t%5d", *(unsigned int *)((char *)iNodeTable + v1 + 16));
}
else
{
printf("\tEMPTY BLOCK ");
printf("\t - ");
}
++v0;
printf("\t%d", *(unsigned int *)((char *)iNodeTable + v1 + 28));
printf("\t%d", *(unsigned int *)((char *)iNodeTable + v1 + 24));
putchar(10);
}
while ( iNodeTableSize > (int)v0 );
}
return 0LL;
}

canonical_decompiled_code:
__int64 func10()
{
__int64 v0; // rbx
__int64 v1; // rbp

printf(
"Size: %d Block: %d DataOffset: %d FirstFile: %d LastFile: %d FirstFree: %d LastFree: %d\n",
*(unsigned int *)superblock,
*((unsigned int *)superblock + 1),
*((unsigned int *)superblock + 2),
*((unsigned int *)superblock + 3),
*((unsigned int *)superblock + 4),
*((unsigned int *)superblock + 5),
*((unsigned int *)superblock + 6));
puts("ID\tFilename\tFileSize\tNextFile\tNextBlock");
if ( iNodeTableSize > 0 )
{
v0 = 0LL;
do
{
printf("%3d", (unsigned int)v0);
v1 = 32 * v0;
if ( *((_BYTE *)iNodeTable + 32 * v0) )
{
printf("\t%15s", (const char *)iNodeTable + 32 * v0);
printf("\t%5d", *(unsigned int *)((char *)iNodeTable + v1 + 16));
}
else
{
printf("\tEMPTY BLOCK ");
printf("\t - ");
}
++v0;
printf("\t%d", *(unsigned int *)((char *)iNodeTable + v1 + 28));
printf("\t%d", *(unsigned int *)((char *)iNodeTable + v1 + 24));
putchar(10);
}
while ( iNodeTableSize > (int)v0 );
}
return 0LL;
}

original_code:
int map(void)
{
 int i;
 printf("Size: %d  Block: %d  DataOffset: %d FirstFile: %d  LastFile: %d FirstFree: %d LastFree: %d\n", superblock->partitionSize, superblock->blockSize, superblock->dataBlockOffset, superblock->firstFile, superblock->lastFile, superblock->firstFree, superblock->lastFree);
 printf("ID\tFilename\tFileSize\tNextFile\tNextBlock\n" );
 for(i = 0; i < iNodeTableSize; i++)
 {
  printf("%3d", i);
  if(strlen(iNodeTable[i].fileName) > 0)
  {
   printf("\t%15s", iNodeTable[i].fileName);
   printf("\t%5d", iNodeTable[i].fileSize);
  }
  else
  {
   printf("\tEMPTY BLOCK    ");
   printf("\t  -  ");
  }
  printf("\t%d", iNodeTable[i].nextFile);
  printf("\t%d", iNodeTable[i].nextBlock);
  printf("\n");
 }
 return 0;
}

canonical_original_code:
int map()
{
 int i;
 printf("Size: %d  Block: %d  DataOffset: %d FirstFile: %d  LastFile: %d FirstFree: %d LastFree: %d\n", superblock->partitionSize, superblock->blockSize, superblock->dataBlockOffset, superblock->firstFile, superblock->lastFile, superblock->firstFree, superblock->lastFree);
 printf("ID\tFilename\tFileSize\tNextFile\tNextBlock\n" );
 for(i = 0; i < iNodeTableSize; i++)
 {
  printf("%3d", i);
  if(strlen(iNodeTable[i].fileName) > 0)
  {
   printf("\t%15s", iNodeTable[i].fileName);
   printf("\t%5d", iNodeTable[i].fileSize);
  }
  else
  {
   printf("\tEMPTY BLOCK    ");
   printf("\t  -  ");
  }
  printf("\t%d", iNodeTable[i].nextFile);
  printf("\t%d", iNodeTable[i].nextBlock);
  printf("\n");
 }
 return 0;
}


ls

decompiled_code:
__int64 ls()
{
const char *v0; // rsi
int v1; // eax
__int64 v2; // rbx
__int64 result; // rax

v0 = (const char *)iNodeTable;
v1 = *((_DWORD *)superblock + 3);
if ( v1 == -1 )
{
puts("Brak plikow na dysku");
result = 0LL;
}
else
{
do
{
v2 = 32LL * v1;
printf("%15s %-5d\n", &v0[v2], *(unsigned int *)&v0[v2 + 16]);
v0 = (const char *)iNodeTable;
v1 = *(_DWORD *)((char *)iNodeTable + v2 + 28);
}
while ( v1 != -1 );
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 func11()
{
const char *v0; // rsi
int v1; // eax
__int64 v2; // rbx
__int64 result; // rax

v0 = (const char *)iNodeTable;
v1 = *((_DWORD *)superblock + 3);
if ( v1 == -1 )
{
puts("Brak plikow na dysku");
result = 0LL;
}
else
{
do
{
v2 = 32LL * v1;
printf("%15s %-5d\n", &v0[v2], *(unsigned int *)&v0[v2 + 16]);
v0 = (const char *)iNodeTable;
v1 = *(_DWORD *)((char *)iNodeTable + v2 + 28);
}
while ( v1 != -1 );
result = 0LL;
}
return result;
}

original_code:
int ls(void)
{
 int fileAddr;
 if(superblock->firstFile == -1)
 {
  printf("Brak plikow na dysku\n");
 }
 else
 {
  for(fileAddr = superblock->firstFile; fileAddr != -1; fileAddr = iNodeTable[fileAddr].nextFile)
  {
   printf("%15s   %-5d\n", iNodeTable[fileAddr].fileName, iNodeTable[fileAddr].fileSize);
  }
 }
 return 0;
}

canonical_original_code:
int ls()
{
 int fileAddr;
 if(superblock->firstFile == -1)
 {
  printf("Brak plikow na dysku\n");
 }
 else
 {
  for(fileAddr = superblock->firstFile; fileAddr != -1; fileAddr = iNodeTable[fileAddr].nextFile)
  {
   printf("%15s   %-5d\n", iNodeTable[fileAddr].fileName, iNodeTable[fileAddr].fileSize);
  }
 }
 return 0;
}

variable_types: fileAddr int


rm

original_code:
int rm(char *filename)
{
 int blockAddr, prevBlockAddr = -1;
 struct INODE* currentFile;
 for(blockAddr = superblock->firstFile; blockAddr != -1; blockAddr = currentFile->nextFile)
 {
  currentFile = &iNodeTable[blockAddr];
  if(strcmp(currentFile->fileName, filename) == 0)
  {
   break;
  }
  prevBlockAddr = blockAddr;
 }
 if(blockAddr == -1)
 {
  printf("Plik o podanej nazwie nie zostal znaleziony\n");
  return 1;
 }
 if(blockAddr == superblock->firstFile)
 {
  superblock->firstFile = currentFile->nextFile;
 }
 if(blockAddr == superblock->lastFile)
 {
  superblock->lastFile = prevBlockAddr;
 }
 if(prevBlockAddr != -1)
 {
  iNodeTable[prevBlockAddr].nextFile = currentFile->nextFile;
 }
 while(blockAddr != -1)
 {
  currentFile->fileName[0] = '\0';
  currentFile->fileSize = 0;
  currentFile->nextFile = -1;
  iNodeTable[superblock->lastFree].nextBlock = blockAddr;
  superblock->lastFree = blockAddr;
  if(superblock->firstFree == -1)
  {
   superblock->firstFree = blockAddr;
  }
  blockAddr = currentFile->nextBlock;
  currentFile->nextBlock = -1;
  currentFile = &iNodeTable[blockAddr];
 }
 return 0;
}

canonical_original_code:
int rm(char *filename)
{
 int blockAddr; int prevBlockAddr = -1;
 struct INODE *currentFile;
 for(blockAddr = superblock->firstFile; blockAddr != -1; blockAddr = currentFile->nextFile)
 {
  currentFile = &iNodeTable[blockAddr];
  if(strcmp(currentFile->fileName, filename) == 0)
  {
   break;
  }
  prevBlockAddr = blockAddr;
 }
 if(blockAddr == -1)
 {
  printf("Plik o podanej nazwie nie zostal znaleziony\n");
  return 1;
 }
 if(blockAddr == superblock->firstFile)
 {
  superblock->firstFile = currentFile->nextFile;
 }
 if(blockAddr == superblock->lastFile)
 {
  superblock->lastFile = prevBlockAddr;
 }
 if(prevBlockAddr != -1)
 {
  iNodeTable[prevBlockAddr].nextFile = currentFile->nextFile;
 }
 while(blockAddr != -1)
 {
  currentFile->fileName[0] = '\0';
  currentFile->fileSize = 0;
  currentFile->nextFile = -1;
  iNodeTable[superblock->lastFree].nextBlock = blockAddr;
  superblock->lastFree = blockAddr;
  if(superblock->firstFree == -1)
  {
   superblock->firstFree = blockAddr;
  }
  blockAddr = currentFile->nextBlock;
  currentFile->nextBlock = -1;
  currentFile = &iNodeTable[blockAddr];
 }
 return 0;
}

variable_types: blockAddr int

variable_types: prevBlockAddr int

variable_types: struct INODE { char fileName[16]; int fileSize; int blockAddres; int nextBlock; int nextFile; } *


repo: pbasista/stc

decompiled_code:
__int64 __fastcall st_slli_bp_simulate_suffix_link_bottom_up(int *a1, int a2, __int64 a3, __int64 a4)
{
int v5; // ecx
__int64 v6; // r12
__int64 result; // rax
int v8[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v8[0] = a2;
if ( a2 )
{
v5 = a2 - 1;
if ( a2 > 0 )
v5 = *(_DWORD *)(*(_QWORD *)(a4 + 24) + 24LL * a2 + 12);
v6 = *a1;
v8[0] = v5;
*a1 = 0;
result = st_slli_bp_go_up(a1, v8, a3, a4);
if ( (_DWORD)result )
{
if ( (_DWORD)result != -1 )
{
fwrite("Error: Suffix link simulation failed permanently!\n", 1uLL, 0x32uLL, stderr);
result = 2LL;
}
}
else
{
*(_DWORD *)(*(_QWORD *)(a4 + 24) + 24 * v6 + 12) = *a1;
}
}
else
{
fwrite("Error: Invalid number of child (0)!\n", 1uLL, 0x24uLL, stderr);
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int *a1, int a2, __int64 a3, __int64 a4)
{
int v5; // ecx
__int64 v6; // r12
__int64 result; // rax
int v8[7]; // [rsp+Ch] [rbp-1Ch] BYREF

v8[0] = a2;
if ( a2 )
{
v5 = a2 - 1;
if ( a2 > 0 )
v5 = *(_DWORD *)(*(_QWORD *)(a4 + 24) + 24LL * a2 + 12);
v6 = *a1;
v8[0] = v5;
*a1 = 0;
result = st_slli_bp_go_up(a1, v8, a3, a4);
if ( (_DWORD)result )
{
if ( (_DWORD)result != -1 )
{
fwrite("Error: Suffix link simulation failed permanently!\n", 1uLL, 0x32uLL, stderr);
result = 2LL;
}
}
else
{
*(_DWORD *)(*(_QWORD *)(a4 + 24) + 24 * v6 + 12) = *a1;
}
}
else
{
fwrite("Error: Invalid number of child (0)!\n", 1uLL, 0x24uLL, stderr);
result = 1LL;
}
return result;
}

original_code:
int st_slli_bp_simulate_suffix_link_bottom_up (signed_integral_type *parent,
  signed_integral_type child,
  unsigned_integral_type target_depth,
  suffix_tree_slli_bp *stree) {
 signed_integral_type starting_from = (*parent);
 int tmp = 0;
 if (child == 0) {
  fprintf(stderr, "Error: Invalid number of child (0)!\n");
  return (1);
 } else if (child > 0) {
  child = stree->tbranch[child].suffix_link;
 } else {
  child = child - 1;
 }
 (*parent) = 0;
 if ((tmp = st_slli_bp_go_up(parent, &child, target_depth,
     stree)) == 0) {
  stree->tbranch[starting_from].suffix_link = (*parent);
  return (0);
 } else if (tmp == (-1)) {
  return (-1);
 } else {
  fprintf(stderr, "Error: Suffix link simulation "
    "failed permanently!\n");
  return (2);
 }
}

canonical_original_code:
int st_slli_bp_simulate_suffix_link_bottom_up(int *parent, int child, unsigned int target_depth, struct suffix_tree_slli_bp_struct *stree) {
 int starting_from = (*parent);
 int tmp = 0;
 if (child == 0) {
  fprintf(stderr, "Error: Invalid number of child (0)!\n");
  return (1);
 } else if (child > 0) {
  child = stree->tbranch[child].suffix_link;
 } else {
  child = child - 1;
 }
 (*parent) = 0;
 if ((tmp = st_slli_bp_go_up(parent, &child, target_depth,
     stree)) == 0) {
  stree->tbranch[starting_from].suffix_link = (*parent);
  return (0);
 } else if (tmp == (-1)) {
  return (-1);
 } else {
  fprintf(stderr, "Error: Suffix link simulation "
    "failed permanently!\n");
  return (2);
 }
}

variable_types:
parent
int *

variable_types: child int

variable_types: struct suffix_tree_slli_bp_struct { unsigned long lr_size; unsigned long br_size; struct leaf_record_slli_bp_struct { int parent; int next_brother; } *tleaf; struct branch_record_slli_bp_struct { int parent; int first_child; int branch_brother; int suffix_link; unsigned int depth; unsigned int head_position; } *tbranch; unsigned long branching_nodes; unsigned long tbranch_size; unsigned long tbsize_increase; } *

variable_types: tmp int

user_defined_types: struct suffix_tree_slli_bp_struct { unsigned long lr_size; unsigned long br_size; struct leaf_record_slli_bp_struct *tleaf; struct branch_record_slli_bp_struct *tbranch; unsigned long branching_nodes; unsigned long tbranch_size; unsigned long tbsize_increase; }