#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 */
scalar_t__ EEXIST;
scalar_t__ MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_EXCL;
int O_RDWR;
int O_TRUNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ TELL_CHILD(int);
int /*<<< orphan*/ TELL_PARENT(int /*<<< orphan*/);
int /*<<< orphan*/ TELL_WAIT();
int /*<<< orphan*/ WAIT_CHILD();
int /*<<< orphan*/ WAIT_PARENT();
int /*<<< orphan*/ err_sys(char *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ getppid();
scalar_t__ mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
                int /*<<< orphan*/);
int open(char *, int, ...);
int /*<<< orphan*/ printf(char *);
int main() {
  int fd = 0;
  pid_t pid;
  TELL_WAIT();
  pid = fork();
  if (pid == -1) {
    err_sys("\nfork failed");
  } else if (pid == 0) {
    WAIT_PARENT();
    fd = open("./temp.map", O_RDWR | O_CREAT | O_TRUNC);
    TELL_PARENT(getppid());
    if (-1 == fd) {
      err_sys("\nopen failed by child");
    } else {
      printf("open by child successful\n");
    }
    exit(1);
  }
  fd = open("./temp.map", O_RDWR | O_CREAT | O_EXCL, 0666);
  if ((-1 == fd) && (errno == EEXIST)) {
    fd = open("./temp.map", O_RDWR, 0666);
  }
  if (-1 == fd) {
    TELL_CHILD(pid);
    err_sys("\ncan not open file by parent");
  }
  if (MAP_FAILED ==
      mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) {
    TELL_CHILD(pid);
    err_sys("\nmmap failed");
  }
  printf("open and mmap by parent success\n");
  TELL_CHILD(pid);
  WAIT_CHILD();
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    $0x0,%al
call   1090 <TELL_WAIT@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1202 <main+0x42>
lea    0xe08(%rip),%rdi
call   1040 <err_sys@plt>
jmp    1287 <main+0xc7>
cmpl   $0x0,-0xc(%rbp)
jne    1282 <main+0xc2>
mov    $0x0,%al
call   10d0 <WAIT_PARENT@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%esi
mov    0x2dd5(%rip),%rax
or     (%rax),%esi
mov    0x2dc4(%rip),%rax
or     (%rax),%esi
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   10b0 <getppid@plt>
mov    %eax,%edi
call   1060 <TELL_PARENT@plt>
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    126c <main+0xac>
lea    0xdb6(%rip),%rdi
call   1040 <err_sys@plt>
jmp    1278 <main+0xb8>
lea    0xdbb(%rip),%rdi
call   1080 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
jmp    1287 <main+0xc7>
mov    0x2d3a(%rip),%rax
mov    (%rax),%esi
mov    0x2d61(%rip),%rax
or     (%rax),%esi
mov    0x2d10(%rip),%rax
or     (%rax),%esi
lea    0xd64(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    12ff <main+0x13f>
mov    0x2cdb(%rip),%rax
mov    (%rax),%rax
mov    0x2ce1(%rip),%rcx
cmp    (%rcx),%rax
jne    12ff <main+0x13f>
mov    0x2ce1(%rip),%rax
mov    (%rax),%esi
lea    0xd1d(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    1321 <main+0x161>
mov    -0xc(%rbp),%edi
call   1050 <TELL_CHILD@plt>
lea    0xd2c(%rip),%rdi
call   1040 <err_sys@plt>
mov    0x2c70(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2cb2(%rip),%rax
mov    (%rax),%edx
mov    0x2c61(%rip),%rax
or     (%rax),%edx
mov    0x2c90(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x400,%esi
xor    %r9d,%r9d
call   1070 <mmap@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
cmp    %rcx,%rax
jne    1383 <main+0x1c3>
mov    -0xc(%rbp),%edi
call   1050 <TELL_CHILD@plt>
lea    0xce7(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xce8(%rip),%rdi
call   1080 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <TELL_CHILD@plt>
mov    $0x0,%al
call   1030 <WAIT_CHILD@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   1090 <TELL_WAIT@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     12da <main+0x11a>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11ed <main+0x2d>
lea    0xe18(%rip),%rdi
call   1050 <err_sys@plt>
mov    0x2dd4(%rip),%r14
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
or     (%r14),%esi
mov    0x2da9(%rip),%rax
or     (%rax),%esi
lea    0xdfd(%rip),%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
jne    126a <main+0xaa>
mov    0x2d80(%rip),%rax
mov    (%rax),%rax
mov    0x2d86(%rip),%rcx
cmp    (%rcx),%rax
jne    1252 <main+0x92>
mov    (%r14),%esi
lea    0xdcc(%rip),%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
jne    126a <main+0xaa>
mov    %ebp,%edi
call   1060 <TELL_CHILD@plt>
lea    0xdce(%rip),%rdi
call   1050 <err_sys@plt>
mov    $0xffffffff,%eax
mov    0x2d27(%rip),%rcx
mov    (%rcx),%rbx
mov    0x2d6d(%rip),%rcx
mov    0x2d1e(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x2d4b(%rip),%rcx
mov    (%rcx),%ecx
xor    %edi,%edi
mov    $0x400,%esi
mov    %eax,%r8d
xor    %r9d,%r9d
call   1080 <mmap@plt>
cmp    %rax,%rbx
jne    12b9 <main+0xf9>
mov    %ebp,%edi
call   1060 <TELL_CHILD@plt>
lea    0xd97(%rip),%rdi
call   1050 <err_sys@plt>
lea    0xd98(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
call   1060 <TELL_CHILD@plt>
xor    %eax,%eax
call   1030 <WAIT_CHILD@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
xor    %eax,%eax
call   10d0 <WAIT_PARENT@plt>
mov    0x2ce0(%rip),%rax
mov    0x2d09(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2cf6(%rip),%rax
or     (%rax),%esi
lea    0xd0a(%rip),%rdi
xor    %eax,%eax
call   10a0 <open@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10b0 <getppid@plt>
mov    %eax,%edi
call   1070 <TELL_PARENT@plt>
cmp    $0xffffffff,%ebx
jne    1335 <main+0x175>
lea    0xcf2(%rip),%rdi
call   1050 <err_sys@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd3c(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"\0Afork failed\00"
@.str.1 = [11 x i8] c"./temp.map\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [22 x i8] c"\0Aopen failed by child\00"
@.str.3 = [26 x i8] c"open by child successful\0A\00"
@O_EXCL = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.4 = [29 x i8] c"\0Acan not open file by parent\00"
@MAP_FAILED = global i64 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.5 = [13 x i8] c"\0Ammap failed\00"
@.str.6 = [33 x i8] c"open and mmap by parent success\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
%4 = call i32 (...) @TELL_WAIT()
%5 = call i32 (...) @fork()
store i32 %5, i32* %3
%6 = load i32, i32* %3
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %32
10: ; preds = %0
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %31
13: ; preds = %10
%14 = call i32 (...) @WAIT_PARENT()
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = load i32, i32* @O_TRUNC
%19 = or i32 %17, %18
%20 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %19)
store i32 %20, i32* %2
%21 = call i32 (...) @getppid()
%22 = call i32 @TELL_PARENT(i32 %21)
%23 = load i32, i32* %2
%24 = icmp eq i32 -1, %23
br i1 %24, label %25, label %27
25: ; preds = %13
%26 = call i32 @err_sys(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %29
27: ; preds = %13
%28 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %29
29: ; preds = %27, %25
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %10
br label %32
32: ; preds = %31, %8
%33 = load i32, i32* @O_RDWR
%34 = load i32, i32* @O_CREAT
%35 = or i32 %33, %34
%36 = load i32, i32* @O_EXCL
%37 = or i32 %35, %36
%38 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %37, i32 438)
store i32 %38, i32* %2
%39 = load i32, i32* %2
%40 = icmp eq i32 -1, %39
br i1 %40, label %41, label %48
41: ; preds = %32
%42 = load i64, i64* @errno
%43 = load i64, i64* @EEXIST
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %48
45: ; preds = %41
%46 = load i32, i32* @O_RDWR
%47 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %46, i32 438)
store i32 %47, i32* %2
br label %48
48: ; preds = %45, %41, %32
%49 = load i32, i32* %2
%50 = icmp eq i32 -1, %49
br i1 %50, label %51, label %55
51: ; preds = %48
%52 = load i32, i32* %3
%53 = call i32 @TELL_CHILD(i32 %52)
%54 = call i32 @err_sys(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %55
55: ; preds = %51, %48
%56 = load i64, i64* @MAP_FAILED
%57 = load i32, i32* @PROT_READ
%58 = load i32, i32* @PROT_WRITE
%59 = or i32 %57, %58
%60 = load i32, i32* @MAP_SHARED
%61 = load i32, i32* %2
%62 = call i64 @mmap(i32* null, i32 1024, i32 %59, i32 %60, i32 %61, i32 0)
%63 = icmp eq i64 %56, %62
br i1 %63, label %64, label %68
64: ; preds = %55
%65 = load i32, i32* %3
%66 = call i32 @TELL_CHILD(i32 %65)
%67 = call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %68
68: ; preds = %64, %55
%69 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.6, i64 0, i64 0))
%70 = load i32, i32* %3
%71 = call i32 @TELL_CHILD(i32 %70)
%72 = call i32 (...) @WAIT_CHILD()
%73 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [13 x i8] c"\0Afork failed\00"
@.str.1 = [11 x i8] c"./temp.map\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [22 x i8] c"\0Aopen failed by child\00"
@O_EXCL = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.4 = [29 x i8] c"\0Acan not open file by parent\00"
@MAP_FAILED = global i64 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@.str.5 = [13 x i8] c"\0Ammap failed\00"
@str = [32 x i8] c"open and mmap by parent success\00"
@str.7 = [25 x i8] c"open by child successful\00"
define i32 @main() {
%1 = tail call i32 (...) @TELL_WAIT()
%2 = tail call i32 (...) @fork()
switch i32 %2, label %22 [
i32 -1, label %3
i32 0, label %5
]
3: ; preds = %0
%4 = tail call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %22
5: ; preds = %0
%6 = tail call i32 (...) @WAIT_PARENT()
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_CREAT
%9 = or i32 %8, %7
%10 = load i32, i32* @O_TRUNC
%11 = or i32 %9, %10
%12 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = tail call i32 (...) @getppid()
%14 = tail call i32 @TELL_PARENT(i32 %13)
%15 = icmp eq i32 %12, -1
br i1 %15, label %16, label %18
16: ; preds = %5
%17 = tail call i32 @err_sys(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %20
18: ; preds = %5
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.7, i64 0, i64 0))
br label %20
20: ; preds = %18, %16
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %0, %3
%23 = load i32, i32* @O_RDWR
%24 = load i32, i32* @O_CREAT
%25 = or i32 %24, %23
%26 = load i32, i32* @O_EXCL
%27 = or i32 %25, %26
%28 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %27, i32 438)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %41
30: ; preds = %22
%31 = load i64, i64* @errno
%32 = load i64, i64* @EEXIST
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* @O_RDWR
%36 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %35, i32 438)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %30, %34
%39 = tail call i32 @TELL_CHILD(i32 %2)
%40 = tail call i32 @err_sys(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %41
41: ; preds = %22, %38, %34
%42 = phi i32 [ -1, %38 ], [ %36, %34 ], [ %28, %22 ]
%43 = load i64, i64* @MAP_FAILED
%44 = load i32, i32* @PROT_READ
%45 = load i32, i32* @PROT_WRITE
%46 = or i32 %45, %44
%47 = load i32, i32* @MAP_SHARED
%48 = tail call i64 @mmap(i32* null, i32 1024, i32 %46, i32 %47, i32 %42, i32 0)
%49 = icmp eq i64 %43, %48
br i1 %49, label %50, label %53
50: ; preds = %41
%51 = tail call i32 @TELL_CHILD(i32 %2)
%52 = tail call i32 @err_sys(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %53
53: ; preds = %50, %41
%54 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%55 = tail call i32 @TELL_CHILD(i32 %2)
%56 = tail call i32 (...) @WAIT_CHILD()
%57 = tail call i32 @exit(i32 0)
unreachable
}