#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 int pid_t;

/* Variables and functions */
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ WTERMSIG(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, long, int);
int /*<<< orphan*/ stderr;
int strsignal(int /*<<< orphan*/);
int waitpid(int, int *, int /*<<< orphan*/);
void wait_for_children() {
  while (1) {
    int status;
    pid_t child = waitpid(-1, &status, WNOHANG);
    if (!child || child == -1)
      return;
    if (WIFEXITED(status)) {
      int code = WEXITSTATUS(status);
      if (code)
        fprintf(stderr, "child process %ld exited with non-zero code %d\n",
                (long)child, code);
    } else {
      fprintf(stderr, "child process %ld killed by signal %s\n", (long)child,
              strsignal(WTERMSIG(status)));
    }
  }
}


x86 assembly (clang, -O0):
<wait_for_children>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x4(%rbp),%rsi
call   1070 <waitpid@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     1196 <wait_for_children+0x36>
cmpl   $0xffffffff,-0x8(%rbp)
jne    119c <wait_for_children+0x3c>
add    $0x20,%rsp
pop    %rbp
ret
mov    -0x4(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11e4 <wait_for_children+0x84>
mov    -0x4(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11df <wait_for_children+0x7f>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
movslq -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
lea    0xe26(%rip),%rsi
call   1030 <fprintf@plt>
jmp    121c <wait_for_children+0xbc>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
movslq -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <WTERMSIG@plt>
mov    %eax,%edi
call   1050 <strsignal@plt>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rdx
mov    %eax,%ecx
lea    0xe19(%rip),%rsi
call   1030 <fprintf@plt>
jmp    1168 <wait_for_children+0x8>

x86 assembly (clang, -O2):
<wait_for_children>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e76(%rip),%r13
mov    0x0(%r13),%edx
lea    0x4(%rsp),%rsi
mov    $0xffffffff,%edi
call   1070 <waitpid@plt>
mov    %eax,%ebx
lea    0x1(%rbx),%eax
cmp    $0x2,%eax
jae    119e <wait_for_children+0x3e>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e33(%rip),%r14
lea    0x4(%rsp),%r12
lea    0xe4f(%rip),%r15
jmp    1204 <wait_for_children+0xa4>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%r14),%ebp
movslq %ebx,%rbx
mov    0x4(%rsp),%edi
call   1040 <WTERMSIG@plt>
mov    %eax,%edi
call   1050 <strsignal@plt>
mov    %ebp,%edi
lea    0xe51(%rip),%rsi
mov    %rbx,%rdx
mov    %eax,%ecx
call   1030 <fprintf@plt>
mov    0x0(%r13),%edx
mov    $0xffffffff,%edi
mov    %r12,%rsi
call   1070 <waitpid@plt>
mov    %eax,%ebx
add    $0x1,%eax
cmp    $0x1,%eax
jbe    118f <wait_for_children+0x2f>
mov    0x4(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     11c0 <wait_for_children+0x60>
mov    0x4(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
test   %eax,%eax
je     11e9 <wait_for_children+0x89>
mov    (%r14),%edi
movslq %ebx,%rdx
mov    %r15,%rsi
jmp    11e2 <wait_for_children+0x82>

LLVM IR (-O0):
@WNOHANG = global i32 0
@stderr = global i32 0
@.str = [48 x i8] c"child process %ld exited with non-zero code %d\0A\00"
@.str.1 = [39 x i8] c"child process %ld killed by signal %s\0A\00"
define void @wait_for_children() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
br label %4
4: ; preds = %0, %37
%5 = load i32, i32* @WNOHANG
%6 = call i32 @waitpid(i32 -1, i32* %1, i32 %5)
store i32 %6, i32* %2
%7 = load i32, i32* %2
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %9, %4
ret void
13: ; preds = %9
%14 = load i32, i32* %1
%15 = call i64 @WIFEXITED(i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %29
17: ; preds = %13
%18 = load i32, i32* %1
%19 = call i32 @WEXITSTATUS(i32 %18)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %17
%23 = load i32, i32* @stderr
%24 = load i32, i32* %2
%25 = sext i32 %24 to i64
%26 = load i32, i32* %3
%27 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i64 %25, i32 %26)
br label %28
28: ; preds = %22, %17
br label %37
29: ; preds = %13
%30 = load i32, i32* @stderr
%31 = load i32, i32* %2
%32 = sext i32 %31 to i64
%33 = load i32, i32* %1
%34 = call i32 @WTERMSIG(i32 %33)
%35 = call i32 @strsignal(i32 %34)
%36 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i64 %32, i32 %35)
br label %37
37: ; preds = %29, %28
br label %4
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@stderr = global i32 0
@.str = [48 x i8] c"child process %ld exited with non-zero code %d\0A\00"
@.str.1 = [39 x i8] c"child process %ld killed by signal %s\0A\00"
define void @wait_for_children() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = load i32, i32* @WNOHANG
%4 = call i32 @waitpid(i32 -1, i32* %1, i32 %3)
%5 = add i32 %4, 1
%6 = icmp ult i32 %5, 2
br i1 %6, label %32, label %7
7: ; preds = %0, %27
%8 = phi i32 [ %29, %27 ], [ %4, %0 ]
%9 = load i32, i32* %1
%10 = call i64 @WIFEXITED(i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %20, label %12
12: ; preds = %7
%13 = load i32, i32* %1
%14 = call i32 @WEXITSTATUS(i32 %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %27, label %16
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = sext i32 %8 to i64
%19 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i64 %18, i32 %14)
br label %27
20: ; preds = %7
%21 = load i32, i32* @stderr
%22 = sext i32 %8 to i64
%23 = load i32, i32* %1
%24 = call i32 @WTERMSIG(i32 %23)
%25 = call i32 @strsignal(i32 %24)
%26 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i64 %22, i32 %25)
br label %27
27: ; preds = %20, %16, %12
%28 = load i32, i32* @WNOHANG
%29 = call i32 @waitpid(i32 -1, i32* %1, i32 %28)
%30 = add i32 %29, 1
%31 = icmp ult i32 %30, 2
br i1 %31, label %32, label %7
32: ; preds = %27, %0
ret void
}