// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3WalFrames
SQLITE_PRIVATE int sqlite3WalFrames(
  Wal *pWal,                      /* Wal handle to write to */
  int szPage,                     /* Database page-size in bytes */
  PgHdr *pList,                   /* List of dirty pages to write */
  Pgno nTruncate,                 /* Database size after this commit */
  int isCommit,                   /* True if this is a commit */
  int sync_flags                  /* Flags to pass to OsSync() (or 0) */
){
  int rc;                         /* Used to catch return codes */
  u32 iFrame;                     /* Next frame address */
  PgHdr *p;                       /* Iterator to run through pList with. */
  PgHdr *pLast = 0;               /* Last frame in list */
  int nExtra = 0;                 /* Number of extra copies of last page */
  int szFrame;                    /* The size of a single frame */
  i64 iOffset;                    /* Next byte to write in WAL file */
  WalWriter w;                    /* The writer */

  assert( pList );
  assert( pWal->writeLock );

  /* If this frame set completes a transaction, then nTruncate>0.  If
  ** nTruncate==0 then this frame set does not complete the transaction. */
  assert( (isCommit!=0)==(nTruncate!=0) );

#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
  { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
    WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
              pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
  }
#endif

  /* See if it is possible to write these frames into the start of the
  ** log file, instead of appending to it at pWal->hdr.mxFrame.
  */
  if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
    return rc;
  }

  /* If this is the first frame written into the log, write the WAL
  ** header to the start of the WAL file. See comments at the top of
  ** this source file for a description of the WAL header format.
  */
  iFrame = pWal->hdr.mxFrame;
  if( iFrame==0 ){
    u8 aWalHdr[WAL_HDRSIZE];      /* Buffer to assemble wal-header in */
    u32 aCksum[2];                /* Checksum for wal-header */

    sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
    sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
    sqlite3Put4byte(&aWalHdr[8], szPage);
    sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
    if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
    memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
    walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
    sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
    sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
    
    pWal->szPage = szPage;
    pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
    pWal->hdr.aFrameCksum[0] = aCksum[0];
    pWal->hdr.aFrameCksum[1] = aCksum[1];
    pWal->truncateOnCommit = 1;

    rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
    WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
    if( rc!=SQLITE_OK ){
      return rc;
    }

    /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
    ** all syncing is turned off by PRAGMA synchronous=OFF).  Otherwise
    ** an out-of-order write following a WAL restart could result in
    ** database corruption.  See the ticket:
    **
    **     http://localhost:591/sqlite/info/ff5be73dee
    */
    if( pWal->syncHeader && sync_flags ){
      rc = sqlite3OsSync(pWal->pWalFd, sync_flags & SQLITE_SYNC_MASK);
      if( rc ) return rc;
    }
  }
  assert( (int)pWal->szPage==szPage );

  /* Setup information needed to write frames into the WAL */
  w.pWal = pWal;
  w.pFd = pWal->pWalFd;
  w.iSyncPoint = 0;
  w.syncFlags = sync_flags;
  w.szPage = szPage;
  iOffset = walFrameOffset(iFrame+1, szPage);
  szFrame = szPage + WAL_FRAME_HDRSIZE;

  /* Write all frames into the log file exactly once */
  for(p=pList; p; p=p->pDirty){
    int nDbSize;   /* 0 normally.  Positive == commit flag */
    iFrame++;
    assert( iOffset==walFrameOffset(iFrame, szPage) );
    nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
    rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
    if( rc ) return rc;
    pLast = p;
    iOffset += szFrame;
  }

  /* If this is the end of a transaction, then we might need to pad
  ** the transaction and/or sync the WAL file.
  **
  ** Padding and syncing only occur if this set of frames complete a
  ** transaction and if PRAGMA synchronous=FULL.  If synchronous==NORMAL
  ** or synchonous==OFF, then no padding or syncing are needed.
  **
  ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
  ** needed and only the sync is done.  If padding is needed, then the
  ** final frame is repeated (with its commit mark) until the next sector
  ** boundary is crossed.  Only the part of the WAL prior to the last
  ** sector boundary is synced; the part of the last frame that extends
  ** past the sector boundary is written after the sync.
  */
  if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
    if( pWal->padToSectorBoundary ){
      int sectorSize = sqlite3SectorSize(pWal->pWalFd);
      w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
      while( iOffset<w.iSyncPoint ){
        rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
        if( rc ) return rc;
        iOffset += szFrame;
        nExtra++;
      }
    }else{
      rc = sqlite3OsSync(w.pFd, sync_flags & SQLITE_SYNC_MASK);
    }
  }

  /* If this frame set completes the first transaction in the WAL and
  ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
  ** journal size limit, if possible.
  */
  if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
    i64 sz = pWal->mxWalSize;
    if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
      sz = walFrameOffset(iFrame+nExtra+1, szPage);
    }
    walLimitSize(pWal, sz);
    pWal->truncateOnCommit = 0;
  }

  /* Append data to the wal-index. It is not necessary to lock the 
  ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
  ** guarantees that there are no other writers, and no data that may
  ** be in use by existing readers is being overwritten.
  */
  iFrame = pWal->hdr.mxFrame;
  for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
    iFrame++;
    rc = walIndexAppend(pWal, iFrame, p->pgno);
  }
  while( rc==SQLITE_OK && nExtra>0 ){
    iFrame++;
    nExtra--;
    rc = walIndexAppend(pWal, iFrame, pLast->pgno);
  }

  if( rc==SQLITE_OK ){
    /* Update the private copy of the header. */
    pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
    testcase( szPage<=32768 );
    testcase( szPage>=65536 );
    pWal->hdr.mxFrame = iFrame;
    if( isCommit ){
      pWal->hdr.iChange++;
      pWal->hdr.nPage = nTruncate;
    }
    /* If this is a commit, update the wal-index header too. */
    if( isCommit ){
      walIndexWriteHdr(pWal);
      pWal->iCallback = iFrame;
    }
  }

  WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl %r8d, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x229f0
