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

/* Variables and functions */
scalar_t__ FD_ISSET(scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ STDIN_FILENO;
int read(scalar_t__, char *, int);
scalar_t__ select(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int kbhit() {
  static char buf[4096];
  struct timeval tv;
  fd_set rfds;
  tv.tv_sec = 0;
  tv.tv_usec = 0;
  FD_ZERO(&rfds);
  FD_SET(STDIN_FILENO, &rfds);
  if (select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tv) > 0) {
    if (FD_ISSET(STDIN_FILENO, &rfds)) {
      int n = read(STDIN_FILENO, buf, sizeof(buf));
      return 1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<kbhit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x1c(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    0x2e68(%rip),%rax
mov    (%rax),%rdi
lea    -0x1c(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
cmp    $0x0,%rax
jle    11fc <kbhit+0xac>
mov    0x2e29(%rip),%rax
mov    (%rax),%rdi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     11f7 <kbhit+0xa7>
mov    0x2e0c(%rip),%rax
mov    (%rax),%rdi
lea    0x2e82(%rip),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
movl   $0x1,-0x4(%rbp)
jmp    1203 <kbhit+0xb3>
jmp    11fc <kbhit+0xac>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kbhit>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
mov    0x2e6b(%rip),%r15
mov    (%r15),%rdi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
mov    (%r15),%rdi
add    $0x1,%rdi
xor    %r14d,%r14d
lea    0x10(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
test   %rax,%rax
jle    11cc <kbhit+0x7c>
mov    (%r15),%rdi
lea    0xc(%rsp),%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     11cc <kbhit+0x7c>
mov    (%r15),%rdi
lea    0x2ea4(%rip),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@kbhit.buf = internal global [4096 x i8] zeroinitializer
@STDIN_FILENO = global i64 0
define i32 @kbhit() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = alloca i32
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
store i64 0, i64* %5
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
store i64 0, i64* %6
%7 = call i32 @FD_ZERO(i32* %3)
%8 = load i64, i64* @STDIN_FILENO
%9 = call i32 @FD_SET(i64 %8, i32* %3)
%10 = load i64, i64* @STDIN_FILENO
%11 = add nsw i64 %10, 1
%12 = call i64 @select(i64 %11, i32* %3, i32* null, i32* null, %struct.timeval* %2)
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %0
%15 = load i64, i64* @STDIN_FILENO
%16 = call i64 @FD_ISSET(i64 %15, i32* %3)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i64, i64* @STDIN_FILENO
%20 = call i32 @read(i64 %19, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @kbhit.buf, i64 0, i64 0), i32 4096)
store i32 %20, i32* %4
store i32 1, i32* %1
br label %23
21: ; preds = %14
br label %22
22: ; preds = %21, %0
store i32 0, i32* %1
br label %23
23: ; preds = %22, %18
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@kbhit.buf = internal global [4096 x i8] zeroinitializer
@STDIN_FILENO = global i64 0
define i32 @kbhit() {
%1 = alloca %struct.timeval
%2 = alloca i32
%3 = bitcast %struct.timeval* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = bitcast %struct.timeval* %1 to i8*
%6 = call i32 @FD_ZERO(i32* %2)
%7 = load i64, i64* @STDIN_FILENO
%8 = call i32 @FD_SET(i64 %7, i32* %2)
%9 = load i64, i64* @STDIN_FILENO
%10 = add nsw i64 %9, 1
%11 = call i64 @select(i64 %10, i32* %2, i32* null, i32* null, %struct.timeval* %1)
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %20
13: ; preds = %0
%14 = load i64, i64* @STDIN_FILENO
%15 = call i64 @FD_ISSET(i64 %14, i32* %2)
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %13
%18 = load i64, i64* @STDIN_FILENO
%19 = call i32 @read(i64 %18, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @kbhit.buf, i64 0, i64 0), i32 4096)
br label %20
20: ; preds = %0, %13, %17
%21 = phi i32 [ 1, %17 ], [ 0, %13 ], [ 0, %0 ]
ret i32 %21
}