#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*/ SIGCLD;
scalar_t__ SIG_ERR;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ signal(int /*<<< orphan*/, void (*)(int));
int wait(int *);
void sig_cld(int signo) {
  pid_t pid;
  int status;
  printf("SIGCLD received\n");
  if (signal(SIGCLD, sig_cld) == SIG_ERR) {
    perror("signal error");
  }
  if ((pid = wait(&status)) < 0) {
    perror("wait error");
  }
  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   1030 <printf@plt>
mov    0x2e88(%rip),%rax
mov    (%rax),%edi
mov    0x2e77(%rip),%rsi
call   1040 <signal@plt>
mov    0x2e5b(%rip),%rcx
cmp    (%rcx),%rax
jne    118a <sig_cld+0x4a>
lea    0xe8c(%rip),%rdi
call   1050 <perror@plt>
lea    -0xc(%rbp),%rdi
call   1060 <wait@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11ab <sig_cld+0x6b>
lea    0xe78(%rip),%rdi
call   1050 <perror@plt>
mov    -0x8(%rbp),%esi
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sig_cld>:
push   %rbx
sub    $0x10,%rsp
lea    0xec6(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
mov    0x2e6f(%rip),%rsi
call   1050 <signal@plt>
mov    0x2e53(%rip),%rcx
cmp    (%rcx),%rax
je     11a8 <sig_cld+0x58>
lea    0xc(%rsp),%rdi
call   1070 <wait@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11b6 <sig_cld+0x66>
lea    0xe7f(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe51(%rip),%rdi
call   1060 <perror@plt>
jmp    1182 <sig_cld+0x32>
lea    0xe50(%rip),%rdi
call   1060 <perror@plt>
jmp    1192 <sig_cld+0x42>

LLVM IR (-O0):
@.str = [17 x i8] c"SIGCLD received\0A\00"
@SIGCLD = global i32 0
@SIG_ERR = global i64 0
@.str.1 = [13 x i8] c"signal error\00"
@.str.2 = [11 x i8] c"wait error\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 ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @SIGCLD
%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 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %1
%13 = call i32 @wait(i32* %4)
store i32 %13, i32* %3
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = load i32, i32* %3
%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):
@SIGCLD = global i32 0
@SIG_ERR = global i64 0
@.str.1 = [13 x i8] c"signal error\00"
@.str.2 = [11 x i8] c"wait error\00"
@.str.3 = [10 x i8] c"pid = %d\0A\00"
@str = [16 x i8] c"SIGCLD 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 ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
%5 = load i32, i32* @SIGCLD
%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 @perror(i8* getelementptr inbounds ([13 x i8], [13 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 @perror(i8* getelementptr inbounds ([11 x i8], [11 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
}