CheckStruct

decompiled_code:
__int64 __fastcall CheckStruct(__int64 a1, __int64 a2)
{
__int64 result; // rax

result = a1 >> 32;
if ( SHIDWORD(a1) <= 0 || (int)a1 <= 0 || !a2 )
{
fwrite("Invalid Structure!", 1uLL, 0x12uLL, stderr);
exit(1);
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, __int64 a2)
{
__int64 result; // rax

result = a1 >> 32;
if ( SHIDWORD(a1) <= 0 || (int)a1 <= 0 || !a2 )
{
fwrite("Invalid Structure!", 1uLL, 0x12uLL, stderr);
exit(1);
}
return result;
}

original_code:
void CheckStruct(const MyStruct stru) {
    if (stru.ny <= 0 || stru.nx <= 0 || stru.data == ((void *)0)) {
        fprintf(stderr, "Invalid Structure!");
        exit(1);
    }
}

canonical_original_code:
void CheckStruct(struct MyStruct stru) {
    if (stru.ny <= 0 || stru.nx <= 0 || stru.data == ((void *)0)) {
        fprintf(stderr, "Invalid Structure!");
        exit(1);
    }
}


initMyStruct

decompiled_code:
double *__fastcall initMyStruct(__int64 a1, int a2, int a3, double a4)
{
double *result; // rax
int i; // ecx
double *v7; // rdx

result = (double *)malloc(8LL * a3 * a2);
*(_QWORD *)(a1 + 8) = result;
if ( !result )
{
fwrite("Alloc space fails!\n", 1uLL, 0x13uLL, stderr);
exit(0);
}
*(_DWORD *)a1 = a2;
*(_DWORD *)(a1 + 4) = a3;
if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
{
v7 = &result[(unsigned int)(a3 - 1) + 1];
if ( a3 > 0 )
{
do
*result++ = a4;
while ( result != v7 );
result = v7;
}
}
}
return result;
}

canonical_decompiled_code:
double *__fastcall func9(__int64 a1, int a2, int a3, double a4)
{
double *result; // rax
int i; // ecx
double *v7; // rdx

result = (double *)malloc(8LL * a3 * a2);
*(_QWORD *)(a1 + 8) = result;
if ( !result )
{
fwrite("Alloc space fails!\n", 1uLL, 0x13uLL, stderr);
exit(0);
}
*(_DWORD *)a1 = a2;
*(_DWORD *)(a1 + 4) = a3;
if ( a2 > 0 )
{
for ( i = 0; i != a2; ++i )
{
v7 = &result[(unsigned int)(a3 - 1) + 1];
if ( a3 > 0 )
{
do
*result++ = a4;
while ( result != v7 );
result = v7;
}
}
}
return result;
}

original_code:
void initMyStruct(MyStruct *coo, int nx, int ny, MyDataF InitValue) {
    int i, j;
    MyDataF *p;
    coo->data = (MyDataF*) malloc(nx * ny * sizeof (MyDataF));
    if (coo->data == ((void *)0)) {
        fprintf(stderr, "Alloc space fails!\n");
        exit(0);
    }
    coo->nx = nx;
    coo->ny = ny;
    p = coo->data;
    for (i = 0; i < nx; i++) {
        for (j = 0; j < ny; j++) {
            *p++ = InitValue;
        }
    }
}

canonical_original_code:
void initMyStruct(struct MyStruct *coo, int nx, int ny, double InitValue) {
    int i; int j;
    double *p;
    coo->data = (double *) malloc(nx * ny * sizeof (MyDataF));
    if (coo->data == ((void *)0)) {
        fprintf(stderr, "Alloc space fails!\n");
        exit(0);
    }
    coo->nx = nx;
    coo->ny = ny;
    p = coo->data;
    for (i = 0; i < nx; i++) {
        for (j = 0; j < ny; j++) {
            *p++ = InitValue;
        }
    }
}

variable_types: struct MyStruct { int nx; int ny; double *data; } *

variable_types: nx int

variable_types: ny int

variable_types: InitValue double

variable_types: i int

variable_types: j int

variable_types:
p
double *

return_type: void


freeData

decompiled_code:
void __fastcall freeData(__int64 a1)
{
void *v1; // rdi

if ( a1 )
{
v1 = *(void **)(a1 + 8);
if ( v1 )
free(v1);
}
}

canonical_decompiled_code:
void __fastcall func10(__int64 a1)
{
void *v1; // rdi

if ( a1 )
{
v1 = *(void **)(a1 + 8);
if ( v1 )
free(v1);
}
}

original_code:
void freeData(MyStruct *mstruct) {
    if (mstruct != ((void *)0) && mstruct->data != ((void *)0)) {
        free(mstruct->data);
    }
}

canonical_original_code:
void freeData(struct MyStruct *mstruct) {
    if (mstruct != ((void *)0) && mstruct->data != ((void *)0)) {
        free(mstruct->data);
    }
}

user_defined_types: struct MyStruct { int nx; int ny; double *data; }


CheckIfOverLimit

decompiled_code:
int __fastcall CheckIfOverLimit(unsigned int a1, unsigned int a2, double a3)
{
int result; // eax

if ( COERCE_DOUBLE(*(_QWORD *)&a3 & LC12) > 1.0e80 )
result = printf("%d\t%d\t%-2.3e\n", a1, a2, a3);
return result;
}

canonical_decompiled_code:
int __fastcall func4(unsigned int a1, unsigned int a2, double a3)
{
int result; // eax

if ( COERCE_DOUBLE(*(_QWORD *)&a3 & LC12) > 1.0e80 )
result = printf("%d\t%d\t%-2.3e\n", a1, a2, a3);
return result;
}

original_code:
void CheckIfOverLimit(int i, int j, MyDataF data) {
    if (fabs(data) > 1e80) {
        printf("%d\t%d\t%-2.3e\n", i, j, data);
    }
}

canonical_original_code:
void CheckIfOverLimit(int i, int j, double data) {
    if (fabs(data) > 1e80) {
        printf("%d\t%d\t%-2.3e\n", i, j, data);
    }
}

variable_types: data double


CheckIfNeOverLimit

decompiled_code:
int __fastcall CheckIfNeOverLimit(unsigned int a1, unsigned int a2, double a3)
{
int result; // eax

if ( COERCE_DOUBLE(*(_QWORD *)&a3 & LC12) > 1.0e307 )
result = printf("%d\t%d\t%-2.3e\n", a1, a2, a3);
return result;
}

canonical_decompiled_code:
int __fastcall func6(unsigned int a1, unsigned int a2, double a3)
{
int result; // eax

if ( COERCE_DOUBLE(*(_QWORD *)&a3 & LC12) > 1.0e307 )
result = printf("%d\t%d\t%-2.3e\n", a1, a2, a3);
return result;
}

original_code:
void CheckIfNeOverLimit(int i, int j, MyDataF data) {
    if (fabs(data) > 1E307) {
        printf("%d\t%d\t%-2.3e\n", i, j, data);
    }
}

canonical_original_code:
void CheckIfNeOverLimit(int i, int j, double data) {
    if (fabs(data) > 1E307) {
        printf("%d\t%d\t%-2.3e\n", i, j, data);
    }
}


chkgrad

decompiled_code:
int __fastcall chkgrad(unsigned int a1, unsigned int a2, double a3, double a4)
{
double v4; // xmm2_8
int result; // eax

*(_QWORD *)&v4 = COERCE_UNSIGNED_INT64(a3 - a4) & LC12;
if ( v4 > 1000.0 )
result = printf("pre \t:%f\nnow\t:%f\ni = %d\tj = %d\ngrad = %f\n", a4, a3, a1, a2, v4);
return result;
}

canonical_decompiled_code:
int __fastcall func7(unsigned int a1, unsigned int a2, double a3, double a4)
{
double v4; // xmm2_8
int result; // eax

*(_QWORD *)&v4 = COERCE_UNSIGNED_INT64(a3 - a4) & LC12;
if ( v4 > 1000.0 )
result = printf("pre \t:%f\nnow\t:%f\ni = %d\tj = %d\ngrad = %f\n", a4, a3, a1, a2, v4);
return result;
}

original_code:
void chkgrad(MyDataF str, MyDataF strpre, int i, int j) {
    MyDataF grad;
    grad = fabs(str - strpre);
    if (grad > 1e3) {
        printf("pre \t:%f\nnow\t:%f\ni = %d\tj = %d\ngrad = %f\n", strpre, str, i, j, grad);
    }
}

canonical_original_code:
void chkgrad(double str, double strpre, int i, int j) {
    double grad;
    grad = fabs(str - strpre);
    if (grad > 1e3) {
        printf("pre \t:%f\nnow\t:%f\ni = %d\tj = %d\ngrad = %f\n", strpre, str, i, j, grad);
    }
}

variable_types: str double

variable_types: strpre double

variable_types: grad double


CheckIfNonZeros

decompiled_code:
int __fastcall CheckIfNonZeros(__int64 a1, __int64 a2)
{
__int64 v2; // rax
int v3; // er14
unsigned int v4; // er13
__int64 v5; // rbx
__int64 v6; // rbp

v2 = a1 >> 32;
if ( (int)a1 > 0 )
{
v3 = 0;
v4 = 0;
do
{
if ( SHIDWORD(a1) > 0 )
{
LODWORD(v2) = v3;
v6 = a2 + 8LL * v3;
v5 = 0LL;
do
{
if ( COERCE_DOUBLE(*(_QWORD *)(v6 + 8 * v5) & LC12) > 1.0e80 )
LODWORD(v2) = printf("%d\t%d\t%-2.3e\n", v4, (unsigned int)v5, *(double *)(v6 + 8 * v5));
++v5;
}
while ( v5 != HIDWORD(a1) );
}
++v4;
v3 += HIDWORD(a1);
}
while ( v4 != (_DWORD)a1 );
}
return v2;
}

canonical_decompiled_code:
int __fastcall func8(__int64 a1, __int64 a2)
{
__int64 v2; // rax
int v3; // er14
unsigned int v4; // er13
__int64 v5; // rbx
__int64 v6; // rbp

v2 = a1 >> 32;
if ( (int)a1 > 0 )
{
v3 = 0;
v4 = 0;
do
{
if ( SHIDWORD(a1) > 0 )
{
LODWORD(v2) = v3;
v6 = a2 + 8LL * v3;
v5 = 0LL;
do
{
if ( COERCE_DOUBLE(*(_QWORD *)(v6 + 8 * v5) & LC12) > 1.0e80 )
LODWORD(v2) = printf("%d\t%d\t%-2.3e\n", v4, (unsigned int)v5, *(double *)(v6 + 8 * v5));
++v5;
}
while ( v5 != HIDWORD(a1) );
}
++v4;
v3 += HIDWORD(a1);
}
while ( v4 != (_DWORD)a1 );
}
return v2;
}

original_code:
void CheckIfNonZeros(MyStruct stru) {
    int i, j;
    for (i = 0; i < stru.nx; i++) {
        for (j = 0; j < stru.ny; j++) {
            if (fabs(stru.data[i * stru.ny + j]) > 1e80) {
                printf("%d\t%d\t%-2.3e\n", i, j, stru.data[i * stru.ny + j]);
            }
        }
    }
}

canonical_original_code:
void CheckIfNonZeros(struct MyStruct stru) {
    int i; int j;
    for (i = 0; i < stru.nx; i++) {
        for (j = 0; j < stru.ny; j++) {
            if (fabs(stru.data[i * stru.ny + j]) > 1e80) {
                printf("%d\t%d\t%-2.3e\n", i, j, stru.data[i * stru.ny + j]);
            }
        }
    }
}

variable_types: struct MyStruct { int nx; int ny; double *data; }


SaveCapField

decompiled_code:
int __fastcall SaveCapField(int a1)
{
int v1; // ecx
int result; // eax
__int64 v4; // rdi
__int64 v5; // [rsp+0h] [rbp-38h]
_WORD v6[8]; // [rsp+10h] [rbp-28h] BYREF
int v7; // [rsp+20h] [rbp-18h]
char v8; // [rsp+24h] [rbp-14h]

v1 = (int)(0.000000001 / dt_F);
strcpy((char *)v6, "ez000000.dat");
v7 = 0;
v8 = 0;
result = scnt_3 / v1;
if ( !(scnt_3 % v1) )
{
LOBYTE(v6[1]) = cnt_2 / 100000 - 10 * (((1717986919LL * (cnt_2 / 100000)) >> 34) - ((cnt_2 / 100000) >> 31)) + 48;
HIBYTE(v6[1]) = cnt_2 / 10000 - 10 * (((1717986919LL * (cnt_2 / 10000)) >> 34) - ((cnt_2 / 10000) >> 31)) + 48;
LOBYTE(v6[2]) = cnt_2 / 1000 - 10 * (((1717986919LL * (cnt_2 / 1000)) >> 34) - ((cnt_2 / 1000) >> 31)) + 48;
HIBYTE(v6[2]) = cnt_2 / 100 % 10 + 48;
LOBYTE(v6[3]) = cnt_2 / 10 % 10 + 48;
v4 = (unsigned int)(cnt_2 % 10);
result = v4 + 48;
HIBYTE(v6[3]) = v4 + 48;
if ( IsTEx )
{
((void (__fastcall *)(__int64, _WORD *, _QWORD, _QWORD, _QWORD, _QWORD, _QWORD, _QWORD))CaptDataNoPML)(
v4,
v6,
Ez,
*((_QWORD *)&Ez + 1),
tpis,
tpie,
tpjs,
tpje);
v6[0] = 25445;
result = CaptDataCenter(
(unsigned int)(cnt_2 % 10),
v6,
Ez,
*((_QWORD *)&Ez + 1),
(unsigned int)(*((_DWORD *)&Ez + 1) / 2),
tpis,
tpie);
}
else if ( IsTMx )
{
LOBYTE(v6[0]) = 104;
((void (__fastcall *)(__int64, _WORD *, _QWORD, _QWORD, _QWORD, _QWORD, _QWORD, _QWORD))CaptDataNoPML)(
v4,
v6,
Hz[0],
Hz[1],
tpis,
tpie,
tpjs,
tpje);
result = saveEfieldCenterTMz(a1);
}
if ( IfWithDensity )
{
v6[0] = 25966;
CaptDataMNoPML(
(unsigned int)(cnt_2 % 10),
v6,
ne,
*((_QWORD *)&ne + 1),
2LL,
(unsigned int)(tpis * m),
(unsigned int)(tpie * m),
(unsigned int)(tpjs * m),
(unsigned int)(tpje * m));
v6[0] = 25454;
CaptDataCenter(
(unsigned int)(cnt_2 % 10),
v6,
ne,
*((_QWORD *)&ne + 1),
(unsigned int)(*((_DWORD *)&ne + 1) / 2),
(unsigned int)(tpis * m),
tpie * m);
v6[0] = 28005;
LODWORD(v5) = tpje * m;
CaptDataMNoPML(
(unsigned int)(cnt_2 % 10),
v6,
Erms,
*((_QWORD *)&Erms + 1),
2LL,
(unsigned int)(tpis * m),
(unsigned int)(tpie * m),
(unsigned int)(tpjs * m),
v5);
v6[0] = 25453;
result = CaptDataCenter(
(unsigned int)(cnt_2 % 10),
v6,
Erms,
*((_QWORD *)&Erms + 1),
(unsigned int)(*((_DWORD *)&Erms + 1) / 2),
(unsigned int)(tpis * m),
tpie * m);
}
++cnt_2;
}
++scnt_3;
return result;
}

