// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: backupOnePage
static int backupOnePage(
  sqlite3_backup *p,              /* Backup handle */
  Pgno iSrcPg,                    /* Source database page to backup */
  const u8 *zSrcData,             /* Source database page data */
  int bUpdate                     /* True for an update, false otherwise */
){
  Pager * const pDestPager = sqlite3BtreePager(p->pDest);
  const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
  int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
  const int nCopy = MIN(nSrcPgsz, nDestPgsz);
  const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
#ifdef SQLITE_HAS_CODEC
  /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
  ** guaranteed that the shared-mutex is held by this thread, handle
  ** p->pSrc may not actually be the owner.  */
  int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
  int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
#endif
  int rc = SQLITE_OK;
  i64 iOff;

  assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
  assert( p->bDestLocked );
  assert( !isFatalError(p->rc) );
  assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
  assert( zSrcData );

  /* Catch the case where the destination is an in-memory database and the
  ** page sizes of the source and destination differ. 
  */
  if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
    rc = SQLITE_READONLY;
  }

#ifdef SQLITE_HAS_CODEC
  /* Backup is not possible if the page size of the destination is changing
  ** and a codec is in use.
  */
  if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
    rc = SQLITE_READONLY;
  }

  /* Backup is not possible if the number of bytes of reserve space differ
  ** between source and destination.  If there is a difference, try to
  ** fix the destination to agree with the source.  If that is not possible,
  ** then the backup cannot proceed.
  */
  if( nSrcReserve!=nDestReserve ){
    u32 newPgsz = nSrcPgsz;
    rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
    if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
  }
#endif

  /* This loop runs once for each destination page spanned by the source 
  ** page. For each iteration, variable iOff is set to the byte offset
  ** of the destination page.
  */
  for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
    DbPage *pDestPg = 0;
    Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
    if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
    if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg))
     && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
    ){
      const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
      u8 *zDestData = sqlite3PagerGetData(pDestPg);
      u8 *zOut = &zDestData[iOff%nDestPgsz];

      /* Copy the data from the source page into the destination page.
      ** Then clear the Btree layer MemPage.isInit flag. Both this module
      ** and the pager code use this trick (clearing the first byte
      ** of the page 'extra' space to invalidate the Btree layers
      ** cached parse of the page). MemPage.isInit is marked 
      ** "MUST BE FIRST" for this purpose.
      */
      memcpy(zOut, zIn, nCopy);
      ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
      if( iOff==0 && bUpdate==0 ){
        sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
      }
    }
    sqlite3PagerUnref(pDestPg);
  }

  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %ecx, -0x34(%rbp)
movq %rdx, -0x60(%rbp)
movq 0x8(%rdi), %rax
movq 0x28(%rdi), %rcx
movq 0x8(%rax), %rax
movq (%rax), %rdx
movq %rdx, -0x48(%rbp)
movq 0x8(%rcx), %rcx
movslq 0x30(%rcx), %rcx
movslq 0x30(%rax), %r15
xorl %r14d, %r14d
movl %r15d, %eax
cmpl %r15d, %ecx
movq %rcx, -0x30(%rbp)
cmovll %ecx, %eax
je 0xdcbe
xorl %r14d, %r14d
movq -0x48(%rbp), %rcx
cmpb $0x0, 0x12(%rcx)
setne %r14b
shll $0x3, %r14d
testl %r14d, %r14d
jne 0xddc1
cmpl $0x0, -0x30(%rbp)
jle 0xddc1
movl %esi, %r9d
movq -0x30(%rbp), %rcx
imulq %rcx, %r9
movq %r9, %r13
subq %rcx, %r13
cltq
movq %rax, -0x58(%rbp)
movl 0x86b52(%rip), %ecx     # 0x94840
movq %rdi, -0x40(%rbp)
movq %r9, -0x68(%rbp)
movq $0x0, -0x50(%rbp)
movq %r13, %rax
cqto
idivq %r15
movq %rax, %rsi
movq %rdx, %rbx
movq 0x8(%rdi), %rax
movq 0x8(%rax), %r8
movl %ecx, %eax
xorl %edx, %edx
divl 0x30(%r8)
movl $0x0, %r14d
cmpl %esi, %eax
je 0xdd64
incl %esi
movq -0x48(%rbp), %rdi
leaq -0x50(%rbp), %rdx
xorl %ecx, %ecx
callq 0xd8e2
movq -0x50(%rbp), %r12
testl %eax, %eax
jne 0xdd4b
movq %r12, %rdi
callq 0xdfbf
testl %eax, %eax
je 0xdd73
movl %eax, %r14d
movq %r12, %rdi
callq 0xddd3
movl 0x86ae4(%rip), %ecx     # 0x94840
movq -0x40(%rbp), %rdi
movq -0x68(%rbp), %r9
testl %r14d, %r14d
jne 0xddc1
addq %r15, %r13
cmpq %r9, %r13
jl 0xdcf6
jmp 0xddc1
movq %r13, %rax
cqto
idivq -0x30(%rbp)
movq -0x60(%rbp), %rax
leaq (%rdx,%rax), %rsi
addq 0x8(%r12), %rbx
movq %rbx, %rdi
movq -0x58(%rbp), %rdx
callq 0x82f0
xorl %r14d, %r14d
movq 0x10(%r12), %rax
movb $0x0, (%rax)
cmpl $0x0, -0x34(%rbp)
jne 0xdd4e
testq %r13, %r13
jne 0xdd4e
movq -0x40(%rbp), %rax
movq 0x28(%rax), %rax
movq 0x8(%rax), %rax
movl 0x3c(%rax), %eax
bswapl %eax
movl %eax, 0x1c(%rbx)
jmp 0xdd4e
movl %r14d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq