#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 timeval {
  int /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};
struct termios {
  int c_lflag;
  int *c_cc;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int ECHO;
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int ICANON;
int ISIG;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ isatty(int /*<<< orphan*/);
int select(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
scalar_t__ tcgetattr(int /*<<< orphan*/, struct termios *);
scalar_t__ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
int kbhit(void) {
  struct termios save_termios;
  struct termios ios;
  fd_set inp;
  struct timeval timeout = {0, 0};
  int result;
  if (!isatty(0))
    return 0;
  if (tcgetattr(0, &save_termios) < 0)
    return 0;
  ios = save_termios;
  ios.c_lflag &= ~(ICANON | ECHO | ISIG);
  ios.c_cc[VMIN] = 1;
  ios.c_cc[VTIME] = 0;
  if (tcsetattr(0, TCSANOW, &ios) < 0)
    return 0;
  FD_ZERO(&inp);
  FD_SET(0, &inp);
  result = select(0 + 1, &inp, NULL, NULL, &timeout) == 1;
  tcsetattr(0, TCSANOW, &save_termios);
  return result;
}


x86 assembly (clang, -O0):
<kbhit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
xor    %edi,%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    11a4 <kbhit+0x34>
movl   $0x0,-0x4(%rbp)
jmp    12a2 <kbhit+0x132>
xor    %edi,%edi
lea    -0x18(%rbp),%rsi
call   1070 <tcgetattr@plt>
cmp    $0x0,%rax
jge    11c5 <kbhit+0x55>
movl   $0x0,-0x4(%rbp)
jmp    12a2 <kbhit+0x132>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    0x2dfb(%rip),%rcx
or     (%rcx),%eax
mov    0x2dea(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2dcc(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
mov    0x2da7(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x28(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmp    $0x0,%rax
jge    124d <kbhit+0xdd>
movl   $0x0,-0x4(%rbp)
jmp    12a2 <kbhit+0x132>
lea    -0x2c(%rbp),%rdi
call   1050 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x2c(%rbp),%rsi
call   1090 <FD_SET@plt>
mov    $0x1,%edi
lea    -0x2c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
cmp    $0x1,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x3c(%rbp)
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1080 <tcsetattr@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kbhit>:
push   %r14
push   %rbx
sub    $0x38,%rsp
movq   $0x0,0x18(%rsp)
xor    %ebx,%ebx
xor    %edi,%edi
call   1030 <isatty@plt>
test   %eax,%eax
je     1247 <kbhit+0xe7>
xor    %ebx,%ebx
lea    0x28(%rsp),%rsi
xor    %edi,%edi
call   1060 <tcgetattr@plt>
test   %rax,%rax
js     1247 <kbhit+0xe7>
movups 0x28(%rsp),%xmm0
movaps %xmm0,(%rsp)
mov    0x2e10(%rip),%rax
mov    0x2e31(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e1e(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,(%rsp)
mov    0x8(%rsp),%rax
mov    0x2e03(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2de2(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2df9(%rip),%r14
mov    (%r14),%esi
xor    %ebx,%ebx
mov    %rsp,%rdx
xor    %edi,%edi
call   1070 <tcsetattr@plt>
test   %rax,%rax
js     1247 <kbhit+0xe7>
lea    0x24(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <FD_ZERO@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1080 <FD_SET@plt>
lea    0x18(%rsp),%r8
mov    $0x1,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
xor    %ebx,%ebx
cmp    $0x1,%eax
sete   %bl
mov    (%r14),%esi
lea    0x28(%rsp),%rdx
xor    %edi,%edi
call   1070 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32* }
%struct.timeval = type { i32, i32 }
@ICANON = global i32 0
@ECHO = global i32 0
@ISIG = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
define i32 @kbhit() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = bitcast %struct.timeval* %5 to i8*
%8 = call i32 @isatty(i32 0)
%9 = icmp ne i32 %8, 0
br i1 %9, label %11, label %10
10: ; preds = %0
store i32 0, i32* %1
br label %48
11: ; preds = %0
%12 = call i64 @tcgetattr(i32 0, %struct.termios* %2)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %11
store i32 0, i32* %1
br label %48
15: ; preds = %11
%16 = bitcast %struct.termios* %3 to i8*
%17 = bitcast %struct.termios* %2 to i8*
%18 = load i32, i32* @ICANON
%19 = load i32, i32* @ECHO
%20 = or i32 %18, %19
%21 = load i32, i32* @ISIG
%22 = or i32 %20, %21
%23 = xor i32 %22, -1
%24 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%25 = load i32, i32* %24
%26 = and i32 %25, %23
store i32 %26, i32* %24
%27 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%28 = load i32*, i32** %27
%29 = load i64, i64* @VMIN
%30 = getelementptr inbounds i32, i32* %28, i64 %29
store i32 1, i32* %30
%31 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%32 = load i32*, i32** %31
%33 = load i64, i64* @VTIME
%34 = getelementptr inbounds i32, i32* %32, i64 %33
store i32 0, i32* %34
%35 = load i32, i32* @TCSANOW
%36 = call i64 @tcsetattr(i32 0, i32 %35, %struct.termios* %3)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %15
store i32 0, i32* %1
br label %48
39: ; preds = %15
%40 = call i32 @FD_ZERO(i32* %4)
%41 = call i32 @FD_SET(i32 0, i32* %4)
%42 = call i32 @select(i64 1, i32* %4, i32* null, i32* null, %struct.timeval* %5)
%43 = icmp eq i32 %42, 1
%44 = zext i1 %43 to i32
store i32 %44, i32* %6
%45 = load i32, i32* @TCSANOW
%46 = call i64 @tcsetattr(i32 0, i32 %45, %struct.termios* %2)
%47 = load i32, i32* %6
store i32 %47, i32* %1
br label %48
48: ; preds = %39, %38, %14, %10
%49 = load i32, i32* %1
ret i32 %49
}

LLVM IR (-O2):
%struct.termios = type { i32, i32* }
%struct.timeval = type { i32, i32 }
@ICANON = global i32 0
@ECHO = global i32 0
@ISIG = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
define i32 @kbhit() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = alloca i32
%4 = alloca i64
%5 = bitcast i64* %4 to %struct.timeval*
%6 = bitcast %struct.termios* %1 to i8*
%7 = bitcast %struct.termios* %2 to i8*
%8 = bitcast i32* %3 to i8*
%9 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%10 = tail call i32 @isatty(i32 0)
%11 = icmp eq i32 %10, 0
br i1 %11, label %42, label %12
12: ; preds = %0
%13 = call i64 @tcgetattr(i32 0, %struct.termios* %1)
%14 = icmp slt i64 %13, 0
br i1 %14, label %42, label %15
15: ; preds = %12
%16 = load i32, i32* @ICANON
%17 = load i32, i32* @ECHO
%18 = or i32 %17, %16
%19 = load i32, i32* @ISIG
%20 = or i32 %18, %19
%21 = xor i32 %20, -1
%22 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%23 = load i32, i32* %22
%24 = and i32 %23, %21
store i32 %24, i32* %22
%25 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%26 = load i32*, i32** %25
%27 = load i64, i64* @VMIN
%28 = getelementptr inbounds i32, i32* %26, i64 %27
store i32 1, i32* %28
%29 = load i64, i64* @VTIME
%30 = getelementptr inbounds i32, i32* %26, i64 %29
store i32 0, i32* %30
%31 = load i32, i32* @TCSANOW
%32 = call i64 @tcsetattr(i32 0, i32 %31, %struct.termios* %2)
%33 = icmp slt i64 %32, 0
br i1 %33, label %42, label %34
34: ; preds = %15
%35 = call i32 @FD_ZERO(i32* %3)
%36 = call i32 @FD_SET(i32 0, i32* %3)
%37 = call i32 @select(i64 1, i32* %3, i32* null, i32* null, %struct.timeval* %5)
%38 = icmp eq i32 %37, 1
%39 = zext i1 %38 to i32
%40 = load i32, i32* @TCSANOW
%41 = call i64 @tcsetattr(i32 0, i32 %40, %struct.termios* %1)
br label %42
42: ; preds = %15, %12, %0, %34
%43 = phi i32 [ %39, %34 ], [ 0, %0 ], [ 0, %12 ], [ 0, %15 ]
ret i32 %43
}