// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/external/sqlite-3.33.0/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;
  getVarint32NR((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;
      sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
      int iListOff = -1;
      if( pSorter->list.pList ){
        iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
      }
      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_BKPT;
      if( iListOff>=0 ){
        pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
      }
      pSorter->list.aMemory = aNew;
      pSorter->nMemory = nNew;
    }

    pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
    pSorter->iMemory += ROUND8(nReq);
    if( pSorter->list.pList ){
      pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
    }
  }else{
    pNew = (SorterRecord *)sqlite3Malloc(nReq);
    if( pNew==0 ){
      return SQLITE_NOMEM_BKPT;
    }
    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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq 0x30(%rdi), %rbx
movq 0x10(%rsi), %rdi
movzbl 0x1(%rdi), %eax
movl %eax, 0xc(%rsp)
testb %al, %al
jns 0x2a68bf
incq %rdi
leaq 0xc(%rsp), %rsi
callq 0x2a470c
movl 0xc(%rsp), %eax
leal -0x1(%rax), %ecx
cmpl $0x9, %ecx
setae %cl
cmpl $0x7, %eax
sete %dl
orb %cl, %dl
jne 0x2a68dc
andb $0x1, 0x5c(%rbx)
jmp 0x2a68f5
cmpl $0xb, %eax
setl %cl
testb $0x1, %al
sete %al
orb %cl, %al
je 0x2a68f1
movb $0x0, 0x5c(%rbx)
jmp 0x2a68f5
andb $0x2, 0x5c(%rbx)
movslq 0xc(%r14), %r12
movl $0x1, %eax
cmpq $0x80, %r12
jb 0x2a6924
movl $0x1, %eax
movq %r12, %rdx
movq %r12, %rcx
shrq $0x7, %rcx
incl %eax
cmpq $0x3fff, %rdx           # imm = 0x3FFF
movq %rcx, %rdx
ja 0x2a6912
leal 0x10(%r12), %r15d
leal (%r12,%rax), %r13d
movl 0x4(%rbx), %eax
xorl %ebp, %ebp
testl %eax, %eax
je 0x2a6972
cmpq $0x0, 0x40(%rbx)
je 0x2a6955
movl 0x50(%rbx), %ecx
testl %ecx, %ecx
setne %dl
addl %r15d, %ecx
cmpl %eax, %ecx
setg %al
andb %dl, %al
testb %al, %al
jne 0x2a6960
jmp 0x2a6972
movl 0x48(%rbx), %ecx
cmpl %eax, %ecx
jle 0x2a6a5d
movq %rbx, %rdi
callq 0x2ad934
movl %eax, %ebp
xorl %eax, %eax
movl %eax, 0x48(%rbx)
movl %eax, 0x50(%rbx)
addl %r13d, 0x48(%rbx)
cmpl 0x8(%rbx), %r13d
jle 0x2a6980
movl %r13d, 0x8(%rbx)
movq 0x40(%rbx), %rdi
testq %rdi, %rdi
je 0x2a6a3c
movslq 0x50(%rbx), %rcx
movslq %r15d, %rax
addq %rcx, %rax
movslq 0x54(%rbx), %rcx
cmpl %ecx, %eax
jle 0x2a6a0e
movl %ebp, 0x8(%rsp)
movq %r14, 0x10(%rsp)
movq 0x38(%rbx), %rdx
movl %edx, %ebp
subl %edi, %ebp
addq %rcx, %rcx
cmpq %rax, %rcx
jl 0x2a69b0
xorl %r13d, %r13d
cmpq $0x1, %rdx
movl $0x0, %r14d
sbbl %r14d, %r14d
movslq 0x4(%rbx), %r15
cmpq %r15, %rcx
cmovlq %rcx, %r15
cmpq %rax, %r15
cmovleq %rax, %r15
movq %r15, %rsi
callq 0x27c4f3
testq %rax, %rax
je 0x2a6a75
orl %ebp, %r14d
js 0x2a69fd
movl %r14d, %ecx
movq %rax, %rdx
addq %rcx, %rdx
movq %rdx, 0x38(%rbx)
movq %rax, 0x40(%rbx)
movl %r15d, 0x54(%rbx)
movq 0x10(%rsp), %r14
movl 0x8(%rsp), %ebp
movq 0x38(%rbx), %rax
movq 0x40(%rbx), %rcx
movslq 0x50(%rbx), %r15
addl $0x17, %r12d
andl $-0x8, %r12d
addl %r15d, %r12d
addq %rcx, %r15
movl %r12d, 0x50(%rbx)
movb $0x1, %r13b
testq %rax, %rax
je 0x2a6a7e
subl %ecx, %eax
movl %eax, 0x8(%r15)
jmp 0x2a6a7e
movslq %r15d, %rdi
callq 0x27c381
testq %rax, %rax
je 0x2a6a56
movq %rax, %r15
movq 0x38(%rbx), %rax
movq %rax, 0x8(%r15)
jmp 0x2a6a88
movl $0x7, %eax
jmp 0x2a6aa6
cmpl (%rbx), %ecx
jle 0x2a6972
movl 0x22314d(%rip), %eax    # 0x4c9bb8
testl %eax, %eax
setne %al
jmp 0x2a694f
movq 0x10(%rsp), %r14
movl 0x8(%rsp), %ebp
movl $0x7, %eax
testb %r13b, %r13b
je 0x2a6aa6
leaq 0x10(%r15), %rdi
movq 0x10(%r14), %rsi
movslq 0xc(%r14), %rdx
callq 0x17660
movl 0xc(%r14), %eax
movl %eax, (%r15)
movq %r15, 0x38(%rbx)
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq