#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*/ pthread_t;
typedef int /*<<< orphan*/ pthread_attr_t;

/* Variables and functions */
int /*<<< orphan*/ pthread_attr_getstack(int /*<<< orphan*/ *, void **,
                                         size_t *);
int /*<<< orphan*/ pthread_getattr_np(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_self();
int is_stack(void *ptr) {
  pthread_t self = pthread_self();
  pthread_attr_t attr;
  void *stack;
  size_t stacksize;
  pthread_getattr_np(self, &attr);
  pthread_attr_getstack(&attr, &stack, &stacksize);
  return ((uintptr_t)ptr >= (uintptr_t)stack &&
          (uintptr_t)ptr < (uintptr_t)stack + stacksize);
}


x86 assembly (clang, -O0):
<is_stack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1050 <pthread_self@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1030 <pthread_getattr_np@plt>
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1040 <pthread_attr_getstack@plt>
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rdx
xor    %eax,%eax
cmp    %rdx,%rcx
mov    %al,-0x21(%rbp)
jb     118e <is_stack+0x5e>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
add    -0x20(%rbp),%rcx
cmp    %rcx,%rax
setb   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_stack>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   1050 <pthread_self@plt>
lea    0x14(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
call   1030 <pthread_getattr_np@plt>
lea    0x8(%rsp),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
call   1040 <pthread_attr_getstack@plt>
mov    0x8(%rsp),%rax
cmp    %rbx,%rax
setbe  %cl
add    (%rsp),%rax
cmp    %rbx,%rax
seta   %al
and    %cl,%al
movzbl %al,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @is_stack(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
store i8* %0, i8** %2
%7 = call i32 (...) @pthread_self()
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = call i32 @pthread_getattr_np(i32 %8, i32* %4)
%10 = call i32 @pthread_attr_getstack(i32* %4, i8** %5, i64* %6)
%11 = load i8*, i8** %2
%12 = ptrtoint i8* %11 to i64
%13 = load i8*, i8** %5
%14 = ptrtoint i8* %13 to i64
%15 = icmp uge i64 %12, %14
br i1 %15, label %16, label %24
16: ; preds = %1
%17 = load i8*, i8** %2
%18 = ptrtoint i8* %17 to i64
%19 = load i8*, i8** %5
%20 = ptrtoint i8* %19 to i64
%21 = load i64, i64* %6
%22 = add i64 %20, %21
%23 = icmp ult i64 %18, %22
br label %24
24: ; preds = %16, %1
%25 = phi i1 [ false, %1 ], [ %23, %16 ]
%26 = zext i1 %25 to i32
ret i32 %26
}

LLVM IR (-O2):
define i32 @is_stack(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = tail call i32 (...) @pthread_self()
%6 = bitcast i32* %2 to i8*
%7 = bitcast i8** %3 to i8*
%8 = bitcast i64* %4 to i8*
%9 = call i32 @pthread_getattr_np(i32 %5, i32* %2)
%10 = call i32 @pthread_attr_getstack(i32* %2, i8** %3, i64* %4)
%11 = load i8*, i8** %3
%12 = icmp ule i8* %11, %0
%13 = ptrtoint i8* %11 to i64
%14 = ptrtoint i8* %0 to i64
%15 = load i64, i64* %4
%16 = add i64 %15, %13
%17 = icmp ugt i64 %16, %14
%18 = select i1 %12, i1 %17, i1 false
%19 = zext i1 %18 to i32
ret i32 %19
}