#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_sec;
  scalar_t__ tv_usec;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_zero;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ dest_addr;

/* 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;
long O_NONBLOCK;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int connect(int, struct sockaddr *, int);
scalar_t__ errno;
long fcntl(int, int /*<<< orphan*/, long);
scalar_t__ getHost(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              void *, int *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(int /*<<< orphan*/, char, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int connectTimeout(int fd, char *host, int port, int timeout) {
  struct sockaddr_in dest_addr;
  fd_set myset;
  struct timeval tv;
  socklen_t lon;
  int valopt;
  long arg = fcntl(fd, F_GETFL, NULL);
  arg |= O_NONBLOCK;
  fcntl(fd, F_SETFL, arg);
  dest_addr.sin_family = AF_INET;
  dest_addr.sin_port = htons(port);
  if (getHost(host, &dest_addr.sin_addr))
    return 0;
  memset(dest_addr.sin_zero, '\0', sizeof dest_addr.sin_zero);
  int res = connect(fd, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
  if (res < 0) {
    if (errno == EINPROGRESS) {
      tv.tv_sec = timeout;
      tv.tv_usec = 0;
      FD_ZERO(&myset);
      FD_SET(fd, &myset);
      if (select(fd + 1, NULL, &myset, NULL, &tv) > 0) {
        lon = sizeof(int);
        getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)(&valopt), &lon);
        if (valopt)
          return 0;
      } else
        return 0;
    } else
      return 0;
  }
  arg = fcntl(fd, F_GETFL, NULL);
  arg &= (~O_NONBLOCK);
  fcntl(fd, F_SETFL, arg);
  return 1;
}


x86 assembly (clang, -O0):
<connectTimeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <fcntl@plt>
mov    %rax,-0x50(%rbp)
mov    0x2e13(%rip),%rax
mov    (%rax),%rax
or     -0x50(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
mov    -0x50(%rbp),%rdx
call   1030 <fcntl@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
add    $0x4,%rsi
call   1070 <getHost@plt>
cmp    $0x0,%rax
je     1223 <connectTimeout+0x93>
movl   $0x0,-0x4(%rbp)
jmp    1362 <connectTimeout+0x1d2>
mov    -0x28(%rbp),%edi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   10a0 <connect@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    1317 <connectTimeout+0x187>
mov    0x2d61(%rip),%rax
mov    (%rax),%rax
mov    0x2d6f(%rip),%rcx
cmp    (%rcx),%rax
jne    1306 <connectTimeout+0x176>
mov    -0x18(%rbp),%eax
mov    %eax,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
lea    -0x2c(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x2c(%rbp),%rdx
lea    -0x40(%rbp),%r8
mov    %rcx,%rsi
call   1090 <select@plt>
cmp    $0x0,%rax
jle    12f5 <connectTimeout+0x165>
movl   $0x4,-0x44(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d26(%rip),%rax
mov    (%rax),%esi
mov    0x2ce5(%rip),%rax
mov    (%rax),%edx
lea    -0x48(%rbp),%rcx
lea    -0x44(%rbp),%r8
call   1060 <getsockopt@plt>
cmpl   $0x0,-0x48(%rbp)
je     12f0 <connectTimeout+0x160>
movl   $0x0,-0x4(%rbp)
jmp    1362 <connectTimeout+0x1d2>
jmp    1301 <connectTimeout+0x171>
movl   $0x0,-0x4(%rbp)
jmp    1362 <connectTimeout+0x1d2>
jmp    1312 <connectTimeout+0x182>
movl   $0x0,-0x4(%rbp)
jmp    1362 <connectTimeout+0x1d2>
jmp    1317 <connectTimeout+0x187>
mov    -0x8(%rbp),%edi
mov    0x2c87(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <fcntl@plt>
mov    %rax,-0x50(%rbp)
mov    0x2ca1(%rip),%rax
mov    (%rax),%rax
xor    $0xffffffffffffffff,%rax
and    -0x50(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2c70(%rip),%rax
mov    (%rax),%esi
mov    -0x50(%rbp),%rdx
call   1030 <fcntl@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connectTimeout>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %ecx,%r15d
mov    %edx,%ebx
mov    %rsi,%rbp
mov    %edi,%r12d
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
xor    %r14d,%r14d
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    0x2e15(%rip),%r13
or     0x0(%r13),%rax
mov    0x2df2(%rip),%rcx
mov    (%rcx),%esi
mov    %r12d,%edi
mov    %rax,%rdx
call   1030 <fcntl@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0x18(%rsp)
lea    0x14(%rsp),%rsi
mov    %rbp,%rdi
call   1070 <getHost@plt>
test   %rax,%rax
jne    12fc <connectTimeout+0x16c>
mov    0x10(%rsp),%edi
xor    %r14d,%r14d
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
lea    0x10(%rsp),%rsi
mov    %r12d,%edi
mov    $0x10,%edx
call   10a0 <connect@plt>
test   %eax,%eax
js     126b <connectTimeout+0xdb>
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    0x0(%r13),%rdx
not    %rdx
and    %rax,%rdx
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   1030 <fcntl@plt>
mov    $0x1,%r14d
jmp    12fc <connectTimeout+0x16c>
mov    0x2d46(%rip),%rax
mov    (%rax),%rax
mov    0x2d54(%rip),%rcx
cmp    (%rcx),%rax
jne    12fc <connectTimeout+0x16c>
mov    %r15d,0x28(%rsp)
movq   $0x0,0x30(%rsp)
lea    0x24(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <FD_ZERO@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
call   10b0 <FD_SET@plt>
lea    0x1(%r12),%edi
xor    %r14d,%r14d
lea    0x28(%rsp),%r8
xor    %esi,%esi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1090 <select@plt>
test   %rax,%rax
jle    12fc <connectTimeout+0x16c>
movl   $0x4,0xc(%rsp)
mov    0x2d14(%rip),%rax
mov    (%rax),%esi
mov    0x2cd3(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %r12d,%edi
call   1060 <getsockopt@plt>
cmpl   $0x0,0x8(%rsp)
je     1232 <connectTimeout+0xa2>
mov    %r14d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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