#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 termio {
  int c_lflag;
  int *c_cc;
};

/* Variables and functions */
int ECHO;
int ICANON;
int /*<<< orphan*/ TCGETA;
int /*<<< orphan*/ TCSETA;
int /*<<< orphan*/ ioctl(int /*<<< orphan*/, int /*<<< orphan*/,
                         struct termio *);
int read(int /*<<< orphan*/, char *, int);
int getch(char *c) {
  int ret;
  struct termio param_save, misparam;
  ioctl(0, TCGETA, &param_save);
  misparam = param_save;
  misparam.c_lflag &= ~(ICANON | ECHO);
  misparam.c_cc[4] = 1;
  ioctl(0, TCSETA, &misparam);
  ret = read(0, c, 1);
  ioctl(0, TCSETA, &param_save);
  return ret;
}


x86 assembly (clang, -O0):
<getch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2eb5(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
mov    0x2e80(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
movl   $0x1,0x10(%rax)
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x30(%rbp),%rdx
call   1030 <ioctl@plt>
mov    -0x8(%rbp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x20(%rbp),%rdx
call   1030 <ioctl@plt>
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getch>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
mov    0x2eb5(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%r14
xor    %edi,%edi
mov    %r14,%rdx
call   1030 <ioctl@plt>
movups 0x10(%rsp),%xmm0
movaps %xmm0,(%rsp)
mov    0x2e74(%rip),%rax
mov    0x2e85(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
not    %ecx
and    %ecx,(%rsp)
mov    0x8(%rsp),%rax
movl   $0x1,0x10(%rax)
mov    0x2e59(%rip),%r15
mov    (%r15),%esi
mov    %rsp,%rdx
xor    %edi,%edi
call   1030 <ioctl@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,%ebx
mov    (%r15),%esi
xor    %edi,%edi
mov    %r14,%rdx
call   1030 <ioctl@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.termio = type { i32, i32* }
@TCGETA = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@TCSETA = global i32 0
define i32 @getch(i8* nocapture %0) {
%2 = alloca %struct.termio
%3 = alloca %struct.termio
%4 = bitcast %struct.termio* %2 to i8*
%5 = bitcast %struct.termio* %3 to i8*
%6 = load i32, i32* @TCGETA
%7 = call i32 @ioctl(i32 0, i32 %6, %struct.termio* %2)
%8 = load i32, i32* @ICANON
%9 = load i32, i32* @ECHO
%10 = or i32 %9, %8
%11 = xor i32 %10, -1
%12 = getelementptr inbounds %struct.termio, %struct.termio* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = and i32 %13, %11
store i32 %14, i32* %12
%15 = getelementptr inbounds %struct.termio, %struct.termio* %3, i64 0, i32 1
%16 = load i32*, i32** %15
%17 = getelementptr inbounds i32, i32* %16, i64 4
store i32 1, i32* %17
%18 = load i32, i32* @TCSETA
%19 = call i32 @ioctl(i32 0, i32 %18, %struct.termio* %3)
%20 = call i32 @read(i32 0, i8* %0, i32 1)
%21 = load i32, i32* @TCSETA
%22 = call i32 @ioctl(i32 0, i32 %21, %struct.termio* %2)
ret i32 %20
}