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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1050 <fork@plt>
cmp    $0x0,%rax
jne    11c9 <main+0x99>
mov    $0x0,%al
call   1050 <fork@plt>
cmp    $0x0,%rax
jne    11ac <main+0x7c>
mov    $0x0,%al
call   1050 <fork@plt>
cmp    $0x0,%rax
jne    118f <main+0x5f>
lea    0xe87(%rip),%rdi
call   1040 <printf@plt>
lea    0xe96(%rip),%rdi
call   1030 <system@plt>
jmp    118a <main+0x5a>
lea    0xe8a(%rip),%rdi
call   1040 <printf@plt>
lea    0xe98(%rip),%rdi
call   1030 <system@plt>
jmp    11a7 <main+0x77>
lea    0xe8b(%rip),%rdi
call   1040 <printf@plt>
lea    0xe99(%rip),%rdi
call   1030 <system@plt>
jmp    11c4 <main+0x94>
lea    0xe8c(%rip),%rdi
call   1040 <printf@plt>
lea    0xe9a(%rip),%rdi
call   1030 <system@plt>
jmp    11e1 <main+0xb1>

x86 assembly (clang, -O2):
<main>:
push   %rax
xor    %eax,%eax
call   1050 <fork@plt>
test   %rax,%rax
je     1162 <main+0x32>
lea    0xecc(%rip),%rdi
call   1030 <puts@plt>
lea    0xebd(%rip),%rdi
call   1040 <system@plt>
cs nopw 0x0(%rax,%rax,1)
nop
jmp    1160 <main+0x30>
xor    %eax,%eax
call   1050 <fork@plt>
test   %rax,%rax
je     1192 <main+0x62>
lea    0xeb4(%rip),%rdi
call   1030 <puts@plt>
lea    0xe88(%rip),%rdi
call   1040 <system@plt>
cs nopw 0x0(%rax,%rax,1)
jmp    1190 <main+0x60>
xor    %eax,%eax
call   1050 <fork@plt>
test   %rax,%rax
je     11c2 <main+0x92>
lea    0xe9d(%rip),%rdi
call   1030 <puts@plt>
lea    0xe54(%rip),%rdi
call   1040 <system@plt>
cs nopw 0x0(%rax,%rax,1)
jmp    11c0 <main+0x90>
lea    0xe92(%rip),%rdi
call   1030 <puts@plt>
lea    0xe2b(%rip),%rdi
call   1040 <system@plt>
nopw   0x0(%rax,%rax,1)
jmp    11e0 <main+0xb0>

LLVM IR (-O0):
@.str = [27 x i8] c"in child 3 running \22date\22\0A\00"
@.str.1 = [5 x i8] c"date\00"
@.str.2 = [26 x i8] c"in child 2 running \22cal\22\0A\00"
@.str.3 = [4 x i8] c"cal\00"
@.str.4 = [26 x i8] c"in child 1 running \22pwd\22\0A\00"
@.str.5 = [4 x i8] c"pwd\00"
@.str.6 = [26 x i8] c"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 %22
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 %14
10: ; preds = %7
%11 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 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 %13
13: ; preds = %10, %13
br label %13
14: ; preds = %7
%15 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 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 = %14, %17
br label %17
18: ; preds = %4
%19 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%20 = call i32 @system(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
br label %21
21: ; preds = %18, %21
br label %21
22: ; preds = %0
%23 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%24 = call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
br label %25
25: ; preds = %22, %25
br label %25
}

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 = [25 x i8] c"parent code running \22ls\22\00"
@str.8 = [25 x i8] c"in child 1 running \22pwd\22\00"
@str.9 = [25 x i8] c"in child 2 running \22cal\22\00"
@str.10 = [26 x i8] c"in 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 %21
3: ; preds = %0
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %17
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 ([26 x i8], [26 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))
br label %12
12: ; preds = %9, %12
br label %12
13: ; preds = %6
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.9, i64 0, i64 0))
%15 = tail call i32 @system(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %16
16: ; preds = %13, %16
br label %16
17: ; preds = %3
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.8, i64 0, i64 0))
%19 = tail call i32 @system(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
br label %20
20: ; preds = %17, %20
br label %20
21: ; preds = %0
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%23 = tail call i32 @system(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
br label %24
24: ; preds = %21, %24
br label %24
}