// File: /workspace/github_c_makefile_star2_O0/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
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq -0x10(%rbp), %rdi
callq 0x244b0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0xb8(%rcx), %ecx
addq %rcx, %rax
cmpq -0x20(%rbp), %rax
jle 0x27926
movl $0x65, -0x4(%rbp)
jmp 0x27b17
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x48(%rbp)
cmpl $0x0, -0x14(%rbp)
jne 0x27946
movq -0x48(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x68(%rcx), %rax
je 0x279a1
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
leaq -0x3c(%rbp), %rsi
movq -0x48(%rbp), %rcx
movl $0x8, %edx
callq 0x23e30
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x27974
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x27b17
leaq -0x3c(%rbp), %rax
movq (%rax), %rax
movabsq $-0x289c5edf06fa2a27, %rcx # imm = 0xD763A120F905D5D9
subq %rcx, %rax
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x2799f
movl $0x65, -0x4(%rbp)
jmp 0x27b17
jmp 0x279a1
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x48(%rbp), %rsi
addq $0x8, %rsi
movq -0x28(%rbp), %rdx
callq 0x287c0
movl %eax, %ecx
movl %ecx, -0x34(%rbp)
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x27a11
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x48(%rbp), %rsi
addq $0xc, %rsi
movq -0x10(%rbp), %rdx
addq $0x34, %rdx
callq 0x287c0
movl %eax, %ecx
movl %ecx, -0x34(%rbp)
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x27a11
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x48(%rbp), %rsi
addq $0x10, %rsi
movq -0x30(%rbp), %rdx
callq 0x287c0
movl %eax, %ecx
movl %ecx, -0x34(%rbp)
xorl %eax, %eax
cmpl %ecx, %eax
je 0x27a1c
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x27b17
movq -0x10(%rbp), %rax
cmpq $0x0, 0x60(%rax)
jne 0x27af9
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x48(%rbp), %rsi
addq $0x14, %rsi
leaq -0x50(%rbp), %rdx
callq 0x287c0
movl %eax, %ecx
movl %ecx, -0x34(%rbp)
xorl %eax, %eax
cmpl %ecx, %eax
jne 0x27a73
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x48(%rbp), %rsi
addq $0x18, %rsi
leaq -0x4c(%rbp), %rdx
callq 0x287c0
movl %eax, %ecx
movl %ecx, -0x34(%rbp)
xorl %eax, %eax
cmpl %ecx, %eax
je 0x27a7e
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x27b17
cmpl $0x0, -0x4c(%rbp)
jne 0x27a91
movq -0x10(%rbp), %rax
movl 0xbc(%rax), %eax
movl %eax, -0x4c(%rbp)
cmpl $0x200, -0x4c(%rbp)     # imm = 0x200
jb 0x27ace
cmpl $0x20, -0x50(%rbp)
jb 0x27ace
cmpl $0x10000, -0x4c(%rbp)   # imm = 0x10000
ja 0x27ace
cmpl $0x10000, -0x50(%rbp)   # imm = 0x10000
ja 0x27ace
movl -0x4c(%rbp), %eax
subl $0x1, %eax
andl -0x4c(%rbp), %eax
cmpl $0x0, %eax
jne 0x27ace
movl -0x50(%rbp), %eax
subl $0x1, %eax
andl -0x50(%rbp), %eax
cmpl $0x0, %eax
je 0x27ad7
movl $0x65, -0x4(%rbp)
jmp 0x27b17
movq -0x10(%rbp), %rdi
leaq -0x4c(%rbp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x20a90
movl %eax, -0x34(%rbp)
movl -0x50(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xb8(%rax)
movq -0x10(%rbp), %rax
movl 0xb8(%rax), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
addq 0x60(%rax), %rcx
movq %rcx, 0x60(%rax)
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq