// File: /workspace/github_c_makefile_star2_O1/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: writeMasterJournal
static int writeMasterJournal(Pager *pPager, const char *zMaster){
  int rc;                          /* Return code */
  int nMaster;                     /* Length of string zMaster */
  i64 iHdrOff;                     /* Offset of header in journal file */
  i64 jrnlSize;                    /* Size of journal file on disk */
  u32 cksum = 0;                   /* Checksum of string zMaster */

  assert( pPager->setMaster==0 );
  assert( !pagerUseWal(pPager) );

  if( !zMaster 
   || pPager->journalMode==PAGER_JOURNALMODE_MEMORY 
   || pPager->journalMode==PAGER_JOURNALMODE_OFF 
  ){
    return SQLITE_OK;
  }
  pPager->setMaster = 1;
  assert( isOpen(pPager->jfd) );
  assert( pPager->journalHdr <= pPager->journalOff );

  /* Calculate the length in bytes and the checksum of zMaster */
  for(nMaster=0; zMaster[nMaster]; nMaster++){
    cksum += zMaster[nMaster];
  }

  /* If in full-sync mode, advance to the next disk sector before writing
  ** the master journal name. This is in case the previous page written to
  ** the journal has already been synced.
  */
  if( pPager->fullSync ){
    pPager->journalOff = journalHdrOffset(pPager);
  }
  iHdrOff = pPager->journalOff;

  /* Write the master journal data to the end of the journal file. If
  ** an error occurs, return the error code to the caller.
  */
  if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
   || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
   || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
   || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
   || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8)))
  ){
    return rc;
  }
  pPager->journalOff += (nMaster+20);

  /* If the pager is in peristent-journal mode, then the physical 
  ** journal-file may extend past the end of the master-journal name
  ** and 8 bytes of magic data just written to the file. This is 
  ** dangerous because the code to rollback a hot-journal file
  ** will not be able to find the master-journal name to determine 
  ** whether or not the journal is hot. 
  **
  ** Easiest thing to do in this scenario is to truncate the journal 
  ** file to the required size.
  */ 
  if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
   && jrnlSize>pPager->journalOff
  ){
    rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
  }
  return rc;
}

// Assembly:
xorl %eax, %eax
testq %rsi, %rsi
je 0x20f54
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movzbl 0x9(%rdi), %ecx
cmpl $0x2, %ecx
je 0x20f46
cmpl $0x4, %ecx
je 0x20f46
movq %rsi, %r15
movb $0x1, 0x16(%rbx)
movb (%rsi), %al
testb %al, %al
je 0x20e13
xorl %r14d, %r14d
xorl %r13d, %r13d
movsbl %al, %eax
addl %eax, %r13d
movb 0x1(%r15,%r14), %al
incq %r14
testb %al, %al
jne 0x20df8
movl %r14d, %eax
movq %rax, -0x38(%rbp)
jmp 0x20e21
movq $0x0, -0x38(%rbp)
xorl %r13d, %r13d
xorl %r14d, %r14d
cmpb $0x0, 0xc(%rbx)
je 0x20e4d
movq 0x60(%rbx), %rax
testq %rax, %rax
je 0x20e47
decq %rax
movl 0xb8(%rbx), %ecx
cqto
idivq %rcx
incq %rax
imulq %rcx, %rax
jmp 0x20e49
xorl %eax, %eax
movq %rax, 0x60(%rbx)
movq 0x50(%rbx), %rdi
movq 0x60(%rbx), %r12
movl 0x739e5(%rip), %eax     # 0x94840
cltd
idivl 0xbc(%rbx)
incl %eax
bswapl %eax
leaq -0x30(%rbp), %rsi
movl %eax, (%rsi)
movq (%rdi), %rax
movl $0x4, %edx
movq %r12, %rcx
callq *0x18(%rax)
testl %eax, %eax
jne 0x20f46
movq 0x50(%rbx), %rdi
addq $0x4, %r12
movq (%rdi), %rax
movq %r15, %rsi
movq -0x38(%rbp), %r15
movl %r15d, %edx
movq %r12, %rcx
callq *0x18(%rax)
testl %eax, %eax
jne 0x20f46
movq 0x50(%rbx), %rdi
addq %r14, %r12
movl %r15d, %eax
bswapl %eax
leaq -0x30(%rbp), %rsi
movl %eax, (%rsi)
movq (%rdi), %rax
movl $0x4, %edx
movq %r12, %rcx
callq *0x18(%rax)
testl %eax, %eax
jne 0x20f46
movq 0x50(%rbx), %rdi
leaq 0x4(%r12), %rcx
bswapl %r13d
leaq -0x30(%rbp), %rsi
movl %r13d, (%rsi)
movq (%rdi), %rax
movl $0x4, %edx
callq *0x18(%rax)
testl %eax, %eax
jne 0x20f46
movq 0x50(%rbx), %rdi
addq $0x8, %r12
movq (%rdi), %rax
leaq 0x55451(%rip), %rsi     # 0x7634e
movl $0x8, %edx
movq %r12, %rcx
callq *0x18(%rax)
testl %eax, %eax
jne 0x20f46
movl %r15d, %eax
movq 0x50(%rbx), %rdi
movq 0x60(%rbx), %rcx
addq %rcx, %rax
addq $0x14, %rax
movq %rax, 0x60(%rbx)
movq (%rdi), %rax
leaq -0x30(%rbp), %rsi
callq *0x30(%rax)
testl %eax, %eax
jne 0x20f46
movq 0x60(%rbx), %rsi
xorl %eax, %eax
cmpq %rsi, -0x30(%rbp)
jle 0x20f46
movq 0x50(%rbx), %rdi
movq (%rdi), %rax
callq *0x20(%rax)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq