// File: /workspace/github_c_makefile_star2_O1/shifengbin[P]bishe/Output/../Persistence/sqlite3.c
// Function: sqlite3VdbeSorterInit
SQLITE_PRIVATE int sqlite3VdbeSorterInit(
  sqlite3 *db,                    /* Database connection (for malloc()) */
  int nField,                     /* Number of key fields in each record */
  VdbeCursor *pCsr                /* Cursor that holds the new sorter */
){
  int pgsz;                       /* Page size of main database */
  int i;                          /* Used to iterate through aTask[] */
  int mxCache;                    /* Cache size */
  VdbeSorter *pSorter;            /* The new sorter */
  KeyInfo *pKeyInfo;              /* Copy of pCsr->pKeyInfo with db==0 */
  int szKeyInfo;                  /* Size of pCsr->pKeyInfo in bytes */
  int sz;                         /* Size of pSorter in bytes */
  int rc = SQLITE_OK;
#if SQLITE_MAX_WORKER_THREADS==0
# define nWorker 0
#else
  int nWorker;
#endif

  /* Initialize the upper limit on the number of worker threads */
#if SQLITE_MAX_WORKER_THREADS>0
  if( sqlite3TempInMemory(db) || sqlite3GlobalConfig.bCoreMutex==0 ){
    nWorker = 0;
  }else{
    nWorker = db->aLimit[SQLITE_LIMIT_WORKER_THREADS];
  }
#endif

  /* Do not allow the total number of threads (main thread + all workers)
  ** to exceed the maximum merge count */
#if SQLITE_MAX_WORKER_THREADS>=SORTER_MAX_MERGE_COUNT
  if( nWorker>=SORTER_MAX_MERGE_COUNT ){
    nWorker = SORTER_MAX_MERGE_COUNT-1;
  }
#endif

  assert( pCsr->pKeyInfo && pCsr->pBt==0 );
  assert( pCsr->eCurType==CURTYPE_SORTER );
  szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nField-1)*sizeof(CollSeq*);
  sz = sizeof(VdbeSorter) + nWorker * sizeof(SortSubtask);

  pSorter = (VdbeSorter*)sqlite3DbMallocZero(db, sz + szKeyInfo);
  pCsr->uc.pSorter = pSorter;
  if( pSorter==0 ){
    rc = SQLITE_NOMEM;
  }else{
    pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
    memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
    pKeyInfo->db = 0;
    if( nField && nWorker==0 ){
      pKeyInfo->nXField += (pKeyInfo->nField - nField);
      pKeyInfo->nField = nField;
    }
    pSorter->pgsz = pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
    pSorter->nTask = nWorker + 1;
    pSorter->iPrev = (u8)(nWorker - 1);
    pSorter->bUseThreads = (pSorter->nTask>1);
    pSorter->db = db;
    for(i=0; i<pSorter->nTask; i++){
      SortSubtask *pTask = &pSorter->aTask[i];
      pTask->pSorter = pSorter;
    }

    if( !sqlite3TempInMemory(db) ){
      u32 szPma = sqlite3GlobalConfig.szPma;
      pSorter->mnPmaSize = szPma * pgsz;
      mxCache = db->aDb[0].pSchema->cache_size;
      if( mxCache<(int)szPma ) mxCache = (int)szPma;
      pSorter->mxPmaSize = MIN((i64)mxCache*pgsz, SQLITE_MAX_PMASZ);

      /* EVIDENCE-OF: R-26747-61719 When the application provides any amount of
      ** scratch memory using SQLITE_CONFIG_SCRATCH, SQLite avoids unnecessary
      ** large heap allocations.
      */
      if( sqlite3GlobalConfig.pScratch==0 ){
        assert( pSorter->iMemory==0 );
        pSorter->nMemory = pgsz;
        pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
        if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM;
      }
    }

    if( (pKeyInfo->nField+pKeyInfo->nXField)<13 
     && (pKeyInfo->aColl[0]==0 || pKeyInfo->aColl[0]==db->pDfltColl)
    ){
      pSorter->typeMask = SORTER_TYPE_INTEGER | SORTER_TYPE_TEXT;
    }
  }

  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdx, %r12
xorl %ecx, %ecx
cmpb $0x2, 0x50(%rdi)
movl %esi, -0x2c(%rbp)
je 0x3376f
cmpl $0x0, 0x5fbbd(%rip)     # 0x93324
je 0x3376f
movl 0x94(%rdi), %ecx
movq %rcx, -0x40(%rbp)
movq 0x20(%r12), %rax
movzwl 0x6(%rax), %r13d
imull $0x68, %ecx, %eax
addl $0xc8, %eax
movslq %eax, %rbx
leaq (%rbx,%r13,8), %r15
addq $0x20, %r15
movq %rdi, -0x38(%rbp)
movq %r15, %rsi
callq 0x144ee
movq %rax, %r14
testq %rax, %rax
je 0x338e6
leaq 0x20(,%r13,8), %r13
movq %r14, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0xa210
movq %r14, 0x10(%r12)
movq %r14, %r15
addq %rbx, %r15
movq %r15, 0x28(%r14)
movq 0x20(%r12), %rsi
movl %r13d, %edx
movq %r15, %rdi
callq 0xa310
movq $0x0, 0x10(%r14,%rbx)
movl -0x2c(%rbp), %edx
testl %edx, %edx
sete %al
movq -0x40(%rbp), %rdi
testl %edi, %edi
setne %cl
orb %al, %cl
jne 0x3380b
movzwl 0x6(%r15), %eax
subl %edx, %eax
addw %ax, 0x8(%r15)
movw %dx, 0x6(%r15)
movq -0x38(%rbp), %r9
movq 0x20(%r9), %rdx
movq 0x8(%rdx), %rax
movq 0x8(%rax), %rax
movl 0x30(%rax), %ecx
leal 0x1(%rdi), %eax
leal -0x1(%rdi), %esi
cmpb $0x2, %al
setae 0x59(%r14)
movl %ecx, 0xc(%r14)
incb %dil
movb %dil, 0x5b(%r14)
movb %sil, 0x5a(%r14)
movq %r9, 0x20(%r14)
je 0x33858
movzbl %dil, %eax
imulq $0x68, %rax, %rax
xorl %esi, %esi
movq %r14, 0x70(%r14,%rsi)
addq $0x68, %rsi
cmpq %rsi, %rax
jne 0x3384a
xorl %eax, %eax
cmpb $0x2, 0x50(%r9)
je 0x338bf
movslq %ecx, %rdi
movl 0x5fc1e(%rip), %esi     # 0x93488
movl %esi, %r8d
imull %ecx, %r8d
movl %r8d, (%r14)
movq 0x18(%rdx), %rdx
movl 0x74(%rdx), %edx
cmpl %esi, %edx
cmovgl %edx, %esi
movslq %esi, %rdx
imulq %rdi, %rdx
movl $0x20000000, %esi       # imm = 0x20000000
cmpq %rsi, %rdx
cmovgeq %rsi, %rdx
movl %edx, 0x4(%r14)
cmpq $0x0, 0x5fbc1(%rip)     # 0x93460
jne 0x338bf
movl %ecx, 0x54(%r14)
callq 0xbf9b
movq -0x38(%rbp), %r9
movq %rax, 0x40(%r14)
xorl %ecx, %ecx
testq %rax, %rax
movl $0x7, %eax
cmovnel %ecx, %eax
movzwl 0x6(%r15), %ecx
movzwl 0x8(%r15), %edx
addl %ecx, %edx
cmpl $0xc, %edx
ja 0x338f0
movq 0x20(%r15), %rcx
testq %rcx, %rcx
je 0x338df
cmpq 0x10(%r9), %rcx
jne 0x338f0
movb $0x3, 0x5c(%r14)
jmp 0x338f0
movq %r14, 0x10(%r12)
movl $0x7, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq