#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;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int const);
char *mmap(int /*<<< orphan*/ *, int const, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int main(int argc, char **argv) {
  const int shared_mem_size = 50 << 20;
  const int linux_page_size = 4 << 10;
  char *p = mmap(NULL, shared_mem_size, PROT_READ | PROT_WRITE,
                 MAP_ANONYMOUS | MAP_SHARED, -1, 0);
  for (int i = 0; i < (shared_mem_size / linux_page_size); i++) {
    p[i * linux_page_size] = 1;
  }
  const int len_pure_heap = 16 << 20;
  char *p2 = (char *)malloc(len_pure_heap);
  for (int i = 0; i < len_pure_heap / linux_page_size; i++) {
    p2[i * linux_page_size] = 1;
  }
  sleep(10 * 60);
  free(p2);
  printf("---program exit---\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x3200000,-0x14(%rbp)
movl   $0x1000,-0x18(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%edx
mov    0x2e44(%rip),%rax
or     (%rax),%edx
mov    0x2e4b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e52(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x3200000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x3200,-0x24(%rbp)
jge    11e6 <main+0x96>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
shl    $0xc,%ecx
movslq %ecx,%rcx
movb   $0x1,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11ba <main+0x6a>
movl   $0x1000000,-0x28(%rbp)
mov    $0x1000000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x1000,-0x34(%rbp)
jge    122e <main+0xde>
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
shl    $0xc,%ecx
movslq %ecx,%rcx
movb   $0x1,(%rax,%rcx,1)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1202 <main+0xb2>
mov    $0x258,%edi
call   1070 <sleep@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
lea    0xdb8(%rip),%rdi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2ea0(%rip),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e6e(%rip),%rax
mov    0x2e77(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x3200000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    $0x4000,%ecx
nop
movb   $0x1,-0x4000(%rax,%rcx,1)
01
movb   $0x1,-0x3000(%rax,%rcx,1)
01
movb   $0x1,-0x2000(%rax,%rcx,1)
01
movb   $0x1,-0x1000(%rax,%rcx,1)
01
movb   $0x1,(%rax,%rcx,1)
add    $0x5000,%rcx
cmp    $0x3204000,%rcx
jne    1190 <main+0x40>
mov    $0x1000000,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    $0x3000,%eax
cs nopw 0x0(%rax,%rax,1)
movb   $0x1,-0x3000(%rbx,%rax,1)
01
movb   $0x1,-0x2000(%rbx,%rax,1)
01
movb   $0x1,-0x1000(%rbx,%rax,1)
01
movb   $0x1,(%rbx,%rax,1)
add    $0x4000,%rax
cmp    $0x1003000,%rax
jne    11e0 <main+0x90>
mov    $0x258,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xddd(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret

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

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_SHARED = global i32 0
@str = [19 x i8] c"---program exit---\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_ANONYMOUS
%7 = load i32, i32* @MAP_SHARED
%8 = or i32 %7, %6
%9 = tail call i8* @mmap(i32* null, i32 52428800, i32 %5, i32 %8, i32 -1, i32 0)
br label %13
10: ; preds = %13
%11 = tail call i64 @malloc(i32 16777216)
%12 = inttoptr i64 %11 to i8*
br label %35
13: ; preds = %13, %2
%14 = phi i64 [ 0, %2 ], [ %29, %13 ]
%15 = shl nuw nsw i64 %14, 12
%16 = getelementptr inbounds i8, i8* %9, i64 %15
store i8 1, i8* %16
%17 = shl i64 %14, 12
%18 = add i64 %17, 4096
%19 = getelementptr inbounds i8, i8* %9, i64 %18
store i8 1, i8* %19
%20 = shl i64 %14, 12
%21 = add i64 %20, 8192
%22 = getelementptr inbounds i8, i8* %9, i64 %21
store i8 1, i8* %22
%23 = shl i64 %14, 12
%24 = add i64 %23, 12288
%25 = getelementptr inbounds i8, i8* %9, i64 %24
store i8 1, i8* %25
%26 = shl i64 %14, 12
%27 = add i64 %26, 16384
%28 = getelementptr inbounds i8, i8* %9, i64 %27
store i8 1, i8* %28
%29 = add nuw nsw i64 %14, 5
%30 = icmp eq i64 %29, 12800
br i1 %30, label %10, label %13
31: ; preds = %35
%32 = tail call i32 @sleep(i32 600)
%33 = tail call i32 @free(i8* %12)
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
ret i32 0
35: ; preds = %35, %10
%36 = phi i64 [ 0, %10 ], [ %48, %35 ]
%37 = shl nuw nsw i64 %36, 12
%38 = getelementptr inbounds i8, i8* %12, i64 %37
store i8 1, i8* %38
%39 = shl i64 %36, 12
%40 = or i64 %39, 4096
%41 = getelementptr inbounds i8, i8* %12, i64 %40
store i8 1, i8* %41
%42 = shl i64 %36, 12
%43 = or i64 %42, 8192
%44 = getelementptr inbounds i8, i8* %12, i64 %43
store i8 1, i8* %44
%45 = shl i64 %36, 12
%46 = or i64 %45, 12288
%47 = getelementptr inbounds i8, i8* %12, i64 %46
store i8 1, i8* %47
%48 = add nuw nsw i64 %36, 4
%49 = icmp eq i64 %48, 4096
br i1 %49, label %31, label %35
}