#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*/ close(int);
int connect(int, struct sockaddr const *, int);
int errno;
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, int);
scalar_t__ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *,
                      int *);
int /*<<< orphan*/ printf(char *);
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 {
    printf("select error:sockfd not set\n");
    exit(-1);
  }
done:
  fcntl(sockfd, F_SETFL, flags);
  if (error) {
    close(sockfd);
    errno = error;
    return -1;
  }
}


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    0x2df1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
mov    0x2dff(%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   10b0 <connect@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1233 <connect_nonb+0x93>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    0x2dbe(%rip),%rcx
cmp    (%rcx),%eax
je     122e <connect_nonb+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a4 <connect_nonb+0x204>
jmp    1233 <connect_nonb+0x93>
cmpl   $0x0,-0x20(%rbp)
jne    1242 <connect_nonb+0xa2>
jmp    136b <connect_nonb+0x1cb>
lea    -0x2c(%rbp),%rdi
call   1080 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   10c0 <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     128b <connect_nonb+0xeb>
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
jmp    1296 <connect_nonb+0xf6>
xor    %eax,%eax
mov    %rax,-0x50(%rbp)
jmp    1296 <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    12e0 <connect_nonb+0x140>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    0x2cdf(%rip),%rax
mov    (%rax),%ecx
mov    0x2cee(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13a4 <connect_nonb+0x204>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
jne    130c <connect_nonb+0x16c>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1350 <connect_nonb+0x1b0>
movl   $0x4,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ccb(%rip),%rax
mov    (%rax),%esi
mov    0x2c92(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
lea    -0x28(%rbp),%r8
call   1070 <getsockopt@plt>
cmp    $0x0,%rax
jge    134b <connect_nonb+0x1ab>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a4 <connect_nonb+0x204>
jmp    1366 <connect_nonb+0x1c6>
lea    0xca9(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
jmp    136b <connect_nonb+0x1cb>
mov    -0x8(%rbp),%edi
mov    0x2c53(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
call   1030 <fcntl@plt>
cmpl   $0x0,-0x24(%rbp)
je     13a4 <connect_nonb+0x204>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2c25(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-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    0x2df0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,%r14d
mov    0x2df5(%rip),%r13
mov    0x0(%r13),%esi
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
or     %r14d,%edx
mov    %ebx,%edi
call   1040 <fcntl@plt>
movl   $0x0,0x8(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r12d,%edx
call   10b0 <connect@plt>
test   %eax,%eax
js     120a <connect_nonb+0x6a>
jne    1222 <connect_nonb+0x82>
jmp    12c5 <connect_nonb+0x125>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    0x2dbe(%rip),%rcx
cmp    (%rcx),%eax
jne    12fb <connect_nonb+0x15b>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1080 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   10c0 <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     12e1 <connect_nonb+0x141>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
jne    1295 <connect_nonb+0xf5>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1050 <FD_ISSET@plt>
test   %rax,%rax
je     130f <connect_nonb+0x16f>
movl   $0x4,0x14(%rsp)
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
mov    0x2d0b(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
lea    0x14(%rsp),%r8
mov    %ebx,%edi
call   1070 <getsockopt@plt>
test   %rax,%rax
js     12fb <connect_nonb+0x15b>
mov    0x0(%r13),%esi
mov    %ebx,%edi
mov    %r14d,%edx
call   1040 <fcntl@plt>
cmpl   $0x0,0x8(%rsp)
je     12fb <connect_nonb+0x15b>
lea    0x8(%rsp),%rbp
jmp    12e8 <connect_nonb+0x148>
mov    0x2cc0(%rip),%rbp
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x0(%rbp),%eax
mov    0x2cc7(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcea(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

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
@.str = [29 x i8] c"select error:sockfd not set\0A\00"
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 %83
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 %82
79: ; preds = %67
%80 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%81 = call i32 @exit(i32 -1)
unreachable
82: ; preds = %78
br label %83
83: ; preds = %82, %40
%84 = load i32, i32* %6
%85 = load i32, i32* @F_SETFL
%86 = load i32, i32* %10
%87 = call i32 @fcntl(i32 %84, i32 %85, i32 %86)
%88 = load i32, i32* %12
%89 = icmp ne i32 %88, 0
br i1 %89, label %90, label %94
90: ; preds = %83
%91 = load i32, i32* %6
%92 = call i32 @close(i32 %91)
%93 = load i32, i32* %12
store i32 %93, i32* @errno
store i32 -1, i32* %5
br label %94
94: ; preds = %35, %59, %77, %90, %83
%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
@str = [28 x i8] c"select error:sockfd not set\00"
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 %63
27: ; preds = %4
%28 = icmp eq i32 %21, 0
br i1 %28, label %54, 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 %59, 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 %63, label %54
51: ; preds = %43
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%53 = call i32 @exit(i32 -1)
unreachable
54: ; preds = %46, %27
%55 = load i32, i32* @F_SETFL
%56 = call i32 @fcntl(i32 %0, i32 %55, i32 %16)
%57 = load i32, i32* %5
%58 = icmp eq i32 %57, 0
br i1 %58, label %63, label %59
59: ; preds = %54, %29
%60 = phi i32* [ @ETIMEDOUT, %29 ], [ %5, %54 ]
%61 = call i32 @close(i32 %0)
%62 = load i32, i32* %60
store i32 %62, i32* @errno
br label %63
63: ; preds = %59, %54, %46, %23
ret i32 -1
}