repo: gstrauss/bsock

decompiled_code:
__int64 __fastcall bsock_bind_send_addr_and_recv(int fd2, __int64 a2, unsigned int a3)
{
unsigned int v5; // er12
int v7; // edi
unsigned int v8; // er14
unsigned int v9; // er12
unsigned int v10; // ebp
int v11; // eax
int fd; // [rsp+4h] [rbp-44h] BYREF
int v13; // [rsp+8h] [rbp-40h] BYREF
unsigned int v14; // [rsp+Ch] [rbp-3Ch] BYREF
__int64 v15[7]; // [rsp+10h] [rbp-38h] BYREF

fd = -1;
v13 = 1;
v14 = 0;
v15[0] = (__int64)&v14;
v15[1] = 4LL;
if ( (unsigned __int8)bsock_addrinfo_send(a3, a2, (unsigned int)fd2)
&& (unsigned int)retry_poll_fd_constprop_0(a3) == 1
&& bsock_unix_recv_fds(a3, &fd, &v13, v15, 1LL) != -1 )
{
v7 = fd;
v5 = v14;
if ( fd != -1 )
{
if ( v14 )
{
LABEL_8:
nointr_close(v7);
return v14;
}
v8 = fcntl64((unsigned int)fd2, 3LL, 0LL);
v9 = fcntl64((unsigned int)fd2, 1LL, 0LL);
do
{
v14 = dup2(fd, fd2);
v10 = v14;
if ( v14 != -1 )
break;
v11 = *_errno_location();
}
while ( v11 == 4 || v11 == 16 );
if ( v10 == fd2 )
{
v14 = 0;
}
else
{
v14 = *_errno_location();
if ( v14 )
{
LABEL_18:
v7 = fd;
goto LABEL_8;
}
}
fcntl64((unsigned int)fd2, 4LL, v8);
fcntl64((unsigned int)fd2, 2LL, v9);
goto LABEL_18;
}
return v5;
}
v5 = *_errno_location();
v14 = v5;
if ( v5 != 32 )
return v5;
if ( bsock_unix_recv_fds(a3, 0LL, 0LL, v15, 1LL) != -1 )
v5 = v14;
return v5;
}

canonical_decompiled_code:
__int64 __fastcall func2(int fd2, __int64 a2, unsigned int a3)
{
unsigned int v5; // er12
int v7; // edi
unsigned int v8; // er14
unsigned int v9; // er12
unsigned int v10; // ebp
int v11; // eax
int fd; // [rsp+4h] [rbp-44h] BYREF
int v13; // [rsp+8h] [rbp-40h] BYREF
unsigned int v14; // [rsp+Ch] [rbp-3Ch] BYREF
__int64 v15[7]; // [rsp+10h] [rbp-38h] BYREF

fd = -1;
v13 = 1;
v14 = 0;
v15[0] = (__int64)&v14;
v15[1] = 4LL;
if ( (unsigned __int8)bsock_addrinfo_send(a3, a2, (unsigned int)fd2)
&& (unsigned int)func1(a3) == 1
&& bsock_unix_recv_fds(a3, &fd, &v13, v15, 1LL) != -1 )
{
v7 = fd;
v5 = v14;
if ( fd != -1 )
{
if ( v14 )
{
LABEL_8:
func0(v7);
return v14;
}
v8 = fcntl64((unsigned int)fd2, 3LL, 0LL);
v9 = fcntl64((unsigned int)fd2, 1LL, 0LL);
do
{
v14 = dup2(fd, fd2);
v10 = v14;
if ( v14 != -1 )
break;
v11 = *_errno_location();
}
while ( v11 == 4 || v11 == 16 );
if ( v10 == fd2 )
{
v14 = 0;
}
else
{
v14 = *_errno_location();
if ( v14 )
{
LABEL_18:
v7 = fd;
goto LABEL_8;
}
}
fcntl64((unsigned int)fd2, 4LL, v8);
fcntl64((unsigned int)fd2, 2LL, v9);
goto LABEL_18;
}
return v5;
}
v5 = *_errno_location();
v14 = v5;
if ( v5 != 32 )
return v5;
if ( bsock_unix_recv_fds(a3, 0LL, 0LL, v15, 1LL) != -1 )
v5 = v14;
return v5;
}

original_code:
__attribute__((__nonnull__ ))
static int
bsock_bind_send_addr_and_recv (const int fd,
                               const struct addrinfo * const restrict ai,
                               const int sfd)
{
    int rfd = -1;
    unsigned int nrfd = 1;
    int errnum = 0;
    struct iovec iov = { .iov_base = &errnum, .iov_len = sizeof(errnum) };
    if (!MSG_DONTWAIT)
        (void)fcntl(sfd, 4, fcntl(sfd, 3, 0) | 04000);
    if (bsock_addrinfo_send(sfd, ai, fd)
        && 1 == retry_poll_fd(sfd, 0x001, 5000)
        && -1 != bsock_unix_recv_fds(sfd, &rfd, &nrfd, &iov, 1)) {
        if (-1 != rfd) {
            if (0 == errnum) {
                const int flflags = fcntl(fd, 3, 0);
                const int fdflags = fcntl(fd, 1, 0);
                do { errnum = dup2(rfd, fd);
                } while (errnum == -1 && ((*__errno_location ()) == 4 || (*__errno_location ()) == 16));
                if (0 == (errnum = (errnum == fd) ? 0 : (*__errno_location ()))) {
                    (void)fcntl(fd, 4, flflags);
                    (void)fcntl(fd, 2, fdflags);
                }
            }
            nointr_close(rfd);
        }
    }
    else {
        errnum = (*__errno_location ());
        if (32 == errnum && -1 == bsock_unix_recv_fds(sfd,((void *)0),((void *)0),&iov,1))
            errnum = 32;
    }
    return errnum;
}

canonical_original_code:
static int bsock_bind_send_addr_and_recv(int fd, struct addrinfo *ai, int sfd)
{
    int rfd = -1;
    unsigned int nrfd = 1;
    int errnum = 0;
    struct iovec iov = { .iov_base = &errnum, .iov_len = sizeof(errnum) };
    if (!MSG_DONTWAIT)
        (void)fcntl(sfd, 4, fcntl(sfd, 3, 0) | 04000);
    if (bsock_addrinfo_send(sfd, ai, fd)
        && 1 == retry_poll_fd(sfd, 0x001, 5000)
        && -1 != bsock_unix_recv_fds(sfd, &rfd, &nrfd, &iov, 1)) {
        if (-1 != rfd) {
            if (0 == errnum) {
                int flflags = fcntl(fd, 3, 0);
                int fdflags = fcntl(fd, 1, 0);
                do { errnum = dup2(rfd, fd);
                } while (errnum == -1 && ((*__errno_location ()) == 4 || (*__errno_location ()) == 16));
                if (0 == (errnum = (errnum == fd) ? 0 : (*__errno_location ()))) {
                    (void)fcntl(fd, 4, flflags);
                    (void)fcntl(fd, 2, fdflags);
                }
            }
            nointr_close(rfd);
        }
    }
    else {
        errnum = (*__errno_location ());
        if (32 == errnum && -1 == bsock_unix_recv_fds(sfd,((void *)0),((void *)0),&iov,1))
            errnum = 32;
    }
    return errnum;
}

variable_types: fd 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: sfd int

variable_types: rfd int

variable_types:
nrfd
unsigned int

variable_types: errnum int

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

variable_types: flflags int

variable_types: fdflags 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; }


decompiled_code:
bool __fastcall bsock_bind_viasock(int fd2, __int64 a2)
{
unsigned int v2; // eax
int v3; // ebp
int v4; // ebx

while ( 1 )
{
v2 = bsock_unix_socket_connect("/usr/local/var/run/bsock/socket");
v3 = v2;
if ( v2 == -1 )
break;
while ( 1 )
{
v4 = bsock_bind_send_addr_and_recv(fd2, a2, v2);
nointr_close(v3);
if ( v4 != 11 )
break;
nanosleep(&ts_2, 0LL);
v2 = bsock_unix_socket_connect("/usr/local/var/run/bsock/socket");
v3 = v2;
if ( v2 == -1 )
return 0;
}
if ( v4 != 62 )
{
*_errno_location() = v4;
return v4 == 0;
}
}
return 0;
}

canonical_decompiled_code:
bool __fastcall func3(int fd2, __int64 a2)
{
unsigned int v2; // eax
int v3; // ebp
int v4; // ebx

while ( 1 )
{
v2 = bsock_unix_socket_connect("/usr/local/var/run/bsock/socket");
v3 = v2;
if ( v2 == -1 )
break;
while ( 1 )
{
v4 = func2(fd2, a2, v2);
func0(v3);
if ( v4 != 11 )
break;
nanosleep(&ts_2, 0LL);
v2 = bsock_unix_socket_connect("/usr/local/var/run/bsock/socket");
v3 = v2;
if ( v2 == -1 )
return 0;
}
if ( v4 != 62 )
{
*_errno_location() = v4;
return v4 == 0;
}
}
return 0;
}

original_code:
__attribute__((__nonnull__ ))
static _Bool
bsock_bind_viasock (const int fd, const struct addrinfo * const restrict ai)
{
    int errnum;
    int sfd;
    do {
        sfd = bsock_unix_socket_connect("/usr/local/var/run/bsock" "/socket");
        if (-1 == sfd)
            return 0;
        errnum = bsock_bind_send_addr_and_recv(fd, ai, sfd);
        nointr_close(sfd);
        if (errnum == 11) {
            static const struct timespec ts = { 0, 10L };
            nanosleep(&ts, ((void *)0));
        }
    } while (errnum == 11 || errnum == 62);
    (*__errno_location ()) = errnum;
    return (0 == errnum);
}

canonical_original_code:
static _Bool bsock_bind_viasock(int fd, struct addrinfo *ai)
{
    int errnum;
    int sfd;
    do {
        sfd = bsock_unix_socket_connect("/usr/local/var/run/bsock" "/socket");
        if (-1 == sfd)
            return 0;
        errnum = bsock_bind_send_addr_and_recv(fd, ai, sfd);
        nointr_close(sfd);
        if (errnum == 11) {
            static struct timespec ts = { 0, 10L };
            nanosleep(&ts, ((void *)0));
        }
    } while (errnum == 11 || errnum == 62);
    (*__errno_location ()) = errnum;
    return (0 == errnum);
}

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

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


decompiled_code:
bool __fastcall bsock_bind_viafork(int fd2, __int64 a2)
{
__pid_t v3; // ebx
int *v4; // rax
int *v5; // r13
int v6; // ebp
int fds[2]; // [rsp+8h] [rbp-C0h] BYREF
struct stat64 v8; // [rsp+10h] [rbp-B8h] BYREF

if ( stat64("/usr/local/sbin/bsock", &v8) )
return 0;
if ( (v8.st_mode & 0x800) == 0 )
{
*_errno_location() = 1;
return 0;
}
if ( socketpair(1, 1, 0, fds) )
return 0;
v3 = fork();
v4 = _errno_location();
v5 = v4;
if ( !v3 )
{
if ( !dup2(fds[0], 0) && (!fds[0] || !close(fds[0])) && (!fds[1] || !close(fds[1])) )
{
fcntl64(0LL, 2LL, 0LL);
execve(args_1, &args_1, environ);
}
exit(*v5);
}
if ( v3 == -1 )
{
v6 = *v4;
nointr_close(fds[0]);
}
else
{
nointr_close(fds[0]);
v6 = bsock_bind_send_addr_and_recv(fd2, a2, fds[1]);
while ( waitpid(v3, 0LL, 0) != v3 && *v5 == 4 )
;
}
nointr_close(fds[1]);
*v5 = v6;
return v6 == 0;
}

canonical_decompiled_code:
bool __fastcall func4(int fd2, __int64 a2)
{
__pid_t v3; // ebx
int *v4; // rax
int *v5; // r13
int v6; // ebp
int fds[2]; // [rsp+8h] [rbp-C0h] BYREF
struct stat64 v8; // [rsp+10h] [rbp-B8h] BYREF

if ( stat64("/usr/local/sbin/bsock", &v8) )
return 0;
if ( (v8.st_mode & 0x800) == 0 )
{
*_errno_location() = 1;
return 0;
}
if ( socketpair(1, 1, 0, fds) )
return 0;
v3 = fork();
v4 = _errno_location();
v5 = v4;
if ( !v3 )
{
if ( !dup2(fds[0], 0) && (!fds[0] || !close(fds[0])) && (!fds[1] || !close(fds[1])) )
{
fcntl64(0LL, 2LL, 0LL);
execve(args_1, &args_1, environ);
}
exit(*v5);
}
if ( v3 == -1 )
{
v6 = *v4;
func0(fds[0]);
}
else
{
func0(fds[0]);
v6 = func2(fd2, a2, fds[1]);
while ( waitpid(v3, 0LL, 0) != v3 && *v5 == 4 )
;
}
func0(fds[1]);
*v5 = v6;
return v6 == 0;
}

original_code:
__attribute__((__nonnull__ ))
static _Bool
bsock_bind_viafork (const int fd, const struct addrinfo * const restrict ai)
{
    int sv[2];
    int errnum;
    pid_t pid;
    struct stat st;
    if (0 != stat("/usr/local/sbin/bsock", &st))
        return 0;
    if (!(st.st_mode & 04000))
        return ((*__errno_location ()) = 1, 0);
    if (0 != socketpair(1, SOCK_STREAM, 0, sv))
        return 0;
    pid = fork();
    if (0 == pid) {
        static char bsock_exe[] = "/usr/local/sbin/bsock";
        static char *args[] = { bsock_exe, ((void *)0) };
        if ( dup2(sv[0], 0) != 0
            || (sv[0] != 0 && 0 != close(sv[0]))
            || (sv[1] != 0 && 0 != close(sv[1])))
            _exit((*__errno_location ()));
        (void)fcntl(0, 2, 0);
        execve(args[0], args, environ);
        _exit((*__errno_location ()));
    }
    else if (-1 != pid) {
        nointr_close(sv[0]);
        errnum = bsock_bind_send_addr_and_recv(fd, ai, sv[1]);
        do {} while (__builtin_expect((pid != waitpid(pid,((void *)0),0)),0) && __builtin_expect((*__errno_location ()) == 4, 1));
    }
    else {
        errnum = (*__errno_location ());
        nointr_close(sv[0]);
    }
    nointr_close(sv[1]);
    (*__errno_location ()) = errnum;
    return (0 == errnum);
}

canonical_original_code:
static _Bool bsock_bind_viafork(int fd, struct addrinfo *ai)
{
    int sv[2];
    int errnum;
    int pid;
    struct stat st;
    if (0 != stat("/usr/local/sbin/bsock", &st))
        return 0;
    if (!(st.st_mode & 04000))
        return ((*__errno_location ()) = 1, 0);
    if (0 != socketpair(1, SOCK_STREAM, 0, sv))
        return 0;
    pid = fork();
    if (0 == pid) {
        static char bsock_exe[] = "/usr/local/sbin/bsock";
        static char *args[] = { bsock_exe, ((void *)0) };
        if ( dup2(sv[0], 0) != 0
            || (sv[0] != 0 && 0 != close(sv[0]))
            || (sv[1] != 0 && 0 != close(sv[1])))
            _exit((*__errno_location ()));
        (void)fcntl(0, 2, 0);
        execve(args[0], args, environ);
        _exit((*__errno_location ()));
    }
    else if (-1 != pid) {
        nointr_close(sv[0]);
        errnum = bsock_bind_send_addr_and_recv(fd, ai, sv[1]);
        do {} while (__builtin_expect((pid != waitpid(pid,((void *)0),0)),0) && __builtin_expect((*__errno_location ()) == 4, 1));
    }
    else {
        errnum = (*__errno_location ());
        nointr_close(sv[0]);
    }
    nointr_close(sv[1]);
    (*__errno_location ()) = errnum;
    return (0 == errnum);
}

