#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int fd;
};
struct epoll_event {
  int events;
  TYPE_1__ data;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ error;

/* Variables and functions */
scalar_t__ EINPROGRESS;
int EPOLLERR;
int EPOLLIN;
int EPOLLOUT;
int /*<<< orphan*/ EPOLL_CTL_ADD;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ERROR;
int /*<<< orphan*/ close(int);
int epoll_create(int);
int epoll_ctl(int, int /*<<< orphan*/, int, struct epoll_event *);
int epoll_wait(int, struct epoll_event *, int, unsigned int);
scalar_t__ errno;
scalar_t__ getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *,
                      int *);
int /*<<< orphan*/ memset(struct epoll_event *, int /*<<< orphan*/, int);
int wait_connect(int sd, unsigned int timeout_in_ms) {
  struct epoll_event connect_event;
  int epoll_fd, num_events, error;
  socklen_t err_len;
  if (errno != EINPROGRESS)
    return -1;
  epoll_fd = epoll_create(1);
  if (epoll_fd == -1)
    return -1;
  connect_event.data.fd = sd;
  connect_event.events = EPOLLOUT | EPOLLIN | EPOLLERR;
  if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sd, &connect_event) == -1) {
    close(epoll_fd);
    return -1;
  }
  memset(&connect_event, 0, sizeof(struct epoll_event));
  num_events = epoll_wait(epoll_fd, &connect_event, 1, timeout_in_ms);
  if (num_events <= 0) {
    close(epoll_fd);
    return -1;
  }
  error = -1;
  err_len = sizeof(error);
  if ((getsockopt(sd, SOL_SOCKET, SO_ERROR, &error, &err_len) != 0) ||
      (error != 0)) {
    close(epoll_fd);
    return -1;
  }
  close(epoll_fd);
  return 0;
}


x86 assembly (clang, -O0):
<wait_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%rax
mov    0x2e61(%rip),%rcx
cmp    (%rcx),%rax
je     1194 <wait_connect+0x34>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bc <wait_connect+0x15c>
mov    $0x1,%edi
call   1080 <epoll_create@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11b7 <wait_connect+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bc <wait_connect+0x15c>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    0x2e0b(%rip),%rcx
or     (%rcx),%eax
mov    0x2dea(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
lea    -0x18(%rbp),%rcx
call   1050 <epoll_ctl@plt>
cmp    $0xffffffff,%eax
jne    1210 <wait_connect+0xb0>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bc <wait_connect+0x15c>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    -0x1c(%rbp),%edi
mov    -0xc(%rbp),%ecx
lea    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1070 <epoll_wait@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jg     1255 <wait_connect+0xf5>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bc <wait_connect+0x15c>
movl   $0xffffffff,-0x24(%rbp)
movl   $0x4,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
mov    0x2d32(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
lea    -0x28(%rbp),%r8
call   1060 <getsockopt@plt>
cmp    $0x0,%rax
jne    1299 <wait_connect+0x139>
cmpl   $0x0,-0x24(%rbp)
je     12ad <wait_connect+0x14d>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bc <wait_connect+0x15c>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    0x2e3d(%rip),%rax
mov    (%rax),%rax
mov    0x2e63(%rip),%rcx
mov    $0xffffffff,%ebx
cmp    (%rcx),%rax
jne    126b <wait_connect+0x10b>
mov    %esi,%r15d
mov    %edi,%ebp
mov    $0x1,%edi
call   1080 <epoll_create@plt>
cmp    $0xffffffff,%eax
je     126b <wait_connect+0x10b>
mov    %eax,%r14d
mov    0x2e0b(%rip),%rax
mov    0x2e24(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e01(%rip),%rax
or     (%rax),%ecx
mov    %ebp,0x14(%rsp)
mov    %ecx,0x10(%rsp)
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rcx
mov    %r14d,%edi
mov    %ebp,%edx
call   1050 <epoll_ctl@plt>
cmp    $0xffffffff,%eax
je     1263 <wait_connect+0x103>
lea    0x10(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
mov    %r15d,%ecx
call   1070 <epoll_wait@plt>
test   %eax,%eax
jle    1263 <wait_connect+0x103>
movl   $0xffffffff,0xc(%rsp)
ff
movl   $0x4,0x1c(%rsp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
mov    0x2d77(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
lea    0x1c(%rsp),%r8
mov    %ebp,%edi
call   1060 <getsockopt@plt>
test   %rax,%rax
setne  %al
cmpl   $0x0,0xc(%rsp)
setne  %bl
or     %al,%bl
mov    %r14d,%edi
call   1040 <close@plt>
movzbl %bl,%ebx
neg    %ebx
jmp    126b <wait_connect+0x10b>
mov    %r14d,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@errno = global i64 0
@EINPROGRESS = global i64 0
@EPOLLOUT = global i32 0
@EPOLLIN = global i32 0
@EPOLLERR = global i32 0
@EPOLL_CTL_ADD = global i32 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
define i32 @wait_connect(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.epoll_event
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINPROGRESS
%13 = icmp ne i64 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 -1, i32* %3
br label %63
15: ; preds = %2
%16 = call i32 @epoll_create(i32 1)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 -1, i32* %3
br label %63
20: ; preds = %15
%21 = load i32, i32* %4
%22 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %6, i32 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = load i32, i32* @EPOLLOUT
%25 = load i32, i32* @EPOLLIN
%26 = or i32 %24, %25
%27 = load i32, i32* @EPOLLERR
%28 = or i32 %26, %27
%29 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %6, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* %7
%31 = load i32, i32* @EPOLL_CTL_ADD
%32 = load i32, i32* %4
%33 = call i32 @epoll_ctl(i32 %30, i32 %31, i32 %32, %struct.epoll_event* %6)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %20
%36 = load i32, i32* %7
%37 = call i32 @close(i32 %36)
store i32 -1, i32* %3
br label %63
38: ; preds = %20
%39 = call i32 @memset(%struct.epoll_event* %6, i32 0, i32 8)
%40 = load i32, i32* %7
%41 = load i32, i32* %5
%42 = call i32 @epoll_wait(i32 %40, %struct.epoll_event* %6, i32 1, i32 %41)
store i32 %42, i32* %8
%43 = load i32, i32* %8
%44 = icmp sle i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %38
%46 = load i32, i32* %7
%47 = call i32 @close(i32 %46)
store i32 -1, i32* %3
br label %63
48: ; preds = %38
store i32 -1, i32* %9
store i32 4, i32* %10
%49 = load i32, i32* %4
%50 = load i32, i32* @SOL_SOCKET
%51 = load i32, i32* @SO_ERROR
%52 = call i64 @getsockopt(i32 %49, i32 %50, i32 %51, i32* %9, i32* %10)
%53 = icmp ne i64 %52, 0
br i1 %53, label %57, label %54
54: ; preds = %48
%55 = load i32, i32* %9
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %60
57: ; preds = %54, %48
%58 = load i32, i32* %7
%59 = call i32 @close(i32 %58)
store i32 -1, i32* %3
br label %63
60: ; preds = %54
%61 = load i32, i32* %7
%62 = call i32 @close(i32 %61)
store i32 0, i32* %3
br label %63
63: ; preds = %60, %57, %45, %35, %19, %14
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@errno = global i64 0
@EINPROGRESS = global i64 0
@EPOLLOUT = global i32 0
@EPOLLIN = global i32 0
@EPOLLERR = global i32 0
@EPOLL_CTL_ADD = global i32 0
@SOL_SOCKET = global i32 0
@SO_ERROR = global i32 0
define i32 @wait_connect(i32 %0, i32 %1) {
%3 = alloca %struct.epoll_event
%4 = alloca i32
%5 = alloca i32
%6 = bitcast %struct.epoll_event* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINPROGRESS
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %44
12: ; preds = %2
%13 = tail call i32 @epoll_create(i32 1)
%14 = icmp eq i32 %13, -1
br i1 %14, label %44, label %15
15: ; preds = %12
%16 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %3, i64 0, i32 1, i32 0
store i32 %0, i32* %16
%17 = load i32, i32* @EPOLLOUT
%18 = load i32, i32* @EPOLLIN
%19 = or i32 %18, %17
%20 = load i32, i32* @EPOLLERR
%21 = or i32 %19, %20
%22 = getelementptr inbounds %struct.epoll_event, %struct.epoll_event* %3, i64 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* @EPOLL_CTL_ADD
%24 = call i32 @epoll_ctl(i32 %13, i32 %23, i32 %0, %struct.epoll_event* %3)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %15
%27 = call i32 @close(i32 %13)
br label %44
28: ; preds = %15
%29 = call i32 @memset(%struct.epoll_event* %3, i32 0, i32 8)
%30 = call i32 @epoll_wait(i32 %13, %struct.epoll_event* %3, i32 1, i32 %1)
%31 = icmp slt i32 %30, 1
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @close(i32 %13)
br label %44
34: ; preds = %28
store i32 -1, i32* %4
store i32 4, i32* %5
%35 = load i32, i32* @SOL_SOCKET
%36 = load i32, i32* @SO_ERROR
%37 = call i64 @getsockopt(i32 %0, i32 %35, i32 %36, i32* %4, i32* %5)
%38 = icmp ne i64 %37, 0
%39 = load i32, i32* %4
%40 = icmp ne i32 %39, 0
%41 = select i1 %38, i1 true, i1 %40
%42 = call i32 @close(i32 %13)
%43 = sext i1 %41 to i32
br label %44
44: ; preds = %34, %12, %2, %32, %26
%45 = phi i32 [ -1, %26 ], [ -1, %32 ], [ -1, %2 ], [ -1, %12 ], [ %43, %34 ]
ret i32 %45
}