// File: /workspace/github_c_makefile_star2_O0/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
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x1, -0x34(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x0, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x23e00
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x26da6
jmp 0x26fa8
movq -0x8(%rbp), %rax
movq 0x108(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x40(%rbp), %rsi
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl 0x8(%rax), %edx
addl $0x1, %edx
callq 0x28a10
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x26dfe
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x26dfe
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rsi
xorl %edx, %edx
leaq -0x34(%rbp), %rcx
callq 0x28b70
movl %eax, -0x30(%rbp)
movq $0x0, -0x40(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x26e12
cmpl $0x0, -0x34(%rbp)
jne 0x26e17
jmp 0x26fa8
movq -0x8(%rbp), %rax
movq $0x0, 0x60(%rax)
movl -0xc(%rbp), %eax
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rdi
movl -0xc(%rbp), %esi
movq -0x20(%rbp), %rdx
leaq -0x24(%rbp), %rcx
leaq -0x2c(%rbp), %r8
callq 0x278d0
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x26e5c
cmpl $0x65, -0x30(%rbp)
jne 0x26e57
movl $0x0, -0x30(%rbp)
jmp 0x26fa8
cmpl $-0x1, -0x24(%rbp)
jne 0x26e8b
movq -0x20(%rbp), %rax
movq -0x8(%rbp), %rcx
movl 0xb8(%rcx), %ecx
subq %rcx, %rax
movq -0x8(%rbp), %rcx
movl 0xbc(%rcx), %ecx
addl $0x8, %ecx
movslq %ecx, %rcx
cqto
idivq %rcx
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x26eda
cmpl $0x0, -0xc(%rbp)
jne 0x26eda
movq -0x8(%rbp), %rax
movq 0x68(%rax), %rax
movq -0x8(%rbp), %rcx
movl 0xb8(%rcx), %ecx
addq %rcx, %rax
movq -0x8(%rbp), %rcx
cmpq 0x60(%rcx), %rax
jne 0x26eda
movq -0x20(%rbp), %rax
movq -0x8(%rbp), %rcx
subq 0x60(%rcx), %rax
movq -0x8(%rbp), %rcx
movl 0xbc(%rcx), %ecx
addl $0x8, %ecx
movslq %ecx, %rcx
cqto
idivq %rcx
movl %eax, -0x24(%rbp)
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rax
movq -0x8(%rbp), %rcx
movl 0xb8(%rcx), %ecx
cmpq %rcx, %rax
jne 0x26f15
movq -0x8(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x28bb0
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x26f0b
jmp 0x26fa8
movl -0x2c(%rbp), %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x1c(%rax)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x24(%rbp), %eax
jae 0x26fa3
cmpl $0x0, -0x44(%rbp)
je 0x26f3a
movq -0x8(%rbp), %rdi
callq 0x24fc0
movl $0x0, -0x44(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x60, %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x27490
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x26f6b
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x26f93
cmpl $0x65, -0x30(%rbp)
jne 0x26f7f
movq -0x20(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x60(%rax)
jmp 0x26fa3
cmpl $0x20a, -0x30(%rbp)     # imm = 0x20A
jne 0x26f91
movl $0x0, -0x30(%rbp)
jmp 0x26fa8
jmp 0x26fa8
jmp 0x26f95
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x26f1c
jmp 0x26e29
movq -0x8(%rbp), %rax
movb 0x10(%rax), %cl
movq -0x8(%rbp), %rax
movb %cl, 0x15(%rax)
cmpl $0x0, -0x30(%rbp)
jne 0x26fec
movq -0x8(%rbp), %rax
movq 0x108(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x40(%rbp), %rsi
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl 0x8(%rax), %edx
addl $0x1, %edx
callq 0x28a10
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x27018
movq -0x8(%rbp), %rax
movzbl 0x13(%rax), %eax
cmpl $0x4, %eax
jge 0x2700c
movq -0x8(%rbp), %rax
movzbl 0x13(%rax), %eax
cmpl $0x0, %eax
jne 0x27018
movq -0x8(%rbp), %rdi
callq 0xf4c0
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x2703e
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movzbl %al, %esi
xorl %edx, %edx
callq 0x26960
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x27066
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x27066
cmpl $0x0, -0x34(%rbp)
je 0x27066
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x28cd0
movl %eax, -0x30(%rbp)
cmpl $0x0, -0xc(%rbp)
je 0x27093
cmpl $0x0, -0x48(%rbp)
je 0x27093
movl -0x48(%rbp), %edx
movq -0x8(%rbp), %rax
movq 0xd8(%rax), %rcx
movl $0x21b, %edi            # imm = 0x21B
leaq 0x872a9(%rip), %rsi     # 0xae335
movb $0x0, %al
callq 0xc5f0
movq -0x8(%rbp), %rdi
callq 0x20e40
movl -0x30(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)