#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, newt;
  int ch;
  int 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);
  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    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
mov    0x2e50(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    0x2e0a(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x14(%rbp)
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
call   1030 <fcntl@plt>
mov    -0x14(%rbp),%eax
mov    0x2d9a(%rip),%rcx
cmp    (%rcx),%eax
je     1253 <console_kbhit+0x103>
mov    -0x14(%rbp),%edi
mov    0x2da0(%rip),%rax
mov    (%rax),%esi
call   1050 <ungetc@plt>
movl   $0x1,-0x4(%rbp)
jmp    125a <console_kbhit+0x10a>
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    0x2e26(%rip),%rax
mov    0x2e4f(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    0x8(%rsp),%ecx
mov    %ecx,0x10(%rsp)
mov    (%r12),%edi
mov    0x2e3e(%rip),%r13
mov    0x0(%r13),%esi
lea    0x10(%rsp),%rdx
call   1070 <tcsetattr@plt>
mov    (%r12),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
xor    %r15d,%r15d
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,%ebx
mov    (%r12),%edi
mov    0x2dec(%rip),%r14
mov    (%r14),%esi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
or     %ebx,%edx
call   1030 <fcntl@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %eax,%ebp
mov    (%r12),%edi
mov    0x0(%r13),%esi
lea    0x8(%rsp),%rdx
call   1070 <tcsetattr@plt>
mov    (%r12),%edi
mov    (%r14),%esi
mov    %ebx,%edx
call   1030 <fcntl@plt>
mov    0x2db9(%rip),%rax
cmp    (%rax),%ebp
je     1229 <console_kbhit+0xd9>
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%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
@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 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 = 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 i32 @fcntl(i32 %20, i32 %21, i32 0)
store i32 %22, i32* %5
%23 = load i32, i32* @STDIN_FILENO
%24 = load i32, i32* @F_SETFL
%25 = load i32, i32* %5
%26 = load i32, i32* @O_NONBLOCK
%27 = or i32 %25, %26
%28 = call i32 @fcntl(i32 %23, i32 %24, i32 %27)
%29 = call i32 (...) @getchar()
store i32 %29, i32* %4
%30 = load i32, i32* @STDIN_FILENO
%31 = load i32, i32* @TCSANOW
%32 = call i32 @tcsetattr(i32 %30, i32 %31, %struct.termios* %2)
%33 = load i32, i32* @STDIN_FILENO
%34 = load i32, i32* @F_SETFL
%35 = load i32, i32* %5
%36 = call i32 @fcntl(i32 %33, i32 %34, i32 %35)
%37 = load i32, i32* %4
%38 = load i32, i32* @EOF
%39 = icmp ne i32 %37, %38
br i1 %39, label %40, label %44
40: ; preds = %0
%41 = load i32, i32* %4
%42 = load i32, i32* @stdin
%43 = call i32 @ungetc(i32 %41, i32 %42)
store i32 1, i32* %1
br label %45
44: ; preds = %0
store i32 0, i32* %1
br label %45
45: ; preds = %44, %40
%46 = load i32, i32* %1
ret i32 %46
}

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 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 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%8 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%9 = load i32, i32* %7
%10 = load i32, i32* @ICANON
%11 = load i32, i32* @ECHO
%12 = or i32 %11, %10
%13 = xor i32 %12, -1
%14 = and i32 %9, %13
store i32 %14, i32* %8
%15 = load i32, i32* @STDIN_FILENO
%16 = load i32, i32* @TCSANOW
%17 = call i32 @tcsetattr(i32 %15, i32 %16, %struct.termios* %2)
%18 = load i32, i32* @STDIN_FILENO
%19 = load i32, i32* @F_GETFL
%20 = call i32 @fcntl(i32 %18, i32 %19, i32 0)
%21 = load i32, i32* @STDIN_FILENO
%22 = load i32, i32* @F_SETFL
%23 = load i32, i32* @O_NONBLOCK
%24 = or i32 %23, %20
%25 = call i32 @fcntl(i32 %21, i32 %22, i32 %24)
%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 %20)
%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
}