#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 */
scalar_t__ FD_ISSET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int kbhit(void) {
  struct timeval tv;
  fd_set read_fd;
  tv.tv_sec = 0;
  tv.tv_usec = 0;
  FD_ZERO(&read_fd);
  FD_SET(0, &read_fd);
  if (select(1, &read_fd, NULL, NULL, &tv) == -1)
    return 0;
  if (FD_ISSET(0, &read_fd))
    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   1040 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x1c(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
cmp    $0xffffffff,%eax
jne    119a <kbhit+0x5a>
movl   $0x0,-0x4(%rbp)
jmp    11c2 <kbhit+0x82>
xor    %edi,%edi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     11bb <kbhit+0x7b>
movl   $0x1,-0x4(%rbp)
jmp    11c2 <kbhit+0x82>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kbhit>:
push   %r14
push   %rbx
sub    $0x28,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1040 <FD_ZERO@plt>
xor    %ebx,%ebx
xor    %edi,%edi
mov    %r14,%rsi
call   1060 <FD_SET@plt>
lea    0x10(%rsp),%r8
mov    $0x1,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
cmp    $0xffffffff,%eax
je     1197 <kbhit+0x57>
lea    0xc(%rsp),%rsi
xor    %edi,%edi
call   1030 <FD_ISSET@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define i32 @kbhit() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
store i64 0, i64* %4
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
store i64 0, i64* %5
%6 = call i32 @FD_ZERO(i32* %3)
%7 = call i32 @FD_SET(i32 0, i32* %3)
%8 = call i32 @select(i32 1, i32* %3, i32* null, i32* null, %struct.timeval* %2)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %11
10: ; preds = %0
store i32 0, i32* %1
br label %16
11: ; preds = %0
%12 = call i64 @FD_ISSET(i32 0, i32* %3)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %11
store i32 1, i32* %1
br label %16
15: ; preds = %11
store i32 0, i32* %1
br label %16
16: ; preds = %15, %14, %10
%17 = load i32, i32* %1
ret i32 %17
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
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 = call i32 @FD_SET(i32 0, i32* %2)
%8 = call i32 @select(i32 1, i32* %2, i32* null, i32* null, %struct.timeval* %1)
%9 = icmp eq i32 %8, -1
br i1 %9, label %14, label %10
10: ; preds = %0
%11 = call i64 @FD_ISSET(i32 0, i32* %2)
%12 = icmp ne i64 %11, 0
%13 = zext i1 %12 to i32
br label %14
14: ; preds = %10, %0
%15 = phi i32 [ 0, %0 ], [ %13, %10 ]
ret i32 %15
}