#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 */
struct timeval {
  int tv_usec;
  scalar_t__ tv_sec;
};
struct sockaddr_in {
  unsigned short sin_port;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_addr;
};
struct hostent {
  int /*<<< orphan*/ h_addrtype;
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ server;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
scalar_t__ connect(int, struct sockaddr_in *, int);
struct hostent *gethostbyname(char *);
scalar_t__ htons(int);
int /*<<< orphan*/ memmove(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              struct timeval *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int makeConnection(char *ip, int port) {
  int s;
  struct hostent *hp;
  struct sockaddr_in server;
  struct timeval timeout;
  if (hp = gethostbyname(ip)) {
    memset((char *)&server, 0, sizeof(server));
    memmove((char *)&server.sin_addr, hp->h_addr, hp->h_length);
    server.sin_family = hp->h_addrtype;
    server.sin_port = (unsigned short)htons(port);
  } else
    return -1;
  if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    return -1;
  timeout.tv_sec = 0;
  timeout.tv_usec = 900000;
  setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&timeout,
             sizeof(struct timeval));
  if (connect(s, &server, sizeof(server)) < 0)
    return -1;
  return s;
}


x86 assembly (clang, -O0):
<makeConnection>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11db <makeConnection+0x6b>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
lea    -0x30(%rbp),%rdi
add    $0x8,%rdi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edx
call   1070 <memmove@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %ax,-0x30(%rbp)
jmp    11e7 <makeConnection+0x77>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <makeConnection+0x108>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1218 <makeConnection+0xa8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <makeConnection+0x108>
movq   $0x0,-0x38(%rbp)
movl   $0xdbba0,-0x40(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
mov    0x2d96(%rip),%rax
mov    (%rax),%edx
lea    -0x40(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1272 <makeConnection+0x102>
movl   $0xffffffff,-0x4(%rbp)
jmp    1278 <makeConnection+0x108>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<makeConnection>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
call   1060 <gethostbyname@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     1231 <makeConnection+0xc1>
mov    %rax,%rbp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
lea    0x8(%rsp),%rdi
mov    0x4(%rbp),%edx
mov    0x8(%rbp),%esi
call   1070 <memmove@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x4(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %ax,(%rsp)
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
test   %eax,%eax
js     1231 <makeConnection+0xc1>
mov    %eax,%ebp
movq   $0x0,0x18(%rsp)
movl   $0xdbba0,0x10(%rsp)
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    %ebp,%edi
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %rax,%rax
mov    $0xffffffff,%ebx
cmovns %ebp,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i16, i32, i32 }
%struct.timeval = type { i32, i64 }
%struct.hostent = type { i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
define i32 @makeConnection(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.timeval
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast %struct.timeval* %4 to i8*
%7 = tail call %struct.hostent* @gethostbyname(i8* %0)
%8 = icmp eq %struct.hostent* %7, null
br i1 %8, label %37, label %9
9: ; preds = %2
%10 = call i32 @memset(i8* %5, i32 0, i32 12)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%12 = bitcast i32* %11 to i8*
%13 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 2
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 1
%16 = load i32, i32* %15
%17 = call i32 @memmove(i8* %12, i32 %14, i32 %16)
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 0
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %19, i32* %20
%21 = call i64 @htons(i32 %1)
%22 = trunc i64 %21 to i16
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i16 %22, i16* %23
%24 = load i32, i32* @AF_INET
%25 = load i32, i32* @SOCK_STREAM
%26 = call i32 @socket(i32 %24, i32 %25, i32 0)
%27 = icmp slt i32 %26, 0
br i1 %27, label %37, label %28
28: ; preds = %9
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %29
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 900000, i32* %30
%31 = load i32, i32* @SOL_SOCKET
%32 = load i32, i32* @SO_RCVTIMEO
%33 = call i32 @setsockopt(i32 %26, i32 %31, i32 %32, %struct.timeval* %4, i32 16)
%34 = call i64 @connect(i32 %26, %struct.sockaddr_in* %3, i32 12)
%35 = icmp slt i64 %34, 0
%36 = select i1 %35, i32 -1, i32 %26
br label %37
37: ; preds = %28, %9, %2
%38 = phi i32 [ -1, %2 ], [ -1, %9 ], [ %36, %28 ]
ret i32 %38
}