#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_HUGETLB;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
void *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int main(int argc, char *argv[]) {
  void *block;
  char *p;
  int i, res;
  block = mmap(NULL, (4 * 1024 * 1024), PROT_READ | PROT_WRITE,
               MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
  if (block == MAP_FAILED) {
    block = mmap(NULL, (4 * 1024 * 1024), PROT_READ | PROT_WRITE,
                 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  }
  if (block == MAP_FAILED) {
    perror("mmap");
    return 3;
  }
  p = (char *)block;
  for (i = 0; i < (4 * 1024 * 1024); i += 4096) {
    p[i] = (char)i;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2ebb(%rip),%rax
mov    (%rax),%edx
mov    0x2e82(%rip),%rax
or     (%rax),%edx
mov    0x2e71(%rip),%rax
mov    (%rax),%ecx
mov    0x2e80(%rip),%rax
or     (%rax),%ecx
mov    0x2e87(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x400000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e2f(%rip),%rcx
cmp    (%rcx),%rax
jne    11d1 <main+0xb1>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
mov    0x2e26(%rip),%rax
or     (%rax),%edx
mov    0x2e15(%rip),%rax
mov    (%rax),%ecx
mov    0x2e24(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x400000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2ddc(%rip),%rcx
cmp    (%rcx),%rax
jne    11fd <main+0xdd>
lea    0xe14(%rip),%rdi
call   1040 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    1240 <main+0x120>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x400000,-0x24(%rbp)
jge    1239 <main+0x119>
mov    -0x24(%rbp),%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1000,%eax
mov    %eax,-0x24(%rbp)
jmp    120c <main+0xec>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    0x2ec8(%rip),%r14
mov    0x2e91(%rip),%r12
mov    (%r12),%edx
or     (%r14),%edx
mov    0x2e7b(%rip),%r15
mov    0x2e8c(%rip),%r13
mov    0x0(%r13),%ecx
or     (%r15),%ecx
mov    0x2e8e(%rip),%rax
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x400000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2e40(%rip),%rbx
mov    (%rbx),%rcx
cmp    %rcx,%rax
jne    11a6 <main+0x86>
mov    (%r12),%edx
or     (%r14),%edx
mov    0x0(%r13),%ecx
or     (%r15),%ecx
xor    %edi,%edi
mov    $0x400000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    (%rbx),%rcx
cmp    %rcx,%rax
je     121c <main+0xfc>
mov    $0xfffffffffffff000,%rcx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movb   $0x0,0x1000(%rax,%rcx,1)
movb   $0x0,0x2000(%rax,%rcx,1)
movb   $0x0,0x3000(%rax,%rcx,1)
movb   $0x0,0x4000(%rax,%rcx,1)
movb   $0x0,0x5000(%rax,%rcx,1)
movb   $0x0,0x6000(%rax,%rcx,1)
movb   $0x0,0x7000(%rax,%rcx,1)
movb   $0x0,0x8000(%rax,%rcx,1)
add    $0x8000,%rcx
cmp    $0x3ff000,%rcx
jb     11c0 <main+0xa0>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xddd(%rip),%rdi
call   1040 <perror@plt>
mov    $0x3,%eax
jmp    1212 <main+0xf2>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_HUGETLB = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %10, %11
%13 = load i32, i32* @MAP_PRIVATE
%14 = load i32, i32* @MAP_ANONYMOUS
%15 = or i32 %13, %14
%16 = load i32, i32* @MAP_HUGETLB
%17 = or i32 %15, %16
%18 = call i8* @mmap(i32* null, i32 4194304, i32 %12, i32 %17, i32 -1, i32 0)
store i8* %18, i8** %6
%19 = load i8*, i8** %6
%20 = load i8*, i8** @MAP_FAILED
%21 = icmp eq i8* %19, %20
br i1 %21, label %22, label %30
22: ; preds = %2
%23 = load i32, i32* @PROT_READ
%24 = load i32, i32* @PROT_WRITE
%25 = or i32 %23, %24
%26 = load i32, i32* @MAP_PRIVATE
%27 = load i32, i32* @MAP_ANONYMOUS
%28 = or i32 %26, %27
%29 = call i8* @mmap(i32* null, i32 4194304, i32 %25, i32 %28, i32 -1, i32 0)
store i8* %29, i8** %6
br label %30
30: ; preds = %22, %2
%31 = load i8*, i8** %6
%32 = load i8*, i8** @MAP_FAILED
%33 = icmp eq i8* %31, %32
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 3, i32* %3
br label %52
36: ; preds = %30
%37 = load i8*, i8** %6
store i8* %37, i8** %7
store i32 0, i32* %8
br label %38
38: ; preds = %48, %36
%39 = load i32, i32* %8
%40 = icmp slt i32 %39, 4194304
br i1 %40, label %41, label %51
41: ; preds = %38
%42 = load i32, i32* %8
%43 = trunc i32 %42 to i8
%44 = load i8*, i8** %7
%45 = load i32, i32* %8
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 %43, i8* %47
br label %48
48: ; preds = %41
%49 = load i32, i32* %8
%50 = add nsw i32 %49, 4096
store i32 %50, i32* %8
br label %38
51: ; preds = %38
store i32 0, i32* %3
br label %52
52: ; preds = %51, %34
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_HUGETLB = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
define i32 @main(i32 %0, i8** nocapture readnone %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_HUGETLB
%10 = or i32 %8, %9
%11 = tail call i8* @mmap(i32* null, i32 4194304, 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 %23
14: ; preds = %2
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %16, %15
%18 = load i32, i32* @MAP_PRIVATE
%19 = load i32, i32* @MAP_ANONYMOUS
%20 = or i32 %19, %18
%21 = tail call i8* @mmap(i32* null, i32 4194304, i32 %17, i32 %20, i32 -1, i32 0)
%22 = load i8*, i8** @MAP_FAILED
br label %23
23: ; preds = %14, %2
%24 = phi i8* [ %22, %14 ], [ %12, %2 ]
%25 = phi i8* [ %21, %14 ], [ %11, %2 ]
%26 = icmp eq i8* %25, %24
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %48
29: ; preds = %23, %29
%30 = phi i64 [ %46, %29 ], [ 0, %23 ]
%31 = getelementptr inbounds i8, i8* %25, i64 %30
store i8 0, i8* %31
%32 = or i64 %30, 4096
%33 = getelementptr inbounds i8, i8* %25, i64 %32
store i8 0, i8* %33
%34 = or i64 %30, 8192
%35 = getelementptr inbounds i8, i8* %25, i64 %34
store i8 0, i8* %35
%36 = or i64 %30, 12288
%37 = getelementptr inbounds i8, i8* %25, i64 %36
store i8 0, i8* %37
%38 = or i64 %30, 16384
%39 = getelementptr inbounds i8, i8* %25, i64 %38
store i8 0, i8* %39
%40 = or i64 %30, 20480
%41 = getelementptr inbounds i8, i8* %25, i64 %40
store i8 0, i8* %41
%42 = or i64 %30, 24576
%43 = getelementptr inbounds i8, i8* %25, i64 %42
store i8 0, i8* %43
%44 = or i64 %30, 28672
%45 = getelementptr inbounds i8, i8* %25, i64 %44
store i8 0, i8* %45
%46 = add nuw nsw i64 %30, 32768
%47 = icmp ult i64 %44, 4190208
br i1 %47, label %29, label %48
48: ; preds = %29, %27
%49 = phi i32 [ 3, %27 ], [ 0, %29 ]
ret i32 %49
}