// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
movq %r8, -0x40(%rbp)
movl %ecx, -0x4c(%rbp)
movq %rdx, -0x58(%rbp)
movq %rsi, -0x48(%rbp)
movq %rdi, %r15
cmpb $0x1, 0x77(%rdi)
jne 0x2cf38
cmpb $0x0, 0x76(%r15)
je 0x2cf38
movq 0xa8(%r15), %rax
cmpb $0x0, 0x2(%rax)
je 0x2cf38
xorl %eax, %eax
movq -0x58(%rbp), %rcx
cmpq %rcx, 0x40(%r15)
je 0x2d281
jge 0x2cf38
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpb $0x0, 0x75(%r15)
jne 0x2d281
movq %r15, %rdi
callq 0x3006b
movl %eax, -0x2c(%rbp)
testl %eax, %eax
jne 0x2d28e
cmpb $0x0, 0x77(%r15)
je 0x2d27c
leaq 0x40(%r15), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x2c(%rbp)
movswq 0x7a(%r15), %rax
movq 0xa8(%r15,%rax,8), %r12
movzwl 0x10(%r12), %ecx
movl %ecx, %esi
decl %esi
movl %esi, %edx
shrl $0x1f, %edx
leal (%rcx,%rdx), %r13d
decl %r13d
sarl %r13d
cmpl $0x0, -0x4c(%rbp)
cmovnel %esi, %r13d
movw %r13w, 0x7c(%r15,%rax,2)
movq $0x0, -0x38(%rbp)
movq %rsi, -0x60(%rbp)
movq %r15, %r14
movw $0x0, 0x5e(%r15)
movzwl 0x12(%r12), %eax
movq 0x60(%r12), %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(%r12), %rsi
movzbl 0x6(%r12), %r15d
addq %rsi, %r15
cmpb $0x0, 0x2(%r12)
je 0x2d008
cmpb $0x0, 0x4(%r12)
je 0x2d0a1
movsbl (%r15), %eax
testl %eax, %eax
js 0x2d08f
movl %eax, -0x50(%rbp)
movl $0x1, %eax
jmp 0x2d09e
movzbl (%r15), %edi
leaq 0x1(%r15), %rax
cmpb 0x7(%r12), %dil
jbe 0x2d08a
movsbl (%rax), %eax
testl %eax, %eax
js 0x2d034
andl $0x7f, %edi
shll $0x7, %edi
orl %eax, %edi
movzwl 0x8(%r12), %eax
cmpl %eax, %edi
jbe 0x2d0da
movl %r9d, -0x30(%rbp)
movq %r12, %rdi
movq -0x68(%rbp), %rbx
movq %rbx, %rdx
callq 0x21585
movl (%rbx), %edi
movl %edi, -0x2c(%rbp)
callq 0xa2a2
movl $0x6, %ebx
testq %rax, %rax
je 0x2d0f3
movq %rax, %r15
movq %r14, %rdi
xorl %esi, %esi
movl -0x2c(%rbp), %edx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x2186f
testl %eax, %eax
je 0x2d102
movl %eax, -0x2c(%rbp)
movq %r15, %rdi
callq 0xa2e8
jmp 0x2d0fa
movq %rax, %rsi
jmp 0x2d0e1
movq %r15, %rdi
leaq -0x50(%rbp), %rsi
callq 0x216ad
movzbl %al, %eax
addq %rax, %r15
movq %r15, %rdi
leaq -0x70(%rbp), %rsi
callq 0x2171c
movq -0x70(%rbp), %rax
xorl %ecx, %ecx
cmpq -0x58(%rbp), %rax
setge %cl
leal -0x1(%rcx,%rcx), %r9d
movl $0x0, %ecx
cmovel %ecx, %r9d
movq %r14, %r15
movb $0x1, 0x76(%r14)
movq %rax, 0x40(%r14)
movq -0x60(%rbp), %rsi
jmp 0x2d134
addq $0x2, %r15
movq %r15, %rsi
movq -0x48(%rbp), %rdx
callq 0x30545
movl %eax, %r9d
xorl %ebx, %ebx
movb $0x1, %al
jmp 0x2d129
movl $0x7, -0x2c(%rbp)
xorl %eax, %eax
movl -0x30(%rbp), %r9d
jmp 0x2d129
movl -0x2c(%rbp), %edi
movq %r15, %rsi
movq -0x48(%rbp), %rdx
callq 0x30545
movl %eax, %ebx
movq %r15, %rdi
callq 0xa2e8
movl %ebx, %r9d
xorl %ebx, %ebx
movb $0x1, %al
movl $0x0, -0x2c(%rbp)
testb %al, %al
movq %r14, %r15
movq -0x60(%rbp), %rsi
je 0x2d1ae
testl %r9d, %r9d
je 0x2d145
js 0x2d170
leal -0x1(%r13), %esi
movq -0x38(%rbp), %rax
jmp 0x2d174
cmpb $0x0, 0x2(%r12)
je 0x2d155
cmpb $0x0, 0x3(%r12)
je 0x2d19f
movq -0x40(%rbp), %rax
movl $0x0, (%rax)
xorl %r9d, %r9d
movl $0x6, %ebx
movl $0x0, -0x2c(%rbp)
jmp 0x2d1ae
leal 0x1(%r13), %eax
movl $0x4, %ebx
cmpl %esi, %eax
movq %rax, -0x38(%rbp)
jg 0x2d1ae
addl %esi, %eax
movl %eax, %r13d
shrl $0x1f, %r13d
addl %eax, %r13d
sarl %r13d
movswq 0x7a(%r15), %rax
movw %r13w, 0x7c(%r15,%rax,2)
xorl %ebx, %ebx
jmp 0x2d1ae
xorl %r9d, %r9d
movl $0x4, %ebx
movl %r13d, %eax
movq %rax, -0x38(%rbp)
testl %ebx, %ebx
je 0x2cfa1
cmpl $0x4, %ebx
jne 0x2d200
xorl %esi, %esi
cmpb $0x0, 0x3(%r12)
movq -0x38(%rbp), %r8
jne 0x2d22f
movzwl 0x10(%r12), %ecx
movq 0x50(%r12), %rax
cmpl %ecx, %r8d
jge 0x2d204
movzwl 0x12(%r12), %ecx
movq 0x60(%r12), %rdx
movslq %r8d, %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 0x2d211
xorl %eax, %eax
jmp 0x2d272
movzbl 0x5(%r12), %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
testl %esi, %esi
je 0x2d262
movswq 0x7a(%r15), %rax
movw %r8w, 0x7c(%r15,%rax,2)
movw $0x0, 0x5e(%r15)
movb $0x0, 0x76(%r15)
movq %r15, %rdi
movl %r9d, %ebx
callq 0x30218
movl %ebx, %r9d
movl %eax, -0x2c(%rbp)
testl %eax, %eax
sete %al
jmp 0x2d272
movq -0x40(%rbp), %rax
movl %r9d, (%rax)
xorl %eax, %eax
movl $0x0, -0x2c(%rbp)
testb %al, %al
jne 0x2cf65
jmp 0x2d28e
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq -0x40(%rbp), %rcx
movl %eax, (%rcx)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq