#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_socktype;
  int /*<<< orphan*/ ai_family;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
char *alloca(int);
scalar_t__ connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int connect_to(const char *host, uint16_t port) {
  struct addrinfo *addrinfos, hints = {
                                  0,
                              };
  char *port_s = NULL;
  int addrinfo_ret = 0;
  int fd = -1;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  port_s = alloca((sizeof("65535") / sizeof("65535"[0])));
  snprintf(port_s, (sizeof("65535") / sizeof("65535"[0])), "%d", port);
  if ((addrinfo_ret = getaddrinfo(host, port_s, &hints, &addrinfos)) != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(addrinfo_ret));
    return -1;
  }
  if ((fd = socket(addrinfos->ai_family, addrinfos->ai_socktype, 0)) < 0) {
    perror("socket");
    freeaddrinfo(addrinfos);
    return -1;
  }
  if (connect(fd, addrinfos->ai_addr, addrinfos->ai_addrlen) < 0) {
    perror("connect");
    freeaddrinfo(addrinfos);
    return -1;
  }
  freeaddrinfo(addrinfos);
  return fd;
}


x86 assembly (clang, -O0):
<connect_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
movq   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0xffffffff,-0x48(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    $0x6,%edi
call   1070 <alloca@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x6,%esi
lea    0xdf2(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
je     1266 <connect_to+0xc6>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x44(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x4c(%rbp),%edi
mov    %rax,%rdx
lea    0xdae(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f7 <connect_to+0x157>
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%edi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x48(%rbp)
cmp    $0x0,%eax
jge    12a8 <connect_to+0x108>
lea    0xd86(%rip),%rdi
call   1080 <perror@plt>
mov    -0x20(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f7 <connect_to+0x157>
mov    -0x48(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    12e8 <connect_to+0x148>
lea    0xd4d(%rip),%rdi
call   1080 <perror@plt>
mov    -0x20(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f7 <connect_to+0x157>
mov    -0x20(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x20(%rsp)
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    $0x6,%edi
call   1060 <alloca@plt>
mov    %rax,%rbx
lea    0xe25(%rip),%rdx
mov    %rax,%rdi
mov    $0x6,%esi
mov    %ebp,%ecx
call   1030 <snprintf@plt>
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     122b <connect_to+0x9b>
mov    0x2dce(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xde9(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    1263 <connect_to+0xd3>
mov    0x8(%rsp),%rax
mov    0x8(%rax),%esi
mov    0xc(%rax),%edi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     126e <connect_to+0xde>
mov    %eax,%ebx
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1080 <connect@plt>
test   %rax,%rax
js     1277 <connect_to+0xe7>
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd9f(%rip),%rdi
jmp    127e <connect_to+0xee>
lea    0xd9d(%rip),%rdi
call   1070 <perror@plt>
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    $0xffffffff,%ebx
jmp    1263 <connect_to+0xd3>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [3 x i8] c"%d\00"
@stderr = global i32 0
@.str.1 = [17 x i8] c"getaddrinfo: %s\0A\00"
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [8 x i8] c"connect\00"
define i32 @connect_to(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.addrinfo*
%7 = alloca %struct.addrinfo
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = bitcast %struct.addrinfo* %7 to i8*
store i8* null, i8** %8
store i32 0, i32* %9
store i32 -1, i32* %10
%12 = load i32, i32* @AF_UNSPEC
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 3
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 2
store i32 %14, i32* %15
%16 = call i8* @alloca(i32 6)
store i8* %16, i8** %8
%17 = load i8*, i8** %8
%18 = load i32, i32* %5
%19 = call i32 @snprintf(i8* %17, i32 6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %18)
%20 = load i8*, i8** %4
%21 = load i8*, i8** %8
%22 = call i32 @getaddrinfo(i8* %20, i8* %21, %struct.addrinfo* %7, %struct.addrinfo** %6)
store i32 %22, i32* %9
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %2
%25 = load i32, i32* @stderr
%26 = load i32, i32* %9
%27 = call i8* @gai_strerror(i32 %26)
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %27)
store i32 -1, i32* %3
br label %60
29: ; preds = %2
%30 = load %struct.addrinfo*, %struct.addrinfo** %6
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %30, i32 0, i32 3
%32 = load i32, i32* %31
%33 = load %struct.addrinfo*, %struct.addrinfo** %6
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %33, i32 0, i32 2
%35 = load i32, i32* %34
%36 = call i32 @socket(i32 %32, i32 %35, i32 0)
store i32 %36, i32* %10
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %29
%39 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%40 = load %struct.addrinfo*, %struct.addrinfo** %6
%41 = call i32 @freeaddrinfo(%struct.addrinfo* %40)
store i32 -1, i32* %3
br label %60
42: ; preds = %29
%43 = load i32, i32* %10
%44 = load %struct.addrinfo*, %struct.addrinfo** %6
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %44, i32 0, i32 1
%46 = load i32, i32* %45
%47 = load %struct.addrinfo*, %struct.addrinfo** %6
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %47, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i64 @connect(i32 %43, i32 %46, i32 %49)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %42
%53 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%54 = load %struct.addrinfo*, %struct.addrinfo** %6
%55 = call i32 @freeaddrinfo(%struct.addrinfo* %54)
store i32 -1, i32* %3
br label %60
56: ; preds = %42
%57 = load %struct.addrinfo*, %struct.addrinfo** %6
%58 = call i32 @freeaddrinfo(%struct.addrinfo* %57)
%59 = load i32, i32* %10
store i32 %59, i32* %3
br label %60
60: ; preds = %56, %52, %38, %24
%61 = load i32, i32* %3
ret i32 %61
}

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