#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 *, int);
int errno;
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *,
                      int *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
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, (struct sockaddr *)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 {
    fprintf(stderr, "select error: sockfd not set");
    return (-1);
  }
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    0x2df9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dfb(%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    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    0x2dce(%rip),%rcx
cmp    (%rcx),%eax
je     121e <connect_nonb+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <connect_nonb+0x21b>
jmp    1223 <connect_nonb+0x93>
cmpl   $0x0,-0x20(%rbp)
jne    1232 <connect_nonb+0xa2>
jmp    1366 <connect_nonb+0x1d6>
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    0x2ce7(%rip),%rax
mov    (%rax),%ecx
mov    0x2cf6(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <connect_nonb+0x21b>
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    0x2c9a(%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    13ab <connect_nonb+0x21b>
jmp    1361 <connect_nonb+0x1d1>
mov    0x2c81(%rip),%rax
mov    (%rax),%edi
lea    0xcb0(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <connect_nonb+0x21b>
jmp    1366 <connect_nonb+0x1d6>
mov    -0x8(%rbp),%edi
mov    0x2c50(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
call   1030 <fcntl@plt>
cmpl   $0x0,-0x24(%rbp)
je     13a4 <connect_nonb+0x214>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%ecx
mov    0x2c22(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13ab <connect_nonb+0x21b>
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    0x2df8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,%r14d
mov    0x2dfd(%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    12be <connect_nonb+0x12e>
mov    0x2db7(%rip),%rax
mov    (%rax),%ecx
mov    0x2dce(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%ecx
jne    12fe <connect_nonb+0x16e>
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     12e0 <connect_nonb+0x150>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
jne    128e <connect_nonb+0xfe>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1311 <connect_nonb+0x181>
movl   $0x4,0x14(%rsp)
mov    0x2d4b(%rip),%rax
mov    (%rax),%esi
mov    0x2d0a(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
lea    0x14(%rsp),%r8
mov    %ebx,%edi
call   1060 <getsockopt@plt>
test   %rax,%rax
js     12f9 <connect_nonb+0x169>
mov    0x0(%r13),%esi
mov    %ebx,%edi
mov    %r14d,%edx
call   1030 <fcntl@plt>
cmpl   $0x0,0x8(%rsp)
je     130d <connect_nonb+0x17d>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x8(%rsp),%eax
jmp    12f0 <connect_nonb+0x160>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2cb2(%rip),%rax
mov    (%rax),%eax
mov    0x2cc1(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    12fe <connect_nonb+0x16e>
mov    0x2cb0(%rip),%rax
mov    (%rax),%edi
lea    0xcdf(%rip),%rsi
call   1070 <fprintf@plt>
jmp    12f9 <connect_nonb+0x169>

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
@stderr = global i32 0
@.str = [29 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 %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 %95
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 %95
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 %95
78: ; preds = %71
br label %82
79: ; preds = %67
%80 = load i32, i32* @stderr
%81 = call i32 @fprintf(i32 %80, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %95
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 %95
94: ; preds = %83
store i32 0, i32* %5
br label %95
95: ; preds = %94, %90, %79, %77, %59, %35
%96 = load i32, i32* %5
ret i32 %96
}

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
@stderr = global i32 0
@.str = [29 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 %65
27: ; preds = %4
%28 = icmp eq i32 %21, 0
br i1 %28, label %57, 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 %40, label %43
40: ; preds = %29
%41 = call i32 @close(i32 %0)
%42 = load i32, i32* @ETIMEDOUT
store i32 %42, i32* @errno
br label %65
43: ; preds = %29
%44 = call i64 @FD_ISSET(i32 %0, i32* %7)
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = call i64 @FD_ISSET(i32 %0, i32* %8)
%48 = icmp eq i64 %47, 0
br i1 %48, label %54, label %49
49: ; preds = %46, %43
store i32 4, i32* %6
%50 = load i32, i32* @SOL_SOCKET
%51 = load i32, i32* @SO_ERROR
%52 = call i64 @getsockopt(i32 %0, i32 %50, i32 %51, i32* %5, i32* %6)
%53 = icmp slt i64 %52, 0
br i1 %53, label %65, label %57
54: ; preds = %46
%55 = load i32, i32* @stderr
%56 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %65
57: ; preds = %49, %27
%58 = load i32, i32* @F_SETFL
%59 = call i32 @fcntl(i32 %0, i32 %58, i32 %16)
%60 = load i32, i32* %5
%61 = icmp eq i32 %60, 0
br i1 %61, label %65, label %62
62: ; preds = %57
%63 = call i32 @close(i32 %0)
%64 = load i32, i32* %5
store i32 %64, i32* @errno
br label %65
65: ; preds = %57, %49, %23, %62, %54, %40
%66 = phi i32 [ -1, %62 ], [ -1, %40 ], [ -1, %54 ], [ -1, %23 ], [ -1, %49 ], [ 0, %57 ]
ret i32 %66
}