#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 */
struct pollfd {
  int dummy;
};
typedef int /*<<< orphan*/ sigset_t;
typedef int /*<<< orphan*/ nfds_t;

/* Variables and functions */
int /*<<< orphan*/ SIG_SETMASK;
int /*<<< orphan*/ assert(struct pollfd *);
int errno;
int poll(struct pollfd *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sigemptyset(int /*<<< orphan*/ *);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ *,
                               int /*<<< orphan*/ *);
int time(int /*<<< orphan*/ *);
int ppoll_compat(struct pollfd fds[], nfds_t nfds, int stoptime) {
  assert(fds);
  sigset_t mask;
  sigemptyset(&mask);
  int timeout = stoptime - time(NULL);
  if (timeout < 0)
    return 0;
  sigset_t origmask;
  sigprocmask(SIG_SETMASK, &mask, &origmask);
  int rc = poll(fds, nfds, timeout * 1000);
  int saved_errno = errno;
  sigprocmask(SIG_SETMASK, &origmask, NULL);
  errno = saved_errno;
  return rc;
}


x86 assembly (clang, -O0):
<ppoll_compat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <assert@plt>
lea    -0x1c(%rbp),%rdi
call   1050 <sigemptyset@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x30(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11a3 <ppoll_compat+0x53>
movl   $0x0,-0x4(%rbp)
jmp    1203 <ppoll_compat+0xb3>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
lea    -0x1c(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1030 <sigprocmask@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
imul   $0x3e8,-0x20(%rbp),%edx
call   1070 <poll@plt>
mov    %eax,-0x28(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    -0x2c(%rbp),%ecx
mov    0x2ddd(%rip),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ppoll_compat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %esi,%r14d
mov    %rdi,%r15
call   1040 <assert@plt>
mov    %rsp,%rdi
call   1050 <sigemptyset@plt>
xor    %ebx,%ebx
xor    %edi,%edi
call   1060 <time@plt>
sub    %eax,%ebp
js     11c6 <ppoll_compat+0x76>
mov    0x2e64(%rip),%r13
mov    0x0(%r13),%edi
mov    %rsp,%rsi
lea    0x4(%rsp),%r12
mov    %r12,%rdx
call   1030 <sigprocmask@plt>
imul   $0x3e8,%ebp,%edx
mov    %r15,%rdi
mov    %r14d,%esi
call   1070 <poll@plt>
mov    %eax,%ebx
mov    0x2e26(%rip),%r14
mov    (%r14),%ebp
mov    0x0(%r13),%edi
mov    %r12,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    %ebp,(%r14)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pollfd = type { i32 }
@SIG_SETMASK = global i32 0
@errno = global i32 0
define i32 @ppoll_compat(%struct.pollfd* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.pollfd*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store %struct.pollfd* %0, %struct.pollfd** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%13 = load %struct.pollfd*, %struct.pollfd** %5
%14 = call i32 @assert(%struct.pollfd* %13)
%15 = call i32 @sigemptyset(i32* %8)
%16 = load i32, i32* %7
%17 = call i32 @time(i32* null)
%18 = sub nsw i32 %16, %17
store i32 %18, i32* %9
%19 = load i32, i32* %9
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %3
store i32 0, i32* %4
br label %35
22: ; preds = %3
%23 = load i32, i32* @SIG_SETMASK
%24 = call i32 @sigprocmask(i32 %23, i32* %8, i32* %10)
%25 = load %struct.pollfd*, %struct.pollfd** %5
%26 = load i32, i32* %6
%27 = load i32, i32* %9
%28 = mul nsw i32 %27, 1000
%29 = call i32 @poll(%struct.pollfd* %25, i32 %26, i32 %28)
store i32 %29, i32* %11
%30 = load i32, i32* @errno
store i32 %30, i32* %12
%31 = load i32, i32* @SIG_SETMASK
%32 = call i32 @sigprocmask(i32 %31, i32* %10, i32* null)
%33 = load i32, i32* %12
store i32 %33, i32* @errno
%34 = load i32, i32* %11
store i32 %34, i32* %4
br label %35
35: ; preds = %22, %21
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
%struct.pollfd = type { i32 }
@SIG_SETMASK = global i32 0
@errno = global i32 0
define i32 @ppoll_compat(%struct.pollfd* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = tail call i32 @assert(%struct.pollfd* %0)
%7 = bitcast i32* %4 to i8*
%8 = call i32 @sigemptyset(i32* %4)
%9 = call i32 @time(i32* null)
%10 = sub nsw i32 %2, %9
%11 = icmp slt i32 %10, 0
br i1 %11, label %21, label %12
12: ; preds = %3
%13 = bitcast i32* %5 to i8*
%14 = load i32, i32* @SIG_SETMASK
%15 = call i32 @sigprocmask(i32 %14, i32* %4, i32* %5)
%16 = mul nsw i32 %10, 1000
%17 = call i32 @poll(%struct.pollfd* %0, i32 %1, i32 %16)
%18 = load i32, i32* @errno
%19 = load i32, i32* @SIG_SETMASK
%20 = call i32 @sigprocmask(i32 %19, i32* %5, i32* null)
store i32 %18, i32* @errno
br label %21
21: ; preds = %3, %12
%22 = phi i32 [ %17, %12 ], [ 0, %3 ]
ret i32 %22
}