#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 /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ raw1();
int select(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int kbhit(void) {
  struct timeval timeout;
  fd_set read_handles;
  int status;
  raw1();
  FD_ZERO(&read_handles);
  FD_SET(0, &read_handles);
  timeout.tv_sec = timeout.tv_usec = 0;
  status = select(0 + 1, &read_handles, NULL, NULL, &timeout);
  if (status < 0) {
    printf("select() failed in kbhit()\n");
    exit(1);
  }
  return status;
}


x86 assembly (clang, -O0):
<kbhit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1030 <raw1@plt>
lea    -0x14(%rbp),%rdi
call   1050 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x14(%rbp),%rsi
call   1080 <FD_SET@plt>
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x8(%rbp)
mov    $0x1,%edi
lea    -0x14(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x10(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11cf <kbhit+0x6f>
lea    0xe40(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kbhit>:
push   %rbx
sub    $0x20,%rsp
xor    %eax,%eax
call   1040 <raw1@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1080 <FD_SET@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
lea    0x10(%rsp),%r8
mov    $0x1,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
test   %eax,%eax
js     11ab <kbhit+0x4b>
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe4e(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@.str = [28 x i8] c"select() failed in kbhit()\0A\00"
define i32 @kbhit() {
%1 = alloca %struct.timeval
%2 = alloca i32
%3 = alloca i32
%4 = call i32 (...) @raw1()
%5 = call i32 @FD_ZERO(i32* %2)
%6 = call i32 @FD_SET(i32 0, i32* %2)
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 0
store i64 0, i64* %7
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i32 0, i32 1
store i64 0, i64* %8
%9 = call i32 @select(i64 1, i32* %2, i32* null, i32* null, %struct.timeval* %1)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %0
%13 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = load i32, i32* %3
ret i32 %16
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@str = [27 x i8] c"select() failed in kbhit()\00"
define i32 @kbhit() {
%1 = alloca %struct.timeval
%2 = alloca i32
%3 = bitcast %struct.timeval* %1 to i8*
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 (...) @raw1()
%6 = call i32 @FD_ZERO(i32* %2)
%7 = call i32 @FD_SET(i32 0, i32* %2)
%8 = bitcast %struct.timeval* %1 to i8*
%9 = call i32 @select(i64 1, i32* %2, i32* null, i32* null, %struct.timeval* %1)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
ret i32 %9
}