#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 SIGCHLD;
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
scalar_t__ WIFSTOPPED(int);
int WNOHANG;
int WUNTRACED;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ explain_wait_status(scalar_t__, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
scalar_t__ waitpid(int, int *, int);
void sigchld_handler(int signum) {
  pid_t p;
  int status;
  if (signum != SIGCHLD) {
    fprintf(stderr, "Internal error: Called for signum %d, not SIGCHLD\n",
            signum);
    exit(1);
  }
  for (;;) {
    p = waitpid(-1, &status, WUNTRACED | WNOHANG);
    if (p < 0) {
      perror("waitpid");
      exit(1);
    }
    if (p == 0)
      break;
    explain_wait_status(p, status);
    if (WIFEXITED(status) || WIFSIGNALED(status)) {
      printf("Parent: Received SIGCHLD, child is dead. Exiting.\n");
      exit(0);
    }
    if (WIFSTOPPED(status)) {
      printf("Parent: Child has been stopped. Moving right along...\n");
    }
  }
}


x86 assembly (clang, -O0):
<sigchld_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%eax
je     11cf <sigchld_handler+0x3f>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe40(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    11d4 <sigchld_handler+0x44>
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
mov    0x2e04(%rip),%rax
or     (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x14(%rbp),%rsi
call   1070 <waitpid@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1219 <sigchld_handler+0x89>
lea    0xe29(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1229 <sigchld_handler+0x99>
jmp    128f <sigchld_handler+0xff>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10b0 <explain_wait_status@plt>
mov    -0x14(%rbp),%edi
call   1090 <WIFEXITED@plt>
cmp    $0x0,%rax
jne    1259 <sigchld_handler+0xc9>
mov    -0x14(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     126c <sigchld_handler+0xdc>
lea    0xddb(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1060 <WIFSTOPPED@plt>
cmp    $0x0,%rax
je     128a <sigchld_handler+0xfa>
lea    0xde9(%rip),%rdi
call   1030 <printf@plt>
jmp    11d4 <sigchld_handler+0x44>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sigchld_handler>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e41(%rip),%rax
cmp    %edi,(%rax)
jne    1283 <sigchld_handler+0xf3>
mov    0x2e22(%rip),%r15
mov    0x2e33(%rip),%r12
mov    (%r12),%edx
or     (%r15),%edx
lea    0x4(%rsp),%rsi
mov    $0xffffffff,%edi
call   1070 <waitpid@plt>
test   %rax,%rax
js     126d <sigchld_handler+0xdd>
lea    0x4(%rsp),%rbx
lea    0xe5b(%rip),%r14
jmp    1209 <sigchld_handler+0x79>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r12),%edx
or     (%r15),%edx
mov    $0xffffffff,%edi
mov    %rbx,%rsi
call   1070 <waitpid@plt>
test   %rax,%rax
js     126d <sigchld_handler+0xdd>
test   %rax,%rax
je     124e <sigchld_handler+0xbe>
mov    0x4(%rsp),%esi
mov    %rax,%rdi
call   10b0 <explain_wait_status@plt>
mov    0x4(%rsp),%edi
call   1090 <WIFEXITED@plt>
test   %rax,%rax
jne    125a <sigchld_handler+0xca>
mov    0x4(%rsp),%edi
call   1040 <WIFSIGNALED@plt>
test   %rax,%rax
jne    125a <sigchld_handler+0xca>
mov    0x4(%rsp),%edi
call   1060 <WIFSTOPPED@plt>
test   %rax,%rax
je     11f0 <sigchld_handler+0x60>
mov    %r14,%rdi
call   1030 <puts@plt>
jmp    11f0 <sigchld_handler+0x60>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xe10(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
lea    0xdbf(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %edi,%edx
mov    0x2d3c(%rip),%rax
mov    (%rax),%edi
lea    0xd6b(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@SIGCHLD = global i32 0
@stderr = global i32 0
@.str = [51 x i8] c"Internal error: Called for signum %d, not SIGCHLD\0A\00"
@WUNTRACED = global i32 0
@WNOHANG = global i32 0
@.str.1 = [8 x i8] c"waitpid\00"
@.str.2 = [51 x i8] c"Parent: Received SIGCHLD, child is dead. Exiting.\0A\00"
@.str.3 = [55 x i8] c"Parent: Child has been stopped. Moving right along...\0A\00"
define void @sigchld_handler(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = load i32, i32* @SIGCHLD
%7 = icmp ne i32 %5, %6
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = load i32, i32* %2
%11 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
br label %14
14: ; preds = %48, %13
%15 = load i32, i32* @WUNTRACED
%16 = load i32, i32* @WNOHANG
%17 = or i32 %15, %16
%18 = call i64 @waitpid(i32 -1, i32* %4, i32 %17)
store i64 %18, i64* %3
%19 = load i64, i64* %3
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %14
%25 = load i64, i64* %3
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %24
br label %49
28: ; preds = %24
%29 = load i64, i64* %3
%30 = load i32, i32* %4
%31 = call i32 @explain_wait_status(i64 %29, i32 %30)
%32 = load i32, i32* %4
%33 = call i64 @WIFEXITED(i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %39, label %35
35: ; preds = %28
%36 = load i32, i32* %4
%37 = call i64 @WIFSIGNALED(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %35, %28
%40 = call i32 @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 0)
unreachable
42: ; preds = %35
%43 = load i32, i32* %4
%44 = call i64 @WIFSTOPPED(i32 %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %42
%47 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.3, i64 0, i64 0))
br label %48
48: ; preds = %46, %42
br label %14
49: ; preds = %27
ret void
}

LLVM IR (-O2):
@SIGCHLD = global i32 0
@stderr = global i32 0
@.str = [51 x i8] c"Internal error: Called for signum %d, not SIGCHLD\0A\00"
@WUNTRACED = global i32 0
@WNOHANG = global i32 0
@.str.1 = [8 x i8] c"waitpid\00"
@str = [54 x i8] c"Parent: Child has been stopped. Moving right along...\00"
@str.4 = [50 x i8] c"Parent: Received SIGCHLD, child is dead. Exiting.\00"
define void @sigchld_handler(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @SIGCHLD
%5 = icmp eq i32 %4, %0
br i1 %5, label %6, label %12
6: ; preds = %1
%7 = load i32, i32* @WUNTRACED
%8 = load i32, i32* @WNOHANG
%9 = or i32 %8, %7
%10 = call i64 @waitpid(i32 -1, i32* %2, i32 %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %16, label %19
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0), i32 %0)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %41, %6
%17 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %6, %41
%20 = phi i64 [ %45, %41 ], [ %10, %6 ]
%21 = icmp eq i64 %20, 0
br i1 %21, label %47, label %22
22: ; preds = %19
%23 = load i32, i32* %2
%24 = call i32 @explain_wait_status(i64 %20, i32 %23)
%25 = load i32, i32* %2
%26 = call i64 @WIFEXITED(i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %22
%29 = load i32, i32* %2
%30 = call i64 @WIFSIGNALED(i32 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %35, label %32
32: ; preds = %28, %22
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @str.4, i64 0, i64 0))
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %28
%36 = load i32, i32* %2
%37 = call i64 @WIFSTOPPED(i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %41, label %39
39: ; preds = %35
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str, i64 0, i64 0))
br label %41
41: ; preds = %39, %35
%42 = load i32, i32* @WUNTRACED
%43 = load i32, i32* @WNOHANG
%44 = or i32 %43, %42
%45 = call i64 @waitpid(i32 -1, i32* %2, i32 %44)
%46 = icmp slt i64 %45, 0
br i1 %46, label %16, label %19
47: ; preds = %19
ret void
}