decompiled_code:
int __fastcall simple_client(int a1, unsigned int a2, __int64 a3, int a4, int a5)
{
__int64 v6; // r13
int v7; // er12
int v8; // eax
int v9; // ecx
__int64 v10; // rax
__int64 v11; // r12
__int64 v12; // rdi
double v13; // xmm0_8
int v14; // er14
double v17; // [rsp+0h] [rbp-1D8h]
__int64 v19; // [rsp+10h] [rbp-1C8h] BYREF
__int64 v20; // [rsp+18h] [rbp-1C0h]
__int64 v21[16]; // [rsp+20h] [rbp-1B8h] BYREF
char v22[128]; // [rsp+A0h] [rbp-138h] BYREF
__int64 v23[23]; // [rsp+120h] [rbp-B8h] BYREF

v6 = host2ipaddr(a3);
memset(v21, 0, sizeof(v21));
memset(v22, 0, sizeof(v22));
memset(v23, 0, 0x80uLL);
v7 = 0;
if ( a1 > 0 )
{
do
{
v8 = create_tx_tcp(v6, (unsigned int)(v7 % a4 + a5));
v9 = v8;
if ( v8 < 0 )
{
puts("Unable to open connection!");
exit(-1);
}
if ( maxfd < v8 )
maxfd = v8;
++v7;
v10 = 40LL * v8;
state[v10 + 9] = a2;
*(_QWORD *)&state[v10 + 10] = 0LL;
v21[v9 >> 6] |= 1LL << v9;
}
while ( a1 != v7 );
}
LODWORD(v11) = -1;
v17 = 0.0;
do
{
do
{
send_request(v21, v22);
v12 = 0LL;
}
while ( (int)sink_data(v22, v23) <= 0 );
while ( 1 )
{
v14 = FD_FFSandC(v12, maxfd, v23);
if ( !v14 )
break;
v11 = (unsigned int)FD_POP(maxfd, v22);
tvsub(&v19, &state[40 * v14 + 30], &state[40 * v14 + 26]);
v13 = (double)(int)state[40 * v14 + 23] * 8.0 / ((double)(int)v19 * 1000000.0 + (double)(int)v20);
v17 = v17 + v13;
fprintf(
stderr,
"Finished with %d after %d bytes (%d pkts). %ld.%03lds = %.4f Mb/s. %d active\n",
(unsigned int)v14,
state[40 * v14 + 23],
state[40 * v14 + 22],
v19,
v20 / 1000,
v13 / scalar,
v11);
close(v14);
v12 = (unsigned int)(v14 + 1);
state[40 * v14 + 8] = 0;
v23[v14 / 64] &= __ROL8__(-2LL, v14 % 64);
}
}
while ( (_DWORD)v11 );
return fprintf(
stderr,
"Rough 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)
{
__int64 v6; // r13
int v7; // er12
int v8; // eax
int v9; // ecx
__int64 v10; // rax
__int64 v11; // r12
__int64 v12; // rdi
double v13; // xmm0_8
int v14; // er14
double v17; // [rsp+0h] [rbp-1D8h]
__int64 v19; // [rsp+10h] [rbp-1C8h] BYREF
__int64 v20; // [rsp+18h] [rbp-1C0h]
__int64 v21[16]; // [rsp+20h] [rbp-1B8h] BYREF
char v22[128]; // [rsp+A0h] [rbp-138h] BYREF
__int64 v23[23]; // [rsp+120h] [rbp-B8h] BYREF

v6 = host2ipaddr(a3);
memset(v21, 0, sizeof(v21));
memset(v22, 0, sizeof(v22));
memset(v23, 0, 0x80uLL);
v7 = 0;
if ( a1 > 0 )
{
do
{
v8 = create_tx_tcp(v6, (unsigned int)(v7 % a4 + a5));
v9 = v8;
if ( v8 < 0 )
{
puts("Unable to open connection!");
exit(-1);
}
if ( maxfd < v8 )
maxfd = v8;
++v7;
v10 = 40LL * v8;
state[v10 + 9] = a2;
*(_QWORD *)&state[v10 + 10] = 0LL;
v21[v9 >> 6] |= 1LL << v9;
}
while ( a1 != v7 );
}
LODWORD(v11) = -1;
v17 = 0.0;
do
{
do
{
send_request(v21, v22);
v12 = 0LL;
}
while ( (int)sink_data(v22, v23) <= 0 );
while ( 1 )
{
v14 = FD_FFSandC(v12, maxfd, v23);
if ( !v14 )
break;
v11 = (unsigned int)FD_POP(maxfd, v22);
tvsub(&v19, &state[40 * v14 + 30], &state[40 * v14 + 26]);
v13 = (double)(int)state[40 * v14 + 23] * 8.0 / ((double)(int)v19 * 1000000.0 + (double)(int)v20);
v17 = v17 + v13;
fprintf(
stderr,
"Finished with %d after %d bytes (%d pkts). %ld.%03lds = %.4f Mb/s. %d active\n",
(unsigned int)v14,
state[40 * v14 + 23],
state[40 * v14 + 22],
v19,
v20 / 1000,
v13 / scalar,
v11);
close(v14);
v12 = (unsigned int)(v14 + 1);
state[40 * v14 + 8] = 0;
v23[v14 / 64] &= __ROL8__(-2LL, v14 % 64);
}
}
while ( (_DWORD)v11 );
return fprintf(
stderr,
"Rough 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_client(int n, int bytes, char *host, int num_ports, int base_rx_port)
{
    int i, left;
    fd_set fds_new, 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_new)->__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);
    for(i=0; i<n; i++)
    {
        int fd = create_tx_tcp(ipaddr, base_rx_port+(i%num_ports));
        if(fd < 0)
        {
            printf("Unable to open connection!\n");
            exit(-1);
        }
        if(fd > maxfd) maxfd = fd;
        state[fd].tx_target = bytes;
        state[fd].tx_sent = 0;
        state[fd].tx_pkts = 0;
        ((void) (((&fds_new)->__fds_bits)[((fd) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((fd) % (8 * (int) sizeof (__fd_mask)))))));
    }
    left = -1;
    while(left)
    {
        int s, fd, rc;
        struct timeval tdiff;
        send_request(&fds_new, &fds_active);
        rc = sink_data(&fds_active, &fds_finished);
        for(s=0;
            (rc>0) && (fd = FD_FFSandC(s, maxfd, &fds_finished));
            s = fd+1)
        {
            double mbs;
            left = FD_POP(maxfd, &fds_active);
            tvsub(&tdiff, &state[fd].rx_stop, &state[fd].rx_start);
            mbs = (double)(8.0*state[fd].rx_rcvd) /
                (double)(tdiff.tv_sec*1.e6 + tdiff.tv_usec);
            tmbs += mbs;
            fprintf(stderr,
                    "Finished with %d after %d bytes (%d pkts). "
                    "%ld.%03lds = %.4f Mb/s. "
                    "%d active\n",
                    fd, state[fd].rx_rcvd, state[fd].rx_pkts,
                    (long int)tdiff.tv_sec, (long int)tdiff.tv_usec/1000,
                    mbs/scalar, left);
            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)))))));
        }
    }
    fprintf(stderr,
            "Rough total b/w estimate was %.2f Mb/s, "
            "Average stream b/w was %.4f Mb/s\n", tmbs, tmbs/n);
}

