#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 */
scalar_t__ SIGSEGV;
scalar_t__ WIFSIGNALED(int);
int /*<<< orphan*/ WNOHANG;
scalar_t__ WTERMSIG(int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ waitpid(int, int *, int /*<<< orphan*/);
void traitement_signal(int sig) {
  printf("Signal %d recu \n", sig);
  int status;
  while (waitpid(-1, &status, WNOHANG) > 0) {
    if (WIFSIGNALED(status)) {
      if (WTERMSIG(status) == SIGSEGV) {
        printf("Erreur de segmentation !\n");
      }
    }
  }
}


x86 assembly (clang, -O0):
<traitement_signal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%esi
lea    0xeab(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e7d(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%edi
lea    -0x8(%rbp),%rsi
call   1060 <waitpid@plt>
cmp    $0x0,%rax
jle    11bf <traitement_signal+0x7f>
mov    -0x8(%rbp),%edi
call   1040 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     11ba <traitement_signal+0x7a>
mov    -0x8(%rbp),%edi
call   1050 <WTERMSIG@plt>
mov    0x2e4a(%rip),%rcx
cmp    (%rcx),%rax
jne    11b5 <traitement_signal+0x75>
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11ba <traitement_signal+0x7a>
jmp    115c <traitement_signal+0x1c>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<traitement_signal>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edi,%esi
lea    0xe9f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e71(%rip),%r15
mov    (%r15),%edx
lea    0x4(%rsp),%rsi
mov    $0xffffffff,%edi
call   1070 <waitpid@plt>
test   %rax,%rax
jle    11dc <traitement_signal+0x8c>
lea    0x4(%rsp),%rbx
mov    0x2e56(%rip),%r12
lea    0xe78(%rip),%r14
jmp    11b5 <traitement_signal+0x65>
nopl   0x0(%rax,%rax,1)
mov    (%r15),%edx
mov    $0xffffffff,%edi
mov    %rbx,%rsi
call   1070 <waitpid@plt>
test   %rax,%rax
jle    11dc <traitement_signal+0x8c>
mov    0x4(%rsp),%edi
call   1050 <WIFSIGNALED@plt>
test   %rax,%rax
je     11a0 <traitement_signal+0x50>
mov    0x4(%rsp),%edi
call   1060 <WTERMSIG@plt>
cmp    (%r12),%rax
jne    11a0 <traitement_signal+0x50>
mov    %r14,%rdi
call   1030 <puts@plt>
jmp    11a0 <traitement_signal+0x50>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [17 x i8] c"Signal %d recu \0A\00"
@WNOHANG = global i32 0
@SIGSEGV = global i64 0
@str = [25 x i8] c"Erreur de segmentation !\00"
define void @traitement_signal(i32 %0) {
%2 = alloca i32
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %0)
%4 = bitcast i32* %2 to i8*
%5 = load i32, i32* @WNOHANG
%6 = call i64 @waitpid(i32 -1, i32* %2, i32 %5)
%7 = icmp sgt i64 %6, 0
br i1 %7, label %8, label %23
8: ; preds = %1, %19
%9 = load i32, i32* %2
%10 = call i64 @WIFSIGNALED(i32 %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %19, label %12
12: ; preds = %8
%13 = load i32, i32* %2
%14 = call i64 @WTERMSIG(i32 %13)
%15 = load i64, i64* @SIGSEGV
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %19
19: ; preds = %12, %17, %8
%20 = load i32, i32* @WNOHANG
%21 = call i64 @waitpid(i32 -1, i32* %2, i32 %20)
%22 = icmp sgt i64 %21, 0
br i1 %22, label %8, label %23
23: ; preds = %19, %1
ret void
}