#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*/ *libc_freopen64(char const *, char const *,
                                   int /*<<< orphan*/ *);
scalar_t__ strchr(char const *, char);
int /*<<< orphan*/ watch_log(char *, char const *);
FILE *freopen64(const char *path, const char *mode, FILE *stream) {
  if (strchr(mode, 'w') || strchr(mode, 'a')) {
    watch_log("open %s w\n", path);
  }
  return libc_freopen64(path, mode, stream);
}


x86 assembly (clang, -O0):
<freopen64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x77,%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
jne    1174 <freopen64+0x44>
mov    -0x10(%rbp),%rdi
mov    $0x61,%esi
call   1030 <strchr@plt>
cmp    $0x0,%rax
je     1184 <freopen64+0x54>
mov    -0x8(%rbp),%rsi
lea    0xe81(%rip),%rdi
call   1040 <watch_log@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <libc_freopen64@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<freopen64>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
mov    %rsi,%rdi
mov    $0x77,%esi
call   1030 <strchr@plt>
test   %rax,%rax
jne    1162 <freopen64+0x32>
mov    %rbx,%rdi
mov    $0x61,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     1171 <freopen64+0x41>
lea    0xe97(%rip),%rdi
mov    %r15,%rsi
call   1040 <watch_log@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <libc_freopen64@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"open %s w\0A\00"
define i32* @freopen64(i8* %0, i8* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32* %2, i32** %6
%7 = load i8*, i8** %5
%8 = call i64 @strchr(i8* %7, i8 signext 119)
%9 = icmp ne i64 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %3
%11 = load i8*, i8** %5
%12 = call i64 @strchr(i8* %11, i8 signext 97)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10, %3
%15 = load i8*, i8** %4
%16 = call i32 @watch_log(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %17
17: ; preds = %14, %10
%18 = load i8*, i8** %4
%19 = load i8*, i8** %5
%20 = load i32*, i32** %6
%21 = call i32* @libc_freopen64(i8* %18, i8* %19, i32* %20)
ret i32* %21
}

LLVM IR (-O2):
@.str = [11 x i8] c"open %s w\0A\00"
define i32* @freopen64(i8* %0, i8* %1, i32* %2) {
%4 = tail call i64 @strchr(i8* %1, i8 signext 119)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i64 @strchr(i8* %1, i8 signext 97)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %6, %3
%10 = tail call i32 @watch_log(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %11
11: ; preds = %9, %6
%12 = tail call i32* @libc_freopen64(i8* %0, i8* %1, i32* %2)
ret i32* %12
}