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

/* Variables and functions */
int /*<<< orphan*/ die(char *, int /*<<< orphan*/, long, ...);
int /*<<< orphan*/ errno;
scalar_t__ getrlimit(int, struct rlimit *);
int /*<<< orphan*/ limit_name(int);
scalar_t__ setrlimit(int, struct rlimit *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void safe_setrlimit(int what, long value) {
  struct rlimit r;
  if (getrlimit(what, &r) < 0)
    die("getrlimit (%s) failed: %s\n", limit_name(what), (strerror(errno)));
  if (value < 0 || (unsigned long)value > r.rlim_max)
    r.rlim_cur = r.rlim_max;
  else
    r.rlim_cur = value;
  if (setrlimit(what, &r) < 0)
    die("setrlimit (%s=%li) failed: %s\n", limit_name(what), value,
        (strerror(errno)));
}


x86 assembly (clang, -O0):
<safe_setrlimit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <getrlimit@plt>
cmp    $0x0,%rax
jge    11a2 <safe_setrlimit+0x52>
mov    -0x4(%rbp),%edi
call   1040 <limit_name@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x24(%rbp),%esi
movslq %eax,%rdx
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1050 <die@plt>
cmpq   $0x0,-0x10(%rbp)
jl     11bb <safe_setrlimit+0x6b>
mov    -0x10(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    11c8 <safe_setrlimit+0x78>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    11d0 <safe_setrlimit+0x80>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1030 <setrlimit@plt>
cmp    $0x0,%rax
jge    121e <safe_setrlimit+0xce>
mov    -0x4(%rbp),%edi
call   1040 <limit_name@plt>
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%esi
mov    -0x30(%rbp),%rdx
mov    %eax,%ecx
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1050 <die@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<safe_setrlimit>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%rsi
call   1060 <getrlimit@plt>
test   %rax,%rax
jns    1196 <safe_setrlimit+0x46>
mov    %ebp,%edi
call   1040 <limit_name@plt>
mov    %eax,%r14d
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
movslq %eax,%rdx
lea    0xe74(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1050 <die@plt>
mov    (%rsp),%rax
cmp    %rbx,%rax
mov    %rbx,%rcx
cmovb  %rax,%rcx
test   %rbx,%rbx
cmovs  %rax,%rcx
mov    %rcx,0x8(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
call   1030 <setrlimit@plt>
test   %rax,%rax
jns    11eb <safe_setrlimit+0x9b>
mov    %ebp,%edi
call   1040 <limit_name@plt>
mov    %eax,%ebp
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe3e(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <die@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.rlimit = type { i64, i64 }
@.str = [27 x i8] c"getrlimit (%s) failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [31 x i8] c"setrlimit (%s=%li) failed: %s\0A\00"
define void @safe_setrlimit(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca %struct.rlimit
store i32 %0, i32* %3
store i64 %1, i64* %4
%6 = load i32, i32* %3
%7 = call i64 @getrlimit(i32 %6, %struct.rlimit* %5)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %2
%10 = load i32, i32* %3
%11 = call i32 @limit_name(i32 %10)
%12 = load i32, i32* @errno
%13 = call i32 @strerror(i32 %12)
%14 = sext i32 %13 to i64
%15 = call i32 (i8*, i32, i64, ...) @die(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %11, i64 %14)
br label %16
16: ; preds = %9, %2
%17 = load i64, i64* %4
%18 = icmp slt i64 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %16
%20 = load i64, i64* %4
%21 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %5, i32 0, i32 0
%22 = load i64, i64* %21
%23 = icmp ugt i64 %20, %22
br i1 %23, label %24, label %28
24: ; preds = %19, %16
%25 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %5, i32 0, i32 0
%26 = load i64, i64* %25
%27 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %5, i32 0, i32 1
store i64 %26, i64* %27
br label %31
28: ; preds = %19
%29 = load i64, i64* %4
%30 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %5, i32 0, i32 1
store i64 %29, i64* %30
br label %31
31: ; preds = %28, %24
%32 = load i32, i32* %3
%33 = call i64 @setrlimit(i32 %32, %struct.rlimit* %5)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %31
%36 = load i32, i32* %3
%37 = call i32 @limit_name(i32 %36)
%38 = load i64, i64* %4
%39 = load i32, i32* @errno
%40 = call i32 @strerror(i32 %39)
%41 = call i32 (i8*, i32, i64, ...) @die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %37, i64 %38, i32 %40)
br label %42
42: ; preds = %35, %31
ret void
}

LLVM IR (-O2):
%struct.rlimit = type { i64, i64 }
@.str = [27 x i8] c"getrlimit (%s) failed: %s\0A\00"
@errno = global i32 0
@.str.1 = [31 x i8] c"setrlimit (%s=%li) failed: %s\0A\00"
define void @safe_setrlimit(i32 %0, i64 %1) {
%3 = alloca %struct.rlimit
%4 = bitcast %struct.rlimit* %3 to i8*
%5 = call i64 @getrlimit(i32 %0, %struct.rlimit* %3)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %2
%8 = call i32 @limit_name(i32 %0)
%9 = load i32, i32* @errno
%10 = call i32 @strerror(i32 %9)
%11 = sext i32 %10 to i64
%12 = call i32 (i8*, i32, i64, ...) @die(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %8, i64 %11)
br label %13
13: ; preds = %7, %2
%14 = icmp slt i64 %1, 0
%15 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 0
%16 = load i64, i64* %15
%17 = icmp ult i64 %16, %1
%18 = select i1 %14, i1 true, i1 %17
%19 = select i1 %18, i64 %16, i64 %1
%20 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 1
store i64 %19, i64* %20
%21 = call i64 @setrlimit(i32 %0, %struct.rlimit* %3)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %13
%24 = call i32 @limit_name(i32 %0)
%25 = load i32, i32* @errno
%26 = call i32 @strerror(i32 %25)
%27 = call i32 (i8*, i32, i64, ...) @die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i32 %24, i64 %1, i32 %26)
br label %28
28: ; preds = %23, %13
ret void
}