eepromtool

decompiled_code:
int __fastcall eepromtool(const char *a1, int a2, int a3, const char *a4)
{
int v7; // eax
int v8; // eax
__time_t v9; // rax
__suseconds_t v10; // rcx
__int64 v12; // rsi
__int64 v13; // rdx
__time_t v14; // rdx
__suseconds_t v15; // rax
int v16; // [rsp+4h] [rbp-34h] BYREF
int v17; // [rsp+8h] [rbp-30h] BYREF
int v18[11]; // [rsp+Ch] [rbp-2Ch] BYREF

if ( !(unsigned int)ec_init() )
return printf("No socket connection on %s\nExcecute as root\n", a1);
printf("ec_init on %s succeeded.\n", a1);
v16 = 0;
v7 = ec_BRD(0LL, 0LL, 4LL, &v16, 20000LL);
wkc = v7;
if ( v7 <= 0 )
{
puts("No slaves found!");
}
else
{
ec_slavecount = v7;
printf("%d slaves found.\n", (unsigned int)v7);
if ( ec_slavecount < a2 || a2 <= 0 )
{
puts("Slave number outside range.");
}
else if ( (unsigned int)(a3 - 1) <= 1 )
{
gettimeofday(&tstart, 0LL);
eeprom_read(a2, 0, 128);
printf("Slave %d data\n", (unsigned int)a2);
printf(" PDI Control : %4.4X\n", (unsigned __int16)ebuf[0]);
printf(" PDI Config : %4.4X\n", (unsigned __int16)ebuf[1]);
printf(" Config Alias : %4.4X\n", (unsigned __int16)ebuf[4]);
printf(" Checksum : %4.4X\n", (unsigned __int16)ebuf[7]);
printf(" Vendor ID : %8.8X\n", *(unsigned int *)&ebuf[8]);
printf(" Product Code : %8.8X\n", *(unsigned int *)&ebuf[10]);
printf(" Revision Number : %8.8X\n", *(unsigned int *)&ebuf[12]);
printf(" Serial Number : %8.8X\n", *(unsigned int *)&ebuf[14]);
printf(" Mailbox Protocol : %4.4X\n", (unsigned __int16)ebuf[28]);
v12 = (unsigned __int16)ebuf[62];
v13 = (unsigned int)(((_DWORD)v12 + 1) << 7);
if ( (int)v12 + 1 > 256 )
{
v18[0] = 0x8000;
v13 = 0x8000LL;
}
else
{
v18[0] = ((_DWORD)v12 + 1) << 7;
}
printf(" Size : %4.4X = %d bytes\n", v12, v13);
printf(" Version : %4.4X\n", (unsigned __int16)ebuf[63]);
if ( v18[0] > 128 )
eeprom_read(a2, 128, v18[0] - 128);
gettimeofday(&tend, 0LL);
v14 = tend.tv_sec - tstart.tv_sec;
v15 = tend.tv_usec - tstart.tv_usec;
tdif = tend.tv_sec - tstart.tv_sec;
*(&tdif + 1) = v15;
if ( v15 < 0 )
{
tdif = v14 - 1;
*(&tdif + 1) = v15 + 1000000;
}
if ( a3 == 2 )
output_intelhex(a4, v18[0]);
else
output_bin(a4, v18[0]);
printf("\nTotal EEPROM read time :%ldms\n", (*(&tdif + 1) + 1000000 * tdif) / 1000);
}
else if ( (unsigned int)(a3 - 3) <= 1 )
{
v17 = 0;
if ( a3 == 4 )
v8 = input_intelhex(a4, &v17, v18);
else
v8 = input_bin(a4, v18);
if ( v8 <= 0 )
{
puts("Error reading file, abort.");
}
else
{
printf("Slave %d\n", (unsigned int)a2);
printf(" Vendor ID : %8.8X\n", *(unsigned int *)&ebuf[8]);
printf(" Product Code : %8.8X\n", *(unsigned int *)&ebuf[10]);
printf(" Revision Number : %8.8X\n", *(unsigned int *)&ebuf[12]);
printf(" Serial Number : %8.8X\n", *(unsigned int *)&ebuf[14]);
printf("Busy");
fflush(stdout);
gettimeofday(&tstart, 0LL);
eeprom_write(a2, v17, v18[0]);
gettimeofday(&tend, 0LL);
v9 = tend.tv_sec - tstart.tv_sec;
v10 = tend.tv_usec - tstart.tv_usec;
tdif = tend.tv_sec - tstart.tv_sec;
*(&tdif + 1) = v10;
if ( v10 < 0 )
{
--v9;
v10 += 1000000LL;
tdif = v9;
*(&tdif + 1) = v10;
}
printf("\nTotal EEPROM write time :%ldms\n", (1000000 * v9 + v10) / 1000);
}
}
}
puts("End, close socket");
return ec_close();
}

canonical_decompiled_code:
int __fastcall func6(const char *a1, int a2, int a3, const char *a4)
{
int v7; // eax
int v8; // eax
__time_t v9; // rax
__suseconds_t v10; // rcx
__int64 v12; // rsi
__int64 v13; // rdx
__time_t v14; // rdx
__suseconds_t v15; // rax
int v16; // [rsp+4h] [rbp-34h] BYREF
int v17; // [rsp+8h] [rbp-30h] BYREF
int v18[11]; // [rsp+Ch] [rbp-2Ch] BYREF

if ( !(unsigned int)ec_init() )
return printf("No socket connection on %s\nExcecute as root\n", a1);
printf("ec_init on %s succeeded.\n", a1);
v16 = 0;
v7 = ec_BRD(0LL, 0LL, 4LL, &v16, 20000LL);
wkc = v7;
if ( v7 <= 0 )
{
puts("No slaves found!");
}
else
{
ec_slavecount = v7;
printf("%d slaves found.\n", (unsigned int)v7);
if ( ec_slavecount < a2 || a2 <= 0 )
{
puts("Slave number outside range.");
}
else if ( (unsigned int)(a3 - 1) <= 1 )
{
gettimeofday(&tstart, 0LL);
func4(a2, 0, 128);
printf("Slave %d data\n", (unsigned int)a2);
printf(" PDI Control : %4.4X\n", (unsigned __int16)ebuf[0]);
printf(" PDI Config : %4.4X\n", (unsigned __int16)ebuf[1]);
printf(" Config Alias : %4.4X\n", (unsigned __int16)ebuf[4]);
printf(" Checksum : %4.4X\n", (unsigned __int16)ebuf[7]);
printf(" Vendor ID : %8.8X\n", *(unsigned int *)&ebuf[8]);
printf(" Product Code : %8.8X\n", *(unsigned int *)&ebuf[10]);
printf(" Revision Number : %8.8X\n", *(unsigned int *)&ebuf[12]);
printf(" Serial Number : %8.8X\n", *(unsigned int *)&ebuf[14]);
printf(" Mailbox Protocol : %4.4X\n", (unsigned __int16)ebuf[28]);
v12 = (unsigned __int16)ebuf[62];
v13 = (unsigned int)(((_DWORD)v12 + 1) << 7);
if ( (int)v12 + 1 > 256 )
{
v18[0] = 0x8000;
v13 = 0x8000LL;
}
else
{
v18[0] = ((_DWORD)v12 + 1) << 7;
}
printf(" Size : %4.4X = %d bytes\n", v12, v13);
printf(" Version : %4.4X\n", (unsigned __int16)ebuf[63]);
if ( v18[0] > 128 )
func4(a2, 128, v18[0] - 128);
gettimeofday(&tend, 0LL);
v14 = tend.tv_sec - tstart.tv_sec;
v15 = tend.tv_usec - tstart.tv_usec;
tdif = tend.tv_sec - tstart.tv_sec;
*(&tdif + 1) = v15;
if ( v15 < 0 )
{
tdif = v14 - 1;
*(&tdif + 1) = v15 + 1000000;
}
if ( a3 == 2 )
func3(a4, v18[0]);
else
func2(a4, v18[0]);
printf("\nTotal EEPROM read time :%ldms\n", (*(&tdif + 1) + 1000000 * tdif) / 1000);
}
else if ( (unsigned int)(a3 - 3) <= 1 )
{
v17 = 0;
if ( a3 == 4 )
v8 = func1(a4, &v17, v18);
else
v8 = func0(a4, v18);
if ( v8 <= 0 )
{
puts("Error reading file, abort.");
}
else
{
printf("Slave %d\n", (unsigned int)a2);
printf(" Vendor ID : %8.8X\n", *(unsigned int *)&ebuf[8]);
printf(" Product Code : %8.8X\n", *(unsigned int *)&ebuf[10]);
printf(" Revision Number : %8.8X\n", *(unsigned int *)&ebuf[12]);
printf(" Serial Number : %8.8X\n", *(unsigned int *)&ebuf[14]);
printf("Busy");
fflush(stdout);
gettimeofday(&tstart, 0LL);
func5(a2, v17, v18[0]);
gettimeofday(&tend, 0LL);
v9 = tend.tv_sec - tstart.tv_sec;
v10 = tend.tv_usec - tstart.tv_usec;
tdif = tend.tv_sec - tstart.tv_sec;
*(&tdif + 1) = v10;
if ( v10 < 0 )
{
--v9;
v10 += 1000000LL;
tdif = v9;
*(&tdif + 1) = v10;
}
printf("\nTotal EEPROM write time :%ldms\n", (1000000 * v9 + v10) / 1000);
}
}
}
puts("End, close socket");
return ec_close();
}

original_code:
void eepromtool(char *ifname, int slave, int mode, char *fname)
{
 int w, rc = 0, estart, esize;
 uint16 *wbuf;
 if (ec_init(ifname))
 {
  printf("ec_init on %s succeeded.\n",ifname);
  w = 0x0000;
     wkc = ec_BRD(0x0000, ECT_REG_TYPE, sizeof(w), &w, 20000);
     if (wkc > 0)
     {
   ec_slavecount = wkc;
   printf("%d slaves found.\n",ec_slavecount);
   if((ec_slavecount >= slave) && (slave > 0))
   {
    if ((mode == 1) || (mode == 2))
    {
     rc = gettimeofday(&tstart, ((void *)0));
     eeprom_read(slave, 0x0000, 128);
     wbuf = (uint16 *)&ebuf[0];
     printf("Slave %d data\n", slave);
     printf(" PDI Control      : %4.4X\n",*(wbuf + 0x00));
     printf(" PDI Config       : %4.4X\n",*(wbuf + 0x01));
     printf(" Config Alias     : %4.4X\n",*(wbuf + 0x04));
     printf(" Checksum         : %4.4X\n",*(wbuf + 0x07));
     printf(" Vendor ID        : %8.8X\n",*(uint32 *)(wbuf + 0x08));
     printf(" Product Code     : %8.8X\n",*(uint32 *)(wbuf + 0x0A));
     printf(" Revision Number  : %8.8X\n",*(uint32 *)(wbuf + 0x0C));
     printf(" Serial Number    : %8.8X\n",*(uint32 *)(wbuf + 0x0E));
     printf(" Mailbox Protocol : %4.4X\n",*(wbuf + 0x1C));
     esize = (*(wbuf + 0x3E) + 1) * 128;
     if (esize > 32768) esize = 32768;
     printf(" Size             : %4.4X = %d bytes\n",*(wbuf + 0x3E), esize);
     printf(" Version          : %4.4X\n",*(wbuf + 0x3F));
     if (esize > 128)
      eeprom_read(slave, 128, esize - 128);
     rc = gettimeofday(&tend, ((void *)0));
     do { (&tdif)->tv_sec = (&tend)->tv_sec - (&tstart)->tv_sec; (&tdif)->tv_usec = (&tend)->tv_usec - (&tstart)->tv_usec; if ((&tdif)->tv_usec < 0) { --(&tdif)->tv_sec; (&tdif)->tv_usec += 1000000; } } while (0);
     if (mode == 2) output_intelhex(fname, esize);
     if (mode == 1) output_bin(fname, esize);
     printf("\nTotal EEPROM read time :%ldms\n", (tdif.tv_usec+(tdif.tv_sec*1000000L)) / 1000);
    }
    if ((mode == 3) || (mode == 4))
    {
     estart = 0;
     if (mode == 4) rc = input_intelhex(fname, &estart, &esize);
     if (mode == 3) rc = input_bin(fname, &esize);
     if (rc > 0)
     {
      wbuf = (uint16 *)&ebuf[0];
      printf("Slave %d\n", slave);
      printf(" Vendor ID        : %8.8X\n",*(uint32 *)(wbuf + 0x08));
      printf(" Product Code     : %8.8X\n",*(uint32 *)(wbuf + 0x0A));
      printf(" Revision Number  : %8.8X\n",*(uint32 *)(wbuf + 0x0C));
      printf(" Serial Number    : %8.8X\n",*(uint32 *)(wbuf + 0x0E));
      printf("Busy");
      fflush(stdout);
      rc = gettimeofday(&tstart, ((void *)0));
      eeprom_write(slave, estart, esize);
      rc = gettimeofday(&tend, ((void *)0));
      do { (&tdif)->tv_sec = (&tend)->tv_sec - (&tstart)->tv_sec; (&tdif)->tv_usec = (&tend)->tv_usec - (&tstart)->tv_usec; if ((&tdif)->tv_usec < 0) { --(&tdif)->tv_sec; (&tdif)->tv_usec += 1000000; } } while (0);
      printf("\nTotal EEPROM write time :%ldms\n", (tdif.tv_usec+(tdif.tv_sec*1000000L)) / 1000);
     }
     else
      printf("Error reading file, abort.\n");
    }
   }
   else
    printf("Slave number outside range.\n");
  }
  else
   printf("No slaves found!\n");
  printf("End, close socket\n");
  ec_close();
 }
 else
  printf("No socket connection on %s\nExcecute as root\n",ifname);
}

canonical_original_code:
void eepromtool(char *ifname, int slave, int mode, char *fname)
{
 int w; int rc = 0; int estart; int esize;
 unsigned short *wbuf;
 if (ec_init(ifname))
 {
  printf("ec_init on %s succeeded.\n",ifname);
  w = 0x0000;
     wkc = ec_BRD(0x0000, ECT_REG_TYPE, sizeof(w), &w, 20000);
     if (wkc > 0)
     {
   ec_slavecount = wkc;
   printf("%d slaves found.\n",ec_slavecount);
   if((ec_slavecount >= slave) && (slave > 0))
   {
    if ((mode == 1) || (mode == 2))
    {
     rc = gettimeofday(&tstart, ((void *)0));
     eeprom_read(slave, 0x0000, 128);
     wbuf = (unsigned short *)&ebuf[0];
     printf("Slave %d data\n", slave);
     printf(" PDI Control      : %4.4X\n",*(wbuf + 0x00));
     printf(" PDI Config       : %4.4X\n",*(wbuf + 0x01));
     printf(" Config Alias     : %4.4X\n",*(wbuf + 0x04));
     printf(" Checksum         : %4.4X\n",*(wbuf + 0x07));
     printf(" Vendor ID        : %8.8X\n",*(unsigned int *)(wbuf + 0x08));
     printf(" Product Code     : %8.8X\n",*(unsigned int *)(wbuf + 0x0A));
     printf(" Revision Number  : %8.8X\n",*(unsigned int *)(wbuf + 0x0C));
     printf(" Serial Number    : %8.8X\n",*(unsigned int *)(wbuf + 0x0E));
     printf(" Mailbox Protocol : %4.4X\n",*(wbuf + 0x1C));
     esize = (*(wbuf + 0x3E) + 1) * 128;
     if (esize > 32768) esize = 32768;
     printf(" Size             : %4.4X = %d bytes\n",*(wbuf + 0x3E), esize);
     printf(" Version          : %4.4X\n",*(wbuf + 0x3F));
     if (esize > 128)
      eeprom_read(slave, 128, esize - 128);
     rc = gettimeofday(&tend, ((void *)0));
     do { (&tdif)->tv_sec = (&tend)->tv_sec - (&tstart)->tv_sec; (&tdif)->tv_usec = (&tend)->tv_usec - (&tstart)->tv_usec; if ((&tdif)->tv_usec < 0) { --(&tdif)->tv_sec; (&tdif)->tv_usec += 1000000; } } while (0);
     if (mode == 2) output_intelhex(fname, esize);
     if (mode == 1) output_bin(fname, esize);
     printf("\nTotal EEPROM read time :%ldms\n", (tdif.tv_usec+(tdif.tv_sec*1000000L)) / 1000);
    }
    if ((mode == 3) || (mode == 4))
    {
     estart = 0;
     if (mode == 4) rc = input_intelhex(fname, &estart, &esize);
     if (mode == 3) rc = input_bin(fname, &esize);
     if (rc > 0)
     {
      wbuf = (unsigned short *)&ebuf[0];
      printf("Slave %d\n", slave);
      printf(" Vendor ID        : %8.8X\n",*(unsigned int *)(wbuf + 0x08));
      printf(" Product Code     : %8.8X\n",*(unsigned int *)(wbuf + 0x0A));
      printf(" Revision Number  : %8.8X\n",*(unsigned int *)(wbuf + 0x0C));
      printf(" Serial Number    : %8.8X\n",*(unsigned int *)(wbuf + 0x0E));
      printf("Busy");
      fflush(stdout);
      rc = gettimeofday(&tstart, ((void *)0));
      eeprom_write(slave, estart, esize);
      rc = gettimeofday(&tend, ((void *)0));
      do { (&tdif)->tv_sec = (&tend)->tv_sec - (&tstart)->tv_sec; (&tdif)->tv_usec = (&tend)->tv_usec - (&tstart)->tv_usec; if ((&tdif)->tv_usec < 0) { --(&tdif)->tv_sec; (&tdif)->tv_usec += 1000000; } } while (0);
      printf("\nTotal EEPROM write time :%ldms\n", (tdif.tv_usec+(tdif.tv_sec*1000000L)) / 1000);
     }
     else
      printf("Error reading file, abort.\n");
    }
   }
   else
    printf("Slave number outside range.\n");
  }
  else
   printf("No slaves found!\n");
  printf("End, close socket\n");
  ec_close();
 }
 else
  printf("No socket connection on %s\nExcecute as root\n",ifname);
}

variable_types:
ifname
char *

variable_types: mode int

variable_types:
fname
char *

variable_types: w int

variable_types: rc int

variable_types: estart int

variable_types: esize int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er8
const char *v5; // rax

puts("SOEM (Simple Open EtherCAT Master)\nEEPROM tool");
if ( argc <= 4 )
{
puts("Usage: eepromtool ifname slave OPTION fname");
puts("ifname = eth0 for example");
puts("slave = slave number in EtherCAT order 1..n");
puts(" -r read EEPROM, output binary format");
puts(" -ri read EEPROM, output Intel Hex format");
puts(" -w write EEPROM, input binary format");
puts(" -wi write EEPROM, input Intel Hex format");
}
else
{
v3 = atoi(argv[2]);
mode = 0;
slave = v3;
v4 = v3;
v5 = argv[3];
if ( *(_WORD *)v5 == 29229 && !v5[2] )
mode = 1;
if ( !strcmp(v5, "-ri") )
mode = 2;
if ( *v5 == 45 && v5[1] == 119 && !v5[2] )
mode = 3;
if ( !strcmp(v5, "-wi") )
mode = 4;
eepromtool(argv[1], v4, mode, argv[4]);
}
puts("End program");
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // er8
const char *v5; // rax

puts("SOEM (Simple Open EtherCAT Master)\nEEPROM tool");
if ( argc <= 4 )
{
puts("Usage: eepromtool ifname slave OPTION fname");
puts("ifname = eth0 for example");
puts("slave = slave number in EtherCAT order 1..n");
puts(" -r read EEPROM, output binary format");
puts(" -ri read EEPROM, output Intel Hex format");
puts(" -w write EEPROM, input binary format");
puts(" -wi write EEPROM, input Intel Hex format");
}
else
{
v3 = atoi(argv[2]);
mode = 0;
slave = v3;
v4 = v3;
v5 = argv[3];
if ( *(_WORD *)v5 == 29229 && !v5[2] )
mode = 1;
if ( !strcmp(v5, "-ri") )
mode = 2;
if ( *v5 == 45 && v5[1] == 119 && !v5[2] )
mode = 3;
if ( !strcmp(v5, "-wi") )
mode = 4;
func6(argv[1], v4, mode, argv[4]);
}
puts("End program");
return 0;
}

