// File: /workspace/github_c_makefile_star2_O3/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, -0x3c(%rbp)
movq %rdx, %r15
movq %rsi, -0x38(%rbp)
movq %rdi, %r12
cmpb $0x1, 0x77(%rdi)
jne 0x2d7fd
cmpb $0x0, 0x76(%r12)
je 0x2d7fd
movq 0xa8(%r12), %rax
cmpb $0x0, 0x2(%rax)
je 0x2d7fd
cmpq %r15, 0x40(%r12)
jne 0x2d7e7
movl $0x0, (%r8)
jmp 0x2dad3
jge 0x2d7fd
cmpb $0x0, 0x75(%r12)
je 0x2d7fd
movl $0xffffffff, (%r8)      # imm = 0xFFFFFFFF
jmp 0x2dad3
movq %r8, -0x30(%rbp)
movq %r12, %rdi
callq 0x3096f
movl %eax, %r14d
testl %eax, %eax
jne 0x2dad6
cmpb $0x0, 0x77(%r12)
je 0x2da9f
leaq 0x40(%r12), %rax
movq %rax, -0x68(%rbp)
movq %r15, -0x60(%rbp)
movq %r12, -0x48(%rbp)
movswq 0x7a(%r12), %rax
movq 0xa8(%r12,%rax,8), %rbx
movzwl 0x10(%rbx), %ecx
movl %ecx, %esi
decl %esi
movl %esi, %edx
shrl $0x1f, %edx
leal (%rcx,%rdx), %r13d
decl %r13d
sarl %r13d
cmpl $0x0, -0x3c(%rbp)
movq %rsi, -0x58(%rbp)
cmovnel %esi, %r13d
movw %r13w, 0x7c(%r12,%rax,2)
movq $0x0, -0x50(%rbp)
movw $0x0, 0x5e(%r12)
movzwl 0x12(%rbx), %eax
movq 0x60(%rbx), %rcx
movslq %r13d, %rdx
movzbl (%rcx,%rdx,2), %edi
shll $0x8, %edi
movzbl 0x1(%rcx,%rdx,2), %esi
orl %edi, %esi
andl %eax, %esi
addq 0x50(%rbx), %rsi
movzbl 0x6(%rbx), %r14d
addq %rsi, %r14
cmpb $0x0, 0x2(%rbx)
je 0x2d900
cmpb $0x0, 0x4(%rbx)
je 0x2d8c8
movl $0x1, %eax
cmpb $0x0, (%r14)
jns 0x2d8c5
movq %r14, %rdi
leaq -0x74(%rbp), %rsi
callq 0x2134f
movzbl %al, %eax
addq %rax, %r14
movq %r14, %rdi
leaq -0x70(%rbp), %rsi
callq 0x213be
movq -0x70(%rbp), %rcx
xorl %eax, %eax
movb $0x1, 0x76(%r12)
movq %rcx, 0x40(%r12)
cmpq %r15, %rcx
setge %cl
je 0x2da07
movb %cl, %al
leal -0x1(,%rax,2), %r14d
jmp 0x2d9a8
movzbl (%r14), %edi
leaq 0x1(%r14), %rax
cmpb 0x7(%rbx), %dil
jbe 0x2d98b
movsbl (%rax), %eax
testl %eax, %eax
js 0x2d925
andl $0x7f, %edi
shll $0x7, %edi
orl %eax, %edi
movzwl 0x8(%rbx), %eax
cmpl %eax, %edi
jbe 0x2d990
movq %rbx, %rdi
movq -0x68(%rbp), %r14
movq %r14, %rdx
callq 0x2121e
movl (%r14), %r15d
movl %r15d, %edi
callq 0xa38c
testq %rax, %rax
je 0x2daab
movq %rax, %r12
movq -0x48(%rbp), %rdi
xorl %esi, %esi
movl %r15d, %edx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x21511
testl %eax, %eax
jne 0x2dab3
movl %r15d, %edi
movq %r12, %rsi
movq -0x38(%rbp), %rdx
callq 0x30e19
movl %eax, %r14d
movq %r12, %rdi
callq 0xa3c7
movq -0x60(%rbp), %r15
movq -0x48(%rbp), %r12
jmp 0x2d9a3
movq %rax, %rsi
jmp 0x2d997
addq $0x2, %r14
movq %r14, %rsi
movq -0x38(%rbp), %rdx
callq 0x30e19
movl %eax, %r14d
testl %r14d, %r14d
je 0x2d9fd
testl %r14d, %r14d
js 0x2d9b9
decl %r13d
movl %r13d, %eax
movq -0x50(%rbp), %rcx
jmp 0x2d9c3
incl %r13d
movl %r13d, %ecx
movq -0x58(%rbp), %rax
cmpl %eax, %ecx
jg 0x2d9ef
movq %rax, -0x58(%rbp)
movq %rcx, -0x50(%rbp)
addl %ecx, %eax
movl %eax, %r13d
shrl $0x1f, %r13d
addl %eax, %r13d
sarl %r13d
movswq 0x7a(%r12), %rax
movw %r13w, 0x7c(%r12,%rax,2)
jmp 0x2d870
cmpb $0x0, 0x3(%rbx)
movl %ecx, %r13d
je 0x2da14
jmp 0x2dac0
cmpb $0x0, 0x2(%rbx)
je 0x2dac9
xorl %r14d, %r14d
cmpb $0x0, 0x3(%rbx)
jne 0x2dac9
movzwl 0x10(%rbx), %ecx
movq 0x50(%rbx), %rax
cmpl %ecx, %r13d
jge 0x2da45
movzwl 0x12(%rbx), %ecx
movq 0x60(%rbx), %rdx
movslq %r13d, %rsi
movzbl (%rdx,%rsi,2), %edi
shll $0x8, %edi
movzbl 0x1(%rdx,%rsi,2), %edx
orl %edi, %edx
andl %ecx, %edx
addq %rdx, %rax
addq $0x3, %rax
jmp 0x2da50
movzbl 0x5(%rbx), %ecx
addq %rcx, %rax
addq $0xb, %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
je 0x2dac0
movswq 0x7a(%r12), %rax
movw %r13w, 0x7c(%r12,%rax,2)
movw $0x0, 0x5e(%r12)
movb $0x0, 0x76(%r12)
movq %r12, %rdi
callq 0x30b22
movl %eax, %r14d
testl %eax, %eax
je 0x2d831
jmp 0x2dad6
movq -0x30(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
jmp 0x2dad3
movl $0x7, %r14d
jmp 0x2dad6
movl %eax, %r14d
movq %r12, %rdi
callq 0xa3c7
jmp 0x2dad6
movq -0x30(%rbp), %rax
movl %r14d, (%rax)
jmp 0x2dad3
movq -0x30(%rbp), %rax
movl $0x0, (%rax)
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq