#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 /*<<< orphan*/ c_lflag;
};

/* Variables and functions */
int /*<<< orphan*/ ECHO;
int /*<<< orphan*/ ICANON;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ read(int /*<<< orphan*/, char **, int);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
char getch(void) {
  char chbuf[1];
  struct termios oldstate, newstate;
  tcgetattr(0, &oldstate);
  newstate = oldstate;
  newstate.c_lflag &= ~ICANON;
  newstate.c_lflag &= ~ECHO;
  tcsetattr(0, TCSANOW, &newstate);
  read(0, &chbuf, 1);
  tcsetattr(0, TCSANOW, &oldstate);
  return chbuf[0];
}


x86 assembly (clang, -O0):
<getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
xor    %edi,%edi
lea    -0x8(%rbp),%rsi
call   1040 <tcgetattr@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x10(%rbp),%rdx
call   1050 <tcsetattr@plt>
lea    -0x1(%rbp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1030 <read@plt>
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x8(%rbp),%rdx
call   1050 <tcsetattr@plt>
movsbl -0x1(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0x10(%rsp),%r14
xor    %edi,%edi
mov    %r14,%rsi
call   1040 <tcgetattr@plt>
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    0x2e8a(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    0x10(%rsp),%eax
mov    %eax,0x8(%rsp)
mov    0x2e7f(%rip),%rbx
mov    (%rbx),%esi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1050 <tcsetattr@plt>
lea    0x7(%rsp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1030 <read@plt>
mov    (%rbx),%esi
xor    %edi,%edi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
mov    0x7(%rsp),%al
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define signext i8 @getch() {
%1 = alloca [1 x i8]
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = call i32 @tcgetattr(i32 0, %struct.termios* %2)
%5 = bitcast %struct.termios* %3 to i8*
%6 = bitcast %struct.termios* %2 to i8*
%7 = load i32, i32* @ICANON
%8 = xor i32 %7, -1
%9 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%10 = load i32, i32* %9
%11 = and i32 %10, %8
store i32 %11, i32* %9
%12 = load i32, i32* @ECHO
%13 = xor i32 %12, -1
%14 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%15 = load i32, i32* %14
%16 = and i32 %15, %13
store i32 %16, i32* %14
%17 = load i32, i32* @TCSANOW
%18 = call i32 @tcsetattr(i32 0, i32 %17, %struct.termios* %3)
%19 = bitcast [1 x i8]* %1 to i8**
%20 = call i32 @read(i32 0, i8** %19, i32 1)
%21 = load i32, i32* @TCSANOW
%22 = call i32 @tcsetattr(i32 0, i32 %21, %struct.termios* %2)
%23 = getelementptr inbounds [1 x i8], [1 x i8]* %1, i64 0, i64 0
%24 = load i8, i8* %23
ret i8 %24
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define signext i8 @getch() {
%1 = alloca [1 x i8]
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = getelementptr inbounds [1 x i8], [1 x i8]* %1, i64 0, i64 0
%5 = bitcast %struct.termios* %2 to i8*
%6 = bitcast %struct.termios* %3 to i8*
%7 = call i32 @tcgetattr(i32 0, %struct.termios* %2)
%8 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%9 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%10 = load i32, i32* %8
%11 = load i32, i32* @ICANON
%12 = load i32, i32* @ECHO
%13 = or i32 %11, %12
%14 = xor i32 %13, -1
%15 = and i32 %10, %14
store i32 %15, i32* %9
%16 = load i32, i32* @TCSANOW
%17 = call i32 @tcsetattr(i32 0, i32 %16, %struct.termios* %3)
%18 = bitcast [1 x i8]* %1 to i8**
%19 = call i32 @read(i32 0, i8** %18, i32 1)
%20 = load i32, i32* @TCSANOW
%21 = call i32 @tcsetattr(i32 0, i32 %20, %struct.termios* %2)
%22 = load i8, i8* %4
ret i8 %22
}