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

/* Variables and functions */
scalar_t__ isprint(char);
int /*<<< orphan*/ printf(char *, ...);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ term_set_raw_mode();
void one_read(void) {
  int ret;
  char buf[32];
  printf("one byte RAW read\n");
  term_set_raw_mode();
  ret = read(0, buf, 32);
  printf("read(%d): ", ret);
  for (int i = 0; i < ret; i++) {
    if (buf[i] == '\033')
      printf("ESC ");
    else if (buf[i] == '\n')
      printf("NL ");
    else if (buf[i] == '\r')
      printf("CR ");
    else if (isprint(buf[i]))
      printf("%c ", buf[i]);
    else
      printf("[0x%x] ", buf[i]);
  }
  printf("\n");
}


x86 assembly (clang, -O0):
<one_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    0xeb1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1040 <term_set_raw_mode@plt>
lea    -0x30(%rbp),%rsi
xor    %edi,%edi
mov    $0x20,%edx
call   1050 <read@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1270 <one_read+0x130>
movslq -0x34(%rbp),%rax
movsbl -0x30(%rbp,%rax,1),%eax
cmp    $0x1b,%eax
jne    11b9 <one_read+0x79>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    125d <one_read+0x11d>
movslq -0x34(%rbp),%rax
movsbl -0x30(%rbp,%rax,1),%eax
cmp    $0xa,%eax
jne    11de <one_read+0x9e>
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1258 <one_read+0x118>
movslq -0x34(%rbp),%rax
movsbl -0x30(%rbp,%rax,1),%eax
cmp    $0xd,%eax
jne    1203 <one_read+0xc3>
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1253 <one_read+0x113>
movslq -0x34(%rbp),%rax
movsbl -0x30(%rbp,%rax,1),%edi
call   1060 <isprint@plt>
cmp    $0x0,%rax
je     1237 <one_read+0xf7>
movslq -0x34(%rbp),%rax
movsbl -0x30(%rbp,%rax,1),%esi
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    124e <one_read+0x10e>
movslq -0x34(%rbp),%rax
movsbl -0x30(%rbp,%rax,1),%esi
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1253 <one_read+0x113>
jmp    1258 <one_read+0x118>
jmp    125d <one_read+0x11d>
jmp    1262 <one_read+0x122>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1188 <one_read+0x48>
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<one_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
lea    0xeaf(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1060 <term_set_raw_mode@plt>
mov    %rsp,%rsi
xor    %edi,%edi
mov    $0x20,%edx
call   1070 <read@plt>
mov    %eax,%ebx
lea    0xe67(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
test   %ebx,%ebx
jle    1237 <one_read+0xd7>
mov    %ebx,%r12d
lea    0xe5c(%rip),%r14
lea    0xe59(%rip),%rbx
lea    0xe49(%rip),%r15
xor    %r13d,%r13d
jmp    11e3 <one_read+0x83>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%r13
cmp    %r13,%r12
je     1237 <one_read+0xd7>
movsbl (%rsp,%r13,1),%ebp
cmp    $0xa,%ebp
je     1200 <one_read+0xa0>
cmp    $0xd,%bpl
je     11d0 <one_read+0x70>
cmp    $0x1b,%bpl
jne    1210 <one_read+0xb0>
mov    %r15,%rdi
jmp    11d3 <one_read+0x73>
xchg   %ax,%ax
mov    %r14,%rdi
jmp    11d3 <one_read+0x73>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
call   1080 <isprint@plt>
test   %rax,%rax
je     1225 <one_read+0xc5>
lea    0xdf5(%rip),%rdi
jmp    122c <one_read+0xcc>
lea    0xdf0(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11da <one_read+0x7a>
mov    $0xa,%edi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <putchar@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"one byte RAW read\0A\00"
@.str.1 = [11 x i8] c"read(%d): \00"
@.str.2 = [5 x i8] c"ESC \00"
@.str.3 = [4 x i8] c"NL \00"
@.str.4 = [4 x i8] c"CR \00"
@.str.5 = [4 x i8] c"%c \00"
@.str.6 = [8 x i8] c"[0x%x] \00"
@.str.7 = [2 x i8] c"\0A\00"
define void @one_read() {
%1 = alloca i32
%2 = alloca [32 x i8]
%3 = alloca i32
%4 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%5 = call i32 (...) @term_set_raw_mode()
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%7 = call i32 @read(i32 0, i8* %6, i32 32)
store i32 %7, i32* %1
%8 = load i32, i32* %1
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %8)
store i32 0, i32* %3
br label %10
10: ; preds = %66, %0
%11 = load i32, i32* %3
%12 = load i32, i32* %1
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %69
14: ; preds = %10
%15 = load i32, i32* %3
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 %16
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 27
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %65
23: ; preds = %14
%24 = load i32, i32* %3
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 10
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %64
32: ; preds = %23
%33 = load i32, i32* %3
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 13
br i1 %38, label %39, label %41
39: ; preds = %32
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
br label %63
41: ; preds = %32
%42 = load i32, i32* %3
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 %43
%45 = load i8, i8* %44
%46 = call i64 @isprint(i8 signext %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %55
48: ; preds = %41
%49 = load i32, i32* %3
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 %50
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %53)
br label %62
55: ; preds = %41
%56 = load i32, i32* %3
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 %57
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 %60)
br label %62
62: ; preds = %55, %48
br label %63
63: ; preds = %62, %39
br label %64
64: ; preds = %63, %30
br label %65
65: ; preds = %64, %21
br label %66
66: ; preds = %65
%67 = load i32, i32* %3
%68 = add nsw i32 %67, 1
store i32 %68, i32* %3
br label %10
69: ; preds = %10
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str.1 = [11 x i8] c"read(%d): \00"
@.str.2 = [5 x i8] c"ESC \00"
@.str.3 = [4 x i8] c"NL \00"
@.str.4 = [4 x i8] c"CR \00"
@.str.5 = [4 x i8] c"%c \00"
@.str.6 = [8 x i8] c"[0x%x] \00"
@str = [18 x i8] c"one byte RAW read\00"
define void @one_read() {
%1 = alloca [32 x i8]
%2 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 (...) @term_set_raw_mode()
%5 = call i32 @read(i32 0, i8* %2, i32 32)
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %5)
%7 = icmp sgt i32 %5, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = zext i32 %5 to i64
br label %12
10: ; preds = %30, %0
%11 = tail call i32 @putchar(i32 10)
ret void
12: ; preds = %8, %30
%13 = phi i64 [ 0, %8 ], [ %31, %30 ]
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %1, i64 0, i64 %13
%15 = load i8, i8* %14
switch i8 %15, label %22 [
i8 27, label %16
i8 10, label %18
i8 13, label %20
]
16: ; preds = %12
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %30
18: ; preds = %12
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %30
20: ; preds = %12
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
br label %30
22: ; preds = %12
%23 = tail call i64 @isprint(i8 signext %15)
%24 = icmp eq i64 %23, 0
%25 = sext i8 %15 to i32
br i1 %24, label %28, label %26
26: ; preds = %22
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %25)
br label %30
28: ; preds = %22
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i32 %25)
br label %30
30: ; preds = %16, %20, %28, %26, %18
%31 = add nuw nsw i64 %13, 1
%32 = icmp eq i64 %31, %9
br i1 %32, label %10, label %12
}