decompiled_code:
void __fastcall chacha_encrypt_bytes(int *a1, unsigned __int8 *a2, char *a3, unsigned int a4)
{
int v4; // er9
int v5; // eax
int v6; // edx
int v7; // ecx
int v8; // er13
int v9; // er14
int v10; // er10
int v11; // ebx
int v12; // edi
int v13; // er8
int v14; // esi
int v15; // er11
int v16; // ebp
int v17; // er12
int v18; // er15
int v19; // er15
int v20; // ecx
int v21; // ebx
int v22; // esi
int v23; // er15
int v24; // ecx
int v25; // ebx
int v26; // er12
int v27; // edx
int v28; // er10
int v29; // er8
int v30; // er12
int v31; // edx
int v32; // er10
int v33; // er8
int v34; // ebp
int v35; // eax
int v36; // er14
int v37; // edi
int v38; // ebp
int v39; // eax
int v40; // er14
int v41; // edi
int v42; // er11
int v43; // er9
int v44; // er13
int v45; // esi
int v46; // er11
int v47; // er9
int v48; // er13
int v49; // esi
int v50; // er15
int v51; // er9
int v52; // er14
int v53; // er8
int v54; // er12
int v55; // ecx
int v56; // er13
int v57; // edi
int v58; // ebp
int v59; // edx
int v60; // ebx
int v61; // esi
int v62; // er11
int v63; // eax
int v64; // er10
int v65; // esi
unsigned __int8 *v66; // r9
int v67; // er12
int v68; // er11
int v69; // er8
int v70; // edi
int v71; // esi
int v72; // ebp
int v73; // er10
int v74; // er14
int v75; // er13
int v76; // ebx
int v77; // ecx
__int64 i; // rax
unsigned int v79; // eax
int v80; // esi
unsigned int v81; // edx
__int64 v82; // rax
__int64 v83; // rcx
int v84; // [rsp+0h] [rbp-100h]
int v85; // [rsp+0h] [rbp-100h]
int v86; // [rsp+4h] [rbp-FCh]
int v87; // [rsp+4h] [rbp-FCh]
int v88; // [rsp+4h] [rbp-FCh]
int v89; // [rsp+8h] [rbp-F8h]
int v90; // [rsp+8h] [rbp-F8h]
int v91; // [rsp+Ch] [rbp-F4h]
unsigned int v92; // [rsp+10h] [rbp-F0h]
int v93; // [rsp+14h] [rbp-ECh]
int v96; // [rsp+28h] [rbp-D8h]
int v97; // [rsp+2Ch] [rbp-D4h]
int v98; // [rsp+30h] [rbp-D0h]
int v99; // [rsp+34h] [rbp-CCh]
int v100; // [rsp+38h] [rbp-C8h]
int v101; // [rsp+3Ch] [rbp-C4h]
int v102; // [rsp+40h] [rbp-C0h]
int v103; // [rsp+44h] [rbp-BCh]
int v104; // [rsp+48h] [rbp-B8h]
int v105; // [rsp+4Ch] [rbp-B4h]
int v106; // [rsp+50h] [rbp-B0h]
int v107; // [rsp+54h] [rbp-ACh]
int v108; // [rsp+58h] [rbp-A8h]
int v109; // [rsp+5Ch] [rbp-A4h]
int v110; // [rsp+70h] [rbp-90h]
int v111; // [rsp+74h] [rbp-8Ch]
char *v112; // [rsp+78h] [rbp-88h]
unsigned int v114; // [rsp+88h] [rbp-78h]
int v115; // [rsp+8Ch] [rbp-74h]
char v116[112]; // [rsp+90h] [rbp-70h] BYREF

v114 = a4;
if ( a4 )
{
v96 = *a1;
v97 = a1[1];
v98 = a1[2];
v99 = a1[3];
v100 = a1[4];
v101 = a1[5];
v102 = a1[6];
v103 = a1[7];
v104 = a1[8];
v105 = a1[9];
v106 = a1[10];
v107 = a1[11];
v115 = a1[12];
v93 = a1[13];
v108 = a1[14];
v109 = a1[15];
v91 = v115;
v92 = a4;
v112 = 0LL;
while ( 1 )
{
if ( v92 <= 0x3F )
{
if ( v92 )
{
for ( i = 0LL; i != v92; ++i )
v116[i] = a2[i];
v112 = a3;
a3 = v116;
a2 = (unsigned __int8 *)v116;
}
else
{
v112 = a3;
a3 = v116;
a2 = (unsigned __int8 *)v116;
}
}
v4 = v109;
v5 = v108;
v6 = v93;
v7 = v91;
v8 = v107;
v9 = v106;
v10 = v105;
v11 = v104;
v84 = v103;
v12 = v102;
v13 = v101;
v14 = v100;
v15 = v99;
v16 = v98;
v17 = v97;
v18 = v96;
v89 = 10;
do
{
v19 = v14 + v18;
v20 = __ROL4__(v19 ^ v7, 16);
v21 = v20 + v11;
v22 = __ROL4__(v21 ^ v14, 12);
v23 = v22 + v19;
v24 = __ROL4__(v23 ^ v20, 8);
v25 = v24 + v21;
v86 = __ROL4__(v25 ^ v22, 7);
v26 = v13 + v17;
v27 = __ROL4__(v26 ^ v6, 16);
v28 = v27 + v10;
v29 = __ROL4__(v28 ^ v13, 12);
v30 = v29 + v26;
v31 = __ROL4__(v30 ^ v27, 8);
v32 = v31 + v28;
v33 = __ROL4__(v32 ^ v29, 7);
v34 = v12 + v16;
v35 = __ROL4__(v34 ^ v5, 16);
v36 = v35 + v9;
v37 = __ROL4__(v36 ^ v12, 12);
v38 = v37 + v34;
v39 = __ROL4__(v38 ^ v35, 8);
v40 = v39 + v36;
v41 = __ROL4__(v40 ^ v37, 7);
v42 = v84 + v15;
v43 = __ROL4__(v42 ^ v4, 16);
v44 = v43 + v8;
v45 = __ROL4__(v44 ^ v84, 12);
v46 = v45 + v42;
v47 = __ROL4__(v46 ^ v43, 8);
v48 = v47 + v44;
v49 = __ROL4__(v48 ^ v45, 7);
v50 = v33 + v23;
v51 = __ROL4__(v50 ^ v47, 16);
v52 = v51 + v40;
v53 = __ROL4__(v52 ^ v33, 12);
v18 = v53 + v50;
v4 = __ROL4__(v18 ^ v51, 8);
v9 = v4 + v52;
v13 = __ROL4__(v9 ^ v53, 7);
v54 = v41 + v30;
v55 = __ROL4__(v54 ^ v24, 16);
v56 = v55 + v48;
v57 = __ROL4__(v56 ^ v41, 12);
v17 = v57 + v54;
v7 = __ROL4__(v17 ^ v55, 8);
v8 = v7 + v56;
v12 = __ROL4__(v8 ^ v57, 7);
v58 = v49 + v38;
v59 = __ROL4__(v58 ^ v31, 16);
v60 = v59 + v25;
v61 = __ROL4__(v60 ^ v49, 12);
v16 = v61 + v58;
v6 = __ROL4__(v16 ^ v59, 8);
v11 = v6 + v60;
v84 = __ROL4__(v11 ^ v61, 7);
v62 = v86 + v46;
v63 = __ROL4__(v62 ^ v39, 16);
v64 = v63 + v32;
v65 = __ROL4__(v64 ^ v86, 12);
v15 = v65 + v62;
v5 = __ROL4__(v15 ^ v63, 8);
v10 = v5 + v64;
v14 = __ROL4__(v10 ^ v65, 7);
--v89;
}
while ( v89 );
v87 = v99 + v15;
v110 = v109 + v4;
v66 = a2;
v67 = ((v66[7] << 24) | v66[4] | (v66[5] << 8) | (v66[6] << 16)) ^ (v97 + v17);
v68 = (v98 + v16) ^ ((v66[11] << 24) | v66[8] | (v66[9] << 8) | (v66[10] << 16));
v88 = v87 ^ ((v66[15] << 24) | v66[12] | (v66[13] << 8) | (v66[14] << 16));
v111 = (v100 + v14) ^ ((v66[19] << 24) | v66[16] | (v66[17] << 8) | (v66[18] << 16));
v69 = ((v66[23] << 24) | v66[20] | (v66[21] << 8) | (v66[22] << 16)) ^ (v101 + v13);
v70 = ((v66[27] << 24) | v66[24] | (v66[25] << 8) | (v66[26] << 16)) ^ (v102 + v12);
v71 = (v103 + v84) ^ ((v66[31] << 24) | v66[28] | (v66[29] << 8) | (v66[30] << 16));
v72 = (v104 + v11) ^ ((v66[35] << 24) | v66[32] | (v66[33] << 8) | (v66[34] << 16));
v73 = ((v66[39] << 24) | v66[36] | (v66[37] << 8) | (v66[38] << 16)) ^ (v105 + v10);
v74 = (v106 + v9) ^ ((v66[43] << 24) | v66[40] | (v66[41] << 8) | (v66[42] << 16));
v75 = (v107 + v8) ^ ((v66[47] << 24) | v66[44] | (v66[45] << 8) | (v66[46] << 16));
v76 = (v91 + v7) ^ ((v66[51] << 24) | v66[48] | (v66[49] << 8) | (v66[50] << 16));
v77 = (v93 + v6) ^ ((v66[55] << 24) | v66[52] | (v66[53] << 8) | (v66[54] << 16));
v85 = (v108 + v5) ^ ((v66[59] << 24) | v66[56] | (v66[57] << 8) | (v66[58] << 16));
v90 = v110 ^ ((v66[63] << 24) | v66[60] | (v66[61] << 8) | (v66[62] << 16));
v93 += ++v91 == 0;
*(_DWORD *)a3 = ((a2[3] << 24) | *a2 | (a2[1] << 8) | (a2[2] << 16)) ^ (v96 + v18);
*((_DWORD *)a3 + 1) = v67;
*((_DWORD *)a3 + 2) = v68;
*((_DWORD *)a3 + 3) = v88;
*((_DWORD *)a3 + 4) = v111;
*((_DWORD *)a3 + 5) = v69;
*((_DWORD *)a3 + 6) = v70;
*((_DWORD *)a3 + 7) = v71;
*((_DWORD *)a3 + 8) = v72;
*((_DWORD *)a3 + 9) = v73;
*((_DWORD *)a3 + 10) = v74;
*((_DWORD *)a3 + 11) = v75;
*((_DWORD *)a3 + 12) = v76;
*((_DWORD *)a3 + 13) = v77;
*((_DWORD *)a3 + 14) = v85;
*((_DWORD *)a3 + 15) = v90;
if ( v92 <= 0x40 )
break;
v92 -= 64;
a3 += 64;
a2 += 64;
}
v79 = (v114 - 1) >> 6;
v80 = v79 + v115 + 1;
v81 = v114 - (v79 << 6) - 1;
if ( v81 <= 0x3E )
{
v82 = 0LL;
do
{
v112[v82] = a3[v82];
v83 = v82++;
}
while ( v81 != v83 );
}
a1[12] = v80;
a1[13] = v93;
}
}

