// File: /workspace/github_c_makefile_star2_O0/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: vdbeSorterMergeTreeBuild
static int vdbeSorterMergeTreeBuild(
  VdbeSorter *pSorter,       /* The VDBE cursor that implements the sort */
  MergeEngine **ppOut        /* Write the MergeEngine here */
){
  MergeEngine *pMain = 0;
  int rc = SQLITE_OK;
  int iTask;

#if SQLITE_MAX_WORKER_THREADS>0
  /* If the sorter uses more than one task, then create the top-level
  ** MergeEngine here. This MergeEngine will read data from exactly
  ** one PmaReader per sub-task.  */
  assert( pSorter->bUseThreads || pSorter->nTask==1 );
  if( pSorter->nTask>1 ){
    pMain = vdbeMergeEngineNew(pSorter->nTask);
    if( pMain==0 ) rc = SQLITE_NOMEM_BKPT;
  }
#endif

  for(iTask=0; rc==SQLITE_OK && iTask<pSorter->nTask; iTask++){
    SortSubtask *pTask = &pSorter->aTask[iTask];
    assert( pTask->nPMA>0 || SQLITE_MAX_WORKER_THREADS>0 );
    if( SQLITE_MAX_WORKER_THREADS==0 || pTask->nPMA ){
      MergeEngine *pRoot = 0;     /* Root node of tree for this task */
      int nDepth = vdbeSorterTreeDepth(pTask->nPMA);
      i64 iReadOff = 0;

      if( pTask->nPMA<=SORTER_MAX_MERGE_COUNT ){
        rc = vdbeMergeEngineLevel0(pTask, pTask->nPMA, &iReadOff, &pRoot);
      }else{
        int i;
        int iSeq = 0;
        pRoot = vdbeMergeEngineNew(SORTER_MAX_MERGE_COUNT);
        if( pRoot==0 ) rc = SQLITE_NOMEM_BKPT;
        for(i=0; i<pTask->nPMA && rc==SQLITE_OK; i += SORTER_MAX_MERGE_COUNT){
          MergeEngine *pMerger = 0; /* New level-0 PMA merger */
          int nReader;              /* Number of level-0 PMAs to merge */

          nReader = MIN(pTask->nPMA - i, SORTER_MAX_MERGE_COUNT);
          rc = vdbeMergeEngineLevel0(pTask, nReader, &iReadOff, &pMerger);
          if( rc==SQLITE_OK ){
            rc = vdbeSorterAddToTree(pTask, nDepth, iSeq++, pRoot, pMerger);
          }
        }
      }

      if( rc==SQLITE_OK ){
#if SQLITE_MAX_WORKER_THREADS>0
        if( pMain!=0 ){
          rc = vdbeIncrMergerNew(pTask, pRoot, &pMain->aReadr[iTask].pIncr);
        }else
#endif
        {
          assert( pMain==0 );
          pMain = pRoot;
        }
      }else{
        vdbeMergeEngineFree(pRoot);
      }
    }
  }

  if( rc!=SQLITE_OK ){
    vdbeMergeEngineFree(pMain);
    pMain = 0;
  }
  *ppOut = pMain;
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x1c(%rbp)
movb %al, -0x55(%rbp)
jne 0x409294
movl -0x20(%rbp), %eax
movq -0x8(%rbp), %rcx
movzbl 0x5b(%rcx), %ecx
cmpl %ecx, %eax
setl %al
movb %al, -0x55(%rbp)
movb -0x55(%rbp), %al
testb $0x1, %al
jne 0x4092a0
jmp 0x409401
movq -0x8(%rbp), %rax
addq $0x60, %rax
movslq -0x20(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x28(%rbp), %rax
movl 0x38(%rax), %edi
callq 0x4094f0
movl %eax, -0x34(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x10, 0x38(%rax)
jg 0x409300
movq -0x28(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x38(%rax), %esi
leaq -0x40(%rbp), %rdx
leaq -0x30(%rbp), %rcx
callq 0x409530
movl %eax, -0x1c(%rbp)
jmp 0x4093d8
movl $0x0, -0x48(%rbp)
movl $0x10, %edi
callq 0x409630
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x409323
movl $0x7, -0x1c(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %ecx
movq -0x28(%rbp), %rdx
xorl %eax, %eax
cmpl 0x38(%rdx), %ecx
movb %al, -0x56(%rbp)
jge 0x409345
cmpl $0x0, -0x1c(%rbp)
sete %al
movb %al, -0x56(%rbp)
movb -0x56(%rbp), %al
testb $0x1, %al
jne 0x409351
jmp 0x4093d6
movq $0x0, -0x50(%rbp)
movq -0x28(%rbp), %rax
movl 0x38(%rax), %eax
subl -0x44(%rbp), %eax
cmpl $0x10, %eax
jge 0x409377
movq -0x28(%rbp), %rax
movl 0x38(%rax), %eax
subl -0x44(%rbp), %eax
movl %eax, -0x5c(%rbp)
jmp 0x409381
movl $0x10, %eax
movl %eax, -0x5c(%rbp)
jmp 0x409381
movl -0x5c(%rbp), %eax
movl %eax, -0x54(%rbp)
movq -0x28(%rbp), %rdi
movl -0x54(%rbp), %esi
leaq -0x40(%rbp), %rdx
leaq -0x50(%rbp), %rcx
callq 0x409530
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x4093c6
movq -0x28(%rbp), %rdi
movl -0x34(%rbp), %esi
movl -0x48(%rbp), %edx
movl %edx, %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
movq -0x30(%rbp), %rcx
movq -0x50(%rbp), %r8
callq 0x4096f0
movl %eax, -0x1c(%rbp)
jmp 0x4093c8
movl -0x44(%rbp), %eax
addl $0x10, %eax
movl %eax, -0x44(%rbp)
jmp 0x40932a
jmp 0x4093d8
cmpl $0x0, -0x1c(%rbp)
jne 0x4093e8
movq -0x30(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0x4093f1
movq -0x30(%rbp), %rdi
callq 0x3ed460
jmp 0x4093f3
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x409276
cmpl $0x0, -0x1c(%rbp)
je 0x409418
movq -0x18(%rbp), %rdi
callq 0x3ed460
movq $0x0, -0x18(%rbp)
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movl -0x1c(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)