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

/* Variables and functions */
int ECONNABORTED;
int EINPROGRESS;
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 const *, size_t);
int errno;
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              void *, int *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int connect_nb(int s, struct sockaddr_rc *addr, size_t slen) {
  int flags;
  int res;
  int status = 0;
  flags = fcntl(s, F_GETFL, NULL) | O_NONBLOCK;
  fcntl(s, F_SETFL, flags);
  res = connect(s, (const struct sockaddr *)addr, slen);
  if (res < 0) {
    fd_set set;
    struct timeval tv;
    socklen_t lon;
    status = errno;
    if (errno == EINPROGRESS) {
      tv.tv_sec = 15;
      tv.tv_usec = 0;
      FD_ZERO(&set);
      FD_SET(s, &set);
      res = select(s + 1, NULL, &set, NULL, &tv);
      if (res > 0) {
        lon = sizeof(int);
        getsockopt(s, SOL_SOCKET, SO_ERROR, (void *)(&status), &lon);
      } else if (res == 0) {
        status = ECONNABORTED;
      } else
        status = errno;
    } else {
      status = errno;
    }
  }
  flags = fcntl(s, F_GETFL, NULL) & (~O_NONBLOCK);
  fcntl(s, F_SETFL, flags);
  return status;
}


x86 assembly (clang, -O0):
<connect_nb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    0x2e4c(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
call   1030 <fcntl@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <connect@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    12a9 <connect_nb+0x149>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    0x2df2(%rip),%rcx
cmp    (%rcx),%eax
jne    1298 <connect_nb+0x138>
movl   $0xf,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
lea    -0x28(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x28(%rbp),%rdx
lea    -0x38(%rbp),%r8
mov    %rcx,%rsi
call   1060 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    1267 <connect_nb+0x107>
movl   $0x4,-0x3c(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
mov    0x2d65(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
lea    -0x3c(%rbp),%r8
call   1040 <getsockopt@plt>
jmp    1293 <connect_nb+0x133>
cmpl   $0x0,-0x20(%rbp)
jne    1282 <connect_nb+0x122>
mov    0x2d30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    128e <connect_nb+0x12e>
mov    0x2d37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    1293 <connect_nb+0x133>
jmp    12a4 <connect_nb+0x144>
mov    0x2d21(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    12a9 <connect_nb+0x149>
mov    -0x4(%rbp),%edi
mov    0x2cfd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    0x2d1d(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2cf1(%rip),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
call   1030 <fcntl@plt>
mov    -0x24(%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    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%ebx
movl   $0x0,0xc(%rsp)
mov    0x2e2b(%rip),%r13
mov    0x0(%r13),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    0x2e49(%rip),%rbp
or     0x0(%rbp),%eax
mov    0x2e27(%rip),%r12
mov    (%r12),%esi
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14,%rdx
call   1070 <connect@plt>
test   %eax,%eax
jns    1261 <connect_nb+0x101>
mov    0x2df6(%rip),%r15
mov    (%r15),%eax
mov    %eax,0xc(%rsp)
mov    0x2e00(%rip),%rcx
cmp    (%rcx),%eax
jne    1261 <connect_nb+0x101>
movl   $0xf,0x18(%rsp)
movq   $0x0,0x20(%rsp)
lea    0x14(%rsp),%r14
mov    %r14,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1080 <FD_SET@plt>
lea    0x1(%rbx),%edi
lea    0x18(%rsp),%r8
xor    %esi,%esi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1060 <select@plt>
test   %eax,%eax
jle    124d <connect_nb+0xed>
movl   $0x4,0x10(%rsp)
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
mov    0x2d80(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
lea    0x10(%rsp),%r8
mov    %ebx,%edi
call   1040 <getsockopt@plt>
jmp    1261 <connect_nb+0x101>
je     1254 <connect_nb+0xf4>
mov    (%r15),%eax
jmp    125d <connect_nb+0xfd>
mov    0x2d4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x0(%r13),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    0x0(%rbp),%edx
not    %edx
and    %eax,%edx
mov    (%r12),%esi
mov    %ebx,%edi
call   1030 <fcntl@plt>
mov    0xc(%rsp),%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_rc = type { i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@errno = global i32 0
@EINPROGRESS = global i32 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
@ECONNABORTED = global i32 0
define i32 @connect_nb(i32 %0, %struct.sockaddr_rc* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%9 = load i32, i32* @F_GETFL
%10 = tail call i32 @fcntl(i32 %0, i32 %9, i32 0)
%11 = load i32, i32* @O_NONBLOCK
%12 = or i32 %11, %10
%13 = load i32, i32* @F_SETFL
%14 = tail call i32 @fcntl(i32 %0, i32 %13, i32 %12)
%15 = bitcast %struct.sockaddr_rc* %1 to %struct.sockaddr*
%16 = tail call i32 @connect(i32 %0, %struct.sockaddr* %15, i64 %2)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %44
18: ; preds = %3
%19 = bitcast i32* %5 to i8*
%20 = bitcast %struct.timeval* %6 to i8*
%21 = bitcast i32* %7 to i8*
%22 = load i32, i32* @errno
store i32 %22, i32* %4
%23 = load i32, i32* @EINPROGRESS
%24 = icmp eq i32 %22, %23
br i1 %24, label %25, label %43
25: ; preds = %18
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 0
store i32 15, i32* %26
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i64 0, i32 1
store i64 0, i64* %27
%28 = call i32 @FD_ZERO(i32* %5)
%29 = call i32 @FD_SET(i32 %0, i32* %5)
%30 = add nsw i32 %0, 1
%31 = call i32 @select(i32 %30, i32* null, i32* %5, i32* null, %struct.timeval* %6)
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %25
store i32 4, i32* %7
%34 = load i32, i32* @SOL_SOCKET
%35 = load i32, i32* @SO_ERROR
%36 = call i32 @getsockopt(i32 %0, i32 %34, i32 %35, i8* %8, i32* %7)
br label %43
37: ; preds = %25
%38 = icmp eq i32 %31, 0
br i1 %38, label %39, label %41
39: ; preds = %37
%40 = load i32, i32* @ECONNABORTED
store i32 %40, i32* %4
br label %43
41: ; preds = %37
%42 = load i32, i32* @errno
store i32 %42, i32* %4
br label %43
43: ; preds = %18, %33, %41, %39
br label %44
44: ; preds = %43, %3
%45 = load i32, i32* @F_GETFL
%46 = call i32 @fcntl(i32 %0, i32 %45, i32 0)
%47 = load i32, i32* @O_NONBLOCK
%48 = xor i32 %47, -1
%49 = and i32 %46, %48
%50 = load i32, i32* @F_SETFL
%51 = call i32 @fcntl(i32 %0, i32 %50, i32 %49)
%52 = load i32, i32* %4
ret i32 %52
}