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


x86 assembly (clang, -O0):
<create_and_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %dl,%al
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %al,-0x19(%rbp)
movl   $0x1,-0x64(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
movsbl -0x19(%rbp),%eax
cmp    $0x0,%eax
je     11f6 <create_and_bind+0x56>
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
jmp    1202 <create_and_bind+0x62>
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
mov    -0x68(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
je     126a <create_and_bind+0xca>
mov    0x2d83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    -0x5c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x6c(%rbp),%edi
mov    %rax,%rdx
lea    0xda9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138b <create_and_bind+0x1eb>
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     134e <create_and_bind+0x1ae>
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x58(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x60(%rbp)
cmpl   $0x0,-0x60(%rbp)
jge    12b5 <create_and_bind+0x115>
lea    0xd66(%rip),%rdi
call   1090 <perror@plt>
jmp    133d <create_and_bind+0x19d>
mov    -0x60(%rbp),%edi
mov    0x2d29(%rip),%rax
mov    (%rax),%esi
mov    0x2ce8(%rip),%rax
mov    (%rax),%edx
lea    -0x64(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1302 <create_and_bind+0x162>
lea    0xd2f(%rip),%rdi
call   1090 <perror@plt>
mov    -0x60(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138b <create_and_bind+0x1eb>
mov    -0x60(%rbp),%edi
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x58(%rbp),%rax
mov    (%rax),%edx
call   1080 <bind@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jne    1329 <create_and_bind+0x189>
jmp    134e <create_and_bind+0x1ae>
lea    0xcf3(%rip),%rdi
call   1090 <perror@plt>
mov    -0x60(%rbp),%edi
call   1060 <close@plt>
mov    -0x58(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    1272 <create_and_bind+0xd2>
cmpq   $0x0,-0x58(%rbp)
jne    137c <create_and_bind+0x1dc>
mov    0x2c60(%rip),%rax
mov    (%rax),%edi
lea    0xcbf(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    138b <create_and_bind+0x1eb>
mov    -0x50(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x60(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%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    %edx,%ebx
mov    %rsi,%r15
mov    %rdi,%rbp
movl   $0x1,0x4(%rsp)
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1050 <memset@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
test   %bl,%bl
jne    11ec <create_and_bind+0x4c>
mov    0x2de6(%rip),%rax
jmp    11f3 <create_and_bind+0x53>
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
je     124a <create_and_bind+0xaa>
mov    0x2d9c(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xdcc(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    12e4 <create_and_bind+0x144>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     12f5 <create_and_bind+0x155>
mov    0x2d89(%rip),%r13
mov    0x2d4a(%rip),%r14
lea    0x4(%rsp),%r12
lea    0xdb1(%rip),%r15
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10c0 <socket@plt>
test   %eax,%eax
js     12cc <create_and_bind+0x12c>
mov    %eax,%ebp
mov    0x0(%r13),%esi
mov    (%r14),%edx
mov    %eax,%edi
mov    %r12,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1311 <create_and_bind+0x171>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1080 <bind@plt>
test   %eax,%eax
je     12da <create_and_bind+0x13a>
mov    %r15,%rdi
call   1090 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    1272 <create_and_bind+0xd2>
jmp    12f5 <create_and_bind+0x155>
lea    0xd3e(%rip),%rdi
call   1090 <perror@plt>
jmp    12c1 <create_and_bind+0x121>
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
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
lea    0xd23(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1240 <create_and_bind+0xa0>
lea    0xd00(%rip),%rdi
call   1090 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1240 <create_and_bind+0xa0>

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

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