#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;
  scalar_t__ *c_cc;
};

/* Variables and functions */
int ECHO;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int ICANON;
int O_NONBLOCK;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
int getchar();
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int getasynckey() {
  struct termios old_termios;
  struct termios new_termios;
  int ch;
  int old_fcntl;
  tcgetattr(fileno(stdin), &old_termios);
  new_termios = old_termios;
  new_termios.c_lflag &= ~(ICANON | ECHO);
  new_termios.c_cc[VTIME] = 0;
  new_termios.c_cc[VMIN] = 0;
  tcsetattr(fileno(stdin), TCSANOW, &new_termios);
  old_fcntl = fcntl(0, F_GETFL, 0);
  fcntl(0, F_SETFL, old_fcntl | O_NONBLOCK);
  ch = getchar();
  tcsetattr(fileno(stdin), TCSANOW, &old_termios);
  fcntl(0, F_SETFL, old_fcntl);
  return ch;
}


x86 assembly (clang, -O0):
<getasynckey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
call   1050 <fileno@plt>
mov    %eax,%edi
lea    -0x10(%rbp),%rsi
call   1060 <tcgetattr@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    0x2e3f(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e01(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
mov    0x2e0b(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1050 <fileno@plt>
mov    %eax,%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
mov    %edx,%edi
call   1030 <fcntl@plt>
mov    %eax,-0x28(%rbp)
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%edx
mov    0x2dd6(%rip),%rax
or     (%rax),%edx
xor    %edi,%edi
call   1030 <fcntl@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x24(%rbp)
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
call   1050 <fileno@plt>
mov    %eax,%edi
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%edx
xor    %edi,%edi
call   1030 <fcntl@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getasynckey>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e7b(%rip),%r15
mov    (%r15),%edi
call   1050 <fileno@plt>
lea    0x18(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
call   1060 <tcgetattr@plt>
movups 0x18(%rsp),%xmm0
movaps %xmm0,(%rsp)
mov    0x2e14(%rip),%rax
mov    0x2e3d(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    %ecx,(%rsp)
mov    0x8(%rsp),%rax
mov    0x2e00(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    0x2e0e(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    (%r15),%edi
call   1050 <fileno@plt>
mov    0x2e04(%rip),%r12
mov    (%r12),%esi
mov    %rsp,%rdx
mov    %eax,%edi
call   1070 <tcsetattr@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,%ebp
mov    0x2dbb(%rip),%r13
mov    0x0(%r13),%esi
mov    0x2de0(%rip),%rax
mov    (%rax),%edx
or     %ebp,%edx
xor    %edi,%edi
call   1030 <fcntl@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %eax,%ebx
mov    (%r15),%edi
call   1050 <fileno@plt>
mov    (%r12),%esi
mov    %eax,%edi
mov    %r14,%rdx
call   1070 <tcsetattr@plt>
mov    0x0(%r13),%esi
xor    %edi,%edi
mov    %ebp,%edx
call   1030 <fcntl@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i64* }
@stdin = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCSANOW = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @getasynckey() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* @stdin
%6 = call i32 @fileno(i32 %5)
%7 = call i32 @tcgetattr(i32 %6, %struct.termios* %1)
%8 = bitcast %struct.termios* %2 to i8*
%9 = bitcast %struct.termios* %1 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
%15 = load i32, i32* %14
%16 = and i32 %15, %13
store i32 %16, i32* %14
%17 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 1
%18 = load i64*, i64** %17
%19 = load i64, i64* @VTIME
%20 = getelementptr inbounds i64, i64* %18, i64 %19
store i64 0, i64* %20
%21 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 1
%22 = load i64*, i64** %21
%23 = load i64, i64* @VMIN
%24 = getelementptr inbounds i64, i64* %22, i64 %23
store i64 0, i64* %24
%25 = load i32, i32* @stdin
%26 = call i32 @fileno(i32 %25)
%27 = load i32, i32* @TCSANOW
%28 = call i32 @tcsetattr(i32 %26, i32 %27, %struct.termios* %2)
%29 = load i32, i32* @F_GETFL
%30 = call i32 @fcntl(i32 0, i32 %29, i32 0)
store i32 %30, i32* %4
%31 = load i32, i32* @F_SETFL
%32 = load i32, i32* %4
%33 = load i32, i32* @O_NONBLOCK
%34 = or i32 %32, %33
%35 = call i32 @fcntl(i32 0, i32 %31, i32 %34)
%36 = call i32 (...) @getchar()
store i32 %36, i32* %3
%37 = load i32, i32* @stdin
%38 = call i32 @fileno(i32 %37)
%39 = load i32, i32* @TCSANOW
%40 = call i32 @tcsetattr(i32 %38, i32 %39, %struct.termios* %1)
%41 = load i32, i32* @F_SETFL
%42 = load i32, i32* %4
%43 = call i32 @fcntl(i32 0, i32 %41, i32 %42)
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
%struct.termios = type { i32, i64* }
@stdin = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCSANOW = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @getasynckey() {
%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
%6 = tail call i32 @fileno(i32 %5)
%7 = call i32 @tcgetattr(i32 %6, %struct.termios* %1)
%8 = load i32, i32* @ICANON
%9 = load i32, i32* @ECHO
%10 = or i32 %9, %8
%11 = xor i32 %10, -1
%12 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = and i32 %13, %11
store i32 %14, i32* %12
%15 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%16 = load i64*, i64** %15
%17 = load i64, i64* @VTIME
%18 = getelementptr inbounds i64, i64* %16, i64 %17
store i64 0, i64* %18
%19 = load i64, i64* @VMIN
%20 = getelementptr inbounds i64, i64* %16, i64 %19
store i64 0, i64* %20
%21 = load i32, i32* @stdin
%22 = call i32 @fileno(i32 %21)
%23 = load i32, i32* @TCSANOW
%24 = call i32 @tcsetattr(i32 %22, i32 %23, %struct.termios* %2)
%25 = load i32, i32* @F_GETFL
%26 = call i32 @fcntl(i32 0, i32 %25, i32 0)
%27 = load i32, i32* @F_SETFL
%28 = load i32, i32* @O_NONBLOCK
%29 = or i32 %28, %26
%30 = call i32 @fcntl(i32 0, i32 %27, i32 %29)
%31 = call i32 (...) @getchar()
%32 = load i32, i32* @stdin
%33 = call i32 @fileno(i32 %32)
%34 = load i32, i32* @TCSANOW
%35 = call i32 @tcsetattr(i32 %33, i32 %34, %struct.termios* %1)
%36 = load i32, i32* @F_SETFL
%37 = call i32 @fcntl(i32 0, i32 %36, i32 %26)
ret i32 %31
}