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

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


x86 assembly (clang, -O0):
<prnt_call_trace32>:
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    11d7 <prnt_call_trace32+0xb7>
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_trace32+0x67>
jmp    11d7 <prnt_call_trace32+0xb7>
cmpl   $0xc000000,-0x14(%rbp)
jle    11ae <prnt_call_trace32+0x8e>
mov    -0x14(%rbp),%esi
lea    0xe6f(%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    11d2 <prnt_call_trace32+0xb2>
jmp    11d7 <prnt_call_trace32+0xb7>
jmp    115b <prnt_call_trace32+0x3b>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<prnt_call_trace32>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
lea    0xec4(%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_trace32+0x83>
mov    0xc(%rsp),%esi
cmp    $0xc000001,%esi
jl     11a0 <prnt_call_trace32+0x70>
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0xffffffff,%r12d
cmp    $0x270f,%ebp
ja     11b3 <prnt_call_trace32+0x83>
add    $0x4,%ebx
add    $0x1,%ebp
test   %r12d,%r12d
jg     1170 <prnt_call_trace32+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 = [11 x i8] c" [%08x]\0A\00"
define void @prnt_call_trace32(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 ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %9)
br label %11
11: ; preds = %37, %2
%12 = load i32, i32* %5
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %38
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 %38
20: ; preds = %14
%21 = load i32, i32* %7
%22 = icmp sgt i32 %21, 201326592
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i32, i32* %7
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %5
%27 = add nsw i32 %26, -1
store i32 %27, i32* %5
br label %28
28: ; preds = %23, %20
%29 = load i32, i32* %4
%30 = sext i32 %29 to i64
%31 = add i64 %30, 4
%32 = trunc i64 %31 to i32
store i32 %32, i32* %4
%33 = load i32, i32* %6
%34 = add nsw i32 %33, 1
store i32 %34, i32* %6
%35 = icmp sgt i32 %34, 10000
br i1 %35, label %36, label %37
36: ; preds = %28
br label %38
37: ; preds = %28
br label %11
38: ; preds = %19, %36, %11
ret void
}

LLVM IR (-O2):
@.str.1 = [11 x i8] c" [%08x]\0A\00"
@str = [12 x i8] c"Call Trace:\00"
define void @prnt_call_trace32(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 ([11 x i8], [11 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 sgt i32 %14, 201326592
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 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
}