#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 */
typedef int /*<<< orphan*/ uport;
struct sockaddr {
  int dummy;
};
struct addrinfo {
  int ai_flags;
  scalar_t__ ai_family;
  int /*<<< orphan*/ ai_addrlen;
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  struct addrinfo *ai_next;
};
typedef int /*<<< orphan*/ portbuf;
typedef int /*<<< orphan*/ on;
typedef int /*<<< orphan*/ off;
typedef int /*<<< orphan*/ ai;

/* Variables and functions */
int AI_ADDRCONFIG;
int AI_PASSIVE;
int /*<<< orphan*/ INET6_ADDRSTRLEN;
int /*<<< orphan*/ IPPROTO_IPV6;
int /*<<< orphan*/ IPV6_V6ONLY;
int NI_NUMERICHOST;
int NI_NUMERICSERV;
scalar_t__ PF_INET6;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SOMAXCONN;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
int getnameinfo(struct sockaddr *, int /*<<< orphan*/, char *,
                int /*<<< orphan*/, char *, int, int);
scalar_t__ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              void *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(scalar_t__, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strlen(char const *);
int try_port(const char *addr, int port) {
  static const int on = 1, off = 0;
  struct addrinfo ai, *res, *e;
  char portbuf[33];
  int sock, rc;
  memset(&ai, 0, sizeof(ai));
  ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
  ai.ai_socktype = SOCK_STREAM;
  ai.ai_family = 0;
  snprintf(portbuf, sizeof(portbuf), "%d", port);
  rc = getaddrinfo(addr && strlen(addr) ? addr : NULL, portbuf, &ai, &res);
  if (rc != 0)
    return -1;
  for (e = res; e != NULL; e = e->ai_next) {
    sock = socket(e->ai_family, e->ai_socktype, e->ai_protocol);
    if (sock < 0)
      continue;
    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
    if (e->ai_family == PF_INET6)
      setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&off, sizeof(off));
    if (bind(sock, e->ai_addr, e->ai_addrlen) == 0) {
      char uaddr[INET6_ADDRSTRLEN + 1];
      char uport[33];
      rc = getnameinfo((struct sockaddr *)e->ai_addr, e->ai_addrlen, uaddr,
                       INET6_ADDRSTRLEN, uport, sizeof(uport) - 1,
                       NI_NUMERICHOST | NI_NUMERICSERV);
      if (rc == 0)
        printf("bound to %s:%s\n", uaddr, uport);
      else
        printf("cannot resolve address spice-server is bound to\n");
      freeaddrinfo(res);
      goto listen;
    }
    close(sock);
    break;
  }
  freeaddrinfo(res);
  return -1;
listen:
  if (listen(sock, SOMAXCONN) != 0) {
    perror("Error in listen");
    close(sock);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<try_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1070 <memset@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    0x2d7e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x48(%rbp)
mov    0x2da2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
movq   $0x0,-0x40(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x21,%esi
lea    0xdda(%rip),%rdx
call   1060 <snprintf@plt>
cmpq   $0x0,-0x10(%rbp)
je     1261 <try_port+0x91>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
je     1261 <try_port+0x91>
mov    -0x10(%rbp),%rax
mov    %rax,-0xd0(%rbp)
jmp    126f <try_port+0x9f>
xor    %eax,%eax
mov    %rax,-0xd0(%rbp)
jmp    126f <try_port+0x9f>
mov    -0xd0(%rbp),%rdi
lea    -0x80(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   10d0 <getaddrinfo@plt>
mov    %eax,-0x88(%rbp)
cmpl   $0x0,-0x88(%rbp)
je     12a6 <try_port+0xd6>
movl   $0xffffffff,-0x4(%rbp)
jmp    14e0 <try_port+0x310>
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     1481 <try_port+0x2b1>
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x58(%rbp),%rax
mov    0x24(%rax),%esi
mov    -0x58(%rbp),%rax
mov    0x20(%rax),%edx
call   10f0 <socket@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jge    12ec <try_port+0x11c>
jmp    1470 <try_port+0x2a0>
mov    -0x84(%rbp),%edi
mov    0x2ce7(%rip),%rax
mov    (%rax),%esi
mov    0x2c8e(%rip),%rax
mov    (%rax),%edx
lea    0xcf5(%rip),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%rax
mov    0x2ca3(%rip),%rcx
cmp    (%rcx),%rax
jne    1358 <try_port+0x188>
mov    -0x84(%rbp),%edi
mov    0x2c5d(%rip),%rax
mov    (%rax),%esi
mov    0x2c5c(%rip),%rax
mov    (%rax),%edx
lea    0xcb7(%rip),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x84(%rbp),%edi
mov    -0x58(%rbp),%rax
mov    0x18(%rax),%rsi
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jne    1460 <try_port+0x290>
mov    0x2c55(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x90(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdx
sub    %rcx,%rdx
mov    %rdx,-0xd8(%rbp)
mov    %rdx,%rsp
mov    %rax,-0x98(%rbp)
mov    -0x58(%rbp),%rax
mov    0x18(%rax),%rdi
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%esi
mov    0x2c0e(%rip),%rax
mov    (%rax),%ecx
lea    -0xc0(%rbp),%r8
mov    0x2bce(%rip),%rax
mov    (%rax),%eax
mov    0x2c05(%rip),%r9
or     (%r9),%eax
mov    $0x20,%r9d
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1090 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,-0x88(%rbp)
cmpl   $0x0,-0x88(%rbp)
jne    1430 <try_port+0x260>
mov    -0xd8(%rbp),%rsi
lea    -0xc0(%rbp),%rdx
lea    0xbe7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    143e <try_port+0x26e>
lea    0xbe4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x50(%rbp),%rdi
call   10e0 <freeaddrinfo@plt>
movl   $0x5,-0xc4(%rbp)
mov    -0x90(%rbp),%rax
mov    %rax,%rsp
jmp    1496 <try_port+0x2c6>
mov    -0x84(%rbp),%edi
call   1080 <close@plt>
jmp    1481 <try_port+0x2b1>
mov    -0x58(%rbp),%rax
mov    0x28(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    12ae <try_port+0xde>
mov    -0x50(%rbp),%rdi
call   10e0 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14e0 <try_port+0x310>
mov    -0x84(%rbp),%edi
mov    0x2b15(%rip),%rax
mov    (%rax),%esi
call   10a0 <listen@plt>
cmp    $0x0,%rax
je     14d7 <try_port+0x307>
lea    0xb91(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x84(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    14e0 <try_port+0x310>
mov    -0x84(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<try_port>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %esi,%r15d
mov    %rdi,%r14
lea    -0x60(%rbp),%rdi
xor    %ebx,%ebx
xor    %esi,%esi
mov    $0x30,%edx
call   1070 <memset@plt>
mov    0x2dcd(%rip),%rax
mov    0x2d76(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,-0x60(%rbp)
mov    0x2d98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
movq   $0x0,-0x58(%rbp)
lea    0xddc(%rip),%rdx
lea    -0xc0(%rbp),%rdi
mov    $0x21,%esi
mov    %r15d,%ecx
call   1060 <snprintf@plt>
test   %r14,%r14
je     124f <try_port+0x7f>
xor    %ebx,%ebx
cmpb   $0x0,(%r14)
cmovne %r14,%rbx
lea    -0xc0(%rbp),%rsi
lea    -0x60(%rbp),%rdx
lea    -0x30(%rbp),%rcx
mov    %rbx,%rdi
call   10d0 <getaddrinfo@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
jne    1334 <try_port+0x164>
mov    -0x30(%rbp),%rbx
test   %rbx,%rbx
je     132b <try_port+0x15b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x8(%rbx),%rdi
mov    0x24(%rbx),%esi
mov    0x20(%rbx),%edx
call   10f0 <socket@plt>
test   %eax,%eax
jns    12ae <try_port+0xde>
mov    0x28(%rbx),%rbx
test   %rbx,%rbx
jne    1290 <try_port+0xc0>
jmp    132b <try_port+0x15b>
mov    %eax,%r12d
mov    0x2d28(%rip),%rax
mov    (%rax),%esi
mov    0x2ccf(%rip),%rax
mov    (%rax),%edx
lea    0xd36(%rip),%rcx
mov    %r12d,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    0x8(%rbx),%rax
mov    0x2ce5(%rip),%rcx
cmp    (%rcx),%rax
jne    130f <try_port+0x13f>
mov    0x2ca9(%rip),%rax
mov    (%rax),%esi
mov    0x2ca8(%rip),%rax
mov    (%rax),%edx
lea    0xd03(%rip),%rcx
mov    %r12d,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    0x18(%rbx),%rsi
mov    0x10(%rbx),%edx
mov    %r12d,%edi
call   10b0 <bind@plt>
test   %rax,%rax
je     1346 <try_port+0x176>
mov    %r12d,%edi
call   1080 <close@plt>
mov    -0x30(%rbp),%rdi
call   10e0 <freeaddrinfo@plt>
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rsp,%r13
mov    0x2c88(%rip),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    %rsp,%r15
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r15
mov    %r15,%rsp
mov    0x18(%rbx),%rdi
mov    0x10(%rbx),%esi
mov    (%rax),%ecx
mov    0x2c32(%rip),%rax
mov    0x2c6b(%rip),%rdx
mov    (%rdx),%ebx
or     (%rax),%ebx
sub    $0x8,%rsp
lea    -0x90(%rbp),%r8
mov    %r15,%rdx
mov    $0x20,%r9d
push   %rbx
call   1090 <getnameinfo@plt>
add    $0x10,%rsp
test   %eax,%eax
je     13b1 <try_port+0x1e1>
lea    0xc81(%rip),%rdi
call   1030 <puts@plt>
jmp    13c9 <try_port+0x1f9>
lea    0xc53(%rip),%rdi
lea    -0x90(%rbp),%rdx
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
call   10e0 <freeaddrinfo@plt>
mov    %r13,%rsp
mov    0x2bdc(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   10a0 <listen@plt>
test   %rax,%rax
jne    13f3 <try_port+0x223>
mov    %r12d,%r14d
jmp    1334 <try_port+0x164>
lea    0xc21(%rip),%rdi
call   10c0 <perror@plt>
mov    %r12d,%edi
call   1080 <close@plt>
jmp    1334 <try_port+0x164>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i64, i32, i64, i32, i32, %struct.addrinfo* }
%struct.sockaddr = type { i32 }
@try_port.on = internal constant i32 1
@try_port.off = internal constant i32 0
@AI_PASSIVE = global i32 0
@AI_ADDRCONFIG = global i32 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"%d\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@PF_INET6 = global i64 0
@IPPROTO_IPV6 = global i32 0
@IPV6_V6ONLY = global i32 0
@INET6_ADDRSTRLEN = global i32 0
@NI_NUMERICHOST = global i32 0
@NI_NUMERICSERV = global i32 0
@.str.1 = [16 x i8] c"bound to %s:%s\0A\00"
@.str.2 = [49 x i8] c"cannot resolve address spice-server is bound to\0A\00"
@SOMAXCONN = global i32 0
@.str.3 = [16 x i8] c"Error in listen\00"
define i32 @try_port(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.addrinfo*
%9 = alloca [33 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i64
%14 = alloca [33 x i8]
%15 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%16 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 48)
%17 = load i32, i32* @AI_PASSIVE
%18 = load i32, i32* @AI_ADDRCONFIG
%19 = or i32 %17, %18
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i32, i32* @SOCK_STREAM
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 5
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 1
store i64 0, i64* %23
%24 = getelementptr inbounds [33 x i8], [33 x i8]* %9, i64 0, i64 0
%25 = load i32, i32* %5
%26 = call i32 @snprintf(i8* %24, i32 33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = load i8*, i8** %4
%28 = icmp ne i8* %27, null
br i1 %28, label %29, label %35
29: ; preds = %2
%30 = load i8*, i8** %4
%31 = call i64 @strlen(i8* %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = load i8*, i8** %4
br label %36
35: ; preds = %29, %2
br label %36
36: ; preds = %35, %33
%37 = phi i8* [ %34, %33 ], [ null, %35 ]
%38 = getelementptr inbounds [33 x i8], [33 x i8]* %9, i64 0, i64 0
%39 = call i32 @getaddrinfo(i8* %37, i8* %38, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %39, i32* %11
%40 = load i32, i32* %11
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %36
store i32 -1, i32* %3
br label %139
43: ; preds = %36
%44 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %44, %struct.addrinfo** %8
br label %45
45: ; preds = %121, %43
%46 = load %struct.addrinfo*, %struct.addrinfo** %8
%47 = icmp ne %struct.addrinfo* %46, null
br i1 %47, label %48, label %125
48: ; preds = %45
%49 = load %struct.addrinfo*, %struct.addrinfo** %8
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i32 0, i32 1
%51 = load i64, i64* %50
%52 = load %struct.addrinfo*, %struct.addrinfo** %8
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %52, i32 0, i32 5
%54 = load i32, i32* %53
%55 = load %struct.addrinfo*, %struct.addrinfo** %8
%56 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i32 0, i32 4
%57 = load i32, i32* %56
%58 = call i32 @socket(i64 %51, i32 %54, i32 %57)
store i32 %58, i32* %10
%59 = load i32, i32* %10
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %48
br label %121
62: ; preds = %48
%63 = load i32, i32* %10
%64 = load i32, i32* @SOL_SOCKET
%65 = load i32, i32* @SO_REUSEADDR
%66 = call i32 @setsockopt(i32 %63, i32 %64, i32 %65, i8* bitcast (i32* @try_port.on to i8*), i32 4)
%67 = load %struct.addrinfo*, %struct.addrinfo** %8
%68 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %67, i32 0, i32 1
%69 = load i64, i64* %68
%70 = load i64, i64* @PF_INET6
%71 = icmp eq i64 %69, %70
br i1 %71, label %72, label %77
72: ; preds = %62
%73 = load i32, i32* %10
%74 = load i32, i32* @IPPROTO_IPV6
%75 = load i32, i32* @IPV6_V6ONLY
%76 = call i32 @setsockopt(i32 %73, i32 %74, i32 %75, i8* bitcast (i32* @try_port.off to i8*), i32 4)
br label %77
77: ; preds = %72, %62
%78 = load i32, i32* %10
%79 = load %struct.addrinfo*, %struct.addrinfo** %8
%80 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %79, i32 0, i32 3
%81 = load i64, i64* %80
%82 = load %struct.addrinfo*, %struct.addrinfo** %8
%83 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %82, i32 0, i32 2
%84 = load i32, i32* %83
%85 = call i64 @bind(i32 %78, i64 %81, i32 %84)
%86 = icmp eq i64 %85, 0
br i1 %86, label %87, label %118
87: ; preds = %77
%88 = load i32, i32* @INET6_ADDRSTRLEN
%89 = add nsw i32 %88, 1
%90 = zext i32 %89 to i64
%91 = call i8* @llvm.stacksave()
store i8* %91, i8** %12
%92 = alloca i8, i64 %90
store i64 %90, i64* %13
%93 = load %struct.addrinfo*, %struct.addrinfo** %8
%94 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %93, i32 0, i32 3
%95 = load i64, i64* %94
%96 = inttoptr i64 %95 to %struct.sockaddr*
%97 = load %struct.addrinfo*, %struct.addrinfo** %8
%98 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %97, i32 0, i32 2
%99 = load i32, i32* %98
%100 = load i32, i32* @INET6_ADDRSTRLEN
%101 = getelementptr inbounds [33 x i8], [33 x i8]* %14, i64 0, i64 0
%102 = load i32, i32* @NI_NUMERICHOST
%103 = load i32, i32* @NI_NUMERICSERV
%104 = or i32 %102, %103
%105 = call i32 @getnameinfo(%struct.sockaddr* %96, i32 %99, i8* %92, i32 %100, i8* %101, i32 32, i32 %104)
store i32 %105, i32* %11
%106 = load i32, i32* %11
%107 = icmp eq i32 %106, 0
br i1 %107, label %108, label %111
108: ; preds = %87
%109 = getelementptr inbounds [33 x i8], [33 x i8]* %14, i64 0, i64 0
%110 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %92, i8* %109)
br label %113
111: ; preds = %87
%112 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0))
br label %113
113: ; preds = %111, %108
%114 = load %struct.addrinfo*, %struct.addrinfo** %7
%115 = call i32 @freeaddrinfo(%struct.addrinfo* %114)
store i32 5, i32* %15
%116 = load i8*, i8** %12
%117 = load i32, i32* %15
switch i32 %117, label %141 [
i32 5, label %128
]
118: ; preds = %77
%119 = load i32, i32* %10
%120 = call i32 @close(i32 %119)
br label %125
121: ; preds = %61
%122 = load %struct.addrinfo*, %struct.addrinfo** %8
%123 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %122, i32 0, i32 6
%124 = load %struct.addrinfo*, %struct.addrinfo** %123
store %struct.addrinfo* %124, %struct.addrinfo** %8
br label %45
125: ; preds = %118, %45
%126 = load %struct.addrinfo*, %struct.addrinfo** %7
%127 = call i32 @freeaddrinfo(%struct.addrinfo* %126)
store i32 -1, i32* %3
br label %139
128: ; preds = %113
%129 = load i32, i32* %10
%130 = load i32, i32* @SOMAXCONN
%131 = call i64 @listen(i32 %129, i32 %130)
%132 = icmp ne i64 %131, 0
br i1 %132, label %133, label %137
133: ; preds = %128
%134 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%135 = load i32, i32* %10
%136 = call i32 @close(i32 %135)
store i32 -1, i32* %3
br label %139
137: ; preds = %128
%138 = load i32, i32* %10
store i32 %138, i32* %3
br label %139
139: ; preds = %137, %133, %125, %42
%140 = load i32, i32* %3
ret i32 %140
141: ; preds = %113
unreachable
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i64, i32, i64, i32, i32, %struct.addrinfo* }
%struct.sockaddr = type { i32 }
@try_port.on = internal constant i32 1
@try_port.off = internal constant i32 0
@AI_PASSIVE = global i32 0
@AI_ADDRCONFIG = global i32 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"%d\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@PF_INET6 = global i64 0
@IPPROTO_IPV6 = global i32 0
@IPV6_V6ONLY = global i32 0
@INET6_ADDRSTRLEN = global i32 0
@NI_NUMERICHOST = global i32 0
@NI_NUMERICSERV = global i32 0
@.str.1 = [16 x i8] c"bound to %s:%s\0A\00"
@SOMAXCONN = global i32 0
@.str.3 = [16 x i8] c"Error in listen\00"
@str = [48 x i8] c"cannot resolve address spice-server is bound to\00"
define i32 @try_port(i8* %0, i32 %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca [33 x i8]
%6 = alloca [33 x i8]
%7 = bitcast %struct.addrinfo* %3 to i8*
%8 = bitcast %struct.addrinfo** %4 to i8*
%9 = getelementptr inbounds [33 x i8], [33 x i8]* %5, i64 0, i64 0
%10 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 48)
%11 = load i32, i32* @AI_PASSIVE
%12 = load i32, i32* @AI_ADDRCONFIG
%13 = or i32 %12, %11
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* @SOCK_STREAM
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 5
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 1
store i64 0, i64* %17
%18 = call i32 @snprintf(i8* %9, i32 33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
%19 = icmp eq i8* %0, null
br i1 %19, label %24, label %20
20: ; preds = %2
%21 = load i8, i8* %0
%22 = icmp eq i8 %21, 0
%23 = select i1 %22, i8* null, i8* %0
br label %24
24: ; preds = %20, %2
%25 = phi i8* [ null, %2 ], [ %23, %20 ]
%26 = call i32 @getaddrinfo(i8* %25, i8* %9, %struct.addrinfo* %3, %struct.addrinfo** %4)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %98
28: ; preds = %24
%29 = load %struct.addrinfo*, %struct.addrinfo** %4
%30 = icmp eq %struct.addrinfo* %29, null
br i1 %30, label %92, label %31
31: ; preds = %28, %88
%32 = phi %struct.addrinfo* [ %90, %88 ], [ %29, %28 ]
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i64 0, i32 1
%34 = load i64, i64* %33
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i64 0, i32 5
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i64 0, i32 4
%38 = load i32, i32* %37
%39 = call i32 @socket(i64 %34, i32 %36, i32 %38)
%40 = icmp slt i32 %39, 0
br i1 %40, label %88, label %41
41: ; preds = %31
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i64 0, i32 1
%43 = load i32, i32* @SOL_SOCKET
%44 = load i32, i32* @SO_REUSEADDR
%45 = call i32 @setsockopt(i32 %39, i32 %43, i32 %44, i8* bitcast (i32* @try_port.on to i8*), i32 4)
%46 = load i64, i64* %42
%47 = load i64, i64* @PF_INET6
%48 = icmp eq i64 %46, %47
br i1 %48, label %49, label %53
49: ; preds = %41
%50 = load i32, i32* @IPPROTO_IPV6
%51 = load i32, i32* @IPV6_V6ONLY
%52 = call i32 @setsockopt(i32 %39, i32 %50, i32 %51, i8* bitcast (i32* @try_port.off to i8*), i32 4)
br label %53
53: ; preds = %49, %41
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i64 0, i32 3
%55 = load i64, i64* %54
%56 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i64 0, i32 2
%57 = load i32, i32* %56
%58 = call i64 @bind(i32 %39, i64 %55, i32 %57)
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %86
60: ; preds = %53
%61 = load i32, i32* @INET6_ADDRSTRLEN
%62 = add nsw i32 %61, 1
%63 = zext i32 %62 to i64
%64 = call i8* @llvm.stacksave()
%65 = alloca i8, i64 %63
%66 = getelementptr inbounds [33 x i8], [33 x i8]* %6, i64 0, i64 0
%67 = load i64, i64* %54
%68 = inttoptr i64 %67 to %struct.sockaddr*
%69 = load i32, i32* %56
%70 = load i32, i32* @INET6_ADDRSTRLEN
%71 = load i32, i32* @NI_NUMERICHOST
%72 = load i32, i32* @NI_NUMERICSERV
%73 = or i32 %72, %71
%74 = call i32 @getnameinfo(%struct.sockaddr* %68, i32 %69, i8* %65, i32 %70, i8* %66, i32 32, i32 %73)
%75 = icmp eq i32 %74, 0
br i1 %75, label %76, label %78
76: ; preds = %60
%77 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %65, i8* %66)
br label %80
78: ; preds = %60
%79 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
br label %80
80: ; preds = %78, %76
%81 = load %struct.addrinfo*, %struct.addrinfo** %4
%82 = call i32 @freeaddrinfo(%struct.addrinfo* %81)
%83 = load i32, i32* @SOMAXCONN
%84 = call i64 @listen(i32 %39, i32 %83)
%85 = icmp eq i64 %84, 0
br i1 %85, label %98, label %95
86: ; preds = %53
%87 = call i32 @close(i32 %39)
br label %92
88: ; preds = %31
%89 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i64 0, i32 6
%90 = load %struct.addrinfo*, %struct.addrinfo** %89
%91 = icmp eq %struct.addrinfo* %90, null
br i1 %91, label %92, label %31
92: ; preds = %88, %28, %86
%93 = load %struct.addrinfo*, %struct.addrinfo** %4
%94 = call i32 @freeaddrinfo(%struct.addrinfo* %93)
br label %98
95: ; preds = %80
%96 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%97 = call i32 @close(i32 %39)
br label %98
98: ; preds = %80, %24, %95, %92
%99 = phi i32 [ -1, %95 ], [ -1, %92 ], [ -1, %24 ], [ %39, %80 ]
ret i32 %99
}