canonical_decompiled_code:
int __fastcall func9(int a1)
{
int v1; // ecx
int result; // eax
__int64 v4; // rdi
__int64 v5; // [rsp+0h] [rbp-38h]
_WORD v6[8]; // [rsp+10h] [rbp-28h] BYREF
int v7; // [rsp+20h] [rbp-18h]
char v8; // [rsp+24h] [rbp-14h]

v1 = (int)(0.000000001 / dt_F);
strcpy((char *)v6, "ez000000.dat");
v7 = 0;
v8 = 0;
result = scnt_3 / v1;
if ( !(scnt_3 % v1) )
{
LOBYTE(v6[1]) = cnt_2 / 100000 - 10 * (((1717986919LL * (cnt_2 / 100000)) >> 34) - ((cnt_2 / 100000) >> 31)) + 48;
HIBYTE(v6[1]) = cnt_2 / 10000 - 10 * (((1717986919LL * (cnt_2 / 10000)) >> 34) - ((cnt_2 / 10000) >> 31)) + 48;
LOBYTE(v6[2]) = cnt_2 / 1000 - 10 * (((1717986919LL * (cnt_2 / 1000)) >> 34) - ((cnt_2 / 1000) >> 31)) + 48;
HIBYTE(v6[2]) = cnt_2 / 100 % 10 + 48;
LOBYTE(v6[3]) = cnt_2 / 10 % 10 + 48;
v4 = (unsigned int)(cnt_2 % 10);
result = v4 + 48;
HIBYTE(v6[3]) = v4 + 48;
if ( IsTEx )
{
((void (__fastcall *)(__int64, _WORD *, _QWORD, _QWORD, _QWORD, _QWORD, _QWORD, _QWORD))CaptDataNoPML)(
v4,
v6,
Ez,
*((_QWORD *)&Ez + 1),
tpis,
tpie,
tpjs,
tpje);
v6[0] = 25445;
result = CaptDataCenter(
(unsigned int)(cnt_2 % 10),
v6,
Ez,
*((_QWORD *)&Ez + 1),
(unsigned int)(*((_DWORD *)&Ez + 1) / 2),
tpis,
tpie);
}
else if ( IsTMx )
{
LOBYTE(v6[0]) = 104;
((void (__fastcall *)(__int64, _WORD *, _QWORD, _QWORD, _QWORD, _QWORD, _QWORD, _QWORD))CaptDataNoPML)(
v4,
v6,
Hz[0],
Hz[1],
tpis,
tpie,
tpjs,
tpje);
result = func5(a1);
}
if ( IfWithDensity )
{
v6[0] = 25966;
CaptDataMNoPML(
(unsigned int)(cnt_2 % 10),
v6,
ne,
*((_QWORD *)&ne + 1),
2LL,
(unsigned int)(tpis * m),
(unsigned int)(tpie * m),
(unsigned int)(tpjs * m),
(unsigned int)(tpje * m));
v6[0] = 25454;
CaptDataCenter(
(unsigned int)(cnt_2 % 10),
v6,
ne,
*((_QWORD *)&ne + 1),
(unsigned int)(*((_DWORD *)&ne + 1) / 2),
(unsigned int)(tpis * m),
tpie * m);
v6[0] = 28005;
LODWORD(v5) = tpje * m;
CaptDataMNoPML(
(unsigned int)(cnt_2 % 10),
v6,
Erms,
*((_QWORD *)&Erms + 1),
2LL,
(unsigned int)(tpis * m),
(unsigned int)(tpie * m),
(unsigned int)(tpjs * m),
v5);
v6[0] = 25453;
result = CaptDataCenter(
(unsigned int)(cnt_2 % 10),
v6,
Erms,
*((_QWORD *)&Erms + 1),
(unsigned int)(*((_DWORD *)&Erms + 1) / 2),
(unsigned int)(tpis * m),
tpie * m);
}
++cnt_2;
}
++scnt_3;
return result;
}

original_code:
void SaveCapField(const int timestep) {
    static int cnt = 1;
    static int scnt = 1;
    int skipstep = 1e-9 / dt_F;
    char file[21] = "ez000000.dat";
    if (scnt % skipstep == 0 ) {
        file[2] = '0' + (cnt / 100000) % 10;
        file[3] = '0' + (cnt / 10000) % 10;
        file[4] = '0' + (cnt / 1000) % 10;
        file[5] = '0' + (cnt / 100) % 10;
        file[6] = '0' + (cnt / 10) % 10;
        file[7] = '0' + cnt % 10;
        if (IsTEx) {
            CaptDataNoPML(cnt % 10, file, Ez, tpis, tpie, tpjs, tpje);
            file[0] = 'e';
            file[1] = 'c';
            CaptDataCenter(cnt % 10, file, Ez, Ez.ny / 2, tpis, tpie);
        } else if (IsTMx) {
            file[0] = 'h';
            file[1] = 'z';
            CaptDataNoPML(cnt % 10, file, Hz, tpis, tpie, tpjs, tpje);
            saveEfieldCenterTMz(timestep);
        }
        if(IfWithDensity) {
            file[0] = 'n';
            file[1] = 'e';
            CaptDataMNoPML(cnt % 10, file, ne, 2, tpis*m, tpie*m, tpjs*m, tpje * m);
            file[0] = 'n';
            file[1] = 'c';
            CaptDataCenter(cnt % 10, file, ne, ne.ny / 2, tpis*m, tpie * m);
            file[0] = 'e';
            file[1] = 'm';
            CaptDataMNoPML(cnt % 10, file, Erms, 2, tpis*m, tpie*m, tpjs*m, tpje * m);
            file[0] = 'm';
            file[1] = 'c';
            CaptDataCenter(cnt % 10, file, Erms, Erms.ny / 2, tpis*m, tpie * m);
        }
        cnt++;
    }
    scnt++;
}

canonical_original_code:
void SaveCapField(int timestep) {
    static int cnt = 1;
    static int scnt = 1;
    int skipstep = 1e-9 / dt_F;
    char file[21] = "ez000000.dat";
    if (scnt % skipstep == 0 ) {
        file[2] = '0' + (cnt / 100000) % 10;
        file[3] = '0' + (cnt / 10000) % 10;
        file[4] = '0' + (cnt / 1000) % 10;
        file[5] = '0' + (cnt / 100) % 10;
        file[6] = '0' + (cnt / 10) % 10;
        file[7] = '0' + cnt % 10;
        if (IsTEx) {
            CaptDataNoPML(cnt % 10, file, Ez, tpis, tpie, tpjs, tpje);
            file[0] = 'e';
            file[1] = 'c';
            CaptDataCenter(cnt % 10, file, Ez, Ez.ny / 2, tpis, tpie);
        } else if (IsTMx) {
            file[0] = 'h';
            file[1] = 'z';
            CaptDataNoPML(cnt % 10, file, Hz, tpis, tpie, tpjs, tpje);
            saveEfieldCenterTMz(timestep);
        }
        if(IfWithDensity) {
            file[0] = 'n';
            file[1] = 'e';
            CaptDataMNoPML(cnt % 10, file, ne, 2, tpis*m, tpie*m, tpjs*m, tpje * m);
            file[0] = 'n';
            file[1] = 'c';
            CaptDataCenter(cnt % 10, file, ne, ne.ny / 2, tpis*m, tpie * m);
            file[0] = 'e';
            file[1] = 'm';
            CaptDataMNoPML(cnt % 10, file, Erms, 2, tpis*m, tpie*m, tpjs*m, tpje * m);
            file[0] = 'm';
            file[1] = 'c';
            CaptDataCenter(cnt % 10, file, Erms, Erms.ny / 2, tpis*m, tpie * m);
        }
        cnt++;
    }
    scnt++;
}

variable_types: timestep int

variable_types: cnt int

variable_types: scnt int

variable_types: skipstep int

variable_types:
file
char [21]


repo: sklvjz/network

decompiled_code:
void eloop_event_setup_fds()
{
__int64 v0; // rdx
struct pollfd *v1; // rax

v0 = events;
if ( events )
{
v1 = fds;
do
{
v1->fd = *(_DWORD *)(v0 + 16);
v1->events = 1;
v1->revents = 0;
*(_QWORD *)(v0 + 40) = v1;
v0 = *(_QWORD *)v0;
++v1;
}
while ( v0 );
}
}

canonical_decompiled_code:
void func0()
{
__int64 v0; // rdx
struct pollfd *v1; // rax

v0 = events;
if ( events )
{
v1 = fds;
do
{
v1->fd = *(_DWORD *)(v0 + 16);
v1->events = 1;
v1->revents = 0;
*(_QWORD *)(v0 + 40) = v1;
v0 = *(_QWORD *)v0;
++v1;
}
while ( v0 );
}
}

original_code:
static void
eloop_event_setup_fds(void)
{
 struct event *e;
 size_t i;
 i = 0;
 for ((e) = ((&events)->tqh_first); (e); (e) = ((e)->next.tqe_next)) {
  fds[i].fd = e->fd;
  fds[i].events = 0x001;
  fds[i].revents = 0;
  e->pollfd = &fds[i];
  i++;
 }
}

canonical_original_code:
static void eloop_event_setup_fds()
{
 struct event *e;
 unsigned long i;
 i = 0;
 for ((e) = ((&events)->tqh_first); (e); (e) = ((e)->next.tqe_next)) {
  fds[i].fd = e->fd;
  fds[i].events = 0x001;
  fds[i].revents = 0;
  e->pollfd = &fds[i];
  i++;
 }
}

variable_types: struct event { struct { struct event *tqe_next; struct event **tqe_prev; } next; int fd; void (*callback)(void *); void *arg; struct pollfd { int fd; short events; short revents; } *pollfd; } *

variable_types:
i
unsigned long

user_defined_types: struct event { struct { struct event *tqe_next; struct event **tqe_prev; } next; int fd; void (*callback)(void *); void *arg; struct pollfd *pollfd; }


