#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 S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWOTH;
int S_IWUSR;
int __fmodeflags(char const *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ *fdopen(int, char const *);
int open(char const *, int, int);
FILE *fopen(const char *restrict path, const char *restrict mode) {
  int flags = __fmodeflags(mode);
  if (flags == -1)
    return NULL;
  if (!(flags & O_CREAT))
    flags &= ~O_EXCL;
  int fd = open(path, flags,
                S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
  if (fd == -1)
    return NULL;
  FILE *result = fdopen(fd, mode);
  if (!result) {
    close(fd);
  }
  return result;
}


x86 assembly (clang, -O0):
<fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <__fmodeflags@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1173 <fopen+0x33>
movq   $0x0,-0x8(%rbp)
jmp    1221 <fopen+0xe1>
mov    -0x1c(%rbp),%eax
mov    0x2e7b(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    119a <fopen+0x5a>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
mov    0x2e27(%rip),%rax
or     (%rax),%edx
mov    0x2e06(%rip),%rax
or     (%rax),%edx
mov    0x2e1d(%rip),%rax
or     (%rax),%edx
mov    0x2dec(%rip),%rax
or     (%rax),%edx
mov    0x2dcb(%rip),%rax
or     (%rax),%edx
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11f6 <fopen+0xb6>
movq   $0x0,-0x8(%rbp)
jmp    1221 <fopen+0xe1>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1050 <fdopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1219 <fopen+0xd9>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fopen>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <__fmodeflags@plt>
cmp    $0xffffffff,%eax
je     11d0 <fopen+0x90>
mov    0x2e9a(%rip),%rcx
mov    (%rcx),%ecx
and    %eax,%ecx
neg    %ecx
sbb    %esi,%esi
mov    0x2e43(%rip),%rcx
mov    0x2e64(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e41(%rip),%rcx
or     (%rcx),%edx
mov    0x2e58(%rip),%rcx
or     (%rcx),%edx
mov    0x2e37(%rip),%rcx
mov    (%rcx),%ecx
not    %ecx
or     %ecx,%esi
mov    0x2e1a(%rip),%rcx
or     (%rcx),%edx
and    %eax,%esi
mov    0x2df7(%rip),%rax
or     (%rax),%edx
mov    %r14,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11d0 <fopen+0x90>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
call   1050 <fdopen@plt>
test   %rax,%rax
jne    11d2 <fopen+0x92>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
define i32* @fopen(i8* noalias %0, i8* noalias %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %5
%10 = call i32 @__fmodeflags(i8* %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %2
store i32* null, i32** %3
br label %53
14: ; preds = %2
%15 = load i32, i32* %6
%16 = load i32, i32* @O_CREAT
%17 = and i32 %15, %16
%18 = icmp ne i32 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %14
%20 = load i32, i32* @O_EXCL
%21 = xor i32 %20, -1
%22 = load i32, i32* %6
%23 = and i32 %22, %21
store i32 %23, i32* %6
br label %24
24: ; preds = %19, %14
%25 = load i8*, i8** %4
%26 = load i32, i32* %6
%27 = load i32, i32* @S_IRUSR
%28 = load i32, i32* @S_IWUSR
%29 = or i32 %27, %28
%30 = load i32, i32* @S_IRGRP
%31 = or i32 %29, %30
%32 = load i32, i32* @S_IWGRP
%33 = or i32 %31, %32
%34 = load i32, i32* @S_IROTH
%35 = or i32 %33, %34
%36 = load i32, i32* @S_IWOTH
%37 = or i32 %35, %36
%38 = call i32 @open(i8* %25, i32 %26, i32 %37)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %42
41: ; preds = %24
store i32* null, i32** %3
br label %53
42: ; preds = %24
%43 = load i32, i32* %7
%44 = load i8*, i8** %5
%45 = call i32* @fdopen(i32 %43, i8* %44)
store i32* %45, i32** %8
%46 = load i32*, i32** %8
%47 = icmp ne i32* %46, null
br i1 %47, label %51, label %48
48: ; preds = %42
%49 = load i32, i32* %7
%50 = call i32 @close(i32 %49)
br label %51
51: ; preds = %48, %42
%52 = load i32*, i32** %8
store i32* %52, i32** %3
br label %53
53: ; preds = %51, %41, %13
%54 = load i32*, i32** %3
ret i32* %54
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@S_IROTH = global i32 0
@S_IWOTH = global i32 0
define noalias i32* @fopen(i8* noalias nocapture %0, i8* noalias %1) {
%3 = tail call i32 @__fmodeflags(i8* %1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %31, label %5
5: ; preds = %2
%6 = load i32, i32* @O_CREAT
%7 = and i32 %6, %3
%8 = icmp eq i32 %7, 0
%9 = load i32, i32* @O_EXCL
%10 = xor i32 %9, -1
%11 = select i1 %8, i32 %10, i32 -1
%12 = and i32 %11, %3
%13 = load i32, i32* @S_IRUSR
%14 = load i32, i32* @S_IWUSR
%15 = or i32 %14, %13
%16 = load i32, i32* @S_IRGRP
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IWGRP
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IROTH
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IWOTH
%23 = or i32 %21, %22
%24 = tail call i32 @open(i8* %0, i32 %12, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %31, label %26
26: ; preds = %5
%27 = tail call i32* @fdopen(i32 %24, i8* %1)
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = tail call i32 @close(i32 %24)
br label %31
31: ; preds = %5, %29, %26, %2
%32 = phi i32* [ null, %2 ], [ null, %5 ], [ null, %29 ], [ %27, %26 ]
ret i32* %32
}