decompiled_code:
__int64 __fastcall ndn_setup_socket(__int64 a1, void (*a2)(__int64, const char *, ...), __int64 a3, __int64 (__fastcall *a4)(__int64, sockaddr *, _QWORD), __int64 a5, int *a6)
{
int v9; // eax
const char *v10; // r12
size_t v11; // r15
const char *v12; // rdi
int v13; // eax
__int64 v14; // rdx
const char *v15; // rdi
char *v16; // r12
int v17; // er15
const char *v18; // r12
int v19; // edi
struct addrinfo *v20; // rax
int v21; // eax
unsigned int v22; // er12
int v23; // eax
int v24; // edi
const char *v26; // rdi
const char *v27; // rax
int v28; // eax
int *v29; // rax
char *v30; // rax
int *v31; // rax
char *v32; // rax
int *v33; // rax
char *v34; // rax
int *v35; // rax
char *v36; // rax
int v37; // edi
const char *v38; // rax
int v39; // edi
int v40; // eax
const char *v41; // rdi
char *v42; // r12
int *v43; // rax
char *v44; // rax
int v46; // [rsp+8h] [rbp-A0h]
int *v48; // [rsp+18h] [rbp-90h]
int optval; // [rsp+24h] [rbp-84h] BYREF
struct addrinfo *v50; // [rsp+28h] [rbp-80h] BYREF
struct addrinfo *ai; // [rsp+30h] [rbp-78h] BYREF
struct addrinfo *pai; // [rsp+38h] [rbp-70h] BYREF
struct addrinfo req; // [rsp+40h] [rbp-68h] BYREF

*(_QWORD *)&req.ai_flags = 0LL;
*(_QWORD *)&req.ai_socktype = 0LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
pai = 0LL;
ai = 0LL;
v50 = 0LL;
optval = 1;
if ( a2 )
a2(a3, "at ndn_sockcreate.c:%d", 194LL);
*a6 = -1;
a6[1] = -1;
v9 = *(_DWORD *)a1;
if ( *(int *)a1 > 0 )
req.ai_protocol = *(_DWORD *)a1;
if ( v9 == 17 )
{
req.ai_socktype = 2;
}
else if ( v9 == 6 )
{
req.ai_socktype = 1;
}
req.ai_flags = 4;
v10 = *(const char **)(a1 + 16);
if ( !v10 || (v11 = strspn(*(const char **)(a1 + 16), "0123456789"), v11 != strlen(v10)) )
{
if ( a2 )
a2(a3, "must specify numeric port");
v46 = -1;
v22 = -1;
goto LABEL_41;
}
if ( a2 )
{
a2(a3, "at ndn_sockcreate.c:%d", 208LL);
v12 = *(const char **)(a1 + 24);
if ( !v12 )
{
LABEL_15:
a2(a3, "at ndn_sockcreate.c:%d", 219LL);
v14 = *(unsigned int *)(a1 + 32);
if ( (int)v14 < 0 )
{
a2(a3, "at ndn_sockcreate.c:%d", 227LL);
v15 = *(const char **)(a1 + 8);
if ( v15 )
goto LABEL_19;
LABEL_63:
a2(a3, "must specify remote address");
v46 = -1;
v22 = -1;
goto LABEL_41;
}
if ( (unsigned int)(v14 - 1) > 0xFE )
{
a2(a3, "mcast_ttl(%d) out of range", v14);
v46 = -1;
v22 = -1;
goto LABEL_41;
}
goto LABEL_17;
}
v13 = getaddrinfo(v12, *(const char **)(a1 + 16), &req, &pai);
if ( v13 )
{
LABEL_57:
v27 = gai_strerror(v13);
a2(a3, "getaddrinfo(\"%s\", ...): %s", *(const char **)(a1 + 24), v27);
v46 = -1;
v22 = -1;
goto LABEL_41;
}
}
else
{
v26 = *(const char **)(a1 + 24);
if ( !v26 )
goto LABEL_58;
v13 = getaddrinfo(v26, *(const char **)(a1 + 16), &req, &pai);
if ( v13 )
{
v46 = -1;
v22 = -1;
goto LABEL_41;
}
}
if ( !pai )
{
v46 = -1;
v22 = -1;
if ( !a2 )
goto LABEL_41;
goto LABEL_57;
}
req.ai_family = pai->ai_family;
if ( a2 )
goto LABEL_15;
LABEL_58:
v28 = *(_DWORD *)(a1 + 32);
if ( v28 < 0 )
{
LABEL_109:
v41 = *(const char **)(a1 + 8);
if ( !v41 )
{
v46 = -1;
v22 = -1;
goto LABEL_41;
}
v42 = strchr(v41, 37);
v17 = 0;
if ( !v42 )
goto LABEL_113;
v18 = v42 + 1;
v48 = _errno_location();
*v48 = 0;
v17 = atoi(v18);
if ( v17 )
{
LABEL_112:
if ( a2 )
goto LABEL_21;
LABEL_113:
if ( getaddrinfo(*(const char **)(a1 + 8), *(const char **)(a1 + 16), &req, &ai) )
{
v46 = -1;
v22 = -1;
goto LABEL_41;
}
v20 = ai;
if ( !ai )
{
v46 = -1;
v22 = -1;
goto LABEL_43;
}
goto LABEL_23;
}
LABEL_64:
v17 = if_nametoindex(v18);
if ( !v17 && *v48 )
{
if ( a2 )
a2(a3, "Invalid interface name %s", v18);
v46 = -1;
v22 = -1;
goto LABEL_41;
}
goto LABEL_112;
}
if ( (unsigned int)(v28 - 1) > 0xFE )
{
v46 = -1;
v22 = -1;
goto LABEL_41;
}
LABEL_17:
if ( !a2 )
goto LABEL_109;
a2(a3, "at ndn_sockcreate.c:%d", 227LL);
v15 = *(const char **)(a1 + 8);
if ( !v15 )
goto LABEL_63;
LABEL_19:
v16 = strchr(v15, 37);
a2(a3, "at ndn_sockcreate.c:%d", 234LL);
v17 = 0;
if ( v16 )
{
v18 = v16 + 1;
v48 = _errno_location();
*v48 = 0;
v17 = atoi(v18);
if ( !v17 )
goto LABEL_64;
}
LABEL_21:
a2(a3, "at ndn_sockcreate.c:%d", 248LL);
v19 = getaddrinfo(*(const char **)(a1 + 8), *(const char **)(a1 + 16), &req, &ai);
if ( v19 || (v20 = ai) == 0LL )
{
v38 = gai_strerror(v19);
a2(a3, "getaddrinfo(\"%s\", ...): %s", *(const char **)(a1 + 8), v38);
v46 = -1;
v22 = -1;
goto LABEL_41;
}
LABEL_23:
v21 = socket(v20->ai_family, v20->ai_socktype, v20->ai_protocol);
v22 = v21;
if ( a2 )
{
a2(a3, "at ndn_sockcreate.c:%d", 257LL);
if ( v22 == -1 )
{
v29 = _errno_location();
v30 = strerror(*v29);
a2(a3, "socket: %s", v30);
v46 = -1;
goto LABEL_41;
}
a2(a3, "at ndn_sockcreate.c:%d", 262LL);
a6[1] = v22;
*a6 = v22;
if ( pai )
goto LABEL_26;
req.ai_family = ai->ai_family;
req.ai_socktype = ai->ai_socktype;
req.ai_flags = 1;
a2(a3, "at ndn_sockcreate.c:%d", 269LL);
if ( getaddrinfo(0LL, *(const char **)(a1 + 16), &req, &v50) )
{
v46 = -1;
v22 = -1;
goto LABEL_41;
}
a2(a3, "at ndn_sockcreate.c:%d", 273LL);
}
else
{
if ( v21 == -1 )
{
v46 = v21;
goto LABEL_41;
}
a6[1] = v21;
*a6 = v21;
if ( pai )
goto LABEL_26;
req.ai_family = ai->ai_family;
req.ai_socktype = ai->ai_socktype;
req.ai_flags = 1;
if ( getaddrinfo(0LL, *(const char **)(a1 + 16), &req, &v50) )
{
v46 = -1;
v22 = -1;
goto LABEL_41;
}
}
if ( bind(a6[1], v50->ai_addr, v50->ai_addrlen) == -1 && a4 )
{
pai = v50;
v50 = 0LL;
if ( !pai )
{
v46 = -1;
goto LABEL_34;
}
a6[1] = -1;
LABEL_27:
if ( a2 )
{
a2(a3, "at ndn_sockcreate.c:%d", 291LL);
v23 = a4(a5, pai->ai_addr, pai->ai_addrlen);
a6[1] = v23;
v46 = -1;
if ( v23 < 0 )
goto LABEL_31;
a2(a3, "at ndn_sockcreate.c:%d", 296LL);
}
else
{
v40 = a4(a5, pai->ai_addr, pai->ai_addrlen);
a6[1] = v40;
if ( v40 < 0 )
{
v46 = -1;
LABEL_31:
if ( a6[1] != -1 )
goto LABEL_32;
goto LABEL_73;
}
}
v46 = a6[1];
goto LABEL_31;
}
if ( !pai )
{
v46 = -1;
goto LABEL_34;
}
LABEL_26:
a6[1] = -1;
if ( a4 )
goto LABEL_27;
v46 = -1;
LABEL_73:
v22 = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
a6[1] = v22;
if ( v22 == -1 )
{
if ( a2 )
{
v31 = _errno_location();
v32 = strerror(*v31);
a2(a3, "socket: %s", v32);
}
goto LABEL_41;
}
LABEL_32:
v22 = setsockopt(*a6, 1, 2, &optval, 4u);
if ( v22 == -1 )
{
if ( a2 )
{
v33 = _errno_location();
v34 = strerror(*v33);
a2(a3, "setsockopt(recving, ..., SO_REUSEADDR, ...): %s", v34);
}
goto LABEL_41;
}
v22 = bind(*a6, ai->ai_addr, ai->ai_addrlen);
if ( v22 == -1 )
{
if ( a2 )
{
v35 = _errno_location();
v36 = strerror(*v35);
a2(a3, "bind(recving, ...): %s", v36);
}
goto LABEL_41;
}
LABEL_34:
if ( a2 )
a2(a3, "at ndn_sockcreate.c:%d", 318LL);
if ( (int)set_multicast_socket_options(*a6, a6[1], (__int64)ai, (__int64)pai, *(_DWORD *)(a1 + 32), v17, a2, a3) < 0 )
{
v22 = -1;
}
else if ( pai )
{
if ( a2 )
{
a2(a3, "at ndn_sockcreate.c:%d", 329LL);
v24 = a6[1];
if ( v46 != v24 && bind(v24, pai->ai_addr, pai->ai_addrlen) == -1 )
{
LABEL_117:
v22 = -1;
if ( a2 )
{
v43 = _errno_location();
v44 = strerror(*v43);
a2(a3, "bind(sending, ...): %s", v44);
}
goto LABEL_41;
}
goto LABEL_40;
}
v39 = a6[1];
if ( v46 == v39 )
{
v46 = a6[1];
v22 = 0;
}
else
{
if ( bind(v39, pai->ai_addr, pai->ai_addrlen) == -1 )
goto LABEL_117;
v22 = 0;
}
}
else
{
v22 = 0;
if ( a2 )
{
LABEL_40:
a2(a3, "at ndn_sockcreate.c:%d", 340LL);
v22 = 0;
goto LABEL_41;
}
}
LABEL_41:
if ( ai )
freeaddrinfo(ai);
LABEL_43:
if ( v50 )
freeaddrinfo(v50);
if ( pai )
freeaddrinfo(pai);
if ( v22 )
{
close(*a6);
v37 = a6[1];
if ( *a6 != v37 && v37 != v46 )
close(v37);
*a6 = -1;
a6[1] = -1;
}
return v22;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, void (*a2)(__int64, const char *, ...), __int64 a3, __int64 (__fastcall *a4)(__int64, sockaddr *, _QWORD), __int64 a5, int *a6)
{
int v9; // eax
const char *v10; // r12
size_t v11; // r15
const char *v12; // rdi
int v13; // eax
__int64 v14; // rdx
const char *v15; // rdi
char *v16; // r12
int v17; // er15
const char *v18; // r12
int v19; // edi
struct addrinfo *v20; // rax
int v21; // eax
unsigned int v22; // er12
int v23; // eax
int v24; // edi
const char *v26; // rdi
const char *v27; // rax
int v28; // eax
int *v29; // rax
char *v30; // rax
int *v31; // rax
char *v32; // rax
int *v33; // rax
char *v34; // rax
int *v35; // rax
char *v36; // rax
int v37; // edi
const char *v38; // rax
int v39; // edi
int v40; // eax
const char *v41; // rdi
char *v42; // r12
int *v43; // rax
char *v44; // rax
int v46; // [rsp+8h] [rbp-A0h]
int *v48; // [rsp+18h] [rbp-90h]
int optval; // [rsp+24h] [rbp-84h] BYREF
struct addrinfo *v50; // [rsp+28h] [rbp-80h] BYREF
struct addrinfo *ai; // [rsp+30h] [rbp-78h] BYREF
struct addrinfo *pai; // [rsp+38h] [rbp-70h] BYREF
struct addrinfo req; // [rsp+40h] [rbp-68h] BYREF

*(_QWORD *)&req.ai_flags = 0LL;
*(_QWORD *)&req.ai_socktype = 0LL;
*(_QWORD *)&req.ai_addrlen = 0LL;
req.ai_addr = 0LL;
req.ai_canonname = 0LL;
req.ai_next = 0LL;
pai = 0LL;
ai = 0LL;
v50 = 0LL;
optval = 1;
if ( a2 )
a2(a3, "at ndn_sockcreate.c:%d", 194LL);
*a6 = -1;
a6[1] = -1;
v9 = *(_DWORD *)a1;
if ( *(int *)a1 > 0 )
req.ai_protocol = *(_DWORD *)a1;
if ( v9 == 17 )
{
req.ai_socktype = 2;
}
else if ( v9 == 6 )
{
req.ai_socktype = 1;
}
req.ai_flags = 4;
v10 = *(const char **)(a1 + 16);
if ( !v10 || (v11 = strspn(*(const char **)(a1 + 16), "0123456789"), v11 != strlen(v10)) )
{
if ( a2 )
a2(a3, "must specify numeric port");
v46 = -1;
v22 = -1;
goto LABEL_41;
}
if ( a2 )
{
a2(a3, "at ndn_sockcreate.c:%d", 208LL);
v12 = *(const char **)(a1 + 24);
if ( !v12 )
{
LABEL_15:
a2(a3, "at ndn_sockcreate.c:%d", 219LL);
v14 = *(unsigned int *)(a1 + 32);
if ( (int)v14 < 0 )
{
a2(a3, "at ndn_sockcreate.c:%d", 227LL);
v15 = *(const char **)(a1 + 8);
if ( v15 )
goto LABEL_19;
LABEL_63:
a2(a3, "must specify remote address");
v46 = -1;
v22 = -1;
goto LABEL_41;
}
if ( (unsigned int)(v14 - 1) > 0xFE )
{
a2(a3, "mcast_ttl(%d) out of range", v14);
v46 = -1;
v22 = -1;
goto LABEL_41;
}
goto LABEL_17;
}
v13 = getaddrinfo(v12, *(const char **)(a1 + 16), &req, &pai);
if ( v13 )
{
LABEL_57:
v27 = gai_strerror(v13);
a2(a3, "getaddrinfo(\"%s\", ...): %s", *(const char **)(a1 + 24), v27);
v46 = -1;
v22 = -1;
goto LABEL_41;
}
}
else
{
v26 = *(const char **)(a1 + 24);
if ( !v26 )
goto LABEL_58;
v13 = getaddrinfo(v26, *(const char **)(a1 + 16), &req, &pai);
if ( v13 )
{
v46 = -1;
v22 = -1;
goto LABEL_41;
}
}
if ( !pai )
{
v46 = -1;
v22 = -1;
if ( !a2 )
goto LABEL_41;
goto LABEL_57;
}
req.ai_family = pai->ai_family;
if ( a2 )
goto LABEL_15;
LABEL_58:
v28 = *(_DWORD *)(a1 + 32);
if ( v28 < 0 )
{
LABEL_109:
v41 = *(const char **)(a1 + 8);
if ( !v41 )
{
v46 = -1;
v22 = -1;
goto LABEL_41;
}
v42 = strchr(v41, 37);
v17 = 0;
if ( !v42 )
goto LABEL_113;
v18 = v42 + 1;
v48 = _errno_location();
*v48 = 0;
v17 = atoi(v18);
if ( v17 )
{
LABEL_112:
if ( a2 )
goto LABEL_21;
LABEL_113:
if ( getaddrinfo(*(const char **)(a1 + 8), *(const char **)(a1 + 16), &req, &ai) )
{
v46 = -1;
v22 = -1;
goto LABEL_41;
}
v20 = ai;
if ( !ai )
{
v46 = -1;
v22 = -1;
goto LABEL_43;
}
goto LABEL_23;
}
LABEL_64:
v17 = if_nametoindex(v18);
if ( !v17 && *v48 )
{
if ( a2 )
a2(a3, "Invalid interface name %s", v18);
v46 = -1;
v22 = -1;
goto LABEL_41;
}
goto LABEL_112;
}
if ( (unsigned int)(v28 - 1) > 0xFE )
{
v46 = -1;
v22 = -1;
goto LABEL_41;
}
LABEL_17:
if ( !a2 )
goto LABEL_109;
a2(a3, "at ndn_sockcreate.c:%d", 227LL);
v15 = *(const char **)(a1 + 8);
if ( !v15 )
goto LABEL_63;
LABEL_19:
v16 = strchr(v15, 37);
a2(a3, "at ndn_sockcreate.c:%d", 234LL);
v17 = 0;
if ( v16 )
{
v18 = v16 + 1;
v48 = _errno_location();
*v48 = 0;
v17 = atoi(v18);
if ( !v17 )
goto LABEL_64;
}
LABEL_21:
a2(a3, "at ndn_sockcreate.c:%d", 248LL);
v19 = getaddrinfo(*(const char **)(a1 + 8), *(const char **)(a1 + 16), &req, &ai);
if ( v19 || (v20 = ai) == 0LL )
{
v38 = gai_strerror(v19);
a2(a3, "getaddrinfo(\"%s\", ...): %s", *(const char **)(a1 + 8), v38);
v46 = -1;
v22 = -1;
goto LABEL_41;
}
LABEL_23:
v21 = socket(v20->ai_family, v20->ai_socktype, v20->ai_protocol);
v22 = v21;
if ( a2 )
{
a2(a3, "at ndn_sockcreate.c:%d", 257LL);
if ( v22 == -1 )
{
v29 = _errno_location();
v30 = strerror(*v29);
a2(a3, "socket: %s", v30);
v46 = -1;
goto LABEL_41;
}
a2(a3, "at ndn_sockcreate.c:%d", 262LL);
a6[1] = v22;
*a6 = v22;
if ( pai )
goto LABEL_26;
req.ai_family = ai->ai_family;
req.ai_socktype = ai->ai_socktype;
req.ai_flags = 1;
a2(a3, "at ndn_sockcreate.c:%d", 269LL);
if ( getaddrinfo(0LL, *(const char **)(a1 + 16), &req, &v50) )
{
v46 = -1;
v22 = -1;
goto LABEL_41;
}
a2(a3, "at ndn_sockcreate.c:%d", 273LL);
}
else
{
if ( v21 == -1 )
{
v46 = v21;
goto LABEL_41;
}
a6[1] = v21;
*a6 = v21;
if ( pai )
goto LABEL_26;
req.ai_family = ai->ai_family;
req.ai_socktype = ai->ai_socktype;
req.ai_flags = 1;
if ( getaddrinfo(0LL, *(const char **)(a1 + 16), &req, &v50) )
{
v46 = -1;
v22 = -1;
goto LABEL_41;
}
}
if ( bind(a6[1], v50->ai_addr, v50->ai_addrlen) == -1 && a4 )
{
pai = v50;
v50 = 0LL;
if ( !pai )
{
v46 = -1;
goto LABEL_34;
}
a6[1] = -1;
LABEL_27:
if ( a2 )
{
a2(a3, "at ndn_sockcreate.c:%d", 291LL);
v23 = a4(a5, pai->ai_addr, pai->ai_addrlen);
a6[1] = v23;
v46 = -1;
if ( v23 < 0 )
goto LABEL_31;
a2(a3, "at ndn_sockcreate.c:%d", 296LL);
}
else
{
v40 = a4(a5, pai->ai_addr, pai->ai_addrlen);
a6[1] = v40;
if ( v40 < 0 )
{
v46 = -1;
LABEL_31:
if ( a6[1] != -1 )
goto LABEL_32;
goto LABEL_73;
}
}
v46 = a6[1];
goto LABEL_31;
}
if ( !pai )
{
v46 = -1;
goto LABEL_34;
}
LABEL_26:
a6[1] = -1;
if ( a4 )
goto LABEL_27;
v46 = -1;
LABEL_73:
v22 = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
a6[1] = v22;
if ( v22 == -1 )
{
if ( a2 )
{
v31 = _errno_location();
v32 = strerror(*v31);
a2(a3, "socket: %s", v32);
}
goto LABEL_41;
}
LABEL_32:
v22 = setsockopt(*a6, 1, 2, &optval, 4u);
if ( v22 == -1 )
{
if ( a2 )
{
v33 = _errno_location();
v34 = strerror(*v33);
a2(a3, "setsockopt(recving, ..., SO_REUSEADDR, ...): %s", v34);
}
goto LABEL_41;
}
v22 = bind(*a6, ai->ai_addr, ai->ai_addrlen);
if ( v22 == -1 )
{
if ( a2 )
{
v35 = _errno_location();
v36 = strerror(*v35);
a2(a3, "bind(recving, ...): %s", v36);
}
goto LABEL_41;
}
LABEL_34:
if ( a2 )
a2(a3, "at ndn_sockcreate.c:%d", 318LL);
if ( (int)func1(*a6, a6[1], (__int64)ai, (__int64)pai, *(_DWORD *)(a1 + 32), v17, a2, a3) < 0 )
{
v22 = -1;
}
else if ( pai )
{
if ( a2 )
{
a2(a3, "at ndn_sockcreate.c:%d", 329LL);
v24 = a6[1];
if ( v46 != v24 && bind(v24, pai->ai_addr, pai->ai_addrlen) == -1 )
{
LABEL_117:
v22 = -1;
if ( a2 )
{
v43 = _errno_location();
v44 = strerror(*v43);
a2(a3, "bind(sending, ...): %s", v44);
}
goto LABEL_41;
}
goto LABEL_40;
}
v39 = a6[1];
if ( v46 == v39 )
{
v46 = a6[1];
v22 = 0;
}
else
{
if ( bind(v39, pai->ai_addr, pai->ai_addrlen) == -1 )
goto LABEL_117;
v22 = 0;
}
}
else
{
v22 = 0;
if ( a2 )
{
LABEL_40:
a2(a3, "at ndn_sockcreate.c:%d", 340LL);
v22 = 0;
goto LABEL_41;
}
}
LABEL_41:
if ( ai )
freeaddrinfo(ai);
LABEL_43:
if ( v50 )
freeaddrinfo(v50);
if ( pai )
freeaddrinfo(pai);
if ( v22 )
{
close(*a6);
v37 = a6[1];
if ( *a6 != v37 && v37 != v46 )
close(v37);
*a6 = -1;
a6[1] = -1;
}
return v22;
}

