#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*/ val;
typedef int uint64_t;
typedef int /*<<< orphan*/ sp;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ xg_read_mem(int, char *, int, int /*<<< orphan*/);
void prnt_call_trace64(uint64_t ip, uint64_t sp) {
  int stack_max = 10;
  uint64_t loopmax = 0, val;
  printf("Call Trace:\n");
  printf("   ["
         "%016lx"
         "]\n",
         ip);
  while (stack_max > 0) {
    if (xg_read_mem(sp, (char *)&val, sizeof(val), 0) != 0)
      return;
    if (val > 0xffffffff80000000UL) {
      printf("   ["
             "%016lx"
             "]\n",
             val);
      --stack_max;
    }
    sp += sizeof(sp);
    if (++loopmax > 10000)
      break;
  }
}


x86 assembly (clang, -O0):
<prnt_call_trace64>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0xa,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
lea    0xebd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%esi
lea    0xeb9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0xc(%rbp)
jle    11da <prnt_call_trace64+0xba>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1040 <xg_read_mem@plt>
cmp    $0x0,%rax
je     1187 <prnt_call_trace64+0x67>
jmp    11da <prnt_call_trace64+0xba>
movslq -0x14(%rbp),%rax
cmp    $0xffffffff80000000,%rax
jbe    11b1 <prnt_call_trace64+0x91>
mov    -0x14(%rbp),%esi
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
movslq -0x8(%rbp),%rax
add    $0x4,%rax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
cmp    $0x2710,%eax
jle    11d5 <prnt_call_trace64+0xb5>
jmp    11da <prnt_call_trace64+0xba>
jmp    115b <prnt_call_trace64+0x3b>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prnt_call_trace64>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
lea    0xec6(%rip),%rdi
call   1030 <puts@plt>
lea    0xead(%rip),%r14
mov    %r14,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%r12d
lea    0xc(%rsp),%r15
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
call   1050 <xg_read_mem@plt>
test   %rax,%rax
jne    11b3 <prnt_call_trace64+0x83>
mov    0xc(%rsp),%esi
cmp    $0x80000001,%esi
jb     11a0 <prnt_call_trace64+0x70>
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0xffffffff,%r12d
cmp    $0x270f,%ebp
ja     11b3 <prnt_call_trace64+0x83>
add    $0x4,%ebx
add    $0x1,%ebp
test   %r12d,%r12d
jg     1170 <prnt_call_trace64+0x40>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"Call Trace:\0A\00"
@.str.1 = [13 x i8] c" [%016lx]\0A\00"
define void @prnt_call_trace64(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 10, i32* %5
store i32 0, i32* %6
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* %3
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %9)
br label %11
11: ; preds = %38, %2
%12 = load i32, i32* %5
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %39
14: ; preds = %11
%15 = load i32, i32* %4
%16 = bitcast i32* %7 to i8*
%17 = call i64 @xg_read_mem(i32 %15, i8* %16, i32 4, i32 0)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %14
br label %39
20: ; preds = %14
%21 = load i32, i32* %7
%22 = sext i32 %21 to i64
%23 = icmp ugt i64 %22, -2147483648
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i32, i32* %7
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %5
%28 = add nsw i32 %27, -1
store i32 %28, i32* %5
br label %29
29: ; preds = %24, %20
%30 = load i32, i32* %4
%31 = sext i32 %30 to i64
%32 = add i64 %31, 4
%33 = trunc i64 %32 to i32
store i32 %33, i32* %4
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
store i32 %35, i32* %6
%36 = icmp sgt i32 %35, 10000
br i1 %36, label %37, label %38
37: ; preds = %29
br label %39
38: ; preds = %29
br label %11
39: ; preds = %19, %37, %11
ret void
}

LLVM IR (-O2):
@.str.1 = [13 x i8] c" [%016lx]\0A\00"
@str = [12 x i8] c"Call Trace:\00"
define void @prnt_call_trace64(i32 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %7
7: ; preds = %2, %19
%8 = phi i32 [ %1, %2 ], [ %21, %19 ]
%9 = phi i32 [ 10, %2 ], [ %20, %19 ]
%10 = phi i32 [ 0, %2 ], [ %22, %19 ]
%11 = call i64 @xg_read_mem(i32 %8, i8* %4, i32 4, i32 0)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %26
13: ; preds = %7
%14 = load i32, i32* %3
%15 = icmp ugt i32 %14, -2147483648
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%18 = add nsw i32 %9, -1
br label %19
19: ; preds = %16, %13
%20 = phi i32 [ %18, %16 ], [ %9, %13 ]
%21 = add i32 %8, 4
%22 = add nuw nsw i32 %10, 1
%23 = icmp ult i32 %10, 10000
%24 = icmp sgt i32 %20, 0
%25 = select i1 %23, i1 %24, i1 false
br i1 %25, label %7, label %26
26: ; preds = %19, %7
ret void
}