decompiled_code:
int __fastcall bsock_bindresvport_sa(int fd, struct sockaddr *addr)
{
char *v3; // r15
__int16 v4; // di
__int16 v5; // bp
unsigned __int16 v6; // bx
int result; // eax
socklen_t len; // [rsp+Ch] [rbp-3Ch]

if ( addr->sa_family == 2 )
{
v3 = addr->sa_data;
v4 = *(_WORD *)addr->sa_data;
len = 16;
}
else
{
if ( addr->sa_family != 10 )
{
*_errno_location() = 97;
return -1;
}
v3 = addr->sa_data;
v4 = *(_WORD *)addr->sa_data;
len = 28;
}
v5 = _bswap_16(v4);
if ( (unsigned __int16)(v5 - 600) > 0x1A7u )
v5 = bsock_bindresvport_random_port();
v6 = v5;
do
{
if ( !bsock_bindresvport_skip(v6) )
{
*(_WORD *)v3 = _bswap_16(v6);
result = bind(fd, addr, len);
if ( !result )
return result;
if ( *_errno_location() != 98 )
return -1;
}
if ( ++v6 == 1024 )
v6 = 600;
}
while ( v6 != v5 );
return -1;
}

canonical_decompiled_code:
int __fastcall func3(int fd, struct sockaddr *addr)
{
char *v3; // r15
__int16 v4; // di
__int16 v5; // bp
unsigned __int16 v6; // bx
int result; // eax
socklen_t len; // [rsp+Ch] [rbp-3Ch]

if ( addr->sa_family == 2 )
{
v3 = addr->sa_data;
v4 = *(_WORD *)addr->sa_data;
len = 16;
}
else
{
if ( addr->sa_family != 10 )
{
*_errno_location() = 97;
return -1;
}
v3 = addr->sa_data;
v4 = *(_WORD *)addr->sa_data;
len = 28;
}
v5 = func2(v4);
if ( (unsigned __int16)(v5 - 600) > 0x1A7u )
v5 = func1();
v6 = v5;
do
{
if ( !func0(v6) )
{
*(_WORD *)v3 = func2(v6);
result = bind(fd, addr, len);
if ( !result )
return result;
if ( *_errno_location() != 98 )
return -1;
}
if ( ++v6 == 1024 )
v6 = 600;
}
while ( v6 != v5 );
return -1;
}

original_code:
__attribute__((__nonnull__ ))
int
bsock_bindresvport_sa (const int sockfd, struct sockaddr *sa)
{
    in_port_t port;
    in_port_t pstart;
    in_port_t *portptr;
    socklen_t addrlen;
    if (2 == sa->sa_family) {
        portptr = &((struct sockaddr_in *)sa)->sin_port;
        addrlen = sizeof(struct sockaddr_in);
    }
    else if (10 == sa->sa_family) {
        portptr = &((struct sockaddr_in6 *)sa)->sin6_port;
        addrlen = sizeof(struct sockaddr_in6);
    }
    else {
        (*__errno_location ()) = 97;
        return -1;
    }
    pstart = ntohs(*portptr);
    if (pstart < 600 || pstart >= IPPORT_RESERVED)
        pstart = (in_port_t) bsock_bindresvport_random_port();
    port = pstart;
    do {
        if (!bsock_bindresvport_skip(port)) {
            *portptr = htons(port);
            if (0 == bind(sockfd, sa, addrlen))
                return 0;
            else if ((*__errno_location ()) != 98)
                return -1;
        }
        if (++port == IPPORT_RESERVED)
            port = 600;
    } while (port != pstart);
    return -1;
}

canonical_original_code:
int bsock_bindresvport_sa(int sockfd, struct sockaddr *sa)
{
    unsigned short port;
    unsigned short pstart;
    unsigned short *portptr;
    unsigned int addrlen;
    if (2 == sa->sa_family) {
        portptr = &((struct sockaddr_in *)sa)->sin_port;
        addrlen = sizeof(struct sockaddr_in);
    }
    else if (10 == sa->sa_family) {
        portptr = &((struct sockaddr_in6 *)sa)->sin6_port;
        addrlen = sizeof(struct sockaddr_in6);
    }
    else {
        (*__errno_location ()) = 97;
        return -1;
    }
    pstart = ntohs(*portptr);
    if (pstart < 600 || pstart >= IPPORT_RESERVED)
        pstart = (unsigned short) bsock_bindresvport_random_port();
    port = pstart;
    do {
        if (!bsock_bindresvport_skip(port)) {
            *portptr = htons(port);
            if (0 == bind(sockfd, sa, addrlen))
                return 0;
            else if ((*__errno_location ()) != 98)
                return -1;
        }
        if (++port == IPPORT_RESERVED)
            port = 600;
    } while (port != pstart);
    return -1;
}

variable_types: sockfd int

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

variable_types:
port
unsigned short

variable_types:
pstart
unsigned short

variable_types:
portptr
unsigned short *

variable_types:
addrlen
unsigned int

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

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


repo: gstrauss/bsock

decompiled_code:
void __fastcall bsock_syslog_setlevel(int a1)
{
bsock_syslog_level = a1;
}

canonical_decompiled_code:
void __fastcall func0(int a1)
{
bsock_syslog_level = a1;
}

original_code:
__attribute__((__cold__))
__attribute__ ((__noinline__))
void
bsock_syslog_setlevel (const int level)
{
    bsock_syslog_level = level;
}

canonical_original_code:
void bsock_syslog_setlevel(int level)
{
    bsock_syslog_level = level;
}

variable_types: level int

return_type: void


decompiled_code:
void __fastcall bsock_syslog_setlogfd(int a1)
{
bsock_syslog_logfd = a1;
}

canonical_decompiled_code:
void __fastcall func1(int a1)
{
bsock_syslog_logfd = a1;
}

original_code:
__attribute__((__cold__))
__attribute__ ((__noinline__))
void
bsock_syslog_setlogfd (const int fd)
{
    bsock_syslog_logfd = fd;
}

canonical_original_code:
void bsock_syslog_setlogfd(int fd)
{
    bsock_syslog_logfd = fd;
}

variable_types: fd int


decompiled_code:
size_t __fastcall bsock_syslog_openlog(char *s, int a2, int a3)
{
size_t result; // rax

if ( bsock_syslog_level != 2 )
openlog(s, a2, a3);
bsock_syslog_ident = (__int64)s;
result = 0LL;
if ( s )
result = strlen(s);
bsock_syslog_identlen = result;
return result;
}

canonical_decompiled_code:
size_t __fastcall func2(char *s, int a2, int a3)
{
size_t result; // rax

if ( bsock_syslog_level != 2 )
openlog(s, a2, a3);
bsock_syslog_ident = (__int64)s;
result = 0LL;
if ( s )
result = strlen(s);
bsock_syslog_identlen = result;
return result;
}

original_code:
__attribute__((__cold__))
__attribute__ ((__noinline__))
void
bsock_syslog_openlog (const char * const ident,
                      const int option, const int facility)
{
    if (bsock_syslog_level != BSOCK_SYSLOG_PERROR_NOSYSLOG)
        openlog(ident, option, facility);
    bsock_syslog_ident = ident;
    bsock_syslog_identlen = (((void *)0) != ident) ? strlen(ident) : 0;
}

canonical_original_code:
void bsock_syslog_openlog(char *ident, int option, int facility)
{
    if (bsock_syslog_level != BSOCK_SYSLOG_PERROR_NOSYSLOG)
        openlog(ident, option, facility);
    bsock_syslog_ident = ident;
    bsock_syslog_identlen = (((void *)0) != ident) ? strlen(ident) : 0;
}

variable_types:
ident
char *

variable_types: option int

variable_types: facility int


decompiled_code:
unsigned __int64 bsock_syslog(unsigned int a1, unsigned int a2, __int64 a3, ...)
{
char *v5; // rdi
__int64 i; // rcx
char *v7; // rdi
__int64 j; // rcx
int v9; // ebx
__int64 v10; // rax
__int64 v11; // rax
gcc_va_list va; // [rsp+8h] [rbp-640h] BYREF
struct iovec iovec; // [rsp+20h] [rbp-628h] BYREF
const char *v15; // [rsp+30h] [rbp-618h]
__int64 v16; // [rsp+38h] [rbp-610h]
__int64 *v17; // [rsp+40h] [rbp-608h]
__int64 v18; // [rsp+48h] [rbp-600h]
char *v19; // [rsp+50h] [rbp-5F8h]
size_t v20; // [rsp+58h] [rbp-5F0h]
const char *v21; // [rsp+60h] [rbp-5E8h]
__int64 v22; // [rsp+68h] [rbp-5E0h]
char s[8]; // [rsp+78h] [rbp-5D0h] BYREF
__int64 v24; // [rsp+80h] [rbp-5C8h]
char v25; // [rsp+88h] [rbp-5C0h] BYREF
__int64 v26[2]; // [rsp+178h] [rbp-4D0h] BYREF
char v27; // [rsp+188h] [rbp-4C0h] BYREF
unsigned __int64 v28; // [rsp+578h] [rbp-D0h]

v28 = __readfsqword(0x28u);
v26[0] = 0LL;
v26[1] = 0LL;
v5 = &v27;
for ( i = 252LL; i; --i )
{
*(_DWORD *)v5 = 0;
v5 += 4;
}
*(_QWORD *)s = 8250LL;
v24 = 0LL;
v7 = &v25;
for ( j = 60LL; j; --j )
{
*(_DWORD *)v7 = 0;
v7 += 4;
}
if ( !a1 || (unsigned int)_xpg_strerror_r(a1, &s[2], 254LL) )
s[0] = 0;
va_start(va, a3);
v9 = _vsnprintf_chk(v26, 1024LL, 1LL, 1024LL, a3, va);
if ( bsock_syslog_level == 2 || (_syslog_chk(a2, 1LL, "%s%s", (const char *)v26, s), bsock_syslog_level) )
{
iovec.iov_base = (void *)bsock_syslog_ident;
v10 = bsock_syslog_identlen;
iovec.iov_len = bsock_syslog_identlen;
v15 = ": ";
if ( bsock_syslog_identlen )
v10 = 2LL;
v16 = v10;
v17 = v26;
v11 = v9;
if ( (unsigned __int64)v9 > 0x400 )
v11 = 1024LL;
v18 = v11;
v19 = s;
v20 = strlen(s);
v21 = "\n";
v22 = 1LL;
writev(bsock_syslog_logfd, &iovec, 5);
}
*_errno_location() = a1;
return v28 - __readfsqword(0x28u);
}

canonical_decompiled_code:
unsigned __int64 func3(unsigned int a1, unsigned int a2, __int64 a3, ...)
{
char *v5; // rdi
__int64 i; // rcx
char *v7; // rdi
__int64 j; // rcx
int v9; // ebx
__int64 v10; // rax
__int64 v11; // rax
gcc_va_list va; // [rsp+8h] [rbp-640h] BYREF
struct iovec iovec; // [rsp+20h] [rbp-628h] BYREF
const char *v15; // [rsp+30h] [rbp-618h]
__int64 v16; // [rsp+38h] [rbp-610h]
__int64 *v17; // [rsp+40h] [rbp-608h]
__int64 v18; // [rsp+48h] [rbp-600h]
char *v19; // [rsp+50h] [rbp-5F8h]
size_t v20; // [rsp+58h] [rbp-5F0h]
const char *v21; // [rsp+60h] [rbp-5E8h]
__int64 v22; // [rsp+68h] [rbp-5E0h]
char s[8]; // [rsp+78h] [rbp-5D0h] BYREF
__int64 v24; // [rsp+80h] [rbp-5C8h]
char v25; // [rsp+88h] [rbp-5C0h] BYREF
__int64 v26[2]; // [rsp+178h] [rbp-4D0h] BYREF
char v27; // [rsp+188h] [rbp-4C0h] BYREF
unsigned __int64 v28; // [rsp+578h] [rbp-D0h]

v28 = __readfsqword(0x28u);
v26[0] = 0LL;
v26[1] = 0LL;
v5 = &v27;
for ( i = 252LL; i; --i )
{
*(_DWORD *)v5 = 0;
v5 += 4;
}
*(_QWORD *)s = 8250LL;
v24 = 0LL;
v7 = &v25;
for ( j = 60LL; j; --j )
{
*(_DWORD *)v7 = 0;
v7 += 4;
}
if ( !a1 || (unsigned int)_xpg_strerror_r(a1, &s[2], 254LL) )
s[0] = 0;
va_start(va, a3);
v9 = _vsnprintf_chk(v26, 1024LL, 1LL, 1024LL, a3, va);
if ( bsock_syslog_level == 2 || (_syslog_chk(a2, 1LL, "%s%s", (const char *)v26, s), bsock_syslog_level) )
{
iovec.iov_base = (void *)bsock_syslog_ident;
v10 = bsock_syslog_identlen;
iovec.iov_len = bsock_syslog_identlen;
v15 = ": ";
if ( bsock_syslog_identlen )
v10 = 2LL;
v16 = v10;
v17 = v26;
v11 = v9;
if ( (unsigned __int64)v9 > 0x400 )
v11 = 1024LL;
v18 = v11;
v19 = s;
v20 = strlen(s);
v21 = "\n";
v22 = 1LL;
writev(bsock_syslog_logfd, &iovec, 5);
}
*_errno_location() = a1;
return v28 - __readfsqword(0x28u);
}

original_code:
__attribute__((__cold__))
__attribute__ ((__noinline__))
__attribute__((__format__ (printf,3,4)))
void
bsock_syslog (const int errnum, const int priority,
              const char * const restrict fmt, ...)
{
    va_list ap;
    size_t len;
    char str[1024] = "";
    char buf[256] = ": ";
    if (0 == errnum || 0 != strerror_r(errnum, buf+2, sizeof(buf)-2))
        buf[0] = '\0';
    __builtin_va_start(ap,fmt);
    len = (size_t)vsnprintf(str, sizeof(str), fmt, ap);
    __builtin_va_end(ap);
    if (bsock_syslog_level != BSOCK_SYSLOG_PERROR_NOSYSLOG)
        syslog(priority, "%s%s", str, buf);
    if (bsock_syslog_level != BSOCK_SYSLOG_DAEMON) {
        struct iovec iov[5] = { { (void *)(uintptr_t)bsock_syslog_ident,
                                  bsock_syslog_identlen },
                                { (void *)(uintptr_t)": ",
                                  bsock_syslog_identlen ? 2 : 0 },
                                { str, len < sizeof(str) ? len : sizeof(str) },
                                { buf, strlen(buf) },
                                { (void *)(uintptr_t)"\n", 1 } };
        if (writev(bsock_syslog_logfd, iov, sizeof(iov)/sizeof(struct iovec))){}
    }
    (*__errno_location ()) = errnum;
}

