#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;
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ log_ret(char *, ...);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_and_bind(int port) {
  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 *result = NULL;
  int addr_info = -1;
  char port_str[10] = {'\0'};
  snprintf(port_str, 10 - 1, "%d", port);
  if ((addr_info = getaddrinfo(NULL, port_str, &hints, &result)) != 0) {
    log_ret("create_and_bind: getaddrinfo: %s\n", gai_strerror(addr_info));
    return -1;
  }
  int server_fd = -1;
  struct addrinfo *iterator = NULL;
  for (iterator = result; iterator != NULL; iterator = iterator->ai_next) {
    server_fd = socket(iterator->ai_family, iterator->ai_socktype,
                       iterator->ai_protocol);
    if (server_fd == -1) {
      continue;
    };
    if ((bind(server_fd, iterator->ai_addr, iterator->ai_addrlen)) == 0) {
      break;
    }
    close(server_fd);
  }
  freeaddrinfo(result);
  if (iterator == NULL) {
    log_ret("create_and_bind: bind failed");
    return -1;
  }
  return server_fd;
}


x86 assembly (clang, -O0):
<create_and_bind>:
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    0x2e2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
lea    -0x46(%rbp),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <memset@plt>
lea    -0x46(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x9,%esi
lea    0xdff(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x46(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
je     124b <create_and_bind+0xbb>
mov    -0x3c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    %eax,%esi
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1080 <log_ret@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130a <create_and_bind+0x17a>
movl   $0xffffffff,-0x4c(%rbp)
movq   $0x0,-0x58(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     12d6 <create_and_bind+0x146>
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x58(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0xffffffff,-0x4c(%rbp)
jne    1299 <create_and_bind+0x109>
jmp    12c5 <create_and_bind+0x135>
mov    -0x4c(%rbp),%edi
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x58(%rbp),%rax
mov    (%rax),%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jne    12bd <create_and_bind+0x12d>
jmp    12d6 <create_and_bind+0x146>
mov    -0x4c(%rbp),%edi
call   1060 <close@plt>
mov    -0x58(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    1262 <create_and_bind+0xd2>
mov    -0x38(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
cmpq   $0x0,-0x58(%rbp)
jne    1304 <create_and_bind+0x174>
lea    0xd34(%rip),%rdi
mov    $0x0,%al
call   1080 <log_ret@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130a <create_and_bind+0x17a>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_and_bind>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %edi,%ebp
lea    0x18(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
movq   $0x0,(%rsp)
movw   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0xe0c(%rip),%rdx
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x9,%esi
mov    %ebp,%ecx
call   1030 <snprintf@plt>
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     1235 <create_and_bind+0xa5>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xdd9(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <log_ret@plt>
jmp    12a0 <create_and_bind+0x110>
mov    (%rsp),%rbp
test   %rbp,%rbp
jne    1259 <create_and_bind+0xc9>
xor    %edi,%edi
jmp    128d <create_and_bind+0xfd>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x18(%rbp),%rbp
test   %rbp,%rbp
je     1289 <create_and_bind+0xf9>
mov    0x10(%rbp),%edi
mov    0x8(%rbp),%edx
mov    0xc(%rbp),%esi
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1250 <create_and_bind+0xc0>
mov    %eax,%ebx
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
mov    %eax,%edi
call   1070 <bind@plt>
test   %rax,%rax
je     12b0 <create_and_bind+0x120>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    1250 <create_and_bind+0xc0>
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
lea    0xd8c(%rip),%rdi
xor    %eax,%eax
call   1080 <log_ret@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
jmp    12a5 <create_and_bind+0x115>

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"
@.str.1 = [34 x i8] c"create_and_bind: getaddrinfo: %s\0A\00"
@.str.2 = [29 x i8] c"create_and_bind: bind failed\00"
define i32 @create_and_bind(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = alloca i32
%7 = alloca [10 x i8]
%8 = alloca i32
%9 = alloca %struct.addrinfo*
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
store %struct.addrinfo* null, %struct.addrinfo** %5
store i32 -1, i32* %6
%17 = bitcast [10 x i8]* %7 to i8*
%18 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%19 = load i32, i32* %3
%20 = call i32 @snprintf(i8* %18, i32 9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
%22 = call i32 @getaddrinfo(i32* null, i8* %21, %struct.addrinfo* %4, %struct.addrinfo** %5)
store i32 %22, i32* %6
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %1
%25 = load i32, i32* %6
%26 = call i32 @gai_strerror(i32 %25)
%27 = call i32 (i8*, ...) @log_ret(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %26)
store i32 -1, i32* %2
br label %74
28: ; preds = %1
store i32 -1, i32* %8
store %struct.addrinfo* null, %struct.addrinfo** %9
%29 = load %struct.addrinfo*, %struct.addrinfo** %5
store %struct.addrinfo* %29, %struct.addrinfo** %9
br label %30
30: ; preds = %61, %28
%31 = load %struct.addrinfo*, %struct.addrinfo** %9
%32 = icmp ne %struct.addrinfo* %31, null
br i1 %32, label %33, label %65
33: ; preds = %30
%34 = load %struct.addrinfo*, %struct.addrinfo** %9
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %34, i32 0, i32 4
%36 = load i32, i32* %35
%37 = load %struct.addrinfo*, %struct.addrinfo** %9
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 3
%39 = load i32, i32* %38
%40 = load %struct.addrinfo*, %struct.addrinfo** %9
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 2
%42 = load i32, i32* %41
%43 = call i32 @socket(i32 %36, i32 %39, i32 %42)
store i32 %43, i32* %8
%44 = load i32, i32* %8
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %47
46: ; preds = %33
br label %61
47: ; preds = %33
%48 = load i32, i32* %8
%49 = load %struct.addrinfo*, %struct.addrinfo** %9
%50 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %49, i32 0, i32 1
%51 = load i32, i32* %50
%52 = load %struct.addrinfo*, %struct.addrinfo** %9
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %52, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i64 @bind(i32 %48, i32 %51, i32 %54)
%56 = icmp eq i64 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %47
br label %65
58: ; preds = %47
%59 = load i32, i32* %8
%60 = call i32 @close(i32 %59)
br label %61
61: ; preds = %58, %46
%62 = load %struct.addrinfo*, %struct.addrinfo** %9
%63 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %62, i32 0, i32 5
%64 = load %struct.addrinfo*, %struct.addrinfo** %63
store %struct.addrinfo* %64, %struct.addrinfo** %9
br label %30
65: ; preds = %57, %30
%66 = load %struct.addrinfo*, %struct.addrinfo** %5
%67 = call i32 @freeaddrinfo(%struct.addrinfo* %66)
%68 = load %struct.addrinfo*, %struct.addrinfo** %9
%69 = icmp eq %struct.addrinfo* %68, null
br i1 %69, label %70, label %72
70: ; preds = %65
%71 = call i32 (i8*, ...) @log_ret(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %74
72: ; preds = %65
%73 = load i32, i32* %8
store i32 %73, i32* %2
br label %74
74: ; preds = %72, %70, %24
%75 = load i32, i32* %2
ret i32 %75
}

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"
@.str.1 = [34 x i8] c"create_and_bind: getaddrinfo: %s\0A\00"
@.str.2 = [29 x i8] c"create_and_bind: bind failed\00"
define i32 @create_and_bind(i32 %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = alloca [10 x i8]
%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*
store %struct.addrinfo* null, %struct.addrinfo** %3
%14 = getelementptr inbounds [10 x i8], [10 x i8]* %4, i64 0, i64 0
%15 = call i32 @snprintf(i8* %14, i32 9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
%16 = call i32 @getaddrinfo(i32* null, i8* %14, %struct.addrinfo* %2, %struct.addrinfo** %3)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %1
%19 = load %struct.addrinfo*, %struct.addrinfo** %3
%20 = icmp eq %struct.addrinfo* %19, null
br i1 %20, label %52, label %24
21: ; preds = %1
%22 = call i32 @gai_strerror(i32 %16)
%23 = call i32 (i8*, ...) @log_ret(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %22)
br label %56
24: ; preds = %18, %43
%25 = phi %struct.addrinfo* [ %45, %43 ], [ %19, %18 ]
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 4
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 3
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 2
%31 = load i32, i32* %30
%32 = call i32 @socket(i32 %27, i32 %29, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %43, label %34
34: ; preds = %24
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 1
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 0
%38 = load i32, i32* %37
%39 = call i64 @bind(i32 %32, i32 %36, i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %47, label %41
41: ; preds = %34
%42 = call i32 @close(i32 %32)
br label %43
43: ; preds = %24, %41
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 5
%45 = load %struct.addrinfo*, %struct.addrinfo** %44
%46 = icmp eq %struct.addrinfo* %45, null
br i1 %46, label %50, label %24
47: ; preds = %34
%48 = load %struct.addrinfo*, %struct.addrinfo** %3
%49 = call i32 @freeaddrinfo(%struct.addrinfo* %48)
br label %56
50: ; preds = %43
%51 = load %struct.addrinfo*, %struct.addrinfo** %3
br label %52
52: ; preds = %50, %18
%53 = phi %struct.addrinfo* [ %51, %50 ], [ null, %18 ]
%54 = call i32 @freeaddrinfo(%struct.addrinfo* %53)
%55 = call i32 (i8*, ...) @log_ret(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %56
56: ; preds = %47, %52, %21
%57 = phi i32 [ -1, %21 ], [ -1, %52 ], [ %32, %47 ]
ret i32 %57
}