#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 /*<<< orphan*/ EXIT_FAILURE;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
scalar_t__ WIFSTOPPED(int);
int /*<<< orphan*/ WUNTRACED;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int waitpid(int, int *, int /*<<< orphan*/);
int priv_usch_waitforall(int child_pid) {
  pid_t wpid;
  int status;
  int child_status;
  do {
    wpid = waitpid(child_pid, &status, WUNTRACED);
    if (wpid == -1) {
      perror("waitpid");
      exit(EXIT_FAILURE);
    }
    if (WIFEXITED(status)) {
      child_status = WEXITSTATUS(status);
    } else if (WIFSIGNALED(status)) {
      child_status = -1;
    } else if (WIFSTOPPED(status)) {
      child_status = -1;
    } else {
      child_status = -1;
    }
  } while (!WIFEXITED(status) && !WIFSIGNALED(status));
  return child_status;
}


x86 assembly (clang, -O0):
<priv_usch_waitforall>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rsi
call   1060 <waitpid@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11b7 <priv_usch_waitforall+0x47>
lea    0xe5c(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11d9 <priv_usch_waitforall+0x69>
mov    -0xc(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x10(%rbp)
jmp    1226 <priv_usch_waitforall+0xb6>
mov    -0xc(%rbp),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     11f7 <priv_usch_waitforall+0x87>
movl   $0xffffffff,-0x10(%rbp)
jmp    1221 <priv_usch_waitforall+0xb1>
mov    -0xc(%rbp),%edi
call   1040 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     1215 <priv_usch_waitforall+0xa5>
movl   $0xffffffff,-0x10(%rbp)
jmp    121c <priv_usch_waitforall+0xac>
movl   $0xffffffff,-0x10(%rbp)
jmp    1221 <priv_usch_waitforall+0xb1>
jmp    1226 <priv_usch_waitforall+0xb6>
jmp    122b <priv_usch_waitforall+0xbb>
mov    -0xc(%rbp),%edi
call   1080 <WIFEXITED@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x11(%rbp)
jne    1259 <priv_usch_waitforall+0xe9>
mov    -0xc(%rbp),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    117b <priv_usch_waitforall+0xb>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<priv_usch_waitforall>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2e60(%rip),%r15
lea    0x4(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    (%r15),%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1209 <priv_usch_waitforall+0x99>
mov    0x4(%rsp),%edi
call   1080 <WIFEXITED@plt>
mov    0x4(%rsp),%edi
test   %rax,%rax
je     11c0 <priv_usch_waitforall+0x50>
call   1050 <WEXITSTATUS@plt>
mov    %eax,%ebp
jmp    11e0 <priv_usch_waitforall+0x70>
nopl   (%rax)
call   1030 <WIFSIGNALED@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11e0 <priv_usch_waitforall+0x70>
mov    0x4(%rsp),%edi
call   1040 <WIFSTOPPED@plt>
nopl   0x0(%rax,%rax,1)
mov    0x4(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
jne    11fc <priv_usch_waitforall+0x8c>
mov    0x4(%rsp),%edi
call   1030 <WIFSIGNALED@plt>
test   %rax,%rax
je     1190 <priv_usch_waitforall+0x20>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf0(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@WUNTRACED = global i32 0
@.str = [8 x i8] c"waitpid\00"
@EXIT_FAILURE = global i32 0
define i32 @priv_usch_waitforall(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
br label %6
6: ; preds = %46, %1
%7 = load i32, i32* %2
%8 = load i32, i32* @WUNTRACED
%9 = call i32 @waitpid(i32 %7, i32* %4, i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %6
%13 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %6
%17 = load i32, i32* %4
%18 = call i64 @WIFEXITED(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* %4
%22 = call i32 @WEXITSTATUS(i32 %21)
store i32 %22, i32* %5
br label %36
23: ; preds = %16
%24 = load i32, i32* %4
%25 = call i64 @WIFSIGNALED(i32 %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23
store i32 -1, i32* %5
br label %35
28: ; preds = %23
%29 = load i32, i32* %4
%30 = call i64 @WIFSTOPPED(i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 -1, i32* %5
br label %34
33: ; preds = %28
store i32 -1, i32* %5
br label %34
34: ; preds = %33, %32
br label %35
35: ; preds = %34, %27
br label %36
36: ; preds = %35, %20
br label %37
37: ; preds = %36
%38 = load i32, i32* %4
%39 = call i64 @WIFEXITED(i32 %38)
%40 = icmp ne i64 %39, 0
br i1 %40, label %46, label %41
41: ; preds = %37
%42 = load i32, i32* %4
%43 = call i64 @WIFSIGNALED(i32 %42)
%44 = icmp ne i64 %43, 0
%45 = xor i1 %44, true
br label %46
46: ; preds = %41, %37
%47 = phi i1 [ false, %37 ], [ %45, %41 ]
br i1 %47, label %6, label %48
48: ; preds = %46
%49 = load i32, i32* %5
ret i32 %49
}

LLVM IR (-O2):
@WUNTRACED = global i32 0
@.str = [8 x i8] c"waitpid\00"
@EXIT_FAILURE = global i32 0
define i32 @priv_usch_waitforall(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
br label %4
4: ; preds = %30, %1
%5 = load i32, i32* @WUNTRACED
%6 = call i32 @waitpid(i32 %0, i32* %2, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %4
%9 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %4
%13 = load i32, i32* %2
%14 = call i64 @WIFEXITED(i32 %13)
%15 = icmp eq i64 %14, 0
%16 = load i32, i32* %2
br i1 %15, label %19, label %17
17: ; preds = %12
%18 = call i32 @WEXITSTATUS(i32 %16)
br label %25
19: ; preds = %12
%20 = call i64 @WIFSIGNALED(i32 %16)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %19
%23 = load i32, i32* %2
%24 = call i64 @WIFSTOPPED(i32 %23)
br label %25
25: ; preds = %22, %19, %17
%26 = phi i32 [ %18, %17 ], [ -1, %19 ], [ -1, %22 ]
%27 = load i32, i32* %2
%28 = call i64 @WIFEXITED(i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %25
%31 = load i32, i32* %2
%32 = call i64 @WIFSIGNALED(i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %4, label %34
34: ; preds = %25, %30
ret i32 %26
}