// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/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 */
  int nFree;                          /* Initial number of pages on free-list */

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

  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]);
    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".
      */
      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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %edx, %r14d
movq %rdi, %r15
movq $0x0, -0x40(%rbp)
movq 0x18(%rdi), %r13
testq %rsi, %rsi
je 0x32dc5
movq %rsi, %rbx
movq 0x68(%rsi), %rax
incw 0x2e(%rax)
jmp 0x32dd3
movq %r15, %rdi
movl %r14d, %esi
callq 0x32d16
movq %rax, %rbx
movq %rbx, -0x48(%rbp)
movq 0x68(%r13), %rdi
callq 0xdfbf
movl %eax, -0x34(%rbp)
testl %eax, %eax
je 0x32e1f
movl %eax, %r12d
testq %rbx, %rbx
je 0x32dfb
movb $0x0, (%rbx)
movq 0x68(%rbx), %rdi
callq 0xddd3
movq -0x40(%rbp), %rax
testq %rax, %rax
je 0x32e0d
movq 0x68(%rax), %rdi
callq 0xddd3
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r14d, -0x2c(%rbp)
movq 0x50(%r13), %rax
movl 0x24(%rax), %r14d
bswapl %r14d
leal 0x1(%r14), %ecx
bswapl %ecx
movl %ecx, 0x24(%rax)
testb $0x4, 0x26(%r15)
je 0x32e6d
testq %rbx, %rbx
je 0x32f17
movq 0x68(%rbx), %rdi
callq 0xdfbf
movl %eax, -0x34(%rbp)
testl %eax, %eax
jne 0x32f58
movq 0x48(%rbx), %rax
movq 0x50(%rbx), %rdi
movl 0x30(%rax), %edx
xorl %esi, %esi
callq 0x8200
cmpb $0x0, 0x21(%r15)
je 0x32e9a
leaq -0x34(%rbp), %r12
movq %r15, %rdi
movl -0x2c(%rbp), %esi
movl $0x2, %edx
xorl %ecx, %ecx
movq %r12, %r8
callq 0x22705
movl (%r12), %r12d
testl %r12d, %r12d
jne 0x32dea
testl %r14d, %r14d
je 0x32f3b
movq 0x50(%r13), %rax
movl 0x20(%rax), %r14d
bswapl %r14d
leaq -0x40(%rbp), %rdx
movq %r15, %rdi
movl %r14d, %esi
xorl %ecx, %ecx
callq 0x20068
testl %eax, %eax
jne 0x32de7
movl %r14d, -0x30(%rbp)
movq -0x40(%rbp), %rdx
movq 0x50(%rdx), %rax
movl 0x4(%rax), %r14d
bswapl %r14d
movl 0x34(%r15), %eax
shrl $0x2, %eax
leal -0x2(%rax), %ecx
cmpl %ecx, %r14d
jbe 0x32faa
leaq 0x46920(%rip), %rsi     # 0x79814
leaq 0x4663e(%rip), %rcx     # 0x79539
movl $0xb, %r12d
movl $0xb, %edi
movl $0xd696, %edx           # imm = 0xD696
xorl %eax, %eax
callq 0xb9c7
jmp 0x32dea
leaq -0x48(%rbp), %rbx
movq %r15, %rdi
movl -0x2c(%rbp), %esi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x20068
movq (%rbx), %rbx
testl %eax, %eax
jne 0x32de7
jmp 0x32e47
movl $0x0, -0x30(%rbp)
testq %rbx, %rbx
movl -0x2c(%rbp), %r14d
je 0x32f60
movq 0x68(%rbx), %rdi
callq 0xdfbf
testl %eax, %eax
je 0x32f81
movl %eax, %r12d
jmp 0x32def
leaq -0x48(%rbp), %rbx
movq %r15, %rdi
movl %r14d, %esi
movq %rbx, %rdx
xorl %ecx, %ecx
callq 0x20068
movq (%rbx), %rbx
testl %eax, %eax
jne 0x32de7
jmp 0x32f4b
movq 0x50(%rbx), %rax
movl -0x30(%rbp), %ecx
bswapl %ecx
movl %ecx, (%rax)
movq 0x50(%rbx), %rax
movl $0x0, 0x4(%rax)
movq 0x50(%r13), %rax
bswapl %r14d
movl %r14d, 0x20(%rax)
xorl %r12d, %r12d
jmp 0x32def
addl $-0x8, %eax
cmpl %eax, %r14d
jae 0x32f42
movq 0x68(%rdx), %rdi
movq %rdx, %r13
callq 0xdfbf
testl %eax, %eax
jne 0x32de7
movq 0x50(%r13), %rax
leal 0x1(%r14), %ecx
bswapl %ecx
movl %ecx, 0x4(%rax)
movq 0x50(%r13), %rax
leal 0x8(,%r14,4), %ecx
movl -0x2c(%rbp), %esi
movl %esi, %edx
bswapl %edx
movl %edx, (%rax,%rcx)
testq %rbx, %rbx
je 0x33016
testb $0x4, 0x26(%r15)
jne 0x33016
movq 0x68(%rbx), %rax
movzwl 0x2c(%rax), %ecx
testb $0x2, %cl
je 0x33016
movq 0x20(%rax), %rdx
cmpl $0x0, 0x80(%rdx)
jne 0x33016
orl $0x20, %ecx
movw %cx, 0x2c(%rax)
movq %r15, %rdi
callq 0x33023
jmp 0x32de7