#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*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, unsigned long, int,
                           size_t);
int getpid();
void *orig_memset(void *, int, size_t);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ *stderr;
void *memset(void *s, int c, size_t n) {
  FILE *fp;
  char filename[1024 + 1];
  snprintf(filename, sizeof(filename), "/tmp/mal-%d.log", getpid());
  fp = fopen(filename, "a");
  if ((fp)) {
    fprintf(fp, "memset(0x%08lx, %i, %u)\n", (unsigned long)s, c, n);
    fclose(fp);
  }
  fprintf(stderr, "memset(0x%08lx, %i, %u)\n", (unsigned long)s, c, n);
  return orig_memset(s, c, n);
}


x86 assembly (clang, -O0):
<memset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x438(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x438(%rbp),%rdi
mov    %eax,%ecx
mov    $0x401,%esi
lea    0xe60(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x430(%rbp),%rdi
lea    0xe5d(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11eb <memset+0x8b>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%r8
lea    0xe35(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%rdi
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%r8
lea    0xe0b(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1050 <orig_memset@plt>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memset>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe7c(%rip),%rdx
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x401,%esi
mov    %eax,%ecx
call   1060 <snprintf@plt>
lea    0xe73(%rip),%rsi
mov    %rbp,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     11cd <memset+0x6d>
mov    %rax,%rbp
lea    0xe5e(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
mov    %r15d,%ecx
mov    %r14,%r8
call   1070 <fprintf@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    0x2e0c(%rip),%rax
mov    (%rax),%rdi
lea    0xe34(%rip),%rsi
mov    %rbx,%rdx
mov    %r15d,%ecx
mov    %r14,%r8
call   1070 <fprintf@plt>
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14,%rdx
call   1050 <orig_memset@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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