// File: /workspace/github_c_makefile_star2_O0/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: fillInCell
static int fillInCell(
  MemPage *pPage,                /* The page that contains the cell */
  unsigned char *pCell,          /* Complete text of the cell */
  const BtreePayload *pX,        /* Payload with which to construct the cell */
  int *pnSize                    /* Write cell size here */
){
  int nPayload;
  const u8 *pSrc;
  int nSrc, n, rc, mn;
  int spaceLeft;
  MemPage *pToRelease;
  unsigned char *pPrior;
  unsigned char *pPayload;
  BtShared *pBt;
  Pgno pgnoOvfl;
  int nHeader;

  assert( sqlite3_mutex_held(pPage->pBt->mutex) );

  /* pPage is not necessarily writeable since pCell might be auxiliary
  ** buffer space that is separate from the pPage buffer area */
  assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
            || sqlite3PagerIswriteable(pPage->pDbPage) );

  /* Fill in the header. */
  nHeader = pPage->childPtrSize;
  if( pPage->intKey ){
    nPayload = pX->nData + pX->nZero;
    pSrc = pX->pData;
    nSrc = pX->nData;
    assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */
    nHeader += putVarint32(&pCell[nHeader], nPayload);
    nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey);
  }else{
    assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
    nSrc = nPayload = (int)pX->nKey;
    pSrc = pX->pKey;
    nHeader += putVarint32(&pCell[nHeader], nPayload);
  }

  /* Fill in the payload */
  pPayload = &pCell[nHeader];
  if( nPayload<=pPage->maxLocal ){
    /* This is the common case where everything fits on the btree page
    ** and no overflow pages are required. */
    n = nHeader + nPayload;
    testcase( n==3 );
    testcase( n==4 );
    if( n<4 ) n = 4;
    *pnSize = n;
    assert( nSrc<=nPayload );
    testcase( nSrc<nPayload );
    memcpy(pPayload, pSrc, nSrc);
    memset(pPayload+nSrc, 0, nPayload-nSrc);
    return SQLITE_OK;
  }

  /* If we reach this point, it means that some of the content will need
  ** to spill onto overflow pages.
  */
  mn = pPage->minLocal;
  n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
  testcase( n==pPage->maxLocal );
  testcase( n==pPage->maxLocal+1 );
  if( n > pPage->maxLocal ) n = mn;
  spaceLeft = n;
  *pnSize = n + nHeader + 4;
  pPrior = &pCell[nHeader+n];
  pToRelease = 0;
  pgnoOvfl = 0;
  pBt = pPage->pBt;

  /* At this point variables should be set as follows:
  **
  **   nPayload           Total payload size in bytes
  **   pPayload           Begin writing payload here
  **   spaceLeft          Space available at pPayload.  If nPayload>spaceLeft,
  **                      that means content must spill into overflow pages.
  **   *pnSize            Size of the local cell (not counting overflow pages)
  **   pPrior             Where to write the pgno of the first overflow page
  **
  ** Use a call to btreeParseCellPtr() to verify that the values above
  ** were computed correctly.
  */
#ifdef SQLITE_DEBUG
  {
    CellInfo info;
    pPage->xParseCell(pPage, pCell, &info);
    assert( nHeader==(int)(info.pPayload - pCell) );
    assert( info.nKey==pX->nKey );
    assert( *pnSize == info.nSize );
    assert( spaceLeft == info.nLocal );
  }
