#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 {
  int tv_usec;
  scalar_t__ tv_sec;
};
struct termios {
  int *c_cc;
  int /*<<< orphan*/ c_lflag;
  scalar_t__ c_oflag;
  scalar_t__ c_iflag;
};

/* Variables and functions */
int /*<<< orphan*/ FIONREAD;
int /*<<< orphan*/ ICANON;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
scalar_t__ ioctl(int /*<<< orphan*/, int /*<<< orphan*/, int *);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
int kbhit(void) {
  int cnt = 0;
  int error;
  static struct termios Otty, Ntty;
  tcgetattr(0, &Otty);
  Ntty = Otty;
  Ntty.c_iflag = 0;
  Ntty.c_oflag = 0;
  Ntty.c_lflag &= ~ICANON;
  Ntty.c_cc[VMIN] = 1;
  Ntty.c_cc[VTIME] = 1;
  if (0 == (error = tcsetattr(0, TCSANOW, &Ntty))) {
    struct timeval tv;
    error += ioctl(0, FIONREAD, &cnt);
    error += tcsetattr(0, TCSANOW, &Otty);
    tv.tv_sec = 0;
    tv.tv_usec = 100;
    select(1, NULL, NULL, NULL, &tv);
  }
  return (error == 0 ? cnt : -1);
}


x86 assembly (clang, -O0):
<kbhit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
xor    %edi,%edi
lea    0x2ef0(%rip),%rsi
call   1050 <tcgetattr@plt>
mov    0x2ee4(%rip),%rax
mov    %rax,0x2efd(%rip)
mov    0x2ede(%rip),%rax
mov    %rax,0x2ef7(%rip)
mov    0x2ed8(%rip),%rax
mov    %rax,0x2ef1(%rip)
mov    0x2ed2(%rip),%rax
mov    %rax,0x2eeb(%rip)
movq   $0x0,0x2ee0(%rip)
movq   $0x0,0x2ecd(%rip)
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    0x2eb3(%rip),%eax
mov    %eax,0x2ead(%rip)
mov    0x2e9e(%rip),%rax
mov    0x2e07(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e86(%rip),%rax
mov    0x2ddf(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0x2e63(%rip),%rdx
call   1060 <tcsetattr@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jne    127e <kbhit+0x13e>
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x4(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %rax,%rcx
movslq -0x8(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x8(%rbp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0x2dfc(%rip),%rdx
call   1060 <tcsetattr@plt>
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x64,-0x18(%rbp)
mov    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1040 <select@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1293 <kbhit+0x153>
mov    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    12a0 <kbhit+0x160>
mov    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    12a0 <kbhit+0x160>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kbhit>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
movl   $0x0,0xc(%rsp)
lea    0x2ef0(%rip),%rsi
xor    %edi,%edi
call   1050 <tcgetattr@plt>
movups 0x2ee2(%rip),%xmm0
movups %xmm0,0x2efb(%rip)
lea    0x2ef4(%rip),%rdx
xorps  %xmm0,%xmm0
movups %xmm0,0x2efa(%rip)
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x2ee1(%rip)
mov    0x2ed2(%rip),%rax
mov    0x2e3b(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e1a(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e21(%rip),%r15
mov    (%r15),%esi
xor    %edi,%edi
call   1060 <tcsetattr@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1223 <kbhit+0xe3>
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
xor    %edi,%edi
call   1030 <ioctl@plt>
mov    %rax,%r14
mov    (%r15),%esi
lea    0x2e57(%rip),%rdx
xor    %edi,%edi
call   1060 <tcsetattr@plt>
mov    %eax,%ebx
add    %r14d,%ebx
movq   $0x0,0x18(%rsp)
movl   $0x64,0x10(%rsp)
lea    0x10(%rsp),%r8
mov    $0x1,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <select@plt>
neg    %ebx
sbb    %eax,%eax
or     0xc(%rsp),%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32*, i32, i64, i64 }
%struct.timeval = type { i32, i64 }
@kbhit.Otty = internal global %struct.termios zeroinitializer
@kbhit.Ntty = internal global %struct.termios zeroinitializer
@ICANON = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@FIONREAD = global i32 0
define i32 @kbhit() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.timeval
store i32 0, i32* %1
%4 = call i32 @tcgetattr(i32 0, %struct.termios* @kbhit.Otty)
store i64 0, i64* getelementptr inbounds (%struct.termios, %struct.termios* @kbhit.Ntty, i32 0, i32 3)
store i64 0, i64* getelementptr inbounds (%struct.termios, %struct.termios* @kbhit.Ntty, i32 0, i32 2)
%5 = load i32, i32* @ICANON
%6 = xor i32 %5, -1
%7 = load i32, i32* getelementptr inbounds (%struct.termios, %struct.termios* @kbhit.Ntty, i32 0, i32 1)
%8 = and i32 %7, %6
store i32 %8, i32* getelementptr inbounds (%struct.termios, %struct.termios* @kbhit.Ntty, i32 0, i32 1)
%9 = load i32*, i32** getelementptr inbounds (%struct.termios, %struct.termios* @kbhit.Ntty, i32 0, i32 0)
%10 = load i64, i64* @VMIN
%11 = getelementptr inbounds i32, i32* %9, i64 %10
store i32 1, i32* %11
%12 = load i32*, i32** getelementptr inbounds (%struct.termios, %struct.termios* @kbhit.Ntty, i32 0, i32 0)
%13 = load i64, i64* @VTIME
%14 = getelementptr inbounds i32, i32* %12, i64 %13
store i32 1, i32* %14
%15 = load i32, i32* @TCSANOW
%16 = call i32 @tcsetattr(i32 0, i32 %15, %struct.termios* @kbhit.Ntty)
store i32 %16, i32* %2
%17 = icmp eq i32 0, %16
br i1 %17, label %18, label %32
18: ; preds = %0
%19 = load i32, i32* @FIONREAD
%20 = call i64 @ioctl(i32 0, i32 %19, i32* %1)
%21 = load i32, i32* %2
%22 = sext i32 %21 to i64
%23 = add nsw i64 %22, %20
%24 = trunc i64 %23 to i32
store i32 %24, i32* %2
%25 = load i32, i32* @TCSANOW
%26 = call i32 @tcsetattr(i32 0, i32 %25, %struct.termios* @kbhit.Otty)
%27 = load i32, i32* %2
%28 = add nsw i32 %27, %26
store i32 %28, i32* %2
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
store i64 0, i64* %29
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
store i32 100, i32* %30
%31 = call i32 @select(i32 1, i32* null, i32* null, i32* null, %struct.timeval* %3)
br label %32
32: ; preds = %18, %0
%33 = load i32, i32* %2
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = load i32, i32* %1
br label %38
37: ; preds = %32
br label %38
38: ; preds = %37, %35
%39 = phi i32 [ %36, %35 ], [ -1, %37 ]
ret i32 %39
}

LLVM IR (-O2):
%struct.termios = type { i32*, i32, i64, i64 }
%struct.timeval = type { i32, i64 }
@kbhit.Otty = internal global %struct.termios zeroinitializer
@kbhit.Ntty = internal global %struct.termios zeroinitializer
@ICANON = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@FIONREAD = global i32 0
define i32 @kbhit() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%4 = tail call i32 @tcgetattr(i32 0, %struct.termios* @kbhit.Otty)
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 dereferenceable(32) bitcast (%struct.termios* @kbhit.Ntty to i8*), i8* align 8 dereferenceable(32) bitcast (%struct.termios* @kbhit.Otty to i8*), i64 16, i1 false)
%5 = load i32, i32* @ICANON
%6 = xor i32 %5, -1
%7 = load i32, i32* getelementptr inbounds (%struct.termios, %struct.termios* @kbhit.Ntty, i64 0, i32 1)
%8 = and i32 %7, %6
store i32 %8, i32* getelementptr inbounds (%struct.termios, %struct.termios* @kbhit.Ntty, i64 0, i32 1)
%9 = load i32*, i32** getelementptr inbounds (%struct.termios, %struct.termios* @kbhit.Ntty, i64 0, i32 0)
%10 = load i64, i64* @VMIN
%11 = getelementptr inbounds i32, i32* %9, i64 %10
store i32 1, i32* %11
%12 = load i64, i64* @VTIME
%13 = getelementptr inbounds i32, i32* %9, i64 %12
store i32 1, i32* %13
%14 = load i32, i32* @TCSANOW
%15 = tail call i32 @tcsetattr(i32 0, i32 %14, %struct.termios* @kbhit.Ntty)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %28
17: ; preds = %0
%18 = bitcast %struct.timeval* %2 to i8*
%19 = load i32, i32* @FIONREAD
%20 = call i64 @ioctl(i32 0, i32 %19, i32* %1)
%21 = trunc i64 %20 to i32
%22 = load i32, i32* @TCSANOW
%23 = call i32 @tcsetattr(i32 0, i32 %22, %struct.termios* @kbhit.Otty)
%24 = add nsw i32 %23, %21
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
store i64 0, i64* %25
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
store i32 100, i32* %26
%27 = call i32 @select(i32 1, i32* null, i32* null, i32* null, %struct.timeval* %2)
br label %28
28: ; preds = %17, %0
%29 = phi i32 [ %24, %17 ], [ %15, %0 ]
%30 = icmp eq i32 %29, 0
%31 = load i32, i32* %1
%32 = select i1 %30, i32 %31, i32 -1
ret i32 %32
}