#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 */
typedef int /*<<< orphan*/ _buf;

/* Variables and functions */
int MAP_ANONYMOUS;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ abort();
size_t errno;
void *mmap(int /*<<< orphan*/, size_t, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ *sys_errlist;
int /*<<< orphan*/ write_string(char *);
void *Malloc(size_t n) {
  n = (n + (4 * 1024) - 1) & ~(uintptr_t)((4 * 1024) - 1);
  void *ret =
      mmap(0, n, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  if (!ret || ret == (void *)-1) {
    do {
      char _buf[128];
      snprintf(_buf, sizeof(_buf),
               "libpregrind.so: "
               "Malloc() failed to allocate %zd bytes: %s\n",
               n, sys_errlist[errno]);
      write_string(_buf);
    } while (0);
    abort();
  }
  return ret;
}


x86 assembly (clang, -O0):
<Malloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1000,%rax
sub    $0x1,%rax
and    $0xfffffffffffff000,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
mov    0x2e86(%rip),%rax
mov    (%rax),%edx
mov    0x2e45(%rip),%rax
or     (%rax),%edx
mov    0x2e34(%rip),%rax
mov    (%rax),%ecx
mov    0x2e4b(%rip),%rax
or     (%rax),%ecx
xor    %r9d,%r9d
mov    $0xffffffff,%r8d
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11c0 <Malloc+0x80>
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x10(%rbp)
jne    120c <Malloc+0xcc>
jmp    11c5 <Malloc+0x85>
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    0x2e19(%rip),%rax
mov    (%rax),%rax
mov    0x2de7(%rip),%rdx
mov    (%rdx),%rdx
mov    (%rax,%rdx,4),%r8d
mov    $0x80,%esi
lea    0xe0c(%rip),%rdx
call   1060 <snprintf@plt>
lea    -0x90(%rbp),%rdi
call   1050 <write_string@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x10(%rbp),%rax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Malloc>:
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rdi,%rbx
add    $0xfff,%rbx
and    $0xfffffffffffff000,%rbx
mov    0x2e96(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e44(%rip),%rax
mov    0x2e5d(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %rbx,%rsi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
lea    0x1(%rax),%rcx
cmp    $0x1,%rcx
jbe    11a7 <Malloc+0x67>
add    $0x88,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e42(%rip),%rax
mov    (%rax),%rax
mov    0x2e10(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,4),%r8d
lea    0xe3a(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x80,%esi
mov    %rbx,%rcx
call   1060 <snprintf@plt>
mov    %r14,%rdi
call   1050 <write_string@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str = [59 x i8] c"libpregrind.so: Malloc() failed to allocate %zd bytes: %s\0A\00"
@sys_errlist = global i32* null
@errno = global i64 0
define i8* @Malloc(i64 %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca [128 x i8]
store i64 %0, i64* %2
%5 = load i64, i64* %2
%6 = add i64 %5, 4096
%7 = sub i64 %6, 1
%8 = and i64 %7, -4096
store i64 %8, i64* %2
%9 = load i64, i64* %2
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %10, %11
%13 = load i32, i32* @MAP_PRIVATE
%14 = load i32, i32* @MAP_ANONYMOUS
%15 = or i32 %13, %14
%16 = call i8* @mmap(i32 0, i64 %9, i32 %12, i32 %15, i32 -1, i32 0)
store i8* %16, i8** %3
%17 = load i8*, i8** %3
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %1
%20 = load i8*, i8** %3
%21 = icmp eq i8* %20, inttoptr (i64 -1 to i8*)
br i1 %21, label %22, label %35
22: ; preds = %19, %1
br label %23
23: ; preds = %22
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%25 = load i64, i64* %2
%26 = load i32*, i32** @sys_errlist
%27 = load i64, i64* @errno
%28 = getelementptr inbounds i32, i32* %26, i64 %27
%29 = load i32, i32* %28
%30 = call i32 @snprintf(i8* %24, i32 128, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i64 %25, i32 %29)
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%32 = call i32 @write_string(i8* %31)
br label %33
33: ; preds = %23
%34 = call i32 (...) @abort()
unreachable
35: ; preds = %19
%36 = load i8*, i8** %3
ret i8* %36
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str = [59 x i8] c"libpregrind.so: Malloc() failed to allocate %zd bytes: %s\0A\00"
@sys_errlist = global i32* null
@errno = global i64 0
define i8* @Malloc(i64 %0) {
%2 = alloca [128 x i8]
%3 = add i64 %0, 4095
%4 = and i64 %3, -4096
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @PROT_WRITE
%7 = or i32 %6, %5
%8 = load i32, i32* @MAP_PRIVATE
%9 = load i32, i32* @MAP_ANONYMOUS
%10 = or i32 %9, %8
%11 = tail call i8* @mmap(i32 0, i64 %4, i32 %7, i32 %10, i32 -1, i32 0)
%12 = ptrtoint i8* %11 to i64
switch i64 %12, label %22 [
i64 -1, label %13
i64 0, label %13
]
13: ; preds = %1, %1
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%15 = load i32*, i32** @sys_errlist
%16 = load i64, i64* @errno
%17 = getelementptr inbounds i32, i32* %15, i64 %16
%18 = load i32, i32* %17
%19 = call i32 @snprintf(i8* %14, i32 128, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str, i64 0, i64 0), i64 %4, i32 %18)
%20 = call i32 @write_string(i8* %14)
%21 = call i32 (...) @abort()
unreachable
22: ; preds = %1
ret i8* %11
}