#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 */
typedef int uint16_t;
struct addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ service;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
char *calloc(int, size_t);
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
int connect_to_host(const char *host, size_t hostlen, uint16_t port) {
  int ret;
  struct addrinfo hints = {
      .ai_family = AF_UNSPEC,
      .ai_socktype = SOCK_STREAM,
  };
  char service[6];
  snprintf(service, sizeof(service), "%u", port);
  char *use_host = calloc(1, hostlen + 1);
  if (!use_host) {
    return -1;
  }
  strncpy(use_host, host, hostlen);
  struct addrinfo *res;
  ret = getaddrinfo(use_host, service, &hints, &res);
  if (ret) {
    free(use_host);
    return -1;
  }
  free(use_host);
  ret = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  if (ret < 0) {
    goto err_freeaddr;
  }
  int fd = ret;
  ret = connect(fd, res->ai_addr, res->ai_addrlen);
  if (ret < 0) {
    goto err_freesocket;
  }
  return fd;
err_freesocket:
  close(fd);
err_freeaddr:
  freeaddrinfo(res);
  return -1;
}


x86 assembly (clang, -O0):
<connect_to_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x30(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
lea    -0x3e(%rbp),%rdi
mov    -0x1c(%rbp),%ecx
mov    $0x6,%esi
lea    0xe1d(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
mov    $0x1,%edi
call   1070 <calloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1215 <connect_to_host+0x85>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e2 <connect_to_host+0x152>
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <strncpy@plt>
mov    -0x48(%rbp),%rdi
lea    -0x3e(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     125d <connect_to_host+0xcd>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e2 <connect_to_host+0x152>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1292 <connect_to_host+0x102>
jmp    12d2 <connect_to_host+0x142>
mov    -0x20(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1080 <connect@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12bf <connect_to_host+0x12f>
jmp    12ca <connect_to_host+0x13a>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12e2 <connect_to_host+0x152>
mov    -0x54(%rbp),%edi
call   1060 <close@plt>
mov    -0x50(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_host>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edx,%ecx
mov    %rsi,%r15
mov    %rdi,%r14
movq   $0x0,0x10(%rsp)
movl   $0x0,0x18(%rsp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
lea    0xe2c(%rip),%rdx
lea    0xa(%rsp),%rdi
mov    $0x6,%esi
call   1050 <snprintf@plt>
lea    0x1(%r15),%rsi
mov    $0x1,%edi
call   1070 <calloc@plt>
test   %rax,%rax
je     125f <connect_to_host+0xcf>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <strncpy@plt>
lea    0xa(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %rsp,%rcx
mov    %rbx,%rdi
call   1090 <getaddrinfo@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%ebx
test   %ebp,%ebp
jne    1276 <connect_to_host+0xe6>
mov    (%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   10b0 <socket@plt>
test   %eax,%eax
js     126d <connect_to_host+0xdd>
mov    %eax,%ebp
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebp,%edi
call   1080 <connect@plt>
test   %eax,%eax
js     1266 <connect_to_host+0xd6>
mov    %ebp,%ebx
jmp    1276 <connect_to_host+0xe6>
mov    $0xffffffff,%ebx
jmp    1276 <connect_to_host+0xe6>
mov    %ebp,%edi
call   1060 <close@plt>
mov    (%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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