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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int read(int /*<<< orphan*/, char *, int);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
bool getch_poll(char *ch) {
  struct timeval tv = {0L, 0L};
  fd_set fds;
  FD_SET(0, &fds);
  if (select(1, &fds, NULL, NULL, &tv)) {
    if (1 == read(0, ch, 1)) {
      if (*ch == 0x03)
        exit(0);
      return true;
    }
  }
  return false;
}


x86 assembly (clang, -O0):
<getch_poll>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
xor    %edi,%edi
lea    -0x24(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    $0x1,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
cmp    $0x0,%rax
je     11e1 <getch_poll+0x91>
mov    -0x10(%rbp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    11dc <getch_poll+0x8c>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3,%eax
jne    11d0 <getch_poll+0x80>
xor    %edi,%edi
call   1060 <exit@plt>
movl   $0x1,-0x4(%rbp)
jmp    11e8 <getch_poll+0x98>
jmp    11e1 <getch_poll+0x91>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch_poll>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r14
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
xor    %ebx,%ebx
lea    0xc(%rsp),%r15
xor    %edi,%edi
mov    %r15,%rsi
call   1060 <FD_SET@plt>
lea    0x10(%rsp),%r8
mov    $0x1,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <select@plt>
test   %rax,%rax
je     11a1 <getch_poll+0x61>
xor    %ebx,%ebx
xor    %edi,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    11a1 <getch_poll+0x61>
cmpb   $0x3,(%r14)
mov    $0x1,%ebx
je     11ad <getch_poll+0x6d>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define i32 @getch_poll(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.timeval
%5 = alloca i32
store i8* %0, i8** %3
%6 = bitcast %struct.timeval* %4 to i8*
%7 = call i32 @FD_SET(i32 0, i32* %5)
%8 = call i64 @select(i32 1, i32* %5, i32* null, i32* null, %struct.timeval* %4)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %23
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 @read(i32 0, i8* %11, i32 1)
%13 = icmp eq i32 1, %12
br i1 %13, label %14, label %22
14: ; preds = %10
%15 = load i8*, i8** %3
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 3
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %14
store i32 1, i32* %2
br label %24
22: ; preds = %10
br label %23
23: ; preds = %22, %1
store i32 0, i32* %2
br label %24
24: ; preds = %23, %21
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i32 @getch_poll(i8* nocapture %0) {
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = bitcast %struct.timeval* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = call i32 @FD_SET(i32 0, i32* %3)
%7 = call i64 @select(i32 1, i32* %3, i32* null, i32* null, %struct.timeval* %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %17, label %9
9: ; preds = %1
%10 = call i32 @read(i32 0, i8* %0, i32 1)
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %17
12: ; preds = %9
%13 = load i8, i8* %0
%14 = icmp eq i8 %13, 3
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %1, %9, %12
%18 = phi i32 [ 1, %12 ], [ 0, %9 ], [ 0, %1 ]
ret i32 %18
}