#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 /*<<< orphan*/ EXIT_SUCCESS;
void *create_shared_memory();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ handle_fatal_error(char *);
int /*<<< orphan*/ manage_child(int *, int *, int *);
int /*<<< orphan*/ manage_parent(int *, int *, int *);
int main(void) {
  pid_t pid;
  void *shared_memory;
  int *buffer;
  int *in;
  int *out;
  shared_memory = create_shared_memory();
  buffer = (int *)shared_memory;
  in = (int *)shared_memory + 5 * sizeof(int);
  out = (int *)shared_memory + (5 + 1) * sizeof(int);
  *in = *out = 0;
  if (-1 == (pid = fork())) {
    handle_fatal_error("Error using fork()\n");
  }
  if (0 == pid) {
    manage_child(buffer, in, out);
  } else {
    manage_parent(buffer, in, out);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1070 <create_shared_memory@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x50,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    $0x60,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movl   $0x0,(%rax)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,%ecx
mov    %ecx,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    11d3 <main+0x73>
lea    0xe32(%rip),%rdi
call   1030 <handle_fatal_error@plt>
xor    %eax,%eax
cmp    -0x8(%rbp),%eax
jne    11f4 <main+0x94>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <manage_child@plt>
jmp    1205 <main+0xa5>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <manage_parent@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
xor    %eax,%eax
call   1070 <create_shared_memory@plt>
mov    %rax,%rbx
lea    0x50(%rax),%r15
lea    0x60(%rax),%r14
movl   $0x0,0x60(%rax)
movl   $0x0,0x50(%rax)
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11b1 <main+0x51>
cmp    $0xffffffff,%eax
jne    11a1 <main+0x41>
lea    0xe64(%rip),%rdi
call   1030 <handle_fatal_error@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1040 <manage_parent@plt>
jmp    11bf <main+0x5f>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <manage_child@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [20 x i8] c"Error using fork()\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
store i32 0, i32* %1
%7 = call i8* (...) @create_shared_memory()
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = bitcast i8* %8 to i32*
store i32* %9, i32** %4
%10 = load i8*, i8** %3
%11 = bitcast i8* %10 to i32*
%12 = getelementptr inbounds i32, i32* %11, i64 20
store i32* %12, i32** %5
%13 = load i8*, i8** %3
%14 = bitcast i8* %13 to i32*
%15 = getelementptr inbounds i32, i32* %14, i64 24
store i32* %15, i32** %6
%16 = load i32*, i32** %6
store i32 0, i32* %16
%17 = load i32*, i32** %5
store i32 0, i32* %17
%18 = call i32 (...) @fork()
store i32 %18, i32* %2
%19 = icmp eq i32 -1, %18
br i1 %19, label %20, label %22
20: ; preds = %0
%21 = call i32 @handle_fatal_error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %0
%23 = load i32, i32* %2
%24 = icmp eq i32 0, %23
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i32*, i32** %4
%27 = load i32*, i32** %5
%28 = load i32*, i32** %6
%29 = call i32 @manage_child(i32* %26, i32* %27, i32* %28)
br label %35
30: ; preds = %22
%31 = load i32*, i32** %4
%32 = load i32*, i32** %5
%33 = load i32*, i32** %6
%34 = call i32 @manage_parent(i32* %31, i32* %32, i32* %33)
br label %35
35: ; preds = %30, %25
%36 = load i32, i32* @EXIT_SUCCESS
%37 = call i32 @exit(i32 %36)
unreachable
}

LLVM IR (-O2):
@.str = [20 x i8] c"Error using fork()\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = tail call i8* (...) @create_shared_memory()
%2 = bitcast i8* %1 to i32*
%3 = getelementptr inbounds i8, i8* %1, i64 80
%4 = bitcast i8* %3 to i32*
%5 = getelementptr inbounds i8, i8* %1, i64 96
%6 = bitcast i8* %5 to i32*
store i32 0, i32* %6
store i32 0, i32* %4
%7 = tail call i32 (...) @fork()
switch i32 %7, label %12 [
i32 -1, label %8
i32 0, label %10
]
8: ; preds = %0
%9 = tail call i32 @handle_fatal_error(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %12
10: ; preds = %0
%11 = tail call i32 @manage_child(i32* %2, i32* %4, i32* %6)
br label %14
12: ; preds = %8, %0
%13 = tail call i32 @manage_parent(i32* %2, i32* %4, i32* %6)
br label %14
14: ; preds = %12, %10
%15 = load i32, i32* @EXIT_SUCCESS
%16 = tail call i32 @exit(i32 %15)
unreachable
}