#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*/ filename;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char const *,
                           char const *);
int getpid();
int /*<<< orphan*/ *orig_fopen(char const *, char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ *stderr;
FILE *fopen(const char *path, const char *mode) {
  FILE *fp;
  char filename[1024 + 1];
  snprintf(filename, sizeof(filename), "/tmp/mal-%d.log", getpid());
  fp = orig_fopen(filename, "a");
  if ((fp)) {
    fprintf(fp, "fopen(\"%s\", \"%s\")\n", path, mode);
    fclose(fp);
  }
  fprintf(stderr, "fopen(\"%s\", \"%s\")\n", path, mode);
  return orig_fopen(path, mode);
}


x86 assembly (clang, -O0):
<fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x428(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x428(%rbp),%rdi
mov    %eax,%ecx
mov    $0x401,%esi
lea    0xe73(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x420(%rbp),%rdi
lea    0xe70(%rip),%rsi
call   1030 <orig_fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d5 <fopen+0x85>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe4b(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2e04(%rip),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe24(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <orig_fopen@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fopen>:
push   %r15
push   %r14
push   %rbx
sub    $0x410,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe90(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x401,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
lea    0xe87(%rip),%rsi
mov    %rbx,%rdi
call   1030 <orig_fopen@plt>
test   %rax,%rax
je     11b6 <fopen+0x66>
mov    %rax,%rbx
lea    0xe72(%rip),%rsi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%rdi
lea    0xe4b(%rip),%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1070 <fprintf@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <orig_fopen@plt>
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [16 x i8] c"/tmp/mal-%d.log\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [19 x i8] c"fopen(\22%s\22, \22%s\22)\0A\00"
@stderr = global i32* null
define i32* @fopen(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [1025 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = getelementptr inbounds [1025 x i8], [1025 x i8]* %6, i64 0, i64 0
%8 = call i32 (...) @getpid()
%9 = call i32 @snprintf(i8* %7, i32 1025, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [1025 x i8], [1025 x i8]* %6, i64 0, i64 0
%11 = call i32* @orig_fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %5
%12 = load i32*, i32** %5
%13 = icmp ne i32* %12, null
br i1 %13, label %14, label %21
14: ; preds = %2
%15 = load i32*, i32** %5
%16 = load i8*, i8** %3
%17 = load i8*, i8** %4
%18 = call i32 @fprintf(i32* %15, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %16, i8* %17)
%19 = load i32*, i32** %5
%20 = call i32 @fclose(i32* %19)
br label %21
21: ; preds = %14, %2
%22 = load i32*, i32** @stderr
%23 = load i8*, i8** %3
%24 = load i8*, i8** %4
%25 = call i32 @fprintf(i32* %22, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %23, i8* %24)
%26 = load i8*, i8** %3
%27 = load i8*, i8** %4
%28 = call i32* @orig_fopen(i8* %26, i8* %27)
ret i32* %28
}

LLVM IR (-O2):
@.str = [16 x i8] c"/tmp/mal-%d.log\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [19 x i8] c"fopen(\22%s\22, \22%s\22)\0A\00"
@stderr = global i32* null
define i32* @fopen(i8* %0, i8* %1) {
%3 = alloca [1025 x i8]
%4 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 0
%5 = tail call i32 (...) @getpid()
%6 = call i32 @snprintf(i8* %4, i32 1025, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = call i32* @orig_fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = call i32 @fprintf(i32* %7, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %1)
%11 = call i32 @fclose(i32* %7)
br label %12
12: ; preds = %9, %2
%13 = load i32*, i32** @stderr
%14 = call i32 @fprintf(i32* %13, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %1)
%15 = call i32* @orig_fopen(i8* %0, i8* %1)
ret i32* %15
}