movl %eax, %ecx
movl %ecx, -0x30(%rbp)
xorl %eax, %eax
cmpl %ecx, %eax
je 0x2245f
movl -0x30(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2295c
movq -0x10(%rbp), %rax
movl 0x58(%rax), %eax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x225e8
leaq -0xa0(%rbp), %rdi
movl $0x377f0682, %esi       # imm = 0x377F0682
callq 0x22060
leaq -0xa0(%rbp), %rdi
addq $0x4, %rdi
movl $0x2de218, %esi         # imm = 0x2DE218
callq 0x22060
leaq -0xa0(%rbp), %rdi
addq $0x8, %rdi
movl -0x14(%rbp), %esi
callq 0x22060
leaq -0xa0(%rbp), %rdi
addq $0xc, %rdi
movq -0x10(%rbp), %rax
movl 0x80(%rax), %esi
callq 0x22060
movq -0x10(%rbp), %rax
cmpl $0x0, 0x80(%rax)
jne 0x224e9
movq -0x10(%rbp), %rsi
addq $0x48, %rsi
addq $0x20, %rsi
movl $0x8, %edi
callq 0xc740
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
movq %rax, -0x90(%rbp)
leaq -0xa0(%rbp), %rsi
leaq -0xa8(%rbp), %r8
movl $0x1, %edi
movl $0x18, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x22b70
leaq -0xa0(%rbp), %rdi
addq $0x18, %rdi
movl -0xa8(%rbp), %esi
callq 0x22060
leaq -0xa0(%rbp), %rdi
addq $0x1c, %rdi
movl -0xa4(%rbp), %esi
callq 0x22060
movl -0x14(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x38(%rax)
movq -0x10(%rbp), %rax
movb $0x0, 0x55(%rax)
movl -0xa8(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x60(%rax)
movl -0xa4(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x64(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x43(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0xa0(%rbp), %rsi
movl $0x20, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0xf430
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x225aa
movl -0x30(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2295c
movq -0x10(%rbp), %rax
movzbl 0x44(%rax), %eax
cmpl $0x0, %eax
je 0x225e6
cmpl $0x0, -0x2c(%rbp)
je 0x225e6
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x2c(%rbp), %esi
andl $0x13, %esi
callq 0x22ce0
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x225e4
movl -0x30(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2295c
jmp 0x225e6
jmp 0x225e8
movq -0x10(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x70(%rbp)
movq $0x0, -0x68(%rbp)
movl -0x2c(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
subl $0x1, %eax
movl %eax, %eax
movl -0x14(%rbp), %ecx
addl $0x18, %ecx
movslq %ecx, %rcx
imulq %rcx, %rax
addq $0x20, %rax
movq %rax, -0x58(%rbp)
movl -0x14(%rbp), %eax
addl $0x18, %eax
movl %eax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x226d7
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x28(%rbp)
je 0x22671
movq -0x40(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jne 0x22671
movl -0x24(%rbp), %eax
movl %eax, -0xbc(%rbp)
jmp 0x2267b
xorl %eax, %eax
movl %eax, -0xbc(%rbp)
jmp 0x2267b
movl -0xbc(%rbp), %eax
movl %eax, -0xac(%rbp)
movq -0x40(%rbp), %rsi
movl -0xac(%rbp), %edx
movq -0x58(%rbp), %rcx
leaq -0x78(%rbp), %rdi
callq 0x22d10
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x226b2
movl -0x30(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2295c
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movslq -0x50(%rbp), %rax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x22641
cmpl $0x0, -0x28(%rbp)
je 0x2279d
movl -0x2c(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x2279d
movq -0x10(%rbp), %rax
cmpb $0x0, 0x45(%rax)
je 0x22789
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x21100
movl %eax, -0xb0(%rbp)
movq -0x58(%rbp), %rax
movslq -0xb0(%rbp), %rcx
addq %rcx, %rax
subq $0x1, %rax
movslq -0xb0(%rbp), %rcx
cqto
idivq %rcx
movslq -0xb0(%rbp), %rcx
imulq %rcx, %rax
movq %rax, -0x68(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x68(%rbp), %rax
jge 0x22787
movq -0x48(%rbp), %rsi
movl -0x24(%rbp), %edx
movq -0x58(%rbp), %rcx
leaq -0x78(%rbp), %rdi
callq 0x22d10
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x22770
movl -0x30(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2295c
movslq -0x50(%rbp), %rax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x2273e
jmp 0x2279b
movq -0x70(%rbp), %rdi
movl -0x2c(%rbp), %esi
andl $0x13, %esi
callq 0x22ce0
movl %eax, -0x30(%rbp)
jmp 0x2279d
cmpl $0x0, -0x28(%rbp)
je 0x22839
movq -0x10(%rbp), %rax
movzbl 0x43(%rax), %eax
cmpl $0x0, %eax
je 0x22839
movq -0x10(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jl 0x22839
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0xb8(%rbp)
movl -0x34(%rbp), %eax
addl -0x4c(%rbp), %eax
addl $0x1, %eax
subl $0x1, %eax
movl %eax, %eax
movl -0x14(%rbp), %ecx
addl $0x18, %ecx
movslq %ecx, %rcx
imulq %rcx, %rax
addq $0x20, %rax
movq -0x10(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jle 0x22821
movl -0x34(%rbp), %eax
addl -0x4c(%rbp), %eax
addl $0x1, %eax
subl $0x1, %eax
movl %eax, %eax
movl -0x14(%rbp), %ecx
addl $0x18, %ecx
movslq %ecx, %rcx
imulq %rcx, %rax
addq $0x20, %rax
movq %rax, -0xb8(%rbp)
movq -0x10(%rbp), %rdi
movq -0xb8(%rbp), %rsi
callq 0x22db0
movq -0x10(%rbp), %rax
movb $0x0, 0x43(%rax)
movq -0x10(%rbp), %rax
movl 0x58(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x40(%rbp)
movb %al, -0xbd(%rbp)
je 0x22867
cmpl $0x0, -0x30(%rbp)
sete %al
movb %al, -0xbd(%rbp)
movb -0xbd(%rbp), %al
testb $0x1, %al
jne 0x22873
jmp 0x228a0
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rdi
movl -0x34(%rbp), %esi
movq -0x40(%rbp), %rax
movl 0x28(%rax), %edx
callq 0x22e30
movl %eax, -0x30(%rbp)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x2284b
jmp 0x228a2
xorl %eax, %eax
cmpl $0x0, -0x30(%rbp)
movb %al, -0xbe(%rbp)
jne 0x228bd
cmpl $0x0, -0x4c(%rbp)
setg %al
movb %al, -0xbe(%rbp)
movb -0xbe(%rbp), %al
testb $0x1, %al
jne 0x228c9
jmp 0x228f3
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x4c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x4c(%rbp)
movq -0x10(%rbp), %rdi
movl -0x34(%rbp), %esi
movq -0x48(%rbp), %rax
movl 0x28(%rax), %edx
callq 0x22e30
movl %eax, -0x30(%rbp)
jmp 0x228a2
cmpl $0x0, -0x30(%rbp)
jne 0x22956
movl -0x14(%rbp), %eax
andl $0xff00, %eax           # imm = 0xFF00
movl -0x14(%rbp), %ecx
sarl $0x10, %ecx
orl %ecx, %eax
movw %ax, %cx
movq -0x10(%rbp), %rax
movw %cx, 0x56(%rax)
movl -0x34(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x58(%rax)
cmpl $0x0, -0x28(%rbp)
je 0x2293b
movq -0x10(%rbp), %rax
movl 0x50(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x50(%rax)
movl -0x24(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x5c(%rax)
cmpl $0x0, -0x28(%rbp)
je 0x22954
movq -0x10(%rbp), %rdi
callq 0x22f60
movl -0x34(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x18(%rax)
jmp 0x22956
movl -0x30(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax,%rax)