#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_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_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    127b <test_limit+0x10b>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
shl    $0x1,%edi
mov    0x2e23(%rip),%rax
or     (%rax),%edi
call   1060 <mlockall@plt>
cmp    $0x0,%rax
je     11ed <test_limit+0x7d>
lea    0xe2d(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    127b <test_limit+0x10b>
mov    -0x10(%rbp),%esi
shl    $0x1,%esi
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
mov    0x2db5(%rip),%rax
or     (%rax),%edx
mov    0x2da4(%rip),%rax
mov    (%rax),%ecx
mov    0x2dbb(%rip),%rax
or     (%rax),%ecx
mov    0x2daa(%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    0x2d6a(%rip),%rcx
cmp    (%rcx),%rax
je     1258 <test_limit+0xe8>
lea    0xdc5(%rip),%rdi
call   1040 <printf@plt>
jmp    126e <test_limit+0xfe>
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    122b <test_limit+0xbb>
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
lea    (%rax,%rax,1),%edi
or     %eax,%edi
call   1060 <mlockall@plt>
test   %rax,%rax
jne    1234 <test_limit+0xc4>
mov    0x8(%rsp),%esi
mov    0x2e40(%rip),%rax
mov    0x2df9(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2de6(%rip),%rax
mov    0x2dff(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
add    %esi,%esi
mov    0x2dea(%rip),%rax
or     (%rax),%ecx
xor    %ebx,%ebx
xor    %edi,%edi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2db2(%rip),%rcx
cmp    (%rcx),%rax
je     120e <test_limit+0x9e>
lea    0xe11(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    121c <test_limit+0xac>
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    0xdce(%rip),%rdi
jmp    123b <test_limit+0xcb>
lea    0xdcf(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%ebx
jmp    1223 <test_limit+0xb3>

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

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