#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;

/* Variables and functions */
int 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 /*<<< orphan*/ changemode(int);
int /*<<< orphan*/ select(scalar_t__, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          struct timeval *);
int _kbhit(void) {
  changemode(1);
  struct timeval tv;
  fd_set rdfs;
  tv.tv_sec = 0;
  tv.tv_usec = 0;
  FD_ZERO(&rdfs);
  FD_SET(STDIN_FILENO, &rdfs);
  select(STDIN_FILENO + 1, &rdfs, NULL, NULL, &tv);
  return FD_ISSET(STDIN_FILENO, &rdfs);
}


x86 assembly (clang, -O0):
<_kbhit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x1,%edi
call   1040 <changemode@plt>
movq   $0x0,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
lea    -0x14(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%rdi
lea    -0x14(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x10(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%rdi
lea    -0x14(%rbp),%rsi
call   1030 <FD_ISSET@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_kbhit>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    $0x1,%edi
call   1040 <changemode@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
mov    0x2e63(%rip),%r14
mov    (%r14),%rdi
mov    %rbx,%rsi
call   1070 <FD_SET@plt>
mov    (%r14),%rdi
add    $0x1,%rdi
lea    0x10(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
mov    (%r14),%rdi
mov    %rbx,%rsi
call   1030 <FD_ISSET@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@STDIN_FILENO = global i64 0
define i32 @_kbhit() {
%1 = alloca %struct.timeval
%2 = alloca i32
%3 = call i32 @changemode(i32 1)
%4 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 1
store i64 0, i64* %4
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 0
store i64 0, i64* %5
%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 i32 @select(i64 %10, i32* %2, i32* null, i32* null, %struct.timeval* %1)
%12 = load i64, i64* @STDIN_FILENO
%13 = call i32 @FD_ISSET(i64 %12, i32* %2)
ret i32 %13
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@STDIN_FILENO = global i64 0
define i32 @_kbhit() {
%1 = alloca %struct.timeval
%2 = alloca i32
%3 = tail call i32 @changemode(i32 1)
%4 = bitcast %struct.timeval* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = bitcast %struct.timeval* %1 to i8*
%7 = call i32 @FD_ZERO(i32* %2)
%8 = load i64, i64* @STDIN_FILENO
%9 = call i32 @FD_SET(i64 %8, i32* %2)
%10 = load i64, i64* @STDIN_FILENO
%11 = add nsw i64 %10, 1
%12 = call i32 @select(i64 %11, i32* %2, i32* null, i32* null, %struct.timeval* %1)
%13 = load i64, i64* @STDIN_FILENO
%14 = call i32 @FD_ISSET(i64 %13, i32* %2)
ret i32 %14
}