#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 /*<<< orphan*/ FIONREAD;
int ICANON;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ getc(int /*<<< orphan*/);
int /*<<< orphan*/ ioctl(int /*<<< orphan*/, int /*<<< orphan*/, int *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int _accacia_kbhit() {
  int res;
  struct termios attr, oldattr;
  getc(stdout);
  tcgetattr(STDIN_FILENO, &attr);
  oldattr = attr;
  attr.c_lflag = ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &attr);
  ioctl(STDIN_FILENO, FIONREAD, &res);
  tcsetattr(STDIN_FILENO, TCSANOW, &oldattr);
  return (res);
}


x86 assembly (clang, -O0):
<_accacia_kbhit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
call   1060 <getc@plt>
mov    0x2e63(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1040 <tcgetattr@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    0x2e5a(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1050 <tcsetattr@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    -0x4(%rbp),%rdx
call   1030 <ioctl@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <tcsetattr@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_accacia_kbhit>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e78(%rip),%rax
mov    (%rax),%edi
call   1060 <getc@plt>
mov    0x2e62(%rip),%rbx
mov    (%rbx),%edi
mov    %rsp,%r14
mov    %r14,%rsi
call   1040 <tcgetattr@plt>
mov    (%rsp),%eax
mov    %eax,0x8(%rsp)
mov    0x2e3f(%rip),%rax
mov    0x2e58(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
mov    %ecx,(%rsp)
mov    (%rbx),%edi
mov    0x2e4e(%rip),%r15
mov    (%r15),%esi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
mov    (%rbx),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
call   1030 <ioctl@plt>
mov    (%rbx),%edi
mov    (%r15),%esi
lea    0x8(%rsp),%rdx
call   1050 <tcsetattr@plt>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@stdout = global i32 0
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@FIONREAD = global i32 0
define i32 @_accacia_kbhit() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = load i32, i32* @stdout
%5 = call i32 @getc(i32 %4)
%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* %2, i32 0, i32 0
store i32 %13, i32* %14
%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* @FIONREAD
%20 = call i32 @ioctl(i32 %18, i32 %19, i32* %1)
%21 = load i32, i32* @STDIN_FILENO
%22 = load i32, i32* @TCSANOW
%23 = call i32 @tcsetattr(i32 %21, i32 %22, %struct.termios* %3)
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@stdout = global i32 0
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@FIONREAD = global i32 0
define i32 @_accacia_kbhit() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = bitcast i32* %1 to i8*
%5 = bitcast %struct.termios* %2 to i8*
%6 = bitcast %struct.termios* %3 to i8*
%7 = load i32, i32* @stdout
%8 = tail call i32 @getc(i32 %7)
%9 = load i32, i32* @STDIN_FILENO
%10 = call i32 @tcgetattr(i32 %9, %struct.termios* %2)
%11 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%12 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%13 = load i32, i32* %11
store i32 %13, i32* %12
%14 = load i32, i32* @ICANON
%15 = load i32, i32* @ECHO
%16 = or i32 %15, %14
%17 = xor i32 %16, -1
store i32 %17, i32* %11
%18 = load i32, i32* @STDIN_FILENO
%19 = load i32, i32* @TCSANOW
%20 = call i32 @tcsetattr(i32 %18, i32 %19, %struct.termios* %2)
%21 = load i32, i32* @STDIN_FILENO
%22 = load i32, i32* @FIONREAD
%23 = call i32 @ioctl(i32 %21, i32 %22, i32* %1)
%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* %1
ret i32 %27
}