// File: /workspace/github_c_makefile_star2_O0/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3BtreeBeginTrans
SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
  sqlite3 *pBlock = 0;
  BtShared *pBt = p->pBt;
  int rc = SQLITE_OK;

  sqlite3BtreeEnter(p);
  btreeIntegrity(p);

  /* If the btree is already in a write-transaction, or it
  ** is already in a read-transaction and a read-transaction
  ** is requested, this is a no-op.
  */
  if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
    goto trans_begun;
  }
  assert( IfNotOmitAV(pBt->bDoTruncate)==0 );

  /* Write transactions are not possible on a read-only database */
  if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
    rc = SQLITE_READONLY;
    goto trans_begun;
  }

#ifndef SQLITE_OMIT_SHARED_CACHE
  /* If another database handle has already opened a write transaction 
  ** on this shared-btree structure and a second write transaction is
  ** requested, return SQLITE_LOCKED.
  */
  if( (wrflag && pBt->inTransaction==TRANS_WRITE)
   || (pBt->btsFlags & BTS_PENDING)!=0
  ){
    pBlock = pBt->pWriter->db;
  }else if( wrflag>1 ){
    BtLock *pIter;
    for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
      if( pIter->pBtree!=p ){
        pBlock = pIter->pBtree->db;
        break;
      }
    }
  }
  if( pBlock ){
    sqlite3ConnectionBlocked(p->db, pBlock);
    rc = SQLITE_LOCKED_SHAREDCACHE;
    goto trans_begun;
  }
#endif

  /* Any read-only or read-write transaction implies a read-lock on 
  ** page 1. So if some other shared-cache client already has a write-lock 
  ** on page 1, the transaction cannot be opened. */
  rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
  if( SQLITE_OK!=rc ) goto trans_begun;

  pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
  if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
  do {
    /* Call lockBtree() until either pBt->pPage1 is populated or
    ** lockBtree() returns something other than SQLITE_OK. lockBtree()
    ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
    ** reading page 1 it discovers that the page-size of the database 
    ** file is not pBt->pageSize. In this case lockBtree() will update
    ** pBt->pageSize to the page-size of the file on disk.
    */
    while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );

    if( rc==SQLITE_OK && wrflag ){
      if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
        rc = SQLITE_READONLY;
      }else{
        rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
        if( rc==SQLITE_OK ){
          rc = newDatabase(pBt);
        }
      }
    }
  
    if( rc!=SQLITE_OK ){
      unlockBtreeIfUnused(pBt);
    }
  }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
          btreeInvokeBusyHandler(pBt) );

  if( rc==SQLITE_OK ){
    if( p->inTrans==TRANS_NONE ){
      pBt->nTransaction++;
#ifndef SQLITE_OMIT_SHARED_CACHE
      if( p->sharable ){
        assert( p->lock.pBtree==p && p->lock.iTable==1 );
        p->lock.eLock = READ_LOCK;
        p->lock.pNext = pBt->pLock;
        pBt->pLock = &p->lock;
      }
#endif
    }
    p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
    if( p->inTrans>pBt->inTransaction ){
      pBt->inTransaction = p->inTrans;
    }
    if( wrflag ){
      MemPage *pPage1 = pBt->pPage1;
#ifndef SQLITE_OMIT_SHARED_CACHE
      assert( !pBt->pWriter );
      pBt->pWriter = p;
      pBt->btsFlags &= ~BTS_EXCLUSIVE;
      if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
#endif

      /* If the db-size header field is incorrect (as it may be if an old
      ** client has been writing the database file), update it now. Doing
      ** this sooner rather than later means the database size can safely 
      ** re-read the database size from page 1 if a savepoint or transaction
      ** rollback occurs within the transaction.
      */
      if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
        rc = sqlite3PagerWrite(pPage1->pDbPage);
        if( rc==SQLITE_OK ){
          put4byte(&pPage1->aData[28], pBt->nPage);
        }
      }
    }
  }


trans_begun:
  if( rc==SQLITE_OK && wrflag ){
    /* This call makes sure that the pager has the correct number of
    ** open savepoints. If the second parameter is greater than 0 and
    ** the sub-journal is not already open, then it will be opened here.
    */
    rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
  }

  btreeIntegrity(p);
  sqlite3BtreeLeave(p);
  return rc;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x8(%rbp), %rdi
