#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_NONE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *, unsigned int);
int getpagesize();
void *mmap(int /*<<< orphan*/ *, unsigned int, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int mprotect(void *, unsigned int, int);
int open_or_die(char *, int /*<<< orphan*/);
void *map_zeroed_pages(unsigned int num) {
  int fd = open_or_die("/dev/zero", O_RDONLY);
  void *addr;
  addr = mmap(NULL, getpagesize() * (num + 2), PROT_NONE, MAP_PRIVATE, fd, 0);
  if (addr == MAP_FAILED)
    err(1, "Mmapping %u pages of /dev/zero", num);
  if (mprotect(addr + getpagesize(), getpagesize() * num,
               PROT_READ | PROT_WRITE) == -1)
    err(1, "mprotect rw %u pages failed", num);
  close(fd);
  return addr + getpagesize();
}


x86 assembly (clang, -O0):
<map_zeroed_pages>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
lea    0xe9a(%rip),%rdi
call   1050 <open_or_die@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1080 <getpagesize@plt>
mov    %eax,%esi
mov    -0x4(%rbp),%eax
add    $0x2,%eax
imul   %eax,%esi
mov    0x2e4c(%rip),%rax
mov    (%rax),%edx
mov    0x2e1b(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2df2(%rip),%rcx
cmp    (%rcx),%rax
jne    11e3 <map_zeroed_pages+0x83>
mov    -0x4(%rbp),%edx
mov    $0x1,%edi
lea    0xe22(%rip),%rsi
call   1060 <err@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1080 <getpagesize@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1080 <getpagesize@plt>
mov    -0x18(%rbp),%rdi
mov    %eax,%esi
imul   -0x4(%rbp),%esi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
mov    0x2da5(%rip),%rax
or     (%rax),%edx
call   1070 <mprotect@plt>
cmp    $0xffffffff,%eax
jne    1247 <map_zeroed_pages+0xe7>
mov    -0x4(%rbp),%edx
mov    $0x1,%edi
lea    0xddd(%rip),%rsi
call   1060 <err@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1080 <getpagesize@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_zeroed_pages>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
lea    0xe9b(%rip),%rdi
call   1050 <open_or_die@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1080 <getpagesize@plt>
lea    0x2(%r15),%esi
imul   %eax,%esi
mov    0x2e51(%rip),%rax
mov    (%rax),%edx
mov    0x2e20(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    0x2dff(%rip),%rax
cmp    (%rax),%rbp
jne    11d2 <map_zeroed_pages+0x72>
lea    0xe3b(%rip),%rsi
mov    $0x1,%edi
mov    %r15d,%edx
call   1060 <err@plt>
xor    %eax,%eax
call   1080 <getpagesize@plt>
movslq %eax,%rbx
add    %rbp,%rbx
xor    %eax,%eax
call   1080 <getpagesize@plt>
imul   %r15d,%eax
mov    0x2e07(%rip),%rcx
mov    0x2dd0(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    %rbx,%rdi
mov    %eax,%esi
call   1070 <mprotect@plt>
cmp    $0xffffffff,%eax
jne    121f <map_zeroed_pages+0xbf>
lea    0xe0d(%rip),%rsi
mov    $0x1,%edi
mov    %r15d,%edx
call   1060 <err@plt>
mov    %r14d,%edi
call   1040 <close@plt>
xor    %eax,%eax
call   1080 <getpagesize@plt>
cltq
add    %rax,%rbp
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/zero\00"
@O_RDONLY = global i32 0
@PROT_NONE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [31 x i8] c"Mmapping %u pages of /dev/zero\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@.str.2 = [28 x i8] c"mprotect rw %u pages failed\00"
define i8* @map_zeroed_pages(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
store i32 %0, i32* %2
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open_or_die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %3
%7 = call i32 (...) @getpagesize()
%8 = load i32, i32* %2
%9 = add i32 %8, 2
%10 = mul i32 %7, %9
%11 = load i32, i32* @PROT_NONE
%12 = load i32, i32* @MAP_PRIVATE
%13 = load i32, i32* %3
%14 = call i8* @mmap(i32* null, i32 %10, i32 %11, i32 %12, i32 %13, i32 0)
store i8* %14, i8** %4
%15 = load i8*, i8** %4
%16 = load i8*, i8** @MAP_FAILED
%17 = icmp eq i8* %15, %16
br i1 %17, label %18, label %21
18: ; preds = %1
%19 = load i32, i32* %2
%20 = call i32 @err(i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %18, %1
%22 = load i8*, i8** %4
%23 = call i32 (...) @getpagesize()
%24 = sext i32 %23 to i64
%25 = getelementptr i8, i8* %22, i64 %24
%26 = call i32 (...) @getpagesize()
%27 = load i32, i32* %2
%28 = mul i32 %26, %27
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @PROT_WRITE
%31 = or i32 %29, %30
%32 = call i32 @mprotect(i8* %25, i32 %28, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %21
%35 = load i32, i32* %2
%36 = call i32 @err(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %34, %21
%38 = load i32, i32* %3
%39 = call i32 @close(i32 %38)
%40 = load i8*, i8** %4
%41 = call i32 (...) @getpagesize()
%42 = sext i32 %41 to i64
%43 = getelementptr i8, i8* %40, i64 %42
ret i8* %43
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/zero\00"
@O_RDONLY = global i32 0
@PROT_NONE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [31 x i8] c"Mmapping %u pages of /dev/zero\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@.str.2 = [28 x i8] c"mprotect rw %u pages failed\00"
define i8* @map_zeroed_pages(i32 %0) {
%2 = load i32, i32* @O_RDONLY
%3 = tail call i32 @open_or_die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = tail call i32 (...) @getpagesize()
%5 = add i32 %0, 2
%6 = mul i32 %4, %5
%7 = load i32, i32* @PROT_NONE
%8 = load i32, i32* @MAP_PRIVATE
%9 = tail call i8* @mmap(i32* null, i32 %6, i32 %7, i32 %8, i32 %3, i32 0)
%10 = load i8*, i8** @MAP_FAILED
%11 = icmp eq i8* %9, %10
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %14
14: ; preds = %12, %1
%15 = tail call i32 (...) @getpagesize()
%16 = sext i32 %15 to i64
%17 = getelementptr i8, i8* %9, i64 %16
%18 = tail call i32 (...) @getpagesize()
%19 = mul i32 %18, %0
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @PROT_WRITE
%22 = or i32 %21, %20
%23 = tail call i32 @mprotect(i8* %17, i32 %19, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %14
%26 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %0)
br label %27
27: ; preds = %25, %14
%28 = tail call i32 @close(i32 %3)
%29 = tail call i32 (...) @getpagesize()
%30 = sext i32 %29 to i64
%31 = getelementptr i8, i8* %9, i64 %30
ret i8* %31
}