// File: /workspace/github_c_makefile_star2_O1/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 0x20ae4
movl $0x3, %r12d
cmpb $0x0, 0x11(%rbx)
je 0x20af6
movl %r12d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, %r14
movq 0x8(%rdi), %rax
movq %rax, -0x30(%rbp)
cmpb $0x2, 0x13(%rbx)
jne 0x20bff
xorl %r12d, %r12d
cmpq $0x0, 0x118(%rbx)
jne 0x20bda
cmpb $0x2, 0x9(%rbx)
je 0x20bda
movq (%rbx), %r12
movl 0x1c(%rbx), %r15d
movl $0x200, %edi            # imm = 0x200
callq 0xa2a2
movq %rax, %r13
testq %rax, %rax
je 0x20b87
movq %r12, -0x38(%rbp)
xorl %r12d, %r12d
movl $0x200, %edx            # imm = 0x200
movq %r13, %rdi
xorl %esi, %esi
callq 0x8200
movl %r15d, (%r13)
movq %r13, 0x40(%rbx)
movq 0x50(%rbx), %rdx
cmpq $0x0, (%rdx)
jne 0x20bb8
cmpb $0x4, 0x9(%rbx)
jne 0x20b93
xorps %xmm0, %xmm0
movups %xmm0, (%rdx)
movups %xmm0, 0x20(%rdx)
movups %xmm0, 0x10(%rdx)
leaq 0x722ce(%rip), %rax     # 0x92e50
movq %rax, (%rdx)
jmp 0x20bb8
movq %r13, 0x40(%rbx)
movl $0x7, %r12d
jmp 0x20bf6
cmpb $0x0, 0x10(%rbx)
movl $0x806, %eax            # imm = 0x806
movl $0x100e, %ecx           # imm = 0x100E
cmovel %eax, %ecx
movq 0xd8(%rbx), %rsi
movq -0x38(%rbp), %rdi
xorl %r8d, %r8d
callq *0x28(%rdi)
movl %eax, %r12d
testl %r12d, %r12d
jne 0x20bda
movl $0x0, 0x30(%rbx)
movb $0x0, 0x16(%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x60(%rbx)
movq %rbx, %rdi
callq 0x1c987
movl %eax, %r12d
testl %r12d, %r12d
je 0x20bf2
movq 0x40(%rbx), %rdi
callq 0x1df1e
movq $0x0, 0x40(%rbx)
jmp 0x20bf6
movb $0x3, 0x13(%rbx)
testl %r12d, %r12d
jne 0x20ae4
movzwl 0x2c(%r14), %ecx
movl %ecx, %eax
andl $-0x21, %eax
movw %ax, 0x2c(%r14)
testb $0x2, %cl
jne 0x20c23
orl $0x2, %eax
movw %ax, 0x2c(%r14)
movq %r14, %rdi
callq 0x1fa07
movq 0x20(%r14), %rax
movq 0x40(%rax), %rdi
movl 0x28(%r14), %r13d
movl %r13d, %esi
callq 0x1b2c2
testl %eax, %eax
je 0x20c52
movl %eax, %r15d
movq %r14, %rdi
callq 0x1a814
xorl %r12d, %r12d
testl %eax, %eax
je 0x20c76
testl %r15d, %r15d
jne 0x20c5f
xorl %r12d, %r12d
cmpq $0x0, 0x118(%rbx)
je 0x20c8b
movq %r14, %rdi
callq 0x1a814
testl %eax, %eax
je 0x20c76
movq %r14, %rdi
callq 0x1a878
movl %eax, %r12d
movl 0x28(%r14), %eax
cmpl %eax, 0x1c(%rbx)
jae 0x20ae4
movl %eax, 0x1c(%rbx)
jmp 0x20ae4
cmpl 0x20(%rbx), %r13d
ja 0x20d5c
movq 0x50(%rbx), %rdi
movq (%rdi), %rax
testq %rax, %rax
je 0x20d5c
movq 0x60(%rbx), %rcx
movl 0x34(%rbx), %r15d
movslq 0xbc(%rbx), %r8
cmpq $0xc9, %r8
jl 0x20ce4
addq $0xc8, %r8
movq -0x30(%rbp), %rsi
movzbl -0x190(%rsi,%r8), %edx
addl %edx, %r15d
addq $-0xc8, %r8
cmpq $0x190, %r8             # imm = 0x190
jg 0x20cc8
orb $0x4, 0x2c(%r14)
bswapl %r13d
leaq -0x3c(%rbp), %rsi
movl %r13d, (%rsi)
movl $0x4, %edx
movq %rcx, %r13
callq *0x18(%rax)
testl %eax, %eax
jne 0x20d4a
movq 0x50(%rbx), %rdi
movl 0xbc(%rbx), %edx
leaq 0x4(%r13), %rcx
movq (%rdi), %rax
movq -0x30(%rbp), %rsi
callq *0x18(%rax)
testl %eax, %eax
jne 0x20d4a
movq 0x50(%rbx), %rdi
movslq 0xbc(%rbx), %rax
leaq (%rax,%r13), %rcx
addq $0x4, %rcx
bswapl %r15d
leaq -0x40(%rbp), %rsi
movl %r15d, (%rsi)
movq (%rdi), %rax
movl $0x4, %edx
callq *0x18(%rax)
testl %eax, %eax
je 0x20d70
movl %eax, %r12d
xorl %eax, %eax
testb %al, %al
je 0x20ae4
jmp 0x20c5f
cmpb $0x4, 0x13(%rbx)
je 0x20c5f
orb $0x4, 0x2c(%r14)
jmp 0x20c5f
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 0x1b642
movl %eax, %r15d
movl 0x28(%r14), %esi
movq %rbx, %rdi
callq 0x1b35e
movl %eax, %r12d
orl %r15d, %r12d
sete %al
jmp 0x20d4f