#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 */
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
void *MAP_FAILED;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ mprotect(void *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, void *, size_t, int, int, int,
                          int /*<<< orphan*/);
void *real_mmap(void *, size_t, int, int, int, int /*<<< orphan*/);
void *mmap(void *addr, size_t length, int prot, int flags, int fd,
           off_t offset) {
  void *ptr;
  printf("mmap(addr = %p, length = %#x, prot = %x, flags = %x, fd = %d, off = "
         "%ld\n",
         addr, length, prot, flags, fd, offset);
  ptr = real_mmap(addr, length, prot, flags, fd, offset);
  if (ptr == MAP_FAILED)
    return ptr;
  mprotect(ptr, length, PROT_WRITE);
  return ptr;
}


x86 assembly (clang, -O0):
<mmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9d,-0x28(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    -0x24(%rbp),%r9d
mov    -0x28(%rbp),%eax
lea    0xe95(%rip),%rdi
mov    %eax,(%rsp)
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x24(%rbp),%r8d
mov    -0x28(%rbp),%r9d
call   1050 <real_mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%rax
jne    11b3 <mmap+0x83>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11d1 <mmap+0xa1>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
call   1040 <mprotect@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmap>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%r15d
mov    %r8d,%r12d
mov    %ecx,%r13d
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %r9d,(%rsp)
lea    0xea9(%rip),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %ebp,%ecx
mov    %r13d,%r8d
mov    %r12d,%r9d
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
mov    %r13d,%ecx
mov    %r12d,%r8d
mov    %r15d,%r9d
call   1050 <real_mmap@plt>
mov    %rax,%rbx
mov    0x2e4e(%rip),%rax
cmp    (%rax),%rbx
je     11a3 <mmap+0x73>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <mprotect@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [73 x i8] c"mmap(addr = %p, length = %#x, prot = %x, flags = %x, fd = %d, off = %ld\0A\00"
@MAP_FAILED = global i8* null
@PROT_WRITE = global i32 0
define i8* @mmap(i8* %0, i64 %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
store i8* %0, i8** %8
store i64 %1, i64* %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
%15 = load i8*, i8** %8
%16 = load i64, i64* %9
%17 = load i32, i32* %10
%18 = load i32, i32* %11
%19 = load i32, i32* %12
%20 = load i32, i32* %13
%21 = call i32 @printf(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str, i64 0, i64 0), i8* %15, i64 %16, i32 %17, i32 %18, i32 %19, i32 %20)
%22 = load i8*, i8** %8
%23 = load i64, i64* %9
%24 = load i32, i32* %10
%25 = load i32, i32* %11
%26 = load i32, i32* %12
%27 = load i32, i32* %13
%28 = call i8* @real_mmap(i8* %22, i64 %23, i32 %24, i32 %25, i32 %26, i32 %27)
store i8* %28, i8** %14
%29 = load i8*, i8** %14
%30 = load i8*, i8** @MAP_FAILED
%31 = icmp eq i8* %29, %30
br i1 %31, label %32, label %34
32: ; preds = %6
%33 = load i8*, i8** %14
store i8* %33, i8** %7
br label %40
34: ; preds = %6
%35 = load i8*, i8** %14
%36 = load i64, i64* %9
%37 = load i32, i32* @PROT_WRITE
%38 = call i32 @mprotect(i8* %35, i64 %36, i32 %37)
%39 = load i8*, i8** %14
store i8* %39, i8** %7
br label %40
40: ; preds = %34, %32
%41 = load i8*, i8** %7
ret i8* %41
}

LLVM IR (-O2):
@.str = [73 x i8] c"mmap(addr = %p, length = %#x, prot = %x, flags = %x, fd = %d, off = %ld\0A\00"
@MAP_FAILED = global i8* null
@PROT_WRITE = global i32 0
define i8* @mmap(i8* %0, i64 %1, i32 %2, i32 %3, i32 %4, i32 %5) {
%7 = tail call i32 @printf(i8* getelementptr inbounds ([73 x i8], [73 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %1, i32 %2, i32 %3, i32 %4, i32 %5)
%8 = tail call i8* @real_mmap(i8* %0, i64 %1, i32 %2, i32 %3, i32 %4, i32 %5)
%9 = load i8*, i8** @MAP_FAILED
%10 = icmp eq i8* %8, %9
br i1 %10, label %14, label %11
11: ; preds = %6
%12 = load i32, i32* @PROT_WRITE
%13 = tail call i32 @mprotect(i8* %8, i64 %1, i32 %12)
br label %14
14: ; preds = %6, %11
ret i8* %8
}