// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: unixRemapfile
static void unixRemapfile(
  unixFile *pFd,                  /* File descriptor object */
  i64 nNew                        /* Required mapping size */
){
  const char *zErr = "mmap";
  int h = pFd->h;                      /* File descriptor open on db file */
  u8 *pOrig = (u8 *)pFd->pMapRegion;   /* Pointer to current file mapping */
  i64 nOrig = pFd->mmapSizeActual;     /* Size of pOrig region in bytes */
  u8 *pNew = 0;                        /* Location of new mapping */
  int flags = PROT_READ;               /* Flags to pass to mmap() */

  assert( pFd->nFetchOut==0 );
  assert( nNew>pFd->mmapSize );
  assert( nNew<=pFd->mmapSizeMax );
  assert( nNew>0 );
  assert( pFd->mmapSizeActual>=pFd->mmapSize );
  assert( MAP_FAILED!=0 );

  if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;

  if( pOrig ){
    const int szSyspage = unixGetPagesize();
    i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
    u8 *pReq = &pOrig[nReuse];

    /* Unmap any pages of the existing mapping that cannot be reused. */
    if( nReuse!=nOrig ){
      osMunmap(pReq, nOrig-nReuse);
    }

#if HAVE_MREMAP
    pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE);
    zErr = "mremap";
#else
    pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
    if( pNew!=MAP_FAILED ){
      if( pNew!=pReq ){
        osMunmap(pNew, nNew - nReuse);
        pNew = 0;
      }else{
        pNew = pOrig;
      }
    }
#endif

    /* The attempt to extend the existing mapping failed. Free it. */
    if( pNew==MAP_FAILED || pNew==0 ){
      osMunmap(pOrig, nReuse);
    }
  }

  /* If pNew is still NULL, try to create an entirely new mapping. */
  if( pNew==0 ){
    pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0);
  }

  if( pNew==MAP_FAILED ){
    pNew = 0;
    nNew = 0;
    unixLogError(SQLITE_OK, zErr, pFd->zPath);

    /* If the mmap() above failed, assume that all subsequent mmap() calls
    ** will probably fail too. Fall back to using xRead/xWrite exclusively
    ** in this case.  */
    pFd->mmapSizeMax = 0;
  }
  pFd->pMapRegion = (void *)pNew;
  pFd->mmapSize = pFd->mmapSizeActual = nNew;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0x8faea(%rip), %rax     # 0xae1a1
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movq 0x68(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movl $0x1, -0x3c(%rbp)
movq -0x8(%rbp), %rax
movzwl 0x1e(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0x1e705
movl -0x3c(%rbp), %eax
orl $0x2, %eax
movl %eax, -0x3c(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x1e7b1
callq 0x1e850
movl %eax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movl -0x40(%rbp), %ecx
subl $0x1, %ecx
xorl $-0x1, %ecx
movslq %ecx, %rcx
andq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x30(%rbp), %rax
je 0x1e75e
movq 0xb23a8(%rip), %rax     # 0xd0af8
movq -0x50(%rbp), %rdi
movq -0x30(%rbp), %rsi
subq -0x48(%rbp), %rsi
callq *%rax
movq 0xb23ab(%rip), %r8      # 0xd0b10
movq -0x28(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x10(%rbp), %rdx
movl $0x1, %ecx
movb $0x0, %al
callq *%r8
movq %rax, -0x38(%rbp)
leaq 0x8fa27(%rip), %rax     # 0xae1ad
movq %rax, -0x18(%rbp)
movq $-0x1, %rax
cmpq %rax, -0x38(%rbp)
je 0x1e79e
cmpq $0x0, -0x38(%rbp)
jne 0x1e7af
movq 0xb2353(%rip), %rax     # 0xd0af8
movq -0x28(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq *%rax
jmp 0x1e7b1
cmpq $0x0, -0x38(%rbp)
jne 0x1e7dd
movq 0xb2321(%rip), %rax     # 0xd0ae0
movq -0x10(%rbp), %rsi
movl -0x3c(%rbp), %edx
movl -0x1c(%rbp), %r8d
xorl %ecx, %ecx
movl %ecx, %r9d
movl $0x1, %ecx
movq %r9, %rdi
callq *%rax
movq %rax, -0x38(%rbp)
movq $-0x1, %rax
cmpq %rax, -0x38(%rbp)
jne 0x1e81e
movq $0x0, -0x38(%rbp)
movq $0x0, -0x10(%rbp)
movq -0x18(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x38(%rax), %rdx
xorl %edi, %edi
movl $0x6c6c, %ecx           # imm = 0x6C6C
callq 0x1d930
movq -0x8(%rbp), %rax
movq $0x0, 0x60(%rax)
movq -0x38(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x10(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x8(%rbp), %rax
movq %rcx, 0x50(%rax)
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)