#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 scalar_t__ uint8_t;

/* Variables and functions */
int MAP_ANONYMOUS;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
void *mmap(void *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int main() {
  volatile void *addr =
      mmap((void *)0x10000000, 2 << 20, PROT_READ | PROT_WRITE,
           MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  if ((long)addr < 0) {
    perror("mmap");
    return 1;
  }
  uint8_t i = 0;
  while (1) {
    *((volatile uint8_t *)addr) = i++;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2ec2(%rip),%rax
mov    (%rax),%edx
mov    0x2e91(%rip),%rax
or     (%rax),%edx
mov    0x2e80(%rip),%rax
mov    (%rax),%ecx
mov    0x2e8f(%rip),%rax
or     (%rax),%ecx
mov    $0x10000000,%edi
mov    $0x200000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmp    $0x0,%rax
jge    1194 <main+0x74>
lea    0xe7c(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbp
ret
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rcx
mov    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    119c <main+0x7c>

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    0x2ed0(%rip),%rax
mov    0x2ea1(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e8e(%rip),%rax
mov    0x2e9f(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    $0x10000000,%edi
mov    $0x200000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
test   %rax,%rax
js     1179 <main+0x59>
xor    %ecx,%ecx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rcx,(%rax)
add    $0x1,%rcx
jmp    1170 <main+0x50>
lea    0xe80(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%eax
pop    %rcx
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str = [5 x i8] c"mmap\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
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 = call i8* @mmap(i8* inttoptr (i64 268435456 to i8*), i32 2097152, i32 %6, i32 %9, i32 -1, i32 0)
store i8* %10, i8** %2
%11 = load i8*, i8** %2
%12 = ptrtoint i8* %11 to i64
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
ret i32 1
16: ; preds = %0
store i64 0, i64* %3
br label %17
17: ; preds = %16, %17
%18 = load i64, i64* %3
%19 = add nsw i64 %18, 1
store i64 %19, i64* %3
%20 = load i8*, i8** %2
%21 = bitcast i8* %20 to i64*
store volatile i64 %18, i64* %21
br label %17
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str = [5 x i8] c"mmap\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 %5, %4
%7 = tail call i8* @mmap(i8* inttoptr (i64 268435456 to i8*), i32 2097152, i32 %3, i32 %6, i32 -1, i32 0)
%8 = icmp slt i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %0
%10 = bitcast i8* %7 to i64*
br label %13
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
ret i32 1
13: ; preds = %9, %13
%14 = phi i64 [ %15, %13 ], [ 0, %9 ]
%15 = add nuw nsw i64 %14, 1
store volatile i64 %14, i64* %10
br label %13
}