// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: editPage
static int editPage(
  MemPage *pPg,                   /* Edit this page */
  int iOld,                       /* Index of first cell currently on page */
  int iNew,                       /* Index of new first cell on page */
  int nNew,                       /* Final number of cells on page */
  CellArray *pCArray              /* Array of cells and sizes */
){
  u8 * const aData = pPg->aData;
  const int hdr = pPg->hdrOffset;
  u8 *pBegin = &pPg->aCellIdx[nNew * 2];
  int nCell = pPg->nCell;       /* Cells stored on pPg */
  u8 *pData;
  u8 *pCellptr;
  int i;
  int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
  int iNewEnd = iNew + nNew;

#ifdef SQLITE_DEBUG
  u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
  memcpy(pTmp, aData, pPg->pBt->usableSize);
#endif

  /* Remove cells from the start and end of the page */
  assert( nCell>=0 );
  if( iOld<iNew ){
    int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
    if( NEVER(nShift>nCell) ) return SQLITE_CORRUPT_BKPT;
    memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
    nCell -= nShift;
  }
  if( iNewEnd < iOldEnd ){
    int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
    assert( nCell>=nTail );
    nCell -= nTail;
  }

  pData = &aData[get2byteNotZero(&aData[hdr+5])];
  if( pData<pBegin ) goto editpage_fail;

  /* Add cells to the start of the page */
  if( iNew<iOld ){
    int nAdd = MIN(nNew,iOld-iNew);
    assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB );
    assert( nAdd>=0 );
    pCellptr = pPg->aCellIdx;
    memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
    if( pageInsertArray(
          pPg, pBegin, &pData, pCellptr,
          iNew, nAdd, pCArray
    ) ) goto editpage_fail;
    nCell += nAdd;
  }

  /* Add any overflow cells */
  for(i=0; i<pPg->nOverflow; i++){
    int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
    if( iCell>=0 && iCell<nNew ){
      pCellptr = &pPg->aCellIdx[iCell * 2];
      if( nCell>iCell ){
        memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
      }
      nCell++;
      cachedCellSize(pCArray, iCell+iNew);
      if( pageInsertArray(
            pPg, pBegin, &pData, pCellptr,
            iCell+iNew, 1, pCArray
      ) ) goto editpage_fail;
    }
  }

  /* Append cells to the end of the page */
  assert( nCell>=0 );
  pCellptr = &pPg->aCellIdx[nCell*2];
  if( pageInsertArray(
        pPg, pBegin, &pData, pCellptr,
        iNew+nCell, nNew-nCell, pCArray
  ) ) goto editpage_fail;

  pPg->nCell = nNew;
  pPg->nOverflow = 0;

  put2byte(&aData[hdr+3], pPg->nCell);
  put2byte(&aData[hdr+5], pData - aData);

#ifdef SQLITE_DEBUG
  for(i=0; i<nNew && !CORRUPT_DB; i++){
    u8 *pCell = pCArray->apCell[i+iNew];
    int iOff = get2byteAligned(&pPg->aCellIdx[i*2]);
    if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){
      pCell = &pTmp[pCell - aData];
    }
    assert( 0==memcmp(pCell, &aData[iOff],
            pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) );
  }
#endif

  return SQLITE_OK;
 editpage_fail:
  /* Unable to edit this page. Rebuild it from scratch instead. */
  populateCellCache(pCArray, iNew, nNew);
  return rebuildPage(pCArray, iNew, nNew, pPg);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, %r15
movl %esi, %r13d
movq %rdi, %r14
movq 0x50(%rdi), %rax
movq %rax, 0x40(%rsp)
movzbl 0x9(%rdi), %eax
movq %rax, 0x38(%rsp)
movq %rcx, 0x8(%rsp)
leal (%rcx,%rcx), %eax
cltq
addq 0x60(%rdi), %rax
movq %rax, 0x20(%rsp)
movzwl 0x18(%rdi), %ebp
movzbl 0xc(%rdi), %r12d
movl %esi, %ebx
movl %ebp, %r8d
subl %edx, %ebx
movq %rdx, 0x18(%rsp)
jge 0x2ad28b
subl %r13d, %edx
movq %r14, %rdi
movl %r13d, 0x10(%rsp)
movl %r13d, %esi
movq %r15, %rcx
callq 0x2ad528
movl %eax, %r13d
movl %ebp, %ecx
subl %eax, %ecx
jge 0x2ad245
leaq 0x1320fe(%rip), %rsi    # 0x3df31f
leaq 0x132208(%rip), %rdx    # 0x3df430
leaq 0x131f56(%rip), %r8     # 0x3df185
movl $0xb, %edi
movl $0x117ed, %ecx          # imm = 0x117ED
xorl %eax, %eax
callq 0x27e637
movl %ebp, %r8d
jmp 0x2ad273
movq 0x60(%r14), %rdi
leal (,%r13,2), %eax
movslq %eax, %rsi
addq %rdi, %rsi
leal (,%rbp,2), %edx
movl %r12d, 0x34(%rsp)
movq %rcx, %r12
callq 0x17cd0
movq %r12, %r8
movl 0x34(%rsp), %r12d
movl $0xb, %eax
cmpl %r13d, %ebp
movq 0x18(%rsp), %rdx
movl 0x10(%rsp), %r13d
jl 0x2ad4ef
addl %r13d, %ebp
addl %r12d, %ebp
movq 0x8(%rsp), %rax
leal (%rax,%rdx), %esi
subl %esi, %ebp
jle 0x2ad2b3
movq %r14, %rdi
movl %ebp, %edx
movq %r15, %rcx
movq %r8, %r12
callq 0x2ad528
movq %r12, %r8
subl %eax, %r8d
movq 0x40(%rsp), %rcx
movq 0x38(%rsp), %rax
movzwl 0x5(%rcx,%rax), %eax
rolw $0x8, %ax
decl %eax
movzwl %ax, %eax
addq %rcx, %rax
incq %rax
movq %rax, 0x28(%rsp)
cmpq 0x20(%rsp), %rax
movq 0x18(%rsp), %rbp
jb 0x2ad452
cmpl %r13d, %ebp
jge 0x2ad350
movq 0x8(%rsp), %rax
cmpl %eax, %ebx
cmovgel %eax, %ebx
movl %r13d, 0x10(%rsp)
movq 0x60(%r14), %r13
leal (%rbx,%rbx), %eax
movslq %eax, %rdi
addq %r13, %rdi
leal (%r8,%r8), %eax
movslq %eax, %rdx
movq %r13, %rsi
movq %r8, %r12
callq 0x17cd0
movq %r15, (%rsp)
leaq 0x28(%rsp), %rdx
movq %r14, %rdi
movq 0x20(%rsp), %rsi
movq %r13, %rcx
movl 0x10(%rsp), %r13d
movl %ebp, %r8d
movl %ebx, %r9d
callq 0x2ad64d
xorl %ecx, %ecx
testl %eax, %eax
cmovel %ebx, %ecx
jne 0x2ad452
addl %r12d, %ecx
movl %ecx, %r8d
cmpb $0x0, 0xc(%r14)
je 0x2ad41f
xorl %ebx, %ebx
movslq %r13d, %r12
movq %r14, 0x10(%rsp)
movzwl 0x1c(%r14,%rbx,2), %r13d
addq %r12, %r13
movl %r13d, %eax
subl %ebp, %eax
sets %dl
cmpl 0x8(%rsp), %eax
setge %sil
movb $0x1, %cl
orb %dl, %sil
jne 0x2ad40a
leal (%rax,%rax), %ebp
addq 0x60(%r14), %rbp
movl %r8d, %ecx
subl %eax, %ecx
jle 0x2ad3b3
leaq 0x2(%rbp), %rdi
addl %ecx, %ecx
movslq %ecx, %rdx
movq %rbp, %rsi
movq %r8, %r14
callq 0x17cd0
movq %r14, %r8
movq 0x10(%rsp), %r14
movq 0x18(%r15), %rax
cmpw $0x0, (%rax,%r13,2)
jne 0x2ad3d5
movq %r15, %rdi
movl %r13d, %esi
movq %r8, %r14
callq 0x2ad4fe
movq %r14, %r8
movq 0x10(%rsp), %r14
incl %r8d
movq %r15, (%rsp)
movq %r14, %rdi
movq 0x20(%rsp), %rsi
leaq 0x28(%rsp), %rdx
movq %rbp, %rcx
movq %r8, %rbp
movl %r13d, %r8d
movl $0x1, %r9d
callq 0x2ad64d
movq %rbp, %r8
testl %eax, %eax
sete %cl
movq 0x18(%rsp), %rbp
testb %cl, %cl
je 0x2ad452
incq %rbx
movzbl 0xc(%r14), %eax
cmpq %rax, %rbx
jb 0x2ad365
leal (%r8,%r8), %eax
movslq %eax, %rcx
addq 0x60(%r14), %rcx
movq 0x8(%rsp), %rax
subl %r8d, %eax
addl %ebp, %r8d
movq %r15, (%rsp)
leaq 0x28(%rsp), %rdx
movq %r14, %rdi
movq 0x20(%rsp), %rsi
movl %eax, %r9d
callq 0x2ad64d
testl %eax, %eax
je 0x2ad4b9
cmpl $0x0, 0x8(%rsp)
jle 0x2ad496
movslq 0x18(%rsp), %rbx
addq %rbx, %rbx
movq 0x8(%rsp), %rax
leal 0x1(%rax), %ebp
movq 0x18(%r15), %rax
cmpw $0x0, (%rax,%rbx)
jne 0x2ad48b
movq 0x8(%r15), %rdi
movq 0x10(%r15), %rax
movq (%rax,%rbx,4), %rsi
callq *0x78(%rdi)
movq 0x18(%r15), %rcx
movw %ax, (%rcx,%rbx)
addq $0x2, %rbx
decl %ebp
cmpl $0x1, %ebp
jg 0x2ad469
movq %r15, %rdi
movq 0x18(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r14, %rcx
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2acfa3
movq 0x8(%rsp), %rax
movw %ax, 0x18(%r14)
movb $0x0, 0xc(%r14)
movq 0x40(%rsp), %rcx
movq 0x38(%rsp), %rdx
movb %ah, 0x3(%rcx,%rdx)
movb 0x18(%r14), %al
movb %al, 0x4(%rcx,%rdx)
movl 0x28(%rsp), %eax
subl %ecx, %eax
rolw $0x8, %ax
movw %ax, 0x5(%rcx,%rdx)
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq