#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 waitpid(int, int *, int /*<<< orphan*/);
void sig_child(int sig_no) {
  pid_t pid;
  int stat;
  if (SIGCHLD == sig_no) {
    printf("recv SIGCHLD signal.\n");
  }
  while ((pid = waitpid(-1, &stat, WNOHANG)) > 0) {
    printf("process (%d) terminate.\n", pid);
  }
}


x86 assembly (clang, -O0):
<sig_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2eae(%rip),%rax
mov    (%rax),%eax
cmp    -0x4(%rbp),%eax
jne    114b <sig_child+0x2b>
lea    0xebc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1150 <sig_child+0x30>
mov    0x2e91(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0xc(%rbp),%rsi
call   1040 <waitpid@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jle    1189 <sig_child+0x69>
mov    -0x8(%rbp),%esi
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1150 <sig_child+0x30>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sig_child>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2ea0(%rip),%rax
cmp    %edi,(%rax)
jne    1150 <sig_child+0x20>
lea    0xece(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e91(%rip),%r15
mov    (%r15),%edx
lea    0xc(%rsp),%rsi
mov    $0xffffffff,%edi
call   1050 <waitpid@plt>
test   %eax,%eax
jle    11a0 <sig_child+0x70>
lea    0xe8c(%rip),%r14
lea    0xc(%rsp),%rbx
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r15),%edx
mov    $0xffffffff,%edi
mov    %rbx,%rsi
call   1050 <waitpid@plt>
test   %eax,%eax
jg     1180 <sig_child+0x50>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@SIGCHLD = global i32 0
@WNOHANG = global i32 0
@.str.1 = [25 x i8] c"process (%d) terminate.\0A\00"
@str = [21 x i8] c"recv SIGCHLD signal.\00"
define void @sig_child(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 %8
6: ; preds = %1
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %8
8: ; preds = %6, %1
%9 = load i32, i32* @WNOHANG
%10 = call i32 @waitpid(i32 -1, i32* %2, i32 %9)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %8, %12
%13 = phi i32 [ %16, %12 ], [ %10, %8 ]
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @WNOHANG
%16 = call i32 @waitpid(i32 -1, i32* %2, i32 %15)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %12, label %18
18: ; preds = %12, %8
ret void
}