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

/* Variables and functions */
int ECHO;
int ICANON;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int getchar();
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int Tui_Getkey(void) {
  struct termios old;
  struct termios new;
  int ch;
  tcgetattr(0, &old);
  new = old;
  new.c_lflag &= ~(ICANON | ECHO);
  new.c_cc[VTIME] = 0;
  new.c_cc[VMIN] = 0;
  tcsetattr(0, TCSANOW, &new);
  ch = getchar();
  tcsetattr(0, TCSANOW, &old);
  return (ch);
}


x86 assembly (clang, -O0):
<Tui_Getkey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
xor    %edi,%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    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    0x2e7d(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e4f(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
mov    0x2e49(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x20(%rbp),%rdx
call   1050 <tcsetattr@plt>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x10(%rbp),%rdx
call   1050 <tcsetattr@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Tui_Getkey>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
lea    0x10(%rsp),%r14
xor    %edi,%edi
mov    %r14,%rsi
call   1040 <tcgetattr@plt>
movups 0x10(%rsp),%xmm0
movaps %xmm0,(%rsp)
mov    0x2e69(%rip),%rax
mov    0x2e82(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    %ecx,(%rsp)
mov    0x8(%rsp),%rax
mov    0x2e55(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    0x2e53(%rip),%rcx
mov    (%rcx),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    0x2e51(%rip),%rbx
mov    (%rbx),%esi
mov    %rsp,%rdx
xor    %edi,%edi
call   1050 <tcsetattr@plt>
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ebp
mov    (%rbx),%esi
xor    %edi,%edi
mov    %r14,%rdx
call   1050 <tcsetattr@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.termios = type { i32, i64* }
@ICANON = global i32 0
@ECHO = global i32 0
@VTIME = global i64 0
@VMIN = global i64 0
@TCSANOW = global i32 0
define i32 @Tui_Getkey() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = bitcast %struct.termios* %1 to i8*
%4 = bitcast %struct.termios* %2 to i8*
%5 = call i32 @tcgetattr(i32 0, %struct.termios* %1)
%6 = load i32, i32* @ICANON
%7 = load i32, i32* @ECHO
%8 = or i32 %7, %6
%9 = xor i32 %8, -1
%10 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = and i32 %11, %9
store i32 %12, i32* %10
%13 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%14 = load i64*, i64** %13
%15 = load i64, i64* @VTIME
%16 = getelementptr inbounds i64, i64* %14, i64 %15
store i64 0, i64* %16
%17 = load i64, i64* @VMIN
%18 = getelementptr inbounds i64, i64* %14, i64 %17
store i64 0, i64* %18
%19 = load i32, i32* @TCSANOW
%20 = call i32 @tcsetattr(i32 0, i32 %19, %struct.termios* %2)
%21 = call i32 (...) @getchar()
%22 = load i32, i32* @TCSANOW
%23 = call i32 @tcsetattr(i32 0, i32 %22, %struct.termios* %1)
ret i32 %21
}