#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 O_CREAT;
int O_EXCL;
int O_RDWR;
int O_WRONLY;
int /*<<< orphan*/ *fdopen(int, char const *);
int /*<<< orphan*/ *fopen(char const *, char const *);
int safe_open(char const *, int);
FILE *safe_fopen(const char *path, const char *mode) {
  if (mode[0] == 'w') {
    int fd;
    int flags = O_CREAT | O_EXCL;
    if (mode[1] == '+')
      flags |= O_RDWR;
    else
      flags |= O_WRONLY;
    if ((fd = safe_open(path, flags)) < 0)
      return (NULL);
    return (fdopen(fd, mode));
  } else
    return (fopen(path, mode));
}


x86 assembly (clang, -O0):
<safe_fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x77,%eax
jne    11d3 <safe_fopen+0xa3>
mov    0x2ea1(%rip),%rax
mov    (%rax),%eax
mov    0x2e70(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movsbl 0x1(%rax),%eax
cmp    $0x2b,%eax
jne    118a <safe_fopen+0x5a>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1199 <safe_fopen+0x69>
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1050 <safe_open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11be <safe_fopen+0x8e>
movq   $0x0,-0x8(%rbp)
jmp    11e4 <safe_fopen+0xb4>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1030 <fdopen@plt>
mov    %rax,-0x8(%rbp)
jmp    11e4 <safe_fopen+0xb4>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_fopen>:
push   %rbx
mov    %rsi,%rbx
cmpb   $0x77,(%rsi)
jne    115a <safe_fopen+0x2a>
mov    0x2eb8(%rip),%rax
mov    0x2e89(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
cmpb   $0x2b,0x1(%rbx)
jne    1163 <safe_fopen+0x33>
mov    0x2e88(%rip),%rax
jmp    116a <safe_fopen+0x3a>
mov    %rbx,%rsi
pop    %rbx
jmp    1040 <fopen@plt>
mov    0x2e56(%rip),%rax
or     (%rax),%esi
call   1050 <safe_open@plt>
test   %eax,%eax
js     1180 <safe_fopen+0x50>
mov    %eax,%edi
mov    %rbx,%rsi
pop    %rbx
jmp    1030 <fdopen@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_RDWR = global i32 0
@O_WRONLY = global i32 0
define i32* @safe_fopen(i8* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = getelementptr inbounds i8, i8* %8, i64 0
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %11, 119
br i1 %12, label %13, label %40
13: ; preds = %2
%14 = load i32, i32* @O_CREAT
%15 = load i32, i32* @O_EXCL
%16 = or i32 %14, %15
store i32 %16, i32* %7
%17 = load i8*, i8** %5
%18 = getelementptr inbounds i8, i8* %17, i64 1
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 43
br i1 %21, label %22, label %26
22: ; preds = %13
%23 = load i32, i32* @O_RDWR
%24 = load i32, i32* %7
%25 = or i32 %24, %23
store i32 %25, i32* %7
br label %30
26: ; preds = %13
%27 = load i32, i32* @O_WRONLY
%28 = load i32, i32* %7
%29 = or i32 %28, %27
store i32 %29, i32* %7
br label %30
30: ; preds = %26, %22
%31 = load i8*, i8** %4
%32 = load i32, i32* %7
%33 = call i32 @safe_open(i8* %31, i32 %32)
store i32 %33, i32* %6
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %30
store i32* null, i32** %3
br label %44
36: ; preds = %30
%37 = load i32, i32* %6
%38 = load i8*, i8** %5
%39 = call i32* @fdopen(i32 %37, i8* %38)
store i32* %39, i32** %3
br label %44
40: ; preds = %2
%41 = load i8*, i8** %4
%42 = load i8*, i8** %5
%43 = call i32* @fopen(i8* %41, i8* %42)
store i32* %43, i32** %3
br label %44
44: ; preds = %40, %36, %35
%45 = load i32*, i32** %3
ret i32* %45
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_RDWR = global i32 0
@O_WRONLY = global i32 0
define noalias i32* @safe_fopen(i8* %0, i8* nocapture %1) {
%3 = load i8, i8* %1
%4 = icmp eq i8 %3, 119
br i1 %4, label %5, label %20
5: ; preds = %2
%6 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_EXCL
%8 = or i32 %7, %6
%9 = getelementptr inbounds i8, i8* %1, i64 1
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 43
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_WRONLY
%14 = select i1 %11, i32 %12, i32 %13
%15 = or i32 %8, %14
%16 = tail call i32 @safe_open(i8* %0, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %5
%19 = tail call i32* @fdopen(i32 %16, i8* %1)
br label %22
20: ; preds = %2
%21 = tail call i32* @fopen(i8* %0, i8* %1)
br label %22
22: ; preds = %18, %5, %20
%23 = phi i32* [ %21, %20 ], [ %19, %18 ], [ null, %5 ]
ret i32* %23
}