#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int openConnection(char *hostname, int port) {
  int sd = socket(PF_INET, SOCK_STREAM, 0);
  if (sd < 0) {
    perror("socket");
    return -1;
  }
  struct sockaddr_in addr;
  addr.sin_family = PF_INET;
  addr.sin_port = htons(port);
  struct hostent *hptr;
  hptr = gethostbyname(hostname);
  if (hptr == NULL) {
    perror("Error resolving hostname");
    return -1;
  }
  memcpy(&addr.sin_addr, hptr->h_addr, hptr->h_length);
  if (connect(sd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("Failed to establish connection");
    return -1;
  }
  return sd;
}


x86 assembly (clang, -O0):
<openConnection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ad <openConnection+0x4d>
lea    0xe64(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <openConnection+0xe3>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11f4 <openConnection+0x94>
lea    0xe24(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <openConnection+0xe3>
lea    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    123d <openConnection+0xdd>
lea    0xdf4(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <openConnection+0xe3>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openConnection>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e62(%rip),%r15
mov    (%r15),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11e1 <openConnection+0x81>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %r14,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     11ea <openConnection+0x8a>
mov    (%rax),%edx
mov    0x4(%rax),%esi
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <memcpy@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
test   %rax,%rax
js     11f3 <openConnection+0x93>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe18(%rip),%rdi
jmp    11fa <openConnection+0x9a>
lea    0xe16(%rip),%rdi
jmp    11fa <openConnection+0x9a>
lea    0xe26(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11d4 <openConnection+0x74>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@.str.1 = [25 x i8] c"Error resolving hostname\00"
@.str.2 = [31 x i8] c"Failed to establish connection\00"
define i32 @openConnection(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %34
10: ; preds = %2
%11 = bitcast %struct.sockaddr_in* %3 to i8*
%12 = load i32, i32* @PF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %12, i32* %13
%14 = tail call i32 @htons(i32 %1)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %14, i32* %15
%16 = tail call %struct.hostent* @gethostbyname(i8* %0)
%17 = icmp eq %struct.hostent* %16, null
br i1 %17, label %18, label %20
18: ; preds = %10
%19 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %32
20: ; preds = %10
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %16, i64 0, i32 1
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %16, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @memcpy(i32* %21, i32 %23, i32 %25)
%27 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%28 = call i64 @connect(i32 %6, %struct.sockaddr* %27, i32 12)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %20
%31 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %20, %30, %18
%33 = phi i32 [ -1, %18 ], [ -1, %30 ], [ %6, %20 ]
br label %34
34: ; preds = %32, %8
%35 = phi i32 [ -1, %8 ], [ %33, %32 ]
ret i32 %35
}