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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int probe_local_addr(int listen_port) {
  struct addrinfo hint_addr, *res_addr;
  memset(&hint_addr, 0, sizeof hint_addr);
  hint_addr.ai_family = AF_UNSPEC;
  hint_addr.ai_socktype = SOCK_STREAM;
  hint_addr.ai_flags = AI_PASSIVE;
  char port_string[6];
  snprintf(port_string, 5, "%d", listen_port);
  if (getaddrinfo(NULL, port_string, &hint_addr, &res_addr) != 0) {
    fprintf(stderr, "server: error getting address\n");
    return -1;
  }
  int listen_fd = -1;
  struct addrinfo *p_addr;
  for (p_addr = res_addr; p_addr != NULL; p_addr = p_addr->ai_next) {
    listen_fd =
        socket(p_addr->ai_family, p_addr->ai_socktype, p_addr->ai_protocol);
    if (listen_fd < 0)
      continue;
    bool yes = true;
    setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes);
    if (bind(listen_fd, p_addr->ai_addr, p_addr->ai_addrlen) < 0) {
      perror("bind");
      close(listen_fd);
      continue;
    }
    break;
  }
  if (p_addr == NULL)
    return -2;
  freeaddrinfo(res_addr);
  return listen_fd;
}


x86 assembly (clang, -O0):
<probe_local_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    -0x3e(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x5,%esi
lea    0xe0e(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x3e(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
cmp    $0x0,%rax
je     1237 <probe_local_addr+0x97>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    131c <probe_local_addr+0x17c>
movl   $0xffffffff,-0x44(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     12f6 <probe_local_addr+0x156>
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    127d <probe_local_addr+0xdd>
jmp    12e5 <probe_local_addr+0x145>
movl   $0x1,-0x54(%rbp)
mov    -0x44(%rbp),%edi
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
mov    0x2d21(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x44(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    12e0 <probe_local_addr+0x140>
lea    0xd54(%rip),%rdi
call   1090 <perror@plt>
mov    -0x44(%rbp),%edi
call   1060 <close@plt>
jmp    12e5 <probe_local_addr+0x145>
jmp    12f6 <probe_local_addr+0x156>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1246 <probe_local_addr+0xa6>
cmpq   $0x0,-0x50(%rbp)
jne    130d <probe_local_addr+0x16d>
movl   $0xfffffffe,-0x4(%rbp)
jmp    131c <probe_local_addr+0x17c>
mov    -0x38(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<probe_local_addr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %edi,%ebp
lea    0x20(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x40(%rsp)
lea    0xe0e(%rip),%rdx
lea    0x1a(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x5,%esi
mov    %ebp,%ecx
call   1040 <snprintf@plt>
lea    0x10(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   10a0 <getaddrinfo@plt>
test   %rax,%rax
je     123c <probe_local_addr+0x9c>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    12de <probe_local_addr+0x13e>
mov    0x10(%rsp),%rbx
test   %rbx,%rbx
je     12d9 <probe_local_addr+0x139>
mov    0x2d97(%rip),%r12
mov    0x2d60(%rip),%r13
lea    0xc(%rsp),%r15
lea    0xdbe(%rip),%r14
jmp    1288 <probe_local_addr+0xe8>
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1090 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     12d9 <probe_local_addr+0x139>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10c0 <socket@plt>
test   %eax,%eax
js     127f <probe_local_addr+0xdf>
mov    %eax,%ebp
movl   $0x1,0xc(%rsp)
mov    (%r12),%esi
mov    0x0(%r13),%edx
mov    %eax,%edi
mov    %r15,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1080 <bind@plt>
test   %rax,%rax
js     1270 <probe_local_addr+0xd0>
mov    0x10(%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
jmp    12de <probe_local_addr+0x13e>
mov    $0xfffffffe,%ebp
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"server: error getting address\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [5 x i8] c"bind\00"
define i32 @probe_local_addr(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = alloca [6 x i8]
%7 = alloca i32
%8 = alloca %struct.addrinfo*
%9 = alloca i32
store i32 %0, i32* %3
%10 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 40)
%11 = load i32, i32* @AF_UNSPEC
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 4
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 3
store i32 %13, i32* %14
%15 = load i32, i32* @AI_PASSIVE
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 6
store i32 %15, i32* %16
%17 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 0
%18 = load i32, i32* %3
%19 = call i32 @snprintf(i8* %17, i32 5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = getelementptr inbounds [6 x i8], [6 x i8]* %6, i64 0, i64 0
%21 = call i64 @getaddrinfo(i32* null, i8* %20, %struct.addrinfo* %4, %struct.addrinfo** %5)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %1
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %76
26: ; preds = %1
store i32 -1, i32* %7
%27 = load %struct.addrinfo*, %struct.addrinfo** %5
store %struct.addrinfo* %27, %struct.addrinfo** %8
br label %28
28: ; preds = %64, %26
%29 = load %struct.addrinfo*, %struct.addrinfo** %8
%30 = icmp ne %struct.addrinfo* %29, null
br i1 %30, label %31, label %68
31: ; preds = %28
%32 = load %struct.addrinfo*, %struct.addrinfo** %8
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 4
%34 = load i32, i32* %33
%35 = load %struct.addrinfo*, %struct.addrinfo** %8
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 3
%37 = load i32, i32* %36
%38 = load %struct.addrinfo*, %struct.addrinfo** %8
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 2
%40 = load i32, i32* %39
%41 = call i32 @socket(i32 %34, i32 %37, i32 %40)
store i32 %41, i32* %7
%42 = load i32, i32* %7
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %31
br label %64
45: ; preds = %31
store i32 1, i32* %9
%46 = load i32, i32* %7
%47 = load i32, i32* @SOL_SOCKET
%48 = load i32, i32* @SO_REUSEADDR
%49 = call i32 @setsockopt(i32 %46, i32 %47, i32 %48, i32* %9, i32 4)
%50 = load i32, i32* %7
%51 = load %struct.addrinfo*, %struct.addrinfo** %8
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 1
%53 = load i32, i32* %52
%54 = load %struct.addrinfo*, %struct.addrinfo** %8
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i64 @bind(i32 %50, i32 %53, i32 %56)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %63
59: ; preds = %45
%60 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%61 = load i32, i32* %7
%62 = call i32 @close(i32 %61)
br label %64
63: ; preds = %45
br label %68
64: ; preds = %59, %44
%65 = load %struct.addrinfo*, %struct.addrinfo** %8
%66 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %65, i32 0, i32 5
%67 = load %struct.addrinfo*, %struct.addrinfo** %66
store %struct.addrinfo* %67, %struct.addrinfo** %8
br label %28
68: ; preds = %63, %28
%69 = load %struct.addrinfo*, %struct.addrinfo** %8
%70 = icmp eq %struct.addrinfo* %69, null
br i1 %70, label %71, label %72
71: ; preds = %68
store i32 -2, i32* %2
br label %76
72: ; preds = %68
%73 = load %struct.addrinfo*, %struct.addrinfo** %5
%74 = call i32 @freeaddrinfo(%struct.addrinfo* %73)
%75 = load i32, i32* %7
store i32 %75, i32* %2
br label %76
76: ; preds = %72, %71, %23
%77 = load i32, i32* %2
ret i32 %77
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"server: error getting address\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [5 x i8] c"bind\00"
define i32 @probe_local_addr(i32 %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = alloca [6 x i8]
%5 = alloca i32
%6 = bitcast %struct.addrinfo* %2 to i8*
%7 = bitcast %struct.addrinfo** %3 to i8*
%8 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 40)
%9 = load i32, i32* @AF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %11, i32* %12
%13 = load i32, i32* @AI_PASSIVE
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 6
store i32 %13, i32* %14
%15 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%16 = call i32 @snprintf(i8* %15, i32 5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
%17 = call i64 @getaddrinfo(i32* null, i8* %15, %struct.addrinfo* %2, %struct.addrinfo** %3)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %1
%20 = load %struct.addrinfo*, %struct.addrinfo** %3
%21 = icmp eq %struct.addrinfo* %20, null
br i1 %21, label %57, label %22
22: ; preds = %19
%23 = bitcast i32* %5 to i8*
br label %27
24: ; preds = %1
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %57
27: ; preds = %22, %50
%28 = phi %struct.addrinfo* [ %20, %22 ], [ %52, %50 ]
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 4
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 3
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 2
%34 = load i32, i32* %33
%35 = call i32 @socket(i32 %30, i32 %32, i32 %34)
%36 = icmp slt i32 %35, 0
br i1 %36, label %50, label %37
37: ; preds = %27
store i32 1, i32* %5
%38 = load i32, i32* @SOL_SOCKET
%39 = load i32, i32* @SO_REUSEADDR
%40 = call i32 @setsockopt(i32 %35, i32 %38, i32 %39, i32* %5, i32 4)
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 1
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 0
%44 = load i32, i32* %43
%45 = call i64 @bind(i32 %35, i32 %42, i32 %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %54
47: ; preds = %37
%48 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%49 = call i32 @close(i32 %35)
br label %50
50: ; preds = %47, %27
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i64 0, i32 5
%52 = load %struct.addrinfo*, %struct.addrinfo** %51
%53 = icmp eq %struct.addrinfo* %52, null
br i1 %53, label %57, label %27
54: ; preds = %37
%55 = load %struct.addrinfo*, %struct.addrinfo** %3
%56 = call i32 @freeaddrinfo(%struct.addrinfo* %55)
br label %57
57: ; preds = %50, %19, %54, %24
%58 = phi i32 [ -1, %24 ], [ %35, %54 ], [ -2, %19 ], [ -2, %50 ]
ret i32 %58
}