#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 const *, char *);
int /*<<< orphan*/ ioutil_remove(char const *);
int util_check_filename_access(const char *filename) {
  FILE *file = NULL;
  file = fopen(filename, "r");
  if (file == NULL) {
    file = fopen(filename, "w");
    if (file == NULL) {
      return -1;
    } else {
      fclose(file);
      ioutil_remove(filename);
      return 0;
    }
  } else {
    fclose(file);
    return 0;
  }
}


x86 assembly (clang, -O0):
<util_check_filename_access>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb1(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ac <util_check_filename_access+0x7c>
mov    -0x10(%rbp),%rdi
lea    0xe94(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    118e <util_check_filename_access+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <util_check_filename_access+0x8c>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1040 <ioutil_remove@plt>
movl   $0x0,-0x4(%rbp)
jmp    11bc <util_check_filename_access+0x8c>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_check_filename_access>:
push   %rbx
mov    %rdi,%rbx
lea    0xec5(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     114f <util_check_filename_access+0x1f>
mov    %rax,%rdi
call   1030 <fclose@plt>
jmp    1173 <util_check_filename_access+0x43>
lea    0xeac(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     1177 <util_check_filename_access+0x47>
mov    %rax,%rdi
call   1030 <fclose@plt>
mov    %rbx,%rdi
call   1040 <ioutil_remove@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
define i32 @util_check_filename_access(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 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %4
%7 = load i32*, i32** %4
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %20
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 eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %9
store i32 -1, i32* %2
br label %23
15: ; preds = %9
%16 = load i32*, i32** %4
%17 = call i32 @fclose(i32* %16)
%18 = load i8*, i8** %3
%19 = call i32 @ioutil_remove(i8* %18)
store i32 0, i32* %2
br label %23
20: ; preds = %1
%21 = load i32*, i32** %4
%22 = call i32 @fclose(i32* %21)
store i32 0, i32* %2
br label %23
23: ; preds = %20, %15, %14
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
define i32 @util_check_filename_access(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %10
4: ; preds = %1
%5 = tail call i32* @fopen(i8* %0, 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 %12, label %7
7: ; preds = %4
%8 = tail call i32 @fclose(i32* %5)
%9 = tail call i32 @ioutil_remove(i8* %0)
br label %12
10: ; preds = %1
%11 = tail call i32 @fclose(i32* %2)
br label %12
12: ; preds = %4, %10, %7
%13 = phi i32 [ 0, %7 ], [ 0, %10 ], [ -1, %4 ]
ret i32 %13
}