// File: /workspace/github_c_makefile_star2_O0/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
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl $0x1, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
cmpq $0x0, (%rax)
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movl $0x0, (%rax)
cmpl $0x0, -0x24(%rbp)
jne 0x2a0b1
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0xd8(%rax), %rsi
xorl %edx, %edx
leaq -0x20(%rbp), %rcx
callq 0x28b70
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x2a21b
cmpl $0x0, -0x20(%rbp)
je 0x2a21b
movl $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x2a470
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x2a219
cmpl $0x0, -0x28(%rbp)
jne 0x2a219
movq -0x8(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x2a230
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x2a217
cmpl $0x0, -0x2c(%rbp)
jne 0x2a166
callq 0x241b0
movq -0x8(%rbp), %rdi
movl $0x2, %esi
callq 0x247e0
cmpl $0x0, %eax
jne 0x2a15c
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0xd8(%rax), %rsi
xorl %edx, %edx
callq 0x259d0
movq -0x8(%rbp), %rax
cmpb $0x0, 0x8(%rax)
jne 0x2a15a
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x26790
jmp 0x2a15c
callq 0x24200
jmp 0x2a215
cmpl $0x0, -0x24(%rbp)
jne 0x2a199
movl $0x801, -0x30(%rbp)     # imm = 0x801
movq -0x18(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0xd8(%rax), %rsi
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rdx
movl -0x30(%rbp), %ecx
leaq -0x30(%rbp), %r8
callq 0x20df0
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x2a1fa
movb $0x0, -0x31(%rbp)
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rdi
leaq -0x31(%rbp), %rsi
movl $0x1, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x23e30
movl %eax, -0x1c(%rbp)
cmpl $0x20a, -0x1c(%rbp)     # imm = 0x20A
jne 0x2a1d0
movl $0x0, -0x1c(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x2a1e3
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rdi
callq 0x20ea0
movzbl -0x31(%rbp), %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movzbl %al, %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
jmp 0x2a213
cmpl $0xe, -0x1c(%rbp)
jne 0x2a211
movq -0x10(%rbp), %rax
movl $0x1, (%rax)
movl $0x0, -0x1c(%rbp)
jmp 0x2a213
jmp 0x2a215
jmp 0x2a217
jmp 0x2a219
jmp 0x2a21b
movl -0x1c(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)