#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*/ fd_set;

/* 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 connect(int, struct sockaddr *, int);
int errno;
scalar_t__ fcntl(int, int /*<<< orphan*/, int);
int getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *,
               int /*<<< orphan*/ *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int connect_nb(int sock, struct sockaddr *saddr, int saddr_size, int sec) {
  int new_stat = 0;
  int old_stat = 0;
  int res = 0, n = 0;
  fd_set rset, wset;
  struct timeval tv;
  int error = 0;
  int esize = 0;
  if ((new_stat = fcntl(sock, F_GETFL, NULL)) < 0) {
    return -1;
  }
  old_stat = new_stat;
  new_stat |= O_NONBLOCK;
  if (fcntl(sock, F_SETFL, new_stat) < 0) {
    return -1;
  }
  if ((res = connect(sock, saddr, saddr_size)) < 0) {
    if (errno != EINPROGRESS) {
      return -1;
    }
  }
  if (res == 0) {
    fcntl(sock, F_SETFL, old_stat);
    return 1;
  }
  FD_ZERO(&rset);
  FD_SET(sock, &rset);
  wset = rset;
  tv.tv_sec = sec;
  tv.tv_usec = 0;
  if ((n = select(sock + 1, &rset, &wset, NULL, &tv)) == 0) {
    errno = ETIMEDOUT;
    return 0;
  }
  if (FD_ISSET(sock, &rset) || FD_ISSET(sock, &wset)) {
    esize = sizeof(int);
    if ((n = getsockopt(sock, SOL_SOCKET, SO_ERROR, &error,
                        (socklen_t *)&esize)) < 0) {
      return -1;
    }
  } else {
    return -1;
  }
  fcntl(sock, F_SETFL, old_stat);
  if (error) {
    errno = error;
    return -1;
  }
  return 1;
}


x86 assembly (clang, -O0):
<connect_nb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11da <connect_nb+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b0 <connect_nb+0x240>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    1219 <connect_nb+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b0 <connect_nb+0x240>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1080 <connect@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    125d <connect_nb+0xed>
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
mov    0x2d94(%rip),%rcx
cmp    (%rcx),%eax
je     1258 <connect_nb+0xe8>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b0 <connect_nb+0x240>
jmp    125d <connect_nb+0xed>
cmpl   $0x0,-0x24(%rbp)
jne    1287 <connect_nb+0x117>
mov    -0x8(%rbp),%edi
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
call   1030 <fcntl@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b0 <connect_nb+0x240>
lea    -0x2c(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1090 <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),%edi
add    $0x1,%edi
lea    -0x2c(%rbp),%rsi
lea    -0x30(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x40(%rbp),%r8
call   1070 <select@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jne    12f5 <connect_nb+0x185>
mov    0x2cca(%rip),%rax
mov    (%rax),%ecx
mov    0x2cd9(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    13b0 <connect_nb+0x240>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
jne    1321 <connect_nb+0x1b1>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1367 <connect_nb+0x1f7>
movl   $0x4,-0x48(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2cb6(%rip),%rax
mov    (%rax),%esi
mov    0x2c7d(%rip),%rax
mov    (%rax),%edx
lea    -0x44(%rbp),%rcx
lea    -0x48(%rbp),%r8
call   1050 <getsockopt@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1362 <connect_nb+0x1f2>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b0 <connect_nb+0x240>
jmp    1373 <connect_nb+0x203>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b0 <connect_nb+0x240>
mov    -0x8(%rbp),%edi
mov    0x2c4b(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
call   1030 <fcntl@plt>
cmpl   $0x0,-0x44(%rbp)
je     13a9 <connect_nb+0x239>
mov    -0x44(%rbp),%ecx
mov    0x2c25(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    13b0 <connect_nb+0x240>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%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   $0x0,0x8(%rsp)
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %rax,%r14
test   %r14d,%r14d
js     12d5 <connect_nb+0x165>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
or     %r14d,%edx
mov    0x2e08(%rip),%r13
mov    0x0(%r13),%esi
mov    %ebx,%edi
call   1030 <fcntl@plt>
test   %rax,%rax
js     12d5 <connect_nb+0x165>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r12d,%edx
call   1080 <connect@plt>
test   %eax,%eax
js     11ff <connect_nb+0x8f>
jne    1217 <connect_nb+0xa7>
mov    0x0(%r13),%esi
mov    %ebx,%edi
mov    %r14d,%edx
call   1030 <fcntl@plt>
mov    $0x1,%ebp
jmp    12da <connect_nb+0x16a>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    0x2dc9(%rip),%rcx
cmp    (%rcx),%eax
jne    12d5 <connect_nb+0x165>
lea    0x10(%rsp),%r12
mov    %r12,%rdi
call   1060 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r12,%rsi
call   1090 <FD_SET@plt>
mov    0x10(%rsp),%eax
mov    %eax,0xc(%rsp)
mov    %r15d,0x18(%rsp)
movq   $0x0,0x20(%rsp)
lea    0x1(%rbx),%edi
xor    %ebp,%ebp
lea    0xc(%rsp),%rdx
lea    0x18(%rsp),%r8
mov    %r12,%rsi
xor    %ecx,%ecx
call   1070 <select@plt>
test   %eax,%eax
je     12eb <connect_nb+0x17b>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
jne    1287 <connect_nb+0x117>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     12d5 <connect_nb+0x165>
movl   $0x4,0x14(%rsp)
mov    0x2d52(%rip),%rax
mov    (%rax),%esi
mov    0x2d19(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
lea    0x14(%rsp),%r8
mov    %ebx,%edi
call   1050 <getsockopt@plt>
test   %eax,%eax
js     12d5 <connect_nb+0x165>
mov    0x0(%r13),%esi
mov    %ebx,%edi
mov    %r14d,%edx
call   1030 <fcntl@plt>
mov    0x8(%rsp),%eax
test   %eax,%eax
je     12ff <connect_nb+0x18f>
mov    0x2ced(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cb6(%rip),%rax
mov    (%rax),%eax
mov    0x2cc5(%rip),%rcx
mov    %eax,(%rcx)
jmp    12da <connect_nb+0x16a>
mov    $0x1,%ebp
jmp    12da <connect_nb+0x16a>

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

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