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

/* Variables and functions */
int /*<<< orphan*/ ICANON;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
char getchar();
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int keydown() {
  char c;
  struct termios new, stored;
  tcgetattr(0, &stored);
  new = stored;
  new.c_lflag &= (~ICANON);
  new.c_cc[VTIME] = 0;
  tcgetattr(0, &stored);
  new.c_cc[VMIN] = 1;
  tcsetattr(0, TCSANOW, &new);
  c = getchar();
  tcsetattr(0, TCSANOW, &stored);
  return c;
}


x86 assembly (clang, -O0):
<keydown>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
xor    %edi,%edi
lea    -0x28(%rbp),%rsi
call   1040 <tcgetattr@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e60(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
xor    %edi,%edi
lea    -0x28(%rbp),%rsi
call   1040 <tcgetattr@plt>
mov    -0x18(%rbp),%rax
mov    0x2e50(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1050 <tcsetattr@plt>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %al,-0x1(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x28(%rbp),%rdx
call   1050 <tcsetattr@plt>
movsbl -0x1(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<keydown>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
lea    0x10(%rsp),%r14
xor    %edi,%edi
mov    %r14,%rsi
call   1040 <tcgetattr@plt>
movups 0x10(%rsp),%xmm0
movaps %xmm0,(%rsp)
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,0x8(%rsp)
mov    (%rsp),%rbx
mov    0x2e65(%rip),%rax
mov    (%rax),%rax
movl   $0x0,(%rbx,%rax,4)
xor    %edi,%edi
mov    %r14,%rsi
call   1040 <tcgetattr@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%rax
movl   $0x1,(%rbx,%rax,4)
mov    0x2e51(%rip),%r15
mov    (%r15),%esi
mov    %rsp,%rdx
xor    %edi,%edi
call   1050 <tcsetattr@plt>
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ebx
mov    (%r15),%esi
xor    %edi,%edi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
movsbl %bl,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32*, i32 }
@ICANON = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCSANOW = global i32 0
define i32 @keydown() {
%1 = alloca i8
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = call i32 @tcgetattr(i32 0, %struct.termios* %3)
%5 = bitcast %struct.termios* %2 to i8*
%6 = bitcast %struct.termios* %3 to i8*
%7 = load i32, i32* @ICANON
%8 = xor i32 %7, -1
%9 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 1
%10 = load i32, i32* %9
%11 = and i32 %10, %8
store i32 %11, i32* %9
%12 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 0
%13 = load i32*, i32** %12
%14 = load i64, i64* @VTIME
%15 = getelementptr inbounds i32, i32* %13, i64 %14
store i32 0, i32* %15
%16 = call i32 @tcgetattr(i32 0, %struct.termios* %3)
%17 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 0
%18 = load i32*, i32** %17
%19 = load i64, i64* @VMIN
%20 = getelementptr inbounds i32, i32* %18, i64 %19
store i32 1, i32* %20
%21 = load i32, i32* @TCSANOW
%22 = call i32 @tcsetattr(i32 0, i32 %21, %struct.termios* %2)
%23 = call signext i8 (...) @getchar()
store i8 %23, i8* %1
%24 = load i32, i32* @TCSANOW
%25 = call i32 @tcsetattr(i32 0, i32 %24, %struct.termios* %3)
%26 = load i8, i8* %1
%27 = sext i8 %26 to i32
ret i32 %27
}

LLVM IR (-O2):
%struct.termios = type { i32*, i32 }
@ICANON = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCSANOW = global i32 0
define i32 @keydown() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %1 to i8*
%4 = bitcast %struct.termios* %2 to i8*
%5 = call i32 @tcgetattr(i32 0, %struct.termios* %2)
%6 = load i32, i32* @ICANON
%7 = xor i32 %6, -1
%8 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 1
%9 = load i32, i32* %8
%10 = and i32 %9, %7
store i32 %10, i32* %8
%11 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%12 = load i32*, i32** %11
%13 = load i64, i64* @VTIME
%14 = getelementptr inbounds i32, i32* %12, i64 %13
store i32 0, i32* %14
%15 = call i32 @tcgetattr(i32 0, %struct.termios* %2)
%16 = load i64, i64* @VMIN
%17 = getelementptr inbounds i32, i32* %12, i64 %16
store i32 1, i32* %17
%18 = load i32, i32* @TCSANOW
%19 = call i32 @tcsetattr(i32 0, i32 %18, %struct.termios* %1)
%20 = call signext i8 (...) @getchar()
%21 = load i32, i32* @TCSANOW
%22 = call i32 @tcsetattr(i32 0, i32 %21, %struct.termios* %2)
%23 = sext i8 %20 to i32
ret i32 %23
}