original_code:
int main(int argc, char *argv[])
{
 printf("SOEM (Simple Open EtherCAT Master)\nEEPROM tool\n");
 if (argc > 4)
 {
  slave = atoi(argv[2]);
  mode = 0;
  if ((strncmp(argv[3], "-r", sizeof("-r")) == 0)) mode = 1;
  if ((strncmp(argv[3], "-ri", sizeof("-ri")) == 0)) mode = 2;
  if ((strncmp(argv[3], "-w", sizeof("-w")) == 0)) mode = 3;
  if ((strncmp(argv[3], "-wi", sizeof("-wi")) == 0)) mode = 4;
  eepromtool(argv[1],slave,mode,argv[4]);
 }
 else
 {
  printf("Usage: eepromtool ifname slave OPTION fname\n");
  printf("ifname = eth0 for example\n");
  printf("slave = slave number in EtherCAT order 1..n\n");
  printf("    -r      read EEPROM, output binary format\n");
  printf("    -ri     read EEPROM, output Intel Hex format\n");
  printf("    -w      write EEPROM, input binary format\n");
  printf("    -wi     write EEPROM, input Intel Hex format\n");
 }
 printf("End program\n");
 return (0);
}

variable_types: argc int

variable_types:
argv
char *[]

return_type: int


simpletest

decompiled_code:
int __fastcall simpletest(const char *a1)
{
int v1; // eax
int v2; // er14
int v3; // eax
int v4; // er13
unsigned __int16 *v5; // rbx
int v6; // ebp
const char *v7; // rax
char v9; // bl
unsigned int v10; // er12
int v11; // eax
unsigned __int64 v12; // rbx
unsigned int v13; // ebp
__int16 v14; // ax
__int64 v15; // rbx
__int64 v16; // rsi
__int64 v17; // rbx
__int64 v18; // rsi

puts("Starting simple test");
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_init(0LL) > 0 )
{
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 || (v4 = 1, !*((_WORD *)&ec_slave + 21)) )
{
v4 = 8;
if ( v3 <= 8 )
v4 = *((_DWORD *)&ec_slave + 11);
}
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 )
{
v7 = (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], v7);
}
++v6;
v5 += 160;
}
while ( ec_slavecount >= v6 );
}
LABEL_15:
puts("\nRequest safe operational state for all slaves");
ec_slave = 1;
ec_writestate(0LL);
goto LABEL_18;
}
v9 = 0;
v10 = 1;
puts("Operational state reached for all slaves.");
while ( 1 )
{
ec_send_processdata();
v11 = ec_receive_processdata(500LL);
if ( *((unsigned __int16 *)&ec_group + 23) <= v11 && !*((_BYTE *)&ec_group + 48) )
{
v15 = 0LL;
printf("Processdata cycle %4d, WKC %d , O:", v10, (unsigned int)v11);
if ( v2 > 0 )
{
do
{
v16 = *(unsigned __int8 *)(*((_QWORD *)&ec_slave + 4) + v15++);
printf(" %2.2x", v16);
}
while ( v2 > (int)v15 );
}
v17 = 0LL;
printf(" I:");
if ( v4 > 0 )
{
do
{
v18 = *(unsigned __int8 *)(*((_QWORD *)&ec_slave + 6) + v17++);
printf(" %2.2x", v18);
}
while ( v4 > (int)v17 );
}
v9 = 1;
putchar(13);
goto LABEL_48;
}
if ( v9 )
putchar(10);
*((_BYTE *)&ec_group + 48) = 0;
v12 = (unsigned int)((_DWORD)&ec_slave + 589);
v13 = 1;
ec_readstate();
if ( ec_slavecount > 0 )
break;
LABEL_46:
v9 = *((_BYTE *)&ec_group + 48);
if ( v9 )
v9 = 0;
else
puts("OK : all slaves resumed OPERATIONAL.");
LABEL_48:
++v10;
usleep(0x2710u);
if ( v10 == 10001 )
goto LABEL_15;
}
while ( 1 )
{
v14 = *(_WORD *)(v12 - 269);
if ( v14 == 8 )
{
if ( *(_BYTE *)v12 )
goto LABEL_38;
}
else
{
*((_BYTE *)&ec_group + 48) = 1;
if ( v14 == 20 )
{
printf("ERROR : slave %d is in SAFE_OP + ERROR, attempting ack.\n", v13);
*(_WORD *)(v12 - 269) = 20;
ec_writestate(v13);
}
else if ( v14 == 4 )
{
printf("WARNING : slave %d is in SAFE_OP, change to OPERATIONAL.\n", v13);
*(_WORD *)(v12 - 269) = 8;
ec_writestate(v13);
}
else if ( v14 )
{
if ( (unsigned int)ec_reconfig_slave(v13) )
{
*(_BYTE *)v12 = 0;
printf("MESSAGE : slave %d reconfigured\n", v13);
}
}
else
{
if ( *(_BYTE *)v12 )
{
if ( (unsigned int)ec_recover_slave(v13) )
goto LABEL_44;
goto LABEL_35;
}
*(_BYTE *)v12 = 1;
printf("ERROR : slave %d lost\n", v13);
}
if ( !*(_BYTE *)v12 )
goto LABEL_35;
if ( *(_WORD *)(v12 - 269) )
{
LABEL_38:
*(_BYTE *)v12 = 0;
printf("MESSAGE : slave %d found\n", v13);
goto LABEL_35;
}
if ( (unsigned int)ec_recover_slave(v13) )
{
LABEL_44:
*(_BYTE *)v12 = 0;
printf("MESSAGE : slave %d recovered\n", v13);
goto LABEL_35;
}
}
LABEL_35:
++v13;
v12 += 320LL;
if ( ec_slavecount < (int)v13 )
goto LABEL_46;
}
}
puts("No slaves found!");
LABEL_18:
puts("End simple test, close socket");
return ec_close();
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
int v1; // eax
int v2; // er14
int v3; // eax
int v4; // er13
unsigned __int16 *v5; // rbx
int v6; // ebp
const char *v7; // rax
char v9; // bl
unsigned int v10; // er12
int v11; // eax
unsigned __int64 v12; // rbx
unsigned int v13; // ebp
__int16 v14; // ax
__int64 v15; // rbx
__int64 v16; // rsi
__int64 v17; // rbx
__int64 v18; // rsi

