// File: /workspace/github_c_makefile_star2_O3/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 $0x58, %rsp
movq %r8, %rbx
movl %ecx, %r14d
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, %rbp
testq %rsi, %rsi
je 0x29c10a
movq (%r13), %rax
leaq 0x3cb4(%rip), %rcx      # 0x29fd95
movq %rcx, (%rsp)
cmpw $0xd, 0x8(%rax)
ja 0x29c166
movq 0x8(%r13), %rcx
movzwl 0x8(%rcx), %ecx
movq 0x18(%rax), %rdx
movb (%rdx), %dl
testb %dl, %dl
je 0x29c127
testb $0x2, %dl
jne 0x29c166
movb $-0x1, %dl
movb $0x1, %sil
jmp 0x29c12c
cmpb $0x0, (%rbp)
jne 0x29c11d
movzbl 0x1(%rbp), %ecx
testb $0x2, %cl
jne 0x29c512
movq $0x0, (%rsp)
jmp 0x29c16b
movb $0x1, %dl
movb $-0x1, %sil
movb %sil, 0x14(%r13)
movb %dl, 0x15(%r13)
testb $0x4, %cl
jne 0x29c15b
testb $0x39, %cl
jne 0x29c166
cmpq $0x0, 0x20(%rax)
leaq 0x3d3c(%rip), %rax      # 0x29fe86
leaq 0x3c44(%rip), %rcx      # 0x29fd95
cmoveq %rax, %rcx
movq %rcx, (%rsp)
jmp 0x29c166
leaq 0x3c3a(%rip), %rax      # 0x29fd9c
movq %rax, (%rsp)
movb $0x0, 0x13(%r13)
movq %rbp, %rdi
callq 0x29f169
cmpl $0x10, %eax
je 0x29c5e5
movl %eax, %r12d
testl %eax, %eax
jne 0x29c640
movq %r15, 0x40(%rsp)
movq %rbx, 0x18(%rsp)
movl $0x1, %ecx
subl %r14d, %ecx
leaq 0x30(%rbp), %rax
movq %rax, 0x48(%rsp)
movq %rbp, 0x8(%rsp)
movq (%rsp), %rbx
movq %r13, 0x20(%rsp)
movl %ecx, 0x34(%rsp)
movq 0x88(%rbp), %r14
movzwl 0x18(%r14), %eax
leal -0x1(%rax), %edx
movl %edx, %r15d
sarl %cl, %r15d
movw %r15w, 0x56(%rbp)
testq %rbx, %rbx
je 0x29c365
movq %rdx, 0x10(%rsp)
xorl %r8d, %r8d
movq 0x60(%r14), %rax
movq 0x68(%r14), %rsi
movzwl 0x1a(%r14), %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(%r14), %dil
movq %r8, 0x28(%rsp)
jbe 0x29c31b
addq %rcx, %rsi
movsbl (%rax), %eax
testl %eax, %eax
js 0x29c238
andl $0x7f, %edi
shll $0x7, %edi
orl %eax, %edi
movzwl 0xe(%r14), %eax
cmpl %eax, %edi
jbe 0x29c320
movzbl 0xa(%r14), %eax
subq %rax, %rsi
movq %r14, %rdi
movq 0x48(%rsp), %rbx
movq %rbx, %rdx
callq *0x80(%r14)
movq (%rbx), %r13
cmpl $0x2, %r13d
jl 0x29c526
movq 0x20(%rbp), %rcx
movl %r13d, %eax
xorl %edx, %edx
divl 0x38(%rcx)
cmpl 0x40(%rcx), %eax
ja 0x29c526
leal 0x12(%r13), %edi
callq 0x272ae6
testq %rax, %rax
je 0x29c5f0
movq %rax, %rbp
movq 0x8(%rsp), %rbx
movw %r15w, 0x56(%rbx)
movq %rbx, %rdi
xorl %esi, %esi
movl %r13d, %edx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0x28e1f6
movl %eax, %r12d
movl %r13d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorps %xmm0, %xmm0
movups %xmm0, (%rbp,%rax)
movw $0x0, 0x10(%rbp,%rax)
andb $-0x5, 0x1(%rbx)
testl %r12d, %r12d
jne 0x29c5f8
movl %r13d, %edi
movq %rbp, %rsi
movq 0x20(%rsp), %r13
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x29ff8a
movl %eax, %r12d
cmpl $0x0, 0x21f374(%rip)    # 0x4bb660
je 0x29c307
movq %rbp, %rdi
callq *0x21f3a1(%rip)         # 0x4bb698
cltq
subq %rax, 0x2227f8(%rip)    # 0x4beaf8
decq 0x222839(%rip)          # 0x4beb40
movq %rbp, %rdi
callq *0x21f378(%rip)         # 0x4bb688
movq 0x8(%rsp), %rbp
movq (%rsp), %rbx
jmp 0x29c32c
movq %rax, %rsi
jmp 0x29c324
addq $0x2, %rsi
movq %r13, %rdx
callq *%rbx
movl %eax, %r12d
testl %r12d, %r12d
movq 0x10(%rsp), %rax
js 0x29c347
je 0x29c54d
leal -0x1(%r15), %eax
movq 0x28(%rsp), %r8
jmp 0x29c34b
leal 0x1(%r15), %r8d
cmpl %eax, %r8d
jg 0x29c455
movq %rax, 0x10(%rsp)
leal (%rax,%r8), %r15d
shrl %r15d
jmp 0x29c1df
movq %rax, 0x38(%rsp)
movq 0x68(%r14), %r12
movzwl 0x1a(%r14), %esi
movq 0x60(%r14), %r13
movb 0x3(%r14), %r9b
xorl %r8d, %r8d
movq %rsi, 0x10(%rsp)
movb %r9b, 0x28(%rsp)
movslq %r15d, %rax
movzbl (%r13,%rax,2), %ecx
shll $0x8, %ecx
movzbl 0x1(%r13,%rax,2), %edi
orl %ecx, %edi
andl %esi, %edi
addq %r12, %rdi
testb %r9b, %r9b
je 0x29c3b9
cmpb $0x0, (%rdi)
jns 0x29c3c1
incq %rdi
cmpq 0x58(%r14), %rdi
jb 0x29c3a6
jmp 0x29c4e1
movq %rdx, %rbp
movq %r8, %rbx
jmp 0x29c3ca
movq %rdx, %rbp
movq %r8, %rbx
incq %rdi
leaq 0x50(%rsp), %rsi
callq 0x28b524
movq 0x50(%rsp), %rax
cmpq 0x40(%rsp), %rax
jge 0x29c3ee
leal 0x1(%r15), %r8d
movq %rbp, %rdx
cmpl %edx, %r15d
jl 0x29c3fe
jmp 0x29c414
jle 0x29c41c
movq %rbx, %r8
cmpl %r15d, %r8d
jge 0x29c441
decl %r15d
movl %r15d, %edx
leal (%rdx,%r8), %r15d
sarl %r15d
movq 0x10(%rsp), %rsi
movb 0x28(%rsp), %r9b
jmp 0x29c388
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x29c447
movq 0x8(%rsp), %rbp
movw %r15w, 0x56(%rbp)
cmpb $0x0, 0x8(%r14)
jne 0x29c652
movq 0x20(%rsp), %r13
movq (%rsp), %rbx
movq 0x38(%rsp), %rcx
jmp 0x29c468
movl $0x1, %r12d
movq 0x8(%rsp), %rbp
movq 0x20(%rsp), %r13
movq (%rsp), %rbx
cmpb $0x0, 0x8(%r14)
jne 0x29c62a
movzwl 0x18(%r14), %ecx
movl %r8d, %r15d
movq 0x50(%r14), %rax
cmpl %ecx, %r15d
jge 0x29c496
movzwl 0x1a(%r14), %ecx
movq 0x60(%r14), %rdx
movslq %r15d, %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 0x29c4a2
movzbl 0x9(%r14), %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
movw %r15w, 0x56(%rbp)
movq %rbp, %rdi
callq 0x29f322
testl %eax, %eax
movl 0x34(%rsp), %ecx
je 0x29c1b4
movl %eax, %r12d
jmp 0x29c63a
leaq 0x1396e3(%rip), %rsi    # 0x3d5bcb
leaq 0x1397ed(%rip), %rdx    # 0x3d5cdc
leaq 0x13953b(%rip), %r8     # 0x3d5a31
movl $0xb, %r12d
movl $0xb, %edi
movl $0x1116d, %ecx          # imm = 0x1116D
xorl %eax, %eax
callq 0x274d08
jmp 0x29c640
movq 0x30(%rbp), %rax
cmpq %r15, %rax
jne 0x29c599
movl $0x0, (%rbx)
jmp 0x29c5eb
leaq 0x13969e(%rip), %rsi    # 0x3d5bcb
leaq 0x1397a8(%rip), %rdx    # 0x3d5cdc
leaq 0x1394f6(%rip), %r8     # 0x3d5a31
movl $0xb, %r12d
movl $0xb, %edi
movl $0x111b8, %ecx          # imm = 0x111B8
jmp 0x29c58d
movq 0x18(%rsp), %rax
movl $0x0, (%rax)
movw %r15w, 0x56(%rbp)
cmpb $0x0, 0x13(%r13)
je 0x29c637
leaq 0x13965c(%rip), %rsi    # 0x3d5bcb
leaq 0x139766(%rip), %rdx    # 0x3d5cdc
leaq 0x1394b4(%rip), %r8     # 0x3d5a31
movl $0xb, %r12d
movl $0xb, %edi
movl $0x111d8, %ecx          # imm = 0x111D8
xorl %eax, %eax
callq 0x274d08
jmp 0x29c63a
jge 0x29c11d
testb $0x8, %cl
jne 0x29c5e5
incq %rax
cmpq %r15, %rax
jne 0x29c11d
movl $0x0, (%rbx)
movq %rbp, %rdi
callq 0x29c670
cmpl $0x65, %eax
je 0x29c11d
movl %eax, %r12d
testl %eax, %eax
jne 0x29c640
movq %rbp, %rdi
callq 0x28e1b0
xorl %r12d, %r12d
cmpq %r15, 0x30(%rbp)
jne 0x29c11d
jmp 0x29c640
movl $0xffffffff, (%rbx)     # imm = 0xFFFFFFFF
xorl %r12d, %r12d
jmp 0x29c640
movl $0x7, %r12d
jmp 0x29c623
cmpl $0x0, 0x21f061(%rip)    # 0x4bb660
je 0x29c61a
movq %rbp, %rdi
callq *0x21f08e(%rip)         # 0x4bb698
cltq
subq %rax, 0x2224e5(%rip)    # 0x4beaf8
decq 0x222526(%rip)          # 0x4beb40
movq %rbp, %rdi
callq *0x21f065(%rip)         # 0x4bb688
movq 0x8(%rsp), %rbp
jmp 0x29c63a
movw %r15w, 0x56(%rbp)
movq 0x18(%rsp), %rax
movl %r12d, (%rax)
xorl %r12d, %r12d
movw $0x0, 0x46(%rbp)
movl %r12d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
orb $0x2, 0x1(%rbp)
movq %rax, 0x30(%rbp)
movw $0x0, 0x46(%rbp)
movq 0x18(%rsp), %rax
movl $0x0, (%rax)
jmp 0x29c5eb