decompiled_code:
void __fastcall __noreturn simple_server(unsigned int a1, unsigned int a2)
{
int v2; // eax
unsigned int v3; // er15
unsigned __int64 v4; // rax
int v5; // eax
unsigned int v6; // ebx
int v7; // eax
__int64 v8; // rbp
__int64 v9; // r12
double v10; // xmm0_8
__int64 v11; // rdi
int v12; // er14
int *v13; // r13
unsigned int v14; // ebx
unsigned int v15; // ebp
double v16; // xmm0_8
unsigned int v17; // er14
double v18; // r13
unsigned int v19; // eax
double v20; // [rsp+8h] [rbp-210h]
unsigned int v21; // [rsp+8h] [rbp-210h]
double v22; // [rsp+10h] [rbp-208h]
unsigned int v23; // [rsp+18h] [rbp-200h]
unsigned int v24; // [rsp+1Ch] [rbp-1FCh]
int v25; // [rsp+24h] [rbp-1F4h]
int *v26; // [rsp+28h] [rbp-1F0h]
char v27[128]; // [rsp+30h] [rbp-1E8h] BYREF
char v28[128]; // [rsp+B0h] [rbp-168h] BYREF
char v29[128]; // [rsp+130h] [rbp-E8h] BYREF
struct timeval tv; // [rsp+1B0h] [rbp-68h] BYREF
__int64 v31; // [rsp+1C0h] [rbp-58h] BYREF
__int64 v32; // [rsp+1C8h] [rbp-50h]
__m128i v33[4]; // [rsp+1D0h] [rbp-48h] BYREF

tv.tv_sec = 0LL;
tv.tv_usec = 0LL;
memset(v29, 0, sizeof(v29));
memset(v28, 0, sizeof(v28));
memset(v27, 0, sizeof(v27));
v2 = create_listeners(v29, a1, a2);
if ( v2 >= 0 )
{
v3 = v2;
v25 = v2 + 1;
v26 = (int *)((char *)&state + 160 * v2 + 208);
while ( 1 )
{
LODWORD(v9) = 0;
v8 = 0LL;
v6 = 0;
do
{
v5 = accept_incoming(v3, v29, v28);
if ( v5 > 0 )
v6 += v5;
v7 = send_data(v28, v27);
if ( v7 > 0 )
v8 = (unsigned int)(v8 + v7);
v9 = (unsigned int)(v9 + 1);
gettimeofday(&tv, 0LL);
tvsub(&v31, &tv, v33);
v10 = (double)(int)v31 * 1000000.0 + (double)(int)v32;
if ( v10 >= 9.223372036854776e18 )
v4 = (unsigned int)(int)(v10 - 9.223372036854776e18) ^ 0x8000000000000000LL;
else
v4 = (unsigned int)(int)v10;
if ( (v4 & 0x8000000000000000LL) != 0LL )
v20 = (double)(int)(v4 & 1 | (v4 >> 1)) + (double)(int)(v4 & 1 | (v4 >> 1));
else
v20 = (double)(int)v4;
}
while ( v20 <= 5000000.0 );
fwrite("\nBandwidth:\n", 1uLL, 0xCuLL, stderr);
v11 = maxfd;
v12 = v25;
if ( v25 > maxfd )
{
v22 = 0.0;
v17 = 0;
v21 = 0;
}
else
{
v13 = v26;
v22 = 0.0;
v23 = v6;
v24 = v8;
v14 = 0;
v15 = 0;
do
{
v14 -= (*v13 == 0) - 1;
v15 += *v13;
v16 = (double)*v13 * 8.0 / v20;
v22 = v22 + v16;
if ( *(v13 - 4) )
{
fprintf(stderr, "%c%.4f ", 43LL, v16);
}
else if ( verbose )
{
fprintf(stderr, "%c%.4f ", 45LL, v16);
}
*v13 = 0;
++v12;
v11 = maxfd;
v13 += 40;
}
while ( maxfd >= v12 );
v9 = (unsigned int)v9;
v17 = v14;
v6 = v23;
v21 = v15;
v8 = v24;
}
v18 = v22 / scalar;
v19 = FD_POP(v11, v28);
fprintf(
stderr,
"\n"
"\t %d streams active, %d made progress: tot = %d, tot Mb/s = %.2f\n"
"\t opened %d, closed %d descriptors (loop count %d)\n"
"\n",
v19,
v17,
v21,
v18,
v6,
v8,
v9);
v33[0] = _mm_loadu_si128((const __m128i *)&tv);
}
}
exit(-1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(unsigned int a1, unsigned int a2)
{
int v2; // eax
unsigned int v3; // er15
unsigned __int64 v4; // rax
int v5; // eax
unsigned int v6; // ebx
int v7; // eax
__int64 v8; // rbp
__int64 v9; // r12
double v10; // xmm0_8
__int64 v11; // rdi
int v12; // er14
int *v13; // r13
unsigned int v14; // ebx
unsigned int v15; // ebp
double v16; // xmm0_8
unsigned int v17; // er14
double v18; // r13
unsigned int v19; // eax
double v20; // [rsp+8h] [rbp-210h]
unsigned int v21; // [rsp+8h] [rbp-210h]
double v22; // [rsp+10h] [rbp-208h]
unsigned int v23; // [rsp+18h] [rbp-200h]
unsigned int v24; // [rsp+1Ch] [rbp-1FCh]
int v25; // [rsp+24h] [rbp-1F4h]
int *v26; // [rsp+28h] [rbp-1F0h]
char v27[128]; // [rsp+30h] [rbp-1E8h] BYREF
char v28[128]; // [rsp+B0h] [rbp-168h] BYREF
char v29[128]; // [rsp+130h] [rbp-E8h] BYREF
struct timeval tv; // [rsp+1B0h] [rbp-68h] BYREF
__int64 v31; // [rsp+1C0h] [rbp-58h] BYREF
__int64 v32; // [rsp+1C8h] [rbp-50h]
__m128i v33[4]; // [rsp+1D0h] [rbp-48h] BYREF

tv.tv_sec = 0LL;
tv.tv_usec = 0LL;
memset(v29, 0, sizeof(v29));
memset(v28, 0, sizeof(v28));
memset(v27, 0, sizeof(v27));
v2 = create_listeners(v29, a1, a2);
if ( v2 >= 0 )
{
v3 = v2;
v25 = v2 + 1;
v26 = (int *)((char *)&state + 160 * v2 + 208);
while ( 1 )
{
LODWORD(v9) = 0;
v8 = 0LL;
v6 = 0;
do
{
v5 = accept_incoming(v3, v29, v28);
if ( v5 > 0 )
v6 += v5;
v7 = send_data(v28, v27);
if ( v7 > 0 )
v8 = (unsigned int)(v8 + v7);
v9 = (unsigned int)(v9 + 1);
gettimeofday(&tv, 0LL);
tvsub(&v31, &tv, v33);
v10 = (double)(int)v31 * 1000000.0 + (double)(int)v32;
if ( v10 >= 9.223372036854776e18 )
v4 = (unsigned int)(int)(v10 - 9.223372036854776e18) ^ 0x8000000000000000LL;
else
v4 = (unsigned int)(int)v10;
if ( (v4 & 0x8000000000000000LL) != 0LL )
v20 = (double)(int)(v4 & 1 | (v4 >> 1)) + (double)(int)(v4 & 1 | (v4 >> 1));
else
v20 = (double)(int)v4;
}
while ( v20 <= 5000000.0 );
fwrite("\nBandwidth:\n", 1uLL, 0xCuLL, stderr);
v11 = maxfd;
v12 = v25;
if ( v25 > maxfd )
{
v22 = 0.0;
v17 = 0;
v21 = 0;
}
else
{
v13 = v26;
v22 = 0.0;
v23 = v6;
v24 = v8;
v14 = 0;
v15 = 0;
do
{
v14 -= (*v13 == 0) - 1;
v15 += *v13;
v16 = (double)*v13 * 8.0 / v20;
v22 = v22 + v16;
if ( *(v13 - 4) )
{
fprintf(stderr, "%c%.4f ", 43LL, v16);
}
else if ( verbose )
{
fprintf(stderr, "%c%.4f ", 45LL, v16);
}
*v13 = 0;
++v12;
v11 = maxfd;
v13 += 40;
}
while ( maxfd >= v12 );
v9 = (unsigned int)v9;
v17 = v14;
v6 = v23;
v21 = v15;
v8 = v24;
}
v18 = v22 / scalar;
v19 = FD_POP(v11, v28);
fprintf(
stderr,
"\n"
"\t %d streams active, %d made progress: tot = %d, tot Mb/s = %.2f\n"
"\t opened %d, closed %d descriptors (loop count %d)\n"
"\n",
v19,
v17,
v21,
v18,
v6,
v8,
v9);
v33[0] = _mm_loadu_si128((const __m128i *)&tv);
}
}
exit(-1);
}

original_code:
void
simple_server(int num_ports, int base_rx_port)
{
    int datafd, maxlfd;
    unsigned long diffus;
    struct timeval last, diff, now ={ 0L, 0L };
    int opened=0, closed=0, cnt=0;
    fd_set fds_listeners, fds_active, fds_finished;
    int rc;
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds_listeners)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds_active)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds_finished)->__fds_bits)[0]) : "memory"); } while (0);
    if((maxlfd = create_listeners(&fds_listeners, num_ports, base_rx_port)) < 0)
        exit(-1);
    datafd = maxlfd+1;
    while(1)
    {
        rc = accept_incoming(maxlfd, &fds_listeners, &fds_active);
        if(rc > 0) opened += rc;
        rc = send_data(&fds_active, &fds_finished);
        if(rc > 0) closed += rc;
        cnt++;
        gettimeofday(&now, (struct timezone *)0);
        tvsub(&diff, &now, &last);
        diffus = diff.tv_sec*1e6 + diff.tv_usec;
        if(diffus > 5e6)
        {
            int i, totb=0, prog=0;
            double mbs, tmbs=0.0;
            fprintf(stderr, "\nBandwidth:\n");
            for(i=datafd; i <= maxfd; i++)
            {
                if(state[i].tx_sent_cpt) prog++;
                totb += state[i].tx_sent_cpt;
                mbs = (double)(8.0*state[i].tx_sent_cpt) / (double)diffus;
                tmbs += mbs;
                if(state[i].open) fprintf(stderr,"%c%.4f ",'+', mbs);
                else
                    if(verbose) fprintf(stderr,"%c%.4f ",'-', mbs);
                state[i].tx_sent_cpt = 0;
            }
            fprintf(stderr,
                    "\n\t %d streams active, %d made progress: "
                    "tot = %d, tot Mb/s = %.2f\n"
                    "\t opened %d, closed %d descriptors (loop count %d)\n\n",
                    FD_POP(maxfd, &fds_active), prog,
                    totb, tmbs/scalar,
                    opened, closed, cnt);
            opened = closed = cnt = 0;
            last = now;
        }
    }
}

canonical_original_code:
void simple_server(int num_ports, int base_rx_port)
{
    int datafd; int maxlfd;
    unsigned long diffus;
    struct timeval last; struct timeval diff; struct timeval now = { 0L, 0L };
    int opened = 0; int closed = 0; int cnt = 0;
    struct fd_set fds_listeners; struct fd_set fds_active; struct fd_set fds_finished;
    int rc;
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds_listeners)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds_active)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds_finished)->__fds_bits)[0]) : "memory"); } while (0);
    if((maxlfd = create_listeners(&fds_listeners, num_ports, base_rx_port)) < 0)
        exit(-1);
    datafd = maxlfd+1;
    while(1)
    {
        rc = accept_incoming(maxlfd, &fds_listeners, &fds_active);
        if(rc > 0) opened += rc;
        rc = send_data(&fds_active, &fds_finished);
        if(rc > 0) closed += rc;
        cnt++;
        gettimeofday(&now, (struct timezone *)0);
        tvsub(&diff, &now, &last);
        diffus = diff.tv_sec*1e6 + diff.tv_usec;
        if(diffus > 5e6)
        {
            int i; int totb = 0; int prog = 0;
            double mbs; double tmbs = 0.0;
            fprintf(stderr, "\nBandwidth:\n");
            for(i=datafd; i <= maxfd; i++)
            {
                if(state[i].tx_sent_cpt) prog++;
                totb += state[i].tx_sent_cpt;
                mbs = (double)(8.0*state[i].tx_sent_cpt) / (double)diffus;
                tmbs += mbs;
                if(state[i].open) fprintf(stderr,"%c%.4f ",'+', mbs);
                else
                    if(verbose) fprintf(stderr,"%c%.4f ",'-', mbs);
                state[i].tx_sent_cpt = 0;
            }
            fprintf(stderr,
                    "\n\t %d streams active, %d made progress: "
                    "tot = %d, tot Mb/s = %.2f\n"
                    "\t opened %d, closed %d descriptors (loop count %d)\n\n",
                    FD_POP(maxfd, &fds_active), prog,
                    totb, tmbs/scalar,
                    opened, closed, cnt);
            opened = closed = cnt = 0;
            last = now;
        }
    }
}

variable_types: datafd int

variable_types: maxlfd int

variable_types:
diffus
unsigned long

variable_types: opened int

variable_types: closed int

variable_types: cnt int

variable_types: totb int

variable_types: prog int

user_defined_types: struct timezone { int tz_minuteswest; int tz_dsttime; }


decompiled_code:
int __fastcall simple_tx(int a1, unsigned int a2, __int64 a3, int a4, int a5)
{
int i; // ebp
__int64 v7; // rdx
int v8; // ebx
unsigned int v9; // er13
int v10; // ebp
__int64 v11; // rcx
__int64 j; // rdi
signed int v13; // ebx
double v14; // rbp
unsigned int v15; // eax
double v17; // [rsp+8h] [rbp-160h]
__int64 v18; // [rsp+10h] [rbp-158h]
__int128 v21; // [rsp+20h] [rbp-148h] BYREF
__int64 v22[16]; // [rsp+30h] [rbp-138h] BYREF
__int64 v23[23]; // [rsp+B0h] [rbp-B8h] BYREF

v18 = host2ipaddr(a3);
memset(v23, 0, 0x80uLL);
memset(v22, 0, sizeof(v22));
if ( a1 > 0 )
{
for ( i = 0; i != a1; ++i )
{
v8 = create_tx_tcp(v18, (unsigned int)(i % a4 + a5));
if ( v8 < 0 )
fwrite("Unable to open connection!\n", 1uLL, 0x1BuLL, stderr);
if ( maxfd < v8 )
maxfd = v8;
state[40 * v8 + 9] = a2;
state[40 * v8 + 11] = 0;
v7 = 40LL * v8;
state[v7 + 10] = 0;
gettimeofday((struct timeval *)&state[v7 + 14], 0LL);
v23[v8 / 64] |= 1LL << (v8 % 64);
}
}
v17 = 0.0;
v9 = -1;
do
{
v15 = send_data(v23, v22);
if ( v15 )
fprintf(stderr, "%d finished\n", v15);
for ( j = 0LL; ; j = (unsigned int)(v13 + 1) )
{
v13 = FD_FFSandC(j, maxfd, v22);
if ( !v13 )
break;
if ( state[40 * v13 + 11] == state[40 * v13 + 9] )
{
v9 = FD_POP(maxfd, v23);
tvsub(&v21, &state[40 * v13 + 18], &state[40 * v13 + 14]);
fprintf(stderr, "Finished with %d after %d bytes. ", (unsigned int)v13, state[40 * v13 + 11]);
v14 = *(double *)&LC0;
if ( v21 != 0 )
{
v14 = (double)(int)state[40 * v13 + 11] * 8.0 / ((double)(int)v21 * 1000000.0 + (double)SDWORD2(v21));
fprintf(
stderr,
"%ld.%03lds => %.4f Mb/s. %d still active\n",
(_QWORD)v21,
*((_QWORD *)&v21 + 1) / 1000LL,
v14 / scalar,
v9);
}
v17 = v14 + v17;
v22[v13 / 64] &= __ROL8__(-2LL, v13 % 64);
}
else
{
v10 = create_tx_tcp(v18, (unsigned int)(v13 % a4 + a5));
if ( v10 < 0 )
fwrite("Unable to open connection!\n", 1uLL, 0x1BuLL, stderr);
fprintf(stderr, "Connection %d was aborted, so restart using fd %d\n", (unsigned int)v13, (unsigned int)v10);
if ( maxfd < v10 )
maxfd = v10;
state[40 * v10 + 9] = a2;
v11 = 40LL * v10;
state[v11 + 11] = 0;
state[v11 + 10] = 0;
gettimeofday((struct timeval *)&state[40 * v13 + 14], 0LL);
v23[v10 / 64] |= 1LL << (v10 % 64);
}
}
}
while ( v9 );
return fprintf(stderr, "Total b/w estimate was %.2f Mb/s, Average stream b/w was %.4f Mb/s\n", v17, v17 / (double)a1);
}

canonical_decompiled_code:
int __fastcall func0(int a1, unsigned int a2, __int64 a3, int a4, int a5)
{
int i; // ebp
__int64 v7; // rdx
int v8; // ebx
unsigned int v9; // er13
int v10; // ebp
__int64 v11; // rcx
__int64 j; // rdi
signed int v13; // ebx
double v14; // rbp
unsigned int v15; // eax
double v17; // [rsp+8h] [rbp-160h]
__int64 v18; // [rsp+10h] [rbp-158h]
__int128 v21; // [rsp+20h] [rbp-148h] BYREF
__int64 v22[16]; // [rsp+30h] [rbp-138h] BYREF
__int64 v23[23]; // [rsp+B0h] [rbp-B8h] BYREF

v18 = host2ipaddr(a3);
memset(v23, 0, 0x80uLL);
memset(v22, 0, sizeof(v22));
if ( a1 > 0 )
{
for ( i = 0; i != a1; ++i )
{
v8 = create_tx_tcp(v18, (unsigned int)(i % a4 + a5));
if ( v8 < 0 )
fwrite("Unable to open connection!\n", 1uLL, 0x1BuLL, stderr);
if ( maxfd < v8 )
maxfd = v8;
state[40 * v8 + 9] = a2;
state[40 * v8 + 11] = 0;
v7 = 40LL * v8;
state[v7 + 10] = 0;
gettimeofday((struct timeval *)&state[v7 + 14], 0LL);
v23[v8 / 64] |= 1LL << (v8 % 64);
}
}
v17 = 0.0;
v9 = -1;
do
{
v15 = send_data(v23, v22);
if ( v15 )
fprintf(stderr, "%d finished\n", v15);
for ( j = 0LL; ; j = (unsigned int)(v13 + 1) )
{
v13 = FD_FFSandC(j, maxfd, v22);
if ( !v13 )
break;
if ( state[40 * v13 + 11] == state[40 * v13 + 9] )
{
v9 = FD_POP(maxfd, v23);
tvsub(&v21, &state[40 * v13 + 18], &state[40 * v13 + 14]);
fprintf(stderr, "Finished with %d after %d bytes. ", (unsigned int)v13, state[40 * v13 + 11]);
v14 = *(double *)&LC0;
if ( v21 != 0 )
{
v14 = (double)(int)state[40 * v13 + 11] * 8.0 / ((double)(int)v21 * 1000000.0 + (double)SDWORD2(v21));
fprintf(
stderr,
"%ld.%03lds => %.4f Mb/s. %d still active\n",
(_QWORD)v21,
*((_QWORD *)&v21 + 1) / 1000LL,
v14 / scalar,
v9);
}
v17 = v14 + v17;
v22[v13 / 64] &= __ROL8__(-2LL, v13 % 64);
}
else
{
v10 = create_tx_tcp(v18, (unsigned int)(v13 % a4 + a5));
if ( v10 < 0 )
fwrite("Unable to open connection!\n", 1uLL, 0x1BuLL, stderr);
fprintf(stderr, "Connection %d was aborted, so restart using fd %d\n", (unsigned int)v13, (unsigned int)v10);
if ( maxfd < v10 )
maxfd = v10;
state[40 * v10 + 9] = a2;
v11 = 40LL * v10;
state[v11 + 11] = 0;
state[v11 + 10] = 0;
gettimeofday((struct timeval *)&state[40 * v13 + 14], 0LL);
v23[v10 / 64] |= 1LL << (v10 % 64);
}
}
}
while ( v9 );
return fprintf(stderr, "Total b/w estimate was %.2f Mb/s, Average stream b/w was %.4f Mb/s\n", v17, v17 / (double)a1);
}

original_code:
void
simple_tx(int n, int bytes, char *host, int num_ports, int base_rx_port)
{
    int i, left;
    fd_set fds_active, fds_finished;
    unsigned long ipaddr;
    double tmbs = 0;
    ;
    ipaddr = host2ipaddr(host);
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds_active)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds_finished)->__fds_bits)[0]) : "memory"); } while (0);
    for(i=0; i<n; i++)
    {
        int fd = create_tx_tcp(ipaddr, base_rx_port+(i%num_ports));
        if(fd < 0) fprintf(stderr, "Unable to open connection!\n");
        if(fd > maxfd) maxfd = fd;
        state[fd].tx_target = bytes;
        state[fd].tx_sent = 0;
        state[fd].tx_pkts = 0;
        gettimeofday(&state[fd].tx_start, (struct timezone *)0);
        ((void) (((&fds_active)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
    }
    left = -1;
    while(left)
    {
        int s, fd, fin;
        struct timeval tdiff;
        fin = send_data(&fds_active, &fds_finished);
        if(fin) fprintf(stderr, "%d finished\n", fin);
        for(s=0; (fd = FD_FFSandC(s, maxfd, &fds_finished)); s = fd+1)
        {
            double mbs = 0;
            if(state[fd].tx_sent != state[fd].tx_target)
            {
                int newfd = create_tx_tcp(ipaddr, base_rx_port+(fd%num_ports));
                if(newfd < 0) fprintf(stderr, "Unable to open connection!\n");
                fprintf(stderr,
                        "Connection %d was aborted, so restart using fd %d\n",
                        fd, newfd);
                if(newfd > maxfd) maxfd = newfd;
                state[newfd].tx_target = bytes;
                state[newfd].tx_sent = 0;
                state[newfd].tx_pkts = 0;
                gettimeofday(&state[fd].tx_start, (struct timezone *)0);
                ((void) (((&fds_active)->__fds_bits)[((newfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((newfd) % (8 * (int) sizeof (__fd_mask)))))));
                continue;
            }
            left = FD_POP(maxfd, &fds_active);
            tvsub(&tdiff, &state[fd].tx_stop, &state[fd].tx_start);
            fprintf(stderr,
                    "Finished with %d after %d bytes. ",
                    fd, state[fd].tx_sent);
            if((tdiff.tv_sec == 0) && (tdiff.tv_usec == 0))
                mbs = 0;
            else
            {
                mbs = (double)(8.0*state[fd].tx_sent)
                      / (double)(tdiff.tv_sec*1e6 + tdiff.tv_usec);
                fprintf(stderr,
                        "%ld.%03lds => %.4f Mb/s. %d still active\n",
                        (long int)tdiff.tv_sec, (long int)tdiff.tv_usec/1000,
                        mbs/scalar, left);
            }
            tmbs += mbs;
            ((void) (((&fds_finished)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
        }
    }
    fprintf(stderr,
            "Total b/w estimate was %.2f Mb/s, "
            "Average stream b/w was %.4f Mb/s\n", tmbs, tmbs/n);
    return;
}

canonical_original_code:
void simple_tx(int n, int bytes, char *host, int num_ports, int base_rx_port)
{
    int i; int left;
    struct fd_set fds_active; struct fd_set fds_finished;
    unsigned long ipaddr;
    double tmbs = 0;
    ;
    ipaddr = host2ipaddr(host);
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds_active)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds_finished)->__fds_bits)[0]) : "memory"); } while (0);
    for(i=0; i<n; i++)
    {
        int fd = create_tx_tcp(ipaddr, base_rx_port+(i%num_ports));
        if(fd < 0) fprintf(stderr, "Unable to open connection!\n");
        if(fd > maxfd) maxfd = fd;
        state[fd].tx_target = bytes;
        state[fd].tx_sent = 0;
        state[fd].tx_pkts = 0;
        gettimeofday(&state[fd].tx_start, (struct timezone *)0);
        ((void) (((&fds_active)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
    }
    left = -1;
    while(left)
    {
        int s; int fd; int fin;
        struct timeval tdiff;
        fin = send_data(&fds_active, &fds_finished);
        if(fin) fprintf(stderr, "%d finished\n", fin);
        for(s=0; (fd = FD_FFSandC(s, maxfd, &fds_finished)); s = fd+1)
        {
            double mbs = 0;
            if(state[fd].tx_sent != state[fd].tx_target)
            {
                int newfd = create_tx_tcp(ipaddr, base_rx_port+(fd%num_ports));
                if(newfd < 0) fprintf(stderr, "Unable to open connection!\n");
                fprintf(stderr,
                        "Connection %d was aborted, so restart using fd %d\n",
                        fd, newfd);
                if(newfd > maxfd) maxfd = newfd;
                state[newfd].tx_target = bytes;
                state[newfd].tx_sent = 0;
                state[newfd].tx_pkts = 0;
                gettimeofday(&state[fd].tx_start, (struct timezone *)0);
                ((void) (((&fds_active)->__fds_bits)[((newfd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((newfd) % (8 * (int) sizeof (__fd_mask)))))));
                continue;
            }
            left = FD_POP(maxfd, &fds_active);
            tvsub(&tdiff, &state[fd].tx_stop, &state[fd].tx_start);
            fprintf(stderr,
                    "Finished with %d after %d bytes. ",
                    fd, state[fd].tx_sent);
            if((tdiff.tv_sec == 0) && (tdiff.tv_usec == 0))
                mbs = 0;
            else
            {
                mbs = (double)(8.0*state[fd].tx_sent)
                      / (double)(tdiff.tv_sec*1e6 + tdiff.tv_usec);
                fprintf(stderr,
                        "%ld.%03lds => %.4f Mb/s. %d still active\n",
                        (long)tdiff.tv_sec, (long)tdiff.tv_usec/1000,
                        mbs/scalar, left);
            }
            tmbs += mbs;
            ((void) (((&fds_finished)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
        }
    }
    fprintf(stderr,
            "Total b/w estimate was %.2f Mb/s, "
            "Average stream b/w was %.4f Mb/s\n", tmbs, tmbs/n);
    return;
}

variable_types: n int

variable_types: bytes int

variable_types:
host
char *

variable_types: i int

variable_types: left int

variable_types:
ipaddr
unsigned long

variable_types: tmbs double

variable_types: fd int

variable_types: s int

variable_types: fin int

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

variable_types: mbs double

variable_types: newfd int

return_type: void


decompiled_code:
void __fastcall __noreturn simple_rx(unsigned int a1, unsigned int a2)
{
int v2; // eax
unsigned int v3; // ebp
__int64 v4; // rdi
int v5; // eax
int v6; // ebx
double v7; // xmm0_8
unsigned __int64 v8; // rax
int v9; // er14
int v10; // eax
unsigned int v11; // er13
int v12; // er12
__int64 v13; // rdi
int v14; // er14
int *v15; // rbx
unsigned int v16; // er12
unsigned int v17; // ebp
double v18; // xmm0_8
unsigned int v19; // er14
double v20; // rbx
unsigned int v21; // eax
double v22; // [rsp+0h] [rbp-218h]
unsigned int v23; // [rsp+Ch] [rbp-20Ch]
double v24; // [rsp+10h] [rbp-208h]
unsigned int v25; // [rsp+18h] [rbp-200h]
unsigned int v26; // [rsp+20h] [rbp-1F8h]
int v27; // [rsp+24h] [rbp-1F4h]
int *v28; // [rsp+28h] [rbp-1F0h]
__int64 v29[16]; // [rsp+30h] [rbp-1E8h] BYREF
char v30[128]; // [rsp+B0h] [rbp-168h] BYREF
char v31[128]; // [rsp+130h] [rbp-E8h] BYREF
struct timeval tv; // [rsp+1B0h] [rbp-68h] BYREF
__int64 v33; // [rsp+1C0h] [rbp-58h] BYREF
__int64 v34; // [rsp+1C8h] [rbp-50h]
__m128i v35[4]; // [rsp+1D0h] [rbp-48h] BYREF

tv.tv_sec = 0LL;
tv.tv_usec = 0LL;
memset(v31, 0, sizeof(v31));
memset(v30, 0, sizeof(v30));
memset(v29, 0, sizeof(v29));
v2 = create_listeners(v31, a1, a2);
if ( v2 >= 0 )
{
v3 = v2;
v27 = v2 + 1;
v28 = &state[40 * v2 + 64];
while ( 1 )
{
v9 = 1;
v23 = 0;
v11 = 0;
do
{
v10 = accept_incoming(v3, v31, v30);
if ( v10 > 0 )
v11 += v10;
v12 = sink_data(v30, v29);
v4 = 0LL;
if ( v12 > 0 )
{
while ( 1 )
{
v5 = FD_FFSandC(v4, maxfd, v29);
v6 = v5;
if ( !v5 )
break;
close(v5);
state[40 * v6 + 8] = 0;
v29[v6 / 64] &= __ROL8__(-2LL, v6 % 64);
v4 = (unsigned int)(v6 + 1);
}
v23 += v12;
v25 = v9;
}
else
{
v25 = v9;
}
gettimeofday(&tv, 0LL);
tvsub(&v33, &tv, v35);
v7 = (double)(int)v33 * 1000000.0 + (double)(int)v34;
if ( v7 >= 9.223372036854776e18 )
v8 = (unsigned int)(int)(v7 - 9.223372036854776e18) ^ 0x8000000000000000LL;
else
v8 = (unsigned int)(int)v7;
if ( (v8 & 0x8000000000000000LL) != 0LL )
v22 = (double)(int)(v8 & 1 | (v8 >> 1)) + (double)(int)(v8 & 1 | (v8 >> 1));
else
v22 = (double)(int)v8;
++v9;
}
while ( v22 <= 5000000.0 );
fwrite("\nBandwidth:\n", 1uLL, 0xCuLL, stderr);
v13 = maxfd;
v14 = v27;
if ( v27 > maxfd )
{
v24 = 0.0;
v16 = 0;
v19 = 0;
}
else
{
v15 = v28;
v24 = 0.0;
v16 = 0;
v26 = v3;
v17 = 0;
do
{
v16 -= (*v15 == 0) - 1;
v17 += *v15;
v18 = (double)*v15 * 8.0 / v22;
v24 = v24 + v18;
if ( *(v15 - 16) )
{
fprintf(stderr, "%c%.4f ", 43LL, v18);
}
else if ( verbose )
{
fprintf(stderr, "%c%.4f ", 45LL, v18);
}
*v15 = 0;
++v14;
v13 = maxfd;
v15 += 40;
}
while ( maxfd >= v14 );
v19 = v17;
v3 = v26;
}
v20 = v24 / scalar;
v21 = FD_POP(v13, v30);
fprintf(
stderr,
"\n"
"\t %d streams active, %d made progress: tot = %d, tot Mb/s = %.2f\n"
"\t opened %d, closed %d descriptors (loop count %d)\n"
"\n",
v21,
v16,
v19,
v20,
v11,
v23,
v25);
v35[0] = _mm_loadu_si128((const __m128i *)&tv);
}
}
exit(-1);
}

canonical_decompiled_code:
void __fastcall __noreturn func0(unsigned int a1, unsigned int a2)
{
int v2; // eax
unsigned int v3; // ebp
__int64 v4; // rdi
int v5; // eax
int v6; // ebx
double v7; // xmm0_8
unsigned __int64 v8; // rax
int v9; // er14
int v10; // eax
unsigned int v11; // er13
int v12; // er12
__int64 v13; // rdi
int v14; // er14
int *v15; // rbx
unsigned int v16; // er12
unsigned int v17; // ebp
double v18; // xmm0_8
unsigned int v19; // er14
double v20; // rbx
unsigned int v21; // eax
double v22; // [rsp+0h] [rbp-218h]
unsigned int v23; // [rsp+Ch] [rbp-20Ch]
double v24; // [rsp+10h] [rbp-208h]
unsigned int v25; // [rsp+18h] [rbp-200h]
unsigned int v26; // [rsp+20h] [rbp-1F8h]
int v27; // [rsp+24h] [rbp-1F4h]
int *v28; // [rsp+28h] [rbp-1F0h]
__int64 v29[16]; // [rsp+30h] [rbp-1E8h] BYREF
char v30[128]; // [rsp+B0h] [rbp-168h] BYREF
char v31[128]; // [rsp+130h] [rbp-E8h] BYREF
struct timeval tv; // [rsp+1B0h] [rbp-68h] BYREF
__int64 v33; // [rsp+1C0h] [rbp-58h] BYREF
__int64 v34; // [rsp+1C8h] [rbp-50h]
__m128i v35[4]; // [rsp+1D0h] [rbp-48h] BYREF

tv.tv_sec = 0LL;
tv.tv_usec = 0LL;
memset(v31, 0, sizeof(v31));
memset(v30, 0, sizeof(v30));
memset(v29, 0, sizeof(v29));
v2 = create_listeners(v31, a1, a2);
if ( v2 >= 0 )
{
v3 = v2;
v27 = v2 + 1;
v28 = &state[40 * v2 + 64];
while ( 1 )
{
v9 = 1;
v23 = 0;
v11 = 0;
do
{
v10 = accept_incoming(v3, v31, v30);
if ( v10 > 0 )
v11 += v10;
v12 = sink_data(v30, v29);
v4 = 0LL;
if ( v12 > 0 )
{
while ( 1 )
{
v5 = FD_FFSandC(v4, maxfd, v29);
v6 = v5;
if ( !v5 )
break;
close(v5);
state[40 * v6 + 8] = 0;
v29[v6 / 64] &= __ROL8__(-2LL, v6 % 64);
v4 = (unsigned int)(v6 + 1);
}
v23 += v12;
v25 = v9;
}
else
{
v25 = v9;
}
gettimeofday(&tv, 0LL);
tvsub(&v33, &tv, v35);
v7 = (double)(int)v33 * 1000000.0 + (double)(int)v34;
if ( v7 >= 9.223372036854776e18 )
v8 = (unsigned int)(int)(v7 - 9.223372036854776e18) ^ 0x8000000000000000LL;
else
v8 = (unsigned int)(int)v7;
if ( (v8 & 0x8000000000000000LL) != 0LL )
v22 = (double)(int)(v8 & 1 | (v8 >> 1)) + (double)(int)(v8 & 1 | (v8 >> 1));
else
v22 = (double)(int)v8;
++v9;
}
while ( v22 <= 5000000.0 );
fwrite("\nBandwidth:\n", 1uLL, 0xCuLL, stderr);
v13 = maxfd;
v14 = v27;
if ( v27 > maxfd )
{
v24 = 0.0;
v16 = 0;
v19 = 0;
}
else
{
v15 = v28;
v24 = 0.0;
v16 = 0;
v26 = v3;
v17 = 0;
do
{
v16 -= (*v15 == 0) - 1;
v17 += *v15;
v18 = (double)*v15 * 8.0 / v22;
v24 = v24 + v18;
if ( *(v15 - 16) )
{
fprintf(stderr, "%c%.4f ", 43LL, v18);
}
else if ( verbose )
{
fprintf(stderr, "%c%.4f ", 45LL, v18);
}
*v15 = 0;
++v14;
v13 = maxfd;
v15 += 40;
}
while ( maxfd >= v14 );
v19 = v17;
v3 = v26;
}
v20 = v24 / scalar;
v21 = FD_POP(v13, v30);
fprintf(
stderr,
"\n"
"\t %d streams active, %d made progress: tot = %d, tot Mb/s = %.2f\n"
"\t opened %d, closed %d descriptors (loop count %d)\n"
"\n",
v21,
v16,
v19,
v20,
v11,
v23,
v25);
v35[0] = _mm_loadu_si128((const __m128i *)&tv);
}
}
exit(-1);
}

original_code:
void
simple_rx(int num_ports, int base_rx_port)
{
    int datafd, maxlfd;
    unsigned long diffus;
    struct timeval last, diff, now={ 0L, 0L };
    int opened=0, closed=0, cnt=0;
    fd_set fds_listeners, fds_active, fds_finished;
    int s, rc, fd;
    ;
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds_listeners)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds_active)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds_finished)->__fds_bits)[0]) : "memory"); } while (0);
    if((maxlfd = create_listeners(&fds_listeners, num_ports, base_rx_port)) < 0)
        exit(-1);
    datafd = maxlfd+1;
    while(1)
    {
        rc = accept_incoming(maxlfd, &fds_listeners, &fds_active);
        if(rc > 0) opened += rc;
        rc = sink_data(&fds_active, &fds_finished);
        if(rc > 0) closed += rc;
        cnt++;
        for(s=0;
            (rc>0) && (fd = FD_FFSandC(s, maxfd, &fds_finished));
            s = fd+1)
        {
            close(fd);
            state[fd].open = 0;
            ((void) (((&fds_finished)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
        }
        gettimeofday(&now, (struct timezone *)0);
        tvsub(&diff, &now, &last);
        diffus = diff.tv_sec*1e6 + diff.tv_usec;
        if(diffus > 5e6)
        {
            int i, totb=0, prog=0;
            double mbs, tmbs=0.0;
            fprintf(stderr, "\nBandwidth:\n");
            for(i=datafd; i <= maxfd; i++)
            {
                if(state[i].rx_rcvd_cpt) prog++;
                totb += state[i].rx_rcvd_cpt;
                mbs = (double)(8.0*state[i].rx_rcvd_cpt) / (double)diffus;
                tmbs += mbs;
                if(state[i].open) fprintf(stderr,"%c%.4f ",'+', mbs);
                else
                    if(verbose) fprintf(stderr,"%c%.4f ",'-', mbs);
                state[i].rx_rcvd_cpt = 0;
            }
            fprintf(stderr,
                    "\n\t %d streams active, %d made progress: "
                    "tot = %d, tot Mb/s = %.2f\n"
                    "\t opened %d, closed %d descriptors (loop count %d)\n\n",
                    FD_POP(maxfd, &fds_active), prog,
                    totb, tmbs/scalar,
                    opened, closed, cnt);
            opened = closed = cnt = 0;
            last = now;
        }
    }
    return;
}

canonical_original_code:
void simple_rx(int num_ports, int base_rx_port)
{
    int datafd; int maxlfd;
    unsigned long diffus;
    struct timeval last; struct timeval diff; struct timeval now = { 0L, 0L };
    int opened = 0; int closed = 0; int cnt = 0;
    struct fd_set fds_listeners; struct fd_set fds_active; struct fd_set fds_finished;
    int s; int rc; int fd;
    ;
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds_listeners)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds_active)->__fds_bits)[0]) : "memory"); } while (0);
    do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&fds_finished)->__fds_bits)[0]) : "memory"); } while (0);
    if((maxlfd = create_listeners(&fds_listeners, num_ports, base_rx_port)) < 0)
        exit(-1);
    datafd = maxlfd+1;
    while(1)
    {
        rc = accept_incoming(maxlfd, &fds_listeners, &fds_active);
        if(rc > 0) opened += rc;
        rc = sink_data(&fds_active, &fds_finished);
        if(rc > 0) closed += rc;
        cnt++;
        for(s=0;
            (rc>0) && (fd = FD_FFSandC(s, maxfd, &fds_finished));
            s = fd+1)
        {
            close(fd);
            state[fd].open = 0;
            ((void) (((&fds_finished)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] &= ~((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
        }
        gettimeofday(&now, (struct timezone *)0);
        tvsub(&diff, &now, &last);
        diffus = diff.tv_sec*1e6 + diff.tv_usec;
        if(diffus > 5e6)
        {
            int i; int totb = 0; int prog = 0;
            double mbs; double tmbs = 0.0;
            fprintf(stderr, "\nBandwidth:\n");
            for(i=datafd; i <= maxfd; i++)
            {
                if(state[i].rx_rcvd_cpt) prog++;
                totb += state[i].rx_rcvd_cpt;
                mbs = (double)(8.0*state[i].rx_rcvd_cpt) / (double)diffus;
                tmbs += mbs;
                if(state[i].open) fprintf(stderr,"%c%.4f ",'+', mbs);
                else
                    if(verbose) fprintf(stderr,"%c%.4f ",'-', mbs);
                state[i].rx_rcvd_cpt = 0;
            }
            fprintf(stderr,
                    "\n\t %d streams active, %d made progress: "
                    "tot = %d, tot Mb/s = %.2f\n"
                    "\t opened %d, closed %d descriptors (loop count %d)\n\n",
                    FD_POP(maxfd, &fds_active), prog,
                    totb, tmbs/scalar,
                    opened, closed, cnt);
            opened = closed = cnt = 0;
            last = now;
        }
    }
    return;
}

variable_types: datafd int

variable_types: maxlfd int

variable_types:
diffus
unsigned long

variable_types: opened int

variable_types: closed int

variable_types: rc int

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


usage

repo: morettoni/zonenotify

decompiled_code:
void __noreturn usage()
{
printf(
"%s, %s\n\nusage: zonenotify zone slave [slave 2 ... [slave n]] \n",
"zonenotify 0.01",
"Luca Morettoni <luca@morettoni.net>");
puts("where zone is the domain name to update on slave server ``slave''");
exit(1);
}

canonical_decompiled_code:
void __noreturn func9()
{
printf(
"%s, %s\n\nusage: zonenotify zone slave [slave 2 ... [slave n]] \n",
"zonenotify 0.01",
"Luca Morettoni <luca@morettoni.net>");
puts("where zone is the domain name to update on slave server ``slave''");
exit(1);
}

original_code:
void usage ()
{
 printf ("%s, %s\n\nusage: zonenotify zone slave [slave 2 ... [slave n]] \n", "zonenotify 0.01", "Luca Morettoni <luca@morettoni.net>");
 printf ("where zone is the domain name to update on slave server ``slave''\n");
 exit (1);
}

canonical_original_code:
void usage()
{
 printf ("%s, %s\n\nusage: zonenotify zone slave [slave 2 ... [slave n]] \n", "zonenotify 0.01", "Luca Morettoni <luca@morettoni.net>");
 printf ("where zone is the domain name to update on slave server ``slave''\n");
 exit (1);
}


decompiled_code:
__int64 __fastcall init_connection(char *name)
{
int v1; // ebx
__int16 v2; // ax
int v3; // eax
struct hostent *v4; // rax
time_t timer; // [rsp+8h] [rbp-40h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF
struct sockaddr inp[2]; // [rsp+20h] [rbp-28h] BYREF

time(&timer);
srandom(timer);
s = socket(2, 2, 0);
if ( s < 0 )
return 0xFFFFFFFFLL;
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)_bswap_32(0);
v1 = 0;
do
{
v2 = random();
*(_WORD *)addr.sa_data = _bswap_16(v2 % 15000 + 1025);
v3 = bind(s, &addr, 0x10u);
++v1;
}
while ( v1 <= 11 && v3 );
if ( v3 )
return 0xFFFFFFFFLL;
*(_QWORD *)&inp[0].sa_family = 0LL;
*(_QWORD *)&inp[0].sa_data[6] = 0LL;
if ( inet_aton(name, (struct in_addr *)&inp[0].sa_data[2]) )
{
LABEL_9:
inp[0].sa_family = 2;
*(_WORD *)inp[0].sa_data = _bswap_16(53);
return (unsigned int)-(connect(s, inp, 0x10u) != 0);
}
v4 = gethostbyname(name);
if ( v4 )
{
*(_DWORD *)&inp[0].sa_data[2] = **(_DWORD **)v4->h_addr_list;
goto LABEL_9;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func10(char *name)
{
int v1; // ebx
__int16 v2; // ax
int v3; // eax
struct hostent *v4; // rax
time_t timer; // [rsp+8h] [rbp-40h] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-38h] BYREF
struct sockaddr inp[2]; // [rsp+20h] [rbp-28h] BYREF

time(&timer);
srandom(timer);
s = socket(2, 2, 0);
if ( s < 0 )
return 0xFFFFFFFFLL;
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_QWORD *)&addr.sa_data[2] = (unsigned int)func7(0);
v1 = 0;
do
{
v2 = random();
*(_WORD *)addr.sa_data = func8(v2 % 15000 + 1025);
v3 = bind(s, &addr, 0x10u);
++v1;
}
while ( v1 <= 11 && v3 );
if ( v3 )
return 0xFFFFFFFFLL;
*(_QWORD *)&inp[0].sa_family = 0LL;
*(_QWORD *)&inp[0].sa_data[6] = 0LL;
if ( inet_aton(name, (struct in_addr *)&inp[0].sa_data[2]) )
{
LABEL_9:
inp[0].sa_family = 2;
*(_WORD *)inp[0].sa_data = func8(53);
return (unsigned int)-(connect(s, inp, 0x10u) != 0);
}
v4 = gethostbyname(name);
if ( v4 )
{
*(_DWORD *)&inp[0].sa_data[2] = **(_DWORD **)v4->h_addr_list;
goto LABEL_9;
}
return 0xFFFFFFFFLL;
}

original_code:
int init_connection (const char *server)
{
 struct sockaddr_in sa, sl;
 struct hostent *he;
 int isbind = 0;
 int i;
 long rand;
 time_t now;
 time (&now);
 srandom ((long) now);
 s = socket (2, SOCK_DGRAM, 0);
 if (s < 0) return -1;
 memset (&sl, 0, sizeof (sl));
 sl.sin_family = 2;
 sl.sin_addr.s_addr = htonl (((in_addr_t) 0x00000000));
 for (i = 0; i < 12 && !isbind; i++) {
  rand = 1025+(random () % 15000);
  sl.sin_port = htons (rand);
  isbind = (bind (s, (struct sockaddr *) &sl, sizeof (sl)) == 0);
 }
 if (!isbind) return -1;
 memset (&sa, 0, sizeof (sa));
 if (inet_aton (server, &sa.sin_addr) == 0) {
  he = gethostbyname (server);
  if (he)
   memcpy (&sa.sin_addr, he->h_addr_list[0], sizeof (sa.sin_addr));
  else
   return -1;
 }
 sa.sin_family = 2;
 sa.sin_port = htons (53);
 if (connect (s, (struct sockaddr *) &sa, sizeof (sa)) != 0)
  return -1;
 return 0;
}

canonical_original_code:
int init_connection(char *server)
{
 struct sockaddr_in sa; struct sockaddr_in sl;
 struct hostent *he;
 int isbind = 0;
 int i;
 long rand;
 long now;
 time (&now);
 srandom ((long) now);
 s = socket (2, SOCK_DGRAM, 0);
 if (s < 0) return -1;
 memset (&sl, 0, sizeof (sl));
 sl.sin_family = 2;
 sl.sin_addr.s_addr = htonl (((unsigned int) 0x00000000));
 for (i = 0; i < 12 && !isbind; i++) {
  rand = 1025+(random () % 15000);
  sl.sin_port = htons (rand);
  isbind = (bind (s, (struct sockaddr *) &sl, sizeof (sl)) == 0);
 }
 if (!isbind) return -1;
 memset (&sa, 0, sizeof (sa));
 if (inet_aton (server, &sa.sin_addr) == 0) {
  he = gethostbyname (server);
  if (he)
   memcpy (&sa.sin_addr, he->h_addr_list[0], sizeof (sa.sin_addr));
  else
   return -1;
 }
 sa.sin_family = 2;
 sa.sin_port = htons (53);
 if (connect (s, (struct sockaddr *) &sa, sizeof (sa)) != 0)
  return -1;
 return 0;
}

variable_types:
server
char *

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

variable_types: struct hostent { char *h_name; char **h_aliases; int h_addrtype; int h_length; char **h_addr_list; } *

variable_types: isbind int

variable_types: rand long

variable_types: now long

return_type: int

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

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


decompiled_code:
int stop_connection()
{
return shutdown(s, 2);
}

canonical_decompiled_code:
int func11()
{
return shutdown(s, 2);
}

original_code:
void stop_connection (void)
{
 shutdown (s, SHUT_RDWR);
}

canonical_original_code:
void stop_connection()
{
 shutdown (s, SHUT_RDWR);
}


decompiled_code:
__int64 __fastcall ns_encode(char *src, char *a2)
{
unsigned int v4; // er15
int v5; // er13
char *v6; // r14
size_t v7; // rbx
char *v8; // rax
char *v9; // rbp
int v10; // eax
int v11; // ebp
char *v13; // r13
__int64 v14; // rbx

v4 = 0;
while ( 1 )
{
v8 = strchr(src, 46);
v9 = v8;
if ( !v8 )
break;
v5 = (_DWORD)v8 - (_DWORD)src;
v6 = a2 + 1;
*a2 = (_BYTE)v8 - (_BYTE)src;
v7 = (int)v8 - (int)src;
strncpy(v6, src, v7);
a2 = &v6[v7];
v4 += v5 + 1;
src = v9 + 1;
}
v10 = strlen(src);
v11 = v10;
if ( v10 )
{
v13 = a2 + 1;
*a2 = v10;
v14 = v10;
strncpy(v13, src, v10);
a2 = &v13[v14];
v4 += v11 + 1;
}
*a2 = 0;
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func12(char *src, char *a2)
{
unsigned int v4; // er15
int v5; // er13
char *v6; // r14
size_t v7; // rbx
char *v8; // rax
char *v9; // rbp
int v10; // eax
int v11; // ebp
char *v13; // r13
__int64 v14; // rbx

v4 = 0;
while ( 1 )
{
v8 = strchr(src, 46);
v9 = v8;
if ( !v8 )
break;
v5 = (_DWORD)v8 - (_DWORD)src;
v6 = a2 + 1;
*a2 = (_BYTE)v8 - (_BYTE)src;
v7 = (int)v8 - (int)src;
strncpy(v6, src, v7);
a2 = &v6[v7];
v4 += v5 + 1;
src = v9 + 1;
}
v10 = strlen(src);
v11 = v10;
if ( v10 )
{
v13 = a2 + 1;
*a2 = v10;
v14 = v10;
strncpy(v13, src, v10);
a2 = &v13[v14];
v4 += v11 + 1;
}
*a2 = 0;
return v4;
}

original_code:
int ns_encode (char *str, char *buff)
{
 char *pos;
 int size;
 int len = 0;
 while (1) {
  pos = (char *) strchr (str, '.');
  if (!pos) break;
  size = pos-str;
  *buff++ = size;
  strncpy (buff, str, size);
  buff += size;
  len += size+1;
  str = pos+1;
 }
 size = strlen (str);
 if (size) {
  *buff++ = size;
  strncpy (buff, str, size);
  buff += size;
  len += size+1;
 }
 *buff = 0;
 return len;
}

canonical_original_code:
int ns_encode(char *str, char *buff)
{
 char *pos;
 int size;
 int len = 0;
 while (1) {
  pos = (char *) strchr (str, '.');
  if (!pos) break;
  size = pos-str;
  *buff++ = size;
  strncpy (buff, str, size);
  buff += size;
  len += size+1;
  str = pos+1;
 }
 size = strlen (str);
 if (size) {
  *buff++ = size;
  strncpy (buff, str, size);
  buff += size;
  len += size+1;
 }
 *buff = 0;
 return len;
}

variable_types:
str
char *

variable_types:
buff
char *

variable_types:
pos
char *

variable_types: size int

variable_types: len int


decompiled_code:
__int64 __fastcall slave_notify(char *src, const char *a2)
{
__int16 v3; // bp
__int16 v4; // di
int v5; // edi
int v6; // edx
__int64 result; // rax
struct timeval v8; // [rsp+0h] [rbp-6C8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-6B8h] BYREF
char srca[1040]; // [rsp+90h] [rbp-638h] BYREF
__int16 buf; // [rsp+4A0h] [rbp-228h] BYREF
char v12; // [rsp+4A2h] [rbp-226h]
char v13; // [rsp+4A3h] [rbp-225h]
__int16 v14; // [rsp+4A4h] [rbp-224h]
__int16 v15; // [rsp+4A6h] [rbp-222h]
__int16 v16; // [rsp+4A8h] [rbp-220h]
__int16 v17; // [rsp+4AAh] [rbp-21Eh]
__int16 dest[270]; // [rsp+4ACh] [rbp-21Ch] BYREF

v12 = 36;
v13 &= 0x30u;
v3 = _bswap_16(1);
v14 = v3;
v15 = 0;
v16 = 0;
v17 = 0;
v4 = unique_0++;
buf = _bswap_16(v4);
LODWORD(src) = ns_encode(src, srca);
memcpy(dest, srca, (int)src + 1);
*(__int16 *)((char *)dest + (int)src + 1) = _bswap_16(6);
*(__int16 *)((char *)&dest[1] + (int)src + 1) = v3;
v8.tv_sec = 120LL;
v8.tv_usec = 0LL;
memset(&readfds, 0, sizeof(readfds));
v5 = s;
readfds.fds_bits[s / 64] |= 1LL << (s % 64);
if ( send(v5, &buf, (int)src + 17LL, 0) == -1 )
return 0xFFFFFFFFLL;
if ( select(1024, &readfds, 0LL, 0LL, &v8) <= 0 )
{
fprintf(stderr, "%s: timeout\n", a2);
result = 0xFFFFFFFFLL;
}
else
{
v6 = recv(s, &buf, 0x200uLL, 0);
result = 0LL;
if ( v6 != -1 && v12 < 0 )
{
if ( (v13 & 0xF) != 0 )
{
fprintf(stderr, "%s: %s\n", a2, dns_errors[v13 & 0xF]);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(char *src, const char *a2)
{
__int16 v3; // bp
__int16 v4; // di
int v5; // edi
int v6; // edx
__int64 result; // rax
struct timeval v8; // [rsp+0h] [rbp-6C8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-6B8h] BYREF
char srca[1040]; // [rsp+90h] [rbp-638h] BYREF
__int16 buf; // [rsp+4A0h] [rbp-228h] BYREF
char v12; // [rsp+4A2h] [rbp-226h]
char v13; // [rsp+4A3h] [rbp-225h]
__int16 v14; // [rsp+4A4h] [rbp-224h]
__int16 v15; // [rsp+4A6h] [rbp-222h]
__int16 v16; // [rsp+4A8h] [rbp-220h]
__int16 v17; // [rsp+4AAh] [rbp-21Eh]
__int16 dest[270]; // [rsp+4ACh] [rbp-21Ch] BYREF

v12 = 36;
v13 &= 0x30u;
v3 = func8(1);
v14 = v3;
v15 = 0;
v16 = 0;
v17 = 0;
v4 = unique_0++;
buf = func8(v4);
LODWORD(src) = func12(src, srca);
memcpy(dest, srca, (int)src + 1);
*(__int16 *)((char *)dest + (int)src + 1) = func8(6);
*(__int16 *)((char *)&dest[1] + (int)src + 1) = v3;
v8.tv_sec = 120LL;
v8.tv_usec = 0LL;
memset(&readfds, 0, sizeof(readfds));
v5 = s;
readfds.fds_bits[s / 64] |= 1LL << (s % 64);
if ( send(v5, &buf, (int)src + 17LL, 0) == -1 )
return 0xFFFFFFFFLL;
if ( select(1024, &readfds, 0LL, 0LL, &v8) <= 0 )
{
fprintf(stderr, "%s: timeout\n", a2);
result = 0xFFFFFFFFLL;
}
else
{
v6 = recv(s, &buf, 0x200uLL, 0);
result = 0LL;
if ( v6 != -1 && v12 < 0 )
{
if ( (v13 & 0xF) != 0 )
{
fprintf(stderr, "%s: %s\n", a2, dns_errors[v13 & 0xF]);
result = 0xFFFFFFFFLL;
}
else
{
result = 0LL;
}
}
}
return result;
}

original_code:
int slave_notify (char *domain, const char *server)
{
 static int unique = 0;
 char buffer[512];
 char name[1025];
 HEADER *hdr;
 int len, reqlen;
 u_int16_t val;
 fd_set active_fd_set;
 struct timeval tv_timeout;
 hdr = (HEADER*) buffer;
        hdr->qr = 0;
        hdr->opcode = ns_o_notify;
        hdr->aa = 1;
        hdr->tc = 0;
        hdr->rd = 0;
        hdr->ra = 0;
        hdr->unused = 0;
        hdr->rcode = 0;
        hdr->qdcount = htons (1);
        hdr->ancount = 0;
        hdr->nscount = 0;
        hdr->arcount = 0;
        hdr->id = htons (unique++);
 reqlen = ns_encode (domain, name)+1;
 memcpy (buffer+sizeof (HEADER), name, reqlen);
 val = htons (ns_t_soa);
 memcpy (buffer+sizeof (HEADER)+reqlen, &val, 2);
 reqlen += 2;
 val = htons(ns_c_in);
 memcpy (buffer+sizeof (HEADER)+reqlen, &val, 2);
 reqlen += 2;
 tv_timeout.tv_sec = 120;
 tv_timeout.tv_usec = 0;
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&active_fd_set)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&active_fd_set)->__fds_bits)[((s) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((s) % (8 * (int) sizeof (__fd_mask)))))));
 if (send (s, buffer, sizeof (HEADER)+reqlen, 0) == -1)
  return -1;
 if (select (1024, &active_fd_set, ((void *)0), ((void *)0), &tv_timeout) < 1)
 {
  fprintf (stderr, "%s: timeout\n", server);
  return -1;
 }
 len = recv (s, buffer, 512, 0);
 if (len != -1) {
  hdr = (HEADER*) buffer;
  if (hdr->qr && hdr->rcode) {
   fprintf (stderr, "%s: %s\n",
     server,
     hdr->rcode < 23 ? dns_errors[hdr->rcode] : "unknow error");
   return -1;
  }
 }
 return 0;
}

canonical_original_code:
int slave_notify(char *domain, char *server)
{
 static int unique = 0;
 char buffer[512];
 char name[1025];
 struct HEADER *hdr;
 int len; int reqlen;
 unsigned int val;
 struct fd_set active_fd_set;
 struct timeval tv_timeout;
 hdr = (struct HEADER *) buffer;
        hdr->qr = 0;
        hdr->opcode = ns_o_notify;
        hdr->aa = 1;
        hdr->tc = 0;
        hdr->rd = 0;
        hdr->ra = 0;
        hdr->unused = 0;
        hdr->rcode = 0;
        hdr->qdcount = htons (1);
        hdr->ancount = 0;
        hdr->nscount = 0;
        hdr->arcount = 0;
        hdr->id = htons (unique++);
 reqlen = ns_encode (domain, name)+1;
 memcpy (buffer+sizeof (HEADER), name, reqlen);
 val = htons (ns_t_soa);
 memcpy (buffer+sizeof (HEADER)+reqlen, &val, 2);
 reqlen += 2;
 val = htons(ns_c_in);
 memcpy (buffer+sizeof (HEADER)+reqlen, &val, 2);
 reqlen += 2;
 tv_timeout.tv_sec = 120;
 tv_timeout.tv_usec = 0;
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&active_fd_set)->__fds_bits)[0]) : "memory"); } while (0);
 ((void) (((&active_fd_set)->__fds_bits)[((s) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((s) % (8 * (int) sizeof (__fd_mask)))))));
 if (send (s, buffer, sizeof (HEADER)+reqlen, 0) == -1)
  return -1;
 if (select (1024, &active_fd_set, ((void *)0), ((void *)0), &tv_timeout) < 1)
 {
  fprintf (stderr, "%s: timeout\n", server);
  return -1;
 }
 len = recv (s, buffer, 512, 0);
 if (len != -1) {
  hdr = (struct HEADER *) buffer;
  if (hdr->qr && hdr->rcode) {
   fprintf (stderr, "%s: %s\n",
     server,
     hdr->rcode < 23 ? dns_errors[hdr->rcode] : "unknow error");
   return -1;
  }
 }
 return 0;
}

