#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*/ perror(char *);
scalar_t__ read(int /*<<< orphan*/, char *, int);
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};
  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>
xor    %edi,%edi
lea    -0x18(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
jge    118d <getch+0x3d>
lea    0xe78(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e1c(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
mov    0x2def(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
jge    1205 <getch+0xb5>
lea    0xe0c(%rip),%rdi
call   1070 <perror@plt>
xor    %edi,%edi
lea    -0x1(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jge    122b <getch+0xdb>
lea    0xdf7(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
jge    1273 <getch+0x123>
lea    0xdb6(%rip),%rdi
call   1070 <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)
lea    0x10(%rsp),%rsi
xor    %edi,%edi
call   1040 <tcgetattr@plt>
test   %rax,%rax
js     120c <getch+0xcc>
mov    0x2e48(%rip),%rbx
mov    (%rbx),%eax
mov    0x2e67(%rip),%r14
or     (%r14),%eax
not    %eax
and    %eax,0x18(%rsp)
mov    0x10(%rsp),%rax
mov    0x2e4a(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e21(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
xor    %edi,%edi
call   1050 <tcsetattr@plt>
test   %rax,%rax
js     121d <getch+0xdd>
lea    0xf(%rsp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
js     122b <getch+0xeb>
mov    0x18(%rsp),%eax
or     (%rbx),%eax
or     (%r14),%eax
mov    %eax,0x18(%rsp)
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rdx
xor    %edi,%edi
call   1050 <tcsetattr@plt>
test   %rax,%rax
js     1239 <getch+0xf9>
mov    0xf(%rsp),%al
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xded(%rip),%rdi
call   1060 <perror@plt>
jmp    1169 <getch+0x29>
lea    0xde8(%rip),%rdi
call   1060 <perror@plt>
jmp    11c3 <getch+0x83>
lea    0xdeb(%rip),%rdi
call   1060 <perror@plt>
jmp    11d9 <getch+0x99>
lea    0xde4(%rip),%rdi
call   1060 <perror@plt>
jmp    1200 <getch+0xc0>

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

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