#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 */
#define ECHILD 129
#define EINTR 128
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int errno;
scalar_t__ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int wait_for_child_process_exit(pid_t child_pid) {
  int status;
  while (waitpid(child_pid, &status, 0) < 0) {
    switch (errno) {
    case ECHILD:
      return 0;
    case EINTR:
      break;
    default:
      return -1;
    }
  }
  if (WIFEXITED(status)) {
    return WEXITSTATUS(status);
  }
  if (WIFSIGNALED(status)) {
    return 128 + WTERMSIG(status);
  }
  return status;
}


x86 assembly (clang, -O0):
<wait_for_child_process_exit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0x0,%rax
jge    11c4 <wait_for_child_process_exit+0x74>
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
sub    $0x80,%eax
je     11ae <wait_for_child_process_exit+0x5e>
jmp    118f <wait_for_child_process_exit+0x3f>
mov    -0x10(%rbp),%eax
sub    $0x81,%eax
jne    11b3 <wait_for_child_process_exit+0x63>
jmp    11a2 <wait_for_child_process_exit+0x52>
movl   $0x0,-0x4(%rbp)
jmp    1213 <wait_for_child_process_exit+0xc3>
jmp    11bf <wait_for_child_process_exit+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1213 <wait_for_child_process_exit+0xc3>
jmp    115b <wait_for_child_process_exit+0xb>
mov    -0xc(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11e6 <wait_for_child_process_exit+0x96>
mov    -0xc(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    1213 <wait_for_child_process_exit+0xc3>
mov    -0xc(%rbp),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     120d <wait_for_child_process_exit+0xbd>
mov    -0xc(%rbp),%edi
call   1040 <WTERMSIG@plt>
add    $0x80,%eax
mov    %eax,-0x4(%rbp)
jmp    1213 <wait_for_child_process_exit+0xc3>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_child_process_exit>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0xc(%rsp),%r14
mov    0x2e79(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
test   %rax,%rax
jns    1196 <wait_for_child_process_exit+0x46>
mov    (%r15),%eax
cmp    $0x80,%eax
je     1170 <wait_for_child_process_exit+0x20>
cmp    $0x81,%eax
jne    11af <wait_for_child_process_exit+0x5f>
xor    %eax,%eax
jmp    11d1 <wait_for_child_process_exit+0x81>
mov    0xc(%rsp),%edi
call   1070 <WIFEXITED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
je     11b6 <wait_for_child_process_exit+0x66>
call   1050 <WEXITSTATUS@plt>
jmp    11d1 <wait_for_child_process_exit+0x81>
mov    $0xffffffff,%eax
jmp    11d1 <wait_for_child_process_exit+0x81>
call   1030 <WIFSIGNALED@plt>
mov    %rax,%rcx
mov    0xc(%rsp),%eax
test   %rcx,%rcx
je     11d1 <wait_for_child_process_exit+0x81>
mov    %eax,%edi
call   1040 <WTERMSIG@plt>
sub    $0xffffff80,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i32 0
define i32 @wait_for_child_process_exit(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
br label %5
5: ; preds = %14, %1
%6 = load i32, i32* %3
%7 = call i64 @waitpid(i32 %6, i32* %4, i32 0)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %5
%10 = load i32, i32* @errno
switch i32 %10, label %13 [
i32 129, label %11
i32 128, label %12
]
11: ; preds = %9
store i32 0, i32* %2
br label %32
12: ; preds = %9
br label %14
13: ; preds = %9
store i32 -1, i32* %2
br label %32
14: ; preds = %12
br label %5
15: ; preds = %5
%16 = load i32, i32* %4
%17 = call i64 @WIFEXITED(i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i32, i32* %4
%21 = call i32 @WEXITSTATUS(i32 %20)
store i32 %21, i32* %2
br label %32
22: ; preds = %15
%23 = load i32, i32* %4
%24 = call i64 @WIFSIGNALED(i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = load i32, i32* %4
%28 = call i32 @WTERMSIG(i32 %27)
%29 = add nsw i32 128, %28
store i32 %29, i32* %2
br label %32
30: ; preds = %22
%31 = load i32, i32* %4
store i32 %31, i32* %2
br label %32
32: ; preds = %30, %26, %19, %13, %11
%33 = load i32, i32* %2
ret i32 %33
}

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