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

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ construct_sockaddr_in(struct sockaddr_in *,
                                         int /*<<< orphan*/,
                                         int /*<<< orphan*/);
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ inet_addr(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int init_remote_server_socket() {
  int remote_server_socket;
  struct sockaddr_in remote_server_addr;
  unsigned short remote_server_port = 80;
  if ((remote_server_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    die("socket() failed");
  construct_sockaddr_in(&remote_server_addr, inet_addr("10.0.0.2"),
                        htons(remote_server_port));
  if (connect(remote_server_socket, (struct sockaddr *)&remote_server_addr,
              sizeof(remote_server_addr)) < 0)
    die("connect() failed");
  return remote_server_socket;
}


x86 assembly (clang, -O0):
<init_remote_server_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movw   $0x50,-0xa(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    0x2e61(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,-0x4(%rbp)
cmp    $0x0,%eax
jge    11a6 <init_remote_server_socket+0x46>
lea    0xe5f(%rip),%rdi
call   1060 <die@plt>
lea    0xe63(%rip),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x10(%rbp)
movzwl -0xa(%rbp),%edi
call   1040 <htons@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%edx
lea    -0x8(%rbp),%rdi
call   1030 <construct_sockaddr_in@plt>
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    11f3 <init_remote_server_socket+0x93>
lea    0xe2b(%rip),%rdi
call   1060 <die@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_remote_server_socket>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
mov    0x2e67(%rip),%rax
mov    (%rax),%edx
call   1080 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    119a <init_remote_server_socket+0x3a>
lea    0xe6b(%rip),%rdi
call   1060 <die@plt>
lea    0xe6f(%rip),%rdi
call   1050 <inet_addr@plt>
mov    %eax,%ebp
mov    $0x50,%edi
call   1040 <htons@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    %ebp,%esi
mov    %eax,%edx
call   1030 <construct_sockaddr_in@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
test   %rax,%rax
jns    11e3 <init_remote_server_socket+0x83>
lea    0xe3b(%rip),%rdi
call   1060 <die@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [16 x i8] c"socket() failed\00"
@.str.1 = [9 x i8] c"10.0.0.2\00"
@.str.2 = [17 x i8] c"connect() failed\00"
define i32 @init_remote_server_socket() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i16
store i16 80, i16* %3
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = load i32, i32* @IPPROTO_TCP
%7 = call i32 @socket(i32 %4, i32 %5, i32 %6)
store i32 %7, i32* %1
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = call i32 @inet_addr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%13 = load i16, i16* %3
%14 = call i32 @htons(i16 zeroext %13)
%15 = call i32 @construct_sockaddr_in(%struct.sockaddr_in* %2, i32 %12, i32 %14)
%16 = load i32, i32* %1
%17 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%18 = call i64 @connect(i32 %16, %struct.sockaddr* %17, i32 4)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = call i32 @die(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %11
%23 = load i32, i32* %1
ret i32 %23
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [16 x i8] c"socket() failed\00"
@.str.1 = [9 x i8] c"10.0.0.2\00"
@.str.2 = [17 x i8] c"connect() failed\00"
define i32 @init_remote_server_socket() {
%1 = alloca %struct.sockaddr_in
%2 = bitcast %struct.sockaddr_in* %1 to i8*
%3 = load i32, i32* @PF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = load i32, i32* @IPPROTO_TCP
%6 = tail call i32 @socket(i32 %3, i32 %4, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @die(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = tail call i32 @inet_addr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @htons(i16 zeroext 80)
%13 = call i32 @construct_sockaddr_in(%struct.sockaddr_in* %1, i32 %11, i32 %12)
%14 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%15 = call i64 @connect(i32 %6, %struct.sockaddr* %14, i32 4)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %10
%18 = call i32 @die(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %19
19: ; preds = %17, %10
ret i32 %6
}