#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 tv_sec;
  long tv_usec;
};
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 timeout;
  fd_set readfds;
  int how;
  FD_ZERO(&readfds);
  FD_SET(0, &readfds);
  timeout.tv_sec = 0L;
  timeout.tv_usec = 0L;
  how = select(1, &readfds, (fd_set *)NULL, (fd_set *)NULL, &timeout);
  if ((how > 0) && FD_ISSET(0, &readfds))
    return 1;
  else
    return 0;
}


x86 assembly (clang, -O0):
<kbhit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x1c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x1c(%rbp),%rsi
call   1060 <FD_SET@plt>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    11b3 <kbhit+0x73>
xor    %edi,%edi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     11b3 <kbhit+0x73>
movl   $0x1,-0x4(%rbp)
jmp    11ba <kbhit+0x7a>
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
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1040 <FD_ZERO@plt>
xor    %edi,%edi
mov    %r14,%rsi
call   1060 <FD_SET@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    $0x1,%ebx
lea    0x10(%rsp),%r8
mov    $0x1,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
test   %eax,%eax
jle    1196 <kbhit+0x56>
lea    0xc(%rsp),%rsi
xor    %edi,%edi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
jne    1198 <kbhit+0x58>
xor    %ebx,%ebx
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 = alloca i32
%5 = call i32 @FD_ZERO(i32* %3)
%6 = call i32 @FD_SET(i32 0, i32* %3)
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
store i64 0, i64* %7
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
store i64 0, i64* %8
%9 = call i32 @select(i32 1, i32* %3, i32* null, i32* null, %struct.timeval* %2)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %0
%13 = call i64 @FD_ISSET(i32 0, i32* %3)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 1, i32* %1
br label %17
16: ; preds = %12, %0
store i32 0, i32* %1
br label %17
17: ; preds = %16, %15
%18 = load i32, i32* %1
ret i32 %18
}

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