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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int clientConnection(char *host_name, int port) {
  struct sockaddr_in serv_addr;
  struct hostent *hent;
  int socket_fd;
  int ret;
  socket_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (socket_fd < 0)
    return socket_fd;
  memset((char *)&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  hent = gethostbyname(host_name);
  if (hent == NULL) {
    close(socket_fd);
    return -1;
  }
  memcpy((char *)&serv_addr.sin_addr.s_addr, (char *)hent->h_addr,
         hent->h_length);
  serv_addr.sin_port = htons(port);
  serv_addr.sin_family = PF_INET;
  ret = connect(socket_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  if (ret < 0) {
    close(socket_fd);
    return (ret);
  }
  return socket_fd;
}


x86 assembly (clang, -O0):
<clientConnection>:
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,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11b0 <clientConnection+0x40>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1261 <clientConnection+0xf1>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f8 <clientConnection+0x88>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1261 <clientConnection+0xf1>
lea    -0x20(%rbp),%rdi
add    $0x8,%rdi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1070 <memcpy@plt>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    125b <clientConnection+0xeb>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1261 <clientConnection+0xf1>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clientConnection>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e61(%rip),%r15
mov    (%r15),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1215 <clientConnection+0xa5>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    %rbp,%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1207 <clientConnection+0x97>
lea    0x10(%rsp),%rdi
mov    0x8(%rax),%rsi
mov    (%rax),%edx
call   1070 <memcpy@plt>
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %eax,%eax
jns    1215 <clientConnection+0xa5>
mov    %eax,%ebp
jmp    120c <clientConnection+0x9c>
mov    $0xffffffff,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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