#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 */

/* Variables and functions */
int /*<<< orphan*/ dump_heap(void *);
void *malloc(int);
int /*<<< orphan*/ printf(char *);
void *malloc_wrapper(int size) {
  void *ptr = NULL;
  if (size <= 0) {
    printf("[-] malloc size == 0\n");
    return ptr;
  }
  ptr = malloc(size);
  if (!ptr) {
    printf("[-] malloc error\n");
    return ptr;
  }
  dump_heap(ptr);
  return ptr;
}


x86 assembly (clang, -O0):
<malloc_wrapper>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     1166 <malloc_wrapper+0x36>
lea    0xeac(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a7 <malloc_wrapper+0x77>
mov    -0xc(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1196 <malloc_wrapper+0x66>
lea    0xe92(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11a7 <malloc_wrapper+0x77>
mov    -0x18(%rbp),%rdi
call   1030 <dump_heap@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<malloc_wrapper>:
push   %rbx
test   %edi,%edi
jle    114c <malloc_wrapper+0x1c>
call   1050 <malloc@plt>
test   %rax,%rax
je     1155 <malloc_wrapper+0x25>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <dump_heap@plt>
jmp    1163 <malloc_wrapper+0x33>
lea    0xebe(%rip),%rdi
jmp    115c <malloc_wrapper+0x2c>
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [22 x i8] c"[-] malloc size == 0\0A\00"
@.str.1 = [18 x i8] c"[-] malloc error\0A\00"
define i8* @malloc_wrapper(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %3
store i8* null, i8** %4
%5 = load i32, i32* %3
%6 = icmp sle i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%9 = load i8*, i8** %4
store i8* %9, i8** %2
br label %22
10: ; preds = %1
%11 = load i32, i32* %3
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %4
%13 = load i8*, i8** %4
%14 = icmp ne i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%17 = load i8*, i8** %4
store i8* %17, i8** %2
br label %22
18: ; preds = %10
%19 = load i8*, i8** %4
%20 = call i32 @dump_heap(i8* %19)
%21 = load i8*, i8** %4
store i8* %21, i8** %2
br label %22
22: ; preds = %18, %15, %7
%23 = load i8*, i8** %2
ret i8* %23
}

LLVM IR (-O2):
@str = [17 x i8] c"[-] malloc error\00"
@str.2 = [21 x i8] c"[-] malloc size == 0\00"
define i8* @malloc_wrapper(i32 %0) {
%2 = icmp slt i32 %0, 1
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.2, i64 0, i64 0))
br label %12
5: ; preds = %1
%6 = tail call i8* @malloc(i32 %0)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %12
10: ; preds = %5
%11 = tail call i32 @dump_heap(i8* %6)
br label %12
12: ; preds = %10, %8, %3
%13 = phi i8* [ null, %3 ], [ %6, %10 ], [ null, %8 ]
ret i8* %13
}