// File: /workspace/github_c_makefile_star2_O0/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: sqlite3WalCheckpoint
SQLITE_PRIVATE int sqlite3WalCheckpoint(
  Wal *pWal,                      /* Wal connection */
  sqlite3 *db,                    /* Check this handle's interrupt flag */
  int eMode,                      /* PASSIVE, FULL, RESTART, or TRUNCATE */
  int (*xBusy)(void*),            /* Function to call when busy */
  void *pBusyArg,                 /* Context argument for xBusyHandler */
  int sync_flags,                 /* Flags to sync db file with (or 0) */
  int nBuf,                       /* Size of temporary buffer */
  u8 *zBuf,                       /* Temporary buffer to use */
  int *pnLog,                     /* OUT: Number of frames in WAL */
  int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
){
  int rc;                         /* Return code */
  int isChanged = 0;              /* True if a new wal-index header is loaded */
  int eMode2 = eMode;             /* Mode to pass to walCheckpoint() */
  int (*xBusy2)(void*) = xBusy;   /* Busy handler for eMode2 */

  assert( pWal->ckptLock==0 );
  assert( pWal->writeLock==0 );

  /* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
  ** in the SQLITE_CHECKPOINT_PASSIVE mode. */
  assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );

  if( pWal->readOnly ) return SQLITE_READONLY;
  WALTRACE(("WAL%p: checkpoint begins\n", pWal));

  /* Enable blocking locks, if possible. If blocking locks are successfully
  ** enabled, set xBusy2=0 so that the busy-handler is never invoked. */
  sqlite3WalDb(pWal, db);
  (void)walEnableBlocking(pWal);

  /* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
  ** "checkpoint" lock on the database file.
  ** EVIDENCE-OF: R-10421-19736 If any other process is running a
  ** checkpoint operation at the same time, the lock cannot be obtained and
  ** SQLITE_BUSY is returned.
  ** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
  ** it will not be invoked in this case.
  */
  rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
  testcase( rc==SQLITE_BUSY );
  testcase( rc!=SQLITE_OK && xBusy2!=0 );
  if( rc==SQLITE_OK ){
    pWal->ckptLock = 1;

    /* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
    ** TRUNCATE modes also obtain the exclusive "writer" lock on the database
    ** file.
    **
    ** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
    ** immediately, and a busy-handler is configured, it is invoked and the
    ** writer lock retried until either the busy-handler returns 0 or the
    ** lock is successfully obtained.
    */
    if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
      rc = walBusyLock(pWal, xBusy2, pBusyArg, WAL_WRITE_LOCK, 1);
      if( rc==SQLITE_OK ){
        pWal->writeLock = 1;
      }else if( rc==SQLITE_BUSY ){
        eMode2 = SQLITE_CHECKPOINT_PASSIVE;
        xBusy2 = 0;
        rc = SQLITE_OK;
      }
    }
  }


  /* Read the wal-index header. */
  if( rc==SQLITE_OK ){
    walDisableBlocking(pWal);
    rc = walIndexReadHdr(pWal, &isChanged);
    (void)walEnableBlocking(pWal);
    if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
      sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
    }
  }

  /* Copy data from the log to the database file. */
  if( rc==SQLITE_OK ){

    if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
      rc = SQLITE_CORRUPT_BKPT;
    }else{
      rc = walCheckpoint(pWal, db, eMode2, xBusy2, pBusyArg, sync_flags, zBuf);
    }

    /* If no error occurred, set the output variables. */
    if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
      if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
      if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
    }
  }

  if( isChanged ){
    /* If a new wal-index header was loaded before the checkpoint was
    ** performed, then the pager-cache associated with pWal is now
    ** out of date. So zero the cached wal-index header to ensure that
    ** next time the pager opens a snapshot on this database it knows that
    ** the cache needs to be reset.
    */
    memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
  }

  walDisableBlocking(pWal);
  sqlite3WalDb(pWal, 0);

  /* Release the locks. */
  sqlite3WalEndWriteTransaction(pWal);
  if( pWal->ckptLock ){
    walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
    pWal->ckptLock = 0;
  }
  WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
  if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY;
#endif
  return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, -0x40(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x42(%rax)
je 0x3e5489
movl $0x8, -0x4(%rbp)
jmp 0x3e563d
movq -0x10(%rbp), %rdi
movl $0x1, %edx
movl %edx, %esi
callq 0x3de5e0
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jne 0x3e54fe
movq -0x10(%rbp), %rax
movb $0x1, 0x41(%rax)
cmpl $0x0, -0x1c(%rbp)
je 0x3e54fc
movq -0x10(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x30(%rbp), %rdx
xorl %ecx, %ecx
movl $0x1, %r8d
callq 0x3e5650
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
jne 0x3e54dc
movq -0x10(%rbp), %rax
movb $0x1, 0x40(%rax)
jmp 0x3e54fa
cmpl $0x5, -0x38(%rbp)
jne 0x3e54f8
movl $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x38(%rbp)
jmp 0x3e54fa
jmp 0x3e54fc
jmp 0x3e54fe
cmpl $0x0, -0x38(%rbp)
jne 0x3e5540
movq -0x10(%rbp), %rdi
leaq -0x3c(%rbp), %rsi
callq 0x3deb90
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x3c(%rbp)
je 0x3e553e
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq (%rax), %rax
cmpl $0x3, (%rax)
jl 0x3e553e
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x3e1110
jmp 0x3e5540
cmpl $0x0, -0x38(%rbp)
jne 0x3e55d4
movq -0x10(%rbp), %rax
cmpl $0x0, 0x58(%rax)
je 0x3e5571
movq -0x10(%rbp), %rdi
callq 0x3e56c0
cmpl 0x10(%rbp), %eax
je 0x3e5571
movl $0xf6f6, %edi           # imm = 0xF6F6
callq 0x3dff80
movl %eax, -0x38(%rbp)
jmp 0x3e5598
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x40(%rbp), %edx
movq -0x48(%rbp), %rcx
movq -0x30(%rbp), %r8
movl -0x34(%rbp), %r9d
movq 0x18(%rbp), %rax
movq %rax, (%rsp)
callq 0x3e56f0
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x38(%rbp)
je 0x3e55a4
cmpl $0x5, -0x38(%rbp)
jne 0x3e55d2
cmpq $0x0, 0x20(%rbp)
je 0x3e55b8
movq -0x10(%rbp), %rax
movl 0x58(%rax), %ecx
movq 0x20(%rbp), %rax
movl %ecx, (%rax)
cmpq $0x0, 0x28(%rbp)
je 0x3e55d0
movq -0x10(%rbp), %rdi
callq 0x3de5c0
movl (%rax), %ecx
movq 0x28(%rbp), %rax
movl %ecx, (%rax)
jmp 0x3e55d2
jmp 0x3e55d4
cmpl $0x0, -0x3c(%rbp)
je 0x3e55ee
movq -0x10(%rbp), %rdi
addq $0x48, %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x19360
movq -0x10(%rbp), %rdi
callq 0x3dfe30
movq -0x10(%rbp), %rax
cmpb $0x0, 0x41(%rax)
je 0x3e5619
movq -0x10(%rbp), %rdi
movl $0x1, %edx
movl %edx, %esi
callq 0x3de700
movq -0x10(%rbp), %rax
movb $0x0, 0x41(%rax)
cmpl $0x0, -0x38(%rbp)
jne 0x3e5631
movl -0x1c(%rbp), %eax
cmpl -0x40(%rbp), %eax
je 0x3e5631
movl $0x5, %eax
movl %eax, -0x4c(%rbp)
jmp 0x3e5637
movl -0x38(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)