#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 rlim_cur;
  int rlim_max;
};

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_STACK;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ setrlimit(int /*<<< orphan*/, struct rlimit *);
void shrink_stack(int stack_size) {
  struct rlimit rlp;
  rlp.rlim_cur = rlp.rlim_max = stack_size;
  if (setrlimit(RLIMIT_STACK, &rlp) < 0) {
    perror(" Failed to reset stacksize\n");
    exit(errno);
  };
  ;
}


x86 assembly (clang, -O0):
<shrink_stack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    %eax,-0x10(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1030 <setrlimit@plt>
cmp    $0x0,%rax
jge    117a <shrink_stack+0x4a>
lea    0xe99(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shrink_stack>:
push   %rax
mov    %edi,0x4(%rsp)
mov    %edi,(%rsp)
mov    0x2ea1(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1030 <setrlimit@plt>
test   %rax,%rax
js     1150 <shrink_stack+0x20>
pop    %rax
ret
lea    0xea9(%rip),%rdi
call   1040 <perror@plt>
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
@RLIMIT_STACK = global i32 0
@.str = [28 x i8] c" Failed to reset stacksize\0A\00"
@errno = global i32 0
define void @shrink_stack(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.rlimit
store i32 %0, i32* %2
%4 = load i32, i32* %2
%5 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i32 0, i32 1
store i32 %4, i32* %5
%6 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i32 0, i32 0
store i32 %4, i32* %6
%7 = load i32, i32* @RLIMIT_STACK
%8 = call i64 @setrlimit(i32 %7, %struct.rlimit* %3)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @errno
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %1
ret void
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32 }
@RLIMIT_STACK = global i32 0
@.str = [28 x i8] c" Failed to reset stacksize\0A\00"
@errno = global i32 0
define void @shrink_stack(i32 %0) {
%2 = alloca %struct.rlimit
%3 = bitcast %struct.rlimit* %2 to i8*
%4 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 1
store i32 %0, i32* %4
%5 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 0
store i32 %0, i32* %5
%6 = load i32, i32* @RLIMIT_STACK
%7 = call i64 @setrlimit(i32 %6, %struct.rlimit* %2)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @errno
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %1
ret void
}