#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_simple(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) {
    strcpy(map, "Hello, World!");
    exit(0);
  }
  wait_for_child_exit(pid, 0);
  assert(strcmp(map, "Hello, World!") == 0);
  assert(munmap(map, pgsz) == 0);
  return 0;
}


x86 assembly (clang, -O0):
<test_sharing_memory_simple>:
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    123e <test_sharing_memory_simple+0xae>
mov    -0x10(%rbp),%rdi
lea    0xdce(%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    0xdac(%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_simple>:
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_simple+0xc4>
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <wait_for_child_exit@plt>
lea    0xde3(%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
lea    0xda5(%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 = [14 x i8] c"Hello, World!\00"
define i32 @test_sharing_memory_simple() {
%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 %30
26: ; preds = %0
%27 = load i8*, i8** %2
%28 = call i32 @strcpy(i8* %27, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %0
%31 = load i64, i64* %3
%32 = call i32 @wait_for_child_exit(i64 %31, i32 0)
%33 = load i8*, i8** %2
%34 = call i64 @strcmp(i8* %33, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
%36 = zext i1 %35 to i32
%37 = call i32 @assert(i32 %36)
%38 = load i8*, i8** %2
%39 = load i64, i64* %1
%40 = call i64 @munmap(i8* %38, i64 %39)
%41 = icmp eq i64 %40, 0
%42 = zext i1 %41 to i32
%43 = call i32 @assert(i32 %42)
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 = [14 x i8] c"Hello, World!\00"
define i32 @test_sharing_memory_simple() {
%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 %23
20: ; preds = %0
%21 = tail call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 0)
unreachable
23: ; preds = %0
%24 = tail call i32 @wait_for_child_exit(i64 %14, i32 0)
%25 = tail call i64 @strcmp(i8* %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
%27 = zext i1 %26 to i32
%28 = tail call i32 @assert(i32 %27)
%29 = tail call i64 @munmap(i8* %8, i64 %1)
%30 = icmp eq i64 %29, 0
%31 = zext i1 %30 to i32
%32 = tail call i32 @assert(i32 %31)
ret i32 0
}