#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*/);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
main() {
  if (fork() == 0) {
    if (fork() == 0) {
      if (fork() == 0) {
        printf("\nin child 3 running \"date\"\n");
        system("date");
        exit(0);
      } else {
        wait(0);
        printf("\nin child 2 running \"cal\"\n");
        system("cal");
        exit(0);
      }
    } else {
      wait(0);
      printf("\nin child 1 running \"pwd\"\n");
      system("pwd");
      exit(0);
    }
  } else {
    wait(0);
    printf("\nin parent code running \"ls\"\n");
    system("ls");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
mov    $0x0,%al
call   1070 <fork@plt>
cmp    $0x0,%rax
jne    11f2 <main+0xa2>
mov    $0x0,%al
call   1070 <fork@plt>
cmp    $0x0,%rax
jne    11cc <main+0x7c>
mov    $0x0,%al
call   1070 <fork@plt>
cmp    $0x0,%rax
jne    11a6 <main+0x56>
lea    0xe72(%rip),%rdi
call   1040 <printf@plt>
lea    0xe82(%rip),%rdi
call   1030 <system@plt>
xor    %edi,%edi
call   1050 <exit@plt>
xor    %edi,%edi
call   1060 <wait@plt>
lea    0xe6d(%rip),%rdi
call   1040 <printf@plt>
lea    0xe7c(%rip),%rdi
call   1030 <system@plt>
xor    %edi,%edi
call   1050 <exit@plt>
xor    %edi,%edi
call   1060 <wait@plt>
lea    0xe66(%rip),%rdi
call   1040 <printf@plt>
lea    0xe75(%rip),%rdi
call   1030 <system@plt>
xor    %edi,%edi
call   1050 <exit@plt>
xor    %edi,%edi
call   1060 <wait@plt>
lea    0xe5f(%rip),%rdi
call   1040 <printf@plt>
lea    0xe71(%rip),%rdi
call   1030 <system@plt>
xor    %eax,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
je     1180 <main+0x30>
xor    %edi,%edi
call   1060 <wait@plt>
lea    0xea5(%rip),%rdi
call   1030 <puts@plt>
lea    0xe96(%rip),%rdi
call   1040 <system@plt>
xor    %eax,%eax
pop    %rcx
ret
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jne    11ad <main+0x5d>
xor    %eax,%eax
call   1070 <fork@plt>
test   %rax,%rax
jne    11c9 <main+0x79>
lea    0xec2(%rip),%rdi
call   1030 <puts@plt>
lea    0xe55(%rip),%rdi
jmp    11e3 <main+0x93>
xor    %edi,%edi
call   1060 <wait@plt>
lea    0xe72(%rip),%rdi
call   1030 <puts@plt>
lea    0xe42(%rip),%rdi
jmp    11e3 <main+0x93>
xor    %edi,%edi
call   1060 <wait@plt>
lea    0xe70(%rip),%rdi
call   1030 <puts@plt>
lea    0xe22(%rip),%rdi
call   1040 <system@plt>
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"\0Ain child 3 running \22date\22\0A\00"
@.str.1 = [5 x i8] c"date\00"
@.str.2 = [27 x i8] c"\0Ain child 2 running \22cal\22\0A\00"
@.str.3 = [4 x i8] c"cal\00"
@.str.4 = [27 x i8] c"\0Ain child 1 running \22pwd\22\0A\00"
@.str.5 = [4 x i8] c"pwd\00"
@.str.6 = [30 x i8] c"\0Ain parent code running \22ls\22\0A\00"
@.str.7 = [3 x i8] c"ls\00"
define i32 @main() {
%1 = call i64 (...) @fork()
%2 = icmp eq i64 %1, 0
br i1 %2, label %3, label %23
3: ; preds = %0
%4 = call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %18
6: ; preds = %3
%7 = call i64 (...) @fork()
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %6
%10 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 0)
unreachable
13: ; preds = %6
%14 = call i32 @wait(i32 0)
%15 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 @system(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%17 = call i32 @exit(i32 0)
unreachable
18: ; preds = %3
%19 = call i32 @wait(i32 0)
%20 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%21 = call i32 @system(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %0
%24 = call i32 @wait(i32 0)
%25 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
%26 = call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
br label %27
27: ; preds = %23
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [5 x i8] c"date\00"
@.str.3 = [4 x i8] c"cal\00"
@.str.5 = [4 x i8] c"pwd\00"
@.str.7 = [3 x i8] c"ls\00"
@str = [29 x i8] c"\0Ain parent code running \22ls\22\00"
@str.8 = [26 x i8] c"\0Ain child 1 running \22pwd\22\00"
@str.9 = [26 x i8] c"\0Ain child 2 running \22cal\22\00"
@str.10 = [27 x i8] c"\0Ain child 3 running \22date\22\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp eq i64 %1, 0
br i1 %2, label %3, label %23
3: ; preds = %0
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %18
6: ; preds = %3
%7 = tail call i64 (...) @fork()
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %6
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.10, i64 0, i64 0))
%11 = tail call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @exit(i32 0)
unreachable
13: ; preds = %6
%14 = tail call i32 @wait(i32 0)
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.9, i64 0, i64 0))
%16 = tail call i32 @system(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%17 = tail call i32 @exit(i32 0)
unreachable
18: ; preds = %3
%19 = tail call i32 @wait(i32 0)
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.8, i64 0, i64 0))
%21 = tail call i32 @system(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
%22 = tail call i32 @exit(i32 0)
unreachable
23: ; preds = %0
%24 = tail call i32 @wait(i32 0)
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%26 = tail call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
ret i32 0
}