#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 {
  void *rlim_max;
  void *rlim_cur;
};
typedef void *rlim_t;

/* Variables and functions */
int /*<<< orphan*/ getrlimit(int, struct rlimit *);
int /*<<< orphan*/ log_stdout(char *, char *, void *, ...);
int /*<<< orphan*/ perror(char *);
int setrlimit(int, struct rlimit *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
void set_one_limit(char *name, int resource, rlim_t val) {
  struct rlimit limstruct;
  char *lim_name = &name[5];
  log_stdout("4010 Setting %s to %lld\n", lim_name, val);
  getrlimit(resource, &limstruct);
  log_stdout("4020 Before: %s soft[%lld] hard[%lld]\n", lim_name,
             limstruct.rlim_cur, limstruct.rlim_max);
  char buf[(4096)];
  sprintf(buf, "4030 %s limit was not set.", name);
  limstruct.rlim_cur = val;
  limstruct.rlim_max = val;
  int rc = setrlimit(resource, &limstruct);
  if (rc != 0) {
    perror(buf);
    return;
  }
  getrlimit(resource, &limstruct);
  log_stdout("4040 After: %s soft[%lld] hard[%lld]\n", lim_name,
             limstruct.rlim_cur, limstruct.rlim_max);
}


x86 assembly (clang, -O0):
<set_one_limit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1040 <log_stdout@plt>
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1070 <getrlimit@plt>
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xe72(%rip),%rdi
mov    $0x0,%al
call   1040 <log_stdout@plt>
lea    -0x1030(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe80(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1030 <setrlimit@plt>
mov    %eax,-0x1034(%rbp)
cmpl   $0x0,-0x1034(%rbp)
je     1205 <set_one_limit+0xb5>
lea    -0x1030(%rbp),%rdi
call   1050 <perror@plt>
jmp    122b <set_one_limit+0xdb>
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1070 <getrlimit@plt>
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1040 <log_stdout@plt>
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_one_limit>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1010,%rsp
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %rdi,%r15
lea    0x5(%rdi),%r14
lea    0xe8e(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <log_stdout@plt>
mov    %rsp,%r12
mov    %ebp,%edi
mov    %r12,%rsi
call   1070 <getrlimit@plt>
mov    (%rsp),%rcx
mov    0x8(%rsp),%rdx
lea    0xe80(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <log_stdout@plt>
lea    0xe96(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %r15,%rdx
call   1060 <sprintf@plt>
mov    %rbx,0x8(%rsp)
mov    %rbx,(%rsp)
mov    %ebp,%edi
mov    %r12,%rsi
call   1030 <setrlimit@plt>
test   %eax,%eax
jne    1202 <set_one_limit+0xb2>
mov    %rsp,%rsi
mov    %ebp,%edi
call   1070 <getrlimit@plt>
mov    (%rsp),%rcx
mov    0x8(%rsp),%rdx
lea    0xe73(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <log_stdout@plt>
add    $0x1010,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x10(%rsp),%rdi
call   1050 <perror@plt>
jmp    11f2 <set_one_limit+0xa2>

LLVM IR (-O0):
%struct.rlimit = type { i8*, i8* }
@.str = [25 x i8] c"4010 Setting %s to %lld\0A\00"
@.str.1 = [39 x i8] c"4020 Before: %s soft[%lld] hard[%lld]\0A\00"
@.str.2 = [27 x i8] c"4030 %s limit was not set.\00"
@.str.3 = [38 x i8] c"4040 After: %s soft[%lld] hard[%lld]\0A\00"
define void @set_one_limit(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.rlimit
%8 = alloca i8*
%9 = alloca [4096 x i8]
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%11 = load i8*, i8** %4
%12 = getelementptr inbounds i8, i8* %11, i64 5
store i8* %12, i8** %8
%13 = load i8*, i8** %8
%14 = load i8*, i8** %6
%15 = call i32 (i8*, i8*, i8*, ...) @log_stdout(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %14)
%16 = load i32, i32* %5
%17 = call i32 @getrlimit(i32 %16, %struct.rlimit* %7)
%18 = load i8*, i8** %8
%19 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 1
%20 = load i8*, i8** %19
%21 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 0
%22 = load i8*, i8** %21
%23 = call i32 (i8*, i8*, i8*, ...) @log_stdout(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %18, i8* %20, i8* %22)
%24 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%25 = load i8*, i8** %4
%26 = call i32 @sprintf(i8* %24, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = load i8*, i8** %6
%28 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 1
store i8* %27, i8** %28
%29 = load i8*, i8** %6
%30 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 0
store i8* %29, i8** %30
%31 = load i32, i32* %5
%32 = call i32 @setrlimit(i32 %31, %struct.rlimit* %7)
store i32 %32, i32* %10
%33 = load i32, i32* %10
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %3
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%37 = call i32 @perror(i8* %36)
br label %47
38: ; preds = %3
%39 = load i32, i32* %5
%40 = call i32 @getrlimit(i32 %39, %struct.rlimit* %7)
%41 = load i8*, i8** %8
%42 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 1
%43 = load i8*, i8** %42
%44 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 0
%45 = load i8*, i8** %44
%46 = call i32 (i8*, i8*, i8*, ...) @log_stdout(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %41, i8* %43, i8* %45)
br label %47
47: ; preds = %38, %35
ret void
}

LLVM IR (-O2):
%struct.rlimit = type { i8*, i8* }
@.str = [25 x i8] c"4010 Setting %s to %lld\0A\00"
@.str.1 = [39 x i8] c"4020 Before: %s soft[%lld] hard[%lld]\0A\00"
@.str.2 = [27 x i8] c"4030 %s limit was not set.\00"
@.str.3 = [38 x i8] c"4040 After: %s soft[%lld] hard[%lld]\0A\00"
define void @set_one_limit(i8* %0, i32 %1, i8* %2) {
%4 = alloca %struct.rlimit
%5 = alloca [4096 x i8]
%6 = bitcast %struct.rlimit* %4 to i8*
%7 = getelementptr inbounds i8, i8* %0, i64 5
%8 = tail call i32 (i8*, i8*, i8*, ...) @log_stdout(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %7, i8* %2)
%9 = call i32 @getrlimit(i32 %1, %struct.rlimit* %4)
%10 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i64 0, i32 1
%11 = load i8*, i8** %10
%12 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i64 0, i32 0
%13 = load i8*, i8** %12
%14 = call i32 (i8*, i8*, i8*, ...) @log_stdout(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %7, i8* %11, i8* %13)
%15 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%16 = call i32 @sprintf(i8* %15, i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %0)
store i8* %2, i8** %10
store i8* %2, i8** %12
%17 = call i32 @setrlimit(i32 %1, %struct.rlimit* %4)
%18 = icmp eq i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %3
%20 = call i32 @perror(i8* %15)
br label %26
21: ; preds = %3
%22 = call i32 @getrlimit(i32 %1, %struct.rlimit* %4)
%23 = load i8*, i8** %10
%24 = load i8*, i8** %12
%25 = call i32 (i8*, i8*, i8*, ...) @log_stdout(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i8* %7, i8* %23, i8* %24)
br label %26
26: ; preds = %21, %19
ret void
}