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

/* Variables and functions */
int /*<<< orphan*/ SIG_SETMASK;
int poll(struct pollfd *, int, int);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ const *,
                               int /*<<< orphan*/ const *);
int ppoll(struct pollfd *restrict fds, int n,
          const struct timespec *restrict ts, const sigset_t *restrict sigset) {
  sigset_t origset;
  int timeout;
  int val;
  if (ts != NULL)
    timeout = (ts->tv_sec * 1000) + (ts->tv_nsec / 1000000);
  else
    timeout = -1;
  sigprocmask(SIG_SETMASK, sigset, &origset);
  val = poll(fds, n, timeout);
  sigprocmask(SIG_SETMASK, &origset, NULL);
  return val;
}


x86 assembly (clang, -O0):
<ppoll>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     116d <ppoll+0x4d>
mov    -0x18(%rbp),%rax
imul   $0x3e8,(%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
add    %ecx,%eax
mov    %eax,-0x28(%rbp)
jmp    1174 <ppoll+0x54>
movl   $0xffffffff,-0x28(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   1030 <sigprocmask@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1040 <poll@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ppoll>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
test   %rdx,%rdx
je     1159 <ppoll+0x39>
imul   $0x3e8,(%rdx),%eax
movslq 0x4(%rdx),%rdx
imul   $0x431bde83,%rdx,%rbp
mov    %rbp,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rbp
add    %edx,%ebp
add    %eax,%ebp
jmp    115e <ppoll+0x3e>
mov    $0xffffffff,%ebp
mov    0x2e83(%rip),%rbx
mov    (%rbx),%edi
lea    0xc(%rsp),%r12
mov    %rcx,%rsi
mov    %r12,%rdx
call   1030 <sigprocmask@plt>
mov    %r15,%rdi
mov    %r14d,%esi
mov    %ebp,%edx
call   1040 <poll@plt>
mov    %eax,%ebp
mov    (%rbx),%edi
mov    %r12,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.pollfd = type { i32 }
%struct.timespec = type { i32, i32 }
@SIG_SETMASK = global i32 0
define i32 @ppoll(%struct.pollfd* noalias %0, i32 %1, %struct.timespec* noalias %2, i32* noalias %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = icmp eq %struct.timespec* %2, null
br i1 %7, label %16, label %8
8: ; preds = %4
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%10 = load i32, i32* %9
%11 = mul nsw i32 %10, 1000
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%13 = load i32, i32* %12
%14 = sdiv i32 %13, 1000000
%15 = add nsw i32 %14, %11
br label %16
16: ; preds = %4, %8
%17 = phi i32 [ %15, %8 ], [ -1, %4 ]
%18 = load i32, i32* @SIG_SETMASK
%19 = call i32 @sigprocmask(i32 %18, i32* %3, i32* %5)
%20 = call i32 @poll(%struct.pollfd* %0, i32 %1, i32 %17)
%21 = load i32, i32* @SIG_SETMASK
%22 = call i32 @sigprocmask(i32 %21, i32* %5, i32* null)
ret i32 %20
}