#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 /*<<< orphan*/ buf;

/* Variables and functions */
size_t backtrace(void **, int);
char **backtrace_symbols(void **, size_t);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
void sighndl(int sig) {
  void *buf[32];
  printf("*********** segfault ***********\n\n");
  size_t size = backtrace(buf, sizeof(buf));
  char **trace = backtrace_symbols(buf, size);
  printf("backtrace:\n");
  for (size_t i = 0; i < size; i++)
    fprintf(stderr, "%s\n", trace[i]);
  exit(-1);
}


x86 assembly (clang, -O0):
<sighndl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe9b(%rip),%rdi
call   1050 <printf@plt>
lea    -0x110(%rbp),%rdi
mov    $0x100,%esi
call   1040 <backtrace@plt>
mov    %rax,-0x118(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x118(%rbp),%rsi
call   1030 <backtrace_symbols@plt>
mov    %rax,-0x120(%rbp)
lea    0xe80(%rip),%rdi
call   1050 <printf@plt>
movq   $0x0,-0x128(%rbp)
mov    -0x128(%rbp),%rax
cmp    -0x118(%rbp),%rax
jae    1205 <sighndl+0xb5>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    -0x120(%rbp),%rax
mov    -0x128(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe46(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x128(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x128(%rbp)
jmp    11b3 <sighndl+0x63>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<sighndl>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x100,%rsp
lea    0xe9d(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1050 <backtrace@plt>
mov    %rax,%r14
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <backtrace_symbols@plt>
mov    %rax,%r12
lea    0xe92(%rip),%rdi
call   1030 <puts@plt>
test   %r14,%r14
je     11c9 <sighndl+0x79>
xor    %ebx,%ebx
mov    0x2e39(%rip),%r13
lea    0xe52(%rip),%r15
xchg   %ax,%ax
mov    0x0(%r13),%edi
mov    (%r12,%rbx,8),%rdx
mov    %r15,%rsi
call   1060 <fprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    11b0 <sighndl+0x60>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"*********** segfault ***********\0A\0A\00"
@.str.1 = [12 x i8] c"backtrace:\0A\00"
@stderr = global i32 0
@.str.2 = [4 x i8] c"%s\0A\00"
define void @sighndl(i32 %0) {
%2 = alloca i32
%3 = alloca [32 x i8*]
%4 = alloca i64
%5 = alloca i8**
%6 = alloca i64
store i32 %0, i32* %2
%7 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [32 x i8*], [32 x i8*]* %3, i64 0, i64 0
%9 = call i64 @backtrace(i8** %8, i32 256)
store i64 %9, i64* %4
%10 = getelementptr inbounds [32 x i8*], [32 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*** %5
%13 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i64 0, i64* %6
br label %14
14: ; preds = %25, %1
%15 = load i64, i64* %6
%16 = load i64, i64* %4
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %28
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = load i8**, i8*** %5
%21 = load i64, i64* %6
%22 = getelementptr inbounds i8*, i8** %20, i64 %21
%23 = load i8*, i8** %22
%24 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %18
%26 = load i64, i64* %6
%27 = add i64 %26, 1
store i64 %27, i64* %6
br label %14
28: ; preds = %14
%29 = call i32 @exit(i32 -1)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str.2 = [4 x i8] c"%s\0A\00"
@str = [34 x i8] c"*********** segfault ***********\0A\00"
@str.3 = [11 x i8] c"backtrace:\00"
define void @sighndl(i32 %0) {
%2 = alloca [32 x i8*]
%3 = bitcast [32 x i8*]* %2 to i8*
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%5 = getelementptr inbounds [32 x i8*], [32 x i8*]* %2, i64 0, i64 0
%6 = call i64 @backtrace(i8** %5, i32 256)
%7 = call i8** @backtrace_symbols(i8** %5, i64 %6)
%8 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.3, i64 0, i64 0))
%9 = icmp eq i64 %6, 0
br i1 %9, label %10, label %12
10: ; preds = %12, %1
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %1, %12
%13 = phi i64 [ %18, %12 ], [ 0, %1 ]
%14 = load i32, i32* @stderr
%15 = getelementptr inbounds i8*, i8** %7, i64 %13
%16 = load i8*, i8** %15
%17 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%18 = add nuw i64 %13, 1
%19 = icmp eq i64 %18, %6
br i1 %19, label %10, label %12
}