// File: /workspace/github_c_makefile_star2_O2/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3PagerSharedLock
SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
  int rc = SQLITE_OK;                /* Return code */

  /* This routine is only called from b-tree and only when there are no
  ** outstanding pages. This implies that the pager state should either
  ** be OPEN or READER. READER is only possible if the pager is or was in 
  ** exclusive access mode.
  */
  assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
  assert( assert_pager_state(pPager) );
  assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
  if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }

  if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
    int bHotJournal = 1;          /* True if there exists a hot journal-file */

    assert( !MEMDB );

    rc = pager_wait_on_lock(pPager, SHARED_LOCK);
    if( rc!=SQLITE_OK ){
      assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
      goto failed;
    }

    /* If a journal file exists, and there is no RESERVED lock on the
    ** database file, then it either needs to be played back or deleted.
    */
    if( pPager->eLock<=SHARED_LOCK ){
      rc = hasHotJournal(pPager, &bHotJournal);
    }
    if( rc!=SQLITE_OK ){
      goto failed;
    }
    if( bHotJournal ){
      if( pPager->readOnly ){
        rc = SQLITE_READONLY_ROLLBACK;
        goto failed;
      }

      /* Get an EXCLUSIVE lock on the database file. At this point it is
      ** important that a RESERVED lock is not obtained on the way to the
      ** EXCLUSIVE lock. If it were, another process might open the
      ** database file, detect the RESERVED lock, and conclude that the
      ** database is safe to read while this process is still rolling the 
      ** hot-journal back.
      ** 
      ** Because the intermediate RESERVED lock is not requested, any
      ** other process attempting to access the database file will get to 
      ** this point in the code and fail to obtain its own EXCLUSIVE lock 
      ** on the database file.
      **
      ** Unless the pager is in locking_mode=exclusive mode, the lock is
      ** downgraded to SHARED_LOCK before this function returns.
      */
      rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
      if( rc!=SQLITE_OK ){
        goto failed;
      }
 
      /* If it is not already open and the file exists on disk, open the 
      ** journal for read/write access. Write access is required because 
      ** in exclusive-access mode the file descriptor will be kept open 
      ** and possibly used for a transaction later on. Also, write-access 
      ** is usually required to finalize the journal in journal_mode=persist 
      ** mode (and also for journal_mode=truncate on some systems).
      **
      ** If the journal does not exist, it usually means that some 
      ** other connection managed to get in and roll it back before 
      ** this connection obtained the exclusive lock above. Or, it 
      ** may mean that the pager was in the error-state when this
      ** function was called and the journal file does not exist.
      */
      if( !isOpen(pPager->jfd) ){
        sqlite3_vfs * const pVfs = pPager->pVfs;
        int bExists;              /* True if journal file exists */
        rc = sqlite3OsAccess(
            pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
        if( rc==SQLITE_OK && bExists ){
          int fout = 0;
          int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
          assert( !pPager->tempFile );
          rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
          assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
          if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
            rc = SQLITE_CANTOPEN_BKPT;
            sqlite3OsClose(pPager->jfd);
          }
        }
      }
 
      /* Playback and delete the journal.  Drop the database write
      ** lock and reacquire the read lock. Purge the cache before
      ** playing back the hot-journal so that we don't end up with
      ** an inconsistent cache.  Sync the hot journal before playing
      ** it back since the process that crashed and left the hot journal
      ** probably did not sync it and we are required to always sync
      ** the journal before playing it back.
      */
      if( isOpen(pPager->jfd) ){
        assert( rc==SQLITE_OK );
        rc = pagerSyncHotJournal(pPager);
        if( rc==SQLITE_OK ){
          rc = pager_playback(pPager, 1);
          pPager->eState = PAGER_OPEN;
        }
      }else if( !pPager->exclusiveMode ){
        pagerUnlockDb(pPager, SHARED_LOCK);
      }

      if( rc!=SQLITE_OK ){
        /* This branch is taken if an error occurs while trying to open
        ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
        ** pager_unlock() routine will be called before returning to unlock
        ** the file. If the unlock attempt fails, then Pager.eLock must be
        ** set to UNKNOWN_LOCK (see the comment above the #define for 
        ** UNKNOWN_LOCK above for an explanation). 
        **
        ** In order to get pager_unlock() to do this, set Pager.eState to
        ** PAGER_ERROR now. This is not actually counted as a transition
        ** to ERROR state in the state diagram at the top of this file,
        ** since we know that the same call to pager_unlock() will very
        ** shortly transition the pager object to the OPEN state. Calling
        ** assert_pager_state() would fail now, as it should not be possible
        ** to be in ERROR state when there are zero outstanding page 
        ** references.
        */
        pager_error(pPager, rc);
        goto failed;
      }

      assert( pPager->eState==PAGER_OPEN );
      assert( (pPager->eLock==SHARED_LOCK)
           || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
      );
    }

    if( !pPager->tempFile && (
        pPager->pBackup 
     || sqlite3PcachePagecount(pPager->pPCache)>0 
     || USEFETCH(pPager)
    )){
      /* The shared-lock has just been acquired on the database file
      ** and there are already pages in the cache (from a previous
      ** read or write transaction).  Check to see if the database
      ** has been modified.  If the database has changed, flush the
      ** cache.
      **
      ** Database changes is detected by looking at 15 bytes beginning
      ** at offset 24 into the file.  The first 4 of these 16 bytes are
      ** a 32-bit counter that is incremented with each change.  The
      ** other bytes change randomly with each file change when
      ** a codec is in use.
      ** 
      ** There is a vanishingly small chance that a change will not be 
      ** detected.  The chance of an undetected change is so small that
      ** it can be neglected.
      */
      Pgno nPage = 0;
      char dbFileVers[sizeof(pPager->dbFileVers)];

      rc = pagerPagecount(pPager, &nPage);
      if( rc ) goto failed;

      if( nPage>0 ){
        IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
        rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
        if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
          goto failed;
        }
      }else{
        memset(dbFileVers, 0, sizeof(dbFileVers));
      }

      if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
        pager_reset(pPager);

        /* Unmap the database file. It is possible that external processes
        ** may have truncated the database file and then extended it back
        ** to its original size while this process was not holding a lock.
        ** In this case there may exist a Pager.pMap mapping that appears
        ** to be the right size but is not actually valid. Avoid this
        ** possibility by unmapping the db here. */
        if( USEFETCH(pPager) ){
          sqlite3OsUnfetch(pPager->fd, 0, 0);
        }
      }
    }

    /* If there is a WAL file in the file-system, open this database in WAL
    ** mode. Otherwise, the following function call is a no-op.
    */
    rc = pagerOpenWalIfPresent(pPager);
#ifndef SQLITE_OMIT_WAL
    assert( pPager->pWal==0 || rc==SQLITE_OK );
#endif
  }

  if( pagerUseWal(pPager) ){
    assert( rc==SQLITE_OK );
    rc = pagerBeginReadTransaction(pPager);
  }

  if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
    rc = pagerPagecount(pPager, &pPager->dbSize);
  }

 failed:
  if( rc!=SQLITE_OK ){
    assert( !MEMDB );
    pager_unlock(pPager);
    assert( pPager->eState==PAGER_OPEN );
  }else{
    pPager->eState = PAGER_READER;
  }
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
cmpb $0x0, 0x12(%rdi)
je 0x1d377
movl 0x2c(%rbx), %r15d
testl %r15d, %r15d
jne 0x1d42a
movq 0x118(%rbx), %rdi
testq %rdi, %rdi
je 0x1d3e2
leaq -0x40(%rbp), %r15
andl $0x0, (%r15)
callq 0x1b618
movq 0x118(%rbx), %r12
pushq $0x1
popq %r13
movq %r12, %rdi
movq %r15, %rsi
xorl %edx, %edx
movl %r13d, %ecx
callq 0x19682
incl %r13d
cmpl $-0x1, %eax
je 0x1d39b
movl %eax, %r14d
movl -0x40(%rbp), %eax
orl %r14d, %eax
je 0x1d3f7
movq %rbx, %rdi
callq 0x1a6c5
cmpb $0x0, 0x94(%rbx)
je 0x1d3f7
movq 0x48(%rbx), %rdi
movq (%rdi), %rax
xorl %esi, %esi
xorl %edx, %edx
callq *0x90(%rax)
jmp 0x1d3f7
xorl %r14d, %r14d
cmpb $0x0, 0x13(%rbx)
je 0x1d43c
movq 0x118(%rbx), %rdi
testq %rdi, %rdi
jne 0x1d383
cmpb $0x0, 0x13(%rbx)
jne 0x1d411
testl %r14d, %r14d
jne 0x1d411
leaq 0x1c(%rbx), %rsi
movq %rbx, %rdi
callq 0x1d947
movl %eax, %r14d
movl %r14d, %r15d
testl %r14d, %r14d
je 0x1d423
movq %rbx, %rdi
callq 0x1a9ff
jmp 0x1d42a
movb $0x1, 0x13(%rbx)
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x1a3a7
testl %eax, %eax
je 0x1d450
movl %eax, %r15d
jmp 0x1d419
cmpb $0x1, 0x14(%rbx)
ja 0x1d644
movq (%rbx), %r13
movl $0x1, -0x40(%rbp)
movq 0x50(%rbx), %rax
movq (%rax), %r12
testq %r12, %r12
jne 0x1d491
movq 0xd8(%rbx), %rsi
leaq -0x40(%rbp), %rcx
movq %r13, %rdi
xorl %edx, %edx
callq *0x38(%r13)
movl %eax, %r15d
testl %eax, %eax
jne 0x1d4da
cmpl $0x0, -0x40(%rbp)
je 0x1d4da
movq %r13, -0x50(%rbp)
leaq -0x2c(%rbp), %r13
andl $0x0, (%r13)
movq 0x48(%rbx), %rdi
movq (%rdi), %rax
movq %r13, %rsi
callq *0x48(%rax)
movl %eax, %r15d
movl (%r13), %eax
orl %r15d, %eax
je 0x1d4bf
movb $0x1, %r13b
jmp 0x1d5d1
leaq -0x48(%rbp), %rsi
movq %rbx, %rdi
callq 0x1d947
movb $0x1, %r13b
testl %eax, %eax
je 0x1d4e8
movl %eax, %r15d
jmp 0x1d5d1
testl %r15d, %r15d
jne 0x1d419
jmp 0x1d5df
cmpl $0x0, -0x48(%rbp)
je 0x1d546
testq %r12, %r12
je 0x1d597
leaq -0x44(%rbp), %rsi
movb $0x0, (%rsi)
movq 0x50(%rbx), %rdi
movq (%rdi), %rax
xorl %r13d, %r13d
pushq $0x1
popq %rdx
xorl %ecx, %ecx
callq *0x10(%rax)
movl %eax, %r15d
cmpl $0x20a, %eax            # imm = 0x20A
cmovel %r13d, %r15d
testq %r12, %r12
jne 0x1d539
movq 0x50(%rbx), %r12
movq (%r12), %rax
testq %rax, %rax
je 0x1d539
movq %r12, %rdi
callq *0x8(%rax)
andq $0x0, (%r12)
cmpb $0x0, -0x44(%rbp)
sete %r13b
jmp 0x1d5d1
movq 0x6ba13(%rip), %rax     # 0x88f60
testq %rax, %rax
je 0x1d554
callq *%rax
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x1a3e8
testl %eax, %eax
jne 0x1d584
movq 0xd8(%rbx), %rsi
movq -0x50(%rbp), %rdi
xorl %edx, %edx
callq *0x30(%rdi)
cmpb $0x0, 0x8(%rbx)
jne 0x1d584
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x1b646
movq 0x6b9dd(%rip), %rax     # 0x88f68
xorl %r15d, %r15d
testq %rax, %rax
je 0x1d5d1
callq *%rax
jmp 0x1d5d1
leaq -0x44(%rbp), %r8
movl $0x801, (%r8)           # imm = 0x801
movq 0x50(%rbx), %rdx
movq 0xd8(%rbx), %rsi
movq -0x50(%rbp), %rdi
movl $0x801, %ecx            # imm = 0x801
callq *0x28(%rdi)
cmpl $0xe, %eax
je 0x1d5cb
movl %eax, %r15d
testl %eax, %eax
je 0x1d4f7
jmp 0x1d5d1
xorl %r13d, %r13d
xorl %r15d, %r15d
testl %r15d, %r15d
jne 0x1d419
testb %r13b, %r13b
je 0x1d644
cmpb $0x0, 0x10(%rbx)
jne 0x1d3eb
cmpq $0x0, 0x70(%rbx)
je 0x1d71d
leaq -0x2c(%rbp), %rsi
andl $0x0, (%rsi)
movq %rbx, %rdi
callq 0x1d947
testl %eax, %eax
jne 0x1d44b
cmpl $0x0, -0x2c(%rbp)
je 0x1d784
movq 0x48(%rbx), %rdi
movq (%rdi), %rax
leaq -0x40(%rbp), %rsi
pushq $0x10
popq %rdx
pushq $0x18
popq %rcx
callq *0x10(%rax)
testl %eax, %eax
je 0x1d78b
movl %eax, %r15d
cmpl $0x20a, %eax            # imm = 0x20A
je 0x1d78b
jmp 0x1d419
movl $0x308, %r15d           # imm = 0x308
cmpb $0x0, 0x11(%rbx)
jne 0x1d419
pushq $0x4
popq %rsi
movq %rbx, %rdi
callq 0x1a3e8
testl %eax, %eax
jne 0x1d44b
movq 0x50(%rbx), %rax
cmpq $0x0, (%rax)
jne 0x1d6fa
movq (%rbx), %r12
movq 0xd8(%rbx), %rsi
leaq -0x40(%rbp), %rcx
movq %r12, %rdi
xorl %edx, %edx
callq *0x38(%r12)
movl %eax, %r15d
movq 0x50(%rbx), %rdx
testl %eax, %eax
jne 0x1d6f4
cmpl $0x0, -0x40(%rbp)
je 0x1d6f4
leaq -0x2c(%rbp), %r8
andl $0x0, (%r8)
movq 0xd8(%rbx), %rsi
movq %r12, %rdi
movl $0x802, %ecx            # imm = 0x802
callq *0x28(%r12)
movl %eax, %r15d
testl %eax, %eax
jne 0x1d6f0
xorl %r15d, %r15d
testb $0x1, -0x2c(%rbp)
je 0x1d6f0
movl $0xa967, %edi           # imm = 0xA967
callq 0x16cb0
movq 0x50(%rbx), %r12
movq (%r12), %rax
pushq $0xe
popq %r15
testq %rax, %rax
je 0x1d6f0
movq %r12, %rdi
callq *0x8(%rax)
andq $0x0, (%r12)
movq 0x50(%rbx), %rdx
cmpq $0x0, (%rdx)
je 0x1d74a
movq %rbx, %rdi
callq 0x1aad7
movl %eax, %r15d
testl %eax, %eax
jne 0x1d764
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x1bdb6
movl %eax, %r15d
movb $0x0, 0x13(%rbx)
jmp 0x1d75b
movq 0x110(%rbx), %rax
movq 0x40(%rax), %rdi
testq %rdi, %rdi
je 0x1d73b
callq *0x69da5(%rip)          # 0x874d8
testl %eax, %eax
jg 0x1d5f4
cmpb $0x0, 0x94(%rbx)
jne 0x1d5f4
jmp 0x1d7c5
cmpb $0x0, 0x8(%rbx)
jne 0x1d75b
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x1b646
testl %r15d, %r15d
je 0x1d5df
cmpb $0xd, %r15b
je 0x1d777
movzbl %r15b, %eax
cmpl $0xa, %eax
jne 0x1d419
movl %r15d, 0x2c(%rbx)
movb $0x6, 0x13(%rbx)
jmp 0x1d419
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
movdqu 0x84(%rbx), %xmm0
pcmpeqb -0x40(%rbp), %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x1d7c5
movq %rbx, %rdi
callq 0x1a6c5
cmpb $0x0, 0x94(%rbx)
je 0x1d7c5
movq 0x48(%rbx), %rdi
movq (%rdi), %rax
xorl %esi, %esi
xorl %edx, %edx
callq *0x90(%rax)
cmpb $0x0, 0x10(%rbx)
jne 0x1d3eb
leaq -0x2c(%rbp), %rsi
movq %rbx, %rdi
callq 0x1d947
movl %eax, %r14d
testl %eax, %eax
jne 0x1d3eb
cmpl $0x0, -0x2c(%rbp)
movq (%rbx), %rdi
movq 0x120(%rbx), %rsi
je 0x1d804
leaq -0x40(%rbp), %rcx
xorl %edx, %edx
callq *0x38(%rdi)
movl %eax, %r14d
jmp 0x1d817
xorl %edx, %edx
callq *0x30(%rdi)
movl %eax, %r14d
andl $0x0, -0x40(%rbp)
cmpl $0x170a, %eax           # imm = 0x170A
je 0x1d838
testl %r14d, %r14d
jne 0x1d3eb
cmpl $0x0, -0x40(%rbp)
je 0x1d838
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1d8aa
movl %eax, %r14d
jmp 0x1d3eb
xorl %r14d, %r14d
cmpb $0x5, 0x9(%rbx)
jne 0x1d3eb
movb $0x0, 0x9(%rbx)
jmp 0x1d3eb