callq 0xe0f0
movq -0x8(%rbp), %rax
movzbl 0x10(%rax), %eax
cmpl $0x2, %eax
je 0xe1a3
movq -0x8(%rbp), %rax
movzbl 0x10(%rax), %eax
cmpl $0x1, %eax
jne 0xe1a8
cmpl $0x0, -0xc(%rbp)
jne 0xe1a8
jmp 0xe4d4
movq -0x20(%rbp), %rax
movzwl 0x26(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xe1ca
cmpl $0x0, -0xc(%rbp)
je 0xe1ca
movl $0x8, -0x24(%rbp)
jmp 0xe4d4
cmpl $0x0, -0xc(%rbp)
je 0xe1dd
movq -0x20(%rbp), %rax
movzbl 0x24(%rax), %eax
cmpl $0x2, %eax
je 0xe1ed
movq -0x20(%rbp), %rax
movzwl 0x26(%rax), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0xe1fe
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0xe248
cmpl $0x1, -0xc(%rbp)
jle 0xe246
movq -0x20(%rbp), %rax
movq 0x70(%rax), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xe244
movq -0x30(%rbp), %rax
movq (%rax), %rax
cmpq -0x8(%rbp), %rax
je 0xe234
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq %rax, -0x18(%rbp)
jmp 0xe244
jmp 0xe236
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0xe210
jmp 0xe246
jmp 0xe248
cmpq $0x0, -0x18(%rbp)
je 0xe25b
movl $0x106, -0x24(%rbp)     # imm = 0x106
jmp 0xe4d4
movq -0x8(%rbp), %rdi
movl $0x1, %edx
movl %edx, %esi
callq 0x29180
movl %eax, -0x24(%rbp)
xorl %eax, %eax
cmpl -0x24(%rbp), %eax
je 0xe27a
jmp 0xe4d4
movq -0x20(%rbp), %rax
movzwl 0x26(%rax), %ecx
andl $-0x9, %ecx
movw %cx, 0x26(%rax)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x3c(%rax)
jne 0xe2a2
movq -0x20(%rbp), %rax
movzwl 0x26(%rax), %ecx
orl $0x8, %ecx
movw %cx, 0x26(%rax)
jmp 0xe2a4
jmp 0xe2a6
movq -0x20(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, 0x18(%rcx)
movb %al, -0x39(%rbp)
jne 0xe2ce
movq -0x20(%rbp), %rdi
callq 0x29260
movl %eax, %ecx
movl %ecx, -0x24(%rbp)
xorl %eax, %eax
cmpl %ecx, %eax
sete %al
movb %al, -0x39(%rbp)
movb -0x39(%rbp), %al
testb $0x1, %al
jne 0xe2d7
jmp 0xe2d9
jmp 0xe2a6
cmpl $0x0, -0x24(%rbp)
jne 0xe34b
cmpl $0x0, -0xc(%rbp)
je 0xe34b
movq -0x20(%rbp), %rax
movzwl 0x26(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xe2fe
movl $0x8, -0x24(%rbp)
jmp 0xe349
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
cmpl $0x1, -0xc(%rbp)
setg %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
callq 0x200c0
movq -0x48(%rbp), %rdi
movl -0x40(%rbp), %esi
movl %eax, %edx
callq 0x29650
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0xe347
movq -0x20(%rbp), %rdi
callq 0x297b0
movl %eax, -0x24(%rbp)
jmp 0xe349
jmp 0xe34b
cmpl $0x0, -0x24(%rbp)
je 0xe35a
movq -0x20(%rbp), %rdi
callq 0x29920
jmp 0xe35c
movl -0x24(%rbp), %ecx
andl $0xff, %ecx
xorl %eax, %eax
cmpl $0x5, %ecx
movb %al, -0x49(%rbp)
jne 0xe393
movq -0x20(%rbp), %rax
movzbl 0x24(%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x49(%rbp)
jne 0xe393
movq -0x20(%rbp), %rdi
callq 0x20a20
cmpl $0x0, %eax
setne %al
movb %al, -0x49(%rbp)
movb -0x49(%rbp), %al
testb $0x1, %al
jne 0xe2a4
cmpl $0x0, -0x24(%rbp)
jne 0xe4d2
movq -0x8(%rbp), %rax
movzbl 0x10(%rax), %eax
cmpl $0x0, %eax
jne 0xe3f6
movq -0x20(%rbp), %rax
movl 0x38(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x38(%rax)
movq -0x8(%rbp), %rax
cmpb $0x0, 0x11(%rax)
je 0xe3f4
movq -0x8(%rbp), %rax
movb $0x1, 0x3c(%rax)
movq -0x20(%rbp), %rax
movq 0x70(%rax), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rcx
addq $0x30, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x70(%rax)
jmp 0xe3f6
movl -0xc(%rbp), %edx
movl $0x1, %eax
movl $0x2, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x10(%rax)
movq -0x8(%rbp), %rax
movzbl 0x10(%rax), %eax
movq -0x20(%rbp), %rcx
movzbl 0x24(%rcx), %ecx
cmpl %ecx, %eax
jle 0xe434
movq -0x8(%rbp), %rax
movb 0x10(%rax), %cl
movq -0x20(%rbp), %rax
movb %cl, 0x24(%rax)
cmpl $0x0, -0xc(%rbp)
je 0xe4d0
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x78(%rax)
movq -0x20(%rbp), %rax
movzwl 0x26(%rax), %ecx
andl $-0x21, %ecx
movw %cx, 0x26(%rax)
cmpl $0x1, -0xc(%rbp)
jle 0xe47a
movq -0x20(%rbp), %rax
movzwl 0x26(%rax), %ecx
orl $0x20, %ecx
movw %cx, 0x26(%rax)
movq -0x20(%rbp), %rax
movl 0x3c(%rax), %eax
movl %eax, -0x50(%rbp)
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x1c, %rdi
callq 0x20a50
movl %eax, %ecx
movl -0x50(%rbp), %eax
cmpl %ecx, %eax
je 0xe4ce
movq -0x38(%rbp), %rax
movq 0x68(%rax), %rdi
callq 0xef80
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0xe4cc
movq -0x38(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x1c, %rdi
movq -0x20(%rbp), %rax
movl 0x3c(%rax), %esi
callq 0x22060
jmp 0xe4ce
jmp 0xe4d0
jmp 0xe4d2
jmp 0xe4d4
cmpl $0x0, -0x24(%rbp)
jne 0xe4fc
cmpl $0x0, -0xc(%rbp)
je 0xe4fc
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl 0x30c(%rax), %esi
callq 0x29970
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)