#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 /*<<< orphan*/ u_char;
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ MADV_SEQUENTIAL;
int /*<<< orphan*/ *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ madvise(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/,
                         int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(int /*<<< orphan*/ *, int);
u_char *remmap(u_char *mem, int fd, off_t offset) {
  if (mem != NULL)
    munmap(mem, (8 * 1024 * 1024));
  mem = mmap(NULL, (8 * 1024 * 1024), PROT_READ, MAP_SHARED, fd, offset);
  if (mem == MAP_FAILED)
    return (NULL);
  madvise(mem, (8 * 1024 * 1024), MADV_SEQUENTIAL);
  return (mem);
}


x86 assembly (clang, -O0):
<remmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     115b <remmap+0x2b>
mov    -0x10(%rbp),%rdi
mov    $0x800000,%esi
call   1040 <munmap@plt>
mov    0x2e96(%rip),%rax
mov    (%rax),%edx
mov    0x2e7d(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x18(%rbp),%r9d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x800000,%esi
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%rax
jne    11a8 <remmap+0x78>
movq   $0x0,-0x8(%rbp)
jmp    11c7 <remmap+0x97>
mov    -0x10(%rbp),%rdi
mov    0x2e0d(%rip),%rax
mov    (%rax),%edx
mov    $0x800000,%esi
call   1050 <madvise@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remmap>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %esi,%ebx
test   %rdi,%rdi
je     1148 <remmap+0x18>
mov    $0x800000,%esi
call   1040 <munmap@plt>
mov    0x2ea9(%rip),%rax
mov    (%rax),%edx
mov    0x2e90(%rip),%rax
mov    (%rax),%ecx
xor    %ebp,%ebp
xor    %edi,%edi
mov    $0x800000,%esi
mov    %ebx,%r8d
mov    %r14d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e58(%rip),%rax
cmp    (%rax),%rbx
je     1196 <remmap+0x66>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x800000,%esi
call   1050 <madvise@plt>
mov    %rbx,%rbp
mov    %rbp,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@MADV_SEQUENTIAL = global i32 0
define i32* @remmap(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i32* %0, i32** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%8 = load i32*, i32** %5
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %13
10: ; preds = %3
%11 = load i32*, i32** %5
%12 = call i32 @munmap(i32* %11, i32 8388608)
br label %13
13: ; preds = %10, %3
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @MAP_SHARED
%16 = load i32, i32* %6
%17 = load i32, i32* %7
%18 = call i32* @mmap(i32* null, i32 8388608, i32 %14, i32 %15, i32 %16, i32 %17)
store i32* %18, i32** %5
%19 = load i32*, i32** %5
%20 = load i32*, i32** @MAP_FAILED
%21 = icmp eq i32* %19, %20
br i1 %21, label %22, label %23
22: ; preds = %13
store i32* null, i32** %4
br label %28
23: ; preds = %13
%24 = load i32*, i32** %5
%25 = load i32, i32* @MADV_SEQUENTIAL
%26 = call i32 @madvise(i32* %24, i32 8388608, i32 %25)
%27 = load i32*, i32** %5
store i32* %27, i32** %4
br label %28
28: ; preds = %23, %22
%29 = load i32*, i32** %4
ret i32* %29
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@MADV_SEQUENTIAL = global i32 0
define i32* @remmap(i32* %0, i32 %1, i32 %2) {
%4 = icmp eq i32* %0, null
br i1 %4, label %7, label %5
5: ; preds = %3
%6 = tail call i32 @munmap(i32* %0, i32 8388608)
br label %7
7: ; preds = %5, %3
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @MAP_SHARED
%10 = tail call i32* @mmap(i32* null, i32 8388608, i32 %8, i32 %9, i32 %1, i32 %2)
%11 = load i32*, i32** @MAP_FAILED
%12 = icmp eq i32* %10, %11
br i1 %12, label %16, label %13
13: ; preds = %7
%14 = load i32, i32* @MADV_SEQUENTIAL
%15 = tail call i32 @madvise(i32* %10, i32 8388608, i32 %14)
br label %16
16: ; preds = %7, %13
%17 = phi i32* [ %10, %13 ], [ null, %7 ]
ret i32* %17
}