// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: readJournalHdr
static int readJournalHdr(
  Pager *pPager,               /* Pager object */
  int isHot,
  i64 journalSize,             /* Size of the open journal file in bytes */
  u32 *pNRec,                  /* OUT: Value read from the nRec field */
  u32 *pDbSize                 /* OUT: Value of original database size field */
){
  int rc;                      /* Return code */
  unsigned char aMagic[8];     /* A buffer to hold the magic header */
  i64 iHdrOff;                 /* Offset of journal header being read */

  assert( isOpen(pPager->jfd) );      /* Journal file must be open. */

  /* Advance Pager.journalOff to the start of the next sector. If the
  ** journal file is too small for there to be a header stored at this
  ** point, return SQLITE_DONE.
  */
  pPager->journalOff = journalHdrOffset(pPager);
  if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
    return SQLITE_DONE;
  }
  iHdrOff = pPager->journalOff;

  /* Read in the first 8 bytes of the journal header. If they do not match
  ** the  magic string found at the start of each journal header, return
  ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
  ** proceed.
  */
  if( isHot || iHdrOff!=pPager->journalHdr ){
    rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
    if( rc ){
      return rc;
    }
    if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
      return SQLITE_DONE;
    }
  }

  /* Read the first three 32-bit fields of the journal header: The nRec
  ** field, the checksum-initializer and the database size at the start
  ** of the transaction. Return an error code if anything goes wrong.
  */
  if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
   || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
   || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
  ){
    return rc;
  }

  if( pPager->journalOff==0 ){
    u32 iPageSize;               /* Page-size field of journal header */
    u32 iSectorSize;             /* Sector-size field of journal header */

    /* Read the page-size and sector-size journal header fields. */
    if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
     || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
    ){
      return rc;
    }

    /* Versions of SQLite prior to 3.5.8 set the page-size field of the
    ** journal header to zero. In this case, assume that the Pager.pageSize
    ** variable is already set to the correct page size.
    */
    if( iPageSize==0 ){
      iPageSize = pPager->pageSize;
    }

    /* Check that the values read from the page-size and sector-size fields
    ** are within range. To be 'in range', both values need to be a power
    ** of two greater than or equal to 512 or 32, and not greater than their 
    ** respective compile time maximum limits.
    */
    if( iPageSize<512                  || iSectorSize<32
     || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
     || ((iPageSize-1)&iPageSize)!=0   || ((iSectorSize-1)&iSectorSize)!=0 
    ){
      /* If the either the page-size or sector-size in the journal-header is 
      ** invalid, then the process that wrote the journal-header must have 
      ** crashed before the header was synced. In this case stop reading 
      ** the journal file here.
      */
      return SQLITE_DONE;
    }

    /* Update the page-size to match the value read from the journal. 
    ** Use a testcase() macro to make sure that malloc failure within 
    ** PagerSetPagesize() is tested.
    */
    rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
    testcase( rc!=SQLITE_OK );

    /* Update the assumed sector-size to match the value used by 
    ** the process that created this journal. If this journal was
    ** created by a process other than this one, then this routine
    ** is being called from within pager_playback(). The local value
    ** of Pager.sectorSize is restored at the end of that routine.
    */
    pPager->sectorSize = iSectorSize;
  }

  pPager->journalOff += JOURNAL_HDR_SZ(pPager);
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r8, %r12
movq %rcx, %r13
movq %rdx, %rcx
movq %rdi, %rbx
movq 0x60(%rdi), %rax
testq %rax, %rax
je 0x1ef31
decq %rax
movl 0xb8(%rbx), %edi
cqto
idivq %rdi
movq %rax, %r15
incq %r15
imulq %rdi, %r15
jmp 0x1ef3a
movl 0xb8(%rbx), %edi
xorl %r15d, %r15d
movq %r15, 0x60(%rbx)
addq %r15, %rdi
movl $0x65, %r14d
cmpq %rcx, %rdi
jg 0x1efc0
testl %esi, %esi
jne 0x1ef56
cmpq 0x68(%rbx), %r15
je 0x1ef80
movq 0x50(%rbx), %rdi
movq (%rdi), %rax
leaq -0x38(%rbp), %rsi
movl $0x8, %edx
movq %r15, %rcx
callq *0x10(%rax)
testl %eax, %eax
jne 0x1efbd
movabsq $-0x289c5edf06fa2a27, %rax # imm = 0xD763A120F905D5D9
cmpq %rax, -0x38(%rbp)
jne 0x1efc0
movq 0x50(%rbx), %rdi
leaq 0x8(%r15), %rsi
movq %r13, %rdx
callq 0x1f74c
testl %eax, %eax
jne 0x1efbd
movq 0x50(%rbx), %rdi
leaq 0xc(%r15), %rsi
leaq 0x34(%rbx), %rdx
callq 0x1f74c
testl %eax, %eax
jne 0x1efbd
movq 0x50(%rbx), %rdi
leaq 0x10(%r15), %rsi
movq %r12, %rdx
callq 0x1f74c
testl %eax, %eax
je 0x1efd2
movl %eax, %r14d
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x60(%rbx), %rax
testq %rax, %rax
je 0x1eff1
movl 0xb8(%rbx), %r15d
xorl %r14d, %r14d
movl %r15d, %ecx
addq %rax, %rcx
movq %rcx, 0x60(%rbx)
jmp 0x1efc0
movq 0x50(%rbx), %rdi
leaq 0x14(%r15), %rsi
leaq -0x30(%rbp), %rdx
callq 0x1f74c
testl %eax, %eax
jne 0x1efbd
movq 0x50(%rbx), %rdi
addq $0x18, %r15
leaq -0x2c(%rbp), %rdx
movq %r15, %rsi
callq 0x1f74c
testl %eax, %eax
jne 0x1efbd
movl -0x2c(%rbp), %eax
testl %eax, %eax
jne 0x1f02e
movl 0xbc(%rbx), %eax
movl %eax, -0x2c(%rbp)
movl -0x30(%rbp), %r15d
movl $0xfffeffff, %ecx       # imm = 0xFFFEFFFF
leal (%rax,%rcx), %edx
cmpl $0xffff01ff, %edx       # imm = 0xFFFF01FF
setb %dl
addl %r15d, %ecx
cmpl $0xffff001f, %ecx       # imm = 0xFFFF001F
setb %cl
orb %dl, %cl
jne 0x1efc0
leal 0x1ffff(%rax), %ecx
testl %eax, %ecx
jne 0x1efc0
leal 0x1ffff(%r15), %eax
testl %r15d, %eax
jne 0x1efc0
leaq -0x2c(%rbp), %rsi
movq %rbx, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x1a26a
movl %eax, %r14d
movl %r15d, 0xb8(%rbx)
movq 0x60(%rbx), %rax
jmp 0x1efe5