// File: /workspace/github_c_makefile_star2_O3/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: pager_playback_one_page
static int pager_playback_one_page(
  Pager *pPager,                /* The pager being played back */
  i64 *pOffset,                 /* Offset of record to playback */
  Bitvec *pDone,                /* Bitvec of pages already played back */
  int isMainJrnl,               /* 1 -> main journal. 0 -> sub-journal. */
  int isSavepnt                 /* True for a savepoint rollback */
){
  int rc;
  PgHdr *pPg;                   /* An existing page in the cache */
  Pgno pgno;                    /* The page number of a page in journal */
  u32 cksum;                    /* Checksum used for sanity checking */
  char *aData;                  /* Temporary storage for the page */
  sqlite3_file *jfd;            /* The file descriptor for the journal file */
  int isSynced;                 /* True if journal page is synced */

  assert( (isMainJrnl&~1)==0 );      /* isMainJrnl is 0 or 1 */
  assert( (isSavepnt&~1)==0 );       /* isSavepnt is 0 or 1 */
  assert( isMainJrnl || pDone );     /* pDone always used on sub-journals */
  assert( isSavepnt || pDone==0 );   /* pDone never used on non-savepoint */

  aData = pPager->pTmpSpace;
  assert( aData );         /* Temp storage must have already been allocated */
  assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );

  /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
  ** or savepoint rollback done at the request of the caller) or this is
  ** a hot-journal rollback. If it is a hot-journal rollback, the pager
  ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
  ** only reads from the main journal, not the sub-journal.
  */
  assert( pPager->eState>=PAGER_WRITER_CACHEMOD
       || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
  );
  assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );

  /* Read the page number and page data from the journal or sub-journal
  ** file. Return an error code to the caller if an IO error occurs.
  */
  jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
  rc = read32bits(jfd, *pOffset, &pgno);
  if( rc!=SQLITE_OK ) return rc;
  rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
  if( rc!=SQLITE_OK ) return rc;
  *pOffset += pPager->pageSize + 4 + isMainJrnl*4;

  /* Sanity checking on the page.  This is more important that I originally
  ** thought.  If a power failure occurs while the journal is being written,
  ** it could cause invalid data to be written into the journal.  We need to
  ** detect this invalid data (with high probability) and ignore it.
  */
  if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
    assert( !isSavepnt );
    return SQLITE_DONE;
  }
  if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
    return SQLITE_OK;
  }
  if( isMainJrnl ){
    rc = read32bits(jfd, (*pOffset)-4, &cksum);
    if( rc ) return rc;
    if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
      return SQLITE_DONE;
    }
  }

  /* If this page has already been played back before during the current
  ** rollback, then don't bother to play it back again.
  */
  if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
    return rc;
  }

  /* When playing back page 1, restore the nReserve setting
  */
  if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
    pPager->nReserve = ((u8*)aData)[20];
  }

  /* If the pager is in CACHEMOD state, then there must be a copy of this
  ** page in the pager cache. In this case just update the pager cache,
  ** not the database file. The page is left marked dirty in this case.
  **
  ** An exception to the above rule: If the database is in no-sync mode
  ** and a page is moved during an incremental vacuum then the page may
  ** not be in the pager cache. Later: if a malloc() or IO error occurs
  ** during a Movepage() call, then the page may not be in the cache
  ** either. So the condition described in the above paragraph is not
  ** assert()able.
  **
  ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
  ** pager cache if it exists and the main file. The page is then marked
  ** not dirty. Since this code is only executed in PAGER_OPEN state for
  ** a hot-journal rollback, it is guaranteed that the page-cache is empty
  ** if the pager is in OPEN state.
  **
  ** Ticket #1171:  The statement journal might contain page content that is
  ** different from the page content at the start of the transaction.
  ** This occurs when a page is changed prior to the start of a statement
  ** then changed again within the statement.  When rolling back such a
  ** statement we must not write to the original database unless we know
  ** for certain that original page contents are synced into the main rollback
  ** journal.  Otherwise, a power loss might leave modified data in the
  ** database file without an entry in the rollback journal that can
  ** restore the database to its original form.  Two conditions must be
  ** met before writing to the database files. (1) the database must be
  ** locked.  (2) we know that the original page content is fully synced
  ** in the main journal either because the page is not in cache or else
  ** the page is marked as needSync==0.
  **
  ** 2008-04-14:  When attempting to vacuum a corrupt database file, it
  ** is possible to fail a statement on a database that does not yet exist.
  ** Do not attempt to write if database file has never been opened.
  */
  if( pagerUseWal(pPager) ){
    pPg = 0;
  }else{
    pPg = sqlite3PagerLookup(pPager, pgno);
  }
  assert( pPg || !MEMDB );
  assert( pPager->eState!=PAGER_OPEN || pPg==0 || pPager->tempFile );
  PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
           PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
           (isMainJrnl?"main-journal":"sub-journal")
  ));
  if( isMainJrnl ){
    isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
  }else{
    isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
  }
  if( isOpen(pPager->fd)
   && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
   && isSynced
  ){
    i64 ofst = (pgno-1)*(i64)pPager->pageSize;
    testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
    assert( !pagerUseWal(pPager) );

    /* Write the data read from the journal back into the database file.
    ** This is usually safe even for an encrypted database - as the data
    ** was encrypted before it was written to the journal file. The exception
    ** is if the data was just read from an in-memory sub-journal. In that
    ** case it must be encrypted here before it is copied into the database
    ** file.  */
    rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);

    if( pgno>pPager->dbFileSize ){
      pPager->dbFileSize = pgno;
    }
    if( pPager->pBackup ){
      sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
    }
  }else if( !isMainJrnl && pPg==0 ){
    /* If this is a rollback of a savepoint and data was not written to
    ** the database and the page is not in-memory, there is a potential
    ** problem. When the page is next fetched by the b-tree layer, it
    ** will be read from the database file, which may or may not be
    ** current.
    **
    ** There are a couple of different ways this can happen. All are quite
    ** obscure. When running in synchronous mode, this can only happen
    ** if the page is on the free-list at the start of the transaction, then
    ** populated, then moved using sqlite3PagerMovepage().
    **
    ** The solution is to add an in-memory page to the cache containing
    ** the data just read from the sub-journal. Mark the page as dirty
    ** and if the pager requires a journal-sync, then mark the page as
    ** requiring a journal-sync before it is written.
    */
    assert( isSavepnt );
    assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
    pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
    rc = sqlite3PagerGet(pPager, pgno, &pPg, 1);
    assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
    pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
    if( rc!=SQLITE_OK ) return rc;
    sqlite3PcacheMakeDirty(pPg);
  }
  if( pPg ){
    /* No page should ever be explicitly rolled back that is in use, except
    ** for page 1 which is held in use in order to keep the lock on the
    ** database active. However such a page may be rolled back as a result
    ** of an internal error resulting in an automatic call to
    ** sqlite3PagerRollback().
    */
    void *pData;
    pData = pPg->pData;
    memcpy(pData, (u8*)aData, pPager->pageSize);
    pPager->xReiniter(pPg);
    /* It used to be that sqlite3PcacheMakeClean(pPg) was called here.  But
    ** that call was dangerous and had no detectable benefit since the cache
    ** is normally cleaned by sqlite3PcacheCleanAll() after rollback and so
    ** has been removed. */
    pager_set_pagehash(pPg);

    /* If this was page 1, then restore the value of Pager.dbFileVers.
    ** Do this before any decoding. */
    if( pgno==1 ){
      memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
    }
    sqlite3PcacheRelease(pPg);
  }
  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r8d, %r12d
