simpletest

decompiled_code:
int __fastcall simpletest(const char *a1)
{
int v1; // eax
int v2; // er15
int v3; // eax
int v4; // edx
unsigned __int16 *v5; // rbx
int v6; // ebp
char v7; // bl
unsigned int v8; // er14
unsigned int i; // ebp
unsigned __int64 v10; // rbx
__int16 v11; // ax
int v12; // eax
__int64 v13; // rbx
__int64 v14; // rbx
const char *v15; // rax
int v17; // [rsp+Ch] [rbp-3Ch]

puts("Starting simple test");
if ( (unsigned int)ec_init(a1) )
{
printf("ec_init on %s succeeded.\n", a1);
if ( (int)ec_config_init(0LL) <= 0 )
{
puts("No slaves found!");
LABEL_53:
puts("End simple test, close socket");
return ec_close();
}
printf("%d slaves found and configured.\n", ec_slavecount);
ec_config_map(&IOmap);
puts("Slaves mapped, state to SAFE_OP.");
ec_statecheck(0LL, 4LL, 8000000LL);
v1 = *((_DWORD *)&ec_slave + 7);
if ( v1 || (v2 = 1, !*((_WORD *)&ec_slave + 12)) )
{
v2 = 8;
if ( v1 <= 8 )
v2 = *((_DWORD *)&ec_slave + 7);
}
v3 = *((_DWORD *)&ec_slave + 11);
if ( v3 || (v17 = 1, !*((_WORD *)&ec_slave + 21)) )
{
v4 = 8;
if ( v3 <= 8 )
v4 = *((_DWORD *)&ec_slave + 11);
v17 = v4;
}
printf(
"segments : %d : %d %d %d %d\n",
*((unsigned __int16 *)&ec_group + 20),
*((unsigned int *)&ec_group + 13),
*((unsigned int *)&ec_group + 14),
*((unsigned int *)&ec_group + 15),
*((unsigned int *)&ec_group + 16));
puts("Request operational state for all slaves");
printf("Calculated workcounter %d\n", *((unsigned __int16 *)&ec_group + 23));
ec_slave = 8;
ec_send_processdata();
ec_receive_processdata(500LL);
ec_writestate(0LL);
ec_statecheck(0LL, 8LL, 8000000LL);
if ( ec_slave != 8 )
{
puts("Not all slaves reached operational state.");
ec_readstate();
if ( ec_slavecount > 0 )
{
v5 = (unsigned __int16 *)(unsigned int)((_DWORD)&ec_slave + 320);
v6 = 1;
do
{
if ( *v5 != 8 )
{
v15 = (const char *)ec_ALstatuscode2string(v5[1]);
printf("Slave %d State=0x%2.2x StatusCode=0x%4.4x : %s\n", (unsigned int)v6, *v5, v5[1], v15);
}
++v6;
v5 += 160;
}
while ( ec_slavecount >= v6 );
}
LABEL_52:
puts("\nRequest safe operational state for all slaves");
ec_slave = 1;
ec_writestate(0LL);
goto LABEL_53;
}
puts("Operational state reached for all slaves.");
v7 = 0;
v8 = 1;
while ( 1 )
{
ec_send_processdata();
v12 = ec_receive_processdata(500LL);
if ( *((unsigned __int16 *)&ec_group + 23) > v12 || *((_BYTE *)&ec_group + 48) )
{
if ( v7 )
putchar(10);
*((_BYTE *)&ec_group + 48) = 0;
ec_readstate();
if ( ec_slavecount > 0 )
{
v10 = (unsigned int)((_DWORD)&ec_slave + 589);
for ( i = 1; ec_slavecount >= (int)i; ++i )
{
v11 = *(_WORD *)(v10 - 269);
if ( v11 == 8 )
{
if ( *(_BYTE *)v10 )
goto LABEL_57;
}
else
{
*((_BYTE *)&ec_group + 48) = 1;
if ( v11 == 20 )
{
printf("ERROR : slave %d is in SAFE_OP + ERROR, attempting ack.\n", i);
*(_WORD *)(v10 - 269) = 20;
ec_writestate(i);
}
else if ( v11 == 4 )
{
printf("WARNING : slave %d is in SAFE_OP, change to OPERATIONAL.\n", i);
*(_WORD *)(v10 - 269) = 8;
ec_writestate(i);
}
else if ( v11 )
{
if ( (unsigned int)ec_reconfig_slave(i) )
{
*(_BYTE *)v10 = 0;
printf("MESSAGE : slave %d reconfigured\n", i);
}
}
else
{
if ( *(_BYTE *)v10 )
goto LABEL_24;
*(_BYTE *)v10 = 1;
printf("ERROR : slave %d lost\n", i);
}
if ( *(_BYTE *)v10 )
{
if ( !*(_WORD *)(v10 - 269) )
{
LABEL_24:
if ( (unsigned int)ec_recover_slave(i) )
{
*(_BYTE *)v10 = 0;
printf("MESSAGE : slave %d recovered\n", i);
}
goto LABEL_18;
}
LABEL_57:
*(_BYTE *)v10 = 0;
printf("MESSAGE : slave %d found\n", i);
goto LABEL_18;
}
}
LABEL_18:
v10 += 320LL;
}
}
v7 = *((_BYTE *)&ec_group + 48);
if ( v7 )
v7 = 0;
else
puts("OK : all slaves resumed OPERATIONAL.");
}
else
{
printf("Processdata cycle %4d, WKC %d , O:", v8, (unsigned int)v12);
if ( v2 > 0 )
{
v13 = 0LL;
do
printf(" %2.2x", *(unsigned __int8 *)(*((_QWORD *)&ec_slave + 4) + v13++));
while ( v2 > (int)v13 );
}
printf(" I:");
if ( v17 > 0 )
{
v14 = 0LL;
do
printf(" %2.2x", *(unsigned __int8 *)(*((_QWORD *)&ec_slave + 6) + v14++));
while ( v17 > (int)v14 );
}
putchar(13);
v7 = 1;
}
usleep(0x2710u);
if ( ++v8 == 10001 )
goto LABEL_52;
}
}
return printf("No socket connection on %s\nExcecute as root\n", a1);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
int v1; // eax
int v2; // er15
int v3; // eax
int v4; // edx
unsigned __int16 *v5; // rbx
int v6; // ebp
char v7; // bl
unsigned int v8; // er14
unsigned int i; // ebp
unsigned __int64 v10; // rbx
__int16 v11; // ax
int v12; // eax
__int64 v13; // rbx
__int64 v14; // rbx
const char *v15; // rax
int v17; // [rsp+Ch] [rbp-3Ch]

puts("Starting simple test");
if ( (unsigned int)ec_init(a1) )
{
printf("ec_init on %s succeeded.\n", a1);
if ( (int)ec_config_init(0LL) <= 0 )
{
puts("No slaves found!");
LABEL_53:
puts("End simple test, close socket");
return ec_close();
}
printf("%d slaves found and configured.\n", ec_slavecount);
ec_config_map(&IOmap);
puts("Slaves mapped, state to SAFE_OP.");
ec_statecheck(0LL, 4LL, 8000000LL);
v1 = *((_DWORD *)&ec_slave + 7);
if ( v1 || (v2 = 1, !*((_WORD *)&ec_slave + 12)) )
{
v2 = 8;
if ( v1 <= 8 )
v2 = *((_DWORD *)&ec_slave + 7);
}
v3 = *((_DWORD *)&ec_slave + 11);
if ( v3 || (v17 = 1, !*((_WORD *)&ec_slave + 21)) )
{
v4 = 8;
if ( v3 <= 8 )
v4 = *((_DWORD *)&ec_slave + 11);
v17 = v4;
}
printf(
"segments : %d : %d %d %d %d\n",
*((unsigned __int16 *)&ec_group + 20),
*((unsigned int *)&ec_group + 13),
*((unsigned int *)&ec_group + 14),
*((unsigned int *)&ec_group + 15),
*((unsigned int *)&ec_group + 16));
puts("Request operational state for all slaves");
printf("Calculated workcounter %d\n", *((unsigned __int16 *)&ec_group + 23));
ec_slave = 8;
ec_send_processdata();
ec_receive_processdata(500LL);
ec_writestate(0LL);
ec_statecheck(0LL, 8LL, 8000000LL);
if ( ec_slave != 8 )
{
puts("Not all slaves reached operational state.");
ec_readstate();
if ( ec_slavecount > 0 )
{
v5 = (unsigned __int16 *)(unsigned int)((_DWORD)&ec_slave + 320);
v6 = 1;
do
{
if ( *v5 != 8 )
{
v15 = (const char *)ec_ALstatuscode2string(v5[1]);
printf("Slave %d State=0x%2.2x StatusCode=0x%4.4x : %s\n", (unsigned int)v6, *v5, v5[1], v15);
}
++v6;
v5 += 160;
}
while ( ec_slavecount >= v6 );
}
LABEL_52:
puts("\nRequest safe operational state for all slaves");
ec_slave = 1;
ec_writestate(0LL);
goto LABEL_53;
}
puts("Operational state reached for all slaves.");
v7 = 0;
v8 = 1;
while ( 1 )
{
ec_send_processdata();
v12 = ec_receive_processdata(500LL);
if ( *((unsigned __int16 *)&ec_group + 23) > v12 || *((_BYTE *)&ec_group + 48) )
{
if ( v7 )
putchar(10);
*((_BYTE *)&ec_group + 48) = 0;
ec_readstate();
if ( ec_slavecount > 0 )
{
v10 = (unsigned int)((_DWORD)&ec_slave + 589);
for ( i = 1; ec_slavecount >= (int)i; ++i )
{
v11 = *(_WORD *)(v10 - 269);
if ( v11 == 8 )
{
if ( *(_BYTE *)v10 )
goto LABEL_57;
}
else
{
*((_BYTE *)&ec_group + 48) = 1;
if ( v11 == 20 )
{
printf("ERROR : slave %d is in SAFE_OP + ERROR, attempting ack.\n", i);
*(_WORD *)(v10 - 269) = 20;
ec_writestate(i);
}
else if ( v11 == 4 )
{
printf("WARNING : slave %d is in SAFE_OP, change to OPERATIONAL.\n", i);
*(_WORD *)(v10 - 269) = 8;
ec_writestate(i);
}
else if ( v11 )
{
if ( (unsigned int)ec_reconfig_slave(i) )
{
*(_BYTE *)v10 = 0;
printf("MESSAGE : slave %d reconfigured\n", i);
}
}
else
{
if ( *(_BYTE *)v10 )
goto LABEL_24;
*(_BYTE *)v10 = 1;
printf("ERROR : slave %d lost\n", i);
}
if ( *(_BYTE *)v10 )
{
if ( !*(_WORD *)(v10 - 269) )
{
LABEL_24:
if ( (unsigned int)ec_recover_slave(i) )
{
*(_BYTE *)v10 = 0;
printf("MESSAGE : slave %d recovered\n", i);
}
goto LABEL_18;
}
LABEL_57:
*(_BYTE *)v10 = 0;
printf("MESSAGE : slave %d found\n", i);
goto LABEL_18;
}
}
LABEL_18:
v10 += 320LL;
}
}
v7 = *((_BYTE *)&ec_group + 48);
if ( v7 )
v7 = 0;
else
puts("OK : all slaves resumed OPERATIONAL.");
}
else
{
printf("Processdata cycle %4d, WKC %d , O:", v8, (unsigned int)v12);
if ( v2 > 0 )
{
v13 = 0LL;
do
printf(" %2.2x", *(unsigned __int8 *)(*((_QWORD *)&ec_slave + 4) + v13++));
while ( v2 > (int)v13 );
}
printf(" I:");
if ( v17 > 0 )
{
v14 = 0LL;
do
printf(" %2.2x", *(unsigned __int8 *)(*((_QWORD *)&ec_slave + 6) + v14++));
while ( v17 > (int)v14 );
}
putchar(13);
v7 = 1;
}
usleep(0x2710u);
if ( ++v8 == 10001 )
goto LABEL_52;
}
}
return printf("No socket connection on %s\nExcecute as root\n", a1);
}

original_code:
void simpletest(char *ifname)
{
 int i, j, oloop, iloop, wkc, wkc_count, slave;
 boolean needlf;
 needlf = 0;
 printf("Starting simple test\n");
 if (ec_init(ifname))
 {
  printf("ec_init on %s succeeded.\n",ifname);
     if ( ec_config_init(0) > 0 )
  {
   printf("%d slaves found and configured.\n",ec_slavecount);
   ec_config_map(&IOmap);
   printf("Slaves mapped, state to SAFE_OP.\n");
   ec_statecheck(0, EC_STATE_SAFE_OP, 2000000 * 4);
   oloop = ec_slave[0].Obytes;
   if ((oloop == 0) && (ec_slave[0].Obits > 0)) oloop = 1;
   if (oloop > 8) oloop = 8;
   iloop = ec_slave[0].Ibytes;
   if ((iloop == 0) && (ec_slave[0].Ibits > 0)) iloop = 1;
   if (iloop > 8) iloop = 8;
   printf("segments : %d : %d %d %d %d\n",ec_group[0].nsegments ,ec_group[0].IOsegment[0],ec_group[0].IOsegment[1],ec_group[0].IOsegment[2],ec_group[0].IOsegment[3]);
   printf("Request operational state for all slaves\n");
   printf("Calculated workcounter %d\n",ec_group[0].expectedWKC);
   ec_slave[0].state = EC_STATE_OPERATIONAL;
   ec_send_processdata();
   ec_receive_processdata(500);
   ec_writestate(0);
   ec_statecheck(0, EC_STATE_OPERATIONAL, 2000000 * 4);
   if (ec_slave[0].state == EC_STATE_OPERATIONAL )
   {
    printf("Operational state reached for all slaves.\n");
    wkc_count = 0;
    for(i = 1; i <= 10000; i++)
    {
     ec_send_processdata();
     wkc = ec_receive_processdata(500);
     if( (wkc < ec_group[0].expectedWKC) || ec_group[0].docheckstate)
     {
      if (needlf)
      {
       needlf = 0;
       printf("\n");
      }
      ec_group[0].docheckstate = 0;
      ec_readstate();
      for (slave = 1; slave <= ec_slavecount; slave++)
      {
       if (ec_slave[slave].state != EC_STATE_OPERATIONAL)
       {
        ec_group[0].docheckstate = 1;
        if (ec_slave[slave].state == (EC_STATE_SAFE_OP + EC_STATE_ERROR))
        {
         printf("ERROR : slave %d is in SAFE_OP + ERROR, attempting ack.\n", slave);
         ec_slave[slave].state = (EC_STATE_SAFE_OP + EC_STATE_ACK);
         ec_writestate(slave);
        }
        else if(ec_slave[slave].state == EC_STATE_SAFE_OP)
        {
         printf("WARNING : slave %d is in SAFE_OP, change to OPERATIONAL.\n", slave);
         ec_slave[slave].state = EC_STATE_OPERATIONAL;
         ec_writestate(slave);
        }
        else if(ec_slave[slave].state > 0)
        {
         if (ec_reconfig_slave(slave))
         {
          ec_slave[slave].islost = 0;
          printf("MESSAGE : slave %d reconfigured\n",slave);
         }
        }
        else if(!ec_slave[slave].islost)
        {
         ec_slave[slave].islost = 1;
         printf("ERROR : slave %d lost\n",slave);
        }
       }
       if (ec_slave[slave].islost)
       {
        if(!ec_slave[slave].state)
        {
         if (ec_recover_slave(slave))
         {
          ec_slave[slave].islost = 0;
          printf("MESSAGE : slave %d recovered\n",slave);
         }
        }
        else
        {
         ec_slave[slave].islost = 0;
         printf("MESSAGE : slave %d found\n",slave);
        }
       }
      }
      if(!ec_group[0].docheckstate)
       printf("OK : all slaves resumed OPERATIONAL.\n");
     }
     else
     {
      printf("Processdata cycle %4d, WKC %d , O:", i, wkc);
      for(j = 0 ; j < oloop; j++)
      {
       printf(" %2.2x", *(ec_slave[0].outputs + j));
      }
      printf(" I:");
      for(j = 0 ; j < iloop; j++)
      {
       printf(" %2.2x", *(ec_slave[0].inputs + j));
      }
      printf("\r");
      needlf = 1;
     }
     usleep(10000);
    }
   }
   else
   {
    printf("Not all slaves reached operational state.\n");
    ec_readstate();
    for(i = 1; i<=ec_slavecount ; i++)
    {
     if(ec_slave[i].state != EC_STATE_OPERATIONAL)
     {
      printf("Slave %d State=0x%2.2x StatusCode=0x%4.4x : %s\n",
       i, ec_slave[i].state, ec_slave[i].ALstatuscode, ec_ALstatuscode2string(ec_slave[i].ALstatuscode));
     }
    }
   }
   printf("\nRequest safe operational state for all slaves\n");
   ec_slave[0].state = EC_STATE_INIT;
   ec_writestate(0);
  }
  else
  {
   printf("No slaves found!\n");
  }
  printf("End simple test, close socket\n");
  ec_close();
 }
 else
 {
  printf("No socket connection on %s\nExcecute as root\n",ifname);
 }
}

canonical_original_code:
void simpletest(char *ifname)
{
 int i; int j; int oloop; int iloop; int wkc; int wkc_count; int slave;
 unsigned char needlf;
 needlf = 0;
 printf("Starting simple test\n");
 if (ec_init(ifname))
 {
  printf("ec_init on %s succeeded.\n",ifname);
     if ( ec_config_init(0) > 0 )
  {
   printf("%d slaves found and configured.\n",ec_slavecount);
   ec_config_map(&IOmap);
   printf("Slaves mapped, state to SAFE_OP.\n");
   ec_statecheck(0, EC_STATE_SAFE_OP, 2000000 * 4);
   oloop = ec_slave[0].Obytes;
   if ((oloop == 0) && (ec_slave[0].Obits > 0)) oloop = 1;
   if (oloop > 8) oloop = 8;
   iloop = ec_slave[0].Ibytes;
   if ((iloop == 0) && (ec_slave[0].Ibits > 0)) iloop = 1;
   if (iloop > 8) iloop = 8;
   printf("segments : %d : %d %d %d %d\n",ec_group[0].nsegments ,ec_group[0].IOsegment[0],ec_group[0].IOsegment[1],ec_group[0].IOsegment[2],ec_group[0].IOsegment[3]);
   printf("Request operational state for all slaves\n");
   printf("Calculated workcounter %d\n",ec_group[0].expectedWKC);
   ec_slave[0].state = EC_STATE_OPERATIONAL;
   ec_send_processdata();
   ec_receive_processdata(500);
   ec_writestate(0);
   ec_statecheck(0, EC_STATE_OPERATIONAL, 2000000 * 4);
   if (ec_slave[0].state == EC_STATE_OPERATIONAL )
   {
    printf("Operational state reached for all slaves.\n");
    wkc_count = 0;
    for(i = 1; i <= 10000; i++)
    {
     ec_send_processdata();
     wkc = ec_receive_processdata(500);
     if( (wkc < ec_group[0].expectedWKC) || ec_group[0].docheckstate)
     {
      if (needlf)
      {
       needlf = 0;
       printf("\n");
      }
      ec_group[0].docheckstate = 0;
      ec_readstate();
      for (slave = 1; slave <= ec_slavecount; slave++)
      {
       if (ec_slave[slave].state != EC_STATE_OPERATIONAL)
       {
        ec_group[0].docheckstate = 1;
        if (ec_slave[slave].state == (EC_STATE_SAFE_OP + EC_STATE_ERROR))
        {
         printf("ERROR : slave %d is in SAFE_OP + ERROR, attempting ack.\n", slave);
         ec_slave[slave].state = (EC_STATE_SAFE_OP + EC_STATE_ACK);
         ec_writestate(slave);
        }
        else if(ec_slave[slave].state == EC_STATE_SAFE_OP)
        {
         printf("WARNING : slave %d is in SAFE_OP, change to OPERATIONAL.\n", slave);
         ec_slave[slave].state = EC_STATE_OPERATIONAL;
         ec_writestate(slave);
        }
        else if(ec_slave[slave].state > 0)
        {
         if (ec_reconfig_slave(slave))
         {
          ec_slave[slave].islost = 0;
          printf("MESSAGE : slave %d reconfigured\n",slave);
         }
        }
        else if(!ec_slave[slave].islost)
        {
         ec_slave[slave].islost = 1;
         printf("ERROR : slave %d lost\n",slave);
        }
       }
       if (ec_slave[slave].islost)
       {
        if(!ec_slave[slave].state)
        {
         if (ec_recover_slave(slave))
         {
          ec_slave[slave].islost = 0;
          printf("MESSAGE : slave %d recovered\n",slave);
         }
        }
        else
        {
         ec_slave[slave].islost = 0;
         printf("MESSAGE : slave %d found\n",slave);
        }
       }
      }
      if(!ec_group[0].docheckstate)
       printf("OK : all slaves resumed OPERATIONAL.\n");
     }
     else
     {
      printf("Processdata cycle %4d, WKC %d , O:", i, wkc);
      for(j = 0 ; j < oloop; j++)
      {
       printf(" %2.2x", *(ec_slave[0].outputs + j));
      }
      printf(" I:");
      for(j = 0 ; j < iloop; j++)
      {
       printf(" %2.2x", *(ec_slave[0].inputs + j));
      }
      printf("\r");
      needlf = 1;
     }
     usleep(10000);
    }
   }
   else
   {
    printf("Not all slaves reached operational state.\n");
    ec_readstate();
    for(i = 1; i<=ec_slavecount ; i++)
    {
     if(ec_slave[i].state != EC_STATE_OPERATIONAL)
     {
      printf("Slave %d State=0x%2.2x StatusCode=0x%4.4x : %s\n",
       i, ec_slave[i].state, ec_slave[i].ALstatuscode, ec_ALstatuscode2string(ec_slave[i].ALstatuscode));
     }
    }
   }
   printf("\nRequest safe operational state for all slaves\n");
   ec_slave[0].state = EC_STATE_INIT;
   ec_writestate(0);
  }
  else
  {
   printf("No slaves found!\n");
  }
  printf("End simple test, close socket\n");
  ec_close();
 }
 else
 {
  printf("No socket connection on %s\nExcecute as root\n",ifname);
 }
}

variable_types: i int

variable_types: j int

variable_types: oloop int

variable_types: iloop int

variable_types: wkc int

variable_types: slave int

variable_types:
needlf
unsigned char


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
puts("SOEM (Simple Open EtherCAT Master)\nSimple test");
if ( argc <= 1 )
puts("Usage: simple_test ifname1\nifname = eth0 for example");
else
simpletest(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)\nSimple test");
if ( argc <= 1 )
puts("Usage: simple_test ifname1\nifname = eth0 for example");
else
func0(argv[1]);
puts("End program");
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 printf("SOEM (Simple Open EtherCAT Master)\nSimple test\n");
 if (argc > 1)
 {
  simpletest(argv[1]);
 }
 else
 {
  printf("Usage: simple_test ifname1\nifname = eth0 for example\n");
 }
 printf("End program\n");
 return (0);
}

variable_types: argc int

variable_types:
argv
char *[]

return_type: int


decompiled_code:
__int64 __fastcall ec_SoEerror(__int16 a1, __int16 a2, __int16 a3)
{
struct timeval v5; // [rsp+0h] [rbp-48h] BYREF
__int16 v6; // [rsp+12h] [rbp-36h]
__int16 v7; // [rsp+14h] [rbp-34h]
char v8; // [rsp+16h] [rbp-32h]
int v9; // [rsp+18h] [rbp-30h]
__int16 v10; // [rsp+1Ch] [rbp-2Ch]

gettimeofday(&v5, 0LL);
v6 = a1;
v7 = a2;
v8 = 0;
EcatError = 1;
v9 = 8;
v10 = a3;
return ec_pusherror(&v5);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int16 a1, __int16 a2, __int16 a3)
{
struct timeval v5; // [rsp+0h] [rbp-48h] BYREF
__int16 v6; // [rsp+12h] [rbp-36h]
__int16 v7; // [rsp+14h] [rbp-34h]
char v8; // [rsp+16h] [rbp-32h]
int v9; // [rsp+18h] [rbp-30h]
__int16 v10; // [rsp+1Ch] [rbp-2Ch]

gettimeofday(&v5, 0LL);
v6 = a1;
v7 = a2;
v8 = 0;
EcatError = 1;
v9 = 8;
v10 = a3;
return ec_pusherror(&v5);
}

original_code:
void ec_SoEerror(uint16 Slave, uint16 idn, uint16 Error)
{
    ec_errort Ec;
 gettimeofday(&Ec.Time, 0);
    Ec.Slave = Slave;
    Ec.Index = idn;
    Ec.SubIdx = 0;
    EcatError = 1;
    Ec.Etype = EC_ERR_TYPE_SOE_ERROR;
    Ec.ErrorCode = Error;
    ec_pusherror(&Ec);
}

canonical_original_code:
void ec_SoEerror(unsigned short Slave, unsigned short idn, unsigned short Error)
{
    struct ec_errort Ec;
 gettimeofday(&Ec.Time, 0);
    Ec.Slave = Slave;
    Ec.Index = idn;
    Ec.SubIdx = 0;
    EcatError = 1;
    Ec.Etype = EC_ERR_TYPE_SOE_ERROR;
    Ec.ErrorCode = Error;
    ec_pusherror(&Ec);
}

variable_types:
Slave
unsigned short

