#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_ANONYMOUS;
char *MAP_FAILED;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
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*/ strcpy(char *, char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  char *str;
  pid_t pid;
  str = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1,
             0);
  if (str == MAP_FAILED) {
    perror("mmap()");
    exit(1);
  }
  pid = fork();
  if (pid < 0) {
    perror("fork()");
    exit(1);
  }
  if (pid == 0) {
    strcpy(str, "Hello!");
    munmap(str, 1024);
    exit(0);
  } else {
    wait(NULL);
    puts(str);
    munmap(str, 1024);
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
mov    0x2e29(%rip),%rax
or     (%rax),%edx
mov    0x2e40(%rip),%rax
mov    (%rax),%ecx
mov    0x2e27(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x400,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2de7(%rip),%rcx
cmp    (%rcx),%rax
jne    11f8 <main+0x78>
lea    0xe17(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1224 <main+0xa4>
lea    0xdf2(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1254 <main+0xd4>
mov    -0x10(%rbp),%rdi
lea    0xdd4(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
mov    $0x400,%esi
call   1060 <munmap@plt>
xor    %edi,%edi
call   1080 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
mov    -0x10(%rbp),%rdi
call   1040 <puts@plt>
mov    -0x10(%rbp),%rdi
mov    $0x400,%esi
call   1060 <munmap@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e70(%rip),%rax
mov    0x2e39(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e4e(%rip),%rax
mov    0x2e37(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x400,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2dfc(%rip),%rax
cmp    (%rax),%rbx
jne    11d2 <main+0x52>
lea    0xe30(%rip),%rdi
jmp    11e5 <main+0x65>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jns    11f4 <main+0x74>
lea    0xe22(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jne    1207 <main+0x87>
lea    0xe11(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
jmp    1216 <main+0x96>
xor    %edi,%edi
call   1090 <wait@plt>
mov    %rbx,%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
call   1060 <munmap@plt>
xor    %edi,%edi
call   1080 <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
@MAP_FAILED = global i8* null
@.str = [7 x i8] c"mmap()\00"
@.str.1 = [7 x i8] c"fork()\00"
@.str.2 = [7 x i8] c"Hello!\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i64
store i32 0, i32* %1
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @PROT_WRITE
%6 = or i32 %4, %5
%7 = load i32, i32* @MAP_SHARED
%8 = load i32, i32* @MAP_ANONYMOUS
%9 = or i32 %7, %8
%10 = call i8* @mmap(i32* null, i32 1024, i32 %6, i32 %9, i32 -1, i32 0)
store i8* %10, i8** %2
%11 = load i8*, i8** %2
%12 = load i8*, i8** @MAP_FAILED
%13 = icmp eq i8* %11, %12
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = call i64 (...) @fork()
store i64 %18, i64* %3
%19 = load i64, i64* %3
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %17
%25 = load i64, i64* %3
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = load i8*, i8** %2
%29 = call i32 @strcpy(i8* %28, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%30 = load i8*, i8** %2
%31 = call i32 @munmap(i8* %30, i32 1024)
%32 = call i32 @exit(i32 0)
unreachable
33: ; preds = %24
%34 = call i32 @wait(i32* null)
%35 = load i8*, i8** %2
%36 = call i32 @puts(i8* %35)
%37 = load i8*, i8** %2
%38 = call i32 @munmap(i8* %37, i32 1024)
%39 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@.str = [7 x i8] c"mmap()\00"
@.str.1 = [7 x i8] c"fork()\00"
@.str.2 = [7 x i8] c"Hello!\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_SHARED
%5 = load i32, i32* @MAP_ANONYMOUS
%6 = or i32 %5, %4
%7 = tail call i8* @mmap(i32* null, i32 1024, 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 %13
10: ; preds = %0
%11 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = tail call i64 (...) @fork()
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %13
%20 = icmp eq i64 %14, 0
br i1 %20, label %21, label %25
21: ; preds = %19
%22 = tail call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i32 @munmap(i8* %7, i32 1024)
%24 = tail call i32 @exit(i32 0)
unreachable
25: ; preds = %19
%26 = tail call i32 @wait(i32* null)
%27 = tail call i32 @puts(i8* dereferenceable(1) %7)
%28 = tail call i32 @munmap(i8* %7, i32 1024)
%29 = tail call i32 @exit(i32 0)
unreachable
}