#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct sockaddr_storage {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct addrinfo {
  scalar_t__ ai_socktype;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ serv;
typedef int /*<<< orphan*/ sa;
typedef int /*<<< orphan*/ optval;
typedef int /*<<< orphan*/ host;

/* Variables and functions */
int /*<<< orphan*/ AI_PASSIVE;
int FD_SETSIZE;
int NI_MAXHOST;
int NI_MAXSERV;
int NI_NUMERICHOST;
int NI_NUMERICSERV;
scalar_t__ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVBUF;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *, ...);
int errno;
int /*<<< orphan*/ errx(int, char *, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int getnameinfo(struct sockaddr *, int, char *, int, char *, int, int);
int getsockname(int, struct sockaddr *, int *);
int listen(int, int);
int /*<<< orphan*/ printf(char *, char *, char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, scalar_t__, int /*<<< orphan*/);
int socket_listen(int *ls, struct addrinfo *hints, const char *listenaddr,
                  const char *listenport) {
  char host[NI_MAXHOST], serv[NI_MAXSERV];
  struct sockaddr_storage sa;
  socklen_t salen;
  struct addrinfo *res, *res0;
  const char *cause = NULL;
  int optval, error, save_errno, nls;
  hints->ai_flags = AI_PASSIVE;
  error = getaddrinfo(listenaddr, listenport, hints, &res0);
  if (error)
    errx(1, "getaddrinfo %s %s: %s", listenaddr == NULL ? "*" : listenaddr,
         listenport, gai_strerror(error));
  for (res = res0, nls = 0; res && nls < FD_SETSIZE; res = res->ai_next) {
    ls[nls] = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (ls[nls] == -1) {
      cause = "listen socket";
      continue;
    }
    optval = 100000;
    if (setsockopt(ls[nls], SOL_SOCKET, SO_RCVBUF, &optval, sizeof(optval)) ==
        -1)
      err(1, "setsockopt rcvbuf");
    optval = 1;
    if (setsockopt(ls[nls], SOL_SOCKET, SO_REUSEADDR, &optval,
                   sizeof(optval)) == -1)
      err(1, "setsockopt reuseaddr");
    if (bind(ls[nls], res->ai_addr, res->ai_addrlen) == -1) {
      cause = "bind";
      save_errno = errno;
      close(ls[nls]);
      errno = save_errno;
      continue;
    }
    if (hints->ai_socktype == SOCK_STREAM) {
      if (listen(ls[nls], 5) == -1)
        err(1, "listen");
    }
    salen = sizeof(sa);
    if (getsockname(ls[nls], (struct sockaddr *)&sa, &salen) == -1)
      err(1, "listen getsockname");
    error = getnameinfo((struct sockaddr *)&sa, salen, host, sizeof(host), serv,
                        sizeof(serv), NI_NUMERICHOST | NI_NUMERICSERV);
    if (error)
      errx(1, "listen getnameinfo: %s", gai_strerror(error));
    printf("listen %s %s\n", host, serv);
    nls++;
  }
  if (nls == 0)
    err(1, "%s", cause);
  freeaddrinfo(res0);
  return nls;
}


x86 assembly (clang, -O0):
<socket_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x90(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x88(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
mov    0x2da8(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x80(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x78(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x60(%rbp)
mov    0x2d7f(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x20(%rax)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   10d0 <getaddrinfo@plt>
mov    %eax,-0x68(%rbp)
cmpl   $0x0,-0x68(%rbp)
je     12f2 <socket_listen+0x122>
cmpq   $0x0,-0x18(%rbp)
jne    12a2 <socket_listen+0xd2>
lea    0xd80(%rip),%rax
mov    %rax,-0x98(%rbp)
jmp    12ad <socket_listen+0xdd>
mov    -0x18(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    -0x98(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0xa0(%rbp)
mov    -0x68(%rbp),%edi
call   1060 <gai_strerror@plt>
mov    -0xa8(%rbp),%rdx
mov    -0xa0(%rbp),%rcx
mov    %eax,%r8d
mov    $0x1,%edi
lea    0xd15(%rip),%rsi
mov    $0x0,%al
call   1040 <errx@plt>
mov    -0x58(%rbp),%rax
mov    %rax,-0x50(%rbp)
movl   $0x0,-0x70(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x50(%rbp)
mov    %al,-0xa9(%rbp)
je     1329 <socket_listen+0x159>
mov    -0x70(%rbp),%eax
mov    0x2c8a(%rip),%rcx
cmp    (%rcx),%eax
setl   %al
mov    %al,-0xa9(%rbp)
mov    -0xa9(%rbp),%al
test   $0x1,%al
jne    133c <socket_listen+0x16c>
jmp    15a9 <socket_listen+0x3d9>
mov    -0x50(%rbp),%rax
mov    0x14(%rax),%edi
mov    -0x50(%rbp),%rax
mov    (%rax),%rsi
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edx
call   10f0 <socket@plt>
mov    %eax,%edx
mov    -0x8(%rbp),%rax
movslq -0x70(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x8(%rbp),%rax
movslq -0x70(%rbp),%rcx
cmpl   $0xffffffff,(%rax,%rcx,4)
jne    1385 <socket_listen+0x1b5>
lea    0xc9c(%rip),%rax
mov    %rax,-0x60(%rbp)
jmp    1598 <socket_listen+0x3c8>
movl   $0x186a0,-0x64(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x70(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2c42(%rip),%rax
mov    (%rax),%esi
mov    0x2c19(%rip),%rax
mov    (%rax),%edx
lea    -0x64(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    13d4 <socket_listen+0x204>
mov    $0x1,%edi
lea    0xc59(%rip),%rsi
mov    $0x0,%al
call   1090 <err@plt>
movl   $0x1,-0x64(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x70(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2bf3(%rip),%rax
mov    (%rax),%esi
mov    0x2ba2(%rip),%rax
mov    (%rax),%edx
lea    -0x64(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1423 <socket_listen+0x253>
mov    $0x1,%edi
lea    0xc1c(%rip),%rsi
mov    $0x0,%al
call   1090 <err@plt>
mov    -0x8(%rbp),%rax
movslq -0x70(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1482 <socket_listen+0x2b2>
lea    0xbfc(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    0x2b44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x70(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
call   1070 <close@plt>
mov    -0x6c(%rbp),%ecx
mov    0x2b25(%rip),%rax
mov    %ecx,(%rax)
jmp    1598 <socket_listen+0x3c8>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2b28(%rip),%rcx
cmp    (%rcx),%rax
jne    14cf <socket_listen+0x2ff>
mov    -0x8(%rbp),%rax
movslq -0x70(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    14ca <socket_listen+0x2fa>
mov    $0x1,%edi
lea    0xb8f(%rip),%rsi
mov    $0x0,%al
call   1090 <err@plt>
jmp    14cf <socket_listen+0x2ff>
movl   $0x4,-0x44(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x70(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
lea    -0x40(%rbp),%rsi
lea    -0x44(%rbp),%rdx
call   10c0 <getsockname@plt>
cmp    $0xffffffff,%eax
jne    150a <socket_listen+0x33a>
mov    $0x1,%edi
lea    0xb56(%rip),%rsi
mov    $0x0,%al
call   1090 <err@plt>
mov    -0x78(%rbp),%r8
mov    -0x88(%rbp),%rdx
mov    -0x80(%rbp),%rax
mov    -0x90(%rbp),%rcx
lea    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%esi
mov    %eax,%r9d
mov    0x2a7f(%rip),%rax
mov    (%rax),%eax
mov    0x2aae(%rip),%r10
or     (%r10),%eax
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1080 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,-0x68(%rbp)
cmpl   $0x0,-0x68(%rbp)
je     1578 <socket_listen+0x3a8>
mov    -0x68(%rbp),%edi
call   1060 <gai_strerror@plt>
movslq %eax,%rdx
mov    $0x1,%edi
lea    0xafb(%rip),%rsi
mov    $0x0,%al
call   1040 <errx@plt>
mov    -0x78(%rbp),%rdx
mov    -0x88(%rbp),%rsi
lea    0xaf9(%rip),%rdi
call   1050 <printf@plt>
mov    -0x70(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x70(%rbp)
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1301 <socket_listen+0x131>
cmpl   $0x0,-0x70(%rbp)
jne    15ca <socket_listen+0x3fa>
mov    -0x60(%rbp),%rdx
mov    $0x1,%edi
lea    0xabd(%rip),%rsi
mov    $0x0,%al
call   1090 <err@plt>
mov    -0x58(%rbp),%rdi
call   10e0 <freeaddrinfo@plt>
mov    -0x70(%rbp),%eax
mov    -0x28(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_listen>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rcx,%rbx
mov    %rdx,%r13
mov    %rsi,%rdx
mov    %rdi,-0x30(%rbp)
mov    0x2d9b(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rax,-0x70(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
mov    %rax,-0x68(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    0x2da3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsi)
lea    -0x60(%rbp),%rcx
mov    %r13,%rdi
mov    %rbx,%rsi
mov    %rdx,-0x78(%rbp)
call   10d0 <getaddrinfo@plt>
test   %eax,%eax
mov    %r14,-0x58(%rbp)
je     1286 <socket_listen+0xb6>
test   %r13,%r13
lea    0xdb7(%rip),%r14
cmovne %r13,%r14
mov    %eax,%edi
call   1060 <gai_strerror@plt>
lea    0xd8f(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
mov    %rbx,%rcx
mov    %eax,%r8d
xor    %eax,%eax
call   1040 <errx@plt>
mov    -0x60(%rbp),%r13
test   %r13,%r13
je     14da <socket_listen+0x30a>
mov    0x2d0e(%rip),%rax
cmpl   $0x0,(%rax)
mov    $0x0,%edx
jle    14dc <socket_listen+0x30c>
lea    -0x38(%rbp),%r14
xor    %ebx,%ebx
xor    %eax,%eax
mov    %rax,-0x48(%rbp)
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x14(%r13),%edi
mov    0x0(%r13),%rsi
mov    0x10(%r13),%edx
call   10f0 <socket@plt>
movslq %ebx,%r12
mov    -0x30(%rbp),%rcx
mov    %eax,(%rcx,%r12,4)
cmp    $0xffffffff,%eax
je     1480 <socket_listen+0x2b0>
mov    %ebx,-0x34(%rbp)
movl   $0x186a0,-0x38(%rbp)
mov    0x2cea(%rip),%r15
mov    (%r15),%esi
mov    0x2cc0(%rip),%rcx
mov    (%rcx),%edx
mov    %eax,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    132a <socket_listen+0x15a>
mov    $0x1,%edi
lea    0xd03(%rip),%rsi
xor    %eax,%eax
call   1090 <err@plt>
movl   $0x1,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax,%r12,4),%edi
mov    (%r15),%esi
mov    0x2c55(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    136b <socket_listen+0x19b>
mov    $0x1,%edi
lea    0xcd4(%rip),%rsi
xor    %eax,%eax
call   1090 <err@plt>
mov    -0x30(%rbp),%rax
mov    (%rax,%r12,4),%edi
mov    0x8(%r13),%edx
mov    0xc(%r13),%esi
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
lea    -0x80(%rbp),%r15
je     1490 <socket_listen+0x2c0>
mov    -0x78(%rbp),%rax
mov    (%rax),%rax
mov    0x2c1d(%rip),%rcx
cmp    (%rcx),%rax
jne    13d0 <socket_listen+0x200>
mov    -0x30(%rbp),%rax
mov    (%rax,%r12,4),%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    13d0 <socket_listen+0x200>
mov    $0x1,%edi
lea    0xc8f(%rip),%rsi
xor    %eax,%eax
call   1090 <err@plt>
nopw   0x0(%rax,%rax,1)
movl   $0x4,-0x3c(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax,%r12,4),%edi
mov    %r15,%rsi
lea    -0x3c(%rbp),%rdx
call   10c0 <getsockname@plt>
cmp    $0xffffffff,%eax
mov    -0x34(%rbp),%ebx
jne    1406 <socket_listen+0x236>
mov    $0x1,%edi
lea    0xc5a(%rip),%rsi
xor    %eax,%eax
call   1090 <err@plt>
mov    -0x3c(%rbp),%esi
mov    0x2bd8(%rip),%rax
mov    (%rax),%eax
mov    0x2b97(%rip),%rcx
or     (%rcx),%eax
sub    $0x8,%rsp
mov    %r15,%rdi
mov    -0x50(%rbp),%rdx
mov    -0x70(%rbp),%rcx
mov    -0x58(%rbp),%r8
mov    -0x68(%rbp),%r9
push   %rax
call   1080 <getnameinfo@plt>
add    $0x10,%rsp
test   %eax,%eax
je     145d <socket_listen+0x28d>
mov    %eax,%edi
call   1060 <gai_strerror@plt>
movslq %eax,%rdx
mov    $0x1,%edi
lea    0xc16(%rip),%rsi
xor    %eax,%eax
call   1040 <errx@plt>
lea    0xc1f(%rip),%rdi
mov    -0x50(%rbp),%rsi
mov    -0x58(%rbp),%rdx
call   1050 <printf@plt>
add    $0x1,%ebx
jmp    14b8 <socket_listen+0x2e8>
cs nopw 0x0(%rax,%rax,1)
lea    0xb91(%rip),%rax
mov    %rax,-0x48(%rbp)
jmp    14b8 <socket_listen+0x2e8>
nopl   (%rax)
mov    0x2b09(%rip),%rbx
mov    (%rbx),%r15d
mov    -0x30(%rbp),%rax
mov    (%rax,%r12,4),%edi
call   1070 <close@plt>
mov    %r15d,(%rbx)
lea    0xb9c(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    -0x34(%rbp),%ebx
mov    0x18(%r13),%r13
test   %r13,%r13
je     14d0 <socket_listen+0x300>
mov    0x2ae0(%rip),%rax
cmp    (%rax),%ebx
jl     12c0 <socket_listen+0xf0>
test   %ebx,%ebx
mov    -0x48(%rbp),%rdx
jne    14f1 <socket_listen+0x321>
jmp    14dc <socket_listen+0x30c>
xor    %edx,%edx
lea    0xb9d(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1090 <err@plt>
xor    %ebx,%ebx
mov    -0x60(%rbp),%rdi
call   10e0 <freeaddrinfo@plt>
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i64, i32, i32, i32, i32, %struct.addrinfo*, i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@AI_PASSIVE = global i32 0
@.str = [22 x i8] c"getaddrinfo %s %s: %s\00"
@.str.1 = [2 x i8] c"*\00"
@FD_SETSIZE = global i32 0
@.str.2 = [14 x i8] c"listen socket\00"
@SOL_SOCKET = global i32 0
@SO_RCVBUF = global i32 0
@.str.3 = [18 x i8] c"setsockopt rcvbuf\00"
@SO_REUSEADDR = global i32 0
@.str.4 = [21 x i8] c"setsockopt reuseaddr\00"
@.str.5 = [5 x i8] c"bind\00"
@errno = global i32 0
@SOCK_STREAM = global i64 0
@.str.6 = [7 x i8] c"listen\00"
@.str.7 = [19 x i8] c"listen getsockname\00"
@NI_NUMERICHOST = global i32 0
@NI_NUMERICSERV = global i32 0
@.str.8 = [23 x i8] c"listen getnameinfo: %s\00"
@.str.9 = [14 x i8] c"listen %s %s\0A\00"
@.str.10 = [3 x i8] c"%s\00"
define i32 @socket_listen(i32* %0, %struct.addrinfo* %1, i8* %2, i8* %3) {
%5 = alloca i32*
%6 = alloca %struct.addrinfo*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca %struct.sockaddr_storage
%13 = alloca i32
%14 = alloca %struct.addrinfo*
%15 = alloca %struct.addrinfo*
%16 = alloca i8*
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
store i32* %0, i32** %5
store %struct.addrinfo* %1, %struct.addrinfo** %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%21 = load i32, i32* @NI_MAXHOST
%22 = zext i32 %21 to i64
%23 = call i8* @llvm.stacksave()
store i8* %23, i8** %9
%24 = alloca i8, i64 %22
store i64 %22, i64* %10
%25 = load i32, i32* @NI_MAXSERV
%26 = zext i32 %25 to i64
%27 = alloca i8, i64 %26
store i64 %26, i64* %11
store i8* null, i8** %16
%28 = load i32, i32* @AI_PASSIVE
%29 = load %struct.addrinfo*, %struct.addrinfo** %6
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 6
store i32 %28, i32* %30
%31 = load i8*, i8** %7
%32 = load i8*, i8** %8
%33 = load %struct.addrinfo*, %struct.addrinfo** %6
%34 = call i32 @getaddrinfo(i8* %31, i8* %32, %struct.addrinfo* %33, %struct.addrinfo** %15)
store i32 %34, i32* %18
%35 = load i32, i32* %18
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %49
37: ; preds = %4
%38 = load i8*, i8** %7
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %41
40: ; preds = %37
br label %43
41: ; preds = %37
%42 = load i8*, i8** %7
br label %43
43: ; preds = %41, %40
%44 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %40 ], [ %42, %41 ]
%45 = load i8*, i8** %8
%46 = load i32, i32* %18
%47 = call i32 @gai_strerror(i32 %46)
%48 = call i32 (i32, i8*, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %44, i8* %45, i32 %47)
br label %49
49: ; preds = %43, %4
%50 = load %struct.addrinfo*, %struct.addrinfo** %15
store %struct.addrinfo* %50, %struct.addrinfo** %14
store i32 0, i32* %20
br label %51
51: ; preds = %178, %49
%52 = load %struct.addrinfo*, %struct.addrinfo** %14
%53 = icmp ne %struct.addrinfo* %52, null
br i1 %53, label %54, label %58
54: ; preds = %51
%55 = load i32, i32* %20
%56 = load i32, i32* @FD_SETSIZE
%57 = icmp slt i32 %55, %56
br label %58
58: ; preds = %54, %51
%59 = phi i1 [ false, %51 ], [ %57, %54 ]
br i1 %59, label %60, label %182
60: ; preds = %58
%61 = load %struct.addrinfo*, %struct.addrinfo** %14
%62 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %61, i32 0, i32 4
%63 = load i32, i32* %62
%64 = load %struct.addrinfo*, %struct.addrinfo** %14
%65 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %64, i32 0, i32 0
%66 = load i64, i64* %65
%67 = load %struct.addrinfo*, %struct.addrinfo** %14
%68 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %67, i32 0, i32 3
%69 = load i32, i32* %68
%70 = call i32 @socket(i32 %63, i64 %66, i32 %69)
%71 = load i32*, i32** %5
%72 = load i32, i32* %20
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i32, i32* %71, i64 %73
store i32 %70, i32* %74
%75 = load i32*, i32** %5
%76 = load i32, i32* %20
%77 = sext i32 %76 to i64
%78 = getelementptr inbounds i32, i32* %75, i64 %77
%79 = load i32, i32* %78
%80 = icmp eq i32 %79, -1
br i1 %80, label %81, label %82
81: ; preds = %60
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8** %16
br label %178
82: ; preds = %60
store i32 100000, i32* %17
%83 = load i32*, i32** %5
%84 = load i32, i32* %20
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i32, i32* %83, i64 %85
%87 = load i32, i32* %86
%88 = load i32, i32* @SOL_SOCKET
%89 = load i32, i32* @SO_RCVBUF
%90 = call i32 @setsockopt(i32 %87, i32 %88, i32 %89, i32* %17, i32 4)
%91 = icmp eq i32 %90, -1
br i1 %91, label %92, label %94
92: ; preds = %82
%93 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %94
94: ; preds = %92, %82
store i32 1, i32* %17
%95 = load i32*, i32** %5
%96 = load i32, i32* %20
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds i32, i32* %95, i64 %97
%99 = load i32, i32* %98
%100 = load i32, i32* @SOL_SOCKET
%101 = load i32, i32* @SO_REUSEADDR
%102 = call i32 @setsockopt(i32 %99, i32 %100, i32 %101, i32* %17, i32 4)
%103 = icmp eq i32 %102, -1
br i1 %103, label %104, label %106
104: ; preds = %94
%105 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %106
106: ; preds = %104, %94
%107 = load i32*, i32** %5
%108 = load i32, i32* %20
%109 = sext i32 %108 to i64
%110 = getelementptr inbounds i32, i32* %107, i64 %109
%111 = load i32, i32* %110
%112 = load %struct.addrinfo*, %struct.addrinfo** %14
%113 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %112, i32 0, i32 2
%114 = load i32, i32* %113
%115 = load %struct.addrinfo*, %struct.addrinfo** %14
%116 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %115, i32 0, i32 1
%117 = load i32, i32* %116
%118 = call i32 @bind(i32 %111, i32 %114, i32 %117)
%119 = icmp eq i32 %118, -1
br i1 %119, label %120, label %129
120: ; preds = %106
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i8** %16
%121 = load i32, i32* @errno
store i32 %121, i32* %19
%122 = load i32*, i32** %5
%123 = load i32, i32* %20
%124 = sext i32 %123 to i64
%125 = getelementptr inbounds i32, i32* %122, i64 %124
%126 = load i32, i32* %125
%127 = call i32 @close(i32 %126)
%128 = load i32, i32* %19
store i32 %128, i32* @errno
br label %178
129: ; preds = %106
%130 = load %struct.addrinfo*, %struct.addrinfo** %6
%131 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %130, i32 0, i32 0
%132 = load i64, i64* %131
%133 = load i64, i64* @SOCK_STREAM
%134 = icmp eq i64 %132, %133
br i1 %134, label %135, label %146
135: ; preds = %129
%136 = load i32*, i32** %5
%137 = load i32, i32* %20
%138 = sext i32 %137 to i64
%139 = getelementptr inbounds i32, i32* %136, i64 %138
%140 = load i32, i32* %139
%141 = call i32 @listen(i32 %140, i32 5)
%142 = icmp eq i32 %141, -1
br i1 %142, label %143, label %145
143: ; preds = %135
%144 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %145
145: ; preds = %143, %135
br label %146
146: ; preds = %145, %129
store i32 4, i32* %13
%147 = load i32*, i32** %5
%148 = load i32, i32* %20
%149 = sext i32 %148 to i64
%150 = getelementptr inbounds i32, i32* %147, i64 %149
%151 = load i32, i32* %150
%152 = bitcast %struct.sockaddr_storage* %12 to %struct.sockaddr*
%153 = call i32 @getsockname(i32 %151, %struct.sockaddr* %152, i32* %13)
%154 = icmp eq i32 %153, -1
br i1 %154, label %155, label %157
155: ; preds = %146
%156 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
br label %157
157: ; preds = %155, %146
%158 = bitcast %struct.sockaddr_storage* %12 to %struct.sockaddr*
%159 = load i32, i32* %13
%160 = trunc i64 %22 to i32
%161 = trunc i64 %26 to i32
%162 = load i32, i32* @NI_NUMERICHOST
%163 = load i32, i32* @NI_NUMERICSERV
%164 = or i32 %162, %163
%165 = call i32 @getnameinfo(%struct.sockaddr* %158, i32 %159, i8* %24, i32 %160, i8* %27, i32 %161, i32 %164)
store i32 %165, i32* %18
%166 = load i32, i32* %18
%167 = icmp ne i32 %166, 0
br i1 %167, label %168, label %174
168: ; preds = %157
%169 = load i32, i32* %18
%170 = call i32 @gai_strerror(i32 %169)
%171 = sext i32 %170 to i64
%172 = inttoptr i64 %171 to i8*
%173 = call i32 (i32, i8*, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0), i8* %172)
br label %174
174: ; preds = %168, %157
%175 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i64 0, i64 0), i8* %24, i8* %27)
%176 = load i32, i32* %20
%177 = add nsw i32 %176, 1
store i32 %177, i32* %20
br label %178
178: ; preds = %174, %120, %81
%179 = load %struct.addrinfo*, %struct.addrinfo** %14
%180 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %179, i32 0, i32 5
%181 = load %struct.addrinfo*, %struct.addrinfo** %180
store %struct.addrinfo* %181, %struct.addrinfo** %14
br label %51
182: ; preds = %58
%183 = load i32, i32* %20
%184 = icmp eq i32 %183, 0
br i1 %184, label %185, label %188
185: ; preds = %182
%186 = load i8*, i8** %16
%187 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0), i8* %186)
br label %188
188: ; preds = %185, %182
%189 = load %struct.addrinfo*, %struct.addrinfo** %15
%190 = call i32 @freeaddrinfo(%struct.addrinfo* %189)
%191 = load i32, i32* %20
%192 = load i8*, i8** %9
ret i32 %191
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, i32, i32, i32, %struct.addrinfo*, i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@NI_MAXHOST = global i32 0
@NI_MAXSERV = global i32 0
@AI_PASSIVE = global i32 0
@.str = [22 x i8] c"getaddrinfo %s %s: %s\00"
@.str.1 = [2 x i8] c"*\00"
@FD_SETSIZE = global i32 0
@.str.2 = [14 x i8] c"listen socket\00"
@SOL_SOCKET = global i32 0
@SO_RCVBUF = global i32 0
@.str.3 = [18 x i8] c"setsockopt rcvbuf\00"
@SO_REUSEADDR = global i32 0
@.str.4 = [21 x i8] c"setsockopt reuseaddr\00"
@.str.5 = [5 x i8] c"bind\00"
@errno = global i32 0
@SOCK_STREAM = global i64 0
@.str.6 = [7 x i8] c"listen\00"
@.str.7 = [19 x i8] c"listen getsockname\00"
@NI_NUMERICHOST = global i32 0
@NI_NUMERICSERV = global i32 0
@.str.8 = [23 x i8] c"listen getnameinfo: %s\00"
@.str.9 = [14 x i8] c"listen %s %s\0A\00"
@.str.10 = [3 x i8] c"%s\00"
define i32 @socket_listen(i32* nocapture %0, %struct.addrinfo* %1, i8* %2, i8* %3) {
%5 = alloca %struct.sockaddr_storage
%6 = alloca i32
%7 = alloca %struct.addrinfo*
%8 = alloca i32
%9 = load i32, i32* @NI_MAXHOST
%10 = zext i32 %9 to i64
%11 = alloca i8, i64 %10
%12 = load i32, i32* @NI_MAXSERV
%13 = zext i32 %12 to i64
%14 = alloca i8, i64 %13
%15 = bitcast %struct.sockaddr_storage* %5 to i8*
%16 = bitcast i32* %6 to i8*
%17 = bitcast %struct.addrinfo** %7 to i8*
%18 = bitcast i32* %8 to i8*
%19 = load i32, i32* @AI_PASSIVE
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 6
store i32 %19, i32* %20
%21 = call i32 @getaddrinfo(i8* %2, i8* %3, %struct.addrinfo* %1, %struct.addrinfo** %7)
%22 = icmp eq i32 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %4
%24 = icmp eq i8* %2, null
%25 = select i1 %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %2
%26 = call i32 @gai_strerror(i32 %21)
%27 = call i32 (i32, i8*, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %25, i8* %3, i32 %26)
br label %28
28: ; preds = %23, %4
%29 = load %struct.addrinfo*, %struct.addrinfo** %7
%30 = icmp ne %struct.addrinfo* %29, null
%31 = load i32, i32* @FD_SETSIZE
%32 = icmp sgt i32 %31, 0
%33 = select i1 %30, i1 %32, i1 false
br i1 %33, label %34, label %120
34: ; preds = %28
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 0
%36 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr*
br label %37
37: ; preds = %34, %109
%38 = phi %struct.addrinfo* [ %29, %34 ], [ %113, %109 ]
%39 = phi i32 [ 0, %34 ], [ %111, %109 ]
%40 = phi i8* [ null, %34 ], [ %110, %109 ]
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 4
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 0
%44 = load i64, i64* %43
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 3
%46 = load i32, i32* %45
%47 = call i32 @socket(i32 %42, i64 %44, i32 %46)
%48 = sext i32 %39 to i64
%49 = getelementptr inbounds i32, i32* %0, i64 %48
store i32 %47, i32* %49
%50 = icmp eq i32 %47, -1
br i1 %50, label %109, label %51
51: ; preds = %37
store i32 100000, i32* %8
%52 = load i32, i32* @SOL_SOCKET
%53 = load i32, i32* @SO_RCVBUF
%54 = call i32 @setsockopt(i32 %47, i32 %52, i32 %53, i32* %8, i32 4)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %58
56: ; preds = %51
%57 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %58
58: ; preds = %56, %51
store i32 1, i32* %8
%59 = load i32, i32* %49
%60 = load i32, i32* @SOL_SOCKET
%61 = load i32, i32* @SO_REUSEADDR
%62 = call i32 @setsockopt(i32 %59, i32 %60, i32 %61, i32* %8, i32 4)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %66
64: ; preds = %58
%65 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %66
66: ; preds = %64, %58
%67 = load i32, i32* %49
%68 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 2
%69 = load i32, i32* %68
%70 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 1
%71 = load i32, i32* %70
%72 = call i32 @bind(i32 %67, i32 %69, i32 %71)
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %78
74: ; preds = %66
%75 = load i32, i32* @errno
%76 = load i32, i32* %49
%77 = call i32 @close(i32 %76)
store i32 %75, i32* @errno
br label %109
78: ; preds = %66
%79 = load i64, i64* %35
%80 = load i64, i64* @SOCK_STREAM
%81 = icmp eq i64 %79, %80
br i1 %81, label %82, label %88
82: ; preds = %78
%83 = load i32, i32* %49
%84 = call i32 @listen(i32 %83, i32 5)
%85 = icmp eq i32 %84, -1
br i1 %85, label %86, label %88
86: ; preds = %82
%87 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %88
88: ; preds = %82, %86, %78
store i32 4, i32* %6
%89 = load i32, i32* %49
%90 = call i32 @getsockname(i32 %89, %struct.sockaddr* %36, i32* %6)
%91 = icmp eq i32 %90, -1
br i1 %91, label %92, label %94
92: ; preds = %88
%93 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
br label %94
94: ; preds = %92, %88
%95 = load i32, i32* %6
%96 = load i32, i32* @NI_NUMERICHOST
%97 = load i32, i32* @NI_NUMERICSERV
%98 = or i32 %97, %96
%99 = call i32 @getnameinfo(%struct.sockaddr* %36, i32 %95, i8* %11, i32 %9, i8* %14, i32 %12, i32 %98)
%100 = icmp eq i32 %99, 0
br i1 %100, label %106, label %101
101: ; preds = %94
%102 = call i32 @gai_strerror(i32 %99)
%103 = sext i32 %102 to i64
%104 = inttoptr i64 %103 to i8*
%105 = call i32 (i32, i8*, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0), i8* %104)
br label %106
106: ; preds = %101, %94
%107 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i64 0, i64 0), i8* %11, i8* %14)
%108 = add nsw i32 %39, 1
br label %109
109: ; preds = %37, %106, %74
%110 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), %74 ], [ %40, %106 ], [ getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), %37 ]
%111 = phi i32 [ %39, %74 ], [ %108, %106 ], [ %39, %37 ]
%112 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i64 0, i32 5
%113 = load %struct.addrinfo*, %struct.addrinfo** %112
%114 = icmp ne %struct.addrinfo* %113, null
%115 = load i32, i32* @FD_SETSIZE
%116 = icmp slt i32 %111, %115
%117 = select i1 %114, i1 %116, i1 false
br i1 %117, label %37, label %118
118: ; preds = %109
%119 = icmp eq i32 %111, 0
br i1 %119, label %120, label %123
120: ; preds = %28, %118
%121 = phi i8* [ %110, %118 ], [ null, %28 ]
%122 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0), i8* %121)
br label %123
123: ; preds = %120, %118
%124 = phi i32 [ 0, %120 ], [ %111, %118 ]
%125 = load %struct.addrinfo*, %struct.addrinfo** %7
%126 = call i32 @freeaddrinfo(%struct.addrinfo* %125)
ret i32 %124
}