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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, char, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_tcp_connection(char *hostname, int port) {
  struct hostent *destination;
  struct sockaddr_in address;
  destination = gethostbyname(hostname);
  int tcp_handle = socket(AF_INET, SOCK_STREAM, 0);
  if (tcp_handle == -1) {
    printf("Exiting due to error: Unable to create TCP socket\n");
    exit(1);
  }
  memset(&(address.sin_zero), '\0', 8);
  address.sin_family = AF_INET;
  address.sin_port = htons(port);
  address.sin_addr = *((struct in_addr *)destination->h_addr_list[0]);
  if (connect(tcp_handle, (struct sockaddr *)&address,
              sizeof(struct sockaddr)) == -1) {
    printf("Exiting due to error: Unable to open TCP socket\n");
    exit(1);
  }
  return tcp_handle;
}


x86 assembly (clang, -O0):
<create_tcp_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11c8 <create_tcp_connection+0x58>
lea    0xe47(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x28(%rbp),%rdi
add    $0xc,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    1232 <create_tcp_connection+0xc2>
lea    0xe10(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_tcp_connection>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
call   1060 <gethostbyname@plt>
mov    %rax,%rbp
mov    0x2e5c(%rip),%r15
mov    (%r15),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     11fa <create_tcp_connection+0x8a>
mov    %eax,%ebx
lea    0x14(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x0(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
je     1203 <create_tcp_connection+0x93>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe2f(%rip),%rdi
jmp    120a <create_tcp_connection+0x9a>
lea    0xdf6(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i64* }
%struct.sockaddr_in = type { %struct.in_addr, i32, i32, i32 }
%struct.in_addr = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [51 x i8] c"Exiting due to error: Unable to create TCP socket\0A\00"
@.str.1 = [49 x i8] c"Exiting due to error: Unable to open TCP socket\0A\00"
define i32 @create_tcp_connection(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.hostent*
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i8*, i8** %3
%9 = call %struct.hostent* @gethostbyname(i8* %8)
store %struct.hostent* %9, %struct.hostent** %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* %7
%13 = load i32, i32* %7
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 3
%20 = call i32 @memset(i32* %19, i8 signext 0, i32 8)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* %4
%24 = call i32 @htons(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%27 = load %struct.hostent*, %struct.hostent** %5
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i32 0, i32 0
%29 = load i64*, i64** %28
%30 = getelementptr inbounds i64, i64* %29, i64 0
%31 = load i64, i64* %30
%32 = inttoptr i64 %31 to %struct.in_addr*
%33 = bitcast %struct.in_addr* %26 to i8*
%34 = bitcast %struct.in_addr* %32 to i8*
%35 = load i32, i32* %7
%36 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%37 = call i32 @connect(i32 %35, %struct.sockaddr* %36, i32 4)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %18
%40 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %18
%43 = load i32, i32* %7
ret i32 %43
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.in_addr, i32, i32, i32 }
%struct.in_addr = type { i32 }
%struct.hostent = type { i64* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@str = [48 x i8] c"Exiting due to error: Unable to open TCP socket\00"
@str.2 = [50 x i8] c"Exiting due to error: Unable to create TCP socket\00"
define i32 @create_tcp_connection(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call %struct.hostent* @gethostbyname(i8* %0)
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str.2, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 3
%15 = call i32 @memset(i32* %14, i8 signext 0, i32 8)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %16, i32* %17
%18 = call i32 @htons(i32 %1)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%21 = load i64*, i64** %20
%22 = load i64, i64* %21
%23 = inttoptr i64 %22 to i32*
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%25 = load i32, i32* %23
store i32 %25, i32* %24
%26 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%27 = call i32 @connect(i32 %8, %struct.sockaddr* %26, i32 4)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %13
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %13
ret i32 %8
}