writeArith

decompiled_code:
int __fastcall writeArith(char *s2)
{
__int64 i; // rbx
int v2; // ebp
int result; // eax
const char *v4; // rbx
size_t v5; // rax
const char *v6; // rbx
size_t v7; // rax
const char *v8; // rbx
size_t v9; // rax
char *s1; // [rsp+0h] [rbp-A8h]
char *s[20]; // [rsp+8h] [rbp-A0h]

s1 = (char *)&unk_E08;
s[0] = "D=D+M\n";
s[1] = "sub";
s[2] = "D=D-M\n";
s[3] = "or";
s[4] = "D=D|M\n";
s[5] = "and";
s[6] = "D=D&M\n";
s[7] = "not";
s[8] = "M=!M\n";
s[9] = "neg";
s[10] = "M=-M\n";
s[11] = "eq";
s[12] = "D;JEQ\n";
s[13] = "gt";
s[14] = "D;JLT\n";
s[15] = "lt";
s[16] = "D;JGT\n";
for ( i = 0LL; i != 9; ++i )
{
v2 = i;
result = strcmp((&s1)[2 * i], s2);
if ( !result )
break;
v2 = i + 1;
}
if ( v2 > 5 )
{
result = v2 - 6;
if ( (unsigned int)(v2 - 6) <= 2 )
{
fwrite("@SP\nAM=M-1\nD=M\nA=A-1\nM=D-M\n@CMP", 1uLL, 0x1DuLL, vm_ofp);
writeNum(vm_lbln);
fwrite("\nD=A\n@SP\nA=M\nM=D\nA=A-1\nD=M\n@LCMP\n", 1uLL, 0x21uLL, vm_ofp);
v6 = s[2 * v2];
v7 = strlen(v6);
fwrite(v6, 1uLL, v7, vm_ofp);
fwrite("@SP\nA=M-1\nM=0\n(CMP", 1uLL, 0x10uLL, vm_ofp);
writeNum(vm_lbln);
result = fwrite(")\n", 1uLL, 2uLL, vm_ofp);
++vm_lbln;
}
}
else if ( v2 > 3 )
{
fwrite("@SP\nA=M-1\n", 1uLL, 0xAuLL, vm_ofp);
v8 = s[2 * v2];
v9 = strlen(v8);
result = fwrite(v8, 1uLL, v9, vm_ofp);
}
else if ( (unsigned int)v2 <= 3 )
{
fwrite("@SP\nM=M-1\nA=M-1\nD=M\nA=A+1\n", 1uLL, 0x1AuLL, vm_ofp);
v4 = s[2 * v2];
v5 = strlen(v4);
fwrite(v4, 1uLL, v5, vm_ofp);
result = fwrite("A=A-1\nM=D\n", 1uLL, 0xAuLL, vm_ofp);
}
return result;
}

canonical_decompiled_code:
int __fastcall func1(char *s2)
{
__int64 i; // rbx
int v2; // ebp
int result; // eax
const char *v4; // rbx
size_t v5; // rax
const char *v6; // rbx
size_t v7; // rax
const char *v8; // rbx
size_t v9; // rax
char *s1; // [rsp+0h] [rbp-A8h]
char *s[20]; // [rsp+8h] [rbp-A0h]

s1 = (char *)&unk_E08;
s[0] = "D=D+M\n";
s[1] = "sub";
s[2] = "D=D-M\n";
s[3] = "or";
s[4] = "D=D|M\n";
s[5] = "and";
s[6] = "D=D&M\n";
s[7] = "not";
s[8] = "M=!M\n";
s[9] = "neg";
s[10] = "M=-M\n";
s[11] = "eq";
s[12] = "D;JEQ\n";
s[13] = "gt";
s[14] = "D;JLT\n";
s[15] = "lt";
s[16] = "D;JGT\n";
for ( i = 0LL; i != 9; ++i )
{
v2 = i;
result = strcmp((&s1)[2 * i], s2);
if ( !result )
break;
v2 = i + 1;
}
if ( v2 > 5 )
{
result = v2 - 6;
if ( (unsigned int)(v2 - 6) <= 2 )
{
fwrite("@SP\nAM=M-1\nD=M\nA=A-1\nM=D-M\n@CMP", 1uLL, 0x1DuLL, vm_ofp);
func0(vm_lbln);
fwrite("\nD=A\n@SP\nA=M\nM=D\nA=A-1\nD=M\n@LCMP\n", 1uLL, 0x21uLL, vm_ofp);
v6 = s[2 * v2];
v7 = strlen(v6);
fwrite(v6, 1uLL, v7, vm_ofp);
fwrite("@SP\nA=M-1\nM=0\n(CMP", 1uLL, 0x10uLL, vm_ofp);
func0(vm_lbln);
result = fwrite(")\n", 1uLL, 2uLL, vm_ofp);
++vm_lbln;
}
}
else if ( v2 > 3 )
{
fwrite("@SP\nA=M-1\n", 1uLL, 0xAuLL, vm_ofp);
v8 = s[2 * v2];
v9 = strlen(v8);
result = fwrite(v8, 1uLL, v9, vm_ofp);
}
else if ( (unsigned int)v2 <= 3 )
{
fwrite("@SP\nM=M-1\nA=M-1\nD=M\nA=A+1\n", 1uLL, 0x1AuLL, vm_ofp);
v4 = s[2 * v2];
v5 = strlen(v4);
fwrite(v4, 1uLL, v5, vm_ofp);
result = fwrite("A=A-1\nM=D\n", 1uLL, 0xAuLL, vm_ofp);
}
return result;
}

original_code:
void writeArith(char* cmd)
{
 const struct {
  char* addr;
  char* hasm;
 } Dict[] = {
  {"add", "D=D+M\n"},
  {"sub", "D=D-M\n"},
  {"or", "D=D|M\n"},
  {"and", "D=D&M\n"},
  {"not", "M=!M\n"},
  {"neg", "M=-M\n"},
  {"eq", "D;JEQ\n"},
  {"gt", "D;JLT\n"},
  {"lt", "D;JGT\n"}
 };
 int i;
 for(i=0; i<9; i++)
 {
  if(strcmp(Dict[i].addr, cmd) == 0)
   break;
 }
 switch(i)
 {
  case 0:
  case 1:
  case 2:
  case 3:
   fwrite("@SP\nM=M-1\nA=M-1\nD=M\nA=A+1\n", 1, 26, _vm_ofp);
   fwrite(Dict[i].hasm, 1, strlen(Dict[i].hasm), _vm_ofp);
   fwrite("A=A-1\nM=D\n", 1, 10, _vm_ofp);
   break;
  case 4:
  case 5:
   fwrite("@SP\nA=M-1\n", 1, 10, _vm_ofp);
   fwrite(Dict[i].hasm, 1, strlen(Dict[i].hasm), _vm_ofp);
   break;
  case 6:
  case 7:
  case 8:
   fwrite("@SP\nAM=M-1\nD=M\nA=A-1\nM=D-M\n@CMP", 1, 29, _vm_ofp);
   writeNum(_vm_lbln);
   fwrite("\nD=A\n@SP\nA=M\nM=D\nA=A-1\nD=M\n@LCMP\n", 1, 33, _vm_ofp);
   fwrite(Dict[i].hasm, 1, strlen(Dict[i].hasm), _vm_ofp);
   fwrite("@SP\nA=M-1\nM=0\n(CMP", 1, 16, _vm_ofp);
   writeNum(_vm_lbln);
   fwrite(")\n", 1, 2, _vm_ofp);
   _vm_lbln++;
   break;
  default:
   return;
 }
}

canonical_original_code:
void writeArith(char *cmd)
{
 struct { char *addr; char *hasm; } Dict[] = {
  {"add", "D=D+M\n"},
  {"sub", "D=D-M\n"},
  {"or", "D=D|M\n"},
  {"and", "D=D&M\n"},
  {"not", "M=!M\n"},
  {"neg", "M=-M\n"},
  {"eq", "D;JEQ\n"},
  {"gt", "D;JLT\n"},
  {"lt", "D;JGT\n"}
 };
 int i;
 for(i=0; i<9; i++)
 {
  if(strcmp(Dict[i].addr, cmd) == 0)
   break;
 }
 switch(i)
 {
  case 0:
  case 1:
  case 2:
  case 3:
   fwrite("@SP\nM=M-1\nA=M-1\nD=M\nA=A+1\n", 1, 26, _vm_ofp);
   fwrite(Dict[i].hasm, 1, strlen(Dict[i].hasm), _vm_ofp);
   fwrite("A=A-1\nM=D\n", 1, 10, _vm_ofp);
   break;
  case 4:
  case 5:
   fwrite("@SP\nA=M-1\n", 1, 10, _vm_ofp);
   fwrite(Dict[i].hasm, 1, strlen(Dict[i].hasm), _vm_ofp);
   break;
  case 6:
  case 7:
  case 8:
   fwrite("@SP\nAM=M-1\nD=M\nA=A-1\nM=D-M\n@CMP", 1, 29, _vm_ofp);
   writeNum(_vm_lbln);
   fwrite("\nD=A\n@SP\nA=M\nM=D\nA=A-1\nD=M\n@LCMP\n", 1, 33, _vm_ofp);
   fwrite(Dict[i].hasm, 1, strlen(Dict[i].hasm), _vm_ofp);
   fwrite("@SP\nA=M-1\nM=0\n(CMP", 1, 16, _vm_ofp);
   writeNum(_vm_lbln);
   fwrite(")\n", 1, 2, _vm_ofp);
   _vm_lbln++;
   break;
  default:
   return;
 }
}

variable_types:
cmd
char *

variable_types:
Dict
struct { char *addr; char *hasm; } []


writePush

repo: lijnk/vmtrans

decompiled_code:
int __fastcall writePush(char *s2, int a2)
{
__int64 i; // rbx
int v3; // ebp
const char *v4; // rbx
size_t v5; // rax
const char *v7; // rbx
size_t v8; // rax
char *v9; // rbx
size_t v10; // rax
char *s1; // [rsp+0h] [rbp-A8h]
char *s[20]; // [rsp+8h] [rbp-A0h]

s1 = "local";
s[0] = "@LCL\n";
s[1] = "argument";
s[2] = "@ARG\n";
s[3] = "this";
s[4] = "@THIS\n";
s[5] = "that";
s[6] = "@THAT\n";
s[7] = "pointer";
s[8] = "@THIS\n";
s[9] = "temp";
s[10] = "@5\n";
s[11] = "static";
s[12] = (char *)&unk_EF7;
s[13] = "constant";
s[14] = (char *)&unk_EF7;
for ( i = 0LL; i != 8; ++i )
{
v3 = i;
if ( !strcmp((&s1)[2 * i], s2) )
break;
v3 = i + 1;
}
if ( v3 == 6 )
{
fwrite("@", 1uLL, 1uLL, vm_ofp);
v9 = vm_sv;
v10 = strlen(vm_sv);
fwrite(v9, 1uLL, v10, vm_ofp);
fwrite(".", 1uLL, 1uLL, vm_ofp);
writeNum(a2);
fwrite("\nD=M\n", 1uLL, 5uLL, vm_ofp);
return fwrite("@SP\nM=M+1\nA=M-1\nM=D\n", 1uLL, 0x14uLL, vm_ofp);
}
if ( v3 > 6 )
{
if ( v3 == 7 )
{
fwrite("@", 1uLL, 1uLL, vm_ofp);
writeNum(a2);
fwrite("\nD=A\n", 1uLL, 5uLL, vm_ofp);
return fwrite("@SP\nM=M+1\nA=M-1\nM=D\n", 1uLL, 0x14uLL, vm_ofp);
}
}
else
{
if ( v3 > 3 )
{
v4 = s[2 * v3];
v5 = strlen(v4);
fwrite(v4, 1uLL, v5, vm_ofp);
fwrite("D=A\n@", 1uLL, 5uLL, vm_ofp);
writeNum(a2);
fwrite("\nA=D+A\nD=M\n", 1uLL, 0xBuLL, vm_ofp);
return fwrite("@SP\nM=M+1\nA=M-1\nM=D\n", 1uLL, 0x14uLL, vm_ofp);
}
if ( v3 >= 0 )
{
v7 = s[2 * v3];
v8 = strlen(v7);
fwrite(v7, 1uLL, v8, vm_ofp);
fwrite("D=M\n@", 1uLL, 5uLL, vm_ofp);
writeNum(a2);
fwrite("\nA=D+A\nD=M\n", 1uLL, 0xBuLL, vm_ofp);
return fwrite("@SP\nM=M+1\nA=M-1\nM=D\n", 1uLL, 0x14uLL, vm_ofp);
}
}
return printf("Unknown push segment: \"%s\"\n", s2);
}

canonical_decompiled_code:
int __fastcall func2(char *s2, int a2)
{
__int64 i; // rbx
int v3; // ebp
const char *v4; // rbx
size_t v5; // rax
const char *v7; // rbx
size_t v8; // rax
char *v9; // rbx
size_t v10; // rax
char *s1; // [rsp+0h] [rbp-A8h]
char *s[20]; // [rsp+8h] [rbp-A0h]

s1 = "local";
s[0] = "@LCL\n";
s[1] = "argument";
s[2] = "@ARG\n";
s[3] = "this";
s[4] = "@THIS\n";
s[5] = "that";
s[6] = "@THAT\n";
s[7] = "pointer";
s[8] = "@THIS\n";
s[9] = "temp";
s[10] = "@5\n";
s[11] = "static";
s[12] = (char *)&unk_EF7;
s[13] = "constant";
s[14] = (char *)&unk_EF7;
for ( i = 0LL; i != 8; ++i )
{
v3 = i;
if ( !strcmp((&s1)[2 * i], s2) )
break;
v3 = i + 1;
}
if ( v3 == 6 )
{
fwrite("@", 1uLL, 1uLL, vm_ofp);
v9 = vm_sv;
v10 = strlen(vm_sv);
fwrite(v9, 1uLL, v10, vm_ofp);
fwrite(".", 1uLL, 1uLL, vm_ofp);
func0(a2);
fwrite("\nD=M\n", 1uLL, 5uLL, vm_ofp);
return fwrite("@SP\nM=M+1\nA=M-1\nM=D\n", 1uLL, 0x14uLL, vm_ofp);
}
if ( v3 > 6 )
{
if ( v3 == 7 )
{
fwrite("@", 1uLL, 1uLL, vm_ofp);
func0(a2);
fwrite("\nD=A\n", 1uLL, 5uLL, vm_ofp);
return fwrite("@SP\nM=M+1\nA=M-1\nM=D\n", 1uLL, 0x14uLL, vm_ofp);
}
}
else
{
if ( v3 > 3 )
{
v4 = s[2 * v3];
v5 = strlen(v4);
fwrite(v4, 1uLL, v5, vm_ofp);
fwrite("D=A\n@", 1uLL, 5uLL, vm_ofp);
func0(a2);
fwrite("\nA=D+A\nD=M\n", 1uLL, 0xBuLL, vm_ofp);
return fwrite("@SP\nM=M+1\nA=M-1\nM=D\n", 1uLL, 0x14uLL, vm_ofp);
}
if ( v3 >= 0 )
{
v7 = s[2 * v3];
v8 = strlen(v7);
fwrite(v7, 1uLL, v8, vm_ofp);
fwrite("D=M\n@", 1uLL, 5uLL, vm_ofp);
func0(a2);
fwrite("\nA=D+A\nD=M\n", 1uLL, 0xBuLL, vm_ofp);
return fwrite("@SP\nM=M+1\nA=M-1\nM=D\n", 1uLL, 0x14uLL, vm_ofp);
}
}
return printf("Unknown push segment: \"%s\"\n", s2);
}

original_code:
void writePush(char* seg, int index)
{
 const struct {
  char* addr;
  char* hasm;
 } Dict[] = {
  {"local", "@LCL\n"},
  {"argument", "@ARG\n"},
  {"this", "@THIS\n"},
  {"that", "@THAT\n"},
  {"pointer", "@THIS\n"},
  {"temp", "@5\n"},
  {"static", ""},
  {"constant", ""},
 };
 int i;
 for(i=0; i<8; i++)
 {
  if(strcmp(Dict[i].addr, seg) == 0)
   break;
 }
 switch(i)
 {
  case 0:
  case 1:
  case 2:
  case 3:
   fwrite(Dict[i].hasm, 1, strlen(Dict[i].hasm), _vm_ofp);
   fwrite("D=M\n@", 1, 5, _vm_ofp);
   writeNum(index);
   fwrite("\nA=D+A\nD=M\n", 1, 11, _vm_ofp);
   break;
  case 4:
  case 5:
   fwrite(Dict[i].hasm, 1, strlen(Dict[i].hasm), _vm_ofp);
   fwrite("D=A\n@", 1, 5, _vm_ofp);
   writeNum(index);
   fwrite("\nA=D+A\nD=M\n", 1, 11, _vm_ofp);
   break;
  case 6:
   fwrite("@", 1, 1, _vm_ofp);
   fwrite(_vm_sv, 1, strlen(_vm_sv), _vm_ofp);
   fwrite(".", 1, 1, _vm_ofp);
   writeNum(index);
   fwrite("\nD=M\n", 1, 5, _vm_ofp);
   break;
  case 7:
   fwrite("@", 1, 1, _vm_ofp);
   writeNum(index);
   fwrite("\nD=A\n", 1, 5, _vm_ofp);
   break;
  default:
   printf("Unknown push segment: \"%s\"\n", seg);
   return;
 }
 fwrite("@SP\nM=M+1\nA=M-1\nM=D\n", 1, 20, _vm_ofp);
}

canonical_original_code:
void writePush(char *seg, int index)
{
 struct { char *addr; char *hasm; } Dict[] = {
  {"local", "@LCL\n"},
  {"argument", "@ARG\n"},
  {"this", "@THIS\n"},
  {"that", "@THAT\n"},
  {"pointer", "@THIS\n"},
  {"temp", "@5\n"},
  {"static", ""},
  {"constant", ""},
 };
 int i;
 for(i=0; i<8; i++)
 {
  if(strcmp(Dict[i].addr, seg) == 0)
   break;
 }
 switch(i)
 {
  case 0:
  case 1:
  case 2:
  case 3:
   fwrite(Dict[i].hasm, 1, strlen(Dict[i].hasm), _vm_ofp);
   fwrite("D=M\n@", 1, 5, _vm_ofp);
   writeNum(index);
   fwrite("\nA=D+A\nD=M\n", 1, 11, _vm_ofp);
   break;
  case 4:
  case 5:
   fwrite(Dict[i].hasm, 1, strlen(Dict[i].hasm), _vm_ofp);
   fwrite("D=A\n@", 1, 5, _vm_ofp);
   writeNum(index);
   fwrite("\nA=D+A\nD=M\n", 1, 11, _vm_ofp);
   break;
  case 6:
   fwrite("@", 1, 1, _vm_ofp);
   fwrite(_vm_sv, 1, strlen(_vm_sv), _vm_ofp);
   fwrite(".", 1, 1, _vm_ofp);
   writeNum(index);
   fwrite("\nD=M\n", 1, 5, _vm_ofp);
   break;
  case 7:
   fwrite("@", 1, 1, _vm_ofp);
   writeNum(index);
   fwrite("\nD=A\n", 1, 5, _vm_ofp);
   break;
  default:
   printf("Unknown push segment: \"%s\"\n", seg);
   return;
 }
 fwrite("@SP\nM=M+1\nA=M-1\nM=D\n", 1, 20, _vm_ofp);
}

variable_types:
seg
char *

variable_types: index int

variable_types: i int

return_type: void


writePop

decompiled_code:
int __fastcall writePop(char *s2, int a2)
{
__int64 i; // rbx
int v3; // ebp
const char *v4; // rbx
size_t v5; // rax
char *v7; // rbx
size_t v8; // rax
const char *v9; // rbx
size_t v10; // rax
char *s1; // [rsp+0h] [rbp-98h]
char *s[18]; // [rsp+8h] [rbp-90h]

s1 = "local";
s[0] = "@LCL\n";
s[1] = "argument";
s[2] = "@ARG\n";
s[3] = "this";
s[4] = "@THIS\n";
s[5] = "that";
s[6] = "@THAT\n";
s[7] = "pointer";
s[8] = "@THIS\n";
s[9] = "temp";
s[10] = "@5\n";
s[11] = "static";
s[12] = (char *)&unk_EF7;
for ( i = 0LL; i != 7; ++i )
{
v3 = i;
if ( !strcmp((&s1)[2 * i], s2) )
break;
v3 = i + 1;
}
if ( v3 > 5 )
{
if ( v3 == 6 )
{
fwrite("@SP\nAM=M-1\nD=M\n@", 1uLL, 0x10uLL, vm_ofp);
v7 = vm_sv;
v8 = strlen(vm_sv);
fwrite(v7, 1uLL, v8, vm_ofp);
fwrite(".", 1uLL, 1uLL, vm_ofp);
writeNum(a2);
return fwrite("\nM=D\n", 1uLL, 5uLL, vm_ofp);
}
}
else
{
if ( v3 > 3 )
{
v9 = s[2 * v3];
v10 = strlen(v9);
fwrite(v9, 1uLL, v10, vm_ofp);
fwrite("D=A\n@", 1uLL, 5uLL, vm_ofp);
writeNum(a2);
fwrite("\nD=D+A\n", 1uLL, 7uLL, vm_ofp);
return fwrite("@SP\nM=M-1\nA=M+1\nM=D\nA=A-1\nD=M\nA=A+1\nA=M\nM=D\n", 1uLL, 0x2CuLL, vm_ofp);
}
if ( (unsigned int)v3 <= 3 )
{
v4 = s[2 * v3];
v5 = strlen(v4);
fwrite(v4, 1uLL, v5, vm_ofp);
fwrite("D=M\n@", 1uLL, 5uLL, vm_ofp);
writeNum(a2);
fwrite("\nD=D+A\n", 1uLL, 7uLL, vm_ofp);
return fwrite("@SP\nM=M-1\nA=M+1\nM=D\nA=A-1\nD=M\nA=A+1\nA=M\nM=D\n", 1uLL, 0x2CuLL, vm_ofp);
}
}
return printf("Unknown pop segment: \"%s\"\n", s2);
}

canonical_decompiled_code:
int __fastcall func3(char *s2, int a2)
{
__int64 i; // rbx
int v3; // ebp
const char *v4; // rbx
size_t v5; // rax
char *v7; // rbx
size_t v8; // rax
const char *v9; // rbx
size_t v10; // rax
char *s1; // [rsp+0h] [rbp-98h]
char *s[18]; // [rsp+8h] [rbp-90h]

s1 = "local";
s[0] = "@LCL\n";
s[1] = "argument";
s[2] = "@ARG\n";
s[3] = "this";
s[4] = "@THIS\n";
s[5] = "that";
s[6] = "@THAT\n";
s[7] = "pointer";
s[8] = "@THIS\n";
s[9] = "temp";
s[10] = "@5\n";
s[11] = "static";
s[12] = (char *)&unk_EF7;
for ( i = 0LL; i != 7; ++i )
{
v3 = i;
if ( !strcmp((&s1)[2 * i], s2) )
break;
v3 = i + 1;
}
if ( v3 > 5 )
{
if ( v3 == 6 )
{
fwrite("@SP\nAM=M-1\nD=M\n@", 1uLL, 0x10uLL, vm_ofp);
v7 = vm_sv;
v8 = strlen(vm_sv);
fwrite(v7, 1uLL, v8, vm_ofp);
fwrite(".", 1uLL, 1uLL, vm_ofp);
func0(a2);
return fwrite("\nM=D\n", 1uLL, 5uLL, vm_ofp);
}
}
else
{
if ( v3 > 3 )
{
v9 = s[2 * v3];
v10 = strlen(v9);
fwrite(v9, 1uLL, v10, vm_ofp);
fwrite("D=A\n@", 1uLL, 5uLL, vm_ofp);
func0(a2);
fwrite("\nD=D+A\n", 1uLL, 7uLL, vm_ofp);
return fwrite("@SP\nM=M-1\nA=M+1\nM=D\nA=A-1\nD=M\nA=A+1\nA=M\nM=D\n", 1uLL, 0x2CuLL, vm_ofp);
}
if ( (unsigned int)v3 <= 3 )
{
v4 = s[2 * v3];
v5 = strlen(v4);
fwrite(v4, 1uLL, v5, vm_ofp);
fwrite("D=M\n@", 1uLL, 5uLL, vm_ofp);
func0(a2);
fwrite("\nD=D+A\n", 1uLL, 7uLL, vm_ofp);
return fwrite("@SP\nM=M-1\nA=M+1\nM=D\nA=A-1\nD=M\nA=A+1\nA=M\nM=D\n", 1uLL, 0x2CuLL, vm_ofp);
}
}
return printf("Unknown pop segment: \"%s\"\n", s2);
}

original_code:
void writePop(char* seg, int index)
{
 const struct {
  char* addr;
  char* hasm;
 } Dict[] = {
  {"local", "@LCL\n"},
  {"argument", "@ARG\n"},
  {"this", "@THIS\n"},
  {"that", "@THAT\n"},
  {"pointer", "@THIS\n"},
  {"temp", "@5\n"},
  {"static", ""}
 };
 int i;
 for(i=0; i<7; i++)
 {
  if(strcmp(Dict[i].addr, seg) == 0)
   break;
 }
 switch(i)
 {
  case 0:
  case 1:
  case 2:
  case 3:
   fwrite(Dict[i].hasm, 1, strlen(Dict[i].hasm), _vm_ofp);
   fwrite("D=M\n@", 1, 5, _vm_ofp);
   writeNum(index);
   fwrite("\nD=D+A\n", 1, 7, _vm_ofp);
   break;
  case 4:
  case 5:
   fwrite(Dict[i].hasm, 1, strlen(Dict[i].hasm), _vm_ofp);
   fwrite("D=A\n@", 1, 5, _vm_ofp);
   writeNum(index);
   fwrite("\nD=D+A\n", 1, 7, _vm_ofp);
   break;
  case 6:
   fwrite("@SP\nAM=M-1\nD=M\n@", 1, 16, _vm_ofp);
   fwrite(_vm_sv, 1, strlen(_vm_sv), _vm_ofp);
   fwrite(".", 1, 1, _vm_ofp);
   writeNum(index);
   fwrite("\nM=D\n", 1, 5, _vm_ofp);
   return;
  default:
   printf("Unknown pop segment: \"%s\"\n", seg);
   return;
 }
 fwrite("@SP\nM=M-1\nA=M+1\nM=D\nA=A-1\nD=M\nA=A+1\nA=M\nM=D\n", 1, 44, _vm_ofp);
}

canonical_original_code:
void writePop(char *seg, int index)
{
 struct { char *addr; char *hasm; } Dict[] = {
  {"local", "@LCL\n"},
  {"argument", "@ARG\n"},
  {"this", "@THIS\n"},
  {"that", "@THAT\n"},
  {"pointer", "@THIS\n"},
  {"temp", "@5\n"},
  {"static", ""}
 };
 int i;
 for(i=0; i<7; i++)
 {
  if(strcmp(Dict[i].addr, seg) == 0)
   break;
 }
 switch(i)
 {
  case 0:
  case 1:
  case 2:
  case 3:
   fwrite(Dict[i].hasm, 1, strlen(Dict[i].hasm), _vm_ofp);
   fwrite("D=M\n@", 1, 5, _vm_ofp);
   writeNum(index);
   fwrite("\nD=D+A\n", 1, 7, _vm_ofp);
   break;
  case 4:
  case 5:
   fwrite(Dict[i].hasm, 1, strlen(Dict[i].hasm), _vm_ofp);
   fwrite("D=A\n@", 1, 5, _vm_ofp);
   writeNum(index);
   fwrite("\nD=D+A\n", 1, 7, _vm_ofp);
   break;
  case 6:
   fwrite("@SP\nAM=M-1\nD=M\n@", 1, 16, _vm_ofp);
   fwrite(_vm_sv, 1, strlen(_vm_sv), _vm_ofp);
   fwrite(".", 1, 1, _vm_ofp);
   writeNum(index);
   fwrite("\nM=D\n", 1, 5, _vm_ofp);
   return;
  default:
   printf("Unknown pop segment: \"%s\"\n", seg);
   return;
 }
 fwrite("@SP\nM=M-1\nA=M+1\nM=D\nA=A-1\nD=M\nA=A+1\nA=M\nM=D\n", 1, 44, _vm_ofp);
}


writeLabel

decompiled_code:
size_t __fastcall writeLabel(char *s)
{
char *v1; // rbp
size_t v2; // rax
size_t v3; // rax

fwrite("(", 1uLL, 1uLL, vm_ofp);
v1 = vm_fcn;
v2 = strlen(vm_fcn);
fwrite(v1, 1uLL, v2, vm_ofp);
fwrite("$", 1uLL, 1uLL, vm_ofp);
v3 = strlen(s);
fwrite(s, 1uLL, v3, vm_ofp);
return fwrite(")\n", 1uLL, 2uLL, vm_ofp);
}

canonical_decompiled_code:
size_t __fastcall func4(char *s)
{
char *v1; // rbp
size_t v2; // rax
size_t v3; // rax

fwrite("(", 1uLL, 1uLL, vm_ofp);
v1 = vm_fcn;
v2 = strlen(vm_fcn);
fwrite(v1, 1uLL, v2, vm_ofp);
fwrite("$", 1uLL, 1uLL, vm_ofp);
v3 = strlen(s);
fwrite(s, 1uLL, v3, vm_ofp);
return fwrite(")\n", 1uLL, 2uLL, vm_ofp);
}

original_code:
void writeLabel(char* lab)
{
 fwrite("(", 1, 1, _vm_ofp);
 fwrite(_vm_fcn, 1, strlen(_vm_fcn), _vm_ofp);
 fwrite("$", 1, 1, _vm_ofp);
 fwrite(lab, 1, strlen(lab), _vm_ofp);
 fwrite(")\n", 1, 2, _vm_ofp);
}

canonical_original_code:
void writeLabel(char *lab)
{
 fwrite("(", 1, 1, _vm_ofp);
 fwrite(_vm_fcn, 1, strlen(_vm_fcn), _vm_ofp);
 fwrite("$", 1, 1, _vm_ofp);
 fwrite(lab, 1, strlen(lab), _vm_ofp);
 fwrite(")\n", 1, 2, _vm_ofp);
}

variable_types:
lab
char *


writeGoto

decompiled_code:
size_t __fastcall writeGoto(char *s)
{
char *v1; // rbp
size_t v2; // rax
size_t v3; // rax

fwrite("@", 1uLL, 1uLL, vm_ofp);
v1 = vm_fcn;
v2 = strlen(vm_fcn);
fwrite(v1, 1uLL, v2, vm_ofp);
fwrite("$", 1uLL, 1uLL, vm_ofp);
v3 = strlen(s);
fwrite(s, 1uLL, v3, vm_ofp);
return fwrite("\n0;JMP\n", 1uLL, 7uLL, vm_ofp);
}

canonical_decompiled_code:
size_t __fastcall func5(char *s)
{
char *v1; // rbp
size_t v2; // rax
size_t v3; // rax

fwrite("@", 1uLL, 1uLL, vm_ofp);
v1 = vm_fcn;
v2 = strlen(vm_fcn);
fwrite(v1, 1uLL, v2, vm_ofp);
fwrite("$", 1uLL, 1uLL, vm_ofp);
v3 = strlen(s);
fwrite(s, 1uLL, v3, vm_ofp);
return fwrite("\n0;JMP\n", 1uLL, 7uLL, vm_ofp);
}

original_code:
void writeGoto(char* lab)
{
 fwrite("@", 1, 1, _vm_ofp);
 fwrite(_vm_fcn, 1, strlen(_vm_fcn), _vm_ofp);
 fwrite("$", 1, 1, _vm_ofp);
 fwrite(lab, 1, strlen(lab), _vm_ofp);
 fwrite("\n0;JMP\n", 1, 7, _vm_ofp);
}

canonical_original_code:
void writeGoto(char *lab)
{
 fwrite("@", 1, 1, _vm_ofp);
 fwrite(_vm_fcn, 1, strlen(_vm_fcn), _vm_ofp);
 fwrite("$", 1, 1, _vm_ofp);
 fwrite(lab, 1, strlen(lab), _vm_ofp);
 fwrite("\n0;JMP\n", 1, 7, _vm_ofp);
}


writeIf

decompiled_code:
size_t __fastcall writeIf(char *s)
{
char *v1; // rbp
size_t v2; // rax
size_t v3; // rax

fwrite("@SP\nAM=M-1\nD=M\n@", 1uLL, 0x10uLL, vm_ofp);
v1 = vm_fcn;
v2 = strlen(vm_fcn);
fwrite(v1, 1uLL, v2, vm_ofp);
fwrite("$", 1uLL, 1uLL, vm_ofp);
v3 = strlen(s);
fwrite(s, 1uLL, v3, vm_ofp);
return fwrite("\nD;JNE\n", 1uLL, 7uLL, vm_ofp);
}

canonical_decompiled_code:
size_t __fastcall func6(char *s)
{
char *v1; // rbp
size_t v2; // rax
size_t v3; // rax

fwrite("@SP\nAM=M-1\nD=M\n@", 1uLL, 0x10uLL, vm_ofp);
v1 = vm_fcn;
v2 = strlen(vm_fcn);
fwrite(v1, 1uLL, v2, vm_ofp);
fwrite("$", 1uLL, 1uLL, vm_ofp);
v3 = strlen(s);
fwrite(s, 1uLL, v3, vm_ofp);
return fwrite("\nD;JNE\n", 1uLL, 7uLL, vm_ofp);
}

