// File: /workspace/github_c_makefile_star2_O0/shifengbin[P]bishe/Output/../Persistence/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 = pTask->list.aMemory;
      void *pCtx = (void*)pTask;

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

      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;
      }

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

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x5b(%rax), %eax
subl $0x1, %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movb $0x1, 0x58(%rax)
movl $0x0, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x4f7d1
movq -0x10(%rbp), %rax
movzbl 0x5a(%rax), %eax
addl -0x18(%rbp), %eax
addl $0x1, %eax
cltd
idivl -0x24(%rbp)
movl %edx, -0x28(%rbp)
movq -0x10(%rbp), %rax
addq $0x60, %rax
movslq -0x28(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x8(%rax)
je 0x4f7b2
movq -0x20(%rbp), %rdi
callq 0x37540
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x4f7c2
movq -0x20(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x4f7c4
jmp 0x4f7d1
jmp 0x4f7c6
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x4f768
cmpl $0x0, -0x14(%rbp)
jne 0x4f8f3
movl -0x18(%rbp), %eax
cmpl -0x24(%rbp), %eax
jne 0x4f80b
movq -0x10(%rbp), %rdi
addq $0x60, %rdi
movslq -0x24(%rbp), %rax
imulq $0x68, %rax, %rax
addq %rax, %rdi
movq -0x10(%rbp), %rsi
addq $0x38, %rsi
callq 0x50200
movl %eax, -0x14(%rbp)
jmp 0x4f8f1
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
addq $0x60, %rcx
subq %rcx, %rax
movl $0x68, %ecx
cqto
idivq %rcx
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x5a(%rax)
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x38(%rcx), %rdx
movq %rdx, 0x20(%rax)
movq 0x40(%rcx), %rdx
movq %rdx, 0x28(%rax)
movq 0x48(%rcx), %rcx
movq %rcx, 0x30(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x38(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x48(%rax)
cmpq $0x0, -0x30(%rbp)
je 0x4f89f
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x30(%rbp), %rdi
callq 0xdd10
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x54(%rax)
jmp 0x4f8da
movq -0x10(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x4f8d8
movq -0x10(%rbp), %rax
movslq 0x54(%rax), %rdi
callq 0xdba0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x40(%rax)
jne 0x4f8d6
movl $0x7, -0x4(%rbp)
jmp 0x4f8f9
jmp 0x4f8d8
jmp 0x4f8da
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rdx
leaq 0xad7(%rip), %rsi       # 0x503c0
callq 0x50390
movl %eax, -0x14(%rbp)
jmp 0x4f8f3
movl -0x14(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)