original_code:
int
ndn_setup_socket(const struct ndn_sockdescr *descr,
                 void (*logger)(void *, const char *, ...),
                 void *logdat,
                 int (*getbound)(void *, struct sockaddr *, socklen_t),
                 void *getbounddat,
                 struct ndn_sockets *socks)
{
    int result = -1;
    char *cp = ((void *)0);
    struct addrinfo hints = {0};
    int res;
    struct addrinfo *mcast_source_addrinfo = ((void *)0);
    struct addrinfo *addrinfo = ((void *)0);
    struct addrinfo *laddrinfo = ((void *)0);
    unsigned int if_index = 0;
    const int one = 1;
    int sock = -1;
    int close_protect = -1;
    if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 194);;
    socks->sending = socks->recving = -1;
    if (descr->ipproto > 0)
        hints.ai_protocol = descr->ipproto;
    if (descr->ipproto == IPPROTO_UDP)
        hints.ai_socktype = SOCK_DGRAM;
    else if (descr->ipproto == IPPROTO_TCP)
        hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0x0004;
    if (descr->port == ((void *)0) ||
        strspn(descr->port, "0123456789") != strlen(descr->port)) {
        if (logger) (*logger)(logdat, "must specify numeric port");
        goto Finish;
    }
    if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 208);;
    if (descr->source_address != ((void *)0)) {
 res = getaddrinfo(descr->source_address, descr->port,
                          &hints, &mcast_source_addrinfo);
 if (res != 0 || mcast_source_addrinfo == ((void *)0)) {
     if (logger) (*logger)(logdat, "getaddrinfo(\"%s\", ...): %s",
                   descr->source_address, gai_strerror(res));
            goto Finish;
 }
        hints.ai_family = mcast_source_addrinfo->ai_family;
    }
    if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 219);;
    if (descr->mcast_ttl >= 0) {
        if (descr->mcast_ttl < 1 || descr->mcast_ttl > 255) {
            if (logger) (*logger)(logdat, "mcast_ttl(%d) out of range", descr->mcast_ttl);
            goto Finish;
        }
    }
    if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 227);;
    if (descr->address == ((void *)0)) {
        if (logger) (*logger)(logdat, "must specify remote address");
        goto Finish;
    }
    cp = strchr(descr->address, '%');
    if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 234);;
    if (cp != ((void *)0)) {
        cp++;
        (*__errno_location ()) = 0;
        if_index = atoi(cp);
        if (if_index == 0) {
            if_index = if_nametoindex(cp);
            if (if_index == 0 && (*__errno_location ()) != 0) {
                if (logger) (*logger)(logdat, "Invalid interface name %s", cp);
                goto Finish;
            }
        }
    }
    if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 248);;
    res = getaddrinfo(descr->address, descr->port,
                      &hints, &addrinfo);
    if (res != 0 || addrinfo == ((void *)0)) {
        if (logger) (*logger)(logdat, "getaddrinfo(\"%s\", ...): %s",
               descr->address, gai_strerror(res));
        goto Finish;
    }
    sock = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol);
    if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 257);;
    if (sock == -1) {
        if (logger) (*logger)(logdat, "socket: %s", strerror((*__errno_location ())));
        goto Finish;
    }
    if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 262);;
    socks->recving = socks->sending = sock;
    if (mcast_source_addrinfo == ((void *)0)) {
        hints.ai_family = addrinfo->ai_family;
        hints.ai_socktype = addrinfo->ai_socktype;
        hints.ai_flags = 0x0001;
        if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 269);;
        res = getaddrinfo(((void *)0), descr->port, &hints, &laddrinfo);
        if (res != 0)
            goto Finish;
        if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 273);;
        res = bind(socks->sending, laddrinfo->ai_addr, laddrinfo->ai_addrlen);
        if (res == -1 && getbound) {
            mcast_source_addrinfo = laddrinfo;
            laddrinfo = ((void *)0);
        }
    }
    if (mcast_source_addrinfo != ((void *)0)) {
        socks->sending = -1;
        if (getbound) {
            if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 291);;
            socks->sending = getbound(getbounddat,
                                      mcast_source_addrinfo->ai_addr,
                                      mcast_source_addrinfo->ai_addrlen);
            if (socks->sending >= 0) {
                if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 296);;
                close_protect = socks->sending;
            }
        }
        if (socks->sending == -1)
            socks->sending = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol);
        if (socks->sending == -1) {
            if (logger) (*logger)(logdat, "socket: %s", strerror((*__errno_location ())));
            goto Finish;
        }
        res = setsockopt(socks->recving, 1, 2, &one, sizeof(one));
        if (res == -1) {
            if (logger) (*logger)(logdat, "setsockopt(recving, ..., SO_REUSEADDR, ...): %s", strerror((*__errno_location ())));
            goto Finish;
        }
 res = bind(socks->recving, addrinfo->ai_addr, addrinfo->ai_addrlen);
        if (res == -1) {
            if (logger) (*logger)(logdat, "bind(recving, ...): %s", strerror((*__errno_location ())));
            goto Finish;
        }
    }
    if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 318);;
    res = set_multicast_socket_options(socks->recving, socks->sending,
                                       addrinfo,
                                       mcast_source_addrinfo,
                                       descr->mcast_ttl,
                                       if_index,
                                       logger,
                                       logdat);
    if (res < 0)
        goto Finish;
    if (mcast_source_addrinfo != ((void *)0)) {
        if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 329);;
        if (socks->sending != close_protect) {
            res = bind(socks->sending,
                       mcast_source_addrinfo->ai_addr,
                       mcast_source_addrinfo->ai_addrlen);
            if (res == -1) {
                if (logger) (*logger)(logdat, "bind(sending, ...): %s", strerror((*__errno_location ())));
                goto Finish;
            }
        }
    }
    if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 340);;
    result = 0;
