// File: /workspace/github_c_makefile_star2_O1/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3PagerSetJournalMode
SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
  u8 eOld = pPager->journalMode;    /* Prior journalmode */

#ifdef SQLITE_DEBUG
  /* The print_pager_state() routine is intended to be used by the debugger
  ** only.  We invoke it once here to suppress a compiler warning. */
  print_pager_state(pPager);
#endif


  /* The eMode parameter is always valid */
  assert(      eMode==PAGER_JOURNALMODE_DELETE
            || eMode==PAGER_JOURNALMODE_TRUNCATE
            || eMode==PAGER_JOURNALMODE_PERSIST
            || eMode==PAGER_JOURNALMODE_OFF 
            || eMode==PAGER_JOURNALMODE_WAL 
            || eMode==PAGER_JOURNALMODE_MEMORY );

  /* This routine is only called from the OP_JournalMode opcode, and
  ** the logic there will never allow a temporary file to be changed
  ** to WAL mode.
  */
  assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );

  /* Do allow the journalmode of an in-memory database to be set to
  ** anything other than MEMORY or OFF
  */
  if( MEMDB ){
    assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
    if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
      eMode = eOld;
    }
  }

  if( eMode!=eOld ){

    /* Change the journal mode. */
    assert( pPager->eState!=PAGER_ERROR );
    pPager->journalMode = (u8)eMode;

    /* When transistioning from TRUNCATE or PERSIST to any other journal
    ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
    ** delete the journal file.
    */
    assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
    assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
    assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
    assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
    assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
    assert( (PAGER_JOURNALMODE_WAL & 5)==5 );

    assert( isOpen(pPager->fd) || pPager->exclusiveMode );
    if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){

      /* In this case we would like to delete the journal file. If it is
      ** not possible, then that is not a problem. Deleting the journal file
      ** here is an optimization only.
      **
      ** Before deleting the journal file, obtain a RESERVED lock on the
      ** database file. This ensures that the journal file is not deleted
      ** while it is in use by some other client.
      */
      sqlite3OsClose(pPager->jfd);
      if( pPager->eLock>=RESERVED_LOCK ){
        sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
      }else{
        int rc = SQLITE_OK;
        int state = pPager->eState;
        assert( state==PAGER_OPEN || state==PAGER_READER );
        if( state==PAGER_OPEN ){
          rc = sqlite3PagerSharedLock(pPager);
        }
        if( pPager->eState==PAGER_READER ){
          assert( rc==SQLITE_OK );
          rc = pagerLockDb(pPager, RESERVED_LOCK);
        }
        if( rc==SQLITE_OK ){
          sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
        }
        if( rc==SQLITE_OK && state==PAGER_READER ){
          pagerUnlockDb(pPager, SHARED_LOCK);
        }else if( state==PAGER_OPEN ){
          pager_unlock(pPager);
        }
        assert( state==pPager->eState );
      }
    }
  }

  /* Return the new journal mode */
  return (int)pPager->journalMode;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
movzbl 0x9(%rdi), %eax
cmpb $0x0, 0x12(%rdi)
je 0x2f1e4
cmpl $0x2, %esi
je 0x2f1e4
cmpl $0x4, %esi
je 0x2f1e4
movl %eax, %esi
cmpl %eax, %esi
jne 0x2f1f7
movzbl 0x9(%rbx), %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movb %sil, 0x9(%rbx)
testb $0x1, %sil
jne 0x2f1e8
andl $0x5, %eax
cmpl $0x1, %eax
jne 0x2f1e8
cmpb $0x0, 0x8(%rbx)
jne 0x2f1e8
movq 0x50(%rbx), %r14
movq (%r14), %rax
testq %rax, %rax
je 0x2f228
movq %r14, %rdi
callq *0x8(%rax)
movq $0x0, (%r14)
cmpb $0x2, 0x14(%rbx)
jb 0x2f23f
movq (%rbx), %rdi
movq 0xd8(%rbx), %rsi
xorl %edx, %edx
callq *0x30(%rdi)
jmp 0x2f1e8
movb 0x13(%rbx), %r15b
xorl %r14d, %r14d
testb %r15b, %r15b
jne 0x2f256
movq %rbx, %rdi
callq 0x1ff1b
movl %eax, %r14d
cmpb $0x1, 0x13(%rbx)
jne 0x2f292
movzbl 0x14(%rbx), %eax
cmpl $0x5, %eax
ja 0x2f28f
movl $0x23, %ecx
btl %eax, %ecx
jae 0x2f28f
movq 0x48(%rbx), %rdi
movq (%rdi), %rax
movl $0x2, %esi
callq *0x38(%rax)
movl %eax, %r14d
testl %eax, %eax
jne 0x2f292
cmpb $0x5, 0x14(%rbx)
je 0x2f28f
movb $0x2, 0x14(%rbx)
xorl %r14d, %r14d
testl %r14d, %r14d
jne 0x2f2a6
movq (%rbx), %rdi
movq 0xd8(%rbx), %rsi
xorl %edx, %edx
callq *0x30(%rdi)
cmpb $0x1, %r15b
jne 0x2f2dc
testl %r14d, %r14d
jne 0x2f2dc
movq 0x48(%rbx), %rdi
movq (%rdi), %rax
testq %rax, %rax
je 0x2f1e8
movl $0x1, %esi
callq *0x40(%rax)
cmpb $0x5, 0x14(%rbx)
je 0x2f1e8
movb $0x1, 0x14(%rbx)
jmp 0x2f1e8
testb %r15b, %r15b
jne 0x2f1e8
movq %rbx, %rdi
callq 0x1d169
jmp 0x2f1e8