#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*/ ucontext_t;
typedef int /*<<< orphan*/ siginfo_t;

/* Variables and functions */
int SIGABRT;
int /*<<< orphan*/ SIG_DFL;
int /*<<< orphan*/ abort();
int backtrace(void **, int);
char **backtrace_symbols(void **, int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char **);
int /*<<< orphan*/ signal(int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strsignal(int);
void _critical_stop_handler(int signum, siginfo_t *info, void *ucontext) {
  void *callstack[100];
  int i, frames = 0;
  char **strs;
  void *caller_address = NULL;
  ucontext_t *uc = ucontext;
  signal(signum, SIG_DFL);
  signal(SIGABRT, SIG_DFL);
  fflush(stdout);
  fprintf(stderr, "\n\nCritical error: received signal %s. Unexpected exit.\n",
          strsignal(signum));
  fprintf(stderr, "Trying to get the backtrace (max. depth %d)...\n", 100);
  frames = backtrace(callstack, 100);
  if (uc != NULL) {
  }
  if (frames > 3 && caller_address != NULL)
    callstack[2] = caller_address;
  strs = backtrace_symbols(callstack, frames);
  for (i = 0; i < frames; ++i)
    fprintf(stderr, "%s\n", strs[i]);
  free(strs);
  abort();
}


x86 assembly (clang, -O0):
<_critical_stop_handler>:
push   %rbp
mov    %rsp,%rbp
sub    $0x370,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x348(%rbp)
movq   $0x0,-0x358(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x360(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x364(%rbp)
mov    -0x4(%rbp),%edi
call   10a0 <strsignal@plt>
mov    -0x364(%rbp),%edi
mov    %rax,%rdx
lea    0xded(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
lea    0xe0e(%rip),%rsi
mov    $0x64,%edx
mov    $0x0,%al
call   1080 <fprintf@plt>
lea    -0x340(%rbp),%rdi
mov    $0x64,%esi
call   1060 <backtrace@plt>
mov    %eax,-0x348(%rbp)
cmpq   $0x0,-0x360(%rbp)
je     1260 <_critical_stop_handler+0xe0>
jmp    1260 <_critical_stop_handler+0xe0>
cmpl   $0x3,-0x348(%rbp)
jle    1289 <_critical_stop_handler+0x109>
cmpq   $0x0,-0x358(%rbp)
je     1289 <_critical_stop_handler+0x109>
mov    -0x358(%rbp),%rax
mov    %rax,-0x330(%rbp)
lea    -0x340(%rbp),%rdi
mov    -0x348(%rbp),%esi
call   1050 <backtrace_symbols@plt>
mov    %rax,-0x350(%rbp)
movl   $0x0,-0x344(%rbp)
mov    -0x344(%rbp),%eax
cmp    -0x348(%rbp),%eax
jge    12fb <_critical_stop_handler+0x17b>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
mov    -0x350(%rbp),%rax
movslq -0x344(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xd88(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x344(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x344(%rbp)
jmp    12ac <_critical_stop_handler+0x12c>
mov    -0x350(%rbp),%rdi
call   1030 <free@plt>
mov    $0x0,%al
call   1040 <abort@plt>

x86 assembly (clang, -O2):
<_critical_stop_handler>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x320,%rsp
mov    %edi,%ebx
mov    0x2e40(%rip),%rbp
mov    0x0(%rbp),%esi
call   1070 <signal@plt>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%esi
call   1070 <signal@plt>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    0x2e1a(%rip),%r15
mov    (%r15),%r14d
mov    %ebx,%edi
call   10a0 <strsignal@plt>
lea    0xe29(%rip),%rsi
mov    %r14d,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%r15),%edi
lea    0xe4a(%rip),%rsi
mov    $0x64,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1060 <backtrace@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <backtrace_symbols@plt>
mov    %rax,%r12
test   %r14d,%r14d
jle    124a <_critical_stop_handler+0xca>
mov    %r14d,%ebp
lea    0xe3f(%rip),%r14
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    (%r15),%edi
mov    (%r12,%rbx,8),%rdx
mov    %r14,%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1230 <_critical_stop_handler+0xb0>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %eax,%eax
call   1040 <abort@plt>

LLVM IR (-O0):
@SIG_DFL = global i32 0
@SIGABRT = global i32 0
@stdout = global i32 0
@stderr = global i32 0
@.str = [56 x i8] c"\0A\0ACritical error: received signal %s. Unexpected exit.\0A\00"
@.str.1 = [48 x i8] c"Trying to get the backtrace (max. depth %d)...\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define void @_critical_stop_handler(i32 %0, i32* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca [100 x i8*]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8**
%11 = alloca i8*
%12 = alloca i32*
store i32 %0, i32* %4
store i32* %1, i32** %5
store i8* %2, i8** %6
store i32 0, i32* %9
store i8* null, i8** %11
%13 = load i8*, i8** %6
%14 = bitcast i8* %13 to i32*
store i32* %14, i32** %12
%15 = load i32, i32* %4
%16 = load i32, i32* @SIG_DFL
%17 = call i32 @signal(i32 %15, i32 %16)
%18 = load i32, i32* @SIGABRT
%19 = load i32, i32* @SIG_DFL
%20 = call i32 @signal(i32 %18, i32 %19)
%21 = load i32, i32* @stdout
%22 = call i32 @fflush(i32 %21)
%23 = load i32, i32* @stderr
%24 = load i32, i32* %4
%25 = call i8* @strsignal(i32 %24)
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i8* %25)
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i32 100)
%29 = getelementptr inbounds [100 x i8*], [100 x i8*]* %7, i64 0, i64 0
%30 = call i32 @backtrace(i8** %29, i32 100)
store i32 %30, i32* %9
%31 = load i32*, i32** %12
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %34
33: ; preds = %3
br label %34
34: ; preds = %33, %3
%35 = load i32, i32* %9
%36 = icmp sgt i32 %35, 3
br i1 %36, label %37, label %43
37: ; preds = %34
%38 = load i8*, i8** %11
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i8*, i8** %11
%42 = getelementptr inbounds [100 x i8*], [100 x i8*]* %7, i64 0, i64 2
store i8* %41, i8** %42
br label %43
43: ; preds = %40, %37, %34
%44 = getelementptr inbounds [100 x i8*], [100 x i8*]* %7, i64 0, i64 0
%45 = load i32, i32* %9
%46 = call i8** @backtrace_symbols(i8** %44, i32 %45)
store i8** %46, i8*** %10
store i32 0, i32* %8
br label %47
47: ; preds = %59, %43
%48 = load i32, i32* %8
%49 = load i32, i32* %9
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %62
51: ; preds = %47
%52 = load i32, i32* @stderr
%53 = load i8**, i8*** %10
%54 = load i32, i32* %8
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8*, i8** %53, i64 %55
%57 = load i8*, i8** %56
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %57)
br label %59
59: ; preds = %51
%60 = load i32, i32* %8
%61 = add nsw i32 %60, 1
store i32 %61, i32* %8
br label %47
62: ; preds = %47
%63 = load i8**, i8*** %10
%64 = call i32 @free(i8** %63)
%65 = call i32 (...) @abort()
unreachable
}

LLVM IR (-O2):
@SIG_DFL = global i32 0
@SIGABRT = global i32 0
@stdout = global i32 0
@stderr = global i32 0
@.str = [56 x i8] c"\0A\0ACritical error: received signal %s. Unexpected exit.\0A\00"
@.str.1 = [48 x i8] c"Trying to get the backtrace (max. depth %d)...\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define void @_critical_stop_handler(i32 %0, i32* nocapture readnone %1, i8* nocapture readnone %2) {
%4 = alloca [100 x i8*]
%5 = bitcast [100 x i8*]* %4 to i8*
%6 = load i32, i32* @SIG_DFL
%7 = tail call i32 @signal(i32 %0, i32 %6)
%8 = load i32, i32* @SIGABRT
%9 = load i32, i32* @SIG_DFL
%10 = tail call i32 @signal(i32 %8, i32 %9)
%11 = load i32, i32* @stdout
%12 = tail call i32 @fflush(i32 %11)
%13 = load i32, i32* @stderr
%14 = tail call i8* @strsignal(i32 %0)
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.1, i64 0, i64 0), i32 100)
%18 = getelementptr inbounds [100 x i8*], [100 x i8*]* %4, i64 0, i64 0
%19 = call i32 @backtrace(i8** %18, i32 100)
%20 = call i8** @backtrace_symbols(i8** %18, i32 %19)
%21 = icmp sgt i32 %19, 0
br i1 %21, label %22, label %32
22: ; preds = %3
%23 = zext i32 %19 to i64
br label %24
24: ; preds = %22, %24
%25 = phi i64 [ 0, %22 ], [ %30, %24 ]
%26 = load i32, i32* @stderr
%27 = getelementptr inbounds i8*, i8** %20, i64 %25
%28 = load i8*, i8** %27
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %28)
%30 = add nuw nsw i64 %25, 1
%31 = icmp eq i64 %30, %23
br i1 %31, label %32, label %24
32: ; preds = %24, %3
%33 = call i32 @free(i8** %20)
%34 = call i32 (...) @abort()
unreachable
}