#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__ pid_t;

/* Variables and functions */
int MAP_ANON;
scalar_t__ MAP_FAILED;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
size_t getpagesize();
char *mmap(int /*<<< orphan*/ *, size_t, int, int, int, int /*<<< orphan*/);
scalar_t__ munmap(char *, size_t);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ wait_for_child_exit(scalar_t__, int /*<<< orphan*/);
int test_sharing_memory_child_and_grandchild(void) {
  size_t pgsz = getpagesize();
  char *map =
      mmap(NULL, pgsz, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
  assert(map != (char *)MAP_FAILED);
  pid_t pid = fork();
  assert(pid >= 0);
  if (pid == 0) {
    pid = fork();
    assert(pid >= 0);
    if (pid == 0) {
      strcpy(map, "Hello from grandchild!");
      exit(0);
    }
    wait_for_child_exit(pid, 0);
    assert(strcmp(map, "Hello from grandchild!") == 0);
    strcpy(map, "Hello from child!");
    exit(0);
  }
  wait_for_child_exit(pid, 0);
  assert(strcmp(map, "Hello from child!") == 0);
  assert(munmap(map, pgsz) == 0);
  return 0;
}


x86 assembly (clang, -O0):
<test_sharing_memory_child_and_grandchild>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1090 <getpagesize@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rsi
mov    0x2e4a(%rip),%rax
mov    (%rax),%edx
mov    0x2e19(%rip),%rax
or     (%rax),%edx
mov    0x2e08(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1f(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2dd4(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
jne    12a9 <test_sharing_memory_child_and_grandchild+0x119>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1266 <test_sharing_memory_child_and_grandchild+0xd6>
mov    -0x10(%rbp),%rdi
lea    0xda6(%rip),%rsi
call   1030 <strcpy@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1040 <wait_for_child_exit@plt>
mov    -0x10(%rbp),%rdi
lea    0xd84(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%rdi
lea    0xd7a(%rip),%rsi
call   1030 <strcpy@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1040 <wait_for_child_exit@plt>
mov    -0x10(%rbp),%rdi
lea    0xd58(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <munmap@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_sharing_memory_child_and_grandchild>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   1090 <getpagesize@plt>
mov    %rax,%r14
mov    0x2e52(%rip),%rax
mov    0x2e23(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e10(%rip),%rax
mov    0x2e29(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %r14,%rsi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%r15
mov    0x2de0(%rip),%rax
xor    %edi,%edi
cmp    (%rax),%r15
setne  %dil
call   1070 <assert@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %rax,%rbx
mov    %rax,%rdi
shr    $0x3f,%rdi
xor    $0x1,%edi
call   1070 <assert@plt>
test   %rbx,%rbx
je     1254 <test_sharing_memory_child_and_grandchild+0xc4>
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <wait_for_child_exit@plt>
lea    0xdfa(%rip),%rsi
mov    %r15,%rdi
call   1060 <strcmp@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1080 <munmap@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %rax,%rbx
mov    %rax,%rdi
shr    $0x3f,%rdi
xor    $0x1,%edi
call   1070 <assert@plt>
test   %rbx,%rbx
jne    127b <test_sharing_memory_child_and_grandchild+0xeb>
lea    0xd87(%rip),%rsi
jmp    12a9 <test_sharing_memory_child_and_grandchild+0x119>
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <wait_for_child_exit@plt>
lea    0xd74(%rip),%rsi
mov    %r15,%rdi
call   1060 <strcmp@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1070 <assert@plt>
lea    0xd6e(%rip),%rsi
mov    %r15,%rdi
call   1030 <strcpy@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANON = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i64 0
@.str = [23 x i8] c"Hello from grandchild!\00"
@.str.1 = [18 x i8] c"Hello from child!\00"
define i32 @test_sharing_memory_child_and_grandchild() {
%1 = alloca i64
%2 = alloca i8*
%3 = alloca i64
%4 = call i64 (...) @getpagesize()
store i64 %4, i64* %1
%5 = load i64, i64* %1
%6 = load i32, i32* @PROT_READ
%7 = load i32, i32* @PROT_WRITE
%8 = or i32 %6, %7
%9 = load i32, i32* @MAP_ANON
%10 = load i32, i32* @MAP_SHARED
%11 = or i32 %9, %10
%12 = call i8* @mmap(i32* null, i64 %5, i32 %8, i32 %11, i32 -1, i32 0)
store i8* %12, i8** %2
%13 = load i8*, i8** %2
%14 = load i64, i64* @MAP_FAILED
%15 = inttoptr i64 %14 to i8*
%16 = icmp ne i8* %13, %15
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = call i64 (...) @fork()
store i64 %19, i64* %3
%20 = load i64, i64* %3
%21 = icmp sge i64 %20, 0
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = load i64, i64* %3
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %49
26: ; preds = %0
%27 = call i64 (...) @fork()
store i64 %27, i64* %3
%28 = load i64, i64* %3
%29 = icmp sge i64 %28, 0
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = load i64, i64* %3
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %26
%35 = load i8*, i8** %2
%36 = call i32 @strcpy(i8* %35, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%37 = call i32 @exit(i32 0)
unreachable
38: ; preds = %26
%39 = load i64, i64* %3
%40 = call i32 @wait_for_child_exit(i64 %39, i32 0)
%41 = load i8*, i8** %2
%42 = call i64 @strcmp(i8* %41, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%43 = icmp eq i64 %42, 0
%44 = zext i1 %43 to i32
%45 = call i32 @assert(i32 %44)
%46 = load i8*, i8** %2
%47 = call i32 @strcpy(i8* %46, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%48 = call i32 @exit(i32 0)
unreachable
49: ; preds = %0
%50 = load i64, i64* %3
%51 = call i32 @wait_for_child_exit(i64 %50, i32 0)
%52 = load i8*, i8** %2
%53 = call i64 @strcmp(i8* %52, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%54 = icmp eq i64 %53, 0
%55 = zext i1 %54 to i32
%56 = call i32 @assert(i32 %55)
%57 = load i8*, i8** %2
%58 = load i64, i64* %1
%59 = call i64 @munmap(i8* %57, i64 %58)
%60 = icmp eq i64 %59, 0
%61 = zext i1 %60 to i32
%62 = call i32 @assert(i32 %61)
ret i32 0
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANON = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i64 0
@.str = [23 x i8] c"Hello from grandchild!\00"
@.str.1 = [18 x i8] c"Hello from child!\00"
define i32 @test_sharing_memory_child_and_grandchild() {
%1 = tail call i64 (...) @getpagesize()
%2 = load i32, i32* @PROT_READ
%3 = load i32, i32* @PROT_WRITE
%4 = or i32 %3, %2
%5 = load i32, i32* @MAP_ANON
%6 = load i32, i32* @MAP_SHARED
%7 = or i32 %6, %5
%8 = tail call i8* @mmap(i32* null, i64 %1, i32 %4, i32 %7, i32 -1, i32 0)
%9 = load i64, i64* @MAP_FAILED
%10 = inttoptr i64 %9 to i8*
%11 = icmp ne i8* %8, %10
%12 = zext i1 %11 to i32
%13 = tail call i32 @assert(i32 %12)
%14 = tail call i64 (...) @fork()
%15 = lshr i64 %14, 63
%16 = trunc i64 %15 to i32
%17 = xor i32 %16, 1
%18 = tail call i32 @assert(i32 %17)
%19 = icmp eq i64 %14, 0
br i1 %19, label %20, label %38
20: ; preds = %0
%21 = tail call i64 (...) @fork()
%22 = lshr i64 %21, 63
%23 = trunc i64 %22 to i32
%24 = xor i32 %23, 1
%25 = tail call i32 @assert(i32 %24)
%26 = icmp eq i64 %21, 0
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = tail call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%29 = tail call i32 @exit(i32 0)
unreachable
30: ; preds = %20
%31 = tail call i32 @wait_for_child_exit(i64 %21, i32 0)
%32 = tail call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%33 = icmp eq i64 %32, 0
%34 = zext i1 %33 to i32
%35 = tail call i32 @assert(i32 %34)
%36 = tail call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%37 = tail call i32 @exit(i32 0)
unreachable
38: ; preds = %0
%39 = tail call i32 @wait_for_child_exit(i64 %14, i32 0)
%40 = tail call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%41 = icmp eq i64 %40, 0
%42 = zext i1 %41 to i32
%43 = tail call i32 @assert(i32 %42)
%44 = tail call i64 @munmap(i8* %8, i64 %1)
%45 = icmp eq i64 %44, 0
%46 = zext i1 %45 to i32
%47 = tail call i32 @assert(i32 %46)
ret i32 0
}