#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;
  int /*<<< orphan*/ sin_family;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
struct hostent {
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ pin;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ debugPrint(char *);
int /*<<< orphan*/ debugPrintStr(char *, char *);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int createConnection(char *hostname, int port) {
  struct in_addr addr;
  struct sockaddr_in pin;
  struct hostent *hp;
  int sd = socket(AF_INET, SOCK_STREAM, 0);
  if (sd == -1) {
    debugPrint("connection : createConnection : Socket Error ");
    perror("socket");
    return -1;
  }
  debugPrintStr("connection : createConnection : Connecting via URL: ",
                hostname);
  memset(&pin, 0, sizeof(pin));
  hp = gethostbyname(hostname);
  if (hp == NULL) {
    debugPrint("connection : createConnection : Host is null. ");
    return -1;
  }
  pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
  pin.sin_family = AF_INET;
  pin.sin_port = htons(port);
  if (connect(sd, (struct sockaddr *)&pin, sizeof(pin)) < 0) {
    debugPrint("connection : createConnection : Connecting Error. ");
    perror("connect");
    return -1;
  } else {
    debugPrint("connection : createConnection : Succesful Connection ");
    return sd;
  }
}


x86 assembly (clang, -O0):
<createConnection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11d9 <createConnection+0x59>
lea    0xe44(%rip),%rdi
call   1070 <debugPrint@plt>
lea    0xe66(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <createConnection+0x11d>
mov    -0x10(%rbp),%rsi
lea    0xe51(%rip),%rdi
call   1030 <debugPrintStr@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1229 <createConnection+0xa9>
lea    0xe52(%rip),%rdi
call   1070 <debugPrint@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <createConnection+0x11d>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    128b <createConnection+0x10b>
lea    0xe2b(%rip),%rdi
call   1070 <debugPrint@plt>
lea    0xe52(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129d <createConnection+0x11d>
lea    0xe42(%rip),%rdi
call   1070 <debugPrint@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createConnection>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e51(%rip),%r15
mov    (%r15),%edi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     122c <createConnection+0xac>
mov    %eax,%ebx
lea    0xe7d(%rip),%rdi
mov    %rbp,%rsi
call   1030 <debugPrintStr@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    %rbp,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     121e <createConnection+0x9e>
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     1241 <createConnection+0xc1>
lea    0xebd(%rip),%rdi
call   1070 <debugPrint@plt>
jmp    125e <createConnection+0xde>
lea    0xe45(%rip),%rdi
call   1070 <debugPrint@plt>
jmp    1259 <createConnection+0xd9>
lea    0xdcd(%rip),%rdi
call   1070 <debugPrint@plt>
lea    0xdef(%rip),%rdi
jmp    1254 <createConnection+0xd4>
lea    0xe51(%rip),%rdi
call   1070 <debugPrint@plt>
lea    0xe78(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [46 x i8] c"connection : createConnection : Socket Error \00"
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [53 x i8] c"connection : createConnection : Connecting via URL: \00"
@.str.3 = [47 x i8] c"connection : createConnection : Host is null. \00"
@.str.4 = [51 x i8] c"connection : createConnection : Connecting Error. \00"
@.str.5 = [8 x i8] c"connect\00"
@.str.6 = [54 x i8] c"connection : createConnection : Succesful Connection \00"
define i32 @createConnection(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.in_addr
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.hostent*
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @debugPrint(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %52
18: ; preds = %2
%19 = load i8*, i8** %4
%20 = call i32 @debugPrintStr(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%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 = %18
%27 = call i32 @debugPrint(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %52
28: ; preds = %18
%29 = load %struct.hostent*, %struct.hostent** %8
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 0
%31 = load i64, i64* %30
%32 = inttoptr i64 %31 to %struct.in_addr*
%33 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
store i32 %34, i32* %36
%37 = load i32, i32* @AF_INET
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %37, i32* %38
%39 = load i32, i32* %5
%40 = call i32 @htons(i32 %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %40, i32* %41
%42 = load i32, i32* %9
%43 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%44 = call i64 @connect(i32 %42, %struct.sockaddr* %43, i32 12)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %28
%47 = call i32 @debugPrint(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.4, i64 0, i64 0))
%48 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %52
49: ; preds = %28
%50 = call i32 @debugPrint(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.6, i64 0, i64 0))
%51 = load i32, i32* %9
store i32 %51, i32* %3
br label %52
52: ; preds = %49, %46, %26, %15
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [46 x i8] c"connection : createConnection : Socket Error \00"
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [53 x i8] c"connection : createConnection : Connecting via URL: \00"
@.str.3 = [47 x i8] c"connection : createConnection : Host is null. \00"
@.str.4 = [51 x i8] c"connection : createConnection : Connecting Error. \00"
@.str.5 = [8 x i8] c"connect\00"
@.str.6 = [54 x i8] c"connection : createConnection : Succesful Connection \00"
define i32 @createConnection(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = tail call i32 @debugPrint(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %38
12: ; preds = %2
%13 = tail call i32 @debugPrintStr(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%14 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%15 = call %struct.hostent* @gethostbyname(i8* %0)
%16 = icmp eq %struct.hostent* %15, null
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @debugPrint(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
br label %38
19: ; preds = %12
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i64 0, i32 0
%21 = load i64, i64* %20
%22 = inttoptr i64 %21 to %struct.in_addr*
%23 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %22, i64 0, i32 0
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %26, i32* %27
%28 = call i32 @htons(i32 %1)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %28, i32* %29
%30 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%31 = call i64 @connect(i32 %7, %struct.sockaddr* %30, i32 12)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %19
%34 = call i32 @debugPrint(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.4, i64 0, i64 0))
%35 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
br label %38
36: ; preds = %19
%37 = call i32 @debugPrint(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.6, i64 0, i64 0))
br label %38
38: ; preds = %36, %33, %17, %9
%39 = phi i32 [ -1, %9 ], [ -1, %17 ], [ -1, %33 ], [ %7, %36 ]
ret i32 %39
}