// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/external/sqlite-3.33.0/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 );
  assert( pPager->errCode==SQLITE_OK );

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

    assert( !MEMDB );
    assert( pPager->tempFile==0 || pPager->eLock==EXCLUSIVE_LOCK );

    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, !pPager->tempFile);
          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->hasHeldSharedLock ){
      /* The shared-lock has just been acquired then check to
      ** see if the database has been modified.  If the database has changed,
      ** flush the cache.  The hasHeldSharedLock flag prevents this from
      ** occurring on the very first access to a file, in order to save a
      ** single unnecessary sqlite3OsRead() call at the start-up.
      **
      ** Database changes are 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.
      */
      char dbFileVers[sizeof(pPager->dbFileVers)];

      IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
      rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
      if( rc!=SQLITE_OK ){
        if( rc!=SQLITE_IOERR_SHORT_READ ){
          goto failed;
        }
        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->tempFile==0 && 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;
    pPager->hasHeldSharedLock = 1;
  }
  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
xorl %ebp, %ebp
cmpq $0x0, 0x120(%rdi)
jne 0x2966d3
cmpb $0x0, 0x14(%rbx)
je 0x29677e
movq 0x120(%rbx), %rdi
testq %rdi, %rdi
je 0x29673e
leaq 0x10(%rsp), %r14
movl $0x0, (%r14)
callq 0x2925b7
movq 0x120(%rbx), %r15
movl $0x1, %r12d
movq %r15, %rdi
movq %r14, %rsi
xorl %edx, %edx
movl %r12d, %ecx
callq 0x29136b
incl %r12d
cmpl $-0x1, %eax
je 0x2966fd
movl %eax, %ebp
movl 0x10(%rsp), %eax
orl %ebp, %eax
je 0x29673e
movq %rbx, %rdi
callq 0x294c73
cmpb $0x0, 0x1a(%rbx)
je 0x29673e
movq 0x48(%rbx), %rdi
movq (%rdi), %rax
xorl %esi, %esi
xorl %edx, %edx
callq *0x90(%rax)
cmpb $0x0, 0x10(%rbx)
jne 0x296b80
cmpb $0x0, 0x14(%rbx)
jne 0x296b80
testl %ebp, %ebp
jne 0x296b80
leaq 0x1c(%rbx), %rsi
movq %rbx, %rdi
callq 0x296d2d
movl %eax, %ebp
jmp 0x296b80
movq 0xe8(%rbx), %rdi
callq *0xe0(%rbx)
testl %eax, %eax
je 0x2967a1
movq %rbx, %rdi
movl $0x1, %esi
callq 0x292a65
movl %eax, %ebp
cmpl $0x5, %eax
je 0x29676d
testl %ebp, %ebp
je 0x2967af
movl $0x2, %r15d
jmp 0x296b6f
movl $0x2, %r15d
testl %ebp, %ebp
jne 0x296b6f
xorl %r12d, %r12d
cmpb $0x1, 0x15(%rbx)
movl $0x0, %ebp
ja 0x29683c
movq (%rbx), %r14
movl $0x1, 0x10(%rsp)
movq 0x50(%rbx), %rax
movq (%rax), %r13
xorl %ebp, %ebp
testq %r13, %r13
jne 0x2967ed
movq 0xd8(%rbx), %rsi
leaq 0x10(%rsp), %rcx
movq %r14, %rdi
xorl %edx, %edx
callq *0x38(%r14)
movl %eax, %ebp
movb $0x1, %r12b
testl %ebp, %ebp
jne 0x29683c
cmpl $0x0, 0x10(%rsp)
je 0x29683c
leaq 0xc(%rsp), %r15
movl $0x0, (%r15)
movq 0x48(%rbx), %rdi
movq (%rdi), %rax
movq %r15, %rsi
callq *0x48(%rax)
movl %eax, %ebp
movl (%r15), %eax
orl %ebp, %eax
je 0x296822
movb $0x1, %r12b
jmp 0x29683c
leaq 0x24(%rsp), %rsi
movq %rbx, %rdi
callq 0x296d2d
movb $0x1, %r12b
testl %eax, %eax
je 0x296919
movl %eax, %ebp
movl $0x2, %r15d
testl %ebp, %ebp
jne 0x296b6f
testb %r12b, %r12b
je 0x296867
cmpb $0x0, 0x10(%rbx)
je 0x296892
movq %rbx, %rdi
callq 0x296cb4
movl %eax, %ebp
xorl %r15d, %r15d
jmp 0x296b6f
movl $0x308, %ebp            # imm = 0x308
cmpb $0x0, 0x12(%rbx)
jne 0x296b6f
movq %rbx, %rdi
movl $0x4, %esi
callq 0x292a65
testl %eax, %eax
je 0x296988
movl %eax, %ebp
jmp 0x296b6f
cmpb $0x0, 0x1b(%rbx)
je 0x296855
movq 0x48(%rbx), %rdi
movq (%rdi), %rax
leaq 0x10(%rsp), %rsi
movl $0x18, %ecx
movl $0x10, %edx
callq *0x10(%rax)
movl %eax, %ebp
xorl %r15d, %r15d
testl %eax, %eax
je 0x2968ce
cmpl $0x20a, %ebp            # imm = 0x20A
jne 0x296a65
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movdqu 0x88(%rbx), %xmm0
pcmpeqb 0x10(%rsp), %xmm0
pmovmskb %xmm0, %eax
movb $0x1, %r14b
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x296a6e
movq %rbx, %rdi
callq 0x294c73
cmpb $0x0, 0x1a(%rbx)
je 0x296a6e
movq 0x48(%rbx), %rdi
movq (%rdi), %rax
xorl %r15d, %r15d
xorl %esi, %esi
xorl %edx, %edx
callq *0x90(%rax)
jmp 0x296a6e
testq %r13, %r13
jne 0x2969d5
cmpl $0x0, 0x24(%rsp)
jne 0x2969d5
movq 0x233514(%rip), %rax    # 0x4c9e48
testq %rax, %rax
je 0x29693b
callq *%rax
movq %rbx, %rdi
movl $0x2, %esi
callq 0x292a65
testl %eax, %eax
jne 0x29696f
movq 0xd8(%rbx), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq *0x30(%r14)
cmpb $0x0, 0x8(%rbx)
jne 0x29696f
movq %rbx, %rdi
movl $0x1, %esi
callq 0x294e1e
movq 0x2334da(%rip), %rax    # 0x4c9e50
xorl %ebp, %ebp
testq %rax, %rax
je 0x29683c
callq *%rax
jmp 0x29683c
movq 0x50(%rbx), %rax
xorl %ebp, %ebp
cmpq $0x0, (%rax)
je 0x296a7c
movq 0x50(%rbx), %rax
cmpq $0x0, (%rax)
je 0x296b30
movq %rbx, %rdi
callq 0x2957a1
movl %eax, %ebp
testl %eax, %eax
jne 0x296b43
xorl %esi, %esi
cmpb $0x0, 0x10(%rbx)
sete %sil
movq %rbx, %rdi
callq 0x293c77
movl %eax, %ebp
movb $0x0, 0x14(%rbx)
jmp 0x296b43
xorl %ebp, %ebp
testq %r13, %r13
jne 0x296a01
leaq 0x8(%rsp), %r8
movl $0x801, (%r8)           # imm = 0x801
movq 0x50(%rbx), %rdx
movq 0xd8(%rbx), %rsi
movq %r14, %rdi
movl $0x801, %ecx            # imm = 0x801
callq *0x28(%r14)
movl %eax, %ebp
cmpl $0xe, %ebp
je 0x296ba7
testl %ebp, %ebp
jne 0x29683c
leaq 0x8(%rsp), %rsi
movb $0x0, (%rsi)
movq 0x50(%rbx), %rdi
movq (%rdi), %rax
xorl %r14d, %r14d
movl $0x1, %edx
xorl %ecx, %ecx
callq *0x10(%rax)
movl %eax, %ebp
cmpl $0x20a, %eax            # imm = 0x20A
cmovel %r14d, %ebp
testq %r13, %r13
jne 0x296a57
movq 0x50(%rbx), %r14
movq (%r14), %rax
testq %rax, %rax
je 0x296a57
movq %r14, %rdi
callq *0x8(%rax)
movq $0x0, (%r14)
cmpb $0x0, 0x8(%rsp)
sete %r12b
jmp 0x29683c
movl $0x2, %r15d
xorl %r14d, %r14d
testb %r14b, %r14b
jne 0x296855
jmp 0x296b6f
movq (%rbx), %r14
movq 0xd8(%rbx), %rsi
leaq 0x10(%rsp), %rcx
movq %r14, %rdi
xorl %edx, %edx
callq *0x38(%r14)
movl %eax, %ebp
testl %eax, %eax
jne 0x296998
cmpl $0x0, 0x10(%rsp)
je 0x296998
leaq 0xc(%rsp), %r8
movl $0x0, (%r8)
movq 0x50(%rbx), %rdx
movq 0xd8(%rbx), %rsi
movq %r14, %rdi
movl $0x802, %ecx            # imm = 0x802
callq *0x28(%r14)
movl %eax, %ebp
testl %eax, %eax
jne 0x296998
xorl %ebp, %ebp
testb $0x1, 0xc(%rsp)
je 0x296998
leaq 0x148835(%rip), %rsi    # 0x3df31f
leaq 0x14881d(%rip), %rdx    # 0x3df30e
leaq 0x14868d(%rip), %r8     # 0x3df185
movl $0xe, %ebp
movl $0xe, %edi
movl $0xde84, %ecx           # imm = 0xDE84
xorl %eax, %eax
callq 0x27e637
movq 0x50(%rbx), %r14
movq (%r14), %rax
testq %rax, %rax
je 0x296998
movq %r14, %rdi
callq *0x8(%rax)
movq $0x0, (%r14)
jmp 0x296998
cmpb $0x0, 0x8(%rbx)
jne 0x296b43
movq %rbx, %rdi
movl $0x1, %esi
callq 0x294e1e
testl %ebp, %ebp
je 0x29684f
cmpb $0xd, %bpl
je 0x296b5a
movzbl %bpl, %eax
cmpl $0xa, %eax
jne 0x296b6f
movl %ebp, 0x2c(%rbx)
movb $0x6, 0x14(%rbx)
leaq -0x3fb3(%rip), %rax     # 0x292bb5
movq %rax, 0x108(%rbx)
cmpl $0x2, %r15d
je 0x296b80
testl %r15d, %r15d
je 0x2966d3
jmp 0x296b96
testl %ebp, %ebp
je 0x296b8e
movq %rbx, %rdi
callq 0x293695
jmp 0x296b96
movb $0x1, %al
movb %al, 0x14(%rbx)
movb %al, 0x1b(%rbx)
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r12d, %r12d
xorl %ebp, %ebp
jmp 0x29683c