#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 SIGCHLD;
int /*<<< orphan*/ WNOHANG;
int /*<<< orphan*/ printf(char *, int);
int waitpid(int, int *, int /*<<< orphan*/);
void xhk_sig_handler(int sig) {
  if (sig == SIGCHLD) {
    int status;
    pid_t child;
    while ((child = waitpid(-1, &status, WNOHANG)) > 0) {
      printf("pid %d exited\n", child);
    }
  }
}


x86 assembly (clang, -O0):
<xhk_sig_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2eab(%rip),%rcx
cmp    (%rcx),%eax
jne    117e <xhk_sig_handler+0x5e>
jmp    1142 <xhk_sig_handler+0x22>
mov    0x2e9f(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x8(%rbp),%rsi
call   1040 <waitpid@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jle    1179 <xhk_sig_handler+0x59>
mov    -0xc(%rbp),%esi
lea    0xe91(%rip),%rdi
call   1030 <printf@plt>
jmp    1142 <xhk_sig_handler+0x22>
jmp    117e <xhk_sig_handler+0x5e>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xhk_sig_handler>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2eb0(%rip),%rax
cmp    %edi,(%rax)
jne    117e <xhk_sig_handler+0x5e>
mov    0x2ead(%rip),%r15
mov    (%r15),%edx
lea    0xc(%rsp),%rsi
mov    $0xffffffff,%edi
call   1040 <waitpid@plt>
test   %eax,%eax
jle    117e <xhk_sig_handler+0x5e>
lea    0xea8(%rip),%r14
lea    0xc(%rsp),%rbx
nopl   (%rax)
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    (%r15),%edx
mov    $0xffffffff,%edi
mov    %rbx,%rsi
call   1040 <waitpid@plt>
test   %eax,%eax
jg     1160 <xhk_sig_handler+0x40>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SIGCHLD = global i32 0
@WNOHANG = global i32 0
@.str = [15 x i8] c"pid %d exited\0A\00"
define void @xhk_sig_handler(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = load i32, i32* @SIGCHLD
%7 = icmp eq i32 %5, %6
br i1 %7, label %8, label %17
8: ; preds = %1
br label %9
9: ; preds = %13, %8
%10 = load i32, i32* @WNOHANG
%11 = call i32 @waitpid(i32 -1, i32* %3, i32 %10)
store i32 %11, i32* %4
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* %4
%15 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %9
16: ; preds = %9
br label %17
17: ; preds = %16, %1
ret void
}

LLVM IR (-O2):
@SIGCHLD = global i32 0
@WNOHANG = global i32 0
@.str = [15 x i8] c"pid %d exited\0A\00"
define void @xhk_sig_handler(i32 %0) {
%2 = alloca i32
%3 = load i32, i32* @SIGCHLD
%4 = icmp eq i32 %3, %0
br i1 %4, label %5, label %17
5: ; preds = %1
%6 = bitcast i32* %2 to i8*
%7 = load i32, i32* @WNOHANG
%8 = call i32 @waitpid(i32 -1, i32* %2, i32 %7)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %5, %10
%11 = phi i32 [ %14, %10 ], [ %8, %5 ]
%12 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32, i32* @WNOHANG
%14 = call i32 @waitpid(i32 -1, i32* %2, i32 %13)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %10, label %16
16: ; preds = %10, %5
br label %17
17: ; preds = %16, %1
ret void
}