#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 *, int);
scalar_t__ signal(int /*<<< orphan*/, void (*)(int));
int wait(int *);
void sig_cld(int signo) {
  pid_t pid;
  int status;
  if (signal(SIGCLD, sig_cld) == SIG_ERR)
    perror("signal error");
  if ((pid = wait(&status)) < 0)
    perror("wait error");
  printf("pid = %d\n", pid);
  return;
}


x86 assembly (clang, -O0):
<sig_cld>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e96(%rip),%rax
mov    (%rax),%edi
mov    0x2e85(%rip),%rsi
call   1040 <signal@plt>
mov    0x2e69(%rip),%rcx
cmp    (%rcx),%rax
jne    117c <sig_cld+0x3c>
lea    0xe89(%rip),%rdi
call   1050 <perror@plt>
lea    -0xc(%rbp),%rdi
call   1060 <wait@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    119d <sig_cld+0x5d>
lea    0xe75(%rip),%rdi
call   1050 <perror@plt>
mov    -0x8(%rbp),%esi
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sig_cld>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e9c(%rip),%rax
mov    (%rax),%edi
mov    0x2e8b(%rip),%rsi
call   1040 <signal@plt>
mov    0x2e6f(%rip),%rcx
cmp    (%rcx),%rax
je     118a <sig_cld+0x4a>
lea    0xc(%rsp),%rdi
call   1060 <wait@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1198 <sig_cld+0x58>
lea    0xe9b(%rip),%rdi
mov    %ebx,%esi
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe6f(%rip),%rdi
call   1050 <perror@plt>
jmp    1166 <sig_cld+0x26>
lea    0xe6e(%rip),%rdi
call   1050 <perror@plt>
jmp    1176 <sig_cld+0x36>

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

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