#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;
};
typedef int /*<<< orphan*/ optval;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int /*<<< orphan*/ show_network_error(char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int get_address(struct addrinfo hints) {
  struct addrinfo *info, *p;
  int s;
  int ret = getaddrinfo(NULL, "12397", &hints, &info);
  if (ret != 0) {
    show_network_error("Could not get address info", ret);
    return -1;
  }
  for (p = info; p != NULL; p = p->ai_next) {
    s = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
    if (s == -1) {
      perror("Could not create socket");
      continue;
    }
    int optval = 1;
    setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
    if (optval != 1) {
      perror("Could not set socket to reuse address");
      continue;
    }
    if (bind(s, p->ai_addr, p->ai_addrlen) == -1) {
      close(s);
      perror("Could not bind to socket");
      continue;
    }
    break;
  }
  freeaddrinfo(info);
  if (p == NULL)
    return -1;
  else
    return s;
}


x86 assembly (clang, -O0):
<get_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0x10(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%edi
lea    0xe69(%rip),%rsi
lea    -0x10(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11c8 <get_address+0x48>
mov    -0x20(%rbp),%esi
lea    0xe4f(%rip),%rdi
call   1050 <show_network_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cc <get_address+0x14c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     12a6 <get_address+0x126>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x18(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1213 <get_address+0x93>
lea    0xe18(%rip),%rdi
call   1070 <perror@plt>
jmp    1295 <get_address+0x115>
movl   $0x1,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmpl   $0x1,-0x24(%rbp)
je     1259 <get_address+0xd9>
lea    0xdea(%rip),%rdi
call   1070 <perror@plt>
jmp    1295 <get_address+0x115>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    1290 <get_address+0x110>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
lea    0xdd9(%rip),%rdi
call   1070 <perror@plt>
jmp    1295 <get_address+0x115>
jmp    12a6 <get_address+0x126>
mov    -0x18(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11d0 <get_address+0x50>
mov    -0x10(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
cmpq   $0x0,-0x18(%rbp)
jne    12c6 <get_address+0x146>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cc <get_address+0x14c>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_address>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe6b(%rip),%rsi
lea    0x50(%rsp),%rdx
lea    0x10(%rsp),%rcx
xor    %edi,%edi
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     11c2 <get_address+0x42>
lea    0xe55(%rip),%rdi
mov    %eax,%esi
call   1050 <show_network_error@plt>
mov    $0xffffffff,%ebp
jmp    1270 <get_address+0xf0>
mov    0x10(%rsp),%rbx
test   %rbx,%rbx
je     1261 <get_address+0xe1>
mov    0x2e11(%rip),%r12
mov    0x2dfa(%rip),%r13
lea    0xc(%rsp),%r15
lea    0xe4f(%rip),%r14
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1258 <get_address+0xd8>
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>
cmpl   $0x1,0xc(%rsp)
mov    %r14,%rdi
jne    1248 <get_address+0xc8>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1060 <bind@plt>
cmp    $0xffffffff,%eax
jne    1266 <get_address+0xe6>
mov    %ebp,%edi
call   1040 <close@plt>
lea    0xe17(%rip),%rdi
call   1070 <perror@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    11ea <get_address+0x6a>
jmp    1261 <get_address+0xe1>
lea    0xdc2(%rip),%rdi
jmp    1248 <get_address+0xc8>
mov    $0xffffffff,%ebp
mov    0x10(%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x18,%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* }
@.str = [6 x i8] c"12397\00"
@.str.1 = [27 x i8] c"Could not get address info\00"
@.str.2 = [24 x i8] c"Could not create socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.3 = [38 x i8] c"Could not set socket to reuse address\00"
@.str.4 = [25 x i8] c"Could not bind to socket\00"
define i32 @get_address(%struct.addrinfo* byval(%struct.addrinfo) align 8 %0) {
%2 = alloca i32
%3 = alloca %struct.addrinfo*
%4 = alloca %struct.addrinfo*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = call i32 @getaddrinfo(i32* null, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %0, %struct.addrinfo** %3)
store i32 %8, i32* %6
%9 = load i32, i32* %6
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* %6
%13 = call i32 @show_network_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %12)
store i32 -1, i32* %2
br label %70
14: ; preds = %1
%15 = load %struct.addrinfo*, %struct.addrinfo** %3
store %struct.addrinfo* %15, %struct.addrinfo** %4
br label %16
16: ; preds = %58, %14
%17 = load %struct.addrinfo*, %struct.addrinfo** %4
%18 = icmp ne %struct.addrinfo* %17, null
br i1 %18, label %19, label %62
19: ; preds = %16
%20 = load %struct.addrinfo*, %struct.addrinfo** %4
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i32 0, i32 4
%22 = load i32, i32* %21
%23 = load %struct.addrinfo*, %struct.addrinfo** %4
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i32 0, i32 3
%25 = load i32, i32* %24
%26 = load %struct.addrinfo*, %struct.addrinfo** %4
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i32 0, i32 2
%28 = load i32, i32* %27
%29 = call i32 @socket(i32 %22, i32 %25, i32 %28)
store i32 %29, i32* %5
%30 = load i32, i32* %5
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %19
%33 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %58
34: ; preds = %19
store i32 1, i32* %7
%35 = load i32, i32* %5
%36 = load i32, i32* @SOL_SOCKET
%37 = load i32, i32* @SO_REUSEADDR
%38 = call i32 @setsockopt(i32 %35, i32 %36, i32 %37, i32* %7, i32 4)
%39 = load i32, i32* %7
%40 = icmp ne i32 %39, 1
br i1 %40, label %41, label %43
41: ; preds = %34
%42 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0))
br label %58
43: ; preds = %34
%44 = load i32, i32* %5
%45 = load %struct.addrinfo*, %struct.addrinfo** %4
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i32 0, i32 1
%47 = load i32, i32* %46
%48 = load %struct.addrinfo*, %struct.addrinfo** %4
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i32 @bind(i32 %44, i32 %47, i32 %50)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %57
53: ; preds = %43
%54 = load i32, i32* %5
%55 = call i32 @close(i32 %54)
%56 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %58
57: ; preds = %43
br label %62
58: ; preds = %53, %41, %32
%59 = load %struct.addrinfo*, %struct.addrinfo** %4
%60 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %59, i32 0, i32 5
%61 = load %struct.addrinfo*, %struct.addrinfo** %60
store %struct.addrinfo* %61, %struct.addrinfo** %4
br label %16
62: ; preds = %57, %16
%63 = load %struct.addrinfo*, %struct.addrinfo** %3
%64 = call i32 @freeaddrinfo(%struct.addrinfo* %63)
%65 = load %struct.addrinfo*, %struct.addrinfo** %4
%66 = icmp eq %struct.addrinfo* %65, null
br i1 %66, label %67, label %68
67: ; preds = %62
store i32 -1, i32* %2
br label %70
68: ; preds = %62
%69 = load i32, i32* %5
store i32 %69, i32* %2
br label %70
70: ; preds = %68, %67, %11
%71 = load i32, i32* %2
ret i32 %71
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@.str = [6 x i8] c"12397\00"
@.str.1 = [27 x i8] c"Could not get address info\00"
@.str.2 = [24 x i8] c"Could not create socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.3 = [38 x i8] c"Could not set socket to reuse address\00"
@.str.4 = [25 x i8] c"Could not bind to socket\00"
define i32 @get_address(%struct.addrinfo* byval(%struct.addrinfo) align 8 %0) {
%2 = alloca %struct.addrinfo*
%3 = alloca i32
%4 = bitcast %struct.addrinfo** %2 to i8*
%5 = call i32 @getaddrinfo(i32* null, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %0, %struct.addrinfo** %2)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load %struct.addrinfo*, %struct.addrinfo** %2
%9 = icmp eq %struct.addrinfo* %8, null
br i1 %9, label %49, label %10
10: ; preds = %7
%11 = bitcast i32* %3 to i8*
br label %14
12: ; preds = %1
%13 = call i32 @show_network_error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i32 %5)
br label %53
14: ; preds = %10, %45
%15 = phi %struct.addrinfo* [ %8, %10 ], [ %47, %45 ]
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 4
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 3
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 2
%21 = load i32, i32* %20
%22 = call i32 @socket(i32 %17, i32 %19, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %14
%25 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %45
26: ; preds = %14
store i32 1, i32* %3
%27 = load i32, i32* @SOL_SOCKET
%28 = load i32, i32* @SO_REUSEADDR
%29 = call i32 @setsockopt(i32 %22, i32 %27, i32 %28, i32* %3, i32 4)
%30 = load i32, i32* %3
%31 = icmp eq i32 %30, 1
br i1 %31, label %32, label %41
32: ; preds = %26
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 1
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 0
%36 = load i32, i32* %35
%37 = call i32 @bind(i32 %22, i32 %34, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %44
39: ; preds = %32
%40 = call i32 @close(i32 %22)
br label %41
41: ; preds = %26, %39
%42 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), %39 ], [ getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), %26 ]
%43 = call i32 @perror(i8* %42)
br label %45
44: ; preds = %32
br label %49
45: ; preds = %41, %24
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 5
%47 = load %struct.addrinfo*, %struct.addrinfo** %46
%48 = icmp eq %struct.addrinfo* %47, null
br i1 %48, label %49, label %14
49: ; preds = %45, %7, %44
%50 = phi i32 [ %22, %44 ], [ -1, %7 ], [ -1, %45 ]
%51 = load %struct.addrinfo*, %struct.addrinfo** %2
%52 = call i32 @freeaddrinfo(%struct.addrinfo* %51)
br label %53
53: ; preds = %49, %12
%54 = phi i32 [ -1, %12 ], [ %50, %49 ]
ret i32 %54
}