decompiled_code:
__int64 __fastcall ndn_uri_append(__int64 a1, __int64 a2, __int64 a3, char a4)
{
__int64 v5; // rbx
int v6; // er13
int v7; // er12
unsigned __int8 *v9; // [rsp+0h] [rbp-88h] BYREF
unsigned __int64 v10; // [rsp+8h] [rbp-80h] BYREF
char v11[120]; // [rsp+10h] [rbp-78h] BYREF

v9 = 0LL;
v10 = 0LL;
v5 = ndn_buf_decoder_start(v11, a2, a3);
if ( (unsigned int)ndn_buf_match_dtag(v5, 26LL) || (unsigned int)ndn_buf_match_dtag(v5, 64LL) )
{
ndn_buf_advance(v5);
if ( (unsigned int)ndn_buf_match_dtag(v5, 37LL) )
ndn_buf_advance_past_element(v5);
}
if ( (unsigned int)ndn_buf_match_dtag(v5, 14LL) )
{
if ( (a4 & 1) != 0 )
ndn_charbuf_append_string(a1, "ndn:");
v6 = 0;
ndn_buf_advance(v5);
while ( (unsigned int)ndn_buf_match_dtag(v5, 15LL) )
{
ndn_buf_advance(v5);
v10 = 0LL;
if ( (unsigned int)ndn_buf_match_blob(v5, &v9, &v10) )
ndn_buf_advance(v5);
ndn_buf_check_close(v5);
v7 = *(_DWORD *)(v5 + 8);
if ( v7 < 0 )
return (unsigned int)v7;
++v6;
ndn_charbuf_append(a1, "/", 1LL);
if ( (a4 & 6) == 0 )
a4 |= 4u;
if ( (a4 & 2) != 0 )
{
ndn_uri_append_mixedescaped(a1, v9, v10);
}
else if ( (a4 & 4) != 0 )
{
ndn_uri_append_percentescaped(a1, (__int64)v9, v10);
}
}
ndn_buf_check_close(v5);
v7 = *(_DWORD *)(v5 + 8);
if ( v7 >= 0 )
{
v7 = v6;
if ( !v6 )
ndn_charbuf_append(a1, "/", 1LL);
}
}
else
{
v7 = -1;
}
return (unsigned int)v7;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2, __int64 a3, char a4)
{
__int64 v5; // rbx
int v6; // er13
int v7; // er12
unsigned __int8 *v9; // [rsp+0h] [rbp-88h] BYREF
unsigned __int64 v10; // [rsp+8h] [rbp-80h] BYREF
char v11[120]; // [rsp+10h] [rbp-78h] BYREF

v9 = 0LL;
v10 = 0LL;
v5 = ndn_buf_decoder_start(v11, a2, a3);
if ( (unsigned int)ndn_buf_match_dtag(v5, 26LL) || (unsigned int)ndn_buf_match_dtag(v5, 64LL) )
{
ndn_buf_advance(v5);
if ( (unsigned int)ndn_buf_match_dtag(v5, 37LL) )
ndn_buf_advance_past_element(v5);
}
if ( (unsigned int)ndn_buf_match_dtag(v5, 14LL) )
{
if ( (a4 & 1) != 0 )
ndn_charbuf_append_string(a1, "ndn:");
v6 = 0;
ndn_buf_advance(v5);
while ( (unsigned int)ndn_buf_match_dtag(v5, 15LL) )
{
ndn_buf_advance(v5);
v10 = 0LL;
if ( (unsigned int)ndn_buf_match_blob(v5, &v9, &v10) )
ndn_buf_advance(v5);
ndn_buf_check_close(v5);
v7 = *(_DWORD *)(v5 + 8);
if ( v7 < 0 )
return (unsigned int)v7;
++v6;
ndn_charbuf_append(a1, "/", 1LL);
if ( (a4 & 6) == 0 )
a4 |= 4u;
if ( (a4 & 2) != 0 )
{
func5(a1, v9, v10);
}
else if ( (a4 & 4) != 0 )
{
func4(a1, (__int64)v9, v10);
}
}
ndn_buf_check_close(v5);
v7 = *(_DWORD *)(v5 + 8);
if ( v7 >= 0 )
{
v7 = v6;
if ( !v6 )
ndn_charbuf_append(a1, "/", 1LL);
}
}
else
{
v7 = -1;
}
return (unsigned int)v7;
}

original_code:
int
ndn_uri_append(struct ndn_charbuf *c,
               const unsigned char *ndnb,
               size_t size,
               int flags)
{
    int ncomp = 0;
    const unsigned char *comp = ((void *)0);
    size_t compsize = 0;
    struct ndn_buf_decoder decoder;
    struct ndn_buf_decoder *d = ndn_buf_decoder_start(&decoder, ndnb, size);
    if (ndn_buf_match_dtag(d, NDN_DTAG_Interest) ||
        ndn_buf_match_dtag(d, NDN_DTAG_ContentObject)) {
        ndn_buf_advance(d);
        if (ndn_buf_match_dtag(d, NDN_DTAG_Signature))
            ndn_buf_advance_past_element(d);
    }
    if (!ndn_buf_match_dtag(d, NDN_DTAG_Name))
        return(-1);
    if (flags & 1)
        ndn_charbuf_append_string(c, "ndn:");
    ndn_buf_advance(d);
    while (ndn_buf_match_dtag(d, NDN_DTAG_Component)) {
        ndn_buf_advance(d);
        compsize = 0;
        if (ndn_buf_match_blob(d, &comp, &compsize))
            ndn_buf_advance(d);
        ndn_buf_check_close(d);
        if (d->decoder.state < 0)
            return(d->decoder.state);
        ncomp += 1;
        ndn_charbuf_append(c, "/", 1);
        if ((flags & (2|4)) == 0)
            flags |= 4;
        if (flags & 2) {
            ndn_uri_append_mixedescaped(c, comp, compsize);
        } else if (flags & 4)
            ndn_uri_append_percentescaped(c, comp, compsize);
    }
    ndn_buf_check_close(d);
    if (d->decoder.state < 0)
        return (d->decoder.state);
    if (ncomp == 0)
        ndn_charbuf_append(c, "/", 1);
    return(ncomp);
}

canonical_original_code:
int ndn_uri_append(struct ndn_charbuf *c, unsigned char *ndnb, unsigned long size, int flags)
{
    int ncomp = 0;
    unsigned char *comp = ((void *)0);
    unsigned long compsize = 0;
    struct ndn_buf_decoder decoder;
    struct ndn_buf_decoder *d = ndn_buf_decoder_start(&decoder, ndnb, size);
    if (ndn_buf_match_dtag(d, NDN_DTAG_Interest) ||
        ndn_buf_match_dtag(d, NDN_DTAG_ContentObject)) {
        ndn_buf_advance(d);
        if (ndn_buf_match_dtag(d, NDN_DTAG_Signature))
            ndn_buf_advance_past_element(d);
    }
    if (!ndn_buf_match_dtag(d, NDN_DTAG_Name))
        return(-1);
    if (flags & 1)
        ndn_charbuf_append_string(c, "ndn:");
    ndn_buf_advance(d);
    while (ndn_buf_match_dtag(d, NDN_DTAG_Component)) {
        ndn_buf_advance(d);
        compsize = 0;
        if (ndn_buf_match_blob(d, &comp, &compsize))
            ndn_buf_advance(d);
        ndn_buf_check_close(d);
        if (d->decoder.state < 0)
            return(d->decoder.state);
        ncomp += 1;
        ndn_charbuf_append(c, "/", 1);
        if ((flags & (2|4)) == 0)
            flags |= 4;
        if (flags & 2) {
            ndn_uri_append_mixedescaped(c, comp, compsize);
        } else if (flags & 4)
            ndn_uri_append_percentescaped(c, comp, compsize);
    }
    ndn_buf_check_close(d);
    if (d->decoder.state < 0)
        return (d->decoder.state);
    if (ncomp == 0)
        ndn_charbuf_append(c, "/", 1);
    return(ncomp);
}

variable_types: flags int

variable_types: ncomp int

variable_types:
comp
unsigned char *

variable_types:
compsize
unsigned long

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; }


repo: danihegglin/ChronosAndroid

decompiled_code:
__int64 __fastcall ndn_name_from_uri(__int64 *a1, const char *a2)
{
int v2; // er12
unsigned __int64 v4; // r15
const char *v5; // r14
unsigned __int64 *v6; // rax
const char *v7; // rbx
int v8; // er13
unsigned __int64 v9; // rax
unsigned __int64 v11; // r15
unsigned __int64 v12; // rdx
unsigned __int64 *v13; // rdi
__int64 v14; // rsi
int v15; // eax
unsigned __int64 v16; // rcx
int v17; // eax
unsigned __int64 *v18; // rdi
__int64 v19; // rbx
int v20; // eax
unsigned __int64 *v21; // [rsp+0h] [rbp-48h] BYREF
unsigned __int64 v22[8]; // [rsp+8h] [rbp-40h] BYREF

v2 = (int)a2;
v22[0] = 0LL;
v4 = strlen(a2);
v5 = &a2[v4];
v6 = (unsigned __int64 *)ndn_charbuf_create(a2);
v21 = v6;
if ( !v6 )
return 0xFFFFFFFFLL;
if ( *a2 == 47 )
{
v7 = a2;
v8 = 0;
goto LABEL_4;
}
v8 = ndn_append_uri_component(v6, (__int64)a2, v4, v22);
if ( v8 < -2 )
goto LABEL_6;
v7 = a2;
*(_BYTE *)ndn_charbuf_reserve(v21, 1LL) = 0;
v11 = v22[0];
if ( a2[v22[0] - 1] == 58 )
{
if ( strcasecmp((const char *)v21[2], "ndn:") )
return 0xFFFFFFFFLL;
v22[0] = 0LL;
v7 = &a2[v11];
}
v9 = *(unsigned __int8 *)v7;
if ( (_BYTE)v9 == 47 )
{
LABEL_4:
ndn_name_init(a1);
if ( v7[1] != 47 )
{
LABEL_5:
v9 = *(unsigned __int8 *)v7;
goto LABEL_11;
}
v18 = v21;
v19 = (__int64)(v7 + 2);
*v21 = 0LL;
v20 = ndn_append_uri_component(v18, v19, (unsigned __int64)&v5[-v19], v22);
v8 = v20;
if ( v20 >= 0 || v20 == -2 )
{
v7 = (const char *)(v22[0] + v19);
v22[0] = 0LL;
goto LABEL_5;
}
LABEL_6:
ndn_charbuf_destroy(&v21);
return 0xFFFFFFFFLL;
}
LABEL_11:
if ( (unsigned __int8)v9 > 0x3Fu || (v12 = 0x8000000800000001LL, !_bittest64((const __int64 *)&v12, v9)) )
{
while ( 1 )
{
v13 = v21;
*v21 = 0LL;
v14 = (__int64)&v7[(_BYTE)v9 == 47];
v15 = ndn_append_uri_component(v13, v14, (unsigned __int64)&v5[-v14], v22);
v7 = (const char *)(v22[0] + v14);
v22[0] = 0LL;
if ( v15 < -2 )
goto LABEL_6;
if ( v15 != -2 )
{
if ( v15 == -1 )
{
v17 = ndn_name_last_component_offset(a1[2], *a1);
if ( v17 < 0 )
goto LABEL_6;
*a1 = v17;
ndn_charbuf_append_closer(a1);
}
else if ( (int)ndn_name_append(a1, v21[2], *v21) < 0 )
{
goto LABEL_6;
}
}
v9 = *(unsigned __int8 *)v7;
if ( (unsigned __int8)v9 <= 0x3Fu )
{
v16 = 0x8000000800000001LL;
if ( _bittest64((const __int64 *)&v16, v9) )
{
ndn_charbuf_destroy(&v21);
goto LABEL_23;
}
}
}
}
ndn_charbuf_destroy(&v21);
if ( v8 < 0 )
return 0xFFFFFFFFLL;
LABEL_23:
if ( (unsigned __int64)*a1 <= 1 || *(_BYTE *)(a1[2] + *a1 - 1) )
return 0xFFFFFFFFLL;
return (unsigned int)((_DWORD)v7 - v2);
}

canonical_decompiled_code:
__int64 __fastcall func7(__int64 *a1, const char *a2)
{
int v2; // er12
unsigned __int64 v4; // r15
const char *v5; // r14
unsigned __int64 *v6; // rax
const char *v7; // rbx
int v8; // er13
unsigned __int64 v9; // rax
unsigned __int64 v11; // r15
unsigned __int64 v12; // rdx
unsigned __int64 *v13; // rdi
__int64 v14; // rsi
int v15; // eax
unsigned __int64 v16; // rcx
int v17; // eax
unsigned __int64 *v18; // rdi
__int64 v19; // rbx
int v20; // eax
unsigned __int64 *v21; // [rsp+0h] [rbp-48h] BYREF
unsigned __int64 v22[8]; // [rsp+8h] [rbp-40h] BYREF

v2 = (int)a2;
v22[0] = 0LL;
v4 = strlen(a2);
v5 = &a2[v4];
v6 = (unsigned __int64 *)ndn_charbuf_create(a2);
v21 = v6;
if ( !v6 )
return 0xFFFFFFFFLL;
if ( *a2 == 47 )
{
v7 = a2;
v8 = 0;
goto LABEL_4;
}
v8 = func2(v6, (__int64)a2, v4, v22);
if ( v8 < -2 )
goto LABEL_6;
v7 = a2;
*(_BYTE *)ndn_charbuf_reserve(v21, 1LL) = 0;
v11 = v22[0];
if ( a2[v22[0] - 1] == 58 )
{
if ( strcasecmp((const char *)v21[2], "ndn:") )
return 0xFFFFFFFFLL;
v22[0] = 0LL;
v7 = &a2[v11];
}
v9 = *(unsigned __int8 *)v7;
if ( (_BYTE)v9 == 47 )
{
LABEL_4:
ndn_name_init(a1);
if ( v7[1] != 47 )
{
LABEL_5:
v9 = *(unsigned __int8 *)v7;
goto LABEL_11;
}
v18 = v21;
v19 = (__int64)(v7 + 2);
*v21 = 0LL;
v20 = func2(v18, v19, (unsigned __int64)&v5[-v19], v22);
v8 = v20;
if ( v20 >= 0 || v20 == -2 )
{
v7 = (const char *)(v22[0] + v19);
v22[0] = 0LL;
goto LABEL_5;
}
LABEL_6:
ndn_charbuf_destroy(&v21);
return 0xFFFFFFFFLL;
}
LABEL_11:
if ( (unsigned __int8)v9 > 0x3Fu || (v12 = 0x8000000800000001LL, !_bittest64((const __int64 *)&v12, v9)) )
{
while ( 1 )
{
v13 = v21;
*v21 = 0LL;
v14 = (__int64)&v7[(_BYTE)v9 == 47];
v15 = func2(v13, v14, (unsigned __int64)&v5[-v14], v22);
v7 = (const char *)(v22[0] + v14);
v22[0] = 0LL;
if ( v15 < -2 )
goto LABEL_6;
if ( v15 != -2 )
{
if ( v15 == -1 )
{
v17 = func3(a1[2], *a1);
if ( v17 < 0 )
goto LABEL_6;
*a1 = v17;
ndn_charbuf_append_closer(a1);
}
else if ( (int)ndn_name_append(a1, v21[2], *v21) < 0 )
{
goto LABEL_6;
}
}
v9 = *(unsigned __int8 *)v7;
if ( (unsigned __int8)v9 <= 0x3Fu )
{
v16 = 0x8000000800000001LL;
if ( _bittest64((const __int64 *)&v16, v9) )
{
ndn_charbuf_destroy(&v21);
goto LABEL_23;
}
}
}
}
ndn_charbuf_destroy(&v21);
if ( v8 < 0 )
return 0xFFFFFFFFLL;
LABEL_23:
if ( (unsigned __int64)*a1 <= 1 || *(_BYTE *)(a1[2] + *a1 - 1) )
return 0xFFFFFFFFLL;
return (unsigned int)((_DWORD)v7 - v2);
}

original_code:
int
ndn_name_from_uri(struct ndn_charbuf *c, const char *uri)
{
    int res = 0;
    struct ndn_charbuf *compbuf = ((void *)0);
    const char *stop = uri + strlen(uri);
    const char *s = uri;
    size_t cont = 0;
    compbuf = ndn_charbuf_create();
    if (compbuf == ((void *)0)) return(-1);
    if (s[0] != '/') {
        res = ndn_append_uri_component(compbuf, s, stop - s, &cont);
        if (res < -2)
            goto Done;
        ndn_charbuf_reserve(compbuf, 1)[0] = 0;
        if (s[cont-1] == ':') {
            if ((0 == strcasecmp((const char *)(compbuf->buf), "ndn:") ||
                 0 == strcasecmp((const char *)(compbuf->buf), "ndn:"))) {
                s += cont;
                cont = 0;
            } else
                return (-1);
        }
    }
    if (s[0] == '/') {
        ndn_name_init(c);
        if (s[1] == '/') {
            s += 2;
            compbuf->length = 0;
            res = ndn_append_uri_component(compbuf, s, stop - s, &cont);
            if (res < 0 && res != -2)
                goto Done;
            s += cont; cont = 0;
        }
    }
    while (s[0] != 0 && s[0] != '?' && s[0] != '#') {
        if (s[0] == '/')
            s++;
        compbuf->length = 0;
        res = ndn_append_uri_component(compbuf, s, stop - s, &cont);
        s += cont; cont = 0;
        if (res < -2)
            goto Done;
        if (res == -2) {
            res = 0;
            continue;
        }
        if (res == -1) {
            res = ndn_name_last_component_offset(c->buf, c->length);
            if (res < 0)
                goto Done;
            c->length = res;
            ndn_charbuf_append_closer(c);
            continue;
        }
        res = ndn_name_append(c, compbuf->buf, compbuf->length);
        if (res < 0)
            goto Done;
    }
Done:
    ndn_charbuf_destroy(&compbuf);
    if (res < 0)
        return(-1);
    if (c->length < 2 || c->buf[c->length-1] != ((unsigned char)(0)))
        return(-1);
    return(s - uri);
}

canonical_original_code:
int ndn_name_from_uri(struct ndn_charbuf *c, char *uri)
{
    int res = 0;
    struct ndn_charbuf *compbuf = ((void *)0);
    char *stop = uri + strlen(uri);
    char *s = uri;
    unsigned long cont = 0;
    compbuf = ndn_charbuf_create();
    if (compbuf == ((void *)0)) return(-1);
    if (s[0] != '/') {
        res = ndn_append_uri_component(compbuf, s, stop - s, &cont);
        if (res < -2)
            goto Done;
        ndn_charbuf_reserve(compbuf, 1)[0] = 0;
        if (s[cont-1] == ':') {
            if ((0 == strcasecmp((char *)(compbuf->buf), "ndn:") ||
                 0 == strcasecmp((char *)(compbuf->buf), "ndn:"))) {
                s += cont;
                cont = 0;
            } else
                return (-1);
        }
    }
    if (s[0] == '/') {
        ndn_name_init(c);
        if (s[1] == '/') {
            s += 2;
            compbuf->length = 0;
            res = ndn_append_uri_component(compbuf, s, stop - s, &cont);
            if (res < 0 && res != -2)
                goto Done;
            s += cont; cont = 0;
        }
    }
    while (s[0] != 0 && s[0] != '?' && s[0] != '#') {
        if (s[0] == '/')
            s++;
        compbuf->length = 0;
        res = ndn_append_uri_component(compbuf, s, stop - s, &cont);
        s += cont; cont = 0;
        if (res < -2)
            goto Done;
        if (res == -2) {
            res = 0;
            continue;
        }
        if (res == -1) {
            res = ndn_name_last_component_offset(c->buf, c->length);
            if (res < 0)
                goto Done;
            c->length = res;
            ndn_charbuf_append_closer(c);
            continue;
        }
        res = ndn_name_append(c, compbuf->buf, compbuf->length);
        if (res < 0)
            goto Done;
    }
Done:
    ndn_charbuf_destroy(&compbuf);
    if (res < 0)
        return(-1);
    if (c->length < 2 || c->buf[c->length-1] != ((unsigned char)(0)))
        return(-1);
    return(s - uri);
}

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

variable_types:
uri
char *

variable_types: res int

variable_types:
stop
char *

variable_types:
s
char *

variable_types:
cont
unsigned long

return_type: int


takedown

