// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: pager_write
static int pager_write(PgHdr *pPg){
  void *pData = pPg->pData;
  Pager *pPager = pPg->pPager;
  int rc = SQLITE_OK;

  /* This routine is not called unless a write-transaction has already 
  ** been started. The journal file may or may not be open at this point.
  ** It is never called in the ERROR state.
  */
  assert( pPager->eState==PAGER_WRITER_LOCKED
       || pPager->eState==PAGER_WRITER_CACHEMOD
       || pPager->eState==PAGER_WRITER_DBMOD
  );
  assert( assert_pager_state(pPager) );

  /* If an error has been previously detected, report the same error
  ** again. This should not happen, but the check provides robustness. */
  if( NEVER(pPager->errCode) )  return pPager->errCode;

  /* Higher-level routines never call this function if database is not
  ** writable.  But check anyway, just for robustness. */
  if( NEVER(pPager->readOnly) ) return SQLITE_PERM;

  CHECK_PAGE(pPg);

  /* The journal file needs to be opened. Higher level routines have already
  ** obtained the necessary locks to begin the write-transaction, but the
  ** rollback journal might not yet be open. Open it now if this is the case.
  **
  ** This is done before calling sqlite3PcacheMakeDirty() on the page. 
  ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
  ** an error might occur and the pager would end up in WRITER_LOCKED state
  ** with pages marked as dirty in the cache.
  */
  if( pPager->eState==PAGER_WRITER_LOCKED ){
    rc = pager_open_journal(pPager);
    if( rc!=SQLITE_OK ) return rc;
  }
  assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
  assert( assert_pager_state(pPager) );

  /* Mark the page as dirty.  If the page has already been written
  ** to the journal then we can return right away.
  */
  sqlite3PcacheMakeDirty(pPg);
  if( pageInJournal(pPg) && !subjRequiresPage(pPg) ){
    assert( !pagerUseWal(pPager) );
  }else{
  
    /* The transaction journal now exists and we have a RESERVED or an
    ** EXCLUSIVE lock on the main database file.  Write the current page to
    ** the transaction journal if it is not there already.
    */
    if( !pageInJournal(pPg) && !pagerUseWal(pPager) ){
      assert( pagerUseWal(pPager)==0 );
      if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
        u32 cksum;
        char *pData2;
        i64 iOff = pPager->journalOff;

        /* We should never write to the journal file the page that
        ** contains the database locks.  The following assert verifies
        ** that we do not. */
        assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );

        assert( pPager->journalHdr<=pPager->journalOff );
        CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
        cksum = pager_cksum(pPager, (u8*)pData2);

        /* Even if an IO or diskfull error occurs while journalling the
        ** page in the block above, set the need-sync flag for the page.
        ** Otherwise, when the transaction is rolled back, the logic in
        ** playback_one_page() will think that the page needs to be restored
        ** in the database file. And if an IO error occurs while doing so,
        ** then corruption may follow.
        */
        pPg->flags |= PGHDR_NEED_SYNC;

        rc = write32bits(pPager->jfd, iOff, pPg->pgno);
        if( rc!=SQLITE_OK ) return rc;
        rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
        if( rc!=SQLITE_OK ) return rc;
        rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
        if( rc!=SQLITE_OK ) return rc;

        IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, 
                 pPager->journalOff, pPager->pageSize));
        PAGER_INCR(sqlite3_pager_writej_count);
        PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
             PAGERID(pPager), pPg->pgno, 
             ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));

        pPager->journalOff += 8 + pPager->pageSize;
        pPager->nRec++;
        assert( pPager->pInJournal!=0 );
        rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
        testcase( rc==SQLITE_NOMEM );
        assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
        rc |= addToSavepointBitvecs(pPager, pPg->pgno);
        if( rc!=SQLITE_OK ){
          assert( rc==SQLITE_NOMEM );
          return rc;
        }
      }else{
        if( pPager->eState!=PAGER_WRITER_DBMOD ){
          pPg->flags |= PGHDR_NEED_SYNC;
        }
        PAGERTRACE(("APPEND %d page %d needSync=%d\n",
                PAGERID(pPager), pPg->pgno,
               ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
      }
    }
  
    /* If the statement journal is open and the page is not in it,
    ** then write the current page to the statement journal.  Note that
    ** the statement journal format differs from the standard journal format
    ** in that it omits the checksums and the header.
    */
    if( subjRequiresPage(pPg) ){
      rc = subjournalPage(pPg);
    }
  }

  /* Update the database size and return.
  */
  if( pPager->dbSize<pPg->pgno ){
    pPager->dbSize = pPg->pgno;
  }
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq 0x20(%rdi), %rbx
movl 0x2c(%rbx), %r12d
testl %r12d, %r12d
jne 0x1dd00
cmpb $0x0, 0x11(%rbx)
je 0x1dc87
pushq $0x3
popq %r12
jmp 0x1dd00
movq %rdi, %r14
movq 0x8(%rdi), %r15
cmpb $0x2, 0x13(%rbx)
jne 0x1dcac
cmpq $0x0, 0x118(%rbx)
jne 0x1dca8
cmpb $0x2, 0x9(%rbx)
jne 0x1dda7
movb $0x3, 0x13(%rbx)
movq %r14, %rdi
callq 0x1ce20
movq %r14, %rdi
callq 0x1dea4
testl %eax, %eax
je 0x1dcce
movq %r14, %rdi
callq 0x18474
testl %eax, %eax
jne 0x1dcd8
jmp 0x1dcf1
cmpq $0x0, 0x118(%rbx)
je 0x1dd12
movq %r14, %rdi
callq 0x18474
testl %eax, %eax
je 0x1dcf1
movq %r14, %rdi
callq 0x184e0
movl %eax, %r12d
jmp 0x1dcf4
xorl %r12d, %r12d
movl 0x28(%r14), %eax
cmpl %eax, 0x1c(%rbx)
jae 0x1dd00
movl %eax, 0x1c(%rbx)
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x28(%r14), %r12d
cmpl 0x20(%rbx), %r12d
ja 0x1dde7
movq 0x50(%rbx), %r13
cmpq $0x0, (%r13)
je 0x1dde7
movq 0x60(%rbx), %rax
movq %rax, -0x38(%rbp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1cdd1
movl %eax, -0x2c(%rbp)
orb $0x4, 0x2c(%r14)
movq %r13, %rdi
movq -0x38(%rbp), %r13
movq %r13, %rsi
movl %r12d, %edx
callq 0x18e5b
testl %eax, %eax
jne 0x1dd9f
movq 0x50(%rbx), %rdi
movl 0xbc(%rbx), %edx
addq $0x4, %r13
movq (%rdi), %rax
movq %r15, %rsi
movq %r13, %rcx
callq *0x18(%rax)
testl %eax, %eax
jne 0x1dd9f
movq 0x50(%rbx), %rdi
movslq 0xbc(%rbx), %rax
addq %rax, %r13
movq %r13, %rsi
movl -0x2c(%rbp), %edx
callq 0x18e5b
testl %eax, %eax
je 0x1de5e
movl %eax, %r12d
jmp 0x1dd00
movq (%rbx), %r12
movl 0x1c(%rbx), %edi
callq 0x19221
movq %rax, 0x40(%rbx)
testq %rax, %rax
je 0x1ddfb
movq 0x50(%rbx), %rdx
cmpq $0x0, (%rdx)
jne 0x1de29
cmpb $0x4, 0x9(%rbx)
jne 0x1de02
xorps %xmm0, %xmm0
andq $0x0, 0x28(%rdx)
movups %xmm0, 0x18(%rdx)
movups %xmm0, 0x8(%rdx)
leaq 0x680be(%rip), %rax     # 0x85ea0
movq %rax, (%rdx)
jmp 0x1de29
cmpb $0x4, 0x13(%rbx)
je 0x1dcd8
orb $0x4, 0x2c(%r14)
jmp 0x1dcd8
pushq $0x7
jmp 0x1dc83
cmpb $0x0, 0x10(%rbx)
movl $0x806, %eax            # imm = 0x806
movl $0x100e, %ecx           # imm = 0x100E
cmovel %eax, %ecx
movq 0xd8(%rbx), %rsi
movq %r12, %rdi
xorl %r8d, %r8d
callq *0x28(%r12)
testl %eax, %eax
jne 0x1de48
andl $0x0, 0x30(%rbx)
movb $0x0, 0x16(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x60(%rbx)
movq %rbx, %rdi
callq 0x1a262
testl %eax, %eax
je 0x1dca8
movl %eax, %r12d
movq 0x40(%rbx), %rdi
callq 0x1b557
andq $0x0, 0x40(%rbx)
jmp 0x1dd00
movslq 0xbc(%rbx), %rax
movq 0x40(%rbx), %rdi
movq 0x60(%rbx), %rcx
addq %rcx, %rax
addq $0x8, %rax
movq %rax, 0x60(%rbx)
incl 0x30(%rbx)
movl 0x28(%r14), %esi
callq 0x190c4
movl %eax, %r15d
movl 0x28(%r14), %esi
movq %rbx, %rdi
callq 0x18e7d
movl %eax, %r12d
orl %r15d, %r12d
jne 0x1dd00
jmp 0x1dcd8