variable_types:
idn
unsigned short

variable_types:
Error
unsigned short

variable_types: struct ec_errort { struct timeval { long tv_sec; long tv_usec; } Time; unsigned char Signal; unsigned short Slave; unsigned short Index; unsigned char SubIdx; enum ec_err_type { EC_ERR_TYPE_SDO_ERROR=0, EC_ERR_TYPE_EMERGENCY=1, EC_ERR_TYPE_PACKET_ERROR=3, EC_ERR_TYPE_SDOINFO_ERROR=4, EC_ERR_TYPE_FOE_ERROR=5, EC_ERR_TYPE_FOE_BUF2SMALL=6, EC_ERR_TYPE_FOE_PACKETNUMBER=7, EC_ERR_TYPE_SOE_ERROR=8 } Etype; union { int AbortCode; struct { unsigned short ErrorCode; unsigned char ErrorReg; unsigned char b1; unsigned short w1; unsigned short w2; }; }; }

return_type: void

user_defined_types: struct ec_errort { struct timeval Time; unsigned char Signal; unsigned short Slave; unsigned short Index; unsigned char SubIdx; enum ec_err_type Etype; union { int AbortCode; struct { unsigned short ErrorCode; unsigned char ErrorReg; unsigned char b1; unsigned short w1; unsigned short w2; }; }; }


original_code:
int ec_SoEread(uint16 slave, uint8 driveNo, uint8 elementflags, uint16 idn, int *psize, void *p, int timeout)
{
    ec_SoEt *SoEp, *aSoEp;
    uint16 totalsize, framedatasize;
 int wkc;
    uint8 *bp;
 uint8 *mp;
 uint16 *errorcode;
    ec_mbxbuft MbxIn, MbxOut;
    uint8 cnt;
 boolean NotLast;
    ec_clearmbx(&MbxIn);
    wkc = ec_mbxreceive(slave, (ec_mbxbuft *)&MbxIn, 0);
    ec_clearmbx(&MbxOut);
    aSoEp = (ec_SoEt *)&MbxIn;
    SoEp = (ec_SoEt *)&MbxOut;
    SoEp->MbxHeader.length = (sizeof(ec_SoEt) - sizeof(ec_mbxheadert));
    SoEp->MbxHeader.address = (0x0000);
    SoEp->MbxHeader.priority = 0x00;
    cnt = ec_nextmbxcnt(ec_slave[slave].mbx_cnt);
    ec_slave[slave].mbx_cnt = cnt;
    SoEp->MbxHeader.mbxtype = ECT_MBXT_SOE + (cnt << 4);
 SoEp->opCode = ECT_SOE_READREQ;
 SoEp->incomplete = 0;
 SoEp->error = 0;
 SoEp->driveNo = driveNo;
 SoEp->elementflags = elementflags;
 SoEp->idn = (idn);
 totalsize = 0;
 bp = p;
 mp = (uint8 *)&MbxIn + sizeof(ec_SoEt);
 NotLast = 1;
    wkc = ec_mbxsend(slave, (ec_mbxbuft *)&MbxOut, 20000);
    if (wkc > 0)
 {
  while (NotLast)
  {
   ec_clearmbx(&MbxIn);
   wkc = ec_mbxreceive(slave, (ec_mbxbuft *)&MbxIn, timeout);
   if (wkc > 0)
   {
    if (((aSoEp->MbxHeader.mbxtype & 0x0f) == ECT_MBXT_SOE) &&
        (aSoEp->opCode == ECT_SOE_READRES) &&
        (aSoEp->error == 0) &&
        (aSoEp->driveNo == driveNo) &&
        (aSoEp->elementflags == elementflags))
    {
     framedatasize = (aSoEp->MbxHeader.length) - sizeof(ec_SoEt) + sizeof(ec_mbxheadert);
     totalsize += framedatasize;
     if (totalsize <= *psize)
     {
      memcpy(bp, mp, framedatasize);
      bp += framedatasize;
     }
     else
     {
      framedatasize -= totalsize - *psize;
      totalsize = *psize;
      if (framedatasize > 0) memcpy(bp, mp, framedatasize);
     }
     if (!aSoEp->incomplete)
     {
      NotLast = 0;
      *psize = totalsize;
     }
    }
    else
    {
     NotLast = 0;
     if (((aSoEp->MbxHeader.mbxtype & 0x0f) == ECT_MBXT_SOE) &&
         (aSoEp->opCode == ECT_SOE_READRES) &&
         (aSoEp->error == 1))
     {
      mp = (uint8 *)&MbxIn + ((aSoEp->MbxHeader.length) + sizeof(ec_mbxheadert) - sizeof(uint16));
      errorcode = (uint16 *)mp;
      ec_SoEerror(slave, idn, *errorcode);
     }
     else
     {
      ec_packeterror(slave, idn, 0, 1);
     }
     wkc = 0;
    }
   }
   else
   {
    NotLast = 0;
    ec_packeterror(slave, idn, 0, 4);
   }
  }
 }
    return wkc;
}

canonical_original_code:
int ec_SoEread(unsigned short slave, unsigned char driveNo, unsigned char elementflags, unsigned short idn, int *psize, void *p, int timeout)
{
    struct ec_SoEt *SoEp; struct ec_SoEt *aSoEp;
    unsigned short totalsize; unsigned short framedatasize;
 int wkc;
    unsigned char *bp;
 unsigned char *mp;
 unsigned short *errorcode;
    unsigned char MbxIn[-1]; unsigned char MbxOut[-1];
    unsigned char cnt;
 unsigned char NotLast;
    ec_clearmbx(&MbxIn);
    wkc = ec_mbxreceive(slave, (unsigned char (*)[-1])&MbxIn, 0);
    ec_clearmbx(&MbxOut);
    aSoEp = (struct ec_SoEt *)&MbxIn;
    SoEp = (struct ec_SoEt *)&MbxOut;
    SoEp->MbxHeader.length = (sizeof(ec_SoEt) - sizeof(ec_mbxheadert));
    SoEp->MbxHeader.address = (0x0000);
    SoEp->MbxHeader.priority = 0x00;
    cnt = ec_nextmbxcnt(ec_slave[slave].mbx_cnt);
    ec_slave[slave].mbx_cnt = cnt;
    SoEp->MbxHeader.mbxtype = ECT_MBXT_SOE + (cnt << 4);
 SoEp->opCode = ECT_SOE_READREQ;
 SoEp->incomplete = 0;
 SoEp->error = 0;
 SoEp->driveNo = driveNo;
 SoEp->elementflags = elementflags;
 SoEp->idn = (idn);
 totalsize = 0;
 bp = p;
 mp = (unsigned char *)&MbxIn + sizeof(ec_SoEt);
 NotLast = 1;
    wkc = ec_mbxsend(slave, (unsigned char (*)[-1])&MbxOut, 20000);
    if (wkc > 0)
 {
  while (NotLast)
  {
   ec_clearmbx(&MbxIn);
   wkc = ec_mbxreceive(slave, (unsigned char (*)[-1])&MbxIn, timeout);
   if (wkc > 0)
   {
    if (((aSoEp->MbxHeader.mbxtype & 0x0f) == ECT_MBXT_SOE) &&
        (aSoEp->opCode == ECT_SOE_READRES) &&
        (aSoEp->error == 0) &&
        (aSoEp->driveNo == driveNo) &&
        (aSoEp->elementflags == elementflags))
    {
     framedatasize = (aSoEp->MbxHeader.length) - sizeof(ec_SoEt) + sizeof(ec_mbxheadert);
     totalsize += framedatasize;
     if (totalsize <= *psize)
     {
      memcpy(bp, mp, framedatasize);
      bp += framedatasize;
     }
     else
     {
      framedatasize -= totalsize - *psize;
      totalsize = *psize;
      if (framedatasize > 0) memcpy(bp, mp, framedatasize);
     }
     if (!aSoEp->incomplete)
     {
      NotLast = 0;
      *psize = totalsize;
     }
    }
    else
    {
     NotLast = 0;
     if (((aSoEp->MbxHeader.mbxtype & 0x0f) == ECT_MBXT_SOE) &&
         (aSoEp->opCode == ECT_SOE_READRES) &&
         (aSoEp->error == 1))
     {
      mp = (unsigned char *)&MbxIn + ((aSoEp->MbxHeader.length) + sizeof(ec_mbxheadert) - sizeof(uint16));
      errorcode = (unsigned short *)mp;
      ec_SoEerror(slave, idn, *errorcode);
     }
     else
     {
      ec_packeterror(slave, idn, 0, 1);
     }
     wkc = 0;
    }
   }
   else
   {
    NotLast = 0;
    ec_packeterror(slave, idn, 0, 4);
   }
  }
 }
    return wkc;
}

variable_types:
slave
unsigned short

variable_types:
driveNo
unsigned char

variable_types:
elementflags
unsigned char

variable_types:
psize
int *

variable_types:
p
void *

variable_types: timeout int

variable_types: struct ec_SoEt { struct ec_mbxheadert { unsigned short length; unsigned short address; unsigned char priority; unsigned char mbxtype; } MbxHeader; unsigned char opCode; unsigned char incomplete; unsigned char error; unsigned char driveNo; unsigned char elementflags; union { unsigned short idn; unsigned short fragmentsleft; }; } *

variable_types:
totalsize
unsigned short

variable_types:
framedatasize
unsigned short

variable_types:
bp
unsigned char *

variable_types:
mp
unsigned char *

variable_types:
errorcode
unsigned short *

variable_types:
MbxIn
unsigned char [-1]

variable_types:
MbxOut
unsigned char [-1]

variable_types:
cnt
unsigned char

variable_types:
NotLast
unsigned char

user_defined_types: struct ec_SoEt { struct ec_mbxheadert MbxHeader; unsigned char opCode; unsigned char incomplete; unsigned char error; unsigned char driveNo; unsigned char elementflags; union { unsigned short idn; unsigned short fragmentsleft; }; }


original_code:
int ec_SoEwrite(uint16 slave, uint8 driveNo, uint8 elementflags, uint16 idn, int psize, void *p, int timeout)
{
    ec_SoEt *SoEp, *aSoEp;
    uint16 framedatasize, maxdata;
 int wkc;
    uint8 *mp;
 uint8 *hp;
 uint16 *errorcode;
    ec_mbxbuft MbxIn, MbxOut;
    uint8 cnt;
 boolean NotLast;
    ec_clearmbx(&MbxIn);
    wkc = ec_mbxreceive(slave, (ec_mbxbuft *)&MbxIn, 0);
    ec_clearmbx(&MbxOut);
    aSoEp = (ec_SoEt *)&MbxIn;
    SoEp = (ec_SoEt *)&MbxOut;
    SoEp->MbxHeader.address = (0x0000);
    SoEp->MbxHeader.priority = 0x00;
 SoEp->opCode = ECT_SOE_WRITEREQ;
 SoEp->error = 0;
 SoEp->driveNo = driveNo;
 SoEp->elementflags = elementflags;
 hp = p;
 mp = (uint8 *)&MbxOut + sizeof(ec_SoEt);
    maxdata = ec_slave[slave].mbx_l - sizeof(ec_SoEt);
 NotLast = 1;
 while (NotLast)
 {
  framedatasize = psize;
  NotLast = 0;
  SoEp->idn = (idn);
  SoEp->incomplete = 0;
  if (framedatasize > maxdata)
  {
   framedatasize = maxdata;
   NotLast = 1;
   SoEp->incomplete = 1;
   SoEp->fragmentsleft = psize / maxdata;
  }
  SoEp->MbxHeader.length = (sizeof(ec_SoEt) - sizeof(ec_mbxheadert) + framedatasize);
  cnt = ec_nextmbxcnt(ec_slave[slave].mbx_cnt);
  ec_slave[slave].mbx_cnt = cnt;
  SoEp->MbxHeader.mbxtype = ECT_MBXT_SOE + (cnt << 4);
  memcpy(mp, hp, framedatasize);
  hp += framedatasize;
  psize -= framedatasize;
  wkc = ec_mbxsend(slave, (ec_mbxbuft *)&MbxOut, 20000);
  if (wkc > 0)
  {
   if (!NotLast || !ec_mbxempty(slave, timeout))
   {
    ec_clearmbx(&MbxIn);
    wkc = ec_mbxreceive(slave, (ec_mbxbuft *)&MbxIn, timeout);
    if (wkc > 0)
    {
     NotLast = 0;
     if (((aSoEp->MbxHeader.mbxtype & 0x0f) == ECT_MBXT_SOE) &&
         (aSoEp->opCode == ECT_SOE_WRITERES) &&
         (aSoEp->error == 0) &&
         (aSoEp->driveNo == driveNo) &&
         (aSoEp->elementflags == elementflags))
     {
     }
     else
     {
      if (((aSoEp->MbxHeader.mbxtype & 0x0f) == ECT_MBXT_SOE) &&
          (aSoEp->opCode == ECT_SOE_READRES) &&
          (aSoEp->error == 1))
      {
       mp = (uint8 *)&MbxIn + ((aSoEp->MbxHeader.length) + sizeof(ec_mbxheadert) - sizeof(uint16));
       errorcode = (uint16 *)mp;
       ec_SoEerror(slave, idn, *errorcode);
      }
      else
      {
       ec_packeterror(slave, idn, 0, 1);
      }
      wkc = 0;
     }
    }
    else
    {
     ec_packeterror(slave, idn, 0, 4);
    }
   }
  }
 }
    return wkc;
}

