#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 /*<<< orphan*/ SIGCHLD;
scalar_t__ SIG_ERR;
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ signal(int /*<<< orphan*/, void (*)(int));
int wait(int *);
void sig_cld(int signo) {
  int status;
  pid_t pid;
  printf("signal SIGCHLD received\n");
  if (signal(SIGCHLD, sig_cld) == SIG_ERR) {
    err_sys("error on establishing signal handler for SIGCHLD");
  }
  if ((pid = wait(&status)) < 0) {
    err_sys("error on wait");
  }
  printf("pid = %d\n", pid);
}


x86 assembly (clang, -O0):
<sig_cld>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
lea    0xeae(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e88(%rip),%rax
mov    (%rax),%edi
mov    0x2e77(%rip),%rsi
call   1050 <signal@plt>
mov    0x2e5b(%rip),%rcx
cmp    (%rcx),%rax
jne    118a <sig_cld+0x4a>
lea    0xe94(%rip),%rdi
call   1030 <err_sys@plt>
lea    -0x8(%rbp),%rdi
call   1060 <wait@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11ab <sig_cld+0x6b>
lea    0xea4(%rip),%rdi
call   1030 <err_sys@plt>
mov    -0xc(%rbp),%esi
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sig_cld>:
push   %rbx
sub    $0x10,%rsp
lea    0xeed(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
mov    0x2e6f(%rip),%rsi
call   1060 <signal@plt>
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%rax
jne    118e <sig_cld+0x3e>
lea    0xe77(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11aa <sig_cld+0x5a>
lea    0xe8c(%rip),%rdi
call   1040 <err_sys@plt>
lea    0xe8e(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [25 x i8] c"signal SIGCHLD received\0A\00"
@SIGCHLD = global i32 0
@SIG_ERR = global i64 0
@.str.1 = [49 x i8] c"error on establishing signal handler for SIGCHLD\00"
@.str.2 = [14 x i8] c"error on wait\00"
@.str.3 = [10 x i8] c"pid = %d\0A\00"
define void @sig_cld(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @SIGCHLD
%7 = call i64 @signal(i32 %6, void (i32)* @sig_cld)
%8 = load i64, i64* @SIG_ERR
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @err_sys(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %1
%13 = call i32 @wait(i32* %3)
store i32 %13, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = load i32, i32* %4
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %18)
ret void
}

LLVM IR (-O2):
@SIGCHLD = global i32 0
@SIG_ERR = global i64 0
@.str.1 = [49 x i8] c"error on establishing signal handler for SIGCHLD\00"
@.str.2 = [14 x i8] c"error on wait\00"
@.str.3 = [10 x i8] c"pid = %d\0A\00"
@str = [24 x i8] c"signal SIGCHLD received\00"
define void @sig_cld(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%5 = load i32, i32* @SIGCHLD
%6 = tail call i64 @signal(i32 %5, void (i32)* @sig_cld)
%7 = load i64, i64* @SIG_ERR
%8 = icmp eq i64 %6, %7
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @err_sys(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %1
%12 = call i32 @wait(i32* %2)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %12)
ret void
}