#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*/ fclose(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 main(void) {
  char fileName[80];
  printf("Write name of file to open: ");
  scanf("%s", fileName);
  printf("\n");
  printf("Content of \"%s\":\n", fileName);
  FILE *inputFile;
  inputFile = fopen(fileName, "r");
  if (inputFile == NULL) {
    printf("Error opening file.\n");
    return 1;
  } else {
    int c;
    do {
      c = getc(inputFile);
      putchar(c);
    } while (c != EOF);
    fclose(inputFile);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x60(%rbp),%rsi
lea    0xe95(%rip),%rdi
call   1060 <scanf@plt>
lea    0xeb3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x60(%rbp),%rsi
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x60(%rbp),%rdi
lea    0xe7a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
jne    11e6 <main+0x86>
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1221 <main+0xc1>
jmp    11eb <main+0x8b>
mov    -0x68(%rbp),%rdi
call   1080 <getc@plt>
mov    %eax,-0x6c(%rbp)
mov    -0x6c(%rbp),%edi
call   1030 <putchar@plt>
mov    -0x6c(%rbp),%eax
mov    0x2ddf(%rip),%rcx
cmp    (%rcx),%eax
jne    11eb <main+0x8b>
mov    -0x68(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
lea    0xe81(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe90(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   1070 <scanf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xe77(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe78(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     1202 <main+0x92>
mov    %rax,%rbx
mov    0x2e17(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1090 <getc@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1030 <putchar@plt>
cmp    (%r14),%ebp
jne    11e0 <main+0x70>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    1213 <main+0xa3>
lea    0xe2b(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"Write name of file to open: \00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [18 x i8] c"Content of \22%s\22:\0A\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [21 x i8] c"Error opening file.\0A\00"
@EOF = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [80 x i8]
%3 = alloca i32*
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%6 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%7 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%9 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%11 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %12, i32** %3
%13 = load i32*, i32** %3
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %31
17: ; preds = %0
br label %18
18: ; preds = %23, %17
%19 = load i32*, i32** %3
%20 = call i32 @getc(i32* %19)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = call i32 @putchar(i32 %21)
br label %23
23: ; preds = %18
%24 = load i32, i32* %4
%25 = load i32, i32* @EOF
%26 = icmp ne i32 %24, %25
br i1 %26, label %18, label %27
27: ; preds = %23
%28 = load i32*, i32** %3
%29 = call i32 @fclose(i32* %28)
br label %30
30: ; preds = %27
store i32 0, i32* %1
br label %31
31: ; preds = %30, %15
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
@.str = [29 x i8] c"Write name of file to open: \00"
@.str.1 = [3 x i8] c"%s\00"
@.str.3 = [18 x i8] c"Content of \22%s\22:\0A\00"
@.str.4 = [2 x i8] c"r\00"
@EOF = global i32 0
@str = [20 x i8] c"Error opening file.\00"
define i32 @main() {
%1 = alloca [80 x i8]
%2 = getelementptr inbounds [80 x i8], [80 x i8]* %1, i64 0, i64 0
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%5 = call i32 @putchar(i32 10)
%6 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%7 = call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %18
11: ; preds = %0, %11
%12 = call i32 @getc(i32* %7)
%13 = call i32 @putchar(i32 %12)
%14 = load i32, i32* @EOF
%15 = icmp eq i32 %12, %14
br i1 %15, label %16, label %11
16: ; preds = %11
%17 = call i32 @fclose(i32* %7)
br label %18
18: ; preds = %16, %9
%19 = phi i32 [ 1, %9 ], [ 0, %16 ]
ret i32 %19
}