#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;
};

/* Variables and functions */
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int get_socket(struct addrinfo *);
int /*<<< orphan*/ perror(int /*<<< orphan*/ *);
struct addrinfo *prepare_addrinfo(char *, char *);
int /*<<< orphan*/ stderr;
int connect_to(char *host, char *port) {
  int socketfd;
  struct addrinfo *res;
  res = prepare_addrinfo(host, port);
  socketfd = get_socket(res);
  if (socketfd != -1) {
    if (connect(socketfd, res->ai_addr, res->ai_addrlen) == -1) {
      fprintf(stderr, "ERROR: connect %s:%s failed: ", host, port);
      perror(NULL);
      close(socketfd);
      socketfd = -1;
    }
  }
  freeaddrinfo(res);
  return socketfd;
}


x86 assembly (clang, -O0):
<connect_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <prepare_addrinfo@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <get_socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     11ff <connect_to+0x8f>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    11fa <connect_to+0x8a>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe23(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x14(%rbp)
jmp    11ff <connect_to+0x8f>
mov    -0x20(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r12
call   1060 <prepare_addrinfo@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <get_socket@plt>
mov    $0xffffffff,%r15d
cmp    $0xffffffff,%eax
je     11af <connect_to+0x3f>
mov    %eax,%ebp
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
je     11c3 <connect_to+0x53>
mov    %ebp,%r15d
mov    %rbx,%rdi
call   1090 <freeaddrinfo@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
mov    %r12,%rdx
mov    %r14,%rcx
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1070 <perror@plt>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    11af <connect_to+0x3f>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32 }
@stderr = global i32 0
@.str = [30 x i8] c"ERROR: connect %s:%s failed: \00"
define i32 @connect_to(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.addrinfo*
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
%8 = load i8*, i8** %4
%9 = call %struct.addrinfo* @prepare_addrinfo(i8* %7, i8* %8)
store %struct.addrinfo* %9, %struct.addrinfo** %6
%10 = load %struct.addrinfo*, %struct.addrinfo** %6
%11 = call i32 @get_socket(%struct.addrinfo* %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp ne i32 %12, -1
br i1 %13, label %14, label %33
14: ; preds = %2
%15 = load i32, i32* %5
%16 = load %struct.addrinfo*, %struct.addrinfo** %6
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i32 0, i32 1
%18 = load i32, i32* %17
%19 = load %struct.addrinfo*, %struct.addrinfo** %6
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @connect(i32 %15, i32 %18, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %32
24: ; preds = %14
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %3
%27 = load i8*, i8** %4
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %26, i8* %27)
%29 = call i32 @perror(i32* null)
%30 = load i32, i32* %5
%31 = call i32 @close(i32 %30)
store i32 -1, i32* %5
br label %32
32: ; preds = %24, %14
br label %33
33: ; preds = %32, %2
%34 = load %struct.addrinfo*, %struct.addrinfo** %6
%35 = call i32 @freeaddrinfo(%struct.addrinfo* %34)
%36 = load i32, i32* %5
ret i32 %36
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32 }
@stderr = global i32 0
@.str = [30 x i8] c"ERROR: connect %s:%s failed: \00"
define i32 @connect_to(i8* %0, i8* %1) {
%3 = tail call %struct.addrinfo* @prepare_addrinfo(i8* %0, i8* %1)
%4 = tail call i32 @get_socket(%struct.addrinfo* %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %18, label %6
6: ; preds = %2
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 1
%8 = load i32, i32* %7
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i32 @connect(i32 %4, i32 %8, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %6
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%16 = tail call i32 @perror(i32* null)
%17 = tail call i32 @close(i32 %4)
br label %18
18: ; preds = %6, %13, %2
%19 = phi i32 [ -1, %13 ], [ %4, %6 ], [ -1, %2 ]
%20 = tail call i32 @freeaddrinfo(%struct.addrinfo* %3)
ret i32 %19
}