#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;
};
struct termios {
  int c_lflag;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int ECHO;
scalar_t__ FD_ISSET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int ICANON;
int /*<<< orphan*/ TCSANOW;
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/,
           struct timeval *);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int _kbhit() {
  struct termios oldt, newt;
  struct timeval tv;
  fd_set rfd;
  tv.tv_sec = 0;
  tv.tv_usec = 0;
  FD_ZERO(&rfd);
  FD_SET(0, &rfd);
  tcgetattr(0, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(0, TCSANOW, &newt);
  if (select(1, &rfd, 0, 0, &tv) == -1)
    return 0;
  tcsetattr(0, TCSANOW, &oldt);
  return (FD_ISSET(0, &rfd) != 0);
}


x86 assembly (clang, -O0):
<_kbhit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    -0x24(%rbp),%rdi
call   1040 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x24(%rbp),%rsi
call   1080 <FD_SET@plt>
xor    %edi,%edi
lea    -0x8(%rbp),%rsi
call   1060 <tcgetattr@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    0x2e33(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x10(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    $0x1,%edi
lea    -0x24(%rbp),%rsi
xor    %ecx,%ecx
lea    -0x20(%rbp),%r8
mov    %ecx,%edx
call   1050 <select@plt>
cmp    $0xffffffff,%eax
jne    11f7 <_kbhit+0x97>
movl   $0x0,-0x4(%rbp)
jmp    1225 <_kbhit+0xc5>
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x8(%rbp),%rdx
call   1070 <tcsetattr@plt>
xor    %edi,%edi
lea    -0x24(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_kbhit>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%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   1080 <FD_SET@plt>
mov    %rsp,%rsi
xor    %edi,%edi
call   1060 <tcgetattr@plt>
mov    0x2e35(%rip),%rax
mov    0x2e3e(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    (%rsp),%ecx
mov    %ecx,0x8(%rsp)
mov    0x2e32(%rip),%r15
mov    (%r15),%esi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1070 <tcsetattr@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     1201 <_kbhit+0xa1>
mov    (%r15),%esi
mov    %rsp,%rdx
xor    %edi,%edi
call   1070 <tcsetattr@plt>
lea    0xc(%rsp),%rsi
xor    %edi,%edi
call   1030 <FD_ISSET@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
%struct.timeval = type { i64, i64 }
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define i32 @_kbhit() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
store i64 0, i64* %6
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
store i64 0, i64* %7
%8 = call i32 @FD_ZERO(i32* %5)
%9 = call i32 @FD_SET(i32 0, i32* %5)
%10 = call i32 @tcgetattr(i32 0, %struct.termios* %2)
%11 = bitcast %struct.termios* %3 to i8*
%12 = bitcast %struct.termios* %2 to i8*
%13 = load i32, i32* @ICANON
%14 = load i32, i32* @ECHO
%15 = or i32 %13, %14
%16 = xor i32 %15, -1
%17 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%18 = load i32, i32* %17
%19 = and i32 %18, %16
store i32 %19, i32* %17
%20 = load i32, i32* @TCSANOW
%21 = call i32 @tcsetattr(i32 0, i32 %20, %struct.termios* %3)
%22 = call i32 @select(i32 1, i32* %5, i32 0, i32 0, %struct.timeval* %4)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %0
store i32 0, i32* %1
br label %31
25: ; preds = %0
%26 = load i32, i32* @TCSANOW
%27 = call i32 @tcsetattr(i32 0, i32 %26, %struct.termios* %2)
%28 = call i64 @FD_ISSET(i32 0, i32* %5)
%29 = icmp ne i64 %28, 0
%30 = zext i1 %29 to i32
store i32 %30, i32* %1
br label %31
31: ; preds = %25, %24
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
%struct.termios = type { i32 }
%struct.timeval = type { i64, i64 }
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define i32 @_kbhit() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = bitcast %struct.termios* %1 to i8*
%6 = bitcast %struct.termios* %2 to i8*
%7 = bitcast %struct.timeval* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast %struct.timeval* %3 to i8*
%10 = call i32 @FD_ZERO(i32* %4)
%11 = call i32 @FD_SET(i32 0, i32* %4)
%12 = call i32 @tcgetattr(i32 0, %struct.termios* %1)
%13 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%14 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%15 = load i32, i32* %13
%16 = load i32, i32* @ICANON
%17 = load i32, i32* @ECHO
%18 = or i32 %17, %16
%19 = xor i32 %18, -1
%20 = and i32 %15, %19
store i32 %20, i32* %14
%21 = load i32, i32* @TCSANOW
%22 = call i32 @tcsetattr(i32 0, i32 %21, %struct.termios* %2)
%23 = call i32 @select(i32 1, i32* %4, i32 0, i32 0, %struct.timeval* %3)
%24 = icmp eq i32 %23, -1
br i1 %24, label %31, label %25
25: ; preds = %0
%26 = load i32, i32* @TCSANOW
%27 = call i32 @tcsetattr(i32 0, i32 %26, %struct.termios* %1)
%28 = call i64 @FD_ISSET(i32 0, i32* %4)
%29 = icmp ne i64 %28, 0
%30 = zext i1 %29 to i32
br label %31
31: ; preds = %0, %25
%32 = phi i32 [ %30, %25 ], [ 0, %0 ]
ret i32 %32
}