#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int WCONTINUED;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int /*<<< orphan*/ WIFSIGNALED(int);
int WUNTRACED;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int waitpid(int, int *, int);
void wait_for_child(pid_t cpid) {
  pid_t w;
  int status;
  do {
    w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
    if (w == -1) {
      perror("waitpid failed");
      exit(EXIT_FAILURE);
    }
    if (WIFEXITED(status)) {
      printf("process exited, exit status is %d", WEXITSTATUS(status));
    }
  } while (!WIFEXITED(status) && !WIFSIGNALED(status));
}


x86 assembly (clang, -O0):
<wait_for_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
mov    0x2e3a(%rip),%rax
or     (%rax),%edx
lea    -0xc(%rbp),%rsi
call   1060 <waitpid@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11c0 <wait_for_child+0x50>
lea    0xe53(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11e8 <wait_for_child+0x78>
mov    -0xc(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe2c(%rip),%rdi
call   1030 <printf@plt>
jmp    11ed <wait_for_child+0x7d>
mov    -0xc(%rbp),%edi
call   1080 <WIFEXITED@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0xd(%rbp)
jne    121a <wait_for_child+0xaa>
mov    -0xc(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0xd(%rbp)
mov    -0xd(%rbp),%al
test   $0x1,%al
jne    117b <wait_for_child+0xb>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_child>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
mov    0x2e5a(%rip),%r12
mov    0x2e3b(%rip),%r13
lea    0xc(%rsp),%r14
lea    0xe76(%rip),%r15
nopl   0x0(%rax)
mov    0x0(%r13),%edx
or     (%r12),%edx
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <waitpid@plt>
cmp    $0xffffffff,%eax
je     1201 <wait_for_child+0x91>
mov    0xc(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     11d8 <wait_for_child+0x68>
mov    0xc(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    0xc(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
jne    11f3 <wait_for_child+0x83>
mov    0xc(%rsp),%edi
call   1040 <WIFSIGNALED@plt>
test   %eax,%eax
je     11a0 <wait_for_child+0x30>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xdf8(%rip),%rdi
call   1070 <perror@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@WUNTRACED = global i32 0
@WCONTINUED = global i32 0
@.str = [15 x i8] c"waitpid failed\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [34 x i8] c"process exited, exit status is %d\00"
define void @wait_for_child(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
br label %5
5: ; preds = %35, %1
%6 = load i32, i32* %2
%7 = load i32, i32* @WUNTRACED
%8 = load i32, i32* @WCONTINUED
%9 = or i32 %7, %8
%10 = call i32 @waitpid(i32 %6, i32* %4, i32 %9)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %17
13: ; preds = %5
%14 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %5
%18 = load i32, i32* %4
%19 = call i64 @WIFEXITED(i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = load i32, i32* %4
%23 = call i32 @WEXITSTATUS(i32 %22)
%24 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %21, %17
br label %26
26: ; preds = %25
%27 = load i32, i32* %4
%28 = call i64 @WIFEXITED(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %26
%31 = load i32, i32* %4
%32 = call i32 @WIFSIGNALED(i32 %31)
%33 = icmp ne i32 %32, 0
%34 = xor i1 %33, true
br label %35
35: ; preds = %30, %26
%36 = phi i1 [ false, %26 ], [ %34, %30 ]
br i1 %36, label %5, label %37
37: ; preds = %35
ret void
}

LLVM IR (-O2):
@WUNTRACED = global i32 0
@WCONTINUED = global i32 0
@.str = [15 x i8] c"waitpid failed\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [34 x i8] c"process exited, exit status is %d\00"
define void @wait_for_child(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
br label %4
4: ; preds = %26, %1
%5 = load i32, i32* @WUNTRACED
%6 = load i32, i32* @WCONTINUED
%7 = or i32 %6, %5
%8 = call i32 @waitpid(i32 %0, i32* %2, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %4
%11 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %4
%15 = load i32, i32* %2
%16 = call i64 @WIFEXITED(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %14
%19 = load i32, i32* %2
%20 = call i32 @WEXITSTATUS(i32 %19)
%21 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %14, %18
%23 = load i32, i32* %2
%24 = call i64 @WIFEXITED(i32 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = load i32, i32* %2
%28 = call i32 @WIFSIGNALED(i32 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %4, label %30
30: ; preds = %22, %26
ret void
}