// File: /workspace/github_c_makefile_star2_O0/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
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x30(%rax)
jne 0x3bff9
movq -0x18(%rbp), %rax
movq $0x0, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x3c1b3
movq -0x10(%rbp), %rdi
callq 0x40900
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x3c1ad
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movswq 0x7a(%rcx), %rcx
movq 0xa8(%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movzbl 0x3(%rax), %eax
cmpl $0x0, %eax
jne 0x3c03f
movq -0x30(%rbp), %rax
cmpb $0x0, 0x2(%rax)
jne 0x3c04f
movq -0x30(%rbp), %rax
movzwl 0x10(%rax), %eax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x30(%rbp), %rax
cmpb $0x0, 0x3(%rax)
je 0x3c0ef
jmp 0x3c05f
movq -0x10(%rbp), %rax
movswl 0x7a(%rax), %eax
cmpl $0x0, %eax
jne 0x3c083
movq -0x20(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x3c1b3
movq -0x10(%rbp), %rdi
callq 0x40b30
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movswq 0x7a(%rcx), %rcx
movzwl 0x7c(%rax,%rcx,2), %eax
movq -0x10(%rbp), %rcx
movq -0x10(%rbp), %rdx
movswq 0x7a(%rdx), %rdx
movq 0xa8(%rcx,%rdx,8), %rcx
movzwl 0x10(%rcx), %ecx
cmpl %ecx, %eax
jge 0x3c05f
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movswq 0x7a(%rcx), %rcx
movw 0x7c(%rax,%rcx,2), %dx
addw $0x1, %dx
movw %dx, 0x7c(%rax,%rcx,2)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movswq 0x7a(%rcx), %rcx
movq 0xa8(%rax,%rcx,8), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq -0x10(%rbp), %rcx
movswq 0x7a(%rcx), %rcx
movzwl 0x7c(%rax,%rcx,2), %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq -0x30(%rbp), %rcx
movzwl 0x10(%rcx), %ecx
cmpl %ecx, %eax
jne 0x3c148
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x30(%rbp), %rax
movzbl 0x5(%rax), %eax
addl $0x8, %eax
cltq
addq %rax, %rdi
callq 0x20a50
movq -0x38(%rbp), %rdi
movl %eax, %esi
callq 0x40b80
movl %eax, -0x24(%rbp)
jmp 0x3c1a8
movq -0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x30(%rbp), %rax
movzwl 0x12(%rax), %eax
movq -0x30(%rbp), %rcx
movq 0x60(%rcx), %rcx
movl -0x28(%rbp), %edx
shll %edx
movslq %edx, %rdx
movzbl (%rcx,%rdx), %ecx
shll $0x8, %ecx
movq -0x30(%rbp), %rdx
movq 0x60(%rdx), %rdx
movl -0x28(%rbp), %esi
shll %esi
movslq %esi, %rsi
movzbl 0x1(%rdx,%rsi), %edx
orl %edx, %ecx
andl %ecx, %eax
cltq
addq %rax, %rdi
callq 0x20a50
movq -0x40(%rbp), %rdi
movl %eax, %esi
callq 0x40b80
movl %eax, -0x24(%rbp)
jmp 0x3c005
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)