#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*/ EXIT_FAILURE;
int /*<<< orphan*/ SIGCHLD;
scalar_t__ SIG_ERR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *, int, int, int);
scalar_t__ signal(int /*<<< orphan*/, void (*)(int));
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int waitpid(int, int *, int /*<<< orphan*/);
void signal_handler(int sig_num) {
  pid_t pid;
  int child_status;
  if ((pid = waitpid(-1, &child_status, 0)) < 0) {
    fprintf(stderr, "Err.(%s) waitpid() error\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  printf("Segnale '%d' verso '%d' - child status: %d\n", sig_num, pid,
         child_status);
  if (signal(SIGCHLD, signal_handler) == SIG_ERR) {
    fprintf(stderr, "Err.(%s) signal() 'sig-han' error\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<signal_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0xffffffff,%edi
lea    -0xc(%rbp),%rsi
xor    %edx,%edx
call   1060 <waitpid@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11c1 <signal_handler+0x61>
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x10(%rbp),%edi
mov    %rax,%rdx
lea    0xe52(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xe49(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    0x2dea(%rip),%rsi
call   1040 <signal@plt>
mov    0x2dd6(%rip),%rcx
cmp    (%rcx),%rax
jne    1235 <signal_handler+0xd5>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%rdx
lea    0xe24(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<signal_handler>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
lea    0xc(%rsp),%rsi
mov    $0xffffffff,%edi
xor    %edx,%edx
call   1060 <waitpid@plt>
test   %eax,%eax
js     11b7 <signal_handler+0x57>
mov    0xc(%rsp),%ecx
lea    0xe93(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1030 <printf@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    0x2e30(%rip),%rsi
call   1040 <signal@plt>
mov    0x2e1c(%rip),%rcx
cmp    (%rcx),%rax
je     11d7 <signal_handler+0x77>
add    $0x10,%rsp
pop    %rbx
ret
mov    0x2e1a(%rip),%rax
mov    (%rax),%ebx
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe2b(%rip),%rsi
jmp    11f5 <signal_handler+0x95>
mov    0x2dfa(%rip),%rax
mov    (%rax),%ebx
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe51(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"Err.(%s) waitpid() error\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [44 x i8] c"Segnale '%d' verso '%d' - child status: %d\0A\00"
@SIGCHLD = global i32 0
@SIG_ERR = global i64 0
@.str.2 = [35 x i8] c"Err.(%s) signal() 'sig-han' error\0A\00"
define void @signal_handler(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i32 @waitpid(i32 -1, i32* %4, i32 0)
store i32 %5, i32* %3
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %1
%15 = load i32, i32* %2
%16 = load i32, i32* %3
%17 = load i32, i32* %4
%18 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %15, i32 %16, i32 %17)
%19 = load i32, i32* @SIGCHLD
%20 = call i64 @signal(i32 %19, void (i32)* @signal_handler)
%21 = load i64, i64* @SIG_ERR
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %30
23: ; preds = %14
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %14
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"Err.(%s) waitpid() error\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.1 = [44 x i8] c"Segnale '%d' verso '%d' - child status: %d\0A\00"
@SIGCHLD = global i32 0
@SIG_ERR = global i64 0
@.str.2 = [35 x i8] c"Err.(%s) signal() 'sig-han' error\0A\00"
define void @signal_handler(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @waitpid(i32 -1, i32* %2, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %13
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = call i8* @strerror(i32 %8)
%10 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %1
%14 = load i32, i32* %2
%15 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %0, i32 %4, i32 %14)
%16 = load i32, i32* @SIGCHLD
%17 = call i64 @signal(i32 %16, void (i32)* @signal_handler)
%18 = load i64, i64* @SIG_ERR
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %27
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %13
ret void
}