// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x108(%rdi), %r14
movl 0xb8(%rdi), %ecx
movl 0xbc(%rdi), %r15d
cmpl %ecx, %r15d
cmovael %ecx, %r15d
movslq 0x80(%rdi), %rdx
testq %rdx, %rdx
jle 0x1c9e7
movq 0x78(%rbx), %rax
shlq $0x4, %rdx
leaq (%rdx,%rdx,2), %rdx
xorl %esi, %esi
cmpq $0x0, 0x8(%rax,%rsi)
jne 0x1c9de
movq 0x60(%rbx), %rdi
movq %rdi, 0x8(%rax,%rsi)
addq $0x30, %rsi
cmpq %rsi, %rdx
jne 0x1c9cd
movq 0x60(%rbx), %rax
testq %rax, %rax
je 0x1ca01
decq %rax
cqto
idivq %rcx
incq %rax
imulq %rcx, %rax
jmp 0x1ca03
xorl %eax, %eax
movq %rax, 0x60(%rbx)
movq %rax, 0x68(%rbx)
cmpb $0x0, 0xb(%rbx)
jne 0x1ca38
cmpb $0x4, 0x9(%rbx)
je 0x1ca38
movq 0x48(%rbx), %rdi
movq (%rdi), %rax
callq *0x60(%rax)
btl $0x9, %eax
jb 0x1ca38
movl $0x0, 0x8(%r14)
movq $0x0, (%r14)
jmp 0x1ca4d
movabsq $-0x289c5edf06fa2a27, %rax # imm = 0xD763A120F905D5D9
movq %rax, (%r14)
movl $0xffffffff, 0x8(%r14)  # imm = 0xFFFFFFFF
leaq 0x34(%rbx), %rsi
movl $0x4, %edi
callq 0xb9d9
movl 0x34(%rbx), %eax
bswapl %eax
movl %eax, 0xc(%r14)
movl 0x20(%rbx), %eax
bswapl %eax
movl %eax, 0x10(%r14)
movl 0xb8(%rbx), %eax
bswapl %eax
movl %eax, 0x14(%r14)
movl 0xbc(%rbx), %eax
bswapl %eax
movl %eax, 0x18(%r14)
movq %r14, -0x30(%rbp)
leaq 0x1c(%r14), %rdi
movl %r15d, %r13d
leaq -0x1c(%r13), %rdx
xorl %r12d, %r12d
xorl %esi, %esi
callq 0x8200
cmpl $0x0, 0xb8(%rbx)
je 0x1cad6
xorl %r14d, %r14d
movq 0x50(%rbx), %rdi
movq 0x60(%rbx), %rcx
movq (%rdi), %rax
movq -0x30(%rbp), %rsi
movl %r15d, %edx
callq *0x18(%rax)
movl %eax, %r12d
addq %r13, 0x60(%rbx)
testl %eax, %eax
jne 0x1cad6
addl %r15d, %r14d
cmpl 0xb8(%rbx), %r14d
jb 0x1caaa
movl %r12d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq