#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;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
int connect(int, struct sockaddr_in const *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to_batch_server(struct hostent *server, int port) {
  int sock_handle, n;
  struct sockaddr_in serv_addr;
  if (-1 == (sock_handle = socket(AF_INET, SOCK_STREAM, 0))) {
    perror("Socket create failed");
    return -1;
  }
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr,
        server->h_length);
  serv_addr.sin_port = htons(port);
  if (-1 == connect(sock_handle, (const struct sockaddr_in *)&serv_addr,
                    sizeof(serv_addr))) {
    perror("Connection failed");
    return -1;
  }
  return sock_handle;
}


x86 assembly (clang, -O0):
<connect_to_batch_server>:
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,%ecx
mov    %ecx,-0x18(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11b2 <connect_to_batch_server+0x52>
lea    0xe5f(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <connect_to_batch_server+0xd0>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
add    $0x4,%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   1040 <bcopy@plt>
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    122a <connect_to_batch_server+0xca>
lea    0xdfc(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <connect_to_batch_server+0xd0>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_batch_server>:
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>
cmp    $0xffffffff,%eax
je     11eb <connect_to_batch_server+0x8b>
mov    %eax,%ebx
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   1060 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x8(%rsp)
mov    0x8(%rbp),%rsi
lea    0x4(%rsp),%rdi
mov    0x0(%rbp),%edx
call   1040 <memmove@plt>
mov    %r14d,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     11f4 <connect_to_batch_server+0x94>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe0e(%rip),%rdi
jmp    11fb <connect_to_batch_server+0x9b>
lea    0xe1a(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11dc <connect_to_batch_server+0x7c>

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
@.str = [21 x i8] c"Socket create failed\00"
@.str.1 = [18 x i8] c"Connection failed\00"
define i32 @connect_to_batch_server(%struct.hostent* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.hostent*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
store %struct.hostent* %0, %struct.hostent** %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 eq i32 -1, %11
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %41
15: ; preds = %2
%16 = bitcast %struct.sockaddr_in* %8 to i8*
%17 = call i32 @bzero(i8* %16, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %18, i32* %19
%20 = load %struct.hostent*, %struct.hostent** %4
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i32 0, i32 1
%22 = load i64, i64* %21
%23 = inttoptr i64 %22 to i8*
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
%26 = bitcast i32* %25 to i8*
%27 = load %struct.hostent*, %struct.hostent** %4
%28 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @bcopy(i8* %23, i8* %26, i32 %29)
%31 = load i32, i32* %5
%32 = call i32 @htons(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %32, i32* %33
%34 = load i32, i32* %6
%35 = call i32 @connect(i32 %34, %struct.sockaddr_in* %8, i32 12)
%36 = icmp eq i32 -1, %35
br i1 %36, label %37, label %39
37: ; preds = %15
%38 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %41
39: ; preds = %15
%40 = load i32, i32* %6
store i32 %40, i32* %3
br label %41
41: ; preds = %39, %37, %13
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
%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
@.str = [21 x i8] c"Socket create failed\00"
@.str.1 = [18 x i8] c"Connection failed\00"
define i32 @connect_to_batch_server(%struct.hostent* nocapture %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 eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %28
11: ; preds = %2
%12 = call i32 @bzero(i8* %4, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %0, i64 0, i32 1
%16 = load i64, i64* %15
%17 = inttoptr i64 %16 to i8*
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%19 = bitcast i32* %18 to i8*
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %0, i64 0, i32 0
%21 = load i32, i32* %20
%22 = tail call i32 @htons(i32 %1)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %22, i32* %23
%24 = call i32 @connect(i32 %7, %struct.sockaddr_in* %3, i32 12)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %11
%27 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %11, %26, %9
%29 = phi i32 [ -1, %9 ], [ -1, %26 ], [ %7, %11 ]
ret i32 %29
}