#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;
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*/ perror(char *);
int 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 (s != 0) {
    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;
    if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
      perror("setsockopt");
    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    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e05(%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   10a0 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     1236 <create_and_bind+0x96>
mov    0x2dbf(%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    0xddd(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1332 <create_and_bind+0x192>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12f5 <create_and_bind+0x155>
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,-0x50(%rbp)
cmpl   $0xffffffff,-0x50(%rbp)
jne    1275 <create_and_bind+0xd5>
jmp    12e4 <create_and_bind+0x144>
movl   $0x1,-0x54(%rbp)
mov    -0x50(%rbp),%edi
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
mov    0x2d29(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    12b5 <create_and_bind+0x115>
lea    0xd61(%rip),%rdi
call   1090 <perror@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    12dc <create_and_bind+0x13c>
jmp    12f5 <create_and_bind+0x155>
mov    -0x50(%rbp),%edi
call   1060 <close@plt>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    123e <create_and_bind+0x9e>
cmpq   $0x0,-0x48(%rbp)
jne    1323 <create_and_bind+0x183>
mov    0x2cc1(%rip),%rax
mov    (%rax),%edi
lea    0xd0c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1332 <create_and_bind+0x192>
mov    -0x40(%rbp),%rdi
call   10b0 <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   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%rbx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
lea    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
je     122f <create_and_bind+0x8f>
mov    0x2dbf(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xde7(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    12ee <create_and_bind+0x14e>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     12c6 <create_and_bind+0x126>
mov    0x2da4(%rip),%r12
mov    0x2d6d(%rip),%r13
lea    0x4(%rsp),%r15
lea    0xdba(%rip),%r14
jmp    1269 <create_and_bind+0xc9>
nopl   0x0(%rax)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     12c6 <create_and_bind+0x126>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     1260 <create_and_bind+0xc0>
mov    %eax,%ebp
movl   $0x1,0x4(%rsp)
mov    (%r12),%esi
mov    0x0(%r13),%edx
mov    %eax,%edi
mov    %r15,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     12bc <create_and_bind+0x11c>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1080 <bind@plt>
test   %eax,%eax
je     12e4 <create_and_bind+0x144>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1260 <create_and_bind+0xc0>
mov    %r14,%rdi
call   1090 <perror@plt>
jmp    12a3 <create_and_bind+0x103>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
lea    0xd46(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    12ee <create_and_bind+0x14e>
mov    0x8(%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
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 = [11 x i8] c"setsockopt\00"
@.str.2 = [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 %19, 0
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 %82
26: ; preds = %1
%27 = load %struct.addrinfo*, %struct.addrinfo** %5
store %struct.addrinfo* %27, %struct.addrinfo** %6
br label %28
28: ; preds = %68, %26
%29 = load %struct.addrinfo*, %struct.addrinfo** %6
%30 = icmp ne %struct.addrinfo* %29, null
br i1 %30, label %31, label %72
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 %68
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 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %45
%52 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %53
53: ; preds = %51, %45
%54 = load i32, i32* %8
%55 = load %struct.addrinfo*, %struct.addrinfo** %6
%56 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %55, i32 0, i32 1
%57 = load i32, i32* %56
%58 = load %struct.addrinfo*, %struct.addrinfo** %6
%59 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %58, i32 0, i32 0
%60 = load i32, i32* %59
%61 = call i32 @bind(i32 %54, i32 %57, i32 %60)
store i32 %61, i32* %7
%62 = load i32, i32* %7
%63 = icmp eq i32 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %53
br label %72
65: ; preds = %53
%66 = load i32, i32* %8
%67 = call i32 @close(i32 %66)
br label %68
68: ; preds = %65, %44
%69 = load %struct.addrinfo*, %struct.addrinfo** %6
%70 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %69, i32 0, i32 5
%71 = load %struct.addrinfo*, %struct.addrinfo** %70
store %struct.addrinfo* %71, %struct.addrinfo** %6
br label %28
72: ; preds = %64, %28
%73 = load %struct.addrinfo*, %struct.addrinfo** %6
%74 = icmp eq %struct.addrinfo* %73, null
br i1 %74, label %75, label %78
75: ; preds = %72
%76 = load i32, i32* @stderr
%77 = call i32 (i32, i8*, ...) @fprintf(i32 %76, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %82
78: ; preds = %72
%79 = load %struct.addrinfo*, %struct.addrinfo** %5
%80 = call i32 @freeaddrinfo(%struct.addrinfo* %79)
%81 = load i32, i32* %8
store i32 %81, i32* %2
br label %82
82: ; preds = %78, %75, %21
%83 = load i32, i32* %2
ret i32 %83
}

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 = [11 x i8] c"setsockopt\00"
@.str.2 = [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 %55, 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 %61
25: ; preds = %19, %51
%26 = phi %struct.addrinfo* [ %17, %19 ], [ %53, %51 ]
%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 %51, 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 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %40, %35
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 1
%44 = load i32, i32* %43
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @bind(i32 %33, i32 %44, i32 %46)
%48 = icmp eq i32 %47, 0
br i1 %48, label %58, label %49
49: ; preds = %42
%50 = call i32 @close(i32 %33)
br label %51
51: ; preds = %49, %25
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 5
%53 = load %struct.addrinfo*, %struct.addrinfo** %52
%54 = icmp eq %struct.addrinfo* %53, null
br i1 %54, label %55, label %25
55: ; preds = %51, %16
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %61
58: ; preds = %42
%59 = load %struct.addrinfo*, %struct.addrinfo** %3
%60 = call i32 @freeaddrinfo(%struct.addrinfo* %59)
br label %61
61: ; preds = %58, %55, %21
%62 = phi i32 [ -1, %21 ], [ -1, %55 ], [ %33, %58 ]
ret i32 %62
}