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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ AI_PASSIVE;
int SOCK_NONBLOCK;
int SOCK_STREAM;
int /*<<< orphan*/ assert(int);
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(int /*<<< orphan*/ *, char *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int create_and_bind_nonblock_sock(char *port) {
  struct addrinfo hints;
  struct addrinfo *result, *rp;
  int s, sfd;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_flags = AI_PASSIVE;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  s = getaddrinfo(NULL, port, &hints, &result);
  assert(s == 0);
  for (rp = result; rp != NULL; rp = rp->ai_next) {
    sfd =
        socket(rp->ai_family, rp->ai_socktype | SOCK_NONBLOCK, rp->ai_protocol);
    if (sfd == -1)
      continue;
    s = bind(sfd, rp->ai_addr, rp->ai_addrlen);
    if (s == 0)
      break;
    close(sfd);
  }
  assert(rp != NULL);
  freeaddrinfo(result);
  return sfd;
}


x86 assembly (clang, -O0):
<create_and_bind_nonblock_sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1261 <create_and_bind_nonblock_sock+0xf1>
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x40(%rbp),%rax
mov    (%rax),%esi
mov    0x2dd0(%rip),%rax
or     (%rax),%esi
mov    -0x40(%rbp),%rax
mov    0xc(%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xffffffff,-0x48(%rbp)
jne    1220 <create_and_bind_nonblock_sock+0xb0>
jmp    1250 <create_and_bind_nonblock_sock+0xe0>
mov    -0x48(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%edx
call   1060 <bind@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jne    1248 <create_and_bind_nonblock_sock+0xd8>
jmp    1261 <create_and_bind_nonblock_sock+0xf1>
mov    -0x48(%rbp),%edi
call   1040 <close@plt>
mov    -0x40(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    11e1 <create_and_bind_nonblock_sock+0x71>
cmpq   $0x0,-0x40(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x38(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x48(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_and_bind_nonblock_sock>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rdi,%rbp
lea    0x10(%rsp),%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1070 <getaddrinfo@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1050 <assert@plt>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     122a <create_and_bind_nonblock_sock+0xba>
mov    0x2de5(%rip),%r15
jmp    11f9 <create_and_bind_nonblock_sock+0x89>
nopl   (%rax)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1232 <create_and_bind_nonblock_sock+0xc2>
mov    0xc(%rbx),%edx
mov    0x10(%rbx),%edi
mov    (%r15),%esi
or     (%rbx),%esi
call   1090 <socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     11f0 <create_and_bind_nonblock_sock+0x80>
mov    0x4(%rbx),%edx
mov    0x8(%rbx),%esi
mov    %ebp,%edi
call   1060 <bind@plt>
test   %eax,%eax
je     122c <create_and_bind_nonblock_sock+0xbc>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11f0 <create_and_bind_nonblock_sock+0x80>
jmp    1232 <create_and_bind_nonblock_sock+0xc2>
mov    $0x1,%r14d
mov    %r14d,%edi
call   1050 <assert@plt>
mov    0x8(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AI_PASSIVE = global i32 0
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@SOCK_NONBLOCK = global i32 0
define i32 @create_and_bind_nonblock_sock(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = bitcast %struct.addrinfo* %2 to i8*
%5 = bitcast %struct.addrinfo** %3 to i8*
%6 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 40)
%7 = load i32, i32* @AI_PASSIVE
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 6
store i32 %7, i32* %8
%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 0
store i32 %11, i32* %12
%13 = call i32 @getaddrinfo(i32* null, i8* %0, %struct.addrinfo* %2, %struct.addrinfo** %3)
%14 = icmp eq i32 %13, 0
%15 = zext i1 %14 to i32
%16 = call i32 @assert(i32 %15)
%17 = load %struct.addrinfo*, %struct.addrinfo** %3
%18 = icmp eq %struct.addrinfo* %17, null
br i1 %18, label %44, label %19
19: ; preds = %1, %40
%20 = phi %struct.addrinfo* [ %42, %40 ], [ %17, %1 ]
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 4
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 0
%24 = load i32, i32* %23
%25 = load i32, i32* @SOCK_NONBLOCK
%26 = or i32 %25, %24
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 3
%28 = load i32, i32* %27
%29 = call i32 @socket(i32 %22, i32 %26, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %40, label %31
31: ; preds = %19
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 2
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 1
%35 = load i32, i32* %34
%36 = call i32 @bind(i32 %29, i32 %33, i32 %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %44, label %38
38: ; preds = %31
%39 = call i32 @close(i32 %29)
br label %40
40: ; preds = %19, %38
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 5
%42 = load %struct.addrinfo*, %struct.addrinfo** %41
%43 = icmp eq %struct.addrinfo* %42, null
br i1 %43, label %44, label %19
44: ; preds = %40, %31, %1
%45 = phi i32 [ 0, %1 ], [ 1, %31 ], [ 0, %40 ]
%46 = phi i32 [ undef, %1 ], [ %29, %31 ], [ %29, %40 ]
%47 = call i32 @assert(i32 %45)
%48 = load %struct.addrinfo*, %struct.addrinfo** %3
%49 = call i32 @freeaddrinfo(%struct.addrinfo* %48)
ret i32 %46
}