step

decompiled_code:
__int64 step()
{
int v0; // er12
int v1; // eax
int i; // ebx
int v3; // ebp

memcpy(life, *(const void **)(srf + 32), sizeof(life));
v0 = 0;
while ( 2 )
{
for ( i = 0; i != 800; ++i )
{
v3 = living_neighbours(i, v0);
if ( !(unsigned int)get_point(i, v0) )
{
v1 = rand();
if ( v1 == 1000 * (v1 / 1000) )
{
LABEL_3:
set_point(i, v0, 65280);
continue;
}
}
if ( v3 <= 1 || v3 > 3 )
{
set_point(i, v0, 0);
}
else if ( v3 == 3 )
{
goto LABEL_3;
}
}
if ( ++v0 != 800 )
continue;
return SDL_UpdateRect();
}
}

canonical_decompiled_code:
__int64 func19()
{
int v0; // er12
int v1; // eax
int i; // ebx
int v3; // ebp

memcpy(life, *(const void **)(srf + 32), sizeof(life));
v0 = 0;
while ( 2 )
{
for ( i = 0; i != 800; ++i )
{
v3 = func18(i, v0);
if ( !(unsigned int)func7(i, v0) )
{
v1 = rand();
if ( v1 == 1000 * (v1 / 1000) )
{
LABEL_3:
func8(i, v0, 65280);
continue;
}
}
if ( v3 <= 1 || v3 > 3 )
{
func8(i, v0, 0);
}
else if ( v3 == 3 )
{
goto LABEL_3;
}
}
if ( ++v0 != 800 )
continue;
return SDL_UpdateRect();
}
}

original_code:
void step() {
  memcpy(life, srf->pixels, 800*800*4);
  int x, y;
  for(y=0;y<800;y++) {
    for(x=0;x<800;x++) {
      int n = living_neighbours(x, y);
      if(!get_point(x,y) && !(rand() % 1000)) {
        set_point(x, y, 0x0000ff00);
        continue;
      }
      if(n < 2) {
        set_point(x, y, 0x00000000);
        continue;
      }
      if(n > 3) {
        set_point(x, y, 0x00000000);
        continue;
      }
      if(n == 3) {
        set_point(x, y, 0x0000ff00);
      }
    }
  }
  SDL_UpdateRect(srf, 0, 0, 0, 0);
}

canonical_original_code:
void step() {
  memcpy(life, srf->pixels, 800*800*4);
  int x; int y;
  for(y=0;y<800;y++) {
    for(x=0;x<800;x++) {
      int n = living_neighbours(x, y);
      if(!get_point(x,y) && !(rand() % 1000)) {
        set_point(x, y, 0x0000ff00);
        continue;
      }
      if(n < 2) {
        set_point(x, y, 0x00000000);
        continue;
      }
      if(n > 3) {
        set_point(x, y, 0x00000000);
        continue;
      }
      if(n == 3) {
        set_point(x, y, 0x0000ff00);
      }
    }
  }
  SDL_UpdateRect(srf, 0, 0, 0, 0);
}

variable_types: n int


repo: selavy/anothersim

original_code:
int check_stall( int reg )
{
  if( MEM1 != -1 )
    if( ( (*Instructions[ MEM1 ]).itype == LD ) && ( (*Instructions[ MEM1 ]).rt == reg ) ) return 1;
  if( MEM2 != -1 )
    if( ( (*Instructions[ MEM2 ]).itype == LD ) && ( (*Instructions[ MEM2 ]).rt == reg ) ) return 1;
  return 0;
}

canonical_original_code:
int check_stall(int reg)
{
  if( MEM1 != -1 )
    if( ( (*Instructions[ MEM1 ]).itype == LD ) && ( (*Instructions[ MEM1 ]).rt == reg ) ) return 1;
  if( MEM2 != -1 )
    if( ( (*Instructions[ MEM2 ]).itype == LD ) && ( (*Instructions[ MEM2 ]).rt == reg ) ) return 1;
  return 0;
}

variable_types: reg int

return_type: int


decompiled_code:
void if1()
{
if ( !flush && (!IF1_stall || IF1 == -1) )
{
if ( inst_counter >= instcount )
{
IF1 = -1;
}
else
{
IF1 = inst_counter;
IF1_count = inst_cycle;
if ( inst_counter != -1 )
{
fprintf(fout, "I%d-IF1 ", (unsigned int)inst_cycle);
++inst_counter;
return;
}
}
++inst_counter;
}
}

canonical_decompiled_code:
void func7()
{
if ( !flush && (!IF1_stall || IF1 == -1) )
{
if ( inst_counter >= instcount )
{
IF1 = -1;
}
else
{
IF1 = inst_counter;
IF1_count = inst_cycle;
if ( inst_counter != -1 )
{
fprintf(fout, "I%d-IF1 ", (unsigned int)inst_cycle);
++inst_counter;
return;
}
}
++inst_counter;
}
}

original_code:
void if1()
{
  if( flush ) return;
  if( IF1_stall && (IF1 != -1) ) return;
  if( inst_counter < instcount )
    {
      IF1 = inst_counter;
      IF1_count = inst_cycle;
    }
  else
    IF1 = -1;
  if( IF1 != -1 )
    fprintf( fout, "I%d-IF1 ", IF1_count );
  inst_counter++;
}

return_type: void


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

if ( IF2_stall )
{
if ( IF2 != -1 )
result = fprintf(fout, "I%d-stall ", (unsigned int)IF2_count);
}
else if ( IF2 != -1 )
{
result = fprintf(fout, "I%d-IF2 ", (unsigned int)IF2_count);
}
return result;
}

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

if ( IF2_stall )
{
if ( IF2 != -1 )
result = fprintf(fout, "I%d-stall ", (unsigned int)IF2_count);
}
else if ( IF2 != -1 )
{
result = fprintf(fout, "I%d-IF2 ", (unsigned int)IF2_count);
}
return result;
}

original_code:
void if2()
{
  if( IF2_stall && (IF2 != -1) )
    {
      fprintf( fout, "I%d-stall ", IF2_count );
      return;
    }
  if( IF2 != -1 )
    fprintf( fout, "I%d-IF2 ", IF2_count );
}


id

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

if ( ID_stall )
{
if ( ID != -1 )
result = fprintf(fout, "I%d-stall ", (unsigned int)ID_count);
}
else if ( ID != -1 )
{
result = fprintf(fout, "I%d-ID ", (unsigned int)ID_count);
}
return result;
}

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

if ( ID_stall )
{
if ( ID != -1 )
result = fprintf(fout, "I%d-stall ", (unsigned int)ID_count);
}
else if ( ID != -1 )
{
result = fprintf(fout, "I%d-ID ", (unsigned int)ID_count);
}
return result;
}

original_code:
void id()
{
  if( ID_stall && (ID != -1) )
    {
      fprintf( fout, "I%d-stall ", ID_count );
      return;
    }
  if( ID != -1 )
    fprintf( fout, "I%d-ID ", ID_count );
}


ex

decompiled_code:
int ex()
{
int result; // eax
_DWORD *v1; // rbx
int v2; // eax
int v3; // ebp
int v4; // er12
int v5; // eax
int v6; // ebp
int v7; // er12
int v8; // ebp
int v9; // er14
__int64 v10; // rbp
const char *v11; // rbx

result = EX;
if ( EX != -1 )
{
v1 = *(&Instructions + EX);
if ( EX_stall )
{
if ( check_stall(v1[68]) || check_stall(v1[69]) )
return fprintf(fout, "I%d-stall ", (unsigned int)EX_count);
EX_stall = 0;
ID_stall = 0;
IF2_stall = 0;
IF1_stall = 0;
}
v2 = *v1;
if ( !*v1 )
{
v3 = v1[68];
if ( !check_stall(v3) )
{
v4 = v1[69];
if ( !check_stall(v4) )
{
if ( v4 == -1 )
v5 = v1[138];
else
v5 = Registers[v4];
Registers[v1[70]] = Registers[v3] + v5;
return fprintf(fout, "I%d-EX ", (unsigned int)EX_count);
}
}
LABEL_13:
EX_stall = 1;
ID_stall = 1;
IF2_stall = 1;
IF1_stall = 1;
return fprintf(fout, "I%d-stall ", (unsigned int)EX_count);
}
if ( v2 == 1 )
{
v6 = v1[68];
if ( check_stall(v6) )
goto LABEL_13;
v7 = v1[69];
if ( check_stall(v7) )
goto LABEL_13;
if ( v7 == -1 )
Registers[v1[70]] = Registers[v6] - v1[138];
else
Registers[v1[70]] = Registers[v6] - Registers[v7];
return fprintf(fout, "I%d-EX ", (unsigned int)EX_count);
}
if ( v2 != 4 )
return fprintf(fout, "I%d-EX ", (unsigned int)EX_count);
v8 = v1[68];
result = check_stall(v8);
if ( result )
goto LABEL_13;
if ( !Registers[v8] )
{
EX = -1;
return fprintf(fout, "I%d-EX ", (unsigned int)EX_count);
}
v9 = instcount;
if ( instcount > 0 )
{
v10 = 0LL;
v11 = (const char *)(v1 + 71);
while ( 1 )
{
result = strcmp((const char *)*(&Instructions + v10) + 4, v11);
if ( !result )
break;
if ( ++v10 == v9 )
return result;
}
if ( v9 > (int)v10 )
{
inst_counter = v10;
flush = 1;
return fprintf(fout, "I%d-EX ", (unsigned int)EX_count);
}
}
}
return result;
}

canonical_decompiled_code:
int func10()
{
int result; // eax
_DWORD *v1; // rbx
int v2; // eax
int v3; // ebp
int v4; // er12
int v5; // eax
int v6; // ebp
int v7; // er12
int v8; // ebp
int v9; // er14
__int64 v10; // rbp
const char *v11; // rbx

result = EX;
if ( EX != -1 )
{
v1 = *(&Instructions + EX);
if ( EX_stall )
{
if ( func6(v1[68]) || func6(v1[69]) )
return fprintf(fout, "I%d-stall ", (unsigned int)EX_count);
EX_stall = 0;
ID_stall = 0;
IF2_stall = 0;
IF1_stall = 0;
}
v2 = *v1;
if ( !*v1 )
{
v3 = v1[68];
if ( !func6(v3) )
{
v4 = v1[69];
if ( !func6(v4) )
{
if ( v4 == -1 )
v5 = v1[138];
else
v5 = Registers[v4];
Registers[v1[70]] = Registers[v3] + v5;
return fprintf(fout, "I%d-EX ", (unsigned int)EX_count);
}
}
LABEL_13:
EX_stall = 1;
ID_stall = 1;
IF2_stall = 1;
IF1_stall = 1;
return fprintf(fout, "I%d-stall ", (unsigned int)EX_count);
}
if ( v2 == 1 )
{
v6 = v1[68];
if ( func6(v6) )
goto LABEL_13;
v7 = v1[69];
if ( func6(v7) )
goto LABEL_13;
if ( v7 == -1 )
Registers[v1[70]] = Registers[v6] - v1[138];
else
Registers[v1[70]] = Registers[v6] - Registers[v7];
return fprintf(fout, "I%d-EX ", (unsigned int)EX_count);
}
if ( v2 != 4 )
return fprintf(fout, "I%d-EX ", (unsigned int)EX_count);
v8 = v1[68];
result = func6(v8);
if ( result )
goto LABEL_13;
if ( !Registers[v8] )
{
EX = -1;
return fprintf(fout, "I%d-EX ", (unsigned int)EX_count);
}
v9 = instcount;
if ( instcount > 0 )
{
v10 = 0LL;
v11 = (const char *)(v1 + 71);
while ( 1 )
{
result = strcmp((const char *)*(&Instructions + v10) + 4, v11);
if ( !result )
break;
if ( ++v10 == v9 )
return result;
}
if ( v9 > (int)v10 )
{
inst_counter = v10;
flush = 1;
return fprintf(fout, "I%d-EX ", (unsigned int)EX_count);
}
}
}
return result;
}

original_code:
void ex()
{
  struct inst * curr;
  int i;
  if( EX == -1 )
    return;
  curr = Instructions[EX];
  if( EX_stall )
    {
      if( check_stall( (*curr).rs ) || check_stall( (*curr).rt ) )
 {
   fprintf( fout, "I%d-stall ", EX_count );
   return;
 }
      else
 {
   IF1_stall = IF2_stall = ID_stall = EX_stall = 0;
 }
    }
  if( (*curr).itype == DADD )
    {
      if( check_stall( (*curr).rs ) || check_stall( (*curr).rt ) )
 {
   IF1_stall = IF2_stall = ID_stall = EX_stall = 1;
   fprintf( fout, "I%d-stall ", EX_count );
   return;
 }
      if( (*curr).rt == -1 )
 Registers[ ( ((*curr).rd) ) ] = Registers[ ( ((*curr).rs) ) ] + (*curr).value;
      else
 Registers[ ( ((*curr).rd) ) ] = Registers[ ( ((*curr).rs) ) ] + Registers[ ( ((*curr).rt) ) ];
    }
  else if( (*curr).itype == SUB )
    {
      if( check_stall( (*curr).rs ) || check_stall( (*curr).rt ) )
 {
   IF1_stall = IF2_stall = ID_stall = EX_stall = 1;
   fprintf( fout, "I%d-stall ", EX_count );
   return;
 }
      if( (*curr).rt == -1 )
 Registers[ ( ((*curr).rd) ) ] = Registers[ ( ((*curr).rs) ) ] - (*curr).value;
      else
 Registers[ ( ((*curr).rd) ) ] = Registers[ ( ((*curr).rs) ) ] - Registers[ ( ((*curr).rt) ) ];
    }
  else if( (*curr).itype == BNEZ )
    {
      if( check_stall( (*curr).rs ) )
 {
   IF1_stall = IF2_stall = ID_stall = EX_stall = 1;
   fprintf( fout, "I%d-stall ", EX_count );
   return;
 }
      if( Registers[ ( ((*curr).rs) ) ] != 0 )
 {
   for( i = 0; i < instcount; ++i )
     {
       if( strcmp( (*Instructions[i]).label, (*curr).btarget ) == 0 )
  break;
     }
   if( i >= instcount )
     return;
   inst_counter = i;
   flush = 1;
 }
      else
 EX = -1;
    }
  fprintf( fout, "I%d-EX ", EX_count );
}

canonical_original_code:
void ex()
{
  struct inst *curr;
  int i;
  if( EX == -1 )
    return;
  curr = Instructions[EX];
  if( EX_stall )
    {
      if( check_stall( (*curr).rs ) || check_stall( (*curr).rt ) )
 {
   fprintf( fout, "I%d-stall ", EX_count );
   return;
 }
      else
 {
   IF1_stall = IF2_stall = ID_stall = EX_stall = 0;
 }
    }
  if( (*curr).itype == DADD )
    {
      if( check_stall( (*curr).rs ) || check_stall( (*curr).rt ) )
 {
   IF1_stall = IF2_stall = ID_stall = EX_stall = 1;
   fprintf( fout, "I%d-stall ", EX_count );
   return;
 }
      if( (*curr).rt == -1 )
 Registers[ ( ((*curr).rd) ) ] = Registers[ ( ((*curr).rs) ) ] + (*curr).value;
      else
 Registers[ ( ((*curr).rd) ) ] = Registers[ ( ((*curr).rs) ) ] + Registers[ ( ((*curr).rt) ) ];
    }
  else if( (*curr).itype == SUB )
    {
      if( check_stall( (*curr).rs ) || check_stall( (*curr).rt ) )
 {
   IF1_stall = IF2_stall = ID_stall = EX_stall = 1;
   fprintf( fout, "I%d-stall ", EX_count );
   return;
 }
      if( (*curr).rt == -1 )
 Registers[ ( ((*curr).rd) ) ] = Registers[ ( ((*curr).rs) ) ] - (*curr).value;
      else
 Registers[ ( ((*curr).rd) ) ] = Registers[ ( ((*curr).rs) ) ] - Registers[ ( ((*curr).rt) ) ];
    }
  else if( (*curr).itype == BNEZ )
    {
      if( check_stall( (*curr).rs ) )
 {
   IF1_stall = IF2_stall = ID_stall = EX_stall = 1;
   fprintf( fout, "I%d-stall ", EX_count );
   return;
 }
      if( Registers[ ( ((*curr).rs) ) ] != 0 )
 {
   for( i = 0; i < instcount; ++i )
     {
       if( strcmp( (*Instructions[i]).label, (*curr).btarget ) == 0 )
  break;
     }
   if( i >= instcount )
     return;
   inst_counter = i;
   flush = 1;
 }
      else
 EX = -1;
    }
  fprintf( fout, "I%d-EX ", EX_count );
}

variable_types: struct inst { enum inst_t { DADD=0, SUB=1, LD=2, SD=3, BNEZ=4 } itype; char label[256]; unsigned long labelsz; int rs; int rt; int rd; char btarget[256]; unsigned long btargetsz; int value; } *

variable_types: i int

user_defined_types: struct inst { enum inst_t itype; char label[256]; unsigned long labelsz; int rs; int rt; int rd; char btarget[256]; unsigned long btargetsz; int value; }


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

if ( MEM1 != -1 )
{
if ( MEM1_stall )
result = fprintf(fout, "I%d-stall ", (unsigned int)MEM1_count);
else
result = fprintf(fout, "I%d-MEM1 ", (unsigned int)MEM1_count);
}
return result;
}

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

if ( MEM1 != -1 )
{
if ( MEM1_stall )
result = fprintf(fout, "I%d-stall ", (unsigned int)MEM1_count);
else
result = fprintf(fout, "I%d-MEM1 ", (unsigned int)MEM1_count);
}
return result;
}

original_code:
void mem1()
{
  if( MEM1 == -1 ) return;
  else if( MEM1_stall )
    {
      fprintf( fout, "I%d-stall ", MEM1_count );
      return;
    }
  else
    fprintf( fout, "I%d-MEM1 ", MEM1_count );
}


decompiled_code:
int mem2()
{
int result; // eax
_DWORD *v1; // rax
__int64 v2; // rcx
__int64 v3; // rdx
int v4; // eax
bool v5; // sf
int v6; // edx
int v7; // eax
__int64 v8; // rcx
int v9; // edx
int v10; // ecx
int v11; // edx

result = MEM2;
if ( MEM2 != -1 )
{
if ( MEM2_stall )
{
result = fprintf(fout, "I%d-stall ", (unsigned int)MEM2_count);
}
else
{
v1 = *(&Instructions + MEM2);
if ( *v1 == 2 )
{
v2 = (int)v1[69];
v3 = (int)v1[68];
v4 = v1[138];
v5 = Registers[v3] + v4 < 0;
v6 = Registers[v3] + v4;
v7 = v6 + 7;
if ( !v5 )
v7 = v6;
Registers[v2] = Memory[v7 >> 3];
}
else if ( *v1 == 3 )
{
v8 = (int)v1[68];
v9 = v1[138];
v5 = Registers[v8] + v9 < 0;
v10 = Registers[v8] + v9;
v11 = v10 + 7;
if ( !v5 )
v11 = v10;
Memory[v11 >> 3] = Registers[v1[69]];
}
result = fprintf(fout, "I%d-MEM2 ", (unsigned int)MEM2_count);
}
}
return result;
}

canonical_decompiled_code:
int func12()
{
int result; // eax
_DWORD *v1; // rax
__int64 v2; // rcx
__int64 v3; // rdx
int v4; // eax
bool v5; // sf
int v6; // edx
int v7; // eax
__int64 v8; // rcx
int v9; // edx
int v10; // ecx
int v11; // edx

result = MEM2;
if ( MEM2 != -1 )
{
if ( MEM2_stall )
{
result = fprintf(fout, "I%d-stall ", (unsigned int)MEM2_count);
}
else
{
v1 = *(&Instructions + MEM2);
if ( *v1 == 2 )
{
v2 = (int)v1[69];
v3 = (int)v1[68];
v4 = v1[138];
v5 = Registers[v3] + v4 < 0;
v6 = Registers[v3] + v4;
v7 = v6 + 7;
if ( !v5 )
v7 = v6;
Registers[v2] = Memory[v7 >> 3];
}
else if ( *v1 == 3 )
{
v8 = (int)v1[68];
v9 = v1[138];
v5 = Registers[v8] + v9 < 0;
v10 = Registers[v8] + v9;
v11 = v10 + 7;
if ( !v5 )
v11 = v10;
Memory[v11 >> 3] = Registers[v1[69]];
}
result = fprintf(fout, "I%d-MEM2 ", (unsigned int)MEM2_count);
}
}
return result;
}

original_code:
void mem2()
{
  struct inst * curr;
  if( MEM2 == -1 ) return;
  else if( MEM2_stall )
    {
      fprintf( fout, "I%d-stall ", MEM2_count );
      return;
    }
  else
    {
      curr = Instructions[MEM2];
      if( (*curr).itype == LD )
 Registers[ ( ((*curr).rt) ) ] = Memory[ ( (Registers[ ( ((*curr).rs) ) ] + (*curr).value) / 8 ) ];
      else if( (*curr).itype == SD )
 Memory[ ( (Registers[ ( ((*curr).rs) ) ] + (*curr).value) / 8 ) ] = Registers[ ( ((*curr).rt) ) ];
      fprintf( fout, "I%d-MEM2 ", MEM2_count );
    }
}

canonical_original_code:
void mem2()
{
  struct inst *curr;
  if( MEM2 == -1 ) return;
  else if( MEM2_stall )
    {
      fprintf( fout, "I%d-stall ", MEM2_count );
      return;
    }
  else
    {
      curr = Instructions[MEM2];
      if( (*curr).itype == LD )
 Registers[ ( ((*curr).rt) ) ] = Memory[ ( (Registers[ ( ((*curr).rs) ) ] + (*curr).value) / 8 ) ];
      else if( (*curr).itype == SD )
 Memory[ ( (Registers[ ( ((*curr).rs) ) ] + (*curr).value) / 8 ) ] = Registers[ ( ((*curr).rt) ) ];
      fprintf( fout, "I%d-MEM2 ", MEM2_count );
    }
}

user_defined_types: enum inst_t { DADD=0, SUB=1, LD=2, SD=3, BNEZ=4 }


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

if ( MEM3 != -1 )
{
if ( MEM3_stall )
result = fprintf(fout, "I%d-stall ", (unsigned int)MEM3_count);
else
result = fprintf(fout, "I%d-MEM3 ", (unsigned int)MEM3_count);
}
return result;
}

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

if ( MEM3 != -1 )
{
if ( MEM3_stall )
result = fprintf(fout, "I%d-stall ", (unsigned int)MEM3_count);
else
result = fprintf(fout, "I%d-MEM3 ", (unsigned int)MEM3_count);
}
return result;
}

original_code:
void mem3()
{
  if( MEM3 == -1 ) return;
  else if( MEM3_stall )
    {
      fprintf( fout, "I%d-stall ", MEM3_count );
      return;
    }
  else
    fprintf( fout, "I%d-MEM3 ", MEM3_count );
}


wb

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

if ( WB != -1 )
{
if ( WB_stall )
result = fprintf(fout, "I%d-stall ", (unsigned int)WB_count);
else
result = fprintf(fout, "I%d-WB ", (unsigned int)WB_count);
}
return result;
}

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

if ( WB != -1 )
{
if ( WB_stall )
result = fprintf(fout, "I%d-stall ", (unsigned int)WB_count);
else
result = fprintf(fout, "I%d-WB ", (unsigned int)WB_count);
}
return result;
}

original_code:
void wb()
{
  if( WB == -1 ) return;
  else if( WB_stall )
    {
      fprintf( fout, "I%d-stall ", WB_count );
      return;
    }
  else
    fprintf( fout, "I%d-WB ", WB_count );
}


repo: selavy/simulator

decompiled_code:
int *init_regs()
{
int *result; // rax

puts("init_regs()");
result = &theRegisterFile;
do
*result++ = 0;
while ( result != &error_bit );
return result;
}

canonical_decompiled_code:
int *func7()
{
int *result; // rax

puts("init_regs()");
result = &theRegisterFile;
do
*result++ = 0;
while ( result != &error_bit );
return result;
}

original_code:
void
init_regs()
{
  short i;
  printf("init_regs()\n");
  for( i = 0; i < 32; ++i )
    theRegisterFile[i] = 0;
}

variable_types: i short


decompiled_code:
int __fastcall set_reg(char *s, int a2)
{
int result; // eax
unsigned int v3; // [rsp+Ch] [rbp-1Ch] BYREF

puts("set_reg()");
result = sscanf(s, "R%d", &v3);
if ( result != 1 )
return 1;
if ( v3 <= 0x1F )
{
theRegisterFile[v3] = a2;
result = 0;
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(char *s, int a2)
{
int result; // eax
unsigned int v3; // [rsp+Ch] [rbp-1Ch] BYREF

puts("set_reg()");
result = sscanf(s, "R%d", &v3);
if ( result != 1 )
return 1;
if ( v3 <= 0x1F )
{
theRegisterFile[v3] = a2;
result = 0;
}
return result;
}

original_code:
int
set_reg( char * reg, Register data )
{
  int n;
  printf("set_reg()\n");
  if( sscanf( reg, "R%d", &n ) != 1 )
    return 1;
  if( ( n >= 32 ) || ( n < 0 ) )
    return 1;
  theRegisterFile[n] = data;
  return 0;
}

canonical_original_code:
int set_reg(char *reg, int data)
{
  int n;
  printf("set_reg()\n");
  if( sscanf( reg, "R%d", &n ) != 1 )
    return 1;
  if( ( n >= 32 ) || ( n < 0 ) )
    return 1;
  theRegisterFile[n] = data;
  return 0;
}

variable_types:
reg
char *

variable_types: data int


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

puts("set_regi()");
result = 1LL;
if ( a1 <= 0x1F )
{
theRegisterFile[a1] = a2;
result = 0LL;
}
return result;
}

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

puts("set_regi()");
result = 1LL;
if ( a1 <= 0x1F )
{
theRegisterFile[a1] = a2;
result = 0LL;
}
return result;
}

original_code:
int
set_regi( int reg, Register data )
{
  printf("set_regi()\n");
  if( ( reg >= 32 ) || ( reg < 0 ) )
    return 1;
  theRegisterFile[reg] = data;
  return 0;
}

canonical_original_code:
int set_regi(int reg, int data)
{
  printf("set_regi()\n");
  if( ( reg >= 32 ) || ( reg < 0 ) )
    return 1;
  theRegisterFile[reg] = data;
  return 0;
}


decompiled_code:
__int64 __fastcall get_reg(char *s)
{
unsigned int v1; // ebx
unsigned int v3; // [rsp+Ch] [rbp-Ch] BYREF

puts("get_reg()");
if ( sscanf(s, "R%d", &v3) == 1 )
{
v1 = 1;
if ( v3 > 0x1F )
perror("Attempt to access register that could not be found!");
else
v1 = theRegisterFile[v3];
}
else
{
perror("Attempt to access register that could not be found!");
v1 = 1;
}
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func10(char *s)
{
unsigned int v1; // ebx
unsigned int v3; // [rsp+Ch] [rbp-Ch] BYREF

puts("get_reg()");
if ( sscanf(s, "R%d", &v3) == 1 )
{
v1 = 1;
if ( v3 > 0x1F )
perror("Attempt to access register that could not be found!");
else
v1 = theRegisterFile[v3];
}
else
{
perror("Attempt to access register that could not be found!");
v1 = 1;
}
return v1;
}

original_code:
Register
get_reg( char * reg )
{
  int n;
  printf("get_reg()\n");
  if( sscanf( reg, "R%d", &n ) != 1 )
    {
      perror("Attempt to access register that could not be found!");
      return 1;
    }
  else if( ( n >= 32 ) || ( n < 0 ) )
    {
      perror("Attempt to access register that could not be found!");
      return 1;
    }
  else
    {
      return theRegisterFile[n];
    }
}

canonical_original_code:
int get_reg(char *reg)
{
  int n;
  printf("get_reg()\n");
  if( sscanf( reg, "R%d", &n ) != 1 )
    {
      perror("Attempt to access register that could not be found!");
      return 1;
    }
  else if( ( n >= 32 ) || ( n < 0 ) )
    {
      perror("Attempt to access register that could not be found!");
      return 1;
    }
  else
    {
      return theRegisterFile[n];
    }
}


decompiled_code:
__int64 __fastcall get_regi(unsigned int a1)
{
puts("get_regi()");
if ( a1 <= 0x1F )
return (unsigned int)theRegisterFile[a1];
perror("Attempt to access register that could not be found!");
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(unsigned int a1)
{
puts("get_regi()");
if ( a1 <= 0x1F )
return (unsigned int)theRegisterFile[a1];
perror("Attempt to access register that could not be found!");
return 0LL;
}

original_code:
Register
get_regi( int reg )
{
  printf("get_regi()\n");
  if( ( reg >= 32 ) || ( reg < 0 ) )
    {
      perror("Attempt to access register that could not be found!");
      return 0;
    }
  else
    {
      return theRegisterFile[reg];
    }
}

canonical_original_code:
int get_regi(int reg)
{
  printf("get_regi()\n");
  if( ( reg >= 32 ) || ( reg < 0 ) )
    {
      perror("Attempt to access register that could not be found!");
      return 0;
    }
  else
    {
      return theRegisterFile[reg];
    }
}


decompiled_code:
int print_regs()
{
__int64 i; // rbx
__int64 v1; // rdx

puts("print_regs()");
puts("REGISTERS");
for ( i = 0LL; i != 32; ++i )
{
v1 = (unsigned int)theRegisterFile[i];
if ( (_DWORD)v1 )
printf("R%d %d\n", (unsigned int)i, v1);
}
return putchar(10);
}

canonical_decompiled_code:
int func12()
{
__int64 i; // rbx
__int64 v1; // rdx

puts("print_regs()");
puts("REGISTERS");
for ( i = 0LL; i != 32; ++i )
{
v1 = (unsigned int)theRegisterFile[i];
if ( (_DWORD)v1 )
printf("R%d %d\n", (unsigned int)i, v1);
}
return putchar(10);
}

original_code:
void
print_regs()
{
  short i;
  Register val;
  printf("print_regs()\n");
  printf("REGISTERS\n");
  for( i = 0; i < 32; ++i )
    {
      val = theRegisterFile[i];
      if( val != 0 )
 printf( "R%d %d\n", i, val );
    }
  if( i > 0 )
    printf("\n");
}

canonical_original_code:
void print_regs()
{
  short i;
  int val;
  printf("print_regs()\n");
  printf("REGISTERS\n");
  for( i = 0; i < 32; ++i )
    {
      val = theRegisterFile[i];
      if( val != 0 )
 printf( "R%d %d\n", i, val );
    }
  if( i > 0 )
    printf("\n");
}

variable_types: val int


decompiled_code:
__int64 init_memfile()
{
int *v0; // rax

v0 = &theMemoryFile;
do
*v0++ = 0;
while ( v0 != (int *)&unk_404374 );
puts("init_memfile()");
return 0LL;
}

canonical_decompiled_code:
__int64 func13()
{
int *v0; // rax

v0 = &theMemoryFile;
do
*v0++ = 0;
while ( v0 != (int *)&unk_404374 );
puts("init_memfile()");
return 0LL;
}

original_code:
int
init_memfile()
{
  short i;
  for( i = 0; i < 125; ++i )
    theMemoryFile[i] = 0;
  printf("init_memfile()\n");
  return 0;
}


store

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

puts("store()");
if ( (unsigned int)a1 > 0x3E0 )
{
perror("Trying to write to a location out-of-range");
error_bit = 1;
result = 1LL;
}
else
{
theMemoryFile[a1 >> 3] = a2;
result = 0LL;
}
return result;
}

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

puts("store()");
if ( (unsigned int)a1 > 0x3E0 )
{
perror("Trying to write to a location out-of-range");
error_bit = 1;
result = 1LL;
}
else
{
theMemoryFile[a1 >> 3] = a2;
result = 0LL;
}
return result;
}

original_code:
int
store( int loc, Memory data )
{
  printf("store()\n");
  if( ( loc > ((125 -1) * (4 * 2)) ) || ( loc < 0 ) )
    {
      perror("Trying to write to a location out-of-range");
      error_bit = 1;
      return 1;
    }
  theMemoryFile[loc / (4 * 2)] = data;
  return 0;
}

canonical_original_code:
int store(int loc, int data)
{
  printf("store()\n");
  if( ( loc > ((125 -1) * (4 * 2)) ) || ( loc < 0 ) )
    {
      perror("Trying to write to a location out-of-range");
      error_bit = 1;
      return 1;
    }
  theMemoryFile[loc / (4 * 2)] = data;
  return 0;
}

variable_types: loc int


load

decompiled_code:
__int64 __fastcall load(int a1)
{
puts("load()");
if ( (unsigned int)a1 <= 0x3E0 )
return (unsigned int)theMemoryFile[a1 >> 3];
perror("Trying to read from location out-of-range");
error_bit = 1;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func15(int a1)
{
puts("load()");
if ( (unsigned int)a1 <= 0x3E0 )
return (unsigned int)theMemoryFile[a1 >> 3];
perror("Trying to read from location out-of-range");
error_bit = 1;
return 0LL;
}

original_code:
Memory
load( int loc )
{
  printf("load()\n");
  if( ( loc > ((125 -1) * (4 * 2)) ) || ( loc < 0 ) )
    {
      perror("Trying to read from location out-of-range");
      error_bit = 1;
      return 0;
    }
  else
    {
      return theMemoryFile[loc / (4 * 2)];
    }
}

canonical_original_code:
int load(int loc)
{
  printf("load()\n");
  if( ( loc > ((125 -1) * (4 * 2)) ) || ( loc < 0 ) )
    {
      perror("Trying to read from location out-of-range");
      error_bit = 1;
      return 0;
    }
  else
    {
      return theMemoryFile[loc / (4 * 2)];
    }
}


decompiled_code:
__int64 print_mem()
{
__int64 i; // rbx
__int64 v1; // rdx

puts("print_mem()");
puts("MEMORY");
for ( i = 0LL; i != 125; ++i )
{
v1 = (unsigned int)theMemoryFile[i];
if ( (_DWORD)v1 )
printf("%d %d\n", (unsigned int)(8 * i), v1);
}
putchar(10);
return 0LL;
}

canonical_decompiled_code:
__int64 func16()
{
__int64 i; // rbx
__int64 v1; // rdx

puts("print_mem()");
puts("MEMORY");
for ( i = 0LL; i != 125; ++i )
{
v1 = (unsigned int)theMemoryFile[i];
if ( (_DWORD)v1 )
printf("%d %d\n", (unsigned int)(8 * i), v1);
}
putchar(10);
return 0LL;
}

original_code:
int
print_mem()
{
  short i;
  printf("print_mem()\n");
  printf("MEMORY\n");
  for( i = 0; i < 125; ++i )
    {
      if( theMemoryFile[i] != 0 )
 printf("%d %d\n", i * (4 * 2), theMemoryFile[i] );
    }
  if( i > 0 )
    printf("\n");
  return 0;
}


decompiled_code:
__int64 get_mem_error_bit()
{
return (unsigned int)error_bit;
}

canonical_decompiled_code:
__int64 func17()
{
return (unsigned int)error_bit;
}

original_code:
int
get_mem_error_bit()
{
  return error_bit;
}


decompiled_code:
void clear_mem_error_bit()
{
error_bit = 0;
}

canonical_decompiled_code:
void func18()
{
error_bit = 0;
}


decompiled_code:
int __fastcall read_register_section(char *s)
{
int v2; // [rsp+Ch] [rbp-20Ch] BYREF
char sa[520]; // [rsp+10h] [rbp-208h] BYREF

printf("read_register_section(): %s", s);
if ( sscanf(s, "%s %d\n", sa, &v2) == 2 )
return set_reg(sa, v2);
perror("Unable to read line");
return 1;
}

canonical_decompiled_code:
int __fastcall func19(char *s)
{
int v2; // [rsp+Ch] [rbp-20Ch] BYREF
char sa[520]; // [rsp+10h] [rbp-208h] BYREF

printf("read_register_section(): %s", s);
if ( sscanf(s, "%s %d\n", sa, &v2) == 2 )
return func8(sa, v2);
perror("Unable to read line");
return 1;
}

original_code:
int
read_register_section( char * line )
{
  char reg[512];
  int value;
  printf("read_register_section(): %s", line );
  if( sscanf( line, "%s %d\n", reg, &value ) != 2 )
    {
      perror("Unable to read line");
      return 1;
    }
  else
    {
      return set_reg( reg, value );
    }
}

canonical_original_code:
int read_register_section(char *line)
{
  char reg[512];
  int value;
  printf("read_register_section(): %s", line );
  if( sscanf( line, "%s %d\n", reg, &value ) != 2 )
    {
      perror("Unable to read line");
      return 1;
    }
  else
    {
      return set_reg( reg, value );
    }
}

variable_types:
line
char *

variable_types:
reg
char [512]

variable_types: value int


decompiled_code:
__int64 __fastcall read_memory_section(char *s)
{
int v2; // [rsp+8h] [rbp-10h] BYREF
int v3; // [rsp+Ch] [rbp-Ch] BYREF

printf("read_memory_section(): %s", s);
if ( sscanf(s, "%d %d\n", &v3, &v2) == 2 )
return store(v3, v2);
perror("Unable to read line");
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func20(char *s)
{
int v2; // [rsp+8h] [rbp-10h] BYREF
int v3; // [rsp+Ch] [rbp-Ch] BYREF

printf("read_memory_section(): %s", s);
if ( sscanf(s, "%d %d\n", &v3, &v2) == 2 )
return func14(v3, v2);
perror("Unable to read line");
return 1LL;
}

original_code:
int
read_memory_section( char * line )
{
  int mem;
  int value;
  printf("read_memory_section(): %s", line );
  if( sscanf( line, "%d %d\n", &mem, &value ) != 2 )
    {
      perror( "Unable to read line" );
      return 1;
    }
  else
    {
      return store( mem, value );
    }
}

canonical_original_code:
int read_memory_section(char *line)
{
  int mem;
  int value;
  printf("read_memory_section(): %s", line );
  if( sscanf( line, "%d %d\n", &mem, &value ) != 2 )
    {
      perror( "Unable to read line" );
      return 1;
    }
  else
    {
      return store( mem, value );
    }
}

variable_types: mem int


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

if ( file_read )
result = fclose(pFile);
file_read = 0;
return result;
}

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

if ( file_read )
result = fclose(pFile);
file_read = 0;
return result;
}

original_code:
void
close_input()
{
  if( file_read )
    fclose( pFile );
  file_read = 0;
}


decompiled_code:
void __fastcall push_inst(char *src)
{
_QWORD *v2; // r13
_QWORD *v3; // rax
_QWORD *v4; // rbx
size_t v5; // rbp
char *v6; // rdi
_QWORD *v7; // rax

v2 = INSTS;
if ( !INSTS )
{
v7 = malloc(0x28uLL);
v4 = v7;
INSTS = v7;
if ( !v7 )
return;
curr = (__int64)v7;
goto LABEL_6;
}
if ( !curr )
curr = (__int64)INSTS;
v2 = (_QWORD *)curr;
v3 = malloc(0x28uLL);
v4 = v3;
v2[3] = v3;
if ( v3 )
{
curr = (__int64)v3;
LABEL_6:
v5 = strlen(src) + 1;
v6 = (char *)malloc(v5);
v4[1] = v6;
if ( v6 )
{
strncpy(v6, src, v5);
v4[2] = v5;
v4[4] = v2;
v4[3] = 0LL;
}
else
{
free(INSTS);
INSTS = 0LL;
}
}
}

canonical_decompiled_code:
void __fastcall func22(char *src)
{
_QWORD *v2; // r13
_QWORD *v3; // rax
_QWORD *v4; // rbx
size_t v5; // rbp
char *v6; // rdi
_QWORD *v7; // rax

v2 = INSTS;
if ( !INSTS )
{
v7 = malloc(0x28uLL);
v4 = v7;
INSTS = v7;
if ( !v7 )
return;
curr = (__int64)v7;
goto LABEL_6;
}
if ( !curr )
curr = (__int64)INSTS;
v2 = (_QWORD *)curr;
v3 = malloc(0x28uLL);
v4 = v3;
v2[3] = v3;
if ( v3 )
{
curr = (__int64)v3;
LABEL_6:
v5 = strlen(src) + 1;
v6 = (char *)malloc(v5);
v4[1] = v6;
if ( v6 )
{
strncpy(v6, src, v5);
v4[2] = v5;
v4[4] = v2;
v4[3] = 0LL;
}
else
{
free(INSTS);
INSTS = 0LL;
}
}
}

original_code:
void
push_inst( char * inst )
{
  int _id;
  size_t inst_sz;
  struct instmem * node;
  struct instmem * prev;
  if( INSTS == ((void *)0) )
    {
      INSTS = malloc( sizeof *INSTS );
      if( INSTS == ((void *)0) )
 return;
      node = INSTS;
      curr = INSTS;
      prev = ((void *)0);
    }
  else
    {
      if( curr == ((void *)0) )
 curr = INSTS;
      (*curr).next = malloc( sizeof( *(*curr).next ) );
      if( (*curr).next == ((void *)0) )
 return;
      node = (*curr).next;
      prev = curr;
      curr = node;
    }
      inst_sz = strlen( inst ) + 1;
      (*node).inst = malloc( sizeof( char ) * inst_sz );
      if( (*node).inst == ((void *)0) )
 {
   free( INSTS );
   INSTS = ((void *)0);
   return;
 }
      strncpy( (*node).inst, inst, inst_sz );
      (*node).inst_sz = inst_sz;
      (*node).prev = prev;
      (*node).next = ((void *)0);
}

canonical_original_code:
void push_inst(char *inst)
{
  int _id;
  unsigned long inst_sz;
  struct instmem *node;
  struct instmem *prev;
  if( INSTS == ((void *)0) )
    {
      INSTS = malloc( sizeof *INSTS );
      if( INSTS == ((void *)0) )
 return;
      node = INSTS;
      curr = INSTS;
      prev = ((void *)0);
    }
  else
    {
      if( curr == ((void *)0) )
 curr = INSTS;
      (*curr).next = malloc( sizeof( *(*curr).next ) );
      if( (*curr).next == ((void *)0) )
 return;
      node = (*curr).next;
      prev = curr;
      curr = node;
    }
      inst_sz = strlen( inst ) + 1;
      (*node).inst = malloc( sizeof( char ) * inst_sz );
      if( (*node).inst == ((void *)0) )
 {
   free( INSTS );
   INSTS = ((void *)0);
   return;
 }
      strncpy( (*node).inst, inst, inst_sz );
      (*node).inst_sz = inst_sz;
      (*node).prev = prev;
      (*node).next = ((void *)0);
}

variable_types:
inst
char *

variable_types: struct instmem { unsigned int _id; char *inst; unsigned long inst_sz; struct instmem *next; struct instmem *prev; } *


decompiled_code:
__int64 __fastcall create_ld(char *a1)
{
push_inst(a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func23(char *a1)
{
func22(a1);
return 0LL;
}

original_code:
struct instmem *
create_ld( char * cmd )
{
  push_inst( cmd );
  return ((void *)0);
}

canonical_original_code:
struct instmem *create_ld(char *cmd)
{
  push_inst( cmd );
  return ((void *)0);
}

variable_types:
cmd
char *

return_type: struct instmem { unsigned int _id; char *inst; unsigned long inst_sz; struct instmem *next; struct instmem *prev; } *


decompiled_code:
__int64 __fastcall create_sd(char *a1)
{
push_inst(a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func24(char *a1)
{
func22(a1);
return 0LL;
}

original_code:
struct instmem *
create_sd( char * cmd )
{
  push_inst( cmd );
  return ((void *)0);
}

canonical_original_code:
struct instmem *create_sd(char *cmd)
{
  push_inst( cmd );
  return ((void *)0);
}

user_defined_types: struct instmem { unsigned int _id; char *inst; unsigned long inst_sz; struct instmem *next; struct instmem *prev; }


decompiled_code:
__int64 __fastcall create_dadd(char *a1)
{
push_inst(a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func25(char *a1)
{
func22(a1);
return 0LL;
}

original_code:
struct instmem *
create_dadd( char * cmd )
{
  push_inst( cmd );
  return ((void *)0);
}

canonical_original_code:
struct instmem *create_dadd(char *cmd)
{
  push_inst( cmd );
  return ((void *)0);
}


decompiled_code:
__int64 __fastcall create_sub(char *a1)
{
push_inst(a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func26(char *a1)
{
func22(a1);
return 0LL;
}

original_code:
struct instmem *
create_sub( char * cmd )
{
  push_inst( cmd );
  return ((void *)0);
}

canonical_original_code:
struct instmem *create_sub(char *cmd)
{
  push_inst( cmd );
  return ((void *)0);
}


decompiled_code:
__int64 __fastcall create_bnez(char *a1)
{
push_inst(a1);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func27(char *a1)
{
func22(a1);
return 0LL;
}

original_code:
struct instmem *
create_bnez( char * cmd )
{
  push_inst( cmd );
  return ((void *)0);
}

canonical_original_code:
struct instmem *create_bnez(char *cmd)
{
  push_inst( cmd );
  return ((void *)0);
}


decompiled_code:
__int64 __fastcall read_code_section(char *s)
{
__int64 result; // rax
char v2[520]; // [rsp+0h] [rbp-208h] BYREF

printf("read_code_section(): %s", s);
if ( sscanf(s, "%s\n", v2) == 1 )
{
if ( !strcmp(v2, "LD") )
{
result = create_ld(v2);
}
else if ( !strcmp(v2, "DADD") )
{
result = create_dadd(v2);
}
else if ( !strcmp(v2, "SUB") )
{
result = create_sub(v2);
}
else if ( !strcmp(v2, "SD") )
{
result = create_sd(v2);
}
else if ( !strcmp(v2, "BNEZ") )
{
result = create_bnez(v2);
}
else
{
printf("jump label: %s\n", v2);
result = 0LL;
}
}
else
{
perror("malformed code");
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func28(char *s)
{
__int64 result; // rax
char v2[520]; // [rsp+0h] [rbp-208h] BYREF

printf("read_code_section(): %s", s);
if ( sscanf(s, "%s\n", v2) == 1 )
{
if ( !strcmp(v2, "LD") )
{
result = func23(v2);
}
else if ( !strcmp(v2, "DADD") )
{
result = func25(v2);
}
else if ( !strcmp(v2, "SUB") )
{
result = func26(v2);
}
else if ( !strcmp(v2, "SD") )
{
result = func24(v2);
}
else if ( !strcmp(v2, "BNEZ") )
{
result = func27(v2);
}
else
{
printf("jump label: %s\n", v2);
result = 0LL;
}
}
else
{
perror("malformed code");
result = 0LL;
}
return result;
}

original_code:
struct instmem *
read_code_section( char * line )
{
  char cmd[512];
  char rs[512];
  char rt[512];
  char rd[512];
  int offset;
  printf("read_code_section(): %s", line );
  if( 1 != sscanf( line, "%s\n", cmd) )
    {
      perror("malformed code");
      return ((void *)0);
    }
  if( strcmp( cmd, "LD" ) == 0 )
    {
      return create_ld( cmd );
    }
  else if( strcmp( cmd, "DADD" ) == 0 )
    {
      return create_dadd( cmd );
    }
  else if( strcmp( cmd, "SUB" ) == 0 )
    {
      return create_sub( cmd );
    }
  else if( strcmp( cmd, "SD" ) == 0 )
    {
      return create_sd( cmd );
    }
  else if( strcmp( cmd, "BNEZ" ) == 0 )
    {
      return create_bnez( cmd );
    }
  else
    {
      printf("jump label: %s\n", cmd);
      return ((void *)0);
    }
}

canonical_original_code:
struct instmem *read_code_section(char *line)
{
  char cmd[512];
  char rs[512];
  char rt[512];
  char rd[512];
  int offset;
  printf("read_code_section(): %s", line );
  if( 1 != sscanf( line, "%s\n", cmd) )
    {
      perror("malformed code");
      return ((void *)0);
    }
  if( strcmp( cmd, "LD" ) == 0 )
    {
      return create_ld( cmd );
    }
  else if( strcmp( cmd, "DADD" ) == 0 )
    {
      return create_dadd( cmd );
    }
  else if( strcmp( cmd, "SUB" ) == 0 )
    {
      return create_sub( cmd );
    }
  else if( strcmp( cmd, "SD" ) == 0 )
    {
      return create_sd( cmd );
    }
  else if( strcmp( cmd, "BNEZ" ) == 0 )
    {
      return create_bnez( cmd );
    }
  else
    {
      printf("jump label: %s\n", cmd);
      return ((void *)0);
    }
}

variable_types:
cmd
char [512]

variable_types:
rs
char [512]

variable_types:
rt
char [512]

variable_types:
rd
char [512]

variable_types: offset int


read

decompiled_code:
char read()
{
bool v0; // zf
char result; // al
char v2[520]; // [rsp+0h] [rbp-208h] BYREF

fgets(v2, 512, pFile);
v0 = strcmp(v2, "REGISTER\n") == 0;
result = !v0;
if ( v0 )
{
state = 0;
}
else
{
v0 = strcmp(v2, "MEMORY\n") == 0;
result = !v0;
if ( v0 )
{
state = 1;
}
else
{
v0 = strcmp(v2, "CODE\n") == 0;
result = !v0;
if ( v0 )
{
state = 2;
}
else
{
v0 = memcmp(v2, "\n", 2uLL) == 0;
result = !v0;
if ( !v0 )
{
if ( state )
{
if ( state == 1 )
{
result = read_memory_section(v2);
}
else if ( state == 2 )
{
result = read_code_section(v2);
}
else
{
result = printf("Malformed code!: %s\n", v2);
}
}
else
{
result = read_register_section(v2);
}
}
}
}
}
return result;
}

canonical_decompiled_code:
char func29()
{
bool v0; // zf
char result; // al
char v2[520]; // [rsp+0h] [rbp-208h] BYREF

fgets(v2, 512, pFile);
v0 = strcmp(v2, "REGISTER\n") == 0;
result = !v0;
if ( v0 )
{
state = 0;
}
else
{
v0 = strcmp(v2, "MEMORY\n") == 0;
result = !v0;
if ( v0 )
{
state = 1;
}
else
{
v0 = strcmp(v2, "CODE\n") == 0;
result = !v0;
if ( v0 )
{
state = 2;
}
else
{
v0 = memcmp(v2, "\n", 2uLL) == 0;
result = !v0;
if ( !v0 )
{
if ( state )
{
if ( state == 1 )
{
result = func20(v2);
}
else if ( state == 2 )
{
result = func28(v2);
}
else
{
result = printf("Malformed code!: %s\n", v2);
}
}
else
{
result = func19(v2);
}
}
}
}
}
return result;
}

original_code:
void
read()
{
  char line[512];
  fgets( line, 512, pFile );
  if( strcmp( line, "REGISTER\n" ) == 0 )
    {
      state = REGISTER;
      return;
    }
  else if( strcmp( line, "MEMORY\n" ) == 0 )
    {
      state = MEMORY;
      return;
    }
  else if( strcmp( line, "CODE\n" ) == 0 )
    {
      state = CODE;
      return;
    }
  else if( strcmp( line, "\n" ) == 0 )
    return;
  else
    {
      if( state == REGISTER )
 {
   read_register_section( line );
 }
      else if( state == MEMORY )
 {
   read_memory_section( line );
 }
      else if( state == CODE )
 {
   read_code_section( line );
 }
      else
 {
   printf( "Malformed code!: %s\n", line );
   return;
 }
    }
}

variable_types:
line
char [512]


decompiled_code:
__int64 __fastcall read_input(char *filename)
{
__int64 result; // rax

puts("read_input()");
result = (__int64)fopen(filename, "r");
pFile = (FILE *)result;
if ( result )
{
while ( !feof(pFile) )
read();
print_regs();
print_mem();
file_read = 1;
result = 1LL;
}
else
{
file_read = 0;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func30(char *filename)
{
__int64 result; // rax

puts("read_input()");
result = (__int64)fopen(filename, "r");
pFile = (FILE *)result;
if ( result )
{
while ( !feof(pFile) )
func29();
func12();
func16();
file_read = 1;
result = 1LL;
}
else
{
file_read = 0;
}
return result;
}

original_code:
int
read_input( char * file )
{
  printf("read_input()\n");
  pFile = fopen( file, "r" );
  if( pFile == ((void *)0) )
    return (file_read = 0);
  while(! feof( pFile ) )
    {
      read();
    }
  print_regs();
  print_mem();
  return (file_read = 1);
}

canonical_original_code:
int read_input(char *file)
{
  printf("read_input()\n");
  pFile = fopen( file, "r" );
  if( pFile == ((void *)0) )
    return (file_read = 0);
  while(! feof( pFile ) )
    {
      read();
    }
  print_regs();
  print_mem();
  return (file_read = 1);
}

variable_types:
file
char *


decompiled_code:
int print_code_listing()
{
void *i; // rbx
int result; // eax

for ( i = INSTS; i; i = (void *)*((_QWORD *)i + 3) )
result = puts(*((const char **)i + 1));
return result;
}

canonical_decompiled_code:
int func31()
{
void *i; // rbx
int result; // eax

for ( i = INSTS; i; i = (void *)*((_QWORD *)i + 3) )
result = puts(*((const char **)i + 1));
return result;
}

original_code:
void
print_code_listing()
{
  struct instmem * n;
  n = INSTS;
  while( n != ((void *)0) )
    {
      printf("%s\n", (*n).inst );
      n = (*n).next;
    }
}

canonical_original_code:
void print_code_listing()
{
  struct instmem *n;
  n = INSTS;
  while( n != ((void *)0) )
    {
      printf("%s\n", (*n).inst );
      n = (*n).next;
    }
}


decompiled_code:
int IF1()
{
return puts("IF1()");
}

canonical_decompiled_code:
int func32()
{
return puts("IF1()");
}

original_code:
void
IF1() {
  printf("IF1()\n");
}

canonical_original_code:
void IF1() {
  printf("IF1()\n");
}


decompiled_code:
int IF2()
{
return puts("IF2()");
}

canonical_decompiled_code:
int func33()
{
return puts("IF2()");
}

original_code:
void IF2() {
  printf("IF2()\n");
}

canonical_original_code:
void IF2() {
  printf("IF2()\n");
}


ID

decompiled_code:
int ID()
{
return puts("ID()");
}

canonical_decompiled_code:
int func34()
{
return puts("ID()");
}

original_code:
void ID() {
  printf("ID()\n");
}

canonical_original_code:
void ID() {
  printf("ID()\n");
}


EX

decompiled_code:
int EX()
{
return puts("EX()");
}

canonical_decompiled_code:
int func35()
{
return puts("EX()");
}

original_code:
void EX() {
  printf("EX()\n");
}

canonical_original_code:
void EX() {
  printf("EX()\n");
}


decompiled_code:
int MEM1()
{
return puts("MEM1()");
}

canonical_decompiled_code:
int func36()
{
return puts("MEM1()");
}

original_code:
void MEM1() {
  printf("MEM1()\n");
}

canonical_original_code:
void MEM1() {
  printf("MEM1()\n");
}


decompiled_code:
int MEM2()
{
return puts("MEM2()");
}

canonical_decompiled_code:
int func37()
{
return puts("MEM2()");
}

original_code:
void MEM2() {
  printf("MEM2()\n");
}

canonical_original_code:
void MEM2() {
  printf("MEM2()\n");
}


decompiled_code:
int MEM3()
{
return puts("MEM3()");
}

canonical_decompiled_code:
int func38()
{
return puts("MEM3()");
}

original_code:
void MEM3() {
  printf("MEM3()\n");
}

canonical_original_code:
void MEM3() {
  printf("MEM3()\n");
}


WB

decompiled_code:
int WB()
{
return puts("WB()");
}

canonical_decompiled_code:
int func39()
{
return puts("WB()");
}

original_code:
void WB() {
  printf("WB()\n");
}

canonical_original_code:
void WB() {
  printf("WB()\n");
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc <= 1 )
{
infile = dftinfile;
outfile = (__int64)dftoutfile;
}
else
{
infile = (char *)argv[1];
if ( argc == 3 )
outfile = (__int64)dftoutfile;
else
outfile = (__int64)argv[2];
}
init_regs();
init_memfile();
read_input(infile);
print_code_listing();
WB();
MEM3();
MEM2();
MEM1();
EX();
ID();
IF2();
IF1();
close_input();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
if ( argc <= 1 )
{
infile = dftinfile;
outfile = (__int64)dftoutfile;
}
else
{
infile = (char *)argv[1];
if ( argc == 3 )
outfile = (__int64)dftoutfile;
else
outfile = (__int64)argv[2];
}
func7();
func13();
func30(infile);
func31();
func39();
func38();
func37();
func36();
func35();
func34();
func33();
func32();
func21();
return 0;
}

original_code:
int main( int argc, char ** argv )
{
  if( argc < 2 )
    {
      infile = dftinfile;
      outfile = dftoutfile;
    }
  else if( argc == 3 )
    {
      infile = argv[1];
      outfile = dftoutfile;
    }
  else
    {
      infile = argv[1];
      outfile = argv[2];
    }
  init_regs();
  init_memfile();
  read_input( infile );
  print_code_listing();
  do
    {
      WB();
      MEM3();
      MEM2();
      MEM1();
      EX();
      ID();
      IF2();
      IF1();
    } while( 0 );
  close_input();
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  if( argc < 2 )
    {
      infile = dftinfile;
      outfile = dftoutfile;
    }
  else if( argc == 3 )
    {
      infile = argv[1];
      outfile = dftoutfile;
    }
  else
    {
      infile = argv[1];
      outfile = argv[2];
    }
  init_regs();
  init_memfile();
  read_input( infile );
  print_code_listing();
  do
    {
      WB();
      MEM3();
      MEM2();
      MEM1();
      EX();
      ID();
      IF2();
      IF1();
    } while( 0 );
  close_input();
  return 0;
}

variable_types: argc int

variable_types:
argv
char **


repo: selavy/tictactoe

decompiled_code:
int __fastcall print_board(unsigned int a1)
{
unsigned int v1; // ebx
int v2; // er12
unsigned int v3; // ebp
int result; // eax

v1 = 0;
v2 = (unsigned __int16)a1;
while ( 1 )
{
if ( _bittest(&v2, v1) )
{
printf(" X ");
}
else
{
v3 = HIWORD(a1);
if ( _bittest((const int *)&v3, v1) )
printf(" O ");
else
printf(" ");
}
if ( v1 == 2 || v1 == 5 )
{
result = puts("\n-----------");
goto LABEL_8;
}
if ( v1 == 8 )
return putchar(10);
result = putchar(124);
LABEL_8:
if ( ++v1 == 9 )
return result;
}
}

canonical_decompiled_code:
int __fastcall func7(unsigned int a1)
{
unsigned int v1; // ebx
int v2; // er12
unsigned int v3; // ebp
int result; // eax

v1 = 0;
v2 = (unsigned __int16)a1;
while ( 1 )
{
if ( _bittest(&v2, v1) )
{
printf(" X ");
}
else
{
v3 = HIWORD(a1);
if ( _bittest((const int *)&v3, v1) )
printf(" O ");
else
printf(" ");
}
if ( v1 == 2 || v1 == 5 )
{
result = puts("\n-----------");
goto LABEL_8;
}
if ( v1 == 8 )
return putchar(10);
result = putchar(124);
LABEL_8:
if ( ++v1 == 9 )
return result;
}
}

original_code:
void print_board(struct board_t board)
{
    int i = 0;
    for (; i < 9; ++i) {
        if (((board.player1) & (1 << (i)))) {
            printf(" X ");
        }
        else if (((board.player2) & (1 << (i)))) {
            printf(" O ");
        }
        else {
            printf("   ");
        }
        if (i == 2 || i == 5) {
            printf("\n-----------\n");
        }
        else if (i == 8) {
            printf("\n");
        }
        else {
            printf("|");
        }
    }
}

variable_types: struct board_t { unsigned short player1; unsigned short player2; }

variable_types: i int


decompiled_code:
__int64 __fastcall ask_for_move(int a1)
{
char v1; // r12
int v2; // ebp
__int64 result; // rax
size_t v4; // [rsp+0h] [rbp-28h] BYREF
char *lineptr; // [rsp+8h] [rbp-20h] BYREF

lineptr = 0LL;
v4 = 0LL;
printf("Enter a move: ");
if ( (int)getline(&lineptr, &v4, stdin) <= 0 )
exit(0);
v1 = *lineptr;
v2 = *lineptr;
free(lineptr);
if ( ((*__ctype_b_loc())[v1] & 0x800) == 0 )
return 0xFFFFFFFFLL;
result = (unsigned int)(v2 - 49);
if ( v2 - 49 < 0 )
return 0xFFFFFFFFLL;
if ( (((int)(unsigned __int16)(HIWORD(a1) | a1) >> (v2 - 49)) & 1) != 0 )
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1)
{
char v1; // r12
int v2; // ebp
__int64 result; // rax
size_t v4; // [rsp+0h] [rbp-28h] BYREF
char *lineptr; // [rsp+8h] [rbp-20h] BYREF

lineptr = 0LL;
v4 = 0LL;
printf("Enter a move: ");
if ( (int)getline(&lineptr, &v4, stdin) <= 0 )
exit(0);
v1 = *lineptr;
v2 = *lineptr;
free(lineptr);
if ( ((*__ctype_b_loc())[v1] & 0x800) == 0 )
return 0xFFFFFFFFLL;
result = (unsigned int)(v2 - 49);
if ( v2 - 49 < 0 )
return 0xFFFFFFFFLL;
if ( (((int)(unsigned __int16)(HIWORD(a1) | a1) >> (v2 - 49)) & 1) != 0 )
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int ask_for_move(struct board_t board) {
    char* line = 0;
    size_t n = 0;
    int ret;
    printf("Enter a move: ");
    ret = getline(&line, &n, stdin);
    if (ret < 1) {
        exit(0);
        return -1;
    }
    else {
        ret = line[0];
        free(line);
        if (((*__ctype_b_loc ())[(int) ((ret))] & (unsigned short int) _ISdigit)) {
            ret = ret - '0' - 1;
            if (ret < 0) {
                return -1;
            }
            else {
                return ((board.player1 | board.player2) & (1 << (ret))) ? -1 : ret;
            }
        }
        return -1;
    }
}

canonical_original_code:
int ask_for_move(struct board_t board) {
    char *line = 0;
    unsigned long n = 0;
    int ret;
    printf("Enter a move: ");
    ret = getline(&line, &n, stdin);
    if (ret < 1) {
        exit(0);
        return -1;
    }
    else {
        ret = line[0];
        free(line);
        if (((*__ctype_b_loc ())[(int) ((ret))] & (unsigned short) _ISdigit)) {
            ret = ret - '0' - 1;
            if (ret < 0) {
                return -1;
            }
            else {
                return ((board.player1 | board.player2) & (1 << (ret))) ? -1 : ret;
            }
        }
        return -1;
    }
}

variable_types:
line
char *

variable_types:
n
unsigned long

variable_types: ret int

user_defined_types: struct board_t { unsigned short player1; unsigned short player2; }


decompiled_code:
int *__fastcall generate_moves(int a1, _DWORD *a2)
{
int v2; // edi
unsigned int i; // eax
int v4; // edx

*a2 = 0;
v2 = (unsigned __int16)(a1 | HIWORD(a1));
for ( i = 0; i != 9; ++i )
{
if ( !_bittest(&v2, i) )
{
v4 = (*a2)++;
buffer_2[v4] = i;
}
}
return buffer_2;
}

canonical_decompiled_code:
int *__fastcall func9(int a1, _DWORD *a2)
{
int v2; // edi
unsigned int i; // eax
int v4; // edx

*a2 = 0;
v2 = (unsigned __int16)(a1 | HIWORD(a1));
for ( i = 0; i != 9; ++i )
{
if ( !_bittest(&v2, i) )
{
v4 = (*a2)++;
buffer_2[v4] = i;
}
}
return buffer_2;
}

original_code:
int *generate_moves(struct board_t board, int *generated) {
    static int buffer[9];
    int i;
    *generated = 0;
    for (i = 0; i < 9; ++i) {
        if (!((board.player1 | board.player2) & (1 << (i)))) {
            buffer[(*generated)++] = i;
        }
    }
    return &(buffer[0]);
}

variable_types:
generated
int *

variable_types:
buffer
int [9]

return_type: int *


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

v4 = *a2;
*(_DWORD *)a1 = *a2;
if ( a3 )
{
result = (1 << a4) | v4;
*(_WORD *)a1 = result;
}
else
{
result = (1 << a4) | HIWORD(v4);
*(_WORD *)(a1 + 2) = result;
}
return result;
}

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

v4 = *a2;
*(_DWORD *)a1 = *a2;
if ( a3 )
{
result = (1 << a4) | v4;
*(_WORD *)a1 = result;
}
else
{
result = (1 << a4) | HIWORD(v4);
*(_WORD *)(a1 + 2) = result;
}
return result;
}

original_code:
void create_child(struct board_t *cpy, struct board_t *board, boolean is_player1, int move) {
    memcpy(cpy, board, sizeof(*board));
    if (is_player1) {
        ((cpy->player1) |= (1 << (move)));
    } else {
        ((cpy->player2) |= (1 << (move)));
    }
}

canonical_original_code:
void create_child(struct board_t *cpy, struct board_t *board, char is_player1, int move) {
    memcpy(cpy, board, sizeof(*board));
    if (is_player1) {
        ((cpy->player1) |= (1 << (move)));
    } else {
        ((cpy->player2) |= (1 << (move)));
    }
}

variable_types: struct board_t { unsigned short player1; unsigned short player2; } *

variable_types:
board
struct board_t *

variable_types: move int


alphabeta

original_code:
int alphabeta(struct board_t board, boolean is_player1, int depth, int alpha, int beta, char maximizing, boolean player1_winner)
{
    int v = 0;
    int i;
    int *tmp;
    int moves[9];
    int poss;
    int score;
    struct board_t cpy;
    if (( (((board.player1) & (0x54)) == (0x54)) || (((board.player1) & (0x111)) == (0x111)) || (((board.player1) & (0x7)) == (0x7)) || (((board.player1) & (0x38)) == (0x38)) || (((board.player1) & (0x1c0)) == (0x1c0)) || (((board.player1) & (0x124)) == (0x124)) || (((board.player1) & (0x92)) == (0x92)) || (((board.player1) & (0x49)) == (0x49)) )) {
        return player1_winner ? 1 : -1;
    } else if (( (((board.player2) & (0x54)) == (0x54)) || (((board.player2) & (0x111)) == (0x111)) || (((board.player2) & (0x7)) == (0x7)) || (((board.player2) & (0x38)) == (0x38)) || (((board.player2) & (0x1c0)) == (0x1c0)) || (((board.player2) & (0x124)) == (0x124)) || (((board.player2) & (0x92)) == (0x92)) || (((board.player2) & (0x49)) == (0x49)) )) {
        return player1_winner ? -1 : 1;
    }
    else if (((board.player1 | board.player2) == 0b111111111)) {
        return 0;
    }
    tmp = generate_moves(board, &poss);
    memcpy(&(moves[0]), tmp, sizeof(int) * poss);
    ((void) sizeof ((poss > 0) ? 1 : 0), __extension__ ({ if (poss > 0) ; else __assert_fail ("poss > 0", "main.c", 151, __extension__ __PRETTY_FUNCTION__); }));
    if (maximizing) {
        v = -1000;
        for (i = 0; i < poss; ++i) {
            create_child(&cpy, &board, is_player1 ^ 1, moves[i]);
            score = alphabeta(cpy, is_player1 ^ 1, depth + 1, alpha, beta, 0, player1_winner);
            v = score > v ? score : v;
            alpha = alpha > v ? alpha : v;
            if (beta <= alpha) {
                break;
            }
        }
    }
    else {
        v = 1000;
        for (i = 0; i < poss; ++i) {
            create_child(&cpy, &board, is_player1 ^ 1, moves[i]);
            score = alphabeta(cpy, is_player1 ^ 1, depth + 1, alpha, beta, 1, player1_winner);
            v = score < v ? score : v;
            beta = v < beta ? v : beta;
            if (beta <= alpha) {
                break;
            }
        }
    }
    return v;
}

canonical_original_code:
int alphabeta(struct board_t board, char is_player1, int depth, int alpha, int beta, char maximizing, char player1_winner)
{
    int v = 0;
    int i;
    int *tmp;
    int moves[9];
    int poss;
    int score;
    struct board_t cpy;
    if (( (((board.player1) & (0x54)) == (0x54)) || (((board.player1) & (0x111)) == (0x111)) || (((board.player1) & (0x7)) == (0x7)) || (((board.player1) & (0x38)) == (0x38)) || (((board.player1) & (0x1c0)) == (0x1c0)) || (((board.player1) & (0x124)) == (0x124)) || (((board.player1) & (0x92)) == (0x92)) || (((board.player1) & (0x49)) == (0x49)) )) {
        return player1_winner ? 1 : -1;
    } else if (( (((board.player2) & (0x54)) == (0x54)) || (((board.player2) & (0x111)) == (0x111)) || (((board.player2) & (0x7)) == (0x7)) || (((board.player2) & (0x38)) == (0x38)) || (((board.player2) & (0x1c0)) == (0x1c0)) || (((board.player2) & (0x124)) == (0x124)) || (((board.player2) & (0x92)) == (0x92)) || (((board.player2) & (0x49)) == (0x49)) )) {
        return player1_winner ? -1 : 1;
    }
    else if (((board.player1 | board.player2) == 0b111111111)) {
        return 0;
    }
    tmp = generate_moves(board, &poss);
    memcpy(&(moves[0]), tmp, sizeof(int) * poss);
    ((void) sizeof ((poss > 0) ? 1 : 0), __extension__ ({ if (poss > 0) ; else __assert_fail ("poss > 0", "main.c", 151, __extension__ __PRETTY_FUNCTION__); }));
    if (maximizing) {
        v = -1000;
        for (i = 0; i < poss; ++i) {
            create_child(&cpy, &board, is_player1 ^ 1, moves[i]);
            score = alphabeta(cpy, is_player1 ^ 1, depth + 1, alpha, beta, 0, player1_winner);
            v = score > v ? score : v;
            alpha = alpha > v ? alpha : v;
            if (beta <= alpha) {
                break;
            }
        }
    }
    else {
        v = 1000;
        for (i = 0; i < poss; ++i) {
            create_child(&cpy, &board, is_player1 ^ 1, moves[i]);
            score = alphabeta(cpy, is_player1 ^ 1, depth + 1, alpha, beta, 1, player1_winner);
            v = score < v ? score : v;
            beta = v < beta ? v : beta;
            if (beta <= alpha) {
                break;
            }
        }
    }
    return v;
}

variable_types: depth int

variable_types: alpha int

variable_types: beta int

variable_types: maximizing char

variable_types: v int

variable_types:
tmp
int *

variable_types:
moves
int [9]

variable_types: poss int

variable_types: score int


decompiled_code:
__int64 __fastcall choose_move(int a1, char a2)
{
int *v2; // rax
int v3; // ebx
unsigned int v4; // er14
__int64 v5; // rbx
int v6; // er13
int v7; // er12
int v8; // eax
unsigned int v10[3]; // [rsp+Ch] [rbp-6Ch] BYREF
unsigned int v11; // [rsp+18h] [rbp-60h] BYREF
int v12; // [rsp+1Ch] [rbp-5Ch] BYREF
int dest[22]; // [rsp+20h] [rbp-58h] BYREF

v10[0] = a1;
v2 = generate_moves(a1, &v12);
v3 = v12;
memcpy(dest, v2, 4LL * v12);
if ( v3 <= 0 )
__assert_fail("poss > 0", "main.c", 0xC2u, "choose_move");
v4 = dest[0];
v5 = 0LL;
v6 = -1;
do
{
v7 = dest[v5];
create_child((__int64)&v11, v10, a2, v7);
v8 = alphabeta(v11, a2, 0, -1000, 1000, 0, a2);
if ( v8 > v6 )
{
v4 = v7;
v6 = v8;
}
++v5;
}
while ( v12 > (int)v5 );
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func12(int a1, char a2)
{
int *v2; // rax
int v3; // ebx
unsigned int v4; // er14
__int64 v5; // rbx
int v6; // er13
int v7; // er12
int v8; // eax
unsigned int v10[3]; // [rsp+Ch] [rbp-6Ch] BYREF
unsigned int v11; // [rsp+18h] [rbp-60h] BYREF
int v12; // [rsp+1Ch] [rbp-5Ch] BYREF
int dest[22]; // [rsp+20h] [rbp-58h] BYREF

v10[0] = a1;
v2 = func9(a1, &v12);
v3 = v12;
memcpy(dest, v2, 4LL * v12);
if ( v3 <= 0 )
__assert_fail("poss > 0", "main.c", 0xC2u, "choose_move");
v4 = dest[0];
v5 = 0LL;
v6 = -1;
do
{
v7 = dest[v5];
func10((__int64)&v11, v10, a2, v7);
v8 = func11(v11, a2, 0, -1000, 1000, 0, a2);
if ( v8 > v6 )
{
v4 = v7;
v6 = v8;
}
++v5;
}
while ( v12 > (int)v5 );
return v4;
}

original_code:
int choose_move(struct board_t board, boolean is_player1) {
    int moves[9];
    int *tmp;
    int poss;
    int best;
    int move;
    int i;
    int score;
    struct board_t cpy;
    tmp = generate_moves(board, &poss);
    memcpy(&(moves[0]), tmp, sizeof(int) * poss);
    ((void) sizeof ((poss > 0) ? 1 : 0), __extension__ ({ if (poss > 0) ; else __assert_fail ("poss > 0", "main.c", 194, __extension__ __PRETTY_FUNCTION__); }));
    best = -1;
    move = moves[0];
    for (i = 0; i < poss; ++i) {
        create_child(&cpy, &board, is_player1, moves[i]);
        score = alphabeta(cpy, is_player1, 0, -1000, 1000, 0, is_player1);
        if (score > best) {
            best = score;
            move = moves[i];
        }
    }
    return move;
}

canonical_original_code:
int choose_move(struct board_t board, char is_player1) {
    int moves[9];
    int *tmp;
    int poss;
    int best;
    int move;
    int i;
    int score;
    struct board_t cpy;
    tmp = generate_moves(board, &poss);
    memcpy(&(moves[0]), tmp, sizeof(int) * poss);
    ((void) sizeof ((poss > 0) ? 1 : 0), __extension__ ({ if (poss > 0) ; else __assert_fail ("poss > 0", "main.c", 194, __extension__ __PRETTY_FUNCTION__); }));
    best = -1;
    move = moves[0];
    for (i = 0; i < poss; ++i) {
        create_child(&cpy, &board, is_player1, moves[i]);
        score = alphabeta(cpy, is_player1, 0, -1000, 1000, 0, is_player1);
        if (score > best) {
            best = score;
            move = moves[i];
        }
    }
    return move;
}

variable_types: struct board_t { unsigned short player1; unsigned short player2; }

variable_types: best int

variable_types: i int


decompiled_code:
char get_human_player_choice()
{
int v0; // edx
char result; // al
size_t v2; // [rsp+0h] [rbp-18h] BYREF
char *lineptr; // [rsp+8h] [rbp-10h] BYREF

lineptr = 0LL;
v2 = 0LL;
printf("Would you like to move first? ");
v0 = getline(&lineptr, &v2, stdin);
result = -1;
if ( v0 >= -1 )
result = (*lineptr & 0xDF) == 89;
return result;
}

canonical_decompiled_code:
char func13()
{
int v0; // edx
char result; // al
size_t v2; // [rsp+0h] [rbp-18h] BYREF
char *lineptr; // [rsp+8h] [rbp-10h] BYREF

lineptr = 0LL;
v2 = 0LL;
printf("Would you like to move first? ");
v0 = getline(&lineptr, &v2, stdin);
result = -1;
if ( v0 >= -1 )
result = (*lineptr & 0xDF) == 89;
return result;
}

original_code:
char get_human_player_choice() {
    char* line = 0;
    size_t n = 0;
    int ret;
    printf("Would you like to move first? ");
    ret = getline(&line, &n, stdin);
    if (ret < -1) {
        return -1;
    }
    else if (line[0] == 'y' || line[0] == 'Y') {
        return 1;
    }
    else {
        return 0;
    }
}

canonical_original_code:
char get_human_player_choice() {
    char *line = 0;
    unsigned long n = 0;
    int ret;
    printf("Would you like to move first? ");
    ret = getline(&line, &n, stdin);
    if (ret < -1) {
        return -1;
    }
    else if (line[0] == 'y' || line[0] == 'Y') {
        return 1;
    }
    else {
        return 0;
    }
}

variable_types:
line
char *

variable_types:
n
unsigned long

variable_types: ret int

return_type: char


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

if ( a2 != a3 )
return choose_move(a1, a2 != 1);
while ( 1 )
{
result = ask_for_move(a1);
if ( (_DWORD)result != -1 )
break;
puts("Invalid move!");
}
return result;
}

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

if ( a2 != a3 )
return func12(a1, a2 != 1);
while ( 1 )
{
result = func8(a1);
if ( (_DWORD)result != -1 )
break;
puts("Invalid move!");
}
return result;
}

original_code:
int get_move(struct board_t board, boolean human_player, boolean turn) {
    int move;
    if (human_player == turn) {
        while ((move = ask_for_move(board)) == -1) {
            printf("Invalid move!\n");
        }
    }
    else {
        move = choose_move(board, human_player != 1);
    }
    return move;
}

canonical_original_code:
int get_move(struct board_t board, char human_player, char turn) {
    int move;
    if (human_player == turn) {
        while ((move = ask_for_move(board)) == -1) {
            printf("Invalid move!\n");
        }
    }
    else {
        move = choose_move(board, human_player != 1);
    }
    return move;
}

variable_types: turn char


main

original_code:
int main(int argc, char **argv)
{
    struct board_t board;
    int move;
    int i;
    boolean turn;
    boolean human_player;
    board.player1 = 0;
    board.player2 = 0;
    turn = 1;
    human_player = get_human_player_choice();
    if (human_player == -1) {
        exit(0);
    }
    for (i = 0; i < 9; ++i) {
        print_board(board);
        if (turn) {
            move = get_move(board, human_player, turn);
            ((board.player1) |= (1 << (move)));
            if (( (((board.player1) & (0x54)) == (0x54)) || (((board.player1) & (0x111)) == (0x111)) || (((board.player1) & (0x7)) == (0x7)) || (((board.player1) & (0x38)) == (0x38)) || (((board.player1) & (0x1c0)) == (0x1c0)) || (((board.player1) & (0x124)) == (0x124)) || (((board.player1) & (0x92)) == (0x92)) || (((board.player1) & (0x49)) == (0x49)) )) {
                printf("Player #1 wins!\n");
                break;
            }
        }
        else {
            move = get_move(board, human_player, turn);
            ((board.player2) |= (1 << (move)));
            if (( (((board.player2) & (0x54)) == (0x54)) || (((board.player2) & (0x111)) == (0x111)) || (((board.player2) & (0x7)) == (0x7)) || (((board.player2) & (0x38)) == (0x38)) || (((board.player2) & (0x1c0)) == (0x1c0)) || (((board.player2) & (0x124)) == (0x124)) || (((board.player2) & (0x92)) == (0x92)) || (((board.player2) & (0x49)) == (0x49)) )) {
                printf("Player #2 wins!\n");
                break;
            }
        }
        turn ^= 1;
        if (((board.player1 | board.player2) == 0b111111111)) {
            printf("Cat's game!\n");
        }
    }
    print_board(board);
    printf("Bye.\n");
    return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
    struct board_t board;
    int move;
    int i;
    char turn;
    char human_player;
    board.player1 = 0;
    board.player2 = 0;
    turn = 1;
    human_player = get_human_player_choice();
    if (human_player == -1) {
        exit(0);
    }
    for (i = 0; i < 9; ++i) {
        print_board(board);
        if (turn) {
            move = get_move(board, human_player, turn);
            ((board.player1) |= (1 << (move)));
            if (( (((board.player1) & (0x54)) == (0x54)) || (((board.player1) & (0x111)) == (0x111)) || (((board.player1) & (0x7)) == (0x7)) || (((board.player1) & (0x38)) == (0x38)) || (((board.player1) & (0x1c0)) == (0x1c0)) || (((board.player1) & (0x124)) == (0x124)) || (((board.player1) & (0x92)) == (0x92)) || (((board.player1) & (0x49)) == (0x49)) )) {
                printf("Player #1 wins!\n");
                break;
            }
        }
        else {
            move = get_move(board, human_player, turn);
            ((board.player2) |= (1 << (move)));
            if (( (((board.player2) & (0x54)) == (0x54)) || (((board.player2) & (0x111)) == (0x111)) || (((board.player2) & (0x7)) == (0x7)) || (((board.player2) & (0x38)) == (0x38)) || (((board.player2) & (0x1c0)) == (0x1c0)) || (((board.player2) & (0x124)) == (0x124)) || (((board.player2) & (0x92)) == (0x92)) || (((board.player2) & (0x49)) == (0x49)) )) {
                printf("Player #2 wins!\n");
                break;
            }
        }
        turn ^= 1;
        if (((board.player1 | board.player2) == 0b111111111)) {
            printf("Cat's game!\n");
        }
    }
    print_board(board);
    printf("Bye.\n");
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: move int


murmur

repo: seletskiy/murmur

original_code:
uint64_t murmur(char* buf, uint64_t buf_len, uint64_t seed)
{
 const uint64_t m = 0xc6a4a7935bd1e995LLU;
 const int r = 47;
 uint64_t h = seed ^ (buf_len * m);
 const uint64_t * data = (const uint64_t *)buf;
 const uint64_t * end = data + (buf_len / 8);
 while(data != end)
 {
  uint64_t k = *data++;
  k *= m;
  k ^= k >> r;
  k *= m;
  h ^= k;
  h *= m;
 }
 const unsigned char * data2 = (const unsigned char*)data;
 switch(buf_len & 7)
 {
 case 7: h ^= (uint64_t)data2[6] << 48;
 case 6: h ^= (uint64_t)data2[5] << 40;
 case 5: h ^= (uint64_t)data2[4] << 32;
 case 4: h ^= (uint64_t)data2[3] << 24;
 case 3: h ^= (uint64_t)data2[2] << 16;
 case 2: h ^= (uint64_t)data2[1] << 8;
 case 1: h ^= (uint64_t)data2[0];
   h *= m;
 };
 h ^= h >> r;
 h *= m;
 h ^= h >> r;
 return h;
}

canonical_original_code:
unsigned long murmur(char *buf, unsigned long buf_len, unsigned long seed)
{
 unsigned long m = 0xc6a4a7935bd1e995LLU;
 int r = 47;
 unsigned long h = seed ^ (buf_len * m);
 unsigned long *data = (const uint64_t *)buf;
 unsigned long *end = data + (buf_len / 8);
 while(data != end)
 {
  unsigned long k = *data++;
  k *= m;
  k ^= k >> r;
  k *= m;
  h ^= k;
  h *= m;
 }
 unsigned char *data2 = (const unsigned char*)data;
 switch(buf_len & 7)
 {
 case 7: h ^= (unsigned long)data2[6] << 48;
 case 6: h ^= (unsigned long)data2[5] << 40;
 case 5: h ^= (unsigned long)data2[4] << 32;
 case 4: h ^= (unsigned long)data2[3] << 24;
 case 3: h ^= (unsigned long)data2[2] << 16;
 case 2: h ^= (unsigned long)data2[1] << 8;
 case 1: h ^= (unsigned long)data2[0];
   h *= m;
 };
 h ^= h >> r;
 h *= m;
 h ^= h >> r;
 return h;
}

variable_types:
buf
char *

variable_types:
seed
unsigned long

variable_types:
m
unsigned long

variable_types: r int

variable_types:
h
unsigned long

variable_types:
data
unsigned long *

variable_types:
end
unsigned long *

variable_types:
k
unsigned long

return_type: unsigned long


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er14
int v5; // eax
int v6; // er15
__int64 v7; // r13
size_t v8; // rbp
unsigned __int8 *v9; // rax
unsigned __int8 *v10; // rbx
__int64 v11; // rsi
unsigned __int64 v12; // r14
unsigned __int64 v13; // rbx
unsigned __int8 *v14; // rax
unsigned __int8 *v15; // r12
__int64 v16; // rsi
int result; // eax
struct stat v18; // [rsp+0h] [rbp-C8h] BYREF

v3 = getpagesize();
if ( argc <= 1 )
{
printf("usage: %s file.ext\n", *argv);
result = 1;
}
else
{
v4 = v3;
v5 = open(argv[1], 0);
v6 = v5;
if ( v5 < 0 )
{
puts("can not open file");
result = 2;
}
else
{
fstat(v5, &v18);
v7 = 0LL;
if ( v18.st_size )
{
v8 = v4;
v9 = (unsigned __int8 *)mmap(0LL, v4, 1, 2, v6, 0LL);
v10 = v9;
v11 = v4;
if ( v18.st_size <= (unsigned __int64)v4 )
v11 = v18.st_size;
v7 = murmur(v9, v11, 0LL);
munmap(v10, v4);
v12 = 10240 * v4;
v13 = 0LL;
while ( 1 )
{
v13 += v8;
if ( v18.st_size <= v13 )
break;
v14 = (unsigned __int8 *)mmap(0LL, v8, 1, 2, v6, v13);
v15 = v14;
v16 = v18.st_size - v13;
if ( v18.st_size - v13 > v8 )
v16 = v8;
v7 = murmur(v14, v16, v7);
munmap(v15, v8);
if ( v13 )
{
if ( !(v13 % v12) )
fprintf(stderr, "hashed %6i MB\n", v13 >> 20);
}
}
}
printf("%llx\n", v7);
close(v6);
result = 0;
}
}
return result;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er14
int v5; // eax
int v6; // er15
__int64 v7; // r13
size_t v8; // rbp
unsigned __int8 *v9; // rax
unsigned __int8 *v10; // rbx
__int64 v11; // rsi
unsigned __int64 v12; // r14
unsigned __int64 v13; // rbx
unsigned __int8 *v14; // rax
unsigned __int8 *v15; // r12
__int64 v16; // rsi
int result; // eax
struct stat v18; // [rsp+0h] [rbp-C8h] BYREF

v3 = getpagesize();
if ( argc <= 1 )
{
printf("usage: %s file.ext\n", *argv);
result = 1;
}
else
{
v4 = v3;
v5 = open(argv[1], 0);
v6 = v5;
if ( v5 < 0 )
{
puts("can not open file");
result = 2;
}
else
{
func10(v5, &v18);
v7 = 0LL;
if ( v18.st_size )
{
v8 = v4;
v9 = (unsigned __int8 *)mmap(0LL, v4, 1, 2, v6, 0LL);
v10 = v9;
v11 = v4;
if ( v18.st_size <= (unsigned __int64)v4 )
v11 = v18.st_size;
v7 = func7(v9, v11, 0LL);
munmap(v10, v4);
v12 = 10240 * v4;
v13 = 0LL;
while ( 1 )
{
v13 += v8;
if ( v18.st_size <= v13 )
break;
v14 = (unsigned __int8 *)mmap(0LL, v8, 1, 2, v6, v13);
v15 = v14;
v16 = v18.st_size - v13;
if ( v18.st_size - v13 > v8 )
v16 = v8;
v7 = func7(v14, v16, v7);
munmap(v15, v8);
if ( v13 )
{
if ( !(v13 % v12) )
fprintf(stderr, "hashed %6i MB\n", v13 >> 20);
}
}
}
printf("%llx\n", v7);
close(v6);
result = 0;
}
}
return result;
}

original_code:
int main(int argc, char** argv) {
    int fd;
    ssize_t n;
    uint64_t seed;
    int page_size = getpagesize();
    uint64_t off;
    struct stat st;
    char *buf;
    if (argc < 2) {
        printf("usage: %s file.ext\n", argv[0]);
        return 1;
    }
    fd = open(argv[1], 00);
    if (fd < 0) {
        printf("can not open file\n");
        return 2;
    }
    fstat(fd, &st);
    off = 0;
    seed = 0;
    while (off < st.st_size) {
        buf = mmap(((void *)0), page_size, 0x1, 0x02, fd, off);
        seed = murmur(buf,
            (st.st_size - off) > page_size ? page_size : (st.st_size - off),
            seed);
        munmap(buf, page_size);
        if (off && off % (page_size * 1024 * 10) == 0) {
            fprintf(stderr, "hashed %6i MB\n", (uint64_t)off / 1024LLU / 1024LLU);
        }
        off += page_size;
    }
    printf("%llx\n", seed);
    close(fd);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
    int fd;
    long n;
    unsigned long seed;
    int page_size = getpagesize();
    unsigned long off;
    struct stat st;
    char *buf;
    if (argc < 2) {
        printf("usage: %s file.ext\n", argv[0]);
        return 1;
    }
    fd = open(argv[1], 00);
    if (fd < 0) {
        printf("can not open file\n");
        return 2;
    }
    fstat(fd, &st);
    off = 0;
    seed = 0;
    while (off < st.st_size) {
        buf = mmap(((void *)0), page_size, 0x1, 0x02, fd, off);
        seed = murmur(buf,
            (st.st_size - off) > page_size ? page_size : (st.st_size - off),
            seed);
        munmap(buf, page_size);
        if (off && off % (page_size * 1024 * 10) == 0) {
            fprintf(stderr, "hashed %6i MB\n", (unsigned long)off / 1024LLU / 1024LLU);
        }
        off += page_size;
    }
    printf("%llx\n", seed);
    close(fd);
    return 0;
}

variable_types: fd int

variable_types: n long

variable_types:
off
unsigned long

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }


repo: selfcommit/MyTail

decompiled_code:
ssize_t __fastcall find_seek_place(void *buf)
{
write(1, "Find_Seek_Place called successfully\n", 0x24uLL);
return write(1, buf, 4uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func7(void *buf)
{
write(1, "Find_Seek_Place called successfully\n", 0x24uLL);
return write(1, buf, 4uLL);
}

original_code:
int find_seek_place(char buffer[4])
{
write (1,"Find_Seek_Place called successfully\n",36);
write (1,buffer,4);
}

variable_types:
buffer
char [4]


decompiled_code:
ssize_t count_newlines()
{
return write(1, "count_newlines called successfully\n", 0x23uLL);
}

canonical_decompiled_code:
ssize_t func8()
{
return write(1, "count_newlines called successfully\n", 0x23uLL);
}

original_code:
int count_newlines()
{
write (1,"count_newlines called successfully\n", 35);
}


decompiled_code:
ssize_t print_remaining()
{
return write(1, "print_remaining called successfully\n", 0x24uLL);
}

canonical_decompiled_code:
ssize_t func9()
{
return write(1, "print_remaining called successfully\n", 0x24uLL);
}

original_code:
int print_remaining()
{
write (1,"print_remaining called successfully\n",36);
}


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int buf[3]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = 1953719668;
write(1, "Main called successfully\n", 0x19uLL);
find_seek_place(buf);
count_newlines();
print_remaining();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int buf[3]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = 1953719668;
write(1, "Main called successfully\n", 0x19uLL);
func7(buf);
func8();
func9();
return 0;
}

original_code:
int main (int argc, char *argv[])
{
char buffer[4]="test";
write (1, "Main called successfully\n",25);
find_seek_place(buffer);
count_newlines();
print_remaining();
}

canonical_original_code:
int main(int argc, char *argv[])
{
char buffer[4] = "test";
write (1, "Main called successfully\n",25);
find_seek_place(buffer);
count_newlines();
print_remaining();
}

variable_types:
argv
char *[]


repo: semahawk/reedd

decompiled_code:
__int64 __fastcall cmd_installs(int argc, char **argv)
{
__int64 v3; // [rsp+0h] [rbp-28h] BYREF
const char *v4; // [rsp+8h] [rbp-20h]
const char *v5; // [rsp+10h] [rbp-18h]

*argv = "reedd installs";
v4 = "null";
v5 = "newest";
argp_parse(&argp, argc, argv, 0x40u, 0LL, &v3);
printf("package %s, version: %s\n", v4, v5);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func1(int argc, char **argv)
{
__int64 v3; // [rsp+0h] [rbp-28h] BYREF
const char *v4; // [rsp+8h] [rbp-20h]
const char *v5; // [rsp+10h] [rbp-18h]

*argv = "reedd installs";
v4 = "null";
v5 = "newest";
argp_parse(&argp, argc, argv, 0x40u, 0LL, &v3);
printf("package %s, version: %s\n", v4, v5);
return 0LL;
}

original_code:
int cmd_installs(int argc, char* argv[], const char* progname){
  struct arguments arguments;
  argv[0] = "reedd installs";
  arguments.pkg.name = "null";
  arguments.pkg.version = "newest";
  argp_parse(&argp, argc, argv, 0x40, 0, &arguments);
  printf("package %s, version: %s\n", arguments.pkg.name, arguments.pkg.version);
  return 0;
}

canonical_original_code:
int cmd_installs(int argc, char *argv[], char *progname){
  struct arguments arguments;
  argv[0] = "reedd installs";
  arguments.pkg.name = "null";
  arguments.pkg.version = "newest";
  argp_parse(&argp, argc, argv, 0x40, 0, &arguments);
  printf("package %s, version: %s\n", arguments.pkg.name, arguments.pkg.version);
  return 0;
}

variable_types: argc int

variable_types:
progname
char *

variable_types: struct arguments { char *args[1]; struct pkg_struct { char *name; char *version; } pkg; }

user_defined_types: struct arguments { char *args[1]; struct pkg_struct pkg; }


repo: senarukana/apuedb

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er13
int v4; // eax
bool v5; // al
bool v6; // cf
bool v7; // zf
const char *v8; // rdi
__int64 v9; // rcx
char **v10; // rsi
char v11; // al
bool v12; // cf
bool v13; // zf
const char *v14; // rdi
__int64 v15; // rcx
char **v16; // rsi
unsigned int v17; // eax
const char *v18; // rdi
char *v19; // rbx
void *v20; // rsp
size_t v21; // r13
char *v22; // r12
char *v23; // r13
char *v24; // r12
const char *v25; // rax
const char *v26; // rbx
size_t v27; // rax
char *string; // [rsp+0h] [rbp-50h] BYREF
char *v29; // [rsp+8h] [rbp-48h]
__int64 v30; // [rsp+10h] [rbp-40h]
size_t v31; // [rsp+18h] [rbp-38h]

v3 = 2;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "nt?");
if ( v4 == -1 )
break;
switch ( v4 )
{
case 'n':
v3 |= 0x40u;
break;
case 't':
v3 |= 0x200u;
break;
case '?':
err_quit("usage: [-n] create new file [-t] truncate db <filename>");
break;
}
}
if ( argc - 1 != optind )
err_quit("usage: [-n] create new file [-t] truncate db <filename>");
v30 = db_open(argv[optind], v3, 420LL);
if ( !v30 )
err_sys("db_open %s error", argv[optind]);
while ( 1 )
{
v20 = alloca(16 * ((MAX_LEN + 15LL) / 0x10uLL));
printf("APUEDB> ");
if ( !fgets((char *)&string, MAX_LEN, stdin) )
{
puts("Bye~");
exit(1);
}
to_lowercase((const char *)&string);
v21 = strlen((const char *)&string);
v31 = v21;
v22 = strchr((const char *)&string, 32);
if ( v22 )
goto LABEL_42;
*((_BYTE *)&string + (int)v21 - 1) = 0;
v5 = strcmp((const char *)&string, "help") != 0;
v6 = 0;
v7 = !v5;
if ( v5 )
{
v8 = "count";
v9 = 6LL;
v10 = &string;
do
{
if ( !v9 )
break;
v6 = *(_BYTE *)v10 < *v8;
v7 = *(_BYTE *)v10 == *v8;
v10 = (char **)((char *)v10 + 1);
++v8;
--v9;
}
while ( v7 );
v11 = (!v6 && !v7) - v6;
v12 = 0;
v13 = v11 == 0;
if ( v11 )
{
v14 = "iterate";
v15 = 8LL;
v16 = &string;
do
{
if ( !v15 )
break;
v12 = *(_BYTE *)v16 < *v14;
v13 = *(_BYTE *)v16 == *v14;
v16 = (char **)((char *)v16 + 1);
++v14;
--v15;
}
while ( v13 );
if ( (!v12 && !v13) == v12 )
{
db_rewind(v30, v16);
while ( 1 )
{
v18 = (const char *)db_nextrec(v30, 0LL);
if ( !v18 )
break;
puts(v18);
}
}
else
{
if ( !strcmp((const char *)&string, "exit") )
{
puts("Bye~");
exit(1);
}
puts("invalid command, use command help for help");
}
}
else
{
v17 = db_count(v30, v10);
printf("%d\n", v17);
LABEL_42:
v23 = strndup((const char *)&string, v22 - (char *)&string);
if ( !strcmp(v23, "add") )
{
string = v22 + 1;
v29 = strchr(v22 + 1, 32);
if ( !v29 )
goto LABEL_33;
v24 = strndup(string, v29 - v22 - 1);
v19 = strndup(v29 + 1, (char *)&string - v29 + (int)v31 - 2);
if ( (unsigned int)db_store(v30, v24, v19, 1LL) )
printf("key %s already exist\n", v24);
else
puts("add +OK");
goto LABEL_36;
}
if ( !strcmp(v23, "update") )
{
string = v22 + 1;
v29 = strchr(v22 + 1, 32);
if ( !v29 )
{
LABEL_33:
puts("invalid add command, usage: add key value");
LABEL_34:
v19 = 0LL;
goto LABEL_38;
}
v24 = strndup(string, v29 - v22 - 1);
v19 = strndup(v29 + 1, (char *)&string - v29 + (int)v31 - 2);
if ( (unsigned int)db_store(v30, v24, v19, 2LL) )
printf("key %s not found\n", v24);
else
puts("update +OK");
LABEL_36:
if ( v24 )
goto LABEL_37;
LABEL_38:
free(v23);
if ( v19 )
free(v19);
}
else if ( !strcmp(v23, "del") )
{
v29 = v22 + 1;
if ( !strchr(v22 + 1, 32) )
{
v24 = strndup(v29, (char *)&string - v22 + (int)v31 - 2);
if ( !(unsigned int)db_delete(v30, v24) )
{
puts("del +OK");
goto LABEL_57;
}
LABEL_59:
printf("key %s not found\n", v24);
goto LABEL_57;
}
puts("invalid del command, usage: del key");
}
else
{
if ( strcmp(v23, "find") )
goto LABEL_34;
v29 = v22 + 1;
if ( strchr(v22 + 1, 32) )
{
puts("invalid find command, usage: find key");
}
else
{
v24 = strndup(v29, (char *)&string - v22 + (int)v31 - 2);
v25 = (const char *)db_fetch(v30, v24);
v26 = v25;
if ( !v25 )
goto LABEL_59;
v27 = strlen(v25);
printf("%d,%s\n", v27, v26);
LABEL_57:
v19 = 0LL;
if ( v24 )
{
LABEL_37:
free(v24);
goto LABEL_38;
}
free(v23);
}
}
}
}
else
{
puts(
"command list:\n"
" [add | update] key value \n"
" [del | find] key \n"
" [open | close] dbname\n"
" [help count iterate exit]");
}
}
}

original_code:
int main(int argc, char **argv) {
 DBHANDLE db;
 int rc,c, flags, len;
 char *ptr_command, *ptr_key, *find;
 char *command, *key, *value;
 command = key = value = ((void *)0);
 char *help_cmd = "command list:\n [add | update] key value \n [del | find] key \n [open | close] dbname\n [help count iterate exit]";
 flags = 02;
 while ((c = getopt(argc, argv, "nt?")) != -1) {
  switch (c) {
  case 'n':
   flags |= 0100;
   break;
  case 't':
   flags |= 01000;
   break;
  case '?':
   err_quit("usage: [-n] create new file [-t] truncate db <filename>");
   break;
  }
 }
 if (optind != argc - 1) {
  err_quit("usage: [-n] create new file [-t] truncate db <filename>");
 }
 if ((db = db_open(argv[optind], flags, (0400 | 0200 | (0400 >> 3) | ((0400 >> 3) >> 3)))) == ((void *)0)) {
  err_sys("db_open %s error", argv[optind]);
 }
 for (;;) {
  char buf[MAX_LEN];
  printf("APUEDB> ");
  if (fgets(buf, MAX_LEN, stdin) == ((void *)0)) {
   printf("Bye~\n");
   exit(1);
  }
  to_lowercase(buf);
  len = strlen(buf);
  if ((ptr_command = strchr(buf,' ')) == ((void *)0)) {
   buf[len-1] = '\0';
   if (strcmp(buf,"help") == 0) {
    printf("%s\n", help_cmd);
    continue;
   } else if (strcmp(buf, "count") == 0) {
    printf("%d\n", db_count(db));
   } else if (strcmp(buf, "iterate") == 0) {
    char *ptr;
    db_rewind(db);
    while ((ptr = db_nextrec(db, ((void *)0))) != ((void *)0)) {
     printf("%s\n", ptr);
    }
    continue;
   } else if (strcmp(buf, "exit") == 0) {
    printf("Bye~\n");
    exit(1);
   } else {
    printf("invalid command, use command help for help\n");
    continue;
   }
  }
  command = strndup(buf, ptr_command - buf);
  if (strcmp(command, "add") == 0) {
   if ((ptr_key = strchr(ptr_command+1, ' ')) == ((void *)0)) {
    printf("invalid add command, usage: add key value\n");
   } else {
    key = strndup(ptr_command+1, ptr_key - ptr_command - 1);
    value = strndup(ptr_key+1, buf + len - 1 - ptr_key - 1);
    rc = db_store(db, key, value, 1);
    if (rc == 0) {
     printf("add +OK\n");
    } else {
     printf("key %s already exist\n", key);
    }
   }
  } else if (strcmp(command, "update") == 0) {
   if ((ptr_key = strchr(ptr_command+1, ' ')) == ((void *)0)) {
    printf("invalid add command, usage: add key value\n");
   } else {
    key = strndup(ptr_command+1, ptr_key - ptr_command - 1);
    value = strndup(ptr_key+1, buf + len - 1 - ptr_key - 1);
    rc = db_store(db, key, value, 2);
    if (rc == 0) {
     printf("update +OK\n");
    } else {
     printf("key %s not found\n", key);
    }
   }
  } else if (strcmp(command, "del") == 0) {
   if ((ptr_key = strchr(ptr_command+1, ' ')) != ((void *)0)) {
    printf("invalid del command, usage: del key\n");
    continue;
   }
   key = strndup(ptr_command+1, buf + len - 1 - ptr_command -1);
   rc = db_delete(db, key);
   if (rc == 0) {
    printf("del +OK\n");
   } else {
    printf("key %s not found\n", key);
   }
  } else if (strcmp(command, "find") == 0) {
   if ((ptr_key = strchr(ptr_command+1, ' ')) != ((void *)0)) {
    printf("invalid find command, usage: find key\n");
    continue;
   }
   key = strndup(ptr_command+1, buf + len - 1 - ptr_command -1);
   find = db_fetch(db, key);
   if (find != ((void *)0)) {
    printf("%d,%s\n", strlen(find), find);
   } else {
    printf("key %s not found\n", key);
   }
  }
  if (key != ((void *)0)) {
   free(key);
   key = ((void *)0);
  }
  if (command != ((void *)0)) {
   free(command);
   command = ((void *)0);
  }
  if (value != ((void *)0)) {
   free(value);
   value = ((void *)0);
  }
 }
}

canonical_original_code:
int main(int argc, char **argv) {
 void *db;
 int rc; int c; int flags; int len;
 char *ptr_command; char *ptr_key; char *find;
 char *command; char *key; char *value;
 command = key = value = ((void *)0);
 char *help_cmd = "command list:\n [add | update] key value \n [del | find] key \n [open | close] dbname\n [help count iterate exit]";
 flags = 02;
 while ((c = getopt(argc, argv, "nt?")) != -1) {
  switch (c) {
  case 'n':
   flags |= 0100;
   break;
  case 't':
   flags |= 01000;
   break;
  case '?':
   err_quit("usage: [-n] create new file [-t] truncate db <filename>");
   break;
  }
 }
 if (optind != argc - 1) {
  err_quit("usage: [-n] create new file [-t] truncate db <filename>");
 }
 if ((db = db_open(argv[optind], flags, (0400 | 0200 | (0400 >> 3) | ((0400 >> 3) >> 3)))) == ((void *)0)) {
  err_sys("db_open %s error", argv[optind]);
 }
 for (;;) {
  char buf[-1];
  printf("APUEDB> ");
  if (fgets(buf, MAX_LEN, stdin) == ((void *)0)) {
   printf("Bye~\n");
   exit(1);
  }
  to_lowercase(buf);
  len = strlen(buf);
  if ((ptr_command = strchr(buf,' ')) == ((void *)0)) {
   buf[len-1] = '\0';
   if (strcmp(buf,"help") == 0) {
    printf("%s\n", help_cmd);
    continue;
   } else if (strcmp(buf, "count") == 0) {
    printf("%d\n", db_count(db));
   } else if (strcmp(buf, "iterate") == 0) {
    char *ptr;
    db_rewind(db);
    while ((ptr = db_nextrec(db, ((void *)0))) != ((void *)0)) {
     printf("%s\n", ptr);
    }
    continue;
   } else if (strcmp(buf, "exit") == 0) {
    printf("Bye~\n");
    exit(1);
   } else {
    printf("invalid command, use command help for help\n");
    continue;
   }
  }
  command = strndup(buf, ptr_command - buf);
  if (strcmp(command, "add") == 0) {
   if ((ptr_key = strchr(ptr_command+1, ' ')) == ((void *)0)) {
    printf("invalid add command, usage: add key value\n");
   } else {
    key = strndup(ptr_command+1, ptr_key - ptr_command - 1);
    value = strndup(ptr_key+1, buf + len - 1 - ptr_key - 1);
    rc = db_store(db, key, value, 1);
    if (rc == 0) {
     printf("add +OK\n");
    } else {
     printf("key %s already exist\n", key);
    }
   }
  } else if (strcmp(command, "update") == 0) {
   if ((ptr_key = strchr(ptr_command+1, ' ')) == ((void *)0)) {
    printf("invalid add command, usage: add key value\n");
   } else {
    key = strndup(ptr_command+1, ptr_key - ptr_command - 1);
    value = strndup(ptr_key+1, buf + len - 1 - ptr_key - 1);
    rc = db_store(db, key, value, 2);
    if (rc == 0) {
     printf("update +OK\n");
    } else {
     printf("key %s not found\n", key);
    }
   }
  } else if (strcmp(command, "del") == 0) {
   if ((ptr_key = strchr(ptr_command+1, ' ')) != ((void *)0)) {
    printf("invalid del command, usage: del key\n");
    continue;
   }
   key = strndup(ptr_command+1, buf + len - 1 - ptr_command -1);
   rc = db_delete(db, key);
   if (rc == 0) {
    printf("del +OK\n");
   } else {
    printf("key %s not found\n", key);
   }
  } else if (strcmp(command, "find") == 0) {
   if ((ptr_key = strchr(ptr_command+1, ' ')) != ((void *)0)) {
    printf("invalid find command, usage: find key\n");
    continue;
   }
   key = strndup(ptr_command+1, buf + len - 1 - ptr_command -1);
   find = db_fetch(db, key);
   if (find != ((void *)0)) {
    printf("%d,%s\n", strlen(find), find);
   } else {
    printf("key %s not found\n", key);
   }
  }
  if (key != ((void *)0)) {
   free(key);
   key = ((void *)0);
  }
  if (command != ((void *)0)) {
   free(command);
   command = ((void *)0);
  }
  if (value != ((void *)0)) {
   free(value);
   value = ((void *)0);
  }
 }
}

variable_types:
argv
char **

variable_types:
db
void *

variable_types: rc int

variable_types: c int

variable_types: flags int

variable_types: len int

variable_types:
find
char *

variable_types:
command
char *

variable_types:
key
char *

variable_types:
value
char *

variable_types:
buf
char [-1]

variable_types:
ptr
char *


decompiled_code:
int __fastcall err_doit(int a1, int a2, const char *a3, void *a4)
{
char *v5; // rbp
size_t v6; // rax
char v7[4120]; // [rsp+0h] [rbp-1018h] BYREF

vsnprintf(v7, 0x1000uLL, a3, a4);
if ( a1 )
{
v5 = strerror(a2);
v6 = strlen(v7);
snprintf(&v7[v6], 4096 - v6, ": %s", v5);
}
*(_WORD *)&v7[strlen(v7)] = 10;
fflush(stdout);
fputs(v7, stderr);
return fflush(0LL);
}

canonical_decompiled_code:
int __fastcall func0(int a1, int a2, const char *a3, void *a4)
{
char *v5; // rbp
size_t v6; // rax
char v7[4120]; // [rsp+0h] [rbp-1018h] BYREF

vsnprintf(v7, 0x1000uLL, a3, a4);
if ( a1 )
{
v5 = strerror(a2);
v6 = strlen(v7);
snprintf(&v7[v6], 4096 - v6, ": %s", v5);
}
*(_WORD *)&v7[strlen(v7)] = 10;
fflush(stdout);
fputs(v7, stderr);
return fflush(0LL);
}

original_code:
static void
err_doit(int errnoflag, int error, const char *fmt, va_list ap)
{
 char buf[4096];
 vsnprintf(buf, 4096, fmt, ap);
 if (errnoflag)
  snprintf(buf+strlen(buf), 4096 -strlen(buf), ": %s",
    strerror(error));
 strcat(buf, "\n");
 fflush(stdout);
 fputs(buf, stderr);
 fflush(((void *)0));
}

canonical_original_code:
static void err_doit(int errnoflag, int error, char *fmt, __builtin_va_list ap)
{
 char buf[4096];
 vsnprintf(buf, 4096, fmt, ap);
 if (errnoflag)
  snprintf(buf+strlen(buf), 4096 -strlen(buf), ": %s",
    strerror(error));
 strcat(buf, "\n");
 fflush(stdout);
 fputs(buf, stderr);
 fflush(((void *)0));
}

variable_types: errnoflag int

variable_types: error int

variable_types:
fmt
char *

variable_types:
buf
char [4096]


decompiled_code:
int err_ret(const char *a1, ...)
{
int *v1; // rax
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
v1 = _errno_location();
return err_doit(1, *v1, a1, va);
}

canonical_decompiled_code:
int func1(const char *a1, ...)
{
int *v1; // rax
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
v1 = _errno_location();
return func0(1, *v1, a1, va);
}

original_code:
void
err_ret(const char *fmt, ...)
{
 va_list ap;
 __builtin_va_start(ap,fmt);
 err_doit(1, (*__errno_location ()), fmt, ap);
 __builtin_va_end(ap);
}

canonical_original_code:
void err_ret(char *fmt, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,fmt);
 err_doit(1, (*__errno_location ()), fmt, ap);
 __builtin_va_end(ap);
}


decompiled_code:
void __noreturn err_sys(const char *a1, ...)
{
int *v1; // rax
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
v1 = _errno_location();
err_doit(1, *v1, a1, va);
exit(1);
}

canonical_decompiled_code:
void __noreturn func2(const char *a1, ...)
{
int *v1; // rax
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
v1 = _errno_location();
func0(1, *v1, a1, va);
exit(1);
}

original_code:
void
err_sys(const char *fmt, ...)
{
 va_list ap;
 __builtin_va_start(ap,fmt);
 err_doit(1, (*__errno_location ()), fmt, ap);
 __builtin_va_end(ap);
 exit(1);
}

canonical_original_code:
void err_sys(char *fmt, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,fmt);
 err_doit(1, (*__errno_location ()), fmt, ap);
 __builtin_va_end(ap);
 exit(1);
}


decompiled_code:
void __noreturn err_exit(int a1, const char *a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
err_doit(1, a1, a2, va);
exit(1);
}

canonical_decompiled_code:
void __noreturn func3(int a1, const char *a2, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a2);
func0(1, a1, a2, va);
exit(1);
}

original_code:
void
err_exit(int error, const char *fmt, ...)
{
 va_list ap;
 __builtin_va_start(ap,fmt);
 err_doit(1, error, fmt, ap);
 __builtin_va_end(ap);
 exit(1);
}

canonical_original_code:
void err_exit(int error, char *fmt, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,fmt);
 err_doit(1, error, fmt, ap);
 __builtin_va_end(ap);
 exit(1);
}


decompiled_code:
void __noreturn err_dump(const char *a1, ...)
{
int *v1; // rax
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
v1 = _errno_location();
err_doit(1, *v1, a1, va);
abort();
}

canonical_decompiled_code:
void __noreturn func4(const char *a1, ...)
{
int *v1; // rax
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
v1 = _errno_location();
func0(1, *v1, a1, va);
abort();
}

original_code:
void
err_dump(const char *fmt, ...)
{
 va_list ap;
 __builtin_va_start(ap,fmt);
 err_doit(1, (*__errno_location ()), fmt, ap);
 __builtin_va_end(ap);
 abort();
 exit(1);
}

canonical_original_code:
void err_dump(char *fmt, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,fmt);
 err_doit(1, (*__errno_location ()), fmt, ap);
 __builtin_va_end(ap);
 abort();
 exit(1);
}


decompiled_code:
int err_msg(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
return err_doit(0, 0, a1, va);
}

canonical_decompiled_code:
int func5(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
return func0(0, 0, a1, va);
}

original_code:
void
err_msg(const char *fmt, ...)
{
 va_list ap;
 __builtin_va_start(ap,fmt);
 err_doit(0, 0, fmt, ap);
 __builtin_va_end(ap);
}

canonical_original_code:
void err_msg(char *fmt, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,fmt);
 err_doit(0, 0, fmt, ap);
 __builtin_va_end(ap);
}


decompiled_code:
void __noreturn err_quit(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
err_doit(0, 0, a1, va);
exit(1);
}

canonical_decompiled_code:
void __noreturn func6(const char *a1, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a1);
func0(0, 0, a1, va);
exit(1);
}

original_code:
void
err_quit(const char *fmt, ...)
{
 va_list ap;
 __builtin_va_start(ap,fmt);
 err_doit(0, 0, fmt, ap);
 __builtin_va_end(ap);
 exit(1);
}

canonical_original_code:
void err_quit(char *fmt, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,fmt);
 err_doit(0, 0, fmt, ap);
 __builtin_va_end(ap);
 exit(1);
}


original_code:
int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len) {
 struct flock lock;
 lock.l_type = type;
 lock.l_start = offset;
 lock.l_whence = whence;
 lock.l_len = len;
 return(fcntl(fd, cmd, &lock));
}

canonical_original_code:
int lock_reg(int fd, int cmd, int type, long offset, int whence, long len) {
 struct flock lock;
 lock.l_type = type;
 lock.l_start = offset;
 lock.l_whence = whence;
 lock.l_len = len;
 return(fcntl(fd, cmd, &lock));
}

variable_types: fd int

variable_types: cmd int

variable_types: type int

variable_types: offset long

variable_types: whence int

variable_types: len long

variable_types: struct flock { short l_type; short l_whence; long l_start; long l_len; int l_pid; }


decompiled_code:
__int64 __fastcall lock_test(int a1, __int16 a2, __int64 a3, __int16 a4, __int64 a5)
{
__int64 result; // rax
__int16 v6[4]; // [rsp+0h] [rbp-28h] BYREF
__int64 v7; // [rsp+8h] [rbp-20h]
__int64 v8; // [rsp+10h] [rbp-18h]
unsigned int v9; // [rsp+18h] [rbp-10h]

v6[0] = a2;
v7 = a3;
v6[1] = a4;
v8 = a5;
if ( fcntl(a1, 5, v6) < 0 )
err_sys("fcntl error");
result = 0LL;
if ( v6[0] != 2 )
result = v9;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, __int16 a2, __int64 a3, __int16 a4, __int64 a5)
{
__int64 result; // rax
__int16 v6[4]; // [rsp+0h] [rbp-28h] BYREF
__int64 v7; // [rsp+8h] [rbp-20h]
__int64 v8; // [rsp+10h] [rbp-18h]
unsigned int v9; // [rsp+18h] [rbp-10h]

v6[0] = a2;
v7 = a3;
v6[1] = a4;
v8 = a5;
if ( fcntl(a1, 5, v6) < 0 )
err_sys("fcntl error");
result = 0LL;
if ( v6[0] != 2 )
result = v9;
return result;
}

original_code:
pid_t lock_test(int fd, int type, off_t offset, int whence, off_t len) {
 struct flock lock;
 lock.l_type = type;
 lock.l_start = offset;
 lock.l_whence = whence;
 lock.l_len = len;
 if (fcntl(fd, 5, &lock) < 0)
  err_sys("fcntl error");
 if (lock.l_type == 2)
  return(0);
 return(lock.l_pid);
}

canonical_original_code:
int lock_test(int fd, int type, long offset, int whence, long len) {
 struct flock lock;
 lock.l_type = type;
 lock.l_start = offset;
 lock.l_whence = whence;
 lock.l_len = len;
 if (fcntl(fd, 5, &lock) < 0)
  err_sys("fcntl error");
 if (lock.l_type == 2)
  return(0);
 return(lock.l_pid);
}

user_defined_types: struct flock { short l_type; short l_whence; long l_start; long l_len; int l_pid; }