#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_cur;
};

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_AS;
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ setrlimit(int /*<<< orphan*/, struct rlimit *);
int limit_ram_usage(const unsigned long max_ram) {
  struct rlimit rl;
  if (getrlimit(RLIMIT_AS, &rl) == 0) {
    printf("\x1B[32m"
           "Current limit : %.3f\n"
           "\033[0m",
           (float)rl.rlim_cur / 1000000);
    rl.rlim_cur = max_ram;
    if (setrlimit(RLIMIT_AS, &rl) == 0) {
      printf("\x1B[32m"
             "RAM limit changes\n"
             "\033[0m");
      getrlimit(RLIMIT_AS, &rl);
      printf("\x1B[32m"
             "Current limit : %.3f\n"
             "\033[0m",
             (float)rl.rlim_cur / 1000000);
      return (rl.rlim_cur == max_ram) ? 0 : 1;
    } else {
      printf("\x1B[31m"
             "Cannot set current limit\n"
             "\033[0m");
      return 1;
    }
  } else {
    printf("\x1B[31m"
           "Cannot get current limit\n"
           "\033[0m");
    return 1;
  }
}


x86 assembly (clang, -O0):
<limit_ram_usage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1050 <getrlimit@plt>
cmp    $0x0,%rax
jne    1293 <limit_ram_usage+0x163>
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
shr    %rdx
mov    %eax,%ecx
and    $0x1,%ecx
or     %rdx,%rcx
cvtsi2ss %rcx,%xmm0
addss  %xmm0,%xmm0
cvtsi2ss %rax,%xmm1
movss  %xmm1,-0x20(%rbp)
test   %rax,%rax
movss  %xmm0,-0x1c(%rbp)
js     1195 <limit_ram_usage+0x65>
movss  -0x20(%rbp),%xmm0
movss  %xmm0,-0x1c(%rbp)
movss  -0x1c(%rbp),%xmm0
movss  0xe5e(%rip),%xmm1
divss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xe53(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1040 <setrlimit@plt>
cmp    $0x0,%rax
jne    1279 <limit_ram_usage+0x149>
lea    0xe40(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1050 <getrlimit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
shr    %rdx
mov    %eax,%ecx
and    $0x1,%ecx
or     %rdx,%rcx
cvtsi2ss %rcx,%xmm0
addss  %xmm0,%xmm0
cvtsi2ss %rax,%xmm1
movss  %xmm1,-0x28(%rbp)
test   %rax,%rax
movss  %xmm0,-0x24(%rbp)
js     1239 <limit_ram_usage+0x109>
movss  -0x28(%rbp),%xmm0
movss  %xmm0,-0x24(%rbp)
movss  -0x24(%rbp),%xmm0
movss  0xdba(%rip),%xmm1
divss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xdaf(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rsi
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    %rsi,%rdx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    12a8 <limit_ram_usage+0x178>
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a8 <limit_ram_usage+0x178>
lea    0xdc8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<limit_ram_usage>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2ea2(%rip),%rbx
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1050 <getrlimit@plt>
test   %rax,%rax
je     1156 <limit_ram_usage+0x26>
lea    0xf0e(%rip),%rdi
jmp    11af <limit_ram_usage+0x7f>
mov    (%rsp),%rax
test   %rax,%rax
js     1166 <limit_ram_usage+0x36>
cvtsi2ss %rax,%xmm0
jmp    117b <limit_ram_usage+0x4b>
mov    %rax,%rcx
shr    %rcx
and    $0x1,%eax
or     %rcx,%rax
cvtsi2ss %rax,%xmm0
addss  %xmm0,%xmm0
divss  0xe7d(%rip),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xe76(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
mov    %r14,(%rsp)
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1040 <setrlimit@plt>
test   %rax,%rax
je     11c3 <limit_ram_usage+0x93>
lea    0xe90(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe59(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1050 <getrlimit@plt>
mov    (%rsp),%rax
test   %rax,%rax
js     11ee <limit_ram_usage+0xbe>
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
jmp    1206 <limit_ram_usage+0xd6>
mov    %rax,%rcx
shr    %rcx
and    $0x1,%eax
or     %rcx,%rax
xorps  %xmm0,%xmm0
cvtsi2ss %rax,%xmm0
addss  %xmm0,%xmm0
divss  0xdf2(%rip),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xdeb(%rip),%rdi
mov    $0x1,%al
call   1030 <printf@plt>
xor    %eax,%eax
cmp    %r14,(%rsp)
setne  %al
jmp    11bb <limit_ram_usage+0x8b>

LLVM IR (-O0):
%struct.rlimit = type { i64 }
@RLIMIT_AS = global i32 0
@.str = [31 x i8] c"\1B[32mCurrent limit : %.3f\0A\1B[0m\00"
@.str.1 = [28 x i8] c"\1B[32mRAM limit changes\0A\1B[0m\00"
@.str.2 = [35 x i8] c"\1B[31mCannot set current limit\0A\1B[0m\00"
@.str.3 = [35 x i8] c"\1B[31mCannot get current limit\0A\1B[0m\00"
define i32 @limit_ram_usage(i64 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca %struct.rlimit
store i64 %0, i64* %3
%5 = load i32, i32* @RLIMIT_AS
%6 = call i64 @getrlimit(i32 %5, %struct.rlimit* %4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %38
8: ; preds = %1
%9 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
%10 = load i64, i64* %9
%11 = uitofp i64 %10 to float
%12 = fdiv float %11, 1.000000e+06
%13 = fpext float %12 to double
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), double %13)
%15 = load i64, i64* %3
%16 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
store i64 %15, i64* %16
%17 = load i32, i32* @RLIMIT_AS
%18 = call i64 @setrlimit(i32 %17, %struct.rlimit* %4)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %36
20: ; preds = %8
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @RLIMIT_AS
%23 = call i64 @getrlimit(i32 %22, %struct.rlimit* %4)
%24 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
%25 = load i64, i64* %24
%26 = uitofp i64 %25 to float
%27 = fdiv float %26, 1.000000e+06
%28 = fpext float %27 to double
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), double %28)
%30 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i32 0, i32 0
%31 = load i64, i64* %30
%32 = load i64, i64* %3
%33 = icmp eq i64 %31, %32
%34 = zext i1 %33 to i64
%35 = select i1 %33, i32 0, i32 1
store i32 %35, i32* %2
br label %40
36: ; preds = %8
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
br label %40
38: ; preds = %1
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %2
br label %40
40: ; preds = %38, %36, %20
%41 = load i32, i32* %2
ret i32 %41
}

LLVM IR (-O2):
%struct.rlimit = type { i64 }
@RLIMIT_AS = global i32 0
@.str = [31 x i8] c"\1B[32mCurrent limit : %.3f\0A\1B[0m\00"
@.str.1 = [28 x i8] c"\1B[32mRAM limit changes\0A\1B[0m\00"
@.str.2 = [35 x i8] c"\1B[31mCannot set current limit\0A\1B[0m\00"
@.str.3 = [35 x i8] c"\1B[31mCannot get current limit\0A\1B[0m\00"
define i32 @limit_ram_usage(i64 %0) {
%2 = alloca %struct.rlimit
%3 = bitcast %struct.rlimit* %2 to i8*
%4 = load i32, i32* @RLIMIT_AS
%5 = call i64 @getrlimit(i32 %4, %struct.rlimit* %2)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %31
7: ; preds = %1
%8 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 0
%9 = load i64, i64* %8
%10 = uitofp i64 %9 to float
%11 = fdiv float %10, 1.000000e+06
%12 = fpext float %11 to double
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), double %12)
store i64 %0, i64* %8
%14 = load i32, i32* @RLIMIT_AS
%15 = call i64 @setrlimit(i32 %14, %struct.rlimit* %2)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %29
17: ; preds = %7
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @RLIMIT_AS
%20 = call i64 @getrlimit(i32 %19, %struct.rlimit* %2)
%21 = load i64, i64* %8
%22 = uitofp i64 %21 to float
%23 = fdiv float %22, 1.000000e+06
%24 = fpext float %23 to double
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), double %24)
%26 = load i64, i64* %8
%27 = icmp ne i64 %26, %0
%28 = zext i1 %27 to i32
br label %33
29: ; preds = %7
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
br label %33
31: ; preds = %1
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %31, %29, %17
%34 = phi i32 [ %28, %17 ], [ 1, %29 ], [ 1, %31 ]
ret i32 %34
}