#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 */
typedef int /*<<< orphan*/ uint16_t;
struct in_addr {
  int dummy;
};
struct sockaddr_in {
  struct in_addr sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ *h_addr_list;
};
typedef int /*<<< orphan*/ name;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ printError(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int createConnectSocket(const char *hostname, uint16_t port) {
  int sock;
  struct sockaddr_in name;
  struct hostent *hostinfo;
  sock = socket(PF_INET, SOCK_STREAM, 0);
  if (sock == -1) {
    printError("Could not create socket");
    return -1;
  }
  name.sin_family = AF_INET;
  name.sin_port = htons(port);
  hostinfo = gethostbyname(hostname);
  if (!hostinfo) {
    printError("Could not lookup hostname");
    return -1;
  }
  name.sin_addr = *(struct in_addr *)hostinfo->h_addr_list[0];
  if (connect(sock, (struct sockaddr *)&name, sizeof(name)) == -1) {
    printError("Could not connect");
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<createConnectSocket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    119d <createConnectSocket+0x4d>
lea    0xe74(%rip),%rdi
call   1050 <printError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <createConnectSocket+0xdb>
mov    0x2e44(%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    11e4 <createConnectSocket+0x94>
lea    0xe45(%rip),%rdi
call   1050 <printError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <createConnectSocket+0xdb>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    1225 <createConnectSocket+0xd5>
lea    0xe1e(%rip),%rdi
call   1050 <printError@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <createConnectSocket+0xdb>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createConnectSocket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
cmp    $0xffffffff,%eax
je     11ca <createConnectSocket+0x7a>
mov    %eax,%ebx
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %r14,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     11d3 <createConnectSocket+0x83>
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    11e4 <createConnectSocket+0x94>
lea    0xe6a(%rip),%rdi
jmp    11da <createConnectSocket+0x8a>
lea    0xe2f(%rip),%rdi
jmp    11da <createConnectSocket+0x8a>
lea    0xe3e(%rip),%rdi
call   1050 <printError@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64* }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"Could not create socket\00"
@AF_INET = global i32 0
@.str.1 = [26 x i8] c"Could not lookup hostname\00"
@.str.2 = [18 x i8] c"Could not connect\00"
define i32 @createConnectSocket(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 eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @printError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %46
16: ; preds = %2
%17 = load i32, i32* @AF_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 ne %struct.hostent* %24, null
br i1 %25, label %28, label %26
26: ; preds = %16
%27 = call i32 @printError(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %46
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 0
%32 = load i64*, i64** %31
%33 = getelementptr inbounds i64, i64* %32, i64 0
%34 = load i64, i64* %33
%35 = inttoptr i64 %34 to %struct.in_addr*
%36 = bitcast %struct.in_addr* %29 to i8*
%37 = bitcast %struct.in_addr* %35 to i8*
%38 = load i32, i32* %6
%39 = bitcast %struct.sockaddr_in* %7 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 = %28
%43 = call i32 @printError(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %46
44: ; preds = %28
%45 = load i32, i32* %6
store i32 %45, i32* %3
br label %46
46: ; preds = %44, %42, %26, %14
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64* }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"Could not create socket\00"
@AF_INET = global i32 0
@.str.1 = [26 x i8] c"Could not lookup hostname\00"
@.str.2 = [18 x i8] c"Could not connect\00"
define i32 @createConnectSocket(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = load i32, i32* @PF_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 %11
9: ; preds = %2
%10 = tail call i32 @printError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %32
11: ; preds = %2
%12 = load i32, i32* @AF_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 = %11
%19 = tail call i32 @printError(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %32
20: ; preds = %11
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %16, i64 0, i32 0
%22 = load i64*, i64** %21
%23 = load i64, i64* %22
%24 = inttoptr i64 %23 to i32*
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%26 = load i32, i32* %24
store i32 %26, i32* %25
%27 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%28 = call i32 @connect(i32 %7, %struct.sockaddr* %27, i32 12)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %20
%31 = call i32 @printError(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %20, %30, %18, %9
%33 = phi i32 [ -1, %9 ], [ -1, %30 ], [ -1, %18 ], [ %7, %20 ]
ret i32 %33
}