#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*/ soerror;
typedef int socklen_t;
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int ECONNREFUSED;
int EINTR;
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int errno;
int getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int proto_waitconnect(int s) {
  fd_set readfd;
  socklen_t len;
  int error, rv, soerror;
  FD_ZERO(&readfd);
  FD_SET(s, &readfd);
  do {
    rv = select(s + 1, &readfd, NULL, NULL, NULL);
  } while (rv == -1 && errno == EINTR);
  if (rv == -1)
    return (-1);
  len = sizeof(soerror);
  error = getsockopt(s, SOL_SOCKET, SO_ERROR, &soerror, &len);
  if (error) {
    errno = ECONNREFUSED;
    return (-1);
  }
  if (soerror) {
    errno = soerror;
    return (-1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<proto_waitconnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
lea    -0xc(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1050 <select@plt>
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x18(%rbp)
mov    %al,-0x1d(%rbp)
jne    11a4 <proto_waitconnect+0x64>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    0x2e24(%rip),%rcx
cmp    (%rcx),%eax
sete   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1160 <proto_waitconnect+0x20>
cmpl   $0xffffffff,-0x18(%rbp)
jne    11c5 <proto_waitconnect+0x85>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <proto_waitconnect+0x102>
movl   $0x4,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
lea    -0x10(%rbp),%r8
call   1030 <getsockopt@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1219 <proto_waitconnect+0xd9>
mov    0x2dde(%rip),%rax
mov    (%rax),%ecx
mov    0x2dc5(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <proto_waitconnect+0x102>
cmpl   $0x0,-0x1c(%rbp)
je     123b <proto_waitconnect+0xfb>
mov    -0x1c(%rbp),%ecx
mov    0x2da3(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <proto_waitconnect+0x102>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proto_waitconnect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r14d
lea    0xc(%rsp),%r12
mov    %r12,%rdi
call   1040 <FD_ZERO@plt>
mov    %r14d,%edi
mov    %r12,%rsi
call   1060 <FD_SET@plt>
lea    0x1(%r14),%ebx
mov    0x2e5e(%rip),%r15
mov    0x2e47(%rip),%rbp
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r12,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1050 <select@plt>
cmp    $0xffffffff,%eax
jne    119e <proto_waitconnect+0x5e>
mov    0x0(%rbp),%ecx
cmp    %ecx,(%r15)
je     1180 <proto_waitconnect+0x40>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11ee <proto_waitconnect+0xae>
movl   $0x4,0x8(%rsp)
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
lea    0x8(%rsp),%r8
mov    %r14d,%edi
call   1030 <getsockopt@plt>
test   %eax,%eax
je     11e3 <proto_waitconnect+0xa3>
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
jmp    11eb <proto_waitconnect+0xab>
mov    0x4(%rsp),%eax
test   %eax,%eax
je     11fd <proto_waitconnect+0xbd>
mov    %eax,(%r15)
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %ebp,%ebp
jmp    11ee <proto_waitconnect+0xae>

LLVM IR (-O0):
@errno = global i32 0
@EINTR = global i32 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
@ECONNREFUSED = global i32 0
define i32 @proto_waitconnect(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
%9 = call i32 @FD_ZERO(i32* %4)
%10 = load i32, i32* %3
%11 = call i32 @FD_SET(i32 %10, i32* %4)
br label %12
12: ; preds = %23, %1
%13 = load i32, i32* %3
%14 = add nsw i32 %13, 1
%15 = call i32 @select(i32 %14, i32* %4, i32* null, i32* null, i32* null)
store i32 %15, i32* %7
br label %16
16: ; preds = %12
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i32, i32* @errno
%21 = load i32, i32* @EINTR
%22 = icmp eq i32 %20, %21
br label %23
23: ; preds = %19, %16
%24 = phi i1 [ false, %16 ], [ %22, %19 ]
br i1 %24, label %12, label %25
25: ; preds = %23
%26 = load i32, i32* %7
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %25
store i32 -1, i32* %2
br label %44
29: ; preds = %25
store i32 4, i32* %5
%30 = load i32, i32* %3
%31 = load i32, i32* @SOL_SOCKET
%32 = load i32, i32* @SO_ERROR
%33 = call i32 @getsockopt(i32 %30, i32 %31, i32 %32, i32* %8, i32* %5)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = load i32, i32* @ECONNREFUSED
store i32 %37, i32* @errno
store i32 -1, i32* %2
br label %44
38: ; preds = %29
%39 = load i32, i32* %8
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = load i32, i32* %8
store i32 %42, i32* @errno
store i32 -1, i32* %2
br label %44
43: ; preds = %38
store i32 0, i32* %2
br label %44
44: ; preds = %43, %41, %36, %28
%45 = load i32, i32* %2
ret i32 %45
}

LLVM IR (-O2):
@errno = global i32 0
@EINTR = global i32 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
@ECONNREFUSED = global i32 0
define i32 @proto_waitconnect(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = call i32 @FD_ZERO(i32* %2)
%9 = call i32 @FD_SET(i32 %0, i32* %2)
%10 = add nsw i32 %0, 1
br label %11
11: ; preds = %11, %1
%12 = call i32 @select(i32 %10, i32* %2, i32* null, i32* null, i32* null)
%13 = icmp eq i32 %12, -1
%14 = load i32, i32* @errno
%15 = load i32, i32* @EINTR
%16 = icmp eq i32 %14, %15
%17 = select i1 %13, i1 %16, i1 false
br i1 %17, label %11, label %18
18: ; preds = %11
br i1 %13, label %31, label %19
19: ; preds = %18
store i32 4, i32* %3
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_ERROR
%22 = call i32 @getsockopt(i32 %0, i32 %20, i32 %21, i32* %4, i32* %3)
%23 = icmp eq i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %19
%25 = load i32, i32* @ECONNREFUSED
br label %29
26: ; preds = %19
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26, %24
%30 = phi i32 [ %25, %24 ], [ %27, %26 ]
store i32 %30, i32* @errno
br label %31
31: ; preds = %29, %26, %18
%32 = phi i32 [ -1, %18 ], [ 0, %26 ], [ -1, %29 ]
ret i32 %32
}