#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__ execl(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/);
int main() {
  int i, p;
  char *cmd[] = {"./p2bq", "/bin/ls", "/bin/ps"};
  printf("Parent START\n");
  for (i = 0; i < 3; i++) {
    p = fork();
    if (p == 0) {
      if (execl(cmd[i], cmd[i], NULL) < 0) {
        perror("Exec failed");
      }
      exit(0);
    }
  }
  printf("Parent MIDDLE\n");
  for (i = 0; i < 3; i++) {
    wait(0);
  }
  printf("Parent END\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2c8a(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2c87(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2c84(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe80(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    120c <main+0xac>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11f9 <main+0x99>
movslq -0x8(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rdi
movslq -0x8(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <execl@plt>
cmp    $0x0,%rax
jge    11f2 <main+0x92>
lea    0xe38(%rip),%rdi
call   1040 <perror@plt>
xor    %edi,%edi
call   1050 <exit@plt>
jmp    11fe <main+0x9e>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11a3 <main+0x43>
lea    0xe1e(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    123e <main+0xde>
xor    %edi,%edi
call   1070 <wait@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    121f <main+0xbf>
lea    0xdfb(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xebb(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11cd <main+0x6d>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11c1 <main+0x61>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11c8 <main+0x68>
lea    0xe99(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <wait@plt>
xor    %edi,%edi
call   1070 <wait@plt>
xor    %edi,%edi
call   1070 <wait@plt>
lea    0xe86(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    $0x1,%ebx
jmp    11cd <main+0x6d>
mov    $0x2,%ebx
lea    0x2c2c(%rip),%rax
mov    (%rax,%rbx,8),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1060 <execl@plt>
test   %rax,%rax
jns    11f3 <main+0x93>
lea    0xe29(%rip),%rdi
call   1040 <perror@plt>
xor    %edi,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"./p2bq\00"
@.str.1 = [8 x i8] c"/bin/ls\00"
@.str.2 = [8 x i8] c"/bin/ps\00"
@__const.main.cmd = [3 x i8*] [i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i32 0, i32 0)]
@.str.3 = [14 x i8] c"Parent START\0A\00"
@.str.4 = [12 x i8] c"Exec failed\00"
@.str.5 = [15 x i8] c"Parent MIDDLE\0A\00"
@.str.6 = [12 x i8] c"Parent END\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [3 x i8*]
store i32 0, i32* %1
%5 = bitcast [3 x i8*]* %4 to i8*
%6 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %2
br label %7
7: ; preds = %30, %0
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 3
br i1 %9, label %10, label %33
10: ; preds = %7
%11 = call i32 (...) @fork()
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %29
14: ; preds = %10
%15 = load i32, i32* %2
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 %16
%18 = load i8*, i8** %17
%19 = load i32, i32* %2
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 %20
%22 = load i8*, i8** %21
%23 = call i64 @execl(i8* %18, i8* %22, i32* null)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %14
%26 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %27
27: ; preds = %25, %14
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %10
br label %30
30: ; preds = %29
%31 = load i32, i32* %2
%32 = add nsw i32 %31, 1
store i32 %32, i32* %2
br label %7
33: ; preds = %7
%34 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %2
br label %35
35: ; preds = %40, %33
%36 = load i32, i32* %2
%37 = icmp slt i32 %36, 3
br i1 %37, label %38, label %43
38: ; preds = %35
%39 = call i32 @wait(i32 0)
br label %40
40: ; preds = %38
%41 = load i32, i32* %2
%42 = add nsw i32 %41, 1
store i32 %42, i32* %2
br label %35
43: ; preds = %35
%44 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"./p2bq\00"
@.str.1 = [8 x i8] c"/bin/ls\00"
@.str.2 = [8 x i8] c"/bin/ps\00"
@__const.main.cmd = [3 x i8*] [i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i32 0, i32 0)]
@.str.4 = [12 x i8] c"Exec failed\00"
@str = [13 x i8] c"Parent START\00"
@str.7 = [14 x i8] c"Parent MIDDLE\00"
@str.8 = [11 x i8] c"Parent END\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 (...) @fork()
%3 = icmp eq i32 %2, 0
br i1 %3, label %4, label %14
4: ; preds = %17, %14, %0
%5 = phi i64 [ 0, %0 ], [ 1, %14 ], [ 2, %17 ]
%6 = getelementptr inbounds [3 x i8*], [3 x i8*]* @__const.main.cmd, i64 0, i64 %5
%7 = load i8*, i8** %6
%8 = tail call i64 @execl(i8* %7, i8* %7, i32* null)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %4
%11 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %12
12: ; preds = %10, %4
%13 = tail call i32 @exit(i32 0)
unreachable
14: ; preds = %0
%15 = tail call i32 (...) @fork()
%16 = icmp eq i32 %15, 0
br i1 %16, label %4, label %17
17: ; preds = %14
%18 = tail call i32 (...) @fork()
%19 = icmp eq i32 %18, 0
br i1 %19, label %4, label %20
20: ; preds = %17
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.7, i64 0, i64 0))
%22 = tail call i32 @wait(i32 0)
%23 = tail call i32 @wait(i32 0)
%24 = tail call i32 @wait(i32 0)
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.8, i64 0, i64 0))
ret i32 0
}