#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 MADV_SEQUENTIAL;
int MADV_WILLNEED;
int MAP_ANON;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ madvise(void *, size_t, int);
scalar_t__ mlock(void *, size_t);
void *mmap(int /*<<< orphan*/ *, size_t, int, int, int /*<<< orphan*/,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(void *, size_t);
void *mmalloc(size_t size) {
  void *addr =
      mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, 0);
  if (addr) {
    madvise(addr, size, MADV_SEQUENTIAL | MADV_WILLNEED);
    if (mlock(addr, size) != 0) {
      munmap(addr, size);
      addr = NULL;
    }
  }
  return addr;
}


x86 assembly (clang, -O0):
<mmalloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
mov    0x2ea1(%rip),%rax
mov    (%rax),%edx
mov    0x2e78(%rip),%rax
or     (%rax),%edx
mov    0x2e67(%rip),%rax
mov    (%rax),%ecx
mov    0x2e4e(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
mov    %r9d,%r8d
call   1040 <mmap@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11e2 <mmalloc+0xa2>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
mov    0x2e1e(%rip),%rax
or     (%rax),%edx
call   1060 <madvise@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <mlock@plt>
cmp    $0x0,%rax
je     11dd <mmalloc+0x9d>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <munmap@plt>
movq   $0x0,-0x10(%rbp)
jmp    11e2 <mmalloc+0xa2>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmalloc>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2ea9(%rip),%rax
mov    0x2e82(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e6f(%rip),%rax
mov    0x2e58(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %r15d,%r15d
xor    %edi,%edi
mov    %r14,%rsi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
test   %rax,%rax
je     11c7 <mmalloc+0x87>
mov    %rax,%rbx
mov    0x2e22(%rip),%rax
mov    0x2e33(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <madvise@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <mlock@plt>
test   %rax,%rax
je     11c4 <mmalloc+0x84>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <munmap@plt>
xor    %r15d,%r15d
jmp    11c7 <mmalloc+0x87>
mov    %rbx,%r15
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
@MADV_SEQUENTIAL = global i32 0
@MADV_WILLNEED = global i32 0
define i8* @mmalloc(i64 %0) {
%2 = alloca i64
%3 = alloca i8*
store i64 %0, i64* %2
%4 = load i64, i64* %2
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @PROT_WRITE
%7 = or i32 %5, %6
%8 = load i32, i32* @MAP_ANON
%9 = load i32, i32* @MAP_PRIVATE
%10 = or i32 %8, %9
%11 = call i8* @mmap(i32* null, i64 %4, i32 %7, i32 %10, i32 0, i32 0)
store i8* %11, i8** %3
%12 = load i8*, i8** %3
%13 = icmp ne i8* %12, null
br i1 %13, label %14, label %30
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = load i64, i64* %2
%17 = load i32, i32* @MADV_SEQUENTIAL
%18 = load i32, i32* @MADV_WILLNEED
%19 = or i32 %17, %18
%20 = call i32 @madvise(i8* %15, i64 %16, i32 %19)
%21 = load i8*, i8** %3
%22 = load i64, i64* %2
%23 = call i64 @mlock(i8* %21, i64 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %14
%26 = load i8*, i8** %3
%27 = load i64, i64* %2
%28 = call i32 @munmap(i8* %26, i64 %27)
store i8* null, i8** %3
br label %29
29: ; preds = %25, %14
br label %30
30: ; preds = %29, %1
%31 = load i8*, i8** %3
ret i8* %31
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANON = global i32 0
@MAP_PRIVATE = global i32 0
@MADV_SEQUENTIAL = global i32 0
@MADV_WILLNEED = global i32 0
define i8* @mmalloc(i64 %0) {
%2 = load i32, i32* @PROT_READ
%3 = load i32, i32* @PROT_WRITE
%4 = or i32 %3, %2
%5 = load i32, i32* @MAP_ANON
%6 = load i32, i32* @MAP_PRIVATE
%7 = or i32 %6, %5
%8 = tail call i8* @mmap(i32* null, i64 %0, i32 %4, i32 %7, i32 0, i32 0)
%9 = icmp eq i8* %8, null
br i1 %9, label %19, label %10
10: ; preds = %1
%11 = load i32, i32* @MADV_SEQUENTIAL
%12 = load i32, i32* @MADV_WILLNEED
%13 = or i32 %12, %11
%14 = tail call i32 @madvise(i8* %8, i64 %0, i32 %13)
%15 = tail call i64 @mlock(i8* %8, i64 %0)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %10
%18 = tail call i32 @munmap(i8* %8, i64 %0)
br label %19
19: ; preds = %10, %17, %1
%20 = phi i8* [ null, %17 ], [ %8, %10 ], [ null, %1 ]
ret i8* %20
}