#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*/ puts(char *);
unsigned int file_exists(char *filename) {
  FILE *fh = 0;
  if (!filename) {
    puts("file_exists: filename was null");
    return 0;
  }
  fh = fopen(filename, "r");
  if (!fh) {
    return 0;
  }
  fclose(fh);
  return 1;
}


x86 assembly (clang, -O0):
<file_exists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1167 <file_exists+0x37>
lea    0xeaa(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,-0x4(%rbp)
jmp    11a2 <file_exists+0x72>
mov    -0x10(%rbp),%rdi
lea    0xead(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1192 <file_exists+0x62>
movl   $0x0,-0x4(%rbp)
jmp    11a2 <file_exists+0x72>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_exists>:
push   %rax
test   %rdi,%rdi
je     1156 <file_exists+0x26>
lea    0xee2(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1162 <file_exists+0x32>
mov    %rax,%rdi
call   1040 <fclose@plt>
mov    $0x1,%eax
pop    %rcx
ret
lea    0xea3(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [31 x i8] c"file_exists: filename was null\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @file_exists(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
store i8* %0, i8** %3
store i32* null, i32** %4
%5 = load i8*, i8** %3
%6 = icmp ne i8* %5, null
br i1 %6, label %9, label %7
7: ; preds = %1
%8 = call i32 @puts(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %2
br label %18
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %4
%12 = load i32*, i32** %4
%13 = icmp ne i32* %12, null
br i1 %13, label %15, label %14
14: ; preds = %9
store i32 0, i32* %2
br label %18
15: ; preds = %9
%16 = load i32*, i32** %4
%17 = call i32 @fclose(i32* %16)
store i32 1, i32* %2
br label %18
18: ; preds = %15, %14, %7
%19 = load i32, i32* %2
ret i32 %19
}

LLVM IR (-O2):
@.str = [31 x i8] c"file_exists: filename was null\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @file_exists(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %10
5: ; preds = %1
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %10, label %8
8: ; preds = %5
%9 = tail call i32 @fclose(i32* %6)
br label %10
10: ; preds = %5, %8, %3
%11 = phi i32 [ 1, %8 ], [ 0, %3 ], [ 0, %5 ]
ret i32 %11
}