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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1060 <fork@plt>
cmp    $0x0,%rax
jne    11ed <main+0xad>
mov    $0x0,%al
call   1060 <fork@plt>
cmp    $0x0,%rax
jne    11c6 <main+0x86>
mov    $0x0,%al
call   1060 <fork@plt>
cmp    $0x0,%rax
jne    119f <main+0x5f>
lea    0xe77(%rip),%rdi
call   1040 <printf@plt>
lea    0xe87(%rip),%rdi
call   1030 <system@plt>
jmp    11c1 <main+0x81>
mov    $0x5,%edi
call   1050 <sleep@plt>
lea    0xe71(%rip),%rdi
call   1040 <printf@plt>
lea    0xe80(%rip),%rdi
call   1030 <system@plt>
jmp    11e8 <main+0xa8>
mov    $0x3,%edi
call   1050 <sleep@plt>
lea    0xe69(%rip),%rdi
call   1040 <printf@plt>
lea    0xe78(%rip),%rdi
call   1030 <system@plt>
jmp    120f <main+0xcf>
mov    $0x9,%edi
call   1050 <sleep@plt>
lea    0xe61(%rip),%rdi
call   1040 <printf@plt>
lea    0xe73(%rip),%rdi
call   1030 <system@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
je     1165 <main+0x25>
lea    0xeb6(%rip),%r14
lea    0xeb2(%rip),%rbx
mov    $0x9,%edi
jmp    11a5 <main+0x65>
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
je     1186 <main+0x46>
lea    0xe91(%rip),%r14
lea    0xeae(%rip),%rbx
mov    $0x3,%edi
jmp    11a5 <main+0x65>
xor    %eax,%eax
call   1060 <fork@plt>
test   %rax,%rax
je     11c4 <main+0x84>
lea    0xe6c(%rip),%r14
lea    0xea7(%rip),%rbx
mov    $0x5,%edi
call   1050 <sleep@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
call   1040 <system@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe35(%rip),%r14
lea    0xe8f(%rip),%rbx
jmp    11aa <main+0x6a>

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 = alloca i32
store i32 0, i32* %1
%2 = call i64 (...) @fork()
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %23
4: ; preds = %0
%5 = call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %18
7: ; preds = %4
%8 = call i64 (...) @fork()
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %17
13: ; preds = %7
%14 = call i32 @sleep(i32 5)
%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))
br label %17
17: ; preds = %13, %10
br label %22
18: ; preds = %4
%19 = call i32 @sleep(i32 3)
%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))
br label %22
22: ; preds = %18, %17
br label %27
23: ; preds = %0
%24 = call i32 @sleep(i32 9)
%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, %22
%28 = load i32, i32* %1
ret i32 %28
}

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 %9
3: ; preds = %0
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i64 (...) @fork()
%8 = icmp eq i64 %7, 0
br i1 %8, label %14, label %9
9: ; preds = %0, %3, %6
%10 = phi i32 [ 5, %6 ], [ 3, %3 ], [ 9, %0 ]
%11 = phi i8* [ getelementptr inbounds ([26 x i8], [26 x i8]* @str.9, i64 0, i64 0), %6 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @str.8, i64 0, i64 0), %3 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0), %0 ]
%12 = phi i8* [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), %6 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), %3 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), %0 ]
%13 = tail call i32 @sleep(i32 %10)
br label %14
14: ; preds = %9, %6
%15 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @str.10, i64 0, i64 0), %6 ], [ %11, %9 ]
%16 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %6 ], [ %12, %9 ]
%17 = tail call i32 @puts(i8* dereferenceable(1) %15)
%18 = tail call i32 @system(i8* %16)
ret i32 0
}