// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: pager_playback
static int pager_playback(Pager *pPager, int isHot){
  sqlite3_vfs *pVfs = pPager->pVfs;
  i64 szJ;                 /* Size of the journal file in bytes */
  u32 nRec;                /* Number of Records in the journal */
  u32 u;                   /* Unsigned loop counter */
  Pgno mxPg = 0;           /* Size of the original file in pages */
  int rc;                  /* Result code of a subroutine */
  int res = 1;             /* Value returned by sqlite3OsAccess() */
  char *zMaster = 0;       /* Name of master journal file if any */
  int needPagerReset;      /* True to reset page prior to first page rollback */
  int nPlayback = 0;       /* Total number of pages restored from journal */

  /* Figure out how many records are in the journal.  Abort early if
  ** the journal is empty.
  */
  assert( isOpen(pPager->jfd) );
  rc = sqlite3OsFileSize(pPager->jfd, &szJ);
  if( rc!=SQLITE_OK ){
    goto end_playback;
  }

  /* Read the master journal name from the journal, if it is present.
  ** If a master journal file name is specified, but the file is not
  ** present on disk, then the journal is not hot and does not need to be
  ** played back.
  **
  ** TODO: Technically the following is an error because it assumes that
  ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
  ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
  **  mxPathname is 512, which is the same as the minimum allowable value
  ** for pageSize.
  */
  zMaster = pPager->pTmpSpace;
  rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
  if( rc==SQLITE_OK && zMaster[0] ){
    rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
  }
  zMaster = 0;
  if( rc!=SQLITE_OK || !res ){
    goto end_playback;
  }
  pPager->journalOff = 0;
  needPagerReset = isHot;

  /* This loop terminates either when a readJournalHdr() or 
  ** pager_playback_one_page() call returns SQLITE_DONE or an IO error 
  ** occurs. 
  */
  while( 1 ){
    /* Read the next journal header from the journal file.  If there are
    ** not enough bytes left in the journal file for a complete header, or
    ** it is corrupted, then a process must have failed while writing it.
    ** This indicates nothing more needs to be rolled back.
    */
    rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
    if( rc!=SQLITE_OK ){ 
      if( rc==SQLITE_DONE ){
        rc = SQLITE_OK;
      }
      goto end_playback;
    }

    /* If nRec is 0xffffffff, then this journal was created by a process
    ** working in no-sync mode. This means that the rest of the journal
    ** file consists of pages, there are no more journal headers. Compute
    ** the value of nRec based on this assumption.
    */
    if( nRec==0xffffffff ){
      assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
      nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
    }

    /* If nRec is 0 and this rollback is of a transaction created by this
    ** process and if this is the final header in the journal, then it means
    ** that this part of the journal was being filled but has not yet been
    ** synced to disk.  Compute the number of pages based on the remaining
    ** size of the file.
    **
    ** The third term of the test was added to fix ticket #2565.
    ** When rolling back a hot journal, nRec==0 always means that the next
    ** chunk of the journal contains zero pages to be rolled back.  But
    ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
    ** the journal, it means that the journal might contain additional
    ** pages that need to be rolled back and that the number of pages 
    ** should be computed based on the journal file size.
    */
    if( nRec==0 && !isHot &&
        pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
      nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
    }

    /* If this is the first header read from the journal, truncate the
    ** database file back to its original size.
    */
    if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
      rc = pager_truncate(pPager, mxPg);
      if( rc!=SQLITE_OK ){
        goto end_playback;
      }
      pPager->dbSize = mxPg;
    }

    /* Copy original pages out of the journal and back into the 
    ** database file and/or page cache.
    */
    for(u=0; u<nRec; u++){
      if( needPagerReset ){
        pager_reset(pPager);
        needPagerReset = 0;
      }
      rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
      if( rc==SQLITE_OK ){
        nPlayback++;
      }else{
        if( rc==SQLITE_DONE ){
          pPager->journalOff = szJ;
          break;
        }else if( rc==SQLITE_IOERR_SHORT_READ ){
          /* If the journal has been truncated, simply stop reading and
          ** processing the journal. This might happen if the journal was
          ** not completely written and synced prior to a crash.  In that
          ** case, the database should have never been written in the
          ** first place so it is OK to simply abandon the rollback. */
          rc = SQLITE_OK;
          goto end_playback;
        }else{
          /* If we are unable to rollback, quit and return the error
          ** code.  This will cause the pager to enter the error state
          ** so that no further harm will be done.  Perhaps the next
          ** process to come along will be able to rollback the database.
          */
          goto end_playback;
        }
      }
    }
  }
  /*NOTREACHED*/
  assert( 0 );

