#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 {
  long tv_nsec;
  int /*<<< orphan*/ tv_sec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ errno;
int pselect(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
            int /*<<< orphan*/ *, struct timespec *, int /*<<< orphan*/ *);
int select(int nfds, fd_set *restrict readfds, fd_set *restrict writefds,
           fd_set *restrict errorfds, struct timeval *restrict timeout) {
  if (timeout != NULL) {
    if (timeout->tv_usec < 0 || timeout->tv_usec >= 1000000) {
      errno = EINVAL;
      return -1;
    }
    struct timespec ts = {.tv_sec = timeout->tv_sec,
                          .tv_nsec = (long)timeout->tv_usec * 1000};
    return pselect(nfds, readfds, writefds, errorfds, &ts, NULL);
  } else {
    return pselect(nfds, readfds, writefds, errorfds, NULL, NULL);
  }
}


x86 assembly (clang, -O0):
<select>:
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)
cmpq   $0x0,-0x28(%rbp)
je     11b2 <select+0xa2>
mov    -0x28(%rbp),%rax
cmpl   $0x0,(%rax)
jl     1153 <select+0x43>
mov    -0x28(%rbp),%rax
cmpl   $0xf4240,(%rax)
jl     1171 <select+0x61>
mov    0x2e7e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e7d(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11d1 <select+0xc1>
mov    -0x28(%rbp),%rax
movslq (%rax),%rax
imul   $0x3e8,%rax,%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    -0x38(%rbp),%r8
xor    %eax,%eax
mov    %eax,%r9d
call   1030 <pselect@plt>
mov    %eax,-0x4(%rbp)
jmp    11d1 <select+0xc1>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1030 <pselect@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<select>:
test   %r8,%r8
je     1140 <select+0x30>
sub    $0x18,%rsp
mov    (%r8),%eax
cmp    $0xf4240,%rax
jb     114b <select+0x3b>
mov    0x2ead(%rip),%rax
mov    (%rax),%eax
mov    0x2eac(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x18,%rsp
ret
xor    %r8d,%r8d
xor    %r9d,%r9d
jmp    1030 <pselect@plt>
imul   $0x3e8,%rax,%rax
mov    %rax,0x8(%rsp)
mov    0x4(%r8),%eax
mov    %eax,0x10(%rsp)
lea    0x8(%rsp),%r8
xor    %r9d,%r9d
call   1030 <pselect@plt>
add    $0x18,%rsp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.timespec = type { i64, i32 }
@EINVAL = global i32 0
@errno = global i32 0
define i32 @select(i32 %0, i32* noalias %1, i32* noalias %2, i32* noalias %3, %struct.timeval* noalias %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca %struct.timeval*
%12 = alloca %struct.timespec
store i32 %0, i32* %7
store i32* %1, i32** %8
store i32* %2, i32** %9
store i32* %3, i32** %10
store %struct.timeval* %4, %struct.timeval** %11
%13 = load %struct.timeval*, %struct.timeval** %11
%14 = icmp ne %struct.timeval* %13, null
br i1 %14, label %15, label %43
15: ; preds = %5
%16 = load %struct.timeval*, %struct.timeval** %11
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = icmp slt i32 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = load %struct.timeval*, %struct.timeval** %11
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = icmp sge i32 %23, 1000000
br i1 %24, label %25, label %27
25: ; preds = %20, %15
%26 = load i32, i32* @EINVAL
store i32 %26, i32* @errno
store i32 -1, i32* %6
br label %49
27: ; preds = %20
%28 = getelementptr inbounds %struct.timespec, %struct.timespec* %12, i32 0, i32 0
%29 = load %struct.timeval*, %struct.timeval** %11
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = sext i32 %31 to i64
%33 = mul nsw i64 %32, 1000
store i64 %33, i64* %28
%34 = getelementptr inbounds %struct.timespec, %struct.timespec* %12, i32 0, i32 1
%35 = load %struct.timeval*, %struct.timeval** %11
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %35, i32 0, i32 1
%37 = load i32, i32* %36
store i32 %37, i32* %34
%38 = load i32, i32* %7
%39 = load i32*, i32** %8
%40 = load i32*, i32** %9
%41 = load i32*, i32** %10
%42 = call i32 @pselect(i32 %38, i32* %39, i32* %40, i32* %41, %struct.timespec* %12, i32* null)
store i32 %42, i32* %6
br label %49
43: ; preds = %5
%44 = load i32, i32* %7
%45 = load i32*, i32** %8
%46 = load i32*, i32** %9
%47 = load i32*, i32** %10
%48 = call i32 @pselect(i32 %44, i32* %45, i32* %46, i32* %47, %struct.timespec* null, i32* null)
store i32 %48, i32* %6
br label %49
49: ; preds = %43, %27, %25
%50 = load i32, i32* %6
ret i32 %50
}

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