#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ *h_addr_list;
};
typedef int /*<<< orphan*/ serveraddr;
typedef int /*<<< orphan*/ __CONST_SOCKADDR_ARG;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
scalar_t__ connect(int, int /*<<< orphan*/, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int open_clientfd(char *hostname, int port) {
  int clientfd;
  struct hostent *hp;
  struct sockaddr_in serveraddr;
  if ((clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    return -1;
  if ((hp = gethostbyname(hostname)) == NULL)
    return -2;
  bzero((char *)&serveraddr, sizeof(serveraddr));
  serveraddr.sin_family = AF_INET;
  bcopy((char *)hp->h_addr_list[0], (char *)&serveraddr.sin_addr.s_addr,
        hp->h_length);
  serveraddr.sin_port = htons(port);
  if (connect(clientfd, (__CONST_SOCKADDR_ARG)&serveraddr, sizeof(serveraddr)) <
      0)
    return -1;
  return clientfd;
}


x86 assembly (clang, -O0):
<open_clientfd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11a0 <open_clientfd+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    1235 <open_clientfd+0xd5>
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11c3 <open_clientfd+0x63>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1235 <open_clientfd+0xd5>
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    0x2e10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rdi
lea    -0x30(%rbp),%rsi
add    $0x4,%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
call   1050 <bcopy@plt>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rax
mov    %eax,%esi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0x0,%rax
jge    122f <open_clientfd+0xcf>
movl   $0xffffffff,-0x4(%rbp)
jmp    1235 <open_clientfd+0xd5>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_clientfd>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e6f(%rip),%r12
mov    (%r12),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
test   %eax,%eax
js     11f6 <open_clientfd+0x96>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     11fd <open_clientfd+0x9d>
mov    %rax,%rbp
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    0x8(%rbp),%rax
mov    (%rax),%rsi
lea    0x4(%rsp),%rdi
mov    0x0(%rbp),%edx
call   1050 <memmove@plt>
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r15d,%esi
mov    $0xc,%edx
call   1070 <connect@plt>
test   %rax,%rax
mov    $0xffffffff,%eax
cmovns %ebx,%eax
jmp    1202 <open_clientfd+0xa2>
mov    $0xffffffff,%eax
jmp    1202 <open_clientfd+0xa2>
mov    $0xfffffffe,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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