#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  int si_signo;
  int si_pid;
  int si_status;
};
typedef TYPE_1__ siginfo_t;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ try_waitid(char *);
int /*<<< orphan*/ try_waitpid(char *);
void handleCHLD(int signum, siginfo_t *psi, void *pv) {
  pid_t pid;
  printf("Handler: signum=%d signo=%d pid=%d status=%d\n", signum,
         psi->si_signo, psi->si_pid, psi->si_status);
  pid = try_waitid("Handler");
  if (pid != psi->si_pid) {
    printf("Handler: waitid() apparently not working\n");
    (void)try_waitpid("Handler");
  }
  printf("Handler: exiting.\n");
}


x86 assembly (clang, -O0):
<handleCHLD>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%r8d
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xebe(%rip),%rdi
call   1040 <try_waitid@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq 0x4(%rcx),%rcx
cmp    %rcx,%rax
je     11a8 <handleCHLD+0x78>
lea    0xea1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe8b(%rip),%rdi
call   1050 <try_waitpid@plt>
lea    0xeb1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handleCHLD>:
push   %rbx
mov    %rsi,%rbx
mov    %edi,%esi
mov    (%rbx),%edx
mov    0x4(%rbx),%ecx
mov    0x8(%rbx),%r8d
lea    0xeaa(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xeca(%rip),%rdi
call   1050 <try_waitid@plt>
movslq 0x4(%rbx),%rcx
cmp    %rcx,%rax
je     118a <handleCHLD+0x4a>
lea    0xecf(%rip),%rdi
call   1030 <puts@plt>
lea    0xea9(%rip),%rdi
call   1060 <try_waitpid@plt>
lea    0xea5(%rip),%rdi
pop    %rbx
jmp    1030 <puts@plt>

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i32, i32 }
@.str = [46 x i8] c"Handler: signum=%d signo=%d pid=%d status=%d\0A\00"
@.str.1 = [8 x i8] c"Handler\00"
@.str.2 = [42 x i8] c"Handler: waitid() apparently not working\0A\00"
@.str.3 = [19 x i8] c"Handler: exiting.\0A\00"
define void @handleCHLD(i32 %0, %struct.TYPE_3__* %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.TYPE_3__*
%6 = alloca i8*
%7 = alloca i64
store i32 %0, i32* %4
store %struct.TYPE_3__* %1, %struct.TYPE_3__** %5
store i8* %2, i8** %6
%8 = load i32, i32* %4
%9 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%10 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %9, i32 0, i32 0
%11 = load i32, i32* %10
%12 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%13 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %12, i32 0, i32 1
%14 = load i32, i32* %13
%15 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%16 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %15, i32 0, i32 2
%17 = load i32, i32* %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %11, i32 %14, i32 %17)
%19 = call i64 @try_waitid(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i64 %19, i64* %7
%20 = load i64, i64* %7
%21 = load %struct.TYPE_3__*, %struct.TYPE_3__** %5
%22 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %21, i32 0, i32 1
%23 = load i32, i32* %22
%24 = sext i32 %23 to i64
%25 = icmp ne i64 %20, %24
br i1 %25, label %26, label %29
26: ; preds = %3
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @try_waitpid(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %26, %3
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i32, i32 }
@.str = [46 x i8] c"Handler: signum=%d signo=%d pid=%d status=%d\0A\00"
@.str.1 = [8 x i8] c"Handler\00"
@str = [18 x i8] c"Handler: exiting.\00"
@str.4 = [41 x i8] c"Handler: waitid() apparently not working\00"
define void @handleCHLD(i32 %0, %struct.TYPE_3__* nocapture %1, i8* nocapture readnone %2) {
%4 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 0
%5 = load i32, i32* %4
%6 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 1
%7 = load i32, i32* %6
%8 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 2
%9 = load i32, i32* %8
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %5, i32 %7, i32 %9)
%11 = tail call i64 @try_waitid(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* %6
%13 = sext i32 %12 to i64
%14 = icmp eq i64 %11, %13
br i1 %14, label %18, label %15
15: ; preds = %3
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.4, i64 0, i64 0))
%17 = tail call i32 @try_waitpid(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %15, %3
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
ret void
}