#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ server_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_connection(char *host, int port) {
  struct sockaddr_in server_addr;
  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(port);
  if (inet_pton(AF_INET, host, &server_addr.sin_addr) < 0) {
    printf("Error processing the hostname\n");
    return -1;
  }
  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    printf("Failed to create socket\n");
    return -1;
  }
  if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) <
      0) {
    printf("Failed to connect to %s:%d\n", host, port);
    return -1;
  }
  return sockfd;
}


x86 assembly (clang, -O0):
<open_connection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1060 <inet_pton@plt>
cmp    $0x0,%rax
jge    11d0 <open_connection+0x70>
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1252 <open_connection+0xf2>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1210 <open_connection+0xb0>
lea    0xe22(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1252 <open_connection+0xf2>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    124c <open_connection+0xec>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1252 <open_connection+0xf2>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_connection>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e4d(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    (%rbx),%edi
mov    %r15,%rsi
mov    %rbp,%rdx
call   1070 <inet_pton@plt>
test   %rax,%rax
js     1203 <open_connection+0x93>
mov    (%rbx),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     120c <open_connection+0x9c>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %rax,%rax
jns    121d <open_connection+0xad>
lea    0xe0c(%rip),%rdi
mov    %r15,%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1218 <open_connection+0xa8>
lea    0xe2a(%rip),%rdi
jmp    1213 <open_connection+0xa3>
lea    0xe09(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [31 x i8] c"Error processing the hostname\0A\00"
@SOCK_STREAM = global i32 0
@.str.1 = [25 x i8] c"Failed to create socket\0A\00"
@.str.2 = [28 x i8] c"Failed to connect to %s:%d\0A\00"
define i32 @open_connection(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %9, i32* %10
%11 = load i32, i32* %5
%12 = call i32 @htons(i32 %11)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* @AF_INET
%15 = load i8*, i8** %4
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%17 = call i64 @inet_pton(i32 %14, i8* %15, i32* %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %40
21: ; preds = %2
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %40
29: ; preds = %21
%30 = load i32, i32* %7
%31 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%32 = call i64 @connect(i32 %30, %struct.sockaddr* %31, i32 12)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = load i8*, i8** %4
%36 = load i32, i32* %5
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %35, i32 %36)
store i32 -1, i32* %3
br label %40
38: ; preds = %29
%39 = load i32, i32* %7
store i32 %39, i32* %3
br label %40
40: ; preds = %38, %34, %27, %19
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [28 x i8] c"Failed to connect to %s:%d\0A\00"
@str = [24 x i8] c"Failed to create socket\00"
@str.3 = [30 x i8] c"Error processing the hostname\00"
define i32 @open_connection(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %6, i32* %7
%8 = call i32 @htons(i32 %1)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %8, i32* %9
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%12 = call i64 @inet_pton(i32 %10, i8* %0, i32* %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.3, i64 0, i64 0))
br label %29
16: ; preds = %2
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %29
23: ; preds = %16
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i64 @connect(i32 %19, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %1)
br label %29
29: ; preds = %21, %27, %23, %14
%30 = phi i32 [ -1, %14 ], [ -1, %21 ], [ -1, %27 ], [ %19, %23 ]
ret i32 %30
}