#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 int rlim_cur;
  unsigned int rlim_max;
};
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_MEMLOCK;
scalar_t__ atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int setrlimit(int /*<<< orphan*/, struct rlimit *);
scalar_t__ strncmp(char *, char *, int);
int mln_adjust_limit(unsigned spare) {
  FILE *fp = fopen("/proc/self/status", "r");
  char buf[256];
  unsigned VmPeak = 0;
  struct rlimit limit;
  if (!fp)
    return -1;
  while (fgets(buf, sizeof(buf), fp)) {
    if (0 == strncmp(buf, "VmPeak:", 7)) {
      VmPeak = (unsigned)atoi(buf + 8);
      break;
    }
  }
  fclose(fp);
  limit.rlim_cur = limit.rlim_max = VmPeak * 1024 + spare;
  return setrlimit(RLIMIT_MEMLOCK, &limit);
}


x86 assembly (clang, -O0):
<mln_adjust_limit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x8(%rbp)
lea    0xe8b(%rip),%rdi
lea    0xe96(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x114(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a6 <mln_adjust_limit+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    1249 <mln_adjust_limit+0xe9>
jmp    11ab <mln_adjust_limit+0x4b>
lea    -0x110(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x100,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1210 <mln_adjust_limit+0xb0>
lea    -0x110(%rbp),%rdi
lea    0xe3c(%rip),%rsi
mov    $0x7,%edx
call   1030 <strncmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    120b <mln_adjust_limit+0xab>
lea    -0x110(%rbp),%rdi
add    $0x8,%rdi
call   1080 <atoi@plt>
mov    %eax,-0x114(%rbp)
jmp    1210 <mln_adjust_limit+0xb0>
jmp    11ab <mln_adjust_limit+0x4b>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x114(%rbp),%eax
shl    $0xa,%eax
add    -0x8(%rbp),%eax
mov    %eax,-0x11c(%rbp)
mov    %eax,-0x120(%rbp)
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    -0x120(%rbp),%rsi
call   1060 <setrlimit@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mln_adjust_limit>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x118,%rsp
mov    %edi,%r14d
lea    0xe89(%rip),%rdi
lea    0xe94(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11dc <mln_adjust_limit+0x7c>
mov    %rax,%rbx
lea    0x10(%rsp),%rbp
lea    0xe7d(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11e3 <mln_adjust_limit+0x83>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0x7,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11a0 <mln_adjust_limit+0x40>
lea    0x18(%rsp),%rdi
call   1080 <atoi@plt>
mov    %rax,%rbp
shl    $0xa,%ebp
jmp    11e5 <mln_adjust_limit+0x85>
mov    $0xffffffff,%eax
jmp    120b <mln_adjust_limit+0xab>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    %r14d,%ebp
mov    %ebp,0xc(%rsp)
mov    %ebp,0x8(%rsp)
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1060 <setrlimit@plt>
add    $0x118,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
@.str = [18 x i8] c"/proc/self/status\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"VmPeak:\00"
@RLIMIT_MEMLOCK = global i32 0
define i32 @mln_adjust_limit(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32*
%5 = alloca [256 x i8]
%6 = alloca i32
%7 = alloca %struct.rlimit
store i32 %0, i32* %3
%8 = call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %4
store i32 0, i32* %6
%9 = load i32*, i32** %4
%10 = icmp ne i32* %9, null
br i1 %10, label %12, label %11
11: ; preds = %1
store i32 -1, i32* %2
br label %39
12: ; preds = %1
br label %13
13: ; preds = %27, %12
%14 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%15 = load i32*, i32** %4
%16 = call i64 @fgets(i8* %14, i32 256, i32* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %28
18: ; preds = %13
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%20 = call i64 @strncmp(i8* %19, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 7)
%21 = icmp eq i64 0, %20
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%24 = getelementptr inbounds i8, i8* %23, i64 8
%25 = call i64 @atoi(i8* %24)
%26 = trunc i64 %25 to i32
store i32 %26, i32* %6
br label %28
27: ; preds = %18
br label %13
28: ; preds = %22, %13
%29 = load i32*, i32** %4
%30 = call i32 @fclose(i32* %29)
%31 = load i32, i32* %6
%32 = mul i32 %31, 1024
%33 = load i32, i32* %3
%34 = add i32 %32, %33
%35 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 1
store i32 %34, i32* %35
%36 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %7, i32 0, i32 0
store i32 %34, i32* %36
%37 = load i32, i32* @RLIMIT_MEMLOCK
%38 = call i32 @setrlimit(i32 %37, %struct.rlimit* %7)
store i32 %38, i32* %2
br label %39
39: ; preds = %28, %11
%40 = load i32, i32* %2
ret i32 %40
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32 }
@.str = [18 x i8] c"/proc/self/status\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"VmPeak:\00"
@RLIMIT_MEMLOCK = global i32 0
define i32 @mln_adjust_limit(i32 %0) {
%2 = alloca [256 x i8]
%3 = alloca %struct.rlimit
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.rlimit* %3 to i8*
%7 = icmp eq i32* %4, null
br i1 %7, label %27, label %8
8: ; preds = %1, %11
%9 = call i64 @fgets(i8* %5, i32 256, i32* %4)
%10 = icmp eq i64 %9, 0
br i1 %10, label %19, label %11
11: ; preds = %8
%12 = call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 7)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %8
14: ; preds = %11
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 8
%16 = call i64 @atoi(i8* %15)
%17 = trunc i64 %16 to i32
%18 = shl i32 %17, 10
br label %19
19: ; preds = %8, %14
%20 = phi i32 [ %18, %14 ], [ 0, %8 ]
%21 = call i32 @fclose(i32* %4)
%22 = add i32 %20, %0
%23 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 1
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i64 0, i32 0
store i32 %22, i32* %24
%25 = load i32, i32* @RLIMIT_MEMLOCK
%26 = call i32 @setrlimit(i32 %25, %struct.rlimit* %3)
br label %27
27: ; preds = %1, %19
%28 = phi i32 [ %26, %19 ], [ -1, %1 ]
ret i32 %28
}