#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;
  int *c_cc;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int ECHO;
int ICANON;
int ISIG;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int _IONBF;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int getchar();
int /*<<< orphan*/ printf(char *, ...);
int setvbuf(int /*<<< orphan*/ *, char *, int, size_t);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
char *strerror(int);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int main(void) {
  struct termios orig, now;
  int c, rv, mode, errsave;
  FILE *stream;
  char *buf;
  size_t size;
  if (0) {
    stream = stdin;
    buf = NULL;
    mode = _IONBF;
    size = 0;
    rv = setvbuf(stream, buf, mode, size);
    if (rv) {
      errsave = errno;
      fprintf(stderr, "setvbuf failed; errno: %d: %s\n", errsave,
              strerror(errsave));
      return rv;
    }
  }
  tcgetattr(0, &orig);
  now = orig;
  now.c_lflag &= ~(ISIG | ICANON | ECHO);
  now.c_cc[VMIN] = 1;
  now.c_cc[VTIME] = 2;
  tcsetattr(0, TCSANOW, &now);
  c = 0;
  while (c != 'q') {
    printf("hit a key ('q' to quit): ");
    c = getchar();
    printf("%03X \n", c);
  }
  tcsetattr(0, TCSANOW, &orig);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
xor    %edi,%edi
lea    -0x18(%rbp),%rsi
call   1050 <tcgetattr@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    0x2e3e(%rip),%rcx
or     (%rcx),%eax
mov    0x2e5d(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e37(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
mov    0x2e12(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x2,(%rax,%rcx,4)
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x28(%rbp),%rdx
call   1060 <tcsetattr@plt>
movl   $0x0,-0x2c(%rbp)
cmpl   $0x71,-0x2c(%rbp)
je     120b <main+0xcb>
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%esi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11d3 <main+0x93>
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1060 <tcsetattr@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
lea    0x18(%rsp),%rsi
xor    %edi,%edi
call   1050 <tcgetattr@plt>
movups 0x18(%rsp),%xmm0
movaps %xmm0,(%rsp)
mov    0x2e72(%rip),%rax
mov    0x2e4b(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e68(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,(%rsp)
mov    0x8(%rsp),%rax
mov    0x2e45(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e24(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x2,(%rax,%rcx,4)
mov    0x2e3b(%rip),%r15
mov    (%r15),%esi
mov    %rsp,%rdx
xor    %edi,%edi
call   1060 <tcsetattr@plt>
lea    0xe3f(%rip),%r14
lea    0xe52(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
cmp    $0x71,%ebp
jne    11d0 <main+0x90>
mov    (%r15),%esi
lea    0x18(%rsp),%rdx
xor    %edi,%edi
call   1060 <tcsetattr@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32* }
@ISIG = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@.str = [26 x i8] c"hit a key ('q' to quit): \00"
@.str.1 = [7 x i8] c"%03X \0A\00"
@_IONBF = global i32 0
@errno = global i32 0
@stderr = global i32 0
@stdin = global i32* null
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.termios
%3 = alloca %struct.termios
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i64
store i32 0, i32* %1
%11 = call i32 @tcgetattr(i32 0, %struct.termios* %2)
%12 = bitcast %struct.termios* %3 to i8*
%13 = bitcast %struct.termios* %2 to i8*
%14 = load i32, i32* @ISIG
%15 = load i32, i32* @ICANON
%16 = or i32 %14, %15
%17 = load i32, i32* @ECHO
%18 = or i32 %16, %17
%19 = xor i32 %18, -1
%20 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%21 = load i32, i32* %20
%22 = and i32 %21, %19
store i32 %22, i32* %20
%23 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%24 = load i32*, i32** %23
%25 = load i64, i64* @VMIN
%26 = getelementptr inbounds i32, i32* %24, i64 %25
store i32 1, i32* %26
%27 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 1
%28 = load i32*, i32** %27
%29 = load i64, i64* @VTIME
%30 = getelementptr inbounds i32, i32* %28, i64 %29
store i32 2, i32* %30
%31 = load i32, i32* @TCSANOW
%32 = call i32 @tcsetattr(i32 0, i32 %31, %struct.termios* %3)
store i32 0, i32* %4
br label %33
33: ; preds = %36, %0
%34 = load i32, i32* %4
%35 = icmp ne i32 %34, 113
br i1 %35, label %36, label %41
36: ; preds = %33
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%38 = call i32 (...) @getchar()
store i32 %38, i32* %4
%39 = load i32, i32* %4
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %39)
br label %33
41: ; preds = %33
%42 = load i32, i32* @TCSANOW
%43 = call i32 @tcsetattr(i32 0, i32 %42, %struct.termios* %2)
ret i32 0
}

LLVM IR (-O2):
%struct.termios = type { i32, i32* }
@ISIG = global i32 0
@ICANON = global i32 0
@ECHO = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@.str = [26 x i8] c"hit a key ('q' to quit): \00"
@.str.1 = [7 x i8] c"%03X \0A\00"
@_IONBF = global i32 0
@errno = global i32 0
@stderr = global i32 0
@stdin = global i32* null
define i32 @main() {
%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* @ISIG
%7 = load i32, i32* @ICANON
%8 = or i32 %7, %6
%9 = load i32, i32* @ECHO
%10 = or i32 %8, %9
%11 = xor i32 %10, -1
%12 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = and i32 %13, %11
store i32 %14, i32* %12
%15 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%16 = load i32*, i32** %15
%17 = load i64, i64* @VMIN
%18 = getelementptr inbounds i32, i32* %16, i64 %17
store i32 1, i32* %18
%19 = load i64, i64* @VTIME
%20 = getelementptr inbounds i32, i32* %16, i64 %19
store i32 2, i32* %20
%21 = load i32, i32* @TCSANOW
%22 = call i32 @tcsetattr(i32 0, i32 %21, %struct.termios* %2)
br label %23
23: ; preds = %0, %23
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%25 = call i32 (...) @getchar()
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = icmp eq i32 %25, 113
br i1 %27, label %28, label %23
28: ; preds = %23
%29 = load i32, i32* @TCSANOW
%30 = call i32 @tcsetattr(i32 0, i32 %29, %struct.termios* %1)
ret i32 0
}