#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 */
scalar_t__ EXIT_SUCCESS;
scalar_t__ WEXITSTATUS(int);
int /*<<< orphan*/ die(int /*<<< orphan*/, char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(scalar_t__);
scalar_t__ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
void waitforexit(pid_t child) {
  int status;
  if (waitpid(child, &status, 0) < 0)
    die(errno, "waitpid");
  else if (WEXITSTATUS(status) != EXIT_SUCCESS)
    exit(WEXITSTATUS(status));
}


x86 assembly (clang, -O0):
<waitforexit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    $0x0,%rax
jge    117d <waitforexit+0x3d>
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
lea    0xe8d(%rip),%rsi
call   1050 <die@plt>
jmp    11aa <waitforexit+0x6a>
mov    -0x8(%rbp),%edi
call   1030 <WEXITSTATUS@plt>
mov    0x2e4c(%rip),%rcx
cmp    (%rcx),%rax
je     11a5 <waitforexit+0x65>
mov    -0x8(%rbp),%edi
call   1030 <WEXITSTATUS@plt>
mov    %rax,%rdi
call   1060 <exit@plt>
jmp    11aa <waitforexit+0x6a>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitforexit>:
push   %rax
lea    0x4(%rsp),%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
test   %rax,%rax
js     1169 <waitforexit+0x29>
mov    0x4(%rsp),%edi
call   1030 <WEXITSTATUS@plt>
mov    0x2e76(%rip),%rcx
cmp    (%rcx),%rax
jne    1180 <waitforexit+0x40>
pop    %rax
ret
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
lea    0xe87(%rip),%rsi
call   1050 <die@plt>
pop    %rax
ret
mov    0x4(%rsp),%edi
call   1030 <WEXITSTATUS@plt>
mov    %rax,%rdi
call   1060 <exit@plt>

LLVM IR (-O0):
@errno = global i32 0
@.str = [8 x i8] c"waitpid\00"
@EXIT_SUCCESS = global i64 0
define void @waitforexit(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = call i64 @waitpid(i32 %4, i32* %3, i32 0)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @errno
%9 = call i32 @die(i32 %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %20
10: ; preds = %1
%11 = load i32, i32* %3
%12 = call i64 @WEXITSTATUS(i32 %11)
%13 = load i64, i64* @EXIT_SUCCESS
%14 = icmp ne i64 %12, %13
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load i32, i32* %3
%17 = call i64 @WEXITSTATUS(i32 %16)
%18 = call i32 @exit(i64 %17)
unreachable
19: ; preds = %10
br label %20
20: ; preds = %19, %7
ret void
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [8 x i8] c"waitpid\00"
@EXIT_SUCCESS = global i64 0
define void @waitforexit(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i64 @waitpid(i32 %0, i32* %2, i32 0)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = call i32 @die(i32 %7, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %18
9: ; preds = %1
%10 = load i32, i32* %2
%11 = call i64 @WEXITSTATUS(i32 %10)
%12 = load i64, i64* @EXIT_SUCCESS
%13 = icmp eq i64 %11, %12
br i1 %13, label %18, label %14
14: ; preds = %9
%15 = load i32, i32* %2
%16 = call i64 @WEXITSTATUS(i32 %15)
%17 = call i32 @exit(i64 %16)
unreachable
18: ; preds = %9, %6
ret void
}