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

/* Variables and functions */
int EALREADY;
int EINPROGRESS;
int EISCONN;
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 connect(int, struct sockaddr *, int /*<<< orphan*/);
int errsock();
int fcntl(int, int /*<<< orphan*/, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int connect_nb(int sock, struct sockaddr *addr, socklen_t len) {
  struct timeval tv = {0};
  fd_set rs, ws;
  int err, flag;
  flag = fcntl(sock, F_GETFL, 0);
  fcntl(sock, F_SETFL, flag | O_NONBLOCK);
  if (connect(sock, addr, len) == -1) {
    err = errsock();
    if (err != EISCONN && err != EINPROGRESS && err != EALREADY)
      return -1;
    FD_ZERO(&rs);
    FD_SET(sock, &rs);
    ws = rs;
    if (select(sock + 1, &rs, &ws, NULL, &tv) == 0)
      return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<connect_nb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%edx
mov    0x2e2a(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    127a <connect_nb+0x10a>
mov    $0x0,%al
call   1050 <errsock@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2de7(%rip),%rcx
cmp    (%rcx),%eax
je     1229 <connect_nb+0xb9>
mov    -0x24(%rbp),%eax
mov    0x2ddd(%rip),%rcx
cmp    (%rcx),%eax
je     1229 <connect_nb+0xb9>
mov    -0x24(%rbp),%eax
mov    0x2db3(%rip),%rcx
cmp    (%rcx),%eax
je     1229 <connect_nb+0xb9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1281 <connect_nb+0x111>
lea    -0x1c(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
lea    -0x20(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
call   1070 <select@plt>
cmp    $0x0,%rax
jne    1275 <connect_nb+0x105>
movl   $0x0,-0x4(%rbp)
jmp    1281 <connect_nb+0x111>
jmp    127a <connect_nb+0x10a>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_nb>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %edi,%ebx
movl   $0x0,(%rsp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    0x2e32(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e51(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %eax,%edx
call   1030 <fcntl@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r14d,%edx
call   1070 <connect@plt>
cmp    $0xffffffff,%eax
je     11c4 <connect_nb+0x64>
mov    $0x1,%ecx
mov    %ecx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
call   1040 <errsock@plt>
mov    0x2e06(%rip),%rcx
cmp    (%rcx),%eax
je     11f1 <connect_nb+0x91>
mov    0x2e03(%rip),%rcx
cmp    (%rcx),%eax
je     11f1 <connect_nb+0x91>
mov    0x2de0(%rip),%rdx
mov    $0xffffffff,%ecx
cmp    (%rdx),%eax
jne    11b9 <connect_nb+0x59>
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1080 <FD_SET@plt>
mov    0xc(%rsp),%eax
mov    %eax,0x8(%rsp)
add    $0x1,%ebx
lea    0x8(%rsp),%rdx
mov    %rsp,%r8
mov    %ebx,%edi
mov    %rbp,%rsi
xor    %ecx,%ecx
call   1060 <select@plt>
mov    $0x0,%ecx
test   %rax,%rax
jne    11b4 <connect_nb+0x54>
jmp    11b9 <connect_nb+0x59>

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

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.timeval = type { i32 }
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@EISCONN = global i32 0
@EINPROGRESS = global i32 0
@EALREADY = global i32 0
define i32 @connect_nb(i32 %0, %struct.sockaddr* %1, i32 %2) {
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = alloca i32
%7 = bitcast %struct.timeval* %4 to i8*
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 0, i32* %8
%9 = bitcast i32* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = load i32, i32* @F_GETFL
%12 = tail call i32 @fcntl(i32 %0, i32 %11, i32 0)
%13 = load i32, i32* @F_SETFL
%14 = load i32, i32* @O_NONBLOCK
%15 = or i32 %14, %12
%16 = tail call i32 @fcntl(i32 %0, i32 %13, i32 %15)
%17 = tail call i32 @connect(i32 %0, %struct.sockaddr* %1, i32 %2)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %36
19: ; preds = %3
%20 = tail call i32 (...) @errsock()
%21 = load i32, i32* @EISCONN
%22 = icmp eq i32 %20, %21
%23 = load i32, i32* @EINPROGRESS
%24 = icmp eq i32 %20, %23
%25 = select i1 %22, i1 true, i1 %24
%26 = load i32, i32* @EALREADY
%27 = icmp eq i32 %20, %26
%28 = select i1 %25, i1 true, i1 %27
br i1 %28, label %29, label %37
29: ; preds = %19
%30 = call i32 @FD_ZERO(i32* %5)
%31 = call i32 @FD_SET(i32 %0, i32* %5)
%32 = load i32, i32* %5
store i32 %32, i32* %6
%33 = add nsw i32 %0, 1
%34 = call i64 @select(i32 %33, i32* %5, i32* %6, i32* null, %struct.timeval* %4)
%35 = icmp eq i64 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %29, %3
br label %37
37: ; preds = %29, %19, %36
%38 = phi i32 [ 1, %36 ], [ -1, %19 ], [ 0, %29 ]
ret i32 %38
}