#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 EOF;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int ICANON;
int O_NONBLOCK;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, int);
int getchar();
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int /*<<< orphan*/ ungetc(int, int /*<<< orphan*/);
int console_kbhit(void) {
  struct termios oldt;
  struct termios newt;
  int oldf;
  int newf;
  int ch;
  tcgetattr(STDIN_FILENO, &oldt);
  oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  newf = oldf | O_NONBLOCK;
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  fcntl(STDIN_FILENO, F_SETFL, newf);
  ch = getchar();
  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  fcntl(STDIN_FILENO, F_SETFL, oldf);
  if (ch != EOF) {
    ungetc(ch, stdin);
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<console_kbhit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1060 <tcgetattr@plt>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    0x2e34(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e37(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x18(%rbp)
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
call   1030 <fcntl@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
call   1030 <fcntl@plt>
mov    -0x1c(%rbp),%eax
mov    0x2d94(%rip),%rcx
cmp    (%rcx),%eax
je     1259 <console_kbhit+0x109>
mov    -0x1c(%rbp),%edi
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
call   1050 <ungetc@plt>
movl   $0x1,-0x4(%rbp)
jmp    1260 <console_kbhit+0x110>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<console_kbhit>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e43(%rip),%r12
mov    (%r12),%edi
lea    0x8(%rsp),%rsi
call   1060 <tcgetattr@plt>
mov    (%r12),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %r15d,%r15d
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    0x2e0f(%rip),%rcx
mov    0x2e38(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
not    %edx
and    0x8(%rsp),%edx
mov    %eax,%ebp
mov    %edx,0x10(%rsp)
mov    0x2e39(%rip),%rax
mov    (%rax),%ebx
or     %ebp,%ebx
mov    (%r12),%edi
mov    0x2e1a(%rip),%r13
mov    0x0(%r13),%esi
lea    0x10(%rsp),%rdx
call   1070 <tcsetattr@plt>
mov    (%r12),%edi
mov    0x2de1(%rip),%r14
mov    (%r14),%esi
mov    %ebx,%edx
call   1030 <fcntl@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %eax,%ebx
mov    (%r12),%edi
mov    0x0(%r13),%esi
lea    0x8(%rsp),%rdx
call   1070 <tcsetattr@plt>
mov    (%r12),%edi
mov    (%r14),%esi
mov    %ebp,%edx
call   1030 <fcntl@plt>
mov    0x2db7(%rip),%rax
cmp    (%rax),%ebx
je     122b <console_kbhit+0xdb>
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1050 <ungetc@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@F_GETFL = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@O_NONBLOCK = global i32 0
@TCSANOW = global i32 0
@F_SETFL = global i32 0
@EOF = global i32 0
@stdin = global i32 0
define i32 @console_kbhit() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = load i32, i32* @STDIN_FILENO
%8 = call i32 @tcgetattr(i32 %7, %struct.termios* %2)
%9 = load i32, i32* @STDIN_FILENO
%10 = load i32, i32* @F_GETFL
%11 = call i32 @fcntl(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %4
%12 = bitcast %struct.termios* %3 to i8*
%13 = bitcast %struct.termios* %2 to i8*
%14 = load i32, i32* @ICANON
%15 = load i32, i32* @ECHO
%16 = or i32 %14, %15
%17 = xor i32 %16, -1
%18 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%19 = load i32, i32* %18
%20 = and i32 %19, %17
store i32 %20, i32* %18
%21 = load i32, i32* %4
%22 = load i32, i32* @O_NONBLOCK
%23 = or i32 %21, %22
store i32 %23, i32* %5
%24 = load i32, i32* @STDIN_FILENO
%25 = load i32, i32* @TCSANOW
%26 = call i32 @tcsetattr(i32 %24, i32 %25, %struct.termios* %3)
%27 = load i32, i32* @STDIN_FILENO
%28 = load i32, i32* @F_SETFL
%29 = load i32, i32* %5
%30 = call i32 @fcntl(i32 %27, i32 %28, i32 %29)
%31 = call i32 (...) @getchar()
store i32 %31, i32* %6
%32 = load i32, i32* @STDIN_FILENO
%33 = load i32, i32* @TCSANOW
%34 = call i32 @tcsetattr(i32 %32, i32 %33, %struct.termios* %2)
%35 = load i32, i32* @STDIN_FILENO
%36 = load i32, i32* @F_SETFL
%37 = load i32, i32* %4
%38 = call i32 @fcntl(i32 %35, i32 %36, i32 %37)
%39 = load i32, i32* %6
%40 = load i32, i32* @EOF
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %46
42: ; preds = %0
%43 = load i32, i32* %6
%44 = load i32, i32* @stdin
%45 = call i32 @ungetc(i32 %43, i32 %44)
store i32 1, i32* %1
br label %47
46: ; preds = %0
store i32 0, i32* %1
br label %47
47: ; preds = %46, %42
%48 = load i32, i32* %1
ret i32 %48
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@F_GETFL = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@O_NONBLOCK = global i32 0
@TCSANOW = global i32 0
@F_SETFL = global i32 0
@EOF = global i32 0
@stdin = global i32 0
define i32 @console_kbhit() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %1 to i8*
%4 = bitcast %struct.termios* %2 to i8*
%5 = load i32, i32* @STDIN_FILENO
%6 = call i32 @tcgetattr(i32 %5, %struct.termios* %1)
%7 = load i32, i32* @STDIN_FILENO
%8 = load i32, i32* @F_GETFL
%9 = call i32 @fcntl(i32 %7, i32 %8, i32 0)
%10 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%11 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%12 = load i32, i32* %10
%13 = load i32, i32* @ICANON
%14 = load i32, i32* @ECHO
%15 = or i32 %14, %13
%16 = xor i32 %15, -1
%17 = and i32 %12, %16
store i32 %17, i32* %11
%18 = load i32, i32* @O_NONBLOCK
%19 = or i32 %18, %9
%20 = load i32, i32* @STDIN_FILENO
%21 = load i32, i32* @TCSANOW
%22 = call i32 @tcsetattr(i32 %20, i32 %21, %struct.termios* %2)
%23 = load i32, i32* @STDIN_FILENO
%24 = load i32, i32* @F_SETFL
%25 = call i32 @fcntl(i32 %23, i32 %24, i32 %19)
%26 = call i32 (...) @getchar()
%27 = load i32, i32* @STDIN_FILENO
%28 = load i32, i32* @TCSANOW
%29 = call i32 @tcsetattr(i32 %27, i32 %28, %struct.termios* %1)
%30 = load i32, i32* @STDIN_FILENO
%31 = load i32, i32* @F_SETFL
%32 = call i32 @fcntl(i32 %30, i32 %31, i32 %9)
%33 = load i32, i32* @EOF
%34 = icmp eq i32 %26, %33
br i1 %34, label %38, label %35
35: ; preds = %0
%36 = load i32, i32* @stdin
%37 = call i32 @ungetc(i32 %26, i32 %36)
br label %38
38: ; preds = %0, %35
%39 = phi i32 [ 1, %35 ], [ 0, %0 ]
ret i32 %39
}