#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);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *fopen(char *, char *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ putchar(int);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ stdin;
int strlen(char *);
int main(void) {
  FILE *fileToBeRead;
  char filename[100];
  int words;
  int counter = 0;
  char quit = 'x';
  printf("Enter Name of the File you would like to view\n");
  fgets(filename, 100, stdin);
  filename[strlen(filename) - 1] = '\0';
  fileToBeRead = fopen(filename, "r");
  if (fileToBeRead == NULL) {
    printf("Cannot open file for reading \n");
    exit(1);
  }
  words = getc(fileToBeRead);
  while (counter < 21 && words != EOF && quit != 'q') {
    putchar(words);
    words = getc(fileToBeRead);
    if (words == '\n' || words == '\r') {
      counter++;
    }
    if (counter == 20) {
      counter = 0;
      printf("\nPress Return to Display 20 more lines or 'q' to quit\n");
      scanf("%c", &quit);
    }
  }
  fclose(fileToBeRead);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x88(%rbp)
movb   $0x78,-0x89(%rbp)
lea    0xe46(%rip),%rdi
call   1060 <printf@plt>
lea    -0x80(%rbp),%rdi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1070 <fgets@plt>
lea    -0x80(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%eax
cltq
movb   $0x0,-0x80(%rbp,%rax,1)
lea    -0x80(%rbp),%rdi
lea    0xe3b(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    121e <main+0x8e>
lea    0xe22(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rdi
call   10b0 <getc@plt>
mov    %eax,-0x84(%rbp)
xor    %eax,%eax
cmpl   $0x15,-0x88(%rbp)
mov    %al,-0x8a(%rbp)
jge    1272 <main+0xe2>
mov    -0x84(%rbp),%ecx
xor    %eax,%eax
mov    0x2d8f(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x8a(%rbp)
je     1272 <main+0xe2>
movsbl -0x89(%rbp),%eax
cmp    $0x71,%eax
setne  %al
mov    %al,-0x8a(%rbp)
mov    -0x8a(%rbp),%al
test   $0x1,%al
jne    1285 <main+0xf5>
jmp    1303 <main+0x173>
mov    -0x84(%rbp),%edi
call   1030 <putchar@plt>
mov    -0x10(%rbp),%rdi
call   10b0 <getc@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0xa,-0x84(%rbp)
je     12b9 <main+0x129>
cmpl   $0xd,-0x84(%rbp)
jne    12c8 <main+0x138>
mov    -0x88(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x88(%rbp)
cmpl   $0x14,-0x88(%rbp)
jne    12fe <main+0x16e>
movl   $0x0,-0x88(%rbp)
lea    0xd6a(%rip),%rdi
call   1060 <printf@plt>
lea    0xd95(%rip),%rdi
lea    -0x89(%rbp),%rsi
call   1080 <scanf@plt>
jmp    122d <main+0x9d>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
movb   $0x78,0xf(%rsp)
lea    0xe5b(%rip),%rdi
call   1040 <puts@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1070 <fgets@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
lea    0xe20(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     12a5 <main+0x115>
mov    %rax,%rbx
mov    %rax,%rdi
call   10b0 <getc@plt>
mov    0x2ddd(%rip),%r13
cmp    0x0(%r13),%eax
jne    1222 <main+0x92>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
xor    %r14d,%r14d
lea    0xdd4(%rip),%r15
lea    0xf(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   10b0 <getc@plt>
mov    %eax,%ebp
cmp    $0xd,%eax
je     125b <main+0xcb>
cmp    $0xa,%ebp
jne    1265 <main+0xd5>
add    $0x1,%r14d
cmp    $0x14,%r14d
je     1280 <main+0xf0>
cmp    0x0(%r13),%ebp
je     1209 <main+0x79>
cmpb   $0x71,0xf(%rsp)
jne    1240 <main+0xb0>
jmp    1209 <main+0x79>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    0xdac(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1080 <scanf@plt>
xor    %r14d,%r14d
cmp    0x0(%r13),%ebp
jne    126b <main+0xdb>
jmp    1209 <main+0x79>
lea    0xdbd(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [47 x i8] c"Enter Name of the File you would like to view\0A\00"
@stdin = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [31 x i8] c"Cannot open file for reading \0A\00"
@EOF = global i32 0
@.str.3 = [55 x i8] c"\0APress Return to Display 20 more lines or 'q' to quit\0A\00"
@.str.4 = [3 x i8] c"%c\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [100 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
store i32 0, i32* %1
store i32 0, i32* %5
store i8 120, i8* %6
%7 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%9 = load i32, i32* @stdin
%10 = call i32 @fgets(i8* %8, i32 100, i32 %9)
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%12 = call i32 @strlen(i8* %11)
%13 = sub nsw i32 %12, 1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 %14
store i8 0, i8* %15
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %2
%18 = load i32*, i32** %2
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %23
20: ; preds = %0
%21 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %0
%24 = load i32*, i32** %2
%25 = call i32 @getc(i32* %24)
store i32 %25, i32* %4
br label %26
26: ; preds = %58, %23
%27 = load i32, i32* %5
%28 = icmp slt i32 %27, 21
br i1 %28, label %29, label %37
29: ; preds = %26
%30 = load i32, i32* %4
%31 = load i32, i32* @EOF
%32 = icmp ne i32 %30, %31
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i8, i8* %6
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 113
br label %37
37: ; preds = %33, %29, %26
%38 = phi i1 [ false, %29 ], [ false, %26 ], [ %36, %33 ]
br i1 %38, label %39, label %59
39: ; preds = %37
%40 = load i32, i32* %4
%41 = call i32 @putchar(i32 %40)
%42 = load i32*, i32** %2
%43 = call i32 @getc(i32* %42)
store i32 %43, i32* %4
%44 = load i32, i32* %4
%45 = icmp eq i32 %44, 10
br i1 %45, label %49, label %46
46: ; preds = %39
%47 = load i32, i32* %4
%48 = icmp eq i32 %47, 13
br i1 %48, label %49, label %52
49: ; preds = %46, %39
%50 = load i32, i32* %5
%51 = add nsw i32 %50, 1
store i32 %51, i32* %5
br label %52
52: ; preds = %49, %46
%53 = load i32, i32* %5
%54 = icmp eq i32 %53, 20
br i1 %54, label %55, label %58
55: ; preds = %52
store i32 0, i32* %5
%56 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0))
%57 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %6)
br label %58
58: ; preds = %55, %52
br label %26
59: ; preds = %37
%60 = load i32*, i32** %2
%61 = call i32 @fclose(i32* %60)
ret i32 0
}

LLVM IR (-O2):
@stdin = global i32 0
@.str.1 = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.4 = [3 x i8] c"%c\00"
@str = [46 x i8] c"Enter Name of the File you would like to view\00"
@str.5 = [54 x i8] c"\0APress Return to Display 20 more lines or 'q' to quit\00"
@str.6 = [30 x i8] c"Cannot open file for reading \00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = alloca i8
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
store i8 120, i8* %2
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
%5 = load i32, i32* @stdin
%6 = call i32 @fgets(i8* %3, i32 100, i32 %5)
%7 = call i32 @strlen(i8* %3)
%8 = add nsw i32 %7, -1
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 %9
store i8 0, i8* %10
%11 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.6, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = call i32 @getc(i32* %11)
%18 = load i32, i32* @EOF
%19 = icmp eq i32 %17, %18
br i1 %19, label %38, label %20
20: ; preds = %16, %31
%21 = phi i32 [ %24, %31 ], [ %17, %16 ]
%22 = phi i32 [ %32, %31 ], [ 0, %16 ]
%23 = call i32 @putchar(i32 %21)
%24 = call i32 @getc(i32* %11)
switch i32 %24, label %31 [
i32 13, label %25
i32 10, label %25
]
25: ; preds = %20, %20
%26 = add nsw i32 %22, 1
%27 = icmp eq i32 %26, 20
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str.5, i64 0, i64 0))
%30 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %2)
br label %31
31: ; preds = %20, %25, %28
%32 = phi i32 [ 0, %28 ], [ %26, %25 ], [ %22, %20 ]
%33 = load i8, i8* %2
%34 = load i32, i32* @EOF
%35 = icmp ne i32 %24, %34
%36 = icmp ne i8 %33, 113
%37 = select i1 %35, i1 %36, i1 false
br i1 %37, label %20, label %38
38: ; preds = %31, %16
%39 = call i32 @fclose(i32* %11)
ret i32 0
}