#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ error_msg(char *, int, ...);
int /*<<< orphan*/ perror_msg(char *);
char *strsignal(int);
int wait_child(int /*<<< orphan*/);
int handle_child_error(pid_t pid) {
  int ret = wait_child(pid);
  if (ret < 0) {
    perror_msg("waitpid");
  } else if (ret >= 256) {
    int sig = ret >> 8;
    const char *str = strsignal(sig);
    error_msg("Child received signal %d (%s)", sig, str ? str : "??");
  } else if (ret) {
    error_msg("Child returned %d", ret);
  }
  return ret;
}


x86 assembly (clang, -O0):
<handle_child_error>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1060 <wait_child@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1171 <handle_child_error+0x31>
lea    0xe99(%rip),%rdi
call   1030 <perror_msg@plt>
jmp    1200 <handle_child_error+0xc0>
cmpl   $0x100,-0x8(%rbp)
jl     11db <handle_child_error+0x9b>
mov    -0x8(%rbp),%eax
sar    $0x8,%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <strsignal@plt>
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11b1 <handle_child_error+0x71>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11c1 <handle_child_error+0x81>
lea    0xe6e(%rip),%rax
mov    %rax,-0x28(%rbp)
jmp    11c1 <handle_child_error+0x81>
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rdx
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <error_msg@plt>
jmp    11fb <handle_child_error+0xbb>
cmpl   $0x0,-0x8(%rbp)
je     11f6 <handle_child_error+0xb6>
mov    -0x8(%rbp),%esi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <error_msg@plt>
jmp    11fb <handle_child_error+0xbb>
jmp    1200 <handle_child_error+0xc0>
mov    -0x8(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_child_error>:
push   %rbp
push   %rbx
push   %rax
call   1060 <wait_child@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1182 <handle_child_error+0x42>
cmp    $0x100,%ebx
jb     1190 <handle_child_error+0x50>
mov    %ebx,%ebp
shr    $0x8,%ebp
mov    %ebp,%edi
call   1050 <strsignal@plt>
test   %rax,%rax
lea    0xeba(%rip),%rdx
cmovne %rax,%rdx
lea    0xe91(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <error_msg@plt>
jmp    11a4 <handle_child_error+0x64>
lea    0xe77(%rip),%rdi
call   1030 <perror_msg@plt>
jmp    11a4 <handle_child_error+0x64>
test   %ebx,%ebx
je     11a4 <handle_child_error+0x64>
lea    0xe8e(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <error_msg@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"waitpid\00"
@.str.1 = [30 x i8] c"Child received signal %d (%s)\00"
@.str.2 = [3 x i8] c"??\00"
@.str.3 = [18 x i8] c"Child returned %d\00"
define i32 @handle_child_error(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = call i32 @wait_child(i32 %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror_msg(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %37
12: ; preds = %1
%13 = load i32, i32* %3
%14 = icmp sge i32 %13, 256
br i1 %14, label %15, label %29
15: ; preds = %12
%16 = load i32, i32* %3
%17 = ashr i32 %16, 8
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = call i8* @strsignal(i32 %18)
store i8* %19, i8** %5
%20 = load i32, i32* %4
%21 = load i8*, i8** %5
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = load i8*, i8** %5
br label %26
25: ; preds = %15
br label %26
26: ; preds = %25, %23
%27 = phi i8* [ %24, %23 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), %25 ]
%28 = call i32 (i8*, i32, ...) @error_msg(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %20, i8* %27)
br label %36
29: ; preds = %12
%30 = load i32, i32* %3
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* %3
%34 = call i32 (i8*, i32, ...) @error_msg(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %32, %29
br label %36
36: ; preds = %35, %26
br label %37
37: ; preds = %36, %10
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
@.str = [8 x i8] c"waitpid\00"
@.str.1 = [30 x i8] c"Child received signal %d (%s)\00"
@.str.2 = [3 x i8] c"??\00"
@.str.3 = [18 x i8] c"Child returned %d\00"
define i32 @handle_child_error(i32 %0) {
%2 = tail call i32 @wait_child(i32 %0)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @perror_msg(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %18
6: ; preds = %1
%7 = icmp ugt i32 %2, 255
br i1 %7, label %8, label %14
8: ; preds = %6
%9 = lshr i32 %2, 8
%10 = tail call i8* @strsignal(i32 %9)
%11 = icmp eq i8* %10, null
%12 = select i1 %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %10
%13 = tail call i32 (i8*, i32, ...) @error_msg(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i32 %9, i8* %12)
br label %18
14: ; preds = %6
%15 = icmp eq i32 %2, 0
br i1 %15, label %18, label %16
16: ; preds = %14
%17 = tail call i32 (i8*, i32, ...) @error_msg(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %2)
br label %18
18: ; preds = %8, %16, %14, %4
ret i32 %2
}