#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 */
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*/ *);
int /*<<< orphan*/ STDIN_FILENO;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int getchar();
int /*<<< orphan*/ perror(char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int getch() {
  fd_set rfds;
  int retcode;
  FD_ZERO(&rfds);
  FD_SET(STDIN_FILENO, &rfds);
  retcode = select(1, &rfds, NULL, NULL, NULL);
  if (retcode == -1 && errno == EINTR) {
    return 0;
  } else if (retcode < 0) {
    perror("select: ");
    exit(-1);
  }
  return getchar();
}


x86 assembly (clang, -O0):
<getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    -0x8(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1080 <FD_SET@plt>
mov    $0x1,%edi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r8d
mov    %r8,%rdx
mov    %r8,%rcx
call   1050 <select@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11cf <getch+0x6f>
mov    0x2e30(%rip),%rax
mov    (%rax),%rax
mov    0x2e1e(%rip),%rcx
cmp    (%rcx),%rax
jne    11cf <getch+0x6f>
movl   $0x0,-0x4(%rbp)
jmp    11fe <getch+0x9e>
cmpl   $0x0,-0xc(%rbp)
jge    11ef <getch+0x8f>
lea    0xe20(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    11f4 <getch+0x94>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %r14
push   %rbx
push   %rax
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1040 <FD_ZERO@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
mov    %r14,%rsi
call   1080 <FD_SET@plt>
xor    %ebx,%ebx
mov    $0x1,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   1050 <select@plt>
cmp    $0xffffffff,%eax
je     11ac <getch+0x4c>
test   %eax,%eax
js     11cc <getch+0x6c>
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ebx
jmp    11c2 <getch+0x62>
mov    0x2e2d(%rip),%rax
mov    (%rax),%rax
mov    0x2e1b(%rip),%rcx
cmp    (%rcx),%rax
jne    11cc <getch+0x6c>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe2d(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [9 x i8] c"select: \00"
define i32 @getch() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = call i32 @FD_ZERO(i32* %2)
%5 = load i32, i32* @STDIN_FILENO
%6 = call i32 @FD_SET(i32 %5, i32* %2)
%7 = call i32 @select(i32 1, i32* %2, i32* null, i32* null, i32* null)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %15
10: ; preds = %0
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %15
14: ; preds = %10
store i32 0, i32* %1
br label %24
15: ; preds = %10, %0
%16 = load i32, i32* %3
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %15
br label %22
22: ; preds = %21
%23 = call i32 (...) @getchar()
store i32 %23, i32* %1
br label %24
24: ; preds = %22, %14
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [9 x i8] c"select: \00"
define i32 @getch() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = call i32 @FD_ZERO(i32* %1)
%4 = load i32, i32* @STDIN_FILENO
%5 = call i32 @FD_SET(i32 %4, i32* %1)
%6 = call i32 @select(i32 1, i32* %1, i32* null, i32* null, i32* null)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = load i64, i64* @errno
%10 = load i64, i64* @EINTR
%11 = icmp eq i64 %9, %10
br i1 %11, label %19, label %14
12: ; preds = %0
%13 = icmp slt i32 %6, 0
br i1 %13, label %14, label %17
14: ; preds = %8, %12
%15 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %12
%18 = call i32 (...) @getchar()
br label %19
19: ; preds = %8, %17
%20 = phi i32 [ %18, %17 ], [ 0, %8 ]
ret i32 %20
}