// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %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)
xorl %eax, %eax
cmpq $0x0, 0xa8(%rdi)
jne 0x2ad97f
leaq 0xa8(%rbx), %rdx
movq %r15, %rdi
xorl %esi, %esi
callq 0x2ade1e
leaq 0x38(%rbx), %r14
testl %eax, %eax
jne 0x2ad9b8
leaq 0x60(%rbx), %r12
movq 0xa8(%rbx), %rsi
movq 0xb0(%rbx), %rax
movslq 0x48(%rbx), %rcx
leaq (%rax,%rcx), %rdx
addq $0x9, %rdx
movq %r15, %rdi
callq 0x2adeb6
movq %r12, %rdi
movq %r14, %rsi
callq 0x2ad7e1
testl %eax, %eax
jne 0x2adabf
movq 0x70(%rbx), %rax
movq 0xa8(%rbx), %r13
movl 0xc(%rax), %eax
movq 0xb0(%rbx), %r15
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movaps %xmm0, 0x10(%rsp)
movaps %xmm0, (%rsp)
movslq %eax, %r12
movq %r12, %rdi
callq 0x27c381
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x2ada1f
movq %r15, %rax
cqto
idivq %r12
movl %edx, 0x14(%rsp)
movl %edx, 0x18(%rsp)
subq %rdx, %r15
movq %r15, 0x20(%rsp)
movl %r12d, 0x10(%rsp)
movq %r13, 0x28(%rsp)
jmp 0x2ada26
movl $0x7, (%rsp)
leaq 0xb0(%rbx), %rax
movq %rax, 0x30(%rsp)
incl 0x98(%rbx)
movslq 0x48(%rbx), %rsi
leaq 0x3e(%rsp), %r12
movq %r12, %rdi
callq 0x2a4a2b
movq %rsp, %rdi
movq %r12, %rsi
movl %eax, %edx
callq 0x2adf3f
movq 0x38(%rbx), %r13
testq %r13, %r13
je 0x2adaab
leaq 0x3e(%rsp), %r12
movq %rsp, %rbp
movq 0x8(%r13), %r15
movslq (%r13), %rsi
movq %r12, %rdi
callq 0x2a4a2b
movq %rbp, %rdi
movq %r12, %rsi
movl %eax, %edx
callq 0x2adf3f
leaq 0x10(%r13), %rsi
movl (%r13), %edx
movq %rbp, %rdi
callq 0x2adf3f
cmpq $0x0, 0x40(%rbx)
jne 0x2adaa3
movq %r13, %rdi
callq 0x27c490
movq %r15, %r13
testq %r15, %r15
jne 0x2ada67
movq $0x0, (%r14)
movq %rsp, %rdi
movq 0x30(%rsp), %rsi
callq 0x2adfe1
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq