#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int MAP_ANONYMOUS;
char *MAP_FAILED;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ mmap(int /*<<< orphan*/ *, size_t, int, int, int,
                int /*<<< orphan*/);
int munmap(char *, size_t);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
void mmapAnonShared() {
  size_t mmapLen = 8;
  char *ptr = (char *)mmap(NULL, mmapLen, PROT_READ | PROT_WRITE,
                           MAP_ANONYMOUS | MAP_SHARED, -1, 0);
  if (ptr == MAP_FAILED) {
    perror("mmap");
    exit(EXIT_FAILURE);
  }
  printf("memory is initialized to zero with MAP_ANONYMOUS\n");
  for (unsigned int i = 0; i < mmapLen; i++)
    printf("ptr[%u] = %d\n", i, ptr[i]);
  if (fork() == 0) {
    for (unsigned int i = 0; i < mmapLen; i++)
      printf("child: ptr[%u] = %d\n", i, ptr[i]);
    printf(
        "Child is putting non-zero data at the beginning of the mmap'd area\n");
    ptr[0] = 42;
    printf("Child is waiting for non-zero data at the end of the mmap'd "
           "area...\n");
    while (ptr[mmapLen - 1] == 0) {
      sleep(1);
    };
    printf("Child is done waiting.\n");
    for (unsigned int i = 0; i < mmapLen; i++)
      printf("child ptr[%u] = %d\n", i, ptr[i]);
    printf("Child exited.\n");
    exit(EXIT_SUCCESS);
  }
  printf("Parent is waiting for non-zero data in the beginning of the mmap'd "
         "area.\n");
  while (ptr[0] != 42) {
    sleep(1);
  };
  printf("Parent is done waiting, writing data into entire mmap'd area...\n");
  for (unsigned int i = 0; i < mmapLen; i++)
    ptr[i] = i;
  printf("Parent is done writing data\n");
  printf("Parent is waiting for the child to finish\n");
  wait(NULL);
  printf("Parent detected that child exited.\n");
  printf("Contents after we store values in memory.\n");
  for (unsigned int i = 0; i < mmapLen; i++)
    printf("ptr[%u] = %d\n", i, ptr[i]);
  if (munmap(ptr, mmapLen) == -1) {
    perror("munmap");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<mmapAnonShared>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x8,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
mov    0x2e5d(%rip),%rax
mov    (%rax),%edx
mov    0x2e1c(%rip),%rax
or     (%rax),%edx
mov    0x2e2b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e32(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2dd7(%rip),%rcx
cmp    (%rcx),%rax
jne    11fc <mmapAnonShared+0x7c>
lea    0xe17(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%rax
jae    1248 <mmapAnonShared+0xc8>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
movsbl (%rax,%rcx,1),%edx
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1211 <mmapAnonShared+0x91>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    134a <mmapAnonShared+0x1ca>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%rax
jae    1297 <mmapAnonShared+0x117>
mov    -0x18(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
movsbl (%rax,%rcx,1),%edx
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1260 <mmapAnonShared+0xe0>
lea    0xdae(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
movb   $0x2a,(%rax)
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    12e2 <mmapAnonShared+0x162>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    12ba <mmapAnonShared+0x13a>
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%rax
jae    132e <mmapAnonShared+0x1ae>
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
movsbl (%rax,%rcx,1),%edx
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12f7 <mmapAnonShared+0x177>
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2c75(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2a,%eax
je     1377 <mmapAnonShared+0x1f7>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    1358 <mmapAnonShared+0x1d8>
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x8(%rbp),%rax
jae    13b6 <mmapAnonShared+0x236>
mov    -0x20(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    138c <mmapAnonShared+0x20c>
lea    0xdde(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x8(%rbp),%rax
jae    1435 <mmapAnonShared+0x2b5>
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    -0x24(%rbp),%ecx
movsbl (%rax,%rcx,1),%edx
lea    0xc1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    13fe <mmapAnonShared+0x27e>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <munmap@plt>
cmp    $0xffffffff,%eax
jne    1465 <mmapAnonShared+0x2e5>
lea    0xde0(%rip),%rdi
call   1060 <perror@plt>
mov    0x2b6a(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmapAnonShared>:
push   %r14
push   %rbx
push   %rax
mov    0x2e5d(%rip),%rax
mov    0x2e1e(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e2b(%rip),%rax
mov    0x2e34(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    $0x8,%esi
xor    %edi,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2dd9(%rip),%rax
cmp    %rbx,(%rax)
je     13c8 <mmapAnonShared+0x238>
lea    0xe4e(%rip),%rdi
call   1030 <puts@plt>
movsbl (%rbx),%edx
lea    0xe16(%rip),%r14
mov    %r14,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x1(%rbx),%edx
mov    %r14,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x2(%rbx),%edx
mov    %r14,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x3(%rbx),%edx
mov    %r14,%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x4(%rbx),%edx
mov    %r14,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x5(%rbx),%edx
mov    %r14,%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x6(%rbx),%edx
mov    %r14,%rdi
mov    $0x6,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x7(%rbx),%edx
mov    %r14,%rdi
mov    $0x7,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     13d4 <mmapAnonShared+0x244>
lea    0xdc8(%rip),%rdi
call   1030 <puts@plt>
cmpb   $0x2a,(%rbx)
je     12bf <mmapAnonShared+0x12f>
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   1090 <sleep@plt>
cmpb   $0x2a,(%rbx)
jne    12b0 <mmapAnonShared+0x120>
lea    0xde9(%rip),%rdi
call   1030 <puts@plt>
movabs $0x706050403020100,%rax
05 06 07
mov    %rax,(%rbx)
lea    0xe10(%rip),%rdi
call   1030 <puts@plt>
lea    0xe20(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
lea    0xe37(%rip),%rdi
call   1030 <puts@plt>
lea    0xe4e(%rip),%rdi
call   1030 <puts@plt>
movsbl (%rbx),%edx
lea    0xcf3(%rip),%r14
mov    %r14,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x1(%rbx),%edx
mov    %r14,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x2(%rbx),%edx
mov    %r14,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x3(%rbx),%edx
mov    %r14,%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x4(%rbx),%edx
mov    %r14,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x5(%rbx),%edx
mov    %r14,%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x6(%rbx),%edx
mov    %r14,%rdi
mov    $0x6,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x7(%rbx),%edx
mov    %r14,%rdi
mov    $0x7,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x8,%esi
mov    %rbx,%rdi
call   1060 <munmap@plt>
cmp    $0xffffffff,%eax
je     1561 <mmapAnonShared+0x3d1>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xc31(%rip),%rdi
jmp    1568 <mmapAnonShared+0x3d8>
movsbl (%rbx),%edx
lea    0xc27(%rip),%r14
mov    %r14,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x1(%rbx),%edx
mov    %r14,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x2(%rbx),%edx
mov    %r14,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x3(%rbx),%edx
mov    %r14,%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x4(%rbx),%edx
mov    %r14,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x5(%rbx),%edx
mov    %r14,%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x6(%rbx),%edx
mov    %r14,%rdi
mov    $0x6,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x7(%rbx),%edx
mov    %r14,%rdi
mov    $0x7,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd0c(%rip),%rdi
call   1030 <puts@plt>
movb   $0x2a,(%rbx)
lea    0xd40(%rip),%rdi
call   1030 <puts@plt>
cmpb   $0x0,0x7(%rbx)
jne    14a0 <mmapAnonShared+0x310>
mov    $0x1,%edi
call   1090 <sleep@plt>
cmpb   $0x0,0x7(%rbx)
je     1490 <mmapAnonShared+0x300>
lea    0xd62(%rip),%rdi
call   1030 <puts@plt>
movsbl (%rbx),%edx
lea    0xb64(%rip),%r14
mov    %r14,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x1(%rbx),%edx
mov    %r14,%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x2(%rbx),%edx
mov    %r14,%rdi
mov    $0x2,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x3(%rbx),%edx
mov    %r14,%rdi
mov    $0x3,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x4(%rbx),%edx
mov    %r14,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x5(%rbx),%edx
mov    %r14,%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x6(%rbx),%edx
mov    %r14,%rdi
mov    $0x6,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movsbl 0x7(%rbx),%edx
mov    %r14,%rdi
mov    $0x7,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xcd2(%rip),%rdi
call   1030 <puts@plt>
mov    0x2a5e(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
lea    0xac6(%rip),%rdi
call   1070 <perror@plt>
mov    0x2a54(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [50 x i8] c"memory is initialized to zero with MAP_ANONYMOUS\0A\00"
@.str.2 = [14 x i8] c"ptr[%u] = %d\0A\00"
@.str.3 = [21 x i8] c"child: ptr[%u] = %d\0A\00"
@.str.4 = [68 x i8] c"Child is putting non-zero data at the beginning of the mmap'd area\0A\00"
@.str.5 = [69 x i8] c"Child is waiting for non-zero data at the end of the mmap'd area...\0A\00"
@.str.6 = [24 x i8] c"Child is done waiting.\0A\00"
@.str.7 = [20 x i8] c"child ptr[%u] = %d\0A\00"
@.str.8 = [15 x i8] c"Child exited.\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.9 = [74 x i8] c"Parent is waiting for non-zero data in the beginning of the mmap'd area.\0A\00"
@.str.10 = [65 x i8] c"Parent is done waiting, writing data into entire mmap'd area...\0A\00"
@.str.11 = [29 x i8] c"Parent is done writing data\0A\00"
@.str.12 = [43 x i8] c"Parent is waiting for the child to finish\0A\00"
@.str.13 = [36 x i8] c"Parent detected that child exited.\0A\00"
@.str.14 = [43 x i8] c"Contents after we store values in memory.\0A\00"
@.str.15 = [7 x i8] c"munmap\00"
define void @mmapAnonShared() {
%1 = alloca i64
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i64 8, i64* %1
%8 = load i64, i64* %1
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %9, %10
%12 = load i32, i32* @MAP_ANONYMOUS
%13 = load i32, i32* @MAP_SHARED
%14 = or i32 %12, %13
%15 = call i64 @mmap(i32* null, i64 %8, i32 %11, i32 %14, i32 -1, i32 0)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %2
%17 = load i8*, i8** %2
%18 = load i8*, i8** @MAP_FAILED
%19 = icmp eq i8* %17, %18
br i1 %19, label %20, label %24
20: ; preds = %0
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %0
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %26
26: ; preds = %40, %24
%27 = load i32, i32* %3
%28 = zext i32 %27 to i64
%29 = load i64, i64* %1
%30 = icmp ult i64 %28, %29
br i1 %30, label %31, label %43
31: ; preds = %26
%32 = load i32, i32* %3
%33 = load i8*, i8** %2
%34 = load i32, i32* %3
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %32, i32 %38)
br label %40
40: ; preds = %31
%41 = load i32, i32* %3
%42 = add i32 %41, 1
store i32 %42, i32* %3
br label %26
43: ; preds = %26
%44 = call i64 (...) @fork()
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %102
46: ; preds = %43
store i32 0, i32* %4
br label %47
47: ; preds = %61, %46
%48 = load i32, i32* %4
%49 = zext i32 %48 to i64
%50 = load i64, i64* %1
%51 = icmp ult i64 %49, %50
br i1 %51, label %52, label %64
52: ; preds = %47
%53 = load i32, i32* %4
%54 = load i8*, i8** %2
%55 = load i32, i32* %4
%56 = zext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %53, i32 %59)
br label %61
61: ; preds = %52
%62 = load i32, i32* %4
%63 = add i32 %62, 1
store i32 %63, i32* %4
br label %47
64: ; preds = %47
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.4, i64 0, i64 0))
%66 = load i8*, i8** %2
%67 = getelementptr inbounds i8, i8* %66, i64 0
store i8 42, i8* %67
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str.5, i64 0, i64 0))
br label %69
69: ; preds = %77, %64
%70 = load i8*, i8** %2
%71 = load i64, i64* %1
%72 = sub i64 %71, 1
%73 = getelementptr inbounds i8, i8* %70, i64 %72
%74 = load i8, i8* %73
%75 = sext i8 %74 to i32
%76 = icmp eq i32 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %69
%78 = call i32 @sleep(i32 1)
br label %69
79: ; preds = %69
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %5
br label %81
81: ; preds = %95, %79
%82 = load i32, i32* %5
%83 = zext i32 %82 to i64
%84 = load i64, i64* %1
%85 = icmp ult i64 %83, %84
br i1 %85, label %86, label %98
86: ; preds = %81
%87 = load i32, i32* %5
%88 = load i8*, i8** %2
%89 = load i32, i32* %5
%90 = zext i32 %89 to i64
%91 = getelementptr inbounds i8, i8* %88, i64 %90
%92 = load i8, i8* %91
%93 = sext i8 %92 to i32
%94 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 %87, i32 %93)
br label %95
95: ; preds = %86
%96 = load i32, i32* %5
%97 = add i32 %96, 1
store i32 %97, i32* %5
br label %81
98: ; preds = %81
%99 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0))
%100 = load i32, i32* @EXIT_SUCCESS
%101 = call i32 @exit(i32 %100)
unreachable
102: ; preds = %43
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str.9, i64 0, i64 0))
br label %104
104: ; preds = %110, %102
%105 = load i8*, i8** %2
%106 = getelementptr inbounds i8, i8* %105, i64 0
%107 = load i8, i8* %106
%108 = sext i8 %107 to i32
%109 = icmp ne i32 %108, 42
br i1 %109, label %110, label %112
110: ; preds = %104
%111 = call i32 @sleep(i32 1)
br label %104
112: ; preds = %104
%113 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.10, i64 0, i64 0))
store i32 0, i32* %6
br label %114
114: ; preds = %126, %112
%115 = load i32, i32* %6
%116 = zext i32 %115 to i64
%117 = load i64, i64* %1
%118 = icmp ult i64 %116, %117
br i1 %118, label %119, label %129
119: ; preds = %114
%120 = load i32, i32* %6
%121 = trunc i32 %120 to i8
%122 = load i8*, i8** %2
%123 = load i32, i32* %6
%124 = zext i32 %123 to i64
%125 = getelementptr inbounds i8, i8* %122, i64 %124
store i8 %121, i8* %125
br label %126
126: ; preds = %119
%127 = load i32, i32* %6
%128 = add i32 %127, 1
store i32 %128, i32* %6
br label %114
129: ; preds = %114
%130 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.11, i64 0, i64 0))
%131 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.12, i64 0, i64 0))
%132 = call i32 @wait(i32* null)
%133 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.13, i64 0, i64 0))
%134 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.14, i64 0, i64 0))
store i32 0, i32* %7
br label %135
135: ; preds = %149, %129
%136 = load i32, i32* %7
%137 = zext i32 %136 to i64
%138 = load i64, i64* %1
%139 = icmp ult i64 %137, %138
br i1 %139, label %140, label %152
140: ; preds = %135
%141 = load i32, i32* %7
%142 = load i8*, i8** %2
%143 = load i32, i32* %7
%144 = zext i32 %143 to i64
%145 = getelementptr inbounds i8, i8* %142, i64 %144
%146 = load i8, i8* %145
%147 = sext i8 %146 to i32
%148 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %141, i32 %147)
br label %149
149: ; preds = %140
%150 = load i32, i32* %7
%151 = add i32 %150, 1
store i32 %151, i32* %7
br label %135
152: ; preds = %135
%153 = load i8*, i8** %2
%154 = load i64, i64* %1
%155 = call i32 @munmap(i8* %153, i64 %154)
%156 = icmp eq i32 %155, -1
br i1 %156, label %157, label %161
157: ; preds = %152
%158 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.15, i64 0, i64 0))
%159 = load i32, i32* @EXIT_FAILURE
%160 = call i32 @exit(i32 %159)
unreachable
161: ; preds = %152
ret void
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [14 x i8] c"ptr[%u] = %d\0A\00"
@.str.3 = [21 x i8] c"child: ptr[%u] = %d\0A\00"
@.str.7 = [20 x i8] c"child ptr[%u] = %d\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.15 = [7 x i8] c"munmap\00"
@str = [49 x i8] c"memory is initialized to zero with MAP_ANONYMOUS\00"
@str.16 = [73 x i8] c"Parent is waiting for non-zero data in the beginning of the mmap'd area.\00"
@str.17 = [64 x i8] c"Parent is done waiting, writing data into entire mmap'd area...\00"
@str.18 = [28 x i8] c"Parent is done writing data\00"
@str.19 = [42 x i8] c"Parent is waiting for the child to finish\00"
@str.20 = [35 x i8] c"Parent detected that child exited.\00"
@str.21 = [42 x i8] c"Contents after we store values in memory.\00"
@str.22 = [67 x i8] c"Child is putting non-zero data at the beginning of the mmap'd area\00"
@str.23 = [68 x i8] c"Child is waiting for non-zero data at the end of the mmap'd area...\00"
@str.24 = [23 x i8] c"Child is done waiting.\00"
@str.25 = [14 x i8] c"Child exited.\00"
define void @mmapAnonShared() {
%1 = load i32, i32* @PROT_READ
%2 = load i32, i32* @PROT_WRITE
%3 = or i32 %2, %1
%4 = load i32, i32* @MAP_ANONYMOUS
%5 = load i32, i32* @MAP_SHARED
%6 = or i32 %5, %4
%7 = tail call i64 @mmap(i32* null, i64 8, i32 %3, i32 %6, i32 -1, i32 0)
%8 = inttoptr i64 %7 to i8*
%9 = load i8*, i8** @MAP_FAILED
%10 = icmp eq i8* %9, %8
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %0
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str, i64 0, i64 0))
%17 = load i8, i8* %8
%18 = sext i8 %17 to i32
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 0, i32 %18)
%20 = getelementptr inbounds i8, i8* %8, i64 1
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 %22)
%24 = getelementptr inbounds i8, i8* %8, i64 2
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 2, i32 %26)
%28 = getelementptr inbounds i8, i8* %8, i64 3
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 3, i32 %30)
%32 = getelementptr inbounds i8, i8* %8, i64 4
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 4, i32 %34)
%36 = getelementptr inbounds i8, i8* %8, i64 5
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 5, i32 %38)
%40 = getelementptr inbounds i8, i8* %8, i64 6
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 6, i32 %42)
%44 = getelementptr inbounds i8, i8* %8, i64 7
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 7, i32 %46)
%48 = tail call i64 (...) @fork()
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %112
50: ; preds = %15
%51 = load i8, i8* %8
%52 = sext i8 %51 to i32
%53 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 0, i32 %52)
%54 = load i8, i8* %20
%55 = sext i8 %54 to i32
%56 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 %55)
%57 = load i8, i8* %24
%58 = sext i8 %57 to i32
%59 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 2, i32 %58)
%60 = load i8, i8* %28
%61 = sext i8 %60 to i32
%62 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 3, i32 %61)
%63 = load i8, i8* %32
%64 = sext i8 %63 to i32
%65 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 4, i32 %64)
%66 = load i8, i8* %36
%67 = sext i8 %66 to i32
%68 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 5, i32 %67)
%69 = load i8, i8* %40
%70 = sext i8 %69 to i32
%71 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 6, i32 %70)
%72 = load i8, i8* %44
%73 = sext i8 %72 to i32
%74 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 7, i32 %73)
%75 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([67 x i8], [67 x i8]* @str.22, i64 0, i64 0))
store i8 42, i8* %8
%76 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([68 x i8], [68 x i8]* @str.23, i64 0, i64 0))
%77 = load i8, i8* %44
%78 = icmp eq i8 %77, 0
br i1 %78, label %79, label %83
79: ; preds = %50, %79
%80 = tail call i32 @sleep(i32 1)
%81 = load i8, i8* %44
%82 = icmp eq i8 %81, 0
br i1 %82, label %79, label %83
83: ; preds = %79, %50
%84 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.24, i64 0, i64 0))
%85 = load i8, i8* %8
%86 = sext i8 %85 to i32
%87 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 0, i32 %86)
%88 = load i8, i8* %20
%89 = sext i8 %88 to i32
%90 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 1, i32 %89)
%91 = load i8, i8* %24
%92 = sext i8 %91 to i32
%93 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 2, i32 %92)
%94 = load i8, i8* %28
%95 = sext i8 %94 to i32
%96 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 3, i32 %95)
%97 = load i8, i8* %32
%98 = sext i8 %97 to i32
%99 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 4, i32 %98)
%100 = load i8, i8* %36
%101 = sext i8 %100 to i32
%102 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 5, i32 %101)
%103 = load i8, i8* %40
%104 = sext i8 %103 to i32
%105 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 6, i32 %104)
%106 = load i8, i8* %44
%107 = sext i8 %106 to i32
%108 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.7, i64 0, i64 0), i32 7, i32 %107)
%109 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.25, i64 0, i64 0))
%110 = load i32, i32* @EXIT_SUCCESS
%111 = tail call i32 @exit(i32 %110)
unreachable
112: ; preds = %15
%113 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([73 x i8], [73 x i8]* @str.16, i64 0, i64 0))
%114 = load i8, i8* %8
%115 = icmp eq i8 %114, 42
br i1 %115, label %120, label %116
116: ; preds = %112, %116
%117 = tail call i32 @sleep(i32 1)
%118 = load i8, i8* %8
%119 = icmp eq i8 %118, 42
br i1 %119, label %120, label %116
120: ; preds = %116, %112
%121 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @str.17, i64 0, i64 0))
store i8 0, i8* %8
store i8 1, i8* %20
store i8 2, i8* %24
store i8 3, i8* %28
store i8 4, i8* %32
store i8 5, i8* %36
store i8 6, i8* %40
store i8 7, i8* %44
%122 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.18, i64 0, i64 0))
%123 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str.19, i64 0, i64 0))
%124 = tail call i32 @wait(i32* null)
%125 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.20, i64 0, i64 0))
%126 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str.21, i64 0, i64 0))
%127 = load i8, i8* %8
%128 = sext i8 %127 to i32
%129 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 0, i32 %128)
%130 = load i8, i8* %20
%131 = sext i8 %130 to i32
%132 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 %131)
%133 = load i8, i8* %24
%134 = sext i8 %133 to i32
%135 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 2, i32 %134)
%136 = load i8, i8* %28
%137 = sext i8 %136 to i32
%138 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 3, i32 %137)
%139 = load i8, i8* %32
%140 = sext i8 %139 to i32
%141 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 4, i32 %140)
%142 = load i8, i8* %36
%143 = sext i8 %142 to i32
%144 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 5, i32 %143)
%145 = load i8, i8* %40
%146 = sext i8 %145 to i32
%147 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 6, i32 %146)
%148 = load i8, i8* %44
%149 = sext i8 %148 to i32
%150 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 7, i32 %149)
%151 = tail call i32 @munmap(i8* %8, i64 8)
%152 = icmp eq i32 %151, -1
br i1 %152, label %153, label %157
153: ; preds = %120
%154 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.15, i64 0, i64 0))
%155 = load i32, i32* @EXIT_FAILURE
%156 = tail call i32 @exit(i32 %155)
unreachable
157: ; preds = %120
ret void
}