#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;
  int *c_cc;
};

/* Variables and functions */
int ECHO;
int EOF;
int ICANON;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
scalar_t__ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int mygetch(void) {
  struct termios oldt, newt;
  int ch = EOF;
  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  newt.c_cc[VMIN] = 0;
  newt.c_cc[VTIME] = 2;
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  char c;
  if (read(STDIN_FILENO, &c, sizeof(char)) > 0)
    ch = (int)c;
  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  return ch;
}


x86 assembly (clang, -O0):
<mygetch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1040 <tcgetattr@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%eax
mov    0x2e6a(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e44(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
mov    0x2e1f(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x2,(%rax,%rcx,4)
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1050 <tcsetattr@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
lea    -0x25(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x0,%rax
jle    11ee <mygetch+0xbe>
movsbl -0x25(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
mov    0x2dea(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <tcsetattr@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mygetch>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    0x2e95(%rip),%rax
mov    (%rax),%r15d
mov    0x2e6b(%rip),%rbx
mov    (%rbx),%edi
lea    0x20(%rsp),%r14
mov    %r14,%rsi
call   1040 <tcgetattr@plt>
movups 0x20(%rsp),%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x2e43(%rip),%rax
mov    0x2e6c(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    %ecx,0x10(%rsp)
mov    0x18(%rsp),%rax
mov    0x2e46(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e25(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x2,(%rax,%rcx,4)
mov    (%rbx),%edi
mov    0x2e3a(%rip),%r12
mov    (%r12),%esi
lea    0x10(%rsp),%rdx
call   1050 <tcsetattr@plt>
mov    (%rbx),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
movsbl 0xf(%rsp),%ebp
cmovle %r15d,%ebp
mov    (%rbx),%edi
mov    (%r12),%esi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.termios = type { i32, i32* }
@EOF = global i32 0
@STDIN_FILENO = 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 @mygetch() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = alloca i8
%4 = bitcast %struct.termios* %1 to i8*
%5 = bitcast %struct.termios* %2 to i8*
%6 = load i32, i32* @EOF
%7 = load i32, i32* @STDIN_FILENO
%8 = call i32 @tcgetattr(i32 %7, %struct.termios* %1)
%9 = load i32, i32* @ICANON
%10 = load i32, i32* @ECHO
%11 = or i32 %10, %9
%12 = xor i32 %11, -1
%13 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%14 = load i32, i32* %13
%15 = and i32 %14, %12
store i32 %15, i32* %13
%16 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%17 = load i32*, i32** %16
%18 = load i64, i64* @VMIN
%19 = getelementptr inbounds i32, i32* %17, i64 %18
store i32 0, i32* %19
%20 = load i64, i64* @VTIME
%21 = getelementptr inbounds i32, i32* %17, i64 %20
store i32 2, i32* %21
%22 = load i32, i32* @STDIN_FILENO
%23 = load i32, i32* @TCSANOW
%24 = call i32 @tcsetattr(i32 %22, i32 %23, %struct.termios* %2)
%25 = load i32, i32* @STDIN_FILENO
%26 = call i64 @read(i32 %25, i8* %3, i32 1)
%27 = icmp sgt i64 %26, 0
%28 = load i8, i8* %3
%29 = sext i8 %28 to i32
%30 = select i1 %27, i32 %29, i32 %6
%31 = load i32, i32* @STDIN_FILENO
%32 = load i32, i32* @TCSANOW
%33 = call i32 @tcsetattr(i32 %31, i32 %32, %struct.termios* %1)
ret i32 %30
}