#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 WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int WTERMSIG(int);
int /*<<< orphan*/ exit_with_error(char *);
int /*<<< orphan*/ sprintf(char *, char *, int, int);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
void wait_for_child_exit(pid_t pid) {
  while (1) {
    int wstatus = 0;
    waitpid(pid, &wstatus, 0);
    if (WIFEXITED(wstatus)) {
      if (WEXITSTATUS(wstatus) != 0) {
        char buf[1024];
        sprintf(buf, "child process %d terminated with status %d", pid,
                WEXITSTATUS(wstatus));
        exit_with_error(buf);
      }
      return;
    } else if (WIFSIGNALED(wstatus)) {
      char buf[1024];
      sprintf(buf, "child process %d terminated by signal %d", pid,
              WTERMSIG(wstatus));
      exit_with_error(buf);
      return;
    }
  }
}


x86 assembly (clang, -O0):
<wait_for_child_exit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    -0x8(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1201 <wait_for_child_exit+0x91>
mov    -0x8(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
cmp    $0x0,%eax
je     11fc <wait_for_child_exit+0x8c>
lea    -0x410(%rbp),%rax
mov    %rax,-0x820(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x814(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    -0x820(%rbp),%rdi
mov    -0x814(%rbp),%edx
mov    %eax,%ecx
lea    0xe15(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x410(%rbp),%rdi
call   1050 <exit_with_error@plt>
jmp    1268 <wait_for_child_exit+0xf8>
mov    -0x8(%rbp),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     125e <wait_for_child_exit+0xee>
lea    -0x810(%rbp),%rax
mov    %rax,-0x830(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x824(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <WTERMSIG@plt>
mov    -0x830(%rbp),%rdi
mov    -0x824(%rbp),%edx
mov    %eax,%ecx
lea    0xde3(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x810(%rbp),%rdi
call   1050 <exit_with_error@plt>
jmp    1268 <wait_for_child_exit+0xf8>
jmp    1263 <wait_for_child_exit+0xf3>
jmp    117e <wait_for_child_exit+0xe>
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_child_exit>:
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %edi,%ebx
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    0xc(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
jne    11e0 <wait_for_child_exit+0x70>
lea    0xc(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0xc(%rsp),%edi
call   1030 <WIFSIGNALED@plt>
test   %rax,%rax
jne    11ff <wait_for_child_exit+0x8f>
movl   $0x0,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1070 <waitpid@plt>
mov    0xc(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     11b0 <wait_for_child_exit+0x40>
mov    0xc(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
test   %eax,%eax
je     1228 <wait_for_child_exit+0xb8>
mov    0xc(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
lea    0xe03(%rip),%rsi
jmp    120f <wait_for_child_exit+0x9f>
mov    0xc(%rsp),%edi
call   1040 <WTERMSIG@plt>
lea    0xe1c(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    %ebx,%edx
mov    %eax,%ecx
call   1090 <sprintf@plt>
mov    %r14,%rdi
call   1050 <exit_with_error@plt>
add    $0x418,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [43 x i8] c"child process %d terminated with status %d\00"
@.str.1 = [41 x i8] c"child process %d terminated by signal %d\00"
define void @wait_for_child_exit(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
store i32 %0, i32* %2
br label %6
6: ; preds = %1, %38
store i32 0, i32* %3
%7 = load i32, i32* %2
%8 = call i32 @waitpid(i32 %7, i32* %3, i32 0)
%9 = load i32, i32* %3
%10 = call i64 @WIFEXITED(i32 %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %25
12: ; preds = %6
%13 = load i32, i32* %3
%14 = call i32 @WEXITSTATUS(i32 %13)
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %12
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%18 = load i32, i32* %2
%19 = load i32, i32* %3
%20 = call i32 @WEXITSTATUS(i32 %19)
%21 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %18, i32 %20)
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%23 = call i32 @exit_with_error(i8* %22)
br label %24
24: ; preds = %16, %12
br label %39
25: ; preds = %6
%26 = load i32, i32* %3
%27 = call i64 @WIFSIGNALED(i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %25
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%31 = load i32, i32* %2
%32 = load i32, i32* %3
%33 = call i32 @WTERMSIG(i32 %32)
%34 = call i32 @sprintf(i8* %30, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %31, i32 %33)
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%36 = call i32 @exit_with_error(i8* %35)
br label %39
37: ; preds = %25
br label %38
38: ; preds = %37
br label %6
39: ; preds = %29, %24
ret void
}

LLVM IR (-O2):
@.str = [43 x i8] c"child process %d terminated with status %d\00"
@.str.1 = [41 x i8] c"child process %d terminated by signal %d\00"
define void @wait_for_child_exit(i32 %0) {
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%6 = call i32 @waitpid(i32 %0, i32* %2, i32 0)
%7 = load i32, i32* %2
%8 = call i64 @WIFEXITED(i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %20, label %10
10: ; preds = %30, %1
%11 = load i32, i32* %2
%12 = call i32 @WEXITSTATUS(i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %35, label %14
14: ; preds = %10
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%16 = load i32, i32* %2
%17 = call i32 @WEXITSTATUS(i32 %16)
%18 = call i32 @sprintf(i8* %15, i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %17)
%19 = call i32 @exit_with_error(i8* %15)
br label %35
20: ; preds = %1, %30
%21 = load i32, i32* %2
%22 = call i64 @WIFSIGNALED(i32 %21)
%23 = icmp eq i64 %22, 0
br i1 %23, label %30, label %24
24: ; preds = %20
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%26 = load i32, i32* %2
%27 = call i32 @WTERMSIG(i32 %26)
%28 = call i32 @sprintf(i8* %25, i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i32 %0, i32 %27)
%29 = call i32 @exit_with_error(i8* %25)
br label %35
30: ; preds = %20
store i32 0, i32* %2
%31 = call i32 @waitpid(i32 %0, i32* %2, i32 0)
%32 = load i32, i32* %2
%33 = call i64 @WIFEXITED(i32 %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %20, label %10
35: ; preds = %24, %14, %10
ret void
}