variable_types:
sv
int [2]

variable_types: pid int

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:
args
char *[]

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


decompiled_code:
__int64 __fastcall bsock_bind_addrinfo(int fd2, __int64 a2)
{
__int64 result; // rax
int *v3; // rax
int v4; // ecx
int *v5; // rdx

if ( bsock_bind_viasock(fd2, a2) || bsock_bind_viafork(fd2, a2) )
return 0LL;
v3 = _errno_location();
v4 = *v3;
v5 = v3;
if ( *v3 > 22 )
{
result = 0xFFFFFFFFLL;
if ( v4 != 88 && v4 != 98 )
goto LABEL_8;
}
else if ( v4 <= 8 || (result = 0xFFFFFFFFLL, ((1LL << v4) & 0x402200) == 0) )
{
LABEL_8:
*v5 = 13;
return 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(int fd2, __int64 a2)
{
__int64 result; // rax
int *v3; // rax
int v4; // ecx
int *v5; // rdx

if ( func3(fd2, a2) || func4(fd2, a2) )
return 0LL;
v3 = _errno_location();
v4 = *v3;
v5 = v3;
if ( *v3 > 22 )
{
result = 0xFFFFFFFFLL;
if ( v4 != 88 && v4 != 98 )
goto LABEL_8;
}
else if ( v4 <= 8 || (result = 0xFFFFFFFFLL, ((1LL << v4) & 0x402200) == 0) )
{
LABEL_8:
*v5 = 13;
return 0xFFFFFFFFLL;
}
return result;
}

original_code:
__attribute__((__nonnull__ ))
int
bsock_bind_addrinfo (const int fd, const struct addrinfo * const restrict ai)
{
    if (bsock_bind_viasock(fd, ai) || bsock_bind_viafork(fd, ai))
        return 0;
    switch ((*__errno_location ())) {
      default: (*__errno_location ()) = 13;
      case 13: case 98: case 9: case 22: case 88:
               return -1;
    }
}

canonical_original_code:
int bsock_bind_addrinfo(int fd, struct addrinfo *ai)
{
    if (bsock_bind_viasock(fd, ai) || bsock_bind_viafork(fd, ai))
        return 0;
    switch ((*__errno_location ())) {
      default: (*__errno_location ()) = 13;
      case 13: case 98: case 9: case 22: case 88:
               return -1;
    }
}

return_type: int


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

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

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

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


decompiled_code:
void __fastcall parse_args(int argc, char **argv, int *a3, int *a4, int *a5)
{
char v8; // al
int v9; // eax
__int64 v10; // rdx
int v11; // eax
int v12; // eax
int v13; // eax
struct rlimit v14; // [rsp+0h] [rbp-38h] BYREF

*a3 = 100;
*a4 = 1;
*a5 = 0;
while ( 1 )
{
v8 = getopt(argc, argv, "n:a:w:");
if ( v8 == -1 )
break;
switch ( v8 )
{
case 'n':
v11 = atoi(optarg);
*a3 = v11;
if ( v11 <= 0 )
{
v10 = 110LL;
goto LABEL_13;
}
break;
case 'w':
v9 = atoi(optarg);
*a5 = v9;
if ( v9 < 0 )
{
v10 = 119LL;
goto LABEL_13;
}
break;
case 'a':
v12 = atoi(optarg);
*a4 = v12;
if ( v12 <= 0 )
{
v10 = 97LL;
LABEL_13:
fprintf(stderr, "Invalid argument -%c \"%s\"\n", v10, optarg);
exit(1);
}
break;
default:
fprintf(stderr, "Invalid argument \"%c\"\n", (unsigned int)v8);
exit(1);
}
}
v13 = *a3;
if ( *a4 > *a3 )
*a4 = v13;
if ( v13 < *a5 )
*a5 = v13;
v14.rlim_max = 2 * v13 + 50;
v14.rlim_cur = v14.rlim_max;
if ( setrlimit(RLIMIT_NOFILE, &v14) == -1 )
perror("setrlimit");
}

canonical_decompiled_code:
void __fastcall func0(int argc, char **argv, int *a3, int *a4, int *a5)
{
char v8; // al
int v9; // eax
__int64 v10; // rdx
int v11; // eax
int v12; // eax
int v13; // eax
struct rlimit v14; // [rsp+0h] [rbp-38h] BYREF

*a3 = 100;
*a4 = 1;
*a5 = 0;
while ( 1 )
{
v8 = getopt(argc, argv, "n:a:w:");
if ( v8 == -1 )
break;
switch ( v8 )
{
case 'n':
v11 = atoi(optarg);
*a3 = v11;
if ( v11 <= 0 )
{
v10 = 110LL;
goto LABEL_13;
}
break;
case 'w':
v9 = atoi(optarg);
*a5 = v9;
if ( v9 < 0 )
{
v10 = 119LL;
goto LABEL_13;
}
break;
case 'a':
v12 = atoi(optarg);
*a4 = v12;
if ( v12 <= 0 )
{
v10 = 97LL;
LABEL_13:
fprintf(stderr, "Invalid argument -%c \"%s\"\n", v10, optarg);
exit(1);
}
break;
default:
fprintf(stderr, "Invalid argument \"%c\"\n", (unsigned int)v8);
exit(1);
}
}
v13 = *a3;
if ( *a4 > *a3 )
*a4 = v13;
if ( v13 < *a5 )
*a5 = v13;
v14.rlim_max = 2 * v13 + 50;
v14.rlim_cur = v14.rlim_max;
if ( setrlimit(RLIMIT_NOFILE, &v14) == -1 )
perror("setrlimit");
}

original_code:
static void __attribute__((noinline))
parse_args(const int argc, char ** const restrict argv,
           int * const restrict num_fdpairs,
           int * const restrict num_active,
           int * const restrict num_writes)
{
    struct rlimit rl;
    char c;
    *num_fdpairs = 100;
    *num_active = 1;
    *num_writes = 0;
    while ((c = getopt(argc, argv, "n:a:w:")) != -1) {
        switch (c) {
          case 'n':
            *num_fdpairs = atoi(optarg); if (*num_fdpairs > 0) continue; break;
          case 'a':
            *num_active = atoi(optarg); if (*num_active > 0) continue; break;
          case 'w':
            *num_writes = atoi(optarg); if (*num_writes >= 0) continue; break;
          default:
            fprintf(stderr, "Invalid argument \"%c\"\n", c); exit(1);
        }
        fprintf(stderr, "Invalid argument -%c \"%s\"\n", c, optarg);
        exit(1);
    }
    if (*num_active > *num_fdpairs)
        *num_active = *num_fdpairs;
    if (*num_writes > *num_fdpairs)
        *num_writes = *num_fdpairs;
    rl.rlim_cur = rl.rlim_max = *num_fdpairs * 2 + 50;
    if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
        perror("setrlimit");
}

canonical_original_code:
static void parse_args(int argc, char **argv, int *num_fdpairs, int *num_active, int *num_writes)
{
    struct rlimit rl;
    char c;
    *num_fdpairs = 100;
    *num_active = 1;
    *num_writes = 0;
    while ((c = getopt(argc, argv, "n:a:w:")) != -1) {
        switch (c) {
          case 'n':
            *num_fdpairs = atoi(optarg); if (*num_fdpairs > 0) continue; break;
          case 'a':
            *num_active = atoi(optarg); if (*num_active > 0) continue; break;
          case 'w':
            *num_writes = atoi(optarg); if (*num_writes >= 0) continue; break;
          default:
            fprintf(stderr, "Invalid argument \"%c\"\n", c); exit(1);
        }
        fprintf(stderr, "Invalid argument -%c \"%s\"\n", c, optarg);
        exit(1);
    }
    if (*num_active > *num_fdpairs)
        *num_active = *num_fdpairs;
    if (*num_writes > *num_fdpairs)
        *num_writes = *num_fdpairs;
    rl.rlim_cur = rl.rlim_max = *num_fdpairs * 2 + 50;
    if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
        perror("setrlimit");
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct rlimit { unsigned long rlim_cur; unsigned long rlim_max; }

variable_types: c char

return_type: void


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
unsigned int v4; // er12
__int64 v5; // rbp
int *v6; // rax
int *v7; // r15
int v8; // eax
int i; // ebx
_BOOL8 v10; // rax
int v11; // ebx
__int64 v12; // rax
__int64 v13; // rsi
__int64 v14; // rdx
__int64 v15; // r12
int v16; // eax
int *v17; // r14
int j; // ebx
int v19; // edi
int **v20; // r14
__int64 v21; // rbx
int *v22; // rdi
int v24; // [rsp+Ch] [rbp-4Ch]
int v25; // [rsp+14h] [rbp-44h] BYREF
int v26; // [rsp+18h] [rbp-40h] BYREF
int v27[15]; // [rsp+1Ch] [rbp-3Ch] BYREF

parse_args(argc, (char **)argv, &v25, &v26, v27);
v3 = bpoll_create(0LL, 0LL, 0LL, 0LL, 0LL);
v4 = v25;
v5 = v3;
v6 = (int *)malloc(8LL * v25);
if ( v5 && (v7 = v6) != 0LL )
{
if ( (unsigned int)bpoll_init(v5, 0LL, v4, 512LL, 0LL) )
{
perror("bpoll_init");
LODWORD(v10) = 1;
}
else
{
*(_DWORD *)(v5 + 108) = 0;
*(_QWORD *)(v5 + 112) = 0LL;
*(_QWORD *)(v5 + 120) = 0LL;
bpoll_timespec_set(v5, v5 + 112);
v8 = v25;
if ( v25 <= 0 )
goto LABEL_19;
for ( i = 0; i < v25; ++i )
{
if ( socketpair(1, 1, 0, &v7[2 * i]) == -1 )
{
perror("socketpair");
LODWORD(v10) = 1;
return v10;
}
v8 = v25;
}
if ( v25 <= 0 )
{
LABEL_19:
v14 = (unsigned int)(v8 >> 31);
v24 = 200;
LODWORD(v14) = v8 % v26;
v15 = 8LL * (v8 / v26);
do
{
v16 = v26;
v17 = v7 + 1;
for ( j = 0; v26 > j; v16 = v26 )
{
v19 = *v17;
++j;
v17 = (int *)((char *)v17 + v15);
write(v19, "e", 1uLL);
}
fired = v16;
count = 0;
writes = v27[0];
do
{
bpoll_poll(v5, v5 + 112, v14);
v14 = *(unsigned int *)(v5 + 16);
if ( (int)v14 > 0 )
{
v20 = *(int ***)(v5 + 40);
v21 = (__int64)&v20[(unsigned int)(v14 - 1) + 1];
do
{
v22 = *v20++;
read_cb_constprop_0(v22);
}
while ( (int **)v21 != v20 );
}
}
while ( count != fired );
--v24;
}
while ( v24 );
bpoll_destroy(v5);
free(v7);
if ( errors )
fprintf(stderr, "errors: %8d\n", (unsigned int)errors);
v10 = errors != 0;
}
else
{
v11 = 0;
while ( 1 )
{
v13 = bpoll_elt_init(v5, 0LL, (unsigned int)v7[2 * v11], 1LL, 0LL);
if ( !v13 )
{
perror("bpoll_elt_init");
LODWORD(v10) = 1;
return v10;
}
v12 = v11 ? 4LL * (2 * v11 - 2) + 4 : 4LL * (2 * v25 - 2) + 4;
*(_QWORD *)(v13 + 24) = *(int *)((char *)v7 + v12);
if ( (unsigned int)bpoll_elt_add(v5, v13, 1LL) )
break;
v8 = v25;
if ( v25 <= ++v11 )
goto LABEL_19;
}
perror("bpoll_elt_add");
LODWORD(v10) = 1;
}
}
}
else
{
perror("malloc");
LODWORD(v10) = 1;
}
return v10;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rax
unsigned int v4; // er12
__int64 v5; // rbp
int *v6; // rax
int *v7; // r15
int v8; // eax
int i; // ebx
_BOOL8 v10; // rax
int v11; // ebx
__int64 v12; // rax
__int64 v13; // rsi
__int64 v14; // rdx
__int64 v15; // r12
int v16; // eax
int *v17; // r14
int j; // ebx
int v19; // edi
int **v20; // r14
__int64 v21; // rbx
int *v22; // rdi
int v24; // [rsp+Ch] [rbp-4Ch]
int v25; // [rsp+14h] [rbp-44h] BYREF
int v26; // [rsp+18h] [rbp-40h] BYREF
int v27[15]; // [rsp+1Ch] [rbp-3Ch] BYREF

func0(argc, (char **)argv, &v25, &v26, v27);
v3 = bpoll_create(0LL, 0LL, 0LL, 0LL, 0LL);
v4 = v25;
v5 = v3;
v6 = (int *)malloc(8LL * v25);
if ( v5 && (v7 = v6) != 0LL )
{
if ( (unsigned int)bpoll_init(v5, 0LL, v4, 512LL, 0LL) )
{
perror("bpoll_init");
LODWORD(v10) = 1;
}
else
{
*(_DWORD *)(v5 + 108) = 0;
*(_QWORD *)(v5 + 112) = 0LL;
*(_QWORD *)(v5 + 120) = 0LL;
bpoll_timespec_set(v5, v5 + 112);
v8 = v25;
if ( v25 <= 0 )
goto LABEL_19;
for ( i = 0; i < v25; ++i )
{
if ( socketpair(1, 1, 0, &v7[2 * i]) == -1 )
{
perror("socketpair");
LODWORD(v10) = 1;
return v10;
}
v8 = v25;
}
if ( v25 <= 0 )
{
LABEL_19:
v14 = (unsigned int)(v8 >> 31);
v24 = 200;
LODWORD(v14) = v8 % v26;
v15 = 8LL * (v8 / v26);
do
{
v16 = v26;
v17 = v7 + 1;
for ( j = 0; v26 > j; v16 = v26 )
{
v19 = *v17;
++j;
v17 = (int *)((char *)v17 + v15);
write(v19, "e", 1uLL);
}
fired = v16;
count = 0;
writes = v27[0];
do
{
bpoll_poll(v5, v5 + 112, v14);
v14 = *(unsigned int *)(v5 + 16);
if ( (int)v14 > 0 )
{
v20 = *(int ***)(v5 + 40);
v21 = (__int64)&v20[(unsigned int)(v14 - 1) + 1];
do
{
v22 = *v20++;
func1(v22);
}
while ( (int **)v21 != v20 );
}
}
while ( count != fired );
--v24;
}
while ( v24 );
bpoll_destroy(v5);
free(v7);
if ( errors )
fprintf(stderr, "errors: %8d\n", (unsigned int)errors);
v10 = errors != 0;
}
else
{
v11 = 0;
while ( 1 )
{
v13 = bpoll_elt_init(v5, 0LL, (unsigned int)v7[2 * v11], 1LL, 0LL);
if ( !v13 )
{
perror("bpoll_elt_init");
LODWORD(v10) = 1;
return v10;
}
v12 = v11 ? 4LL * (2 * v11 - 2) + 4 : 4LL * (2 * v25 - 2) + 4;
*(_QWORD *)(v13 + 24) = *(int *)((char *)v7 + v12);
if ( (unsigned int)bpoll_elt_add(v5, v13, 1LL) )
break;
v8 = v25;
if ( v25 <= ++v11 )
goto LABEL_19;
}
perror("bpoll_elt_add");
LODWORD(v10) = 1;
}
}
}
else
{
perror("malloc");
LODWORD(v10) = 1;
}
return v10;
}

original_code:
int
main (const int argc, char ** const argv)
{
    int i, j, num_fdpairs, num_active, num_writes;
    parse_args(argc, argv, &num_fdpairs, &num_active, &num_writes);
    struct bpollelt_t *bpelt;
    struct bpollelt_t **results;
    struct bpollset_t * const restrict bpset =
      bpoll_create(((void *)0), fn_cb_event, ((void *)0), ((void *)0), ((void *)0));
    int * const restrict fdpairs = (int *)
      malloc(num_fdpairs * 2 * sizeof(int));
    if (bpset == ((void *)0) || fdpairs == ((void *)0))
        return perror("malloc"), 1;
    if (bpoll_init(bpset, BPOLL_M_NOT_SET, num_fdpairs, 512, 0) != 0)
        return perror("bpoll_init"), 1;
    ((bpset)->timeout = (0), (bpset)->ts.tv_sec = (time_t)((0) / 1000), (bpset)->ts.tv_nsec = ((0) % 1000) * 1000000, bpoll_timespec_set((bpset), &(bpset)->ts));
    for (i = 0; i < num_fdpairs; ++i) {
        if (socketpair(1, SOCK_STREAM, 0, fdpairs+(i<<1)) == -1)
            return perror("socketpair"), 1;
    }
    ;
    for (i = 0; i < num_fdpairs; ++i) {
        bpelt = bpoll_elt_init(bpset, ((void *)0), fdpairs[i<<1],
                               BPOLL_FD_SOCKET,
                               BPOLL_FL_ZERO);
        if (bpelt == ((void *)0))
            return perror("bpoll_elt_init"), 1;
        bpelt->udata = (void *)(uintptr_t)
          fdpairs[ (i != 0 ? i-1 : num_fdpairs-1) * 2 + 1 ];
        if (bpoll_elt_add(bpset, bpelt, 0x001) != 0)
            return perror("bpoll_elt_add"), 1;
    }
    ;
    const int space = (num_fdpairs / num_active) * 2;
    ssize_t w;
    for (int k = 0; k < 200; ++k) {
        for (i = 0; i < num_active; ++i)
            w = write(fdpairs[i * space + 1], "e", 1);
        fired = num_active;
        count = 0 & w;
        writes = num_writes;
        do {
            bpoll_poll(bpset, (&(bpset)->ts));
            if (!fn_cb_event) {
                results = ((bpset)->results);
                for (i = 0, j = ((bpset)->nfound); i < j; ++i)
                    read_cb(bpset, results[i], -1);
            }
        } while (count != fired);
    }
    bpoll_destroy(bpset);
    ;
    free(fdpairs);
    if (0 != errors)
        fprintf(stderr, "errors: %8d\n", errors);
    return (0 != errors);
}

canonical_original_code:
int main(int argc, char **argv)
{
    int i; int j; int num_fdpairs; int num_active; int num_writes;
    parse_args(argc, argv, &num_fdpairs, &num_active, &num_writes);
    struct bpollelt_t *bpelt;
    struct bpollelt_t **results;
    struct bpollset_t *bpset = bpoll_create(((void *)0), fn_cb_event, ((void *)0), ((void *)0), ((void *)0));
    int *fdpairs = (int *)
      malloc(num_fdpairs * 2 * sizeof(int));
    if (bpset == ((void *)0) || fdpairs == ((void *)0))
        return perror("malloc"), 1;
    if (bpoll_init(bpset, BPOLL_M_NOT_SET, num_fdpairs, 512, 0) != 0)
        return perror("bpoll_init"), 1;
    ((bpset)->timeout = (0), (bpset)->ts.tv_sec = (time_t)((0) / 1000), (bpset)->ts.tv_nsec = ((0) % 1000) * 1000000, bpoll_timespec_set((bpset), &(bpset)->ts));
    for (i = 0; i < num_fdpairs; ++i) {
        if (socketpair(1, SOCK_STREAM, 0, fdpairs+(i<<1)) == -1)
            return perror("socketpair"), 1;
    }
    ;
    for (i = 0; i < num_fdpairs; ++i) {
        bpelt = bpoll_elt_init(bpset, ((void *)0), fdpairs[i<<1],
                               BPOLL_FD_SOCKET,
                               BPOLL_FL_ZERO);
        if (bpelt == ((void *)0))
            return perror("bpoll_elt_init"), 1;
        bpelt->udata = (void *)(unsigned long)
          fdpairs[ (i != 0 ? i-1 : num_fdpairs-1) * 2 + 1 ];
        if (bpoll_elt_add(bpset, bpelt, 0x001) != 0)
            return perror("bpoll_elt_add"), 1;
    }
    ;
    int space = (num_fdpairs / num_active) * 2;
    long w;
    for (int k = 0; k < 200; ++k) {
        for (i = 0; i < num_active; ++i)
            w = write(fdpairs[i * space + 1], "e", 1);
        fired = num_active;
        count = 0 & w;
        writes = num_writes;
        do {
            bpoll_poll(bpset, (&(bpset)->ts));
            if (!fn_cb_event) {
                results = ((bpset)->results);
                for (i = 0, j = ((bpset)->nfound); i < j; ++i)
                    read_cb(bpset, results[i], -1);
            }
        } while (count != fired);
    }
    bpoll_destroy(bpset);
    ;
    free(fdpairs);
    if (0 != errors)
        fprintf(stderr, "errors: %8d\n", errors);
    return (0 != errors);
}

variable_types: i int

variable_types: j int

variable_types: struct bpollset_t { unsigned int mech; unsigned int idx; unsigned int clr; unsigned int limit; int nfound; unsigned int queue_sz; unsigned int results_sz; unsigned int bpollelts_sz; struct bpollelt_t { int fd; int events; int revents; unsigned int idx; unsigned int fdtype; unsigned int flags; unsigned int flpriv; void *udata; } **bpollelts; struct bpollelt_t **results; struct bpollelt_t **rmlist; int rmsz; int rmidx; struct pollfd { int fd; short events; short revents; } *pollfds; struct pollfd *pfd_ready; struct epoll_event { unsigned int events; union epoll_data { void *ptr; int fd; unsigned int u32; unsigned long u64; } data; } *epoll_events; struct epoll_event *epoll_ready; struct __sigset_t { unsigned long __val[-1]; } *sigmaskp; int fd; int timeout; struct timespec { long tv_sec; long tv_nsec; } ts; void (*fn_cb_event)(struct bpollset_t *, struct bpollelt_t *, int data); void (*fn_cb_close)(struct bpollset_t *, struct bpollelt_t *); void *(*fn_mem_alloc)(void *, unsigned long); void (*fn_mem_free)(void *, void *); void *vdata; unsigned long mem_chunk_sz; struct bpoll_mem_block { struct bpollelt_t b; char data[]; } *mem_chunk_head; struct bpoll_mem_block *mem_chunk_tail; struct bpoll_mem_block *mem_block_head; unsigned int mem_block_sz; int mem_block_freed; void *bpollelts_used[16]; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } mutex; int nelts; } *

variable_types:
fdpairs
int *

variable_types: space int

variable_types: w long

variable_types: k int

user_defined_types: struct bpollset_t { unsigned int mech; unsigned int idx; unsigned int clr; unsigned int limit; int nfound; unsigned int queue_sz; unsigned int results_sz; unsigned int bpollelts_sz; struct bpollelt_t **bpollelts; struct bpollelt_t **results; struct bpollelt_t **rmlist; int rmsz; int rmidx; struct pollfd *pollfds; struct pollfd *pfd_ready; struct epoll_event *epoll_events; struct epoll_event *epoll_ready; struct __sigset_t *sigmaskp; int fd; int timeout; struct timespec ts; void (*fn_cb_event)(struct bpollset_t *, struct bpollelt_t *, int data); void (*fn_cb_close)(struct bpollset_t *, struct bpollelt_t *); void *(*fn_mem_alloc)(void *, unsigned long); void (*fn_mem_free)(void *, void *); void *vdata; unsigned long mem_chunk_sz; struct bpoll_mem_block *mem_chunk_head; struct bpoll_mem_block *mem_chunk_tail; struct bpoll_mem_block *mem_block_head; unsigned int mem_block_sz; int mem_block_freed; void *bpollelts_used[16]; union pthread_mutex_t mutex; int nelts; }

user_defined_types: union pthread_mutex_t { struct __pthread_mutex_s __data; char __size[40]; long __align; }

user_defined_types: struct epoll_event { unsigned int events; union epoll_data data; }

user_defined_types: struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list __list; }


decompiled_code:
__int64 __fastcall bsock_authz_valid(__int64 a1, unsigned int a2, unsigned int a3)
{
unsigned int v3; // er12
__int64 i; // rbx
unsigned int v5; // eax

LOBYTE(a3) = a3 == 0 || a2 == 0;
v3 = a3;
if ( (_BYTE)a3 )
return v3;
for ( i = *(_QWORD *)(*((_QWORD *)bsock_authz_hash + 1) + 8LL * (*(_DWORD *)bsock_authz_hash & a2));
i;
i = *(_QWORD *)(i + 40) )
{
if ( *(_DWORD *)i == a2 && *(_DWORD *)(i + 4) == *(_DWORD *)(a1 + 4) && *(_QWORD *)(i + 8) == *(_QWORD *)(a1 + 8) )
{
v5 = *(_DWORD *)(i + 16);
if ( v5 == *(_DWORD *)(a1 + 16) && !memcmp(*(const void **)(i + 24), *(const void **)(a1 + 24), v5) )
return 1;
}
}
*_errno_location() = 13;
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, unsigned int a2, unsigned int a3)
{
unsigned int v3; // er12
__int64 i; // rbx
unsigned int v5; // eax

LOBYTE(a3) = a3 == 0 || a2 == 0;
v3 = a3;
if ( (_BYTE)a3 )
return v3;
for ( i = *(_QWORD *)(*((_QWORD *)bsock_authz_hash + 1) + 8LL * (*(_DWORD *)bsock_authz_hash & a2));
i;
i = *(_QWORD *)(i + 40) )
{
if ( *(_DWORD *)i == a2 && *(_DWORD *)(i + 4) == *(_DWORD *)(a1 + 4) && *(_QWORD *)(i + 8) == *(_QWORD *)(a1 + 8) )
{
v5 = *(_DWORD *)(i + 16);
if ( v5 == *(_DWORD *)(a1 + 16) && !memcmp(*(const void **)(i + 24), *(const void **)(a1 + 24), v5) )
return 1;
}
}
*_errno_location() = 13;
return v3;
}

original_code:
__attribute__((__nonnull__ ))
static _Bool
bsock_authz_valid (const struct addrinfo * const restrict ai,
                   const uid_t uid, const gid_t gid)
{
    const struct addrinfo * restrict h;
    if (uid == 0 || gid == 0)
        return 1;
    h = bsock_authz_hash->table[(uid & bsock_authz_hash->mask)];
    for (; h != ((void *)0); h = h->ai_next) {
        if ( h->ai_flags == (int)uid
            && h->ai_family == ai->ai_family
            && h->ai_socktype == ai->ai_socktype
            && h->ai_protocol == ai->ai_protocol
            && h->ai_addrlen == ai->ai_addrlen
            && 0 == memcmp(h->ai_addr, ai->ai_addr, h->ai_addrlen) )
            return 1;
    }
    return (((*__errno_location ()) = 13), 0);
}

canonical_original_code:
static _Bool bsock_authz_valid(struct addrinfo *ai, unsigned int uid, unsigned int gid)
{
    struct addrinfo *h;
    if (uid == 0 || gid == 0)
        return 1;
    h = bsock_authz_hash->table[(uid & bsock_authz_hash->mask)];
    for (; h != ((void *)0); h = h->ai_next) {
        if ( h->ai_flags == (int)uid
            && h->ai_family == ai->ai_family
            && h->ai_socktype == ai->ai_socktype
            && h->ai_protocol == ai->ai_protocol
            && h->ai_addrlen == ai->ai_addrlen
            && 0 == memcmp(h->ai_addr, ai->ai_addr, h->ai_addrlen) )
            return 1;
    }
    return (((*__errno_location ()) = 13), 0);
}

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

variable_types:
uid
unsigned int

variable_types:
gid
unsigned int


decompiled_code:
_BOOL8 __fastcall bsock_authz_validate(__int64 a1, unsigned int a2, unsigned int a3)
{
char v4; // r8
_BOOL8 result; // rax
__int64 v6; // r13
int v7; // eax
char v8; // al
__int128 v9; // [rsp+0h] [rbp-48h] BYREF
unsigned __int64 v10; // [rsp+18h] [rbp-30h]

v10 = __readfsqword(0x28u);
v4 = bsock_authz_valid(a1, a2, a3);
result = 1LL;
if ( !v4 )
{
result = 0LL;
if ( *(_DWORD *)(a1 + 4) == 2 )
{
v6 = *(_QWORD *)(a1 + 24);
v9 = 0LL;
LOWORD(v9) = 2;
WORD1(v9) = *(_WORD *)(v6 + 2);
v7 = *(_DWORD *)(v6 + 4);
*(_QWORD *)(a1 + 24) = &v9;
DWORD1(v9) = v7;
v8 = bsock_authz_valid(a1, a2, a3);
*(_QWORD *)(a1 + 24) = v6;
result = v8 != 0;
}
}
return result;
}

canonical_decompiled_code:
_BOOL8 __fastcall func1(__int64 a1, unsigned int a2, unsigned int a3)
{
char v4; // r8
_BOOL8 result; // rax
__int64 v6; // r13
int v7; // eax
char v8; // al
__int128 v9; // [rsp+0h] [rbp-48h] BYREF
unsigned __int64 v10; // [rsp+18h] [rbp-30h]

v10 = __readfsqword(0x28u);
v4 = func0(a1, a2, a3);
result = 1LL;
if ( !v4 )
{
result = 0LL;
if ( *(_DWORD *)(a1 + 4) == 2 )
{
v6 = *(_QWORD *)(a1 + 24);
v9 = 0LL;
LOWORD(v9) = 2;
WORD1(v9) = *(_WORD *)(v6 + 2);
v7 = *(_DWORD *)(v6 + 4);
*(_QWORD *)(a1 + 24) = &v9;
DWORD1(v9) = v7;
v8 = func0(a1, a2, a3);
*(_QWORD *)(a1 + 24) = v6;
result = v8 != 0;
}
}
return result;
}

original_code:
__attribute__((__nonnull__ ))
int
bsock_authz_validate (struct addrinfo * const restrict ai,
                      const uid_t uid, const gid_t gid)
{
    if (bsock_authz_valid(ai, uid, gid))
        return 1;
    if (ai->ai_family == 2) {
        struct sockaddr * const restrict sockaddr_orig = ai->ai_addr;
        struct sockaddr_in sin;
        memset(&sin, '\0', sizeof(struct sockaddr_in));
        sin.sin_family = 2;
        sin.sin_port =((struct sockaddr_in *)ai->ai_addr)->sin_port;
        sin.sin_addr.s_addr=((struct sockaddr_in*)ai->ai_addr)->sin_addr.s_addr;
        ai->ai_addr = (struct sockaddr *)&sin;
        const _Bool rc = bsock_authz_valid(ai, uid, gid);
        ai->ai_addr = sockaddr_orig;
        if (rc)
            return 1;
    }
    return 0;
}

canonical_original_code:
int bsock_authz_validate(struct addrinfo *ai, unsigned int uid, unsigned int gid)
{
    if (bsock_authz_valid(ai, uid, gid))
        return 1;
    if (ai->ai_family == 2) {
        struct sockaddr *sockaddr_orig = ai->ai_addr;
        struct sockaddr_in sin;
        memset(&sin, '\0', sizeof(struct sockaddr_in));
        sin.sin_family = 2;
        sin.sin_port =((struct sockaddr_in *)ai->ai_addr)->sin_port;
        sin.sin_addr.s_addr=((struct sockaddr_in *)ai->ai_addr)->sin_addr.s_addr;
        ai->ai_addr = (struct sockaddr *)&sin;
        _Bool rc = bsock_authz_valid(ai, uid, gid);
        ai->ai_addr = sockaddr_orig;
        if (rc)
            return 1;
    }
    return 0;
}

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

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:
__int64 __fastcall bsock_authz_config_parse(__int64 a1, const char *a2)
{
unsigned int v2; // er14
__int64 v4; // rbx
__int64 v5; // r15
char *v6; // rax
char *v7; // rbp
char v9; // al
__int64 v10; // rdx
__uid_t v11; // eax
_QWORD *v12; // rax
char *v13; // [rsp+8h] [rbp-8B0h]
struct passwd *result; // [rsp+18h] [rbp-8A0h] BYREF
char v15[40]; // [rsp+20h] [rbp-898h] BYREF
struct passwd resultbuf; // [rsp+48h] [rbp-870h] BYREF
char buffer[2048]; // [rsp+78h] [rbp-840h] BYREF
unsigned __int64 v18; // [rsp+878h] [rbp-40h]

v2 = 1;
v4 = *(_QWORD *)(a1 + 16);
v5 = *(_QWORD *)(a1 + 24);
v18 = __readfsqword(0x28u);
result = 0LL;
setpwent();
while ( 1 )
{
v13 = strchr(a2, 10);
if ( !v13 )
break;
if ( *a2 != 10 && *a2 != 35 )
{
*v13 = 0;
v6 = strchr(a2, 32);
v7 = v6;
if ( !v6 || (*v6 = 0, getpwnam_r(a2, &resultbuf, buffer, 0x800uLL, &result)) || !result )
{
bsock_syslog(22LL, 3LL, "invalid line (%d) in config (%s)", v2, "/usr/local/etc/bsock");
return 22LL;
}
do
++v7;
while ( *v7 == 32 );
*(_QWORD *)(v4 + 24) = v5;
*(_DWORD *)(v4 + 16) = 128;
v9 = bsock_addrinfo_split_str(v15, v7);
if ( !v9 || !(unsigned __int8)bsock_addrinfo_from_strs(v4, v15) )
{
bsock_syslog(22LL, 3LL, "invalid line (%d) in config (%s)", v2, "/usr/local/etc/bsock");
return (unsigned int)*_errno_location();
}
v10 = *(_QWORD *)(a1 + 8);
v5 += 4LL * ((*(_DWORD *)(v4 + 16) + 7) & 0xFFFFFFF8);
v11 = resultbuf.pw_uid;
*(_DWORD *)v4 = resultbuf.pw_uid;
v12 = (_QWORD *)(v10 + 8LL * (*(_DWORD *)a1 & v11));
*(_QWORD *)(v4 + 40) = *v12;
*v12 = v4;
v4 += 48LL;
}
++v2;
a2 = v13 + 1;
}
endpwent();
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, const char *a2)
{
unsigned int v2; // er14
__int64 v4; // rbx
__int64 v5; // r15
char *v6; // rax
char *v7; // rbp
char v9; // al
__int64 v10; // rdx
__uid_t v11; // eax
_QWORD *v12; // rax
char *v13; // [rsp+8h] [rbp-8B0h]
struct passwd *result; // [rsp+18h] [rbp-8A0h] BYREF
char v15[40]; // [rsp+20h] [rbp-898h] BYREF
struct passwd resultbuf; // [rsp+48h] [rbp-870h] BYREF
char buffer[2048]; // [rsp+78h] [rbp-840h] BYREF
unsigned __int64 v18; // [rsp+878h] [rbp-40h]

v2 = 1;
v4 = *(_QWORD *)(a1 + 16);
v5 = *(_QWORD *)(a1 + 24);
v18 = __readfsqword(0x28u);
result = 0LL;
setpwent();
while ( 1 )
{
v13 = strchr(a2, 10);
if ( !v13 )
break;
if ( *a2 != 10 && *a2 != 35 )
{
*v13 = 0;
v6 = strchr(a2, 32);
v7 = v6;
if ( !v6 || (*v6 = 0, getpwnam_r(a2, &resultbuf, buffer, 0x800uLL, &result)) || !result )
{
bsock_syslog(22LL, 3LL, "invalid line (%d) in config (%s)", v2, "/usr/local/etc/bsock");
return 22LL;
}
do
++v7;
while ( *v7 == 32 );
*(_QWORD *)(v4 + 24) = v5;
*(_DWORD *)(v4 + 16) = 128;
v9 = bsock_addrinfo_split_str(v15, v7);
if ( !v9 || !(unsigned __int8)bsock_addrinfo_from_strs(v4, v15) )
{
bsock_syslog(22LL, 3LL, "invalid line (%d) in config (%s)", v2, "/usr/local/etc/bsock");
return (unsigned int)*_errno_location();
}
v10 = *(_QWORD *)(a1 + 8);
v5 += 4LL * ((*(_DWORD *)(v4 + 16) + 7) & 0xFFFFFFF8);
v11 = resultbuf.pw_uid;
*(_DWORD *)v4 = resultbuf.pw_uid;
v12 = (_QWORD *)(v10 + 8LL * (*(_DWORD *)a1 & v11));
*(_QWORD *)(v4 + 40) = *v12;
*v12 = v4;
v4 += 48LL;
}
++v2;
a2 = v13 + 1;
}
endpwent();
return 0LL;
}

original_code:
__attribute__((__cold__))
__attribute__ ((__noinline__))
static int
bsock_authz_config_parse (struct bsock_authz_hash_st* const restrict authz_hash,
                          char * const restrict buf)
{
    char *b, *e, *name;
    struct addrinfo * restrict ai = authz_hash->ai;
    int * restrict addr = authz_hash->addr;
    struct addrinfo **hh;
    struct bsock_addrinfo_strs aistr;
    int line = 1;
    struct passwd pw;
    struct passwd *pwres = ((void *)0);
    char pwbuf[2048];
    setpwent();
    for (b = buf; ((void *)0) != (e = strchr(b, '\n')); b = e+1, ++line) {
        if (*b == '\n' || *b == '#')
            continue;
        *e = '\0';
        b = strchr((name = b), ' ');
        if (((void *)0) != b && (*b = '\0',
                          0 == getpwnam_r(name,&pw,pwbuf,sizeof(pwbuf),&pwres)
                          && ((void *)0) != pwres)) {
            do { ++b; } while (*b == ' ');
        }
        else {
            bsock_syslog(22, 3, "invalid line (%d) in config (%s)",
                         line, "/usr/local/etc/bsock");
            return 22;
        }
        ai->ai_addr = (struct sockaddr *)addr;
        ai->ai_addrlen = sizeof(struct sockaddr_storage);
        if (bsock_addrinfo_split_str(&aistr, b)
            && bsock_addrinfo_from_strs(ai, &aistr)) {
            addr += ((ai->ai_addrlen + 7) & (size_t)~0x7);
            ai->ai_flags = (int)pw.pw_uid;
            hh = &authz_hash->table[(pw.pw_uid & authz_hash->mask)];
            ai->ai_next = *hh;
            *hh = ai++;
        }
        else {
            bsock_syslog(22, 3, "invalid line (%d) in config (%s)",
                         line, "/usr/local/etc/bsock");
            return (*__errno_location ());
        }
    }
    endpwent();
    return 0;
}

canonical_original_code:
static int bsock_authz_config_parse(struct bsock_authz_hash_st *authz_hash, char *buf)
{
    char *b; char *e; char *name;
    struct addrinfo *ai = authz_hash->ai;
    int *addr = authz_hash->addr;
    struct addrinfo **hh;
    struct bsock_addrinfo_strs aistr;
    int line = 1;
    struct passwd pw;
    struct passwd *pwres = ((void *)0);
    char pwbuf[2048];
    setpwent();
    for (b = buf; ((void *)0) != (e = strchr(b, '\n')); b = e+1, ++line) {
        if (*b == '\n' || *b == '#')
            continue;
        *e = '\0';
        b = strchr((name = b), ' ');
        if (((void *)0) != b && (*b = '\0',
                          0 == getpwnam_r(name,&pw,pwbuf,sizeof(pwbuf),&pwres)
                          && ((void *)0) != pwres)) {
            do { ++b; } while (*b == ' ');
        }
        else {
            bsock_syslog(22, 3, "invalid line (%d) in config (%s)",
                         line, "/usr/local/etc/bsock");
            return 22;
        }
        ai->ai_addr = (struct sockaddr *)addr;
        ai->ai_addrlen = sizeof(struct sockaddr_storage);
        if (bsock_addrinfo_split_str(&aistr, b)
            && bsock_addrinfo_from_strs(ai, &aistr)) {
            addr += ((ai->ai_addrlen + 7) & (unsigned long)~0x7);
            ai->ai_flags = (int)pw.pw_uid;
            hh = &authz_hash->table[(pw.pw_uid & authz_hash->mask)];
            ai->ai_next = *hh;
            *hh = ai++;
        }
        else {
            bsock_syslog(22, 3, "invalid line (%d) in config (%s)",
                         line, "/usr/local/etc/bsock");
            return (*__errno_location ());
        }
    }
    endpwent();
    return 0;
}

variable_types: struct bsock_authz_hash_st { unsigned int mask; 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; } **table; struct addrinfo *ai; int *addr; } *

variable_types:
buf
char *

variable_types:
b
char *

variable_types:
e
char *

variable_types:
name
char *

variable_types:
addr
int *

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

variable_types: line int

variable_types: struct passwd { char *pw_name; char *pw_passwd; unsigned int pw_uid; unsigned int pw_gid; char *pw_gecos; char *pw_dir; char *pw_shell; } *

variable_types:
pwbuf
char [2048]

user_defined_types: struct sockaddr_storage { unsigned short ss_family; char __ss_padding[-1]; unsigned long __ss_align; }

user_defined_types: struct bsock_authz_hash_st { unsigned int mask; struct addrinfo **table; struct addrinfo *ai; int *addr; }


decompiled_code:
void bsock_authz_config()
{
int v0; // eax
int *v1; // rax
int v2; // ebx
char *v3; // rbp
int v4; // er13
int v5; // er14
__uid_t v6; // ebp
__off_t v7; // r12
char *v8; // rax
char *v9; // r13
ssize_t v10; // rax
bool v11; // al
int *v12; // rax
char *v13; // rax
void *v14; // r12
int *v15; // rax
__int64 v16; // rdx
void *v17; // rdx
void *v18; // rdi
void *v19; // rbp
char *v20; // rax
char *v21; // rdi
int *v22; // rax
char *v23; // rdi
struct stat64 v24; // [rsp+0h] [rbp-B8h] BYREF

v0 = open64("/usr/local/etc/bsock", 2048, 0LL);
if ( v0 == -1 )
{
v1 = _errno_location();
v2 = 0;
v3 = 0LL;
v4 = 8;
bsock_syslog((unsigned int)*v1, 3LL, "/usr/local/etc/bsock");
}
else
{
v5 = v0;
v2 = fstat64(v0, &v24);
if ( v2 || (v6 = v24.st_uid, v6 != geteuid()) || (v24.st_mode & 0x12) != 0 )
{
v2 = 0;
v4 = 8;
v3 = 0LL;
bsock_syslog(1LL, 3LL, "ownership/permissions incorrect on %s", "/usr/local/etc/bsock");
}
else
{
v7 = v24.st_size;
v8 = (char *)malloc(v24.st_size + 2);
v3 = v8;
if ( v8 )
{
v8[v7 + 1] = 0;
v9 = v8;
v8[v7] = 0;
do
{
v10 = read(v5, v9, v7);
if ( v10 == -1 )
{
v11 = *_errno_location() == 4;
}
else
{
v9 += v10;
v7 -= v10;
v11 = v7 != 0;
}
}
while ( v11 );
if ( v7 )
{
v22 = _errno_location();
v4 = 8;
bsock_syslog((unsigned int)*v22, 3LL, "read config (%s)", "/usr/local/etc/bsock");
v23 = v3;
v3 = 0LL;
free(v23);
}
else
{
if ( v3[v24.st_size - 1] != 10 )
v3[v24.st_size] = 10;
v20 = v3;
while ( 1 )
{
v20 = strchr(v20 + 1, 10);
if ( !v20 )
break;
++v2;
}
v4 = 8;
v2 -= ((v2 & 1) == 0) - 1;
if ( v2 > 0x100000 )
{
bsock_syslog(22LL, 3LL, "too many lines in config (%s)", "/usr/local/etc/bsock");
v21 = v3;
v3 = 0LL;
free(v21);
}
else
{
while ( v2 > v4 )
v4 *= 2;
}
}
}
else
{
v12 = _errno_location();
v4 = 8;
bsock_syslog((unsigned int)*v12, 3LL, "malloc");
}
}
nointr_close_isra_0(v5);
}
v13 = (char *)malloc(8 * (v4 + 22LL * v2 + 4));
v14 = v13;
if ( v13 )
{
v16 = (__int64)&v13[8 * v4 + 32];
*(_DWORD *)v13 = v4 - 1;
*((_QWORD *)v13 + 2) = v16;
*((_QWORD *)v13 + 1) = v13 + 32;
v17 = (void *)(48LL * v2 + v16);
*((_QWORD *)v13 + 3) = v17;
memset(v13 + 32, 0, 8LL * v4);
memset(v17, 0, (__int64)v2 << 7);
if ( !v3 || (unsigned int)bsock_authz_config_parse((__int64)v13, v3) )
{
v18 = v14;
v14 = 0LL;
free(v18);
}
free(v3);
}
else
{
v15 = _errno_location();
bsock_syslog((unsigned int)*v15, 3LL, "malloc");
free(v3);
}
v19 = bsock_authz_hash;
if ( bsock_authz_hash )
{
if ( v14 )
{
bsock_authz_hash = v14;
while ( poll(0LL, 0LL, 1000) )
;
free(v19);
}
}
else
{
bsock_authz_hash = v14;
if ( !v14 )
exit(1);
}
}

