#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*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ printf(char *);
int main() {
  printf("creating the file...\n");
  FILE *f = fopen("tmp/foo", "w");
  printf("writing to the file...\n");
  fprintf(f, "this is good\n");
  printf("closing the file...\n");
  fclose(f);
  printf("I am about to open the file...\n");
  f = fopen("tmp/Foo", "r");
  printf("I am about to finally close the file...\n");
  if (f)
    fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
call   1040 <printf@plt>
lea    0xeb4(%rip),%rdi
lea    0xeb5(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    0xea7(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    0xeaf(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xeb1(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xeb1(%rip),%rdi
call   1040 <printf@plt>
lea    0xec5(%rip),%rdi
lea    0xec6(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    0xeb8(%rip),%rdi
call   1040 <printf@plt>
cmpq   $0x0,-0x10(%rbp)
je     11e6 <main+0xa6>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xeda(%rip),%rdi
call   1030 <puts@plt>
lea    0xeac(%rip),%rdi
lea    0xead(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
lea    0xecd(%rip),%rdi
call   1030 <puts@plt>
lea    0xe94(%rip),%rdi
mov    $0xd,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1060 <fwrite@plt>
lea    0xebf(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xebf(%rip),%rdi
call   1030 <puts@plt>
lea    0xe69(%rip),%rdi
lea    0xe6a(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
lea    0xebc(%rip),%rdi
call   1030 <puts@plt>
test   %rbx,%rbx
je     11d7 <main+0x97>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [22 x i8] c"creating the file...\0A\00"
@.str.1 = [8 x i8] c"tmp/foo\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [24 x i8] c"writing to the file...\0A\00"
@.str.4 = [14 x i8] c"this is good\0A\00"
@.str.5 = [21 x i8] c"closing the file...\0A\00"
@.str.6 = [32 x i8] c"I am about to open the file...\0A\00"
@.str.7 = [8 x i8] c"tmp/Foo\00"
@.str.8 = [2 x i8] c"r\00"
@.str.9 = [41 x i8] c"I am about to finally close the file...\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
store i32 0, i32* %1
%3 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%4 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %4, i32** %2
%5 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%6 = load i32*, i32** %2
%7 = call i32 @fprintf(i32* %6, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%8 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0))
%9 = load i32*, i32** %2
%10 = call i32 @fclose(i32* %9)
%11 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
%12 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
store i32* %12, i32** %2
%13 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.9, i64 0, i64 0))
%14 = load i32*, i32** %2
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %0
%17 = load i32*, i32** %2
%18 = call i32 @fclose(i32* %17)
br label %19
19: ; preds = %16, %0
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [8 x i8] c"tmp/foo\00"
@.str.2 = [2 x i8] c"w\00"
@.str.4 = [14 x i8] c"this is good\0A\00"
@.str.7 = [8 x i8] c"tmp/Foo\00"
@.str.8 = [2 x i8] c"r\00"
@str = [21 x i8] c"creating the file...\00"
@str.10 = [23 x i8] c"writing to the file...\00"
@str.11 = [20 x i8] c"closing the file...\00"
@str.12 = [31 x i8] c"I am about to open the file...\00"
@str.13 = [40 x i8] c"I am about to finally close the file...\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.10, i64 0, i64 0))
%4 = tail call i64 @fwrite(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i64 13, i64 1, i32* %2)
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.11, i64 0, i64 0))
%6 = tail call i32 @fclose(i32* %2)
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.12, i64 0, i64 0))
%8 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.13, i64 0, i64 0))
%10 = icmp eq i32* %8, null
br i1 %10, label %13, label %11
11: ; preds = %0
%12 = tail call i32 @fclose(i32* %8)
br label %13
13: ; preds = %11, %0
ret i32 0
}