#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 stop;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char **backtrace_symbols(void *const *, int const);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
void print_stack_trace(void *const *a, const int size, FILE *out) {
  int i;
  int stop = size;
  char **strings = 0;
  strings = backtrace_symbols(a, size);
  for (i = 0; i < size; i++) {
    void *addr = a[i];
    if (addr == 0) {
      stop = i;
      break;
    }
  }
  for (i = stop - 1; i >= 0; i--) {
    void *addr = a[i];
    fprintf(out, "#%-3d [0x%lx]: ", (stop)-i, (unsigned long)addr);
    if (strings)
      fprintf(out, "%s", strings[i]);
    fprintf(out, "\n");
  }
}


x86 assembly (clang, -O0):
<print_stack_trace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <backtrace_symbols@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    119d <print_stack_trace+0x7d>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    118a <print_stack_trace+0x6a>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    119d <print_stack_trace+0x7d>
jmp    118f <print_stack_trace+0x6f>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1158 <print_stack_trace+0x38>
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     1225 <print_stack_trace+0x105>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%edx
sub    -0x1c(%rbp),%edx
mov    -0x38(%rbp),%rcx
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
cmpq   $0x0,-0x28(%rbp)
je     1205 <print_stack_trace+0xe5>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%rdi
lea    0xe03(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a6 <print_stack_trace+0x86>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_stack_trace>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r13
mov    %esi,%ebp
mov    %rdi,%r14
call   1030 <backtrace_symbols@plt>
test   %ebp,%ebp
jle    11e8 <print_stack_trace+0xa8>
mov    %rax,%r15
mov    %ebp,%ecx
xor    %eax,%eax
nopw   0x0(%rax,%rax,1)
cmpq   $0x0,(%r14,%rax,8)
je     1182 <print_stack_trace+0x42>
add    $0x1,%rax
cmp    %rax,%rcx
jne    1170 <print_stack_trace+0x30>
mov    %ecx,%eax
test   %eax,%eax
jle    11e8 <print_stack_trace+0xa8>
mov    %eax,%ebx
add    $0x1,%rbx
mov    $0x1,%ebp
jmp    11ba <print_stack_trace+0x7a>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0xa,%edi
mov    %r13,%rsi
call   1050 <fputc@plt>
add    $0xffffffffffffffff,%rbx
add    $0x1,%ebp
cmp    $0x1,%rbx
jbe    11e8 <print_stack_trace+0xa8>
lea    -0x2(%rbx),%r12d
mov    (%r14,%r12,8),%rcx
mov    %r13,%rdi
lea    0xe34(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
test   %r15,%r15
je     11a0 <print_stack_trace+0x60>
mov    (%r15,%r12,8),%rdi
mov    %r13,%rsi
call   1040 <fputs@plt>
jmp    11a0 <print_stack_trace+0x60>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"#%-3d [0x%lx]: \00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [2 x i8] c"\0A\00"
define void @print_stack_trace(i8** %0, i32 %1, i32* %2) {
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8**
%10 = alloca i8*
%11 = alloca i8*
store i8** %0, i8*** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
%12 = load i32, i32* %5
store i32 %12, i32* %8
store i8** null, i8*** %9
%13 = load i8**, i8*** %4
%14 = load i32, i32* %5
%15 = call i8** @backtrace_symbols(i8** %13, i32 %14)
store i8** %15, i8*** %9
store i32 0, i32* %7
br label %16
16: ; preds = %31, %3
%17 = load i32, i32* %7
%18 = load i32, i32* %5
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %34
20: ; preds = %16
%21 = load i8**, i8*** %4
%22 = load i32, i32* %7
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8*, i8** %21, i64 %23
%25 = load i8*, i8** %24
store i8* %25, i8** %10
%26 = load i8*, i8** %10
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %30
28: ; preds = %20
%29 = load i32, i32* %7
store i32 %29, i32* %8
br label %34
30: ; preds = %20
br label %31
31: ; preds = %30
%32 = load i32, i32* %7
%33 = add nsw i32 %32, 1
store i32 %33, i32* %7
br label %16
34: ; preds = %28, %16
%35 = load i32, i32* %8
%36 = sub nsw i32 %35, 1
store i32 %36, i32* %7
br label %37
37: ; preds = %66, %34
%38 = load i32, i32* %7
%39 = icmp sge i32 %38, 0
br i1 %39, label %40, label %69
40: ; preds = %37
%41 = load i8**, i8*** %4
%42 = load i32, i32* %7
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8*, i8** %41, i64 %43
%45 = load i8*, i8** %44
store i8* %45, i8** %11
%46 = load i32*, i32** %6
%47 = load i32, i32* %8
%48 = load i32, i32* %7
%49 = sub nsw i32 %47, %48
%50 = load i8*, i8** %11
%51 = ptrtoint i8* %50 to i64
%52 = call i32 (i32*, i8*, ...) @fprintf(i32* %46, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %49, i64 %51)
%53 = load i8**, i8*** %9
%54 = icmp ne i8** %53, null
br i1 %54, label %55, label %63
55: ; preds = %40
%56 = load i32*, i32** %6
%57 = load i8**, i8*** %9
%58 = load i32, i32* %7
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8*, i8** %57, i64 %59
%61 = load i8*, i8** %60
%62 = call i32 (i32*, i8*, ...) @fprintf(i32* %56, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %61)
br label %63
63: ; preds = %55, %40
%64 = load i32*, i32** %6
%65 = call i32 (i32*, i8*, ...) @fprintf(i32* %64, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %66
66: ; preds = %63
%67 = load i32, i32* %7
%68 = add nsw i32 %67, -1
store i32 %68, i32* %7
br label %37
69: ; preds = %37
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"#%-3d [0x%lx]: \00"
define void @print_stack_trace(i8** %0, i32 %1, i32* nocapture %2) {
%4 = tail call i8** @backtrace_symbols(i8** %0, i32 %1)
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %41
6: ; preds = %3
%7 = zext i32 %1 to i64
br label %8
8: ; preds = %6, %13
%9 = phi i64 [ 0, %6 ], [ %14, %13 ]
%10 = getelementptr inbounds i8*, i8** %0, i64 %9
%11 = load i8*, i8** %10
%12 = icmp eq i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %8
%14 = add nuw nsw i64 %9, 1
%15 = icmp eq i64 %14, %7
br i1 %15, label %18, label %8
16: ; preds = %8
%17 = trunc i64 %9 to i32
br label %18
18: ; preds = %13, %16
%19 = phi i32 [ %17, %16 ], [ %1, %13 ]
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %41
21: ; preds = %18
%22 = icmp eq i8** %4, null
%23 = zext i32 %19 to i64
br label %24
24: ; preds = %21, %38
%25 = phi i64 [ %23, %21 ], [ %26, %38 ]
%26 = add nsw i64 %25, -1
%27 = trunc i64 %26 to i32
%28 = and i64 %26, 4294967295
%29 = getelementptr inbounds i8*, i8** %0, i64 %28
%30 = load i8*, i8** %29
%31 = sub nsw i32 %19, %27
%32 = ptrtoint i8* %30 to i64
%33 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %2, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %31, i64 %32)
br i1 %22, label %38, label %34
34: ; preds = %24
%35 = getelementptr inbounds i8*, i8** %4, i64 %28
%36 = load i8*, i8** %35
%37 = tail call i32 @fputs(i8* %36, i32* %2)
br label %38
38: ; preds = %34, %24
%39 = tail call i32 @fputc(i32 10, i32* %2)
%40 = icmp ugt i64 %25, 1
br i1 %40, label %24, label %41
41: ; preds = %38, %3, %18
ret void
}