rcexpr

decompiled_code:
void __fastcall rcexpr(int *a1, __int64 a2, const char *a3)
{
__int64 v3; // rax
int v5; // esi
const char *v6; // rdx
const char *v7; // rax
__int64 v8; // rsi
const char *v9; // rdi
unsigned int v10; // er12
const char *v11; // rax
unsigned int v12; // er13
__int64 v13; // rdi
__int64 v14; // rsi
unsigned int v15; // er12
__int64 v16; // rdi

if ( a1 )
{
v3 = *a1;
v5 = *a1;
if ( (*((_BYTE *)&opdope + 4 * v3) & 2) != 0 )
{
lvalexp(a1, v3);
}
else
{
switch ( (int)v3 )
{
case 16:
v10 = pushargs(*((_QWORD *)a1 + 2), v3);
rcexpr(*((_QWORD *)a1 + 1));
printf("\tshl\t$2,%%eax\n");
printf("\tcall\t*%%eax\n");
v8 = v10;
v9 = "\tadd\t$%d,%%esp\n";
if ( v10 )
goto LABEL_11;
return;
case 21:
rcexpr(*((_QWORD *)a1 + 1));
printf("\ttest\t%%eax,%%eax\n");
printf("\tsete\t%%al\n");
printf("\tmovzb\t%%al,%%eax\n");
return;
case 22:
rcexpr(*((_QWORD *)a1 + 1));
printf("\tneg\t%%eax\n");
return;
case 23:
v11 = *(const char **)(*((_QWORD *)a1 + 1) + 8LL);
v6 = (const char *)*(unsigned int *)v11;
if ( (_DWORD)v6 == 2 )
{
printf("\tmov\t$_%s,%%eax\n", v11 + 24);
}
else
{
if ( (_DWORD)v6 != 1 )
goto LABEL_6;
printf("\tlea\t%d(%%ebp),%%eax\n", *((unsigned int *)v11 + 1));
}
goto LABEL_14;
case 24:
rcexpr(*((_QWORD *)a1 + 1));
printf("\tmov\t(,%%eax,4),%%eax\n");
return;
case 25:
v12 = isn;
v13 = *((_QWORD *)a1 + 1);
v14 = (unsigned int)isn++;
cbranch(v13, v14, 0LL);
rcexpr(*(_QWORD *)(*((_QWORD *)a1 + 2) + 8LL));
v15 = isn;
v16 = (unsigned int)isn++;
jump(v16);
label(v12);
rcexpr(*(_QWORD *)(*((_QWORD *)a1 + 2) + 16LL));
label(v15);
return;
case 30:
binary(a1, v3);
pop("ebx");
printf("\tadd\t%%ebx,%%eax\n");
return;
case 31:
binary(a1, v3);
printf("\tmov\t%%eax,%%ebx\n");
pop("eax");
printf("\tsub\t%%ebx,%%eax\n");
return;
case 32:
binary(a1, v3);
printf("\tmov\t%%eax,%%ebx\n");
pop("eax");
printf("\txor\t%%edx,%%edx\n");
printf("\tdiv\t%%ebx\n");
printf("\tmov\t%%edx,%%eax\n");
return;
case 33:
binary(a1, v3);
pop("ebx");
printf("\tmul\t%%ebx\n");
return;
case 34:
binary(a1, v3);
printf("\tmov\t%%eax,%%ebx\n");
pop("eax");
printf("\txor\t%%edx,%%edx\n");
printf("\tdiv\t%%ebx\n");
return;
case 35:
binary(a1, v3);
pop("ebx");
printf("\tor\t%%ebx,%%eax\n");
return;
case 36:
binary(a1, v3);
pop("ebx");
printf("\tand\t%%ebx,%%eax\n");
return;
case 37:
binary(a1, v3);
printf("\tmov\t%%eax,%%ecx\n");
pop("eax");
printf("\tshl\t%%cl,%%eax\n");
return;
case 38:
binary(a1, v3);
printf("\tmov\t%%eax,%%ecx\n");
pop("eax");
printf("\tshr\t%%cl,%%eax\n");
return;
case 39:
case 40:
case 41:
case 42:
case 43:
case 44:
binary(a1, v3);
pop("ebx");
printf("\tcmp\t%%eax,%%ebx\n");
switch ( *a1 )
{
case '\'':
printf("\tsete\t%%al\n");
break;
case '(':
printf("\tsetne\t%%al\n");
break;
case ')':
printf("\tsetle\t%%al\n");
break;
case '*':
printf("\tsetl\t%%al\n");
break;
case '+':
printf("\tsetge\t%%al\n");
break;
case ',':
printf("\tsetg\t%%al\n");
break;
default:
break;
}
printf("\tmovzb\t%%al,%%eax\n");
return;
case 65:
printf("\tmov\t$%d,%%eax\n", (unsigned int)a1[1]);
return;
case 66:
printf("\tmov\t$L%d,%%eax\n", (unsigned int)a1[1]);
LABEL_14:
printf("\tshr\t$2,%%eax\n");
return;
case 67:
v7 = (const char *)*((_QWORD *)a1 + 1);
v6 = (const char *)*(unsigned int *)v7;
if ( (_DWORD)v6 == 2 )
{
printf("\tmov\t_%s,%%eax\n", v7 + 24);
}
else if ( (_DWORD)v6 == 1 )
{
v8 = *((unsigned int *)v7 + 1);
v9 = "\tmov\t%d(%%ebp),%%eax\n";
LABEL_11:
printf(v9, v8);
}
else
{
LABEL_6:
error((int)"Storage class", v5, v6);
}
break;
default:
error((int)"Can't print tree (op: %d)", v3, a3);
goto LABEL_6;
}
}
}
}

canonical_decompiled_code:
void __fastcall func2(int *a1, __int64 a2, const char *a3)
{
__int64 v3; // rax
int v5; // esi
const char *v6; // rdx
const char *v7; // rax
__int64 v8; // rsi
const char *v9; // rdi
unsigned int v10; // er12
const char *v11; // rax
unsigned int v12; // er13
__int64 v13; // rdi
__int64 v14; // rsi
unsigned int v15; // er12
__int64 v16; // rdi

if ( a1 )
{
v3 = *a1;
v5 = *a1;
if ( (*((_BYTE *)&opdope + 4 * v3) & 2) != 0 )
{
func5(a1, v3);
}
else
{
switch ( (int)v3 )
{
case 16:
v10 = func4(*((_QWORD *)a1 + 2), v3);
func2(*((_QWORD *)a1 + 1));
printf("\tshl\t$2,%%eax\n");
printf("\tcall\t*%%eax\n");
v8 = v10;
v9 = "\tadd\t$%d,%%esp\n";
if ( v10 )
goto LABEL_11;
return;
case 21:
func2(*((_QWORD *)a1 + 1));
printf("\ttest\t%%eax,%%eax\n");
printf("\tsete\t%%al\n");
printf("\tmovzb\t%%al,%%eax\n");
return;
case 22:
func2(*((_QWORD *)a1 + 1));
printf("\tneg\t%%eax\n");
return;
case 23:
v11 = *(const char **)(*((_QWORD *)a1 + 1) + 8LL);
v6 = (const char *)*(unsigned int *)v11;
if ( (_DWORD)v6 == 2 )
{
printf("\tmov\t$_%s,%%eax\n", v11 + 24);
}
else
{
if ( (_DWORD)v6 != 1 )
goto LABEL_6;
printf("\tlea\t%d(%%ebp),%%eax\n", *((unsigned int *)v11 + 1));
}
goto LABEL_14;
case 24:
func2(*((_QWORD *)a1 + 1));
printf("\tmov\t(,%%eax,4),%%eax\n");
return;
case 25:
v12 = isn;
v13 = *((_QWORD *)a1 + 1);
v14 = (unsigned int)isn++;
cbranch(v13, v14, 0LL);
func2(*(_QWORD *)(*((_QWORD *)a1 + 2) + 8LL));
v15 = isn;
v16 = (unsigned int)isn++;
jump(v16);
label(v12);
func2(*(_QWORD *)(*((_QWORD *)a1 + 2) + 16LL));
label(v15);
return;
case 30:
func3(a1, v3);
func1("ebx");
printf("\tadd\t%%ebx,%%eax\n");
return;
case 31:
func3(a1, v3);
printf("\tmov\t%%eax,%%ebx\n");
func1("eax");
printf("\tsub\t%%ebx,%%eax\n");
return;
case 32:
func3(a1, v3);
printf("\tmov\t%%eax,%%ebx\n");
func1("eax");
printf("\txor\t%%edx,%%edx\n");
printf("\tdiv\t%%ebx\n");
printf("\tmov\t%%edx,%%eax\n");
return;
case 33:
func3(a1, v3);
func1("ebx");
printf("\tmul\t%%ebx\n");
return;
case 34:
func3(a1, v3);
printf("\tmov\t%%eax,%%ebx\n");
func1("eax");
printf("\txor\t%%edx,%%edx\n");
printf("\tdiv\t%%ebx\n");
return;
case 35:
func3(a1, v3);
func1("ebx");
printf("\tor\t%%ebx,%%eax\n");
return;
case 36:
func3(a1, v3);
func1("ebx");
printf("\tand\t%%ebx,%%eax\n");
return;
case 37:
func3(a1, v3);
printf("\tmov\t%%eax,%%ecx\n");
func1("eax");
printf("\tshl\t%%cl,%%eax\n");
return;
case 38:
func3(a1, v3);
printf("\tmov\t%%eax,%%ecx\n");
func1("eax");
printf("\tshr\t%%cl,%%eax\n");
return;
case 39:
case 40:
case 41:
case 42:
case 43:
case 44:
func3(a1, v3);
func1("ebx");
printf("\tcmp\t%%eax,%%ebx\n");
switch ( *a1 )
{
case '\'':
printf("\tsete\t%%al\n");
break;
case '(':
printf("\tsetne\t%%al\n");
break;
case ')':
printf("\tsetle\t%%al\n");
break;
case '*':
printf("\tsetl\t%%al\n");
break;
case '+':
printf("\tsetge\t%%al\n");
break;
case ',':
printf("\tsetg\t%%al\n");
break;
default:
break;
}
printf("\tmovzb\t%%al,%%eax\n");
return;
case 65:
printf("\tmov\t$%d,%%eax\n", (unsigned int)a1[1]);
return;
case 66:
printf("\tmov\t$L%d,%%eax\n", (unsigned int)a1[1]);
LABEL_14:
printf("\tshr\t$2,%%eax\n");
return;
case 67:
v7 = (const char *)*((_QWORD *)a1 + 1);
v6 = (const char *)*(unsigned int *)v7;
if ( (_DWORD)v6 == 2 )
{
printf("\tmov\t_%s,%%eax\n", v7 + 24);
}
else if ( (_DWORD)v6 == 1 )
{
v8 = *((unsigned int *)v7 + 1);
v9 = "\tmov\t%d(%%ebp),%%eax\n";
LABEL_11:
printf(v9, v8);
}
else
{
LABEL_6:
error((int)"Storage class", v5, v6);
}
break;
default:
error((int)"Can't print tree (op: %d)", v3, a3);
goto LABEL_6;
}
}
}
}

original_code:
void
rcexpr(struct tnode *tr)
{
 int o1, o2;
 int stk;
 struct hshtab *bs;
 if (tr == ((void *)0))
  return;
 if (opdope[tr->op]&02) {
  lvalexp(tr);
  return;
 }
 switch (tr->op) {
 case 65:
  printf("\tmov\t$%d,%%eax\n", tr->value);
  return;
 case 66:
  printf("\tmov\t$L%d,%%eax\n", tr->value);
  printf("\tshr\t$2,%%eax\n");
  return;
 case 67:
  bs = (struct hshtab *) tr->tr1;
  if (bs->class == 2)
   printf("\tmov\t_%s,%%eax\n", bs->name);
  else if (bs->class == 1)
   printf("\tmov\t%d(%%ebp),%%eax\n", bs->offset);
  else
   goto classerror;
  return;
 case 16:
  stk = pushargs(tr->tr2);
  rcexpr(tr->tr1);
  printf("\tshl\t$2,%%eax\n");
  printf("\tcall\t*%%eax\n");
  if (stk)
   printf("\tadd\t$%d,%%esp\n",stk);
  return;
 case 23:
  bs = (struct hshtab *) tr->tr1->tr1;
  if (bs->class == 2) {
   printf("\tmov\t$_%s,%%eax\n", bs->name);
   printf("\tshr\t$2,%%eax\n");
  } else if (bs->class == 1) {
   printf("\tlea\t%d(%%ebp),%%eax\n", bs->offset);
   printf("\tshr\t$2,%%eax\n");
  } else
   goto classerror;
  return;
 case 24:
  rcexpr(tr->tr1);
  printf("\tmov\t(,%%eax,4),%%eax\n");
  return;
 case 30:
  binary(tr);
  pop("ebx");
  printf("\tadd\t%%ebx,%%eax\n");
  return;
 case 31:
  binary(tr);
  printf("\tmov\t%%eax,%%ebx\n");
  pop("eax");
  printf("\tsub\t%%ebx,%%eax\n");
  return;
 case 33:
  binary(tr);
  pop("ebx");
  printf("\tmul\t%%ebx\n");
  return;
 case 34:
  binary(tr);
  printf("\tmov\t%%eax,%%ebx\n");
  pop("eax");
  printf("\txor\t%%edx,%%edx\n");
  printf("\tdiv\t%%ebx\n");
  return;
 case 32:
  binary(tr);
  printf("\tmov\t%%eax,%%ebx\n");
  pop("eax");
  printf("\txor\t%%edx,%%edx\n");
  printf("\tdiv\t%%ebx\n");
  printf("\tmov\t%%edx,%%eax\n");
  return;
 case 36:
  binary(tr);
  pop("ebx");
  printf("\tand\t%%ebx,%%eax\n");
  return;
 case 35:
  binary(tr);
  pop("ebx");
  printf("\tor\t%%ebx,%%eax\n");
  return;
 case 37:
  binary(tr);
  printf("\tmov\t%%eax,%%ecx\n");
  pop("eax");
  printf("\tshl\t%%cl,%%eax\n");
  return;
 case 38:
  binary(tr);
  printf("\tmov\t%%eax,%%ecx\n");
  pop("eax");
  printf("\tshr\t%%cl,%%eax\n");
  return;
 case 39:
 case 40:
 case 42:
 case 41:
 case 44:
 case 43:
  binary(tr);
  pop("ebx");
  printf("\tcmp\t%%eax,%%ebx\n");
  switch (tr->op) {
  case 39:
   printf("\tsete\t%%al\n");
   break;
  case 40:
   printf("\tsetne\t%%al\n");
   break;
  case 42:
   printf("\tsetl\t%%al\n");
   break;
  case 41:
   printf("\tsetle\t%%al\n");
   break;
  case 44:
   printf("\tsetg\t%%al\n");
   break;
  case 43:
   printf("\tsetge\t%%al\n");
   break;
  }
  printf("\tmovzb\t%%al,%%eax\n");
  return;
 case 21:
  rcexpr(tr->tr1);
  printf("\ttest\t%%eax,%%eax\n");
  printf("\tsete\t%%al\n");
  printf("\tmovzb\t%%al,%%eax\n");
  return;
 case 22:
  rcexpr(tr->tr1);
  printf("\tneg\t%%eax\n");
  return;
 case 25:
  cbranch(tr->tr1, o1=isn++, 0);
  rcexpr(tr->tr2->tr1);
  jump(o2 = isn++);
  label(o1);
  rcexpr(tr->tr2->tr2);
  label(o2);
  return;
 default:
  error("Can't print tree (op: %d)", tr->op);
 }
classerror:
 error("Storage class");
}

canonical_original_code:
void rcexpr(struct tnode *tr)
{
 int o1; int o2;
 int stk;
 struct hshtab *bs;
 if (tr == ((void *)0))
  return;
 if (opdope[tr->op]&02) {
  lvalexp(tr);
  return;
 }
 switch (tr->op) {
 case 65:
  printf("\tmov\t$%d,%%eax\n", tr->value);
  return;
 case 66:
  printf("\tmov\t$L%d,%%eax\n", tr->value);
  printf("\tshr\t$2,%%eax\n");
  return;
 case 67:
  bs = (struct hshtab *) tr->tr1;
  if (bs->class == 2)
   printf("\tmov\t_%s,%%eax\n", bs->name);
  else if (bs->class == 1)
   printf("\tmov\t%d(%%ebp),%%eax\n", bs->offset);
  else
   goto classerror;
  return;
 case 16:
  stk = pushargs(tr->tr2);
  rcexpr(tr->tr1);
  printf("\tshl\t$2,%%eax\n");
  printf("\tcall\t*%%eax\n");
  if (stk)
   printf("\tadd\t$%d,%%esp\n",stk);
  return;
 case 23:
  bs = (struct hshtab *) tr->tr1->tr1;
  if (bs->class == 2) {
   printf("\tmov\t$_%s,%%eax\n", bs->name);
   printf("\tshr\t$2,%%eax\n");
  } else if (bs->class == 1) {
   printf("\tlea\t%d(%%ebp),%%eax\n", bs->offset);
   printf("\tshr\t$2,%%eax\n");
  } else
   goto classerror;
  return;
 case 24:
  rcexpr(tr->tr1);
  printf("\tmov\t(,%%eax,4),%%eax\n");
  return;
 case 30:
  binary(tr);
  pop("ebx");
  printf("\tadd\t%%ebx,%%eax\n");
  return;
 case 31:
  binary(tr);
  printf("\tmov\t%%eax,%%ebx\n");
  pop("eax");
  printf("\tsub\t%%ebx,%%eax\n");
  return;
 case 33:
  binary(tr);
  pop("ebx");
  printf("\tmul\t%%ebx\n");
  return;
 case 34:
  binary(tr);
  printf("\tmov\t%%eax,%%ebx\n");
  pop("eax");
  printf("\txor\t%%edx,%%edx\n");
  printf("\tdiv\t%%ebx\n");
  return;
 case 32:
  binary(tr);
  printf("\tmov\t%%eax,%%ebx\n");
  pop("eax");
  printf("\txor\t%%edx,%%edx\n");
  printf("\tdiv\t%%ebx\n");
  printf("\tmov\t%%edx,%%eax\n");
  return;
 case 36:
  binary(tr);
  pop("ebx");
  printf("\tand\t%%ebx,%%eax\n");
  return;
 case 35:
  binary(tr);
  pop("ebx");
  printf("\tor\t%%ebx,%%eax\n");
  return;
 case 37:
  binary(tr);
  printf("\tmov\t%%eax,%%ecx\n");
  pop("eax");
  printf("\tshl\t%%cl,%%eax\n");
  return;
 case 38:
  binary(tr);
  printf("\tmov\t%%eax,%%ecx\n");
  pop("eax");
  printf("\tshr\t%%cl,%%eax\n");
  return;
 case 39:
 case 40:
 case 42:
 case 41:
 case 44:
 case 43:
  binary(tr);
  pop("ebx");
  printf("\tcmp\t%%eax,%%ebx\n");
  switch (tr->op) {
  case 39:
   printf("\tsete\t%%al\n");
   break;
  case 40:
   printf("\tsetne\t%%al\n");
   break;
  case 42:
   printf("\tsetl\t%%al\n");
   break;
  case 41:
   printf("\tsetle\t%%al\n");
   break;
  case 44:
   printf("\tsetg\t%%al\n");
   break;
  case 43:
   printf("\tsetge\t%%al\n");
   break;
  }
  printf("\tmovzb\t%%al,%%eax\n");
  return;
 case 21:
  rcexpr(tr->tr1);
  printf("\ttest\t%%eax,%%eax\n");
  printf("\tsete\t%%al\n");
  printf("\tmovzb\t%%al,%%eax\n");
  return;
 case 22:
  rcexpr(tr->tr1);
  printf("\tneg\t%%eax\n");
  return;
 case 25:
  cbranch(tr->tr1, o1=isn++, 0);
  rcexpr(tr->tr2->tr1);
  jump(o2 = isn++);
  label(o1);
  rcexpr(tr->tr2->tr2);
  label(o2);
  return;
 default:
  error("Can't print tree (op: %d)", tr->op);
 }
classerror:
 error("Storage class");
}

variable_types: struct tnode { int op; int value; struct tnode *tr1; struct tnode *tr2; } *

variable_types: stk int

variable_types: struct hshtab { int class; int offset; int dim; struct hshtab *next; char name[-1]; } *


binary

repo: aap/abc

decompiled_code:
void __fastcall binary(__int64 a1, __int64 a2, const char *a3)
{
const char *v3; // rdx

rcexpr(*(int **)(a1 + 8), a2, a3);
push();
rcexpr(*(int **)(a1 + 16), a2, v3);
}

canonical_decompiled_code:
void __fastcall func3(__int64 a1, __int64 a2, const char *a3)
{
const char *v3; // rdx

func2(*(int **)(a1 + 8), a2, a3);
func0();
func2(*(int **)(a1 + 16), a2, v3);
}

original_code:
void
binary(struct tnode *tr)
{
 rcexpr(tr->tr1);
 push();
 rcexpr(tr->tr2);
}

return_type: void

user_defined_types: struct tnode { int op; int value; struct tnode *tr1; struct tnode *tr2; }


pushargs

decompiled_code:
__int64 __fastcall pushargs(__int64 a1, __int64 a2, const char *a3)
{
__int64 v3; // rbp
int v4; // ebx
unsigned int v5; // er12

if ( !a1 )
return 0;
v3 = a1;
v4 = 4;
while ( 1 )
{
v5 = v4;
if ( *(_DWORD *)v3 != 9 )
break;
v4 += 4;
rcexpr(*(int **)(v3 + 16), a2, a3);
push();
v3 = *(_QWORD *)(v3 + 8);
if ( !v3 )
return v5;
}
rcexpr((int *)v3, a2, a3);
push();
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, __int64 a2, const char *a3)
{
__int64 v3; // rbp
int v4; // ebx
unsigned int v5; // er12

if ( !a1 )
return 0;
v3 = a1;
v4 = 4;
while ( 1 )
{
v5 = v4;
if ( *(_DWORD *)v3 != 9 )
break;
v4 += 4;
func2(*(int **)(v3 + 16), a2, a3);
func0();
v3 = *(_QWORD *)(v3 + 8);
if ( !v3 )
return v5;
}
func2((int *)v3, a2, a3);
func0();
return v5;
}

original_code:
int
pushargs(struct tnode *tr)
{
 int stk;
 if (tr == ((void *)0))
  return 0;
 if (tr->op == 9) {
  rcexpr(tr->tr2);
  push();
  stk = pushargs(tr->tr1);
  return stk+4;
 }
 rcexpr(tr);
 push();
 return 4;
}

return_type: int


lvalexp

decompiled_code:
void __fastcall lvalexp(__int64 a1, __int64 a2, const char *a3)
{
int v4; // eax
__int64 v5; // rax
__int64 v6; // rdi
const char *v7; // rsi
__int64 v8; // rdx
int *v9; // rax
const char *v10; // rdx
__int64 v11; // rax
const char *v12; // rdx
const char *v13; // rsi
char v14[88]; // [rsp+0h] [rbp-58h] BYREF

v4 = *(_DWORD *)a1;
if ( *(_DWORD *)a1 == 49 )
{
rcexpr(*(int **)(a1 + 16), a2, a3);
v11 = *(_QWORD *)(a1 + 8);
if ( *(_DWORD *)v11 == 24 )
{
push();
rcexpr(*(int **)(*(_QWORD *)(a1 + 8) + 8LL), a2, v12);
pop("ebx");
printf("\tmov\t%%ebx,(,%%eax,4)\n");
return;
}
a2 = *(_QWORD *)(v11 + 8);
if ( *(_DWORD *)a2 == 2 )
{
printf("\tmov\t%%eax,_%s\n", (const char *)(a2 + 24));
return;
}
if ( *(_DWORD *)a2 == 1 )
{
printf("\tmov\t%%eax,%d(%%ebp)\n", *(unsigned int *)(a2 + 4));
return;
}
goto LABEL_14;
}
if ( v4 > 49 )
{
a3 = (const char *)(unsigned int)(v4 - 50);
if ( (unsigned int)a3 <= 0xE )
{
v8 = *(_QWORD *)(a1 + 8);
*(_DWORD *)a1 = v4 - 20;
v9 = (int *)block(49LL, 0LL, v8, a1);
rcexpr(v9, 0LL, v10);
return;
}
goto LABEL_14;
}
if ( (unsigned int)(v4 - 17) > 3 )
{
LABEL_14:
error((int)"Storage class", a2, a3);
return;
}
v5 = *(_QWORD *)(a1 + 8);
v6 = *(_QWORD *)(v5 + 8);
if ( *(_DWORD *)v5 == 24 )
{
rcexpr((int *)v6, a2, a3);
printf("\tmov\t%%eax,%%ebx\n");
sprintf(v14, "(,%%ebx,4)");
}
else
{
if ( *(_DWORD *)v6 != 2 )
{
if ( *(_DWORD *)v6 == 1 )
{
sprintf(v14, "%d(%%ebp)", *(unsigned int *)(v6 + 4));
goto LABEL_8;
}
goto LABEL_14;
}
sprintf(v14, "_%s", (const char *)(v6 + 24));
}
LABEL_8:
if ( (unsigned int)(*(_DWORD *)a1 - 17) > 1 )
{
printf("\tmov\t%s,%%eax\n", v14);
v13 = "decl";
if ( *(_DWORD *)a1 != 19 )
v13 = "incl";
printf("\t%s\t%s\n", v13, v14);
}
else
{
v7 = "decl";
if ( *(_DWORD *)a1 != 17 )
v7 = "incl";
printf("\t%s\t%s\n", v7, v14);
printf("\tmov\t%s,%%eax\n", v14);
}
}

canonical_decompiled_code:
void __fastcall func5(__int64 a1, __int64 a2, const char *a3)
{
int v4; // eax
__int64 v5; // rax
__int64 v6; // rdi
const char *v7; // rsi
__int64 v8; // rdx
int *v9; // rax
const char *v10; // rdx
__int64 v11; // rax
const char *v12; // rdx
const char *v13; // rsi
char v14[88]; // [rsp+0h] [rbp-58h] BYREF

v4 = *(_DWORD *)a1;
if ( *(_DWORD *)a1 == 49 )
{
func2(*(int **)(a1 + 16), a2, a3);
v11 = *(_QWORD *)(a1 + 8);
if ( *(_DWORD *)v11 == 24 )
{
func0();
func2(*(int **)(*(_QWORD *)(a1 + 8) + 8LL), a2, v12);
func1("ebx");
printf("\tmov\t%%ebx,(,%%eax,4)\n");
return;
}
a2 = *(_QWORD *)(v11 + 8);
if ( *(_DWORD *)a2 == 2 )
{
printf("\tmov\t%%eax,_%s\n", (const char *)(a2 + 24));
return;
}
if ( *(_DWORD *)a2 == 1 )
{
printf("\tmov\t%%eax,%d(%%ebp)\n", *(unsigned int *)(a2 + 4));
return;
}
goto LABEL_14;
}
if ( v4 > 49 )
{
a3 = (const char *)(unsigned int)(v4 - 50);
if ( (unsigned int)a3 <= 0xE )
{
v8 = *(_QWORD *)(a1 + 8);
*(_DWORD *)a1 = v4 - 20;
v9 = (int *)block(49LL, 0LL, v8, a1);
func2(v9, 0LL, v10);
return;
}
goto LABEL_14;
}
if ( (unsigned int)(v4 - 17) > 3 )
{
LABEL_14:
error((int)"Storage class", a2, a3);
return;
}
v5 = *(_QWORD *)(a1 + 8);
v6 = *(_QWORD *)(v5 + 8);
if ( *(_DWORD *)v5 == 24 )
{
func2((int *)v6, a2, a3);
printf("\tmov\t%%eax,%%ebx\n");
sprintf(v14, "(,%%ebx,4)");
}
else
{
if ( *(_DWORD *)v6 != 2 )
{
if ( *(_DWORD *)v6 == 1 )
{
sprintf(v14, "%d(%%ebp)", *(unsigned int *)(v6 + 4));
goto LABEL_8;
}
goto LABEL_14;
}
sprintf(v14, "_%s", (const char *)(v6 + 24));
}
LABEL_8:
if ( (unsigned int)(*(_DWORD *)a1 - 17) > 1 )
{
printf("\tmov\t%s,%%eax\n", v14);
v13 = "decl";
if ( *(_DWORD *)a1 != 19 )
v13 = "incl";
printf("\t%s\t%s\n", v13, v14);
}
else
{
v7 = "decl";
if ( *(_DWORD *)a1 != 17 )
v7 = "incl";
printf("\t%s\t%s\n", v7, v14);
printf("\tmov\t%s,%%eax\n", v14);
}
}

original_code:
void
lvalexp(struct tnode *tr)
{
 struct hshtab *bs;
 char memloc[64];
 switch (tr->op) {
 case 17:
 case 18:
 case 19:
 case 20:
  if (tr->tr1->op == 24) {
   rcexpr(tr->tr1->tr1);
   printf("\tmov\t%%eax,%%ebx\n");
   sprintf(memloc,"(,%%ebx,4)");
  } else {
   bs = (struct hshtab *) tr->tr1->tr1;
   if (bs->class == 2)
    sprintf(memloc,"_%s", bs->name);
   else if (bs->class == 1)
    sprintf(memloc,"%d(%%ebp)", bs->offset);
   else
    goto classerror;
  }
  if (tr->op == 17 || tr->op == 18) {
   printf("\t%s\t%s\n", tr->op == 17 ? "decl" : "incl",
          memloc);
   printf("\tmov\t%s,%%eax\n", memloc);
  } else {
   printf("\tmov\t%s,%%eax\n", memloc);
   printf("\t%s\t%s\n", tr->op == 19 ? "decl" : "incl",
          memloc);
  }
  return;
 case 49:
  rcexpr(tr->tr2);
  if (tr->tr1->op == 24) {
   push();
   rcexpr(tr->tr1->tr1);
   pop("ebx");
   printf("\tmov\t%%ebx,(,%%eax,4)\n");
  } else {
   bs = (struct hshtab *) tr->tr1->tr1;
   if (bs->class == 2)
    printf("\tmov\t%%eax,_%s\n", bs->name);
   else if (bs->class == 1)
    printf("\tmov\t%%eax,%d(%%ebp)\n", bs->offset);
   else
    goto classerror;
  }
  return;
 case 50:
 case 51:
 case 52:
 case 53:
 case 54:
 case 55:
 case 56:
 case 57:
 case 58:
 case 59:
 case 60:
 case 61:
 case 62:
 case 63:
 case 64:
  tr->op -= 50 -30;
  rcexpr(block(49,0,tr->tr1,tr));
  return;
 }
classerror:
 error("Storage class");
}

variable_types:
memloc
char [64]

user_defined_types: struct hshtab { int class; int offset; int dim; struct hshtab *next; char name[-1]; }


repo: aarzilli/minstrel

decompiled_code:
__int64 __fastcall queue_could_be_prev(int a1)
{
__int64 v1; // rdi
__int64 result; // rax

v1 = 16LL * a1;
result = queue[v1];
if ( (_BYTE)result )
result = queue[v1 + 1];
return result;
}

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

v1 = 16LL * a1;
result = queue[v1];
if ( (_BYTE)result )
result = queue[v1 + 1];
return result;
}

original_code:
static _Bool queue_could_be_prev(int idx) {
 if (!queue[idx].occupied) return 0;
 if (!queue[idx].played) return 0;
 return 1;
}

variable_types: idx int


decompiled_code:
__int64 clear_screen()
{
return putctlcod("cl", stdout);
}

canonical_decompiled_code:
__int64 func2()
{
return putctlcod("cl", stdout);
}

original_code:
static void clear_screen(void) {
 putctlcod("cl", stdout);
}

canonical_original_code:
static void clear_screen() {
 putctlcod("cl", stdout);
}


decompiled_code:
void queue_init()
{
bzero(queue, 0x8000uLL);
queue_position = 0;
queue_currently_playing_idx = -1;
}

canonical_decompiled_code:
void func3()
{
bzero(queue, 0x8000uLL);
queue_position = 0;
queue_currently_playing_idx = -1;
}

original_code:
void queue_init(void) {
 bzero(queue, sizeof(queue));
 queue_position = 0;
 queue_currently_playing_idx = -1;
}

canonical_original_code:
void queue_init() {
 bzero(queue, sizeof(queue));
 queue_position = 0;
 queue_currently_playing_idx = -1;
}


decompiled_code:
size_t __fastcall queue_append(__int64 a1)
{
size_t result; // rax
__int64 v2; // rdx

if ( queue_position > 2047 )
return fwrite("can not add anything anymore\n", 1uLL, 0x1DuLL, stderr);
result = (unsigned int)(queue_position + 1);
v2 = 16LL * queue_position++;
*(_WORD *)&queue[v2] = 1;
*(_QWORD *)&queue[v2 + 8] = a1;
return result;
}

canonical_decompiled_code:
size_t __fastcall func4(__int64 a1)
{
size_t result; // rax
__int64 v2; // rdx

if ( queue_position > 2047 )
return fwrite("can not add anything anymore\n", 1uLL, 0x1DuLL, stderr);
result = (unsigned int)(queue_position + 1);
v2 = 16LL * queue_position++;
*(_WORD *)&queue[v2] = 1;
*(_QWORD *)&queue[v2 + 8] = a1;
return result;
}

original_code:
void queue_append(int64_t id) {
 if (queue_position >= 2048) {
  fprintf(stderr, "can not add anything anymore\n");
  return;
 }
 queue[queue_position].occupied = 1;
 queue[queue_position].played = 0;
 queue[queue_position].id = id;
 ++queue_position;
}

canonical_original_code:
void queue_append(long id) {
 if (queue_position >= 2048) {
  fprintf(stderr, "can not add anything anymore\n");
  return;
 }
 queue[queue_position].occupied = 1;
 queue[queue_position].played = 0;
 queue[queue_position].id = id;
 ++queue_position;
}

variable_types: id long


decompiled_code:
unsigned __int8 *queue_currently_playing()
{
return &queue[16 * queue_currently_playing_idx];
}

canonical_decompiled_code:
unsigned __int8 *func5()
{
return &queue[16 * queue_currently_playing_idx];
}

original_code:
struct item *queue_currently_playing(void) {
 return queue+queue_currently_playing_idx;
}

canonical_original_code:
struct item *queue_currently_playing() {
 return queue+queue_currently_playing_idx;
}

return_type: struct item { _Bool occupied; _Bool played; long id; } *


decompiled_code:
__int64 queue_to_prev()
{
unsigned int v0; // er8
int v1; // edx

v0 = queue_could_be_prev((queue_currently_playing_idx - 1) % 2048);
if ( (_BYTE)v0 )
{
queue_currently_playing()[1] = 0;
queue_currently_playing_idx = v1;
}
return v0;
}

canonical_decompiled_code:
__int64 func10()
{
unsigned int v0; // er8
int v1; // edx

v0 = func0((queue_currently_playing_idx - 1) % 2048);
if ( (_BYTE)v0 )
{
func5()[1] = 0;
queue_currently_playing_idx = v1;
}
return v0;
}

original_code:
_Bool queue_to_prev(void) {
 int prev_idx = (queue_currently_playing_idx - 1) % 2048;
 if (!queue_could_be_prev(prev_idx)) return 0;
 queue_currently_playing()->played = 0;
 queue_currently_playing_idx = prev_idx;
 return 1;
}

canonical_original_code:
_Bool queue_to_prev() {
 int prev_idx = (queue_currently_playing_idx - 1) % 2048;
 if (!queue_could_be_prev(prev_idx)) return 0;
 queue_currently_playing()->played = 0;
 queue_currently_playing_idx = prev_idx;
 return 1;
}


setaddr

decompiled_code:
int __fastcall setaddr(char *a1)
{
__uid_t v1; // eax

bzero(a1, 0x6EuLL);
*(_WORD *)a1 = 1;
v1 = getuid();
return snprintf(a1 + 2, 0x6BuLL, "/tmp/minstrel.%d", v1);
}

canonical_decompiled_code:
int __fastcall func0(char *a1)
{
__uid_t v1; // eax

bzero(a1, 0x6EuLL);
*(_WORD *)a1 = 1;
v1 = getuid();
return snprintf(a1 + 2, 0x6BuLL, "/tmp/minstrel.%d", v1);
}

original_code:
static void setaddr(struct sockaddr_un *address) {
 bzero(address, sizeof(*address));
 address->sun_family = 1;
 snprintf(address->sun_path, sizeof(address->sun_path) / sizeof(char) - sizeof(char), "/tmp/minstrel.%d", getuid());
}

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; } *


conn

decompiled_code:
__int64 conn()
{
int v0; // eax
unsigned int v1; // er12
unsigned int v2; // eax
int v4; // edi
__int64 v5[2]; // [rsp+0h] [rbp-88h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-78h] BYREF

setaddr((char *)&addr);
v0 = socket(1, 2, 0);
if ( v0 < 0 )
{
perror("Couldn't create a unix domain socket\n");
exit(1);
}
v1 = v0;
if ( connect(v0, &addr, 0x6Eu) )
goto LABEL_6;
v5[0] = 0LL;
v5[1] = 0LL;
v2 = send(v1, v5, 0x10uLL, 0);
if ( v2 != 16 )
{
fprintf(stderr, "Couldn't send handshake %d\n", v2);
LABEL_6:
v4 = v1;
v1 = -1;
close(v4);
}
return v1;
}

canonical_decompiled_code:
__int64 func1()
{
int v0; // eax
unsigned int v1; // er12
unsigned int v2; // eax
int v4; // edi
__int64 v5[2]; // [rsp+0h] [rbp-88h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-78h] BYREF

func0((char *)&addr);
v0 = socket(1, 2, 0);
if ( v0 < 0 )
{
perror("Couldn't create a unix domain socket\n");
exit(1);
}
v1 = v0;
if ( connect(v0, &addr, 0x6Eu) )
goto LABEL_6;
v5[0] = 0LL;
v5[1] = 0LL;
v2 = send(v1, v5, 0x10uLL, 0);
if ( v2 != 16 )
{
fprintf(stderr, "Couldn't send handshake %d\n", v2);
LABEL_6:
v4 = v1;
v1 = -1;
close(v4);
}
return v1;
}

original_code:
int conn(void) {
 struct sockaddr_un address;
 setaddr(&address);
 int fd = socket(1, SOCK_DGRAM, 0);
 if (fd < 0) {
  perror("Couldn't create a unix domain socket\n");
  exit(1);
 }
 if (connect(fd, (struct sockaddr *) &address, sizeof(struct sockaddr_un)) != 0) {
  close(fd);
  return -1;
 }
 int64_t hs[2] = { CMD_HANDSHAKE, 0 };
 int r = send(fd, (void *)hs, sizeof(hs), 0);
 if (r != sizeof(hs)) {
  fprintf(stderr, "Couldn't send handshake %d\n", r);
  close(fd);
  return -1;
 }
 return fd;
}

canonical_original_code:
int conn() {
 struct sockaddr_un address;
 setaddr(&address);
 int fd = socket(1, SOCK_DGRAM, 0);
 if (fd < 0) {
  perror("Couldn't create a unix domain socket\n");
  exit(1);
 }
 if (connect(fd, (struct sockaddr *) &address, sizeof(struct sockaddr_un)) != 0) {
  close(fd);
  return -1;
 }
 long hs[2] = { CMD_HANDSHAKE, 0 };
 int r = send(fd, (void *)hs, sizeof(hs), 0);
 if (r != sizeof(hs)) {
  fprintf(stderr, "Couldn't send handshake %d\n", r);
  close(fd);
  return -1;
 }
 return fd;
}

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }

variable_types: fd int

variable_types:
hs
long [2]

variable_types: r int

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


serve

decompiled_code:
__int64 serve()
{
int v0; // eax
unsigned int v1; // er12
struct sockaddr v3[7]; // [rsp+0h] [rbp-78h] BYREF

setaddr((char *)v3);
unlink(v3[0].sa_data);
v0 = socket(1, 2, 0);
if ( v0 < 0 )
{
perror("Couldn't create a unix domain socket\n");
exit(1);
}
v1 = v0;
if ( bind(v0, v3, 0x6Eu) )
{
perror("Couldn't open control socket");
exit(1);
}
return v1;
}

canonical_decompiled_code:
__int64 func2()
{
int v0; // eax
unsigned int v1; // er12
struct sockaddr v3[7]; // [rsp+0h] [rbp-78h] BYREF

func0((char *)v3);
unlink(v3[0].sa_data);
v0 = socket(1, 2, 0);
if ( v0 < 0 )
{
perror("Couldn't create a unix domain socket\n");
exit(1);
}
v1 = v0;
if ( bind(v0, v3, 0x6Eu) )
{
perror("Couldn't open control socket");
exit(1);
}
return v1;
}

original_code:
int serve(void) {
 struct sockaddr_un address;
 setaddr(&address);
 unlink(address.sun_path);
 int fd = socket(1, SOCK_DGRAM, 0);
 if (fd < 0) {
  perror("Couldn't create a unix domain socket\n");
  exit(1);
 }
 if (bind(fd, (struct sockaddr *) &address, sizeof(address)) != 0) {
  perror("Couldn't open control socket");
  exit(1);
 }
 return fd;
}

canonical_original_code:
int serve() {
 struct sockaddr_un address;
 setaddr(&address);
 unlink(address.sun_path);
 int fd = socket(1, SOCK_DGRAM, 0);
 if (fd < 0) {
  perror("Couldn't create a unix domain socket\n");
  exit(1);
 }
 if (bind(fd, (struct sockaddr *) &address, sizeof(address)) != 0) {
  perror("Couldn't open control socket");
  exit(1);
 }
 return fd;
}

user_defined_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }


decompiled_code:
int __fastcall conn_and_send(void *buf)
{
int result; // eax
int v2; // ebp

result = conn();
if ( result != -1 )
{
v2 = result;
send(result, buf, 0x10uLL, 0);
result = close(v2);
}
return result;
}

canonical_decompiled_code:
int __fastcall func3(void *buf)
{
int result; // eax
int v2; // ebp

result = func1();
if ( result != -1 )
{
v2 = result;
send(result, buf, 0x10uLL, 0);
result = close(v2);
}
return result;
}

original_code:
void conn_and_send(int64_t cmd[2]) {
 int fd = conn();
 if (fd == -1) return;
 send(fd, (void *)cmd, sizeof(int64_t)*2, 0);
 close(fd);
}

canonical_original_code:
void conn_and_send(long cmd[2]) {
 int fd = conn();
 if (fd == -1) return;
 send(fd, (void *)cmd, sizeof(long)*2, 0);
 close(fd);
}

variable_types:
cmd
long [2]


decompiled_code:
ssize_t __fastcall send_add(int a1, __int64 a2)
{
__int64 v3[3]; // [rsp+0h] [rbp-18h] BYREF

v3[1] = a2;
v3[0] = 20LL;
return send(a1, v3, 0x10uLL, 0);
}

canonical_decompiled_code:
ssize_t __fastcall func4(int a1, __int64 a2)
{
__int64 v3[3]; // [rsp+0h] [rbp-18h] BYREF

v3[1] = a2;
v3[0] = 20LL;
return send(a1, v3, 0x10uLL, 0);
}

original_code:
void send_add(int fd, int64_t idx) {
 int64_t cmd[] = { CMD_ADD, idx };
 send(fd, (void *)cmd, sizeof(cmd), 0);
 return;
}

canonical_original_code:
void send_add(int fd, long idx) {
 long cmd[] = { CMD_ADD, idx };
 send(fd, (void *)cmd, sizeof(cmd), 0);
 return;
}

variable_types: idx long

variable_types:
cmd
long []


oomp

decompiled_code:
void __fastcall oomp(__int64 a1)
{
if ( !a1 )
{
perror("Out of memory");
exit(1);
}
}

canonical_decompiled_code:
void __fastcall func0(__int64 a1)
{
if ( !a1 )
{
perror("Out of memory");
exit(1);
}
}

original_code:
void oomp(void *ptr) {
 if (ptr == ((void *)0)) {
  perror("Out of memory");
  exit(1);
 }
}

variable_types:
ptr
void *


strstart

decompiled_code:
bool __fastcall strstart(char *s2, char *s1)
{
size_t v2; // rax

v2 = strlen(s1);
return strncmp(s1, s2, v2) == 0;
}

canonical_decompiled_code:
bool __fastcall func2(char *s2, char *s1)
{
size_t v2; // rax

v2 = strlen(s1);
return strncmp(s1, s2, v2) == 0;
}

original_code:
_Bool strstart(const char *haystack, const char *needle) {
    return strncmp(needle, haystack, strlen(needle)) == 0;
}

canonical_original_code:
_Bool strstart(char *haystack, char *needle) {
    return strncmp(needle, haystack, strlen(needle)) == 0;
}

variable_types:
haystack
char *

variable_types:
needle
char *


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

result = getenv("TERM");
if ( !strcmp(result, "dumb") || !*result )
dumb_terminal = 1;
return result;
}

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

result = getenv("TERM");
if ( !strcmp(result, "dumb") || !*result )
dumb_terminal = 1;
return result;
}

original_code:
void term_init(void) {
 char *termenv = getenv("TERM");
 if ((strcmp(termenv, "dumb") == 0) || strcmp(termenv, "") == 0) {
  dumb_terminal = 1;
 }
}

canonical_original_code:
void term_init() {
 char *termenv = getenv("TERM");
 if ((strcmp(termenv, "dumb") == 0) || strcmp(termenv, "") == 0) {
  dumb_terminal = 1;
 }
}

variable_types:
termenv
char *


checksum

decompiled_code:
__int64 __fastcall checksum(char *a1)
{
__int64 v1; // rax
__int64 i; // r8

v1 = *a1;
for ( i = 0LL; (_BYTE)v1; v1 = *a1 )
{
++a1;
i += v1;
}
return i;
}

original_code:
int64_t checksum(const char *a) {
 int64_t r = 0;
 for (const char *c = a; *c != '\0'; ++c) {
  r += *c;
 }
 return r;
}

canonical_original_code:
long checksum(char *a) {
 long r = 0;
 for (char *c = a; *c != '\0'; ++c) {
  r += *c;
 }
 return r;
}

variable_types:
a
char *

variable_types: r long

variable_types:
c
char *

return_type: long


decompiled_code:
__int64 __fastcall should_autoindex_file(const char *a1)
{
unsigned int v1; // er12
char *v2; // rax
char *v3; // rbp
__int64 v4; // rbx
const __int32_t *v5; // rcx
char *v6; // rax
const char **v7; // rbx

v1 = 0;
v2 = strrchr(a1, 46);
if ( v2 )
{
v3 = strdup(v2 + 1);
oomp(v3);
v4 = *v3;
if ( (_BYTE)v4 )
{
v5 = *_ctype_tolower_loc();
v6 = v3;
do
{
*v6++ = v5[v4];
v4 = *v6;
}
while ( (_BYTE)v4 );
}
v7 = (const char **)&KNOWN_AUDIO_EXTENSIONS;
do
{
if ( !strcmp(*v7, v3) )
{
v1 = 1;
goto LABEL_9;
}
++v7;
}
while ( (const char **)(unsigned int)((_DWORD)&KNOWN_AUDIO_EXTENSIONS + 192) != v7 );
v1 = 0;
LABEL_9:
free(v3);
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1)
{
unsigned int v1; // er12
char *v2; // rax
char *v3; // rbp
__int64 v4; // rbx
const __int32_t *v5; // rcx
char *v6; // rax
const char **v7; // rbx

v1 = 0;
v2 = strrchr(a1, 46);
if ( v2 )
{
v3 = strdup(v2 + 1);
oomp(v3);
v4 = *v3;
if ( (_BYTE)v4 )
{
v5 = *_ctype_tolower_loc();
v6 = v3;
do
{
*v6++ = v5[v4];
v4 = *v6;
}
while ( (_BYTE)v4 );
}
v7 = (const char **)&KNOWN_AUDIO_EXTENSIONS;
do
{
if ( !strcmp(*v7, v3) )
{
v1 = 1;
goto LABEL_9;
}
++v7;
}
while ( (const char **)(unsigned int)((_DWORD)&KNOWN_AUDIO_EXTENSIONS + 192) != v7 );
v1 = 0;
LABEL_9:
free(v3);
}
return v1;
}

original_code:
static _Bool should_autoindex_file(const char *full_file_name) {
 char *dot = strrchr(full_file_name, '.');
 if (dot == ((void *)0)) return 0;
 char *ext = strdup(dot+1);
 oomp(ext);
 for (char *c = ext; *c != '\0'; ++c) {
  *c = tolower(*c);
 }
 _Bool r = 0;
 for (int i = 0; i < sizeof(KNOWN_AUDIO_EXTENSIONS)/sizeof(const char *); ++i) {
  if (strcmp(KNOWN_AUDIO_EXTENSIONS[i], ext) == 0) {
   r = 1;
   break;
  }
 }
 free(ext);
 return r;
}

canonical_original_code:
static _Bool should_autoindex_file(char *full_file_name) {
 char *dot = strrchr(full_file_name, '.');
 if (dot == ((void *)0)) return 0;
 char *ext = strdup(dot+1);
 oomp(ext);
 for (char *c = ext; *c != '\0'; ++c) {
  *c = tolower(*c);
 }
 _Bool r = 0;
 for (int i = 0; i < sizeof(KNOWN_AUDIO_EXTENSIONS)/sizeof(char *); ++i) {
  if (strcmp(KNOWN_AUDIO_EXTENSIONS[i], ext) == 0) {
   r = 1;
   break;
  }
 }
 free(ext);
 return r;
}

variable_types:
dot
char *

variable_types:
ext
char *

variable_types: i int


decompiled_code:
__int64 rating_init()
{
__int64 result; // rax
const char *v1[2]; // [rsp+8h] [rbp-10h] BYREF

rating_db = open_or_create_db("rating");
sqlite3_exec(rating_db, "pragma foreign_keys = on;", 0LL, 0LL, v1);
if ( v1[0]
|| (sqlite3_exec(rating_db, "pragma synchronous = off;", 0LL, 0LL, v1), v1[0])
|| (result = sqlite3_exec(
rating_db,
"CREATE TABLE IF NOT EXISTS rating(filename text primary key, listened integer default 0, added integer default 0);",
0LL,
0LL,
v1),
v1[0]) )
{
fprintf(stderr, "Sqlite error building rating db: %s\n", v1[0]);
sqlite3_free(v1[0]);
sqlite3_close(rating_db);
exit(1);
}
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax
const char *v1[2]; // [rsp+8h] [rbp-10h] BYREF

rating_db = open_or_create_db("rating");
sqlite3_exec(rating_db, "pragma foreign_keys = on;", 0LL, 0LL, v1);
if ( v1[0]
|| (sqlite3_exec(rating_db, "pragma synchronous = off;", 0LL, 0LL, v1), v1[0])
|| (result = sqlite3_exec(
rating_db,
"CREATE TABLE IF NOT EXISTS rating(filename text primary key, listened integer default 0, added integer default 0);",
0LL,
0LL,
v1),
v1[0]) )
{
fprintf(stderr, "Sqlite error building rating db: %s\n", v1[0]);
sqlite3_free(v1[0]);
sqlite3_close(rating_db);
exit(1);
}
return result;
}

original_code:
void rating_init(void) {
 char *errmsg;
 rating_db = open_or_create_db("rating");
 sqlite3_exec(rating_db, "pragma foreign_keys = on;", ((void *)0), ((void *)0), &errmsg);
 if (errmsg != ((void *)0)) goto rating_init_failure;
 sqlite3_exec(rating_db, "pragma synchronous = off;", ((void *)0), ((void *)0), &errmsg);
 if (errmsg != ((void *)0)) goto rating_init_failure;
 sqlite3_exec(rating_db, "CREATE TABLE IF NOT EXISTS rating(filename text primary key, listened integer default 0, added integer default 0);", ((void *)0), ((void *)0), &errmsg);
 if (errmsg != ((void *)0)) goto rating_init_failure;
 return;
rating_init_failure:
 fprintf(stderr, "Sqlite error building rating db: %s\n", errmsg);
 sqlite3_free(errmsg);
 sqlite3_close(rating_db);
 exit(1);
}

canonical_original_code:
void rating_init() {
 char *errmsg;
 rating_db = open_or_create_db("rating");
 sqlite3_exec(rating_db, "pragma foreign_keys = on;", ((void *)0), ((void *)0), &errmsg);
 if (errmsg != ((void *)0)) goto rating_init_failure;
 sqlite3_exec(rating_db, "pragma synchronous = off;", ((void *)0), ((void *)0), &errmsg);
 if (errmsg != ((void *)0)) goto rating_init_failure;
 sqlite3_exec(rating_db, "CREATE TABLE IF NOT EXISTS rating(filename text primary key, listened integer default 0, added integer default 0);", ((void *)0), ((void *)0), &errmsg);
 if (errmsg != ((void *)0)) goto rating_init_failure;
 return;
rating_init_failure:
 fprintf(stderr, "Sqlite error building rating db: %s\n", errmsg);
 sqlite3_free(errmsg);
 sqlite3_close(rating_db);
 exit(1);
}

variable_types:
errmsg
char *


main

canonical_name: main

repo: aashishthite/OSCourse

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
Hash_Init();
Hash_Insert();
if ( Hash_Lookup() )
return 0;
puts("No val returned");
return -1;
}

original_code:
int main()
{
   int element = 255;
    unsigned int key = 127;
    hash_t hs;
    Hash_Init(&hs, 32);
    Hash_Insert(&hs, (void*)&element, key);
    void *val = Hash_Lookup(&hs, key);
    if (val)
 return 0;
    else
 {
  printf("No val returned\n");
  return -1;
 }
}

canonical_original_code:
int main()
{
   int element = 255;
    unsigned int key = 127;
    struct hash_t hs;
    Hash_Init(&hs, 32);
    Hash_Insert(&hs, (void *)&element, key);
    void *val = Hash_Lookup(&hs, key);
    if (val)
 return 0;
    else
 {
  printf("No val returned\n");
  return -1;
 }
}

variable_types: element int

variable_types:
key
unsigned int

variable_types: struct hash_t { unsigned int numBuckets; struct list_t { struct spinlock_t { int state; } lock; struct listItem { struct listItem *next; void *element; unsigned int key; } *head; } **buckets; }

variable_types:
val
void *

user_defined_types: struct hash_t { unsigned int numBuckets; struct list_t **buckets; }

user_defined_types: struct list_t { struct spinlock_t lock; struct listItem *head; }


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

if ( a1 )
{
spinlock_acquire();
*(_QWORD *)(a1 + 8) = 0LL;
result = spinlock_release();
}
return result;
}

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

if ( a1 )
{
spinlock_acquire();
*(_QWORD *)(a1 + 8) = 0LL;
result = spinlock_release();
}
return result;
}

original_code:
void List_Init(list_t *list)
{
 if(list)
 {
  spinlock_acquire(&list->lock);
  list->head = ((void *)0);
  spinlock_release(&list->lock);
 }
}

canonical_original_code:
void List_Init(struct list_t *list)
{
 if(list)
 {
  spinlock_acquire(&list->lock);
  list->head = ((void *)0);
  spinlock_release(&list->lock);
 }
}

variable_types: struct list_t { struct spinlock_t { int state; } lock; struct listItem { struct listItem *next; void *element; unsigned int key; } *head; } *


decompiled_code:
void __fastcall List_Insert(__int64 a1, __int64 a2, int a3)
{
_DWORD *v4; // rax
_QWORD *v5; // rbx
__int64 v6; // rax

if ( a1 )
{
v4 = malloc(0x18uLL);
v4[4] = a3;
v5 = v4;
*((_QWORD *)v4 + 1) = a2;
*(_QWORD *)v4 = 0LL;
spinlock_acquire();
v6 = *(_QWORD *)(a1 + 8);
*(_QWORD *)(a1 + 8) = v5;
*v5 = v6;
spinlock_release();
}
}

canonical_decompiled_code:
void __fastcall func7(__int64 a1, __int64 a2, int a3)
{
_DWORD *v4; // rax
_QWORD *v5; // rbx
__int64 v6; // rax

if ( a1 )
{
v4 = malloc(0x18uLL);
v4[4] = a3;
v5 = v4;
*((_QWORD *)v4 + 1) = a2;
*(_QWORD *)v4 = 0LL;
spinlock_acquire();
v6 = *(_QWORD *)(a1 + 8);
*(_QWORD *)(a1 + 8) = v5;
*v5 = v6;
spinlock_release();
}
}

original_code:
void List_Insert(list_t *list, void *element, unsigned int key)
{
 if(list)
 {
  listItem * newItem = malloc(sizeof(listItem));
  newItem->key = key;
  newItem->element = element;
  newItem->next = ((void *)0);
  spinlock_acquire(&list->lock);
  newItem->next = list->head;
  list->head = newItem;
  spinlock_release(&list->lock);
 }
}

canonical_original_code:
void List_Insert(struct list_t *list, void *element, unsigned int key)
{
 if(list)
 {
  struct listItem *newItem = malloc(sizeof(listItem));
  newItem->key = key;
  newItem->element = element;
  newItem->next = ((void *)0);
  spinlock_acquire(&list->lock);
  newItem->next = list->head;
  list->head = newItem;
  spinlock_release(&list->lock);
 }
}

variable_types:
element
void *

variable_types: struct listItem { struct listItem *next; void *element; unsigned int key; } *


decompiled_code:
void __fastcall List_Delete(__int64 a1, int a2)
{
_DWORD *v3; // rbp
_QWORD *v4; // rax
void *v5; // rdi

if ( a1 )
{
spinlock_acquire();
v3 = *(_DWORD **)(a1 + 8);
if ( v3 )
{
if ( v3[4] == a2 )
{
v5 = *(void **)(a1 + 8);
*(_QWORD *)(a1 + 8) = *(_QWORD *)v3;
free(v5);
}
else
{
while ( 1 )
{
v4 = v3;
v3 = *(_DWORD **)v3;
if ( !v3 )
break;
if ( v3[4] == a2 )
{
*v4 = *(_QWORD *)v3;
spinlock_release();
free(v3);
return;
}
}
}
}
spinlock_release();
}
}

canonical_decompiled_code:
void __fastcall func8(__int64 a1, int a2)
{
_DWORD *v3; // rbp
_QWORD *v4; // rax
void *v5; // rdi

if ( a1 )
{
spinlock_acquire();
v3 = *(_DWORD **)(a1 + 8);
if ( v3 )
{
if ( v3[4] == a2 )
{
v5 = *(void **)(a1 + 8);
*(_QWORD *)(a1 + 8) = *(_QWORD *)v3;
free(v5);
}
else
{
while ( 1 )
{
v4 = v3;
v3 = *(_DWORD **)v3;
if ( !v3 )
break;
if ( v3[4] == a2 )
{
*v4 = *(_QWORD *)v3;
spinlock_release();
free(v3);
return;
}
}
}
}
spinlock_release();
}
}

original_code:
void List_Delete(list_t *list, unsigned int key)
{
 if(list)
 {
  spinlock_acquire(&list->lock);
  listItem* temp = list->head;
  if(!temp)
  {
   spinlock_release(&list->lock);
    return;
  }
  if(temp && temp->key == key)
  {
   list->head = temp->next;
   free(temp);
   spinlock_release(&list->lock);
   return;
  }
  while(temp->next)
  {
   if(temp->next->key == key)
   {
    listItem * toFree = temp->next;
    temp->next = toFree->next;
    spinlock_release(&list->lock);
    free(toFree);
    return;
   }
   temp = temp -> next;
  }
  spinlock_release(&list->lock);
 }
}

canonical_original_code:
void List_Delete(struct list_t *list, unsigned int key)
{
 if(list)
 {
  spinlock_acquire(&list->lock);
  struct listItem *temp = list->head;
  if(!temp)
  {
   spinlock_release(&list->lock);
    return;
  }
  if(temp && temp->key == key)
  {
   list->head = temp->next;
   free(temp);
   spinlock_release(&list->lock);
   return;
  }
  while(temp->next)
  {
   if(temp->next->key == key)
   {
    struct listItem *toFree = temp->next;
    temp->next = toFree->next;
    spinlock_release(&list->lock);
    free(toFree);
    return;
   }
   temp = temp -> next;
  }
  spinlock_release(&list->lock);
 }
}

user_defined_types: struct listItem { struct listItem *next; void *element; unsigned int key; }


decompiled_code:
__int64 __fastcall List_Lookup(__int64 a1, int a2)
{
__int64 result; // rax
__int64 *v3; // rbx

result = 0LL;
if ( a1 )
{
spinlock_acquire();
v3 = *(__int64 **)(a1 + 8);
if ( v3 )
{
while ( *((_DWORD *)v3 + 4) != a2 )
{
v3 = (__int64 *)*v3;
if ( !v3 )
goto LABEL_7;
}
spinlock_release();
result = v3[1];
}
else
{
LABEL_7:
spinlock_release();
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1, int a2)
{
__int64 result; // rax
__int64 *v3; // rbx

result = 0LL;
if ( a1 )
{
spinlock_acquire();
v3 = *(__int64 **)(a1 + 8);
if ( v3 )
{
while ( *((_DWORD *)v3 + 4) != a2 )
{
v3 = (__int64 *)*v3;
if ( !v3 )
goto LABEL_7;
}
spinlock_release();
result = v3[1];
}
else
{
LABEL_7:
spinlock_release();
result = 0LL;
}
}
return result;
}

original_code:
void *List_Lookup(list_t *list, unsigned int key)
{
 if(list)
 {
  listItem * temp;
  spinlock_acquire(&list->lock);
  temp = list->head;
  while(temp)
  {
   if(temp->key == key)
   {
    spinlock_release(&list->lock);
    return temp->element;
   }
   temp = temp->next;
  }
  spinlock_release(&list->lock);
 }
 return ((void *)0);
}

canonical_original_code:
void *List_Lookup(struct list_t *list, unsigned int key)
{
 if(list)
 {
  struct listItem *temp;
  spinlock_acquire(&list->lock);
  temp = list->head;
  while(temp)
  {
   if(temp->key == key)
   {
    spinlock_release(&list->lock);
    return temp->element;
   }
   temp = temp->next;
  }
  spinlock_release(&list->lock);
 }
 return ((void *)0);
}

return_type: void *


original_code:
void Hash_Init(hash_t *hash, int buckets)
{
 if(hash)
 {
  buckets=buckets>0?buckets:1;
  hash->numBuckets = buckets;
  hash->buckets = malloc(buckets* sizeof(list_t*));
  int i;
  for(i =0; i < buckets; ++i)
  {
   hash->buckets[i]=malloc(sizeof(list_t));
   List_Init(hash->buckets[i]);
  }
 }
}

canonical_original_code:
void Hash_Init(struct hash_t *hash, int buckets)
{
 if(hash)
 {
  buckets=buckets>0?buckets:1;
  hash->numBuckets = buckets;
  hash->buckets = malloc(buckets* sizeof(struct list_t *));
  int i;
  for(i =0; i < buckets; ++i)
  {
   hash->buckets[i]=malloc(sizeof(list_t));
   List_Init(hash->buckets[i]);
  }
 }
}

variable_types: struct hash_t { unsigned int numBuckets; struct list_t { struct spinlock_t { int state; } lock; struct listItem { struct listItem *next; void *element; unsigned int key; } *head; } **buckets; } *

variable_types: buckets int


getBucket

decompiled_code:
__int64 __fastcall getBucket(__int64 a1, unsigned int a2)
{
return *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * (a2 % *(_DWORD *)a1));
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, unsigned int a2)
{
return *(_QWORD *)(*(_QWORD *)(a1 + 8) + 8LL * (a2 % *(_DWORD *)a1));
}

original_code:
list_t* getBucket(hash_t * hash, unsigned int key)
{
 return hash->buckets[key%(hash->numBuckets)];
}

canonical_original_code:
struct list_t *getBucket(struct hash_t *hash, unsigned int key)
{
 return hash->buckets[key%(hash->numBuckets)];
}

variable_types: struct hash_t { unsigned int numBuckets; struct list_t **buckets; } *

return_type: struct list_t { struct spinlock_t { int state; } lock; struct listItem { struct listItem *next; void *element; unsigned int key; } *head; } *


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

if ( a1 )
{
getBucket(a1, a3);
result = List_Insert();
}
return result;
}

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

if ( a1 )
{
func7(a1, a3);
result = List_Insert();
}
return result;
}

original_code:
void Hash_Insert(hash_t *hash, void *element, unsigned int key)
{
 if(hash)
 {
  List_Insert(getBucket(hash,key), element, key);
 }
}

canonical_original_code:
void Hash_Insert(struct hash_t *hash, void *element, unsigned int key)
{
 if(hash)
 {
  List_Insert(getBucket(hash,key), element, key);
 }
}


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

if ( a1 )
{
getBucket(a1, a2);
result = List_Delete();
}
return result;
}

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

if ( a1 )
{
func7(a1, a2);
result = List_Delete();
}
return result;
}

original_code:
void Hash_Delete(hash_t *hash, unsigned int key)
{
 if(hash)
 {
  List_Delete(getBucket(hash,key), key);
 }
}

canonical_original_code:
void Hash_Delete(struct hash_t *hash, unsigned int key)
{
 if(hash)
 {
  List_Delete(getBucket(hash,key), key);
 }
}


decompiled_code:
__int64 __fastcall Hash_Lookup(__int64 a1, __int64 a2)
{
if ( !a1 )
return 0LL;
getBucket(a1, a2);
return List_Lookup();
}

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1, __int64 a2)
{
if ( !a1 )
return 0LL;
func7(a1, a2);
return List_Lookup();
}

original_code:
void *Hash_Lookup(hash_t *hash, unsigned int key)
{
 if(hash)
 {
  return List_Lookup(getBucket(hash,key),key);
 }
 return ((void *)0);
}

canonical_original_code:
void *Hash_Lookup(struct hash_t *hash, unsigned int key)
{
 if(hash)
 {
  return List_Lookup(getBucket(hash,key),key);
 }
 return ((void *)0);
}


decompiled_code:
__int64 __fastcall Mem_Init(int a1, unsigned int a2)
{
int v3; // ebx
int v4; // er12
_QWORD *v5; // rax
__int64 v6; // rsi
__int64 result; // rax
int v8; // er15

if ( a1 <= 0 || freeList || sizeMem >= 0 || a2 > 1 )
{
m_error = 4;
result = 0xFFFFFFFFLL;
}
else
{
v3 = a1;
v4 = open("/dev/zero", 2);
if ( a1 % getpagesize() )
{
v8 = getpagesize() + a1;
v3 = v8 - a1 % getpagesize();
}
v5 = mmap(0LL, v3, 3, 2, v4, 0LL);
sizeMem = v3;
startMem = v5;
if ( v5 == (_QWORD *)-1LL )
{
perror("mmap");
result = 0xFFFFFFFFLL;
}
else
{
v6 = (unsigned int)(v3 - 24);
freeList = v5;
v5[1] = 0LL;
*(_DWORD *)v5 = v6;
v5[2] = 0LL;
debugMode = a2;
if ( a2 )
fillMemWithPattern(v5 + 3, v6, 3735928559LL);
close(v4);
result = 0LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, unsigned int a2)
{
int v3; // ebx
int v4; // er12
_QWORD *v5; // rax
__int64 v6; // rsi
__int64 result; // rax
int v8; // er15

if ( a1 <= 0 || freeList || sizeMem >= 0 || a2 > 1 )
{
m_error = 4;
result = 0xFFFFFFFFLL;
}
else
{
v3 = a1;
v4 = open("/dev/zero", 2);
if ( a1 % getpagesize() )
{
v8 = getpagesize() + a1;
v3 = v8 - a1 % getpagesize();
}
v5 = mmap(0LL, v3, 3, 2, v4, 0LL);
sizeMem = v3;
startMem = v5;
if ( v5 == (_QWORD *)-1LL )
{
perror("mmap");
result = 0xFFFFFFFFLL;
}
else
{
v6 = (unsigned int)(v3 - 24);
freeList = v5;
v5[1] = 0LL;
*(_DWORD *)v5 = v6;
v5[2] = 0LL;
debugMode = a2;
if ( a2 )
func7(v5 + 3, v6, 3735928559LL);
close(v4);
result = 0LL;
}
}
return result;
}

original_code:
int Mem_Init(int sizeOfRegion, int debug)
{
 if(sizeOfRegion <= 0 || freeList != ((void *)0) || sizeMem>=0 || (debug!=0 && debug!=1))
 {
  m_error = (4);
  return -1;
 }
 int fd = open("/dev/zero", 02);
 if(sizeOfRegion%getpagesize())
  sizeOfRegion = sizeOfRegion + getpagesize()-(sizeOfRegion%getpagesize());
 void* ptr = mmap(((void *)0), sizeOfRegion, 0x1 | 0x2, 0x02, fd, 0);
 sizeMem = sizeOfRegion;
 startMem = ptr;
 if(ptr == ((void *) -1))
 {
  perror("mmap");
  return -1;
 }
 freeList = ptr;
 freeList->size = sizeOfRegion - sizeof(memNode);
 freeList->next = ((void *)0);
 freeList->prev = ((void *)0);
 debugMode = debug;
 if(debugMode)
 {
  fillMemWithPattern((void*)ptr+sizeof(memNode), freeList->size,0xDEADBEEF);
 }
 close(fd);
 return 0;
}

canonical_original_code:
int Mem_Init(int sizeOfRegion, int debug)
{
 if(sizeOfRegion <= 0 || freeList != ((void *)0) || sizeMem>=0 || (debug!=0 && debug!=1))
 {
  m_error = (4);
  return -1;
 }
 int fd = open("/dev/zero", 02);
 if(sizeOfRegion%getpagesize())
  sizeOfRegion = sizeOfRegion + getpagesize()-(sizeOfRegion%getpagesize());
 void *ptr = mmap(((void *)0), sizeOfRegion, 0x1 | 0x2, 0x02, fd, 0);
 sizeMem = sizeOfRegion;
 startMem = ptr;
 if(ptr == ((void *) -1))
 {
  perror("mmap");
  return -1;
 }
 freeList = ptr;
 freeList->size = sizeOfRegion - sizeof(memNode);
 freeList->next = ((void *)0);
 freeList->prev = ((void *)0);
 debugMode = debug;
 if(debugMode)
 {
  fillMemWithPattern((void *)ptr+sizeof(memNode), freeList->size,0xDEADBEEF);
 }
 close(fd);
 return 0;
}

variable_types: sizeOfRegion int

variable_types: debug int

variable_types: fd int

variable_types:
ptr
void *


original_code:
void* Mem_Alloc(int size)
{
 if(size <= 0)
  return ((void *)0);
 if(!freeList)
 {
  m_error = (1);
  return ((void *)0);
 }
 size_t memNodeSize = sizeof(memNode);
 size_t allocHeaderSize = sizeof(allocHeader);
 if(size%4)
 {
  size = size + 4 - (size%4);
 }
 if(debugMode)
  size = size + 64 *2;
 size_t sizeplusheader = size + allocHeaderSize;
 memNode * firstFit = ((void *)0);
 memNode * temp = freeList;
 while(temp)
 {
  if(debugMode)
  {
   size_t len = temp->size;
   unsigned int* tempptr = (void*)temp+memNodeSize;
   while(len>4)
   {
    len = len - 4;
    if(*tempptr != 0xDEADBEEF)
    {
     m_error = (2);
     return ((void *)0);
    }
    ++tempptr;
   }
  }
  if(temp->size >= size)
  {
   firstFit = temp;
   break;
  }
  temp = temp->next;
 }
 if(firstFit == ((void *)0))
 {
  m_error = (1);
  return ((void *)0);
 }
 if(firstFit->size == size)
 {
  removeMemNode(firstFit);
 }
 else
 {
  memNode* newNode = (memNode*)((void*)firstFit + sizeplusheader);
  newNode->size = firstFit->size - sizeplusheader;
  newNode->next = firstFit->next;
  newNode->prev = firstFit;
  firstFit->next = newNode;
  if(newNode->next!=((void *)0))
  {
   newNode->next->prev = newNode;
  }
  removeMemNode(firstFit);
 }
 if(debugMode)
 {
  allocHeader* ff = (allocHeader*)firstFit;
  ff->size = size -64*2;
  ff->redundancy = 0xAAAAAAAA;
  fillMemWithPattern((void*)firstFit+allocHeaderSize, 64,0xABCDDCBA);
  fillMemWithPattern((void*)firstFit+sizeplusheader-64, 64,0xABCDDCBA);
  return (void*)firstFit + allocHeaderSize + 64;
 }
 allocHeader* ff = (allocHeader*)firstFit;
 ff->size = size;
 ff->redundancy = 0xAAAAAAAA;
 return (void*)firstFit + allocHeaderSize;
}

canonical_original_code:
void *Mem_Alloc(int size)
{
 if(size <= 0)
  return ((void *)0);
 if(!freeList)
 {
  m_error = (1);
  return ((void *)0);
 }
 unsigned long memNodeSize = sizeof(memNode);
 unsigned long allocHeaderSize = sizeof(allocHeader);
 if(size%4)
 {
  size = size + 4 - (size%4);
 }
 if(debugMode)
  size = size + 64 *2;
 unsigned long sizeplusheader = size + allocHeaderSize;
 struct memNode *firstFit = ((void *)0);
 struct memNode *temp = freeList;
 while(temp)
 {
  if(debugMode)
  {
   unsigned long len = temp->size;
   unsigned int *tempptr = (void*)temp+memNodeSize;
   while(len>4)
   {
    len = len - 4;
    if(*tempptr != 0xDEADBEEF)
    {
     m_error = (2);
     return ((void *)0);
    }
    ++tempptr;
   }
  }
  if(temp->size >= size)
  {
   firstFit = temp;
   break;
  }
  temp = temp->next;
 }
 if(firstFit == ((void *)0))
 {
  m_error = (1);
  return ((void *)0);
 }
 if(firstFit->size == size)
 {
  removeMemNode(firstFit);
 }
 else
 {
  struct memNode *newNode = (memNode*)((void*)firstFit + sizeplusheader);
  newNode->size = firstFit->size - sizeplusheader;
  newNode->next = firstFit->next;
  newNode->prev = firstFit;
  firstFit->next = newNode;
  if(newNode->next!=((void *)0))
  {
   newNode->next->prev = newNode;
  }
  removeMemNode(firstFit);
 }
 if(debugMode)
 {
  struct allocHeader *ff = (allocHeader*)firstFit;
  ff->size = size -64*2;
  ff->redundancy = 0xAAAAAAAA;
  fillMemWithPattern((void *)firstFit+allocHeaderSize, 64,0xABCDDCBA);
  fillMemWithPattern((void *)firstFit+sizeplusheader-64, 64,0xABCDDCBA);
  return (void *)firstFit + allocHeaderSize + 64;
 }
 struct allocHeader *ff = (allocHeader*)firstFit;
 ff->size = size;
 ff->redundancy = 0xAAAAAAAA;
 return (void *)firstFit + allocHeaderSize;
}

variable_types: size int

variable_types:
memNodeSize
unsigned long

variable_types:
allocHeaderSize
unsigned long

variable_types:
sizeplusheader
unsigned long

variable_types: struct memNode { unsigned int size; struct memNode *next; struct memNode *prev; } *

variable_types:
len
unsigned long

variable_types:
tempptr
unsigned int *

variable_types: struct allocHeader { unsigned int size; unsigned long long gap; unsigned int redundancy; } *


decompiled_code:
__int64 __fastcall Mem_Free(__int64 a1)
{
unsigned int v1; // er8
unsigned int *v2; // rax
unsigned int *v3; // rdi
__int64 i; // rax
__int64 v5; // rsi
unsigned __int64 v6; // rax
int v7; // ebx
unsigned __int64 v9; // rdx
int *v10; // rcx
int v11; // er10
int v12; // er11
int v13; // ebx
unsigned int v14; // er9
int v15; // esi
__int64 v16; // rsi
__int64 v17; // rax
int v18; // esi
__int64 v19; // rax
int v20; // er9
__int64 v21; // rax
__int64 v22; // rax

if ( !a1 )
return 0LL;
v1 = debugMode;
v2 = (unsigned int *)(a1 - 88);
v3 = (unsigned int *)(a1 - 24);
if ( debugMode )
v3 = v2;
if ( startMem > (unsigned __int64)v3 || (unsigned __int64)v3 >= sizeMem + startMem || v3[4] != -1431655766 )
goto LABEL_47;
for ( i = 15LL; i != -1; --i )
{
if ( !debugMode )
{
*((_QWORD *)v3 + 1) = 0LL;
*((_QWORD *)v3 + 2) = 0LL;
v6 = freeList;
if ( !freeList )
{
freeList = v3;
return v1;
}
LABEL_13:
if ( (unsigned __int64)v3 >= v6 )
{
while ( 1 )
{
v9 = v6;
v6 = *(_QWORD *)(v6 + 8);
if ( !v6 )
break;
if ( (unsigned __int64)v3 < v9 )
goto LABEL_21;
}
if ( (unsigned __int64)v3 >= v9 )
{
if ( (_DWORD)v3 - *(_DWORD *)v9 - 24 != (_DWORD)v9 )
{
*(_QWORD *)(v9 + 8) = v3;
*((_QWORD *)v3 + 2) = v9;
if ( !v1 )
return 0LL;
fillMemWithPattern(v3 + 6, *v3, 3735928559LL);
return 0;
}
*(_DWORD *)v9 += *v3 + 24;
if ( !v1 )
return 0LL;
}
else
{
LABEL_21:
v10 = *(int **)(v9 + 16);
v11 = (_DWORD)v10 + *v10 + 24;
v12 = *v10;
v13 = (_DWORD)v3 - v11;
v14 = *v3;
v15 = (_DWORD)v3 + *v3 + 24 - v9;
if ( (_DWORD)v3 != v11 && v15 )
{
*((_QWORD *)v3 + 2) = v10;
*((_QWORD *)v3 + 1) = v9;
*((_QWORD *)v10 + 1) = v3;
*(_QWORD *)(v9 + 16) = v3;
if ( !v1 )
return 0LL;
fillMemWithPattern(v3 + 6, v14, 3735928559LL);
return 0;
}
if ( !(v13 | v15) )
{
v18 = *(_DWORD *)v9;
*((_QWORD *)v10 + 1) = v6;
v19 = *(_QWORD *)(v9 + 8);
*v10 = v12 + v14 + v18 + 48;
if ( v19 )
*(_QWORD *)(v19 + 16) = v10;
if ( v1 )
{
fillMemWithPattern(v3, *v3 + 48, 3735928559LL);
return 0;
}
return 0LL;
}
v5 = v14 + 24;
if ( v13 )
{
v20 = *(_DWORD *)v9;
*((_QWORD *)v3 + 1) = v6;
v21 = *(_QWORD *)(v9 + 8);
*((_QWORD *)v3 + 2) = v10;
*v3 = v5 + v20;
if ( v21 )
*(_QWORD *)(v21 + 16) = v3;
v22 = *(_QWORD *)(v9 + 16);
if ( v22 )
*(_QWORD *)(v22 + 8) = v3;
if ( v1 )
goto LABEL_40;
return 0LL;
}
*v10 = v12 + v5;
if ( !v1 )
return 0LL;
}
fillMemWithPattern(v3, *v3 + 24, 3735928559LL);
return 0;
}
v5 = *v3;
v7 = (_DWORD)v3 + *v3 + 24 - v6;
if ( v7 <= 0 )
{
if ( (_DWORD)v3 + *v3 + 24 != (_DWORD)v6 )
{
*((_QWORD *)v3 + 1) = v6;
freeList = v3;
*(_QWORD *)(v6 + 16) = v3;
if ( v1 )
goto LABEL_40;
return 0LL;
}
v16 = (unsigned int)(v5 + 24);
*v3 = v16 + *(_DWORD *)v6;
*((_QWORD *)v3 + 1) = *(_QWORD *)(v6 + 8);
v17 = *(_QWORD *)(v6 + 8);
if ( v17 )
*(_QWORD *)(v17 + 16) = v3;
freeList = v3;
if ( !v1 )
return 0LL;
fillMemWithPattern(v3 + 6, v16, 3735928559LL);
return (unsigned int)v7;
}
LABEL_47:
v1 = -1;
m_error = 5;
return v1;
}
if ( v3[i + 6] != -1412571974 || *(unsigned int *)((char *)&v3[i + 22] + *v3) != -1412571974 )
{
v1 = -1;
m_error = 3;
return v1;
}
}
v5 = *v3 + 128;
*((_QWORD *)v3 + 1) = 0LL;
*v3 = v5;
v6 = freeList;
*((_QWORD *)v3 + 2) = 0LL;
if ( v6 )
goto LABEL_13;
freeList = v3;
LABEL_40:
fillMemWithPattern(v3 + 6, v5, 3735928559LL);
return 0LL;
}

original_code:
int Mem_Free(void* ptr)
{
 if(ptr == ((void *)0))
  return 0;
 size_t memNodeSize = sizeof(memNode);
 size_t allocHeaderSize = sizeof(allocHeader);
 memNode* toFree;
 void* start;
 if(debugMode)
 {
  start = ptr - allocHeaderSize - 64;
 }
 else
  start = (ptr - allocHeaderSize);
 allocHeader* alloc = (allocHeader*) start;
 if(start< startMem || start >= startMem+sizeMem)
 {
  m_error = (5);
  return -1;
 }
 if(alloc->redundancy!= 0xAAAAAAAA)
 {
  m_error = (5);
  return -1;
 }
 int tempPadding = 60;
 while(tempPadding>=0 && debugMode)
 {
  if(*(int*)(start+allocHeaderSize+tempPadding) != 0xABCDDCBA || *(int*)(start+allocHeaderSize+alloc->size+64 +tempPadding) !=0xABCDDCBA)
  {
   m_error = (3);
   return -1;
  }
  tempPadding-=4;
 }
 size_t sizeOfChunk = alloc->size;
 toFree = start;
 if(debugMode)
  toFree->size = sizeOfChunk + allocHeaderSize + 2*64 -memNodeSize;
 else
  toFree->size = sizeOfChunk + allocHeaderSize - memNodeSize;
 toFree->next = ((void *)0);
 toFree->prev = ((void *)0);
 if(freeList ==((void *)0))
 {
  freeList = toFree;
  if(debugMode)
  {
   fillMemWithPattern((void*)toFree + memNodeSize, toFree->size,0xDEADBEEF);
  }
  return 0;
 }
 if((void*)toFree<(void*)freeList)
 {
  int diff = (void*)toFree + toFree->size + memNodeSize - (void*)freeList;
  if(diff > 0)
  {
   m_error = (5);
   return -1;
  }
  else if(diff<0)
  {
   toFree ->next = freeList;
   freeList->prev = toFree;
   freeList = toFree;
   if(debugMode)
   {
    fillMemWithPattern((void*)toFree + memNodeSize, toFree->size,0xDEADBEEF);
   }
  }
  else if(diff ==0)
  {
   int lengthToFree = toFree->size + memNodeSize;
   toFree->size = toFree->size + freeList->size + memNodeSize;
   toFree->next = freeList->next;
   if(freeList->next)
    freeList->next->prev = toFree;
   freeList = toFree;
   if(debugMode)
   {
    fillMemWithPattern((void*)toFree + memNodeSize, lengthToFree,0xDEADBEEF);
   }
  }
 }
 else
 {
  memNode* fit = freeList;
  while((fit->next) && (void*)fit <= (void*)toFree)
  {
   fit = fit -> next;
  }
  if(fit->next == ((void *)0) && (void*)fit <= (void*)toFree)
  {
   int diff = (void*) toFree - fit->size -memNodeSize -(void*) fit;
   if(diff==0)
   {
    fit->size = fit->size + toFree->size + memNodeSize;
    if(debugMode)
    {
     fillMemWithPattern((void*)toFree, toFree->size + memNodeSize,0xDEADBEEF);
    }
   }
   else
   {
    fit->next = toFree;
    toFree->prev = fit;
    if(debugMode)
    {
     fillMemWithPattern((void*)toFree + memNodeSize, toFree->size ,0xDEADBEEF);
    }
   }
  }
  else
  {
   int diff1 = (void*)toFree - ((void*) fit->prev + fit->prev->size + memNodeSize);
   int diff2 = (void*) toFree + toFree->size + memNodeSize - (void*)fit;
   if( diff1 == 0 || diff2 == 0)
   {
    if(diff1==0 && diff2==0)
    {
     fit->prev->size = fit->prev->size + toFree->size + fit->size + 2* memNodeSize;
     fit->prev->next = fit->next;
     if(fit->next)
      fit->next->prev = fit->prev;
     if(debugMode)
     {
      fillMemWithPattern((void*)toFree, toFree->size + 2*memNodeSize,0xDEADBEEF);
     }
    }
    else if(diff1==0)
    {
     fit->prev->size = fit->prev->size + toFree->size+ memNodeSize;
     if(debugMode)
     {
      fillMemWithPattern((void*)toFree, toFree->size + memNodeSize,0xDEADBEEF);
     }
    }
    else
    {
     int lenghtToFree = toFree->size +memNodeSize;
     toFree->size = toFree->size + fit->size + memNodeSize;
     toFree->next = fit->next;
     toFree -> prev = fit-> prev;
     if(fit->next)
      fit->next->prev = toFree;
     if(fit -> prev)
      fit->prev->next = toFree;
     if(debugMode)
     {
      fillMemWithPattern((void*)toFree + memNodeSize, lenghtToFree,0xDEADBEEF);
     }
    }
   }
   else
   {
    toFree->prev = fit->prev;
    toFree->next = fit;
    toFree->prev->next = toFree;
    fit->prev = toFree;
    if(debugMode)
    {
     fillMemWithPattern((void*)toFree + memNodeSize, toFree->size,0xDEADBEEF);
    }
   }
  }
 }
 return 0;
}

canonical_original_code:
int Mem_Free(void *ptr)
{
 if(ptr == ((void *)0))
  return 0;
 unsigned long memNodeSize = sizeof(memNode);
 unsigned long allocHeaderSize = sizeof(allocHeader);
 struct memNode *toFree;
 void *start;
 if(debugMode)
 {
  start = ptr - allocHeaderSize - 64;
 }
 else
  start = (ptr - allocHeaderSize);
 struct allocHeader *alloc = (allocHeader*) start;
 if(start< startMem || start >= startMem+sizeMem)
 {
  m_error = (5);
  return -1;
 }
 if(alloc->redundancy!= 0xAAAAAAAA)
 {
  m_error = (5);
  return -1;
 }
 int tempPadding = 60;
 while(tempPadding>=0 && debugMode)
 {
  if(*(int *)(start+allocHeaderSize+tempPadding) != 0xABCDDCBA || *(int *)(start+allocHeaderSize+alloc->size+64 +tempPadding) !=0xABCDDCBA)
  {
   m_error = (3);
   return -1;
  }
  tempPadding-=4;
 }
 unsigned long sizeOfChunk = alloc->size;
 toFree = start;
 if(debugMode)
  toFree->size = sizeOfChunk + allocHeaderSize + 2*64 -memNodeSize;
 else
  toFree->size = sizeOfChunk + allocHeaderSize - memNodeSize;
 toFree->next = ((void *)0);
 toFree->prev = ((void *)0);
 if(freeList ==((void *)0))
 {
  freeList = toFree;
  if(debugMode)
  {
   fillMemWithPattern((void *)toFree + memNodeSize, toFree->size,0xDEADBEEF);
  }
  return 0;
 }
 if((void *)toFree<(void *)freeList)
 {
  int diff = (void*)toFree + toFree->size + memNodeSize - (void*)freeList;
  if(diff > 0)
  {
   m_error = (5);
   return -1;
  }
  else if(diff<0)
  {
   toFree ->next = freeList;
   freeList->prev = toFree;
   freeList = toFree;
   if(debugMode)
   {
    fillMemWithPattern((void *)toFree + memNodeSize, toFree->size,0xDEADBEEF);
   }
  }
  else if(diff ==0)
  {
   int lengthToFree = toFree->size + memNodeSize;
   toFree->size = toFree->size + freeList->size + memNodeSize;
   toFree->next = freeList->next;
   if(freeList->next)
    freeList->next->prev = toFree;
   freeList = toFree;
   if(debugMode)
   {
    fillMemWithPattern((void *)toFree + memNodeSize, lengthToFree,0xDEADBEEF);
   }
  }
 }
 else
 {
  struct memNode *fit = freeList;
  while((fit->next) && (void *)fit <= (void *)toFree)
  {
   fit = fit -> next;
  }
  if(fit->next == ((void *)0) && (void *)fit <= (void *)toFree)
  {
   int diff = (void*) toFree - fit->size -memNodeSize -(void*) fit;
   if(diff==0)
   {
    fit->size = fit->size + toFree->size + memNodeSize;
    if(debugMode)
    {
     fillMemWithPattern((void *)toFree, toFree->size + memNodeSize,0xDEADBEEF);
    }
   }
   else
   {
    fit->next = toFree;
    toFree->prev = fit;
    if(debugMode)
    {
     fillMemWithPattern((void *)toFree + memNodeSize, toFree->size ,0xDEADBEEF);
    }
   }
  }
  else
  {
   int diff1 = (void*)toFree - ((void*) fit->prev + fit->prev->size + memNodeSize);
   int diff2 = (void*) toFree + toFree->size + memNodeSize - (void*)fit;
   if( diff1 == 0 || diff2 == 0)
   {
    if(diff1==0 && diff2==0)
    {
     fit->prev->size = fit->prev->size + toFree->size + fit->size + 2* memNodeSize;
     fit->prev->next = fit->next;
     if(fit->next)
      fit->next->prev = fit->prev;
     if(debugMode)
     {
      fillMemWithPattern((void *)toFree, toFree->size + 2*memNodeSize,0xDEADBEEF);
     }
    }
    else if(diff1==0)
    {
     fit->prev->size = fit->prev->size + toFree->size+ memNodeSize;
     if(debugMode)
     {
      fillMemWithPattern((void *)toFree, toFree->size + memNodeSize,0xDEADBEEF);
     }
    }
    else
    {
     int lenghtToFree = toFree->size +memNodeSize;
     toFree->size = toFree->size + fit->size + memNodeSize;
     toFree->next = fit->next;
     toFree -> prev = fit-> prev;
     if(fit->next)
      fit->next->prev = toFree;
     if(fit -> prev)
      fit->prev->next = toFree;
     if(debugMode)
     {
      fillMemWithPattern((void *)toFree + memNodeSize, lenghtToFree,0xDEADBEEF);
     }
    }
   }
   else
   {
    toFree->prev = fit->prev;
    toFree->next = fit;
    toFree->prev->next = toFree;
    fit->prev = toFree;
    if(debugMode)
    {
     fillMemWithPattern((void *)toFree + memNodeSize, toFree->size,0xDEADBEEF);
    }
   }
  }
 }
 return 0;
}

variable_types:
start
void *

variable_types: tempPadding int

variable_types:
sizeOfChunk
unsigned long

variable_types: diff int

variable_types: lengthToFree int

variable_types: lenghtToFree int

user_defined_types: struct memNode { unsigned int size; struct memNode *next; struct memNode *prev; }

user_defined_types: struct allocHeader { unsigned int size; unsigned long long gap; unsigned int redundancy; }


decompiled_code:
int Mem_Dump()
{
unsigned int *v0; // rbx

puts("\nMemory Dump:");
v0 = (unsigned int *)freeList;
if ( freeList )
{
do
{
printf("Start Address: %p\t Available space: %d bytes\n", v0 + 6, *v0);
v0 = (unsigned int *)*((_QWORD *)v0 + 1);
}
while ( v0 );
}
return puts("End of memdump");
}

canonical_decompiled_code:
int func11()
{
unsigned int *v0; // rbx

puts("\nMemory Dump:");
v0 = (unsigned int *)freeList;
if ( freeList )
{
do
{
printf("Start Address: %p\t Available space: %d bytes\n", v0 + 6, *v0);
v0 = (unsigned int *)*((_QWORD *)v0 + 1);
}
while ( v0 );
}
return puts("End of memdump");
}

original_code:
void Mem_Dump()
{
 printf("\nMemory Dump:\n");
 memNode* temp = freeList;
 while(temp)
 {
  printf("Start Address: %p\t Available space: %d bytes\n",(void*)temp+sizeof(memNode), temp->size);
  temp = temp->next;
 }
 printf("End of memdump\n");
}

canonical_original_code:
void Mem_Dump()
{
 printf("\nMemory Dump:\n");
 struct memNode *temp = freeList;
 while(temp)
 {
  printf("Start Address: %p\t Available space: %d bytes\n",(void *)temp+sizeof(memNode), temp->size);
  temp = temp->next;
 }
 printf("End of memdump\n");
}


decompiled_code:
__int64 __fastcall spinlock_acquire(volatile __int32 *a1)
{
while ( (unsigned int)xchg(a1, 1) == 1 )
;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func7(volatile __int32 *a1)
{
while ( (unsigned int)func5(a1, 1) == 1 )
;
return 0LL;
}

original_code:
int spinlock_acquire(spinlock_t* lock)
{
  while (xchg(&lock->state, 1) == 1)
    ;
  return 0;
}

canonical_original_code:
int spinlock_acquire(struct spinlock_t *lock)
{
  while (xchg(&lock->state, 1) == 1)
    ;
  return 0;
}

variable_types:
lock
struct spinlock_t { int state; } *


decompiled_code:
// attributes: thunk
__int64 __fastcall spinlock_release(volatile __int32 *a1)
{
return spinlock_init_localalias(a1);
}

canonical_decompiled_code:
__int64 __fastcall func8(volatile __int32 *a1)
{
return func6(a1);
}

original_code:
int spinlock_release(spinlock_t* lock)
{
  xchg(&lock->state, 0);
  return 0;
}

canonical_original_code:
int spinlock_release(struct spinlock_t *lock)
{
  xchg(&lock->state, 0);
  return 0;
}


decompiled_code:
__int64 __fastcall Counter_GetValue(unsigned int *a1)
{
return *a1;
}

canonical_decompiled_code:
__int64 __fastcall func7(unsigned int *a1)
{
return *a1;
}

original_code:
int Counter_GetValue(counter_t *c)
{
  return c->value;
}

canonical_original_code:
int Counter_GetValue(struct counter_type *c)
{
  return c->value;
}

variable_types:
c
struct counter_type { int value; } *


decompiled_code:
__int64 __fastcall Counter_Increment(_DWORD *a1)
{
spinlock_acquire();
++*a1;
return spinlock_release();
}

canonical_decompiled_code:
__int64 __fastcall func8(_DWORD *a1)
{
spinlock_acquire();
++*a1;
return spinlock_release();
}

original_code:
void Counter_Increment(counter_t *c)
{
  spinlock_acquire(&slock);
  c->value += 1;
  spinlock_release(&slock);
}

canonical_original_code:
void Counter_Increment(struct counter_type *c)
{
  spinlock_acquire(&slock);
  c->value += 1;
  spinlock_release(&slock);
}


decompiled_code:
__int64 __fastcall Counter_Decrement(_DWORD *a1)
{
spinlock_acquire();
--*a1;
return spinlock_release();
}

canonical_decompiled_code:
__int64 __fastcall func9(_DWORD *a1)
{
spinlock_acquire();
--*a1;
return spinlock_release();
}

original_code:
void Counter_Decrement(counter_t *c)
{
  spinlock_acquire(&slock);
  c->value -= 1;
  spinlock_release(&slock);
}

canonical_original_code:
void Counter_Decrement(struct counter_type *c)
{
  spinlock_acquire(&slock);
  c->value -= 1;
  spinlock_release(&slock);
}


repo: aayvazyan-tgm/Sudoku

decompiled_code:
__int64 __fastcall char_to_value(char a1)
{
__int64 v1; // rax
char i; // dl
unsigned int v3; // er8

v1 = 1LL;
for ( i = 49; ; i = value_chars[v1] )
{
v3 = v1;
if ( a1 == i )
break;
if ( ++v1 == 10 )
return 0;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(char a1)
{
__int64 v1; // rax
char i; // dl
unsigned int v3; // er8

v1 = 1LL;
for ( i = 49; ; i = value_chars[v1] )
{
v3 = v1;
if ( a1 == i )
break;
if ( ++v1 == 10 )
return 0;
}
return v3;
}

original_code:
value_t char_to_value(char c) {
 value_t value;
 for (value = 1; value <= psize; ++value) {
  if (value_chars[value] == c) {
   return value;
  }
 }
 return 0;
}

canonical_original_code:
unsigned char char_to_value(char c) {
 unsigned char value;
 for (value = 1; value <= psize; ++value) {
  if (value_chars[value] == c) {
   return value;
  }
 }
 return 0;
}

variable_types: c char

variable_types:
value
unsigned char

return_type: unsigned char


decompiled_code:
__int64 __fastcall puzzle_fill_cell(__int64 a1, unsigned int a2, unsigned int a3, char a4)
{
__int64 v4; // rdi
__int64 result; // rax

v4 = 9LL * (int)a2 + a1;
result = (int)a3;
if ( a4 && *(_BYTE *)(v4 + (int)a3) )
{
fprintf(stderr, "cell (%d, %d) already has a value!", a2, a3);
exit(-1);
}
*(_BYTE *)(v4 + (int)a3) = a4;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, unsigned int a2, unsigned int a3, char a4)
{
__int64 v4; // rdi
__int64 result; // rax

v4 = 9LL * (int)a2 + a1;
result = (int)a3;
if ( a4 && *(_BYTE *)(v4 + (int)a3) )
{
fprintf(stderr, "cell (%d, %d) already has a value!", a2, a3);
exit(-1);
}
*(_BYTE *)(v4 + (int)a3) = a4;
return result;
}

original_code:
void puzzle_fill_cell(value_t puzzle[psize][psize], int row, int col,
  value_t value) {
 if (value && puzzle[row][col]) {
  fprintf(stderr, "cell (%d, %d) already has a value!", row, col);
  exit(-1);
 }
 puzzle[row][col] = value;
}

canonical_original_code:
void puzzle_fill_cell(unsigned char puzzle[-1][-1], int row, int col, unsigned char value) {
 if (value && puzzle[row][col]) {
  fprintf(stderr, "cell (%d, %d) already has a value!", row, col);
  exit(-1);
 }
 puzzle[row][col] = value;
}

variable_types:
puzzle
unsigned char [-1][-1]

variable_types: row int

variable_types: col int


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

v4 = 0;
while ( 2 )
{
for ( i = 0LL; i != 9; ++i )
{
if ( !*(_BYTE *)(a1 + i) )
{
*a2 = v4;
*a3 = i;
return 1LL;
}
}
++v4;
a1 += 9LL;
if ( v4 != 9 )
continue;
break;
}
*a2 = 9;
result = 0LL;
*a3 = 9;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, _DWORD *a2, _DWORD *a3)
{
int v4; // edx
__int64 i; // rax
__int64 result; // rax

v4 = 0;
while ( 2 )
{
for ( i = 0LL; i != 9; ++i )
{
if ( !*(_BYTE *)(a1 + i) )
{
*a2 = v4;
*a3 = i;
return 1LL;
}
}
++v4;
a1 += 9LL;
if ( v4 != 9 )
continue;
break;
}
*a2 = 9;
result = 0LL;
*a3 = 9;
return result;
}

original_code:
int puzzle_find_empty(value_t puzzle[psize][psize], int* row_out, int* col_out) {
 int row, col;
 for (row = 0; row < psize; ++row) {
  for (col = 0; col < psize; ++col) {
   value_t value = puzzle[row][col];
   if (!value) {
    *row_out = row;
    *col_out = col;
    return 1;
   }
  }
 }
 *row_out = psize;
 *col_out = psize;
 return 0;
}

canonical_original_code:
int puzzle_find_empty(unsigned char puzzle[-1][-1], int *row_out, int *col_out) {
 int row; int col;
 for (row = 0; row < psize; ++row) {
  for (col = 0; col < psize; ++col) {
   unsigned char value = puzzle[row][col];
   if (!value) {
    *row_out = row;
    *col_out = col;
    return 1;
   }
  }
 }
 *row_out = psize;
 *col_out = psize;
 return 0;
}


decompiled_code:
__int64 __fastcall region_check_and_mark(_WORD *a1, char a2)
{
__int64 result; // rax
int v3; // ecx

result = 1LL;
if ( a2 )
{
v3 = 1 << (a2 - 1);
result = 0LL;
if ( ((unsigned __int16)v3 & *a1) == 0 )
{
result = 1LL;
*a1 |= v3;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func6(_WORD *a1, char a2)
{
__int64 result; // rax
int v3; // ecx

result = 1LL;
if ( a2 )
{
v3 = 1 << (a2 - 1);
result = 0LL;
if ( ((unsigned __int16)v3 & *a1) == 0 )
{
result = 1LL;
*a1 |= v3;
}
}
return result;
}

original_code:
int region_check_and_mark(value_set_t* region, value_t value) {
 if (value) {
  int marker = (1 << (value - 1));
  if (*region & marker) {
   return 0;
  } else {
   *region = *region | marker;
  }
 }
 return 1;
}

canonical_original_code:
int region_check_and_mark(unsigned short *region, unsigned char value) {
 if (value) {
  int marker = (1 << (value - 1));
  if (*region & marker) {
   return 0;
  } else {
   *region = *region | marker;
  }
 }
 return 1;
}

variable_types:
region
unsigned short *

variable_types: marker int


original_code:
int puzzle_is_valid(value_t puzzle[psize][psize]) {
 int row, col, start_row, start_col, block_row, block_col;
 value_set_t region;
 for (row = 0; row < psize; ++row) {
  region = 0;
  for (col = 0; col < psize; ++col) {
   if (!region_check_and_mark(&region, puzzle[row][col])) {
    return 0;
   }
  }
 }
 for (col = 0; col < psize; ++col) {
  region = 0;
  for (row = 0; row < psize; ++row) {
   if (!region_check_and_mark(&region, puzzle[row][col])) {
    return 0;
   }
  }
 }
 for (start_row = 0; start_row < psize; start_row += bsize) {
  for (start_col = 0; start_col < psize; start_col += bsize) {
   region = 0;
   for (block_row = 0; block_row < bsize; ++block_row) {
    for (block_col = 0; block_col < bsize; ++block_col) {
     row = start_row + block_row;
     col = start_col + block_col;
     if (!region_check_and_mark(&region, puzzle[row][col])) {
      return 0;
     }
    }
   }
  }
 }
 if(xsudoku==1){
  int horizontal;
  int vertikal;
  value_t zero=0;
  for(vertikal=0;vertikal<psize;vertikal++){
   for(horizontal=0;horizontal<psize;horizontal++){
    if(vertikal==horizontal)continue;
    if(puzzle[vertikal][vertikal]==zero)continue;
    if(puzzle[horizontal][horizontal]==zero)continue;
    if(puzzle[vertikal][vertikal]==puzzle[horizontal][horizontal])
     {
     return 0;
     }
   }
  }
  for(vertikal=psize;vertikal<psize;vertikal--){
   for(horizontal=0;horizontal<psize;horizontal++){
    if(vertikal==horizontal)continue;
    if(puzzle[vertikal][vertikal]==zero)continue;
    if(puzzle[horizontal-psize+1][horizontal]==zero)continue;
    if(puzzle[vertikal][vertikal]==puzzle[horizontal-psize+1][horizontal])return 0;
   }
  }
 }
 return 1;
}

canonical_original_code:
int puzzle_is_valid(unsigned char puzzle[-1][-1]) {
 int row; int col; int start_row; int start_col; int block_row; int block_col;
 unsigned short region;
 for (row = 0; row < psize; ++row) {
  region = 0;
  for (col = 0; col < psize; ++col) {
   if (!region_check_and_mark(&region, puzzle[row][col])) {
    return 0;
   }
  }
 }
 for (col = 0; col < psize; ++col) {
  region = 0;
  for (row = 0; row < psize; ++row) {
   if (!region_check_and_mark(&region, puzzle[row][col])) {
    return 0;
   }
  }
 }
 for (start_row = 0; start_row < psize; start_row += bsize) {
  for (start_col = 0; start_col < psize; start_col += bsize) {
   region = 0;
   for (block_row = 0; block_row < bsize; ++block_row) {
    for (block_col = 0; block_col < bsize; ++block_col) {
     row = start_row + block_row;
     col = start_col + block_col;
     if (!region_check_and_mark(&region, puzzle[row][col])) {
      return 0;
     }
    }
   }
  }
 }
 if(xsudoku==1){
  int horizontal;
  int vertikal;
  unsigned char zero = 0;
  for(vertikal=0;vertikal<psize;vertikal++){
   for(horizontal=0;horizontal<psize;horizontal++){
    if(vertikal==horizontal)continue;
    if(puzzle[vertikal][vertikal]==zero)continue;
    if(puzzle[horizontal][horizontal]==zero)continue;
    if(puzzle[vertikal][vertikal]==puzzle[horizontal][horizontal])
     {
     return 0;
     }
   }
  }
  for(vertikal=psize;vertikal<psize;vertikal--){
   for(horizontal=0;horizontal<psize;horizontal++){
    if(vertikal==horizontal)continue;
    if(puzzle[vertikal][vertikal]==zero)continue;
    if(puzzle[horizontal-psize+1][horizontal]==zero)continue;
    if(puzzle[vertikal][vertikal]==puzzle[horizontal-psize+1][horizontal])return 0;
   }
  }
 }
 return 1;
}

variable_types:
region
unsigned short

variable_types: horizontal int

variable_types: vertikal int

variable_types:
zero
unsigned char


repo: ab/stripe-ctf

decompiled_code:
__int64 __fastcall char_at(const char *a1, int a2)
{
size_t v2; // r8
__int64 result; // rax

v2 = strlen(a1);
result = 0LL;
if ( a2 < v2 )
result = (unsigned __int8)a1[a2];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(const char *a1, int a2)
{
size_t v2; // r8
__int64 result; // rax

v2 = strlen(a1);
result = 0LL;
if ( a2 < v2 )
result = (unsigned __int8)a1[a2];
return result;
}

original_code:
char
char_at(char *str, int pos)
{
 return pos < strlen(str) ? str[pos] : 0;
}

variable_types:
str
char *

variable_types: pos int

return_type: char


taunt

decompiled_code:
__pid_t taunt()
{
__pid_t result; // eax

result = fork();
if ( !result )
{
execl("/bin/echo", "/bin/echo", "Ha ha, your password is incorrect!", 0LL);
exit(1);
}
return result;
}

canonical_decompiled_code:
__pid_t func8()
{
__pid_t result; // eax

result = fork();
if ( !result )
{
execl("/bin/echo", "/bin/echo", "Ha ha, your password is incorrect!", 0LL);
exit(1);
}
return result;
}

original_code:
void
taunt()
{
 if (!fork()) {
  execl("/bin/echo", "/bin/echo", "Ha ha, your password is incorrect!", ((void *)0));
  exit(1);
 }
}


decompiled_code:
__int64 __fastcall command_status(__int64 a1)
{
return *(unsigned int *)(a1 + 4);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
return *(unsigned int *)(a1 + 4);
}

original_code:
int
command_status (command_t c)
{
  return c->status;
}

canonical_original_code:
int command_status(struct command *c)
{
  return c->status;
}

variable_types: struct command { enum command_type { AND_COMMAND=0, SEQUENCE_COMMAND=1, OR_COMMAND=2, PIPE_COMMAND=3, SIMPLE_COMMAND=4, START_SUBSHELL_COMMAND=5, END_SUBSHELL_COMMAND=6, SUBSHELL_COMMAND=7, NEWLINE_COMMAND=8, REDIRECT_FROM_COMMAND=9, REDIRECT_TO_COMMAND=10 } type; int status; char *input; char *output; union { struct command *command[2]; char **word; struct command *subshell_command; } u; } *

user_defined_types: struct command { enum command_type type; int status; char *input; char *output; union { struct command *command[2]; char **word; struct command *subshell_command; } u; }


decompiled_code:
int print_dependency_matrix()
{
int result; // eax
int v1; // er12
__int64 v2; // rbp
__int64 v3; // rbx
__int64 v4; // rsi

result = num_threads;
if ( num_threads > 0 )
{
v1 = 0;
v2 = 0LL;
do
{
v3 = 0LL;
do
{
v4 = (unsigned int)dependency_table[v3++ + v2];
printf(format, v4);
}
while ( num_threads > (int)v3 );
++v1;
v2 += 10000LL;
result = putchar(10);
}
while ( num_threads > v1 );
}
return result;
}

canonical_decompiled_code:
int func1()
{
int result; // eax
int v1; // er12
__int64 v2; // rbp
__int64 v3; // rbx
__int64 v4; // rsi

result = num_threads;
if ( num_threads > 0 )
{
v1 = 0;
v2 = 0LL;
do
{
v3 = 0LL;
do
{
v4 = (unsigned int)dependency_table[v3++ + v2];
printf(format, v4);
}
while ( num_threads > (int)v3 );
++v1;
v2 += 10000LL;
result = putchar(10);
}
while ( num_threads > v1 );
}
return result;
}

original_code:
void
print_dependency_matrix ()
{
 int i,j;
 for (i = 0; i < num_threads; i++)
 {
  for(j = 0; j < num_threads; j++)
  {
   printf("%i ",dependency_table[i][j]);
  }
  printf("\n");
 }
}

canonical_original_code:
void print_dependency_matrix()
{
 int i; int j;
 for (i = 0; i < num_threads; i++)
 {
  for(j = 0; j < num_threads; j++)
  {
   printf("%i ",dependency_table[i][j]);
  }
  printf("\n");
 }
}

variable_types: i int

variable_types: j int


decompiled_code:
__int64 __fastcall num_sequence_commands(_DWORD *a1)
{
unsigned int v1; // er12
_DWORD *v2; // rbx
int v3; // eax

v1 = 0;
if ( *a1 == 1 )
{
v2 = a1;
do
{
v3 = num_sequence_commands(*((_QWORD *)v2 + 3));
v2 = (_DWORD *)*((_QWORD *)v2 + 4);
v1 += v3 + 1;
}
while ( *v2 == 1 );
}
return v1;
}

original_code:
int
num_sequence_commands(command_t c)
{
 if(c->type != SEQUENCE_COMMAND) {
  return 0;
 }
 else
 {
  return (1+num_sequence_commands(c->u.command[0])+num_sequence_commands(c->u.command[1]));
 }
}

canonical_original_code:
int num_sequence_commands(struct command *c)
{
 if(c->type != SEQUENCE_COMMAND) {
  return 0;
 }
 else
 {
  return (1+num_sequence_commands(c->u.command[0])+num_sequence_commands(c->u.command[1]));
 }
}

user_defined_types: enum command_type { AND_COMMAND=0, SEQUENCE_COMMAND=1, OR_COMMAND=2, PIPE_COMMAND=3, SIMPLE_COMMAND=4, START_SUBSHELL_COMMAND=5, END_SUBSHELL_COMMAND=6, SUBSHELL_COMMAND=7, NEWLINE_COMMAND=8, REDIRECT_FROM_COMMAND=9, REDIRECT_TO_COMMAND=10 }


decompiled_code:
__int64 __fastcall find_threads(__int64 a1)
{
__int64 i; // rbx
int v2; // eax
__int64 result; // rax

for ( i = a1; *(_DWORD *)i == 1; i = *(_QWORD *)(i + 32) )
find_threads(*(_QWORD *)(i + 24));
v2 = index_counter;
pthread_list[index_counter] = i;
result = (unsigned int)(v2 + 1);
index_counter = result;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int64 i; // rbx
int v2; // eax
__int64 result; // rax

for ( i = a1; *(_DWORD *)i == 1; i = *(_QWORD *)(i + 32) )
func3(*(_QWORD *)(i + 24));
v2 = index_counter;
pthread_list[index_counter] = i;
result = (unsigned int)(v2 + 1);
index_counter = result;
return result;
}

original_code:
void
find_threads (command_t cmd)
{
 if(cmd->type != SEQUENCE_COMMAND)
 {
  pthread_list[index_counter] = cmd;
  index_counter++;
 }
 else
 {
  find_threads(cmd->u.command[0]);
  find_threads(cmd->u.command[1]);
 }
}

canonical_original_code:
void find_threads(struct command *cmd)
{
 if(cmd->type != SEQUENCE_COMMAND)
 {
  pthread_list[index_counter] = cmd;
  index_counter++;
 }
 else
 {
  find_threads(cmd->u.command[0]);
  find_threads(cmd->u.command[1]);
 }
}


decompiled_code:
__int64 __fastcall analyze_subtree(const char *a1, _DWORD *a2)
{
unsigned int v2; // er12
int v4; // eax
_DWORD *i; // rbx
int v6; // eax
const char *v7; // rdi

v2 = 0;
v4 = *a2;
for ( i = a2; *i != 4; v4 = *i )
{
if ( v4 == 7 )
{
i = (_DWORD *)*((_QWORD *)i + 3);
}
else
{
v6 = analyze_subtree(a1, *((_QWORD *)i + 3));
i = (_DWORD *)*((_QWORD *)i + 4);
v2 += v6;
}
}
v7 = (const char *)*((_QWORD *)i + 1);
if ( v7 )
v2 += strcmp(v7, a1) == 0;
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1, _DWORD *a2)
{
unsigned int v2; // er12
int v4; // eax
_DWORD *i; // rbx
int v6; // eax
const char *v7; // rdi

v2 = 0;
v4 = *a2;
for ( i = a2; *i != 4; v4 = *i )
{
if ( v4 == 7 )
{
i = (_DWORD *)*((_QWORD *)i + 3);
}
else
{
v6 = func4(a1, *((_QWORD *)i + 3));
i = (_DWORD *)*((_QWORD *)i + 4);
v2 += v6;
}
}
v7 = (const char *)*((_QWORD *)i + 1);
if ( v7 )
v2 += strcmp(v7, a1) == 0;
return v2;
}

original_code:
int
analyze_subtree (char * file, command_t cmd)
{
 if(cmd->type == SIMPLE_COMMAND)
 {
  if(cmd->input)
  {
   if(strcmp(cmd->input,file) == 0)
   {
    return 1;
   }
  }
  return 0;
 }
 else
 {
  switch(cmd->type)
  {
   case SUBSHELL_COMMAND:
    return analyze_subtree(file,cmd->u.subshell_command);
   default:
    return analyze_subtree(file,cmd->u.command[0]) + analyze_subtree(file,cmd->u.command[1]);
  }
 }
 return 0;
}

canonical_original_code:
int analyze_subtree(char *file, struct command *cmd)
{
 if(cmd->type == SIMPLE_COMMAND)
 {
  if(cmd->input)
  {
   if(strcmp(cmd->input,file) == 0)
   {
    return 1;
   }
  }
  return 0;
 }
 else
 {
  switch(cmd->type)
  {
   case SUBSHELL_COMMAND:
    return analyze_subtree(file,cmd->u.subshell_command);
   default:
    return analyze_subtree(file,cmd->u.command[0]) + analyze_subtree(file,cmd->u.command[1]);
  }
 }
 return 0;
}

variable_types:
file
char *


decompiled_code:
void __fastcall locate_dependencies(const char *a1, int a2)
{
__int64 v2; // rbx
int v3; // er13
int *v4; // rbp

LODWORD(v2) = a2 + 1;
v3 = index_counter;
if ( a2 + 1 < index_counter )
{
v2 = (int)v2;
v4 = &dependency_table[10001 * a2 + 10000];
do
{
if ( (unsigned int)analyze_subtree(a1, (_DWORD *)pthread_list[v2]) )
*v4 = 1;
++v2;
v4 += 10000;
}
while ( v3 > (int)v2 );
}
}

canonical_decompiled_code:
void __fastcall func5(const char *a1, int a2)
{
__int64 v2; // rbx
int v3; // er13
int *v4; // rbp

LODWORD(v2) = a2 + 1;
v3 = index_counter;
if ( a2 + 1 < index_counter )
{
v2 = (int)v2;
v4 = &dependency_table[10001 * a2 + 10000];
do
{
if ( (unsigned int)func4(a1, (_DWORD *)pthread_list[v2]) )
*v4 = 1;
++v2;
v4 += 10000;
}
while ( v3 > (int)v2 );
}
}

original_code:
void
locate_dependencies (char * file, int thread_id)
{
 int k = thread_id+1;
 int dependency_found;
 for(; k < index_counter; k++)
 {
  dependency_found = analyze_subtree(file, pthread_list[k]);
  if(dependency_found) {
   dependency_table[k][thread_id] = 1;
  }
 }
}

canonical_original_code:
void locate_dependencies(char *file, int thread_id)
{
 int k = thread_id+1;
 int dependency_found;
 for(; k < index_counter; k++)
 {
  dependency_found = analyze_subtree(file, pthread_list[k]);
  if(dependency_found) {
   dependency_table[k][thread_id] = 1;
  }
 }
}

variable_types: k int


decompiled_code:
void __fastcall extract_dependencies(_DWORD *a1, unsigned int a2)
{
_DWORD *v2; // rbx
int i; // eax
const char *v4; // rdi

v2 = a1;
for ( i = *a1; *v2 != 4; i = *v2 )
{
if ( i == 7 )
{
v2 = (_DWORD *)*((_QWORD *)v2 + 3);
}
else
{
extract_dependencies(*((_QWORD *)v2 + 3), a2);
v2 = (_DWORD *)*((_QWORD *)v2 + 4);
}
}
v4 = (const char *)*((_QWORD *)v2 + 2);
if ( v4 )
locate_dependencies(v4, a2);
}

canonical_decompiled_code:
void __fastcall func6(_DWORD *a1, unsigned int a2)
{
_DWORD *v2; // rbx
int i; // eax
const char *v4; // rdi

v2 = a1;
for ( i = *a1; *v2 != 4; i = *v2 )
{
if ( i == 7 )
{
v2 = (_DWORD *)*((_QWORD *)v2 + 3);
}
else
{
func6(*((_QWORD *)v2 + 3), a2);
v2 = (_DWORD *)*((_QWORD *)v2 + 4);
}
}
v4 = (const char *)*((_QWORD *)v2 + 2);
if ( v4 )
func5(v4, a2);
}

original_code:
void
extract_dependencies (command_t cmd, int thread_id)
{
 if(cmd->type == SIMPLE_COMMAND)
 {
  if(cmd->output)
  {
   locate_dependencies(cmd->output, thread_id);
  }
  return;
 }
 else
 {
  switch(cmd->type)
  {
   case SUBSHELL_COMMAND:
    extract_dependencies(cmd->u.subshell_command,thread_id);
    break;
   default:
    extract_dependencies(cmd->u.command[0],thread_id);
    extract_dependencies(cmd->u.command[1],thread_id);
  }
 }
}

canonical_original_code:
void extract_dependencies(struct command *cmd, int thread_id)
{
 if(cmd->type == SIMPLE_COMMAND)
 {
  if(cmd->output)
  {
   locate_dependencies(cmd->output, thread_id);
  }
  return;
 }
 else
 {
  switch(cmd->type)
  {
   case SUBSHELL_COMMAND:
    extract_dependencies(cmd->u.subshell_command,thread_id);
    break;
   default:
    extract_dependencies(cmd->u.command[0],thread_id);
    extract_dependencies(cmd->u.command[1],thread_id);
  }
 }
}


decompiled_code:
void rmv_dependencies()
{
;
}

canonical_decompiled_code:
void func7()
{
;
}

original_code:
void
rmv_dependencies (int finished_thread_id)
{
}

canonical_original_code:
void rmv_dependencies(int finished_thread_id)
{
}


runnable

decompiled_code:
__int64 __fastcall runnable(int a1)
{
__int64 v1; // rdx

if ( index_counter <= 0 )
return 1LL;
v1 = 0LL;
while ( ((dependency_table[10000 * a1 + v1] + 1) & 0xFFFFFFFD) != 0 )
{
if ( index_counter == ++v1 )
return 1LL;
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1)
{
__int64 v1; // rdx

if ( index_counter <= 0 )
return 1LL;
v1 = 0LL;
while ( ((dependency_table[10000 * a1 + v1] + 1) & 0xFFFFFFFD) != 0 )
{
if ( index_counter == ++v1 )
return 1LL;
}
return 0LL;
}

original_code:
int
runnable (int thread_id)
{
 int i = 0;
 for(; i < index_counter; i++)
 {
  if(dependency_table[thread_id][i] == 1 || dependency_table[thread_id][i] == -1)
   return 0;
 }
 return 1;
}

canonical_original_code:
int runnable(int thread_id)
{
 int i = 0;
 for(; i < index_counter; i++)
 {
  if(dependency_table[thread_id][i] == 1 || dependency_table[thread_id][i] == -1)
   return 0;
 }
 return 1;
}


decompiled_code:
__int64 __fastcall execute_simple_command(__int64 a1)
{
int v2; // eax
const char *v3; // rdi
int v4; // eax
int v5; // ebp
const char *v6; // rdi
int v7; // eax
int v8; // ebp
__int64 result; // rax
int stat_loc; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = fork();
if ( v2 > 0 )
goto LABEL_14;
if ( v2 )
{
LABEL_19:
error(1, 0, "You forked up");
goto LABEL_20;
}
v3 = *(const char **)(a1 + 8);
if ( !v3 )
{
LABEL_7:
v6 = *(const char **)(a1 + 16);
if ( v6 )
{
v7 = open(v6, 577, 438LL);
v8 = v7;
if ( v7 == -1 )
{
error(1, 0, "Output file could not be opened");
}
else if ( dup2(v7, 1) != -1 )
{
close(v8);
goto LABEL_11;
}
error(1, 0, "Output redirect failed");
LABEL_18:
error(1, 0, "Child process failed");
goto LABEL_19;
}
LABEL_11:
execvp(**(const char ***)(a1 + 24), *(char *const **)(a1 + 24));
if ( execvp(**(const char ***)(a1 + 24), *(char *const **)(a1 + 24)) >= 0 )
exit(*(_DWORD *)(a1 + 4));
error(1, 0, "Error in execution of simple command \n");
LABEL_14:
stat_loc = 0;
if ( waitpid(v2, &stat_loc, 0) != -1 )
{
result = BYTE1(stat_loc);
*(_DWORD *)(a1 + 4) = BYTE1(stat_loc);
return result;
}
goto LABEL_18;
}
v4 = open(v3, 0, 438LL);
v5 = v4;
if ( v4 == -1 )
{
LABEL_20:
error(1, 0, "Input file could not be opened");
goto LABEL_21;
}
if ( dup2(v4, 0) != -1 )
{
close(v5);
goto LABEL_7;
}
LABEL_21:
error(1, 0, "Input redirect failed");
return execute_subshell_command();
}

canonical_decompiled_code:
__int64 __fastcall func9(__int64 a1)
{
int v2; // eax
const char *v3; // rdi
int v4; // eax
int v5; // ebp
const char *v6; // rdi
int v7; // eax
int v8; // ebp
__int64 result; // rax
int stat_loc; // [rsp+Ch] [rbp-1Ch] BYREF

v2 = fork();
if ( v2 > 0 )
goto LABEL_14;
if ( v2 )
{
LABEL_19:
error(1, 0, "You forked up");
goto LABEL_20;
}
v3 = *(const char **)(a1 + 8);
if ( !v3 )
{
LABEL_7:
v6 = *(const char **)(a1 + 16);
if ( v6 )
{
v7 = open(v6, 577, 438LL);
v8 = v7;
if ( v7 == -1 )
{
error(1, 0, "Output file could not be opened");
}
else if ( dup2(v7, 1) != -1 )
{
close(v8);
goto LABEL_11;
}
error(1, 0, "Output redirect failed");
LABEL_18:
error(1, 0, "Child process failed");
goto LABEL_19;
}
LABEL_11:
execvp(**(const char ***)(a1 + 24), *(char *const **)(a1 + 24));
if ( execvp(**(const char ***)(a1 + 24), *(char *const **)(a1 + 24)) >= 0 )
exit(*(_DWORD *)(a1 + 4));
error(1, 0, "Error in execution of simple command \n");
LABEL_14:
stat_loc = 0;
if ( waitpid(v2, &stat_loc, 0) != -1 )
{
result = BYTE1(stat_loc);
*(_DWORD *)(a1 + 4) = BYTE1(stat_loc);
return result;
}
goto LABEL_18;
}
v4 = open(v3, 0, 438LL);
v5 = v4;
if ( v4 == -1 )
{
LABEL_20:
error(1, 0, "Input file could not be opened");
goto LABEL_21;
}
if ( dup2(v4, 0) != -1 )
{
close(v5);
goto LABEL_7;
}
LABEL_21:
error(1, 0, "Input redirect failed");
return func10();
}

original_code:
void
execute_simple_command (command_t c)
{
 pid_t pid = fork();
 if(pid > 0)
 {
  int status = 0;
  pid_t test = waitpid(pid,&status,0);
  if(test == -1)
   error(1,0,"Child process failed");
  c->status = (((status) & 0xff00) >> 8);
 }
 else if(pid == 0)
 {
  int fd_in,fd_out;
  if(c->input)
  {
   fd_in = open(c->input, 00, 0666);
   if(fd_in == -1)
    error(1,0,"Input file could not be opened");
   if(dup2(fd_in,0) == -1)
    error(1,0,"Input redirect failed");
   close(fd_in);
  }
  if(c->output)
  {
   fd_out = open(c->output, 01|0100|01000,0666);
   if(fd_out == -1)
    error(1,0,"Output file could not be opened");
   if(dup2(fd_out,1) == -1)
    error(1,0,"Output redirect failed");
   close(fd_out);
  }
  execvp(c->u.word[0],c->u.word);
  if(execvp(c->u.word[0],c->u.word) < 0)
  {
   error(1,0,"Error in execution of simple command \n");
   return;
  }
  exit(c->status);
 }
 else
 {
  error(1,0,"You forked up");
 }
}

canonical_original_code:
void execute_simple_command(struct command *c)
{
 int pid = fork();
 if(pid > 0)
 {
  int status = 0;
  int test = waitpid(pid,&status,0);
  if(test == -1)
   error(1,0,"Child process failed");
  c->status = (((status) & 0xff00) >> 8);
 }
 else if(pid == 0)
 {
  int fd_in; int fd_out;
  if(c->input)
  {
   fd_in = open(c->input, 00, 0666);
   if(fd_in == -1)
    error(1,0,"Input file could not be opened");
   if(dup2(fd_in,0) == -1)
    error(1,0,"Input redirect failed");
   close(fd_in);
  }
  if(c->output)
  {
   fd_out = open(c->output, 01|0100|01000,0666);
   if(fd_out == -1)
    error(1,0,"Output file could not be opened");
   if(dup2(fd_out,1) == -1)
    error(1,0,"Output redirect failed");
   close(fd_out);
  }
  execvp(c->u.word[0],c->u.word);
  if(execvp(c->u.word[0],c->u.word) < 0)
  {
   error(1,0,"Error in execution of simple command \n");
   return;
  }
  exit(c->status);
 }
 else
 {
  error(1,0,"You forked up");
 }
}

variable_types: pid int

variable_types: status int

variable_types: test int


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

do_command(*(_QWORD *)(a1 + 24));
result = *(unsigned int *)(*(_QWORD *)(a1 + 24) + 4LL);
*(_DWORD *)(a1 + 4) = result;
return result;
}

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

func11(*(_QWORD *)(a1 + 24));
result = *(unsigned int *)(*(_QWORD *)(a1 + 24) + 4LL);
*(_DWORD *)(a1 + 4) = result;
return result;
}

original_code:
void
execute_subshell_command (command_t c)
{
 do_command(c->u.subshell_command);
 c->status = c->u.subshell_command->status;
}

canonical_original_code:
void execute_subshell_command(struct command *c)
{
 do_command(c->u.subshell_command);
 c->status = c->u.subshell_command->status;
}


decompiled_code:
__int64 __fastcall do_command(_DWORD *a1)
{
__int64 result; // rax

switch ( *a1 )
{
case 0:
result = execute_and_operator();
break;
case 1:
result = execute_sequence_operator();
break;
case 2:
result = execute_or_operator();
break;
case 3:
result = execute_pipe_operator();
break;
case 4:
result = execute_simple_command((__int64)a1);
break;
case 7:
result = execute_subshell_command((__int64)a1);
break;
default:
error(1, 0, "Command not found. Try again fucker.");
result = execute_command();
break;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func11(_DWORD *a1)
{
__int64 result; // rax

switch ( *a1 )
{
case 0:
result = func14();
break;
case 1:
result = func17();
break;
case 2:
result = func15();
break;
case 3:
result = func16();
break;
case 4:
result = func9((__int64)a1);
break;
case 7:
result = func10((__int64)a1);
break;
default:
error(1, 0, "Command not found. Try again fucker.");
result = func12();
break;
}
return result;
}

original_code:
void
do_command (command_t c)
{
 switch(c->type)
 {
  case AND_COMMAND:
   execute_and_operator(c);
   break;
  case OR_COMMAND:
   execute_or_operator(c);
   break;
  case PIPE_COMMAND:
   execute_pipe_operator(c);
   break;
  case SIMPLE_COMMAND:
   execute_simple_command(c);
   break;
  case SUBSHELL_COMMAND:
   execute_subshell_command(c);
   break;
  case SEQUENCE_COMMAND:
   execute_sequence_operator(c);
   break;
  default:
   error(1,0,"Command not found. Try again fucker.");
   break;
 }
}

canonical_original_code:
void do_command(struct command *c)
{
 switch(c->type)
 {
  case AND_COMMAND:
   execute_and_operator(c);
   break;
  case OR_COMMAND:
   execute_or_operator(c);
   break;
  case PIPE_COMMAND:
   execute_pipe_operator(c);
   break;
  case SIMPLE_COMMAND:
   execute_simple_command(c);
   break;
  case SUBSHELL_COMMAND:
   execute_subshell_command(c);
   break;
  case SEQUENCE_COMMAND:
   execute_sequence_operator(c);
   break;
  default:
   error(1,0,"Command not found. Try again fucker.");
   break;
 }
}