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

/* Variables and functions */
int EOF;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fgetc(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ putchar(int);
int /*<<< orphan*/ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ reset_terminal();
int /*<<< orphan*/ set_terminal();
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  FILE *f;
  char c = 0, buf[512];
  int d;
  if (argc < 2) {
    fprintf(stderr, "needed a file name\n");
    exit(0);
  }
  f = fopen(argv[1], "r");
  if (!f) {
    fprintf(stderr, "unable to open %s\n", argv[1]);
    exit(0);
  }
  set_terminal();
  while (c != 0x1b) {
    read(0, &c, 1);
    if (c == 0x0a || c == 0x0d) {
      if (fgets(buf, 512, f))
        fputs(buf, stdout);
      else
        break;
    } else {
      if ((d = fgetc(f)) != EOF)
        putchar(d);
      else
        break;
    }
    fflush(stdout);
  }
  reset_terminal();
  fputs("\033[0m\n", stdout);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x0,-0x19(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11f5 <main+0x45>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe10(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    123e <main+0x8e>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xde6(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   1040 <set_terminal@plt>
movsbl -0x19(%rbp),%eax
cmp    $0x1b,%eax
je     130a <main+0x15a>
xor    %edi,%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
je     127c <main+0xcc>
movsbl -0x19(%rbp),%eax
cmp    $0xd,%eax
jne    12bf <main+0x10f>
lea    -0x220(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x200,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     12b5 <main+0x105>
lea    -0x220(%rbp),%rdi
mov    0x2d27(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
jmp    12ba <main+0x10a>
jmp    130a <main+0x15a>
jmp    12f7 <main+0x147>
mov    -0x18(%rbp),%rdi
call   1060 <fgetc@plt>
mov    %eax,-0x224(%rbp)
mov    0x2d13(%rip),%rcx
cmp    (%rcx),%eax
je     12ed <main+0x13d>
mov    -0x224(%rbp),%edi
call   1030 <putchar@plt>
jmp    12f2 <main+0x142>
jmp    130a <main+0x15a>
jmp    12f7 <main+0x147>
mov    0x2cd2(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
jmp    1245 <main+0x95>
mov    $0x0,%al
call   10b0 <reset_terminal@plt>
mov    0x2cb8(%rip),%rax
mov    (%rax),%esi
lea    0xd08(%rip),%rdi
call   1050 <fputs@plt>
xor    %eax,%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
movb   $0x0,0xf(%rsp)
cmp    $0x1,%edi
jle    129f <main+0xef>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
lea    0xe37(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     12bd <main+0x10d>
mov    %rax,%rbp
xor    %eax,%eax
call   1040 <set_terminal@plt>
lea    0xf(%rsp),%r14
lea    0x10(%rsp),%r15
mov    0x2dca(%rip),%r12
mov    0x2ddb(%rip),%r13
jmp    1233 <main+0x83>
nop
mov    %rbp,%rdi
call   1060 <fgetc@plt>
cmp    0x0(%r13),%eax
je     1274 <main+0xc4>
mov    %eax,%edi
call   1030 <putchar@plt>
mov    (%r12),%edi
call   10a0 <fflush@plt>
cmp    $0x1b,%bl
je     1274 <main+0xc4>
xor    %edi,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
movzbl 0xf(%rsp),%ebx
cmp    $0xd,%bl
je     1251 <main+0xa1>
cmp    $0xa,%bl
jne    1210 <main+0x60>
mov    %r15,%rdi
mov    $0x200,%esi
mov    %rbp,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1274 <main+0xc4>
mov    (%r12),%esi
mov    %r15,%rdi
call   1050 <fputs@plt>
jmp    1225 <main+0x75>
xor    %eax,%eax
call   10b0 <reset_terminal@plt>
mov    (%r12),%esi
lea    0xda3(%rip),%rdi
call   1050 <fputs@plt>
xor    %eax,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
lea    0xd51(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xd45(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"needed a file name\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"unable to open %s\0A\00"
@stdout = global i32 0
@EOF = global i32 0
@.str.3 = [6 x i8] c"\1B[0m\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8
%8 = alloca [512 x i8]
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8 0, i8* %7
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %20, i32** %6
%21 = load i32*, i32** %6
%22 = icmp ne i32* %21, null
br i1 %22, label %30, label %23
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %16
%31 = call i32 (...) @set_terminal()
br label %32
32: ; preds = %66, %30
%33 = load i8, i8* %7
%34 = sext i8 %33 to i32
%35 = icmp ne i32 %34, 27
br i1 %35, label %36, label %69
36: ; preds = %32
%37 = call i32 @read(i32 0, i8* %7, i32 1)
%38 = load i8, i8* %7
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 10
br i1 %40, label %45, label %41
41: ; preds = %36
%42 = load i8, i8* %7
%43 = sext i8 %42 to i32
%44 = icmp eq i32 %43, 13
br i1 %44, label %45, label %56
45: ; preds = %41, %36
%46 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%47 = load i32*, i32** %6
%48 = call i64 @fgets(i8* %46, i32 512, i32* %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %45
%51 = getelementptr inbounds [512 x i8], [512 x i8]* %8, i64 0, i64 0
%52 = load i32, i32* @stdout
%53 = call i32 @fputs(i8* %51, i32 %52)
br label %55
54: ; preds = %45
br label %69
55: ; preds = %50
br label %66
56: ; preds = %41
%57 = load i32*, i32** %6
%58 = call i32 @fgetc(i32* %57)
store i32 %58, i32* %9
%59 = load i32, i32* @EOF
%60 = icmp ne i32 %58, %59
br i1 %60, label %61, label %64
61: ; preds = %56
%62 = load i32, i32* %9
%63 = call i32 @putchar(i32 %62)
br label %65
64: ; preds = %56
br label %69
65: ; preds = %61
br label %66
66: ; preds = %65, %55
%67 = load i32, i32* @stdout
%68 = call i32 @fflush(i32 %67)
br label %32
69: ; preds = %64, %54, %32
%70 = call i32 (...) @reset_terminal()
%71 = load i32, i32* @stdout
%72 = call i32 @fputs(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %71)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"needed a file name\0A\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [19 x i8] c"unable to open %s\0A\00"
@stdout = global i32 0
@EOF = global i32 0
@.str.3 = [6 x i8] c"\1B[0m\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = alloca [512 x i8]
store i8 0, i8* %3
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%6 = icmp slt i32 %0, 2
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %21
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %12
%19 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = tail call i32 @exit(i32 0)
unreachable
21: ; preds = %11
%22 = tail call i32 (...) @set_terminal()
br label %23
23: ; preds = %21, %38
%24 = call i32 @read(i32 0, i8* %3, i32 1)
%25 = load i8, i8* %3
switch i8 %25, label %32 [
i8 13, label %26
i8 10, label %26
]
26: ; preds = %23, %23
%27 = call i64 @fgets(i8* %5, i32 512, i32* %14)
%28 = icmp eq i64 %27, 0
br i1 %28, label %42, label %29
29: ; preds = %26
%30 = load i32, i32* @stdout
%31 = call i32 @fputs(i8* %5, i32 %30)
br label %38
32: ; preds = %23
%33 = call i32 @fgetc(i32* %14)
%34 = load i32, i32* @EOF
%35 = icmp eq i32 %33, %34
br i1 %35, label %42, label %36
36: ; preds = %32
%37 = call i32 @putchar(i32 %33)
br label %38
38: ; preds = %36, %29
%39 = load i32, i32* @stdout
%40 = call i32 @fflush(i32 %39)
%41 = icmp eq i8 %25, 27
br i1 %41, label %42, label %23
42: ; preds = %38, %26, %32
%43 = call i32 (...) @reset_terminal()
%44 = load i32, i32* @stdout
%45 = call i32 @fputs(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %44)
ret i32 0
}