#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 stat {
  int /*<<< orphan*/ st_size;
};
typedef int /*<<< orphan*/ ssize_t;

/* Variables and functions */
int MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_NONE;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/, int, int,
           int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ remap_file_pages(char *, int, int, int /*<<< orphan*/, int);
int sysconf(int /*<<< orphan*/);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fd, pgsz;
  struct stat s;
  char *data;
  char f[] = "remap_file_pages_test_file";
  fd = open(f, O_RDWR | O_CREAT, 0777);
  write(fd, "blah", 4);
  fstat(fd, &s);
  data = mmap(0, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
  pgsz = sysconf(_SC_PAGESIZE);
  remap_file_pages(data, pgsz, PROT_NONE, 0, MAP_SHARED);
  close(fd);
  unlink(f);
  remap_file_pages((void *)-1, pgsz, PROT_NONE, 0, MAP_SHARED);
  remap_file_pages(data, (size_t)-1, PROT_NONE, 0, MAP_SHARED);
  remap_file_pages(data, pgsz, -1, 0, MAP_SHARED);
  remap_file_pages(data, pgsz, PROT_NONE, (ssize_t)-1, MAP_SHARED);
  remap_file_pages(data, pgsz, PROT_NONE, 0, -1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe6a(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe67(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe64(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe61(%rip),%ax
mov    %ax,-0x28(%rbp)
mov    0xe59(%rip),%al
mov    %al,-0x26(%rbp)
lea    -0x40(%rbp),%rdi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    0x2e20(%rip),%rax
or     (%rax),%esi
mov    $0x1ff,%edx
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    0xe2a(%rip),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   10a0 <fstat@plt>
mov    -0x10(%rbp),%esi
mov    0x2ddf(%rip),%rax
mov    (%rax),%edx
mov    0x2dbe(%rip),%rax
mov    (%rax),%ecx
mov    -0x8(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1050 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    0x2d92(%rip),%rax
mov    (%rax),%edx
mov    0x2d81(%rip),%rax
mov    (%rax),%r8d
xor    %ecx,%ecx
call   1090 <remap_file_pages@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
lea    -0x40(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0xc(%rbp),%esi
mov    0x2d64(%rip),%rax
mov    (%rax),%edx
mov    0x2d53(%rip),%rax
mov    (%rax),%r8d
mov    $0xffffffffffffffff,%rdi
xor    %ecx,%ecx
call   1090 <remap_file_pages@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d3f(%rip),%rax
mov    (%rax),%edx
mov    0x2d2e(%rip),%rax
mov    (%rax),%r8d
mov    $0xffffffff,%esi
xor    %ecx,%ecx
call   1090 <remap_file_pages@plt>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    0x2d11(%rip),%rax
mov    (%rax),%r8d
mov    $0xffffffff,%edx
xor    %ecx,%ecx
call   1090 <remap_file_pages@plt>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    0x2cfc(%rip),%rax
mov    (%rax),%edx
mov    0x2ceb(%rip),%rax
mov    (%rax),%r8d
mov    $0xffffffff,%ecx
call   1090 <remap_file_pages@plt>
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    0x2cd8(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
mov    $0xffffffff,%r8d
call   1090 <remap_file_pages@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
movups 0xe76(%rip),%xmm0
movups %xmm0,0x1b(%rsp)
movaps 0xe5f(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
mov    0x2e1b(%rip),%rax
mov    0x2e44(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x1ff,%edx
call   1070 <open@plt>
mov    %eax,%ebx
lea    0xe48(%rip),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1040 <write@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   10a0 <fstat@plt>
mov    0x8(%rsp),%esi
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
mov    0x2dd9(%rip),%r12
mov    (%r12),%ecx
xor    %edi,%edi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%r15
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,%ebp
mov    0x2db6(%rip),%r13
mov    0x0(%r13),%edx
mov    (%r12),%r8d
mov    %r15,%rdi
mov    %eax,%esi
xor    %ecx,%ecx
call   1090 <remap_file_pages@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
mov    0x0(%r13),%edx
mov    (%r12),%r8d
mov    $0xffffffffffffffff,%rdi
mov    %ebp,%esi
xor    %ecx,%ecx
call   1090 <remap_file_pages@plt>
mov    0x0(%r13),%edx
mov    (%r12),%r8d
mov    %r15,%rdi
mov    $0xffffffff,%esi
xor    %ecx,%ecx
call   1090 <remap_file_pages@plt>
mov    (%r12),%r8d
mov    %r15,%rdi
mov    %ebp,%esi
mov    $0xffffffff,%edx
xor    %ecx,%ecx
call   1090 <remap_file_pages@plt>
mov    0x0(%r13),%edx
mov    (%r12),%r8d
mov    %r15,%rdi
mov    %ebp,%esi
mov    $0xffffffff,%ecx
call   1090 <remap_file_pages@plt>
mov    0x0(%r13),%edx
mov    %r15,%rdi
mov    %ebp,%esi
xor    %ecx,%ecx
mov    $0xffffffff,%r8d
call   1090 <remap_file_pages@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@__const.main.f = [27 x i8] c"remap_file_pages_test_file\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [5 x i8] c"blah\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@_SC_PAGESIZE = global i32 0
@PROT_NONE = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca i8*
%6 = alloca [27 x i8]
store i32 0, i32* %1
%7 = bitcast [27 x i8]* %6 to i8*
%8 = getelementptr inbounds [27 x i8], [27 x i8]* %6, i64 0, i64 0
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = call i32 @open(i8* %8, i32 %11, i32 511)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = call i32 @write(i32 %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%15 = load i32, i32* %2
%16 = call i32 @fstat(i32 %15, %struct.stat* %4)
%17 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @MAP_SHARED
%21 = load i32, i32* %2
%22 = call i8* @mmap(i32 0, i32 %18, i32 %19, i32 %20, i32 %21, i32 0)
store i8* %22, i8** %5
%23 = load i32, i32* @_SC_PAGESIZE
%24 = call i32 @sysconf(i32 %23)
store i32 %24, i32* %3
%25 = load i8*, i8** %5
%26 = load i32, i32* %3
%27 = load i32, i32* @PROT_NONE
%28 = load i32, i32* @MAP_SHARED
%29 = call i32 @remap_file_pages(i8* %25, i32 %26, i32 %27, i32 0, i32 %28)
%30 = load i32, i32* %2
%31 = call i32 @close(i32 %30)
%32 = getelementptr inbounds [27 x i8], [27 x i8]* %6, i64 0, i64 0
%33 = call i32 @unlink(i8* %32)
%34 = load i32, i32* %3
%35 = load i32, i32* @PROT_NONE
%36 = load i32, i32* @MAP_SHARED
%37 = call i32 @remap_file_pages(i8* inttoptr (i64 -1 to i8*), i32 %34, i32 %35, i32 0, i32 %36)
%38 = load i8*, i8** %5
%39 = load i32, i32* @PROT_NONE
%40 = load i32, i32* @MAP_SHARED
%41 = call i32 @remap_file_pages(i8* %38, i32 -1, i32 %39, i32 0, i32 %40)
%42 = load i8*, i8** %5
%43 = load i32, i32* %3
%44 = load i32, i32* @MAP_SHARED
%45 = call i32 @remap_file_pages(i8* %42, i32 %43, i32 -1, i32 0, i32 %44)
%46 = load i8*, i8** %5
%47 = load i32, i32* %3
%48 = load i32, i32* @PROT_NONE
%49 = load i32, i32* @MAP_SHARED
%50 = call i32 @remap_file_pages(i8* %46, i32 %47, i32 %48, i32 -1, i32 %49)
%51 = load i8*, i8** %5
%52 = load i32, i32* %3
%53 = load i32, i32* @PROT_NONE
%54 = call i32 @remap_file_pages(i8* %51, i32 %52, i32 %53, i32 0, i32 -1)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@__const.main.f = [27 x i8] c"remap_file_pages_test_file\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str = [5 x i8] c"blah\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@_SC_PAGESIZE = global i32 0
@PROT_NONE = global i32 0
define i32 @main() {
%1 = alloca %struct.stat
%2 = alloca [27 x i8]
%3 = bitcast %struct.stat* %1 to i8*
%4 = getelementptr inbounds [27 x i8], [27 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = call i32 @open(i8* %4, i32 %7, i32 511)
%9 = tail call i32 @write(i32 %8, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%10 = call i32 @fstat(i32 %8, %struct.stat* %1)
%11 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%12 = load i32, i32* %11
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @MAP_SHARED
%15 = tail call i8* @mmap(i32 0, i32 %12, i32 %13, i32 %14, i32 %8, i32 0)
%16 = load i32, i32* @_SC_PAGESIZE
%17 = tail call i32 @sysconf(i32 %16)
%18 = load i32, i32* @PROT_NONE
%19 = load i32, i32* @MAP_SHARED
%20 = tail call i32 @remap_file_pages(i8* %15, i32 %17, i32 %18, i32 0, i32 %19)
%21 = tail call i32 @close(i32 %8)
%22 = call i32 @unlink(i8* %4)
%23 = load i32, i32* @PROT_NONE
%24 = load i32, i32* @MAP_SHARED
%25 = tail call i32 @remap_file_pages(i8* inttoptr (i64 -1 to i8*), i32 %17, i32 %23, i32 0, i32 %24)
%26 = load i32, i32* @PROT_NONE
%27 = load i32, i32* @MAP_SHARED
%28 = tail call i32 @remap_file_pages(i8* %15, i32 -1, i32 %26, i32 0, i32 %27)
%29 = load i32, i32* @MAP_SHARED
%30 = tail call i32 @remap_file_pages(i8* %15, i32 %17, i32 -1, i32 0, i32 %29)
%31 = load i32, i32* @PROT_NONE
%32 = load i32, i32* @MAP_SHARED
%33 = tail call i32 @remap_file_pages(i8* %15, i32 %17, i32 %31, i32 -1, i32 %32)
%34 = load i32, i32* @PROT_NONE
%35 = tail call i32 @remap_file_pages(i8* %15, i32 %17, i32 %34, i32 0, i32 -1)
ret i32 0
}