#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 rlimit {
  int /*<<< orphan*/ rlim_max;
  int /*<<< orphan*/ rlim_cur;
};
typedef int /*<<< orphan*/ pthread_attr_t;

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_STACK;
int /*<<< orphan*/ getrlimit(int /*<<< orphan*/, struct rlimit *);
scalar_t__ malloc(int);
int malloc_usable_size(char *);
size_t next_frame();
int /*<<< orphan*/ printf(char *, ...);
int pthread_attr_getstacksize(int /*<<< orphan*/ *, size_t *);
int main() {
  char top_stack;
  struct rlimit r;
  size_t sz;
  pthread_attr_t attr;
  int err;
  size_t ptr;
  char *val;
  int a, b, d;
  printf("\n\n");
  printf("top_stack %p\n", &top_stack);
  getrlimit(RLIMIT_STACK, &r);
  printf("rlimit_stack cur: %ld max: %ld\n", r.rlim_cur, r.rlim_max);
  err = pthread_attr_getstacksize(&attr, &sz);
  if (err) {
    printf("pthread_attr_getstacksize %zd\n", sz);
  } else {
    printf("no pthread_attr_getstacksize\n");
  }
  ptr = next_frame();
  printf("stack is growing %s\n", (ptr < (size_t)&top_stack) ? "down" : "up");
  val = (char *)malloc(10);
  a = malloc_usable_size(val);
  b = malloc_usable_size(val + 1);
  d = malloc_usable_size(val + 20);
  printf("malloc_usable_size\nok %d\nfalse offset: %d\nfalse offset: %d\n", a,
         b, d);
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe7f(%rip),%rdi
lea    -0x5(%rbp),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1070 <getrlimit@plt>
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x1c(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <pthread_attr_getstacksize@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     11e6 <main+0x86>
mov    -0x18(%rbp),%rsi
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11f4 <main+0x94>
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <next_frame@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rcx
lea    -0x5(%rbp),%rdx
lea    0xe7a(%rip),%rsi
lea    0xe6e(%rip),%rax
cmp    %rdx,%rcx
cmovb  %rax,%rsi
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0xa,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <malloc_usable_size@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
call   1040 <malloc_usable_size@plt>
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x14,%rdi
call   1040 <malloc_usable_size@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x34(%rbp),%esi
mov    -0x38(%rbp),%edx
mov    -0x3c(%rbp),%ecx
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xd7f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
lea    0xf15(%rip),%rdi
call   1040 <puts@plt>
lea    0xe65(%rip),%rdi
lea    0x7(%rsp),%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1090 <getrlimit@plt>
mov    0x8(%rsp),%edx
mov    0xc(%rsp),%esi
lea    0xe42(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x1c(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   10a0 <pthread_attr_getstacksize@plt>
test   %eax,%eax
je     11fb <main+0x7b>
mov    0x10(%rsp),%rsi
lea    0xe3c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1207 <main+0x87>
lea    0xea4(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
call   1080 <next_frame@plt>
cmp    %rbx,%rax
lea    0xe4a(%rip),%rax
lea    0xe48(%rip),%rsi
cmovb  %rax,%rsi
lea    0xe23(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <malloc_usable_size@plt>
mov    %eax,%r14d
lea    0x1(%rbx),%rdi
call   1060 <malloc_usable_size@plt>
mov    %eax,%ebp
add    $0x14,%rbx
mov    %rbx,%rdi
call   1060 <malloc_usable_size@plt>
lea    0xe03(%rip),%rdi
mov    %r14d,%esi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
@.str = [3 x i8] c"\0A\0A\00"
@.str.1 = [14 x i8] c"top_stack %p\0A\00"
@RLIMIT_STACK = global i32 0
@.str.2 = [32 x i8] c"rlimit_stack cur: %ld max: %ld\0A\00"
@.str.3 = [31 x i8] c"pthread_attr_getstacksize %zd\0A\00"
@.str.4 = [30 x i8] c"no pthread_attr_getstacksize\0A\00"
@.str.5 = [21 x i8] c"stack is growing %s\0A\00"
@.str.6 = [5 x i8] c"down\00"
@.str.7 = [3 x i8] c"up\00"
@.str.8 = [60 x i8] c"malloc_usable_size\0Aok %d\0Afalse offset: %d\0Afalse offset: %d\0A\00"
@.str.9 = [2 x i8] c"\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca %struct.rlimit
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %1
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%14 = load i32, i32* @RLIMIT_STACK
%15 = call i32 @getrlimit(i32 %14, %struct.rlimit* %3)
%16 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i32 0, i32 1
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %17, i32 %19)
%21 = call i32 @pthread_attr_getstacksize(i32* %5, i64* %4)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %0
%25 = load i64, i64* %4
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i64 %25)
br label %29
27: ; preds = %0
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
%30 = call i64 (...) @next_frame()
store i64 %30, i64* %7
%31 = load i64, i64* %7
%32 = ptrtoint i8* %2 to i64
%33 = icmp ult i64 %31, %32
%34 = zext i1 %33 to i64
%35 = select i1 %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %35)
%37 = call i64 @malloc(i32 10)
%38 = inttoptr i64 %37 to i8*
store i8* %38, i8** %8
%39 = load i8*, i8** %8
%40 = call i32 @malloc_usable_size(i8* %39)
store i32 %40, i32* %9
%41 = load i8*, i8** %8
%42 = getelementptr inbounds i8, i8* %41, i64 1
%43 = call i32 @malloc_usable_size(i8* %42)
store i32 %43, i32* %10
%44 = load i8*, i8** %8
%45 = getelementptr inbounds i8, i8* %44, i64 20
%46 = call i32 @malloc_usable_size(i8* %45)
store i32 %46, i32* %11
%47 = load i32, i32* %9
%48 = load i32, i32* %10
%49 = load i32, i32* %11
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.8, i64 0, i64 0), i32 %47, i32 %48, i32 %49)
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32 }
@.str.1 = [14 x i8] c"top_stack %p\0A\00"
@RLIMIT_STACK = global i32 0
@.str.2 = [32 x i8] c"rlimit_stack cur: %ld max: %ld\0A\00"
@.str.3 = [31 x i8] c"pthread_attr_getstacksize %zd\0A\00"
@.str.5 = [21 x i8] c"stack is growing %s\0A\00"
@.str.6 = [5 x i8] c"down\00"
@.str.7 = [3 x i8] c"up\00"
@.str.8 = [60 x i8] c"malloc_usable_size\0Aok %d\0Afalse offset: %d\0Afalse offset: %d\0A\00"
@str = [2 x i8] c"\0A\00"
@str.10 = [29 x i8] c"no pthread_attr_getstacksize\00"
define i32 @main() {
%1 = alloca i8
%2 = alloca %struct.rlimit
%3 = alloca i64
%4 = alloca i32
%5 = bitcast %struct.rlimit* %2 to i8*
%6 = bitcast i64* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%10 = load i32, i32* @RLIMIT_STACK
%11 = call i32 @getrlimit(i32 %10, %struct.rlimit* %2)
%12 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 1
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %13, i32 %15)
%17 = call i32 @pthread_attr_getstacksize(i32* %4, i64* %3)
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %0
%20 = load i64, i64* %3
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i64 %20)
br label %24
22: ; preds = %0
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.10, i64 0, i64 0))
br label %24
24: ; preds = %22, %19
%25 = call i64 (...) @next_frame()
%26 = ptrtoint i8* %1 to i64
%27 = icmp ult i64 %25, %26
%28 = select i1 %27, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %28)
%30 = call i64 @malloc(i32 10)
%31 = inttoptr i64 %30 to i8*
%32 = call i32 @malloc_usable_size(i8* %31)
%33 = getelementptr inbounds i8, i8* %31, i64 1
%34 = call i32 @malloc_usable_size(i8* %33)
%35 = getelementptr inbounds i8, i8* %31, i64 20
%36 = call i32 @malloc_usable_size(i8* %35)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([60 x i8], [60 x i8]* @.str.8, i64 0, i64 0), i32 %32, i32 %34, i32 %36)
%38 = call i32 @putchar(i32 10)
ret i32 0
}