// File: /workspace/github_c_makefile_star2_O3/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
movq 0xa8(%rdi), %rsi
testq %rsi, %rsi
je 0x2a3e6a
leaq 0x60(%rbx), %r12
leaq 0x38(%rbx), %r14
movq 0xb0(%rbx), %rax
movslq 0x48(%rbx), %rcx
leaq (%rax,%rcx), %rdx
addq $0x9, %rdx
movl 0xac(%r15), %edi
callq 0x2a4349
movq %r12, %rdi
movq %r14, %rsi
callq 0x2a3c66
testl %eax, %eax
jne 0x2a3f51
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 0x272ae6
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x2a3e8e
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 0x2a3e95
leaq 0xa8(%rbx), %r14
movq %r15, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x2a42ad
testl %eax, %eax
jne 0x2a3f51
movq (%r14), %rsi
jmp 0x2a3dd1
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 0x29b924
movq %rsp, %rdi
movq %r12, %rsi
movl %eax, %edx
callq 0x2a43d1
movq 0x38(%rbx), %r13
testq %r13, %r13
je 0x2a3f3d
leaq 0x3e(%rsp), %r12
movq %rsp, %rbp
movq 0x8(%r13), %r15
movslq (%r13), %rsi
movq %r12, %rdi
callq 0x29b924
movq %rbp, %rdi
movq %r12, %rsi
movl %eax, %edx
callq 0x2a43d1
leaq 0x10(%r13), %rsi
movl (%r13), %edx
movq %rbp, %rdi
callq 0x2a43d1
cmpq $0x0, 0x40(%rbx)
jne 0x2a3f35
cmpl $0x0, 0x21774f(%rip)    # 0x4bb660
je 0x2a3f2c
movq %r13, %rdi
callq *0x21777c(%rip)         # 0x4bb698
cltq
subq %rax, 0x21abd3(%rip)    # 0x4beaf8
decq 0x21ac14(%rip)          # 0x4beb40
movq %r13, %rdi
callq *0x217753(%rip)         # 0x4bb688
movq %r15, %r13
testq %r15, %r15
jne 0x2a3ed6
movq $0x0, (%r14)
movq %rsp, %rdi
movq 0x30(%rsp), %rsi
callq 0x2a4473
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq