// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3VdbeSorterRewind
SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
  VdbeSorter *pSorter = pCsr->pSorter;
  int rc;                         /* Return code */
  sqlite3_file *pTemp2 = 0;       /* Second temp file to use */
  i64 iWrite2 = 0;                /* Write offset for pTemp2 */
  int nIter;                      /* Number of iterators used */
  int nByte;                      /* Bytes of space required for aIter/aTree */
  int N = 2;                      /* Power of 2 >= nIter */

  assert( pSorter );

  /* If no data has been written to disk, then do not do so now. Instead,
  ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
  ** from the in-memory list.  */
  if( pSorter->nPMA==0 ){
    *pbEof = !pSorter->pRecord;
    assert( pSorter->aTree==0 );
    return vdbeSorterSort(pCsr);
  }

  /* Write the current in-memory list to a PMA. */
  rc = vdbeSorterListToPMA(db, pCsr);
  if( rc!=SQLITE_OK ) return rc;

  /* Allocate space for aIter[] and aTree[]. */
  nIter = pSorter->nPMA;
  if( nIter>SORTER_MAX_MERGE_COUNT ) nIter = SORTER_MAX_MERGE_COUNT;
  assert( nIter>0 );
  while( N<nIter ) N += N;
  nByte = N * (sizeof(int) + sizeof(VdbeSorterIter));
  pSorter->aIter = (VdbeSorterIter *)sqlite3DbMallocZero(db, nByte);
  if( !pSorter->aIter ) return SQLITE_NOMEM;
  pSorter->aTree = (int *)&pSorter->aIter[N];
  pSorter->nTree = N;

  do {
    int iNew;                     /* Index of new, merged, PMA */

    for(iNew=0; 
        rc==SQLITE_OK && iNew*SORTER_MAX_MERGE_COUNT<pSorter->nPMA; 
        iNew++
    ){
      int rc2;                    /* Return code from fileWriterFinish() */
      FileWriter writer;          /* Object used to write to disk */
      i64 nWrite;                 /* Number of bytes in new PMA */

      memset(&writer, 0, sizeof(FileWriter));

      /* If there are SORTER_MAX_MERGE_COUNT or less PMAs in file pTemp1,
      ** initialize an iterator for each of them and break out of the loop.
      ** These iterators will be incrementally merged as the VDBE layer calls
      ** sqlite3VdbeSorterNext().
      **
      ** Otherwise, if pTemp1 contains more than SORTER_MAX_MERGE_COUNT PMAs,
      ** initialize interators for SORTER_MAX_MERGE_COUNT of them. These PMAs
      ** are merged into a single PMA that is written to file pTemp2.
      */
      rc = vdbeSorterInitMerge(db, pCsr, &nWrite);
      assert( rc!=SQLITE_OK || pSorter->aIter[ pSorter->aTree[1] ].pFile );
      if( rc!=SQLITE_OK || pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
        break;
      }

      /* Open the second temp file, if it is not already open. */
      if( pTemp2==0 ){
        assert( iWrite2==0 );
        rc = vdbeSorterOpenTempFile(db, &pTemp2);
      }

      if( rc==SQLITE_OK ){
        int bEof = 0;
        fileWriterInit(db, pTemp2, &writer, iWrite2);
        fileWriterWriteVarint(&writer, nWrite);
        while( rc==SQLITE_OK && bEof==0 ){
          VdbeSorterIter *pIter = &pSorter->aIter[ pSorter->aTree[1] ];
          assert( pIter->pFile );

          fileWriterWriteVarint(&writer, pIter->nKey);
          fileWriterWrite(&writer, pIter->aKey, pIter->nKey);
          rc = sqlite3VdbeSorterNext(db, pCsr, &bEof);
        }
        rc2 = fileWriterFinish(db, &writer, &iWrite2);
        if( rc==SQLITE_OK ) rc = rc2;
      }
    }

    if( pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
      break;
    }else{
      sqlite3_file *pTmp = pSorter->pTemp1;
      pSorter->nPMA = iNew;
      pSorter->pTemp1 = pTemp2;
      pTemp2 = pTmp;
      pSorter->iWriteOff = iWrite2;
      pSorter->iReadOff = 0;
      iWrite2 = 0;
    }
  }while( rc==SQLITE_OK );

  if( pTemp2 ){
    sqlite3OsCloseFree(pTemp2);
  }
  *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq %rax, -0x28(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x2, -0x4c(%rbp)
movq -0x28(%rbp), %rax
cmpl $0x0, 0x18(%rax)
jne 0x3dbde
movq -0x28(%rbp), %rax
cmpq $0x0, 0x40(%rax)
setne %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rdi
callq 0x45150
movl %eax, -0x4(%rbp)
jmp 0x3ded8
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x45290
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x3dbff
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x3ded8
movq -0x28(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x44(%rbp)
cmpl $0x10, -0x44(%rbp)
jle 0x3dc16
movl $0x10, -0x44(%rbp)
jmp 0x3dc18
movl -0x4c(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x3dc2b
movl -0x4c(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
jmp 0x3dc18
movslq -0x4c(%rbp), %rax
imulq $0x44, %rax, %rax
movl %eax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
movl -0x48(%rbp), %esi
callq 0x11ca0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x3dc64
movl $0x7, -0x4(%rbp)
jmp 0x3ded8
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rcx
movslq -0x4c(%rbp), %rax
shlq $0x6, %rax
addq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x30(%rax)
movl -0x4c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x14(%rax)
movl $0x0, -0x50(%rbp)
xorl %eax, %eax
cmpl $0x0, -0x2c(%rbp)
movb %al, -0xa9(%rbp)
jne 0x3dcb4
movl -0x50(%rbp), %eax
shll $0x4, %eax
movq -0x28(%rbp), %rcx
cmpl 0x18(%rcx), %eax
setl %al
movb %al, -0xa9(%rbp)
movb -0xa9(%rbp), %al
testb $0x1, %al
jne 0x3dcc3
jmp 0x3de2d
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x8200
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x90(%rbp), %rdx
callq 0x453f0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x3dcfd
movq -0x28(%rbp), %rax
cmpl $0x10, 0x18(%rax)
jg 0x3dd02
jmp 0x3de2d
cmpq $0x0, -0x38(%rbp)
jne 0x3dd19
movq -0x10(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x45500
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x3de1d
movl $0x0, -0x94(%rbp)
movq -0x10(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rcx
leaq -0x88(%rbp), %rdx
callq 0x45540
movq -0x90(%rbp), %rsi
leaq -0x88(%rbp), %rdi
callq 0x45600
xorl %eax, %eax
cmpl $0x0, -0x2c(%rbp)
movb %al, -0xaa(%rbp)
jne 0x3dd76
cmpl $0x0, -0x94(%rbp)
sete %al
movb %al, -0xaa(%rbp)
movb -0xaa(%rbp), %al
testb $0x1, %al
jne 0x3dd82
jmp 0x3ddf8
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x30(%rcx), %rcx
movslq 0x4(%rcx), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movslq 0x14(%rax), %rsi
leaq -0x88(%rbp), %rdi
callq 0x45600
movq -0xa0(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0xa0(%rbp), %rax
movl 0x14(%rax), %edx
leaq -0x88(%rbp), %rdi
callq 0x45640
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x94(%rbp), %rdx
callq 0x3def0
movl %eax, -0x2c(%rbp)
jmp 0x3dd58
movq -0x10(%rbp), %rdi
leaq -0x88(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x45780
movl %eax, -0x54(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x3de1b
movl -0x54(%rbp), %eax
movl %eax, -0x2c(%rbp)
jmp 0x3de1d
jmp 0x3de1f
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x3dc90
movq -0x28(%rbp), %rax
cmpl $0x10, 0x18(%rax)
jg 0x3de39
jmp 0x3de94
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0xa8(%rbp)
movl -0x50(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x38(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0xa8(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rax
movq $0x0, 0x8(%rax)
movq $0x0, -0x40(%rbp)
jmp 0x3de8a
cmpl $0x0, -0x2c(%rbp)
je 0x3dc89
cmpq $0x0, -0x38(%rbp)
je 0x3dea4
movq -0x38(%rbp), %rdi
callq 0x300e0
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x30(%rcx), %rcx
movslq 0x4(%rcx), %rcx
shlq $0x6, %rcx
addq %rcx, %rax
cmpq $0x0, 0x18(%rax)
sete %al
andb $0x1, %al
movzbl %al, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl -0x2c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)