#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ *h_addr_list;
  int /*<<< orphan*/ h_addrtype;
};
typedef int /*<<< orphan*/ server;

/* Variables and functions */
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int open_connection(char *hostname, int remote_port, int socket_desc) {
  struct sockaddr_in server;
  struct hostent *host;
  host = gethostbyname(hostname);
  if (host == NULL) {
    fprintf(stderr, "Failed to resolve hostname %s!", hostname);
    return -1;
  }
  server.sin_family = host->h_addrtype;
  memcpy((char *)&server.sin_addr.s_addr, host->h_addr_list[0], host->h_length);
  server.sin_port = htons(remote_port);
  if (connect(socket_desc, (struct sockaddr *)&server, sizeof(server)) == -1) {
    perror("Error while connecting to target host");
    socket_desc = -1;
  }
  return socket_desc;
}


x86 assembly (clang, -O0):
<open_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11af <open_connection+0x4f>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe62(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1214 <open_connection+0xb4>
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%eax
mov    %eax,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x4,%rdi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    120e <open_connection+0xae>
lea    0xe1d(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_connection>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebx
mov    %esi,%r14d
mov    %rdi,%rbp
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     11c0 <open_connection+0x60>
mov    0x10(%rax),%ecx
mov    %ecx,0x8(%rsp)
lea    0x4(%rsp),%rdi
mov    0x8(%rax),%rcx
mov    (%rcx),%esi
mov    (%rax),%edx
call   1060 <memcpy@plt>
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    11dd <open_connection+0x7d>
lea    0xe66(%rip),%rdi
call   1070 <perror@plt>
jmp    11d8 <open_connection+0x78>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
mov    %rbp,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32*, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [31 x i8] c"Failed to resolve hostname %s!\00"
@.str.1 = [38 x i8] c"Error while connecting to target host\00"
define i32 @open_connection(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.hostent*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = call %struct.hostent* @gethostbyname(i8* %10)
store %struct.hostent* %11, %struct.hostent** %9
%12 = load %struct.hostent*, %struct.hostent** %9
%13 = icmp eq %struct.hostent* %12, null
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %5
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %4
br label %46
18: ; preds = %3
%19 = load %struct.hostent*, %struct.hostent** %9
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i32 0, i32 2
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
%25 = bitcast i32* %24 to i8*
%26 = load %struct.hostent*, %struct.hostent** %9
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 1
%28 = load i32*, i32** %27
%29 = getelementptr inbounds i32, i32* %28, i64 0
%30 = load i32, i32* %29
%31 = load %struct.hostent*, %struct.hostent** %9
%32 = getelementptr inbounds %struct.hostent, %struct.hostent* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call i32 @memcpy(i8* %25, i32 %30, i32 %33)
%35 = load i32, i32* %6
%36 = call i32 @htons(i32 %35)
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %36, i32* %37
%38 = load i32, i32* %7
%39 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%40 = call i32 @connect(i32 %38, %struct.sockaddr* %39, i32 12)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %18
%43 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %7
br label %44
44: ; preds = %42, %18
%45 = load i32, i32* %7
store i32 %45, i32* %4
br label %46
46: ; preds = %44, %14
%47 = load i32, i32* %4
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32*, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [31 x i8] c"Failed to resolve hostname %s!\00"
@.str.1 = [38 x i8] c"Error while connecting to target host\00"
define i32 @open_connection(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = tail call %struct.hostent* @gethostbyname(i8* %0)
%7 = icmp eq %struct.hostent* %6, null
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %30
11: ; preds = %3
%12 = getelementptr inbounds %struct.hostent, %struct.hostent* %6, i64 0, i32 2
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
%16 = bitcast i32* %15 to i8*
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %6, i64 0, i32 1
%18 = load i32*, i32** %17
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %6, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @memcpy(i8* %16, i32 %19, i32 %21)
%23 = call i32 @htons(i32 %1)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%26 = call i32 @connect(i32 %2, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %11
%29 = call i32 @perror(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %11, %28, %8
%31 = phi i32 [ -1, %8 ], [ -1, %28 ], [ %2, %11 ]
ret i32 %31
}