#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_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int resolv_hostname(char const *, struct sockaddr_in *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_connection_to(const char *remote_name, int remote_port) {
  int fd;
  struct sockaddr_in serv_addr;
  if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
    perror("ERROR opening socket");
    return 0;
  }
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(remote_port);
  if (resolv_hostname(remote_name, &serv_addr) == -1) {
    return 0;
  }
  if (connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) {
    perror("ERROR connecting");
    return 0;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_connection_to>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
mov    0x2e68(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11a3 <open_connection_to+0x53>
lea    0xe6e(%rip),%rdi
call   1040 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1214 <open_connection_to+0xc4>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <resolv_hostname@plt>
cmp    $0xffffffff,%eax
jne    11dc <open_connection_to+0x8c>
movl   $0x0,-0x4(%rbp)
jmp    1214 <open_connection_to+0xc4>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    120e <open_connection_to+0xbe>
lea    0xe18(%rip),%rdi
call   1040 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1214 <open_connection_to+0xc4>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_connection_to>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2e85(%rip),%r15
mov    (%r15),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    0x2e6a(%rip),%rax
mov    (%rax),%edx
call   1070 <socket@plt>
test   %eax,%eax
js     11cb <open_connection_to+0x7b>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0x4(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %r14,%rdi
call   1050 <resolv_hostname@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
je     11c0 <open_connection_to+0x70>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   1060 <connect@plt>
mov    %eax,%ecx
mov    %ebx,%eax
cmp    $0xffffffff,%ecx
je     11d4 <open_connection_to+0x84>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe2e(%rip),%rdi
jmp    11db <open_connection_to+0x8b>
lea    0xe3a(%rip),%rdi
call   1040 <perror@plt>
xor    %eax,%eax
jmp    11c0 <open_connection_to+0x70>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@.str.1 = [17 x i8] c"ERROR connecting\00"
define i32 @open_connection_to(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = load i32, i32* @IPPROTO_TCP
%11 = call i32 @socket(i32 %8, i32 %9, i32 %10)
store i32 %11, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %34
15: ; preds = %2
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* %5
%19 = call i32 @htons(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %19, i32* %20
%21 = load i8*, i8** %4
%22 = call i32 @resolv_hostname(i8* %21, %struct.sockaddr_in* %7)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %15
store i32 0, i32* %3
br label %34
25: ; preds = %15
%26 = load i32, i32* %6
%27 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%28 = call i32 @connect(i32 %26, %struct.sockaddr* %27, i32 8)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %34
32: ; preds = %25
%33 = load i32, i32* %6
store i32 %33, i32* %3
br label %34
34: ; preds = %32, %30, %24, %13
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [21 x i8] c"ERROR opening socket\00"
@.str.1 = [17 x i8] c"ERROR connecting\00"
define i32 @open_connection_to(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 = load i32, i32* @IPPROTO_TCP
%8 = tail call i32 @socket(i32 %5, i32 %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %25
12: ; preds = %2
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 %1)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %15, i32* %16
%17 = call i32 @resolv_hostname(i8* %0, %struct.sockaddr_in* %3)
%18 = icmp eq i32 %17, -1
br i1 %18, label %25, label %19
19: ; preds = %12
%20 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%21 = call i32 @connect(i32 %8, %struct.sockaddr* %20, i32 8)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %19, %12, %23, %10
%26 = phi i32 [ 0, %10 ], [ 0, %23 ], [ 0, %12 ], [ %8, %19 ]
ret i32 %26
}