decompiled_code:
__int64 __fastcall takedown(signed int a1, signed int a2)
{
if ( a2 > 0 )
{
fillout_isra_0(0x20u, a2);
fillout_isra_0(8u, a2);
}
if ( a1 > 0 )
{
fillout_isra_0(8u, a1);
fillout_isra_0(0x20u, a1);
fillout_isra_0(8u, a1);
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(signed int a1, signed int a2)
{
if ( a2 > 0 )
{
func1(0x20u, a2);
func1(8u, a2);
}
if ( a1 > 0 )
{
func1(8u, a1);
func1(0x20u, a1);
func1(8u, a1);
}
return 0LL;
}

original_code:
static int
takedown(int n, int r)
{
    if (r > 0) {
        fillout(' ', r);
        fillout('\b', r);
    }
    if (n > 0) {
        fillout('\b', n);
        fillout(' ', n);
        fillout('\b', n);
    }
    return(0);
}

variable_types: n int

variable_types: r int


decompiled_code:
__int64 __fastcall lned_run(unsigned int a1, __int64 a2, const char *a3, __int64 (__fastcall *a4)(_QWORD, __int64))
{
int v5; // ebx
struct termios *v6; // r15
__m128i v7; // xmm0
__m128i v8; // xmm1
__m128i v9; // xmm2
_DWORD stat_loc[3]; // [rsp+1Ch] [rbp-13Ch] BYREF
char buf[8]; // [rsp+28h] [rbp-130h] BYREF
struct termios termios_p[4]; // [rsp+30h] [rbp-128h] BYREF

v5 = 0;
*(_QWORD *)&stat_loc[1] = -1LL;
v6 = termios_p;
memset(termios_p, 0, sizeof(termios_p));
do
{
if ( tcgetattr(v5, v6) < 0 && v5 <= 1 )
return a4(a1, a2);
++v5;
++v6;
}
while ( v5 != 3 );
if ( socketpair(1, 1, 0, &stat_loc[1]) < 0 )
return a4(a1, a2);
v7 = _mm_loadu_si128((const __m128i *)termios_p);
v8 = _mm_loadu_si128((const __m128i *)&termios_p[0].c_line);
v9 = _mm_loadu_si128((const __m128i *)&termios_p[0].c_cc[15]);
*(_QWORD *)&termios_p[3].c_cc[31] = *(_QWORD *)&termios_p[0].c_cc[31];
*(__m128i *)&termios_p[3].c_iflag = v7;
termios_p[3].c_ospeed = termios_p[0].c_ospeed;
termios_p[3].c_lflag = v7.m128i_i32[3] & 0xFFFFFFF5;
*(__m128i *)&termios_p[3].c_line = v8;
*(__m128i *)&termios_p[3].c_cc[15] = v9;
*(_WORD *)&termios_p[3].c_cc[5] = 256;
if ( tcsetattr(0, 0, &termios_p[3]) < 0 )
return a4(a1, a2);
if ( !fork() )
{
dup2(stat_loc[2], 0);
dup2(stat_loc[2], 1);
if ( isatty(2) )
dup2(stat_loc[2], 2);
close(stat_loc[1]);
close(stat_loc[2]);
return a4(a1, a2);
}
close(stat_loc[2]);
dup2(1, 2);
shuttle_isra_0(stat_loc[1], a3);
shutdown(stat_loc[1], 1);
while ( read(stat_loc[1], buf, 1uLL) == 1 )
write(1, buf, 1uLL);
wait((__WAIT_STATUS)stat_loc);
tcsetattr(0, 0, termios_p);
return stat_loc[0];
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1, __int64 a2, const char *a3, __int64 (__fastcall *a4)(_QWORD, __int64))
{
int v5; // ebx
struct termios *v6; // r15
__m128i v7; // xmm0
__m128i v8; // xmm1
__m128i v9; // xmm2
_DWORD stat_loc[3]; // [rsp+1Ch] [rbp-13Ch] BYREF
char buf[8]; // [rsp+28h] [rbp-130h] BYREF
struct termios termios_p[4]; // [rsp+30h] [rbp-128h] BYREF

v5 = 0;
*(_QWORD *)&stat_loc[1] = -1LL;
v6 = termios_p;
memset(termios_p, 0, sizeof(termios_p));
do
{
if ( tcgetattr(v5, v6) < 0 && v5 <= 1 )
return a4(a1, a2);
++v5;
++v6;
}
while ( v5 != 3 );
if ( socketpair(1, 1, 0, &stat_loc[1]) < 0 )
return a4(a1, a2);
v7 = _mm_loadu_si128((const __m128i *)termios_p);
v8 = _mm_loadu_si128((const __m128i *)&termios_p[0].c_line);
v9 = _mm_loadu_si128((const __m128i *)&termios_p[0].c_cc[15]);
*(_QWORD *)&termios_p[3].c_cc[31] = *(_QWORD *)&termios_p[0].c_cc[31];
*(__m128i *)&termios_p[3].c_iflag = v7;
termios_p[3].c_ospeed = termios_p[0].c_ospeed;
termios_p[3].c_lflag = v7.m128i_i32[3] & 0xFFFFFFF5;
*(__m128i *)&termios_p[3].c_line = v8;
*(__m128i *)&termios_p[3].c_cc[15] = v9;
*(_WORD *)&termios_p[3].c_cc[5] = 256;
if ( tcsetattr(0, 0, &termios_p[3]) < 0 )
return a4(a1, a2);
if ( !fork() )
{
dup2(stat_loc[2], 0);
dup2(stat_loc[2], 1);
if ( isatty(2) )
dup2(stat_loc[2], 2);
close(stat_loc[1]);
close(stat_loc[2]);
return a4(a1, a2);
}
close(stat_loc[2]);
dup2(1, 2);
shuttle_isra_0(stat_loc[1], a3);
shutdown(stat_loc[1], 1);
while ( read(stat_loc[1], buf, 1uLL) == 1 )
write(1, buf, 1uLL);
wait((__WAIT_STATUS)stat_loc);
tcsetattr(0, 0, termios_p);
return stat_loc[0];
}

original_code:
int
lned_run(int argc, char** argv, const char *prompt, int (*worker)(int, char**))
{
    struct termios tc[4];
    struct termios *t;
    char cb[8];
    int sp[2] = {-1, -1};
    int i;
    int res;
    int st;
    pid_t pid;
    memset(tc, 0, sizeof(tc));
    for (i = 0; i < 3; i++) {
        res = tcgetattr(i, &(tc[i]));
        if (res < 0 && i < 2)
            goto Direct;
    }
    res = socketpair(1, SOCK_STREAM, 0, sp);
    if (res < 0) goto Direct;
    t = &(tc[3]);
    *t = tc[0];
    t->c_lflag &= ~(0000010 | 0000002);
    t->c_cc[6] = 1;
    t->c_cc[5] = 0;
    res = tcsetattr(0, 0, t);
    if (res < 0) goto Direct;
    pid = fork();
    if (pid == 0) {
        dup2(sp[1], 0);
        dup2(sp[1], 1);
        if (isatty(2))
            dup2(sp[1], 2);
        close(sp[0]);
        close(sp[1]);
        goto Direct;
    }
    close(sp[1]);
    dup2(1, 2);
    shuttle(sp[0], prompt);
    shutdown(sp[0], SHUT_WR);
    while (read(sp[0], cb, 1) == 1)
        write(1, cb, 1);
    wait(&st);
    tcsetattr(0, 0, &(tc[0]));
    return(st);
Direct:
    return(worker(argc, argv));
}

canonical_original_code:
int lned_run(int argc, char **argv, char *prompt, int (*worker)(int, char **))
{
    struct termios tc[4];
    struct termios *t;
    char cb[8];
    int sp[2] = {-1, -1};
    int i;
    int res;
    int st;
    int pid;
    memset(tc, 0, sizeof(tc));
    for (i = 0; i < 3; i++) {
        res = tcgetattr(i, &(tc[i]));
        if (res < 0 && i < 2)
            goto Direct;
    }
    res = socketpair(1, SOCK_STREAM, 0, sp);
    if (res < 0) goto Direct;
    t = &(tc[3]);
    *t = tc[0];
    t->c_lflag &= ~(0000010 | 0000002);
    t->c_cc[6] = 1;
    t->c_cc[5] = 0;
    res = tcsetattr(0, 0, t);
    if (res < 0) goto Direct;
    pid = fork();
    if (pid == 0) {
        dup2(sp[1], 0);
        dup2(sp[1], 1);
        if (isatty(2))
            dup2(sp[1], 2);
        close(sp[0]);
        close(sp[1]);
        goto Direct;
    }
    close(sp[1]);
    dup2(1, 2);
    shuttle(sp[0], prompt);
    shutdown(sp[0], SHUT_WR);
    while (read(sp[0], cb, 1) == 1)
        write(1, cb, 1);
    wait(&st);
    tcsetattr(0, 0, &(tc[0]));
    return(st);
Direct:
    return(worker(argc, argv));
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
prompt
char *

variable_types:
worker
int (*)(int, char **)

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

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

variable_types:
cb
char [8]

variable_types:
sp
int [2]

variable_types: i int

variable_types: st int

variable_types: pid int


decompiled_code:
_BOOL8 __fastcall ndn_excluded(__int64 a1, __int64 a2, const void *a3, size_t a4)
{
__int64 v6; // rbx
bool v7; // zf
int v8; // eax
_BOOL8 result; // rax
__int64 v10; // rdi
__int16 v11; // [rsp+Eh] [rbp-8Ah] BYREF
__int16 *v12; // [rsp+10h] [rbp-88h] BYREF
__int64 v13; // [rsp+18h] [rbp-80h] BYREF
void *s1; // [rsp+20h] [rbp-78h] BYREF
unsigned __int64 v15; // [rsp+28h] [rbp-70h] BYREF
char v16[104]; // [rsp+30h] [rbp-68h] BYREF

v11 = 45;
v12 = 0LL;
v13 = 0LL;
s1 = 0LL;
v15 = 0LL;
v6 = ndn_buf_decoder_start(v16, a1, a2);
if ( !(unsigned int)ndn_buf_match_dtag(v6, 43LL) )
ndn_excluded_cold();
ndn_buf_advance(v6);
if ( (unsigned int)ndn_buf_match_dtag(v6, 13LL) )
{
ndn_buf_advance(v6);
v12 = &v11;
ndn_buf_check_close(v6);
}
else if ( (unsigned int)ndn_buf_match_dtag(v6, 44LL) )
{
ndn_buf_advance(v6);
if ( (unsigned int)ndn_buf_match_blob(v6, &v12, &v13) )
ndn_buf_advance(v6);
ndn_buf_check_close(v6);
}
while ( (unsigned int)ndn_buf_match_dtag(v6, 15LL) )
{
ndn_buf_advance(v6);
v15 = 0LL;
if ( (unsigned int)ndn_buf_match_blob(v6, &s1, &v15) )
{
ndn_buf_advance(v6);
ndn_buf_check_close(v6);
v7 = v15 == a4;
if ( v15 > a4 )
break;
}
else
{
ndn_buf_check_close(v6);
v7 = v15 == a4;
if ( v15 > a4 )
break;
}
if ( v7 )
{
v8 = memcmp(s1, a3, a4);
if ( !v8 )
return 1LL;
if ( v8 > 0 )
break;
}
v12 = 0LL;
v13 = 0LL;
if ( (unsigned int)ndn_buf_match_dtag(v6, 13LL) )
{
ndn_buf_advance(v6);
v12 = &v11;
ndn_buf_check_close(v6);
}
else if ( (unsigned int)ndn_buf_match_dtag(v6, 44LL) )
{
ndn_buf_advance(v6);
if ( (unsigned int)ndn_buf_match_blob(v6, &v12, &v13) )
ndn_buf_advance(v6);
ndn_buf_check_close(v6);
}
}
if ( v12 != &v11 )
{
result = 0LL;
if ( !v13 )
return result;
v10 = ndn_bloom_validate_wire();
if ( v10 )
return (unsigned int)ndn_bloom_match_wire(v10, a3, a4) != 0;
}
return 1LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func2(__int64 a1, __int64 a2, const void *a3, size_t a4)
{
__int64 v6; // rbx
bool v7; // zf
int v8; // eax
_BOOL8 result; // rax
__int64 v10; // rdi
__int16 v11; // [rsp+Eh] [rbp-8Ah] BYREF
__int16 *v12; // [rsp+10h] [rbp-88h] BYREF
__int64 v13; // [rsp+18h] [rbp-80h] BYREF
void *s1; // [rsp+20h] [rbp-78h] BYREF
unsigned __int64 v15; // [rsp+28h] [rbp-70h] BYREF
char v16[104]; // [rsp+30h] [rbp-68h] BYREF

v11 = 45;
v12 = 0LL;
v13 = 0LL;
s1 = 0LL;
v15 = 0LL;
v6 = ndn_buf_decoder_start(v16, a1, a2);
if ( !(unsigned int)ndn_buf_match_dtag(v6, 43LL) )
func5();
ndn_buf_advance(v6);
if ( (unsigned int)ndn_buf_match_dtag(v6, 13LL) )
{
ndn_buf_advance(v6);
v12 = &v11;
ndn_buf_check_close(v6);
}
else if ( (unsigned int)ndn_buf_match_dtag(v6, 44LL) )
{
ndn_buf_advance(v6);
if ( (unsigned int)ndn_buf_match_blob(v6, &v12, &v13) )
ndn_buf_advance(v6);
ndn_buf_check_close(v6);
}
while ( (unsigned int)ndn_buf_match_dtag(v6, 15LL) )
{
ndn_buf_advance(v6);
v15 = 0LL;
if ( (unsigned int)ndn_buf_match_blob(v6, &s1, &v15) )
{
ndn_buf_advance(v6);
ndn_buf_check_close(v6);
v7 = v15 == a4;
if ( v15 > a4 )
break;
}
else
{
ndn_buf_check_close(v6);
v7 = v15 == a4;
if ( v15 > a4 )
break;
}
if ( v7 )
{
v8 = memcmp(s1, a3, a4);
if ( !v8 )
return 1LL;
if ( v8 > 0 )
break;
}
v12 = 0LL;
v13 = 0LL;
if ( (unsigned int)ndn_buf_match_dtag(v6, 13LL) )
{
ndn_buf_advance(v6);
v12 = &v11;
ndn_buf_check_close(v6);
}
else if ( (unsigned int)ndn_buf_match_dtag(v6, 44LL) )
{
ndn_buf_advance(v6);
if ( (unsigned int)ndn_buf_match_blob(v6, &v12, &v13) )
ndn_buf_advance(v6);
ndn_buf_check_close(v6);
}
}
if ( v12 != &v11 )
{
result = 0LL;
if ( !v13 )
return result;
v10 = ndn_bloom_validate_wire();
if ( v10 )
return (unsigned int)ndn_bloom_match_wire(v10, a3, a4) != 0;
}
return 1LL;
}

original_code:
int
ndn_excluded(const unsigned char *excl,
             size_t excl_size,
             const unsigned char *nextcomp,
             size_t nextcomp_size)
{
    unsigned char match_any[2] = "-";
    const unsigned char *bloom = ((void *)0);
    size_t bloom_size = 0;
    struct ndn_buf_decoder decoder;
    struct ndn_buf_decoder *d = ndn_buf_decoder_start(&decoder, excl, excl_size);
    const unsigned char *comp = ((void *)0);
    size_t comp_size = 0;
    const int excluded = 1;
    if (!ndn_buf_match_dtag(d, NDN_DTAG_Exclude))
        abort();
    ndn_buf_advance(d);
    if (ndn_buf_match_dtag(d, NDN_DTAG_Any)) {
        ndn_buf_advance(d);
        bloom = match_any;
        ndn_buf_check_close(d);
    }
    else if (ndn_buf_match_dtag(d, NDN_DTAG_Bloom)) {
        ndn_buf_advance(d);
        if (ndn_buf_match_blob(d, &bloom, &bloom_size))
            ndn_buf_advance(d);
        ndn_buf_check_close(d);
    }
    while (ndn_buf_match_dtag(d, NDN_DTAG_Component)) {
        ndn_buf_advance(d);
        comp_size = 0;
        if (ndn_buf_match_blob(d, &comp, &comp_size))
            ndn_buf_advance(d);
        ndn_buf_check_close(d);
        if (comp_size > nextcomp_size)
            break;
        if (comp_size == nextcomp_size) {
            int res = memcmp(comp, nextcomp, comp_size);
            if (res == 0)
                return(excluded);
            if (res > 0)
                break;
        }
        bloom = ((void *)0);
        bloom_size = 0;
        if (ndn_buf_match_dtag(d, NDN_DTAG_Any)) {
            ndn_buf_advance(d);
            bloom = match_any;
            ndn_buf_check_close(d);
        }
        else if (ndn_buf_match_dtag(d, NDN_DTAG_Bloom)) {
            ndn_buf_advance(d);
            if (ndn_buf_match_blob(d, &bloom, &bloom_size))
                ndn_buf_advance(d);
            ndn_buf_check_close(d);
        }
    }
    if (bloom == match_any)
        return(excluded);
    else if (bloom_size != 0) {
        const struct ndn_bloom_wire *f = ndn_bloom_validate_wire(bloom, bloom_size);
        if (f == ((void *)0))
            return(excluded);
        if (ndn_bloom_match_wire(f, nextcomp, nextcomp_size))
            return(excluded);
    }
    return(!excluded);
}

canonical_original_code:
int ndn_excluded(unsigned char *excl, unsigned long excl_size, unsigned char *nextcomp, unsigned long nextcomp_size)
{
    unsigned char match_any[2] = "-";
    unsigned char *bloom = ((void *)0);
    unsigned long bloom_size = 0;
    struct ndn_buf_decoder decoder;
    struct ndn_buf_decoder *d = ndn_buf_decoder_start(&decoder, excl, excl_size);
    unsigned char *comp = ((void *)0);
    unsigned long comp_size = 0;
    int excluded = 1;
    if (!ndn_buf_match_dtag(d, NDN_DTAG_Exclude))
        abort();
    ndn_buf_advance(d);
    if (ndn_buf_match_dtag(d, NDN_DTAG_Any)) {
        ndn_buf_advance(d);
        bloom = match_any;
        ndn_buf_check_close(d);
    }
    else if (ndn_buf_match_dtag(d, NDN_DTAG_Bloom)) {
        ndn_buf_advance(d);
        if (ndn_buf_match_blob(d, &bloom, &bloom_size))
            ndn_buf_advance(d);
        ndn_buf_check_close(d);
    }
    while (ndn_buf_match_dtag(d, NDN_DTAG_Component)) {
        ndn_buf_advance(d);
        comp_size = 0;
        if (ndn_buf_match_blob(d, &comp, &comp_size))
            ndn_buf_advance(d);
        ndn_buf_check_close(d);
        if (comp_size > nextcomp_size)
            break;
        if (comp_size == nextcomp_size) {
            int res = memcmp(comp, nextcomp, comp_size);
            if (res == 0)
                return(excluded);
            if (res > 0)
                break;
        }
        bloom = ((void *)0);
        bloom_size = 0;
        if (ndn_buf_match_dtag(d, NDN_DTAG_Any)) {
            ndn_buf_advance(d);
            bloom = match_any;
            ndn_buf_check_close(d);
        }
        else if (ndn_buf_match_dtag(d, NDN_DTAG_Bloom)) {
            ndn_buf_advance(d);
            if (ndn_buf_match_blob(d, &bloom, &bloom_size))
                ndn_buf_advance(d);
            ndn_buf_check_close(d);
        }
    }
    if (bloom == match_any)
        return(excluded);
    else if (bloom_size != 0) {
        struct ndn_bloom_wire *f = ndn_bloom_validate_wire(bloom, bloom_size);
        if (f == ((void *)0))
            return(excluded);
        if (ndn_bloom_match_wire(f, nextcomp, nextcomp_size))
            return(excluded);
    }
    return(!excluded);
}

variable_types:
excl
unsigned char *

variable_types:
nextcomp
unsigned char *

variable_types:
bloom
unsigned char *

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:
comp
unsigned char *

variable_types: excluded int

variable_types: res int

variable_types: struct ndn_bloom_wire { unsigned char lg_bits; unsigned char n_hash; unsigned char method; unsigned char reserved; unsigned char seed[4]; unsigned char bloom[1024]; } *

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


original_code:
int
ndn_content_matches_interest(const unsigned char *content_object,
                             size_t content_object_size,
                             int implicit_content_digest,
                             struct ndn_parsed_ContentObject *pc,
                             const unsigned char *interest_msg,
                             size_t interest_msg_size,
                             const struct ndn_parsed_interest *pi)
{
    struct ndn_parsed_ContentObject pc_store;
    struct ndn_parsed_interest pi_store;
    int res;
    int ncomps;
    int prefixstart;
    int prefixbytes;
    int namecompstart;
    int namecompbytes;
    int checkdigest = 0;
    struct ndn_buf_decoder decoder;
    struct ndn_buf_decoder *d;
    const unsigned char *nextcomp;
    size_t nextcomp_size = 0;
    const unsigned char *comp = ((void *)0);
    size_t comp_size = 0;
    if (pc == ((void *)0)) {
        res = ndn_parse_ContentObject(content_object, content_object_size,
                                      &pc_store, ((void *)0));
        if (res < 0) return(0);
        pc = &pc_store;
    }
    if (pi == ((void *)0)) {
        res = ndn_parse_interest(interest_msg, interest_msg_size,
                                 &pi_store, ((void *)0));
        if (res < 0) return(0);
        pi = &pi_store;
    }
    if (!ndn_pubid_matches(content_object, pc, interest_msg, pi))
        return(0);
    ncomps = pc->name_ncomps + (implicit_content_digest ? 1 : 0);
    if (ncomps < pi->prefix_comps + pi->min_suffix_comps)
        return(0);
    if (ncomps > pi->prefix_comps + pi->max_suffix_comps)
        return(0);
    prefixstart = pi->offset[NDN_PI_B_Component0];
    prefixbytes = pi->offset[NDN_PI_E_LastPrefixComponent] - prefixstart;
    namecompstart = pc->offset[NDN_PCO_B_Component0];
    namecompbytes = pc->offset[NDN_PCO_E_ComponentLast] - namecompstart;
    if (prefixbytes > namecompbytes) {
        if (implicit_content_digest &&
            pi->offset[NDN_PI_B_LastPrefixComponent] - prefixstart == namecompbytes &&
            (pi->offset[NDN_PI_E_LastPrefixComponent] -
             pi->offset[NDN_PI_B_LastPrefixComponent]) == 1 + 2 + 32 + 1) {
            prefixbytes = namecompbytes;
            checkdigest = 1;
        }
        else
            return(0);
    }
    if (0 != memcmp(interest_msg + prefixstart,
                    content_object + namecompstart,
                    prefixbytes))
        return(0);
    if (checkdigest) {
        ndn_digest_ContentObject(content_object, pc);
        d = ndn_buf_decoder_start(&decoder,
                        interest_msg + pi->offset[NDN_PI_B_LastPrefixComponent],
                        (pi->offset[NDN_PI_E_LastPrefixComponent] -
                         pi->offset[NDN_PI_B_LastPrefixComponent]));
        comp_size = 0;
        if (ndn_buf_match_dtag(d, NDN_DTAG_Component)) {
                ndn_buf_advance(d);
                ndn_buf_match_blob(d, &comp, &comp_size);
            }
        if (comp_size != pc->digest_bytes) abort();
        if (0 != memcmp(comp, pc->digest, comp_size))
            return(0);
    }
    else if (pi->offset[NDN_PI_E_Exclude] > pi->offset[NDN_PI_B_Exclude]) {
        if (prefixbytes < namecompbytes) {
            d = ndn_buf_decoder_start(&decoder,
                                      content_object +
                                        (namecompstart + prefixbytes),
                                      pc->offset[NDN_PCO_E_ComponentLast] -
                                        (namecompstart + prefixbytes));
            if (ndn_buf_match_dtag(d, NDN_DTAG_Component)) {
                ndn_buf_advance(d);
                ndn_buf_match_blob(d, &nextcomp, &nextcomp_size);
            }
            else
                return(0);
        }
        else if (!implicit_content_digest)
            goto exclude_checked;
        else if (prefixbytes == namecompbytes) {
            ndn_digest_ContentObject(content_object, pc);
            nextcomp_size = pc->digest_bytes;
            nextcomp = pc->digest;
        }
        else abort();
        if (ndn_excluded(interest_msg + pi->offset[NDN_PI_B_Exclude],
                         (pi->offset[NDN_PI_E_Exclude] -
                          pi->offset[NDN_PI_B_Exclude]),
                         nextcomp,
                         nextcomp_size))
            return(0);
    exclude_checked: {}
    }
    return(1);
}

canonical_original_code:
int ndn_content_matches_interest(unsigned char *content_object, unsigned long content_object_size, int implicit_content_digest, struct ndn_parsed_ContentObject *pc, unsigned char *interest_msg, unsigned long interest_msg_size, struct ndn_parsed_interest *pi)
{
    struct ndn_parsed_ContentObject pc_store;
    struct ndn_parsed_interest pi_store;
    int res;
    int ncomps;
    int prefixstart;
    int prefixbytes;
    int namecompstart;
    int namecompbytes;
    int checkdigest = 0;
    struct ndn_buf_decoder decoder;
    struct ndn_buf_decoder *d;
    unsigned char *nextcomp;
    unsigned long nextcomp_size = 0;
    unsigned char *comp = ((void *)0);
    unsigned long comp_size = 0;
    if (pc == ((void *)0)) {
        res = ndn_parse_ContentObject(content_object, content_object_size,
                                      &pc_store, ((void *)0));
        if (res < 0) return(0);
        pc = &pc_store;
    }
    if (pi == ((void *)0)) {
        res = ndn_parse_interest(interest_msg, interest_msg_size,
                                 &pi_store, ((void *)0));
        if (res < 0) return(0);
        pi = &pi_store;
    }
    if (!ndn_pubid_matches(content_object, pc, interest_msg, pi))
        return(0);
    ncomps = pc->name_ncomps + (implicit_content_digest ? 1 : 0);
    if (ncomps < pi->prefix_comps + pi->min_suffix_comps)
        return(0);
    if (ncomps > pi->prefix_comps + pi->max_suffix_comps)
        return(0);
    prefixstart = pi->offset[NDN_PI_B_Component0];
    prefixbytes = pi->offset[NDN_PI_E_LastPrefixComponent] - prefixstart;
    namecompstart = pc->offset[NDN_PCO_B_Component0];
    namecompbytes = pc->offset[NDN_PCO_E_ComponentLast] - namecompstart;
    if (prefixbytes > namecompbytes) {
        if (implicit_content_digest &&
            pi->offset[NDN_PI_B_LastPrefixComponent] - prefixstart == namecompbytes &&
            (pi->offset[NDN_PI_E_LastPrefixComponent] -
             pi->offset[NDN_PI_B_LastPrefixComponent]) == 1 + 2 + 32 + 1) {
            prefixbytes = namecompbytes;
            checkdigest = 1;
        }
        else
            return(0);
    }
    if (0 != memcmp(interest_msg + prefixstart,
                    content_object + namecompstart,
                    prefixbytes))
        return(0);
    if (checkdigest) {
        ndn_digest_ContentObject(content_object, pc);
        d = ndn_buf_decoder_start(&decoder,
                        interest_msg + pi->offset[NDN_PI_B_LastPrefixComponent],
                        (pi->offset[NDN_PI_E_LastPrefixComponent] -
                         pi->offset[NDN_PI_B_LastPrefixComponent]));
        comp_size = 0;
        if (ndn_buf_match_dtag(d, NDN_DTAG_Component)) {
                ndn_buf_advance(d);
                ndn_buf_match_blob(d, &comp, &comp_size);
            }
        if (comp_size != pc->digest_bytes) abort();
        if (0 != memcmp(comp, pc->digest, comp_size))
            return(0);
    }
    else if (pi->offset[NDN_PI_E_Exclude] > pi->offset[NDN_PI_B_Exclude]) {
        if (prefixbytes < namecompbytes) {
            d = ndn_buf_decoder_start(&decoder,
                                      content_object +
                                        (namecompstart + prefixbytes),
                                      pc->offset[NDN_PCO_E_ComponentLast] -
                                        (namecompstart + prefixbytes));
            if (ndn_buf_match_dtag(d, NDN_DTAG_Component)) {
                ndn_buf_advance(d);
                ndn_buf_match_blob(d, &nextcomp, &nextcomp_size);
            }
            else
                return(0);
        }
        else if (!implicit_content_digest)
            goto exclude_checked;
        else if (prefixbytes == namecompbytes) {
            ndn_digest_ContentObject(content_object, pc);
            nextcomp_size = pc->digest_bytes;
            nextcomp = pc->digest;
        }
        else abort();
        if (ndn_excluded(interest_msg + pi->offset[NDN_PI_B_Exclude],
                         (pi->offset[NDN_PI_E_Exclude] -
                          pi->offset[NDN_PI_B_Exclude]),
                         nextcomp,
                         nextcomp_size))
            return(0);
    exclude_checked: {}
    }
    return(1);
}

variable_types: struct ndn_parsed_ContentObject { int magic; enum ndn_content_type { NDN_CONTENT_DATA=787648, NDN_CONTENT_ENCR=1101969, NDN_CONTENT_GONE=1631044, NDN_CONTENT_KEY=2639423, NDN_CONTENT_LINK=2917194, NDN_CONTENT_NACK=3408010 } type; int name_ncomps; unsigned short offset[-1]; unsigned char digest[32]; int digest_bytes; } *

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: ncomps int

variable_types: prefixstart int

variable_types: prefixbytes int

variable_types: namecompstart int

variable_types: namecompbytes int

variable_types: checkdigest int

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; }

user_defined_types: struct ndn_parsed_ContentObject { int magic; enum ndn_content_type type; int name_ncomps; unsigned short offset[-1]; unsigned char digest[32]; int digest_bytes; }


decompiled_code:
__int64 __fastcall ms_to_tu(int a1)
{
return (unsigned int)((a1 << 12) / 1000);
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
return (unsigned int)((a1 << 12) / 1000);
}

original_code:
static int
ms_to_tu(int m)
{
    return ((m * 4096) / 1000);
}

variable_types: m int


decompiled_code:
__int64 __fastcall append_future_vcomp(__int64 a1)
{
int v2; // [rsp+9h] [rbp-Fh] BYREF
__int16 v3; // [rsp+Dh] [rbp-Bh]
char v4; // [rsp+Fh] [rbp-9h]

v2 = 254;
v3 = 0;
v4 = 0;
ndn_charbuf_append_tt(a1, 15LL, 2LL);
ndn_charbuf_append_tt(a1, 7LL, 5LL);
ndn_charbuf_append(a1, &v2, 7LL);
return ndn_charbuf_append_closer(a1);
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
int v2; // [rsp+9h] [rbp-Fh] BYREF
__int16 v3; // [rsp+Dh] [rbp-Bh]
char v4; // [rsp+Fh] [rbp-9h]

v2 = 254;
v3 = 0;
v4 = 0;
ndn_charbuf_append_tt(a1, 15LL, 2LL);
ndn_charbuf_append_tt(a1, 7LL, 5LL);
ndn_charbuf_append(a1, &v2, 7LL);
return ndn_charbuf_append_closer(a1);
}

original_code:
static void
append_future_vcomp(struct ndn_charbuf *templ)
{
    unsigned char b[7] = {NDN_MARKER_VERSION + 1, 0, 0, 0, 0, 0, 0};
    ndn_charbuf_append_tt(templ, NDN_DTAG_Component, NDN_DTAG);
    ndn_charbuf_append_tt(templ, sizeof(b), NDN_BLOB);
    ndn_charbuf_append(templ, b, sizeof(b));
    ndn_charbuf_append_closer(templ);
}

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

variable_types:
b
unsigned char [7]

return_type: void


decompiled_code:
__int64 __fastcall append_filter_all(__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_filter_all(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 answer_highest(__int64 a1)
{
return ndnb_tagged_putf(a1, 85LL, &LC0);
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
return ndnb_tagged_putf(a1, 85LL, &LC0);
}

original_code:
static void
answer_highest(struct ndn_charbuf *templ)
{
    ndnb_tagged_putf(templ, NDN_DTAG_ChildSelector, "1");
}


decompiled_code:
__int64 __fastcall answer_passive(__int64 a1)
{
ndn_charbuf_append_tt(a1, 47LL, 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, 47LL, 2LL);
ndn_charbuf_append_tt(a1, 1LL, 6LL);
ndn_charbuf_append(a1, &LC0, 1LL);
return ndn_charbuf_append_closer(a1);
}

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


original_code:
static int
set_multicast_socket_options(int socket_r, int socket_w,
                             struct addrinfo *ai,
                             struct addrinfo *localif_for_mcast_addrinfo,
                             int multicastttl,
                             int ifindex,
                             void (*logger)(void *, const char *, ...),
                             void *logdat)
{
    struct addrinfo hints;
    struct ip_mreq mreq;
    struct ipv6_mreq mreq6;
    unsigned char csockopt;
    unsigned int isockopt;
    int res;
    memset((void *)&hints, 0, sizeof(hints));
    memset((void *)&mreq, 0, sizeof(mreq));
    memset((void *)&mreq6, 0, sizeof(mreq6));
    if (ai->ai_family == 2 && ((((in_addr_t)(ntohl(((struct sockaddr_in *)(ai->ai_addr))->sin_addr.s_addr))) & 0xf0000000) == 0xe0000000)) {
        if (logger) (*logger)(logdat, "IPv4 multicast");
        memcpy((void *)&mreq.imr_multiaddr, &((struct sockaddr_in *)ai->ai_addr)->sin_addr, sizeof(mreq.imr_multiaddr));
        if (localif_for_mcast_addrinfo != ((void *)0)) {
            memcpy((void *)&mreq.imr_interface.s_addr, &((struct sockaddr_in *)localif_for_mcast_addrinfo->ai_addr)->sin_addr, sizeof(mreq.imr_interface.s_addr));
        }
        res = setsockopt(socket_r, IPPROTO_IP, 35, &mreq, sizeof(mreq));
        if (res == -1) {
            if (logger) (*logger)(logdat, "setsockopt(..., IP_ADD_MEMBERSHIP, ...): %s", strerror((*__errno_location ())));
            return(-1);
        }
        csockopt = 0;
        res = setsockopt(socket_w, IPPROTO_IP, 34, &csockopt, sizeof(csockopt));
        if (res == -1) {
            if (logger) (*logger)(logdat, "setsockopt(..., IP_MULTICAST_LOOP, ...): %s", strerror((*__errno_location ())));
            return(-1);
        }
        if (multicastttl > 0) {
            csockopt = multicastttl;
            res = setsockopt(socket_w, IPPROTO_IP, 33, &csockopt, sizeof(csockopt));
            if (res == -1) {
                if (logger) (*logger)(logdat, "setsockopt(..., IP_MULTICAST_TTL, ...): %s", strerror((*__errno_location ())));
                return(-1);
            }
        }
        if (localif_for_mcast_addrinfo != ((void *)0)) {
            struct in_addr ifaddr = { 0 };
            ifaddr = ((struct sockaddr_in *)localif_for_mcast_addrinfo->ai_addr)->sin_addr;
            res = setsockopt(socket_w, IPPROTO_IP, 32, &ifaddr, sizeof(ifaddr));
            if (res == -1) {
                if (logger) (*logger)(logdat, "setsockopt(..., IP_MULTICAST_IF, ...): %s", strerror((*__errno_location ())));
                return(-1);
            }
        }
    } else if (ai->ai_family == 10 && (((const uint8_t *) ((&((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr)))[0] == 0xff)) {
        if (logger) (*logger)(logdat, "IPv6 multicast");
        memcpy((void *)&mreq6.ipv6mr_multiaddr, &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr, sizeof(mreq6.ipv6mr_multiaddr));
        if (ifindex > 0) {
            mreq6.ipv6mr_interface = ifindex;
        }
        res = setsockopt(socket_r, IPPROTO_IPV6, 20, &mreq6, sizeof(mreq6));
        if (res == -1) {
            if (logger) (*logger)(logdat, "setsockopt(..., IPV6_JOIN_GROUP, ...): %s", strerror((*__errno_location ())));
            return(-1);
        }
        isockopt = 0;
        res = setsockopt(socket_w, IPPROTO_IPV6, 19, &isockopt, sizeof(isockopt));
        if (res == -1) {
            if (logger) (*logger)(logdat, "setsockopt(..., IPV6_MULTICAST_LOOP, ...): %s", strerror((*__errno_location ())));
            return(-1);
        }
        if (multicastttl > 0) {
            isockopt = multicastttl;
            res = setsockopt(socket_w, IPPROTO_IPV6, 18, &isockopt, sizeof(isockopt));
            if (res == -1) {
                if (logger) (*logger)(logdat, "setsockopt(..., IPV6_MULTICAST_LOOP, ...): %s", strerror((*__errno_location ())));
                return(-1);
            }
        }
    }
    return(0);
}

canonical_original_code:
static int set_multicast_socket_options(int socket_r, int socket_w, struct addrinfo *ai, struct addrinfo *localif_for_mcast_addrinfo, int multicastttl, int ifindex, void (*logger)(void *, char *, ...), void *logdat)
{
    struct addrinfo hints;
    struct ip_mreq mreq;
    struct ipv6_mreq mreq6;
    unsigned char csockopt;
    unsigned int isockopt;
    int res;
    memset((void *)&hints, 0, sizeof(hints));
    memset((void *)&mreq, 0, sizeof(mreq));
    memset((void *)&mreq6, 0, sizeof(mreq6));
    if (ai->ai_family == 2 && ((((in_addr_t)(ntohl(((struct sockaddr_in *)(ai->ai_addr))->sin_addr.s_addr))) & 0xf0000000) == 0xe0000000)) {
        if (logger) (*logger)(logdat, "IPv4 multicast");
        memcpy((void *)&mreq.imr_multiaddr, &((struct sockaddr_in *)ai->ai_addr)->sin_addr, sizeof(mreq.imr_multiaddr));
        if (localif_for_mcast_addrinfo != ((void *)0)) {
            memcpy((void *)&mreq.imr_interface.s_addr, &((struct sockaddr_in *)localif_for_mcast_addrinfo->ai_addr)->sin_addr, sizeof(mreq.imr_interface.s_addr));
        }
        res = setsockopt(socket_r, IPPROTO_IP, 35, &mreq, sizeof(mreq));
        if (res == -1) {
            if (logger) (*logger)(logdat, "setsockopt(..., IP_ADD_MEMBERSHIP, ...): %s", strerror((*__errno_location ())));
            return(-1);
        }
        csockopt = 0;
        res = setsockopt(socket_w, IPPROTO_IP, 34, &csockopt, sizeof(csockopt));
        if (res == -1) {
            if (logger) (*logger)(logdat, "setsockopt(..., IP_MULTICAST_LOOP, ...): %s", strerror((*__errno_location ())));
            return(-1);
        }
        if (multicastttl > 0) {
            csockopt = multicastttl;
            res = setsockopt(socket_w, IPPROTO_IP, 33, &csockopt, sizeof(csockopt));
            if (res == -1) {
                if (logger) (*logger)(logdat, "setsockopt(..., IP_MULTICAST_TTL, ...): %s", strerror((*__errno_location ())));
                return(-1);
            }
        }
        if (localif_for_mcast_addrinfo != ((void *)0)) {
            struct in_addr ifaddr = { 0 };
            ifaddr = ((struct sockaddr_in *)localif_for_mcast_addrinfo->ai_addr)->sin_addr;
            res = setsockopt(socket_w, IPPROTO_IP, 32, &ifaddr, sizeof(ifaddr));
            if (res == -1) {
                if (logger) (*logger)(logdat, "setsockopt(..., IP_MULTICAST_IF, ...): %s", strerror((*__errno_location ())));
                return(-1);
            }
        }
    } else if (ai->ai_family == 10 && (((unsigned char *) ((&((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr)))[0] == 0xff)) {
        if (logger) (*logger)(logdat, "IPv6 multicast");
        memcpy((void *)&mreq6.ipv6mr_multiaddr, &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr, sizeof(mreq6.ipv6mr_multiaddr));
        if (ifindex > 0) {
            mreq6.ipv6mr_interface = ifindex;
        }
        res = setsockopt(socket_r, IPPROTO_IPV6, 20, &mreq6, sizeof(mreq6));
        if (res == -1) {
            if (logger) (*logger)(logdat, "setsockopt(..., IPV6_JOIN_GROUP, ...): %s", strerror((*__errno_location ())));
            return(-1);
        }
        isockopt = 0;
        res = setsockopt(socket_w, IPPROTO_IPV6, 19, &isockopt, sizeof(isockopt));
        if (res == -1) {
            if (logger) (*logger)(logdat, "setsockopt(..., IPV6_MULTICAST_LOOP, ...): %s", strerror((*__errno_location ())));
            return(-1);
        }
        if (multicastttl > 0) {
            isockopt = multicastttl;
            res = setsockopt(socket_w, IPPROTO_IPV6, 18, &isockopt, sizeof(isockopt));
            if (res == -1) {
                if (logger) (*logger)(logdat, "setsockopt(..., IPV6_MULTICAST_LOOP, ...): %s", strerror((*__errno_location ())));
                return(-1);
            }
        }
    }
    return(0);
}

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: multicastttl int

variable_types: ifindex int

variable_types:
logger
void (*)(void *, char *, ...)

variable_types:
logdat
void *

variable_types: struct ip_mreq { struct in_addr { unsigned int s_addr; } imr_multiaddr; struct in_addr imr_interface; }

variable_types: struct ipv6_mreq { struct in6_addr { union { unsigned char __u6_addr8[16]; unsigned short __u6_addr16[8]; unsigned int __u6_addr32[4]; } __in6_u; } ipv6mr_multiaddr; unsigned int ipv6mr_interface; }

variable_types:
csockopt
unsigned char

variable_types:
isockopt
unsigned int

variable_types:
ifaddr
struct in_addr { unsigned int s_addr; }

user_defined_types: struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; unsigned int ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }

user_defined_types: struct ip_mreq { struct in_addr imr_multiaddr; struct in_addr imr_interface; }

user_defined_types: struct ipv6_mreq { struct in6_addr ipv6mr_multiaddr; unsigned int ipv6mr_interface; }

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; }


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 v10; // eax
const char *v11; // r15
size_t v12; // r14
const char *v13; // rdi
int v14; // eax
__int64 v15; // rdx
const char *v16; // rdi
char *v17; // r15
int *v18; // rax
const char *v19; // r15
int v20; // eax
struct addrinfo *v21; // rax
int v22; // eax
unsigned int v23; // er14
bool v24; // zf
int v25; // er15
int v26; // eax
int v27; // edi
const char *v29; // r14
int *v30; // rax
int v31; // edi
const char *v32; // rdi
const char *v33; // rsi
int v34; // eax
struct addrinfo *v35; // rax
int v36; // eax
int v37; // edi
const char *v38; // rax
const char *v39; // rdx
const char *v40; // rcx
int v41; // eax
int *v42; // rax
char *v43; // rax
int *v44; // rax
char *v45; // rax
const char *v46; // rax
int *v47; // rax
char *v48; // rax
int *v49; // rax
char *v50; // rax
int *v51; // rax
char *v52; // rax
int v54; // [rsp+Ch] [rbp-9Ch]
int *v56; // [rsp+18h] [rbp-90h]
int optval; // [rsp+24h] [rbp-84h] BYREF
struct addrinfo *pai; // [rsp+28h] [rbp-80h] BYREF
struct addrinfo *ai; // [rsp+30h] [rbp-78h] BYREF
struct addrinfo *v60; // [rsp+38h] [rbp-70h] BYREF
struct addrinfo req; // [rsp+40h] [rbp-68h] BYREF

pai = 0LL;
ai = 0LL;
v60 = 0LL;
optval = 1;
*(_OWORD *)&req.ai_flags = 0LL;
*(_OWORD *)&req.ai_addrlen = 0LL;
*(_OWORD *)&req.ai_canonname = 0LL;
if ( a2 )
a2(a3, "at ndn_sockcreate.c:%d", 194LL);
*(_QWORD *)a6 = -1LL;
v10 = *(_DWORD *)a1;
if ( *(int *)a1 > 0 )
{
req.ai_protocol = *(_DWORD *)a1;
if ( v10 == 17 )
{
req.ai_socktype = 2;
}
else if ( v10 == 6 )
{
req.ai_socktype = 1;
}
}
v11 = *(const char **)(a1 + 16);
req.ai_flags = 4;
if ( !v11 || (v12 = strspn(v11, "0123456789"), v12 != strlen(v11)) )
{
if ( a2 )
a2(a3, "must specify numeric port");
goto LABEL_54;
}
if ( a2 )
{
a2(a3, "at ndn_sockcreate.c:%d", 208LL);
v13 = *(const char **)(a1 + 24);
if ( v13 )
{
v14 = getaddrinfo(v13, *(const char **)(a1 + 16), &req, &pai);
if ( v14 || !pai )
{
v46 = gai_strerror(v14);
v39 = *(const char **)(a1 + 24);
v40 = v46;
goto LABEL_96;
}
req.ai_family = pai->ai_family;
}
a2(a3, "at ndn_sockcreate.c:%d", 219LL);
v15 = *(unsigned int *)(a1 + 32);
if ( (int)v15 >= 0 )
goto LABEL_14;
a2(a3, "at ndn_sockcreate.c:%d", 227LL);
v29 = *(const char **)(a1 + 8);
if ( !v29 )
goto LABEL_116;
LABEL_50:
v17 = strchr(v29, 37);
if ( a2 )
goto LABEL_18;
goto LABEL_51;
}
v32 = *(const char **)(a1 + 24);
if ( v32 )
{
if ( getaddrinfo(v32, v11, &req, &pai) || !pai )
goto LABEL_54;
req.ai_family = pai->ai_family;
}
v15 = *(unsigned int *)(a1 + 32);
if ( (int)v15 < 0 )
{
v29 = *(const char **)(a1 + 8);
if ( !v29 )
goto LABEL_54;
goto LABEL_50;
}
LABEL_14:
if ( (unsigned int)(v15 - 1) > 0xFE )
{
if ( a2 )
{
v25 = -1;
a2(a3, "mcast_ttl(%d) out of range", v15);
v23 = -1;
goto LABEL_41;
}
goto LABEL_54;
}
if ( a2 )
{
a2(a3, "at ndn_sockcreate.c:%d", 227LL);
v16 = *(const char **)(a1 + 8);
if ( v16 )
{
v17 = strchr(v16, 37);
LABEL_18:
a2(a3, "at ndn_sockcreate.c:%d", 234LL);
if ( !v17 )
{
v54 = 0;
LABEL_20:
a2(a3, "at ndn_sockcreate.c:%d", 248LL);
v20 = getaddrinfo(*(const char **)(a1 + 8), *(const char **)(a1 + 16), &req, &ai);
if ( v20 )
goto LABEL_95;
goto LABEL_21;
}
v18 = _errno_location();
v19 = v17 + 1;
*v18 = 0;
v56 = v18;
v54 = atoi(v19);
if ( v54 )
goto LABEL_20;
goto LABEL_57;
}
LABEL_116:
v25 = -1;
a2(a3, "must specify remote address");
v23 = -1;
goto LABEL_41;
}
v29 = *(const char **)(a1 + 8);
if ( !v29 )
goto LABEL_54;
v17 = strchr(*(const char **)(a1 + 8), 37);
LABEL_51:
if ( !v17 )
{
v54 = 0;
LABEL_53:
if ( getaddrinfo(v29, *(const char **)(a1 + 16), &req, &ai) )
goto LABEL_54;
LABEL_21:
v21 = ai;
if ( ai )
goto LABEL_22;
v25 = -1;
v23 = -1;
if ( !a2 )
goto LABEL_43;
goto LABEL_94;
}
v30 = _errno_location();
v19 = v17 + 1;
*v30 = 0;
v56 = v30;
v54 = atoi(v19);
if ( v54 )
goto LABEL_53;
LABEL_57:
v54 = if_nametoindex(v19);
if ( !v54 && *v56 )
{
if ( a2 )
{
a2(a3, "Invalid interface name %s", v19);
v25 = -1;
v23 = -1;
goto LABEL_41;
}
goto LABEL_54;
}
if ( !a2 )
{
v29 = *(const char **)(a1 + 8);
goto LABEL_53;
}
a2(a3, "at ndn_sockcreate.c:%d", 248LL);
v20 = getaddrinfo(*(const char **)(a1 + 8), *(const char **)(a1 + 16), &req, &ai);
if ( v20 )
{
LABEL_95:
v38 = gai_strerror(v20);
v39 = *(const char **)(a1 + 8);
v40 = v38;
LABEL_96:
a2(a3, "getaddrinfo(\"%s\", ...): %s", v39, v40);
goto LABEL_54;
}
v21 = ai;
if ( !ai )
{
LABEL_94:
v20 = 0;
goto LABEL_95;
}
LABEL_22:
v22 = socket(v21->ai_family, v21->ai_socktype, v21->ai_protocol);
v23 = v22;
if ( !a2 )
{
if ( v22 != -1 )
{
v24 = pai == 0LL;
a6[1] = v22;
*a6 = v22;
if ( !v24 )
goto LABEL_25;
v33 = *(const char **)(a1 + 16);
req.ai_flags = 1;
v34 = ai->ai_socktype;
req.ai_family = ai->ai_family;
req.ai_socktype = v34;
if ( !getaddrinfo(0LL, v33, &req, &v60) )
{
LABEL_73:
if ( bind(a6[1], v60->ai_addr, v60->ai_addrlen) == -1 && a4 )
{
v35 = v60;
v60 = 0LL;
pai = v35;
if ( v35 )
{
a6[1] = -1;
LABEL_26:
if ( a2 )
{
v25 = -1;
a2(a3, "at ndn_sockcreate.c:%d", 291LL);
v26 = a4(a5, pai->ai_addr, pai->ai_addrlen);
a6[1] = v26;
if ( v26 < 0 )
goto LABEL_30;
a2(a3, "at ndn_sockcreate.c:%d", 296LL);
v25 = a6[1];
}
else
{
v26 = a4(a5, pai->ai_addr, pai->ai_addrlen);
a6[1] = v26;
v25 = v26;
if ( v26 < 0 )
{
v25 = -1;
LABEL_30:
if ( v26 != -1 )
goto LABEL_31;
goto LABEL_99;
}
}
v26 = v25;
goto LABEL_30;
}
}
else if ( pai )
{
goto LABEL_25;
}
v25 = -1;
goto LABEL_33;
}
}
LABEL_54:
v25 = -1;
v23 = -1;
goto LABEL_41;
}
a2(a3, "at ndn_sockcreate.c:%d", 257LL);
if ( v23 == -1 )
{
v49 = _errno_location();
v25 = -1;
v50 = strerror(*v49);
a2(a3, "socket: %s", v50);
goto LABEL_41;
}
a2(a3, "at ndn_sockcreate.c:%d", 262LL);
v24 = pai == 0LL;
a6[1] = v23;
*a6 = v23;
if ( v24 )
{
req.ai_flags = 1;
v36 = ai->ai_socktype;
req.ai_family = ai->ai_family;
req.ai_socktype = v36;
a2(a3, "at ndn_sockcreate.c:%d", 269LL);
if ( !getaddrinfo(0LL, *(const char **)(a1 + 16), &req, &v60) )
{
a2(a3, "at ndn_sockcreate.c:%d", 273LL);
goto LABEL_73;
}
goto LABEL_54;
}
LABEL_25:
a6[1] = -1;
v25 = -1;
if ( a4 )
goto LABEL_26;
LABEL_99:
v41 = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
a6[1] = v41;
v23 = v41;
if ( v41 == -1 )
{
if ( a2 )
{
v42 = _errno_location();
v43 = strerror(*v42);
a2(a3, "socket: %s", v43);
}
goto LABEL_41;
}
LABEL_31:
v23 = setsockopt(*a6, 1, 2, &optval, 4u);
if ( v23 == -1 )
{
if ( a2 )
{
v44 = _errno_location();
v45 = strerror(*v44);
a2(a3, "setsockopt(recving, ..., SO_REUSEADDR, ...): %s", v45);
}
goto LABEL_41;
}
v23 = bind(*a6, ai->ai_addr, ai->ai_addrlen);
if ( v23 == -1 )
{
if ( a2 )
{
v47 = _errno_location();
v48 = strerror(*v47);
a2(a3, "bind(recving, ...): %s", v48);
}
goto LABEL_41;
}
LABEL_33:
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),
v54,
(__int64)a2,
a3) < 0 )
{
v23 = -1;
goto LABEL_41;
}
if ( !pai )
{
if ( a2 )
{
LABEL_40:
v23 = 0;
a2(a3, "at ndn_sockcreate.c:%d", 340LL);
goto LABEL_41;
}
goto LABEL_78;
}
if ( !a2 )
{
v37 = a6[1];
if ( v25 != v37 )
{
v23 = -(bind(v37, pai->ai_addr, pai->ai_addrlen) == -1);
goto LABEL_41;
}
LABEL_78:
v23 = 0;
goto LABEL_41;
}
a2(a3, "at ndn_sockcreate.c:%d", 329LL);
v27 = a6[1];
if ( v25 == v27 )
goto LABEL_40;
v23 = bind(v27, pai->ai_addr, pai->ai_addrlen);
if ( v23 != -1 )
goto LABEL_40;
v51 = _errno_location();
v52 = strerror(*v51);
a2(a3, "bind(sending, ...): %s", v52);
LABEL_41:
if ( ai )
freeaddrinfo(ai);
LABEL_43:
if ( v60 )
freeaddrinfo(v60);
if ( pai )
freeaddrinfo(pai);
if ( v23 )
{
close(*a6);
v31 = a6[1];
if ( *a6 == v31 || v31 == v25 )
{
*(_QWORD *)a6 = -1LL;
}
else
{
close(v31);
*(_QWORD *)a6 = -1LL;
}
}
return v23;
}

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 v10; // eax
const char *v11; // r15
size_t v12; // r14
const char *v13; // rdi
int v14; // eax
__int64 v15; // rdx
const char *v16; // rdi
char *v17; // r15
int *v18; // rax
const char *v19; // r15
int v20; // eax
struct addrinfo *v21; // rax
int v22; // eax
unsigned int v23; // er14
bool v24; // zf
int v25; // er15
int v26; // eax
int v27; // edi
const char *v29; // r14
int *v30; // rax
int v31; // edi
const char *v32; // rdi
const char *v33; // rsi
int v34; // eax
struct addrinfo *v35; // rax
int v36; // eax
int v37; // edi
const char *v38; // rax
const char *v39; // rdx
const char *v40; // rcx
int v41; // eax
int *v42; // rax
char *v43; // rax
int *v44; // rax
char *v45; // rax
const char *v46; // rax
int *v47; // rax
char *v48; // rax
int *v49; // rax
char *v50; // rax
int *v51; // rax
char *v52; // rax
int v54; // [rsp+Ch] [rbp-9Ch]
int *v56; // [rsp+18h] [rbp-90h]
int optval; // [rsp+24h] [rbp-84h] BYREF
struct addrinfo *pai; // [rsp+28h] [rbp-80h] BYREF
struct addrinfo *ai; // [rsp+30h] [rbp-78h] BYREF
struct addrinfo *v60; // [rsp+38h] [rbp-70h] BYREF
struct addrinfo req; // [rsp+40h] [rbp-68h] BYREF

pai = 0LL;
ai = 0LL;
v60 = 0LL;
optval = 1;
*(_OWORD *)&req.ai_flags = 0LL;
*(_OWORD *)&req.ai_addrlen = 0LL;
*(_OWORD *)&req.ai_canonname = 0LL;
if ( a2 )
a2(a3, "at ndn_sockcreate.c:%d", 194LL);
*(_QWORD *)a6 = -1LL;
v10 = *(_DWORD *)a1;
if ( *(int *)a1 > 0 )
{
req.ai_protocol = *(_DWORD *)a1;
if ( v10 == 17 )
{
req.ai_socktype = 2;
}
else if ( v10 == 6 )
{
req.ai_socktype = 1;
}
}
v11 = *(const char **)(a1 + 16);
req.ai_flags = 4;
if ( !v11 || (v12 = strspn(v11, "0123456789"), v12 != strlen(v11)) )
{
if ( a2 )
a2(a3, "must specify numeric port");
goto LABEL_54;
}
if ( a2 )
{
a2(a3, "at ndn_sockcreate.c:%d", 208LL);
v13 = *(const char **)(a1 + 24);
if ( v13 )
{
v14 = getaddrinfo(v13, *(const char **)(a1 + 16), &req, &pai);
if ( v14 || !pai )
{
v46 = gai_strerror(v14);
v39 = *(const char **)(a1 + 24);
v40 = v46;
goto LABEL_96;
}
req.ai_family = pai->ai_family;
}
a2(a3, "at ndn_sockcreate.c:%d", 219LL);
v15 = *(unsigned int *)(a1 + 32);
if ( (int)v15 >= 0 )
goto LABEL_14;
a2(a3, "at ndn_sockcreate.c:%d", 227LL);
v29 = *(const char **)(a1 + 8);
if ( !v29 )
goto LABEL_116;
LABEL_50:
v17 = strchr(v29, 37);
if ( a2 )
goto LABEL_18;
goto LABEL_51;
}
v32 = *(const char **)(a1 + 24);
if ( v32 )
{
if ( getaddrinfo(v32, v11, &req, &pai) || !pai )
goto LABEL_54;
req.ai_family = pai->ai_family;
}
v15 = *(unsigned int *)(a1 + 32);
if ( (int)v15 < 0 )
{
v29 = *(const char **)(a1 + 8);
if ( !v29 )
goto LABEL_54;
goto LABEL_50;
}
LABEL_14:
if ( (unsigned int)(v15 - 1) > 0xFE )
{
if ( a2 )
{
v25 = -1;
a2(a3, "mcast_ttl(%d) out of range", v15);
v23 = -1;
goto LABEL_41;
}
goto LABEL_54;
}
if ( a2 )
{
a2(a3, "at ndn_sockcreate.c:%d", 227LL);
v16 = *(const char **)(a1 + 8);
if ( v16 )
{
v17 = strchr(v16, 37);
LABEL_18:
a2(a3, "at ndn_sockcreate.c:%d", 234LL);
if ( !v17 )
{
v54 = 0;
LABEL_20:
a2(a3, "at ndn_sockcreate.c:%d", 248LL);
v20 = getaddrinfo(*(const char **)(a1 + 8), *(const char **)(a1 + 16), &req, &ai);
if ( v20 )
goto LABEL_95;
goto LABEL_21;
}
v18 = _errno_location();
v19 = v17 + 1;
*v18 = 0;
v56 = v18;
v54 = atoi(v19);
if ( v54 )
goto LABEL_20;
goto LABEL_57;
}
LABEL_116:
v25 = -1;
a2(a3, "must specify remote address");
v23 = -1;
goto LABEL_41;
}
v29 = *(const char **)(a1 + 8);
if ( !v29 )
goto LABEL_54;
v17 = strchr(*(const char **)(a1 + 8), 37);
LABEL_51:
if ( !v17 )
{
v54 = 0;
LABEL_53:
if ( getaddrinfo(v29, *(const char **)(a1 + 16), &req, &ai) )
goto LABEL_54;
LABEL_21:
v21 = ai;
if ( ai )
goto LABEL_22;
v25 = -1;
v23 = -1;
if ( !a2 )
goto LABEL_43;
goto LABEL_94;
}
v30 = _errno_location();
v19 = v17 + 1;
*v30 = 0;
v56 = v30;
v54 = atoi(v19);
if ( v54 )
goto LABEL_53;
LABEL_57:
v54 = if_nametoindex(v19);
if ( !v54 && *v56 )
{
if ( a2 )
{
a2(a3, "Invalid interface name %s", v19);
v25 = -1;
v23 = -1;
goto LABEL_41;
}
goto LABEL_54;
}
if ( !a2 )
{
v29 = *(const char **)(a1 + 8);
goto LABEL_53;
}
a2(a3, "at ndn_sockcreate.c:%d", 248LL);
v20 = getaddrinfo(*(const char **)(a1 + 8), *(const char **)(a1 + 16), &req, &ai);
if ( v20 )
{
LABEL_95:
v38 = gai_strerror(v20);
v39 = *(const char **)(a1 + 8);
v40 = v38;
LABEL_96:
a2(a3, "getaddrinfo(\"%s\", ...): %s", v39, v40);
goto LABEL_54;
}
v21 = ai;
if ( !ai )
{
LABEL_94:
v20 = 0;
goto LABEL_95;
}
LABEL_22:
v22 = socket(v21->ai_family, v21->ai_socktype, v21->ai_protocol);
v23 = v22;
if ( !a2 )
{
if ( v22 != -1 )
{
v24 = pai == 0LL;
a6[1] = v22;
*a6 = v22;
if ( !v24 )
goto LABEL_25;
v33 = *(const char **)(a1 + 16);
req.ai_flags = 1;
v34 = ai->ai_socktype;
req.ai_family = ai->ai_family;
req.ai_socktype = v34;
if ( !getaddrinfo(0LL, v33, &req, &v60) )
{
LABEL_73:
if ( bind(a6[1], v60->ai_addr, v60->ai_addrlen) == -1 && a4 )
{
v35 = v60;
v60 = 0LL;
pai = v35;
if ( v35 )
{
a6[1] = -1;
LABEL_26:
if ( a2 )
{
v25 = -1;
a2(a3, "at ndn_sockcreate.c:%d", 291LL);
v26 = a4(a5, pai->ai_addr, pai->ai_addrlen);
a6[1] = v26;
if ( v26 < 0 )
goto LABEL_30;
a2(a3, "at ndn_sockcreate.c:%d", 296LL);
v25 = a6[1];
}
else
{
v26 = a4(a5, pai->ai_addr, pai->ai_addrlen);
a6[1] = v26;
v25 = v26;
if ( v26 < 0 )
{
v25 = -1;
LABEL_30:
if ( v26 != -1 )
goto LABEL_31;
goto LABEL_99;
}
}
v26 = v25;
goto LABEL_30;
}
}
else if ( pai )
{
goto LABEL_25;
}
v25 = -1;
goto LABEL_33;
}
}
LABEL_54:
v25 = -1;
v23 = -1;
goto LABEL_41;
}
a2(a3, "at ndn_sockcreate.c:%d", 257LL);
if ( v23 == -1 )
{
v49 = _errno_location();
v25 = -1;
v50 = strerror(*v49);
a2(a3, "socket: %s", v50);
goto LABEL_41;
}
a2(a3, "at ndn_sockcreate.c:%d", 262LL);
v24 = pai == 0LL;
a6[1] = v23;
*a6 = v23;
if ( v24 )
{
req.ai_flags = 1;
v36 = ai->ai_socktype;
req.ai_family = ai->ai_family;
req.ai_socktype = v36;
a2(a3, "at ndn_sockcreate.c:%d", 269LL);
if ( !getaddrinfo(0LL, *(const char **)(a1 + 16), &req, &v60) )
{
a2(a3, "at ndn_sockcreate.c:%d", 273LL);
goto LABEL_73;
}
goto LABEL_54;
}
LABEL_25:
a6[1] = -1;
v25 = -1;
if ( a4 )
goto LABEL_26;
LABEL_99:
v41 = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
a6[1] = v41;
v23 = v41;
if ( v41 == -1 )
{
if ( a2 )
{
v42 = _errno_location();
v43 = strerror(*v42);
a2(a3, "socket: %s", v43);
}
goto LABEL_41;
}
LABEL_31:
v23 = setsockopt(*a6, 1, 2, &optval, 4u);
if ( v23 == -1 )
{
if ( a2 )
{
v44 = _errno_location();
v45 = strerror(*v44);
a2(a3, "setsockopt(recving, ..., SO_REUSEADDR, ...): %s", v45);
}
goto LABEL_41;
}
v23 = bind(*a6, ai->ai_addr, ai->ai_addrlen);
if ( v23 == -1 )
{
if ( a2 )
{
v47 = _errno_location();
v48 = strerror(*v47);
a2(a3, "bind(recving, ...): %s", v48);
}
goto LABEL_41;
}
LABEL_33:
if ( a2 )
a2(a3, "at ndn_sockcreate.c:%d", 318LL);
if ( (int)func1(
*a6,
a6[1],
(__int64)ai,
(__int64)pai,
*(_DWORD *)(a1 + 32),
v54,
(__int64)a2,
a3) < 0 )
{
v23 = -1;
goto LABEL_41;
}
if ( !pai )
{
if ( a2 )
{
LABEL_40:
v23 = 0;
a2(a3, "at ndn_sockcreate.c:%d", 340LL);
goto LABEL_41;
}
goto LABEL_78;
}
if ( !a2 )
{
v37 = a6[1];
if ( v25 != v37 )
{
v23 = -(bind(v37, pai->ai_addr, pai->ai_addrlen) == -1);
goto LABEL_41;
}
LABEL_78:
v23 = 0;
goto LABEL_41;
}
a2(a3, "at ndn_sockcreate.c:%d", 329LL);
v27 = a6[1];
if ( v25 == v27 )
goto LABEL_40;
v23 = bind(v27, pai->ai_addr, pai->ai_addrlen);
if ( v23 != -1 )
goto LABEL_40;
v51 = _errno_location();
v52 = strerror(*v51);
a2(a3, "bind(sending, ...): %s", v52);
LABEL_41:
if ( ai )
freeaddrinfo(ai);
LABEL_43:
if ( v60 )
freeaddrinfo(v60);
if ( pai )
freeaddrinfo(pai);
if ( v23 )
{
close(*a6);
v31 = a6[1];
if ( *a6 == v31 || v31 == v25 )
{
*(_QWORD *)a6 = -1LL;
}
else
{
close(v31);
*(_QWORD *)a6 = -1LL;
}
}
return v23;
}

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: res int

variable_types: one int

variable_types: sock int


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; } **

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; } *


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

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

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

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);
}

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);
}


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, &LC1, 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, &LC1, 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; // er13

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 )
return (unsigned int)-1;
--*(_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; // er13

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 )
return (unsigned int)-1;
--*(_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