#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int MAP_ANONYMOUS;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
char *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ wait(int *);
int main(int argc, char **argv) {
  int fd = -1, size = 4096, status;
  char *area;
  pid_t pid;
  area = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS,
              fd, 0);
  pid = fork();
  if (pid == 0) {
    strcpy(area, "This is a message from the child");
    printf("Child has written: %s\n", area);
    exit(EXIT_SUCCESS);
  }
  if (pid > 0) {
    wait(&status);
    printf("Parent has read:   %s\n", area);
    exit(EXIT_SUCCESS);
  }
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0x1000,-0x18(%rbp)
mov    -0x18(%rbp),%esi
mov    0x2e6a(%rip),%rax
mov    (%rax),%edx
mov    0x2e29(%rip),%rax
or     (%rax),%edx
mov    0x2e48(%rip),%rax
mov    (%rax),%ecx
mov    0x2e2f(%rip),%rax
or     (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1203 <main+0xa3>
mov    -0x28(%rbp),%rdi
lea    0xe20(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rsi
lea    0xe31(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
cmpq   $0x0,-0x30(%rbp)
jle    1235 <main+0xd5>
lea    -0x1c(%rbp),%rdi
call   1070 <wait@plt>
mov    -0x28(%rbp),%rsi
lea    0xe16(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e8c(%rip),%rax
mov    0x2e4d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e6a(%rip),%rax
mov    0x2e53(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
jne    11c5 <main+0x65>
lea    0xe4c(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
lea    0xe5e(%rip),%rdi
jmp    11d8 <main+0x78>
jle    11ee <main+0x8e>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
lea    0xe60(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str = [33 x i8] c"This is a message from the child\00"
@.str.1 = [23 x i8] c"Child has written: %s\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [23 x i8] c"Parent has read: %s\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 4096, i32* %7
%11 = load i32, i32* %7
%12 = load i32, i32* @PROT_READ
%13 = load i32, i32* @PROT_WRITE
%14 = or i32 %12, %13
%15 = load i32, i32* @MAP_SHARED
%16 = load i32, i32* @MAP_ANONYMOUS
%17 = or i32 %15, %16
%18 = load i32, i32* %6
%19 = call i8* @mmap(i32* null, i32 %11, i32 %14, i32 %17, i32 %18, i32 0)
store i8* %19, i8** %9
%20 = call i64 (...) @fork()
store i64 %20, i64* %10
%21 = load i64, i64* %10
%22 = icmp eq i64 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %2
%24 = load i8*, i8** %9
%25 = call i32 @strcpy(i8* %24, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%26 = load i8*, i8** %9
%27 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = load i32, i32* @EXIT_SUCCESS
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %2
%31 = load i64, i64* %10
%32 = icmp sgt i64 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %30
%34 = call i32 @wait(i32* %8)
%35 = load i8*, i8** %9
%36 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = load i32, i32* @EXIT_SUCCESS
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %30
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str = [33 x i8] c"This is a message from the child\00"
@.str.1 = [23 x i8] c"Child has written: %s\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.2 = [23 x i8] c"Parent has read: %s\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i32, i32* @PROT_READ
%6 = load i32, i32* @PROT_WRITE
%7 = or i32 %6, %5
%8 = load i32, i32* @MAP_SHARED
%9 = load i32, i32* @MAP_ANONYMOUS
%10 = or i32 %9, %8
%11 = tail call i8* @mmap(i32* null, i32 4096, i32 %7, i32 %10, i32 -1, i32 0)
%12 = tail call i64 (...) @fork()
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = tail call i32 @strcpy(i8* %11, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%17 = load i32, i32* @EXIT_SUCCESS
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = icmp sgt i64 %12, 0
br i1 %20, label %21, label %26
21: ; preds = %19
%22 = call i32 @wait(i32* %3)
%23 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%24 = load i32, i32* @EXIT_SUCCESS
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %19
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
}