#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*/ exit(int /*<<< orphan*/);
int fork();
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int main() {
  printf("I am the parent, my pid is: %d\n", getpid());
  int a = 10, b = 25, pid = 0;
  pid = fork();
  if (pid == 0) {
    a = a + b;
    printf("I am child, a is: %d\n", a);
    printf("I am child, b is: %d\n", b);
    printf("I am child, current pid is: %d\n", pid);
    pid = fork();
    if (pid != 0) {
      b = b + 20;
      printf("I am second parent, a is: %d\n", a);
      printf("I am second parent, b is: %d\n", b);
      printf("I am second parent, my current child pid is: %d\n", pid);
      exit(0);
    } else {
      a = (a * b) + 30;
      printf("I am second child, a is: %d\n", a);
      printf("I am second child, b is: %d\n", b);
      printf("I am second child, current pid is: %d\n", pid);
      printf("\n");
    }
    exit(0);
  } else {
    printf("I just forked. I am parent: %d\n", getpid());
    b = a + b - 5;
    printf("I am the top parent, bottom else\n");
    printf("I am the top parent, a is: %d\n", a);
    printf("I am the top parent, b is: %d\n", b);
    printf("I am the top parent, my current child pid is: %d\n", pid);
    printf("\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xa,-0x8(%rbp)
movl   $0x19,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    1277 <main+0x137>
mov    -0x8(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%esi
lea    0xe83(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1060 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1222 <main+0xe2>
mov    -0xc(%rbp),%eax
add    $0x14,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%esi
lea    0xe87(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xe94(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>
mov    -0x8(%rbp),%eax
imul   -0xc(%rbp),%eax
add    $0x1e,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xea0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%esi
lea    0xeac(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xeb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xeb3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%eax
add    -0xc(%rbp),%eax
sub    $0x5,%eax
mov    %eax,-0xc(%rbp)
lea    0xeb9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%esi
lea    0xeca(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%esi
lea    0xed8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xee6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xe91(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11ec <main+0x8c>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getpid@plt>
lea    0xfa7(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0x1027(%rip),%rdi
call   1040 <puts@plt>
lea    0xfab(%rip),%rdi
mov    $0xa,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xfb7(%rip),%rdi
mov    $0x1e,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xfc3(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe2d(%rip),%rdi
mov    $0x23,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe30(%rip),%rdi
mov    $0x19,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe33(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
jne    1274 <main+0x114>
lea    0xea5(%rip),%rdi
mov    $0x389,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xeaf(%rip),%rdi
mov    $0x19,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xeb9(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    %eax,%ebx
lea    0xdef(%rip),%rdi
mov    $0x23,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdfa(%rip),%rdi
mov    $0x2d,%esi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe05(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"I am the parent, my pid is: %d\0A\00"
@.str.1 = [22 x i8] c"I am child, a is: %d\0A\00"
@.str.2 = [22 x i8] c"I am child, b is: %d\0A\00"
@.str.3 = [32 x i8] c"I am child, current pid is: %d\0A\00"
@.str.4 = [30 x i8] c"I am second parent, a is: %d\0A\00"
@.str.5 = [30 x i8] c"I am second parent, b is: %d\0A\00"
@.str.6 = [49 x i8] c"I am second parent, my current child pid is: %d\0A\00"
@.str.7 = [29 x i8] c"I am second child, a is: %d\0A\00"
@.str.8 = [29 x i8] c"I am second child, b is: %d\0A\00"
@.str.9 = [39 x i8] c"I am second child, current pid is: %d\0A\00"
@.str.10 = [2 x i8] c"\0A\00"
@.str.11 = [32 x i8] c"I just forked. I am parent: %d\0A\00"
@.str.12 = [34 x i8] c"I am the top parent, bottom else\0A\00"
@.str.13 = [31 x i8] c"I am the top parent, a is: %d\0A\00"
@.str.14 = [31 x i8] c"I am the top parent, b is: %d\0A\00"
@.str.15 = [50 x i8] c"I am the top parent, my current child pid is: %d\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 (...) @getpid()
%6 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %5)
store i32 10, i32* %2
store i32 25, i32* %3
store i32 0, i32* %4
%7 = call i32 (...) @fork()
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %47
10: ; preds = %0
%11 = load i32, i32* %2
%12 = load i32, i32* %3
%13 = add nsw i32 %11, %12
store i32 %13, i32* %2
%14 = load i32, i32* %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = load i32, i32* %3
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = load i32, i32* %4
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%20 = call i32 (...) @fork()
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %33
23: ; preds = %10
%24 = load i32, i32* %3
%25 = add nsw i32 %24, 20
store i32 %25, i32* %3
%26 = load i32, i32* %2
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %3
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %4
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.6, i64 0, i64 0), i32 %30)
%32 = call i32 @exit(i32 0)
unreachable
33: ; preds = %10
%34 = load i32, i32* %2
%35 = load i32, i32* %3
%36 = mul nsw i32 %34, %35
%37 = add nsw i32 %36, 30
store i32 %37, i32* %2
%38 = load i32, i32* %2
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %3
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %4
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.9, i64 0, i64 0), i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0))
br label %45
45: ; preds = %33
%46 = call i32 @exit(i32 0)
unreachable
47: ; preds = %0
%48 = call i32 (...) @getpid()
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.11, i64 0, i64 0), i32 %48)
%50 = load i32, i32* %2
%51 = load i32, i32* %3
%52 = add nsw i32 %50, %51
%53 = sub nsw i32 %52, 5
store i32 %53, i32* %3
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.12, i64 0, i64 0))
%55 = load i32, i32* %2
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.13, i64 0, i64 0), i32 %55)
%57 = load i32, i32* %3
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.14, i64 0, i64 0), i32 %57)
%59 = load i32, i32* %4
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.15, i64 0, i64 0), i32 %59)
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0))
br label %62
62: ; preds = %47
ret i32 0
}

LLVM IR (-O2):
@.str = [32 x i8] c"I am the parent, my pid is: %d\0A\00"
@.str.1 = [22 x i8] c"I am child, a is: %d\0A\00"
@.str.2 = [22 x i8] c"I am child, b is: %d\0A\00"
@.str.3 = [32 x i8] c"I am child, current pid is: %d\0A\00"
@.str.4 = [30 x i8] c"I am second parent, a is: %d\0A\00"
@.str.5 = [30 x i8] c"I am second parent, b is: %d\0A\00"
@.str.6 = [49 x i8] c"I am second parent, my current child pid is: %d\0A\00"
@.str.7 = [29 x i8] c"I am second child, a is: %d\0A\00"
@.str.8 = [29 x i8] c"I am second child, b is: %d\0A\00"
@.str.9 = [39 x i8] c"I am second child, current pid is: %d\0A\00"
@.str.11 = [32 x i8] c"I just forked. I am parent: %d\0A\00"
@.str.13 = [31 x i8] c"I am the top parent, a is: %d\0A\00"
@.str.14 = [31 x i8] c"I am the top parent, b is: %d\0A\00"
@.str.15 = [50 x i8] c"I am the top parent, my current child pid is: %d\0A\00"
@str = [33 x i8] c"I am the top parent, bottom else\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = tail call i32 (...) @fork()
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %22
5: ; preds = %0
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 35)
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 25)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 0)
%9 = tail call i32 (...) @fork()
%10 = icmp eq i32 %9, 0
br i1 %10, label %16, label %11
11: ; preds = %5
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i32 35)
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 45)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.6, i64 0, i64 0), i32 %9)
%15 = tail call i32 @exit(i32 0)
unreachable
16: ; preds = %5
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.7, i64 0, i64 0), i32 905)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0), i32 25)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.9, i64 0, i64 0), i32 0)
%20 = tail call i32 @putchar(i32 10)
%21 = tail call i32 @exit(i32 0)
unreachable
22: ; preds = %0
%23 = tail call i32 (...) @getpid()
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.11, i64 0, i64 0), i32 %23)
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.13, i64 0, i64 0), i32 10)
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.14, i64 0, i64 0), i32 30)
%28 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str.15, i64 0, i64 0), i32 %3)
%29 = tail call i32 @putchar(i32 10)
ret i32 0
}