// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3PagerCommitPhaseOne
SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
  Pager *pPager,                  /* Pager object */
  const char *zMaster,            /* If not NULL, the master 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;

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

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

  if( MEMDB ){
    /* 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{
    if( pagerUseWal(pPager) ){
      PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
      PgHdr *pPageOne = 0;
      if( pList==0 ){
        /* Must have at least one page for the WAL commit flag.
        ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
        rc = sqlite3PagerGet(pPager, 1, &pPageOne);
        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 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.
      */
  #ifdef SQLITE_ENABLE_ATOMIC_WRITE
      PgHdr *pPg;
      assert( isOpen(pPager->jfd) 
           || pPager->journalMode==PAGER_JOURNALMODE_OFF 
           || pPager->journalMode==PAGER_JOURNALMODE_WAL 
      );
      if( !zMaster && isOpen(pPager->jfd) 
       && pPager->journalOff==jrnlBufferSize(pPager) 
       && pPager->dbSize>=pPager->dbOrigSize
       && (0==(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
      rc = pager_incr_changecounter(pPager, 0);
  #endif
      if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
  
      /* Write the master journal name into the journal file. If a master 
      ** journal file name has already been written to the journal file, 
      ** or if zMaster is NULL (no master journal), then this call is a no-op.
      */
      rc = writeMasterJournal(pPager, zMaster);
      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;
  
      rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
      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);
      }
      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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl 0x2c(%rdi), %r12d
testl %r12d, %r12d
jne 0xd2ea
movq %rdi, %rbx
cmpb $0x3, 0x13(%rdi)
jb 0xd2e7
cmpb $0x0, 0x12(%rbx)
je 0xd2fc
movq 0x70(%rbx), %rdi
callq 0x1a7a0
cmpq $0x0, 0x118(%rbx)
jne 0xd2e7
movb $0x5, 0x13(%rbx)
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x0, 0x118(%rbx)
je 0xd328
movq 0x110(%rbx), %rdi
callq 0x1c848
andq $0x0, -0x38(%rbp)
testq %rax, %rax
je 0xd3d2
xorl %r14d, %r14d
jmp 0xd3f1
movl %edx, %r14d
movq %rsi, %r15
cmpb $0x0, 0x15(%rbx)
je 0xd42c
testq %r15, %r15
je 0xd34f
movzbl 0x9(%rbx), %eax
cmpl $0x2, %eax
je 0xd34f
cmpl $0x4, %eax
jne 0xd47b
movq %rbx, %rdi
xorl %esi, %esi
callq 0x18abb
testl %eax, %eax
jne 0xd56c
movq 0x110(%rbx), %rdi
callq 0x1c848
movq %rbx, %rdi
movq %rax, %rsi
callq 0x18c5d
testl %eax, %eax
jne 0xd56c
movq 0x110(%rbx), %rdi
callq 0x1d06f
movl 0x1c(%rbx), %esi
cmpl 0x24(%rbx), %esi
jbe 0xd3bc
movl 0x7a4a6(%rip), %eax     # 0x87840
cltd
idivl 0xbc(%rbx)
incl %eax
xorl %ecx, %ecx
cmpl %eax, %esi
sete %cl
subl %ecx, %esi
movq %rbx, %rdi
callq 0x1cfc2
testl %eax, %eax
jne 0xd56c
testl %r14d, %r14d
jne 0xd2d9
movq %rbx, %rdi
callq 0xd5a9
movl %eax, %r12d
jmp 0xd41e
pushq $0x1
popq %rsi
leaq -0x38(%rbp), %r14
movq %rbx, %rdi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0xca7d
movq (%r14), %r14
andq $0x0, 0x18(%r14)
movq %r14, %rax
movl 0x1c(%rbx), %edx
pushq $0x1
popq %rcx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x185a9
movl %eax, %r12d
movq %r14, %rdi
callq 0xcf6e
testl %r12d, %r12d
jne 0xd41e
movq 0x110(%rbx), %rdi
callq 0x1d06f
testl %r12d, %r12d
jne 0xd2ea
jmp 0xd2d9
cmpl $0x0, 0x1c(%rbx)
je 0xd338
pushq $0x1
popq %rsi
leaq -0x38(%rbp), %r13
movq %rbx, %rdi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0xca7d
movq (%r13), %r13
testl %eax, %eax
jne 0xd462
movq %r13, %rdi
callq 0xd10b
testl %eax, %eax
je 0xd574
movl %eax, %r12d
movq %r13, %rdi
callq 0xcf6e
testl %r12d, %r12d
jne 0xd2ea
jmp 0xd338
movb $0x1, 0x16(%rbx)
xorl %r13d, %r13d
xorl %ecx, %ecx
movsbl (%r15,%r13), %eax
testl %eax, %eax
je 0xd494
addl %eax, %ecx
incq %r13
jmp 0xd484
cmpb $0x0, 0xc(%rbx)
movl %ecx, -0x3c(%rbp)
je 0xd4ae
movq %rbx, %rdi
callq 0x1a23a
movq %rax, %rcx
movq %rax, 0x60(%rbx)
jmp 0xd4b2
movq 0x60(%rbx), %rcx
movq 0x50(%rbx), %rdi
movl 0x7a384(%rip), %eax     # 0x87840
cltd
idivl 0xbc(%rbx)
leal 0x1(%rax), %edx
movq %rcx, -0x30(%rbp)
movq %rcx, %rsi
callq 0x18e5b
testl %eax, %eax
jne 0xd56c
movq 0x50(%rbx), %rdi
movq -0x30(%rbp), %rax
leaq 0x4(%rax), %rcx
movq (%rdi), %rax
movq %r15, %rsi
movl %r13d, %edx
callq *0x18(%rax)
testl %eax, %eax
jne 0xd56c
movq 0x50(%rbx), %rdi
movq -0x30(%rbp), %rax
leaq (%rax,%r13), %rsi
addq $0x4, %rsi
movl %r13d, %edx
callq 0x18e5b
testl %eax, %eax
jne 0xd56c
movq 0x50(%rbx), %rdi
movq -0x30(%rbp), %rax
leaq (%rax,%r13), %rsi
addq $0x8, %rsi
movl -0x3c(%rbp), %edx
callq 0x18e5b
testl %eax, %eax
jne 0xd56c
movq 0x50(%rbx), %rdi
movq -0x30(%rbp), %rax
leaq (%rax,%r13), %rcx
addq $0xc, %rcx
movq (%rdi), %rax
leaq 0x5ae16(%rip), %rsi     # 0x6835e
pushq $0x8
popq %rdx
callq *0x18(%rax)
testl %eax, %eax
jne 0xd56c
addl $0x14, %r13d
addq %r13, 0x60(%rbx)
movq 0x50(%rbx), %rdi
movq (%rdi), %rax
leaq -0x38(%rbp), %rsi
callq *0x30(%rax)
testl %eax, %eax
je 0xd588
movl %eax, %r12d
jmp 0xd2ea
movq %r13, %rdi
callq 0x19241
movb $0x1, 0x15(%rbx)
xorl %r12d, %r12d
jmp 0xd465
movq 0x60(%rbx), %rsi
cmpq %rsi, -0x38(%rbp)
jle 0xd34f
movq 0x50(%rbx), %rdi
movq (%rdi), %rax
callq *0x20(%rax)
testl %eax, %eax
jne 0xd56c
jmp 0xd34f