#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 scalar_t__ caddr_t;

/* Variables and functions */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
scalar_t__ mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
                int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
void *mapmalloc(int size) {
  int fd, i;
  caddr_t base;
  char dummy = 0;
  fd = open("/dev/zero", O_RDWR);
  base = mmap(NULL, (size_t)(size), PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  close(fd);
  return (void *)base;
}


x86 assembly (clang, -O0):
<mapmalloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movb   $0x0,-0x19(%rbp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
lea    0xeb1(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
movslq -0x4(%rbp),%rsi
mov    0x2e96(%rip),%rax
mov    (%rax),%edx
mov    0x2e65(%rip),%rax
or     (%rax),%edx
mov    0x2e54(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mapmalloc>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
lea    0xebb(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebp
movslq %ebx,%rsi
mov    0x2ea2(%rip),%rax
mov    0x2e73(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e60(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/zero\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
define i8* @mapmalloc(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8
store i32 %0, i32* %2
store i8 0, i8* %6
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %3
%9 = load i32, i32* %2
%10 = sext i32 %9 to i64
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @PROT_WRITE
%13 = or i32 %11, %12
%14 = load i32, i32* @MAP_PRIVATE
%15 = load i32, i32* %3
%16 = call i64 @mmap(i32* null, i64 %10, i32 %13, i32 %14, i32 %15, i32 0)
store i64 %16, i64* %5
%17 = load i32, i32* %3
%18 = call i32 @close(i32 %17)
%19 = load i64, i64* %5
%20 = inttoptr i64 %19 to i8*
ret i8* %20
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/zero\00"
@O_RDWR = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
define i8* @mapmalloc(i32 %0) {
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = sext i32 %0 to i64
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @PROT_WRITE
%7 = or i32 %6, %5
%8 = load i32, i32* @MAP_PRIVATE
%9 = tail call i64 @mmap(i32* null, i64 %4, i32 %7, i32 %8, i32 %3, i32 0)
%10 = tail call i32 @close(i32 %3)
%11 = inttoptr i64 %9 to i8*
ret i8* %11
}