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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ EPERM;
#define RLIMIT_CORE 136
#define RLIMIT_CPU 135
#define RLIMIT_DATA 134
#define RLIMIT_FSIZE 133
#define RLIMIT_MEMLOCK 132
#define RLIMIT_NOFILE 131
#define RLIMIT_NPROC 130
#define RLIMIT_RSS 129
#define RLIMIT_STACK 128
int /*<<< orphan*/ errno;
int setrlimit(int rltype, const struct rlimit *rlimitp) {
  if (rlimitp->rlim_cur > rlimitp->rlim_max || rlimitp == NULL) {
    errno = EINVAL;
    return -1;
  }
  switch (rltype) {
  case RLIMIT_CPU:
  case RLIMIT_FSIZE:
  case RLIMIT_DATA:
  case RLIMIT_STACK:
  case RLIMIT_CORE:
  case RLIMIT_RSS:
  case RLIMIT_MEMLOCK:
  case RLIMIT_NPROC:
  case RLIMIT_NOFILE:
    errno = EPERM;
    return -1;
  default:
    errno = EINVAL;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<setrlimit>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x10(%rbp),%rcx
cmp    0x8(%rcx),%rax
jg     112b <setrlimit+0x2b>
cmpq   $0x0,-0x10(%rbp)
jne    1149 <setrlimit+0x49>
mov    0x2ea6(%rip),%rax
mov    (%rax),%ecx
mov    0x2ea5(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1194 <setrlimit+0x94>
mov    -0x8(%rbp),%eax
add    $0xffffff80,%eax
sub    $0x8,%eax
ja     117b <setrlimit+0x7b>
jmp    115d <setrlimit+0x5d>
mov    0x2e64(%rip),%rax
mov    (%rax),%ecx
mov    0x2e73(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1194 <setrlimit+0x94>
mov    0x2e56(%rip),%rax
mov    (%rax),%ecx
mov    0x2e55(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<setrlimit>:
mov    (%rsi),%rax
add    $0xffffff80,%edi
mov    0x2ecb(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,%edx
cmp    $0x9,%edi
jae    111f <setrlimit+0x1f>
mov    0x2eab(%rip),%rdx
mov    (%rdx),%edx
cmp    0x8(%rsi),%rax
cmovg  %ecx,%edx
test   %rsi,%rsi
cmove  %ecx,%edx
mov    0x2ead(%rip),%rax
mov    %edx,(%rax)
mov    $0xffffffff,%eax
ret

LLVM IR (-O0):
%struct.rlimit = type { i64, i64 }
@EINVAL = global i32 0
@errno = global i32 0
@EPERM = global i32 0
define i32 @setrlimit(i32 %0, %struct.rlimit* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.rlimit*
store i32 %0, i32* %4
store %struct.rlimit* %1, %struct.rlimit** %5
%6 = load %struct.rlimit*, %struct.rlimit** %5
%7 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 0
%8 = load i64, i64* %7
%9 = load %struct.rlimit*, %struct.rlimit** %5
%10 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %9, i32 0, i32 1
%11 = load i64, i64* %10
%12 = icmp sgt i64 %8, %11
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load %struct.rlimit*, %struct.rlimit** %5
%15 = icmp eq %struct.rlimit* %14, null
br i1 %15, label %16, label %18
16: ; preds = %13, %2
%17 = load i32, i32* @EINVAL
store i32 %17, i32* @errno
store i32 -1, i32* %3
br label %24
18: ; preds = %13
%19 = load i32, i32* %4
switch i32 %19, label %22 [
i32 135, label %20
i32 133, label %20
i32 134, label %20
i32 128, label %20
i32 136, label %20
i32 129, label %20
i32 132, label %20
i32 130, label %20
i32 131, label %20
]
20: ; preds = %18, %18, %18, %18, %18, %18, %18, %18, %18
%21 = load i32, i32* @EPERM
store i32 %21, i32* @errno
store i32 -1, i32* %3
br label %24
22: ; preds = %18
%23 = load i32, i32* @EINVAL
store i32 %23, i32* @errno
store i32 -1, i32* %3
br label %24
24: ; preds = %22, %20, %16
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
%struct.rlimit = type { i64, i64 }
@EINVAL = global i32 0
@errno = global i32 0
@EPERM = global i32 0
define i32 @setrlimit(i32 %0, %struct.rlimit* %1) {
%3 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 0
%4 = load i64, i64* %3
%5 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 1
%6 = load i64, i64* %5
%7 = icmp sgt i64 %4, %6
%8 = icmp eq %struct.rlimit* %1, null
%9 = or i1 %8, %7
%10 = add i32 %0, -128
%11 = icmp ult i32 %10, 9
%12 = load i32, i32* @EINVAL
%13 = load i32, i32* @EPERM
%14 = select i1 %11, i32 %13, i32 %12
%15 = select i1 %9, i32 %12, i32 %14
store i32 %15, i32* @errno
ret i32 -1
}