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

/* Variables and functions */
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int WTERMSIG(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
scalar_t__ wait(int *);
int wait_child(pid_t child) {
  pid_t pid;
  int status;
  pid = wait(&status);
  if (pid < 0) {
    perror("Waiting on child process");
    return -1;
  }
  if (pid != child) {
    fprintf(stderr, "Unexpected return value from wait, expected %d, got %d\n",
            child, pid);
    return -99;
  }
  if (WIFEXITED(status)) {
    int code = WEXITSTATUS(status);
    if (code != 0) {
      fprintf(stderr, "Child exited with code %d\n", code);
      return -1;
    }
  } else {
    fprintf(stderr, "Child exited via signal %d\n", WTERMSIG(status));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<wait_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x1c(%rbp),%rdi
call   1080 <wait@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    119c <wait_child+0x3c>
lea    0xe75(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <wait_child+0x103>
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11d7 <wait_child+0x77>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    %eax,%edx
mov    -0x18(%rbp),%rcx
lea    0xe55(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffff9d,-0x4(%rbp)
jmp    1263 <wait_child+0x103>
mov    -0x1c(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1229 <wait_child+0xc9>
mov    -0x1c(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1224 <wait_child+0xc4>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%edx
lea    0xe40(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <wait_child+0x103>
jmp    125c <wait_child+0xfc>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <WTERMSIG@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%edx
lea    0xe23(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <wait_child+0x103>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_child>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0xc(%rsp),%rdi
call   1080 <wait@plt>
test   %rax,%rax
js     1205 <wait_child+0xa5>
cmp    %rbx,%rax
jne    11bc <wait_child+0x5c>
mov    0xc(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %rax,%rax
je     11df <wait_child+0x7f>
mov    0xc(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,%ebx
test   %eax,%eax
je     11b6 <wait_child+0x56>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
lea    0xea4(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
neg    %ebx
sbb    %eax,%eax
jmp    1216 <wait_child+0xb6>
mov    0x2e1d(%rip),%rcx
mov    (%rcx),%edi
lea    0xe4d(%rip),%rsi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0xffffff9d,%eax
jmp    1216 <wait_child+0xb6>
mov    0x2dfa(%rip),%rax
mov    (%rax),%ebx
mov    0xc(%rsp),%edi
call   1040 <WTERMSIG@plt>
lea    0xe74(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    1211 <wait_child+0xb1>
lea    0xdf4(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [25 x i8] c"Waiting on child process\00"
@stderr = global i32 0
@.str.1 = [56 x i8] c"Unexpected return value from wait, expected %d, got %d\0A\00"
@.str.2 = [27 x i8] c"Child exited with code %d\0A\00"
@.str.3 = [28 x i8] c"Child exited via signal %d\0A\00"
define i32 @wait_child(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i64 %0, i64* %3
%7 = call i64 @wait(i32* %5)
store i64 %7, i64* %4
%8 = load i64, i64* %4
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %42
12: ; preds = %1
%13 = load i64, i64* %4
%14 = load i64, i64* %3
%15 = icmp ne i64 %13, %14
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = load i64, i64* %3
%19 = trunc i64 %18 to i32
%20 = load i64, i64* %4
%21 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i32 %19, i64 %20)
store i32 -99, i32* %2
br label %42
22: ; preds = %12
%23 = load i32, i32* %5
%24 = call i64 @WIFEXITED(i32 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %22
%27 = load i32, i32* %5
%28 = call i32 @WEXITSTATUS(i32 %27)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %26
%32 = load i32, i32* @stderr
%33 = load i32, i32* %6
%34 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %33)
store i32 -1, i32* %2
br label %42
35: ; preds = %26
br label %41
36: ; preds = %22
%37 = load i32, i32* @stderr
%38 = load i32, i32* %5
%39 = call i32 @WTERMSIG(i32 %38)
%40 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %39)
store i32 -1, i32* %2
br label %42
41: ; preds = %35
store i32 0, i32* %2
br label %42
42: ; preds = %41, %36, %31, %16, %10
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@.str = [25 x i8] c"Waiting on child process\00"
@stderr = global i32 0
@.str.1 = [56 x i8] c"Unexpected return value from wait, expected %d, got %d\0A\00"
@.str.2 = [27 x i8] c"Child exited with code %d\0A\00"
@.str.3 = [28 x i8] c"Child exited via signal %d\0A\00"
define i32 @wait_child(i64 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i64 @wait(i32* %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %33
8: ; preds = %1
%9 = icmp eq i64 %4, %0
br i1 %9, label %14, label %10
10: ; preds = %8
%11 = load i32, i32* @stderr
%12 = trunc i64 %0 to i32
%13 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.1, i64 0, i64 0), i32 %12, i64 %4)
br label %33
14: ; preds = %8
%15 = load i32, i32* %2
%16 = call i64 @WIFEXITED(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %28, label %18
18: ; preds = %14
%19 = load i32, i32* %2
%20 = call i32 @WEXITSTATUS(i32 %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %20)
br label %25
25: ; preds = %18, %22
%26 = xor i1 %21, true
%27 = sext i1 %26 to i32
br label %33
28: ; preds = %14
%29 = load i32, i32* @stderr
%30 = load i32, i32* %2
%31 = call i32 @WTERMSIG(i32 %30)
%32 = call i32 (i32, i8*, i32, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %31)
br label %33
33: ; preds = %25, %28, %10, %6
%34 = phi i32 [ -1, %6 ], [ -99, %10 ], [ -1, %28 ], [ %27, %25 ]
ret i32 %34
}