#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 double uint32_t;
struct sockaddr_un {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct pollfd {
  int fd;
  int events;
  int revents;
};

/* Variables and functions */
scalar_t__ EINPROGRESS;
scalar_t__ EINTR;
scalar_t__ ETIMEDOUT;
int POLLERR;
int POLLHUP;
int POLLOUT;
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ errno;
double monotonic_seconds();
scalar_t__ poll(struct pollfd *, int, double);
scalar_t__ sockaddrpathfill(struct sockaddr_un *, char const *);
int sockgetstatus(int);
scalar_t__ socknonblock(int);
int unixtoconnect(int sock, const char *path, uint32_t msecto) {
  struct sockaddr_un sa;
  if (socknonblock(sock) < 0) {
    return -1;
  }
  if (sockaddrpathfill(&sa, path) < 0) {
    return -1;
  }
  if (connect(sock, (struct sockaddr *)&sa, sizeof(struct sockaddr_un)) >= 0) {
    return 0;
  }
  if (errno == EINPROGRESS) {
    double s, c;
    uint32_t msecpassed;
    struct pollfd pfd;
    s = monotonic_seconds();
    msecpassed = 0;
    while (1) {
      pfd.fd = sock;
      pfd.events = POLLOUT;
      pfd.revents = 0;
      if (poll(&pfd, 1, msecto - msecpassed) >= 0) {
        break;
      }
      if (errno == EINTR) {
        c = monotonic_seconds();
        msecpassed = (c - s) * 1000.0;
        if (msecpassed >= msecto) {
          errno = ETIMEDOUT;
          return -1;
        }
        continue;
      } else {
        return -1;
      }
    }
    if (pfd.revents & (POLLHUP | POLLERR)) {
      return -1;
    }
    if (pfd.revents & POLLOUT) {
      return sockgetstatus(sock);
    }
    errno = ETIMEDOUT;
  }
  return -1;
}


x86 assembly (clang, -O0):
<unixtoconnect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <socknonblock@plt>
cmp    $0x0,%rax
jge    1192 <unixtoconnect+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <unixtoconnect+0x1d8>
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdi
call   1040 <sockaddrpathfill@plt>
cmp    $0x0,%rax
jge    11b5 <unixtoconnect+0x55>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <unixtoconnect+0x1d8>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jl     11dc <unixtoconnect+0x7c>
movl   $0x0,-0x4(%rbp)
jmp    1338 <unixtoconnect+0x1d8>
mov    0x2ddd(%rip),%rax
mov    (%rax),%rax
mov    0x2deb(%rip),%rcx
cmp    (%rcx),%rax
jne    1331 <unixtoconnect+0x1d1>
mov    $0x0,%al
call   1070 <monotonic_seconds@plt>
movsd  %xmm0,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
movl   $0x0,-0x40(%rbp)
movsd  -0x18(%rbp),%xmm0
subsd  -0x38(%rbp),%xmm0
lea    -0x48(%rbp),%rdi
mov    $0x1,%esi
call   1050 <poll@plt>
cmp    $0x0,%rax
jl     124a <unixtoconnect+0xea>
jmp    12cc <unixtoconnect+0x16c>
mov    0x2d6f(%rip),%rax
mov    (%rax),%rax
mov    0x2d55(%rip),%rcx
cmp    (%rcx),%rax
jne    12c0 <unixtoconnect+0x160>
mov    $0x0,%al
call   1070 <monotonic_seconds@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
movsd  0xd7e(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
ucomisd -0x18(%rbp),%xmm0
jb     12bb <unixtoconnect+0x15b>
mov    0x2d16(%rip),%rax
mov    (%rax),%rcx
mov    0x2d14(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <unixtoconnect+0x1d8>
jmp    120a <unixtoconnect+0xaa>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <unixtoconnect+0x1d8>
mov    -0x40(%rbp),%eax
mov    0x2d0a(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2cf1(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     12f8 <unixtoconnect+0x198>
movl   $0xffffffff,-0x4(%rbp)
jmp    1338 <unixtoconnect+0x1d8>
mov    -0x40(%rbp),%eax
mov    0x2cf6(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     131d <unixtoconnect+0x1bd>
mov    -0x8(%rbp),%edi
call   1060 <sockgetstatus@plt>
mov    %eax,-0x4(%rbp)
jmp    1338 <unixtoconnect+0x1d8>
mov    0x2c94(%rip),%rax
mov    (%rax),%rcx
mov    0x2c92(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unixtoconnect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movsd  %xmm0,0x18(%rsp)
mov    %rsi,%rbx
mov    %edi,%r15d
call   1030 <socknonblock@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     12bd <unixtoconnect+0x15d>
mov    %rsp,%rdi
mov    %rbx,%rsi
call   1040 <sockaddrpathfill@plt>
test   %rax,%rax
js     12bd <unixtoconnect+0x15d>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x4,%edx
call   1080 <connect@plt>
xor    %ebp,%ebp
test   %rax,%rax
jns    12bd <unixtoconnect+0x15d>
mov    0x2dfd(%rip),%r12
mov    (%r12),%rax
mov    0x2e0a(%rip),%rcx
mov    $0xffffffff,%ebp
cmp    (%rcx),%rax
jne    12bd <unixtoconnect+0x15d>
xor    %eax,%eax
call   1070 <monotonic_seconds@plt>
movsd  %xmm0,0x20(%rsp)
xorpd  %xmm0,%xmm0
mov    0x2e04(%rip),%r13
lea    0x8(%rsp),%r14
mov    0x2db0(%rip),%rbx
movsd  0x18(%rsp),%xmm1
mov    $0xffffffff,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r15d,0x8(%rsp)
mov    0x0(%r13),%eax
mov    %eax,0xc(%rsp)
movl   $0x0,0x10(%rsp)
subsd  %xmm0,%xmm1
mov    %r14,%rdi
mov    $0x1,%esi
movapd %xmm1,%xmm0
call   1050 <poll@plt>
test   %rax,%rax
jns    1279 <unixtoconnect+0x119>
mov    (%r12),%rax
cmp    (%rbx),%rax
jne    12bd <unixtoconnect+0x15d>
xor    %eax,%eax
call   1070 <monotonic_seconds@plt>
subsd  0x20(%rsp),%xmm0
mulsd  0xda3(%rip),%xmm0
movsd  0x18(%rsp),%xmm1
ucomisd %xmm1,%xmm0
jb     1210 <unixtoconnect+0xb0>
mov    0x2d48(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r12)
jmp    12bd <unixtoconnect+0x15d>
mov    0x10(%rsp),%eax
mov    0x2d5c(%rip),%rcx
mov    0x2d45(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    $0xffffffff,%ebp
test   %eax,%edx
jne    12bd <unixtoconnect+0x15d>
test   %eax,0x0(%r13)
je     12aa <unixtoconnect+0x14a>
mov    %r15d,%edi
call   1060 <sockgetstatus@plt>
mov    %eax,%ebp
jmp    12bd <unixtoconnect+0x15d>
mov    0x2d07(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r12)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32 }
%struct.pollfd = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINPROGRESS = global i64 0
@POLLOUT = global i32 0
@EINTR = global i64 0
@ETIMEDOUT = global i64 0
@POLLHUP = global i32 0
@POLLERR = global i32 0
define i32 @unixtoconnect(i32 %0, i8* %1, double %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca double
%8 = alloca %struct.sockaddr_un
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca %struct.pollfd
store i32 %0, i32* %5
store i8* %1, i8** %6
store double %2, double* %7
%13 = load i32, i32* %5
%14 = call i64 @socknonblock(i32 %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 -1, i32* %4
br label %84
17: ; preds = %3
%18 = load i8*, i8** %6
%19 = call i64 @sockaddrpathfill(%struct.sockaddr_un* %8, i8* %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %17
store i32 -1, i32* %4
br label %84
22: ; preds = %17
%23 = load i32, i32* %5
%24 = bitcast %struct.sockaddr_un* %8 to %struct.sockaddr*
%25 = call i64 @connect(i32 %23, %struct.sockaddr* %24, i32 4)
%26 = icmp sge i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %22
store i32 0, i32* %4
br label %84
28: ; preds = %22
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINPROGRESS
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %83
32: ; preds = %28
%33 = call double (...) @monotonic_seconds()
store double %33, double* %9
store double 0.000000e+00, double* %11
br label %34
34: ; preds = %32, %61
%35 = load i32, i32* %5
%36 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i32 0, i32 0
store i32 %35, i32* %36
%37 = load i32, i32* @POLLOUT
%38 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i32 0, i32 1
store i32 %37, i32* %38
%39 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i32 0, i32 2
store i32 0, i32* %39
%40 = load double, double* %7
%41 = load double, double* %11
%42 = fsub double %40, %41
%43 = call i64 @poll(%struct.pollfd* %12, i32 1, double %42)
%44 = icmp sge i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %34
br label %63
46: ; preds = %34
%47 = load i64, i64* @errno
%48 = load i64, i64* @EINTR
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %62
50: ; preds = %46
%51 = call double (...) @monotonic_seconds()
store double %51, double* %10
%52 = load double, double* %10
%53 = load double, double* %9
%54 = fsub double %52, %53
%55 = fmul double %54, 1.000000e+03
store double %55, double* %11
%56 = load double, double* %11
%57 = load double, double* %7
%58 = fcmp oge double %56, %57
br i1 %58, label %59, label %61
59: ; preds = %50
%60 = load i64, i64* @ETIMEDOUT
store i64 %60, i64* @errno
store i32 -1, i32* %4
br label %84
61: ; preds = %50
br label %34
62: ; preds = %46
store i32 -1, i32* %4
br label %84
63: ; preds = %45
%64 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i32 0, i32 2
%65 = load i32, i32* %64
%66 = load i32, i32* @POLLHUP
%67 = load i32, i32* @POLLERR
%68 = or i32 %66, %67
%69 = and i32 %65, %68
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %63
store i32 -1, i32* %4
br label %84
72: ; preds = %63
%73 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %12, i32 0, i32 2
%74 = load i32, i32* %73
%75 = load i32, i32* @POLLOUT
%76 = and i32 %74, %75
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %72
%79 = load i32, i32* %5
%80 = call i32 @sockgetstatus(i32 %79)
store i32 %80, i32* %4
br label %84
81: ; preds = %72
%82 = load i64, i64* @ETIMEDOUT
store i64 %82, i64* @errno
br label %83
83: ; preds = %81, %28
store i32 -1, i32* %4
br label %84
84: ; preds = %83, %78, %71, %62, %59, %27, %21, %16
%85 = load i32, i32* %4
ret i32 %85
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32 }
%struct.pollfd = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@errno = global i64 0
@EINPROGRESS = global i64 0
@POLLOUT = global i32 0
@EINTR = global i64 0
@ETIMEDOUT = global i64 0
@POLLHUP = global i32 0
@POLLERR = global i32 0
define i32 @unixtoconnect(i32 %0, i8* %1, double %2) {
%4 = alloca %struct.sockaddr_un
%5 = alloca %struct.pollfd
%6 = bitcast %struct.sockaddr_un* %4 to i8*
%7 = tail call i64 @socknonblock(i32 %0)
%8 = icmp slt i64 %7, 0
br i1 %8, label %60, label %9
9: ; preds = %3
%10 = call i64 @sockaddrpathfill(%struct.sockaddr_un* %4, i8* %1)
%11 = icmp slt i64 %10, 0
br i1 %11, label %60, label %12
12: ; preds = %9
%13 = bitcast %struct.sockaddr_un* %4 to %struct.sockaddr*
%14 = call i64 @connect(i32 %0, %struct.sockaddr* %13, i32 4)
%15 = icmp sgt i64 %14, -1
br i1 %15, label %60, label %16
16: ; preds = %12
%17 = load i64, i64* @errno
%18 = load i64, i64* @EINPROGRESS
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %60
20: ; preds = %16
%21 = bitcast %struct.pollfd* %5 to i8*
%22 = call double (...) @monotonic_seconds()
%23 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 0
%24 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 1
%25 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %5, i64 0, i32 2
br label %26
26: ; preds = %36, %20
%27 = phi double [ 0.000000e+00, %20 ], [ %39, %36 ]
store i32 %0, i32* %23
%28 = load i32, i32* @POLLOUT
store i32 %28, i32* %24
store i32 0, i32* %25
%29 = fsub double %2, %27
%30 = call i64 @poll(%struct.pollfd* %5, i32 1, double %29)
%31 = icmp sgt i64 %30, -1
br i1 %31, label %43, label %32
32: ; preds = %26
%33 = load i64, i64* @errno
%34 = load i64, i64* @EINTR
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %56
36: ; preds = %32
%37 = call double (...) @monotonic_seconds()
%38 = fsub double %37, %22
%39 = fmul double %38, 1.000000e+03
%40 = fcmp ult double %39, %2
br i1 %40, label %26, label %41
41: ; preds = %36
%42 = load i64, i64* @ETIMEDOUT
store i64 %42, i64* @errno
br label %56
43: ; preds = %26
%44 = load i32, i32* %25
%45 = load i32, i32* @POLLHUP
%46 = load i32, i32* @POLLERR
%47 = or i32 %46, %45
%48 = and i32 %47, %44
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %56
50: ; preds = %43
%51 = load i32, i32* @POLLOUT
%52 = and i32 %51, %44
%53 = icmp eq i32 %52, 0
br i1 %53, label %58, label %54
54: ; preds = %50
%55 = call i32 @sockgetstatus(i32 %0)
br label %56
56: ; preds = %32, %54, %41, %43
%57 = phi i32 [ -1, %43 ], [ -1, %41 ], [ %55, %54 ], [ -1, %32 ]
br label %60
58: ; preds = %50
%59 = load i64, i64* @ETIMEDOUT
store i64 %59, i64* @errno
br label %60
60: ; preds = %16, %58, %56, %12, %9, %3
%61 = phi i32 [ -1, %3 ], [ -1, %9 ], [ 0, %12 ], [ %57, %56 ], [ -1, %58 ], [ -1, %16 ]
ret i32 %61
}