#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 /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ printf(char *, int);
scalar_t__ waitpid(int, int *, int /*<<< orphan*/);
void handle_sigchld(int sig) {
  int status;
  pid_t pid;
  pid = waitpid(-1, &status, WNOHANG);
  if (WIFEXITED(status)) {
    printf("Remove child PID: %ld\n", (long)pid);
    printf("Child send: %d\n", WEXITSTATUS(status));
  }
}


x86 assembly (clang, -O0):
<handle_sigchld>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e96(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x8(%rbp),%rsi
call   1050 <waitpid@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11a0 <handle_sigchld+0x60>
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xe7b(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xe7c(%rip),%rdi
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_sigchld>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e9c(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rsi
mov    $0xffffffff,%edi
call   1050 <waitpid@plt>
mov    %rax,%rbx
mov    0xc(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %rax,%rax
je     1193 <handle_sigchld+0x53>
lea    0xe8b(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
mov    0xc(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
lea    0xe8b(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@WNOHANG = global i32 0
@.str = [23 x i8] c"Remove child PID: %ld\0A\00"
@.str.1 = [16 x i8] c"Child send: %d\0A\00"
define void @handle_sigchld(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
store i32 %0, i32* %2
%5 = load i32, i32* @WNOHANG
%6 = call i64 @waitpid(i32 -1, i32* %3, i32 %5)
store i64 %6, i64* %4
%7 = load i32, i32* %3
%8 = call i64 @WIFEXITED(i32 %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %1
%11 = load i64, i64* %4
%12 = trunc i64 %11 to i32
%13 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %3
%15 = call i32 @WEXITSTATUS(i32 %14)
%16 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %15)
br label %17
17: ; preds = %10, %1
ret void
}

LLVM IR (-O2):
@WNOHANG = global i32 0
@.str = [23 x i8] c"Remove child PID: %ld\0A\00"
@.str.1 = [16 x i8] c"Child send: %d\0A\00"
define void @handle_sigchld(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @WNOHANG
%5 = call i64 @waitpid(i32 -1, i32* %2, i32 %4)
%6 = load i32, i32* %2
%7 = call i64 @WIFEXITED(i32 %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %15, label %9
9: ; preds = %1
%10 = trunc i64 %5 to i32
%11 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = load i32, i32* %2
%13 = call i32 @WEXITSTATUS(i32 %12)
%14 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %9, %1
ret void
}