#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 O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ PROT_NONE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err(int, char *, unsigned int);
int /*<<< orphan*/ ftruncate(int, int);
int getpagesize();
void *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int mprotect(void *, unsigned int, int);
int open_memfile(int);
void *map_zeroed_pages(unsigned int num, bool clean) {
  static int length = 0;
  int newLength = 0;
  int fd;
  void *addr;
  int header_pages = 2;
  newLength = getpagesize() * (num + header_pages + 1);
  if (!length && clean) {
    fd = open_memfile(O_CREAT | O_RDWR | O_TRUNC);
    ftruncate(fd, newLength);
  } else {
    fd = open_memfile(O_RDWR);
    if (newLength > length) {
      ftruncate(fd, newLength);
    }
  }
  length = newLength;
  addr = mmap(NULL, length, PROT_NONE, MAP_PRIVATE, fd, 0);
  if (addr == MAP_FAILED)
    err(1, "Mmapping %u pages", num);
  if (mprotect(addr + (getpagesize() * header_pages), getpagesize() * num,
               PROT_READ | PROT_WRITE) == -1)
    err(1, "mprotect rw %u pages failed", num);
  close(fd);
  return addr + (getpagesize() * header_pages);
}


x86 assembly (clang, -O0):
<map_zeroed_pages>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x2,-0x1c(%rbp)
mov    $0x0,%al
call   1090 <getpagesize@plt>
mov    -0x4(%rbp),%ecx
add    -0x1c(%rbp),%ecx
add    $0x1,%ecx
imul   %ecx,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,0x2eb7(%rip)
jne    11ec <map_zeroed_pages+0x7c>
cmpl   $0x0,-0x8(%rbp)
je     11ec <map_zeroed_pages+0x7c>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    0x2dff(%rip),%rax
or     (%rax),%edi
mov    0x2e1e(%rip),%rax
or     (%rax),%edi
call   1030 <open_memfile@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1050 <ftruncate@plt>
jmp    121c <map_zeroed_pages+0xac>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1030 <open_memfile@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
cmp    0x2e5a(%rip),%eax
jle    1217 <map_zeroed_pages+0xa7>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1050 <ftruncate@plt>
jmp    121c <map_zeroed_pages+0xac>
mov    -0xc(%rbp),%eax
mov    %eax,0x2e3b(%rip)
mov    0x2e35(%rip),%esi
mov    0x2da6(%rip),%rax
mov    (%rax),%edx
mov    0x2d75(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d4c(%rip),%rcx
cmp    (%rcx),%rax
jne    1279 <map_zeroed_pages+0x109>
mov    -0x4(%rbp),%edx
mov    $0x1,%edi
lea    0xd8c(%rip),%rsi
call   1070 <err@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1090 <getpagesize@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
imul   -0x1c(%rbp),%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1090 <getpagesize@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%esi
imul   -0x4(%rbp),%esi
mov    0x2d34(%rip),%rax
mov    (%rax),%edx
mov    0x2cfb(%rip),%rax
or     (%rax),%edx
call   1080 <mprotect@plt>
cmp    $0xffffffff,%eax
jne    12e1 <map_zeroed_pages+0x171>
mov    -0x4(%rbp),%edx
mov    $0x1,%edi
lea    0xd36(%rip),%rsi
call   1070 <err@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1090 <getpagesize@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
imul   -0x1c(%rbp),%ecx
movslq %ecx,%rcx
add    %rcx,%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<map_zeroed_pages>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %edi,%r14d
xor    %eax,%eax
call   1090 <getpagesize@plt>
lea    0x3(%r14),%ebx
imul   %eax,%ebx
test   %ebp,%ebp
je     11bc <map_zeroed_pages+0x4c>
cmpl   $0x0,0x2ecb(%rip)
jne    11bc <map_zeroed_pages+0x4c>
mov    0x2e5a(%rip),%rax
mov    0x2e23(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
mov    0x2e40(%rip),%rax
or     (%rax),%edi
call   1030 <open_memfile@plt>
mov    %eax,%r15d
jmp    11d5 <map_zeroed_pages+0x65>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
call   1030 <open_memfile@plt>
mov    %eax,%r15d
cmp    0x2e8d(%rip),%ebx
jle    11df <map_zeroed_pages+0x6f>
mov    %r15d,%edi
mov    %ebx,%esi
call   1050 <ftruncate@plt>
mov    %ebx,0x2e7b(%rip)
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
mov    0x2dbb(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2d98(%rip),%rax
cmp    (%rax),%rbx
jne    1229 <map_zeroed_pages+0xb9>
lea    0xde4(%rip),%rsi
mov    $0x1,%edi
mov    %r14d,%edx
call   1070 <err@plt>
xor    %eax,%eax
call   1090 <getpagesize@plt>
add    %eax,%eax
movslq %eax,%rbp
add    %rbx,%rbp
xor    %eax,%eax
call   1090 <getpagesize@plt>
imul   %r14d,%eax
mov    0x2d9e(%rip),%rcx
mov    0x2d67(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    %rbp,%rdi
mov    %eax,%esi
call   1080 <mprotect@plt>
cmp    $0xffffffff,%eax
jne    1278 <map_zeroed_pages+0x108>
lea    0xda7(%rip),%rsi
mov    $0x1,%edi
mov    %r14d,%edx
call   1070 <err@plt>
mov    %r15d,%edi
call   1060 <close@plt>
xor    %eax,%eax
call   1090 <getpagesize@plt>
add    %eax,%eax
cltq
add    %rax,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@map_zeroed_pages.length = internal global i32 0
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@PROT_NONE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str = [18 x i8] c"Mmapping %u pages\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@.str.1 = [28 x i8] c"mprotect rw %u pages failed\00"
define i8* @map_zeroed_pages(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i32 2, i32* %8
%9 = call i32 (...) @getpagesize()
%10 = load i32, i32* %3
%11 = load i32, i32* %8
%12 = add i32 %10, %11
%13 = add i32 %12, 1
%14 = mul i32 %9, %13
store i32 %14, i32* %5
%15 = load i32, i32* @map_zeroed_pages.length
%16 = icmp ne i32 %15, 0
br i1 %16, label %30, label %17
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %30
20: ; preds = %17
%21 = load i32, i32* @O_CREAT
%22 = load i32, i32* @O_RDWR
%23 = or i32 %21, %22
%24 = load i32, i32* @O_TRUNC
%25 = or i32 %23, %24
%26 = call i32 @open_memfile(i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = load i32, i32* %5
%29 = call i32 @ftruncate(i32 %27, i32 %28)
br label %41
30: ; preds = %17, %2
%31 = load i32, i32* @O_RDWR
%32 = call i32 @open_memfile(i32 %31)
store i32 %32, i32* %6
%33 = load i32, i32* %5
%34 = load i32, i32* @map_zeroed_pages.length
%35 = icmp sgt i32 %33, %34
br i1 %35, label %36, label %40
36: ; preds = %30
%37 = load i32, i32* %6
%38 = load i32, i32* %5
%39 = call i32 @ftruncate(i32 %37, i32 %38)
br label %40
40: ; preds = %36, %30
br label %41
41: ; preds = %40, %20
%42 = load i32, i32* %5
store i32 %42, i32* @map_zeroed_pages.length
%43 = load i32, i32* @map_zeroed_pages.length
%44 = load i32, i32* @PROT_NONE
%45 = load i32, i32* @MAP_PRIVATE
%46 = load i32, i32* %6
%47 = call i8* @mmap(i32* null, i32 %43, i32 %44, i32 %45, i32 %46, i32 0)
store i8* %47, i8** %7
%48 = load i8*, i8** %7
%49 = load i8*, i8** @MAP_FAILED
%50 = icmp eq i8* %48, %49
br i1 %50, label %51, label %54
51: ; preds = %41
%52 = load i32, i32* %3
%53 = call i32 @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %52)
br label %54
54: ; preds = %51, %41
%55 = load i8*, i8** %7
%56 = call i32 (...) @getpagesize()
%57 = load i32, i32* %8
%58 = mul nsw i32 %56, %57
%59 = sext i32 %58 to i64
%60 = getelementptr i8, i8* %55, i64 %59
%61 = call i32 (...) @getpagesize()
%62 = load i32, i32* %3
%63 = mul i32 %61, %62
%64 = load i32, i32* @PROT_READ
%65 = load i32, i32* @PROT_WRITE
%66 = or i32 %64, %65
%67 = call i32 @mprotect(i8* %60, i32 %63, i32 %66)
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %72
69: ; preds = %54
%70 = load i32, i32* %3
%71 = call i32 @err(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %70)
br label %72
72: ; preds = %69, %54
%73 = load i32, i32* %6
%74 = call i32 @close(i32 %73)
%75 = load i8*, i8** %7
%76 = call i32 (...) @getpagesize()
%77 = load i32, i32* %8
%78 = mul nsw i32 %76, %77
%79 = sext i32 %78 to i64
%80 = getelementptr i8, i8* %75, i64 %79
ret i8* %80
}

LLVM IR (-O2):
@map_zeroed_pages.length = internal unnamed_addr global i32 0
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@PROT_NONE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str = [18 x i8] c"Mmapping %u pages\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@.str.1 = [28 x i8] c"mprotect rw %u pages failed\00"
define i8* @map_zeroed_pages(i32 %0, i32 %1) {
%3 = tail call i32 (...) @getpagesize()
%4 = add i32 %0, 3
%5 = mul i32 %3, %4
%6 = load i32, i32* @map_zeroed_pages.length
%7 = icmp eq i32 %6, 0
%8 = icmp ne i32 %1, 0
%9 = and i1 %8, %7
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = load i32, i32* @O_CREAT
%12 = load i32, i32* @O_RDWR
%13 = or i32 %12, %11
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = tail call i32 @open_memfile(i32 %15)
br label %22
17: ; preds = %2
%18 = load i32, i32* @O_RDWR
%19 = tail call i32 @open_memfile(i32 %18)
%20 = load i32, i32* @map_zeroed_pages.length
%21 = icmp sgt i32 %5, %20
br i1 %21, label %22, label %25
22: ; preds = %17, %10
%23 = phi i32 [ %16, %10 ], [ %19, %17 ]
%24 = tail call i32 @ftruncate(i32 %23, i32 %5)
br label %25
25: ; preds = %22, %17
%26 = phi i32 [ %19, %17 ], [ %23, %22 ]
store i32 %5, i32* @map_zeroed_pages.length
%27 = load i32, i32* @PROT_NONE
%28 = load i32, i32* @MAP_PRIVATE
%29 = tail call i8* @mmap(i32* null, i32 %5, i32 %27, i32 %28, i32 %26, i32 0)
%30 = load i8*, i8** @MAP_FAILED
%31 = icmp eq i8* %29, %30
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %34
34: ; preds = %32, %25
%35 = tail call i32 (...) @getpagesize()
%36 = shl nsw i32 %35, 1
%37 = sext i32 %36 to i64
%38 = getelementptr i8, i8* %29, i64 %37
%39 = tail call i32 (...) @getpagesize()
%40 = mul i32 %39, %0
%41 = load i32, i32* @PROT_READ
%42 = load i32, i32* @PROT_WRITE
%43 = or i32 %42, %41
%44 = tail call i32 @mprotect(i8* %38, i32 %40, i32 %43)
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %34
%47 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %48
48: ; preds = %46, %34
%49 = tail call i32 @close(i32 %26)
%50 = tail call i32 (...) @getpagesize()
%51 = shl nsw i32 %50, 1
%52 = sext i32 %51 to i64
%53 = getelementptr i8, i8* %29, i64 %52
ret i8* %53
}