canonical_original_code:
void simple_client(int n, int bytes, char *host, int num_ports, int base_rx_port)
{
    int i; int left;
    struct fd_set fds_new; 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_new)->__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);
    for(i=0; i<n; i++)
    {
        int fd = create_tx_tcp(ipaddr, base_rx_port+(i%num_ports));
        if(fd < 0)
        {
            printf("Unable to open connection!\n");
            exit(-1);
        }
        if(fd > maxfd) maxfd = fd;
        state[fd].tx_target = bytes;
        state[fd].tx_sent = 0;
        state[fd].tx_pkts = 0;
        ((void) (((&fds_new)->__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 rc;
        struct timeval tdiff;
        send_request(&fds_new, &fds_active);
        rc = sink_data(&fds_active, &fds_finished);
        for(s=0;
            (rc>0) && (fd = FD_FFSandC(s, maxfd, &fds_finished));
            s = fd+1)
        {
            double mbs;
            left = FD_POP(maxfd, &fds_active);
            tvsub(&tdiff, &state[fd].rx_stop, &state[fd].rx_start);
            mbs = (double)(8.0*state[fd].rx_rcvd) /
                (double)(tdiff.tv_sec*1.e6 + tdiff.tv_usec);
            tmbs += mbs;
            fprintf(stderr,
                    "Finished with %d after %d bytes (%d pkts). "
                    "%ld.%03lds = %.4f Mb/s. "
                    "%d active\n",
                    fd, state[fd].rx_rcvd, state[fd].rx_pkts,
                    (long)tdiff.tv_sec, (long)tdiff.tv_usec/1000,
                    mbs/scalar, left);
            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)))))));
        }
    }
    fprintf(stderr,
            "Rough total b/w estimate was %.2f Mb/s, "
            "Average stream b/w was %.4f Mb/s\n", tmbs, tmbs/n);
}

variable_types: left int


