// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: writeJournalHdr
static int writeJournalHdr(Pager *pPager){
  int rc = SQLITE_OK;                 /* Return code */
  char *zHeader = pPager->pTmpSpace;  /* Temporary space used to build header */
  u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
  u32 nWrite;                         /* Bytes of header sector written */
  int ii;                             /* Loop counter */

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

  if( nHeader>JOURNAL_HDR_SZ(pPager) ){
    nHeader = JOURNAL_HDR_SZ(pPager);
  }

  /* If there are active savepoints and any of them were created 
  ** since the most recent journal header was written, update the 
  ** PagerSavepoint.iHdrOffset fields now.
  */
  for(ii=0; ii<pPager->nSavepoint; ii++){
    if( pPager->aSavepoint[ii].iHdrOffset==0 ){
      pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
    }
  }

  pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);

  /* 
  ** Write the nRec Field - the number of page records that follow this
  ** journal header. Normally, zero is written to this value at this time.
  ** After the records are added to the journal (and the journal synced, 
  ** if in full-sync mode), the zero is overwritten with the true number
  ** of records (see syncJournal()).
  **
  ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
  ** reading the journal this value tells SQLite to assume that the
  ** rest of the journal file contains valid page records. This assumption
  ** is dangerous, as if a failure occurred whilst writing to the journal
  ** file it may contain some garbage data. There are two scenarios
  ** where this risk can be ignored:
  **
  **   * When the pager is in no-sync mode. Corruption can follow a
  **     power failure in this case anyway.
  **
  **   * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
  **     that garbage data is never appended to the journal file.
  */
  assert( isOpen(pPager->fd) || pPager->noSync );
  if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
   || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) 
  ){
    memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
    put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
  }else{
    memset(zHeader, 0, sizeof(aJournalMagic)+4);
  }

  /* The random check-hash initializer */ 
  sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
  put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
  /* The initial database size */
  put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
  /* The assumed sector size for this process */
  put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);

  /* The page size */
  put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);

  /* Initializing the tail of the buffer is not necessary.  Everything
  ** works find if the following memset() is omitted.  But initializing
  ** the memory prevents valgrind from complaining, so we are willing to
  ** take the performance hit.
  */
  memset(&zHeader[sizeof(aJournalMagic)+20], 0,
         nHeader-(sizeof(aJournalMagic)+20));

  /* In theory, it is only necessary to write the 28 bytes that the 
  ** journal header consumes to the journal file here. Then increment the 
  ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next 
  ** record is written to the following sector (leaving a gap in the file
  ** that will be implicitly filled in by the OS).
  **
  ** However it has been discovered that on some systems this pattern can 
  ** be significantly slower than contiguously writing data to the file,
  ** even if that means explicitly writing data to the block of 
  ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
  ** is done. 
  **
  ** The loop is required here in case the sector-size is larger than the 
  ** database page size. Since the zHeader buffer is only Pager.pageSize
  ** bytes in size, more than one call to sqlite3OsWrite() may be required
  ** to populate the entire journal header sector.
  */ 
  for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
    IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
    rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
    assert( pPager->journalHdr <= pPager->journalOff );
    pPager->journalOff += nHeader;
  }

  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movl $0x0, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq 0x108(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rax
movl 0xbc(%rax), %eax
movl %eax, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0xb8(%rcx), %eax
jbe 0x2455b
movq -0x8(%rbp), %rax
movl 0xb8(%rax), %eax
movl %eax, -0x1c(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x80(%rcx), %eax
jge 0x245b7
movq -0x8(%rbp), %rax
movq 0x78(%rax), %rax
movslq -0x24(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, 0x8(%rax)
jne 0x245aa
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rcx
movq -0x8(%rbp), %rax
movq 0x78(%rax), %rax
movslq -0x24(%rbp), %rdx
imulq $0x30, %rdx, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
jmp 0x245ac
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x24562
movq -0x8(%rbp), %rdi
callq 0x244b0
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x8(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x8(%rbp), %rax
movzbl 0xb(%rax), %eax
cmpl $0x0, %eax
jne 0x24604
movq -0x8(%rbp), %rax
movzbl 0x9(%rax), %eax
cmpl $0x4, %eax
je 0x24604
movq -0x8(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x210d0
andl $0x200, %eax            # imm = 0x200
cmpl $0x0, %eax
je 0x24626
movq -0x18(%rbp), %rax
movq 0x868b5(%rip), %rcx     # 0xaaec4
movq %rcx, (%rax)
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x22060
jmp 0x24636
movq -0x18(%rbp), %rdi
xorl %esi, %esi
movl $0xc, %edx
callq 0x8200
movq -0x8(%rbp), %rsi
addq $0x34, %rsi
movl $0x4, %edi
callq 0xc740
movq -0x18(%rbp), %rdi
addq $0xc, %rdi
movq -0x8(%rbp), %rax
movl 0x34(%rax), %esi
callq 0x22060
movq -0x18(%rbp), %rdi
addq $0x10, %rdi
movq -0x8(%rbp), %rax
movl 0x20(%rax), %esi
callq 0x22060
movq -0x18(%rbp), %rdi
addq $0x14, %rdi
movq -0x8(%rbp), %rax
movl 0xb8(%rax), %esi
callq 0x22060
movq -0x18(%rbp), %rdi
addq $0x18, %rdi
movq -0x8(%rbp), %rax
movl 0xbc(%rax), %esi
callq 0x22060
movq -0x18(%rbp), %rdi
addq $0x1c, %rdi
movl -0x1c(%rbp), %eax
movl %eax, %edx
subq $0x1c, %rdx
xorl %esi, %esi
callq 0x8200
movl $0x0, -0x20(%rbp)
xorl %eax, %eax
cmpl $0x0, -0xc(%rbp)
movb %al, -0x25(%rbp)
jne 0x246db
movl -0x20(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0xb8(%rcx), %eax
setb %al
movb %al, -0x25(%rbp)
movb -0x25(%rbp), %al
testb $0x1, %al
jne 0x246e4
jmp 0x2471f
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rcx
callq 0xf430
movl %eax, -0xc(%rbp)
movl -0x1c(%rbp), %eax
movl %eax, %ecx
movq -0x8(%rbp), %rax
addq 0x60(%rax), %rcx
movq %rcx, 0x60(%rax)
movl -0x1c(%rbp), %eax
addl -0x20(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0x246bd
movl -0xc(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)