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

/* 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*/ exit(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ rsp_log(char *, ...);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_and_bind(char *server_port_str) {
  struct addrinfo hints;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  struct addrinfo *addrs;
  int getaddrinfo_error;
  getaddrinfo_error = getaddrinfo(NULL, server_port_str, &hints, &addrs);
  if (getaddrinfo_error != 0) {
    rsp_log("Couldn't find local host details: %s",
            gai_strerror(getaddrinfo_error));
    exit(1);
  }
  int server_socket_fd;
  struct addrinfo *addr_iter;
  for (addr_iter = addrs; addr_iter != NULL; addr_iter = addr_iter->ai_next) {
    server_socket_fd = socket(addr_iter->ai_family, addr_iter->ai_socktype,
                              addr_iter->ai_protocol);
    if (server_socket_fd == -1) {
      continue;
    }
    int so_reuseaddr = 1;
    if (setsockopt(server_socket_fd, SOL_SOCKET, SO_REUSEADDR, &so_reuseaddr,
                   sizeof(so_reuseaddr)) != 0) {
      continue;
    }
    if (bind(server_socket_fd, addr_iter->ai_addr, addr_iter->ai_addrlen) ==
        0) {
      break;
    }
    close(server_socket_fd);
  }
  if (addr_iter == NULL) {
    rsp_log("Couldn't bind");
    exit(1);
  }
  freeaddrinfo(addrs);
  return server_socket_fd;
}


x86 assembly (clang, -O0):
<create_and_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     1224 <create_and_bind+0x84>
mov    -0x3c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    %eax,%esi
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1080 <rsp_log@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12da <create_and_bind+0x13a>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    1263 <create_and_bind+0xc3>
jmp    12c9 <create_and_bind+0x129>
movl   $0x1,-0x4c(%rbp)
mov    -0x40(%rbp),%edi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
mov    0x2d43(%rip),%rax
mov    (%rax),%edx
lea    -0x4c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     129d <create_and_bind+0xfd>
jmp    12c9 <create_and_bind+0x129>
mov    -0x40(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jne    12c1 <create_and_bind+0x121>
jmp    12da <create_and_bind+0x13a>
mov    -0x40(%rbp),%edi
call   1060 <close@plt>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    122c <create_and_bind+0x8c>
cmpq   $0x0,-0x48(%rbp)
jne    12fd <create_and_bind+0x15d>
lea    0xd39(%rip),%rdi
mov    $0x0,%al
call   1080 <rsp_log@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x38(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x40(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_and_bind>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x40,%rsp
mov    %rdi,%rbx
lea    0x18(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
lea    0x10(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
jne    12bd <create_and_bind+0x11d>
mov    0x10(%rsp),%rbx
test   %rbx,%rbx
je     12a5 <create_and_bind+0x105>
mov    0x2dcf(%rip),%r15
mov    0x2da0(%rip),%r12
lea    0xc(%rsp),%r14
jmp    1239 <create_and_bind+0x99>
nopw   0x0(%rax,%rax,1)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     12a5 <create_and_bind+0x105>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     1230 <create_and_bind+0x90>
mov    %eax,%ebp
movl   $0x1,0xc(%rsp)
mov    (%r15),%esi
mov    (%r12),%edx
mov    %eax,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
jne    1230 <create_and_bind+0x90>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1070 <bind@plt>
test   %rax,%rax
je     128c <create_and_bind+0xec>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1230 <create_and_bind+0x90>
mov    0x10(%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd79(%rip),%rdi
xor    %eax,%eax
call   1080 <rsp_log@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xd35(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <rsp_log@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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 = [37 x i8] c"Couldn't find local host details: %s\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [14 x i8] c"Couldn't bind\00"
define i32 @create_and_bind(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.addrinfo*
%8 = alloca i32
store i8* %0, i8** %2
%9 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 40)
%10 = load i32, i32* @AF_UNSPEC
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 4
store i32 %10, i32* %11
%12 = load i32, i32* @SOCK_STREAM
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 3
store i32 %12, i32* %13
%14 = load i32, i32* @AI_PASSIVE
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i32 0, i32 6
store i32 %14, i32* %15
%16 = load i8*, i8** %2
%17 = call i32 @getaddrinfo(i32* null, i8* %16, %struct.addrinfo* %3, %struct.addrinfo** %4)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %1
%21 = load i32, i32* %5
%22 = call i32 @gai_strerror(i32 %21)
%23 = call i32 (i8*, ...) @rsp_log(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %1
%26 = load %struct.addrinfo*, %struct.addrinfo** %4
store %struct.addrinfo* %26, %struct.addrinfo** %7
br label %27
27: ; preds = %65, %25
%28 = load %struct.addrinfo*, %struct.addrinfo** %7
%29 = icmp ne %struct.addrinfo* %28, null
br i1 %29, label %30, label %69
30: ; preds = %27
%31 = load %struct.addrinfo*, %struct.addrinfo** %7
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 4
%33 = load i32, i32* %32
%34 = load %struct.addrinfo*, %struct.addrinfo** %7
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 3
%36 = load i32, i32* %35
%37 = load %struct.addrinfo*, %struct.addrinfo** %7
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 2
%39 = load i32, i32* %38
%40 = call i32 @socket(i32 %33, i32 %36, i32 %39)
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %44
43: ; preds = %30
br label %65
44: ; preds = %30
store i32 1, i32* %8
%45 = load i32, i32* %6
%46 = load i32, i32* @SOL_SOCKET
%47 = load i32, i32* @SO_REUSEADDR
%48 = call i64 @setsockopt(i32 %45, i32 %46, i32 %47, i32* %8, i32 4)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %44
br label %65
51: ; preds = %44
%52 = load i32, i32* %6
%53 = load %struct.addrinfo*, %struct.addrinfo** %7
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 1
%55 = load i32, i32* %54
%56 = load %struct.addrinfo*, %struct.addrinfo** %7
%57 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i64 @bind(i32 %52, i32 %55, i32 %58)
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %62
61: ; preds = %51
br label %69
62: ; preds = %51
%63 = load i32, i32* %6
%64 = call i32 @close(i32 %63)
br label %65
65: ; preds = %62, %50, %43
%66 = load %struct.addrinfo*, %struct.addrinfo** %7
%67 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %66, i32 0, i32 5
%68 = load %struct.addrinfo*, %struct.addrinfo** %67
store %struct.addrinfo* %68, %struct.addrinfo** %7
br label %27
69: ; preds = %61, %27
%70 = load %struct.addrinfo*, %struct.addrinfo** %7
%71 = icmp eq %struct.addrinfo* %70, null
br i1 %71, label %72, label %75
72: ; preds = %69
%73 = call i32 (i8*, ...) @rsp_log(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%74 = call i32 @exit(i32 1)
unreachable
75: ; preds = %69
%76 = load %struct.addrinfo*, %struct.addrinfo** %4
%77 = call i32 @freeaddrinfo(%struct.addrinfo* %76)
%78 = load i32, i32* %6
ret i32 %78
}

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 = [37 x i8] c"Couldn't find local host details: %s\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [14 x i8] c"Couldn't bind\00"
define i32 @create_and_bind(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = alloca i32
%5 = bitcast %struct.addrinfo* %2 to i8*
%6 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 40)
%7 = load i32, i32* @AF_UNSPEC
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %7, i32* %8
%9 = load i32, i32* @SOCK_STREAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %9, i32* %10
%11 = load i32, i32* @AI_PASSIVE
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 6
store i32 %11, i32* %12
%13 = bitcast %struct.addrinfo** %3 to i8*
%14 = call i32 @getaddrinfo(i32* null, i8* %0, %struct.addrinfo* %2, %struct.addrinfo** %3)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %1
%17 = load %struct.addrinfo*, %struct.addrinfo** %3
%18 = icmp eq %struct.addrinfo* %17, null
br i1 %18, label %54, label %19
19: ; preds = %16
%20 = bitcast i32* %4 to i8*
br label %25
21: ; preds = %1
%22 = call i32 @gai_strerror(i32 %14)
%23 = call i32 (i8*, ...) @rsp_log(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %19, %50
%26 = phi %struct.addrinfo* [ %17, %19 ], [ %52, %50 ]
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 4
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 3
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 2
%32 = load i32, i32* %31
%33 = call i32 @socket(i32 %28, i32 %30, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %50, label %35
35: ; preds = %25
store i32 1, i32* %4
%36 = load i32, i32* @SOL_SOCKET
%37 = load i32, i32* @SO_REUSEADDR
%38 = call i64 @setsockopt(i32 %33, i32 %36, i32 %37, i32* %4, i32 4)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %49
40: ; preds = %35
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 1
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 0
%44 = load i32, i32* %43
%45 = call i64 @bind(i32 %33, i32 %42, i32 %44)
%46 = icmp eq i64 %45, 0
br i1 %46, label %57, label %47
47: ; preds = %40
%48 = call i32 @close(i32 %33)
br label %49
49: ; preds = %47, %35
br label %50
50: ; preds = %49, %25
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 5
%52 = load %struct.addrinfo*, %struct.addrinfo** %51
%53 = icmp eq %struct.addrinfo* %52, null
br i1 %53, label %54, label %25
54: ; preds = %50, %16
%55 = call i32 (i8*, ...) @rsp_log(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %40
%58 = load %struct.addrinfo*, %struct.addrinfo** %3
%59 = call i32 @freeaddrinfo(%struct.addrinfo* %58)
ret i32 %33
}