#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 termio {
  int c_lflag;
};
typedef int /*<<< orphan*/ ch;

/* Variables and functions */
int ECHO;
int ICANON;
int ISIG;
int /*<<< orphan*/ TCGETA;
int /*<<< orphan*/ TCSETA;
int fileno(int /*<<< orphan*/);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct termio *);
int /*<<< orphan*/ printf(char *, char);
int /*<<< orphan*/ stdin;
int getch(void) {
  char ch;
  int fd = fileno(stdin);
  struct termio old_tty, new_tty;
  ioctl(fd, TCGETA, &old_tty);
  new_tty = old_tty;
  new_tty.c_lflag &= ~(ICANON | ECHO | ISIG);
  ioctl(fd, TCSETA, &new_tty);
  fread(&ch, 1, sizeof(ch), stdin);
  ioctl(fd, TCSETA, &old_tty);
  printf("get key(press 1 = quit): %d\n", ch);
  return ch;
}


x86 assembly (clang, -O0):
<getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%edi
call   1060 <fileno@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <ioctl@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    0x2e54(%rip),%rcx
or     (%rcx),%eax
mov    0x2e43(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <ioctl@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%ecx
lea    -0x1(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    -0x8(%rbp),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1050 <ioctl@plt>
lea    0xe1e(%rip),%rdi
movsbl -0x1(%rbp),%esi
call   1040 <printf@plt>
movsbl -0x1(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e8e(%rip),%r15
mov    (%r15),%edi
call   1060 <fileno@plt>
mov    %eax,%ebx
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%r14
mov    %ebx,%edi
mov    %r14,%rdx
call   1050 <ioctl@plt>
mov    0x2e3d(%rip),%rax
mov    0x2e56(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e43(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    0x10(%rsp),%ecx
mov    %ecx,0x8(%rsp)
mov    0x2e20(%rip),%r12
mov    (%r12),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1050 <ioctl@plt>
mov    (%r15),%ecx
lea    0x7(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    (%r12),%esi
mov    %ebx,%edi
mov    %r14,%rdx
call   1050 <ioctl@plt>
movsbl 0x7(%rsp),%esi
lea    0xe1f(%rip),%rdi
call   1040 <printf@plt>
movsbl 0x7(%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termio = type { i32 }
@stdin = global i32 0
@TCGETA = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ISIG = global i32 0
@TCSETA = global i32 0
@.str = [29 x i8] c"get key(press 1 = quit): %d\0A\00"
define i32 @getch() {
%1 = alloca i8
%2 = alloca i32
%3 = alloca %struct.termio
%4 = alloca %struct.termio
%5 = load i32, i32* @stdin
%6 = call i32 @fileno(i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = load i32, i32* @TCGETA
%9 = call i32 @ioctl(i32 %7, i32 %8, %struct.termio* %3)
%10 = bitcast %struct.termio* %4 to i8*
%11 = bitcast %struct.termio* %3 to i8*
%12 = load i32, i32* @ICANON
%13 = load i32, i32* @ECHO
%14 = or i32 %12, %13
%15 = load i32, i32* @ISIG
%16 = or i32 %14, %15
%17 = xor i32 %16, -1
%18 = getelementptr inbounds %struct.termio, %struct.termio* %4, i32 0, i32 0
%19 = load i32, i32* %18
%20 = and i32 %19, %17
store i32 %20, i32* %18
%21 = load i32, i32* %2
%22 = load i32, i32* @TCSETA
%23 = call i32 @ioctl(i32 %21, i32 %22, %struct.termio* %4)
%24 = load i32, i32* @stdin
%25 = call i32 @fread(i8* %1, i32 1, i32 1, i32 %24)
%26 = load i32, i32* %2
%27 = load i32, i32* @TCSETA
%28 = call i32 @ioctl(i32 %26, i32 %27, %struct.termio* %3)
%29 = load i8, i8* %1
%30 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8 signext %29)
%31 = load i8, i8* %1
%32 = sext i8 %31 to i32
ret i32 %32
}

LLVM IR (-O2):
%struct.termio = type { i32 }
@stdin = global i32 0
@TCGETA = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@ISIG = global i32 0
@TCSETA = global i32 0
@.str = [29 x i8] c"get key(press 1 = quit): %d\0A\00"
define i32 @getch() {
%1 = alloca i8
%2 = alloca %struct.termio
%3 = alloca %struct.termio
%4 = load i32, i32* @stdin
%5 = tail call i32 @fileno(i32 %4)
%6 = bitcast %struct.termio* %2 to i8*
%7 = bitcast %struct.termio* %3 to i8*
%8 = load i32, i32* @TCGETA
%9 = call i32 @ioctl(i32 %5, i32 %8, %struct.termio* %2)
%10 = getelementptr inbounds %struct.termio, %struct.termio* %2, i64 0, i32 0
%11 = getelementptr inbounds %struct.termio, %struct.termio* %3, i64 0, i32 0
%12 = load i32, i32* %10
%13 = load i32, i32* @ICANON
%14 = load i32, i32* @ECHO
%15 = or i32 %14, %13
%16 = load i32, i32* @ISIG
%17 = or i32 %15, %16
%18 = xor i32 %17, -1
%19 = and i32 %12, %18
store i32 %19, i32* %11
%20 = load i32, i32* @TCSETA
%21 = call i32 @ioctl(i32 %5, i32 %20, %struct.termio* %3)
%22 = load i32, i32* @stdin
%23 = call i32 @fread(i8* %1, i32 1, i32 1, i32 %22)
%24 = load i32, i32* @TCSETA
%25 = call i32 @ioctl(i32 %5, i32 %24, %struct.termio* %2)
%26 = load i8, i8* %1
%27 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8 signext %26)
%28 = load i8, i8* %1
%29 = sext i8 %28 to i32
ret i32 %29
}