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

/* Variables and functions */
int MAP_ANONYMOUS;
void *MAP_FAILED;
int MAP_PRIVATE;
int /*<<< orphan*/ PROT_NONE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _SC_PAGESIZE;
void *mmap(void *, unsigned long const, int, int, int const, int);
scalar_t__ mprotect(void *, unsigned long const, int /*<<< orphan*/);
scalar_t__ munmap(void *, unsigned long const);
int /*<<< orphan*/ printf(char *, void *, unsigned long const, ...);
int sysconf(int /*<<< orphan*/);
int main(void) {
  const intmax_t pagesize = sysconf(_SC_PAGESIZE);
  const unsigned long length = pagesize * 3;
  const int fd = -1;
  off_t offset;
  void *addr, *p;
  offset = 0xdeadbeef000 & -pagesize;
  addr = (void *)(unsigned int)(0xfaced000 & -pagesize);
  p = mmap(addr, length, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
           fd, offset);
  if (p == MAP_FAILED || mprotect(p, length, PROT_NONE) || munmap(p, length))
    return 77;
  if (sizeof(offset) == sizeof(int))
    printf("(mmap2?|old_mmap)\\(%p, %lu, PROT_READ\\|PROT_WRITE, "
           "MAP_PRIVATE\\|MAP_ANONYMOUS, %d, %#x\\) = %p\n",
           addr, length, fd, (unsigned int)offset, p);
  else
    printf("(mmap2?|old_mmap)\\(%p, %lu, PROT_READ\\|PROT_WRITE, "
           "MAP_PRIVATE\\|MAP_ANONYMOUS, %d, %#jx\\) = %p\n",
           addr, length, fd, (uintmax_t)offset, p);
  printf("mprotect\\(%p, %lu, PROT_NONE\\) += 0\n", p, length);
  printf("munmap\\(%p, %lu\\) += 0\n", p, length);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %eax,-0x8(%rbp)
imul   $0x3,-0x8(%rbp),%eax
cltq
mov    %rax,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
xor    %eax,%eax
sub    -0x8(%rbp),%eax
cltq
movabs $0xdeadbeef000,%rcx
and    %rcx,%rax
mov    %eax,-0x18(%rbp)
xor    %eax,%eax
sub    -0x8(%rbp),%eax
and    $0xfaced000,%eax
mov    %eax,%eax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
mov    0x2e00(%rip),%rax
or     (%rax),%edx
mov    0x2def(%rip),%rax
mov    (%rax),%ecx
mov    0x2dfe(%rip),%rax
or     (%rax),%ecx
mov    -0x18(%rbp),%r9d
mov    $0xffffffff,%r8d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2dbe(%rip),%rcx
cmp    (%rcx),%rax
je     1232 <main+0xe2>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
call   1060 <mprotect@plt>
cmp    $0x0,%rax
jne    1232 <main+0xe2>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <munmap@plt>
cmp    $0x0,%rax
je     123e <main+0xee>
movl   $0x4d,-0x4(%rbp)
jmp    1294 <main+0x144>
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%r8d
mov    -0x28(%rbp),%r9
lea    0xdab(%rip),%rdi
mov    $0xffffffff,%ecx
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
mov    %eax,%ebx
lea    (%rbx,%rbx,2),%eax
movslq %eax,%r14
neg    %ebx
mov    %ebx,%ebp
and    $0xdbeef000,%ebp
and    $0xfaced000,%ebx
mov    0x2e73(%rip),%rax
mov    0x2e34(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e21(%rip),%rax
mov    0x2e32(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0xffffffff,%r8d
mov    %ebp,%r9d
call   1030 <mmap@plt>
mov    %rax,%r12
mov    0x2df0(%rip),%rax
mov    $0x4d,%r15d
cmp    (%rax),%r12
je     123e <main+0xee>
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    %r14,%rsi
call   1060 <mprotect@plt>
test   %rax,%rax
jne    123e <main+0xee>
mov    %r12,%rdi
mov    %r14,%rsi
call   1050 <munmap@plt>
test   %rax,%rax
jne    123e <main+0xee>
lea    0xe05(%rip),%rdi
xor    %r15d,%r15d
mov    %rbx,%rsi
mov    %r14,%rdx
mov    $0xffffffff,%ecx
mov    %ebp,%r8d
mov    %r12,%r9
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe42(%rip),%rdi
mov    %r12,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe53(%rip),%rdi
mov    %r12,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@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
@PROT_NONE = global i32 0
@.str = [95 x i8] c"(mmap2?|old_mmap)\\(%p, %lu, PROT_READ\\|PROT_WRITE, MAP_PRIVATE\\|MAP_ANONYMOUS, %d, %#x\\) = %p\0A\00"
@.str.1 = [37 x i8] c"mprotect\\(%p, %lu, PROT_NONE\\) += 0\0A\00"
@.str.2 = [24 x i8] c"munmap\\(%p, %lu\\) += 0\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i32 0, i32* %1
%8 = load i32, i32* @_SC_PAGESIZE
%9 = call i32 @sysconf(i32 %8)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = mul nsw i32 %10, 3
%12 = sext i32 %11 to i64
store i64 %12, i64* %3
store i32 -1, i32* %4
%13 = load i32, i32* %2
%14 = sub nsw i32 0, %13
%15 = sext i32 %14 to i64
%16 = and i64 15302363377664, %15
%17 = trunc i64 %16 to i32
store i32 %17, i32* %5
%18 = load i32, i32* %2
%19 = sub nsw i32 0, %18
%20 = and i32 -87109632, %19
%21 = zext i32 %20 to i64
%22 = inttoptr i64 %21 to i8*
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = load i64, i64* %3
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @PROT_WRITE
%27 = or i32 %25, %26
%28 = load i32, i32* @MAP_PRIVATE
%29 = load i32, i32* @MAP_ANONYMOUS
%30 = or i32 %28, %29
%31 = load i32, i32* %5
%32 = call i8* @mmap(i8* %23, i64 %24, i32 %27, i32 %30, i32 -1, i32 %31)
store i8* %32, i8** %7
%33 = load i8*, i8** %7
%34 = load i8*, i8** @MAP_FAILED
%35 = icmp eq i8* %33, %34
br i1 %35, label %47, label %36
36: ; preds = %0
%37 = load i8*, i8** %7
%38 = load i64, i64* %3
%39 = load i32, i32* @PROT_NONE
%40 = call i64 @mprotect(i8* %37, i64 %38, i32 %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %47, label %42
42: ; preds = %36
%43 = load i8*, i8** %7
%44 = load i64, i64* %3
%45 = call i64 @munmap(i8* %43, i64 %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %42, %36, %0
store i32 77, i32* %1
br label %60
48: ; preds = %42
%49 = load i8*, i8** %6
%50 = load i64, i64* %3
%51 = load i32, i32* %5
%52 = load i8*, i8** %7
%53 = call i32 (i8*, i8*, i64, ...) @printf(i8* getelementptr inbounds ([95 x i8], [95 x i8]* @.str, i64 0, i64 0), i8* %49, i64 %50, i32 -1, i32 %51, i8* %52)
%54 = load i8*, i8** %7
%55 = load i64, i64* %3
%56 = call i32 (i8*, i8*, i64, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %54, i64 %55)
%57 = load i8*, i8** %7
%58 = load i64, i64* %3
%59 = call i32 (i8*, i8*, i64, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %57, i64 %58)
store i32 0, i32* %1
br label %60
60: ; preds = %48, %47
%61 = load i32, i32* %1
ret i32 %61
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@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
@PROT_NONE = global i32 0
@.str = [95 x i8] c"(mmap2?|old_mmap)\\(%p, %lu, PROT_READ\\|PROT_WRITE, MAP_PRIVATE\\|MAP_ANONYMOUS, %d, %#x\\) = %p\0A\00"
@.str.1 = [37 x i8] c"mprotect\\(%p, %lu, PROT_NONE\\) += 0\0A\00"
@.str.2 = [24 x i8] c"munmap\\(%p, %lu\\) += 0\0A\00"
define i32 @main() {
%1 = load i32, i32* @_SC_PAGESIZE
%2 = tail call i32 @sysconf(i32 %1)
%3 = mul nsw i32 %2, 3
%4 = sext i32 %3 to i64
%5 = sub nsw i32 0, %2
%6 = and i32 %5, -605097984
%7 = and i32 %5, -87109632
%8 = zext i32 %7 to i64
%9 = inttoptr i64 %8 to i8*
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %11, %10
%13 = load i32, i32* @MAP_PRIVATE
%14 = load i32, i32* @MAP_ANONYMOUS
%15 = or i32 %14, %13
%16 = tail call i8* @mmap(i8* %9, i64 %4, i32 %12, i32 %15, i32 -1, i32 %6)
%17 = load i8*, i8** @MAP_FAILED
%18 = icmp eq i8* %16, %17
br i1 %18, label %30, label %19
19: ; preds = %0
%20 = load i32, i32* @PROT_NONE
%21 = tail call i64 @mprotect(i8* %16, i64 %4, i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %19
%24 = tail call i64 @munmap(i8* %16, i64 %4)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = tail call i32 (i8*, i8*, i64, ...) @printf(i8* getelementptr inbounds ([95 x i8], [95 x i8]* @.str, i64 0, i64 0), i8* %9, i64 %4, i32 -1, i32 %6, i8* %16)
%28 = tail call i32 (i8*, i8*, i64, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %16, i64 %4)
%29 = tail call i32 (i8*, i8*, i64, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %16, i64 %4)
br label %30
30: ; preds = %0, %19, %23, %26
%31 = phi i32 [ 0, %26 ], [ 77, %23 ], [ 77, %19 ], [ 77, %0 ]
ret i32 %31
}