// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: sqlite3PagerCommitPhaseOne
SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
  Pager *pPager,                  /* Pager object */
  const char *zSuper,            /* If not NULL, the super-journal name */
  int noSync                      /* True to omit the xSync on the db file */
){
  int rc = SQLITE_OK;             /* Return code */

  assert( pPager->eState==PAGER_WRITER_LOCKED
       || pPager->eState==PAGER_WRITER_CACHEMOD
       || pPager->eState==PAGER_WRITER_DBMOD
       || pPager->eState==PAGER_ERROR
  );
  assert( assert_pager_state(pPager) );

  /* If a prior error occurred, report that error again. */
  if( NEVER(pPager->errCode) ) return pPager->errCode;

  /* Provide the ability to easily simulate an I/O error during testing */
  if( sqlite3FaultSim(400) ) return SQLITE_IOERR;

  PAGERTRACE(("DATABASE SYNC: File=%s zSuper=%s nSize=%d\n",
      pPager->zFilename, zSuper, pPager->dbSize));

  /* If no database changes have been made, return early. */
  if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;

  assert( MEMDB==0 || pPager->tempFile );
  assert( isOpen(pPager->fd) || pPager->tempFile );
  if( 0==pagerFlushOnCommit(pPager, 1) ){
    /* If this is an in-memory db, or no pages have been written to, or this
    ** function has already been called, it is mostly a no-op.  However, any
    ** backup in progress needs to be restarted.  */
    sqlite3BackupRestart(pPager->pBackup);
  }else{
    PgHdr *pList;
    if( pagerUseWal(pPager) ){
      PgHdr *pPageOne = 0;
      pList = sqlite3PcacheDirtyList(pPager->pPCache);
      if( pList==0 ){
        /* Must have at least one page for the WAL commit flag.
        ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
        rc = sqlite3PagerGet(pPager, 1, &pPageOne, 0);
        pList = pPageOne;
        pList->pDirty = 0;
      }
      assert( rc==SQLITE_OK );
      if( ALWAYS(pList) ){
        rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
      }
      sqlite3PagerUnref(pPageOne);
      if( rc==SQLITE_OK ){
        sqlite3PcacheCleanAll(pPager->pPCache);
      }
    }else{
      /* The bBatch boolean is true if the batch-atomic-write commit method
      ** should be used.  No rollback journal is created if batch-atomic-write
      ** is enabled.
      */
#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
      sqlite3_file *fd = pPager->fd;
      int bBatch = zSuper==0    /* An SQLITE_IOCAP_BATCH_ATOMIC commit */
        && (sqlite3OsDeviceCharacteristics(fd) & SQLITE_IOCAP_BATCH_ATOMIC)
        && !pPager->noSync
        && sqlite3JournalIsInMemory(pPager->jfd);
#else
#     define bBatch 0
#endif

#ifdef SQLITE_ENABLE_ATOMIC_WRITE
      /* The following block updates the change-counter. Exactly how it
      ** does this depends on whether or not the atomic-update optimization
      ** was enabled at compile time, and if this transaction meets the
      ** runtime criteria to use the operation:
      **
      **    * The file-system supports the atomic-write property for
      **      blocks of size page-size, and
      **    * This commit is not part of a multi-file transaction, and
      **    * Exactly one page has been modified and store in the journal file.
      **
      ** If the optimization was not enabled at compile time, then the
      ** pager_incr_changecounter() function is called to update the change
      ** counter in 'indirect-mode'. If the optimization is compiled in but
      ** is not applicable to this transaction, call sqlite3JournalCreate()
      ** to make sure the journal file has actually been created, then call
      ** pager_incr_changecounter() to update the change-counter in indirect
      ** mode.
      **
      ** Otherwise, if the optimization is both enabled and applicable,
      ** then call pager_incr_changecounter() to update the change-counter
      ** in 'direct' mode. In this case the journal file will never be
      ** created for this transaction.
      */
      if( bBatch==0 ){
        PgHdr *pPg;
        assert( isOpen(pPager->jfd)
            || pPager->journalMode==PAGER_JOURNALMODE_OFF
            || pPager->journalMode==PAGER_JOURNALMODE_WAL
            );
        if( !zSuper && isOpen(pPager->jfd)
         && pPager->journalOff==jrnlBufferSize(pPager)
         && pPager->dbSize>=pPager->dbOrigSize
         && (!(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
        ){
          /* Update the db file change counter via the direct-write method. The
          ** following call will modify the in-memory representation of page 1
          ** to include the updated change counter and then write page 1
          ** directly to the database file. Because of the atomic-write
          ** property of the host file-system, this is safe.
          */
          rc = pager_incr_changecounter(pPager, 1);
        }else{
          rc = sqlite3JournalCreate(pPager->jfd);
          if( rc==SQLITE_OK ){
            rc = pager_incr_changecounter(pPager, 0);
          }
        }
      }
#else  /* SQLITE_ENABLE_ATOMIC_WRITE */
#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
      if( zSuper ){
        rc = sqlite3JournalCreate(pPager->jfd);
        if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
        assert( bBatch==0 );
      }
#endif
      rc = pager_incr_changecounter(pPager, 0);
#endif /* !SQLITE_ENABLE_ATOMIC_WRITE */
      if( rc!=SQLITE_OK ) goto commit_phase_one_exit;

      /* Write the super-journal name into the journal file. If a
      ** super-journal file name has already been written to the journal file,
      ** or if zSuper is NULL (no super-journal), then this call is a no-op.
      */
      rc = writeSuperJournal(pPager, zSuper);
      if( rc!=SQLITE_OK ) goto commit_phase_one_exit;

      /* Sync the journal file and write all dirty pages to the database.
      ** If the atomic-update optimization is being used, this sync will not
      ** create the journal file or perform any real IO.
      **
      ** Because the change-counter page was just modified, unless the
      ** atomic-update optimization is used it is almost certain that the
      ** journal requires a sync here. However, in locking_mode=exclusive
      ** on a system under memory pressure it is just possible that this is
      ** not the case. In this case it is likely enough that the redundant
      ** xSync() call will be changed to a no-op by the OS anyhow.
      */
      rc = syncJournal(pPager, 0);
      if( rc!=SQLITE_OK ) goto commit_phase_one_exit;

      pList = sqlite3PcacheDirtyList(pPager->pPCache);
#ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
      if( bBatch ){
        rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE, 0);
        if( rc==SQLITE_OK ){
          rc = pager_write_pagelist(pPager, pList);
          if( rc==SQLITE_OK ){
            rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, 0);
          }
          if( rc!=SQLITE_OK ){
            sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
          }
        }

        if( (rc&0xFF)==SQLITE_IOERR && rc!=SQLITE_IOERR_NOMEM ){
          rc = sqlite3JournalCreate(pPager->jfd);
          if( rc!=SQLITE_OK ){
            sqlite3OsClose(pPager->jfd);
            goto commit_phase_one_exit;
          }
          bBatch = 0;
        }else{
          sqlite3OsClose(pPager->jfd);
        }
      }
#endif /* SQLITE_ENABLE_BATCH_ATOMIC_WRITE */

      if( bBatch==0 ){
        rc = pager_write_pagelist(pPager, pList);
      }
      if( rc!=SQLITE_OK ){
        assert( rc!=SQLITE_IOERR_BLOCKED );
        goto commit_phase_one_exit;
      }
      sqlite3PcacheCleanAll(pPager->pPCache);

      /* If the file on disk is smaller than the database image, use
      ** pager_truncate to grow the file here. This can happen if the database
      ** image was extended as part of the current transaction and then the
      ** last page in the db image moved to the free-list. In this case the
      ** last page is never written out to disk, leaving the database file
      ** undersized. Fix this now if it is the case.  */
      if( pPager->dbSize>pPager->dbFileSize ){
        Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
        assert( pPager->eState==PAGER_WRITER_DBMOD );
        rc = pager_truncate(pPager, nNew);
        if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
      }

      /* Finally, sync the database file. */
      if( !noSync ){
        rc = sqlite3PagerSync(pPager, zSuper);
      }
      IOTRACE(("DBSYNC %p\n", pPager))
    }
  }

commit_phase_one_exit:
  if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
    pPager->eState = PAGER_WRITER_FINISHED;
  }
  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl 0x2c(%rdi), %r15d
testl %r15d, %r15d
jne 0x223822
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x21703b(%rip), %rax    # 0x43a798
testq %rax, %rax
je 0x223776
movl $0x190, %edi            # imm = 0x190
callq *%rax
testl %eax, %eax
je 0x223776
pushq $0xa
popq %r15
jmp 0x223822
cmpb $0x3, 0x14(%rbx)
jb 0x22381f
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x23489f
testl %eax, %eax
je 0x223808
cmpq $0x0, 0x120(%rbx)
je 0x223834
andq $0x0, 0x8(%rsp)
movq 0x118(%rbx), %rdi
callq 0x234394
testq %rax, %rax
jne 0x2237d2
pushq $0x1
popq %rsi
leaq 0x8(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rdx
xorl %ecx, %ecx
callq *0x108(%rbx)
movq (%r14), %rax
andq $0x0, 0x20(%rax)
movl 0x1c(%rbx), %edx
pushq $0x1
popq %rcx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x2301fc
movl %eax, %r15d
movq 0x8(%rsp), %rdi
callq 0x2235a4
testl %r15d, %r15d
jne 0x223801
movq 0x118(%rbx), %rdi
callq 0x2348fb
testl %r15d, %r15d
jne 0x223822
jmp 0x223811
movq 0x70(%rbx), %rdi
callq 0x2344a5
cmpq $0x0, 0x120(%rbx)
jne 0x22381f
movb $0x5, 0x14(%rbx)
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x0, 0x16(%rbx)
je 0x223876
testq %r14, %r14
je 0x2239bf
cmpb $0x4, 0x9(%rbx)
je 0x2239bf
movq 0x50(%rbx), %r15
cmpq $0x0, (%r15)
je 0x2239bf
movb $0x1, 0x17(%rbx)
xorl %r12d, %r12d
xorl %r13d, %r13d
movsbl (%r14,%r12), %eax
testl %eax, %eax
je 0x2238c0
addl %eax, %r13d
incq %r12
jmp 0x223865
cmpl $0x0, 0x1c(%rbx)
je 0x22383a
pushq $0x1
popq %rsi
leaq 0x8(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq *0x108(%rbx)
testl %eax, %eax
jne 0x2238a5
movq 0x8(%rsp), %rdi
callq 0x2236f2
testl %eax, %eax
je 0x223a43
movl %eax, %r15d
movq 0x8(%rsp), %rdi
callq 0x2235a4
testl %r15d, %r15d
jne 0x223822
jmp 0x22383a
cmpb $0x0, 0xc(%rbx)
je 0x2238d7
movq %rbx, %rdi
callq 0x23278e
movq %rax, %rcx
movq %rax, 0x60(%rbx)
jmp 0x2238db
movq 0x60(%rbx), %rcx
movl 0x21718f(%rip), %eax    # 0x43aa70
cltd
idivl 0xbc(%rbx)
leal 0x1(%rax), %edx
movq %r15, %rdi
movq %rcx, 0x10(%rsp)
movq %rcx, %rsi
callq 0x230c64
testl %eax, %eax
jne 0x2239e8
movq 0x50(%rbx), %rdi
movq 0x10(%rsp), %rax
leaq 0x4(%rax), %rcx
movq (%rdi), %rax
movq %r14, %rsi
movl %r12d, %edx
callq *0x18(%rax)
testl %eax, %eax
jne 0x2239e8
movq 0x50(%rbx), %rdi
movq 0x10(%rsp), %rax
leaq (%rax,%r12), %rsi
addq $0x4, %rsi
movl %r12d, %edx
callq 0x230c64
testl %eax, %eax
jne 0x2239e8
movq 0x50(%rbx), %rdi
movq 0x10(%rsp), %rax
leaq (%rax,%r12), %rsi
addq $0x8, %rsi
movl %r13d, %edx
callq 0x230c64
testl %eax, %eax
jne 0x2239e8
movq 0x50(%rbx), %rdi
movq 0x10(%rsp), %rax
leaq (%rax,%r12), %rcx
addq $0xc, %rcx
movq (%rdi), %rax
leaq 0x14e56e(%rip), %rsi    # 0x371eef
pushq $0x8
popq %rdx
callq *0x18(%rax)
testl %eax, %eax
jne 0x2239e8
addl $0x14, %r12d
addq %r12, 0x60(%rbx)
movq 0x50(%rbx), %rdi
movq (%rdi), %rax
leaq 0x8(%rsp), %rsi
callq *0x30(%rax)
testl %eax, %eax
jne 0x2239e8
movq 0x60(%rbx), %rsi
cmpq %rsi, 0x8(%rsp)
jle 0x2239bf
movq 0x50(%rbx), %rdi
movq (%rdi), %rax
callq *0x20(%rax)
testl %eax, %eax
jne 0x2239e8
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2308b9
testl %eax, %eax
jne 0x2239e8
movq 0x118(%rbx), %rdi
callq 0x234394
movq %rbx, %rdi
movq %rax, %rsi
callq 0x230a5e
testl %eax, %eax
je 0x2239f0
movl %eax, %r15d
jmp 0x223822
movq 0x118(%rbx), %rdi
callq 0x2348fb
movl 0x1c(%rbx), %esi
cmpl 0x24(%rbx), %esi
jbe 0x223a28
movl 0x217066(%rip), %eax    # 0x43aa70
cltd
idivl 0xbc(%rbx)
incl %eax
xorl %ecx, %ecx
cmpl %eax, %esi
sete %cl
subl %ecx, %esi
movq %rbx, %rdi
callq 0x2346d6
testl %eax, %eax
jne 0x2239e8
testl %ebp, %ebp
jne 0x223811
movq %rbx, %rdi
movq %r14, %rsi
callq 0x223a59
movl %eax, %r15d
jmp 0x223801
movq 0x8(%rsp), %rdi
callq 0x23117e
movb $0x1, 0x16(%rbx)
xorl %r15d, %r15d
jmp 0x2238a8