// File: /workspace/github_c_makefile_star2_O0/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
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x44(%rbp)
movq -0x8(%rbp), %rdi
callq 0x26100
cmpl $0x0, %eax
jne 0x44121
cmpl $0x0, 0x7b3a5(%rip)     # 0xbf4c4
jne 0x4412a
movl $0x0, -0x48(%rbp)
jmp 0x44137
movq -0x8(%rbp), %rax
movl 0x94(%rax), %eax
movl %eax, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movzwl 0x6(%rax), %eax
subl $0x1, %eax
cltq
shlq $0x3, %rax
addq $0x28, %rax
movl %eax, -0x3c(%rbp)
movslq -0x48(%rbp), %rax
imulq $0x68, %rax, %rax
addq $0xc8, %rax
movl %eax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movl -0x40(%rbp), %eax
addl -0x3c(%rbp), %eax
movslq %eax, %rsi
callq 0x15b80
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
cmpq $0x0, -0x30(%rbp)
jne 0x44199
movl $0x7, -0x44(%rbp)
jmp 0x443ad
movq -0x30(%rbp), %rcx
movslq -0x40(%rbp), %rax
addq %rax, %rcx
movq %rcx, -0x38(%rbp)
movq -0x30(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x38(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rsi
movslq -0x3c(%rbp), %rdx
callq 0xb420
movq -0x38(%rbp), %rax
movq $0x0, 0x10(%rax)
cmpl $0x0, -0xc(%rbp)
je 0x44204
cmpl $0x0, -0x48(%rbp)
jne 0x44204
movq -0x38(%rbp), %rax
movzwl 0x6(%rax), %edx
subl -0xc(%rbp), %edx
movq -0x38(%rbp), %rax
movzwl 0x8(%rax), %ecx
addl %edx, %ecx
movw %cx, 0x8(%rax)
movl -0xc(%rbp), %eax
movw %ax, %cx
movq -0x38(%rbp), %rax
movw %cx, 0x6(%rax)
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rdi
callq 0x12320
movl %eax, %ecx
movl %ecx, -0x1c(%rbp)
movq -0x30(%rbp), %rax
movl %ecx, 0xc(%rax)
movl -0x48(%rbp), %eax
addl $0x1, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x5b(%rax)
movl -0x48(%rbp), %eax
subl $0x1, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x5a(%rax)
movq -0x30(%rbp), %rax
movzbl 0x5b(%rax), %eax
cmpl $0x1, %eax
setg %al
andb $0x1, %al
movzbl %al, %eax
movb %al, %cl
movq -0x30(%rbp), %rax
movb %cl, 0x59(%rax)
movq -0x8(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x20(%rax)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x30(%rbp), %rcx
movzbl 0x5b(%rcx), %ecx
cmpl %ecx, %eax
jge 0x442ab
movq -0x30(%rbp), %rax
addq $0x60, %rax
movslq -0x20(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x30(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x4426e
movq -0x8(%rbp), %rdi
callq 0x26100
cmpl $0x0, %eax
jne 0x4436f
movl 0x7b365(%rip), %eax     # 0xbf628
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %ecx
imull -0x1c(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x18(%rax), %rax
movl 0x74(%rax), %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x442f3
movl -0x54(%rbp), %eax
movl %eax, -0x24(%rbp)
movslq -0x24(%rbp), %rax
movslq -0x1c(%rbp), %rcx
imulq %rcx, %rax
cmpq $0x20000000, %rax       # imm = 0x20000000
jge 0x44319
movslq -0x24(%rbp), %rax
movslq -0x1c(%rbp), %rcx
imulq %rcx, %rax
movq %rax, -0x60(%rbp)
jmp 0x44324
movl $0x20000000, %eax       # imm = 0x20000000
movq %rax, -0x60(%rbp)
jmp 0x44324
movq -0x60(%rbp), %rax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x4(%rax)
cmpq $0x0, 0x7b2c7(%rip)     # 0xbf600
jne 0x4436d
movl -0x1c(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x54(%rax)
movslq -0x1c(%rbp), %rdi
callq 0xdba0
movq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x40(%rax)
jne 0x4436b
movl $0x7, -0x44(%rbp)
jmp 0x4436d
jmp 0x4436f
movq -0x38(%rbp), %rax
movzwl 0x6(%rax), %eax
movq -0x38(%rbp), %rcx
movzwl 0x8(%rcx), %ecx
addl %ecx, %eax
cmpl $0xd, %eax
jge 0x443ab
movq -0x38(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x443a3
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x8(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x443ab
movq -0x30(%rbp), %rax
movb $0x3, 0x5c(%rax)
jmp 0x443ad
movl -0x44(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)