#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 */
typedef int /*<<< orphan*/ tv;
struct timeval {
  int tv_sec;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ fd_set;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
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*/ bzero(struct timeval *, int);
int connect(int, struct sockaddr *, int /*<<< orphan*/);
int fcntl(int, int /*<<< orphan*/, int);
int getpeername(int, struct sockaddr *, int *);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/, struct timeval *);
int sock_connect(int sfd, struct sockaddr *serv_addr, socklen_t addrlen,
                 int timeout) {
  int res, slen, flags;
  struct timeval tv;
  struct sockaddr_in addr;
  fd_set rdf, wrf;
  fcntl(sfd, F_SETFL, O_NONBLOCK);
  res = connect(sfd, serv_addr, addrlen);
  if (res >= 0)
    return res;
  FD_ZERO(&rdf);
  FD_ZERO(&wrf);
  FD_SET(sfd, &rdf);
  FD_SET(sfd, &wrf);
  bzero(&tv, sizeof(tv));
  tv.tv_sec = timeout;
  if (select(sfd + 1, &rdf, &wrf, 0, &tv) <= 0)
    return -1;
  if (FD_ISSET(sfd, &wrf) || FD_ISSET(sfd, &rdf)) {
    slen = sizeof(addr);
    if (getpeername(sfd, (struct sockaddr *)&addr, &slen) == -1)
      return -1;
    flags = fcntl(sfd, F_GETFL, NULL);
    fcntl(sfd, F_SETFL, flags & ~O_NONBLOCK);
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<sock_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1090 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11d6 <sock_connect+0x56>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12e6 <sock_connect+0x166>
lea    -0x34(%rbp),%rdi
call   1060 <FD_ZERO@plt>
lea    -0x38(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   10a0 <FD_SET@plt>
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10a0 <FD_SET@plt>
lea    -0x28(%rbp),%rdi
mov    $0x4,%esi
call   1080 <bzero@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x34(%rbp),%rsi
lea    -0x38(%rbp),%rdx
xor    %ecx,%ecx
lea    -0x28(%rbp),%r8
call   1070 <select@plt>
cmp    $0x0,%rax
jg     1243 <sock_connect+0xc3>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e6 <sock_connect+0x166>
mov    -0x8(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
jne    126f <sock_connect+0xef>
mov    -0x8(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1040 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12df <sock_connect+0x15f>
movl   $0x4,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <getpeername@plt>
cmp    $0xffffffff,%eax
jne    129b <sock_connect+0x11b>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e6 <sock_connect+0x166>
mov    -0x8(%rbp),%edi
mov    0x2d2b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d1d(%rip),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
mov    0x2d21(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%edx
call   1030 <fcntl@plt>
movl   $0x0,-0x4(%rbp)
jmp    12e6 <sock_connect+0x166>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_connect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
mov    0x2e48(%rip),%r12
mov    (%r12),%esi
mov    0x2e4d(%rip),%r13
mov    0x0(%r13),%edx
call   1030 <fcntl@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1080 <connect@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11c8 <sock_connect+0x58>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
lea    0x4(%rsp),%rbp
mov    %rbp,%rdi
call   1060 <FD_ZERO@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1090 <FD_SET@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
call   1090 <FD_SET@plt>
mov    %r14d,(%rsp)
lea    0x1(%r15),%edi
mov    %rsp,%r8
mov    %rbx,%rsi
mov    %rbp,%rdx
xor    %ecx,%ecx
call   1070 <select@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jle    11b7 <sock_connect+0x47>
lea    0x4(%rsp),%rsi
mov    %r15d,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
jne    1242 <sock_connect+0xd2>
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     11b7 <sock_connect+0x47>
movl   $0x4,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r15d,%edi
call   1050 <getpeername@plt>
cmp    $0xffffffff,%eax
je     11b7 <sock_connect+0x47>
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
mov    %r15d,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    (%r12),%esi
mov    0x0(%r13),%edx
not    %edx
and    %eax,%edx
mov    %r15d,%edi
call   1030 <fcntl@plt>
jmp    11b7 <sock_connect+0x47>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.timeval = type { i32 }
%struct.sockaddr_in = type { i32 }
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_GETFL = global i32 0
define i32 @sock_connect(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 %struct.timeval
%14 = alloca %struct.sockaddr_in
%15 = alloca i32
%16 = alloca i32
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_SETFL
%19 = load i32, i32* @O_NONBLOCK
%20 = call i32 @fcntl(i32 %17, i32 %18, i32 %19)
%21 = load i32, i32* %6
%22 = load %struct.sockaddr*, %struct.sockaddr** %7
%23 = load i32, i32* %8
%24 = call i32 @connect(i32 %21, %struct.sockaddr* %22, i32 %23)
store i32 %24, i32* %10
%25 = load i32, i32* %10
%26 = icmp sge i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %4
%28 = load i32, i32* %10
store i32 %28, i32* %5
br label %70
29: ; preds = %4
%30 = call i32 @FD_ZERO(i32* %15)
%31 = call i32 @FD_ZERO(i32* %16)
%32 = load i32, i32* %6
%33 = call i32 @FD_SET(i32 %32, i32* %15)
%34 = load i32, i32* %6
%35 = call i32 @FD_SET(i32 %34, i32* %16)
%36 = call i32 @bzero(%struct.timeval* %13, i32 4)
%37 = load i32, i32* %9
%38 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
store i32 %37, i32* %38
%39 = load i32, i32* %6
%40 = add nsw i32 %39, 1
%41 = call i64 @select(i32 %40, i32* %15, i32* %16, i32 0, %struct.timeval* %13)
%42 = icmp sle i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %29
store i32 -1, i32* %5
br label %70
44: ; preds = %29
%45 = load i32, i32* %6
%46 = call i64 @FD_ISSET(i32 %45, i32* %16)
%47 = icmp ne i64 %46, 0
br i1 %47, label %52, label %48
48: ; preds = %44
%49 = load i32, i32* %6
%50 = call i64 @FD_ISSET(i32 %49, i32* %15)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %69
52: ; preds = %48, %44
store i32 4, i32* %11
%53 = load i32, i32* %6
%54 = bitcast %struct.sockaddr_in* %14 to %struct.sockaddr*
%55 = call i32 @getpeername(i32 %53, %struct.sockaddr* %54, i32* %11)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %58
57: ; preds = %52
store i32 -1, i32* %5
br label %70
58: ; preds = %52
%59 = load i32, i32* %6
%60 = load i32, i32* @F_GETFL
%61 = call i32 @fcntl(i32 %59, i32 %60, i32 0)
store i32 %61, i32* %12
%62 = load i32, i32* %6
%63 = load i32, i32* @F_SETFL
%64 = load i32, i32* %12
%65 = load i32, i32* @O_NONBLOCK
%66 = xor i32 %65, -1
%67 = and i32 %64, %66
%68 = call i32 @fcntl(i32 %62, i32 %63, i32 %67)
store i32 0, i32* %5
br label %70
69: ; preds = %48
store i32 -1, i32* %5
br label %70
70: ; preds = %69, %58, %57, %43, %27
%71 = load i32, i32* %5
ret i32 %71
}

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