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

/* Variables and functions */
int /*<<< orphan*/ SIG_SETMASK;
int /*<<< orphan*/ TIMESPEC_TO_TIMEVAL(struct timeval *,
                                       struct timespec const *);
int errno;
int pthread_sigmask(int /*<<< orphan*/, int /*<<< orphan*/ const *,
                    int /*<<< orphan*/ const *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int pselect(int count, fd_set *restrict rfds, fd_set *restrict wfds,
            fd_set *restrict efds, const struct timespec *restrict timo,
            const sigset_t *restrict mask) {
  sigset_t omask;
  struct timeval tvtimo, *tvp;
  int rv, sverrno;
  if (timo) {
    TIMESPEC_TO_TIMEVAL(&tvtimo, timo);
    tvp = &tvtimo;
  } else
    tvp = 0;
  if (mask != 0) {
    rv = pthread_sigmask(SIG_SETMASK, mask, &omask);
    if (rv != 0)
      return rv;
  }
  rv = select(count, rfds, wfds, efds, tvp);
  if (mask != 0) {
    sverrno = errno;
    pthread_sigmask(SIG_SETMASK, &omask, (sigset_t *)0);
    errno = sverrno;
  }
  return rv;
}


x86 assembly (clang, -O0):
<pselect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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,-0x28(%rbp)
je     1174 <pselect+0x44>
mov    -0x28(%rbp),%rsi
lea    -0x38(%rbp),%rdi
call   1050 <TIMESPEC_TO_TIMEVAL@plt>
lea    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    117c <pselect+0x4c>
movq   $0x0,-0x40(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11ba <pselect+0x8a>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   1040 <pthread_sigmask@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     11b5 <pselect+0x85>
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1214 <pselect+0xe4>
jmp    11ba <pselect+0x8a>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x40(%rbp),%r8
call   1030 <select@plt>
mov    %eax,-0x44(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     120e <pselect+0xde>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    -0x34(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <pthread_sigmask@plt>
mov    -0x48(%rbp),%ecx
mov    0x2dcc(%rip),%rax
mov    %ecx,(%rax)
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%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,%rbp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%r13d
test   %r8,%r8
je     11bd <pselect+0x8d>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %r8,%rsi
call   1050 <TIMESPEC_TO_TIMEVAL@plt>
test   %rbp,%rbp
je     11c4 <pselect+0x94>
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rdx
mov    %rbp,%rsi
call   1040 <pthread_sigmask@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11da <pselect+0xaa>
mov    %r13d,%edi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %rbx,%r8
call   1030 <select@plt>
mov    %eax,%ebp
mov    0x2e38(%rip),%r14
mov    (%r14),%ebx
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1040 <pthread_sigmask@plt>
mov    %ebx,(%r14)
jmp    11da <pselect+0xaa>
xor    %ebx,%ebx
test   %rbp,%rbp
jne    1167 <pselect+0x37>
mov    %r13d,%edi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
mov    %rbx,%r8
call   1030 <select@plt>
mov    %eax,%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 }
%struct.timeval = type { i32 }
@SIG_SETMASK = global i32 0
@errno = global i32 0
define i32 @pselect(i32 %0, i32* noalias %1, i32* noalias %2, i32* noalias %3, %struct.timespec* noalias %4, i32* noalias %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 %struct.timeval
%16 = alloca %struct.timeval*
%17 = alloca i32
%18 = alloca i32
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
%19 = load %struct.timespec*, %struct.timespec** %12
%20 = icmp ne %struct.timespec* %19, null
br i1 %20, label %21, label %24
21: ; preds = %6
%22 = load %struct.timespec*, %struct.timespec** %12
%23 = call i32 @TIMESPEC_TO_TIMEVAL(%struct.timeval* %15, %struct.timespec* %22)
store %struct.timeval* %15, %struct.timeval** %16
br label %25
24: ; preds = %6
store %struct.timeval* null, %struct.timeval** %16
br label %25
25: ; preds = %24, %21
%26 = load i32*, i32** %13
%27 = icmp ne i32* %26, null
br i1 %27, label %28, label %37
28: ; preds = %25
%29 = load i32, i32* @SIG_SETMASK
%30 = load i32*, i32** %13
%31 = call i32 @pthread_sigmask(i32 %29, i32* %30, i32* %14)
store i32 %31, i32* %17
%32 = load i32, i32* %17
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = load i32, i32* %17
store i32 %35, i32* %7
br label %53
36: ; preds = %28
br label %37
37: ; preds = %36, %25
%38 = load i32, i32* %8
%39 = load i32*, i32** %9
%40 = load i32*, i32** %10
%41 = load i32*, i32** %11
%42 = load %struct.timeval*, %struct.timeval** %16
%43 = call i32 @select(i32 %38, i32* %39, i32* %40, i32* %41, %struct.timeval* %42)
store i32 %43, i32* %17
%44 = load i32*, i32** %13
%45 = icmp ne i32* %44, null
br i1 %45, label %46, label %51
46: ; preds = %37
%47 = load i32, i32* @errno
store i32 %47, i32* %18
%48 = load i32, i32* @SIG_SETMASK
%49 = call i32 @pthread_sigmask(i32 %48, i32* %14, i32* null)
%50 = load i32, i32* %18
store i32 %50, i32* @errno
br label %51
51: ; preds = %46, %37
%52 = load i32, i32* %17
store i32 %52, i32* %7
br label %53
53: ; preds = %51, %34
%54 = load i32, i32* %7
ret i32 %54
}

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