// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3PagerMovepage
SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
  PgHdr *pPgOld;               /* The page being overwritten. */
  Pgno needSyncPgno = 0;       /* Old value of pPg->pgno, if sync is required */
  int rc;                      /* Return code */
  Pgno origPgno;               /* The original page number */

  assert( pPg->nRef>0 );
  assert( pPager->eState==PAGER_WRITER_CACHEMOD
       || pPager->eState==PAGER_WRITER_DBMOD
  );
  assert( assert_pager_state(pPager) );

  /* In order to be able to rollback, an in-memory database must journal
  ** the page we are moving from.
  */
  if( MEMDB ){
    rc = sqlite3PagerWrite(pPg);
    if( rc ) return rc;
  }

  /* If the page being moved is dirty and has not been saved by the latest
  ** savepoint, then save the current contents of the page into the 
  ** sub-journal now. This is required to handle the following scenario:
  **
  **   BEGIN;
  **     <journal page X, then modify it in memory>
  **     SAVEPOINT one;
  **       <Move page X to location Y>
  **     ROLLBACK TO one;
  **
  ** If page X were not written to the sub-journal here, it would not
  ** be possible to restore its contents when the "ROLLBACK TO one"
  ** statement were is processed.
  **
  ** subjournalPage() may need to allocate space to store pPg->pgno into
  ** one or more savepoint bitvecs. This is the reason this function
  ** may return SQLITE_NOMEM.
  */
  if( pPg->flags&PGHDR_DIRTY
   && subjRequiresPage(pPg)
   && SQLITE_OK!=(rc = subjournalPage(pPg))
  ){
    return rc;
  }

  PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n", 
      PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
  IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))

  /* If the journal needs to be sync()ed before page pPg->pgno can
  ** be written to, store pPg->pgno in local variable needSyncPgno.
  **
  ** If the isCommit flag is set, there is no need to remember that
  ** the journal needs to be sync()ed before database page pPg->pgno 
  ** can be written to. The caller has already promised not to write to it.
  */
  if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
    needSyncPgno = pPg->pgno;
    assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
            pageInJournal(pPg) || pPg->pgno>pPager->dbOrigSize );
    assert( pPg->flags&PGHDR_DIRTY );
  }

  /* If the cache contains a page with page-number pgno, remove it
  ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for 
  ** page pgno before the 'move' operation, it needs to be retained 
  ** for the page moved there.
  */
  pPg->flags &= ~PGHDR_NEED_SYNC;
  pPgOld = pager_lookup(pPager, pgno);
  assert( !pPgOld || pPgOld->nRef==1 );
  if( pPgOld ){
    pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
    if( MEMDB ){
      /* Do not discard pages from an in-memory database since we might
      ** need to rollback later.  Just move the page out of the way. */
      sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
    }else{
      sqlite3PcacheDrop(pPgOld);
    }
  }

  origPgno = pPg->pgno;
  sqlite3PcacheMove(pPg, pgno);
  sqlite3PcacheMakeDirty(pPg);

  /* For an in-memory database, make sure the original page continues
  ** to exist, in case the transaction needs to roll back.  Use pPgOld
  ** as the original page since it has already been allocated.
  */
  if( MEMDB ){
    assert( pPgOld );
    sqlite3PcacheMove(pPgOld, origPgno);
    sqlite3PagerUnref(pPgOld);
  }

  if( needSyncPgno ){
    /* If needSyncPgno is non-zero, then the journal file needs to be 
    ** sync()ed before any data is written to database file page needSyncPgno.
    ** Currently, no such page exists in the page-cache and the 
    ** "is journaled" bitvec flag has been set. This needs to be remedied by
    ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
    ** flag.
    **
    ** If the attempt to load the page into the page-cache fails, (due
    ** to a malloc() or IO failure), clear the bit in the pInJournal[]
    ** array. Otherwise, if the page is loaded and written again in
    ** this transaction, it may be written to the database file before
    ** it is synced into the journal file. This way, it may end up in
    ** the journal file twice, but that is not a problem.
    */
    PgHdr *pPgHdr;
    rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
    if( rc!=SQLITE_OK ){
      if( needSyncPgno<=pPager->dbOrigSize ){
        assert( pPager->pTmpSpace!=0 );
        sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
      }
      return rc;
    }
    pPgHdr->flags |= PGHDR_NEED_SYNC;
    sqlite3PcacheMakeDirty(pPgHdr);
    sqlite3PagerUnref(pPgHdr);
  }

  return SQLITE_OK;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x12(%rax)
je 0x2da66
movq -0x18(%rbp), %rdi
callq 0xef80
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x2da64
movl -0x30(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2dbe2
jmp 0x2da66
movq -0x18(%rbp), %rax
movzwl 0x2c(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x2daa3
movq -0x18(%rbp), %rdi
callq 0x211c0
cmpl $0x0, %eax
je 0x2daa3
movq -0x18(%rbp), %rdi
callq 0x21270
movl %eax, %ecx
movl %ecx, -0x30(%rbp)
xorl %eax, %eax
cmpl %ecx, %eax
je 0x2daa3
movl -0x30(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2dbe2
movq -0x18(%rbp), %rax
movzwl 0x2c(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x2dac3
cmpl $0x0, -0x20(%rbp)
jne 0x2dac3
movq -0x18(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movzwl 0x2c(%rax), %ecx
andl $-0x5, %ecx
movw %cx, 0x2c(%rax)
movq -0x10(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x28870
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x2db2c
movq -0x28(%rbp), %rax
movzwl 0x2c(%rax), %edx
andl $0x4, %edx
movq -0x18(%rbp), %rax
movzwl 0x2c(%rax), %ecx
orl %edx, %ecx
movw %cx, 0x2c(%rax)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x12(%rax)
je 0x2db21
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %esi
addl $0x1, %esi
callq 0x2e090
jmp 0x2db2a
movq -0x28(%rbp), %rdi
callq 0x27e60
jmp 0x2db2c
movq -0x18(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x18(%rbp), %rdi
movl -0x1c(%rbp), %esi
callq 0x2e090
movq -0x18(%rbp), %rdi
callq 0x288b0
movq -0x10(%rbp), %rax
cmpb $0x0, 0x12(%rax)
je 0x2db6a
movq -0x28(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x2e090
movq -0x28(%rbp), %rdi
callq 0xec30
cmpl $0x0, -0x2c(%rbp)
je 0x2dbdb
movq -0x10(%rbp), %rdi
movl -0x2c(%rbp), %esi
leaq -0x40(%rbp), %rdx
xorl %ecx, %ecx
callq 0xe5f0
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x2dbba
movl -0x2c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x20(%rcx), %eax
ja 0x2dbb2
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rdi
movl -0x2c(%rbp), %esi
movq -0x10(%rbp), %rax
movq 0x108(%rax), %rdx
callq 0x2e110
movl -0x30(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2dbe2
movq -0x40(%rbp), %rax
movzwl 0x2c(%rax), %ecx
orl $0x4, %ecx
movw %cx, 0x2c(%rax)
movq -0x40(%rbp), %rdi
callq 0x288b0
movq -0x40(%rbp), %rdi
callq 0xec30
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax,%rax)