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

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_AS;
int /*<<< orphan*/ RLIMIT_CPU;
int /*<<< orphan*/ RLIMIT_FSIZE;
int /*<<< orphan*/ RLIMIT_NOFILE;
int /*<<< orphan*/ RLIMIT_NPROC;
int /*<<< orphan*/ RLIMIT_STACK;
long RLIM_INFINITY;
int setrlimit(int /*<<< orphan*/, struct rlimit *);
int setlimit(int type, long softlim, long hardlim) {
  static struct rlimit lim;
  lim.rlim_cur = (softlim <= 0 ? RLIM_INFINITY : softlim),
  lim.rlim_max = (hardlim <= 0 ? RLIM_INFINITY : hardlim);
  switch (type) {
  case 0:
    return setrlimit(RLIMIT_AS, &lim);
  case 1:
    return setrlimit(RLIMIT_CPU, &lim);
  case 2:
    return setrlimit(RLIMIT_FSIZE, &lim);
  case 3:
    return setrlimit(RLIMIT_NOFILE, &lim);
  case 4:
    return setrlimit(RLIMIT_STACK, &lim);
  case 5:
    return setrlimit(RLIMIT_NPROC, &lim);
  default:
    return -1;
  }
}


x86 assembly (clang, -O0):
<setlimit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jg     1141 <setlimit+0x31>
mov    0x2e8b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1149 <setlimit+0x39>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,0x2edc(%rip)
cmpq   $0x0,-0x18(%rbp)
jg     1172 <setlimit+0x62>
mov    0x2e5a(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    117a <setlimit+0x6a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,0x2eb3(%rip)
mov    -0x8(%rbp),%eax
mov    %rax,-0x30(%rbp)
sub    $0x5,%rax
ja     1258 <setlimit+0x148>
mov    -0x30(%rbp),%rax
lea    0xe5f(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    0x2e76(%rip),%rsi
call   1030 <setrlimit@plt>
mov    %eax,-0x4(%rbp)
jmp    125f <setlimit+0x14f>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
lea    0x2e59(%rip),%rsi
call   1030 <setrlimit@plt>
mov    %eax,-0x4(%rbp)
jmp    125f <setlimit+0x14f>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
lea    0x2e3c(%rip),%rsi
call   1030 <setrlimit@plt>
mov    %eax,-0x4(%rbp)
jmp    125f <setlimit+0x14f>
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
lea    0x2e1f(%rip),%rsi
call   1030 <setrlimit@plt>
mov    %eax,-0x4(%rbp)
jmp    125f <setlimit+0x14f>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
lea    0x2e02(%rip),%rsi
call   1030 <setrlimit@plt>
mov    %eax,-0x4(%rbp)
jmp    125f <setlimit+0x14f>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    0x2de5(%rip),%rsi
call   1030 <setrlimit@plt>
mov    %eax,-0x4(%rbp)
jmp    125f <setlimit+0x14f>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setlimit>:
test   %rsi,%rsi
mov    0x2ec6(%rip),%rax
mov    (%rax),%rax
cmovle %rax,%rsi
mov    %rsi,0x2f08(%rip)
test   %rdx,%rdx
cmovle %rax,%rdx
mov    %rdx,0x2f02(%rip)
cmp    $0x5,%edi
ja     1157 <setlimit+0x47>
movslq %edi,%rax
lea    0x2ca3(%rip),%rcx
mov    (%rcx,%rax,8),%rax
mov    (%rax),%edi
lea    0x2ede(%rip),%rsi
jmp    1030 <setrlimit@plt>
mov    $0xffffffff,%eax
ret

LLVM IR (-O0):
%struct.rlimit = type { i64, i64 }
@setlimit.lim = internal global %struct.rlimit zeroinitializer
@RLIM_INFINITY = global i64 0
@RLIMIT_AS = global i32 0
@RLIMIT_CPU = global i32 0
@RLIMIT_FSIZE = global i32 0
@RLIMIT_NOFILE = global i32 0
@RLIMIT_STACK = global i32 0
@RLIMIT_NPROC = global i32 0
define i32 @setlimit(i32 %0, i64 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
store i32 %0, i32* %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%8 = load i64, i64* %6
%9 = icmp sle i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %3
%11 = load i64, i64* @RLIM_INFINITY
br label %14
12: ; preds = %3
%13 = load i64, i64* %6
br label %14
14: ; preds = %12, %10
%15 = phi i64 [ %11, %10 ], [ %13, %12 ]
store i64 %15, i64* getelementptr inbounds (%struct.rlimit, %struct.rlimit* @setlimit.lim, i32 0, i32 0)
%16 = load i64, i64* %7
%17 = icmp sle i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = load i64, i64* @RLIM_INFINITY
br label %22
20: ; preds = %14
%21 = load i64, i64* %7
br label %22
22: ; preds = %20, %18
%23 = phi i64 [ %19, %18 ], [ %21, %20 ]
store i64 %23, i64* getelementptr inbounds (%struct.rlimit, %struct.rlimit* @setlimit.lim, i32 0, i32 1)
%24 = load i32, i32* %5
switch i32 %24, label %43 [
i32 0, label %25
i32 1, label %28
i32 2, label %31
i32 3, label %34
i32 4, label %37
i32 5, label %40
]
25: ; preds = %22
%26 = load i32, i32* @RLIMIT_AS
%27 = call i32 @setrlimit(i32 %26, %struct.rlimit* @setlimit.lim)
store i32 %27, i32* %4
br label %44
28: ; preds = %22
%29 = load i32, i32* @RLIMIT_CPU
%30 = call i32 @setrlimit(i32 %29, %struct.rlimit* @setlimit.lim)
store i32 %30, i32* %4
br label %44
31: ; preds = %22
%32 = load i32, i32* @RLIMIT_FSIZE
%33 = call i32 @setrlimit(i32 %32, %struct.rlimit* @setlimit.lim)
store i32 %33, i32* %4
br label %44
34: ; preds = %22
%35 = load i32, i32* @RLIMIT_NOFILE
%36 = call i32 @setrlimit(i32 %35, %struct.rlimit* @setlimit.lim)
store i32 %36, i32* %4
br label %44
37: ; preds = %22
%38 = load i32, i32* @RLIMIT_STACK
%39 = call i32 @setrlimit(i32 %38, %struct.rlimit* @setlimit.lim)
store i32 %39, i32* %4
br label %44
40: ; preds = %22
%41 = load i32, i32* @RLIMIT_NPROC
%42 = call i32 @setrlimit(i32 %41, %struct.rlimit* @setlimit.lim)
store i32 %42, i32* %4
br label %44
43: ; preds = %22
store i32 -1, i32* %4
br label %44
44: ; preds = %43, %40, %37, %34, %31, %28, %25
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
%struct.rlimit = type { i64, i64 }
@setlimit.lim = internal global %struct.rlimit zeroinitializer
@RLIM_INFINITY = global i64 0
@RLIMIT_AS = global i32 0
@RLIMIT_CPU = global i32 0
@RLIMIT_FSIZE = global i32 0
@RLIMIT_NOFILE = global i32 0
@RLIMIT_STACK = global i32 0
@RLIMIT_NPROC = global i32 0
@switch.table.setlimit = [6 x i32*] [i32* @RLIMIT_AS, i32* @RLIMIT_CPU, i32* @RLIMIT_FSIZE, i32* @RLIMIT_NOFILE, i32* @RLIMIT_STACK, i32* @RLIMIT_NPROC]
define i32 @setlimit(i32 %0, i64 %1, i64 %2) {
%4 = icmp slt i64 %1, 1
%5 = load i64, i64* @RLIM_INFINITY
%6 = select i1 %4, i64 %5, i64 %1
store i64 %6, i64* getelementptr inbounds (%struct.rlimit, %struct.rlimit* @setlimit.lim, i64 0, i32 0)
%7 = icmp slt i64 %2, 1
%8 = select i1 %7, i64 %5, i64 %2
store i64 %8, i64* getelementptr inbounds (%struct.rlimit, %struct.rlimit* @setlimit.lim, i64 0, i32 1)
%9 = icmp ult i32 %0, 6
br i1 %9, label %10, label %16
10: ; preds = %3
%11 = sext i32 %0 to i64
%12 = getelementptr inbounds [6 x i32*], [6 x i32*]* @switch.table.setlimit, i64 0, i64 %11
%13 = load i32*, i32** %12
%14 = load i32, i32* %13
%15 = tail call i32 @setrlimit(i32 %14, %struct.rlimit* @setlimit.lim)
br label %16
16: ; preds = %3, %10
%17 = phi i32 [ -1, %3 ], [ %15, %10 ]
ret i32 %17
}