decompiled_code:
void __fastcall __noreturn simple_server(unsigned int a1, unsigned int a2)
{
__int64 v2; // r12
__int64 v3; // rbp
unsigned int v4; // ebx
double v5; // xmm1_8
int v6; // eax
int v7; // eax
double v8; // xmm0_8
unsigned __int64 v9; // rdx
__int64 v10; // rdi
unsigned int v11; // er8
double v12; // xmm2_8
int v13; // ebx
unsigned int v14; // ebp
int *v15; // r12
double v16; // xmm0_8
unsigned int v17; // er10
unsigned int v18; // eax
double v19; // [rsp+0h] [rbp-218h]
int v20; // [rsp+8h] [rbp-210h]
unsigned int v21; // [rsp+Ch] [rbp-20Ch]
unsigned int v22; // [rsp+Ch] [rbp-20Ch]
unsigned int v23; // [rsp+10h] [rbp-208h]
unsigned int v24; // [rsp+10h] [rbp-208h]
unsigned int v25; // [rsp+14h] [rbp-204h]
unsigned int v26; // [rsp+20h] [rbp-1F8h]
unsigned int v27; // [rsp+20h] [rbp-1F8h]
__m128i v28; // [rsp+30h] [rbp-1E8h] BYREF
__int64 v29; // [rsp+40h] [rbp-1D8h] BYREF
__int64 v30; // [rsp+48h] [rbp-1D0h]
struct timeval tv; // [rsp+50h] [rbp-1C8h] BYREF
char v32[128]; // [rsp+60h] [rbp-1B8h] BYREF
char v33[128]; // [rsp+E0h] [rbp-138h] BYREF
char v34[184]; // [rsp+160h] [rbp-B8h] BYREF

tv.tv_sec = 0LL;
tv.tv_usec = 0LL;
memset(v32, 0, sizeof(v32));
memset(v33, 0, sizeof(v33));
memset(v34, 0, 0x80uLL);
v20 = create_listeners(v32, a1, a2);
if ( v20 < 0 )
exit(-1);
while ( 1 )
{
LODWORD(v2) = 0;
v3 = 0LL;
v4 = 0;
do
{
while ( 1 )
{
v6 = accept_incoming((unsigned int)v20, v32, v33);
if ( v6 > 0 )
v4 += v6;
v7 = send_data(v33, v34);
if ( v7 > 0 )
v3 = (unsigned int)(v3 + v7);
v2 = (unsigned int)(v2 + 1);
gettimeofday(&tv, 0LL);
tvsub(&v29, &tv, &v28);
v8 = (double)(int)v29 * 1000000.0 + (double)(int)v30;
if ( v8 >= 9.223372036854776e18 )
break;
v5 = (double)(int)v8;
if ( v5 > 5000000.0 )
goto LABEL_10;
}
v9 = (int)(v8 - 9.223372036854776e18) & 1 | (((unsigned int)(int)(v8 - 9.223372036854776e18) ^ 0x8000000000000000LL) >> 1);
v5 = (double)(int)v9 + (double)(int)v9;
}
while ( v5 <= 5000000.0 );
LABEL_10:
fwrite("\nBandwidth:\n", 1uLL, 0xCuLL, stderr);
v10 = maxfd;
if ( maxfd < v20 + 1 )
{
v12 = 0.0;
v17 = 0;
v11 = 0;
}
else
{
v11 = 0;
v21 = v4;
v12 = 0.0;
v13 = v20 + 1;
v23 = v3;
v14 = 0;
v25 = v2;
v15 = (int *)((char *)&state + 160 * v20 + 208);
do
{
v14 -= (*v15 == 0) - 1;
v11 += *v15;
v16 = (double)*v15 * 8.0 / v5;
v12 = v12 + v16;
if ( *(v15 - 4) )
{
v26 = v11;
fprintf(stderr, "%c%.4f ", 43LL, v16);
v10 = maxfd;
v11 = v26;
}
else if ( verbose )
{
v27 = v11;
fprintf(stderr, "%c%.4f ", 45LL, v16);
v10 = maxfd;
v11 = v27;
}
++v13;
*v15 = 0;
v15 += 40;
}
while ( (int)v10 >= v13 );
v17 = v14;
v4 = v21;
v2 = v25;
v3 = v23;
}
v24 = v11;
v22 = v17;
v19 = v12 / scalar;
v18 = FD_POP(v10, v33);
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",
v18,
v22,
v24,
v19,
v4,
v3,
v2);
v28 = _mm_loadu_si128((const __m128i *)&tv);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func0(unsigned int a1, unsigned int a2)
{
__int64 v2; // r12
__int64 v3; // rbp
unsigned int v4; // ebx
double v5; // xmm1_8
int v6; // eax
int v7; // eax
double v8; // xmm0_8
unsigned __int64 v9; // rdx
__int64 v10; // rdi
unsigned int v11; // er8
double v12; // xmm2_8
int v13; // ebx
unsigned int v14; // ebp
int *v15; // r12
double v16; // xmm0_8
unsigned int v17; // er10
unsigned int v18; // eax
double v19; // [rsp+0h] [rbp-218h]
int v20; // [rsp+8h] [rbp-210h]
unsigned int v21; // [rsp+Ch] [rbp-20Ch]
unsigned int v22; // [rsp+Ch] [rbp-20Ch]
unsigned int v23; // [rsp+10h] [rbp-208h]
unsigned int v24; // [rsp+10h] [rbp-208h]
unsigned int v25; // [rsp+14h] [rbp-204h]
unsigned int v26; // [rsp+20h] [rbp-1F8h]
unsigned int v27; // [rsp+20h] [rbp-1F8h]
__m128i v28; // [rsp+30h] [rbp-1E8h] BYREF
__int64 v29; // [rsp+40h] [rbp-1D8h] BYREF
__int64 v30; // [rsp+48h] [rbp-1D0h]
struct timeval tv; // [rsp+50h] [rbp-1C8h] BYREF
char v32[128]; // [rsp+60h] [rbp-1B8h] BYREF
char v33[128]; // [rsp+E0h] [rbp-138h] BYREF
char v34[184]; // [rsp+160h] [rbp-B8h] BYREF

tv.tv_sec = 0LL;
tv.tv_usec = 0LL;
memset(v32, 0, sizeof(v32));
memset(v33, 0, sizeof(v33));
memset(v34, 0, 0x80uLL);
v20 = create_listeners(v32, a1, a2);
if ( v20 < 0 )
exit(-1);
while ( 1 )
{
LODWORD(v2) = 0;
v3 = 0LL;
v4 = 0;
do
{
while ( 1 )
{
v6 = accept_incoming((unsigned int)v20, v32, v33);
if ( v6 > 0 )
v4 += v6;
v7 = send_data(v33, v34);
if ( v7 > 0 )
v3 = (unsigned int)(v3 + v7);
v2 = (unsigned int)(v2 + 1);
gettimeofday(&tv, 0LL);
tvsub(&v29, &tv, &v28);
v8 = (double)(int)v29 * 1000000.0 + (double)(int)v30;
if ( v8 >= 9.223372036854776e18 )
break;
v5 = (double)(int)v8;
if ( v5 > 5000000.0 )
goto LABEL_10;
}
v9 = (int)(v8 - 9.223372036854776e18) & 1 | (((unsigned int)(int)(v8 - 9.223372036854776e18) ^ 0x8000000000000000LL) >> 1);
v5 = (double)(int)v9 + (double)(int)v9;
}
while ( v5 <= 5000000.0 );
LABEL_10:
fwrite("\nBandwidth:\n", 1uLL, 0xCuLL, stderr);
v10 = maxfd;
if ( maxfd < v20 + 1 )
{
v12 = 0.0;
v17 = 0;
v11 = 0;
}
else
{
v11 = 0;
v21 = v4;
v12 = 0.0;
v13 = v20 + 1;
v23 = v3;
v14 = 0;
v25 = v2;
v15 = (int *)((char *)&state + 160 * v20 + 208);
do
{
v14 -= (*v15 == 0) - 1;
v11 += *v15;
v16 = (double)*v15 * 8.0 / v5;
v12 = v12 + v16;
if ( *(v15 - 4) )
{
v26 = v11;
fprintf(stderr, "%c%.4f ", 43LL, v16);
v10 = maxfd;
v11 = v26;
}
else if ( verbose )
{
v27 = v11;
fprintf(stderr, "%c%.4f ", 45LL, v16);
v10 = maxfd;
v11 = v27;
}
++v13;
*v15 = 0;
v15 += 40;
}
while ( (int)v10 >= v13 );
v17 = v14;
v4 = v21;
v2 = v25;
v3 = v23;
}
v24 = v11;
v22 = v17;
v19 = v12 / scalar;
v18 = FD_POP(v10, v33);
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",
v18,
v22,
v24,
v19,
v4,
v3,
v2);
v28 = _mm_loadu_si128((const __m128i *)&tv);
}
}

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: struct timeval { long tv_sec; long tv_usec; }

variable_types: opened int

variable_types: closed int

variable_types: cnt int

variable_types: rc int

variable_types: i int

variable_types: totb int

variable_types: prog int

variable_types: mbs double

variable_types: tmbs double

return_type: void

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


decompiled_code:
int __fastcall simple_tx(int a1, int a2, __int64 a3, int a4, int a5)
{
int i; // ebx
__int64 v6; // rdi
int v7; // er12
unsigned int v8; // ebx
unsigned int v9; // eax
__int64 j; // rdi
int v11; // ebp
__int64 v12; // rax
int v13; // er15
__int64 v14; // rdx
struct timeval *v15; // r12
int *v16; // rbp
double v17; // xmm1_8
int v18; // eax
double v19; // xmm1_8
double v21; // [rsp+8h] [rbp-170h]
double v22; // [rsp+10h] [rbp-168h]
__int64 v24; // [rsp+20h] [rbp-158h]
__int128 v27; // [rsp+30h] [rbp-148h] BYREF
__int64 v28[16]; // [rsp+40h] [rbp-138h] BYREF
__int64 v29[23]; // [rsp+C0h] [rbp-B8h] BYREF

v24 = host2ipaddr(a3);
memset(v28, 0, sizeof(v28));
memset(v29, 0, 0x80uLL);
if ( a1 > 0 )
{
for ( i = 0; i != a1; ++i )
{
v7 = create_tx_tcp(v24, (unsigned int)(i % a4 + a5));
if ( v7 < 0 )
fwrite("Unable to open connection!\n", 1uLL, 0x1BuLL, stderr);
if ( maxfd < v7 )
maxfd = v7;
v6 = 160LL * v7;
*(_DWORD *)((char *)&state + v6 + 36) = a2;
*(_QWORD *)((char *)&state + v6 + 40) = 0LL;
gettimeofday((struct timeval *)((char *)&state + v6 + 56), 0LL);
v28[v7 / 64] |= 1LL << (v7 % 64);
}
}
v21 = 0.0;
v8 = -1;
do
{
v9 = send_data(v28, v29);
if ( v9 )
fprintf(stderr, "%d finished\n", v9);
for ( j = 0LL; ; j = (unsigned int)(v13 + 1) )
{
v13 = FD_FFSandC(j, maxfd, v29);
if ( !v13 )
break;
v14 = 160LL * v13;
v15 = (struct timeval *)((char *)&state + v14 + 56);
v16 = (int *)((char *)&state + v14);
if ( *(_DWORD *)((char *)&state + v14 + 44) == *(_DWORD *)((char *)&state + v14 + 36) )
{
v8 = FD_POP(maxfd, v28);
tvsub(&v27, (char *)&state + 160 * v13 + 72, v15);
fprintf(stderr, "Finished with %d after %d bytes. ", (unsigned int)v13, (unsigned int)v16[11]);
v17 = 0.0;
if ( v27 != 0 )
{
v22 = (double)v16[11] * 8.0 / ((double)(int)v27 * 1000000.0 + (double)SDWORD2(v27));
fprintf(
stderr,
"%ld.%03lds => %.4f Mb/s. %d still active\n",
(_QWORD)v27,
*((_QWORD *)&v27 + 1) / 1000LL,
v22 / scalar,
v8);
v17 = v22;
}
v18 = v13 + 63;
v19 = v17 + v21;
if ( v13 >= 0 )
v18 = v13;
v21 = v19;
v29[v18 >> 6] &= __ROL8__(-2LL, v13 % 64);
}
else
{
v11 = create_tx_tcp(v24, (unsigned int)(v13 % a4 + a5));
if ( v11 < 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)v11);
if ( maxfd < v11 )
maxfd = v11;
v12 = 160LL * v11;
*(_DWORD *)((char *)&state + v12 + 36) = a2;
*(_QWORD *)((char *)&state + v12 + 40) = 0LL;
gettimeofday(v15, 0LL);
v28[v11 / 64] |= 1LL << (v11 % 64);
}
}
}
while ( v8 );
return fprintf(stderr, "Total b/w estimate was %.2f Mb/s, Average stream b/w was %.4f Mb/s\n", v21, v21 / (double)a1);
}

