#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;
};

/* Variables and functions */
int ECHO;
int ICANON;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ exit(int);
char getchar();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
char getch(void) {
  char ch;
  static struct termios old, new;
  tcgetattr(0, &old);
  new = old;
  new.c_lflag &= (~ICANON) | ECHO;
  tcsetattr(0, TCSANOW, &new);
  ch = getchar();
  tcsetattr(0, TCSANOW, &old);
  if (ch == 3) {
    printf("\n^C\n");
    exit(2);
  }
  return ch;
}


x86 assembly (clang, -O0):
<getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %edi,%edi
lea    0x2eeb(%rip),%rsi
call   1050 <tcgetattr@plt>
mov    0x2ee0(%rip),%eax
mov    %eax,0x2ede(%rip)
mov    0x2e57(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
mov    0x2e5b(%rip),%rcx
or     (%rcx),%eax
and    0x2ec3(%rip),%eax
mov    %eax,0x2ebd(%rip)
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0x2eab(%rip),%rdx
call   1060 <tcsetattr@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %al,-0x1(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    0x2e86(%rip),%rdx
call   1060 <tcsetattr@plt>
movsbl -0x1(%rbp),%eax
cmp    $0x3,%eax
jne    11ee <getch+0x9e>
lea    0xe21(%rip),%rdi
call   1030 <printf@plt>
mov    $0x2,%edi
call   1070 <exit@plt>
movsbl -0x1(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %r15
push   %r14
push   %rbx
lea    0x2ef0(%rip),%r14
xor    %edi,%edi
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
not    %eax
mov    0x2e68(%rip),%rcx
or     (%rcx),%eax
and    0x2ecc(%rip),%eax
mov    %eax,0x2eca(%rip)
lea    0x2ec3(%rip),%rdx
mov    0x2e54(%rip),%r15
mov    (%r15),%esi
xor    %edi,%edi
call   1060 <tcsetattr@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %eax,%ebx
mov    (%r15),%esi
xor    %edi,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
cmp    $0x3,%bl
je     11c1 <getch+0x71>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe38(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@getch.old = internal global %struct.termios zeroinitializer
@getch.new = internal global %struct.termios zeroinitializer
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@.str = [5 x i8] c"\0A^C\0A\00"
define signext i8 @getch() {
%1 = alloca i8
%2 = call i32 @tcgetattr(i32 0, %struct.termios* @getch.old)
%3 = load i32, i32* @ICANON
%4 = xor i32 %3, -1
%5 = load i32, i32* @ECHO
%6 = or i32 %4, %5
%7 = load i32, i32* getelementptr inbounds (%struct.termios, %struct.termios* @getch.new, i32 0, i32 0)
%8 = and i32 %7, %6
store i32 %8, i32* getelementptr inbounds (%struct.termios, %struct.termios* @getch.new, i32 0, i32 0)
%9 = load i32, i32* @TCSANOW
%10 = call i32 @tcsetattr(i32 0, i32 %9, %struct.termios* @getch.new)
%11 = call signext i8 (...) @getchar()
store i8 %11, i8* %1
%12 = load i32, i32* @TCSANOW
%13 = call i32 @tcsetattr(i32 0, i32 %12, %struct.termios* @getch.old)
%14 = load i8, i8* %1
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 3
br i1 %16, label %17, label %20
17: ; preds = %0
%18 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 2)
unreachable
20: ; preds = %0
%21 = load i8, i8* %1
ret i8 %21
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@getch.old = internal global %struct.termios zeroinitializer
@getch.new = internal global %struct.termios zeroinitializer
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@str = [4 x i8] c"\0A^C\00"
define signext i8 @getch() {
%1 = tail call i32 @tcgetattr(i32 0, %struct.termios* @getch.old)
%2 = load i32, i32* getelementptr inbounds (%struct.termios, %struct.termios* @getch.old, i64 0, i32 0)
%3 = load i32, i32* @ICANON
%4 = xor i32 %3, -1
%5 = load i32, i32* @ECHO
%6 = or i32 %5, %4
%7 = and i32 %6, %2
store i32 %7, i32* getelementptr inbounds (%struct.termios, %struct.termios* @getch.new, i64 0, i32 0)
%8 = load i32, i32* @TCSANOW
%9 = tail call i32 @tcsetattr(i32 0, i32 %8, %struct.termios* @getch.new)
%10 = tail call signext i8 (...) @getchar()
%11 = load i32, i32* @TCSANOW
%12 = tail call i32 @tcsetattr(i32 0, i32 %11, %struct.termios* @getch.old)
%13 = icmp eq i8 %10, 3
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 2)
unreachable
17: ; preds = %0
ret i8 %10
}