#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;
short getchar();
int /*<<< orphan*/ tcgetattr(int, struct termios *);
int /*<<< orphan*/ tcsetattr(int, int /*<<< orphan*/, struct termios *);
short tolower(short);
short getch(void) {
  struct termios oldattr, newattr;
  short ch;
  tcgetattr(1, &oldattr);
  newattr = oldattr;
  newattr.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(1, TCSANOW, &newattr);
  ch = getchar();
  tcsetattr(1, TCSANOW, &oldattr);
  return tolower(ch);
}


x86 assembly (clang, -O0):
<getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x1,%edi
lea    -0x8(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    0x2e74(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    -0x10(%rbp),%rdx
call   1060 <tcsetattr@plt>
mov    $0x0,%al
call   1030 <getchar@plt>
mov    %ax,-0x12(%rbp)
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    -0x8(%rbp),%rdx
call   1060 <tcsetattr@plt>
movswl -0x12(%rbp),%edi
call   1040 <tolower@plt>
cwtl
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%r14
mov    $0x1,%edi
mov    %r14,%rsi
call   1050 <tcgetattr@plt>
mov    0x2e6f(%rip),%rax
mov    0x2e78(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    0x8(%rsp),%ecx
mov    %ecx,(%rsp)
mov    0x2e6c(%rip),%rbx
mov    (%rbx),%esi
mov    %rsp,%rdx
mov    $0x1,%edi
call   1060 <tcsetattr@plt>
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %eax,%ebp
mov    (%rbx),%esi
mov    $0x1,%edi
mov    %r14,%rdx
call   1060 <tcsetattr@plt>
movswl %bp,%edi
call   1040 <tolower@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define signext i16 @getch() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = alloca i16
%4 = call i32 @tcgetattr(i32 1, %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 = load i32, i32* @TCSANOW
%15 = call i32 @tcsetattr(i32 1, i32 %14, %struct.termios* %2)
%16 = call signext i16 (...) @getchar()
store i16 %16, i16* %3
%17 = load i32, i32* @TCSANOW
%18 = call i32 @tcsetattr(i32 1, i32 %17, %struct.termios* %1)
%19 = load i16, i16* %3
%20 = call signext i16 @tolower(i16 signext %19)
ret i16 %20
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@ICANON = global i32 0
@ECHO = global i32 0
@TCSANOW = global i32 0
define signext i16 @getch() {
%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 1, %struct.termios* %1)
%6 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%7 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%8 = load i32, i32* %6
%9 = load i32, i32* @ICANON
%10 = load i32, i32* @ECHO
%11 = or i32 %10, %9
%12 = xor i32 %11, -1
%13 = and i32 %8, %12
store i32 %13, i32* %7
%14 = load i32, i32* @TCSANOW
%15 = call i32 @tcsetattr(i32 1, i32 %14, %struct.termios* %2)
%16 = call signext i16 (...) @getchar()
%17 = load i32, i32* @TCSANOW
%18 = call i32 @tcsetattr(i32 1, i32 %17, %struct.termios* %1)
%19 = call signext i16 @tolower(i16 signext %16)
ret i16 %19
}