#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fmemopen(char *, int, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ mymemdump(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ orgmemdump(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ perror(char *);
char *memdump_str(int useMine, char *p, int len) {
  int maxBufferSize = 10 * 1024;
  char *buffer = (char *)malloc(maxBufferSize);
  if (buffer == NULL) {
    perror("malloc:");
    exit(1);
  }
  FILE *fd = fmemopen(buffer, maxBufferSize, "w");
  if (fd == NULL) {
    perror("fmemopen:");
    exit(1);
  }
  if (useMine) {
    mymemdump(fd, p, len);
  } else {
    orgmemdump(fd, p, len);
  }
  fclose(fd);
  return buffer;
}


x86 assembly (clang, -O0):
<memdump_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x2800,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b6 <memdump_str+0x46>
lea    0xe59(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
lea    0xe44(%rip),%rdx
call   1030 <fmemopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ee <memdump_str+0x7e>
lea    0xe2b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
je     120d <memdump_str+0x9d>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <mymemdump@plt>
jmp    121d <memdump_str+0xad>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <orgmemdump@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memdump_str>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%r12d
mov    $0x2800,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11dd <memdump_str+0x6d>
mov    %rax,%rbx
lea    0xe6e(%rip),%rdx
mov    %rax,%rdi
mov    $0x2800,%esi
call   1030 <fmemopen@plt>
test   %rax,%rax
je     11e6 <memdump_str+0x76>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
test   %r12d,%r12d
je     11c4 <memdump_str+0x54>
call   1070 <mymemdump@plt>
jmp    11c9 <memdump_str+0x59>
call   1050 <orgmemdump@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe1c(%rip),%rdi
jmp    11ed <memdump_str+0x7d>
lea    0xe1d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"malloc:\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [10 x i8] c"fmemopen:\00"
define i8* @memdump_str(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 10240, i32* %7
%10 = load i32, i32* %7
%11 = call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %8
%13 = load i8*, i8** %8
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %3
%19 = load i8*, i8** %8
%20 = load i32, i32* %7
%21 = call i32* @fmemopen(i8* %19, i32 %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %21, i32** %9
%22 = load i32*, i32** %9
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %18
%28 = load i32, i32* %4
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i32*, i32** %9
%32 = load i8*, i8** %5
%33 = load i32, i32* %6
%34 = call i32 @mymemdump(i32* %31, i8* %32, i32 %33)
br label %40
35: ; preds = %27
%36 = load i32*, i32** %9
%37 = load i8*, i8** %5
%38 = load i32, i32* %6
%39 = call i32 @orgmemdump(i32* %36, i8* %37, i32 %38)
br label %40
40: ; preds = %35, %30
%41 = load i32*, i32** %9
%42 = call i32 @fclose(i32* %41)
%43 = load i8*, i8** %8
ret i8* %43
}

LLVM IR (-O2):
@.str = [8 x i8] c"malloc:\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [10 x i8] c"fmemopen:\00"
define i8* @memdump_str(i32 %0, i8* %1, i32 %2) {
%4 = tail call i64 @malloc(i32 10240)
%5 = inttoptr i64 %4 to i8*
%6 = icmp eq i64 %4, 0
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %3
%11 = tail call i32* @fmemopen(i8* %5, i32 10240, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = icmp eq i32 %0, 0
br i1 %17, label %20, label %18
18: ; preds = %16
%19 = tail call i32 @mymemdump(i32* %11, i8* %1, i32 %2)
br label %22
20: ; preds = %16
%21 = tail call i32 @orgmemdump(i32* %11, i8* %1, i32 %2)
br label %22
22: ; preds = %20, %18
%23 = tail call i32 @fclose(i32* %11)
ret i8* %5
}