decompiled_code:
__int64 __fastcall eloop_event_add(int a1, _QWORD *a2, _QWORD *a3)
{
__int64 v4; // rax
_QWORD **v5; // rbp
__int64 v6; // rax
__int64 result; // rax
__int64 v8; // r14

v4 = events;
if ( events )
{
while ( *(_DWORD *)(v4 + 16) != a1 )
{
v4 = *(_QWORD *)v4;
if ( !v4 )
goto LABEL_4;
}
*(_QWORD *)(v4 + 24) = a2;
*(_QWORD *)(v4 + 32) = a3;
result = 0LL;
}
else
{
LABEL_4:
v5 = (_QWORD **)free_events;
if ( free_events )
{
if ( *(_QWORD *)free_events )
*(_QWORD *)(*(_QWORD *)free_events + 8LL) = *((_QWORD *)free_events + 1);
else
off_828 = *((_QWORD *)free_events + 1);
*v5[1] = *v5;
}
else
{
v5 = (_QWORD **)malloc(0x30uLL);
if ( !v5 )
{
syslog(3, "%s: %m", "eloop_event_add");
return 0xFFFFFFFFLL;
}
}
if ( ++events_len <= fds_len
|| (fds_len += 5LL, v8 = fds_len, free(fds), (fds = (struct pollfd *)malloc(8 * v8)) != 0LL) )
{
*((_DWORD *)v5 + 4) = a1;
v5[3] = a2;
v5[4] = a3;
v6 = events;
*v5 = (_QWORD *)events;
if ( v6 )
*(_QWORD *)(events + 8) = v5;
else
off_838 = (__int64 *)v5;
events = (__int64)v5;
v5[1] = &events;
eloop_event_setup_fds();
result = 0LL;
}
else
{
syslog(3, "%s: %m", "eloop_event_add");
free(v5);
result = 0xFFFFFFFFLL;
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, _QWORD *a2, _QWORD *a3)
{
__int64 v4; // rax
_QWORD **v5; // rbp
__int64 v6; // rax
__int64 result; // rax
__int64 v8; // r14

v4 = events;
if ( events )
{
while ( *(_DWORD *)(v4 + 16) != a1 )
{
v4 = *(_QWORD *)v4;
if ( !v4 )
goto LABEL_4;
}
*(_QWORD *)(v4 + 24) = a2;
*(_QWORD *)(v4 + 32) = a3;
result = 0LL;
}
else
{
LABEL_4:
v5 = (_QWORD **)free_events;
if ( free_events )
{
if ( *(_QWORD *)free_events )
*(_QWORD *)(*(_QWORD *)free_events + 8LL) = *((_QWORD *)free_events + 1);
else
off_828 = *((_QWORD *)free_events + 1);
*v5[1] = *v5;
}
else
{
v5 = (_QWORD **)malloc(0x30uLL);
if ( !v5 )
{
syslog(3, "%s: %m", "eloop_event_add");
return 0xFFFFFFFFLL;
}
}
if ( ++events_len <= fds_len
|| (fds_len += 5LL, v8 = fds_len, free(fds), (fds = (struct pollfd *)malloc(8 * v8)) != 0LL) )
{
*((_DWORD *)v5 + 4) = a1;
v5[3] = a2;
v5[4] = a3;
v6 = events;
*v5 = (_QWORD *)events;
if ( v6 )
*(_QWORD *)(events + 8) = v5;
else
off_838 = (__int64 *)v5;
events = (__int64)v5;
v5[1] = &events;
func0();
result = 0LL;
}
else
{
syslog(3, "%s: %m", "eloop_event_add");
free(v5);
result = 0xFFFFFFFFLL;
}
}
return result;
}

original_code:
int
eloop_event_add(int fd, void (*callback)(void *), void *arg)
{
 struct event *e;
 for ((e) = ((&events)->tqh_first); (e); (e) = ((e)->next.tqe_next)) {
  if (e->fd == fd) {
   e->callback = callback;
   e->arg = arg;
   return 0;
  }
 }
 if ((e = ((&free_events)->tqh_first))) {
  do { if (((e)->next.tqe_next) != ((void *)0)) (e)->next.tqe_next->next.tqe_prev = (e)->next.tqe_prev; else (&free_events)->tqh_last = (e)->next.tqe_prev; *(e)->next.tqe_prev = (e)->next.tqe_next; } while ( 0);
 } else {
  e = malloc(sizeof(*e));
  if (e == ((void *)0)) {
   syslog(3, "%s: %m", __func__);
   return -1;
  }
 }
 events_len++;
 if (events_len > fds_len) {
  fds_len += 5;
  free(fds);
  fds = malloc(sizeof(*fds) * fds_len);
  if (fds == ((void *)0)) {
   syslog(3, "%s: %m", __func__);
   free(e);
   return -1;
  }
 }
 e->fd = fd;
 e->callback = callback;
 e->arg = arg;
 do { if (((e)->next.tqe_next = (&events)->tqh_first) != ((void *)0)) (&events)->tqh_first->next.tqe_prev = &(e)->next.tqe_next; else (&events)->tqh_last = &(e)->next.tqe_next; (&events)->tqh_first = (e); (e)->next.tqe_prev = &(&events)->tqh_first; } while ( 0);
 eloop_event_setup_fds();
 return 0;
}

variable_types: fd int

variable_types:
callback
void (*)(void *)

variable_types:
arg
void *

return_type: int

user_defined_types: struct pollfd { int fd; short events; short revents; }


original_code:
void
eloop_event_delete(int fd)
{
 struct event *e;
 for ((e) = ((&events)->tqh_first); (e); (e) = ((e)->next.tqe_next)) {
  if (e->fd == fd) {
   do { if (((e)->next.tqe_next) != ((void *)0)) (e)->next.tqe_next->next.tqe_prev = (e)->next.tqe_prev; else (&events)->tqh_last = (e)->next.tqe_prev; *(e)->next.tqe_prev = (e)->next.tqe_next; } while ( 0);
   do { (e)->next.tqe_next = ((void *)0); (e)->next.tqe_prev = (&free_events)->tqh_last; *(&free_events)->tqh_last = (e); (&free_events)->tqh_last = &(e)->next.tqe_next; } while ( 0);
   events_len--;
   eloop_event_setup_fds();
   break;
  }
 }
}


decompiled_code:
__int64 __fastcall eloop_q_timeout_add_tv(int a1, _QWORD *a2, __int64 a3, __int64 a4)
{
__int64 v6; // rbx
__int64 v7; // rsi
__int64 v8; // r12
_QWORD *v9; // rax
_QWORD *v10; // rdx
_QWORD *v11; // rdx
__int64 v13; // rcx

get_monotonic(&now);
v6 = *a2 + now;
v7 = a2[1] + qword_878;
v8 = v7;
if ( v7 > 999999 )
{
++v6;
v8 = v7 - 1000000;
}
if ( now == v6 )
{
if ( qword_878 <= v8 )
goto LABEL_5;
LABEL_13:
*_errno_location() = 34;
return 0xFFFFFFFFLL;
}
if ( now > v6 )
goto LABEL_13;
LABEL_5:
v9 = (_QWORD *)timeouts;
if ( timeouts )
{
while ( v9[4] != a3 || v9[5] != a4 )
{
v9 = (_QWORD *)*v9;
if ( !v9 )
goto LABEL_6;
}
if ( *v9 )
*(_QWORD *)(*v9 + 8LL) = v9[1];
else
off_818 = v9[1];
*(_QWORD *)v9[1] = *v9;
}
else
{
LABEL_6:
v9 = (_QWORD *)free_timeouts;
if ( free_timeouts )
{
if ( *(_QWORD *)free_timeouts )
*(_QWORD *)(*(_QWORD *)free_timeouts + 8LL) = *(_QWORD *)(free_timeouts + 8);
else
off_808 = *(_QWORD *)(free_timeouts + 8);
*(_QWORD *)v9[1] = *v9;
}
else
{
v9 = malloc(0x38uLL);
if ( !v9 )
{
syslog(3, "%s: %m", "eloop_q_timeout_add_tv");
return 0xFFFFFFFFLL;
}
}
}
v9[2] = v6;
v9[3] = v8;
v9[4] = a3;
v9[5] = a4;
*((_DWORD *)v9 + 12) = a1;
v10 = (_QWORD *)timeouts;
if ( !timeouts )
{
LABEL_11:
*v9 = 0LL;
v11 = (_QWORD *)off_818;
v9[1] = off_818;
*v11 = v9;
off_818 = (__int64)v9;
return 0LL;
}
while ( 1 )
{
v13 = v10[2];
if ( v13 != v6 )
break;
if ( v10[3] > v8 )
goto LABEL_28;
LABEL_25:
v10 = (_QWORD *)*v10;
if ( !v10 )
goto LABEL_11;
}
if ( v13 <= v6 )
goto LABEL_25;
LABEL_28:
v9[1] = v10[1];
*v9 = v10;
*(_QWORD *)v10[1] = v9;
v10[1] = v9;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(int a1, _QWORD *a2, __int64 a3, __int64 a4)
{
__int64 v6; // rbx
__int64 v7; // rsi
__int64 v8; // r12
_QWORD *v9; // rax
_QWORD *v10; // rdx
_QWORD *v11; // rdx
__int64 v13; // rcx

get_monotonic(&now);
v6 = *a2 + now;
v7 = a2[1] + qword_878;
v8 = v7;
if ( v7 > 999999 )
{
++v6;
v8 = v7 - 1000000;
}
if ( now == v6 )
{
if ( qword_878 <= v8 )
goto LABEL_5;
LABEL_13:
*_errno_location() = 34;
return 0xFFFFFFFFLL;
}
if ( now > v6 )
goto LABEL_13;
LABEL_5:
v9 = (_QWORD *)timeouts;
if ( timeouts )
{
while ( v9[4] != a3 || v9[5] != a4 )
{
v9 = (_QWORD *)*v9;
if ( !v9 )
goto LABEL_6;
}
if ( *v9 )
*(_QWORD *)(*v9 + 8LL) = v9[1];
else
off_818 = v9[1];
*(_QWORD *)v9[1] = *v9;
}
else
{
LABEL_6:
v9 = (_QWORD *)free_timeouts;
if ( free_timeouts )
{
if ( *(_QWORD *)free_timeouts )
*(_QWORD *)(*(_QWORD *)free_timeouts + 8LL) = *(_QWORD *)(free_timeouts + 8);
else
off_808 = *(_QWORD *)(free_timeouts + 8);
*(_QWORD *)v9[1] = *v9;
}
else
{
v9 = malloc(0x38uLL);
if ( !v9 )
{
syslog(3, "%s: %m", "eloop_q_timeout_add_tv");
return 0xFFFFFFFFLL;
}
}
}
v9[2] = v6;
v9[3] = v8;
v9[4] = a3;
v9[5] = a4;
*((_DWORD *)v9 + 12) = a1;
v10 = (_QWORD *)timeouts;
if ( !timeouts )
{
LABEL_11:
*v9 = 0LL;
v11 = (_QWORD *)off_818;
v9[1] = off_818;
*v11 = v9;
off_818 = (__int64)v9;
return 0LL;
}
while ( 1 )
{
v13 = v10[2];
if ( v13 != v6 )
break;
if ( v10[3] > v8 )
goto LABEL_28;
LABEL_25:
v10 = (_QWORD *)*v10;
if ( !v10 )
goto LABEL_11;
}
if ( v13 <= v6 )
goto LABEL_25;
LABEL_28:
v9[1] = v10[1];
*v9 = v10;
*(_QWORD *)v10[1] = v9;
v10[1] = v9;
return 0LL;
}

original_code:
int
eloop_q_timeout_add_tv(int queue,
    const struct timeval *when, void (*callback)(void *), void *arg)
{
 struct timeval w;
 struct timeout *t, *tt = ((void *)0);
 get_monotonic(&now);
 do { (&w)->tv_sec = (&now)->tv_sec + (when)->tv_sec; (&w)->tv_usec = (&now)->tv_usec + (when)->tv_usec; if ((&w)->tv_usec >= 1000000) { ++(&w)->tv_sec; (&w)->tv_usec -= 1000000; } } while (0);
 if ((((&w)->tv_sec == (&now)->tv_sec) ? ((&w)->tv_usec < (&now)->tv_usec) : ((&w)->tv_sec < (&now)->tv_sec))) {
  (*__errno_location ()) = 34;
  return -1;
 }
 for ((t) = ((&timeouts)->tqh_first); (t); (t) = ((t)->next.tqe_next)) {
  if (t->callback == callback && t->arg == arg) {
   do { if (((t)->next.tqe_next) != ((void *)0)) (t)->next.tqe_next->next.tqe_prev = (t)->next.tqe_prev; else (&timeouts)->tqh_last = (t)->next.tqe_prev; *(t)->next.tqe_prev = (t)->next.tqe_next; } while ( 0);
   break;
  }
 }
 if (t == ((void *)0)) {
  if ((t = ((&free_timeouts)->tqh_first))) {
   do { if (((t)->next.tqe_next) != ((void *)0)) (t)->next.tqe_next->next.tqe_prev = (t)->next.tqe_prev; else (&free_timeouts)->tqh_last = (t)->next.tqe_prev; *(t)->next.tqe_prev = (t)->next.tqe_next; } while ( 0);
  } else {
   t = malloc(sizeof(*t));
   if (t == ((void *)0)) {
    syslog(3, "%s: %m", __func__);
    return -1;
   }
  }
 }
 t->when.tv_sec = w.tv_sec;
 t->when.tv_usec = w.tv_usec;
 t->callback = callback;
 t->arg = arg;
 t->queue = queue;
 for ((tt) = ((&timeouts)->tqh_first); (tt); (tt) = ((tt)->next.tqe_next)) {
  if ((((&t->when)->tv_sec == (&tt->when)->tv_sec) ? ((&t->when)->tv_usec < (&tt->when)->tv_usec) : ((&t->when)->tv_sec < (&tt->when)->tv_sec))) {
   do { (t)->next.tqe_prev = (tt)->next.tqe_prev; (t)->next.tqe_next = (tt); *(tt)->next.tqe_prev = (t); (tt)->next.tqe_prev = &(t)->next.tqe_next; } while ( 0);
   return 0;
  }
 }
 do { (t)->next.tqe_next = ((void *)0); (t)->next.tqe_prev = (&timeouts)->tqh_last; *(&timeouts)->tqh_last = (t); (&timeouts)->tqh_last = &(t)->next.tqe_next; } while ( 0);
 return 0;
}

canonical_original_code:
int eloop_q_timeout_add_tv(int queue, struct timeval *when, void (*callback)(void *), void *arg)
{
 struct timeval w;
 struct timeout *t; struct timeout *tt = ((void *)0);
 get_monotonic(&now);
 do { (&w)->tv_sec = (&now)->tv_sec + (when)->tv_sec; (&w)->tv_usec = (&now)->tv_usec + (when)->tv_usec; if ((&w)->tv_usec >= 1000000) { ++(&w)->tv_sec; (&w)->tv_usec -= 1000000; } } while (0);
 if ((((&w)->tv_sec == (&now)->tv_sec) ? ((&w)->tv_usec < (&now)->tv_usec) : ((&w)->tv_sec < (&now)->tv_sec))) {
  (*__errno_location ()) = 34;
  return -1;
 }
 for ((t) = ((&timeouts)->tqh_first); (t); (t) = ((t)->next.tqe_next)) {
  if (t->callback == callback && t->arg == arg) {
   do { if (((t)->next.tqe_next) != ((void *)0)) (t)->next.tqe_next->next.tqe_prev = (t)->next.tqe_prev; else (&timeouts)->tqh_last = (t)->next.tqe_prev; *(t)->next.tqe_prev = (t)->next.tqe_next; } while ( 0);
   break;
  }
 }
 if (t == ((void *)0)) {
  if ((t = ((&free_timeouts)->tqh_first))) {
   do { if (((t)->next.tqe_next) != ((void *)0)) (t)->next.tqe_next->next.tqe_prev = (t)->next.tqe_prev; else (&free_timeouts)->tqh_last = (t)->next.tqe_prev; *(t)->next.tqe_prev = (t)->next.tqe_next; } while ( 0);
  } else {
   t = malloc(sizeof(*t));
   if (t == ((void *)0)) {
    syslog(3, "%s: %m", __func__);
    return -1;
   }
  }
 }
 t->when.tv_sec = w.tv_sec;
 t->when.tv_usec = w.tv_usec;
 t->callback = callback;
 t->arg = arg;
 t->queue = queue;
 for ((tt) = ((&timeouts)->tqh_first); (tt); (tt) = ((tt)->next.tqe_next)) {
  if ((((&t->when)->tv_sec == (&tt->when)->tv_sec) ? ((&t->when)->tv_usec < (&tt->when)->tv_usec) : ((&t->when)->tv_sec < (&tt->when)->tv_sec))) {
   do { (t)->next.tqe_prev = (tt)->next.tqe_prev; (t)->next.tqe_next = (tt); *(tt)->next.tqe_prev = (t); (tt)->next.tqe_prev = &(t)->next.tqe_next; } while ( 0);
   return 0;
  }
 }
 do { (t)->next.tqe_next = ((void *)0); (t)->next.tqe_prev = (&timeouts)->tqh_last; *(&timeouts)->tqh_last = (t); (&timeouts)->tqh_last = &(t)->next.tqe_next; } while ( 0);
 return 0;
}

variable_types: queue int

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

variable_types: struct timeout { struct { struct timeout *tqe_next; struct timeout **tqe_prev; } next; struct timeval { long tv_sec; long tv_usec; } when; void (*callback)(void *); void *arg; int queue; } *

user_defined_types: struct timeout { struct { struct timeout *tqe_next; struct timeout **tqe_prev; } next; struct timeval when; void (*callback)(void *); void *arg; int queue; }


decompiled_code:
__int64 __fastcall eloop_q_timeout_add_sec(int a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 v5[3]; // [rsp+0h] [rbp-18h] BYREF

v5[0] = a2;
v5[1] = 0LL;
return eloop_q_timeout_add_tv(a1, v5, a3, a4);
}

original_code:
int
eloop_q_timeout_add_sec(int queue, time_t when,
    void (*callback)(void *), void *arg)
{
 struct timeval tv;
 tv.tv_sec = when;
 tv.tv_usec = 0;
 return eloop_q_timeout_add_tv(queue, &tv, callback, arg);
}

canonical_original_code:
int eloop_q_timeout_add_sec(int queue, long when, void (*callback)(void *), void *arg)
{
 struct timeval tv;
 tv.tv_sec = when;
 tv.tv_usec = 0;
 return eloop_q_timeout_add_tv(queue, &tv, callback, arg);
}

variable_types: when long

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


decompiled_code:
__int64 __fastcall eloop_timeout_add_now(__int64 a1, __int64 a2)
{
__int64 result; // rax

if ( timeout0 )
{
syslog(4, "%s: timeout0 already set", "eloop_timeout_add_now");
result = eloop_q_timeout_add_sec(0, 0LL, a1, a2);
}
else
{
timeout0 = a1;
timeout0_arg = a2;
result = 0LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2)
{
__int64 result; // rax

if ( timeout0 )
{
syslog(4, "%s: timeout0 already set", "eloop_timeout_add_now");
result = func4(0, 0LL, a1, a2);
}
else
{
timeout0 = a1;
timeout0_arg = a2;
result = 0LL;
}
return result;
}

original_code:
int
eloop_timeout_add_now(void (*callback)(void *), void *arg)
{
 if (timeout0 != ((void *)0)) {
  syslog(4, "%s: timeout0 already set", __func__);
  return eloop_q_timeout_add_sec(0, 0, callback, arg);
 }
 timeout0 = callback;
 timeout0_arg = arg;
 return 0;
}


decompiled_code:
_QWORD *eloop_q_timeouts_delete(int a1, __int64 a2, __int64 a3, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a3);
return eloop_q_timeouts_delete_v(a1, a2, a3, (const __m128i *)va);
}

canonical_decompiled_code:
_QWORD *func6(int a1, __int64 a2, __int64 a3, ...)
{
gcc_va_list va; // [rsp+8h] [rbp-D0h] BYREF

va_start(va, a3);
return eloop_q_timeouts_delete_v(a1, a2, a3, (const __m128i *)va);
}

original_code:
void
eloop_q_timeouts_delete(int queue, void *arg, void (*callback)(void *), ...)
{
 va_list va;
 __builtin_va_start(va,callback);
 eloop_q_timeouts_delete_v(queue, arg, callback, va);
 __builtin_va_end(va);
}

canonical_original_code:
void eloop_q_timeouts_delete(int queue, void *arg, void (*callback)(void *), ...)
{
 __builtin_va_list va;
 __builtin_va_start(va,callback);
 eloop_q_timeouts_delete_v(queue, arg, callback, va);
 __builtin_va_end(va);
}


decompiled_code:
void __fastcall __noreturn eloop_start(__sigset_t *ss)
{
void (__fastcall *v1)(__int64); // rax
const struct timespec *v2; // rbx
__int64 v3; // rax
__int64 v4; // rdx
__int64 v5; // rax
int v6; // eax
__int64 v7; // rax
const struct timespec **v8; // rax
int v9; // eax
__int64 v10[5]; // [rsp+0h] [rbp-28h] BYREF

while ( 1 )
{
while ( 1 )
{
LABEL_3:
while ( timeout0 )
{
v1 = (void (__fastcall *)(__int64))timeout0;
timeout0 = 0LL;
v1(timeout0_arg);
}
v2 = (const struct timespec *)timeouts;
if ( !timeouts )
{
if ( !events_len )
{
syslog(3, "nothing to do");
exit(1);
}
goto LABEL_11;
}
get_monotonic(&now);
v3 = v2[1].tv_sec;
if ( now != v3 )
break;
if ( qword_878 <= v2[1].tv_nsec )
goto LABEL_7;
LABEL_18:
if ( v2->tv_sec )
*(_QWORD *)(v2->tv_sec + 8) = v2->tv_nsec;
else
off_818 = v2->tv_nsec;
*(_QWORD *)v2->tv_nsec = v2->tv_sec;
((void (__fastcall *)(__syscall_slong_t))v2[2].tv_sec)(v2[2].tv_nsec);
v2->tv_sec = 0LL;
v8 = (const struct timespec **)off_808;
v2->tv_nsec = off_808;
*v8 = v2;
off_808 = (__int64)v2;
}
if ( now > v3 )
goto LABEL_18;
LABEL_7:
v4 = v3 - now;
v5 = v2[1].tv_nsec - qword_878;
if ( v5 < 0 )
{
--v4;
v5 += 1000000LL;
}
v10[0] = v4;
v10[1] = 1000 * v5;
v2 = (const struct timespec *)v10;
LABEL_11:
v6 = ppoll(fds, events_len, v2, ss);
if ( v6 == -1 )
{
v9 = *_errno_location();
if ( v9 != 11 && v9 != 4 )
{
syslog(3, "poll: %m");
exit(1);
}
}
else if ( v6 > 0 )
{
v7 = events;
if ( events )
{
while ( (*(_BYTE *)(*(_QWORD *)(v7 + 40) + 6LL) & 0x11) == 0 )
{
v7 = *(_QWORD *)v7;
if ( !v7 )
goto LABEL_3;
}
(*(void (__fastcall **)(_QWORD))(v7 + 24))(*(_QWORD *)(v7 + 32));
}
}
}
}

canonical_decompiled_code:
void __fastcall __noreturn func7(__sigset_t *ss)
{
void (__fastcall *v1)(__int64); // rax
const struct timespec *v2; // rbx
__int64 v3; // rax
__int64 v4; // rdx
__int64 v5; // rax
int v6; // eax
__int64 v7; // rax
const struct timespec **v8; // rax
int v9; // eax
__int64 v10[5]; // [rsp+0h] [rbp-28h] BYREF

while ( 1 )
{
while ( 1 )
{
LABEL_3:
while ( timeout0 )
{
v1 = (void (__fastcall *)(__int64))timeout0;
timeout0 = 0LL;
v1(timeout0_arg);
}
v2 = (const struct timespec *)timeouts;
if ( !timeouts )
{
if ( !events_len )
{
syslog(3, "nothing to do");
exit(1);
}
goto LABEL_11;
}
get_monotonic(&now);
v3 = v2[1].tv_sec;
if ( now != v3 )
break;
if ( qword_878 <= v2[1].tv_nsec )
goto LABEL_7;
LABEL_18:
if ( v2->tv_sec )
*(_QWORD *)(v2->tv_sec + 8) = v2->tv_nsec;
else
off_818 = v2->tv_nsec;
*(_QWORD *)v2->tv_nsec = v2->tv_sec;
((void (__fastcall *)(__syscall_slong_t))v2[2].tv_sec)(v2[2].tv_nsec);
v2->tv_sec = 0LL;
v8 = (const struct timespec **)off_808;
v2->tv_nsec = off_808;
*v8 = v2;
off_808 = (__int64)v2;
}
if ( now > v3 )
goto LABEL_18;
LABEL_7:
v4 = v3 - now;
v5 = v2[1].tv_nsec - qword_878;
if ( v5 < 0 )
{
--v4;
v5 += 1000000LL;
}
v10[0] = v4;
v10[1] = 1000 * v5;
v2 = (const struct timespec *)v10;
LABEL_11:
v6 = ppoll(fds, events_len, v2, ss);
if ( v6 == -1 )
{
v9 = *_errno_location();
if ( v9 != 11 && v9 != 4 )
{
syslog(3, "poll: %m");
exit(1);
}
}
else if ( v6 > 0 )
{
v7 = events;
if ( events )
{
while ( (*(_BYTE *)(*(_QWORD *)(v7 + 40) + 6LL) & 0x11) == 0 )
{
v7 = *(_QWORD *)v7;
if ( !v7 )
goto LABEL_3;
}
(*(void (__fastcall **)(_QWORD))(v7 + 24))(*(_QWORD *)(v7 + 32));
}
}
}
}

original_code:
__attribute__((__noreturn__)) void
eloop_start(const sigset_t *sigmask)
{
 int n;
 struct event *e;
 struct timeout *t;
 struct timeval tv;
 struct timespec ts, *tsp;
 void (*t0)(void *);
 for (;;) {
  if (timeout0) {
   t0 = timeout0;
   timeout0 = ((void *)0);
   t0(timeout0_arg);
   continue;
  }
  if ((t = ((&timeouts)->tqh_first))) {
   get_monotonic(&now);
   if ((((&now)->tv_sec == (&t->when)->tv_sec) ? ((&now)->tv_usec > (&t->when)->tv_usec) : ((&now)->tv_sec > (&t->when)->tv_sec))) {
    do { if (((t)->next.tqe_next) != ((void *)0)) (t)->next.tqe_next->next.tqe_prev = (t)->next.tqe_prev; else (&timeouts)->tqh_last = (t)->next.tqe_prev; *(t)->next.tqe_prev = (t)->next.tqe_next; } while ( 0);
    t->callback(t->arg);
    do { (t)->next.tqe_next = ((void *)0); (t)->next.tqe_prev = (&free_timeouts)->tqh_last; *(&free_timeouts)->tqh_last = (t); (&free_timeouts)->tqh_last = &(t)->next.tqe_next; } while ( 0);
    continue;
   }
   do { (&tv)->tv_sec = (&t->when)->tv_sec - (&now)->tv_sec; (&tv)->tv_usec = (&t->when)->tv_usec - (&now)->tv_usec; if ((&tv)->tv_usec < 0) { --(&tv)->tv_sec; (&tv)->tv_usec += 1000000; } } while (0);
   { (&ts)->tv_sec = (&tv)->tv_sec; (&ts)->tv_nsec = (&tv)->tv_usec * 1000; };
   tsp = &ts;
  } else
   tsp = ((void *)0);
  if (tsp == ((void *)0) && events_len == 0) {
   syslog(3, "nothing to do");
   exit(1);
  }
  n = pollts(fds, events_len, tsp, sigmask);
  if (n == -1) {
   if ((*__errno_location ()) == 11 || (*__errno_location ()) == 4)
    continue;
   syslog(3, "poll: %m");
   exit(1);
  }
  if (n > 0) {
   for ((e) = ((&events)->tqh_first); (e); (e) = ((e)->next.tqe_next)) {
    if (e->pollfd->revents & (0x001 | 0x010)) {
     e->callback(e->arg);
     break;
    }
   }
  }
 }
}

canonical_original_code:
void eloop_start(struct __sigset_t *sigmask)
{
 int n;
 struct event *e;
 struct timeout *t;
 struct timeval tv;
 struct timespec ts; struct timespec *tsp;
 void (*t0)(void *);
 for (;;) {
  if (timeout0) {
   t0 = timeout0;
   timeout0 = ((void *)0);
   t0(timeout0_arg);
   continue;
  }
  if ((t = ((&timeouts)->tqh_first))) {
   get_monotonic(&now);
   if ((((&now)->tv_sec == (&t->when)->tv_sec) ? ((&now)->tv_usec > (&t->when)->tv_usec) : ((&now)->tv_sec > (&t->when)->tv_sec))) {
    do { if (((t)->next.tqe_next) != ((void *)0)) (t)->next.tqe_next->next.tqe_prev = (t)->next.tqe_prev; else (&timeouts)->tqh_last = (t)->next.tqe_prev; *(t)->next.tqe_prev = (t)->next.tqe_next; } while ( 0);
    t->callback(t->arg);
    do { (t)->next.tqe_next = ((void *)0); (t)->next.tqe_prev = (&free_timeouts)->tqh_last; *(&free_timeouts)->tqh_last = (t); (&free_timeouts)->tqh_last = &(t)->next.tqe_next; } while ( 0);
    continue;
   }
   do { (&tv)->tv_sec = (&t->when)->tv_sec - (&now)->tv_sec; (&tv)->tv_usec = (&t->when)->tv_usec - (&now)->tv_usec; if ((&tv)->tv_usec < 0) { --(&tv)->tv_sec; (&tv)->tv_usec += 1000000; } } while (0);
   { (&ts)->tv_sec = (&tv)->tv_sec; (&ts)->tv_nsec = (&tv)->tv_usec * 1000; };
   tsp = &ts;
  } else
   tsp = ((void *)0);
  if (tsp == ((void *)0) && events_len == 0) {
   syslog(3, "nothing to do");
   exit(1);
  }
  n = pollts(fds, events_len, tsp, sigmask);
  if (n == -1) {
   if ((*__errno_location ()) == 11 || (*__errno_location ()) == 4)
    continue;
   syslog(3, "poll: %m");
   exit(1);
  }
  if (n > 0) {
   for ((e) = ((&events)->tqh_first); (e); (e) = ((e)->next.tqe_next)) {
    if (e->pollfd->revents & (0x001 | 0x010)) {
     e->callback(e->arg);
     break;
    }
   }
  }
 }
}

variable_types: struct __sigset_t { unsigned long __val[-1]; } *

variable_types: n int

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

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


decompiled_code:
int control_handle()
{
_DWORD *v0; // rax
unsigned int v1; // ebx
socklen_t addr_len; // [rsp+Ch] [rbp-7Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-78h] BYREF

addr_len = 110;
LODWORD(v0) = accept(fd, &addr, &addr_len);
if ( (_DWORD)v0 != -1 )
{
v1 = (unsigned int)v0;
set_cloexec((unsigned int)v0);
v0 = malloc(0x10uLL);
if ( v0 )
{
*v0 = v1;
v0[1] = 0;
*((_QWORD *)v0 + 1) = control_fds;
control_fds = v0;
LODWORD(v0) = eloop_event_add(v1, control_handle_data);
}
}
return (int)v0;
}

canonical_decompiled_code:
int func0()
{
_DWORD *v0; // rax
unsigned int v1; // ebx
socklen_t addr_len; // [rsp+Ch] [rbp-7Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-78h] BYREF

addr_len = 110;
LODWORD(v0) = accept(fd, &addr, &addr_len);
if ( (_DWORD)v0 != -1 )
{
v1 = (unsigned int)v0;
set_cloexec((unsigned int)v0);
v0 = malloc(0x10uLL);
if ( v0 )
{
*v0 = v1;
v0[1] = 0;
*((_QWORD *)v0 + 1) = control_fds;
control_fds = v0;
LODWORD(v0) = eloop_event_add(v1, control_handle_data);
}
}
return (int)v0;
}

original_code:
static void
control_handle(__attribute__((__unused__)) void *arg)
{
 struct sockaddr_un run;
 socklen_t len;
 struct fd_list *l;
 int f;
 len = sizeof(run);
 if ((f = accept(fd, (struct sockaddr *)&run, &len)) == -1)
  return;
 set_cloexec(f);
 l = malloc(sizeof(*l));
 if (l) {
  l->fd = f;
  l->listener = 0;
  l->next = control_fds;
  control_fds = l;
  eloop_event_add(l->fd, control_handle_data, l);
 }
}

canonical_original_code:
static void control_handle(void *arg)
{
 struct sockaddr_un run;
 unsigned int len;
 struct fd_list *l;
 int f;
 len = sizeof(run);
 if ((f = accept(fd, (struct sockaddr *)&run, &len)) == -1)
  return;
 set_cloexec(f);
 l = malloc(sizeof(*l));
 if (l) {
  l->fd = f;
  l->listener = 0;
  l->next = control_fds;
  control_fds = l;
  eloop_event_add(l->fd, control_handle_data, l);
 }
}

variable_types: struct sockaddr_un { unsigned short sun_family; char sun_path[108]; }

variable_types:
len
unsigned int

variable_types: struct fd_list { int fd; int listener; struct fd_list *next; } *

variable_types: f int

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


decompiled_code:
void __fastcall control_remove(void *ptr)
{
_QWORD *v1; // rax
_QWORD *v2; // rbp

v1 = control_fds;
v2 = 0LL;
while ( v1 )
{
if ( v1 == ptr )
{
close(*(_DWORD *)ptr);
eloop_event_delete(*(unsigned int *)ptr);
if ( v2 )
v2[1] = *((_QWORD *)ptr + 1);
else
control_fds = (void *)*((_QWORD *)ptr + 1);
free(ptr);
return;
}
v2 = v1;
v1 = (_QWORD *)v1[1];
}
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
_QWORD *v1; // rax
_QWORD *v2; // rbp

v1 = control_fds;
v2 = 0LL;
while ( v1 )
{
if ( v1 == ptr )
{
close(*(_DWORD *)ptr);
eloop_event_delete(*(unsigned int *)ptr);
if ( v2 )
v2[1] = *((_QWORD *)ptr + 1);
else
control_fds = (void *)*((_QWORD *)ptr + 1);
free(ptr);
return;
}
v2 = v1;
v1 = (_QWORD *)v1[1];
}
}

original_code:
static void
control_remove(void *arg)
{
 struct fd_list *l, *n, *last = ((void *)0);
 l = control_fds;
 while (l) {
  n = l->next;
  if (l == arg) {
   close(l->fd);
   eloop_event_delete(l->fd);
   if (last == ((void *)0))
    control_fds = l->next;
   else
    last->next = l->next;
   free(l);
   break;
  }
  last = l;
  l = n;
 }
}

canonical_original_code:
static void control_remove(void *arg)
{
 struct fd_list *l; struct fd_list *n; struct fd_list *last = ((void *)0);
 l = control_fds;
 while (l) {
  n = l->next;
  if (l == arg) {
   close(l->fd);
   eloop_event_delete(l->fd);
   if (last == ((void *)0))
    control_fds = l->next;
   else
    last->next = l->next;
   free(l);
   break;
  }
  last = l;
  l = n;
 }
}

user_defined_types: struct fd_list { int fd; int listener; struct fd_list *next; }


decompiled_code:
void __fastcall control_handle_data(void *ptr)
{
ssize_t v1; // rax
char *v2; // r13
_QWORD *v3; // r12
const char *v4; // rbx
unsigned int v5; // ebp

v1 = read(*(_DWORD *)ptr, buffer, 0x3FFuLL);
if ( (unsigned __int64)(v1 + 1) <= 1 )
{
control_remove(ptr);
}
else
{
buffer[v1] = 0;
v2 = &buffer[v1];
if ( &buffer[v1] <= buffer )
{
v5 = 0;
}
else
{
v3 = &argvp;
v4 = buffer;
v5 = 0;
do
{
++v5;
*v3++ = v4;
v4 += strlen(v4) + 1;
}
while ( v2 > v4 && v5 <= 0x7F7 );
}
handle_args(ptr, v5, &argvp);
}
}

canonical_decompiled_code:
void __fastcall func2(void *ptr)
{
ssize_t v1; // rax
char *v2; // r13
_QWORD *v3; // r12
const char *v4; // rbx
unsigned int v5; // ebp

v1 = read(*(_DWORD *)ptr, buffer, 0x3FFuLL);
if ( (unsigned __int64)(v1 + 1) <= 1 )
{
func1(ptr);
}
else
{
buffer[v1] = 0;
v2 = &buffer[v1];
if ( &buffer[v1] <= buffer )
{
v5 = 0;
}
else
{
v3 = &argvp;
v4 = buffer;
v5 = 0;
do
{
++v5;
*v3++ = v4;
v4 += strlen(v4) + 1;
}
while ( v2 > v4 && v5 <= 0x7F7 );
}
handle_args(ptr, v5, &argvp);
}
}

original_code:
static void
control_handle_data(void *arg)
{
 struct fd_list *l = arg;
 ssize_t bytes;
 int argc;
 char *e, *p;
 char **ap;
 bytes = read(l->fd, buffer, sizeof(buffer) - 1);
 if (bytes == -1 || bytes == 0) {
  control_remove(l);
  return;
 }
 buffer[bytes] = '\0';
 p = buffer;
 e = buffer + bytes;
 argc = 0;
 ap = argvp;
 while (p < e && (size_t)argc < sizeof(argvp)) {
  argc++;
  *ap++ = p;
  p += strlen(p) + 1;
 }
 handle_args(l, argc, argvp);
}

canonical_original_code:
static void control_handle_data(void *arg)
{
 struct fd_list *l = arg;
 long bytes;
 int argc;
 char *e; char *p;
 char **ap;
 bytes = read(l->fd, buffer, sizeof(buffer) - 1);
 if (bytes == -1 || bytes == 0) {
  control_remove(l);
  return;
 }
 buffer[bytes] = '\0';
 p = buffer;
 e = buffer + bytes;
 argc = 0;
 ap = argvp;
 while (p < e && (unsigned long)argc < sizeof(argvp)) {
  argc++;
  *ap++ = p;
  p += strlen(p) + 1;
 }
 handle_args(l, argc, argvp);
}

variable_types: bytes long

variable_types: argc int

variable_types:
e
char *

variable_types:
p
char *

variable_types:
ap
char **


decompiled_code:
int make_sock()
{
int result; // eax

result = socket(1, 1, 0);
fd = result;
if ( result != -1 )
{
memset(&sun, 0, 0x68uLL);
*((_DWORD *)&sun + 26) = 0;
word_48C = 0;
sun.sa_family = 1;
strlcpy(sun.sa_data, "/var/run/dhcpcd.sock", 108LL);
result = strlen(sun.sa_data) + 3;
}
return result;
}

canonical_decompiled_code:
int func3()
{
int result; // eax

result = socket(1, 1, 0);
fd = result;
if ( result != -1 )
{
memset(&sun, 0, 0x68uLL);
*((_DWORD *)&sun + 26) = 0;
word_48C = 0;
sun.sa_family = 1;
strlcpy(sun.sa_data, "/var/run/dhcpcd.sock", 108LL);
result = strlen(sun.sa_data) + 3;
}
return result;
}

original_code:
static int
make_sock(void)
{
 if ((fd = socket(1, SOCK_STREAM, 0)) == -1)
  return -1;
 memset(&sun, 0, sizeof(sun));
 sun.sun_family = 1;
 strlcpy(sun.sun_path, "/var/run" "/" "dhcpcd" ".sock", sizeof(sun.sun_path));
 return sizeof(sun.sun_family) + strlen(sun.sun_path) + 1;
}

canonical_original_code:
static int make_sock()
{
 if ((fd = socket(1, SOCK_STREAM, 0)) == -1)
  return -1;
 memset(&sun, 0, sizeof(sun));
 sun.sun_family = 1;
 strlcpy(sun.sun_path, "/var/run" "/" "dhcpcd" ".sock", sizeof(sun.sun_path));
 return sizeof(sun.sun_family) + strlen(sun.sun_path) + 1;
}


decompiled_code:
__int64 control_start()
{
unsigned int v0; // ebx
unsigned int v1; // ebp
unsigned int v2; // ebx

v0 = make_sock();
if ( v0 != -1 )
{
unlink("/var/run/dhcpcd.sock");
v1 = fd;
if ( bind(fd, &sun, v0) == -1
|| chmod("/var/run/dhcpcd.sock", 0x1B0u) == -1
|| (unsigned int)set_cloexec(v1) == -1
|| (unsigned int)set_nonblock((unsigned int)fd) == -1
|| (v2 = fd, listen(fd, 8) == -1) )
{
close(fd);
v0 = -1;
}
else
{
eloop_event_add(v2, control_handle);
v0 = fd;
}
}
return v0;
}

canonical_decompiled_code:
__int64 func4()
{
unsigned int v0; // ebx
unsigned int v1; // ebp
unsigned int v2; // ebx

v0 = func3();
if ( v0 != -1 )
{
unlink("/var/run/dhcpcd.sock");
v1 = fd;
if ( bind(fd, &sun, v0) == -1
|| chmod("/var/run/dhcpcd.sock", 0x1B0u) == -1
|| (unsigned int)set_cloexec(v1) == -1
|| (unsigned int)set_nonblock((unsigned int)fd) == -1
|| (v2 = fd, listen(fd, 8) == -1) )
{
close(fd);
v0 = -1;
}
else
{
eloop_event_add(v2, control_handle);
v0 = fd;
}
}
return v0;
}

original_code:
int
control_start(void)
{
 int len;
 if ((len = make_sock()) == -1)
  return -1;
 unlink("/var/run" "/" "dhcpcd" ".sock");
 if (bind(fd, (struct sockaddr *)&sun, len) == -1 ||
     chmod("/var/run" "/" "dhcpcd" ".sock",
  0400 | 0200 | (0400 >> 3) | (0200 >> 3)) == -1 ||
     set_cloexec(fd) == -1 ||
     set_nonblock(fd) == -1 ||
     listen(fd, sizeof(control_fds)) == -1)
 {
  close(fd);
  return -1;
 }
 eloop_event_add(fd, control_handle, ((void *)0));
 return fd;
}

canonical_original_code:
int control_start()
{
 int len;
 if ((len = make_sock()) == -1)
  return -1;
 unlink("/var/run" "/" "dhcpcd" ".sock");
 if (bind(fd, (struct sockaddr *)&sun, len) == -1 ||
     chmod("/var/run" "/" "dhcpcd" ".sock",
  0400 | 0200 | (0400 >> 3) | (0200 >> 3)) == -1 ||
     set_cloexec(fd) == -1 ||
     set_nonblock(fd) == -1 ||
     listen(fd, sizeof(control_fds)) == -1)
 {
  close(fd);
  return -1;
 }
 eloop_event_add(fd, control_handle, ((void *)0));
 return fd;
}

variable_types: len int


decompiled_code:
__int64 control_stop()
{
int v0; // ebx
unsigned int v1; // ebp
unsigned int *i; // rbx

eloop_event_delete((unsigned int)fd);
v0 = shutdown(fd, 2);
fd = -1;
v1 = unlink("/var/run/dhcpcd.sock");
if ( v1 != -1 )
v1 = v0 == -1;
for ( i = (unsigned int *)control_fds; control_fds; i = (unsigned int *)control_fds )
{
control_fds = (void *)*((_QWORD *)i + 1);
eloop_event_delete(*i);
shutdown(*i, 2);
free(i);
}
return v1;
}

canonical_decompiled_code:
__int64 func5()
{
int v0; // ebx
unsigned int v1; // ebp
unsigned int *i; // rbx

eloop_event_delete((unsigned int)fd);
v0 = shutdown(fd, 2);
fd = -1;
v1 = unlink("/var/run/dhcpcd.sock");
if ( v1 != -1 )
v1 = v0 == -1;
for ( i = (unsigned int *)control_fds; control_fds; i = (unsigned int *)control_fds )
{
control_fds = (void *)*((_QWORD *)i + 1);
eloop_event_delete(*i);
shutdown(*i, 2);
free(i);
}
return v1;
}

original_code:
int
control_stop(void)
{
 int retval = 0;
 struct fd_list *l;
 eloop_event_delete(fd);
 if (shutdown(fd, SHUT_RDWR) == -1)
  retval = 1;
 fd = -1;
 if (unlink("/var/run" "/" "dhcpcd" ".sock") == -1)
  retval = -1;
 l = control_fds;
 while (l != ((void *)0)) {
  control_fds = l->next;
  eloop_event_delete(l->fd);
  shutdown(l->fd, SHUT_RDWR);
  free(l);
  l = control_fds;
 }
 return retval;
}

canonical_original_code:
int control_stop()
{
 int retval = 0;
 struct fd_list *l;
 eloop_event_delete(fd);
 if (shutdown(fd, SHUT_RDWR) == -1)
  retval = 1;
 fd = -1;
 if (unlink("/var/run" "/" "dhcpcd" ".sock") == -1)
  retval = -1;
 l = control_fds;
 while (l != ((void *)0)) {
  control_fds = l->next;
  eloop_event_delete(l->fd);
  shutdown(l->fd, SHUT_RDWR);
  free(l);
  l = control_fds;
 }
 return retval;
}

variable_types: retval int


decompiled_code:
int control_open()
{
int result; // eax

result = make_sock();
if ( result != -1 )
result = connect(fd, &sun, result);
return result;
}

canonical_decompiled_code:
int func6()
{
int result; // eax

result = func3();
if ( result != -1 )
result = connect(fd, &sun, result);
return result;
}

original_code:
int
control_open(void)
{
 int len;
 if ((len = make_sock()) == -1)
  return -1;
 return connect(fd, (struct sockaddr *)&sun, len);
}

canonical_original_code:
int control_open()
{
 int len;
 if ((len = make_sock()) == -1)
  return -1;
 return connect(fd, (struct sockaddr *)&sun, len);
}


decompiled_code:
ssize_t __fastcall control_send(int a1, const char **a2)
{
char *v2; // rbp
const char **v3; // r12
const char *v4; // r13
size_t v5; // rbx

if ( a1 <= 255 )
{
v2 = buffer;
if ( a1 <= 0 )
return write(fd, buffer, v2 - buffer);
v3 = a2;
v2 = buffer;
while ( 1 )
{
v4 = *v3;
v5 = strlen(*v3) + 1;
if ( v5 + v2 - buffer > 0x400 )
break;
memcpy(v2, v4, v5);
v2 += v5;
if ( ++v3 == &a2[(unsigned int)(a1 - 1) + 1] )
return write(fd, buffer, v2 - buffer);
}
}
*_errno_location() = 105;
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
ssize_t __fastcall func7(int a1, const char **a2)
{
char *v2; // rbp
const char **v3; // r12
const char *v4; // r13
size_t v5; // rbx

if ( a1 <= 255 )
{
v2 = buffer;
if ( a1 <= 0 )
return write(fd, buffer, v2 - buffer);
v3 = a2;
v2 = buffer;
while ( 1 )
{
v4 = *v3;
v5 = strlen(*v3) + 1;
if ( v5 + v2 - buffer > 0x400 )
break;
memcpy(v2, v4, v5);
v2 += v5;
if ( ++v3 == &a2[(unsigned int)(a1 - 1) + 1] )
return write(fd, buffer, v2 - buffer);
}
}
*_errno_location() = 105;
return 0xFFFFFFFFLL;
}

original_code:
int
control_send(int argc, char * const *argv)
{
 char *p = buffer;
 int i;
 size_t len;
 if (argc > 255) {
  (*__errno_location ()) = 105;
  return -1;
 }
 for (i = 0; i < argc; i++) {
  len = strlen(argv[i]) + 1;
  if ((p - buffer) + len > sizeof(buffer)) {
   (*__errno_location ()) = 105;
   return -1;
  }
  memcpy(p, argv[i], len);
  p += len;
 }
 return write(fd, buffer, p - buffer);
}

canonical_original_code:
int control_send(int argc, char **argv)
{
 char *p = buffer;
 int i;
 unsigned long len;
 if (argc > 255) {
  (*__errno_location ()) = 105;
  return -1;
 }
 for (i = 0; i < argc; i++) {
  len = strlen(argv[i]) + 1;
  if ((p - buffer) + len > sizeof(buffer)) {
   (*__errno_location ()) = 105;
   return -1;
  }
  memcpy(p, argv[i], len);
  p += len;
 }
 return write(fd, buffer, p - buffer);
}

variable_types:
argv
char **

variable_types: i int

variable_types:
len
unsigned long


serverSocket

decompiled_code:
__int64 __fastcall serverSocket(__int16 a1, const char *a2)
{
int v2; // eax
unsigned int v3; // ebx
int *v5; // rax
char v6[16]; // [rsp+0h] [rbp-58h] BYREF
__int64 v7; // [rsp+10h] [rbp-48h]
__int64 v8; // [rsp+18h] [rbp-40h]
__int64 v9; // [rsp+20h] [rbp-38h]
int optval; // [rsp+2Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+30h] [rbp-28h] BYREF

optval = 1;
v2 = socket(2, 2, 17);
v3 = v2;
if ( v2 != -1 )
{
if ( setsockopt(v2, 1, 2, &optval, 4u) == -1 )
{
v3 = -1;
}
else
{
v7 = 0LL;
v8 = 0LL;
v9 = 0LL;
strncpy(v6, a2, 0x10uLL);
if ( setsockopt(v3, 1, 25, v6, 0x28u) < 0 )
{
v5 = _errno_location();
syslog(6, "setsockopt(SO_BINDTODEVICE) %d\n", (unsigned int)*v5);
}
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = _bswap_16(a1);
if ( bind(v3, &addr, 0x10u) == -1 )
v3 = -1;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int16 a1, const char *a2)
{
int v2; // eax
unsigned int v3; // ebx
int *v5; // rax
char v6[16]; // [rsp+0h] [rbp-58h] BYREF
__int64 v7; // [rsp+10h] [rbp-48h]
__int64 v8; // [rsp+18h] [rbp-40h]
__int64 v9; // [rsp+20h] [rbp-38h]
int optval; // [rsp+2Ch] [rbp-2Ch] BYREF
struct sockaddr addr; // [rsp+30h] [rbp-28h] BYREF

optval = 1;
v2 = socket(2, 2, 17);
v3 = v2;
if ( v2 != -1 )
{
if ( setsockopt(v2, 1, 2, &optval, 4u) == -1 )
{
v3 = -1;
}
else
{
v7 = 0LL;
v8 = 0LL;
v9 = 0LL;
strncpy(v6, a2, 0x10uLL);
if ( setsockopt(v3, 1, 25, v6, 0x28u) < 0 )
{
v5 = _errno_location();
syslog(6, "setsockopt(SO_BINDTODEVICE) %d\n", (unsigned int)*v5);
}
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = func0(a1);
if ( bind(v3, &addr, 0x10u) == -1 )
v3 = -1;
}
}
return v3;
}

original_code:
int serverSocket(short listen_port, char *interface_name) {
        int server_socket;
        struct sockaddr_in server;
        int n = 1;
        server_socket = socket(2, SOCK_DGRAM, IPPROTO_UDP);
        if(server_socket == -1)
     return -1;
        if(setsockopt(server_socket, 1, 2, (char *) &n, sizeof(n)) == -1)
  return -1;
 {
  struct ifreq intf;
  bzero(&intf, sizeof(intf));
  strncpy(intf.ifr_ifrn.ifrn_name, interface_name, 16);
  if (setsockopt(server_socket, 1, 25,
    &intf, sizeof(intf)) < 0)
   syslog(6, "setsockopt(SO_BINDTODEVICE) %d\n", (*__errno_location ()));
 }
        bzero(&server, sizeof(server));
        server.sin_family = 2;
        server.sin_port = htons(listen_port);
        server.sin_addr.s_addr = ((in_addr_t) 0x00000000);
        if(bind(server_socket, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1)
                return -1;
        return server_socket;
}

canonical_original_code:
int serverSocket(short listen_port, char *interface_name) {
        int server_socket;
        struct sockaddr_in server;
        int n = 1;
        server_socket = socket(2, SOCK_DGRAM, IPPROTO_UDP);
        if(server_socket == -1)
     return -1;
        if(setsockopt(server_socket, 1, 2, (char *) &n, sizeof(n)) == -1)
  return -1;
 {
  struct ifreq intf;
  bzero(&intf, sizeof(intf));
  strncpy(intf.ifr_ifrn.ifrn_name, interface_name, 16);
  if (setsockopt(server_socket, 1, 25,
    &intf, sizeof(intf)) < 0)
   syslog(6, "setsockopt(SO_BINDTODEVICE) %d\n", (*__errno_location ()));
 }
        bzero(&server, sizeof(server));
        server.sin_family = 2;
        server.sin_port = htons(listen_port);
        server.sin_addr.s_addr = ((unsigned int) 0x00000000);
        if(bind(server_socket, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1)
                return -1;
        return server_socket;
}

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 ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr { unsigned short sa_family; char sa_data[14]; } ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; } ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }

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 ifreq { union { char ifrn_name[16]; } ifr_ifrn; union { struct sockaddr ifru_addr; struct sockaddr ifru_dstaddr; struct sockaddr ifru_broadaddr; struct sockaddr ifru_netmask; struct sockaddr ifru_hwaddr; short ifru_flags; int ifru_ivalue; int ifru_mtu; struct ifmap ifru_map; char ifru_slave[16]; char ifru_newname[16]; char *ifru_data; } ifr_ifru; }


clientSocket

decompiled_code:
__int64 __fastcall clientSocket(__int16 a1, __int16 a2, const char *a3)
{
int v4; // eax
unsigned int v5; // ebx
int *v7; // rax
char v8[16]; // [rsp+0h] [rbp-78h] BYREF
__int64 v9; // [rsp+10h] [rbp-68h]
__int64 v10; // [rsp+18h] [rbp-60h]
__int64 v11; // [rsp+20h] [rbp-58h]
struct sockaddr addr; // [rsp+30h] [rbp-48h] BYREF
int optval[11]; // [rsp+4Ch] [rbp-2Ch] BYREF

optval[0] = 1;
v4 = socket(2, 2, 17);
v5 = v4;
if ( v4 != -1 )
{
if ( setsockopt(v4, 1, 2, optval, 4u) == -1 )
{
v5 = -1;
}
else
{
setsockopt(v5, 1, 6, optval, 4u);
v9 = 0LL;
v10 = 0LL;
v11 = 0LL;
strncpy(v8, a3, 0x10uLL);
if ( setsockopt(v5, 1, 25, v8, 0x28u) < 0 )
{
v7 = _errno_location();
syslog(6, "setsockopt(SO_BINDTODEVICE) %d\n", (unsigned int)*v7);
}
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = _bswap_16(a1);
if ( bind(v5, &addr, 0x10u) == -1 )
{
v5 = -1;
}
else
{
*(_QWORD *)&addr.sa_data[6] = 0LL;
addr.sa_family = 2;
*(_WORD *)addr.sa_data = _bswap_16(a2);
*(_DWORD *)&addr.sa_data[2] = -1;
if ( connect(v5, &addr, 0x10u) == -1 )
v5 = -1;
}
}
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int16 a1, __int16 a2, const char *a3)
{
int v4; // eax
unsigned int v5; // ebx
int *v7; // rax
char v8[16]; // [rsp+0h] [rbp-78h] BYREF
__int64 v9; // [rsp+10h] [rbp-68h]
__int64 v10; // [rsp+18h] [rbp-60h]
__int64 v11; // [rsp+20h] [rbp-58h]
struct sockaddr addr; // [rsp+30h] [rbp-48h] BYREF
int optval[11]; // [rsp+4Ch] [rbp-2Ch] BYREF

optval[0] = 1;
v4 = socket(2, 2, 17);
v5 = v4;
if ( v4 != -1 )
{
if ( setsockopt(v4, 1, 2, optval, 4u) == -1 )
{
v5 = -1;
}
else
{
setsockopt(v5, 1, 6, optval, 4u);
v9 = 0LL;
v10 = 0LL;
v11 = 0LL;
strncpy(v8, a3, 0x10uLL);
if ( setsockopt(v5, 1, 25, v8, 0x28u) < 0 )
{
v7 = _errno_location();
syslog(6, "setsockopt(SO_BINDTODEVICE) %d\n", (unsigned int)*v7);
}
*(_QWORD *)&addr.sa_family = 2LL;
*(_QWORD *)&addr.sa_data[6] = 0LL;
*(_WORD *)addr.sa_data = func0(a1);
if ( bind(v5, &addr, 0x10u) == -1 )
{
v5 = -1;
}
else
{
*(_QWORD *)&addr.sa_data[6] = 0LL;
addr.sa_family = 2;
*(_WORD *)addr.sa_data = func0(a2);
*(_DWORD *)&addr.sa_data[2] = -1;
if ( connect(v5, &addr, 0x10u) == -1 )
v5 = -1;
}
}
}
return v5;
}

original_code:
int clientSocket(short send_from_port, short send_to_port, char *interface_name) {
        int n = 1;
        int client_socket;
        struct sockaddr_in client;
        client_socket = socket(2, SOCK_DGRAM, IPPROTO_UDP);
        if(client_socket == -1)
                return -1;
        if (setsockopt(client_socket, 1, 2, (char *) &n, sizeof(n)) == -1)
  return -1;
        setsockopt(client_socket, 1, 6, (char *) &n, sizeof(n));
 {
  struct ifreq intf;
  bzero(&intf, sizeof(intf));
  strncpy(intf.ifr_ifrn.ifrn_name, interface_name, 16);
  if (setsockopt(client_socket, 1, 25,
    &intf, sizeof(intf)) < 0)
   syslog(6, "setsockopt(SO_BINDTODEVICE) %d\n", (*__errno_location ()));
 }
        bzero(&client, sizeof(client));
        client.sin_family = 2;
        client.sin_port = htons(send_from_port);
        client.sin_addr.s_addr = ((in_addr_t) 0x00000000);
        if(bind(client_socket,(struct sockaddr *)&client, sizeof(struct sockaddr))==-1)
                return -1;
        bzero(&client, sizeof(client));
        client.sin_family = 2;
        client.sin_port = htons(send_to_port);
        client.sin_addr.s_addr = ((in_addr_t) 0xffffffff);
        if(connect(client_socket, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1)
                return -1;
        return client_socket;
}

canonical_original_code:
int clientSocket(short send_from_port, short send_to_port, char *interface_name) {
        int n = 1;
        int client_socket;
        struct sockaddr_in client;
        client_socket = socket(2, SOCK_DGRAM, IPPROTO_UDP);
        if(client_socket == -1)
                return -1;
        if (setsockopt(client_socket, 1, 2, (char *) &n, sizeof(n)) == -1)
  return -1;
        setsockopt(client_socket, 1, 6, (char *) &n, sizeof(n));
 {
  struct ifreq intf;
  bzero(&intf, sizeof(intf));
  strncpy(intf.ifr_ifrn.ifrn_name, interface_name, 16);
  if (setsockopt(client_socket, 1, 25,
    &intf, sizeof(intf)) < 0)
   syslog(6, "setsockopt(SO_BINDTODEVICE) %d\n", (*__errno_location ()));
 }
        bzero(&client, sizeof(client));
        client.sin_family = 2;
        client.sin_port = htons(send_from_port);
        client.sin_addr.s_addr = ((unsigned int) 0x00000000);
        if(bind(client_socket,(struct sockaddr *)&client, sizeof(struct sockaddr))==-1)
                return -1;
        bzero(&client, sizeof(client));
        client.sin_family = 2;
        client.sin_port = htons(send_to_port);
        client.sin_addr.s_addr = ((unsigned int) 0xffffffff);
        if(connect(client_socket, (struct sockaddr *)&client, sizeof(struct sockaddr)) == -1)
                return -1;
        return client_socket;
}

variable_types: n int

user_defined_types: struct ifmap { unsigned long mem_start; unsigned long mem_end; unsigned short base_addr; unsigned char irq; unsigned char dma; unsigned char port; }


decompiled_code:
__int64 __fastcall valid_dhcp_packet(__int64 a1)
{
__int16 v1; // r13
__int16 v2; // r12
__int16 v3; // bp
int v4; // eax
__int64 result; // rax
__int16 v6; // ax

v1 = *(_WORD *)(a1 + 2);
v2 = *(_WORD *)(a1 + 10);
v3 = *(_WORD *)(a1 + 26);
*(_WORD *)(a1 + 10) = 0;
if ( (unsigned __int16)checksum((unsigned __int8 *)a1, 0x14u) == v2 )
{
v4 = *(_DWORD *)(a1 + 12);
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
*(_WORD *)(a1 + 26) = 0;
*(_BYTE *)(a1 + 9) = 17;
*(_DWORD *)(a1 + 12) = v4;
*(_WORD *)(a1 + 2) = *(_WORD *)(a1 + 24);
result = 0LL;
if ( v3 )
{
v6 = _bswap_16(v1);
if ( (unsigned __int16)checksum((unsigned __int8 *)a1, v6) == v3 )
{
result = 0LL;
}
else
{
logger(3LL, "bad UDP checksum, ignoring");
result = 0xFFFFFFFFLL;
}
}
}
else
{
logger(7LL, "bad IP header checksum, ignoring");
result = 0xFFFFFFFFLL;
}
*(_WORD *)(a1 + 10) = v2;
*(_WORD *)(a1 + 2) = v1;
*(_WORD *)(a1 + 26) = v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
__int16 v1; // r13
__int16 v2; // r12
__int16 v3; // bp
int v4; // eax
__int64 result; // rax
__int16 v6; // ax

v1 = *(_WORD *)(a1 + 2);
v2 = *(_WORD *)(a1 + 10);
v3 = *(_WORD *)(a1 + 26);
*(_WORD *)(a1 + 10) = 0;
if ( (unsigned __int16)func2((unsigned __int8 *)a1, 0x14u) == v2 )
{
v4 = *(_DWORD *)(a1 + 12);
*(_QWORD *)a1 = 0LL;
*(_QWORD *)(a1 + 8) = 0LL;
*(_WORD *)(a1 + 26) = 0;
*(_BYTE *)(a1 + 9) = 17;
*(_DWORD *)(a1 + 12) = v4;
*(_WORD *)(a1 + 2) = *(_WORD *)(a1 + 24);
result = 0LL;
if ( v3 )
{
v6 = func1(v1);
if ( (unsigned __int16)func2((unsigned __int8 *)a1, v6) == v3 )
{
result = 0LL;
}
else
{
logger(3LL, "bad UDP checksum, ignoring");
result = 0xFFFFFFFFLL;
}
}
}
else
{
logger(7LL, "bad IP header checksum, ignoring");
result = 0xFFFFFFFFLL;
}
*(_WORD *)(a1 + 10) = v2;
*(_WORD *)(a1 + 2) = v1;
*(_WORD *)(a1 + 26) = v3;
return result;
}

original_code:
static int valid_dhcp_packet (unsigned char * data)
{
  struct udp_dhcp_packet *packet = (struct udp_dhcp_packet *) data;
  uint16_t bytes = ntohs (packet->ip.ip_len);
  uint16_t ipsum = packet->ip.ip_sum;
  uint16_t iplen = packet->ip.ip_len;
  uint16_t udpsum = packet->udp.uh_sum;
  struct in_addr source;
  struct in_addr dest;
  int retval = 0;
  packet->ip.ip_sum = 0;
  if (ipsum != checksum ((unsigned char *) &packet->ip, sizeof (struct ip)))
    {
      logger (7, "bad IP header checksum, ignoring");
      retval = -1;
      goto eexit;
    }
  memcpy (&source, &packet->ip.ip_src, sizeof (struct in_addr));
  memcpy (&dest, &packet->ip.ip_dst, sizeof (struct in_addr));
  memset (&packet->ip, 0, sizeof (struct ip));
  packet->udp.uh_sum = 0;
  packet->ip.ip_p = IPPROTO_UDP;
  memcpy (&packet->ip.ip_src, &source, sizeof (struct in_addr));
  memcpy (&packet->ip.ip_dst, &dest, sizeof (struct in_addr));
  packet->ip.ip_len = packet->udp.uh_ulen;
  if (udpsum && udpsum != checksum ((unsigned char *) packet, bytes))
    {
      logger (3, "bad UDP checksum, ignoring");
      retval = -1;
    }
eexit:
  packet->ip.ip_sum = ipsum;
  packet->ip.ip_len = iplen;
  packet->udp.uh_sum = udpsum;
  return retval;
}

canonical_original_code:
static int valid_dhcp_packet(unsigned char *data)
{
  struct udp_dhcp_packet *packet = (struct udp_dhcp_packet *) data;
  unsigned short bytes = ntohs (packet->ip.ip_len);
  unsigned short ipsum = packet->ip.ip_sum;
  unsigned short iplen = packet->ip.ip_len;
  unsigned short udpsum = packet->udp.uh_sum;
  struct in_addr source;
  struct in_addr dest;
  int retval = 0;
  packet->ip.ip_sum = 0;
  if (ipsum != checksum ((unsigned char *) &packet->ip, sizeof (struct ip)))
    {
      logger (7, "bad IP header checksum, ignoring");
      retval = -1;
      goto eexit;
    }
  memcpy (&source, &packet->ip.ip_src, sizeof (struct in_addr));
  memcpy (&dest, &packet->ip.ip_dst, sizeof (struct in_addr));
  memset (&packet->ip, 0, sizeof (struct ip));
  packet->udp.uh_sum = 0;
  packet->ip.ip_p = IPPROTO_UDP;
  memcpy (&packet->ip.ip_src, &source, sizeof (struct in_addr));
  memcpy (&packet->ip.ip_dst, &dest, sizeof (struct in_addr));
  packet->ip.ip_len = packet->udp.uh_ulen;
  if (udpsum && udpsum != checksum ((unsigned char *) packet, bytes))
    {
      logger (3, "bad UDP checksum, ignoring");
      retval = -1;
    }
eexit:
  packet->ip.ip_sum = ipsum;
  packet->ip.ip_len = iplen;
  packet->udp.uh_sum = udpsum;
  return retval;
}

variable_types:
data
unsigned char *

variable_types: struct udp_dhcp_packet { struct ip { unsigned int ip_hl; unsigned int ip_v; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short ip_sum; struct in_addr { unsigned int s_addr; } ip_src; } ip; struct udphdr { union { struct { unsigned short uh_sport; unsigned short uh_dport; unsigned short uh_ulen; unsigned short uh_sum; }; struct { unsigned short source; unsigned short dest; unsigned short len; unsigned short check; }; }; } udp; struct dhcpmessage_t { char op; char hwtype; char hwlen; char hwopcount; int xid; short secs; short flags; int ciaddr; int yiaddr; int siaddr; int giaddr; unsigned char hwaddr[16]; char servername[64]; char bootfile[128]; unsigned int cookie; unsigned char options[308]; } dhcp; } *

variable_types:
bytes
unsigned short

variable_types:
ipsum
unsigned short

variable_types:
iplen
unsigned short

variable_types:
udpsum
unsigned short

variable_types:
source
struct in_addr { unsigned int s_addr; }

variable_types:
dest
struct in_addr { unsigned int s_addr; }

user_defined_types: struct ip { unsigned int ip_hl; unsigned int ip_v; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short ip_sum; struct in_addr ip_src; }

user_defined_types: struct udp_dhcp_packet { struct ip ip; struct udphdr udp; struct dhcpmessage_t dhcp; }


original_code:
void make_dhcp_packet(struct udp_dhcp_packet *packet,
        unsigned char *data,
        struct in_addr source, struct in_addr dest)
{
  struct ip *ip = &packet->ip;
  struct udphdr *udp = &packet->udp;
  memcpy (&packet->dhcp, data, sizeof (dhcpmessage_t));
  ip->ip_p = IPPROTO_UDP;
  ip->ip_src.s_addr = htonl (source.s_addr);
  if (dest.s_addr == 0)
    ip->ip_dst.s_addr = htonl (((in_addr_t) 0xffffffff));
  else
    ip->ip_dst.s_addr = htonl (dest.s_addr);
  udp->uh_sport = htons (68);
  udp->uh_dport = htons (67);
  udp->uh_ulen = htons (sizeof (struct udphdr) + sizeof (struct dhcpmessage_t));
  ip->ip_len = udp->uh_ulen;
  udp->uh_sum = checksum ((unsigned char *) packet,
     sizeof (struct udp_dhcp_packet));
  ip->ip_v = 4;
  ip->ip_hl = 5;
  ip->ip_id = 0;
  ip->ip_tos = 0x10;
  ip->ip_len = htons (sizeof (struct ip) + sizeof (struct udphdr) +
        sizeof (struct dhcpmessage_t));
  ip->ip_id = 0;
  ip->ip_off = 0;
  ip->ip_ttl = 64;
  ip->ip_sum = checksum ((unsigned char *) ip, sizeof (struct ip));
}

canonical_original_code:
void make_dhcp_packet(struct udp_dhcp_packet *packet, unsigned char *data, struct in_addr source, struct in_addr dest)
{
  struct ip *ip = &packet->ip;
  struct udphdr *udp = &packet->udp;
  memcpy (&packet->dhcp, data, sizeof (dhcpmessage_t));
  ip->ip_p = IPPROTO_UDP;
  ip->ip_src.s_addr = htonl (source.s_addr);
  if (dest.s_addr == 0)
    ip->ip_dst.s_addr = htonl (((unsigned int) 0xffffffff));
  else
    ip->ip_dst.s_addr = htonl (dest.s_addr);
  udp->uh_sport = htons (68);
  udp->uh_dport = htons (67);
  udp->uh_ulen = htons (sizeof (struct udphdr) + sizeof (struct dhcpmessage_t));
  ip->ip_len = udp->uh_ulen;
  udp->uh_sum = checksum ((unsigned char *) packet,
     sizeof (struct udp_dhcp_packet));
  ip->ip_v = 4;
  ip->ip_hl = 5;
  ip->ip_id = 0;
  ip->ip_tos = 0x10;
  ip->ip_len = htons (sizeof (struct ip) + sizeof (struct udphdr) +
        sizeof (struct dhcpmessage_t));
  ip->ip_id = 0;
  ip->ip_off = 0;
  ip->ip_ttl = 64;
  ip->ip_sum = checksum ((unsigned char *) ip, sizeof (struct ip));
}

variable_types:
source
struct in_addr

variable_types:
dest
struct in_addr

variable_types: struct ip { unsigned int ip_hl; unsigned int ip_v; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short ip_sum; struct in_addr { unsigned int s_addr; } ip_src; } *

variable_types: struct udphdr { union { struct { unsigned short uh_sport; unsigned short uh_dport; unsigned short uh_ulen; unsigned short uh_sum; }; struct { unsigned short source; unsigned short dest; unsigned short len; unsigned short check; }; }; } *

user_defined_types: struct dhcpmessage_t { char op; char hwtype; char hwlen; char hwopcount; int xid; short secs; short flags; int ciaddr; int yiaddr; int siaddr; int giaddr; unsigned char hwaddr[16]; char servername[64]; char bootfile[128]; unsigned int cookie; unsigned char options[308]; }


decompiled_code:
__int64 __fastcall open_socket(char *ifname, char a2)
{
unsigned __int16 v3; // r12
int v4; // eax
unsigned int v5; // ebx
int v6; // eax
int v7; // edi
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
int *v13; // rax
char *v14; // rax
__int64 v15; // [rsp+0h] [rbp-48h] BYREF
_BYTE v16[12]; // [rsp+8h] [rbp-40h]

v3 = _bswap_16(2048);
v4 = socket(17, 2, v3);
v5 = v4;
if ( v4 == -1 )
{
v9 = _errno_location();
v10 = strerror(*v9);
logger(3LL, "socket: %s", v10);
}
else
{
v6 = fcntl(v4, 1, 0LL);
if ( v6 < 0 || fcntl(v5, 2, v6 | 1u) < 0 )
{
v11 = _errno_location();
v12 = strerror(*v11);
logger(3LL, "fcntl: %s", v12);
close(v5);
v5 = -1;
}
else
{
v15 = 17LL;
*(_QWORD *)v16 = 0LL;
*(_DWORD *)&v16[8] = 0;
if ( a2 )
v3 = _bswap_16(2054);
WORD1(v15) = v3;
HIDWORD(v15) = if_nametoindex(ifname);
v16[3] = 6;
*(_QWORD *)&v16[4] = -1LL;
if ( bind(v5, (const struct sockaddr *)&v15, 0x14u) == -1 )
{
v13 = _errno_location();
v14 = strerror(*v13);
logger(3LL, "bind: %s", v14);
close(v5);
v5 = -1;
}
else
{
v7 = *((_DWORD *)ifname + 6);
if ( v7 >= 0 )
close(v7);
*((_DWORD *)ifname + 6) = v5;
*((_DWORD *)ifname + 8) = (unsigned __int16)_bswap_16(SWORD1(v15));
*((_DWORD *)ifname + 7) = 4096;
}
}
}
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func5(char *ifname, char a2)
{
unsigned __int16 v3; // r12
int v4; // eax
unsigned int v5; // ebx
int v6; // eax
int v7; // edi
int *v9; // rax
char *v10; // rax
int *v11; // rax
char *v12; // rax
int *v13; // rax
char *v14; // rax
__int64 v15; // [rsp+0h] [rbp-48h] BYREF
_BYTE v16[12]; // [rsp+8h] [rbp-40h]

v3 = func1(2048);
v4 = socket(17, 2, v3);
v5 = v4;
if ( v4 == -1 )
{
v9 = _errno_location();
v10 = strerror(*v9);
logger(3LL, "socket: %s", v10);
}
else
{
v6 = fcntl(v4, 1, 0LL);
if ( v6 < 0 || fcntl(v5, 2, v6 | 1u) < 0 )
{
v11 = _errno_location();
v12 = strerror(*v11);
logger(3LL, "fcntl: %s", v12);
close(v5);
v5 = -1;
}
else
{
v15 = 17LL;
*(_QWORD *)v16 = 0LL;
*(_DWORD *)&v16[8] = 0;
if ( a2 )
v3 = func1(2054);
WORD1(v15) = v3;
HIDWORD(v15) = if_nametoindex(ifname);
v16[3] = 6;
*(_QWORD *)&v16[4] = -1LL;
if ( bind(v5, (const struct sockaddr *)&v15, 0x14u) == -1 )
{
v13 = _errno_location();
v14 = strerror(*v13);
logger(3LL, "bind: %s", v14);
close(v5);
v5 = -1;
}
else
{
v7 = *((_DWORD *)ifname + 6);
if ( v7 >= 0 )
close(v7);
*((_DWORD *)ifname + 6) = v5;
*((_DWORD *)ifname + 8) = (unsigned __int16)func1(SWORD1(v15));
*((_DWORD *)ifname + 7) = 4096;
}
}
}
return v5;
}

original_code:
int open_socket (interface_t *iface, _Bool arp)
{
  int fd;
  int flags;
  struct sockaddr_ll sll;
  if ((fd = socket (17, SOCK_DGRAM, htons (0x0800))) == -1)
    {
      logger (3, "socket: %s", strerror ((*__errno_location ())));
      return -1;
    }
  if ((flags = fcntl (fd, 1, 0)) < 0
      || fcntl (fd, 2, flags | 1) < 0)
    {
      logger (3, "fcntl: %s", strerror ((*__errno_location ())));
      close (fd);
      return -1;
    }
  memset (&sll, 0, sizeof (struct sockaddr_ll));
  sll.sll_family = 17;
  if (arp)
    sll.sll_protocol = htons (0x0806);
  else
    sll.sll_protocol = htons (0x0800);
  sll.sll_ifindex = if_nametoindex (iface->name);
  sll.sll_halen = 6;
  memset(sll.sll_addr, 0xff, sizeof (sll.sll_addr));
  if (bind(fd, (struct sockaddr *) &sll, sizeof (struct sockaddr_ll)) == -1)
    {
      logger (3, "bind: %s", strerror ((*__errno_location ())));
      close (fd);
      return -1;
    }
  if (iface->fd > -1)
    close (iface->fd);
  iface->fd = fd;
  iface->socket_protocol = ntohs (sll.sll_protocol);
  iface->buffer_length = 4096;
  return fd;
}

canonical_original_code:
int open_socket(struct interface_t *iface, _Bool arp)
{
  int fd;
  int flags;
  struct sockaddr_ll sll;
  if ((fd = socket (17, SOCK_DGRAM, htons (0x0800))) == -1)
    {
      logger (3, "socket: %s", strerror ((*__errno_location ())));
      return -1;
    }
  if ((flags = fcntl (fd, 1, 0)) < 0
      || fcntl (fd, 2, flags | 1) < 0)
    {
      logger (3, "fcntl: %s", strerror ((*__errno_location ())));
      close (fd);
      return -1;
    }
  memset (&sll, 0, sizeof (struct sockaddr_ll));
  sll.sll_family = 17;
  if (arp)
    sll.sll_protocol = htons (0x0806);
  else
    sll.sll_protocol = htons (0x0800);
  sll.sll_ifindex = if_nametoindex (iface->name);
  sll.sll_halen = 6;
  memset(sll.sll_addr, 0xff, sizeof (sll.sll_addr));
  if (bind(fd, (struct sockaddr *) &sll, sizeof (struct sockaddr_ll)) == -1)
    {
      logger (3, "bind: %s", strerror ((*__errno_location ())));
      close (fd);
      return -1;
    }
  if (iface->fd > -1)
    close (iface->fd);
  iface->fd = fd;
  iface->socket_protocol = ntohs (sll.sll_protocol);
  iface->buffer_length = 4096;
  return fd;
}

variable_types: struct interface_t { char name[16]; struct ether_addr { unsigned char ether_addr_octet[6]; } ethernet_address; _Bool arpable; int fd; int buffer_length; int socket_protocol; char infofile[4096]; struct in_addr { unsigned int s_addr; } previous_address; struct route_t { struct in_addr destination; struct in_addr netmask; struct in_addr gateway; struct route_t *next; } *previous_routes; } *

variable_types: fd int

variable_types: flags int

variable_types: struct sockaddr_ll { unsigned short sll_family; unsigned short sll_protocol; int sll_ifindex; unsigned short sll_hatype; unsigned char sll_pkttype; unsigned char sll_halen; unsigned char sll_addr[8]; }

user_defined_types: struct interface_t { char name[16]; struct ether_addr ethernet_address; _Bool arpable; int fd; int buffer_length; int socket_protocol; char infofile[4096]; struct in_addr previous_address; struct route_t *previous_routes; }


decompiled_code:
__int64 __fastcall send_packet(char *ifname, __int16 a2, const void *a3, int a4)
{
int v6; // ebx
int *v8; // rax
char *v9; // rax
__int64 v10; // [rsp+0h] [rbp-38h] BYREF
int v11; // [rsp+8h] [rbp-30h]
__int64 v12; // [rsp+Ch] [rbp-2Ch]

if ( ifname )
{
LOWORD(v10) = 17;
v11 = 100663296;
WORD1(v10) = _bswap_16(a2);
HIDWORD(v10) = if_nametoindex(ifname);
v12 = -1LL;
v6 = sendto(*((_DWORD *)ifname + 6), a3, a4, 0, (const struct sockaddr *)&v10, 0x14u);
if ( v6 < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
logger(3LL, "sendto: %s", v9);
}
}
else
{
v6 = -1;
}
return (unsigned int)v6;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *ifname, __int16 a2, const void *a3, int a4)
{
int v6; // ebx
int *v8; // rax
char *v9; // rax
__int64 v10; // [rsp+0h] [rbp-38h] BYREF
int v11; // [rsp+8h] [rbp-30h]
__int64 v12; // [rsp+Ch] [rbp-2Ch]

if ( ifname )
{
LOWORD(v10) = 17;
v11 = 100663296;
WORD1(v10) = func1(a2);
HIDWORD(v10) = if_nametoindex(ifname);
v12 = -1LL;
v6 = sendto(*((_DWORD *)ifname + 6), a3, a4, 0, (const struct sockaddr *)&v10, 0x14u);
if ( v6 < 0 )
{
v8 = _errno_location();
v9 = strerror(*v8);
logger(3LL, "sendto: %s", v9);
}
}
else
{
v6 = -1;
}
return (unsigned int)v6;
}

original_code:
int send_packet (interface_t *iface, int type, unsigned char *data, int len)
{
  struct sockaddr_ll sll;
  int retval;
  if (! iface)
    return -1;
  memset (&sll, 0, sizeof (struct sockaddr_ll));
  sll.sll_family = 17;
  sll.sll_protocol = htons (type);
  sll.sll_ifindex = if_nametoindex (iface->name);
  sll.sll_halen = 6;
  memset(sll.sll_addr, 0xff, sizeof (sll.sll_addr));
  if ((retval = sendto (iface->fd, data, len, 0, (struct sockaddr *) &sll,
   sizeof (struct sockaddr_ll))) < 0)
    logger (3, "sendto: %s", strerror ((*__errno_location ())));
  return retval;
}

canonical_original_code:
int send_packet(struct interface_t *iface, int type, unsigned char *data, int len)
{
  struct sockaddr_ll sll;
  int retval;
  if (! iface)
    return -1;
  memset (&sll, 0, sizeof (struct sockaddr_ll));
  sll.sll_family = 17;
  sll.sll_protocol = htons (type);
  sll.sll_ifindex = if_nametoindex (iface->name);
  sll.sll_halen = 6;
  memset(sll.sll_addr, 0xff, sizeof (sll.sll_addr));
  if ((retval = sendto (iface->fd, data, len, 0, (struct sockaddr *) &sll,
   sizeof (struct sockaddr_ll))) < 0)
    logger (3, "sendto: %s", strerror ((*__errno_location ())));
  return retval;
}

variable_types: type int

variable_types: len int

variable_types: retval int

user_defined_types: struct sockaddr_ll { unsigned short sll_family; unsigned short sll_protocol; int sll_ifindex; unsigned short sll_hatype; unsigned char sll_pkttype; unsigned char sll_halen; unsigned char sll_addr[8]; }

user_defined_types: struct ether_addr { unsigned char ether_addr_octet[6]; }

user_defined_types: struct route_t { struct in_addr destination; struct in_addr netmask; struct in_addr gateway; struct route_t *next; }


original_code:
size_t get_packet (interface_t *iface, unsigned char *data,
     unsigned char *buffer, int *buffer_len, int *buffer_pos)
{
  long bytes;
  *buffer_pos = 0;
  memset (buffer, 0, iface->buffer_length);
  bytes = read (iface->fd, buffer, iface->buffer_length);
  if (bytes < 0)
    {
      logger (3, "read: %s", strerror ((*__errno_location ())));
      return -1;
    }
  *buffer_len = bytes;
  if (iface->socket_protocol == 0x0806)
    {
      memcpy (data, buffer, bytes);
      return bytes;
    }
  if (bytes < (sizeof (struct ip) + sizeof (struct udphdr)))
    {
      logger (7, "message too short, ignoring");
      return -1;
    }
  struct udp_dhcp_packet *dhcp = (struct udp_dhcp_packet *) buffer;
  if (bytes < ntohs (dhcp->ip.ip_len))
    {
      logger (7, "truncated packet, ignoring");
      return -1;
    }
  bytes = ntohs (dhcp->ip.ip_len);
  if (dhcp->ip.ip_p != IPPROTO_UDP || dhcp->ip.ip_v != 4 ||
      dhcp->ip.ip_hl != sizeof (dhcp->ip) >> 2 ||
      dhcp->udp.uh_dport != htons (68) ||
      bytes > (int) sizeof (struct udp_dhcp_packet) ||
      ntohs (dhcp->udp.uh_ulen) != (uint16_t) (bytes - sizeof (dhcp->ip)))
    {
      return -1;
    }
  if (valid_dhcp_packet (buffer) < 0)
    return -1;
  memcpy(data, &dhcp->dhcp, bytes - (sizeof (dhcp->ip) +
         sizeof (dhcp->udp)));
  return bytes - (sizeof (dhcp->ip) + sizeof (dhcp->udp));
}

canonical_original_code:
unsigned long get_packet(struct interface_t *iface, unsigned char *data, unsigned char *buffer, int *buffer_len, int *buffer_pos)
{
  long bytes;
  *buffer_pos = 0;
  memset (buffer, 0, iface->buffer_length);
  bytes = read (iface->fd, buffer, iface->buffer_length);
  if (bytes < 0)
    {
      logger (3, "read: %s", strerror ((*__errno_location ())));
      return -1;
    }
  *buffer_len = bytes;
  if (iface->socket_protocol == 0x0806)
    {
      memcpy (data, buffer, bytes);
      return bytes;
    }
  if (bytes < (sizeof (struct ip) + sizeof (struct udphdr)))
    {
      logger (7, "message too short, ignoring");
      return -1;
    }
  struct udp_dhcp_packet *dhcp = (struct udp_dhcp_packet *) buffer;
  if (bytes < ntohs (dhcp->ip.ip_len))
    {
      logger (7, "truncated packet, ignoring");
      return -1;
    }
  bytes = ntohs (dhcp->ip.ip_len);
  if (dhcp->ip.ip_p != IPPROTO_UDP || dhcp->ip.ip_v != 4 ||
      dhcp->ip.ip_hl != sizeof (dhcp->ip) >> 2 ||
      dhcp->udp.uh_dport != htons (68) ||
      bytes > (int) sizeof (struct udp_dhcp_packet) ||
      ntohs (dhcp->udp.uh_ulen) != (unsigned short) (bytes - sizeof (dhcp->ip)))
    {
      return -1;
    }
  if (valid_dhcp_packet (buffer) < 0)
    return -1;
  memcpy(data, &dhcp->dhcp, bytes - (sizeof (dhcp->ip) +
         sizeof (dhcp->udp)));
  return bytes - (sizeof (dhcp->ip) + sizeof (dhcp->udp));
}

variable_types:
data
unsigned char *

variable_types:
buffer
unsigned char *

variable_types: bytes long

variable_types: struct udp_dhcp_packet { struct ip { unsigned int ip_hl; unsigned int ip_v; unsigned char ip_tos; unsigned short ip_len; unsigned short ip_id; unsigned short ip_off; unsigned char ip_ttl; unsigned char ip_p; unsigned short ip_sum; struct in_addr { unsigned int s_addr; } ip_src; } ip; struct udphdr { union { struct { unsigned short uh_sport; unsigned short uh_dport; unsigned short uh_ulen; unsigned short uh_sum; }; struct { unsigned short source; unsigned short dest; unsigned short len; unsigned short check; }; }; } udp; struct dhcpmessage_t { char op; char hwtype; char hwlen; char hwopcount; int xid; short secs; short flags; int ciaddr; int yiaddr; int siaddr; int giaddr; unsigned char hwaddr[16]; char servername[64]; char bootfile[128]; unsigned int cookie; unsigned char options[308]; } dhcp; } *

return_type: unsigned long


checksum

decompiled_code:
__int64 __fastcall checksum(unsigned __int8 *a1, unsigned __int16 a2)
{
unsigned __int8 *v2; // rdx
unsigned int v3; // ebx
__int16 v4; // ax

if ( a2 <= 1u )
{
v4 = a2;
v2 = a1;
v3 = 0;
}
else
{
v2 = &a1[2 * (unsigned __int16)(((unsigned __int16)(a2 - 2) >> 1) + 1)];
v3 = 0;
do
{
a1 += 2;
v3 += *((unsigned __int16 *)a1 - 1);
}
while ( a1 != v2 );
v4 = a2 & 1;
}
if ( v4 == 1 )
v3 += (unsigned __int16)_bswap_16(*v2) << 8;
return ~(HIWORD(v3) + (unsigned __int16)v3 + ((unsigned int)(HIWORD(v3) + (unsigned __int16)v3) >> 16));
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int8 *a1, unsigned __int16 a2)
{
unsigned __int8 *v2; // rdx
unsigned int v3; // ebx
__int16 v4; // ax

if ( a2 <= 1u )
{
v4 = a2;
v2 = a1;
v3 = 0;
}
else
{
v2 = &a1[2 * (unsigned __int16)(((unsigned __int16)(a2 - 2) >> 1) + 1)];
v3 = 0;
do
{
a1 += 2;
v3 += *((unsigned __int16 *)a1 - 1);
}
while ( a1 != v2 );
v4 = a2 & 1;
}
if ( v4 == 1 )
v3 += (unsigned __int16)func1(*v2) << 8;
return ~(HIWORD(v3) + (unsigned __int16)v3 + ((unsigned int)(HIWORD(v3) + (unsigned __int16)v3) >> 16));
}

original_code:
static uint16_t checksum (unsigned char *addr, uint16_t len)
{
  register uint32_t sum = 0;
  register uint16_t *w = (uint16_t *) addr;
  register uint16_t nleft = len;
  while (nleft > 1)
    {
      sum += *w++;
      nleft -= 2;
    }
  if (nleft == 1)
    {
      uint8_t a = 0;
      memcpy (&a, w, 1);
      sum += ntohs (a) << 8;
    }
  sum = (sum >> 16) + (sum & 0xffff);
  sum += (sum >> 16);
  return ~sum;
}

canonical_original_code:
static unsigned short checksum(unsigned char *addr, unsigned short len)
{
  register unsigned int sum = 0;
  register unsigned short *w = (uint16_t *) addr;
  register unsigned short nleft = len;
  while (nleft > 1)
    {
      sum += *w++;
      nleft -= 2;
    }
  if (nleft == 1)
    {
      unsigned char a = 0;
      memcpy (&a, w, 1);
      sum += ntohs (a) << 8;
    }
  sum = (sum >> 16) + (sum & 0xffff);
  sum += (sum >> 16);
  return ~sum;
}

variable_types:
addr
unsigned char *

variable_types:
len
unsigned short

variable_types:
sum
unsigned int

variable_types:
w
unsigned short *

variable_types:
nleft
unsigned short

variable_types:
a
unsigned char

return_type: unsigned short


decompiled_code:
__int64 __fastcall ipv4_getrawpacket(__int64 a1, int a2, __int64 a3, __int64 a4, int *a5)
{
__int64 v6; // rax
int v7; // edi
ssize_t v8; // rbp
struct cmsghdr *v9; // rsi
__int64 v11; // [rsp+0h] [rbp-98h] BYREF
struct msghdr message; // [rsp+30h] [rbp-68h] BYREF
__int64 v13[5]; // [rsp+70h] [rbp-28h] BYREF

v13[0] = a3;
v13[1] = a4;
message.msg_name = 0LL;
*(_QWORD *)&message.msg_namelen = 0LL;
*(_QWORD *)&message.msg_flags = 0LL;
message.msg_iov = (iovec *)v13;
message.msg_iovlen = 1LL;
message.msg_control = &v11;
message.msg_controllen = 36LL;
v6 = *(_QWORD *)(a1 + 192);
if ( a2 == 2054 )
v7 = *(_DWORD *)(v6 + 184);
else
v7 = *(_DWORD *)(v6 + 176);
v8 = recvmsg(v7, &message, 0);
if ( v8 == -1 )
return -(__int64)(*_errno_location() != 11);
if ( a5 )
{
*a5 = 0;
if ( message.msg_controllen > 0xF )
{
v9 = (struct cmsghdr *)message.msg_control;
if ( message.msg_control )
{
do
{
if ( *(_QWORD *)&v9->cmsg_level == 0x800000107LL )
*a5 = *(_DWORD *)v9->__cmsg_data & 8;
v9 = _cmsg_nxthdr(&message, v9);
}
while ( v9 );
}
}
}
return v8;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int a2, __int64 a3, __int64 a4, int *a5)
{
__int64 v6; // rax
int v7; // edi
ssize_t v8; // rbp
struct cmsghdr *v9; // rsi
__int64 v11; // [rsp+0h] [rbp-98h] BYREF
struct msghdr message; // [rsp+30h] [rbp-68h] BYREF
__int64 v13[5]; // [rsp+70h] [rbp-28h] BYREF

v13[0] = a3;
v13[1] = a4;
message.msg_name = 0LL;
*(_QWORD *)&message.msg_namelen = 0LL;
*(_QWORD *)&message.msg_flags = 0LL;
message.msg_iov = (iovec *)v13;
message.msg_iovlen = 1LL;
message.msg_control = &v11;
message.msg_controllen = 36LL;
v6 = *(_QWORD *)(a1 + 192);
if ( a2 == 2054 )
v7 = *(_DWORD *)(v6 + 184);
else
v7 = *(_DWORD *)(v6 + 176);
v8 = recvmsg(v7, &message, 0);
if ( v8 == -1 )
return -(__int64)(*_errno_location() != 11);
if ( a5 )
{
*a5 = 0;
if ( message.msg_controllen > 0xF )
{
v9 = (struct cmsghdr *)message.msg_control;
if ( message.msg_control )
{
do
{
if ( *(_QWORD *)&v9->cmsg_level == 0x800000107LL )
*a5 = *(_DWORD *)v9->__cmsg_data & 8;
v9 = _cmsg_nxthdr(&message, v9);
}
while ( v9 );
}
}
}
return v8;
}

original_code:
ssize_t
ipv4_getrawpacket(struct interface *ifp, int protocol,
    void *data, ssize_t len, int *partialcsum)
{
 struct iovec iov = {
  .iov_base = data,
  .iov_len = len,
 };
 struct msghdr msg = {
  .msg_iov = &iov,
  .msg_iovlen = 1,
 };
 struct dhcp_state *state;
 unsigned char cmsgbuf[((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (sizeof(struct tpacket_auxdata)))];
 struct cmsghdr *cmsg;
 struct tpacket_auxdata *aux;
 ssize_t bytes;
 int fd = -1;
 msg.msg_control = cmsgbuf;
 msg.msg_controllen = sizeof(cmsgbuf);
 state = ((struct dhcp_state *)(ifp)->if_data[0]);
 if (protocol == 0x0806)
  fd = state->arp_fd;
 else
  fd = state->raw_fd;
 bytes = recvmsg(fd, &msg, 0);
 if (bytes == -1)
  return (*__errno_location ()) == 11 ? 0 : -1;
 if (partialcsum != ((void *)0)) {
  *partialcsum = 0;
  for (cmsg = ((size_t) (&msg)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&msg)->msg_control : (struct cmsghdr *) 0);
       cmsg;
       cmsg = __cmsg_nxthdr (&msg, cmsg))
  {
   if (cmsg->cmsg_level == 263 &&
       cmsg->cmsg_type == 8) {
    aux = (void *)((cmsg)->__cmsg_data);
    *partialcsum = aux->tp_status &
        (1 << 3);
   }
  }
 }
 return bytes;
}

canonical_original_code:
long ipv4_getrawpacket(struct interface *ifp, int protocol, void *data, long len, int *partialcsum)
{
 struct iovec iov = {
  .iov_base = data,
  .iov_len = len,
 };
 struct msghdr msg = {
  .msg_iov = &iov,
  .msg_iovlen = 1,
 };
 struct dhcp_state *state;
 unsigned char cmsgbuf[-1];
 struct cmsghdr *cmsg;
 struct tpacket_auxdata *aux;
 long bytes;
 int fd = -1;
 msg.msg_control = cmsgbuf;
 msg.msg_controllen = sizeof(cmsgbuf);
 state = ((struct dhcp_state *)(ifp)->if_data[0]);
 if (protocol == 0x0806)
  fd = state->arp_fd;
 else
  fd = state->raw_fd;
 bytes = recvmsg(fd, &msg, 0);
 if (bytes == -1)
  return (*__errno_location ()) == 11 ? 0 : -1;
 if (partialcsum != ((void *)0)) {
  *partialcsum = 0;
  for (cmsg = ((unsigned long) (&msg)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&msg)->msg_control : (struct cmsghdr *) 0);
       cmsg;
       cmsg = __cmsg_nxthdr (&msg, cmsg))
  {
   if (cmsg->cmsg_level == 263 &&
       cmsg->cmsg_type == 8) {
    aux = (void *)((cmsg)->__cmsg_data);
    *partialcsum = aux->tp_status &
        (1 << 3);
   }
  }
 }
 return bytes;
}

variable_types: struct interface { struct { struct interface *tqe_next; struct interface **tqe_prev; } next; char name[16]; unsigned int index; int flags; unsigned short family; unsigned char hwaddr[20]; unsigned long hwlen; int metric; int carrier; int wireless; char ssid[33]; char profile[64]; struct if_options { int metric; unsigned char requestmask[-1]; unsigned char requiremask[-1]; unsigned char nomask[-1]; unsigned char requestmask6[-1]; unsigned char requiremask6[-1]; unsigned char nomask6[-1]; unsigned char dstmask[-1]; unsigned int leasetime; long timeout; long reboot; unsigned long long options; struct in_addr { unsigned int s_addr; } req_addr; struct in_addr req_mask; struct rt_head { struct rt { struct { struct rt *tqe_next; struct rt **tqe_prev; } next; struct in_addr dest; struct in_addr net; struct in_addr gate; struct interface *iface; int metric; struct in_addr src; } *tqh_first; struct rt **tqh_last; } *routes; char **config; char **environ; char script[4096]; char hostname[-1]; int fqdn; unsigned char vendorclassid[-1]; char clientid[-1]; unsigned char userclass[-1]; unsigned char vendor[-1]; unsigned long blacklist_len; unsigned int *blacklist; unsigned long whitelist_len; unsigned int *whitelist; unsigned long arping_len; unsigned int *arping; char *fallback; unsigned short ia_type; unsigned long iaid_len; struct if_iaid { unsigned char iaid[4]; unsigned long sla_len; struct if_sla { char ifname[16]; unsigned int sla; short prefix_len; char sla_set; } *sla; } *iaid; int dadtransmits; } *options; void *if_data[4]; } *

variable_types: protocol int

variable_types:
data
void *

variable_types: len long

variable_types:
partialcsum
int *

variable_types: struct msghdr { void *msg_name; unsigned int msg_namelen; struct iovec { void *iov_base; unsigned long iov_len; } *msg_iov; unsigned long msg_iovlen; void *msg_control; unsigned long msg_controllen; int msg_flags; }

variable_types: struct dhcp_state { enum DHS { DHS_INIT=0, DHS_DISCOVER=1, DHS_REQUEST=2, DHS_BOUND=3, DHS_RENEW=4, DHS_REBIND=5, DHS_REBOOT=6, DHS_INFORM=7, DHS_RENEW_REQUESTED=8, DHS_INIT_IPV4LL=9, DHS_PROBE=10 } state; struct dhcp_message { unsigned char op; unsigned char hwtype; unsigned char hwlen; unsigned char hwopcount; unsigned int xid; unsigned short secs; unsigned short flags; unsigned int ciaddr; unsigned int yiaddr; unsigned int siaddr; unsigned int giaddr; unsigned char chaddr[16]; unsigned char servername[64]; unsigned char bootfile[128]; unsigned int cookie; unsigned char options[-1]; } *sent; struct dhcp_message *offer; struct dhcp_message *new; struct dhcp_message *old; struct dhcp_lease { struct in_addr { unsigned int s_addr; } addr; struct in_addr net; struct in_addr brd; unsigned int leasetime; unsigned int renewaltime; unsigned int rebindtime; struct in_addr server; long leasedfrom; struct timeval { long tv_sec; long tv_usec; } boundtime; unsigned char frominfo; unsigned int cookie; } lease; char *reason; long interval; long nakoff; unsigned int xid; int socket; int probes; int claims; int conflicts; long defend; struct in_addr fail; unsigned long arping_index; int raw_fd; int udp_fd; int arp_fd; unsigned long buffer_size; unsigned char *buffer; struct in_addr addr; struct in_addr net; struct in_addr dst; char leasefile[4096]; long start_uptime; unsigned char *clientid; } *

variable_types:
cmsgbuf
unsigned char [-1]

variable_types: struct cmsghdr { unsigned long cmsg_len; int cmsg_level; int cmsg_type; unsigned char __cmsg_data[]; } *

variable_types: struct tpacket_auxdata { unsigned int tp_status; unsigned int tp_len; unsigned int tp_snaplen; unsigned short tp_mac; unsigned short tp_net; unsigned short tp_vlan_tci; unsigned short tp_vlan_tpid; } *

return_type: long

user_defined_types: struct msghdr { void *msg_name; unsigned int msg_namelen; struct iovec *msg_iov; unsigned long msg_iovlen; void *msg_control; unsigned long msg_controllen; int msg_flags; }

user_defined_types: struct interface { struct { struct interface *tqe_next; struct interface **tqe_prev; } next; char name[16]; unsigned int index; int flags; unsigned short family; unsigned char hwaddr[20]; unsigned long hwlen; int metric; int carrier; int wireless; char ssid[33]; char profile[64]; struct if_options *options; void *if_data[4]; }

user_defined_types: struct dhcp_state { enum DHS state; struct dhcp_message *sent; struct dhcp_message *offer; struct dhcp_message *new; struct dhcp_message *old; struct dhcp_lease lease; char *reason; long interval; long nakoff; unsigned int xid; int socket; int probes; int claims; int conflicts; long defend; struct in_addr fail; unsigned long arping_index; int raw_fd; int udp_fd; int arp_fd; unsigned long buffer_size; unsigned char *buffer; struct in_addr addr; struct in_addr net; struct in_addr dst; char leasefile[4096]; long start_uptime; unsigned char *clientid; }

user_defined_types: struct dhcp_lease { struct in_addr addr; struct in_addr net; struct in_addr brd; unsigned int leasetime; unsigned int renewaltime; unsigned int rebindtime; struct in_addr server; long leasedfrom; struct timeval boundtime; unsigned char frominfo; unsigned int cookie; }

user_defined_types: struct if_options { int metric; unsigned char requestmask[-1]; unsigned char requiremask[-1]; unsigned char nomask[-1]; unsigned char requestmask6[-1]; unsigned char requiremask6[-1]; unsigned char nomask6[-1]; unsigned char dstmask[-1]; unsigned int leasetime; long timeout; long reboot; unsigned long long options; struct in_addr req_addr; struct in_addr req_mask; struct rt_head *routes; char **config; char **environ; char script[4096]; char hostname[-1]; int fqdn; unsigned char vendorclassid[-1]; char clientid[-1]; unsigned char userclass[-1]; unsigned char vendor[-1]; unsigned long blacklist_len; unsigned int *blacklist; unsigned long whitelist_len; unsigned int *whitelist; unsigned long arping_len; unsigned int *arping; char *fallback; unsigned short ia_type; unsigned long iaid_len; struct if_iaid *iaid; int dadtransmits; }

user_defined_types: struct rt_head { struct rt *tqh_first; struct rt **tqh_last; }

user_defined_types: struct if_iaid { unsigned char iaid[4]; unsigned long sla_len; struct if_sla *sla; }


original_code:
void
ipv4ll_start(void *arg)
{
 struct interface *ifp = arg;
 struct dhcp_state *state = ((struct dhcp_state *)(ifp)->if_data[0]);
 uint32_t addr;
 eloop_q_timeout_delete(1, ((void *)0), ifp);
 state->probes = 0;
 state->claims = 0;
 if (state->addr.s_addr) {
  state->conflicts = 0;
  if (((htonl(state->addr.s_addr) & 0xffff0000) == 0xa9fe0000)) {
   arp_announce(ifp);
   return;
  }
 }
 if (state->offer == ((void *)0))
  addr = 0;
 else {
  addr = state->offer->yiaddr;
  free(state->offer);
 }
 if (!((htonl(addr) & 0xffff0000) == 0xa9fe0000)) {
  syslog(6, "%s: probing for an IPv4LL address",
      ifp->name);
  addr = 0;
 }
 if (addr == 0)
  state->offer = ipv4ll_find_lease(addr);
 else
  state->offer = ipv4ll_make_lease(addr);
 if (state->offer == ((void *)0))
  syslog(3, "%s: %m", __func__);
 else {
  state->lease.frominfo = 0;
  arp_probe(ifp);
 }
}

canonical_original_code:
void ipv4ll_start(void *arg)
{
 struct interface *ifp = arg;
 struct dhcp_state *state = ((struct dhcp_state *)(ifp)->if_data[0]);
 unsigned int addr;
 eloop_q_timeout_delete(1, ((void *)0), ifp);
 state->probes = 0;
 state->claims = 0;
 if (state->addr.s_addr) {
  state->conflicts = 0;
  if (((htonl(state->addr.s_addr) & 0xffff0000) == 0xa9fe0000)) {
   arp_announce(ifp);
   return;
  }
 }
 if (state->offer == ((void *)0))
  addr = 0;
 else {
  addr = state->offer->yiaddr;
  free(state->offer);
 }
 if (!((htonl(addr) & 0xffff0000) == 0xa9fe0000)) {
  syslog(6, "%s: probing for an IPv4LL address",
      ifp->name);
  addr = 0;
 }
 if (addr == 0)
  state->offer = ipv4ll_find_lease(addr);
 else
  state->offer = ipv4ll_make_lease(addr);
 if (state->offer == ((void *)0))
  syslog(3, "%s: %m", __func__);
 else {
  state->lease.frominfo = 0;
  arp_probe(ifp);
 }
}

variable_types:
arg
void *

variable_types:
addr
unsigned int

user_defined_types: enum DHS { DHS_INIT=0, DHS_DISCOVER=1, DHS_REQUEST=2, DHS_BOUND=3, DHS_RENEW=4, DHS_REBIND=5, DHS_REBOOT=6, DHS_INFORM=7, DHS_RENEW_REQUESTED=8, DHS_INIT_IPV4LL=9, DHS_PROBE=10 }

user_defined_types: struct dhcp_message { unsigned char op; unsigned char hwtype; unsigned char hwlen; unsigned char hwopcount; unsigned int xid; unsigned short secs; unsigned short flags; unsigned int ciaddr; unsigned int yiaddr; unsigned int siaddr; unsigned int giaddr; unsigned char chaddr[16]; unsigned char servername[64]; unsigned char bootfile[128]; unsigned int cookie; unsigned char options[-1]; }

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

user_defined_types: struct rt { struct { struct rt *tqe_next; struct rt **tqe_prev; } next; struct in_addr dest; struct in_addr net; struct in_addr gate; struct interface *iface; int metric; struct in_addr src; }

user_defined_types: struct if_sla { char ifname[16]; unsigned int sla; short prefix_len; char sla_set; }