// File: /workspace/github_c_makefile_star2_O0/ryancdotorg[P]bloomslice/mmapf.c
// Function: _bloomslice_mmapf
int mmapf(mmapf_ctx *ctx, const unsigned char *filename, size_t size, int flags) {
  size_t page_sz = sysconf(_SC_PAGESIZE);
  struct stat sb;

  int mmode = 0, mflags = 0;
  int fmode = 0;
  int ret, fd;

  // initialize
  ctx->mem = NULL;
  ctx->fd = -1;
  ctx->file_sz = size;

  // round up to the next multiple of the page size
  ctx->mmap_sz = size % page_sz ? (size/page_sz+1)*page_sz : size;

  mflags |= flags & MMAPF_COW ? MAP_PRIVATE : MAP_SHARED;

  if (flags & MMAPF_RW) {
    mmode |= PROT_READ|PROT_WRITE;
    fmode |= O_RDWR;
  } else if (flags & MMAPF_RD) {
    mflags |= MAP_NORESERVE;
    mmode |= PROT_READ;
    fmode |= O_RDONLY;
  } else if (flags & MMAPF_WR) {
    mmode |= PROT_WRITE;
    fmode |= O_WRONLY;
  }

  if (flags & MMAPF_CR) { fmode |= O_CREAT; }
  if (flags & MMAPF_EX) { fmode |= O_EXCL; }
  if (flags & MMAPF_PRE) { mflags |= MAP_POPULATE; }

  if (!filename) {
    ctx->mem = mmap(NULL, ctx->mmap_sz, mmode, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
  } else {
    if (stat(filename, &sb) == 0) { // file exists
      if (!S_ISREG(sb.st_mode)) { return MMAPF_ENREG; } // not a regular file
      if (sb.st_size != (off_t)size) { return MMAPF_ESIZE; } // wrong size
      if ((fd = open64(filename, fmode)) < 0) { return errno; } // open failed
    } else if (flags & MMAPF_CR) { // file missing, but creation requested
      if ((fd = open64(filename, fmode, 0644)) < 0) { return errno; } // open failed
      if ((ret = posix_fallocate(fd, 0, size)) != 0) {
        // EBADF is returned on an unsupported filesystem, ignore it
        if (ret != EBADF) { return ret; }
      }
    } else { // file missing, creation *not* requested
      return ENOENT;
    }

    //if ((ret = posix_fadvise(fd, 0, size, fadv)) != 0) { return ret; }
    // ignore results
    if (flags & MMAPF_NOREUSE) posix_fadvise(fd, 0, size, POSIX_FADV_NOREUSE);
    if (flags & MMAPF_RND) posix_fadvise(fd, 0, size, POSIX_FADV_RANDOM);
    if (flags & MMAPF_SEQ) posix_fadvise(fd, 0, size, POSIX_FADV_SEQUENTIAL);
    if (flags & MMAPF_DONTNEED) posix_fadvise(fd, 0, size, POSIX_FADV_DONTNEED);
    if (flags & MMAPF_WILLNEED) posix_fadvise(fd, 0, size, POSIX_FADV_WILLNEED);

    ctx->mem = mmap(NULL, ctx->mmap_sz, mmode, mflags, fd, 0);
  }

  if (ctx->mem == MAP_FAILED) {
    return errno;
  } else if (ctx->mem == NULL) {
    return ENOMEM;
  }

  if (flags & MMAPF_RND && (ret = posix_madvise(ctx->mem, ctx->mmap_sz, POSIX_MADV_RANDOM)) != 0) {
    munmap(ctx->mem, ctx->mmap_sz);
    ctx->mem = NULL;
    return ret;
  }

  if (flags & MMAPF_SEQ && (ret = posix_madvise(ctx->mem, ctx->mmap_sz, POSIX_MADV_SEQUENTIAL)) != 0) {
    munmap(ctx->mem, ctx->mmap_sz);
    ctx->mem = NULL;
    return ret;
  }

  if (flags & MMAPF_DONTNEED && (ret = posix_madvise(ctx->mem, ctx->mmap_sz, POSIX_MADV_DONTNEED)) != 0) {
    munmap(ctx->mem, ctx->mmap_sz);
    ctx->mem = NULL;
    return ret;
  }

  if (flags & MMAPF_WILLNEED && filename && (ret = posix_madvise(ctx->mem, ctx->mmap_sz, POSIX_MADV_WILLNEED)) != 0) {
    munmap(ctx->mem, ctx->mmap_sz);
    ctx->mem = NULL;
    return ret;
  }

#ifdef MADV_HUGEPAGE
  // reduce overhead for large mappings
  if (size > (1<<26)) { madvise(ctx->mem, ctx->mmap_sz, MADV_HUGEPAGE); }
#endif
#ifdef MADV_DONTDUMP
  // don't include in a core dump
  madvise(ctx->mem, ctx->mmap_sz, MADV_DONTDUMP);
#endif

  return MMAPF_OKAY;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl $0x1e, %edi
callq 0x1200
movq %rax, -0x30(%rbp)
movl $0x0, -0xc4(%rbp)
movl $0x0, -0xc8(%rbp)
movl $0x0, -0xcc(%rbp)
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movq -0x10(%rbp), %rax
movl $0xffffffff, 0x18(%rax) # imm = 0xFFFFFFFF
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x30(%rbp)
cmpq $0x0, %rdx
je 0x9c28
movq -0x20(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x30(%rbp)
addq $0x1, %rax
imulq -0x30(%rbp), %rax
movq %rax, -0xe0(%rbp)
jmp 0x9c33
movq -0x20(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movl -0x24(%rbp), %edx
andl $0x800, %edx            # imm = 0x800
movl $0x1, %eax
movl $0x2, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
orl -0xc8(%rbp), %eax
movl %eax, -0xc8(%rbp)
movl -0x24(%rbp), %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x9c92
movl -0xc4(%rbp), %eax
orl $0x3, %eax
movl %eax, -0xc4(%rbp)
movl -0xcc(%rbp), %eax
orl $0x2, %eax
movl %eax, -0xcc(%rbp)
jmp 0x9cfb
movl -0x24(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x9cce
movl -0xc8(%rbp), %eax
orl $0x4000, %eax           # imm = 0x4000
movl %eax, -0xc8(%rbp)
movl -0xc4(%rbp), %eax
orl $0x1, %eax
movl %eax, -0xc4(%rbp)
movl -0xcc(%rbp), %eax
orl $0x0, %eax
movl %eax, -0xcc(%rbp)
jmp 0x9cf9
movl -0x24(%rbp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x9cf7
movl -0xc4(%rbp), %eax
orl $0x2, %eax
movl %eax, -0xc4(%rbp)
movl -0xcc(%rbp), %eax
orl $0x1, %eax
movl %eax, -0xcc(%rbp)
jmp 0x9cf9
jmp 0x9cfb
movl -0x24(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x9d15
movl -0xcc(%rbp), %eax
orl $0x40, %eax
movl %eax, -0xcc(%rbp)
movl -0x24(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x9d31
movl -0xcc(%rbp), %eax
orl $0x80, %eax
movl %eax, -0xcc(%rbp)
movl -0x24(%rbp), %eax
andl $0x400, %eax            # imm = 0x400
cmpl $0x0, %eax
je 0x9d4f
movl -0xc8(%rbp), %eax
orl $0x8000, %eax           # imm = 0x8000
movl %eax, -0xc8(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x9d8b
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movl -0xc4(%rbp), %edx
xorl %eax, %eax
movl %eax, %r9d
movl $0x22, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movq %r9, %rdi
callq 0x10a0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
jmp 0x9f74
movq -0x18(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x1140
cmpl $0x0, %eax
jne 0x9e07
movl -0xa8(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x8000, %eax           # imm = 0x8000
je 0x9dbe
movl $0x3e9, -0x4(%rbp)      # imm = 0x3E9
jmp 0xa16a
movq -0x90(%rbp), %rax
cmpq -0x20(%rbp), %rax
je 0x9dd7
movl $0x3ea, -0x4(%rbp)      # imm = 0x3EA
jmp 0xa16a
movq -0x18(%rbp), %rdi
movl -0xcc(%rbp), %esi
movb $0x0, %al
callq 0x11d0
movl %eax, -0xd4(%rbp)
cmpl $0x0, %eax
jge 0x9e02
callq 0x1030
movl (%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0xa16a
jmp 0x9e89
movl -0x24(%rbp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x9e7b
movq -0x18(%rbp), %rdi
movl -0xcc(%rbp), %esi
movl $0x1a4, %edx            # imm = 0x1A4
movb $0x0, %al
callq 0x11d0
movl %eax, -0xd4(%rbp)
cmpl $0x0, %eax
jge 0x9e42
callq 0x1030
movl (%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0xa16a
movl -0xd4(%rbp), %edi
movq -0x20(%rbp), %rdx
xorl %eax, %eax
movl %eax, %esi
callq 0x1150
movl %eax, -0xd0(%rbp)
cmpl $0x0, %eax
je 0x9e79
cmpl $0x9, -0xd0(%rbp)
je 0x9e77
movl -0xd0(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xa16a
jmp 0x9e79
jmp 0x9e87
movl $0x2, -0x4(%rbp)
jmp 0xa16a
jmp 0x9e89
movl -0x24(%rbp), %eax
andl $0x1000, %eax           # imm = 0x1000
cmpl $0x0, %eax
je 0x9eae
movl -0xd4(%rbp), %edi
movq -0x20(%rbp), %rdx
xorl %eax, %eax
movl %eax, %esi
movl $0x5, %ecx
callq 0x1110
movl -0x24(%rbp), %eax
andl $0x100, %eax            # imm = 0x100
cmpl $0x0, %eax
je 0x9ed3
movl -0xd4(%rbp), %edi
movq -0x20(%rbp), %rdx
xorl %eax, %eax
movl %eax, %esi
movl $0x1, %ecx
callq 0x1110
movl -0x24(%rbp), %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
je 0x9ef8
movl -0xd4(%rbp), %edi
movq -0x20(%rbp), %rdx
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %ecx
callq 0x1110
movl -0x24(%rbp), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x0, %eax
je 0x9f1d
movl -0xd4(%rbp), %edi
movq -0x20(%rbp), %rdx
xorl %eax, %eax
movl %eax, %esi
movl $0x4, %ecx
callq 0x1110
movl -0x24(%rbp), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
je 0x9f42
movl -0xd4(%rbp), %edi
movq -0x20(%rbp), %rdx
xorl %eax, %eax
movl %eax, %esi
movl $0x3, %ecx
callq 0x1110
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movl -0xc4(%rbp), %edx
movl -0xc8(%rbp), %ecx
movl -0xd4(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movq %r9, %rdi
callq 0x10a0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
movq $-0x1, %rcx
cmpq %rcx, (%rax)
jne 0x9f93
callq 0x1030
movl (%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0xa16a
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x9fa9
movl $0xc, -0x4(%rbp)
jmp 0xa16a
jmp 0x9fab
movl -0x24(%rbp), %eax
andl $0x100, %eax            # imm = 0x100
cmpl $0x0, %eax
je 0xa009
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movl $0x1, %edx
callq 0x1220
movl %eax, -0xd0(%rbp)
cmpl $0x0, %eax
je 0xa009
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x11b0
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movl -0xd0(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xa16a
movl -0x24(%rbp), %eax
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
je 0xa067
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movl $0x2, %edx
callq 0x1220
movl %eax, -0xd0(%rbp)
cmpl $0x0, %eax
je 0xa067
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x11b0
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movl -0xd0(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xa16a
movl -0x24(%rbp), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x0, %eax
je 0xa0c5
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movl $0x4, %edx
callq 0x1220
movl %eax, -0xd0(%rbp)
cmpl $0x0, %eax
je 0xa0c5
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x11b0
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movl -0xd0(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xa16a
movl -0x24(%rbp), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
je 0xa127
cmpq $0x0, -0x18(%rbp)
je 0xa127
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movl $0x3, %edx
callq 0x1220
movl %eax, -0xd0(%rbp)
cmpl $0x0, %eax
je 0xa127
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x11b0
movq -0x10(%rbp), %rax
movq $0x0, (%rax)
movl -0xd0(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0xa16a
cmpq $0x4000000, -0x20(%rbp) # imm = 0x4000000
jbe 0xa14a
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movl $0xe, %edx
callq 0x11c0
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movl $0x10, %edx
callq 0x11c0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq