decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
__pid_t v4; // ebp
int v5; // ebx
int v6; // eax
unsigned __int64 v7; // r12
unsigned __int64 v8; // rbp
__int64 v9; // rax
__int64 v10; // rax
unsigned int v11; // ebx
unsigned __int64 v12; // r12
unsigned __int64 v13; // rbp
unsigned int v14; // eax
int v15; // eax
unsigned __int64 v16; // r12
unsigned __int64 v17; // rbp
int v18; // ebx
__int64 v19; // rax
__int64 v20; // rax
unsigned int v21; // ebx
unsigned __int64 v22; // r12
unsigned __int64 v23; // rbp
unsigned int v24; // eax
struct timeval v26; // [rsp+0h] [rbp-48h] BYREF
struct timeval tv; // [rsp+10h] [rbp-38h] BYREF

if ( argc > 1 )
max_count = atoi(argv[1]);
v3 = open("ipc_lock", 2, envp);
if ( v3 == -1 )
__assert_fail("lockfile != -1", "notify_test.c", 0x2Bu, "main");
if ( !off_4040A8(v3) )
__assert_fail("fctxt != NULL", "notify_test.c", 0x2Fu, "main");
v4 = fork();
v5 = v4;
if ( v4 == -1 )
__assert_fail("pid != -1", "notify_test.c", 0x35u, "main");
gettimeofday(&v26, 0LL);
if ( !v4 )
{
puts("Initializing...");
printf("Done initializing, max_count:%d\n", (unsigned int)max_count);
v6 = max_count;
if ( max_count <= 0 )
{
v7 = 0LL;
v8 = 0LL;
}
else
{
v7 = 0LL;
v8 = 0LL;
do
{
gettimeofday(&tv, 0LL);
v9 = off_4040B8(&futex_test_ops);
futex_unlock(v9);
gettimeofday(&v26, 0LL);
v8 += v26.tv_usec + 1000000 * (v26.tv_sec - tv.tv_sec) - tv.tv_usec;
++v5;
gettimeofday(&tv, 0LL);
v10 = off_4040C0(&futex_test_ops);
futex_ops(v10);
gettimeofday(&v26, 0LL);
v7 += v26.tv_usec + 1000000 * (v26.tv_sec - tv.tv_sec) - tv.tv_usec;
v6 = max_count;
}
while ( max_count > v5 );
}
v11 = dowait;
v12 = v7 / v6;
v13 = v8 / v6;
v14 = getpid();
printf("Parent %d done, up avg:%ld, down avg:%ld, dowait:%u\n", v14, v13, v12, v11);
exit(0);
}
puts("Waiting for child...");
v15 = max_count;
if ( max_count <= 0 )
{
v16 = 0LL;
v17 = 0LL;
}
else
{
v16 = 0LL;
v17 = 0LL;
v18 = 0;
do
{
gettimeofday(&tv, 0LL);
v19 = off_4040B8(&futex_test_ops);
futex_ops(v19);
gettimeofday(&v26, 0LL);
v16 += v26.tv_usec + 1000000 * (v26.tv_sec - tv.tv_sec) - tv.tv_usec;
++v18;
gettimeofday(&tv, 0LL);
v20 = off_4040C0(&futex_test_ops);
futex_unlock(v20);
gettimeofday(&v26, 0LL);
v17 += v26.tv_usec + 1000000 * (v26.tv_sec - tv.tv_sec) - tv.tv_usec;
v15 = max_count;
}
while ( max_count > v18 );
}
v21 = dowait;
v22 = v16 / v15;
v23 = v17 / v15;
v24 = getpid();
printf("Child %d done, up avg:%ld, down avg:%ld, dowait:%d\n", v24, v23, v22, v21);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // eax
__pid_t v4; // ebp
int v5; // ebx
int v6; // eax
unsigned __int64 v7; // r12
unsigned __int64 v8; // rbp
__int64 v9; // rax
__int64 v10; // rax
unsigned int v11; // ebx
unsigned __int64 v12; // r12
unsigned __int64 v13; // rbp
unsigned int v14; // eax
int v15; // eax
unsigned __int64 v16; // r12
unsigned __int64 v17; // rbp
int v18; // ebx
__int64 v19; // rax
__int64 v20; // rax
unsigned int v21; // ebx
unsigned __int64 v22; // r12
unsigned __int64 v23; // rbp
unsigned int v24; // eax
struct timeval v26; // [rsp+0h] [rbp-48h] BYREF
struct timeval tv; // [rsp+10h] [rbp-38h] BYREF

if ( argc > 1 )
max_count = atoi(argv[1]);
v3 = open("ipc_lock", 2, envp);
if ( v3 == -1 )
__assert_fail("lockfile != -1", "notify_test.c", 0x2Bu, "main");
if ( !off_4040A8(v3) )
__assert_fail("fctxt != NULL", "notify_test.c", 0x2Fu, "main");
v4 = fork();
v5 = v4;
if ( v4 == -1 )
__assert_fail("pid != -1", "notify_test.c", 0x35u, "main");
gettimeofday(&v26, 0LL);
if ( !v4 )
{
puts("Initializing...");
printf("Done initializing, max_count:%d\n", (unsigned int)max_count);
v6 = max_count;
if ( max_count <= 0 )
{
v7 = 0LL;
v8 = 0LL;
}
else
{
v7 = 0LL;
v8 = 0LL;
do
{
gettimeofday(&tv, 0LL);
v9 = off_4040B8(&futex_test_ops);
func11(v9);
gettimeofday(&v26, 0LL);
v8 += v26.tv_usec + 1000000 * (v26.tv_sec - tv.tv_sec) - tv.tv_usec;
++v5;
gettimeofday(&tv, 0LL);
v10 = off_4040C0(&futex_test_ops);
futex_ops(v10);
gettimeofday(&v26, 0LL);
v7 += v26.tv_usec + 1000000 * (v26.tv_sec - tv.tv_sec) - tv.tv_usec;
v6 = max_count;
}
while ( max_count > v5 );
}
v11 = dowait;
v12 = v7 / v6;
v13 = v8 / v6;
v14 = getpid();
printf("Parent %d done, up avg:%ld, down avg:%ld, dowait:%u\n", v14, v13, v12, v11);
exit(0);
}
puts("Waiting for child...");
v15 = max_count;
if ( max_count <= 0 )
{
v16 = 0LL;
v17 = 0LL;
}
else
{
v16 = 0LL;
v17 = 0LL;
v18 = 0;
do
{
gettimeofday(&tv, 0LL);
v19 = off_4040B8(&futex_test_ops);
futex_ops(v19);
gettimeofday(&v26, 0LL);
v16 += v26.tv_usec + 1000000 * (v26.tv_sec - tv.tv_sec) - tv.tv_usec;
++v18;
gettimeofday(&tv, 0LL);
v20 = off_4040C0(&futex_test_ops);
func11(v20);
gettimeofday(&v26, 0LL);
v17 += v26.tv_usec + 1000000 * (v26.tv_sec - tv.tv_sec) - tv.tv_usec;
v15 = max_count;
}
while ( max_count > v18 );
}
v21 = dowait;
v22 = v16 / v15;
v23 = v17 / v15;
v24 = getpid();
printf("Child %d done, up avg:%ld, down avg:%ld, dowait:%d\n", v24, v23, v22, v21);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
    const struct lock_ops *ops = &futex_ops;
    struct test_ops *tops = &futex_test_ops;
 if (argc > 1)
  max_count = atoi(argv[1]);
    if (argc > 2) {
        if (strstr(argv[2], "futex") != ((void *)0)) {
            ops = &futex_ops;
            tops = &futex_test_ops;
        }
    }
 int lockfile = open("ipc_lock", 02);
 ((void) sizeof ((lockfile != -1) ? 1 : 0), __extension__ ({ if (lockfile != -1) ; else __assert_fail ("lockfile != -1", "notify_test.c", 43, __extension__ __PRETTY_FUNCTION__); }));
    struct futex_context *fctxt;
    fctxt = tops->init(lockfile);
    ((void) sizeof ((fctxt != ((void *)0)) ? 1 : 0), __extension__ ({ if (fctxt != ((void *)0)) ; else __assert_fail ("fctxt != NULL", "notify_test.c", 47, __extension__ __PRETTY_FUNCTION__); }));
    int pcount = 0;
    int ccount = 0;
 int pid = fork();
 ((void) sizeof ((pid != -1) ? 1 : 0), __extension__ ({ if (pid != -1) ; else __assert_fail ("pid != -1", "notify_test.c", 53, __extension__ __PRETTY_FUNCTION__); }));
 struct timeval old, new;
 gettimeofday(&new, ((void *)0));
 uint64_t unlock_usecs, lock_usecs;
    unlock_usecs = lock_usecs = 0;
 if( pid == 0 ) {
  printf("Initializing...\n");
  printf("Done initializing, max_count:%d\n", max_count);
  while (pcount < max_count) {
   gettimeofday(&old, ((void *)0));
            ops->unlock(tops->get_top_lock((void *)tops));
   gettimeofday(&new, ((void *)0));
   unlock_usecs += (new.tv_sec-old.tv_sec)*1000000+(new.tv_usec-old.tv_usec);
   pcount++;
   gettimeofday(&old, ((void *)0));
            ops->lock(tops->get_bottom_lock((void *)tops));
   gettimeofday(&new, ((void *)0));
   lock_usecs += (new.tv_sec-old.tv_sec)*1000000+(new.tv_usec-old.tv_usec);
  }
     printf("Parent %d done, up avg:%ld, down avg:%ld, dowait:%u\n", getpid(), unlock_usecs/max_count, lock_usecs/max_count, dowait);
  exit(0);
 }
 printf("Waiting for child...\n");
 while (ccount < max_count)
 {
        gettimeofday(&old, ((void *)0));
        ops->lock(tops->get_top_lock((void *)tops));
        gettimeofday(&new, ((void *)0));
        lock_usecs += (new.tv_sec-old.tv_sec)*1000000+(new.tv_usec-old.tv_usec);
  ccount++;
  gettimeofday(&old, ((void *)0));
        ops->unlock(tops->get_bottom_lock((void *)tops));
  gettimeofday(&new, ((void *)0));
  unlock_usecs += (new.tv_sec-old.tv_sec)*1000000+(new.tv_usec-old.tv_usec);
 }
 printf("Child %d done, up avg:%ld, down avg:%ld, dowait:%d\n", getpid(), unlock_usecs/max_count, lock_usecs/max_count, dowait);
 return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    struct lock_ops *ops = &futex_ops;
    struct test_ops *tops = &futex_test_ops;
 if (argc > 1)
  max_count = atoi(argv[1]);
    if (argc > 2) {
        if (strstr(argv[2], "futex") != ((void *)0)) {
            ops = &futex_ops;
            tops = &futex_test_ops;
        }
    }
 int lockfile = open("ipc_lock", 02);
 ((void) sizeof ((lockfile != -1) ? 1 : 0), __extension__ ({ if (lockfile != -1) ; else __assert_fail ("lockfile != -1", "notify_test.c", 43, __extension__ __PRETTY_FUNCTION__); }));
    struct futex_context *fctxt;
    fctxt = tops->init(lockfile);
    ((void) sizeof ((fctxt != ((void *)0)) ? 1 : 0), __extension__ ({ if (fctxt != ((void *)0)) ; else __assert_fail ("fctxt != NULL", "notify_test.c", 47, __extension__ __PRETTY_FUNCTION__); }));
    int pcount = 0;
    int ccount = 0;
 int pid = fork();
 ((void) sizeof ((pid != -1) ? 1 : 0), __extension__ ({ if (pid != -1) ; else __assert_fail ("pid != -1", "notify_test.c", 53, __extension__ __PRETTY_FUNCTION__); }));
 struct timeval old; struct timeval new;
 gettimeofday(&new, ((void *)0));
 unsigned long unlock_usecs; unsigned long lock_usecs;
    unlock_usecs = lock_usecs = 0;
 if( pid == 0 ) {
  printf("Initializing...\n");
  printf("Done initializing, max_count:%d\n", max_count);
  while (pcount < max_count) {
   gettimeofday(&old, ((void *)0));
            ops->unlock(tops->get_top_lock((void *)tops));
   gettimeofday(&new, ((void *)0));
   unlock_usecs += (new.tv_sec-old.tv_sec)*1000000+(new.tv_usec-old.tv_usec);
   pcount++;
   gettimeofday(&old, ((void *)0));
            ops->lock(tops->get_bottom_lock((void *)tops));
   gettimeofday(&new, ((void *)0));
   lock_usecs += (new.tv_sec-old.tv_sec)*1000000+(new.tv_usec-old.tv_usec);
  }
     printf("Parent %d done, up avg:%ld, down avg:%ld, dowait:%u\n", getpid(), unlock_usecs/max_count, lock_usecs/max_count, dowait);
  exit(0);
 }
 printf("Waiting for child...\n");
 while (ccount < max_count)
 {
        gettimeofday(&old, ((void *)0));
        ops->lock(tops->get_top_lock((void *)tops));
        gettimeofday(&new, ((void *)0));
        lock_usecs += (new.tv_sec-old.tv_sec)*1000000+(new.tv_usec-old.tv_usec);
  ccount++;
  gettimeofday(&old, ((void *)0));
        ops->unlock(tops->get_bottom_lock((void *)tops));
  gettimeofday(&new, ((void *)0));
  unlock_usecs += (new.tv_sec-old.tv_sec)*1000000+(new.tv_usec-old.tv_usec);
 }
 printf("Child %d done, up avg:%ld, down avg:%ld, dowait:%d\n", getpid(), unlock_usecs/max_count, lock_usecs/max_count, dowait);
 return 0;
}

variable_types: struct lock_ops { int (*lock)(void *context); int (*unlock)(void *context); } *

variable_types: struct test_ops { void *context; void *(*init)(int mmap_fd); void (*destroy)(void *context); void *(*get_top_lock)(void *context); void *(*get_bottom_lock)(void *context); } *

variable_types: lockfile int

variable_types: struct futex_context { struct simplefu_semaphore { int avail; int waiters; } top_lock; struct simplefu_semaphore bottom_lock; } *

variable_types: pcount int

