#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);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int wait_child(pid_t pid) {
  int status;
  do {
    if (waitpid(pid, &status, 0) < 0) {
      perror("waitpid failed:");
      exit(-1);
    }
  } while (!WIFEXITED(status));
  return WEXITSTATUS(status);
}


x86 assembly (clang, -O0):
<wait_child>:
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    1189 <wait_child+0x39>
lea    0xe86(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    118e <wait_child+0x3e>
mov    -0x8(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    115b <wait_child+0xb>
mov    -0x8(%rbp),%edi
call   1030 <WEXITSTATUS@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_child>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
lea    0x4(%rsp),%r14
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1040 <waitpid@plt>
test   %rax,%rax
js     118f <wait_child+0x3f>
mov    0x4(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %eax,%eax
je     1160 <wait_child+0x10>
mov    0x4(%rsp),%edi
call   1030 <WEXITSTATUS@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe6a(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

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

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