// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: walCheckpoint
static int walCheckpoint(
  Wal *pWal,                      /* Wal connection */
  int eMode,                      /* One of PASSIVE, FULL or RESTART */
  int (*xBusyCall)(void*),        /* Function to call when busy */
  void *pBusyArg,                 /* Context argument for xBusyHandler */
  int sync_flags,                 /* Flags for OsSync() (or 0) */
  u8 *zBuf                        /* Temporary buffer to use */
){
  int rc;                         /* Return code */
  int szPage;                     /* Database page-size */
  WalIterator *pIter = 0;         /* Wal iterator context */
  u32 iDbpage = 0;                /* Next database page to write */
  u32 iFrame = 0;                 /* Wal frame containing data for iDbpage */
  u32 mxSafeFrame;                /* Max frame that can be backfilled */
  u32 mxPage;                     /* Max database page to write */
  int i;                          /* Loop counter */
  volatile WalCkptInfo *pInfo;    /* The checkpoint status information */
  int (*xBusy)(void*) = 0;        /* Function to call when waiting for locks */

  szPage = walPagesize(pWal);
  testcase( szPage<=32768 );
  testcase( szPage>=65536 );
  pInfo = walCkptInfo(pWal);
  if( pInfo->nBackfill>=pWal->hdr.mxFrame ) return SQLITE_OK;

  /* Allocate the iterator */
  rc = walIteratorInit(pWal, &pIter);
  if( rc!=SQLITE_OK ){
    return rc;
  }
  assert( pIter );

  if( eMode!=SQLITE_CHECKPOINT_PASSIVE ) xBusy = xBusyCall;

  /* Compute in mxSafeFrame the index of the last frame of the WAL that is
  ** safe to write into the database.  Frames beyond mxSafeFrame might
  ** overwrite database pages that are in use by active readers and thus
  ** cannot be backfilled from the WAL.
  */
  mxSafeFrame = pWal->hdr.mxFrame;
  mxPage = pWal->hdr.nPage;
  for(i=1; i<WAL_NREADER; i++){
    u32 y = pInfo->aReadMark[i];
    if( mxSafeFrame>y ){
      assert( y<=pWal->hdr.mxFrame );
      rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
      if( rc==SQLITE_OK ){
        pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED);
        walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
      }else if( rc==SQLITE_BUSY ){
        mxSafeFrame = y;
        xBusy = 0;
      }else{
        goto walcheckpoint_out;
      }
    }
  }

  if( pInfo->nBackfill<mxSafeFrame
   && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0), 1))==SQLITE_OK
  ){
    i64 nSize;                    /* Current size of database file */
    u32 nBackfill = pInfo->nBackfill;

    /* Sync the WAL to disk */
    if( sync_flags ){
      rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
    }

    /* If the database may grow as a result of this checkpoint, hint
    ** about the eventual size of the db file to the VFS layer.
    */
    if( rc==SQLITE_OK ){
      i64 nReq = ((i64)mxPage * szPage);
      rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
      if( rc==SQLITE_OK && nSize<nReq ){
        sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
      }
    }


    /* Iterate through the contents of the WAL, copying data to the db file. */
    while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
      i64 iOffset;
      assert( walFramePgno(pWal, iFrame)==iDbpage );
      if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ) continue;
      iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
      /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
      rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
      if( rc!=SQLITE_OK ) break;
      iOffset = (iDbpage-1)*(i64)szPage;
      testcase( IS_BIG_INT(iOffset) );
      rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
      if( rc!=SQLITE_OK ) break;
    }

    /* If work was actually accomplished... */
    if( rc==SQLITE_OK ){
      if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
        i64 szDb = pWal->hdr.nPage*(i64)szPage;
        testcase( IS_BIG_INT(szDb) );
        rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
        if( rc==SQLITE_OK && sync_flags ){
          rc = sqlite3OsSync(pWal->pDbFd, sync_flags);
        }
      }
      if( rc==SQLITE_OK ){
        pInfo->nBackfill = mxSafeFrame;
      }
    }

    /* Release the reader lock held while backfilling */
    walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
  }

  if( rc==SQLITE_BUSY ){
    /* Reset the return code so as not to report a checkpoint failure
    ** just because there are active readers.  */
    rc = SQLITE_OK;
  }

  /* If this is an SQLITE_CHECKPOINT_RESTART operation, and the entire wal
  ** file has been copied into the database file, then block until all
  ** readers have finished using the wal file. This ensures that the next
  ** process to write to the database restarts the wal file.
  */
  if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
    assert( pWal->writeLock );
    if( pInfo->nBackfill<pWal->hdr.mxFrame ){
      rc = SQLITE_BUSY;
    }else if( eMode==SQLITE_CHECKPOINT_RESTART ){
      assert( mxSafeFrame==pWal->hdr.mxFrame );
      rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
      if( rc==SQLITE_OK ){
        walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
      }
    }
  }

 walcheckpoint_out:
  walIteratorFree(pIter);
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movq %r9, -0x38(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x10(%rbp), %rdi
callq 0x25ab0
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
callq 0x23000
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movl (%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x58(%rcx), %eax
jb 0x25b54
movl $0x0, -0x4(%rbp)
jmp 0x25f1b
movq -0x10(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x25f70
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0x25b75
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x25f1b
cmpl $0x0, -0x14(%rbp)
je 0x25b83
movq -0x20(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rax
movl 0x58(%rax), %eax
movl %eax, -0x54(%rbp)
movq -0x10(%rbp), %rax
movl 0x5c(%rax), %eax
movl %eax, -0x58(%rbp)
movl $0x1, -0x5c(%rbp)
cmpl $0x5, -0x5c(%rbp)
jge 0x25c5e
movq -0x68(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movl 0x4(%rax,%rcx,4), %eax
movl %eax, -0x74(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x74(%rbp), %eax
jbe 0x25c4e
movq -0x10(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl -0x5c(%rbp), %ecx
addl $0x3, %ecx
movl $0x1, %r8d
callq 0x25a00
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x25c2f
cmpl $0x1, -0x5c(%rbp)
jne 0x25bfa
movl -0x54(%rbp), %eax
movl %eax, -0xa4(%rbp)
jmp 0x25c07
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, -0xa4(%rbp)
jmp 0x25c07
movl -0xa4(%rbp), %edx
movq -0x68(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movl %edx, 0x4(%rax,%rcx,4)
movq -0x10(%rbp), %rdi
movl -0x5c(%rbp), %esi
addl $0x3, %esi
movl $0x1, %edx
callq 0x23070
jmp 0x25c4c
cmpl $0x5, -0x3c(%rbp)
jne 0x25c45
movl -0x74(%rbp), %eax
movl %eax, -0x54(%rbp)
movq $0x0, -0x70(%rbp)
jmp 0x25c4a
jmp 0x25f0c
jmp 0x25c4c
jmp 0x25c4e
jmp 0x25c50
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
jmp 0x25b9e
movq -0x68(%rbp), %rax
movl (%rax), %eax
cmpl -0x54(%rbp), %eax
jae 0x25e9a
movq -0x10(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl $0x3, %ecx
movl $0x1, %r8d
callq 0x25a00
movl %eax, -0x3c(%rbp)
cmpl $0x0, %eax
jne 0x25e9a
movq -0x68(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x84(%rbp)
cmpl $0x0, -0x2c(%rbp)
je 0x25cba
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movl -0x2c(%rbp), %esi
callq 0x22ce0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x25d14
movl -0x58(%rbp), %eax
movslq -0x40(%rbp), %rcx
imulq %rcx, %rax
movq %rax, -0x90(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq -0x80(%rbp), %rsi
callq 0x23e00
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x25d12
movq -0x80(%rbp), %rax
cmpq -0x90(%rbp), %rax
jge 0x25d12
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x5, %esi
leaq -0x90(%rbp), %rdx
callq 0x24890
jmp 0x25d14
jmp 0x25d16
xorl %eax, %eax
cmpl $0x0, -0x3c(%rbp)
movb %al, -0xa5(%rbp)
jne 0x25d44
movq -0x48(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
leaq -0x50(%rbp), %rdx
callq 0x261d0
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
sete %al
movb %al, -0xa5(%rbp)
movb -0xa5(%rbp), %al
testb $0x1, %al
jne 0x25d53
jmp 0x25dfc
movl -0x50(%rbp), %eax
cmpl -0x84(%rbp), %eax
jbe 0x25d6e
movl -0x50(%rbp), %eax
cmpl -0x54(%rbp), %eax
ja 0x25d6e
movl -0x4c(%rbp), %eax
cmpl -0x58(%rbp), %eax
jbe 0x25d70
jmp 0x25d16
movl -0x50(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movl -0x40(%rbp), %ecx
addl $0x18, %ecx
movslq %ecx, %rcx
imulq %rcx, %rax
addq $0x20, %rax
addq $0x18, %rax
movq %rax, -0x98(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x38(%rbp), %rsi
movl -0x40(%rbp), %edx
movq -0x98(%rbp), %rcx
callq 0x23e30
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0x25dba
jmp 0x25dfc
movl -0x4c(%rbp), %eax
subl $0x1, %eax
movl %eax, %eax
movslq -0x40(%rbp), %rcx
imulq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x38(%rbp), %rsi
movl -0x40(%rbp), %edx
movq -0x98(%rbp), %rcx
callq 0xf430
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0x25df7
jmp 0x25dfc
jmp 0x25d16
cmpl $0x0, -0x3c(%rbp)
jne 0x25e87
movl -0x54(%rbp), %eax
movl %eax, -0xac(%rbp)
movq -0x10(%rbp), %rdi
callq 0x23680
movq %rax, %rcx
movl -0xac(%rbp), %eax
movl 0x10(%rcx), %ecx
cmpl %ecx, %eax
jne 0x25e76
movq -0x10(%rbp), %rax
movl 0x5c(%rax), %eax
movslq -0x40(%rbp), %rcx
imulq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0xa0(%rbp), %rsi
callq 0x241d0
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x25e74
cmpl $0x0, -0x2c(%rbp)
je 0x25e74
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl -0x2c(%rbp), %esi
callq 0x22ce0
movl %eax, -0x3c(%rbp)
jmp 0x25e76
cmpl $0x0, -0x3c(%rbp)
jne 0x25e85
movl -0x54(%rbp), %ecx
movq -0x68(%rbp), %rax
movl %ecx, (%rax)
jmp 0x25e87
movq -0x10(%rbp), %rdi
movl $0x3, %esi
movl $0x1, %edx
callq 0x23070
cmpl $0x5, -0x3c(%rbp)
jne 0x25ea7
movl $0x0, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x25f0a
cmpl $0x0, -0x14(%rbp)
je 0x25f0a
movq -0x68(%rbp), %rax
movl (%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x58(%rcx), %eax
jae 0x25ecb
movl $0x5, -0x3c(%rbp)
jmp 0x25f08
cmpl $0x2, -0x14(%rbp)
jne 0x25f06
movq -0x10(%rbp), %rdi
movq -0x70(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl $0x4, %r8d
movl %r8d, %ecx
callq 0x25a00
movl %eax, -0x3c(%rbp)
cmpl $0x0, -0x3c(%rbp)
jne 0x25f04
movq -0x10(%rbp), %rdi
movl $0x4, %edx
movl %edx, %esi
callq 0x23070
jmp 0x25f06
jmp 0x25f08
jmp 0x25f0a
jmp 0x25f0c
movq -0x48(%rbp), %rdi
callq 0x262d0
movl -0x3c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xb0, %rsp
popq %rbp
retq
nopw (%rax,%rax)