#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);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int waitpid(int, int *, int /*<<< orphan*/);
int wait_child(char *name, pid_t pid) {
  int child_status;
  if (waitpid(pid, &child_status, 0) == -1) {
    perror("waitpid");
    return -1;
  }
  if (WIFEXITED(child_status)) {
    fprintf(stderr, "Child \"%s\" [%u] exited with status %d\n", name, pid,
            WEXITSTATUS(child_status));
    return child_status;
  } else {
    fprintf(stderr, "Child \"%s\" [%u] exited abnormally\n", name, pid);
    return -1;
  }
}


x86 assembly (clang, -O0):
<wait_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    118e <wait_child+0x3e>
lea    0xe83(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    120d <wait_child+0xbd>
mov    -0x18(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11e8 <wait_child+0x98>
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    %eax,%r8d
lea    0xe32(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120d <wait_child+0xbd>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xe30(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_child>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
cmp    $0xffffffff,%eax
je     11d1 <wait_child+0x81>
mov    0xc(%rsp),%edi
call   1070 <WIFEXITED@plt>
mov    0x2e60(%rip),%rcx
mov    (%rcx),%ebp
test   %rax,%rax
je     11ae <wait_child+0x5e>
mov    0xc(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
lea    0xe71(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %ebx,%ecx
mov    %eax,%r8d
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    0xc(%rsp),%eax
jmp    11c8 <wait_child+0x78>
lea    0xe7a(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %ebx,%ecx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe28(%rip),%rdi
call   1060 <perror@plt>
jmp    11c3 <wait_child+0x73>

LLVM IR (-O0):
@.str = [8 x i8] c"waitpid\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"Child \22%s\22 [%u] exited with status %d\0A\00"
@.str.2 = [35 x i8] c"Child \22%s\22 [%u] exited abnormally\0A\00"
define i32 @wait_child(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = call i32 @waitpid(i32 %7, i32* %6, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %29
12: ; preds = %2
%13 = load i32, i32* %6
%14 = call i64 @WIFEXITED(i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %4
%19 = load i32, i32* %5
%20 = load i32, i32* %6
%21 = call i32 @WEXITSTATUS(i32 %20)
%22 = call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %18, i32 %19, i32 %21)
%23 = load i32, i32* %6
store i32 %23, i32* %3
br label %29
24: ; preds = %12
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %4
%27 = load i32, i32* %5
%28 = call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %26, i32 %27)
store i32 -1, i32* %3
br label %29
29: ; preds = %24, %16, %10
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
@.str = [8 x i8] c"waitpid\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"Child \22%s\22 [%u] exited with status %d\0A\00"
@.str.2 = [35 x i8] c"Child \22%s\22 [%u] exited abnormally\0A\00"
define i32 @wait_child(i8* %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @waitpid(i32 %1, i32* %3, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %21
9: ; preds = %2
%10 = load i32, i32* %3
%11 = call i64 @WIFEXITED(i32 %10)
%12 = icmp eq i64 %11, 0
%13 = load i32, i32* @stderr
br i1 %12, label %19, label %14
14: ; preds = %9
%15 = load i32, i32* %3
%16 = call i32 @WEXITSTATUS(i32 %15)
%17 = call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %1, i32 %16)
%18 = load i32, i32* %3
br label %21
19: ; preds = %9
%20 = call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %1)
br label %21
21: ; preds = %19, %14, %7
%22 = phi i32 [ -1, %7 ], [ %18, %14 ], [ -1, %19 ]
ret i32 %22
}