#endif

  /* Write the payload into the local Cell and any extra into overflow pages */
  while( 1 ){
    n = nPayload;
    if( n>spaceLeft ) n = spaceLeft;

    /* If pToRelease is not zero than pPayload points into the data area
    ** of pToRelease.  Make sure pToRelease is still writeable. */
    assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );

    /* If pPayload is part of the data area of pPage, then make sure pPage
    ** is still writeable */
    assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
            || sqlite3PagerIswriteable(pPage->pDbPage) );

    if( nSrc>=n ){
      memcpy(pPayload, pSrc, n);
    }else if( nSrc>0 ){
      n = nSrc;
      memcpy(pPayload, pSrc, n);
    }else{
      memset(pPayload, 0, n);
    }
    nPayload -= n;
    if( nPayload<=0 ) break;
    pPayload += n;
    pSrc += n;
    nSrc -= n;
    spaceLeft -= n;
    if( spaceLeft==0 ){
      MemPage *pOvfl = 0;
#ifndef SQLITE_OMIT_AUTOVACUUM
      Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
      if( pBt->autoVacuum ){
        do{
          pgnoOvfl++;
        } while(
          PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
        );
      }
#endif
      rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
#ifndef SQLITE_OMIT_AUTOVACUUM
      /* If the database supports auto-vacuum, and the second or subsequent
      ** overflow page is being allocated, add an entry to the pointer-map
      ** for that page now.
      **
      ** If this is the first overflow page, then write a partial entry
      ** to the pointer-map. If we write nothing to this pointer-map slot,
      ** then the optimistic overflow chain processing in clearCell()
      ** may misinterpret the uninitialized values and delete the
      ** wrong pages from the database.
      */
      if( pBt->autoVacuum && rc==SQLITE_OK ){
        u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
        ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
        if( rc ){
          releasePage(pOvfl);
        }
      }
#endif
      if( rc ){
        releasePage(pToRelease);
        return rc;
      }

      /* If pToRelease is not zero than pPrior points into the data area
      ** of pToRelease.  Make sure pToRelease is still writeable. */
      assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );

      /* If pPrior is part of the data area of pPage, then make sure pPage
      ** is still writeable */
      assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
            || sqlite3PagerIswriteable(pPage->pDbPage) );

      put4byte(pPrior, pgnoOvfl);
      releasePage(pToRelease);
      pToRelease = pOvfl;
      pPrior = pOvfl->aData;
      put4byte(pPrior, 0);
      pPayload = &pOvfl->aData[4];
      spaceLeft = pBt->usableSize - 4;
    }
  }
  releasePage(pToRelease);
  return SQLITE_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movzbl 0xa(%rax), %eax
movl %eax, -0x78(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x2(%rax)
je 0x403a5a
movq -0x20(%rbp), %rax
movl 0x24(%rax), %eax
movq -0x20(%rbp), %rcx
addl 0x28(%rcx), %eax
movl %eax, -0x2c(%rbp)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movl 0x24(%rax), %eax
movl %eax, -0x3c(%rbp)
cmpl $0x80, -0x2c(%rbp)
jae 0x403a11
movl -0x2c(%rbp), %eax
movb %al, %dl
movq -0x18(%rbp), %rax
movslq -0x78(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0x403a2b
movq -0x18(%rbp), %rdi
movslq -0x78(%rbp), %rax
addq %rax, %rdi
movslq -0x2c(%rbp), %rsi
callq 0x3faa70
movl %eax, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
movzbl %al, %eax
addl -0x78(%rbp), %eax
movl %eax, -0x78(%rbp)
movq -0x18(%rbp), %rdi
movslq -0x78(%rbp), %rax
addq %rax, %rdi
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x3faa70
addl -0x78(%rbp), %eax
movl %eax, -0x78(%rbp)
jmp 0x403ac2
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, -0x2c(%rbp)
movl %eax, -0x3c(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
cmpl $0x80, -0x2c(%rbp)
jae 0x403a99
movl -0x2c(%rbp), %eax
movb %al, %dl
movq -0x18(%rbp), %rax
movslq -0x78(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x403ab3
movq -0x18(%rbp), %rdi
movslq -0x78(%rbp), %rax
addq %rax, %rdi
movslq -0x2c(%rbp), %rsi
callq 0x3faa70
movl %eax, -0x90(%rbp)
movl -0x90(%rbp), %eax
movzbl %al, %eax
addl -0x78(%rbp), %eax
movl %eax, -0x78(%rbp)
movq -0x18(%rbp), %rax
movslq -0x78(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movl -0x2c(%rbp), %eax
movq -0x10(%rbp), %rcx
movzwl 0xe(%rcx), %ecx
cmpl %ecx, %eax
jg 0x403b37
movl -0x78(%rbp), %eax
addl -0x2c(%rbp), %eax
movl %eax, -0x40(%rbp)
cmpl $0x4, -0x40(%rbp)
jge 0x403af6
movl $0x4, -0x40(%rbp)
movl -0x40(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x68(%rbp), %rdi
movq -0x38(%rbp), %rsi
movslq -0x3c(%rbp), %rdx
callq 0x19610
movq -0x68(%rbp), %rdi
movslq -0x3c(%rbp), %rax
addq %rax, %rdi
movl -0x2c(%rbp), %eax
subl -0x3c(%rbp), %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x19360
movl $0x0, -0x4(%rbp)
jmp 0x403def
movq -0x10(%rbp), %rax
movzwl 0x10(%rax), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %eax
movl %eax, -0x94(%rbp)
movl -0x2c(%rbp), %eax
subl -0x48(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x48(%rcx), %rcx
movl 0x38(%rcx), %ecx
subl $0x4, %ecx
xorl %edx, %edx
divl %ecx
movl -0x94(%rbp), %eax
addl %edx, %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x10(%rbp), %rcx
movzwl 0xe(%rcx), %ecx
cmpl %ecx, %eax
jle 0x403b83
movl -0x48(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x40(%rbp), %ecx
addl -0x78(%rbp), %ecx
addl $0x4, %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
movl -0x78(%rbp), %ecx
addl -0x40(%rbp), %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq $0x0, -0x58(%rbp)
movl $0x0, -0x74(%rbp)
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x70(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x4c(%rbp), %eax
jle 0x403bdb
movl -0x4c(%rbp), %eax
movl %eax, -0x40(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0x40(%rbp), %eax
jl 0x403bf6
movq -0x68(%rbp), %rdi
movq -0x38(%rbp), %rsi
movslq -0x40(%rbp), %rdx
callq 0x19610
jmp 0x403c26
cmpl $0x0, -0x3c(%rbp)
jle 0x403c15
movl -0x3c(%rbp), %eax
movl %eax, -0x40(%rbp)
movq -0x68(%rbp), %rdi
movq -0x38(%rbp), %rsi
movslq -0x40(%rbp), %rdx
callq 0x19610
jmp 0x403c24
movq -0x68(%rbp), %rdi
movslq -0x40(%rbp), %rdx
xorl %esi, %esi
callq 0x19360
jmp 0x403c26
movl -0x40(%rbp), %ecx
movl -0x2c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jg 0x403c3c
jmp 0x403ddf
movl -0x40(%rbp), %ecx
movq -0x68(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movl -0x40(%rbp), %ecx
movq -0x38(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movl -0x40(%rbp), %ecx
movl -0x3c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x3c(%rbp)
movl -0x40(%rbp), %ecx
movl -0x4c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jne 0x403dda
movq $0x0, -0x80(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x84(%rbp)
movq -0x70(%rbp), %rax
cmpb $0x0, 0x21(%rax)
je 0x403cf9
jmp 0x403c9b
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
movq -0x70(%rbp), %rdi
movl -0x74(%rbp), %esi
callq 0x3e8c00
movl %eax, %ecx
movb $0x1, %al
cmpl -0x74(%rbp), %ecx
movb %al, -0x95(%rbp)
je 0x403ced
movl -0x74(%rbp), %eax
movl %eax, -0x9c(%rbp)
movl 0x23bfe2(%rip), %eax    # 0x63fcb0
movq -0x70(%rbp), %rcx
xorl %edx, %edx
divl 0x34(%rcx)
movl %eax, %ecx
movl -0x9c(%rbp), %eax
addl $0x1, %ecx
cmpl %ecx, %eax
sete %al
movb %al, -0x95(%rbp)
movb -0x95(%rbp), %al
testb $0x1, %al
jne 0x403c9b
jmp 0x403cf9
movq -0x70(%rbp), %rdi
movl -0x74(%rbp), %ecx
leaq -0x80(%rbp), %rsi
leaq -0x74(%rbp), %rdx
xorl %r8d, %r8d
callq 0x3e9bc0
movl %eax, -0x44(%rbp)
movq -0x70(%rbp), %rax
movzbl 0x21(%rax), %eax
cmpl $0x0, %eax
je 0x403d72
cmpl $0x0, -0x44(%rbp)
jne 0x403d72
movl -0x84(%rbp), %edx
movl $0x3, %eax
movl $0x4, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movb %al, -0x85(%rbp)
movq -0x70(%rbp), %rdi
movl -0x74(%rbp), %esi
movb -0x85(%rbp), %al
movl -0x84(%rbp), %ecx
leaq -0x44(%rbp), %r8
movzbl %al, %edx
callq 0x3eaaf0
cmpl $0x0, -0x44(%rbp)
je 0x403d70
movq -0x80(%rbp), %rdi
callq 0x3e9b70
jmp 0x403d72
cmpl $0x0, -0x44(%rbp)
je 0x403d89
movq -0x58(%rbp), %rdi
callq 0x3e9b70
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x403def
movq -0x60(%rbp), %rdi
movl -0x74(%rbp), %esi
callq 0x3dd200
movq -0x58(%rbp), %rdi
callq 0x3e9b70
movq -0x80(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x80(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
xorl %esi, %esi
callq 0x3dd200
movq -0x80(%rbp), %rax
movq 0x50(%rax), %rax
addq $0x4, %rax
movq %rax, -0x68(%rbp)
movq -0x70(%rbp), %rax
movl 0x38(%rax), %eax
subl $0x4, %eax
movl %eax, -0x4c(%rbp)
jmp 0x403bc7
movq -0x58(%rbp), %rdi
callq 0x3e9b70
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)