#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 int uint64_t;

/* Variables and functions */
int MAP_ANONYMOUS;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int mlock(void *, int);
void *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sleep(int);
int main() {
  void *mem;
  int err;
  uint64_t size = 4.5 * 1024 * 1024;
  size *= 1024;
  mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1,
             0);
  printf("%d\n", mem);
  err = mlock(mem, size);
  printf("err is %d\n", err);
  while (1) {
    sleep(10000);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x480000,-0x18(%rbp)
mov    -0x18(%rbp),%eax
shl    $0xa,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
mov    0x2e8f(%rip),%rax
mov    (%rax),%edx
mov    0x2e56(%rip),%rax
or     (%rax),%edx
mov    0x2e6d(%rip),%rax
mov    (%rax),%ecx
mov    0x2e54(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%esi
lea    0xe5e(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1030 <mlock@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe39(%rip),%rdi
call   1050 <printf@plt>
mov    $0x2710,%edi
call   1060 <sleep@plt>
jmp    11cc <main+0x8c>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2eb0(%rip),%rax
mov    0x2e79(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e8e(%rip),%rax
mov    0x2e77(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x20000000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
lea    0xe83(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    $0x20000000,%esi
call   1030 <mlock@plt>
lea    0xe61(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
cs nopw 0x0(%rax,%rax,1)
mov    $0x2710,%edi
call   1060 <sleep@plt>
jmp    11b0 <main+0x70>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [11 x i8] c"err is %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 4718592, i32* %4
%5 = load i32, i32* %4
%6 = mul nsw i32 %5, 1024
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %8, %9
%11 = load i32, i32* @MAP_SHARED
%12 = load i32, i32* @MAP_ANONYMOUS
%13 = or i32 %11, %12
%14 = call i8* @mmap(i32* null, i32 %7, i32 %10, i32 %13, i32 -1, i32 0)
store i8* %14, i8** %2
%15 = load i8*, i8** %2
%16 = ptrtoint i8* %15 to i32
%17 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = load i8*, i8** %2
%19 = load i32, i32* %4
%20 = call i32 @mlock(i8* %18, i32 %19)
store i32 %20, i32* %3
%21 = load i32, i32* %3
%22 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %0, %23
%24 = call i32 @sleep(i32 10000)
br label %23
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = global i32 0
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [11 x i8] c"err is %d\0A\00"
define i32 @main() {
%1 = load i32, i32* @PROT_READ
%2 = load i32, i32* @PROT_WRITE
%3 = or i32 %2, %1
%4 = load i32, i32* @MAP_SHARED
%5 = load i32, i32* @MAP_ANONYMOUS
%6 = or i32 %5, %4
%7 = tail call i8* @mmap(i32* null, i32 536870912, i32 %3, i32 %6, i32 -1, i32 0)
%8 = ptrtoint i8* %7 to i64
%9 = trunc i64 %8 to i32
%10 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = tail call i32 @mlock(i8* %7, i32 536870912)
%12 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %13, %0
%14 = tail call i32 @sleep(i32 10000)
br label %13
}