puts("Starting simple test");
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_init(0LL) > 0 )
{
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 || (v4 = 1, !*((_WORD *)&ec_slave + 21)) )
{
v4 = 8;
if ( v3 <= 8 )
v4 = *((_DWORD *)&ec_slave + 11);
}
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 )
{
v7 = (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], v7);
}
++v6;
v5 += 160;
}
while ( ec_slavecount >= v6 );
}
LABEL_15:
puts("\nRequest safe operational state for all slaves");
ec_slave = 1;
ec_writestate(0LL);
goto LABEL_18;
}
v9 = 0;
v10 = 1;
puts("Operational state reached for all slaves.");
while ( 1 )
{
ec_send_processdata();
v11 = ec_receive_processdata(500LL);
if ( *((unsigned __int16 *)&ec_group + 23) <= v11 && !*((_BYTE *)&ec_group + 48) )
{
v15 = 0LL;
printf("Processdata cycle %4d, WKC %d , O:", v10, (unsigned int)v11);
if ( v2 > 0 )
{
do
{
v16 = *(unsigned __int8 *)(*((_QWORD *)&ec_slave + 4) + v15++);
printf(" %2.2x", v16);
}
while ( v2 > (int)v15 );
}
v17 = 0LL;
printf(" I:");
if ( v4 > 0 )
{
do
{
v18 = *(unsigned __int8 *)(*((_QWORD *)&ec_slave + 6) + v17++);
printf(" %2.2x", v18);
}
while ( v4 > (int)v17 );
}
v9 = 1;
putchar(13);
goto LABEL_48;
}
if ( v9 )
putchar(10);
*((_BYTE *)&ec_group + 48) = 0;
v12 = (unsigned int)((_DWORD)&ec_slave + 589);
v13 = 1;
ec_readstate();
if ( ec_slavecount > 0 )
break;
LABEL_46:
v9 = *((_BYTE *)&ec_group + 48);
if ( v9 )
v9 = 0;
else
puts("OK : all slaves resumed OPERATIONAL.");
LABEL_48:
++v10;
usleep(0x2710u);
if ( v10 == 10001 )
goto LABEL_15;
}
while ( 1 )
{
v14 = *(_WORD *)(v12 - 269);
if ( v14 == 8 )
{
if ( *(_BYTE *)v12 )
goto LABEL_38;
}
else
{
*((_BYTE *)&ec_group + 48) = 1;
if ( v14 == 20 )
{
printf("ERROR : slave %d is in SAFE_OP + ERROR, attempting ack.\n", v13);
*(_WORD *)(v12 - 269) = 20;
ec_writestate(v13);
}
else if ( v14 == 4 )
{
printf("WARNING : slave %d is in SAFE_OP, change to OPERATIONAL.\n", v13);
*(_WORD *)(v12 - 269) = 8;
ec_writestate(v13);
}
else if ( v14 )
{
if ( (unsigned int)ec_reconfig_slave(v13) )
{
*(_BYTE *)v12 = 0;
printf("MESSAGE : slave %d reconfigured\n", v13);
}
}
else
{
if ( *(_BYTE *)v12 )
{
if ( (unsigned int)ec_recover_slave(v13) )
goto LABEL_44;
goto LABEL_35;
}
*(_BYTE *)v12 = 1;
printf("ERROR : slave %d lost\n", v13);
}
if ( !*(_BYTE *)v12 )
goto LABEL_35;
if ( *(_WORD *)(v12 - 269) )
{
LABEL_38:
*(_BYTE *)v12 = 0;
printf("MESSAGE : slave %d found\n", v13);
goto LABEL_35;
}
if ( (unsigned int)ec_recover_slave(v13) )
{
LABEL_44:
*(_BYTE *)v12 = 0;
printf("MESSAGE : slave %d recovered\n", v13);
goto LABEL_35;
}
}
LABEL_35:
++v13;
v12 += 320LL;
if ( ec_slavecount < (int)v13 )
goto LABEL_46;
}
}
puts("No slaves found!");
LABEL_18:
puts("End simple test, close socket");
return ec_close();
}

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

return_type: void


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


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;
v10 = a3;
v8 = 0;
v9 = 8;
EcatError = 1;
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;
v10 = a3;
v8 = 0;
v9 = 8;
EcatError = 1;
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; }; }; }

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


decompiled_code:
_BOOL8 __fastcall ec_readIDNmap(unsigned __int16 a1, _DWORD *a2, int *a3)
{
_BOOL8 result; // rax
unsigned __int16 v5; // ax
unsigned __int16 *v6; // r13
unsigned __int16 *v7; // r14
unsigned __int16 v8; // ax
unsigned __int16 *v9; // r13
unsigned __int16 *v10; // r14
int v11[15]; // [rsp+14h] [rbp-3Ch] BYREF

*a3 = 0;
v11[0] = 132;
*a2 = 0;
if ( (int)ec_SoEread(a1, 0, 64, 0x18u, v11, (char *)&SoEmapping, 0xAAE60u) > 0 && v11[0] > 3 )
{
v8 = SoEmapping;
if ( (unsigned __int16)(SoEmapping - 2) <= 0x7Fu )
{
*a2 = 16;
v9 = (unsigned __int16 *)&unk_7C4;
v10 = (unsigned __int16 *)((char *)&unk_7C6 + 2 * (unsigned __int16)((v8 >> 1) - 1));
do
{
v11[0] = 4;
if ( (int)ec_SoEread(a1, 0, 4, *v9, v11, SoEattribute, 0xAAE60u) > 0 && (byte_7A2 & 4) == 0 )
*a2 += 8 << (byte_7A2 & 3);
++v9;
}
while ( v9 != v10 );
}
}
v11[0] = 132;
if ( (int)ec_SoEread(a1, 0, 64, 0x10u, v11, (char *)&SoEmapping, 0xAAE60u) > 0 && v11[0] > 3 )
{
v5 = SoEmapping;
if ( (unsigned __int16)(SoEmapping - 2) <= 0x7Fu )
{
*a3 = 16;
v6 = (unsigned __int16 *)&unk_7C4;
v7 = (unsigned __int16 *)((char *)&unk_7C6 + 2 * (unsigned __int16)((v5 >> 1) - 1));
do
{
v11[0] = 4;
if ( (int)ec_SoEread(a1, 0, 4, *v6, v11, SoEattribute, 0xAAE60u) > 0 && (byte_7A2 & 4) == 0 )
*a3 += 8 << (byte_7A2 & 3);
++v6;
}
while ( v6 != v7 );
}
}
result = 1LL;
if ( *a3 <= 0 )
result = *a2 > 0;
return result;
}

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 v5; // eax
int v6; // ebp
int v7; // eax
int v8; // er12
int v9; // er13
__int64 v10; // rsi
__int64 v11; // rbx
__int64 v12; // rsi
__int64 v13; // rbx
__int64 v14; // rsi

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
{
v2 = (unsigned int)((_DWORD)&ec_slave + 600);
v3 = 1;
printf("%d slaves found and configured.\n", ec_slavecount);
ec_statecheck(0LL, 4LL, 2000000LL);
ec_configdc();
ec_readstate();
if ( ec_slavecount > 0 )
{
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);
v5 = *((_DWORD *)&ec_slave + 7);
if ( v5 || (v6 = 1, !*((_WORD *)&ec_slave + 12)) )
{
v6 = 8;
if ( v5 <= 8 )
v6 = *((_DWORD *)&ec_slave + 7);
}
v7 = *((_DWORD *)&ec_slave + 11);
if ( v7 || (v8 = 1, !*((_WORD *)&ec_slave + 21)) )
{
v8 = 8;
if ( v7 <= 8 )
v8 = *((_DWORD *)&ec_slave + 11);
}
if ( ec_slave == 8 )
{
v9 = 500;
puts("Operational state reached for all slaves.");
v10 = 1LL;
for ( dorun = 1; ; v10 = (unsigned int)dorun )
{
v11 = 0LL;
printf("Processdata cycle %5d , DCtime %12lld, O:", v10, ec_DCtime);
if ( v6 > 0 )
{
do
{
v12 = *(unsigned __int8 *)(*((_QWORD *)&ec_slave + 4) + v11++);
printf(" %2.2x", v12);
}
while ( v6 > (int)v11 );
}
v13 = 0LL;
printf(" I:");
if ( v8 > 0 )
{
do
{
v14 = *(unsigned __int8 *)(*((_QWORD *)&ec_slave + 6) + v13++);
printf(" %2.2x", v14);
}
while ( v8 > (int)v13 );
}
putchar(10);
usleep(0x4E20u);
if ( !--v9 )
break;
}
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 v5; // eax
int v6; // ebp
int v7; // eax
int v8; // er12
int v9; // er13
__int64 v10; // rsi
__int64 v11; // rbx
__int64 v12; // rsi
__int64 v13; // rbx
__int64 v14; // rsi

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
{
v2 = (unsigned int)((_DWORD)&ec_slave + 600);
v3 = 1;
printf("%d slaves found and configured.\n", ec_slavecount);
ec_statecheck(0LL, 4LL, 2000000LL);
ec_configdc();
ec_readstate();
if ( ec_slavecount > 0 )
{
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);
v5 = *((_DWORD *)&ec_slave + 7);
if ( v5 || (v6 = 1, !*((_WORD *)&ec_slave + 12)) )
{
v6 = 8;
if ( v5 <= 8 )
v6 = *((_DWORD *)&ec_slave + 7);
}
v7 = *((_DWORD *)&ec_slave + 11);
if ( v7 || (v8 = 1, !*((_WORD *)&ec_slave + 21)) )
{
v8 = 8;
if ( v7 <= 8 )
v8 = *((_DWORD *)&ec_slave + 11);
}
if ( ec_slave == 8 )
{
v9 = 500;
puts("Operational state reached for all slaves.");
v10 = 1LL;
for ( dorun = 1; ; v10 = (unsigned int)dorun )
{
v11 = 0LL;
printf("Processdata cycle %5d , DCtime %12lld, O:", v10, ec_DCtime);
if ( v6 > 0 )
{
do
{
v12 = *(unsigned __int8 *)(*((_QWORD *)&ec_slave + 4) + v11++);
printf(" %2.2x", v12);
}
while ( v6 > (int)v11 );
}
v13 = 0LL;
printf(" I:");
if ( v8 > 0 )
{
do
{
v14 = *(unsigned __int8 *)(*((_QWORD *)&ec_slave + 6) + v13++);
printf(" %2.2x", v14);
}
while ( v8 > (int)v13 );
}
putchar(10);
usleep(0x4E20u);
if ( !--v9 )
break;
}
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
double v6; // xmm0_8
__int64 v7; // rbx
int v8; // [rsp+14h] [rbp-2Ch] BYREF
char s[40]; // [rsp+18h] [rbp-28h] BYREF

memset(usdo, 0, 0x80uLL);
v8 = 127;
ec_SDOread(a1, a2, a3, 0LL, &v8);
if ( EcatError )
return (char *)((__int64 (__fastcall *)(_QWORD))ec_elist2string)(a1);
switch ( a4 )
{
case 1:
if ( usdo[0] )
{
strcpy(hstr, "TRUE");
result = hstr;
}
else
{
result = hstr;
strcpy(hstr, "FALSE");
}
return result;
case 2:
sprintf(hstr, "0x%2.2x %d", (unsigned int)usdo[0], (unsigned int)usdo[0]);
return hstr;
case 3:
sprintf(hstr, "0x%4.4x %d", (unsigned int)*(__int16 *)usdo, (unsigned int)*(__int16 *)usdo);
return hstr;
case 4:
case 16:
sprintf(hstr, "0x%8.8x %d", *(unsigned int *)usdo, *(unsigned int *)usdo);
return hstr;
case 5:
sprintf(hstr, "0x%2.2x %u", (unsigned __int8)usdo[0], (unsigned __int8)usdo[0]);
return hstr;
case 6:
sprintf(hstr, "0x%4.4x %u", *(unsigned __int16 *)usdo, *(unsigned __int16 *)usdo);
return hstr;
case 7:
case 22:
sprintf(hstr, "0x%8.8x %u", *(unsigned int *)usdo, *(unsigned int *)usdo);
return hstr;
case 8:
v6 = *(float *)usdo;
goto LABEL_13;
case 9:
strcpy(hstr, usdo);
return hstr;
case 10:
hstr[0] = 0;
v7 = 0LL;
while ( v8 > (int)v7 )
{
sprintf(s, "0x%2.2x ", (unsigned int)usdo[v7++]);
strcat(hstr, s);
}
goto LABEL_5;
case 17:
v6 = *(double *)usdo;
LABEL_13:
sprintf(hstr, "%f", v6);
return hstr;
case 21:
sprintf(hstr, "0x%16.16llx %lld", *(_QWORD *)usdo, *(_QWORD *)usdo);
return hstr;
case 27:
sprintf(hstr, "0x%16.16llx %llu", *(_QWORD *)usdo, *(_QWORD *)usdo);
return hstr;
case 48:
case 49:
case 50:
case 51:
case 52:
case 53:
case 54:
case 55:
sprintf(hstr, "0x%x", (unsigned __int8)usdo[0]);
return hstr;
default:
strcpy(hstr, "Unknown type");
LABEL_5:
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
double v6; // xmm0_8
__int64 v7; // rbx
int v8; // [rsp+14h] [rbp-2Ch] BYREF
char s[40]; // [rsp+18h] [rbp-28h] BYREF

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