#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;
scalar_t__ getchar();
int /*<<< orphan*/ memcpy(struct termios *, struct termios *, int);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
char getche(void) {
  char c;
  struct termios oldattr, newattr;
  tcgetattr(STDIN_FILENO, &oldattr);
  memcpy(&newattr, &oldattr, sizeof(struct termios));
  newattr.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newattr);
  c = (char)getchar();
  tcsetattr(STDIN_FILENO, TCSANOW, &oldattr);
  return (c);
}


x86 assembly (clang, -O0):
<getche>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1050 <tcgetattr@plt>
lea    -0x10(%rbp),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    0x2e64(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %al,-0x1(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1060 <tcsetattr@plt>
movsbl -0x1(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getche>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e7e(%rip),%r15
mov    (%r15),%edi
lea    0x10(%rsp),%r14
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    0x2e4a(%rip),%rax
mov    0x2e5b(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    %ecx,0x8(%rsp)
mov    (%r15),%edi
mov    0x2e4f(%rip),%r12
mov    (%r12),%esi
mov    %rbx,%rdx
call   1060 <tcsetattr@plt>
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %rax,%rbx
mov    (%r15),%edi
mov    (%r12),%esi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
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
define signext i8 @getche() {
%1 = alloca i8
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = load i32, i32* @STDIN_FILENO
%5 = call i32 @tcgetattr(i32 %4, %struct.termios* %2)
%6 = call i32 @memcpy(%struct.termios* %3, %struct.termios* %2, i32 4)
%7 = load i32, i32* @ICANON
%8 = load i32, i32* @ECHO
%9 = or i32 %7, %8
%10 = xor i32 %9, -1
%11 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%12 = load i32, i32* %11
%13 = and i32 %12, %10
store i32 %13, i32* %11
%14 = load i32, i32* @STDIN_FILENO
%15 = load i32, i32* @TCSANOW
%16 = call i32 @tcsetattr(i32 %14, i32 %15, %struct.termios* %3)
%17 = call i64 (...) @getchar()
%18 = trunc i64 %17 to i8
store i8 %18, i8* %1
%19 = load i32, i32* @STDIN_FILENO
%20 = load i32, i32* @TCSANOW
%21 = call i32 @tcsetattr(i32 %19, i32 %20, %struct.termios* %2)
%22 = load i8, i8* %1
ret i8 %22
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define signext i8 @getche() {
%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 = call i32 @memcpy(%struct.termios* %2, %struct.termios* %1, i32 4)
%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 = load i32, i32* @STDIN_FILENO
%16 = load i32, i32* @TCSANOW
%17 = call i32 @tcsetattr(i32 %15, i32 %16, %struct.termios* %2)
%18 = call i64 (...) @getchar()
%19 = trunc i64 %18 to i8
%20 = load i32, i32* @STDIN_FILENO
%21 = load i32, i32* @TCSANOW
%22 = call i32 @tcsetattr(i32 %20, i32 %21, %struct.termios* %1)
ret i8 %19
}