#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 /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  char buf[100];
  char filename[60];
  FILE *fp;
  puts("Enter name of text file to display: ");
  gets(filename);
  if ((fp = fopen(filename, "r")) == NULL) {
    fprintf(stderr, "Error opening file.");
    exit(1);
  }
  while (!feof(fp)) {
    fgets(buf, 100, fp);
    printf("%s", buf);
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe50(%rip),%rdi
call   1030 <puts@plt>
lea    -0xc0(%rbp),%rdi
call   1090 <gets@plt>
lea    -0xc0(%rbp),%rdi
lea    0xe56(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0xc8(%rbp)
cmp    $0x0,%rax
jne    1204 <main+0x74>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    1209 <main+0x79>
mov    -0xc8(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    122a <main+0x9a>
jmp    1254 <main+0xc4>
lea    -0x80(%rbp),%rdi
mov    -0xc8(%rbp),%rdx
mov    $0x64,%esi
call   1060 <fgets@plt>
lea    -0x80(%rbp),%rsi
lea    0xdf1(%rip),%rdi
call   1050 <printf@plt>
jmp    1209 <main+0x79>
mov    -0xc8(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0xb0,%rsp
lea    0xe5d(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1090 <gets@plt>
lea    0xe6b(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     122e <main+0x9e>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <feof@plt>
test   %eax,%eax
jne    1217 <main+0x87>
lea    0x40(%rsp),%r14
lea    0xe59(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    $0x64,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1080 <feof@plt>
test   %eax,%eax
je     11f0 <main+0x60>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0xde9(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [37 x i8] c"Enter name of text file to display: \00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"Error opening file.\00"
@.str.3 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [100 x i8]
%7 = alloca [60 x i8]
%8 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 @puts(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%10 = getelementptr inbounds [60 x i8], [60 x i8]* %7, i64 0, i64 0
%11 = call i32 @gets(i8* %10)
%12 = getelementptr inbounds [60 x i8], [60 x i8]* %7, i64 0, i64 0
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %8
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %19
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
br label %20
20: ; preds = %25, %19
%21 = load i32*, i32** %8
%22 = call i32 @feof(i32* %21)
%23 = icmp ne i32 %22, 0
%24 = xor i1 %23, true
br i1 %24, label %25, label %31
25: ; preds = %20
%26 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%27 = load i32*, i32** %8
%28 = call i32 @fgets(i8* %26, i32 100, i32* %27)
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%30 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %29)
br label %20
31: ; preds = %20
%32 = load i32*, i32** %8
%33 = call i32 @fclose(i32* %32)
ret i32 0
}

LLVM IR (-O2):
@.str = [37 x i8] c"Enter name of text file to display: \00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"Error opening file.\00"
@.str.3 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [100 x i8]
%4 = alloca [60 x i8]
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [60 x i8], [60 x i8]* %4, i64 0, i64 0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @gets(i8* %6)
%9 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = call i32 @feof(i32* %9)
%13 = icmp eq i32 %12, 0
br i1 %13, label %18, label %23
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %11, %18
%19 = call i32 @fgets(i8* %5, i32 100, i32* %9)
%20 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %5)
%21 = call i32 @feof(i32* %9)
%22 = icmp eq i32 %21, 0
br i1 %22, label %18, label %23
23: ; preds = %18, %11
%24 = call i32 @fclose(i32* %9)
ret i32 0
}