#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_RDONLY;
int O_WRONLY;
int /*<<< orphan*/ gendep__register_open(char const *, int);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ *real_fopen(char const *, char const *);
scalar_t__ strchr(char const *, char);
FILE *__fopen(const char *path, const char *mode) {
  FILE *f;
  int binmode;
  if (0)
    printf("fopen(%s)\n", path);
  f = real_fopen(path, mode);
  if (f) {
    if (strchr(mode, 'w') || strchr(mode, 'a')) {
      binmode = O_WRONLY;
    } else {
      binmode = O_RDONLY;
    }
    gendep__register_open(path, binmode);
  }
  return f;
}


x86 assembly (clang, -O0):
<__fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <real_fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b5 <__fopen+0x85>
mov    -0x10(%rbp),%rdi
mov    $0x77,%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
jne    118c <__fopen+0x5c>
mov    -0x10(%rbp),%rdi
mov    $0x61,%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     119d <__fopen+0x6d>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    11a9 <__fopen+0x79>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <gendep__register_open@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__fopen>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
call   1040 <real_fopen@plt>
mov    %rax,%r15
test   %rax,%rax
je     1186 <__fopen+0x56>
mov    %rbx,%rdi
mov    $0x77,%esi
call   1030 <strchr@plt>
test   %rax,%rax
jne    1175 <__fopen+0x45>
mov    %rbx,%rdi
mov    $0x61,%esi
call   1030 <strchr@plt>
test   %rax,%rax
jne    1175 <__fopen+0x45>
mov    0x2e75(%rip),%rax
jmp    117c <__fopen+0x4c>
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1050 <gendep__register_open@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_RDONLY = global i32 0
define i32* @__fopen(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
%8 = load i8*, i8** %4
%9 = call i32* @real_fopen(i8* %7, i8* %8)
store i32* %9, i32** %5
%10 = load i32*, i32** %5
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %28
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = call i64 @strchr(i8* %13, i8 signext 119)
%15 = icmp ne i64 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %12
%17 = load i8*, i8** %4
%18 = call i64 @strchr(i8* %17, i8 signext 97)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16, %12
%21 = load i32, i32* @O_WRONLY
store i32 %21, i32* %6
br label %24
22: ; preds = %16
%23 = load i32, i32* @O_RDONLY
store i32 %23, i32* %6
br label %24
24: ; preds = %22, %20
%25 = load i8*, i8** %3
%26 = load i32, i32* %6
%27 = call i32 @gendep__register_open(i8* %25, i32 %26)
br label %28
28: ; preds = %24, %2
%29 = load i32*, i32** %5
ret i32* %29
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_RDONLY = global i32 0
define i32* @__fopen(i8* %0, i8* %1) {
%3 = tail call i32* @real_fopen(i8* %0, i8* %1)
%4 = icmp eq i32* %3, null
br i1 %4, label %16, label %5
5: ; preds = %2
%6 = tail call i64 @strchr(i8* %1, i8 signext 119)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %5
%9 = tail call i64 @strchr(i8* %1, i8 signext 97)
%10 = icmp eq i64 %9, 0
%11 = select i1 %10, i32* @O_RDONLY, i32* @O_WRONLY
br label %12
12: ; preds = %8, %5
%13 = phi i32* [ @O_WRONLY, %5 ], [ %11, %8 ]
%14 = load i32, i32* %13
%15 = tail call i32 @gendep__register_open(i8* %0, i32 %14)
br label %16
16: ; preds = %12, %2
ret i32* %3
}