#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 /*<<< orphan*/ first_local;

/* Variables and functions */
unsigned long junk(int *);
void getStackTrace(unsigned long *results, int max) {
  int first_local;
  unsigned long fp, ret_addr;
  first_local = 0;
  fp = junk(&first_local);
  fp = ((unsigned)&(first_local) + 4);
  ret_addr = *((unsigned *)(fp + 4));
  while (ret_addr > 0x80482fc && max-- > 1) {
    *results++ = ret_addr;
    if (fp < (unsigned long)&first_local)
      break;
    fp = *((unsigned *)fp);
    if (!fp)
      break;
    if (fp < (unsigned long)&first_local)
      break;
    ret_addr = *((unsigned *)(fp + 4));
  }
  *results++ = 0;
}


x86 assembly (clang, -O0):
<getStackTrace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
lea    -0x10(%rbp),%rdi
call   1030 <junk@plt>
mov    %rax,-0x18(%rbp)
lea    -0x10(%rbp),%rax
add    $0x4,%eax
mov    %eax,%eax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%eax
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpq   $0x80482fc,-0x20(%rbp)
08
mov    %al,-0x21(%rbp)
jbe    1172 <getStackTrace+0x62>
mov    -0xc(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xc(%rbp)
cmp    $0x1,%eax
setg   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1182 <getStackTrace+0x72>
jmp    11ee <getStackTrace+0xde>
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x8,%rdx
mov    %rdx,-0x8(%rbp)
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
lea    -0x10(%rbp),%rcx
cmp    %rcx,%rax
jae    11ae <getStackTrace+0x9e>
jmp    11ee <getStackTrace+0xde>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c8 <getStackTrace+0xb8>
jmp    11ee <getStackTrace+0xde>
mov    -0x18(%rbp),%rax
lea    -0x10(%rbp),%rcx
cmp    %rcx,%rax
jae    11de <getStackTrace+0xce>
jmp    11ee <getStackTrace+0xde>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%eax
mov    %rax,-0x20(%rbp)
jmp    114b <getStackTrace+0x3b>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0x8(%rbp)
movq   $0x0,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getStackTrace>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
movl   $0x0,0xc(%rsp)
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1030 <junk@plt>
lea    0x4(%r14),%eax
cs nopw 0x0(%rax,%rax,1)
mov    0x4(%rax),%ecx
cmp    $0x80482fd,%rcx
jb     116c <getStackTrace+0x5c>
cmp    $0x2,%ebp
jl     116c <getStackTrace+0x5c>
mov    %rcx,(%rbx)
add    $0x8,%rbx
cmp    %r14,%rax
jb     116c <getStackTrace+0x5c>
mov    (%rax),%eax
test   %rax,%rax
je     116c <getStackTrace+0x5c>
add    $0xffffffff,%ebp
cmp    %r14,%rax
jae    1140 <getStackTrace+0x30>
movq   $0x0,(%rbx)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define void @getStackTrace(i64* %0, i32 %1) {
%3 = alloca i64*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
store i64* %0, i64** %3
store i32 %1, i32* %4
store i32 0, i32* %5
%8 = call i64 @junk(i32* %5)
store i64 %8, i64* %6
%9 = ptrtoint i32* %5 to i32
%10 = add i32 %9, 4
%11 = zext i32 %10 to i64
store i64 %11, i64* %6
%12 = load i64, i64* %6
%13 = add i64 %12, 4
%14 = inttoptr i64 %13 to i32*
%15 = load i32, i32* %14
%16 = zext i32 %15 to i64
store i64 %16, i64* %7
br label %17
17: ; preds = %47, %2
%18 = load i64, i64* %7
%19 = icmp ugt i64 %18, 134513404
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load i32, i32* %4
%22 = add nsw i32 %21, -1
store i32 %22, i32* %4
%23 = icmp sgt i32 %21, 1
br label %24
24: ; preds = %20, %17
%25 = phi i1 [ false, %17 ], [ %23, %20 ]
br i1 %25, label %26, label %53
26: ; preds = %24
%27 = load i64, i64* %7
%28 = load i64*, i64** %3
%29 = getelementptr inbounds i64, i64* %28, i32 1
store i64* %29, i64** %3
store i64 %27, i64* %28
%30 = load i64, i64* %6
%31 = ptrtoint i32* %5 to i64
%32 = icmp ult i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %26
br label %53
34: ; preds = %26
%35 = load i64, i64* %6
%36 = inttoptr i64 %35 to i32*
%37 = load i32, i32* %36
%38 = zext i32 %37 to i64
store i64 %38, i64* %6
%39 = load i64, i64* %6
%40 = icmp ne i64 %39, 0
br i1 %40, label %42, label %41
41: ; preds = %34
br label %53
42: ; preds = %34
%43 = load i64, i64* %6
%44 = ptrtoint i32* %5 to i64
%45 = icmp ult i64 %43, %44
br i1 %45, label %46, label %47
46: ; preds = %42
br label %53
47: ; preds = %42
%48 = load i64, i64* %6
%49 = add i64 %48, 4
%50 = inttoptr i64 %49 to i32*
%51 = load i32, i32* %50
%52 = zext i32 %51 to i64
store i64 %52, i64* %7
br label %17
53: ; preds = %46, %41, %33, %24
%54 = load i64*, i64** %3
%55 = getelementptr inbounds i64, i64* %54, i32 1
store i64* %55, i64** %3
store i64 0, i64* %54
ret void
}

LLVM IR (-O2):
define void @getStackTrace(i64* nocapture writeonly %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%5 = call i64 @junk(i32* %3)
%6 = ptrtoint i32* %3 to i64
%7 = add i64 %6, 4
%8 = and i64 %7, 4294967292
br label %9
9: ; preds = %24, %2
%10 = phi i64* [ %0, %2 ], [ %22, %24 ]
%11 = phi i32 [ %1, %2 ], [ %19, %24 ]
%12 = phi i64 [ %8, %2 ], [ %27, %24 ]
%13 = add nuw nsw i64 %12, 4
%14 = inttoptr i64 %13 to i32*
%15 = load i32, i32* %14
%16 = zext i32 %15 to i64
%17 = icmp ugt i32 %15, 134513404
br i1 %17, label %18, label %31
18: ; preds = %9
%19 = add nsw i32 %11, -1
%20 = icmp sgt i32 %11, 1
br i1 %20, label %21, label %31
21: ; preds = %18
%22 = getelementptr inbounds i64, i64* %10, i64 1
store i64 %16, i64* %10
%23 = icmp ult i64 %12, %6
br i1 %23, label %31, label %24
24: ; preds = %21
%25 = inttoptr i64 %12 to i32*
%26 = load i32, i32* %25
%27 = zext i32 %26 to i64
%28 = icmp eq i32 %26, 0
%29 = icmp ult i64 %27, %6
%30 = or i1 %28, %29
br i1 %30, label %31, label %9
31: ; preds = %9, %24, %21, %18
%32 = phi i64* [ %22, %21 ], [ %22, %24 ], [ %10, %18 ], [ %10, %9 ]
store i64 0, i64* %32
ret void
}