// File: /workspace/github_c_makefile_star2_O0/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
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x2c(%rax)
je 0xf213
movq -0x10(%rbp), %rax
movl 0x2c(%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0xf41b
movq -0x10(%rbp), %rax
movzbl 0x13(%rax), %eax
cmpl $0x3, %eax
jge 0xf22c
movl $0x0, -0x4(%rbp)
jmp 0xf41b
movq -0x10(%rbp), %rax
cmpb $0x0, 0x12(%rax)
je 0xf248
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x25140
jmp 0xf3f7
movq -0x10(%rbp), %rdi
callq 0x211a0
cmpl $0x0, %eax
je 0xf2ef
movq -0x10(%rbp), %rax
movq 0x110(%rax), %rdi
callq 0x27d50
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0xf2a8
movq -0x10(%rbp), %rdi
movl $0x1, %esi
leaq -0x30(%rbp), %rdx
xorl %ecx, %ecx
callq 0xe5f0
movl %eax, -0x20(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq $0x0, 0x18(%rax)
cmpq $0x0, -0x28(%rbp)
je 0xf2cb
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %edx
movl $0x1, %ecx
callq 0x21360
movl %eax, -0x20(%rbp)
movq -0x30(%rbp), %rdi
callq 0xec30
cmpl $0x0, -0x20(%rbp)
jne 0xf2ea
movq -0x10(%rbp), %rax
movq 0x110(%rax), %rdi
callq 0x29030
jmp 0xf3f5
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x2b1f0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0xf308
jmp 0xf3f9
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x2b270
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0xf323
jmp 0xf3f9
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x21490
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0xf33c
jmp 0xf3f9
movq -0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x110(%rax), %rdi
callq 0x27d50
movq -0x40(%rbp), %rdi
movq %rax, %rsi
callq 0x21710
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0xf36e
jmp 0xf3f9
movq -0x10(%rbp), %rax
movq 0x110(%rax), %rdi
callq 0x29030
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x24(%rcx), %eax
jbe 0xf3e1
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movl 0x1c(%rax), %ecx
movl 0xc152b(%rip), %eax     # 0xd08d0
movq -0x10(%rbp), %rsi
cltd
idivl 0xbc(%rsi)
movl %eax, %edx
movl -0x44(%rbp), %eax
addl $0x1, %edx
cmpl %edx, %ecx
sete %cl
andb $0x1, %cl
movzbl %cl, %ecx
subl %ecx, %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rdi
movl -0x34(%rbp), %esi
callq 0x28bb0
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0xf3df
jmp 0xf3f9
jmp 0xf3e1
cmpl $0x0, -0x1c(%rbp)
jne 0xf3f3
movq -0x10(%rbp), %rdi
callq 0xf4c0
movl %eax, -0x20(%rbp)
jmp 0xf3f5
jmp 0xf3f7
jmp 0xf3f9
cmpl $0x0, -0x20(%rbp)
jne 0xf415
movq -0x10(%rbp), %rdi
callq 0x211a0
cmpl $0x0, %eax
jne 0xf415
movq -0x10(%rbp), %rax
movb $0x5, 0x13(%rax)
movl -0x20(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)