canonical_original_code:
void bsock_syslog(int errnum, int priority, char *fmt, ...)
{
    __builtin_va_list ap;
    unsigned long len;
    char str[1024] = "";
    char buf[256] = ": ";
    if (0 == errnum || 0 != strerror_r(errnum, buf+2, sizeof(buf)-2))
        buf[0] = '\0';
    __builtin_va_start(ap,fmt);
    len = (unsigned long)vsnprintf(str, sizeof(str), fmt, ap);
    __builtin_va_end(ap);
    if (bsock_syslog_level != BSOCK_SYSLOG_PERROR_NOSYSLOG)
        syslog(priority, "%s%s", str, buf);
    if (bsock_syslog_level != BSOCK_SYSLOG_DAEMON) {
        struct iovec iov[5] = { { (void *)(uintptr_t)bsock_syslog_ident,
                                  bsock_syslog_identlen },
                                { (void *)(uintptr_t)": ",
                                  bsock_syslog_identlen ? 2 : 0 },
                                { str, len < sizeof(str) ? len : sizeof(str) },
                                { buf, strlen(buf) },
                                { (void *)(uintptr_t)"\n", 1 } };
        if (writev(bsock_syslog_logfd, iov, sizeof(iov)/sizeof(struct iovec))){}
    }
    (*__errno_location ()) = errnum;
}

variable_types: errnum int

variable_types: priority int

variable_types:
fmt
char *

variable_types:
len
unsigned long

variable_types:
str
char [1024]

variable_types:
buf
char [256]

variable_types: struct iovec { void *iov_base; unsigned long iov_len; } [5]


decompiled_code:
void __fastcall bsock_unix_recv_ancillary(struct msghdr *mhdr, __int64 a2, unsigned int *a3)
{
struct cmsghdr *v3; // rbx
char *v4; // rbp
unsigned __int64 v5; // r12
unsigned __int64 v6; // r12
unsigned int v7; // er15
unsigned int v9; // [rsp+10h] [rbp-48h]
unsigned int v10; // [rsp+14h] [rbp-44h]

if ( a3 )
{
v10 = *a3;
v9 = 0;
if ( mhdr->msg_controllen <= 0xF )
{
LABEL_16:
*a3 = v9;
return;
}
}
else
{
if ( mhdr->msg_controllen <= 0xF )
return;
v10 = 0;
}
v3 = (struct cmsghdr *)mhdr->msg_control;
if ( v3 )
{
v9 = 0;
do
{
if ( *(_QWORD *)&v3->cmsg_level == 0x100000001LL )
{
v6 = (v3->cmsg_len - 16) >> 2;
v7 = v10 - v9;
if ( v10 != v9 )
{
if ( v7 > (unsigned int)v6 )
v7 = (v3->cmsg_len - 16) >> 2;
memcpy((void *)(a2 + 4LL * v9), v3->__cmsg_data, 4LL * v7);
v9 += v7;
}
if ( (unsigned int)v6 > v7 )
{
v4 = (char *)v3 + 4 * v7;
v5 = (unsigned __int64)&v3->cmsg_len + 4 * v7 + 4 * (unsigned __int64)((_DWORD)v6 - 1 - v7) + 4;
do
{
nointr_close(*((_DWORD *)v4 + 4));
v4 += 4;
}
while ( v4 != (char *)v5 );
}
}
v3 = _cmsg_nxthdr(mhdr, v3);
}
while ( v3 );
}
else
{
v9 = 0;
}
if ( a3 )
goto LABEL_16;
}

canonical_decompiled_code:
void __fastcall func1(struct msghdr *mhdr, __int64 a2, unsigned int *a3)
{
struct cmsghdr *v3; // rbx
char *v4; // rbp
unsigned __int64 v5; // r12
unsigned __int64 v6; // r12
unsigned int v7; // er15
unsigned int v9; // [rsp+10h] [rbp-48h]
unsigned int v10; // [rsp+14h] [rbp-44h]

if ( a3 )
{
v10 = *a3;
v9 = 0;
if ( mhdr->msg_controllen <= 0xF )
{
LABEL_16:
*a3 = v9;
return;
}
}
else
{
if ( mhdr->msg_controllen <= 0xF )
return;
v10 = 0;
}
v3 = (struct cmsghdr *)mhdr->msg_control;
if ( v3 )
{
v9 = 0;
do
{
if ( *(_QWORD *)&v3->cmsg_level == 0x100000001LL )
{
v6 = (v3->cmsg_len - 16) >> 2;
v7 = v10 - v9;
if ( v10 != v9 )
{
if ( v7 > (unsigned int)v6 )
v7 = (v3->cmsg_len - 16) >> 2;
memcpy((void *)(a2 + 4LL * v9), v3->__cmsg_data, 4LL * v7);
v9 += v7;
}
if ( (unsigned int)v6 > v7 )
{
v4 = (char *)v3 + 4 * v7;
v5 = (unsigned __int64)&v3->cmsg_len + 4 * v7 + 4 * (unsigned __int64)((_DWORD)v6 - 1 - v7) + 4;
do
{
func0(*((_DWORD *)v4 + 4));
v4 += 4;
}
while ( v4 != (char *)v5 );
}
}
v3 = _cmsg_nxthdr(mhdr, v3);
}
while ( v3 );
}
else
{
v9 = 0;
}
if ( a3 )
goto LABEL_16;
}

original_code:
__attribute__((__nonnull__ (1)))
static void
bsock_unix_recv_ancillary (struct msghdr * const restrict msg,
                           int * const restrict rfds,
                           unsigned int * const restrict nrfdsp)
{
    struct cmsghdr *cmsg;
    unsigned int nrfd = 0;
    const unsigned int nrfds = nrfdsp != ((void *)0) ? *nrfdsp : 0;
    for (cmsg = ((size_t) (msg)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (msg)->msg_control : (struct cmsghdr *) 0); ((void *)0) != cmsg; cmsg = __cmsg_nxthdr (msg, cmsg)){
        if (cmsg->cmsg_type == SCM_RIGHTS && cmsg->cmsg_level == 1) {
            const int * const restrict fds = (int *)((cmsg)->__cmsg_data);
            const unsigned int nfds = (cmsg->cmsg_len-((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (0)))/sizeof(int);
            unsigned int nfd = nrfds - nrfd;
            if (0 !=nfd) {
                if (nfd > nfds)
                    nfd = nfds;
                memcpy(rfds+nrfd, fds, nfd*sizeof(int));
                nrfd += nfd;
            }
            while (nfd < nfds)
                nointr_close(fds[nfd++]);
        }
    }
    if (nrfdsp != ((void *)0))
        *nrfdsp = nrfd;
}

canonical_original_code:
static void bsock_unix_recv_ancillary(struct msghdr *msg, int *rfds, unsigned int *nrfdsp)
{
    struct cmsghdr *cmsg;
    unsigned int nrfd = 0;
    unsigned int nrfds = nrfdsp != ((void *)0) ? *nrfdsp : 0;
    for (cmsg = ((unsigned long) (msg)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (msg)->msg_control : (struct cmsghdr *) 0); ((void *)0) != cmsg; cmsg = __cmsg_nxthdr (msg, cmsg)){
        if (cmsg->cmsg_type == SCM_RIGHTS && cmsg->cmsg_level == 1) {
            int *fds = (int *)((cmsg)->__cmsg_data);
            unsigned int nfds = (cmsg->cmsg_len-((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (0)))/sizeof(int);
            unsigned int nfd = nrfds - nrfd;
            if (0 !=nfd) {
                if (nfd > nfds)
                    nfd = nfds;
                memcpy(rfds+nrfd, fds, nfd*sizeof(int));
                nrfd += nfd;
            }
            while (nfd < nfds)
                nointr_close(fds[nfd++]);
        }
    }
    if (nrfdsp != ((void *)0))
        *nrfdsp = nrfd;
}

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

variable_types:
rfds
int *

variable_types:
nrfdsp
unsigned int *

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

variable_types:
nrfd
unsigned int

variable_types:
nrfds
unsigned int

variable_types:
fds
int *

variable_types:
nfds
unsigned int

variable_types:
nfd
unsigned int

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


decompiled_code:
__int64 __fastcall bsock_unix_socket_connect(const char *a1)
{
size_t v1; // rax
int v2; // eax
unsigned int v3; // ebx
int *v5; // rbp
int v6; // er12
struct sockaddr v7[7]; // [rsp+0h] [rbp-98h] BYREF
unsigned __int64 v8; // [rsp+78h] [rbp-20h]

v8 = __readfsqword(0x28u);
v1 = strlen(a1);
if ( v1 > 0x6B )
{
*_errno_location() = 22;
v3 = -1;
}
else
{
v7[0].sa_family = 1;
_memcpy_chk(v7[0].sa_data, a1, v1 + 1, 108LL);
v2 = socket(1, 1, 0);
v3 = v2;
if ( v2 != -1 && connect(v2, v7, 0x6Eu) )
{
v5 = _errno_location();
v6 = *v5;
nointr_close(v3);
*v5 = v6;
v3 = -1;
}
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func4(const char *a1)
{
size_t v1; // rax
int v2; // eax
unsigned int v3; // ebx
int *v5; // rbp
int v6; // er12
struct sockaddr v7[7]; // [rsp+0h] [rbp-98h] BYREF
unsigned __int64 v8; // [rsp+78h] [rbp-20h]

v8 = __readfsqword(0x28u);
v1 = strlen(a1);
if ( v1 > 0x6B )
{
*_errno_location() = 22;
v3 = -1;
}
else
{
v7[0].sa_family = 1;
_memcpy_chk(v7[0].sa_data, a1, v1 + 1, 108LL);
v2 = socket(1, 1, 0);
v3 = v2;
if ( v2 != -1 && connect(v2, v7, 0x6Eu) )
{
v5 = _errno_location();
v6 = *v5;
func0(v3);
*v5 = v6;
v3 = -1;
}
}
return v3;
}

original_code:
__attribute__((__nonnull__ ))
int
bsock_unix_socket_connect (const char * const restrict sockpath)
{
    struct sockaddr_un saddr;
    const size_t len = strlen(sockpath);
    int fd;
    if (len >= sizeof(saddr.sun_path)) {
        (*__errno_location ()) = 22;
        return -1;
    }
    saddr.sun_family = 1;
    memcpy(saddr.sun_path, sockpath, len+1);
    if ( -1 != (fd = socket(1, SOCK_STREAM, 0))
        && 0 == connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)))
        return fd;
    if (-1 != fd) {
        const int errnum = (*__errno_location ());
        nointr_close(fd);
        (*__errno_location ()) = errnum;
    }
    return -1;
}

canonical_original_code:
int bsock_unix_socket_connect(char *sockpath)
{
    struct sockaddr_un saddr;
    unsigned long len = strlen(sockpath);
    int fd;
    if (len >= sizeof(saddr.sun_path)) {
        (*__errno_location ()) = 22;
        return -1;
    }
    saddr.sun_family = 1;
    memcpy(saddr.sun_path, sockpath, len+1);
    if ( -1 != (fd = socket(1, SOCK_STREAM, 0))
        && 0 == connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)))
        return fd;
    if (-1 != fd) {
        int errnum = (*__errno_location ());
        nointr_close(fd);
        (*__errno_location ()) = errnum;
    }
    return -1;
}

variable_types:
sockpath
char *

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

variable_types: fd int

return_type: int

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


decompiled_code:
__int64 __fastcall bsock_unix_socket_bind_listen(char *name, int *a2)
{
size_t v2; // rax
int v3; // eax
unsigned int v4; // ebx
int v6; // eax
int *v7; // r13
int *v8; // rbp
int v9; // er12
unsigned int v10; // eax
int v11; // eax
int v12; // eax
struct sockaddr v13[7]; // [rsp+0h] [rbp-A8h] BYREF
unsigned __int64 v14; // [rsp+78h] [rbp-30h]

v14 = __readfsqword(0x28u);
v2 = strlen(name);
if ( v2 > 0x6B )
{
*_errno_location() = 22;
v4 = -1;
}
else
{
v13[0].sa_family = 1;
_memcpy_chk(v13[0].sa_data, name, v2 + 1, 108LL);
v3 = socket(1, 1, 0);
v4 = v3;
if ( v3 != -1 )
{
v6 = bind(v3, v13, 0x6Eu);
*a2 = v6;
if ( v6 || listen(v4, 128) )
{
if ( (v4 & 0x80000000) != 0
|| (v7 = _errno_location(), *v7 != 98)
|| (v10 = fcntl64(v4, 3LL, 0LL), v10 == -1)
|| (BYTE1(v10) |= 8u, (unsigned int)fcntl64(v4, 4LL, v10))
|| !connect(v4, v13, 0x6Eu)
|| *v7 != 111
|| unlink(name)
|| (unsigned int)nointr_close(v4)
|| (v11 = socket(1, 1, 0), v4 = v11, v11 != -1)
&& ((v12 = bind(v11, v13, 0x6Eu), (*a2 = v12) != 0) || listen(v4, 128)) )
{
v8 = _errno_location();
v9 = *v8;
nointr_close(v4);
*v8 = v9;
v4 = -1;
}
}
}
}
return v4;
}

original_code:
__attribute__((__nonnull__ ))
int
bsock_unix_socket_bind_listen (const char * const restrict sockpath,
                               int * const restrict bound)
{
    struct sockaddr_un saddr;
    const size_t len = strlen(sockpath);
    int fd, flags;
    if (len >= sizeof(saddr.sun_path)) {
        (*__errno_location ()) = 22;
        return -1;
    }
    saddr.sun_family = 1;
    memcpy(saddr.sun_path, sockpath, len+1);
    if ( -1 != (fd = socket(1, SOCK_STREAM, 0))
        && 0 == (*bound = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)))
        && 0 == listen(fd, 128))
        return fd;
    if ( fd >= 0 && (*__errno_location ()) == 98
        &&-1 != (flags = fcntl(fd, 3, 0))
        && 0 == fcntl(fd, 4, flags|04000)
        && 0 != connect(fd, (struct sockaddr *)&saddr, sizeof(saddr))
        && (*__errno_location ()) == 111
        && 0 == unlink(sockpath)
        && 0 == nointr_close(fd)
        &&-1 != (fd = socket(1, SOCK_STREAM, 0))
        && 0 == (*bound = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)))
        && 0 == listen(fd, 128))
        return fd;
    if (-1 != fd) {
        const int errnum = (*__errno_location ());
        nointr_close(fd);
        (*__errno_location ()) = errnum;
    }
    return -1;
}

canonical_original_code:
int bsock_unix_socket_bind_listen(char *sockpath, int *bound)
{
    struct sockaddr_un saddr;
    unsigned long len = strlen(sockpath);
    int fd; int flags;
    if (len >= sizeof(saddr.sun_path)) {
        (*__errno_location ()) = 22;
        return -1;
    }
    saddr.sun_family = 1;
    memcpy(saddr.sun_path, sockpath, len+1);
    if ( -1 != (fd = socket(1, SOCK_STREAM, 0))
        && 0 == (*bound = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)))
        && 0 == listen(fd, 128))
        return fd;
    if ( fd >= 0 && (*__errno_location ()) == 98
        &&-1 != (flags = fcntl(fd, 3, 0))
        && 0 == fcntl(fd, 4, flags|04000)
        && 0 != connect(fd, (struct sockaddr *)&saddr, sizeof(saddr))
        && (*__errno_location ()) == 111
        && 0 == unlink(sockpath)
        && 0 == nointr_close(fd)
        &&-1 != (fd = socket(1, SOCK_STREAM, 0))
        && 0 == (*bound = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)))
        && 0 == listen(fd, 128))
        return fd;
    if (-1 != fd) {
        int errnum = (*__errno_location ());
        nointr_close(fd);
        (*__errno_location ()) = errnum;
    }
    return -1;
}

variable_types:
bound
int *

variable_types: flags int

variable_types: errnum int

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


original_code:
__attribute__((__nonnull__ (4)))
ssize_t
bsock_unix_recv_fds (const int fd,
                     int * const restrict rfds,
                     unsigned int * const restrict nrfds,
                     struct iovec * const restrict iov,
                     const size_t iovlen)
{
    char ctrlbuf[10240];
    struct msghdr msg = {
      .msg_name = ((void *)0),
      .msg_namelen = 0,
      .msg_iov = iov,
      .msg_iovlen = iovlen,
      .msg_control = ctrlbuf,
      .msg_controllen = sizeof(ctrlbuf),
      .msg_flags = 0
    };
    return bsock_unix_recv_fds_msghdr(fd, rfds, nrfds, &msg);
}

canonical_original_code:
long bsock_unix_recv_fds(int fd, int *rfds, unsigned int *nrfds, struct iovec *iov, unsigned long iovlen)
{
    char ctrlbuf[10240];
    struct msghdr msg = {
      .msg_name = ((void *)0),
      .msg_namelen = 0,
      .msg_iov = iov,
      .msg_iovlen = iovlen,
      .msg_control = ctrlbuf,
      .msg_controllen = sizeof(ctrlbuf),
      .msg_flags = 0
    };
    return bsock_unix_recv_fds_msghdr(fd, rfds, nrfds, &msg);
}

variable_types:
nrfds
unsigned int *

variable_types:
iovlen
unsigned long

variable_types:
ctrlbuf
char [10240]

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

return_type: long


original_code:
__attribute__((__nonnull__ (4,6)))
ssize_t
bsock_unix_recv_fds_ex (const int fd,
                        int * const restrict rfds,
                        unsigned int * const restrict nrfds,
                        struct iovec * const restrict iov,
                        const size_t iovlen,
                        char * const restrict ctrlbuf,
                        const size_t ctrlbuf_sz)
{
    struct msghdr msg = {
      .msg_name = ((void *)0),
      .msg_namelen = 0,
      .msg_iov = iov,
      .msg_iovlen = iovlen,
      .msg_control = ctrlbuf,
      .msg_controllen = ctrlbuf_sz,
      .msg_flags = 0
    };
    return bsock_unix_recv_fds_msghdr(fd, rfds, nrfds, &msg);
}

canonical_original_code:
long bsock_unix_recv_fds_ex(int fd, int *rfds, unsigned int *nrfds, struct iovec *iov, unsigned long iovlen, char *ctrlbuf, unsigned long ctrlbuf_sz)
{
    struct msghdr msg = {
      .msg_name = ((void *)0),
      .msg_namelen = 0,
      .msg_iov = iov,
      .msg_iovlen = iovlen,
      .msg_control = ctrlbuf,
      .msg_controllen = ctrlbuf_sz,
      .msg_flags = 0
    };
    return bsock_unix_recv_fds_msghdr(fd, rfds, nrfds, &msg);
}

variable_types:
rfds
int *

variable_types: struct iovec { void *iov_base; unsigned long iov_len; } *

variable_types:
ctrlbuf
char *


decompiled_code:
ssize_t __fastcall bsock_unix_send_fds(int fd, const void *a2, int a3, iovec *a4, size_t a5)
{
unsigned int v5; // edx
void *v6; // rsp
__int64 v7; // r12
ssize_t v8; // r12
struct msghdr message; // [rsp+0h] [rbp-60h] BYREF
unsigned __int64 v11; // [rsp+38h] [rbp-28h]

v11 = __readfsqword(0x28u);
message.msg_name = 0LL;
message.msg_namelen = 0;
message.msg_iov = a4;
message.msg_iovlen = a5;
message.msg_control = 0LL;
message.msg_controllen = 0LL;
message.msg_flags = 0;
v5 = 4 * a3;
v6 = alloca(16 * ((((v5 + 4 + 7LL) & 0xFFFFFFFFFFFFFFF8LL) + 31) >> 4));
if ( v5 )
{
message.msg_control = &message;
v7 = v5;
memcpy(&message.msg_iov, a2, v5);
message.msg_namelen = 1;
*(&message.msg_namelen + 1) = 1;
message.msg_controllen = v7 + 16;
message.msg_name = (void *)(v7 + 16);
}
do
v8 = sendmsg(fd, &message, 16448);
while ( v8 == -1 && *_errno_location() == 4 );
return v8;
}

canonical_decompiled_code:
ssize_t __fastcall func8(int fd, const void *a2, int a3, iovec *a4, size_t a5)
{
unsigned int v5; // edx
void *v6; // rsp
__int64 v7; // r12
ssize_t v8; // r12
struct msghdr message; // [rsp+0h] [rbp-60h] BYREF
unsigned __int64 v11; // [rsp+38h] [rbp-28h]

v11 = __readfsqword(0x28u);
message.msg_name = 0LL;
message.msg_namelen = 0;
message.msg_iov = a4;
message.msg_iovlen = a5;
message.msg_control = 0LL;
message.msg_controllen = 0LL;
message.msg_flags = 0;
v5 = 4 * a3;
v6 = alloca(16 * ((((v5 + 4 + 7LL) & 0xFFFFFFFFFFFFFFF8LL) + 31) >> 4));
if ( v5 )
{
message.msg_control = &message;
v7 = v5;
memcpy(&message.msg_iov, a2, v5);
message.msg_namelen = 1;
*(&message.msg_namelen + 1) = 1;
message.msg_controllen = v7 + 16;
message.msg_name = (void *)(v7 + 16);
}
do
v8 = sendmsg(fd, &message, 16448);
while ( v8 == -1 && *_errno_location() == 4 );
return v8;
}

original_code:
__attribute__((__nonnull__ (4)))
ssize_t
bsock_unix_send_fds (const int fd,
                     const int * const restrict sfds,
                     unsigned int nsfds,
                     struct iovec * const restrict iov,
                     const size_t iovlen)
{
    ssize_t w;
    struct msghdr msg = {
      .msg_name = ((void *)0),
      .msg_namelen = 0,
      .msg_iov = iov,
      .msg_iovlen = iovlen,
      .msg_control = ((void *)0),
      .msg_controllen = 0,
      .msg_flags = 0
    };
    char ctrlbuf[(((((nsfds*=sizeof(int))+4) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)))];
    if (0 != nsfds) {
        struct cmsghdr * restrict cmsg;
        msg.msg_control = ctrlbuf;
        msg.msg_controllen = sizeof(ctrlbuf);
        cmsg = ((size_t) (&msg)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&msg)->msg_control : (struct cmsghdr *) 0);
        memcpy(((cmsg)->__cmsg_data), sfds, nsfds);
        cmsg->cmsg_level = 1;
        cmsg->cmsg_type = SCM_RIGHTS;
        cmsg->cmsg_len = msg.msg_controllen = ((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (nsfds));
    }
    do {(w=sendmsg(fd,&msg,MSG_DONTWAIT|MSG_NOSIGNAL));} while (__builtin_expect((-1 == w),0) && __builtin_expect((*__errno_location ())==4, 1));
    return w;
}

canonical_original_code:
long bsock_unix_send_fds(int fd, int *sfds, unsigned int nsfds, struct iovec *iov, unsigned long iovlen)
{
    long w;
    struct msghdr msg = {
      .msg_name = ((void *)0),
      .msg_namelen = 0,
      .msg_iov = iov,
      .msg_iovlen = iovlen,
      .msg_control = ((void *)0),
      .msg_controllen = 0,
      .msg_flags = 0
    };
    char ctrlbuf[-1];
    if (0 != nsfds) {
        struct cmsghdr *cmsg;
        msg.msg_control = ctrlbuf;
        msg.msg_controllen = sizeof(ctrlbuf);
        cmsg = ((unsigned long) (&msg)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&msg)->msg_control : (struct cmsghdr *) 0);
        memcpy(((cmsg)->__cmsg_data), sfds, nsfds);
        cmsg->cmsg_level = 1;
        cmsg->cmsg_type = SCM_RIGHTS;
        cmsg->cmsg_len = msg.msg_controllen = ((((sizeof (struct cmsghdr)) + sizeof (unsigned long) - 1) & (unsigned long) ~(sizeof (unsigned long) - 1)) + (nsfds));
    }
    do {(w=sendmsg(fd,&msg,MSG_DONTWAIT|MSG_NOSIGNAL));} while (__builtin_expect((-1 == w),0) && __builtin_expect((*__errno_location ())==4, 1));
    return w;
}

variable_types:
sfds
int *

variable_types:
nsfds
unsigned int

variable_types: w long

variable_types:
ctrlbuf
char [-1]

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

user_defined_types: struct iovec { void *iov_base; unsigned long iov_len; }


decompiled_code:
int __fastcall bsock_unix_getpeereid(int a1, _DWORD *a2, _DWORD *a3)
{
return getpeereid(a1, a2, a3);
}

canonical_decompiled_code:
int __fastcall func9(int a1, _DWORD *a2, _DWORD *a3)
{
return func3(a1, a2, a3);
}

original_code:
__attribute__((__nonnull__ ))
int
bsock_unix_getpeereid (const int s,
                       uid_t * const restrict euid,
                       gid_t * const restrict egid)
{
    return getpeereid(s, euid, egid);
}

canonical_original_code:
int bsock_unix_getpeereid(int s, unsigned int *euid, unsigned int *egid)
{
    return getpeereid(s, euid, egid);
}

variable_types: s int

variable_types:
euid
unsigned int *

variable_types:
egid
unsigned int *