original_code:
void writeIf(char* lab)
{
 fwrite("@SP\nAM=M-1\nD=M\n@", 1, 16, _vm_ofp);
 fwrite(_vm_fcn, 1, strlen(_vm_fcn), _vm_ofp);
 fwrite("$", 1, 1, _vm_ofp);
 fwrite(lab, 1, strlen(lab), _vm_ofp);
 fwrite("\nD;JNE\n", 1, 7, _vm_ofp);
}

canonical_original_code:
void writeIf(char *lab)
{
 fwrite("@SP\nAM=M-1\nD=M\n@", 1, 16, _vm_ofp);
 fwrite(_vm_fcn, 1, strlen(_vm_fcn), _vm_ofp);
 fwrite("$", 1, 1, _vm_ofp);
 fwrite(lab, 1, strlen(lab), _vm_ofp);
 fwrite("\nD;JNE\n", 1, 7, _vm_ofp);
}


writeCall

decompiled_code:
size_t __fastcall writeCall(char *s, int a2)
{
size_t v2; // rax
size_t result; // rax

fwrite("@LC", 1uLL, 3uLL, vm_ofp);
writeNum(vm_lcn);
fwrite("\nD=A\n@SP\nM=M+1\nA=M-1\nM=D\n", 1uLL, 0x19uLL, vm_ofp);
fwrite("@LCL\nD=M\n@SP\nM=M+1\nA=M-1\nM=D\n", 1uLL, 0x1DuLL, vm_ofp);
fwrite("@ARG\nD=M\n@SP\nM=M+1\nA=M-1\nM=D\n", 1uLL, 0x1DuLL, vm_ofp);
fwrite("@THIS\nD=M\n@SP\nM=M+1\nA=M-1\nM=D\n", 1uLL, 0x1EuLL, vm_ofp);
fwrite("@THAT\nD=M\n@SP\nM=M+1\nA=M-1\nM=D\n", 1uLL, 0x1EuLL, vm_ofp);
fwrite("@SP\nD=M\n@", 1uLL, 9uLL, vm_ofp);
writeNum(a2);
fwrite("\nD=D-A\n@5\nD=D-A\n@ARG\nM=D\n", 1uLL, 0x19uLL, vm_ofp);
fwrite("@SP\nD=M\n@LCL\nM=D\n@", 1uLL, 0x12uLL, vm_ofp);
v2 = strlen(s);
fwrite(s, 1uLL, v2, vm_ofp);
fwrite("\n0;JMP\n(LC", 1uLL, 0xAuLL, vm_ofp);
writeNum(vm_lcn);
result = fwrite(")\n", 1uLL, 2uLL, vm_ofp);
++vm_lcn;
return result;
}

canonical_decompiled_code:
size_t __fastcall func7(char *s, int a2)
{
size_t v2; // rax
size_t result; // rax

fwrite("@LC", 1uLL, 3uLL, vm_ofp);
func0(vm_lcn);
fwrite("\nD=A\n@SP\nM=M+1\nA=M-1\nM=D\n", 1uLL, 0x19uLL, vm_ofp);
fwrite("@LCL\nD=M\n@SP\nM=M+1\nA=M-1\nM=D\n", 1uLL, 0x1DuLL, vm_ofp);
fwrite("@ARG\nD=M\n@SP\nM=M+1\nA=M-1\nM=D\n", 1uLL, 0x1DuLL, vm_ofp);
fwrite("@THIS\nD=M\n@SP\nM=M+1\nA=M-1\nM=D\n", 1uLL, 0x1EuLL, vm_ofp);
fwrite("@THAT\nD=M\n@SP\nM=M+1\nA=M-1\nM=D\n", 1uLL, 0x1EuLL, vm_ofp);
fwrite("@SP\nD=M\n@", 1uLL, 9uLL, vm_ofp);
func0(a2);
fwrite("\nD=D-A\n@5\nD=D-A\n@ARG\nM=D\n", 1uLL, 0x19uLL, vm_ofp);
fwrite("@SP\nD=M\n@LCL\nM=D\n@", 1uLL, 0x12uLL, vm_ofp);
v2 = strlen(s);
fwrite(s, 1uLL, v2, vm_ofp);
fwrite("\n0;JMP\n(LC", 1uLL, 0xAuLL, vm_ofp);
func0(vm_lcn);
result = fwrite(")\n", 1uLL, 2uLL, vm_ofp);
++vm_lcn;
return result;
}

original_code:
void writeCall(char* fn, int args)
{
 fwrite("@LC", 1, 3, _vm_ofp);
 writeNum(_vm_lcn);
 fwrite("\nD=A\n@SP\nM=M+1\nA=M-1\nM=D\n", 1, 25, _vm_ofp);
 fwrite("@LCL\nD=M\n@SP\nM=M+1\nA=M-1\nM=D\n", 1, 29, _vm_ofp);
 fwrite("@ARG\nD=M\n@SP\nM=M+1\nA=M-1\nM=D\n", 1, 29, _vm_ofp);
 fwrite("@THIS\nD=M\n@SP\nM=M+1\nA=M-1\nM=D\n", 1, 30, _vm_ofp);
 fwrite("@THAT\nD=M\n@SP\nM=M+1\nA=M-1\nM=D\n", 1, 30, _vm_ofp);
 fwrite("@SP\nD=M\n@", 1, 9, _vm_ofp);
 writeNum(args);
 fwrite("\nD=D-A\n@5\nD=D-A\n@ARG\nM=D\n", 1, 25, _vm_ofp);
 fwrite("@SP\nD=M\n@LCL\nM=D\n@", 1, 18, _vm_ofp);
 fwrite(fn, 1, strlen(fn), _vm_ofp);
 fwrite("\n0;JMP\n(LC", 1, 10, _vm_ofp);
 writeNum(_vm_lcn);
 fwrite(")\n", 1, 2, _vm_ofp);
 _vm_lcn++;
}

canonical_original_code:
void writeCall(char *fn, int args)
{
 fwrite("@LC", 1, 3, _vm_ofp);
 writeNum(_vm_lcn);
 fwrite("\nD=A\n@SP\nM=M+1\nA=M-1\nM=D\n", 1, 25, _vm_ofp);
 fwrite("@LCL\nD=M\n@SP\nM=M+1\nA=M-1\nM=D\n", 1, 29, _vm_ofp);
 fwrite("@ARG\nD=M\n@SP\nM=M+1\nA=M-1\nM=D\n", 1, 29, _vm_ofp);
 fwrite("@THIS\nD=M\n@SP\nM=M+1\nA=M-1\nM=D\n", 1, 30, _vm_ofp);
 fwrite("@THAT\nD=M\n@SP\nM=M+1\nA=M-1\nM=D\n", 1, 30, _vm_ofp);
 fwrite("@SP\nD=M\n@", 1, 9, _vm_ofp);
 writeNum(args);
 fwrite("\nD=D-A\n@5\nD=D-A\n@ARG\nM=D\n", 1, 25, _vm_ofp);
 fwrite("@SP\nD=M\n@LCL\nM=D\n@", 1, 18, _vm_ofp);
 fwrite(fn, 1, strlen(fn), _vm_ofp);
 fwrite("\n0;JMP\n(LC", 1, 10, _vm_ofp);
 writeNum(_vm_lcn);
 fwrite(")\n", 1, 2, _vm_ofp);
 _vm_lcn++;
}

variable_types:
fn
char *

variable_types: args int


writeInit

decompiled_code:
size_t writeInit()
{
fwrite("@256\nD=A\n@SP\nM=D\n", 1uLL, 0x11uLL, vm_ofp);
writeCall("Sys.init", 0);
return fwrite("(LCMP)\n@SP\nA=M-1\nM=-1\nA=A+1\nA=M\n0;JMP\n", 1uLL, 0x26uLL, vm_ofp);
}

canonical_decompiled_code:
size_t func8()
{
fwrite("@256\nD=A\n@SP\nM=D\n", 1uLL, 0x11uLL, vm_ofp);
func7("Sys.init", 0);
return fwrite("(LCMP)\n@SP\nA=M-1\nM=-1\nA=A+1\nA=M\n0;JMP\n", 1uLL, 0x26uLL, vm_ofp);
}

original_code:
void writeInit()
{
 fwrite("@256\nD=A\n@SP\nM=D\n", 1, 17, _vm_ofp);
 writeCall("Sys.init", 0);
 fwrite("(LCMP)\n@SP\nA=M-1\nM=-1\nA=A+1\nA=M\n0;JMP\n", 1, 38, _vm_ofp);
}


writeReturn

decompiled_code:
size_t writeReturn()
{
fwrite("@LCL\nD=M\n@R15\nM=D\n", 1uLL, 0x12uLL, vm_ofp);
fwrite("@5\nD=A\n@R15\nA=M-D\nD=M\n@R14\nM=D\n", 1uLL, 0x1FuLL, vm_ofp);
fwrite("@SP\nA=M-1\nD=M\n@ARG\nA=M\nM=D\n", 1uLL, 0x1BuLL, vm_ofp);
fwrite("@ARG\nD=M+1\n@SP\nM=D\n", 1uLL, 0x13uLL, vm_ofp);
fwrite("@R15\nAM=M-1\nD=M\n@THAT\nM=D\n", 1uLL, 0x1AuLL, vm_ofp);
fwrite("@R15\nAM=M-1\nD=M\n@THIS\nM=D\n", 1uLL, 0x1AuLL, vm_ofp);
fwrite("@R15\nAM=M-1\nD=M\n@ARG\nM=D\n", 1uLL, 0x19uLL, vm_ofp);
fwrite("@R15\nAM=M-1\nD=M\n@LCL\nM=D\n", 1uLL, 0x19uLL, vm_ofp);
return fwrite("@R14\nA=M\n0;JMP\n", 1uLL, 0xFuLL, vm_ofp);
}

canonical_decompiled_code:
size_t func9()
{
fwrite("@LCL\nD=M\n@R15\nM=D\n", 1uLL, 0x12uLL, vm_ofp);
fwrite("@5\nD=A\n@R15\nA=M-D\nD=M\n@R14\nM=D\n", 1uLL, 0x1FuLL, vm_ofp);
fwrite("@SP\nA=M-1\nD=M\n@ARG\nA=M\nM=D\n", 1uLL, 0x1BuLL, vm_ofp);
fwrite("@ARG\nD=M+1\n@SP\nM=D\n", 1uLL, 0x13uLL, vm_ofp);
fwrite("@R15\nAM=M-1\nD=M\n@THAT\nM=D\n", 1uLL, 0x1AuLL, vm_ofp);
fwrite("@R15\nAM=M-1\nD=M\n@THIS\nM=D\n", 1uLL, 0x1AuLL, vm_ofp);
fwrite("@R15\nAM=M-1\nD=M\n@ARG\nM=D\n", 1uLL, 0x19uLL, vm_ofp);
fwrite("@R15\nAM=M-1\nD=M\n@LCL\nM=D\n", 1uLL, 0x19uLL, vm_ofp);
return fwrite("@R14\nA=M\n0;JMP\n", 1uLL, 0xFuLL, vm_ofp);
}

original_code:
void writeReturn()
{
 fwrite("@LCL\nD=M\n@R15\nM=D\n", 1, 18, _vm_ofp);
 fwrite("@5\nD=A\n@R15\nA=M-D\nD=M\n@R14\nM=D\n", 1, 31, _vm_ofp);
 fwrite("@SP\nA=M-1\nD=M\n@ARG\nA=M\nM=D\n", 1, 27, _vm_ofp);
 fwrite("@ARG\nD=M+1\n@SP\nM=D\n", 1, 19, _vm_ofp);
 fwrite("@R15\nAM=M-1\nD=M\n@THAT\nM=D\n", 1, 26, _vm_ofp);
 fwrite("@R15\nAM=M-1\nD=M\n@THIS\nM=D\n", 1, 26, _vm_ofp);
 fwrite("@R15\nAM=M-1\nD=M\n@ARG\nM=D\n", 1, 25, _vm_ofp);
 fwrite("@R15\nAM=M-1\nD=M\n@LCL\nM=D\n", 1, 25, _vm_ofp);
 fwrite("@R14\nA=M\n0;JMP\n", 1, 15, _vm_ofp);
}


writeFunction

decompiled_code:
size_t __fastcall writeFunction(char *src, int a2)
{
size_t v2; // rax
char *v3; // r12
size_t v4; // rax
size_t v5; // rax
int i; // ebx

if ( vm_fcn )
free(vm_fcn);
v2 = strlen(src);
v3 = (char *)calloc(v2, 1uLL);
vm_fcn = v3;
v4 = strlen(src);
strncpy(v3, src, v4);
fwrite("(", 1uLL, 1uLL, vm_ofp);
v5 = strlen(src);
fwrite(src, 1uLL, v5, vm_ofp);
fwrite(")\n@SP\nA=M\n", 1uLL, 0xAuLL, vm_ofp);
if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
fwrite("M=0\nA=A+1\n", 1uLL, 0xAuLL, vm_ofp);
}
return fwrite("D=A\n@SP\nM=D\n", 1uLL, 0xCuLL, vm_ofp);
}

canonical_decompiled_code:
size_t __fastcall func10(char *src, int a2)
{
size_t v2; // rax
char *v3; // r12
size_t v4; // rax
size_t v5; // rax
int i; // ebx

if ( vm_fcn )
free(vm_fcn);
v2 = strlen(src);
v3 = (char *)calloc(v2, 1uLL);
vm_fcn = v3;
v4 = strlen(src);
strncpy(v3, src, v4);
fwrite("(", 1uLL, 1uLL, vm_ofp);
v5 = strlen(src);
fwrite(src, 1uLL, v5, vm_ofp);
fwrite(")\n@SP\nA=M\n", 1uLL, 0xAuLL, vm_ofp);
if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
fwrite("M=0\nA=A+1\n", 1uLL, 0xAuLL, vm_ofp);
}
return fwrite("D=A\n@SP\nM=D\n", 1uLL, 0xCuLL, vm_ofp);
}

original_code:
void writeFunction(char* fn, int lcls)
{
 if(_vm_fcn != ((void *)0))
  free(_vm_fcn);
 _vm_fcn = (char*) calloc(strlen(fn), sizeof(char));
 strncpy(_vm_fcn, fn, strlen(fn));
 fwrite("(", 1, 1, _vm_ofp);
 fwrite(fn, 1, strlen(fn), _vm_ofp);
 fwrite(")\n@SP\nA=M\n", 1, 10, _vm_ofp);
 for(int i = 0; i<lcls; i++)
  fwrite("M=0\nA=A+1\n", 1, 10, _vm_ofp);
 fwrite("D=A\n@SP\nM=D\n", 1, 12, _vm_ofp);
}

canonical_original_code:
void writeFunction(char *fn, int lcls)
{
 if(_vm_fcn != ((void *)0))
  free(_vm_fcn);
 _vm_fcn = (char *) calloc(strlen(fn), sizeof(char));
 strncpy(_vm_fcn, fn, strlen(fn));
 fwrite("(", 1, 1, _vm_ofp);
 fwrite(fn, 1, strlen(fn), _vm_ofp);
 fwrite(")\n@SP\nA=M\n", 1, 10, _vm_ofp);
 for(int i = 0; i<lcls; i++)
  fwrite("M=0\nA=A+1\n", 1, 10, _vm_ofp);
 fwrite("D=A\n@SP\nM=D\n", 1, 12, _vm_ofp);
}

variable_types: lcls int


initParse

decompiled_code:
size_t __fastcall initParse(const char *a1)
{
size_t result; // rax

parser_stream = (__int64)a1;
parser_com = 0LL;
parser_cur = 0;
result = strlen(a1);
parser_sz = result;
parser_ln = 0;
return result;
}

canonical_decompiled_code:
size_t __fastcall func0(const char *a1)
{
size_t result; // rax

parser_stream = (__int64)a1;
parser_com = 0LL;
parser_cur = 0;
result = strlen(a1);
parser_sz = result;
parser_ln = 0;
return result;
}

original_code:
void initParse(char* buf)
{
 _parser_stream = buf;
 _parser_com = ((void *)0);
 _parser_cur = 0;
 _parser_sz = strlen(_parser_stream);
 _parser_ln = 0;
}

canonical_original_code:
void initParse(char *buf)
{
 _parser_stream = buf;
 _parser_com = ((void *)0);
 _parser_cur = 0;
 _parser_sz = strlen(_parser_stream);
 _parser_ln = 0;
}

variable_types:
buf
char *


hasMoreCommands

decompiled_code:
bool hasMoreCommands()
{
return parser_cur < parser_sz;
}

canonical_decompiled_code:
bool func1()
{
return parser_cur < parser_sz;
}

original_code:
_Bool hasMoreCommands()
{
 if(_parser_cur < _parser_sz)
  return 1;
 return 0;
}


resetCmd

decompiled_code:
void resetCmd()
{
if ( parser_com )
{
free(parser_com);
parser_com = 0LL;
}
}

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