canonical_decompiled_code:
void __fastcall func2(int *a1, unsigned __int8 *a2, char *a3, unsigned int a4)
{
int v4; // er9
int v5; // eax
int v6; // edx
int v7; // ecx
int v8; // er13
int v9; // er14
int v10; // er10
int v11; // ebx
int v12; // edi
int v13; // er8
int v14; // esi
int v15; // er11
int v16; // ebp
int v17; // er12
int v18; // er15
int v19; // er15
int v20; // ecx
int v21; // ebx
int v22; // esi
int v23; // er15
int v24; // ecx
int v25; // ebx
int v26; // er12
int v27; // edx
int v28; // er10
int v29; // er8
int v30; // er12
int v31; // edx
int v32; // er10
int v33; // er8
int v34; // ebp
int v35; // eax
int v36; // er14
int v37; // edi
int v38; // ebp
int v39; // eax
int v40; // er14
int v41; // edi
int v42; // er11
int v43; // er9
int v44; // er13
int v45; // esi
int v46; // er11
int v47; // er9
int v48; // er13
int v49; // esi
int v50; // er15
int v51; // er9
int v52; // er14
int v53; // er8
int v54; // er12
int v55; // ecx
int v56; // er13
int v57; // edi
int v58; // ebp
int v59; // edx
int v60; // ebx
int v61; // esi
int v62; // er11
int v63; // eax
int v64; // er10
int v65; // esi
unsigned __int8 *v66; // r9
int v67; // er12
int v68; // er11
int v69; // er8
int v70; // edi
int v71; // esi
int v72; // ebp
int v73; // er10
int v74; // er14
int v75; // er13
int v76; // ebx
int v77; // ecx
__int64 i; // rax
unsigned int v79; // eax
int v80; // esi
unsigned int v81; // edx
__int64 v82; // rax
__int64 v83; // rcx
int v84; // [rsp+0h] [rbp-100h]
int v85; // [rsp+0h] [rbp-100h]
int v86; // [rsp+4h] [rbp-FCh]
int v87; // [rsp+4h] [rbp-FCh]
int v88; // [rsp+4h] [rbp-FCh]
int v89; // [rsp+8h] [rbp-F8h]
int v90; // [rsp+8h] [rbp-F8h]
int v91; // [rsp+Ch] [rbp-F4h]
unsigned int v92; // [rsp+10h] [rbp-F0h]
int v93; // [rsp+14h] [rbp-ECh]
int v96; // [rsp+28h] [rbp-D8h]
int v97; // [rsp+2Ch] [rbp-D4h]
int v98; // [rsp+30h] [rbp-D0h]
int v99; // [rsp+34h] [rbp-CCh]
int v100; // [rsp+38h] [rbp-C8h]
int v101; // [rsp+3Ch] [rbp-C4h]
int v102; // [rsp+40h] [rbp-C0h]
int v103; // [rsp+44h] [rbp-BCh]
int v104; // [rsp+48h] [rbp-B8h]
int v105; // [rsp+4Ch] [rbp-B4h]
int v106; // [rsp+50h] [rbp-B0h]
int v107; // [rsp+54h] [rbp-ACh]
int v108; // [rsp+58h] [rbp-A8h]
int v109; // [rsp+5Ch] [rbp-A4h]
int v110; // [rsp+70h] [rbp-90h]
int v111; // [rsp+74h] [rbp-8Ch]
char *v112; // [rsp+78h] [rbp-88h]
unsigned int v114; // [rsp+88h] [rbp-78h]
int v115; // [rsp+8Ch] [rbp-74h]
char v116[112]; // [rsp+90h] [rbp-70h] BYREF

v114 = a4;
if ( a4 )
{
v96 = *a1;
v97 = a1[1];
v98 = a1[2];
v99 = a1[3];
v100 = a1[4];
v101 = a1[5];
v102 = a1[6];
v103 = a1[7];
v104 = a1[8];
v105 = a1[9];
v106 = a1[10];
v107 = a1[11];
v115 = a1[12];
v93 = a1[13];
v108 = a1[14];
v109 = a1[15];
v91 = v115;
v92 = a4;
v112 = 0LL;
while ( 1 )
{
if ( v92 <= 0x3F )
{
if ( v92 )
{
for ( i = 0LL; i != v92; ++i )
v116[i] = a2[i];
v112 = a3;
a3 = v116;
a2 = (unsigned __int8 *)v116;
}
else
{
v112 = a3;
a3 = v116;
a2 = (unsigned __int8 *)v116;
}
}
v4 = v109;
v5 = v108;
v6 = v93;
v7 = v91;
v8 = v107;
v9 = v106;
v10 = v105;
v11 = v104;
v84 = v103;
v12 = v102;
v13 = v101;
v14 = v100;
v15 = v99;
v16 = v98;
v17 = v97;
v18 = v96;
v89 = 10;
do
{
v19 = v14 + v18;
v20 = __ROL4__(v19 ^ v7, 16);
v21 = v20 + v11;
v22 = __ROL4__(v21 ^ v14, 12);
v23 = v22 + v19;
v24 = __ROL4__(v23 ^ v20, 8);
v25 = v24 + v21;
v86 = __ROL4__(v25 ^ v22, 7);
v26 = v13 + v17;
v27 = __ROL4__(v26 ^ v6, 16);
v28 = v27 + v10;
v29 = __ROL4__(v28 ^ v13, 12);
v30 = v29 + v26;
v31 = __ROL4__(v30 ^ v27, 8);
v32 = v31 + v28;
v33 = __ROL4__(v32 ^ v29, 7);
v34 = v12 + v16;
v35 = __ROL4__(v34 ^ v5, 16);
v36 = v35 + v9;
v37 = __ROL4__(v36 ^ v12, 12);
v38 = v37 + v34;
v39 = __ROL4__(v38 ^ v35, 8);
v40 = v39 + v36;
v41 = __ROL4__(v40 ^ v37, 7);
v42 = v84 + v15;
v43 = __ROL4__(v42 ^ v4, 16);
v44 = v43 + v8;
v45 = __ROL4__(v44 ^ v84, 12);
v46 = v45 + v42;
v47 = __ROL4__(v46 ^ v43, 8);
v48 = v47 + v44;
v49 = __ROL4__(v48 ^ v45, 7);
v50 = v33 + v23;
v51 = __ROL4__(v50 ^ v47, 16);
v52 = v51 + v40;
v53 = __ROL4__(v52 ^ v33, 12);
v18 = v53 + v50;
v4 = __ROL4__(v18 ^ v51, 8);
v9 = v4 + v52;
v13 = __ROL4__(v9 ^ v53, 7);
v54 = v41 + v30;
v55 = __ROL4__(v54 ^ v24, 16);
v56 = v55 + v48;
v57 = __ROL4__(v56 ^ v41, 12);
v17 = v57 + v54;
v7 = __ROL4__(v17 ^ v55, 8);
v8 = v7 + v56;
v12 = __ROL4__(v8 ^ v57, 7);
v58 = v49 + v38;
v59 = __ROL4__(v58 ^ v31, 16);
v60 = v59 + v25;
v61 = __ROL4__(v60 ^ v49, 12);
v16 = v61 + v58;
v6 = __ROL4__(v16 ^ v59, 8);
v11 = v6 + v60;
v84 = __ROL4__(v11 ^ v61, 7);
v62 = v86 + v46;
v63 = __ROL4__(v62 ^ v39, 16);
v64 = v63 + v32;
v65 = __ROL4__(v64 ^ v86, 12);
v15 = v65 + v62;
v5 = __ROL4__(v15 ^ v63, 8);
v10 = v5 + v64;
v14 = __ROL4__(v10 ^ v65, 7);
--v89;
}
while ( v89 );
v87 = v99 + v15;
v110 = v109 + v4;
v66 = a2;
v67 = ((v66[7] << 24) | v66[4] | (v66[5] << 8) | (v66[6] << 16)) ^ (v97 + v17);
v68 = (v98 + v16) ^ ((v66[11] << 24) | v66[8] | (v66[9] << 8) | (v66[10] << 16));
v88 = v87 ^ ((v66[15] << 24) | v66[12] | (v66[13] << 8) | (v66[14] << 16));
v111 = (v100 + v14) ^ ((v66[19] << 24) | v66[16] | (v66[17] << 8) | (v66[18] << 16));
v69 = ((v66[23] << 24) | v66[20] | (v66[21] << 8) | (v66[22] << 16)) ^ (v101 + v13);
v70 = ((v66[27] << 24) | v66[24] | (v66[25] << 8) | (v66[26] << 16)) ^ (v102 + v12);
v71 = (v103 + v84) ^ ((v66[31] << 24) | v66[28] | (v66[29] << 8) | (v66[30] << 16));
v72 = (v104 + v11) ^ ((v66[35] << 24) | v66[32] | (v66[33] << 8) | (v66[34] << 16));
v73 = ((v66[39] << 24) | v66[36] | (v66[37] << 8) | (v66[38] << 16)) ^ (v105 + v10);
v74 = (v106 + v9) ^ ((v66[43] << 24) | v66[40] | (v66[41] << 8) | (v66[42] << 16));
v75 = (v107 + v8) ^ ((v66[47] << 24) | v66[44] | (v66[45] << 8) | (v66[46] << 16));
v76 = (v91 + v7) ^ ((v66[51] << 24) | v66[48] | (v66[49] << 8) | (v66[50] << 16));
v77 = (v93 + v6) ^ ((v66[55] << 24) | v66[52] | (v66[53] << 8) | (v66[54] << 16));
v85 = (v108 + v5) ^ ((v66[59] << 24) | v66[56] | (v66[57] << 8) | (v66[58] << 16));
v90 = v110 ^ ((v66[63] << 24) | v66[60] | (v66[61] << 8) | (v66[62] << 16));
v93 += ++v91 == 0;
*(_DWORD *)a3 = ((a2[3] << 24) | *a2 | (a2[1] << 8) | (a2[2] << 16)) ^ (v96 + v18);
*((_DWORD *)a3 + 1) = v67;
*((_DWORD *)a3 + 2) = v68;
*((_DWORD *)a3 + 3) = v88;
*((_DWORD *)a3 + 4) = v111;
*((_DWORD *)a3 + 5) = v69;
*((_DWORD *)a3 + 6) = v70;
*((_DWORD *)a3 + 7) = v71;
*((_DWORD *)a3 + 8) = v72;
*((_DWORD *)a3 + 9) = v73;
*((_DWORD *)a3 + 10) = v74;
*((_DWORD *)a3 + 11) = v75;
*((_DWORD *)a3 + 12) = v76;
*((_DWORD *)a3 + 13) = v77;
*((_DWORD *)a3 + 14) = v85;
*((_DWORD *)a3 + 15) = v90;
if ( v92 <= 0x40 )
break;
v92 -= 64;
a3 += 64;
a2 += 64;
}
v79 = (v114 - 1) >> 6;
v80 = v79 + v115 + 1;
v81 = v114 - (v79 << 6) - 1;
if ( v81 <= 0x3E )
{
v82 = 0LL;
do
{
v112[v82] = a3[v82];
v83 = v82++;
}
while ( v81 != v83 );
}
a1[12] = v80;
a1[13] = v93;
}
}

canonical_original_code:
void chacha_encrypt_bytes(struct chacha_ctx *x, unsigned char *m, unsigned char *c, unsigned int bytes)
{
 unsigned int x0; unsigned int x1; unsigned int x2; unsigned int x3; unsigned int x4; unsigned int x5; unsigned int x6; unsigned int x7; unsigned int x8; unsigned int x9; unsigned int x10; unsigned int x11; unsigned int x12; unsigned int x13; unsigned int x14; unsigned int x15;
 unsigned int j0; unsigned int j1; unsigned int j2; unsigned int j3; unsigned int j4; unsigned int j5; unsigned int j6; unsigned int j7; unsigned int j8; unsigned int j9; unsigned int j10; unsigned int j11; unsigned int j12; unsigned int j13; unsigned int j14; unsigned int j15;
 unsigned char *ctarget = ((void *)0);
 unsigned char tmp[64];
 unsigned int i;
 if (!bytes)
  return;
 j0 = x->input[0];
 j1 = x->input[1];
 j2 = x->input[2];
 j3 = x->input[3];
 j4 = x->input[4];
 j5 = x->input[5];
 j6 = x->input[6];
 j7 = x->input[7];
 j8 = x->input[8];
 j9 = x->input[9];
 j10 = x->input[10];
 j11 = x->input[11];
 j12 = x->input[12];
 j13 = x->input[13];
 j14 = x->input[14];
 j15 = x->input[15];
 for (;;) {
  if (bytes < 64) {
   for (i = 0; i < bytes; ++i)
    tmp[i] = m[i];
   m = tmp;
   ctarget = c;
   c = tmp;
  }
  x0 = j0;
  x1 = j1;
  x2 = j2;
  x3 = j3;
  x4 = j4;
  x5 = j5;
  x6 = j6;
  x7 = j7;
  x8 = j8;
  x9 = j9;
  x10 = j10;
  x11 = j11;
  x12 = j12;
  x13 = j13;
  x14 = j14;
  x15 = j15;
  for (i = 20; i > 0; i -= 2) {
   x0 = (((u32)((x0) + (x4)) & (0xFFFFFFFFU))); x12 = ((((u32)((((x12) ^ (x0))) << (16)) & (0xFFFFFFFFU)) | ((((x12) ^ (x0))) >> (32 - (16))))); x8 = (((u32)((x8) + (x12)) & (0xFFFFFFFFU))); x4 = ((((u32)((((x4) ^ (x8))) << (12)) & (0xFFFFFFFFU)) | ((((x4) ^ (x8))) >> (32 - (12))))); x0 = (((u32)((x0) + (x4)) & (0xFFFFFFFFU))); x12 = ((((u32)((((x12) ^ (x0))) << (8)) & (0xFFFFFFFFU)) | ((((x12) ^ (x0))) >> (32 - (8))))); x8 = (((u32)((x8) + (x12)) & (0xFFFFFFFFU))); x4 = ((((u32)((((x4) ^ (x8))) << (7)) & (0xFFFFFFFFU)) | ((((x4) ^ (x8))) >> (32 - (7)))));
   x1 = (((u32)((x1) + (x5)) & (0xFFFFFFFFU))); x13 = ((((u32)((((x13) ^ (x1))) << (16)) & (0xFFFFFFFFU)) | ((((x13) ^ (x1))) >> (32 - (16))))); x9 = (((u32)((x9) + (x13)) & (0xFFFFFFFFU))); x5 = ((((u32)((((x5) ^ (x9))) << (12)) & (0xFFFFFFFFU)) | ((((x5) ^ (x9))) >> (32 - (12))))); x1 = (((u32)((x1) + (x5)) & (0xFFFFFFFFU))); x13 = ((((u32)((((x13) ^ (x1))) << (8)) & (0xFFFFFFFFU)) | ((((x13) ^ (x1))) >> (32 - (8))))); x9 = (((u32)((x9) + (x13)) & (0xFFFFFFFFU))); x5 = ((((u32)((((x5) ^ (x9))) << (7)) & (0xFFFFFFFFU)) | ((((x5) ^ (x9))) >> (32 - (7)))));
   x2 = (((u32)((x2) + (x6)) & (0xFFFFFFFFU))); x14 = ((((u32)((((x14) ^ (x2))) << (16)) & (0xFFFFFFFFU)) | ((((x14) ^ (x2))) >> (32 - (16))))); x10 = (((u32)((x10) + (x14)) & (0xFFFFFFFFU))); x6 = ((((u32)((((x6) ^ (x10))) << (12)) & (0xFFFFFFFFU)) | ((((x6) ^ (x10))) >> (32 - (12))))); x2 = (((u32)((x2) + (x6)) & (0xFFFFFFFFU))); x14 = ((((u32)((((x14) ^ (x2))) << (8)) & (0xFFFFFFFFU)) | ((((x14) ^ (x2))) >> (32 - (8))))); x10 = (((u32)((x10) + (x14)) & (0xFFFFFFFFU))); x6 = ((((u32)((((x6) ^ (x10))) << (7)) & (0xFFFFFFFFU)) | ((((x6) ^ (x10))) >> (32 - (7)))));
   x3 = (((u32)((x3) + (x7)) & (0xFFFFFFFFU))); x15 = ((((u32)((((x15) ^ (x3))) << (16)) & (0xFFFFFFFFU)) | ((((x15) ^ (x3))) >> (32 - (16))))); x11 = (((u32)((x11) + (x15)) & (0xFFFFFFFFU))); x7 = ((((u32)((((x7) ^ (x11))) << (12)) & (0xFFFFFFFFU)) | ((((x7) ^ (x11))) >> (32 - (12))))); x3 = (((u32)((x3) + (x7)) & (0xFFFFFFFFU))); x15 = ((((u32)((((x15) ^ (x3))) << (8)) & (0xFFFFFFFFU)) | ((((x15) ^ (x3))) >> (32 - (8))))); x11 = (((u32)((x11) + (x15)) & (0xFFFFFFFFU))); x7 = ((((u32)((((x7) ^ (x11))) << (7)) & (0xFFFFFFFFU)) | ((((x7) ^ (x11))) >> (32 - (7)))));
   x0 = (((u32)((x0) + (x5)) & (0xFFFFFFFFU))); x15 = ((((u32)((((x15) ^ (x0))) << (16)) & (0xFFFFFFFFU)) | ((((x15) ^ (x0))) >> (32 - (16))))); x10 = (((u32)((x10) + (x15)) & (0xFFFFFFFFU))); x5 = ((((u32)((((x5) ^ (x10))) << (12)) & (0xFFFFFFFFU)) | ((((x5) ^ (x10))) >> (32 - (12))))); x0 = (((u32)((x0) + (x5)) & (0xFFFFFFFFU))); x15 = ((((u32)((((x15) ^ (x0))) << (8)) & (0xFFFFFFFFU)) | ((((x15) ^ (x0))) >> (32 - (8))))); x10 = (((u32)((x10) + (x15)) & (0xFFFFFFFFU))); x5 = ((((u32)((((x5) ^ (x10))) << (7)) & (0xFFFFFFFFU)) | ((((x5) ^ (x10))) >> (32 - (7)))));
   x1 = (((u32)((x1) + (x6)) & (0xFFFFFFFFU))); x12 = ((((u32)((((x12) ^ (x1))) << (16)) & (0xFFFFFFFFU)) | ((((x12) ^ (x1))) >> (32 - (16))))); x11 = (((u32)((x11) + (x12)) & (0xFFFFFFFFU))); x6 = ((((u32)((((x6) ^ (x11))) << (12)) & (0xFFFFFFFFU)) | ((((x6) ^ (x11))) >> (32 - (12))))); x1 = (((u32)((x1) + (x6)) & (0xFFFFFFFFU))); x12 = ((((u32)((((x12) ^ (x1))) << (8)) & (0xFFFFFFFFU)) | ((((x12) ^ (x1))) >> (32 - (8))))); x11 = (((u32)((x11) + (x12)) & (0xFFFFFFFFU))); x6 = ((((u32)((((x6) ^ (x11))) << (7)) & (0xFFFFFFFFU)) | ((((x6) ^ (x11))) >> (32 - (7)))));
   x2 = (((u32)((x2) + (x7)) & (0xFFFFFFFFU))); x13 = ((((u32)((((x13) ^ (x2))) << (16)) & (0xFFFFFFFFU)) | ((((x13) ^ (x2))) >> (32 - (16))))); x8 = (((u32)((x8) + (x13)) & (0xFFFFFFFFU))); x7 = ((((u32)((((x7) ^ (x8))) << (12)) & (0xFFFFFFFFU)) | ((((x7) ^ (x8))) >> (32 - (12))))); x2 = (((u32)((x2) + (x7)) & (0xFFFFFFFFU))); x13 = ((((u32)((((x13) ^ (x2))) << (8)) & (0xFFFFFFFFU)) | ((((x13) ^ (x2))) >> (32 - (8))))); x8 = (((u32)((x8) + (x13)) & (0xFFFFFFFFU))); x7 = ((((u32)((((x7) ^ (x8))) << (7)) & (0xFFFFFFFFU)) | ((((x7) ^ (x8))) >> (32 - (7)))));
   x3 = (((u32)((x3) + (x4)) & (0xFFFFFFFFU))); x14 = ((((u32)((((x14) ^ (x3))) << (16)) & (0xFFFFFFFFU)) | ((((x14) ^ (x3))) >> (32 - (16))))); x9 = (((u32)((x9) + (x14)) & (0xFFFFFFFFU))); x4 = ((((u32)((((x4) ^ (x9))) << (12)) & (0xFFFFFFFFU)) | ((((x4) ^ (x9))) >> (32 - (12))))); x3 = (((u32)((x3) + (x4)) & (0xFFFFFFFFU))); x14 = ((((u32)((((x14) ^ (x3))) << (8)) & (0xFFFFFFFFU)) | ((((x14) ^ (x3))) >> (32 - (8))))); x9 = (((u32)((x9) + (x14)) & (0xFFFFFFFFU))); x4 = ((((u32)((((x4) ^ (x9))) << (7)) & (0xFFFFFFFFU)) | ((((x4) ^ (x9))) >> (32 - (7)))));
  }
  x0 = (((u32)((x0) + (j0)) & (0xFFFFFFFFU)));
  x1 = (((u32)((x1) + (j1)) & (0xFFFFFFFFU)));
  x2 = (((u32)((x2) + (j2)) & (0xFFFFFFFFU)));
  x3 = (((u32)((x3) + (j3)) & (0xFFFFFFFFU)));
  x4 = (((u32)((x4) + (j4)) & (0xFFFFFFFFU)));
  x5 = (((u32)((x5) + (j5)) & (0xFFFFFFFFU)));
  x6 = (((u32)((x6) + (j6)) & (0xFFFFFFFFU)));
  x7 = (((u32)((x7) + (j7)) & (0xFFFFFFFFU)));
  x8 = (((u32)((x8) + (j8)) & (0xFFFFFFFFU)));
  x9 = (((u32)((x9) + (j9)) & (0xFFFFFFFFU)));
  x10 = (((u32)((x10) + (j10)) & (0xFFFFFFFFU)));
  x11 = (((u32)((x11) + (j11)) & (0xFFFFFFFFU)));
  x12 = (((u32)((x12) + (j12)) & (0xFFFFFFFFU)));
  x13 = (((u32)((x13) + (j13)) & (0xFFFFFFFFU)));
  x14 = (((u32)((x14) + (j14)) & (0xFFFFFFFFU)));
  x15 = (((u32)((x15) + (j15)) & (0xFFFFFFFFU)));
  x0 = ((x0) ^ ((((u32)((m + 0)[0]) ) | ((u32)((m + 0)[1]) << 8) | ((u32)((m + 0)[2]) << 16) | ((u32)((m + 0)[3]) << 24))));
  x1 = ((x1) ^ ((((u32)((m + 4)[0]) ) | ((u32)((m + 4)[1]) << 8) | ((u32)((m + 4)[2]) << 16) | ((u32)((m + 4)[3]) << 24))));
  x2 = ((x2) ^ ((((u32)((m + 8)[0]) ) | ((u32)((m + 8)[1]) << 8) | ((u32)((m + 8)[2]) << 16) | ((u32)((m + 8)[3]) << 24))));
  x3 = ((x3) ^ ((((u32)((m + 12)[0]) ) | ((u32)((m + 12)[1]) << 8) | ((u32)((m + 12)[2]) << 16) | ((u32)((m + 12)[3]) << 24))));
  x4 = ((x4) ^ ((((u32)((m + 16)[0]) ) | ((u32)((m + 16)[1]) << 8) | ((u32)((m + 16)[2]) << 16) | ((u32)((m + 16)[3]) << 24))));
  x5 = ((x5) ^ ((((u32)((m + 20)[0]) ) | ((u32)((m + 20)[1]) << 8) | ((u32)((m + 20)[2]) << 16) | ((u32)((m + 20)[3]) << 24))));
  x6 = ((x6) ^ ((((u32)((m + 24)[0]) ) | ((u32)((m + 24)[1]) << 8) | ((u32)((m + 24)[2]) << 16) | ((u32)((m + 24)[3]) << 24))));
  x7 = ((x7) ^ ((((u32)((m + 28)[0]) ) | ((u32)((m + 28)[1]) << 8) | ((u32)((m + 28)[2]) << 16) | ((u32)((m + 28)[3]) << 24))));
  x8 = ((x8) ^ ((((u32)((m + 32)[0]) ) | ((u32)((m + 32)[1]) << 8) | ((u32)((m + 32)[2]) << 16) | ((u32)((m + 32)[3]) << 24))));
  x9 = ((x9) ^ ((((u32)((m + 36)[0]) ) | ((u32)((m + 36)[1]) << 8) | ((u32)((m + 36)[2]) << 16) | ((u32)((m + 36)[3]) << 24))));
  x10 = ((x10) ^ ((((u32)((m + 40)[0]) ) | ((u32)((m + 40)[1]) << 8) | ((u32)((m + 40)[2]) << 16) | ((u32)((m + 40)[3]) << 24))));
  x11 = ((x11) ^ ((((u32)((m + 44)[0]) ) | ((u32)((m + 44)[1]) << 8) | ((u32)((m + 44)[2]) << 16) | ((u32)((m + 44)[3]) << 24))));
  x12 = ((x12) ^ ((((u32)((m + 48)[0]) ) | ((u32)((m + 48)[1]) << 8) | ((u32)((m + 48)[2]) << 16) | ((u32)((m + 48)[3]) << 24))));
  x13 = ((x13) ^ ((((u32)((m + 52)[0]) ) | ((u32)((m + 52)[1]) << 8) | ((u32)((m + 52)[2]) << 16) | ((u32)((m + 52)[3]) << 24))));
  x14 = ((x14) ^ ((((u32)((m + 56)[0]) ) | ((u32)((m + 56)[1]) << 8) | ((u32)((m + 56)[2]) << 16) | ((u32)((m + 56)[3]) << 24))));
  x15 = ((x15) ^ ((((u32)((m + 60)[0]) ) | ((u32)((m + 60)[1]) << 8) | ((u32)((m + 60)[2]) << 16) | ((u32)((m + 60)[3]) << 24))));
  j12 = ((((u32)(((j12)) + (1)) & (0xFFFFFFFFU))));
  if (!j12) {
   j13 = ((((u32)(((j13)) + (1)) & (0xFFFFFFFFU))));
  }
  do { (c + 0)[0] = ((u8)((x0)) & (0xFFU)); (c + 0)[1] = ((u8)((x0) >> 8) & (0xFFU)); (c + 0)[2] = ((u8)((x0) >> 16) & (0xFFU)); (c + 0)[3] = ((u8)((x0) >> 24) & (0xFFU)); } while (0);
  do { (c + 4)[0] = ((u8)((x1)) & (0xFFU)); (c + 4)[1] = ((u8)((x1) >> 8) & (0xFFU)); (c + 4)[2] = ((u8)((x1) >> 16) & (0xFFU)); (c + 4)[3] = ((u8)((x1) >> 24) & (0xFFU)); } while (0);
  do { (c + 8)[0] = ((u8)((x2)) & (0xFFU)); (c + 8)[1] = ((u8)((x2) >> 8) & (0xFFU)); (c + 8)[2] = ((u8)((x2) >> 16) & (0xFFU)); (c + 8)[3] = ((u8)((x2) >> 24) & (0xFFU)); } while (0);
  do { (c + 12)[0] = ((u8)((x3)) & (0xFFU)); (c + 12)[1] = ((u8)((x3) >> 8) & (0xFFU)); (c + 12)[2] = ((u8)((x3) >> 16) & (0xFFU)); (c + 12)[3] = ((u8)((x3) >> 24) & (0xFFU)); } while (0);
  do { (c + 16)[0] = ((u8)((x4)) & (0xFFU)); (c + 16)[1] = ((u8)((x4) >> 8) & (0xFFU)); (c + 16)[2] = ((u8)((x4) >> 16) & (0xFFU)); (c + 16)[3] = ((u8)((x4) >> 24) & (0xFFU)); } while (0);
  do { (c + 20)[0] = ((u8)((x5)) & (0xFFU)); (c + 20)[1] = ((u8)((x5) >> 8) & (0xFFU)); (c + 20)[2] = ((u8)((x5) >> 16) & (0xFFU)); (c + 20)[3] = ((u8)((x5) >> 24) & (0xFFU)); } while (0);
  do { (c + 24)[0] = ((u8)((x6)) & (0xFFU)); (c + 24)[1] = ((u8)((x6) >> 8) & (0xFFU)); (c + 24)[2] = ((u8)((x6) >> 16) & (0xFFU)); (c + 24)[3] = ((u8)((x6) >> 24) & (0xFFU)); } while (0);
  do { (c + 28)[0] = ((u8)((x7)) & (0xFFU)); (c + 28)[1] = ((u8)((x7) >> 8) & (0xFFU)); (c + 28)[2] = ((u8)((x7) >> 16) & (0xFFU)); (c + 28)[3] = ((u8)((x7) >> 24) & (0xFFU)); } while (0);
  do { (c + 32)[0] = ((u8)((x8)) & (0xFFU)); (c + 32)[1] = ((u8)((x8) >> 8) & (0xFFU)); (c + 32)[2] = ((u8)((x8) >> 16) & (0xFFU)); (c + 32)[3] = ((u8)((x8) >> 24) & (0xFFU)); } while (0);
  do { (c + 36)[0] = ((u8)((x9)) & (0xFFU)); (c + 36)[1] = ((u8)((x9) >> 8) & (0xFFU)); (c + 36)[2] = ((u8)((x9) >> 16) & (0xFFU)); (c + 36)[3] = ((u8)((x9) >> 24) & (0xFFU)); } while (0);
  do { (c + 40)[0] = ((u8)((x10)) & (0xFFU)); (c + 40)[1] = ((u8)((x10) >> 8) & (0xFFU)); (c + 40)[2] = ((u8)((x10) >> 16) & (0xFFU)); (c + 40)[3] = ((u8)((x10) >> 24) & (0xFFU)); } while (0);
  do { (c + 44)[0] = ((u8)((x11)) & (0xFFU)); (c + 44)[1] = ((u8)((x11) >> 8) & (0xFFU)); (c + 44)[2] = ((u8)((x11) >> 16) & (0xFFU)); (c + 44)[3] = ((u8)((x11) >> 24) & (0xFFU)); } while (0);
  do { (c + 48)[0] = ((u8)((x12)) & (0xFFU)); (c + 48)[1] = ((u8)((x12) >> 8) & (0xFFU)); (c + 48)[2] = ((u8)((x12) >> 16) & (0xFFU)); (c + 48)[3] = ((u8)((x12) >> 24) & (0xFFU)); } while (0);
  do { (c + 52)[0] = ((u8)((x13)) & (0xFFU)); (c + 52)[1] = ((u8)((x13) >> 8) & (0xFFU)); (c + 52)[2] = ((u8)((x13) >> 16) & (0xFFU)); (c + 52)[3] = ((u8)((x13) >> 24) & (0xFFU)); } while (0);
  do { (c + 56)[0] = ((u8)((x14)) & (0xFFU)); (c + 56)[1] = ((u8)((x14) >> 8) & (0xFFU)); (c + 56)[2] = ((u8)((x14) >> 16) & (0xFFU)); (c + 56)[3] = ((u8)((x14) >> 24) & (0xFFU)); } while (0);
  do { (c + 60)[0] = ((u8)((x15)) & (0xFFU)); (c + 60)[1] = ((u8)((x15) >> 8) & (0xFFU)); (c + 60)[2] = ((u8)((x15) >> 16) & (0xFFU)); (c + 60)[3] = ((u8)((x15) >> 24) & (0xFFU)); } while (0);
  if (bytes <= 64) {
   if (bytes < 64) {
    for (i = 0; i < bytes; ++i)
     ctarget[i] = c[i];
   }
   x->input[12] = j12;
   x->input[13] = j13;
   return;
  }
  bytes -= 64;
  c += 64;
  m += 64;
 }
}