Finish:
    if (addrinfo != ((void *)0))
        freeaddrinfo(addrinfo);
    if (laddrinfo != ((void *)0))
        freeaddrinfo(laddrinfo);
    if (mcast_source_addrinfo != ((void *)0))
        freeaddrinfo(mcast_source_addrinfo);
    if (result != 0) {
        close(socks->recving);
        if (socks->sending != socks->recving && socks->sending != close_protect)
            close(socks->sending);
        socks->sending = socks->recving = -1;
    }
    return(result);
}

canonical_original_code:
int ndn_setup_socket(struct ndn_sockdescr *descr, void (*logger)(void *, char *, ...), void *logdat, int (*getbound)(void *, struct sockaddr *, unsigned int), void *getbounddat, struct ndn_sockets *socks)
{
    int result = -1;
    char *cp = ((void *)0);
    struct addrinfo hints = {0};
    int res;
    struct addrinfo *mcast_source_addrinfo = ((void *)0);
    struct addrinfo *addrinfo = ((void *)0);
    struct addrinfo *laddrinfo = ((void *)0);
    unsigned int if_index = 0;
    int one = 1;
    int sock = -1;
    int close_protect = -1;
    if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 194);;
    socks->sending = socks->recving = -1;
    if (descr->ipproto > 0)
        hints.ai_protocol = descr->ipproto;
    if (descr->ipproto == IPPROTO_UDP)
        hints.ai_socktype = SOCK_DGRAM;
    else if (descr->ipproto == IPPROTO_TCP)
        hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0x0004;
    if (descr->port == ((void *)0) ||
        strspn(descr->port, "0123456789") != strlen(descr->port)) {
        if (logger) (*logger)(logdat, "must specify numeric port");
        goto Finish;
    }
    if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 208);;
    if (descr->source_address != ((void *)0)) {
 res = getaddrinfo(descr->source_address, descr->port,
                          &hints, &mcast_source_addrinfo);
 if (res != 0 || mcast_source_addrinfo == ((void *)0)) {
     if (logger) (*logger)(logdat, "getaddrinfo(\"%s\", ...): %s",
                   descr->source_address, gai_strerror(res));
            goto Finish;
 }
        hints.ai_family = mcast_source_addrinfo->ai_family;
    }
    if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 219);;
    if (descr->mcast_ttl >= 0) {
        if (descr->mcast_ttl < 1 || descr->mcast_ttl > 255) {
            if (logger) (*logger)(logdat, "mcast_ttl(%d) out of range", descr->mcast_ttl);
            goto Finish;
        }
    }
    if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 227);;
    if (descr->address == ((void *)0)) {
        if (logger) (*logger)(logdat, "must specify remote address");
        goto Finish;
    }
    cp = strchr(descr->address, '%');
    if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 234);;
    if (cp != ((void *)0)) {
        cp++;
        (*__errno_location ()) = 0;
        if_index = atoi(cp);
        if (if_index == 0) {
            if_index = if_nametoindex(cp);
            if (if_index == 0 && (*__errno_location ()) != 0) {
                if (logger) (*logger)(logdat, "Invalid interface name %s", cp);
                goto Finish;
            }
        }
    }
    if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 248);;
    res = getaddrinfo(descr->address, descr->port,
                      &hints, &addrinfo);
    if (res != 0 || addrinfo == ((void *)0)) {
        if (logger) (*logger)(logdat, "getaddrinfo(\"%s\", ...): %s",
               descr->address, gai_strerror(res));
        goto Finish;
    }
    sock = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol);
    if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 257);;
    if (sock == -1) {
        if (logger) (*logger)(logdat, "socket: %s", strerror((*__errno_location ())));
        goto Finish;
    }
    if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 262);;
    socks->recving = socks->sending = sock;
    if (mcast_source_addrinfo == ((void *)0)) {
        hints.ai_family = addrinfo->ai_family;
        hints.ai_socktype = addrinfo->ai_socktype;
        hints.ai_flags = 0x0001;
        if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 269);;
        res = getaddrinfo(((void *)0), descr->port, &hints, &laddrinfo);
        if (res != 0)
            goto Finish;
        if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 273);;
        res = bind(socks->sending, laddrinfo->ai_addr, laddrinfo->ai_addrlen);
        if (res == -1 && getbound) {
            mcast_source_addrinfo = laddrinfo;
            laddrinfo = ((void *)0);
        }
    }
    if (mcast_source_addrinfo != ((void *)0)) {
        socks->sending = -1;
        if (getbound) {
            if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 291);;
            socks->sending = getbound(getbounddat,
                                      mcast_source_addrinfo->ai_addr,
                                      mcast_source_addrinfo->ai_addrlen);
            if (socks->sending >= 0) {
                if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 296);;
                close_protect = socks->sending;
            }
        }
        if (socks->sending == -1)
            socks->sending = socket(addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol);
        if (socks->sending == -1) {
            if (logger) (*logger)(logdat, "socket: %s", strerror((*__errno_location ())));
            goto Finish;
        }
        res = setsockopt(socks->recving, 1, 2, &one, sizeof(one));
        if (res == -1) {
            if (logger) (*logger)(logdat, "setsockopt(recving, ..., SO_REUSEADDR, ...): %s", strerror((*__errno_location ())));
            goto Finish;
        }
 res = bind(socks->recving, addrinfo->ai_addr, addrinfo->ai_addrlen);
        if (res == -1) {
            if (logger) (*logger)(logdat, "bind(recving, ...): %s", strerror((*__errno_location ())));
            goto Finish;
        }
    }
    if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 318);;
    res = set_multicast_socket_options(socks->recving, socks->sending,
                                       addrinfo,
                                       mcast_source_addrinfo,
                                       descr->mcast_ttl,
                                       if_index,
                                       logger,
                                       logdat);
    if (res < 0)
        goto Finish;
    if (mcast_source_addrinfo != ((void *)0)) {
        if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 329);;
        if (socks->sending != close_protect) {
            res = bind(socks->sending,
                       mcast_source_addrinfo->ai_addr,
                       mcast_source_addrinfo->ai_addrlen);
            if (res == -1) {
                if (logger) (*logger)(logdat, "bind(sending, ...): %s", strerror((*__errno_location ())));
                goto Finish;
            }
        }
    }
    if (logger) (*logger)(logdat, "at ndn_sockcreate.c:%d", 340);;
    result = 0;
Finish:
    if (addrinfo != ((void *)0))
        freeaddrinfo(addrinfo);
    if (laddrinfo != ((void *)0))
        freeaddrinfo(laddrinfo);
    if (mcast_source_addrinfo != ((void *)0))
        freeaddrinfo(mcast_source_addrinfo);
    if (result != 0) {
        close(socks->recving);
        if (socks->sending != socks->recving && socks->sending != close_protect)
            close(socks->sending);
        socks->sending = socks->recving = -1;
    }
    return(result);
}

variable_types: struct ndn_sockdescr { int ipproto; char *address; char *port; char *source_address; int mcast_ttl; } *

variable_types: int (*)(void *, struct sockaddr { unsigned short sa_family; char sa_data[14]; } *, unsigned int)

variable_types:
getbounddat
void *

variable_types: struct ndn_sockets { int recving; int sending; } *

variable_types: result int

variable_types:
cp
char *

variable_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr { unsigned short sa_family; char sa_data[14]; } *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }

variable_types: one int

variable_types: sock int


repo: danihegglin/ChronosAndroid

decompiled_code:
void __fastcall ndn_face_instance_destroy(void **a1)
{
char *v2; // rdi

v2 = (char *)*a1;
if ( v2 )
{
ndn_charbuf_destroy(v2 + 80);
free(*a1);
*a1 = 0LL;
}
}

canonical_decompiled_code:
void __fastcall func0(void **a1)
{
char *v2; // rdi

v2 = (char *)*a1;
if ( v2 )
{
ndn_charbuf_destroy(v2 + 80);
free(*a1);
*a1 = 0LL;
}
}

original_code:
void
ndn_face_instance_destroy(struct ndn_face_instance **pfi)
{
    if (*pfi == ((void *)0))
        return;
    ndn_charbuf_destroy(&(*pfi)->store);
    free(*pfi);
    *pfi = ((void *)0);
}

variable_types: struct ndn_face_instance { char *action; unsigned char *ndnd_id; unsigned long ndnd_id_size; unsigned int faceid; struct ndn_sockdescr { int ipproto; char *address; char *port; char *source_address; int mcast_ttl; } descr; int lifetime; struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } *store; } **

return_type: void

user_defined_types: struct ndn_face_instance { char *action; unsigned char *ndnd_id; unsigned long ndnd_id_size; unsigned int faceid; struct ndn_sockdescr descr; int lifetime; struct ndn_charbuf *store; }


decompiled_code:
__int64 __fastcall ndnb_append_face_instance(__int64 a1, __int64 a2)
{
int v2; // ebx
int v3; // ecx

v2 = ndnb_element_begin(a1, 80LL);
if ( *(_QWORD *)a2 )
v2 |= ndnb_tagged_putf(a1, 73LL, &LC0);
if ( *(_QWORD *)(a2 + 16) )
v2 |= ndnb_append_tagged_blob(a1, 60LL, *(_QWORD *)(a2 + 8));
if ( *(_DWORD *)(a2 + 24) != -1 )
v2 |= ndnb_tagged_putf(a1, 74LL, &LC1);
if ( *(int *)(a2 + 32) >= 0 )
v2 |= ndnb_tagged_putf(a1, 75LL, &LC2);
if ( *(_QWORD *)(a2 + 40) )
v2 |= ndnb_tagged_putf(a1, 76LL, &LC0);
if ( *(_QWORD *)(a2 + 48) )
v2 |= ndnb_tagged_putf(a1, 77LL, &LC0);
if ( *(_QWORD *)(a2 + 56) )
v2 |= ndnb_tagged_putf(a1, 78LL, &LC0);
v3 = *(_DWORD *)(a2 + 64);
if ( v3 >= 0 && v3 != 1 )
v2 |= ndnb_tagged_putf(a1, 82LL, &LC2);
if ( *(int *)(a2 + 72) >= 0 )
v2 |= ndnb_tagged_putf(a1, 58LL, &LC2);
return v2 | (unsigned int)ndnb_element_end(a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1, __int64 a2)
{
int v2; // ebx
int v3; // ecx

v2 = ndnb_element_begin(a1, 80LL);
if ( *(_QWORD *)a2 )
v2 |= ndnb_tagged_putf(a1, 73LL, &LC0);
if ( *(_QWORD *)(a2 + 16) )
v2 |= ndnb_append_tagged_blob(a1, 60LL, *(_QWORD *)(a2 + 8));
if ( *(_DWORD *)(a2 + 24) != -1 )
v2 |= ndnb_tagged_putf(a1, 74LL, &LC1);
if ( *(int *)(a2 + 32) >= 0 )
v2 |= ndnb_tagged_putf(a1, 75LL, &LC2);
if ( *(_QWORD *)(a2 + 40) )
v2 |= ndnb_tagged_putf(a1, 76LL, &LC0);
if ( *(_QWORD *)(a2 + 48) )
v2 |= ndnb_tagged_putf(a1, 77LL, &LC0);
if ( *(_QWORD *)(a2 + 56) )
v2 |= ndnb_tagged_putf(a1, 78LL, &LC0);
v3 = *(_DWORD *)(a2 + 64);
if ( v3 >= 0 && v3 != 1 )
v2 |= ndnb_tagged_putf(a1, 82LL, &LC2);
if ( *(int *)(a2 + 72) >= 0 )
v2 |= ndnb_tagged_putf(a1, 58LL, &LC2);
return v2 | (unsigned int)ndnb_element_end(a1);
}

original_code:
int
ndnb_append_face_instance(struct ndn_charbuf *c,
                          const struct ndn_face_instance *fi)
{
    int res;
    res = ndnb_element_begin(c, NDN_DTAG_FaceInstance);
    if (fi->action != ((void *)0))
        res |= ndnb_tagged_putf(c, NDN_DTAG_Action, "%s",
                                fi->action);
    if (fi->ndnd_id_size != 0)
        res |= ndnb_append_tagged_blob(c, NDN_DTAG_PublisherPublicKeyDigest,
                                          fi->ndnd_id, fi->ndnd_id_size);
    if (fi->faceid != ~0)
        res |= ndnb_tagged_putf(c, NDN_DTAG_FaceID, "%u",
                                   fi->faceid);
    if (fi->descr.ipproto >= 0)
        res |= ndnb_tagged_putf(c, NDN_DTAG_IPProto, "%d",
                                   fi->descr.ipproto);
    if (fi->descr.address != ((void *)0))
        res |= ndnb_tagged_putf(c, NDN_DTAG_Host, "%s",
                                   fi->descr.address);
    if (fi->descr.port != ((void *)0))
        res |= ndnb_tagged_putf(c, NDN_DTAG_Port, "%s",
                                   fi->descr.port);
    if (fi->descr.source_address != ((void *)0))
        res |= ndnb_tagged_putf(c, NDN_DTAG_MulticastInterface, "%s",
                                   fi->descr.source_address);
    if (fi->descr.mcast_ttl >= 0 && fi->descr.mcast_ttl != 1)
        res |= ndnb_tagged_putf(c, NDN_DTAG_MulticastTTL, "%d",
                                   fi->descr.mcast_ttl);
    if (fi->lifetime >= 0)
        res |= ndnb_tagged_putf(c, NDN_DTAG_FreshnessSeconds, "%d",
                                   fi->lifetime);
    res |= ndnb_element_end(c);
    return(res);
}

canonical_original_code:
int ndnb_append_face_instance(struct ndn_charbuf *c, struct ndn_face_instance *fi)
{
    int res;
    res = ndnb_element_begin(c, NDN_DTAG_FaceInstance);
    if (fi->action != ((void *)0))
        res |= ndnb_tagged_putf(c, NDN_DTAG_Action, "%s",
                                fi->action);
    if (fi->ndnd_id_size != 0)
        res |= ndnb_append_tagged_blob(c, NDN_DTAG_PublisherPublicKeyDigest,
                                          fi->ndnd_id, fi->ndnd_id_size);
    if (fi->faceid != ~0)
        res |= ndnb_tagged_putf(c, NDN_DTAG_FaceID, "%u",
                                   fi->faceid);
    if (fi->descr.ipproto >= 0)
        res |= ndnb_tagged_putf(c, NDN_DTAG_IPProto, "%d",
                                   fi->descr.ipproto);
    if (fi->descr.address != ((void *)0))
        res |= ndnb_tagged_putf(c, NDN_DTAG_Host, "%s",
                                   fi->descr.address);
    if (fi->descr.port != ((void *)0))
        res |= ndnb_tagged_putf(c, NDN_DTAG_Port, "%s",
                                   fi->descr.port);
    if (fi->descr.source_address != ((void *)0))
        res |= ndnb_tagged_putf(c, NDN_DTAG_MulticastInterface, "%s",
                                   fi->descr.source_address);
    if (fi->descr.mcast_ttl >= 0 && fi->descr.mcast_ttl != 1)
        res |= ndnb_tagged_putf(c, NDN_DTAG_MulticastTTL, "%d",
                                   fi->descr.mcast_ttl);
    if (fi->lifetime >= 0)
        res |= ndnb_tagged_putf(c, NDN_DTAG_FreshnessSeconds, "%d",
                                   fi->lifetime);
    res |= ndnb_element_end(c);
    return(res);
}

variable_types: struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } *

variable_types: struct ndn_face_instance { char *action; unsigned char *ndnd_id; unsigned long ndnd_id_size; unsigned int faceid; struct ndn_sockdescr { int ipproto; char *address; char *port; char *source_address; int mcast_ttl; } descr; int lifetime; struct ndn_charbuf *store; } *

variable_types: res int

return_type: int


decompiled_code:
__int64 __fastcall ndn_charbuf_duplicate(_QWORD *a1)
{
__int64 v1; // rbx

v1 = ndn_charbuf_create();
ndn_charbuf_append(v1, a1[2], *a1);
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1)
{
__int64 v1; // rbx

v1 = ndn_charbuf_create();
ndn_charbuf_append(v1, a1[2], *a1);
return v1;
}

original_code:
static struct ndn_charbuf *
ndn_charbuf_duplicate(struct ndn_charbuf *c)
{
    struct ndn_charbuf *ans = ndn_charbuf_create();
    ndn_charbuf_append(ans, c->buf, c->length);
    return(ans);
}

canonical_original_code:
static struct ndn_charbuf *ndn_charbuf_duplicate(struct ndn_charbuf *c)
{
    struct ndn_charbuf *ans = ndn_charbuf_create();
    ndn_charbuf_append(ans, c->buf, c->length);
    return(ans);
}

variable_types:
c
struct ndn_charbuf *

return_type: struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } *


decompiled_code:
__int64 __fastcall append_Any_filter(__int64 a1)
{
ndn_charbuf_append_tt(a1, 13LL, 2LL);
return ndn_charbuf_append_closer(a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1)
{
ndn_charbuf_append_tt(a1, 13LL, 2LL);
return ndn_charbuf_append_closer(a1);
}

original_code:
static void
append_Any_filter(struct ndn_charbuf *c)
{
    ndn_charbuf_append_tt(c, NDN_DTAG_Any, NDN_DTAG);
    ndn_charbuf_append_closer(c);
}

user_defined_types: struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; }


decompiled_code:
__int64 __fastcall local_scope(__int64 a1)
{
ndn_charbuf_append_tt(a1, 42LL, 2LL);
ndn_charbuf_append_tt(a1, 1LL, 6LL);
ndn_charbuf_append(a1, &LC0, 1LL);
return ndn_charbuf_append_closer(a1);
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1)
{
ndn_charbuf_append_tt(a1, 42LL, 2LL);
ndn_charbuf_append_tt(a1, 1LL, 6LL);
ndn_charbuf_append(a1, &LC0, 1LL);
return ndn_charbuf_append_closer(a1);
}

original_code:
static void
local_scope(struct ndn_charbuf *templ)
{
    ndn_charbuf_append_tt(templ, NDN_DTAG_Scope, NDN_DTAG);
    ndn_charbuf_append_tt(templ, 1, NDN_UDATA);
    ndn_charbuf_append(templ, "0", 1);
    ndn_charbuf_append_closer(templ);
}


decompiled_code:
__int64 __fastcall bts_close(__int64 a1, __int64 a2)
{
int *v2; // r12
unsigned int v3; // ebp

v2 = *(int **)(a2 + 16);
if ( !v2 )
return (unsigned int)-1;
if ( *(_QWORD *)v2 != a2 )
return (unsigned int)-1;
v3 = close(v2[2]);
if ( v3 != -1 || *_errno_location() != 4 )
{
--*(_DWORD *)(a1 + 60);
*(_QWORD *)(a2 + 16) = 0LL;
free(v2);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
int *v2; // r12
unsigned int v3; // ebp

v2 = *(int **)(a2 + 16);
if ( !v2 )
return (unsigned int)-1;
if ( *(_QWORD *)v2 != a2 )
return (unsigned int)-1;
v3 = close(v2[2]);
if ( v3 != -1 || *_errno_location() != 4 )
{
--*(_DWORD *)(a1 + 60);
*(_QWORD *)(a2 + 16) = 0LL;
free(v2);
}
return v3;
}

original_code:
static int
bts_close(struct ndn_btree_io *io, struct ndn_btree_node *node)
{
    struct bts_node_state *nd = node->iodata;
    int res = -1;
    if (nd != ((void *)0) && nd->node == node) {
        res = close(nd->fd);
        if (res == -1 && (*__errno_location ()) == 4)
            return(res);
        io->openfds--;
        nd->node = ((void *)0);
        node->iodata = ((void *)0);
        free(nd);
    }
    return(res);
}

variable_types: struct ndn_btree_io { char clue[16]; int (*btopen)(struct ndn_btree_io *, struct ndn_btree_node { unsigned int nodeid; struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } *buf; void *iodata; unsigned int parent; unsigned int clean; unsigned int freelow; unsigned int corrupt; unsigned int activity; } *); int (*btread)(struct ndn_btree_io *, struct ndn_btree_node *, unsigned int); int (*btwrite)(struct ndn_btree_io *, struct ndn_btree_node *); int (*btclose)(struct ndn_btree_io *, struct ndn_btree_node *); int (*btdestroy)(struct ndn_btree_io **); unsigned int maxnodeid; int openfds; void *data; } *

variable_types:
node
struct ndn_btree_node *

variable_types: struct bts_node_state { struct ndn_btree_node { unsigned int nodeid; struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } *buf; void *iodata; unsigned int parent; unsigned int clean; unsigned int freelow; unsigned int corrupt; unsigned int activity; } *node; int fd; } *

user_defined_types: struct ndn_btree_io { char clue[16]; int (*btopen)(struct ndn_btree_io *, struct ndn_btree_node *); int (*btread)(struct ndn_btree_io *, struct ndn_btree_node *, unsigned int); int (*btwrite)(struct ndn_btree_io *, struct ndn_btree_node *); int (*btclose)(struct ndn_btree_io *, struct ndn_btree_node *); int (*btdestroy)(struct ndn_btree_io **); unsigned int maxnodeid; int openfds; void *data; }

user_defined_types: struct bts_node_state { struct ndn_btree_node *node; int fd; }

user_defined_types: struct ndn_btree_node { unsigned int nodeid; struct ndn_charbuf *buf; void *iodata; unsigned int parent; unsigned int clean; unsigned int freelow; unsigned int corrupt; unsigned int activity; }


original_code:
static int
bts_write(struct ndn_btree_io *io, struct ndn_btree_node *node)
{
    struct bts_node_state *nd = node->iodata;
    ssize_t sres;
    off_t offset;
    size_t clean = 0;
    if (nd == ((void *)0) || nd->node != node) abort();
    if (node->clean > 0 && node->clean <= node->buf->length)
        clean = node->clean;
    offset = lseek(nd->fd, clean, 0);
    if (offset == (off_t)-1)
        return(-1);
    if (offset != clean)
        abort();
    sres = write(nd->fd, node->buf->buf + clean, node->buf->length - clean);
    if (sres == -1)
        return(-1);
    if (sres + clean != node->buf->length)
        abort();
    return(ftruncate(nd->fd, node->buf->length));
}

canonical_original_code:
static int bts_write(struct ndn_btree_io *io, struct ndn_btree_node *node)
{
    struct bts_node_state *nd = node->iodata;
    long sres;
    long offset;
    unsigned long clean = 0;
    if (nd == ((void *)0) || nd->node != node) abort();
    if (node->clean > 0 && node->clean <= node->buf->length)
        clean = node->clean;
    offset = lseek(nd->fd, clean, 0);
    if (offset == (off_t)-1)
        return(-1);
    if (offset != clean)
        abort();
    sres = write(nd->fd, node->buf->buf + clean, node->buf->length - clean);
    if (sres == -1)
        return(-1);
    if (sres + clean != node->buf->length)
        abort();
    return(ftruncate(nd->fd, node->buf->length));
}

variable_types: sres long

variable_types: offset long

variable_types:
clean
unsigned long


decompiled_code:
__int64 __fastcall bts_open(__int64 a1, unsigned int *a2)
{
_QWORD *v2; // r13
unsigned int *v4; // r12
__int64 v5; // rdi
int v6; // er14
const char *v7; // rax
int v8; // eax
__int64 v9; // rdx
const char *v10; // rax
int v11; // er13
int v12; // er14
__int64 result; // rax
int v14; // edi
__int64 v15[6]; // [rsp+8h] [rbp-30h] BYREF

v15[0] = 0LL;
v2 = *(_QWORD **)(a1 + 64);
if ( *((_QWORD *)a2 + 2) || *v2 != a1 )
abort();
v4 = (unsigned int *)calloc(1uLL, 0x10uLL);
if ( !v4 )
return 0xFFFFFFFFLL;
v5 = ndn_charbuf_create();
v15[0] = v5;
if ( !v5 )
goto LABEL_12;
v6 = ndn_charbuf_append_charbuf(v5, v2[1]);
if ( (int)(ndn_charbuf_putf(v15[0], "/%u", *a2) | v6) < 0 )
goto LABEL_12;
v7 = (const char *)ndn_charbuf_as_string(v15[0]);
v8 = open(v7, 66, 416LL);
v4[2] = v8;
if ( v8 < 0 )
goto LABEL_12;
if ( *a2 <= *(_DWORD *)(a1 + 56) )
goto LABEL_10;
*(_DWORD *)(a1 + 56) = *a2;
*(_QWORD *)v15[0] = 0LL;
ndn_charbuf_append_charbuf(v15[0], v2[1]);
ndn_charbuf_putf(v15[0], "/maxnodeid", v9);
v10 = (const char *)ndn_charbuf_as_string(v15[0]);
v11 = open(v10, 578, 416LL);
if ( v11 < 0 )
goto LABEL_12;
*(_QWORD *)v15[0] = 0LL;
ndn_charbuf_putf(v15[0], "%u", *a2);
v12 = write(v11, *(const void **)(v15[0] + 16), *(_QWORD *)v15[0]);
close(v11);
if ( v12 == *(_QWORD *)v15[0] )
{
LABEL_10:
ndn_charbuf_destroy(v15);
++*(_DWORD *)(a1 + 60);
*(_QWORD *)v4 = a2;
*((_QWORD *)a2 + 2) = v4;
result = v4[2];
}
else
{
LABEL_12:
ndn_charbuf_destroy(v15);
v14 = v4[2];
if ( v14 >= 0 )
close(v14);
free(v4);
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, unsigned int *a2)
{
_QWORD *v2; // r13
unsigned int *v4; // r12
__int64 v5; // rdi
int v6; // er14
const char *v7; // rax
int v8; // eax
__int64 v9; // rdx
const char *v10; // rax
int v11; // er13
int v12; // er14
__int64 result; // rax
int v14; // edi
__int64 v15[6]; // [rsp+8h] [rbp-30h] BYREF

v15[0] = 0LL;
v2 = *(_QWORD **)(a1 + 64);
if ( *((_QWORD *)a2 + 2) || *v2 != a1 )
abort();
v4 = (unsigned int *)calloc(1uLL, 0x10uLL);
if ( !v4 )
return 0xFFFFFFFFLL;
v5 = ndn_charbuf_create();
v15[0] = v5;
if ( !v5 )
goto LABEL_12;
v6 = ndn_charbuf_append_charbuf(v5, v2[1]);
if ( (int)(ndn_charbuf_putf(v15[0], "/%u", *a2) | v6) < 0 )
goto LABEL_12;
v7 = (const char *)ndn_charbuf_as_string(v15[0]);
v8 = open(v7, 66, 416LL);
v4[2] = v8;
if ( v8 < 0 )
goto LABEL_12;
if ( *a2 <= *(_DWORD *)(a1 + 56) )
goto LABEL_10;
*(_DWORD *)(a1 + 56) = *a2;
*(_QWORD *)v15[0] = 0LL;
ndn_charbuf_append_charbuf(v15[0], v2[1]);
ndn_charbuf_putf(v15[0], "/maxnodeid", v9);
v10 = (const char *)ndn_charbuf_as_string(v15[0]);
v11 = open(v10, 578, 416LL);
if ( v11 < 0 )
goto LABEL_12;
*(_QWORD *)v15[0] = 0LL;
ndn_charbuf_putf(v15[0], "%u", *a2);
v12 = write(v11, *(const void **)(v15[0] + 16), *(_QWORD *)v15[0]);
close(v11);
if ( v12 == *(_QWORD *)v15[0] )
{
LABEL_10:
ndn_charbuf_destroy(v15);
++*(_DWORD *)(a1 + 60);
*(_QWORD *)v4 = a2;
*((_QWORD *)a2 + 2) = v4;
result = v4[2];
}
else
{
LABEL_12:
ndn_charbuf_destroy(v15);
v14 = v4[2];
if ( v14 >= 0 )
close(v14);
free(v4);
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
static int
bts_open(struct ndn_btree_io *io, struct ndn_btree_node *node)
{
    struct bts_node_state *nd = ((void *)0);
    struct ndn_charbuf *temp = ((void *)0);
    struct bts_data *md = io->data;
    int res, l;
    if (node->iodata != ((void *)0) || io != md->io) abort();
    nd = calloc(1, sizeof(*nd));
    if (nd == ((void *)0))
        return(-1);
    temp = ndn_charbuf_create();
    if (temp == ((void *)0))
        goto Bail;
    res = ndn_charbuf_append_charbuf(temp, md->dirpath);
    res |= ndn_charbuf_putf(temp, "/%u", (unsigned)node->nodeid);
    if (res < 0)
        goto Bail;
    nd->fd = open(ndn_charbuf_as_string(temp),
               (02 | 0100),
               0640);
    if (nd->fd < 0)
        goto Bail;
    if (node->nodeid > io->maxnodeid) {
        io->maxnodeid = node->nodeid;
        temp->length = 0;
        ndn_charbuf_append_charbuf(temp, md->dirpath);
        ndn_charbuf_putf(temp, "/maxnodeid");
        res = open(ndn_charbuf_as_string(temp),
                   (02 | 0100 | 01000),
                   0640);
        if (res < 0)
            goto Bail;
        temp->length = 0;
        ndn_charbuf_putf(temp, "%u", (unsigned)node->nodeid);
        l = write(res, temp->buf, temp->length);
        close(res);
        if (l != temp->length)
            goto Bail;
    }
    ndn_charbuf_destroy(&temp);
    io->openfds++;
    nd->node = node;
    node->iodata = nd;
    return(nd->fd);
Bail:
    ndn_charbuf_destroy(&temp);
    if (nd->fd >= 0)
        close(nd->fd);
    free(nd);
    return(-1);
}

canonical_original_code:
static int bts_open(struct ndn_btree_io *io, struct ndn_btree_node *node)
{
    struct bts_node_state *nd = ((void *)0);
    struct ndn_charbuf *temp = ((void *)0);
    struct bts_data *md = io->data;
    int res; int l;
    if (node->iodata != ((void *)0) || io != md->io) abort();
    nd = calloc(1, sizeof(*nd));
    if (nd == ((void *)0))
        return(-1);
    temp = ndn_charbuf_create();
    if (temp == ((void *)0))
        goto Bail;
    res = ndn_charbuf_append_charbuf(temp, md->dirpath);
    res |= ndn_charbuf_putf(temp, "/%u", (unsigned int)node->nodeid);
    if (res < 0)
        goto Bail;
    nd->fd = open(ndn_charbuf_as_string(temp),
               (02 | 0100),
               0640);
    if (nd->fd < 0)
        goto Bail;
    if (node->nodeid > io->maxnodeid) {
        io->maxnodeid = node->nodeid;
        temp->length = 0;
        ndn_charbuf_append_charbuf(temp, md->dirpath);
        ndn_charbuf_putf(temp, "/maxnodeid");
        res = open(ndn_charbuf_as_string(temp),
                   (02 | 0100 | 01000),
                   0640);
        if (res < 0)
            goto Bail;
        temp->length = 0;
        ndn_charbuf_putf(temp, "%u", (unsigned int)node->nodeid);
        l = write(res, temp->buf, temp->length);
        close(res);
        if (l != temp->length)
            goto Bail;
    }
    ndn_charbuf_destroy(&temp);
    io->openfds++;
    nd->node = node;
    node->iodata = nd;
    return(nd->fd);
Bail:
    ndn_charbuf_destroy(&temp);
    if (nd->fd >= 0)
        close(nd->fd);
    free(nd);
    return(-1);
}

variable_types: struct bts_data { struct ndn_btree_io { char clue[16]; int (*btopen)(struct ndn_btree_io *, struct ndn_btree_node { unsigned int nodeid; struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } *buf; void *iodata; unsigned int parent; unsigned int clean; unsigned int freelow; unsigned int corrupt; unsigned int activity; } *); int (*btread)(struct ndn_btree_io *, struct ndn_btree_node *, unsigned int); int (*btwrite)(struct ndn_btree_io *, struct ndn_btree_node *); int (*btclose)(struct ndn_btree_io *, struct ndn_btree_node *); int (*btdestroy)(struct ndn_btree_io **); unsigned int maxnodeid; int openfds; void *data; } *io; struct ndn_charbuf *dirpath; int lfd; } *

variable_types: l int

user_defined_types: struct bts_data { struct ndn_btree_io *io; struct ndn_charbuf *dirpath; int lfd; }


decompiled_code:
__int64 __fastcall bts_destroy(void **a1)
{
__int64 v2; // rdi
unsigned int v3; // ebp
void **v4; // r12

v2 = (__int64)*a1;
if ( !v2 )
return 0;
if ( *(__int64 (__fastcall **)())(v2 + 48) != bts_destroy )
abort();
v3 = bts_remove_lockfile(v2);
v4 = (void **)*((_QWORD *)*a1 + 8);
if ( *a1 != *v4 )
abort();
ndn_charbuf_destroy(v4 + 1);
free(v4);
*((_QWORD *)*a1 + 8) = 0LL;
free(*a1);
*a1 = 0LL;
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func4(void **a1)
{
__int64 v2; // rdi
unsigned int v3; // ebp
void **v4; // r12

v2 = (__int64)*a1;
if ( !v2 )
return 0;
if ( *(__int64 (__fastcall **)())(v2 + 48) != bts_destroy )
abort();
v3 = func3(v2);
v4 = (void **)*((_QWORD *)*a1 + 8);
if ( *a1 != *v4 )
abort();
ndn_charbuf_destroy(v4 + 1);
free(v4);
*((_QWORD *)*a1 + 8) = 0LL;
free(*a1);
*a1 = 0LL;
return v3;
}

original_code:
static int
bts_destroy(struct ndn_btree_io **pio)
{
    int res;
    struct bts_data *md = ((void *)0);
    if (*pio == ((void *)0))
        return(0);
    if ((*pio)->btdestroy != &bts_destroy)
        abort();
    res = bts_remove_lockfile(*pio);
    md = (*pio)->data;
    if (md->io != *pio) abort();
    ndn_charbuf_destroy(&md->dirpath);
    free(md);
    (*pio)->data = ((void *)0);
    free(*pio);
    *pio = ((void *)0);
    return(res);
}

variable_types: struct ndn_btree_io { char clue[16]; int (*btopen)(struct ndn_btree_io *, struct ndn_btree_node { unsigned int nodeid; struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } *buf; void *iodata; unsigned int parent; unsigned int clean; unsigned int freelow; unsigned int corrupt; unsigned int activity; } *); int (*btread)(struct ndn_btree_io *, struct ndn_btree_node *, unsigned int); int (*btwrite)(struct ndn_btree_io *, struct ndn_btree_node *); int (*btclose)(struct ndn_btree_io *, struct ndn_btree_node *); int (*btdestroy)(struct ndn_btree_io **); unsigned int maxnodeid; int openfds; void *data; } **


decompiled_code:
__int64 __fastcall bts_read(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 v3; // r12
__off_t v5; // rax
unsigned int v6; // eax
__off_t v7; // r13
__off_t v8; // rax
size_t v9; // rbx
void *v10; // rax
ssize_t v11; // rax
unsigned __int64 *v12; // rdx
unsigned __int64 v13; // rax

v3 = *(_QWORD *)(a2 + 16);
if ( !v3 || *(_QWORD *)v3 != a2 )
abort();
v5 = lseek(*(_DWORD *)(v3 + 8), 0LL, 2);
if ( v5 == -1 )
return 0xFFFFFFFFLL;
if ( a3 > v5 )
a3 = v5;
v6 = *(_DWORD *)(a2 + 28);
v7 = 0LL;
if ( v6 && (unsigned __int64)v6 <= **(_QWORD **)(a2 + 8) )
v7 = v6;
v8 = lseek(*(_DWORD *)(v3 + 8), v7, 0);
if ( v8 == -1 )
return 0xFFFFFFFFLL;
if ( v7 != v8 )
abort();
**(_QWORD **)(a2 + 8) = v8;
v9 = a3 - v8;
v10 = (void *)ndn_charbuf_reserve(*(_QWORD *)(a2 + 8), v9);
v11 = read(*(_DWORD *)(v3 + 8), v10, v9);
if ( v11 < 0 )
return 0xFFFFFFFFLL;
if ( v9 != v11 )
abort();
v12 = *(unsigned __int64 **)(a2 + 8);
v13 = *v12 + v11;
if ( v13 > v12[1] )
abort();
*v12 = v13;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, unsigned int a3)
{
__int64 v3; // r12
__off_t v5; // rax
unsigned int v6; // eax
__off_t v7; // r13
__off_t v8; // rax
size_t v9; // rbx
void *v10; // rax
ssize_t v11; // rax
unsigned __int64 *v12; // rdx
unsigned __int64 v13; // rax

v3 = *(_QWORD *)(a2 + 16);
if ( !v3 || *(_QWORD *)v3 != a2 )
abort();
v5 = lseek(*(_DWORD *)(v3 + 8), 0LL, 2);
if ( v5 == -1 )
return 0xFFFFFFFFLL;
if ( a3 > v5 )
a3 = v5;
v6 = *(_DWORD *)(a2 + 28);
v7 = 0LL;
if ( v6 && (unsigned __int64)v6 <= **(_QWORD **)(a2 + 8) )
v7 = v6;
v8 = lseek(*(_DWORD *)(v3 + 8), v7, 0);
if ( v8 == -1 )
return 0xFFFFFFFFLL;
if ( v7 != v8 )
abort();
**(_QWORD **)(a2 + 8) = v8;
v9 = a3 - v8;
v10 = (void *)ndn_charbuf_reserve(*(_QWORD *)(a2 + 8), v9);
v11 = read(*(_DWORD *)(v3 + 8), v10, v9);
if ( v11 < 0 )
return 0xFFFFFFFFLL;
if ( v9 != v11 )
abort();
v12 = *(unsigned __int64 **)(a2 + 8);
v13 = *v12 + v11;
if ( v13 > v12[1] )
abort();
*v12 = v13;
return 0LL;
}

original_code:
static int
bts_read(struct ndn_btree_io *io, struct ndn_btree_node *node, unsigned limit)
{
    struct bts_node_state *nd = node->iodata;
    ssize_t sres;
    off_t offset;
    off_t clean = 0;
    if (nd == ((void *)0) || nd->node != node) abort();
    offset = lseek(nd->fd, 0, 2);
    if (offset == (off_t)-1)
        return(-1);
    if (offset < limit)
        limit = offset;
    if (node->clean > 0 && node->clean <= node->buf->length)
        clean = node->clean;
    offset = lseek(nd->fd, clean, 0);
    if (offset == (off_t)-1)
        return(-1);
    if (offset != clean)
        abort();
    node->buf->length = clean;
    sres = read(nd->fd, ndn_charbuf_reserve(node->buf, limit - clean), limit - clean);
    if (sres < 0)
        return(-1);
    if (sres != limit - clean) {
        abort();
    }
    if (sres + node->buf->length > node->buf->limit) {
        abort();
    }
    node->buf->length += sres;
    return(0);
}

canonical_original_code:
static int bts_read(struct ndn_btree_io *io, struct ndn_btree_node *node, unsigned int limit)
{
    struct bts_node_state *nd = node->iodata;
    long sres;
    long offset;
    long clean = 0;
    if (nd == ((void *)0) || nd->node != node) abort();
    offset = lseek(nd->fd, 0, 2);
    if (offset == (off_t)-1)
        return(-1);
    if (offset < limit)
        limit = offset;
    if (node->clean > 0 && node->clean <= node->buf->length)
        clean = node->clean;
    offset = lseek(nd->fd, clean, 0);
    if (offset == (off_t)-1)
        return(-1);
    if (offset != clean)
        abort();
    node->buf->length = clean;
    sres = read(nd->fd, ndn_charbuf_reserve(node->buf, limit - clean), limit - clean);
    if (sres < 0)
        return(-1);
    if (sres != limit - clean) {
        abort();
    }
    if (sres + node->buf->length > node->buf->limit) {
        abort();
    }
    node->buf->length += sres;
    return(0);
}

variable_types:
limit
unsigned int

variable_types: clean long


decompiled_code:
__int64 __fastcall ndn_interest_lifetime(__int64 a1, __int64 a2)
{
unsigned __int16 v2; // cx
__int64 result; // rax
__int64 v4; // rbx
__int64 v5; // rcx
int v6; // edx
__int64 v7; // [rsp-48h] [rbp-48h] BYREF

v2 = *(_WORD *)(a2 + 70);
result = 0x4000LL;
if ( *(unsigned __int16 *)(a2 + 72) != v2 )
{
v4 = ndn_buf_decoder_start(&v7, a1 + v2);
v5 = ndn_parse_optional_tagged_binary_number(v4, 48LL, 1LL, 7LL, 0x4000LL);
v6 = *(_DWORD *)(v4 + 8);
result = v6;
if ( v6 >= 0 )
result = v5;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
unsigned __int16 v2; // cx
__int64 result; // rax
__int64 v4; // rbx
__int64 v5; // rcx
int v6; // edx
__int64 v7; // [rsp-48h] [rbp-48h] BYREF

v2 = *(_WORD *)(a2 + 70);
result = 0x4000LL;
if ( *(unsigned __int16 *)(a2 + 72) != v2 )
{
v4 = ndn_buf_decoder_start(&v7, a1 + v2);
v5 = ndn_parse_optional_tagged_binary_number(v4, 48LL, 1LL, 7LL, 0x4000LL);
v6 = *(_DWORD *)(v4 + 8);
result = v6;
if ( v6 >= 0 )
result = v5;
}
return result;
}

original_code:
intmax_t
ndn_interest_lifetime(const unsigned char *msg,
                      const struct ndn_parsed_interest *pi)
{
    struct ndn_buf_decoder decoder;
    struct ndn_buf_decoder *d = ((void *)0);
    unsigned start = pi->offset[NDN_PI_B_InterestLifetime];
    size_t size = pi->offset[NDN_PI_E_InterestLifetime] - start;
    uintmax_t val;
    if (size == 0)
        return(4 << 12);
    d = ndn_buf_decoder_start(&decoder, msg + start, size);
    val = ndn_parse_optional_tagged_binary_number(d, NDN_DTAG_InterestLifetime,
   1, 7, 4 << 12);
    if (d->decoder.state < 0)
        return (d->decoder.state);
    return(val);
}

canonical_original_code:
long ndn_interest_lifetime(unsigned char *msg, struct ndn_parsed_interest *pi)
{
    struct ndn_buf_decoder decoder;
    struct ndn_buf_decoder *d = ((void *)0);
    unsigned int start = pi->offset[NDN_PI_B_InterestLifetime];
    unsigned long size = pi->offset[NDN_PI_E_InterestLifetime] - start;
    unsigned long val;
    if (size == 0)
        return(4 << 12);
    d = ndn_buf_decoder_start(&decoder, msg + start, size);
    val = ndn_parse_optional_tagged_binary_number(d, NDN_DTAG_InterestLifetime,
   1, 7, 4 << 12);
    if (d->decoder.state < 0)
        return (d->decoder.state);
    return(val);
}

variable_types:
msg
unsigned char *

variable_types: struct ndn_parsed_interest { int magic; int prefix_comps; int min_suffix_comps; int max_suffix_comps; int orderpref; int answerfrom; int scope; unsigned short offset[-1]; } *

variable_types: struct ndn_buf_decoder { struct ndn_skeleton_decoder { long index; int state; int nest; unsigned long numval; unsigned long token_index; unsigned long element_index; } decoder; unsigned char *buf; unsigned long size; } *

variable_types:
start
unsigned int

variable_types:
size
unsigned long

variable_types:
val
unsigned long

return_type: long

user_defined_types: struct ndn_buf_decoder { struct ndn_skeleton_decoder decoder; unsigned char *buf; unsigned long size; }


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

result = ndn_interest_lifetime(a1, a2);
if ( result >= 0 )
result = (unsigned int)(result >> 12);
return result;
}

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

result = func0(a1, a2);
if ( result >= 0 )
result = (unsigned int)(result >> 12);
return result;
}

original_code:
int
ndn_interest_lifetime_seconds(const unsigned char *msg,
                              const struct ndn_parsed_interest *pi)
{
    intmax_t val = ndn_interest_lifetime(msg, pi);
    if (val < 0)
        return(val);
    return(val >> 12);
}

canonical_original_code:
int ndn_interest_lifetime_seconds(unsigned char *msg, struct ndn_parsed_interest *pi)
{
    long val = ndn_interest_lifetime(msg, pi);
    if (val < 0)
        return(val);
    return(val >> 12);
}

variable_types: val long

user_defined_types: struct ndn_parsed_interest { int magic; int prefix_comps; int min_suffix_comps; int max_suffix_comps; int orderpref; int answerfrom; int scope; unsigned short offset[-1]; }


original_code:
ssize_t
ndn_skeleton_decode(struct ndn_skeleton_decoder *d,
                    const unsigned char *p, size_t n)
{
    enum ndn_decoder_state state = d->state;
    int tagstate = 0;
    size_t numval = d->numval;
    ssize_t i = 0;
    unsigned char c;
    size_t chunk;
    int pause = 0;
    if (d->state >= 0) {
        pause = d->state & (1 << 15);
        tagstate = (d->state >> 8) & 3;
        state = d->state & 0xFF;
    }
    while (i < n) {
        switch (state) {
            case NDN_DSTATE_INITIAL:
            case NDN_DSTATE_NEWTOKEN:
                d->token_index = i + d->index;
                if (tagstate > 1 && tagstate-- == 2) {
                    ((void)0);
                }
                if (p[i] == ((unsigned char)(0))) {
                    i++;
                    if (d->nest <= 0 || tagstate > 1) {
                        state = NDN_DSTATE_ERR_NEST;
                        break;
                    }
                    if (tagstate == 1) {
                        tagstate = 0;
                        ((void)0);
                    }
                    else {
                        ((void)0);
                    }
                    d->nest -= 1;
                    if (d->nest == 0) {
                        state = NDN_DSTATE_INITIAL;
                        n = i;
                    }
                    if (pause) {
                        state |= (((int)NDN_NO_TOKEN) << 16);
                        n = i;
                    }
                    break;
                }
                numval = 0;
                state = NDN_DSTATE_NUMVAL;
            case NDN_DSTATE_NUMVAL:
                c = p[i++];
                if ((c & ((unsigned char)(1 << 7))) == ((unsigned char)(0))) {
                    if (numval > ((~(size_t)0U) >> (7 + 3)))
                        state = NDN_DSTATE_ERR_OVERFLOW;
                    numval = (numval << 7) + (c & 127);
                }
                else {
                    numval = (numval << (7-3)) +
                             ((c >> 3) & ((1 << (7-3)) - 1));
                    c &= ((1 << 3) - 1);
                    switch (c) {
                        case NDN_EXT:
                            if (tagstate == 1) {
                                tagstate = 0;
                                ((void)0);
                            }
                            d->nest += 1;
                            d->element_index = d->token_index;
                            state = NDN_DSTATE_NEWTOKEN;
                            break;
                        case NDN_DTAG:
                            if (tagstate == 1) {
                                tagstate = 0;
                                ((void)0);
                            }
                            d->nest += 1;
                            d->element_index = d->token_index;
                            ((void)0);
                            tagstate = 1;
                            state = NDN_DSTATE_NEWTOKEN;
                            break;
                        case NDN_BLOB:
                            if (tagstate == 1) {
                                tagstate = 0;
                                ((void)0);
                            }
                            state = NDN_DSTATE_BLOB;
                            if (numval == 0)
                                state = NDN_DSTATE_NEWTOKEN;
                            break;
                        case NDN_UDATA:
                            if (tagstate == 1) {
                                tagstate = 0;
                                ((void)0);
                            }
                            state = NDN_DSTATE_UDATA;
                            if (numval == 0)
                                state = NDN_DSTATE_NEWTOKEN;
                            break;
                        case NDN_DATTR:
                            if (tagstate != 1) {
                                state = NDN_DSTATE_ERR_ATTR;
                                break;
                            }
                            tagstate = 3;
                            state = NDN_DSTATE_NEWTOKEN;
                            break;
                        case NDN_ATTR:
                            if (tagstate != 1) {
                                state = NDN_DSTATE_ERR_ATTR;
                                break;
                            }
                            numval += 1;
                            state = NDN_DSTATE_ATTRNAME;
                            break;
                        case NDN_TAG:
                            if (tagstate == 1) {
                                tagstate = 0;
                                ((void)0);
                            }
                            numval += 1;
                            d->nest += 1;
                            d->element_index = d->token_index;
                            state = NDN_DSTATE_TAGNAME;
                            break;
                        default:
                            state = NDN_DSTATE_ERR_CODING;
                    }
                    if (pause) {
                        state |= (c << 16);
                        n = i;
                    }
                }
                break;
            case NDN_DSTATE_TAGNAME:
                chunk = n - i;
                if (chunk > numval)
                    chunk = numval;
                if (chunk == 0) {
                    state = NDN_DSTATE_ERR_BUG;
                    break;
                }
                numval -= chunk;
                i += chunk;
                if (numval == 0) {
                    if (d->nest == 0) {
                        state = NDN_DSTATE_ERR_NEST;
                        break;
                    }
                    ((void)0);
                    tagstate = 1;
                    state = NDN_DSTATE_NEWTOKEN;
                }
                break;
            case NDN_DSTATE_ATTRNAME:
                chunk = n - i;
                if (chunk > numval)
                    chunk = numval;
                if (chunk == 0) {
                    state = NDN_DSTATE_ERR_BUG;
                    break;
                }
                numval -= chunk;
                i += chunk;
                if (numval == 0) {
                    if (d->nest == 0) {
                        state = NDN_DSTATE_ERR_ATTR;
                        break;
                    }
                    ((void)0);
                    tagstate = 3;
                    state = NDN_DSTATE_NEWTOKEN;
                }
                break;
            case NDN_DSTATE_UDATA:
            case NDN_DSTATE_BLOB:
                chunk = n - i;
                if (chunk > numval)
                    chunk = numval;
                if (chunk == 0) {
                    state = NDN_DSTATE_ERR_BUG;
                    break;
                }
                numval -= chunk;
                i += chunk;
                if (numval == 0)
                    state = NDN_DSTATE_NEWTOKEN;
                break;
            default:
                n = i;
        }
    }
    if (state < 0)
        tagstate = pause = 0;
    d->state = state | pause | (tagstate << 8);
    d->numval = numval;
    d->index += i;
    return(i);
}

canonical_original_code:
long ndn_skeleton_decode(struct ndn_skeleton_decoder *d, unsigned char *p, unsigned long n)
{
    enum ndn_decoder_state state = d->state;
    int tagstate = 0;
    unsigned long numval = d->numval;
    long i = 0;
    unsigned char c;
    unsigned long chunk;
    int pause = 0;
    if (d->state >= 0) {
        pause = d->state & (1 << 15);
        tagstate = (d->state >> 8) & 3;
        state = d->state & 0xFF;
    }
    while (i < n) {
        switch (state) {
            case NDN_DSTATE_INITIAL:
            case NDN_DSTATE_NEWTOKEN:
                d->token_index = i + d->index;
                if (tagstate > 1 && tagstate-- == 2) {
                    ((void)0);
                }
                if (p[i] == ((unsigned char)(0))) {
                    i++;
                    if (d->nest <= 0 || tagstate > 1) {
                        state = NDN_DSTATE_ERR_NEST;
                        break;
                    }
                    if (tagstate == 1) {
                        tagstate = 0;
                        ((void)0);
                    }
                    else {
                        ((void)0);
                    }
                    d->nest -= 1;
                    if (d->nest == 0) {
                        state = NDN_DSTATE_INITIAL;
                        n = i;
                    }
                    if (pause) {
                        state |= (((int)NDN_NO_TOKEN) << 16);
                        n = i;
                    }
                    break;
                }
                numval = 0;
                state = NDN_DSTATE_NUMVAL;
            case NDN_DSTATE_NUMVAL:
                c = p[i++];
                if ((c & ((unsigned char)(1 << 7))) == ((unsigned char)(0))) {
                    if (numval > ((~(unsigned long)0U) >> (7 + 3)))
                        state = NDN_DSTATE_ERR_OVERFLOW;
                    numval = (numval << 7) + (c & 127);
                }
                else {
                    numval = (numval << (7-3)) +
                             ((c >> 3) & ((1 << (7-3)) - 1));
                    c &= ((1 << 3) - 1);
                    switch (c) {
                        case NDN_EXT:
                            if (tagstate == 1) {
                                tagstate = 0;
                                ((void)0);
                            }
                            d->nest += 1;
                            d->element_index = d->token_index;
                            state = NDN_DSTATE_NEWTOKEN;
                            break;
                        case NDN_DTAG:
                            if (tagstate == 1) {
                                tagstate = 0;
                                ((void)0);
                            }
                            d->nest += 1;
                            d->element_index = d->token_index;
                            ((void)0);
                            tagstate = 1;
                            state = NDN_DSTATE_NEWTOKEN;
                            break;
                        case NDN_BLOB:
                            if (tagstate == 1) {
                                tagstate = 0;
                                ((void)0);
                            }
                            state = NDN_DSTATE_BLOB;
                            if (numval == 0)
                                state = NDN_DSTATE_NEWTOKEN;
                            break;
                        case NDN_UDATA:
                            if (tagstate == 1) {
                                tagstate = 0;
                                ((void)0);
                            }
                            state = NDN_DSTATE_UDATA;
                            if (numval == 0)
                                state = NDN_DSTATE_NEWTOKEN;
                            break;
                        case NDN_DATTR:
                            if (tagstate != 1) {
                                state = NDN_DSTATE_ERR_ATTR;
                                break;
                            }
                            tagstate = 3;
                            state = NDN_DSTATE_NEWTOKEN;
                            break;
                        case NDN_ATTR:
                            if (tagstate != 1) {
                                state = NDN_DSTATE_ERR_ATTR;
                                break;
                            }
                            numval += 1;
                            state = NDN_DSTATE_ATTRNAME;
                            break;
                        case NDN_TAG:
                            if (tagstate == 1) {
                                tagstate = 0;
                                ((void)0);
                            }
                            numval += 1;
                            d->nest += 1;
                            d->element_index = d->token_index;
                            state = NDN_DSTATE_TAGNAME;
                            break;
                        default:
                            state = NDN_DSTATE_ERR_CODING;
                    }
                    if (pause) {
                        state |= (c << 16);
                        n = i;
                    }
                }
                break;
            case NDN_DSTATE_TAGNAME:
                chunk = n - i;
                if (chunk > numval)
                    chunk = numval;
                if (chunk == 0) {
                    state = NDN_DSTATE_ERR_BUG;
                    break;
                }
                numval -= chunk;
                i += chunk;
                if (numval == 0) {
                    if (d->nest == 0) {
                        state = NDN_DSTATE_ERR_NEST;
                        break;
                    }
                    ((void)0);
                    tagstate = 1;
                    state = NDN_DSTATE_NEWTOKEN;
                }
                break;
            case NDN_DSTATE_ATTRNAME:
                chunk = n - i;
                if (chunk > numval)
                    chunk = numval;
                if (chunk == 0) {
                    state = NDN_DSTATE_ERR_BUG;
                    break;
                }
                numval -= chunk;
                i += chunk;
                if (numval == 0) {
                    if (d->nest == 0) {
                        state = NDN_DSTATE_ERR_ATTR;
                        break;
                    }
                    ((void)0);
                    tagstate = 3;
                    state = NDN_DSTATE_NEWTOKEN;
                }
                break;
            case NDN_DSTATE_UDATA:
            case NDN_DSTATE_BLOB:
                chunk = n - i;
                if (chunk > numval)
                    chunk = numval;
                if (chunk == 0) {
                    state = NDN_DSTATE_ERR_BUG;
                    break;
                }
                numval -= chunk;
                i += chunk;
                if (numval == 0)
                    state = NDN_DSTATE_NEWTOKEN;
                break;
            default:
                n = i;
        }
    }
    if (state < 0)
        tagstate = pause = 0;
    d->state = state | pause | (tagstate << 8);
    d->numval = numval;
    d->index += i;
    return(i);
}

variable_types: struct ndn_skeleton_decoder { long index; int state; int nest; unsigned long numval; unsigned long token_index; unsigned long element_index; } *

variable_types:
p
unsigned char *

variable_types:
n
unsigned long

variable_types: enum ndn_decoder_state { NDN_DSTATE_INITIAL=0, NDN_DSTATE_NEWTOKEN=1, NDN_DSTATE_NUMVAL=2, NDN_DSTATE_UDATA=3, NDN_DSTATE_TAGNAME=4, NDN_DSTATE_ATTRNAME=5, NDN_DSTATE_BLOB=6, NDN_DSTATE_ERR_OVERFLOW=-1, NDN_DSTATE_ERR_ATTR=-2, NDN_DSTATE_ERR_CODING=-3, NDN_DSTATE_ERR_NEST=-4, NDN_DSTATE_ERR_BUG=-5 }

variable_types: tagstate int

variable_types:
numval
unsigned long

variable_types: i long

variable_types:
c
unsigned char

variable_types:
chunk
unsigned long

variable_types: pause int


decompiled_code:
__int64 __fastcall ndn_decimal_seqfunc(__int64 a1, __int64 a2, _QWORD *a3)
{
if ( *a3 )
_assert_fail("resultbuf->length == 0", "ndn_bulkdata.c", 0x3Eu, "ndn_decimal_seqfunc");
return ndn_charbuf_putf(a3, "%ju", a1);
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, _QWORD *a3)
{
if ( *a3 )
_assert_fail("resultbuf->length == 0", "ndn_bulkdata.c", 0x3Eu, "ndn_decimal_seqfunc");
return ndn_charbuf_putf(a3, "%ju", a1);
}

original_code:
void
ndn_decimal_seqfunc(uintmax_t x, void *param, struct ndn_charbuf *resultbuf)
{
    (void)param;
    ((void) sizeof ((resultbuf->length == 0) ? 1 : 0), __extension__ ({ if (resultbuf->length == 0) ; else __assert_fail ("resultbuf->length == 0", "ndn_bulkdata.c", 62, __extension__ __PRETTY_FUNCTION__); }));
    ndn_charbuf_putf(resultbuf, "%ju", x);
}

canonical_original_code:
void ndn_decimal_seqfunc(unsigned long x, void *param, struct ndn_charbuf *resultbuf)
{
    (void)param;
    ((void) sizeof ((resultbuf->length == 0) ? 1 : 0), __extension__ ({ if (resultbuf->length == 0) ; else __assert_fail ("resultbuf->length == 0", "ndn_bulkdata.c", 62, __extension__ __PRETTY_FUNCTION__); }));
    ndn_charbuf_putf(resultbuf, "%ju", x);
}

variable_types:
x
unsigned long

variable_types:
param
void *

variable_types: struct ndn_charbuf { unsigned long length; unsigned long limit; unsigned char *buf; } *


decompiled_code:
__int64 __fastcall ndn_charbuf_append_sockaddr(__int64 *a1, unsigned __int16 *a2)
{
__int64 v2; // r14
unsigned int v4; // er12
const void *v5; // r15
void *v6; // r13
char *v7; // rax
const char *v8; // rdi

v2 = *a1;
if ( !a2 )
return (unsigned int)-1;
if ( *a2 == 2 )
{
v5 = a2 + 2;
v4 = (unsigned __int16)_bswap_16(a2[1]);
v6 = &LC0;
}
else
{
v4 = -1;
if ( *a2 != 10 )
return v4;
v5 = a2 + 4;
v4 = (unsigned __int16)_bswap_16(a2[1]);
ndn_charbuf_append_string(a1, &LC2);
v6 = &LC1;
}
v7 = (char *)ndn_charbuf_reserve(a1, 80LL);
v8 = inet_ntop(*a2, v5, v7, 0x50u);
if ( v8 )
{
*a1 += strlen(v8);
ndn_charbuf_append_string(a1, v6);
}
else
{
*a1 = v2;
v4 = -1;
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 *a1, unsigned __int16 *a2)
{
__int64 v2; // r14
unsigned int v4; // er12
const void *v5; // r15
void *v6; // r13
char *v7; // rax
const char *v8; // rdi

v2 = *a1;
if ( !a2 )
return (unsigned int)-1;
if ( *a2 == 2 )
{
v5 = a2 + 2;
v4 = (unsigned __int16)func0(a2[1]);
v6 = &LC0;
}
else
{
v4 = -1;
if ( *a2 != 10 )
return v4;
v5 = a2 + 4;
v4 = (unsigned __int16)func0(a2[1]);
ndn_charbuf_append_string(a1, &LC2);
v6 = &LC1;
}
v7 = (char *)ndn_charbuf_reserve(a1, 80LL);
v8 = inet_ntop(*a2, v5, v7, 0x50u);
if ( v8 )
{
*a1 += strlen(v8);
ndn_charbuf_append_string(a1, v6);
}
else
{
*a1 = v2;
v4 = -1;
}
return v4;
}

original_code:
int
ndn_charbuf_append_sockaddr(struct ndn_charbuf *c, const struct sockaddr *sa)
{
    const unsigned char *rawaddr = ((void *)0);
    const char *s = ((void *)0);
    const char *closer = "";
    const struct sockaddr_in *addr4 = ((void *)0);
    const struct sockaddr_in6 *addr6 = ((void *)0);
    size_t savlen = c->length;
    socklen_t sz = 80;
    int port = 0;
    if (sa == ((void *)0))
        return(-1);
    switch (sa->sa_family) {
        case 2:
            addr4 = (struct sockaddr_in *)sa;
            rawaddr = (const unsigned char *)&addr4->sin_addr.s_addr;
            port = htons(addr4->sin_port);break;
        case 10:
            addr6 = (struct sockaddr_in6 *)sa;
            rawaddr = (const unsigned char *)&addr6->sin6_addr;
            port = htons(addr6->sin6_port);
            ndn_charbuf_append_string(c, "[");
            closer = "]";
            break;
        default:
            return(-1);
    }
    s = inet_ntop(sa->sa_family, rawaddr,
                  (void *)ndn_charbuf_reserve(c, sz), sz);
    if (s == ((void *)0)) {
        c->length = savlen;
        return(-1);
    }
    c->length += strlen(s);
    ndn_charbuf_append_string(c, closer);
    return(port);
}

canonical_original_code:
int ndn_charbuf_append_sockaddr(struct ndn_charbuf *c, struct sockaddr *sa)
{
    unsigned char *rawaddr = ((void *)0);
    char *s = ((void *)0);
    char *closer = "";
    struct sockaddr_in *addr4 = ((void *)0);
    struct sockaddr_in6 *addr6 = ((void *)0);
    unsigned long savlen = c->length;
    unsigned int sz = 80;
    int port = 0;
    if (sa == ((void *)0))
        return(-1);
    switch (sa->sa_family) {
        case 2:
            addr4 = (struct sockaddr_in *)sa;
            rawaddr = (unsigned char *)&addr4->sin_addr.s_addr;
            port = htons(addr4->sin_port);break;
        case 10:
            addr6 = (struct sockaddr_in6 *)sa;
            rawaddr = (unsigned char *)&addr6->sin6_addr;
            port = htons(addr6->sin6_port);
            ndn_charbuf_append_string(c, "[");
            closer = "]";
            break;
        default:
            return(-1);
    }
    s = inet_ntop(sa->sa_family, rawaddr,
                  (void *)ndn_charbuf_reserve(c, sz), sz);
    if (s == ((void *)0)) {
        c->length = savlen;
        return(-1);
    }
    c->length += strlen(s);
    ndn_charbuf_append_string(c, closer);
    return(port);
}

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

variable_types:
rawaddr
unsigned char *

variable_types:
s
char *

variable_types:
closer
char *

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

variable_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } sin6_addr; unsigned int sin6_scope_id; } *

variable_types:
savlen
unsigned long

variable_types:
sz
unsigned int

variable_types: port int

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

user_defined_types: struct sockaddr_in6 { unsigned short sin6_family; unsigned short sin6_port; unsigned int sin6_flowinfo; struct in6_addr sin6_addr; unsigned int sin6_scope_id; }


usage

repo: dankamongmen/dellicacy

decompiled_code:
size_t usage()
{
return fwrite("usage: ipc [ -r maxrate/sec ]\n", 1uLL, 0x1EuLL, stderr);
}

canonical_decompiled_code:
size_t func7()
{
return fwrite("usage: ipc [ -r maxrate/sec ]\n", 1uLL, 0x1EuLL, stderr);
}

original_code:
static void
usage(void){
 fprintf(stderr,"usage: ipc [ -r maxrate/sec ]\n");
}

canonical_original_code:
static void usage(){
 fprintf(stderr,"usage: ipc [ -r maxrate/sec ]\n");
}


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
int v4; // ebx
int v6; // eax
int v7; // er12
int *v8; // rax
char *v9; // rax
char *v10; // [rsp+0h] [rbp-38h] BYREF
void *ptr; // [rsp+8h] [rbp-30h] BYREF

v3 = 0LL;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "r:");
if ( v4 == -1 )
break;
if ( v4 == 114 && !v3 )
{
v3 = strtoull(optarg, &v10, 0);
if ( (unsigned __int64)(v3 - 1) <= 0xFFFFFFFFFFFFFFFDLL )
continue;
}
goto LABEL_6;
}
if ( argv[optind] )
{
LABEL_6:
usage();
return 1;
}
if ( v3 )
printf("Ratelimited to %llu message per second\n", v3);
else
puts("No ratelimit in effect");
printf("Serving %u %s\n", 10LL, "children");
if ( (unsigned int)spawn_hungry_children(0xAu, &ptr) )
return 1;
v6 = open("/dev/urandom", 0);
v7 = v6;
if ( v6 < 0 )
{
v8 = __errno_location();
v9 = strerror(*v8);
fprintf(stderr, "Coudln't open /dev/urandom for input (%s?)\n", v9);
}
else
{
v4 = feed_hungry_children(v6, 10, (__int64)ptr, v3);
close(v7);
free(ptr);
if ( v4 )
v4 = 1;
}
return v4;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
int v4; // ebx
int v6; // eax
int v7; // er12
int *v8; // rax
char *v9; // rax
char *v10; // [rsp+0h] [rbp-38h] BYREF
void *ptr; // [rsp+8h] [rbp-30h] BYREF

v3 = 0LL;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "r:");
if ( v4 == -1 )
break;
if ( v4 == 114 && !v3 )
{
v3 = strtoull(optarg, &v10, 0);
if ( (unsigned __int64)(v3 - 1) <= 0xFFFFFFFFFFFFFFFDLL )
continue;
}
goto LABEL_6;
}
if ( argv[optind] )
{
LABEL_6:
func7();
return 1;
}
if ( v3 )
printf("Ratelimited to %llu message per second\n", v3);
else
puts("No ratelimit in effect");
printf("Serving %u %s\n", 10LL, "children");
if ( (unsigned int)func9(0xAu, &ptr) )
return 1;
v6 = open("/dev/urandom", 0);
v7 = v6;
if ( v6 < 0 )
{
v8 = __errno_location();
v9 = strerror(*v8);
fprintf(stderr, "Coudln't open /dev/urandom for input (%s?)\n", v9);
}
else
{
v4 = func12(v6, 10, (__int64)ptr, v3);
close(v7);
free(ptr);
if ( v4 )
v4 = 1;
}
return v4;
}

original_code:
int main(int argc,char **argv){
 unsigned mouths = 10;
 unsigned long long rate = 0;
 int c,*pipes,rnd;
 while((c = getopt(argc,argv,"r:")) != -1){
  switch(c){
   case 'r':{
    char *e;
    if(rate){
     usage();
     return 1;
    }
    if((rate = strtoull(optarg,&e,0)) == 0 || rate == (0x7fffffffffffffffLL * 2ULL + 1ULL)){
     usage();
     return 1;
    }
    break;}
   case '?':
   default:
    usage();
    return 1;
  }
 }
 if(argv[optind]){
  usage();
  return 1;
 }
 if(rate){
  printf("Ratelimited to %llu message per second\n",rate);
 }else{
  printf("No ratelimit in effect\n");
 }
 printf("Serving %u %s\n",mouths,mouths == 1 ? "child" : "children");
 if(spawn_hungry_children(mouths,&pipes)){
  return 1;
 }
 if((rnd = open("/dev/urandom",00)) < 0){
  fprintf(stderr,"Coudln't open /dev/urandom for input (%s?)\n",
    strerror((*__errno_location ())));
  return -1;
 }
 if(feed_hungry_children(rnd,mouths,pipes,rate)){
  close(rnd);
  free(pipes);
  return 1;
 }
 close(rnd);
 free(pipes);
 return 0;
}

