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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int establishSocket(struct hostent *hostName, int hostPort) {
  int socketDescriptor;
  if ((socketDescriptor = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    printf("Error creating a socket");
    exit(0);
  }
  struct sockaddr_in serverAddr;
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_port = htons(hostPort);
  memcpy(&serverAddr.sin_addr.s_addr, hostName->h_addr, hostName->h_length);
  if (connect(socketDescriptor, (struct sockaddr *)&serverAddr,
              sizeof serverAddr) != 0) {
    printf("Error connecting to server");
    exit(0);
  }
  return socketDescriptor;
}


x86 assembly (clang, -O0):
<establishSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x10(%rbp)
cmp    $0xffffffff,%eax
jne    11a7 <establishSocket+0x47>
lea    0xe65(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x8(%rbp),%rax
mov    (%rax),%edx
call   1050 <memcpy@plt>
mov    -0x10(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
je     1202 <establishSocket+0xa2>
lea    0xe22(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<establishSocket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e72(%rip),%r15
mov    (%r15),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11d7 <establishSocket+0x77>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%r14),%edx
mov    0x4(%r14),%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
jne    11e0 <establishSocket+0x80>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe22(%rip),%rdi
jmp    11e7 <establishSocket+0x87>
lea    0xe31(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i32, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"Error creating a socket\00"
@.str.1 = [27 x i8] c"Error connecting to server\00"
define i32 @establishSocket(%struct.hostent* %0, i32 %1) {
%3 = alloca %struct.hostent*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store %struct.hostent* %0, %struct.hostent** %3
store i32 %1, i32* %4
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %5
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %2
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* %4
%18 = call i32 @htons(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
%22 = load %struct.hostent*, %struct.hostent** %3
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %22, i32 0, i32 1
%24 = load i32, i32* %23
%25 = load %struct.hostent*, %struct.hostent** %3
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @memcpy(i32* %21, i32 %24, i32 %27)
%29 = load i32, i32* %5
%30 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%31 = call i64 @connect(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %14
%34 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%35 = call i32 @exit(i32 0)
unreachable
36: ; preds = %14
%37 = load i32, i32* %5
ret i32 %37
}

LLVM IR (-O2):
%struct.hostent = type { i32, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"Error creating a socket\00"
@.str.1 = [27 x i8] c"Error connecting to server\00"
define i32 @establishSocket(%struct.hostent* nocapture %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %2
%12 = bitcast %struct.sockaddr_in* %3 to i8*
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 %1)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %0, i64 0, i32 1
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %0, i64 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @memcpy(i32* %17, i32 %19, i32 %21)
%23 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%24 = call i64 @connect(i32 %6, %struct.sockaddr* %23, i32 12)
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %11
%27 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %11
ret i32 %6
}