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

/* Variables and functions */
int MAP_ANONYMOUS;
void *MAP_FAILED;
int MAP_POPULATE;
int MAP_PRIVATE;
int MCL_CURRENT;
int MCL_FUTURE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ RLIMIT_MEMLOCK;
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
scalar_t__ mlockall(int);
void *mmap(int /*<<< orphan*/ *, int, int, int, int /*<<< orphan*/,
           int /*<<< orphan*/);
int /*<<< orphan*/ munlockall();
int /*<<< orphan*/ munmap(void *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int test_limit(void) {
  int ret = 1;
  struct rlimit lims;
  void *map;
  if (getrlimit(RLIMIT_MEMLOCK, &lims)) {
    perror("getrlimit");
    return ret;
  }
  if (mlockall(MCL_CURRENT | (MCL_FUTURE << 1) | MCL_FUTURE)) {
    perror("mlockall");
    return ret;
  }
  map = mmap(NULL, 2 * lims.rlim_max, PROT_READ | PROT_WRITE,
             MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, 0, 0);
  if (map != MAP_FAILED)
    printf("mmap should have failed, but didn't\n");
  else {
    ret = 0;
    munmap(map, 2 * lims.rlim_max);
  }
  munlockall();
  return ret;
}


x86 assembly (clang, -O0):
<test_limit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x1,-0x8(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1090 <getrlimit@plt>
cmp    $0x0,%rax
je     11b2 <test_limit+0x42>
lea    0xe5e(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1286 <test_limit+0x116>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    0x2e26(%rip),%rax
mov    (%rax),%eax
shl    $0x1,%eax
or     %eax,%edi
mov    0x2e18(%rip),%rax
or     (%rax),%edi
call   1060 <mlockall@plt>
cmp    $0x0,%rax
je     11f8 <test_limit+0x88>
lea    0xe22(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1286 <test_limit+0x116>
mov    -0x10(%rbp),%esi
shl    $0x1,%esi
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
mov    0x2da2(%rip),%rax
or     (%rax),%edx
mov    0x2d91(%rip),%rax
mov    (%rax),%ecx
mov    0x2db0(%rip),%rax
or     (%rax),%ecx
mov    0x2d9f(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
mov    %r9d,%r8d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d57(%rip),%rcx
cmp    (%rcx),%rax
je     1263 <test_limit+0xf3>
lea    0xdba(%rip),%rdi
call   1040 <printf@plt>
jmp    1279 <test_limit+0x109>
movl   $0x0,-0x8(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%esi
shl    $0x1,%esi
call   1050 <munmap@plt>
mov    $0x0,%al
call   1080 <munlockall@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_limit>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1090 <getrlimit@plt>
test   %rax,%rax
jne    1234 <test_limit+0xc4>
mov    0x2e20(%rip),%rax
mov    0x2e49(%rip),%rcx
mov    (%rcx),%edi
lea    (%rdi,%rdi,1),%ecx
or     (%rax),%edi
or     %ecx,%edi
call   1060 <mlockall@plt>
test   %rax,%rax
jne    123d <test_limit+0xcd>
mov    0x8(%rsp),%esi
mov    0x2e37(%rip),%rax
mov    0x2de8(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dd5(%rip),%rax
mov    0x2df6(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
add    %esi,%esi
mov    0x2de1(%rip),%rax
or     (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2da1(%rip),%rcx
cmp    (%rcx),%rax
je     1217 <test_limit+0xa7>
lea    0xe08(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    1225 <test_limit+0xb5>
mov    0x8(%rsp),%esi
add    %esi,%esi
mov    %rax,%rdi
call   1050 <munmap@plt>
xor    %eax,%eax
call   1080 <munlockall@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xdc5(%rip),%rdi
jmp    1244 <test_limit+0xd4>
lea    0xdc6(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%ebx
jmp    122c <test_limit+0xbc>

LLVM IR (-O0):
%struct.rlimit = type { i32 }
@RLIMIT_MEMLOCK = global i32 0
@.str = [10 x i8] c"getrlimit\00"
@MCL_CURRENT = global i32 0
@MCL_FUTURE = global i32 0
@.str.1 = [9 x i8] c"mlockall\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_POPULATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [37 x i8] c"mmap should have failed, but didn't\0A\00"
define i32 @test_limit() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.rlimit
%4 = alloca i8*
store i32 1, i32* %2
%5 = load i32, i32* @RLIMIT_MEMLOCK
%6 = call i64 @getrlimit(i32 %5, %struct.rlimit* %3)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* %2
store i32 %10, i32* %1
br label %50
11: ; preds = %0
%12 = load i32, i32* @MCL_CURRENT
%13 = load i32, i32* @MCL_FUTURE
%14 = shl i32 %13, 1
%15 = or i32 %12, %14
%16 = load i32, i32* @MCL_FUTURE
%17 = or i32 %15, %16
%18 = call i64 @mlockall(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %11
%21 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* %2
store i32 %22, i32* %1
br label %50
23: ; preds = %11
%24 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i32 0, i32 0
%25 = load i32, i32* %24
%26 = mul nsw i32 2, %25
%27 = load i32, i32* @PROT_READ
%28 = load i32, i32* @PROT_WRITE
%29 = or i32 %27, %28
%30 = load i32, i32* @MAP_PRIVATE
%31 = load i32, i32* @MAP_ANONYMOUS
%32 = or i32 %30, %31
%33 = load i32, i32* @MAP_POPULATE
%34 = or i32 %32, %33
%35 = call i8* @mmap(i32* null, i32 %26, i32 %29, i32 %34, i32 0, i32 0)
store i8* %35, i8** %4
%36 = load i8*, i8** %4
%37 = load i8*, i8** @MAP_FAILED
%38 = icmp ne i8* %36, %37
br i1 %38, label %39, label %41
39: ; preds = %23
%40 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
br label %47
41: ; preds = %23
store i32 0, i32* %2
%42 = load i8*, i8** %4
%43 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %3, i32 0, i32 0
%44 = load i32, i32* %43
%45 = mul nsw i32 2, %44
%46 = call i32 @munmap(i8* %42, i32 %45)
br label %47
47: ; preds = %41, %39
%48 = call i32 (...) @munlockall()
%49 = load i32, i32* %2
store i32 %49, i32* %1
br label %50
50: ; preds = %47, %20, %8
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
%struct.rlimit = type { i32 }
@RLIMIT_MEMLOCK = global i32 0
@.str = [10 x i8] c"getrlimit\00"
@MCL_CURRENT = global i32 0
@MCL_FUTURE = global i32 0
@.str.1 = [9 x i8] c"mlockall\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_POPULATE = global i32 0
@MAP_FAILED = global i8* null
@str = [36 x i8] c"mmap should have failed, but didn't\00"
define i32 @test_limit() {
%1 = alloca %struct.rlimit
%2 = bitcast %struct.rlimit* %1 to i8*
%3 = load i32, i32* @RLIMIT_MEMLOCK
%4 = call i64 @getrlimit(i32 %3, %struct.rlimit* %1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %8, label %6
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %42
8: ; preds = %0
%9 = load i32, i32* @MCL_CURRENT
%10 = load i32, i32* @MCL_FUTURE
%11 = shl i32 %10, 1
%12 = or i32 %10, %9
%13 = or i32 %12, %11
%14 = call i64 @mlockall(i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %8
%17 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %42
18: ; preds = %8
%19 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 0
%20 = load i32, i32* %19
%21 = shl nsw i32 %20, 1
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @PROT_WRITE
%24 = or i32 %23, %22
%25 = load i32, i32* @MAP_PRIVATE
%26 = load i32, i32* @MAP_ANONYMOUS
%27 = or i32 %26, %25
%28 = load i32, i32* @MAP_POPULATE
%29 = or i32 %27, %28
%30 = call i8* @mmap(i32* null, i32 %21, i32 %24, i32 %29, i32 0, i32 0)
%31 = load i8*, i8** @MAP_FAILED
%32 = icmp eq i8* %30, %31
br i1 %32, label %35, label %33
33: ; preds = %18
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0))
br label %39
35: ; preds = %18
%36 = load i32, i32* %19
%37 = shl nsw i32 %36, 1
%38 = call i32 @munmap(i8* %30, i32 %37)
br label %39
39: ; preds = %35, %33
%40 = phi i32 [ 1, %33 ], [ 0, %35 ]
%41 = call i32 (...) @munlockall()
br label %42
42: ; preds = %39, %16, %6
%43 = phi i32 [ 1, %6 ], [ 1, %16 ], [ %40, %39 ]
ret i32 %43
}