#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_NOFILE;
int /*<<< orphan*/ RLIMIT_STACK;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ getrlimit(int /*<<< orphan*/, struct rlimit *);
int setrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ stderr;
int setup_limits(void) {
  struct rlimit rlim;
  int result, err;
  rlim.rlim_cur = rlim.rlim_max = 100000;
  if ((result = setrlimit(RLIMIT_NOFILE, &rlim)) < 0) {
    getrlimit(RLIMIT_NOFILE, &rlim);
    fprintf(stderr,
            "Could not raise the limit of open files. Using system default of "
            "%lu.\n",
            rlim.rlim_cur);
  }
  rlim.rlim_cur = rlim.rlim_max = 1024 * 1024;
  if ((result = setrlimit(RLIMIT_STACK, &rlim)) < 0) {
    getrlimit(RLIMIT_STACK, &rlim);
    fprintf(stderr,
            "Could not raise the stack size. Using system default of %lu.\n",
            rlim.rlim_cur);
  }
  return result;
}


x86 assembly (clang, -O0):
<setup_limits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x186a0,-0x4(%rbp)
movl   $0x186a0,-0x8(%rbp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1040 <setrlimit@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    118e <setup_limits+0x5e>
mov    0x2e7d(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1050 <getrlimit@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe77(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x100000,-0x4(%rbp)
movl   $0x100000,-0x8(%rbp)
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1040 <setrlimit@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11e4 <setup_limits+0xb4>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1050 <getrlimit@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xe68(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_limits>:
push   %r14
push   %rbx
push   %rax
movabs $0x186a0000186a0,%rax
mov    %rax,(%rsp)
mov    0x2e9f(%rip),%rbx
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1040 <setrlimit@plt>
test   %eax,%eax
jns    1179 <setup_limits+0x49>
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1050 <getrlimit@plt>
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
mov    (%rsp),%edx
lea    0xe8c(%rip),%rsi
call   1030 <fprintf@plt>
movabs $0x10000000100000,%rax
mov    %rax,(%rsp)
mov    0x2e42(%rip),%r14
mov    (%r14),%edi
mov    %rsp,%rsi
call   1040 <setrlimit@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11c2 <setup_limits+0x92>
mov    (%r14),%edi
mov    %rsp,%rsi
call   1050 <getrlimit@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
mov    (%rsp),%edx
lea    0xe8a(%rip),%rsi
call   1030 <fprintf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
@RLIMIT_NOFILE = global i32 0
@stderr = global i32 0
@.str = [71 x i8] c"Could not raise the limit of open files. Using system default of %lu.\0A\00"
@RLIMIT_STACK = global i32 0
@.str.1 = [62 x i8] c"Could not raise the stack size. Using system default of %lu.\0A\00"
define i32 @setup_limits() {
%1 = alloca %struct.rlimit
%2 = alloca i32
%3 = alloca i32
%4 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 1
store i32 100000, i32* %4
%5 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 0
store i32 100000, i32* %5
%6 = load i32, i32* @RLIMIT_NOFILE
%7 = call i32 @setrlimit(i32 %6, %struct.rlimit* %1)
store i32 %7, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %0
%10 = load i32, i32* @RLIMIT_NOFILE
%11 = call i32 @getrlimit(i32 %10, %struct.rlimit* %1)
%12 = load i32, i32* @stderr
%13 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %9, %0
%17 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 1
store i32 1048576, i32* %17
%18 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 0
store i32 1048576, i32* %18
%19 = load i32, i32* @RLIMIT_STACK
%20 = call i32 @setrlimit(i32 %19, %struct.rlimit* %1)
store i32 %20, i32* %2
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %29
22: ; preds = %16
%23 = load i32, i32* @RLIMIT_STACK
%24 = call i32 @getrlimit(i32 %23, %struct.rlimit* %1)
%25 = load i32, i32* @stderr
%26 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %22, %16
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32 }
@RLIMIT_NOFILE = global i32 0
@stderr = global i32 0
@.str = [71 x i8] c"Could not raise the limit of open files. Using system default of %lu.\0A\00"
@RLIMIT_STACK = global i32 0
@.str.1 = [62 x i8] c"Could not raise the stack size. Using system default of %lu.\0A\00"
define i32 @setup_limits() {
%1 = alloca %struct.rlimit
%2 = bitcast %struct.rlimit* %1 to i8*
%3 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 1
store i32 100000, i32* %3
%4 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 0
store i32 100000, i32* %4
%5 = load i32, i32* @RLIMIT_NOFILE
%6 = call i32 @setrlimit(i32 %5, %struct.rlimit* %1)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %0
%9 = load i32, i32* @RLIMIT_NOFILE
%10 = call i32 @getrlimit(i32 %9, %struct.rlimit* %1)
%11 = load i32, i32* @stderr
%12 = load i32, i32* %4
%13 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %8, %0
store i32 1048576, i32* %3
store i32 1048576, i32* %4
%15 = load i32, i32* @RLIMIT_STACK
%16 = call i32 @setrlimit(i32 %15, %struct.rlimit* %1)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %14
%19 = load i32, i32* @RLIMIT_STACK
%20 = call i32 @getrlimit(i32 %19, %struct.rlimit* %1)
%21 = load i32, i32* @stderr
%22 = load i32, i32* %4
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %18, %14
ret i32 %16
}