#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 */
size_t backtrace(void **, int);
char **backtrace_symbols(void **, size_t);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char **);
int /*<<< orphan*/ printf(char *, ...);
void sigsegv_handler(int signum) {
  printf("#sigsegv# receive SIGSEGV signal\n");
  void *array[30];
  size_t size, i;
  char **str;
  size = backtrace(array, 30);
  str = backtrace_symbols(array, size);
  printf("#sigsegv# obtained %d stack frames\n", size);
  for (i = 0; i < size; i++)
    printf("%s\n", str[i]);
  free(str);
  exit(0);
}


x86 assembly (clang, -O0):
<sigsegv_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe9b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x100(%rbp),%rdi
mov    $0x1e,%esi
call   1050 <backtrace@plt>
mov    %rax,-0x108(%rbp)
lea    -0x100(%rbp),%rdi
mov    -0x108(%rbp),%rsi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0x118(%rbp)
mov    -0x108(%rbp),%rsi
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movq   $0x0,-0x110(%rbp)
mov    -0x110(%rbp),%rax
cmp    -0x108(%rbp),%rax
jae    1209 <sigsegv_handler+0xb9>
mov    -0x118(%rbp),%rax
mov    -0x110(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x110(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x110(%rbp)
jmp    11be <sigsegv_handler+0x6e>
mov    -0x118(%rbp),%rdi
call   1030 <free@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<sigsegv_handler>:
push   %r15
push   %r14
push   %rbx
sub    $0xf0,%rsp
lea    0xeb1(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1e,%esi
call   1060 <backtrace@plt>
mov    %rax,%r14
mov    %rbx,%rdi
mov    %rax,%rsi
call   1050 <backtrace_symbols@plt>
mov    %rax,%r15
lea    0xe60(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
test   %r14,%r14
je     11d2 <sigsegv_handler+0x72>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r15,%rbx,8),%rdi
call   1040 <puts@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    11c0 <sigsegv_handler+0x60>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [34 x i8] c"#sigsegv# receive SIGSEGV signal\0A\00"
@.str.1 = [36 x i8] c"#sigsegv# obtained %d stack frames\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define void @sigsegv_handler(i32 %0) {
%2 = alloca i32
%3 = alloca [30 x i8*]
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8**
store i32 %0, i32* %2
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [30 x i8*], [30 x i8*]* %3, i64 0, i64 0
%9 = call i64 @backtrace(i8** %8, i32 30)
store i64 %9, i64* %4
%10 = getelementptr inbounds [30 x i8*], [30 x i8*]* %3, i64 0, i64 0
%11 = load i64, i64* %4
%12 = call i8** @backtrace_symbols(i8** %10, i64 %11)
store i8** %12, i8*** %6
%13 = load i64, i64* %4
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i64 %13)
store i64 0, i64* %5
br label %15
15: ; preds = %25, %1
%16 = load i64, i64* %5
%17 = load i64, i64* %4
%18 = icmp ult i64 %16, %17
br i1 %18, label %19, label %28
19: ; preds = %15
%20 = load i8**, i8*** %6
%21 = load i64, i64* %5
%22 = getelementptr inbounds i8*, i8** %20, i64 %21
%23 = load i8*, i8** %22
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %19
%26 = load i64, i64* %5
%27 = add i64 %26, 1
store i64 %27, i64* %5
br label %15
28: ; preds = %15
%29 = load i8**, i8*** %6
%30 = call i32 @free(i8** %29)
%31 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [36 x i8] c"#sigsegv# obtained %d stack frames\0A\00"
@str = [33 x i8] c"#sigsegv# receive SIGSEGV signal\00"
define void @sigsegv_handler(i32 %0) {
%2 = alloca [30 x i8*]
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%4 = bitcast [30 x i8*]* %2 to i8*
%5 = getelementptr inbounds [30 x i8*], [30 x i8*]* %2, i64 0, i64 0
%6 = call i64 @backtrace(i8** %5, i32 30)
%7 = call i8** @backtrace_symbols(i8** %5, i64 %6)
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i64 %6)
%9 = icmp eq i64 %6, 0
br i1 %9, label %17, label %10
10: ; preds = %1, %10
%11 = phi i64 [ %15, %10 ], [ 0, %1 ]
%12 = getelementptr inbounds i8*, i8** %7, i64 %11
%13 = load i8*, i8** %12
%14 = call i32 @puts(i8* dereferenceable(1) %13)
%15 = add nuw i64 %11, 1
%16 = icmp eq i64 %15, %6
br i1 %16, label %17, label %10
17: ; preds = %10, %1
%18 = call i32 @free(i8** %7)
%19 = call i32 @exit(i32 0)
unreachable
}