#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*/ u_short;
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINPROGRESS;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, int);
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int client_comm_connect(int sock, const char *dest_host, u_short dest_port,
                        struct timeval *tvp) {
  const struct hostent *hp = NULL;
  struct sockaddr_in to_addr;
  int flags, res;
  fd_set fdr, fdw;
  struct timeval timeout;
  if ((flags = fcntl(sock, F_GETFL, 0)) < 0)
    return -1;
  if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0)
    return -1;
  if (hp == NULL) {
    to_addr.sin_family = AF_INET;
    if ((hp = gethostbyname(dest_host)) == 0)
      return (-1);
    memcpy(&to_addr.sin_addr, hp->h_addr, hp->h_length);
    to_addr.sin_port = htons(dest_port);
  }
  if (connect(sock, (struct sockaddr *)&to_addr, sizeof(struct sockaddr_in)) !=
      0) {
    if (errno != EINPROGRESS)
      return -1;
    else
      goto select;
  } else
    return -1;
select:
  FD_ZERO(&fdr);
  FD_ZERO(&fdw);
  FD_SET(sock, &fdr);
  FD_SET(sock, &fdw);
  timeout.tv_sec = 10;
  timeout.tv_usec = 0;
  res = select(sock + 1, &fdr, &fdw, NULL, &timeout);
  if (res < 0) {
    return -1;
  } else if (res == 0) {
    return -1;
  } else {
    return 1;
  }
}


x86 assembly (clang, -O0):
<client_comm_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jge    11c9 <client_comm_connect+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    1336 <client_comm_connect+0x1b6>
mov    -0x8(%rbp),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%esi
mov    -0x3c(%rbp),%edx
mov    0x2e01(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    11fb <client_comm_connect+0x7b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1336 <client_comm_connect+0x1b6>
cmpq   $0x0,-0x28(%rbp)
jne    125a <client_comm_connect+0xda>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1235 <client_comm_connect+0xb5>
movl   $0xffffffff,-0x4(%rbp)
jmp    1336 <client_comm_connect+0x1b6>
lea    -0x38(%rbp),%rdi
add    $0x4,%rdi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1060 <memcpy@plt>
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
je     12a0 <client_comm_connect+0x120>
mov    0x2d44(%rip),%rax
mov    (%rax),%rax
mov    0x2d52(%rip),%rcx
cmp    (%rcx),%rax
je     129b <client_comm_connect+0x11b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1336 <client_comm_connect+0x1b6>
jmp    12ac <client_comm_connect+0x12c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1336 <client_comm_connect+0x1b6>
lea    -0x44(%rbp),%rdi
call   1070 <FD_ZERO@plt>
lea    -0x48(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x44(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x8(%rbp),%edi
lea    -0x48(%rbp),%rsi
call   10a0 <FD_SET@plt>
movl   $0xa,-0x58(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x44(%rbp),%rsi
lea    -0x48(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x58(%rbp),%r8
call   1080 <select@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    1319 <client_comm_connect+0x199>
movl   $0xffffffff,-0x4(%rbp)
jmp    1336 <client_comm_connect+0x1b6>
cmpl   $0x0,-0x40(%rbp)
jne    132f <client_comm_connect+0x1af>
movl   $0xffffffff,-0x4(%rbp)
jmp    1336 <client_comm_connect+0x1b6>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_comm_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     1299 <client_comm_connect+0x119>
mov    0x2e11(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e20(%rip),%rcx
or     (%rcx),%eax
mov    %ebp,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     1299 <client_comm_connect+0x119>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %rbx,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     1299 <client_comm_connect+0x119>
lea    0x4(%rsp),%rdi
mov    (%rax),%edx
mov    0x4(%rax),%esi
call   1060 <memcpy@plt>
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
je     1299 <client_comm_connect+0x119>
mov    0x2d9a(%rip),%rax
mov    (%rax),%rax
mov    0x2da8(%rip),%rcx
cmp    (%rcx),%rax
jne    1299 <client_comm_connect+0x119>
lea    0x14(%rsp),%r15
mov    %r15,%rdi
call   1070 <FD_ZERO@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r15,%rsi
call   10a0 <FD_SET@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   10a0 <FD_SET@plt>
movl   $0xa,0x18(%rsp)
movq   $0x0,0x20(%rsp)
add    $0x1,%ebp
lea    0x18(%rsp),%r8
mov    %ebp,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1080 <select@plt>
test   %eax,%eax
js     1299 <client_comm_connect+0x119>
cmp    $0x1,%eax
sbb    %r14d,%r14d
or     $0x1,%r14d
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@AF_INET = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
define i32 @client_comm_connect(i32 %0, i8* %1, i32 %2, %struct.timeval* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.timeval*
%10 = alloca %struct.hostent*
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.timeval
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store %struct.timeval* %3, %struct.timeval** %9
store %struct.hostent* null, %struct.hostent** %10
%17 = load i32, i32* %6
%18 = load i32, i32* @F_GETFL
%19 = call i32 @fcntl(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %12
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %4
store i32 -1, i32* %5
br label %85
22: ; preds = %4
%23 = load i32, i32* %6
%24 = load i32, i32* @F_SETFL
%25 = load i32, i32* %12
%26 = load i32, i32* @O_NONBLOCK
%27 = or i32 %25, %26
%28 = call i32 @fcntl(i32 %23, i32 %24, i32 %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %22
store i32 -1, i32* %5
br label %85
31: ; preds = %22
%32 = load %struct.hostent*, %struct.hostent** %10
%33 = icmp eq %struct.hostent* %32, null
br i1 %33, label %34, label %53
34: ; preds = %31
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %35, i32* %36
%37 = load i8*, i8** %7
%38 = call %struct.hostent* @gethostbyname(i8* %37)
store %struct.hostent* %38, %struct.hostent** %10
%39 = icmp eq %struct.hostent* %38, null
br i1 %39, label %40, label %41
40: ; preds = %34
store i32 -1, i32* %5
br label %85
41: ; preds = %34
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%43 = load %struct.hostent*, %struct.hostent** %10
%44 = getelementptr inbounds %struct.hostent, %struct.hostent* %43, i32 0, i32 1
%45 = load i32, i32* %44
%46 = load %struct.hostent*, %struct.hostent** %10
%47 = getelementptr inbounds %struct.hostent, %struct.hostent* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i32 @memcpy(i32* %42, i32 %45, i32 %48)
%50 = load i32, i32* %8
%51 = call i32 @htons(i32 %50)
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %51, i32* %52
br label %53
53: ; preds = %41, %31
%54 = load i32, i32* %6
%55 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%56 = call i64 @connect(i32 %54, %struct.sockaddr* %55, i32 12)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %64
58: ; preds = %53
%59 = load i64, i64* @errno
%60 = load i64, i64* @EINPROGRESS
%61 = icmp ne i64 %59, %60
br i1 %61, label %62, label %63
62: ; preds = %58
store i32 -1, i32* %5
br label %85
63: ; preds = %58
br label %65
64: ; preds = %53
store i32 -1, i32* %5
br label %85
65: ; preds = %63
%66 = call i32 @FD_ZERO(i32* %14)
%67 = call i32 @FD_ZERO(i32* %15)
%68 = load i32, i32* %6
%69 = call i32 @FD_SET(i32 %68, i32* %14)
%70 = load i32, i32* %6
%71 = call i32 @FD_SET(i32 %70, i32* %15)
%72 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 0
store i32 10, i32* %72
%73 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 1
store i64 0, i64* %73
%74 = load i32, i32* %6
%75 = add nsw i32 %74, 1
%76 = call i32 @select(i32 %75, i32* %14, i32* %15, i32* null, %struct.timeval* %16)
store i32 %76, i32* %13
%77 = load i32, i32* %13
%78 = icmp slt i32 %77, 0
br i1 %78, label %79, label %80
79: ; preds = %65
store i32 -1, i32* %5
br label %85
80: ; preds = %65
%81 = load i32, i32* %13
%82 = icmp eq i32 %81, 0
br i1 %82, label %83, label %84
83: ; preds = %80
store i32 -1, i32* %5
br label %85
84: ; preds = %80
store i32 1, i32* %5
br label %85
85: ; preds = %84, %83, %79, %64, %62, %40, %30, %21
%86 = load i32, i32* %5
ret i32 %86
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@AF_INET = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
define i32 @client_comm_connect(i32 %0, i8* %1, i32 %2, %struct.timeval* nocapture readnone %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = bitcast i32* %7 to i8*
%12 = bitcast %struct.timeval* %8 to i8*
%13 = load i32, i32* @F_GETFL
%14 = tail call i32 @fcntl(i32 %0, i32 %13, i32 0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %56, label %16
16: ; preds = %4
%17 = load i32, i32* @F_SETFL
%18 = load i32, i32* @O_NONBLOCK
%19 = or i32 %18, %14
%20 = tail call i32 @fcntl(i32 %0, i32 %17, i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %56, label %22
22: ; preds = %16
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %23, i32* %24
%25 = tail call %struct.hostent* @gethostbyname(i8* %1)
%26 = icmp eq %struct.hostent* %25, null
br i1 %26, label %56, label %27
27: ; preds = %22
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i64 0, i32 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i64 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @memcpy(i32* %28, i32 %30, i32 %32)
%34 = call i32 @htons(i32 %2)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %34, i32* %35
%36 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%37 = call i64 @connect(i32 %0, %struct.sockaddr* %36, i32 12)
%38 = icmp eq i64 %37, 0
br i1 %38, label %56, label %39
39: ; preds = %27
%40 = load i64, i64* @errno
%41 = load i64, i64* @EINPROGRESS
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %56
43: ; preds = %39
%44 = call i32 @FD_ZERO(i32* %6)
%45 = call i32 @FD_ZERO(i32* %7)
%46 = call i32 @FD_SET(i32 %0, i32* %6)
%47 = call i32 @FD_SET(i32 %0, i32* %7)
%48 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 0
store i32 10, i32* %48
%49 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 1
store i64 0, i64* %49
%50 = add nsw i32 %0, 1
%51 = call i32 @select(i32 %50, i32* %6, i32* %7, i32* null, %struct.timeval* %8)
%52 = icmp slt i32 %51, 0
br i1 %52, label %56, label %53
53: ; preds = %43
%54 = icmp eq i32 %51, 0
%55 = select i1 %54, i32 -1, i32 1
br label %56
56: ; preds = %53, %43, %27, %39, %22, %16, %4
%57 = phi i32 [ -1, %4 ], [ -1, %16 ], [ -1, %22 ], [ -1, %39 ], [ -1, %27 ], [ -1, %43 ], [ %55, %53 ]
ret i32 %57
}