#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_family;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_addrtype;
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(void *, void *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int remote_socket(const char *host, int port) {
  struct sockaddr_in sa;
  struct hostent *hp;
  int s;
  if (!(hp = gethostbyname(host))) {
    return -1;
  }
  memset(&sa, 0, sizeof(sa));
  sa.sin_port = htons(port);
  sa.sin_family = hp->h_addrtype;
  memcpy((void *)&sa.sin_addr, (void *)hp->h_addr, hp->h_length);
  if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
    return -1;
  }
  if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) != 0) {
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<remote_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11a2 <remote_socket+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    1241 <remote_socket+0xd1>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edx
call   1070 <memcpy@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    120c <remote_socket+0x9c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1241 <remote_socket+0xd1>
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
je     123b <remote_socket+0xcb>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1241 <remote_socket+0xd1>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remote_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
call   1060 <gethostbyname@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     11fd <remote_socket+0x8d>
mov    %rax,%rbx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x10(%rsp)
mov    (%rbx),%eax
mov    %eax,0xc(%rsp)
mov    0x8(%rbx),%rsi
mov    0x4(%rbx),%edx
mov    %r15,%rdi
call   1070 <memcpy@plt>
mov    (%rbx),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     11fd <remote_socket+0x8d>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %rax,%rax
je     11fa <remote_socket+0x8a>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11fd <remote_socket+0x8d>
mov    %ebx,%r14d
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32, i64 }
%struct.sockaddr = type { i32 }
@SOCK_STREAM = global i32 0
define i32 @remote_socket(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 = icmp eq %struct.hostent* %5, null
br i1 %6, label %30, label %7
7: ; preds = %2
%8 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%9 = call i32 @htons(i32 %1)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %9, i32* %10
%11 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%12 = load i32, i32* %11
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 2
%15 = load i64, i64* %14
%16 = inttoptr i64 %15 to i8*
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 1
%18 = load i32, i32* %17
%19 = call i32 @memcpy(i8* %4, i8* %16, i32 %18)
%20 = load i32, i32* %11
%21 = load i32, i32* @SOCK_STREAM
%22 = call i32 @socket(i32 %20, i32 %21, i32 0)
%23 = icmp slt i32 %22, 0
br i1 %23, label %30, label %24
24: ; preds = %7
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i64 @connect(i32 %22, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = call i32 @close(i32 %22)
br label %30
30: ; preds = %24, %7, %2, %28
%31 = phi i32 [ -1, %28 ], [ -1, %2 ], [ -1, %7 ], [ %22, %24 ]
ret i32 %31
}