#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 {
  scalar_t__ ai_family;
  int ai_socktype;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ ai_flags;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
scalar_t__ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int SOCK_CLOEXEC;
int SOCK_NONBLOCK;
int SOCK_STREAM;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int socket(scalar_t__, int, int /*<<< orphan*/);
int createAndBind(char *host, char *port) {
  assert(port != NULL);
  struct addrinfo hints = {
      .ai_flags = AI_PASSIVE,
      .ai_family = AF_UNSPEC,
      .ai_socktype = SOCK_STREAM,
      .ai_protocol = 0,
  };
  struct addrinfo *res = NULL;
  int err = getaddrinfo(host, port, &hints, &res);
  if (err != 0) {
    return err;
  }
  int sfd = -1;
  for (struct addrinfo *ai = res; ai != NULL; ai = ai->ai_next) {
    if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) {
      continue;
    }
    sfd = socket(ai->ai_family, ai->ai_socktype | SOCK_NONBLOCK | SOCK_CLOEXEC,
                 ai->ai_protocol);
    if (sfd == -1) {
      continue;
    }
    err = bind(sfd, ai->ai_addr, ai->ai_addrlen);
    if (err == 0) {
      break;
    }
    close(sfd);
  }
  return sfd;
}


x86 assembly (clang, -O0):
<createAndBind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
je     11ea <createAndBind+0x9a>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12bc <createAndBind+0x16c>
movl   $0xffffffff,-0x50(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     12b6 <createAndBind+0x166>
mov    -0x58(%rbp),%rax
mov    (%rax),%rax
mov    0x2dc6(%rip),%rcx
cmp    (%rcx),%rax
je     1237 <createAndBind+0xe7>
mov    -0x58(%rbp),%rax
mov    (%rax),%rax
mov    0x2d9f(%rip),%rcx
cmp    (%rcx),%rax
je     1237 <createAndBind+0xe7>
jmp    12a5 <createAndBind+0x155>
mov    -0x58(%rbp),%rax
mov    (%rax),%rdi
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%esi
mov    0x2d6c(%rip),%rax
or     (%rax),%esi
mov    0x2d9b(%rip),%rax
or     (%rax),%esi
mov    -0x58(%rbp),%rax
mov    0x14(%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0xffffffff,-0x50(%rbp)
jne    1275 <createAndBind+0x125>
jmp    12a5 <createAndBind+0x155>
mov    -0x50(%rbp),%edi
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x58(%rbp),%rax
mov    0xc(%rax),%edx
call   1050 <bind@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jne    129d <createAndBind+0x14d>
jmp    12b6 <createAndBind+0x166>
mov    -0x50(%rbp),%edi
call   1030 <close@plt>
mov    -0x58(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    11f9 <createAndBind+0xa9>
mov    -0x50(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createAndBind>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %edi,%edi
test   %rsi,%rsi
setne  %dil
call   1040 <assert@plt>
mov    0x2e57(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x1c(%rsp)
movl   $0x0,0x2c(%rsp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <getaddrinfo@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11e0 <createAndBind+0x90>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rsp),%rbx
mov    $0xffffffff,%ebp
test   %rbx,%rbx
je     11cf <createAndBind+0x7f>
mov    0x2de2(%rip),%r14
mov    0x2dcb(%rip),%r15
mov    0x2db4(%rip),%r12
mov    0x2de5(%rip),%r13
jmp    1229 <createAndBind+0xd9>
mov    $0xffffffff,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     11cf <createAndBind+0x7f>
mov    (%rbx),%rdi
cmp    (%r14),%rdi
je     1236 <createAndBind+0xe6>
cmp    (%r15),%rdi
jne    1220 <createAndBind+0xd0>
mov    (%r12),%esi
or     0x8(%rbx),%esi
or     0x0(%r13),%esi
mov    0x14(%rbx),%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     120d <createAndBind+0xbd>
mov    %eax,%ebp
mov    0xc(%rbx),%edx
mov    0x10(%rbx),%esi
mov    %eax,%edi
call   1050 <bind@plt>
test   %eax,%eax
je     11cf <createAndBind+0x7f>
mov    %ebp,%edi
call   1030 <close@plt>
jmp    1220 <createAndBind+0xd0>

LLVM IR (-O0):
%struct.addrinfo = type { i64, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_UNSPEC = global i64 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@SOCK_NONBLOCK = global i32 0
@SOCK_CLOEXEC = global i32 0
define i32 @createAndBind(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.addrinfo*
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 0
%16 = load i64, i64* @AF_UNSPEC
store i64 %16, i64* %15
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 1
%18 = load i32, i32* @SOCK_STREAM
store i32 %18, i32* %17
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 2
store i32 0, i32* %19
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 0, i32* %20
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 0, i32* %21
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 5
store %struct.addrinfo* null, %struct.addrinfo** %22
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 6
%24 = load i32, i32* @AI_PASSIVE
store i32 %24, i32* %23
store %struct.addrinfo* null, %struct.addrinfo** %7
%25 = load i8*, i8** %4
%26 = load i8*, i8** %5
%27 = call i32 @getaddrinfo(i8* %25, i8* %26, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %2
%31 = load i32, i32* %8
store i32 %31, i32* %3
br label %89
32: ; preds = %2
store i32 -1, i32* %9
%33 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %33, %struct.addrinfo** %10
br label %34
34: ; preds = %83, %32
%35 = load %struct.addrinfo*, %struct.addrinfo** %10
%36 = icmp ne %struct.addrinfo* %35, null
br i1 %36, label %37, label %87
37: ; preds = %34
%38 = load %struct.addrinfo*, %struct.addrinfo** %10
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 0
%40 = load i64, i64* %39
%41 = load i64, i64* @AF_INET
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %50
43: ; preds = %37
%44 = load %struct.addrinfo*, %struct.addrinfo** %10
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i32 0, i32 0
%46 = load i64, i64* %45
%47 = load i64, i64* @AF_INET6
%48 = icmp ne i64 %46, %47
br i1 %48, label %49, label %50
49: ; preds = %43
br label %83
50: ; preds = %43, %37
%51 = load %struct.addrinfo*, %struct.addrinfo** %10
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 0
%53 = load i64, i64* %52
%54 = load %struct.addrinfo*, %struct.addrinfo** %10
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %54, i32 0, i32 1
%56 = load i32, i32* %55
%57 = load i32, i32* @SOCK_NONBLOCK
%58 = or i32 %56, %57
%59 = load i32, i32* @SOCK_CLOEXEC
%60 = or i32 %58, %59
%61 = load %struct.addrinfo*, %struct.addrinfo** %10
%62 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %61, i32 0, i32 4
%63 = load i32, i32* %62
%64 = call i32 @socket(i64 %53, i32 %60, i32 %63)
store i32 %64, i32* %9
%65 = load i32, i32* %9
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %68
67: ; preds = %50
br label %83
68: ; preds = %50
%69 = load i32, i32* %9
%70 = load %struct.addrinfo*, %struct.addrinfo** %10
%71 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %70, i32 0, i32 3
%72 = load i32, i32* %71
%73 = load %struct.addrinfo*, %struct.addrinfo** %10
%74 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %73, i32 0, i32 2
%75 = load i32, i32* %74
%76 = call i32 @bind(i32 %69, i32 %72, i32 %75)
store i32 %76, i32* %8
%77 = load i32, i32* %8
%78 = icmp eq i32 %77, 0
br i1 %78, label %79, label %80
79: ; preds = %68
br label %87
80: ; preds = %68
%81 = load i32, i32* %9
%82 = call i32 @close(i32 %81)
br label %83
83: ; preds = %80, %67, %49
%84 = load %struct.addrinfo*, %struct.addrinfo** %10
%85 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %84, i32 0, i32 5
%86 = load %struct.addrinfo*, %struct.addrinfo** %85
store %struct.addrinfo* %86, %struct.addrinfo** %10
br label %34
87: ; preds = %79, %34
%88 = load i32, i32* %9
store i32 %88, i32* %3
br label %89
89: ; preds = %87, %30
%90 = load i32, i32* %3
ret i32 %90
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_UNSPEC = global i64 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@SOCK_NONBLOCK = global i32 0
@SOCK_CLOEXEC = global i32 0
define i32 @createAndBind(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = icmp ne i8* %1, null
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = bitcast %struct.addrinfo* %3 to i8*
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
%10 = load i64, i64* @AF_UNSPEC
store i64 %10, i64* %9
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 1
%12 = load i32, i32* @SOCK_STREAM
store i32 %12, i32* %11
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 6
%15 = bitcast i32* %13 to i8*
%16 = load i32, i32* @AI_PASSIVE
store i32 %16, i32* %14
%17 = bitcast %struct.addrinfo** %4 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %4
%18 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %58
20: ; preds = %2
%21 = load %struct.addrinfo*, %struct.addrinfo** %4
%22 = icmp eq %struct.addrinfo* %21, null
br i1 %22, label %58, label %23
23: ; preds = %20, %53
%24 = phi %struct.addrinfo* [ %56, %53 ], [ %21, %20 ]
%25 = phi i32 [ %54, %53 ], [ -1, %20 ]
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 0
%27 = load i64, i64* %26
%28 = load i64, i64* @AF_INET
%29 = icmp eq i64 %27, %28
%30 = load i64, i64* @AF_INET6
%31 = icmp eq i64 %27, %30
%32 = select i1 %29, i1 true, i1 %31
br i1 %32, label %33, label %53
33: ; preds = %23
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 1
%35 = load i32, i32* %34
%36 = load i32, i32* @SOCK_NONBLOCK
%37 = or i32 %36, %35
%38 = load i32, i32* @SOCK_CLOEXEC
%39 = or i32 %37, %38
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 4
%41 = load i32, i32* %40
%42 = call i32 @socket(i64 %27, i32 %39, i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %53, label %44
44: ; preds = %33
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 3
%46 = load i32, i32* %45
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 2
%48 = load i32, i32* %47
%49 = call i32 @bind(i32 %42, i32 %46, i32 %48)
%50 = icmp eq i32 %49, 0
br i1 %50, label %58, label %51
51: ; preds = %44
%52 = call i32 @close(i32 %42)
br label %53
53: ; preds = %23, %33, %51
%54 = phi i32 [ -1, %33 ], [ %42, %51 ], [ %25, %23 ]
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 5
%56 = load %struct.addrinfo*, %struct.addrinfo** %55
%57 = icmp eq %struct.addrinfo* %56, null
br i1 %57, label %58, label %23
58: ; preds = %44, %53, %20, %2
%59 = phi i32 [ %18, %2 ], [ -1, %20 ], [ %42, %44 ], [ %54, %53 ]
ret i32 %59
}