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

/* Variables and functions */
int /*<<< orphan*/ dump_ln(void *, int);
int /*<<< orphan*/ printf(char *);
void show_stack(unsigned long sp) {
  unsigned long start;
  unsigned long end;
  int num;
  start = sp & ~0xf;
  end = (sp & ~0xfff) + 0x1000;
  num = (end - start) / sizeof(long);
  if (num < 0)
    num = 4;
  if (num > (0x1000 / sizeof(long)))
    num = 0x1000 / sizeof(long);
  printf("\n");
  dump_ln((void *)start, num);
}


x86 assembly (clang, -O0):
<show_stack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
and    $0xfffffffffffffff0,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
and    $0xfffffffffffff000,%rax
add    $0x1000,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x10(%rbp),%rax
shr    $0x3,%rax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    116c <show_stack+0x4c>
movl   $0x4,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    $0x200,%rax
jbe    1183 <show_stack+0x63>
movl   $0x200,-0x1c(%rbp)
lea    0xe76(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <dump_ln@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<show_stack>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0x1000(%rdi),%rax
and    $0xfffffffffffffff0,%rbx
movabs $0x7fffff000,%rcx
and    %rax,%rcx
sub    %rbx,%rcx
shr    $0x3,%rcx
test   %ecx,%ecx
mov    $0x4,%eax
cmovns %ecx,%eax
cmp    $0x200,%eax
mov    $0x200,%ebp
cmovb  %eax,%ebp
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <dump_ln@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"\0A\00"
define void @show_stack(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
store i64 %0, i64* %2
%6 = load i64, i64* %2
%7 = and i64 %6, -16
store i64 %7, i64* %3
%8 = load i64, i64* %2
%9 = and i64 %8, -4096
%10 = add i64 %9, 4096
store i64 %10, i64* %4
%11 = load i64, i64* %4
%12 = load i64, i64* %3
%13 = sub i64 %11, %12
%14 = udiv i64 %13, 8
%15 = trunc i64 %14 to i32
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %1
store i32 4, i32* %5
br label %19
19: ; preds = %18, %1
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = icmp ugt i64 %21, 512
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 512, i32* %5
br label %24
24: ; preds = %23, %19
%25 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%26 = load i64, i64* %3
%27 = inttoptr i64 %26 to i8*
%28 = load i32, i32* %5
%29 = call i32 @dump_ln(i8* %27, i32 %28)
ret void
}

LLVM IR (-O2):
define void @show_stack(i64 %0) {
%2 = and i64 %0, -16
%3 = add i64 %0, 4096
%4 = and i64 %3, 34359734272
%5 = sub i64 %4, %2
%6 = lshr exact i64 %5, 3
%7 = trunc i64 %6 to i32
%8 = icmp slt i32 %7, 0
%9 = select i1 %8, i32 4, i32 %7
%10 = icmp ult i32 %9, 512
%11 = select i1 %10, i32 %9, i32 512
%12 = tail call i32 @putchar(i32 10)
%13 = inttoptr i64 %2 to i8*
%14 = tail call i32 @dump_ln(i8* %13, i32 %11)
ret void
}