#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_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int main(int argc, char **argv) {
  char *p_map;
  p_map = (char *)mmap(NULL, 100, PROT_READ | PROT_WRITE,
                       MAP_SHARED | MAP_ANONYMOUS, -1, 0);
  if (fork() == 0) {
    sleep(1);
    printf("child got a message: %s\n", p_map);
    sprintf(p_map, "%s", "hi, dad, this is son");
    munmap(p_map, 100);
    exit(0);
  }
  sprintf(p_map, "%s", "hi, this is father");
  sleep(2);
  printf("parent got a message: %s\n", p_map);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%edx
mov    0x2e32(%rip),%rax
or     (%rax),%edx
mov    0x2e49(%rip),%rax
mov    (%rax),%ecx
mov    0x2e30(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x64,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    121c <main+0xac>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0x18(%rbp),%rsi
lea    0xe15(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe1e(%rip),%rsi
lea    0xe1a(%rip),%rdx
call   1060 <sprintf@plt>
mov    -0x18(%rbp),%rdi
mov    $0x64,%esi
call   1050 <munmap@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    0xdf2(%rip),%rsi
lea    0xe03(%rip),%rdx
call   1060 <sprintf@plt>
mov    $0x2,%edi
call   1080 <sleep@plt>
mov    -0x18(%rbp),%rsi
lea    0xdfc(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e90(%rip),%rax
mov    0x2e59(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e6e(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x64,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     11d7 <main+0x77>
movups 0xe7e(%rip),%xmm0
movups %xmm0,(%rbx)
movl   $0x726568,0xf(%rbx)
mov    $0x2,%edi
call   1070 <sleep@plt>
lea    0xe76(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    $0x1,%edi
call   1070 <sleep@plt>
lea    0xe18(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
movabs $0x6e6f7320736920,%rax
mov    %rax,0xd(%rbx)
movups 0xe14(%rip),%xmm0
movups %xmm0,(%rbx)
mov    %rbx,%rdi
mov    $0x64,%esi
call   1050 <munmap@plt>
xor    %edi,%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 = [25 x i8] c"child got a message: %s\0A\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [21 x i8] c"hi, dad, this is son\00"
@.str.3 = [19 x i8] c"hi, this is father\00"
@.str.4 = [26 x i8] c"parent got a message: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* @PROT_READ
%8 = load i32, i32* @PROT_WRITE
%9 = or i32 %7, %8
%10 = load i32, i32* @MAP_SHARED
%11 = load i32, i32* @MAP_ANONYMOUS
%12 = or i32 %10, %11
%13 = call i64 @mmap(i32* null, i32 100, i32 %9, i32 %12, i32 -1, i32 0)
%14 = inttoptr i64 %13 to i8*
store i8* %14, i8** %6
%15 = call i64 (...) @fork()
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %2
%18 = call i32 @sleep(i32 1)
%19 = load i8*, i8** %6
%20 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = load i8*, i8** %6
%22 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%23 = load i8*, i8** %6
%24 = call i32 @munmap(i8* %23, i32 100)
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %2
%27 = load i8*, i8** %6
%28 = call i32 @sprintf(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 @sleep(i32 2)
%30 = load i8*, i8** %6
%31 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %30)
ret i32 0
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str = [25 x i8] c"child got a message: %s\0A\00"
@.str.2 = [21 x i8] c"hi, dad, this is son\00"
@.str.3 = [19 x i8] c"hi, this is father\00"
@.str.4 = [26 x i8] c"parent got a message: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @PROT_READ
%4 = load i32, i32* @PROT_WRITE
%5 = or i32 %4, %3
%6 = load i32, i32* @MAP_SHARED
%7 = load i32, i32* @MAP_ANONYMOUS
%8 = or i32 %7, %6
%9 = tail call i64 @mmap(i32* null, i32 100, i32 %5, i32 %8, i32 -1, i32 0)
%10 = inttoptr i64 %9 to i8*
%11 = tail call i64 (...) @fork()
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = tail call i32 @sleep(i32 1)
%15 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %10)
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(21) %10, i8* align 1 dereferenceable(21) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i64 21, i1 false)
%16 = tail call i32 @munmap(i8* %10, i32 100)
%17 = tail call i32 @exit(i32 0)
unreachable
18: ; preds = %2
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(19) %10, i8* align 1 dereferenceable(19) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i64 19, i1 false)
%19 = tail call i32 @sleep(i32 2)
%20 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i8* %10)
ret i32 0
}