#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;
};
typedef enum __vlimit_resource {
  ____Placeholder___vlimit_resource
} __vlimit_resource;
typedef enum __rlimit_resource {
  ____Placeholder___rlimit_resource
} __rlimit_resource;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int LIM_CPU;
int LIM_MAXRSS;
scalar_t__ __getrlimit(int, struct rlimit *);
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
int __setrlimit(int, struct rlimit *);
int vlimit(enum __vlimit_resource resource, int value) {
  if (resource >= LIM_CPU && resource <= LIM_MAXRSS) {
    enum __rlimit_resource rlimit_res =
        (enum __rlimit_resource)((int)resource - 1);
    struct rlimit lims;
    if (__getrlimit(rlimit_res, &lims) < 0)
      return -1;
    lims.rlim_cur = value;
    return __setrlimit(rlimit_res, &lims);
  }
  __set_errno(EINVAL);
  return -1;
}


x86 assembly (clang, -O0):
<vlimit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    0x2eb0(%rip),%rcx
cmp    (%rcx),%eax
jb     11a7 <vlimit+0x77>
mov    -0x8(%rbp),%eax
mov    0x2e86(%rip),%rcx
cmp    (%rcx),%eax
ja     11a7 <vlimit+0x77>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <__getrlimit@plt>
cmp    $0x0,%rax
jge    118d <vlimit+0x5d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <vlimit+0x8c>
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1050 <__setrlimit@plt>
mov    %eax,-0x4(%rbp)
jmp    11bc <vlimit+0x8c>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vlimit>:
push   %rbp
push   %rbx
push   %rax
mov    0x2ebe(%rip),%rax
cmp    %edi,(%rax)
ja     116e <vlimit+0x3e>
mov    %edi,%ebx
mov    0x2e99(%rip),%rax
cmp    %edi,(%rax)
jb     116e <vlimit+0x3e>
mov    %esi,%ebp
add    $0xffffffff,%ebx
mov    %rsp,%rsi
mov    %ebx,%edi
call   1040 <__getrlimit@plt>
test   %rax,%rax
js     117c <vlimit+0x4c>
mov    %ebp,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
call   1050 <__setrlimit@plt>
jmp    1181 <vlimit+0x51>
mov    0x2e5b(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.rlimit = type { i32 }
@LIM_CPU = global i32 0
@LIM_MAXRSS = global i32 0
@EINVAL = global i32 0
define i32 @vlimit(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.rlimit
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = load i32, i32* @LIM_CPU
%10 = icmp uge i32 %8, %9
br i1 %10, label %11, label %27
11: ; preds = %2
%12 = load i32, i32* %4
%13 = load i32, i32* @LIM_MAXRSS
%14 = icmp ule i32 %12, %13
br i1 %14, label %15, label %27
15: ; preds = %11
%16 = load i32, i32* %4
%17 = sub nsw i32 %16, 1
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = call i64 @__getrlimit(i32 %18, %struct.rlimit* %7)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
store i32 -1, i32* %3
br label %30
22: ; preds = %15
%23 = load i32, i32* %5
%24 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* %6
%26 = call i32 @__setrlimit(i32 %25, %struct.rlimit* %7)
store i32 %26, i32* %3
br label %30
27: ; preds = %11, %2
%28 = load i32, i32* @EINVAL
%29 = call i32 @__set_errno(i32 %28)
store i32 -1, i32* %3
br label %30
30: ; preds = %27, %22, %21
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
%struct.rlimit = type { i32 }
@LIM_CPU = global i32 0
@LIM_MAXRSS = global i32 0
@EINVAL = global i32 0
define i32 @vlimit(i32 %0, i32 %1) {
%3 = alloca %struct.rlimit
%4 = load i32, i32* @LIM_CPU
%5 = icmp ugt i32 %4, %0
%6 = load i32, i32* @LIM_MAXRSS
%7 = icmp ult i32 %6, %0
%8 = select i1 %5, i1 true, i1 %7
br i1 %8, label %19, label %9
9: ; preds = %2
%10 = add nsw i32 %0, -1
%11 = bitcast %struct.rlimit* %3 to i8*
%12 = call i64 @__getrlimit(i32 %10, %struct.rlimit* %3)
%13 = icmp slt i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 0
store i32 %1, i32* %15
%16 = call i32 @__setrlimit(i32 %10, %struct.rlimit* %3)
br label %17
17: ; preds = %9, %14
%18 = phi i32 [ %16, %14 ], [ -1, %9 ]
br label %22
19: ; preds = %2
%20 = load i32, i32* @EINVAL
%21 = tail call i32 @__set_errno(i32 %20)
br label %22
22: ; preds = %19, %17
%23 = phi i32 [ %18, %17 ], [ -1, %19 ]
ret i32 %23
}