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

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int pselect(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
            int /*<<< orphan*/ *, struct timespec *, int /*<<< orphan*/ *);
int watch_input(void) {
  fd_set rfds;
  struct timespec tout;
  int ret;
  tout.tv_sec = 0;
  tout.tv_nsec = 100 * 1000 * 1000;
  ;
  FD_ZERO(&rfds);
  FD_SET(0, &rfds);
  errno = 0;
  ret = pselect(1, &rfds, NULL, NULL, &tout, NULL);
  if (ret < 0) {
    if (errno == EINTR) {
      return -2;
    }
    return -1;
  } else if (ret == 0) {
    return -2;
  }
  return 0;
}


x86 assembly (clang, -O0):
<watch_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x10(%rbp)
movl   $0x5f5e100,-0x18(%rbp)
lea    -0x8(%rbp),%rdi
call   1040 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x8(%rbp),%rsi
call   1050 <FD_SET@plt>
mov    0x2e7e(%rip),%rax
movq   $0x0,(%rax)
mov    $0x1,%edi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r9d
lea    -0x18(%rbp),%r8
mov    %r9,%rdx
mov    %r9,%rcx
call   1030 <pselect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11c5 <watch_input+0x95>
mov    0x2e46(%rip),%rax
mov    (%rax),%rax
mov    0x2e34(%rip),%rcx
cmp    (%rcx),%rax
jne    11b9 <watch_input+0x89>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11e7 <watch_input+0xb7>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e7 <watch_input+0xb7>
cmpl   $0x0,-0x1c(%rbp)
jne    11db <watch_input+0xab>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11e7 <watch_input+0xb7>
jmp    11e0 <watch_input+0xb0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<watch_input>:
push   %r14
push   %rbx
sub    $0x18,%rsp
movq   $0x0,0x10(%rsp)
movl   $0x5f5e100,0x8(%rsp)
05
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1050 <FD_SET@plt>
mov    0x2e7a(%rip),%r14
movq   $0x0,(%r14)
lea    0x8(%rsp),%r8
mov    $0x1,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r9d,%r9d
call   1030 <pselect@plt>
test   %eax,%eax
js     1197 <watch_input+0x67>
setne  %al
movzbl %al,%eax
add    %eax,%eax
add    $0xfffffffe,%eax
jmp    11ab <watch_input+0x7b>
mov    (%r14),%rcx
mov    0x2e37(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
sete   %al
not    %eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @watch_input() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.timespec
%4 = alloca i32
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
store i64 0, i64* %5
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 100000000, i32* %6
%7 = call i32 @FD_ZERO(i32* %2)
%8 = call i32 @FD_SET(i32 0, i32* %2)
store i64 0, i64* @errno
%9 = call i32 @pselect(i32 1, i32* %2, i32* null, i32* null, %struct.timespec* %3, i32* null)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %0
%13 = load i64, i64* @errno
%14 = load i64, i64* @EINTR
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 -2, i32* %1
br label %24
17: ; preds = %12
store i32 -1, i32* %1
br label %24
18: ; preds = %0
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %18
store i32 -2, i32* %1
br label %24
22: ; preds = %18
br label %23
23: ; preds = %22
store i32 0, i32* %1
br label %24
24: ; preds = %23, %21, %17, %16
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
%struct.timespec = type { i32, i64 }
@errno = global i64 0
@EINTR = global i64 0
define i32 @watch_input() {
%1 = alloca i32
%2 = alloca %struct.timespec
%3 = bitcast i32* %1 to i8*
%4 = bitcast %struct.timespec* %2 to i8*
%5 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
store i64 0, i64* %5
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
store i32 100000000, i32* %6
%7 = call i32 @FD_ZERO(i32* %1)
%8 = call i32 @FD_SET(i32 0, i32* %1)
store i64 0, i64* @errno
%9 = call i32 @pselect(i32 1, i32* %1, i32* null, i32* null, %struct.timespec* %2, i32* null)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %0
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
%15 = select i1 %14, i32 -2, i32 -1
br label %19
16: ; preds = %0
%17 = icmp eq i32 %9, 0
%18 = select i1 %17, i32 -2, i32 0
br label %19
19: ; preds = %16, %11
%20 = phi i32 [ %15, %11 ], [ %18, %16 ]
ret i32 %20
}