// File: /workspace/github_c_makefile_star2_O1/jkbenaim[P]psfrag/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[] */
  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->pBtx==0 );
  assert( pCsr->eCurType==CURTYPE_SORTER );
  szKeyInfo = sizeof(KeyInfo) + (pCsr->pKeyInfo->nKeyField-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_BKPT;
  }else{
    pSorter->pKeyInfo = pKeyInfo = (KeyInfo*)((u8*)pSorter + sz);
    memcpy(pKeyInfo, pCsr->pKeyInfo, szKeyInfo);
    pKeyInfo->db = 0;
    if( nField && nWorker==0 ){
      pKeyInfo->nKeyField = 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) ){
      i64 mxCache;                /* Cache size in bytes*/
      u32 szPma = sqlite3GlobalConfig.szPma;
      pSorter->mnPmaSize = szPma * pgsz;

      mxCache = db->aDb[0].pSchema->cache_size;
      if( mxCache<0 ){
        /* A negative cache-size value C indicates that the cache is abs(C)
        ** KiB in size.  */
        mxCache = mxCache * -1024;
      }else{
        mxCache = mxCache * pgsz;
      }
      mxCache = MIN(mxCache, SQLITE_MAX_PMASZ);
      pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);

      /* Avoid large memory allocations if the application has requested
      ** SQLITE_CONFIG_SMALL_MALLOC. */
      if( sqlite3GlobalConfig.bSmallMalloc==0 ){
        assert( pSorter->iMemory==0 );
        pSorter->nMemory = pgsz;
        pSorter->list.aMemory = (u8*)sqlite3Malloc(pgsz);
        if( !pSorter->list.aMemory ) rc = SQLITE_NOMEM_BKPT;
      }
    }

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

  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r12
movq %rdi, %rbx
xorl %r13d, %r13d
cmpb $0x2, 0x60(%rdi)
movl %esi, 0x4(%rsp)
je 0x34e6f
cmpb $0x0, 0x7542e(%rip)     # 0xaa294
je 0x34e6f
movl 0xa8(%rbx), %r13d
movq 0x38(%r12), %rax
movzwl 0x6(%rax), %r15d
imull $0x68, %r13d, %eax
addl $0xc8, %eax
movslq %eax, %rbp
leaq 0x20(,%r15,8), %rsi
addq %rbp, %rsi
movq %rbx, %rdi
callq 0x11de0
movq %rax, 0x30(%r12)
testq %rax, %rax
je 0x34fc6
movq %rax, %r14
leaq 0x20(,%r15,8), %rax
movq %r14, %r15
addq %rbp, %r15
movq %r15, 0x28(%r14)
movq 0x38(%r12), %rsi
movl %eax, %edx
movq %r15, %rdi
callq 0x81d0
movq $0x0, 0x10(%r14,%rbp)
movl 0x4(%rsp), %edx
testl %edx, %edx
sete %al
testl %r13d, %r13d
setne %cl
orb %al, %cl
jne 0x34eeb
movw %dx, 0x6(%r15)
movq 0x20(%rbx), %rdx
movq 0x8(%rdx), %rax
movq 0x8(%rax), %rax
movl 0x30(%rax), %ecx
leal 0x1(%r13), %eax
leal -0x1(%r13), %esi
cmpb $0x2, %al
setae 0x59(%r14)
movl %ecx, 0xc(%r14)
incb %r13b
movb %r13b, 0x5b(%r14)
movb %sil, 0x5a(%r14)
movq %rbx, 0x20(%r14)
je 0x34f36
movzbl %r13b, %eax
imulq $0x68, %rax, %rax
xorl %esi, %esi
movq %r14, 0x70(%r14,%rsi)
addq $0x68, %rsi
cmpq %rsi, %rax
jne 0x34f28
xorl %eax, %eax
cmpb $0x2, 0x60(%rbx)
je 0x34f9f
movl 0x7549c(%rip), %esi     # 0xaa3e0
imull %ecx, %esi
movl %esi, (%r14)
movq 0x18(%rdx), %rdx
movslq 0x74(%rdx), %rdx
testq %rdx, %rdx
movl $0xfffffc00, %edi       # imm = 0xFFFFFC00
cmovnsl %ecx, %edi
movslq %edi, %rdi
imulq %rdx, %rdi
movl $0x20000000, %edx       # imm = 0x20000000
cmpq %rdx, %rdi
cmovgeq %rdx, %rdi
cmpl %edi, %esi
cmovlel %edi, %esi
movl %esi, 0x4(%r14)
cmpb $0x0, 0x75318(%rip)     # 0xaa298
jne 0x34f9f
movslq %ecx, %rdi
movl %ecx, 0x54(%r14)
callq 0xa88d
movq %rax, 0x40(%r14)
xorl %ecx, %ecx
testq %rax, %rax
movl $0x7, %eax
cmovnel %ecx, %eax
cmpw $0xc, 0x8(%r15)
ja 0x34fcb
movq 0x20(%r15), %rcx
testq %rcx, %rcx
je 0x34fb6
cmpq 0x10(%rbx), %rcx
jne 0x34fcb
movq 0x18(%r15), %rcx
testb $0x2, (%rcx)
jne 0x34fcb
movb $0x3, 0x5c(%r14)
jmp 0x34fcb
movl $0x7, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq