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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to_host(const char *hostname, int port) {
  struct addrinfo hints, *servinfo, *p;
  int sockfd = -1, s, err;
  char service[16];
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  snprintf(service, sizeof(service), "%d", port);
  err = getaddrinfo(hostname, service, &hints, &servinfo);
  if (err != 0) {
    return -1;
  }
  for (p = servinfo; p != NULL; p = p->ai_next) {
    s = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
    if (s == -1) {
      continue;
    }
    err = connect(s, p->ai_addr, p->ai_addrlen);
    if (err == -1) {
      close(s);
      continue;
    }
    sockfd = s;
    break;
  }
  freeaddrinfo(servinfo);
  return sockfd;
}


x86 assembly (clang, -O0):
<connect_to_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0xffffffff,-0x4c(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x10,%esi
lea    0xe3f(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x70(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
je     11f4 <connect_to_host+0x84>
movl   $0xffffffff,-0x4(%rbp)
jmp    128d <connect_to_host+0x11d>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     127e <connect_to_host+0x10e>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x48(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edx
call   1090 <socket@plt>
mov    %eax,-0x50(%rbp)
cmpl   $0xffffffff,-0x50(%rbp)
jne    1233 <connect_to_host+0xc3>
jmp    126d <connect_to_host+0xfd>
mov    -0x50(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x48(%rbp),%rax
mov    (%rax),%edx
call   1060 <connect@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    1262 <connect_to_host+0xf2>
mov    -0x50(%rbp),%edi
call   1050 <close@plt>
jmp    126d <connect_to_host+0xfd>
mov    -0x50(%rbp),%eax
mov    %eax,-0x4c(%rbp)
jmp    127e <connect_to_host+0x10e>
mov    -0x48(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    11fc <connect_to_host+0x8c>
mov    -0x40(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
mov    -0x4c(%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    $0x48,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
lea    0xe4c(%rip),%rdx
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x10,%esi
mov    %ebp,%ecx
call   1030 <snprintf@plt>
lea    0x8(%rsp),%rcx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1070 <getaddrinfo@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
jne    1248 <connect_to_host+0xd8>
mov    0x8(%rsp),%rbp
test   %rbp,%rbp
jne    1209 <connect_to_host+0x99>
mov    $0xffffffff,%ebx
xor    %edi,%edi
jmp    1243 <connect_to_host+0xd3>
nopw   0x0(%rax,%rax,1)
mov    0x18(%rbp),%rbp
test   %rbp,%rbp
je     1239 <connect_to_host+0xc9>
mov    0x10(%rbp),%edi
mov    0x8(%rbp),%edx
mov    0xc(%rbp),%esi
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1200 <connect_to_host+0x90>
mov    %eax,%ebx
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
mov    %eax,%edi
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    123e <connect_to_host+0xce>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1200 <connect_to_host+0x90>
mov    $0xffffffff,%ebx
mov    0x8(%rsp),%rdi
call   1080 <freeaddrinfo@plt>
mov    %ebx,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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