#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_FIXED;
int MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int PROT_EXEC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ err(int, char *, unsigned int, void *);
unsigned int getpagesize();
void *mmap(void *, unsigned int, int, int, int, int /*<<< orphan*/);
int open_or_die(char *, int /*<<< orphan*/);
void *map_zeroed_pages(unsigned long addr, unsigned int num) {
  static int fd = -1;
  if (fd == -1)
    fd = open_or_die("/dev/zero", O_RDONLY);
  if (mmap((void *)addr, getpagesize() * num,
           PROT_READ | PROT_WRITE | PROT_EXEC, MAP_FIXED | MAP_PRIVATE, fd,
           0) != (void *)addr)
    err(1, "Mmaping %u pages of /dev/zero @%p", num, (void *)addr);
  return (void *)addr;
}


x86 assembly (clang, -O0):
<map_zeroed_pages>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0xffffffff,0x2eea(%rip)
jne    1177 <map_zeroed_pages+0x37>
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
lea    0xe94(%rip),%rdi
call   1040 <open_or_die@plt>
mov    %eax,0x2ec9(%rip)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <getpagesize@plt>
mov    -0x18(%rbp),%rdi
mov    %eax,%esi
imul   -0xc(%rbp),%esi
mov    0x2e61(%rip),%rax
mov    (%rax),%edx
mov    0x2e28(%rip),%rax
or     (%rax),%edx
mov    0x2e2f(%rip),%rax
or     (%rax),%edx
mov    0x2e06(%rip),%rax
mov    (%rax),%ecx
mov    0x2e05(%rip),%rax
or     (%rax),%ecx
mov    0x2e7c(%rip),%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    -0x8(%rbp),%rcx
cmp    %rcx,%rax
je     11f1 <map_zeroed_pages+0xb1>
mov    -0xc(%rbp),%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%edi
lea    0xe1e(%rip),%rsi
call   1050 <err@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_zeroed_pages>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
cmpl   $0xffffffff,0x2ef1(%rip)
jne    116c <map_zeroed_pages+0x2c>
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
lea    0xe9f(%rip),%rdi
call   1040 <open_or_die@plt>
mov    %eax,0x2ed4(%rip)
xor    %eax,%eax
call   1060 <getpagesize@plt>
imul   %ebp,%eax
mov    0x2e7b(%rip),%rcx
mov    0x2e44(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e49(%rip),%rcx
or     (%rcx),%edx
mov    0x2e20(%rip),%rsi
mov    0x2e21(%rip),%rcx
mov    (%rcx),%ecx
or     (%rsi),%ecx
mov    0x2e96(%rip),%r8d
mov    %rbx,%rdi
mov    %eax,%esi
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    %rbx,%rax
je     11d2 <map_zeroed_pages+0x92>
lea    0xe47(%rip),%rsi
mov    $0x1,%edi
mov    %ebp,%edx
mov    %rbx,%rcx
call   1050 <err@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@map_zeroed_pages.fd = internal global i32 -1
@.str = [10 x i8] c"/dev/zero\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_FIXED = global i32 0
@MAP_PRIVATE = global i32 0
@.str.1 = [34 x i8] c"Mmaping %u pages of /dev/zero @%p\00"
define i8* @map_zeroed_pages(i64 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i32
store i64 %0, i64* %3
store i32 %1, i32* %4
%5 = load i32, i32* @map_zeroed_pages.fd
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open_or_die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* @map_zeroed_pages.fd
br label %10
10: ; preds = %7, %2
%11 = load i64, i64* %3
%12 = inttoptr i64 %11 to i8*
%13 = call i32 (...) @getpagesize()
%14 = load i32, i32* %4
%15 = mul i32 %13, %14
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @PROT_WRITE
%18 = or i32 %16, %17
%19 = load i32, i32* @PROT_EXEC
%20 = or i32 %18, %19
%21 = load i32, i32* @MAP_FIXED
%22 = load i32, i32* @MAP_PRIVATE
%23 = or i32 %21, %22
%24 = load i32, i32* @map_zeroed_pages.fd
%25 = call i8* @mmap(i8* %12, i32 %15, i32 %20, i32 %23, i32 %24, i32 0)
%26 = load i64, i64* %3
%27 = inttoptr i64 %26 to i8*
%28 = icmp ne i8* %25, %27
br i1 %28, label %29, label %34
29: ; preds = %10
%30 = load i32, i32* %4
%31 = load i64, i64* %3
%32 = inttoptr i64 %31 to i8*
%33 = call i32 @err(i32 1, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %30, i8* %32)
br label %34
34: ; preds = %29, %10
%35 = load i64, i64* %3
%36 = inttoptr i64 %35 to i8*
ret i8* %36
}

LLVM IR (-O2):
@map_zeroed_pages.fd = internal unnamed_addr global i32 -1
@.str = [10 x i8] c"/dev/zero\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@PROT_EXEC = global i32 0
@MAP_FIXED = global i32 0
@MAP_PRIVATE = global i32 0
@.str.1 = [34 x i8] c"Mmaping %u pages of /dev/zero @%p\00"
define i8* @map_zeroed_pages(i64 %0, i32 %1) {
%3 = load i32, i32* @map_zeroed_pages.fd
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open_or_die(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* @map_zeroed_pages.fd
br label %8
8: ; preds = %5, %2
%9 = inttoptr i64 %0 to i8*
%10 = tail call i32 (...) @getpagesize()
%11 = mul i32 %10, %1
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %13, %12
%15 = load i32, i32* @PROT_EXEC
%16 = or i32 %14, %15
%17 = load i32, i32* @MAP_FIXED
%18 = load i32, i32* @MAP_PRIVATE
%19 = or i32 %18, %17
%20 = load i32, i32* @map_zeroed_pages.fd
%21 = tail call i8* @mmap(i8* %9, i32 %11, i32 %16, i32 %19, i32 %20, i32 0)
%22 = icmp eq i8* %21, %9
br i1 %22, label %25, label %23
23: ; preds = %8
%24 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %1, i8* %9)
br label %25
25: ; preds = %23, %8
ret i8* %9
}