#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;
  scalar_t__ c_oflag;
  scalar_t__ c_iflag;
};

/* Variables and functions */
int /*<<< orphan*/ ECHO;
int /*<<< orphan*/ ICANON;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/, struct termios *);
int getch(void) {
  char ch;
  int error;
  static struct termios Otty, Ntty;
  fflush(stdout);
  tcgetattr(0, &Otty);
  Ntty = Otty;
  Ntty.c_iflag = 0;
  Ntty.c_oflag = 0;
  Ntty.c_lflag &= ~ICANON;
  Ntty.c_lflag &= ~ECHO;
  Ntty.c_cc[VMIN] = 1;
  Ntty.c_cc[VTIME] = 1;
  if (0 == (error = tcsetattr(0, TCSANOW, &Ntty))) {
    error = read(0, &ch, 1);
    error += tcsetattr(0, TCSANOW, &Otty);
  }
  return (error == 1 ? (int)ch : -1);
}


x86 assembly (clang, -O0):
<getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
xor    %edi,%edi
lea    0x2ee9(%rip),%rsi
call   1050 <tcgetattr@plt>
mov    0x2edd(%rip),%rax
mov    %rax,0x2ef6(%rip)
mov    0x2ed7(%rip),%rax
mov    %rax,0x2ef0(%rip)
mov    0x2ed1(%rip),%rax
mov    %rax,0x2eea(%rip)
mov    0x2ecb(%rip),%rax
mov    %rax,0x2ee4(%rip)
movq   $0x0,0x2ed9(%rip)
movq   $0x0,0x2ec6(%rip)
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    0x2eac(%rip),%eax
mov    %eax,0x2ea6(%rip)
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    0x2e94(%rip),%eax
mov    %eax,0x2e8e(%rip)
mov    0x2e7f(%rip),%rax
mov    0x2de8(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e67(%rip),%rax
mov    0x2db8(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0x2e44(%rip),%rdx
call   1060 <tcsetattr@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
xor    %eax,%eax
cmp    %ecx,%eax
jne    1268 <getch+0x128>
xor    %edi,%edi
lea    -0x1(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x8(%rbp)
mov    0x2d96(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0x2deb(%rip),%rdx
call   1060 <tcsetattr@plt>
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x1,-0x8(%rbp)
jne    127e <getch+0x13e>
movsbl -0x1(%rbp),%eax
mov    %eax,-0xc(%rbp)
jmp    128b <getch+0x14b>
mov    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    128b <getch+0x14b>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %r14
push   %rbx
push   %rax
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
lea    0x2eef(%rip),%rsi
xor    %ebx,%ebx
xor    %edi,%edi
call   1050 <tcgetattr@plt>
movups 0x2edf(%rip),%xmm0
movups %xmm0,0x2ef8(%rip)
lea    0x2ef1(%rip),%rdx
xorps  %xmm0,%xmm0
movups %xmm0,0x2ef7(%rip)
mov    0x2e30(%rip),%rax
mov    (%rax),%eax
mov    0x2e4f(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    %eax,0x2ed5(%rip)
mov    0x2ec6(%rip),%rax
mov    0x2e2f(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e06(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e1d(%rip),%r14
mov    (%r14),%esi
xor    %edi,%edi
call   1060 <tcsetattr@plt>
test   %eax,%eax
jne    1204 <getch+0xc4>
lea    0x7(%rsp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,%ebx
mov    (%r14),%esi
lea    0x2e52(%rip),%rdx
xor    %edi,%edi
call   1060 <tcsetattr@plt>
add    %ebx,%eax
movsbl 0x7(%rsp),%ebx
cmp    $0x1,%eax
mov    $0xffffffff,%eax
cmove  %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32*, i32, i64, i64 }
@getch.Otty = internal global %struct.termios zeroinitializer
@getch.Ntty = internal global %struct.termios zeroinitializer
@stdout = global i32 0
@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 i8
%2 = alloca i32
%3 = load i32, i32* @stdout
%4 = call i32 @fflush(i32 %3)
%5 = call i32 @tcgetattr(i32 0, %struct.termios* @getch.Otty)
store i64 0, i64* getelementptr inbounds (%struct.termios, %struct.termios* @getch.Ntty, i32 0, i32 3)
store i64 0, i64* getelementptr inbounds (%struct.termios, %struct.termios* @getch.Ntty, i32 0, i32 2)
%6 = load i32, i32* @ICANON
%7 = xor i32 %6, -1
%8 = load i32, i32* getelementptr inbounds (%struct.termios, %struct.termios* @getch.Ntty, i32 0, i32 1)
%9 = and i32 %8, %7
store i32 %9, i32* getelementptr inbounds (%struct.termios, %struct.termios* @getch.Ntty, i32 0, i32 1)
%10 = load i32, i32* @ECHO
%11 = xor i32 %10, -1
%12 = load i32, i32* getelementptr inbounds (%struct.termios, %struct.termios* @getch.Ntty, i32 0, i32 1)
%13 = and i32 %12, %11
store i32 %13, i32* getelementptr inbounds (%struct.termios, %struct.termios* @getch.Ntty, i32 0, i32 1)
%14 = load i32*, i32** getelementptr inbounds (%struct.termios, %struct.termios* @getch.Ntty, i32 0, i32 0)
%15 = load i64, i64* @VMIN
%16 = getelementptr inbounds i32, i32* %14, i64 %15
store i32 1, i32* %16
%17 = load i32*, i32** getelementptr inbounds (%struct.termios, %struct.termios* @getch.Ntty, i32 0, i32 0)
%18 = load i64, i64* @VTIME
%19 = getelementptr inbounds i32, i32* %17, i64 %18
store i32 1, i32* %19
%20 = load i32, i32* @TCSANOW
%21 = call i32 @tcsetattr(i32 0, i32 %20, %struct.termios* @getch.Ntty)
store i32 %21, i32* %2
%22 = icmp eq i32 0, %21
br i1 %22, label %23, label %29
23: ; preds = %0
%24 = call i32 @read(i32 0, i8* %1, i32 1)
store i32 %24, i32* %2
%25 = load i32, i32* @TCSANOW
%26 = call i32 @tcsetattr(i32 0, i32 %25, %struct.termios* @getch.Otty)
%27 = load i32, i32* %2
%28 = add nsw i32 %27, %26
store i32 %28, i32* %2
br label %29
29: ; preds = %23, %0
%30 = load i32, i32* %2
%31 = icmp eq i32 %30, 1
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i8, i8* %1
%34 = sext i8 %33 to i32
br label %36
35: ; preds = %29
br label %36
36: ; preds = %35, %32
%37 = phi i32 [ %34, %32 ], [ -1, %35 ]
ret i32 %37
}

LLVM IR (-O2):
%struct.termios = type { i32*, i32, i64, i64 }
@getch.Otty = internal global %struct.termios zeroinitializer
@getch.Ntty = internal global %struct.termios zeroinitializer
@stdout = global i32 0
@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 i8
%2 = load i32, i32* @stdout
%3 = tail call i32 @fflush(i32 %2)
%4 = tail call i32 @tcgetattr(i32 0, %struct.termios* @getch.Otty)
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 dereferenceable(32) bitcast (%struct.termios* @getch.Ntty to i8*), i8* align 8 dereferenceable(32) bitcast (%struct.termios* @getch.Otty to i8*), i64 16, i1 false)
%5 = load i32, i32* @ICANON
%6 = load i32, i32* getelementptr inbounds (%struct.termios, %struct.termios* @getch.Ntty, i64 0, i32 1)
%7 = load i32, i32* @ECHO
%8 = or i32 %5, %7
%9 = xor i32 %8, -1
%10 = and i32 %6, %9
store i32 %10, i32* getelementptr inbounds (%struct.termios, %struct.termios* @getch.Ntty, i64 0, i32 1)
%11 = load i32*, i32** getelementptr inbounds (%struct.termios, %struct.termios* @getch.Ntty, i64 0, i32 0)
%12 = load i64, i64* @VMIN
%13 = getelementptr inbounds i32, i32* %11, i64 %12
store i32 1, i32* %13
%14 = load i64, i64* @VTIME
%15 = getelementptr inbounds i32, i32* %11, i64 %14
store i32 1, i32* %15
%16 = load i32, i32* @TCSANOW
%17 = tail call i32 @tcsetattr(i32 0, i32 %16, %struct.termios* @getch.Ntty)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %0
%20 = call i32 @read(i32 0, i8* %1, i32 1)
%21 = load i32, i32* @TCSANOW
%22 = tail call i32 @tcsetattr(i32 0, i32 %21, %struct.termios* @getch.Otty)
%23 = add nsw i32 %22, %20
%24 = load i8, i8* %1
%25 = sext i8 %24 to i32
br label %26
26: ; preds = %19, %0
%27 = phi i32 [ %25, %19 ], [ 0, %0 ]
%28 = phi i32 [ %23, %19 ], [ %17, %0 ]
%29 = icmp eq i32 %28, 1
%30 = select i1 %29, i32 %27, i32 -1
ret i32 %30
}