#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 WEXITSTATUS(int);
int add(int, int);
int divi(int, int);
scalar_t__ fork();
int /*<<< orphan*/ printChild();
int /*<<< orphan*/ printParent();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main() {
  printf("Program begins here!\n");
  printParent();
  pid_t pid1, pid2, pid3;
  int a = 2, b = 2, c = 2, d = 2, e = 2, f = 2;
  int result, rtval1, rtval2, rtval3;
  pid1 = fork();
  if (pid1 == 0) {
    printChild();
    return add(a, b);
  } else {
    printf("I just finished forking pid1.\n");
    waitpid(-1, &rtval1, 0);
    rtval1 = WEXITSTATUS(rtval1);
    printParent();
    pid2 = fork();
    if (pid2 == 0) {
      printChild();
      return add(c, b);
    } else {
      printf("I just finished forking pid2.\n");
      waitpid(-1, &rtval2, 0);
      rtval2 = WEXITSTATUS(rtval2);
      printParent();
      pid3 = fork();
      if (pid3 == 0) {
        printChild();
        return divi(e, f);
      } else {
        printf("I just finished forking pid3.\n");
        waitpid(-1, &rtval3, 0);
        rtval3 = WEXITSTATUS(rtval3);
        printParent();
      }
    }
  }
  result = (rtval1 * rtval2) - rtval3;
  printf("The result of doing (%d + %d) * (%d + %d) - (%d / %d) is: %d\n", a, b,
         c, d, e, f, result);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   1060 <printParent@plt>
movl   $0x2,-0x24(%rbp)
movl   $0x2,-0x28(%rbp)
movl   $0x2,-0x2c(%rbp)
movl   $0x2,-0x30(%rbp)
movl   $0x2,-0x34(%rbp)
movl   $0x2,-0x38(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11fe <main+0x7e>
mov    $0x0,%al
call   1040 <printChild@plt>
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1030 <add@plt>
mov    %eax,-0x4(%rbp)
jmp    133a <main+0x1ba>
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
lea    -0x40(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    -0x40(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,-0x40(%rbp)
mov    $0x0,%al
call   1060 <printParent@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    125e <main+0xde>
mov    $0x0,%al
call   1040 <printChild@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1030 <add@plt>
mov    %eax,-0x4(%rbp)
jmp    133a <main+0x1ba>
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
lea    -0x44(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    -0x44(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,-0x44(%rbp)
mov    $0x0,%al
call   1060 <printParent@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12be <main+0x13e>
mov    $0x0,%al
call   1040 <printChild@plt>
mov    -0x34(%rbp),%edi
mov    -0x38(%rbp),%esi
call   1070 <divi@plt>
mov    %eax,-0x4(%rbp)
jmp    133a <main+0x1ba>
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
lea    -0x48(%rbp),%rsi
xor    %edx,%edx
call   1090 <waitpid@plt>
mov    -0x48(%rbp),%edi
call   1080 <WEXITSTATUS@plt>
mov    %eax,-0x48(%rbp)
mov    $0x0,%al
call   1060 <printParent@plt>
jmp    12f3 <main+0x173>
jmp    12f8 <main+0x178>
mov    -0x40(%rbp),%eax
imul   -0x44(%rbp),%eax
sub    -0x48(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x2c(%rbp),%ecx
mov    -0x30(%rbp),%r8d
mov    -0x34(%rbp),%r9d
mov    -0x38(%rbp),%r10d
mov    -0x3c(%rbp),%eax
lea    0xd4f(%rip),%rdi
mov    %r10d,(%rsp)
mov    %eax,0x8(%rsp)
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
lea    0xea1(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1070 <printParent@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     12ad <main+0x11d>
lea    0xe93(%rip),%rdi
call   1040 <puts@plt>
lea    0x14(%rsp),%rsi
mov    $0xffffffff,%edi
xor    %edx,%edx
call   10a0 <waitpid@plt>
mov    0x14(%rsp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,0x14(%rsp)
xor    %eax,%eax
call   1070 <printParent@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     12c9 <main+0x139>
lea    0xe70(%rip),%rdi
call   1040 <puts@plt>
lea    0x10(%rsp),%rsi
xor    %ebp,%ebp
mov    $0xffffffff,%edi
xor    %edx,%edx
call   10a0 <waitpid@plt>
mov    0x10(%rsp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,0x10(%rsp)
xor    %eax,%eax
call   1070 <printParent@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     12e1 <main+0x151>
lea    0xe4b(%rip),%rdi
call   1040 <puts@plt>
lea    0xc(%rsp),%rsi
mov    $0xffffffff,%edi
xor    %edx,%edx
call   10a0 <waitpid@plt>
mov    0xc(%rsp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,0xc(%rsp)
xor    %eax,%eax
call   1070 <printParent@plt>
mov    0x10(%rsp),%ebx
imul   0x14(%rsp),%ebx
sub    0xc(%rsp),%ebx
lea    0xd7e(%rip),%rdi
mov    $0x2,%esi
mov    $0x2,%edx
mov    $0x2,%ecx
mov    $0x2,%r8d
mov    $0x2,%r9d
xor    %eax,%eax
push   %rbx
push   $0x2
call   1060 <printf@plt>
add    $0x10,%rsp
jmp    12f9 <main+0x169>
xor    %eax,%eax
call   1050 <printChild@plt>
mov    $0x2,%edi
mov    $0x2,%esi
add    $0x18,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <add@plt>
xor    %eax,%eax
call   1050 <printChild@plt>
mov    $0x2,%edi
mov    $0x2,%esi
call   1030 <add@plt>
jmp    12f7 <main+0x167>
xor    %eax,%eax
call   1050 <printChild@plt>
mov    $0x2,%edi
mov    $0x2,%esi
call   1080 <divi@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"Program begins here!\0A\00"
@.str.1 = [31 x i8] c"I just finished forking pid1.\0A\00"
@.str.2 = [31 x i8] c"I just finished forking pid2.\0A\00"
@.str.3 = [31 x i8] c"I just finished forking pid3.\0A\00"
@.str.4 = [62 x i8] c"The result of doing (%d + %d) * (%d + %d) - (%d / %d) is: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %1
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%16 = call i32 (...) @printParent()
store i32 2, i32* %5
store i32 2, i32* %6
store i32 2, i32* %7
store i32 2, i32* %8
store i32 2, i32* %9
store i32 2, i32* %10
%17 = call i64 (...) @fork()
store i64 %17, i64* %2
%18 = load i64, i64* %2
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %0
%21 = call i32 (...) @printChild()
%22 = load i32, i32* %5
%23 = load i32, i32* %6
%24 = call i32 @add(i32 %22, i32 %23)
store i32 %24, i32* %1
br label %75
25: ; preds = %0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @waitpid(i32 -1, i32* %12, i32 0)
%28 = load i32, i32* %12
%29 = call i32 @WEXITSTATUS(i32 %28)
store i32 %29, i32* %12
%30 = call i32 (...) @printParent()
%31 = call i64 (...) @fork()
store i64 %31, i64* %3
%32 = load i64, i64* %3
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %25
%35 = call i32 (...) @printChild()
%36 = load i32, i32* %7
%37 = load i32, i32* %6
%38 = call i32 @add(i32 %36, i32 %37)
store i32 %38, i32* %1
br label %75
39: ; preds = %25
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @waitpid(i32 -1, i32* %13, i32 0)
%42 = load i32, i32* %13
%43 = call i32 @WEXITSTATUS(i32 %42)
store i32 %43, i32* %13
%44 = call i32 (...) @printParent()
%45 = call i64 (...) @fork()
store i64 %45, i64* %4
%46 = load i64, i64* %4
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %39
%49 = call i32 (...) @printChild()
%50 = load i32, i32* %9
%51 = load i32, i32* %10
%52 = call i32 @divi(i32 %50, i32 %51)
store i32 %52, i32* %1
br label %75
53: ; preds = %39
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%55 = call i32 @waitpid(i32 -1, i32* %14, i32 0)
%56 = load i32, i32* %14
%57 = call i32 @WEXITSTATUS(i32 %56)
store i32 %57, i32* %14
%58 = call i32 (...) @printParent()
br label %59
59: ; preds = %53
br label %60
60: ; preds = %59
br label %61
61: ; preds = %60
%62 = load i32, i32* %12
%63 = load i32, i32* %13
%64 = mul nsw i32 %62, %63
%65 = load i32, i32* %14
%66 = sub nsw i32 %64, %65
store i32 %66, i32* %11
%67 = load i32, i32* %5
%68 = load i32, i32* %6
%69 = load i32, i32* %7
%70 = load i32, i32* %8
%71 = load i32, i32* %9
%72 = load i32, i32* %10
%73 = load i32, i32* %11
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.4, i64 0, i64 0), i32 %67, i32 %68, i32 %69, i32 %70, i32 %71, i32 %72, i32 %73)
store i32 0, i32* %1
br label %75
75: ; preds = %61, %48, %34, %20
%76 = load i32, i32* %1
ret i32 %76
}

LLVM IR (-O2):
@.str.4 = [62 x i8] c"The result of doing (%d + %d) * (%d + %d) - (%d / %d) is: %d\0A\00"
@str = [21 x i8] c"Program begins here!\00"
@str.5 = [30 x i8] c"I just finished forking pid1.\00"
@str.6 = [30 x i8] c"I just finished forking pid2.\00"
@str.7 = [30 x i8] c"I just finished forking pid3.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 (...) @printParent()
%6 = bitcast i32* %1 to i8*
%7 = bitcast i32* %2 to i8*
%8 = bitcast i32* %3 to i8*
%9 = tail call i64 (...) @fork()
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = tail call i32 (...) @printChild()
%13 = tail call i32 @add(i32 2, i32 2)
br label %48
14: ; preds = %0
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.5, i64 0, i64 0))
%16 = call i32 @waitpid(i32 -1, i32* %1, i32 0)
%17 = load i32, i32* %1
%18 = call i32 @WEXITSTATUS(i32 %17)
store i32 %18, i32* %1
%19 = call i32 (...) @printParent()
%20 = call i64 (...) @fork()
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %14
%23 = call i32 (...) @printChild()
%24 = call i32 @add(i32 2, i32 2)
br label %48
25: ; preds = %14
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.6, i64 0, i64 0))
%27 = call i32 @waitpid(i32 -1, i32* %2, i32 0)
%28 = load i32, i32* %2
%29 = call i32 @WEXITSTATUS(i32 %28)
store i32 %29, i32* %2
%30 = call i32 (...) @printParent()
%31 = call i64 (...) @fork()
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %25
%34 = call i32 (...) @printChild()
%35 = call i32 @divi(i32 2, i32 2)
br label %48
36: ; preds = %25
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.7, i64 0, i64 0))
%38 = call i32 @waitpid(i32 -1, i32* %3, i32 0)
%39 = load i32, i32* %3
%40 = call i32 @WEXITSTATUS(i32 %39)
store i32 %40, i32* %3
%41 = call i32 (...) @printParent()
%42 = load i32, i32* %1
%43 = load i32, i32* %2
%44 = mul nsw i32 %43, %42
%45 = load i32, i32* %3
%46 = sub nsw i32 %44, %45
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([62 x i8], [62 x i8]* @.str.4, i64 0, i64 0), i32 2, i32 2, i32 2, i32 2, i32 2, i32 2, i32 %46)
br label %48
48: ; preds = %36, %33, %22, %11
%49 = phi i32 [ %13, %11 ], [ %24, %22 ], [ %35, %33 ], [ 0, %36 ]
ret i32 %49
}