#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;
  int /*<<< orphan*/ ai_flags;
};

/* 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*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(int /*<<< orphan*/ *, char const *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int tcreate_host(const char *PORT) {
  int retfd;
  struct addrinfo hints, *servinfo;
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE;
  if (getaddrinfo(NULL, PORT, &hints, &servinfo) != 0) {
    return -1;
  }
  if ((retfd = socket(servinfo->ai_family, servinfo->ai_socktype,
                      servinfo->ai_protocol)) == -1) {
    return -2;
  }
  int yes = 1;
  if (setsockopt(retfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
    return -4;
  }
  if (bind(retfd, servinfo->ai_addr, servinfo->ai_addrlen) == -1) {
    return -3;
  }
  freeaddrinfo(servinfo);
  return retfd;
}


x86 assembly (clang, -O0):
<tcreate_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1060 <getaddrinfo@plt>
cmp    $0x0,%rax
je     11cb <tcreate_host+0x6b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1276 <tcreate_host+0x116>
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x38(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11fd <tcreate_host+0x9d>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1276 <tcreate_host+0x116>
movl   $0x1,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
lea    -0x3c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    123d <tcreate_host+0xdd>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1276 <tcreate_host+0x116>
mov    -0x14(%rbp),%edi
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
jne    1267 <tcreate_host+0x107>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1276 <tcreate_host+0x116>
mov    -0x38(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcreate_host>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1060 <getaddrinfo@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     11c9 <tcreate_host+0x69>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
mov    (%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     1233 <tcreate_host+0xd3>
mov    %eax,%ebx
movl   $0x1,0xc(%rsp)
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     123a <tcreate_host+0xda>
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1050 <bind@plt>
cmp    $0xffffffff,%eax
je     1244 <tcreate_host+0xe4>
mov    (%rsp),%rdi
call   1070 <freeaddrinfo@plt>
jmp    11bf <tcreate_host+0x5f>
mov    $0xfffffffe,%ebx
jmp    11bf <tcreate_host+0x5f>
mov    $0xfffffffc,%ebx
jmp    11bf <tcreate_host+0x5f>
mov    $0xfffffffd,%ebx
jmp    11bf <tcreate_host+0x5f>

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

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