#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 */
int backtrace(void **, int);
char **backtrace_symbols(void **, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, char *);
int /*<<< orphan*/ free(char **);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
void hkl_printbt(void) {
  void *array[20];
  int size;
  char **strings;
  int i;
  size = backtrace(array, 20);
  strings = backtrace_symbols(array, size);
  printf("Got a backtrace:\n");
  for (i = 0; i < size; ++i)
    fprintf(stderr, "#%i %s\n", i, strings[i]);
  free(strings);
}


x86 assembly (clang, -O0):
<hkl_printbt>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
lea    -0xa0(%rbp),%rdi
mov    $0x14,%esi
call   1050 <backtrace@plt>
mov    %eax,-0xa4(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0xa4(%rbp),%esi
call   1040 <backtrace_symbols@plt>
mov    %rax,-0xb0(%rbp)
lea    0xe6e(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0xb4(%rbp)
mov    -0xb4(%rbp),%eax
cmp    -0xa4(%rbp),%eax
jge    11f4 <hkl_printbt+0xa4>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    -0xb4(%rbp),%edx
mov    -0xb0(%rbp),%rax
movslq -0xb4(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
lea    0xe37(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0xb4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xb4(%rbp)
jmp    11a1 <hkl_printbt+0x51>
mov    -0xb0(%rbp),%rdi
call   1030 <free@plt>
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hkl_printbt>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa0,%rsp
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x14,%esi
call   1060 <backtrace@plt>
mov    %eax,%ebx
mov    %r14,%rdi
mov    %eax,%esi
call   1050 <backtrace_symbols@plt>
mov    %rax,%r14
lea    0xe82(%rip),%rdi
call   1040 <puts@plt>
test   %ebx,%ebx
jle    11cb <hkl_printbt+0x7b>
mov    %ebx,%r12d
xor    %ebx,%ebx
mov    0x2e45(%rip),%r13
lea    0xe5e(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x0(%r13),%edi
mov    (%r14,%rbx,8),%rcx
mov    %r15,%rsi
mov    %ebx,%edx
call   1070 <fprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11b0 <hkl_printbt+0x60>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0xa0,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [18 x i8] c"Got a backtrace:\0A\00"
@stderr = global i32 0
@.str.1 = [8 x i8] c"#%i %s\0A\00"
define void @hkl_printbt() {
%1 = alloca [20 x i8*]
%2 = alloca i32
%3 = alloca i8**
%4 = alloca i32
%5 = getelementptr inbounds [20 x i8*], [20 x i8*]* %1, i64 0, i64 0
%6 = call i32 @backtrace(i8** %5, i32 20)
store i32 %6, i32* %2
%7 = getelementptr inbounds [20 x i8*], [20 x i8*]* %1, i64 0, i64 0
%8 = load i32, i32* %2
%9 = call i8** @backtrace_symbols(i8** %7, i32 %8)
store i8** %9, i8*** %3
%10 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %4
br label %11
11: ; preds = %24, %0
%12 = load i32, i32* %4
%13 = load i32, i32* %2
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %27
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = load i32, i32* %4
%18 = load i8**, i8*** %3
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8*, i8** %18, i64 %20
%22 = load i8*, i8** %21
%23 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %17, i8* %22)
br label %24
24: ; preds = %15
%25 = load i32, i32* %4
%26 = add nsw i32 %25, 1
store i32 %26, i32* %4
br label %11
27: ; preds = %11
%28 = load i8**, i8*** %3
%29 = call i32 @free(i8** %28)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str.1 = [8 x i8] c"#%i %s\0A\00"
@str = [17 x i8] c"Got a backtrace:\00"
define void @hkl_printbt() {
%1 = alloca [20 x i8*]
%2 = bitcast [20 x i8*]* %1 to i8*
%3 = getelementptr inbounds [20 x i8*], [20 x i8*]* %1, i64 0, i64 0
%4 = call i32 @backtrace(i8** %3, i32 20)
%5 = call i8** @backtrace_symbols(i8** %3, i32 %4)
%6 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%7 = icmp sgt i32 %4, 0
br i1 %7, label %8, label %19
8: ; preds = %0
%9 = zext i32 %4 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %17, %10 ]
%12 = load i32, i32* @stderr
%13 = getelementptr inbounds i8*, i8** %5, i64 %11
%14 = load i8*, i8** %13
%15 = trunc i64 %11 to i32
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %15, i8* %14)
%17 = add nuw nsw i64 %11, 1
%18 = icmp eq i64 %17, %9
br i1 %18, label %19, label %10
19: ; preds = %10, %0
%20 = call i32 @free(i8** %5)
ret void
}