#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 termios {
  int c_lflag;
};

/* Variables and functions */
int ECHO;
int EIO;
char EOF;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int ICANON;
short O_NONBLOCK;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ exit(int);
short fcntl(int /*<<< orphan*/, int /*<<< orphan*/, short);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
short tolower(char);
short kbhit(void) {
  struct termios oldt, newt;
  char ch[20];
  short oldf;
  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
  if (!(scanf("%s", ch))) {
    fprintf(stderr, "error -> %d\n", EIO);
    exit(3);
  }
  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  fcntl(STDIN_FILENO, F_SETFL, oldf);
  if (ch[0] != EOF) {
    return tolower(ch[0]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<kbhit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1070 <tcgetattr@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%eax
mov    0x2e38(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %ax,-0x32(%rbp)
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
movswl -0x32(%rbp),%eax
mov    0x2de8(%rip),%rcx
movswl (%rcx),%ecx
or     %ecx,%eax
movswl %ax,%edx
call   1030 <fcntl@plt>
lea    -0x30(%rbp),%rsi
lea    0xde8(%rip),%rdi
call   1060 <scanf@plt>
cmp    $0x0,%eax
jne    124e <kbhit+0xde>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
mov    0x2d8a(%rip),%rax
mov    (%rax),%edx
lea    0xdc4(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
mov    0x2d37(%rip),%rax
mov    (%rax),%esi
movswl -0x32(%rbp),%edx
call   1030 <fcntl@plt>
movsbl -0x30(%rbp),%eax
mov    0x2d41(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     12ac <kbhit+0x13c>
movsbl -0x30(%rbp),%edi
call   1050 <tolower@plt>
mov    %ax,-0x2(%rbp)
jmp    12b2 <kbhit+0x142>
movw   $0x0,-0x2(%rbp)
movswl -0x2(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kbhit>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e1e(%rip),%r14
mov    (%r14),%edi
mov    %rsp,%rsi
call   1070 <tcgetattr@plt>
mov    0x2e04(%rip),%rax
mov    0x2e3d(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    (%rsp),%ecx
mov    %ecx,0x8(%rsp)
mov    (%r14),%edi
mov    0x2e2e(%rip),%r15
mov    (%r15),%esi
lea    0x8(%rsp),%rdx
call   1080 <tcsetattr@plt>
mov    (%r14),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,%ebx
mov    (%r14),%edi
mov    0x2dd2(%rip),%r12
mov    (%r12),%esi
mov    0x2dff(%rip),%rax
movzwl (%rax),%eax
or     %bx,%ax
movswl %ax,%edx
call   1030 <fcntl@plt>
lea    0xe02(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1060 <scanf@plt>
test   %eax,%eax
je     1250 <kbhit+0xe0>
mov    (%r14),%edi
mov    (%r15),%esi
mov    %rsp,%rdx
call   1080 <tcsetattr@plt>
mov    (%r14),%edi
mov    (%r12),%esi
movswl %bx,%edx
call   1030 <fcntl@plt>
mov    0x10(%rsp),%al
mov    0x2d9c(%rip),%rcx
cmp    (%rcx),%al
jne    123c <kbhit+0xcc>
xor    %eax,%eax
jmp    1244 <kbhit+0xd4>
movsbl %al,%edi
call   1050 <tolower@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
mov    0x2d60(%rip),%rax
mov    (%rax),%edx
lea    0xd9a(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x3,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i16 0
@.str = [3 x i8] c"%s\00"
@stderr = global i32 0
@.str.1 = [13 x i8] c"error -> %d\0A\00"
@EIO = global i32 0
@EOF = global i8 0
define signext i16 @kbhit() {
%1 = alloca i16
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = alloca [20 x i8]
%5 = alloca i16
%6 = load i32, i32* @STDIN_FILENO
%7 = call i32 @tcgetattr(i32 %6, %struct.termios* %2)
%8 = bitcast %struct.termios* %3 to i8*
%9 = bitcast %struct.termios* %2 to i8*
%10 = load i32, i32* @ICANON
%11 = load i32, i32* @ECHO
%12 = or i32 %10, %11
%13 = xor i32 %12, -1
%14 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%15 = load i32, i32* %14
%16 = and i32 %15, %13
store i32 %16, i32* %14
%17 = load i32, i32* @STDIN_FILENO
%18 = load i32, i32* @TCSANOW
%19 = call i32 @tcsetattr(i32 %17, i32 %18, %struct.termios* %3)
%20 = load i32, i32* @STDIN_FILENO
%21 = load i32, i32* @F_GETFL
%22 = call signext i16 @fcntl(i32 %20, i32 %21, i16 signext 0)
store i16 %22, i16* %5
%23 = load i32, i32* @STDIN_FILENO
%24 = load i32, i32* @F_SETFL
%25 = load i16, i16* %5
%26 = sext i16 %25 to i32
%27 = load i16, i16* @O_NONBLOCK
%28 = sext i16 %27 to i32
%29 = or i32 %26, %28
%30 = trunc i32 %29 to i16
%31 = call signext i16 @fcntl(i32 %23, i32 %24, i16 signext %30)
%32 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%33 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %40, label %35
35: ; preds = %0
%36 = load i32, i32* @stderr
%37 = load i32, i32* @EIO
%38 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %37)
%39 = call i32 @exit(i32 3)
unreachable
40: ; preds = %0
%41 = load i32, i32* @STDIN_FILENO
%42 = load i32, i32* @TCSANOW
%43 = call i32 @tcsetattr(i32 %41, i32 %42, %struct.termios* %2)
%44 = load i32, i32* @STDIN_FILENO
%45 = load i32, i32* @F_SETFL
%46 = load i16, i16* %5
%47 = call signext i16 @fcntl(i32 %44, i32 %45, i16 signext %46)
%48 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = load i8, i8* @EOF
%52 = sext i8 %51 to i32
%53 = icmp ne i32 %50, %52
br i1 %53, label %54, label %58
54: ; preds = %40
%55 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%56 = load i8, i8* %55
%57 = call signext i16 @tolower(i8 signext %56)
store i16 %57, i16* %1
br label %59
58: ; preds = %40
store i16 0, i16* %1
br label %59
59: ; preds = %58, %54
%60 = load i16, i16* %1
ret i16 %60
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i16 0
@.str = [3 x i8] c"%s\00"
@stderr = global i32 0
@.str.1 = [13 x i8] c"error -> %d\0A\00"
@EIO = global i32 0
@EOF = global i8 0
define signext i16 @kbhit() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = alloca [20 x i8]
%4 = bitcast %struct.termios* %1 to i8*
%5 = bitcast %struct.termios* %2 to i8*
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @STDIN_FILENO
%8 = call i32 @tcgetattr(i32 %7, %struct.termios* %1)
%9 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%10 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%11 = load i32, i32* %9
%12 = load i32, i32* @ICANON
%13 = load i32, i32* @ECHO
%14 = or i32 %13, %12
%15 = xor i32 %14, -1
%16 = and i32 %11, %15
store i32 %16, i32* %10
%17 = load i32, i32* @STDIN_FILENO
%18 = load i32, i32* @TCSANOW
%19 = call i32 @tcsetattr(i32 %17, i32 %18, %struct.termios* %2)
%20 = load i32, i32* @STDIN_FILENO
%21 = load i32, i32* @F_GETFL
%22 = call signext i16 @fcntl(i32 %20, i32 %21, i16 signext 0)
%23 = load i32, i32* @STDIN_FILENO
%24 = load i32, i32* @F_SETFL
%25 = load i16, i16* @O_NONBLOCK
%26 = or i16 %25, %22
%27 = call signext i16 @fcntl(i32 %23, i32 %24, i16 signext %26)
%28 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %6)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %0
%31 = load i32, i32* @stderr
%32 = load i32, i32* @EIO
%33 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = call i32 @exit(i32 3)
unreachable
35: ; preds = %0
%36 = load i32, i32* @STDIN_FILENO
%37 = load i32, i32* @TCSANOW
%38 = call i32 @tcsetattr(i32 %36, i32 %37, %struct.termios* %1)
%39 = load i32, i32* @STDIN_FILENO
%40 = load i32, i32* @F_SETFL
%41 = call signext i16 @fcntl(i32 %39, i32 %40, i16 signext %22)
%42 = load i8, i8* %6
%43 = load i8, i8* @EOF
%44 = icmp eq i8 %42, %43
br i1 %44, label %47, label %45
45: ; preds = %35
%46 = call signext i16 @tolower(i8 signext %42)
br label %47
47: ; preds = %35, %45
%48 = phi i16 [ %46, %45 ], [ 0, %35 ]
ret i16 %48
}