#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*/ exit(int);
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) {
    printf("signal error\n");
    exit(1);
  }
  if ((pid = wait(&status)) < 0) {
    printf("wait error\n");
    exit(1);
  }
  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    1196 <sig_cld+0x56>
lea    0xe8c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
lea    -0xc(%rbp),%rdi
call   1060 <wait@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11c3 <sig_cld+0x83>
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x8(%rbp),%esi
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sig_cld>:
push   %rax
lea    0xeb0(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
mov    0x2e73(%rip),%rsi
call   1050 <signal@plt>
mov    0x2e57(%rip),%rcx
cmp    (%rcx),%rax
je     119e <sig_cld+0x4e>
lea    0x4(%rsp),%rdi
call   1070 <wait@plt>
test   %eax,%eax
js     11a7 <sig_cld+0x57>
lea    0xe6d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
pop    %rax
ret
lea    0xe7e(%rip),%rdi
jmp    11ae <sig_cld+0x5e>
lea    0xe6a(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
@SIGCLD = global i32 0
@SIG_ERR = global i64 0
@.str.3 = [8 x i8] c"pid=%d\0A\00"
@str = [16 x i8] c"SIGCLD received\00"
@str.4 = [11 x i8] c"wait error\00"
@str.5 = [13 x i8] c"signal error\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 %12
9: ; preds = %1
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.5, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = call i32 @wait(i32* %2)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.4, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %12
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %13)
ret void
}