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

/* Variables and functions */
int /*<<< orphan*/ SIG_SETMASK;
int /*<<< orphan*/ pthread_sigmask(int /*<<< orphan*/,
                                   int /*<<< orphan*/ const *,
                                   int /*<<< orphan*/ const *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int pselect(int n, fd_set *readfds, fd_set *writefds, fd_set *errfds,
            const struct timespec *timeout, const sigset_t *sigmask) {
  sigset_t oldmask;
  int result;
  struct timeval tv, *tv_timeout = NULL;
  if (sigmask != NULL)
    pthread_sigmask(SIG_SETMASK, sigmask, &oldmask);
  if (timeout != NULL) {
    tv_timeout = &tv;
    tv.tv_sec = timeout->tv_sec;
    tv.tv_usec = (timeout->tv_nsec + 999) / 1000;
    if (tv.tv_usec >= 1000000) {
      tv.tv_sec += 1;
      tv.tv_usec -= 1000000;
    }
  }
  result = select(n, readfds, writefds, errfds, tv_timeout);
  if (sigmask != NULL)
    pthread_sigmask(SIG_SETMASK, &oldmask, NULL);
  return result;
}


x86 assembly (clang, -O0):
<pselect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%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)
movq   $0x0,-0x48(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1168 <pselect+0x48>
mov    0x2e8f(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   1040 <pthread_sigmask@plt>
cmpq   $0x0,-0x28(%rbp)
je     11c1 <pselect+0xa1>
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%eax
add    $0x3e7,%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x3c(%rbp)
cmpl   $0xf4240,-0x3c(%rbp)
jl     11bc <pselect+0x9c>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
mov    -0x3c(%rbp),%eax
sub    $0xf4240,%eax
mov    %eax,-0x3c(%rbp)
jmp    11c1 <pselect+0xa1>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x48(%rbp),%r8
call   1030 <select@plt>
mov    %eax,-0x38(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11fd <pselect+0xdd>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    -0x34(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <pthread_sigmask@plt>
mov    -0x38(%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,%rbx
mov    %r8,%rbp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%r13d
test   %r9,%r9
je     115b <pselect+0x3b>
mov    0x2e9c(%rip),%rax
mov    (%rax),%edi
lea    0x14(%rsp),%rdx
mov    %rbx,%rsi
call   1040 <pthread_sigmask@plt>
test   %rbp,%rbp
je     11ab <pselect+0x8b>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    0x4(%rbp),%edx
lea    0x3e7(%rdx),%ecx
movslq %ecx,%rcx
imul   $0x10624dd3,%rcx,%rcx
mov    %rcx,%rsi
shr    $0x3f,%rsi
sar    $0x26,%rcx
add    %esi,%ecx
mov    %ecx,0xc(%rsp)
lea    0x8(%rsp),%r8
cmp    $0x3b9ac619,%edx
jl     11ae <pselect+0x8e>
add    $0x1,%eax
mov    %eax,0x8(%rsp)
add    $0xfff0bdc0,%ecx
mov    %ecx,0xc(%rsp)
jmp    11ae <pselect+0x8e>
xor    %r8d,%r8d
mov    %r13d,%edi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1030 <select@plt>
mov    %eax,%ebp
test   %rbx,%rbx
je     11db <pselect+0xbb>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
lea    0x14(%rsp),%rsi
xor    %edx,%edx
call   1040 <pthread_sigmask@plt>
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 %struct.timespec*
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
%15 = alloca %struct.timeval
%16 = alloca %struct.timeval*
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
store %struct.timeval* null, %struct.timeval** %16
%17 = load i32*, i32** %12
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %23
19: ; preds = %6
%20 = load i32, i32* @SIG_SETMASK
%21 = load i32*, i32** %12
%22 = call i32 @pthread_sigmask(i32 %20, i32* %21, i32* %13)
br label %23
23: ; preds = %19, %6
%24 = load %struct.timespec*, %struct.timespec** %11
%25 = icmp ne %struct.timespec* %24, null
br i1 %25, label %26, label %48
26: ; preds = %23
store %struct.timeval* %15, %struct.timeval** %16
%27 = load %struct.timespec*, %struct.timespec** %11
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 0
store i32 %29, i32* %30
%31 = load %struct.timespec*, %struct.timespec** %11
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %31, i32 0, i32 1
%33 = load i32, i32* %32
%34 = add nsw i32 %33, 999
%35 = sdiv i32 %34, 1000
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 1
store i32 %35, i32* %36
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 1
%38 = load i32, i32* %37
%39 = icmp sge i32 %38, 1000000
br i1 %39, label %40, label %47
40: ; preds = %26
%41 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 0
%42 = load i32, i32* %41
%43 = add nsw i32 %42, 1
store i32 %43, i32* %41
%44 = getelementptr inbounds %struct.timeval, %struct.timeval* %15, i32 0, i32 1
%45 = load i32, i32* %44
%46 = sub nsw i32 %45, 1000000
store i32 %46, i32* %44
br label %47
47: ; preds = %40, %26
br label %48
48: ; preds = %47, %23
%49 = load i32, i32* %7
%50 = load i32*, i32** %8
%51 = load i32*, i32** %9
%52 = load i32*, i32** %10
%53 = load %struct.timeval*, %struct.timeval** %16
%54 = call i32 @select(i32 %49, i32* %50, i32* %51, i32* %52, %struct.timeval* %53)
store i32 %54, i32* %14
%55 = load i32*, i32** %12
%56 = icmp ne i32* %55, null
br i1 %56, label %57, label %60
57: ; preds = %48
%58 = load i32, i32* @SIG_SETMASK
%59 = call i32 @pthread_sigmask(i32 %58, i32* %13, i32* null)
br label %60
60: ; preds = %57, %48
%61 = load i32, i32* %14
ret i32 %61
}

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 %15, label %12
12: ; preds = %6
%13 = load i32, i32* @SIG_SETMASK
%14 = call i32 @pthread_sigmask(i32 %13, i32* %5, i32* %7)
br label %15
15: ; preds = %12, %6
%16 = icmp eq %struct.timespec* %4, null
br i1 %16, label %30, label %17
17: ; preds = %15
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 0
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 0
store i32 %19, i32* %20
%21 = getelementptr inbounds %struct.timespec, %struct.timespec* %4, i64 0, i32 1
%22 = load i32, i32* %21
%23 = add nsw i32 %22, 999
%24 = sdiv i32 %23, 1000
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i64 0, i32 1
store i32 %24, i32* %25
%26 = icmp sgt i32 %22, 999999000
br i1 %26, label %27, label %30
27: ; preds = %17
%28 = add nsw i32 %19, 1
store i32 %28, i32* %20
%29 = add nsw i32 %24, -1000000
store i32 %29, i32* %25
br label %30
30: ; preds = %17, %27, %15
%31 = phi %struct.timeval* [ %8, %27 ], [ %8, %17 ], [ null, %15 ]
%32 = call i32 @select(i32 %0, i32* %1, i32* %2, i32* %3, %struct.timeval* %31)
br i1 %11, label %36, label %33
33: ; preds = %30
%34 = load i32, i32* @SIG_SETMASK
%35 = call i32 @pthread_sigmask(i32 %34, i32* %7, i32* null)
br label %36
36: ; preds = %33, %30
ret i32 %32
}