// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: vdbeSorterFlushPMA
static int vdbeSorterFlushPMA(VdbeSorter *pSorter){
#if SQLITE_MAX_WORKER_THREADS==0
  pSorter->bUsePMA = 1;
  return vdbeSorterListToPMA(&pSorter->aTask[0], &pSorter->list);
#else
  int rc = SQLITE_OK;
  int i;
  SortSubtask *pTask = 0;    /* Thread context used to create new PMA */
  int nWorker = (pSorter->nTask-1);

  /* Set the flag to indicate that at least one PMA has been written.
  ** Or will be, anyhow.  */
  pSorter->bUsePMA = 1;

  /* Select a sub-task to sort and flush the current list of in-memory
  ** records to disk. If the sorter is running in multi-threaded mode,
  ** round-robin between the first (pSorter->nTask-1) tasks. Except, if
  ** the background thread from a sub-tasks previous turn is still running,
  ** skip it. If the first (pSorter->nTask-1) sub-tasks are all still busy,
  ** fall back to using the final sub-task. The first (pSorter->nTask-1)
  ** sub-tasks are prefered as they use background threads - the final
  ** sub-task uses the main thread. */
  for(i=0; i<nWorker; i++){
    int iTest = (pSorter->iPrev + i + 1) % nWorker;
    pTask = &pSorter->aTask[iTest];
    if( pTask->bDone ){
      rc = vdbeSorterJoinThread(pTask);
    }
    if( rc!=SQLITE_OK || pTask->pThread==0 ) break;
  }

  if( rc==SQLITE_OK ){
    if( i==nWorker ){
      /* Use the foreground thread for this operation */
      rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
    }else{
      /* Launch a background thread for this operation */
      u8 *aMem;
      void *pCtx;

      assert( pTask!=0 );
      assert( pTask->pThread==0 && pTask->bDone==0 );
      assert( pTask->list.pList==0 );
      assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );

      aMem = pTask->list.aMemory;
      pCtx = (void*)pTask;
      pSorter->iPrev = (u8)(pTask - pSorter->aTask);
      pTask->list = pSorter->list;
      pSorter->list.pList = 0;
      pSorter->list.szPMA = 0;
      if( aMem ){
        pSorter->list.aMemory = aMem;
        pSorter->nMemory = sqlite3MallocSize(aMem);
      }else if( pSorter->list.aMemory ){
        pSorter->list.aMemory = sqlite3Malloc(pSorter->nMemory);
        if( !pSorter->list.aMemory ) return SQLITE_NOMEM_BKPT;
      }

      rc = vdbeSorterCreateThread(pTask, vdbeSorterFlushThread, pCtx);
    }
  }

  return rc;
#endif /* SQLITE_MAX_WORKER_THREADS!=0 */
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movb $0x1, 0x58(%rdi)
movq 0x70(%rdi), %rax
movq 0x20(%rax), %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, (%rsp)
movq 0xa8(%rdi), %rsi
testq %rsi, %rsi
je 0x24973f
leaq 0x60(%rbx), %r12
leaq 0x38(%rbx), %r14
movq 0xb0(%rbx), %rax
movslq 0x48(%rbx), %rcx
leaq (%rax,%rcx), %rdx
addq $0x9, %rdx
movq %r15, %rdi
callq 0x249b30
movq %r12, %rdi
movq %r14, %rsi
callq 0x249524
testl %eax, %eax
jne 0x24976e
leaq 0xb0(%rbx), %r15
movq 0x70(%rbx), %rax
movq 0xa8(%rbx), %rdi
movl 0xc(%rax), %edx
movq 0xb0(%rbx), %rcx
movq %rsp, %r12
movq %r12, %rsi
callq 0x249bb5
incl 0x98(%rbx)
movslq 0x48(%rbx), %rsi
movq %r12, %rdi
callq 0x249c19
movq 0x38(%rbx), %rbp
testq %rbp, %rbp
je 0x24975f
movq %rbp, %r13
movq 0x8(%rbp), %rbp
movslq (%r13), %rsi
movq %r12, %rdi
callq 0x249c19
leaq 0x10(%r13), %rsi
movl (%r13), %edx
movq %r12, %rdi
callq 0x249c45
cmpq $0x0, 0x40(%rbx)
jne 0x249706
movq %r13, %rdi
callq 0x21f7f9
jmp 0x249706
leaq 0xa8(%rbx), %r14
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x249a9a
testl %eax, %eax
jne 0x24976e
movq (%r14), %rsi
jmp 0x249693
andq $0x0, (%r14)
movq %rsp, %rdi
movq %r15, %rsi
callq 0x249ce0
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq