#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_PRIVATE;
int PROT_READ;
int PROT_WRITE;
void *_mmap(int /*<<< orphan*/, size_t, int, int, int, int /*<<< orphan*/);
void *malloc(size_t size) {
  size += sizeof(size_t);
  void *p = _mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | 0x20, -1, 0);
  if (p == 0)
    return 0;
  *(size_t *)p = size;
  return p + sizeof(size_t);
}


x86 assembly (clang, -O0):
<malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
mov    0x2ec5(%rip),%rax
mov    (%rax),%edx
mov    0x2e9c(%rip),%rax
or     (%rax),%edx
mov    0x2e8b(%rip),%rax
mov    (%rax),%ecx
or     $0x20,%ecx
xor    %r9d,%r9d
mov    $0xffffffff,%r8d
mov    %r9d,%edi
call   1030 <_mmap@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1177 <malloc+0x67>
movq   $0x0,-0x8(%rbp)
jmp    118e <malloc+0x7e>
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<malloc>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    0x2eda(%rip),%rax
mov    0x2eb3(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
add    $0x8,%rbx
mov    0x2e9c(%rip),%rax
mov    (%rax),%ecx
or     $0x20,%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <_mmap@plt>
test   %rax,%rax
je     115e <malloc+0x4e>
mov    %rbx,(%rax)
add    $0x8,%rax
mov    %rax,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
define i8* @malloc(i64 %0) {
%2 = add i64 %0, 8
%3 = load i32, i32* @PROT_READ
%4 = load i32, i32* @PROT_WRITE
%5 = or i32 %4, %3
%6 = load i32, i32* @MAP_PRIVATE
%7 = or i32 %6, 32
%8 = tail call i8* @_mmap(i32 0, i64 %2, i32 %5, i32 %7, i32 -1, i32 0)
%9 = icmp eq i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %1
%11 = bitcast i8* %8 to i64*
store i64 %2, i64* %11
%12 = getelementptr i8, i8* %8, i64 8
br label %13
13: ; preds = %1, %10
%14 = phi i8* [ %12, %10 ], [ null, %1 ]
ret i8* %14
}