#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*/ backup_oldfile(char *);
char *fc_add(char *);
char *fc_search(char *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *get_fullpath(char *);
FILE *fileopen(char *filename, char type) {
  char *fc = fc_search(filename);
  char *fullpath;
  FILE *fp;
  if (fc == NULL) {
    if (type == 'r')
      return NULL;
    fc = fc_add(filename);
  }
  fullpath = get_fullpath(fc);
  if (type == 'w') {
    backup_oldfile(fullpath);
    fp = fopen(fullpath, "w");
  } else {
    fp = fopen(fullpath, "r");
  }
  free(fullpath);
  return fp;
}


x86 assembly (clang, -O0):
<fileopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %sil,%al
mov    %rdi,-0x10(%rbp)
mov    %al,-0x11(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <fc_search@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b1 <fileopen+0x51>
movsbl -0x11(%rbp),%eax
cmp    $0x72,%eax
jne    11a4 <fileopen+0x44>
movq   $0x0,-0x8(%rbp)
jmp    1212 <fileopen+0xb2>
mov    -0x10(%rbp),%rdi
call   1040 <fc_add@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <get_fullpath@plt>
mov    %rax,-0x28(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0x77,%eax
jne    11ed <fileopen+0x8d>
mov    -0x28(%rbp),%rdi
call   1050 <backup_oldfile@plt>
mov    -0x28(%rbp),%rdi
lea    0xe21(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
jmp    1201 <fileopen+0xa1>
mov    -0x28(%rbp),%rdi
lea    0xe0a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fileopen>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1070 <fc_search@plt>
test   %rax,%rax
jne    1184 <fileopen+0x24>
cmp    $0x72,%bpl
jne    117c <fileopen+0x1c>
xor    %eax,%eax
jmp    11c3 <fileopen+0x63>
mov    %rbx,%rdi
call   1040 <fc_add@plt>
mov    %rax,%rdi
call   1060 <get_fullpath@plt>
mov    %rax,%rbx
cmp    $0x77,%bpl
jne    11a6 <fileopen+0x46>
mov    %rbx,%rdi
call   1050 <backup_oldfile@plt>
lea    0xe5c(%rip),%rsi
jmp    11ad <fileopen+0x4d>
lea    0xe55(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"r\00"
define noalias i32* @fileopen(i8* %0, i8 signext %1) {
%3 = tail call i8* @fc_search(i8* %0)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = icmp eq i8 %1, 114
br i1 %6, label %19, label %7
7: ; preds = %5
%8 = tail call i8* @fc_add(i8* %0)
br label %9
9: ; preds = %7, %2
%10 = phi i8* [ %8, %7 ], [ %3, %2 ]
%11 = tail call i8* @get_fullpath(i8* %10)
%12 = icmp eq i8 %1, 119
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @backup_oldfile(i8* %11)
br label %15
15: ; preds = %9, %13
%16 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %13 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %9 ]
%17 = tail call i32* @fopen(i8* %11, i8* %16)
%18 = tail call i32 @free(i8* %11)
br label %19
19: ; preds = %5, %15
%20 = phi i32* [ %17, %15 ], [ null, %5 ]
ret i32* %20
}