canonical_original_code:
int main(int argc, char **argv){
 unsigned int mouths = 10;
 unsigned long long rate = 0;
 int c; int *pipes; int rnd;
 while((c = getopt(argc,argv,"r:")) != -1){
  switch(c){
   case 'r':{
    char *e;
    if(rate){
     usage();
     return 1;
    }
    if((rate = strtoull(optarg,&e,0)) == 0 || rate == (0x7fffffffffffffffLL * 2ULL + 1ULL)){
     usage();
     return 1;
    }
    break;}
   case '?':
   default:
    usage();
    return 1;
  }
 }
 if(argv[optind]){
  usage();
  return 1;
 }
 if(rate){
  printf("Ratelimited to %llu message per second\n",rate);
 }else{
  printf("No ratelimit in effect\n");
 }
 printf("Serving %u %s\n",mouths,mouths == 1 ? "child" : "children");
 if(spawn_hungry_children(mouths,&pipes)){
  return 1;
 }
 if((rnd = open("/dev/urandom",00)) < 0){
  fprintf(stderr,"Coudln't open /dev/urandom for input (%s?)\n",
    strerror((*__errno_location ())));
  return -1;
 }
 if(feed_hungry_children(rnd,mouths,pipes,rate)){
  close(rnd);
  free(pipes);
  return 1;
 }
 close(rnd);
 free(pipes);
 return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
mouths
unsigned int

variable_types:
rate
unsigned long long

variable_types: c int

variable_types:
pipes
int *

variable_types: rnd int

variable_types:
e
char *


repo: danking/test-harness

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v4; // [rsp+8h] [rbp-10h] BYREF

yyparse(&v4, argv, envp);
printf("%s", v4);
return 0;
}

original_code:
int
main (void)
{
  char* result;
  yyparse(&result);
  printf("%s", result);
  return 0;
}

canonical_original_code:
int main()
{
  char *result;
  yyparse(&result);
  printf("%s", result);
  return 0;
}

variable_types:
result
char *


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v3; // ebx
double v5; // xmm0_8
float v6; // [rsp+Ch] [rbp-Ch]

v3 = 0;
v6 = 0.0;
if ( *(float *)&buf_len > 0.0 )
{
do
{
if ( (v3 & 7) == 0 )
putchar(10);
v5 = sin((float)((float)((float)(v6 * *(float *)&span) / *(float *)&buf_len) + *(float *)&w0));
printf("%5d", (unsigned int)(offset + (int)((double)peak * v5)));
if ( (float)(*(float *)&buf_len - 1.0) <= v6 )
putchar(10);
else
printf(", ");
v6 = (float)++v3;
}
while ( *(float *)&buf_len > (float)v3 );
}
return 0;
}

original_code:
int main()
{
  int i;
  for (i = 0; i < buf_len; i++) {
    if (i % 8 == 0)
      printf("\n");
    printf("%5d", offset + (int)(peak * sin(w0 + span * i / buf_len)));
    if (i < buf_len - 1)
      printf(", ");
    else
      printf("\n");
  }
  return 0;
}

variable_types: i int


handler

repo: danluu/setjmp-longjmp-ucontext-snippets

decompiled_code:
void __noreturn handler()
{
printf("stack overflow %d\n", (unsigned int)i_0);
longjmp(try, ++i_0);
}

canonical_decompiled_code:
void __noreturn func7()
{
printf("stack overflow %d\n", (unsigned int)i_0);
longjmp(try, ++i_0);
}

original_code:
void handler(int sig) {
  static int i = 0;
  printf("stack overflow %d\n", i);
  longjmp(try, ++i);
  ((void) sizeof ((0) ? 1 : 0), __extension__ ({ if (0) ; else __assert_fail ("0", "sigaltstack.c", 17, __extension__ __PRETTY_FUNCTION__); }));
}

variable_types: sig int


recurse

decompiled_code:
__int64 recurse()
{
return (unsigned int)recurse() + 1;
}

canonical_decompiled_code:
__int64 func8()
{
return (unsigned int)func8() + 1;
}

original_code:
unsigned recurse(unsigned x) {
    return recurse(x)+1;
}

canonical_original_code:
unsigned int recurse(unsigned int x) {
    return recurse(x)+1;
}

variable_types:
x
unsigned int

return_type: unsigned int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
struct sigaction v4; // [rsp+0h] [rbp-C8h] BYREF
struct sigaltstack v5; // [rsp+A0h] [rbp-28h] BYREF

*(_QWORD *)&v5.ss_flags = 0LL;
v5.ss_sp = malloc(0x10000uLL);
v5.ss_size = 0x2000LL;
memset(&v4.sa_mask, 0, 0x90uLL);
v4.sa_handler = (__sighandler_t)handler;
v4.sa_flags = 0x8000000;
sigaltstack(&v5, 0LL);
sigfillset(&v4.sa_mask);
sigaction(14, &v4, 0LL);
raise(14);
return 0;
}

original_code:
int main() {
  char* stack;
  stack = malloc(sizeof(stack) * 8192);
  stack_t ss = {
    .ss_size = 8192,
    .ss_sp = stack,
  };
  struct sigaction sa = {
    .__sigaction_handler.sa_handler = handler,
    .sa_flags = 0x08000000
  };
  sigaltstack(&ss, 0);
  sigfillset(&sa.sa_mask);
  sigaction(14, &sa, 0);
  raise(14);
  return 0;
  if (_setjmp (try) < 3) {
    recurse(0);
  } else {
    printf("caught exception!\n");
  }
  return 0;
}

canonical_original_code:
int main() {
  char *stack;
  stack = malloc(sizeof(stack) * 8192);
  struct stack_t ss = {
    .ss_size = 8192,
    .ss_sp = stack,
  };
  struct sigaction sa = {
    .__sigaction_handler.sa_handler = handler,
    .sa_flags = 0x08000000
  };
  sigaltstack(&ss, 0);
  sigfillset(&sa.sa_mask);
  sigaction(14, &sa, 0);
  raise(14);
  return 0;
  if (_setjmp (try) < 3) {
    recurse(0);
  } else {
    printf("caught exception!\n");
  }
  return 0;
}

variable_types:
stack
char *

variable_types: struct stack_t { void *ss_sp; int ss_flags; unsigned long ss_size; }

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


repo: danni/docker-nss

decompiled_code:
__int64 __fastcall lookup_container_ip(__int64 a1, struct in_addr *a2)
{
const char *v2; // rbp
unsigned int v3; // ebx
int v5; // [rsp+34h] [rbp-24h]
const char *v6; // [rsp+38h] [rbp-20h]

v2 = (const char *)g_strdup();
*strrchr(v2, 46) = 0;
v3 = g_spawn_sync();
if ( v3 )
{
v3 = 0;
if ( !v5 )
{
g_strchug();
v6 = (const char *)g_strchomp();
v3 = inet_aton(v6, a2);
}
}
g_free();
g_free();
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, struct in_addr *a2)
{
const char *v2; // rbp
unsigned int v3; // ebx
int v5; // [rsp+34h] [rbp-24h]
const char *v6; // [rsp+38h] [rbp-20h]

v2 = (const char *)g_strdup();
*strrchr(v2, 46) = 0;
v3 = g_spawn_sync();
if ( v3 )
{
v3 = 0;
if ( !v5 )
{
g_strchug();
v6 = (const char *)g_strchomp();
v3 = inet_aton(v6, a2);
}
}
g_free();
g_free();
return v3;
}

original_code:
static gboolean
lookup_container_ip (const char *name,
        struct in_addr *addr)
{
    char *stdout_s, *name_s;
    int exit_status;
    gboolean success = (0);
    name_s = g_strdup (name);
    *strrchr(name_s, '.') = '\0';
    char *argv[] = {
        "docker",
        "inspect",
        "-format={{.NetworkSettings.IPAddress}}",
        name_s,
        ((void *)0),
    };
    if (!g_spawn_sync(((void *)0),
               argv,
               ((void *)0),
               G_SPAWN_STDERR_TO_DEV_NULL | G_SPAWN_SEARCH_PATH,
               ((void *)0), ((void *)0),
               &stdout_s, ((void *)0),
               &exit_status,
               ((void *)0)))
      {
          goto finally;
      }
    if (exit_status != 0)
      {
        goto finally;
      }
    stdout_s = g_strchomp (g_strchug (stdout_s));
    success = inet_aton (stdout_s, addr);
finally:
    g_free (name_s);
    g_free (stdout_s);
    return success;
}

canonical_original_code:
static int lookup_container_ip(char *name, struct in_addr *addr)
{
    char *stdout_s; char *name_s;
    int exit_status;
    int success = (0);
    name_s = g_strdup (name);
    *strrchr(name_s, '.') = '\0';
    char *argv[] = {
        "docker",
        "inspect",
        "-format={{.NetworkSettings.IPAddress}}",
        name_s,
        ((void *)0),
    };
    if (!g_spawn_sync(((void *)0),
               argv,
               ((void *)0),
               G_SPAWN_STDERR_TO_DEV_NULL | G_SPAWN_SEARCH_PATH,
               ((void *)0), ((void *)0),
               &stdout_s, ((void *)0),
               &exit_status,
               ((void *)0)))
      {
          goto finally;
      }
    if (exit_status != 0)
      {
        goto finally;
      }
    stdout_s = g_strchomp (g_strchug (stdout_s));
    success = inet_aton (stdout_s, addr);
finally:
    g_free (name_s);
    g_free (stdout_s);
    return success;
}

variable_types:
name
char *

variable_types: struct in_addr { unsigned int s_addr; } *

variable_types: success int

variable_types:
argv
char *[]


decompiled_code:
ssize_t __fastcall escribir_bloque(int a1, int a2, const void *a3)
{
return pwrite64(a1, a3, 0x1000uLL, a2 << 12);
}

canonical_decompiled_code:
ssize_t __fastcall func7(int a1, int a2, const void *a3)
{
return pwrite64(a1, a3, 0x1000uLL, a2 << 12);
}

original_code:
int escribir_bloque(int fd, int lba, void *buffer)
{
    return pwrite(fd, buffer, 4096, lba*4096);
}

variable_types: fd int

variable_types: lba int

variable_types:
buffer
void *


decompiled_code:
ssize_t __fastcall leer_bloque(int a1, int a2, void *a3)
{
return pread64(a1, a3, 0x1000uLL, a2 << 12);
}

canonical_decompiled_code:
ssize_t __fastcall func8(int a1, int a2, void *a3)
{
return pread64(a1, a3, 0x1000uLL, a2 << 12);
}

original_code:
int leer_bloque(int fd, int lba, void *buffer)
{
    return pread(fd, buffer, 4096, lba*4096);
}


getSize

decompiled_code:
__int64 __fastcall getSize(char *a1)
{
__int64 v1; // rbx
__int32_t v2; // eax
__int64 v3; // rdx
char *endptr; // [rsp+8h] [rbp-10h] BYREF

endptr = a1;
v1 = strtol(a1, &endptr, 0);
v2 = (*__ctype_toupper_loc())[*endptr];
if ( v2 == 71 )
return v1 << 30;
if ( v2 == 77 )
{
v3 = v1 << 20;
descripcion = 77;
}
else
{
v3 = -1LL;
if ( v2 == 75 )
{
v3 = v1 << 10;
descripcion = 75;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func9(char *a1)
{
__int64 v1; // rbx
__int32_t v2; // eax
__int64 v3; // rdx
char *endptr; // [rsp+8h] [rbp-10h] BYREF

endptr = a1;
v1 = strtol(a1, &endptr, 0);
v2 = (*__ctype_toupper_loc())[*endptr];
if ( v2 == 71 )
return v1 << 30;
if ( v2 == 77 )
{
v3 = v1 << 20;
descripcion = 77;
}
else
{
v3 = -1LL;
if ( v2 == 75 )
{
v3 = v1 << 10;
descripcion = 75;
}
}
return v3;
}

original_code:
unsigned long getSize(char *s)
{
    unsigned long valor = strtol(s, &s, 0);
    if (toupper(*s) == 'G') {
        valor *= (1024 * 1024 * 1024);
        return valor;
    }
    if (toupper(*s) == 'M'){
        valor *= (1024 * 1024);
        if (valor % 128 == 0){
            descripcion = 'M';
            return valor;
        }
    }
    if (toupper(*s) == 'K'){
        valor *= 1024;
        if (valor % 128 == 0){
            descripcion = 'K';
            return valor;
        }
    }
    return -1;
}

variable_types:
s
char *

variable_types:
valor
unsigned long

return_type: unsigned long


mensaje

decompiled_code:
void __noreturn mensaje()
{
puts("uso: mkfs [--crear <tamano (g|m|k)>] <image-file>");
exit(1);
}

canonical_decompiled_code:
void __noreturn func10()
{
puts("uso: mkfs [--crear <tamano (g|m|k)>] <image-file>");
exit(1);
}

original_code:
void mensaje()
{
    printf("uso: mkfs [--crear <tamano (g|m|k)>] <image-file>\n");
    exit(1);
}


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

if ( a2 == 71 )
return (unsigned int)(a1 >> 27);
if ( a2 == 77 )
return (unsigned int)(a1 >> 27);
v2 = a1 << 10 >> 27;
result = 0xFFFFFFFFLL;
if ( a2 == 75 )
result = (unsigned int)v2;
return result;
}

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

if ( a2 == 71 )
return (unsigned int)(a1 >> 27);
if ( a2 == 77 )
return (unsigned int)(a1 >> 27);
v2 = a1 << 10 >> 27;
result = 0xFFFFFFFFLL;
if ( a2 == 75 )
result = (unsigned int)v2;
return result;
}

original_code:
int cantidad_bloques_bits(unsigned long size, char tipo)
{
    unsigned long total_bloques;
    unsigned long tmp;
    int r;
    if (tipo == 'G'){
        tmp = size / 4096;
        tmp = tmp / 8;
        total_bloques = tmp / 4096;
        r = total_bloques;
        return r;
    } else if (tipo == 'M'){
        tmp = size / 4096;
        tmp = tmp / 8;
        total_bloques = tmp / 4096;
        r = total_bloques;
        return r;
    } else if(tipo =='K'){
        total_bloques = size * 1024;
        tmp = total_bloques / 4096;
        tmp = tmp / 8;
        total_bloques = tmp / 4096;
        r = total_bloques;
        return r;
    }
    return -1;
}

variable_types:
size
unsigned long

variable_types: tipo char

variable_types:
tmp
unsigned long

variable_types: r int