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

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ ETIMEDOUT;
int POLLERR;
int POLLHUP;
int POLLIN;
int accept(int, struct sockaddr *, int /*<<< orphan*/);
scalar_t__ errno;
double monotonic_seconds();
scalar_t__ poll(struct pollfd *, int, double);
int streamtoaccept(int lsock, uint32_t msecto) {
  int i;
  double s, c;
  uint32_t msecpassed;
  struct pollfd pfd;
  i = accept(lsock, (struct sockaddr *)NULL, 0);
  if (i >= 0) {
    return i;
  } else if ((errno != EAGAIN)) {
    return -1;
  }
  s = monotonic_seconds();
  msecpassed = 0;
  while (1) {
    pfd.fd = lsock;
    pfd.events = POLLIN;
    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 & POLLIN) {
    return accept(lsock, (struct sockaddr *)NULL, 0);
  }
  errno = ETIMEDOUT;
  return -1;
}


x86 assembly (clang, -O0):
<streamtoaccept>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <accept@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     1166 <streamtoaccept+0x36>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d9 <streamtoaccept+0x1a9>
mov    0x2e5b(%rip),%rax
mov    (%rax),%rax
mov    0x2e31(%rip),%rcx
cmp    (%rcx),%rax
je     118c <streamtoaccept+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d9 <streamtoaccept+0x1a9>
jmp    1191 <streamtoaccept+0x61>
mov    $0x0,%al
call   1050 <monotonic_seconds@plt>
movsd  %xmm0,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
movl   $0x0,-0x38(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x30(%rbp),%xmm0
lea    -0x40(%rbp),%rdi
mov    $0x1,%esi
call   1030 <poll@plt>
cmp    $0x0,%rax
jl     11e5 <streamtoaccept+0xb5>
jmp    1267 <streamtoaccept+0x137>
mov    0x2ddc(%rip),%rax
mov    (%rax),%rax
mov    0x2dc2(%rip),%rcx
cmp    (%rcx),%rax
jne    125b <streamtoaccept+0x12b>
mov    $0x0,%al
call   1050 <monotonic_seconds@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
subsd  -0x20(%rbp),%xmm0
movsd  0xde3(%rip),%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x30(%rbp),%xmm0
ucomisd -0x10(%rbp),%xmm0
jb     1256 <streamtoaccept+0x126>
mov    0x2d83(%rip),%rax
mov    (%rax),%rcx
mov    0x2d81(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    12d9 <streamtoaccept+0x1a9>
jmp    11a5 <streamtoaccept+0x75>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d9 <streamtoaccept+0x1a9>
mov    -0x38(%rbp),%eax
mov    0x2d77(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2d66(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1293 <streamtoaccept+0x163>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d9 <streamtoaccept+0x1a9>
mov    -0x38(%rbp),%eax
mov    0x2d3b(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12be <streamtoaccept+0x18e>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <accept@plt>
mov    %eax,-0x4(%rbp)
jmp    12d9 <streamtoaccept+0x1a9>
mov    0x2cfb(%rip),%rax
mov    (%rax),%rcx
mov    0x2cf9(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<streamtoaccept>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
movsd  %xmm0,0x18(%rsp)
mov    %edi,%ebp
xor    %esi,%esi
xor    %edx,%edx
call   1040 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1244 <streamtoaccept+0x114>
mov    0x2e68(%rip),%r15
mov    (%r15),%rax
mov    0x2e3e(%rip),%rcx
mov    $0xffffffff,%ebx
cmp    (%rcx),%rax
jne    1244 <streamtoaccept+0x114>
xor    %eax,%eax
call   1050 <monotonic_seconds@plt>
movsd  %xmm0,0x20(%rsp)
xorpd  %xmm0,%xmm0
mov    0x2e48(%rip),%r12
lea    0x8(%rsp),%r14
mov    0x2e1c(%rip),%r13
movsd  0x18(%rsp),%xmm1
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebp,0x8(%rsp)
mov    (%r12),%eax
mov    %eax,0xc(%rsp)
movl   $0x0,0x10(%rsp)
subsd  %xmm0,%xmm1
mov    %r14,%rdi
mov    $0x1,%esi
movapd %xmm1,%xmm0
call   1030 <poll@plt>
test   %rax,%rax
jns    1217 <streamtoaccept+0xe7>
mov    (%r15),%rax
cmp    0x0(%r13),%rax
jne    1244 <streamtoaccept+0x114>
xor    %eax,%eax
call   1050 <monotonic_seconds@plt>
subsd  0x20(%rsp),%xmm0
mulsd  0xe04(%rip),%xmm0
movsd  0x18(%rsp),%xmm1
ucomisd %xmm1,%xmm0
jb     11b0 <streamtoaccept+0x80>
mov    0x2db1(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r15)
jmp    1244 <streamtoaccept+0x114>
mov    0x10(%rsp),%eax
mov    0x2dc6(%rip),%rcx
mov    0x2db7(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
test   %eax,%edx
jne    1244 <streamtoaccept+0x114>
test   %eax,(%r12)
je     1208 <streamtoaccept+0xd8>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <accept@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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