#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 */
typedef int /*<<< orphan*/ term;
struct termios {
  int c_lflag;
  int *c_cc;
};

/* Variables and functions */
int ECHO;
int ICANON;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int 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 *);
int getch(void) {
  struct termios term, oterm;
  int c = 0;
  tcgetattr(0, &oterm);
  memcpy(&term, &oterm, sizeof(term));
  term.c_lflag &= ~(ICANON | ECHO);
  term.c_cc[VMIN] = 1;
  term.c_cc[VTIME] = 0;
  tcsetattr(0, TCSANOW, &term);
  c = getchar();
  tcsetattr(0, TCSANOW, &oterm);
  return c;
}


x86 assembly (clang, -O0):
<getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x24(%rbp)
xor    %edi,%edi
lea    -0x20(%rbp),%rsi
call   1050 <tcgetattr@plt>
lea    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   1040 <memcpy@plt>
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    0x2e64(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    0x2e46(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x8(%rbp),%rax
mov    0x2e21(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x10(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x20(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
lea    0x10(%rsp),%r14
xor    %edi,%edi
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x10,%edx
call   1040 <memcpy@plt>
mov    0x2e4e(%rip),%rax
mov    0x2e67(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    %ecx,(%rsp)
mov    0x8(%rsp),%rax
mov    0x2e4a(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e29(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e38(%rip),%r15
mov    (%r15),%esi
xor    %edi,%edi
mov    %rbx,%rdx
call   1060 <tcsetattr@plt>
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ebx
mov    (%r15),%esi
xor    %edi,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32* }
@ICANON = global i32 0
@ECHO = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
define i32 @getch() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = alloca i32
store i32 0, i32* %3
%4 = call i32 @tcgetattr(i32 0, %struct.termios* %2)
%5 = call i32 @memcpy(%struct.termios* %1, %struct.termios* %2, i32 16)
%6 = load i32, i32* @ICANON
%7 = load i32, i32* @ECHO
%8 = or i32 %6, %7
%9 = xor i32 %8, -1
%10 = getelementptr inbounds %struct.termios, %struct.termios* %1, i32 0, i32 0
%11 = load i32, i32* %10
%12 = and i32 %11, %9
store i32 %12, i32* %10
%13 = getelementptr inbounds %struct.termios, %struct.termios* %1, i32 0, i32 1
%14 = load i32*, i32** %13
%15 = load i64, i64* @VMIN
%16 = getelementptr inbounds i32, i32* %14, i64 %15
store i32 1, i32* %16
%17 = getelementptr inbounds %struct.termios, %struct.termios* %1, i32 0, i32 1
%18 = load i32*, i32** %17
%19 = load i64, i64* @VTIME
%20 = getelementptr inbounds i32, i32* %18, i64 %19
store i32 0, i32* %20
%21 = load i32, i32* @TCSANOW
%22 = call i32 @tcsetattr(i32 0, i32 %21, %struct.termios* %1)
%23 = call i32 (...) @getchar()
store i32 %23, i32* %3
%24 = load i32, i32* @TCSANOW
%25 = call i32 @tcsetattr(i32 0, i32 %24, %struct.termios* %2)
%26 = load i32, i32* %3
ret i32 %26
}

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