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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to_host(char *ip, char *port) {
  int status;
  int sock;
  struct addrinfo hints;
  struct addrinfo *server_info;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  status = getaddrinfo(ip, port, &hints, &server_info);
  if (status != 0) {
    return status;
  }
  sock = socket(server_info->ai_family, server_info->ai_socktype,
                server_info->ai_protocol);
  status = connect(sock, server_info->ai_addr, server_info->ai_addrlen);
  if (status != 0) {
    return status;
  }
  freeaddrinfo(server_info);
  return sock;
}


x86 assembly (clang, -O0):
<connect_to_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1050 <getaddrinfo@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11b5 <connect_to_host+0x65>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <connect_to_host+0xbe>
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x40(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x40(%rbp),%rax
mov    (%rax),%edx
call   1040 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11ff <connect_to_host+0xaf>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120e <connect_to_host+0xbe>
mov    -0x40(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_host>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <getaddrinfo@plt>
test   %eax,%eax
jne    11d5 <connect_to_host+0x85>
mov    (%rsp),%rax
mov    0x10(%rax),%edi
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
call   1070 <socket@plt>
mov    %eax,%ebx
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebx,%edi
call   1040 <connect@plt>
test   %eax,%eax
jne    11d5 <connect_to_host+0x85>
mov    (%rsp),%rdi
call   1060 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
define i32 @connect_to_host(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 20)
%8 = load i32, i32* @AF_UNSPEC
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %10, i32* %11
%12 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %33
14: ; preds = %2
%15 = load %struct.addrinfo*, %struct.addrinfo** %4
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 4
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 3
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 2
%21 = load i32, i32* %20
%22 = call i32 @socket(i32 %17, i32 %19, i32 %21)
%23 = load %struct.addrinfo*, %struct.addrinfo** %4
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 1
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @connect(i32 %22, i32 %25, i32 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %14
%31 = load %struct.addrinfo*, %struct.addrinfo** %4
%32 = call i32 @freeaddrinfo(%struct.addrinfo* %31)
br label %33
33: ; preds = %14, %2, %30
%34 = phi i32 [ %22, %30 ], [ %12, %2 ], [ %28, %14 ]
ret i32 %34
}