#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 MAP_ANONYMOUS;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
void **mmap(int /*<<< orphan*/ *, size_t, int, int, int, int /*<<< orphan*/);
void *malloc(size_t sz) {
  sz += 4;
  if ((sz & 0xFFF) != 0)
    sz = (sz | 0xFFF) + 1;
  void **r = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
                  -1, 0);
  *r = (void *)sz;
  return (void *)(r + 1);
}


x86 assembly (clang, -O0):
<malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
and    $0xfff,%rax
cmp    $0x0,%rax
je     114e <malloc+0x3e>
mov    -0x8(%rbp),%rax
or     $0xfff,%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
mov    0x2e9f(%rip),%rax
mov    (%rax),%edx
mov    0x2e6e(%rip),%rax
or     (%rax),%edx
mov    0x2e5d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e6c(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<malloc>:
push   %rbx
add    $0x4,%rdi
mov    %rdi,%rbx
or     $0xfff,%rbx
add    $0x1,%rbx
test   $0xfff,%edi
cmove  %rdi,%rbx
mov    0x2ec4(%rip),%rax
mov    0x2e95(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e82(%rip),%rax
mov    0x2e93(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rbx,(%rax)
add    $0x8,%rax
pop    %rbx
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
define i8* @malloc(i64 %0) {
%2 = alloca i64
%3 = alloca i8**
store i64 %0, i64* %2
%4 = load i64, i64* %2
%5 = add i64 %4, 4
store i64 %5, i64* %2
%6 = load i64, i64* %2
%7 = and i64 %6, 4095
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i64, i64* %2
%11 = or i64 %10, 4095
%12 = add i64 %11, 1
store i64 %12, i64* %2
br label %13
13: ; preds = %9, %1
%14 = load i64, i64* %2
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %15, %16
%18 = load i32, i32* @MAP_PRIVATE
%19 = load i32, i32* @MAP_ANONYMOUS
%20 = or i32 %18, %19
%21 = call i8** @mmap(i32* null, i64 %14, i32 %17, i32 %20, i32 -1, i32 0)
store i8** %21, i8*** %3
%22 = load i64, i64* %2
%23 = inttoptr i64 %22 to i8*
%24 = load i8**, i8*** %3
store i8* %23, i8** %24
%25 = load i8**, i8*** %3
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = bitcast i8** %26 to i8*
ret i8* %27
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
define i8* @malloc(i64 %0) {
%2 = add i64 %0, 4
%3 = and i64 %2, 4095
%4 = icmp eq i64 %3, 0
%5 = or i64 %2, 4095
%6 = add i64 %5, 1
%7 = select i1 %4, i64 %2, i64 %6
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %9, %8
%11 = load i32, i32* @MAP_PRIVATE
%12 = load i32, i32* @MAP_ANONYMOUS
%13 = or i32 %12, %11
%14 = tail call i8** @mmap(i32* null, i64 %7, i32 %10, i32 %13, i32 -1, i32 0)
%15 = inttoptr i64 %7 to i8*
store i8* %15, i8** %14
%16 = getelementptr inbounds i8*, i8** %14, i64 1
%17 = bitcast i8** %16 to i8*
ret i8* %17
}