#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);
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)));
  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   1040 <waitpid@plt>
mov    %rax,-0x18(%rbp)
xor    %eax,%eax
cmp    -0x18(%rbp),%rax
jle    1181 <collect_child+0x51>
mov    0x2e67(%rip),%rax
mov    (%rax),%rcx
mov    $0x1,%al
mov    0x2e6b(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x1d(%rbp)
je     11ac <collect_child+0x7c>
xor    %eax,%eax
xor    %ecx,%ecx
cmp    -0x18(%rbp),%rcx
mov    %al,-0x1e(%rbp)
jg     11a6 <collect_child+0x76>
mov    -0x1c(%rbp),%edi
call   1050 <WIFEXITED@plt>
cmp    $0x0,%rax
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    113c <collect_child+0xc>
mov    -0x1c(%rbp),%edi
call   1050 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11d9 <collect_child+0xa9>
mov    -0x1c(%rbp),%edi
call   1030 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    11e5 <collect_child+0xb5>
mov    0x2df8(%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    0x2e89(%rip),%r15
mov    0x2e92(%rip),%r12
jmp    1159 <collect_child+0x29>
mov    (%r15),%rax
cmp    (%r12),%rax
jne    1181 <collect_child+0x51>
movl   $0x0,0x4(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
test   %rax,%rax
js     1150 <collect_child+0x20>
mov    0x4(%rsp),%edi
call   1050 <WIFEXITED@plt>
test   %rax,%rax
je     1159 <collect_child+0x29>
mov    0x4(%rsp),%edi
call   1050 <WIFEXITED@plt>
test   %rax,%rax
je     119a <collect_child+0x6a>
mov    0x4(%rsp),%edi
call   1030 <WEXITSTATUS@plt>
jmp    11a3 <collect_child+0x73>
mov    0x2e37(%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 = %26, %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 %26, label %16
16: ; preds = %12, %9
%17 = load i64, i64* %4
%18 = icmp sle i64 0, %17
br i1 %18, label %19, label %24
19: ; preds = %16
%20 = load i32, i32* %5
%21 = call i64 @WIFEXITED(i32 %20)
%22 = icmp ne i64 %21, 0
%23 = xor i1 %22, true
br label %24
24: ; preds = %19, %16
%25 = phi i1 [ false, %16 ], [ %23, %19 ]
br label %26
26: ; preds = %24, %12
%27 = phi i1 [ true, %12 ], [ %25, %24 ]
br i1 %27, label %6, label %28
28: ; preds = %26
%29 = load i32, i32* %5
%30 = call i64 @WIFEXITED(i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i32, i32* %5
%34 = call i32 @WEXITSTATUS(i32 %33)
store i32 %34, i32* %2
br label %37
35: ; preds = %28
%36 = load i32, i32* @EXIT_FAILURE
store i32 %36, i32* %2
br label %37
37: ; preds = %35, %32
%38 = load i32, i32* %2
ret i32 %38
}

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 = %15, %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 %15, label %16
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 %16
15: ; preds = %11, %7
br label %4
16: ; preds = %7, %11
%17 = load i32, i32* %2
%18 = call i64 @WIFEXITED(i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = load i32, i32* %2
%22 = call i32 @WEXITSTATUS(i32 %21)
br label %25
23: ; preds = %16
%24 = load i32, i32* @EXIT_FAILURE
br label %25
25: ; preds = %23, %20
%26 = phi i32 [ %22, %20 ], [ %24, %23 ]
ret i32 %26
}