// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3BtreeMovetoUnpacked
SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
  BtCursor *pCur,          /* The cursor to be moved */
  UnpackedRecord *pIdxKey, /* Unpacked index key */
  i64 intKey,              /* The table key */
  int biasRight,           /* If true, bias the search to the high end */
  int *pRes                /* Write search results here */
){
  int rc;

  assert( cursorHoldsMutex(pCur) );
  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  assert( pRes );
  assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );

  /* If the cursor is already positioned at the point we are trying
  ** to move to, then just return without doing any work */
  if( pCur->eState==CURSOR_VALID && pCur->validNKey 
   && pCur->apPage[0]->intKey 
  ){
    if( pCur->info.nKey==intKey ){
      *pRes = 0;
      return SQLITE_OK;
    }
    if( pCur->atLast && pCur->info.nKey<intKey ){
      *pRes = -1;
      return SQLITE_OK;
    }
  }

  rc = moveToRoot(pCur);
  if( rc ){
    return rc;
  }
  assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
  assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
  assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
  if( pCur->eState==CURSOR_INVALID ){
    *pRes = -1;
    assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
    return SQLITE_OK;
  }
  assert( pCur->apPage[0]->intKey || pIdxKey );
  for(;;){
    int lwr, upr, idx;
    Pgno chldPg;
    MemPage *pPage = pCur->apPage[pCur->iPage];
    int c;

    /* pPage->nCell must be greater than zero. If this is the root-page
    ** the cursor would have been INVALID above and this for(;;) loop
    ** not run. If this is not the root-page, then the moveToChild() routine
    ** would have already detected db corruption. Similarly, pPage must
    ** be the right kind (index or table) of b-tree page. Otherwise
    ** a moveToChild() or moveToRoot() call would have detected corruption.  */
    assert( pPage->nCell>0 );
    assert( pPage->intKey==(pIdxKey==0) );
    lwr = 0;
    upr = pPage->nCell-1;
    if( biasRight ){
      pCur->aiIdx[pCur->iPage] = (u16)(idx = upr);
    }else{
      pCur->aiIdx[pCur->iPage] = (u16)(idx = (upr+lwr)/2);
    }
    for(;;){
      u8 *pCell;                          /* Pointer to current cell in pPage */

      assert( idx==pCur->aiIdx[pCur->iPage] );
      pCur->info.nSize = 0;
      pCell = findCell(pPage, idx) + pPage->childPtrSize;
      if( pPage->intKey ){
        i64 nCellKey;
        if( pPage->hasData ){
          u32 dummy;
          pCell += getVarint32(pCell, dummy);
        }
        getVarint(pCell, (u64*)&nCellKey);
        if( nCellKey==intKey ){
          c = 0;
        }else if( nCellKey<intKey ){
          c = -1;
        }else{
          assert( nCellKey>intKey );
          c = +1;
        }
        pCur->validNKey = 1;
        pCur->info.nKey = nCellKey;
      }else{
        /* The maximum supported page-size is 65536 bytes. This means that
        ** the maximum number of record bytes stored on an index B-Tree
        ** page is less than 16384 bytes and may be stored as a 2-byte
        ** varint. This information is used to attempt to avoid parsing 
        ** the entire cell by checking for the cases where the record is 
        ** stored entirely within the b-tree page by inspecting the first 
        ** 2 bytes of the cell.
        */
        int nCell = pCell[0];
        if( nCell<=pPage->max1bytePayload
         /* && (pCell+nCell)<pPage->aDataEnd */
        ){
          /* This branch runs if the record-size field of the cell is a
          ** single byte varint and the record fits entirely on the main
          ** b-tree page.  */
          testcase( pCell+nCell+1==pPage->aDataEnd );
          c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
        }else if( !(pCell[1] & 0x80) 
          && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
          /* && (pCell+nCell+2)<=pPage->aDataEnd */
        ){
          /* The record-size field is a 2 byte varint and the record 
          ** fits entirely on the main b-tree page.  */
          testcase( pCell+nCell+2==pPage->aDataEnd );
          c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
        }else{
          /* The record flows over onto one or more overflow pages. In
          ** this case the whole cell needs to be parsed, a buffer allocated
          ** and accessPayload() used to retrieve the record into the
          ** buffer before VdbeRecordCompare() can be called. */
          void *pCellKey;
          u8 * const pCellBody = pCell - pPage->childPtrSize;
          btreeParseCellPtr(pPage, pCellBody, &pCur->info);
          nCell = (int)pCur->info.nKey;
          pCellKey = sqlite3Malloc( nCell );
          if( pCellKey==0 ){
            rc = SQLITE_NOMEM;
            goto moveto_finish;
          }
          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
          if( rc ){
            sqlite3_free(pCellKey);
            goto moveto_finish;
          }
          c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
          sqlite3_free(pCellKey);
        }
      }
      if( c==0 ){
        if( pPage->intKey && !pPage->leaf ){
          lwr = idx;
          break;
        }else{
          *pRes = 0;
          rc = SQLITE_OK;
          goto moveto_finish;
        }
      }
      if( c<0 ){
        lwr = idx+1;
      }else{
        upr = idx-1;
      }
      if( lwr>upr ){
        break;
      }
      pCur->aiIdx[pCur->iPage] = (u16)(idx = (lwr+upr)/2);
    }
    assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
    assert( pPage->isInit );
    if( pPage->leaf ){
      chldPg = 0;
    }else if( lwr>=pPage->nCell ){
      chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
    }else{
      chldPg = get4byte(findCell(pPage, lwr));
    }
    if( chldPg==0 ){
      assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
      *pRes = c;
      rc = SQLITE_OK;
      goto moveto_finish;
    }
    pCur->aiIdx[pCur->iPage] = (u16)lwr;
    pCur->info.nSize = 0;
    pCur->validNKey = 0;
    rc = moveToChild(pCur, chldPg);
    if( rc ) goto moveto_finish;
  }
moveto_finish:
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %ecx, -0x44(%rbp)
movq %rdx, %r12
movq %rsi, -0x40(%rbp)
cmpb $0x1, 0x77(%rdi)
jne 0x2a65f
cmpb $0x0, 0x76(%rdi)
je 0x2a65f
movq 0xa8(%rdi), %rax
cmpb $0x0, 0x2(%rax)
je 0x2a65f
cmpq %r12, 0x40(%rdi)
jne 0x2a64e
andl $0x0, (%r8)
jmp 0x2a908
jge 0x2a65f
cmpb $0x0, 0x75(%rdi)
je 0x2a65f
orl $-0x1, (%r8)
jmp 0x2a908
movq %r8, -0x38(%rbp)
movq %rdi, %rbx
callq 0x2bba7
movl %eax, %r15d
testl %eax, %eax
jne 0x2a90b
cmpb $0x0, 0x77(%rbx)
je 0x2a8dc
leaq 0x40(%rbx), %rax
movq %rax, -0x68(%rbp)
movq %rbx, %r13
movq %r12, -0x60(%rbp)
movq %rbx, -0x30(%rbp)
movswq 0x7a(%r13), %rcx
movq 0xa8(%r13,%rcx,8), %r14
movzwl 0x10(%r14), %edi
decl %edi
movl %edi, %eax
cltd
pushq $0x2
popq %rsi
idivl %esi
movl %eax, %ebx
cmpl $0x0, -0x44(%rbp)
movq %rdi, -0x58(%rbp)
cmovnel %edi, %ebx
movw %bx, 0x7c(%r13,%rcx,2)
movq $0x0, -0x50(%rbp)
andw $0x0, 0x5e(%r13)
movzwl 0x12(%r14), %eax
movq 0x60(%r14), %rcx
movslq %ebx, %rdx
movzbl (%rcx,%rdx,2), %edi
shll $0x8, %edi
movzbl 0x1(%rcx,%rdx,2), %esi
orl %edi, %esi
andl %eax, %esi
addq 0x50(%r14), %rsi
movzbl 0x6(%r14), %r15d
addq %rsi, %r15
cmpb $0x0, 0x2(%r14)
je 0x2a75b
cmpb $0x0, 0x4(%r14)
je 0x2a721
cmpb $0x0, (%r15)
pushq $0x1
popq %rax
jns 0x2a71e
movq %r15, %rdi
leaq -0x74(%rbp), %rsi
callq 0x1e561
movzbl %al, %eax
addq %rax, %r15
movq %r15, %rdi
leaq -0x70(%rbp), %rsi
callq 0x1e5d0
movq -0x70(%rbp), %rcx
xorl %eax, %eax
movq -0x30(%rbp), %r13
movb $0x1, 0x76(%r13)
movq %rcx, 0x40(%r13)
cmpq %r12, %rcx
setge %cl
je 0x2a85a
movb %cl, %al
leal -0x1(,%rax,2), %r15d
jmp 0x2a804
movzbl (%r15), %edi
leaq 0x1(%r15), %rax
cmpb 0x7(%r14), %dil
jbe 0x2a7e3
movsbl (%rax), %eax
testl %eax, %eax
js 0x2a781
andl $0x7f, %edi
shll $0x7, %edi
orl %eax, %edi
movzwl 0x8(%r14), %eax
cmpl %eax, %edi
jbe 0x2a7e8
movq %r14, %rdi
movq -0x68(%rbp), %r15
movq %r15, %rdx
callq 0x1e436
movl (%r15), %r13d
movl %r13d, %edi
callq 0x9c40
testq %rax, %rax
je 0x2a8e5
movq %rax, %r12
movq -0x30(%rbp), %rdi
xorl %esi, %esi
movl %r13d, %edx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x1e723
testl %eax, %eax
jne 0x2a8eb
movl %r13d, %edi
movq %r12, %rsi
movq -0x40(%rbp), %rdx
callq 0x2bfb3
movl %eax, %r15d
movq %r12, %rdi
callq 0x9c7b
movq -0x60(%rbp), %r12
jmp 0x2a7fb
movq %rax, %rsi
jmp 0x2a7ef
addq $0x2, %r15
movq %r15, %rsi
movq -0x40(%rbp), %rdx
callq 0x2bfb3
movl %eax, %r15d
testl %r15d, %r15d
movq -0x30(%rbp), %r13
je 0x2a84f
testl %r15d, %r15d
js 0x2a813
decl %ebx
movl %ebx, %eax
movq -0x50(%rbp), %rcx
jmp 0x2a81b
incl %ebx
movl %ebx, %ecx
movq -0x58(%rbp), %rax
cmpl %eax, %ecx
jg 0x2a841
movq %rax, -0x58(%rbp)
movq %rcx, -0x50(%rbp)
addl %ecx, %eax
cltd
pushq $0x2
popq %rcx
idivl %ecx
movl %eax, %ebx
movswq 0x7a(%r13), %rax
movw %bx, 0x7c(%r13,%rax,2)
jmp 0x2a6ca
cmpb $0x0, 0x3(%r14)
movl %ecx, %ebx
je 0x2a868
jmp 0x2a8f8
cmpb $0x0, 0x2(%r14)
je 0x2a901
xorl %r15d, %r15d
cmpb $0x0, 0x3(%r14)
jne 0x2a901
movzwl 0x10(%r14), %eax
movq 0x50(%r14), %rdi
cmpl %eax, %ebx
jge 0x2a896
movzwl 0x12(%r14), %eax
movq 0x60(%r14), %rcx
movslq %ebx, %rdx
movzbl (%rcx,%rdx,2), %esi
shll $0x8, %esi
movzbl 0x1(%rcx,%rdx,2), %ecx
orl %esi, %ecx
andl %eax, %ecx
addq %rcx, %rdi
jmp 0x2a8a2
movzbl 0x5(%r14), %eax
addq %rax, %rdi
addq $0x8, %rdi
callq 0x180a3
testl %eax, %eax
movq -0x30(%rbp), %r13
je 0x2a8f8
movswq 0x7a(%r13), %rcx
movw %bx, 0x7c(%r13,%rcx,2)
andw $0x0, 0x5e(%r13)
movb $0x0, 0x76(%r13)
movq %r13, %rdi
movl %eax, %esi
callq 0x2bcf4
movl %eax, %r15d
testl %eax, %eax
je 0x2a693
jmp 0x2a90b
movq -0x38(%rbp), %rax
orl $-0x1, (%rax)
jmp 0x2a908
pushq $0x7
popq %r15
jmp 0x2a90b
movl %eax, %r15d
movq %r12, %rdi
callq 0x9c7b
jmp 0x2a90b
movq -0x38(%rbp), %rax
movl %r15d, (%rax)
jmp 0x2a908
movq -0x38(%rbp), %rax
andl $0x0, (%rax)
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq