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

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
void stack_print(long int previous_sp, int level) {
  if (level <= 0) {
    return;
  }
  register long int rsp asm("sp");
  char buf[level * 1024];
  stack_print(rsp, level - 1);
  printf("%d. psp=%#lx, sp=%#lx, size=%dB(%dB+%dKB)", level, previous_sp, rsp,
         (int)(previous_sp - rsp), (int)(previous_sp - rsp - sizeof(buf)),
         (int)(sizeof(buf) / 1024));
  printf("\n");
}


x86 assembly (clang, -O0):
<stack_print>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jg     113e <stack_print+0x1e>
jmp    11be <stack_print+0x9e>
mov    -0xc(%rbp),%eax
shl    $0xa,%eax
mov    %eax,%eax
mov    %rax,-0x30(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
sub    $0x1,%esi
call   1040 <stack_print@plt>
mov    -0x30(%rbp),%rax
mov    -0xc(%rbp),%esi
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x8(%rbp),%rdi
sub    -0x18(%rbp),%rdi
mov    %edi,%r8d
mov    -0x8(%rbp),%rdi
sub    -0x18(%rbp),%rdi
sub    %rax,%rdi
mov    %edi,%r9d
shr    $0xa,%rax
lea    0xe69(%rip),%rdi
sub    $0x10,%rsp
mov    %eax,(%rsp)
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x10,%rsp
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stack_print>:
test   %esi,%esi
jle    1181 <stack_print+0x51>
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
mov    %rsp,%r15
mov    %esi,%ebp
and    $0x3fffff,%ebp
lea    -0x1(%rbx),%esi
call   1050 <stack_print@plt>
mov    %ebp,(%rsp)
lea    0xea3(%rip),%rdi
mov    %ebx,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r15,%rsp
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
ret

LLVM IR (-O0):
@.str = [42 x i8] c"%d. psp=%#lx, sp=%#lx, size=%dB(%dB+%dKB)\00"
@.str.1 = [2 x i8] c"\0A\00"
define void @stack_print(i64 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
store i64 %0, i64* %3
store i32 %1, i32* %4
%8 = load i32, i32* %4
%9 = icmp sle i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %2
br label %37
11: ; preds = %2
%12 = load i32, i32* %4
%13 = mul nsw i32 %12, 1024
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %6
%16 = alloca i8, i64 %14
store i64 %14, i64* %7
%17 = load i64, i64* %5
%18 = load i32, i32* %4
%19 = sub nsw i32 %18, 1
%20 = load i32, i32* %4
%21 = load i64, i64* %3
%22 = load i64, i64* %5
%23 = load i64, i64* %3
%24 = load i64, i64* %5
%25 = sub nsw i64 %23, %24
%26 = trunc i64 %25 to i32
%27 = load i64, i64* %3
%28 = load i64, i64* %5
%29 = sub nsw i64 %27, %28
%30 = sub i64 %29, %14
%31 = trunc i64 %30 to i32
%32 = udiv i64 %14, 1024
%33 = trunc i64 %32 to i32
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %20, i64 %21, i64 %22, i32 %26, i32 %31, i32 %33)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%36 = load i8*, i8** %6
br label %37
37: ; preds = %11, %10
ret void
}

LLVM IR (-O2):
@.str = [42 x i8] c"%d. psp=%#lx, sp=%#lx, size=%dB(%dB+%dKB)\00"
define void @stack_print(i64 %0, i32 %1) {
%3 = icmp slt i32 %1, 1
br i1 %3, label %4, label %5
4: ; preds = %2, %5
ret void
5: ; preds = %2
%6 = and i32 %1, 4194303
%7 = tail call i8* @llvm.stacksave()
%8 = add nsw i32 %1, -1
tail call void @stack_print(i64 undef, i32 %8)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %1, i64 %0, i64 undef, i32 undef, i32 undef, i32 %6)
%10 = tail call i32 @putchar(i32 10)
tail call void @llvm.stackrestore(i8* %7)
br label %4
}