#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*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int ICANON;
int O_NONBLOCK;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, int);
int getchar();
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int console_getch(void) {
  struct termios oldt, newt;
  int ch;
  int oldf;
  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
  ch = getchar();
  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  fcntl(STDIN_FILENO, F_SETFL, oldf);
  return ch;
}


x86 assembly (clang, -O0):
<console_getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    0x2e68(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    0x2e1a(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
call   1030 <fcntl@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<console_getch>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e63(%rip),%r15
mov    (%r15),%edi
lea    0x10(%rsp),%r14
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
mov    0x2e44(%rip),%rax
mov    0x2e65(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    0x10(%rsp),%ecx
mov    %ecx,0x8(%rsp)
mov    (%r15),%edi
mov    0x2e55(%rip),%r12
mov    (%r12),%esi
lea    0x8(%rsp),%rdx
call   1060 <tcsetattr@plt>
mov    (%r15),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,%ebp
mov    (%r15),%edi
mov    0x2e10(%rip),%r13
mov    0x0(%r13),%esi
mov    0x2e25(%rip),%rax
mov    (%rax),%edx
or     %ebp,%edx
call   1030 <fcntl@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %eax,%ebx
mov    (%r15),%edi
mov    (%r12),%esi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
mov    (%r15),%edi
mov    0x0(%r13),%esi
mov    %ebp,%edx
call   1030 <fcntl@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@F_GETFL = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @console_getch() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = alloca i32
%4 = alloca i32
%5 = load i32, i32* @STDIN_FILENO
%6 = call i32 @tcgetattr(i32 %5, %struct.termios* %1)
%7 = bitcast %struct.termios* %2 to i8*
%8 = bitcast %struct.termios* %1 to i8*
%9 = load i32, i32* @ICANON
%10 = load i32, i32* @ECHO
%11 = or i32 %9, %10
%12 = xor i32 %11, -1
%13 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 0
%14 = load i32, i32* %13
%15 = and i32 %14, %12
store i32 %15, i32* %13
%16 = load i32, i32* @STDIN_FILENO
%17 = load i32, i32* @TCSANOW
%18 = call i32 @tcsetattr(i32 %16, i32 %17, %struct.termios* %2)
%19 = load i32, i32* @STDIN_FILENO
%20 = load i32, i32* @F_GETFL
%21 = call i32 @fcntl(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %4
%22 = load i32, i32* @STDIN_FILENO
%23 = load i32, i32* @F_SETFL
%24 = load i32, i32* %4
%25 = load i32, i32* @O_NONBLOCK
%26 = or i32 %24, %25
%27 = call i32 @fcntl(i32 %22, i32 %23, i32 %26)
%28 = call i32 (...) @getchar()
store i32 %28, i32* %3
%29 = load i32, i32* @STDIN_FILENO
%30 = load i32, i32* @TCSANOW
%31 = call i32 @tcsetattr(i32 %29, i32 %30, %struct.termios* %1)
%32 = load i32, i32* @STDIN_FILENO
%33 = load i32, i32* @F_SETFL
%34 = load i32, i32* %4
%35 = call i32 @fcntl(i32 %32, i32 %33, i32 %34)
%36 = load i32, i32* %3
ret i32 %36
}

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