#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*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ read(int /*<<< orphan*/, char **, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int getch(void) {
  char chbuf[1];
  struct termios oldstate, newstate;
  fflush(stdout);
  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
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
xor    %edi,%edi
lea    -0x8(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x10(%rbp),%rdx
call   1060 <tcsetattr@plt>
lea    -0x1(%rbp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1030 <read@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x8(%rbp),%rdx
call   1060 <tcsetattr@plt>
movsbl -0x1(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
call   1040 <fflush@plt>
lea    0x10(%rsp),%r14
xor    %edi,%edi
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    0x2e6c(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    0x10(%rsp),%eax
mov    %eax,0x8(%rsp)
mov    0x2e61(%rip),%rbx
mov    (%rbx),%esi
lea    0x8(%rsp),%rdx
xor    %edi,%edi
call   1060 <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   1060 <tcsetattr@plt>
movsbl 0x7(%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.termios = type { i32 }
@stdout = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define i32 @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 = load i32, i32* @stdout
%8 = tail call i32 @fflush(i32 %7)
%9 = call i32 @tcgetattr(i32 0, %struct.termios* %2)
%10 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%11 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%12 = load i32, i32* %10
%13 = load i32, i32* @ICANON
%14 = load i32, i32* @ECHO
%15 = or i32 %13, %14
%16 = xor i32 %15, -1
%17 = and i32 %12, %16
store i32 %17, i32* %11
%18 = load i32, i32* @TCSANOW
%19 = call i32 @tcsetattr(i32 0, i32 %18, %struct.termios* %3)
%20 = bitcast [1 x i8]* %1 to i8**
%21 = call i32 @read(i32 0, i8** %20, i32 1)
%22 = load i32, i32* @TCSANOW
%23 = call i32 @tcsetattr(i32 0, i32 %22, %struct.termios* %2)
%24 = load i8, i8* %4
%25 = sext i8 %24 to i32
ret i32 %25
}