#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 /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int waitpid(int, int *, int /*<<< orphan*/);
int waitchldrn(int **pidlst, int numchld) {
  int i, cpid, ret, errflag = 0;
  int status;
  for (i = 0; i < numchld; i++) {
    cpid = *(*pidlst + i);
    if (cpid == 0)
      continue;
    if ((ret = waitpid(cpid, &status, 0)) != cpid) {
      fprintf(stderr, "waitchldrn: wait failed for child %d, pid %d: %s\n", i,
              cpid, strerror(errno));
      errflag--;
    }
    if (status)
      errflag = -1;
  }
  return (errflag);
}


x86 assembly (clang, -O0):
<waitchldrn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11fe <waitchldrn+0xce>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1179 <waitchldrn+0x49>
jmp    11f0 <waitchldrn+0xc0>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
mov    %eax,-0x18(%rbp)
cmp    -0x14(%rbp),%eax
je     11da <waitchldrn+0xaa>
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    %rax,%r8
lea    0xe34(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11eb <waitchldrn+0xbb>
movl   $0xffffffff,-0x1c(%rbp)
jmp    11f0 <waitchldrn+0xc0>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    114d <waitchldrn+0x1d>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitchldrn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %esi,%esi
jle    11b7 <waitchldrn+0x87>
mov    %rdi,%r12
mov    %esi,%r14d
xor    %r15d,%r15d
xor    %ebx,%ebx
xor    %r13d,%r13d
jmp    1163 <waitchldrn+0x33>
nop
cmp    0x4(%rsp),%r15d
sbb    %eax,%eax
or     %eax,%r13d
add    $0x1,%rbx
cmp    %rbx,%r14
je     11ba <waitchldrn+0x8a>
mov    (%r12),%rax
mov    (%rax,%rbx,4),%ebp
test   %ebp,%ebp
je     115a <waitchldrn+0x2a>
mov    %ebp,%edi
lea    0x4(%rsp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    %ebp,%eax
je     1150 <waitchldrn+0x20>
mov    0x2e59(%rip),%rax
mov    (%rax),%r15d
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    %r15d,%edi
xor    %r15d,%r15d
lea    0xe5b(%rip),%rsi
mov    %ebx,%edx
mov    %ebp,%ecx
mov    %rax,%r8
call   1030 <fprintf@plt>
add    $0xffffffff,%r13d
jmp    1150 <waitchldrn+0x20>
xor    %r13d,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [50 x i8] c"waitchldrn: wait failed for child %d, pid %d: %s\0A\00"
@errno = global i32 0
define i32 @waitchldrn(i32** %0, i32 %1) {
%3 = alloca i32**
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32** %0, i32*** %3
store i32 %1, i32* %4
store i32 0, i32* %8
store i32 0, i32* %5
br label %10
10: ; preds = %43, %2
%11 = load i32, i32* %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %46
14: ; preds = %10
%15 = load i32**, i32*** %3
%16 = load i32*, i32** %15
%17 = load i32, i32* %5
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i32, i32* %16, i64 %18
%20 = load i32, i32* %19
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %14
br label %43
24: ; preds = %14
%25 = load i32, i32* %6
%26 = call i32 @waitpid(i32 %25, i32* %9, i32 0)
store i32 %26, i32* %7
%27 = load i32, i32* %6
%28 = icmp ne i32 %26, %27
br i1 %28, label %29, label %38
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = load i32, i32* %5
%32 = load i32, i32* %6
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %31, i32 %32, i8* %34)
%36 = load i32, i32* %8
%37 = add nsw i32 %36, -1
store i32 %37, i32* %8
br label %38
38: ; preds = %29, %24
%39 = load i32, i32* %9
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %38
store i32 -1, i32* %8
br label %42
42: ; preds = %41, %38
br label %43
43: ; preds = %42, %23
%44 = load i32, i32* %5
%45 = add nsw i32 %44, 1
store i32 %45, i32* %5
br label %10
46: ; preds = %10
%47 = load i32, i32* %8
ret i32 %47
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [50 x i8] c"waitchldrn: wait failed for child %d, pid %d: %s\0A\00"
@errno = global i32 0
define i32 @waitchldrn(i32** nocapture %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %34
6: ; preds = %2
%7 = zext i32 %1 to i64
br label %8
8: ; preds = %6, %30
%9 = phi i64 [ 0, %6 ], [ %32, %30 ]
%10 = phi i32 [ 0, %6 ], [ %31, %30 ]
%11 = load i32*, i32** %0
%12 = getelementptr inbounds i32, i32* %11, i64 %9
%13 = load i32, i32* %12
%14 = icmp eq i32 %13, 0
br i1 %14, label %30, label %15
15: ; preds = %8
%16 = call i32 @waitpid(i32 %13, i32* %3, i32 0)
%17 = icmp eq i32 %16, %13
br i1 %17, label %25, label %18
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = trunc i64 %9 to i32
%23 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 %22, i32 %13, i8* %21)
%24 = add nsw i32 %10, -1
br label %25
25: ; preds = %18, %15
%26 = phi i32 [ %24, %18 ], [ %10, %15 ]
%27 = load i32, i32* %3
%28 = icmp eq i32 %27, 0
%29 = select i1 %28, i32 %26, i32 -1
br label %30
30: ; preds = %25, %8
%31 = phi i32 [ %10, %8 ], [ %29, %25 ]
%32 = add nuw nsw i64 %9, 1
%33 = icmp eq i64 %32, %7
br i1 %33, label %34, label %8
34: ; preds = %30, %2
%35 = phi i32 [ 0, %2 ], [ %31, %30 ]
ret i32 %35
}