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

/* Variables and functions */
int ECHO;
int ICANON;
int /*<<< orphan*/ TCSANOW;
int fgetc(int /*<<< orphan*/);
int /*<<< orphan*/ fileno(int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(struct termios *, struct termios *, int);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int getkey() {
  int character;
  struct termios orig_term_attr;
  struct termios new_term_attr;
  tcgetattr(fileno(stdin), &orig_term_attr);
  memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios));
  new_term_attr.c_lflag &= ~(ECHO | ICANON);
  tcsetattr(fileno(stdin), TCSANOW, &new_term_attr);
  character = fgetc(stdin);
  tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
  return character;
}


x86 assembly (clang, -O0):
<getkey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e89(%rip),%rax
mov    (%rax),%edi
call   1050 <fileno@plt>
mov    %eax,%edi
lea    -0x8(%rbp),%rsi
call   1060 <tcgetattr@plt>
lea    -0x10(%rbp),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    0x2e35(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1050 <fileno@plt>
mov    %eax,%edi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
call   1030 <fgetc@plt>
mov    %eax,-0x4(%rbp)
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1050 <fileno@plt>
mov    %eax,%edi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1070 <tcsetattr@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getkey>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e86(%rip),%r15
mov    (%r15),%edi
call   1050 <fileno@plt>
lea    0x10(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
call   1060 <tcgetattr@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    0x2e43(%rip),%rax
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    %ecx,0x8(%rsp)
mov    (%r15),%edi
call   1050 <fileno@plt>
mov    0x2e2b(%rip),%r12
mov    (%r12),%esi
mov    %eax,%edi
mov    %rbx,%rdx
call   1070 <tcsetattr@plt>
mov    (%r15),%edi
call   1030 <fgetc@plt>
mov    %eax,%ebx
mov    (%r15),%edi
call   1050 <fileno@plt>
mov    (%r12),%esi
mov    %eax,%edi
mov    %r14,%rdx
call   1070 <tcsetattr@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@stdin = global i32 0
@ECHO = global i32 0
@ICANON = global i32 0
@TCSANOW = global i32 0
define i32 @getkey() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = load i32, i32* @stdin
%5 = call i32 @fileno(i32 %4)
%6 = call i32 @tcgetattr(i32 %5, %struct.termios* %2)
%7 = call i32 @memcpy(%struct.termios* %3, %struct.termios* %2, i32 4)
%8 = load i32, i32* @ECHO
%9 = load i32, i32* @ICANON
%10 = or i32 %8, %9
%11 = xor i32 %10, -1
%12 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%13 = load i32, i32* %12
%14 = and i32 %13, %11
store i32 %14, i32* %12
%15 = load i32, i32* @stdin
%16 = call i32 @fileno(i32 %15)
%17 = load i32, i32* @TCSANOW
%18 = call i32 @tcsetattr(i32 %16, i32 %17, %struct.termios* %3)
%19 = load i32, i32* @stdin
%20 = call i32 @fgetc(i32 %19)
store i32 %20, i32* %1
%21 = load i32, i32* @stdin
%22 = call i32 @fileno(i32 %21)
%23 = load i32, i32* @TCSANOW
%24 = call i32 @tcsetattr(i32 %22, i32 %23, %struct.termios* %2)
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@stdin = global i32 0
@ECHO = global i32 0
@ICANON = global i32 0
@TCSANOW = global i32 0
define i32 @getkey() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %1 to i8*
%4 = bitcast %struct.termios* %2 to i8*
%5 = load i32, i32* @stdin
%6 = tail call i32 @fileno(i32 %5)
%7 = call i32 @tcgetattr(i32 %6, %struct.termios* %1)
%8 = call i32 @memcpy(%struct.termios* %2, %struct.termios* %1, i32 4)
%9 = load i32, i32* @ECHO
%10 = load i32, i32* @ICANON
%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 = load i32, i32* @stdin
%17 = call i32 @fileno(i32 %16)
%18 = load i32, i32* @TCSANOW
%19 = call i32 @tcsetattr(i32 %17, i32 %18, %struct.termios* %2)
%20 = load i32, i32* @stdin
%21 = call i32 @fgetc(i32 %20)
%22 = load i32, i32* @stdin
%23 = call i32 @fileno(i32 %22)
%24 = load i32, i32* @TCSANOW
%25 = call i32 @tcsetattr(i32 %23, i32 %24, %struct.termios* %1)
ret i32 %21
}