#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 */
scalar_t__ ECHILD;
scalar_t__ errno;
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
scalar_t__ waitpid(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
void handler(int sig) {
  pid_t pid;
  while ((pid = waitpid(-1, NULL, 0)) > 0) {
    printf("Handler reaped child %d\n", (int)pid);
  }
  if (errno != ECHILD) {
    fputs("waitpid error", stderr);
  }
  sleep(2);
  return;
}


x86 assembly (clang, -O0):
<handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jle    1180 <handler+0x40>
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xe8a(%rip),%rdi
call   1030 <printf@plt>
jmp    114b <handler+0xb>
mov    0x2e51(%rip),%rax
mov    (%rax),%rax
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%rax
je     11af <handler+0x6f>
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
lea    0xe6f(%rip),%rdi
call   1040 <fputs@plt>
mov    $0x2,%edi
call   1060 <sleep@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handler>:
push   %rbx
mov    $0xffffffff,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %rax,%rax
jle    117d <handler+0x3d>
lea    0xea5(%rip),%rbx
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <waitpid@plt>
test   %rax,%rax
jg     1160 <handler+0x20>
mov    0x2e54(%rip),%rax
mov    (%rax),%rax
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%rax
je     11a8 <handler+0x68>
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
lea    0xe76(%rip),%rdi
call   1040 <fputs@plt>
mov    $0x2,%edi
pop    %rbx
jmp    1060 <sleep@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"Handler reaped child %d\0A\00"
@errno = global i64 0
@ECHILD = global i64 0
@.str.1 = [14 x i8] c"waitpid error\00"
@stderr = global i32 0
define void @handler(i32 %0) {
%2 = alloca i32
%3 = alloca i64
store i32 %0, i32* %2
br label %4
4: ; preds = %7, %1
%5 = call i64 @waitpid(i32 -1, i32* null, i32 0)
store i64 %5, i64* %3
%6 = icmp sgt i64 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %4
%8 = load i64, i64* %3
%9 = trunc i64 %8 to i32
%10 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %4
11: ; preds = %4
%12 = load i64, i64* @errno
%13 = load i64, i64* @ECHILD
%14 = icmp ne i64 %12, %13
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = call i32 @fputs(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %16)
br label %18
18: ; preds = %15, %11
%19 = call i32 @sleep(i32 2)
ret void
}

LLVM IR (-O2):
@.str = [25 x i8] c"Handler reaped child %d\0A\00"
@errno = global i64 0
@ECHILD = global i64 0
@.str.1 = [14 x i8] c"waitpid error\00"
@stderr = global i32 0
define void @handler(i32 %0) {
%2 = tail call i64 @waitpid(i32 -1, i32* null, i32 0)
%3 = icmp sgt i64 %2, 0
br i1 %3, label %4, label %10
4: ; preds = %1, %4
%5 = phi i64 [ %8, %4 ], [ %2, %1 ]
%6 = trunc i64 %5 to i32
%7 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = tail call i64 @waitpid(i32 -1, i32* null, i32 0)
%9 = icmp sgt i64 %8, 0
br i1 %9, label %4, label %10
10: ; preds = %4, %1
%11 = load i64, i64* @errno
%12 = load i64, i64* @ECHILD
%13 = icmp eq i64 %11, %12
br i1 %13, label %17, label %14
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = tail call i32 @fputs(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %15)
br label %17
17: ; preds = %14, %10
%18 = tail call i32 @sleep(i32 2)
ret void
}