// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/sqlite-3.33.0/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;
  RecordCompare xRecordCompare;

  assert( cursorOwnsBtShared(pCur) );
  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
  assert( pRes );
  assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
  assert( pCur->eState!=CURSOR_VALID || (pIdxKey==0)==(pCur->curIntKey!=0) );

  /* If the cursor is already positioned at the point we are trying
  ** to move to, then just return without doing any work */
  if( pIdxKey==0
   && pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0
  ){
    if( pCur->info.nKey==intKey ){
      *pRes = 0;
      return SQLITE_OK;
    }
    if( pCur->info.nKey<intKey ){
      if( (pCur->curFlags & BTCF_AtLast)!=0 ){
        *pRes = -1;
        return SQLITE_OK;
      }
      /* If the requested key is one more than the previous key, then
      ** try to get there using sqlite3BtreeNext() rather than a full
      ** binary search.  This is an optimization only.  The correct answer
      ** is still obtained without this case, only a little more slowely */
      if( pCur->info.nKey+1==intKey ){
        *pRes = 0;
        rc = sqlite3BtreeNext(pCur, 0);
        if( rc==SQLITE_OK ){
          getCellInfo(pCur);
          if( pCur->info.nKey==intKey ){
            return SQLITE_OK;
          }
        }else if( rc==SQLITE_DONE ){
          rc = SQLITE_OK;
        }else{
          return rc;
        }
      }
    }
  }

  if( pIdxKey ){
    xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
    pIdxKey->errCode = 0;
    assert( pIdxKey->default_rc==1
         || pIdxKey->default_rc==0
         || pIdxKey->default_rc==-1
    );
  }else{
    xRecordCompare = 0; /* All keys are integers */
  }

  rc = moveToRoot(pCur);
  if( rc ){
    if( rc==SQLITE_EMPTY ){
      assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
      *pRes = -1;
      return SQLITE_OK;
    }
    return rc;
  }
  assert( pCur->pPage );
  assert( pCur->pPage->isInit );
  assert( pCur->eState==CURSOR_VALID );
  assert( pCur->pPage->nCell > 0 );
  assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
  assert( pCur->curIntKey || pIdxKey );
  for(;;){
    int lwr, upr, idx, c;
    Pgno chldPg;
    MemPage *pPage = pCur->pPage;
    u8 *pCell;                          /* Pointer to current cell in pPage */

    /* 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;
    assert( biasRight==0 || biasRight==1 );
    idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
    pCur->ix = (u16)idx;
    if( xRecordCompare==0 ){
      for(;;){
        i64 nCellKey;
        pCell = findCellPastPtr(pPage, idx);
        if( pPage->intKeyLeaf ){
          while( 0x80 <= *(pCell++) ){
            if( pCell>=pPage->aDataEnd ){
              return SQLITE_CORRUPT_PAGE(pPage);
            }
          }
        }
        getVarint(pCell, (u64*)&nCellKey);
        if( nCellKey<intKey ){
          lwr = idx+1;
          if( lwr>upr ){ c = -1; break; }
        }else if( nCellKey>intKey ){
          upr = idx-1;
          if( lwr>upr ){ c = +1; break; }
        }else{
          assert( nCellKey==intKey );
          pCur->ix = (u16)idx;
          if( !pPage->leaf ){
            lwr = idx;
            goto moveto_next_layer;
          }else{
            pCur->curFlags |= BTCF_ValidNKey;
            pCur->info.nKey = nCellKey;
            pCur->info.nSize = 0;
            *pRes = 0;
            return SQLITE_OK;
          }
        }
        assert( lwr+upr>=0 );
        idx = (lwr+upr)>>1;  /* idx = (lwr+upr)/2; */
      }
    }else{
      for(;;){
        int nCell;  /* Size of the pCell cell in bytes */
        pCell = findCellPastPtr(pPage, idx);

        /* 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.
        */
        nCell = pCell[0];
        if( nCell<=pPage->max1bytePayload ){
          /* 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 = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
        }else if( !(pCell[1] & 0x80)
          && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
        ){
          /* 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 = xRecordCompare(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.
          **
          ** If the record is corrupt, the xRecordCompare routine may read
          ** up to two varints past the end of the buffer. An extra 18
          ** bytes of padding is allocated at the end of the buffer in
          ** case this happens.  */
          void *pCellKey;
          u8 * const pCellBody = pCell - pPage->childPtrSize;
          const int nOverrun = 18;  /* Size of the overrun padding */
          pPage->xParseCell(pPage, pCellBody, &pCur->info);
          nCell = (int)pCur->info.nKey;
          testcase( nCell<0 );   /* True if key size is 2^32 or more */
          testcase( nCell==0 );  /* Invalid key size:  0x80 0x80 0x00 */
          testcase( nCell==1 );  /* Invalid key size:  0x80 0x80 0x01 */
          testcase( nCell==2 );  /* Minimum legal index key size */
          if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
            rc = SQLITE_CORRUPT_PAGE(pPage);
            goto moveto_finish;
          }
          pCellKey = sqlite3Malloc( nCell+nOverrun );
          if( pCellKey==0 ){
            rc = SQLITE_NOMEM_BKPT;
            goto moveto_finish;
          }
          pCur->ix = (u16)idx;
          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
          memset(((u8*)pCellKey)+nCell,0,nOverrun); /* Fix uninit warnings */
          pCur->curFlags &= ~BTCF_ValidOvfl;
          if( rc ){
            sqlite3_free(pCellKey);
            goto moveto_finish;
          }
          c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
          sqlite3_free(pCellKey);
        }
        assert(
            (pIdxKey->errCode!=SQLITE_CORRUPT || c==0)
         && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed)
        );
        if( c<0 ){
          lwr = idx+1;
        }else if( c>0 ){
          upr = idx-1;
        }else{
          assert( c==0 );
          *pRes = 0;
          rc = SQLITE_OK;
          pCur->ix = (u16)idx;
          if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT;
          goto moveto_finish;
        }
        if( lwr>upr ) break;
        assert( lwr+upr>=0 );
        idx = (lwr+upr)>>1;  /* idx = (lwr+upr)/2 */
      }
    }
    assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
    assert( pPage->isInit );
    if( pPage->leaf ){
      assert( pCur->ix<pCur->pPage->nCell );
      pCur->ix = (u16)idx;
      *pRes = c;
      rc = SQLITE_OK;
      goto moveto_finish;
    }
moveto_next_layer:
    if( lwr>=pPage->nCell ){
      chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
    }else{
      chldPg = get4byte(findCell(pPage, lwr));
    }
    pCur->ix = (u16)lwr;
    rc = moveToChild(pCur, chldPg);
    if( rc ) break;
  }
moveto_finish:
  pCur->info.nSize = 0;
  assert( (pCur->curFlags & BTCF_ValidOvfl)==0 );
  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, %rbx
movl %ecx, %r14d
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %r13
testq %rsi, %rsi
je 0x242e58
movq (%r15), %rax
leaq 0x303c(%rip), %rcx      # 0x245e6a
movq %rcx, 0x10(%rsp)
cmpw $0xd, 0x8(%rax)
ja 0x242eb9
movq 0x8(%r15), %rcx
movzwl 0x8(%rcx), %ecx
movq 0x18(%rax), %rdx
movb (%rdx), %dl
testb %dl, %dl
je 0x242e78
testb $0x2, %dl
jne 0x242eb9
movb $-0x1, %dl
movb $0x1, %sil
jmp 0x242e7d
cmpb $0x0, (%r13)
jne 0x242e6d
movzbl 0x1(%r13), %ecx
testb $0x2, %cl
jne 0x2431de
movq $0x0, 0x10(%rsp)
jmp 0x242ebe
movb $0x1, %dl
movb $-0x1, %sil
movb %sil, 0x14(%r15)
movb %dl, 0x15(%r15)
testb $0x4, %cl
jne 0x242ead
testb $0x39, %cl
jne 0x242eb9
cmpq $0x0, 0x20(%rax)
leaq 0x30b7(%rip), %rax      # 0x245f52
leaq 0x2fc8(%rip), %rcx      # 0x245e6a
cmoveq %rax, %rcx
movq %rcx, 0x10(%rsp)
jmp 0x242eb9
leaq 0x2fbd(%rip), %rax      # 0x245e71
movq %rax, 0x10(%rsp)
movb $0x0, 0x13(%r15)
movq %r13, %rdi
callq 0x245470
cmpl $0x10, %eax
je 0x24325c
testl %eax, %eax
jne 0x24328c
movq %r12, 0x50(%rsp)
movq %r15, 0x18(%rsp)
movq %rbx, 0x28(%rsp)
pushq $0x1
popq %rcx
subl %r14d, %ecx
leaq 0x30(%r13), %rax
movq %rax, 0x58(%rsp)
movq 0x10(%rsp), %rbx
movl %ecx, 0x3c(%rsp)
movq 0x88(%r13), %r12
movzwl 0x18(%r12), %eax
leal -0x1(%rax), %ebp
movl %ebp, %r15d
sarl %cl, %r15d
movw %r15w, 0x56(%r13)
testq %rbx, %rbx
je 0x243088
movq %rbp, 0x20(%rsp)
xorl %r14d, %r14d
movq 0x60(%r12), %rax
movq 0x68(%r12), %rsi
movzwl 0x1a(%r12), %edx
movslq %r15d, %rcx
movzbl (%rax,%rcx,2), %edi
shll $0x8, %edi
movzbl 0x1(%rax,%rcx,2), %ecx
orl %edi, %ecx
andl %edx, %ecx
leaq (%rsi,%rcx), %rax
incq %rax
movzbl -0x1(%rax), %edi
cmpb 0xb(%r12), %dil
jbe 0x243043
addq %rcx, %rsi
movsbl (%rax), %eax
testl %eax, %eax
js 0x242f83
andl $0x7f, %edi
shll $0x7, %edi
orl %eax, %edi
movzwl 0xe(%r12), %eax
cmpl %eax, %edi
jbe 0x243048
movzbl 0xa(%r12), %eax
subq %rax, %rsi
movq %r12, %rdi
movq 0x58(%rsp), %rbx
movq %rbx, %rdx
callq *0x80(%r12)
movq (%rbx), %rbp
cmpl $0x2, %ebp
jl 0x2431ec
movq 0x20(%r13), %rcx
movl %ebp, %eax
xorl %edx, %edx
divl 0x38(%rcx)
cmpl 0x40(%rcx), %eax
ja 0x2431ec
movq %r14, 0x30(%rsp)
leal 0x12(%rbp), %edi
callq 0x21f6df
testq %rax, %rax
je 0x243263
movq %rax, %r14
movw %r15w, 0x56(%r13)
movq %r13, %rdi
xorl %esi, %esi
movl %ebp, %edx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x2371d5
movq %r13, %rcx
movl %eax, %r13d
movl %ebp, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorps %xmm0, %xmm0
movups %xmm0, (%r14,%rax)
andw $0x0, 0x10(%r14,%rax)
movq %rcx, %rbx
andb $-0x5, 0x1(%rcx)
testl %r13d, %r13d
jne 0x243268
movl %ebp, %edi
movq %r14, %rsi
movq 0x18(%rsp), %rdx
callq 0x245e6a
movl %eax, %ebp
movq %r14, %rdi
callq 0x21f7f9
movq %rbx, %r13
movq 0x10(%rsp), %rbx
movq 0x30(%rsp), %r14
jmp 0x243055
movq %rax, %rsi
jmp 0x24304c
addq $0x2, %rsi
movq 0x18(%rsp), %rdx
callq *%rbx
movl %eax, %ebp
testl %ebp, %ebp
movq 0x20(%rsp), %rax
js 0x24306a
je 0x2431f3
leal -0x1(%r15), %eax
jmp 0x24306e
leal 0x1(%r15), %r14d
cmpl %eax, %r14d
jg 0x24315e
movq %rax, 0x20(%rsp)
leal (%rax,%r14), %r15d
shrl %r15d
jmp 0x242f2a
movq %rax, 0x48(%rsp)
movq %r13, 0x40(%rsp)
movq 0x68(%r12), %rsi
movzwl 0x1a(%r12), %r8d
movq 0x60(%r12), %r13
movb 0x3(%r12), %r9b
xorl %r14d, %r14d
movq %rsi, 0x20(%rsp)
movq %r8, 0x30(%rsp)
movb %r9b, 0xf(%rsp)
movslq %r15d, %rax
movzbl (%r13,%rax,2), %ecx
shll $0x8, %ecx
movzbl 0x1(%r13,%rax,2), %edi
orl %ecx, %edi
andl %r8d, %edi
addq %rsi, %rdi
testb %r9b, %r9b
je 0x2430ef
cmpb $0x0, (%rdi)
jns 0x2430ec
incq %rdi
cmpq 0x58(%r12), %rdi
jb 0x2430d8
jmp 0x2431cf
incq %rdi
leaq 0x60(%rsp), %rsi
callq 0x234e18
movq 0x60(%rsp), %rax
cmpq 0x50(%rsp), %rax
jge 0x243110
leal 0x1(%r15), %r14d
cmpl %ebp, %r15d
jl 0x24311d
jmp 0x243135
jle 0x243139
cmpl %r15d, %r14d
jge 0x243156
decl %r15d
movl %r15d, %ebp
leal (%r14,%rbp), %r15d
sarl %r15d
movq 0x20(%rsp), %rsi
movq 0x30(%rsp), %r8
movb 0xf(%rsp), %r9b
jmp 0x2430b9
pushq $-0x1
jmp 0x243158
movq 0x40(%rsp), %r13
movw %r15w, 0x56(%r13)
cmpb $0x0, 0x8(%r12)
jne 0x24329b
movq 0x48(%rsp), %rax
jmp 0x243173
pushq $0x1
popq %rbp
movq 0x40(%rsp), %r13
cmpb $0x0, 0x8(%r12)
jne 0x243278
movzwl 0x18(%r12), %eax
movl %r14d, %r15d
movq 0x50(%r12), %rdi
cmpl %eax, %r15d
jge 0x2431a0
movzwl 0x1a(%r12), %eax
movq 0x60(%r12), %rcx
movslq %r15d, %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 0x2431ad
movzbl 0x9(%r12), %eax
addq %rax, %rdi
addq $0x8, %rdi
callq 0x22fc59
movw %r15w, 0x56(%r13)
movq %r13, %rdi
movl %eax, %esi
callq 0x2455c6
testl %eax, %eax
movl 0x3c(%rsp), %ecx
je 0x242efe
jmp 0x243215
movl $0x1116d, %edi          # imm = 0x1116D
callq 0x232547
jmp 0x24328c
movq 0x30(%r13), %rax
cmpq %r12, %rax
jne 0x243217
andl $0x0, (%rbx)
jmp 0x24325f
movl $0x111b8, %edi          # imm = 0x111B8
jmp 0x243210
movq 0x28(%rsp), %rax
andl $0x0, (%rax)
movw %r15w, 0x56(%r13)
movq 0x18(%rsp), %rax
cmpb $0x0, 0x13(%rax)
je 0x243284
movl $0x111d8, %edi          # imm = 0x111D8
callq 0x232547
jmp 0x243286
jge 0x242e6d
testb $0x8, %cl
jne 0x24325c
incq %rax
cmpq %r12, %rax
jne 0x242e6d
andl $0x0, (%rbx)
movq %r13, %rdi
callq 0x2432b4
cmpl $0x65, %eax
je 0x242e6d
testl %eax, %eax
jne 0x24328c
movq %r13, %rdi
callq 0x23718f
xorl %eax, %eax
cmpq %r12, 0x30(%r13)
jne 0x242e6d
jmp 0x24328c
orl $-0x1, (%rbx)
xorl %eax, %eax
jmp 0x24328c
pushq $0x7
popq %rax
jmp 0x243286
movq %r14, %rdi
callq 0x21f7f9
movl %r13d, %eax
movq %rbx, %r13
jmp 0x243286
movw %r15w, 0x56(%r13)
movq 0x28(%rsp), %rax
movl %ebp, (%rax)
xorl %eax, %eax
andw $0x0, 0x46(%r13)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
orb $0x2, 0x1(%r13)
movq %rax, 0x30(%r13)
andw $0x0, 0x46(%r13)
movq 0x28(%rsp), %rax
andl $0x0, (%rax)
jmp 0x24325f