canonical_original_code:
int ec_SoEwrite(unsigned short slave, unsigned char driveNo, unsigned char elementflags, unsigned short idn, int psize, void *p, int timeout)
{
    struct ec_SoEt *SoEp; struct ec_SoEt *aSoEp;
    unsigned short framedatasize; unsigned short maxdata;
 int wkc;
    unsigned char *mp;
 unsigned char *hp;
 unsigned short *errorcode;
    unsigned char MbxIn[-1]; unsigned char MbxOut[-1];
    unsigned char cnt;
 unsigned char NotLast;
    ec_clearmbx(&MbxIn);
    wkc = ec_mbxreceive(slave, (unsigned char (*)[-1])&MbxIn, 0);
    ec_clearmbx(&MbxOut);
    aSoEp = (struct ec_SoEt *)&MbxIn;
    SoEp = (struct ec_SoEt *)&MbxOut;
    SoEp->MbxHeader.address = (0x0000);
    SoEp->MbxHeader.priority = 0x00;
 SoEp->opCode = ECT_SOE_WRITEREQ;
 SoEp->error = 0;
 SoEp->driveNo = driveNo;
 SoEp->elementflags = elementflags;
 hp = p;
 mp = (unsigned char *)&MbxOut + sizeof(ec_SoEt);
    maxdata = ec_slave[slave].mbx_l - sizeof(ec_SoEt);
 NotLast = 1;
 while (NotLast)
 {
  framedatasize = psize;
  NotLast = 0;
  SoEp->idn = (idn);
  SoEp->incomplete = 0;
  if (framedatasize > maxdata)
  {
   framedatasize = maxdata;
   NotLast = 1;
   SoEp->incomplete = 1;
   SoEp->fragmentsleft = psize / maxdata;
  }
  SoEp->MbxHeader.length = (sizeof(ec_SoEt) - sizeof(ec_mbxheadert) + framedatasize);
  cnt = ec_nextmbxcnt(ec_slave[slave].mbx_cnt);
  ec_slave[slave].mbx_cnt = cnt;
  SoEp->MbxHeader.mbxtype = ECT_MBXT_SOE + (cnt << 4);
  memcpy(mp, hp, framedatasize);
  hp += framedatasize;
  psize -= framedatasize;
  wkc = ec_mbxsend(slave, (unsigned char (*)[-1])&MbxOut, 20000);
  if (wkc > 0)
  {
   if (!NotLast || !ec_mbxempty(slave, timeout))
   {
    ec_clearmbx(&MbxIn);
    wkc = ec_mbxreceive(slave, (unsigned char (*)[-1])&MbxIn, timeout);
    if (wkc > 0)
    {
     NotLast = 0;
     if (((aSoEp->MbxHeader.mbxtype & 0x0f) == ECT_MBXT_SOE) &&
         (aSoEp->opCode == ECT_SOE_WRITERES) &&
         (aSoEp->error == 0) &&
         (aSoEp->driveNo == driveNo) &&
         (aSoEp->elementflags == elementflags))
     {
     }
     else
     {
      if (((aSoEp->MbxHeader.mbxtype & 0x0f) == ECT_MBXT_SOE) &&
          (aSoEp->opCode == ECT_SOE_READRES) &&
          (aSoEp->error == 1))
      {
       mp = (unsigned char *)&MbxIn + ((aSoEp->MbxHeader.length) + sizeof(ec_mbxheadert) - sizeof(uint16));
       errorcode = (unsigned short *)mp;
       ec_SoEerror(slave, idn, *errorcode);
      }
      else
      {
       ec_packeterror(slave, idn, 0, 1);
      }
      wkc = 0;
     }
    }
    else
    {
     ec_packeterror(slave, idn, 0, 4);
    }
   }
  }
 }
    return wkc;
}

variable_types:
idn
unsigned short

variable_types: psize int

variable_types:
maxdata
unsigned short

variable_types: wkc int

variable_types:
hp
unsigned char *

user_defined_types: struct ec_mbxheadert { unsigned short length; unsigned short address; unsigned char priority; unsigned char mbxtype; }


original_code:
int ec_readIDNmap(uint16 slave, int *Osize, int *Isize)
{
    int retVal = 0;
 int wkc;
 int psize;
 uint16 entries, itemcount;
 *Isize = 0;
 *Osize = 0;
 psize = sizeof(SoEmapping);
 wkc = ec_SoEread(slave, 0, 0x40, 24, &psize, &SoEmapping, 700000);
 if ((wkc > 0) && (psize >= 4) && ((entries = (SoEmapping.currentlength) / 2) > 0) && (entries <= 64))
 {
  *Osize = 16;
  for (itemcount = 0 ; itemcount < entries ; itemcount++)
  {
   psize = sizeof(SoEattribute);
   wkc = ec_SoEread(slave, 0, 0x04, SoEmapping.idn[itemcount], &psize, &SoEattribute, 700000);
   if ((wkc > 0) && (!SoEattribute.list))
   {
    *Osize += (int)8 << SoEattribute.length;
   }
  }
 }
 psize = sizeof(SoEmapping);
 wkc = ec_SoEread(slave, 0, 0x40, 16, &psize, &SoEmapping, 700000);
 if ((wkc > 0) && (psize >= 4) && ((entries = (SoEmapping.currentlength) / 2) > 0) && (entries <= 64))
 {
  *Isize = 16;
  for (itemcount = 0 ; itemcount < entries ; itemcount++)
  {
   psize = sizeof(SoEattribute);
   wkc = ec_SoEread(slave, 0, 0x04, SoEmapping.idn[itemcount], &psize, &SoEattribute, 700000);
   if ((wkc > 0) && (!SoEattribute.list))
   {
    *Isize += (int)8 << SoEattribute.length;
   }
  }
 }
    if ((*Isize > 0) || (*Osize > 0))
    {
        retVal = 1;
    }
    return retVal;
}

canonical_original_code:
int ec_readIDNmap(unsigned short slave, int *Osize, int *Isize)
{
    int retVal = 0;
 int wkc;
 int psize;
 unsigned short entries; unsigned short itemcount;
 *Isize = 0;
 *Osize = 0;
 psize = sizeof(SoEmapping);
 wkc = ec_SoEread(slave, 0, 0x40, 24, &psize, &SoEmapping, 700000);
 if ((wkc > 0) && (psize >= 4) && ((entries = (SoEmapping.currentlength) / 2) > 0) && (entries <= 64))
 {
  *Osize = 16;
  for (itemcount = 0 ; itemcount < entries ; itemcount++)
  {
   psize = sizeof(SoEattribute);
   wkc = ec_SoEread(slave, 0, 0x04, SoEmapping.idn[itemcount], &psize, &SoEattribute, 700000);
   if ((wkc > 0) && (!SoEattribute.list))
   {
    *Osize += (int)8 << SoEattribute.length;
   }
  }
 }
 psize = sizeof(SoEmapping);
 wkc = ec_SoEread(slave, 0, 0x40, 16, &psize, &SoEmapping, 700000);
 if ((wkc > 0) && (psize >= 4) && ((entries = (SoEmapping.currentlength) / 2) > 0) && (entries <= 64))
 {
  *Isize = 16;
  for (itemcount = 0 ; itemcount < entries ; itemcount++)
  {
   psize = sizeof(SoEattribute);
   wkc = ec_SoEread(slave, 0, 0x04, SoEmapping.idn[itemcount], &psize, &SoEattribute, 700000);
   if ((wkc > 0) && (!SoEattribute.list))
   {
    *Isize += (int)8 << SoEattribute.length;
   }
  }
 }
    if ((*Isize > 0) || (*Osize > 0))
    {
        retVal = 1;
    }
    return retVal;
}

variable_types:
slave
unsigned short

variable_types:
Osize
int *

variable_types:
Isize
int *

variable_types: retVal int

variable_types:
entries
unsigned short

variable_types:
itemcount
unsigned short


original_code:
int ec_FOEread(uint16 slave, char *filename, uint32 password, int *psize, void *p, int timeout)
{
    ec_FOEt *FOEp, *aFOEp;
 int wkc;
    int32 dataread = 0;
 int32 buffersize, packetnumber, prevpacket = 0;
 uint16 fnsize, maxdata, segmentdata;
    ec_mbxbuft MbxIn, MbxOut;
    uint8 cnt;
    boolean worktodo;
 buffersize = *psize;
    ec_clearmbx(&MbxIn);
    wkc = ec_mbxreceive(slave, (ec_mbxbuft *)&MbxIn, 0);
    ec_clearmbx(&MbxOut);
    aFOEp = (ec_FOEt *)&MbxIn;
    FOEp = (ec_FOEt *)&MbxOut;
 fnsize = strlen(filename);
 maxdata = ec_slave[slave].mbx_l - 12;
 if (fnsize > maxdata)
  fnsize = maxdata;
    FOEp->MbxHeader.length = (0x0006 + fnsize);
    FOEp->MbxHeader.address = (0x0000);
    FOEp->MbxHeader.priority = 0x00;
    cnt = ec_nextmbxcnt(ec_slave[slave].mbx_cnt);
    ec_slave[slave].mbx_cnt = cnt;
    FOEp->MbxHeader.mbxtype = ECT_MBXT_FOE + (cnt << 4);
    FOEp->OpCode = ECT_FOE_READ;
    FOEp->Password = (password);
 memcpy(&FOEp->FileName[0], filename, fnsize);
    wkc = ec_mbxsend(slave, (ec_mbxbuft *)&MbxOut, 20000);
    if (wkc > 0)
    {
  do
  {
   worktodo = 0;
   ec_clearmbx(&MbxIn);
   wkc = ec_mbxreceive(slave, (ec_mbxbuft *)&MbxIn, timeout);
   if (wkc > 0)
   {
    if ((aFOEp->MbxHeader.mbxtype & 0x0f) == ECT_MBXT_FOE)
    {
     if(aFOEp->OpCode == ECT_FOE_DATA)
     {
      segmentdata = (aFOEp->MbxHeader.length) - 0x0006;
      packetnumber = (aFOEp->PacketNumber);
      if ((packetnumber == ++prevpacket) && (dataread + segmentdata <= buffersize))
      {
       memcpy(p, &aFOEp->Data[0], segmentdata);
       dataread += segmentdata;
       p += segmentdata;
       if (segmentdata == maxdata)
        worktodo = 1;
       FOEp->MbxHeader.length = (0x0006);
       FOEp->MbxHeader.address = (0x0000);
       FOEp->MbxHeader.priority = 0x00;
       cnt = ec_nextmbxcnt(ec_slave[slave].mbx_cnt);
       ec_slave[slave].mbx_cnt = cnt;
       FOEp->MbxHeader.mbxtype = ECT_MBXT_FOE + (cnt << 4);
       FOEp->OpCode = ECT_FOE_ACK;
       FOEp->PacketNumber = (packetnumber);
       wkc = ec_mbxsend(slave, (ec_mbxbuft *)&MbxOut, 20000);
       if (wkc <= 0)
        worktodo = 0;
      }
      else
      {
       wkc = -EC_ERR_TYPE_FOE_BUF2SMALL;
      }
     }
     else
     if(aFOEp->OpCode == ECT_FOE_ERROR)
     {
      wkc = -EC_ERR_TYPE_FOE_ERROR;
     }
     else
     {
      wkc = -EC_ERR_TYPE_PACKET_ERROR;
     }
    }
    else
    {
     wkc = -EC_ERR_TYPE_PACKET_ERROR;
    }
    *psize = dataread;
   }
  } while (worktodo);
 }
 return wkc;
}

canonical_original_code:
int ec_FOEread(unsigned short slave, char *filename, unsigned int password, int *psize, void *p, int timeout)
{
    struct ec_FOEt *FOEp; struct ec_FOEt *aFOEp;
 int wkc;
    int dataread = 0;
 int buffersize; int packetnumber; int prevpacket = 0;
 unsigned short fnsize; unsigned short maxdata; unsigned short segmentdata;
    unsigned char MbxIn[-1]; unsigned char MbxOut[-1];
    unsigned char cnt;
    unsigned char worktodo;
 buffersize = *psize;
    ec_clearmbx(&MbxIn);
    wkc = ec_mbxreceive(slave, (unsigned char (*)[-1])&MbxIn, 0);
    ec_clearmbx(&MbxOut);
    aFOEp = (struct ec_FOEt *)&MbxIn;
    FOEp = (struct ec_FOEt *)&MbxOut;
 fnsize = strlen(filename);
 maxdata = ec_slave[slave].mbx_l - 12;
 if (fnsize > maxdata)
  fnsize = maxdata;
    FOEp->MbxHeader.length = (0x0006 + fnsize);
    FOEp->MbxHeader.address = (0x0000);
    FOEp->MbxHeader.priority = 0x00;
    cnt = ec_nextmbxcnt(ec_slave[slave].mbx_cnt);
    ec_slave[slave].mbx_cnt = cnt;
    FOEp->MbxHeader.mbxtype = ECT_MBXT_FOE + (cnt << 4);
    FOEp->OpCode = ECT_FOE_READ;
    FOEp->Password = (password);
 memcpy(&FOEp->FileName[0], filename, fnsize);
    wkc = ec_mbxsend(slave, (unsigned char (*)[-1])&MbxOut, 20000);
    if (wkc > 0)
    {
  do
  {
   worktodo = 0;
   ec_clearmbx(&MbxIn);
   wkc = ec_mbxreceive(slave, (unsigned char (*)[-1])&MbxIn, timeout);
   if (wkc > 0)
   {
    if ((aFOEp->MbxHeader.mbxtype & 0x0f) == ECT_MBXT_FOE)
    {
     if(aFOEp->OpCode == ECT_FOE_DATA)
     {
      segmentdata = (aFOEp->MbxHeader.length) - 0x0006;
      packetnumber = (aFOEp->PacketNumber);
      if ((packetnumber == ++prevpacket) && (dataread + segmentdata <= buffersize))
      {
       memcpy(p, &aFOEp->Data[0], segmentdata);
       dataread += segmentdata;
       p += segmentdata;
       if (segmentdata == maxdata)
        worktodo = 1;
       FOEp->MbxHeader.length = (0x0006);
       FOEp->MbxHeader.address = (0x0000);
       FOEp->MbxHeader.priority = 0x00;
       cnt = ec_nextmbxcnt(ec_slave[slave].mbx_cnt);
       ec_slave[slave].mbx_cnt = cnt;
       FOEp->MbxHeader.mbxtype = ECT_MBXT_FOE + (cnt << 4);
       FOEp->OpCode = ECT_FOE_ACK;
       FOEp->PacketNumber = (packetnumber);
       wkc = ec_mbxsend(slave, (unsigned char (*)[-1])&MbxOut, 20000);
       if (wkc <= 0)
        worktodo = 0;
      }
      else
      {
       wkc = -EC_ERR_TYPE_FOE_BUF2SMALL;
      }
     }
     else
     if(aFOEp->OpCode == ECT_FOE_ERROR)
     {
      wkc = -EC_ERR_TYPE_FOE_ERROR;
     }
     else
     {
      wkc = -EC_ERR_TYPE_PACKET_ERROR;
     }
    }
    else
    {
     wkc = -EC_ERR_TYPE_PACKET_ERROR;
    }
    *psize = dataread;
   }
  } while (worktodo);
 }
 return wkc;
}

variable_types:
filename
char *

variable_types:
password
unsigned int

variable_types:
psize
int *

variable_types:
p
void *

variable_types: timeout int

variable_types: struct ec_FOEt { struct ec_mbxheadert { unsigned short length; unsigned short address; unsigned char priority; unsigned char mbxtype; } MbxHeader; unsigned char OpCode; unsigned char Reserved; union { unsigned int Password; unsigned int PacketNumber; unsigned int ErrorCode; }; union { char FileName[512]; unsigned char Data[512]; char ErrorText[512]; }; } *

variable_types: dataread int

variable_types: buffersize int

variable_types: packetnumber int

variable_types: prevpacket int

variable_types:
fnsize
unsigned short

variable_types:
segmentdata
unsigned short

variable_types:
worktodo
unsigned char

user_defined_types: struct ec_FOEt { struct ec_mbxheadert MbxHeader; unsigned char OpCode; unsigned char Reserved; union { unsigned int Password; unsigned int PacketNumber; unsigned int ErrorCode; }; union { char FileName[512]; unsigned char Data[512]; char ErrorText[512]; }; }


original_code:
int ec_FOEwrite(uint16 slave, char *filename, uint32 password, int psize, void *p, int timeout)
{
    ec_FOEt *FOEp, *aFOEp;
 int wkc;
 int32 packetnumber, sendpacket = 0;
 uint16 fnsize, maxdata;
 int segmentdata;
    ec_mbxbuft MbxIn, MbxOut;
    uint8 cnt;
    boolean worktodo;
 int tsize;
    ec_clearmbx(&MbxIn);
    wkc = ec_mbxreceive(slave, (ec_mbxbuft *)&MbxIn, 0);
    ec_clearmbx(&MbxOut);
    aFOEp = (ec_FOEt *)&MbxIn;
    FOEp = (ec_FOEt *)&MbxOut;
 fnsize = strlen(filename);
 maxdata = ec_slave[slave].mbx_l - 12;
 if (fnsize > maxdata)
  fnsize = maxdata;
    FOEp->MbxHeader.length = (0x0006 + fnsize);
    FOEp->MbxHeader.address = (0x0000);
    FOEp->MbxHeader.priority = 0x00;
    cnt = ec_nextmbxcnt(ec_slave[slave].mbx_cnt);
    ec_slave[slave].mbx_cnt = cnt;
    FOEp->MbxHeader.mbxtype = ECT_MBXT_FOE + (cnt << 4);
    FOEp->OpCode = ECT_FOE_WRITE;
    FOEp->Password = (password);
 memcpy(&FOEp->FileName[0], filename, fnsize);
    wkc = ec_mbxsend(slave, (ec_mbxbuft *)&MbxOut, 20000);
    if (wkc > 0)
    {
  do
  {
   worktodo = 0;
   ec_clearmbx(&MbxIn);
   wkc = ec_mbxreceive(slave, (ec_mbxbuft *)&MbxIn, timeout);
   if (wkc > 0)
   {
    if ((aFOEp->MbxHeader.mbxtype & 0x0f) == ECT_MBXT_FOE)
    {
     if(aFOEp->OpCode == ECT_FOE_ACK)
     {
      packetnumber = (aFOEp->PacketNumber);
      if (packetnumber == sendpacket)
      {
       tsize = psize;
       if (tsize > maxdata)
       {
        worktodo = 1;
        tsize = maxdata;
       }
       segmentdata = tsize;
       psize -= segmentdata;
       FOEp->MbxHeader.length = (0x0006 + segmentdata);
       FOEp->MbxHeader.address = (0x0000);
       FOEp->MbxHeader.priority = 0x00;
       cnt = ec_nextmbxcnt(ec_slave[slave].mbx_cnt);
       ec_slave[slave].mbx_cnt = cnt;
       FOEp->MbxHeader.mbxtype = ECT_MBXT_FOE + (cnt << 4);
       FOEp->OpCode = ECT_FOE_DATA;
       FOEp->PacketNumber = (++sendpacket);
       memcpy(&FOEp->Data[0], p, segmentdata);
       p += segmentdata;
       wkc = ec_mbxsend(slave, (ec_mbxbuft *)&MbxOut, 20000);
       if (wkc <= 0)
        worktodo = 0;
      }
      else
      {
       wkc = -EC_ERR_TYPE_FOE_PACKETNUMBER;
      }
     }
     else
     if(aFOEp->OpCode == ECT_FOE_ERROR)
     {
      wkc = -EC_ERR_TYPE_FOE_ERROR;
     }
     else
     {
      wkc = -EC_ERR_TYPE_PACKET_ERROR;
     }
    }
    else
    {
     wkc = -EC_ERR_TYPE_PACKET_ERROR;
    }
   }
  } while (worktodo);
 }
 return wkc;
}

canonical_original_code:
int ec_FOEwrite(unsigned short slave, char *filename, unsigned int password, int psize, void *p, int timeout)
{
    struct ec_FOEt *FOEp; struct ec_FOEt *aFOEp;
 int wkc;
 int packetnumber; int sendpacket = 0;
 unsigned short fnsize; unsigned short maxdata;
 int segmentdata;
    unsigned char MbxIn[-1]; unsigned char MbxOut[-1];
    unsigned char cnt;
    unsigned char worktodo;
 int tsize;
    ec_clearmbx(&MbxIn);
    wkc = ec_mbxreceive(slave, (unsigned char (*)[-1])&MbxIn, 0);
    ec_clearmbx(&MbxOut);
    aFOEp = (struct ec_FOEt *)&MbxIn;
    FOEp = (struct ec_FOEt *)&MbxOut;
 fnsize = strlen(filename);
 maxdata = ec_slave[slave].mbx_l - 12;
 if (fnsize > maxdata)
  fnsize = maxdata;
    FOEp->MbxHeader.length = (0x0006 + fnsize);
    FOEp->MbxHeader.address = (0x0000);
    FOEp->MbxHeader.priority = 0x00;
    cnt = ec_nextmbxcnt(ec_slave[slave].mbx_cnt);
    ec_slave[slave].mbx_cnt = cnt;
    FOEp->MbxHeader.mbxtype = ECT_MBXT_FOE + (cnt << 4);
    FOEp->OpCode = ECT_FOE_WRITE;
    FOEp->Password = (password);
 memcpy(&FOEp->FileName[0], filename, fnsize);
    wkc = ec_mbxsend(slave, (unsigned char (*)[-1])&MbxOut, 20000);
    if (wkc > 0)
    {
  do
  {
   worktodo = 0;
   ec_clearmbx(&MbxIn);
   wkc = ec_mbxreceive(slave, (unsigned char (*)[-1])&MbxIn, timeout);
   if (wkc > 0)
   {
    if ((aFOEp->MbxHeader.mbxtype & 0x0f) == ECT_MBXT_FOE)
    {
     if(aFOEp->OpCode == ECT_FOE_ACK)
     {
      packetnumber = (aFOEp->PacketNumber);
      if (packetnumber == sendpacket)
      {
       tsize = psize;
       if (tsize > maxdata)
       {
        worktodo = 1;
        tsize = maxdata;
       }
       segmentdata = tsize;
       psize -= segmentdata;
       FOEp->MbxHeader.length = (0x0006 + segmentdata);
       FOEp->MbxHeader.address = (0x0000);
       FOEp->MbxHeader.priority = 0x00;
       cnt = ec_nextmbxcnt(ec_slave[slave].mbx_cnt);
       ec_slave[slave].mbx_cnt = cnt;
       FOEp->MbxHeader.mbxtype = ECT_MBXT_FOE + (cnt << 4);
       FOEp->OpCode = ECT_FOE_DATA;
       FOEp->PacketNumber = (++sendpacket);
       memcpy(&FOEp->Data[0], p, segmentdata);
       p += segmentdata;
       wkc = ec_mbxsend(slave, (unsigned char (*)[-1])&MbxOut, 20000);
       if (wkc <= 0)
        worktodo = 0;
      }
      else
      {
       wkc = -EC_ERR_TYPE_FOE_PACKETNUMBER;
      }
     }
     else
     if(aFOEp->OpCode == ECT_FOE_ERROR)
     {
      wkc = -EC_ERR_TYPE_FOE_ERROR;
     }
     else
     {
      wkc = -EC_ERR_TYPE_PACKET_ERROR;
     }
    }
    else
    {
     wkc = -EC_ERR_TYPE_PACKET_ERROR;
    }
   }
  } while (worktodo);
 }
 return wkc;
}

variable_types:
slave
unsigned short

variable_types: psize int

variable_types: wkc int

variable_types: sendpacket int

variable_types:
maxdata
unsigned short

variable_types: segmentdata int

variable_types:
MbxIn
unsigned char [-1]

variable_types:
MbxOut
unsigned char [-1]

variable_types:
cnt
unsigned char

variable_types: tsize int


redtest

decompiled_code:
int __fastcall redtest(const char *a1, __int64 a2)
{
unsigned __int64 v2; // rbx
int v3; // ebp
int v4; // eax
int v5; // er12
int v6; // eax
int v7; // ebp
int v9; // er13
__int64 v10; // rbx
__int64 v11; // rbx

puts("Starting Redundant test");
if ( !(unsigned int)ec_init_redundant(a1, a2) )
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);
ec_statecheck(0LL, 4LL, 2000000LL);
ec_configdc();
ec_readstate();
if ( ec_slavecount > 0 )
{
v2 = (unsigned int)((_DWORD)&ec_slave + 600);
v3 = 1;
do
{
printf(
"Slave:%d Name:%s Output size:%3dbits Input size:%3dbits State:%2d delay:%d.%d\n",
(unsigned int)v3,
(const char *)v2,
*(unsigned __int16 *)(v2 - 256),
*(unsigned __int16 *)(v2 - 238),
*(unsigned __int16 *)(v2 - 280),
*(unsigned int *)(v2 - 44),
*(unsigned __int8 *)(v2 - 71));
printf(
" Out:%8.8x,%4d In:%8.8x,%4d\n",
*(unsigned int *)(v2 - 248),
*(unsigned int *)(v2 - 252),
*(unsigned int *)(v2 - 232),
*(unsigned int *)(v2 - 236));
if ( !digout && ((*(_DWORD *)(v2 - 268) - 131346514) & 0xFFFBFFFF) == 0 )
digout = *(_QWORD *)(v2 - 248);
++v3;
v2 += 320LL;
}
while ( ec_slavecount >= v3 );
}
puts("Request operational state for all slaves");
ec_slave = 8;
ec_writestate(0LL);
ec_statecheck(0LL, 8LL, 2000000LL);
v4 = *((_DWORD *)&ec_slave + 7);
if ( v4 || (v5 = 1, !*((_WORD *)&ec_slave + 12)) )
{
v5 = 8;
if ( v4 <= 8 )
v5 = *((_DWORD *)&ec_slave + 7);
}
v6 = *((_DWORD *)&ec_slave + 11);
if ( v6 || (v7 = 1, !*((_WORD *)&ec_slave + 21)) )
{
v7 = 8;
if ( v6 <= 8 )
v7 = *((_DWORD *)&ec_slave + 11);
}
if ( ec_slave == 8 )
{
puts("Operational state reached for all slaves.");
dorun = 1;
v9 = 500;
do
{
printf("Processdata cycle %5d , DCtime %12lld, O:", (unsigned int)dorun, ec_DCtime);
if ( v5 > 0 )
{
v10 = 0LL;
do
printf(" %2.2x", *(unsigned __int8 *)(*((_QWORD *)&ec_slave + 4) + v10++));
while ( v5 > (int)v10 );
}
printf(" I:");
if ( v7 > 0 )
{
v11 = 0LL;
do
printf(" %2.2x", *(unsigned __int8 *)(*((_QWORD *)&ec_slave + 6) + v11++));
while ( v7 > (int)v11 );
}
putchar(10);
usleep(0x4E20u);
--v9;
}
while ( v9 );
dorun = 0;
}
else
{
puts("Not all slaves reached operational state.");
}
puts("Request safe operational state for all slaves");
ec_slave = 4;
ec_writestate(0LL);
}
puts("End redundant test, close socket");
return ec_close();
}

canonical_decompiled_code:
int __fastcall func0(const char *a1, __int64 a2)
{
unsigned __int64 v2; // rbx
int v3; // ebp
int v4; // eax
int v5; // er12
int v6; // eax
int v7; // ebp
int v9; // er13
__int64 v10; // rbx
__int64 v11; // rbx

puts("Starting Redundant test");
if ( !(unsigned int)ec_init_redundant(a1, a2) )
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);
ec_statecheck(0LL, 4LL, 2000000LL);
ec_configdc();
ec_readstate();
if ( ec_slavecount > 0 )
{
v2 = (unsigned int)((_DWORD)&ec_slave + 600);
v3 = 1;
do
{
printf(
"Slave:%d Name:%s Output size:%3dbits Input size:%3dbits State:%2d delay:%d.%d\n",
(unsigned int)v3,
(const char *)v2,
*(unsigned __int16 *)(v2 - 256),
*(unsigned __int16 *)(v2 - 238),
*(unsigned __int16 *)(v2 - 280),
*(unsigned int *)(v2 - 44),
*(unsigned __int8 *)(v2 - 71));
printf(
" Out:%8.8x,%4d In:%8.8x,%4d\n",
*(unsigned int *)(v2 - 248),
*(unsigned int *)(v2 - 252),
*(unsigned int *)(v2 - 232),
*(unsigned int *)(v2 - 236));
if ( !digout && ((*(_DWORD *)(v2 - 268) - 131346514) & 0xFFFBFFFF) == 0 )
digout = *(_QWORD *)(v2 - 248);
++v3;
v2 += 320LL;
}
while ( ec_slavecount >= v3 );
}
puts("Request operational state for all slaves");
ec_slave = 8;
ec_writestate(0LL);
ec_statecheck(0LL, 8LL, 2000000LL);
v4 = *((_DWORD *)&ec_slave + 7);
if ( v4 || (v5 = 1, !*((_WORD *)&ec_slave + 12)) )
{
v5 = 8;
if ( v4 <= 8 )
v5 = *((_DWORD *)&ec_slave + 7);
}
v6 = *((_DWORD *)&ec_slave + 11);
if ( v6 || (v7 = 1, !*((_WORD *)&ec_slave + 21)) )
{
v7 = 8;
if ( v6 <= 8 )
v7 = *((_DWORD *)&ec_slave + 11);
}
if ( ec_slave == 8 )
{
puts("Operational state reached for all slaves.");
dorun = 1;
v9 = 500;
do
{
printf("Processdata cycle %5d , DCtime %12lld, O:", (unsigned int)dorun, ec_DCtime);
if ( v5 > 0 )
{
v10 = 0LL;
do
printf(" %2.2x", *(unsigned __int8 *)(*((_QWORD *)&ec_slave + 4) + v10++));
while ( v5 > (int)v10 );
}
printf(" I:");
if ( v7 > 0 )
{
v11 = 0LL;
do
printf(" %2.2x", *(unsigned __int8 *)(*((_QWORD *)&ec_slave + 6) + v11++));
while ( v7 > (int)v11 );
}
putchar(10);
usleep(0x4E20u);
--v9;
}
while ( v9 );
dorun = 0;
}
else
{
puts("Not all slaves reached operational state.");
}
puts("Request safe operational state for all slaves");
ec_slave = 4;
ec_writestate(0LL);
}
puts("End redundant test, close socket");
return ec_close();
}

original_code:
void redtest(char *ifname, char *ifname2)
{
 int cnt, i, j, oloop, iloop;
 printf("Starting Redundant test\n");
 if (ec_init_redundant(ifname, ifname2))
 {
  printf("ec_init on %s succeeded.\n",ifname);
  if ( ec_config(0, &IOmap) > 0 )
  {
   printf("%d slaves found and configured.\n",ec_slavecount);
   ec_statecheck(0, EC_STATE_SAFE_OP, 2000000);
   ec_configdc();
   ec_readstate();
   for(cnt = 1; cnt <= ec_slavecount ; cnt++)
   {
    printf("Slave:%d Name:%s Output size:%3dbits Input size:%3dbits State:%2d delay:%d.%d\n",
        cnt, ec_slave[cnt].name, ec_slave[cnt].Obits, ec_slave[cnt].Ibits,
        ec_slave[cnt].state, (int)ec_slave[cnt].pdelay, ec_slave[cnt].hasdc);
    printf("         Out:%8.8x,%4d In:%8.8x,%4d\n",
        (int)ec_slave[cnt].outputs, ec_slave[cnt].Obytes, (int)ec_slave[cnt].inputs, ec_slave[cnt].Ibytes);
    if( !digout && ((ec_slave[cnt].eep_id == 0x07d43052) || (ec_slave[cnt].eep_id == 0x07d83052)))
    {
     digout = ec_slave[cnt].outputs;
    }
   }
   printf("Request operational state for all slaves\n");
   ec_slave[0].state = EC_STATE_OPERATIONAL;
   ec_writestate(0);
   ec_statecheck(0, EC_STATE_OPERATIONAL, 2000000);
   oloop = ec_slave[0].Obytes;
   if ((oloop == 0) && (ec_slave[0].Obits > 0)) oloop = 1;
   if (oloop > 8) oloop = 8;
   iloop = ec_slave[0].Ibytes;
   if ((iloop == 0) && (ec_slave[0].Ibits > 0)) iloop = 1;
   if (iloop > 8) iloop = 8;
   if (ec_slave[0].state == EC_STATE_OPERATIONAL )
   {
    printf("Operational state reached for all slaves.\n");
    dorun = 1;
    for(i = 1; i <= 500; i++)
    {
     printf("Processdata cycle %5d , DCtime %12lld, O:", dorun, ec_DCtime);
     for(j = 0 ; j < oloop; j++)
     {
      printf(" %2.2x", *(ec_slave[0].outputs + j));
     }
     printf(" I:");
     for(j = 0 ; j < iloop; j++)
     {
      printf(" %2.2x", *(ec_slave[0].inputs + j));
     }
     printf("\n");
     usleep(20000);
    }
    dorun = 0;
   }
   else
   {
    printf("Not all slaves reached operational state.\n");
   }
   printf("Request safe operational state for all slaves\n");
   ec_slave[0].state = EC_STATE_SAFE_OP;
   ec_writestate(0);
  }
  else
  {
   printf("No slaves found!\n");
  }
  printf("End redundant test, close socket\n");
  ec_close();
 }
 else
 {
  printf("No socket connection on %s\nExcecute as root\n",ifname);
 }
}

canonical_original_code:
void redtest(char *ifname, char *ifname2)
{
 int cnt; int i; int j; int oloop; int iloop;
 printf("Starting Redundant test\n");
 if (ec_init_redundant(ifname, ifname2))
 {
  printf("ec_init on %s succeeded.\n",ifname);
  if ( ec_config(0, &IOmap) > 0 )
  {
   printf("%d slaves found and configured.\n",ec_slavecount);
   ec_statecheck(0, EC_STATE_SAFE_OP, 2000000);
   ec_configdc();
   ec_readstate();
   for(cnt = 1; cnt <= ec_slavecount ; cnt++)
   {
    printf("Slave:%d Name:%s Output size:%3dbits Input size:%3dbits State:%2d delay:%d.%d\n",
        cnt, ec_slave[cnt].name, ec_slave[cnt].Obits, ec_slave[cnt].Ibits,
        ec_slave[cnt].state, (int)ec_slave[cnt].pdelay, ec_slave[cnt].hasdc);
    printf("         Out:%8.8x,%4d In:%8.8x,%4d\n",
        (int)ec_slave[cnt].outputs, ec_slave[cnt].Obytes, (int)ec_slave[cnt].inputs, ec_slave[cnt].Ibytes);
    if( !digout && ((ec_slave[cnt].eep_id == 0x07d43052) || (ec_slave[cnt].eep_id == 0x07d83052)))
    {
     digout = ec_slave[cnt].outputs;
    }
   }
   printf("Request operational state for all slaves\n");
   ec_slave[0].state = EC_STATE_OPERATIONAL;
   ec_writestate(0);
   ec_statecheck(0, EC_STATE_OPERATIONAL, 2000000);
   oloop = ec_slave[0].Obytes;
   if ((oloop == 0) && (ec_slave[0].Obits > 0)) oloop = 1;
   if (oloop > 8) oloop = 8;
   iloop = ec_slave[0].Ibytes;
   if ((iloop == 0) && (ec_slave[0].Ibits > 0)) iloop = 1;
   if (iloop > 8) iloop = 8;
   if (ec_slave[0].state == EC_STATE_OPERATIONAL )
   {
    printf("Operational state reached for all slaves.\n");
    dorun = 1;
    for(i = 1; i <= 500; i++)
    {
     printf("Processdata cycle %5d , DCtime %12lld, O:", dorun, ec_DCtime);
     for(j = 0 ; j < oloop; j++)
     {
      printf(" %2.2x", *(ec_slave[0].outputs + j));
     }
     printf(" I:");
     for(j = 0 ; j < iloop; j++)
     {
      printf(" %2.2x", *(ec_slave[0].inputs + j));
     }
     printf("\n");
     usleep(20000);
    }
    dorun = 0;
   }
   else
   {
    printf("Not all slaves reached operational state.\n");
   }
   printf("Request safe operational state for all slaves\n");
   ec_slave[0].state = EC_STATE_SAFE_OP;
   ec_writestate(0);
  }
  else
  {
   printf("No slaves found!\n");
  }
  printf("End redundant test, 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: i int

variable_types: j int

variable_types: oloop int

variable_types: iloop int


original_code:
void add_timespec(struct timespec *ts, int64 addtime)
{
 int64 sec, nsec;
 nsec = addtime % 1000000000;
 sec = (addtime - nsec) / 1000000000;
 ts->tv_sec += sec;
 ts->tv_nsec += nsec;
 if ( ts->tv_nsec > 1000000000 )
 {
  nsec = ts->tv_nsec % 1000000000;
  ts->tv_sec += (ts->tv_nsec - nsec) / 1000000000;
  ts->tv_nsec = nsec;
 }
}

canonical_original_code:
void add_timespec(struct timespec *ts, long long addtime)
{
 long long sec; long long nsec;
 nsec = addtime % 1000000000;
 sec = (addtime - nsec) / 1000000000;
 ts->tv_sec += sec;
 ts->tv_nsec += nsec;
 if ( ts->tv_nsec > 1000000000 )
 {
  nsec = ts->tv_nsec % 1000000000;
  ts->tv_sec += (ts->tv_nsec - nsec) / 1000000000;
  ts->tv_nsec = nsec;
 }
}

variable_types: struct timespec { long tv_sec; long tv_nsec; } *

variable_types:
addtime
long long

variable_types:
sec
long long

variable_types:
nsec
long long


decompiled_code:
char *__fastcall SDO2string(unsigned __int16 a1, unsigned __int16 a2, unsigned __int8 a3, __int16 a4)
{
char *result; // rax
__int64 v6; // rbx
char v7[44]; // [rsp+8h] [rbp-38h] BYREF
int v8[3]; // [rsp+34h] [rbp-Ch] BYREF

v8[0] = 127;
memset(usdo, 0, 0x80uLL);
ec_SDOread(a1, a2, a3, 0LL, v8);
if ( EcatError )
return (char *)ec_elist2string();
switch ( a4 )
{
case 1:
if ( usdo[0] )
strcpy(hstr, "TRUE");
else
strcpy(hstr, "FALSE");
result = hstr;
break;
case 2:
sprintf(hstr, "0x%2.2x %d", (unsigned int)usdo[0], (unsigned int)usdo[0]);
result = hstr;
break;
case 3:
sprintf(hstr, "0x%4.4x %d", (unsigned int)*(__int16 *)usdo, (unsigned int)*(__int16 *)usdo);
result = hstr;
break;
case 4:
case 16:
sprintf(hstr, "0x%8.8x %d", *(unsigned int *)usdo, *(unsigned int *)usdo);
result = hstr;
break;
case 5:
sprintf(hstr, "0x%2.2x %u", (unsigned __int8)usdo[0], (unsigned __int8)usdo[0]);
result = hstr;
break;
case 6:
sprintf(hstr, "0x%4.4x %u", *(unsigned __int16 *)usdo, *(unsigned __int16 *)usdo);
result = hstr;
break;
case 7:
case 22:
sprintf(hstr, "0x%8.8x %u", *(unsigned int *)usdo, *(unsigned int *)usdo);
result = hstr;
break;
case 8:
sprintf(hstr, "%f", *(float *)usdo);
result = hstr;
break;
case 9:
strcpy(hstr, usdo);
result = hstr;
break;
case 10:
hstr[0] = 0;
if ( v8[0] <= 0 )
{
result = hstr;
}
else
{
v6 = 0LL;
do
{
sprintf(v7, "0x%2.2x ", (unsigned int)usdo[v6]);
strcat(hstr, v7);
++v6;
}
while ( v8[0] > (int)v6 );
result = hstr;
}
break;
case 17:
sprintf(hstr, "%f", *(double *)usdo);
result = hstr;
break;
case 21:
sprintf(hstr, "0x%16.16llx %lld", *(_QWORD *)usdo, *(_QWORD *)usdo);
result = hstr;
break;
case 27:
sprintf(hstr, "0x%16.16llx %llu", *(_QWORD *)usdo, *(_QWORD *)usdo);
result = hstr;
break;
case 48:
case 49:
case 50:
case 51:
case 52:
case 53:
case 54:
case 55:
sprintf(hstr, "0x%x", (unsigned __int8)usdo[0]);
result = hstr;
break;
default:
strcpy(hstr, "Unknown type");
result = hstr;
break;
}
return result;
}

canonical_decompiled_code:
char *__fastcall func0(unsigned __int16 a1, unsigned __int16 a2, unsigned __int8 a3, __int16 a4)
{
char *result; // rax
__int64 v6; // rbx
char v7[44]; // [rsp+8h] [rbp-38h] BYREF
int v8[3]; // [rsp+34h] [rbp-Ch] BYREF

v8[0] = 127;
memset(usdo, 0, 0x80uLL);
ec_SDOread(a1, a2, a3, 0LL, v8);
if ( EcatError )
return (char *)ec_elist2string();
switch ( a4 )
{
case 1:
if ( usdo[0] )
strcpy(hstr, "TRUE");
else
strcpy(hstr, "FALSE");
result = hstr;
break;
case 2:
sprintf(hstr, "0x%2.2x %d", (unsigned int)usdo[0], (unsigned int)usdo[0]);
result = hstr;
break;
case 3:
sprintf(hstr, "0x%4.4x %d", (unsigned int)*(__int16 *)usdo, (unsigned int)*(__int16 *)usdo);
result = hstr;
break;
case 4:
case 16:
sprintf(hstr, "0x%8.8x %d", *(unsigned int *)usdo, *(unsigned int *)usdo);
result = hstr;
break;
case 5:
sprintf(hstr, "0x%2.2x %u", (unsigned __int8)usdo[0], (unsigned __int8)usdo[0]);
result = hstr;
break;
case 6:
sprintf(hstr, "0x%4.4x %u", *(unsigned __int16 *)usdo, *(unsigned __int16 *)usdo);
result = hstr;
break;
case 7:
case 22:
sprintf(hstr, "0x%8.8x %u", *(unsigned int *)usdo, *(unsigned int *)usdo);
result = hstr;
break;
case 8:
sprintf(hstr, "%f", *(float *)usdo);
result = hstr;
break;
case 9:
strcpy(hstr, usdo);
result = hstr;
break;
case 10:
hstr[0] = 0;
if ( v8[0] <= 0 )
{
result = hstr;
}
else
{
v6 = 0LL;
do
{
sprintf(v7, "0x%2.2x ", (unsigned int)usdo[v6]);
strcat(hstr, v7);
++v6;
}
while ( v8[0] > (int)v6 );
result = hstr;
}
break;
case 17:
sprintf(hstr, "%f", *(double *)usdo);
result = hstr;
break;
case 21:
sprintf(hstr, "0x%16.16llx %lld", *(_QWORD *)usdo, *(_QWORD *)usdo);
result = hstr;
break;
case 27:
sprintf(hstr, "0x%16.16llx %llu", *(_QWORD *)usdo, *(_QWORD *)usdo);
result = hstr;
break;
case 48:
case 49:
case 50:
case 51:
case 52:
case 53:
case 54:
case 55:
sprintf(hstr, "0x%x", (unsigned __int8)usdo[0]);
result = hstr;
break;
default:
strcpy(hstr, "Unknown type");
result = hstr;
break;
}
return result;
}

original_code:
char* SDO2string(uint16 slave, uint16 index, uint8 subidx, uint16 dtype)
{
 int l = sizeof(usdo) - 1, i;
 uint8 *u8;
 int8 *i8;
 uint16 *u16;
 int16 *i16;
 uint32 *u32;
 int32 *i32;
 uint64 *u64;
 int64 *i64;
 float *sr;
 double *dr;
 char es[32];
 memset(&usdo, 0, 128);
 ec_SDOread(slave, index, subidx, 0, &l, &usdo, 700000);
 if (EcatError)
 {
  return ec_elist2string();
 }
 else
 {
  switch(dtype)
  {
   case ECT_BOOLEAN:
    u8 = (uint8*) &usdo[0];
    if (*u8) sprintf(hstr, "TRUE");
     else sprintf(hstr, "FALSE");
    break;
   case ECT_INTEGER8:
    i8 = (int8*) &usdo[0];
    sprintf(hstr, "0x%2.2x %d", *i8, *i8);
    break;
   case ECT_INTEGER16:
    i16 = (int16*) &usdo[0];
    sprintf(hstr, "0x%4.4x %d", *i16, *i16);
    break;
   case ECT_INTEGER32:
   case ECT_INTEGER24:
    i32 = (int32*) &usdo[0];
    sprintf(hstr, "0x%8.8x %d", *i32, *i32);
    break;
   case ECT_INTEGER64:
    i64 = (int64*) &usdo[0];
    sprintf(hstr, "0x%16.16llx %lld", *i64, *i64);
    break;
   case ECT_UNSIGNED8:
    u8 = (uint8*) &usdo[0];
    sprintf(hstr, "0x%2.2x %u", *u8, *u8);
    break;
   case ECT_UNSIGNED16:
    u16 = (uint16*) &usdo[0];
    sprintf(hstr, "0x%4.4x %u", *u16, *u16);
    break;
   case ECT_UNSIGNED32:
   case ECT_UNSIGNED24:
    u32 = (uint32*) &usdo[0];
    sprintf(hstr, "0x%8.8x %u", *u32, *u32);
    break;
   case ECT_UNSIGNED64:
    u64 = (uint64*) &usdo[0];
    sprintf(hstr, "0x%16.16llx %llu", *u64, *u64);
    break;
   case ECT_REAL32:
    sr = (float*) &usdo[0];
    sprintf(hstr, "%f", *sr);
    break;
   case ECT_REAL64:
    dr = (double*) &usdo[0];
    sprintf(hstr, "%f", *dr);
    break;
   case ECT_BIT1:
   case ECT_BIT2:
   case ECT_BIT3:
   case ECT_BIT4:
   case ECT_BIT5:
   case ECT_BIT6:
   case ECT_BIT7:
   case ECT_BIT8:
    u8 = (uint8*) &usdo[0];
    sprintf(hstr, "0x%x", *u8);
    break;
   case ECT_VISIBLE_STRING:
    strcpy(hstr, usdo);
    break;
   case ECT_OCTET_STRING:
    hstr[0] = 0x00;
    for (i = 0 ; i < l ; i++)
    {
     sprintf(es, "0x%2.2x ", usdo[i]);
     strcat( hstr, es);
    }
    break;
   default:
    sprintf(hstr, "Unknown type");
  }
  return hstr;
 }
}

canonical_original_code:
char *SDO2string(unsigned short slave, unsigned short index, unsigned char subidx, unsigned short dtype)
{
 int l = sizeof(usdo) - 1; int i;
 unsigned char *u8;
 char *i8;
 unsigned short *u16;
 short *i16;
 unsigned int *u32;
 int *i32;
 unsigned long long *u64;
 long long *i64;
 float *sr;
 double *dr;
 char es[32];
 memset(&usdo, 0, 128);
 ec_SDOread(slave, index, subidx, 0, &l, &usdo, 700000);
 if (EcatError)
 {
  return ec_elist2string();
 }
 else
 {
  switch(dtype)
  {
   case ECT_BOOLEAN:
    u8 = (unsigned char *) &usdo[0];
    if (*u8) sprintf(hstr, "TRUE");
     else sprintf(hstr, "FALSE");
    break;
   case ECT_INTEGER8:
    i8 = (char *) &usdo[0];
    sprintf(hstr, "0x%2.2x %d", *i8, *i8);
    break;
   case ECT_INTEGER16:
    i16 = (short *) &usdo[0];
    sprintf(hstr, "0x%4.4x %d", *i16, *i16);
    break;
   case ECT_INTEGER32:
   case ECT_INTEGER24:
    i32 = (int *) &usdo[0];
    sprintf(hstr, "0x%8.8x %d", *i32, *i32);
    break;
   case ECT_INTEGER64:
    i64 = (long long *) &usdo[0];
    sprintf(hstr, "0x%16.16llx %lld", *i64, *i64);
    break;
   case ECT_UNSIGNED8:
    u8 = (unsigned char *) &usdo[0];
    sprintf(hstr, "0x%2.2x %u", *u8, *u8);
    break;
   case ECT_UNSIGNED16:
    u16 = (unsigned short *) &usdo[0];
    sprintf(hstr, "0x%4.4x %u", *u16, *u16);
    break;
   case ECT_UNSIGNED32:
   case ECT_UNSIGNED24:
    u32 = (unsigned int *) &usdo[0];
    sprintf(hstr, "0x%8.8x %u", *u32, *u32);
    break;
   case ECT_UNSIGNED64:
    u64 = (unsigned long long *) &usdo[0];
    sprintf(hstr, "0x%16.16llx %llu", *u64, *u64);
    break;
   case ECT_REAL32:
    sr = (float *) &usdo[0];
    sprintf(hstr, "%f", *sr);
    break;
   case ECT_REAL64:
    dr = (double *) &usdo[0];
    sprintf(hstr, "%f", *dr);
    break;
   case ECT_BIT1:
   case ECT_BIT2:
   case ECT_BIT3:
   case ECT_BIT4:
   case ECT_BIT5:
   case ECT_BIT6:
   case ECT_BIT7:
   case ECT_BIT8:
    u8 = (unsigned char *) &usdo[0];
    sprintf(hstr, "0x%x", *u8);
    break;
   case ECT_VISIBLE_STRING:
    strcpy(hstr, usdo);
    break;
   case ECT_OCTET_STRING:
    hstr[0] = 0x00;
    for (i = 0 ; i < l ; i++)
    {
     sprintf(es, "0x%2.2x ", usdo[i]);
     strcat( hstr, es);
    }
    break;
   default:
    sprintf(hstr, "Unknown type");
  }
  return hstr;
 }
}

variable_types:
slave
unsigned short

variable_types:
index
unsigned short

variable_types:
subidx
unsigned char

variable_types:
dtype
unsigned short

variable_types: l int

variable_types:
sr
float *

variable_types:
dr
double *

variable_types:
es
char [32]

return_type: char *


slaveinfo

decompiled_code:
int __fastcall slaveinfo(const char *a1)
{
__int64 v1; // rbx
unsigned int v2; // er13
unsigned __int16 *v3; // rbx
int v4; // ebp
const char *v5; // rax
__int64 i; // rbp
unsigned __int16 v7; // dx
char *v8; // rbp
int v9; // er12
__int16 v10; // ax
__int16 v11; // bp
__int16 v12; // ax
const char *v13; // rax
__int64 v14; // r14
__int64 v15; // rbp
unsigned __int64 v16; // r12
unsigned __int16 v17; // dx
unsigned __int16 v18; // cx
__int64 v19; // r15
char *v20; // rax
const char *v21; // rax
const char *v22; // rax
const char *v24; // [rsp+8h] [rbp-40h]

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 )
{
v3 = (unsigned __int16 *)(unsigned int)((_DWORD)&ec_slave + 320);
v4 = 1;
do
{
if ( *v3 != 4 )
{
v5 = (const char *)ec_ALstatuscode2string(v3[1]);
printf("Slave %d State=%2x StatusCode=%4x : %s\n", (unsigned int)v4, *v3, v3[1], v5);
}
++v4;
v3 += 160;
}
while ( ec_slavecount >= v4 );
}
}
ec_configdc();
ec_readstate();
if ( ec_slavecount > 0 )
{
v1 = 320LL;
v2 = 1;
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));
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));
for ( i = 0LL; i != 8; ++i )
{
v7 = *(_WORD *)((char *)&ec_slave + 8 * i + v1 + 57);
if ( v7 )
printf(
" SM%1d A:%4.4x L:%4d F:%8.8x Type:%d\n",
(unsigned int)i,
v7,
*(unsigned __int16 *)((char *)&ec_slave + 8 * i + v1 + 59),
*(unsigned int *)((char *)&ec_slave + 8 * i + v1 + 61),
*((unsigned __int8 *)&ec_slave + v1 + i + 121));
}
if ( *((_BYTE *)&ec_slave + v1 + 268) )
{
v8 = (char *)&ec_slave + v1;
v9 = 0;
do
{
printf(
" FMMU%1d Ls:%8.8x Ll:%4d Lsb:%d Leb:%d Ps:%4.4x Psb:%d Ty:%2.2x Act:%2.2x\n",
(unsigned int)v9++,
*(unsigned int *)(v8 + 129),
*(unsigned __int16 *)(v8 + 133),
(unsigned __int8)v8[135],
(unsigned __int8)v8[136],
*(unsigned __int16 *)(v8 + 137),
(unsigned __int8)v8[139],
(unsigned __int8)v8[140],
(unsigned __int8)v8[141]);
v8 += 16;
}
while ( *((unsigned __int8 *)&ec_slave + v1 + 268) > v9 );
}
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));
v10 = ec_siifind(v2, 30LL);
v11 = v10;
if ( v10 )
{
*((_BYTE *)&ec_slave + v1 + 260) = ec_siigetbyte(v2, (unsigned __int16)(v10 + 7));
*((_BYTE *)&ec_slave + v1 + 261) = ec_siigetbyte(v2, (unsigned __int16)(v11 + 8));
*((_BYTE *)&ec_slave + v1 + 262) = ec_siigetbyte(v2, (unsigned __int16)(v11 + 9));
*((_BYTE *)&ec_slave + v1 + 263) = ec_siigetbyte(v2, (unsigned __int16)(v11 + 10));
if ( (ec_siigetbyte(v2, (unsigned __int16)(v11 + 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)(v11 + 14));
v12 = *(_WORD *)((char *)&ec_slave + v1 + 264)
+ ((unsigned __int16)ec_siigetbyte(v2, (unsigned __int16)(v11 + 15)) << 8);
*(_WORD *)((char *)&ec_slave + v1 + 264) = v12;
*((_WORD *)&ec_slave + 132) += v12;
}
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);
if ( (unsigned int)ec_readODlist(v2, ODlist) )
{
printf(" CoE Object Description found, %d entries.\n", (unsigned __int16)ODlist[1]);
if ( ODlist[1] )
{
v24 = (const char *)&ODlist[3074];
v14 = 0LL;
do
{
ec_readODdescription((unsigned int)v14, ODlist);
while ( EcatError )
{
v21 = (const char *)ec_elist2string();
printf("%s", v21);
}
printf(
" Index: %4.4x Datatype: %4.4x Objectcode: %2.2x Name: %s\n",
(unsigned __int16)ODlist[v14 + 2],
(unsigned __int16)ODlist[v14 + 1026],
*((unsigned __int8 *)&ODlist[2050] + v14),
v24);
memset(OElist, 0, 0x2C02uLL);
ec_readOE((unsigned int)v14, ODlist, OElist);
while ( EcatError )
{
v22 = (const char *)ec_elist2string();
printf("%s", v22);
}
v16 = (unsigned int)((_DWORD)OElist + 1794);
v15 = 0LL;
do
{
v17 = OElist[v15 + 129];
if ( v17 )
{
v18 = OElist[v15 + 385];
if ( v18 )
{
v19 = (unsigned int)((_DWORD)OElist + 1282);
printf(
" Sub: %2.2x Datatype: %4.4x Bitlength: %4.4x Obj.access: %4.4x Name: %s\n",
(unsigned int)v15,
v17,
v18,
*(unsigned __int16 *)(v19 + 2 * v15),
(const char *)v16);
if ( (*(_BYTE *)(v19 + 2 * v15) & 7) != 0 )
{
v20 = SDO2string(v2, ODlist[v14 + 2], v15, OElist[v15 + 129]);
printf(" Value :%s\n", v20);
}
}
}
++v15;
v16 += 37LL;
}
while ( *((unsigned __int8 *)&ODlist[2562] + v14) >= (int)v15 );
++v14;
v24 += 37;
}
while ( (unsigned __int16)ODlist[1] > (int)v14 );
}
}
else
{
while ( EcatError )
{
v13 = (const char *)ec_elist2string();
printf("%s", v13);
}
}
}
++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; // er13
unsigned __int16 *v3; // rbx
int v4; // ebp
const char *v5; // rax
__int64 i; // rbp
unsigned __int16 v7; // dx
char *v8; // rbp
int v9; // er12
__int16 v10; // ax
__int16 v11; // bp
__int16 v12; // ax
const char *v13; // rax
__int64 v14; // r14
__int64 v15; // rbp
unsigned __int64 v16; // r12
unsigned __int16 v17; // dx
unsigned __int16 v18; // cx
__int64 v19; // r15
char *v20; // rax
const char *v21; // rax
const char *v22; // rax
const char *v24; // [rsp+8h] [rbp-40h]

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 )
{
v3 = (unsigned __int16 *)(unsigned int)((_DWORD)&ec_slave + 320);
v4 = 1;
do
{
if ( *v3 != 4 )
{
v5 = (const char *)ec_ALstatuscode2string(v3[1]);
printf("Slave %d State=%2x StatusCode=%4x : %s\n", (unsigned int)v4, *v3, v3[1], v5);
}
++v4;
v3 += 160;
}
while ( ec_slavecount >= v4 );
}
}
ec_configdc();
ec_readstate();
if ( ec_slavecount > 0 )
{
v1 = 320LL;
v2 = 1;
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));
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));
for ( i = 0LL; i != 8; ++i )
{
v7 = *(_WORD *)((char *)&ec_slave + 8 * i + v1 + 57);
if ( v7 )
printf(
" SM%1d A:%4.4x L:%4d F:%8.8x Type:%d\n",
(unsigned int)i,
v7,
*(unsigned __int16 *)((char *)&ec_slave + 8 * i + v1 + 59),
*(unsigned int *)((char *)&ec_slave + 8 * i + v1 + 61),
*((unsigned __int8 *)&ec_slave + v1 + i + 121));
}
if ( *((_BYTE *)&ec_slave + v1 + 268) )
{
v8 = (char *)&ec_slave + v1;
v9 = 0;
do
{
printf(
" FMMU%1d Ls:%8.8x Ll:%4d Lsb:%d Leb:%d Ps:%4.4x Psb:%d Ty:%2.2x Act:%2.2x\n",
(unsigned int)v9++,
*(unsigned int *)(v8 + 129),
*(unsigned __int16 *)(v8 + 133),
(unsigned __int8)v8[135],
(unsigned __int8)v8[136],
*(unsigned __int16 *)(v8 + 137),
(unsigned __int8)v8[139],
(unsigned __int8)v8[140],
(unsigned __int8)v8[141]);
v8 += 16;
}
while ( *((unsigned __int8 *)&ec_slave + v1 + 268) > v9 );
}
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));
v10 = ec_siifind(v2, 30LL);
v11 = v10;
if ( v10 )
{
*((_BYTE *)&ec_slave + v1 + 260) = ec_siigetbyte(v2, (unsigned __int16)(v10 + 7));
*((_BYTE *)&ec_slave + v1 + 261) = ec_siigetbyte(v2, (unsigned __int16)(v11 + 8));
*((_BYTE *)&ec_slave + v1 + 262) = ec_siigetbyte(v2, (unsigned __int16)(v11 + 9));
*((_BYTE *)&ec_slave + v1 + 263) = ec_siigetbyte(v2, (unsigned __int16)(v11 + 10));
if ( (ec_siigetbyte(v2, (unsigned __int16)(v11 + 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)(v11 + 14));
v12 = *(_WORD *)((char *)&ec_slave + v1 + 264)
+ ((unsigned __int16)ec_siigetbyte(v2, (unsigned __int16)(v11 + 15)) << 8);
*(_WORD *)((char *)&ec_slave + v1 + 264) = v12;
*((_WORD *)&ec_slave + 132) += v12;
}
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);
if ( (unsigned int)ec_readODlist(v2, ODlist) )
{
printf(" CoE Object Description found, %d entries.\n", (unsigned __int16)ODlist[1]);
if ( ODlist[1] )
{
v24 = (const char *)&ODlist[3074];
v14 = 0LL;
do
{
ec_readODdescription((unsigned int)v14, ODlist);
while ( EcatError )
{
v21 = (const char *)ec_elist2string();
printf("%s", v21);
}
printf(
" Index: %4.4x Datatype: %4.4x Objectcode: %2.2x Name: %s\n",
(unsigned __int16)ODlist[v14 + 2],
(unsigned __int16)ODlist[v14 + 1026],
*((unsigned __int8 *)&ODlist[2050] + v14),
v24);
memset(OElist, 0, 0x2C02uLL);
ec_readOE((unsigned int)v14, ODlist, OElist);
while ( EcatError )
{
v22 = (const char *)ec_elist2string();
printf("%s", v22);
}
v16 = (unsigned int)((_DWORD)OElist + 1794);
v15 = 0LL;
do
{
v17 = OElist[v15 + 129];
if ( v17 )
{
v18 = OElist[v15 + 385];
if ( v18 )
{
v19 = (unsigned int)((_DWORD)OElist + 1282);
printf(
" Sub: %2.2x Datatype: %4.4x Bitlength: %4.4x Obj.access: %4.4x Name: %s\n",
(unsigned int)v15,
v17,
v18,
*(unsigned __int16 *)(v19 + 2 * v15),
(const char *)v16);
if ( (*(_BYTE *)(v19 + 2 * v15) & 7) != 0 )
{
v20 = func0(v2, ODlist[v14 + 2], v15, OElist[v15 + 129]);
printf(" Value :%s\n", v20);
}
}
}
++v15;
v16 += 37LL;
}
while ( *((unsigned __int8 *)&ODlist[2562] + v14) >= (int)v15 );
++v14;
v24 += 37;
}
while ( (unsigned __int16)ODlist[1] > (int)v14 );
}
}
else
{
while ( EcatError )
{
v13 = (const char *)ec_elist2string();
printf("%s", v13);
}
}
}
++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

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 *[]


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


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; // ebp

v3 = (unsigned int)ptsname(0);
if ( !v3 || (v4 = v3, grantpt(0)) || (v5 = unlockpt(0)) != 0 )
{
perror("pseudotty");
v5 = 2;
}
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


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

memset(savedregs, 0, 0x200uLL);
v1 = send(fd, savedregs, 0x200uLL, 0);
printf("send %d bytes\n", v1);
v2 = recv(fd, savedregs, 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 savedregs[536]; // [rsp+0h] [rbp+0h] BYREF

memset(savedregs, 0, 0x200uLL);
v1 = send(fd, savedregs, 0x200uLL, 0);
printf("send %d bytes\n", v1);
v2 = recv(fd, savedregs, 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[1032]; // [rsp+0h] [rbp-408h] 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[1032]; // [rsp+0h] [rbp-408h] 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]


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");
}