#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 addrinfo {
  int ai_family;
  int ai_socktype;
  void *ai_addr;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_protocol;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
};
typedef int /*<<< orphan*/ sockopt;

/* Variables and functions */
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ IPPROTO_IPV6;
int /*<<< orphan*/ IPV6_V6ONLY;
scalar_t__ PF_INET6;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SOMAXCONN;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, void *, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ debug(int, char *, ...);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int stub1(int, void *, int, int);
int socket_create_listen(char *bindaddr, char *service, int family,
                         int socktype,
                         int (*addsock)(int sock, void *addr, int family,
                                        int socktype)) {
  struct addrinfo hints, *list, *p;
  int sock = -1, res;
  int sockcount = 0;
  if (!addsock)
    return -1;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = family;
  hints.ai_socktype = socktype;
  if (bindaddr == NULL)
    hints.ai_flags = AI_PASSIVE;
  if ((res = getaddrinfo(bindaddr, service, &hints, &list)) == -1) {
    debug(2, "socket_create_listen(): getaddrinfo failed: %s",
          gai_strerror(res));
    return -1;
  }
  for (p = list; p != NULL; p = p->ai_next) {
    int sockopt = 1;
    if ((sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
      debug(2,
            "socket_create_listen(): Call to socket failed with %s, "
            "continuing...",
            strerror(errno));
      continue;
    }
    if (p->ai_family == PF_INET6) {
      setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &sockopt, sizeof(sockopt));
      debug(2, "socket_create_listen(): Setting IPV6_V6ONLY socket option");
    }
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(sockopt));
    if (bind(sock, p->ai_addr, p->ai_addrlen) == -1) {
      debug(2, "socket_create_listen(): Bind failed with %s, continuing...",
            strerror(errno));
      close(sock);
      continue;
    }
    if (listen(sock, SOMAXCONN) == -1) {
      debug(2, "socket_create_listen(): Listen failed with %s, continuing...",
            strerror(errno));
      close(sock);
      continue;
    }
    sockcount++;
    if ((*addsock)(sock, p->ai_addr, p->ai_family, p->ai_socktype))
      break;
  }
  freeaddrinfo(list);
  if (!sockcount) {
    debug(
        2,
        "socket_create_listen(): could not create, bind or listen on a socket");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<socket_create_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0xffffffff,-0x64(%rbp)
movl   $0x0,-0x6c(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ef <socket_create_listen+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1463 <socket_create_listen+0x2b3>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1222 <socket_create_listen+0x72>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x68(%rbp)
cmp    $0xffffffff,%eax
jne    126c <socket_create_listen+0xbc>
mov    -0x68(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    %eax,%edx
mov    $0x2,%edi
lea    0xda7(%rip),%rsi
mov    $0x0,%al
call   1040 <debug@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1463 <socket_create_listen+0x2b3>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     142a <socket_create_listen+0x27a>
movl   $0x1,-0x70(%rbp)
mov    -0x60(%rbp),%rax
mov    (%rax),%edi
mov    -0x60(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x60(%rbp),%rax
mov    0x14(%rax),%edx
call   10d0 <socket@plt>
mov    %eax,-0x64(%rbp)
cmp    $0xffffffff,%eax
jne    12d3 <socket_create_listen+0x123>
mov    0x2d06(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%edx
mov    $0x2,%edi
lea    0xd68(%rip),%rsi
mov    $0x0,%al
call   1040 <debug@plt>
jmp    1419 <socket_create_listen+0x269>
mov    -0x60(%rbp),%rax
movslq (%rax),%rax
mov    0x2cf7(%rip),%rcx
cmp    (%rcx),%rax
jne    1321 <socket_create_listen+0x171>
mov    -0x64(%rbp),%edi
mov    0x2cbc(%rip),%rax
mov    (%rax),%esi
mov    0x2cc3(%rip),%rax
mov    (%rax),%edx
lea    -0x70(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    $0x2,%edi
lea    0xd5a(%rip),%rsi
mov    $0x0,%al
call   1040 <debug@plt>
mov    -0x64(%rbp),%edi
mov    0x2cbd(%rip),%rax
mov    (%rax),%esi
mov    0x2c74(%rip),%rax
mov    (%rax),%edx
lea    -0x70(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x60(%rbp),%rax
mov    0x10(%rax),%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1395 <socket_create_listen+0x1e5>
mov    0x2c4c(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%edx
mov    $0x2,%edi
lea    0xd2d(%rip),%rsi
mov    $0x0,%al
call   1040 <debug@plt>
mov    -0x64(%rbp),%edi
call   1070 <close@plt>
jmp    1419 <socket_create_listen+0x269>
mov    -0x64(%rbp),%edi
mov    0x2c29(%rip),%rax
mov    (%rax),%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    13df <socket_create_listen+0x22f>
mov    0x2c02(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %eax,%edx
mov    $0x2,%edi
lea    0xd1e(%rip),%rsi
mov    $0x0,%al
call   1040 <debug@plt>
mov    -0x64(%rbp),%edi
call   1070 <close@plt>
jmp    1419 <socket_create_listen+0x269>
mov    -0x6c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x6c(%rbp)
mov    -0x28(%rbp),%rax
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rcx
mov    0x8(%rcx),%rsi
mov    -0x60(%rbp),%rcx
mov    (%rcx),%edx
mov    -0x60(%rbp),%rcx
mov    0x4(%rcx),%ecx
call   *%rax
cmp    $0x0,%eax
je     1414 <socket_create_listen+0x264>
jmp    142a <socket_create_listen+0x27a>
jmp    1419 <socket_create_listen+0x269>
mov    -0x60(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x60(%rbp)
jmp    1274 <socket_create_listen+0xc4>
mov    -0x58(%rbp),%rdi
call   10c0 <freeaddrinfo@plt>
cmpl   $0x0,-0x6c(%rbp)
jne    145c <socket_create_listen+0x2ac>
mov    $0x2,%edi
lea    0xcdd(%rip),%rsi
mov    $0x0,%al
call   1040 <debug@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1463 <socket_create_listen+0x2b3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_create_listen>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    $0xffffffff,%ebx
test   %r8,%r8
je     13e9 <socket_create_listen+0x239>
mov    %r8,%r15
mov    %ecx,%r12d
mov    %edx,%r13d
mov    %rsi,%r14
mov    %rdi,%rbp
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1060 <memset@plt>
mov    %r13d,0x10(%rsp)
mov    %r12d,0x14(%rsp)
test   %rbp,%rbp
jne    1208 <socket_create_listen+0x58>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %rbp,%rdi
mov    %r14,%rsi
call   10a0 <getaddrinfo@plt>
cmp    $0xffffffff,%eax
je     13a4 <socket_create_listen+0x1f4>
mov    0x8(%rsp),%r13
test   %r13,%r13
je     13ca <socket_create_listen+0x21a>
mov    0x2d7d(%rip),%r12
lea    0x4(%rsp),%rbx
xor    %r14d,%r14d
jmp    127b <socket_create_listen+0xcb>
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r12),%edi
call   10b0 <strerror@plt>
mov    $0x2,%edi
lea    0xdca(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <debug@plt>
mov    0x18(%r13),%r13
test   %r13,%r13
je     1391 <socket_create_listen+0x1e1>
movl   $0x1,0x4(%rsp)
mov    0x0(%r13),%edi
mov    0x4(%r13),%esi
mov    0x14(%r13),%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     1250 <socket_create_listen+0xa0>
mov    %eax,%ebp
movslq 0x0(%r13),%rax
mov    0x2d32(%rip),%rcx
cmp    %rax,(%rcx)
jne    12e0 <socket_create_listen+0x130>
mov    0x2cfe(%rip),%rax
mov    (%rax),%esi
mov    0x2d05(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %rbx,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    $0x2,%edi
lea    0xd9b(%rip),%rsi
xor    %eax,%eax
call   1040 <debug@plt>
mov    0x2d01(%rip),%rax
mov    (%rax),%esi
mov    0x2cb8(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %rbx,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x8(%r13),%rsi
mov    0x10(%r13),%edx
mov    %ebp,%edi
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     1350 <socket_create_listen+0x1a0>
mov    0x2cab(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
je     1367 <socket_create_listen+0x1b7>
add    $0x1,%r14d
mov    0x8(%r13),%rsi
mov    0x0(%r13),%edx
mov    0x4(%r13),%ecx
mov    %ebp,%edi
call   *%r15
test   %eax,%eax
je     126e <socket_create_listen+0xbe>
jmp    1391 <socket_create_listen+0x1e1>
nopw   0x0(%rax,%rax,1)
mov    (%r12),%edi
call   10b0 <strerror@plt>
mov    $0x2,%edi
lea    0xd49(%rip),%rsi
jmp    137c <socket_create_listen+0x1cc>
mov    (%r12),%edi
call   10b0 <strerror@plt>
mov    $0x2,%edi
lea    0xd6d(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <debug@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    126e <socket_create_listen+0xbe>
mov    0x8(%rsp),%rdi
call   10c0 <freeaddrinfo@plt>
xor    %ebx,%ebx
test   %r14d,%r14d
jne    13e9 <socket_create_listen+0x239>
jmp    13d1 <socket_create_listen+0x221>
mov    $0xffffffff,%ebx
mov    $0xffffffff,%edi
call   1050 <gai_strerror@plt>
lea    0xc46(%rip),%rsi
mov    $0x2,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <debug@plt>
jmp    13e9 <socket_create_listen+0x239>
xor    %edi,%edi
call   10c0 <freeaddrinfo@plt>
lea    0xd4e(%rip),%rsi
mov    $0x2,%edi
xor    %eax,%eax
call   1040 <debug@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i8*, i32, i32, %struct.addrinfo*, i32 }
@AI_PASSIVE = global i32 0
@.str = [47 x i8] c"socket_create_listen(): getaddrinfo failed: %s\00"
@.str.1 = [69 x i8] c"socket_create_listen(): Call to socket failed with %s, continuing...\00"
@errno = global i32 0
@PF_INET6 = global i64 0
@IPPROTO_IPV6 = global i32 0
@IPV6_V6ONLY = global i32 0
@.str.2 = [58 x i8] c"socket_create_listen(): Setting IPV6_V6ONLY socket option\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.3 = [59 x i8] c"socket_create_listen(): Bind failed with %s, continuing...\00"
@SOMAXCONN = global i32 0
@.str.4 = [61 x i8] c"socket_create_listen(): Listen failed with %s, continuing...\00"
@.str.5 = [69 x i8] c"socket_create_listen(): could not create, bind or listen on a socket\00"
define i32 @socket_create_listen(i8* %0, i8* %1, i32 %2, i32 %3, i32 (i32, i8*, i32, i32)* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32 (i32, i8*, i32, i32)*
%12 = alloca %struct.addrinfo
%13 = alloca %struct.addrinfo*
%14 = alloca %struct.addrinfo*
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 (i32, i8*, i32, i32)* %4, i32 (i32, i8*, i32, i32)** %11
store i32 -1, i32* %15
store i32 0, i32* %17
%19 = load i32 (i32, i8*, i32, i32)*, i32 (i32, i8*, i32, i32)** %11
%20 = icmp ne i32 (i32, i8*, i32, i32)* %19, null
br i1 %20, label %22, label %21
21: ; preds = %5
store i32 -1, i32* %6
br label %137
22: ; preds = %5
%23 = call i32 @memset(%struct.addrinfo* %12, i32 0, i32 40)
%24 = load i32, i32* %9
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* %10
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 1
store i32 %26, i32* %27
%28 = load i8*, i8** %7
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %33
30: ; preds = %22
%31 = load i32, i32* @AI_PASSIVE
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 6
store i32 %31, i32* %32
br label %33
33: ; preds = %30, %22
%34 = load i8*, i8** %7
%35 = load i8*, i8** %8
%36 = call i32 @getaddrinfo(i8* %34, i8* %35, %struct.addrinfo* %12, %struct.addrinfo** %13)
store i32 %36, i32* %16
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %42
38: ; preds = %33
%39 = load i32, i32* %16
%40 = call i32 @gai_strerror(i32 %39)
%41 = call i32 (i32, i8*, ...) @debug(i32 2, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %40)
store i32 -1, i32* %6
br label %137
42: ; preds = %33
%43 = load %struct.addrinfo*, %struct.addrinfo** %13
store %struct.addrinfo* %43, %struct.addrinfo** %14
br label %44
44: ; preds = %125, %42
%45 = load %struct.addrinfo*, %struct.addrinfo** %14
%46 = icmp ne %struct.addrinfo* %45, null
br i1 %46, label %47, label %129
47: ; preds = %44
store i32 1, i32* %18
%48 = load %struct.addrinfo*, %struct.addrinfo** %14
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = load %struct.addrinfo*, %struct.addrinfo** %14
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 1
%53 = load i32, i32* %52
%54 = load %struct.addrinfo*, %struct.addrinfo** %14
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %54, i32 0, i32 4
%56 = load i32, i32* %55
%57 = call i32 @socket(i32 %50, i32 %53, i32 %56)
store i32 %57, i32* %15
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %63
59: ; preds = %47
%60 = load i32, i32* @errno
%61 = call i32 @strerror(i32 %60)
%62 = call i32 (i32, i8*, ...) @debug(i32 2, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.1, i64 0, i64 0), i32 %61)
br label %125
63: ; preds = %47
%64 = load %struct.addrinfo*, %struct.addrinfo** %14
%65 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %64, i32 0, i32 0
%66 = load i32, i32* %65
%67 = sext i32 %66 to i64
%68 = load i64, i64* @PF_INET6
%69 = icmp eq i64 %67, %68
br i1 %69, label %70, label %76
70: ; preds = %63
%71 = load i32, i32* %15
%72 = load i32, i32* @IPPROTO_IPV6
%73 = load i32, i32* @IPV6_V6ONLY
%74 = call i32 @setsockopt(i32 %71, i32 %72, i32 %73, i32* %18, i32 4)
%75 = call i32 (i32, i8*, ...) @debug(i32 2, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0))
br label %76
76: ; preds = %70, %63
%77 = load i32, i32* %15
%78 = load i32, i32* @SOL_SOCKET
%79 = load i32, i32* @SO_REUSEADDR
%80 = call i32 @setsockopt(i32 %77, i32 %78, i32 %79, i32* %18, i32 4)
%81 = load i32, i32* %15
%82 = load %struct.addrinfo*, %struct.addrinfo** %14
%83 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %82, i32 0, i32 2
%84 = load i8*, i8** %83
%85 = load %struct.addrinfo*, %struct.addrinfo** %14
%86 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %85, i32 0, i32 3
%87 = load i32, i32* %86
%88 = call i32 @bind(i32 %81, i8* %84, i32 %87)
%89 = icmp eq i32 %88, -1
br i1 %89, label %90, label %96
90: ; preds = %76
%91 = load i32, i32* @errno
%92 = call i32 @strerror(i32 %91)
%93 = call i32 (i32, i8*, ...) @debug(i32 2, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.3, i64 0, i64 0), i32 %92)
%94 = load i32, i32* %15
%95 = call i32 @close(i32 %94)
br label %125
96: ; preds = %76
%97 = load i32, i32* %15
%98 = load i32, i32* @SOMAXCONN
%99 = call i32 @listen(i32 %97, i32 %98)
%100 = icmp eq i32 %99, -1
br i1 %100, label %101, label %107
101: ; preds = %96
%102 = load i32, i32* @errno
%103 = call i32 @strerror(i32 %102)
%104 = call i32 (i32, i8*, ...) @debug(i32 2, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.4, i64 0, i64 0), i32 %103)
%105 = load i32, i32* %15
%106 = call i32 @close(i32 %105)
br label %125
107: ; preds = %96
%108 = load i32, i32* %17
%109 = add nsw i32 %108, 1
store i32 %109, i32* %17
%110 = load i32 (i32, i8*, i32, i32)*, i32 (i32, i8*, i32, i32)** %11
%111 = load i32, i32* %15
%112 = load %struct.addrinfo*, %struct.addrinfo** %14
%113 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %112, i32 0, i32 2
%114 = load i8*, i8** %113
%115 = load %struct.addrinfo*, %struct.addrinfo** %14
%116 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %115, i32 0, i32 0
%117 = load i32, i32* %116
%118 = load %struct.addrinfo*, %struct.addrinfo** %14
%119 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %118, i32 0, i32 1
%120 = load i32, i32* %119
%121 = call i32 %110(i32 %111, i8* %114, i32 %117, i32 %120)
%122 = icmp ne i32 %121, 0
br i1 %122, label %123, label %124
123: ; preds = %107
br label %129
124: ; preds = %107
br label %125
125: ; preds = %124, %101, %90, %59
%126 = load %struct.addrinfo*, %struct.addrinfo** %14
%127 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %126, i32 0, i32 5
%128 = load %struct.addrinfo*, %struct.addrinfo** %127
store %struct.addrinfo* %128, %struct.addrinfo** %14
br label %44
129: ; preds = %123, %44
%130 = load %struct.addrinfo*, %struct.addrinfo** %13
%131 = call i32 @freeaddrinfo(%struct.addrinfo* %130)
%132 = load i32, i32* %17
%133 = icmp ne i32 %132, 0
br i1 %133, label %136, label %134
134: ; preds = %129
%135 = call i32 (i32, i8*, ...) @debug(i32 2, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %6
br label %137
136: ; preds = %129
store i32 0, i32* %6
br label %137
137: ; preds = %136, %134, %38, %21
%138 = load i32, i32* %6
ret i32 %138
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i8*, i32, i32, %struct.addrinfo*, i32 }
@AI_PASSIVE = global i32 0
@.str = [47 x i8] c"socket_create_listen(): getaddrinfo failed: %s\00"
@.str.1 = [69 x i8] c"socket_create_listen(): Call to socket failed with %s, continuing...\00"
@errno = global i32 0
@PF_INET6 = global i64 0
@IPPROTO_IPV6 = global i32 0
@IPV6_V6ONLY = global i32 0
@.str.2 = [58 x i8] c"socket_create_listen(): Setting IPV6_V6ONLY socket option\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.3 = [59 x i8] c"socket_create_listen(): Bind failed with %s, continuing...\00"
@SOMAXCONN = global i32 0
@.str.4 = [61 x i8] c"socket_create_listen(): Listen failed with %s, continuing...\00"
@.str.5 = [69 x i8] c"socket_create_listen(): could not create, bind or listen on a socket\00"
define i32 @socket_create_listen(i8* %0, i8* %1, i32 %2, i32 %3, i32 (i32, i8*, i32, i32)* %4) {
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca i32
%9 = bitcast %struct.addrinfo* %6 to i8*
%10 = bitcast %struct.addrinfo** %7 to i8*
%11 = icmp eq i32 (i32, i8*, i32, i32)* %4, null
br i1 %11, label %102, label %12
12: ; preds = %5
%13 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 40)
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 0
store i32 %2, i32* %14
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 1
store i32 %3, i32* %15
%16 = icmp eq i8* %0, null
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i32, i32* @AI_PASSIVE
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 6
store i32 %18, i32* %19
br label %20
20: ; preds = %17, %12
%21 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %6, %struct.addrinfo** %7)
%22 = icmp eq i32 %21, -1
br i1 %22, label %30, label %23
23: ; preds = %20
%24 = load %struct.addrinfo*, %struct.addrinfo** %7
%25 = icmp eq %struct.addrinfo* %24, null
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = call i32 @freeaddrinfo(%struct.addrinfo* null)
br label %100
28: ; preds = %23
%29 = bitcast i32* %8 to i8*
br label %33
30: ; preds = %20
%31 = call i32 @gai_strerror(i32 -1)
%32 = call i32 (i32, i8*, ...) @debug(i32 2, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %31)
br label %102
33: ; preds = %28, %90
%34 = phi %struct.addrinfo* [ %24, %28 ], [ %93, %90 ]
%35 = phi i32 [ 0, %28 ], [ %91, %90 ]
store i32 1, i32* %8
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i64 0, i32 0
%37 = load i32, i32* %36
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i64 0, i32 1
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i64 0, i32 4
%41 = load i32, i32* %40
%42 = call i32 @socket(i32 %37, i32 %39, i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %48
44: ; preds = %33
%45 = load i32, i32* @errno
%46 = call i32 @strerror(i32 %45)
%47 = call i32 (i32, i8*, ...) @debug(i32 2, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.1, i64 0, i64 0), i32 %46)
br label %82
48: ; preds = %33
%49 = load i32, i32* %36
%50 = sext i32 %49 to i64
%51 = load i64, i64* @PF_INET6
%52 = icmp eq i64 %51, %50
br i1 %52, label %53, label %58
53: ; preds = %48
%54 = load i32, i32* @IPPROTO_IPV6
%55 = load i32, i32* @IPV6_V6ONLY
%56 = call i32 @setsockopt(i32 %42, i32 %54, i32 %55, i32* %8, i32 4)
%57 = call i32 (i32, i8*, ...) @debug(i32 2, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0))
br label %58
58: ; preds = %53, %48
%59 = load i32, i32* @SOL_SOCKET
%60 = load i32, i32* @SO_REUSEADDR
%61 = call i32 @setsockopt(i32 %42, i32 %59, i32 %60, i32* %8, i32 4)
%62 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i64 0, i32 2
%63 = load i8*, i8** %62
%64 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i64 0, i32 3
%65 = load i32, i32* %64
%66 = call i32 @bind(i32 %42, i8* %63, i32 %65)
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %73
68: ; preds = %58
%69 = load i32, i32* @errno
%70 = call i32 @strerror(i32 %69)
%71 = call i32 (i32, i8*, ...) @debug(i32 2, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.3, i64 0, i64 0), i32 %70)
%72 = call i32 @close(i32 %42)
br label %82
73: ; preds = %58
%74 = load i32, i32* @SOMAXCONN
%75 = call i32 @listen(i32 %42, i32 %74)
%76 = icmp eq i32 %75, -1
br i1 %76, label %77, label %83
77: ; preds = %73
%78 = load i32, i32* @errno
%79 = call i32 @strerror(i32 %78)
%80 = call i32 (i32, i8*, ...) @debug(i32 2, i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.4, i64 0, i64 0), i32 %79)
%81 = call i32 @close(i32 %42)
br label %82
82: ; preds = %44, %68, %77
br label %90
83: ; preds = %73
%84 = add nsw i32 %35, 1
%85 = load i8*, i8** %62
%86 = load i32, i32* %36
%87 = load i32, i32* %38
%88 = call i32 %4(i32 %42, i8* %85, i32 %86, i32 %87)
%89 = icmp eq i32 %88, 0
br i1 %89, label %90, label %95
90: ; preds = %82, %83
%91 = phi i32 [ %35, %82 ], [ %84, %83 ]
%92 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i64 0, i32 5
%93 = load %struct.addrinfo*, %struct.addrinfo** %92
%94 = icmp eq %struct.addrinfo* %93, null
br i1 %94, label %95, label %33
95: ; preds = %90, %83
%96 = phi i32 [ %91, %90 ], [ %84, %83 ]
%97 = load %struct.addrinfo*, %struct.addrinfo** %7
%98 = call i32 @freeaddrinfo(%struct.addrinfo* %97)
%99 = icmp eq i32 %96, 0
br i1 %99, label %100, label %102
100: ; preds = %26, %95
%101 = call i32 (i32, i8*, ...) @debug(i32 2, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.5, i64 0, i64 0))
br label %102
102: ; preds = %95, %5, %100, %30
%103 = phi i32 [ -1, %30 ], [ -1, %100 ], [ -1, %5 ], [ 0, %95 ]
ret i32 %103
}