#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 WEXITSTATUS(int);
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ execlp(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ usleep(int);
int waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int it = 1;
  while (argv[it] != NULL) {
    if (fork() == 0) {
      execlp(argv[it], argv[it], NULL);
      printf("Failed to run %s.\n", argv[it]);
      exit(-1);
    }
    it++;
  }
  int status;
  while (1) {
    int ret = waitpid(-1, &status, WNOHANG);
    if (ret == -1) {
      printf("All children ran successfully.\n");
      break;
    } else if (ret == 0) {
      usleep(100);
    } else {
      printf("Child %d existed with code %d.\n", ret, WEXITSTATUS(status));
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1204 <main+0x94>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%rax
jne    11f6 <main+0x86>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <execlp@plt>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    118d <main+0x1d>
jmp    1209 <main+0x99>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x18(%rbp),%rsi
call   1050 <waitpid@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1240 <main+0xd0>
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1284 <main+0x114>
cmpl   $0x0,-0x1c(%rbp)
jne    1259 <main+0xe9>
mov    $0x64,%edi
call   1090 <usleep@plt>
jmp    127a <main+0x10a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    -0x20(%rbp),%esi
mov    %eax,%edx
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    127f <main+0x10f>
jmp    1209 <main+0x99>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmpq   $0x0,0x8(%rsi)
je     11b8 <main+0x38>
mov    %rsi,%rbx
mov    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     1227 <main+0xa7>
cmpq   $0x0,0x8(%rbx,%rbp,8)
lea    0x1(%rbp),%rbp
jne    11a0 <main+0x20>
mov    0x2e29(%rip),%r15
lea    0x4(%rsp),%rbx
lea    0xe48(%rip),%r14
jmp    11da <main+0x5a>
nopl   (%rax)
mov    $0x64,%edi
call   10a0 <usleep@plt>
mov    (%r15),%edx
mov    $0xffffffff,%edi
mov    %rbx,%rsi
call   1060 <waitpid@plt>
test   %eax,%eax
je     11d0 <main+0x50>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     120e <main+0x8e>
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11da <main+0x5a>
lea    0xe1e(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%ebp
mov    (%rbx,%rbp,8),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1080 <execlp@plt>
mov    (%rbx,%rbp,8),%rsi
lea    0xdbe(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"Failed to run %s.\0A\00"
@WNOHANG = global i32 0
@.str.1 = [32 x i8] c"All children ran successfully.\0A\00"
@.str.2 = [32 x i8] c"Child %d existed with code %d.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %6
br label %9
9: ; preds = %38, %2
%10 = load i8**, i8*** %5
%11 = load i32, i32* %6
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8*, i8** %10, i64 %12
%14 = load i8*, i8** %13
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %41
16: ; preds = %9
%17 = call i64 (...) @fork()
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %38
19: ; preds = %16
%20 = load i8**, i8*** %5
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8*, i8** %20, i64 %22
%24 = load i8*, i8** %23
%25 = load i8**, i8*** %5
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8*, i8** %25, i64 %27
%29 = load i8*, i8** %28
%30 = call i32 @execlp(i8* %24, i8* %29, i32* null)
%31 = load i8**, i8*** %5
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8*, i8** %31, i64 %33
%35 = load i8*, i8** %34
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %35)
%37 = call i32 @exit(i32 -1)
unreachable
38: ; preds = %16
%39 = load i32, i32* %6
%40 = add nsw i32 %39, 1
store i32 %40, i32* %6
br label %9
41: ; preds = %9
br label %42
42: ; preds = %41, %60
%43 = load i32, i32* @WNOHANG
%44 = call i32 @waitpid(i32 -1, i32* %7, i32 %43)
store i32 %44, i32* %8
%45 = load i32, i32* %8
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %49
47: ; preds = %42
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
br label %61
49: ; preds = %42
%50 = load i32, i32* %8
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = call i32 @usleep(i32 100)
br label %59
54: ; preds = %49
%55 = load i32, i32* %8
%56 = load i32, i32* %7
%57 = call i32 @WEXITSTATUS(i32 %56)
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %55, i32 %57)
br label %59
59: ; preds = %54, %52
br label %60
60: ; preds = %59
br label %42
61: ; preds = %47
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
@.str = [19 x i8] c"Failed to run %s.\0A\00"
@WNOHANG = global i32 0
@.str.2 = [32 x i8] c"Child %d existed with code %d.\0A\00"
@str = [31 x i8] c"All children ran successfully.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = icmp eq i8* %5, null
br i1 %6, label %24, label %7
7: ; preds = %2, %19
%8 = phi i64 [ %20, %19 ], [ 1, %2 ]
%9 = tail call i64 (...) @fork()
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %19
11: ; preds = %7
%12 = and i64 %8, 4294967295
%13 = getelementptr inbounds i8*, i8** %1, i64 %12
%14 = load i8*, i8** %13
%15 = tail call i32 @execlp(i8* %14, i8* %14, i32* null)
%16 = load i8*, i8** %13
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = tail call i32 @exit(i32 -1)
unreachable
19: ; preds = %7
%20 = add nuw i64 %8, 1
%21 = getelementptr inbounds i8*, i8** %1, i64 %20
%22 = load i8*, i8** %21
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %7
24: ; preds = %19, %2
%25 = bitcast i32* %3 to i8*
br label %26
26: ; preds = %35, %24
%27 = load i32, i32* @WNOHANG
%28 = call i32 @waitpid(i32 -1, i32* %3, i32 %27)
switch i32 %28, label %31 [
i32 -1, label %36
i32 0, label %29
]
29: ; preds = %26
%30 = call i32 @usleep(i32 100)
br label %35
31: ; preds = %26
%32 = load i32, i32* %3
%33 = call i32 @WEXITSTATUS(i32 %32)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %28, i32 %33)
br label %35
35: ; preds = %31, %29
br label %26
36: ; preds = %26
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
ret i32 0
}