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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ inet_aton(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int client_connect(const char *addr, uint16_t port) {
  int s;
  struct sockaddr_in sa;
  s = socket(AF_INET, SOCK_STREAM, 0);
  if (s < 0)
    return -1;
  sa.sin_family = AF_INET;
  sa.sin_port = htons(port);
  if (inet_aton(addr, &sa.sin_addr) == 0) {
    struct hostent *he;
    he = gethostbyname(addr);
    if (he == NULL) {
      close(s);
      return -1;
    }
    memcpy(&sa.sin_addr, he->h_addr, sizeof(struct in_addr));
  }
  if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
    close(s);
    return -1;
  }
  return s;
}


x86 assembly (clang, -O0):
<client_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11b1 <client_connect+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    1254 <client_connect+0xe4>
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <inet_aton@plt>
cmp    $0x0,%rax
jne    121f <client_connect+0xaf>
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    120b <client_connect+0x9b>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1254 <client_connect+0xe4>
lea    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    124e <client_connect+0xde>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1254 <client_connect+0xe4>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %rdi,%r15
mov    0x2e60(%rip),%r12
mov    (%r12),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     1207 <client_connect+0x97>
mov    %eax,%ebp
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %rsp,%rsi
mov    %r15,%rdi
call   1070 <inet_aton@plt>
test   %rax,%rax
jne    11e7 <client_connect+0x77>
mov    %r15,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     1200 <client_connect+0x90>
mov    (%rax),%esi
mov    %rsp,%rdi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %rax,%rax
js     1200 <client_connect+0x90>
mov    %ebp,%r14d
jmp    1207 <client_connect+0x97>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @client_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*
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 = load i32, i32* %6
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %49
15: ; preds = %2
%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 i8*, i8** %4
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%23 = call i64 @inet_aton(i8* %21, i32* %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %39
25: ; preds = %15
%26 = load i8*, i8** %4
%27 = call %struct.hostent* @gethostbyname(i8* %26)
store %struct.hostent* %27, %struct.hostent** %8
%28 = load %struct.hostent*, %struct.hostent** %8
%29 = icmp eq %struct.hostent* %28, null
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = load i32, i32* %6
%32 = call i32 @close(i32 %31)
store i32 -1, i32* %3
br label %49
33: ; preds = %25
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%35 = load %struct.hostent*, %struct.hostent** %8
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @memcpy(i32* %34, i32 %37, i32 4)
br label %39
39: ; preds = %33, %15
%40 = load i32, i32* %6
%41 = bitcast %struct.sockaddr_in* %7 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 = %39
%45 = load i32, i32* %6
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %3
br label %49
47: ; preds = %39
%48 = load i32, i32* %6
store i32 %48, i32* %3
br label %49
49: ; preds = %47, %44, %30, %14
%50 = load i32, i32* %3
ret i32 %50
}

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