canonical_decompiled_code:
void func4()
{
int v0; // eax
int *v1; // rax
int v2; // ebx
char *v3; // rbp
int v4; // er13
int v5; // er14
__uid_t v6; // ebp
__off_t v7; // r12
char *v8; // rax
char *v9; // r13
ssize_t v10; // rax
bool v11; // al
int *v12; // rax
char *v13; // rax
void *v14; // r12
int *v15; // rax
__int64 v16; // rdx
void *v17; // rdx
void *v18; // rdi
void *v19; // rbp
char *v20; // rax
char *v21; // rdi
int *v22; // rax
char *v23; // rdi
struct stat64 v24; // [rsp+0h] [rbp-B8h] BYREF

v0 = open64("/usr/local/etc/bsock", 2048, 0LL);
if ( v0 == -1 )
{
v1 = _errno_location();
v2 = 0;
v3 = 0LL;
v4 = 8;
bsock_syslog((unsigned int)*v1, 3LL, "/usr/local/etc/bsock");
}
else
{
v5 = v0;
v2 = fstat64(v0, &v24);
if ( v2 || (v6 = v24.st_uid, v6 != geteuid()) || (v24.st_mode & 0x12) != 0 )
{
v2 = 0;
v4 = 8;
v3 = 0LL;
bsock_syslog(1LL, 3LL, "ownership/permissions incorrect on %s", "/usr/local/etc/bsock");
}
else
{
v7 = v24.st_size;
v8 = (char *)malloc(v24.st_size + 2);
v3 = v8;
if ( v8 )
{
v8[v7 + 1] = 0;
v9 = v8;
v8[v7] = 0;
do
{
v10 = read(v5, v9, v7);
if ( v10 == -1 )
{
v11 = *_errno_location() == 4;
}
else
{
v9 += v10;
v7 -= v10;
v11 = v7 != 0;
}
}
while ( v11 );
if ( v7 )
{
v22 = _errno_location();
v4 = 8;
bsock_syslog((unsigned int)*v22, 3LL, "read config (%s)", "/usr/local/etc/bsock");
v23 = v3;
v3 = 0LL;
free(v23);
}
else
{
if ( v3[v24.st_size - 1] != 10 )
v3[v24.st_size] = 10;
v20 = v3;
while ( 1 )
{
v20 = strchr(v20 + 1, 10);
if ( !v20 )
break;
++v2;
}
v4 = 8;
v2 -= ((v2 & 1) == 0) - 1;
if ( v2 > 0x100000 )
{
bsock_syslog(22LL, 3LL, "too many lines in config (%s)", "/usr/local/etc/bsock");
v21 = v3;
v3 = 0LL;
free(v21);
}
else
{
while ( v2 > v4 )
v4 *= 2;
}
}
}
else
{
v12 = _errno_location();
v4 = 8;
bsock_syslog((unsigned int)*v12, 3LL, "malloc");
}
}
func3(v5);
}
v13 = (char *)malloc(8 * (v4 + 22LL * v2 + 4));
v14 = v13;
if ( v13 )
{
v16 = (__int64)&v13[8 * v4 + 32];
*(_DWORD *)v13 = v4 - 1;
*((_QWORD *)v13 + 2) = v16;
*((_QWORD *)v13 + 1) = v13 + 32;
v17 = (void *)(48LL * v2 + v16);
*((_QWORD *)v13 + 3) = v17;
memset(v13 + 32, 0, 8LL * v4);
memset(v17, 0, (__int64)v2 << 7);
if ( !v3 || (unsigned int)func2((__int64)v13, v3) )
{
v18 = v14;
v14 = 0LL;
free(v18);
}
free(v3);
}
else
{
v15 = _errno_location();
bsock_syslog((unsigned int)*v15, 3LL, "malloc");
free(v3);
}
v19 = bsock_authz_hash;
if ( bsock_authz_hash )
{
if ( v14 )
{
bsock_authz_hash = v14;
while ( poll(0LL, 0LL, 1000) )
;
free(v19);
}
}
else
{
bsock_authz_hash = v14;
if ( !v14 )
exit(1);
}
}

original_code:
__attribute__((__cold__))
__attribute__ ((__noinline__))
void
bsock_authz_config (void)
{
    char * restrict buf = ((void *)0);
    struct bsock_authz_hash_st * restrict authz_hash = ((void *)0);
    int fd = -1;
    int n = 0;
    int table_sz = 8;
    do {
        struct stat st;
        if (-1 == (fd = open("/usr/local/etc/bsock", 00|04000, 0))) {
            bsock_syslog((*__errno_location ()), 3, "/usr/local/etc/bsock");
            break;
        }
        if (0 != fstat(fd, &st)
            || st.st_uid != geteuid() || (st.st_mode & ((0200 >> 3)|((0200 >> 3) >> 3)))) {
            bsock_syslog(1, 3,
                         "ownership/permissions incorrect on %s", "/usr/local/etc/bsock");
            break;
        }
        if (((void *)0) != (buf = malloc((size_t)st.st_size+2))) {
            char * restrict b = buf;
            ssize_t r = 0;
            ssize_t len = st.st_size;
            b[len] = b[len+1] = '\0';
            do {
                r = read(fd, b, len);
            } while (-1 != r ? (b+=r, len-=r) : ((*__errno_location ()) == 4));
            if (0 == len) {
                b = buf;
                if (b[st.st_size-1] != '\n')
                    b[st.st_size] = '\n';
                while (((void *)0) != (b = strchr(b+1, '\n')))
                    ++n;
                if (n & 1)
                    ++n;
                if (n <= 1048576) {
                    while (table_sz < n)
                        table_sz <<= 1;
                }
                else {
                    bsock_syslog(22,3,"too many lines in config (%s)",
                                 "/usr/local/etc/bsock");
                    free(buf);
                    buf = ((void *)0);
                }
            }
            else {
                bsock_syslog((*__errno_location ()), 3, "read config (%s)", "/usr/local/etc/bsock");
                free(buf);
                buf = ((void *)0);
            }
        }
        else
            bsock_syslog((*__errno_location ()), 3, "malloc");
    } while (0);
    if (-1 != fd)
        nointr_close(fd);
    authz_hash = malloc( sizeof(struct bsock_authz_hash_st)
                        + sizeof(struct addrinfo *) * table_sz
                        + sizeof(struct addrinfo) * n
                        + sizeof(struct sockaddr_storage) * n);
    if (((void *)0) == authz_hash) {
        bsock_syslog((*__errno_location ()), 3, "malloc");
        free(buf);
        buf = ((void *)0);
    }
    else {
        authz_hash->mask = (uid_t)(table_sz-1);
        authz_hash->table = (struct addrinfo **)(authz_hash+1);
        authz_hash->ai = (struct addrinfo *)(authz_hash->table+table_sz);
        authz_hash->addr = (int *)(authz_hash->ai+n);
        memset(authz_hash->table, '\0', sizeof(struct addrinfo *) * table_sz);
        memset(authz_hash->addr, '\0', sizeof(struct sockaddr_storage) * n);
        if (((void *)0) == buf || 0 != bsock_authz_config_parse(authz_hash, buf)) {
            free(authz_hash);
            authz_hash = ((void *)0);
        }
        free(buf);
    }
    if (((void *)0) == bsock_authz_hash) {
        bsock_authz_hash = authz_hash;
        if (((void *)0) == authz_hash)
            exit(1);
    }
    else if (((void *)0) != authz_hash) {
        struct bsock_authz_hash_st * const restrict p = bsock_authz_hash;
        bsock_authz_hash = authz_hash;
        while (poll(((void *)0), 0, 1000) != 0)
            ;
        free(p);
    }
}

canonical_original_code:
void bsock_authz_config()
{
    char *buf = ((void *)0);
    struct bsock_authz_hash_st *authz_hash = ((void *)0);
    int fd = -1;
    int n = 0;
    int table_sz = 8;
    do {
        struct stat st;
        if (-1 == (fd = open("/usr/local/etc/bsock", 00|04000, 0))) {
            bsock_syslog((*__errno_location ()), 3, "/usr/local/etc/bsock");
            break;
        }
        if (0 != fstat(fd, &st)
            || st.st_uid != geteuid() || (st.st_mode & ((0200 >> 3)|((0200 >> 3) >> 3)))) {
            bsock_syslog(1, 3,
                         "ownership/permissions incorrect on %s", "/usr/local/etc/bsock");
            break;
        }
        if (((void *)0) != (buf = malloc((unsigned long)st.st_size+2))) {
            char *b = buf;
            long r = 0;
            long len = st.st_size;
            b[len] = b[len+1] = '\0';
            do {
                r = read(fd, b, len);
            } while (-1 != r ? (b+=r, len-=r) : ((*__errno_location ()) == 4));
            if (0 == len) {
                b = buf;
                if (b[st.st_size-1] != '\n')
                    b[st.st_size] = '\n';
                while (((void *)0) != (b = strchr(b+1, '\n')))
                    ++n;
                if (n & 1)
                    ++n;
                if (n <= 1048576) {
                    while (table_sz < n)
                        table_sz <<= 1;
                }
                else {
                    bsock_syslog(22,3,"too many lines in config (%s)",
                                 "/usr/local/etc/bsock");
                    free(buf);
                    buf = ((void *)0);
                }
            }
            else {
                bsock_syslog((*__errno_location ()), 3, "read config (%s)", "/usr/local/etc/bsock");
                free(buf);
                buf = ((void *)0);
            }
        }
        else
            bsock_syslog((*__errno_location ()), 3, "malloc");
    } while (0);
    if (-1 != fd)
        nointr_close(fd);
    authz_hash = malloc( sizeof(struct bsock_authz_hash_st)
                        + sizeof(struct addrinfo *) * table_sz
                        + sizeof(struct addrinfo) * n
                        + sizeof(struct sockaddr_storage) * n);
    if (((void *)0) == authz_hash) {
        bsock_syslog((*__errno_location ()), 3, "malloc");
        free(buf);
        buf = ((void *)0);
    }
    else {
        authz_hash->mask = (uid_t)(table_sz-1);
        authz_hash->table = (struct addrinfo **)(authz_hash+1);
        authz_hash->ai = (struct addrinfo *)(authz_hash->table+table_sz);
        authz_hash->addr = (int *)(authz_hash->ai+n);
        memset(authz_hash->table, '\0', sizeof(struct addrinfo *) * table_sz);
        memset(authz_hash->addr, '\0', sizeof(struct sockaddr_storage) * n);
        if (((void *)0) == buf || 0 != bsock_authz_config_parse(authz_hash, buf)) {
            free(authz_hash);
            authz_hash = ((void *)0);
        }
        free(buf);
    }
    if (((void *)0) == bsock_authz_hash) {
        bsock_authz_hash = authz_hash;
        if (((void *)0) == authz_hash)
            exit(1);
    }
    else if (((void *)0) != authz_hash) {
        struct bsock_authz_hash_st *p = bsock_authz_hash;
        bsock_authz_hash = authz_hash;
        while (poll(((void *)0), 0, 1000) != 0)
            ;
        free(p);
    }
}

variable_types: fd int

variable_types: n int

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: r long

variable_types: len long


decompiled_code:
const char *__fastcall bsock_addrinfo_family_to_str(int a1)
{
const char *result; // rax

result = "AF_INET";
if ( a1 != 2 )
{
if ( a1 > 2 )
{
result = "AF_INET6";
if ( a1 != 10 )
{
LABEL_8:
*_errno_location() = 97;
return 0LL;
}
}
else
{
result = "AF_UNSPEC";
if ( a1 )
{
if ( a1 == 1 )
return "AF_UNIX";
goto LABEL_8;
}
}
}
return result;
}

canonical_decompiled_code:
const char *__fastcall func0(int a1)
{
const char *result; // rax

result = "AF_INET";
if ( a1 != 2 )
{
if ( a1 > 2 )
{
result = "AF_INET6";
if ( a1 != 10 )
{
LABEL_8:
*_errno_location() = 97;
return 0LL;
}
}
else
{
result = "AF_UNSPEC";
if ( a1 )
{
if ( a1 == 1 )
return "AF_UNIX";
goto LABEL_8;
}
}
}
return result;
}

original_code:
static const char *
bsock_addrinfo_family_to_str (const int family)
{
    switch (family) {
      case 0: return "AF_UNSPEC";
      case 1: return "AF_UNIX";
      case 2: return "AF_INET";
      case 10: return "AF_INET6";
      default: (*__errno_location ()) = 97;
                       return ((void *)0);
    }
}

canonical_original_code:
static char *bsock_addrinfo_family_to_str(int family)
{
    switch (family) {
      case 0: return "AF_UNSPEC";
      case 1: return "AF_UNIX";
      case 2: return "AF_INET";
      case 10: return "AF_INET6";
      default: (*__errno_location ()) = 97;
                       return ((void *)0);
    }
}

variable_types: family int

return_type: char *


decompiled_code:
const char *__fastcall bsock_addrinfo_socktype_to_str(int a1)
{
const char *result; // rax

switch ( a1 )
{
case 1:
result = "SOCK_STREAM";
break;
case 2:
result = "SOCK_DGRAM";
break;
case 3:
result = "SOCK_RAW";
break;
case 4:
result = "SOCK_RDM";
break;
case 5:
result = "SOCK_SEQPACKET";
break;
case 6:
result = "SOCK_DCCP";
break;
case 10:
result = "SOCK_PACKET";
break;
default:
*_errno_location() = 94;
result = 0LL;
break;
}
return result;
}

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

switch ( a1 )
{
case 1:
result = "SOCK_STREAM";
break;
case 2:
result = "SOCK_DGRAM";
break;
case 3:
result = "SOCK_RAW";
break;
case 4:
result = "SOCK_RDM";
break;
case 5:
result = "SOCK_SEQPACKET";
break;
case 6:
result = "SOCK_DCCP";
break;
case 10:
result = "SOCK_PACKET";
break;
default:
*_errno_location() = 94;
result = 0LL;
break;
}
return result;
}

original_code:
static const char *
bsock_addrinfo_socktype_to_str (const int socktype)
{
    switch (socktype) {
      case SOCK_STREAM: return "SOCK_STREAM";
      case SOCK_DGRAM: return "SOCK_DGRAM";
      case SOCK_RAW: return "SOCK_RAW";
      case SOCK_RDM: return "SOCK_RDM";
      case SOCK_SEQPACKET: return "SOCK_SEQPACKET";
      case SOCK_DCCP: return "SOCK_DCCP";
      case SOCK_PACKET: return "SOCK_PACKET";
      default: (*__errno_location ()) = 94;
                           return ((void *)0);
    }
}

canonical_original_code:
static char *bsock_addrinfo_socktype_to_str(int socktype)
{
    switch (socktype) {
      case SOCK_STREAM: return "SOCK_STREAM";
      case SOCK_DGRAM: return "SOCK_DGRAM";
      case SOCK_RAW: return "SOCK_RAW";
      case SOCK_RDM: return "SOCK_RDM";
      case SOCK_SEQPACKET: return "SOCK_SEQPACKET";
      case SOCK_DCCP: return "SOCK_DCCP";
      case SOCK_PACKET: return "SOCK_PACKET";
      default: (*__errno_location ()) = 94;
                           return ((void *)0);
    }
}

variable_types: socktype int


original_code:
__attribute__((__nonnull__ ))
static int
bsock_addrinfo_family_from_str (const char * const restrict family)
{
    if ( 0 == memcmp((family),("AF_INET"),sizeof("AF_INET"))
             || 0 == memcmp((family),("PF_INET"),sizeof("PF_INET")))
        return 2;
    else if ( 0 == memcmp((family),("AF_INET6"),sizeof("AF_INET6"))
             || 0 == memcmp((family),("PF_INET6"),sizeof("PF_INET6")))
        return 10;
    else if ( 0 == memcmp((family),("AF_LOCAL"),sizeof("AF_LOCAL"))
             || 0 == memcmp((family),("AF_UNIX"),sizeof("AF_UNIX"))
             || 0 == memcmp((family),("AF_FILE"),sizeof("AF_FILE"))
             || 0 == memcmp((family),("PF_LOCAL"),sizeof("PF_LOCAL"))
             || 0 == memcmp((family),("PF_UNIX"),sizeof("PF_UNIX"))
             || 0 == memcmp((family),("PF_FILE"),sizeof("PF_FILE")))
        return 1;
    else if ( 0 == memcmp((family),("AF_UNSPEC"),sizeof("AF_UNSPEC"))
             || 0 == memcmp((family),("PF_UNSPEC"),sizeof("PF_UNSPEC")))
        return 0;
    else {
        (*__errno_location ()) = 97;
        return -1;
    }
}

canonical_original_code:
static int bsock_addrinfo_family_from_str(char *family)
{
    if ( 0 == memcmp((family),("AF_INET"),sizeof("AF_INET"))
             || 0 == memcmp((family),("PF_INET"),sizeof("PF_INET")))
        return 2;
    else if ( 0 == memcmp((family),("AF_INET6"),sizeof("AF_INET6"))
             || 0 == memcmp((family),("PF_INET6"),sizeof("PF_INET6")))
        return 10;
    else if ( 0 == memcmp((family),("AF_LOCAL"),sizeof("AF_LOCAL"))
             || 0 == memcmp((family),("AF_UNIX"),sizeof("AF_UNIX"))
             || 0 == memcmp((family),("AF_FILE"),sizeof("AF_FILE"))
             || 0 == memcmp((family),("PF_LOCAL"),sizeof("PF_LOCAL"))
             || 0 == memcmp((family),("PF_UNIX"),sizeof("PF_UNIX"))
             || 0 == memcmp((family),("PF_FILE"),sizeof("PF_FILE")))
        return 1;
    else if ( 0 == memcmp((family),("AF_UNSPEC"),sizeof("AF_UNSPEC"))
             || 0 == memcmp((family),("PF_UNSPEC"),sizeof("PF_UNSPEC")))
        return 0;
    else {
        (*__errno_location ()) = 97;
        return -1;
    }
}

variable_types:
family
char *


original_code:
__attribute__((__nonnull__ ))
static int
bsock_addrinfo_socktype_from_str (const char * const restrict socktype)
{
    if ( 0 == memcmp((socktype),("SOCK_STREAM"),sizeof("SOCK_STREAM")))
        return SOCK_STREAM;
    else if (0 == memcmp((socktype),("SOCK_DGRAM"),sizeof("SOCK_DGRAM")))
        return SOCK_DGRAM;
    else if (0 == memcmp((socktype),("SOCK_RAW"),sizeof("SOCK_RAW")))
        return SOCK_RAW;
    else if (0 == memcmp((socktype),("SOCK_RDM"),sizeof("SOCK_RDM")))
        return SOCK_RDM;
    else if (0 == memcmp((socktype),("SOCK_SEQPACKET"),sizeof("SOCK_SEQPACKET")))
        return SOCK_SEQPACKET;
    else if (0 == memcmp((socktype),("SOCK_DCCP"),sizeof("SOCK_DCCP")))
        return SOCK_DCCP;
    else if (0 == memcmp((socktype),("SOCK_PACKET"),sizeof("SOCK_PACKET")))
        return SOCK_PACKET;
    else {
        (*__errno_location ()) = 94;
        return -1;
    }
}

canonical_original_code:
static int bsock_addrinfo_socktype_from_str(char *socktype)
{
    if ( 0 == memcmp((socktype),("SOCK_STREAM"),sizeof("SOCK_STREAM")))
        return SOCK_STREAM;
    else if (0 == memcmp((socktype),("SOCK_DGRAM"),sizeof("SOCK_DGRAM")))
        return SOCK_DGRAM;
    else if (0 == memcmp((socktype),("SOCK_RAW"),sizeof("SOCK_RAW")))
        return SOCK_RAW;
    else if (0 == memcmp((socktype),("SOCK_RDM"),sizeof("SOCK_RDM")))
        return SOCK_RDM;
    else if (0 == memcmp((socktype),("SOCK_SEQPACKET"),sizeof("SOCK_SEQPACKET")))
        return SOCK_SEQPACKET;
    else if (0 == memcmp((socktype),("SOCK_DCCP"),sizeof("SOCK_DCCP")))
        return SOCK_DCCP;
    else if (0 == memcmp((socktype),("SOCK_PACKET"),sizeof("SOCK_PACKET")))
        return SOCK_PACKET;
    else {
        (*__errno_location ()) = 94;
        return -1;
    }
}

variable_types:
socktype
char *


decompiled_code:
__int64 __fastcall bsock_addrinfo_protocol_from_str(char *nptr)
{
unsigned int v1; // er12
int *v3; // rax
int *v4; // rbx
unsigned __int64 v5; // rax
struct protoent *result; // [rsp+8h] [rbp-450h] BYREF
struct protoent result_buf; // [rsp+10h] [rbp-448h] BYREF
char buf[1032]; // [rsp+30h] [rbp-428h] BYREF
unsigned __int64 v9; // [rsp+438h] [rbp-20h]

v9 = __readfsqword(0x28u);
if ( ((*_ctype_b_loc())[(unsigned __int8)*nptr] & 0x800) == 0 )
{
if ( !getprotobyname_r(nptr, &result_buf, buf, 0x400uLL, &result) )
return (unsigned int)result_buf.p_proto;
v4 = _errno_location();
LABEL_9:
*v4 = 93;
return (unsigned int)-1;
}
v3 = _errno_location();
*v3 = 0;
v4 = v3;
v5 = strtol(nptr, &result_buf.p_name, 10);
if ( *v4 )
goto LABEL_9;
if ( *result_buf.p_name )
goto LABEL_9;
if ( v5 >> 31 )
goto LABEL_9;
v1 = v5;
if ( !getprotobynumber(v5) )
goto LABEL_9;
return v1;
}

canonical_decompiled_code:
__int64 __fastcall func4(char *nptr)
{
unsigned int v1; // er12
int *v3; // rax
int *v4; // rbx
unsigned __int64 v5; // rax
struct protoent *result; // [rsp+8h] [rbp-450h] BYREF
struct protoent result_buf; // [rsp+10h] [rbp-448h] BYREF
char buf[1032]; // [rsp+30h] [rbp-428h] BYREF
unsigned __int64 v9; // [rsp+438h] [rbp-20h]

v9 = __readfsqword(0x28u);
if ( ((*_ctype_b_loc())[(unsigned __int8)*nptr] & 0x800) == 0 )
{
if ( !getprotobyname_r(nptr, &result_buf, buf, 0x400uLL, &result) )
return (unsigned int)result_buf.p_proto;
v4 = _errno_location();
LABEL_9:
*v4 = 93;
return (unsigned int)-1;
}
v3 = _errno_location();
*v3 = 0;
v4 = v3;
v5 = strtol(nptr, &result_buf.p_name, 10);
if ( *v4 )
goto LABEL_9;
if ( *result_buf.p_name )
goto LABEL_9;
if ( v5 >> 31 )
goto LABEL_9;
v1 = v5;
if ( !getprotobynumber(v5) )
goto LABEL_9;
return v1;
}

original_code:
__attribute__((__nonnull__ ))
static int
bsock_addrinfo_protocol_from_str (const char * const restrict protocol)
{
    if (!((*__ctype_b_loc ())[(int) ((((const unsigned char *)protocol)[0]))] & (unsigned short int) _ISdigit)) {
        struct protoent pe;
        struct protoent *peres;
        char buf[1024];
        if (0 == getprotobyname_r(protocol, &pe, buf, sizeof(buf), &peres))
            return pe.p_proto;
    }
    else {
        char *e;
        long lproto;
        if (((*__errno_location ()) = 0, lproto = strtol(protocol, &e, 10), 0 == (*__errno_location ()))
            && '\0' == *e && 0 == (lproto >> 31)
            && ((void *)0) != getprotobynumber((int)lproto))
            return (int)lproto;
    }
    (*__errno_location ()) = 93;
    return -1;
}

canonical_original_code:
static int bsock_addrinfo_protocol_from_str(char *protocol)
{
    if (!((*__ctype_b_loc ())[(int) ((((unsigned char *)protocol)[0]))] & (unsigned short) _ISdigit)) {
        struct protoent pe;
        struct protoent *peres;
        char buf[1024];
        if (0 == getprotobyname_r(protocol, &pe, buf, sizeof(buf), &peres))
            return pe.p_proto;
    }
    else {
        char *e;
        long lproto;
        if (((*__errno_location ()) = 0, lproto = strtol(protocol, &e, 10), 0 == (*__errno_location ()))
            && '\0' == *e && 0 == (lproto >> 31)
            && ((void *)0) != getprotobynumber((int)lproto))
            return (int)lproto;
    }
    (*__errno_location ()) = 93;
    return -1;
}

variable_types:
protocol
char *

variable_types: struct protoent { char *p_name; char **p_aliases; int p_proto; } *

variable_types:
buf
char [1024]

variable_types: lproto long


decompiled_code:
char *__fastcall bsock_addrinfo_protocol_to_str(int a1, char *a2, size_t a3)
{
int v3; // eax
int v5; // ebx
struct protoent *result; // [rsp+8h] [rbp-30h] BYREF
struct protoent result_buf; // [rsp+10h] [rbp-28h] BYREF

v3 = getprotobynumber_r(a1, &result_buf, a2, a3, &result);
if ( !v3 )
return result_buf.p_name;
v5 = 28;
if ( v3 != 34 )
v5 = 93;
*_errno_location() = v5;
return 0LL;
}

canonical_decompiled_code:
char *__fastcall func5(int a1, char *a2, size_t a3)
{
int v3; // eax
int v5; // ebx
struct protoent *result; // [rsp+8h] [rbp-30h] BYREF
struct protoent result_buf; // [rsp+10h] [rbp-28h] BYREF

v3 = getprotobynumber_r(a1, &result_buf, a2, a3, &result);
if ( !v3 )
return result_buf.p_name;
v5 = 28;
if ( v3 != 34 )
v5 = 93;
*_errno_location() = v5;
return 0LL;
}

original_code:
__attribute__((__nonnull__ ))
static char *
bsock_addrinfo_protocol_to_str (const int proto,
                                char * const buf, const size_t bufsz)
{
    struct protoent pe;
    struct protoent *peres;
    int rc;
    return (0 == (rc = getprotobynumber_r(proto, &pe, buf, bufsz, &peres)))
      ? pe.p_name
      : ((*__errno_location ()) = (rc == 34 ? 28 : 93), (char *)((void *)0));
}

canonical_original_code:
static char *bsock_addrinfo_protocol_to_str(int proto, char *buf, unsigned long bufsz)
{
    struct protoent pe;
    struct protoent *peres;
    int rc;
    return (0 == (rc = getprotobynumber_r(proto, &pe, buf, bufsz, &peres)))
      ? pe.p_name
      : ((*__errno_location ()) = (rc == 34 ? 28 : 93), (char *)((void *)0));
}

variable_types: proto int

variable_types:
buf
char *

variable_types:
bufsz
unsigned long

variable_types: struct protoent { char *p_name; char **p_aliases; int p_proto; }

variable_types: rc int


