#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 */

/* Variables and functions */
int /*<<< orphan*/ consumer();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ initShared();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ producer();
int wait(int /*<<< orphan*/);
int main() {
  initShared();
  for (int i = 0; i < 2; i++) {
    switch (fork()) {
    case 0:
      producer();
      exit(0);
    default:
      break;
    }
  }
  for (int i = 0; i < 2; i++) {
    switch (fork()) {
    case 0:
      consumer();
      exit(0);
    default:
      break;
    }
  }
  for (int i = 0; i < 2 + 2; i++) {
    if (wait(0) == -1) {
      printf("%d finished\n", i);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1050 <initShared@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11d1 <main+0x61>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
jne    11b9 <main+0x49>
jmp    11ab <main+0x3b>
mov    $0x0,%al
call   1030 <producer@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    11be <main+0x4e>
jmp    11c3 <main+0x53>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    118d <main+0x1d>
movl   $0x0,-0xc(%rbp)
cmpl   $0x2,-0xc(%rbp)
jge    121c <main+0xac>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
jne    1204 <main+0x94>
jmp    11f6 <main+0x86>
mov    $0x0,%al
call   1060 <consumer@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    1209 <main+0x99>
jmp    120e <main+0x9e>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11d8 <main+0x68>
movl   $0x0,-0x10(%rbp)
cmpl   $0x4,-0x10(%rbp)
jge    125f <main+0xef>
xor    %edi,%edi
call   1080 <wait@plt>
cmp    $0xffffffff,%eax
jne    124c <main+0xdc>
mov    -0x10(%rbp),%esi
lea    0xdb9(%rip),%rdi
call   1040 <printf@plt>
jmp    1251 <main+0xe1>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1223 <main+0xb3>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1050 <initShared@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     1229 <main+0xb9>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     1229 <main+0xb9>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     1237 <main+0xc7>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     1237 <main+0xc7>
xor    %edi,%edi
call   1080 <wait@plt>
cmp    $0xffffffff,%eax
jne    11ce <main+0x5e>
lea    0xe39(%rip),%rdi
xor    %esi,%esi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <wait@plt>
cmp    $0xffffffff,%eax
jne    11eb <main+0x7b>
lea    0xe1f(%rip),%rdi
mov    $0x1,%esi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <wait@plt>
cmp    $0xffffffff,%eax
jne    1208 <main+0x98>
lea    0xe02(%rip),%rdi
mov    $0x2,%esi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <wait@plt>
cmp    $0xffffffff,%eax
jne    1225 <main+0xb5>
lea    0xde5(%rip),%rdi
mov    $0x3,%esi
call   1040 <printf@plt>
xor    %eax,%eax
pop    %rcx
ret
xor    %eax,%eax
call   1030 <producer@plt>
xor    %edi,%edi
call   1070 <exit@plt>
xor    %eax,%eax
call   1060 <consumer@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"%d finished\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 (...) @initShared()
store i32 0, i32* %2
br label %6
6: ; preds = %16, %0
%7 = load i32, i32* %2
%8 = icmp slt i32 %7, 2
br i1 %8, label %9, label %19
9: ; preds = %6
%10 = call i32 (...) @fork()
switch i32 %10, label %14 [
i32 0, label %11
]
11: ; preds = %9
%12 = call i32 (...) @producer()
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %9
br label %15
15: ; preds = %14
br label %16
16: ; preds = %15
%17 = load i32, i32* %2
%18 = add nsw i32 %17, 1
store i32 %18, i32* %2
br label %6
19: ; preds = %6
store i32 0, i32* %3
br label %20
20: ; preds = %30, %19
%21 = load i32, i32* %3
%22 = icmp slt i32 %21, 2
br i1 %22, label %23, label %33
23: ; preds = %20
%24 = call i32 (...) @fork()
switch i32 %24, label %28 [
i32 0, label %25
]
25: ; preds = %23
%26 = call i32 (...) @consumer()
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %23
br label %29
29: ; preds = %28
br label %30
30: ; preds = %29
%31 = load i32, i32* %3
%32 = add nsw i32 %31, 1
store i32 %32, i32* %3
br label %20
33: ; preds = %20
store i32 0, i32* %4
br label %34
34: ; preds = %44, %33
%35 = load i32, i32* %4
%36 = icmp slt i32 %35, 4
br i1 %36, label %37, label %47
37: ; preds = %34
%38 = call i32 @wait(i32 0)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i32, i32* %4
%42 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %41)
br label %43
43: ; preds = %40, %37
br label %44
44: ; preds = %43
%45 = load i32, i32* %4
%46 = add nsw i32 %45, 1
store i32 %46, i32* %4
br label %34
47: ; preds = %34
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"%d finished\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @initShared()
%2 = tail call i32 (...) @fork()
%3 = icmp eq i32 %2, 0
br i1 %3, label %10, label %4
4: ; preds = %0
%5 = tail call i32 (...) @fork()
%6 = icmp eq i32 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %4
%8 = tail call i32 (...) @fork()
%9 = icmp eq i32 %8, 0
br i1 %9, label %19, label %13
10: ; preds = %4, %0
%11 = tail call i32 (...) @producer()
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %7
%14 = tail call i32 (...) @fork()
%15 = icmp eq i32 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = tail call i32 @wait(i32 0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %22, label %24
19: ; preds = %13, %7
%20 = tail call i32 (...) @consumer()
%21 = tail call i32 @exit(i32 0)
unreachable
22: ; preds = %16
%23 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 0)
br label %24
24: ; preds = %16, %22
%25 = tail call i32 @wait(i32 0)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 1)
br label %29
29: ; preds = %27, %24
%30 = tail call i32 @wait(i32 0)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 2)
br label %34
34: ; preds = %32, %29
%35 = tail call i32 @wait(i32 0)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 3)
br label %39
39: ; preds = %37, %34
ret i32 0
}