#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 */
struct pt_regs {
  int dummy;
};

/* Variables and functions */
scalar_t__ kernel_stack_pointer(struct pt_regs *);
scalar_t__ regs_within_kernel_stack(struct pt_regs *, unsigned long);
__attribute__((always_)) unsigned long
regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n) {
  unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
  addr += n;
  if (regs_within_kernel_stack(regs, (unsigned long)addr))
    return *addr;
  else
    return 0;
}


x86 assembly (clang, -O0):
<regs_get_kernel_stack_nth>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <kernel_stack_pointer@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,%ecx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <regs_within_kernel_stack@plt>
cmp    $0x0,%rax
je     1177 <regs_get_kernel_stack_nth+0x57>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    117f <regs_get_kernel_stack_nth+0x5f>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<regs_get_kernel_stack_nth>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%rbx
call   1040 <kernel_stack_pointer@plt>
mov    %ebp,%ecx
lea    (%rax,%rcx,8),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <regs_within_kernel_stack@plt>
test   %rax,%rax
je     1149 <regs_get_kernel_stack_nth+0x29>
mov    (%r14),%rax
jmp    114b <regs_get_kernel_stack_nth+0x2b>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.pt_regs = type { i32 }
define i64 @regs_get_kernel_stack_nth(%struct.pt_regs* %0, i32 %1) {
%3 = alloca i64
%4 = alloca %struct.pt_regs*
%5 = alloca i32
%6 = alloca i64*
store %struct.pt_regs* %0, %struct.pt_regs** %4
store i32 %1, i32* %5
%7 = load %struct.pt_regs*, %struct.pt_regs** %4
%8 = call i64 @kernel_stack_pointer(%struct.pt_regs* %7)
%9 = inttoptr i64 %8 to i64*
store i64* %9, i64** %6
%10 = load i32, i32* %5
%11 = load i64*, i64** %6
%12 = zext i32 %10 to i64
%13 = getelementptr inbounds i64, i64* %11, i64 %12
store i64* %13, i64** %6
%14 = load %struct.pt_regs*, %struct.pt_regs** %4
%15 = load i64*, i64** %6
%16 = ptrtoint i64* %15 to i64
%17 = call i64 @regs_within_kernel_stack(%struct.pt_regs* %14, i64 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = load i64*, i64** %6
%21 = load i64, i64* %20
store i64 %21, i64* %3
br label %23
22: ; preds = %2
store i64 0, i64* %3
br label %23
23: ; preds = %22, %19
%24 = load i64, i64* %3
ret i64 %24
}

LLVM IR (-O2):
%struct.pt_regs = type { i32 }
define i64 @regs_get_kernel_stack_nth(%struct.pt_regs* %0, i32 %1) {
%3 = tail call i64 @kernel_stack_pointer(%struct.pt_regs* %0)
%4 = inttoptr i64 %3 to i64*
%5 = zext i32 %1 to i64
%6 = getelementptr inbounds i64, i64* %4, i64 %5
%7 = ptrtoint i64* %6 to i64
%8 = tail call i64 @regs_within_kernel_stack(%struct.pt_regs* %0, i64 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = load i64, i64* %6
br label %12
12: ; preds = %2, %10
%13 = phi i64 [ %11, %10 ], [ 0, %2 ]
ret i64 %13
}