#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 int pid_t;

/* Variables and functions */
int EXIT_SUCCESS;
int MAP_ANON;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ exit(int);
int fork();
unsigned char *mmap(int /*<<< orphan*/, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
int main(int argc, char **argv) {
  unsigned char *m;
  int i;
  pid_t pid;
  m = mmap(0, 1024 * 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1,
           0);
  for (i = 0; i < 1024 * 1024; i++)
    m[i] = 10;
  pid = fork();
  if (pid == -1) {
    exit(1);
  } else if (pid == 0) {
    sleep(10);
  } else {
    sleep(5);
    for (i = 0; i < 1024 * 1024; i++)
      m[i] = 20;
    printf("cow is done in parent pscess\n");
    sleep(10);
  }
  return EXIT_SUCCESS;
}


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    0x2e8b(%rip),%rax
mov    (%rax),%edx
mov    0x2e62(%rip),%rax
or     (%rax),%edx
mov    0x2e41(%rip),%rax
mov    (%rax),%ecx
mov    0x2e48(%rip),%rax
or     (%rax),%ecx
xor    %r9d,%r9d
mov    $0x100000,%esi
mov    $0xffffffff,%r8d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x100000,-0x1c(%rbp)
jge    11d2 <main+0x82>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0xa,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ab <main+0x5b>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11f0 <main+0xa0>
mov    $0x1,%edi
call   1050 <exit@plt>
cmpl   $0x0,-0x20(%rbp)
jne    1209 <main+0xb9>
mov    $0xa,%edi
call   1060 <sleep@plt>
jmp    1257 <main+0x107>
mov    $0x5,%edi
call   1060 <sleep@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x100000,-0x1c(%rbp)
jge    1241 <main+0xf1>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x14,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    121a <main+0xca>
lea    0xdb8(%rip),%rdi
call   1040 <printf@plt>
mov    $0xa,%edi
call   1060 <sleep@plt>
jmp    125c <main+0x10c>
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e90(%rip),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e46(%rip),%rax
mov    0x2e4f(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x100000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    $0x100000,%edx
mov    %rax,%rdi
mov    $0xa,%esi
call   1050 <memset@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11e7 <main+0x87>
cmp    $0xffffffff,%eax
je     11fc <main+0x9c>
mov    $0x5,%edi
call   1070 <sleep@plt>
mov    $0x100000,%edx
mov    %rbx,%rdi
mov    $0x14,%esi
call   1050 <memset@plt>
lea    0xe1e(%rip),%rdi
call   1030 <puts@plt>
mov    $0xa,%edi
call   1070 <sleep@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
pop    %rbx
ret
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANON = global i32 0
@.str = [30 x i8] c"cow is done in parent pscess\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @PROT_WRITE
%11 = or i32 %9, %10
%12 = load i32, i32* @MAP_PRIVATE
%13 = load i32, i32* @MAP_ANON
%14 = or i32 %12, %13
%15 = call i8* @mmap(i32 0, i32 1048576, i32 %11, i32 %14, i32 -1, i32 0)
store i8* %15, i8** %6
store i32 0, i32* %7
br label %16
16: ; preds = %24, %2
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 1048576
br i1 %18, label %19, label %27
19: ; preds = %16
%20 = load i8*, i8** %6
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
store i8 10, i8* %23
br label %24
24: ; preds = %19
%25 = load i32, i32* %7
%26 = add nsw i32 %25, 1
store i32 %26, i32* %7
br label %16
27: ; preds = %16
%28 = call i32 (...) @fork()
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %27
%34 = load i32, i32* %8
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @sleep(i32 10)
br label %54
38: ; preds = %33
%39 = call i32 @sleep(i32 5)
store i32 0, i32* %7
br label %40
40: ; preds = %48, %38
%41 = load i32, i32* %7
%42 = icmp slt i32 %41, 1048576
br i1 %42, label %43, label %51
43: ; preds = %40
%44 = load i8*, i8** %6
%45 = load i32, i32* %7
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 20, i8* %47
br label %48
48: ; preds = %43
%49 = load i32, i32* %7
%50 = add nsw i32 %49, 1
store i32 %50, i32* %7
br label %40
51: ; preds = %40
%52 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%53 = call i32 @sleep(i32 10)
br label %54
54: ; preds = %51, %36
br label %55
55: ; preds = %54
%56 = load i32, i32* @EXIT_SUCCESS
ret i32 %56
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANON = global i32 0
@EXIT_SUCCESS = global i32 0
@str = [29 x i8] c"cow is done in parent pscess\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_PRIVATE
%7 = load i32, i32* @MAP_ANON
%8 = or i32 %7, %6
%9 = tail call i8* @mmap(i32 0, i32 1048576, i32 %5, i32 %8, i32 -1, i32 0)
%10 = tail call i32 (...) @fork()
switch i32 %10, label %13 [
i32 -1, label %11
i32 0, label %16
]
11: ; preds = %2
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = tail call i32 @sleep(i32 5)
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %16
16: ; preds = %2, %13
%17 = tail call i32 @sleep(i32 10)
%18 = load i32, i32* @EXIT_SUCCESS
ret i32 %18
}