variable_types: struct chacha_ctx { unsigned int input[16]; } *

variable_types:
m
unsigned char *

variable_types:
c
unsigned char *

variable_types:
bytes
unsigned int

variable_types:
ctarget
unsigned char *

variable_types:
tmp
unsigned char [64]

variable_types:
i
unsigned int


repo: telehash/telehash-c

variable_types:
k
unsigned char *

variable_types:
kbits
unsigned int

variable_types:
constants
char *

return_type: void

user_defined_types: struct chacha_ctx { unsigned int input[16]; }


decompiled_code:
__int64 __fastcall net_loopback_new(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
_QWORD *v3; // rbx
__int64 v4; // rax
__int64 v5; // rax

v2 = malloc(0x10uLL);
if ( !v2 )
return util_sys_log(7LL, "src/net/loopback.c", 19LL, "net_loopback_new", "OOM");
v3 = v2;
*v2 = a1;
v2[1] = a2;
v4 = link_get_keys(a1, *(_QWORD *)(a2 + 8));
link_pipe(v4, pair_send, v3);
v5 = link_get_keys(a2, *(_QWORD *)(a1 + 8));
link_pipe(v5, pair_send, v3);
return (__int64)v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
_QWORD *v2; // rax
_QWORD *v3; // rbx
__int64 v4; // rax
__int64 v5; // rax

v2 = malloc(0x10uLL);
if ( !v2 )
return util_sys_log(7LL, "src/net/loopback.c", 19LL, "net_loopback_new", "OOM");
v3 = v2;
*v2 = a1;
v2[1] = a2;
v4 = link_get_keys(a1, *(_QWORD *)(a2 + 8));
link_pipe(v4, pair_send, v3);
v5 = link_get_keys(a2, *(_QWORD *)(a1 + 8));
link_pipe(v5, pair_send, v3);
return (__int64)v3;
}

original_code:
net_loopback_t net_loopback_new(mesh_t a, mesh_t b)
{
  net_loopback_t pair;
  if(!(pair = malloc(sizeof (struct net_loopback_struct)))) return util_sys_log(7, "src/net/loopback.c", 19, __func__, "OOM");
  memset(pair,0,sizeof (struct net_loopback_struct));
  pair->a = a;
  pair->b = b;
  link_pipe(link_get_keys(a,b->keys),pair_send,pair);
  link_pipe(link_get_keys(b,a->keys),pair_send,pair);
  return pair;
}

canonical_original_code:
struct net_loopback_struct *net_loopback_new(struct mesh_struct *a, struct mesh_struct *b)
{
  struct net_loopback_struct *pair;
  if(!(pair = malloc(sizeof (struct net_loopback_struct)))) return util_sys_log(7, "src/net/loopback.c", 19, __func__, "OOM");
  memset(pair,0,sizeof (struct net_loopback_struct));
  pair->a = a;
  pair->b = b;
  link_pipe(link_get_keys(a,b->keys),pair_send,pair);
  link_pipe(link_get_keys(b,a->keys),pair_send,pair);
  return pair;
}

variable_types:
a
struct mesh_struct *

variable_types:
b
struct mesh_struct *

variable_types: struct net_loopback_struct { struct mesh_struct { struct hashname_struct { unsigned char bin[32]; } *id; struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; } *keys; struct e3x_self_struct { struct lob_struct *keys[2]; void *locals[2]; } *self; void *on; unsigned int state; struct link_struct { struct hashname_struct *id; struct e3x_exchange_struct { struct e3x_cipher_struct { unsigned char *(*rand)(unsigned char *bytes, unsigned long len); unsigned char *(*hash)(unsigned char *in, unsigned long len, unsigned char *out32); unsigned char *(*err)(); unsigned char (*generate)(struct lob_struct *keys, struct lob_struct *secrets); void *(*local_new)(struct lob_struct *keys, struct lob_struct *secrets); void (*local_free)(void *local); struct lob_struct *(*local_decrypt)(void *local, struct lob_struct *outer); struct lob_struct *(*local_sign)(void *local, struct lob_struct *args, unsigned char *data, unsigned long len); void *(*remote_new)(struct lob_struct *key, unsigned char *token); void (*remote_free)(void *remote); unsigned char (*remote_verify)(void *remote, void *local, struct lob_struct *outer); struct lob_struct *(*remote_encrypt)(void *remote, void *local, struct lob_struct *inner); unsigned char (*remote_validate)(void *remote, struct lob_struct *args, struct lob_struct *sig, unsigned char *data, unsigned long len); void *(*ephemeral_new)(void *remote, struct lob_struct *outer); void (*ephemeral_free)(void *ephemeral); struct lob_struct *(*ephemeral_encrypt)(void *ephemeral, struct lob_struct *inner); struct lob_struct *(*ephemeral_decrypt)(void *ephemeral, struct lob_struct *outer); unsigned char id; char hex[3]; } *cs; struct e3x_self_struct *self; void *remote; void *ephem; unsigned int in; unsigned int cid; unsigned char token[16]; unsigned char csid; char hex[3]; } *x; struct mesh_struct *mesh; struct lob_struct *key; struct chan_struct { struct link_struct *link; struct chan_struct *next; unsigned int id; char *type; struct lob_struct *in; unsigned int tsent; unsigned int timeout; void *arg; void (*handle)(struct chan_struct *c, void *arg); enum chan_states { CHAN_ENDED=0, CHAN_OPENING=1, CHAN_OPEN=2 } state; } *chans; unsigned int state; void *send_arg; struct link_struct *(*send_cb)(struct link_struct *link, struct lob_struct *packet, void *arg); struct link_struct *next; unsigned char csid; } *links; } *a; } *

user_defined_types: struct net_loopback_struct { struct mesh_struct *a; }

user_defined_types: struct mesh_struct { struct hashname_struct *id; struct lob_struct *keys; struct e3x_self_struct *self; void *on; unsigned int state; struct link_struct *links; }

user_defined_types: struct link_struct { struct hashname_struct *id; struct e3x_exchange_struct *x; struct mesh_struct *mesh; struct lob_struct *key; struct chan_struct *chans; unsigned int state; void *send_arg; struct link_struct *(*send_cb)(struct link_struct *link, struct lob_struct *packet, void *arg); struct link_struct *next; unsigned char csid; }

user_defined_types: struct e3x_exchange_struct { struct e3x_cipher_struct *cs; struct e3x_self_struct *self; void *remote; void *ephem; unsigned int in; unsigned int cid; unsigned char token[16]; unsigned char csid; char hex[3]; }

user_defined_types: struct chan_struct { struct link_struct *link; struct chan_struct *next; unsigned int id; char *type; struct lob_struct *in; unsigned int tsent; unsigned int timeout; void *arg; void (*handle)(struct chan_struct *c, void *arg); enum chan_states state; }


