// File: /workspace/github_c_makefile_star2_O3/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 0x20891
movl $0x3, %r12d
cmpb $0x0, 0x11(%rbx)
jne 0x20891
movq %rdi, %r14
movq 0x8(%rdi), %r15
cmpb $0x2, 0x13(%rbx)
jne 0x20815
cmpq $0x0, 0x118(%rbx)
jne 0x20811
cmpb $0x2, 0x9(%rbx)
jne 0x2096f
movb $0x3, 0x13(%rbx)
movzwl 0x2c(%r14), %ecx
movl %ecx, %eax
andl $-0x21, %eax
movw %ax, 0x2c(%r14)
testb $0x2, %cl
jne 0x20839
orl $0x2, %eax
movw %ax, 0x2c(%r14)
movq %r14, %rdi
callq 0x1f7db
movq 0x20(%r14), %rax
movl 0x28(%r14), %r12d
movq 0x40(%rax), %rdi
movl %r12d, %esi
callq 0x1b0b1
testl %eax, %eax
je 0x2085f
movq %r14, %rdi
callq 0x1a636
testl %eax, %eax
jne 0x20869
jmp 0x20882
cmpq $0x0, 0x118(%rbx)
je 0x208a3
movq %r14, %rdi
callq 0x1a636
testl %eax, %eax
je 0x20882
movq %r14, %rdi
callq 0x1a69a
movl %eax, %r12d
jmp 0x20885
xorl %r12d, %r12d
movl 0x28(%r14), %eax
cmpl %eax, 0x1c(%rbx)
jae 0x20891
movl %eax, 0x1c(%rbx)
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl 0x20(%rbx), %r12d
ja 0x209b2
movq 0x50(%rbx), %rdi
movq (%rdi), %rax
testq %rax, %rax
je 0x209b2
movq 0x60(%rbx), %rcx
movl 0x34(%rbx), %r13d
movslq 0xbc(%rbx), %rsi
cmpq $0xc9, %rsi
jl 0x208f8
addq $0xc8, %rsi
movzbl -0x190(%r15,%rsi), %edx
addl %edx, %r13d
addq $-0xc8, %rsi
cmpq $0x190, %rsi            # imm = 0x190
ja 0x208dc
orb $0x4, 0x2c(%r14)
bswapl %r12d
leaq -0x34(%rbp), %rsi
movl %r12d, (%rsi)
movl $0x4, %edx
movq %rcx, -0x30(%rbp)
callq *0x18(%rax)
testl %eax, %eax
jne 0x20967
movq 0x50(%rbx), %rdi
movl 0xbc(%rbx), %edx
movq -0x30(%rbp), %rcx
addq $0x4, %rcx
movq (%rdi), %rax
movq %r15, %rsi
movq %rcx, %r15
callq *0x18(%rax)
testl %eax, %eax
jne 0x20967
movq 0x50(%rbx), %rdi
movslq 0xbc(%rbx), %rax
addq %rax, %r15
bswapl %r13d
leaq -0x38(%rbp), %rsi
movl %r13d, (%rsi)
movq (%rdi), %rax
movl $0x4, %edx
movq %r15, %rcx
callq *0x18(%rax)
testl %eax, %eax
je 0x20a33
movl %eax, %r12d
jmp 0x20891
movq (%rbx), %r12
movl 0x1c(%rbx), %edi
callq 0x1b61e
movq %rax, 0x40(%rbx)
testq %rax, %rax
je 0x209c6
movq 0x50(%rbx), %rdx
cmpq $0x0, (%rdx)
jne 0x209f8
cmpb $0x4, 0x9(%rbx)
jne 0x209d1
xorps %xmm0, %xmm0
movups %xmm0, 0x18(%rdx)
movups %xmm0, 0x8(%rdx)
movq $0x0, 0x28(%rdx)
leaq 0x724a3(%rip), %rax     # 0x92e50
movq %rax, (%rdx)
jmp 0x209f8
cmpb $0x4, 0x13(%rbx)
je 0x20869
orb $0x4, 0x2c(%r14)
jmp 0x20869
movl $0x7, %r12d
jmp 0x20891
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 0x20a1a
movl $0x0, 0x30(%rbx)
movb $0x0, 0x16(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x60(%rbx)
movq %rbx, %rdi
callq 0x1c7f5
testl %eax, %eax
je 0x20811
movl %eax, %r12d
movq 0x40(%rbx), %rdi
callq 0x1dcec
movq $0x0, 0x40(%rbx)
jmp 0x20891
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 0x1b472
movl %eax, %r15d
movl 0x28(%r14), %esi
movq %rbx, %rdi
callq 0x1b14d
movl %eax, %r12d
orl %r15d, %r12d
jne 0x20891
jmp 0x20869