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

/* Variables and functions */
int /*<<< orphan*/ ICANON;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int getchar();
int /*<<< orphan*/ memcpy(struct termios *, struct termios *, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ ungetc(int, int /*<<< orphan*/);
int kbhit(void) {
  struct termios term, oterm;
  int fd = 0;
  int c = 0;
  tcgetattr(fd, &oterm);
  memcpy(&term, &oterm, sizeof(term));
  term.c_lflag = term.c_lflag & (!ICANON);
  term.c_cc[VMIN] = 0;
  term.c_cc[VTIME] = 1;
  tcsetattr(fd, TCSANOW, &term);
  c = getchar();
  tcsetattr(fd, TCSANOW, &oterm);
  if (c != -1)
    ungetc(c, stdin);
  return ((c != -1) ? 1 : 0);
}


x86 assembly (clang, -O0):
<kbhit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <tcgetattr@plt>
lea    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%eax
mov    0x2e32(%rip),%rcx
cmpl   $0x0,(%rcx)
setne  %cl
xor    $0xff,%cl
and    $0x1,%cl
movzbl %cl,%ecx
and    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    0x2e2b(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x8(%rbp),%rax
mov    0x2e06(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x24(%rbp),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmpl   $0xffffffff,-0x28(%rbp)
je     121b <kbhit+0xcb>
mov    -0x28(%rbp),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
call   1050 <ungetc@plt>
mov    -0x28(%rbp),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0xffffffff,%edx
cmovne %ecx,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kbhit>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
lea    0x10(%rsp),%r14
xor    %edi,%edi
mov    %r14,%rsi
call   1060 <tcgetattr@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %r14,%rsi
mov    $0x10,%edx
call   1040 <memcpy@plt>
mov    0x2e3f(%rip),%rax
xor    %ecx,%ecx
cmpl   $0x0,(%rax)
sete   %cl
and    %ecx,(%rsp)
mov    0x8(%rsp),%rax
mov    0x2e40(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e1f(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e26(%rip),%rbx
mov    (%rbx),%esi
xor    %edi,%edi
mov    %rbp,%rdx
call   1070 <tcsetattr@plt>
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ebp
mov    (%rbx),%esi
xor    %edi,%edi
mov    %r14,%rdx
call   1070 <tcsetattr@plt>
cmp    $0xffffffff,%ebp
je     11f0 <kbhit+0xa0>
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
call   1050 <ungetc@plt>
xor    %eax,%eax
cmp    $0xffffffff,%ebp
setne  %al
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32* }
@ICANON = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@stdin = global i32 0
define i32 @kbhit() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %3
store i32 0, i32* %4
%5 = load i32, i32* %3
%6 = call i32 @tcgetattr(i32 %5, %struct.termios* %2)
%7 = call i32 @memcpy(%struct.termios* %1, %struct.termios* %2, i32 16)
%8 = getelementptr inbounds %struct.termios, %struct.termios* %1, i32 0, i32 0
%9 = load i32, i32* %8
%10 = load i32, i32* @ICANON
%11 = icmp ne i32 %10, 0
%12 = xor i1 %11, true
%13 = zext i1 %12 to i32
%14 = and i32 %9, %13
%15 = getelementptr inbounds %struct.termios, %struct.termios* %1, i32 0, i32 0
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.termios, %struct.termios* %1, i32 0, i32 1
%17 = load i32*, i32** %16
%18 = load i64, i64* @VMIN
%19 = getelementptr inbounds i32, i32* %17, i64 %18
store i32 0, i32* %19
%20 = getelementptr inbounds %struct.termios, %struct.termios* %1, i32 0, i32 1
%21 = load i32*, i32** %20
%22 = load i64, i64* @VTIME
%23 = getelementptr inbounds i32, i32* %21, i64 %22
store i32 1, i32* %23
%24 = load i32, i32* %3
%25 = load i32, i32* @TCSANOW
%26 = call i32 @tcsetattr(i32 %24, i32 %25, %struct.termios* %1)
%27 = call i32 (...) @getchar()
store i32 %27, i32* %4
%28 = load i32, i32* %3
%29 = load i32, i32* @TCSANOW
%30 = call i32 @tcsetattr(i32 %28, i32 %29, %struct.termios* %2)
%31 = load i32, i32* %4
%32 = icmp ne i32 %31, -1
br i1 %32, label %33, label %37
33: ; preds = %0
%34 = load i32, i32* %4
%35 = load i32, i32* @stdin
%36 = call i32 @ungetc(i32 %34, i32 %35)
br label %37
37: ; preds = %33, %0
%38 = load i32, i32* %4
%39 = icmp ne i32 %38, -1
%40 = zext i1 %39 to i64
%41 = select i1 %39, i32 1, i32 0
ret i32 %41
}

LLVM IR (-O2):
%struct.termios = type { i32, i32* }
@ICANON = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@stdin = global i32 0
define i32 @kbhit() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %1 to i8*
%4 = bitcast %struct.termios* %2 to i8*
%5 = call i32 @tcgetattr(i32 0, %struct.termios* %2)
%6 = call i32 @memcpy(%struct.termios* %1, %struct.termios* %2, i32 16)
%7 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%8 = load i32, i32* %7
%9 = load i32, i32* @ICANON
%10 = icmp eq i32 %9, 0
%11 = zext i1 %10 to i32
%12 = and i32 %8, %11
store i32 %12, i32* %7
%13 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 1
%14 = load i32*, i32** %13
%15 = load i64, i64* @VMIN
%16 = getelementptr inbounds i32, i32* %14, i64 %15
store i32 0, i32* %16
%17 = load i64, i64* @VTIME
%18 = getelementptr inbounds i32, i32* %14, i64 %17
store i32 1, i32* %18
%19 = load i32, i32* @TCSANOW
%20 = call i32 @tcsetattr(i32 0, i32 %19, %struct.termios* %1)
%21 = call i32 (...) @getchar()
%22 = load i32, i32* @TCSANOW
%23 = call i32 @tcsetattr(i32 0, i32 %22, %struct.termios* %2)
%24 = icmp eq i32 %21, -1
br i1 %24, label %28, label %25
25: ; preds = %0
%26 = load i32, i32* @stdin
%27 = call i32 @ungetc(i32 %21, i32 %26)
br label %28
28: ; preds = %25, %0
%29 = xor i1 %24, true
%30 = zext i1 %29 to i32
ret i32 %30
}