decompiled_code:
void __fastcall net_loopback_free(void *a1)
{
free(a1);
}

canonical_decompiled_code:
void __fastcall func1(void *a1)
{
free(a1);
}

original_code:
void net_loopback_free(net_loopback_t pair)
{
  free(pair);
  return;
}

canonical_original_code:
void net_loopback_free(struct net_loopback_struct *pair)
{
  free(pair);
  return;
}

user_defined_types: struct hashname_struct { unsigned char bin[32]; }

user_defined_types: struct lob_struct { unsigned char *raw; unsigned char *body; unsigned long body_len; unsigned char *head; unsigned long head_len; unsigned int id; void *arg; struct lob_struct *chain; char *cache; struct lob_struct *next; }

user_defined_types: struct e3x_self_struct { struct lob_struct *keys[2]; void *locals[2]; }

user_defined_types: enum chan_states { CHAN_ENDED=0, CHAN_OPENING=1, CHAN_OPEN=2 }

user_defined_types: struct e3x_cipher_struct { unsigned char *(*rand)(unsigned char *bytes, unsigned long len); unsigned char *(*hash)(unsigned char *in, unsigned long len, unsigned char *out32); unsigned char *(*err)(); unsigned char (*generate)(struct lob_struct *keys, struct lob_struct *secrets); void *(*local_new)(struct lob_struct *keys, struct lob_struct *secrets); void (*local_free)(void *local); struct lob_struct *(*local_decrypt)(void *local, struct lob_struct *outer); struct lob_struct *(*local_sign)(void *local, struct lob_struct *args, unsigned char *data, unsigned long len); void *(*remote_new)(struct lob_struct *key, unsigned char *token); void (*remote_free)(void *remote); unsigned char (*remote_verify)(void *remote, void *local, struct lob_struct *outer); struct lob_struct *(*remote_encrypt)(void *remote, void *local, struct lob_struct *inner); unsigned char (*remote_validate)(void *remote, struct lob_struct *args, struct lob_struct *sig, unsigned char *data, unsigned long len); void *(*ephemeral_new)(void *remote, struct lob_struct *outer); void (*ephemeral_free)(void *ephemeral); struct lob_struct *(*ephemeral_encrypt)(void *ephemeral, struct lob_struct *inner); struct lob_struct *(*ephemeral_decrypt)(void *ephemeral, struct lob_struct *outer); unsigned char id; char hex[3]; }


repo: tenchman/gsclu

decompiled_code:
__int64 __fastcall check_cn(_BYTE *a1, __int64 a2)
{
_BYTE *v2; // rbx
__int64 v3; // rbp
int v5; // er12
int v6; // eax

v2 = a1;
v3 = a2;
if ( *a1 == 42 )
{
v2 = a1 + 1;
v5 = str_len(a2);
v6 = v5 - str_len(a1 + 1);
if ( v6 >= 0 )
v3 = a2 + v6;
}
return str_casecmp(v3, v2);
}

original_code:
int check_cn(char *cn, char *fqdn)
{
  int n;
  if (*cn == '*') {
    cn++;
    n = str_len(fqdn) - str_len(cn);
    if (n >= 0)
      fqdn += n;
  }
  return str_casecmp(fqdn, cn);
}

variable_types:
cn
char *

variable_types:
fqdn
char *

variable_types: n int

return_type: int


rblcheck

original_code:
static char *rblcheck(struct in_addr *in, rbldomain_t * dom)
{
  unsigned char fixedans[512];
  unsigned char *answer = fixedans;
  unsigned char *cp;
  char *result = (char *) 0;
  char domain[dom->len + 17];
  int len, packetsz = 512, type = ns_t_a;
  time_t now = time(((void *)0));
  if (dom->failcount >= 5
      && (now - dom->lastsuccess) < 5 * 60)
    return ((void *)0);
  if (_setjmp (env) == 14) {
    write(2, "timeout querying ", sizeof("timeout querying ") - 1);
    write(2, dom->name, dom->len);
    write(2, "\n", sizeof("\n") - 1);
    dom->failcount++;
    return ((void *)0);
  }
  strcpy(domain, (const char *) inet_ntoa(*in));
  strncat(domain, ".", 1);
  strncat(domain, dom->name, dom->len);
tryagain:
  alarm(15);
  len = __res_query(domain, ns_c_in, type, answer, packetsz);
  alarm(0);
  dom->lastsuccess = now;
  dom->failcount = 0;
  if (len == -1)
    return result;
  if (len > packetsz) {
    answer = malloc(len);
    packetsz = len;
    goto tryagain;
  }
  if (type == ns_t_a) {
    result = malloc(0xFE + 1);
    result[0] = result[0xFE] = '\0';
    type = ns_t_txt;
    goto tryagain;
  }
  cp = answer + sizeof(HEADER);
  cp += __dn_skipname(cp, answer + len);
  cp += 2 + 2;
  cp += __dn_skipname(cp, answer + len);
  cp += 2 + 2 + 4;
  do { const unsigned char *t_cp = (const unsigned char *)(cp); (len) = ((uint16_t)t_cp[0] << 8) | ((uint16_t)t_cp[1]) ; (cp) += 2; } while (0);
  if (len) {
    if ((len = (int) *cp++) > 0xFE)
      len = 0xFE;
    strncpy(result, (char *) cp, len);
    result[len] = '\0';
  }
  if (answer != fixedans)
    free(answer);
  return result;
}

canonical_original_code:
static char *rblcheck(struct in_addr *in, struct rbldomain_t *dom)
{
  unsigned char fixedans[512];
  unsigned char *answer = fixedans;
  unsigned char *cp;
  char *result = (char *) 0;
  char domain[-1];
  int len; int packetsz = 512; int type = ns_t_a;
  long now = time(((void *)0));
  if (dom->failcount >= 5
      && (now - dom->lastsuccess) < 5 * 60)
    return ((void *)0);
  if (_setjmp (env) == 14) {
    write(2, "timeout querying ", sizeof("timeout querying ") - 1);
    write(2, dom->name, dom->len);
    write(2, "\n", sizeof("\n") - 1);
    dom->failcount++;
    return ((void *)0);
  }
  strcpy(domain, (char *) inet_ntoa(*in));
  strncat(domain, ".", 1);
  strncat(domain, dom->name, dom->len);
tryagain:
  alarm(15);
  len = __res_query(domain, ns_c_in, type, answer, packetsz);
  alarm(0);
  dom->lastsuccess = now;
  dom->failcount = 0;
  if (len == -1)
    return result;
  if (len > packetsz) {
    answer = malloc(len);
    packetsz = len;
    goto tryagain;
  }
  if (type == ns_t_a) {
    result = malloc(0xFE + 1);
    result[0] = result[0xFE] = '\0';
    type = ns_t_txt;
    goto tryagain;
  }
  cp = answer + sizeof(HEADER);
  cp += __dn_skipname(cp, answer + len);
  cp += 2 + 2;
  cp += __dn_skipname(cp, answer + len);
  cp += 2 + 2 + 4;
  do { unsigned char *t_cp = (const unsigned char *)(cp); (len) = ((unsigned short)t_cp[0] << 8) | ((unsigned short)t_cp[1]) ; (cp) += 2; } while (0);
  if (len) {
    if ((len = (int) *cp++) > 0xFE)
      len = 0xFE;
    strncpy(result, (char *) cp, len);
    result[len] = '\0';
  }
  if (answer != fixedans)
    free(answer);
  return result;
}

variable_types: struct in_addr { unsigned int s_addr; } *

variable_types: struct rbldomain_t { unsigned char failcount; long lastsuccess; unsigned long len; char name[]; } *

variable_types:
fixedans
unsigned char [512]

variable_types:
answer
unsigned char *

variable_types:
cp
unsigned char *

variable_types:
result
char *

variable_types:
domain
char [-1]

variable_types: len int

variable_types: packetsz int

variable_types: type int

variable_types: now long

return_type: char *


sigalarm

decompiled_code:
void __fastcall __noreturn sigalarm(int a1)
{
longjmp(env, a1);
}

canonical_decompiled_code:
void __fastcall __noreturn func3(int a1)
{
longjmp(env, a1);
}

original_code:
static void sigalarm(int signal)
{
  longjmp(env, signal);
}

variable_types: signal int


original_code:
static int read_rbldomains(config_t * c)
{
  char dombuf[1024];
  rbldomain_t *dom = ((void *)0);
  char *dombufpos = dombuf;
  char *buf, *tmp;
  struct stat st;
  if ((buf = tmp = mmapfile(c->domaincfg, &st)) == ((void *)0))
    return -1;
  while (tmp < buf + st.st_size) {
    size_t n = strcspn(tmp, "\r\n");
    if (!n)
      n = buf + st.st_size - tmp;
    dom = (rbldomain_t *) dombufpos;
    dom->failcount = 0;
    dom->lastsuccess = time(((void *)0));
    dom->len = n;
    strncpy(dom->name, tmp, n);
    dombufpos = dom->name + n;
    tmp += n;
    while (*tmp == '\n' || *tmp == '\r')
      ++tmp;
  }
  if (dom) {
    char *r = realloc(c->rbldomains, dombufpos - dombuf);
    if (!r) {
      munmap(buf, st.st_size);
      return -1;
    }
    memcpy(r, dombuf, dombufpos - dombuf);
    c->rbldomains = r;
  }
  c->timestamp = st.st_mtim.tv_sec;
  munmap(buf, st.st_size);
  return 0;
}

canonical_original_code:
static int read_rbldomains(struct config_t *c)
{
  char dombuf[1024];
  struct rbldomain_t *dom = ((void *)0);
  char *dombufpos = dombuf;
  char *buf; char *tmp;
  struct stat st;
  if ((buf = tmp = mmapfile(c->domaincfg, &st)) == ((void *)0))
    return -1;
  while (tmp < buf + st.st_size) {
    unsigned long n = strcspn(tmp, "\r\n");
    if (!n)
      n = buf + st.st_size - tmp;
    dom = (struct rbldomain_t *) dombufpos;
    dom->failcount = 0;
    dom->lastsuccess = time(((void *)0));
    dom->len = n;
    strncpy(dom->name, tmp, n);
    dombufpos = dom->name + n;
    tmp += n;
    while (*tmp == '\n' || *tmp == '\r')
      ++tmp;
  }
  if (dom) {
    char *r = realloc(c->rbldomains, dombufpos - dombuf);
    if (!r) {
      munmap(buf, st.st_size);
      return -1;
    }
    memcpy(r, dombuf, dombufpos - dombuf);
    c->rbldomains = r;
  }
  c->timestamp = st.st_mtim.tv_sec;
  munmap(buf, st.st_size);
  return 0;
}

variable_types: struct config_t { char *domaincfg; char *socketpath; char *pidfile; void *rbldomains; long timestamp; int clientfd; int serverfd; } *

variable_types:
dombuf
char [1024]

variable_types:
dombufpos
char *

variable_types:
buf
char *

variable_types:
tmp
char *

variable_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec { long tv_sec; long tv_nsec; } st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

variable_types:
n
unsigned long

variable_types:
r
char *

user_defined_types: struct stat { unsigned long st_dev; unsigned long st_ino; unsigned long st_nlink; unsigned int st_mode; unsigned int st_uid; unsigned int st_gid; int __pad0; unsigned long st_rdev; long st_size; long st_blksize; long st_blocks; struct timespec st_atim; struct timespec st_mtim; struct timespec st_ctim; long __glibc_reserved[3]; }

user_defined_types: struct rbldomain_t { unsigned char failcount; long lastsuccess; unsigned long len; char name[]; }


decompiled_code:
ssize_t __fastcall write_errno(void *buf)
{
int *v1; // rax
char *v2; // rbx
size_t v3; // rax
size_t v4; // rax

v1 = _errno_location();
v2 = strerror(*v1);
v3 = strlen((const char *)buf);
write(2, buf, v3);
write(2, &unk_3EA, 2uLL);
v4 = strlen(v2);
write(2, v2, v4);
return write(2, &unk_E, 1uLL);
}

canonical_decompiled_code:
ssize_t __fastcall func5(void *buf)
{
int *v1; // rax
char *v2; // rbx
size_t v3; // rax
size_t v4; // rax

v1 = _errno_location();
v2 = strerror(*v1);
v3 = strlen((const char *)buf);
write(2, buf, v3);
write(2, &unk_3EA, 2uLL);
v4 = strlen(v2);
write(2, v2, v4);
return write(2, &unk_E, 1uLL);
}

original_code:
static void write_errno(char *msg)
{
  char *err = strerror((*__errno_location ()));
  write(2, msg, strlen(msg));
  write(2, ": ", 2);
  write(2, err, strlen(err));
  write(2, "\n", 1);
}

variable_types:
msg
char *

variable_types:
err
char *


