#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 */
int EXIT_FAILURE;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ errno_exit(char *);
scalar_t__ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int await_child(pid_t child_pid) {
  int wait_status;
  if (waitpid(child_pid, &wait_status, 0) != child_pid)
    errno_exit("cannot await child process");
  if (WIFEXITED(wait_status))
    return WEXITSTATUS(wait_status);
  if (WIFSIGNALED(wait_status))
    return WTERMSIG(wait_status) | 0x80;
  return EXIT_FAILURE;
}


x86 assembly (clang, -O0):
<await_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    -0x10(%rbp),%rax
je     1191 <await_child+0x31>
lea    0xe74(%rip),%rdi
call   1030 <errno_exit@plt>
mov    -0x14(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11b3 <await_child+0x53>
mov    -0x14(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    11e6 <await_child+0x86>
mov    -0x14(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     11da <await_child+0x7a>
mov    -0x14(%rbp),%edi
call   1050 <WTERMSIG@plt>
or     $0x80,%eax
mov    %eax,-0x4(%rbp)
jmp    11e6 <await_child+0x86>
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<await_child>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
cmp    %rbx,%rax
je     1185 <await_child+0x25>
lea    0xe80(%rip),%rdi
call   1030 <errno_exit@plt>
mov    0xc(%rsp),%edi
call   1080 <WIFEXITED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
je     119e <await_child+0x3e>
call   1060 <WEXITSTATUS@plt>
jmp    11c1 <await_child+0x61>
call   1040 <WIFSIGNALED@plt>
test   %rax,%rax
je     11b8 <await_child+0x58>
mov    0xc(%rsp),%edi
call   1050 <WTERMSIG@plt>
or     $0x80,%eax
jmp    11c1 <await_child+0x61>
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [27 x i8] c"cannot await child process\00"
@EXIT_FAILURE = global i32 0
define i32 @await_child(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
store i64 %0, i64* %3
%5 = load i64, i64* %3
%6 = call i64 @waitpid(i64 %5, i32* %4, i32 0)
%7 = load i64, i64* %3
%8 = icmp ne i64 %6, %7
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @errno_exit(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %1
%12 = load i32, i32* %4
%13 = call i64 @WIFEXITED(i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* %4
%17 = call i32 @WEXITSTATUS(i32 %16)
store i32 %17, i32* %2
br label %28
18: ; preds = %11
%19 = load i32, i32* %4
%20 = call i64 @WIFSIGNALED(i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = load i32, i32* %4
%24 = call i32 @WTERMSIG(i32 %23)
%25 = or i32 %24, 128
store i32 %25, i32* %2
br label %28
26: ; preds = %18
%27 = load i32, i32* @EXIT_FAILURE
store i32 %27, i32* %2
br label %28
28: ; preds = %26, %22, %15
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@.str = [27 x i8] c"cannot await child process\00"
@EXIT_FAILURE = global i32 0
define i32 @await_child(i64 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i64 @waitpid(i64 %0, i32* %2, i32 0)
%5 = icmp eq i64 %4, %0
br i1 %5, label %8, label %6
6: ; preds = %1
%7 = call i32 @errno_exit(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %1
%9 = load i32, i32* %2
%10 = call i64 @WIFEXITED(i32 %9)
%11 = icmp eq i64 %10, 0
%12 = load i32, i32* %2
br i1 %11, label %15, label %13
13: ; preds = %8
%14 = call i32 @WEXITSTATUS(i32 %12)
br label %24
15: ; preds = %8
%16 = call i64 @WIFSIGNALED(i32 %12)
%17 = icmp eq i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %15
%19 = load i32, i32* %2
%20 = call i32 @WTERMSIG(i32 %19)
%21 = or i32 %20, 128
br label %24
22: ; preds = %15
%23 = load i32, i32* @EXIT_FAILURE
br label %24
24: ; preds = %22, %18, %13
%25 = phi i32 [ %14, %13 ], [ %21, %18 ], [ %23, %22 ]
ret i32 %25
}