#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 /*<<< orphan*/ c_lflag;
};

/* Variables and functions */
int /*<<< orphan*/ FIONREAD;
int /*<<< orphan*/ ICANON;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ ioctl(int const, int /*<<< orphan*/, int *);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ tcgetattr(int const, struct termios *);
int /*<<< orphan*/ tcsetattr(int const, int /*<<< orphan*/, struct termios *);
int vogl_kbhit() {
  static const int STDIN = 0;
  static int initialized = 0;
  if (!initialized) {
    struct termios term;
    tcgetattr(STDIN, &term);
    term.c_lflag &= ~ICANON;
    tcsetattr(STDIN, TCSANOW, &term);
    setbuf(stdin, NULL);
    initialized = 1;
  }
  int bytesWaiting;
  ioctl(STDIN, FIONREAD, &bytesWaiting);
  return bytesWaiting;
}


x86 assembly (clang, -O0):
<vogl_kbhit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
cmpl   $0x0,0x2ef5(%rip)
jne    11a2 <vogl_kbhit+0x62>
xor    %edi,%edi
lea    -0x8(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x8(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <setbuf@plt>
movl   $0x1,0x2ea2(%rip)
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0xc(%rbp),%rdx
call   1040 <ioctl@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vogl_kbhit>:
push   %rbx
sub    $0x10,%rsp
cmpb   $0x0,0x2ef8(%rip)
jne    1196 <vogl_kbhit+0x56>
lea    0x8(%rsp),%rbx
xor    %edi,%edi
mov    %rbx,%rsi
call   1050 <tcgetattr@plt>
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x8(%rsp)
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
mov    %rbx,%rdx
call   1060 <tcsetattr@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1030 <setbuf@plt>
movb   $0x1,0x2eae(%rip)
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
xor    %edi,%edi
call   1040 <ioctl@plt>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@vogl_kbhit.STDIN = internal constant i32 0
@vogl_kbhit.initialized = internal global i32 0
@ICANON = global i32 0
@TCSANOW = global i32 0
@stdin = global i32 0
@FIONREAD = global i32 0
define i32 @vogl_kbhit() {
%1 = alloca %struct.termios
%2 = alloca i32
%3 = load i32, i32* @vogl_kbhit.initialized
%4 = icmp ne i32 %3, 0
br i1 %4, label %16, label %5
5: ; preds = %0
%6 = call i32 @tcgetattr(i32 0, %struct.termios* %1)
%7 = load i32, i32* @ICANON
%8 = xor i32 %7, -1
%9 = getelementptr inbounds %struct.termios, %struct.termios* %1, i32 0, i32 0
%10 = load i32, i32* %9
%11 = and i32 %10, %8
store i32 %11, i32* %9
%12 = load i32, i32* @TCSANOW
%13 = call i32 @tcsetattr(i32 0, i32 %12, %struct.termios* %1)
%14 = load i32, i32* @stdin
%15 = call i32 @setbuf(i32 %14, i32* null)
store i32 1, i32* @vogl_kbhit.initialized
br label %16
16: ; preds = %5, %0
%17 = load i32, i32* @FIONREAD
%18 = call i32 @ioctl(i32 0, i32 %17, i32* %2)
%19 = load i32, i32* %2
ret i32 %19
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@vogl_kbhit.initialized = internal unnamed_addr global i1 false
@ICANON = global i32 0
@TCSANOW = global i32 0
@stdin = global i32 0
@FIONREAD = global i32 0
define i32 @vogl_kbhit() {
%1 = alloca %struct.termios
%2 = alloca i32
%3 = load i1, i1* @vogl_kbhit.initialized
br i1 %3, label %16, label %4
4: ; preds = %0
%5 = bitcast %struct.termios* %1 to i8*
%6 = call i32 @tcgetattr(i32 0, %struct.termios* %1)
%7 = load i32, i32* @ICANON
%8 = xor i32 %7, -1
%9 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%10 = load i32, i32* %9
%11 = and i32 %10, %8
store i32 %11, i32* %9
%12 = load i32, i32* @TCSANOW
%13 = call i32 @tcsetattr(i32 0, i32 %12, %struct.termios* %1)
%14 = load i32, i32* @stdin
%15 = call i32 @setbuf(i32 %14, i32* null)
store i1 true, i1* @vogl_kbhit.initialized
br label %16
16: ; preds = %4, %0
%17 = bitcast i32* %2 to i8*
%18 = load i32, i32* @FIONREAD
%19 = call i32 @ioctl(i32 0, i32 %18, i32* %2)
%20 = load i32, i32* %2
ret i32 %20
}