#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ gets(char *);
int /*<<< orphan*/ printf(char *);
main() {
  FILE *fp;
  char filename[10];
  printf("Please enter the filename you wish to open: ");
  gets(filename);
  fp = fopen(filename, "r");
  if (fp == NULL) {
    printf("\n\n Error: File cannot be opened");
  } else {
    printf("\n\n File opened successfully\n\n");
    fclose(fp);
    printf("\n\n File has been now closed \n\n");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
call   1040 <printf@plt>
lea    -0x1a(%rbp),%rdi
call   1050 <gets@plt>
lea    -0x1a(%rbp),%rdi
lea    0xebe(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1194 <main+0x54>
lea    0xea5(%rip),%rdi
call   1040 <printf@plt>
jmp    11b5 <main+0x75>
lea    0xeb4(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xebd(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xea4(%rip),%rdi
call   1050 <printf@plt>
lea    0x6(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <gets@plt>
lea    0xeb8(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11a7 <main+0x57>
mov    %rax,%rbx
lea    0xec3(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xecc(%rip),%rdi
call   1030 <puts@plt>
jmp    11b3 <main+0x63>
lea    0xe81(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [45 x i8] c"Please enter the filename you wish to open: \00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [32 x i8] c"\0A\0A Error: File cannot be opened\00"
@.str.3 = [30 x i8] c"\0A\0A File opened successfully\0A\0A\00"
@.str.4 = [31 x i8] c"\0A\0A File has been now closed \0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [10 x i8]
store i32 0, i32* %1
%4 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%5 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%6 = call i32 @gets(i8* %5)
%7 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %2
%9 = load i32*, i32** %2
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %18
13: ; preds = %0
%14 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
%15 = load i32*, i32** %2
%16 = call i32 @fclose(i32* %15)
%17 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %18
18: ; preds = %13, %11
%19 = load i32, i32* %1
ret i32 %19
}

LLVM IR (-O2):
@.str = [45 x i8] c"Please enter the filename you wish to open: \00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [32 x i8] c"\0A\0A Error: File cannot be opened\00"
@str = [29 x i8] c"\0A\0A File opened successfully\0A\00"
@str.5 = [30 x i8] c"\0A\0A File has been now closed \0A\00"
define i32 @main() {
%1 = alloca [10 x i8]
%2 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%3 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0))
%4 = call i32 @gets(i8* %2)
%5 = call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
br label %13
9: ; preds = %0
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%11 = call i32 @fclose(i32* %5)
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.5, i64 0, i64 0))
br label %13
13: ; preds = %9, %7
ret i32 0
}