#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 select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ sigprocmask(int /*<<< orphan*/, int /*<<< orphan*/ const *,
                               int /*<<< orphan*/ const *);
int __libc_pselect(int nfds, fd_set *readfds, fd_set *writefds,
                   fd_set *exceptfds, const struct timespec *timeout,
                   const sigset_t *sigmask) {
  struct timeval tval;
  int retval;
  sigset_t savemask;
  if (timeout != NULL)
    TIMESPEC_TO_TIMEVAL(&tval, timeout);
  if (sigmask != NULL)
    sigprocmask(SIG_SETMASK, sigmask, &savemask);
  retval = select(nfds, readfds, writefds, exceptfds,
                  timeout != NULL ? &tval : NULL);
  if (sigmask != NULL)
    sigprocmask(SIG_SETMASK, &savemask, NULL);
  return retval;
}


x86 assembly (clang, -O0):
<__libc_pselect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%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     1167 <__libc_pselect+0x37>
mov    -0x28(%rbp),%rsi
lea    -0x38(%rbp),%rdi
call   1050 <TIMESPEC_TO_TIMEVAL@plt>
cmpq   $0x0,-0x30(%rbp)
je     1188 <__libc_pselect+0x58>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1030 <sigprocmask@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11be <__libc_pselect+0x8e>
lea    -0x38(%rbp),%rax
mov    %rax,-0x68(%rbp)
jmp    11c9 <__libc_pselect+0x99>
xor    %eax,%eax
mov    %rax,-0x68(%rbp)
jmp    11c9 <__libc_pselect+0x99>
mov    -0x48(%rbp),%rcx
mov    -0x50(%rbp),%rdx
mov    -0x58(%rbp),%rsi
mov    -0x5c(%rbp),%edi
mov    -0x68(%rbp),%r8
call   1040 <select@plt>
mov    %eax,-0x3c(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1205 <__libc_pselect+0xd5>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1030 <sigprocmask@plt>
mov    -0x3c(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__libc_pselect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%rbp
mov    %r8,%rbx
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%r13d
test   %r8,%r8
je     115d <__libc_pselect+0x2d>
mov    %rsp,%rdi
mov    %rbx,%rsi
call   1050 <TIMESPEC_TO_TIMEVAL@plt>
test   %rbp,%rbp
je     11ad <__libc_pselect+0x7d>
mov    0x2e7f(%rip),%rax
mov    (%rax),%edi
lea    0x4(%rsp),%rdx
mov    %rbp,%rsi
mov    %rdx,%rbp
call   1030 <sigprocmask@plt>
test   %rbx,%rbx
mov    %rsp,%r8
cmove  %rbx,%r8
mov    %r13d,%edi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <select@plt>
mov    %eax,%ebx
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    %rbp,%rsi
xor    %edx,%edx
call   1030 <sigprocmask@plt>
jmp    11ca <__libc_pselect+0x9a>
test   %rbx,%rbx
mov    %rsp,%r8
cmove  %rbx,%r8
mov    %r13d,%edi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <select@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x8,%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
define i32 @__libc_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 %struct.timespec*
%12 = alloca i32*
%13 = alloca %struct.timeval
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %7
store i32* %1, i32** %8
store i32* %2, i32** %9
store i32* %3, i32** %10
store %struct.timespec* %4, %struct.timespec** %11
store i32* %5, i32** %12
%16 = load %struct.timespec*, %struct.timespec** %11
%17 = icmp ne %struct.timespec* %16, null
br i1 %17, label %18, label %21
18: ; preds = %6
%19 = load %struct.timespec*, %struct.timespec** %11
%20 = call i32 @TIMESPEC_TO_TIMEVAL(%struct.timeval* %13, %struct.timespec* %19)
br label %21
21: ; preds = %18, %6
%22 = load i32*, i32** %12
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = load i32, i32* @SIG_SETMASK
%26 = load i32*, i32** %12
%27 = call i32 @sigprocmask(i32 %25, i32* %26, i32* %15)
br label %28
28: ; preds = %24, %21
%29 = load i32, i32* %7
%30 = load i32*, i32** %8
%31 = load i32*, i32** %9
%32 = load i32*, i32** %10
%33 = load %struct.timespec*, %struct.timespec** %11
%34 = icmp ne %struct.timespec* %33, null
br i1 %34, label %35, label %36
35: ; preds = %28
br label %37
36: ; preds = %28
br label %37
37: ; preds = %36, %35
%38 = phi %struct.timeval* [ %13, %35 ], [ null, %36 ]
%39 = call i32 @select(i32 %29, i32* %30, i32* %31, i32* %32, %struct.timeval* %38)
store i32 %39, i32* %14
%40 = load i32*, i32** %12
%41 = icmp ne i32* %40, null
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = load i32, i32* @SIG_SETMASK
%44 = call i32 @sigprocmask(i32 %43, i32* %15, i32* null)
br label %45
45: ; preds = %42, %37
%46 = load i32, i32* %14
ret i32 %46
}

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