#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 */
typedef scalar_t__ pid_t;

/* Variables and functions */
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ WNOHANG;
int errno;
int /*<<< orphan*/ execl(char *, char *, char *, char *, char *, char *, char *,
                         int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sched_yield();
int /*<<< orphan*/ stderr;
scalar_t__ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
main(int argc, char *argv[]) {
  pid_t pid = fork();
  if (pid) {
    while (1) {
      int status;
      pid_t cpid = waitpid(0, &status, WNOHANG);
      if (cpid > 0) {
        fprintf(stderr, "Child pid: %d\n", cpid);
        if (WIFEXITED(status)) {
          int res = WEXITSTATUS(status);
          fprintf(stderr, "Child exited with value %d\n", res);
          exit(res);
        } else {
          exit(1);
        }
      }
      if (cpid < 0) {
        perror("wait:");
        exit(1);
      }
      sched_yield();
    }
  } else {
    execl(argv[1], argv[1], argv[2], "--", argv[3], argv[4], argv[5], NULL);
    fprintf(stderr, "execl failed with errno: %d\n", errno);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     126c <main+0xdc>
jmp    11ba <main+0x2a>
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
lea    -0x1c(%rbp),%rsi
call   1060 <waitpid@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jle    123f <main+0xaf>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rax
mov    %eax,%edx
lea    0xe0d(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1235 <main+0xa5>
mov    -0x1c(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
lea    0xde7(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x2c(%rbp),%edi
call   1090 <exit@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
jge    1260 <main+0xd0>
lea    0xdda(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   1030 <sched_yield@plt>
jmp    11ba <main+0x2a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%r8
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%r9
mov    -0x10(%rbp),%rax
mov    0x28(%rax),%rax
lea    0xd8e(%rip),%rcx
xor    %r10d,%r10d
mov    %rax,(%rsp)
movq   $0x0,0x8(%rsp)
call   10a0 <execl@plt>
mov    0x2d19(%rip),%rax
mov    (%rax),%edi
mov    0x2d08(%rip),%rax
mov    (%rax),%edx
lea    0xd63(%rip),%rsi
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
jne    11f3 <main+0x63>
mov    0x8(%rbx),%rdi
mov    0x10(%rbx),%rdx
mov    0x18(%rbx),%r8
mov    0x20(%rbx),%r9
lea    0xe77(%rip),%rcx
mov    %rdi,%rsi
push   $0x0
push   0x28(%rbx)
call   10a0 <execl@plt>
add    $0x10,%rsp
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
lea    0xe50(%rip),%rsi
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2dee(%rip),%r14
mov    (%r14),%edx
lea    0x4(%rsp),%rsi
xor    %edi,%edi
call   1060 <waitpid@plt>
test   %rax,%rax
jg     123e <main+0xae>
lea    0x4(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
test   %rax,%rax
js     1291 <main+0x101>
xor    %eax,%eax
call   1030 <sched_yield@plt>
mov    (%r14),%edx
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <waitpid@plt>
test   %rax,%rax
jle    1220 <main+0x90>
mov    0x2d93(%rip),%r14
mov    (%r14),%edi
lea    0xdb1(%rip),%rsi
mov    %eax,%edx
call   1040 <fprintf@plt>
mov    0x4(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
jne    126e <main+0xde>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x4(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%ebx
mov    (%r14),%edi
lea    0xd8c(%rip),%rsi
mov    %eax,%edx
call   1040 <fprintf@plt>
mov    %ebx,%edi
call   1090 <exit@plt>
lea    0xd93(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@WNOHANG = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"Child pid: %d\0A\00"
@.str.1 = [28 x i8] c"Child exited with value %d\0A\00"
@.str.2 = [6 x i8] c"wait:\00"
@.str.3 = [3 x i8] c"--\00"
@.str.4 = [29 x i8] c"execl failed with errno: %d\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%9 = call i64 (...) @fork()
store i64 %9, i64* %5
%10 = load i64, i64* %5
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %44
12: ; preds = %2
br label %13
13: ; preds = %12, %42
%14 = load i32, i32* @WNOHANG
%15 = call i64 @waitpid(i32 0, i32* %6, i32 %14)
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = icmp sgt i64 %16, 0
br i1 %17, label %18, label %36
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = load i64, i64* %7
%21 = trunc i64 %20 to i32
%22 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %21)
%23 = load i32, i32* %6
%24 = call i64 @WIFEXITED(i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %18
%27 = load i32, i32* %6
%28 = call i32 @WEXITSTATUS(i32 %27)
store i32 %28, i32* %8
%29 = load i32, i32* @stderr
%30 = load i32, i32* %8
%31 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %8
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %18
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %13
%37 = load i64, i64* %7
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %36
%43 = call i32 (...) @sched_yield()
br label %13
44: ; preds = %2
%45 = load i8**, i8*** %4
%46 = getelementptr inbounds i8*, i8** %45, i64 1
%47 = load i8*, i8** %46
%48 = load i8**, i8*** %4
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
%51 = load i8**, i8*** %4
%52 = getelementptr inbounds i8*, i8** %51, i64 2
%53 = load i8*, i8** %52
%54 = load i8**, i8*** %4
%55 = getelementptr inbounds i8*, i8** %54, i64 3
%56 = load i8*, i8** %55
%57 = load i8**, i8*** %4
%58 = getelementptr inbounds i8*, i8** %57, i64 4
%59 = load i8*, i8** %58
%60 = load i8**, i8*** %4
%61 = getelementptr inbounds i8*, i8** %60, i64 5
%62 = load i8*, i8** %61
%63 = call i32 @execl(i8* %47, i8* %50, i8* %53, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %56, i8* %59, i8* %62, i32* null)
%64 = load i32, i32* @stderr
%65 = load i32, i32* @errno
%66 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %65)
br label %67
67: ; preds = %44
ret i32 0
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"Child pid: %d\0A\00"
@.str.1 = [28 x i8] c"Child exited with value %d\0A\00"
@.str.2 = [6 x i8] c"wait:\00"
@.str.3 = [3 x i8] c"--\00"
@.str.4 = [29 x i8] c"execl failed with errno: %d\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %38, label %6
6: ; preds = %2
%7 = bitcast i32* %3 to i8*
%8 = load i32, i32* @WNOHANG
%9 = call i64 @waitpid(i32 0, i32* %3, i32 %8)
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %27
11: ; preds = %33, %6
%12 = phi i64 [ %9, %6 ], [ %36, %33 ]
%13 = load i32, i32* @stderr
%14 = trunc i64 %12 to i32
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = load i32, i32* %3
%17 = call i64 @WIFEXITED(i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %25, label %19
19: ; preds = %11
%20 = load i32, i32* %3
%21 = call i32 @WEXITSTATUS(i32 %20)
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%24 = call i32 @exit(i32 %21)
unreachable
25: ; preds = %11
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %6, %33
%28 = phi i64 [ %36, %33 ], [ %9, %6 ]
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %27
%34 = call i32 (...) @sched_yield()
%35 = load i32, i32* @WNOHANG
%36 = call i64 @waitpid(i32 0, i32* %3, i32 %35)
%37 = icmp sgt i64 %36, 0
br i1 %37, label %11, label %27
38: ; preds = %2
%39 = getelementptr inbounds i8*, i8** %1, i64 1
%40 = load i8*, i8** %39
%41 = getelementptr inbounds i8*, i8** %1, i64 2
%42 = load i8*, i8** %41
%43 = getelementptr inbounds i8*, i8** %1, i64 3
%44 = load i8*, i8** %43
%45 = getelementptr inbounds i8*, i8** %1, i64 4
%46 = load i8*, i8** %45
%47 = getelementptr inbounds i8*, i8** %1, i64 5
%48 = load i8*, i8** %47
%49 = tail call i32 @execl(i8* %40, i8* %40, i8* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %44, i8* %46, i8* %48, i32* null)
%50 = load i32, i32* @stderr
%51 = load i32, i32* @errno
%52 = tail call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i32 %51)
ret i32 0
}