// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movzbl 0x1(%rax), %eax
movl %eax, -0x40(%rbp)
cmpl $0x80, -0x40(%rbp)
jl 0x3fd140
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
addq $0x1, %rdi
leaq -0x40(%rbp), %rsi
callq 0x3fa520
cmpl $0x0, -0x40(%rbp)
jle 0x3fd162
cmpl $0xa, -0x40(%rbp)
jge 0x3fd162
cmpl $0x7, -0x40(%rbp)
je 0x3fd162
movq -0x20(%rbp), %rax
movzbl 0x5c(%rax), %ecx
andl $0x1, %ecx
movb %cl, 0x5c(%rax)
jmp 0x3fd18d
cmpl $0xa, -0x40(%rbp)
jle 0x3fd183
movl -0x40(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x3fd183
movq -0x20(%rbp), %rax
movzbl 0x5c(%rax), %ecx
andl $0x2, %ecx
movb %cl, 0x5c(%rax)
jmp 0x3fd18b
movq -0x20(%rbp), %rax
movb $0x0, 0x5c(%rax)
jmp 0x3fd18d
movq -0x18(%rbp), %rax
movslq 0xc(%rax), %rax
addq $0x10, %rax
movl %eax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movslq 0xc(%rax), %rdi
callq 0x3faa30
movl %eax, %ecx
movl -0x60(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x3c(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0x3fd27b
movq -0x20(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x3fd209
movq -0x20(%rbp), %rcx
xorl %eax, %eax
cmpl $0x0, 0x50(%rcx)
movb %al, -0x61(%rbp)
je 0x3fd1fc
movq -0x20(%rbp), %rax
movl 0x50(%rax), %eax
addl -0x38(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x4(%rcx), %eax
setg %al
movb %al, -0x61(%rbp)
movb -0x61(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x34(%rbp)
jmp 0x3fd251
movq -0x20(%rbp), %rax
movl 0x48(%rax), %ecx
movq -0x20(%rbp), %rdx
movb $0x1, %al
cmpl 0x4(%rdx), %ecx
movb %al, -0x62(%rbp)
jg 0x3fd246
movq -0x20(%rbp), %rax
movl 0x48(%rax), %ecx
movq -0x20(%rbp), %rdx
xorl %eax, %eax
cmpl (%rdx), %ecx
movb %al, -0x63(%rbp)
jle 0x3fd240
callq 0x40a770
cmpl $0x0, %eax
setne %al
movb %al, -0x63(%rbp)
movb -0x63(%rbp), %al
movb %al, -0x62(%rbp)
movb -0x62(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x3fd279
movq -0x20(%rbp), %rdi
callq 0x4085d0
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rax
movl $0x0, 0x48(%rax)
movq -0x20(%rbp), %rax
movl $0x0, 0x50(%rax)
jmp 0x3fd27b
movl -0x3c(%rbp), %ecx
movq -0x20(%rbp), %rax
addl 0x48(%rax), %ecx
movl %ecx, 0x48(%rax)
movl -0x3c(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jle 0x3fd29e
movl -0x3c(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x40(%rax)
je 0x3fd3fc
movq -0x20(%rbp), %rax
movl 0x50(%rax), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x54(%rcx), %eax
jle 0x3fd3a9
movq -0x20(%rbp), %rax
movslq 0x54(%rax), %rax
shlq %rax
movq %rax, -0x58(%rbp)
movl $0xffffffff, -0x5c(%rbp) # imm = 0xFFFFFFFF
movq -0x20(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x3fd301
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x40(%rcx), %rcx
subq %rcx, %rax
movl %eax, -0x5c(%rbp)
jmp 0x3fd303
movq -0x58(%rbp), %rax
movslq -0x44(%rbp), %rcx
cmpq %rcx, %rax
jge 0x3fd31d
movq -0x58(%rbp), %rax
shlq %rax
movq %rax, -0x58(%rbp)
jmp 0x3fd303
movq -0x58(%rbp), %rax
movq -0x20(%rbp), %rcx
movslq 0x4(%rcx), %rcx
cmpq %rcx, %rax
jle 0x3fd33a
movq -0x20(%rbp), %rax
movslq 0x4(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
movslq -0x44(%rbp), %rcx
cmpq %rcx, %rax
jge 0x3fd34f
movslq -0x44(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rdi
movq -0x58(%rbp), %rsi
callq 0x3c0910
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x3fd377
movl $0x7, -0x4(%rbp)
jmp 0x3fd465
cmpl $0x0, -0x5c(%rbp)
jl 0x3fd390
movq -0x50(%rbp), %rcx
movslq -0x5c(%rbp), %rax
addq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x50(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x58(%rbp), %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x54(%rax)
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rax
movq -0x20(%rbp), %rcx
movslq 0x50(%rcx), %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movl -0x38(%rbp), %ecx
addl $0x7, %ecx
andl $-0x8, %ecx
movq -0x20(%rbp), %rax
addl 0x50(%rax), %ecx
movl %ecx, 0x50(%rax)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0x3fd3fa
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rax
movq -0x20(%rbp), %rcx
movq 0x40(%rcx), %rcx
subq %rcx, %rax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x8(%rax)
jmp 0x3fd429
movslq -0x38(%rbp), %rdi
callq 0x3c0760
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x3fd419
movl $0x7, -0x4(%rbp)
jmp 0x3fd465
movq -0x20(%rbp), %rax
movq 0x38(%rax), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rdi
addq $0x10, %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x18(%rbp), %rax
movslq 0xc(%rax), %rdx
callq 0x19610
movq -0x18(%rbp), %rax
movl 0xc(%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movq -0x30(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x38(%rax)
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nop