// File: /workspace/github_c_makefile_star2_O0/genometools[P]genometools/src/external/sqlite-3.33.0/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 *zSuper = 0;        /* Name of super-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 */
  u32 savedPageSize = pPager->pageSize;

  /* 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 super-journal name from the journal, if it is present.
  ** If a super-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.
  */
  zSuper = pPager->pTmpSpace;
  rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
  if( rc==SQLITE_OK && zSuper[0] ){
    rc = sqlite3OsAccess(pVfs, zSuper, SQLITE_ACCESS_EXISTS, &res);
  }
  zSuper = 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:
  if( rc==SQLITE_OK ){
    rc = sqlite3PagerSetPagesize(pPager, &savedPageSize, -1);
  }
  /* 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
  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 ){
    zSuper = pPager->pTmpSpace;
    rc = readSuperJournal(pPager->jfd, zSuper, pPager->pVfs->mxPathname+1);
    testcase( rc!=SQLITE_OK );
  }
  if( rc==SQLITE_OK
   && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
  ){
    rc = sqlite3PagerSync(pPager, 0);
  }
  if( rc==SQLITE_OK ){
    rc = pager_end_transaction(pPager, zSuper[0]!='\0', 0);
    testcase( rc!=SQLITE_OK );
  }
  if( rc==SQLITE_OK && zSuper[0] && res ){
    /* If there was a super-journal and this routine will return success,
    ** see if it is possible to delete the super-journal.
    */
    rc = pager_delsuper(pPager, zSuper);
    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
movl 0xbc(%rax), %eax
movl %eax, -0x4c(%rbp)
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x3dfbd0
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x3e1f63
jmp 0x3e2165
movq -0x8(%rbp), %rax
movq 0x110(%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 0x3e3560
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x3e1fbb
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x3e1fbb
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rsi
xorl %edx, %edx
leaq -0x34(%rbp), %rcx
callq 0x3e36f0
movl %eax, -0x30(%rbp)
movq $0x0, -0x40(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x3e1fcf
cmpl $0x0, -0x34(%rbp)
jne 0x3e1fd4
jmp 0x3e2165
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 0x3e2ae0
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x3e2019
cmpl $0x65, -0x30(%rbp)
jne 0x3e2014
movl $0x0, -0x30(%rbp)
jmp 0x3e2165
cmpl $-0x1, -0x24(%rbp)
jne 0x3e2048
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 0x3e2097
cmpl $0x0, -0xc(%rbp)
jne 0x3e2097
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 0x3e2097
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 0x3e20d2
movq -0x8(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x3e3730
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x3e20c8
jmp 0x3e2165
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 0x3e2160
cmpl $0x0, -0x44(%rbp)
je 0x3e20f7
movq -0x8(%rbp), %rdi
callq 0x3e3850
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 0x3e26f0
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x3e2128
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x3e2150
cmpl $0x65, -0x30(%rbp)
jne 0x3e213c
movq -0x20(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x60(%rax)
jmp 0x3e2160
cmpl $0x20a, -0x30(%rbp)     # imm = 0x20A
jne 0x3e214e
movl $0x0, -0x30(%rbp)
jmp 0x3e2165
jmp 0x3e2165
jmp 0x3e2152
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x3e20d9
jmp 0x3e1fe6
cmpl $0x0, -0x30(%rbp)
jne 0x3e2180
movq -0x8(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x3db760
movl %eax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movb 0x10(%rax), %cl
movq -0x8(%rbp), %rax
movb %cl, 0x16(%rax)
cmpl $0x0, -0x30(%rbp)
jne 0x3e21c4
movq -0x8(%rbp), %rax
movq 0x110(%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 0x3e3560
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x3e21f4
movq -0x8(%rbp), %rax
movzbl 0x14(%rax), %eax
cmpl $0x4, %eax
jge 0x3e21e4
movq -0x8(%rbp), %rax
movzbl 0x14(%rax), %eax
cmpl $0x0, %eax
jne 0x3e21f4
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x3c69c0
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x3e221a
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 0x3e18b0
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
jne 0x3e2242
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x3e2242
cmpl $0x0, -0x34(%rbp)
je 0x3e2242
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x3e38a0
movl %eax, -0x30(%rbp)
cmpl $0x0, -0xc(%rbp)
je 0x3e226f
cmpl $0x0, -0x48(%rbp)
je 0x3e226f
movl -0x48(%rbp), %edx
movq -0x8(%rbp), %rax
movq 0xd8(%rax), %rcx
movl $0x21b, %edi            # imm = 0x21B
leaq 0x1c8f2f(%rip), %rsi    # 0x5ab197
movb $0x0, %al
callq 0x3c3970
movq -0x8(%rbp), %rdi
callq 0x3dbc10
movl -0x30(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)