#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*/ execl(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int getpid();
int getppid();
int /*<<< orphan*/ printf(char *, int, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
main() {
  printf("in parent pid=%d  ppid=%d\n", getpid(), getppid());
  if (fork() == 0) {
    printf("in child %d ppid=%d\n", getpid(), getppid());
    execl("/bin/ls", "ls", NULL);
    exit(1);
  } else {
    wait(0);
    if (fork() == 0) {
      printf("in child %d ppid=%d\n", getpid(), getppid());
      execl("/bin/pwd", "pwd", NULL);
      exit(2);
    } else {
      wait(0);
      if (fork() == 0) {
        printf("in child %d ppid=%d\n", getpid(), getppid());
        execl("/usr/bin/cal", "cal", NULL);
        exit(3);
      } else {
        wait(0);
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x4(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x4(%rbp),%esi
mov    %eax,%edx
lea    0xe6b(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    11ee <main+0x7e>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x8(%rbp),%esi
mov    %eax,%edx
lea    0xe53(%rip),%rdi
call   1040 <printf@plt>
lea    0xe5c(%rip),%rdi
lea    0xe5a(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <execl@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
xor    %edi,%edi
call   1080 <wait@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    1249 <main+0xd9>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0xc(%rbp),%esi
mov    %eax,%edx
lea    0xdf8(%rip),%rdi
call   1040 <printf@plt>
lea    0xe09(%rip),%rdi
lea    0xe07(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <execl@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
xor    %edi,%edi
call   1080 <wait@plt>
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%rax
jne    12a4 <main+0x134>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1050 <getppid@plt>
mov    -0x10(%rbp),%esi
mov    %eax,%edx
lea    0xd9d(%rip),%rdi
call   1040 <printf@plt>
lea    0xdb7(%rip),%rdi
lea    0xdb9(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <execl@plt>
mov    $0x3,%edi
call   1060 <exit@plt>
xor    %edi,%edi
call   1080 <wait@plt>
jmp    12b0 <main+0x140>
jmp    12b5 <main+0x145>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xe78(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     11d2 <main+0x62>
xor    %edi,%edi
call   1080 <wait@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     1211 <main+0xa1>
xor    %edi,%edi
call   1080 <wait@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     1250 <main+0xe0>
xor    %edi,%edi
call   1080 <wait@plt>
xor    %eax,%eax
pop    %rbx
ret
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xe32(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
lea    0xe37(%rip),%rdi
lea    0xe35(%rip),%rsi
xor    %edx,%edx
call   1070 <execl@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xdf3(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
lea    0xe00(%rip),%rdi
lea    0xdfe(%rip),%rsi
xor    %edx,%edx
call   1070 <execl@plt>
mov    $0x2,%edi
call   1060 <exit@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getppid@plt>
lea    0xdb4(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <printf@plt>
lea    0xdca(%rip),%rdi
lea    0xdcc(%rip),%rsi
xor    %edx,%edx
call   1070 <execl@plt>
mov    $0x3,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"in parent pid=%d ppid=%d\0A\00"
@.str.1 = [21 x i8] c"in child %d ppid=%d\0A\00"
@.str.2 = [8 x i8] c"/bin/ls\00"
@.str.3 = [3 x i8] c"ls\00"
@.str.4 = [9 x i8] c"/bin/pwd\00"
@.str.5 = [4 x i8] c"pwd\00"
@.str.6 = [13 x i8] c"/usr/bin/cal\00"
@.str.7 = [4 x i8] c"cal\00"
define i32 @main() {
%1 = call i32 (...) @getpid()
%2 = call i32 (...) @getppid()
%3 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %2)
%4 = call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %0
%7 = call i32 (...) @getpid()
%8 = call i32 (...) @getppid()
%9 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %7, i32 %8)
%10 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* null)
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = call i32 @wait(i32 0)
%14 = call i64 (...) @fork()
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = call i32 (...) @getpid()
%18 = call i32 (...) @getppid()
%19 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %18)
%20 = call i32 @execl(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32* null)
%21 = call i32 @exit(i32 2)
unreachable
22: ; preds = %12
%23 = call i32 @wait(i32 0)
%24 = call i64 (...) @fork()
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %22
%27 = call i32 (...) @getpid()
%28 = call i32 (...) @getppid()
%29 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %27, i32 %28)
%30 = call i32 @execl(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32* null)
%31 = call i32 @exit(i32 3)
unreachable
32: ; preds = %22
%33 = call i32 @wait(i32 0)
br label %34
34: ; preds = %32
br label %35
35: ; preds = %34
br label %36
36: ; preds = %35
ret i32 0
}

LLVM IR (-O2):
@.str = [27 x i8] c"in parent pid=%d ppid=%d\0A\00"
@.str.1 = [21 x i8] c"in child %d ppid=%d\0A\00"
@.str.2 = [8 x i8] c"/bin/ls\00"
@.str.3 = [3 x i8] c"ls\00"
@.str.4 = [9 x i8] c"/bin/pwd\00"
@.str.5 = [4 x i8] c"pwd\00"
@.str.6 = [13 x i8] c"/usr/bin/cal\00"
@.str.7 = [4 x i8] c"cal\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 (...) @getppid()
%3 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %2)
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %0
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 (...) @getppid()
%9 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %7, i32 %8)
%10 = tail call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* null)
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = tail call i32 @wait(i32 0)
%14 = tail call i64 (...) @fork()
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = tail call i32 (...) @getpid()
%18 = tail call i32 (...) @getppid()
%19 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %18)
%20 = tail call i32 @execl(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32* null)
%21 = tail call i32 @exit(i32 2)
unreachable
22: ; preds = %12
%23 = tail call i32 @wait(i32 0)
%24 = tail call i64 (...) @fork()
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %22
%27 = tail call i32 (...) @getpid()
%28 = tail call i32 (...) @getppid()
%29 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %27, i32 %28)
%30 = tail call i32 @execl(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32* null)
%31 = tail call i32 @exit(i32 3)
unreachable
32: ; preds = %22
%33 = tail call i32 @wait(i32 0)
ret i32 0
}