// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3BtreeCount
SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
  i64 nEntry = 0;                      /* Value to return in *pnEntry */
  int rc;                              /* Return code */

  if( pCur->pgnoRoot==0 ){
    *pnEntry = 0;
    return SQLITE_OK;
  }
  rc = moveToRoot(pCur);

  /* Unless an error occurs, the following loop runs one iteration for each
  ** page in the B-Tree structure (not including overflow pages). 
  */
  while( rc==SQLITE_OK ){
    int iIdx;                          /* Index of child node in parent */
    MemPage *pPage;                    /* Current page of the b-tree */

    /* If this is a leaf page or the tree is not an int-key tree, then 
    ** this page contains countable entries. Increment the entry counter
    ** accordingly.
    */
    pPage = pCur->apPage[pCur->iPage];
    if( pPage->leaf || !pPage->intKey ){
      nEntry += pPage->nCell;
    }

    /* pPage is a leaf node. This loop navigates the cursor so that it 
    ** points to the first interior cell that it points to the parent of
    ** the next page in the tree that has not yet been visited. The
    ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
    ** of the page, or to the number of cells in the page if the next page
    ** to visit is the right-child of its parent.
    **
    ** If all pages in the tree have been visited, return SQLITE_OK to the
    ** caller.
    */
    if( pPage->leaf ){
      do {
        if( pCur->iPage==0 ){
          /* All pages of the b-tree have been visited. Return successfully. */
          *pnEntry = nEntry;
          return SQLITE_OK;
        }
        moveToParent(pCur);
      }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell );

      pCur->aiIdx[pCur->iPage]++;
      pPage = pCur->apPage[pCur->iPage];
    }

    /* Descend to the child node of the cell that the cursor currently 
    ** points at. This is the right-child if (iIdx==pPage->nCell).
    */
    iIdx = pCur->aiIdx[pCur->iPage];
    if( iIdx==pPage->nCell ){
      rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
    }else{
      rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
    }
  }

  /* An error has occurred. Return an error code. */
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rsi, %rbx
xorl %r15d, %r15d
cmpl $0x0, 0x30(%rdi)
je 0x2d18c
movq %rdi, %r14
callq 0x3096f
testl %eax, %eax
jne 0x2d191
xorl %r15d, %r15d
movzwl 0x7a(%r14), %edx
movswq %dx, %rcx
movq 0xa8(%r14,%rcx,8), %rax
cmpb $0x0, 0x3(%rax)
je 0x2d10f
movzwl 0x10(%rax), %eax
addq %rax, %r15
testw %dx, %dx
je 0x2d18c
movq %r14, %rdi
callq 0x30ae3
movzwl 0x7a(%r14), %edx
movswq %dx, %rax
movzwl 0x7c(%r14,%rax,2), %ecx
movq 0xa8(%r14,%rax,8), %rsi
cmpw 0x10(%rsi), %cx
jae 0x2d0ca
incl %ecx
movw %cx, 0x7c(%r14,%rax,2)
movswq 0x7a(%r14), %rcx
movq 0xa8(%r14,%rcx,8), %rax
jmp 0x2d11c
cmpb $0x0, 0x2(%rax)
jne 0x2d11c
movzwl 0x10(%rax), %edx
addq %rdx, %r15
movzwl 0x7c(%r14,%rcx,2), %ecx
cmpw 0x10(%rax), %cx
jne 0x2d139
movq 0x50(%rax), %rcx
movzbl 0x5(%rax), %eax
addq %rcx, %rax
addq $0xb, %rax
jmp 0x2d15c
movzwl 0x12(%rax), %edx
movq 0x50(%rax), %rsi
movq 0x60(%rax), %rax
movzbl (%rax,%rcx,2), %edi
shll $0x8, %edi
movzbl 0x1(%rax,%rcx,2), %eax
orl %edi, %eax
andl %edx, %eax
addq %rsi, %rax
addq $0x3, %rax
movzbl -0x3(%rax), %ecx
shll $0x18, %ecx
movzbl -0x2(%rax), %edx
shll $0x10, %edx
orl %ecx, %edx
movzbl -0x1(%rax), %ecx
shll $0x8, %ecx
orl %edx, %ecx
movzbl (%rax), %esi
orl %ecx, %esi
movq %r14, %rdi
callq 0x30b22
testl %eax, %eax
je 0x2d0ac
jmp 0x2d191
movq %r15, (%rbx)
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq