#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 */
scalar_t__ EINTR;
int EXIT_FAILURE;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ WIFSIGNALED(int);
scalar_t__ errno;
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int collect_child(pid_t c_pid) {
  pid_t ret_val;
  int status;
  do {
    status = 0;
    ret_val = waitpid(c_pid, &status, 0);
  } while ((0 > ret_val && EINTR == errno) ||
           (0 <= ret_val && !WIFEXITED(status) && !WIFSIGNALED(status)));
  if (WIFEXITED(status))
    return WEXITSTATUS(status);
  else
    return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<collect_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jle    1191 <collect_child+0x51>
mov    0x2e57(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%al
mov    0x2e5b(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x1d(%rbp)
je     11d5 <collect_child+0x95>
xor    %eax,%eax
xor    %ecx,%ecx
cmp    -0x18(%rbp),%rcx
mov    %al,-0x1e(%rbp)
jg     11cf <collect_child+0x8f>
mov    -0x1c(%rbp),%edi
call   1060 <WIFEXITED@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x1e(%rbp)
jne    11cf <collect_child+0x8f>
mov    -0x1c(%rbp),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x1e(%rbp)
mov    -0x1e(%rbp),%al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    114c <collect_child+0xc>
mov    -0x1c(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1202 <collect_child+0xc2>
mov    -0x1c(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    120e <collect_child+0xce>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<collect_child>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0x4(%rsp),%r14
mov    0x2e79(%rip),%r15
mov    0x2e82(%rip),%r12
jmp    1169 <collect_child+0x29>
mov    (%r15),%rax
cmp    (%r12),%rax
jne    119e <collect_child+0x5e>
movl   $0x0,0x4(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %rax,%rax
js     1160 <collect_child+0x20>
mov    0x4(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
jne    119e <collect_child+0x5e>
mov    0x4(%rsp),%edi
call   1030 <WIFSIGNALED@plt>
test   %eax,%eax
je     1169 <collect_child+0x29>
mov    0x4(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
je     11b7 <collect_child+0x77>
mov    0x4(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
jmp    11c0 <collect_child+0x80>
mov    0x2e1a(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@EINTR = global i64 0
@errno = global i64 0
@EXIT_FAILURE = global i32 0
define i32 @collect_child(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
store i64 %0, i64* %3
br label %6
6: ; preds = %30, %1
store i32 0, i32* %5
%7 = load i64, i64* %3
%8 = call i64 @waitpid(i64 %7, i32* %5, i32 0)
store i64 %8, i64* %4
br label %9
9: ; preds = %6
%10 = load i64, i64* %4
%11 = icmp sgt i64 0, %10
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load i64, i64* @EINTR
%14 = load i64, i64* @errno
%15 = icmp eq i64 %13, %14
br i1 %15, label %30, label %16
16: ; preds = %12, %9
%17 = load i64, i64* %4
%18 = icmp sle i64 0, %17
br i1 %18, label %19, label %28
19: ; preds = %16
%20 = load i32, i32* %5
%21 = call i64 @WIFEXITED(i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %19
%24 = load i32, i32* %5
%25 = call i32 @WIFSIGNALED(i32 %24)
%26 = icmp ne i32 %25, 0
%27 = xor i1 %26, true
br label %28
28: ; preds = %23, %19, %16
%29 = phi i1 [ false, %19 ], [ false, %16 ], [ %27, %23 ]
br label %30
30: ; preds = %28, %12
%31 = phi i1 [ true, %12 ], [ %29, %28 ]
br i1 %31, label %6, label %32
32: ; preds = %30
%33 = load i32, i32* %5
%34 = call i64 @WIFEXITED(i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i32, i32* %5
%38 = call i32 @WEXITSTATUS(i32 %37)
store i32 %38, i32* %2
br label %41
39: ; preds = %32
%40 = load i32, i32* @EXIT_FAILURE
store i32 %40, i32* %2
br label %41
41: ; preds = %39, %36
%42 = load i32, i32* %2
ret i32 %42
}

LLVM IR (-O2):
@EINTR = global i64 0
@errno = global i64 0
@EXIT_FAILURE = global i32 0
define i32 @collect_child(i64 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
br label %4
4: ; preds = %19, %1
store i32 0, i32* %2
%5 = call i64 @waitpid(i64 %0, i32* %2, i32 0)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %4
%8 = load i64, i64* @EINTR
%9 = load i64, i64* @errno
%10 = icmp eq i64 %8, %9
br i1 %10, label %19, label %20
11: ; preds = %4
%12 = load i32, i32* %2
%13 = call i64 @WIFEXITED(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = load i32, i32* %2
%17 = call i32 @WIFSIGNALED(i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15, %7
br label %4
20: ; preds = %7, %11, %15
%21 = load i32, i32* %2
%22 = call i64 @WIFEXITED(i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %20
%25 = load i32, i32* %2
%26 = call i32 @WEXITSTATUS(i32 %25)
br label %29
27: ; preds = %20
%28 = load i32, i32* @EXIT_FAILURE
br label %29
29: ; preds = %27, %24
%30 = phi i32 [ %26, %24 ], [ %28, %27 ]
ret i32 %30
}