#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_POPULATE;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int getpagesize();
int /*<<< orphan*/ madvise(char *, int, int);
char *mmap(int /*<<< orphan*/ *, int, int, int, int /*<<< orphan*/,
           int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main(void) {
  int PS = getpagesize();
  char *ptr = mmap(NULL, PS, PROT_READ | PROT_WRITE,
                   MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, 0, 0);
  ;
  printf("ptr = %p\n", ptr);
  madvise(ptr, PS, 100);
  printf("faulting\n");
  *ptr = 1;
  printf("waiting\n");
  sleep(100);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1060 <getpagesize@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
mov    0x2e85(%rip),%rax
mov    (%rax),%edx
mov    0x2e4c(%rip),%rax
or     (%rax),%edx
mov    0x2e3b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e52(%rip),%rax
or     (%rax),%ecx
mov    0x2e41(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
mov    %r9d,%r8d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%esi
mov    $0x64,%edx
call   1050 <madvise@plt>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
movb   $0x1,(%rax)
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x64,%edi
call   1070 <sleep@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1070 <getpagesize@plt>
mov    0x2e87(%rip),%rcx
mov    0x2e50(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2e3d(%rip),%rsi
mov    0x2e56(%rip),%rcx
mov    (%rcx),%ecx
or     (%rsi),%ecx
mov    %eax,%ebp
mov    0x2e41(%rip),%rax
or     (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%esi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
lea    0xe4e(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    $0x64,%edx
call   1060 <madvise@plt>
lea    0xe38(%rip),%rdi
call   1030 <puts@plt>
movb   $0x1,(%rbx)
lea    0xe32(%rip),%rdi
call   1030 <puts@plt>
mov    $0x64,%edi
call   1080 <sleep@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_POPULATE = global i32 0
@.str = [10 x i8] c"ptr = %p\0A\00"
@.str.1 = [10 x i8] c"faulting\0A\00"
@.str.2 = [9 x i8] c"waiting\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
store i32 0, i32* %1
%4 = call i32 (...) @getpagesize()
store i32 %4, i32* %2
%5 = load i32, i32* %2
%6 = load i32, i32* @PROT_READ
%7 = load i32, i32* @PROT_WRITE
%8 = or i32 %6, %7
%9 = load i32, i32* @MAP_PRIVATE
%10 = load i32, i32* @MAP_ANONYMOUS
%11 = or i32 %9, %10
%12 = load i32, i32* @MAP_POPULATE
%13 = or i32 %11, %12
%14 = call i8* @mmap(i32* null, i32 %5, i32 %8, i32 %13, i32 0, i32 0)
store i8* %14, i8** %3
%15 = load i8*, i8** %3
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i8*, i8** %3
%18 = load i32, i32* %2
%19 = call i32 @madvise(i8* %17, i32 %18, i32 100)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%21 = load i8*, i8** %3
store i8 1, i8* %21
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @sleep(i32 100)
ret i32 0
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_POPULATE = global i32 0
@.str = [10 x i8] c"ptr = %p\0A\00"
@str = [9 x i8] c"faulting\00"
@str.3 = [8 x i8] c"waiting\00"
define i32 @main() {
%1 = tail call i32 (...) @getpagesize()
%2 = load i32, i32* @PROT_READ
%3 = load i32, i32* @PROT_WRITE
%4 = or i32 %3, %2
%5 = load i32, i32* @MAP_PRIVATE
%6 = load i32, i32* @MAP_ANONYMOUS
%7 = or i32 %6, %5
%8 = load i32, i32* @MAP_POPULATE
%9 = or i32 %7, %8
%10 = tail call i8* @mmap(i32* null, i32 %1, i32 %4, i32 %9, i32 0, i32 0)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @madvise(i8* %10, i32 %1, i32 100)
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
store i8 1, i8* %10
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str.3, i64 0, i64 0))
%15 = tail call i32 @sleep(i32 100)
ret i32 0
}