#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, long, long, ...);
int getrlimit(int, struct rlimit *);
int setrlimit(int, struct rlimit *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int setrlimiter(char *name, int rlimit, int nset) {
  int ret = -1;
  struct rlimit rlim;
  if (name) {
    if (getrlimit(rlimit, &rlim) == -1)
      return -1;
    else {
      fprintf(stdout, "getrlimit %s cur[%ld] max[%ld]\n", name,
              (long)rlim.rlim_cur, (long)rlim.rlim_max);
    }
    if (rlim.rlim_cur > nset && rlim.rlim_max > nset)
      return 0;
    rlim.rlim_cur = nset;
    rlim.rlim_max = nset;
    if ((ret = setrlimit(rlimit, &rlim)) == 0) {
      fprintf(stdout, "setrlimit %s cur[%ld] max[%ld]\n", name,
              (long)rlim.rlim_cur, (long)rlim.rlim_max);
      return 0;
    } else {
      fprintf(stderr, "setrlimit %s cur[%ld] max[%ld] failed, %s\n", name,
              (long)rlim.rlim_cur, (long)rlim.rlim_max, strerror(errno));
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<setrlimiter>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1276 <setrlimiter+0x136>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1050 <getrlimit@plt>
cmp    $0xffffffff,%eax
jne    1185 <setrlimiter+0x45>
movl   $0xffffffff,-0x4(%rbp)
jmp    127c <setrlimiter+0x13c>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
movslq -0x28(%rbp),%rcx
movslq -0x24(%rbp),%r8
lea    0xe5f(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x28(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    11cc <setrlimiter+0x8c>
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    11cc <setrlimiter+0x8c>
movl   $0x0,-0x4(%rbp)
jmp    127c <setrlimiter+0x13c>
mov    -0x18(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1040 <setrlimit@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    121f <setrlimiter+0xdf>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
movslq -0x28(%rbp),%rcx
movslq -0x24(%rbp),%r8
lea    0xe14(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    127c <setrlimiter+0x13c>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
movslq -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
movslq -0x24(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    -0x30(%rbp),%r8
mov    %eax,%r9d
lea    0xdd6(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    1276 <setrlimiter+0x136>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setrlimiter>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     1227 <setrlimiter+0xe7>
mov    %edx,%ebx
mov    %esi,%r15d
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
call   1050 <getrlimit@plt>
cmp    $0xffffffff,%eax
je     1227 <setrlimiter+0xe7>
mov    0x2e51(%rip),%r12
mov    (%r12),%edi
movslq 0x8(%rsp),%rcx
movslq 0xc(%rsp),%r8
lea    0xe6c(%rip),%rsi
xor    %ebp,%ebp
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
cmp    %ebx,0x8(%rsp)
jle    11ac <setrlimiter+0x6c>
cmp    %ebx,0xc(%rsp)
jg     1227 <setrlimiter+0xe7>
mov    %ebx,0x8(%rsp)
mov    %ebx,0xc(%rsp)
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
call   1040 <setrlimit@plt>
test   %eax,%eax
je     1206 <setrlimiter+0xc6>
mov    %eax,%ebp
mov    0x2e12(%rip),%rax
mov    (%rax),%ebx
movslq 0x8(%rsp),%r15
movslq 0xc(%rsp),%r12
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe51(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %r12,%r8
mov    %eax,%r9d
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    1227 <setrlimiter+0xe7>
mov    (%r12),%edi
movslq 0x8(%rsp),%rcx
movslq 0xc(%rsp),%r8
lea    0xe05(%rip),%rsi
xor    %ebp,%ebp
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
@stdout = global i32 0
@.str = [32 x i8] c"getrlimit %s cur[%ld] max[%ld]\0A\00"
@.str.1 = [32 x i8] c"setrlimit %s cur[%ld] max[%ld]\0A\00"
@stderr = global i32 0
@.str.2 = [43 x i8] c"setrlimit %s cur[%ld] max[%ld] failed, %s\0A\00"
@errno = global i32 0
define i32 @setrlimiter(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.rlimit
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 -1, i32* %8
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %69
12: ; preds = %3
%13 = load i32, i32* %6
%14 = call i32 @getrlimit(i32 %13, %struct.rlimit* %9)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 -1, i32* %4
br label %71
17: ; preds = %12
%18 = load i32, i32* @stdout
%19 = load i8*, i8** %5
%20 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %9, i32 0, i32 0
%21 = load i32, i32* %20
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %9, i32 0, i32 1
%24 = load i32, i32* %23
%25 = sext i32 %24 to i64
%26 = call i32 (i32, i8*, i8*, i64, i64, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %19, i64 %22, i64 %25)
br label %27
27: ; preds = %17
%28 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %9, i32 0, i32 0
%29 = load i32, i32* %28
%30 = load i32, i32* %7
%31 = icmp sgt i32 %29, %30
br i1 %31, label %32, label %38
32: ; preds = %27
%33 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %9, i32 0, i32 1
%34 = load i32, i32* %33
%35 = load i32, i32* %7
%36 = icmp sgt i32 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %32
store i32 0, i32* %4
br label %71
38: ; preds = %32, %27
%39 = load i32, i32* %7
%40 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %9, i32 0, i32 0
store i32 %39, i32* %40
%41 = load i32, i32* %7
%42 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %9, i32 0, i32 1
store i32 %41, i32* %42
%43 = load i32, i32* %6
%44 = call i32 @setrlimit(i32 %43, %struct.rlimit* %9)
store i32 %44, i32* %8
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %56
46: ; preds = %38
%47 = load i32, i32* @stdout
%48 = load i8*, i8** %5
%49 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %9, i32 0, i32 0
%50 = load i32, i32* %49
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %9, i32 0, i32 1
%53 = load i32, i32* %52
%54 = sext i32 %53 to i64
%55 = call i32 (i32, i8*, i8*, i64, i64, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %48, i64 %51, i64 %54)
store i32 0, i32* %4
br label %71
56: ; preds = %38
%57 = load i32, i32* @stderr
%58 = load i8*, i8** %5
%59 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %9, i32 0, i32 0
%60 = load i32, i32* %59
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %9, i32 0, i32 1
%63 = load i32, i32* %62
%64 = sext i32 %63 to i64
%65 = load i32, i32* @errno
%66 = call i32 @strerror(i32 %65)
%67 = call i32 (i32, i8*, i8*, i64, i64, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %58, i64 %61, i64 %64, i32 %66)
br label %68
68: ; preds = %56
br label %69
69: ; preds = %68, %3
%70 = load i32, i32* %8
store i32 %70, i32* %4
br label %71
71: ; preds = %69, %46, %37, %16
%72 = load i32, i32* %4
ret i32 %72
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32 }
@stdout = global i32 0
@.str = [32 x i8] c"getrlimit %s cur[%ld] max[%ld]\0A\00"
@.str.1 = [32 x i8] c"setrlimit %s cur[%ld] max[%ld]\0A\00"
@stderr = global i32 0
@.str.2 = [43 x i8] c"setrlimit %s cur[%ld] max[%ld] failed, %s\0A\00"
@errno = global i32 0
define i32 @setrlimiter(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.rlimit
%5 = bitcast %struct.rlimit* %4 to i8*
%6 = icmp eq i8* %0, null
br i1 %6, label %43, label %7
7: ; preds = %3
%8 = call i32 @getrlimit(i32 %1, %struct.rlimit* %4)
%9 = icmp eq i32 %8, -1
br i1 %9, label %43, label %10
10: ; preds = %7
%11 = load i32, i32* @stdout
%12 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i64 0, i32 0
%13 = load i32, i32* %12
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i64 0, i32 1
%16 = load i32, i32* %15
%17 = sext i32 %16 to i64
%18 = call i32 (i32, i8*, i8*, i64, i64, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %14, i64 %17)
%19 = load i32, i32* %12
%20 = icmp sgt i32 %19, %2
%21 = load i32, i32* %15
%22 = icmp sgt i32 %21, %2
%23 = select i1 %20, i1 %22, i1 false
br i1 %23, label %43, label %24
24: ; preds = %10
store i32 %2, i32* %12
store i32 %2, i32* %15
%25 = call i32 @setrlimit(i32 %1, %struct.rlimit* %4)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %24
%28 = load i32, i32* @stdout
%29 = load i32, i32* %12
%30 = sext i32 %29 to i64
%31 = load i32, i32* %15
%32 = sext i32 %31 to i64
%33 = call i32 (i32, i8*, i8*, i64, i64, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0, i64 %30, i64 %32)
br label %43
34: ; preds = %24
%35 = load i32, i32* @stderr
%36 = load i32, i32* %12
%37 = sext i32 %36 to i64
%38 = load i32, i32* %15
%39 = sext i32 %38 to i64
%40 = load i32, i32* @errno
%41 = call i32 @strerror(i32 %40)
%42 = call i32 (i32, i8*, i8*, i64, i64, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %0, i64 %37, i64 %39, i32 %41)
br label %43
43: ; preds = %3, %34, %10, %7, %27
%44 = phi i32 [ 0, %27 ], [ -1, %7 ], [ 0, %10 ], [ %25, %34 ], [ -1, %3 ]
ret i32 %44
}