#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 */
typedef int /*<<< orphan*/ uint32_t;
typedef int /*<<< orphan*/ uint16_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  scalar_t__ *h_addr_list;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int /*<<< orphan*/ const);
int /*<<< orphan*/ inet_addr(char const *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int cl_connect(const char *remote_addr, const uint16_t port) {
  int conn_sock;
  struct sockaddr_in addr;
  struct hostent *resolved;
  int i = 0;
  if ((resolved = gethostbyname(remote_addr)) == NULL)
    return -1;
  memset(&addr, 0, sizeof(struct sockaddr_in));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  if (resolved->h_addr_list[0]) {
    addr.sin_addr.s_addr = *(uint32_t *)resolved->h_addr_list[i];
  } else {
    addr.sin_addr.s_addr = inet_addr(remote_addr);
  }
  if ((conn_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    return -1;
  if (connect(conn_sock, (struct sockaddr *)(&addr), sizeof(struct sockaddr)) ==
      -1)
    return -1;
  return conn_sock;
}


x86 assembly (clang, -O0):
<cl_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1199 <cl_connect+0x39>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <cl_connect+0xf3>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
cmpq   $0x0,(%rax)
je     11ea <cl_connect+0x8a>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
jmp    11f6 <cl_connect+0x96>
mov    -0x10(%rbp),%rdi
call   1050 <inet_addr@plt>
mov    %eax,-0x28(%rbp)
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1227 <cl_connect+0xc7>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <cl_connect+0xf3>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
jne    124d <cl_connect+0xed>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <cl_connect+0xf3>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cl_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r15d
mov    %rdi,%rbx
call   1060 <gethostbyname@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
je     11fd <cl_connect+0x9d>
mov    %rax,%rbp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e4d(%rip),%r12
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %r15d,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x0(%rbp),%rax
mov    (%rax),%rax
test   %rax,%rax
je     11bf <cl_connect+0x5f>
mov    (%rax),%eax
jmp    11c7 <cl_connect+0x67>
mov    %rbx,%rdi
call   1050 <inet_addr@plt>
mov    %eax,(%rsp)
mov    (%r12),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
cmp    $0xffffffff,%eax
je     11fd <cl_connect+0x9d>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebx
mov    %ebx,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i64* }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @cl_connect(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.hostent*
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %9
%10 = load i8*, i8** %4
%11 = call %struct.hostent* @gethostbyname(i8* %10)
store %struct.hostent* %11, %struct.hostent** %8
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %58
14: ; preds = %2
%15 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
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 1
store i32 %19, i32* %20
%21 = load %struct.hostent*, %struct.hostent** %8
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %21, i32 0, i32 0
%23 = load i64*, i64** %22
%24 = getelementptr inbounds i64, i64* %23, i64 0
%25 = load i64, i64* %24
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %39
27: ; preds = %14
%28 = load %struct.hostent*, %struct.hostent** %8
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %28, i32 0, i32 0
%30 = load i64*, i64** %29
%31 = load i32, i32* %9
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i64, i64* %30, i64 %32
%34 = load i64, i64* %33
%35 = inttoptr i64 %34 to i32*
%36 = load i32, i32* %35
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%38 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %37, i32 0, i32 0
store i32 %36, i32* %38
br label %44
39: ; preds = %14
%40 = load i8*, i8** %4
%41 = call i32 @inet_addr(i8* %40)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%43 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %42, i32 0, i32 0
store i32 %41, i32* %43
br label %44
44: ; preds = %39, %27
%45 = load i32, i32* @AF_INET
%46 = load i32, i32* @SOCK_STREAM
%47 = call i32 @socket(i32 %45, i32 %46, i32 0)
store i32 %47, i32* %6
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %50
49: ; preds = %44
store i32 -1, i32* %3
br label %58
50: ; preds = %44
%51 = load i32, i32* %6
%52 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%53 = call i32 @connect(i32 %51, %struct.sockaddr* %52, i32 4)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %56
55: ; preds = %50
store i32 -1, i32* %3
br label %58
56: ; preds = %50
%57 = load i32, i32* %6
store i32 %57, i32* %3
br label %58
58: ; preds = %56, %55, %49, %13
%59 = load i32, i32* %3
ret i32 %59
}

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