// File: /workspace/github_c_makefile_star2_O0/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
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x2c(%rax)
je 0x2adf4
movq -0x20(%rbp), %rax
movl 0x2c(%rax), %eax
movl %eax, -0x4(%rbp)
jmp 0x2b040
movq -0x20(%rbp), %rax
cmpb $0x0, 0x11(%rax)
je 0x2ae0a
movl $0x3, -0x4(%rbp)
jmp 0x2b040
movq -0x20(%rbp), %rax
movzbl 0x13(%rax), %eax
cmpl $0x2, %eax
jne 0x2ae36
movq -0x20(%rbp), %rdi
callq 0x2b050
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x2ae34
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2b040
jmp 0x2ae36
movq -0x10(%rbp), %rdi
callq 0x288b0
movq -0x10(%rbp), %rdi
callq 0x2b1c0
cmpl $0x0, %eax
je 0x2ae60
movq -0x10(%rbp), %rdi
callq 0x211c0
cmpl $0x0, %eax
jne 0x2ae60
jmp 0x2b01c
movq -0x10(%rbp), %rdi
callq 0x2b1c0
cmpl $0x0, %eax
jne 0x2b000
movq -0x20(%rbp), %rdi
callq 0x211a0
cmpl $0x0, %eax
jne 0x2b000
movq -0x10(%rbp), %rax
movl 0x28(%rax), %eax
movq -0x20(%rbp), %rcx
cmpl 0x20(%rcx), %eax
ja 0x2afe0
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rax
cmpq $0x0, (%rax)
je 0x2afe0
movq -0x20(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x28810
movl %eax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movzwl 0x2c(%rax), %ecx
orl $0x4, %ecx
movw %cx, 0x2c(%rax)
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x10(%rbp), %rax
movl 0x28(%rax), %edx
callq 0x21b50
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x2af09
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2b040
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x20(%rbp), %rax
movl 0xbc(%rax), %edx
movq -0x38(%rbp), %rcx
addq $0x4, %rcx
callq 0xf430
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x2af40
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2b040
movq -0x20(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x20(%rbp), %rax
movslq 0xbc(%rax), %rax
addq %rax, %rsi
addq $0x4, %rsi
movl -0x28(%rbp), %edx
callq 0x21b50
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x2af7a
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2b040
movq -0x20(%rbp), %rax
movl 0xbc(%rax), %eax
addl $0x8, %eax
movslq %eax, %rcx
movq -0x20(%rbp), %rax
addq 0x60(%rax), %rcx
movq %rcx, 0x60(%rax)
movq -0x20(%rbp), %rax
movl 0x30(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x30(%rax)
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x28(%rax), %esi
callq 0x220b0
movl %eax, -0x24(%rbp)
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x28(%rax), %esi
callq 0x21b90
orl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
je 0x2afde
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x2b040
jmp 0x2affe
movq -0x20(%rbp), %rax
movzbl 0x13(%rax), %eax
cmpl $0x4, %eax
je 0x2affc
movq -0x10(%rbp), %rax
movzwl 0x2c(%rax), %ecx
orl $0x4, %ecx
movw %cx, 0x2c(%rax)
jmp 0x2affe
jmp 0x2b000
movq -0x10(%rbp), %rdi
callq 0x211c0
cmpl $0x0, %eax
je 0x2b01a
movq -0x10(%rbp), %rdi
callq 0x21270
movl %eax, -0x24(%rbp)
jmp 0x2b01c
movq -0x20(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jae 0x2b03a
movq -0x10(%rbp), %rax
movl 0x28(%rax), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x1c(%rax)
movl -0x24(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)