#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 ICANON;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
int getchar();
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int getch() {
  static struct termios oldt, newt;
  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  int retVal = getchar();
  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  return (retVal);
}


x86 assembly (clang, -O0):
<getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
lea    0x2ef4(%rip),%rsi
call   1040 <tcgetattr@plt>
mov    0x2ee9(%rip),%eax
mov    %eax,0x2ee7(%rip)
mov    0x2e68(%rip),%rax
mov    (%rax),%eax
mov    0x2e77(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    0x2ecc(%rip),%eax
mov    %eax,0x2ec6(%rip)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
lea    0x2ead(%rip),%rdx
call   1050 <tcsetattr@plt>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,-0x4(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
lea    0x2e81(%rip),%rdx
call   1050 <tcsetattr@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e92(%rip),%r15
mov    (%r15),%edi
lea    0x2ef4(%rip),%r14
mov    %r14,%rsi
call   1040 <tcgetattr@plt>
mov    0x2e71(%rip),%rax
mov    0x2e82(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    0x2ed2(%rip),%ecx
mov    %ecx,0x2ed0(%rip)
lea    0x2ec9(%rip),%rdx
mov    (%r15),%edi
mov    0x2e67(%rip),%rbx
mov    (%rbx),%esi
call   1050 <tcsetattr@plt>
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ebp
mov    (%r15),%edi
mov    (%rbx),%esi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@getch.oldt = internal global %struct.termios zeroinitializer
@getch.newt = internal global %struct.termios zeroinitializer
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define i32 @getch() {
%1 = alloca i32
%2 = load i32, i32* @STDIN_FILENO
%3 = call i32 @tcgetattr(i32 %2, %struct.termios* @getch.oldt)
%4 = load i32, i32* @ICANON
%5 = load i32, i32* @ECHO
%6 = or i32 %4, %5
%7 = xor i32 %6, -1
%8 = load i32, i32* getelementptr inbounds (%struct.termios, %struct.termios* @getch.newt, i32 0, i32 0)
%9 = and i32 %8, %7
store i32 %9, i32* getelementptr inbounds (%struct.termios, %struct.termios* @getch.newt, i32 0, i32 0)
%10 = load i32, i32* @STDIN_FILENO
%11 = load i32, i32* @TCSANOW
%12 = call i32 @tcsetattr(i32 %10, i32 %11, %struct.termios* @getch.newt)
%13 = call i32 (...) @getchar()
store i32 %13, i32* %1
%14 = load i32, i32* @STDIN_FILENO
%15 = load i32, i32* @TCSANOW
%16 = call i32 @tcsetattr(i32 %14, i32 %15, %struct.termios* @getch.oldt)
%17 = load i32, i32* %1
ret i32 %17
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@getch.oldt = internal global %struct.termios zeroinitializer
@getch.newt = internal global %struct.termios zeroinitializer
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define i32 @getch() {
%1 = load i32, i32* @STDIN_FILENO
%2 = tail call i32 @tcgetattr(i32 %1, %struct.termios* @getch.oldt)
%3 = load i32, i32* getelementptr inbounds (%struct.termios, %struct.termios* @getch.oldt, i64 0, i32 0)
%4 = load i32, i32* @ICANON
%5 = load i32, i32* @ECHO
%6 = or i32 %5, %4
%7 = xor i32 %6, -1
%8 = and i32 %3, %7
store i32 %8, i32* getelementptr inbounds (%struct.termios, %struct.termios* @getch.newt, i64 0, i32 0)
%9 = load i32, i32* @STDIN_FILENO
%10 = load i32, i32* @TCSANOW
%11 = tail call i32 @tcsetattr(i32 %9, i32 %10, %struct.termios* @getch.newt)
%12 = tail call i32 (...) @getchar()
%13 = load i32, i32* @STDIN_FILENO
%14 = load i32, i32* @TCSANOW
%15 = tail call i32 @tcsetattr(i32 %13, i32 %14, %struct.termios* @getch.oldt)
ret i32 %12
}