// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3PagerWrite
SQLITE_PRIVATE int sqlite3PagerWrite(DbPage *pDbPage){
  int rc = SQLITE_OK;

  PgHdr *pPg = pDbPage;
  Pager *pPager = pPg->pPager;
  Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);

  assert( (pPg->flags & PGHDR_MMAP)==0 );
  assert( pPager->eState>=PAGER_WRITER_LOCKED );
  assert( pPager->eState!=PAGER_ERROR );
  assert( assert_pager_state(pPager) );

  if( nPagePerSector>1 ){
    Pgno nPageCount;          /* Total number of pages in database file */
    Pgno pg1;                 /* First page of the sector pPg is located on. */
    int nPage = 0;            /* Number of pages starting at pg1 to journal */
    int ii;                   /* Loop counter */
    int needSync = 0;         /* True if any page has PGHDR_NEED_SYNC */

    /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
    ** a journal header to be written between the pages journaled by
    ** this function.
    */
    assert( !MEMDB );
    assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
    pPager->doNotSpill |= SPILLFLAG_NOSYNC;

    /* This trick assumes that both the page-size and sector-size are
    ** an integer power of 2. It sets variable pg1 to the identifier
    ** of the first page of the sector pPg is located on.
    */
    pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;

    nPageCount = pPager->dbSize;
    if( pPg->pgno>nPageCount ){
      nPage = (pPg->pgno - pg1)+1;
    }else if( (pg1+nPagePerSector-1)>nPageCount ){
      nPage = nPageCount+1-pg1;
    }else{
      nPage = nPagePerSector;
    }
    assert(nPage>0);
    assert(pg1<=pPg->pgno);
    assert((pg1+nPage)>pPg->pgno);

    for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
      Pgno pg = pg1+ii;
      PgHdr *pPage;
      if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
        if( pg!=PAGER_MJ_PGNO(pPager) ){
          rc = sqlite3PagerGet(pPager, pg, &pPage);
          if( rc==SQLITE_OK ){
            rc = pager_write(pPage);
            if( pPage->flags&PGHDR_NEED_SYNC ){
              needSync = 1;
            }
            sqlite3PagerUnref(pPage);
          }
        }
      }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
        if( pPage->flags&PGHDR_NEED_SYNC ){
          needSync = 1;
        }
        sqlite3PagerUnref(pPage);
      }
    }

    /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages 
    ** starting at pg1, then it needs to be set for all of them. Because
    ** writing to any of these nPage pages may damage the others, the
    ** journal file must contain sync()ed copies of all of them
    ** before any of them can be written out to the database file.
    */
    if( rc==SQLITE_OK && needSync ){
      assert( !MEMDB );
      for(ii=0; ii<nPage; ii++){
        PgHdr *pPage = pager_lookup(pPager, pg1+ii);
        if( pPage ){
          pPage->flags |= PGHDR_NEED_SYNC;
          sqlite3PagerUnref(pPage);
        }
      }
    }

    assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
    pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
  }else{
    rc = pager_write(pDbPage);
  }
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq 0x20(%rdi), %rsi
movl 0xb8(%rsi), %eax
xorl %edx, %edx
divl 0xbc(%rsi)
cmpl $0x2, %eax
jb 0xd156
orb $0x4, 0x17(%rsi)
movq %rdi, -0x40(%rbp)
movl 0x28(%rdi), %ebx
leal -0x1(%rbx), %ecx
movl %eax, %r14d
negl %r14d
andl %ecx, %r14d
movl 0x1c(%rsi), %ecx
cmpl %ecx, %ebx
jbe 0xd169
subl %r14d, %ebx
jmp 0xd177
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1dc59
leal (%r14,%rax), %edx
movl %ecx, %ebx
subl %r14d, %ebx
cmpl %ecx, %edx
cmovbel %eax, %ebx
incl %r14d
xorl %r15d, %r15d
movl $0x0, -0x2c(%rbp)
xorl %r13d, %r13d
movq %rsi, -0x38(%rbp)
cmpl %ebx, %r15d
jge 0xd252
testl %r13d, %r13d
jne 0xd252
leal (%r14,%r15), %r12d
movq -0x40(%rbp), %rax
cmpl 0x28(%rax), %r12d
je 0xd1ea
movq 0x40(%rsi), %rdi
movl %r12d, %esi
callq 0x18dd4
movq -0x38(%rbp), %rsi
testl %eax, %eax
je 0xd1ea
movq %rsi, %rdi
movl %r12d, %esi
callq 0x1cdfb
xorl %r13d, %r13d
testq %rax, %rax
je 0xd246
movzwl 0x2c(%rax), %ecx
testb $0x4, %cl
movl -0x2c(%rbp), %ecx
pushq $0x1
popq %rdx
cmovnel %edx, %ecx
movl %ecx, -0x2c(%rbp)
movq %rax, %rdi
jmp 0xd241
movl 0x7a650(%rip), %eax     # 0x87840
cltd
idivl 0xbc(%rsi)
incl %eax
xorl %r13d, %r13d
cmpl %eax, %r12d
je 0xd24a
movq %rsi, %rdi
movl %r12d, %esi
leaq -0x48(%rbp), %rdx
xorl %ecx, %ecx
callq 0xca7d
testl %eax, %eax
je 0xd21b
movl %eax, %r13d
jmp 0xd246
movq -0x48(%rbp), %r12
movq %r12, %rdi
callq 0x1dc59
movl %eax, %r13d
movzwl 0x2c(%r12), %eax
testb $0x4, %al
movl -0x2c(%rbp), %eax
pushq $0x1
popq %rcx
cmovnel %ecx, %eax
movl %eax, -0x2c(%rbp)
movq %r12, %rdi
callq 0xcf6e
movq -0x38(%rbp), %rsi
incl %r15d
jmp 0xd18b
testl %r13d, %r13d
jne 0xd291
cmpl $0x0, -0x2c(%rbp)
je 0xd291
xorl %r15d, %r15d
testl %ebx, %ebx
cmovgl %ebx, %r15d
subl $0x1, %r15d
jb 0xd291
movq %rsi, %rdi
movl %r14d, %esi
callq 0x1cdfb
testq %rax, %rax
je 0xd288
orb $0x4, 0x2c(%rax)
movq %rax, %rdi
callq 0xcf6e
incl %r14d
movq -0x38(%rbp), %rsi
jmp 0xd266
andb $-0x5, 0x17(%rsi)
movl %r13d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq