#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*/ target_addr;
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, char, int);
int /*<<< orphan*/ set_nonblock(int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_remote_host(char *host, int port) {
  struct hostent *hostinfo;
  int target_sockfd;
  int on = 1;
  struct sockaddr_in target_addr;
  if (!(hostinfo = gethostbyname(host)))
    return -2;
  if ((target_sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    return target_sockfd;
  setsockopt(target_sockfd, SOL_SOCKET, SO_REUSEADDR, &on, 4);
  memset(&target_addr, '\0', sizeof(target_addr));
  target_addr.sin_family = AF_INET;
  memcpy(&target_addr.sin_addr, hostinfo->h_addr, hostinfo->h_length);
  target_addr.sin_port = htons(port);
  if (connect(target_sockfd, (struct sockaddr *)&target_addr,
              sizeof(target_addr)) < 0) {
    close(target_sockfd);
    return -1;
  }
  set_nonblock(target_sockfd);
  return target_sockfd;
}


x86 assembly (clang, -O0):
<open_remote_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11c9 <open_remote_host+0x39>
movl   $0xfffffffe,-0x4(%rbp)
jmp    129b <open_remote_host+0x10b>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11f9 <open_remote_host+0x69>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129b <open_remote_host+0x10b>
mov    -0x24(%rbp),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
lea    -0x38(%rbp),%rdi
add    $0x4,%rdi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1080 <memcpy@plt>
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x38(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    128d <open_remote_host+0xfd>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129b <open_remote_host+0x10b>
mov    -0x24(%rbp),%edi
call   1090 <set_nonblock@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_remote_host>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
movl   $0x1,0xc(%rsp)
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     1256 <open_remote_host+0xc6>
mov    %rax,%rbp
mov    0x2e21(%rip),%r12
mov    (%r12),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1269 <open_remote_host+0xd9>
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    0x2ddb(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
lea    0x14(%rsp),%rdi
mov    0x0(%rbp),%edx
mov    0x4(%rbp),%esi
call   1080 <memcpy@plt>
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
test   %rax,%rax
js     125d <open_remote_host+0xcd>
mov    %ebx,%edi
call   1090 <set_nonblock@plt>
jmp    1269 <open_remote_host+0xd9>
mov    $0xfffffffe,%ebx
jmp    1269 <open_remote_host+0xd9>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i32, i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @open_remote_host(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.hostent*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 1, i32* %8
%10 = load i8*, i8** %4
%11 = call %struct.hostent* @gethostbyname(i8* %10)
store %struct.hostent* %11, %struct.hostent** %6
%12 = icmp ne %struct.hostent* %11, null
br i1 %12, label %14, label %13
13: ; preds = %2
store i32 -2, i32* %3
br label %51
14: ; preds = %2
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = load i32, i32* %7
store i32 %20, i32* %3
br label %51
21: ; preds = %14
%22 = load i32, i32* %7
%23 = load i32, i32* @SOL_SOCKET
%24 = load i32, i32* @SO_REUSEADDR
%25 = call i32 @setsockopt(i32 %22, i32 %23, i32 %24, i32* %8, i32 4)
%26 = call i32 @memset(%struct.sockaddr_in* %9, i8 signext 0, i32 12)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%30 = load %struct.hostent*, %struct.hostent** %6
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i32 0, i32 1
%32 = load i32, i32* %31
%33 = load %struct.hostent*, %struct.hostent** %6
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @memcpy(i32* %29, i32 %32, i32 %35)
%37 = load i32, i32* %5
%38 = call i32 @htons(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %38, i32* %39
%40 = load i32, i32* %7
%41 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%42 = call i64 @connect(i32 %40, %struct.sockaddr* %41, i32 12)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %21
%45 = load i32, i32* %7
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %3
br label %51
47: ; preds = %21
%48 = load i32, i32* %7
%49 = call i32 @set_nonblock(i32 %48)
%50 = load i32, i32* %7
store i32 %50, i32* %3
br label %51
51: ; preds = %47, %44, %19, %13
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
define i32 @open_remote_host(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = tail call %struct.hostent* @gethostbyname(i8* %0)
%8 = icmp eq %struct.hostent* %7, null
br i1 %8, label %36, label %9
9: ; preds = %2
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %36, label %14
14: ; preds = %9
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_REUSEADDR
%17 = call i32 @setsockopt(i32 %12, i32 %15, i32 %16, i32* %3, i32 4)
%18 = call i32 @memset(%struct.sockaddr_in* %4, i8 signext 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 1
%23 = load i32, i32* %22
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @memcpy(i32* %21, i32 %23, i32 %25)
%27 = call i32 @htons(i32 %1)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %27, i32* %28
%29 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%30 = call i64 @connect(i32 %12, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %14
%33 = call i32 @close(i32 %12)
br label %36
34: ; preds = %14
%35 = call i32 @set_nonblock(i32 %12)
br label %36
36: ; preds = %9, %2, %34, %32
%37 = phi i32 [ -1, %32 ], [ %12, %34 ], [ -2, %2 ], [ %12, %9 ]
ret i32 %37
}