// File: /workspace/github_c_makefile_star2_O1/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: hasHotJournal
static int hasHotJournal(Pager *pPager, int *pExists){
  sqlite3_vfs * const pVfs = pPager->pVfs;
  int rc = SQLITE_OK;           /* Return code */
  int exists = 1;               /* True if a journal file is present */
  int jrnlOpen = !!isOpen(pPager->jfd);

  assert( pPager->useJournal );
  assert( isOpen(pPager->fd) );
  assert( pPager->eState==PAGER_OPEN );

  assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
    SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
  ));

  *pExists = 0;
  if( !jrnlOpen ){
    rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
  }
  if( rc==SQLITE_OK && exists ){
    int locked = 0;             /* True if some process holds a RESERVED lock */

    /* Race condition here:  Another process might have been holding the
    ** the RESERVED lock and have a journal open at the sqlite3OsAccess() 
    ** call above, but then delete the journal and drop the lock before
    ** we get to the following sqlite3OsCheckReservedLock() call.  If that
    ** is the case, this routine might think there is a hot journal when
    ** in fact there is none.  This results in a false-positive which will
    ** be dealt with by the playback routine.  Ticket #3883.
    */
    rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
    if( rc==SQLITE_OK && !locked ){
      Pgno nPage;                 /* Number of pages in database file */

      /* Check the size of the database file. If it consists of 0 pages,
      ** then delete the journal file. See the header comment above for 
      ** the reasoning here.  Delete the obsolete journal file under
      ** a RESERVED lock to avoid race conditions and to avoid violating
      ** [H33020].
      */
      rc = pagerPagecount(pPager, &nPage);
      if( rc==SQLITE_OK ){
        if( nPage==0 ){
          sqlite3BeginBenignMalloc();
          if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
            sqlite3OsDelete(pVfs, pPager->zJournal, 0);
            if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
          }
          sqlite3EndBenignMalloc();
        }else{
          /* The journal file exists and no other connection has a reserved
          ** or greater lock on the database file. Now check that there is
          ** at least one non-zero bytes at the start of the journal file.
          ** If there is, then we consider this journal to be hot. If not, 
          ** it can be ignored.
          */
          if( !jrnlOpen ){
            int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
            rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
          }
          if( rc==SQLITE_OK ){
            u8 first = 0;
            rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
            if( rc==SQLITE_IOERR_SHORT_READ ){
              rc = SQLITE_OK;
            }
            if( !jrnlOpen ){
              sqlite3OsClose(pPager->jfd);
            }
            *pExists = (first!=0);
          }else if( rc==SQLITE_CANTOPEN ){
            /* If we cannot open the rollback journal file in order to see if
            ** its has a zero header, that might be due to an I/O error, or
            ** it might be due to the race condition described above and in
            ** ticket #3883.  Either way, assume that the journal is hot.
            ** This might be a false positive.  But if it is, then the
            ** automatic journal playback and recovery mechanism will deal
            ** with it under an EXCLUSIVE lock where we do not need to
            ** worry so much with race conditions.
            */
            *pExists = 1;
            rc = SQLITE_OK;
          }
        }
      }
    }
  }

  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq (%rdi), %r13
movl $0x1, -0x30(%rbp)
movq 0x50(%rdi), %rax
movq (%rax), %r15
xorl %r12d, %r12d
movl $0x0, (%rsi)
testq %r15, %r15
jne 0x2047d
movq 0xd8(%rbx), %rsi
leaq -0x30(%rbp), %rcx
movq %r13, %rdi
xorl %edx, %edx
callq *0x38(%r13)
movl %eax, %r12d
testl %r12d, %r12d
jne 0x204c4
cmpl $0x0, -0x30(%rbp)
je 0x204c4
movq %r14, -0x40(%rbp)
movq %r13, %r14
leaq -0x44(%rbp), %r13
movl $0x0, (%r13)
movq 0x48(%rbx), %rdi
movq (%rdi), %rax
movq %r13, %rsi
callq *0x48(%rax)
movl %eax, %r12d
orl (%r13), %eax
jne 0x204c4
leaq -0x34(%rbp), %rsi
movq %rbx, %rdi
callq 0x20615
movl %eax, %r12d
testl %eax, %eax
je 0x204d6
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, -0x34(%rbp)
je 0x20575
xorl %r12d, %r12d
testq %r15, %r15
jne 0x2050d
leaq -0x2c(%rbp), %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, %r12d
cmpl $0xe, %r12d
je 0x205e3
testl %r12d, %r12d
jne 0x204c4
leaq -0x2c(%rbp), %rsi
movb $0x0, (%rsi)
movq 0x50(%rbx), %rdi
movq (%rdi), %rax
xorl %r13d, %r13d
movl $0x1, %edx
xorl %ecx, %ecx
callq *0x10(%rax)
movl %eax, %r12d
cmpl $0x20a, %eax            # imm = 0x20A
cmovel %r13d, %r12d
testq %r15, %r15
jne 0x20561
movq 0x50(%rbx), %rbx
movq (%rbx), %rax
testq %rax, %rax
je 0x20561
movq %rbx, %rdi
callq *0x8(%rax)
movq $0x0, (%rbx)
xorl %eax, %eax
cmpb $0x0, -0x2c(%rbp)
setne %al
movq -0x40(%rbp), %rcx
movl %eax, (%rcx)
jmp 0x204c4
movq 0x759e4(%rip), %rax     # 0x95f60
testq %rax, %rax
je 0x20583
callq *%rax
movzbl 0x14(%rbx), %eax
cmpl $0x5, %eax
ja 0x205b3
movl $0x23, %ecx
btl %eax, %ecx
jae 0x205b3
movq 0x48(%rbx), %rdi
movq (%rdi), %rax
movl $0x2, %esi
callq *0x38(%rax)
testl %eax, %eax
jne 0x205c9
cmpb $0x5, 0x14(%rbx)
je 0x205b3
movb $0x2, 0x14(%rbx)
movq 0xd8(%rbx), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq *0x30(%r14)
cmpb $0x0, 0x8(%rbx)
je 0x205f5
movq 0x75998(%rip), %rax     # 0x95f68
xorl %r12d, %r12d
testq %rax, %rax
je 0x204c4
callq *%rax
jmp 0x204c4
movq -0x40(%rbp), %rax
movl $0x1, (%rax)
xorl %r12d, %r12d
jmp 0x204c4
movq 0x48(%rbx), %rdi
movq (%rdi), %rax
testq %rax, %rax
je 0x205c9
movl $0x1, %esi
callq *0x40(%rax)
cmpb $0x5, 0x14(%rbx)
je 0x205c9
movb $0x1, 0x14(%rbx)
jmp 0x205c9