#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 {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ flags;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ err;

/* Variables and functions */
int EINPROGRESS;
int EIO;
int ETIMEDOUT;
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int /*<<< orphan*/ SO_KEEPALIVE;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int /*<<< orphan*/);
int errno;
int fcntl(int, int /*<<< orphan*/, int);
scalar_t__ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, void *,
                      unsigned int *);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, void *, int);
int connect_nb(int fd, struct sockaddr *dest, socklen_t len, int timeout) {
  int ret, flags = 1, err;
  unsigned l;
  fd_set rfds, wfds;
  struct timeval tv;
  if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&flags, sizeof(flags)) <
      0)
    return -1;
  flags = fcntl(fd, F_GETFL, 0);
  fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  ret = connect(fd, dest, len);
  if ((ret < 0) && (errno != EINPROGRESS))
    return -1;
  if (ret != 0) {
    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);
    FD_ZERO(&wfds);
    FD_SET(fd, &wfds);
    tv.tv_sec = timeout;
    tv.tv_usec = 0;
    if (select(fd + 1, &rfds, &wfds, NULL, &tv) == 0) {
      errno = ETIMEDOUT;
      return -1;
    }
    if (FD_ISSET(fd, &rfds) || FD_ISSET(fd, &wfds)) {
      l = sizeof(err);
      if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&err, &l) < 0) {
        close(fd);
        return -1;
      }
      if (err != 0) {
        close(fd);
        errno = err;
        return -1;
      }
      fcntl(fd, F_SETFL, flags);
      return 0;
    }
  }
  errno = EIO;
  return -1;
}


x86 assembly (clang, -O0):
<connect_nb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2dd1(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    11e6 <connect_nb+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b9 <connect_nb+0x229>
mov    -0x8(%rbp),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
mov    0x2dce(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   10a0 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1259 <connect_nb+0xc9>
mov    0x2d7c(%rip),%rax
mov    (%rax),%eax
mov    0x2d93(%rip),%rcx
cmp    (%rcx),%eax
je     1259 <connect_nb+0xc9>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b9 <connect_nb+0x229>
cmpl   $0x0,-0x1c(%rbp)
je     13a0 <connect_nb+0x210>
lea    -0x2c(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   10b0 <FD_SET@plt>
lea    -0x30(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x2c(%rbp),%rsi
lea    -0x30(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x40(%rbp),%r8
call   1090 <select@plt>
cmp    $0x0,%rax
jne    12de <connect_nb+0x14e>
mov    0x2cd9(%rip),%rax
mov    (%rax),%ecx
mov    0x2ce8(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13b9 <connect_nb+0x229>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
jne    130a <connect_nb+0x17a>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1050 <FD_ISSET@plt>
cmp    $0x0,%rax
je     139b <connect_nb+0x20b>
movl   $0x4,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ccd(%rip),%rax
mov    (%rax),%esi
mov    0x2c8c(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
lea    -0x28(%rbp),%r8
call   1070 <getsockopt@plt>
cmp    $0x0,%rax
jge    1351 <connect_nb+0x1c1>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b9 <connect_nb+0x229>
cmpl   $0x0,-0x24(%rbp)
je     137b <connect_nb+0x1eb>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2c4b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13b9 <connect_nb+0x229>
mov    -0x8(%rbp),%edi
mov    0x2c3b(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
call   1040 <fcntl@plt>
movl   $0x0,-0x4(%rbp)
jmp    13b9 <connect_nb+0x229>
jmp    13a0 <connect_nb+0x210>
mov    0x2c21(%rip),%rax
mov    (%rax),%ecx
mov    0x2c08(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_nb>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%r15d
mov    %edx,%r12d
mov    %rsi,%rbp
mov    %edi,%ebx
movl   $0x1,0x4(%rsp)
mov    0x2e30(%rip),%r13
mov    0x0(%r13),%esi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    $0xffffffff,%r14d
test   %rax,%rax
js     1319 <connect_nb+0x189>
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,0x4(%rsp)
mov    0x2dbf(%rip),%rcx
mov    (%rcx),%esi
mov    0x2dd6(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
call   1040 <fcntl@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r12d,%edx
call   10a0 <connect@plt>
test   %eax,%eax
js     1234 <connect_nb+0xa4>
jne    124c <connect_nb+0xbc>
mov    0x2d99(%rip),%rax
jmp    130e <connect_nb+0x17e>
mov    0x2d7d(%rip),%rax
mov    (%rax),%eax
mov    0x2d94(%rip),%rcx
cmp    (%rcx),%eax
jne    1319 <connect_nb+0x189>
lea    0x10(%rsp),%r12
mov    %r12,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r12,%rsi
call   10b0 <FD_SET@plt>
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10b0 <FD_SET@plt>
mov    %r15d,0x18(%rsp)
movq   $0x0,0x20(%rsp)
lea    0x1(%rbx),%edi
lea    0x18(%rsp),%r8
mov    %r12,%rsi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1090 <select@plt>
test   %rax,%rax
je     1307 <connect_nb+0x177>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
jne    12c8 <connect_nb+0x138>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     1228 <connect_nb+0x98>
movl   $0x4,0x14(%rsp)
mov    0x0(%r13),%esi
mov    0x2cd5(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
lea    0x14(%rsp),%r8
mov    %ebx,%edi
call   1070 <getsockopt@plt>
test   %rax,%rax
js     132b <connect_nb+0x19b>
cmpl   $0x0,0x8(%rsp)
je     1334 <connect_nb+0x1a4>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x8(%rsp),%eax
jmp    1310 <connect_nb+0x180>
mov    0x2c92(%rip),%rax
mov    (%rax),%eax
mov    0x2ca1(%rip),%rcx
mov    %eax,(%rcx)
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%edi
call   1060 <close@plt>
jmp    1319 <connect_nb+0x189>
mov    0x2c85(%rip),%rax
mov    (%rax),%esi
mov    0x4(%rsp),%edx
mov    %ebx,%edi
call   1040 <fcntl@plt>
xor    %r14d,%r14d
jmp    1319 <connect_nb+0x189>

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

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