#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 */

/* Variables and functions */
int const SIGCHLD;
scalar_t__ WIFSIGNALED(int);
int /*<<< orphan*/ WNOHANG;
int WTERMSIG(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int waitpid(int, int *, int /*<<< orphan*/);
void signal_treatment(const int sig) {
  if (sig == SIGCHLD) {
    int status;
    if (waitpid(-1, &status, WNOHANG) == -1)
      perror("waitpid");
    if (WIFSIGNALED(status))
      printf("killed by %d\n", WTERMSIG(status));
  }
}


x86 assembly (clang, -O0):
<signal_treatment>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    0x2e7b(%rip),%rcx
cmp    (%rcx),%eax
jne    11c6 <signal_treatment+0x76>
mov    0x2e74(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x8(%rbp),%rsi
call   1060 <waitpid@plt>
cmp    $0xffffffff,%eax
jne    1199 <signal_treatment+0x49>
lea    0xe70(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     11c1 <signal_treatment+0x71>
mov    -0x8(%rbp),%edi
call   1050 <WTERMSIG@plt>
mov    %eax,%esi
lea    0xe50(%rip),%rdi
call   1030 <printf@plt>
jmp    11c6 <signal_treatment+0x76>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<signal_treatment>:
test   %edi,%edi
je     1155 <signal_treatment+0x5>
ret
push   %rax
mov    0x2e8b(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rsi
mov    $0xffffffff,%edi
call   1060 <waitpid@plt>
cmp    $0xffffffff,%eax
je     119d <signal_treatment+0x4d>
mov    0x4(%rsp),%edi
call   1040 <WIFSIGNALED@plt>
test   %rax,%rax
je     1198 <signal_treatment+0x48>
mov    0x4(%rsp),%edi
call   1050 <WTERMSIG@plt>
lea    0xe7b(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
add    $0x8,%rsp
ret
lea    0xe60(%rip),%rdi
call   1070 <perror@plt>
jmp    1173 <signal_treatment+0x23>

LLVM IR (-O0):
@SIGCHLD = constant i32 0
@WNOHANG = global i32 0
@.str = [8 x i8] c"waitpid\00"
@.str.1 = [14 x i8] c"killed by %d\0A\00"
define void @signal_treatment(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = load i32, i32* @SIGCHLD
%6 = icmp eq i32 %4, %5
br i1 %6, label %7, label %22
7: ; preds = %1
%8 = load i32, i32* @WNOHANG
%9 = call i32 @waitpid(i32 -1, i32* %3, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %7
%14 = load i32, i32* %3
%15 = call i64 @WIFSIGNALED(i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = load i32, i32* %3
%19 = call i32 @WTERMSIG(i32 %18)
%20 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %17, %13
br label %22
22: ; preds = %21, %1
ret void
}

LLVM IR (-O2):
@SIGCHLD = constant i32 0
@WNOHANG = global i32 0
@.str = [8 x i8] c"waitpid\00"
@.str.1 = [14 x i8] c"killed by %d\0A\00"
define void @signal_treatment(i32 %0) {
%2 = alloca i32
%3 = icmp eq i32 %0, 0
br i1 %3, label %4, label %20
4: ; preds = %1
%5 = bitcast i32* %2 to i8*
%6 = load i32, i32* @WNOHANG
%7 = call i32 @waitpid(i32 -1, i32* %2, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %4
%12 = load i32, i32* %2
%13 = call i64 @WIFSIGNALED(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = load i32, i32* %2
%17 = call i32 @WTERMSIG(i32 %16)
%18 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %15, %11
br label %20
20: ; preds = %19, %1
ret void
}