#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 dummy;
};
typedef int /*<<< orphan*/ sigset_t;
typedef int /*<<< orphan*/ siginfo_t;

/* Variables and functions */
scalar_t__ EINTR;
int _NSIG;
scalar_t__ errno;
int syscall_cp(int, int /*<<< orphan*/ const *, int /*<<< orphan*/ *,
               struct timespec const *, int);
int sigtimedwait(const sigset_t *restrict mask, siginfo_t *restrict si,
                 const struct timespec *restrict timeout) {
  int ret;
  do
    ret = syscall_cp(177, mask, si, timeout, _NSIG / 8);
  while (ret < 0 && errno == EINTR);
  return ret;
}


x86 assembly (clang, -O0):
<sigtimedwait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rcx
mov    0x2eb5(%rip),%rax
mov    (%rax),%eax
mov    $0x8,%edi
cltd
idiv   %edi
mov    -0x28(%rbp),%rdx
mov    %eax,%r8d
mov    $0xb1,%edi
call   1030 <syscall_cp@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x29(%rbp)
jge    1182 <sigtimedwait+0x72>
mov    0x2e69(%rip),%rax
mov    (%rax),%rax
mov    0x2e57(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1124 <sigtimedwait+0x14>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sigtimedwait>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
mov    0x2ec5(%rip),%r12
mov    0x2ea6(%rip),%r13
mov    0x2e97(%rip),%rbp
nopl   0x0(%rax)
mov    (%r12),%eax
lea    0x7(%rax),%r8d
test   %eax,%eax
cmovns %eax,%r8d
sar    $0x3,%r8d
mov    $0xb1,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1030 <syscall_cp@plt>
test   %eax,%eax
jns    1173 <sigtimedwait+0x63>
mov    0x0(%rbp),%rcx
cmp    %rcx,0x0(%r13)
je     1140 <sigtimedwait+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
@_NSIG = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @sigtimedwait(i32* noalias %0, i32* noalias %1, %struct.timespec* noalias %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca %struct.timespec*
%7 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store %struct.timespec* %2, %struct.timespec** %6
br label %8
8: ; preds = %22, %3
%9 = load i32*, i32** %4
%10 = load i32*, i32** %5
%11 = load %struct.timespec*, %struct.timespec** %6
%12 = load i32, i32* @_NSIG
%13 = sdiv i32 %12, 8
%14 = call i32 @syscall_cp(i32 177, i32* %9, i32* %10, %struct.timespec* %11, i32 %13)
store i32 %14, i32* %7
br label %15
15: ; preds = %8
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINTR
%21 = icmp eq i64 %19, %20
br label %22
22: ; preds = %18, %15
%23 = phi i1 [ false, %15 ], [ %21, %18 ]
br i1 %23, label %8, label %24
24: ; preds = %22
%25 = load i32, i32* %7
ret i32 %25
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
@_NSIG = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @sigtimedwait(i32* noalias %0, i32* noalias %1, %struct.timespec* noalias %2) {
br label %4
4: ; preds = %4, %3
%5 = load i32, i32* @_NSIG
%6 = sdiv i32 %5, 8
%7 = tail call i32 @syscall_cp(i32 177, i32* %0, i32* %1, %struct.timespec* %2, i32 %6)
%8 = icmp slt i32 %7, 0
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINTR
%11 = icmp eq i64 %9, %10
%12 = select i1 %8, i1 %11, i1 false
br i1 %12, label %4, label %13
13: ; preds = %4
ret i32 %7
}