// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3_backup_step
SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
  int rc;
  int destMode;       /* Destination journal mode */
  int pgszSrc = 0;    /* Source page size */
  int pgszDest = 0;   /* Destination page size */

  sqlite3_mutex_enter(p->pSrcDb->mutex);
  sqlite3BtreeEnter(p->pSrc);
  if( p->pDestDb ){
    sqlite3_mutex_enter(p->pDestDb->mutex);
  }

  rc = p->rc;
  if( !isFatalError(rc) ){
    Pager * const pSrcPager = sqlite3BtreePager(p->pSrc);     /* Source pager */
    Pager * const pDestPager = sqlite3BtreePager(p->pDest);   /* Dest pager */
    int ii;                            /* Iterator variable */
    int nSrcPage = -1;                 /* Size of source db in pages */
    int bCloseTrans = 0;               /* True if src db requires unlocking */

    /* If the source pager is currently in a write-transaction, return
    ** SQLITE_BUSY immediately.
    */
    if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
      rc = SQLITE_BUSY;
    }else{
      rc = SQLITE_OK;
    }

    /* Lock the destination database, if it is not locked already. */
    if( SQLITE_OK==rc && p->bDestLocked==0
     && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2)) 
    ){
      p->bDestLocked = 1;
      sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
    }

    /* If there is no open read-transaction on the source database, open
    ** one now. If a transaction is opened here, then it will be closed
    ** before this function exits.
    */
    if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
      rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
      bCloseTrans = 1;
    }

    /* Do not allow backup if the destination database is in WAL mode
    ** and the page sizes are different between source and destination */
    pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
    pgszDest = sqlite3BtreeGetPageSize(p->pDest);
    destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
    if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
      rc = SQLITE_READONLY;
    }
  
    /* Now that there is a read-lock on the source database, query the
    ** source pager for the number of pages in the database.
    */
    nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
    assert( nSrcPage>=0 );
    for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
      const Pgno iSrcPg = p->iNext;                 /* Source page number */
      if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
        DbPage *pSrcPg;                             /* Source page object */
        rc = sqlite3PagerAcquire(pSrcPager, iSrcPg, &pSrcPg,
                                 PAGER_GET_READONLY);
        if( rc==SQLITE_OK ){
          rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
          sqlite3PagerUnref(pSrcPg);
        }
      }
      p->iNext++;
    }
    if( rc==SQLITE_OK ){
      p->nPagecount = nSrcPage;
      p->nRemaining = nSrcPage+1-p->iNext;
      if( p->iNext>(Pgno)nSrcPage ){
        rc = SQLITE_DONE;
      }else if( !p->isAttached ){
        attachBackupObject(p);
      }
    }
  
    /* Update the schema version field in the destination database. This
    ** is to make sure that the schema-version really does change in
    ** the case where the source and destination databases have the
    ** same schema version.
    */
    if( rc==SQLITE_DONE ){
      if( nSrcPage==0 ){
        rc = sqlite3BtreeNewDb(p->pDest);
        nSrcPage = 1;
      }
      if( rc==SQLITE_OK || rc==SQLITE_DONE ){
        rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
      }
      if( rc==SQLITE_OK ){
        if( p->pDestDb ){
          sqlite3ResetAllSchemasOfConnection(p->pDestDb);
        }
        if( destMode==PAGER_JOURNALMODE_WAL ){
          rc = sqlite3BtreeSetVersion(p->pDest, 2);
        }
      }
      if( rc==SQLITE_OK ){
        int nDestTruncate;
        /* Set nDestTruncate to the final number of pages in the destination
        ** database. The complication here is that the destination page
        ** size may be different to the source page size. 
        **
        ** If the source page size is smaller than the destination page size, 
        ** round up. In this case the call to sqlite3OsTruncate() below will
        ** fix the size of the file. However it is important to call
        ** sqlite3PagerTruncateImage() here so that any pages in the 
        ** destination file that lie beyond the nDestTruncate page mark are
        ** journalled by PagerCommitPhaseOne() before they are destroyed
        ** by the file truncation.
        */
        assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
        assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
        if( pgszSrc<pgszDest ){
          int ratio = pgszDest/pgszSrc;
          nDestTruncate = (nSrcPage+ratio-1)/ratio;
          if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
            nDestTruncate--;
          }
        }else{
          nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
        }
        assert( nDestTruncate>0 );

        if( pgszSrc<pgszDest ){
          /* If the source page-size is smaller than the destination page-size,
          ** two extra things may need to happen:
          **
          **   * The destination may need to be truncated, and
          **
          **   * Data stored on the pages immediately following the 
          **     pending-byte page in the source database may need to be
          **     copied into the destination database.
          */
          const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
          sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
          Pgno iPg;
          int nDstPage;
          i64 iOff;
          i64 iEnd;

          assert( pFile );
          assert( nDestTruncate==0 
              || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
                nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
             && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
          ));

          /* This block ensures that all data required to recreate the original
          ** database has been stored in the journal for pDestPager and the
          ** journal synced to disk. So at this point we may safely modify
          ** the database file in any way, knowing that if a power failure
          ** occurs, the original database will be reconstructed from the 
          ** journal file.  */
          sqlite3PagerPagecount(pDestPager, &nDstPage);
          for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){
            if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
              DbPage *pPg;
              rc = sqlite3PagerGet(pDestPager, iPg, &pPg);
              if( rc==SQLITE_OK ){
                rc = sqlite3PagerWrite(pPg);
                sqlite3PagerUnref(pPg);
              }
            }
          }
          if( rc==SQLITE_OK ){
            rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
          }

          /* Write the extra pages and truncate the database file as required */
          iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
          for(
            iOff=PENDING_BYTE+pgszSrc; 
            rc==SQLITE_OK && iOff<iEnd; 
            iOff+=pgszSrc
          ){
            PgHdr *pSrcPg = 0;
            const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
            rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
            if( rc==SQLITE_OK ){
              u8 *zData = sqlite3PagerGetData(pSrcPg);
              rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
            }
            sqlite3PagerUnref(pSrcPg);
          }
          if( rc==SQLITE_OK ){
            rc = backupTruncateFile(pFile, iSize);
          }

          /* Sync the database file to disk. */
          if( rc==SQLITE_OK ){
            rc = sqlite3PagerSync(pDestPager);
          }
        }else{
          sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
          rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
        }
    
        /* Finish committing the transaction to the destination database. */
        if( SQLITE_OK==rc
         && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
        ){
          rc = SQLITE_DONE;
        }
      }
    }
  
    /* If bCloseTrans is true, then this function opened a read transaction
    ** on the source database. Close the read transaction here. There is
    ** no need to check the return values of the btree methods here, as
    ** "committing" a read-only transaction cannot fail.
    */
    if( bCloseTrans ){
      TESTONLY( int rc2 );
      TESTONLY( rc2  = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
      TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
      assert( rc2==SQLITE_OK );
    }
  
    if( rc==SQLITE_IOERR_NOMEM ){
      rc = SQLITE_NOMEM;
    }
    p->rc = rc;
  }
  if( p->pDestDb ){
    sqlite3_mutex_leave(p->pDestDb->mutex);
  }
  sqlite3BtreeLeave(p->pSrc);
  sqlite3_mutex_leave(p->pSrcDb->mutex);
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %r14
movq 0x28(%rdi), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movq %rax, 0x8(%rdx)
movl 0x30(%r14), %eax
cmpl $0x6, %eax
ja 0xd17a
movl $0x61, %ecx
btl %eax, %ecx
jae 0xd17a
movq (%rdx), %r9
movq 0x8(%r14), %rcx
movq 0x8(%rcx), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, (%r14)
movl %esi, -0x40(%rbp)
je 0xcccd
cmpb $0x2, 0x24(%rdx)
jne 0xcccd
movl $0x5, %eax
jmp 0xccdc
cmpl $0x0, 0x14(%r14)
je 0xcce3
cmpb $0x0, 0x10(%rdi)
je 0xcd10
xorl %eax, %eax
movb $0x1, %dl
movl %edx, -0x2c(%rbp)
jmp 0xcd2f
movq %r9, %rbx
movq %rcx, %rdi
movl $0x2, %esi
callq 0xd1b2
movq 0x8(%r14), %rcx
movq 0x28(%r14), %rdi
testl %eax, %eax
je 0xd0c8
movb $0x1, %dl
movl %edx, -0x2c(%rbp)
movl -0x40(%rbp), %esi
movq %rbx, %r9
jmp 0xcd2f
movl $0x0, -0x2c(%rbp)
xorl %esi, %esi
movq %r9, %rbx
callq 0xd1b2
movq %rbx, %r9
movl -0x40(%rbp), %esi
movq 0x8(%r14), %rcx
movq 0x28(%r14), %rdi
movq 0x8(%rdi), %rdx
movslq 0x30(%rdx), %r8
movq 0x8(%rcx), %rcx
movl 0x30(%rcx), %edi
xorl %r11d, %r11d
movq %r8, -0x68(%rbp)
movl %edi, -0x54(%rbp)
cmpl %edi, %r8d
setne %r11b
movq (%rcx), %rcx
movb 0x9(%rcx), %cl
shll $0x3, %r11d
movb %cl, -0x60(%rbp)
cmpb $0x5, %cl
cmovnel %eax, %r11d
testl %eax, %eax
cmovnel %eax, %r11d
movl 0x3c(%rdx), %edi
testl %esi, %esi
movq %r9, -0x70(%rbp)
je 0xce2c
movl 0x18(%r14), %r15d
movl $0x1, %ebx
movq %r14, -0x50(%rbp)
cmpl %edi, %r15d
ja 0xce2c
testl %r11d, %r11d
jne 0xce2c
movl 0x87aa3(%rip), %eax     # 0x94840
movq 0x28(%r14), %rcx
movq 0x8(%rcx), %rcx
xorl %edx, %edx
divl 0x30(%rcx)
xorl %r11d, %r11d
incl %eax
cmpl %eax, %r15d
je 0xce0f
movq %rdi, %r13
movq %r9, %r12
movq %r9, %rdi
movl %r15d, %esi
leaq -0x38(%rbp), %rdx
movl $0x2, %ecx
callq 0xd8e2
movl %eax, %r11d
testl %eax, %eax
jne 0xce02
movq -0x38(%rbp), %r12
movq 0x8(%r12), %rdx
movq -0x50(%rbp), %rdi
movl %r15d, %esi
xorl %ecx, %ecx
callq 0xdc62
movl %eax, %r15d
movq %r12, %rdi
movq -0x50(%rbp), %r14
movq -0x70(%rbp), %r12
callq 0xddd3
movl %r15d, %r11d
movl 0x18(%r14), %r15d
movl -0x40(%rbp), %esi
movq %r13, %rdi
movq %r12, %r9
testl %esi, %esi
sets %al
incl %r15d
movl %r15d, 0x18(%r14)
cmpl %esi, %ebx
setl %cl
orb %al, %cl
incl %ebx
testb %cl, %cl
jne 0xcd85
cmpl $0x65, %r11d
je 0xce4e
testl %r11d, %r11d
jne 0xd13e
movl %edi, 0x38(%r14)
movl %edi, %eax
subl 0x18(%r14), %eax
leal 0x1(%rax), %eax
movl %eax, 0x34(%r14)
jae 0xceba
movq %r9, %r13
testl %edi, %edi
je 0xce7f
movq %rdi, %rbx
movq 0x8(%r14), %rax
movl 0x10(%r14), %ecx
incl %ecx
movq (%rax), %rdi
movq 0x8(%rax), %rsi
movl $0x1, %edx
callq 0xde56
testl %eax, %eax
je 0xceec
movl %eax, %r11d
jmp 0xd13e
movq %r14, %rbx
movq 0x8(%r14), %rax
movq (%rax), %rcx
movq 0x8(%rax), %rdi
movq %rcx, 0x8(%rdi)
movl $0x0, 0x3c(%rdi)
callq 0x1fa9f
movl $0x1, %edi
cmpl $0x65, %eax
je 0xd0f7
movl %eax, %r11d
testl %eax, %eax
movq %rbx, %r14
je 0xce55
jmp 0xd13e
xorl %r11d, %r11d
cmpl $0x0, 0x3c(%r14)
jne 0xd13e
movq 0x28(%r14), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
movq 0x70(%rax), %rcx
movq %rcx, 0x40(%r14)
movq %r14, 0x70(%rax)
movl $0x1, 0x3c(%r14)
jmp 0xd13e
movq (%r14), %rdi
testq %rdi, %rdi
je 0xcef9
callq 0xdea0
cmpb $0x5, -0x60(%rbp)
jne 0xcf15
movq 0x8(%r14), %rdi
movl $0x2, %esi
callq 0xdf30
testl %eax, %eax
jne 0xce77
movq -0x68(%rbp), %rcx
movl -0x54(%rbp), %esi
cmpl %esi, %ecx
jge 0xd0ff
movl %esi, %eax
cltd
idivl %ecx
movq %rcx, %rsi
movl %eax, %ecx
leal (%rbx,%rcx), %eax
decl %eax
cltd
idivl %ecx
movl %eax, %r12d
movl 0x87901(%rip), %eax     # 0x94840
movq %r14, -0x50(%rbp)
movq 0x8(%r14), %rcx
movq 0x8(%rcx), %rcx
xorl %edx, %edx
divl 0x30(%rcx)
incl %eax
xorl %ecx, %ecx
cmpl %eax, %r12d
sete %cl
subl %ecx, %r12d
movslq %ebx, %r14
imulq %rsi, %r14
movq -0x48(%rbp), %rax
movq 0x48(%rax), %rcx
movq %rcx, -0x40(%rbp)
movl 0x1c(%rax), %ebx
cmpl %ebx, %r12d
movq %r14, -0x60(%rbp)
ja 0xcff0
leaq -0x38(%rbp), %r15
movl 0x878ba(%rip), %eax     # 0x94840
movq -0x50(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq 0x8(%rcx), %rcx
xorl %edx, %edx
divl 0x30(%rcx)
incl %eax
movl $0x0, %r11d
cmpl %eax, %r12d
je 0xcfda
movq -0x48(%rbp), %rdi
movl %r12d, %esi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0xd8e2
movl %eax, %r11d
testl %eax, %eax
jne 0xcfda
movq -0x38(%rbp), %r13
movq %r13, %rdi
callq 0xdfbf
movl %eax, %r14d
movq %r13, %rdi
movq -0x70(%rbp), %r13
callq 0xddd3
movl %r14d, %r11d
testl %r11d, %r11d
jne 0xcfe7
incl %r12d
cmpl %ebx, %r12d
jbe 0xcf80
testl %r11d, %r11d
movq -0x60(%rbp), %r14
jne 0xd003
movq -0x48(%rbp), %rdi
xorl %esi, %esi
movl $0x1, %edx
callq 0xe17d
movl %eax, %r11d
movl 0x87837(%rip), %ecx     # 0x94840
movl -0x54(%rbp), %eax
addl %ecx, %eax
movslq %eax, %rbx
cmpq %rbx, %r14
cmovlq %r14, %rbx
testl %r11d, %r11d
sete %al
jne 0xd098
movq -0x68(%rbp), %rdx
addl %edx, %ecx
movslq %ecx, %r15
cmpq %r15, %rbx
jle 0xd098
movq %rdx, %rcx
movq %r13, %rdi
leaq -0x38(%rbp), %r12
movq $0x0, -0x38(%rbp)
movq %r15, %rax
cqto
idivq %rcx
leal 0x1(%rax), %esi
movq %r12, %rdx
xorl %ecx, %ecx
callq 0xd8e2
movq -0x38(%rbp), %r13
testl %eax, %eax
jne 0xd072
movq 0x8(%r13), %rsi
movq -0x40(%rbp), %rdi
movq (%rdi), %rax
movq -0x68(%rbp), %rdx
movq %r15, %rcx
callq *0x18(%rax)
movq %r13, %rdi
movl %eax, %r14d
callq 0xddd3
movl %r14d, %r11d
testl %r14d, %r14d
sete %al
jne 0xd098
movq -0x68(%rbp), %rcx
addq %rcx, %r15
cmpq %rbx, %r15
movq -0x70(%rbp), %rdi
jl 0xd038
testb %al, %al
movq -0x50(%rbp), %r14
je 0xd13e
movq %r14, %r15
movq -0x40(%rbp), %rbx
movq (%rbx), %rax
leaq -0x38(%rbp), %rsi
movq %rbx, %rdi
callq *0x30(%rax)
testl %eax, %eax
je 0xd189
movl %eax, %r11d
movq %r15, %r14
jmp 0xd13e
movl $0x1, 0x14(%r14)
movq (%rcx), %rax
movq 0x8(%rcx), %rdx
movq %rax, 0x8(%rdx)
movq 0x18(%rdx), %rax
movq 0x50(%rax), %rax
movl 0x28(%rax), %eax
bswapl %eax
movl %eax, 0x10(%r14)
movl -0x40(%rbp), %esi
movq %rbx, %r9
jmp 0xccd4
movq %rbx, %r14
jmp 0xce55
movq %r14, %r15
movl %ecx, %eax
cltd
idivl %esi
imull %ebx, %eax
movq -0x48(%rbp), %rdi
movl %eax, 0x1c(%rdi)
xorl %esi, %esi
xorl %edx, %edx
callq 0xe17d
movl %eax, %r11d
testl %eax, %eax
movq %r15, %r14
jne 0xd13e
movq 0x8(%r14), %rdi
xorl %esi, %esi
callq 0xe3c0
movq %r15, %r14
testl %eax, %eax
movl $0x65, %r11d
cmovnel %eax, %r11d
cmpb $0x0, -0x2c(%rbp)
jne 0xd166
movq 0x28(%r14), %rdi
xorl %esi, %esi
movq %r14, %rbx
movl %r11d, %r14d
callq 0xe473
movq 0x28(%rbx), %rdi
xorl %esi, %esi
callq 0xe3c0
movl %r14d, %r11d
movq %rbx, %r14
cmpl $0xc0a, %r11d           # imm = 0xC0A
movl $0x7, %eax
cmovnel %r11d, %eax
movl %eax, 0x30(%r14)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x60(%rbp), %rsi
cmpq %rsi, -0x38(%rbp)
jle 0xd1a4
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x20(%rax)
testl %eax, %eax
jne 0xd0c0
movq -0x48(%rbp), %rdi
callq 0xe37c
jmp 0xd11a