#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 **, size_t const);
char **backtrace_symbols(void **, size_t);
int /*<<< orphan*/ free(char **);
int /*<<< orphan*/ printf(char *, ...);
void dump_backtrace(void) {
  const size_t len = 30;
  void *array[len];
  size_t size;
  char **strings;
  size_t i;
  size = backtrace(array, len);
  strings = backtrace_symbols(array, size);
  printf("Obtained %zd stack frames.\n", size);
  for (i = 1; i < size; i++)
    printf("%s\n", strings[i]);
  free(strings);
}


x86 assembly (clang, -O0):
<dump_backtrace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movq   $0x1e,-0x8(%rbp)
mov    %rsp,%rax
mov    %rax,-0x10(%rbp)
lea    -0x120(%rbp),%rdi
mov    $0x1e,%esi
call   1050 <backtrace@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    -0x120(%rbp),%rdi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movq   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11d6 <dump_backtrace+0x96>
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    119d <dump_backtrace+0x5d>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_backtrace>:
push   %r15
push   %r14
push   %rbx
sub    $0xf0,%rsp
mov    %rsp,%r14
mov    $0x1e,%esi
mov    %r14,%rdi
call   1060 <backtrace@plt>
mov    %rax,%r15
mov    %r14,%rdi
mov    %rax,%rsi
call   1050 <backtrace_symbols@plt>
mov    %rax,%r14
lea    0xe7c(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
cmp    $0x2,%r15
jb     11b2 <dump_backtrace+0x62>
mov    $0x1,%ebx
nopl   0x0(%rax)
mov    (%r14,%rbx,8),%rdi
call   1040 <puts@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    11a0 <dump_backtrace+0x50>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0xf0,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [28 x i8] c"Obtained %zd stack frames.\0A\00"
define void @dump_backtrace() {
%1 = alloca [30 x i8*]
%2 = getelementptr inbounds [30 x i8*], [30 x i8*]* %1, i64 0, i64 0
%3 = call i64 @backtrace(i8** %2, i64 30)
%4 = call i8** @backtrace_symbols(i8** %2, i64 %3)
%5 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i64 %3)
%6 = icmp ugt i64 %3, 1
br i1 %6, label %7, label %14
7: ; preds = %0, %7
%8 = phi i64 [ %12, %7 ], [ 1, %0 ]
%9 = getelementptr inbounds i8*, i8** %4, i64 %8
%10 = load i8*, i8** %9
%11 = call i32 @puts(i8* dereferenceable(1) %10)
%12 = add nuw i64 %8, 1
%13 = icmp eq i64 %12, %3
br i1 %13, label %14, label %7
14: ; preds = %7, %0
%15 = call i32 @free(i8** %4)
ret void
}