// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: freePage2
static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
  MemPage *pTrunk = 0;                /* Free-list trunk page */
  Pgno iTrunk = 0;                    /* Page number of free-list trunk page */
  MemPage *pPage1 = pBt->pPage1;      /* Local reference to page 1 */
  MemPage *pPage;                     /* Page being freed. May be NULL. */
  int rc;                             /* Return Code */
  u32 nFree;                          /* Initial number of pages on free-list */

  assert( sqlite3_mutex_held(pBt->mutex) );
  assert( CORRUPT_DB || iPage>1 );
  assert( !pMemPage || pMemPage->pgno==iPage );

  if( iPage<2 || iPage>pBt->nPage ){
    return SQLITE_CORRUPT_BKPT;
  }
  if( pMemPage ){
    pPage = pMemPage;
    sqlite3PagerRef(pPage->pDbPage);
  }else{
    pPage = btreePageLookup(pBt, iPage);
  }

  /* Increment the free page count on pPage1 */
  rc = sqlite3PagerWrite(pPage1->pDbPage);
  if( rc ) goto freepage_out;
  nFree = get4byte(&pPage1->aData[36]);
  put4byte(&pPage1->aData[36], nFree+1);

  if( pBt->btsFlags & BTS_SECURE_DELETE ){
    /* If the secure_delete option is enabled, then
    ** always fully overwrite deleted information with zeros.
    */
    if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
     ||            ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
    ){
      goto freepage_out;
    }
    memset(pPage->aData, 0, pPage->pBt->pageSize);
  }

  /* If the database supports auto-vacuum, write an entry in the pointer-map
  ** to indicate that the page is free.
  */
  if( ISAUTOVACUUM ){
    ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
    if( rc ) goto freepage_out;
  }

  /* Now manipulate the actual database free-list structure. There are two
  ** possibilities. If the free-list is currently empty, or if the first
  ** trunk page in the free-list is full, then this page will become a
  ** new free-list trunk page. Otherwise, it will become a leaf of the
  ** first trunk page in the current free-list. This block tests if it
  ** is possible to add the page as a new free-list leaf.
  */
  if( nFree!=0 ){
    u32 nLeaf;                /* Initial number of leaf cells on trunk page */

    iTrunk = get4byte(&pPage1->aData[32]);
    if( iTrunk>btreePagecount(pBt) ){
      rc = SQLITE_CORRUPT_BKPT;
      goto freepage_out;
    }
    rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
    if( rc!=SQLITE_OK ){
      goto freepage_out;
    }

    nLeaf = get4byte(&pTrunk->aData[4]);
    assert( pBt->usableSize>32 );
    if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
      rc = SQLITE_CORRUPT_BKPT;
      goto freepage_out;
    }
    if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
      /* In this case there is room on the trunk page to insert the page
      ** being freed as a new leaf.
      **
      ** Note that the trunk page is not really full until it contains
      ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
      ** coded.  But due to a coding error in versions of SQLite prior to
      ** 3.6.0, databases with freelist trunk pages holding more than
      ** usableSize/4 - 8 entries will be reported as corrupt.  In order
      ** to maintain backwards compatibility with older versions of SQLite,
      ** we will continue to restrict the number of entries to usableSize/4 - 8
      ** for now.  At some point in the future (once everyone has upgraded
      ** to 3.6.0 or later) we should consider fixing the conditional above
      ** to read "usableSize/4-2" instead of "usableSize/4-8".
      **
      ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still
      ** avoid using the last six entries in the freelist trunk page array in
      ** order that database files created by newer versions of SQLite can be
      ** read by older versions of SQLite.
      */
      rc = sqlite3PagerWrite(pTrunk->pDbPage);
      if( rc==SQLITE_OK ){
        put4byte(&pTrunk->aData[4], nLeaf+1);
        put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
        if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
          sqlite3PagerDontWrite(pPage->pDbPage);
        }
        rc = btreeSetHasContent(pBt, iPage);
      }
      TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
      goto freepage_out;
    }
  }

  /* If control flows to this point, then it was not possible to add the
  ** the page being freed as a leaf page of the first trunk in the free-list.
  ** Possibly because the free-list is empty, or possibly because the
  ** first trunk in the free-list is full. Either way, the page being freed
  ** will become the new first trunk page in the free-list.
  */
  if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
    goto freepage_out;
  }
  rc = sqlite3PagerWrite(pPage->pDbPage);
  if( rc!=SQLITE_OK ){
    goto freepage_out;
  }
  put4byte(pPage->aData, iTrunk);
  put4byte(&pPage->aData[4], 0);
  put4byte(&pPage1->aData[32], iPage);
  TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));

freepage_out:
  if( pPage ){
    pPage->isInit = 0;
  }
  releasePage(pPage);
  releasePage(pTrunk);
  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq $0x0, 0x10(%rsp)