variable_types:
domain
char *

variable_types: unique int

variable_types:
buffer
char [512]

variable_types:
name
char [1025]

variable_types: struct HEADER { unsigned int id; unsigned int rd; unsigned int tc; unsigned int aa; unsigned int opcode; unsigned int qr; unsigned int rcode; unsigned int cd; unsigned int ad; unsigned int unused; unsigned int ra; unsigned int qdcount; unsigned int ancount; unsigned int nscount; unsigned int arcount; } *

variable_types: reqlen int

variable_types:
val
unsigned int

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


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbx
int v4; // ebp

if ( argc > 2 )
{
v3 = argv + 2;
v4 = 0;
while ( 1 )
{
if ( (unsigned int)init_connection((char *)*v3) == -1 )
{
fprintf(stderr, "Can't connect to %s\n", *v3);
}
else
{
if ( (unsigned int)slave_notify((char *)argv[1], *v3) == -1 )
v4 = 1;
stop_connection();
}
if ( ++v3 == &argv[(unsigned int)(argc - 3) + 3] )
exit(v4);
}
}
usage(argc, argv, envp);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char **v3; // rbx
int v4; // ebp

if ( argc > 2 )
{
v3 = argv + 2;
v4 = 0;
while ( 1 )
{
if ( (unsigned int)func10((char *)*v3) == -1 )
{
fprintf(stderr, "Can't connect to %s\n", *v3);
}
else
{
if ( (unsigned int)func13((char *)argv[1], *v3) == -1 )
v4 = 1;
func11();
}
if ( ++v3 == &argv[(unsigned int)(argc - 3) + 3] )
exit(v4);
}
}
func9(argc, argv, envp);
}

original_code:
int main(int argc, char *argv[])
{
 int i = 2;
 int ret = 0;
 if (argc < 3) usage ();
 while (i < argc) {
  if (init_connection (argv[i]) != -1) {
   if (slave_notify (argv[1], argv[i]) == -1) ret = 1;
   stop_connection ();
  } else
   fprintf (stderr, "Can't connect to %s\n", argv[i]);
  i++;
 }
 exit (ret);
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: i int

variable_types: ret int


decompiled_code:
__int64 __fastcall gpio_export(unsigned int a1)
{
int v1; // ebx
int v2; // eax
char v4[88]; // [rsp+0h] [rbp-58h] BYREF

v1 = open("/sys/class/gpio/export", 1);
if ( v1 < 0 )
{
perror("gpio/export");
}
else
{
v2 = snprintf(v4, 0x40uLL, "%d", a1);
write(v1, v4, v2);
close(v1);
v1 = 0;
}
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned int a1)
{
int v1; // ebx
int v2; // eax
char v4[88]; // [rsp+0h] [rbp-58h] BYREF

v1 = open("/sys/class/gpio/export", 1);
if ( v1 < 0 )
{
perror("gpio/export");
}
else
{
v2 = snprintf(v4, 0x40uLL, "%d", a1);
write(v1, v4, v2);
close(v1);
v1 = 0;
}
return (unsigned int)v1;
}

original_code:
int gpio_export(unsigned int gpio)
{
  int fd, len;
  char buf[64];
  fd = open("/sys/class/gpio" "/export", 01);
  if (fd < 0)
  {
    perror("gpio/export");
    return fd;
  }
  len = snprintf(buf, sizeof(buf), "%d", gpio);
  write(fd, buf, len);
  close(fd);
  return 0;
}

canonical_original_code:
int gpio_export(unsigned int gpio)
{
  int fd; int len;
  char buf[64];
  fd = open("/sys/class/gpio" "/export", 01);
  if (fd < 0)
  {
    perror("gpio/export");
    return fd;
  }
  len = snprintf(buf, sizeof(buf), "%d", gpio);
  write(fd, buf, len);
  close(fd);
  return 0;
}

variable_types:
gpio
unsigned int

variable_types: fd int

variable_types:
buf
char [64]


decompiled_code:
__int64 __fastcall gpio_unexport(unsigned int a1)
{
int v1; // ebx
int v2; // eax
char v4[88]; // [rsp+0h] [rbp-58h] BYREF

v1 = open("/sys/class/gpio/unexport", 1);
if ( v1 < 0 )
{
perror("gpio/export");
}
else
{
v2 = snprintf(v4, 0x40uLL, "%d", a1);
write(v1, v4, v2);
close(v1);
v1 = 0;
}
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned int a1)
{
int v1; // ebx
int v2; // eax
char v4[88]; // [rsp+0h] [rbp-58h] BYREF

v1 = open("/sys/class/gpio/unexport", 1);
if ( v1 < 0 )
{
perror("gpio/export");
}
else
{
v2 = snprintf(v4, 0x40uLL, "%d", a1);
write(v1, v4, v2);
close(v1);
v1 = 0;
}
return (unsigned int)v1;
}

original_code:
int gpio_unexport(unsigned int gpio)
{
  int fd, len;
  char buf[64];
  fd = open("/sys/class/gpio" "/unexport", 01);
  if (fd < 0)
  {
    perror("gpio/export");
    return fd;
  }
  len = snprintf(buf, sizeof(buf), "%d", gpio);
  write(fd, buf, len);
  close(fd);
  return 0;
}

canonical_original_code:
int gpio_unexport(unsigned int gpio)
{
  int fd; int len;
  char buf[64];
  fd = open("/sys/class/gpio" "/unexport", 01);
  if (fd < 0)
  {
    perror("gpio/export");
    return fd;
  }
  len = snprintf(buf, sizeof(buf), "%d", gpio);
  write(fd, buf, len);
  close(fd);
  return 0;
}


decompiled_code:
__int64 __fastcall gpio_set_dir(unsigned int a1, int a2)
{
int v2; // eax
unsigned int v3; // ebx
char v5[88]; // [rsp+0h] [rbp-58h] BYREF

snprintf(v5, 0x40uLL, "/sys/class/gpio/gpio%d/direction", a1);
v2 = open(v5, 1);
v3 = v2;
if ( v2 < 0 )
{
perror("gpio/direction");
}
else
{
if ( a2 )
write(v2, "out", 4uLL);
else
write(v2, "in", 3uLL);
close(v3);
v3 = 0;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, int a2)
{
int v2; // eax
unsigned int v3; // ebx
char v5[88]; // [rsp+0h] [rbp-58h] BYREF

snprintf(v5, 0x40uLL, "/sys/class/gpio/gpio%d/direction", a1);
v2 = open(v5, 1);
v3 = v2;
if ( v2 < 0 )
{
perror("gpio/direction");
}
else
{
if ( a2 )
write(v2, "out", 4uLL);
else
write(v2, "in", 3uLL);
close(v3);
v3 = 0;
}
return v3;
}

original_code:
int gpio_set_dir(unsigned int gpio, unsigned int out_flag)
{
  int fd, len;
  char buf[64];
  len = snprintf(buf, sizeof(buf), "/sys/class/gpio" "/gpio%d/direction", gpio);
  fd = open(buf, 01);
  if (fd < 0)
  {
    perror("gpio/direction");
    return fd;
  }
  if (out_flag)
    write(fd, "out", 4);
  else
    write(fd, "in", 3);
  close(fd);
  return 0;
}

canonical_original_code:
int gpio_set_dir(unsigned int gpio, unsigned int out_flag)
{
  int fd; int len;
  char buf[64];
  len = snprintf(buf, sizeof(buf), "/sys/class/gpio" "/gpio%d/direction", gpio);
  fd = open(buf, 01);
  if (fd < 0)
  {
    perror("gpio/direction");
    return fd;
  }
  if (out_flag)
    write(fd, "out", 4);
  else
    write(fd, "in", 3);
  close(fd);
  return 0;
}


decompiled_code:
__int64 __fastcall gpio_set_value(unsigned int a1, int a2)
{
int v2; // eax
unsigned int v3; // ebx
char v5[88]; // [rsp+0h] [rbp-58h] BYREF

snprintf(v5, 0x40uLL, "/sys/class/gpio/gpio%d/value", a1);
v2 = open(v5, 1);
v3 = v2;
if ( v2 < 0 )
{
perror("gpio/set-value");
}
else
{
if ( a2 )
write(v2, "1", 2uLL);
else
write(v2, "0", 2uLL);
close(v3);
v3 = 0;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1, int a2)
{
int v2; // eax
unsigned int v3; // ebx
char v5[88]; // [rsp+0h] [rbp-58h] BYREF

snprintf(v5, 0x40uLL, "/sys/class/gpio/gpio%d/value", a1);
v2 = open(v5, 1);
v3 = v2;
if ( v2 < 0 )
{
perror("gpio/set-value");
}
else
{
if ( a2 )
write(v2, "1", 2uLL);
else
write(v2, "0", 2uLL);
close(v3);
v3 = 0;
}
return v3;
}

original_code:
int gpio_set_value(unsigned int gpio, unsigned int value)
{
  int fd, len;
  char buf[64];
  len = snprintf(buf, sizeof(buf), "/sys/class/gpio" "/gpio%d/value", gpio);
  fd = open(buf, 01);
  if (fd < 0)
  {
    perror("gpio/set-value");
    return fd;
  }
  if (value)
    write(fd, "1", 2);
  else
    write(fd, "0", 2);
  close(fd);
  return 0;
}

canonical_original_code:
int gpio_set_value(unsigned int gpio, unsigned int value)
{
  int fd; int len;
  char buf[64];
  len = snprintf(buf, sizeof(buf), "/sys/class/gpio" "/gpio%d/value", gpio);
  fd = open(buf, 01);
  if (fd < 0)
  {
    perror("gpio/set-value");
    return fd;
  }
  if (value)
    write(fd, "1", 2);
  else
    write(fd, "0", 2);
  close(fd);
  return 0;
}

variable_types:
value
unsigned int


decompiled_code:
__int64 __fastcall gpio_get_value(unsigned int a1, _DWORD *a2)
{
int v2; // eax
unsigned int v3; // ebx
char buf; // [rsp+Fh] [rbp-59h] BYREF
char s[88]; // [rsp+10h] [rbp-58h] BYREF

snprintf(s, 0x40uLL, "/sys/class/gpio/gpio%d/value", a1);
v2 = open(s, 0);
v3 = v2;
if ( v2 < 0 )
{
perror("gpio/get-value");
}
else
{
read(v2, &buf, 1uLL);
*a2 = buf != 48;
close(v3);
v3 = 0;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1, _DWORD *a2)
{
int v2; // eax
unsigned int v3; // ebx
char buf; // [rsp+Fh] [rbp-59h] BYREF
char s[88]; // [rsp+10h] [rbp-58h] BYREF

snprintf(s, 0x40uLL, "/sys/class/gpio/gpio%d/value", a1);
v2 = open(s, 0);
v3 = v2;
if ( v2 < 0 )
{
perror("gpio/get-value");
}
else
{
read(v2, &buf, 1uLL);
*a2 = buf != 48;
close(v3);
v3 = 0;
}
return v3;
}

original_code:
int gpio_get_value(unsigned int gpio, unsigned int *value)
{
  int fd, len;
  char buf[64];
  char ch;
  len = snprintf(buf, sizeof(buf), "/sys/class/gpio" "/gpio%d/value", gpio);
  fd = open(buf, 00);
  if (fd < 0)
  {
    perror("gpio/get-value");
    return fd;
  }
  read(fd, &ch, 1);
  if (ch != '0')
  {
    *value = 1;
  }
  else
  {
    *value = 0;
  }
  close(fd);
  return 0;
}

canonical_original_code:
int gpio_get_value(unsigned int gpio, unsigned int *value)
{
  int fd; int len;
  char buf[64];
  char ch;
  len = snprintf(buf, sizeof(buf), "/sys/class/gpio" "/gpio%d/value", gpio);
  fd = open(buf, 00);
  if (fd < 0)
  {
    perror("gpio/get-value");
    return fd;
  }
  read(fd, &ch, 1);
  if (ch != '0')
  {
    *value = 1;
  }
  else
  {
    *value = 0;
  }
  close(fd);
  return 0;
}

variable_types:
value
unsigned int *

variable_types: len int

variable_types: ch char


decompiled_code:
__int64 __fastcall gpio_set_edge(unsigned int a1, const char *a2)
{
int v2; // ebx
size_t v3; // rax
char v5[88]; // [rsp+0h] [rbp-58h] BYREF

snprintf(v5, 0x40uLL, "/sys/class/gpio/gpio%d/edge", a1);
v2 = open(v5, 1);
if ( v2 < 0 )
{
perror("gpio/set-edge");
}
else
{
v3 = strlen(a2);
write(v2, a2, v3 + 1);
close(v2);
v2 = 0;
}
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1, const char *a2)
{
int v2; // ebx
size_t v3; // rax
char v5[88]; // [rsp+0h] [rbp-58h] BYREF

snprintf(v5, 0x40uLL, "/sys/class/gpio/gpio%d/edge", a1);
v2 = open(v5, 1);
if ( v2 < 0 )
{
perror("gpio/set-edge");
}
else
{
v3 = strlen(a2);
write(v2, a2, v3 + 1);
close(v2);
v2 = 0;
}
return (unsigned int)v2;
}

original_code:
int gpio_set_edge(unsigned int gpio, char *edge)
{
  int fd, len;
  char buf[64];
  len = snprintf(buf, sizeof(buf), "/sys/class/gpio" "/gpio%d/edge", gpio);
  fd = open(buf, 01);
  if (fd < 0)
  {
    perror("gpio/set-edge");
    return fd;
  }
  write(fd, edge, strlen(edge) + 1);
  close(fd);
  return 0;
}

canonical_original_code:
int gpio_set_edge(unsigned int gpio, char *edge)
{
  int fd; int len;
  char buf[64];
  len = snprintf(buf, sizeof(buf), "/sys/class/gpio" "/gpio%d/edge", gpio);
  fd = open(buf, 01);
  if (fd < 0)
  {
    perror("gpio/set-edge");
    return fd;
  }
  write(fd, edge, strlen(edge) + 1);
  close(fd);
  return 0;
}

variable_types:
edge
char *


decompiled_code:
__int64 __fastcall gpio_fd_open(unsigned int a1)
{
int v1; // ebx
char v3[72]; // [rsp+0h] [rbp-48h] BYREF

snprintf(v3, 0x40uLL, "/sys/class/gpio/gpio%d/value", a1);
v1 = open(v3, 2048);
if ( v1 < 0 )
perror("gpio/fd_open");
return (unsigned int)v1;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1)
{
int v1; // ebx
char v3[72]; // [rsp+0h] [rbp-48h] BYREF

snprintf(v3, 0x40uLL, "/sys/class/gpio/gpio%d/value", a1);
v1 = open(v3, 2048);
if ( v1 < 0 )
perror("gpio/fd_open");
return (unsigned int)v1;
}

original_code:
int gpio_fd_open(unsigned int gpio)
{
  int fd, len;
  char buf[64];
  len = snprintf(buf, sizeof(buf), "/sys/class/gpio" "/gpio%d/value", gpio);
  fd = open(buf, 00 | 04000 );
  if (fd < 0)
  {
    perror("gpio/fd_open");
  }
  return fd;
}

canonical_original_code:
int gpio_fd_open(unsigned int gpio)
{
  int fd; int len;
  char buf[64];
  len = snprintf(buf, sizeof(buf), "/sys/class/gpio" "/gpio%d/value", gpio);
  fd = open(buf, 00 | 04000 );
  if (fd < 0)
  {
    perror("gpio/fd_open");
  }
  return fd;
}


decompiled_code:
int __fastcall gpio_fd_close(int a1)
{
return close(a1);
}

canonical_decompiled_code:
int __fastcall func7(int a1)
{
return close(a1);
}

original_code:
int gpio_fd_close(int fd)
{
  return close(fd);
}


expandWrite

decompiled_code:
__int64 __fastcall expandWrite(char a1)
{
return wiringOliI2CWriteReg8((unsigned int)dword_494, 0LL, (unsigned __int8)(byte_49D | a1));
}

canonical_decompiled_code:
__int64 __fastcall func0(char a1)
{
return wiringOliI2CWriteReg8((unsigned int)dword_494, 0LL, (unsigned __int8)(byte_49D | a1));
}

original_code:
static void expandWrite(unsigned char value)
{
  wiringOliI2CWriteReg8(LCDDev.fd, 0x00, value | LCDDev.backLightEn);
}

variable_types:
value
unsigned char


pulseEnable

decompiled_code:
__int64 __fastcall pulseEnable(char a1)
{
expandWrite(a1 | 4);
delayMicroseconds(50LL);
expandWrite(a1 & 0xFB);
return delayMicroseconds(50LL);
}

canonical_decompiled_code:
__int64 __fastcall func1(char a1)
{
func0(a1 | 4);
delayMicroseconds(50LL);
func0(a1 & 0xFB);
return delayMicroseconds(50LL);
}

original_code:
static void pulseEnable(unsigned char value)
{
  expandWrite(value | 4);
  delayMicroseconds(50);
  expandWrite(value & ~4);
  delayMicroseconds(50);
}


decompiled_code:
__int64 __fastcall write4Bits(char a1)
{
expandWrite(a1);
return pulseEnable(a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(char a1)
{
func0(a1);
return func1(a1);
}

original_code:
static void write4Bits(unsigned char value)
{
  expandWrite(value);
  pulseEnable(value);
}


writeCmd

decompiled_code:
__int64 __fastcall writeCmd(char a1)
{
write4Bits(a1 & 0xF0);
return write4Bits(16 * a1);
}

canonical_decompiled_code:
__int64 __fastcall func3(char a1)
{
func2(a1 & 0xF0);
return func2(16 * a1);
}

original_code:
static void writeCmd(unsigned char value)
{
  unsigned char tmp;
  tmp = value & 0xF0;
  write4Bits(tmp);
  tmp = value << 4;
  write4Bits(tmp);
}

variable_types:
tmp
unsigned char


writeData

decompiled_code:
__int64 __fastcall writeData(char a1)
{
write4Bits(a1 & 0xF0 | 1);
return write4Bits((16 * a1) | 1u);
}

canonical_decompiled_code:
__int64 __fastcall func4(char a1)
{
func2(a1 & 0xF0 | 1);
return func2((16 * a1) | 1u);
}

original_code:
static void writeData(unsigned char value)
{
  unsigned char tmp;
  tmp = value & 0xF0;
  write4Bits(tmp | 1);
  tmp = value << 4;
  write4Bits(tmp | 1);
}


LCDClear

decompiled_code:
__int64 LCDClear()
{
writeCmd(1);
return delayMicroseconds(50000LL);
}

canonical_decompiled_code:
__int64 func5()
{
func3(1);
return delayMicroseconds(50000LL);
}

original_code:
void LCDClear(void)
{
  writeCmd(0x01);
  delayMicroseconds(50000);
}

canonical_original_code:
void LCDClear()
{
  writeCmd(0x01);
  delayMicroseconds(50000);
}


LCDHome

decompiled_code:
__int64 LCDHome()
{
writeCmd(2);
return delayMicroseconds(50000LL);
}

canonical_decompiled_code:
__int64 func6()
{
func3(2);
return delayMicroseconds(50000LL);
}

original_code:
void LCDHome(void)
{
  writeCmd(0x02);
  delayMicroseconds(50000);
}

canonical_original_code:
void LCDHome()
{
  writeCmd(0x02);
  delayMicroseconds(50000);
}


LCDSetCursor

decompiled_code:
__int64 __fastcall LCDSetCursor(int a1, int a2)
{
__int64 result; // rax
int v3[3]; // [rsp+Ch] [rbp-Ch]

v3[0] = 1410613248;
result = (unsigned __int8)byte_49B;
if ( (unsigned __int8)byte_49B > a2 )
{
result = (unsigned __int8)byte_49C;
if ( (unsigned __int8)byte_49C > a1 )
result = writeCmd((*((_BYTE *)v3 + a1) + a2) | 0x80u);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1, int a2)
{
__int64 result; // rax
int v3[3]; // [rsp+Ch] [rbp-Ch]

v3[0] = 1410613248;
result = (unsigned __int8)byte_49B;
if ( (unsigned __int8)byte_49B > a2 )
{
result = (unsigned __int8)byte_49C;
if ( (unsigned __int8)byte_49C > a1 )
result = func3((*((_BYTE *)v3 + a1) + a2) | 0x80u);
}
return result;
}

original_code:
void LCDSetCursor(int row, int col)
{
  unsigned char rowOffset[] = {0x00, 0x40, 0x14, 0x54};
  if (col < LCDDev.numCols && row < LCDDev.numRows)
  {
    writeCmd(0x80 | (col + rowOffset[row]));
  }
}

variable_types: row int

variable_types: col int

variable_types:
rowOffset
unsigned char []


LCDDisplayOff

decompiled_code:
__int64 LCDDisplayOff()
{
byte_499 &= 0xFBu;
return writeCmd(byte_499 | 8u);
}

canonical_decompiled_code:
__int64 func8()
{
byte_499 &= 0xFBu;
return func3(byte_499 | 8u);
}

original_code:
void LCDDisplayOff(void)
{
  LCDDev.displayControl &= ~0x04;
  writeCmd(0x08 | LCDDev.displayControl);
}

canonical_original_code:
void LCDDisplayOff()
{
  LCDDev.displayControl &= ~0x04;
  writeCmd(0x08 | LCDDev.displayControl);
}


LCDDisplayOn

decompiled_code:
__int64 LCDDisplayOn()
{
char v0; // di

v0 = byte_499;
byte_499 |= 4u;
return writeCmd(v0 | 0xCu);
}

canonical_decompiled_code:
__int64 func9()
{
char v0; // di

v0 = byte_499;
byte_499 |= 4u;
return func3(v0 | 0xCu);
}

original_code:
void LCDDisplayOn(void)
{
  LCDDev.displayControl |= 0x04;
  writeCmd(0x08 | LCDDev.displayControl);
}

canonical_original_code:
void LCDDisplayOn()
{
  LCDDev.displayControl |= 0x04;
  writeCmd(0x08 | LCDDev.displayControl);
}


LCDCursorOff

decompiled_code:
__int64 LCDCursorOff()
{
byte_499 &= 0xFDu;
return writeCmd(byte_499 | 8u);
}

canonical_decompiled_code:
__int64 func10()
{
byte_499 &= 0xFDu;
return func3(byte_499 | 8u);
}

original_code:
void LCDCursorOff(void)
{
  LCDDev.displayControl &= ~0x02;
  writeCmd(0x08 | LCDDev.displayControl);
}

canonical_original_code:
void LCDCursorOff()
{
  LCDDev.displayControl &= ~0x02;
  writeCmd(0x08 | LCDDev.displayControl);
}


LCDCursorOn

decompiled_code:
__int64 LCDCursorOn()
{
char v0; // di

v0 = byte_499;
byte_499 |= 2u;
return writeCmd(v0 | 0xAu);
}

canonical_decompiled_code:
__int64 func11()
{
char v0; // di

v0 = byte_499;
byte_499 |= 2u;
return func3(v0 | 0xAu);
}

original_code:
void LCDCursorOn(void)
{
  LCDDev.displayControl |= 0x02;
  writeCmd(0x08 | LCDDev.displayControl);
}

canonical_original_code:
void LCDCursorOn()
{
  LCDDev.displayControl |= 0x02;
  writeCmd(0x08 | LCDDev.displayControl);
}


LCDBlinkOff

decompiled_code:
__int64 LCDBlinkOff()
{
byte_499 &= 0xFEu;
return writeCmd(byte_499 | 8u);
}

canonical_decompiled_code:
__int64 func12()
{
byte_499 &= 0xFEu;
return func3(byte_499 | 8u);
}

original_code:
void LCDBlinkOff(void)
{
  LCDDev.displayControl &= ~0x01;
  writeCmd(0x08 | LCDDev.displayControl);
}

canonical_original_code:
void LCDBlinkOff()
{
  LCDDev.displayControl &= ~0x01;
  writeCmd(0x08 | LCDDev.displayControl);
}


LCDBlinkOn

decompiled_code:
__int64 LCDBlinkOn()
{
char v0; // di

v0 = byte_499;
byte_499 |= 1u;
return writeCmd(v0 | 9u);
}

canonical_decompiled_code:
__int64 func13()
{
char v0; // di

v0 = byte_499;
byte_499 |= 1u;
return func3(v0 | 9u);
}

original_code:
void LCDBlinkOn(void)
{
  LCDDev.displayControl |= 0x01;
  writeCmd(0x08 | LCDDev.displayControl);
}

canonical_original_code:
void LCDBlinkOn()
{
  LCDDev.displayControl |= 0x01;
  writeCmd(0x08 | LCDDev.displayControl);
}


LCDScrollDisplayLeft

decompiled_code:
__int64 LCDScrollDisplayLeft()
{
return writeCmd(24);
}

canonical_decompiled_code:
__int64 func14()
{
return func3(24);
}

original_code:
void LCDScrollDisplayLeft(void)
{
  writeCmd(0x08 | 0x10 | 0x00);
}

canonical_original_code:
void LCDScrollDisplayLeft()
{
  writeCmd(0x08 | 0x10 | 0x00);
}


LCDScrollDisplayRight

decompiled_code:
__int64 LCDScrollDisplayRight()
{
return writeCmd(28);
}

canonical_decompiled_code:
__int64 func15()
{
return func3(28);
}

original_code:
void LCDScrollDisplayRight(void)
{
  writeCmd(0x08 | 0x10 | 0x04);
}

canonical_original_code:
void LCDScrollDisplayRight()
{
  writeCmd(0x08 | 0x10 | 0x04);
}


LCDAutoScrollOff

decompiled_code:
__int64 LCDAutoScrollOff()
{
byte_49A &= 0xFEu;
return writeCmd(byte_49A | 4u);
}

canonical_decompiled_code:
__int64 func16()
{
byte_49A &= 0xFEu;
return func3(byte_49A | 4u);
}

original_code:
void LCDAutoScrollOff(void)
{
  LCDDev.displayMode &= ~0x01;
  writeCmd(0x04 | LCDDev.displayMode);
}

canonical_original_code:
void LCDAutoScrollOff()
{
  LCDDev.displayMode &= ~0x01;
  writeCmd(0x04 | LCDDev.displayMode);
}


LCDAutoScrollOn

decompiled_code:
__int64 LCDAutoScrollOn()
{
char v0; // di

v0 = byte_49A;
byte_49A |= 1u;
return writeCmd(v0 | 5u);
}

canonical_decompiled_code:
__int64 func17()
{
char v0; // di

v0 = byte_49A;
byte_49A |= 1u;
return func3(v0 | 5u);
}

original_code:
void LCDAutoScrollOn(void)
{
  LCDDev.displayMode |= 0x01;
  writeCmd(0x04 | LCDDev.displayMode);
}

canonical_original_code:
void LCDAutoScrollOn()
{
  LCDDev.displayMode |= 0x01;
  writeCmd(0x04 | LCDDev.displayMode);
}


LCDBacklightOff

decompiled_code:
__int64 LCDBacklightOff()
{
byte_49D = 0;
return expandWrite(0);
}

canonical_decompiled_code:
__int64 func18()
{
byte_49D = 0;
return func0(0);
}

original_code:
void LCDBacklightOff(void)
{
  LCDDev.backLightEn=0x00;
  expandWrite(0);
}

canonical_original_code:
void LCDBacklightOff()
{
  LCDDev.backLightEn=0x00;
  expandWrite(0);
}


LCDBacklightOn

decompiled_code:
__int64 LCDBacklightOn()
{
byte_49D = 8;
return expandWrite(0);
}

canonical_decompiled_code:
__int64 func19()
{
byte_49D = 8;
return func0(0);
}

original_code:
void LCDBacklightOn(void)
{
  LCDDev.backLightEn=0x08;
  expandWrite(0);
}

canonical_original_code:
void LCDBacklightOn()
{
  LCDDev.backLightEn=0x08;
  expandWrite(0);
}


LCDCreateChar

decompiled_code:
__int64 __fastcall LCDCreateChar(char a1, char *a2)
{
char *v2; // rbx
__int64 result; // rax

writeCmd((8 * a1) & 0x38 | 0x40);
v2 = a2;
do
{
result = writeData(*v2);
v2 += 4;
}
while ( v2 != a2 + 32 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func20(char a1, char *a2)
{
char *v2; // rbx
__int64 result; // rax

func3((8 * a1) & 0x38 | 0x40);
v2 = a2;
do
{
result = func4(*v2);
v2 += 4;
}
while ( v2 != a2 + 32 );
return result;
}

original_code:
void LCDCreateChar(int location, int charMap[])
{
  unsigned char i;
  location &= 0x07;
  writeCmd(0x40 | location << 3);
  for (i=0; i<8; i++)
  {
    writeData(charMap[i]);
  }
}

variable_types: location int

variable_types:
charMap
int []

variable_types:
i
unsigned char


LCDShowChar

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

result = (unsigned __int8)byte_49B;
if ( (unsigned __int8)byte_49B > a2 )
{
result = (unsigned __int8)byte_49C;
if ( (unsigned __int8)byte_49C > a1 )
{
LCDSetCursor(a1, a2);
result = writeData(a3);
}
}
return result;
}

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

result = (unsigned __int8)byte_49B;
if ( (unsigned __int8)byte_49B > a2 )
{
result = (unsigned __int8)byte_49C;
if ( (unsigned __int8)byte_49C > a1 )
{
func7(a1, a2);
result = func4(a3);
}
}
return result;
}

original_code:
void LCDShowChar(int row, int col, char ch)
{
  if (col < LCDDev.numCols && row < LCDDev.numRows)
  {
    LCDSetCursor(row, col);
    writeData(ch);
  }
}


LCDPrintString

decompiled_code:
__int64 __fastcall LCDPrintString(int a1, int a2, char *a3)
{
__int64 result; // rax
int v4; // ebx
int v5; // ebp
char *v6; // r12
char v7; // dl

result = (unsigned __int8)byte_49B;
if ( (unsigned __int8)byte_49B > a2 )
{
v4 = a1;
v5 = a2;
v6 = a3;
result = (unsigned __int8)byte_49C;
if ( (unsigned __int8)byte_49C > a1 )
{
v7 = *a3;
if ( v7 )
{
do
{
LCDShowChar(v4, v5, v7);
++v6;
if ( (unsigned __int8)*(&LCDDev + 11) <= ++v5 )
{
++v4;
v5 = 0;
}
result = 0LL;
if ( v4 >= 2 )
v4 = 0;
v7 = *v6;
}
while ( *v6 );
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func22(int a1, int a2, char *a3)
{
__int64 result; // rax
int v4; // ebx
int v5; // ebp
char *v6; // r12
char v7; // dl

result = (unsigned __int8)byte_49B;
if ( (unsigned __int8)byte_49B > a2 )
{
v4 = a1;
v5 = a2;
v6 = a3;
result = (unsigned __int8)byte_49C;
if ( (unsigned __int8)byte_49C > a1 )
{
v7 = *a3;
if ( v7 )
{
do
{
func21(v4, v5, v7);
++v6;
if ( (unsigned __int8)*(&LCDDev + 11) <= ++v5 )
{
++v4;
v5 = 0;
}
result = 0LL;
if ( v4 >= 2 )
v4 = 0;
v7 = *v6;
}
while ( *v6 );
}
}
}
return result;
}

original_code:
void LCDPrintString(int row, int col, char *pStr)
{
  char *p;
  p = pStr;
  if (col < LCDDev.numCols && row < LCDDev.numRows)
  {
    while (*p)
    {
      LCDShowChar(row, col, *p);
      p++;
      col++;
      if (col >= LCDDev.numCols)
      {
       col=0;
       row++;
      }
      if (row>1)
      {
        row=0;
      }
    }
  }
}

variable_types:
pStr
char *

variable_types:
p
char *


LCDInit

decompiled_code:
int __fastcall LCDInit(unsigned __int8 a1, char a2, char a3)
{
int v4; // eax

LCDDev = a1;
v4 = wiringOliI2CSetup(0LL, a1);
if ( v4 < 0 )
return printf("Error initializing I2C bus");
dword_494 = v4;
byte_49C = a2;
byte_49B = a3;
delayMicroseconds(50000LL);
expandWrite(byte_49D);
byte_49D = 8;
byte_498 |= 8u;
delayMicroseconds(5000LL);
write4Bits(48);
delayMicroseconds(50000LL);
write4Bits(48);
delayMicroseconds(50000LL);
write4Bits(48);
delayMicroseconds(50000LL);
write4Bits(32);
delayMicroseconds(50000LL);
writeCmd(byte_498 | 0x20);
LCDDisplayOn();
LCDCursorOn();
LCDClear();
LCDAutoScrollOff();
return LCDHome();
}

canonical_decompiled_code:
int __fastcall func23(unsigned __int8 a1, char a2, char a3)
{
int v4; // eax

LCDDev = a1;
v4 = wiringOliI2CSetup(0LL, a1);
if ( v4 < 0 )
return printf("Error initializing I2C bus");
dword_494 = v4;
byte_49C = a2;
byte_49B = a3;
delayMicroseconds(50000LL);
func0(byte_49D);
byte_49D = 8;
byte_498 |= 8u;
delayMicroseconds(5000LL);
func2(48);
delayMicroseconds(50000LL);
func2(48);
delayMicroseconds(50000LL);
func2(48);
delayMicroseconds(50000LL);
func2(32);
delayMicroseconds(50000LL);
func3(byte_498 | 0x20);
func9();
func11();
func5();
func16();
return func6();
}

original_code:
void LCDInit(int address, int row, int col)
{
  int fd;
  LCDDev.devAddr=address;
  fd = wiringOliI2CSetup(0, LCDDev.devAddr);
  if (fd < 0)
  {
    printf("Error initializing I2C bus");
    return;
  }
  LCDDev.fd = fd;
  LCDDev.numRows=row;
  LCDDev.numCols=col;
  delayMicroseconds(50000);
  expandWrite(LCDDev.backLightEn);
  LCDDev.backLightEn=0x08;
  LCDDev.dispalyFunction |= 0x08;
  delayMicroseconds(5000);
  write4Bits(0x30);
  delayMicroseconds(50000);
  write4Bits(0x30);
  delayMicroseconds(50000);
  write4Bits(0x30);
  delayMicroseconds(50000);
  write4Bits(0x20);
  delayMicroseconds(50000);
  writeCmd(0x20 | LCDDev.dispalyFunction);
  LCDDisplayOn();
  LCDCursorOn();
  LCDClear();
  LCDAutoScrollOff();
  LCDHome();
}

variable_types: address int

variable_types: fd int


serialOpen

decompiled_code:
__int64 __fastcall serialOpen(const char *a1, int a2, int a3, int a4)
{
speed_t v6; // ebx
unsigned int v7; // ebp
int v8; // eax
tcflag_t v9; // edx
tcflag_t v10; // eax
int v11; // edx
int v12; // eax
unsigned int v13; // eax
int v15; // [rsp+Ch] [rbp-6Ch] BYREF
struct termios termios_p; // [rsp+10h] [rbp-68h] BYREF

if ( a2 == 1200 )
{
v6 = 9;
}
else if ( a2 > 1200 )
{
if ( a2 == 38400 )
{
v6 = 15;
}
else if ( a2 <= 38400 )
{
if ( a2 == 9600 )
{
v6 = 13;
}
else if ( a2 <= 9600 )
{
if ( a2 == 1800 )
{
v6 = 10;
}
else
{
if ( a2 != 2400 )
return (unsigned int)-2;
v6 = 11;
}
}
else
{
v6 = 14;
v7 = -2;
if ( a2 != 19200 )
return v7;
}
}
else
{
switch ( a2 )
{
case 115200:
v6 = 4098;
break;
case 230400:
v6 = 4099;
break;
case 57600:
v6 = 4097;
break;
default:
return (unsigned int)-2;
}
}
}
else if ( a2 == 150 )
{
v6 = 5;
}
else if ( a2 <= 150 )
{
if ( a2 == 110 )
{
v6 = 3;
}
else if ( a2 <= 110 )
{
if ( a2 == 50 )
{
v6 = 1;
}
else
{
if ( a2 != 75 )
return (unsigned int)-2;
v6 = 2;
}
}
else
{
v6 = 4;
v7 = -2;
if ( a2 != 134 )
return v7;
}
}
else
{
switch ( a2 )
{
case 300:
v6 = 7;
break;
case 600:
v6 = 8;
break;
case 200:
v6 = 6;
break;
default:
return (unsigned int)-2;
}
}
v8 = open(a1, 2306);
v7 = v8;
if ( v8 != -1 )
{
fcntl(v8, 4, 2LL);
tcgetattr(v7, &termios_p);
cfmakeraw(&termios_p);
cfsetispeed(&termios_p, v6);
cfsetospeed(&termios_p, v6);
v9 = termios_p.c_cflag;
v10 = termios_p.c_cflag;
BYTE1(v10) = BYTE1(termios_p.c_cflag) & 0xFE;
BYTE1(v9) = BYTE1(termios_p.c_cflag) & 0xFD;
v11 = v9 | 0x880;
v12 = v10 | 0x880;
if ( a4 == 1 )
v12 = v11;
v13 = v12 & 0xFFFFFF8F;
if ( a3 == 7 )
{
termios_p.c_cflag = v13 | 0x20;
goto LABEL_54;
}
if ( a3 > 7 )
{
if ( a3 == 8 )
{
termios_p.c_cflag = v13 | 0x30;
goto LABEL_54;
}
}
else
{
if ( a3 == 5 )
{
termios_p.c_cflag = v13;
goto LABEL_54;
}
if ( a3 == 6 )
{
termios_p.c_cflag = v13 | 0x10;
LABEL_54:
termios_p.c_lflag &= 0xFFFFFFE4;
termios_p.c_oflag &= 0xFFFFFFFE;
termios_p.c_cc[6] = 0;
termios_p.c_cc[5] = 100;
tcsetattr(v7, 2, &termios_p);
ioctl(v7, 0x5415uLL, &v15);
v15 |= 6u;
ioctl(v7, 0x5418uLL, &v15);
usleep(0x2710u);
return v7;
}
}
termios_p.c_cflag = v13 | 0x30;
goto LABEL_54;
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, int a2, int a3, int a4)
{
speed_t v6; // ebx
unsigned int v7; // ebp
int v8; // eax
tcflag_t v9; // edx
tcflag_t v10; // eax
int v11; // edx
int v12; // eax
unsigned int v13; // eax
int v15; // [rsp+Ch] [rbp-6Ch] BYREF
struct termios termios_p; // [rsp+10h] [rbp-68h] BYREF

if ( a2 == 1200 )
{
v6 = 9;
}
else if ( a2 > 1200 )
{
if ( a2 == 38400 )
{
v6 = 15;
}
else if ( a2 <= 38400 )
{
if ( a2 == 9600 )
{
v6 = 13;
}
else if ( a2 <= 9600 )
{
if ( a2 == 1800 )
{
v6 = 10;
}
else
{
if ( a2 != 2400 )
return (unsigned int)-2;
v6 = 11;
}
}
else
{
v6 = 14;
v7 = -2;
if ( a2 != 19200 )
return v7;
}
}
else
{
switch ( a2 )
{
case 115200:
v6 = 4098;
break;
case 230400:
v6 = 4099;
break;
case 57600:
v6 = 4097;
break;
default:
return (unsigned int)-2;
}
}
}
else if ( a2 == 150 )
{
v6 = 5;
}
else if ( a2 <= 150 )
{
if ( a2 == 110 )
{
v6 = 3;
}
else if ( a2 <= 110 )
{
if ( a2 == 50 )
{
v6 = 1;
}
else
{
if ( a2 != 75 )
return (unsigned int)-2;
v6 = 2;
}
}
else
{
v6 = 4;
v7 = -2;
if ( a2 != 134 )
return v7;
}
}
else
{
switch ( a2 )
{
case 300:
v6 = 7;
break;
case 600:
v6 = 8;
break;
case 200:
v6 = 6;
break;
default:
return (unsigned int)-2;
}
}
v8 = open(a1, 2306);
v7 = v8;
if ( v8 != -1 )
{
fcntl(v8, 4, 2LL);
tcgetattr(v7, &termios_p);
cfmakeraw(&termios_p);
cfsetispeed(&termios_p, v6);
cfsetospeed(&termios_p, v6);
v9 = termios_p.c_cflag;
v10 = termios_p.c_cflag;
BYTE1(v10) = BYTE1(termios_p.c_cflag) & 0xFE;
BYTE1(v9) = BYTE1(termios_p.c_cflag) & 0xFD;
v11 = v9 | 0x880;
v12 = v10 | 0x880;
if ( a4 == 1 )
v12 = v11;
v13 = v12 & 0xFFFFFF8F;
if ( a3 == 7 )
{
termios_p.c_cflag = v13 | 0x20;
goto LABEL_54;
}
if ( a3 > 7 )
{
if ( a3 == 8 )
{
termios_p.c_cflag = v13 | 0x30;
goto LABEL_54;
}
}
else
{
if ( a3 == 5 )
{
termios_p.c_cflag = v13;
goto LABEL_54;
}
if ( a3 == 6 )
{
termios_p.c_cflag = v13 | 0x10;
LABEL_54:
termios_p.c_lflag &= 0xFFFFFFE4;
termios_p.c_oflag &= 0xFFFFFFFE;
termios_p.c_cc[6] = 0;
termios_p.c_cc[5] = 100;
tcsetattr(v7, 2, &termios_p);
ioctl(v7, 0x5415uLL, &v15);
v15 |= 6u;
ioctl(v7, 0x5418uLL, &v15);
usleep(0x2710u);
return v7;
}
}
termios_p.c_cflag = v13 | 0x30;
goto LABEL_54;
}
return v7;
}

original_code:
int serialOpen(char *device, int baud, int numbit, int parity)
{
  struct termios options;
  speed_t myBaud;
  int status, fd;
  switch(baud)
  {
    case 50: myBaud = 0000001; break;
    case 75: myBaud = 0000002; break;
    case 110: myBaud = 0000003; break;
    case 134: myBaud = 0000004; break;
    case 150: myBaud = 0000005; break;
    case 200: myBaud = 0000006; break;
    case 300: myBaud = 0000007; break;
    case 600: myBaud = 0000010; break;
    case 1200: myBaud = 0000011; break;
    case 1800: myBaud = 0000012; break;
    case 2400: myBaud = 0000013; break;
    case 9600: myBaud = 0000015; break;
    case 19200: myBaud = 0000016; break;
    case 38400: myBaud = 0000017; break;
    case 57600: myBaud = 0010001; break;
    case 115200: myBaud = 0010002; break;
    case 230400: myBaud = 0010003; break;
    default:
      return -2;
  }
  if ((fd = open (device, 02 | 0400 | 04000 | 04000)) == -1)
    return -1;
  fcntl(fd, 4, 02);
  tcgetattr (fd, &options);
  cfmakeraw(&options);
  cfsetispeed (&options, myBaud);
  cfsetospeed (&options, myBaud);
  options.c_cflag |= (0004000 | 0000200);
  if (parity == 1)
  {
    options.c_cflag &= ~0001000;
  }
  else
  {
    options.c_cflag &= ~0000400;
  }
  options.c_cflag &= ~0000100;
  options.c_cflag &= ~0000060;
  switch(numbit)
  {
    case 5: options.c_cflag |= 0000000; break;
    case 6: options.c_cflag |= 0000020; break;
    case 7: options.c_cflag |= 0000040; break;
    case 8: options.c_cflag |= 0000060; break;
    default:
      options.c_cflag |= 0000060;
  }
  options.c_lflag &= ~(0000002 | 0000010 | 0000020 | 0000001);
  options.c_oflag &= ~0000001;
  options.c_cc [6] = 0;
  options.c_cc [5] = 100;
  tcsetattr(fd, 0 | 2, &options);
  ioctl(fd, 0x5415, &status);
  status |= 0x002;
  status |= 0x004;
  ioctl(fd, 0x5418, &status);
  usleep(10000);
  return fd;
}

canonical_original_code:
int serialOpen(char *device, int baud, int numbit, int parity)
{
  struct termios options;
  unsigned int myBaud;
  int status; int fd;
  switch(baud)
  {
    case 50: myBaud = 0000001; break;
    case 75: myBaud = 0000002; break;
    case 110: myBaud = 0000003; break;
    case 134: myBaud = 0000004; break;
    case 150: myBaud = 0000005; break;
    case 200: myBaud = 0000006; break;
    case 300: myBaud = 0000007; break;
    case 600: myBaud = 0000010; break;
    case 1200: myBaud = 0000011; break;
    case 1800: myBaud = 0000012; break;
    case 2400: myBaud = 0000013; break;
    case 9600: myBaud = 0000015; break;
    case 19200: myBaud = 0000016; break;
    case 38400: myBaud = 0000017; break;
    case 57600: myBaud = 0010001; break;
    case 115200: myBaud = 0010002; break;
    case 230400: myBaud = 0010003; break;
    default:
      return -2;
  }
  if ((fd = open (device, 02 | 0400 | 04000 | 04000)) == -1)
    return -1;
  fcntl(fd, 4, 02);
  tcgetattr (fd, &options);
  cfmakeraw(&options);
  cfsetispeed (&options, myBaud);
  cfsetospeed (&options, myBaud);
  options.c_cflag |= (0004000 | 0000200);
  if (parity == 1)
  {
    options.c_cflag &= ~0001000;
  }
  else
  {
    options.c_cflag &= ~0000400;
  }
  options.c_cflag &= ~0000100;
  options.c_cflag &= ~0000060;
  switch(numbit)
  {
    case 5: options.c_cflag |= 0000000; break;
    case 6: options.c_cflag |= 0000020; break;
    case 7: options.c_cflag |= 0000040; break;
    case 8: options.c_cflag |= 0000060; break;
    default:
      options.c_cflag |= 0000060;
  }
  options.c_lflag &= ~(0000002 | 0000010 | 0000020 | 0000001);
  options.c_oflag &= ~0000001;
  options.c_cc [6] = 0;
  options.c_cc [5] = 100;
  tcsetattr(fd, 0 | 2, &options);
  ioctl(fd, 0x5415, &status);
  status |= 0x002;
  status |= 0x004;
  ioctl(fd, 0x5418, &status);
  usleep(10000);
  return fd;
}

variable_types:
device
char *

variable_types: baud int

variable_types: numbit int

variable_types: parity int

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }

variable_types:
myBaud
unsigned int

variable_types: status int


serialFlush

decompiled_code:
int __fastcall serialFlush(int a1)
{
return tcflush(a1, 2);
}

canonical_decompiled_code:
int __fastcall func1(int a1)
{
return tcflush(a1, 2);
}

original_code:
void serialFlush(int fd)
{
  tcflush(fd, 2);
}


serialClose

decompiled_code:
int __fastcall serialClose(int a1)
{
return close(a1);
}

canonical_decompiled_code:
int __fastcall func2(int a1)
{
return close(a1);
}

original_code:
void serialClose(int fd)
{
  close(fd) ;
}


serialPutchar

decompiled_code:
ssize_t __fastcall serialPutchar(int a1, char a2)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a2;
return write(a1, buf, 1uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func3(int a1, char a2)
{
char buf[12]; // [rsp+Ch] [rbp-Ch] BYREF

buf[0] = a2;
return write(a1, buf, 1uLL);
}

original_code:
void serialPutchar(int fd, unsigned char c)
{
  write(fd, &c, 1);
}

variable_types:
c
unsigned char


serialPuts

decompiled_code:
ssize_t __fastcall serialPuts(int fd, char *s)
{
size_t v2; // rax

v2 = strlen(s);
return write(fd, s, v2);
}

canonical_decompiled_code:
ssize_t __fastcall func4(int fd, char *s)
{
size_t v2; // rax

v2 = strlen(s);
return write(fd, s, v2);
}

original_code:
void serialPuts(int fd, char *s)
{
  write(fd, s, strlen(s));
}

variable_types:
s
char *


serialPrintf

decompiled_code:
ssize_t serialPrintf(int fd, char *format, ...)
{
char v3[1032]; // [rsp+0h] [rbp-4E8h] BYREF
gcc_va_list arg; // [rsp+408h] [rbp-E0h] BYREF

va_start(arg, format);
vsnprintf(v3, 0x3FFuLL, format, arg);
return serialPuts(fd, v3);
}

canonical_decompiled_code:
ssize_t func5(int fd, char *format, ...)
{
char v3[1032]; // [rsp+0h] [rbp-4E8h] BYREF
gcc_va_list arg; // [rsp+408h] [rbp-E0h] BYREF

va_start(arg, format);
vsnprintf(v3, 0x3FFuLL, format, arg);
return func4(fd, v3);
}

original_code:
void serialPrintf (int fd, char *message, ...)
{
  va_list argp;
  char buffer[1024];
  __builtin_va_start(argp,message);
  vsnprintf(buffer, 1023, message, argp);
  __builtin_va_end(argp);
  serialPuts(fd, buffer);
}

canonical_original_code:
void serialPrintf(int fd, char *message, ...)
{
  __builtin_va_list argp;
  char buffer[1024];
  __builtin_va_start(argp,message);
  vsnprintf(buffer, 1023, message, argp);
  __builtin_va_end(argp);
  serialPuts(fd, buffer);
}

variable_types:
message
char *

variable_types:
buffer
char [1024]


serialDataAvail

decompiled_code:
int __fastcall serialDataAvail(int a1)
{
int result; // eax
int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

result = ioctl(a1, 0x541BuLL, v2);
if ( result != -1 )
result = v2[0];
return result;
}

canonical_decompiled_code:
int __fastcall func6(int a1)
{
int result; // eax
int v2[3]; // [rsp+Ch] [rbp-Ch] BYREF

result = ioctl(a1, 0x541BuLL, v2);
if ( result != -1 )
result = v2[0];
return result;
}

original_code:
int serialDataAvail(int fd)
{
  int result;
  if (ioctl(fd, 0x541B, &result) == -1)
    return -1;
  return result;
}

variable_types: result int


serialGetchar

decompiled_code:
__int64 __fastcall serialGetchar(int a1)
{
__int64 result; // rax
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

if ( read(a1, buf, 1uLL) == 1 )
result = (unsigned __int8)buf[0];
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(int a1)
{
__int64 result; // rax
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

if ( read(a1, buf, 1uLL) == 1 )
result = (unsigned __int8)buf[0];
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int serialGetchar(int fd)
{
  uint8_t x;
  if (read(fd, &x, 1) != 1)
    return -1;
  return((int)x) & 0xFF;
}

canonical_original_code:
int serialGetchar(int fd)
{
  unsigned char x;
  if (read(fd, &x, 1) != 1)
    return -1;
  return((int)x) & 0xFF;
}

variable_types:
x
unsigned char


softPwmThread

decompiled_code:
void __noreturn softPwmThread()
{
unsigned int v0; // er13
int v1; // ebp
int v2; // ebx

v0 = newPin;
newPin = -1;
oliHiPri(50LL);
while ( 1 )
{
v1 = marks[v0];
v2 = range[v0] - v1;
if ( v1 )
digitalWrite(v0, 1LL);
delayMicroseconds((unsigned int)(100 * v1));
if ( v2 )
digitalWrite(v0, 0LL);
delayMicroseconds((unsigned int)(100 * v2));
}
}

canonical_decompiled_code:
void __noreturn func0()
{
unsigned int v0; // er13
int v1; // ebp
int v2; // ebx

v0 = newPin;
newPin = -1;
oliHiPri(50LL);
while ( 1 )
{
v1 = marks[v0];
v2 = range[v0] - v1;
if ( v1 )
digitalWrite(v0, 1LL);
delayMicroseconds((unsigned int)(100 * v1));
if ( v2 )
digitalWrite(v0, 0LL);
delayMicroseconds((unsigned int)(100 * v2));
}
}

original_code:
static void *softPwmThread (void *dummy)
{
  int pin, mark, space;
  pin = newPin;
  newPin = -1;
  oliHiPri(50);
  for (;;)
  {
    mark = marks[pin];
    space = range[pin] - mark;
    if (mark != 0)
      digitalWrite(pin, 1);
    delayMicroseconds(mark * 100);
    if (space != 0)
      digitalWrite(pin, 0);
    delayMicroseconds(space * 100);
  }
  return ((void *)0);
}

canonical_original_code:
static void *softPwmThread(void *dummy)
{
  int pin; int mark; int space;
  pin = newPin;
  newPin = -1;
  oliHiPri(50);
  for (;;)
  {
    mark = marks[pin];
    space = range[pin] - mark;
    if (mark != 0)
      digitalWrite(pin, 1);
    delayMicroseconds(mark * 100);
    if (space != 0)
      digitalWrite(pin, 0);
    delayMicroseconds(space * 100);
  }
  return ((void *)0);
}

variable_types:
dummy
void *

variable_types: pin int

variable_types: mark int

variable_types: space int

return_type: void *


softPwmCreate

decompiled_code:
__int64 __fastcall softPwmCreate(__int64 a1, int a2, int a3)
{
unsigned int v4; // ebx

pinMode(a1, 1LL);
digitalWrite((unsigned int)a1, 0LL);
marks[(int)a1] = a2;
range[(int)a1] = a3;
newPin = a1;
v4 = oliThreadCreate(softPwmThread);
while ( newPin != -1 )
delay(1LL);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, int a2, int a3)
{
unsigned int v4; // ebx

pinMode(a1, 1LL);
digitalWrite((unsigned int)a1, 0LL);
marks[(int)a1] = a2;
range[(int)a1] = a3;
newPin = a1;
v4 = oliThreadCreate(softPwmThread);
while ( newPin != -1 )
delay(1LL);
return v4;
}

original_code:
int softPwmCreate(int pin, int initialValue, int pwmRange)
{
  int res;
  pinMode (pin, 1);
  digitalWrite(pin, 0);
  marks[pin] = initialValue;
  range[pin] = pwmRange;
  newPin = pin;
  res = oliThreadCreate(softPwmThread);
  while (newPin != -1)
    delay(1);
  return res;
}

variable_types: initialValue int

variable_types: pwmRange int

variable_types: res int


decompiled_code:
__int64 wiringOliI2CRead()
{
return i2c_smbus_read_byte();
}

canonical_decompiled_code:
__int64 func0()
{
return i2c_smbus_read_byte();
}

original_code:
int wiringOliI2CRead(int fd)
{
  return i2c_smbus_read_byte(fd);
}

variable_types: fd int


decompiled_code:
__int64 wiringOliI2CReadReg8()
{
return i2c_smbus_read_byte_data();
}

canonical_decompiled_code:
__int64 func1()
{
return i2c_smbus_read_byte_data();
}

original_code:
int wiringOliI2CReadReg8(int fd, int reg)
{
  return i2c_smbus_read_byte_data(fd, reg);
}

variable_types: reg int


decompiled_code:
__int64 wiringOliI2CReadReg16()
{
return i2c_smbus_read_word_data();
}

canonical_decompiled_code:
__int64 func2()
{
return i2c_smbus_read_word_data();
}

original_code:
int wiringOliI2CReadReg16(int fd, int reg)
{
  return i2c_smbus_read_word_data(fd, reg);
}


decompiled_code:
__int64 wiringOliI2CWrite()
{
return i2c_smbus_write_byte();
}

canonical_decompiled_code:
__int64 func3()
{
return i2c_smbus_write_byte();
}

original_code:
int wiringOliI2CWrite(int fd, int data)
{
  return i2c_smbus_write_byte(fd, data);
}

variable_types: data int


decompiled_code:
__int64 wiringOliI2CWriteReg8()
{
return i2c_smbus_write_byte_data();
}

canonical_decompiled_code:
__int64 func4()
{
return i2c_smbus_write_byte_data();
}

original_code:
int wiringOliI2CWriteReg8(int fd, int reg, int data)
{
  return i2c_smbus_write_byte_data(fd, reg, data);
}


decompiled_code:
__int64 wiringOliI2CWriteReg16()
{
return i2c_smbus_write_word_data();
}

canonical_decompiled_code:
__int64 func5()
{
return i2c_smbus_write_word_data();
}

original_code:
int wiringOliI2CWriteReg16(int fd, int reg, int data)
{
  return i2c_smbus_write_word_data(fd, reg, data);
}


decompiled_code:
__int64 __fastcall wiringOliI2CSetup(int a1, unsigned int a2)
{
int v2; // ebx

if ( a1 == 2 )
{
v2 = open("/dev/i2c-2", 2);
}
else if ( a1 > 2 )
{
v2 = -1;
if ( a1 != 3 )
return (unsigned int)v2;
v2 = open("/dev/i2c-3", 2);
}
else if ( a1 )
{
if ( a1 != 1 )
return (unsigned int)-1;
v2 = open("/dev/i2c-1", 2);
}
else
{
v2 = open("/dev/i2c-0", 2);
}
if ( v2 < 0 )
{
v2 = -1;
}
else if ( ioctl(v2, 0x706uLL, a2) < 0 )
{
v2 = -1;
}
return (unsigned int)v2;
}

canonical_decompiled_code:
__int64 __fastcall func6(int a1, unsigned int a2)
{
int v2; // ebx

if ( a1 == 2 )
{
v2 = open("/dev/i2c-2", 2);
}
else if ( a1 > 2 )
{
v2 = -1;
if ( a1 != 3 )
return (unsigned int)v2;
v2 = open("/dev/i2c-3", 2);
}
else if ( a1 )
{
if ( a1 != 1 )
return (unsigned int)-1;
v2 = open("/dev/i2c-1", 2);
}
else
{
v2 = open("/dev/i2c-0", 2);
}
if ( v2 < 0 )
{
v2 = -1;
}
else if ( ioctl(v2, 0x706uLL, a2) < 0 )
{
v2 = -1;
}
return (unsigned int)v2;
}

original_code:
int wiringOliI2CSetup(int device, int address)
{
  int fd=-1;
  switch(device)
  {
    case 0:
      fd = open("/dev/i2c-0", 02);
      break;
    case 1:
      fd = open("/dev/i2c-1", 02);
      break;
    case 2:
      fd = open("/dev/i2c-2", 02);
      break;
    case 3:
      fd = open("/dev/i2c-3", 02);
      break;
    default:
      break;
  }
  if (fd < 0)
    return -1;
  if (ioctl(fd, 0x0706, address) < 0)
    return -1;
  return fd;
}

canonical_original_code:
int wiringOliI2CSetup(int device, int address)
{
  int fd = -1;
  switch(device)
  {
    case 0:
      fd = open("/dev/i2c-0", 02);
      break;
    case 1:
      fd = open("/dev/i2c-1", 02);
      break;
    case 2:
      fd = open("/dev/i2c-2", 02);
      break;
    case 3:
      fd = open("/dev/i2c-3", 02);
      break;
    default:
      break;
  }
  if (fd < 0)
    return -1;
  if (ioctl(fd, 0x0706, address) < 0)
    return -1;
  return fd;
}

variable_types: device int

variable_types: address int


decompiled_code:
int __fastcall I2C_Close(int a1)
{
return close(a1);
}

canonical_decompiled_code:
int __fastcall func7(int a1)
{
return close(a1);
}

original_code:
void I2C_Close(int fd)
{
  if(close(fd) < 0)
  {
    return;
  }
}


decompiled_code:
ssize_t __fastcall I2C_Send(int a1, const void *a2, unsigned __int8 a3)
{
return write(a1, a2, a3);
}

canonical_decompiled_code:
ssize_t __fastcall func8(int a1, const void *a2, unsigned __int8 a3)
{
return write(a1, a2, a3);
}

original_code:
void I2C_Send(int fd, unsigned char *buffer, unsigned char num_bytes)
{
  int count = 0;
  count = write(fd, buffer, num_bytes);
  if(count != num_bytes)
  {
    return;
  }
}

variable_types:
buffer
unsigned char *

variable_types: count int


decompiled_code:
ssize_t __fastcall I2C_Read(int a1, void *a2, unsigned __int8 a3)
{
return read(a1, a2, a3);
}

canonical_decompiled_code:
ssize_t __fastcall func9(int a1, void *a2, unsigned __int8 a3)
{
return read(a1, a2, a3);
}

original_code:
void I2C_Read(int fd, unsigned char *buffer, unsigned char num_bytes)
{
  int count = 0;
  count = read(fd, buffer, num_bytes);
  if(count != num_bytes)
  {
    return;
  }
}


shiftIn

decompiled_code:
__int64 __fastcall shiftIn(unsigned __int8 a1, unsigned __int8 a2, char a3)
{
int v3; // ebx
unsigned int v4; // ebp
int i; // ebx

v3 = 0;
v4 = 0;
if ( a3 == 1 )
{
for ( i = 7; i != -1; --i )
{
digitalWrite(a2, 1LL);
v4 |= (unsigned int)digitalRead(a1) << i;
digitalWrite(a2, 0LL);
}
}
else
{
do
{
digitalWrite(a2, 1LL);
v4 |= (unsigned int)digitalRead(a1) << v3;
digitalWrite(a2, 0LL);
++v3;
}
while ( v3 != 8 );
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(unsigned __int8 a1, unsigned __int8 a2, char a3)
{
int v3; // ebx
unsigned int v4; // ebp
int i; // ebx

v3 = 0;
v4 = 0;
if ( a3 == 1 )
{
for ( i = 7; i != -1; --i )
{
digitalWrite(a2, 1LL);
v4 |= (unsigned int)digitalRead(a1) << i;
digitalWrite(a2, 0LL);
}
}
else
{
do
{
digitalWrite(a2, 1LL);
v4 |= (unsigned int)digitalRead(a1) << v3;
digitalWrite(a2, 0LL);
++v3;
}
while ( v3 != 8 );
}
return v4;
}

original_code:
uint8_t shiftIn(uint8_t dPin, uint8_t cPin, uint8_t order)
{
  uint8_t value = 0;
  int8_t i;
  if (order == 1)
    for (i = 7; i >= 0; --i)
    {
      digitalWrite(cPin, 1);
      value |= digitalRead(dPin) << i;
      digitalWrite(cPin, 0);
    }
  else
    for (i = 0; i < 8; ++i)
    {
      digitalWrite(cPin, 1);
      value |= digitalRead(dPin) << i;
      digitalWrite(cPin, 0);
    }
  return value;
}

canonical_original_code:
unsigned char shiftIn(unsigned char dPin, unsigned char cPin, unsigned char order)
{
  unsigned char value = 0;
  char i;
  if (order == 1)
    for (i = 7; i >= 0; --i)
    {
      digitalWrite(cPin, 1);
      value |= digitalRead(dPin) << i;
      digitalWrite(cPin, 0);
    }
  else
    for (i = 0; i < 8; ++i)
    {
      digitalWrite(cPin, 1);
      value |= digitalRead(dPin) << i;
      digitalWrite(cPin, 0);
    }
  return value;
}

variable_types:
dPin
unsigned char

variable_types:
cPin
unsigned char

variable_types:
order
unsigned char

variable_types:
value
unsigned char

variable_types: i char

return_type: unsigned char


shiftOut

decompiled_code:
__int64 __fastcall shiftOut(unsigned __int8 a1, unsigned __int8 a2, char a3, unsigned __int8 a4)
{
int v5; // ebp
__int64 result; // rax
int i; // ebp

v5 = 0;
if ( a3 == 1 )
{
for ( i = 7; i != -1; --i )
{
digitalWrite(a1, a4 & (unsigned int)(1 << i));
digitalWrite(a2, 1LL);
result = digitalWrite(a2, 0LL);
}
}
else
{
do
{
digitalWrite(a1, a4 & (unsigned int)(1 << v5));
digitalWrite(a2, 1LL);
result = digitalWrite(a2, 0LL);
++v5;
}
while ( v5 != 8 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int8 a1, unsigned __int8 a2, char a3, unsigned __int8 a4)
{
int v5; // ebp
__int64 result; // rax
int i; // ebp

v5 = 0;
if ( a3 == 1 )
{
for ( i = 7; i != -1; --i )
{
digitalWrite(a1, a4 & (unsigned int)(1 << i));
digitalWrite(a2, 1LL);
result = digitalWrite(a2, 0LL);
}
}
else
{
do
{
digitalWrite(a1, a4 & (unsigned int)(1 << v5));
digitalWrite(a2, 1LL);
result = digitalWrite(a2, 0LL);
++v5;
}
while ( v5 != 8 );
}
return result;
}

original_code:
void shiftOut(uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val)
{
  int8_t i;
  if (order == 1)
    for (i = 7; i >= 0; --i)
    {
      digitalWrite(dPin, val & (1 << i));
      digitalWrite(cPin, 1);
      digitalWrite(cPin, 0);
    }
  else
    for (i = 0; i < 8; ++i)
    {
      digitalWrite(dPin, val & (1 << i));
      digitalWrite(cPin, 1);
      digitalWrite(cPin, 0);
    }
}

canonical_original_code:
void shiftOut(unsigned char dPin, unsigned char cPin, unsigned char order, unsigned char val)
{
  char i;
  if (order == 1)
    for (i = 7; i >= 0; --i)
    {
      digitalWrite(dPin, val & (1 << i));
      digitalWrite(cPin, 1);
      digitalWrite(cPin, 0);
    }
  else
    for (i = 0; i < 8; ++i)
    {
      digitalWrite(dPin, val & (1 << i));
      digitalWrite(cPin, 1);
      digitalWrite(cPin, 0);
    }
}

variable_types:
val
unsigned char