// File: /workspace/github_c_makefile_star2_O3/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, 0x38(%rsp)
movzbl 0x9(%rdi), %eax
movq %rax, 0x30(%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 %rcx, 0x8(%rsp)
movq %rdx, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
jge 0x2a376a
subl %r13d, %edx
movq %r14, %rdi
movl %r13d, %esi
movq %r15, %rcx
callq 0x2a39f2
movl %ebp, %ecx
subl %eax, %ecx
jge 0x2a373d
leaq 0x1324b7(%rip), %rsi    # 0x3d5bcb
leaq 0x1325c1(%rip), %rdx    # 0x3d5cdc
leaq 0x13230f(%rip), %r8     # 0x3d5a31
movl $0xb, %ebx
movl $0xb, %edi
movl $0x117ed, %ecx          # imm = 0x117ED
xorl %eax, %eax
callq 0x274d08
jmp 0x2a39b7
movq 0x60(%r14), %rdi
addl %eax, %eax
movslq %eax, %rsi
addq %rdi, %rsi
leal (,%rbp,2), %edx
movq %rcx, %r14
callq 0x17cd0
movq %r14, %r8
movq 0x18(%rsp), %r14
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rdx
addl %r13d, %ebp
addl %r12d, %ebp
leal (%rcx,%rdx), %esi
subl %esi, %ebp
jle 0x2a3792
movq %r14, %rdi
movl %ebp, %edx
movq %r15, %rcx
movq %r8, %r14
callq 0x2a39f2
movq %r14, %r8
movq 0x18(%rsp), %r14
subl %eax, %r8d
movq 0x38(%rsp), %rcx
movq 0x30(%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 0x10(%rsp), %rbp
jb 0x2a391a
cmpl %r13d, %ebp
jge 0x2a382c
movq 0x8(%rsp), %rax
cmpl %eax, %ebx
cmovgel %eax, %ebx
movq 0x60(%r14), %rbp
leal (%rbx,%rbx), %eax
movslq %eax, %rdi
addq %rbp, %rdi
leal (%r8,%r8), %eax
movslq %eax, %rdx
movq %rbp, %rsi
movq %r8, %r14
callq 0x17cd0
movq %r15, (%rsp)
leaq 0x28(%rsp), %rdx
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %rbp, %rcx
movq 0x10(%rsp), %rbp
movl %ebp, %r8d
movl %ebx, %r9d
callq 0x2a3aef
movq %r14, %r8
movq 0x18(%rsp), %r14
testl %eax, %eax
jne 0x2a391a
addl %ebx, %r8d
movb 0xc(%r14), %al
testb %al, %al
je 0x2a38e7
xorl %ebx, %ebx
movslq %r13d, %r12
movq %r12, 0x40(%rsp)
movzwl 0x1c(%r14,%rbx,2), %r13d
addq %r12, %r13
movl %r13d, %ecx
subl %ebp, %ecx
sets %dl
cmpl 0x8(%rsp), %ecx
setge %sil
orb %dl, %sil
jne 0x2a38d8
leal (%rcx,%rcx), %ebp
addq 0x60(%r14), %rbp
movq %r8, %r12
movl %r8d, %eax
subl %ecx, %eax
jle 0x2a3882
leaq 0x2(%rbp), %rdi
addl %eax, %eax
movslq %eax, %rdx
movq %rbp, %rsi
callq 0x17cd0
movq 0x18(%r15), %rax
cmpw $0x0, (%rax,%r13,2)
jne 0x2a3899
movq %r15, %rdi
movl %r13d, %esi
callq 0x2a39c8
movq %r15, (%rsp)
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq 0x20(%rsp), %rsi
leaq 0x28(%rsp), %rdx
movq %rbp, %rcx
movl %r13d, %r8d
movl $0x1, %r9d
callq 0x2a3aef
testl %eax, %eax
movq 0x10(%rsp), %rbp
jne 0x2a391a
movq %r12, %r8
incl %r8d
movb 0xc(%r14), %al
movq 0x40(%rsp), %r12
incq %rbx
movzbl %al, %ecx
cmpq %rcx, %rbx
jb 0x2a3842
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 0x2a3aef
testl %eax, %eax
je 0x2a3981
cmpl $0x0, 0x8(%rsp)
jle 0x2a395e
movslq 0x10(%rsp), %rbx
movq 0x18(%r15), %rcx
addq %rbx, %rbx
movq 0x8(%rsp), %rax
leal 0x1(%rax), %ebp
cmpw $0x0, (%rcx,%rbx)
jne 0x2a3953
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 0x2a3935
movq %r15, %rdi
movq 0x10(%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 0x2a3473
movq 0x8(%rsp), %rax
movw %ax, 0x18(%r14)
movb $0x0, 0xc(%r14)
movq 0x38(%rsp), %rcx
movq 0x30(%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 %ebx, %ebx
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq