#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_ANONYMOUS;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
scalar_t__ mmap(int /*<<< orphan*/, unsigned int, int, int, int,
                int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, unsigned int);
int /*<<< orphan*/ perror(char *);
char *mkmap(unsigned sz) {
  static char *map;
  static unsigned mapsz;
  char *p;
  if (map != NULL)
    munmap(map, mapsz);
  p = (char *)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
                   -1, 0);
  if (p == (char *)-1) {
    perror("mmap");
    exit(1);
  }
  map = p;
  mapsz = sz;
  return p;
}


x86 assembly (clang, -O0):
<mkmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
cmpq   $0x0,0x2ef5(%rip)
je     116b <mkmap+0x2b>
mov    0x2ee8(%rip),%rdi
mov    0x2eea(%rip),%esi
call   1040 <munmap@plt>
mov    -0x4(%rbp),%esi
mov    0x2e83(%rip),%rax
mov    (%rax),%edx
mov    0x2e52(%rip),%rax
or     (%rax),%edx
mov    0x2e41(%rip),%rax
mov    (%rax),%ecx
mov    0x2e50(%rip),%rax
or     (%rax),%ecx
xor    %r9d,%r9d
mov    $0xffffffff,%r8d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x10(%rbp)
jne    11ce <mkmap+0x8e>
lea    0xe41(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,0x2e6f(%rip)
mov    -0x4(%rbp),%eax
mov    %eax,0x2e6e(%rip)
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkmap>:
push   %rbx
mov    %edi,%ebx
mov    0x2efe(%rip),%rdi
test   %rdi,%rdi
je     115a <mkmap+0x1a>
mov    0x2efb(%rip),%esi
call   1040 <munmap@plt>
mov    0x2e97(%rip),%rax
mov    0x2e68(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e55(%rip),%rax
mov    0x2e66(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %ebx,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     11a5 <mkmap+0x65>
mov    %rax,0x2eab(%rip)
mov    %ebx,0x2ead(%rip)
pop    %rbx
ret
lea    0xe54(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@mkmap.map = internal global i8* null
@mkmap.mapsz = internal global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str = [5 x i8] c"mmap\00"
define i8* @mkmap(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
store i32 %0, i32* %2
%4 = load i8*, i8** @mkmap.map
%5 = icmp ne i8* %4, null
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = load i8*, i8** @mkmap.map
%8 = load i32, i32* @mkmap.mapsz
%9 = call i32 @munmap(i8* %7, i32 %8)
br label %10
10: ; preds = %6, %1
%11 = load i32, i32* %2
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %12, %13
%15 = load i32, i32* @MAP_PRIVATE
%16 = load i32, i32* @MAP_ANONYMOUS
%17 = or i32 %15, %16
%18 = call i64 @mmap(i32 0, i32 %11, i32 %14, i32 %17, i32 -1, i32 0)
%19 = inttoptr i64 %18 to i8*
store i8* %19, i8** %3
%20 = load i8*, i8** %3
%21 = icmp eq i8* %20, inttoptr (i64 -1 to i8*)
br i1 %21, label %22, label %25
22: ; preds = %10
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %10
%26 = load i8*, i8** %3
store i8* %26, i8** @mkmap.map
%27 = load i32, i32* %2
store i32 %27, i32* @mkmap.mapsz
%28 = load i8*, i8** %3
ret i8* %28
}

LLVM IR (-O2):
@mkmap.map = internal unnamed_addr global i8* null
@mkmap.mapsz = internal unnamed_addr global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str = [5 x i8] c"mmap\00"
define i8* @mkmap(i32 %0) {
%2 = load i8*, i8** @mkmap.map
%3 = icmp eq i8* %2, null
br i1 %3, label %7, label %4
4: ; preds = %1
%5 = load i32, i32* @mkmap.mapsz
%6 = tail call i32 @munmap(i8* %2, i32 %5)
br label %7
7: ; preds = %4, %1
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %9, %8
%11 = load i32, i32* @MAP_PRIVATE
%12 = load i32, i32* @MAP_ANONYMOUS
%13 = or i32 %12, %11
%14 = tail call i64 @mmap(i32 0, i32 %0, i32 %10, i32 %13, i32 -1, i32 0)
%15 = inttoptr i64 %14 to i8*
%16 = icmp eq i8* %15, inttoptr (i64 -1 to i8*)
br i1 %16, label %17, label %20
17: ; preds = %7
%18 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %7
store i8* %15, i8** @mkmap.map
store i32 %0, i32* @mkmap.mapsz
ret i8* %15
}