#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*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int getpid();
void *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, scalar_t__);
int main() {
  int fd = open("/etc/passwd", O_RDONLY);
  assert(fd > 0);
  void *ptr = mmap(NULL, (8192), PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  assert(ptr != NULL);
  close(fd);
  pid_t pid = fork();
  assert(pid >= 0);
  if (pid == 0) {
    printf("child pid=%d\n", getpid());
    exit(0);
  } else {
    printf("parent: child pid=%d\n", pid);
    exit(0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
lea    0xe61(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    0x2e08(%rip),%rax
or     (%rax),%edx
mov    0x2df7(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x2000,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
jne    124b <main+0xcb>
mov    $0x0,%al
call   1030 <getpid@plt>
movslq %eax,%rsi
lea    0xdd5(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0xdb6(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
lea    0xe6f(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
xor    %edi,%edi
test   %eax,%eax
setg   %dil
call   1070 <assert@plt>
mov    0x2e4c(%rip),%rax
mov    0x2e1d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e0a(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x2000,%esi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1070 <assert@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,%rbx
mov    %rax,%rdi
shr    $0x3f,%rdi
xor    $0x1,%edi
call   1070 <assert@plt>
test   %rbx,%rbx
jne    1218 <main+0x98>
xor    %eax,%eax
call   1030 <getpid@plt>
movslq %eax,%rsi
lea    0xdfe(%rip),%rdi
jmp    1222 <main+0xa2>
lea    0xded(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@.str.1 = [14 x i8] c"child pid=%d\0A\00"
@.str.2 = [22 x i8] c"parent: child pid=%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
store i32 0, i32* %1
%5 = load i32, i32* @O_RDONLY
%6 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp sgt i32 %7, 0
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i32, i32* @PROT_READ
%12 = load i32, i32* @PROT_WRITE
%13 = or i32 %11, %12
%14 = load i32, i32* @MAP_PRIVATE
%15 = load i32, i32* %2
%16 = call i8* @mmap(i32* null, i32 8192, i32 %13, i32 %14, i32 %15, i32 0)
store i8* %16, i8** %3
%17 = load i8*, i8** %3
%18 = icmp ne i8* %17, null
%19 = zext i1 %18 to i32
%20 = call i32 @assert(i32 %19)
%21 = load i32, i32* %2
%22 = call i32 @close(i32 %21)
%23 = call i64 (...) @fork()
store i64 %23, i64* %4
%24 = load i64, i64* %4
%25 = icmp sge i64 %24, 0
%26 = zext i1 %25 to i32
%27 = call i32 @assert(i32 %26)
%28 = load i64, i64* %4
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %0
%31 = call i32 (...) @getpid()
%32 = sext i32 %31 to i64
%33 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i64 %32)
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %0
%36 = load i64, i64* %4
%37 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 %36)
%38 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [12 x i8] c"/etc/passwd\00"
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@.str.1 = [14 x i8] c"child pid=%d\0A\00"
@.str.2 = [22 x i8] c"parent: child pid=%d\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_RDONLY
%2 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp sgt i32 %2, 0
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = load i32, i32* @PROT_READ
%7 = load i32, i32* @PROT_WRITE
%8 = or i32 %7, %6
%9 = load i32, i32* @MAP_PRIVATE
%10 = tail call i8* @mmap(i32* null, i32 8192, i32 %8, i32 %9, i32 %2, i32 0)
%11 = icmp ne i8* %10, null
%12 = zext i1 %11 to i32
%13 = tail call i32 @assert(i32 %12)
%14 = tail call i32 @close(i32 %2)
%15 = tail call i64 (...) @fork()
%16 = lshr i64 %15, 63
%17 = trunc i64 %16 to i32
%18 = xor i32 %17, 1
%19 = tail call i32 @assert(i32 %18)
%20 = icmp eq i64 %15, 0
br i1 %20, label %21, label %26
21: ; preds = %0
%22 = tail call i32 (...) @getpid()
%23 = sext i32 %22 to i64
%24 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i64 %23)
%25 = tail call i32 @exit(i32 0)
unreachable
26: ; preds = %0
%27 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i64 %15)
%28 = tail call i32 @exit(i32 0)
unreachable
}