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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int waitpid(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/);
void reap_children(pid_t *ids, const size_t count) {
  for (size_t i = 0; i < count; ++i) {
    if (waitpid(ids[i], NULL, 0) == -1) {
      perror("error calling waitpid()");
      exit(EXIT_FAILURE);
    }
  }
}


x86 assembly (clang, -O0):
<reap_children>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11a5 <reap_children+0x75>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1030 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    118f <reap_children+0x5f>
lea    0xe84(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
jmp    1194 <reap_children+0x64>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1148 <reap_children+0x18>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reap_children>:
push   %r15
push   %r14
push   %rbx
test   %rsi,%rsi
je     116b <reap_children+0x3b>
mov    %rsi,%r15
mov    %rdi,%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r14,%rbx,4),%edi
xor    %esi,%esi
xor    %edx,%edx
call   1030 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1171 <reap_children+0x41>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1150 <reap_children+0x20>
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe88(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"error calling waitpid()\00"
@EXIT_FAILURE = global i32 0
define void @reap_children(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
store i32* %0, i32** %3
store i64 %1, i64* %4
store i64 0, i64* %5
br label %6
6: ; preds = %22, %2
%7 = load i64, i64* %5
%8 = load i64, i64* %4
%9 = icmp ult i64 %7, %8
br i1 %9, label %10, label %25
10: ; preds = %6
%11 = load i32*, i32** %3
%12 = load i64, i64* %5
%13 = getelementptr inbounds i32, i32* %11, i64 %12
%14 = load i32, i32* %13
%15 = call i32 @waitpid(i32 %14, i32* null, i32 0)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %10
%18 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %10
br label %22
22: ; preds = %21
%23 = load i64, i64* %5
%24 = add i64 %23, 1
store i64 %24, i64* %5
br label %6
25: ; preds = %6
ret void
}

LLVM IR (-O2):
@.str = [24 x i8] c"error calling waitpid()\00"
@EXIT_FAILURE = global i32 0
define void @reap_children(i32* nocapture %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %6, label %7
4: ; preds = %7
%5 = icmp eq i64 %13, %1
br i1 %5, label %6, label %7
6: ; preds = %4, %2
ret void
7: ; preds = %2, %4
%8 = phi i64 [ %13, %4 ], [ 0, %2 ]
%9 = getelementptr inbounds i32, i32* %0, i64 %8
%10 = load i32, i32* %9
%11 = tail call i32 @waitpid(i32 %10, i32* null, i32 0)
%12 = icmp eq i32 %11, -1
%13 = add nuw i64 %8, 1
br i1 %12, label %14, label %4
14: ; preds = %7
%15 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
}