// File: /workspace/github_c_makefile_star2_O3/shifengbin[P]bishe/Output/../Persistence/sqlite3.c
// Function: sqlite3VdbeSorterWrite
SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
  const VdbeCursor *pCsr,         /* Sorter cursor */
  Mem *pVal                       /* Memory cell containing record */
){
  VdbeSorter *pSorter;
  int rc = SQLITE_OK;             /* Return Code */
  SorterRecord *pNew;             /* New list element */
  int bFlush;                     /* True to flush contents of memory to PMA */
  int nReq;                       /* Bytes of memory required */
  int nPMA;                       /* Bytes of PMA space required */
  int t;                          /* serial type of first record field */

  assert( pCsr->eCurType==CURTYPE_SORTER );
  pSorter = pCsr->uc.pSorter;
  getVarint32((const u8*)&pVal->z[1], t);
  if( t>0 && t<10 && t!=7 ){
    pSorter->typeMask &= SORTER_TYPE_INTEGER;
  }else if( t>10 && (t & 0x01) ){
    pSorter->typeMask &= SORTER_TYPE_TEXT;
  }else{
    pSorter->typeMask = 0;
  }

  assert( pSorter );

  /* Figure out whether or not the current contents of memory should be
  ** flushed to a PMA before continuing. If so, do so.
  **
  ** If using the single large allocation mode (pSorter->aMemory!=0), then
  ** flush the contents of memory to a new PMA if (a) at least one value is
  ** already in memory and (b) the new value will not fit in memory.
  ** 
  ** Or, if using separate allocations for each record, flush the contents
  ** of memory to a PMA if either of the following are true:
  **
  **   * The total memory allocated for the in-memory list is greater 
  **     than (page-size * cache-size), or
  **
  **   * The total memory allocated for the in-memory list is greater 
  **     than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
  */
  nReq = pVal->n + sizeof(SorterRecord);
  nPMA = pVal->n + sqlite3VarintLen(pVal->n);
  if( pSorter->mxPmaSize ){
    if( pSorter->list.aMemory ){
      bFlush = pSorter->iMemory && (pSorter->iMemory+nReq) > pSorter->mxPmaSize;
    }else{
      bFlush = (
          (pSorter->list.szPMA > pSorter->mxPmaSize)
       || (pSorter->list.szPMA > pSorter->mnPmaSize && sqlite3HeapNearlyFull())
      );
    }
    if( bFlush ){
      rc = vdbeSorterFlushPMA(pSorter);
      pSorter->list.szPMA = 0;
      pSorter->iMemory = 0;
      assert( rc!=SQLITE_OK || pSorter->list.pList==0 );
    }
  }

  pSorter->list.szPMA += nPMA;
  if( nPMA>pSorter->mxKeysize ){
    pSorter->mxKeysize = nPMA;
  }

  if( pSorter->list.aMemory ){
    int nMin = pSorter->iMemory + nReq;

    if( nMin>pSorter->nMemory ){
      u8 *aNew;
      int nNew = pSorter->nMemory * 2;
      while( nNew < nMin ) nNew = nNew*2;
      if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
      if( nNew < nMin ) nNew = nMin;

      aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
      if( !aNew ) return SQLITE_NOMEM;
      pSorter->list.pList = (SorterRecord*)(
          aNew + ((u8*)pSorter->list.pList - pSorter->list.aMemory)
      );
      pSorter->list.aMemory = aNew;
      pSorter->nMemory = nNew;
    }

    pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
    pSorter->iMemory += ROUND8(nReq);
    pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
  }else{
    pNew = (SorterRecord *)sqlite3Malloc(nReq);
    if( pNew==0 ){
      return SQLITE_NOMEM;
    }
    pNew->u.pNext = pSorter->list.pList;
  }

  memcpy(SRVAL(pNew), pVal->z, pVal->n);
  pNew->nVal = pVal->n;
  pSorter->list.pList = pNew;

  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %r14
movq %rdi, %rbx
movq 0x10(%rsi), %rdi
movsbl 0x1(%rdi), %eax
testl %eax, %eax
jns 0x33fea
incq %rdi
leaq -0x30(%rbp), %r15
movq %r15, %rsi
callq 0x3220d
movl (%r15), %eax
leal -0x1(%rax), %ecx
cmpl $0x9, %ecx
setae %cl
cmpl $0x7, %eax
sete %dl
orb %cl, %dl
jne 0x34003
andb $0x1, 0x5c(%rbx)
jmp 0x3401c
cmpl $0xb, %eax
setl %cl
testb $0x1, %al
sete %al
orb %cl, %al
je 0x34018
movb $0x0, 0x5c(%rbx)
jmp 0x3401c
andb $0x2, 0x5c(%rbx)
movslq 0xc(%r14), %r13
movl $0x1, %eax
cmpq $0x80, %r13
jb 0x3404b
movl $0x1, %eax
movq %r13, %rdx
movq %r13, %rcx
shrq $0x7, %rcx
incl %eax
cmpq $0x3fff, %rdx           # imm = 0x3FFF
movq %rcx, %rdx
ja 0x34039
leal 0x10(%r13), %r12d
leal (%rax,%r13), %r15d
movl 0x4(%rbx), %eax
xorl %esi, %esi
testl %eax, %eax
je 0x3409f
cmpq $0x0, 0x40(%rbx)
je 0x34079
movl 0x50(%rbx), %ecx
testl %ecx, %ecx
sete %dl
addl %r12d, %ecx
cmpl %eax, %ecx
setle %al
orb %dl, %al
je 0x3408d
jmp 0x3409f
movl 0x48(%rbx), %ecx
cmpl %eax, %ecx
jg 0x3408d
cmpl (%rbx), %ecx
jle 0x3409f
cmpl $0x0, 0x5eff1(%rip)     # 0x9307c
je 0x3409f
movq %rbx, %rdi
callq 0x3a6f7
movl %eax, %esi
xorl %eax, %eax
movl %eax, 0x48(%rbx)
movl %eax, 0x50(%rbx)
addl %r15d, 0x48(%rbx)
cmpl 0x8(%rbx), %r15d
jle 0x340ad
movl %r15d, 0x8(%rbx)
movq 0x40(%rbx), %rax
testq %rax, %rax
movl %esi, -0x2c(%rbp)
je 0x3410c
movl 0x50(%rbx), %edx
movl 0x54(%rbx), %ecx
addl %edx, %r12d
cmpl %ecx, %r12d
jle 0x34127
addl %ecx, %ecx
cmpl %r12d, %ecx
jl 0x340c7
movl 0x4(%rbx), %r15d
cmpl %r15d, %ecx
cmovll %ecx, %r15d
cmpl %r12d, %r15d
cmovlel %r12d, %r15d
movslq %r15d, %rsi
movq %rax, %rdi
callq 0xc2eb
testq %rax, %rax
je 0x3416a
movq 0x38(%rbx), %rcx
subq 0x40(%rbx), %rcx
addq %rax, %rcx
movq %rcx, 0x38(%rbx)
movq %rax, 0x40(%rbx)
movl %r15d, 0x54(%rbx)
movl 0x50(%rbx), %edx
jmp 0x3412b
movslq %r12d, %rdi
callq 0xc0e2
testq %rax, %rax
je 0x3416a
movq %rax, %r12
movq 0x38(%rbx), %rax
movq %rax, 0x8(%r12)
jmp 0x34147
movq 0x38(%rbx), %rcx
movslq %edx, %rdx
leaq (%rax,%rdx), %r12
addl $0x17, %r13d
andl $-0x8, %r13d
addl %edx, %r13d
movl %r13d, 0x50(%rbx)
subl %eax, %ecx
movl %ecx, 0x8(%rax,%rdx)
leaq 0x10(%r12), %rdi
movq 0x10(%r14), %rsi
movslq 0xc(%r14), %rdx
callq 0xa320
movl 0xc(%r14), %eax
movl %eax, (%r12)
movq %r12, 0x38(%rbx)
movl -0x2c(%rbp), %eax
jmp 0x3416f
movl $0x7, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq