#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 *);
FILE *check_and_open_file(char *fname) {
  FILE *f;
  char *c;
  c = fname;
  while (*c)
    c++;
  if (c > fname)
    c--;
  if (*c == '/') {
    return (0);
  }
  if (!fname)
    return (0);
  if (!*fname)
    return (0);
  f = fopen(fname, "r");
  if (f) {
    fclose(f);
    return (0);
  }
  f = fopen(fname, "w");
  return (f);
}


x86 assembly (clang, -O0):
<check_and_open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     1152 <check_and_open_file+0x32>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1134 <check_and_open_file+0x14>
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    116c <check_and_open_file+0x4c>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1189 <check_and_open_file+0x69>
movq   $0x0,-0x8(%rbp)
jmp    120c <check_and_open_file+0xec>
cmpq   $0x0,-0x10(%rbp)
jne    11a1 <check_and_open_file+0x81>
movq   $0x0,-0x8(%rbp)
jmp    120c <check_and_open_file+0xec>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11bb <check_and_open_file+0x9b>
movq   $0x0,-0x8(%rbp)
jmp    120c <check_and_open_file+0xec>
mov    -0x10(%rbp),%rdi
lea    0xe3a(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11f0 <check_and_open_file+0xd0>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
jmp    120c <check_and_open_file+0xec>
mov    -0x10(%rbp),%rdi
lea    0xe07(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_and_open_file>:
push   %rbx
mov    %rdi,%rbx
lea    -0x1(%rdi),%rax
nopl   0x0(%rax,%rax,1)
cmpb   $0x0,0x1(%rax)
lea    0x1(%rax),%rax
jne    1130 <check_and_open_file+0x10>
cmp    %rax,%rbx
sbb    $0x0,%rax
test   %rbx,%rbx
je     116c <check_and_open_file+0x4c>
cmpb   $0x2f,(%rax)
je     116c <check_and_open_file+0x4c>
cmpb   $0x0,(%rbx)
je     116c <check_and_open_file+0x4c>
lea    0xea9(%rip),%rsi
mov    %rbx,%rdi
call   1040 <fopen@plt>
test   %rax,%rax
je     1170 <check_and_open_file+0x50>
mov    %rax,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe8b(%rip),%rsi
mov    %rbx,%rdi
pop    %rbx
jmp    1040 <fopen@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
define i32* @check_and_open_file(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
store i8* %6, i8** %5
br label %7
7: ; preds = %11, %1
%8 = load i8*, i8** %5
%9 = load i8, i8* %8
%10 = icmp ne i8 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %7
%12 = load i8*, i8** %5
%13 = getelementptr inbounds i8, i8* %12, i32 1
store i8* %13, i8** %5
br label %7
14: ; preds = %7
%15 = load i8*, i8** %5
%16 = load i8*, i8** %3
%17 = icmp ugt i8* %15, %16
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i8*, i8** %5
%20 = getelementptr inbounds i8, i8* %19, i32 -1
store i8* %20, i8** %5
br label %21
21: ; preds = %18, %14
%22 = load i8*, i8** %5
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 47
br i1 %25, label %26, label %27
26: ; preds = %21
store i32* null, i32** %2
br label %48
27: ; preds = %21
%28 = load i8*, i8** %3
%29 = icmp ne i8* %28, null
br i1 %29, label %31, label %30
30: ; preds = %27
store i32* null, i32** %2
br label %48
31: ; preds = %27
%32 = load i8*, i8** %3
%33 = load i8, i8* %32
%34 = icmp ne i8 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %31
store i32* null, i32** %2
br label %48
36: ; preds = %31
%37 = load i8*, i8** %3
%38 = call i32* @fopen(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %38, i32** %4
%39 = load i32*, i32** %4
%40 = icmp ne i32* %39, null
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i32*, i32** %4
%43 = call i32 @fclose(i32* %42)
store i32* null, i32** %2
br label %48
44: ; preds = %36
%45 = load i8*, i8** %3
%46 = call i32* @fopen(i8* %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %46, i32** %4
%47 = load i32*, i32** %4
store i32* %47, i32** %2
br label %48
48: ; preds = %44, %41, %35, %30, %26
%49 = load i32*, i32** %2
ret i32* %49
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"w\00"
define noalias i32* @check_and_open_file(i8* %0) {
br label %2
2: ; preds = %2, %1
%3 = phi i8* [ %0, %1 ], [ %6, %2 ]
%4 = load i8, i8* %3
%5 = icmp eq i8 %4, 0
%6 = getelementptr inbounds i8, i8* %3, i64 1
br i1 %5, label %7, label %2
7: ; preds = %2
%8 = icmp ugt i8* %3, %0
%9 = sext i1 %8 to i64
%10 = getelementptr i8, i8* %3, i64 %9
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 47
%13 = icmp eq i8* %0, null
%14 = or i1 %13, %12
br i1 %14, label %25, label %15
15: ; preds = %7
%16 = load i8, i8* %0
%17 = icmp eq i8 %16, 0
br i1 %17, label %25, label %18
18: ; preds = %15
%19 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = tail call i32 @fclose(i32* %19)
br label %25
23: ; preds = %18
%24 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %15, %7, %23, %21
%26 = phi i32* [ null, %21 ], [ %24, %23 ], [ null, %7 ], [ null, %15 ]
ret i32* %26
}