#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 /*<<< orphan*/ c_lflag;
};

/* Variables and functions */
int /*<<< orphan*/ ECHO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ TCSADRAIN;
int /*<<< orphan*/ cfmakeraw(struct termios *);
int /*<<< orphan*/ printf(char *, ...);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ tcgetattr(int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ tcsetattr(int /*<<< orphan*/, int /*<<< orphan*/,
                             struct termios *);
int main() {
  struct termios bak;
  struct termios cfg;
  int ret;
  int q;
  int i;
  char buf[128];
  tcgetattr(STDIN_FILENO, &cfg);
  bak = cfg;
  cfmakeraw(&cfg);
  cfg.c_lflag &= ~ECHO;
  tcsetattr(STDIN_FILENO, TCSADRAIN, &cfg);
  q = 0;
  while ((ret = read(STDIN_FILENO, buf, 128)) > 0) {
    if (ret == 1 && buf[0] == 'q') {
      if (++q == 2)
        break;
    } else {
      q = 0;
    }
    for (i = 0; i < ret; i++) {
      printf("%X ", buf[i]);
    }
    printf("\n\r");
  }
  tcsetattr(STDIN_FILENO, TCSADRAIN, &bak);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1060 <tcgetattr@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
call   1050 <cfmakeraw@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1070 <tcsetattr@plt>
movl   $0x0,-0x18(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1040 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    1268 <main+0x118>
cmpl   $0x1,-0x14(%rbp)
jne    1213 <main+0xc3>
movsbl -0xa0(%rbp),%eax
cmp    $0x71,%eax
jne    1213 <main+0xc3>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmp    $0x2,%eax
jne    120e <main+0xbe>
jmp    1268 <main+0x118>
jmp    121a <main+0xca>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1255 <main+0x105>
movslq -0x1c(%rbp),%rax
movsbl -0xa0(%rbp,%rax,1),%esi
ff
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1221 <main+0xd1>
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11b7 <main+0x67>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
mov    0x2d60(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1070 <tcsetattr@plt>
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    0x2e68(%rip),%rbp
mov    0x0(%rbp),%edi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   1060 <tcgetattr@plt>
mov    (%rsp),%eax
mov    %eax,0x8(%rsp)
mov    %rbx,%rdi
call   1050 <cfmakeraw@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,(%rsp)
mov    0x0(%rbp),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdx
call   1070 <tcsetattr@plt>
mov    0x0(%rbp),%edi
lea    0x10(%rsp),%rsi
mov    $0x80,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    126e <main+0x11e>
mov    %eax,%ebp
lea    0xe36(%rip),%rbx
lea    0xe33(%rip),%r14
lea    0x10(%rsp),%r15
xor    %eax,%eax
jmp    1209 <main+0xb9>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    %r15,%rsi
mov    $0x80,%edx
call   1040 <read@plt>
mov    %eax,%ebp
mov    %r13d,%eax
test   %ebp,%ebp
jle    126e <main+0x11e>
mov    0x10(%rsp),%cl
mov    $0x0,%r13d
cmp    $0x1,%ebp
jne    122e <main+0xde>
mov    $0x0,%r13d
cmp    $0x71,%cl
jne    122e <main+0xde>
mov    $0x1,%r13d
cmp    $0x1,%eax
je     126e <main+0x11e>
movsbl %cl,%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
cmp    $0x1,%ebp
je     11e0 <main+0x90>
mov    %ebp,%ebp
mov    $0x1,%r12d
nopl   0x0(%rax,%rax,1)
movsbl 0x10(%rsp,%r12,1),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%r12
cmp    %r12,%rbp
jne    1250 <main+0x100>
jmp    11e0 <main+0x90>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1070 <tcsetattr@plt>
xor    %eax,%eax
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@ECHO = global i32 0
@TCSADRAIN = global i32 0
@.str = [4 x i8] c"%X \00"
@.str.1 = [3 x i8] c"\0A\0D\00"
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 [128 x i8]
store i32 0, i32* %1
%8 = load i32, i32* @STDIN_FILENO
%9 = call i32 @tcgetattr(i32 %8, %struct.termios* %3)
%10 = bitcast %struct.termios* %2 to i8*
%11 = bitcast %struct.termios* %3 to i8*
%12 = call i32 @cfmakeraw(%struct.termios* %3)
%13 = load i32, i32* @ECHO
%14 = xor i32 %13, -1
%15 = getelementptr inbounds %struct.termios, %struct.termios* %3, i32 0, i32 0
%16 = load i32, i32* %15
%17 = and i32 %16, %14
store i32 %17, i32* %15
%18 = load i32, i32* @STDIN_FILENO
%19 = load i32, i32* @TCSADRAIN
%20 = call i32 @tcsetattr(i32 %18, i32 %19, %struct.termios* %3)
store i32 0, i32* %5
br label %21
21: ; preds = %56, %0
%22 = load i32, i32* @STDIN_FILENO
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 128)
store i32 %24, i32* %4
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %58
26: ; preds = %21
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, 1
br i1 %28, label %29, label %40
29: ; preds = %26
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 113
br i1 %33, label %34, label %40
34: ; preds = %29
%35 = load i32, i32* %5
%36 = add nsw i32 %35, 1
store i32 %36, i32* %5
%37 = icmp eq i32 %36, 2
br i1 %37, label %38, label %39
38: ; preds = %34
br label %58
39: ; preds = %34
br label %41
40: ; preds = %29, %26
store i32 0, i32* %5
br label %41
41: ; preds = %40, %39
store i32 0, i32* %6
br label %42
42: ; preds = %53, %41
%43 = load i32, i32* %6
%44 = load i32, i32* %4
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %56
46: ; preds = %42
%47 = load i32, i32* %6
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 %48
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %51)
br label %53
53: ; preds = %46
%54 = load i32, i32* %6
%55 = add nsw i32 %54, 1
store i32 %55, i32* %6
br label %42
56: ; preds = %42
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %21
58: ; preds = %38, %21
%59 = load i32, i32* @STDIN_FILENO
%60 = load i32, i32* @TCSADRAIN
%61 = call i32 @tcsetattr(i32 %59, i32 %60, %struct.termios* %2)
ret i32 0
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@STDIN_FILENO = global i32 0
@ECHO = global i32 0
@TCSADRAIN = global i32 0
@.str = [4 x i8] c"%X \00"
@.str.1 = [3 x i8] c"\0A\0D\00"
define i32 @main() {
%1 = alloca %struct.termios
%2 = alloca %struct.termios
%3 = alloca [128 x i8]
%4 = bitcast %struct.termios* %1 to i8*
%5 = bitcast %struct.termios* %2 to i8*
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @STDIN_FILENO
%8 = call i32 @tcgetattr(i32 %7, %struct.termios* %2)
%9 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%10 = getelementptr inbounds %struct.termios, %struct.termios* %1, i64 0, i32 0
%11 = load i32, i32* %9
store i32 %11, i32* %10
%12 = call i32 @cfmakeraw(%struct.termios* %2)
%13 = load i32, i32* @ECHO
%14 = xor i32 %13, -1
%15 = load i32, i32* %9
%16 = and i32 %15, %14
store i32 %16, i32* %9
%17 = load i32, i32* @STDIN_FILENO
%18 = load i32, i32* @TCSADRAIN
%19 = call i32 @tcsetattr(i32 %17, i32 %18, %struct.termios* %2)
%20 = load i32, i32* @STDIN_FILENO
%21 = call i32 @read(i32 %20, i8* %6, i32 128)
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %51
23: ; preds = %0, %46
%24 = phi i32 [ %49, %46 ], [ %21, %0 ]
%25 = phi i32 [ %33, %46 ], [ 0, %0 ]
%26 = icmp eq i32 %24, 1
%27 = load i8, i8* %6
%28 = icmp eq i8 %27, 113
%29 = select i1 %26, i1 %28, i1 false
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = icmp eq i32 %25, 1
br i1 %31, label %51, label %32
32: ; preds = %30, %23
%33 = phi i32 [ 1, %30 ], [ 0, %23 ]
%34 = zext i32 %24 to i64
%35 = sext i8 %27 to i32
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %35)
%37 = icmp eq i32 %24, 1
br i1 %37, label %46, label %38
38: ; preds = %32, %38
%39 = phi i64 [ %44, %38 ], [ 1, %32 ]
%40 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 %39
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %42)
%44 = add nuw nsw i64 %39, 1
%45 = icmp eq i64 %44, %34
br i1 %45, label %46, label %38
46: ; preds = %38, %32
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%48 = load i32, i32* @STDIN_FILENO
%49 = call i32 @read(i32 %48, i8* %6, i32 128)
%50 = icmp sgt i32 %49, 0
br i1 %50, label %23, label %51
51: ; preds = %46, %30, %0
%52 = load i32, i32* @STDIN_FILENO
%53 = load i32, i32* @TCSADRAIN
%54 = call i32 @tcsetattr(i32 %52, i32 %53, %struct.termios* %1)
ret i32 0
}