decompiled_code:
__int64 __fastcall nointr_close(int fd)
{
unsigned int v1; // ebx

do
v1 = close(fd);
while ( v1 && *_errno_location() == 4 );
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func0(int fd)
{
unsigned int v1; // ebx

do
v1 = close(fd);
while ( v1 && *_errno_location() == 4 );
return v1;
}

original_code:
static int
nointr_close (const int fd)
{ int r; do {(r = close(fd));} while (__builtin_expect((r != 0),0) && __builtin_expect((*__errno_location ())==4, 1)); return r; }

canonical_original_code:
static int nointr_close(int fd)
{ int r; do {(r = close(fd));} while (__builtin_expect((r != 0),0) && __builtin_expect((*__errno_location ())==4, 1)); return r; }

variable_types: fd int

variable_types: r int


decompiled_code:
void bsock_daemon_sa_ignore()
{
;
}

canonical_decompiled_code:
void func0()
{
;
}

original_code:
static void
bsock_daemon_sa_ignore (int signum __attribute__((__unused__)))
{
}

canonical_original_code:
static void bsock_daemon_sa_ignore(int signum)
{
}

variable_types: signum int


decompiled_code:
__int64 bsock_daemon_signal_init()
{
__int64 result; // rax
int *v1; // rax
int *v2; // rax
struct sigaction v3; // [rsp+0h] [rbp-A8h] BYREF

sigemptyset(&v3.sa_mask);
if ( sigprocmask(2, &v3.sa_mask, 0LL) )
{
v1 = _errno_location();
bsock_syslog((unsigned int)*v1, 3LL, "sigprocmask");
result = 0LL;
}
else
{
v3.sa_handler = 0LL;
v3.sa_flags = 0;
if ( sigaction(14, &v3, 0LL) )
goto LABEL_11;
v3.sa_handler = 0LL;
v3.sa_flags = 268435457;
if ( sigaction(17, &v3, 0LL)
|| (v3.sa_handler = (__sighandler_t)bsock_daemon_sa_ignore, v3.sa_flags = 0, sigaction(13, &v3, 0LL))
|| (v3.sa_handler = (__sighandler_t)bsock_daemon_sa_handler, v3.sa_flags = 0x10000000, sigaction(1, &v3, 0LL))
|| (v3.sa_handler = (__sighandler_t)bsock_daemon_sa_handler, v3.sa_flags = 0, sigaction(2, &v3, 0LL))
|| sigaction(3, &v3, 0LL)
|| sigaction(15, &v3, 0LL) )
{
LABEL_11:
v2 = _errno_location();
bsock_syslog((unsigned int)*v2, 3LL, "sigaction");
result = 0LL;
}
else
{
result = 1LL;
}
}
return result;
}

canonical_decompiled_code:
__int64 func1()
{
__int64 result; // rax
int *v1; // rax
int *v2; // rax
struct sigaction v3; // [rsp+0h] [rbp-A8h] BYREF

sigemptyset(&v3.sa_mask);
if ( sigprocmask(2, &v3.sa_mask, 0LL) )
{
v1 = _errno_location();
bsock_syslog((unsigned int)*v1, 3LL, "sigprocmask");
result = 0LL;
}
else
{
v3.sa_handler = 0LL;
v3.sa_flags = 0;
if ( sigaction(14, &v3, 0LL) )
goto LABEL_11;
v3.sa_handler = 0LL;
v3.sa_flags = 268435457;
if ( sigaction(17, &v3, 0LL)
|| (v3.sa_handler = (__sighandler_t)bsock_daemon_sa_ignore, v3.sa_flags = 0, sigaction(13, &v3, 0LL))
|| (v3.sa_handler = (__sighandler_t)bsock_daemon_sa_handler, v3.sa_flags = 0x10000000, sigaction(1, &v3, 0LL))
|| (v3.sa_handler = (__sighandler_t)bsock_daemon_sa_handler, v3.sa_flags = 0, sigaction(2, &v3, 0LL))
|| sigaction(3, &v3, 0LL)
|| sigaction(15, &v3, 0LL) )
{
LABEL_11:
v2 = _errno_location();
bsock_syslog((unsigned int)*v2, 3LL, "sigaction");
result = 0LL;
}
else
{
result = 1LL;
}
}
return result;
}

original_code:
static _Bool
bsock_daemon_signal_init (void)
{
    struct sigaction act;
    (void) sigemptyset(&act.sa_mask);
    if (0 != sigprocmask(2, &act.sa_mask, (sigset_t *) ((void *)0))) {
        bsock_syslog((*__errno_location ()), 3, "sigprocmask");
        return 0;
    }
    act.__sigaction_handler.sa_handler = ((__sighandler_t) 0);
    act.sa_flags = 0;
    if (sigaction(14, &act, (struct sigaction *) ((void *)0)) != 0) {
        bsock_syslog((*__errno_location ()), 3, "sigaction");
        return 0;
    }
    act.__sigaction_handler.sa_handler = ((__sighandler_t) 0);
    act.sa_flags = 0x10000000 | 1;
    if (sigaction(17, &act, (struct sigaction *) ((void *)0)) != 0) {
        bsock_syslog((*__errno_location ()), 3, "sigaction");
        return 0;
    }
    act.__sigaction_handler.sa_handler = bsock_daemon_sa_ignore;
    act.sa_flags = 0;
    if (sigaction(13, &act, (struct sigaction *) ((void *)0)) != 0) {
        bsock_syslog((*__errno_location ()), 3, "sigaction");
        return 0;
    }
    act.__sigaction_handler.sa_handler = bsock_daemon_sa_handler;
    act.sa_flags = 0x10000000;
    if (sigaction(1, &act, (struct sigaction *) ((void *)0)) != 0) {
        bsock_syslog((*__errno_location ()), 3, "sigaction");
        return 0;
    }
    act.__sigaction_handler.sa_handler = bsock_daemon_sa_handler;
    act.sa_flags = 0;
    if ( sigaction(2, &act, (struct sigaction *) ((void *)0)) != 0
        || sigaction(3, &act, (struct sigaction *) ((void *)0)) != 0
        || sigaction(15, &act, (struct sigaction *) ((void *)0)) != 0) {
        bsock_syslog((*__errno_location ()), 3, "sigaction");
        return 0;
    }
    return 1;
}

canonical_original_code:
static _Bool bsock_daemon_signal_init()
{
    struct sigaction act;
    (void) sigemptyset(&act.sa_mask);
    if (0 != sigprocmask(2, &act.sa_mask, (struct __sigset_t *) ((void *)0))) {
        bsock_syslog((*__errno_location ()), 3, "sigprocmask");
        return 0;
    }
    act.__sigaction_handler.sa_handler = ((void (*)(int)) 0);
    act.sa_flags = 0;
    if (sigaction(14, &act, (struct sigaction *) ((void *)0)) != 0) {
        bsock_syslog((*__errno_location ()), 3, "sigaction");
        return 0;
    }
    act.__sigaction_handler.sa_handler = ((void (*)(int)) 0);
    act.sa_flags = 0x10000000 | 1;
    if (sigaction(17, &act, (struct sigaction *) ((void *)0)) != 0) {
        bsock_syslog((*__errno_location ()), 3, "sigaction");
        return 0;
    }
    act.__sigaction_handler.sa_handler = bsock_daemon_sa_ignore;
    act.sa_flags = 0;
    if (sigaction(13, &act, (struct sigaction *) ((void *)0)) != 0) {
        bsock_syslog((*__errno_location ()), 3, "sigaction");
        return 0;
    }
    act.__sigaction_handler.sa_handler = bsock_daemon_sa_handler;
    act.sa_flags = 0x10000000;
    if (sigaction(1, &act, (struct sigaction *) ((void *)0)) != 0) {
        bsock_syslog((*__errno_location ()), 3, "sigaction");
        return 0;
    }
    act.__sigaction_handler.sa_handler = bsock_daemon_sa_handler;
    act.sa_flags = 0;
    if ( sigaction(2, &act, (struct sigaction *) ((void *)0)) != 0
        || sigaction(3, &act, (struct sigaction *) ((void *)0)) != 0
        || sigaction(15, &act, (struct sigaction *) ((void *)0)) != 0) {
        bsock_syslog((*__errno_location ()), 3, "sigaction");
        return 0;
    }
    return 1;
}

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

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

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


decompiled_code:
void __noreturn bsock_daemon_sa_handler()
{
exit(0);
}

canonical_decompiled_code:
void __noreturn func2()
{
exit(0);
}

original_code:
__attribute__((__noreturn__))
static void
bsock_daemon_sa_handler (int signum __attribute__((__unused__)))
{
    exit(0);
}

canonical_original_code:
static void bsock_daemon_sa_handler(int signum)
{
    exit(0);
}


decompiled_code:
void bsock_daemon_atexit(void)
{
if ( !bsock_daemon_socket_bound && getpid() == bsock_daemon_pid )
unlink(bsock_daemon_socket_path);
}

canonical_decompiled_code:
void func4(void)
{
if ( !bsock_daemon_socket_bound && getpid() == bsock_daemon_pid )
unlink(bsock_daemon_socket_path);
}

original_code:
static void
bsock_daemon_atexit (void)
{
    if (0 == bsock_daemon_socket_bound && getpid() == bsock_daemon_pid)
        (void)unlink(bsock_daemon_socket_path);
}

canonical_original_code:
static void bsock_daemon_atexit()
{
    if (0 == bsock_daemon_socket_bound && getpid() == bsock_daemon_pid)
        (void)unlink(bsock_daemon_socket_path);
}


decompiled_code:
__int64 bsock_daemon_setuid_stdinit()
{
int *v1; // rax
sigset_t v2; // [rsp+0h] [rbp-88h] BYREF

environ = basic_env_2;
if ( !sigemptyset(&v2) && !sigprocmask(2, &v2, 0LL) )
return 1LL;
v1 = _errno_location();
bsock_syslog((unsigned int)*v1, 3LL, "sigprocmask");
return 0LL;
}

canonical_decompiled_code:
__int64 func5()
{
int *v1; // rax
sigset_t v2; // [rsp+0h] [rbp-88h] BYREF

environ = basic_env_2;
if ( !sigemptyset(&v2) && !sigprocmask(2, &v2, 0LL) )
return 1LL;
v1 = _errno_location();
bsock_syslog((unsigned int)*v1, 3LL, "sigprocmask");
return 0LL;
}

original_code:
_Bool
bsock_daemon_setuid_stdinit (void)
{
    static char default_path[] = "PATH=/usr/bin:/bin";
    static char default_lang[] = "LANG=C";
    static char *basic_env[] = { default_path, default_lang, ((void *)0) };
    environ = basic_env;
    sigset_t sigset_empty;
    if (0 != sigemptyset(&sigset_empty)
        || 0 != sigprocmask(2, &sigset_empty, (sigset_t *) ((void *)0))) {
        bsock_syslog((*__errno_location ()), 3, "sigprocmask");
        return 0;
    }
    return 1;
}

canonical_original_code:
_Bool bsock_daemon_setuid_stdinit()
{
    static char default_path[] = "PATH=/usr/bin:/bin";
    static char default_lang[] = "LANG=C";
    static char *basic_env[] = { default_path, default_lang, ((void *)0) };
    environ = basic_env;
    struct __sigset_t sigset_empty;
    if (0 != sigemptyset(&sigset_empty)
        || 0 != sigprocmask(2, &sigset_empty, (struct __sigset_t *) ((void *)0))) {
        bsock_syslog((*__errno_location ()), 3, "sigprocmask");
        return 0;
    }
    return 1;
}

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


decompiled_code:
__int64 __fastcall bsock_daemon_init_socket(char *file, __uid_t owner, __gid_t a3, __mode_t a4)
{
char *v4; // rax
char *v5; // r12
void *v6; // rsp
__int64 v7; // rdx
__mode_t v8; // er14
unsigned int v9; // er12
int v10; // eax
int *v12; // rax
int *v13; // rax
int *v14; // rax
char v15[8]; // [rsp+0h] [rbp-E0h] BYREF
__mode_t mode; // [rsp+8h] [rbp-D8h]
__gid_t group; // [rsp+Ch] [rbp-D4h]
struct stat64 v18; // [rsp+10h] [rbp-D0h] BYREF
unsigned __int64 v19; // [rsp+A8h] [rbp-38h]

group = a3;
mode = a4;
v19 = __readfsqword(0x28u);
v4 = strrchr(file, 47);
if ( !v4 || (v5 = v4, *file != 47) )
{
bsock_syslog(22LL, 3LL, "socket path must be absolute path");
return (unsigned int)-1;
}
v6 = alloca(v4 - file + 2);
memcpy(v15, file, v4 - file + 1);
v7 = v5 - file;
if ( file == v5 )
v7 = 1LL;
v15[v7] = 0;
if ( stat64(v15, &v18) )
{
v12 = _errno_location();
bsock_syslog((unsigned int)*v12, 3LL, "%s", v15);
return (unsigned int)-1;
}
if ( v18.st_uid != owner || (v18.st_mode & 0x12) != 0 )
{
bsock_syslog(1LL, 3LL, "ownership/permissions incorrect on %s", v15);
return (unsigned int)-1;
}
bsock_daemon_socket_path = file;
bsock_daemon_pid = getpid();
atexit(bsock_daemon_atexit);
v8 = umask(0x7Fu);
v9 = bsock_unix_socket_bind_listen(file, &bsock_daemon_socket_bound);
umask(v8);
if ( v9 == -1 )
{
v13 = _errno_location();
bsock_syslog((unsigned int)*v13, 3LL, "socket,bind,listen");
}
else
{
v10 = fcntl64(v9, 1LL, 0LL);
fcntl64(v9, 2LL, v10 | 1u);
if ( chown(file, owner, group) || chmod(file, mode) )
{
v14 = _errno_location();
bsock_syslog((unsigned int)*v14, 3LL, "chown,chmod");
unlink(bsock_daemon_socket_path);
bsock_daemon_socket_bound = -1;
nointr_close(v9);
v9 = -1;
}
}
return v9;
}

canonical_decompiled_code:
__int64 __fastcall func6(char *file, __uid_t owner, __gid_t a3, __mode_t a4)
{
char *v4; // rax
char *v5; // r12
void *v6; // rsp
__int64 v7; // rdx
__mode_t v8; // er14
unsigned int v9; // er12
int v10; // eax
int *v12; // rax
int *v13; // rax
int *v14; // rax
char v15[8]; // [rsp+0h] [rbp-E0h] BYREF
__mode_t mode; // [rsp+8h] [rbp-D8h]
__gid_t group; // [rsp+Ch] [rbp-D4h]
struct stat64 v18; // [rsp+10h] [rbp-D0h] BYREF
unsigned __int64 v19; // [rsp+A8h] [rbp-38h]

group = a3;
mode = a4;
v19 = __readfsqword(0x28u);
v4 = strrchr(file, 47);
if ( !v4 || (v5 = v4, *file != 47) )
{
bsock_syslog(22LL, 3LL, "socket path must be absolute path");
return (unsigned int)-1;
}
v6 = alloca(v4 - file + 2);
memcpy(v15, file, v4 - file + 1);
v7 = v5 - file;
if ( file == v5 )
v7 = 1LL;
v15[v7] = 0;
if ( stat64(v15, &v18) )
{
v12 = _errno_location();
bsock_syslog((unsigned int)*v12, 3LL, "%s", v15);
return (unsigned int)-1;
}
if ( v18.st_uid != owner || (v18.st_mode & 0x12) != 0 )
{
bsock_syslog(1LL, 3LL, "ownership/permissions incorrect on %s", v15);
return (unsigned int)-1;
}
bsock_daemon_socket_path = file;
bsock_daemon_pid = getpid();
atexit(bsock_daemon_atexit);
v8 = umask(0x7Fu);
v9 = bsock_unix_socket_bind_listen(file, &bsock_daemon_socket_bound);
umask(v8);
if ( v9 == -1 )
{
v13 = _errno_location();
bsock_syslog((unsigned int)*v13, 3LL, "socket,bind,listen");
}
else
{
v10 = fcntl64(v9, 1LL, 0LL);
fcntl64(v9, 2LL, v10 | 1u);
if ( chown(file, owner, group) || chmod(file, mode) )
{
v14 = _errno_location();
bsock_syslog((unsigned int)*v14, 3LL, "chown,chmod");
unlink(bsock_daemon_socket_path);
bsock_daemon_socket_bound = -1;
func3(v9);
v9 = -1;
}
}
return v9;
}

original_code:
__attribute__((__nonnull__ ))
int
bsock_daemon_init_socket (const char * const restrict sockpath,
                          const uid_t uid, const gid_t gid, const mode_t mode)
{
    int sfd;
    mode_t mask;
    char * const slash = strrchr(sockpath,'/');
    if (((void *)0) != slash && '/' == *sockpath) {
        struct stat st;
        char dir[slash-sockpath+2];
        memcpy(dir, sockpath, (size_t)(slash-sockpath+1));
        dir[slash != sockpath ? slash-sockpath : 1] = '\0';
        if (0 != stat(dir, &st)) {
            bsock_syslog((*__errno_location ()), 3, "%s", dir);
            return -1;
        }
        if (st.st_uid != uid || (st.st_mode & ((0200 >> 3)|((0200 >> 3) >> 3)))) {
            bsock_syslog(1, 3,
                         "ownership/permissions incorrect on %s", dir);
            return -1;
        }
    }
    else {
        bsock_syslog(22, 3, "socket path must be absolute path");
        return -1;
    }
    bsock_daemon_socket_path = sockpath;
    bsock_daemon_pid = getpid();
    atexit(bsock_daemon_atexit);
    mask = umask(0177);
    sfd = bsock_unix_socket_bind_listen(sockpath, &bsock_daemon_socket_bound);
    umask(mask);
    if (-1 == sfd) {
        bsock_syslog((*__errno_location ()), 3, "socket,bind,listen");
        return -1;
    }
    (void)fcntl(sfd, 2, fcntl(sfd, 1, 0) | 1);
    if (0 == chown(sockpath, uid, gid) && 0 == chmod(sockpath, mode))
        return sfd;
    bsock_syslog((*__errno_location ()), 3, "chown,chmod");
    (void)unlink(bsock_daemon_socket_path);
    bsock_daemon_socket_bound = -1;
    nointr_close(sfd);
    return -1;
}

canonical_original_code:
int bsock_daemon_init_socket(char *sockpath, unsigned int uid, unsigned int gid, unsigned int mode)
{
    int sfd;
    unsigned int mask;
    char *slash = strrchr(sockpath,'/');
    if (((void *)0) != slash && '/' == *sockpath) {
        struct stat st;
        char dir[-1];
        memcpy(dir, sockpath, (unsigned long)(slash-sockpath+1));
        dir[slash != sockpath ? slash-sockpath : 1] = '\0';
        if (0 != stat(dir, &st)) {
            bsock_syslog((*__errno_location ()), 3, "%s", dir);
            return -1;
        }
        if (st.st_uid != uid || (st.st_mode & ((0200 >> 3)|((0200 >> 3) >> 3)))) {
            bsock_syslog(1, 3,
                         "ownership/permissions incorrect on %s", dir);
            return -1;
        }
    }
    else {
        bsock_syslog(22, 3, "socket path must be absolute path");
        return -1;
    }
    bsock_daemon_socket_path = sockpath;
    bsock_daemon_pid = getpid();
    atexit(bsock_daemon_atexit);
    mask = umask(0177);
    sfd = bsock_unix_socket_bind_listen(sockpath, &bsock_daemon_socket_bound);
    umask(mask);
    if (-1 == sfd) {
        bsock_syslog((*__errno_location ()), 3, "socket,bind,listen");
        return -1;
    }
    (void)fcntl(sfd, 2, fcntl(sfd, 1, 0) | 1);
    if (0 == chown(sockpath, uid, gid) && 0 == chmod(sockpath, mode))
        return sfd;
    bsock_syslog((*__errno_location ()), 3, "chown,chmod");
    (void)unlink(bsock_daemon_socket_path);
    bsock_daemon_socket_bound = -1;
    nointr_close(sfd);
    return -1;
}

variable_types:
sockpath
char *

variable_types:
uid
unsigned int

variable_types:
gid
unsigned int

variable_types:
mode
unsigned int

variable_types: sfd int

variable_types:
mask
unsigned int

variable_types:
slash
char *

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

variable_types:
dir
char [-1]

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


decompiled_code:
__int64 bsock_daemon_msg_control_max()
{
int v0; // eax
__int64 v1; // rbp
int v3; // ebx
ssize_t v4; // rax
int *v5; // r12
__int64 v6; // rax
char v7[40]; // [rsp+0h] [rbp-48h] BYREF
unsigned __int64 v8; // [rsp+28h] [rbp-20h]

v8 = __readfsqword(0x28u);
v0 = open64("/proc/sys/net/core/optmem_max", 2048, 0LL);
v1 = 10240LL;
if ( v0 != -1 )
{
v3 = v0;
v4 = read(v0, v7, 0x1FuLL);
if ( v4 >= 0 )
{
v7[v4] = 0;
v5 = _errno_location();
*v5 = 0;
v6 = strtol(v7, 0LL, 10);
v1 = v6;
if ( *v5 || v6 <= 10239 )
v1 = 10240LL;
}
nointr_close(v3);
}
return v1;
}

original_code:
size_t
bsock_daemon_msg_control_max (void)
{
    long optmem_max = 10240;
    ssize_t r;
    const int fd = open("/proc/sys/net/core/optmem_max",00|04000,0);
    char buf[32];
    if (-1 != fd) {
        if ((r = read(fd, buf, sizeof(buf)-1)) >= 0) {
            buf[r] = '\0';
            (*__errno_location ()) = 0;
            optmem_max = strtol(buf, ((void *)0), 10);
            if (0 != (*__errno_location ()) || 10240 > optmem_max)
                optmem_max = 10240;
        }
        nointr_close(fd);
    }
    return (size_t)optmem_max;
}

canonical_original_code:
unsigned long bsock_daemon_msg_control_max()
{
    long optmem_max = 10240;
    long r;
    int fd = open("/proc/sys/net/core/optmem_max",00|04000,0);
    char buf[32];
    if (-1 != fd) {
        if ((r = read(fd, buf, sizeof(buf)-1)) >= 0) {
            buf[r] = '\0';
            (*__errno_location ()) = 0;
            optmem_max = strtol(buf, ((void *)0), 10);
            if (0 != (*__errno_location ()) || 10240 > optmem_max)
                optmem_max = 10240;
        }
        nointr_close(fd);
    }
    return (unsigned long)optmem_max;
}

variable_types: r long

variable_types:
buf
char [32]

return_type: unsigned long


decompiled_code:
__int64 __fastcall bsock_daemon_init(int a1, unsigned int a2)
{
unsigned int v2; // ebx
__pid_t v3; // er13
int *v4; // rax
int *v5; // rax
int v6; // eax
__pid_t v7; // eax
int *v9; // rax
unsigned __int64 v10; // rbx
int *v11; // rax
int *v12; // rax
struct stat64 status; // [rsp+0h] [rbp-B8h] BYREF

if ( chdir("/") )
{
v5 = _errno_location();
bsock_syslog((unsigned int)*v5, 3LL, "chdir /");
return 0;
}
v2 = bsock_daemon_signal_init();
if ( (_BYTE)v2 )
{
if ( a1 )
{
v3 = getpgrp();
if ( v3 != getpid() && setsid() == -1 )
{
v4 = _errno_location();
bsock_syslog((unsigned int)*v4, 3LL, "setsid");
return 0;
}
goto LABEL_11;
}
v6 = fork();
if ( v6 )
{
LODWORD(status.st_dev) = 1;
if ( v6 > 0 && waitpid(v6, (int *)&status, 0) != v6 )
LODWORD(status.st_dev) = 1;
exit(status.st_dev);
}
if ( setsid() == -1 )
goto LABEL_20;
v7 = fork();
if ( !v7 )
{
LABEL_11:
if ( (unsigned int)nointr_close(0) )
return 0;
if ( (unsigned int)nointr_close(1) )
return 0;
if ( a1 )
{
if ( fstat64(2, &status) )
{
v11 = _errno_location();
bsock_syslog((unsigned int)*v11, 3LL, "stat STDERR_FILENO");
return 0;
}
}
else
{
if ( (unsigned int)nointr_close(2) )
return 0;
bsock_syslog_setlevel(0LL);
}
if ( (_BYTE)a2 )
{
v10 = bsock_daemon_msg_control_max();
if ( v10 > 0x2800 )
{
v12 = _errno_location();
bsock_syslog(
(unsigned int)*v12,
3LL,
"max ancillary data very large (%zu > %d); consider recompiling bsock with larger BSOCK_ANCILLARY_DATA_MAX",
v10,
10240LL);
}
v2 = a2;
}
return v2;
}
if ( v7 == -1 )
{
LABEL_20:
v9 = _errno_location();
bsock_syslog((unsigned int)*v9, 3LL, "setsid,fork");
v7 = -1;
}
exit(v7 == -1);
}
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func8(int a1, unsigned int a2)
{
unsigned int v2; // ebx
__pid_t v3; // er13
int *v4; // rax
int *v5; // rax
int v6; // eax
__pid_t v7; // eax
int *v9; // rax
unsigned __int64 v10; // rbx
int *v11; // rax
int *v12; // rax
struct stat64 status; // [rsp+0h] [rbp-B8h] BYREF

if ( chdir("/") )
{
v5 = _errno_location();
bsock_syslog((unsigned int)*v5, 3LL, "chdir /");
return 0;
}
v2 = func1();
if ( (_BYTE)v2 )
{
if ( a1 )
{
v3 = getpgrp();
if ( v3 != getpid() && setsid() == -1 )
{
v4 = _errno_location();
bsock_syslog((unsigned int)*v4, 3LL, "setsid");
return 0;
}
goto LABEL_11;
}
v6 = fork();
if ( v6 )
{
LODWORD(status.st_dev) = 1;
if ( v6 > 0 && waitpid(v6, (int *)&status, 0) != v6 )
LODWORD(status.st_dev) = 1;
exit(status.st_dev);
}
if ( setsid() == -1 )
goto LABEL_20;
v7 = fork();
if ( !v7 )
{
LABEL_11:
if ( (unsigned int)func3(0) )
return 0;
if ( (unsigned int)func3(1) )
return 0;
if ( a1 )
{
if ( fstat64(2, &status) )
{
v11 = _errno_location();
bsock_syslog((unsigned int)*v11, 3LL, "stat STDERR_FILENO");
return 0;
}
}
else
{
if ( (unsigned int)func3(2) )
return 0;
bsock_syslog_setlevel(0LL);
}
if ( (_BYTE)a2 )
{
v10 = func7();
if ( v10 > 0x2800 )
{
v12 = _errno_location();
bsock_syslog(
(unsigned int)*v12,
3LL,
"max ancillary data very large (%zu > %d); consider recompiling bsock with larger BSOCK_ANCILLARY_DATA_MAX",
v10,
10240LL);
}
v2 = a2;
}
return v2;
}
if ( v7 == -1 )
{
LABEL_20:
v9 = _errno_location();
bsock_syslog((unsigned int)*v9, 3LL, "setsid,fork");
v7 = -1;
}
exit(v7 == -1);
}
return v2;
}

original_code:
_Bool
bsock_daemon_init (const int supervised, const _Bool check)
{
    if (0 != chdir("/")) {
        bsock_syslog((*__errno_location ()), 3, "chdir /");
        return 0;
    }
    if (!bsock_daemon_signal_init())
        return 0;
    if (supervised) {
        if (getpgrp() != getpid() && setsid() == (pid_t)-1) {
            bsock_syslog((*__errno_location ()), 3, "setsid");
            return 0;
        }
    }
    else {
        pid_t pid;
        if ((pid = fork()) != 0) {
            int status = 1;
            if (pid > 0 && waitpid(pid, &status, 0) != pid)
                status = 1;
            _exit(status);
        }
        else if ((pid = setsid()) == (pid_t)-1 || (pid = fork()) != 0) {
            if ((pid_t)-1==pid) bsock_syslog((*__errno_location ()), 3, "setsid,fork");
            _exit((pid_t)-1 == pid);
        }
    }
    if (0 != nointr_close(0)) return 0;
    if (0 != nointr_close(1)) return 0;
    if (!supervised) {
        if (0 != nointr_close(2)) return 0;
        bsock_syslog_setlevel(BSOCK_SYSLOG_DAEMON);
    }
    else {
        struct stat st;
        if (0 != fstat(2, &st)) {
            bsock_syslog((*__errno_location ()), 3, "stat STDERR_FILENO");
            return 0;
        }
    }
    if (check) {
        const size_t optmem_max = bsock_daemon_msg_control_max();
        if (optmem_max > 10240)
            bsock_syslog((*__errno_location ()), 3, "max ancillary data very large "
                         "(%zu > %d); consider recompiling bsock with larger "
                         "BSOCK_ANCILLARY_DATA_MAX", optmem_max,
                         10240);
    }
    return 1;
}

canonical_original_code:
_Bool bsock_daemon_init(int supervised, _Bool check)
{
    if (0 != chdir("/")) {
        bsock_syslog((*__errno_location ()), 3, "chdir /");
        return 0;
    }
    if (!bsock_daemon_signal_init())
        return 0;
    if (supervised) {
        if (getpgrp() != getpid() && setsid() == (pid_t)-1) {
            bsock_syslog((*__errno_location ()), 3, "setsid");
            return 0;
        }
    }
    else {
        int pid;
        if ((pid = fork()) != 0) {
            int status = 1;
            if (pid > 0 && waitpid(pid, &status, 0) != pid)
                status = 1;
            _exit(status);
        }
        else if ((pid = setsid()) == (pid_t)-1 || (pid = fork()) != 0) {
            if ((pid_t)-1==pid) bsock_syslog((*__errno_location ()), 3, "setsid,fork");
            _exit((pid_t)-1 == pid);
        }
    }
    if (0 != nointr_close(0)) return 0;
    if (0 != nointr_close(1)) return 0;
    if (!supervised) {
        if (0 != nointr_close(2)) return 0;
        bsock_syslog_setlevel(BSOCK_SYSLOG_DAEMON);
    }
    else {
        struct stat st;
        if (0 != fstat(2, &st)) {
            bsock_syslog((*__errno_location ()), 3, "stat STDERR_FILENO");
            return 0;
        }
    }
    if (check) {
        unsigned long optmem_max = bsock_daemon_msg_control_max();
        if (optmem_max > 10240)
            bsock_syslog((*__errno_location ()), 3, "max ancillary data very large "
                         "(%zu > %d); consider recompiling bsock with larger "
                         "BSOCK_ANCILLARY_DATA_MAX", optmem_max,
                         10240);
    }
    return 1;
}

variable_types: supervised int

variable_types: pid int

variable_types: status int

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


canonical_decompiled_code:
__int64 __fastcall func3(int fd)
{
unsigned int v1; // ebx

do
v1 = close(fd);
while ( v1 && *_errno_location() == 4 );
return v1;
}

variable_types: fd int

variable_types: r int


decompiled_code:
int __fastcall bsock_preload_bind(__int64 fd, unsigned __int16 *a2, unsigned int a3)
{
unsigned __int16 v4; // ax
int result; // eax
socklen_t optlen; // [rsp+Ch] [rbp-4Ch] BYREF
int v7[2]; // [rsp+10h] [rbp-48h] BYREF
int optval; // [rsp+18h] [rbp-40h] BYREF
int v9; // [rsp+1Ch] [rbp-3Ch] BYREF
unsigned int v10; // [rsp+20h] [rbp-38h]
unsigned __int16 *v11; // [rsp+28h] [rbp-30h]
__int64 v12; // [rsp+30h] [rbp-28h]
__int64 v13; // [rsp+38h] [rbp-20h]

v7[0] = 0;
v4 = *a2;
v7[1] = *a2;
optval = 0;
v9 = 6;
v10 = a3;
v11 = a2;
v12 = 0LL;
v13 = 0LL;
if ( (v4 & 0xFFF7) == 2 )
{
if ( (unsigned __int16)_bswap_16(a2[1]) > 0x3FFu )
{
result = bind_rtld_next((unsigned int)fd, a2, a3);
if ( !result )
return result;
}
}
else if ( v4 != 1 )
{
return bind_rtld_next(fd, a2, a3);
}
if ( geteuid() || (result = bind_rtld_next((unsigned int)fd, v11, v10)) != 0 )
{
optlen = 4;
result = getsockopt(fd, 1, 3, &optval, &optlen);
if ( result != -1 )
{
optlen = 4;
result = getsockopt(fd, 1, 38, &v9, &optlen);
if ( result != -1 )
result = bsock_bind_addrinfo((unsigned int)fd, v7);
}
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(__int64 fd, unsigned __int16 *a2, unsigned int a3)
{
unsigned __int16 v4; // ax
int result; // eax
socklen_t optlen; // [rsp+Ch] [rbp-4Ch] BYREF
int v7[2]; // [rsp+10h] [rbp-48h] BYREF
int optval; // [rsp+18h] [rbp-40h] BYREF
int v9; // [rsp+1Ch] [rbp-3Ch] BYREF
unsigned int v10; // [rsp+20h] [rbp-38h]
unsigned __int16 *v11; // [rsp+28h] [rbp-30h]
__int64 v12; // [rsp+30h] [rbp-28h]
__int64 v13; // [rsp+38h] [rbp-20h]

v7[0] = 0;
v4 = *a2;
v7[1] = *a2;
optval = 0;
v9 = 6;
v10 = a3;
v11 = a2;
v12 = 0LL;
v13 = 0LL;
if ( (v4 & 0xFFF7) == 2 )
{
if ( (unsigned __int16)func1(a2[1]) > 0x3FFu )
{
result = bind_rtld_next((unsigned int)fd, a2, a3);
if ( !result )
return result;
}
}
else if ( v4 != 1 )
{
return bind_rtld_next(fd, a2, a3);
}
if ( geteuid() || (result = bind_rtld_next((unsigned int)fd, v11, v10)) != 0 )
{
optlen = 4;
result = getsockopt(fd, 1, 3, &optval, &optlen);
if ( result != -1 )
{
optlen = 4;
result = getsockopt(fd, 1, 38, &v9, &optlen);
if ( result != -1 )
result = bsock_bind_addrinfo((unsigned int)fd, v7);
}
}
return result;
}

original_code:
__attribute__ ((__noinline__))
__attribute__((__nonnull__ ))
static int
bsock_preload_bind (const int sockfd, const struct sockaddr *addr,
                    const socklen_t addrlen)
{
    struct addrinfo ai = {
      .ai_flags = 0,
      .ai_family = addr->sa_family,
      .ai_socktype = 0,
      .ai_protocol = IPPROTO_TCP,
      .ai_addrlen = addrlen,
      .ai_addr = (struct sockaddr *)(uintptr_t)addr,
      .ai_canonname= ((void *)0),
      .ai_next = ((void *)0)
    };
    socklen_t optlen;
    if (ai.ai_family == 2 || ai.ai_family == 10) {
        const int port = (ai.ai_family == 2)
          ? ntohs(((struct sockaddr_in *)ai.ai_addr)->sin_port)
          : ntohs(((struct sockaddr_in6 *)ai.ai_addr)->sin6_port);
        if (port >= 1024
            && 0 == bind_rtld_next(sockfd, ai.ai_addr, ai.ai_addrlen))
            return 0;
    }
    else if (ai.ai_family != 1)
        return bind_rtld_next(sockfd, ai.ai_addr, ai.ai_addrlen);
    if (0 == geteuid() && 0 == bind_rtld_next(sockfd,ai.ai_addr,ai.ai_addrlen))
        return 0;
    optlen = sizeof(ai.ai_socktype);
    if (-1 == getsockopt(sockfd,1,3,&ai.ai_socktype,&optlen))
        return -1;
    optlen = sizeof(ai.ai_protocol);
    if (-1 == getsockopt(sockfd,1,38,&ai.ai_protocol,&optlen))
        return -1;
    return bsock_bind_addrinfo(sockfd, &ai);
}

canonical_original_code:
static int bsock_preload_bind(int sockfd, struct sockaddr *addr, unsigned int addrlen)
{
    struct addrinfo ai = {
      .ai_flags = 0,
      .ai_family = addr->sa_family,
      .ai_socktype = 0,
      .ai_protocol = IPPROTO_TCP,
      .ai_addrlen = addrlen,
      .ai_addr = (struct sockaddr *)(uintptr_t)addr,
      .ai_canonname= ((void *)0),
      .ai_next = ((void *)0)
    };
    unsigned int optlen;
    if (ai.ai_family == 2 || ai.ai_family == 10) {
        int port = (ai.ai_family == 2)
          ? ntohs(((struct sockaddr_in *)ai.ai_addr)->sin_port)
          : ntohs(((struct sockaddr_in6 *)ai.ai_addr)->sin6_port);
        if (port >= 1024
            && 0 == bind_rtld_next(sockfd, ai.ai_addr, ai.ai_addrlen))
            return 0;
    }
    else if (ai.ai_family != 1)
        return bind_rtld_next(sockfd, ai.ai_addr, ai.ai_addrlen);
    if (0 == geteuid() && 0 == bind_rtld_next(sockfd,ai.ai_addr,ai.ai_addrlen))
        return 0;
    optlen = sizeof(ai.ai_socktype);
    if (-1 == getsockopt(sockfd,1,3,&ai.ai_socktype,&optlen))
        return -1;
    optlen = sizeof(ai.ai_protocol);
    if (-1 == getsockopt(sockfd,1,38,&ai.ai_protocol,&optlen))
        return -1;
    return bsock_bind_addrinfo(sockfd, &ai);
}

variable_types: sockfd int

variable_types:
addrlen
unsigned int

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:
optlen
unsigned int

variable_types: port int

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


bind

decompiled_code:
int __fastcall bind(__int64 a1, unsigned __int16 *a2, unsigned int a3)
{
return bsock_preload_bind(a1, a2, a3);
}

canonical_decompiled_code:
int __fastcall func3(__int64 a1, unsigned __int16 *a2, unsigned int a3)
{
return func2(a1, a2, a3);
}

original_code:
__attribute__((__visibility__("default"))) int
bind (const int sockfd, const struct sockaddr * const restrict addr,
      const socklen_t addrlen)
{
    return bsock_preload_bind(sockfd, addr, addrlen);
}

canonical_original_code:
int bind(int sockfd, struct sockaddr *addr, unsigned int addrlen)
{
    return bsock_preload_bind(sockfd, addr, addrlen);
}

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


bindresvport

decompiled_code:
int __fastcall bindresvport(__int64 a1, unsigned __int16 *a2)
{
return bsock_preload_bind(a1, a2, 0x10u);
}

canonical_decompiled_code:
int __fastcall func4(__int64 a1, unsigned __int16 *a2)
{
return func2(a1, a2, 0x10u);
}

original_code:
__attribute__((__visibility__("default"))) int
bindresvport (const int sockfd, struct sockaddr_in * const restrict sin)
{
    return bsock_preload_bind(sockfd, (const struct sockaddr *)sin,
                              sizeof(struct sockaddr_in));
}

canonical_original_code:
int bindresvport(int sockfd, struct sockaddr_in *sin)
{
    return bsock_preload_bind(sockfd, (struct sockaddr *)sin,
                              sizeof(struct sockaddr_in));
}

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

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


decompiled_code:
int __fastcall bindresvport6(__int64 a1, unsigned __int16 *a2)
{
return bsock_preload_bind(a1, a2, 0x1Cu);
}

canonical_decompiled_code:
int __fastcall func5(__int64 a1, unsigned __int16 *a2)
{
return func2(a1, a2, 0x1Cu);
}

original_code:
__attribute__((__visibility__("default"))) int
bindresvport6 (const int sockfd, struct sockaddr_in6 * const restrict sin6)
{
    return bsock_preload_bind(sockfd, (const struct sockaddr *)sin6,
                              sizeof(struct sockaddr_in6));
}

canonical_original_code:
int bindresvport6(int sockfd, struct sockaddr_in6 *sin6)
{
    return bsock_preload_bind(sockfd, (struct sockaddr *)sin6,
                              sizeof(struct sockaddr_in6));
}

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


decompiled_code:
__int64 bsock_resvaddr_count()
{
__int64 result; // rax

result = 0LL;
if ( bsock_resvaddr_alloc )
result = *((_QWORD *)bsock_resvaddr_alloc + 2);
return result;
}

canonical_decompiled_code:
__int64 func0()
{
__int64 result; // rax

result = 0LL;
if ( bsock_resvaddr_alloc )
result = *((_QWORD *)bsock_resvaddr_alloc + 2);
return result;
}

original_code:
static size_t
bsock_resvaddr_count (void)
{
    return (((void *)0) != bsock_resvaddr_alloc) ? bsock_resvaddr_alloc->elt_count : 0;
}

canonical_original_code:
static unsigned long bsock_resvaddr_count()
{
    return (((void *)0) != bsock_resvaddr_alloc) ? bsock_resvaddr_alloc->elt_count : 0;
}


decompiled_code:
__int64 __fastcall bsock_resvaddr_hash(__int64 a1)
{
unsigned __int8 *v1; // rdx
unsigned __int8 *v2; // rsi
__int64 result; // rax

v1 = *(unsigned __int8 **)(a1 + 24);
v2 = &v1[*(unsigned int *)(a1 + 16)];
if ( v1 >= v2 )
return 5381LL;
LODWORD(result) = 5381;
do
result = *v1++ ^ (unsigned int)(33 * result);
while ( v2 != v1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(__int64 a1)
{
unsigned __int8 *v1; // rdx
unsigned __int8 *v2; // rsi
__int64 result; // rax

v1 = *(unsigned __int8 **)(a1 + 24);
v2 = &v1[*(unsigned int *)(a1 + 16)];
if ( v1 >= v2 )
return 5381LL;
LODWORD(result) = 5381;
do
result = *v1++ ^ (unsigned int)(33 * result);
while ( v2 != v1 );
return result;
}

original_code:
__attribute__((__nonnull__ ))
static uint32_t
bsock_resvaddr_hash (const struct addrinfo * const restrict ai)
{
    const unsigned char * restrict addr = (const unsigned char *)ai->ai_addr;
    const unsigned char * const e =(const unsigned char *)addr + ai->ai_addrlen;
    uint32_t h = 5381;
    for (; __builtin_expect( (addr < e), 1); ++addr)
        h = (h + (h << 5)) ^ *addr;
    return h;
}

canonical_original_code:
static unsigned int bsock_resvaddr_hash(struct addrinfo *ai)
{
    unsigned char *addr = (const unsigned char *)ai->ai_addr;
    unsigned char *e = (const unsigned char *)addr + ai->ai_addrlen;
    unsigned int h = 5381;
    for (; __builtin_expect( (addr < e), 1); ++addr)
        h = (h + (h << 5)) ^ *addr;
    return h;
}

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

variable_types:
e
unsigned char *

variable_types:
h
unsigned int

return_type: unsigned int


decompiled_code:
__int64 __fastcall bsock_resvaddr_rebind(__int64 a1, int *a2)
{
int v2; // ebp
int v3; // er13
int *v4; // rax
int *v5; // rax
int *v6; // r12
int *v8; // rax
int *v9; // rax
int optval; // [rsp+Ch] [rbp-DCh] BYREF
const char *v11[6]; // [rsp+10h] [rbp-D8h] BYREF
char v12[120]; // [rsp+40h] [rbp-A8h] BYREF
unsigned __int64 v13; // [rsp+B8h] [rbp-30h]

v13 = __readfsqword(0x28u);
optval = 1;
v2 = socket(*(_DWORD *)(a1 + 4), *(_DWORD *)(a1 + 8), *(_DWORD *)(a1 + 12));
if ( v2 != -1 )
{
v3 = *a2;
*a2 = -1;
if ( v3 == -1 || !(unsigned int)nointr_close(v3) )
{
if ( !bind(v2, *(const struct sockaddr **)(a1 + 24), *(_DWORD *)(a1 + 16)) )
{
*a2 = v2;
return (unsigned int)*a2;
}
v3 = -1;
}
if ( (*(_DWORD *)(a1 + 4) & 0xFFFFFFF7) == 2 )
{
v5 = _errno_location();
v6 = v5;
if ( *v5 == 98 )
goto LABEL_16;
bsock_syslog((unsigned int)*v5, 3LL, "socket,setsockopt,bind");
}
else
{
v4 = _errno_location();
bsock_syslog((unsigned int)*v4, 3LL, "socket,setsockopt,bind");
}
LABEL_9:
nointr_close(v2);
goto LABEL_10;
}
if ( (*(_DWORD *)(a1 + 4) & 0xFFFFFFF7) != 2 )
{
v8 = _errno_location();
bsock_syslog((unsigned int)*v8, 3LL, "socket,setsockopt,bind");
return (unsigned int)*a2;
}
v9 = _errno_location();
v6 = v9;
if ( *v9 != 98 )
{
bsock_syslog((unsigned int)*v9, 3LL, "socket,setsockopt,bind");
v3 = v2;
goto LABEL_10;
}
v3 = v2;
LABEL_16:
if ( !setsockopt(v2, 1, 2, &optval, 4u) && !bind(v2, *(const struct sockaddr **)(a1 + 24), *(_DWORD *)(a1 + 16)) )
{
*a2 = v2;
if ( (*(_BYTE *)a1 & 1) == 0 && (unsigned __int8)bsock_addrinfo_to_strs(a1, v11, v12, 112LL) )
bsock_syslog(0LL, 4LL, "bind SO_REUSEADDR: %s %s %s %s %s", v11[0], v11[1], v11[2], v11[3], v11[4]);
return (unsigned int)*a2;
}
bsock_syslog((unsigned int)*v6, 3LL, "socket,setsockopt,bind");
if ( v2 != -1 )
goto LABEL_9;
LABEL_10:
if ( v3 != -1 )
nointr_close(v3);
return (unsigned int)*a2;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, int *a2)
{
int v2; // ebp
int v3; // er13
int *v4; // rax
int *v5; // rax
int *v6; // r12
int *v8; // rax
int *v9; // rax
int optval; // [rsp+Ch] [rbp-DCh] BYREF
const char *v11[6]; // [rsp+10h] [rbp-D8h] BYREF
char v12[120]; // [rsp+40h] [rbp-A8h] BYREF
unsigned __int64 v13; // [rsp+B8h] [rbp-30h]

v13 = __readfsqword(0x28u);
optval = 1;
v2 = socket(*(_DWORD *)(a1 + 4), *(_DWORD *)(a1 + 8), *(_DWORD *)(a1 + 12));
if ( v2 != -1 )
{
v3 = *a2;
*a2 = -1;
if ( v3 == -1 || !(unsigned int)func2(v3) )
{
if ( !bind(v2, *(const struct sockaddr **)(a1 + 24), *(_DWORD *)(a1 + 16)) )
{
*a2 = v2;
return (unsigned int)*a2;
}
v3 = -1;
}
if ( (*(_DWORD *)(a1 + 4) & 0xFFFFFFF7) == 2 )
{
v5 = _errno_location();
v6 = v5;
if ( *v5 == 98 )
goto LABEL_16;
bsock_syslog((unsigned int)*v5, 3LL, "socket,setsockopt,bind");
}
else
{
v4 = _errno_location();
bsock_syslog((unsigned int)*v4, 3LL, "socket,setsockopt,bind");
}
LABEL_9:
func2(v2);
goto LABEL_10;
}
if ( (*(_DWORD *)(a1 + 4) & 0xFFFFFFF7) != 2 )
{
v8 = _errno_location();
bsock_syslog((unsigned int)*v8, 3LL, "socket,setsockopt,bind");
return (unsigned int)*a2;
}
v9 = _errno_location();
v6 = v9;
if ( *v9 != 98 )
{
bsock_syslog((unsigned int)*v9, 3LL, "socket,setsockopt,bind");
v3 = v2;
goto LABEL_10;
}
v3 = v2;
LABEL_16:
if ( !setsockopt(v2, 1, 2, &optval, 4u) && !bind(v2, *(const struct sockaddr **)(a1 + 24), *(_DWORD *)(a1 + 16)) )
{
*a2 = v2;
if ( (*(_BYTE *)a1 & 1) == 0 && (unsigned __int8)bsock_addrinfo_to_strs(a1, v11, v12, 112LL) )
bsock_syslog(0LL, 4LL, "bind SO_REUSEADDR: %s %s %s %s %s", v11[0], v11[1], v11[2], v11[3], v11[4]);
return (unsigned int)*a2;
}
bsock_syslog((unsigned int)*v6, 3LL, "socket,setsockopt,bind");
if ( v2 != -1 )
goto LABEL_9;
LABEL_10:
if ( v3 != -1 )
func2(v3);
return (unsigned int)*a2;
}

original_code:
__attribute__ ((__noinline__))
__attribute__((__nonnull__ ))
static int
bsock_resvaddr_rebind (const struct addrinfo * restrict ai,
                       int * const restrict tfd)
{
    int fd[] = { -1, -1 }, flag = 1;
    if (-1 != (fd[0] = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol))
        && (fd[1] = *tfd, *tfd = -1, -1 == fd[1] || 0 == nointr_close(fd[1]))
        && (fd[1] = -1, 0 == bind(fd[0], ai->ai_addr, ai->ai_addrlen))) {
        *tfd = fd[0]; fd[0] = -1;
    }
    else if ((2 == ai->ai_family || 10 == ai->ai_family)
             && (*__errno_location ()) == 98
             && 0==setsockopt(fd[0],1,2,&flag,sizeof(flag))
             && 0==bind(fd[0], ai->ai_addr, ai->ai_addrlen)) {
        *tfd = fd[0]; fd[0] = -1;
        if (!(ai->ai_flags & 0x0001)) {
            struct bsock_addrinfo_strs aistr;
            char bufstr[112];
            if (bsock_addrinfo_to_strs(ai, &aistr, bufstr, sizeof(bufstr))) {
                bsock_syslog(0,4,"bind SO_REUSEADDR: %s %s %s %s %s",
                             aistr.family, aistr.socktype, aistr.protocol,
                             aistr.service, aistr.addr);
            }
        }
    }
    else {
        bsock_syslog((*__errno_location ()), 3, "socket,setsockopt,bind");
        if (-1 != fd[0])
            nointr_close(fd[0]);
        if (-1 != fd[1])
            nointr_close(fd[1]);
    }
    return *tfd;
}

canonical_original_code:
static int bsock_resvaddr_rebind(struct addrinfo *ai, int *tfd)
{
    int fd[] = { -1, -1 }; int flag = 1;
    if (-1 != (fd[0] = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol))
        && (fd[1] = *tfd, *tfd = -1, -1 == fd[1] || 0 == nointr_close(fd[1]))
        && (fd[1] = -1, 0 == bind(fd[0], ai->ai_addr, ai->ai_addrlen))) {
        *tfd = fd[0]; fd[0] = -1;
    }
    else if ((2 == ai->ai_family || 10 == ai->ai_family)
             && (*__errno_location ()) == 98
             && 0==setsockopt(fd[0],1,2,&flag,sizeof(flag))
             && 0==bind(fd[0], ai->ai_addr, ai->ai_addrlen)) {
        *tfd = fd[0]; fd[0] = -1;
        if (!(ai->ai_flags & 0x0001)) {
            struct bsock_addrinfo_strs aistr;
            char bufstr[112];
            if (bsock_addrinfo_to_strs(ai, &aistr, bufstr, sizeof(bufstr))) {
                bsock_syslog(0,4,"bind SO_REUSEADDR: %s %s %s %s %s",
                             aistr.family, aistr.socktype, aistr.protocol,
                             aistr.service, aistr.addr);
            }
        }
    }
    else {
        bsock_syslog((*__errno_location ()), 3, "socket,setsockopt,bind");
        if (-1 != fd[0])
            nointr_close(fd[0]);
        if (-1 != fd[1])
            nointr_close(fd[1]);
    }
    return *tfd;
}

variable_types:
tfd
int *

variable_types:
fd
int []

variable_types: flag int

variable_types: struct bsock_addrinfo_strs { char *family; char *socktype; char *protocol; char *service; char *addr; }

variable_types:
bufstr
char [112]


decompiled_code:
__int64 __fastcall bsock_resvaddr_fd(__int64 a1)
{
int *v1; // rbx
unsigned int v2; // er12
int v3; // eax
__int64 v4; // rbp
int v5; // eax
__int64 result; // rax

v1 = *(int **)(*(_QWORD *)bsock_resvaddr_alloc
+ 8LL
* (((unsigned int)*((_QWORD *)bsock_resvaddr_alloc + 1) - 1) & (unsigned int)bsock_resvaddr_hash(a1)));
if ( !v1 )
return 0xFFFFFFFFLL;
v2 = *(_DWORD *)(a1 + 16);
while ( 1 )
{
v4 = *((_QWORD *)v1 + 1);
if ( v2 == *(_DWORD *)(v4 + 16)
&& !memcmp(*(const void **)(a1 + 24), *(const void **)(v4 + 24), v2)
&& *(_DWORD *)(a1 + 4) == *(_DWORD *)(v4 + 4) )
{
v5 = *(_DWORD *)(a1 + 8);
if ( *(_DWORD *)(v4 + 8) == v5 || !v5 )
{
v3 = *(_DWORD *)(a1 + 12);
if ( *(_DWORD *)(v4 + 12) == v3 || !v3 )
break;
}
}
v1 = *(int **)v1;
if ( !v1 )
return 0xFFFFFFFFLL;
}
result = (unsigned int)v1[4];
if ( (_DWORD)result == -1 || (*(_BYTE *)a1 & 1) != 0 )
result = bsock_resvaddr_rebind(a1, v1 + 4);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
int *v1; // rbx
unsigned int v2; // er12
int v3; // eax
__int64 v4; // rbp
int v5; // eax
__int64 result; // rax

v1 = *(int **)(*(_QWORD *)bsock_resvaddr_alloc
+ 8LL
* (((unsigned int)*((_QWORD *)bsock_resvaddr_alloc + 1) - 1) & (unsigned int)func1(a1)));
if ( !v1 )
return 0xFFFFFFFFLL;
v2 = *(_DWORD *)(a1 + 16);
while ( 1 )
{
v4 = *((_QWORD *)v1 + 1);
if ( v2 == *(_DWORD *)(v4 + 16)
&& !memcmp(*(const void **)(a1 + 24), *(const void **)(v4 + 24), v2)
&& *(_DWORD *)(a1 + 4) == *(_DWORD *)(v4 + 4) )
{
v5 = *(_DWORD *)(a1 + 8);
if ( *(_DWORD *)(v4 + 8) == v5 || !v5 )
{
v3 = *(_DWORD *)(a1 + 12);
if ( *(_DWORD *)(v4 + 12) == v3 || !v3 )
break;
}
}
v1 = *(int **)v1;
if ( !v1 )
return 0xFFFFFFFFLL;
}
result = (unsigned int)v1[4];
if ( (_DWORD)result == -1 || (*(_BYTE *)a1 & 1) != 0 )
result = func3(a1, v1 + 4);
return result;
}

original_code:
__attribute__((__nonnull__ ))
int
bsock_resvaddr_fd (const struct addrinfo * const restrict ai)
{
    const uint32_t h = bsock_resvaddr_hash(ai);
    struct bsock_resvaddr_alloc * const ar = bsock_resvaddr_alloc;
    struct bsock_resvaddr * restrict t = ar->table[h & (ar->table_sz-1)];
    const struct addrinfo * restrict tai;
    for (; ((void *)0) != t; t = t->next) {
        tai = t->ai;
        if ( ai->ai_addrlen == tai->ai_addrlen
            && 0 == memcmp(ai->ai_addr, tai->ai_addr, ai->ai_addrlen)
            && ai->ai_family == tai->ai_family
            && (ai->ai_socktype == tai->ai_socktype || 0 == ai->ai_socktype)
            && (ai->ai_protocol == tai->ai_protocol || 0 == ai->ai_protocol))
            return (-1 != t->fd && !(ai->ai_flags & 0x0001))
              ? t->fd
              : bsock_resvaddr_rebind(ai, &t->fd);
    }
    return -1;
}

canonical_original_code:
int bsock_resvaddr_fd(struct addrinfo *ai)
{
    unsigned int h = bsock_resvaddr_hash(ai);
    struct bsock_resvaddr_alloc *ar = bsock_resvaddr_alloc;
    struct bsock_resvaddr *t = ar->table[h & (ar->table_sz-1)];
    struct addrinfo *tai;
    for (; ((void *)0) != t; t = t->next) {
        tai = t->ai;
        if ( ai->ai_addrlen == tai->ai_addrlen
            && 0 == memcmp(ai->ai_addr, tai->ai_addr, ai->ai_addrlen)
            && ai->ai_family == tai->ai_family
            && (ai->ai_socktype == tai->ai_socktype || 0 == ai->ai_socktype)
            && (ai->ai_protocol == tai->ai_protocol || 0 == ai->ai_protocol))
            return (-1 != t->fd && !(ai->ai_flags & 0x0001))
              ? t->fd
              : bsock_resvaddr_rebind(ai, &t->fd);
    }
    return -1;
}

variable_types: struct bsock_resvaddr_alloc { struct bsock_resvaddr { struct bsock_resvaddr *next; 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; } *ai; int fd; } **table; unsigned long table_sz; unsigned long elt_count; struct bsock_resvaddr *elt; struct addrinfo *ai; char *buf; unsigned int buf_sz; struct bsock_resvaddr_alloc *prev; } *

user_defined_types: struct bsock_resvaddr_alloc { struct bsock_resvaddr **table; unsigned long table_sz; unsigned long elt_count; struct bsock_resvaddr *elt; struct addrinfo *ai; char *buf; unsigned int buf_sz; struct bsock_resvaddr_alloc *prev; }

user_defined_types: struct bsock_resvaddr { struct bsock_resvaddr *next; struct addrinfo *ai; int fd; }


decompiled_code:
__int64 __fastcall nointr_close(int fd)
{
unsigned int v1; // ebx

do
v1 = close(fd);
while ( v1 && *_errno_location() == 4 );
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd)
{
unsigned int v1; // ebx

do
v1 = close(fd);
while ( v1 && *_errno_location() == 4 );
return v1;
}

original_code:
__attribute__ ((__noinline__))
static int
nointr_close (const int fd)
{ int r; do {(r = close(fd));} while (__builtin_expect((r != 0),0) && __builtin_expect((*__errno_location ())==4, 1)); return r; }

canonical_original_code:
static int nointr_close(int fd)
{ int r; do {(r = close(fd));} while (__builtin_expect((r != 0),0) && __builtin_expect((*__errno_location ())==4, 1)); return r; }

variable_types: fd int

variable_types: r int


repo: gstrauss/mcdb

decompiled_code:
__int64 __fastcall nss_mcdb_setent(unsigned int a1)
{
__int64 v1; // rax
__int64 result; // rax
__int64 v3; // rbp

if ( *(_QWORD *)((char *)nss_mcdb_db_openshared + 64 * (unsigned __int64)a1 + _tls_get_addr(&nss_mcdb_st_tlsind))
|| (v3 = nss_mcdb_db_getshared(a1),
*(_QWORD *)((char *)nss_mcdb_db_openshared + 64 * (unsigned __int64)a1 + _tls_get_addr(&nss_mcdb_st_tlsind)) = v3,
result = 0xFFFFFFFFLL,
v3) )
{
v1 = _tls_get_addr(&nss_mcdb_st_tlsind);
*(_QWORD *)((char *)nss_mcdb_db_openshared + 64 * (unsigned __int64)a1 + v1 + 24) = **(_QWORD **)((char *)nss_mcdb_db_openshared + 64 * (unsigned __int64)a1 + v1)
+ 4096LL;
result = 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned int a1)
{
__int64 v1; // rax
__int64 result; // rax
__int64 v3; // rbp

if ( *(_QWORD *)((char *)nss_mcdb_db_openshared + 64 * (unsigned __int64)a1 + _tls_get_addr(&nss_mcdb_st_tlsind))
|| (v3 = func1(a1),
*(_QWORD *)((char *)nss_mcdb_db_openshared + 64 * (unsigned __int64)a1 + _tls_get_addr(&nss_mcdb_st_tlsind)) = v3,
result = 0xFFFFFFFFLL,
v3) )
{
v1 = _tls_get_addr(&nss_mcdb_st_tlsind);
*(_QWORD *)((char *)nss_mcdb_db_openshared + 64 * (unsigned __int64)a1 + v1 + 24) = **(_QWORD **)((char *)nss_mcdb_db_openshared + 64 * (unsigned __int64)a1 + v1)
+ 4096LL;
result = 1LL;
}
return result;
}

original_code:
__attribute__ ((__noinline__))
__attribute__((__visibility__("internal"))) nss_status_t
nss_mcdb_setent(const enum nss_dbtype dbtype,
                const int stayopen __attribute__((__unused__)))
{
    struct mcdb * const restrict m = &_nss_mcdb_st[dbtype];
    if (m->map != ((void *)0) || (m->map = _nss_mcdb_db_getshared(dbtype)) != ((void *)0)) {
        m->hpos = (uintptr_t)(m->map->ptr + ((1u<<8)<<4));
        return NSS_STATUS_SUCCESS;
    }
    return NSS_STATUS_UNAVAIL;
}

canonical_original_code:
enum nss_status nss_mcdb_setent(enum nss_dbtype dbtype, int stayopen)
{
    struct mcdb *m = &_nss_mcdb_st[dbtype];
    if (m->map != ((void *)0) || (m->map = _nss_mcdb_db_getshared(dbtype)) != ((void *)0)) {
        m->hpos = (unsigned long)(m->map->ptr + ((1u<<8)<<4));
        return NSS_STATUS_SUCCESS;
    }
    return NSS_STATUS_UNAVAIL;
}

variable_types: enum nss_dbtype { NSS_DBTYPE_ALIASES=0, NSS_DBTYPE_ETHERS=1, NSS_DBTYPE_GROUP=2, NSS_DBTYPE_HOSTS=3, NSS_DBTYPE_NETGROUP=4, NSS_DBTYPE_NETWORKS=5, NSS_DBTYPE_PASSWD=6, NSS_DBTYPE_PROTOCOLS=7, NSS_DBTYPE_PUBLICKEY=8, NSS_DBTYPE_RPC=9, NSS_DBTYPE_SERVICES=10, NSS_DBTYPE_SHADOW=11, NSS_DBTYPE_SENTINEL=12 }

variable_types: stayopen int

variable_types: struct mcdb { struct mcdb_mmap { unsigned char *ptr; unsigned int b; unsigned int n; unsigned int hash_init; unsigned int hash_pad; unsigned int (*hash_fn)(unsigned int, void *, unsigned long); unsigned long size; long mtime; struct mcdb_mmap *next; void *(*fn_malloc)(unsigned long); void (*fn_free)(void *); char *fname; char fnamebuf[112]; int allocated; int dfd; unsigned int refcnt; } *map; unsigned int loop; unsigned int hslots; unsigned long kpos; unsigned long hpos; unsigned long dpos; unsigned int dlen; unsigned int klen; unsigned int khash; unsigned int pad0; void *vp; } *

return_type: enum nss_status { NSS_STATUS_TRYAGAIN=-2, NSS_STATUS_UNAVAIL=-1, NSS_STATUS_NOTFOUND=0, NSS_STATUS_SUCCESS=1, NSS_STATUS_RETURN=2 }

user_defined_types: struct mcdb { struct mcdb_mmap *map; unsigned int loop; unsigned int hslots; unsigned long kpos; unsigned long hpos; unsigned long dpos; unsigned int dlen; unsigned int klen; unsigned int khash; unsigned int pad0; void *vp; }


decompiled_code:
__int64 __fastcall nss_mcdb_endent(unsigned int a1)
{
__int64 *v1; // rax
__int64 v2; // rdx
__int64 result; // rax
__int64 v4; // [rsp+8h] [rbp-10h] BYREF

v1 = (__int64 *)((char *)nss_mcdb_db_openshared + 64 * (unsigned __int64)a1 + _tls_get_addr(&nss_mcdb_st_tlsind));
v4 = *v1;
v2 = v4;
*v1 = 0LL;
result = 1LL;
if ( v2 )
result = mcdb_mmap_thread_registration_h(&v4, 0LL) == 0 ? -1 : 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned int a1)
{
__int64 *v1; // rax
__int64 v2; // rdx
__int64 result; // rax
__int64 v4; // [rsp+8h] [rbp-10h] BYREF

v1 = (__int64 *)((char *)nss_mcdb_db_openshared + 64 * (unsigned __int64)a1 + _tls_get_addr(&nss_mcdb_st_tlsind));
v4 = *v1;
v2 = v4;
*v1 = 0LL;
result = 1LL;
if ( v2 )
result = mcdb_mmap_thread_registration_h(&v4, 0LL) == 0 ? -1 : 1;
return result;
}

original_code:
__attribute__((__visibility__("internal"))) nss_status_t
nss_mcdb_endent(const enum nss_dbtype dbtype)
{
    struct mcdb * const restrict m = &_nss_mcdb_st[dbtype];
    struct mcdb_mmap *map = m->map;
    m->map = ((void *)0);
    return (map == ((void *)0) || mcdb_mmap_thread_registration_h(&(map), MCDB_REGISTER_USE_DECR))
      ? NSS_STATUS_SUCCESS
      : NSS_STATUS_UNAVAIL;
}

canonical_original_code:
enum nss_status nss_mcdb_endent(enum nss_dbtype dbtype)
{
    struct mcdb *m = &_nss_mcdb_st[dbtype];
    struct mcdb_mmap *map = m->map;
    m->map = ((void *)0);
    return (map == ((void *)0) || mcdb_mmap_thread_registration_h(&(map), MCDB_REGISTER_USE_DECR))
      ? NSS_STATUS_SUCCESS
      : NSS_STATUS_UNAVAIL;
}

variable_types: struct mcdb_mmap { unsigned char *ptr; unsigned int b; unsigned int n; unsigned int hash_init; unsigned int hash_pad; unsigned int (*hash_fn)(unsigned int, void *, unsigned long); unsigned long size; long mtime; struct mcdb_mmap *next; void *(*fn_malloc)(unsigned long); void (*fn_free)(void *); char *fname; char fnamebuf[112]; int allocated; int dfd; unsigned int refcnt; } *

user_defined_types: enum nss_dbtype { NSS_DBTYPE_ALIASES=0, NSS_DBTYPE_ETHERS=1, NSS_DBTYPE_GROUP=2, NSS_DBTYPE_HOSTS=3, NSS_DBTYPE_NETGROUP=4, NSS_DBTYPE_NETWORKS=5, NSS_DBTYPE_PASSWD=6, NSS_DBTYPE_PROTOCOLS=7, NSS_DBTYPE_PUBLICKEY=8, NSS_DBTYPE_RPC=9, NSS_DBTYPE_SERVICES=10, NSS_DBTYPE_SHADOW=11, NSS_DBTYPE_SENTINEL=12 }

user_defined_types: enum nss_status { NSS_STATUS_TRYAGAIN=-2, NSS_STATUS_UNAVAIL=-1, NSS_STATUS_NOTFOUND=0, NSS_STATUS_SUCCESS=1, NSS_STATUS_RETURN=2 }


decompiled_code:
__int64 __fastcall nss_mcdb_getent(unsigned int a1, __int64 a2)
{
_QWORD *v2; // r15
__int64 v3; // rbp
unsigned int v4; // er12
__int64 v5; // rbx
char *v6; // r13
__int64 result; // rax
__int64 v8; // rax
__int64 v9[2]; // [rsp+10h] [rbp-58h] BYREF
unsigned int v10; // [rsp+20h] [rbp-48h]
unsigned int v11; // [rsp+24h] [rbp-44h]
_QWORD *v12; // [rsp+28h] [rbp-40h]

v2 = (_QWORD *)((char *)nss_mcdb_db_openshared + 64 * (unsigned __int64)a1 + _tls_get_addr(&nss_mcdb_st_tlsind));
if ( *v2 || (unsigned int)nss_mcdb_setent(a1) == 1 )
{
mcdb_iter_init_h(v9, v2);
v9[0] = *(__int64 *)((char *)&qword_1488[8 * (unsigned __int64)a1 - 654] + _tls_get_addr(&nss_mcdb_st_tlsind));
while ( (unsigned __int8)mcdb_iter_h(v9) )
{
v3 = v9[0];
v4 = v11;
v5 = -(__int64)v11;
if ( *(_BYTE *)(v9[0] + v5 - v10) == 61 )
{
v6 = (char *)nss_mcdb_db_openshared + 64 * (unsigned __int64)a1 + _tls_get_addr(&nss_mcdb_st_tlsind);
*((_QWORD *)v6 + 3) = v3;
*((_QWORD *)v6 + 4) = v5 + v3 - *v12;
*((_DWORD *)v6 + 10) = v4;
return (*(__int64 (__fastcall **)(_QWORD *, __int64))a2)(v2, a2);
}
}
v8 = _tls_get_addr(&nss_mcdb_st_tlsind);
*(__int64 *)((char *)&qword_1488[8 * (unsigned __int64)a1 - 654] + v8) = v9[0];
*_errno_location() = 2;
**(_DWORD **)(a2 + 32) = 2;
result = 0LL;
}
else
{
**(_DWORD **)(a2 + 32) = *_errno_location();
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(unsigned int a1, __int64 a2)
{
_QWORD *v2; // r15
__int64 v3; // rbp
unsigned int v4; // er12
__int64 v5; // rbx
char *v6; // r13
__int64 result; // rax
__int64 v8; // rax
__int64 v9[2]; // [rsp+10h] [rbp-58h] BYREF
unsigned int v10; // [rsp+20h] [rbp-48h]
unsigned int v11; // [rsp+24h] [rbp-44h]
_QWORD *v12; // [rsp+28h] [rbp-40h]

v2 = (_QWORD *)((char *)nss_mcdb_db_openshared + 64 * (unsigned __int64)a1 + _tls_get_addr(&nss_mcdb_st_tlsind));
if ( *v2 || (unsigned int)func3(a1) == 1 )
{
mcdb_iter_init_h(v9, v2);
v9[0] = *(__int64 *)((char *)&qword_1488[8 * (unsigned __int64)a1 - 654] + _tls_get_addr(&nss_mcdb_st_tlsind));
while ( (unsigned __int8)mcdb_iter_h(v9) )
{
v3 = v9[0];
v4 = v11;
v5 = -(__int64)v11;
if ( *(_BYTE *)(v9[0] + v5 - v10) == 61 )
{
v6 = (char *)nss_mcdb_db_openshared + 64 * (unsigned __int64)a1 + _tls_get_addr(&nss_mcdb_st_tlsind);
*((_QWORD *)v6 + 3) = v3;
*((_QWORD *)v6 + 4) = v5 + v3 - *v12;
*((_DWORD *)v6 + 10) = v4;
return (*(__int64 (__fastcall **)(_QWORD *, __int64))a2)(v2, a2);
}
}
v8 = _tls_get_addr(&nss_mcdb_st_tlsind);
*(__int64 *)((char *)&qword_1488[8 * (unsigned __int64)a1 - 654] + v8) = v9[0];
*_errno_location() = 2;
**(_DWORD **)(a2 + 32) = 2;
result = 0LL;
}
else
{
**(_DWORD **)(a2 + 32) = *_errno_location();
result = 0xFFFFFFFFLL;
}
return result;
}

original_code:
__attribute__((__visibility__("internal"))) nss_status_t
nss_mcdb_getent(const enum nss_dbtype dbtype,
                const struct nss_mcdb_vinfo * const restrict v)
{
    struct mcdb_iter iter;
    struct mcdb * const m = &_nss_mcdb_st[dbtype];
    if (__builtin_expect(m->map == ((void *)0), 0)
        && nss_mcdb_setent(dbtype,0) != NSS_STATUS_SUCCESS) {
        *v->errnop = (*__errno_location ());
        return NSS_STATUS_UNAVAIL;
    }
    mcdb_iter_init_h(&iter, m);
    iter.ptr = (unsigned char *)m->hpos;
    while (mcdb_iter_h(&iter)) {
        if (((&iter)->ptr-(&iter)->dlen-(&iter)->klen)[0] == (unsigned char)'=') {
            m->hpos = (uintptr_t)iter.ptr;
            m->dpos = (uintptr_t)((&iter)->ptr-(&iter)->dlen-(&iter)->map->ptr);
            m->dlen = ((&iter)->dlen);
            return v->decode(m, v);
        }
    }
    m->hpos = (uintptr_t)iter.ptr;
    *v->errnop = (*__errno_location ()) = 2;
    return NSS_STATUS_NOTFOUND;
}

canonical_original_code:
enum nss_status nss_mcdb_getent(enum nss_dbtype dbtype, struct nss_mcdb_vinfo *v)
{
    struct mcdb_iter iter;
    struct mcdb *m = &_nss_mcdb_st[dbtype];
    if (__builtin_expect(m->map == ((void *)0), 0)
        && nss_mcdb_setent(dbtype,0) != NSS_STATUS_SUCCESS) {
        *v->errnop = (*__errno_location ());
        return NSS_STATUS_UNAVAIL;
    }
    mcdb_iter_init_h(&iter, m);
    iter.ptr = (unsigned char *)m->hpos;
    while (mcdb_iter_h(&iter)) {
        if (((&iter)->ptr-(&iter)->dlen-(&iter)->klen)[0] == (unsigned char)'=') {
            m->hpos = (unsigned long)iter.ptr;
            m->dpos = (unsigned long)((&iter)->ptr-(&iter)->dlen-(&iter)->map->ptr);
            m->dlen = ((&iter)->dlen);
            return v->decode(m, v);
        }
    }
    m->hpos = (unsigned long)iter.ptr;
    *v->errnop = (*__errno_location ()) = 2;
    return NSS_STATUS_NOTFOUND;
}

variable_types: struct nss_mcdb_vinfo { enum nss_status { NSS_STATUS_TRYAGAIN=-2, NSS_STATUS_UNAVAIL=-1, NSS_STATUS_NOTFOUND=0, NSS_STATUS_SUCCESS=1, NSS_STATUS_RETURN=2 } (*decode)(struct mcdb { struct mcdb_mmap { unsigned char *ptr; unsigned int b; unsigned int n; unsigned int hash_init; unsigned int hash_pad; unsigned int (*hash_fn)(unsigned int, void *, unsigned long); unsigned long size; long mtime; struct mcdb_mmap *next; void *(*fn_malloc)(unsigned long); void (*fn_free)(void *); char *fname; char fnamebuf[112]; int allocated; int dfd; unsigned int refcnt; } *map; unsigned int loop; unsigned int hslots; unsigned long kpos; unsigned long hpos; unsigned long dpos; unsigned int dlen; unsigned int klen; unsigned int khash; unsigned int pad0; void *vp; } *, struct nss_mcdb_vinfo *); void *vstruct; char *buf; unsigned long bufsz; int *errnop; char *key; unsigned long klen; unsigned char tagc; } *

variable_types: struct mcdb_iter { unsigned char *ptr; unsigned char *eod; unsigned int klen; unsigned int dlen; struct mcdb_mmap { unsigned char *ptr; unsigned int b; unsigned int n; unsigned int hash_init; unsigned int hash_pad; unsigned int (*hash_fn)(unsigned int, void *, unsigned long); unsigned long size; long mtime; struct mcdb_mmap *next; void *(*fn_malloc)(unsigned long); void (*fn_free)(void *); char *fname; char fnamebuf[112]; int allocated; int dfd; unsigned int refcnt; } *map; }

user_defined_types: struct mcdb_iter { unsigned char *ptr; unsigned char *eod; unsigned int klen; unsigned int dlen; struct mcdb_mmap *map; }

user_defined_types: struct nss_mcdb_vinfo { enum nss_status (*decode)(struct mcdb *, struct nss_mcdb_vinfo *); void *vstruct; char *buf; unsigned long bufsz; int *errnop; char *key; unsigned long klen; unsigned char tagc; }


decompiled_code:
__int64 __fastcall nss_mcdb_getentstart(unsigned int a1, __int64 a2)
{
char *v2; // r13
unsigned int v3; // ebp

v2 = (char *)nss_mcdb_db_openshared + 64 * (unsigned __int64)a1 + _tls_get_addr(&nss_mcdb_st_tlsind);
if ( (unsigned int)nss_mcdb_setent(a1) == 1 )
{
v3 = 1;
if ( !(unsigned __int8)mcdb_findtagstart_h(
v2,
*(_QWORD *)(a2 + 40),
*(_QWORD *)(a2 + 48),
*(unsigned __int8 *)(a2 + 56))
|| !(unsigned __int8)mcdb_findtagnext_h(
v2,
*(_QWORD *)(a2 + 40),
*(_QWORD *)(a2 + 48),
*(unsigned __int8 *)(a2 + 56)) )
{
*_errno_location() = 2;
**(_DWORD **)(a2 + 32) = 2;
v3 = 0;
}
}
else
{
**(_DWORD **)(a2 + 32) = *_errno_location();
v3 = -1;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func6(unsigned int a1, __int64 a2)
{
char *v2; // r13
unsigned int v3; // ebp

v2 = (char *)nss_mcdb_db_openshared + 64 * (unsigned __int64)a1 + _tls_get_addr(&nss_mcdb_st_tlsind);
if ( (unsigned int)func3(a1) == 1 )
{
v3 = 1;
if ( !(unsigned __int8)mcdb_findtagstart_h(
v2,
*(_QWORD *)(a2 + 40),
*(_QWORD *)(a2 + 48),
*(unsigned __int8 *)(a2 + 56))
|| !(unsigned __int8)mcdb_findtagnext_h(
v2,
*(_QWORD *)(a2 + 40),
*(_QWORD *)(a2 + 48),
*(unsigned __int8 *)(a2 + 56)) )
{
*_errno_location() = 2;
**(_DWORD **)(a2 + 32) = 2;
v3 = 0;
}
}
else
{
**(_DWORD **)(a2 + 32) = *_errno_location();
v3 = -1;
}
return v3;
}

original_code:
__attribute__((__visibility__("internal"))) nss_status_t
nss_mcdb_getentstart(const enum nss_dbtype dbtype,
                     const struct nss_mcdb_vinfo * const restrict v)
{
    struct mcdb * const m = &_nss_mcdb_st[dbtype];
    if (__builtin_expect( nss_mcdb_setent(dbtype,0) != NSS_STATUS_SUCCESS, 0)) {
        *v->errnop = (*__errno_location ());
        return NSS_STATUS_UNAVAIL;
    }
    if ( __builtin_expect( mcdb_findtagstart_h(m,v->key,v->klen,v->tagc), 1)
        && __builtin_expect( mcdb_findtagnext_h(m,v->key,v->klen,v->tagc), 1))
        return NSS_STATUS_SUCCESS;
    *v->errnop = (*__errno_location ()) = 2;
    return NSS_STATUS_NOTFOUND;
}

canonical_original_code:
enum nss_status nss_mcdb_getentstart(enum nss_dbtype dbtype, struct nss_mcdb_vinfo *v)
{
    struct mcdb *m = &_nss_mcdb_st[dbtype];
    if (__builtin_expect( nss_mcdb_setent(dbtype,0) != NSS_STATUS_SUCCESS, 0)) {
        *v->errnop = (*__errno_location ());
        return NSS_STATUS_UNAVAIL;
    }
    if ( __builtin_expect( mcdb_findtagstart_h(m,v->key,v->klen,v->tagc), 1)
        && __builtin_expect( mcdb_findtagnext_h(m,v->key,v->klen,v->tagc), 1))
        return NSS_STATUS_SUCCESS;
    *v->errnop = (*__errno_location ()) = 2;
    return NSS_STATUS_NOTFOUND;
}

user_defined_types: struct mcdb_mmap { unsigned char *ptr; unsigned int b; unsigned int n; unsigned int hash_init; unsigned int hash_pad; unsigned int (*hash_fn)(unsigned int, void *, unsigned long); unsigned long size; long mtime; struct mcdb_mmap *next; void *(*fn_malloc)(unsigned long); void (*fn_free)(void *); char *fname; char fnamebuf[112]; int allocated; int dfd; unsigned int refcnt; }