#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*/ c;

/* Variables and functions */
int ECHO;
int ICANON;
int ISIG;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int main(void) {
  int nq = 0;
  struct termios old, new;
  char c;
  printf("To quit, type three 'q's (the letter q) in a row\n");
  tcgetattr(0, &old);
  new = old;
  new.c_lflag &= ~(ICANON | ECHO | ISIG);
  new.c_cc[VMIN] = 1;
  new.c_cc[VTIME] = 0;
  tcsetattr(0, TCSANOW, &new);
  for (;;) {
    if (read(0, &c, sizeof(c)) < 0) {
      break;
    }
    if (c == 'q') {
      if (++nq >= 3) {
        break;
      }
    } else {
      nq = 0;
    }
    (void)printf("%c\t%d\t0%o\t0x%x\n", c, c, c, c);
  }
  tcsetattr(0, TCSANOW, &old);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
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    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    0x2e51(%rip),%rcx
or     (%rcx),%eax
mov    0x2e40(%rip),%rcx
or     (%rcx),%eax
xor    $0xffffffff,%eax
and    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e22(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x20(%rbp),%rax
mov    0x2dfd(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x28(%rbp),%rdx
call   1060 <tcsetattr@plt>
xor    %edi,%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jge    1200 <main+0xc0>
jmp    1254 <main+0x114>
movsbl -0x29(%rbp),%eax
cmp    $0x71,%eax
jne    1229 <main+0xe9>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
cmp    $0x3,%eax
jl     1224 <main+0xe4>
jmp    1254 <main+0x114>
jmp    1230 <main+0xf0>
movl   $0x0,-0x8(%rbp)
movsbl -0x29(%rbp),%esi
movsbl -0x29(%rbp),%edx
movsbl -0x29(%rbp),%ecx
movsbl -0x29(%rbp),%r8d
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11e1 <main+0xa1>
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
lea    -0x18(%rbp),%rdx
call   1060 <tcsetattr@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
lea    0xeaf(%rip),%rdi
call   1030 <puts@plt>
lea    0x28(%rsp),%rsi
xor    %edi,%edi
call   1060 <tcgetattr@plt>
movups 0x28(%rsp),%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x2e35(%rip),%rax
mov    0x2e56(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e43(%rip),%rax
or     (%rax),%ecx
not    %ecx
and    %ecx,0x10(%rsp)
mov    0x18(%rsp),%rax
mov    0x2e27(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    0x2e06(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2e1d(%rip),%r15
mov    (%r15),%esi
lea    0x10(%rsp),%rdx
xor    %edi,%edi
call   1070 <tcsetattr@plt>
lea    0xf(%rsp),%rsi
xor    %edi,%edi
mov    $0x1,%edx
call   1050 <read@plt>
test   %rax,%rax
js     123f <main+0xef>
lea    0xe09(%rip),%r14
lea    0xf(%rsp),%rbx
xor    %edx,%edx
jmp    1227 <main+0xd7>
mov    %r14,%rdi
mov    %ecx,%esi
mov    %ecx,%edx
mov    %ecx,%r8d
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %ebp,%edx
test   %rax,%rax
js     123f <main+0xef>
movsbl 0xf(%rsp),%ecx
xor    %ebp,%ebp
cmp    $0x71,%ecx
jne    1200 <main+0xb0>
cmp    $0x1,%edx
jg     123f <main+0xef>
add    $0x1,%edx
mov    %edx,%ebp
jmp    1200 <main+0xb0>
mov    (%r15),%esi
lea    0x28(%rsp),%rdx
xor    %edi,%edi
call   1070 <tcsetattr@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32, i32* }
@.str = [50 x i8] c"To quit, type three 'q's (the letter q) in a row\0A\00"
@ICANON = global i32 0
@ECHO = global i32 0
@ISIG = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@.str.1 = [16 x i8] c"%c\09%d\090%o\090x%x\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.termios
%4 = alloca %struct.termios
%5 = alloca i8
store i32 0, i32* %1
store i32 0, i32* %2
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%7 = call i32 @tcgetattr(i32 0, %struct.termios* %3)
%8 = bitcast %struct.termios* %4 to i8*
%9 = bitcast %struct.termios* %3 to i8*
%10 = load i32, i32* @ICANON
%11 = load i32, i32* @ECHO
%12 = or i32 %10, %11
%13 = load i32, i32* @ISIG
%14 = or i32 %12, %13
%15 = xor i32 %14, -1
%16 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%17 = load i32, i32* %16
%18 = and i32 %17, %15
store i32 %18, i32* %16
%19 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
%20 = load i32*, i32** %19
%21 = load i64, i64* @VMIN
%22 = getelementptr inbounds i32, i32* %20, i64 %21
store i32 1, i32* %22
%23 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 1
%24 = load i32*, i32** %23
%25 = load i64, i64* @VTIME
%26 = getelementptr inbounds i32, i32* %24, i64 %25
store i32 0, i32* %26
%27 = load i32, i32* @TCSANOW
%28 = call i32 @tcsetattr(i32 0, i32 %27, %struct.termios* %4)
br label %29
29: ; preds = %44, %0
%30 = call i64 @read(i32 0, i8* %5, i32 1)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
br label %54
33: ; preds = %29
%34 = load i8, i8* %5
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 113
br i1 %36, label %37, label %43
37: ; preds = %33
%38 = load i32, i32* %2
%39 = add nsw i32 %38, 1
store i32 %39, i32* %2
%40 = icmp sge i32 %39, 3
br i1 %40, label %41, label %42
41: ; preds = %37
br label %54
42: ; preds = %37
br label %44
43: ; preds = %33
store i32 0, i32* %2
br label %44
44: ; preds = %43, %42
%45 = load i8, i8* %5
%46 = sext i8 %45 to i32
%47 = load i8, i8* %5
%48 = sext i8 %47 to i32
%49 = load i8, i8* %5
%50 = sext i8 %49 to i32
%51 = load i8, i8* %5
%52 = sext i8 %51 to i32
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %46, i32 %48, i32 %50, i32 %52)
br label %29
54: ; preds = %41, %32
%55 = load i32, i32* @TCSANOW
%56 = call i32 @tcsetattr(i32 0, i32 %55, %struct.termios* %3)
ret i32 0
}

LLVM IR (-O2):
%struct.termios = type { i32, i32* }
@ICANON = global i32 0
@ECHO = global i32 0
@ISIG = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@.str.1 = [16 x i8] c"%c\09%d\090%o\090x%x\0A\00"
@str = [49 x i8] c"To quit, type three 'q's (the letter q) in a row\00"
define i32 @main() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = alloca i8
%4 = bitcast %struct.termios* %1 to i8*
%5 = bitcast %struct.termios* %2 to i8*
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str, i64 0, i64 0))
%7 = call i32 @tcgetattr(i32 0, %struct.termios* %1)
%8 = load i32, i32* @ICANON
%9 = load i32, i32* @ECHO
%10 = or i32 %9, %8
%11 = load i32, i32* @ISIG
%12 = or i32 %10, %11
%13 = xor i32 %12, -1
%14 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = and i32 %15, %13
store i32 %16, i32* %14
%17 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 1
%18 = load i32*, i32** %17
%19 = load i64, i64* @VMIN
%20 = getelementptr inbounds i32, i32* %18, i64 %19
store i32 1, i32* %20
%21 = load i64, i64* @VTIME
%22 = getelementptr inbounds i32, i32* %18, i64 %21
store i32 0, i32* %22
%23 = load i32, i32* @TCSANOW
%24 = call i32 @tcsetattr(i32 0, i32 %23, %struct.termios* %2)
%25 = call i64 @read(i32 0, i8* %3, i32 1)
%26 = icmp slt i64 %25, 0
br i1 %26, label %40, label %27
27: ; preds = %0, %34
%28 = phi i32 [ %35, %34 ], [ 0, %0 ]
%29 = load i8, i8* %3
%30 = icmp eq i8 %29, 113
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = add nsw i32 %28, 1
%33 = icmp sgt i32 %28, 1
br i1 %33, label %40, label %34
34: ; preds = %27, %31
%35 = phi i32 [ %32, %31 ], [ 0, %27 ]
%36 = sext i8 %29 to i32
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %36, i32 %36, i32 %36, i32 %36)
%38 = call i64 @read(i32 0, i8* %3, i32 1)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %27
40: ; preds = %34, %31, %0
%41 = load i32, i32* @TCSANOW
%42 = call i32 @tcsetattr(i32 0, i32 %41, %struct.termios* %1)
ret i32 0
}