#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 */
typedef int /*<<< orphan*/ yes;
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;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
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 /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int create_and_bind(char *port) {
  struct addrinfo hints;
  struct addrinfo *result, *rp;
  int s, sfd;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  s = getaddrinfo(NULL, port, &hints, &result);
  if (0 != s) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
    return -1;
  }
  for (rp = result; rp != NULL; rp = rp->ai_next) {
    sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (sfd == -1)
      continue;
    int yes = 1;
    setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
    s = bind(sfd, rp->ai_addr, rp->ai_addrlen);
    if (s == 0)
      break;
    close(sfd);
  }
  if (rp == NULL) {
    fprintf(stderr, "Could not bind\n");
    return -1;
  }
  freeaddrinfo(result);
  return sfd;
}


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   1050 <memset@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e15(%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   1090 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
xor    %eax,%eax
cmp    -0x4c(%rbp),%eax
je     1227 <create_and_bind+0x97>
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    -0x4c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x58(%rbp),%edi
mov    %rax,%rdx
lea    0xdec(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130e <create_and_bind+0x17e>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12d1 <create_and_bind+0x141>
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   10b0 <socket@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0xffffffff,-0x50(%rbp)
jne    1266 <create_and_bind+0xd6>
jmp    12c0 <create_and_bind+0x130>
movl   $0x1,-0x54(%rbp)
mov    -0x50(%rbp),%edi
mov    0x2d71(%rip),%rax
mov    (%rax),%esi
mov    0x2d38(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x50(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1080 <bind@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jne    12b8 <create_and_bind+0x128>
jmp    12d1 <create_and_bind+0x141>
mov    -0x50(%rbp),%edi
call   1060 <close@plt>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    122f <create_and_bind+0x9f>
cmpq   $0x0,-0x48(%rbp)
jne    12ff <create_and_bind+0x16f>
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
lea    0xd25(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    130e <create_and_bind+0x17e>
mov    -0x40(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x50(%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   %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    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
lea    0x10(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     121d <create_and_bind+0x8d>
mov    0x2dd1(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xdf9(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    12be <create_and_bind+0x12e>
mov    0x10(%rsp),%rbx
test   %rbx,%rbx
je     1296 <create_and_bind+0x106>
mov    0x2dba(%rip),%r15
mov    0x2d83(%rip),%r12
lea    0xc(%rsp),%r14
jmp    1249 <create_and_bind+0xb9>
nopl   0x0(%rax)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1296 <create_and_bind+0x106>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1240 <create_and_bind+0xb0>
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>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1080 <bind@plt>
test   %eax,%eax
je     12b4 <create_and_bind+0x124>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1240 <create_and_bind+0xb0>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
lea    0xd6b(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    12be <create_and_bind+0x12e>
mov    0x10(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.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 %struct.addrinfo*
%7 = alloca i32
%8 = alloca i32
%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* %7
%19 = load i32, i32* %7
%20 = icmp ne i32 0, %19
br i1 %20, label %21, label %26
21: ; preds = %1
%22 = load i32, i32* @stderr
%23 = load i32, i32* %7
%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 %78
26: ; preds = %1
%27 = load %struct.addrinfo*, %struct.addrinfo** %5
store %struct.addrinfo* %27, %struct.addrinfo** %6
br label %28
28: ; preds = %64, %26
%29 = load %struct.addrinfo*, %struct.addrinfo** %6
%30 = icmp ne %struct.addrinfo* %29, null
br i1 %30, label %31, label %68
31: ; preds = %28
%32 = load %struct.addrinfo*, %struct.addrinfo** %6
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 4
%34 = load i32, i32* %33
%35 = load %struct.addrinfo*, %struct.addrinfo** %6
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 3
%37 = load i32, i32* %36
%38 = load %struct.addrinfo*, %struct.addrinfo** %6
%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* %8
%42 = load i32, i32* %8
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %45
44: ; preds = %31
br label %64
45: ; preds = %31
store i32 1, i32* %9
%46 = load i32, i32* %8
%47 = load i32, i32* @SOL_SOCKET
%48 = load i32, i32* @SO_REUSEADDR
%49 = call i32 @setsockopt(i32 %46, i32 %47, i32 %48, i32* %9, i32 4)
%50 = load i32, i32* %8
%51 = load %struct.addrinfo*, %struct.addrinfo** %6
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 1
%53 = load i32, i32* %52
%54 = load %struct.addrinfo*, %struct.addrinfo** %6
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 @bind(i32 %50, i32 %53, i32 %56)
store i32 %57, i32* %7
%58 = load i32, i32* %7
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %45
br label %68
61: ; preds = %45
%62 = load i32, i32* %8
%63 = call i32 @close(i32 %62)
br label %64
64: ; preds = %61, %44
%65 = load %struct.addrinfo*, %struct.addrinfo** %6
%66 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %65, i32 0, i32 5
%67 = load %struct.addrinfo*, %struct.addrinfo** %66
store %struct.addrinfo* %67, %struct.addrinfo** %6
br label %28
68: ; preds = %60, %28
%69 = load %struct.addrinfo*, %struct.addrinfo** %6
%70 = icmp eq %struct.addrinfo* %69, null
br i1 %70, label %71, label %74
71: ; preds = %68
%72 = load i32, i32* @stderr
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %72, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %78
74: ; preds = %68
%75 = load %struct.addrinfo*, %struct.addrinfo** %5
%76 = call i32 @freeaddrinfo(%struct.addrinfo* %75)
%77 = load i32, i32* %8
store i32 %77, i32* %2
br label %78
78: ; preds = %74, %71, %21
%79 = load i32, i32* %2
ret i32 %79
}

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"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.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 = alloca i32
%5 = bitcast %struct.addrinfo* %2 to i8*
%6 = bitcast %struct.addrinfo** %3 to i8*
%7 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 40)
%8 = load i32, i32* @AF_UNSPEC
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 4
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %10, i32* %11
%12 = load i32, i32* @AI_PASSIVE
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 6
store i32 %12, i32* %13
%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 %51, label %19
19: ; preds = %16
%20 = bitcast i32* %4 to i8*
br label %25
21: ; preds = %1
%22 = load i32, i32* @stderr
%23 = call i8* @gai_strerror(i32 %14)
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %57
25: ; preds = %19, %47
%26 = phi %struct.addrinfo* [ %17, %19 ], [ %49, %47 ]
%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 %47, label %35
35: ; preds = %25
store i32 1, i32* %4
%36 = load i32, i32* @SOL_SOCKET
%37 = load i32, i32* @SO_REUSEADDR
%38 = call i32 @setsockopt(i32 %33, i32 %36, i32 %37, i32* %4, i32 4)
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 1
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @bind(i32 %33, i32 %40, i32 %42)
%44 = icmp eq i32 %43, 0
br i1 %44, label %54, label %45
45: ; preds = %35
%46 = call i32 @close(i32 %33)
br label %47
47: ; preds = %45, %25
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 5
%49 = load %struct.addrinfo*, %struct.addrinfo** %48
%50 = icmp eq %struct.addrinfo* %49, null
br i1 %50, label %51, label %25
51: ; preds = %47, %16
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %57
54: ; preds = %35
%55 = load %struct.addrinfo*, %struct.addrinfo** %3
%56 = call i32 @freeaddrinfo(%struct.addrinfo* %55)
br label %57
57: ; preds = %54, %51, %21
%58 = phi i32 [ -1, %21 ], [ -1, %51 ], [ %33, %54 ]
ret i32 %58
}