// File: mshabbirhasan[P]ups-indel/include/seqan/file/file_mapping.h
// Function: void* seqan::mapFileSegment<void, int, long, seqan::FileMappingMode>(seqan::FileMapping<void>&, int, long, seqan::FileMappingMode)
inline void *
mapFileSegment(FileMapping<TSpec> &mapping, TPos fileOfs, TSize size, TFileMappingMode mode)
{
    SEQAN_ASSERT_EQ((int)OPEN_RDONLY, (int)MAP_RDONLY);
    SEQAN_ASSERT_EQ((int)OPEN_WRONLY, (int)MAP_WRONLY);

    void *addr;
    if (size == 0)
        return NULL;
    mode = (FileMappingMode)(mode & (mapping.openMode & OPEN_MASK));

#ifdef PLATFORM_WINDOWS

    DWORD access = ((mode & OPEN_MASK) == OPEN_RDONLY) ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS;
    LARGE_INTEGER largeOfs;
    largeOfs.QuadPart = fileOfs;

    addr = MapViewOfFile(
        mapping.handle,                 // _In_     HANDLE hFileMappingObject,
        access,                         // _In_     DWORD dwDesiredAccess,
        largeOfs.HighPart,              // _In_     DWORD dwFileOffsetHigh,
        largeOfs.LowPart,               // _In_     DWORD dwFileOffsetLow,
        size);                          // _In_     SIZE_T dwNumberOfBytesToMap (0 = map the whole file)
#else
    int prot = 0;

    if (mode & MAP_RDONLY) prot |= PROT_READ;
    if (mode & MAP_WRONLY) prot |= PROT_WRITE;

    int flags = 0;
    if ((mode & MAP_COPYONWRITE) != 0)
        flags |= MAP_PRIVATE;
    else
        flags |= MAP_SHARED;

    addr = mmap(
        NULL,
        size,
        prot,
        flags,
        mapping.file.handle,
        fileOfs);
//    std::cerr << "mmap(0,"<<size<<','<<prot<<','<<flags<<','<<mapping.file.handle<<','<<fileOfs<<")="<<addr<<std::endl;

    if (addr == MAP_FAILED)
        addr = NULL;
#endif
    if (addr == NULL)
    {
        SEQAN_FAIL("mapFileSegment(%i,%i,%i) failed (filesize=%i): \"%s\"", fileOfs, size, mode, seqan::size(mapping.file), strerror(errno));
    }
    return addr;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl $0x1, -0x28(%rbp)
movl $0x1, -0x2c(%rbp)
leaq 0x168c8(%rip), %rdi     # 0x28766
movl $0x2f2, %esi            # imm = 0x2F2
leaq -0x28(%rbp), %rdx
leaq 0x169f7(%rip), %rcx     # 0x288a5
leaq -0x2c(%rbp), %r8
leaq 0x169fd(%rip), %r9      # 0x288b6
callq 0x12050
xorb $-0x1, %al
testb $0x1, %al
jne 0x11ec6
jmp 0x11ecb
callq 0xb2a0
jmp 0x11ecd
jmp 0x11ecf
movl $0x2, -0x30(%rbp)
movl $0x2, -0x34(%rbp)
leaq 0x16882(%rip), %rdi     # 0x28766
movl $0x2f3, %esi            # imm = 0x2F3
leaq -0x30(%rbp), %rdx
leaq 0x169d2(%rip), %rcx     # 0x288c6
leaq -0x34(%rbp), %r8
leaq 0x169d8(%rip), %r9      # 0x288d7
callq 0x12050
xorb $-0x1, %al
testb $0x1, %al
jne 0x11f0c
jmp 0x11f11
callq 0xb2a0
jmp 0x11f13
cmpq $0x0, -0x20(%rbp)
jne 0x11f27
movq $0x0, -0x8(%rbp)
jmp 0x12041
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x18(%rcx), %ecx
andl $0x3, %ecx
andl %ecx, %eax
movl %eax, -0x24(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x24(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x11f54
movl -0x44(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x44(%rbp)
movl -0x24(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x11f68
movl -0x44(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x24(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x11f85
movl -0x48(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x48(%rbp)
jmp 0x11f8e
movl -0x48(%rbp), %eax
orl $0x1, %eax
movl %eax, -0x48(%rbp)
movq -0x20(%rbp), %rsi
movl -0x44(%rbp), %edx
movl -0x48(%rbp), %ecx
movq -0x10(%rbp), %rax
movl 0x10(%rax), %r8d
movslq -0x14(%rbp), %r9
xorl %eax, %eax
movl %eax, %edi
callq 0x4790
movq %rax, -0x40(%rbp)
movq $-0x1, %rax
cmpq %rax, -0x40(%rbp)
jne 0x11fc6
movq $0x0, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x12039
jmp 0x11fcf
movl -0x14(%rbp), %eax
movl %eax, -0x64(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x60(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rdi
addq $0x8, %rdi
callq 0x120a0
movq %rax, -0x50(%rbp)
callq 0x4080
movl (%rax), %edi
callq 0x4190
movl -0x64(%rbp), %ecx
movq -0x60(%rbp), %r8
movl -0x54(%rbp), %r9d
movq -0x50(%rbp), %r10
leaq 0x16750(%rip), %rdi     # 0x28766
movl $0x320, %esi            # imm = 0x320
leaq 0x168c5(%rip), %rdx     # 0x288e7
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movb $0x0, %al
callq 0xb130
callq 0xb2a0
jmp 0x12039
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nop