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

/* Variables and functions */
int /*<<< orphan*/ SIG_SETMASK;
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ const *,
                int /*<<< orphan*/ const *);
int pselect(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
            const struct timespec *timeout, const sigset_t *sigmask) {
  int result;
  sigset_t saved_sigmask;
  struct timeval saved_timeout;
  if (sigmask && sigprocmask(SIG_SETMASK, sigmask, &saved_sigmask) == -1)
    return -1;
  if (timeout) {
    saved_timeout.tv_sec = timeout->tv_sec;
    saved_timeout.tv_usec = timeout->tv_nsec / 1000;
    result = select(n, readfds, writefds, exceptfds, &saved_timeout);
  } else {
    result = select(n, readfds, writefds, exceptfds, NULL);
  }
  if (sigmask && sigprocmask(SIG_SETMASK, &saved_sigmask, NULL) == -1)
    return -1;
  return result;
}


x86 assembly (clang, -O0):
<pselect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1175 <pselect+0x55>
mov    0x2e97(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1030 <sigprocmask@plt>
cmp    $0xffffffff,%eax
jne    1175 <pselect+0x55>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <pselect+0xf3>
cmpq   $0x0,-0x28(%rbp)
je     11bb <pselect+0x9b>
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x40(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x40(%rbp),%r8
call   1040 <select@plt>
mov    %eax,-0x34(%rbp)
jmp    11d7 <pselect+0xb7>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1040 <select@plt>
mov    %eax,-0x34(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     120d <pselect+0xed>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
cmp    $0xffffffff,%eax
jne    120d <pselect+0xed>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <pselect+0xf3>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pselect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%rbx
mov    %r8,%rbp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%r13d
test   %r9,%r9
je     1160 <pselect+0x40>
mov    0x2e9c(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rdx
mov    %rbx,%rsi
call   1030 <sigprocmask@plt>
cmp    $0xffffffff,%eax
je     11d0 <pselect+0xb0>
test   %rbp,%rbp
je     119b <pselect+0x7b>
mov    0x4(%rbp),%eax
mov    %eax,0x14(%rsp)
movslq 0x0(%rbp),%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%r8
mov    %r13d,%edi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
jmp    11aa <pselect+0x8a>
mov    %r13d,%edi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
call   1040 <select@plt>
mov    %eax,%ebp
test   %rbx,%rbx
je     11d5 <pselect+0xb5>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
cmp    $0xffffffff,%eax
jne    11d5 <pselect+0xb5>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32, i32 }
@SIG_SETMASK = global i32 0
define i32 @pselect(i32 %0, i32* %1, i32* %2, i32* %3, %struct.timespec* %4, i32* %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca %struct.timespec*
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.timeval
store i32 %0, i32* %8
store i32* %1, i32** %9
store i32* %2, i32** %10
store i32* %3, i32** %11
store %struct.timespec* %4, %struct.timespec** %12
store i32* %5, i32** %13
%17 = load i32*, i32** %13
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %25
19: ; preds = %6
%20 = load i32, i32* @SIG_SETMASK
%21 = load i32*, i32** %13
%22 = call i32 @sigprocmask(i32 %20, i32* %21, i32* %15)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %19
store i32 -1, i32* %7
br label %59
25: ; preds = %19, %6
%26 = load %struct.timespec*, %struct.timespec** %12
%27 = icmp ne %struct.timespec* %26, null
br i1 %27, label %28, label %43
28: ; preds = %25
%29 = load %struct.timespec*, %struct.timespec** %12
%30 = getelementptr inbounds %struct.timespec, %struct.timespec* %29, i32 0, i32 1
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 1
store i32 %31, i32* %32
%33 = load %struct.timespec*, %struct.timespec** %12
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = sdiv i32 %35, 1000
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 0
store i32 %36, i32* %37
%38 = load i32, i32* %8
%39 = load i32*, i32** %9
%40 = load i32*, i32** %10
%41 = load i32*, i32** %11
%42 = call i32 @select(i32 %38, i32* %39, i32* %40, i32* %41, %struct.timeval* %16)
store i32 %42, i32* %14
br label %49
43: ; preds = %25
%44 = load i32, i32* %8
%45 = load i32*, i32** %9
%46 = load i32*, i32** %10
%47 = load i32*, i32** %11
%48 = call i32 @select(i32 %44, i32* %45, i32* %46, i32* %47, %struct.timeval* null)
store i32 %48, i32* %14
br label %49
49: ; preds = %43, %28
%50 = load i32*, i32** %13
%51 = icmp ne i32* %50, null
br i1 %51, label %52, label %57
52: ; preds = %49
%53 = load i32, i32* @SIG_SETMASK
%54 = call i32 @sigprocmask(i32 %53, i32* %15, i32* null)
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %57
56: ; preds = %52
store i32 -1, i32* %7
br label %59
57: ; preds = %52, %49
%58 = load i32, i32* %14
store i32 %58, i32* %7
br label %59
59: ; preds = %57, %56, %24
%60 = load i32, i32* %7
ret i32 %60
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32, i32 }
@SIG_SETMASK = global i32 0
define i32 @pselect(i32 %0, i32* %1, i32* %2, i32* %3, %struct.timespec* %4, i32* %5) {
%7 = alloca i32
%8 = alloca %struct.timeval
%9 = bitcast i32* %7 to i8*
%10 = bitcast %struct.timeval* %8 to i8*
%11 = icmp eq i32* %5, null
br i1 %11, label %16, label %12
12: ; preds = %6
%13 = load i32, i32* @SIG_SETMASK
%14 = call i32 @sigprocmask(i32 %13, i32* %5, i32* %7)
%15 = icmp eq i32 %14, -1
br i1 %15, label %36, label %16
16: ; preds = %12, %6
%17 = icmp eq %struct.timespec* %4, null
br i1 %17, label %27, label %18
18: ; preds = %16
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%23 = load i32, i32* %22
%24 = sdiv i32 %23, 1000
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 0
store i32 %24, i32* %25
%26 = call i32 @select(i32 %0, i32* %1, i32* %2, i32* %3, %struct.timeval* %8)
br label %29
27: ; preds = %16
%28 = call i32 @select(i32 %0, i32* %1, i32* %2, i32* %3, %struct.timeval* null)
br label %29
29: ; preds = %27, %18
%30 = phi i32 [ %26, %18 ], [ %28, %27 ]
br i1 %11, label %35, label %31
31: ; preds = %29
%32 = load i32, i32* @SIG_SETMASK
%33 = call i32 @sigprocmask(i32 %32, i32* %7, i32* null)
%34 = icmp eq i32 %33, -1
br i1 %34, label %36, label %35
35: ; preds = %31, %29
br label %36
36: ; preds = %31, %12, %35
%37 = phi i32 [ %30, %35 ], [ -1, %12 ], [ -1, %31 ]
ret i32 %37
}