#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 socklen_t;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ error;

/* Variables and functions */
int EINPROGRESS;
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*/ assert(int);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr const *, int);
int errno;
int fcntl(int, int /*<<< orphan*/, int);
scalar_t__ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *,
                      int *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int connect_nonb(int sockfd, const struct sockaddr *saptr, socklen_t salen,
                 int nsec) {
  int flags, n, error;
  socklen_t len;
  fd_set rset, wset;
  struct timeval tval;
  flags = fcntl(sockfd, F_GETFL, 0);
  fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
  error = 0;
  if ((n = connect(sockfd, saptr, salen)) < 0)
    if (errno != EINPROGRESS)
      return (-1);
  if (n == 0)
    goto done;
  FD_ZERO(&rset);
  FD_SET(sockfd, &rset);
  wset = rset;
  tval.tv_sec = nsec;
  tval.tv_usec = 0;
  if ((n = select(sockfd + 1, &rset, &wset, NULL, nsec ? &tval : NULL)) == 0) {
    close(sockfd);
    errno = ETIMEDOUT;
    return (-1);
  }
  if (FD_ISSET(sockfd, &rset) || FD_ISSET(sockfd, &wset)) {
    len = sizeof(error);
    if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
      return (-1);
  } else
    assert(0 && "select error: sockfd not set");
done:
  fcntl(sockfd, F_SETFL, flags);
  if (error) {
    close(sockfd);
    errno = error;
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<connect_nonb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
mov    0x2e0f(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   10a0 <connect@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1223 <connect_nonb+0x93>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    0x2dce(%rip),%rcx
cmp    (%rcx),%eax
je     121e <connect_nonb+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1391 <connect_nonb+0x201>
jmp    1223 <connect_nonb+0x93>
cmpl   $0x0,-0x20(%rbp)
jne    1232 <connect_nonb+0xa2>
jmp    134c <connect_nonb+0x1bc>
lea    -0x2c(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     127b <connect_nonb+0xeb>
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    1286 <connect_nonb+0xf6>
xor    %eax,%eax
mov    %rax,-0x50(%rbp)
jmp    1286 <connect_nonb+0xf6>
mov    -0x44(%rbp),%edi
mov    -0x50(%rbp),%r8
lea    -0x2c(%rbp),%rsi
lea    -0x30(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
call   1090 <select@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jne    12d0 <connect_nonb+0x140>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    0x2cef(%rip),%rax
mov    (%rax),%ecx
mov    0x2cfe(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1391 <connect_nonb+0x201>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
jne    12fc <connect_nonb+0x16c>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1340 <connect_nonb+0x1b0>
movl   $0x4,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2cdb(%rip),%rax
mov    (%rax),%esi
mov    0x2ca2(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
lea    -0x28(%rbp),%r8
call   1060 <getsockopt@plt>
cmp    $0x0,%rax
jge    133b <connect_nonb+0x1ab>
movl   $0xffffffff,-0x4(%rbp)
jmp    1391 <connect_nonb+0x201>
jmp    1347 <connect_nonb+0x1b7>
xor    %edi,%edi
call   1070 <assert@plt>
jmp    134c <connect_nonb+0x1bc>
mov    -0x8(%rbp),%edi
mov    0x2c72(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
call   1030 <fcntl@plt>
cmpl   $0x0,-0x24(%rbp)
je     138a <connect_nonb+0x1fa>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2c44(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1391 <connect_nonb+0x201>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_nonb>:
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
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,%r14d
mov    0x2e05(%rip),%r13
mov    0x0(%r13),%esi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
or     %r14d,%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
movl   $0x0,0x8(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r12d,%edx
call   10a0 <connect@plt>
test   %eax,%eax
js     11fa <connect_nonb+0x6a>
jne    1217 <connect_nonb+0x87>
jmp    12cc <connect_nonb+0x13c>
mov    0x2dbf(%rip),%rax
mov    (%rax),%ecx
mov    0x2dce(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%ecx
jne    1302 <connect_nonb+0x172>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10b0 <FD_SET@plt>
mov    0x10(%rsp),%eax
mov    %eax,0xc(%rsp)
mov    %r15d,0x18(%rsp)
movq   $0x0,0x20(%rsp)
lea    0x1(%rbx),%edi
xor    %eax,%eax
test   %r15d,%r15d
lea    0x18(%rsp),%r8
cmove  %rax,%r8
lea    0xc(%rsp),%rdx
mov    %rbp,%rsi
xor    %ecx,%ecx
call   1090 <select@plt>
test   %eax,%eax
je     12bc <connect_nonb+0x12c>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
jne    128a <connect_nonb+0xfa>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     12c5 <connect_nonb+0x135>
movl   $0x4,0x14(%rsp)
mov    0x2d4f(%rip),%rax
mov    (%rax),%esi
mov    0x2d16(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
lea    0x14(%rsp),%r8
mov    %ebx,%edi
call   1060 <getsockopt@plt>
test   %rax,%rax
jns    12cc <connect_nonb+0x13c>
jmp    12f9 <connect_nonb+0x169>
mov    0x2ce5(%rip),%rbp
jmp    12e6 <connect_nonb+0x156>
xor    %edi,%edi
call   1070 <assert@plt>
mov    0x0(%r13),%esi
mov    %ebx,%edi
mov    %r14d,%edx
call   1030 <fcntl@plt>
cmpl   $0x0,0x8(%rsp)
je     1300 <connect_nonb+0x170>
lea    0x8(%rsp),%rbp
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x0(%rbp),%eax
mov    0x2cc9(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
jmp    1302 <connect_nonb+0x172>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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