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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ TCP_NODELAY;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ wth_error(char *, char const *, char const *,
                             int /*<<< orphan*/);
int connect_to_host(const char *host, const char *port) {
  int flag = 1;
  int fd;
  int ret;
  struct addrinfo hints = {
      0,
  };
  struct addrinfo *res, *r;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  ret = getaddrinfo(host, port, &hints, &res);
  if (ret < 0) {
    wth_error("Connect to %s port %s failed (getaddrinfo: %s)", host, port,
              strerror(errno));
    return -1;
  }
  for (r = res; r != NULL; r = r->ai_next) {
    fd = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
    if (fd == -1)
      continue;
    if (connect(fd, r->ai_addr, r->ai_addrlen) != -1)
      break;
    close(fd);
    fd = -1;
  }
  if (fd != -1)
    setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int));
  return fd;
}


x86 assembly (clang, -O0):
<connect_to_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   1080 <getaddrinfo@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    122d <connect_to_host+0xad>
mov    -0x10(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x70(%rbp),%rsi
mov    -0x68(%rbp),%rdx
mov    %eax,%ecx
lea    0xde4(%rip),%rdi
call   1060 <wth_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <connect_to_host+0x163>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     12af <connect_to_host+0x12f>
mov    -0x60(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x60(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    126c <connect_to_host+0xec>
jmp    129e <connect_to_host+0x11e>
mov    -0x20(%rbp),%edi
mov    -0x60(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x60(%rbp),%rax
mov    (%rax),%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     128f <connect_to_host+0x10f>
jmp    12af <connect_to_host+0x12f>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x20(%rbp)
mov    -0x60(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x60(%rbp)
jmp    1235 <connect_to_host+0xb5>
cmpl   $0xffffffff,-0x20(%rbp)
je     12dd <connect_to_host+0x15d>
mov    -0x20(%rbp),%edi
mov    0x2d15(%rip),%rax
mov    (%rax),%esi
mov    0x2d1c(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_host>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
movl   $0x1,0x4(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x30(%rsp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e19(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
js     1228 <connect_to_host+0xb8>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
jne    11f9 <connect_to_host+0x89>
jmp    124a <connect_to_host+0xda>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     124a <connect_to_host+0xda>
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     11f0 <connect_to_host+0x80>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    125c <connect_to_host+0xec>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11f0 <connect_to_host+0x80>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdc3(%rip),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %eax,%ecx
call   1050 <wth_error@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r15),%esi
mov    0x2d82(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
jmp    124f <connect_to_host+0xdf>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [47 x i8] c"Connect to %s port %s failed (getaddrinfo: %s)\00"
@errno = global i32 0
@TCP_NODELAY = global i32 0
define i32 @connect_to_host(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.addrinfo
%10 = alloca %struct.addrinfo*
%11 = alloca %struct.addrinfo*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 1, i32* %6
%12 = bitcast %struct.addrinfo* %9 to i8*
%13 = load i32, i32* @AF_UNSPEC
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 4
store i32 %13, i32* %14
%15 = load i32, i32* @SOCK_STREAM
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 3
store i32 %15, i32* %16
%17 = load i32, i32* @IPPROTO_TCP
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i8*, i8** %4
%20 = load i8*, i8** %5
%21 = call i32 @getaddrinfo(i8* %19, i8* %20, %struct.addrinfo* %9, %struct.addrinfo** %10)
store i32 %21, i32* %8
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %2
%25 = load i8*, i8** %4
%26 = load i8*, i8** %5
%27 = load i32, i32* @errno
%28 = call i32 @strerror(i32 %27)
%29 = call i32 @wth_error(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %25, i8* %26, i32 %28)
store i32 -1, i32* %3
br label %78
30: ; preds = %2
%31 = load %struct.addrinfo*, %struct.addrinfo** %10
store %struct.addrinfo* %31, %struct.addrinfo** %11
br label %32
32: ; preds = %63, %30
%33 = load %struct.addrinfo*, %struct.addrinfo** %11
%34 = icmp ne %struct.addrinfo* %33, null
br i1 %34, label %35, label %67
35: ; preds = %32
%36 = load %struct.addrinfo*, %struct.addrinfo** %11
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 4
%38 = load i32, i32* %37
%39 = load %struct.addrinfo*, %struct.addrinfo** %11
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i32 0, i32 3
%41 = load i32, i32* %40
%42 = load %struct.addrinfo*, %struct.addrinfo** %11
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %42, i32 0, i32 2
%44 = load i32, i32* %43
%45 = call i32 @socket(i32 %38, i32 %41, i32 %44)
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %49
48: ; preds = %35
br label %63
49: ; preds = %35
%50 = load i32, i32* %7
%51 = load %struct.addrinfo*, %struct.addrinfo** %11
%52 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %51, i32 0, i32 1
%53 = load i32, i32* %52
%54 = load %struct.addrinfo*, %struct.addrinfo** %11
%55 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 @connect(i32 %50, i32 %53, i32 %56)
%58 = icmp ne i32 %57, -1
br i1 %58, label %59, label %60
59: ; preds = %49
br label %67
60: ; preds = %49
%61 = load i32, i32* %7
%62 = call i32 @close(i32 %61)
store i32 -1, i32* %7
br label %63
63: ; preds = %60, %48
%64 = load %struct.addrinfo*, %struct.addrinfo** %11
%65 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %64, i32 0, i32 5
%66 = load %struct.addrinfo*, %struct.addrinfo** %65
store %struct.addrinfo* %66, %struct.addrinfo** %11
br label %32
67: ; preds = %59, %32
%68 = load i32, i32* %7
%69 = icmp ne i32 %68, -1
br i1 %69, label %70, label %76
70: ; preds = %67
%71 = load i32, i32* %7
%72 = load i32, i32* @IPPROTO_TCP
%73 = load i32, i32* @TCP_NODELAY
%74 = bitcast i32* %6 to i8*
%75 = call i32 @setsockopt(i32 %71, i32 %72, i32 %73, i8* %74, i32 4)
br label %76
76: ; preds = %70, %67
%77 = load i32, i32* %7
store i32 %77, i32* %3
br label %78
78: ; preds = %76, %24
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo*, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [47 x i8] c"Connect to %s port %s failed (getaddrinfo: %s)\00"
@errno = global i32 0
@TCP_NODELAY = global i32 0
define i32 @connect_to_host(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%7 = bitcast %struct.addrinfo* %4 to i8*
%8 = bitcast %struct.addrinfo** %5 to i8*
%9 = load i32, i32* @AF_UNSPEC
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 3
store i32 %11, i32* %12
%13 = load i32, i32* @IPPROTO_TCP
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %4, %struct.addrinfo** %5)
%16 = icmp slt i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %2
%18 = load %struct.addrinfo*, %struct.addrinfo** %5
%19 = icmp eq %struct.addrinfo* %18, null
br i1 %19, label %51, label %24
20: ; preds = %2
%21 = load i32, i32* @errno
%22 = call i32 @strerror(i32 %21)
%23 = call i32 @wth_error(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1, i32 %22)
br label %51
24: ; preds = %17, %43
%25 = phi %struct.addrinfo* [ %45, %43 ], [ %18, %17 ]
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 4
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 3
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 2
%31 = load i32, i32* %30
%32 = call i32 @socket(i32 %27, i32 %29, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %43, label %34
34: ; preds = %24
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 1
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 0
%38 = load i32, i32* %37
%39 = call i32 @connect(i32 %32, i32 %36, i32 %38)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %47
41: ; preds = %34
%42 = call i32 @close(i32 %32)
br label %43
43: ; preds = %24, %41
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i64 0, i32 5
%45 = load %struct.addrinfo*, %struct.addrinfo** %44
%46 = icmp eq %struct.addrinfo* %45, null
br i1 %46, label %51, label %24
47: ; preds = %34
%48 = load i32, i32* @IPPROTO_TCP
%49 = load i32, i32* @TCP_NODELAY
%50 = call i32 @setsockopt(i32 %32, i32 %48, i32 %49, i8* %6, i32 4)
br label %51
51: ; preds = %43, %17, %47, %20
%52 = phi i32 [ -1, %20 ], [ %32, %47 ], [ -1, %17 ], [ -1, %43 ]
ret i32 %52
}