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

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_KEEPALIVE;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int bindToAddress(struct addrinfo *addr) {
  int sfd = 0;
  int optVal = 1;
  if (!addr)
    return 0;
  else if ((sfd = socket(addr->ai_family, addr->ai_socktype,
                         addr->ai_protocol)) != -1 &&
           setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &optVal, sizeof(int)) !=
               -1 &&
           setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &optVal, sizeof(int)) !=
               -1 &&
           bind(sfd, addr->ai_addr, addr->ai_addrlen) != -1) {
    return sfd;
  } else {
    perror("BindToAddress");
    close(sfd);
    return bindToAddress(addr->ai_next);
  }
}


x86 assembly (clang, -O0):
<bindToAddress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1191 <bindToAddress+0x31>
movl   $0x0,-0x4(%rbp)
jmp    125e <bindToAddress+0xfe>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x14(%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     123b <bindToAddress+0xdb>
mov    -0x14(%rbp),%edi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     123b <bindToAddress+0xdb>
mov    -0x14(%rbp),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     123b <bindToAddress+0xdb>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
je     123b <bindToAddress+0xdb>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    125e <bindToAddress+0xfe>
lea    0xdbe(%rip),%rdi
call   1060 <perror@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1070 <bindToAddress@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bindToAddress>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0x1,0xc(%rsp)
test   %rdi,%rdi
je     120f <bindToAddress+0xaf>
mov    %rdi,%rbx
mov    0x18(%rdi),%edi
mov    0x10(%rbx),%edx
mov    0x14(%rbx),%esi
call   1080 <socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11f0 <bindToAddress+0x90>
mov    0x2e50(%rip),%r14
mov    (%r14),%esi
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     11f0 <bindToAddress+0x90>
mov    (%r14),%esi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     11f0 <bindToAddress+0x90>
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
mov    %ebp,%edi
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
jne    1211 <bindToAddress+0xb1>
lea    0xe09(%rip),%rdi
call   1060 <perror@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    (%rbx),%rdi
call   1070 <bindToAddress@plt>
mov    %eax,%ebp
jmp    1211 <bindToAddress+0xb1>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { %struct.addrinfo*, i32, i32, i32, i32, i32 }
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@SO_KEEPALIVE = global i32 0
@.str = [14 x i8] c"BindToAddress\00"
define i32 @bindToAddress(%struct.addrinfo* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%4 = icmp eq %struct.addrinfo* %0, null
br i1 %4, label %37, label %5
5: ; preds = %1
%6 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 5
%7 = load i32, i32* %6
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 4
%9 = load i32, i32* %8
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 3
%11 = load i32, i32* %10
%12 = tail call i32 @socket(i32 %7, i32 %9, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %31, label %14
14: ; preds = %5
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_REUSEADDR
%17 = call i32 @setsockopt(i32 %12, i32 %15, i32 %16, i32* %2, i32 4)
%18 = icmp eq i32 %17, -1
br i1 %18, label %31, label %19
19: ; preds = %14
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_KEEPALIVE
%22 = call i32 @setsockopt(i32 %12, i32 %20, i32 %21, i32* %2, i32 4)
%23 = icmp eq i32 %22, -1
br i1 %23, label %31, label %24
24: ; preds = %19
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 2
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 1
%28 = load i32, i32* %27
%29 = call i32 @bind(i32 %12, i32 %26, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %37
31: ; preds = %24, %19, %14, %5
%32 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%33 = call i32 @close(i32 %12)
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %0, i64 0, i32 0
%35 = load %struct.addrinfo*, %struct.addrinfo** %34
%36 = call i32 @bindToAddress(%struct.addrinfo* %35)
br label %37
37: ; preds = %24, %1, %31
%38 = phi i32 [ %36, %31 ], [ 0, %1 ], [ %12, %24 ]
ret i32 %38
}