// File: /workspace/github_c_makefile_star2_O3/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:
movq 0x20(%rdi), %rsi
movl 0xb8(%rsi), %eax
xorl %edx, %edx
divl 0xbc(%rsi)
cmpl $0x2, %eax
jb 0x207be
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
orb $0x4, 0x17(%rsi)
movl 0x28(%rdi), %r14d
leal -0x1(%r14), %ecx
movl %eax, %r8d
negl %r8d
andl %ecx, %r8d
movl 0x1c(%rsi), %ecx
cmpl %ecx, %r14d
jbe 0xe00d
subl %r8d, %r14d
jmp 0xe01d
leal (%r8,%rax), %edx
movl %ecx, %r14d
subl %r8d, %r14d
cmpl %ecx, %edx
cmovbel %eax, %r14d
testl %r14d, %r14d
jle 0xe15b
movq %rdi, -0x50(%rbp)
leal 0x1(%r8), %r15d
movl $0x0, -0x2c(%rbp)
movl $0x1, %ebx
movq %rsi, -0x38(%rbp)
movq %r8, -0x40(%rbp)
leal (%r8,%rbx), %r13d
movq -0x50(%rbp), %rax
cmpl 0x28(%rax), %r13d
je 0xe09f
movq -0x38(%rbp), %rax
movq 0x40(%rax), %rdi
movl %r13d, %esi
callq 0x1b0b1
movq -0x38(%rbp), %rsi
testl %eax, %eax
je 0xe09f
movq 0x110(%rsi), %rdi
xorl %r12d, %r12d
movl %r13d, %esi
xorl %edx, %edx
leaq -0x48(%rbp), %rcx
callq 0x1f4e2
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0xe0fb
movzwl 0x2c(%rdi), %eax
testb $0x4, %al
movl -0x2c(%rbp), %eax
movl $0x1, %ecx
cmovnel %ecx, %eax
movl %eax, -0x2c(%rbp)
jmp 0xe0f6
movl 0x8679b(%rip), %eax     # 0x94840
cltd
idivl 0xbc(%rsi)
incl %eax
xorl %r12d, %r12d
cmpl %eax, %r13d
je 0xe0ff
movq %rsi, %rdi
movl %r13d, %esi
leaq -0x58(%rbp), %rdx
xorl %ecx, %ecx
callq 0xd8e2
testl %eax, %eax
jne 0xe160
movq -0x58(%rbp), %r13
movq %r13, %rdi
callq 0x207be
movl %eax, %r12d
movzwl 0x2c(%r13), %eax
testb $0x4, %al
movl -0x2c(%rbp), %eax
movl $0x1, %ecx
cmovnel %ecx, %eax
movl %eax, -0x2c(%rbp)
movq %r13, %rdi
callq 0xddd3
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %r8
cmpl %r14d, %ebx
jge 0xe113
incl %ebx
testl %r12d, %r12d
je 0xe042
testl %r12d, %r12d
jne 0xe167
cmpl $0x0, -0x2c(%rbp)
je 0xe167
testl %r14d, %r14d
jle 0xe167
leaq -0x48(%rbp), %r13
movq 0x110(%rsi), %rdi
movl %r15d, %esi
xorl %edx, %edx
movq %r13, %rcx
callq 0x1f4e2
movq -0x48(%rbp), %rdi
testq %rdi, %rdi
je 0xe14d
orb $0x4, 0x2c(%rdi)
callq 0xddd3
incl %r15d
decl %r14d
movq -0x38(%rbp), %rsi
jne 0xe127
jmp 0xe167
xorl %r12d, %r12d
jmp 0xe167
movl %eax, %r12d
movq -0x38(%rbp), %rsi
andb $-0x5, 0x17(%rsi)
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq