void Debug_PrintCallstack(unsigned int skipFrames) {
  int lr = 0;
  int depth = 0;
  int *fp = get_fp();
  skipFrames -= 1;
  do {
    lr = *fp;
    fp = (int *)*(fp - 1);
    if (depth > skipFrames)
      printf("Frame %d: %s (0x%h)\n", depth - 2,
             Debug_GetClosestPreviousFunction(lr), lr);
    if (fp < 0x80000000)
      fp += 0x80000000;
    else if (fp > 0x80000000 + 0x10000000)
      break;
  } while (fp != 0 && depth++ < 10 && lr != 0x80CC);
}


x86 assembly (clang, -O0):
<Debug_PrintCallstack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x0,%al
call   1050 <get_fp@plt>
cltq
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x4(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
jbe    11a9 <Debug_PrintCallstack+0x79>
mov    -0xc(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x0,%al
call   1030 <Debug_GetClosestPreviousFunction@plt>
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
mov    -0x8(%rbp),%ecx
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x80000000,%eax
cmp    %rax,-0x18(%rbp)
jae    11cf <Debug_PrintCallstack+0x9f>
movabs $0x200000000,%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    11e8 <Debug_PrintCallstack+0xb8>
mov    $0x90000000,%eax
cmp    %rax,-0x18(%rbp)
jbe    11e3 <Debug_PrintCallstack+0xb3>
jmp    122e <Debug_PrintCallstack+0xfe>
jmp    11e8 <Debug_PrintCallstack+0xb8>
jmp    11ed <Debug_PrintCallstack+0xbd>
xor    %eax,%eax
cmpq   $0x0,-0x18(%rbp)
mov    %al,-0x1d(%rbp)
je     1223 <Debug_PrintCallstack+0xf3>
mov    -0xc(%rbp),%ecx
mov    %ecx,%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x1d(%rbp)
jge    1223 <Debug_PrintCallstack+0xf3>
cmpl   $0x80cc,-0x8(%rbp)
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    115f <Debug_PrintCallstack+0x2f>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Debug_PrintCallstack>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r15d
mov    $0x80000000,%r14d
xor    %eax,%eax
call   1050 <get_fp@plt>
movslq %eax,%rbp
add    $0xffffffff,%r15d
mov    $0xfffffffe,%ebx
add    $0x10000000,%r14
xchg   %ax,%ax
lea    0x2(%rbx),%r12d
mov    0x0(%rbp),%r13d
movslq -0x4(%rbp),%rbp
cmp    %r15d,%r12d
jbe    1190 <Debug_PrintCallstack+0x60>
mov    %r13d,%edi
xor    %eax,%eax
call   1030 <Debug_GetClosestPreviousFunction@plt>
lea    0xe7e(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
mov    %r13d,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x80000000,%eax
cmp    %rax,%rbp
jae    11b0 <Debug_PrintCallstack+0x80>
movabs $0x200000000,%rax
add    %rax,%rbp
jmp    11b9 <Debug_PrintCallstack+0x89>
nopl   0x0(%rax)
cmp    %r14,%rbp
ja     11cb <Debug_PrintCallstack+0x9b>
test   %ebp,%ebp
je     11cb <Debug_PrintCallstack+0x9b>
cmp    $0x9,%r12d
ja     11cb <Debug_PrintCallstack+0x9b>
add    $0x1,%ebx
cmp    $0x80cc,%r13d
jne    1160 <Debug_PrintCallstack+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"Frame %d: %s (0x%h)\0A\00"
define void @Debug_PrintCallstack(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
store i32 %0, i32* %2
store i32 0, i32* %3
store i32 0, i32* %4
%6 = call i32 (...) @get_fp()
%7 = sext i32 %6 to i64
%8 = inttoptr i64 %7 to i32*
store i32* %8, i32** %5
%9 = load i32, i32* %2
%10 = sub i32 %9, 1
store i32 %10, i32* %2
br label %11
11: ; preds = %51, %1
%12 = load i32*, i32** %5
%13 = load i32, i32* %12
store i32 %13, i32* %3
%14 = load i32*, i32** %5
%15 = getelementptr inbounds i32, i32* %14, i64 -1
%16 = load i32, i32* %15
%17 = sext i32 %16 to i64
%18 = inttoptr i64 %17 to i32*
store i32* %18, i32** %5
%19 = load i32, i32* %4
%20 = load i32, i32* %2
%21 = icmp ugt i32 %19, %20
br i1 %21, label %22, label %29
22: ; preds = %11
%23 = load i32, i32* %4
%24 = sub nsw i32 %23, 2
%25 = load i32, i32* %3
%26 = call i32 (i32, ...) bitcast (i32 (...)* @Debug_GetClosestPreviousFunction to i32 (i32, ...)*)(i32 %25)
%27 = load i32, i32* %3
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %26, i32 %27)
br label %29
29: ; preds = %22, %11
%30 = load i32*, i32** %5
%31 = icmp ult i32* %30, inttoptr (i64 2147483648 to i32*)
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32*, i32** %5
%34 = getelementptr inbounds i32, i32* %33, i64 2147483648
store i32* %34, i32** %5
br label %40
35: ; preds = %29
%36 = load i32*, i32** %5
%37 = icmp ugt i32* %36, inttoptr (i64 2415919104 to i32*)
br i1 %37, label %38, label %39
38: ; preds = %35
br label %53
39: ; preds = %35
br label %40
40: ; preds = %39, %32
br label %41
41: ; preds = %40
%42 = load i32*, i32** %5
%43 = icmp ne i32* %42, null
br i1 %43, label %44, label %51
44: ; preds = %41
%45 = load i32, i32* %4
%46 = add nsw i32 %45, 1
store i32 %46, i32* %4
%47 = icmp slt i32 %45, 10
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load i32, i32* %3
%50 = icmp ne i32 %49, 32972
br label %51
51: ; preds = %48, %44, %41
%52 = phi i1 [ false, %44 ], [ false, %41 ], [ %50, %48 ]
br i1 %52, label %11, label %53
53: ; preds = %51, %38
ret void
}

LLVM IR (-O2):
@.str = [21 x i8] c"Frame %d: %s (0x%h)\0A\00"
define void @Debug_PrintCallstack(i32 %0) {
%2 = tail call i32 (...) @get_fp()
%3 = sext i32 %2 to i64
%4 = inttoptr i64 %3 to i32*
%5 = add i32 %0, -1
br label %6
6: ; preds = %27, %1
%7 = phi i32 [ 0, %1 ], [ %29, %27 ]
%8 = phi i32* [ %4, %1 ], [ %28, %27 ]
%9 = load i32, i32* %8
%10 = getelementptr inbounds i32, i32* %8, i64 -1
%11 = load i32, i32* %10
%12 = sext i32 %11 to i64
%13 = inttoptr i64 %12 to i32*
%14 = icmp ugt i32 %7, %5
br i1 %14, label %15, label %19
15: ; preds = %6
%16 = add nsw i32 %7, -2
%17 = tail call i32 (i32, ...) bitcast (i32 (...)* @Debug_GetClosestPreviousFunction to i32 (i32, ...)*)(i32 %9)
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %17, i32 %9)
br label %19
19: ; preds = %15, %6
%20 = icmp ult i32* %13, inttoptr (i64 2147483648 to i32*)
br i1 %20, label %21, label %23
21: ; preds = %19
%22 = getelementptr inbounds i32, i32* %13, i64 2147483648
br label %27
23: ; preds = %19
%24 = icmp ule i32* %13, inttoptr (i64 2415919104 to i32*)
%25 = icmp ne i32 %11, 0
%26 = select i1 %24, i1 %25, i1 false
br i1 %26, label %27, label %33
27: ; preds = %21, %23
%28 = phi i32* [ %22, %21 ], [ %13, %23 ]
%29 = add nuw nsw i32 %7, 1
%30 = icmp ult i32 %7, 10
%31 = icmp ne i32 %9, 32972
%32 = select i1 %30, i1 %31, i1 false
br i1 %32, label %6, label %33
33: ; preds = %27, %23
ret void
}