canonical_decompiled_code:
int __fastcall func0(int a1, int a2, __int64 a3, int a4, int a5)
{
int i; // ebx
__int64 v6; // rdi
int v7; // er12
unsigned int v8; // ebx
unsigned int v9; // eax
__int64 j; // rdi
int v11; // ebp
__int64 v12; // rax
int v13; // er15
__int64 v14; // rdx
struct timeval *v15; // r12
int *v16; // rbp
double v17; // xmm1_8
int v18; // eax
double v19; // xmm1_8
double v21; // [rsp+8h] [rbp-170h]
double v22; // [rsp+10h] [rbp-168h]
__int64 v24; // [rsp+20h] [rbp-158h]
__int128 v27; // [rsp+30h] [rbp-148h] BYREF
__int64 v28[16]; // [rsp+40h] [rbp-138h] BYREF
__int64 v29[23]; // [rsp+C0h] [rbp-B8h] BYREF

v24 = host2ipaddr(a3);
memset(v28, 0, sizeof(v28));
memset(v29, 0, 0x80uLL);
if ( a1 > 0 )
{
for ( i = 0; i != a1; ++i )
{
v7 = create_tx_tcp(v24, (unsigned int)(i % a4 + a5));
if ( v7 < 0 )
fwrite("Unable to open connection!\n", 1uLL, 0x1BuLL, stderr);
if ( maxfd < v7 )
maxfd = v7;
v6 = 160LL * v7;
*(_DWORD *)((char *)&state + v6 + 36) = a2;
*(_QWORD *)((char *)&state + v6 + 40) = 0LL;
gettimeofday((struct timeval *)((char *)&state + v6 + 56), 0LL);
v28[v7 / 64] |= 1LL << (v7 % 64);
}
}
v21 = 0.0;
v8 = -1;
do
{
v9 = send_data(v28, v29);
if ( v9 )
fprintf(stderr, "%d finished\n", v9);
for ( j = 0LL; ; j = (unsigned int)(v13 + 1) )
{
v13 = FD_FFSandC(j, maxfd, v29);
if ( !v13 )
break;
v14 = 160LL * v13;
v15 = (struct timeval *)((char *)&state + v14 + 56);
v16 = (int *)((char *)&state + v14);
if ( *(_DWORD *)((char *)&state + v14 + 44) == *(_DWORD *)((char *)&state + v14 + 36) )
{
v8 = FD_POP(maxfd, v28);
tvsub(&v27, (char *)&state + 160 * v13 + 72, v15);
fprintf(stderr, "Finished with %d after %d bytes. ", (unsigned int)v13, (unsigned int)v16[11]);
v17 = 0.0;
if ( v27 != 0 )
{
v22 = (double)v16[11] * 8.0 / ((double)(int)v27 * 1000000.0 + (double)SDWORD2(v27));
fprintf(
stderr,
"%ld.%03lds => %.4f Mb/s. %d still active\n",
(_QWORD)v27,
*((_QWORD *)&v27 + 1) / 1000LL,
v22 / scalar,
v8);
v17 = v22;
}
v18 = v13 + 63;
v19 = v17 + v21;
if ( v13 >= 0 )
v18 = v13;
v21 = v19;
v29[v18 >> 6] &= __ROL8__(-2LL, v13 % 64);
}
else
{
v11 = create_tx_tcp(v24, (unsigned int)(v13 % a4 + a5));
if ( v11 < 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)v11);
if ( maxfd < v11 )
maxfd = v11;
v12 = 160LL * v11;
*(_DWORD *)((char *)&state + v12 + 36) = a2;
*(_QWORD *)((char *)&state + v12 + 40) = 0LL;
gettimeofday(v15, 0LL);
v28[v11 / 64] |= 1LL << (v11 % 64);
}
}
}
while ( v8 );
return fprintf(stderr, "Total b/w estimate was %.2f Mb/s, Average stream b/w was %.4f Mb/s\n", v21, v21 / (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:
ipaddr
unsigned long

variable_types: fd int

variable_types: s int

variable_types: fin int

variable_types: newfd int

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


decompiled_code:
void __fastcall __noreturn simple_rx(unsigned int a1, unsigned int a2)
{
unsigned int v2; // er13
int v3; // eax
__int64 v4; // rdi
int v5; // er12
int v6; // eax
int v7; // er14
double v8; // xmm0_8
double v9; // xmm1_8
__int64 v10; // rdi
int *v11; // r12
unsigned int v12; // er8
double v13; // xmm2_8
unsigned int v14; // er13
int v15; // er14
double v16; // xmm0_8
unsigned int v17; // ecx
unsigned int v18; // eax
unsigned __int64 v19; // rdx
unsigned int v20; // [rsp+0h] [rbp-218h]
int v21; // [rsp+4h] [rbp-214h]
double v22; // [rsp+8h] [rbp-210h]
unsigned int v23; // [rsp+10h] [rbp-208h]
unsigned int v24; // [rsp+14h] [rbp-204h]
unsigned int v25; // [rsp+14h] [rbp-204h]
unsigned int v26; // [rsp+20h] [rbp-1F8h]
unsigned int v27; // [rsp+20h] [rbp-1F8h]
unsigned int v28; // [rsp+20h] [rbp-1F8h]
__m128i v29; // [rsp+30h] [rbp-1E8h] BYREF
__int64 v30; // [rsp+40h] [rbp-1D8h] BYREF
__int64 v31; // [rsp+48h] [rbp-1D0h]
struct timeval tv; // [rsp+50h] [rbp-1C8h] BYREF
char v33[128]; // [rsp+60h] [rbp-1B8h] BYREF
char v34[128]; // [rsp+E0h] [rbp-138h] BYREF
__int64 v35[23]; // [rsp+160h] [rbp-B8h] BYREF

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

canonical_decompiled_code:
void __fastcall __noreturn func0(unsigned int a1, unsigned int a2)
{
unsigned int v2; // er13
int v3; // eax
__int64 v4; // rdi
int v5; // er12
int v6; // eax
int v7; // er14
double v8; // xmm0_8
double v9; // xmm1_8
__int64 v10; // rdi
int *v11; // r12
unsigned int v12; // er8
double v13; // xmm2_8
unsigned int v14; // er13
int v15; // er14
double v16; // xmm0_8
unsigned int v17; // ecx
unsigned int v18; // eax
unsigned __int64 v19; // rdx
unsigned int v20; // [rsp+0h] [rbp-218h]
int v21; // [rsp+4h] [rbp-214h]
double v22; // [rsp+8h] [rbp-210h]
unsigned int v23; // [rsp+10h] [rbp-208h]
unsigned int v24; // [rsp+14h] [rbp-204h]
unsigned int v25; // [rsp+14h] [rbp-204h]
unsigned int v26; // [rsp+20h] [rbp-1F8h]
unsigned int v27; // [rsp+20h] [rbp-1F8h]
unsigned int v28; // [rsp+20h] [rbp-1F8h]
__m128i v29; // [rsp+30h] [rbp-1E8h] BYREF
__int64 v30; // [rsp+40h] [rbp-1D8h] BYREF
__int64 v31; // [rsp+48h] [rbp-1D0h]
struct timeval tv; // [rsp+50h] [rbp-1C8h] BYREF
char v33[128]; // [rsp+60h] [rbp-1B8h] BYREF
char v34[128]; // [rsp+E0h] [rbp-138h] BYREF
__int64 v35[23]; // [rsp+160h] [rbp-B8h] BYREF

tv.tv_sec = 0LL;
tv.tv_usec = 0LL;
memset(v33, 0, sizeof(v33));
memset(v34, 0, sizeof(v34));
memset(v35, 0, 0x80uLL);
v21 = create_listeners(v33, a1, a2);
if ( v21 >= 0 )
{
while ( 1 )
{
v20 = 0;
v2 = 0;
v23 = 0;
do
{
v3 = accept_incoming((unsigned int)v21, v33, v34);
if ( v3 > 0 )
v2 += v3;
++v20;
v4 = 0LL;
v5 = sink_data(v34, v35);
if ( v5 > 0 )
{
while ( 1 )
{
v6 = FD_FFSandC(v4, maxfd, v35);
v7 = v6;
if ( !v6 )
break;
close(v6);
v4 = (unsigned int)(v7 + 1);
state[40 * v7 + 8] = 0;
v35[v7 / 64] &= ~(1LL << (v7 % 64));
}
v23 += v5;
}
gettimeofday(&tv, 0LL);
tvsub(&v30, &tv, &v29);
v8 = (double)(int)v30 * 1000000.0 + (double)(int)v31;
if ( v8 >= 9.223372036854776e18 )
{
v19 = (int)(v8 - 9.223372036854776e18) & 1 | (((unsigned int)(int)(v8 - 9.223372036854776e18) ^ 0x8000000000000000LL) >> 1);
v9 = (double)(int)v19 + (double)(int)v19;
}
else
{
v9 = (double)(int)v8;
}
}
while ( v9 <= 5000000.0 );
fwrite("\nBandwidth:\n", 1uLL, 0xCuLL, stderr);
v10 = maxfd;
if ( maxfd < v21 + 1 )
{
v13 = 0.0;
v17 = 0;
v12 = 0;
}
else
{
v11 = &state[40 * v21 + 64];
v12 = 0;
v24 = v2;
v13 = 0.0;
v14 = 0;
v15 = v21 + 1;
do
{
v14 -= (*v11 == 0) - 1;
v12 += *v11;
v16 = (double)*v11 * 8.0 / v9;
v13 = v13 + v16;
if ( *(v11 - 16) )
{
v27 = v12;
fprintf(stderr, "%c%.4f ", 43LL, v16);
v10 = maxfd;
v12 = v27;
}
else if ( verbose )
{
v28 = v12;
fprintf(stderr, "%c%.4f ", 45LL, v16);
v10 = maxfd;
v12 = v28;
}
++v15;
*v11 = 0;
v11 += 40;
}
while ( (int)v10 >= v15 );
v17 = v14;
v2 = v24;
}
v26 = v12;
v25 = v17;
v22 = v13 / scalar;
v18 = FD_POP(v10, v34);
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",
v18,
v25,
v26,
v22,
v2,
v23,
v20);
v29 = _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: struct timeval { long tv_sec; long tv_usec; }

variable_types: opened int


main

canonical_name: main

repo: morettoni/zonenotify

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

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

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

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

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

return_type: int


usage

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; // er12
__int64 v2; // rax
socklen_t v3; // edx
int v4; // eax
struct hostent *v5; // rax
socklen_t v6; // edx
time_t timer; // [rsp+8h] [rbp-40h] BYREF
struct sockaddr v9; // [rsp+10h] [rbp-38h] BYREF
struct sockaddr v10; // [rsp+20h] [rbp-28h] BYREF

time(&timer);
srandom(timer);
s = socket(2, 2, 0);
if ( s < 0 )
return 0xFFFFFFFFLL;
v1 = 0;
v10 = 0LL;
v10.sa_family = 2;
*(_DWORD *)&v10.sa_data[2] = _bswap_32_constprop_0();
while ( 1 )
{
v2 = random();
++v1;
*(_WORD *)v10.sa_data = _bswap_16(v2 % 15000 + 1025);
v4 = bind(s, &v10, v3);
if ( v1 > 11 )
break;
if ( !v4 )
goto LABEL_5;
}
if ( v4 )
return 0xFFFFFFFFLL;
LABEL_5:
v9 = 0LL;
if ( !inet_aton(name, (struct in_addr *)&v9.sa_data[2]) )
{
v5 = gethostbyname(name);
if ( !v5 )
return 0xFFFFFFFFLL;
*(_DWORD *)&v9.sa_data[2] = **(_DWORD **)v5->h_addr_list;
}
v9.sa_family = 2;
*(_WORD *)v9.sa_data = _bswap_16(53);
return (unsigned int)-(connect(s, &v9, v6) != 0);
}

canonical_decompiled_code:
__int64 __fastcall func10(char *name)
{
int v1; // er12
__int64 v2; // rax
socklen_t v3; // edx
int v4; // eax
struct hostent *v5; // rax
socklen_t v6; // edx
time_t timer; // [rsp+8h] [rbp-40h] BYREF
struct sockaddr v9; // [rsp+10h] [rbp-38h] BYREF
struct sockaddr v10; // [rsp+20h] [rbp-28h] BYREF

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

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

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 v3; // er12
char *v5; // rsi
int v6; // er14
char *v7; // rax
int v8; // eax
__int64 result; // rax
unsigned int v10; // er12
char *v11; // rbx

v3 = 0;
while ( 1 )
{
v7 = strchr(src, 46);
if ( !v7 )
break;
v5 = src;
v6 = (_DWORD)v7 - (_DWORD)src;
src = v7 + 1;
*a2 = v6;
v3 += v6 + 1;
a2 = &strncpy(a2 + 1, v5, v6)[v6];
}
v8 = strlen(src);
if ( v8 )
{
*a2 = v8;
v10 = v3 + v8 + 1;
v11 = &strncpy(a2 + 1, src, v8)[v8];
result = v10;
*v11 = 0;
}
else
{
*a2 = 0;
result = v3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(char *src, char *a2)
{
unsigned int v3; // er12
char *v5; // rsi
int v6; // er14
char *v7; // rax
int v8; // eax
__int64 result; // rax
unsigned int v10; // er12
char *v11; // rbx

v3 = 0;
while ( 1 )
{
v7 = strchr(src, 46);
if ( !v7 )
break;
v5 = src;
v6 = (_DWORD)v7 - (_DWORD)src;
src = v7 + 1;
*a2 = v6;
v3 += v6 + 1;
a2 = &strncpy(a2 + 1, v5, v6)[v6];
}
v8 = strlen(src);
if ( v8 )
{
*a2 = v8;
v10 = v3 + v8 + 1;
v11 = &strncpy(a2 + 1, src, v8)[v8];
result = v10;
*v11 = 0;
}
else
{
*a2 = 0;
result = v3;
}
return result;
}

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(__int64 a1, const char *a2)
{
__int16 v2; // bp
__int16 v3; // di
char *v4; // r8
size_t v5; // r13
__int64 v6; // rdx
__int64 v7; // rcx
int v8; // edi
int v9; // er8
__int64 result; // rax
struct timeval v11; // [rsp+0h] [rbp-6C8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-6B8h] BYREF
__int16 buf; // [rsp+90h] [rbp-638h] BYREF
__int64 v14; // [rsp+92h] [rbp-636h]
__int16 v15; // [rsp+9Ah] [rbp-62Eh]
__int16 dest[250]; // [rsp+9Ch] [rbp-62Ch] BYREF
char src[1080]; // [rsp+290h] [rbp-438h] BYREF

WORD1(v14) = _bswap_16(1);
v2 = WORD1(v14);
v14 = (unsigned int)v14 & 0xFFFF3000 | 0x24LL;
v15 = 0;
v3 = unique_0++;
buf = _bswap_16(v3);
v5 = (int)(ns_encode(v4, src) + 1);
memcpy(dest, src, v5);
v11.tv_sec = 120LL;
*(__int16 *)((char *)dest + v5) = _bswap_16(6);
*(__int16 *)((char *)&dest[1] + v6 + 1) = v2;
v11.tv_usec = 0LL;
memset(&readfds, 0, 8 * v7);
v8 = s;
readfds.fds_bits[s / 64] |= 1LL << (s % 64);
if ( send(v8, &buf, v6 + 17, 0) == -1 )
return 0xFFFFFFFFLL;
if ( select(1024, &readfds, 0LL, 0LL, &v11) <= 0 )
{
fprintf(stderr, "%s: timeout\n", a2);
result = 0xFFFFFFFFLL;
}
else
{
v9 = recv(s, &buf, 0x200uLL, 0);
result = 0LL;
if ( v9 != -1 && (v14 & 0x80u) != 0LL && (v14 & 0xF00) != 0 )
{
fprintf(stderr, "%s: %s\n", a2, dns_errors[BYTE1(v14) & 0xF]);
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func13(__int64 a1, const char *a2)
{
__int16 v2; // bp
__int16 v3; // di
char *v4; // r8
size_t v5; // r13
__int64 v6; // rdx
__int64 v7; // rcx
int v8; // edi
int v9; // er8
__int64 result; // rax
struct timeval v11; // [rsp+0h] [rbp-6C8h] BYREF
fd_set readfds; // [rsp+10h] [rbp-6B8h] BYREF
__int16 buf; // [rsp+90h] [rbp-638h] BYREF
__int64 v14; // [rsp+92h] [rbp-636h]
__int16 v15; // [rsp+9Ah] [rbp-62Eh]
__int16 dest[250]; // [rsp+9Ch] [rbp-62Ch] BYREF
char src[1080]; // [rsp+290h] [rbp-438h] BYREF

WORD1(v14) = func7(1);
v2 = WORD1(v14);
v14 = (unsigned int)v14 & 0xFFFF3000 | 0x24LL;
v15 = 0;
v3 = unique_0++;
buf = func7(v3);
v5 = (int)(func12(v4, src) + 1);
memcpy(dest, src, v5);
v11.tv_sec = 120LL;
*(__int16 *)((char *)dest + v5) = func7(6);
*(__int16 *)((char *)&dest[1] + v6 + 1) = v2;
v11.tv_usec = 0LL;
memset(&readfds, 0, 8 * v7);
v8 = s;
readfds.fds_bits[s / 64] |= 1LL << (s % 64);
if ( send(v8, &buf, v6 + 17, 0) == -1 )
return 0xFFFFFFFFLL;
if ( select(1024, &readfds, 0LL, 0LL, &v11) <= 0 )
{
fprintf(stderr, "%s: timeout\n", a2);
result = 0xFFFFFFFFLL;
}
else
{
v9 = recv(s, &buf, 0x200uLL, 0);
result = 0LL;
if ( v9 != -1 && (v14 & 0x80u) != 0LL && (v14 & 0xF00) != 0 )
{
fprintf(stderr, "%s: %s\n", a2, dns_errors[BYTE1(v14) & 0xF]);
result = 0xFFFFFFFFLL;
}
}
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; }


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

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

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

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

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; // er12
int v2; // eax
__int64 result; // rax
char v4[88]; // [rsp+0h] [rbp-58h] BYREF

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

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

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

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; // er12
__int64 result; // rax
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");
result = v3;
}
else
{
if ( a2 )
write(v2, "out", 4uLL);
else
write(v2, "in", 3uLL);
close(v3);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, int a2)
{
int v2; // eax
unsigned int v3; // er12
__int64 result; // rax
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");
result = v3;
}
else
{
if ( a2 )
write(v2, "out", 4uLL);
else
write(v2, "in", 3uLL);
close(v3);
result = 0LL;
}
return result;
}

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; // er12
__int64 result; // rax
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");
result = v3;
}
else
{
if ( a2 )
write(v2, "1", 2uLL);
else
write(v2, "0", 2uLL);
close(v3);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1, int a2)
{
int v2; // eax
unsigned int v3; // er12
__int64 result; // rax
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");
result = v3;
}
else
{
if ( a2 )
write(v2, "1", 2uLL);
else
write(v2, "0", 2uLL);
close(v3);
result = 0LL;
}
return result;
}

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; // er12
__int64 result; // rax
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");
result = v3;
}
else
{
read(v2, &buf, 1uLL);
*a2 = buf != 48;
close(v3);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1, _DWORD *a2)
{
int v2; // eax
unsigned int v3; // er12
__int64 result; // rax
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");
result = v3;
}
else
{
read(v2, &buf, 1uLL);
*a2 = buf != 48;
close(v3);
result = 0LL;
}
return result;
}

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: ch char


decompiled_code:
__int64 __fastcall gpio_set_edge(unsigned int a1, const char *a2)
{
int v2; // er12
size_t v3; // rax
__int64 result; // 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");
result = (unsigned int)v2;
}
else
{
v3 = strlen(a2);
write(v2, a2, v3 + 1);
close(v2);
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1, const char *a2)
{
int v2; // er12
size_t v3; // rax
__int64 result; // 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");
result = (unsigned int)v2;
}
else
{
v3 = strlen(a2);
write(v2, a2, v3 + 1);
close(v2);
result = 0LL;
}
return result;
}

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:
int __fastcall gpio_fd_open(unsigned int a1)
{
int result; // eax
int v2; // [rsp+Ch] [rbp-4Ch]
char s[72]; // [rsp+10h] [rbp-48h] BYREF

snprintf(s, 0x40uLL, "/sys/class/gpio/gpio%d/value", a1);
result = open(s, 2048);
if ( result < 0 )
{
v2 = result;
perror("gpio/fd_open");
result = v2;
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(unsigned int a1)
{
int result; // eax
int v2; // [rsp+Ch] [rbp-4Ch]
char s[72]; // [rsp+10h] [rbp-48h] BYREF

snprintf(s, 0x40uLL, "/sys/class/gpio/gpio%d/value", a1);
result = open(s, 2048);
if ( result < 0 )
{
v2 = result;
perror("gpio/fd_open");
result = v2;
}
return result;
}

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:
// attributes: thunk
int gpio_fd_close(int fd)
{
return close(fd);
}

canonical_decompiled_code:
int func7(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+0h] [rbp-Ch]

result = (unsigned __int8)byte_49B;
v3[0] = 1410613248;
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+0h] [rbp-Ch]

result = (unsigned __int8)byte_49B;
v3[0] = 1410613248;
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 | 0xC;
byte_499 |= 4u;
return writeCmd(v0);
}

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

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

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 | 0xA;
byte_499 |= 2u;
return writeCmd(v0);
}

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

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

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 | 9;
byte_499 |= 1u;
return writeCmd(v0);
}

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 | 5;
byte_49A |= 1u;
return writeCmd(v0);
}

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

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

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
char v3; // di
__int64 result; // rax

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

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

v2 = a2;
func3((8 * a1) & 0x38 | 0x40);
do
{
v3 = *v2;
v2 += 4;
result = func4(v3);
}
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
char *v5; // r12
char v6; // dl
int v7; // ebp
int v8; // esi

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

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

result = (unsigned __int8)byte_49B;
if ( (unsigned __int8)byte_49B > a2 )
{
v4 = a1;
result = (unsigned __int8)byte_49C;
if ( (unsigned __int8)byte_49C > a1 )
{
v5 = a3;
v6 = *a3;
if ( v6 )
{
v7 = a2;
do
{
v8 = v7++;
++v5;
func21(v4, v8, v6);
result = (unsigned __int8)byte_49B;
if ( (unsigned __int8)byte_49B <= v7 )
{
++v4;
v7 = 0;
}
v6 = *v5;
if ( v4 >= 2 )
v4 = 0;
}
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_498 |= 8u;
byte_49D = 8;
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_498 |= 8u;
byte_49D = 8;
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


serialOpen

decompiled_code:
__int64 __fastcall serialOpen(const char *a1, int a2, int a3, int a4)
{
speed_t v6; // ebp
int v7; // eax
unsigned int v8; // er13
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;
goto LABEL_8;
}
if ( a2 > 1200 )
{
if ( a2 == 38400 )
{
v6 = 15;
goto LABEL_8;
}
if ( a2 <= 38400 )
{
if ( a2 == 9600 )
{
v6 = 13;
goto LABEL_8;
}
if ( a2 <= 9600 )
{
if ( a2 == 1800 )
{
v6 = 10;
goto LABEL_8;
}
if ( a2 == 2400 )
{
v6 = 11;
goto LABEL_8;
}
}
else
{
v6 = 14;
if ( a2 == 19200 )
goto LABEL_8;
}
}
else
{
switch ( a2 )
{
case 115200:
v6 = 4098;
goto LABEL_8;
case 230400:
v6 = 4099;
goto LABEL_8;
case 57600:
v6 = 4097;
goto LABEL_8;
}
}
return 4294967294LL;
}
if ( a2 == 150 )
{
v6 = 5;
goto LABEL_8;
}
if ( a2 <= 150 )
{
if ( a2 == 110 )
{
v6 = 3;
goto LABEL_8;
}
if ( a2 <= 110 )
{
if ( a2 == 50 )
{
v6 = 1;
goto LABEL_8;
}
if ( a2 == 75 )
{
v6 = 2;
goto LABEL_8;
}
}
else
{
v6 = 4;
if ( a2 == 134 )
goto LABEL_8;
}
return 4294967294LL;
}
if ( a2 == 300 )
{
v6 = 7;
goto LABEL_8;
}
if ( a2 != 600 )
{
if ( a2 == 200 )
{
v6 = 6;
goto LABEL_8;
}
return 4294967294LL;
}
v6 = 8;
LABEL_8:
v7 = open(a1, 2306);
v8 = v7;
if ( v7 != -1 )
{
fcntl(v7, 4, 2LL);
tcgetattr(v8, &termios_p);
cfmakeraw(&termios_p);
cfsetispeed(&termios_p, v6);
cfsetospeed(&termios_p, v6);
v9 = termios_p.c_cflag;
v10 = termios_p.c_cflag;
BYTE1(v9) = BYTE1(termios_p.c_cflag) & 0xFD;
BYTE1(v10) = BYTE1(termios_p.c_cflag) & 0xFE;
v11 = v9 | 0x880;
v12 = v10 | 0x880;
if ( a4 == 1 )
v12 = v11;
v13 = v12 & 0xFFFFFF8F;
if ( a3 == 6 )
{
termios_p.c_cflag = v13 | 0x10;
}
else if ( a3 == 7 )
{
termios_p.c_cflag = v13 | 0x20;
}
else
{
if ( a3 != 5 )
v13 |= 0x30u;
termios_p.c_cflag = v13;
}
termios_p.c_lflag &= 0xFFFFFFE4;
termios_p.c_oflag &= 0xFFFFFFFE;
strcpy((char *)&termios_p.c_cc[5], "d");
tcsetattr(v8, 2, &termios_p);
ioctl(v8, 0x5415uLL, &v15);
v15 |= 6u;
ioctl(v8, 0x5418uLL, &v15);
usleep(0x2710u);
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func0(const char *a1, int a2, int a3, int a4)
{
speed_t v6; // ebp
int v7; // eax
unsigned int v8; // er13
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;
goto LABEL_8;
}
if ( a2 > 1200 )
{
if ( a2 == 38400 )
{
v6 = 15;
goto LABEL_8;
}
if ( a2 <= 38400 )
{
if ( a2 == 9600 )
{
v6 = 13;
goto LABEL_8;
}
if ( a2 <= 9600 )
{
if ( a2 == 1800 )
{
v6 = 10;
goto LABEL_8;
}
if ( a2 == 2400 )
{
v6 = 11;
goto LABEL_8;
}
}
else
{
v6 = 14;
if ( a2 == 19200 )
goto LABEL_8;
}
}
else
{
switch ( a2 )
{
case 115200:
v6 = 4098;
goto LABEL_8;
case 230400:
v6 = 4099;
goto LABEL_8;
case 57600:
v6 = 4097;
goto LABEL_8;
}
}
return 4294967294LL;
}
if ( a2 == 150 )
{
v6 = 5;
goto LABEL_8;
}
if ( a2 <= 150 )
{
if ( a2 == 110 )
{
v6 = 3;
goto LABEL_8;
}
if ( a2 <= 110 )
{
if ( a2 == 50 )
{
v6 = 1;
goto LABEL_8;
}
if ( a2 == 75 )
{
v6 = 2;
goto LABEL_8;
}
}
else
{
v6 = 4;
if ( a2 == 134 )
goto LABEL_8;
}
return 4294967294LL;
}
if ( a2 == 300 )
{
v6 = 7;
goto LABEL_8;
}
if ( a2 != 600 )
{
if ( a2 == 200 )
{
v6 = 6;
goto LABEL_8;
}
return 4294967294LL;
}
v6 = 8;
LABEL_8:
v7 = open(a1, 2306);
v8 = v7;
if ( v7 != -1 )
{
fcntl(v7, 4, 2LL);
tcgetattr(v8, &termios_p);
cfmakeraw(&termios_p);
cfsetispeed(&termios_p, v6);
cfsetospeed(&termios_p, v6);
v9 = termios_p.c_cflag;
v10 = termios_p.c_cflag;
BYTE1(v9) = BYTE1(termios_p.c_cflag) & 0xFD;
BYTE1(v10) = BYTE1(termios_p.c_cflag) & 0xFE;
v11 = v9 | 0x880;
v12 = v10 | 0x880;
if ( a4 == 1 )
v12 = v11;
v13 = v12 & 0xFFFFFF8F;
if ( a3 == 6 )
{
termios_p.c_cflag = v13 | 0x10;
}
else if ( a3 == 7 )
{
termios_p.c_cflag = v13 | 0x20;
}
else
{
if ( a3 != 5 )
v13 |= 0x30u;
termios_p.c_cflag = v13;
}
termios_p.c_lflag &= 0xFFFFFFE4;
termios_p.c_oflag &= 0xFFFFFFFE;
strcpy((char *)&termios_p.c_cc[5], "d");
tcsetattr(v8, 2, &termios_p);
ioctl(v8, 0x5415uLL, &v15);
v15 |= 6u;
ioctl(v8, 0x5418uLL, &v15);
usleep(0x2710u);
}
return v8;
}

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

variable_types: fd 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:
// attributes: thunk
int serialClose(int fd)
{
return close(fd);
}

canonical_decompiled_code:
int func2(int fd)
{
return close(fd);
}

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, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-4E0h] BYREF
char s[1024]; // [rsp+20h] [rbp-4C8h] BYREF

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

canonical_decompiled_code:
ssize_t func5(int fd, char *format, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-4E0h] BYREF
char s[1024]; // [rsp+20h] [rbp-4C8h] BYREF

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

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