#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 /*<<< orphan*/ WTERMSIG(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ trace(char *, int, ...);
int waitpid(int, int *, int /*<<< orphan*/);
int check_wait(pid_t pid) {
  int status;
  pid_t wait_result = waitpid(pid, &status, 0);
  if (wait_result == -1)
    perror("check_wait");
  else if (WIFEXITED(status)) {
    trace("child %d exited with code %d", pid, WEXITSTATUS(status));
    return WEXITSTATUS(status);
  } else if (WIFSIGNALED(status)) {
    trace("child %d killed by signal %d", pid, WTERMSIG(status));
  } else
    trace("child %d exited with unknown reason", pid);
  return 1;
}


x86 assembly (clang, -O0):
<check_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11a7 <check_wait+0x37>
lea    0xe63(%rip),%rdi
call   1080 <perror@plt>
jmp    123d <check_wait+0xcd>
mov    -0xc(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11ea <check_wait+0x7a>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1070 <trace@plt>
mov    -0xc(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
jmp    1244 <check_wait+0xd4>
mov    -0xc(%rbp),%edi
call   1030 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1222 <check_wait+0xb2>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <WTERMSIG@plt>
mov    -0x18(%rbp),%esi
mov    %eax,%edx
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1070 <trace@plt>
jmp    1233 <check_wait+0xc3>
mov    -0x8(%rbp),%esi
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1070 <trace@plt>
jmp    1238 <check_wait+0xc8>
jmp    123d <check_wait+0xcd>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_wait>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0xc(%rsp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
cmp    $0xffffffff,%eax
je     11f5 <check_wait+0x85>
mov    0xc(%rsp),%edi
call   1090 <WIFEXITED@plt>
mov    0xc(%rsp),%edi
test   %rax,%rax
je     11bc <check_wait+0x4c>
call   1050 <WEXITSTATUS@plt>
lea    0xe65(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <trace@plt>
mov    0xc(%rsp),%edi
call   1050 <WEXITSTATUS@plt>
jmp    1206 <check_wait+0x96>
call   1030 <WIFSIGNALED@plt>
test   %rax,%rax
je     11e3 <check_wait+0x73>
mov    0xc(%rsp),%edi
call   1040 <WTERMSIG@plt>
lea    0xe52(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <trace@plt>
jmp    1201 <check_wait+0x91>
lea    0xe5b(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <trace@plt>
jmp    1201 <check_wait+0x91>
lea    0xe04(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
@.str = [11 x i8] c"check_wait\00"
@.str.1 = [29 x i8] c"child %d exited with code %d\00"
@.str.2 = [29 x i8] c"child %d killed by signal %d\00"
@.str.3 = [36 x i8] c"child %d exited with unknown reason\00"
define i32 @check_wait(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @waitpid(i32 %0, i32* %2, i32 0)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %27
8: ; preds = %1
%9 = load i32, i32* %2
%10 = call i64 @WIFEXITED(i32 %9)
%11 = icmp eq i64 %10, 0
%12 = load i32, i32* %2
br i1 %11, label %18, label %13
13: ; preds = %8
%14 = call i32 @WEXITSTATUS(i32 %12)
%15 = call i32 (i8*, i32, ...) @trace(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %0, i32 %14)
%16 = load i32, i32* %2
%17 = call i32 @WEXITSTATUS(i32 %16)
br label %27
18: ; preds = %8
%19 = call i64 @WIFSIGNALED(i32 %12)
%20 = icmp eq i64 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %18
%22 = load i32, i32* %2
%23 = call i32 @WTERMSIG(i32 %22)
%24 = call i32 (i8*, i32, ...) @trace(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %23)
br label %27
25: ; preds = %18
%26 = call i32 (i8*, i32, ...) @trace(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %0)
br label %27
27: ; preds = %6, %21, %25, %13
%28 = phi i32 [ %17, %13 ], [ 1, %25 ], [ 1, %21 ], [ 1, %6 ]
ret i32 %28
}