#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*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int /*<<< orphan*/ SOCK_STREAM;
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int create_and_bind(char *port) {
  struct addrinfo hints;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  struct addrinfo *ai_list;
  int gai_result = getaddrinfo(NULL, port, &hints, &ai_list);
  if (gai_result != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(gai_result));
    return -1;
  }
  int socket_fd = -1;
  struct addrinfo *ai_ptr;
  for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) {
    socket_fd =
        socket(ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
    if (-1 == socket_fd)
      continue;
    int bind_result = bind(socket_fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
    if (bind_result == 0) {
      break;
    }
    close(socket_fd);
  }
  freeaddrinfo(ai_list);
  if (ai_ptr == NULL) {
    fprintf(stderr, "Could not bind\n");
    return -1;
  }
  return socket_fd;
}


x86 assembly (clang, -O0):
<create_and_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     1216 <create_and_bind+0x96>
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    -0x44(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x58(%rbp),%edi
mov    %rax,%rdx
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dd <create_and_bind+0x15d>
movl   $0xffffffff,-0x48(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     12a0 <create_and_bind+0x120>
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   10a0 <socket@plt>
mov    %eax,-0x48(%rbp)
mov    $0xffffffff,%eax
cmp    -0x48(%rbp),%eax
jne    1260 <create_and_bind+0xe0>
jmp    128f <create_and_bind+0x10f>
mov    -0x48(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1070 <bind@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jne    1287 <create_and_bind+0x107>
jmp    12a0 <create_and_bind+0x120>
mov    -0x48(%rbp),%edi
call   1050 <close@plt>
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1225 <create_and_bind+0xa5>
mov    -0x40(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
cmpq   $0x0,-0x50(%rbp)
jne    12d7 <create_and_bind+0x157>
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
lea    0xd4d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dd <create_and_bind+0x15d>
mov    -0x48(%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    $0x30,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1080 <getaddrinfo@plt>
test   %eax,%eax
je     11ff <create_and_bind+0x7f>
mov    0x2def(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xe0f(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1267 <create_and_bind+0xe7>
mov    (%rsp),%rbx
test   %rbx,%rbx
jne    1219 <create_and_bind+0x99>
xor    %edi,%edi
jmp    124b <create_and_bind+0xcb>
nopl   0x0(%rax)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1247 <create_and_bind+0xc7>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1210 <create_and_bind+0x90>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1070 <bind@plt>
test   %eax,%eax
je     1277 <create_and_bind+0xf7>
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1210 <create_and_bind+0x90>
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
lea    0xdb1(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    (%rsp),%rdi
call   1090 <freeaddrinfo@plt>
jmp    126c <create_and_bind+0xec>

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