#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 /*<<< orphan*/ EXIT_FAILURE;
int MAP_ANONYMOUS;
void *MAP_FAILED;
int MAP_FIXED;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
void *mmap64(void *, size_t, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
void safe_anon_mmap(void *addr, size_t size) {
  void *ret_addr = mmap64(addr, size, PROT_READ | PROT_WRITE,
                          MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
  if (ret_addr == MAP_FAILED) {
    perror("MMAP_WRAPPER(anon)");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<safe_anon_mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2ea9(%rip),%rax
mov    (%rax),%edx
mov    0x2e70(%rip),%rax
or     (%rax),%edx
mov    0x2e5f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e76(%rip),%rax
or     (%rax),%ecx
mov    0x2e45(%rip),%rax
or     (%rax),%ecx
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap64@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e1e(%rip),%rcx
cmp    (%rcx),%rax
jne    11b5 <safe_anon_mmap+0x85>
lea    0xe5e(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_anon_mmap>:
push   %rax
mov    0x2ec0(%rip),%rax
mov    0x2e89(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e76(%rip),%rax
mov    0x2e8f(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e5c(%rip),%rax
or     (%rax),%ecx
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap64@plt>
mov    0x2e3d(%rip),%rcx
cmp    (%rcx),%rax
je     117a <safe_anon_mmap+0x4a>
pop    %rax
ret
lea    0xe7f(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FIXED = global i32 0
@MAP_FAILED = global i8* null
@.str = [19 x i8] c"MMAP_WRAPPER(anon)\00"
@EXIT_FAILURE = global i32 0
define void @safe_anon_mmap(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%6 = load i8*, i8** %3
%7 = load i64, i64* %4
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %8, %9
%11 = load i32, i32* @MAP_PRIVATE
%12 = load i32, i32* @MAP_ANONYMOUS
%13 = or i32 %11, %12
%14 = load i32, i32* @MAP_FIXED
%15 = or i32 %13, %14
%16 = call i8* @mmap64(i8* %6, i64 %7, i32 %10, i32 %15, i32 -1, i32 0)
store i8* %16, i8** %5
%17 = load i8*, i8** %5
%18 = load i8*, i8** @MAP_FAILED
%19 = icmp eq i8* %17, %18
br i1 %19, label %20, label %24
20: ; preds = %2
%21 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %2
ret void
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FIXED = global i32 0
@MAP_FAILED = global i8* null
@.str = [19 x i8] c"MMAP_WRAPPER(anon)\00"
@EXIT_FAILURE = global i32 0
define void @safe_anon_mmap(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_PRIVATE
%7 = load i32, i32* @MAP_ANONYMOUS
%8 = or i32 %7, %6
%9 = load i32, i32* @MAP_FIXED
%10 = or i32 %8, %9
%11 = tail call i8* @mmap64(i8* %0, i64 %1, i32 %5, i32 %10, i32 -1, i32 0)
%12 = load i8*, i8** @MAP_FAILED
%13 = icmp eq i8* %11, %12
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
ret void
}