decompiled_code:
void __fastcall __noreturn die_errno(void *a1)
{
write_errno(a1);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func6(void *a1)
{
func5(a1);
exit(1);
}

original_code:
static void die_errno(char *msg)
{
  write_errno(msg);
  exit(1);
}


query

original_code:
int query(struct in_addr *in, config_t * c)
{
  char *ptr = c->rbldomains;
  rbldomain_t *dom = (rbldomain_t *) ptr;
  pid_t child;
  while (*dom->name) {
    pid_t pid = fork();
    switch (pid) {
    case -1:
      break;
    case 0:{
 char *result;
 if ((result = rblcheck(in, dom))) {
   char outbuf[strlen(result) + 4 + dom->len];
   strcpy(outbuf, dom->name);
   strcat(outbuf, ": ");
   strcat(outbuf, result);
   strcat(outbuf, "\n");
   write(c->clientfd, outbuf, sizeof(outbuf));
 }
      }
      exit(0);
    default:
      ;
    }
    ptr += sizeof(rbldomain_t) + dom->len;
    dom = (rbldomain_t *) ptr;
  };
  do {
    child = waitpid(-1, ((void *)0), 0);
  } while (child != -1);
  return 0;
}

canonical_original_code:
int query(struct in_addr *in, struct config_t *c)
{
  char *ptr = c->rbldomains;
  struct rbldomain_t *dom = (rbldomain_t *) ptr;
  int child;
  while (*dom->name) {
    int pid = fork();
    switch (pid) {
    case -1:
      break;
    case 0:{
 char *result;
 if ((result = rblcheck(in, dom))) {
   char outbuf[-1];
   strcpy(outbuf, dom->name);
   strcat(outbuf, ": ");
   strcat(outbuf, result);
   strcat(outbuf, "\n");
   write(c->clientfd, outbuf, sizeof(outbuf));
 }
      }
      exit(0);
    default:
      ;
    }
    ptr += sizeof(rbldomain_t) + dom->len;
    dom = (struct rbldomain_t *) ptr;
  };
  do {
    child = waitpid(-1, ((void *)0), 0);
  } while (child != -1);
  return 0;
}

variable_types: struct in_addr { unsigned int s_addr; } *

variable_types:
ptr
char *

variable_types: struct rbldomain_t { unsigned char failcount; long lastsuccess; unsigned long len; char name[]; } *

variable_types: child int

variable_types: pid int

variable_types:
result
char *

variable_types:
outbuf
char [-1]

user_defined_types: struct config_t { char *domaincfg; char *socketpath; char *pidfile; void *rbldomains; long timestamp; int clientfd; int serverfd; }


listener

decompiled_code:
__pid_t __fastcall listener(__int64 a1)
{
struct __res_state *v1; // rax
int v2; // eax
int v3; // ebp
int v4; // er12
__pid_t result; // eax
int v6; // er13
int v7; // ebx
struct in_addr inp; // [rsp+4h] [rbp-234h] BYREF
struct pollfd fds; // [rsp+8h] [rbp-230h] BYREF
struct stat v10; // [rsp+10h] [rbp-228h] BYREF
char buf[256]; // [rsp+A0h] [rbp-198h] BYREF
struct sockaddr addr; // [rsp+1A0h] [rbp-98h] BYREF
int v13; // [rsp+208h] [rbp-30h]
__int16 v14; // [rsp+20Ch] [rbp-2Ch]

v1 = _res_state();
v1->options &= 0xFFFFFFFFFFFFFFBFLL;
umask(0x49u);
if ( unlink(*(const char **)(a1 + 8)) == -1 && *_errno_location() != 2 )
die_errno("can't unlink /tmp/.rbldsock");
memset(&addr, 0, 0x68uLL);
v13 = 0;
v14 = 0;
addr.sa_family = 1;
strncpy(addr.sa_data, *(const char **)(a1 + 8), 0x6CuLL);
v2 = socket(1, 1, 0);
v3 = v2;
if ( v2 == -1 )
die_errno("error creating listener socket");
if ( bind(v2, &addr, 0x6Eu) == -1 )
{
close(v3);
die_errno("can't bind to socket");
}
if ( listen(v3, 10) == -1 )
{
close(v3);
die_errno("can't listen to socket");
}
signal(15, (__sighandler_t)cleanup);
signal(2, (__sighandler_t)cleanup);
signal(14, (__sighandler_t)sigalarm);
signal(17, handler);
signal(13, handler);
*(_DWORD *)(a1 + 44) = v3;
v4 = signaled;
if ( signaled )
return 0;
do
{
if ( stat(*(const char **)a1, &v10) == -1 )
{
write_errno("can't stat /etc/rbldomains");
}
else if ( v10.st_mtim.tv_sec > *(_QWORD *)(a1 + 32) )
{
write(2, "rereading /etc/rbldomains\n", 0x1AuLL);
read_rbldomains(a1);
}
fds.fd = v3;
fds.events = 1;
fds.revents = 0;
poll(&fds, 1uLL, -1);
if ( (fds.revents & 1) != 0 )
{
v6 = accept(v3, 0LL, 0LL);
if ( v6 == -1 )
{
write_errno("accept failed");
}
else
{
result = fork();
if ( result == -1 )
return result;
if ( !result )
{
close(v3);
*(_DWORD *)(a1 + 40) = v6;
if ( (unsigned int)read(v6, buf, 0x100uLL) != -1 )
{
if ( !inet_aton(buf, &inp) )
die("invalid ip address");
inp.s_addr = _bswap_32(inp.s_addr);
v7 = query(&inp, a1);
write(v6, &unk_741, 1uLL);
close(v6);
exit(v7);
}
die_errno("can't read from socket");
}
waitpid(result, 0LL, 0);
}
}
}
while ( !signaled );
return v4;
}

canonical_decompiled_code:
__pid_t __fastcall func11(__int64 a1)
{
struct __res_state *v1; // rax
int v2; // eax
int v3; // ebp
int v4; // er12
__pid_t result; // eax
int v6; // er13
int v7; // ebx
struct in_addr inp; // [rsp+4h] [rbp-234h] BYREF
struct pollfd fds; // [rsp+8h] [rbp-230h] BYREF
struct stat v10; // [rsp+10h] [rbp-228h] BYREF
char buf[256]; // [rsp+A0h] [rbp-198h] BYREF
struct sockaddr addr; // [rsp+1A0h] [rbp-98h] BYREF
int v13; // [rsp+208h] [rbp-30h]
__int16 v14; // [rsp+20Ch] [rbp-2Ch]

v1 = _res_state();
v1->options &= 0xFFFFFFFFFFFFFFBFLL;
umask(0x49u);
if ( unlink(*(const char **)(a1 + 8)) == -1 && *_errno_location() != 2 )
func6("can't unlink /tmp/.rbldsock");
memset(&addr, 0, 0x68uLL);
v13 = 0;
v14 = 0;
addr.sa_family = 1;
strncpy(addr.sa_data, *(const char **)(a1 + 8), 0x6CuLL);
v2 = socket(1, 1, 0);
v3 = v2;
if ( v2 == -1 )
func6("error creating listener socket");
if ( bind(v2, &addr, 0x6Eu) == -1 )
{
close(v3);
func6("can't bind to socket");
}
if ( listen(v3, 10) == -1 )
{
close(v3);
func6("can't listen to socket");
}
signal(15, (__sighandler_t)cleanup);
signal(2, (__sighandler_t)cleanup);
signal(14, (__sighandler_t)sigalarm);
signal(17, handler);
signal(13, handler);
*(_DWORD *)(a1 + 44) = v3;
v4 = signaled;
if ( signaled )
return 0;
do
{
if ( stat(*(const char **)a1, &v10) == -1 )
{
func5("can't stat /etc/rbldomains");
}
else if ( v10.st_mtim.tv_sec > *(_QWORD *)(a1 + 32) )
{
write(2, "rereading /etc/rbldomains\n", 0x1AuLL);
func4(a1);
}
fds.fd = v3;
fds.events = 1;
fds.revents = 0;
poll(&fds, 1uLL, -1);
if ( (fds.revents & 1) != 0 )
{
v6 = accept(v3, 0LL, 0LL);
if ( v6 == -1 )
{
func5("accept failed");
}
else
{
result = fork();
if ( result == -1 )
return result;
if ( !result )
{
close(v3);
*(_DWORD *)(a1 + 40) = v6;
if ( (unsigned int)read(v6, buf, 0x100uLL) != -1 )
{
if ( !inet_aton(buf, &inp) )
func1("invalid ip address");
inp.s_addr = func7(inp.s_addr);
v7 = func10(&inp, a1);
write(v6, &unk_741, 1uLL);
close(v6);
exit(v7);
}
func6("can't read from socket");
}
waitpid(result, 0LL, 0);
}
}
}
while ( !signaled );
return v4;
}

original_code:
int listener(config_t * c)
{
  pid_t pid;
  struct sockaddr_un server;
  int sfd, cfd;
  (*__res_state()).options &= ~0x00000040;
  umask(0111);
  if (unlink(c->socketpath) == -1 && (*__errno_location ()) != 2)
    die_errno("can't unlink " "/tmp/.rbldsock");
  memset((char *) &server, 0, sizeof(server));
  server.sun_family = 1;
  strncpy(server.sun_path, c->socketpath, sizeof(server.sun_path));
  if ((sfd = socket(1, SOCK_STREAM, 0)) == -1)
    die_errno("error creating listener socket");
  if (bind(sfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) == -1) {
    close(sfd);
    die_errno("can't bind to socket");
  }
  if (listen(sfd, 10) == -1) {
    close(sfd);
    die_errno("can't listen to socket");
  }
  signal(15, cleanup);
  signal(2, cleanup);
  signal(14, sigalarm);
  signal(17, ((__sighandler_t) 1));
  signal(13, ((__sighandler_t) 1));
  c->serverfd = sfd;
  while (!signaled) {
    struct stat st;
    struct pollfd pfd;
    if (stat(c->domaincfg, &st) == -1) {
      write_errno("can't stat " "/etc/rbldomains");
    } else if (st.st_mtim.tv_sec > c->timestamp) {
      write(2, "rereading " "/etc/rbldomains" "\n", sizeof("rereading " "/etc/rbldomains" "\n") - 1);
      read_rbldomains(c);
    }
    pfd.fd = sfd;
    pfd.events = 0x001;
    pfd.revents = 0;
    poll(&pfd, 1, -1);
    if ((pfd.revents & 0x001) != 0) {
      if ((cfd = accept(sfd, ((void *)0), ((void *)0))) == -1) {
 write_errno("accept failed");
 continue;
      }
      pid = fork();
      switch (pid) {
      case -1:
 return -1;
      case 0:{
   struct in_addr inaddr;
   char buf[256];
   int retval;
   close(sfd);
   c->clientfd = cfd;
   if ((retval = read(cfd, buf, sizeof(buf))) == -1)
     die_errno("can't read from socket");
   if (inet_aton(buf, &inaddr) == 0)
     die("invalid ip address");
   inaddr.s_addr = htonl(inaddr.s_addr);
   retval = query(&inaddr, c);
   write(cfd, "", 1);
   close(cfd);
   exit(retval);
 }
      default:
 waitpid(pid, ((void *)0), 0);
      }
    }
  }
  return 0;
}

canonical_original_code:
int listener(struct config_t *c)
{
  int pid;
  struct sockaddr_un server;
  int sfd; int cfd;
  (*__res_state()).options &= ~0x00000040;
  umask(0111);
  if (unlink(c->socketpath) == -1 && (*__errno_location ()) != 2)
    die_errno("can't unlink " "/tmp/.rbldsock");
  memset((char *) &server, 0, sizeof(server));
  server.sun_family = 1;
  strncpy(server.sun_path, c->socketpath, sizeof(server.sun_path));
  if ((sfd = socket(1, SOCK_STREAM, 0)) == -1)
    die_errno("error creating listener socket");
  if (bind(sfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) == -1) {
    close(sfd);
    die_errno("can't bind to socket");
  }
  if (listen(sfd, 10) == -1) {
    close(sfd);
    die_errno("can't listen to socket");
  }
  signal(15, cleanup);
  signal(2, cleanup);
  signal(14, sigalarm);
  signal(17, ((void (*)(int)) 1));
  signal(13, ((void (*)(int)) 1));
  c->serverfd = sfd;
  while (!signaled) {
    struct stat st;
    struct pollfd pfd;
    if (stat(c->domaincfg, &st) == -1) {
      write_errno("can't stat " "/etc/rbldomains");
    } else if (st.st_mtim.tv_sec > c->timestamp) {
      write(2, "rereading " "/etc/rbldomains" "\n", sizeof("rereading " "/etc/rbldomains" "\n") - 1);
      read_rbldomains(c);
    }
    pfd.fd = sfd;
    pfd.events = 0x001;
    pfd.revents = 0;
    poll(&pfd, 1, -1);
    if ((pfd.revents & 0x001) != 0) {
      if ((cfd = accept(sfd, ((void *)0), ((void *)0))) == -1) {
 write_errno("accept failed");
 continue;
      }
      pid = fork();
      switch (pid) {
      case -1:
 return -1;
      case 0:{
   struct in_addr inaddr;
   char buf[256];
   int retval;
   close(sfd);
   c->clientfd = cfd;
   if ((retval = read(cfd, buf, sizeof(buf))) == -1)
     die_errno("can't read from socket");
   if (inet_aton(buf, &inaddr) == 0)
     die("invalid ip address");
   inaddr.s_addr = htonl(inaddr.s_addr);
   retval = query(&inaddr, c);
   write(cfd, "", 1);
   close(cfd);
   exit(retval);
 }
      default:
 waitpid(pid, ((void *)0), 0);
      }
    }
  }
  return 0;
}

variable_types: struct config_t { char *domaincfg; char *socketpath; char *pidfile; void *rbldomains; long timestamp; int clientfd; int serverfd; } *

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

variable_types: sfd int

variable_types: cfd int

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

variable_types:
inaddr
struct in_addr { unsigned int s_addr; }

variable_types:
buf
char [256]

variable_types: retval int

user_defined_types: struct timespec { long tv_sec; long tv_nsec; }

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


cleanup

decompiled_code:
void cleanup()
{
signaled = 1;
}

canonical_decompiled_code:
void func0()
{
signaled = 1;
}

original_code:
static void cleanup(int signal)
{
  signaled = 1;
}

variable_types: signal int


die

decompiled_code:
void __fastcall __noreturn die(void *buf)
{
size_t v1; // rax

v1 = strlen((const char *)buf);
write(2, buf, v1);
write(2, &unk_E, 1uLL);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func1(void *buf)
{
size_t v1; // rax

v1 = strlen((const char *)buf);
write(2, buf, v1);
write(2, &unk_E, 1uLL);
exit(1);
}

original_code:
static void die(char *msg)
{
  write(2, msg, strlen(msg));
  write(2, "\n", 1);
  exit(1);
}

variable_types:
msg
char *


decompiled_code:
__int64 __fastcall fmt_ulong(__int64 a1, unsigned __int64 a2)
{
unsigned __int64 v2; // rdx
unsigned int v3; // er8
unsigned __int64 v4; // rcx
__int64 v5; // rdi
unsigned __int64 v6; // rax

if ( a2 <= 9 )
{
v3 = 1;
}
else
{
v2 = a2;
v3 = 1;
do
{
++v3;
v4 = v2;
v2 /= 0xAuLL;
}
while ( v4 > 0x63 );
}
if ( a1 )
{
v5 = v3 + a1;
do
{
*(_BYTE *)--v5 = a2 % 0xA + 48;
v6 = a2;
a2 /= 0xAuLL;
}
while ( v6 > 9 );
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, unsigned __int64 a2)
{
unsigned __int64 v2; // rdx
unsigned int v3; // er8
unsigned __int64 v4; // rcx
__int64 v5; // rdi
unsigned __int64 v6; // rax

if ( a2 <= 9 )
{
v3 = 1;
}
else
{
v2 = a2;
v3 = 1;
do
{
++v3;
v4 = v2;
v2 /= 0xAuLL;
}
while ( v4 > 0x63 );
}
if ( a1 )
{
v5 = v3 + a1;
do
{
*(_BYTE *)--v5 = a2 % 0xA + 48;
v6 = a2;
a2 /= 0xAuLL;
}
while ( v6 > 9 );
}
return v3;
}

original_code:
unsigned int fmt_ulong(register char *s, register unsigned long u)
{
  register unsigned int len;
  register unsigned long q;
  len = 1;
  q = u;
  while (q > 9) {
    ++len;
    q /= 10;
  }
  if (s) {
    s += len;
    do {
      *--s = '0' + (u % 10);
      u /= 10;
    } while (u);
  }
  return len;
}

canonical_original_code:
unsigned int fmt_ulong(char *s, unsigned long u)
{
  register unsigned int len;
  register unsigned long q;
  len = 1;
  q = u;
  while (q > 9) {
    ++len;
    q /= 10;
  }
  if (s) {
    s += len;
    do {
      *--s = '0' + (u % 10);
      u /= 10;
    } while (u);
  }
  return len;
}

variable_types:
s
char *

variable_types:
u
unsigned long

variable_types:
len
unsigned int

variable_types:
q
unsigned long

return_type: unsigned int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
const char *v5; // [rsp+0h] [rbp-38h] BYREF
char *v6; // [rsp+8h] [rbp-30h]
char *name; // [rsp+10h] [rbp-28h]
__int64 v8; // [rsp+18h] [rbp-20h]
__int64 v9; // [rsp+20h] [rbp-18h]
int v10; // [rsp+28h] [rbp-10h]
int fd; // [rsp+2Ch] [rbp-Ch]

v8 = 0LL;
v9 = 0LL;
v10 = 0;
v5 = "/etc/rbldomains";
v6 = "/tmp/.rbldsock";
name = "/var/run/rbld.pid";
fd = -1;
v3 = writepid((__int64)&v5);
if ( v3 == -1 )
die_errno("can't write pid file");
if ( v3 )
die("I'm already running!");
if ( (unsigned int)read_rbldomains((__int64)&v5) == -1 )
die("can't read /etc/rbldomains");
listener((__int64)&v5);
if ( fd != -1 )
close(fd);
unlink(name);
unlink(v6);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // eax
const char *v5; // [rsp+0h] [rbp-38h] BYREF
char *v6; // [rsp+8h] [rbp-30h]
char *name; // [rsp+10h] [rbp-28h]
__int64 v8; // [rsp+18h] [rbp-20h]
__int64 v9; // [rsp+20h] [rbp-18h]
int v10; // [rsp+28h] [rbp-10h]
int fd; // [rsp+2Ch] [rbp-Ch]

v8 = 0LL;
v9 = 0LL;
v10 = 0;
v5 = "/etc/rbldomains";
v6 = "/tmp/.rbldsock";
name = "/var/run/rbld.pid";
fd = -1;
v3 = func9((__int64)&v5);
if ( v3 == -1 )
func6("can't write pid file");
if ( v3 )
func1("I'm already running!");
if ( (unsigned int)func4((__int64)&v5) == -1 )
func1("can't read /etc/rbldomains");
func11((__int64)&v5);
if ( fd != -1 )
close(fd);
unlink(name);
unlink(v6);
return 0;
}

original_code:
int main(int argc, char **argv)
{
  config_t config = {
    .domaincfg = "/etc/rbldomains",
    .socketpath = "/tmp/.rbldsock",
    .pidfile = "/var/run/rbld.pid",
    .rbldomains = ((void *)0),
    .serverfd = -1
  };
  switch (writepid(&config)) {
  case -1:
    die_errno("can't write pid file");
  case 0:
    break;
  default:
    die("I'm already running!");
  }
  if (read_rbldomains(&config) == -1)
    die("can't read " "/etc/rbldomains");
  listener(&config);
  if (config.serverfd != -1)
    close(config.serverfd);
  unlink(config.pidfile);
  unlink(config.socketpath);
  return 0;
}

canonical_original_code:
int main(int argc, char **argv)
{
  struct config_t config = {
    .domaincfg = "/etc/rbldomains",
    .socketpath = "/tmp/.rbldsock",
    .pidfile = "/var/run/rbld.pid",
    .rbldomains = ((void *)0),
    .serverfd = -1
  };
  switch (writepid(&config)) {
  case -1:
    die_errno("can't write pid file");
  case 0:
    break;
  default:
    die("I'm already running!");
  }
  if (read_rbldomains(&config) == -1)
    die("can't read " "/etc/rbldomains");
  listener(&config);
  if (config.serverfd != -1)
    close(config.serverfd);
  unlink(config.pidfile);
  unlink(config.socketpath);
  return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct config_t { char *domaincfg; char *socketpath; char *pidfile; void *rbldomains; long timestamp; int clientfd; int serverfd; }


decompiled_code:
__int64 __fastcall strbuf_check(__int64 a1, __int64 a2)
{
unsigned __int64 v2; // rax
__int64 result; // rax

v2 = 0LL;
if ( a1 )
v2 = (unsigned int)(*(_DWORD *)(a1 + 12) - *(_DWORD *)(a1 + 8));
if ( a2 + 1 > v2 )
result = strbuf_realloc(a1, a2, 0LL);
else
result = 0LL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
unsigned __int64 v2; // rax
__int64 result; // rax

v2 = 0LL;
if ( a1 )
v2 = (unsigned int)(*(_DWORD *)(a1 + 12) - *(_DWORD *)(a1 + 8));
if ( a2 + 1 > v2 )
result = strbuf_realloc(a1, a2, 0LL);
else
result = 0LL;
return result;
}

original_code:
size_t strbuf_check(strbuf_t * strbuf, const size_t len)
{
  if ((strbuf?strbuf->total-strbuf->len:0) < len + 1)
    return strbuf_realloc(strbuf, len);
  return 0;
}

canonical_original_code:
unsigned long strbuf_check(struct strbuf_t *strbuf, unsigned long len)
{
  if ((strbuf?strbuf->total-strbuf->len:0) < len + 1)
    return strbuf_realloc(strbuf, len);
  return 0;
}

variable_types: struct strbuf_t { char *s; unsigned int len; unsigned int total; } *

variable_types:
len
unsigned long

return_type: unsigned long


readulonglong

decompiled_code:
__int64 __fastcall readulonglong(_BYTE *a1, _QWORD *a2)
{
__int64 result; // rax
char v4; // dl
__int64 v5; // rsi
__int64 v6; // rcx

if ( (unsigned __int8)(*a1 - 9) <= 1u || *a1 == 32 )
{
LODWORD(result) = 0;
do
{
do
{
result = (unsigned int)(result + 1);
v4 = a1[(unsigned int)result];
}
while ( (unsigned __int8)(v4 - 9) <= 1u );
}
while ( v4 == 32 );
}
else
{
result = 0LL;
}
v5 = (unsigned __int8)(a1[(unsigned int)result] - 48);
if ( (unsigned __int8)(a1[(unsigned int)result] - 48) > 9u )
{
v6 = 0LL;
}
else
{
v6 = 0LL;
do
{
v6 = v5 + 10 * v6;
result = (unsigned int)(result + 1);
v5 = (unsigned __int8)(a1[(unsigned int)result] - 48);
}
while ( (unsigned __int8)(a1[(unsigned int)result] - 48) <= 9u );
}
*a2 = v6;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_BYTE *a1, _QWORD *a2)
{
__int64 result; // rax
char v4; // dl
__int64 v5; // rsi
__int64 v6; // rcx

if ( (unsigned __int8)(*a1 - 9) <= 1u || *a1 == 32 )
{
LODWORD(result) = 0;
do
{
do
{
result = (unsigned int)(result + 1);
v4 = a1[(unsigned int)result];
}
while ( (unsigned __int8)(v4 - 9) <= 1u );
}
while ( v4 == 32 );
}
else
{
result = 0LL;
}
v5 = (unsigned __int8)(a1[(unsigned int)result] - 48);
if ( (unsigned __int8)(a1[(unsigned int)result] - 48) > 9u )
{
v6 = 0LL;
}
else
{
v6 = 0LL;
do
{
v6 = v5 + 10 * v6;
result = (unsigned int)(result + 1);
v5 = (unsigned __int8)(a1[(unsigned int)result] - 48);
}
while ( (unsigned __int8)(a1[(unsigned int)result] - 48) <= 9u );
}
*a2 = v6;
return result;
}

original_code:
int readulonglong(char *s, unsigned long long *u)
{
  register unsigned int pos = 0;
  unsigned long long result = 0;
  unsigned long long c;
  while (s[pos] == ' ' || s[pos] == '\n' || s[pos] == '\t')
    ++pos;
  while ((c = (unsigned long long) (unsigned char) (s[pos] - '0')) < 10) {
    result = result * 10 + c;
    ++pos;
  }
  *u = result;
  return pos;
}

variable_types:
u
unsigned long long *

variable_types:
pos
unsigned int

variable_types:
result
unsigned long long

variable_types:
c
unsigned long long


readulong

decompiled_code:
__int64 __fastcall readulong(_BYTE *a1, _QWORD *a2)
{
__int64 result; // rax
__int64 v3; // [rsp+8h] [rbp-10h] BYREF

result = readulonglong(a1, &v3);
*a2 = v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(_BYTE *a1, _QWORD *a2)
{
__int64 result; // rax
__int64 v3; // [rsp+8h] [rbp-10h] BYREF

result = func1(a1, &v3);
*a2 = v3;
return result;
}

original_code:
int readulong(char *s, unsigned long *u)
{
  unsigned long long ull;
  int pos = readulonglong(s, &ull);
  *u = (unsigned long) ull;
  return pos;
}

variable_types:
u
unsigned long *

variable_types:
ull
unsigned long long

variable_types: pos int


readlong

original_code:
int readlong(char *s, long *l)
{
  unsigned long long ull;
  int negative = 0, pos;
  char *tmp = s;
  while (*tmp == ' ')
    ++tmp;
  if (*tmp == '-') {
    negative = 1;
    ++tmp;
  }
  pos = readulonglong(tmp, &ull);
  if (negative)
    *l = -(long) ull;
  else
    *l = (long) ull;
  return pos + tmp - s;
}

canonical_original_code:
int readlong(char *s, long *l)
{
  unsigned long long ull;
  int negative = 0; int pos;
  char *tmp = s;
  while (*tmp == ' ')
    ++tmp;
  if (*tmp == '-') {
    negative = 1;
    ++tmp;
  }
  pos = readulonglong(tmp, &ull);
  if (negative)
    *l = -(long) ull;
  else
    *l = (long) ull;
  return pos + tmp - s;
}

variable_types:
l
long *

variable_types: negative int

variable_types:
tmp
char *


readint

decompiled_code:
__int64 __fastcall readint(_BYTE *a1, _DWORD *a2)
{
__int64 result; // rax
__int64 v3; // [rsp+8h] [rbp-10h] BYREF

result = readlong(a1, &v3);
*a2 = v3;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(_BYTE *a1, _DWORD *a2)
{
__int64 result; // rax
__int64 v3; // [rsp+8h] [rbp-10h] BYREF

result = func3(a1, &v3);
*a2 = v3;
return result;
}

original_code:
int readint(char *s, int *i)
{
  long l;
  int pos = readlong(s, &l);
  *i = (int) l;
  return pos;
}

variable_types:
s
char *

variable_types:
i
int *

variable_types: l long


skip

decompiled_code:
char *__fastcall skip(char *a1, int a2)
{
int v2; // ebp
const unsigned __int16 *v3; // rcx
char *v4; // rax

if ( a2 )
{
v2 = a2;
v3 = *_ctype_b_loc();
v4 = a1;
do
{
while ( (v3[*v4] & 0x2000) != 0 )
++v4;
do
++v4;
while ( (v3[*v4] & 0x2000) == 0 );
--v2;
}
while ( v2 );
}
else
{
v4 = a1;
}
return (char *)(v4 - a1);
}

canonical_decompiled_code:
char *__fastcall func8(char *a1, int a2)
{
int v2; // ebp
const unsigned __int16 *v3; // rcx
char *v4; // rax

if ( a2 )
{
v2 = a2;
v3 = *_ctype_b_loc();
v4 = a1;
do
{
while ( (v3[*v4] & 0x2000) != 0 )
++v4;
do
++v4;
while ( (v3[*v4] & 0x2000) == 0 );
--v2;
}
while ( v2 );
}
else
{
v4 = a1;
}
return (char *)(v4 - a1);
}

original_code:
int skip(char *s, int n)
{
  char *pos = s;
  while (n) {
    while (((*__ctype_b_loc ())[(int) ((*pos))] & (unsigned short int) _ISspace))
      ++pos;
    while (!((*__ctype_b_loc ())[(int) ((*pos))] & (unsigned short int) _ISspace))
      ++pos;
    --n;
  }
  return pos - s;
}

canonical_original_code:
int skip(char *s, int n)
{
  char *pos = s;
  while (n) {
    while (((*__ctype_b_loc ())[(int) ((*pos))] & (unsigned short) _ISspace))
      ++pos;
    while (!((*__ctype_b_loc ())[(int) ((*pos))] & (unsigned short) _ISspace))
      ++pos;
    --n;
  }
  return pos - s;
}

variable_types: n int

variable_types:
pos
char *


writeulonglong

original_code:
int writeulonglong(register char *s, char *end, unsigned long long u, unsigned int width,
     int align)
{
  register unsigned int len;
  unsigned long long q;
  int retval;
  len = 1;
  q = u;
  while (q > 9) {
    ++len;
    q /= 10;
  }
  if (len > width)
    width = len;
  retval = width;
  if (s) {
    s += width;
    if (align == 0) {
      while (width > len) {
 *--s = ' ';
 --width;
      }
    }
    do {
      *--s = '0' + (u % 10);
      u /= 10;
    } while (u);
    if (align == 1) {
      width -= len;
      while (width--)
 *--s = ' ';
    }
  }
  return retval;
}

canonical_original_code:
int writeulonglong(char *s, char *end, unsigned long long u, unsigned int width, int align)
{
  register unsigned int len;
  unsigned long long q;
  int retval;
  len = 1;
  q = u;
  while (q > 9) {
    ++len;
    q /= 10;
  }
  if (len > width)
    width = len;
  retval = width;
  if (s) {
    s += width;
    if (align == 0) {
      while (width > len) {
 *--s = ' ';
 --width;
      }
    }
    do {
      *--s = '0' + (u % 10);
      u /= 10;
    } while (u);
    if (align == 1) {
      width -= len;
      while (width--)
 *--s = ' ';
    }
  }
  return retval;
}

variable_types:
end
char *

variable_types:
u
unsigned long long

variable_types:
width
unsigned int

variable_types: align int

variable_types:
len
unsigned int

variable_types:
q
unsigned long long

variable_types: retval int


writelonglong

decompiled_code:
__int64 __fastcall writelonglong(_BYTE *a1, __int64 a2, signed __int64 a3, unsigned int a4, int a5)
{
int v5; // ebx

v5 = 0;
if ( a3 < 0 )
{
*a1 = 45;
a3 = -a3;
++a1;
v5 = 1;
}
return v5 + (unsigned int)writeulonglong((__int64)a1, a2, a3, a4, a5);
}

canonical_decompiled_code:
__int64 __fastcall func10(_BYTE *a1, __int64 a2, signed __int64 a3, unsigned int a4, int a5)
{
int v5; // ebx

v5 = 0;
if ( a3 < 0 )
{
*a1 = 45;
a3 = -a3;
++a1;
v5 = 1;
}
return v5 + (unsigned int)func9((__int64)a1, a2, a3, a4, a5);
}

original_code:
int writelonglong(register char *s, char *end, long long l, unsigned int width,
    int align)
{
  register unsigned int len = 0;
  if (l < 0LL) {
    *s++ = '-';
    ++len;
    l = -l;
  }
  return len + writeulonglong(s, end, (unsigned long long) l, width, align);
}

canonical_original_code:
int writelonglong(char *s, char *end, long long l, unsigned int width, int align)
{
  register unsigned int len = 0;
  if (l < 0LL) {
    *s++ = '-';
    ++len;
    l = -l;
  }
  return len + writeulonglong(s, end, (unsigned long long) l, width, align);
}

variable_types:
l
long long


writestring

decompiled_code:
__int64 __fastcall writestring(char *a1, unsigned __int64 a2, const char *a3, unsigned int a4)
{
char *v4; // rbx
const char *v5; // rbp
int v7; // eax
unsigned int v8; // edx
int v9; // ecx
char v10; // al
char *v11; // rax

v4 = a1;
v5 = a3;
v7 = strlen(a3);
v8 = v7;
if ( v7 > (int)a4 )
{
v9 = 0;
if ( (unsigned __int64)a1 >= a2 )
goto LABEL_7;
}
else
{
v9 = a4 - v7;
if ( (unsigned __int64)a1 >= a2 )
goto LABEL_5;
}
do
{
v10 = *v5;
*v4 = *v5;
if ( !v10 )
break;
++v4;
++v5;
}
while ( a2 > (unsigned __int64)v4 );
LABEL_5:
v11 = &v4[v9];
if ( v9 )
{
do
*v4++ = 32;
while ( v11 != v4 );
}
LABEL_7:
if ( !a4 )
a4 = v8;
return a4;
}

canonical_decompiled_code:
__int64 __fastcall func11(char *a1, unsigned __int64 a2, const char *a3, unsigned int a4)
{
char *v4; // rbx
const char *v5; // rbp
int v7; // eax
unsigned int v8; // edx
int v9; // ecx
char v10; // al
char *v11; // rax

v4 = a1;
v5 = a3;
v7 = strlen(a3);
v8 = v7;
if ( v7 > (int)a4 )
{
v9 = 0;
if ( (unsigned __int64)a1 >= a2 )
goto LABEL_7;
}
else
{
v9 = a4 - v7;
if ( (unsigned __int64)a1 >= a2 )
goto LABEL_5;
}
do
{
v10 = *v5;
*v4 = *v5;
if ( !v10 )
break;
++v4;
++v5;
}
while ( a2 > (unsigned __int64)v4 );
LABEL_5:
v11 = &v4[v9];
if ( v9 )
{
do
*v4++ = 32;
while ( v11 != v4 );
}
LABEL_7:
if ( !a4 )
a4 = v8;
return a4;
}

original_code:
int writestring(char *s, char *end, char *str, int width)
{
  int len = strlen(str);
  int pad = len > width ? 0 : width - len;
  while (s < end) {
    if (!(*s = *str))
      break;
    ++s;
    ++str;
  }
  while (pad) {
    *s++ = ' ';
    --pad;
  }
  return width ? width : len;
}

variable_types:
str
char *

variable_types: width int

variable_types: len int

variable_types: pad int


decompiled_code:
ssize_t __fastcall strbuf_write(__int64 a1, int a2)
{
ssize_t result; // rax
unsigned int v3; // edx

result = 0LL;
if ( a1 )
{
v3 = *(_DWORD *)(a1 + 8);
if ( v3 )
result = write(a2, *(const void **)a1, v3);
}
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func0(__int64 a1, int a2)
{
ssize_t result; // rax
unsigned int v3; // edx

result = 0LL;
if ( a1 )
{
v3 = *(_DWORD *)(a1 + 8);
if ( v3 )
result = write(a2, *(const void **)a1, v3);
}
return result;
}

original_code:
size_t strbuf_write(strbuf_t * strbuf, int fd)
{
  size_t retval = 0;
  if ((strbuf?strbuf->len:0))
    retval = write(fd, strbuf->s, strbuf->len);
  return retval;
}

canonical_original_code:
unsigned long strbuf_write(struct strbuf_t *strbuf, int fd)
{
  unsigned long retval = 0;
  if ((strbuf?strbuf->len:0))
    retval = write(fd, strbuf->s, strbuf->len);
  return retval;
}

variable_types: struct strbuf_t { char *s; unsigned int len; unsigned int total; } *

variable_types: fd int

variable_types:
retval
unsigned long


decompiled_code:
size_t __fastcall strbuf_prepends(__int64 a1, const char *a2)
{
size_t v2; // r13
size_t v3; // rbp

v2 = 0LL;
if ( a2 )
{
v3 = strlen(a2);
v2 = v3;
if ( !strbuf_check(a1, v3) )
{
memmove((void *)(*(_QWORD *)a1 + v3), *(const void **)a1, (unsigned int)(*(_DWORD *)(a1 + 8) + 1));
memcpy(*(void **)a1, a2, v3);
*(_DWORD *)(a1 + 8) += v3;
}
}
return v2;
}

canonical_decompiled_code:
size_t __fastcall func0(__int64 a1, const char *a2)
{
size_t v2; // r13
size_t v3; // rbp

v2 = 0LL;
if ( a2 )
{
v3 = strlen(a2);
v2 = v3;
if ( !strbuf_check(a1, v3) )
{
memmove((void *)(*(_QWORD *)a1 + v3), *(const void **)a1, (unsigned int)(*(_DWORD *)(a1 + 8) + 1));
memcpy(*(void **)a1, a2, v3);
*(_DWORD *)(a1 + 8) += v3;
}
}
return v2;
}

original_code:
size_t strbuf_prepends(strbuf_t * strbuf, const char *s)
{
  size_t len = 0;
  if (s) {
    len = strlen(s);
    if (strbuf_check(strbuf, len) == 0) {
      memmove(strbuf->s + len, strbuf->s, strbuf->len + 1);
      memcpy(strbuf->s, s, len);
      strbuf->len += len;
    }
  }
  return len;
}

canonical_original_code:
unsigned long strbuf_prepends(struct strbuf_t *strbuf, char *s)
{
  unsigned long len = 0;
  if (s) {
    len = strlen(s);
    if (strbuf_check(strbuf, len) == 0) {
      memmove(strbuf->s + len, strbuf->s, strbuf->len + 1);
      memcpy(strbuf->s, s, len);
      strbuf->len += len;
    }
  }
  return len;
}

variable_types:
s
char *

variable_types:
len
unsigned long

user_defined_types: struct strbuf_t { char *s; unsigned int len; unsigned int total; }


tryopen

decompiled_code:
const char *__fastcall tryopen(const char *a1)
{
int v2; // edi
const char *result; // rax

v2 = open(a1, 0);
result = 0LL;
if ( v2 > 0 )
{
close(v2);
result = a1;
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func1(const char *a1)
{
int v2; // edi
const char *result; // rax

v2 = open(a1, 0);
result = 0LL;
if ( v2 > 0 )
{
close(v2);
result = a1;
}
return result;
}

original_code:
char *tryopen(const char *path)
{
  int fd;
  char *result = ((void *)0);
  if ((fd = open(path, 00)) > 0) {
    close(fd);
    result = (char *) path;
  }
  return result;
}

canonical_original_code:
char *tryopen(char *path)
{
  int fd;
  char *result = ((void *)0);
  if ((fd = open(path, 00)) > 0) {
    close(fd);
    result = (char *) path;
  }
  return result;
}

variable_types:
path
char *

variable_types:
result
char *


decompiled_code:
void __fastcall __noreturn die_protocol(__int64 a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9)
{
*(_DWORD *)(a1 + 20) = -3;
die(a1, "protocol not supported", 0LL, a2, a3, a4, a5, a6, a7, a8, a9);
}

canonical_decompiled_code:
void __fastcall __noreturn func4(__int64 a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9)
{
*(_DWORD *)(a1 + 20) = -3;
func3(a1, "protocol not supported", 0LL, a2, a3, a4, a5, a6, a7, a8, a9);
}

original_code:
void die_protocol(connection_t * conn)
{
  conn->code = -3;
  die(conn, "protocol not supported", ((void *)0));
}

canonical_original_code:
void die_protocol(struct connection_s *conn)
{
  conn->code = -3;
  die(conn, "protocol not supported", ((void *)0));
}

variable_types: struct connection_s { int sockfd; int postfd; int outfd; int conffd; int httpversion; int code; char *outfile; char *caFile; char *ccFile; char *ckFile; char *cwd; int timeout; int method; char *postdata; unsigned long postlen; char *uri; struct destination_t { char *host; char *auth; int port; } *remote; struct destination_t *proxy; struct response_header_t { char *reasonstr; char *location; char *digest; unsigned long contentlength; unsigned long chunklength; unsigned long bodylength; unsigned long totallength; unsigned long headerlength; int chunked; int reason; } *response; void *ssl; char *ressource; struct GTtimer_t { struct timeval { long tv_sec; long tv_usec; } start; struct timeval stop; unsigned long elapsed; } connect_timer; struct GTtimer_t timer; long (*write)(struct connection_s *, char *, unsigned long); long (*read)(struct connection_s *, char *, unsigned long); unsigned int verbosity; unsigned int flags; } *

user_defined_types: struct connection_s { int sockfd; int postfd; int outfd; int conffd; int httpversion; int code; char *outfile; char *caFile; char *ccFile; char *ckFile; char *cwd; int timeout; int method; char *postdata; unsigned long postlen; char *uri; struct destination_t *remote; struct destination_t *proxy; struct response_header_t *response; void *ssl; char *ressource; struct GTtimer_t connect_timer; struct GTtimer_t timer; long (*write)(struct connection_s *, char *, unsigned long); long (*read)(struct connection_s *, char *, unsigned long); unsigned int verbosity; unsigned int flags; }

user_defined_types: struct GTtimer_t { struct timeval start; struct timeval stop; unsigned long elapsed; }


decompiled_code:
void __fastcall __noreturn die_sys(__int64 a1, const void *a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10)
{
int *v10; // rax
char *v11; // rax
double v12; // xmm4_8
double v13; // xmm5_8

*(_DWORD *)(a1 + 20) = -1;
v10 = _errno_location();
v11 = strerror(*v10);
die(a1, a2, (__int64)v11, a3, a4, a5, a6, v12, v13, a9, a10);
}

canonical_decompiled_code:
void __fastcall __noreturn func5(__int64 a1, const void *a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10)
{
int *v10; // rax
char *v11; // rax
double v12; // xmm4_8
double v13; // xmm5_8

*(_DWORD *)(a1 + 20) = -1;
v10 = _errno_location();
v11 = strerror(*v10);
func3(a1, a2, (__int64)v11, a3, a4, a5, a6, v12, v13, a9, a10);
}

original_code:
void die_sys(connection_t * conn, const char *message)
{
  conn->code = -1;
  die(conn, message, strerror((*__errno_location ())));
}

canonical_original_code:
void die_sys(struct connection_s *conn, char *message)
{
  conn->code = -1;
  die(conn, message, strerror((*__errno_location ())));
}

variable_types:
message
char *

user_defined_types: struct destination_t { char *host; char *auth; int port; }

user_defined_types: struct response_header_t { char *reasonstr; char *location; char *digest; unsigned long contentlength; unsigned long chunklength; unsigned long bodylength; unsigned long totallength; unsigned long headerlength; int chunked; int reason; }

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


decompiled_code:
void __fastcall __noreturn die_write(__int64 a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9)
{
die_sys(a1, "write error", a2, a3, a4, a5, a6, a7, a8, a9);
}

canonical_decompiled_code:
void __fastcall __noreturn func6(__int64 a1, double a2, double a3, double a4, double a5, double a6, double a7, double a8, double a9)
{
func5(a1, "write error", a2, a3, a4, a5, a6, a7, a8, a9);
}

original_code:
void die_write(connection_t * conn)
{
  die_sys(conn, "write error");
}

canonical_original_code:
void die_write(struct connection_s *conn)
{
  die_sys(conn, "write error");
}


decompiled_code:
const char *__fastcall tryopen_alt(__int64 a1, const char *a2, const char *a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11)
{
const char *v11; // rbx
double v12; // xmm4_8
double v13; // xmm5_8
double v16; // xmm4_8
double v17; // xmm5_8

if ( a2 )
{
v11 = tryopen(a2);
if ( !v11 )
die_sys(a1, a2, a4, a5, a6, a7, v12, v13, a10, a11);
}
else
{
v11 = tryopen(a3);
if ( !v11 && *_errno_location() != 2 )
die_sys(a1, a3, a4, a5, a6, a7, v16, v17, a10, a11);
}
return v11;
}

canonical_decompiled_code:
const char *__fastcall func7(__int64 a1, const char *a2, const char *a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11)
{
const char *v11; // rbx
double v12; // xmm4_8
double v13; // xmm5_8
double v16; // xmm4_8
double v17; // xmm5_8

if ( a2 )
{
v11 = func1(a2);
if ( !v11 )
func5(a1, a2, a4, a5, a6, a7, v12, v13, a10, a11);
}
else
{
v11 = func1(a3);
if ( !v11 && *_errno_location() != 2 )
func5(a1, a3, a4, a5, a6, a7, v16, v17, a10, a11);
}
return v11;
}

original_code:
char *tryopen_alt(connection_t * conn, const char *file1, const char *file2)
{
  char *result = ((void *)0);
  if (file1) {
    if (!(result = tryopen(file1)))
      die_sys(conn, file1);
  } else {
    if ((result = tryopen(file2)) == ((void *)0) && (*__errno_location ()) != 2)
      die_sys(conn, file2);
  }
  return result;
}

canonical_original_code:
char *tryopen_alt(struct connection_s *conn, char *file1, char *file2)
{
  char *result = ((void *)0);
  if (file1) {
    if (!(result = tryopen(file1)))
      die_sys(conn, file1);
  } else {
    if ((result = tryopen(file2)) == ((void *)0) && (*__errno_location ()) != 2)
      die_sys(conn, file2);
  }
  return result;
}


cleanup

original_code:
void cleanup(connection_t * conn, int how)
{
  if (conn->flags & (1<<0))
    gtget_ssl_close(conn);
  if (conn->sockfd > 0)
    close(conn->sockfd);
  memset(conn->response, 0, sizeof(response_header_t));
  conn->ressource = ((void *)0);
  conn->remote->host = ((void *)0);
  if (how) {
    if (conn->postfd > 0)
      close(conn->postfd);
    conn->postfd = 0;
    if (conn->outfd > 1)
      close(conn->outfd);
    conn->outfd = -1;
    if (conn->conffd > 1)
      close(conn->conffd);
    conn->conffd = -1;
    if (how > 1)
      unlink(conn->outfile);
  }
}

canonical_original_code:
void cleanup(struct connection_s *conn, int how)
{
  if (conn->flags & (1<<0))
    gtget_ssl_close(conn);
  if (conn->sockfd > 0)
    close(conn->sockfd);
  memset(conn->response, 0, sizeof(response_header_t));
  conn->ressource = ((void *)0);
  conn->remote->host = ((void *)0);
  if (how) {
    if (conn->postfd > 0)
      close(conn->postfd);
    conn->postfd = 0;
    if (conn->outfd > 1)
      close(conn->outfd);
    conn->outfd = -1;
    if (conn->conffd > 1)
      close(conn->conffd);
    conn->conffd = -1;
    if (how > 1)
      unlink(conn->outfile);
  }
}

variable_types: how int


die

decompiled_code:
void __fastcall __noreturn die(__int64 a1, const void *a2, __int64 a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11)
{
size_t v12; // rax
double v13; // xmm4_8
double v14; // xmm5_8

v12 = str_len(a2);
write(2, a2, v12);
if ( a3 )
write2f(": %s\n", a4, a5, a6, a7, v13, v14, a10, a11, a3);
else
write(2, "\n", 1uLL);
cleanup(a1, 2);
exit(1);
}

canonical_decompiled_code:
void __fastcall __noreturn func3(__int64 a1, const void *a2, __int64 a3, double a4, double a5, double a6, double a7, double a8, double a9, double a10, double a11)
{
size_t v12; // rax
double v13; // xmm4_8
double v14; // xmm5_8

v12 = str_len(a2);
write(2, a2, v12);
if ( a3 )
func2(": %s\n", a4, a5, a6, a7, v13, v14, a10, a11, a3);
else
write(2, "\n", 1uLL);
func0(a1, 2);
exit(1);
}

original_code:
void die(connection_t * conn, const char *msg1, const char *msg2)
{
  write(2, msg1, str_len(msg1));
  if (msg2)
    write2f(": %s\n", msg2);
  else
    write(2, "\n", 1);
  cleanup(conn, 2);
  _exit(1);
}

canonical_original_code:
void die(struct connection_s *conn, char *msg1, char *msg2)
{
  write(2, msg1, str_len(msg1));
  if (msg2)
    write2f(": %s\n", msg2);
  else
    write(2, "\n", 1);
  cleanup(conn, 2);
  _exit(1);
}


decompiled_code:
__int64 __fastcall strbuf_vappendf(__int64 a1, const char *a2, void *a3)
{
int v3; // eax
__int64 v4; // rdx
char *ptr; // [rsp+8h] [rbp-10h] BYREF

ptr = 0LL;
v3 = vasprintf(&ptr, a2, a3);
v4 = 0LL;
if ( v3 != -1 )
v4 = strbuf_nappends(a1, ptr, v3);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, const char *a2, void *a3)
{
int v3; // eax
__int64 v4; // rdx
char *ptr; // [rsp+8h] [rbp-10h] BYREF

ptr = 0LL;
v3 = vasprintf(&ptr, a2, a3);
v4 = 0LL;
if ( v3 != -1 )
v4 = strbuf_nappends(a1, ptr, v3);
return v4;
}

original_code:
size_t strbuf_vappendf(strbuf_t * strbuf, const char *format, va_list ap)
{
  size_t len = 0;
  int r;
  char *s = ((void *)0);
  if (-1 != (r = vasprintf(&s, format, ap))) {
    len = strbuf_nappends(strbuf, s, r);
  }
  return len;
}

canonical_original_code:
unsigned long strbuf_vappendf(struct strbuf_t *strbuf, char *format, __builtin_va_list ap)
{
  unsigned long len = 0;
  int r;
  char *s = ((void *)0);
  if (-1 != (r = vasprintf(&s, format, ap))) {
    len = strbuf_nappends(strbuf, s, r);
  }
  return len;
}

variable_types:
format
char *

variable_types: r int


decompiled_code:
__int64 __fastcall fmt_long(_BYTE *a1, __int64 a2)
{
if ( a2 >= 0 )
return ((__int64 (*)(void))fmt_ulong)();
*a1 = 45;
return fmt_ulong(a1 + 1, -a2) + 1;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1, __int64 a2)
{
if ( a2 >= 0 )
return ((__int64 (*)(void))fmt_ulong)();
*a1 = 45;
return fmt_ulong(a1 + 1, -a2) + 1;
}

original_code:
size_t fmt_long(char *dest, long n)
{
  if (n >= 0)
    return fmt_ulong(dest, n);
  *dest++ = '-';
  return 1 + fmt_ulong(dest, -n);
}

canonical_original_code:
unsigned long fmt_long(char *dest, long n)
{
  if (n >= 0)
    return fmt_ulong(dest, n);
  *dest++ = '-';
  return 1 + fmt_ulong(dest, -n);
}

variable_types:
dest
char *

variable_types: n long


decompiled_code:
__int64 __fastcall strbuf_appendllx(__int64 a1)
{
char v2[56]; // [rsp+0h] [rbp-38h] BYREF

v2[(int)fmt_xlonglong(v2)] = 0;
return strbuf_appends(a1, v2);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1)
{
char v2[56]; // [rsp+0h] [rbp-38h] BYREF

v2[(int)fmt_xlonglong(v2)] = 0;
return strbuf_appends(a1, v2);
}

original_code:
size_t strbuf_appendllx(strbuf_t * strbuf, const unsigned long long l)
{
  char buf[41];
  int len;
  len = fmt_xlonglong(buf, l);
  buf[len] = '\0';
  return strbuf_appends(strbuf, buf);
}

canonical_original_code:
unsigned long strbuf_appendllx(struct strbuf_t *strbuf, unsigned long long l)
{
  char buf[41];
  int len;
  len = fmt_xlonglong(buf, l);
  buf[len] = '\0';
  return strbuf_appends(strbuf, buf);
}

variable_types:
l
unsigned long long

variable_types:
buf
char [41]


decompiled_code:
size_t __fastcall strbuf_nappends(__int64 a1, const char *a2, size_t a3)
{
size_t v3; // rbx
size_t v5; // rax
size_t v6; // rbp

v3 = 0LL;
if ( a2 )
{
v5 = strlen(a2);
v3 = a3;
if ( v5 <= a3 )
v3 = v5;
if ( a1 )
{
v6 = v3 + 1;
if ( v3 + 1 <= (unsigned int)(*(_DWORD *)(a1 + 12) - *(_DWORD *)(a1 + 8)) )
{
LABEL_6:
memcpy((void *)(*(_QWORD *)a1 + *(unsigned int *)(a1 + 8)), a2, v6);
*(_DWORD *)(a1 + 8) += v3;
return v3;
}
}
else
{
v6 = v3 + 1;
}
if ( strbuf_realloc(a1, v3) == -1 )
return 0LL;
goto LABEL_6;
}
return v3;
}

canonical_decompiled_code:
size_t __fastcall func0(__int64 a1, const char *a2, size_t a3)
{
size_t v3; // rbx
size_t v5; // rax
size_t v6; // rbp

v3 = 0LL;
if ( a2 )
{
v5 = strlen(a2);
v3 = a3;
if ( v5 <= a3 )
v3 = v5;
if ( a1 )
{
v6 = v3 + 1;
if ( v3 + 1 <= (unsigned int)(*(_DWORD *)(a1 + 12) - *(_DWORD *)(a1 + 8)) )
{
LABEL_6:
memcpy((void *)(*(_QWORD *)a1 + *(unsigned int *)(a1 + 8)), a2, v6);
*(_DWORD *)(a1 + 8) += v3;
return v3;
}
}
else
{
v6 = v3 + 1;
}
if ( strbuf_realloc(a1, v3) == -1 )
return 0LL;
goto LABEL_6;
}
return v3;
}

original_code:
size_t strbuf_nappends(strbuf_t * strbuf, const char *s, const size_t n)
{
  size_t len = 0;
  if (s) {
    len = strlen(s);
    len = (n < len) ? n : len;
    if ((strbuf?strbuf->total-strbuf->len:0) < len + 1) {
      if (strbuf_realloc(strbuf, len) == (size_t)-1)
 return 0;
    }
    memcpy(strbuf->s + strbuf->len, s, len + 1);
    strbuf->len += len;
  }
  return len;
}

canonical_original_code:
unsigned long strbuf_nappends(struct strbuf_t *strbuf, char *s, unsigned long n)
{
  unsigned long len = 0;
  if (s) {
    len = strlen(s);
    len = (n < len) ? n : len;
    if ((strbuf?strbuf->total-strbuf->len:0) < len + 1) {
      if (strbuf_realloc(strbuf, len) == (unsigned long)-1)
 return 0;
    }
    memcpy(strbuf->s + strbuf->len, s, len + 1);
    strbuf->len += len;
  }
  return len;
}

variable_types:
n
unsigned long


original_code:
int parse_mac(unsigned char *mac, char *str)
{
  int i;
  int count;
  char c;
  unsigned char val;
  int colon_ok = 1;
  for (i = 0; i < 6; i++) {
    mac[i] = 0;
  }
  for (i = 0; i < 6; i++) {
    count = 0;
    val = 0;
    do {
      c = toupper(*str++);
      if (c >= '0' && c <= '9') {
 val = (val * 16) + (c - '0');
      } else if (c >= 'A' && c <= 'F') {
 val = (val * 16) + (c - 'A') + 10;
      } else if (c == ':') {
 if (colon_ok || count-- != 0)
   break;
      } else if (c == '\0') {
 str--;
 break;
      } else {
 return 0;
      }
      colon_ok = 1;
    } while (++count < 2);
    colon_ok = (count < 2);
    *mac++ = val;
  }
  if (*str)
    return 0;
  return 1;
}

variable_types:
mac
unsigned char *

variable_types:
str
char *

variable_types: i int

variable_types: count int

variable_types: c char

variable_types:
val
unsigned char


usage

decompiled_code:
void __fastcall __noreturn usage(void *buf)
{
size_t v1; // rax

write(1, "Usage: ", 8uLL);
v1 = strlen((const char *)buf);
write(1, buf, v1);
write(
1,
"[options] [mac] [broadcast] [port]\n"
" -b addr broadcast address\n"
" -m mac mac address of host\n"
" -p port UDP port to broadcast to\n"
" -v version\n",
0xA7uLL);
exit(0);
}

canonical_decompiled_code:
void __fastcall __noreturn func8(void *buf)
{
size_t v1; // rax

write(1, "Usage: ", 8uLL);
v1 = strlen((const char *)buf);
write(1, buf, v1);
write(
1,
"[options] [mac] [broadcast] [port]\n"
" -b addr broadcast address\n"
" -m mac mac address of host\n"
" -p port UDP port to broadcast to\n"
" -v version\n",
0xA7uLL);
exit(0);
}

original_code:
void usage(char *name)
{
  write(1, "Usage: ", sizeof("Usage: "));
  write(1, name, strlen(name));
  write(1, "[options] [mac] [broadcast] [port]\n" "    -b addr    broadcast address\n" "    -m mac     mac address of host\n" "    -p port    UDP port to broadcast to\n" "    -v         version\n", sizeof("[options] [mac] [broadcast] [port]\n" "    -b addr    broadcast address\n" "    -m mac     mac address of host\n" "    -p port    UDP port to broadcast to\n" "    -v         version\n"));
  exit(0);
}

variable_types:
name
char *


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int16 v3; // r12
const char *v4; // r14
char *v5; // r15
int v6; // eax
int v7; // eax
char **v8; // rcx
const char **v9; // rcx
const char **v10; // rdx
int v11; // ebp
__int16 v12; // ax
char *v13; // rcx
size_t v14; // rax
__int64 i; // rax
int v16; // eax
int v17; // ebx
int *v19; // rbx
char *v20; // rax
size_t v21; // rbp
char *v22; // rax
char v23[6]; // [rsp+6h] [rbp-462h] BYREF
struct in_addr inp; // [rsp+Ch] [rbp-45Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-458h] BYREF
char buf[6]; // [rsp+20h] [rbp-448h] BYREF
char v27; // [rsp+26h] [rbp-442h] BYREF
char v28; // [rsp+86h] [rbp-3E2h] BYREF
int optval[15]; // [rsp+42Ch] [rbp-3Ch] BYREF

optval[0] = 1;
v3 = _bswap_16(0x7FFF);
v4 = "255.255.255.255";
v5 = "00a0c9852a5f";
while ( 1 )
{
v6 = getopt(argc, (char *const *)argv, "hvp:m:b:");
if ( v6 == -1 )
break;
if ( v6 == 63 )
usage((void *)*argv);
}
v7 = _bss_start;
v8 = (char **)&argv[_bss_start];
if ( *v8 )
{
++_bss_start;
v5 = *v8;
v9 = &argv[v7 + 1];
if ( *v9 )
{
_bss_start = v7 + 2;
v4 = *v9;
v10 = &argv[v7 + 2];
if ( *v10 )
{
v11 = v7 + 3;
_bss_start = v7 + 3;
v12 = atoi(*v10);
v3 = _bswap_16(v12);
if ( argv[v11] )
usage((void *)*argv);
}
}
}
if ( !parse_mac(v23, v5) )
{
write(1, "Illegal MAC address: ", 0x16uLL);
v14 = strlen(v5);
write(1, v5, v14);
write(1, "\n", 2uLL);
exit(1);
}
if ( !inet_aton(v4, &inp) )
inp.s_addr = **(_DWORD **)gethostbyname(v4)->h_addr_list;
buf[0] = -1;
buf[1] = -1;
buf[2] = -1;
buf[3] = -1;
buf[4] = -1;
buf[5] = -1;
v13 = &v27;
do
{
for ( i = 0LL; i != 6; ++i )
v13[i] = v23[i];
v13 += 6;
}
while ( v13 != &v28 );
*(_QWORD *)&addr.sa_data[6] = 0LL;
addr.sa_family = 2;
*(struct in_addr *)&addr.sa_data[2] = inp;
*(_WORD *)addr.sa_data = v3;
v16 = socket(2, 2, 0);
v17 = v16;
if ( v16 < 0 )
{
write(1, "Can't allocate socket\n", 0x17uLL);
exit(1);
}
if ( setsockopt(v16, 1, 6, optval, 4u) < 0 )
{
write(1, "Can't set socket option SO_BROADCAST: ", 0x27uLL);
v19 = __errno_location();
v20 = strerror(*v19);
v21 = strlen(v20);
v22 = strerror(*v19);
write(1, v22, v21);
write(1, "\n", 2uLL);
exit(1);
}
sendto(v17, buf, 0x66uLL, 0, &addr, 0x10u);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int16 v3; // r12
const char *v4; // r14
char *v5; // r15
int v6; // eax
int v7; // eax
char **v8; // rcx
const char **v9; // rcx
const char **v10; // rdx
int v11; // ebp
__int16 v12; // ax
char *v13; // rcx
size_t v14; // rax
__int64 i; // rax
int v16; // eax
int v17; // ebx
int *v19; // rbx
char *v20; // rax
size_t v21; // rbp
char *v22; // rax
char v23[6]; // [rsp+6h] [rbp-462h] BYREF
struct in_addr inp; // [rsp+Ch] [rbp-45Ch] BYREF
struct sockaddr addr; // [rsp+10h] [rbp-458h] BYREF
char buf[6]; // [rsp+20h] [rbp-448h] BYREF
char v27; // [rsp+26h] [rbp-442h] BYREF
char v28; // [rsp+86h] [rbp-3E2h] BYREF
int optval[15]; // [rsp+42Ch] [rbp-3Ch] BYREF

optval[0] = 1;
v3 = func7(0x7FFF);
v4 = "255.255.255.255";
v5 = "00a0c9852a5f";
while ( 1 )
{
v6 = getopt(argc, (char *const *)argv, "hvp:m:b:");
if ( v6 == -1 )
break;
if ( v6 == 63 )
func8((void *)*argv);
}
v7 = _bss_start;
v8 = (char **)&argv[_bss_start];
if ( *v8 )
{
++_bss_start;
v5 = *v8;
v9 = &argv[v7 + 1];
if ( *v9 )
{
_bss_start = v7 + 2;
v4 = *v9;
v10 = &argv[v7 + 2];
if ( *v10 )
{
v11 = v7 + 3;
_bss_start = v7 + 3;
v12 = atoi(*v10);
v3 = func7(v12);
if ( argv[v11] )
func8((void *)*argv);
}
}
}
if ( !func9(v23, v5) )
{
write(1, "Illegal MAC address: ", 0x16uLL);
v14 = strlen(v5);
write(1, v5, v14);
write(1, "\n", 2uLL);
exit(1);
}
if ( !inet_aton(v4, &inp) )
inp.s_addr = **(_DWORD **)gethostbyname(v4)->h_addr_list;
buf[0] = -1;
buf[1] = -1;
buf[2] = -1;
buf[3] = -1;
buf[4] = -1;
buf[5] = -1;
v13 = &v27;
do
{
for ( i = 0LL; i != 6; ++i )
v13[i] = v23[i];
v13 += 6;
}
while ( v13 != &v28 );
*(_QWORD *)&addr.sa_data[6] = 0LL;
addr.sa_family = 2;
*(struct in_addr *)&addr.sa_data[2] = inp;
*(_WORD *)addr.sa_data = v3;
v16 = socket(2, 2, 0);
v17 = v16;
if ( v16 < 0 )
{
write(1, "Can't allocate socket\n", 0x17uLL);
exit(1);
}
if ( setsockopt(v16, 1, 6, optval, 4u) < 0 )
{
write(1, "Can't set socket option SO_BROADCAST: ", 0x27uLL);
v19 = __errno_location();
v20 = strerror(*v19);
v21 = strlen(v20);
v22 = strerror(*v19);
write(1, v22, v21);
write(1, "\n", 2uLL);
exit(1);
}
sendto(v17, buf, 0x66uLL, 0, &addr, 0x10u);
return 0;
}

original_code:
int main(int argc, char *argv[])
{
  int sock;
  int optval = 1;
  int version = 0;
  int i, c, rc;
  size_t j;
  char msg[1024];
  int msglen = 0;
  struct sockaddr_in bcast;
  struct hostent *he;
  struct in_addr inaddr;
  unsigned char macaddr[6];
  char *mac = "00a0c9852a5f";
  char *target = "255.255.255.255";
  short bport = htons(32767);
  while ((c = getopt(argc, argv, "hvp:m:b:")) != (-1)) {
    switch (c) {
    case 'b':
      target = optarg;
      break;
    case 'm':
      mac = optarg;
      break;
    case 'p':
      bport = htons(atoi(optarg));
      break;
    case 'v':
      version++;
      break;
    case 'h':
    case '?':
      usage(argv[0]);
    }
  }
  if (version) {
    write(1, "Version: ", sizeof("Version: "));
    write(1, versionid, strlen(versionid));
    write(1, "\n", sizeof("\n"));
    exit(0);
  }
  if (argv[optind] != ((void *)0)) {
    mac = argv[optind++];
  }
  if (argv[optind] != ((void *)0)) {
    target = argv[optind++];
  }
  if (argv[optind] != ((void *)0)) {
    bport = htons(atoi(argv[optind++]));
  }
  if (argv[optind] != ((void *)0)) {
    usage(argv[0]);
  }
  if (!parse_mac(macaddr, mac)) {
    write(1, "Illegal MAC address: ", sizeof("Illegal MAC address: "));
    write(1, mac, strlen(mac));
    write(1, "\n", sizeof("\n"));
    exit(1);
  }
  if (!inet_aton(target, &inaddr)) {
    he = gethostbyname(target);
    inaddr = *(struct in_addr *) he->h_addr_list[0];
  }
  for (i = 0; i < 6; i++) {
    msg[msglen++] = 0xff;
  }
  for (i = 0; i < 16; i++) {
    for (j = 0; j < sizeof(macaddr); j++) {
      msg[msglen++] = macaddr[j];
    }
  }
  memset(&bcast, 0, sizeof(bcast));
  bcast.sin_family = 2;
  bcast.sin_addr.s_addr = inaddr.s_addr;
  bcast.sin_port = bport;
  sock = socket(2, SOCK_DGRAM, 0);
  if (sock < 0) {
    write(1, "Can't allocate socket\n", sizeof("Can't allocate socket\n"));
    exit(1);
  }
  if ((rc =
       setsockopt(sock, 1, 6, &optval,
    sizeof(optval))) < 0) {
    write(1, "Can't set socket option SO_BROADCAST: ", sizeof("Can't set socket option SO_BROADCAST: "));
    write(1, strerror((*__errno_location ())), strlen(strerror((*__errno_location ()))));
    write(1, "\n", sizeof("\n"));
    exit(1);
  }
  sendto(sock, &msg, msglen, 0, (struct sockaddr *) &bcast, sizeof(bcast));
  return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
  int sock;
  int optval = 1;
  int version = 0;
  int i; int c; int rc;
  unsigned long j;
  char msg[1024];
  int msglen = 0;
  struct sockaddr_in bcast;
  struct hostent *he;
  struct in_addr inaddr;
  unsigned char macaddr[6];
  char *mac = "00a0c9852a5f";
  char *target = "255.255.255.255";
  short bport = htons(32767);
  while ((c = getopt(argc, argv, "hvp:m:b:")) != (-1)) {
    switch (c) {
    case 'b':
      target = optarg;
      break;
    case 'm':
      mac = optarg;
      break;
    case 'p':
      bport = htons(atoi(optarg));
      break;
    case 'v':
      version++;
      break;
    case 'h':
    case '?':
      usage(argv[0]);
    }
  }
  if (version) {
    write(1, "Version: ", sizeof("Version: "));
    write(1, versionid, strlen(versionid));
    write(1, "\n", sizeof("\n"));
    exit(0);
  }
  if (argv[optind] != ((void *)0)) {
    mac = argv[optind++];
  }
  if (argv[optind] != ((void *)0)) {
    target = argv[optind++];
  }
  if (argv[optind] != ((void *)0)) {
    bport = htons(atoi(argv[optind++]));
  }
  if (argv[optind] != ((void *)0)) {
    usage(argv[0]);
  }
  if (!parse_mac(macaddr, mac)) {
    write(1, "Illegal MAC address: ", sizeof("Illegal MAC address: "));
    write(1, mac, strlen(mac));
    write(1, "\n", sizeof("\n"));
    exit(1);
  }
  if (!inet_aton(target, &inaddr)) {
    he = gethostbyname(target);
    inaddr = *(struct in_addr *) he->h_addr_list[0];
  }
  for (i = 0; i < 6; i++) {
    msg[msglen++] = 0xff;
  }
  for (i = 0; i < 16; i++) {
    for (j = 0; j < sizeof(macaddr); j++) {
      msg[msglen++] = macaddr[j];
    }
  }
  memset(&bcast, 0, sizeof(bcast));
  bcast.sin_family = 2;
  bcast.sin_addr.s_addr = inaddr.s_addr;
  bcast.sin_port = bport;
  sock = socket(2, SOCK_DGRAM, 0);
  if (sock < 0) {
    write(1, "Can't allocate socket\n", sizeof("Can't allocate socket\n"));
    exit(1);
  }
  if ((rc =
       setsockopt(sock, 1, 6, &optval,
    sizeof(optval))) < 0) {
    write(1, "Can't set socket option SO_BROADCAST: ", sizeof("Can't set socket option SO_BROADCAST: "));
    write(1, strerror((*__errno_location ())), strlen(strerror((*__errno_location ()))));
    write(1, "\n", sizeof("\n"));
    exit(1);
  }
  sendto(sock, &msg, msglen, 0, (struct sockaddr *) &bcast, sizeof(bcast));
  return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: sock int

variable_types: optval int

variable_types: version int

variable_types: c int

variable_types: rc int

variable_types:
j
unsigned long

variable_types:
msg
char [1024]

variable_types: msglen int

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

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

variable_types:
inaddr
struct in_addr { unsigned int s_addr; }

variable_types:
macaddr
unsigned char [6]

variable_types:
mac
char *

variable_types:
target
char *

variable_types: bport short

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