#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_cc;
  int /*<<< orphan*/ c_lflag;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ ECHO;
int /*<<< orphan*/ ICANON;
int /*<<< orphan*/ TCSADRAIN;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stdout;
scalar_t__ tcgetattr(int /*<<< orphan*/, struct termios *);
scalar_t__ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
char getch() {
  char buf = 0;
  struct termios old = {0};
  fflush(stdout);
  if (tcgetattr(0, &old) < 0)
    perror("tcsetattr()");
  old.c_lflag &= ~ICANON;
  old.c_lflag &= ~ECHO;
  old.c_cc[VMIN] = 1;
  old.c_cc[VTIME] = 0;
  if (tcsetattr(0, TCSANOW, &old) < 0)
    perror("tcsetattr ICANON");
  if (read(0, &buf, 1) < 0)
    perror("read()");
  old.c_lflag |= ICANON;
  old.c_lflag |= ECHO;
  if (tcsetattr(0, TCSADRAIN, &old) < 0)
    perror("tcsetattr ~ICANON");
  return buf;
}


x86 assembly (clang, -O0):
<getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movb   $0x0,-0x1(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
xor    %edi,%edi
lea    -0x18(%rbp),%rsi
call   1060 <tcgetattr@plt>
cmp    $0x0,%rax
jge    11ab <getch+0x4b>
lea    0xe5a(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2dfe(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
mov    0x2dc9(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2de8(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0x0,%rax
jge    1223 <getch+0xc3>
lea    0xdee(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
lea    -0x1(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jge    1249 <getch+0xe9>
lea    0xdd9(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d81(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0x0,%rax
jge    1291 <getch+0x131>
lea    0xd98(%rip),%rdi
call   1080 <perror@plt>
movsbl -0x1(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %r14
push   %rbx
sub    $0x28,%rsp
movb   $0x0,0xf(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
lea    0x10(%rsp),%rsi
xor    %edi,%edi
call   1050 <tcgetattr@plt>
test   %rax,%rax
js     122a <getch+0xda>
mov    0x2e22(%rip),%rbx
mov    (%rbx),%eax
mov    0x2e49(%rip),%r14
or     (%r14),%eax
not    %eax
and    %eax,0x18(%rsp)
mov    0x10(%rsp),%rax
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2dfb(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
xor    %edi,%edi
call   1060 <tcsetattr@plt>
test   %rax,%rax
js     123b <getch+0xeb>
lea    0xf(%rsp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
js     1249 <getch+0xf9>
mov    0x18(%rsp),%eax
or     (%rbx),%eax
or     (%r14),%eax
mov    %eax,0x18(%rsp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
xor    %edi,%edi
call   1060 <tcsetattr@plt>
test   %rax,%rax
js     1257 <getch+0x107>
mov    0xf(%rsp),%al
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdcf(%rip),%rdi
call   1070 <perror@plt>
jmp    1187 <getch+0x37>
lea    0xdca(%rip),%rdi
call   1070 <perror@plt>
jmp    11e1 <getch+0x91>
lea    0xdcd(%rip),%rdi
call   1070 <perror@plt>
jmp    11f7 <getch+0xa7>
lea    0xdc6(%rip),%rdi
call   1070 <perror@plt>
jmp    121e <getch+0xce>

LLVM IR (-O0):
%struct.termios = type { i32*, i32, i32 }
@stdout = global i32 0
@.str = [12 x i8] c"tcsetattr()\00"
@ICANON = global i32 0
@ECHO = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@.str.1 = [17 x i8] c"tcsetattr ICANON\00"
@.str.2 = [7 x i8] c"read()\00"
@TCSADRAIN = global i32 0
@.str.3 = [18 x i8] c"tcsetattr ~ICANON\00"
define signext i8 @getch() {
%1 = alloca i8
%2 = alloca %struct.termios
store i8 0, i8* %1
%3 = bitcast %struct.termios* %2 to i8*
%4 = load i32, i32* @stdout
%5 = call i32 @fflush(i32 %4)
%6 = call i64 @tcgetattr(i32 0, %struct.termios* %2)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = load i32, i32* @ICANON
%12 = xor i32 %11, -1
%13 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 1
%14 = load i32, i32* %13
%15 = and i32 %14, %12
store i32 %15, i32* %13
%16 = load i32, i32* @ECHO
%17 = xor i32 %16, -1
%18 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 1
%19 = load i32, i32* %18
%20 = and i32 %19, %17
store i32 %20, i32* %18
%21 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 0
%22 = load i32*, i32** %21
%23 = load i64, i64* @VMIN
%24 = getelementptr inbounds i32, i32* %22, i64 %23
store i32 1, i32* %24
%25 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 0
%26 = load i32*, i32** %25
%27 = load i64, i64* @VTIME
%28 = getelementptr inbounds i32, i32* %26, i64 %27
store i32 0, i32* %28
%29 = load i32, i32* @TCSANOW
%30 = call i64 @tcsetattr(i32 0, i32 %29, %struct.termios* %2)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %10
%33 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %10
%35 = call i64 @read(i32 0, i8* %1, i32 1)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %39
39: ; preds = %37, %34
%40 = load i32, i32* @ICANON
%41 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 1
%42 = load i32, i32* %41
%43 = or i32 %42, %40
store i32 %43, i32* %41
%44 = load i32, i32* @ECHO
%45 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 1
%46 = load i32, i32* %45
%47 = or i32 %46, %44
store i32 %47, i32* %45
%48 = load i32, i32* @TCSADRAIN
%49 = call i64 @tcsetattr(i32 0, i32 %48, %struct.termios* %2)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %39
%52 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %53
53: ; preds = %51, %39
%54 = load i8, i8* %1
ret i8 %54
}

LLVM IR (-O2):
%struct.termios = type { i32*, i32, i32 }
@stdout = global i32 0
@.str = [12 x i8] c"tcsetattr()\00"
@ICANON = global i32 0
@ECHO = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@.str.1 = [17 x i8] c"tcsetattr ICANON\00"
@.str.2 = [7 x i8] c"read()\00"
@TCSADRAIN = global i32 0
@.str.3 = [18 x i8] c"tcsetattr ~ICANON\00"
define signext i8 @getch() {
%1 = alloca i8
%2 = alloca %struct.termios
store i8 0, i8* %1
%3 = bitcast %struct.termios* %2 to i8*
%4 = load i32, i32* @stdout
%5 = tail call i32 @fflush(i32 %4)
%6 = call i64 @tcgetattr(i32 0, %struct.termios* %2)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = load i32, i32* @ICANON
%12 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%13 = load i32, i32* %12
%14 = load i32, i32* @ECHO
%15 = or i32 %11, %14
%16 = xor i32 %15, -1
%17 = and i32 %13, %16
store i32 %17, i32* %12
%18 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%19 = load i32*, i32** %18
%20 = load i64, i64* @VMIN
%21 = getelementptr inbounds i32, i32* %19, i64 %20
store i32 1, i32* %21
%22 = load i64, i64* @VTIME
%23 = getelementptr inbounds i32, i32* %19, i64 %22
store i32 0, i32* %23
%24 = load i32, i32* @TCSANOW
%25 = call i64 @tcsetattr(i32 0, i32 %24, %struct.termios* %2)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %10
%28 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %10
%30 = call i64 @read(i32 0, i8* %1, i32 1)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %29
%35 = load i32, i32* @ICANON
%36 = load i32, i32* %12
%37 = or i32 %36, %35
%38 = load i32, i32* @ECHO
%39 = or i32 %38, %37
store i32 %39, i32* %12
%40 = load i32, i32* @TCSADRAIN
%41 = call i64 @tcsetattr(i32 0, i32 %40, %struct.termios* %2)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %34
%44 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %43, %34
%46 = load i8, i8* %1
ret i8 %46
}