#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_FIXED;
int MAP_HUGETLB;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
void *mmap(void *, unsigned long, int, int, int, int /*<<< orphan*/);
void *do_mapping(void *base, unsigned long len) {
  void *mem;
  mem = mmap((void *)base, len, PROT_READ | PROT_WRITE,
             MAP_FIXED | MAP_HUGETLB | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  if (mem != (void *)base) {
    mem = mmap((void *)base, len, PROT_READ | PROT_WRITE,
               MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    if (mem != (void *)base)
      return NULL;
  }
  return mem;
}


x86 assembly (clang, -O0):
<do_mapping>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2ec9(%rip),%rax
mov    (%rax),%edx
mov    0x2e90(%rip),%rax
or     (%rax),%edx
mov    0x2e77(%rip),%rax
mov    (%rax),%ecx
mov    0x2e9e(%rip),%rax
or     (%rax),%ecx
mov    0x2e6d(%rip),%rax
or     (%rax),%ecx
mov    0x2e7c(%rip),%rax
or     (%rax),%ecx
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11e5 <do_mapping+0xd5>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e6b(%rip),%rax
mov    (%rax),%edx
mov    0x2e32(%rip),%rax
or     (%rax),%edx
mov    0x2e19(%rip),%rax
mov    (%rax),%ecx
mov    0x2e18(%rip),%rax
or     (%rax),%ecx
mov    0x2e27(%rip),%rax
or     (%rax),%ecx
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11e0 <do_mapping+0xd0>
movq   $0x0,-0x8(%rbp)
jmp    11ed <do_mapping+0xdd>
jmp    11e5 <do_mapping+0xd5>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_mapping>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2ed6(%rip),%rax
mov    0x2e9f(%rip),%r13
mov    0x0(%r13),%edx
or     (%rax),%edx
mov    0x2e82(%rip),%r12
mov    0x2eab(%rip),%rax
mov    (%rax),%ecx
or     (%r12),%ecx
mov    0x2e76(%rip),%r15
or     (%r15),%ecx
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e7e(%rip),%rbp
or     0x0(%rbp),%ecx
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    %rbx,%rax
je     11a2 <do_mapping+0x92>
mov    0x0(%r13),%edx
mov    0x2e7d(%rip),%rax
or     (%rax),%edx
mov    (%r15),%ecx
or     (%r12),%ecx
or     0x0(%rbp),%ecx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
cmp    %rbx,%rax
je     11a2 <do_mapping+0x92>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FIXED = global i32 0
@MAP_HUGETLB = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
define i8* @do_mapping(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i8*, i8** %4
%8 = load i64, i64* %5
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %9, %10
%12 = load i32, i32* @MAP_FIXED
%13 = load i32, i32* @MAP_HUGETLB
%14 = or i32 %12, %13
%15 = load i32, i32* @MAP_PRIVATE
%16 = or i32 %14, %15
%17 = load i32, i32* @MAP_ANONYMOUS
%18 = or i32 %16, %17
%19 = call i8* @mmap(i8* %7, i64 %8, i32 %11, i32 %18, i32 -1, i32 0)
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = load i8*, i8** %4
%22 = icmp ne i8* %20, %21
br i1 %22, label %23, label %40
23: ; preds = %2
%24 = load i8*, i8** %4
%25 = load i64, i64* %5
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @PROT_WRITE
%28 = or i32 %26, %27
%29 = load i32, i32* @MAP_FIXED
%30 = load i32, i32* @MAP_PRIVATE
%31 = or i32 %29, %30
%32 = load i32, i32* @MAP_ANONYMOUS
%33 = or i32 %31, %32
%34 = call i8* @mmap(i8* %24, i64 %25, i32 %28, i32 %33, i32 -1, i32 0)
store i8* %34, i8** %6
%35 = load i8*, i8** %6
%36 = load i8*, i8** %4
%37 = icmp ne i8* %35, %36
br i1 %37, label %38, label %39
38: ; preds = %23
store i8* null, i8** %3
br label %42
39: ; preds = %23
br label %40
40: ; preds = %39, %2
%41 = load i8*, i8** %6
store i8* %41, i8** %3
br label %42
42: ; preds = %40, %38
%43 = load i8*, i8** %3
ret i8* %43
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FIXED = global i32 0
@MAP_HUGETLB = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
define i8* @do_mapping(i8* %0, i64 %1) {
%3 = load i32, i32* @PROT_READ
%4 = load i32, i32* @PROT_WRITE
%5 = or i32 %4, %3
%6 = load i32, i32* @MAP_FIXED
%7 = load i32, i32* @MAP_HUGETLB
%8 = or i32 %7, %6
%9 = load i32, i32* @MAP_PRIVATE
%10 = or i32 %8, %9
%11 = load i32, i32* @MAP_ANONYMOUS
%12 = or i32 %10, %11
%13 = tail call i8* @mmap(i8* %0, i64 %1, i32 %5, i32 %12, i32 -1, i32 0)
%14 = icmp eq i8* %13, %0
br i1 %14, label %26, label %15
15: ; preds = %2
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @PROT_WRITE
%18 = or i32 %17, %16
%19 = load i32, i32* @MAP_FIXED
%20 = load i32, i32* @MAP_PRIVATE
%21 = or i32 %20, %19
%22 = load i32, i32* @MAP_ANONYMOUS
%23 = or i32 %21, %22
%24 = tail call i8* @mmap(i8* %0, i64 %1, i32 %18, i32 %23, i32 -1, i32 0)
%25 = icmp eq i8* %24, %0
br i1 %25, label %26, label %27
26: ; preds = %15, %2
br label %27
27: ; preds = %15, %26
%28 = phi i8* [ %0, %26 ], [ null, %15 ]
ret i8* %28
}