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

/* 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 setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, 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;
    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);
  int one = 1;
  if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one)) ==
      -1) {
    fprintf(stderr, "Could not setsockopt\n");
    return -1;
  }
  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)
cmpl   $0x0,-0x4c(%rbp)
je     1226 <create_and_bind+0x96>
mov    0x2dcf(%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    0xded(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1339 <create_and_bind+0x1a9>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     12a5 <create_and_bind+0x115>
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    1265 <create_and_bind+0xd5>
jmp    1294 <create_and_bind+0x104>
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    128c <create_and_bind+0xfc>
jmp    12a5 <create_and_bind+0x115>
mov    -0x50(%rbp),%edi
call   1060 <close@plt>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    122e <create_and_bind+0x9e>
cmpq   $0x0,-0x48(%rbp)
jne    12d3 <create_and_bind+0x143>
mov    0x2d11(%rip),%rax
mov    (%rax),%edi
lea    0xd51(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1339 <create_and_bind+0x1a9>
mov    -0x40(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
movl   $0x1,-0x54(%rbp)
mov    -0x50(%rbp),%edi
mov    0x2cfb(%rip),%rax
mov    (%rax),%esi
mov    0x2cc2(%rip),%rax
mov    (%rax),%edx
lea    -0x54(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1333 <create_and_bind+0x1a3>
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
lea    0xd01(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1339 <create_and_bind+0x1a9>
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   %r14
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    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x38(%rsp)
lea    0x10(%rsp),%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     1214 <create_and_bind+0x84>
mov    0x2dd5(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xdfd(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12b9 <create_and_bind+0x129>
mov    0x10(%rsp),%rbx
jmp    122b <create_and_bind+0x9b>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     12a2 <create_and_bind+0x112>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     1227 <create_and_bind+0x97>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1080 <bind@plt>
test   %eax,%eax
jne    1220 <create_and_bind+0x90>
mov    0x10(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
movl   $0x1,0xc(%rsp)
mov    0x2d7a(%rip),%rax
mov    (%rax),%esi
mov    0x2d41(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    12be <create_and_bind+0x12e>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
lea    0xd81(%rip),%rsi
jmp    12b2 <create_and_bind+0x122>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
lea    0xd5f(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
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"
@.str.1 = [16 x i8] c"Could not bind\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [22 x i8] c"Could not setsockopt\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 %84
26: ; preds = %1
%27 = load %struct.addrinfo*, %struct.addrinfo** %5
store %struct.addrinfo* %27, %struct.addrinfo** %6
br label %28
28: ; preds = %60, %26
%29 = load %struct.addrinfo*, %struct.addrinfo** %6
%30 = icmp ne %struct.addrinfo* %29, null
br i1 %30, label %31, label %64
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 %60
45: ; preds = %31
%46 = load i32, i32* %8
%47 = load %struct.addrinfo*, %struct.addrinfo** %6
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 1
%49 = load i32, i32* %48
%50 = load %struct.addrinfo*, %struct.addrinfo** %6
%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* %7
%54 = load i32, i32* %7
%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* %8
%59 = call i32 @close(i32 %58)
br label %60
60: ; preds = %57, %44
%61 = load %struct.addrinfo*, %struct.addrinfo** %6
%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** %6
br label %28
64: ; preds = %56, %28
%65 = load %struct.addrinfo*, %struct.addrinfo** %6
%66 = icmp eq %struct.addrinfo* %65, null
br i1 %66, label %67, label %70
67: ; preds = %64
%68 = load i32, i32* @stderr
%69 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %84
70: ; preds = %64
%71 = load %struct.addrinfo*, %struct.addrinfo** %5
%72 = call i32 @freeaddrinfo(%struct.addrinfo* %71)
store i32 1, i32* %9
%73 = load i32, i32* %8
%74 = load i32, i32* @SOL_SOCKET
%75 = load i32, i32* @SO_REUSEADDR
%76 = bitcast i32* %9 to i8*
%77 = call i32 @setsockopt(i32 %73, i32 %74, i32 %75, i8* %76, i32 4)
%78 = icmp eq i32 %77, -1
br i1 %78, label %79, label %82
79: ; preds = %70
%80 = load i32, i32* @stderr
%81 = call i32 (i32, i8*, ...) @fprintf(i32 %80, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %84
82: ; preds = %70
%83 = load i32, i32* %8
store i32 %83, i32* %2
br label %84
84: ; preds = %82, %79, %67, %21
%85 = load i32, i32* %2
ret i32 %85
}

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"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [22 x i8] c"Could not setsockopt\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 %19
16: ; preds = %1
%17 = load %struct.addrinfo*, %struct.addrinfo** %3
%18 = icmp eq %struct.addrinfo* %17, null
br i1 %18, label %46, label %23
19: ; preds = %1
%20 = load i32, i32* @stderr
%21 = call i8* @gai_strerror(i32 %14)
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %21)
br label %62
23: ; preds = %16, %42
%24 = phi %struct.addrinfo* [ %44, %42 ], [ %17, %16 ]
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 4
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 3
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 2
%30 = load i32, i32* %29
%31 = call i32 @socket(i32 %26, i32 %28, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %42, label %33
33: ; preds = %23
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 1
%35 = load i32, i32* %34
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @bind(i32 %31, i32 %35, i32 %37)
%39 = icmp eq i32 %38, 0
br i1 %39, label %49, label %40
40: ; preds = %33
%41 = call i32 @close(i32 %31)
br label %42
42: ; preds = %23, %40
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 5
%44 = load %struct.addrinfo*, %struct.addrinfo** %43
%45 = icmp eq %struct.addrinfo* %44, null
br i1 %45, label %46, label %23
46: ; preds = %42, %16
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %62
49: ; preds = %33
%50 = load %struct.addrinfo*, %struct.addrinfo** %3
%51 = call i32 @freeaddrinfo(%struct.addrinfo* %50)
%52 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%53 = load i32, i32* @SOL_SOCKET
%54 = load i32, i32* @SO_REUSEADDR
%55 = call i32 @setsockopt(i32 %31, i32 %53, i32 %54, i8* %52, i32 4)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %60
57: ; preds = %49
%58 = load i32, i32* @stderr
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %60
60: ; preds = %49, %57
%61 = phi i32 [ -1, %57 ], [ %31, %49 ]
br label %62
62: ; preds = %60, %46, %19
%63 = phi i32 [ -1, %19 ], [ -1, %46 ], [ %61, %60 ]
ret i32 %63
}