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

/* 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*/ 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 i, sfd;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  i = getaddrinfo(NULL, port, &hints, &result);
  if (i != 0) {
    fprintf(stderr, "getaddrinfo() failed: %s\n", gai_strerror(i));
    return -1;
  }
  i = 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;
    if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) == -1) {
      perror("setsockopt() failed");
      close(sfd);
      freeaddrinfo(result);
      return -1;
    }
    if (bind(sfd, rp->ai_addr, rp->ai_addrlen) == 0)
      break;
    close(sfd);
  }
  if (rp == NULL)
    fprintf(stderr, "Could not bind socket to any address\n");
  freeaddrinfo(result);
  return (rp == NULL) ? -1 : 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,-0x54(%rbp)
mov    -0x4c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x54(%rbp),%edi
mov    %rax,%rdx
lea    0xddd(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135e <create_and_bind+0x1be>
movl   $0x1,-0x4c(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     130f <create_and_bind+0x16f>
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    127c <create_and_bind+0xdc>
jmp    12fe <create_and_bind+0x15e>
mov    -0x50(%rbp),%edi
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
mov    0x2d29(%rip),%rax
mov    (%rax),%edx
lea    -0x4c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    12d2 <create_and_bind+0x132>
lea    0xd6a(%rip),%rdi
call   1090 <perror@plt>
mov    -0x50(%rbp),%edi
call   1060 <close@plt>
mov    -0x40(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135e <create_and_bind+0x1be>
mov    -0x50(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jne    12f6 <create_and_bind+0x156>
jmp    130f <create_and_bind+0x16f>
mov    -0x50(%rbp),%edi
call   1060 <close@plt>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    1245 <create_and_bind+0xa5>
cmpq   $0x0,-0x48(%rbp)
jne    1331 <create_and_bind+0x191>
mov    0x2ca7(%rip),%rax
mov    (%rax),%edi
lea    0xd04(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x40(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
cmpq   $0x0,-0x48(%rbp)
jne    1352 <create_and_bind+0x1b2>
mov    $0xffffffff,%eax
mov    %eax,-0x58(%rbp)
jmp    1358 <create_and_bind+0x1b8>
mov    -0x50(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x58(%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),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
lea    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
je     122d <create_and_bind+0x8d>
mov    0x2dc1(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xde9(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    12da <create_and_bind+0x13a>
movl   $0x1,0x14(%rsp)
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     12b4 <create_and_bind+0x114>
mov    0x2da2(%rip),%r15
mov    0x2d6b(%rip),%r12
lea    0x14(%rsp),%r14
jmp    1269 <create_and_bind+0xc9>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     12b4 <create_and_bind+0x114>
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
mov    (%r15),%esi
mov    (%r12),%edx
mov    %eax,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     12e9 <create_and_bind+0x149>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1080 <bind@plt>
test   %rax,%rax
je     12d0 <create_and_bind+0x130>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1260 <create_and_bind+0xc0>
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
lea    0xd6a(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    0x8(%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd2a(%rip),%rdi
call   1090 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x8(%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    $0xffffffff,%ebp
jmp    12da <create_and_bind+0x13a>

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

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