#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 /*<<< orphan*/ FNDELAY;
int /*<<< orphan*/ F_SETFL;
int ICANON;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ fcntl(int /*<<< orphan*/, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int getchar();
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int util_getch(void) {
  struct termios oldt, newt;
  int ch = -1;
  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  fcntl(STDIN_FILENO, F_SETFL, FNDELAY);
  ch = getchar();
  fcntl(STDIN_FILENO, F_SETFL, 0);
  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  return ch;
}


x86 assembly (clang, -O0):
<util_getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0xffffffff,-0x14(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    0x2e69(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x14(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_getch>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e6d(%rip),%rbx
mov    (%rbx),%edi
lea    0x8(%rsp),%r14
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
mov    0x2e4f(%rip),%rax
mov    0x2e70(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    0x8(%rsp),%ecx
mov    %ecx,(%rsp)
mov    (%rbx),%edi
mov    0x2e62(%rip),%r15
mov    (%r15),%esi
mov    %rsp,%rdx
call   1060 <tcsetattr@plt>
mov    (%rbx),%edi
mov    0x2e36(%rip),%r12
mov    (%r12),%esi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %eax,%ebp
mov    (%rbx),%edi
mov    (%r12),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    (%rbx),%edi
mov    (%r15),%esi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
@F_SETFL = global i32 0
@FNDELAY = global i32 0
define i32 @util_getch() {
%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_FILENO
%6 = call i32 @tcgetattr(i32 %5, %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
%10 = load i32, i32* @ICANON
%11 = load i32, i32* @ECHO
%12 = or i32 %11, %10
%13 = xor i32 %12, -1
%14 = and i32 %9, %13
store i32 %14, i32* %8
%15 = load i32, i32* @STDIN_FILENO
%16 = load i32, i32* @TCSANOW
%17 = call i32 @tcsetattr(i32 %15, i32 %16, %struct.termios* %2)
%18 = load i32, i32* @STDIN_FILENO
%19 = load i32, i32* @F_SETFL
%20 = load i32, i32* @FNDELAY
%21 = call i32 @fcntl(i32 %18, i32 %19, i32 %20)
%22 = call i32 (...) @getchar()
%23 = load i32, i32* @STDIN_FILENO
%24 = load i32, i32* @F_SETFL
%25 = call i32 @fcntl(i32 %23, i32 %24, i32 0)
%26 = load i32, i32* @STDIN_FILENO
%27 = load i32, i32* @TCSANOW
%28 = call i32 @tcsetattr(i32 %26, i32 %27, %struct.termios* %1)
ret i32 %22
}