// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x38(%rbp)
cmpl $0x2, -0x1c(%rbp)
jb 0x401a70
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x40(%rcx), %eax
jbe 0x401a82
movl $0x1144a, %edi          # imm = 0x1144A
callq 0x3dff80
movl %eax, -0x4(%rbp)
jmp 0x401d62
cmpq $0x0, -0x18(%rbp)
je 0x401aa0
movq -0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x401d70
jmp 0x401ab0
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x4019d0
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x3c6610
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x401acb
jmp 0x401d3c
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x24, %rdi
callq 0x3db720
movl %eax, -0x48(%rbp)
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x24, %rdi
movl -0x48(%rbp), %esi
addl $0x1, %esi
callq 0x3dd200
movq -0x10(%rbp), %rax
movzwl 0x28(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x401b5d
cmpq $0x0, -0x40(%rbp)
jne 0x401b27
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %esi
leaq -0x40(%rbp), %rdx
xorl %ecx, %ecx
callq 0x3e6f40
movl %eax, -0x44(%rbp)
cmpl $0x0, %eax
jne 0x401b3c
movq -0x40(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x3c6610
movl %eax, -0x44(%rbp)
cmpl $0x0, %eax
je 0x401b41
jmp 0x401d3c
movq -0x40(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x40(%rbp), %rax
movq 0x48(%rax), %rax
movl 0x34(%rax), %eax
movl %eax, %edx
xorl %esi, %esi
callq 0x19360
movq -0x10(%rbp), %rax
cmpb $0x0, 0x21(%rax)
je 0x401b8b
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %esi
movl $0x2, %edx
xorl %ecx, %ecx
leaq -0x44(%rbp), %r8
callq 0x3eaaf0
cmpl $0x0, -0x44(%rbp)
je 0x401b89
jmp 0x401d3c
jmp 0x401b8b
cmpl $0x0, -0x48(%rbp)
je 0x401cc7
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x20, %rdi
callq 0x3db720
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x50(%rbp)
movq -0x10(%rbp), %rdi
callq 0x3e7da0
movl %eax, %ecx
movl -0x50(%rbp), %eax
cmpl %ecx, %eax
jbe 0x401bd3
movl $0x11479, %edi          # imm = 0x11479
callq 0x3dff80
movl %eax, -0x44(%rbp)
jmp 0x401d3c
movq -0x10(%rbp), %rdi
movl -0x2c(%rbp), %esi
leaq -0x28(%rbp), %rdx
xorl %ecx, %ecx
callq 0x3e6f40
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x401bf3
jmp 0x401d3c
movq -0x28(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x4, %rdi
callq 0x3db720
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x38(%rcx), %ecx
shrl $0x2, %ecx
subl $0x2, %ecx
cmpl %ecx, %eax
jbe 0x401c2d
movl $0x11484, %edi          # imm = 0x11484
callq 0x3dff80
movl %eax, -0x44(%rbp)
jmp 0x401d3c
movl -0x4c(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0x38(%rcx), %ecx
shrl $0x2, %ecx
subl $0x8, %ecx
cmpl %ecx, %eax
jae 0x401cc5
movq -0x28(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x3c6610
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jne 0x401cc3
movq -0x28(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x4, %rdi
movl -0x4c(%rbp), %esi
addl $0x1, %esi
callq 0x3dd200
movq -0x28(%rbp), %rax
movq 0x50(%rax), %rdi
movl -0x4c(%rbp), %eax
shll $0x2, %eax
addl $0x8, %eax
movl %eax, %eax
addq %rax, %rdi
movl -0x1c(%rbp), %esi
callq 0x3dd200
cmpq $0x0, -0x40(%rbp)
je 0x401cb4
movq -0x10(%rbp), %rax
movzwl 0x28(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
jne 0x401cb4
movq -0x40(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x401d90
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x401df0
movl %eax, -0x44(%rbp)
jmp 0x401d3c
jmp 0x401cc7
cmpq $0x0, -0x40(%rbp)
jne 0x401ced
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %esi
leaq -0x40(%rbp), %rdx
xorl %ecx, %ecx
callq 0x3e6f40
movl %eax, %ecx
movl %ecx, -0x44(%rbp)
xorl %eax, %eax
cmpl %ecx, %eax
je 0x401ced
jmp 0x401d3c
movq -0x40(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x3c6610
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x401d05
jmp 0x401d3c
movq -0x40(%rbp), %rax
movq 0x50(%rax), %rdi
movl -0x2c(%rbp), %esi
callq 0x3dd200
movq -0x40(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x4, %rdi
xorl %esi, %esi
callq 0x3dd200
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x20, %rdi
movl -0x1c(%rbp), %esi
callq 0x3dd200
cmpq $0x0, -0x40(%rbp)
je 0x401d4a
movq -0x40(%rbp), %rax
movb $0x0, (%rax)
movq -0x40(%rbp), %rdi
callq 0x3e9b70
movq -0x28(%rbp), %rdi
callq 0x3e9b70
movl -0x44(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)