// File: /workspace/github_c_makefile_star2_O3/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, %rbx
movq %rdi, %r15
movq 0x8(%rdi), %r12
movq 0x10(%rdi), %rax
movslq 0x4(%rax), %r14
leaq (%r14,%r14,4), %rdi
shlq $0x4, %rdi
addq 0x18(%r15), %rdi
callq 0x2a4a33
testl %eax, %eax
jne 0x2a4f5c
movq %r12, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movl $0x0, 0x4(%rsp)
movl (%r15), %ebx
addl %r14d, %ebx
movq 0x18(%r15), %rax
cmpl $0x1, %ebx
jle 0x2a4f2d
movq %r14, %rcx
orq $0x1, %rcx
leaq (%rcx,%rcx,4), %r13
shlq $0x4, %r13
addq %rax, %r13
andl $0xfffe, %r14d          # imm = 0xFFFE
leaq (%r14,%r14,4), %r12
shll $0x4, %r12d
addq %rax, %r12
movl %ebx, %ebp
shrl %ebp
cmpq $0x0, 0x18(%r12)
movl %ebp, %r14d
je 0x2a4ef0
cmpq $0x0, 0x18(%r13)
je 0x2a4ea6
movq 0x28(%r12), %rdx
movl 0x14(%r12), %ecx
movq 0x28(%r13), %r8
movl 0x14(%r13), %r9d
movq 0x8(%rsp), %rdi
leaq 0x4(%rsp), %rsi
callq *0x40(%rdi)
testl %eax, %eax
js 0x2a4ea6
jne 0x2a4ee0
cmpq %r13, %r12
jae 0x2a4ee0
movq 0x10(%r15), %rcx
movq 0x18(%r15), %rax
movq %r12, %rdx
subq %rax, %rdx
shrq $0x4, %rdx
imull $0xcccccccd, %edx, %edx # imm = 0xCCCCCCCD
movl %edx, (%rcx,%r14,4)
movl %ebp, %edx
xorl $0x1, %edx
movslq (%rcx,%rdx,4), %rdx
leaq (%rdx,%rdx,4), %r13
shlq $0x4, %r13
addq %rax, %r13
movl $0x0, 0x4(%rsp)
jmp 0x2a4f20
cmpq $0x0, 0x18(%r12)
je 0x2a4ef0
movl $0x0, 0x4(%rsp)
movq 0x10(%r15), %rcx
movq 0x18(%r15), %rax
movq %r13, %rdx
subq %rax, %rdx
shrq $0x4, %rdx
imull $0xcccccccd, %edx, %edx # imm = 0xCCCCCCCD
movl %edx, (%rcx,%r14,4)
movl %ebp, %edx
xorl $0x1, %edx
movslq (%rcx,%rdx,4), %rdx
leaq (%rdx,%rdx,4), %r12
shlq $0x4, %r12
addq %rax, %r12
cmpl $0x3, %ebx
movl %ebp, %ebx
ja 0x2a4e68
jmp 0x2a4f31
movq 0x10(%r15), %rcx
movslq 0x4(%rcx), %rcx
leaq (%rcx,%rcx,4), %rcx
shlq $0x4, %rcx
xorl %edx, %edx
cmpq $0x0, 0x18(%rax,%rcx)
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