original_code:
void resetCmd()
{
 if(_parser_com != ((void *)0))
 {
  free(_parser_com);
  _parser_com = ((void *)0);
 }
}


advance

decompiled_code:
char advance()
{
int v0; // ebp
const char *v1; // r12
char *v2; // rbx
char *v3; // rax
size_t v4; // rbx
char *v5; // rbp
unsigned int v6; // ecx
bool v7; // di
int v8; // er8
__int64 v9; // rax
char *v10; // rax
int v11; // ecx
unsigned int v12; // eax
__int64 v13; // rdx
char v14; // dl
char v15; // si
char result; // al

resetCmd();
v0 = parser_stream;
v1 = (const char *)(parser_stream + parser_cur);
v2 = strchr(v1, 10);
if ( !v2 )
v2 = (char *)&v1[strlen(v1)];
parser_cur = (_DWORD)v2 - v0 + 1;
v3 = strstr(v1, "//");
if ( v3 && v2 > v3 )
v2 = v3;
v4 = v2 - v1;
v5 = (char *)calloc(v4, 1uLL);
strncpy(v5, v1, v4);
if ( v4 )
{
v6 = 0;
v7 = 1;
v8 = 0;
v9 = 0LL;
do
{
v10 = &v5[v9];
if ( *v10 == 32 || (*v10 & 0xFB) == 9 )
{
if ( v7 )
{
++v8;
*v10 = 32;
}
else
{
v7 = *v10 == 32 || (*v10 & 0xFB) == 9;
}
}
else
{
v7 = 0;
}
v9 = ++v6;
}
while ( v6 < v4 );
parser_com = (char *)calloc(v4 + 1 - v8, 1uLL);
v12 = 0;
v15 = 1;
v11 = 0;
v13 = 0LL;
do
{
v14 = v5[v13];
if ( v14 == 32 )
{
if ( v15 )
{
++v11;
}
else
{
parser_com[v12 - v11] = 32;
v15 = 1;
}
}
else
{
parser_com[v12 - v11] = v14;
v15 = 0;
}
v13 = ++v12;
}
while ( v12 < v4 );
}
else
{
parser_com = (char *)calloc(1uLL, 1uLL);
v11 = 0;
}
parser_com[v4 - 1 - v11] = 0;
++parser_ln;
free(v5);
result = 1;
if ( !*parser_com )
{
result = hasMoreCommands();
if ( result )
{
resetCmd();
result = advance();
}
}
return result;
}

canonical_decompiled_code:
char func3()
{
int v0; // ebp
const char *v1; // r12
char *v2; // rbx
char *v3; // rax
size_t v4; // rbx
char *v5; // rbp
unsigned int v6; // ecx
bool v7; // di
int v8; // er8
__int64 v9; // rax
char *v10; // rax
int v11; // ecx
unsigned int v12; // eax
__int64 v13; // rdx
char v14; // dl
char v15; // si
char result; // al

func2();
v0 = parser_stream;
v1 = (const char *)(parser_stream + parser_cur);
v2 = strchr(v1, 10);
if ( !v2 )
v2 = (char *)&v1[strlen(v1)];
parser_cur = (_DWORD)v2 - v0 + 1;
v3 = strstr(v1, "//");
if ( v3 && v2 > v3 )
v2 = v3;
v4 = v2 - v1;
v5 = (char *)calloc(v4, 1uLL);
strncpy(v5, v1, v4);
if ( v4 )
{
v6 = 0;
v7 = 1;
v8 = 0;
v9 = 0LL;
do
{
v10 = &v5[v9];
if ( *v10 == 32 || (*v10 & 0xFB) == 9 )
{
if ( v7 )
{
++v8;
*v10 = 32;
}
else
{
v7 = *v10 == 32 || (*v10 & 0xFB) == 9;
}
}
else
{
v7 = 0;
}
v9 = ++v6;
}
while ( v6 < v4 );
parser_com = (char *)calloc(v4 + 1 - v8, 1uLL);
v12 = 0;
v15 = 1;
v11 = 0;
v13 = 0LL;
do
{
v14 = v5[v13];
if ( v14 == 32 )
{
if ( v15 )
{
++v11;
}
else
{
parser_com[v12 - v11] = 32;
v15 = 1;
}
}
else
{
parser_com[v12 - v11] = v14;
v15 = 0;
}
v13 = ++v12;
}
while ( v12 < v4 );
}
else
{
parser_com = (char *)calloc(1uLL, 1uLL);
v11 = 0;
}
parser_com[v4 - 1 - v11] = 0;
++parser_ln;
free(v5);
result = 1;
if ( !*parser_com )
{
result = func1();
if ( result )
{
func2();
result = func3();
}
}
return result;
}

original_code:
_Bool advance()
{
 resetCmd();
 char* sp = _parser_stream+_parser_cur;
 char* ep = strchr(sp, '\n');
 char* res;
 size_t size;
 if(ep == ((void *)0))
  ep = strchr(sp, 0);
 _parser_cur = (ep - _parser_stream) + 1;
 char* tp = strstr(sp, "//");
 if(tp != ((void *)0) && tp < ep)
  ep = tp;
 size = ep-sp;
 res = (char*) calloc(size, sizeof(char));
 strncpy(res, sp, size);
 int ws = 0;
 _Bool is = 1;
 for(unsigned int i = 0; i<size; i++)
 {
  if(res[i] == ' ' || res[i] == '\t' || res[i] == '\r')
  {
   if(is)
   {
    ws++;
    res[i] = ' ';
   }
   else
    is = 1;
  }
  else
   is = 0;
 }
 _parser_com = (char*) calloc(size - ws + 1, sizeof(char));
 is = 1;
 ws = 0;
 for(unsigned int i = 0; i<size; i++)
 {
  if(res[i] == ' ')
  {
   if(is)
    ws++;
   else
   {
    _parser_com[i-ws] = res[i];
    is = 1;
   }
  }
  else
  {
   _parser_com[i-ws] = res[i];
   is = 0;
  }
 }
 _parser_com[size-ws-1] = 0;
 _parser_ln++;
 free(res);
 if(_parser_com[0] == 0)
 {
  if(!hasMoreCommands())
   return 0;
  resetCmd();
  return advance();
 }
 return 1;
}

canonical_original_code:
_Bool advance()
{
 resetCmd();
 char *sp = _parser_stream+_parser_cur;
 char *ep = strchr(sp, '\n');
 char *res;
 unsigned long size;
 if(ep == ((void *)0))
  ep = strchr(sp, 0);
 _parser_cur = (ep - _parser_stream) + 1;
 char *tp = strstr(sp, "//");
 if(tp != ((void *)0) && tp < ep)
  ep = tp;
 size = ep-sp;
 res = (char *) calloc(size, sizeof(char));
 strncpy(res, sp, size);
 int ws = 0;
 _Bool is = 1;
 for(unsigned int i = 0; i<size; i++)
 {
  if(res[i] == ' ' || res[i] == '\t' || res[i] == '\r')
  {
   if(is)
   {
    ws++;
    res[i] = ' ';
   }
   else
    is = 1;
  }
  else
   is = 0;
 }
 _parser_com = (char *) calloc(size - ws + 1, sizeof(char));
 is = 1;
 ws = 0;
 for(unsigned int i = 0; i<size; i++)
 {
  if(res[i] == ' ')
  {
   if(is)
    ws++;
   else
   {
    _parser_com[i-ws] = res[i];
    is = 1;
   }
  }
  else
  {
   _parser_com[i-ws] = res[i];
   is = 0;
  }
 }
 _parser_com[size-ws-1] = 0;
 _parser_ln++;
 free(res);
 if(_parser_com[0] == 0)
 {
  if(!hasMoreCommands())
   return 0;
  resetCmd();
  return advance();
 }
 return 1;
}

variable_types:
sp
char *

variable_types:
ep
char *

variable_types:
res
char *

variable_types:
size
unsigned long

variable_types:
tp
char *

variable_types: ws int

variable_types:
i
unsigned int


commandType

original_code:
int commandType()
{
 char* tp = strchr(_parser_com, ' ');
 char* com;
 int res = -1;
 if((tp = strchr(_parser_com, ' ')) == ((void *)0))
  tp = strchr(_parser_com, 0);
 const char* Type[] =
 {
  "add", "sub", "neg",
  "eq", "gt", "lt",
  "and", "or", "not",
  "push", "pop", "label",
  "goto", "if-goto", "function",
  "call", "return"
 };
 com = (char*) calloc(tp - _parser_com + 1, sizeof(char));
 strncpy(com, _parser_com, tp - _parser_com);
 com[tp - _parser_com] = 0;
 for(int i = 0; i<17; i++)
 {
  if(strcmp(com, Type[i]) == 0)
  {
   res = (i-8)<0 ? 0 : (i-8);
   break;
  }
 }
 free(com);
 return res;
}

canonical_original_code:
int commandType()
{
 char *tp = strchr(_parser_com, ' ');
 char *com;
 int res = -1;
 if((tp = strchr(_parser_com, ' ')) == ((void *)0))
  tp = strchr(_parser_com, 0);
 char *Type[] = {
  "add", "sub", "neg",
  "eq", "gt", "lt",
  "and", "or", "not",
  "push", "pop", "label",
  "goto", "if-goto", "function",
  "call", "return"
 };
 com = (char *) calloc(tp - _parser_com + 1, sizeof(char));
 strncpy(com, _parser_com, tp - _parser_com);
 com[tp - _parser_com] = 0;
 for(int i = 0; i<17; i++)
 {
  if(strcmp(com, Type[i]) == 0)
  {
   res = (i-8)<0 ? 0 : (i-8);
   break;
  }
 }
 free(com);
 return res;
}

variable_types:
com
char *

variable_types: res int

variable_types:
Type
char *[]

return_type: int


decompiled_code:
char *arg1()
{
char *v0; // rbx
const char *v1; // r12
char *v2; // rbp
size_t v3; // rbp
const char *v5; // r12
size_t v6; // rbp
char *v7; // rbx

v0 = strchr(parser_com, 32);
if ( (unsigned int)commandType() )
{
if ( v0 )
{
v1 = v0 + 1;
v2 = strchr(v0 + 1, 32);
if ( !v2 )
v2 = &v0[strlen(v0)];
v3 = v2 - v0;
v0 = (char *)calloc(v3, 1uLL);
strncpy(v0, v1, v3 - 1);
v0[v3 - 1] = 0;
}
}
else
{
if ( !v0 )
{
v7 = parser_com;
v0 = &v7[strlen(parser_com)];
}
v5 = parser_com;
v6 = v0 - parser_com;
v0 = (char *)calloc(v0 - parser_com + 1, 1uLL);
strncpy(v0, v5, v6);
v0[v6] = 0;
}
return v0;
}

canonical_decompiled_code:
char *func5()
{
char *v0; // rbx
const char *v1; // r12
char *v2; // rbp
size_t v3; // rbp
const char *v5; // r12
size_t v6; // rbp
char *v7; // rbx

v0 = strchr(parser_com, 32);
if ( (unsigned int)func4() )
{
if ( v0 )
{
v1 = v0 + 1;
v2 = strchr(v0 + 1, 32);
if ( !v2 )
v2 = &v0[strlen(v0)];
v3 = v2 - v0;
v0 = (char *)calloc(v3, 1uLL);
strncpy(v0, v1, v3 - 1);
v0[v3 - 1] = 0;
}
}
else
{
if ( !v0 )
{
v7 = parser_com;
v0 = &v7[strlen(parser_com)];
}
v5 = parser_com;
v6 = v0 - parser_com;
v0 = (char *)calloc(v0 - parser_com + 1, 1uLL);
strncpy(v0, v5, v6);
v0[v6] = 0;
}
return v0;
}

original_code:
char* arg1()
{
 char* sp = strchr(_parser_com, ' ');
 char* ep;
 char* res;
 if(commandType() == C_ARITHMETIC)
 {
 if(sp == ((void *)0))
  sp = strchr(_parser_com, 0);
  res = (char*) calloc(sp-_parser_com+1, sizeof(char));
  strncpy(res, _parser_com, sp-_parser_com);
  res[sp-_parser_com] = 0;
  return res;
 }
 if(sp == ((void *)0))
  return ((void *)0);
 if((ep = strchr(sp+1, ' ')) == ((void *)0))
  ep = strchr(sp, 0);
 res = (char*) calloc(ep-sp, sizeof(char));
 strncpy(res, sp+1, ep-sp-1);
 res[ep-sp-1] = 0;
 return res;
}

canonical_original_code:
char *arg1()
{
 char *sp = strchr(_parser_com, ' ');
 char *ep;
 char *res;
 if(commandType() == C_ARITHMETIC)
 {
 if(sp == ((void *)0))
  sp = strchr(_parser_com, 0);
  res = (char *) calloc(sp-_parser_com+1, sizeof(char));
  strncpy(res, _parser_com, sp-_parser_com);
  res[sp-_parser_com] = 0;
  return res;
 }
 if(sp == ((void *)0))
  return ((void *)0);
 if((ep = strchr(sp+1, ' ')) == ((void *)0))
  ep = strchr(sp, 0);
 res = (char *) calloc(ep-sp, sizeof(char));
 strncpy(res, sp+1, ep-sp-1);
 res[ep-sp-1] = 0;
 return res;
}

return_type: char *


toNum

decompiled_code:
__int64 __fastcall toNum(const char *a1)
{
size_t v1; // r8
unsigned int v2; // ecx
size_t v3; // rax
int v4; // edx
unsigned int v5; // esi
char v6; // al

v1 = strlen(a1);
v2 = v1 - 1;
v3 = (unsigned int)(v1 - 1);
if ( v1 <= v3 )
return 0;
v4 = 1;
v5 = 0;
while ( 1 )
{
v6 = a1[v3];
if ( (unsigned __int8)(v6 - 48) > 9u )
break;
v5 += v4 * (v6 - 48);
v4 *= 10;
v3 = --v2;
if ( v1 <= v2 )
return v5;
}
return (unsigned int)-1;
}

canonical_decompiled_code:
__int64 __fastcall func6(const char *a1)
{
size_t v1; // r8
unsigned int v2; // ecx
size_t v3; // rax
int v4; // edx
unsigned int v5; // esi
char v6; // al

v1 = strlen(a1);
v2 = v1 - 1;
v3 = (unsigned int)(v1 - 1);
if ( v1 <= v3 )
return 0;
v4 = 1;
v5 = 0;
while ( 1 )
{
v6 = a1[v3];
if ( (unsigned __int8)(v6 - 48) > 9u )
break;
v5 += v4 * (v6 - 48);
v4 *= 10;
v3 = --v2;
if ( v1 <= v2 )
return v5;
}
return (unsigned int)-1;
}

original_code:
int toNum(char* s)
{
 int res = 0;
 int mul = 1;
 for(unsigned int i=strlen(s) - 1; i < strlen(s); i--)
 {
  if(s[i] < 48 || s[i] > 57)
   return -1;
  res += (s[i] - 48) * mul;
  mul *= 10;
 }
 return res;
}

canonical_original_code:
int toNum(char *s)
{
 int res = 0;
 int mul = 1;
 for(unsigned int i = strlen(s) - 1; i < strlen(s); i--)
 {
  if(s[i] < 48 || s[i] > 57)
   return -1;
  res += (s[i] - 48) * mul;
  mul *= 10;
 }
 return res;
}

variable_types:
s
char *

variable_types: mul int


decompiled_code:
__int64 arg2()
{
char *v0; // rax
char *v1; // rbx
size_t v2; // r12
char *v3; // rbp
unsigned int v4; // ebx

v0 = strrchr(parser_com, 32);
if ( !v0 )
return (unsigned int)-1;
v1 = v0;
v2 = strlen(v0);
v3 = (char *)calloc(v2, 1uLL);
strncpy(v3, v1 + 1, v2);
v3[v2] = 0;
v4 = toNum(v3);
free(v3);
return v4;
}

canonical_decompiled_code:
__int64 func7()
{
char *v0; // rax
char *v1; // rbx
size_t v2; // r12
char *v3; // rbp
unsigned int v4; // ebx

v0 = strrchr(parser_com, 32);
if ( !v0 )
return (unsigned int)-1;
v1 = v0;
v2 = strlen(v0);
v3 = (char *)calloc(v2, 1uLL);
strncpy(v3, v1 + 1, v2);
v3[v2] = 0;
v4 = func6(v3);
free(v3);
return v4;
}

original_code:
int arg2()
{
 char* sp = strrchr(_parser_com, ' ');
 char* ep;
 char* d;
 int res = -1;
 if(sp == ((void *)0))
  return res;
 ep = strchr(sp, 0);
 d = (char*) calloc(ep-sp, sizeof(char));
 strncpy(d, sp+1, ep-sp);
 d[ep-sp] = 0;
 res = toNum(d);
 free(d);
 return res;
}

canonical_original_code:
int arg2()
{
 char *sp = strrchr(_parser_com, ' ');
 char *ep;
 char *d;
 int res = -1;
 if(sp == ((void *)0))
  return res;
 ep = strchr(sp, 0);
 d = (char *) calloc(ep-sp, sizeof(char));
 strncpy(d, sp+1, ep-sp);
 d[ep-sp] = 0;
 res = toNum(d);
 free(d);
 return res;
}

variable_types:
d
char *


debugString

decompiled_code:
int __fastcall debugString(char *s)
{
unsigned int v1; // ebp
__int64 v2; // rbx

printf("DEBUG: ");
v1 = 0;
v2 = 0LL;
do
{
printf("%d ", (unsigned int)s[v2]);
v2 = ++v1;
}
while ( v1 <= strlen(s) );
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func8(char *s)
{
unsigned int v1; // ebp
__int64 v2; // rbx

printf("DEBUG: ");
v1 = 0;
v2 = 0LL;
do
{
printf("%d ", (unsigned int)s[v2]);
v2 = ++v1;
}
while ( v1 <= strlen(s) );
return putchar(10);
}

original_code:
void debugString(char* s)
{
 printf("DEBUG: ");
 for(unsigned int i = 0; i<=strlen(s); i++)
 {
  printf("%d ", s[i]);
 }
 printf("\n");
}

canonical_original_code:
void debugString(char *s)
{
 printf("DEBUG: ");
 for(unsigned int i = 0; i<=strlen(s); i++)
 {
  printf("%d ", s[i]);
 }
 printf("\n");
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbx
int result; // eax

if ( argc <= 2 )
{
puts("Usage: vmtrans <output> <input, ...>");
result = 1;
}
else
{
v3 = argv + 2;
do
vmstart(*v3++, argv, envp);
while ( v3 != &argv[(unsigned int)(argc - 3) + 3] );
vmasm((unsigned int)(argc - 2), argv + 2, argv[1]);
result = 0;
}
return result;
}

original_code:
int main(int argc, char** argv)
{
 if(argc < 3)
 {
  printf("Usage: vmtrans <output> <input, ...>\n");
  return 1;
 }
 for(int i = 2; i < argc; i++)
 {
  vmstart(argv[i]);
 }
 vmasm(argc-2, argv+2, argv[1]);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
 if(argc < 3)
 {
  printf("Usage: vmtrans <output> <input, ...>\n");
  return 1;
 }
 for(int i = 2; i < argc; i++)
 {
  vmstart(argv[i]);
 }
 vmasm(argc-2, argv+2, argv[1]);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: i int


vmstart

decompiled_code:
int __fastcall vmstart(char *s)
{
char *v1; // rbx
size_t v2; // rbx
char *v3; // r12
char *v4; // r13

v1 = strrchr(s, 46);
if ( !v1 )
v1 = &s[strlen(s)];
vm_ifn = s;
vm_fcn = 0LL;
v2 = v1 - s;
v3 = (char *)calloc(v2 + 3, 1uLL);
vm_ofn = (__int64)v3;
v4 = (char *)calloc(v2, 1uLL);
vm_sv = (__int64)v4;
strncpy(v3, s, v2);
strncpy(v4, s, v2);
strcat(v3, ".s");
vm_ofp = fopen(v3, "wb");
if ( !vm_lbln )
vm_lbln = 1;
if ( !vm_lcn )
vm_lcn = 1;
vmtrans();
return fclose(vm_ofp);
}

canonical_decompiled_code:
int __fastcall func1(char *s)
{
char *v1; // rbx
size_t v2; // rbx
char *v3; // r12
char *v4; // r13

v1 = strrchr(s, 46);
if ( !v1 )
v1 = &s[strlen(s)];
vm_ifn = s;
vm_fcn = 0LL;
v2 = v1 - s;
v3 = (char *)calloc(v2 + 3, 1uLL);
vm_ofn = (__int64)v3;
v4 = (char *)calloc(v2, 1uLL);
vm_sv = (__int64)v4;
strncpy(v3, s, v2);
strncpy(v4, s, v2);
strcat(v3, ".s");
vm_ofp = fopen(v3, "wb");
if ( !vm_lbln )
vm_lbln = 1;
if ( !vm_lcn )
vm_lcn = 1;
func0();
return fclose(vm_ofp);
}

original_code:
void vmstart(char* fn)
{
 char* sp = strrchr(fn, '.');
 if(sp == ((void *)0))
  sp = fn + strlen(fn);
 _vm_ifn = fn;
 _vm_fcn = ((void *)0);
 _vm_ofn = (char*) calloc(sp-fn+3, sizeof(char));
 _vm_sv = (char*) calloc(sp-fn, sizeof(char));
 strncpy(_vm_ofn, fn, sp-fn);
 strncpy(_vm_sv, fn, sp-fn);
 strcat(_vm_ofn, ".s\0");
 _vm_ofp = fopen(_vm_ofn, "wb");
 if(_vm_lbln == 0)
  _vm_lbln = 1;
 if(_vm_lcn == 0)
  _vm_lcn = 1;
 vmtrans();
 fclose(_vm_ofp);
}

canonical_original_code:
void vmstart(char *fn)
{
 char *sp = strrchr(fn, '.');
 if(sp == ((void *)0))
  sp = fn + strlen(fn);
 _vm_ifn = fn;
 _vm_fcn = ((void *)0);
 _vm_ofn = (char *) calloc(sp-fn+3, sizeof(char));
 _vm_sv = (char *) calloc(sp-fn, sizeof(char));
 strncpy(_vm_ofn, fn, sp-fn);
 strncpy(_vm_sv, fn, sp-fn);
 strcat(_vm_ofn, ".s\0");
 _vm_ofp = fopen(_vm_ofn, "wb");
 if(_vm_lbln == 0)
  _vm_lbln = 1;
 if(_vm_lcn == 0)
  _vm_lcn = 1;
 vmtrans();
 fclose(_vm_ofp);
}

variable_types:
fn
char *


vminitout

decompiled_code:
__int64 __fastcall vminitout(const char *a1)
{
unsigned int v1; // edx

vm_ofp = fopen(a1, "wb");
v1 = 1;
if ( vm_ofp )
{
writeInit(a1, "wb", 1LL);
v1 = 0;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func2(const char *a1)
{
unsigned int v1; // edx

vm_ofp = fopen(a1, "wb");
v1 = 1;
if ( vm_ofp )
{
writeInit(a1, "wb", 1LL);
v1 = 0;
}
return v1;
}

original_code:
int vminitout(char* out)
{
 if((_vm_ofp = fopen(out, "wb")) == ((void *)0))
  return 1;
 writeInit();
 return 0;
}

canonical_original_code:
int vminitout(char *out)
{
 if((_vm_ofp = fopen(out, "wb")) == ((void *)0))
  return 1;
 writeInit();
 return 0;
}

variable_types:
out
char *


vmasm

decompiled_code:
__int64 __fastcall vmasm(int a1, const char **a2, const char *a3)
{
const char **v4; // r12
char *v5; // rbp
const char *v6; // rbp
char *v7; // rbx
unsigned int v9; // [rsp+Ch] [rbp-3Ch]

v9 = vminitout(a3);
if ( v9 )
return 1;
if ( a1 <= 0 )
{
LABEL_8:
fclose(vm_ofp);
}
else
{
v4 = a2;
while ( 1 )
{
v6 = *v4;
v7 = strrchr(*v4, 46);
if ( !v7 )
v7 = (char *)&v6[strlen(v6)];
v5 = (char *)calloc(v7 - v6 + 3, 1uLL);
vm_ifn = v5;
strncpy(v5, *v4, v7 - *v4);
strcat(v5, ".s");
printf("%s -> %s\n", v5, a3);
if ( (unsigned int)vmout(vm_ifn) )
break;
remove(vm_ifn);
if ( ++v4 == &a2[(unsigned int)(a1 - 1) + 1] )
goto LABEL_8;
}
v9 = 1;
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func4(int a1, const char **a2, const char *a3)
{
const char **v4; // r12
char *v5; // rbp
const char *v6; // rbp
char *v7; // rbx
unsigned int v9; // [rsp+Ch] [rbp-3Ch]

v9 = func2(a3);
if ( v9 )
return 1;
if ( a1 <= 0 )
{
LABEL_8:
fclose(vm_ofp);
}
else
{
v4 = a2;
while ( 1 )
{
v6 = *v4;
v7 = strrchr(*v4, 46);
if ( !v7 )
v7 = (char *)&v6[strlen(v6)];
v5 = (char *)calloc(v7 - v6 + 3, 1uLL);
vm_ifn = v5;
strncpy(v5, *v4, v7 - *v4);
strcat(v5, ".s");
printf("%s -> %s\n", v5, a3);
if ( (unsigned int)func3(vm_ifn) )
break;
remove(vm_ifn);
if ( ++v4 == &a2[(unsigned int)(a1 - 1) + 1] )
goto LABEL_8;
}
v9 = 1;
}
return v9;
}

original_code:
int vmasm(int inc, char** in, char* out)
{
 if(vminitout(out) != 0)
  return 1;
 for(int i=0; i<inc; i++)
 {
  char* sp = strrchr(in[i], '.');
  if(sp == ((void *)0))
   sp = in[i] + strlen(in[i]);
  _vm_ifn = (char*) calloc(sp-in[i]+3, sizeof(char));
  strncpy(_vm_ifn, in[i], sp-in[i]);
  strcat(_vm_ifn, ".s\0");
  printf("%s -> %s\n", _vm_ifn, out);
  if(vmout(_vm_ifn) != 0)
   return 1;
  remove(_vm_ifn);
 }
 fclose(_vm_ofp);
 return 0;
}

canonical_original_code:
int vmasm(int inc, char **in, char *out)
{
 if(vminitout(out) != 0)
  return 1;
 for(int i = 0; i<inc; i++)
 {
  char *sp = strrchr(in[i], '.');
  if(sp == ((void *)0))
   sp = in[i] + strlen(in[i]);
  _vm_ifn = (char *) calloc(sp-in[i]+3, sizeof(char));
  strncpy(_vm_ifn, in[i], sp-in[i]);
  strcat(_vm_ifn, ".s\0");
  printf("%s -> %s\n", _vm_ifn, out);
  if(vmout(_vm_ifn) != 0)
   return 1;
  remove(_vm_ifn);
 }
 fclose(_vm_ofp);
 return 0;
}

variable_types: inc int

variable_types:
in
char **

variable_types:
sp
char *


repo: lilingxin/mapred

decompiled_code:
__int64 thread_start()
{
if ( pthread_create(wet, 0LL, wthread_proc, wet) >= 0 && pthread_create(ret, 0LL, rthread_proc, ret) >= 0 )
return 0LL;
fwrite("pthread_create error\n", 1uLL, 0x15uLL, stderr);
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 func6()
{
if ( pthread_create(wet, 0LL, wthread_proc, wet) >= 0 && pthread_create(ret, 0LL, rthread_proc, ret) >= 0 )
return 0LL;
fwrite("pthread_create error\n", 1uLL, 0x15uLL, stderr);
return 0xFFFFFFFFLL;
}

original_code:
int thread_start()
{
    if (pthread_create(&wet->id, ((void *)0), wthread_proc, (void*)wet) < 0) {
        fprintf(stderr, "pthread_create error\n");
        return -1;
    }
    if (pthread_create(&ret->id, ((void *)0), rthread_proc, (void*)ret) < 0) {
        fprintf(stderr, "pthread_create error\n");
        return -1;
    }
    return 0;
}

canonical_original_code:
int thread_start()
{
    if (pthread_create(&wet->id, ((void *)0), wthread_proc, (void *)wet) < 0) {
        fprintf(stderr, "pthread_create error\n");
        return -1;
    }
    if (pthread_create(&ret->id, ((void *)0), rthread_proc, (void *)ret) < 0) {
        fprintf(stderr, "pthread_create error\n");
        return -1;
    }
    return 0;
}


decompiled_code:
__int64 thread_term()
{
pthread_t *v0; // rdi
__int64 v1; // rbp
int v2; // ebx
void *thread_return; // [rsp+8h] [rbp-20h] BYREF

event_base_free(wet[1]);
term_buf(wet + 2);
free(wet);
thread_return = 0LL;
pthread_join(*ret, &thread_return);
event_base_free(ret[1]);
v0 = ret;
if ( *((int *)ret + 4) > 0 )
{
v1 = 20LL;
v2 = 0;
do
{
term_buf(&v0[v1]);
++v2;
v0 = ret;
v1 += 21LL;
}
while ( *((_DWORD *)ret + 4) > v2 );
}
free(v0);
return 0LL;
}

canonical_decompiled_code:
__int64 func7()
{
pthread_t *v0; // rdi
__int64 v1; // rbp
int v2; // ebx
void *thread_return; // [rsp+8h] [rbp-20h] BYREF

event_base_free(wet[1]);
term_buf(wet + 2);
free(wet);
thread_return = 0LL;
pthread_join(*ret, &thread_return);
event_base_free(ret[1]);
v0 = ret;
if ( *((int *)ret + 4) > 0 )
{
v1 = 20LL;
v2 = 0;
do
{
term_buf(&v0[v1]);
++v2;
v0 = ret;
v1 += 21LL;
}
while ( *((_DWORD *)ret + 4) > v2 );
}
free(v0);
return 0LL;
}

original_code:
int thread_term()
{
    event_base_free(wet->base);
    term_buf(&wet->buf);
    free(wet);
    void* retval = ((void *)0);
    pthread_join(ret->id, &retval);
    event_base_free(ret->base);
    for (int i = 0; i < ret->evno; ++i) {
        term_buf(&ret->evs[i].buf);
    }
    free(ret);
    return 0;
}

canonical_original_code:
int thread_term()
{
    event_base_free(wet->base);
    term_buf(&wet->buf);
    free(wet);
    void *retval = ((void *)0);
    pthread_join(ret->id, &retval);
    event_base_free(ret->base);
    for (int i = 0; i < ret->evno; ++i) {
        term_buf(&ret->evs[i].buf);
    }
    free(ret);
    return 0;
}

variable_types:
retval
void *


usage

decompiled_code:
int __fastcall usage(const char *a1)
{
return fprintf(
stdout,
"%s usage : \n"
"%s [option] [INPUT] \n"
"option is :\n"
" --mapper | -m the command to execute in shell\n"
" --count | -c the process num in background\n"
" --help | -h \n",
a1,
a1);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
return fprintf(
stdout,
"%s usage : \n"
"%s [option] [INPUT] \n"
"option is :\n"
" --mapper | -m the command to execute in shell\n"
" --count | -c the process num in background\n"
" --help | -h \n",
a1,
a1);
}

original_code:
static void usage(char* proc)
{
    fprintf(stdout, "%s usage : \n"
            "%s [option] [INPUT] \n"
            "option is :\n"
            " --mapper | -m the command to execute in shell\n"
            " --count  | -c the process num in background\n"
            " --help   | -h \n"
            , proc, proc);
}

canonical_original_code:
static void usage(char *proc)
{
    fprintf(stdout, "%s usage : \n"
            "%s [option] [INPUT] \n"
            "option is :\n"
            " --mapper | -m the command to execute in shell\n"
            " --count  | -c the process num in background\n"
            " --help   | -h \n"
            , proc, proc);
}

variable_types:
proc
char *


decompiled_code:
void __fastcall sig_handler(int a1)
{
int v1; // er12
__int64 v2; // rbx

if ( (unsigned int)(a1 - 2) <= 1 || a1 == 15 )
{
v1 = count;
if ( count > 0 )
{
v2 = 0LL;
do
kill(pids[v2++], a1);
while ( v2 != v1 );
}
}
}

canonical_decompiled_code:
void __fastcall func1(int a1)
{
int v1; // er12
__int64 v2; // rbx

if ( (unsigned int)(a1 - 2) <= 1 || a1 == 15 )
{
v1 = count;
if ( count > 0 )
{
v2 = 0LL;
do
kill(pids[v2++], a1);
while ( v2 != v1 );
}
}
}

original_code:
void sig_handler(int signo)
{
    if (signo == 3 || signo == 15
        || signo == 2) {
        for (int i = 0; i < count; ++i) {
            kill(pids[i], signo);
        }
    }
}

variable_types: signo int


getopts

decompiled_code:
int __fastcall getopts(int argc, char **argv)
{
char *v2; // r12
size_t v3; // rax
int v4; // eax
int v5; // eax
int v7; // eax
int v8; // ebx
const char **v9; // rsi
const char **v10; // rdx
struct option v11; // [rsp+0h] [rbp-78h] BYREF
const char *v12; // [rsp+20h] [rbp-58h]
int v13; // [rsp+28h] [rbp-50h]
__int64 v14; // [rsp+30h] [rbp-48h]
int v15; // [rsp+38h] [rbp-40h]
const char *v16; // [rsp+40h] [rbp-38h]
int v17; // [rsp+48h] [rbp-30h]
__int64 v18; // [rsp+50h] [rbp-28h]
int v19; // [rsp+58h] [rbp-20h]

v11.name = "mapper";
v11.has_arg = 1;
v11.flag = 0LL;
v11.val = 109;
v12 = "count";
v13 = 1;
v14 = 0LL;
v15 = 99;
v16 = "help";
v17 = 0;
v18 = 0LL;
v19 = 104;
while ( 1 )
{
v4 = getopt_long(argc, argv, "m:c:h", &v11, 0LL);
if ( v4 == -1 )
break;
switch ( v4 )
{
case 'h':
usage(*argv);
break;
case 'm':
memset(&cmd, 0, 0x1000uLL);
v2 = optarg;
v3 = strlen(optarg);
memcpy(&cmd, v2, v3);
break;
case 'c':
count = atoi(optarg);
break;
}
}
v5 = optind;
if ( optind >= argc )
return 0;
++optind;
v7 = open(argv[v5], 0);
v8 = v7;
if ( v7 >= 0 && dup2(v7, 0) >= 0 )
{
close(v8);
return 0;
}
v9 = (const char **)(unsigned int)*_errno_location();
error_at_line(1, (int)v9, "main.c", 0x44u, "open file error");
return main(1, v9, v10);
}

canonical_decompiled_code:
int __fastcall func2(int argc, char **argv)
{
char *v2; // r12
size_t v3; // rax
int v4; // eax
int v5; // eax
int v7; // eax
int v8; // ebx
const char **v9; // rsi
const char **v10; // rdx
struct option v11; // [rsp+0h] [rbp-78h] BYREF
const char *v12; // [rsp+20h] [rbp-58h]
int v13; // [rsp+28h] [rbp-50h]
__int64 v14; // [rsp+30h] [rbp-48h]
int v15; // [rsp+38h] [rbp-40h]
const char *v16; // [rsp+40h] [rbp-38h]
int v17; // [rsp+48h] [rbp-30h]
__int64 v18; // [rsp+50h] [rbp-28h]
int v19; // [rsp+58h] [rbp-20h]

v11.name = "mapper";
v11.has_arg = 1;
v11.flag = 0LL;
v11.val = 109;
v12 = "count";
v13 = 1;
v14 = 0LL;
v15 = 99;
v16 = "help";
v17 = 0;
v18 = 0LL;
v19 = 104;
while ( 1 )
{
v4 = getopt_long(argc, argv, "m:c:h", &v11, 0LL);
if ( v4 == -1 )
break;
switch ( v4 )
{
case 'h':
func0(*argv);
break;
case 'm':
memset(&cmd, 0, 0x1000uLL);
v2 = optarg;
v3 = strlen(optarg);
memcpy(&cmd, v2, v3);
break;
case 'c':
count = atoi(optarg);
break;
}
}
v5 = optind;
if ( optind >= argc )
return 0;
++optind;
v7 = open(argv[v5], 0);
v8 = v7;
if ( v7 >= 0 && dup2(v7, 0) >= 0 )
{
close(v8);
return 0;
}
v9 = (const char **)(unsigned int)*_errno_location();
error_at_line(1, (int)v9, "main.c", 0x44u, "open file error");
return main(1, v9, v10);
}

original_code:
int getopts(int argc, char** argv)
{
    char* short_opt = "m:c:h";
    struct option long_opt[] = {
        {"mapper", 1, ((void *)0), 'm'},
        {"count", 1, ((void *)0), 'c'},
        {"help", 0, ((void *)0), 'h'}
    };
    int o = -1;
    while ((o = getopt_long(argc, argv, short_opt, long_opt, ((void *)0))) != -1) {
        switch (o) {
        case 'm':
            memset(cmd, 0, 4096);
            memcpy(cmd, optarg, strlen(optarg));
            break;
        case 'c':
            count = atoi(optarg);
            break;
        case 'h':
            usage(argv[0]);
            break;
        }
    }
    if (optind < argc) {
        int fd = open(argv[optind++], 00);
        if (fd < 0 || dup2(fd, 0) < 0) {
            error_at_line(1, (*__errno_location ()), "main.c", 68, "open file error");
        }
        close(fd);
    }
    return 0;
}

canonical_original_code:
int getopts(int argc, char **argv)
{
    char *short_opt = "m:c:h";
    struct option long_opt[] = {
        {"mapper", 1, ((void *)0), 'm'},
        {"count", 1, ((void *)0), 'c'},
        {"help", 0, ((void *)0), 'h'}
    };
    int o = -1;
    while ((o = getopt_long(argc, argv, short_opt, long_opt, ((void *)0))) != -1) {
        switch (o) {
        case 'm':
            memset(cmd, 0, 4096);
            memcpy(cmd, optarg, strlen(optarg));
            break;
        case 'c':
            count = atoi(optarg);
            break;
        case 'h':
            usage(argv[0]);
            break;
        }
    }
    if (optind < argc) {
        int fd = open(argv[optind++], 00);
        if (fd < 0 || dup2(fd, 0) < 0) {
            error_at_line(1, (*__errno_location ()), "main.c", 68, "open file error");
        }
        close(fd);
    }
    return 0;
}

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []

variable_types: o int

variable_types: fd int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
int v4; // ebp
unsigned int v6; // [rsp+8h] [rbp-20h] BYREF
unsigned int v7[7]; // [rsp+Ch] [rbp-1Ch] BYREF

signal(13, (__sighandler_t)((char *)usage + 1));
signal(3, sig_handler);
signal(2, sig_handler);
signal(15, sig_handler);
getopts(argc, (char **)argv);
thread_init((unsigned int)count);
v7[0] = 0;
v6 = 0;
if ( count > 0 )
{
v3 = 0LL;
do
{
v4 = spawn_process(&cmd, v7, &v6);
set_cloexec(v6);
set_cloexec(v7[0]);
thread_add(v6, v7[0]);
pids[v3++] = v4;
}
while ( count > (int)v3 );
}
thread_start();
wait_children();
thread_term();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbx
int v4; // ebp
unsigned int v6; // [rsp+8h] [rbp-20h] BYREF
unsigned int v7[7]; // [rsp+Ch] [rbp-1Ch] BYREF

signal(13, (__sighandler_t)((char *)usage + 1));
signal(3, sig_handler);
signal(2, sig_handler);
signal(15, sig_handler);
func2(argc, (char **)argv);
thread_init((unsigned int)count);
v7[0] = 0;
v6 = 0;
if ( count > 0 )
{
v3 = 0LL;
do
{
v4 = spawn_process(&cmd, v7, &v6);
set_cloexec(v6);
set_cloexec(v7[0]);
thread_add(v6, v7[0]);
pids[v3++] = v4;
}
while ( count > (int)v3 );
}
thread_start();
wait_children();
thread_term();
return 0;
}

original_code:
int main(int argc, char* argv[])
{
    signal(13, ((__sighandler_t) 1));
    signal(3, sig_handler);
    signal(2, sig_handler);
    signal(15, sig_handler);
    getopts(argc, argv);
    thread_init(count);
    int in = 0, out = 0;
    for (int i = 0; i < count; ++i) {
        int pid = spawn_process(cmd, &in, &out);
        set_cloexec(out);
        set_cloexec(in);
        thread_add(out, in);
        pids[i] = pid;
    }
    thread_start();
    wait_children();
    thread_term();
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    signal(13, ((void (*)(int)) 1));
    signal(3, sig_handler);
    signal(2, sig_handler);
    signal(15, sig_handler);
    getopts(argc, argv);
    thread_init(count);
    int in = 0; int out = 0;
    for (int i = 0; i < count; ++i) {
        int pid = spawn_process(cmd, &in, &out);
        set_cloexec(out);
        set_cloexec(in);
        thread_add(out, in);
        pids[i] = pid;
    }
    thread_start();
    wait_children();
    thread_term();
    return 0;
}

variable_types:
argv
char *[]

variable_types: in int

variable_types: out int

variable_types: pid int


decompiled_code:
__int64 __fastcall init_buf(_QWORD *a1, int a2)
{
void *v2; // rax

a1[3] = a2;
a1[2] = 0LL;
v2 = malloc(a2);
*a1 = v2;
if ( !v2 )
return 1LL;
a1[1] = v2;
return 0LL;
}

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

a1[3] = a2;
a1[2] = 0LL;
v2 = malloc(a2);
*a1 = v2;
if ( !v2 )
return 1LL;
a1[1] = v2;
return 0LL;
}

original_code:
int init_buf(struct buf_t* b, int alloc)
{
    b->capacity = alloc;
    b->bytes = 0;
    b->ptr = (char*) malloc(b->capacity);
    if (!b->ptr) {
        return E_ERROR;
    }
    b->cur = b->ptr;
    return E_OK;
}

canonical_original_code:
int init_buf(struct buf_t *b, int alloc)
{
    b->capacity = alloc;
    b->bytes = 0;
    b->ptr = (char *) malloc(b->capacity);
    if (!b->ptr) {
        return E_ERROR;
    }
    b->cur = b->ptr;
    return E_OK;
}

variable_types: struct buf_t { char *ptr; char *cur; unsigned long bytes; unsigned long capacity; } *

variable_types: alloc int


original_code:
int term_buf(struct buf_t* b)
{
    if (b->ptr) {
        free(b->ptr);
    }
    b->ptr = b->cur = ((void *)0);
    b->capacity = b->bytes = 0;
    return E_OK;
}

canonical_original_code:
int term_buf(struct buf_t *b)
{
    if (b->ptr) {
        free(b->ptr);
    }
    b->ptr = b->cur = ((void *)0);
    b->capacity = b->bytes = 0;
    return E_OK;
}

user_defined_types: struct buf_t { char *ptr; char *cur; unsigned long bytes; unsigned long capacity; }


original_code:
int try_read_more(struct buf_t* b, int fd)
{
    if (b->cur != b->ptr) {
        if (b->bytes != 0) {
            memmove(b->ptr, b->cur, b->bytes);
        }
        b->cur = b->ptr;
    }
    if (b->bytes >= b->capacity) {
        char* new_ptr = (char*)realloc(b->ptr, b->capacity * 2);
        if (!new_ptr) {
            error_at_line(1, (*__errno_location ()), "buf.c", 45, "not enough memory, realloc buf");
        }
        b->cur = b->ptr = new_ptr;
        b->capacity *= 2;
    }
    int avail = b->capacity - b->bytes;
    int nread = read(fd, b->cur + b->bytes, avail);
    if (nread > 0) {
        b->bytes += nread;
    }
    if (nread == 0) return E_ERROR;
    if (nread < 0) {
        if ((*__errno_location ()) == 11 || (*__errno_location ()) == 11) {
            return E_OK;
        }
        fprintf(stderr, "read error, %s\n", strerror((*__errno_location ())));
        return E_ERROR;
    }
    return E_OK;
}

canonical_original_code:
int try_read_more(struct buf_t *b, int fd)
{
    if (b->cur != b->ptr) {
        if (b->bytes != 0) {
            memmove(b->ptr, b->cur, b->bytes);
        }
        b->cur = b->ptr;
    }
    if (b->bytes >= b->capacity) {
        char *new_ptr = (char*)realloc(b->ptr, b->capacity * 2);
        if (!new_ptr) {
            error_at_line(1, (*__errno_location ()), "buf.c", 45, "not enough memory, realloc buf");
        }
        b->cur = b->ptr = new_ptr;
        b->capacity *= 2;
    }
    int avail = b->capacity - b->bytes;
    int nread = read(fd, b->cur + b->bytes, avail);
    if (nread > 0) {
        b->bytes += nread;
    }
    if (nread == 0) return E_ERROR;
    if (nread < 0) {
        if ((*__errno_location ()) == 11 || (*__errno_location ()) == 11) {
            return E_OK;
        }
        fprintf(stderr, "read error, %s\n", strerror((*__errno_location ())));
        return E_ERROR;
    }
    return E_OK;
}

variable_types: avail int

variable_types: nread int


original_code:
int get_line(struct buf_t* b, char** line, size_t* len)
{
    char* bp = (char*)memchr(b->cur, '\n', b->bytes);
    if (bp == ((void *)0)) {
        return E_NEED_MORE;
    }
    *line = b->cur;
    *len = ++bp - b->cur;
    b->cur = bp;
    b->bytes -= *len;
    return E_OK;
}

canonical_original_code:
int get_line(struct buf_t *b, char **line, unsigned long *len)
{
    char *bp = (char*)memchr(b->cur, '\n', b->bytes);
    if (bp == ((void *)0)) {
        return E_NEED_MORE;
    }
    *line = b->cur;
    *len = ++bp - b->cur;
    b->cur = bp;
    b->bytes -= *len;
    return E_OK;
}

variable_types:
line
char **

variable_types:
len
unsigned long *

variable_types:
bp
char *


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

result = getenv("SHELL");
if ( !result )
result = "/bin/sh";
return result;
}

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

result = getenv("SHELL");
if ( !result )
result = "/bin/sh";
return result;
}

original_code:
static char* get_shell()
{
    char* shell = getenv("SHELL");
    return shell ? shell : "/bin/sh";
}

canonical_original_code:
static char *get_shell()
{
    char *shell = getenv("SHELL");
    return shell ? shell : "/bin/sh";
}

variable_types:
shell
char *


decompiled_code:
char *__fastcall last_component(const char *a1)
{
return strrchr(a1, 47) + 1;
}

canonical_decompiled_code:
char *__fastcall func1(const char *a1)
{
return strrchr(a1, 47) + 1;
}

original_code:
static char* last_component(const char* str)
{
    char* bp = strrchr(str, (int)'/');
    return ++bp;
}

canonical_original_code:
static char *last_component(char *str)
{
    char *bp = strrchr(str, (int)'/');
    return ++bp;
}

variable_types:
str
char *


decompiled_code:
int __fastcall set_noblocking(int fd)
{
int v1; // eax

v1 = fcntl(fd, 1, 0LL);
return fcntl(fd, 2, v1 | 1u);
}

canonical_decompiled_code:
int __fastcall func2(int fd)
{
int v1; // eax

v1 = fcntl(fd, 1, 0LL);
return fcntl(fd, 2, v1 | 1u);
}

original_code:
int set_noblocking(int fd)
{
    int flags = fcntl(fd, 1, 0);
    do { if ((flags >= 0) < 0) { error(1, (*__errno_location ()), "fcntl error"); } } while (0);
    return fcntl(fd, 2, flags | 1);
}

variable_types: flags int


decompiled_code:
int __fastcall set_cloexec(int fd)
{
int v1; // eax

v1 = fcntl(fd, 1, 0LL);
return fcntl(fd, 2, v1 | 1u);
}

canonical_decompiled_code:
int __fastcall func3(int fd)
{
int v1; // eax

v1 = fcntl(fd, 1, 0LL);
return fcntl(fd, 2, v1 | 1u);
}

original_code:
int set_cloexec(int fd)
{
    int flags = fcntl(fd, 1, 0);
    do { if ((flags >= 0) < 0) { error(1, (*__errno_location ()), "fcntl error"); } } while (0);
    return fcntl(fd, 2, flags | 1);
}


decompiled_code:
__pid_t wait_children()
{
__pid_t result; // eax
int stat_loc[3]; // [rsp+Ch] [rbp-Ch] BYREF

stat_loc[0] = -1;
while ( 1 )
{
result = waitpid(-1, stat_loc, 0);
if ( result <= 0 )
break;
if ( !__OFSUB__((stat_loc[0] & 0x7F) + 1, 1) && (stat_loc[0] & 0x7F) != 0 )
fprintf(stderr, "child [%d] exit by signal %d\n", (unsigned int)result, stat_loc[0] & 0x7F);
}
return result;
}

canonical_decompiled_code:
__pid_t func4()
{
__pid_t result; // eax
int stat_loc[3]; // [rsp+Ch] [rbp-Ch] BYREF

stat_loc[0] = -1;
while ( 1 )
{
result = waitpid(-1, stat_loc, 0);
if ( result <= 0 )
break;
if ( !__OFSUB__((stat_loc[0] & 0x7F) + 1, 1) && (stat_loc[0] & 0x7F) != 0 )
fprintf(stderr, "child [%d] exit by signal %d\n", (unsigned int)result, stat_loc[0] & 0x7F);
}
return result;
}

original_code:
void wait_children()
{
    int status = -1;
    int pid = -1;
    while ( (pid = waitpid(-1, &status, 0)) > 0 ) {
        if ((((signed char) (((status) & 0x7f) + 1) >> 1) > 0)) {
            int signo = ((status) & 0x7f);
            fprintf(stderr, "child [%d] exit by signal %d\n", pid, signo);
        }
    }
}

canonical_original_code:
void wait_children()
{
    int status = -1;
    int pid = -1;
    while ( (pid = waitpid(-1, &status, 0)) > 0 ) {
        if ((((char) (((status) & 0x7f) + 1) >> 1) > 0)) {
            int signo = ((status) & 0x7f);
            fprintf(stderr, "child [%d] exit by signal %d\n", pid, signo);
        }
    }
}

variable_types: status int

variable_types: signo int


decompiled_code:
__int64 __fastcall spawn_process(__int64 a1, _DWORD *a2, int *a3)
{
char *v3; // rbx
__pid_t v5; // eax
char *v6; // rax
int *v7; // rax
int *v8; // rax
int *v9; // rax
int *v10; // rax
int *v11; // rax
int v13; // [rsp+0h] [rbp-38h] BYREF
int fd; // [rsp+4h] [rbp-34h]
int pipedes[2]; // [rsp+8h] [rbp-30h] BYREF

if ( pipe(pipedes) < 0 || pipe(&v13) < 0 )
{
LABEL_8:
v8 = _errno_location();
error(1, *v8, "pipe error");
goto LABEL_9;
}
v5 = fork();
LODWORD(v3) = v5;
if ( v5 < 0 )
{
LABEL_9:
v9 = _errno_location();
error(1, *v9, "fork error");
goto LABEL_10;
}
if ( !v5 )
{
close(pipedes[0]);
close(fd);
if ( dup2(v13, 0) < 0 )
{
LABEL_10:
v10 = _errno_location();
error(1, *v10, "dup2 error");
goto LABEL_11;
}
if ( dup2(pipedes[1], 1) < 0 )
{
LABEL_11:
v11 = _errno_location();
error(1, *v11, "dup2 error");
goto LABEL_12;
}
close(v13);
v3 = get_shell();
v6 = last_component(v3);
execl(v3, v6, "-c", a1, 0LL);
v7 = _errno_location();
error(1, *v7, "execl error");
goto LABEL_8;
}
LABEL_12:
close(pipedes[1]);
close(v13);
*a2 = pipedes[0];
*a3 = fd;
return (unsigned int)v3;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, _DWORD *a2, int *a3)
{
char *v3; // rbx
__pid_t v5; // eax
char *v6; // rax
int *v7; // rax
int *v8; // rax
int *v9; // rax
int *v10; // rax
int *v11; // rax
int v13; // [rsp+0h] [rbp-38h] BYREF
int fd; // [rsp+4h] [rbp-34h]
int pipedes[2]; // [rsp+8h] [rbp-30h] BYREF

if ( pipe(pipedes) < 0 || pipe(&v13) < 0 )
{
LABEL_8:
v8 = _errno_location();
error(1, *v8, "pipe error");
goto LABEL_9;
}
v5 = fork();
LODWORD(v3) = v5;
if ( v5 < 0 )
{
LABEL_9:
v9 = _errno_location();
error(1, *v9, "fork error");
goto LABEL_10;
}
if ( !v5 )
{
close(pipedes[0]);
close(fd);
if ( dup2(v13, 0) < 0 )
{
LABEL_10:
v10 = _errno_location();
error(1, *v10, "dup2 error");
goto LABEL_11;
}
if ( dup2(pipedes[1], 1) < 0 )
{
LABEL_11:
v11 = _errno_location();
error(1, *v11, "dup2 error");
goto LABEL_12;
}
close(v13);
v3 = func0();
v6 = func1(v3);
execl(v3, v6, "-c", a1, 0LL);
v7 = _errno_location();
error(1, *v7, "execl error");
goto LABEL_8;
}
LABEL_12:
close(pipedes[1]);
close(v13);
*a2 = pipedes[0];
*a3 = fd;
return (unsigned int)v3;
}

original_code:
int spawn_process(const char* cmd, int* in, int* out)
{
    int in_fds[2], out_fds[2];
    if (pipe(in_fds) < 0 || pipe(out_fds) < 0) {
        error(1, (*__errno_location ()), "pipe error");
    }
    int pid = fork();
    if (pid < 0) {
        error(1, (*__errno_location ()), "fork error");
    }
    if (pid == 0) {
        close(in_fds[0]);
        close(out_fds[1]);
        if (dup2(out_fds[0], 0) < 0) {
            error(1, (*__errno_location ()), "dup2 error");
        }
        if (dup2(in_fds[1], 1) < 0) {
            error(1, (*__errno_location ()), "dup2 error");
        }
        close(out_fds[0]);
        char* shell = get_shell();
        execl(shell, last_component(shell), "-c", cmd, ((void *)0));
        error(1, (*__errno_location ()), "execl error");
    }
    close(in_fds[1]);
    close(out_fds[0]);
    *in = in_fds[0];
    *out = out_fds[1];
    return pid;
}

canonical_original_code:
int spawn_process(char *cmd, int *in, int *out)
{
    int in_fds[2]; int out_fds[2];
    if (pipe(in_fds) < 0 || pipe(out_fds) < 0) {
        error(1, (*__errno_location ()), "pipe error");
    }
    int pid = fork();
    if (pid < 0) {
        error(1, (*__errno_location ()), "fork error");
    }
    if (pid == 0) {
        close(in_fds[0]);
        close(out_fds[1]);
        if (dup2(out_fds[0], 0) < 0) {
            error(1, (*__errno_location ()), "dup2 error");
        }
        if (dup2(in_fds[1], 1) < 0) {
            error(1, (*__errno_location ()), "dup2 error");
        }
        close(out_fds[0]);
        char *shell = get_shell();
        execl(shell, last_component(shell), "-c", cmd, ((void *)0));
        error(1, (*__errno_location ()), "execl error");
    }
    close(in_fds[1]);
    close(out_fds[0]);
    *in = in_fds[0];
    *out = out_fds[1];
    return pid;
}

variable_types:
cmd
char *

variable_types:
in
int *

variable_types:
out
int *


repo: limoges/luma

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
putchar(10);
printf("%s", "\x1B[1mET_BRIGHT \x1B[0m");
printf("%s", "\x1B[2mET_FAINT \x1B[0m");
printf("%s", "\x1B[3mET_ITALIC \x1B[0m");
printf("%s", "\x1B[4mET_UNDERLINE\x1B[0m");
putchar(10);
printf("%s", "\x1B[5mET_BLINK \x1B[0m");
printf("%s", "\x1B[7mET_REVERSE \x1B[0m");
printf("%s", "\x1B[9mET_CROSSED \x1B[0m");
putchar(10);
printf("%s", "\x1B[30mEF_BLACK \x1B[0m");
printf("%s", "\x1B[31mEF_RED \x1B[0m");
printf("%s", "\x1B[32mEF_GREEN \x1B[0m");
printf("%s", "\x1B[33mEF_YELLOW \x1B[0m");
putchar(10);
printf("%s", "\x1B[34mEF_BLUE \x1B[0m");
printf("%s", "\x1B[35mEF_MAGENTA \x1B[0m");
printf("%s", "\x1B[36mEF_CYAN \x1B[0m");
printf("%s", "\x1B[37mEF_WHITE \x1B[0m");
putchar(10);
printf("%s", "\x1B[40mEB_BLACK \x1B[0m");
printf("%s", "\x1B[41mEB_RED \x1B[0m");
printf("%s", "\x1B[42mEB_GREEN \x1B[0m");
printf("%s", "\x1B[43mEB_YELLOW \x1B[0m");
putchar(10);
printf("%s", "\x1B[44mEB_BLUE \x1B[0m");
printf("%s", "\x1B[45mEB_MAGENTA \x1B[0m");
printf("%s", "\x1B[46mEB_CYAN \x1B[0m");
printf("%s", "\x1B[47mEB_WHITE \x1B[0m");
exit(0);
}


fgetskipcr

repo: lindenb/hershey

decompiled_code:
int fgetskipcr()
{
int result; // eax

do
{
result = fgetc(stdin);
if ( result == -1 )
{
fwrite("Expected char but got EOF", 1uLL, 0x19uLL, stderr);
exit(1);
}
}
while ( result == 10 );
return result;
}

canonical_decompiled_code:
int func7()
{
int result; // eax

do
{
result = fgetc(stdin);
if ( result == -1 )
{
fwrite("Expected char but got EOF", 1uLL, 0x19uLL, stderr);
exit(1);
}
}
while ( result == 10 );
return result;
}

original_code:
static int fgetskipcr()
 {
 int c;
 while((c=fgetc(stdin))!=(-1))
  {
  if(c=='\n') continue;
  return c;
  }
 fprintf(stderr,"Expected char but got EOF");
 exit(1);
 }

variable_types: c int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er13
int v4; // eax
int v5; // ebx
const unsigned __int16 **v6; // rbp
unsigned int v7; // er14
int v8; // er14
int v9; // ebx
int v10; // er13
int i; // ebp
int v12; // eax
int v13; // eax

LABEL_17:
v3 = 0;
v7 = 0;
while ( 1 )
{
v4 = fgetc(stdin);
v5 = v4;
if ( v4 == -1 )
return 0;
if ( v4 == 10 )
{
v3 = 0;
}
else
{
++v3;
v6 = __ctype_b_loc();
if ( ((*v6)[v5] & 0x2000) == 0 )
v7 = v5 + 10 * v7 - 48;
if ( v3 > 4 )
{
if ( !v7 )
return v7;
printf("%d\t", v7);
v8 = 3;
v9 = 0;
do
{
v10 = fgetc(stdin);
fputc(v10, _bss_start);
if ( ((*v6)[v10] & 0x2000) == 0 )
v9 = v10 + 10 * v9 - 48;
--v8;
}
while ( v8 );
if ( v9 > 0 )
{
for ( i = 0; i != v9; ++i )
{
v12 = fgetskipcr();
putchar(v12);
v13 = fgetskipcr();
putchar(v13);
}
}
printf("\t%d", (unsigned int)v9);
fputc(10, _bss_start);
if ( fgetc(stdin) != 10 )
{
fwrite("syntax error\n", 1uLL, 0xDuLL, stderr);
return -1;
}
goto LABEL_17;
}
}
}
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // er13
int v4; // eax
int v5; // ebx
const unsigned __int16 **v6; // rbp
unsigned int v7; // er14
int v8; // er14
int v9; // ebx
int v10; // er13
int i; // ebp
int v12; // eax
int v13; // eax

LABEL_17:
v3 = 0;
v7 = 0;
while ( 1 )
{
v4 = fgetc(stdin);
v5 = v4;
if ( v4 == -1 )
return 0;
if ( v4 == 10 )
{
v3 = 0;
}
else
{
++v3;
v6 = __ctype_b_loc();
if ( ((*v6)[v5] & 0x2000) == 0 )
v7 = v5 + 10 * v7 - 48;
if ( v3 > 4 )
{
if ( !v7 )
return v7;
printf("%d\t", v7);
v8 = 3;
v9 = 0;
do
{
v10 = fgetc(stdin);
fputc(v10, _bss_start);
if ( ((*v6)[v10] & 0x2000) == 0 )
v9 = v10 + 10 * v9 - 48;
--v8;
}
while ( v8 );
if ( v9 > 0 )
{
for ( i = 0; i != v9; ++i )
{
v12 = func7();
putchar(v12);
v13 = func7();
putchar(v13);
}
}
printf("\t%d", (unsigned int)v9);
fputc(10, _bss_start);
if ( fgetc(stdin) != 10 )
{
fwrite("syntax error\n", 1uLL, 0xDuLL, stderr);
return -1;
}
goto LABEL_17;
}
}
}
}

original_code:
int main(int argc,char** argv)
 {
 for(;;)
  {
  int letter_id=0,num_vertices=0,i=0,vertex;
  int c;
  while(i< 5)
   {
   c=fgetc(stdin);
   if(c==(-1)) { letter_id=0; break;}
   if(c=='\n') {i=0;continue;}
   i++;
   if(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISspace)) continue;
   letter_id=letter_id*10+(c-'0');
   }
  if(letter_id==0) break;
  printf("%d\t",letter_id);
  for( i=0;i< 3;++i)
   {
   int c=fgetc(stdin);
   fprintf(stdout,"%c",c);
   if(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISspace)) continue;
   num_vertices = num_vertices*10+(c-'0');
   }
  for( vertex=0;vertex < num_vertices;++vertex )
   {
   int c1 = fgetskipcr();
   printf("%c",c1);
   c1 = fgetskipcr();
   printf("%c",c1);
   }
  printf("\t%d",num_vertices);
  fputc('\n',stdout);
  if((c=fgetc(stdin))!='\n')
   {
   fprintf(stderr, "syntax error\n");
   return -1;
   }
  }
 return 0;
 }

canonical_original_code:
int main(int argc, char **argv)
 {
 for(;;)
  {
  int letter_id = 0; int num_vertices = 0; int i = 0; int vertex;
  int c;
  while(i< 5)
   {
   c=fgetc(stdin);
   if(c==(-1)) { letter_id=0; break;}
   if(c=='\n') {i=0;continue;}
   i++;
   if(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISspace)) continue;
   letter_id=letter_id*10+(c-'0');
   }
  if(letter_id==0) break;
  printf("%d\t",letter_id);
  for( i=0;i< 3;++i)
   {
   int c = fgetc(stdin);
   fprintf(stdout,"%c",c);
   if(((*__ctype_b_loc ())[(int) ((c))] & (unsigned short) _ISspace)) continue;
   num_vertices = num_vertices*10+(c-'0');
   }
  for( vertex=0;vertex < num_vertices;++vertex )
   {
   int c1 = fgetskipcr();
   printf("%c",c1);
   c1 = fgetskipcr();
   printf("%c",c1);
   }
  printf("\t%d",num_vertices);
  fputc('\n',stdout);
  if((c=fgetc(stdin))!='\n')
   {
   fprintf(stderr, "syntax error\n");
   return -1;
   }
  }
 return 0;
 }

variable_types: argc int

variable_types:
argv
char **

variable_types: i int

variable_types: vertex int


connectTCP

repo: linfan/TCP-IP-Network-basic-examples

decompiled_code:
__int64 __fastcall connectTCP(char *a1, char a2)
{
return clientsock(a1, a2, (char)&unk_402004, 0LL);
}

canonical_decompiled_code:
__int64 __fastcall func7(char *a1, char a2)
{
return func12(a1, a2, (char)&unk_402004, 0LL);
}

original_code:
int connectTCP(const char* host, const char* port)
{
    return clientsock(host, port, "TCP", ((void *)0));
}

canonical_original_code:
int connectTCP(char *host, char *port)
{
    return clientsock(host, port, "TCP", ((void *)0));
}

variable_types:
host
char *

variable_types:
port
char *


readn

decompiled_code:
size_t __fastcall readn(int fd, void *buf, size_t n)
{
size_t v3; // r13
size_t v5; // rbx
ssize_t v6; // rax

v3 = n;
if ( !n )
return v3;
v5 = n;
while ( 1 )
{
v6 = recv(fd, buf, v5, 0);
if ( v6 < 0 )
break;
LABEL_3:
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3;
}
if ( *__errno_location() == 4 )
{
v6 = 0LL;
goto LABEL_3;
}
return -1LL;
}

canonical_decompiled_code:
size_t __fastcall func8(int fd, void *buf, size_t n)
{
size_t v3; // r13
size_t v5; // rbx
ssize_t v6; // rax

v3 = n;
if ( !n )
return v3;
v5 = n;
while ( 1 )
{
v6 = recv(fd, buf, v5, 0);
if ( v6 < 0 )
break;
LABEL_3:
buf = (char *)buf + v6;
v5 -= v6;
if ( !v5 )
return v3;
}
if ( *__errno_location() == 4 )
{
v6 = 0LL;
goto LABEL_3;
}
return -1LL;
}

original_code:
ssize_t readn(int fd, void* vptr, size_t n)
{
    size_t nleft;
    ssize_t nread;
    char* ptr;
    ptr = vptr;
    nleft = n;
    while (nleft > 0)
    {
        if ((nread = recv(fd, ptr, nleft, 0)) < 0)
        {
            if ((*__errno_location ()) == 4)
                nread = 0;
            else
                return -1;
        }
        nleft -= nread;
        ptr += nread;
    }
    if (n - nleft < 0)
        echo("[CLIENT] Error: readn error.\n");
    return (n - nleft);
}

canonical_original_code:
long readn(int fd, void *vptr, unsigned long n)
{
    unsigned long nleft;
    long nread;
    char *ptr;
    ptr = vptr;
    nleft = n;
    while (nleft > 0)
    {
        if ((nread = recv(fd, ptr, nleft, 0)) < 0)
        {
            if ((*__errno_location ()) == 4)
                nread = 0;
            else
                return -1;
        }
        nleft -= nread;
        ptr += nread;
    }
    if (n - nleft < 0)
        echo("[CLIENT] Error: readn error.\n");
    return (n - nleft);
}

variable_types: fd int

variable_types:
vptr
void *

variable_types:
n
unsigned long

variable_types:
nleft
unsigned long

variable_types: nread long

variable_types:
ptr
char *

return_type: long


errexit

decompiled_code:
void __noreturn errexit(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
exit(1);
}

canonical_decompiled_code:
void __noreturn func9(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
exit(1);
}

original_code:
int errexit(const char* format, ...)
{
    va_list args;
    __builtin_va_start(args,format);
    vfprintf(stderr, format, args);
    __builtin_va_end(args);
    exit(1);
}

canonical_original_code:
int errexit(char *format, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,format);
    vfprintf(stderr, format, args);
    __builtin_va_end(args);
    exit(1);
}

variable_types:
format
char *


echo

decompiled_code:
int echo(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
return vfprintf(stderr, format, arg);
}

canonical_decompiled_code:
int func10(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
return vfprintf(stderr, format, arg);
}

original_code:
void echo(const char* format, ...)
{
    va_list args;
    __builtin_va_start(args,format);
    vfprintf(stderr, format, args);
    __builtin_va_end(args);
}

canonical_original_code:
void echo(char *format, ...)
{
    __builtin_va_list args;
    __builtin_va_start(args,format);
    vfprintf(stderr, format, args);
    __builtin_va_end(args);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
int v4; // er15
__int64 v5; // r14
int i; // ebx
__pid_t v8; // er12
int v9; // ebp
size_t v10; // rax
char v11; // [rsp+0h] [rbp-1058h]
char *v12; // [rsp+0h] [rbp-1058h]
const char *v13; // [rsp+8h] [rbp-1050h]
char buf[4102]; // [rsp+10h] [rbp-1048h] BYREF
char s[66]; // [rsp+1016h] [rbp-42h] BYREF

if ( argc == 6 )
{
v12 = (char *)argv[1];
v13 = argv[2];
v3 = atoi(argv[3]);
v4 = atoi(argv[4]);
v5 = (unsigned int)atoi(argv[5]);
snprintf(s, 0xAuLL, "%d", v5);
if ( v3 <= 0 )
return 0;
}
else
{
echo("usage : client <hostname or IPaddr> <port> <#children> <#loops/child> <#bytes/request>\n", v11);
snprintf(s, 0xAuLL, "%d", 4000LL);
LODWORD(v5) = 4000;
v4 = 500;
v3 = 10;
v13 = "10011";
v12 = "127.0.0.1";
}
for ( i = 0; i < v3; ++i )
{
v8 = fork();
if ( !v8 )
{
if ( v4 > 0 )
{
do
{
v9 = connectTCP(v12, (char)v13);
v10 = strlen(s);
send(v9, s, v10, 0);
if ( (int)v5 != readn(v9, buf, (int)v5) )
echo("[CLIENT] Error: request %d bytes, received %d bytes\n", (char)v12);
close(v9);
++v8;
}
while ( v8 != v4 );
}
echo("[CLIENT] child %d done.\n", (char)v12);
exit(0);
}
while ( wait(0LL) > 0 )
;
if ( *__errno_location() != 10 )
errexit("[CLIENT] wait failed.\n", 10);
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebp
int v4; // er15
__int64 v5; // r14
int i; // ebx
__pid_t v8; // er12
int v9; // ebp
size_t v10; // rax
char v11; // [rsp+0h] [rbp-1058h]
char *v12; // [rsp+0h] [rbp-1058h]
const char *v13; // [rsp+8h] [rbp-1050h]
char buf[4102]; // [rsp+10h] [rbp-1048h] BYREF
char s[66]; // [rsp+1016h] [rbp-42h] BYREF

if ( argc == 6 )
{
v12 = (char *)argv[1];
v13 = argv[2];
v3 = atoi(argv[3]);
v4 = atoi(argv[4]);
v5 = (unsigned int)atoi(argv[5]);
snprintf(s, 0xAuLL, "%d", v5);
if ( v3 <= 0 )
return 0;
}
else
{
func10("usage : client <hostname or IPaddr> <port> <#children> <#loops/child> <#bytes/request>\n", v11);
snprintf(s, 0xAuLL, "%d", 4000LL);
LODWORD(v5) = 4000;
v4 = 500;
v3 = 10;
v13 = "10011";
v12 = "127.0.0.1";
}
for ( i = 0; i < v3; ++i )
{
v8 = fork();
if ( !v8 )
{
if ( v4 > 0 )
{
do
{
v9 = func7(v12, (char)v13);
v10 = strlen(s);
send(v9, s, v10, 0);
if ( (int)v5 != func8(v9, buf, (int)v5) )
func10("[CLIENT] Error: request %d bytes, received %d bytes\n", (char)v12);
close(v9);
++v8;
}
while ( v8 != v4 );
}
func10("[CLIENT] child %d done.\n", (char)v12);
exit(0);
}
while ( wait(0LL) > 0 )
;
if ( *__errno_location() != 10 )
func9("[CLIENT] wait failed.\n", 10);
}
return 0;
}

original_code:
int main(int argc,char* argv[])
{
    int i, j, fd;
    pid_t pid;
    ssize_t n;
    char request[10], reply[4096];
    char *hostname = "127.0.0.1";
    char *port = "10011";
    int nchildren = 10;
    int nloops = 500;
    int nbytes = 4000;
    if (argc != 6)
        echo("usage : client <hostname or IPaddr> <port> <#children> <#loops/child> <#bytes/request>\n");
    else
    {
        hostname = argv[1];
        port = argv[2];
        nchildren = atoi(argv[3]);
        nloops = atoi(argv[4]);
        nbytes = atoi(argv[5]);
    }
    snprintf(request, sizeof(request), "%d", nbytes);
    for (i = 0; i < nchildren; ++i)
    {
        if ((pid = fork()) == 0)
        {
            for (j = 0; j < nloops; ++j)
            {
                fd = connectTCP(hostname, port);
                send(fd, request, strlen(request), 0);
                if ((n = readn(fd, reply, nbytes)) != nbytes)
                    echo("[CLIENT] Error: request %d bytes, received %d bytes\n", nbytes, n);
                close(fd);
            }
            echo("[CLIENT] child %d done.\n", i);
            exit(0);
        }
        while(wait(((void *)0)) > 0)
            ;
        if ((*__errno_location ()) != 10)
            errexit("[CLIENT] wait failed.\n");
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int i; int j; int fd;
    int pid;
    long n;
    char request[10]; char reply[4096];
    char *hostname = "127.0.0.1";
    char *port = "10011";
    int nchildren = 10;
    int nloops = 500;
    int nbytes = 4000;
    if (argc != 6)
        echo("usage : client <hostname or IPaddr> <port> <#children> <#loops/child> <#bytes/request>\n");
    else
    {
        hostname = argv[1];
        port = argv[2];
        nchildren = atoi(argv[3]);
        nloops = atoi(argv[4]);
        nbytes = atoi(argv[5]);
    }
    snprintf(request, sizeof(request), "%d", nbytes);
    for (i = 0; i < nchildren; ++i)
    {
        if ((pid = fork()) == 0)
        {
            for (j = 0; j < nloops; ++j)
            {
                fd = connectTCP(hostname, port);
                send(fd, request, strlen(request), 0);
                if ((n = readn(fd, reply, nbytes)) != nbytes)
                    echo("[CLIENT] Error: request %d bytes, received %d bytes\n", nbytes, n);
                close(fd);
            }
            echo("[CLIENT] child %d done.\n", i);
            exit(0);
        }
        while(wait(((void *)0)) > 0)
            ;
        if ((*__errno_location ()) != 10)
            errexit("[CLIENT] wait failed.\n");
    }
    return 0;
}

variable_types:
argv
char *[]

variable_types: j int

variable_types: pid int

variable_types: n long

variable_types:
request
char [10]

variable_types:
reply
char [4096]

variable_types:
hostname
char *

variable_types: nchildren int

variable_types: nloops int

variable_types: nbytes int


recvFromServer

decompiled_code:
__int64 __fastcall recvFromServer(int fd, __int64 a2, int a3)
{
int *v4; // r14
int v5; // edx
int v6; // ebx
int v7; // eax
char v9; // [rsp+0h] [rbp-28h]

v4 = __errno_location();
*v4 = 0;
v5 = a3;
v6 = 0;
if ( a3 > 0 )
{
do
{
v7 = recv(fd, (void *)(a2 + v6), v5, 64);
if ( v7 <= 0 )
break;
v6 += v7;
v5 = a3 - v6;
}
while ( a3 - v6 > 0 );
if ( !v6 && *v4 == 11 )
{
echo("[CLIENT] no message received.\n", v9);
v6 = -1;
}
}
return (unsigned int)v6;
}

canonical_decompiled_code:
__int64 __fastcall func9(int fd, __int64 a2, int a3)
{
int *v4; // r14
int v5; // edx
int v6; // ebx
int v7; // eax
char v9; // [rsp+0h] [rbp-28h]

v4 = __errno_location();
*v4 = 0;
v5 = a3;
v6 = 0;
if ( a3 > 0 )
{
do
{
v7 = recv(fd, (void *)(a2 + v6), v5, 64);
if ( v7 <= 0 )
break;
v6 += v7;
v5 = a3 - v6;
}
while ( a3 - v6 > 0 );
if ( !v6 && *v4 == 11 )
{
func12("[CLIENT] no message received.\n", v9);
v6 = -1;
}
}
return (unsigned int)v6;
}

original_code:
int recvFromServer(int sockfd, char* buf, int buf_size)
{
    int n, offset = 0;
    (*__errno_location ()) = 0;
    while (buf_size - offset > 0 &&
            (n = recv(sockfd, buf + offset, buf_size - offset, MSG_DONTWAIT)) > 0)
    {
        offset += n;
    }
    if (offset == 0 && (*__errno_location ()) == 11)
    {
        echo("[CLIENT] no message received.\n");
        return -1;
    }
    else
    {
        return offset;
    }
}

canonical_original_code:
int recvFromServer(int sockfd, char *buf, int buf_size)
{
    int n; int offset = 0;
    (*__errno_location ()) = 0;
    while (buf_size - offset > 0 &&
            (n = recv(sockfd, buf + offset, buf_size - offset, MSG_DONTWAIT)) > 0)
    {
        offset += n;
    }
    if (offset == 0 && (*__errno_location ()) == 11)
    {
        echo("[CLIENT] no message received.\n");
        return -1;
    }
    else
    {
        return offset;
    }
}

variable_types: sockfd int

variable_types:
buf
char *

variable_types: n int

variable_types: offset int


errexit

canonical_decompiled_code:
void __noreturn func11(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
vfprintf(stderr, format, arg);
exit(1);
}


echo

canonical_decompiled_code:
int func12(char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, format);
return vfprintf(stderr, format, arg);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbx
__int16 v4; // r12
struct hostent *v5; // rbp
int v6; // ebx
struct in_addr v7; // er12
int *v9; // rax
char v10; // al
int *v11; // rax
char v12; // al
struct sockaddr in[2]; // [rsp+0h] [rbp-28h] BYREF

switch ( argc )
{
case 2:
v3 = argv[1];
v4 = 10012;
break;
case 3:
v3 = argv[1];
v4 = atoi(argv[2]);
break;
case 1:
v4 = 10012;
v3 = "127.0.0.1";
break;
default:
errexit("usage: %s [host [port]]\n", (char)*argv);
}
v5 = gethostbyname(v3);
if ( !v5 )
errexit("gethostbyname failed.\n", (char)argv);
v6 = socket(2, 1, 0);
if ( v6 < 0 )
{
v9 = __errno_location();
v10 = (unsigned __int8)strerror(*v9);
errexit("create socket failed: %s\n", v10);
}
*(_QWORD *)&in[0].sa_family = 2LL;
*(_QWORD *)&in[0].sa_data[6] = 0LL;
*(_WORD *)in[0].sa_data = _bswap_16(v4);
v7.s_addr = **(_DWORD **)v5->h_addr_list;
_bswap_16(*(__int16 *)in[0].sa_data);
inet_ntoa(v7);
echo("[CLIENT] server addr: %s, port: %u\n", 2);
if ( connect(v6, in, 0x10u) < 0 )
{
v11 = __errno_location();
v12 = (unsigned __int8)strerror(*v11);
errexit("connect to server failed: %s\n", v12);
}
inet_ntoa(*(struct in_addr *)&in[0].sa_data[2]);
echo("[CLIENT] connected to server %s\n", in[0].sa_family);
process(_bss_start, v6);
close(v6);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v3; // rbx
__int16 v4; // r12
struct hostent *v5; // rbp
int v6; // ebx
struct in_addr v7; // er12
int *v9; // rax
char v10; // al
int *v11; // rax
char v12; // al
struct sockaddr in[2]; // [rsp+0h] [rbp-28h] BYREF

switch ( argc )
{
case 2:
v3 = argv[1];
v4 = 10012;
break;
case 3:
v3 = argv[1];
v4 = atoi(argv[2]);
break;
case 1:
v4 = 10012;
v3 = "127.0.0.1";
break;
default:
func11("usage: %s [host [port]]\n", (char)*argv);
}
v5 = gethostbyname(v3);
if ( !v5 )
func11("gethostbyname failed.\n", (char)argv);
v6 = socket(2, 1, 0);
if ( v6 < 0 )
{
v9 = __errno_location();
v10 = (unsigned __int8)strerror(*v9);
func11("create socket failed: %s\n", v10);
}
*(_QWORD *)&in[0].sa_family = 2LL;
*(_QWORD *)&in[0].sa_data[6] = 0LL;
*(_WORD *)in[0].sa_data = func7(v4);
v7.s_addr = **(_DWORD **)v5->h_addr_list;
func7(*(__int16 *)in[0].sa_data);
inet_ntoa(v7);
func12("[CLIENT] server addr: %s, port: %u\n", 2);
if ( connect(v6, in, 0x10u) < 0 )
{
v11 = __errno_location();
v12 = (unsigned __int8)strerror(*v11);
func11("connect to server failed: %s\n", v12);
}
inet_ntoa(*(struct in_addr *)&in[0].sa_data[2]);
func12("[CLIENT] connected to server %s\n", in[0].sa_family);
func10(_bss_start, v6);
close(v6);
return 0;
}

original_code:
int main(int argc,char* argv[])
{
    int sock;
    struct hostent *hent;
    struct sockaddr_in server;
    char *host = "127.0.0.1";
    unsigned short port = 10012;
    switch (argc)
    {
        case 1:
            break;
        case 2:
            host = argv[1];
            break;
        case 3:
            host = argv[1];
            port = atoi(argv[2]);
            break;
        default:
            errexit("usage: %s [host [port]]\n", argv[0]);
    }
    if ((hent = gethostbyname(host)) == ((void *)0))
        errexit("gethostbyname failed.\n");
    if ((sock = socket(2, SOCK_STREAM, 0)) < 0)
        errexit("create socket failed: %s\n", strerror((*__errno_location ())));
    bzero(&server, sizeof(server));
    server.sin_family = 2;
    server.sin_port = htons(port);
    server.sin_addr = *((struct in_addr*)hent->h_addr_list[0]);
    echo("[CLIENT] server addr: %s, port: %u\n", inet_ntoa(server.sin_addr), ntohs(server.sin_port));
    if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0)
        errexit("connect to server failed: %s\n", strerror((*__errno_location ())));
    echo("[CLIENT] connected to server %s\n", inet_ntoa(server.sin_addr));
    process(stdin, sock);
    close(sock);
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int sock;
    struct hostent *hent;
    struct sockaddr_in server;
    char *host = "127.0.0.1";
    unsigned short port = 10012;
    switch (argc)
    {
        case 1:
            break;
        case 2:
            host = argv[1];
            break;
        case 3:
            host = argv[1];
            port = atoi(argv[2]);
            break;
        default:
            errexit("usage: %s [host [port]]\n", argv[0]);
    }
    if ((hent = gethostbyname(host)) == ((void *)0))
        errexit("gethostbyname failed.\n");
    if ((sock = socket(2, SOCK_STREAM, 0)) < 0)
        errexit("create socket failed: %s\n", strerror((*__errno_location ())));
    bzero(&server, sizeof(server));
    server.sin_family = 2;
    server.sin_port = htons(port);
    server.sin_addr = *((struct in_addr *)hent->h_addr_list[0]);
    echo("[CLIENT] server addr: %s, port: %u\n", inet_ntoa(server.sin_addr), ntohs(server.sin_port));
    if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0)
        errexit("connect to server failed: %s\n", strerror((*__errno_location ())));
    echo("[CLIENT] connected to server %s\n", inet_ntoa(server.sin_addr));
    process(stdin, sock);
    close(sock);
}

variable_types: argc int

variable_types: sock int

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

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

variable_types:
host
char *

variable_types:
port
unsigned short

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

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


connectUDP

decompiled_code:
__int64 __fastcall connectUDP(char *a1, char a2, void *a3)
{
return clientsock(a1, a2, (char)&unk_402004, a3);
}

canonical_decompiled_code:
__int64 __fastcall func8(char *a1, char a2, void *a3)
{
return func10(a1, a2, (char)&unk_402004, a3);
}

original_code:
int connectUDP(const char *host, const char *service, struct sockaddr_in* sin)
{
    return clientsock(host, service, "UDP", sin);
}

canonical_original_code:
int connectUDP(char *host, char *service, struct sockaddr_in *sin)
{
    return clientsock(host, service, "UDP", sin);
}

variable_types:
service
char *

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


errexit

variable_types:
format
char *