// File: /workspace/github_c_makefile_star2_O1/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), %rbx
movl 0xb8(%rbx), %eax
xorl %edx, %edx
divl 0xbc(%rbx)
cmpl $0x2, %eax
jb 0xdf09
orb $0x4, 0x17(%rbx)
movl 0x28(%rdi), %r15d
leal -0x1(%r15), %ecx
movl %eax, %r14d
negl %r14d
andl %ecx, %r14d
movl 0x1c(%rbx), %ecx
cmpl %ecx, %r15d
jbe 0xdf1c
subl %r14d, %r15d
jmp 0xdf2c
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x20aba
leal (%r14,%rax), %edx
movl %ecx, %r15d
subl %r14d, %r15d
cmpl %ecx, %edx
cmovbel %eax, %r15d
testl %r15d, %r15d
jle 0xe02d
movq %rbx, -0x40(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x1, %ebx
movq %rdi, -0x48(%rbp)
leal (%r14,%rbx), %r13d
cmpl 0x28(%rdi), %r13d
je 0xdfa6
movq -0x40(%rbp), %rax
movq 0x40(%rax), %rdi
movl %r13d, %esi
callq 0x1b2c2
testl %eax, %eax
je 0xdfa6
movq -0x40(%rbp), %rax
movq 0x110(%rax), %rdi
xorl %r12d, %r12d
movl %r13d, %esi
xorl %edx, %edx
leaq -0x38(%rbp), %rcx
callq 0x1f6fc
movq -0x38(%rbp), %rdi
movq %rdi, -0x38(%rbp)
testq %rdi, %rdi
je 0xe004
movzwl 0x2c(%rdi), %eax
testb $0x4, %al
movl -0x2c(%rbp), %eax
movl $0x1, %ecx
cmovnel %ecx, %eax
movl %eax, -0x2c(%rbp)
jmp 0xdfff
movl 0x86894(%rip), %eax     # 0x94840
cltd
movq -0x40(%rbp), %rdi
idivl 0xbc(%rdi)
incl %eax
xorl %r12d, %r12d
cmpl %eax, %r13d
je 0xe004
movl %r13d, %esi
leaq -0x38(%rbp), %rdx
xorl %ecx, %ecx
callq 0xd803
testl %eax, %eax
je 0xdfd8
movl %eax, %r12d
jmp 0xe004
movq -0x38(%rbp), %r13
movq %r13, %rdi
callq 0x20aba
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 0xdcd4
cmpl %r15d, %ebx
movq -0x48(%rbp), %rdi
jge 0xe018
incl %ebx
testl %r12d, %r12d
je 0xdf49
testl %r12d, %r12d
sete %cl
cmpl $0x0, -0x2c(%rbp)
setne %al
andb %cl, %al
movq -0x40(%rbp), %rbx
jmp 0xe032
xorl %r12d, %r12d
xorl %eax, %eax
testb %al, %al
je 0xe070
testl %r15d, %r15d
jle 0xe070
incl %r14d
leaq -0x38(%rbp), %r13
movq 0x110(%rbx), %rdi
movl %r14d, %esi
xorl %edx, %edx
movq %r13, %rcx
callq 0x1f6fc
movq -0x38(%rbp), %rdi
testq %rdi, %rdi
je 0xe068
orb $0x4, 0x2c(%rdi)
callq 0xdcd4
incl %r14d
decl %r15d
jne 0xe042
andb $-0x5, 0x17(%rbx)
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq