// File: /workspace/github_c_makefile_star2_O0/genometools[P]genometools/src/external/sqlite-3.33.0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movzwl 0x6(%rax), %eax
subl $0x1, %eax
cltq
shlq $0x3, %rax
addq $0x28, %rax
movl %eax, -0x34(%rbp)
movl $0xc8, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movl -0x38(%rbp), %eax
addl -0x34(%rbp), %eax
movslq %eax, %rsi
callq 0x3c9520
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
cmpq $0x0, -0x28(%rbp)
jne 0x3fb382
movl $0x7, -0x3c(%rbp)
jmp 0x3fb5a0
movq -0x28(%rbp), %rcx
movslq -0x38(%rbp), %rax
addq %rax, %rcx
movq %rcx, -0x30(%rbp)
movq -0x28(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x30(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rsi
movslq -0x34(%rbp), %rdx
callq 0x19610
movq -0x30(%rbp), %rax
movq $0x0, 0x10(%rax)
cmpl $0x0, -0xc(%rbp)
je 0x3fb3ce
movl -0xc(%rbp), %eax
movw %ax, %cx
movq -0x30(%rbp), %rax
movw %cx, 0x6(%rax)
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x8(%rax), %rdi
callq 0x3c5fc0
movl %eax, %ecx
movl %ecx, -0x1c(%rbp)
movq -0x28(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x28(%rbp), %rax
movb $0x1, 0x5b(%rax)
movq -0x28(%rbp), %rax
movb $-0x1, 0x5a(%rax)
movq -0x28(%rbp), %rax
movzbl 0x5b(%rax), %eax
cmpl $0x1, %eax
setg %al
andb $0x1, %al
movzbl %al, %eax
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, 0x59(%rax)
movq -0x8(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x20(%rax)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x28(%rbp), %rcx
movzbl 0x5b(%rcx), %ecx
cmpl %ecx, %eax
jge 0x3fb467
movq -0x28(%rbp), %rax
addq $0x60, %rax
movslq -0x20(%rbp), %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x28(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x3fb42a
movq -0x8(%rbp), %rdi
callq 0x3dac70
cmpl $0x0, %eax
jne 0x3fb559
movl 0x244521(%rip), %eax    # 0x63f9a0
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %ecx
imull -0x1c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x18(%rax), %rax
movslq 0x74(%rax), %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jge 0x3fb4b8
imulq $-0x400, -0x50(%rbp), %rax # imm = 0xFC00
movq %rax, -0x50(%rbp)
jmp 0x3fb4c8
movq -0x50(%rbp), %rax
movslq -0x1c(%rbp), %rcx
imulq %rcx, %rax
movq %rax, -0x50(%rbp)
cmpq $0x20000000, -0x50(%rbp) # imm = 0x20000000
jge 0x3fb4dc
movq -0x50(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x3fb4e7
movl $0x20000000, %eax       # imm = 0x20000000
movq %rax, -0x60(%rbp)
jmp 0x3fb4e7
movq -0x60(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x28(%rbp), %rax
movl (%rax), %eax
movq -0x50(%rbp), %rcx
cmpl %ecx, %eax
jle 0x3fb508
movq -0x28(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x64(%rbp)
jmp 0x3fb50f
movq -0x50(%rbp), %rax
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x4(%rax)
movzbl 0x244338(%rip), %eax    # 0x63f858
cmpl $0x0, %eax
jne 0x3fb557
movl -0x1c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x54(%rax)
movslq -0x1c(%rbp), %rdi
callq 0x3c0760
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x40(%rax)
jne 0x3fb555
movl $0x7, -0x3c(%rbp)
jmp 0x3fb557
jmp 0x3fb559
movq -0x30(%rbp), %rax
movzwl 0x8(%rax), %eax
cmpl $0xd, %eax
jge 0x3fb59e
movq -0x30(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x3fb583
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x8(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jne 0x3fb59e
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movzbl (%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
jne 0x3fb59e
movq -0x28(%rbp), %rax
movb $0x3, 0x5c(%rax)
jmp 0x3fb5a0
movl -0x3c(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)