#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 backtrace(void **, int);
int /*<<< orphan*/ backtrace_symbols_fd(void **, int, int);
int /*<<< orphan*/ exit(int);
void *malloc(size_t);
int /*<<< orphan*/ printf(char *);
void *mlm_malloc(size_t size, int level) {
  if (size == 0) {
    printf("ZERO BYTE MALLOC\n");
    void *bt_entries[64];
    int entries = backtrace(bt_entries, 64);
    backtrace_symbols_fd(bt_entries, entries, 1);
    exit(-1);
  }
  return malloc(size);
}


x86 assembly (clang, -O0):
<mlm_malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11b1 <mlm_malloc+0x61>
lea    0xe8c(%rip),%rdi
call   1050 <printf@plt>
lea    -0x210(%rbp),%rdi
mov    $0x40,%esi
call   1040 <backtrace@plt>
mov    %eax,-0x214(%rbp)
lea    -0x210(%rbp),%rdi
mov    -0x214(%rbp),%esi
mov    $0x1,%edx
call   1030 <backtrace_symbols_fd@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1060 <malloc@plt>
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mlm_malloc>:
test   %rdi,%rdi
je     115a <mlm_malloc+0xa>
jmp    1060 <malloc@plt>
push   %rbx
sub    $0x200,%rsp
lea    0xe97(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   1050 <backtrace@plt>
mov    %rbx,%rdi
mov    %eax,%esi
mov    $0x1,%edx
call   1030 <backtrace_symbols_fd@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"ZERO BYTE MALLOC\0A\00"
define i8* @mlm_malloc(i64 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca [64 x i8*]
%6 = alloca i32
store i64 %0, i64* %3
store i32 %1, i32* %4
%7 = load i64, i64* %3
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %2
%10 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%11 = getelementptr inbounds [64 x i8*], [64 x i8*]* %5, i64 0, i64 0
%12 = call i32 @backtrace(i8** %11, i32 64)
store i32 %12, i32* %6
%13 = getelementptr inbounds [64 x i8*], [64 x i8*]* %5, i64 0, i64 0
%14 = load i32, i32* %6
%15 = call i32 @backtrace_symbols_fd(i8** %13, i32 %14, i32 1)
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = load i64, i64* %3
%19 = call i8* @malloc(i64 %18)
ret i8* %19
}

LLVM IR (-O2):
@str = [17 x i8] c"ZERO BYTE MALLOC\00"
define noalias i8* @mlm_malloc(i64 %0, i32 %1) {
%3 = alloca [64 x i8*]
%4 = icmp eq i64 %0, 0
br i1 %4, label %5, label %12
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%7 = bitcast [64 x i8*]* %3 to i8*
%8 = getelementptr inbounds [64 x i8*], [64 x i8*]* %3, i64 0, i64 0
%9 = call i32 @backtrace(i8** %8, i32 64)
%10 = call i32 @backtrace_symbols_fd(i8** %8, i32 %9, i32 1)
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %2
%13 = tail call i8* @malloc(i64 %0)
ret i8* %13
}