#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 EOF;
int ICANON;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
int getchar();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int getch() {
  struct termios old_tio, new_tio;
  int ch;
  tcgetattr(STDIN_FILENO, &old_tio);
  new_tio = old_tio;
  new_tio.c_lflag &= (~ICANON & ~ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &new_tio);
  ch = getchar();
  tcsetattr(STDIN_FILENO, TCSANOW, &old_tio);
  if (ch == 4)
    ch = EOF;
  else
    printf("%c", ch);
  return ch;
}


x86 assembly (clang, -O0):
<getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
mov    0x2e6d(%rip),%rcx
mov    (%rcx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmpl   $0x4,-0x14(%rbp)
jne    11db <getch+0x9b>
mov    0x2e07(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    11ea <getch+0xaa>
mov    -0x14(%rbp),%esi
lea    0xe1b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e77(%rip),%r15
mov    (%r15),%edi
lea    0x10(%rsp),%r14
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
mov    0x2e58(%rip),%rax
mov    0x2e71(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    0x10(%rsp),%ecx
mov    %ecx,0x8(%rsp)
mov    (%r15),%edi
mov    0x2e61(%rip),%rbx
mov    (%rbx),%esi
lea    0x8(%rsp),%rdx
call   1060 <tcsetattr@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %eax,%ebp
mov    (%r15),%edi
mov    (%rbx),%esi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
cmp    $0x4,%ebp
jne    11b9 <getch+0x79>
mov    0x2e23(%rip),%rax
mov    (%rax),%ebp
jmp    11c7 <getch+0x87>
lea    0xe40(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@EOF = global i32 0
@.str = [3 x i8] c"%c\00"
define i32 @getch() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = alloca i32
%4 = load i32, i32* @STDIN_FILENO
%5 = call i32 @tcgetattr(i32 %4, %struct.termios* %1)
%6 = bitcast %struct.termios* %2 to i8*
%7 = bitcast %struct.termios* %1 to i8*
%8 = load i32, i32* @ICANON
%9 = xor i32 %8, -1
%10 = load i32, i32* @ECHO
%11 = xor i32 %10, -1
%12 = and i32 %9, %11
%13 = getelementptr inbounds %struct.termios, %struct.termios* %2, i32 0, i32 0
%14 = load i32, i32* %13
%15 = and i32 %14, %12
store i32 %15, i32* %13
%16 = load i32, i32* @STDIN_FILENO
%17 = load i32, i32* @TCSANOW
%18 = call i32 @tcsetattr(i32 %16, i32 %17, %struct.termios* %2)
%19 = call i32 (...) @getchar()
store i32 %19, i32* %3
%20 = load i32, i32* @STDIN_FILENO
%21 = load i32, i32* @TCSANOW
%22 = call i32 @tcsetattr(i32 %20, i32 %21, %struct.termios* %1)
%23 = load i32, i32* %3
%24 = icmp eq i32 %23, 4
br i1 %24, label %25, label %27
25: ; preds = %0
%26 = load i32, i32* @EOF
store i32 %26, i32* %3
br label %30
27: ; preds = %0
%28 = load i32, i32* %3
%29 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %28)
br label %30
30: ; preds = %27, %25
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@EOF = global i32 0
@.str = [3 x i8] c"%c\00"
define i32 @getch() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %1 to i8*
%4 = bitcast %struct.termios* %2 to i8*
%5 = load i32, i32* @STDIN_FILENO
%6 = call i32 @tcgetattr(i32 %5, %struct.termios* %1)
%7 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%8 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%9 = load i32, i32* %7
%10 = load i32, i32* @ICANON
%11 = load i32, i32* @ECHO
%12 = or i32 %11, %10
%13 = xor i32 %12, -1
%14 = and i32 %9, %13
store i32 %14, i32* %8
%15 = load i32, i32* @STDIN_FILENO
%16 = load i32, i32* @TCSANOW
%17 = call i32 @tcsetattr(i32 %15, i32 %16, %struct.termios* %2)
%18 = call i32 (...) @getchar()
%19 = load i32, i32* @STDIN_FILENO
%20 = load i32, i32* @TCSANOW
%21 = call i32 @tcsetattr(i32 %19, i32 %20, %struct.termios* %1)
%22 = icmp eq i32 %18, 4
br i1 %22, label %23, label %25
23: ; preds = %0
%24 = load i32, i32* @EOF
br label %27
25: ; preds = %0
%26 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %18)
br label %27
27: ; preds = %25, %23
%28 = phi i32 [ %24, %23 ], [ %18, %25 ]
ret i32 %28
}