#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;
int /*<<< orphan*/ check_bytes(void *);
int /*<<< orphan*/ exit(int);
void *mmap(void *, unsigned long, int, int, int, int /*<<< orphan*/);
scalar_t__ munmap(void *, unsigned long);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, void *);
int read_bytes(void *);
int /*<<< orphan*/ write_bytes(void *);
int main(void) {
  void *addr;
  int ret;
  addr = mmap((void *)(0x0UL), (256UL * 1024 * 1024), (PROT_READ | PROT_WRITE),
              (MAP_PRIVATE | MAP_ANONYMOUS | 0x40000), -1, 0);
  if (addr == MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  printf("Returned address is %p\n", addr);
  check_bytes(addr);
  write_bytes(addr);
  ret = read_bytes(addr);
  if (munmap(addr, (256UL * 1024 * 1024))) {
    perror("munmap");
    exit(1);
  }
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
mov    0x2e31(%rip),%rax
or     (%rax),%edx
mov    0x2e20(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2f(%rip),%rax
or     (%rax),%ecx
or     $0x40000,%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x10000000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2de1(%rip),%rcx
cmp    (%rcx),%rax
jne    11fe <main+0x7e>
lea    0xe11(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rsi
lea    0xdfc(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1080 <check_bytes@plt>
mov    -0x10(%rbp),%rdi
call   1060 <write_bytes@plt>
mov    -0x10(%rbp),%rdi
call   1030 <read_bytes@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x10000000,%esi
call   1070 <munmap@plt>
cmp    $0x0,%rax
je     125a <main+0xda>
lea    0xdd2(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e6e(%rip),%rax
mov    0x2e3f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e2c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e3b(%rip),%rax
or     (%rax),%ecx
or     $0x40000,%ecx
mov    $0x10000000,%esi
xor    %edi,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2df4(%rip),%rax
cmp    (%rax),%rbx
je     1215 <main+0x95>
lea    0xe2d(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1080 <check_bytes@plt>
mov    %rbx,%rdi
call   1060 <write_bytes@plt>
mov    %rbx,%rdi
call   1030 <read_bytes@plt>
mov    %eax,%ebp
mov    $0x10000000,%esi
mov    %rbx,%rdi
call   1070 <munmap@plt>
test   %rax,%rax
jne    121e <main+0x9e>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xde4(%rip),%rdi
jmp    1225 <main+0xa5>
lea    0xdf8(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <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_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
@.str.1 = [24 x i8] c"Returned address is %p\0A\00"
@.str.2 = [7 x i8] c"munmap\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @PROT_WRITE
%6 = or i32 %4, %5
%7 = load i32, i32* @MAP_PRIVATE
%8 = load i32, i32* @MAP_ANONYMOUS
%9 = or i32 %7, %8
%10 = or i32 %9, 262144
%11 = call i8* @mmap(i8* null, i64 268435456, i32 %6, i32 %10, i32 -1, i32 0)
store i8* %11, i8** %2
%12 = load i8*, i8** %2
%13 = load i8*, i8** @MAP_FAILED
%14 = icmp eq i8* %12, %13
br i1 %14, label %15, label %18
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %0
%19 = load i8*, i8** %2
%20 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = load i8*, i8** %2
%22 = call i32 @check_bytes(i8* %21)
%23 = load i8*, i8** %2
%24 = call i32 @write_bytes(i8* %23)
%25 = load i8*, i8** %2
%26 = call i32 @read_bytes(i8* %25)
store i32 %26, i32* %3
%27 = load i8*, i8** %2
%28 = call i64 @munmap(i8* %27, i64 268435456)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %18
%31 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %18
%34 = load i32, i32* %3
ret i32 %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 = [5 x i8] c"mmap\00"
@.str.1 = [24 x i8] c"Returned address is %p\0A\00"
@.str.2 = [7 x i8] c"munmap\00"
define i32 @main() {
%1 = load i32, i32* @PROT_READ
%2 = load i32, i32* @PROT_WRITE
%3 = or i32 %2, %1
%4 = load i32, i32* @MAP_PRIVATE
%5 = load i32, i32* @MAP_ANONYMOUS
%6 = or i32 %4, %5
%7 = or i32 %6, 262144
%8 = tail call i8* @mmap(i8* null, i64 268435456, i32 %3, i32 %7, i32 -1, i32 0)
%9 = load i8*, i8** @MAP_FAILED
%10 = icmp eq i8* %8, %9
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%16 = tail call i32 @check_bytes(i8* %8)
%17 = tail call i32 @write_bytes(i8* %8)
%18 = tail call i32 @read_bytes(i8* %8)
%19 = tail call i64 @munmap(i8* %8, i64 268435456)
%20 = icmp eq i64 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %14
%22 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %14
ret i32 %18
}