decompiled_code:
__int64 __fastcall bsock_addrinfo_from_strs(__int64 a1, __int64 a2)
{
unsigned int v3; // er12
__int64 v4; // rdi
int v5; // eax
__int64 v6; // rdi
int v7; // eax
char *v8; // rdi
int v9; // ebp
const char *v10; // rbx
size_t v11; // rax
int v13; // eax
struct addrinfo *v14; // rdi
size_t v15; // rdx
int v16; // eax
void *v17; // r8
sockaddr *v18; // rsi
__int64 v19; // rax
__int64 v20; // rdi
unsigned int v21; // eax
__int64 v22; // rcx
__int64 v23; // rdi
struct addrinfo *pai; // [rsp+0h] [rbp-60h] BYREF
struct addrinfo req; // [rsp+8h] [rbp-58h] BYREF

v3 = 0;
v4 = *(_QWORD *)a2;
*(_OWORD *)&req.ai_flags = 0LL;
*(_OWORD *)&req.ai_addrlen = 0LL;
*(_OWORD *)&req.ai_canonname = 0LL;
req.ai_flags = 40;
v5 = bsock_addrinfo_family_from_str(v4);
v6 = *(_QWORD *)(a2 + 8);
req.ai_family = v5;
v7 = bsock_addrinfo_socktype_from_str(v6);
v8 = *(char **)(a2 + 16);
req.ai_socktype = v7;
req.ai_protocol = bsock_addrinfo_protocol_from_str(v8);
v9 = req.ai_protocol;
if ( req.ai_family == -1 )
return v3;
LOBYTE(v3) = req.ai_socktype == -1 || req.ai_protocol == -1;
if ( (_BYTE)v3 )
return 0LL;
if ( (req.ai_family & 0xFFFFFFF7) == 2 )
{
v13 = getaddrinfo(*(const char **)(a2 + 32), *(const char **)(a2 + 24), &req, &pai);
if ( v13 )
{
if ( v13 != -11 )
*_errno_location() = 22;
}
else
{
v14 = pai;
v15 = pai->ai_addrlen;
if ( *(_DWORD *)(a1 + 16) < (unsigned int)v15 )
{
freeaddrinfo(pai);
*_errno_location() = 28;
}
else
{
v16 = pai->ai_family;
v17 = *(void **)(a1 + 24);
*(_DWORD *)(a1 + 16) = v15;
v3 = 1;
*(_DWORD *)a1 = 0;
v18 = v14->ai_addr;
*(_DWORD *)(a1 + 4) = v16;
v19 = *(_QWORD *)&v14->ai_socktype;
*(_QWORD *)(a1 + 32) = 0LL;
*(_QWORD *)(a1 + 8) = v19;
*(_QWORD *)(a1 + 40) = 0LL;
memcpy(v17, v18, v15);
freeaddrinfo(pai);
}
}
return v3;
}
if ( req.ai_family == 1 )
{
v10 = *(const char **)(a2 + 32);
v11 = strlen(v10);
if ( v11 <= 0x6B && *(_DWORD *)(a1 + 16) > 0x6Du )
{
v20 = *(_QWORD *)(a1 + 24);
v21 = v11 + 1;
*(_DWORD *)(a1 + 8) = req.ai_socktype;
*(_DWORD *)(a1 + 12) = v9;
*(_QWORD *)a1 = 0x100000000LL;
v22 = v20 + 2;
*(_DWORD *)(a1 + 16) = 110;
*(_QWORD *)(a1 + 32) = 0LL;
*(_QWORD *)(a1 + 40) = 0LL;
*(_WORD *)v20 = 1;
if ( v21 >= 8 )
{
v23 = v20 + 10;
*(_QWORD *)(v23 - 8) = *(_QWORD *)v10;
*(_QWORD *)(v22 + v21 - 8) = *(_QWORD *)&v10[v21 - 8];
qmemcpy(
(void *)(v23 & 0xFFFFFFFFFFFFFFF8LL),
&v10[-(v22 - (v23 & 0xFFFFFFFFFFFFFFF8LL))],
8 * ((unsigned __int64)((_DWORD)v22 - (v23 & 0xFFFFFFF8) + v21) >> 3));
}
else if ( (v21 & 4) != 0 )
{
*(_DWORD *)(v20 + 2) = *(_DWORD *)v10;
*(_DWORD *)(v22 + v21 - 4) = *(_DWORD *)&v10[v21 - 4];
}
else if ( v21 )
{
*(_BYTE *)(v20 + 2) = *v10;
if ( (v21 & 2) != 0 )
*(_WORD *)(v20 + v21) = *(_WORD *)&v10[v21 - 2];
}
v3 = 1;
}
else
{
*_errno_location() = 28;
}
return v3;
}
*_errno_location() = 97;
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, __int64 a2)
{
unsigned int v3; // er12
__int64 v4; // rdi
int v5; // eax
__int64 v6; // rdi
int v7; // eax
char *v8; // rdi
int v9; // ebp
const char *v10; // rbx
size_t v11; // rax
int v13; // eax
struct addrinfo *v14; // rdi
size_t v15; // rdx
int v16; // eax
void *v17; // r8
sockaddr *v18; // rsi
__int64 v19; // rax
__int64 v20; // rdi
unsigned int v21; // eax
__int64 v22; // rcx
__int64 v23; // rdi
struct addrinfo *pai; // [rsp+0h] [rbp-60h] BYREF
struct addrinfo req; // [rsp+8h] [rbp-58h] BYREF

v3 = 0;
v4 = *(_QWORD *)a2;
*(_OWORD *)&req.ai_flags = 0LL;
*(_OWORD *)&req.ai_addrlen = 0LL;
*(_OWORD *)&req.ai_canonname = 0LL;
req.ai_flags = 40;
v5 = func2(v4);
v6 = *(_QWORD *)(a2 + 8);
req.ai_family = v5;
v7 = func3(v6);
v8 = *(char **)(a2 + 16);
req.ai_socktype = v7;
req.ai_protocol = func4(v8);
v9 = req.ai_protocol;
if ( req.ai_family == -1 )
return v3;
LOBYTE(v3) = req.ai_socktype == -1 || req.ai_protocol == -1;
if ( (_BYTE)v3 )
return 0LL;
if ( (req.ai_family & 0xFFFFFFF7) == 2 )
{
v13 = getaddrinfo(*(const char **)(a2 + 32), *(const char **)(a2 + 24), &req, &pai);
if ( v13 )
{
if ( v13 != -11 )
*_errno_location() = 22;
}
else
{
v14 = pai;
v15 = pai->ai_addrlen;
if ( *(_DWORD *)(a1 + 16) < (unsigned int)v15 )
{
freeaddrinfo(pai);
*_errno_location() = 28;
}
else
{
v16 = pai->ai_family;
v17 = *(void **)(a1 + 24);
*(_DWORD *)(a1 + 16) = v15;
v3 = 1;
*(_DWORD *)a1 = 0;
v18 = v14->ai_addr;
*(_DWORD *)(a1 + 4) = v16;
v19 = *(_QWORD *)&v14->ai_socktype;
*(_QWORD *)(a1 + 32) = 0LL;
*(_QWORD *)(a1 + 8) = v19;
*(_QWORD *)(a1 + 40) = 0LL;
memcpy(v17, v18, v15);
freeaddrinfo(pai);
}
}
return v3;
}
if ( req.ai_family == 1 )
{
v10 = *(const char **)(a2 + 32);
v11 = strlen(v10);
if ( v11 <= 0x6B && *(_DWORD *)(a1 + 16) > 0x6Du )
{
v20 = *(_QWORD *)(a1 + 24);
v21 = v11 + 1;
*(_DWORD *)(a1 + 8) = req.ai_socktype;
*(_DWORD *)(a1 + 12) = v9;
*(_QWORD *)a1 = 0x100000000LL;
v22 = v20 + 2;
*(_DWORD *)(a1 + 16) = 110;
*(_QWORD *)(a1 + 32) = 0LL;
*(_QWORD *)(a1 + 40) = 0LL;
*(_WORD *)v20 = 1;
if ( v21 >= 8 )
{
v23 = v20 + 10;
*(_QWORD *)(v23 - 8) = *(_QWORD *)v10;
*(_QWORD *)(v22 + v21 - 8) = *(_QWORD *)&v10[v21 - 8];
qmemcpy(
(void *)(v23 & 0xFFFFFFFFFFFFFFF8LL),
&v10[-(v22 - (v23 & 0xFFFFFFFFFFFFFFF8LL))],
8 * ((unsigned __int64)((_DWORD)v22 - (v23 & 0xFFFFFFF8) + v21) >> 3));
}
else if ( (v21 & 4) != 0 )
{
*(_DWORD *)(v20 + 2) = *(_DWORD *)v10;
*(_DWORD *)(v22 + v21 - 4) = *(_DWORD *)&v10[v21 - 4];
}
else if ( v21 )
{
*(_BYTE *)(v20 + 2) = *v10;
if ( (v21 & 2) != 0 )
*(_WORD *)(v20 + v21) = *(_WORD *)&v10[v21 - 2];
}
v3 = 1;
}
else
{
*_errno_location() = 28;
}
return v3;
}
*_errno_location() = 97;
return v3;
}

original_code:
__attribute__((__nonnull__ ))
_Bool
bsock_addrinfo_from_strs(struct addrinfo * const restrict ai,
                         const struct bsock_addrinfo_strs *
                           const restrict aistr)
{
    struct addrinfo hints = {
      .ai_flags = 0x0008 | 0x0020,
      .ai_addrlen = 0,
      .ai_addr = ((void *)0),
      .ai_canonname = ((void *)0),
      .ai_next = ((void *)0)
    };
    hints.ai_family = bsock_addrinfo_family_from_str(aistr->family);
    hints.ai_socktype = bsock_addrinfo_socktype_from_str(aistr->socktype);
    hints.ai_protocol = bsock_addrinfo_protocol_from_str(aistr->protocol);
    if (-1==hints.ai_family || -1==hints.ai_socktype || -1==hints.ai_protocol)
        return 0;
    if (hints.ai_family == 2 || hints.ai_family == 10) {
        struct addrinfo *gai;
        int r;
        if (0 == (r = getaddrinfo(aistr->addr, aistr->service, &hints, &gai))) {
            if (ai->ai_addrlen >= gai->ai_addrlen) {
                ai->ai_flags = 0;
                ai->ai_family = gai->ai_family;
                ai->ai_socktype = gai->ai_socktype;
                ai->ai_protocol = gai->ai_protocol;
                ai->ai_addrlen = gai->ai_addrlen;
                ai->ai_canonname = ((void *)0);
                ai->ai_next = ((void *)0);
                memcpy(ai->ai_addr, gai->ai_addr, gai->ai_addrlen);
                freeaddrinfo(gai);
                return 1;
            }
            else {
                freeaddrinfo(gai);
                (*__errno_location ()) = 28;
                return 0;
            }
        }
        if (-11 != r) (*__errno_location ()) = 22;
        return 0;
    }
    else if (hints.ai_family == 1) {
        const size_t len = strlen(aistr->addr);
        if (len < sizeof(((struct sockaddr_un *)0)->sun_path)
            && (socklen_t)sizeof(struct sockaddr_un) <= ai->ai_addrlen) {
            ai->ai_flags = 0;
            ai->ai_family = hints.ai_family;
            ai->ai_socktype = hints.ai_socktype;
            ai->ai_protocol = hints.ai_protocol;
            ai->ai_addrlen = sizeof(struct sockaddr_un);
            ai->ai_canonname= ((void *)0);
            ai->ai_next = ((void *)0);
            ((struct sockaddr_un *)ai->ai_addr)->sun_family = 1;
            memcpy(((struct sockaddr_un *)ai->ai_addr)->sun_path,
                   aistr->addr, len+1);
            return 1;
        }
        (*__errno_location ()) = 28;
        return 0;
    }
    else {
        (*__errno_location ()) = 97;
        return 0;
    }
}

canonical_original_code:
_Bool bsock_addrinfo_from_strs(struct addrinfo *ai, struct bsock_addrinfo_strs *aistr)
{
    struct addrinfo hints = {
      .ai_flags = 0x0008 | 0x0020,
      .ai_addrlen = 0,
      .ai_addr = ((void *)0),
      .ai_canonname = ((void *)0),
      .ai_next = ((void *)0)
    };
    hints.ai_family = bsock_addrinfo_family_from_str(aistr->family);
    hints.ai_socktype = bsock_addrinfo_socktype_from_str(aistr->socktype);
    hints.ai_protocol = bsock_addrinfo_protocol_from_str(aistr->protocol);
    if (-1==hints.ai_family || -1==hints.ai_socktype || -1==hints.ai_protocol)
        return 0;
    if (hints.ai_family == 2 || hints.ai_family == 10) {
        struct addrinfo *gai;
        int r;
        if (0 == (r = getaddrinfo(aistr->addr, aistr->service, &hints, &gai))) {
            if (ai->ai_addrlen >= gai->ai_addrlen) {
                ai->ai_flags = 0;
                ai->ai_family = gai->ai_family;
                ai->ai_socktype = gai->ai_socktype;
                ai->ai_protocol = gai->ai_protocol;
                ai->ai_addrlen = gai->ai_addrlen;
                ai->ai_canonname = ((void *)0);
                ai->ai_next = ((void *)0);
                memcpy(ai->ai_addr, gai->ai_addr, gai->ai_addrlen);
                freeaddrinfo(gai);
                return 1;
            }
            else {
                freeaddrinfo(gai);
                (*__errno_location ()) = 28;
                return 0;
            }
        }
        if (-11 != r) (*__errno_location ()) = 22;
        return 0;
    }
    else if (hints.ai_family == 1) {
        unsigned long len = strlen(aistr->addr);
        if (len < sizeof(((struct sockaddr_un *)0)->sun_path)
            && (unsigned int)sizeof(struct sockaddr_un) <= ai->ai_addrlen) {
            ai->ai_flags = 0;
            ai->ai_family = hints.ai_family;
            ai->ai_socktype = hints.ai_socktype;
            ai->ai_protocol = hints.ai_protocol;
            ai->ai_addrlen = sizeof(struct sockaddr_un);
            ai->ai_canonname= ((void *)0);
            ai->ai_next = ((void *)0);
            ((struct sockaddr_un *)ai->ai_addr)->sun_family = 1;
            memcpy(((struct sockaddr_un *)ai->ai_addr)->sun_path,
                   aistr->addr, len+1);
            return 1;
        }
        (*__errno_location ()) = 28;
        return 0;
    }
    else {
        (*__errno_location ()) = 97;
        return 0;
    }
}

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

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

variable_types: r int

variable_types:
len
unsigned long

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


decompiled_code:
__int64 __fastcall bsock_addrinfo_to_strs(__int64 a1, const char **a2, char *a3, size_t a4)
{
unsigned int v4; // er15
const char *v9; // rax
int v10; // edi
const char *v11; // rax
int v12; // edi
char *v13; // rax
bool v14; // zf
char *v15; // r12
const char *v16; // rax
socklen_t v18; // esi
size_t v19; // [rsp+18h] [rbp-40h]

v9 = bsock_addrinfo_family_to_str(*(_DWORD *)(a1 + 4));
v10 = *(_DWORD *)(a1 + 8);
*a2 = v9;
v11 = bsock_addrinfo_socktype_to_str(v10);
v12 = *(_DWORD *)(a1 + 12);
a2[1] = v11;
v13 = bsock_addrinfo_protocol_to_str(v12, a3, a4);
v14 = *a2 == 0LL;
a2[2] = v13;
if ( v14 )
return 0;
v15 = v13;
LOBYTE(v4) = v13 == 0LL || a2[1] == 0LL;
if ( (_BYTE)v4 )
return 0;
v19 = strlen(v13);
v16 = (const char *)memmove(&a3[a4 - (v19 + 1)], v15, v19 + 1);
a2[2] = v16;
if ( (*(_DWORD *)(a1 + 4) & 0xFFFFFFF7) == 2 )
{
if ( v19 + 53 <= a4 )
{
a2[4] = a3;
v18 = *(_DWORD *)(a1 + 16);
a2[3] = v16 - 6;
LOBYTE(v4) = getnameinfo(
*(const struct sockaddr **)(a1 + 24),
v18,
a3,
a4 - 6 - (v19 + 1),
&a3[a4 - (v19 + 1) - 6],
6u,
3) == 0;
}
else
{
*_errno_location() = 28;
}
}
else if ( *(_DWORD *)(a1 + 4) == 1 )
{
v4 = 1;
a2[3] = "0";
a2[4] = (const char *)(*(_QWORD *)(a1 + 24) + 2LL);
}
else
{
*_errno_location() = 97;
}
return v4;
}

original_code:
__attribute__((__nonnull__ ))
_Bool
bsock_addrinfo_to_strs(const struct addrinfo * const restrict ai,
                       struct bsock_addrinfo_strs * const aistr,
                       char * const restrict buf, const size_t bufsz)
{
    size_t protolen;
    aistr->family = bsock_addrinfo_family_to_str(ai->ai_family);
    aistr->socktype = bsock_addrinfo_socktype_to_str(ai->ai_socktype);
    aistr->protocol = bsock_addrinfo_protocol_to_str(ai->ai_protocol,buf,bufsz);
    if (((void *)0)==aistr->family || ((void *)0)==aistr->socktype || ((void *)0)==aistr->protocol)
        return 0;
    protolen = strlen(aistr->protocol) + 1;
    memmove(buf+bufsz-protolen, aistr->protocol, protolen);
    aistr->protocol = buf+bufsz-protolen;
    if (ai->ai_family == 2 || ai->ai_family == 10) {
        if (46 + 6 + protolen > bufsz) {
            (*__errno_location ()) = 28;
            return 0;
        }
        aistr->service = aistr->protocol - 6;
        aistr->addr = buf;
        if (0 == getnameinfo(ai->ai_addr, ai->ai_addrlen,
                             buf, bufsz-protolen-6,
                             buf+bufsz-protolen-6, 6,
                             1|2))
            return 1;
    }
    else if (ai->ai_family == 1) {
        aistr->service = "0";
        aistr->addr = ((struct sockaddr_un *)ai->ai_addr)->sun_path;
        return 1;
    }
    else
        (*__errno_location ()) = 97;
    return 0;
}

canonical_original_code:
_Bool bsock_addrinfo_to_strs(struct addrinfo *ai, struct bsock_addrinfo_strs *aistr, char *buf, unsigned long bufsz)
{
    unsigned long protolen;
    aistr->family = bsock_addrinfo_family_to_str(ai->ai_family);
    aistr->socktype = bsock_addrinfo_socktype_to_str(ai->ai_socktype);
    aistr->protocol = bsock_addrinfo_protocol_to_str(ai->ai_protocol,buf,bufsz);
    if (((void *)0)==aistr->family || ((void *)0)==aistr->socktype || ((void *)0)==aistr->protocol)
        return 0;
    protolen = strlen(aistr->protocol) + 1;
    memmove(buf+bufsz-protolen, aistr->protocol, protolen);
    aistr->protocol = buf+bufsz-protolen;
    if (ai->ai_family == 2 || ai->ai_family == 10) {
        if (46 + 6 + protolen > bufsz) {
            (*__errno_location ()) = 28;
            return 0;
        }
        aistr->service = aistr->protocol - 6;
        aistr->addr = buf;
        if (0 == getnameinfo(ai->ai_addr, ai->ai_addrlen,
                             buf, bufsz-protolen-6,
                             buf+bufsz-protolen-6, 6,
                             1|2))
            return 1;
    }
    else if (ai->ai_family == 1) {
        aistr->service = "0";
        aistr->addr = ((struct sockaddr_un *)ai->ai_addr)->sun_path;
        return 1;
    }
    else
        (*__errno_location ()) = 97;
    return 0;
}

variable_types:
protolen
unsigned long

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


decompiled_code:
__int64 __fastcall bsock_addrinfo_split_str(char **a1, char *a2)
{
char *v2; // rax
char *v3; // rax
char *v4; // rax
char *v5; // rax
char *v6; // rax

v2 = strtok(a2, " ");
*a1 = v2;
if ( v2 )
{
v3 = strtok(0LL, " ");
a1[1] = v3;
if ( v3 )
{
v4 = strtok(0LL, " ");
a1[2] = v4;
if ( v4 )
{
v5 = strtok(0LL, " ");
a1[3] = v5;
if ( v5 )
{
v6 = strtok(0LL, " \n");
a1[4] = v6;
if ( v6 )
{
if ( !strtok(0LL, " \n") )
return 1LL;
}
}
}
}
}
*_errno_location() = 22;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func8(char **a1, char *a2)
{
char *v2; // rax
char *v3; // rax
char *v4; // rax
char *v5; // rax
char *v6; // rax

v2 = strtok(a2, " ");
*a1 = v2;
if ( v2 )
{
v3 = strtok(0LL, " ");
a1[1] = v3;
if ( v3 )
{
v4 = strtok(0LL, " ");
a1[2] = v4;
if ( v4 )
{
v5 = strtok(0LL, " ");
a1[3] = v5;
if ( v5 )
{
v6 = strtok(0LL, " \n");
a1[4] = v6;
if ( v6 )
{
if ( !strtok(0LL, " \n") )
return 1LL;
}
}
}
}
}
*_errno_location() = 22;
return 0LL;
}

original_code:
__attribute__((__nonnull__ ))
_Bool
bsock_addrinfo_split_str(struct bsock_addrinfo_strs * const aistr,
                         char * const restrict str)
{
    return ( ((void *)0) != (aistr->family = strtok(str, " " ))
            && ((void *)0) != (aistr->socktype = strtok(((void *)0), " " ))
            && ((void *)0) != (aistr->protocol = strtok(((void *)0), " " ))
            && ((void *)0) != (aistr->service = strtok(((void *)0), " " ))
            && ((void *)0) != (aistr->addr = strtok(((void *)0), " \n"))
            && ((void *)0) == ( strtok(((void *)0), " \n"))
           ) || ((*__errno_location ()) = 22, 0);
}

canonical_original_code:
_Bool bsock_addrinfo_split_str(struct bsock_addrinfo_strs *aistr, char *str)
{
    return ( ((void *)0) != (aistr->family = strtok(str, " " ))
            && ((void *)0) != (aistr->socktype = strtok(((void *)0), " " ))
            && ((void *)0) != (aistr->protocol = strtok(((void *)0), " " ))
            && ((void *)0) != (aistr->service = strtok(((void *)0), " " ))
            && ((void *)0) != (aistr->addr = strtok(((void *)0), " \n"))
            && ((void *)0) == ( strtok(((void *)0), " \n"))
           ) || ((*__errno_location ()) = 22, 0);
}

variable_types:
str
char *


original_code:
__attribute__((__nonnull__ (2,3)))
_Bool
bsock_addrinfo_recv_ex (const int fd,
                        struct addrinfo * const restrict ai,
                        int * const restrict rfd,
                        char * const restrict ctrlbuf,
                        const size_t ctrlbuf_sz)
{
    uint64_t protover = 0;
    struct iovec iov[] = {
      { .iov_base = &protover, .iov_len = sizeof(protover) },
      { .iov_base = ai, .iov_len = sizeof(struct addrinfo) },
      { .iov_base = ai->ai_addr, .iov_len = ai->ai_addrlen }
    };
    unsigned int nrfds = 1;
    ssize_t r = (ctrlbuf != ((void *)0))
      ? bsock_unix_recv_fds_ex(fd, rfd, &nrfds, iov,
                               sizeof(iov)/sizeof(struct iovec),
                               ctrlbuf, ctrlbuf_sz)
      : bsock_unix_recv_fds(fd, rfd, &nrfds, iov,
                            sizeof(iov)/sizeof(struct iovec));
    if (r <= 0)
        return 0;
    if (r < (ssize_t)sizeof(protover))
        return 0;
    if (0 == protover) {
        if (r >= (ssize_t)(sizeof(protover)+sizeof(struct addrinfo))
            && ai->ai_addrlen > 0
            && r == (ssize_t)
                    (sizeof(protover)+sizeof(struct addrinfo)+ai->ai_addrlen)) {
            ai->ai_addr = iov[2].iov_base;
            ai->ai_canonname = ((void *)0);
            ai->ai_next = ((void *)0);
            return 1;
        }
        return 0;
    }
    else if ('F' == ((char *)&protover)[1] && '_' == ((char *)&protover)[2]) {
        struct bsock_addrinfo_strs aistr;
        char line[256];
        if (r >= (ssize_t)sizeof(line)) return 0;
        memcpy(line, &protover, sizeof(protover));
        memcpy(line + sizeof(protover), ai, sizeof(struct addrinfo));
        line[r] = '\0';
        if ((r -= (ssize_t)(sizeof(protover) + sizeof(struct addrinfo))) > 0)
            memcpy(line + sizeof(protover) + sizeof(struct addrinfo),
                   iov[2].iov_base, (size_t)r);
        ai->ai_addrlen = iov[2].iov_len;
        ai->ai_addr = (struct sockaddr *)iov[2].iov_base;
        return bsock_addrinfo_split_str(&aistr, line)
          ? bsock_addrinfo_from_strs(ai, &aistr)
          : 0;
    }
    return 0;
}

canonical_original_code:
_Bool bsock_addrinfo_recv_ex(int fd, struct addrinfo *ai, int *rfd, char *ctrlbuf, unsigned long ctrlbuf_sz)
{
    unsigned long protover = 0;
    struct iovec iov[] = {
      { .iov_base = &protover, .iov_len = sizeof(protover) },
      { .iov_base = ai, .iov_len = sizeof(struct addrinfo) },
      { .iov_base = ai->ai_addr, .iov_len = ai->ai_addrlen }
    };
    unsigned int nrfds = 1;
    long r = (ctrlbuf != ((void *)0))
      ? bsock_unix_recv_fds_ex(fd, rfd, &nrfds, iov,
                               sizeof(iov)/sizeof(struct iovec),
                               ctrlbuf, ctrlbuf_sz)
      : bsock_unix_recv_fds(fd, rfd, &nrfds, iov,
                            sizeof(iov)/sizeof(struct iovec));
    if (r <= 0)
        return 0;
    if (r < (long)sizeof(protover))
        return 0;
    if (0 == protover) {
        if (r >= (long)(sizeof(protover)+sizeof(struct addrinfo))
            && ai->ai_addrlen > 0
            && r == (long)
                    (sizeof(protover)+sizeof(struct addrinfo)+ai->ai_addrlen)) {
            ai->ai_addr = iov[2].iov_base;
            ai->ai_canonname = ((void *)0);
            ai->ai_next = ((void *)0);
            return 1;
        }
        return 0;
    }
    else if ('F' == ((char *)&protover)[1] && '_' == ((char *)&protover)[2]) {
        struct bsock_addrinfo_strs aistr;
        char line[256];
        if (r >= (long)sizeof(line)) return 0;
        memcpy(line, &protover, sizeof(protover));
        memcpy(line + sizeof(protover), ai, sizeof(struct addrinfo));
        line[r] = '\0';
        if ((r -= (long)(sizeof(protover) + sizeof(struct addrinfo))) > 0)
            memcpy(line + sizeof(protover) + sizeof(struct addrinfo),
                   iov[2].iov_base, (unsigned long)r);
        ai->ai_addrlen = iov[2].iov_len;
        ai->ai_addr = (struct sockaddr *)iov[2].iov_base;
        return bsock_addrinfo_split_str(&aistr, line)
          ? bsock_addrinfo_from_strs(ai, &aistr)
          : 0;
    }
    return 0;
}

variable_types: fd int

variable_types:
rfd
int *

variable_types:
ctrlbuf
char *

variable_types:
protover
unsigned long

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

variable_types:
nrfds
unsigned int

variable_types: r long

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

variable_types:
line
char [256]


original_code:
__attribute__((__nonnull__ ))
_Bool
bsock_addrinfo_send (const int fd,
                     const struct addrinfo * const restrict ai, const int sfd)
{
    uint64_t protover = 0;
    struct iovec iov[] = {
      { .iov_base = &protover, .iov_len = sizeof(protover) },
      { .iov_base = (void *)(uintptr_t)ai, .iov_len = sizeof(struct addrinfo) },
      { .iov_base = ai->ai_addr, .iov_len = ai->ai_addrlen }
    };
    ssize_t w = bsock_unix_send_fds(fd, &sfd, (sfd >= 0), iov,
                                    sizeof(iov)/sizeof(struct iovec));
    return w == (ssize_t)
                (sizeof(protover) + sizeof(struct addrinfo) + ai->ai_addrlen);
}

canonical_original_code:
_Bool bsock_addrinfo_send(int fd, struct addrinfo *ai, int sfd)
{
    unsigned long protover = 0;
    struct iovec iov[] = {
      { .iov_base = &protover, .iov_len = sizeof(protover) },
      { .iov_base = (void *)(uintptr_t)ai, .iov_len = sizeof(struct addrinfo) },
      { .iov_base = ai->ai_addr, .iov_len = ai->ai_addrlen }
    };
    long w = bsock_unix_send_fds(fd, &sfd, (sfd >= 0), iov,
                                    sizeof(iov)/sizeof(struct iovec));
    return w == (long)
                (sizeof(protover) + sizeof(struct addrinfo) + ai->ai_addrlen);
}

variable_types: sfd int

variable_types: w long

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


decompiled_code:
__int64 __fastcall bsock_bindresvport_skip(int a1)
{
int v1; // edx
__int64 v2; // rax

v1 = 623;
v2 = 0LL;
while ( a1 != v1 )
{
if ( ++v2 == 12 )
return 0LL;
v1 = skiplist_0[v2];
}
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
int v1; // edx
__int64 v2; // rax

v1 = 623;
v2 = 0LL;
while ( a1 != v1 )
{
if ( ++v2 == 12 )
return 0LL;
v1 = skiplist_0[v2];
}
return 1LL;
}

original_code:
static int
bsock_bindresvport_skip (const unsigned int port)
{
    static const unsigned int skiplist[] = { 623,631,636,664,749,750,783,873,992,993,994,995 };
    size_t i;
    for (i = 0; i < sizeof(skiplist)/sizeof(int) && skiplist[i] != port; ++i) ;
    return (i != sizeof(skiplist)/sizeof(int));
}

canonical_original_code:
static int bsock_bindresvport_skip(unsigned int port)
{
    static unsigned int skiplist[] = { 623,631,636,664,749,750,783,873,992,993,994,995 };
    unsigned long i;
    for (i = 0; i < sizeof(skiplist)/sizeof(int) && skiplist[i] != port; ++i) ;
    return (i != sizeof(skiplist)/sizeof(int));
}

variable_types:
port
unsigned int

variable_types:
skiplist
unsigned int []

variable_types:
i
unsigned long


decompiled_code:
__int64 bsock_bindresvport_random_port()
{
__pid_t v0; // ebx
int v1; // eax
int v3; // edi
int buf[3]; // [rsp+Ch] [rbp-Ch] BYREF

if ( fd_1 != -1 )
{
if ( read(fd_1, buf, 4uLL) == 4 )
goto LABEL_7;
v3 = fd_1;
if ( fd_1 != -1 )
{
while ( close(v3) && *_errno_location() == 4 )
v3 = fd_1;
}
}
fd_1 = open64("/dev/urandom", 2048);
if ( fd_1 == -1 || read(fd_1, buf, 4uLL) != 4 )
{
buf[0] = -1;
goto LABEL_5;
}
LABEL_7:
if ( buf[0] != -1 )
{
v1 = buf[0] ^ (((unsigned int)buf[0] >> 28) | (buf[0] >> 20) & 0xF0 | (buf[0] >> 12) & 0xF00 | (buf[0] >> 4) & 0xF000);
return (unsigned int)(v1 % 424 + 600);
}
LABEL_5:
v0 = getpid();
v1 = v0 ^ time(0LL);
return (unsigned int)(v1 % 424 + 600);
}

original_code:
static int
bsock_bindresvport_random_port (void)
{
    static int fd = -1;
    int r;
    if (-1 == fd || read(fd, &r, sizeof(int)) != sizeof(int)) {
        if (-1 != fd)
            do {} while (__builtin_expect((0 != close(fd)),0) && __builtin_expect((*__errno_location ()) == 4, 1));
        fd = open("/dev/urandom", 00|04000);
        if (-1 == fd || read(fd, &r, sizeof(int)) != sizeof(int))
            r = -1;
    }
    if (-1 != r)
        r ^= (((r>>4)&0xF000)|((r>>12)&0xF00)|((r>>20)&0xF0)|((r>>28)&0xF));
    else
        r = (int)(getpid() ^ time(((void *)0)));
    return(600 + (r % (IPPORT_RESERVED-600)));
}

canonical_original_code:
static int bsock_bindresvport_random_port()
{
    static int fd = -1;
    int r;
    if (-1 == fd || read(fd, &r, sizeof(int)) != sizeof(int)) {
        if (-1 != fd)
            do {} while (__builtin_expect((0 != close(fd)),0) && __builtin_expect((*__errno_location ()) == 4, 1));
        fd = open("/dev/urandom", 00|04000);
        if (-1 == fd || read(fd, &r, sizeof(int)) != sizeof(int))
            r = -1;
    }
    if (-1 != r)
        r ^= (((r>>4)&0xF000)|((r>>12)&0xF00)|((r>>20)&0xF0)|((r>>28)&0xF));
    else
        r = (int)(getpid() ^ time(((void *)0)));
    return(600 + (r % (IPPORT_RESERVED-600)));
}

variable_types: r int