getStackTrace(results, max) unsigned long *results;
int max;
{
  unsigned long *sp, *getStackPointer(), *getFramePointer();
  unsigned long *ra, mainCall;
  extern int main();
  flushWindows(32);
  sp = getFramePointer();
  while (max) {
    ra = (unsigned long *)sp[15];
    sp = (unsigned long *)sp[14];
    mainCall =
        ((((unsigned long)main) - ((unsigned long)ra)) >> 2) | 0x40000000;
    if (ra[0] == mainCall) {
      *results++ = 0;
      break;
    }
    *results++ = (unsigned long)ra;
    max--;
  }
}


x86 assembly (clang, -O0):
<getStackTrace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0x20,%edi
mov    $0x0,%al
call   1040 <flushWindows@plt>
mov    $0x0,%al
call   1030 <getFramePointer@plt>
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11d4 <getStackTrace+0xb4>
mov    -0x20(%rbp),%rax
mov    0x78(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    0x70(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rcx
mov    0x2e75(%rip),%rax
sub    %rcx,%rax
shr    $0x2,%rax
or     $0x40000000,%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
cmp    -0x30(%rbp),%rax
jne    11b0 <getStackTrace+0x90>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x10(%rbp)
movq   $0x0,(%rax)
jmp    11d4 <getStackTrace+0xb4>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x8,%rdx
mov    %rdx,-0x10(%rbp)
mov    %rcx,(%rax)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1146 <getStackTrace+0x26>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getStackTrace>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    $0x20,%edi
xor    %eax,%eax
call   1040 <flushWindows@plt>
xor    %eax,%eax
call   1030 <getFramePointer@plt>
test   %ebp,%ebp
je     1184 <getStackTrace+0x64>
mov    %ebp,%ecx
xor    %edx,%edx
mov    0x2e9e(%rip),%rbp
nopw   0x0(%rax,%rax,1)
mov    0x78(%rax),%rdi
mov    %rbp,%rsi
sub    %rdi,%rsi
shr    $0x2,%rsi
or     $0x40000000,%rsi
cmp    %rsi,(%rdi)
je     117c <getStackTrace+0x5c>
mov    0x70(%rax),%rax
mov    %rdi,(%rbx,%rdx,8)
add    $0x1,%rdx
cmp    %edx,%ecx
jne    1150 <getStackTrace+0x30>
jmp    1184 <getStackTrace+0x64>
movq   $0x0,(%rbx,%rdx,8)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @getStackTrace(i64* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i64*
%5 = alloca i32
%6 = alloca i64*
%7 = alloca i64*
%8 = alloca i64
store i64* %0, i64** %4
store i32 %1, i32* %5
%9 = call i32 (i32, ...) bitcast (i32 (...)* @flushWindows to i32 (i32, ...)*)(i32 32)
%10 = call i64* (...) @getFramePointer()
store i64* %10, i64** %6
br label %11
11: ; preds = %36, %2
%12 = load i32, i32* %5
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %43
14: ; preds = %11
%15 = load i64*, i64** %6
%16 = getelementptr inbounds i64, i64* %15, i64 15
%17 = load i64, i64* %16
%18 = inttoptr i64 %17 to i64*
store i64* %18, i64** %7
%19 = load i64*, i64** %6
%20 = getelementptr inbounds i64, i64* %19, i64 14
%21 = load i64, i64* %20
%22 = inttoptr i64 %21 to i64*
store i64* %22, i64** %6
%23 = load i64*, i64** %7
%24 = ptrtoint i64* %23 to i64
%25 = sub i64 ptrtoint (i32 (...)* @main to i64), %24
%26 = lshr i64 %25, 2
%27 = or i64 %26, 1073741824
store i64 %27, i64* %8
%28 = load i64*, i64** %7
%29 = getelementptr inbounds i64, i64* %28, i64 0
%30 = load i64, i64* %29
%31 = load i64, i64* %8
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %36
33: ; preds = %14
%34 = load i64*, i64** %4
%35 = getelementptr inbounds i64, i64* %34, i32 1
store i64* %35, i64** %4
store i64 0, i64* %34
br label %43
36: ; preds = %14
%37 = load i64*, i64** %7
%38 = ptrtoint i64* %37 to i64
%39 = load i64*, i64** %4
%40 = getelementptr inbounds i64, i64* %39, i32 1
store i64* %40, i64** %4
store i64 %38, i64* %39
%41 = load i32, i32* %5
%42 = add nsw i32 %41, -1
store i32 %42, i32* %5
br label %11
43: ; preds = %33, %11
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
define i32 @getStackTrace(i64* nocapture writeonly %0, i32 %1) {
%3 = tail call i32 (i32, ...) bitcast (i32 (...)* @flushWindows to i32 (i32, ...)*)(i32 32)
%4 = tail call i64* (...) @getFramePointer()
%5 = icmp eq i32 %1, 0
br i1 %5, label %26, label %6
6: ; preds = %2, %19
%7 = phi i64* [ %23, %19 ], [ %0, %2 ]
%8 = phi i32 [ %24, %19 ], [ %1, %2 ]
%9 = phi i64* [ %22, %19 ], [ %4, %2 ]
%10 = getelementptr inbounds i64, i64* %9, i64 15
%11 = load i64, i64* %10
%12 = inttoptr i64 %11 to i64*
%13 = sub i64 ptrtoint (i32 (...)* @main to i64), %11
%14 = lshr i64 %13, 2
%15 = or i64 %14, 1073741824
%16 = load i64, i64* %12
%17 = icmp eq i64 %16, %15
br i1 %17, label %18, label %19
18: ; preds = %6
store i64 0, i64* %7
br label %26
19: ; preds = %6
%20 = getelementptr inbounds i64, i64* %9, i64 14
%21 = load i64, i64* %20
%22 = inttoptr i64 %21 to i64*
%23 = getelementptr inbounds i64, i64* %7, i64 1
store i64 %11, i64* %7
%24 = add nsw i32 %8, -1
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %6
26: ; preds = %19, %2, %18
ret i32 undef
}