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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
size_t backtrace(void **, int);
char **backtrace_symbols(void **, size_t);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char **);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
void dump_stack(void) {
  void *array[30] = {0};
  size_t size = backtrace(array, (sizeof(array) / sizeof(array[0])));
  char **strings = backtrace_symbols(array, size);
  size_t i;
  if (strings == NULL) {
    perror("backtrace_symbols.");
    exit(EXIT_FAILURE);
  }
  printf("Obtained %zd stack frames.\n", size);
  for (i = 0; i < size; i++)
    printf("%s\n", strings[i]);
  free(strings);
  strings = NULL;
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<dump_stack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
lea    -0xf0(%rbp),%rdi
xor    %esi,%esi
mov    $0xf0,%edx
call   1070 <memset@plt>
lea    -0xf0(%rbp),%rdi
mov    $0x1e,%esi
call   1050 <backtrace@plt>
mov    %rax,-0xf8(%rbp)
lea    -0xf0(%rbp),%rdi
mov    -0xf8(%rbp),%rsi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0x100(%rbp)
cmpq   $0x0,-0x100(%rbp)
jne    11e8 <dump_stack+0x78>
lea    0xe2b(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0xf8(%rbp),%rsi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movq   $0x0,-0x108(%rbp)
mov    -0x108(%rbp),%rax
cmp    -0xf8(%rbp),%rax
jae    1253 <dump_stack+0xe3>
mov    -0x100(%rbp),%rax
mov    -0x108(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x108(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x108(%rbp)
jmp    1208 <dump_stack+0x98>
mov    -0x100(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x100(%rbp)
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<dump_stack>:
push   %r15
push   %r14
push   %rbx
sub    $0xf0,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
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>
test   %rax,%rax
jne    121b <dump_stack+0xab>
lea    0xdf8(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    %rax,%r15
lea    0xdee(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
test   %r14,%r14
je     1252 <dump_stack+0xe2>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    (%r15,%rbx,8),%rdi
call   1040 <puts@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1240 <dump_stack+0xd0>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"backtrace_symbols.\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [28 x i8] c"Obtained %zd stack frames.\0A\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@EXIT_SUCCESS = global i32 0
define void @dump_stack() {
%1 = alloca [30 x i8*]
%2 = alloca i64
%3 = alloca i8**
%4 = alloca i64
%5 = bitcast [30 x i8*]* %1 to i8*
%6 = getelementptr inbounds [30 x i8*], [30 x i8*]* %1, i64 0, i64 0
%7 = call i64 @backtrace(i8** %6, i32 30)
store i64 %7, i64* %2
%8 = getelementptr inbounds [30 x i8*], [30 x i8*]* %1, i64 0, i64 0
%9 = load i64, i64* %2
%10 = call i8** @backtrace_symbols(i8** %8, i64 %9)
store i8** %10, i8*** %3
%11 = load i8**, i8*** %3
%12 = icmp eq i8** %11, null
br i1 %12, label %13, label %17
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %0
%18 = load i64, i64* %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i64 %18)
store i64 0, i64* %4
br label %20
20: ; preds = %30, %17
%21 = load i64, i64* %4
%22 = load i64, i64* %2
%23 = icmp ult i64 %21, %22
br i1 %23, label %24, label %33
24: ; preds = %20
%25 = load i8**, i8*** %3
%26 = load i64, i64* %4
%27 = getelementptr inbounds i8*, i8** %25, i64 %26
%28 = load i8*, i8** %27
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %24
%31 = load i64, i64* %4
%32 = add i64 %31, 1
store i64 %32, i64* %4
br label %20
33: ; preds = %20
%34 = load i8**, i8*** %3
%35 = call i32 @free(i8** %34)
store i8** null, i8*** %3
%36 = load i32, i32* @EXIT_SUCCESS
%37 = call i32 @exit(i32 %36)
unreachable
}

LLVM IR (-O2):
@.str = [19 x i8] c"backtrace_symbols.\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [28 x i8] c"Obtained %zd stack frames.\0A\00"
@EXIT_SUCCESS = global i32 0
define void @dump_stack() {
%1 = alloca [30 x i8*]
%2 = bitcast [30 x i8*]* %1 to i8*
%3 = getelementptr inbounds [30 x i8*], [30 x i8*]* %1, i64 0, i64 0
%4 = call i64 @backtrace(i8** %3, i32 30)
%5 = call i8** @backtrace_symbols(i8** %3, i64 %4)
%6 = icmp eq i8** %5, null
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i64 %4)
%13 = icmp eq i64 %4, 0
br i1 %13, label %21, label %14
14: ; preds = %11, %14
%15 = phi i64 [ %19, %14 ], [ 0, %11 ]
%16 = getelementptr inbounds i8*, i8** %5, i64 %15
%17 = load i8*, i8** %16
%18 = call i32 @puts(i8* dereferenceable(1) %17)
%19 = add nuw i64 %15, 1
%20 = icmp eq i64 %19, %4
br i1 %20, label %21, label %14
21: ; preds = %14, %11
%22 = call i32 @free(i8** %5)
%23 = load i32, i32* @EXIT_SUCCESS
%24 = call i32 @exit(i32 %23)
unreachable
}