// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movq %rdx, 0x10(%rsp)
movl %esi, %r15d
movq %rdi, %r12
movzbl 0x9(%rcx), %edi
movq 0x48(%rcx), %rax
movq 0x50(%rcx), %rsi
movslq 0x38(%rax), %rdx
movq %rdx, %r13
addq %rsi, %r13
movq %rcx, 0x20(%rsp)
movq 0x60(%rcx), %rbp
movq (%rax), %rax
movq %rdi, 0x28(%rsp)
movzwl 0x5(%rsi,%rdi), %ecx
rolw $0x8, %cx
movzwl %cx, %ecx
xorl %ebx, %ebx
cmpl %edx, %ecx
cmoval %ebx, %ecx
movq 0x110(%rax), %rax
movq %rax, 0x30(%rsp)
leaq (%rax,%rcx), %rdi
movq %rsi, %r14
addq %rcx, %rsi
subl %ecx, %edx
callq 0x17660
leaq 0x1(%rbx), %rax
cmpq $0x5, %rbx
ja 0x2ad027
cmpl %r15d, 0x50(%r12,%rbx,4)
movq %rax, %rbx
jle 0x2ad013
movq 0x10(%rsp), %rcx
addl %r15d, %ecx
movl %ecx, 0xc(%rsp)
leal -0x1(%rax), %ecx
movl %ecx, 0x8(%rsp)
movq 0x18(%r12,%rax,8), %rax
movq %rax, 0x18(%rsp)
movq %r13, %rbx
movslq %r15d, %rax
movq 0x10(%r12), %rcx
movq 0x18(%r12), %rdx
movq (%rcx,%rax,8), %rsi
movzwl (%rdx,%rax,2), %edx
leaq (%rsi,%rdx), %rcx
movq %rsi, %rax
subq %r14, %rax
jb 0x2ad08c
cmpq %r13, %rsi
jae 0x2ad08c
cmpq %r13, %rcx
jbe 0x2ad0b5
movl $0xb, %edi
leaq 0x1322a1(%rip), %rsi    # 0x3df31f
leaq 0x1323ab(%rip), %rdx    # 0x3df430
movl $0x1171f, %ecx          # imm = 0x1171F
jmp 0x2ad0f0
movq 0x18(%rsp), %rax
cmpq %rax, %rsi
jae 0x2ad0bd
cmpq %rax, %rcx
jbe 0x2ad0bd
movl $0xb, %edi
leaq 0x132278(%rip), %rsi    # 0x3df31f
leaq 0x132382(%rip), %rdx    # 0x3df430
movl $0x11724, %ecx          # imm = 0x11724
jmp 0x2ad0f0
addq 0x30(%rsp), %rax
movq %rax, %rsi
subq %rdx, %rbx
movl %ebx, %eax
movq %r14, %rcx
subl %ecx, %eax
rolw $0x8, %ax
movw %ax, (%rbp)
addq $0x2, %rbp
cmpq %rbp, %rbx
jae 0x2ad10d
movl $0xb, %edi
leaq 0x13223b(%rip), %rsi    # 0x3df31f
leaq 0x132345(%rip), %rdx    # 0x3df430
movl $0x1172a, %ecx          # imm = 0x1172A
leaq 0x13208e(%rip), %r8     # 0x3df185
xorl %eax, %eax
callq 0x27e637
movl $0x1, %eax
testl %eax, %eax
je 0x2ad047
jmp 0x2ad145
movq %rbx, %rdi
callq 0x17660
incl %r15d
movl $0x6, %eax
cmpl 0xc(%rsp), %r15d
jge 0x2ad103
movslq 0x8(%rsp), %rcx
xorl %eax, %eax
cmpl %r15d, 0x50(%r12,%rcx,4)
jg 0x2ad103
leal 0x1(%rcx), %edx
movl %edx, 0x8(%rsp)
movq 0x28(%r12,%rcx,8), %rcx
movq %rcx, 0x18(%rsp)
jmp 0x2ad103
cmpl $0x6, %eax
jne 0x2ad190
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %rax
movw %ax, 0x18(%rdi)
xorl %eax, %eax
movb %al, 0xc(%rdi)
movq 0x28(%rsp), %rdx
movq %r14, %rsi
movw $0x0, 0x1(%r14,%rdx)
movb 0x19(%rdi), %cl
movb %cl, 0x3(%r14,%rdx)
movb 0x18(%rdi), %cl
movb %cl, 0x4(%r14,%rdx)
subq %r14, %rbx
movb %bh, 0x5(%rsi,%rdx)
movb %bl, 0x6(%r14,%rdx)
movb %al, 0x7(%r14,%rdx)
jmp 0x2ad195
movl $0xb, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq