#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 */

/* Variables and functions */
int /*<<< orphan*/ WAIT_ANY;
int /*<<< orphan*/ WNOHANG;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int, int);
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stdout;
int waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
void sigchld_handler(int signum) {
  int pid, status, serrno;
  serrno = errno;
  while (1) {
    pid = waitpid(WAIT_ANY, &status, WNOHANG);
    fprintf(stdout, "getpid=%d waitpid=%d status=%d\n", getpid(), pid, status);
    if (pid < 0) {
      perror("waitpid");
      break;
    }
    if (pid == 0)
      break;
  }
  errno = serrno;
}


x86 assembly (clang, -O0):
<sigchld_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
mov    0x2e81(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rsi
call   1050 <waitpid@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e4c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x14(%rbp),%edi
mov    %eax,%edx
mov    -0x8(%rbp),%ecx
mov    -0xc(%rbp),%r8d
lea    0xe65(%rip),%rsi
call   1040 <fprintf@plt>
cmpl   $0x0,-0x8(%rbp)
jge    11bb <sigchld_handler+0x7b>
lea    0xe6f(%rip),%rdi
call   1060 <perror@plt>
jmp    11cf <sigchld_handler+0x8f>
cmpl   $0x0,-0x8(%rbp)
jne    11ca <sigchld_handler+0x8a>
jmp    11cf <sigchld_handler+0x8f>
jmp    1157 <sigchld_handler+0x17>
mov    -0x10(%rbp),%ecx
mov    0x2df7(%rip),%rax
mov    %ecx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sigchld_handler>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2e8a(%rip),%r13
lea    0x4(%rsp),%r14
mov    0x2e5e(%rip),%r12
lea    0xe8f(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    0x0(%r13),%edx
mov    %r14,%rsi
call   1050 <waitpid@plt>
mov    %eax,%ebp
mov    (%r12),%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x4(%rsp),%r8d
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
mov    %ebp,%ecx
call   1040 <fprintf@plt>
test   %ebp,%ebp
js     11d6 <sigchld_handler+0x96>
jne    1180 <sigchld_handler+0x40>
mov    0x2e0e(%rip),%rax
mov    (%rsp),%ecx
mov    %ecx,(%rax)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe43(%rip),%rdi
call   1060 <perror@plt>
jmp    11bb <sigchld_handler+0x7b>

LLVM IR (-O0):
@errno = global i32 0
@WAIT_ANY = global i32 0
@WNOHANG = global i32 0
@stdout = global i32 0
@.str = [32 x i8] c"getpid=%d waitpid=%d status=%d\0A\00"
@.str.1 = [8 x i8] c"waitpid\00"
define void @sigchld_handler(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* @errno
store i32 %6, i32* %5
br label %7
7: ; preds = %1, %24
%8 = load i32, i32* @WAIT_ANY
%9 = load i32, i32* @WNOHANG
%10 = call i32 @waitpid(i32 %8, i32* %4, i32 %9)
store i32 %10, i32* %3
%11 = load i32, i32* @stdout
%12 = call i32 (...) @getpid()
%13 = load i32, i32* %3
%14 = load i32, i32* %4
%15 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13, i32 %14)
%16 = load i32, i32* %3
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %7
%19 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %25
20: ; preds = %7
%21 = load i32, i32* %3
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20
br label %25
24: ; preds = %20
br label %7
25: ; preds = %23, %18
%26 = load i32, i32* %5
store i32 %26, i32* @errno
ret void
}

LLVM IR (-O2):
@errno = global i32 0
@WAIT_ANY = global i32 0
@WNOHANG = global i32 0
@stdout = global i32 0
@.str = [32 x i8] c"getpid=%d waitpid=%d status=%d\0A\00"
@.str.1 = [8 x i8] c"waitpid\00"
define void @sigchld_handler(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @errno
br label %5
5: ; preds = %16, %1
%6 = load i32, i32* @WAIT_ANY
%7 = load i32, i32* @WNOHANG
%8 = call i32 @waitpid(i32 %6, i32* %2, i32 %7)
%9 = load i32, i32* @stdout
%10 = call i32 (...) @getpid()
%11 = load i32, i32* %2
%12 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %8, i32 %11)
%13 = icmp slt i32 %8, 0
br i1 %13, label %14, label %16
14: ; preds = %5
%15 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %18
16: ; preds = %5
%17 = icmp eq i32 %8, 0
br i1 %17, label %18, label %5
18: ; preds = %16, %14
store i32 %4, i32* @errno
ret void
}