#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*/ perror(char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int kbhit(void) {
  fd_set rfds;
  struct timeval tv;
  int retval;
  FD_ZERO(&rfds);
  FD_SET(0, &rfds);
  tv.tv_sec = 0;
  tv.tv_usec = 0;
  retval = select(1, &rfds, NULL, NULL, &tv);
  if (retval == -1) {
    perror("select()");
    return 0;
  } else if (retval)
    return 1;
  else
    return 0;
  return 0;
}


x86 assembly (clang, -O0):
<kbhit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x8(%rbp),%rdi
call   1030 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x8(%rbp),%rsi
call   1060 <FD_SET@plt>
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x1,%edi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1040 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11aa <kbhit+0x6a>
lea    0xe67(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11c7 <kbhit+0x87>
cmpl   $0x0,-0x1c(%rbp)
je     11c0 <kbhit+0x80>
movl   $0x1,-0x4(%rbp)
jmp    11c7 <kbhit+0x87>
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   1030 <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   1040 <select@plt>
test   %eax,%eax
je     1196 <kbhit+0x56>
cmp    $0xffffffff,%eax
jne    1198 <kbhit+0x58>
lea    0xe6f(%rip),%rdi
call   1050 <perror@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@.str = [9 x i8] c"select()\00"
define i32 @kbhit() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = bitcast i32* %1 to i8*
%4 = bitcast %struct.timeval* %2 to i8*
%5 = call i32 @FD_ZERO(i32* %1)
%6 = call i32 @FD_SET(i32 0, i32* %1)
%7 = bitcast %struct.timeval* %2 to i8*
%8 = call i32 @select(i32 1, i32* %1, i32* null, i32* null, %struct.timeval* %2)
switch i32 %8, label %12 [
i32 -1, label %9
i32 0, label %11
]
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %12
11: ; preds = %0
br label %12
12: ; preds = %0, %11, %9
%13 = phi i32 [ 0, %9 ], [ 0, %11 ], [ 1, %0 ]
ret i32 %13
}