#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*/ EXIT_FAILURE;
int /*<<< orphan*/ TELL_CHILD();
int /*<<< orphan*/ TELL_PARENT();
int /*<<< orphan*/ TELL_WAIT();
int /*<<< orphan*/ WAIT_CHILD();
int /*<<< orphan*/ WAIT_PARENT();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ getchar();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ write_msgs(char *);
int main(void) {
  printf("Started\n");
  TELL_WAIT();
  pid_t f = fork();
  if (f < 0) {
    perror("Couldn't fork");
    exit(EXIT_FAILURE);
  }
  if (f == 0) {
    write_msgs("child: ");
    TELL_PARENT();
    WAIT_PARENT();
    printf("Child exiting\n");
    TELL_PARENT();
  } else {
    WAIT_CHILD();
    write_msgs("parent: ");
    printf("Press any key to terminate\n");
    getchar();
    TELL_CHILD();
    WAIT_CHILD();
    printf("Parent exiting\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe3a(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1070 <TELL_WAIT@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1202 <main+0x52>
lea    0xe1a(%rip),%rdi
call   1090 <perror@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    123f <main+0x8f>
lea    0xe03(%rip),%rdi
call   10c0 <write_msgs@plt>
mov    $0x0,%al
call   1050 <TELL_PARENT@plt>
mov    $0x0,%al
call   10b0 <WAIT_PARENT@plt>
lea    0xdf1(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1050 <TELL_PARENT@plt>
jmp    127f <main+0xcf>
mov    $0x0,%al
call   1030 <WAIT_CHILD@plt>
lea    0xde1(%rip),%rdi
call   10c0 <write_msgs@plt>
lea    0xdde(%rip),%rdi
call   1060 <printf@plt>
mov    $0x0,%al
call   1080 <getchar@plt>
mov    $0x0,%al
call   1040 <TELL_CHILD@plt>
mov    $0x0,%al
call   1030 <WAIT_CHILD@plt>
lea    0xdd9(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
lea    0xe67(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1070 <TELL_WAIT@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     1245 <main+0x95>
je     1214 <main+0x64>
xor    %eax,%eax
call   1030 <WAIT_CHILD@plt>
lea    0xe36(%rip),%rdi
call   10c0 <write_msgs@plt>
lea    0xe3b(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1080 <getchar@plt>
xor    %eax,%eax
call   1050 <TELL_CHILD@plt>
xor    %eax,%eax
call   1030 <WAIT_CHILD@plt>
lea    0xe35(%rip),%rdi
call   1040 <puts@plt>
jmp    1241 <main+0x91>
lea    0xdf3(%rip),%rdi
call   10c0 <write_msgs@plt>
xor    %eax,%eax
call   1060 <TELL_PARENT@plt>
xor    %eax,%eax
call   10b0 <WAIT_PARENT@plt>
lea    0xe1c(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1060 <TELL_PARENT@plt>
xor    %eax,%eax
pop    %rcx
ret
lea    0xdb4(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"Started\0A\00"
@.str.1 = [14 x i8] c"Couldn't fork\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [8 x i8] c"child: \00"
@.str.3 = [15 x i8] c"Child exiting\0A\00"
@.str.4 = [9 x i8] c"parent: \00"
@.str.5 = [28 x i8] c"Press any key to terminate\0A\00"
@.str.6 = [16 x i8] c"Parent exiting\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
store i32 0, i32* %1
%3 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%4 = call i32 (...) @TELL_WAIT()
%5 = call i64 (...) @fork()
store i64 %5, i64* %2
%6 = load i64, i64* %2
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = load i64, i64* %2
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = call i32 @write_msgs(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 (...) @TELL_PARENT()
%18 = call i32 (...) @WAIT_PARENT()
%19 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 (...) @TELL_PARENT()
br label %29
21: ; preds = %12
%22 = call i32 (...) @WAIT_CHILD()
%23 = call i32 @write_msgs(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%24 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
%25 = call i32 (...) @getchar()
%26 = call i32 (...) @TELL_CHILD()
%27 = call i32 (...) @WAIT_CHILD()
%28 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
br label %29
29: ; preds = %21, %15
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [14 x i8] c"Couldn't fork\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [8 x i8] c"child: \00"
@.str.4 = [9 x i8] c"parent: \00"
@str = [8 x i8] c"Started\00"
@str.7 = [27 x i8] c"Press any key to terminate\00"
@str.8 = [15 x i8] c"Parent exiting\00"
@str.9 = [14 x i8] c"Child exiting\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 (...) @TELL_WAIT()
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = icmp eq i64 %3, 0
br i1 %10, label %11, label %17
11: ; preds = %9
%12 = tail call i32 @write_msgs(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32 (...) @TELL_PARENT()
%14 = tail call i32 (...) @WAIT_PARENT()
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.9, i64 0, i64 0))
%16 = tail call i32 (...) @TELL_PARENT()
br label %25
17: ; preds = %9
%18 = tail call i32 (...) @WAIT_CHILD()
%19 = tail call i32 @write_msgs(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.7, i64 0, i64 0))
%21 = tail call i32 (...) @getchar()
%22 = tail call i32 (...) @TELL_CHILD()
%23 = tail call i32 (...) @WAIT_CHILD()
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.8, i64 0, i64 0))
br label %25
25: ; preds = %17, %11
ret i32 0
}