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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe4a(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   10a0 <NOTIFY_INIT@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11f2 <main+0x52>
lea    0xe2a(%rip),%rdi
call   1060 <perror@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    123f <main+0x9f>
lea    0xe13(%rip),%rdi
call   1090 <write_msgs@plt>
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %rax,%rdi
call   1030 <NOTIFY@plt>
mov    $0x0,%al
call   10c0 <WAIT_NOTIFY@plt>
lea    0xdf9(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1070 <getppid@plt>
mov    %rax,%rdi
call   1030 <NOTIFY@plt>
jmp    1281 <main+0xe1>
mov    $0x0,%al
call   10c0 <WAIT_NOTIFY@plt>
lea    0xde1(%rip),%rdi
call   1090 <write_msgs@plt>
lea    0xdde(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    -0x10(%rbp),%rdi
call   1030 <NOTIFY@plt>
mov    $0x0,%al
call   10c0 <WAIT_NOTIFY@plt>
lea    0xdd7(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xe77(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   10a0 <NOTIFY_INIT@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     124d <main+0xad>
je     120c <main+0x6c>
mov    %rax,%rbx
xor    %eax,%eax
call   10c0 <WAIT_NOTIFY@plt>
lea    0xe3f(%rip),%rdi
call   1090 <write_msgs@plt>
lea    0xe44(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    %rbx,%rdi
call   1030 <NOTIFY@plt>
xor    %eax,%eax
call   10c0 <WAIT_NOTIFY@plt>
lea    0xe3d(%rip),%rdi
call   1040 <puts@plt>
jmp    1249 <main+0xa9>
lea    0xdfb(%rip),%rdi
call   1090 <write_msgs@plt>
xor    %eax,%eax
call   1070 <getppid@plt>
mov    %rax,%rdi
call   1030 <NOTIFY@plt>
xor    %eax,%eax
call   10c0 <WAIT_NOTIFY@plt>
lea    0xe1c(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1070 <getppid@plt>
mov    %rax,%rdi
call   1030 <NOTIFY@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xdac(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   1080 <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 (...) @NOTIFY_INIT()
%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 %23
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 i64 (...) @getppid()
%18 = call i32 @NOTIFY(i64 %17)
%19 = call i32 (...) @WAIT_NOTIFY()
%20 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%21 = call i64 (...) @getppid()
%22 = call i32 @NOTIFY(i64 %21)
br label %32
23: ; preds = %12
%24 = call i32 (...) @WAIT_NOTIFY()
%25 = call i32 @write_msgs(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%26 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
%27 = call i32 (...) @getchar()
%28 = load i64, i64* %2
%29 = call i32 @NOTIFY(i64 %28)
%30 = call i32 (...) @WAIT_NOTIFY()
%31 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
br label %32
32: ; preds = %23, %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 (...) @NOTIFY_INIT()
%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 %19
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 i64 (...) @getppid()
%14 = tail call i32 @NOTIFY(i64 %13)
%15 = tail call i32 (...) @WAIT_NOTIFY()
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.9, i64 0, i64 0))
%17 = tail call i64 (...) @getppid()
%18 = tail call i32 @NOTIFY(i64 %17)
br label %27
19: ; preds = %9
%20 = tail call i32 (...) @WAIT_NOTIFY()
%21 = tail call i32 @write_msgs(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.7, i64 0, i64 0))
%23 = tail call i32 (...) @getchar()
%24 = tail call i32 @NOTIFY(i64 %3)
%25 = tail call i32 (...) @WAIT_NOTIFY()
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.8, i64 0, i64 0))
br label %27
27: ; preds = %19, %11
ret i32 0
}