#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;
};

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_STACK;
int /*<<< orphan*/ f(unsigned long);
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  struct rlimit limit;
  if (getrlimit(RLIMIT_STACK, &limit) == 0) {
    printf("Stack limit (soft) = %ld\n", limit.rlim_cur);
    printf("Stack limit (hard) = %ld\n", limit.rlim_max);
  }
  unsigned long currentSize = 1;
  while (currentSize > 0) {
    printf("Allocating %lu bytes on stack\n", currentSize);
    f(currentSize);
    currentSize = currentSize << 1;
  }
  printf("Apparently we didn't crash...\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e9a(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1050 <getrlimit@plt>
cmp    $0x0,%rax
jne    117d <main+0x4d>
mov    -0xc(%rbp),%esi
lea    0xe9b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%esi
lea    0xea4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x1,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    11bc <main+0x8c>
mov    -0x18(%rbp),%rsi
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <f@plt>
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1185 <main+0x55>
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1060 <getrlimit@plt>
test   %rax,%rax
jne    117d <main+0x3d>
mov    0x4(%rsp),%esi
lea    0xe9b(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%esi
lea    0xea4(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebx
lea    0xeab(%rip),%r14
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <f@plt>
add    %rbx,%rbx
jne    1190 <main+0x50>
lea    0xea2(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
@RLIMIT_STACK = global i32 0
@.str = [26 x i8] c"Stack limit (soft) = %ld\0A\00"
@.str.1 = [26 x i8] c"Stack limit (hard) = %ld\0A\00"
@.str.2 = [31 x i8] c"Allocating %lu bytes on stack\0A\00"
@.str.3 = [31 x i8] c"Apparently we didn't crash...\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.rlimit
%3 = alloca i64
store i32 0, i32* %1
%4 = load i32, i32* @RLIMIT_STACK
%5 = call i64 @getrlimit(i32 %4, %struct.rlimit* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %14
7: ; preds = %0
%8 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 1
%9 = load i32, i32* %8
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %7, %0
store i64 1, i64* %3
br label %15
15: ; preds = %18, %14
%16 = load i64, i64* %3
%17 = icmp ugt i64 %16, 0
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = load i64, i64* %3
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i64 %19)
%21 = load i64, i64* %3
%22 = call i32 @f(i64 %21)
%23 = load i64, i64* %3
%24 = shl i64 %23, 1
store i64 %24, i64* %3
br label %15
25: ; preds = %15
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32 }
@RLIMIT_STACK = global i32 0
@.str = [26 x i8] c"Stack limit (soft) = %ld\0A\00"
@.str.1 = [26 x i8] c"Stack limit (hard) = %ld\0A\00"
@.str.2 = [31 x i8] c"Allocating %lu bytes on stack\0A\00"
@str = [30 x i8] c"Apparently we didn't crash...\00"
define i32 @main() {
%1 = alloca %struct.rlimit
%2 = bitcast %struct.rlimit* %1 to i8*
%3 = load i32, i32* @RLIMIT_STACK
%4 = call i64 @getrlimit(i32 %3, %struct.rlimit* %1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %13
6: ; preds = %0
%7 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 1
%8 = load i32, i32* %7
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 0
%11 = load i32, i32* %10
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %6, %0
br label %14
14: ; preds = %13, %14
%15 = phi i64 [ %18, %14 ], [ 1, %13 ]
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i64 %15)
%17 = call i32 @f(i64 %15)
%18 = shl i64 %15, 1
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %14
20: ; preds = %14
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
ret i32 0
}