#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 pollfd {
  int events;
  scalar_t__ revents;
  int /*<<< orphan*/ fd;
};

/* Variables and functions */
int POLLIN;
int POLLPRI;
int POLLRDBAND;
int POLLRDNORM;
int /*<<< orphan*/ STDIN_FILENO;
scalar_t__ poll(struct pollfd *, int, int);
scalar_t__ read(int /*<<< orphan*/, int *, int);
int stdin_getch(int timeout) {
  struct pollfd polls;
  polls.fd = STDIN_FILENO;
  polls.events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI;
  polls.revents = 0;
  if (poll(&polls, 1, timeout) < 0)
    return -1;
  int ch = 0;
  if (polls.revents) {
    if (read(STDIN_FILENO, &ch, 1) < 0)
      return -1;
  }
  return ch;
}


x86 assembly (clang, -O0):
<stdin_getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%eax
mov    0x2e99(%rip),%rcx
or     (%rcx),%eax
mov    0x2e78(%rip),%rcx
or     (%rcx),%eax
mov    0x2e97(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edx
lea    -0x20(%rbp),%rdi
mov    $0x1,%esi
call   1040 <poll@plt>
cmp    $0x0,%rax
jge    118d <stdin_getch+0x6d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <stdin_getch+0xb7>
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d1 <stdin_getch+0xb1>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
lea    -0x24(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x0,%rax
jge    11cc <stdin_getch+0xac>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <stdin_getch+0xb7>
jmp    11d1 <stdin_getch+0xb1>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stdin_getch>:
push   %rbx
sub    $0x20,%rsp
mov    %edi,%edx
mov    0x2e92(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    0x2e9d(%rip),%rax
mov    0x2e9e(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e7b(%rip),%rax
or     (%rax),%ecx
mov    0x2e9a(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
call   1040 <poll@plt>
test   %rax,%rax
js     11ad <stdin_getch+0x8d>
movl   $0x0,0x4(%rsp)
cmpq   $0x0,0x10(%rsp)
je     11b4 <stdin_getch+0x94>
mov    (%rbx),%edi
lea    0x4(%rsp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
js     11b6 <stdin_getch+0x96>
mov    0x4(%rsp),%eax
jmp    11b6 <stdin_getch+0x96>
mov    $0xffffffff,%eax
jmp    11b6 <stdin_getch+0x96>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.pollfd = type { i32, i64, i32 }
@STDIN_FILENO = global i32 0
@POLLIN = global i32 0
@POLLRDNORM = global i32 0
@POLLRDBAND = global i32 0
@POLLPRI = global i32 0
define i32 @stdin_getch(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.pollfd
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* @STDIN_FILENO
%7 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 2
store i32 %6, i32* %7
%8 = load i32, i32* @POLLIN
%9 = load i32, i32* @POLLRDNORM
%10 = or i32 %8, %9
%11 = load i32, i32* @POLLRDBAND
%12 = or i32 %10, %11
%13 = load i32, i32* @POLLPRI
%14 = or i32 %12, %13
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 0
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 1
store i64 0, i64* %16
%17 = load i32, i32* %3
%18 = call i64 @poll(%struct.pollfd* %4, i32 1, i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %1
store i32 -1, i32* %2
br label %33
21: ; preds = %1
store i32 0, i32* %5
%22 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %4, i32 0, i32 1
%23 = load i64, i64* %22
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %21
%26 = load i32, i32* @STDIN_FILENO
%27 = call i64 @read(i32 %26, i32* %5, i32 1)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 -1, i32* %2
br label %33
30: ; preds = %25
br label %31
31: ; preds = %30, %21
%32 = load i32, i32* %5
store i32 %32, i32* %2
br label %33
33: ; preds = %31, %29, %20
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
%struct.pollfd = type { i32, i64, i32 }
@STDIN_FILENO = global i32 0
@POLLIN = global i32 0
@POLLRDNORM = global i32 0
@POLLRDBAND = global i32 0
@POLLPRI = global i32 0
define i32 @stdin_getch(i32 %0) {
%2 = alloca %struct.pollfd
%3 = alloca i32
%4 = bitcast %struct.pollfd* %2 to i8*
%5 = load i32, i32* @STDIN_FILENO
%6 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 2
store i32 %5, i32* %6
%7 = load i32, i32* @POLLIN
%8 = load i32, i32* @POLLRDNORM
%9 = or i32 %8, %7
%10 = load i32, i32* @POLLRDBAND
%11 = or i32 %9, %10
%12 = load i32, i32* @POLLPRI
%13 = or i32 %11, %12
%14 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 0
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %2, i64 0, i32 1
store i64 0, i64* %15
%16 = call i64 @poll(%struct.pollfd* %2, i32 1, i32 %0)
%17 = icmp slt i64 %16, 0
br i1 %17, label %30, label %18
18: ; preds = %1
%19 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%20 = load i64, i64* %15
%21 = icmp eq i64 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %18
%23 = load i32, i32* @STDIN_FILENO
%24 = call i64 @read(i32 %23, i32* %3, i32 1)
%25 = icmp slt i64 %24, 0
%26 = load i32, i32* %3
%27 = select i1 %25, i32 -1, i32 %26
br label %28
28: ; preds = %22, %18
%29 = phi i32 [ 0, %18 ], [ %27, %22 ]
br label %30
30: ; preds = %1, %28
%31 = phi i32 [ %29, %28 ], [ -1, %1 ]
ret i32 %31
}