// File: /workspace/github_c_makefile_star2_O1/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 0x1f11b
decq %rax
movl 0xb8(%rbx), %edi
cqto
idivq %rdi
movq %rax, %r14
incq %r14
imulq %rdi, %r14
jmp 0x1f11e
xorl %r14d, %r14d
movq %r14, 0x60(%rbx)
movl 0xb8(%rbx), %eax
addq %r14, %rax
movl $0x65, %r15d
cmpq %rcx, %rax
jg 0x1f1ff
testl %esi, %esi
jne 0x1f144
cmpq 0x68(%rbx), %r14
je 0x1f176
movq 0x50(%rbx), %rdi
movq (%rdi), %rax
leaq -0x38(%rbp), %rsi
movl $0x8, %edx
movq %r14, %rcx
callq *0x10(%rax)
testl %eax, %eax
jne 0x1f1fc
movabsq $-0x289c5edf06fa2a27, %rax # imm = 0xD763A120F905D5D9
cmpq %rax, -0x38(%rbp)
jne 0x1f1ff
movq 0x50(%rbx), %rdi
leaq 0x8(%r14), %rsi
movq %r13, %rdx
callq 0x1f978
testl %eax, %eax
jne 0x1f1fc
movq 0x50(%rbx), %rdi
leaq 0xc(%r14), %rsi
leaq 0x34(%rbx), %rdx
callq 0x1f978
testl %eax, %eax
jne 0x1f1fc
movq 0x50(%rbx), %rdi
leaq 0x10(%r14), %rsi
movq %r12, %rdx
callq 0x1f978
testl %eax, %eax
jne 0x1f1fc
xorl %eax, %eax
cmpq $0x0, 0x60(%rbx)
jne 0x1f1f2
movq 0x50(%rbx), %rdi
leaq 0x14(%r14), %rsi
leaq -0x30(%rbp), %rdx
callq 0x1f978
testl %eax, %eax
jne 0x1f1e9
movq 0x50(%rbx), %rdi
addq $0x18, %r14
leaq -0x2c(%rbp), %rdx
movq %r14, %rsi
callq 0x1f978
testl %eax, %eax
je 0x1f211
xorl %ecx, %ecx
movl %eax, %r15d
testb %cl, %cl
je 0x1f1ff
movl 0xb8(%rbx), %ecx
addq %rcx, 0x60(%rbx)
movl %eax, %r15d
movl %r15d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, -0x2c(%rbp)
jne 0x1f220
movl 0xbc(%rbx), %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %ecx
movl -0x30(%rbp), %r14d
movl $0xfffeffff, %eax       # imm = 0xFFFEFFFF
leal (%rcx,%rax), %edx
cmpl $0xffff01ff, %edx       # imm = 0xFFFF01FF
setb %dl
addl %r14d, %eax
cmpl $0xffff001f, %eax       # imm = 0xFFFF001F
setb %sil
xorl %eax, %eax
orb %dl, %sil
jne 0x1f257
leal 0x1ffff(%rcx), %edx
xorl %eax, %eax
testl %ecx, %edx
je 0x1f25b
xorl %ecx, %ecx
jmp 0x1f1ee
leal 0x1ffff(%r14), %edx
xorl %eax, %eax
movl $0x0, %ecx
testl %r14d, %edx
jne 0x1f1ee
leaq -0x2c(%rbp), %rsi
movq %rbx, %rdi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x1a405
movl %r14d, 0xb8(%rbx)
movb $0x1, %cl
jmp 0x1f1ee