// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: vdbeMergeEngineStep
static int vdbeMergeEngineStep(
  MergeEngine *pMerger,      /* The merge engine to advance to the next row */
  int *pbEof                 /* Set TRUE at EOF.  Set false for more content */
){
  int rc;
  int iPrev = pMerger->aTree[1];/* Index of PmaReader to advance */
  SortSubtask *pTask = pMerger->pTask;

  /* Advance the current PmaReader */
  rc = vdbePmaReaderNext(&pMerger->aReadr[iPrev]);

  /* Update contents of aTree[] */
  if( rc==SQLITE_OK ){
    int i;                      /* Index of aTree[] to recalculate */
    PmaReader *pReadr1;         /* First PmaReader to compare */
    PmaReader *pReadr2;         /* Second PmaReader to compare */
    int bCached = 0;

    /* Find the first two PmaReaders to compare. The one that was just
    ** advanced (iPrev) and the one next to it in the array.  */
    pReadr1 = &pMerger->aReadr[(iPrev & 0xFFFE)];
    pReadr2 = &pMerger->aReadr[(iPrev | 0x0001)];

    for(i=(pMerger->nTree+iPrev)/2; i>0; i=i/2){
      /* Compare pReadr1 and pReadr2. Store the result in variable iRes. */
      int iRes;
      if( pReadr1->pFd==0 ){
        iRes = +1;
      }else if( pReadr2->pFd==0 ){
        iRes = -1;
      }else{
        iRes = pTask->xCompare(pTask, &bCached,
            pReadr1->aKey, pReadr1->nKey, pReadr2->aKey, pReadr2->nKey
        );
      }

      /* If pReadr1 contained the smaller value, set aTree[i] to its index.
      ** Then set pReadr2 to the next PmaReader to compare to pReadr1. In this
      ** case there is no cache of pReadr2 in pTask->pUnpacked, so set
      ** pKey2 to point to the record belonging to pReadr2.
      **
      ** Alternatively, if pReadr2 contains the smaller of the two values,
      ** set aTree[i] to its index and update pReadr1. If vdbeSorterCompare()
      ** was actually called above, then pTask->pUnpacked now contains
      ** a value equivalent to pReadr2. So set pKey2 to NULL to prevent
      ** vdbeSorterCompare() from decoding pReadr2 again.
      **
      ** If the two values were equal, then the value from the oldest
      ** PMA should be considered smaller. The VdbeSorter.aReadr[] array
      ** is sorted from oldest to newest, so pReadr1 contains older values
      ** than pReadr2 iff (pReadr1<pReadr2).  */
      if( iRes<0 || (iRes==0 && pReadr1<pReadr2) ){
        pMerger->aTree[i] = (int)(pReadr1 - pMerger->aReadr);
        pReadr2 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
        bCached = 0;
      }else{
        if( pReadr1->pFd ) bCached = 0;
        pMerger->aTree[i] = (int)(pReadr2 - pMerger->aReadr);
        pReadr1 = &pMerger->aReadr[ pMerger->aTree[i ^ 0x0001] ];
      }
    }
    *pbEof = (pMerger->aReadr[pMerger->aTree[1]].pFd==0);
  }

  return (rc==SQLITE_OK ? pTask->pUnpacked->errCode : rc);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r12
movq %rdi, %r15
movq 0x8(%rdi), %r14
movq 0x10(%rdi), %rax
movslq 0x4(%rax), %rbx
imulq $0x50, %rbx, %rdi
addq 0x18(%r15), %rdi
callq 0x24a224
testl %eax, %eax
jne 0x24a6a3
movq %r14, 0x8(%rsp)
movq %r12, 0x10(%rsp)
leaq 0x4(%rsp), %rax
andl $0x0, (%rax)
movl %ebx, %eax
movq 0x18(%r15), %rcx
movl %eax, %edx
andl $0xfffe, %edx           # imm = 0xFFFE
imulq $0x50, %rdx, %rbx
addq %rcx, %rbx
movl %eax, %edx
orl $0x1, %edx
movslq %edx, %rdx
imulq $0x50, %rdx, %r12
addq %rcx, %r12
addl (%r15), %eax
pushq $0x2
popq %rsi
cltd
idivl %esi
movl %eax, %ebp
pushq $0x50
popq %r14
testl %ebp, %ebp
jle 0x24a678
cmpq $0x0, 0x18(%rbx)
movl %ebp, %r13d
je 0x24a64a
cmpq $0x0, 0x18(%r12)
je 0x24a610
movq 0x28(%rbx), %rdx
movl 0x14(%rbx), %ecx
movq 0x28(%r12), %r8
movl 0x14(%r12), %r9d
movq 0x8(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq *0x40(%rdi)
testl %eax, %eax
js 0x24a610
jne 0x24a63e
cmpq %r12, %rbx
jae 0x24a63e
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rcx
movq %rbx, %rax
subq %rcx, %rax
cqto
idivq %r14
movl %eax, (%rsi,%r13,4)
movl %ebp, %eax
xorl $0x1, %eax
movslq (%rsi,%rax,4), %rax
imulq $0x50, %rax, %r12
addq %rcx, %r12
andl $0x0, 0x4(%rsp)
jmp 0x24a671
cmpq $0x0, 0x18(%rbx)
je 0x24a64a
andl $0x0, 0x4(%rsp)
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rcx
movq %r12, %rax
subq %rcx, %rax
cqto
idivq %r14
movl %eax, (%rsi,%r13,4)
movl %ebp, %eax
xorl $0x1, %eax
movslq (%rsi,%rax,4), %rax
imulq $0x50, %rax, %rbx
addq %rcx, %rbx
shrl %ebp
jmp 0x24a5cd
movq 0x10(%r15), %rax
movslq 0x4(%rax), %rax
imulq $0x50, %rax, %rax
xorl %edx, %edx
cmpq $0x0, 0x18(%rcx,%rax)
sete %dl
movq 0x10(%rsp), %rax
movl %edx, (%rax)
movq 0x8(%rsp), %rax
movq 0x18(%rax), %rax
movzbl 0x13(%rax), %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq