#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 dummy;
};

/* Variables and functions */
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int getch(void) {
  struct termios t, t2;
  char c = 0;
  tcgetattr(1, &t);
  t2 = t;
  cfmakeraw(&t2);
  tcsetattr(1, TCSANOW, &t2);
  fread(&c, 1, 1, stdin);
  tcsetattr(1, TCSANOW, &t);
  return c;
}


x86 assembly (clang, -O0):
<getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movb   $0x0,-0x11(%rbp)
mov    $0x1,%edi
lea    -0x8(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
lea    -0x10(%rbp),%rdi
call   1040 <cfmakeraw@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    -0x10(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%ecx
lea    -0x11(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    -0x8(%rbp),%rdx
call   1060 <tcsetattr@plt>
movsbl -0x11(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
movb   $0x0,0xf(%rsp)
lea    0x18(%rsp),%r14
mov    $0x1,%edi
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
mov    0x18(%rsp),%eax
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <cfmakeraw@plt>
mov    0x2e64(%rip),%r15
mov    (%r15),%esi
mov    $0x1,%edi
mov    %rbx,%rdx
call   1060 <tcsetattr@plt>
mov    0x2e55(%rip),%rax
mov    (%rax),%ecx
lea    0xf(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
mov    (%r15),%esi
mov    $0x1,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
movsbl 0xf(%rsp),%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@TCSANOW = global i32 0
@stdin = global i32 0
define i32 @getch() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = alloca i8
store i8 0, i8* %3
%4 = call i32 @tcgetattr(i32 1, %struct.termios* %1)
%5 = bitcast %struct.termios* %2 to i8*
%6 = bitcast %struct.termios* %1 to i8*
%7 = call i32 @cfmakeraw(%struct.termios* %2)
%8 = load i32, i32* @TCSANOW
%9 = call i32 @tcsetattr(i32 1, i32 %8, %struct.termios* %2)
%10 = load i32, i32* @stdin
%11 = call i32 @fread(i8* %3, i32 1, i32 1, i32 %10)
%12 = load i32, i32* @TCSANOW
%13 = call i32 @tcsetattr(i32 1, i32 %12, %struct.termios* %1)
%14 = load i8, i8* %3
%15 = sext i8 %14 to i32
ret i32 %15
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@TCSANOW = global i32 0
@stdin = global i32 0
define i32 @getch() {
%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*
store i8 0, i8* %3
%6 = call i32 @tcgetattr(i32 1, %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
store i32 %9, i32* %8
%10 = call i32 @cfmakeraw(%struct.termios* %2)
%11 = load i32, i32* @TCSANOW
%12 = call i32 @tcsetattr(i32 1, i32 %11, %struct.termios* %2)
%13 = load i32, i32* @stdin
%14 = call i32 @fread(i8* %3, i32 1, i32 1, i32 %13)
%15 = load i32, i32* @TCSANOW
%16 = call i32 @tcsetattr(i32 1, i32 %15, %struct.termios* %1)
%17 = load i8, i8* %3
%18 = sext i8 %17 to i32
ret i32 %18
}