#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*/ TCSANOW;
int fileno(int /*<<< orphan*/);
int getchar();
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int getch() {
  static int ch = -1, fd = 0;
  struct termios neu, alt;
  fd = fileno(stdin);
  tcgetattr(fd, &alt);
  neu = alt;
  neu.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(fd, TCSANOW, &neu);
  ch = getchar();
  tcsetattr(fd, TCSANOW, &alt);
  return ch;
}


x86 assembly (clang, -O0):
<getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%edi
call   1040 <fileno@plt>
mov    %eax,0x2eec(%rip)
mov    0x2ee6(%rip),%edi
lea    -0x10(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
mov    0x2e57(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    0x2eb6(%rip),%edi
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,0x2e8f(%rip)
mov    0x2e91(%rip),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    0x2e71(%rip),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e9a(%rip),%rax
mov    (%rax),%edi
call   1040 <fileno@plt>
mov    %eax,0x2eed(%rip)
lea    0x8(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
mov    0x2e57(%rip),%rax
mov    0x2e60(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    0x8(%rsp),%ecx
mov    %ecx,0x10(%rsp)
mov    0x2ebc(%rip),%edi
mov    0x2e4d(%rip),%rbx
mov    (%rbx),%esi
lea    0x10(%rsp),%rdx
call   1060 <tcsetattr@plt>
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,0x2e94(%rip)
mov    0x2e96(%rip),%edi
mov    (%rbx),%esi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
mov    0x2e7e(%rip),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@getch.ch = internal global i32 -1
@getch.fd = internal global i32 0
@stdin = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define i32 @getch() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = load i32, i32* @stdin
%4 = call i32 @fileno(i32 %3)
store i32 %4, i32* @getch.fd
%5 = load i32, i32* @getch.fd
%6 = call i32 @tcgetattr(i32 %5, %struct.termios* %2)
%7 = bitcast %struct.termios* %1 to i8*
%8 = bitcast %struct.termios* %2 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* %1, i32 0, i32 0
%14 = load i32, i32* %13
%15 = and i32 %14, %12
store i32 %15, i32* %13
%16 = load i32, i32* @getch.fd
%17 = load i32, i32* @TCSANOW
%18 = call i32 @tcsetattr(i32 %16, i32 %17, %struct.termios* %1)
%19 = call i32 (...) @getchar()
store i32 %19, i32* @getch.ch
%20 = load i32, i32* @getch.fd
%21 = load i32, i32* @TCSANOW
%22 = call i32 @tcsetattr(i32 %20, i32 %21, %struct.termios* %2)
%23 = load i32, i32* @getch.ch
ret i32 %23
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@getch.ch = internal unnamed_addr global i32 -1
@getch.fd = internal unnamed_addr global i32 0
@stdin = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define i32 @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
%6 = tail call i32 @fileno(i32 %5)
store i32 %6, i32* @getch.fd
%7 = call i32 @tcgetattr(i32 %6, %struct.termios* %2)
%8 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%9 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%10 = load i32, i32* %8
%11 = load i32, i32* @ICANON
%12 = load i32, i32* @ECHO
%13 = or i32 %12, %11
%14 = xor i32 %13, -1
%15 = and i32 %10, %14
store i32 %15, i32* %9
%16 = load i32, i32* @getch.fd
%17 = load i32, i32* @TCSANOW
%18 = call i32 @tcsetattr(i32 %16, i32 %17, %struct.termios* %1)
%19 = call i32 (...) @getchar()
store i32 %19, i32* @getch.ch
%20 = load i32, i32* @getch.fd
%21 = load i32, i32* @TCSANOW
%22 = call i32 @tcsetattr(i32 %20, i32 %21, %struct.termios* %2)
%23 = load i32, i32* @getch.ch
ret i32 %23
}