#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;
char *MAP_FAILED;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
scalar_t__ fork();
char *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strcpy(char *, char *);
int main() {
  char *p =
      mmap(NULL, 20, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
  if (p == MAP_FAILED) {
    perror("mmap");
    return 1;
  }
  if (fork() == 0) {
    strcpy(p, "Hello,芙蓉");
    sleep(5);
  } else {
    sleep(2);
    puts(p);
  }
  munmap(p, 20);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e72(%rip),%rax
mov    (%rax),%edx
mov    0x2e39(%rip),%rax
or     (%rax),%edx
mov    0x2e40(%rip),%rax
mov    (%rax),%ecx
mov    0x2e47(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x14,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2df7(%rip),%rcx
cmp    (%rcx),%rax
jne    11ea <main+0x7a>
lea    0xe27(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    123b <main+0xcb>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    121a <main+0xaa>
mov    -0x10(%rbp),%rdi
lea    0xdff(%rip),%rsi
call   1030 <strcpy@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
jmp    122d <main+0xbd>
mov    $0x2,%edi
call   1080 <sleep@plt>
mov    -0x10(%rbp),%rdi
call   1040 <puts@plt>
mov    -0x10(%rbp),%rdi
mov    $0x14,%esi
call   1060 <munmap@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e80(%rip),%rax
mov    0x2e49(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e4e(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x14,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2e0c(%rip),%rax
cmp    (%rax),%rbx
je     1203 <main+0x93>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     11d9 <main+0x69>
mov    $0x2,%edi
call   1080 <sleep@plt>
mov    %rbx,%rdi
call   1040 <puts@plt>
jmp    11f2 <main+0x82>
lea    0xe25(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    $0x5,%edi
call   1080 <sleep@plt>
mov    %rbx,%rdi
mov    $0x14,%esi
call   1060 <munmap@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xdf6(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
pop    %rbx
ret

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"
@.str.1 = [13 x i8] c"Hello,\E8\8A\99\E8\93\89\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
store i32 0, i32* %1
%3 = load i32, i32* @PROT_READ
%4 = load i32, i32* @PROT_WRITE
%5 = or i32 %3, %4
%6 = load i32, i32* @MAP_ANONYMOUS
%7 = load i32, i32* @MAP_SHARED
%8 = or i32 %6, %7
%9 = call i8* @mmap(i32* null, i32 20, i32 %5, i32 %8, i32 -1, i32 0)
store i8* %9, i8** %2
%10 = load i8*, i8** %2
%11 = load i8*, i8** @MAP_FAILED
%12 = icmp eq i8* %10, %11
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %29
15: ; preds = %0
%16 = call i64 (...) @fork()
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i8*, i8** %2
%20 = call i32 @strcpy(i8* %19, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @sleep(i32 5)
br label %26
22: ; preds = %15
%23 = call i32 @sleep(i32 2)
%24 = load i8*, i8** %2
%25 = call i32 @puts(i8* %24)
br label %26
26: ; preds = %22, %18
%27 = load i8*, i8** %2
%28 = call i32 @munmap(i8* %27, i32 20)
br label %29
29: ; preds = %26, %13
%30 = load i32, i32* %1
ret i32 %30
}

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"
@.str.1 = [13 x i8] c"Hello,\E8\8A\99\E8\93\89\00"
define i32 @main() {
%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 i8* @mmap(i32* null, i32 20, i32 %3, i32 %6, i32 -1, i32 0)
%8 = load i8*, i8** @MAP_FAILED
%9 = icmp eq i8* %7, %8
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %23
12: ; preds = %0
%13 = tail call i64 (...) @fork()
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = tail call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @sleep(i32 5)
br label %21
18: ; preds = %12
%19 = tail call i32 @sleep(i32 2)
%20 = tail call i32 @puts(i8* dereferenceable(1) %7)
br label %21
21: ; preds = %18, %15
%22 = tail call i32 @munmap(i8* %7, i32 20)
br label %23
23: ; preds = %21, %10
%24 = phi i32 [ 1, %10 ], [ 0, %21 ]
ret i32 %24
}