#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__ {
  unsigned long s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ server;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ atoi(char const *);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int connectToServer(const char *hostnameParam, const char *portParam) {
  unsigned short port;
  struct hostent *hostname;
  struct sockaddr_in server;
  int returnSocket;
  hostname = gethostbyname(hostnameParam);
  if (hostname == (struct hostent *)0) {
    fprintf(stderr, "Gethostbyname failed\n");
    exit(2);
  }
  port = (unsigned short)atoi(portParam);
  server.sin_family = AF_INET;
  server.sin_port = htons(port);
  server.sin_addr.s_addr = *((unsigned long *)hostname->h_addr);
  if ((returnSocket = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    perror("Socket()");
    exit(3);
  }
  if (connect(returnSocket, (struct sockaddr *)&server, sizeof(server)) < 0) {
    perror("Connect()");
    exit(4);
  }
  return returnSocket;
}


x86 assembly (clang, -O0):
<connectToServer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11c7 <connectToServer+0x47>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe48(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1070 <atoi@plt>
mov    %ax,-0x12(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
movzwl -0x12(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    1235 <connectToServer+0xb5>
lea    0xdf0(%rip),%rdi
call   1060 <perror@plt>
mov    $0x3,%edi
call   1080 <exit@plt>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    1266 <connectToServer+0xe6>
lea    0xdc8(%rip),%rdi
call   1060 <perror@plt>
mov    $0x4,%edi
call   1080 <exit@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connectToServer>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     1202 <connectToServer+0x82>
mov    %rax,%rbx
mov    %r14,%rdi
call   1070 <atoi@plt>
mov    0x2e42(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0x14(%rsp)
movzwl %ax,%edi
call   1030 <htons@plt>
mov    %eax,0x10(%rsp)
mov    (%rbx),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1221 <connectToServer+0xa1>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     1237 <connectToServer+0xb7>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
lea    0xdee(%rip),%rdi
call   1060 <perror@plt>
mov    $0x3,%edi
call   1080 <exit@plt>
lea    0xde1(%rip),%rdi
call   1060 <perror@plt>
mov    $0x4,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [22 x i8] c"Gethostbyname failed\0A\00"
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [9 x i8] c"Socket()\00"
@.str.2 = [10 x i8] c"Connect()\00"
define i32 @connectToServer(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i16
%6 = alloca %struct.hostent*
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = call %struct.hostent* @gethostbyname(i8* %9)
store %struct.hostent* %10, %struct.hostent** %6
%11 = load %struct.hostent*, %struct.hostent** %6
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 2)
unreachable
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = call i64 @atoi(i8* %18)
%20 = trunc i64 %19 to i16
store i16 %20, i16* %5
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %21, i32* %22
%23 = load i16, i16* %5
%24 = call i32 @htons(i16 zeroext %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %24, i32* %25
%26 = load %struct.hostent*, %struct.hostent** %6
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 0
%28 = load i64, i64* %27
%29 = inttoptr i64 %28 to i64*
%30 = load i64, i64* %29
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 0
store i64 %30, i64* %32
%33 = load i32, i32* @PF_INET
%34 = load i32, i32* @SOCK_STREAM
%35 = call i32 @socket(i32 %33, i32 %34, i32 0)
store i32 %35, i32* %8
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %17
%38 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%39 = call i32 @exit(i32 3)
unreachable
40: ; preds = %17
%41 = load i32, i32* %8
%42 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%43 = call i64 @connect(i32 %41, %struct.sockaddr* %42, i32 16)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %40
%46 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%47 = call i32 @exit(i32 4)
unreachable
48: ; preds = %40
%49 = load i32, i32* %8
ret i32 %49
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i64 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [22 x i8] c"Gethostbyname failed\0A\00"
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [9 x i8] c"Socket()\00"
@.str.2 = [10 x i8] c"Connect()\00"
define i32 @connectToServer(i8* %0, i8* nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call %struct.hostent* @gethostbyname(i8* %0)
%6 = icmp eq %struct.hostent* %5, null
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 2)
unreachable
11: ; preds = %2
%12 = tail call i64 @atoi(i8* %1)
%13 = trunc i64 %12 to i16
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %14, i32* %15
%16 = tail call i32 @htons(i16 zeroext %13)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%19 = load i64, i64* %18
%20 = inttoptr i64 %19 to i64*
%21 = load i64, i64* %20
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i64 %21, i64* %22
%23 = load i32, i32* @PF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = tail call i32 @socket(i32 %23, i32 %24, i32 0)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %11
%28 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%29 = tail call i32 @exit(i32 3)
unreachable
30: ; preds = %11
%31 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%32 = call i64 @connect(i32 %25, %struct.sockaddr* %31, i32 16)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 4)
unreachable
37: ; preds = %30
ret i32 %25
}