#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 */
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ err(int, char *, ...);
int /*<<< orphan*/ exit(int);
int waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
void waitAndCheckStatus(pid_t child) {
  int status;
  if (waitpid(child, &status, 0) == -1) {
    err(1, "waitpid() failed");
    exit(1);
  }
  if (WIFEXITED(status)) {
    int es = WEXITSTATUS(status);
    if (es) {
      err(1, "child exit status %d", es);
      exit(1);
    }
  } else {
    err(1, "child did not exit normally");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<waitAndCheckStatus>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    118f <waitAndCheckStatus+0x3f>
mov    $0x1,%edi
lea    0xe82(%rip),%rsi
mov    $0x0,%al
call   1030 <err@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11db <waitAndCheckStatus+0x8b>
mov    -0x8(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11d6 <waitAndCheckStatus+0x86>
mov    -0xc(%rbp),%edx
mov    $0x1,%edi
lea    0xe4c(%rip),%rsi
mov    $0x0,%al
call   1030 <err@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
jmp    11f8 <waitAndCheckStatus+0xa8>
mov    $0x1,%edi
lea    0xe3f(%rip),%rsi
mov    $0x0,%al
call   1030 <err@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitAndCheckStatus>:
push   %rax
lea    0x4(%rsp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
je     117f <waitAndCheckStatus+0x2f>
mov    0x4(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
je     1188 <waitAndCheckStatus+0x38>
mov    0x4(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
test   %eax,%eax
jne    11a5 <waitAndCheckStatus+0x55>
pop    %rax
ret
lea    0xe7a(%rip),%rsi
jmp    118f <waitAndCheckStatus+0x3f>
lea    0xe97(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1030 <err@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe65(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <err@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"waitpid() failed\00"
@.str.1 = [21 x i8] c"child exit status %d\00"
@.str.2 = [28 x i8] c"child did not exit normally\00"
define void @waitAndCheckStatus(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = call i32 @waitpid(i32 %5, i32* %3, i32 0)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %1
%12 = load i32, i32* %3
%13 = call i64 @WIFEXITED(i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %25
15: ; preds = %11
%16 = load i32, i32* %3
%17 = call i32 @WEXITSTATUS(i32 %16)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = load i32, i32* %4
%22 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %15
br label %28
25: ; preds = %11
%26 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %24
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"waitpid() failed\00"
@.str.1 = [21 x i8] c"child exit status %d\00"
@.str.2 = [28 x i8] c"child did not exit normally\00"
define void @waitAndCheckStatus(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @waitpid(i32 %0, i32* %2, i32 0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %1
%10 = load i32, i32* %2
%11 = call i64 @WIFEXITED(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %9
%14 = load i32, i32* %2
%15 = call i32 @WEXITSTATUS(i32 %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %13
%18 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %13
ret void
21: ; preds = %9
%22 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
}