variable_types: ccount int

variable_types: pid int

variable_types: struct timeval { long tv_sec; long tv_usec; }

user_defined_types: struct futex_context { struct simplefu_semaphore top_lock; struct simplefu_semaphore bottom_lock; }


decompiled_code:
__int64 __fastcall futex_get_top_lock(__int64 a1)
{
return *(_QWORD *)a1;
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1)
{
return *(_QWORD *)a1;
}

original_code:
void *futex_get_top_lock(void *context)
{
    struct test_ops *tops = (struct test_ops *)context;
    struct futex_context *test_cxt = (struct futex_context *)(tops->context);
    return &test_cxt->top_lock;
}

variable_types:
context
void *

return_type: void *

user_defined_types: struct test_ops { void *context; void *(*init)(int mmap_fd); void (*destroy)(void *context); void *(*get_top_lock)(void *context); void *(*get_bottom_lock)(void *context); }

user_defined_types: struct simplefu_semaphore { int avail; int waiters; }


decompiled_code:
__int64 __fastcall futex_get_bottom_lock(_QWORD *a1)
{
return *a1 + 8LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(_QWORD *a1)
{
return *a1 + 8LL;
}

original_code:
void *futex_get_bottom_lock(void *context)
{
    struct test_ops *tops = (struct test_ops *)context;
    struct futex_context *test_cxt = (struct futex_context *)(tops->context);
    return &test_cxt->bottom_lock;
}


decompiled_code:
int __fastcall futex_test_destroy(void *a1)
{
return munmap(a1, 0x10uLL);
}

canonical_decompiled_code:
int __fastcall func9(void *a1)
{
return munmap(a1, 0x10uLL);
}

original_code:
void futex_test_destroy(void *context)
{
    munmap(context, 2*sizeof(simplefu));
}

return_type: void


decompiled_code:
__int64 __fastcall futex_lock(volatile signed __int32 *a1)
{
volatile signed __int32 *i; // rbp
__int64 v2; // rcx

for ( i = a1 + 1; ; _InterlockedSub(i, 1u) )
{
v2 = *(unsigned int *)a1;
if ( (int)v2 > 0 && (_DWORD)v2 == _InterlockedCompareExchange(a1, v2 - 1, v2) )
break;
_InterlockedAdd(&dowait, 1u);
_InterlockedAdd(i, 1u);
syscall(202LL, a1, 0LL, v2, 0LL, 0LL, 0LL);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func10(volatile signed __int32 *a1)
{
volatile signed __int32 *i; // rbp
__int64 v2; // rcx

for ( i = a1 + 1; ; _InterlockedSub(i, 1u) )
{
v2 = *(unsigned int *)a1;
if ( (int)v2 > 0 && (_DWORD)v2 == _InterlockedCompareExchange(a1, v2 - 1, v2) )
break;
_InterlockedAdd(&dowait, 1u);
_InterlockedAdd(i, 1u);
syscall(202LL, a1, 0LL, v2, 0LL, 0LL, 0LL);
}
return 0LL;
}

original_code:
int futex_lock(void *context)
{
    simplefu *who = (simplefu *)context;
 int val;
 do {
  val = who->avail;
  if( val > 0 && __sync_bool_compare_and_swap(&who->avail, val, val - 1) )
  {
   return;
  }
        __sync_fetch_and_add(&dowait, 1);
  __sync_fetch_and_add(&who->waiters, 1);
  syscall(202, &who->avail, 0, val, ((void *)0), 0, 0);
  __sync_fetch_and_sub(&who->waiters, 1);
 } while(1);
    return 0;
}

canonical_original_code:
int futex_lock(void *context)
{
    struct simplefu_semaphore *who = (simplefu *)context;
 int val;
 do {
  val = who->avail;
  if( val > 0 && __sync_bool_compare_and_swap(&who->avail, val, val - 1) )
  {
   return;
  }
        __sync_fetch_and_add(&dowait, 1);
  __sync_fetch_and_add(&who->waiters, 1);
  syscall(202, &who->avail, 0, val, ((void *)0), 0, 0);
  __sync_fetch_and_sub(&who->waiters, 1);
 } while(1);
    return 0;
}

variable_types: struct simplefu_semaphore { int avail; int waiters; } *

variable_types: val int

return_type: int


decompiled_code:
__int64 __fastcall futex_unlock(__int64 a1)
{
__int64 v1; // rcx

v1 = (unsigned int)_InterlockedIncrement((volatile signed __int32 *)a1);
if ( *(int *)(a1 + 4) > 0 )
syscall(202LL, a1, 1LL, v1, 0LL, 0LL, 0LL);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1)
{
__int64 v1; // rcx

v1 = (unsigned int)_InterlockedIncrement((volatile signed __int32 *)a1);
if ( *(int *)(a1 + 4) > 0 )
syscall(202LL, a1, 1LL, v1, 0LL, 0LL, 0LL);
return 0LL;
}

original_code:
int futex_unlock(void *context)
{
    simplefu *who = (simplefu *)context;
 int nval = __sync_add_and_fetch(&who->avail, 1);
 if( who->waiters > 0 )
 {
  int nr = syscall(202, &who->avail, 1, nval, ((void *)0), 0, 0);
 }
    return 0;
}

canonical_original_code:
int futex_unlock(void *context)
{
    struct simplefu_semaphore *who = (simplefu *)context;
 int nval = __sync_add_and_fetch(&who->avail, 1);
 if( who->waiters > 0 )
 {
  int nr = syscall(202, &who->avail, 1, nval, ((void *)0), 0, 0);
 }
    return 0;
}

variable_types: nval int

variable_types: nr int


original_code:
int ec_setupdatagram(void *frame, uint8 com, uint8 idx, uint16 ADP, uint16 ADO, uint16 length, void *data)
{
 ec_comt *datagramP;
 uint8 *frameP;
 frameP = frame;
 datagramP = (ec_comt*)&frameP[sizeof(ec_etherheadert)];
 datagramP->elength = (0x1000 + sizeof(ec_comt) + length);
 datagramP->command = com;
 datagramP->index = idx;
 datagramP->ADP = (ADP);
 datagramP->ADO = (ADO);
 datagramP->dlength = (length);
 if (length > 0)
 {
  memcpy(&frameP[sizeof(ec_etherheadert) + sizeof(ec_comt)], data, length);
 }
 frameP[sizeof(ec_etherheadert) + sizeof(ec_comt) + length] = 0x00;
 frameP[sizeof(ec_etherheadert) + sizeof(ec_comt) + length + 1] = 0x00;
 ec_txbuflength[idx] = sizeof(ec_etherheadert) + sizeof(ec_comt) + sizeof(uint16) + length;
 return 0;
}

canonical_original_code:
int ec_setupdatagram(void *frame, unsigned char com, unsigned char idx, unsigned short ADP, unsigned short ADO, unsigned short length, void *data)
{
 struct ec_comt *datagramP;
 unsigned char *frameP;
 frameP = frame;
 datagramP = (struct ec_comt *)&frameP[sizeof(ec_etherheadert)];
 datagramP->elength = (0x1000 + sizeof(ec_comt) + length);
 datagramP->command = com;
 datagramP->index = idx;
 datagramP->ADP = (ADP);
 datagramP->ADO = (ADO);
 datagramP->dlength = (length);
 if (length > 0)
 {
  memcpy(&frameP[sizeof(ec_etherheadert) + sizeof(ec_comt)], data, length);
 }
 frameP[sizeof(ec_etherheadert) + sizeof(ec_comt) + length] = 0x00;
 frameP[sizeof(ec_etherheadert) + sizeof(ec_comt) + length + 1] = 0x00;
 ec_txbuflength[idx] = sizeof(ec_etherheadert) + sizeof(ec_comt) + sizeof(uint16) + length;
 return 0;
}

variable_types:
frame
void *

variable_types:
com
unsigned char

variable_types:
idx
unsigned char

variable_types:
ADP
unsigned short

variable_types:
ADO
unsigned short

variable_types:
length
unsigned short

variable_types:
data
void *

variable_types: struct ec_comt { unsigned short elength; unsigned char command; unsigned char index; unsigned short ADP; unsigned short ADO; unsigned short dlength; unsigned short irpt; } *

variable_types:
frameP
unsigned char *


original_code:
int ec_adddatagram(void *frame, uint8 com, uint8 idx, boolean more, uint16 ADP, uint16 ADO, uint16 length, void *data)
{
 ec_comt *datagramP;
 uint8 *frameP;
 uint16 prevlength;
 frameP = frame;
 prevlength = ec_txbuflength[idx];
 datagramP = (ec_comt*)&frameP[sizeof(ec_etherheadert)];
 datagramP->elength = ((datagramP->elength) + sizeof(ec_comt) + length);
 datagramP->dlength = ((datagramP->dlength) | (1 << 15));
 datagramP = (ec_comt*)&frameP[prevlength - sizeof(uint16)];
 datagramP->command = com;
 datagramP->index = idx;
 datagramP->ADP = (ADP);
 datagramP->ADO = (ADO);
 if (more)
 {
  datagramP->dlength = (length | (1 << 15));
 }
 else
 {
  datagramP->dlength = (length);
 }
 if (length > 0)
 {
  memcpy(&frameP[prevlength + sizeof(ec_comt) - sizeof(uint16)], data, length);
 }
 frameP[prevlength + sizeof(ec_comt) - sizeof(uint16) + length] = 0x00;
 frameP[prevlength + sizeof(ec_comt) - sizeof(uint16) + length + 1] = 0x00;
 ec_txbuflength[idx] = prevlength + sizeof(ec_comt) - sizeof(uint16) + sizeof(uint16) + length;
 return prevlength + sizeof(ec_comt) - sizeof(uint16) - sizeof(ec_etherheadert);
}

canonical_original_code:
int ec_adddatagram(void *frame, unsigned char com, unsigned char idx, unsigned char more, unsigned short ADP, unsigned short ADO, unsigned short length, void *data)
{
 struct ec_comt *datagramP;
 unsigned char *frameP;
 unsigned short prevlength;
 frameP = frame;
 prevlength = ec_txbuflength[idx];
 datagramP = (struct ec_comt *)&frameP[sizeof(ec_etherheadert)];
 datagramP->elength = ((datagramP->elength) + sizeof(ec_comt) + length);
 datagramP->dlength = ((datagramP->dlength) | (1 << 15));
 datagramP = (struct ec_comt *)&frameP[prevlength - sizeof(uint16)];
 datagramP->command = com;
 datagramP->index = idx;
 datagramP->ADP = (ADP);
 datagramP->ADO = (ADO);
 if (more)
 {
  datagramP->dlength = (length | (1 << 15));
 }
 else
 {
  datagramP->dlength = (length);
 }
 if (length > 0)
 {
  memcpy(&frameP[prevlength + sizeof(ec_comt) - sizeof(uint16)], data, length);
 }
 frameP[prevlength + sizeof(ec_comt) - sizeof(uint16) + length] = 0x00;
 frameP[prevlength + sizeof(ec_comt) - sizeof(uint16) + length + 1] = 0x00;
 ec_txbuflength[idx] = prevlength + sizeof(ec_comt) - sizeof(uint16) + sizeof(uint16) + length;
 return prevlength + sizeof(ec_comt) - sizeof(uint16) - sizeof(ec_etherheadert);
}

variable_types:
more
unsigned char

variable_types:
prevlength
unsigned short

user_defined_types: struct ec_comt { unsigned short elength; unsigned char command; unsigned char index; unsigned short ADP; unsigned short ADO; unsigned short dlength; unsigned short irpt; }


decompiled_code:
__int64 __fastcall ec_BWR(__int16 a1, __int16 a2, unsigned __int16 a3, void *a4, unsigned int a5)
{
unsigned __int8 v8; // al
unsigned int v9; // ebp
unsigned int v10; // ebx

v8 = ec_getindex();
v9 = v8;
ec_setupdatagram((__int64)&ec_txbuf + 1518 * v8, 8, v8, a1, a2, a3, a4);
v10 = ec_srconfirm(v9, a5);
ec_setbufstat(v9, 0LL);
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int16 a1, __int16 a2, unsigned __int16 a3, void *a4, unsigned int a5)
{
unsigned __int8 v8; // al
unsigned int v9; // ebp
unsigned int v10; // ebx

v8 = ec_getindex();
v9 = v8;
func0((__int64)&ec_txbuf + 1518 * v8, 8, v8, a1, a2, a3, a4);
v10 = ec_srconfirm(v9, a5);
ec_setbufstat(v9, 0LL);
return v10;
}

original_code:
int ec_BWR(uint16 ADP, uint16 ADO, uint16 length, void *data, int timeout)
{
 uint8 idx;
 int wkc;
 idx = ec_getindex();
 ec_setupdatagram(&ec_txbuf[idx], EC_CMD_BWR, idx, ADP, ADO, length, data);
 wkc = ec_srconfirm (idx, timeout);
    ec_setbufstat(idx, EC_BUF_EMPTY);
 return wkc;
}

canonical_original_code:
int ec_BWR(unsigned short ADP, unsigned short ADO, unsigned short length, void *data, int timeout)
{
 unsigned char idx;
 int wkc;
 idx = ec_getindex();
 ec_setupdatagram(&ec_txbuf[idx], EC_CMD_BWR, idx, ADP, ADO, length, data);
 wkc = ec_srconfirm (idx, timeout);
    ec_setbufstat(idx, EC_BUF_EMPTY);
 return wkc;
}

variable_types: timeout int

variable_types: wkc int


decompiled_code:
__int64 __fastcall ec_BRD(__int16 a1, __int16 a2, unsigned __int16 a3, void *a4, unsigned int a5)
{
unsigned __int8 v7; // al
unsigned int v8; // er14
__int64 v9; // rbx
int v10; // ebp

v7 = ec_getindex();
v8 = v7;
v9 = 1518LL * v7;
ec_setupdatagram((__int64)&ec_txbuf + v9, 7, v7, a1, a2, a3, a4);
v10 = ec_srconfirm(v8, a5);
if ( v10 > 0 )
memcpy(a4, (char *)&ec_rxbuf + v9 + 12, a3);
ec_setbufstat(v8, 0LL);
return (unsigned int)v10;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int16 a1, __int16 a2, unsigned __int16 a3, void *a4, unsigned int a5)
{
unsigned __int8 v7; // al
unsigned int v8; // er14
__int64 v9; // rbx
int v10; // ebp

v7 = ec_getindex();
v8 = v7;
v9 = 1518LL * v7;
func0((__int64)&ec_txbuf + v9, 7, v7, a1, a2, a3, a4);
v10 = ec_srconfirm(v8, a5);
if ( v10 > 0 )
memcpy(a4, (char *)&ec_rxbuf + v9 + 12, a3);
ec_setbufstat(v8, 0LL);
return (unsigned int)v10;
}

original_code:
int ec_BRD(uint16 ADP, uint16 ADO, uint16 length, void *data, int timeout)
{
 uint8 idx;
 int wkc;
 idx=ec_getindex();
 ec_setupdatagram(&ec_txbuf[idx], EC_CMD_BRD, idx, ADP, ADO, length, data);
 wkc = ec_srconfirm (idx, timeout);
 if (wkc > 0)
 {
  memcpy(data, &ec_rxbuf[idx][sizeof(ec_comt)], length);
 }
    ec_setbufstat(idx, EC_BUF_EMPTY);
 return wkc;
}

canonical_original_code:
int ec_BRD(unsigned short ADP, unsigned short ADO, unsigned short length, void *data, int timeout)
{
 unsigned char idx;
 int wkc;
 idx=ec_getindex();
 ec_setupdatagram(&ec_txbuf[idx], EC_CMD_BRD, idx, ADP, ADO, length, data);
 wkc = ec_srconfirm (idx, timeout);
 if (wkc > 0)
 {
  memcpy(data, &ec_rxbuf[idx][sizeof(ec_comt)], length);
 }
    ec_setbufstat(idx, EC_BUF_EMPTY);
 return wkc;
}


decompiled_code:
__int64 __fastcall ec_APRD(__int16 a1, __int16 a2, unsigned __int16 a3, void *a4, unsigned int a5)
{
unsigned __int8 v7; // al
unsigned int v8; // er14
__int64 v9; // rbx
int v10; // ebp

v7 = ec_getindex();
v8 = v7;
v9 = 1518LL * v7;
ec_setupdatagram((__int64)&ec_txbuf + v9, 1, v7, a1, a2, a3, a4);
v10 = ec_srconfirm(v8, a5);
if ( v10 > 0 )
memcpy(a4, (char *)&ec_rxbuf + v9 + 12, a3);
ec_setbufstat(v8, 0LL);
return (unsigned int)v10;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int16 a1, __int16 a2, unsigned __int16 a3, void *a4, unsigned int a5)
{
unsigned __int8 v7; // al
unsigned int v8; // er14
__int64 v9; // rbx
int v10; // ebp

v7 = ec_getindex();
v8 = v7;
v9 = 1518LL * v7;
func0((__int64)&ec_txbuf + v9, 1, v7, a1, a2, a3, a4);
v10 = ec_srconfirm(v8, a5);
if ( v10 > 0 )
memcpy(a4, (char *)&ec_rxbuf + v9 + 12, a3);
ec_setbufstat(v8, 0LL);
return (unsigned int)v10;
}

original_code:
int ec_APRD(uint16 ADP, uint16 ADO, uint16 length, void *data, int timeout)
{
    int wkc;
    uint8 idx;
    idx = ec_getindex();
    ec_setupdatagram(&ec_txbuf[idx], EC_CMD_APRD, idx, ADP, ADO, length, data);
    wkc = ec_srconfirm(idx, timeout);
    if (wkc > 0)
    {
        memcpy(data, &ec_rxbuf[idx][sizeof(ec_comt)], length);
    }
    ec_setbufstat(idx, EC_BUF_EMPTY);
    return wkc;
}

canonical_original_code:
int ec_APRD(unsigned short ADP, unsigned short ADO, unsigned short length, void *data, int timeout)
{
    int wkc;
    unsigned char idx;
    idx = ec_getindex();
    ec_setupdatagram(&ec_txbuf[idx], EC_CMD_APRD, idx, ADP, ADO, length, data);
    wkc = ec_srconfirm(idx, timeout);
    if (wkc > 0)
    {
        memcpy(data, &ec_rxbuf[idx][sizeof(ec_comt)], length);
    }
    ec_setbufstat(idx, EC_BUF_EMPTY);
    return wkc;
}


decompiled_code:
__int64 __fastcall ec_ARMW(__int16 a1, __int16 a2, unsigned __int16 a3, void *a4, unsigned int a5)
{
unsigned __int8 v7; // al
unsigned int v8; // er14
__int64 v9; // rbx
int v10; // ebp

v7 = ec_getindex();
v8 = v7;
v9 = 1518LL * v7;
ec_setupdatagram((__int64)&ec_txbuf + v9, 13, v7, a1, a2, a3, a4);
v10 = ec_srconfirm(v8, a5);
if ( v10 > 0 )
memcpy(a4, (char *)&ec_rxbuf + v9 + 12, a3);
ec_setbufstat(v8, 0LL);
return (unsigned int)v10;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int16 a1, __int16 a2, unsigned __int16 a3, void *a4, unsigned int a5)
{
unsigned __int8 v7; // al
unsigned int v8; // er14
__int64 v9; // rbx
int v10; // ebp

v7 = ec_getindex();
v8 = v7;
v9 = 1518LL * v7;
func0((__int64)&ec_txbuf + v9, 13, v7, a1, a2, a3, a4);
v10 = ec_srconfirm(v8, a5);
if ( v10 > 0 )
memcpy(a4, (char *)&ec_rxbuf + v9 + 12, a3);
ec_setbufstat(v8, 0LL);
return (unsigned int)v10;
}

original_code:
int ec_ARMW(uint16 ADP, uint16 ADO, uint16 length, void *data, int timeout)
{
    int wkc;
    uint8 idx;
    idx = ec_getindex();
    ec_setupdatagram(&ec_txbuf[idx], EC_CMD_ARMW, idx, ADP, ADO, length, data);
    wkc = ec_srconfirm(idx, timeout);
    if (wkc > 0)
    {
        memcpy(data, &ec_rxbuf[idx][sizeof(ec_comt)], length);
    }
    ec_setbufstat(idx, EC_BUF_EMPTY);
    return wkc;
}

canonical_original_code:
int ec_ARMW(unsigned short ADP, unsigned short ADO, unsigned short length, void *data, int timeout)
{
    int wkc;
    unsigned char idx;
    idx = ec_getindex();
    ec_setupdatagram(&ec_txbuf[idx], EC_CMD_ARMW, idx, ADP, ADO, length, data);
    wkc = ec_srconfirm(idx, timeout);
    if (wkc > 0)
    {
        memcpy(data, &ec_rxbuf[idx][sizeof(ec_comt)], length);
    }
    ec_setbufstat(idx, EC_BUF_EMPTY);
    return wkc;
}


decompiled_code:
__int64 __fastcall ec_FRMW(__int16 a1, __int16 a2, unsigned __int16 a3, void *a4, unsigned int a5)
{
unsigned __int8 v7; // al
unsigned int v8; // er14
__int64 v9; // rbx
int v10; // ebp

v7 = ec_getindex();
v8 = v7;
v9 = 1518LL * v7;
ec_setupdatagram((__int64)&ec_txbuf + v9, 14, v7, a1, a2, a3, a4);
v10 = ec_srconfirm(v8, a5);
if ( v10 > 0 )
memcpy(a4, (char *)&ec_rxbuf + v9 + 12, a3);
ec_setbufstat(v8, 0LL);
return (unsigned int)v10;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int16 a1, __int16 a2, unsigned __int16 a3, void *a4, unsigned int a5)
{
unsigned __int8 v7; // al
unsigned int v8; // er14
__int64 v9; // rbx
int v10; // ebp

v7 = ec_getindex();
v8 = v7;
v9 = 1518LL * v7;
func0((__int64)&ec_txbuf + v9, 14, v7, a1, a2, a3, a4);
v10 = ec_srconfirm(v8, a5);
if ( v10 > 0 )
memcpy(a4, (char *)&ec_rxbuf + v9 + 12, a3);
ec_setbufstat(v8, 0LL);
return (unsigned int)v10;
}

original_code:
int ec_FRMW(uint16 ADP, uint16 ADO, uint16 length, void *data, int timeout)
{
    int wkc;
    uint8 idx;
    idx = ec_getindex();
    ec_setupdatagram(&ec_txbuf[idx],EC_CMD_FRMW, idx, ADP, ADO, length, data);
    wkc = ec_srconfirm(idx, timeout);
    if (wkc > 0)
    {
        memcpy(data, &ec_rxbuf[idx][sizeof(ec_comt)], length);
    }
    ec_setbufstat(idx, EC_BUF_EMPTY);
    return wkc;
}

canonical_original_code:
int ec_FRMW(unsigned short ADP, unsigned short ADO, unsigned short length, void *data, int timeout)
{
    int wkc;
    unsigned char idx;
    idx = ec_getindex();
    ec_setupdatagram(&ec_txbuf[idx],EC_CMD_FRMW, idx, ADP, ADO, length, data);
    wkc = ec_srconfirm(idx, timeout);
    if (wkc > 0)
    {
        memcpy(data, &ec_rxbuf[idx][sizeof(ec_comt)], length);
    }
    ec_setbufstat(idx, EC_BUF_EMPTY);
    return wkc;
}


decompiled_code:
__int64 __fastcall ec_APRDw(__int16 a1, __int16 a2, unsigned int a3)
{
unsigned __int16 v4[5]; // [rsp+Eh] [rbp-Ah] BYREF

v4[0] = 0;
ec_APRD(a1, a2, 2u, v4, a3);
return v4[0];
}

canonical_decompiled_code:
__int64 __fastcall func7(__int16 a1, __int16 a2, unsigned int a3)
{
unsigned __int16 v4[5]; // [rsp+Eh] [rbp-Ah] BYREF

v4[0] = 0;
func4(a1, a2, 2u, v4, a3);
return v4[0];
}

original_code:
uint16 ec_APRDw(uint16 ADP, uint16 ADO, int timeout)
{
    uint16 w;
 w = 0;
    ec_APRD(ADP,ADO, sizeof(w), &w, timeout);
    return w;
}

canonical_original_code:
unsigned short ec_APRDw(unsigned short ADP, unsigned short ADO, int timeout)
{
    unsigned short w;
 w = 0;
    ec_APRD(ADP,ADO, sizeof(w), &w, timeout);
    return w;
}

variable_types:
w
unsigned short

return_type: unsigned short


decompiled_code:
__int64 __fastcall ec_FPRD(__int16 a1, __int16 a2, unsigned __int16 a3, void *a4, unsigned int a5)
{
unsigned __int8 v7; // al
unsigned int v8; // er14
__int64 v9; // rbx
int v10; // ebp

v7 = ec_getindex();
v8 = v7;
v9 = 1518LL * v7;
ec_setupdatagram((__int64)&ec_txbuf + v9, 4, v7, a1, a2, a3, a4);
v10 = ec_srconfirm(v8, a5);
if ( v10 > 0 )
memcpy(a4, (char *)&ec_rxbuf + v9 + 12, a3);
ec_setbufstat(v8, 0LL);
return (unsigned int)v10;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int16 a1, __int16 a2, unsigned __int16 a3, void *a4, unsigned int a5)
{
unsigned __int8 v7; // al
unsigned int v8; // er14
__int64 v9; // rbx
int v10; // ebp

v7 = ec_getindex();
v8 = v7;
v9 = 1518LL * v7;
func0((__int64)&ec_txbuf + v9, 4, v7, a1, a2, a3, a4);
v10 = ec_srconfirm(v8, a5);
if ( v10 > 0 )
memcpy(a4, (char *)&ec_rxbuf + v9 + 12, a3);
ec_setbufstat(v8, 0LL);
return (unsigned int)v10;
}

original_code:
int ec_FPRD(uint16 ADP, uint16 ADO, uint16 length, void *data, int timeout)
{
    int wkc;
    uint8 idx;
    idx = ec_getindex();
    ec_setupdatagram(&ec_txbuf[idx], EC_CMD_FPRD, idx, ADP, ADO, length, data);
    wkc = ec_srconfirm(idx, timeout);
    if (wkc > 0)
    {
        memcpy(data, &ec_rxbuf[idx][sizeof(ec_comt)], length);
    }
    ec_setbufstat(idx, EC_BUF_EMPTY);
    return wkc;
}

canonical_original_code:
int ec_FPRD(unsigned short ADP, unsigned short ADO, unsigned short length, void *data, int timeout)
{
    int wkc;
    unsigned char idx;
    idx = ec_getindex();
    ec_setupdatagram(&ec_txbuf[idx], EC_CMD_FPRD, idx, ADP, ADO, length, data);
    wkc = ec_srconfirm(idx, timeout);
    if (wkc > 0)
    {
        memcpy(data, &ec_rxbuf[idx][sizeof(ec_comt)], length);
    }
    ec_setbufstat(idx, EC_BUF_EMPTY);
    return wkc;
}


decompiled_code:
__int64 __fastcall ec_FPRDw(__int16 a1, __int16 a2, __int16 a3)
{
unsigned __int16 v4[5]; // [rsp+Eh] [rbp-Ah] BYREF

v4[0] = 0;
ec_FPRD(a1, a2, 2u, v4, a3);
return v4[0];
}

canonical_decompiled_code:
__int64 __fastcall func9(__int16 a1, __int16 a2, __int16 a3)
{
unsigned __int16 v4[5]; // [rsp+Eh] [rbp-Ah] BYREF

v4[0] = 0;
func8(a1, a2, 2u, v4, a3);
return v4[0];
}

original_code:
uint16 ec_FPRDw(uint16 ADP, uint16 ADO, int16 timeout)
{
    uint16 w;
 w = 0;
    ec_FPRD(ADP, ADO, sizeof(w), &w, timeout);
    return w;
}

canonical_original_code:
unsigned short ec_FPRDw(unsigned short ADP, unsigned short ADO, short timeout)
{
    unsigned short w;
 w = 0;
    ec_FPRD(ADP, ADO, sizeof(w), &w, timeout);
    return w;
}

variable_types: timeout short


decompiled_code:
__int64 __fastcall ec_APWR(__int16 a1, __int16 a2, unsigned __int16 a3, void *a4, unsigned int a5)
{
unsigned __int8 v8; // al
unsigned int v9; // ebp
unsigned int v10; // ebx

v8 = ec_getindex();
v9 = v8;
ec_setupdatagram((__int64)&ec_txbuf + 1518 * v8, 2, v8, a1, a2, a3, a4);
v10 = ec_srconfirm(v9, a5);
ec_setbufstat(v9, 0LL);
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func10(__int16 a1, __int16 a2, unsigned __int16 a3, void *a4, unsigned int a5)
{
unsigned __int8 v8; // al
unsigned int v9; // ebp
unsigned int v10; // ebx

v8 = ec_getindex();
v9 = v8;
func0((__int64)&ec_txbuf + 1518 * v8, 2, v8, a1, a2, a3, a4);
v10 = ec_srconfirm(v9, a5);
ec_setbufstat(v9, 0LL);
return v10;
}

original_code:
int ec_APWR(uint16 ADP, uint16 ADO, uint16 length, void *data, int timeout)
{
    uint8 idx;
 int wkc;
    idx = ec_getindex();
    ec_setupdatagram(&ec_txbuf[idx], EC_CMD_APWR, idx, ADP, ADO, length, data);
    wkc=ec_srconfirm(idx, timeout);
    ec_setbufstat(idx, EC_BUF_EMPTY);
 return wkc;
}

canonical_original_code:
int ec_APWR(unsigned short ADP, unsigned short ADO, unsigned short length, void *data, int timeout)
{
    unsigned char idx;
 int wkc;
    idx = ec_getindex();
    ec_setupdatagram(&ec_txbuf[idx], EC_CMD_APWR, idx, ADP, ADO, length, data);
    wkc=ec_srconfirm(idx, timeout);
    ec_setbufstat(idx, EC_BUF_EMPTY);
 return wkc;
}


decompiled_code:
__int64 __fastcall ec_APWRw(__int16 a1, __int16 a2, __int16 a3, unsigned int a4)
{
__int16 v5[6]; // [rsp+Ch] [rbp-Ch] BYREF

v5[0] = a3;
return ec_APWR(a1, a2, 2u, v5, a4);
}

canonical_decompiled_code:
__int64 __fastcall func11(__int16 a1, __int16 a2, __int16 a3, unsigned int a4)
{
__int16 v5[6]; // [rsp+Ch] [rbp-Ch] BYREF

v5[0] = a3;
return func10(a1, a2, 2u, v5, a4);
}

original_code:
int ec_APWRw(uint16 ADP, uint16 ADO, uint16 data, int timeout)
{
    return ec_APWR(ADP,ADO, sizeof(data), &data, timeout);
}

canonical_original_code:
int ec_APWRw(unsigned short ADP, unsigned short ADO, unsigned short data, int timeout)
{
    return ec_APWR(ADP,ADO, sizeof(data), &data, timeout);
}

variable_types:
data
unsigned short


decompiled_code:
__int64 __fastcall ec_FPWR(__int16 a1, __int16 a2, unsigned __int16 a3, void *a4, unsigned int a5)
{
unsigned __int8 v8; // al
unsigned int v9; // ebp
unsigned int v10; // ebx

v8 = ec_getindex();
v9 = v8;
ec_setupdatagram((__int64)&ec_txbuf + 1518 * v8, 5, v8, a1, a2, a3, a4);
v10 = ec_srconfirm(v9, a5);
ec_setbufstat(v9, 0LL);
return v10;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int16 a1, __int16 a2, unsigned __int16 a3, void *a4, unsigned int a5)
{
unsigned __int8 v8; // al
unsigned int v9; // ebp
unsigned int v10; // ebx

v8 = ec_getindex();
v9 = v8;
func0((__int64)&ec_txbuf + 1518 * v8, 5, v8, a1, a2, a3, a4);
v10 = ec_srconfirm(v9, a5);
ec_setbufstat(v9, 0LL);
return v10;
}

original_code:
int ec_FPWR(uint16 ADP, uint16 ADO, uint16 length, void *data, int timeout)
{
    int wkc;
    uint8 idx;
    idx = ec_getindex();
    ec_setupdatagram(&ec_txbuf[idx], EC_CMD_FPWR, idx, ADP, ADO, length, data);
    wkc = ec_srconfirm(idx, timeout);
    ec_setbufstat(idx, EC_BUF_EMPTY);
    return wkc;
}

canonical_original_code:
int ec_FPWR(unsigned short ADP, unsigned short ADO, unsigned short length, void *data, int timeout)
{
    int wkc;
    unsigned char idx;
    idx = ec_getindex();
    ec_setupdatagram(&ec_txbuf[idx], EC_CMD_FPWR, idx, ADP, ADO, length, data);
    wkc = ec_srconfirm(idx, timeout);
    ec_setbufstat(idx, EC_BUF_EMPTY);
    return wkc;
}


decompiled_code:
__int64 __fastcall ec_FPWRw(__int16 a1, __int16 a2, __int16 a3, unsigned int a4)
{
__int16 v5[6]; // [rsp+Ch] [rbp-Ch] BYREF

v5[0] = a3;
return ec_FPWR(a1, a2, 2u, v5, a4);
}

canonical_decompiled_code:
__int64 __fastcall func13(__int16 a1, __int16 a2, __int16 a3, unsigned int a4)
{
__int16 v5[6]; // [rsp+Ch] [rbp-Ch] BYREF

v5[0] = a3;
return func12(a1, a2, 2u, v5, a4);
}

original_code:
int ec_FPWRw(uint16 ADP, uint16 ADO, uint16 data, int timeout)
{
    return ec_FPWR(ADP,ADO, sizeof(data), &data, timeout);
}

canonical_original_code:
int ec_FPWRw(unsigned short ADP, unsigned short ADO, unsigned short data, int timeout)
{
    return ec_FPWR(ADP,ADO, sizeof(data), &data, timeout);
}


decompiled_code:
__int64 __fastcall ec_LRW(int a1, unsigned __int16 a2, void *a3, unsigned int a4)
{
unsigned __int8 v6; // al
unsigned int v7; // ebp
__int64 v8; // r15
int v9; // ebx

v6 = ec_getindex();
v7 = v6;
v8 = 1518LL * v6;
ec_setupdatagram((__int64)&ec_txbuf + v8, 12, v6, a1, SHIWORD(a1), a2, a3);
v9 = ec_srconfirm(v7, a4);
if ( v9 > 0 && ec_rxbuf[1518 * v7 + 2] == 12 )
memcpy(a3, &ec_rxbuf[v8 + 12], a2);
ec_setbufstat(v7, 0LL);
return (unsigned int)v9;
}

canonical_decompiled_code:
__int64 __fastcall func14(int a1, unsigned __int16 a2, void *a3, unsigned int a4)
{
unsigned __int8 v6; // al
unsigned int v7; // ebp
__int64 v8; // r15
int v9; // ebx

v6 = ec_getindex();
v7 = v6;
v8 = 1518LL * v6;
func0((__int64)&ec_txbuf + v8, 12, v6, a1, SHIWORD(a1), a2, a3);
v9 = ec_srconfirm(v7, a4);
if ( v9 > 0 && ec_rxbuf[1518 * v7 + 2] == 12 )
memcpy(a3, &ec_rxbuf[v8 + 12], a2);
ec_setbufstat(v7, 0LL);
return (unsigned int)v9;
}

original_code:
int ec_LRW(uint32 LogAdr, uint16 length, void *data, int timeout)
{
    uint8 idx;
 int wkc;
    idx = ec_getindex();
    ec_setupdatagram(&ec_txbuf[idx], EC_CMD_LRW, idx, ((LogAdr) & 0xffff), ((LogAdr) >> 16), length, data);
    wkc = ec_srconfirm(idx, timeout);
    if ((wkc > 0) && (ec_rxbuf[idx][sizeof(uint16)] == EC_CMD_LRW))
    {
        memcpy(data, &ec_rxbuf[idx][sizeof(ec_comt)], length);
    }
    ec_setbufstat(idx, EC_BUF_EMPTY);
    return wkc;
}

canonical_original_code:
int ec_LRW(unsigned int LogAdr, unsigned short length, void *data, int timeout)
{
    unsigned char idx;
 int wkc;
    idx = ec_getindex();
    ec_setupdatagram(&ec_txbuf[idx], EC_CMD_LRW, idx, ((LogAdr) & 0xffff), ((LogAdr) >> 16), length, data);
    wkc = ec_srconfirm(idx, timeout);
    if ((wkc > 0) && (ec_rxbuf[idx][sizeof(uint16)] == EC_CMD_LRW))
    {
        memcpy(data, &ec_rxbuf[idx][sizeof(ec_comt)], length);
    }
    ec_setbufstat(idx, EC_BUF_EMPTY);
    return wkc;
}

variable_types:
LogAdr
unsigned int


decompiled_code:
__int64 __fastcall ec_LRD(int a1, unsigned __int16 a2, void *a3, unsigned int a4)
{
unsigned __int8 v6; // al
unsigned int v7; // ebp
__int64 v8; // r15
int v9; // ebx

v6 = ec_getindex();
v7 = v6;
v8 = 1518LL * v6;
ec_setupdatagram((__int64)&ec_txbuf + v8, 10, v6, a1, SHIWORD(a1), a2, a3);
v9 = ec_srconfirm(v7, a4);
if ( v9 > 0 && ec_rxbuf[1518 * v7 + 2] == 10 )
memcpy(a3, &ec_rxbuf[v8 + 12], a2);
ec_setbufstat(v7, 0LL);
return (unsigned int)v9;
}

canonical_decompiled_code:
__int64 __fastcall func15(int a1, unsigned __int16 a2, void *a3, unsigned int a4)
{
unsigned __int8 v6; // al
unsigned int v7; // ebp
__int64 v8; // r15
int v9; // ebx

v6 = ec_getindex();
v7 = v6;
v8 = 1518LL * v6;
func0((__int64)&ec_txbuf + v8, 10, v6, a1, SHIWORD(a1), a2, a3);
v9 = ec_srconfirm(v7, a4);
if ( v9 > 0 && ec_rxbuf[1518 * v7 + 2] == 10 )
memcpy(a3, &ec_rxbuf[v8 + 12], a2);
ec_setbufstat(v7, 0LL);
return (unsigned int)v9;
}

original_code:
int ec_LRD(uint32 LogAdr, uint16 length, void *data, int timeout)
{
    uint8 idx;
 int wkc;
    idx = ec_getindex();
    ec_setupdatagram(&ec_txbuf[idx], EC_CMD_LRD, idx, ((LogAdr) & 0xffff), ((LogAdr) >> 16), length, data);
    wkc = ec_srconfirm(idx, timeout);
    if ((wkc > 0) && (ec_rxbuf[idx][sizeof(uint16)]==EC_CMD_LRD))
    {
        memcpy(data, &ec_rxbuf[idx][sizeof(ec_comt)], length);
    }
    ec_setbufstat(idx, EC_BUF_EMPTY);
    return wkc;
}

canonical_original_code:
int ec_LRD(unsigned int LogAdr, unsigned short length, void *data, int timeout)
{
    unsigned char idx;
 int wkc;
    idx = ec_getindex();
    ec_setupdatagram(&ec_txbuf[idx], EC_CMD_LRD, idx, ((LogAdr) & 0xffff), ((LogAdr) >> 16), length, data);
    wkc = ec_srconfirm(idx, timeout);
    if ((wkc > 0) && (ec_rxbuf[idx][sizeof(uint16)]==EC_CMD_LRD))
    {
        memcpy(data, &ec_rxbuf[idx][sizeof(ec_comt)], length);
    }
    ec_setbufstat(idx, EC_BUF_EMPTY);
    return wkc;
}


decompiled_code:
__int64 __fastcall ec_LWR(int a1, unsigned __int16 a2, void *a3, unsigned int a4)
{
unsigned __int8 v6; // al
unsigned int v7; // ebp
unsigned int v8; // ebx

v6 = ec_getindex();
v7 = v6;
ec_setupdatagram((__int64)&ec_txbuf + 1518 * v6, 11, v6, a1, SHIWORD(a1), a2, a3);
v8 = ec_srconfirm(v7, a4);
ec_setbufstat(v7, 0LL);
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func16(int a1, unsigned __int16 a2, void *a3, unsigned int a4)
{
unsigned __int8 v6; // al
unsigned int v7; // ebp
unsigned int v8; // ebx

v6 = ec_getindex();
v7 = v6;
func0((__int64)&ec_txbuf + 1518 * v6, 11, v6, a1, SHIWORD(a1), a2, a3);
v8 = ec_srconfirm(v7, a4);
ec_setbufstat(v7, 0LL);
return v8;
}

original_code:
int ec_LWR(uint32 LogAdr, uint16 length, void *data, int timeout)
{
    uint8 idx;
 int wkc;
    idx = ec_getindex();
    ec_setupdatagram(&ec_txbuf[idx], EC_CMD_LWR, idx, ((LogAdr) & 0xffff), ((LogAdr) >> 16), length, data);
    wkc = ec_srconfirm(idx, timeout);
    ec_setbufstat(idx, EC_BUF_EMPTY);
    return wkc;
}

canonical_original_code:
int ec_LWR(unsigned int LogAdr, unsigned short length, void *data, int timeout)
{
    unsigned char idx;
 int wkc;
    idx = ec_getindex();
    ec_setupdatagram(&ec_txbuf[idx], EC_CMD_LWR, idx, ((LogAdr) & 0xffff), ((LogAdr) >> 16), length, data);
    wkc = ec_srconfirm(idx, timeout);
    ec_setbufstat(idx, EC_BUF_EMPTY);
    return wkc;
}


decompiled_code:
__int64 __fastcall ec_LRWDC(int a1, unsigned __int16 a2, void *a3, __int16 a4, __int64 *a5, unsigned int a6)
{
unsigned __int8 v8; // al
unsigned int v9; // ebp
__int64 v10; // rbx
unsigned __int16 v11; // r12
__int64 src; // [rsp+10h] [rbp-48h] BYREF
unsigned int v16; // [rsp+1Ch] [rbp-3Ch]

v8 = ec_getindex();
v9 = v8;
v10 = 1518LL * v8;
ec_setupdatagram((__int64)&ec_txbuf + v10, 12, v8, a1, SHIWORD(a1), a2, a3);
src = *a5;
v11 = ec_adddatagram((__int64)&ec_txbuf + v10, 14, v9, 0, a4, 2320, 8, &src);
v16 = ec_srconfirm(v9, a6);
if ( (int)v16 > 0 && ec_rxbuf[1518 * v9 + 2] == 12 )
{
memcpy(a3, &ec_rxbuf[v10 + 12], a2);
LOWORD(v16) = *(_WORD *)&ec_rxbuf[a2 + 12 + v10];
src = *(_QWORD *)&ec_rxbuf[v10 + v11];
*a5 = src;
}
ec_setbufstat(v9, 0LL);
return v16;
}

canonical_decompiled_code:
__int64 __fastcall func17(int a1, unsigned __int16 a2, void *a3, __int16 a4, __int64 *a5, unsigned int a6)
{
unsigned __int8 v8; // al
unsigned int v9; // ebp
__int64 v10; // rbx
unsigned __int16 v11; // r12
__int64 src; // [rsp+10h] [rbp-48h] BYREF
unsigned int v16; // [rsp+1Ch] [rbp-3Ch]

v8 = ec_getindex();
v9 = v8;
v10 = 1518LL * v8;
func0((__int64)&ec_txbuf + v10, 12, v8, a1, SHIWORD(a1), a2, a3);
src = *a5;
v11 = func1((__int64)&ec_txbuf + v10, 14, v9, 0, a4, 2320, 8, &src);
v16 = ec_srconfirm(v9, a6);
if ( (int)v16 > 0 && ec_rxbuf[1518 * v9 + 2] == 12 )
{
memcpy(a3, &ec_rxbuf[v10 + 12], a2);
LOWORD(v16) = *(_WORD *)&ec_rxbuf[a2 + 12 + v10];
src = *(_QWORD *)&ec_rxbuf[v10 + v11];
*a5 = src;
}
ec_setbufstat(v9, 0LL);
return v16;
}

original_code:
int ec_LRWDC(uint32 LogAdr, uint16 length, void *data, uint16 DCrs, int64 *DCtime, int timeout)
{
    uint16 DCtO;
    uint8 idx;
 int wkc;
 uint64 DCtE;
    idx = ec_getindex();
    ec_setupdatagram(&ec_txbuf[idx], EC_CMD_LRW, idx, ((LogAdr) & 0xffff), ((LogAdr) >> 16), length, data);
 DCtE = (*DCtime);
    DCtO = ec_adddatagram(&ec_txbuf[idx], EC_CMD_FRMW, idx, 0, DCrs, ECT_REG_DCSYSTIME, sizeof(DCtime), &DCtE);
    wkc = ec_srconfirm(idx, timeout);
    if ((wkc > 0) && (ec_rxbuf[idx][sizeof(uint16)] == EC_CMD_LRW))
    {
        memcpy(data, &ec_rxbuf[idx][sizeof(ec_comt)], length);
  memcpy(&wkc, &ec_rxbuf[idx][sizeof(ec_comt) + length], sizeof(uint16));
  memcpy(&DCtE, &ec_rxbuf[idx][DCtO], sizeof(*DCtime));
  *DCtime = (DCtE);
    }
    ec_setbufstat(idx, EC_BUF_EMPTY);
    return wkc;
}

canonical_original_code:
int ec_LRWDC(unsigned int LogAdr, unsigned short length, void *data, unsigned short DCrs, long long *DCtime, int timeout)
{
    unsigned short DCtO;
    unsigned char idx;
 int wkc;
 unsigned long long DCtE;
    idx = ec_getindex();
    ec_setupdatagram(&ec_txbuf[idx], EC_CMD_LRW, idx, ((LogAdr) & 0xffff), ((LogAdr) >> 16), length, data);
 DCtE = (*DCtime);
    DCtO = ec_adddatagram(&ec_txbuf[idx], EC_CMD_FRMW, idx, 0, DCrs, ECT_REG_DCSYSTIME, sizeof(DCtime), &DCtE);
    wkc = ec_srconfirm(idx, timeout);
    if ((wkc > 0) && (ec_rxbuf[idx][sizeof(uint16)] == EC_CMD_LRW))
    {
        memcpy(data, &ec_rxbuf[idx][sizeof(ec_comt)], length);
  memcpy(&wkc, &ec_rxbuf[idx][sizeof(ec_comt) + length], sizeof(uint16));
  memcpy(&DCtE, &ec_rxbuf[idx][DCtO], sizeof(*DCtime));
  *DCtime = (DCtE);
    }
    ec_setbufstat(idx, EC_BUF_EMPTY);
    return wkc;
}

variable_types:
DCrs
unsigned short

variable_types:
DCtime
long long *

variable_types:
DCtO
unsigned short

variable_types:
DCtE
unsigned long long


original_code:
void ec_dcsync0(uint16 slave, boolean act, uint32 CyclTime, uint32 CyclShift)
{
    uint8 h, RA;
    uint16 wc, slaveh;
    int64 t, t1;
    int32 tc;
    slaveh = ec_slave[slave].configadr;
    RA = 0;
    wc = ec_FPWR(slaveh, ECT_REG_DCSYNCACT, sizeof(RA), &RA, 500);
    if (act)
    {
        RA = 1 + 2;
    }
    h = 0;
    wc = ec_FPWR(slaveh, ECT_REG_DCCUC, sizeof(h), &h, 500);
    wc = ec_FPRD(slaveh, ECT_REG_DCSYSTIME, sizeof(t1), &t1, 500);
 t1 = (t1);
    if (CyclTime > 0)
    {
        t = ((t1 + ((int32)100000000)) / CyclTime) * CyclTime + CyclTime + CyclShift;
    }
    else
    {
        t = t1 + ((int32)100000000) + CyclShift;
    }
 t = (t);
    wc = ec_FPWR(slaveh, ECT_REG_DCSTART0, sizeof(t), &t, 500);
    tc = (CyclTime);
    wc = ec_FPWR(slaveh, ECT_REG_DCCYCLE0, sizeof(tc), &tc, 500);
    wc = ec_FPWR(slaveh, ECT_REG_DCSYNCACT, sizeof(RA), &RA, 500);
}

canonical_original_code:
void ec_dcsync0(unsigned short slave, unsigned char act, unsigned int CyclTime, unsigned int CyclShift)
{
    unsigned char h; unsigned char RA;
    unsigned short wc; unsigned short slaveh;
    long long t; long long t1;
    int tc;
    slaveh = ec_slave[slave].configadr;
    RA = 0;
    wc = ec_FPWR(slaveh, ECT_REG_DCSYNCACT, sizeof(RA), &RA, 500);
    if (act)
    {
        RA = 1 + 2;
    }
    h = 0;
    wc = ec_FPWR(slaveh, ECT_REG_DCCUC, sizeof(h), &h, 500);
    wc = ec_FPRD(slaveh, ECT_REG_DCSYSTIME, sizeof(t1), &t1, 500);
 t1 = (t1);
    if (CyclTime > 0)
    {
        t = ((t1 + ((int)100000000)) / CyclTime) * CyclTime + CyclTime + CyclShift;
    }
    else
    {
        t = t1 + ((int)100000000) + CyclShift;
    }
 t = (t);
    wc = ec_FPWR(slaveh, ECT_REG_DCSTART0, sizeof(t), &t, 500);
    tc = (CyclTime);
    wc = ec_FPWR(slaveh, ECT_REG_DCCYCLE0, sizeof(tc), &tc, 500);
    wc = ec_FPWR(slaveh, ECT_REG_DCSYNCACT, sizeof(RA), &RA, 500);
}

variable_types:
slave
unsigned short

variable_types:
act
unsigned char

variable_types:
CyclTime
unsigned int

variable_types:
CyclShift
unsigned int

variable_types:
h
unsigned char

variable_types:
RA
unsigned char

variable_types:
wc
unsigned short

variable_types:
slaveh
unsigned short

variable_types:
t
long long

variable_types: tc int


original_code:
void ec_dcsync01(uint16 slave, boolean act, uint32 CyclTime0, uint32 CyclTime1, uint32 CyclShift)
{
    uint8 h, RA;
    uint16 wc, slaveh;
    int64 t, t1;
    int32 tc;
    slaveh = ec_slave[slave].configadr;
    RA = 0;
    wc = ec_FPWR(slaveh, ECT_REG_DCSYNCACT, sizeof(RA), &RA, 500);
    if (act)
    {
        RA = 1 + 2 + 4;
    }
    h = 0;
    wc = ec_FPWR(slaveh, ECT_REG_DCCUC, sizeof(h), &h, 500);
    wc = ec_FPRD(slaveh, ECT_REG_DCSYSTIME, sizeof(t1), &t1, 500);
 t1 = (t1);
    if (CyclTime0 > 0)
    {
        t = ((t1 + ((int32)100000000)) / CyclTime0) * CyclTime0 + CyclTime0 + CyclShift;
    }
    else
    {
        t = t1 + ((int32)100000000) + CyclShift;
    }
 t = (t);
    wc = ec_FPWR(slaveh, ECT_REG_DCSTART0, sizeof(t), &t, 500);
    tc = (CyclTime0);
    wc = ec_FPWR(slaveh, ECT_REG_DCCYCLE0, sizeof(tc), &tc, 500);
    tc = (CyclTime1);
    wc = ec_FPWR(slaveh, ECT_REG_DCCYCLE1, sizeof(tc), &tc, 500);
    wc = ec_FPWR(slaveh, ECT_REG_DCSYNCACT, sizeof(RA), &RA, 500);
}

canonical_original_code:
void ec_dcsync01(unsigned short slave, unsigned char act, unsigned int CyclTime0, unsigned int CyclTime1, unsigned int CyclShift)
{
    unsigned char h; unsigned char RA;
    unsigned short wc; unsigned short slaveh;
    long long t; long long t1;
    int tc;
    slaveh = ec_slave[slave].configadr;
    RA = 0;
    wc = ec_FPWR(slaveh, ECT_REG_DCSYNCACT, sizeof(RA), &RA, 500);
    if (act)
    {
        RA = 1 + 2 + 4;
    }
    h = 0;
    wc = ec_FPWR(slaveh, ECT_REG_DCCUC, sizeof(h), &h, 500);
    wc = ec_FPRD(slaveh, ECT_REG_DCSYSTIME, sizeof(t1), &t1, 500);
 t1 = (t1);
    if (CyclTime0 > 0)
    {
        t = ((t1 + ((int)100000000)) / CyclTime0) * CyclTime0 + CyclTime0 + CyclShift;
    }
    else
    {
        t = t1 + ((int)100000000) + CyclShift;
    }
 t = (t);
    wc = ec_FPWR(slaveh, ECT_REG_DCSTART0, sizeof(t), &t, 500);
    tc = (CyclTime0);
    wc = ec_FPWR(slaveh, ECT_REG_DCCYCLE0, sizeof(tc), &tc, 500);
    tc = (CyclTime1);
    wc = ec_FPWR(slaveh, ECT_REG_DCCYCLE1, sizeof(tc), &tc, 500);
    wc = ec_FPWR(slaveh, ECT_REG_DCSYNCACT, sizeof(RA), &RA, 500);
}


decompiled_code:
__int64 __fastcall ec_porttime(unsigned __int16 a1, unsigned __int8 a2)
{
__int64 result; // rax

if ( a2 == 2 )
return *(unsigned int *)&ec_slave[160 * a1 + 114];
if ( a2 > 2u )
{
if ( a2 == 3 )
result = *(unsigned int *)&ec_slave[160 * a1 + 116];
else
result = 0LL;
}
else if ( a2 )
{
result = *(unsigned int *)&ec_slave[160 * a1 + 112];
}
else
{
result = *(unsigned int *)&ec_slave[160 * a1 + 110];
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int16 a1, unsigned __int8 a2)
{
__int64 result; // rax

if ( a2 == 2 )
return *(unsigned int *)&ec_slave[160 * a1 + 114];
if ( a2 > 2u )
{
if ( a2 == 3 )
result = *(unsigned int *)&ec_slave[160 * a1 + 116];
else
result = 0LL;
}
else if ( a2 )
{
result = *(unsigned int *)&ec_slave[160 * a1 + 112];
}
else
{
result = *(unsigned int *)&ec_slave[160 * a1 + 110];
}
return result;
}

original_code:
int32 ec_porttime(uint16 slave, uint8 port)
{
 int32 ts;
 switch (port)
 {
  case 0:
   ts = ec_slave[slave].DCrtA;
   break;
  case 1:
   ts = ec_slave[slave].DCrtB;
   break;
  case 2:
   ts = ec_slave[slave].DCrtC;
   break;
  case 3:
   ts = ec_slave[slave].DCrtD;
   break;
  default:
   ts = 0;
 }
 return ts;
}

canonical_original_code:
int ec_porttime(unsigned short slave, unsigned char port)
{
 int ts;
 switch (port)
 {
  case 0:
   ts = ec_slave[slave].DCrtA;
   break;
  case 1:
   ts = ec_slave[slave].DCrtB;
   break;
  case 2:
   ts = ec_slave[slave].DCrtC;
   break;
  case 3:
   ts = ec_slave[slave].DCrtD;
   break;
  default:
   ts = 0;
 }
 return ts;
}

variable_types:
port
unsigned char

variable_types: ts int


decompiled_code:
__int64 __fastcall ec_prevport(unsigned __int16 a1, unsigned int a2)
{
char v2; // dl
__int64 result; // rax

v2 = ec_slave[160 * a1 + 106];
if ( (_BYTE)a2 == 2 )
{
result = 1LL;
if ( (v2 & 2) == 0 )
{
result = v2 & 8;
if ( (v2 & 8) != 0 )
{
result = 3LL;
}
else if ( (v2 & 1) == 0 )
{
result = a2;
}
}
}
else if ( (unsigned __int8)a2 > 2u )
{
if ( (_BYTE)a2 == 3 )
{
result = 0LL;
if ( (v2 & 1) == 0 )
{
result = 2LL;
if ( (v2 & 4) == 0 )
{
result = 1LL;
if ( (v2 & 2) == 0 )
result = a2;
}
}
}
else
{
result = a2;
}
}
else if ( (_BYTE)a2 )
{
result = v2 & 8;
if ( (v2 & 8) != 0 )
{
result = 3LL;
}
else if ( (v2 & 1) == 0 )
{
LOBYTE(result) = (v2 & 4) != 0;
result = (unsigned int)(result + 1);
}
}
else
{
result = (v2 & 2) != 0;
if ( (v2 & 4) != 0 )
result = 2LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int16 a1, unsigned int a2)
{
char v2; // dl
__int64 result; // rax

v2 = ec_slave[160 * a1 + 106];
if ( (_BYTE)a2 == 2 )
{
result = 1LL;
if ( (v2 & 2) == 0 )
{
result = v2 & 8;
if ( (v2 & 8) != 0 )
{
result = 3LL;
}
else if ( (v2 & 1) == 0 )
{
result = a2;
}
}
}
else if ( (unsigned __int8)a2 > 2u )
{
if ( (_BYTE)a2 == 3 )
{
result = 0LL;
if ( (v2 & 1) == 0 )
{
result = 2LL;
if ( (v2 & 4) == 0 )
{
result = 1LL;
if ( (v2 & 2) == 0 )
result = a2;
}
}
}
else
{
result = a2;
}
}
else if ( (_BYTE)a2 )
{
result = v2 & 8;
if ( (v2 & 8) != 0 )
{
result = 3LL;
}
else if ( (v2 & 1) == 0 )
{
LOBYTE(result) = (v2 & 4) != 0;
result = (unsigned int)(result + 1);
}
}
else
{
result = (v2 & 2) != 0;
if ( (v2 & 4) != 0 )
result = 2LL;
}
return result;
}

original_code:
uint8 ec_prevport(uint16 slave, uint8 port)
{
 uint8 pport = port;
 uint8 aport = ec_slave[slave].activeports;
 switch(port)
 {
  case 0:
   if(aport & 0x04)
    pport = 2;
   else if (aport & 0x02)
    pport = 1;
   else if (aport & 0x04)
    pport = 3;
   break;
  case 1:
   if(aport & 0x08)
    pport = 3;
   else if (aport & 0x01)
    pport = 0;
   else if (aport & 0x04)
    pport = 2;
   break;
  case 2:
   if(aport & 0x02)
    pport = 1;
   else if (aport & 0x08)
    pport = 3;
   else if (aport & 0x01)
    pport = 0;
   break;
  case 3:
   if(aport & 0x01)
    pport = 0;
   else if (aport & 0x04)
    pport = 2;
   else if (aport & 0x02)
    pport = 1;
   break;
 }
 return pport;
}

canonical_original_code:
unsigned char ec_prevport(unsigned short slave, unsigned char port)
{
 unsigned char pport = port;
 unsigned char aport = ec_slave[slave].activeports;
 switch(port)
 {
  case 0:
   if(aport & 0x04)
    pport = 2;
   else if (aport & 0x02)
    pport = 1;
   else if (aport & 0x04)
    pport = 3;
   break;
  case 1:
   if(aport & 0x08)
    pport = 3;
   else if (aport & 0x01)
    pport = 0;
   else if (aport & 0x04)
    pport = 2;
   break;
  case 2:
   if(aport & 0x02)
    pport = 1;
   else if (aport & 0x08)
    pport = 3;
   else if (aport & 0x01)
    pport = 0;
   break;
  case 3:
   if(aport & 0x01)
    pport = 0;
   else if (aport & 0x04)
    pport = 2;
   else if (aport & 0x02)
    pport = 1;
   break;
 }
 return pport;
}

variable_types:
pport
unsigned char

variable_types:
aport
unsigned char

return_type: unsigned char


original_code:
uint8 ec_parentport(uint16 parent)
{
 uint8 parentport = 0;
 uint8 b;
 b = ec_slave[parent].consumedports;
 if (b & 0x08)
 {
  parentport = 3;
  b &= (uint8)~0x08;
 }
 else if (b & 0x02)
 {
  parentport = 1;
  b &= (uint8)~0x02;
 }
 else if (b & 0x04)
 {
  parentport = 2;
  b &= (uint8)~0x04;
 }
 else if (b & 0x01)
 {
  parentport = 0;
  b &= (uint8)~0x01;
 }
 ec_slave[parent].consumedports = b;
 return parentport;
}

canonical_original_code:
unsigned char ec_parentport(unsigned short parent)
{
 unsigned char parentport = 0;
 unsigned char b;
 b = ec_slave[parent].consumedports;
 if (b & 0x08)
 {
  parentport = 3;
  b &= (unsigned char)~0x08;
 }
 else if (b & 0x02)
 {
  parentport = 1;
  b &= (unsigned char)~0x02;
 }
 else if (b & 0x04)
 {
  parentport = 2;
  b &= (unsigned char)~0x04;
 }
 else if (b & 0x01)
 {
  parentport = 0;
  b &= (unsigned char)~0x01;
 }
 ec_slave[parent].consumedports = b;
 return parentport;
}

variable_types:
parent
unsigned short

variable_types:
parentport
unsigned char

variable_types:
b
unsigned char


original_code:
boolean ec_configdc(void)
{
    uint16 i, wc, slaveh, parent, child;
 uint16 parenthold = 0;
 uint16 prevDCslave = 0;
    int32 ht, dt1, dt2, dt3;
    int64 hrt;
 uint8 entryport;
 int8 nlist;
 int8 plist[4];
 int32 tlist[4];
 ec_slave[0].hasdc = 0;
 ec_group[0].hasdc = 0;
    ht = 0;
    ec_BWR(0, ECT_REG_DCTIME0, sizeof(ht), &ht, 500);
    for (i = 1; i <= ec_slavecount; i++)
    {
  ec_slave[i].consumedports = ec_slave[i].activeports;
        if (ec_slave[i].hasdc)
        {
            if (!ec_slave[0].hasdc)
            {
    ec_slave[0].hasdc = 1;
    ec_slave[0].DCnext = i;
    ec_slave[i].DCprevious = 0;
    ec_group[0].hasdc = 1;
    ec_group[0].DCnext = i;
            }
   else
   {
    ec_slave[prevDCslave].DCnext = i;
    ec_slave[i].DCprevious = prevDCslave;
   }
   parenthold = 0;
   prevDCslave = i;
            slaveh = ec_slave[i].configadr;
            wc = ec_FPRD(slaveh, ECT_REG_DCTIME0, sizeof(ht), &ht, 500);
            ec_slave[i].DCrtA = (ht);
            wc = ec_FPRD(slaveh, ECT_REG_DCSOF, sizeof(hrt), &hrt, 500);
            hrt = (-(hrt));
            wc = ec_FPWR(slaveh, ECT_REG_DCSYSOFFSET, sizeof(hrt), &hrt, 500);
            wc = ec_FPRD(slaveh, ECT_REG_DCTIME1, sizeof(ht), &ht, 500);
            ec_slave[i].DCrtB = (ht);
            wc = ec_FPRD(slaveh, ECT_REG_DCTIME2, sizeof(ht), &ht, 500);
            ec_slave[i].DCrtC = (ht);
            wc = ec_FPRD(slaveh, ECT_REG_DCTIME3, sizeof(ht), &ht, 500);
            ec_slave[i].DCrtD = (ht);
   nlist = 0;
   if (ec_slave[i].activeports & 0x01)
   {
    plist[nlist] = 0;
    tlist[nlist] = ec_slave[i].DCrtA;
    nlist++;
   }
   if (ec_slave[i].activeports & 0x08)
   {
    plist[nlist] = 3;
    tlist[nlist] = ec_slave[i].DCrtD;
    nlist++;
   }
   if (ec_slave[i].activeports & 0x02)
   {
    plist[nlist] = 1;
    tlist[nlist] = ec_slave[i].DCrtB;
    nlist++;
   }
   if (ec_slave[i].activeports & 0x04)
   {
    plist[nlist] = 2;
    tlist[nlist] = ec_slave[i].DCrtC;
    nlist++;
   }
   entryport = 0;
   if((nlist > 1) && (tlist[1] < tlist[entryport]))
   {
    entryport = 1;
   }
   if((nlist > 2) && (tlist[2] < tlist[entryport]))
   {
    entryport = 2;
   }
   if((nlist > 3) && (tlist[3] < tlist[entryport]))
   {
    entryport = 3;
   }
   entryport = plist[entryport];
   ec_slave[i].entryport = entryport;
   ec_slave[i].consumedports &= (uint8)~(1 << entryport);
            parent = i;
            do
            {
                child = parent;
                parent = ec_slave[parent].parent;
            }
            while (!((parent == 0) || (ec_slave[parent].hasdc)));
            if (parent > 0)
            {
    ec_slave[i].parentport = ec_parentport(parent);
    if (ec_slave[parent].topology == 1)
    {
     ec_slave[i].parentport = ec_slave[parent].entryport;
    }
    dt1 = 0;
    dt2 = 0;
    dt3 = ec_porttime(parent, ec_slave[i].parentport) -
       ec_porttime(parent, ec_prevport(parent, ec_slave[i].parentport));
    if (ec_slave[i].topology > 1)
    {
     dt1 = ec_porttime(i, ec_prevport(i, ec_slave[i].entryport)) -
           ec_porttime(i, ec_slave[i].entryport);
    }
    if (dt1 > dt3) dt1 = -dt1;
    if ((child - parent) > 1)
    {
     dt2 = ec_porttime(parent, ec_prevport(parent, ec_slave[i].parentport)) -
           ec_porttime(parent, ec_slave[parent].entryport);
    }
    if (dt2 < 0) dt2 = -dt2;
                ec_slave[i].pdelay = ((dt3 - dt1) / 2) + dt2 + ec_slave[parent].pdelay;
                ht = (ec_slave[i].pdelay);
                wc = ec_FPWR(slaveh, ECT_REG_DCSYSDELAY, sizeof(ht), &ht, 500);
            }
        }
        else
        {
            ec_slave[i].DCrtA = 0;
            ec_slave[i].DCrtB = 0;
            ec_slave[i].DCrtC = 0;
            ec_slave[i].DCrtD = 0;
   parent = ec_slave[i].parent;
   if ( (parent > 0) && (ec_slave[parent].topology > 2))
    parenthold = parent;
   if ( parenthold && (ec_slave[i].topology == 1))
   {
    ec_parentport(parenthold);
    parenthold = 0;
   }
        }
    }
    return ec_slave[0].hasdc;
}

canonical_original_code:
unsigned char ec_configdc()
{
    unsigned short i; unsigned short wc; unsigned short slaveh; unsigned short parent; unsigned short child;
 unsigned short parenthold = 0;
 unsigned short prevDCslave = 0;
    int ht; int dt1; int dt2; int dt3;
    long long hrt;
 unsigned char entryport;
 char nlist;
 char plist[4];
 int tlist[4];
 ec_slave[0].hasdc = 0;
 ec_group[0].hasdc = 0;
    ht = 0;
    ec_BWR(0, ECT_REG_DCTIME0, sizeof(ht), &ht, 500);
    for (i = 1; i <= ec_slavecount; i++)
    {
  ec_slave[i].consumedports = ec_slave[i].activeports;
        if (ec_slave[i].hasdc)
        {
            if (!ec_slave[0].hasdc)
            {
    ec_slave[0].hasdc = 1;
    ec_slave[0].DCnext = i;
    ec_slave[i].DCprevious = 0;
    ec_group[0].hasdc = 1;
    ec_group[0].DCnext = i;
            }
   else
   {
    ec_slave[prevDCslave].DCnext = i;
    ec_slave[i].DCprevious = prevDCslave;
   }
   parenthold = 0;
   prevDCslave = i;
            slaveh = ec_slave[i].configadr;
            wc = ec_FPRD(slaveh, ECT_REG_DCTIME0, sizeof(ht), &ht, 500);
            ec_slave[i].DCrtA = (ht);
            wc = ec_FPRD(slaveh, ECT_REG_DCSOF, sizeof(hrt), &hrt, 500);
            hrt = (-(hrt));
            wc = ec_FPWR(slaveh, ECT_REG_DCSYSOFFSET, sizeof(hrt), &hrt, 500);
            wc = ec_FPRD(slaveh, ECT_REG_DCTIME1, sizeof(ht), &ht, 500);
            ec_slave[i].DCrtB = (ht);
            wc = ec_FPRD(slaveh, ECT_REG_DCTIME2, sizeof(ht), &ht, 500);
            ec_slave[i].DCrtC = (ht);
            wc = ec_FPRD(slaveh, ECT_REG_DCTIME3, sizeof(ht), &ht, 500);
            ec_slave[i].DCrtD = (ht);
   nlist = 0;
   if (ec_slave[i].activeports & 0x01)
   {
    plist[nlist] = 0;
    tlist[nlist] = ec_slave[i].DCrtA;
    nlist++;
   }
   if (ec_slave[i].activeports & 0x08)
   {
    plist[nlist] = 3;
    tlist[nlist] = ec_slave[i].DCrtD;
    nlist++;
   }
   if (ec_slave[i].activeports & 0x02)
   {
    plist[nlist] = 1;
    tlist[nlist] = ec_slave[i].DCrtB;
    nlist++;
   }
   if (ec_slave[i].activeports & 0x04)
   {
    plist[nlist] = 2;
    tlist[nlist] = ec_slave[i].DCrtC;
    nlist++;
   }
   entryport = 0;
   if((nlist > 1) && (tlist[1] < tlist[entryport]))
   {
    entryport = 1;
   }
   if((nlist > 2) && (tlist[2] < tlist[entryport]))
   {
    entryport = 2;
   }
   if((nlist > 3) && (tlist[3] < tlist[entryport]))
   {
    entryport = 3;
   }
   entryport = plist[entryport];
   ec_slave[i].entryport = entryport;
   ec_slave[i].consumedports &= (unsigned char)~(1 << entryport);
            parent = i;
            do
            {
                child = parent;
                parent = ec_slave[parent].parent;
            }
            while (!((parent == 0) || (ec_slave[parent].hasdc)));
            if (parent > 0)
            {
    ec_slave[i].parentport = ec_parentport(parent);
    if (ec_slave[parent].topology == 1)
    {
     ec_slave[i].parentport = ec_slave[parent].entryport;
    }
    dt1 = 0;
    dt2 = 0;
    dt3 = ec_porttime(parent, ec_slave[i].parentport) -
       ec_porttime(parent, ec_prevport(parent, ec_slave[i].parentport));
    if (ec_slave[i].topology > 1)
    {
     dt1 = ec_porttime(i, ec_prevport(i, ec_slave[i].entryport)) -
           ec_porttime(i, ec_slave[i].entryport);
    }
    if (dt1 > dt3) dt1 = -dt1;
    if ((child - parent) > 1)
    {
     dt2 = ec_porttime(parent, ec_prevport(parent, ec_slave[i].parentport)) -
           ec_porttime(parent, ec_slave[parent].entryport);
    }
    if (dt2 < 0) dt2 = -dt2;
                ec_slave[i].pdelay = ((dt3 - dt1) / 2) + dt2 + ec_slave[parent].pdelay;
                ht = (ec_slave[i].pdelay);
                wc = ec_FPWR(slaveh, ECT_REG_DCSYSDELAY, sizeof(ht), &ht, 500);
            }
        }
        else
        {
            ec_slave[i].DCrtA = 0;
            ec_slave[i].DCrtB = 0;
            ec_slave[i].DCrtC = 0;
            ec_slave[i].DCrtD = 0;
   parent = ec_slave[i].parent;
   if ( (parent > 0) && (ec_slave[parent].topology > 2))
    parenthold = parent;
   if ( parenthold && (ec_slave[i].topology == 1))
   {
    ec_parentport(parenthold);
    parenthold = 0;
   }
        }
    }
    return ec_slave[0].hasdc;
}

variable_types:
i
unsigned short

variable_types:
child
unsigned short

variable_types:
parenthold
unsigned short

variable_types:
prevDCslave
unsigned short

variable_types: ht int

variable_types:
hrt
long long

variable_types:
entryport
unsigned char

variable_types: nlist char

variable_types:
plist
char [4]

variable_types:
tlist
int [4]


decompiled_code:
char *__fastcall ec_sdoerror2string(int a1)
{
unsigned __int64 v1; // rax
int v2; // ecx
int v3; // edx

v1 = (unsigned int)((_DWORD)&ec_sdoerrorlist + 132);
v2 = 0;
do
{
++v2;
v3 = *(_DWORD *)v1;
v1 += 132LL;
}
while ( v3 != a1 && v3 != -1 );
return (char *)&ec_sdoerrorlist + 132 * v2 + 4;
}

canonical_decompiled_code:
char *__fastcall func0(int a1)
{
unsigned __int64 v1; // rax
int v2; // ecx
int v3; // edx

v1 = (unsigned int)((_DWORD)&ec_sdoerrorlist + 132);
v2 = 0;
do
{
++v2;
v3 = *(_DWORD *)v1;
v1 += 132LL;
}
while ( v3 != a1 && v3 != -1 );
return (char *)&ec_sdoerrorlist + 132 * v2 + 4;
}

original_code:
char* ec_sdoerror2string( uint32 sdoerrorcode)
{
 int i = 0;
 do
 {
  i++;
 }
 while ( (ec_sdoerrorlist[i].errorcode != 0xfffffffful) &&
   (ec_sdoerrorlist[i].errorcode != sdoerrorcode) );
 return (char*) ec_sdoerrorlist[i].errordescription;
}

canonical_original_code:
char *ec_sdoerror2string(unsigned int sdoerrorcode)
{
 int i = 0;
 do
 {
  i++;
 }
 while ( (ec_sdoerrorlist[i].errorcode != 0xfffffffful) &&
   (ec_sdoerrorlist[i].errorcode != sdoerrorcode) );
 return (char *) ec_sdoerrorlist[i].errordescription;
}

variable_types:
sdoerrorcode
unsigned int

variable_types: i int

return_type: char *


decompiled_code:
char *__fastcall ec_ALstatuscode2string(__int16 a1)
{
unsigned __int64 v1; // rax
int v2; // ecx
__int16 v3; // dx

v1 = (unsigned int)((_DWORD)&ec_ALstatuscodelist + 130);
v2 = 0;
do
{
++v2;
v3 = *(_WORD *)v1;
v1 += 130LL;
}
while ( v3 != a1 && v3 != -1 );
return (char *)&ec_ALstatuscodelist + 130 * v2 + 2;
}

canonical_decompiled_code:
char *__fastcall func1(__int16 a1)
{
unsigned __int64 v1; // rax
int v2; // ecx
__int16 v3; // dx

v1 = (unsigned int)((_DWORD)&ec_ALstatuscodelist + 130);
v2 = 0;
do
{
++v2;
v3 = *(_WORD *)v1;
v1 += 130LL;
}
while ( v3 != a1 && v3 != -1 );
return (char *)&ec_ALstatuscodelist + 130 * v2 + 2;
}

original_code:
char* ec_ALstatuscode2string( uint16 ALstatuscode)
{
 int i = 0;
 do
 {
  i++;
 }
 while ( (ec_ALstatuscodelist[i].ALstatuscode != 0xffff) &&
   (ec_ALstatuscodelist[i].ALstatuscode != ALstatuscode) );
 return (char *) ec_ALstatuscodelist[i].ALstatuscodedescription;
}

canonical_original_code:
char *ec_ALstatuscode2string(unsigned short ALstatuscode)
{
 int i = 0;
 do
 {
  i++;
 }
 while ( (ec_ALstatuscodelist[i].ALstatuscode != 0xffff) &&
   (ec_ALstatuscodelist[i].ALstatuscode != ALstatuscode) );
 return (char *) ec_ALstatuscodelist[i].ALstatuscodedescription;
}

variable_types:
ALstatuscode
unsigned short


decompiled_code:
char *__fastcall ec_soeerror2string(__int16 a1)
{
unsigned __int64 v1; // rax
int v2; // ecx
__int16 v3; // dx

v1 = (unsigned int)((_DWORD)&ec_soeerrorlist + 130);
v2 = 0;
do
{
++v2;
v3 = *(_WORD *)v1;
v1 += 130LL;
}
while ( v3 != a1 && v3 != -1 );
return (char *)&ec_soeerrorlist + 130 * v2 + 2;
}

canonical_decompiled_code:
char *__fastcall func2(__int16 a1)
{
unsigned __int64 v1; // rax
int v2; // ecx
__int16 v3; // dx

v1 = (unsigned int)((_DWORD)&ec_soeerrorlist + 130);
v2 = 0;
do
{
++v2;
v3 = *(_WORD *)v1;
v1 += 130LL;
}
while ( v3 != a1 && v3 != -1 );
return (char *)&ec_soeerrorlist + 130 * v2 + 2;
}

original_code:
char* ec_soeerror2string( uint16 errorcode)
{
 int i = 0;
 do
 {
  i++;
 }
 while ( (ec_soeerrorlist[i].errorcode != 0xffff) &&
   (ec_soeerrorlist[i].errorcode != errorcode) );
 return (char *) ec_soeerrorlist[i].errordescription;
}

canonical_original_code:
char *ec_soeerror2string(unsigned short errorcode)
{
 int i = 0;
 do
 {
  i++;
 }
 while ( (ec_soeerrorlist[i].errorcode != 0xffff) &&
   (ec_soeerrorlist[i].errorcode != errorcode) );
 return (char *) ec_soeerrorlist[i].errordescription;
}

variable_types:
errorcode
unsigned short


decompiled_code:
char *ec_elist2string()
{
char v0; // dl
char *result; // rax
__int64 v2; // rbx
char *v3; // rax
char *v4; // rax
char v5[32]; // [rsp+0h] [rbp-58h] BYREF
__int64 v6; // [rsp+20h] [rbp-38h] BYREF
__int64 v7; // [rsp+28h] [rbp-30h]
unsigned __int16 v8; // [rsp+32h] [rbp-26h]
unsigned __int16 v9; // [rsp+34h] [rbp-24h]
unsigned __int8 v10; // [rsp+36h] [rbp-22h]
int v11; // [rsp+38h] [rbp-20h]
unsigned int v12; // [rsp+3Ch] [rbp-1Ch]

v0 = ec_poperror(&v6);
result = (char *)&unk_2FF;
if ( v0 )
{
sprintf(v5, "Time:%12.3f", (double)(int)v7 / 1000000.0 + (double)(int)v6);
switch ( v11 )
{
case 0:
case 4:
v2 = v12;
v3 = ec_sdoerror2string(v12);
sprintf(estring, "%s SDO slave:%d index:%4.4x.%2.2x error:%8.8x %s\n", v5, v8, v9, v10, v2, v3);
result = estring;
break;
case 1:
sprintf(estring, "%s EMERGENCY slave:%d error:%4.4x\n", v5, v8, (unsigned __int16)v12);
result = estring;
break;
case 3:
sprintf(estring, "%s PACKET slave:%d index:%4.4x.%2.2x error:%d\n", v5, v8, v9, v10, (unsigned __int16)v12);
result = estring;
break;
case 8:
v4 = ec_soeerror2string(v12);
sprintf(estring, "%s SoE slave:%d IDN:%4.4x error:%4.4x %s\n", v5, v8, v9, v12, v4);
result = estring;
break;
default:
sprintf(estring, "%s error:%8.8x\n", v5, v12);
result = estring;
break;
}
}
return result;
}

canonical_decompiled_code:
char *func3()
{
char v0; // dl
char *result; // rax
__int64 v2; // rbx
char *v3; // rax
char *v4; // rax
char v5[32]; // [rsp+0h] [rbp-58h] BYREF
__int64 v6; // [rsp+20h] [rbp-38h] BYREF
__int64 v7; // [rsp+28h] [rbp-30h]
unsigned __int16 v8; // [rsp+32h] [rbp-26h]
unsigned __int16 v9; // [rsp+34h] [rbp-24h]
unsigned __int8 v10; // [rsp+36h] [rbp-22h]
int v11; // [rsp+38h] [rbp-20h]
unsigned int v12; // [rsp+3Ch] [rbp-1Ch]

v0 = ec_poperror(&v6);
result = (char *)&unk_2FF;
if ( v0 )
{
sprintf(v5, "Time:%12.3f", (double)(int)v7 / 1000000.0 + (double)(int)v6);
switch ( v11 )
{
case 0:
case 4:
v2 = v12;
v3 = func0(v12);
sprintf(estring, "%s SDO slave:%d index:%4.4x.%2.2x error:%8.8x %s\n", v5, v8, v9, v10, v2, v3);
result = estring;
break;
case 1:
sprintf(estring, "%s EMERGENCY slave:%d error:%4.4x\n", v5, v8, (unsigned __int16)v12);
result = estring;
break;
case 3:
sprintf(estring, "%s PACKET slave:%d index:%4.4x.%2.2x error:%d\n", v5, v8, v9, v10, (unsigned __int16)v12);
result = estring;
break;
case 8:
v4 = func2(v12);
sprintf(estring, "%s SoE slave:%d IDN:%4.4x error:%4.4x %s\n", v5, v8, v9, v12, v4);
result = estring;
break;
default:
sprintf(estring, "%s error:%8.8x\n", v5, v12);
result = estring;
break;
}
}
return result;
}

original_code:
char* ec_elist2string(void)
{
 ec_errort Ec;
 char timestr[20];
 if (ec_poperror(&Ec))
 {
  sprintf(timestr, "Time:%12.3f", Ec.Time.tv_sec + (Ec.Time.tv_usec / 1000000.0) );
  switch (Ec.Etype)
  {
   case EC_ERR_TYPE_SDO_ERROR:
    sprintf(estring, "%s SDO slave:%d index:%4.4x.%2.2x error:%8.8x %s\n",
     timestr, Ec.Slave, Ec.Index, Ec.SubIdx, Ec.AbortCode, ec_sdoerror2string(Ec.AbortCode));
    break;
   case EC_ERR_TYPE_EMERGENCY:
    sprintf(estring, "%s EMERGENCY slave:%d error:%4.4x\n",
     timestr, Ec.Slave, Ec.ErrorCode);
    break;
   case EC_ERR_TYPE_PACKET_ERROR:
    sprintf(estring, "%s PACKET slave:%d index:%4.4x.%2.2x error:%d\n",
     timestr, Ec.Slave, Ec.Index, Ec.SubIdx, Ec.ErrorCode);
    break;
   case EC_ERR_TYPE_SDOINFO_ERROR:
    sprintf(estring, "%s SDO slave:%d index:%4.4x.%2.2x error:%8.8x %s\n",
     timestr, Ec.Slave, Ec.Index, Ec.SubIdx, Ec.AbortCode, ec_sdoerror2string(Ec.AbortCode));
    break;
   case EC_ERR_TYPE_SOE_ERROR:
    sprintf(estring, "%s SoE slave:%d IDN:%4.4x error:%4.4x %s\n",
     timestr, Ec.Slave, Ec.Index, Ec.AbortCode, ec_soeerror2string(Ec.ErrorCode));
    break;
   default:
    sprintf(estring, "%s error:%8.8x\n",
     timestr, Ec.AbortCode);
  }
  return (char*) estring;
 }
 else return "";
}

canonical_original_code:
char *ec_elist2string()
{
 struct ec_errort Ec;
 char timestr[20];
 if (ec_poperror(&Ec))
 {
  sprintf(timestr, "Time:%12.3f", Ec.Time.tv_sec + (Ec.Time.tv_usec / 1000000.0) );
  switch (Ec.Etype)
  {
   case EC_ERR_TYPE_SDO_ERROR:
    sprintf(estring, "%s SDO slave:%d index:%4.4x.%2.2x error:%8.8x %s\n",
     timestr, Ec.Slave, Ec.Index, Ec.SubIdx, Ec.AbortCode, ec_sdoerror2string(Ec.AbortCode));
    break;
   case EC_ERR_TYPE_EMERGENCY:
    sprintf(estring, "%s EMERGENCY slave:%d error:%4.4x\n",
     timestr, Ec.Slave, Ec.ErrorCode);
    break;
   case EC_ERR_TYPE_PACKET_ERROR:
    sprintf(estring, "%s PACKET slave:%d index:%4.4x.%2.2x error:%d\n",
     timestr, Ec.Slave, Ec.Index, Ec.SubIdx, Ec.ErrorCode);
    break;
   case EC_ERR_TYPE_SDOINFO_ERROR:
    sprintf(estring, "%s SDO slave:%d index:%4.4x.%2.2x error:%8.8x %s\n",
     timestr, Ec.Slave, Ec.Index, Ec.SubIdx, Ec.AbortCode, ec_sdoerror2string(Ec.AbortCode));
    break;
   case EC_ERR_TYPE_SOE_ERROR:
    sprintf(estring, "%s SoE slave:%d IDN:%4.4x error:%4.4x %s\n",
     timestr, Ec.Slave, Ec.Index, Ec.AbortCode, ec_soeerror2string(Ec.ErrorCode));
    break;
   default:
    sprintf(estring, "%s error:%8.8x\n",
     timestr, Ec.AbortCode);
  }
  return (char *) estring;
 }
 else return "";
}

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

variable_types:
timestr
char [20]

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 eeprom_read(int slave, int start, int length)
{
 int i, wkc, ainc = 4;
    uint16 estat, aiadr;
 uint32 b4;
 uint64 b8;
 uint8 eepctl;
 if((ec_slavecount >= slave) && (slave > 0) && ((start + length) <= 32768))
 {
  aiadr = 1 - slave;
  eepctl = 2;
  wkc = ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl , 500);
  eepctl = 0;
  wkc = ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl , 500);
  estat = 0x0000;
  aiadr = 1 - slave;
  wkc=ec_APRD(aiadr, ECT_REG_EEPSTAT, sizeof(estat), &estat, 500);
  estat = (estat);
  if (estat & 0x0040)
  {
   ainc = 8;
   for (i = start ; i < (start + length) ; i+=ainc)
   {
    b8 = ec_readeepromAP(aiadr, i >> 1 , 20000);
    ebuf[i] = b8;
    ebuf[i+1] = b8 >> 8;
    ebuf[i+2] = b8 >> 16;
    ebuf[i+3] = b8 >> 24;
    ebuf[i+4] = b8 >> 32;
    ebuf[i+5] = b8 >> 40;
    ebuf[i+6] = b8 >> 48;
    ebuf[i+7] = b8 >> 56;
   }
  }
  else
  {
   for (i = start ; i < (start + length) ; i+=ainc)
   {
    b4 = ec_readeepromAP(aiadr, i >> 1 , 20000);
    ebuf[i] = b4;
    ebuf[i+1] = b4 >> 8;
    ebuf[i+2] = b4 >> 16;
    ebuf[i+3] = b4 >> 24;
   }
  }
  return 1;
 }
 return 0;
}

canonical_original_code:
int eeprom_read(int slave, int start, int length)
{
 int i; int wkc; int ainc = 4;
    unsigned short estat; unsigned short aiadr;
 unsigned int b4;
 unsigned long long b8;
 unsigned char eepctl;
 if((ec_slavecount >= slave) && (slave > 0) && ((start + length) <= 32768))
 {
  aiadr = 1 - slave;
  eepctl = 2;
  wkc = ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl , 500);
  eepctl = 0;
  wkc = ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl , 500);
  estat = 0x0000;
  aiadr = 1 - slave;
  wkc=ec_APRD(aiadr, ECT_REG_EEPSTAT, sizeof(estat), &estat, 500);
  estat = (estat);
  if (estat & 0x0040)
  {
   ainc = 8;
   for (i = start ; i < (start + length) ; i+=ainc)
   {
    b8 = ec_readeepromAP(aiadr, i >> 1 , 20000);
    ebuf[i] = b8;
    ebuf[i+1] = b8 >> 8;
    ebuf[i+2] = b8 >> 16;
    ebuf[i+3] = b8 >> 24;
    ebuf[i+4] = b8 >> 32;
    ebuf[i+5] = b8 >> 40;
    ebuf[i+6] = b8 >> 48;
    ebuf[i+7] = b8 >> 56;
   }
  }
  else
  {
   for (i = start ; i < (start + length) ; i+=ainc)
   {
    b4 = ec_readeepromAP(aiadr, i >> 1 , 20000);
    ebuf[i] = b4;
    ebuf[i+1] = b4 >> 8;
    ebuf[i+2] = b4 >> 16;
    ebuf[i+3] = b4 >> 24;
   }
  }
  return 1;
 }
 return 0;
}

variable_types: slave int

variable_types: start int

variable_types: length int

variable_types: wkc int

variable_types: ainc int

variable_types:
estat
unsigned short

variable_types:
aiadr
unsigned short

variable_types:
eepctl
unsigned char


decompiled_code:
__int64 __fastcall eeprom_write(int a1, int a2, int a3)
{
int v3; // ebx
__int64 result; // rax
int v5; // ebp
int v6; // er12
char v7[41]; // [rsp+1h] [rbp-29h] BYREF

if ( ec_slavecount < a1 )
return 0LL;
v3 = a2;
result = 0LL;
if ( a1 > 0 )
{
v5 = a2 + a3;
result = 0LL;
if ( a2 + a3 <= 0x8000 )
{
v7[0] = 2;
ec_APWR((unsigned __int16)(1 - a1), 1280LL, 1LL, v7, 500LL);
v7[0] = 0;
ec_APWR((unsigned __int16)(1 - a1), 1280LL, 1LL, v7, 500LL);
result = 1LL;
if ( v5 > a2 )
{
ec_writeeepromAP(
(unsigned __int16)(1 - a1),
(unsigned __int16)(a2 >> 1),
(unsigned __int16)ebuf[a2 >> 1],
20000LL);
v6 = 1;
while ( 1 )
{
v3 += 2;
if ( v5 <= v3 )
break;
ec_writeeepromAP(
(unsigned __int16)(1 - a1),
(unsigned __int16)(v3 >> 1),
(unsigned __int16)ebuf[v3 >> 1],
20000LL);
if ( ++v6 > 99 )
{
putchar(46);
fflush(stdout);
v6 = 0;
}
}
result = 1LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int a1, int a2, int a3)
{
int v3; // ebx
__int64 result; // rax
int v5; // ebp
int v6; // er12
char v7[41]; // [rsp+1h] [rbp-29h] BYREF

if ( ec_slavecount < a1 )
return 0LL;
v3 = a2;
result = 0LL;
if ( a1 > 0 )
{
v5 = a2 + a3;
result = 0LL;
if ( a2 + a3 <= 0x8000 )
{
v7[0] = 2;
ec_APWR((unsigned __int16)(1 - a1), 1280LL, 1LL, v7, 500LL);
v7[0] = 0;
ec_APWR((unsigned __int16)(1 - a1), 1280LL, 1LL, v7, 500LL);
result = 1LL;
if ( v5 > a2 )
{
ec_writeeepromAP(
(unsigned __int16)(1 - a1),
(unsigned __int16)(a2 >> 1),
(unsigned __int16)ebuf[a2 >> 1],
20000LL);
v6 = 1;
while ( 1 )
{
v3 += 2;
if ( v5 <= v3 )
break;
ec_writeeepromAP(
(unsigned __int16)(1 - a1),
(unsigned __int16)(v3 >> 1),
(unsigned __int16)ebuf[v3 >> 1],
20000LL);
if ( ++v6 > 99 )
{
putchar(46);
fflush(stdout);
v6 = 0;
}
}
result = 1LL;
}
}
}
return result;
}

original_code:
int eeprom_write(int slave, int start, int length)
{
 int i, wkc, dc = 0;
    uint16 aiadr, *wbuf;
 uint8 eepctl;
 int ret;
 if((ec_slavecount >= slave) && (slave > 0) && ((start + length) <= 32768))
 {
  aiadr = 1 - slave;
  eepctl = 2;
  wkc = ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl , 500);
  eepctl = 0;
  wkc = ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl , 500);
  aiadr = 1 - slave;
  wbuf = (uint16 *)&ebuf[0];
  for (i = start ; i < (start + length) ; i+=2)
  {
   ret = ec_writeeepromAP(aiadr, i >> 1 , *(wbuf + (i >> 1)), 20000);
   if (++dc >= 100)
   {
    dc = 0;
    printf(".");
    fflush(stdout);
   }
  }
  return 1;
 }
 return 0;
}

canonical_original_code:
int eeprom_write(int slave, int start, int length)
{
 int i; int wkc; int dc = 0;
    unsigned short aiadr; unsigned short *wbuf;
 unsigned char eepctl;
 int ret;
 if((ec_slavecount >= slave) && (slave > 0) && ((start + length) <= 32768))
 {
  aiadr = 1 - slave;
  eepctl = 2;
  wkc = ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl , 500);
  eepctl = 0;
  wkc = ec_APWR(aiadr, ECT_REG_EEPCFG, sizeof(eepctl), &eepctl , 500);
  aiadr = 1 - slave;
  wbuf = (unsigned short *)&ebuf[0];
  for (i = start ; i < (start + length) ; i+=2)
  {
   ret = ec_writeeepromAP(aiadr, i >> 1 , *(wbuf + (i >> 1)), 20000);
   if (++dc >= 100)
   {
    dc = 0;
    printf(".");
    fflush(stdout);
   }
  }
  return 1;
 }
 return 0;
}

variable_types: dc int

variable_types:
wbuf
unsigned short *

variable_types: ret int


eepromtool

decompiled_code:
int __fastcall eepromtool(const char *a1, int a2, int a3, const char *a4)
{
int v7; // eax
int v8; // eax
__int64 v9; // rsi
__int64 v10; // rdx
__time_t v11; // rdx
__suseconds_t v12; // rax
__time_t v13; // rdx
__suseconds_t v14; // 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);
v18[0] = 0;
v7 = ec_BRD(0LL, 0LL, 4LL, v18, 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]);
v9 = (unsigned __int16)ebuf[62];
v10 = (unsigned int)(((_DWORD)v9 + 1) << 7);
if ( (int)v9 + 1 > 256 )
v10 = 0x8000LL;
v16 = v10;
printf(" Size : %4.4X = %d bytes\n", v9, v10);
printf(" Version : %4.4X\n", (unsigned __int16)ebuf[63]);
if ( v16 > 128 )
eeprom_read(a2, 128, v16 - 128);
gettimeofday(&tend, 0LL);
v11 = tend.tv_sec - tstart.tv_sec;
tdif = tend.tv_sec - tstart.tv_sec;
v12 = tend.tv_usec - tstart.tv_usec;
*(&tdif + 1) = v12;
if ( v12 < 0 )
{
tdif = v11 - 1;
*(&tdif + 1) = v12 + 1000000;
}
if ( a3 == 2 )
output_intelhex(a4, v16);
else
output_bin(a4, v16);
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, &v16);
else
v8 = input_bin(a4, &v16);
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, v16);
gettimeofday(&tend, 0LL);
v13 = tend.tv_sec - tstart.tv_sec;
tdif = tend.tv_sec - tstart.tv_sec;
v14 = tend.tv_usec - tstart.tv_usec;
*(&tdif + 1) = v14;
if ( v14 < 0 )
{
tdif = v13 - 1;
*(&tdif + 1) = v14 + 1000000;
}
printf("\nTotal EEPROM write time :%ldms\n", (*(&tdif + 1) + 1000000 * tdif) / 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
__int64 v9; // rsi
__int64 v10; // rdx
__time_t v11; // rdx
__suseconds_t v12; // rax
__time_t v13; // rdx
__suseconds_t v14; // 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);
v18[0] = 0;
v7 = ec_BRD(0LL, 0LL, 4LL, v18, 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]);
v9 = (unsigned __int16)ebuf[62];
v10 = (unsigned int)(((_DWORD)v9 + 1) << 7);
if ( (int)v9 + 1 > 256 )
v10 = 0x8000LL;
v16 = v10;
printf(" Size : %4.4X = %d bytes\n", v9, v10);
printf(" Version : %4.4X\n", (unsigned __int16)ebuf[63]);
if ( v16 > 128 )
func4(a2, 128, v16 - 128);
gettimeofday(&tend, 0LL);
v11 = tend.tv_sec - tstart.tv_sec;
tdif = tend.tv_sec - tstart.tv_sec;
v12 = tend.tv_usec - tstart.tv_usec;
*(&tdif + 1) = v12;
if ( v12 < 0 )
{
tdif = v11 - 1;
*(&tdif + 1) = v12 + 1000000;
}
if ( a3 == 2 )
func3(a4, v16);
else
func2(a4, v16);
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, &v16);
else
v8 = func0(a4, &v16);
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, v16);
gettimeofday(&tend, 0LL);
v13 = tend.tv_sec - tstart.tv_sec;
tdif = tend.tv_sec - tstart.tv_sec;
v14 = tend.tv_usec - tstart.tv_usec;
*(&tdif + 1) = v14;
if ( v14 < 0 )
{
tdif = v13 - 1;
*(&tdif + 1) = v14 + 1000000;
}
printf("\nTotal EEPROM write time :%ldms\n", (*(&tdif + 1) + 1000000 * tdif) / 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; // er8
const char *v4; // rax
int v5; // edx

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]);
slave = v3;
mode = 0;
v4 = argv[3];
v5 = strcmp(v4, "-r") == 0;
mode = v5;
if ( !strcmp(v4, "-ri") )
v5 = 2;
mode = v5;
if ( !strcmp(v4, "-w") )
v5 = 3;
mode = v5;
if ( !strcmp(v4, "-wi") )
v5 = 4;
mode = v5;
eepromtool(argv[1], v3, v5, argv[4]);
}
puts("End program");
return 0;
}

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

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]);
slave = v3;
mode = 0;
v4 = argv[3];
v5 = strcmp(v4, "-r") == 0;
mode = v5;
if ( !strcmp(v4, "-ri") )
v5 = 2;
mode = v5;
if ( !strcmp(v4, "-w") )
v5 = 3;
mode = v5;
if ( !strcmp(v4, "-wi") )
v5 = 4;
mode = v5;
func6(argv[1], v3, v5, 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 *[]