movl %ecx, %r14d
movq %rdx, %r13
movq %rsi, %rbp
movq %rdi, %rbx
movq 0x110(%rdi), %rax
movq %rax, 0x18(%rsp)
xorl %eax, %eax
testl %ecx, %ecx
sete %al
movq 0x50(%rdi,%rax,8), %r15
movq (%rsi), %rsi
leaq 0x2c(%rsp), %rdx
movq %r15, %rdi
callq 0x28ac09
testl %eax, %eax
jne 0x28a4f4
movl %r12d, 0x24(%rsp)
movq %r13, 0x10(%rsp)
movl 0xbc(%rbx), %edx
movq (%rbp), %rcx
addq $0x4, %rcx
movq (%r15), %rax
movq %r15, %rdi
movq 0x18(%rsp), %rsi
callq *0x10(%rax)
testl %eax, %eax
je 0x28a507
movl %eax, %esi
movl %esi, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbp, %r13
movl 0xbc(%rbx), %ecx
leal (%rcx,%r14,4), %eax
addl $0x4, %eax
movslq %eax, %r12
addq (%rbp), %r12
movq %r12, (%rbp)
movl 0x2c(%rsp), %r9d
movl $0x65, %esi
testl %r9d, %r9d
je 0x28a4f6
movl 0x231589(%rip), %eax    # 0x4bbac0
cltd
idivl %ecx
incl %eax
cmpl %eax, %r9d
je 0x28a4f6
xorl %esi, %esi
cmpl 0x1c(%rbx), %r9d
ja 0x28a4f6
movq %r14, %r10
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
movq %r9, 0x30(%rsp)
je 0x28a57f
movq 0x10(%rsp), %rdi
movl %r9d, %esi
callq 0x286b64
xorl %esi, %esi
movq 0x10(%rsp), %rdi
movq %r14, %r10
movq 0x30(%rsp), %r9
testl %eax, %eax
jne 0x28a4f6
testl %r10d, %r10d
je 0x28a5fc
addq $-0x4, %r12
leaq 0x28(%rsp), %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x28ac09
testl %eax, %eax
jne 0x28a4f4
cmpl $0x0, 0x24(%rsp)
movq 0x30(%rsp), %r9
movq %r14, %r10
movq 0x10(%rsp), %rdi
jne 0x28a5fc
movl 0x34(%rbx), %eax
movslq 0xbc(%rbx), %rcx
cmpq $0xc9, %rcx
jl 0x28a5ed
addq $0xc8, %rcx
movq 0x18(%rsp), %rsi
movzbl -0x190(%rsi,%rcx), %edx
addl %edx, %eax
addq $-0xc8, %rcx
cmpq $0x190, %rcx            # imm = 0x190
ja 0x28a5d3
movl $0x65, %esi
cmpl 0x28(%rsp), %eax
jne 0x28a4f6
testq %rdi, %rdi
je 0x28a61a
movl %r9d, %esi
movq %r9, %r15
callq 0x286fa5
movq %r14, %r10
movq %r15, %r9
testl %eax, %eax
jne 0x28a4f4
cmpl $0x1, %r9d
movq 0x18(%rsp), %r12
jne 0x28a63d
movswl 0xb2(%rbx), %ecx
movzbl 0x14(%r12), %eax
cmpl %eax, %ecx
je 0x28a63d
movw %ax, 0xb2(%rbx)
cmpq $0x0, 0x120(%rbx)
je 0x28a65c
xorl %r15d, %r15d
movq $0x0, 0x8(%rsp)
testl %r10d, %r10d
jne 0x28a67d
movb $0x1, %al
jmp 0x28a6a9
movq %rbx, %rdi
movq %r9, %rbp
movl %r9d, %esi
callq 0x289239
movq %r14, %r10
movq %rax, %r15
movq %rax, 0x8(%rsp)
testl %r10d, %r10d
je 0x28a692
movq %rbp, %r9
movb $0x1, %al
cmpb $0x0, 0xb(%rbx)
jne 0x28a6a9
movq (%r13), %rax
cmpq 0x68(%rbx), %rax
setle %al
jmp 0x28a6a9
testq %r15, %r15
je 0x28a6a1
testb $0x8, 0x34(%r15)
sete %al
jmp 0x28a6a6
movb $0x1, %al
xorl %r15d, %r15d
movq %rbp, %r9
movq 0x48(%rbx), %rdi
movq (%rdi), %r8
testq %r8, %r8
je 0x28a6c1
movb 0x14(%rbx), %cl
cmpb $0x3, %cl
jbe 0x28a714
testb %al, %al
jne 0x28a71d
xorl %esi, %esi
testl %r10d, %r10d
jne 0x28a760
testq %r15, %r15
jne 0x28a760
orb $0x2, 0x18(%rbx)
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
movl %r9d, %esi
movl $0x1, %ecx
movq %r9, %r14
callq *0x108(%rbx)
andb $-0x3, 0x18(%rbx)
testl %eax, %eax
jne 0x28a4f4
movq 0x8(%rsp), %rdi
callq 0x28ac36
movq 0x8(%rsp), %r15
movq %r14, %r9
xorl %esi, %esi
jmp 0x28a760
testb %cl, %cl
sete %cl
testb %al, %cl
je 0x28a6c1
leal -0x1(%r9), %ecx
movslq 0xbc(%rbx), %rdx
imulq %rdx, %rcx
movq %r12, %rsi
movq %r9, %r14
callq *0x18(%r8)
movq %r14, %r9
movl %eax, %esi
cmpl 0x24(%rbx), %r9d
jbe 0x28a745
movl %r9d, 0x24(%rbx)
movq 0x70(%rbx), %rdi
testq %rdi, %rdi
je 0x28a760
movl %esi, %ebp
movl %r9d, %esi
movq %r12, %rdx
callq 0x288a4a
movl %ebp, %esi
movq %r14, %r9
testq %r15, %r15
je 0x28a4f6
movl %esi, %ebp
movq 0x8(%r15), %r14
movslq 0xbc(%rbx), %rdx
movq %r14, %rdi
movq %r12, %rsi
movq %r9, %r15
callq 0x17660
movq 0x8(%rsp), %rdi
callq *0x100(%rbx)
cmpl $0x1, %r15d
jne 0x28a7a1
movups 0x18(%r14), %xmm0
movups %xmm0, 0x88(%rbx)
movq 0x8(%rsp), %rdi
callq 0x28abd0
movl %ebp, %esi
jmp 0x28a4f6