slaveinfo

decompiled_code:
int __fastcall slaveinfo(const char *a1)
{
__int64 v1; // rbx
unsigned int v2; // er12
__int64 v3; // rbp
__int64 v4; // rdx
__int64 v5; // rcx
__int64 v6; // r8
__int64 v7; // rsi
__int64 v8; // r9
char *v9; // rbp
int v10; // er13
__int64 v11; // rax
__int64 v12; // rdx
__int64 v13; // rsi
__int64 v14; // rcx
__int16 v15; // ax
__int16 v16; // bp
__int16 v18; // ax
__int16 *v19; // rsi
const char *v20; // rdi
const char *v21; // rax
unsigned __int16 *v22; // rbx
int v23; // ebp
const char *v24; // rax
unsigned __int64 v25; // r14
__int64 v26; // r13
__int16 *v27; // rsi
unsigned __int64 v28; // rdi
const char *v29; // rax
__int16 *v30; // rsi
unsigned __int64 v31; // rdi
const char *v32; // rax
unsigned __int64 v33; // r15
__int64 v34; // rbp
__int64 v35; // rdx
__int64 v36; // rcx
char *v37; // rax

puts("Starting slaveinfo");
if ( !(unsigned int)ec_init(a1) )
return printf("No socket connection on %s\nExcecute as root\n", a1);
printf("ec_init on %s succeeded.\n", a1);
if ( (int)ec_config(0LL, &IOmap) <= 0 )
{
puts("No slaves found!");
}
else
{
printf("%d slaves found and configured.\n", ec_slavecount);
printf("Calculated workcounter %d\n", ec_group[23]);
ec_statecheck(0LL, 4LL, 6000000LL);
if ( ec_slave != 4 )
{
puts("Not all slaves reached safe operational state.");
ec_readstate();
if ( ec_slavecount > 0 )
{
v22 = (unsigned __int16 *)(unsigned int)((_DWORD)&ec_slave + 320);
v23 = 1;
do
{
if ( *v22 != 4 )
{
v24 = (const char *)ec_ALstatuscode2string(v22[1]);
printf("Slave %d State=%2x StatusCode=%4x : %s\n", (unsigned int)v23, *v22, v22[1], v24);
}
++v23;
v22 += 160;
}
while ( ec_slavecount >= v23 );
}
}
v1 = 320LL;
v2 = 1;
ec_configdc();
ec_readstate();
if ( ec_slavecount > 0 )
{
do
{
printf(
"\nSlave:%d\n Name:%s\n Output size: %dbits\n Input size: %dbits\n State: %d\n Delay: %d[ns]\n Has DC: %d\n",
v2,
(const char *)&ec_slave + v1 + 280,
*(unsigned __int16 *)((char *)&ec_slave + v1 + 24),
*(unsigned __int16 *)((char *)&ec_slave + v1 + 42),
*(unsigned __int16 *)((char *)&ec_slave + v1),
*(unsigned int *)((char *)&ec_slave + v1 + 236),
*((unsigned __int8 *)&ec_slave + v1 + 209));
if ( *((_BYTE *)&ec_slave + v1 + 209) )
printf(" DCParentport:%d\n", *((unsigned __int8 *)&ec_slave + v1 + 216));
v3 = 0LL;
printf(
" Activeports:%d.%d.%d.%d\n",
*((_BYTE *)&ec_slave + v1 + 212) & 1,
(*((_BYTE *)&ec_slave + v1 + 212) & 2) != 0,
(*((_BYTE *)&ec_slave + v1 + 212) & 4) != 0,
(*((_BYTE *)&ec_slave + v1 + 212) & 8) != 0);
printf(" Configured address: %4.4x\n", *(unsigned __int16 *)((char *)&ec_slave + v1 + 4));
printf(
" Man: %8.8x ID: %8.8x Rev: %8.8x\n",
*(unsigned int *)((char *)&ec_slave + v1 + 8),
*(unsigned int *)((char *)&ec_slave + v1 + 12),
*(unsigned int *)((char *)&ec_slave + v1 + 16));
do
{
while ( 1 )
{
v4 = *(unsigned __int16 *)((char *)&ec_slave + 8 * v3 + v1 + 57);
if ( (_WORD)v4 )
break;
if ( ++v3 == 8 )
goto LABEL_11;
}
v5 = *(unsigned __int16 *)((char *)&ec_slave + 8 * v3 + v1 + 59);
v6 = *(unsigned int *)((char *)&ec_slave + 8 * v3 + v1 + 61);
v7 = (unsigned int)v3;
v8 = *((unsigned __int8 *)&ec_slave + v1 + v3++ + 121);
printf(" SM%1d A:%4.4x L:%4d F:%8.8x Type:%d\n", v7, v4, v5, v6, v8);
}
while ( v3 != 8 );
LABEL_11:
if ( *((_BYTE *)&ec_slave + v1 + 268) )
{
v9 = (char *)&ec_slave + v1;
v10 = 0;
do
{
v11 = (unsigned __int8)v9[141];
v12 = *(unsigned int *)(v9 + 129);
v13 = (unsigned int)v10++;
v14 = *(unsigned __int16 *)(v9 + 133);
v9 += 16;
printf(
" FMMU%1d Ls:%8.8x Ll:%4d Lsb:%d Leb:%d Ps:%4.4x Psb:%d Ty:%2.2x Act:%2.2x\n",
v13,
v12,
v14,
(unsigned __int8)v9[119],
(unsigned __int8)v9[120],
*(unsigned __int16 *)(v9 + 121),
(unsigned __int8)v9[123],
(unsigned __int8)v9[124],
v11);
}
while ( *((unsigned __int8 *)&ec_slave + v1 + 268) > v10 );
}
printf(
" FMMUfunc 0:%d 1:%d 2:%d 3:%d\n",
*((unsigned __int8 *)&ec_slave + v1 + 193),
*((unsigned __int8 *)&ec_slave + v1 + 195),
*((unsigned __int8 *)&ec_slave + v1 + 195),
*((unsigned __int8 *)&ec_slave + v1 + 196));
printf(
" MBX length wr: %d rd: %d MBX protocols : %2.2x\n",
*(unsigned __int16 *)((char *)&ec_slave + v1 + 198),
*(unsigned __int16 *)((char *)&ec_slave + v1 + 202),
*(unsigned __int16 *)((char *)&ec_slave + v1 + 206));
v15 = ec_siifind(v2, 30LL);
v16 = v15;
if ( v15 )
{
*((_BYTE *)&ec_slave + v1 + 260) = ec_siigetbyte(v2, (unsigned __int16)(v15 + 7));
*((_BYTE *)&ec_slave + v1 + 261) = ec_siigetbyte(v2, (unsigned __int16)(v16 + 8));
*((_BYTE *)&ec_slave + v1 + 262) = ec_siigetbyte(v2, (unsigned __int16)(v16 + 9));
*((_BYTE *)&ec_slave + v1 + 263) = ec_siigetbyte(v2, (unsigned __int16)(v16 + 10));
if ( (ec_siigetbyte(v2, (unsigned __int16)(v16 + 13)) & 2) != 0 )
{
*((_BYTE *)&ec_slave + v1 + 266) = 1;
++*((_BYTE *)&ec_slave + 266);
}
*(_WORD *)((char *)&ec_slave + v1 + 264) = (unsigned __int8)ec_siigetbyte(v2, (unsigned __int16)(v16 + 14));
v18 = *(_WORD *)((char *)&ec_slave + v1 + 264)
+ ((unsigned __int16)ec_siigetbyte(v2, (unsigned __int16)(v16 + 15)) << 8);
*(_WORD *)((char *)&ec_slave + v1 + 264) = v18;
*((_WORD *)&ec_slave + 132) += v18;
}
printf(
" CoE details: %2.2x FoE details: %2.2x EoE details: %2.2x SoE details: %2.2x\n",
*((unsigned __int8 *)&ec_slave + v1 + 260),
*((unsigned __int8 *)&ec_slave + v1 + 261),
*((unsigned __int8 *)&ec_slave + v1 + 262),
*((unsigned __int8 *)&ec_slave + v1 + 263));
printf(
" Ebus current: %d[mA]\n only LRD/LWR:%d\n",
(unsigned int)*(__int16 *)((char *)&ec_slave + v1 + 264),
*((unsigned __int8 *)&ec_slave + v1 + 266));
if ( (*((_BYTE *)&ec_slave + v1 + 206) & 4) != 0 && printSDO )
{
memset(ODlist, 0, 0xAC04uLL);
v19 = ODlist;
v20 = (const char *)v2;
if ( (unsigned int)ec_readODlist(v2, ODlist) )
{
printf(" CoE Object Description found, %d entries.\n", (unsigned __int16)ODlist[1]);
if ( ODlist[1] )
{
v25 = (unsigned int)((_DWORD)ODlist + 6148);
v26 = 0LL;
do
{
v27 = ODlist;
v28 = (unsigned int)v26;
ec_readODdescription((unsigned int)v26, ODlist);
while ( EcatError )
{
v29 = (const char *)ec_elist2string(v28, v27);
v28 = (unsigned __int64)"%s";
v27 = (__int16 *)v29;
printf("%s", v29);
}
printf(
" Index: %4.4x Datatype: %4.4x Objectcode: %2.2x Name: %s\n",
(unsigned __int16)ODlist[v26 + 2],
(unsigned __int16)ODlist[v26 + 1026],
*((unsigned __int8 *)&ODlist[2050] + v26),
(const char *)v25);
memset(OElist, 0, 0x2C02uLL);
v30 = ODlist;
v31 = (unsigned int)v26;
ec_readOE((unsigned int)v26, ODlist, OElist);
while ( EcatError )
{
v32 = (const char *)ec_elist2string(v31, v30);
v31 = (unsigned __int64)"%s";
v30 = (__int16 *)v32;
printf("%s", v32);
}
v33 = (unsigned int)((_DWORD)OElist + 1794);
v34 = 0LL;
do
{
v35 = (unsigned __int16)OElist[v34 + 129];
if ( (_WORD)v35 )
{
v36 = (unsigned __int16)OElist[v34 + 385];
if ( (_WORD)v36 )
{
printf(
" Sub: %2.2x Datatype: %4.4x Bitlength: %4.4x Obj.access: %4.4x Name: %s\n",
(unsigned int)v34,
v35,
v36,
(unsigned __int16)OElist[v34 + 641],
(const char *)v33);
if ( (OElist[v34 + 641] & 7) != 0 )
{
v37 = SDO2string(v2, ODlist[v26 + 2], v34, OElist[v34 + 129]);
printf(" Value :%s\n", v37);
}
}
}
++v34;
v33 += 37LL;
}
while ( *((unsigned __int8 *)&ODlist[2562] + v26) >= (int)v34 );
++v26;
v25 += 37LL;
}
while ( (unsigned __int16)ODlist[1] > (int)v26 );
}
}
else
{
while ( EcatError )
{
v21 = (const char *)ec_elist2string(v20, v19);
v20 = "%s";
v19 = (__int16 *)v21;
printf("%s", v21);
}
}
}
++v2;
v1 += 320LL;
}
while ( ec_slavecount >= (int)v2 );
}
}
puts("End slaveinfo, close socket");
return ec_close();
}

canonical_decompiled_code:
int __fastcall func1(const char *a1)
{
__int64 v1; // rbx
unsigned int v2; // er12
__int64 v3; // rbp
__int64 v4; // rdx
__int64 v5; // rcx
__int64 v6; // r8
__int64 v7; // rsi
__int64 v8; // r9
char *v9; // rbp
int v10; // er13
__int64 v11; // rax
__int64 v12; // rdx
__int64 v13; // rsi
__int64 v14; // rcx
__int16 v15; // ax
__int16 v16; // bp
__int16 v18; // ax
__int16 *v19; // rsi
const char *v20; // rdi
const char *v21; // rax
unsigned __int16 *v22; // rbx
int v23; // ebp
const char *v24; // rax
unsigned __int64 v25; // r14
__int64 v26; // r13
__int16 *v27; // rsi
unsigned __int64 v28; // rdi
const char *v29; // rax
__int16 *v30; // rsi
unsigned __int64 v31; // rdi
const char *v32; // rax
unsigned __int64 v33; // r15
__int64 v34; // rbp
__int64 v35; // rdx
__int64 v36; // rcx
char *v37; // rax

puts("Starting slaveinfo");
if ( !(unsigned int)ec_init(a1) )
return printf("No socket connection on %s\nExcecute as root\n", a1);
printf("ec_init on %s succeeded.\n", a1);
if ( (int)ec_config(0LL, &IOmap) <= 0 )
{
puts("No slaves found!");
}
else
{
printf("%d slaves found and configured.\n", ec_slavecount);
printf("Calculated workcounter %d\n", ec_group[23]);
ec_statecheck(0LL, 4LL, 6000000LL);
if ( ec_slave != 4 )
{
puts("Not all slaves reached safe operational state.");
ec_readstate();
if ( ec_slavecount > 0 )
{
v22 = (unsigned __int16 *)(unsigned int)((_DWORD)&ec_slave + 320);
v23 = 1;
do
{
if ( *v22 != 4 )
{
v24 = (const char *)ec_ALstatuscode2string(v22[1]);
printf("Slave %d State=%2x StatusCode=%4x : %s\n", (unsigned int)v23, *v22, v22[1], v24);
}
++v23;
v22 += 160;
}
while ( ec_slavecount >= v23 );
}
}
v1 = 320LL;
v2 = 1;
ec_configdc();
ec_readstate();
if ( ec_slavecount > 0 )
{
do
{
printf(
"\nSlave:%d\n Name:%s\n Output size: %dbits\n Input size: %dbits\n State: %d\n Delay: %d[ns]\n Has DC: %d\n",
v2,
(const char *)&ec_slave + v1 + 280,
*(unsigned __int16 *)((char *)&ec_slave + v1 + 24),
*(unsigned __int16 *)((char *)&ec_slave + v1 + 42),
*(unsigned __int16 *)((char *)&ec_slave + v1),
*(unsigned int *)((char *)&ec_slave + v1 + 236),
*((unsigned __int8 *)&ec_slave + v1 + 209));
if ( *((_BYTE *)&ec_slave + v1 + 209) )
printf(" DCParentport:%d\n", *((unsigned __int8 *)&ec_slave + v1 + 216));
v3 = 0LL;
printf(
" Activeports:%d.%d.%d.%d\n",
*((_BYTE *)&ec_slave + v1 + 212) & 1,
(*((_BYTE *)&ec_slave + v1 + 212) & 2) != 0,
(*((_BYTE *)&ec_slave + v1 + 212) & 4) != 0,
(*((_BYTE *)&ec_slave + v1 + 212) & 8) != 0);
printf(" Configured address: %4.4x\n", *(unsigned __int16 *)((char *)&ec_slave + v1 + 4));
printf(
" Man: %8.8x ID: %8.8x Rev: %8.8x\n",
*(unsigned int *)((char *)&ec_slave + v1 + 8),
*(unsigned int *)((char *)&ec_slave + v1 + 12),
*(unsigned int *)((char *)&ec_slave + v1 + 16));
do
{
while ( 1 )
{
v4 = *(unsigned __int16 *)((char *)&ec_slave + 8 * v3 + v1 + 57);
if ( (_WORD)v4 )
break;
if ( ++v3 == 8 )
goto LABEL_11;
}
v5 = *(unsigned __int16 *)((char *)&ec_slave + 8 * v3 + v1 + 59);
v6 = *(unsigned int *)((char *)&ec_slave + 8 * v3 + v1 + 61);
v7 = (unsigned int)v3;
v8 = *((unsigned __int8 *)&ec_slave + v1 + v3++ + 121);
printf(" SM%1d A:%4.4x L:%4d F:%8.8x Type:%d\n", v7, v4, v5, v6, v8);
}
while ( v3 != 8 );
LABEL_11:
if ( *((_BYTE *)&ec_slave + v1 + 268) )
{
v9 = (char *)&ec_slave + v1;
v10 = 0;
do
{
v11 = (unsigned __int8)v9[141];
v12 = *(unsigned int *)(v9 + 129);
v13 = (unsigned int)v10++;
v14 = *(unsigned __int16 *)(v9 + 133);
v9 += 16;
printf(
" FMMU%1d Ls:%8.8x Ll:%4d Lsb:%d Leb:%d Ps:%4.4x Psb:%d Ty:%2.2x Act:%2.2x\n",
v13,
v12,
v14,
(unsigned __int8)v9[119],
(unsigned __int8)v9[120],
*(unsigned __int16 *)(v9 + 121),
(unsigned __int8)v9[123],
(unsigned __int8)v9[124],
v11);
}
while ( *((unsigned __int8 *)&ec_slave + v1 + 268) > v10 );
}
printf(
" FMMUfunc 0:%d 1:%d 2:%d 3:%d\n",
*((unsigned __int8 *)&ec_slave + v1 + 193),
*((unsigned __int8 *)&ec_slave + v1 + 195),
*((unsigned __int8 *)&ec_slave + v1 + 195),
*((unsigned __int8 *)&ec_slave + v1 + 196));
printf(
" MBX length wr: %d rd: %d MBX protocols : %2.2x\n",
*(unsigned __int16 *)((char *)&ec_slave + v1 + 198),
*(unsigned __int16 *)((char *)&ec_slave + v1 + 202),
*(unsigned __int16 *)((char *)&ec_slave + v1 + 206));
v15 = ec_siifind(v2, 30LL);
v16 = v15;
if ( v15 )
{
*((_BYTE *)&ec_slave + v1 + 260) = ec_siigetbyte(v2, (unsigned __int16)(v15 + 7));
*((_BYTE *)&ec_slave + v1 + 261) = ec_siigetbyte(v2, (unsigned __int16)(v16 + 8));
*((_BYTE *)&ec_slave + v1 + 262) = ec_siigetbyte(v2, (unsigned __int16)(v16 + 9));
*((_BYTE *)&ec_slave + v1 + 263) = ec_siigetbyte(v2, (unsigned __int16)(v16 + 10));
if ( (ec_siigetbyte(v2, (unsigned __int16)(v16 + 13)) & 2) != 0 )
{
*((_BYTE *)&ec_slave + v1 + 266) = 1;
++*((_BYTE *)&ec_slave + 266);
}
*(_WORD *)((char *)&ec_slave + v1 + 264) = (unsigned __int8)ec_siigetbyte(v2, (unsigned __int16)(v16 + 14));
v18 = *(_WORD *)((char *)&ec_slave + v1 + 264)
+ ((unsigned __int16)ec_siigetbyte(v2, (unsigned __int16)(v16 + 15)) << 8);
*(_WORD *)((char *)&ec_slave + v1 + 264) = v18;
*((_WORD *)&ec_slave + 132) += v18;
}
printf(
" CoE details: %2.2x FoE details: %2.2x EoE details: %2.2x SoE details: %2.2x\n",
*((unsigned __int8 *)&ec_slave + v1 + 260),
*((unsigned __int8 *)&ec_slave + v1 + 261),
*((unsigned __int8 *)&ec_slave + v1 + 262),
*((unsigned __int8 *)&ec_slave + v1 + 263));
printf(
" Ebus current: %d[mA]\n only LRD/LWR:%d\n",
(unsigned int)*(__int16 *)((char *)&ec_slave + v1 + 264),
*((unsigned __int8 *)&ec_slave + v1 + 266));
if ( (*((_BYTE *)&ec_slave + v1 + 206) & 4) != 0 && printSDO )
{
memset(ODlist, 0, 0xAC04uLL);
v19 = ODlist;
v20 = (const char *)v2;
if ( (unsigned int)ec_readODlist(v2, ODlist) )
{
printf(" CoE Object Description found, %d entries.\n", (unsigned __int16)ODlist[1]);
if ( ODlist[1] )
{
v25 = (unsigned int)((_DWORD)ODlist + 6148);
v26 = 0LL;
do
{
v27 = ODlist;
v28 = (unsigned int)v26;
ec_readODdescription((unsigned int)v26, ODlist);
while ( EcatError )
{
v29 = (const char *)ec_elist2string(v28, v27);
v28 = (unsigned __int64)"%s";
v27 = (__int16 *)v29;
printf("%s", v29);
}
printf(
" Index: %4.4x Datatype: %4.4x Objectcode: %2.2x Name: %s\n",
(unsigned __int16)ODlist[v26 + 2],
(unsigned __int16)ODlist[v26 + 1026],
*((unsigned __int8 *)&ODlist[2050] + v26),
(const char *)v25);
memset(OElist, 0, 0x2C02uLL);
v30 = ODlist;
v31 = (unsigned int)v26;
ec_readOE((unsigned int)v26, ODlist, OElist);
while ( EcatError )
{
v32 = (const char *)ec_elist2string(v31, v30);
v31 = (unsigned __int64)"%s";
v30 = (__int16 *)v32;
printf("%s", v32);
}
v33 = (unsigned int)((_DWORD)OElist + 1794);
v34 = 0LL;
do
{
v35 = (unsigned __int16)OElist[v34 + 129];
if ( (_WORD)v35 )
{
v36 = (unsigned __int16)OElist[v34 + 385];
if ( (_WORD)v36 )
{
printf(
" Sub: %2.2x Datatype: %4.4x Bitlength: %4.4x Obj.access: %4.4x Name: %s\n",
(unsigned int)v34,
v35,
v36,
(unsigned __int16)OElist[v34 + 641],
(const char *)v33);
if ( (OElist[v34 + 641] & 7) != 0 )
{
v37 = func0(v2, ODlist[v26 + 2], v34, OElist[v34 + 129]);
printf(" Value :%s\n", v37);
}
}
}
++v34;
v33 += 37LL;
}
while ( *((unsigned __int8 *)&ODlist[2562] + v26) >= (int)v34 );
++v26;
v25 += 37LL;
}
while ( (unsigned __int16)ODlist[1] > (int)v26 );
}
}
else
{
while ( EcatError )
{
v21 = (const char *)ec_elist2string(v20, v19);
v20 = "%s";
v19 = (__int16 *)v21;
printf("%s", v21);
}
}
}
++v2;
v1 += 320LL;
}
while ( ec_slavecount >= (int)v2 );
}
}
puts("End slaveinfo, close socket");
return ec_close();
}

original_code:
void slaveinfo(char *ifname)
{
 int cnt, i, j, nSM;
 uint16 ssigen, dtype;
 printf("Starting slaveinfo\n");
 if (ec_init(ifname))
 {
  printf("ec_init on %s succeeded.\n",ifname);
  if ( ec_config(0, &IOmap) > 0 )
  {
   printf("%d slaves found and configured.\n",ec_slavecount);
   printf("Calculated workcounter %d\n",ec_group[0].expectedWKC);
   ec_statecheck(0, EC_STATE_SAFE_OP, 2000000 * 3);
   if (ec_slave[0].state != EC_STATE_SAFE_OP )
   {
    printf("Not all slaves reached safe operational state.\n");
    ec_readstate();
    for(i = 1; i<=ec_slavecount ; i++)
    {
     if(ec_slave[i].state != EC_STATE_SAFE_OP)
     {
      printf("Slave %d State=%2x StatusCode=%4x : %s\n",
       i, ec_slave[i].state, ec_slave[i].ALstatuscode, ec_ALstatuscode2string(ec_slave[i].ALstatuscode));
     }
    }
   }
   ec_configdc();
   ec_readstate();
   for( cnt = 1 ; cnt <= ec_slavecount ; cnt++)
   {
    printf("\nSlave:%d\n Name:%s\n Output size: %dbits\n Input size: %dbits\n State: %d\n Delay: %d[ns]\n Has DC: %d\n",
        cnt, ec_slave[cnt].name, ec_slave[cnt].Obits, ec_slave[cnt].Ibits,
        ec_slave[cnt].state, ec_slave[cnt].pdelay, ec_slave[cnt].hasdc);
    if (ec_slave[cnt].hasdc) printf(" DCParentport:%d\n", ec_slave[cnt].parentport);
    printf(" Activeports:%d.%d.%d.%d\n", (ec_slave[cnt].activeports & 0x01) > 0 ,
                 (ec_slave[cnt].activeports & 0x02) > 0 ,
                 (ec_slave[cnt].activeports & 0x04) > 0 ,
                 (ec_slave[cnt].activeports & 0x08) > 0 );
    printf(" Configured address: %4.4x\n", ec_slave[cnt].configadr);
    printf(" Man: %8.8x ID: %8.8x Rev: %8.8x\n", (int)ec_slave[cnt].eep_man, (int)ec_slave[cnt].eep_id, (int)ec_slave[cnt].eep_rev);
    for(nSM = 0 ; nSM < 8 ; nSM++)
    {
     if(ec_slave[cnt].SM[nSM].StartAddr > 0)
      printf(" SM%1d A:%4.4x L:%4d F:%8.8x Type:%d\n",nSM, ec_slave[cnt].SM[nSM].StartAddr, ec_slave[cnt].SM[nSM].SMlength,
           (int)ec_slave[cnt].SM[nSM].SMflags, ec_slave[cnt].SMtype[nSM]);
    }
    for(j = 0 ; j < ec_slave[cnt].FMMUunused ; j++)
    {
     printf(" FMMU%1d Ls:%8.8x Ll:%4d Lsb:%d Leb:%d Ps:%4.4x Psb:%d Ty:%2.2x Act:%2.2x\n", j,
           (int)ec_slave[cnt].FMMU[j].LogStart, ec_slave[cnt].FMMU[j].LogLength, ec_slave[cnt].FMMU[j].LogStartbit,
           ec_slave[cnt].FMMU[j].LogEndbit, ec_slave[cnt].FMMU[j].PhysStart, ec_slave[cnt].FMMU[j].PhysStartBit,
           ec_slave[cnt].FMMU[j].FMMUtype, ec_slave[cnt].FMMU[j].FMMUactive);
    }
    printf(" FMMUfunc 0:%d 1:%d 2:%d 3:%d\n",
             ec_slave[cnt].FMMU0func, ec_slave[cnt].FMMU2func, ec_slave[cnt].FMMU2func, ec_slave[cnt].FMMU3func);
    printf(" MBX length wr: %d rd: %d MBX protocols : %2.2x\n", ec_slave[cnt].mbx_l, ec_slave[cnt].mbx_rl, ec_slave[cnt].mbx_proto);
    ssigen = ec_siifind(cnt, ECT_SII_GENERAL);
    if (ssigen)
                {
     ec_slave[cnt].CoEdetails = ec_siigetbyte(cnt, ssigen + 0x07);
     ec_slave[cnt].FoEdetails = ec_siigetbyte(cnt, ssigen + 0x08);
     ec_slave[cnt].EoEdetails = ec_siigetbyte(cnt, ssigen + 0x09);
     ec_slave[cnt].SoEdetails = ec_siigetbyte(cnt, ssigen + 0x0a);
     if((ec_siigetbyte(cnt, ssigen + 0x0d) & 0x02) > 0)
     {
      ec_slave[cnt].blockLRW = 1;
      ec_slave[0].blockLRW++;
     }
     ec_slave[cnt].Ebuscurrent = ec_siigetbyte(cnt, ssigen + 0x0e);
     ec_slave[cnt].Ebuscurrent += ec_siigetbyte(cnt, ssigen + 0x0f) << 8;
     ec_slave[0].Ebuscurrent += ec_slave[cnt].Ebuscurrent;
                }
    printf(" CoE details: %2.2x FoE details: %2.2x EoE details: %2.2x SoE details: %2.2x\n",
          ec_slave[cnt].CoEdetails, ec_slave[cnt].FoEdetails, ec_slave[cnt].EoEdetails, ec_slave[cnt].SoEdetails);
    printf(" Ebus current: %d[mA]\n only LRD/LWR:%d\n",
          ec_slave[cnt].Ebuscurrent, ec_slave[cnt].blockLRW);
    if ((ec_slave[cnt].mbx_proto & 0x04) && printSDO)
    {
     ODlist.Entries = 0;
     memset(&ODlist, 0, sizeof(ODlist));
     if( ec_readODlist(cnt, &ODlist))
     {
      printf(" CoE Object Description found, %d entries.\n",ODlist.Entries);
      for( i = 0 ; i < ODlist.Entries ; i++)
      {
       ec_readODdescription(i, &ODlist);
       while(EcatError)
       {
        printf("%s", ec_elist2string());
       }
       printf(" Index: %4.4x Datatype: %4.4x Objectcode: %2.2x Name: %s\n",
           ODlist.Index[i], ODlist.DataType[i], ODlist.ObjectCode[i], ODlist.Name[i]);
       memset(&OElist, 0, sizeof(OElist));
       ec_readOE(i, &ODlist, &OElist);
       while(EcatError)
       {
        printf("%s", ec_elist2string());
       }
       for( j = 0 ; j < ODlist.MaxSub[i]+1 ; j++)
       {
        if ((OElist.DataType[j] > 0) && (OElist.BitLength[j] > 0))
        {
         printf("  Sub: %2.2x Datatype: %4.4x Bitlength: %4.4x Obj.access: %4.4x Name: %s\n",
            j, OElist.DataType[j], OElist.BitLength[j], OElist.ObjAccess[j], OElist.Name[j]);
         if ((OElist.ObjAccess[j] & 0x0007))
         {
          dtype = OElist.DataType[j];
          printf("          Value :%s\n", SDO2string(cnt, ODlist.Index[i], j, OElist.DataType[j]));
         }
        }
       }
      }
     }
     else
     {
      while(EcatError)
      {
       printf("%s", ec_elist2string());
      }
     }
    }
   }
  }
  else
  {
   printf("No slaves found!\n");
  }
  printf("End slaveinfo, close socket\n");
  ec_close();
 }
 else
 {
  printf("No socket connection on %s\nExcecute as root\n",ifname);
 }
}

canonical_original_code:
void slaveinfo(char *ifname)
{
 int cnt; int i; int j; int nSM;
 unsigned short ssigen; unsigned short dtype;
 printf("Starting slaveinfo\n");
 if (ec_init(ifname))
 {
  printf("ec_init on %s succeeded.\n",ifname);
  if ( ec_config(0, &IOmap) > 0 )
  {
   printf("%d slaves found and configured.\n",ec_slavecount);
   printf("Calculated workcounter %d\n",ec_group[0].expectedWKC);
   ec_statecheck(0, EC_STATE_SAFE_OP, 2000000 * 3);
   if (ec_slave[0].state != EC_STATE_SAFE_OP )
   {
    printf("Not all slaves reached safe operational state.\n");
    ec_readstate();
    for(i = 1; i<=ec_slavecount ; i++)
    {
     if(ec_slave[i].state != EC_STATE_SAFE_OP)
     {
      printf("Slave %d State=%2x StatusCode=%4x : %s\n",
       i, ec_slave[i].state, ec_slave[i].ALstatuscode, ec_ALstatuscode2string(ec_slave[i].ALstatuscode));
     }
    }
   }
   ec_configdc();
   ec_readstate();
   for( cnt = 1 ; cnt <= ec_slavecount ; cnt++)
   {
    printf("\nSlave:%d\n Name:%s\n Output size: %dbits\n Input size: %dbits\n State: %d\n Delay: %d[ns]\n Has DC: %d\n",
        cnt, ec_slave[cnt].name, ec_slave[cnt].Obits, ec_slave[cnt].Ibits,
        ec_slave[cnt].state, ec_slave[cnt].pdelay, ec_slave[cnt].hasdc);
    if (ec_slave[cnt].hasdc) printf(" DCParentport:%d\n", ec_slave[cnt].parentport);
    printf(" Activeports:%d.%d.%d.%d\n", (ec_slave[cnt].activeports & 0x01) > 0 ,
                 (ec_slave[cnt].activeports & 0x02) > 0 ,
                 (ec_slave[cnt].activeports & 0x04) > 0 ,
                 (ec_slave[cnt].activeports & 0x08) > 0 );
    printf(" Configured address: %4.4x\n", ec_slave[cnt].configadr);
    printf(" Man: %8.8x ID: %8.8x Rev: %8.8x\n", (int)ec_slave[cnt].eep_man, (int)ec_slave[cnt].eep_id, (int)ec_slave[cnt].eep_rev);
    for(nSM = 0 ; nSM < 8 ; nSM++)
    {
     if(ec_slave[cnt].SM[nSM].StartAddr > 0)
      printf(" SM%1d A:%4.4x L:%4d F:%8.8x Type:%d\n",nSM, ec_slave[cnt].SM[nSM].StartAddr, ec_slave[cnt].SM[nSM].SMlength,
           (int)ec_slave[cnt].SM[nSM].SMflags, ec_slave[cnt].SMtype[nSM]);
    }
    for(j = 0 ; j < ec_slave[cnt].FMMUunused ; j++)
    {
     printf(" FMMU%1d Ls:%8.8x Ll:%4d Lsb:%d Leb:%d Ps:%4.4x Psb:%d Ty:%2.2x Act:%2.2x\n", j,
           (int)ec_slave[cnt].FMMU[j].LogStart, ec_slave[cnt].FMMU[j].LogLength, ec_slave[cnt].FMMU[j].LogStartbit,
           ec_slave[cnt].FMMU[j].LogEndbit, ec_slave[cnt].FMMU[j].PhysStart, ec_slave[cnt].FMMU[j].PhysStartBit,
           ec_slave[cnt].FMMU[j].FMMUtype, ec_slave[cnt].FMMU[j].FMMUactive);
    }
    printf(" FMMUfunc 0:%d 1:%d 2:%d 3:%d\n",
             ec_slave[cnt].FMMU0func, ec_slave[cnt].FMMU2func, ec_slave[cnt].FMMU2func, ec_slave[cnt].FMMU3func);
    printf(" MBX length wr: %d rd: %d MBX protocols : %2.2x\n", ec_slave[cnt].mbx_l, ec_slave[cnt].mbx_rl, ec_slave[cnt].mbx_proto);
    ssigen = ec_siifind(cnt, ECT_SII_GENERAL);
    if (ssigen)
                {
     ec_slave[cnt].CoEdetails = ec_siigetbyte(cnt, ssigen + 0x07);
     ec_slave[cnt].FoEdetails = ec_siigetbyte(cnt, ssigen + 0x08);
     ec_slave[cnt].EoEdetails = ec_siigetbyte(cnt, ssigen + 0x09);
     ec_slave[cnt].SoEdetails = ec_siigetbyte(cnt, ssigen + 0x0a);
     if((ec_siigetbyte(cnt, ssigen + 0x0d) & 0x02) > 0)
     {
      ec_slave[cnt].blockLRW = 1;
      ec_slave[0].blockLRW++;
     }
     ec_slave[cnt].Ebuscurrent = ec_siigetbyte(cnt, ssigen + 0x0e);
     ec_slave[cnt].Ebuscurrent += ec_siigetbyte(cnt, ssigen + 0x0f) << 8;
     ec_slave[0].Ebuscurrent += ec_slave[cnt].Ebuscurrent;
                }
    printf(" CoE details: %2.2x FoE details: %2.2x EoE details: %2.2x SoE details: %2.2x\n",
          ec_slave[cnt].CoEdetails, ec_slave[cnt].FoEdetails, ec_slave[cnt].EoEdetails, ec_slave[cnt].SoEdetails);
    printf(" Ebus current: %d[mA]\n only LRD/LWR:%d\n",
          ec_slave[cnt].Ebuscurrent, ec_slave[cnt].blockLRW);
    if ((ec_slave[cnt].mbx_proto & 0x04) && printSDO)
    {
     ODlist.Entries = 0;
     memset(&ODlist, 0, sizeof(ODlist));
     if( ec_readODlist(cnt, &ODlist))
     {
      printf(" CoE Object Description found, %d entries.\n",ODlist.Entries);
      for( i = 0 ; i < ODlist.Entries ; i++)
      {
       ec_readODdescription(i, &ODlist);
       while(EcatError)
       {
        printf("%s", ec_elist2string());
       }
       printf(" Index: %4.4x Datatype: %4.4x Objectcode: %2.2x Name: %s\n",
           ODlist.Index[i], ODlist.DataType[i], ODlist.ObjectCode[i], ODlist.Name[i]);
       memset(&OElist, 0, sizeof(OElist));
       ec_readOE(i, &ODlist, &OElist);
       while(EcatError)
       {
        printf("%s", ec_elist2string());
       }
       for( j = 0 ; j < ODlist.MaxSub[i]+1 ; j++)
       {
        if ((OElist.DataType[j] > 0) && (OElist.BitLength[j] > 0))
        {
         printf("  Sub: %2.2x Datatype: %4.4x Bitlength: %4.4x Obj.access: %4.4x Name: %s\n",
            j, OElist.DataType[j], OElist.BitLength[j], OElist.ObjAccess[j], OElist.Name[j]);
         if ((OElist.ObjAccess[j] & 0x0007))
         {
          dtype = OElist.DataType[j];
          printf("          Value :%s\n", SDO2string(cnt, ODlist.Index[i], j, OElist.DataType[j]));
         }
        }
       }
      }
     }
     else
     {
      while(EcatError)
      {
       printf("%s", ec_elist2string());
      }
     }
    }
   }
  }
  else
  {
   printf("No slaves found!\n");
  }
  printf("End slaveinfo, close socket\n");
  ec_close();
 }
 else
 {
  printf("No socket connection on %s\nExcecute as root\n",ifname);
 }
}

variable_types:
ifname
char *

variable_types: cnt int

variable_types: nSM int

variable_types:
ssigen
unsigned short


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
puts("SOEM (Simple Open EtherCAT Master)\nSlaveinfo");
if ( argc <= 1 )
{
puts("Usage: slaveinfo ifname [options]\nifname = eth0 for example\nOptions : -sdo : print SDO info");
}
else
{
if ( argc != 2 && !strcmp(argv[2], "-sdo") )
printSDO = 1;
slaveinfo(argv[1]);
}
puts("End program");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
puts("SOEM (Simple Open EtherCAT Master)\nSlaveinfo");
if ( argc <= 1 )
{
puts("Usage: slaveinfo ifname [options]\nifname = eth0 for example\nOptions : -sdo : print SDO info");
}
else
{
if ( argc != 2 && !strcmp(argv[2], "-sdo") )
printSDO = 1;
func1(argv[1]);
}
puts("End program");
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 printf("SOEM (Simple Open EtherCAT Master)\nSlaveinfo\n");
 if (argc > 1)
 {
  if ((argc > 2) && (strncmp(argv[2], "-sdo", sizeof("-sdo")) == 0)) printSDO = 1;
  slaveinfo(argv[1]);
 }
 else
 {
  printf("Usage: slaveinfo ifname [options]\nifname = eth0 for example\nOptions : -sdo : print SDO info\n");
 }
 printf("End program\n");
 return (0);
}

variable_types: argc int

variable_types:
argv
char *[]

return_type: int


repo: drewt/Telos

original_code:
char *itoa_16 (unsigned int val, char *buf)
{
 char *ptr, *rc;
 rc = ptr = buf;
 do {
  *ptr++ = "0123456789abcdef"[val & 0xF];
  val >>= 4;
 } while (val);
 *ptr-- = '\0';
 while (buf < ptr) {
  char tmp = *buf;
  *buf++ = *ptr;
  *ptr-- = tmp;
 }
 return rc;
}

canonical_original_code:
char *itoa_16(unsigned int val, char *buf)
{
 char *ptr; char *rc;
 rc = ptr = buf;
 do {
  *ptr++ = "0123456789abcdef"[val & 0xF];
  val >>= 4;
 } while (val);
 *ptr-- = '\0';
 while (buf < ptr) {
  char tmp = *buf;
  *buf++ = *ptr;
  *ptr-- = tmp;
 }
 return rc;
}

variable_types:
val
unsigned int

variable_types:
buf
char *

variable_types:
ptr
char *

variable_types:
rc
char *

variable_types: tmp char

return_type: char *


original_code:
char *itoa_2 (unsigned int val, char *buf)
{
 char *ptr, *rc;
 rc = ptr = buf;
 do {
  *ptr++ = val & 1 ? '1' : '0';
  val >>= 1;
 } while (val);
 *ptr-- = '\0';
 while (buf < ptr) {
  char tmp = *buf;
  *buf++ = *ptr;
  *ptr-- = tmp;
 }
 return rc;
}

canonical_original_code:
char *itoa_2(unsigned int val, char *buf)
{
 char *ptr; char *rc;
 rc = ptr = buf;
 do {
  *ptr++ = val & 1 ? '1' : '0';
  val >>= 1;
 } while (val);
 *ptr-- = '\0';
 while (buf < ptr) {
  char tmp = *buf;
  *buf++ = *ptr;
  *ptr-- = tmp;
 }
 return rc;
}


itoa

original_code:
char *itoa (int val, char *str, int base)
{
 char *rc, *ptr, *low;
 if ( base < 2 || base > 36 ) {
  *str = '\0';
  return str;
 }
 rc = ptr = str;
 if ( val < 0 && base == 10 )
  *ptr++ = '-';
 low = ptr;
 do {
  *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmno"
    "pqrstuvwxyz"[35 + val % base];
  val /= base;
 } while ( val );
 *ptr-- = '\0';
 while ( low < ptr ) {
  char tmp = *low;
  *low++ = *ptr;
  *ptr-- = tmp;
 }
 return rc;
}

canonical_original_code:
char *itoa(int val, char *str, int base)
{
 char *rc; char *ptr; char *low;
 if ( base < 2 || base > 36 ) {
  *str = '\0';
  return str;
 }
 rc = ptr = str;
 if ( val < 0 && base == 10 )
  *ptr++ = '-';
 low = ptr;
 do {
  *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmno"
    "pqrstuvwxyz"[35 + val % base];
  val /= base;
 } while ( val );
 *ptr-- = '\0';
 while ( low < ptr ) {
  char tmp = *low;
  *low++ = *ptr;
  *ptr-- = tmp;
 }
 return rc;
}

variable_types: val int

variable_types:
str
char *

variable_types: base int

variable_types:
low
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
signed int v4; // ebx
int v5; // er12

v3 = (unsigned int)ptsname(0);
if ( !v3 || (v4 = v3, grantpt(0)) || (v5 = unlockpt(0)) != 0 )
{
v5 = 2;
perror("pseudotty");
}
else
{
puts((const char *)v4);
}
return v5;
}

original_code:
int
main(void)
{
    const char *s;
    int fd = 0;
    s = ptsname(fd);
    if(!s) {
        goto ptsnameError;
    }
    int e;
    e = grantpt(fd);
    if(e) {
        goto grantptError;
    }
    e = unlockpt(fd);
    if(e) {
        goto unlockptError;
    }
    printf("%s\n", s);
    return 0;
unlockptError:
grantptError:
ptsnameError:
    perror("pseudotty");
    return 2;
}

canonical_original_code:
int main()
{
    char *s;
    int fd = 0;
    s = ptsname(fd);
    if(!s) {
        goto ptsnameError;
    }
    int e;
    e = grantpt(fd);
    if(e) {
        goto grantptError;
    }
    e = unlockpt(fd);
    if(e) {
        goto unlockptError;
    }
    printf("%s\n", s);
    return 0;
unlockptError:
grantptError:
ptsnameError:
    perror("pseudotty");
    return 2;
}

variable_types:
s
char *

variable_types: fd int

variable_types: e int


repo: dropletzhu/msuite

decompiled_code:
void __fastcall __noreturn term_handler()
{
unsigned int v0; // ebx
int *v1; // rax

v1 = s_count;
do
v0 += *v1++;
while ( v1 != &l_count );
printf("Total received:\t%u\n", (unsigned int)l_count);
printf("Total sent:\t%u\n", v0);
if ( l_count > v0 )
puts("Total lost: 0");
else
printf("Total lost:\t%u\n", v0 - l_count);
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func7()
{
unsigned int v0; // ebx
int *v1; // rax

v1 = s_count;
do
v0 += *v1++;
while ( v1 != &l_count );
printf("Total received:\t%u\n", (unsigned int)l_count);
printf("Total sent:\t%u\n", v0);
if ( l_count > v0 )
puts("Total lost: 0");
else
printf("Total lost:\t%u\n", v0 - l_count);
exit(0);
}

original_code:
void term_handler(int signum)
{
 unsigned int s_total, i;
 for( i = 0; i < 65535; i++ ) {
  s_total += s_count[i];
 }
 printf("Total received:	%u\n", l_count);
 printf("Total sent:	%u\n", s_total);
 if( l_count <= s_total )
  printf("Total lost:	%u\n", s_total - l_count);
 else
  printf("Total lost: 0\n");
 exit(0);
}

canonical_original_code:
void term_handler(int signum)
{
 unsigned int s_total; unsigned int i;
 for( i = 0; i < 65535; i++ ) {
  s_total += s_count[i];
 }
 printf("Total received:	%u\n", l_count);
 printf("Total sent:	%u\n", s_total);
 if( l_count <= s_total )
  printf("Total lost:	%u\n", s_total - l_count);
 else
  printf("Total lost: 0\n");
 exit(0);
}

variable_types: signum int

variable_types:
i
unsigned int

return_type: void


decompiled_code:
int __fastcall short_lived_connection(int fd)
{
ssize_t v1; // rax
ssize_t v2; // rax
_BYTE v4[536]; // [rsp+0h] [rbp-218h] BYREF

memset(v4, 0, 0x200uLL);
v1 = send(fd, v4, 0x200uLL, 0);
printf("send %d bytes\n", v1);
v2 = recv(fd, v4, 0x200uLL, 0);
return printf("recv %d bytes\n", v2);
}

canonical_decompiled_code:
int __fastcall func8(int fd)
{
ssize_t v1; // rax
ssize_t v2; // rax
_BYTE v4[536]; // [rsp+0h] [rbp-218h] BYREF

memset(v4, 0, 0x200uLL);
v1 = send(fd, v4, 0x200uLL, 0);
printf("send %d bytes\n", v1);
v2 = recv(fd, v4, 0x200uLL, 0);
return printf("recv %d bytes\n", v2);
}

original_code:
void short_lived_connection(int sockfd)
{
 char buffer[512];
 int retcode;
 memset(buffer, 0, 512);
 retcode = send(sockfd, buffer, 512, 0);
 printf("send %d bytes\n", retcode);
 retcode = recv(sockfd, buffer, 512, 0);
 printf("recv %d bytes\n", retcode);
}

variable_types: sockfd int

variable_types:
buffer
char [512]

variable_types: retcode int


decompiled_code:
void __fastcall long_lived_connection(int fd)
{
_BYTE v1[1048]; // [rsp+0h] [rbp-418h] BYREF

memset(v1, 0, 0x400uLL);
while ( (int)send(fd, v1, 0x400uLL, 0) >= 0 )
;
perror("send()");
}

canonical_decompiled_code:
void __fastcall func9(int fd)
{
_BYTE v1[1048]; // [rsp+0h] [rbp-418h] BYREF

memset(v1, 0, 0x400uLL);
while ( (int)send(fd, v1, 0x400uLL, 0) >= 0 )
;
perror("send()");
}

original_code:
void long_lived_connection(int sockfd)
{
 char buffer[1024];
 int retcode;
 memset(buffer, 0, 1024);
 while (1) {
  retcode = send(sockfd, buffer, 1024, 0);
  if (retcode < 0) {
   perror("send()");
   break;
  }
 }
}

variable_types:
buffer
char [1024]


decompiled_code:
// local variable allocation has failed, the output may be wrong!
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int16 v3; // r13
unsigned int v4; // ebx
const char **v5; // rbp
int v6; // er12
char v7; // al
int v9; // ebp
__int16 v10; // ax
socklen_t v11; // edx
struct in_addr inp; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr v13[3]; // [rsp+10h] [rbp-38h] BYREF

if ( argc <= 1 )
{
LABEL_7:
usage(*(_QWORD *)&argc, argv, envp);
return -1;
}
v4 = argc;
v5 = argv;
v6 = 0;
while ( 1 )
{
argv = v5;
*(_QWORD *)&argc = v4;
v7 = getopt(v4, (char *const *)v5, "s:p:l");
if ( v7 == -1 )
break;
if ( v7 == 112 )
goto LABEL_9;
if ( v7 == 115 )
{
if ( !inet_aton(_bss_start, &inp) )
{
puts("wrong address format");
return -1;
}
LABEL_9:
v3 = atoi(_bss_start);
}
else
{
v6 = 1;
if ( v7 != 108 )
goto LABEL_7;
}
}
v9 = socket(2, 1, 0);
if ( v9 == -1 )
{
perror("socket()");
exit(-1);
}
v13[0].sa_family = 2;
v10 = _bswap_16(v3, v13, 16LL);
*(_QWORD *)&v13[0].sa_data[6] = 0LL;
*(_WORD *)v13[0].sa_data = v10;
*(struct in_addr *)&v13[0].sa_data[2] = inp;
if ( connect(v9, v13, v11) == -1 )
{
perror("connect()");
exit(-1);
}
if ( v6 )
long_lived_connection(v9);
else
short_lived_connection(v9);
close(v9);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int16 v3; // r13
unsigned int v4; // ebx
const char **v5; // rbp
int v6; // er12
char v7; // al
int v9; // ebp
__int16 v10; // ax
socklen_t v11; // edx
struct in_addr inp; // [rsp+Ch] [rbp-3Ch] BYREF
struct sockaddr v13[3]; // [rsp+10h] [rbp-38h] BYREF

if ( argc <= 1 )
{
LABEL_7:
func10(*(_QWORD *)&argc, argv, envp);
return -1;
}
v4 = argc;
v5 = argv;
v6 = 0;
while ( 1 )
{
argv = v5;
*(_QWORD *)&argc = v4;
v7 = getopt(v4, (char *const *)v5, "s:p:l");
if ( v7 == -1 )
break;
if ( v7 == 112 )
goto LABEL_9;
if ( v7 == 115 )
{
if ( !inet_aton(_bss_start, &inp) )
{
puts("wrong address format");
return -1;
}
LABEL_9:
v3 = atoi(_bss_start);
}
else
{
v6 = 1;
if ( v7 != 108 )
goto LABEL_7;
}
}
v9 = socket(2, 1, 0);
if ( v9 == -1 )
{
perror("socket()");
exit(-1);
}
v13[0].sa_family = 2;
v10 = func7(v3, v13, 16LL);
*(_QWORD *)&v13[0].sa_data[6] = 0LL;
*(_WORD *)v13[0].sa_data = v10;
*(struct in_addr *)&v13[0].sa_data[2] = inp;
if ( connect(v9, v13, v11) == -1 )
{
perror("connect()");
exit(-1);
}
if ( v6 )
func9(v9);
else
func8(v9);
close(v9);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
    int sockfd;
    struct sockaddr_in their_addr;
 struct in_addr server_ip;
 int port;
 char ch;
 int retcode;
 int long_lived = 0;
    if (argc < 2) {
        usage();
        return -1;
    }
    while ((ch = getopt (argc, argv, "s:p:l")) != -1) {
        switch (ch) {
  case 's':
   retcode = inet_aton(optarg, &server_ip);
   if (retcode == 0) {
    printf("wrong address format\n");
    return -1;
   }
        case 'p':
            port = atoi(optarg);
            break;
        case 'l':
            long_lived = 1;
            break;
        default:
            usage();
            return -1;
        }
    }
    if((sockfd = socket(2, SOCK_STREAM, 0)) == -1) {
        perror("socket()");
        exit(-1);
    }
    their_addr.sin_family = 2;
    their_addr.sin_port = htons(port);
    their_addr.sin_addr.s_addr = server_ip.s_addr;
    memset(&(their_addr.sin_zero), '\0', 8);
    if(connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) {
        perror("connect()");
        exit(-1);
    }
 if (long_lived) {
  long_lived_connection(sockfd);
 } else {
  short_lived_connection(sockfd);
 }
    close(sockfd);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

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

variable_types: port int

variable_types: ch char

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

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


usage

decompiled_code:
int usage()
{
puts("Usage: ./tcpclient -s [server_ip] -p [port] -l");
puts(" -s\tserver ip address");
puts(" -p server port");
return puts(" -l long-lived connection, by default, it is short-lived echo connection");
}

canonical_decompiled_code:
int func10()
{
puts("Usage: ./tcpclient -s [server_ip] -p [port] -l");
puts(" -s\tserver ip address");
puts(" -p server port");
return puts(" -l long-lived connection, by default, it is short-lived echo connection");
}

original_code:
void usage()
{
 printf("Usage: ./tcpclient -s [server_ip] -p [port] -l\n");
 printf(" -s	server ip address\n");
 printf(" -p  server port\n");
 printf(" -l  long-lived connection, by default, it is short-lived echo connection\n");
}


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

do
result = wait(0LL);
while ( result > 0 );
return result;
}

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

do
result = wait(0LL);
while ( result > 0 );
return result;
}

original_code:
void sigchld_handler(int s)
{
    while(wait(((void *)0)) > 0);
}

variable_types: s int


decompiled_code:
int __fastcall echo_server(int fd)
{
ssize_t v1; // rax
ssize_t v2; // rax
_BYTE v4[536]; // [rsp+0h] [rbp-218h] BYREF

memset(v4, 0, 0x200uLL);
v1 = recv(fd, v4, 0x200uLL, 0);
printf("recv %d\n", v1);
v2 = send(fd, v4, 0x200uLL, 0);
return printf("send %d\n", v2);
}

canonical_decompiled_code:
int __fastcall func9(int fd)
{
ssize_t v1; // rax
ssize_t v2; // rax
_BYTE v4[536]; // [rsp+0h] [rbp-218h] BYREF

memset(v4, 0, 0x200uLL);
v1 = recv(fd, v4, 0x200uLL, 0);
printf("recv %d\n", v1);
v2 = send(fd, v4, 0x200uLL, 0);
return printf("send %d\n", v2);
}

original_code:
void echo_server(int socket_fd)
{
 int result;
 char buffer[512];
 memset(buffer, 0, 512);
 result = recv(socket_fd, buffer, 512, 0);
 printf("recv %d\n", result);
 result = send(socket_fd, buffer, 512, 0);
 printf("send %d\n", result);
}

variable_types: result int


decompiled_code:
int __fastcall long_lived_server(int fd)
{
int v1; // er15
__int64 v3; // rbx
__int64 v4; // r13
int v5; // eax
int v6; // eax
char *v7; // rax
int v8; // edi
char *v9; // rax
int *v11; // [rsp+8h] [rbp-8C0h]
fd_set readfds; // [rsp+10h] [rbp-8B8h] BYREF
char buf[2104]; // [rsp+90h] [rbp-838h] BYREF

v1 = fd + 1;
v3 = fd / 64;
v4 = 1LL << (fd % 64);
v11 = __errno_location();
do
{
while ( 1 )
{
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v3] |= v4;
v5 = select(v1, &readfds, 0LL, 0LL, 0LL);
if ( v5 != -1 )
break;
v8 = *v11;
if ( *v11 != 4 )
goto LABEL_9;
}
if ( v5 <= 0 )
break;
if ( (readfds.fds_bits[v3] & v4) != 0 )
{
do
v6 = recv(fd, buf, 0x800uLL, 0);
while ( v6 > 0 );
if ( v6 )
{
v7 = strerror(*v11);
printf("Error on recv(): %s\n", v7);
}
}
}
}
while ( !v5 );
v8 = *v11;
LABEL_9:
v9 = strerror(v8);
return printf("Error on select(): %s\n", v9);
}

canonical_decompiled_code:
int __fastcall func10(int fd)
{
int v1; // er15
__int64 v3; // rbx
__int64 v4; // r13
int v5; // eax
int v6; // eax
char *v7; // rax
int v8; // edi
char *v9; // rax
int *v11; // [rsp+8h] [rbp-8C0h]
fd_set readfds; // [rsp+10h] [rbp-8B8h] BYREF
char buf[2104]; // [rsp+90h] [rbp-838h] BYREF

v1 = fd + 1;
v3 = fd / 64;
v4 = 1LL << (fd % 64);
v11 = __errno_location();
do
{
while ( 1 )
{
while ( 1 )
{
memset(&readfds, 0, sizeof(readfds));
readfds.fds_bits[v3] |= v4;
v5 = select(v1, &readfds, 0LL, 0LL, 0LL);
if ( v5 != -1 )
break;
v8 = *v11;
if ( *v11 != 4 )
goto LABEL_9;
}
if ( v5 <= 0 )
break;
if ( (readfds.fds_bits[v3] & v4) != 0 )
{
do
v6 = recv(fd, buf, 0x800uLL, 0);
while ( v6 > 0 );
if ( v6 )
{
v7 = strerror(*v11);
printf("Error on recv(): %s\n", v7);
}
}
}
}
while ( !v5 );
v8 = *v11;
LABEL_9:
v9 = strerror(v8);
return printf("Error on select(): %s\n", v9);
}

original_code:
void long_lived_server(int socket_fd)
{
 int result;
 fd_set readset;
 char buffer[2048];
 while (1) {
  do {
   do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readset)->__fds_bits)[0]) : "memory"); } while (0);
   ((void) (((&readset)->__fds_bits)[((socket_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((socket_fd) % (8 * (int) sizeof (__fd_mask)))))));
   result = select(socket_fd + 1, &readset, ((void *)0), ((void *)0), ((void *)0));
  } while (result == -1 && (*__errno_location ()) == 4);
  if (result > 0) {
   if (((((&readset)->__fds_bits)[((socket_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((socket_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    do {
     result = recv(socket_fd, buffer, 2048, 0);
    } while (result > 0);
    if (result < 0) {
     printf("Error on recv(): %s\n", strerror((*__errno_location ())));
    }
   }
  } else if (result < 0) {
   printf("Error on select(): %s\n", strerror((*__errno_location ())));
   break;
  }
 }
}

canonical_original_code:
void long_lived_server(int socket_fd)
{
 int result;
 struct fd_set readset;
 char buffer[2048];
 while (1) {
  do {
   do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&readset)->__fds_bits)[0]) : "memory"); } while (0);
   ((void) (((&readset)->__fds_bits)[((socket_fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((socket_fd) % (8 * (int) sizeof (__fd_mask)))))));
   result = select(socket_fd + 1, &readset, ((void *)0), ((void *)0), ((void *)0));
  } while (result == -1 && (*__errno_location ()) == 4);
  if (result > 0) {
   if (((((&readset)->__fds_bits)[((socket_fd) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((socket_fd) % (8 * (int) sizeof (__fd_mask)))))) != 0)) {
    do {
     result = recv(socket_fd, buffer, 2048, 0);
    } while (result > 0);
    if (result < 0) {
     printf("Error on recv(): %s\n", strerror((*__errno_location ())));
    }
   }
  } else if (result < 0) {
   printf("Error on select(): %s\n", strerror((*__errno_location ())));
   break;
  }
 }
}

variable_types:
readset
struct fd_set { long __fds_bits[-1]; }

variable_types:
buffer
char [2048]


main

decompiled_code:
// local variable allocation has failed, the output may be wrong!
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int16 v3; // r12
const char **v4; // r13
int v5; // ebp
unsigned int v6; // ebx
char v7; // al
int v9; // er13
__int16 v10; // ax
socklen_t v11; // edx
__int64 v12; // rdx
int v13; // er12
unsigned __int16 v14; // bx
char *v15; // rax
socklen_t addr_len; // [rsp+8h] [rbp-F0h] BYREF
int optval; // [rsp+Ch] [rbp-ECh] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-E8h] BYREF
struct sockaddr v19; // [rsp+20h] [rbp-D8h] BYREF
struct sigaction v20; // [rsp+30h] [rbp-C8h] BYREF

v4 = argv;
v5 = 0;
v6 = argc;
optval = 1;
if ( argc > 1 )
{
while ( 1 )
{
while ( 1 )
{
argv = v4;
*(_QWORD *)&argc = v6;
v7 = getopt(v6, (char *const *)v4, "p:l");
if ( v7 == -1 )
{
v9 = socket(2, 1, 0);
if ( v9 != -1 )
{
puts("Server-socket() sockfd is OK...");
if ( setsockopt(v9, 1, 2, &optval, 4u) != -1 )
{
puts("Server-setsockopt is OK...");
addr.sa_family = 2;
v10 = _bswap_16(v3, 1LL, 16LL);
*(_DWORD *)&addr.sa_data[2] = 0;
*(_WORD *)addr.sa_data = v10;
*(_QWORD *)&addr.sa_data[6] = 0LL;
if ( bind(v9, &addr, v11) != -1 )
{
puts("Server-bind() is OK...");
if ( listen(v9, 10) != -1 )
{
v20.sa_handler = (__sighandler_t)sigchld_handler;
sigemptyset(&v20.sa_mask);
v20.sa_flags = 0x10000000;
if ( sigaction(17, &v20, 0LL) != -1 )
{
puts("Server-sigaction() is OK...");
while ( 1 )
{
while ( 1 )
{
addr_len = 16;
v13 = accept(v9, &v19, &addr_len);
if ( v13 != -1 )
break;
perror("Server-accept() error");
}
v14 = _bswap_16(*(unsigned __int16 *)v19.sa_data, &v19, v12);
v15 = inet_ntoa(*(struct in_addr *)&v19.sa_data[2]);
printf("Server: Got connection from %s-%d\n", v15, v14);
if ( !fork() )
break;
close(v13);
}
close(v9);
if ( v5 )
long_lived_server(v13);
else
echo_server(v13);
close(v13);
exit(0);
}
perror("Server-sigaction() error");
exit(1);
}
perror("Server-listen() error");
exit(1);
}
perror("Server-bind() error");
exit(1);
}
perror("Server-setsockopt() error lol!");
exit(1);
}
perror("Server-socket() error lol!");
exit(1);
}
if ( v7 != 108 )
break;
v5 = 1;
}
if ( v7 != 112 )
break;
v3 = atoi(_bss_start);
}
}
usage(*(_QWORD *)&argc, argv, envp);
return -1;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned __int16 v3; // r12
const char **v4; // r13
int v5; // ebp
unsigned int v6; // ebx
char v7; // al
int v9; // er13
__int16 v10; // ax
socklen_t v11; // edx
__int64 v12; // rdx
int v13; // er12
unsigned __int16 v14; // bx
char *v15; // rax
socklen_t addr_len; // [rsp+8h] [rbp-F0h] BYREF
int optval; // [rsp+Ch] [rbp-ECh] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-E8h] BYREF
struct sockaddr v19; // [rsp+20h] [rbp-D8h] BYREF
struct sigaction v20; // [rsp+30h] [rbp-C8h] BYREF

v4 = argv;
v5 = 0;
v6 = argc;
optval = 1;
if ( argc > 1 )
{
while ( 1 )
{
while ( 1 )
{
argv = v4;
*(_QWORD *)&argc = v6;
v7 = getopt(v6, (char *const *)v4, "p:l");
if ( v7 == -1 )
{
v9 = socket(2, 1, 0);
if ( v9 != -1 )
{
puts("Server-socket() sockfd is OK...");
if ( setsockopt(v9, 1, 2, &optval, 4u) != -1 )
{
puts("Server-setsockopt is OK...");
addr.sa_family = 2;
v10 = func8(v3, 1LL, 16LL);
*(_DWORD *)&addr.sa_data[2] = 0;
*(_WORD *)addr.sa_data = v10;
*(_QWORD *)&addr.sa_data[6] = 0LL;
if ( bind(v9, &addr, v11) != -1 )
{
puts("Server-bind() is OK...");
if ( listen(v9, 10) != -1 )
{
v20.sa_handler = (__sighandler_t)sigchld_handler;
sigemptyset(&v20.sa_mask);
v20.sa_flags = 0x10000000;
if ( sigaction(17, &v20, 0LL) != -1 )
{
puts("Server-sigaction() is OK...");
while ( 1 )
{
while ( 1 )
{
addr_len = 16;
v13 = accept(v9, &v19, &addr_len);
if ( v13 != -1 )
break;
perror("Server-accept() error");
}
v14 = func8(*(unsigned __int16 *)v19.sa_data, &v19, v12);
v15 = inet_ntoa(*(struct in_addr *)&v19.sa_data[2]);
printf("Server: Got connection from %s-%d\n", v15, v14);
if ( !fork() )
break;
close(v13);
}
close(v9);
if ( v5 )
func10(v13);
else
func9(v13);
close(v13);
exit(0);
}
perror("Server-sigaction() error");
exit(1);
}
perror("Server-listen() error");
exit(1);
}
perror("Server-bind() error");
exit(1);
}
perror("Server-setsockopt() error lol!");
exit(1);
}
perror("Server-socket() error lol!");
exit(1);
}
if ( v7 != 108 )
break;
v5 = 1;
}
if ( v7 != 112 )
break;
v3 = atoi(_bss_start);
}
}
func11(*(_QWORD *)&argc, argv, envp);
return -1;
}

original_code:
int main(int argc, char *argv[ ])
{
    int sockfd, new_fd;
    struct sockaddr_in my_addr;
    struct sockaddr_in their_addr;
    int sin_size;
    struct sigaction sa;
    int yes = 1;
 int port;
 char ch;
 int long_lived = 0;
 if (argc < 2) {
  usage();
  return -1;
 }
 while ((ch = getopt (argc, argv, "p:l")) != -1) {
        switch (ch) {
        case 'p':
   port = atoi(optarg);
            break;
  case 'l':
   long_lived = 1;
   break;
  default:
   usage();
   return -1;
  }
 }
    if ((sockfd = socket(2, SOCK_STREAM, 0)) == -1) {
        perror("Server-socket() error lol!");
        exit(1);
    } else {
        printf("Server-socket() sockfd is OK...\n");
    }
    if (setsockopt(sockfd, 1, 2, &yes, sizeof(int)) == -1) {
        perror("Server-setsockopt() error lol!");
        exit(1);
    } else {
        printf("Server-setsockopt is OK...\n");
    }
    my_addr.sin_family = 2;
    my_addr.sin_port = htons(port);
    my_addr.sin_addr.s_addr = ((in_addr_t) 0x00000000);
    memset(&(my_addr.sin_zero), '\0', 8);
    if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
        perror("Server-bind() error");
        exit(1);
    } else {
        printf("Server-bind() is OK...\n");
    }
    if(listen(sockfd, 10) == -1) {
        perror("Server-listen() error");
        exit(1);
    }
    sa.__sigaction_handler.sa_handler = sigchld_handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0x10000000;
    if(sigaction(17, &sa, ((void *)0)) == -1) {
        perror("Server-sigaction() error");
        exit(1);
    } else {
        printf("Server-sigaction() is OK...\n");
    }
    while(1) {
        sin_size = sizeof(struct sockaddr_in);
        if((new_fd = accept(sockfd, (struct sockaddr *)&their_addr,(socklen_t*)&sin_size)) == -1) {
            perror("Server-accept() error");
            continue;
        }
        printf("Server: Got connection from %s-%d\n", inet_ntoa(their_addr.sin_addr), ntohs(their_addr.sin_port));
        if(!fork()) {
            close(sockfd);
   if (long_lived) {
    long_lived_server(new_fd);
   } else {
    echo_server(new_fd);
   }
            close(new_fd);
            exit(0);
        } else {
            close(new_fd);
        }
    }
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    int sockfd; int new_fd;
    struct sockaddr_in my_addr;
    struct sockaddr_in their_addr;
    int sin_size;
    struct sigaction sa;
    int yes = 1;
 int port;
 char ch;
 int long_lived = 0;
 if (argc < 2) {
  usage();
  return -1;
 }
 while ((ch = getopt (argc, argv, "p:l")) != -1) {
        switch (ch) {
        case 'p':
   port = atoi(optarg);
            break;
  case 'l':
   long_lived = 1;
   break;
  default:
   usage();
   return -1;
  }
 }
    if ((sockfd = socket(2, SOCK_STREAM, 0)) == -1) {
        perror("Server-socket() error lol!");
        exit(1);
    } else {
        printf("Server-socket() sockfd is OK...\n");
    }
    if (setsockopt(sockfd, 1, 2, &yes, sizeof(int)) == -1) {
        perror("Server-setsockopt() error lol!");
        exit(1);
    } else {
        printf("Server-setsockopt is OK...\n");
    }
    my_addr.sin_family = 2;
    my_addr.sin_port = htons(port);
    my_addr.sin_addr.s_addr = ((unsigned int) 0x00000000);
    memset(&(my_addr.sin_zero), '\0', 8);
    if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
        perror("Server-bind() error");
        exit(1);
    } else {
        printf("Server-bind() is OK...\n");
    }
    if(listen(sockfd, 10) == -1) {
        perror("Server-listen() error");
        exit(1);
    }
    sa.__sigaction_handler.sa_handler = sigchld_handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0x10000000;
    if(sigaction(17, &sa, ((void *)0)) == -1) {
        perror("Server-sigaction() error");
        exit(1);
    } else {
        printf("Server-sigaction() is OK...\n");
    }
    while(1) {
        sin_size = sizeof(struct sockaddr_in);
        if((new_fd = accept(sockfd, (struct sockaddr *)&their_addr,(unsigned int *)&sin_size)) == -1) {
            perror("Server-accept() error");
            continue;
        }
        printf("Server: Got connection from %s-%d\n", inet_ntoa(their_addr.sin_addr), ntohs(their_addr.sin_port));
        if(!fork()) {
            close(sockfd);
   if (long_lived) {
    long_lived_server(new_fd);
   } else {
    echo_server(new_fd);
   }
            close(new_fd);
            exit(0);
        } else {
            close(new_fd);
        }
    }
    return 0;
}

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

variable_types: yes int

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


usage

decompiled_code:
int usage()
{
puts("Usage: ./tcpserver -p [port] -l");
puts(" -p [port]\t\tlisten on this port");
return puts(" -l\t\t\tcreate long-lived connection, by default, it is short-lived echo connection");
}

canonical_decompiled_code:
int func11()
{
puts("Usage: ./tcpserver -p [port] -l");
puts(" -p [port]\t\tlisten on this port");
return puts(" -l\t\t\tcreate long-lived connection, by default, it is short-lived echo connection");
}

original_code:
void usage()
{
 printf("Usage: ./tcpserver -p [port] -l\n");
 printf("  -p [port]		listen on this port\n");
 printf("  -l			create long-lived connection, by default, it is short-lived echo connection\n");
}


error

decompiled_code:
void __fastcall __noreturn error(const char *a1)
{
perror(a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func9(const char *a1)
{
perror(a1);
exit(1);
}

original_code:
void error(char *msg) {
  perror(msg);
  exit(1);
}

variable_types:
msg
char *


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned __int16 v3; // bp
int v4; // eax
int v5; // ebx
socklen_t v6; // edx
char *v7; // rsi
size_t v8; // rax
socklen_t addr_len; // [rsp+8h] [rbp-440h] BYREF
int optval; // [rsp+Ch] [rbp-43Ch] BYREF
struct sockaddr v11; // [rsp+10h] [rbp-438h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-428h] BYREF
char s[1048]; // [rsp+30h] [rbp-418h] BYREF

if ( argc != 2 )
{
fprintf(stderr, "usage: %s <port>\n", *argv);
exit(1);
}
v3 = atoi(argv[1]);
v4 = socket(2, 2, 0);
v5 = v4;
if ( v4 >= 0 )
{
optval = 1;
setsockopt(v4, 1, 2, &optval, 4u);
*(_QWORD *)&v11.sa_data[6] = 0LL;
v11.sa_family = 2;
*(_QWORD *)&v11.sa_data[2] = (unsigned int)_bswap_32_constprop_0(v3, &v11, 16LL);
*(_WORD *)v11.sa_data = _bswap_16();
if ( bind(v5, &v11, v6) >= 0 )
{
addr_len = 16;
while ( 1 )
{
memset(s, 0, 0x400uLL);
if ( (int)recvfrom(v5, s, 0x400uLL, 0, &addr, &addr_len) < 0 )
break;
v7 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
if ( !v7 )
error("ERROR on inet_ntoa\n");
printf("server received datagram from %s\n", v7);
v8 = strlen(s);
if ( (int)sendto(v5, s, v8, 0, &addr, addr_len) < 0 )
error("ERROR in sendto");
}
error("ERROR in recvfrom");
}
error("ERROR on binding");
}
error("ERROR opening socket");
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
unsigned __int16 v3; // bp
int v4; // eax
int v5; // ebx
socklen_t v6; // edx
char *v7; // rsi
size_t v8; // rax
socklen_t addr_len; // [rsp+8h] [rbp-440h] BYREF
int optval; // [rsp+Ch] [rbp-43Ch] BYREF
struct sockaddr v11; // [rsp+10h] [rbp-438h] BYREF
struct sockaddr addr; // [rsp+20h] [rbp-428h] BYREF
char s[1048]; // [rsp+30h] [rbp-418h] BYREF

if ( argc != 2 )
{
fprintf(stderr, "usage: %s <port>\n", *argv);
exit(1);
}
v3 = atoi(argv[1]);
v4 = socket(2, 2, 0);
v5 = v4;
if ( v4 >= 0 )
{
optval = 1;
setsockopt(v4, 1, 2, &optval, 4u);
*(_QWORD *)&v11.sa_data[6] = 0LL;
v11.sa_family = 2;
*(_QWORD *)&v11.sa_data[2] = (unsigned int)func8(v3, &v11, 16LL);
*(_WORD *)v11.sa_data = func7();
if ( bind(v5, &v11, v6) >= 0 )
{
addr_len = 16;
while ( 1 )
{
memset(s, 0, 0x400uLL);
if ( (int)recvfrom(v5, s, 0x400uLL, 0, &addr, &addr_len) < 0 )
break;
v7 = inet_ntoa(*(struct in_addr *)&addr.sa_data[2]);
if ( !v7 )
func9("ERROR on inet_ntoa\n");
printf("server received datagram from %s\n", v7);
v8 = strlen(s);
if ( (int)sendto(v5, s, v8, 0, &addr, addr_len) < 0 )
func9("ERROR in sendto");
}
func9("ERROR in recvfrom");
}
func9("ERROR on binding");
}
func9("ERROR opening socket");
}

original_code:
int main(int argc, char **argv) {
  int sockfd;
  int portno;
  unsigned int clientlen;
  struct sockaddr_in serveraddr;
  struct sockaddr_in clientaddr;
  char buf[1024];
  char *hostaddrp;
  int optval;
  int n;
  if (argc != 2) {
    fprintf(stderr, "usage: %s <port>\n", argv[0]);
    exit(1);
  }
  portno = atoi(argv[1]);
  sockfd = socket(2, SOCK_DGRAM, 0);
  if (sockfd < 0)
    error("ERROR opening socket");
  optval = 1;
  setsockopt(sockfd, 1, 2,
      (const void *)&optval , sizeof(int));
  bzero((char *) &serveraddr, sizeof(serveraddr));
  serveraddr.sin_family = 2;
  serveraddr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
  serveraddr.sin_port = htons((unsigned short)portno);
  if (bind(sockfd, (struct sockaddr *) &serveraddr,
    sizeof(serveraddr)) < 0)
    error("ERROR on binding");
  clientlen = sizeof(clientaddr);
  while (1) {
    bzero(buf, 1024);
    n = recvfrom(sockfd, buf, 1024, 0,
   (struct sockaddr *) &clientaddr, &clientlen);
    if (n < 0)
      error("ERROR in recvfrom");
    hostaddrp = inet_ntoa(clientaddr.sin_addr);
    if (hostaddrp == ((void *)0))
      error("ERROR on inet_ntoa\n");
    printf("server received datagram from %s\n", hostaddrp);
    n = sendto(sockfd, buf, strlen(buf), 0,
        (struct sockaddr *) &clientaddr, clientlen);
    if (n < 0)
      error("ERROR in sendto");
  }
}

canonical_original_code:
int main(int argc, char **argv) {
  int sockfd;
  int portno;
  unsigned int clientlen;
  struct sockaddr_in serveraddr;
  struct sockaddr_in clientaddr;
  char buf[1024];
  char *hostaddrp;
  int optval;
  int n;
  if (argc != 2) {
    fprintf(stderr, "usage: %s <port>\n", argv[0]);
    exit(1);
  }
  portno = atoi(argv[1]);
  sockfd = socket(2, SOCK_DGRAM, 0);
  if (sockfd < 0)
    error("ERROR opening socket");
  optval = 1;
  setsockopt(sockfd, 1, 2,
      (void *)&optval , sizeof(int));
  bzero((char *) &serveraddr, sizeof(serveraddr));
  serveraddr.sin_family = 2;
  serveraddr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
  serveraddr.sin_port = htons((unsigned short)portno);
  if (bind(sockfd, (struct sockaddr *) &serveraddr,
    sizeof(serveraddr)) < 0)
    error("ERROR on binding");
  clientlen = sizeof(clientaddr);
  while (1) {
    bzero(buf, 1024);
    n = recvfrom(sockfd, buf, 1024, 0,
   (struct sockaddr *) &clientaddr, &clientlen);
    if (n < 0)
      error("ERROR in recvfrom");
    hostaddrp = inet_ntoa(clientaddr.sin_addr);
    if (hostaddrp == ((void *)0))
      error("ERROR on inet_ntoa\n");
    printf("server received datagram from %s\n", hostaddrp);
    n = sendto(sockfd, buf, strlen(buf), 0,
        (struct sockaddr *) &clientaddr, clientlen);
    if (n < 0)
      error("ERROR in sendto");
  }
}

variable_types:
argv
char **

variable_types: portno int

variable_types:
clientlen
unsigned int

variable_types:
buf
char [1024]

variable_types:
hostaddrp
char *

variable_types: optval int

variable_types: n int


decompiled_code:
__int64 __fastcall buffer_push(__int64 a1, const char *a2)
{
pthread_mutex_t *v2; // r13
int v3; // er12
int v4; // eax
_QWORD *v5; // rbp
char *v6; // rax
size_t v7; // rax
__int64 v8; // rax
bool v9; // zf
__int64 result; // rax

v2 = (pthread_mutex_t *)(a1 + 48);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 48));
v3 = *(_DWORD *)(a1 + 8);
v4 = *(_DWORD *)(a1 + 12);
if ( v3 == v4 && v4 )
{
pthread_mutex_unlock(v2);
++*(_QWORD *)(a1 + 16);
*_errno_location() = 105;
result = 0xFFFFFFFFLL;
}
else
{
v5 = malloc(0x20uLL);
if ( v5 )
{
v6 = strdup(a2);
*v5 = v6;
v7 = strlen(v6);
v5[3] = 0LL;
v5[1] = v7;
v8 = *(_QWORD *)(a1 + 96);
v5[2] = v8;
if ( v8 )
*(_QWORD *)(v8 + 24) = v5;
v9 = *(_QWORD *)(a1 + 88) == 0LL;
*(_QWORD *)(a1 + 96) = v5;
if ( v9 )
*(_QWORD *)(a1 + 88) = v5;
++*(_QWORD *)(a1 + 24);
*(_DWORD *)(a1 + 8) = v3 + 1;
pthread_mutex_unlock(v2);
result = 0LL;
}
else
{
pthread_mutex_unlock(v2);
++*(_QWORD *)(a1 + 16);
*_errno_location() = 12;
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, const char *a2)
{
pthread_mutex_t *v2; // r13
int v3; // er12
int v4; // eax
_QWORD *v5; // rbp
char *v6; // rax
size_t v7; // rax
__int64 v8; // rax
bool v9; // zf
__int64 result; // rax

v2 = (pthread_mutex_t *)(a1 + 48);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 48));
v3 = *(_DWORD *)(a1 + 8);
v4 = *(_DWORD *)(a1 + 12);
if ( v3 == v4 && v4 )
{
pthread_mutex_unlock(v2);
++*(_QWORD *)(a1 + 16);
*_errno_location() = 105;
result = 0xFFFFFFFFLL;
}
else
{
v5 = malloc(0x20uLL);
if ( v5 )
{
v6 = strdup(a2);
*v5 = v6;
v7 = strlen(v6);
v5[3] = 0LL;
v5[1] = v7;
v8 = *(_QWORD *)(a1 + 96);
v5[2] = v8;
if ( v8 )
*(_QWORD *)(v8 + 24) = v5;
v9 = *(_QWORD *)(a1 + 88) == 0LL;
*(_QWORD *)(a1 + 96) = v5;
if ( v9 )
*(_QWORD *)(a1 + 88) = v5;
++*(_QWORD *)(a1 + 24);
*(_DWORD *)(a1 + 8) = v3 + 1;
pthread_mutex_unlock(v2);
result = 0LL;
}
else
{
pthread_mutex_unlock(v2);
++*(_QWORD *)(a1 + 16);
*_errno_location() = 12;
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int buffer_push(buffer_t*buffer,const char*chunk_buffer) {
 buffer_chunk_t*bc_new;
 pthread_mutex_lock(&(buffer->mutex));
 if(buffer->nb_chunk == buffer->nb_chunk_max && buffer->nb_chunk_max != 0) {
  pthread_mutex_unlock(&(buffer->mutex));
  buffer->discarded++;
  (*__errno_location ())=105;
  return -1;
 }
 bc_new=malloc(sizeof(buffer_chunk_t));
 if(bc_new == ((void *)0)) {
  pthread_mutex_unlock(&(buffer->mutex));
  buffer->discarded++;
  (*__errno_location ())=12;
  return -1;
 }
 bc_new->buffer=strdup(chunk_buffer);
 bc_new->nb_chars=strlen(bc_new->buffer);
 bc_new->next=((void *)0);
 bc_new->prev=buffer->last;
 if(buffer->last != ((void *)0)) {
  buffer->last->next=bc_new;
 }
 buffer->last=bc_new;
 if(buffer->first == ((void *)0)) {
  buffer->first = buffer->last;
 }
 buffer->nb_chunk++;
 buffer->added++;
 pthread_mutex_unlock(&(buffer->mutex));
 return 0;
}

canonical_original_code:
int buffer_push(struct buffer *buffer, char *chunk_buffer) {
 struct buffer_chunk *bc_new;
 pthread_mutex_lock(&(buffer->mutex));
 if(buffer->nb_chunk == buffer->nb_chunk_max && buffer->nb_chunk_max != 0) {
  pthread_mutex_unlock(&(buffer->mutex));
  buffer->discarded++;
  (*__errno_location ())=105;
  return -1;
 }
 bc_new=malloc(sizeof(buffer_chunk_t));
 if(bc_new == ((void *)0)) {
  pthread_mutex_unlock(&(buffer->mutex));
  buffer->discarded++;
  (*__errno_location ())=12;
  return -1;
 }
 bc_new->buffer=strdup(chunk_buffer);
 bc_new->nb_chars=strlen(bc_new->buffer);
 bc_new->next=((void *)0);
 bc_new->prev=buffer->last;
 if(buffer->last != ((void *)0)) {
  buffer->last->next=bc_new;
 }
 buffer->last=bc_new;
 if(buffer->first == ((void *)0)) {
  buffer->first = buffer->last;
 }
 buffer->nb_chunk++;
 buffer->added++;
 pthread_mutex_unlock(&(buffer->mutex));
 return 0;
}

variable_types: struct buffer { char *separator; unsigned int nb_chunk; unsigned int nb_chunk_max; unsigned long long discarded; unsigned long long added; unsigned long long deleted; char *incomplete_chunk; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; struct buffer_chunk { char *buffer; long nb_chars; struct buffer_chunk *prev; struct buffer_chunk *next; } *first; struct buffer_chunk *last; } *

user_defined_types: struct buffer { char *separator; unsigned int nb_chunk; unsigned int nb_chunk_max; unsigned long long discarded; unsigned long long added; unsigned long long deleted; char *incomplete_chunk; union pthread_mutex_t mutex; struct buffer_chunk *first; struct buffer_chunk *last; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
__int64 __fastcall buffer_push_vprintf(__int64 a1, const char *a2, void *a3)
{
unsigned int v4; // er12
char *v5; // rax
char *v6; // rbp
int v7; // er13

v4 = vsnprintf(0LL, 0LL, a2, a3);
v5 = (char *)malloc((int)(v4 + 1));
if ( v5 )
{
v6 = v5;
vsprintf(v5, a2, a3);
v7 = buffer_push(a1, v6);
free(v6);
if ( v7 == -1 )
v4 = -1;
}
else
{
v4 = -1;
*_errno_location() = 12;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, const char *a2, void *a3)
{
unsigned int v4; // er12
char *v5; // rax
char *v6; // rbp
int v7; // er13

v4 = vsnprintf(0LL, 0LL, a2, a3);
v5 = (char *)malloc((int)(v4 + 1));
if ( v5 )
{
v6 = v5;
vsprintf(v5, a2, a3);
v7 = func1(a1, v6);
free(v6);
if ( v7 == -1 )
v4 = -1;
}
else
{
v4 = -1;
*_errno_location() = 12;
}
return v4;
}

original_code:
int buffer_push_vprintf(buffer_t*buffer,const char*format, va_list va) {
 char*chunk_buffer;
 int nb_chars;
 int result;
 nb_chars=vsnprintf(chunk_buffer,0,format,va);
 chunk_buffer=malloc((nb_chars+1)*sizeof(char));
 if(chunk_buffer == ((void *)0)) {
  (*__errno_location ())=12;
  return -1;
 }
 vsprintf(chunk_buffer,format,va);
 result = buffer_push(buffer,chunk_buffer);
 free(chunk_buffer);
 chunk_buffer=((void *)0);
 if(result == -1) {
  return -1;
 }
 return nb_chars;
}

canonical_original_code:
int buffer_push_vprintf(struct buffer *buffer, char *format, __builtin_va_list va) {
 char *chunk_buffer;
 int nb_chars;
 int result;
 nb_chars=vsnprintf(chunk_buffer,0,format,va);
 chunk_buffer=malloc((nb_chars+1)*sizeof(char));
 if(chunk_buffer == ((void *)0)) {
  (*__errno_location ())=12;
  return -1;
 }
 vsprintf(chunk_buffer,format,va);
 result = buffer_push(buffer,chunk_buffer);
 free(chunk_buffer);
 chunk_buffer=((void *)0);
 if(result == -1) {
  return -1;
 }
 return nb_chars;
}

variable_types:
format
char *

user_defined_types: struct buffer_chunk { char *buffer; long nb_chars; struct buffer_chunk *prev; struct buffer_chunk *next; }

user_defined_types: struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; }


decompiled_code:
__int64 buffer_push_printf(__int64 a1, const char *a2, ...)
{
unsigned int v2; // er12
char *v3; // rax
char *v4; // rbp
int v5; // er13
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF

va_start(arg, a2);
v2 = vsnprintf(0LL, 0LL, a2, arg);
v3 = (char *)malloc((int)(v2 + 1));
v4 = v3;
if ( v3 )
{
va_end(arg);
va_start(arg, a2);
vsprintf(v3, a2, arg);
v5 = buffer_push(a1, v4);
free(v4);
if ( v5 == -1 )
v2 = -1;
}
else
{
v2 = -1;
*_errno_location() = 12;
}
return v2;
}

canonical_decompiled_code:
__int64 func3(__int64 a1, const char *a2, ...)
{
unsigned int v2; // er12
char *v3; // rax
char *v4; // rbp
int v5; // er13
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF

va_start(arg, a2);
v2 = vsnprintf(0LL, 0LL, a2, arg);
v3 = (char *)malloc((int)(v2 + 1));
v4 = v3;
if ( v3 )
{
va_end(arg);
va_start(arg, a2);
vsprintf(v3, a2, arg);
v5 = func1(a1, v4);
free(v4);
if ( v5 == -1 )
v2 = -1;
}
else
{
v2 = -1;
*_errno_location() = 12;
}
return v2;
}

original_code:
int buffer_push_printf(buffer_t*buffer,const char*format, ...) {
 char*chunk_buffer;
 va_list va;
 int nb_chars;
 int result;
 __builtin_va_start(va,format);
 nb_chars=vsnprintf(chunk_buffer,0,format,va);
 __builtin_va_end(va);
 chunk_buffer=malloc((nb_chars+1)*sizeof(char));
 if(chunk_buffer == ((void *)0)) {
  (*__errno_location ())=12;
  return -1;
 }
 __builtin_va_start(va,format);
 vsprintf(chunk_buffer,format,va);
 __builtin_va_end(va);
 result = buffer_push(buffer,chunk_buffer);
 free(chunk_buffer);
 chunk_buffer=((void *)0);
 if(result == -1) {
  return -1;
 }
 return nb_chars;
}

canonical_original_code:
int buffer_push_printf(struct buffer *buffer, char *format, ...) {
 char *chunk_buffer;
 __builtin_va_list va;
 int nb_chars;
 int result;
 __builtin_va_start(va,format);
 nb_chars=vsnprintf(chunk_buffer,0,format,va);
 __builtin_va_end(va);
 chunk_buffer=malloc((nb_chars+1)*sizeof(char));
 if(chunk_buffer == ((void *)0)) {
  (*__errno_location ())=12;
  return -1;
 }
 __builtin_va_start(va,format);
 vsprintf(chunk_buffer,format,va);
 __builtin_va_end(va);
 result = buffer_push(buffer,chunk_buffer);
 free(chunk_buffer);
 chunk_buffer=((void *)0);
 if(result == -1) {
  return -1;
 }
 return nb_chars;
}


decompiled_code:
__int64 __fastcall buffer_push_vnprintf(__int64 a1, signed int a2, const char *a3, void *a4)
{
unsigned int v6; // er12
char *v7; // rax
char *v8; // rbp
int v9; // er13

v6 = vsnprintf(0LL, 0LL, a3, a4);
if ( (int)(v6 - 1) > a2 )
{
v6 = -1;
*_errno_location() = 36;
}
else
{
v7 = (char *)malloc((int)(v6 + 1));
v8 = v7;
if ( v7 )
{
vsprintf(v7, a3, a4);
v9 = buffer_push(a1, v8);
free(v8);
if ( v9 == -1 )
v6 = -1;
}
else
{
v6 = -1;
*_errno_location() = 12;
}
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1, signed int a2, const char *a3, void *a4)
{
unsigned int v6; // er12
char *v7; // rax
char *v8; // rbp
int v9; // er13

v6 = vsnprintf(0LL, 0LL, a3, a4);
if ( (int)(v6 - 1) > a2 )
{
v6 = -1;
*_errno_location() = 36;
}
else
{
v7 = (char *)malloc((int)(v6 + 1));
v8 = v7;
if ( v7 )
{
vsprintf(v7, a3, a4);
v9 = func1(a1, v8);
free(v8);
if ( v9 == -1 )
v6 = -1;
}
else
{
v6 = -1;
*_errno_location() = 12;
}
}
return v6;
}

original_code:
int buffer_push_vnprintf(buffer_t*buffer,size_t size, const char*format, va_list va) {
 char*chunk_buffer;
 int nb_chars;
 int result;
 nb_chars=vsnprintf(chunk_buffer,0,format,va);
 if((nb_chars-1) > (int)size) {
  (*__errno_location ())=36;
  return -1;
 }
 chunk_buffer=malloc((nb_chars+1)*sizeof(char));
 if(chunk_buffer == ((void *)0)) {
  (*__errno_location ())=12;
  return -1;
 }
 vsprintf(chunk_buffer,format,va);
 result = buffer_push(buffer,chunk_buffer);
 free(chunk_buffer);
 chunk_buffer=((void *)0);
 if(result == -1) {
  return -1;
 }
 return nb_chars;
}

canonical_original_code:
int buffer_push_vnprintf(struct buffer *buffer, unsigned long size, char *format, __builtin_va_list va) {
 char *chunk_buffer;
 int nb_chars;
 int result;
 nb_chars=vsnprintf(chunk_buffer,0,format,va);
 if((nb_chars-1) > (int)size) {
  (*__errno_location ())=36;
  return -1;
 }
 chunk_buffer=malloc((nb_chars+1)*sizeof(char));
 if(chunk_buffer == ((void *)0)) {
  (*__errno_location ())=12;
  return -1;
 }
 vsprintf(chunk_buffer,format,va);
 result = buffer_push(buffer,chunk_buffer);
 free(chunk_buffer);
 chunk_buffer=((void *)0);
 if(result == -1) {
  return -1;
 }
 return nb_chars;
}

variable_types:
size
unsigned long


decompiled_code:
__int64 buffer_push_nprintf(__int64 a1, signed int a2, const char *a3, ...)
{
unsigned int v4; // er12
char *v5; // rax
char *v6; // rbp
int v7; // er13
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF

va_start(arg, a3);
v4 = vsnprintf(0LL, 0LL, a3, arg);
if ( (int)(v4 - 1) > a2 )
{
v4 = -1;
*_errno_location() = 36;
}
else
{
v5 = (char *)malloc((int)(v4 + 1));
v6 = v5;
if ( v5 )
{
va_end(arg);
va_start(arg, a3);
vsprintf(v5, a3, arg);
v7 = buffer_push(a1, v6);
free(v6);
if ( v7 == -1 )
v4 = -1;
}
else
{
v4 = -1;
*_errno_location() = 12;
}
}
return v4;
}

canonical_decompiled_code:
__int64 func5(__int64 a1, signed int a2, const char *a3, ...)
{
unsigned int v4; // er12
char *v5; // rax
char *v6; // rbp
int v7; // er13
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF

va_start(arg, a3);
v4 = vsnprintf(0LL, 0LL, a3, arg);
if ( (int)(v4 - 1) > a2 )
{
v4 = -1;
*_errno_location() = 36;
}
else
{
v5 = (char *)malloc((int)(v4 + 1));
v6 = v5;
if ( v5 )
{
va_end(arg);
va_start(arg, a3);
vsprintf(v5, a3, arg);
v7 = func1(a1, v6);
free(v6);
if ( v7 == -1 )
v4 = -1;
}
else
{
v4 = -1;
*_errno_location() = 12;
}
}
return v4;
}

original_code:
int buffer_push_nprintf(buffer_t*buffer,size_t size, const char*format, ...) {
 char*chunk_buffer;
 va_list va;
 int nb_chars;
 int result;
 __builtin_va_start(va,format);
 nb_chars=vsnprintf(chunk_buffer,0,format,va);
 __builtin_va_end(va);
 if((nb_chars-1) > (int)size) {
  (*__errno_location ())=36;
  return -1;
 }
 chunk_buffer=malloc((nb_chars+1)*sizeof(char));
 if(chunk_buffer == ((void *)0)) {
  (*__errno_location ())=12;
  return -1;
 }
 __builtin_va_start(va,format);
 vsprintf(chunk_buffer,format,va);
 __builtin_va_end(va);
 result = buffer_push(buffer,chunk_buffer);
 free(chunk_buffer);
 chunk_buffer=((void *)0);
 if(result == -1) {
  return -1;
 }
 return nb_chars;
}

canonical_original_code:
int buffer_push_nprintf(struct buffer *buffer, unsigned long size, char *format, ...) {
 char *chunk_buffer;
 __builtin_va_list va;
 int nb_chars;
 int result;
 __builtin_va_start(va,format);
 nb_chars=vsnprintf(chunk_buffer,0,format,va);
 __builtin_va_end(va);
 if((nb_chars-1) > (int)size) {
  (*__errno_location ())=36;
  return -1;
 }
 chunk_buffer=malloc((nb_chars+1)*sizeof(char));
 if(chunk_buffer == ((void *)0)) {
  (*__errno_location ())=12;
  return -1;
 }
 __builtin_va_start(va,format);
 vsprintf(chunk_buffer,format,va);
 __builtin_va_end(va);
 result = buffer_push(buffer,chunk_buffer);
 free(chunk_buffer);
 chunk_buffer=((void *)0);
 if(result == -1) {
  return -1;
 }
 return nb_chars;
}


original_code:
int buffer_destroy(buffer_t**buffer) {
 buffer_chunk_t*bc_temp;
 if(*buffer == ((void *)0)) {
  return 0;
 }
 bc_temp=(**buffer).first;
 while(bc_temp != ((void *)0)) {
  free(bc_temp->buffer);
  bc_temp->buffer=((void *)0);
  if(bc_temp->next == ((void *)0)) {
   free(bc_temp);
   bc_temp=((void *)0);
  } else {
   bc_temp=bc_temp->next;
   free(bc_temp->prev);
   bc_temp->prev=((void *)0);
  }
 }
 if((**buffer).incomplete_chunk != ((void *)0)) {
  free((**buffer).incomplete_chunk);
  (**buffer).incomplete_chunk=((void *)0);
 }
 if((**buffer).separator != ((void *)0)) {
  free((**buffer).separator);
  (**buffer).separator=((void *)0);
 }
 free(*buffer);
 *buffer=((void *)0);
 return 0;
}

canonical_original_code:
int buffer_destroy(struct buffer **buffer) {
 struct buffer_chunk *bc_temp;
 if(*buffer == ((void *)0)) {
  return 0;
 }
 bc_temp=(**buffer).first;
 while(bc_temp != ((void *)0)) {
  free(bc_temp->buffer);
  bc_temp->buffer=((void *)0);
  if(bc_temp->next == ((void *)0)) {
   free(bc_temp);
   bc_temp=((void *)0);
  } else {
   bc_temp=bc_temp->next;
   free(bc_temp->prev);
   bc_temp->prev=((void *)0);
  }
 }
 if((**buffer).incomplete_chunk != ((void *)0)) {
  free((**buffer).incomplete_chunk);
  (**buffer).incomplete_chunk=((void *)0);
 }
 if((**buffer).separator != ((void *)0)) {
  free((**buffer).separator);
  (**buffer).separator=((void *)0);
 }
 free(*buffer);
 *buffer=((void *)0);
 return 0;
}

variable_types: struct buffer { char *separator; unsigned int nb_chunk; unsigned int nb_chunk_max; unsigned long long discarded; unsigned long long added; unsigned long long deleted; char *incomplete_chunk; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; struct buffer_chunk { char *buffer; long nb_chars; struct buffer_chunk *prev; struct buffer_chunk *next; } *first; struct buffer_chunk *last; } **


decompiled_code:
__sighandler_t __fastcall signal_ignore(int a1)
{
return signal(a1, (__sighandler_t)((char *)buffer_shift + 1));
}

canonical_decompiled_code:
__sighandler_t __fastcall func7(int a1)
{
return signal(a1, (__sighandler_t)((char *)buffer_shift + 1));
}

original_code:
void signal_ignore(int signo) {
 signal(signo,((__sighandler_t) 1));
}

canonical_original_code:
void signal_ignore(int signo) {
 signal(signo,((void (*)(int)) 1));
}

variable_types: signo int


decompiled_code:
__sighandler_t __fastcall signal_reset(int a1)
{
return signal(a1, 0LL);
}

canonical_decompiled_code:
__sighandler_t __fastcall func8(int a1)
{
return signal(a1, 0LL);
}

original_code:
void signal_reset(int signo) {
 signal(signo,((__sighandler_t) 0));
}

canonical_original_code:
void signal_reset(int signo) {
 signal(signo,((void (*)(int)) 0));
}


decompiled_code:
__int64 __fastcall ss_sleep(unsigned int a1)
{
unsigned int v1; // eax
unsigned int v2; // edi

v1 = sleep(a1);
if ( v1 )
{
v2 = v1;
do
v2 = sleep(v2);
while ( v2 );
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func9(unsigned int a1)
{
unsigned int v1; // eax
unsigned int v2; // edi

v1 = sleep(a1);
if ( v1 )
{
v2 = v1;
do
v2 = sleep(v2);
while ( v2 );
}
return 0LL;
}

original_code:
unsigned int ss_sleep(unsigned int seconds) {
 unsigned int result;
 result = sleep(seconds);
 while(result != 0) {
  result = sleep(result);
 }
 return 0;
}

variable_types:
seconds
unsigned int

variable_types:
result
unsigned int

return_type: unsigned int


decompiled_code:
ssize_t __fastcall ss_write(int fd, void *buf, size_t n)
{
ssize_t v4; // r14
int *v5; // r13
ssize_t result; // rax

v4 = write(fd, buf, n);
if ( v4 == -1 )
{
v5 = _errno_location();
while ( *v5 == 4 )
{
puts("Ecriture interrompue, on reprend");
result = write(fd, buf, n);
if ( result != -1 )
return result;
}
}
return v4;
}

canonical_decompiled_code:
ssize_t __fastcall func10(int fd, void *buf, size_t n)
{
ssize_t v4; // r14
int *v5; // r13
ssize_t result; // rax

v4 = write(fd, buf, n);
if ( v4 == -1 )
{
v5 = _errno_location();
while ( *v5 == 4 )
{
puts("Ecriture interrompue, on reprend");
result = write(fd, buf, n);
if ( result != -1 )
return result;
}
}
return v4;
}

original_code:
ssize_t ss_write(int fildes, const void *buf, size_t nbyte) {
 ssize_t result;
 result = write(fildes,buf,nbyte);
 while(result == -1 && (*__errno_location ()) == 4) {
  printf("Ecriture interrompue, on reprend\n");
  result = write(fildes,buf,nbyte);
 }
 return result;
}

canonical_original_code:
long ss_write(int fildes, void *buf, unsigned long nbyte) {
 long result;
 result = write(fildes,buf,nbyte);
 while(result == -1 && (*__errno_location ()) == 4) {
  printf("Ecriture interrompue, on reprend\n");
  result = write(fildes,buf,nbyte);
 }
 return result;
}

variable_types: fildes int

variable_types:
buf
void *

variable_types:
nbyte
unsigned long

variable_types: result long

return_type: long


decompiled_code:
ssize_t __fastcall ss_read(int fd, void *buf, size_t nbytes)
{
ssize_t v4; // r14
int *v5; // r13
ssize_t result; // rax

v4 = read(fd, buf, nbytes);
if ( v4 == -1 )
{
v5 = _errno_location();
while ( *v5 == 4 )
{
puts("Lecture interrompue, on reprend");
result = read(fd, buf, nbytes);
if ( result != -1 )
return result;
}
}
return v4;
}

canonical_decompiled_code:
ssize_t __fastcall func11(int fd, void *buf, size_t nbytes)
{
ssize_t v4; // r14
int *v5; // r13
ssize_t result; // rax

v4 = read(fd, buf, nbytes);
if ( v4 == -1 )
{
v5 = _errno_location();
while ( *v5 == 4 )
{
puts("Lecture interrompue, on reprend");
result = read(fd, buf, nbytes);
if ( result != -1 )
return result;
}
}
return v4;
}

original_code:
ssize_t ss_read(int fildes, void *buf, size_t nbyte) {
 ssize_t result;
 result = read(fildes,buf,nbyte);
 while(result == -1 && (*__errno_location ()) == 4) {
  printf("Lecture interrompue, on reprend\n");
  result = read(fildes,buf,nbyte);
 }
 return result;
}

canonical_original_code:
long ss_read(int fildes, void *buf, unsigned long nbyte) {
 long result;
 result = read(fildes,buf,nbyte);
 while(result == -1 && (*__errno_location ()) == 4) {
  printf("Lecture interrompue, on reprend\n");
  result = read(fildes,buf,nbyte);
 }
 return result;
}


timers

decompiled_code:
__time_t __fastcall timers(int a1, unsigned __int16 a2)
{
__time_t result; // rax
struct timeval v3; // [rsp+0h] [rbp-18h] BYREF

if ( first_time_2 == 1 )
{
first_time_2 = 0;
memset(timers_status_1, 0, sizeof(timers_status_1));
}
if ( a2 > 0x1FFu )
{
*_errno_location() = 22;
result = 0LL;
}
else if ( a1 )
{
if ( a1 == 1 )
{
result = 0LL;
if ( timers_status_1[a2] )
{
gettimeofday(&v3, 0LL);
result = v3.tv_sec - timers_0[2 * a2] - (v3.tv_usec < qword_988[2 * a2]);
}
}
else
{
result = 0LL;
}
}
else
{
gettimeofday((struct timeval *)&timers_0[2 * a2], 0LL);
timers_status_1[a2] = 1;
result = timers_0[2 * a2];
}
return result;
}

canonical_decompiled_code:
__time_t __fastcall func12(int a1, unsigned __int16 a2)
{
__time_t result; // rax
struct timeval v3; // [rsp+0h] [rbp-18h] BYREF

if ( first_time_2 == 1 )
{
first_time_2 = 0;
memset(timers_status_1, 0, sizeof(timers_status_1));
}
if ( a2 > 0x1FFu )
{
*_errno_location() = 22;
result = 0LL;
}
else if ( a1 )
{
if ( a1 == 1 )
{
result = 0LL;
if ( timers_status_1[a2] )
{
gettimeofday(&v3, 0LL);
result = v3.tv_sec - timers_0[2 * a2] - (v3.tv_usec < qword_988[2 * a2]);
}
}
else
{
result = 0LL;
}
}
else
{
gettimeofday((struct timeval *)&timers_0[2 * a2], 0LL);
timers_status_1[a2] = 1;
result = timers_0[2 * a2];
}
return result;
}

original_code:
struct timeval timers(timer_action_t action, unsigned short int index) {
 static struct timeval timers[512];
 static timer_status_t timers_status[512];
 static bool_t first_time = TRUE;
 unsigned short int i;
 struct timeval tv;
 struct timeval result;
 result.tv_sec=0;
 result.tv_usec=0;
 if(first_time == TRUE) {
  for(i=0;i<512;i++) {
   timers_status[i]=UNUSED;
  }
  first_time = FALSE;
 }
 if(index > 512 -1) {
  (*__errno_location ())=22;
  return result;
 }
 switch(action) {
  case RESET:
   gettimeofday(&timers[index],((void *)0));
   timers_status[index]=INUSE;
   result.tv_sec=timers[index].tv_sec;
   result.tv_usec=timers[index].tv_usec;
   break;
  case GET:
   if(timers_status[index] == UNUSED) {
    result.tv_sec=0;
    result.tv_usec=0;
   } else {
    gettimeofday(&tv,((void *)0));
    if(tv.tv_usec < timers[index].tv_usec) {
     result.tv_usec=tv.tv_usec+1000000-timers[index].tv_usec;
     i=1;
    } else {
     result.tv_usec=tv.tv_usec-timers[index].tv_usec;
     i=0;
    }
    result.tv_sec=tv.tv_sec-timers[index].tv_sec-i;
   }
   break;
 }
 return result;
}

canonical_original_code:
struct timeval timers(enum timer_action_t action, unsigned short index) {
 static struct timeval timers[512];
 static enum timer_status_t timers_status[512];
 static enum bool_t first_time = TRUE;
 unsigned short i;
 struct timeval tv;
 struct timeval result;
 result.tv_sec=0;
 result.tv_usec=0;
 if(first_time == TRUE) {
  for(i=0;i<512;i++) {
   timers_status[i]=UNUSED;
  }
  first_time = FALSE;
 }
 if(index > 512 -1) {
  (*__errno_location ())=22;
  return result;
 }
 switch(action) {
  case RESET:
   gettimeofday(&timers[index],((void *)0));
   timers_status[index]=INUSE;
   result.tv_sec=timers[index].tv_sec;
   result.tv_usec=timers[index].tv_usec;
   break;
  case GET:
   if(timers_status[index] == UNUSED) {
    result.tv_sec=0;
    result.tv_usec=0;
   } else {
    gettimeofday(&tv,((void *)0));
    if(tv.tv_usec < timers[index].tv_usec) {
     result.tv_usec=tv.tv_usec+1000000-timers[index].tv_usec;
     i=1;
    } else {
     result.tv_usec=tv.tv_usec-timers[index].tv_usec;
     i=0;
    }
    result.tv_sec=tv.tv_sec-timers[index].tv_sec-i;
   }
   break;
 }
 return result;
}

variable_types:
action
enum timer_action_t { RESET=0, GET=1 }

variable_types:
index
unsigned short

variable_types: struct timeval { long tv_sec; long tv_usec; } [512]

variable_types: enum timer_status_t { UNUSED=0, INUSE=1 } [512]

variable_types:
i
unsigned short


decompiled_code:
__int64 __fastcall a2open(const char *a1)
{
int v1; // eax
unsigned int v2; // er12
void *v3; // rdx
int v4; // eax
int v6; // edi
int v7; // edi
int v8; // edi
struct sockaddr v9; // [rsp+0h] [rbp-28h] BYREF

v1 = socket(2, 1, 6);
if ( v1 < 0 )
{
v2 = -1;
perror("Cannot create socket");
}
else
{
v2 = v1;
v9 = 0LL;
v9.sa_family = 2;
*(_WORD *)v9.sa_data = _bswap_16_constprop_0(2LL, a1, &v9.sa_data[2]);
v4 = inet_pton(2, a1, v3);
if ( v4 < 0 )
{
perror("First parameter is not a valid address family");
v7 = v2;
v2 = -1;
close(v7);
}
else if ( v4 )
{
if ( connect(v2, &v9, 0x10u) < 0 )
{
perror("Connect failed");
v8 = v2;
v2 = -1;
close(v8);
}
}
else
{
perror("Char string (second parameter does not contain valid ipaddress)");
v6 = v2;
v2 = -1;
close(v6);
}
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(const char *a1)
{
int v1; // eax
unsigned int v2; // er12
void *v3; // rdx
int v4; // eax
int v6; // edi
int v7; // edi
int v8; // edi
struct sockaddr v9; // [rsp+0h] [rbp-28h] BYREF

v1 = socket(2, 1, 6);
if ( v1 < 0 )
{
v2 = -1;
perror("Cannot create socket");
}
else
{
v2 = v1;
v9 = 0LL;
v9.sa_family = 2;
*(_WORD *)v9.sa_data = func7(2LL, a1, &v9.sa_data[2]);
v4 = inet_pton(2, a1, v3);
if ( v4 < 0 )
{
perror("First parameter is not a valid address family");
v7 = v2;
v2 = -1;
close(v7);
}
else if ( v4 )
{
if ( connect(v2, &v9, 0x10u) < 0 )
{
perror("Connect failed");
v8 = v2;
v2 = -1;
close(v8);
}
}
else
{
perror("Char string (second parameter does not contain valid ipaddress)");
v6 = v2;
v2 = -1;
close(v6);
}
}
return v2;
}

original_code:
int a2open(char *ipaddr)
{
    struct sockaddr_in piaddr;
    int res;
    int fd = socket(2, SOCK_STREAM, IPPROTO_TCP);
    if (fd < 0)
    {
        perror("Cannot create socket");
        return -1;
    }
    memset(&piaddr, 0, sizeof(piaddr));
    piaddr.sin_family = 2;
    piaddr.sin_port = htons(6551);
    res = inet_pton(2, ipaddr, &piaddr.sin_addr);
    if (res < 0)
    {
        perror("First parameter is not a valid address family");
        close(fd);
        return -1;
    }
    else if (res == 0)
    {
        perror("Char string (second parameter does not contain valid ipaddress)");
        close(fd);
        return -1;
    }
    if (connect(fd, (struct sockaddr *)&piaddr, sizeof(piaddr)) < 0)
    {
        perror("Connect failed");
        close(fd);
        return -1;
    }
    return fd;
}

variable_types:
ipaddr
char *

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

variable_types: res int

variable_types: fd int


decompiled_code:
int __fastcall a2close(int fd)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

buf[0] = -1;
write(fd, buf, 1uLL);
shutdown(fd, 2);
return close(fd);
}

canonical_decompiled_code:
int __fastcall func9(int fd)
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

buf[0] = -1;
write(fd, buf, 1uLL);
shutdown(fd, 2);
return close(fd);
}

original_code:
void a2close(int fd)
{
    char closepkt;
    closepkt = 0xFF;
    write(fd, &closepkt, 1);
    shutdown(fd, SHUT_RDWR);
    close(fd);
}

variable_types: closepkt char


decompiled_code:
_BOOL8 __fastcall a2read(int fd, __int16 a2, int a3, void *a4)
{
char buf; // [rsp+8h] [rbp-20h] BYREF
__int16 v8; // [rsp+9h] [rbp-1Fh]
__int16 v9; // [rsp+Bh] [rbp-1Dh]

v8 = a2;
v9 = a3;
buf = -112;
write(fd, &buf, 5uLL);
read(fd, a4, a3);
read(fd, &buf, 2uLL);
return buf == -98;
}

canonical_decompiled_code:
_BOOL8 __fastcall func10(int fd, __int16 a2, int a3, void *a4)
{
char buf; // [rsp+8h] [rbp-20h] BYREF
__int16 v8; // [rsp+9h] [rbp-1Fh]
__int16 v9; // [rsp+Bh] [rbp-1Dh]

v8 = a2;
v9 = a3;
buf = -112;
write(fd, &buf, 5uLL);
read(fd, a4, a3);
read(fd, &buf, 2uLL);
return buf == -98;
}

original_code:
int a2read(int fd, int address, int count, char *buffer)
{
    char readpkt[8];
    readpkt[0] = 0x90;
    readpkt[1] = address;
    readpkt[2] = address >> 8;
    readpkt[3] = count;
    readpkt[4] = count >> 8;
    write(fd, readpkt, 5);
    read(fd, buffer, count);
    read(fd, readpkt, 2);
    return ((unsigned char)readpkt[0] == 0x9E);
}

variable_types: address int

variable_types: count int

variable_types:
buffer
char *

variable_types:
readpkt
char [8]


decompiled_code:
_BOOL8 __fastcall a2write(int fd, __int16 a2, int a3, const void *a4)
{
char buf; // [rsp+8h] [rbp-20h] BYREF
__int16 v8; // [rsp+9h] [rbp-1Fh]
__int16 v9; // [rsp+Bh] [rbp-1Dh]

v8 = a2;
v9 = a3;
buf = -110;
write(fd, &buf, 5uLL);
write(fd, a4, a3);
read(fd, &buf, 2uLL);
return buf == -98;
}

canonical_decompiled_code:
_BOOL8 __fastcall func11(int fd, __int16 a2, int a3, const void *a4)
{
char buf; // [rsp+8h] [rbp-20h] BYREF
__int16 v8; // [rsp+9h] [rbp-1Fh]
__int16 v9; // [rsp+Bh] [rbp-1Dh]

v8 = a2;
v9 = a3;
buf = -110;
write(fd, &buf, 5uLL);
write(fd, a4, a3);
read(fd, &buf, 2uLL);
return buf == -98;
}

original_code:
int a2write(int fd, int address, int count, char *buffer)
{
    char writepkt[8];
    writepkt[0] = 0x92;
    writepkt[1] = address;
    writepkt[2] = address >> 8;
    writepkt[3] = count;
    writepkt[4] = count >> 8;
    write(fd, writepkt, 5);
    write(fd, buffer, count);
    read(fd, writepkt, 2);
    return ((unsigned char)writepkt[0] == 0x9E);
}

variable_types:
writepkt
char [8]


decompiled_code:
_BOOL8 __fastcall a2call(int fd, __int16 a2, _DWORD *a3)
{
char buf; // [rsp+Ch] [rbp-1Ch] BYREF
__int16 v6; // [rsp+Dh] [rbp-1Bh]

v6 = a2;
buf = -102;
write(fd, &buf, 3uLL);
read(fd, &buf, 2uLL);
if ( a3 )
*a3 = (unsigned __int8)v6;
return buf == -98;
}

canonical_decompiled_code:
_BOOL8 __fastcall func12(int fd, __int16 a2, _DWORD *a3)
{
char buf; // [rsp+Ch] [rbp-1Ch] BYREF
__int16 v6; // [rsp+Dh] [rbp-1Bh]

v6 = a2;
buf = -102;
write(fd, &buf, 3uLL);
read(fd, &buf, 2uLL);
if ( a3 )
*a3 = (unsigned __int8)v6;
return buf == -98;
}

original_code:
int a2call(int fd, int address, int *result)
{
    char callpkt[4];
    callpkt[0] = 0x9A;
    callpkt[1] = address;
    callpkt[2] = address >> 8;
    write(fd, callpkt, 3);
    read(fd, callpkt, 2);
    if (result)
        *result = (unsigned char)callpkt[1];
    return ((unsigned char)callpkt[0] == 0x9E);
}

variable_types:
result
int *

variable_types:
callpkt
char [4]


decompiled_code:
_BOOL8 __fastcall a2quickcall(int fd, __int16 a2, _DWORD *a3)
{
char buf; // [rsp+Ch] [rbp-1Ch] BYREF
__int16 v6; // [rsp+Dh] [rbp-1Bh]

v6 = a2;
buf = -108;
write(fd, &buf, 3uLL);
read(fd, &buf, 2uLL);
if ( a3 )
*a3 = (unsigned __int8)v6;
return buf == -98;
}

canonical_decompiled_code:
_BOOL8 __fastcall func13(int fd, __int16 a2, _DWORD *a3)
{
char buf; // [rsp+Ch] [rbp-1Ch] BYREF
__int16 v6; // [rsp+Dh] [rbp-1Bh]

v6 = a2;
buf = -108;
write(fd, &buf, 3uLL);
read(fd, &buf, 2uLL);
if ( a3 )
*a3 = (unsigned __int8)v6;
return buf == -98;
}

original_code:
int a2quickcall(int fd, int address, int *result)
{
    char callpkt[4];
    callpkt[0] = 0x94;
    callpkt[1] = address;
    callpkt[2] = address >> 8;
    write(fd, callpkt, 3);
    read(fd, callpkt, 2);
    if (result)
        *result = (unsigned char)callpkt[1];
    return ((unsigned char)callpkt[0] == 0x9E);
}


prbytes

decompiled_code:
int __fastcall prbytes(unsigned int a1, int a2, unsigned __int8 *a3)
{
__int64 v4; // rbp
__int64 v5; // rsi

printf("%04X:", a1);
if ( a2 > 0 )
{
v4 = (__int64)&a3[a2 - 1 + 1];
do
{
v5 = *a3++;
printf(" %02X", v5);
}
while ( a3 != (unsigned __int8 *)v4 );
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func14(unsigned int a1, int a2, unsigned __int8 *a3)
{
__int64 v4; // rbp
__int64 v5; // rsi

printf("%04X:", a1);
if ( a2 > 0 )
{
v4 = (__int64)&a3[a2 - 1 + 1];
do
{
v5 = *a3++;
printf(" %02X", v5);
}
while ( a3 != (unsigned __int8 *)v4 );
}
return putchar(10);
}

original_code:
void prbytes(int address, int count, char *data)
{
    int i;
    printf("%04X:", address);
    for (i = 0; i < count; i++)
 printf(" %02X", (unsigned char)data[i]);
    printf("\n");
}

variable_types:
data
char *

variable_types: i int


decompiled_code:
__int64 __fastcall a2exec(int fd, int a2, signed int *a3, signed int a4, void *a5, int *a6)
{
signed int v8; // ebx
signed int v9; // er15
int v10; // ebp
__int64 result; // rax
int v12; // edx
unsigned int i; // [rsp+8h] [rbp-40h]
int *v14; // [rsp+8h] [rbp-40h]

if ( a2 == 146 )
{
v12 = *a6;
if ( *a6 )
{
v14 = a6;
result = a2write(fd, *a3, v12, a5);
a6 = v14;
v12 = *v14;
}
*a3 += v12;
*a6 = 0;
}
else if ( a2 > 146 )
{
if ( a2 == 148 )
result = a2call(fd, *a3, 0LL);
}
else if ( a2 )
{
if ( a2 == 144 )
{
v8 = *a3;
if ( *a3 >= a4 )
a4 = *a3;
v9 = a4;
for ( i = a4 + 1; v9 >= v8; v8 += 16 )
{
v10 = i - v8;
if ( v9 > v8 + 15 )
v10 = 16;
a2read(fd, v8, v10, a5);
prbytes(v8, v10, (unsigned __int8 *)a5);
}
result = i;
*a3 = i;
}
}
else if ( a4 != -1 )
{
*a3 = a4;
}
return result;
}

original_code:
void a2exec(int fd, int cmd, int *address, int value, char *data, int *index)
{
    int a, c;
    switch (cmd)
    {
 case 0x00:
     if (value != -1)
  *address = value;
     break;
 case 0x90:
     if (value < *address)
  value = *address;
     for (a = *address; a <= value; a += 16)
     {
  c = a + 16 > value ? value - a + 1 : 16;
  a2read(fd, a, c, data);
  prbytes(a, c, data);
     }
     *address = value + 1;
     break;
 case 0x92:
     if (*index)
  a2write(fd, *address, *index, data);
     *address += *index;
     *index = 0;
     break;
 case 0x94:
     a2call(fd, *address, ((void *)0));
     break;
    }
}

canonical_original_code:
void a2exec(int fd, int cmd, int *address, int value, char *data, int *index)
{
    int a; int c;
    switch (cmd)
    {
 case 0x00:
     if (value != -1)
  *address = value;
     break;
 case 0x90:
     if (value < *address)
  value = *address;
     for (a = *address; a <= value; a += 16)
     {
  c = a + 16 > value ? value - a + 1 : 16;
  a2read(fd, a, c, data);
  prbytes(a, c, data);
     }
     *address = value + 1;
     break;
 case 0x92:
     if (*index)
  a2write(fd, *address, *index, data);
     *address += *index;
     *index = 0;
     break;
 case 0x94:
     a2call(fd, *address, ((void *)0));
     break;
    }
}

variable_types: cmd int

variable_types:
address
int *

variable_types: value int

variable_types:
index
int *

variable_types: a int

variable_types: c int


parsestr

original_code:
int parsestr(int fd, char *cmdstr)
{
    char databuf[1024];
    static int addr = 0;
    int index = 0;
    int parseval = -1;
    int cmd =0 ;
    while (1)
    {
 switch (*cmdstr)
 {
     case ':':
  a2exec(fd, cmd, &addr, parseval, databuf, &index);
  cmd = 0x92;
  parseval = -1;
  break;
     case '.':
  a2exec(fd, cmd, &addr, parseval, databuf, &index);
  cmd = 0x90;
  parseval = -1;
  break;
     case 'R':
     case 'r':
  a2exec(fd, cmd, &addr, parseval, databuf, &index);
  cmd = 0x94;
  break;
     case 'a':
     case 'b':
     case 'c':
     case 'd':
     case 'e':
     case 'f':
  *cmdstr -= 'a' - 'A';
     case 'A':
     case 'B':
     case 'C':
     case 'D':
     case 'E':
     case 'F':
  *cmdstr -= 'A' - '9' - 1;
     case '0':
     case '1':
     case '2':
     case '3':
     case '4':
     case '5':
     case '6':
     case '7':
     case '8':
     case '9':
  if (parseval == -1)
      parseval = 0;
  parseval = parseval * 16 + *cmdstr - '0';
  break;
     case ' ':
  if (parseval != -1)
  {
      if (cmd == 0x92)
   databuf[index++] = parseval;
      else
   a2exec(fd, cmd, &addr, parseval, databuf, &index);
      parseval = -1;
  }
  break;
     case '\n':
     case '\0':
  if (parseval != -1)
  {
      if (cmd == 0x92)
   databuf[index++] = parseval;
      else if (cmd == 0)
      {
   addr = parseval;
   cmd = 0x90;
      }
  }
  a2exec(fd, cmd, &addr, parseval, databuf, &index);
  return 1;
  break;
     case 'Q':
     case 'q':
  return 0;
     default:
  return 1;
 }
 cmdstr++;
    }
    return 1;
}

canonical_original_code:
int parsestr(int fd, char *cmdstr)
{
    char databuf[1024];
    static int addr = 0;
    int index = 0;
    int parseval = -1;
    int cmd = 0;
    while (1)
    {
 switch (*cmdstr)
 {
     case ':':
  a2exec(fd, cmd, &addr, parseval, databuf, &index);
  cmd = 0x92;
  parseval = -1;
  break;
     case '.':
  a2exec(fd, cmd, &addr, parseval, databuf, &index);
  cmd = 0x90;
  parseval = -1;
  break;
     case 'R':
     case 'r':
  a2exec(fd, cmd, &addr, parseval, databuf, &index);
  cmd = 0x94;
  break;
     case 'a':
     case 'b':
     case 'c':
     case 'd':
     case 'e':
     case 'f':
  *cmdstr -= 'a' - 'A';
     case 'A':
     case 'B':
     case 'C':
     case 'D':
     case 'E':
     case 'F':
  *cmdstr -= 'A' - '9' - 1;
     case '0':
     case '1':
     case '2':
     case '3':
     case '4':
     case '5':
     case '6':
     case '7':
     case '8':
     case '9':
  if (parseval == -1)
      parseval = 0;
  parseval = parseval * 16 + *cmdstr - '0';
  break;
     case ' ':
  if (parseval != -1)
  {
      if (cmd == 0x92)
   databuf[index++] = parseval;
      else
   a2exec(fd, cmd, &addr, parseval, databuf, &index);
      parseval = -1;
  }
  break;
     case '\n':
     case '\0':
  if (parseval != -1)
  {
      if (cmd == 0x92)
   databuf[index++] = parseval;
      else if (cmd == 0)
      {
   addr = parseval;
   cmd = 0x90;
      }
  }
  a2exec(fd, cmd, &addr, parseval, databuf, &index);
  return 1;
  break;
     case 'Q':
     case 'q':
  return 0;
     default:
  return 1;
 }
 cmdstr++;
    }
    return 1;
}

variable_types: fd int

variable_types:
cmdstr
char *

variable_types:
databuf
char [1024]

variable_types: addr int

variable_types: index int

variable_types: parseval int