// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: rebuildPage
static int rebuildPage(
  CellArray *pCArray,             /* Content to be added to page pPg */
  int iFirst,                     /* First cell in pCArray to use */
  int nCell,                      /* Final number of cells on page */
  MemPage *pPg                    /* The page to be reconstructed */
){
  const int hdr = pPg->hdrOffset;          /* Offset of header on pPg */
  u8 * const aData = pPg->aData;           /* Pointer to data for pPg */
  const int usableSize = pPg->pBt->usableSize;
  u8 * const pEnd = &aData[usableSize];
  int i = iFirst;                 /* Which cell to copy from pCArray*/
  u32 j;                          /* Start of cell content area */
  int iEnd = i+nCell;             /* Loop terminator */
  u8 *pCellptr = pPg->aCellIdx;
  u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
  u8 *pData;
  int k;                          /* Current slot in pCArray->apEnd[] */
  u8 *pSrcEnd;                    /* Current pCArray->apEnd[k] value */

  assert( i<iEnd );
  j = get2byte(&aData[hdr+5]);
  if( NEVER(j>(u32)usableSize) ){ j = 0; }
  memcpy(&pTmp[j], &aData[j], usableSize - j);

  for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
  pSrcEnd = pCArray->apEnd[k];

  pData = pEnd;
  while( 1/*exit by break*/ ){
    u8 *pCell = pCArray->apCell[i];
    u16 sz = pCArray->szCell[i];
    assert( sz>0 );
    if( SQLITE_WITHIN(pCell,aData,pEnd) ){
      if( ((uptr)(pCell+sz))>(uptr)pEnd ) return SQLITE_CORRUPT_BKPT;
      pCell = &pTmp[pCell - aData];
    }else if( (uptr)(pCell+sz)>(uptr)pSrcEnd
           && (uptr)(pCell)<(uptr)pSrcEnd
    ){
      return SQLITE_CORRUPT_BKPT;
    }

    pData -= sz;
    put2byte(pCellptr, (pData - aData));
    pCellptr += 2;
    if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT;
    memcpy(pData, pCell, sz);
    assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB );
    testcase( sz!=pPg->xCellSize(pPg,pCell) )
    i++;
    if( i>=iEnd ) break;
    if( pCArray->ixNx[k]<=i ){
      k++;
      pSrcEnd = pCArray->apEnd[k];
    }
  }

  /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
  pPg->nCell = nCell;
  pPg->nOverflow = 0;

  put2byte(&aData[hdr+1], 0);
  put2byte(&aData[hdr+3], pPg->nCell);
  put2byte(&aData[hdr+5], pData - aData);
  aData[hdr+7] = 0x00;
  return SQLITE_OK;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edx, %ebp
movl %esi, %r12d
movq %rdi, %r15
movzbl 0x9(%rcx), %esi
movq 0x48(%rcx), %rax
movq 0x50(%rcx), %r8
movslq 0x38(%rax), %rdx
movq %rdx, %rdi
addq %r8, %rdi
movq %rdi, 0x8(%rsp)
movq %rcx, 0x18(%rsp)
movq 0x60(%rcx), %r13
movq (%rax), %rax
movq %rsi, 0x20(%rsp)
movzwl 0x5(%r8,%rsi), %ecx
rolw $0x8, %cx
movzwl %cx, %ecx
xorl %r14d, %r14d
cmpl %edx, %ecx
cmoval %r14d, %ecx
movq 0x110(%rax), %rax
movq %rax, 0x28(%rsp)
leaq (%rax,%rcx), %rdi
movq %r8, (%rsp)
leaq (%r8,%rcx), %rsi
subl %ecx, %edx
callq 0x17630
leaq 0x1(%r14), %rbx
cmpq $0x5, %r14
ja 0x249115
cmpl %r12d, 0x50(%r15,%r14,4)
movq %rbx, %r14
jle 0x249101
movq %rbp, 0x10(%rsp)
leal (%r12,%rbp), %eax
movslq %r12d, %r14
cltq
movq %rax, 0x40(%rsp)
decq %rbx
movq 0x8(%rsp), %rbp
movq (%rsp), %rdi
movq 0x20(%r15,%rbx,8), %rax
movq %rax, 0x30(%rsp)
movslq %ebx, %rax
movq %rax, 0x38(%rsp)
movq 0x10(%r15), %rax
movq 0x18(%r15), %rcx
movq (%rax,%r14,8), %rsi
movzwl (%rcx,%r14,2), %edx
leaq (%rsi,%rdx), %rcx
movq %rsi, %rax
subq %rdi, %rax
jb 0x24917f
cmpq 0x8(%rsp), %rsi
jae 0x24917f
cmpq 0x8(%rsp), %rcx
ja 0x249243
addq 0x28(%rsp), %rax
movq %rax, %rsi
jmp 0x249192
movq 0x30(%rsp), %rax
cmpq %rax, %rsi
jae 0x249192
cmpq %rax, %rcx
ja 0x24924a
subq %rdx, %rbp
movq %rbp, %r12
subq %rdi, %r12
movl %r12d, %eax
rolw $0x8, %ax
movw %ax, (%r13)
addq $0x2, %r13
cmpq %r13, %rbp
jb 0x2491e3
movq %rbp, %rdi
callq 0x17630
incq %r14
cmpq 0x40(%rsp), %r14
jge 0x2491ea
movq 0x38(%rsp), %rax
movslq 0x50(%r15,%rax,4), %rax
cmpq %rax, %r14
movq (%rsp), %rdi
jl 0x249146
incl %ebx
movslq %ebx, %rbx
jmp 0x249134
movl $0x1172a, %edi          # imm = 0x1172A
jmp 0x24924f
movl %r12d, %eax
shrl $0x8, %eax
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rcx
movw %cx, 0x18(%rdi)
xorl %ecx, %ecx
movb %cl, 0xc(%rdi)
movq 0x20(%rsp), %rsi
movq (%rsp), %r8
andw $0x0, 0x1(%r8,%rsi)
movb 0x19(%rdi), %dl
movb %dl, 0x3(%r8,%rsi)
movb 0x18(%rdi), %dl
movb %dl, 0x4(%r8,%rsi)
movb %al, 0x5(%r8,%rsi)
movb %r12b, 0x6(%r8,%rsi)
movb %cl, 0x7(%r8,%rsi)
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1171f, %edi          # imm = 0x1171F
jmp 0x24924f
movl $0x11724, %edi          # imm = 0x11724
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x232547