// File: /workspace/github_c_makefile_star2_O0/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
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movl $0x0, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movl 0xb8(%rax), %eax
movq -0x20(%rbp), %rcx
xorl %edx, %edx
divl 0xbc(%rcx)
movl %eax, -0x24(%rbp)
cmpl $0x1, -0x24(%rbp)
jbe 0xf1c3
movl $0x0, -0x30(%rbp)
movl $0x0, -0x38(%rbp)
movq -0x20(%rbp), %rax
movzbl 0x17(%rax), %ecx
orl $0x4, %ecx
movb %cl, 0x17(%rax)
movq -0x18(%rbp), %rax
movl 0x28(%rax), %eax
subl $0x1, %eax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
xorl $-0x1, %ecx
andl %ecx, %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movq -0x20(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movl 0x28(%rax), %eax
cmpl -0x28(%rbp), %eax
jbe 0xf029
movq -0x18(%rbp), %rax
movl 0x28(%rax), %eax
subl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xf04d
movl -0x2c(%rbp), %eax
addl -0x24(%rbp), %eax
subl $0x1, %eax
cmpl -0x28(%rbp), %eax
jbe 0xf045
movl -0x28(%rbp), %eax
addl $0x1, %eax
subl -0x2c(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0xf04b
movl -0x24(%rbp), %eax
movl %eax, -0x30(%rbp)
jmp 0xf04d
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %ecx
xorl %eax, %eax
cmpl -0x30(%rbp), %ecx
movb %al, -0x51(%rbp)
jge 0xf06b
cmpl $0x0, -0xc(%rbp)
sete %al
movb %al, -0x51(%rbp)
movb -0x51(%rbp), %al
testb $0x1, %al
jne 0xf077
jmp 0xf157
movl -0x2c(%rbp), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x28(%rcx), %eax
je 0xf0a1
movq -0x20(%rbp), %rax
movq 0x40(%rax), %rdi
movl -0x3c(%rbp), %esi
callq 0x219a0
cmpl $0x0, %eax
jne 0xf10f
movl -0x3c(%rbp), %eax
movl %eax, -0x58(%rbp)
movl 0xc1823(%rip), %eax     # 0xd08d0
movq -0x20(%rbp), %rcx
cltd
idivl 0xbc(%rcx)
movl %eax, %ecx
movl -0x58(%rbp), %eax
addl $0x1, %ecx
cmpl %ecx, %eax
je 0xf10d
movq -0x20(%rbp), %rdi
movl -0x3c(%rbp), %esi
leaq -0x48(%rbp), %rdx
xorl %ecx, %ecx
callq 0xe5f0
movl %eax, -0xc(%rbp)
cmpl $0x0, -0xc(%rbp)
jne 0xf10b
movq -0x48(%rbp), %rdi
callq 0x2adb0
movl %eax, -0xc(%rbp)
movq -0x48(%rbp), %rax
movzwl 0x2c(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0xf102
movl $0x1, -0x38(%rbp)
movq -0x48(%rbp), %rdi
callq 0xec30
jmp 0xf10d
jmp 0xf147
movq -0x20(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x28870
movq %rax, -0x48(%rbp)
cmpq $0x0, %rax
je 0xf145
movq -0x48(%rbp), %rax
movzwl 0x2c(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0xf13c
movl $0x1, -0x38(%rbp)
movq -0x48(%rbp), %rdi
callq 0xec30
jmp 0xf147
jmp 0xf149
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xf054
cmpl $0x0, -0xc(%rbp)
jne 0xf1b3
cmpl $0x0, -0x38(%rbp)
je 0xf1b3
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x30(%rbp), %eax
jge 0xf1b1
movq -0x20(%rbp), %rdi
movl -0x2c(%rbp), %esi
addl -0x34(%rbp), %esi
callq 0x28870
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0xf1a4
movq -0x50(%rbp), %rax
movzwl 0x2c(%rax), %ecx
orl $0x4, %ecx
movw %cx, 0x2c(%rax)
movq -0x50(%rbp), %rdi
callq 0xec30
jmp 0xf1a6
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xf16a
jmp 0xf1b3
movq -0x20(%rbp), %rax
movzbl 0x17(%rax), %ecx
andl $-0x5, %ecx
movb %cl, 0x17(%rax)
jmp 0xf1cf
movq -0x8(%rbp), %rdi
callq 0x2adb0
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)