end_playback:
  /* Following a rollback, the database file should be back in its original
  ** state prior to the start of the transaction, so invoke the
  ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
  ** assertion that the transaction counter was modified.
  */
#ifdef SQLITE_DEBUG
  if( pPager->fd->pMethods ){
    sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
  }
#endif

  /* If this playback is happening automatically as a result of an IO or 
  ** malloc error that occurred after the change-counter was updated but 
  ** before the transaction was committed, then the change-counter 
  ** modification may just have been reverted. If this happens in exclusive 
  ** mode, then subsequent transactions performed by the connection will not
  ** update the change-counter at all. This may lead to cache inconsistency
  ** problems for other processes at some point in the future. So, just
  ** in case this has happened, clear the changeCountDone flag now.
  */
  pPager->changeCountDone = pPager->tempFile;

  if( rc==SQLITE_OK ){
    zMaster = pPager->pTmpSpace;
    rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
    testcase( rc!=SQLITE_OK );
  }
  if( rc==SQLITE_OK
   && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
  ){
    rc = sqlite3PagerSync(pPager);
  }
  if( rc==SQLITE_OK ){
    rc = pager_end_transaction(pPager, zMaster[0]!='\0', 0);
    testcase( rc!=SQLITE_OK );
  }
  if( rc==SQLITE_OK && zMaster[0] && res ){
    /* If there was a master journal and this routine will return success,
    ** see if it is possible to delete the master journal.
    */
    rc = pager_delmaster(pPager, zMaster);
    testcase( rc!=SQLITE_OK );
  }
  if( isHot && nPlayback ){
    sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK, "recovered %d pages from %s",
                nPlayback, pPager->zJournal);
  }

  /* The Pager.sectorSize variable may have been updated while rolling
  ** back a journal created by a process with a different sector size
  ** value. Reset it to the correct value for this process.
  */
  setSectorSize(pPager);
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdi, %r13
movq (%rdi), %r14
andl $0x0, -0x6c(%rbp)
movl %esi, %r15d
movl $0x1, -0x54(%rbp)
movq 0x50(%rdi), %rdi
movq (%rdi), %rax
leaq -0x78(%rbp), %rsi
callq *0x30(%rax)
testl %eax, %eax
je 0x1be00
movl %eax, %r12d
xorl %r14d, %r14d
movb 0x10(%r13), %al
movb %al, 0x15(%r13)
jmp 0x1c073
movl %r15d, -0x2c(%rbp)
movq 0x108(%r13), %r15
movq (%r13), %rax
movq 0x50(%r13), %rdi
movl 0x8(%rax), %edx
incl %edx
movq %r15, %rsi
callq 0x1cebf
movl %eax, %r12d
testl %eax, %eax
jne 0x1be45
cmpb $0x0, (%r15)
je 0x1be42
leaq -0x54(%rbp), %rcx
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq *0x38(%r14)
movl %eax, %r12d
jmp 0x1be45
xorl %r12d, %r12d
xorl %r14d, %r14d
testl %r12d, %r12d
movq %r13, -0x38(%rbp)
jne 0x1bff4
cmpl $0x0, -0x54(%rbp)
movl -0x2c(%rbp), %r15d
je 0x1c004
leaq 0x60(%r13), %rax
movq %rax, -0x50(%rbp)
andq $0x0, 0x60(%r13)
movq $0x0, -0x40(%rbp)
leaq -0x44(%rbp), %rcx
leaq -0x6c(%rbp), %r8
movq %r13, %r14
movl %r15d, %r13d
movq -0x78(%rbp), %rdx
movq %r14, %rdi
movl %r15d, %esi
movq %r8, %rbx
callq 0x1c634
testl %eax, %eax
jne 0x1c0b4
movl -0x44(%rbp), %r15d
movl 0xb8(%r14), %ecx
cmpl $-0x1, %r15d
jne 0x1bed3
movq -0x78(%rbp), %rax
subq %rcx, %rax
movq -0x38(%rbp), %rdx
movslq 0xbc(%rdx), %rsi
addq $0x8, %rsi
cqto
idivq %rsi
movq %rax, %r15
movl %r15d, -0x44(%rbp)
movl %r15d, %eax
orl -0x2c(%rbp), %eax
movq -0x50(%rbp), %rax
movq (%rax), %rsi
movq -0x40(%rbp), %r8
jne 0x1bf14
movq -0x38(%rbp), %rdx
movq 0x68(%rdx), %rax
addq %rcx, %rax
cmpq %rsi, %rax
jne 0x1bf14
movq -0x78(%rbp), %rax
subq %rsi, %rax
movslq 0xbc(%rdx), %rdi
addq $0x8, %rdi
cqto
idivq %rdi
movq %rax, %r15
movl %r15d, -0x44(%rbp)
cmpq %rcx, %rsi
jne 0x1bf3d
movl -0x6c(%rbp), %r14d
movq -0x38(%rbp), %rdi
movl %r14d, %esi
callq 0x1cfc2
testl %eax, %eax
jne 0x1c168
movq -0x38(%rbp), %rax
movl %r14d, 0x1c(%rax)
movq -0x40(%rbp), %r8
movq %r8, -0x40(%rbp)
leal (%r8,%r15), %eax
movl %eax, -0x68(%rbp)
negl %r15d
xorl %r14d, %r14d
movq %rbx, %r8
cmpl %r14d, %r15d
je 0x1bfbb
testl %r13d, %r13d
movq -0x38(%rbp), %r13
je 0x1bf67
movq %r13, %rdi
callq 0x1a6c5
movq %r13, %rdi
movq -0x50(%rbp), %rsi
xorl %edx, %edx
pushq $0x1
popq %rcx
xorl %r8d, %r8d
callq 0x1c326
testl %eax, %eax
jne 0x1bf87
decl %r14d
xorl %r13d, %r13d
jmp 0x1bf4e
movl %eax, %r12d
cmpl $0x65, %eax
jne 0x1bfd3
movq -0x78(%rbp), %rax
movq -0x50(%rbp), %rcx
movq %rax, (%rcx)
movq -0x40(%rbp), %rax
subl %r14d, %eax
movq %rax, -0x40(%rbp)
movq %r13, %r14
xorl %r13d, %r13d
movl -0x2c(%rbp), %r15d
leaq -0x44(%rbp), %rcx
movq %rbx, %r8
jmp 0x1be86
movl -0x68(%rbp), %eax
movq %rax, -0x40(%rbp)
movl -0x2c(%rbp), %r15d
leaq -0x44(%rbp), %rcx
movq -0x38(%rbp), %r14
jmp 0x1be86
cmpl $0x20a, %r12d           # imm = 0x20A
movl -0x2c(%rbp), %r15d
jne 0x1bffa
movq -0x40(%rbp), %rax
subl %r14d, %eax
movq %rax, %r14
movb 0x10(%r13), %al
movb %al, 0x15(%r13)
jmp 0x1c011
movl -0x2c(%rbp), %r15d
jmp 0x1c004
movq -0x40(%rbp), %rax
subl %r14d, %eax
movq %rax, %r14
movb 0x10(%r13), %al
movb %al, 0x15(%r13)
testl %r12d, %r12d
jne 0x1c073
movq %r13, %rbx
movq 0x108(%r13), %r13
movq (%rbx), %rax
movq 0x50(%rbx), %rdi
movl 0x8(%rax), %edx
incl %edx
movq %r13, %rsi
callq 0x1cebf
testl %eax, %eax
je 0x1c03b
movl %eax, %r12d
movq %rbx, %r13
jmp 0x1c073
movb 0x13(%rbx), %al
addb $-0x4, %al
cmpb $-0x4, %al
ja 0x1c050
movq %rbx, %rdi
callq 0xd5a9
testl %eax, %eax
jne 0x1c033
xorl %esi, %esi
cmpb $0x0, (%r13)
setne %sil
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %edx, %edx
callq 0x1b71b
testl %eax, %eax
je 0x1c0cd
movl %eax, %r12d
movq -0x38(%rbp), %r13
testl %r15d, %r15d
je 0x1c09a
testl %r14d, %r14d
je 0x1c09a
movq 0xd8(%r13), %rcx
leaq 0x4f7d3(%rip), %rsi     # 0x6b85e
movl $0x21b, %edi            # imm = 0x21B
movl %r14d, %edx
xorl %eax, %eax
callq 0xaf99
movq %r13, %rdi
callq 0x1832e
movl %r12d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %r12d
cmpl $0x65, %eax
jne 0x1c15b
movq -0x38(%rbp), %r13
movq -0x40(%rbp), %r14
jmp 0x1bfea
cmpb $0x0, (%r13)
je 0x1c06f
cmpl $0x0, -0x54(%rbp)
je 0x1c06f
movq -0x38(%rbp), %rax
movq (%rax), %rbx
movl 0x4(%rbx), %edi
addl %edi, %edi
callq 0xbeb2
pushq $0x7
popq %r12
testq %rax, %rax
movq %rax, -0x50(%rbp)
je 0x1c126
movq %r14, -0x40(%rbp)
movq %rbx, %rdi
movslq 0x4(%rbx), %rcx
movq %rcx, -0x68(%rbp)
movq %r13, %rsi
movq %rax, %r14
movq %rax, %rdx
movl $0x4001, %ecx           # imm = 0x4001
xorl %r8d, %r8d
callq *0x28(%rdi)
testl %eax, %eax
je 0x1c17c
movl %eax, %r12d
movq -0x40(%rbp), %r14
xorl %edi, %edi
movq -0x38(%rbp), %r13
callq 0x9c7b
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x1c073
movq (%rdi), %rax
testq %rax, %rax
je 0x1c151
callq *0x8(%rax)
movq -0x50(%rbp), %rdi
andq $0x0, (%rdi)
callq 0x9c7b
jmp 0x1c073
movq -0x38(%rbp), %r13
movq -0x40(%rbp), %r14
jmp 0x1bdf3
movl %eax, %r12d
movl -0x2c(%rbp), %r15d
movq -0x40(%rbp), %r14
movq -0x38(%rbp), %r13
jmp 0x1bdf3
movq %rbx, -0xa0(%rbp)
movq (%r14), %rax
leaq -0x80(%rbp), %rsi
movq %r14, %rdi
callq *0x30(%rax)
testl %eax, %eax
je 0x1c19f
xorl %edi, %edi
movl %eax, %r12d
movq -0x40(%rbp), %r14
jmp 0x1c128
movq -0xa0(%rbp), %r14
movl 0x8(%r14), %ecx
movl -0x80(%rbp), %eax
movq %rcx, -0x88(%rbp)
leal (%rcx,%rax), %edi
addl $0x2, %edi
callq 0x9c40
testq %rax, %rax
je 0x1c1f7
movq -0x80(%rbp), %r15
movq -0x50(%rbp), %rdi
movq (%rdi), %r8
movq %rax, -0x60(%rbp)
movq %rax, %rsi
movl %r15d, %edx
xorl %ecx, %ecx
callq *0x10(%r8)
testl %eax, %eax
je 0x1c1fb
movl %eax, %r12d
movl -0x2c(%rbp), %r15d
movq -0x40(%rbp), %r14
movq -0x60(%rbp), %rdi
jmp 0x1c128
xorl %edi, %edi
jmp 0x1c199
movq -0x68(%rbp), %rbx
addq -0x50(%rbp), %rbx
movq -0x88(%rbp), %rax
incl %eax
movq %rax, -0x88(%rbp)
movq -0x60(%rbp), %rcx
leaq (%rcx,%r15), %rax
incq %rax
movq %rax, -0x90(%rbp)
movq -0x80(%rbp), %rax
movb $0x0, (%rcx,%rax)
movq %rcx, %r15
movq %rbx, -0x68(%rbp)
movq %r15, %rax
subq %rcx, %rax
cmpq -0x80(%rbp), %rax
jge 0x1c2f8
movq %r14, %rdi
movq %r15, %rsi
xorl %edx, %edx
leaq -0x94(%rbp), %rcx
callq *0x38(%r14)
movl %eax, %r12d
testl %eax, %eax
jne 0x1c1e6
cmpl $0x0, -0x94(%rbp)
je 0x1c2df
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
movl $0x801, %ecx            # imm = 0x801
xorl %r8d, %r8d
callq *0x28(%r14)
testl %eax, %eax
jne 0x1c1e3
movq %rbx, %rdi
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
callq 0x1cebf
movq (%rbx), %rcx
testq %rcx, %rcx
je 0x1c2b4
movq %rbx, %rdi
movl %eax, %ebx
callq *0x8(%rcx)
movl %ebx, %eax
movq -0x68(%rbp), %rbx
andq $0x0, (%rbx)
testl %eax, %eax
jne 0x1c1e3
movq -0x90(%rbp), %rax
cmpb $0x0, (%rax)
je 0x1c2df
movq -0x90(%rbp), %rdi
movq %r13, %rsi
callq 0x8510
testl %eax, %eax
je 0x1c1e6
movq %r15, %rdi
callq 0x8f8e
movl %eax, %eax
addq %rax, %r15
incq %r15
movq -0x60(%rbp), %rcx
jmp 0x1c234
movq -0x50(%rbp), %rax
movq (%rax), %rax
testq %rax, %rax
je 0x1c312
movq -0x50(%rbp), %r14
movq %r14, %rdi
callq *0x8(%rax)
andq $0x0, (%r14)
movq -0xa0(%rbp), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq *0x30(%rdi)
jmp 0x1c1e3