#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;
void *MAP_FAILED;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
void *mmap(void *, size_t, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, void *, void *);
void *plat_mmap(unsigned long addr, size_t size) {
  void *req, *ret;
  req = (void *)addr;
  ret = mmap(req, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1,
             0);
  if (ret == MAP_FAILED)
    return NULL;
  if (ret != req)
    printf("warning: mmaped to %p, requested %p\n", ret, req);
  return ret;
}


x86 assembly (clang, -O0):
<plat_mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2eb1(%rip),%rax
mov    (%rax),%edx
mov    0x2e80(%rip),%rax
or     (%rax),%edx
mov    0x2e6f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e7e(%rip),%rax
or     (%rax),%ecx
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%rax
jne    1197 <plat_mmap+0x77>
movq   $0x0,-0x8(%rbp)
jmp    11c1 <plat_mmap+0xa1>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     11b9 <plat_mmap+0x99>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xe4c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<plat_mmap>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2ec9(%rip),%rax
mov    0x2e9a(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e87(%rip),%rax
mov    0x2e98(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %r15d,%r15d
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e59(%rip),%rax
cmp    (%rax),%rbx
je     118b <plat_mmap+0x6b>
cmp    %r14,%rbx
je     1188 <plat_mmap+0x68>
lea    0xe88(%rip),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <printf@plt>
mov    %rbx,%r15
jmp    118b <plat_mmap+0x6b>
mov    %r14,%r15
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@.str = [37 x i8] c"warning: mmaped to %p, requested %p\0A\00"
define i8* @plat_mmap(i64 %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
store i64 %0, i64* %4
store i64 %1, i64* %5
%8 = load i64, i64* %4
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %6
%10 = load i8*, i8** %6
%11 = load i64, i64* %5
%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 i8* @mmap(i8* %10, i64 %11, i32 %14, i32 %17, i32 -1, i32 0)
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = load i8*, i8** @MAP_FAILED
%21 = icmp eq i8* %19, %20
br i1 %21, label %22, label %23
22: ; preds = %2
store i8* null, i8** %3
br label %33
23: ; preds = %2
%24 = load i8*, i8** %7
%25 = load i8*, i8** %6
%26 = icmp ne i8* %24, %25
br i1 %26, label %27, label %31
27: ; preds = %23
%28 = load i8*, i8** %7
%29 = load i8*, i8** %6
%30 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %28, i8* %29)
br label %31
31: ; preds = %27, %23
%32 = load i8*, i8** %7
store i8* %32, i8** %3
br label %33
33: ; preds = %31, %22
%34 = load i8*, i8** %3
ret i8* %34
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@.str = [37 x i8] c"warning: mmaped to %p, requested %p\0A\00"
define i8* @plat_mmap(i64 %0, i64 %1) {
%3 = inttoptr i64 %0 to i8*
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @PROT_WRITE
%6 = or i32 %5, %4
%7 = load i32, i32* @MAP_PRIVATE
%8 = load i32, i32* @MAP_ANONYMOUS
%9 = or i32 %8, %7
%10 = tail call i8* @mmap(i8* %3, i64 %1, i32 %6, i32 %9, i32 -1, i32 0)
%11 = load i8*, i8** @MAP_FAILED
%12 = icmp eq i8* %10, %11
br i1 %12, label %17, label %13
13: ; preds = %2
%14 = icmp eq i8* %10, %3
br i1 %14, label %17, label %15
15: ; preds = %13
%16 = tail call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %10, i8* %3)
br label %17
17: ; preds = %13, %15, %2
%18 = phi i8* [ null, %2 ], [ %10, %15 ], [ %3, %13 ]
ret i8* %18
}