cmpl $0x2, %edx
jb 0x2a9278
movl %edx, %ebx
movq %rdi, %r14
cmpl %edx, 0x40(%rdi)
jae 0x2a92a5
leaq 0x1360a0(%rip), %rsi    # 0x3df31f
leaq 0x1361aa(%rip), %rdx    # 0x3df430
leaq 0x135ef8(%rip), %r8     # 0x3df185
movl $0xb, %ebx
movl $0xb, %edi
movl $0x1144a, %ecx          # imm = 0x1144A
xorl %eax, %eax
callq 0x27e637
jmp 0x2a9312
movq 0x18(%r14), %r12
testq %rsi, %rsi
je 0x2a92c4
movq %rsi, 0x8(%rsp)
movq 0x70(%rsi), %rax
incw 0x36(%rax)
movq 0x18(%rax), %rax
incl 0x18(%rax)
jmp 0x2a92d3
movq %r14, %rdi
movl %ebx, %esi
callq 0x2a9201
movq %rax, 0x8(%rsp)
movq 0x70(%r12), %rdi
callq 0x280e3a
movl %eax, 0x4(%rsp)
testl %eax, %eax
je 0x2a9323
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x2a92fb
movb $0x0, (%rax)
movq 0x70(%rax), %rdi
callq 0x2949b7
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x2a930e
movq 0x70(%rax), %rdi
callq 0x2949b7
movl 0x4(%rsp), %ebx
movl %ebx, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x50(%r12), %rax
movl 0x24(%rax), %r13d
bswapl %r13d
leal 0x1(%r13), %ecx
bswapl %ecx
movl %ecx, 0x24(%rax)
testb $0x4, 0x28(%r14)
je 0x2a9373
cmpq $0x0, 0x8(%rsp)
je 0x2a93e9
movq 0x8(%rsp), %r15
movq 0x70(%r15), %rdi
callq 0x280e3a
movl %eax, 0x4(%rsp)
testl %eax, %eax
jne 0x2a92e5
movq 0x48(%r15), %rax
movq 0x50(%r15), %rdi
movl 0x34(%rax), %edx
xorl %esi, %esi
callq 0x173a0
cmpb $0x0, 0x21(%r14)
je 0x2a939d
leaq 0x4(%rsp), %r15
movq %r14, %rdi
movl %ebx, %esi
movl $0x2, %edx
xorl %ecx, %ecx
movq %r15, %r8
callq 0x2991c1
cmpl $0x0, (%r15)
jne 0x2a92e5
testl %r13d, %r13d
je 0x2a940b
movq 0x50(%r12), %rax
movl 0x20(%rax), %ebp
bswapl %ebp
cmpl 0x40(%r14), %ebp
jbe 0x2a9455
leaq 0x135f62(%rip), %rsi    # 0x3df31f
leaq 0x13606c(%rip), %rdx    # 0x3df430
leaq 0x135dba(%rip), %r8     # 0x3df185
movl $0xb, %edi
movl $0x11479, %ecx          # imm = 0x11479
xorl %eax, %eax
callq 0x27e637
movl $0xb, 0x4(%rsp)
jmp 0x2a92e5
leaq 0x8(%rsp), %rdx
movq %r14, %rdi
movl %ebx, %esi
xorl %ecx, %ecx
callq 0x296bb1
movl %eax, 0x4(%rsp)
testl %eax, %eax
jne 0x2a92e5
jmp 0x2a934b
xorl %ebp, %ebp
cmpq $0x0, 0x8(%rsp)
je 0x2a94b5
movq 0x8(%rsp), %r14
movq 0x70(%r14), %rdi
callq 0x280e3a
movl %eax, 0x4(%rsp)
testl %eax, %eax
jne 0x2a92e5
movq 0x50(%r14), %rax
bswapl %ebp
movl %ebp, (%rax)
movq 0x50(%r14), %rax
movl $0x0, 0x4(%rax)
movq 0x50(%r12), %rax
bswapl %ebx
movl %ebx, 0x20(%rax)
jmp 0x2a92e5
leaq 0x10(%rsp), %rdx
movq %r14, %rdi
movl %ebp, %esi
xorl %ecx, %ecx
callq 0x296bb1
movl %eax, 0x4(%rsp)
testl %eax, %eax
jne 0x2a92e5
movq 0x10(%rsp), %r15
movq 0x50(%r15), %rax
movl 0x4(%rax), %r13d
bswapl %r13d
movl 0x38(%r14), %eax
shrl $0x2, %eax
leal -0x2(%rax), %ecx
cmpl %ecx, %r13d
jbe 0x2a94d7
leaq 0x135e87(%rip), %rsi    # 0x3df31f
leaq 0x135f91(%rip), %rdx    # 0x3df430
leaq 0x135cdf(%rip), %r8     # 0x3df185
movl $0xb, %edi
movl $0x11484, %ecx          # imm = 0x11484
jmp 0x2a93d5
leaq 0x8(%rsp), %rdx
movq %r14, %rdi
movl %ebx, %esi
xorl %ecx, %ecx
callq 0x296bb1
movl %eax, 0x4(%rsp)
testl %eax, %eax
jne 0x2a92e5
jmp 0x2a9419
addl $-0x8, %eax
cmpl %eax, %r13d
jae 0x2a940d
movq 0x70(%r15), %rdi
callq 0x280e3a
movl %eax, 0x4(%rsp)
testl %eax, %eax
jne 0x2a92e5
movq 0x50(%r15), %rax
leal 0x1(%r13), %ecx
bswapl %ecx
movl %ecx, 0x4(%rax)
movq 0x50(%r15), %rax
leal 0x8(,%r13,4), %ecx
movl %ebx, %edx
bswapl %edx
movl %edx, (%rax,%rcx)
movq 0x8(%rsp), %rax
testq %rax, %rax
je 0x2a9553
testb $0x4, 0x28(%r14)
jne 0x2a9553
movq 0x70(%rax), %rax
movq 0x28(%rax), %rdx
cmpb $0x0, 0x10(%rdx)
jne 0x2a9553
movzwl 0x34(%rax), %ecx
testb $0x2, %cl
je 0x2a9553
cmpl $0x0, 0x80(%rdx)
jne 0x2a9553
andl $-0x15, %ecx
orl $0x10, %ecx
movw %cx, 0x34(%rax)
movq %r14, %rdi
movl %ebx, %esi
callq 0x2a9566
movl %eax, 0x4(%rsp)
jmp 0x2a92e5