#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;
  int *c_cc;
};

/* Variables and functions */
int ECHO;
int EOF;
int ICANON;
int ISIG;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ isatty(int /*<<< orphan*/);
int read(int /*<<< orphan*/, int *, int);
scalar_t__ tcgetattr(int /*<<< orphan*/, struct termios *);
scalar_t__ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
int getch(void) {
  struct termios save_termios;
  struct termios ios;
  int c = 0;
  if (!isatty(0))
    return EOF;
  if (tcgetattr(0, &save_termios) < 0)
    return EOF;
  ios = save_termios;
  ios.c_lflag &= ~(ICANON | ECHO | ISIG);
  ios.c_cc[VMIN] = 1;
  ios.c_cc[VTIME] = 0;
  if (tcsetattr(0, TCSANOW, &ios) < 0)
    return EOF;
  if (read(0, &c, 1) != 1)
    c = EOF;
  tcsetattr(0, TCSANOW, &save_termios);
  return c;
}


x86 assembly (clang, -O0):
<getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x2c(%rbp)
xor    %edi,%edi
call   1030 <isatty@plt>
cmp    $0x0,%eax
jne    1170 <getch+0x30>
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1262 <getch+0x122>
xor    %edi,%edi
lea    -0x18(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
jge    1196 <getch+0x56>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1262 <getch+0x122>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    0x2e2a(%rip),%rcx
or     (%rcx),%eax
mov    0x2e19(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2df3(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
mov    0x2dce(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x28(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
jge    1223 <getch+0xe3>
mov    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1262 <getch+0x122>
xor    %edi,%edi
lea    -0x2c(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
je     1248 <getch+0x108>
mov    0x2d8d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %r14
push   %rbx
sub    $0x38,%rsp
movl   $0x0,0xc(%rsp)
xor    %edi,%edi
call   1030 <isatty@plt>
mov    0x2e73(%rip),%rbx
test   %eax,%eax
je     121c <getch+0xdc>
lea    0x28(%rsp),%rsi
xor    %edi,%edi
call   1050 <tcgetattr@plt>
test   %rax,%rax
js     121c <getch+0xdc>
movups 0x28(%rsp),%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x2e25(%rip),%rax
mov    0x2e4e(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e3b(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x10(%rsp)
mov    0x18(%rsp),%rax
mov    0x2e17(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2df6(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e15(%rip),%r14
mov    (%r14),%esi
lea    0x10(%rsp),%rdx
xor    %edi,%edi
call   1060 <tcsetattr@plt>
test   %rax,%rax
js     121c <getch+0xdc>
lea    0xc(%rsp),%rbx
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x1,%eax
je     120d <getch+0xcd>
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    (%r14),%esi
lea    0x28(%rsp),%rdx
xor    %edi,%edi
call   1060 <tcsetattr@plt>
mov    (%rbx),%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

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

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