#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 */
typedef struct rlimit {
  size_t rlim_cur;
  int /*<<< orphan*/ rlim_max;
} const rlimit;

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_AS;
int /*<<< orphan*/ getrlimit(int /*<<< orphan*/, struct rlimit const *);
scalar_t__ setrlimit(int /*<<< orphan*/, struct rlimit const *);
const struct rlimit *setup_rlimit(size_t lim, struct rlimit *_new_lim) {
  static struct rlimit old_lim;
  getrlimit(RLIMIT_AS, &old_lim);
  struct rlimit new_lim = {
      .rlim_cur = lim,
      .rlim_max = old_lim.rlim_max,
  };
  if (setrlimit(RLIMIT_AS, &new_lim))
    return NULL;
  if (_new_lim)
    *_new_lim = new_lim;
  return &old_lim;
}


x86 assembly (clang, -O0):
<setup_rlimit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea9(%rip),%rax
mov    (%rax),%edi
lea    0x2ef8(%rip),%rsi
call   1040 <getrlimit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2eed(%rip),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1030 <setrlimit@plt>
cmp    $0x0,%rax
je     117f <setup_rlimit+0x5f>
movq   $0x0,-0x8(%rbp)
jmp    11a8 <setup_rlimit+0x88>
cmpq   $0x0,-0x18(%rbp)
je     119d <setup_rlimit+0x7d>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rcx
mov    %rcx,0x8(%rax)
lea    0x2e94(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_rlimit>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2ea8(%rip),%r12
mov    (%r12),%edi
lea    0x2ef5(%rip),%rbx
mov    %rbx,%rsi
call   1040 <getrlimit@plt>
mov    %r15,0x8(%rsp)
mov    0x2eea(%rip),%eax
mov    %eax,0x10(%rsp)
mov    (%r12),%edi
lea    0x8(%rsp),%rsi
call   1030 <setrlimit@plt>
test   %rax,%rax
je     1171 <setup_rlimit+0x51>
xor    %ebx,%ebx
jmp    117f <setup_rlimit+0x5f>
test   %r14,%r14
je     117f <setup_rlimit+0x5f>
movups 0x8(%rsp),%xmm0
movups %xmm0,(%r14)
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.rlimit = type { i64, i32 }
@setup_rlimit.old_lim = internal global %struct.rlimit zeroinitializer
@RLIMIT_AS = global i32 0
define %struct.rlimit* @setup_rlimit(i64 %0, %struct.rlimit* %1) {
%3 = alloca %struct.rlimit*
%4 = alloca i64
%5 = alloca %struct.rlimit*
%6 = alloca %struct.rlimit
store i64 %0, i64* %4
store %struct.rlimit* %1, %struct.rlimit** %5
%7 = load i32, i32* @RLIMIT_AS
%8 = call i32 @getrlimit(i32 %7, %struct.rlimit* @setup_rlimit.old_lim)
%9 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 0
%10 = load i64, i64* %4
store i64 %10, i64* %9
%11 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 1
%12 = load i32, i32* getelementptr inbounds (%struct.rlimit, %struct.rlimit* @setup_rlimit.old_lim, i32 0, i32 1)
store i32 %12, i32* %11
%13 = load i32, i32* @RLIMIT_AS
%14 = call i64 @setrlimit(i32 %13, %struct.rlimit* %6)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store %struct.rlimit* null, %struct.rlimit** %3
br label %25
17: ; preds = %2
%18 = load %struct.rlimit*, %struct.rlimit** %5
%19 = icmp ne %struct.rlimit* %18, null
br i1 %19, label %20, label %24
20: ; preds = %17
%21 = load %struct.rlimit*, %struct.rlimit** %5
%22 = bitcast %struct.rlimit* %21 to i8*
%23 = bitcast %struct.rlimit* %6 to i8*
br label %24
24: ; preds = %20, %17
store %struct.rlimit* @setup_rlimit.old_lim, %struct.rlimit** %3
br label %25
25: ; preds = %24, %16
%26 = load %struct.rlimit*, %struct.rlimit** %3
ret %struct.rlimit* %26
}

LLVM IR (-O2):
%struct.rlimit = type { i64, i32 }
@setup_rlimit.old_lim = internal global %struct.rlimit zeroinitializer
@RLIMIT_AS = global i32 0
define %struct.rlimit* @setup_rlimit(i64 %0, %struct.rlimit* writeonly %1) {
%3 = alloca %struct.rlimit
%4 = load i32, i32* @RLIMIT_AS
%5 = tail call i32 @getrlimit(i32 %4, %struct.rlimit* @setup_rlimit.old_lim)
%6 = bitcast %struct.rlimit* %3 to i8*
%7 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 0
store i64 %0, i64* %7
%8 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 1
%9 = load i32, i32* getelementptr inbounds (%struct.rlimit, %struct.rlimit* @setup_rlimit.old_lim, i64 0, i32 1)
store i32 %9, i32* %8
%10 = load i32, i32* @RLIMIT_AS
%11 = call i64 @setrlimit(i32 %10, %struct.rlimit* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = icmp eq %struct.rlimit* %1, null
br i1 %14, label %17, label %15
15: ; preds = %13
%16 = bitcast %struct.rlimit* %1 to i8*
br label %17
17: ; preds = %13, %15, %2
%18 = phi %struct.rlimit* [ null, %2 ], [ @setup_rlimit.old_lim, %15 ], [ @setup_rlimit.old_lim, %13 ]
ret %struct.rlimit* %18
}