#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*/ enable_waiting();
scalar_t__ fork();
int /*<<< orphan*/ notify_child();
int /*<<< orphan*/ notify_parent();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ wait_for_child();
int /*<<< orphan*/ wait_for_parent();
int main(void) {
  pid_t chpid;
  enable_waiting();
  chpid = fork();
  if (chpid < 0) {
    printf("\nfork error\n");
    return -1;
  }
  if (chpid > 0) {
    printf("P: Msg1");
    notify_child();
    wait_for_child();
    printf("P: Msg2");
    notify_child();
    wait_for_child();
    printf("P: Msg3");
    notify_child();
    wait(NULL);
  } else {
    wait_for_parent();
    printf("C: Msg1");
    notify_parent();
    wait_for_parent();
    printf("C: Msg2");
    notify_parent();
    wait_for_parent();
    printf("C: Msg3");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1060 <enable_waiting@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11c4 <main+0x44>
lea    0xe4d(%rip),%rdi
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1272 <main+0xf2>
cmpq   $0x0,-0x10(%rbp)
jle    1224 <main+0xa4>
lea    0xe37(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1070 <notify_child@plt>
mov    $0x0,%al
call   1080 <wait_for_child@plt>
lea    0xe25(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1070 <notify_child@plt>
mov    $0x0,%al
call   1080 <wait_for_child@plt>
lea    0xe13(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1070 <notify_child@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
jmp    126b <main+0xeb>
mov    $0x0,%al
call   1030 <wait_for_parent@plt>
lea    0xdf3(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <notify_parent@plt>
mov    $0x0,%al
call   1030 <wait_for_parent@plt>
lea    0xde1(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1040 <notify_parent@plt>
mov    $0x0,%al
call   1030 <wait_for_parent@plt>
lea    0xdcf(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1070 <enable_waiting@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     11f8 <main+0x68>
je     120b <main+0x7b>
lea    0xe53(%rip),%rdi
call   1060 <printf@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <notify_child@plt>
xor    %eax,%eax
call   1090 <wait_for_child@plt>
lea    0xe3f(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
call   1080 <notify_child@plt>
xor    %eax,%eax
call   1090 <wait_for_child@plt>
lea    0xe2d(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
call   1080 <notify_child@plt>
xor    %edi,%edi
call   10a0 <wait@plt>
jmp    1254 <main+0xc4>
lea    0xe31(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
jmp    1254 <main+0xc4>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <wait_for_parent@plt>
lea    0xdfd(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
call   1050 <notify_parent@plt>
xor    %eax,%eax
call   1030 <wait_for_parent@plt>
lea    0xdeb(%rip),%rdi
call   1060 <printf@plt>
xor    %eax,%eax
call   1050 <notify_parent@plt>
xor    %eax,%eax
call   1030 <wait_for_parent@plt>
lea    0xdd9(%rip),%rdi
call   1060 <printf@plt>
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"\0Afork error\0A\00"
@.str.1 = [8 x i8] c"P: Msg1\00"
@.str.2 = [8 x i8] c"P: Msg2\00"
@.str.3 = [8 x i8] c"P: Msg3\00"
@.str.4 = [8 x i8] c"C: Msg1\00"
@.str.5 = [8 x i8] c"C: Msg2\00"
@.str.6 = [8 x i8] c"C: Msg3\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
store i32 0, i32* %1
%3 = call i32 (...) @enable_waiting()
%4 = call i64 (...) @fork()
store i64 %4, i64* %2
%5 = load i64, i64* %2
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %32
9: ; preds = %0
%10 = load i64, i64* %2
%11 = icmp sgt i64 %10, 0
br i1 %11, label %12, label %22
12: ; preds = %9
%13 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 (...) @notify_child()
%15 = call i32 (...) @wait_for_child()
%16 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 (...) @notify_child()
%18 = call i32 (...) @wait_for_child()
%19 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%20 = call i32 (...) @notify_child()
%21 = call i32 @wait(i32* null)
br label %31
22: ; preds = %9
%23 = call i32 (...) @wait_for_parent()
%24 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%25 = call i32 (...) @notify_parent()
%26 = call i32 (...) @wait_for_parent()
%27 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%28 = call i32 (...) @notify_parent()
%29 = call i32 (...) @wait_for_parent()
%30 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %31
31: ; preds = %22, %12
store i32 0, i32* %1
br label %32
32: ; preds = %31, %7
%33 = load i32, i32* %1
ret i32 %33
}

LLVM IR (-O2):
@.str.1 = [8 x i8] c"P: Msg1\00"
@.str.2 = [8 x i8] c"P: Msg2\00"
@.str.3 = [8 x i8] c"P: Msg3\00"
@.str.4 = [8 x i8] c"C: Msg1\00"
@.str.5 = [8 x i8] c"C: Msg2\00"
@.str.6 = [8 x i8] c"C: Msg3\00"
@str = [12 x i8] c"\0Afork error\00"
define i32 @main() {
%1 = tail call i32 (...) @enable_waiting()
%2 = tail call i64 (...) @fork()
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %27
6: ; preds = %0
%7 = icmp eq i64 %2, 0
br i1 %7, label %18, label %8
8: ; preds = %6
%9 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 (...) @notify_child()
%11 = tail call i32 (...) @wait_for_child()
%12 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32 (...) @notify_child()
%14 = tail call i32 (...) @wait_for_child()
%15 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%16 = tail call i32 (...) @notify_child()
%17 = tail call i32 @wait(i32* null)
br label %27
18: ; preds = %6
%19 = tail call i32 (...) @wait_for_parent()
%20 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%21 = tail call i32 (...) @notify_parent()
%22 = tail call i32 (...) @wait_for_parent()
%23 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0))
%24 = tail call i32 (...) @notify_parent()
%25 = tail call i32 (...) @wait_for_parent()
%26 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
br label %27
27: ; preds = %8, %18, %4
%28 = phi i32 [ -1, %4 ], [ 0, %18 ], [ 0, %8 ]
ret i32 %28
}