// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, %r15d
movq %rdi, %rbx
movq (%rdi), %rax
movq 0x8(%rdi), %r14
movq %rax, 0x8(%r14)
movb 0x10(%rdi), %al
xorl %r12d, %r12d
cmpb $0x2, %al
je 0xd7c8
cmpb $0x1, %al
sete %al
testl %r15d, %r15d
sete %cl
testb %al, %cl
jne 0xd7c8
movzwl 0x26(%r14), %eax
testl %r15d, %r15d
je 0xd0c5
movl %eax, %ecx
movl $0x8, %r12d
andl $0x1, %ecx
jne 0xd7c8
testl %r15d, %r15d
je 0xd0f2
testb $0x40, %al
jne 0xd0f6
cmpb $0x2, 0x24(%r14)
je 0xd0f6
cmpl $0x2, %r15d
jl 0xd10a
movq 0x70(%r14), %rax
testq %rax, %rax
je 0xd10a
movq (%rax), %rcx
cmpq %rbx, %rcx
jne 0xd0fa
movq 0x10(%rax), %rax
jmp 0xd0df
testb $0x40, %al
je 0xd0d5
movq 0x78(%r14), %rcx
movl $0x106, %r12d           # imm = 0x106
cmpq $0x0, (%rcx)
jne 0xd7c8
movq %rbx, %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0x1fc86
testl %eax, %eax
je 0xd128
movl %eax, %r12d
jmp 0xd7c8
movl %r15d, -0x2c(%rbp)
movzwl 0x26(%r14), %eax
movl %eax, %ecx
andl $-0x9, %ecx
movw %cx, 0x26(%r14)
cmpl $0x0, 0x3c(%r14)
jne 0xd14a
orl $0x8, %eax
movw %ax, 0x26(%r14)
leaq 0x30(%r14), %rax
movq %rax, -0x48(%rbp)
xorl %r12d, %r12d
cmpq $0x0, 0x18(%r14)
jne 0xd469
jmp 0xd430
movq -0x50(%rbp), %r8
movq 0x50(%r8), %r15
movl 0x1c(%r15), %r13d
bswapl %r13d
movq (%r14), %rdi
movl 0x1c(%rdi), %r9d
testl %r13d, %r13d
je 0xd18a
movl 0x18(%r15), %eax
cmpl 0x5c(%r15), %eax
je 0xd18d
movl %r9d, %r13d
testl %r13d, %r13d
jle 0xd1d8
movdqu (%r15), %xmm0
pcmpeqb 0x6c1d1(%rip), %xmm0    # 0x79370
pmovmskb %xmm0, %eax
movl $0x1a, %esi
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0xd1c6
cmpb $0x3, 0x12(%r15)
jb 0xd1bb
orb $0x1, 0x26(%r14)
cmpb $0x2, 0x13(%r15)
jbe 0xd26d
movl $0x2, %ecx
cmpl $0x2, %ecx
je 0xd249
testl %ecx, %ecx
jne 0xd45a
movl 0x34(%r14), %ecx
movl %ecx, %eax
shll $0x6, %eax
addl $0xfffffd00, %eax       # imm = 0xFFFFFD00
movl $0x80808081, %esi       # imm = 0x80808081
imulq %rsi, %rax
shrq $0x27, %rax
addl $-0x17, %eax
movw %ax, 0x28(%r14)
movl %ecx, %edx
shll $0x5, %edx
addl $0xfffffe80, %edx       # imm = 0xFFFFFE80
imulq %rsi, %rdx
shrq $0x27, %rdx
addl $-0x17, %edx
movw %dx, 0x2a(%r14)
addl $-0x23, %ecx
movw %cx, 0x2c(%r14)
movw %dx, 0x2e(%r14)
movzwl %ax, %ecx
cmpl $0x80, %ecx
jb 0xd235
movb $0x7f, 0x25(%r14)
jmp 0xd239
movb %al, 0x25(%r14)
movq %r8, 0x18(%r14)
movl %r13d, 0x3c(%r14)
xorl %r12d, %r12d
jmp 0xd45a
testq %r8, %r8
je 0xd25d
movq 0x68(%r8), %rdi
movl %esi, %r15d
callq 0xdcd4
movl %r15d, %esi
movq $0x0, 0x18(%r14)
movl %esi, %r12d
jmp 0xd45a
jne 0xd2e5
testb $0x10, 0x26(%r14)
jne 0xd2e5
movl %r9d, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
leaq -0x3c(%rbp), %rsi
callq 0x203ba
xorl %r12d, %r12d
testl %eax, %eax
je 0xd2a4
movl %eax, %esi
movl $0x2, %ecx
xorl %eax, %eax
movq -0x38(%rbp), %r8
jmp 0xd2d9
cmpl $0x0, -0x3c(%rbp)
movq -0x38(%rbp), %r8
je 0xd2b9
movl $0x1a, %esi
xorl %ecx, %ecx
movb $0x1, %al
jmp 0xd2d9
movl $0x1, %ecx
testq %r8, %r8
je 0xd2d5
movq 0x68(%r8), %rdi
callq 0xdcd4
movl $0x1, %ecx
movq -0x38(%rbp), %r8
xorl %eax, %eax
xorl %esi, %esi
testb %al, %al
movl -0x30(%rbp), %r9d
je 0xd1cb
movzwl 0x15(%r15), %eax
xorl $0x2040, %eax           # imm = 0x2040
movzbl 0x17(%r15), %ecx
xorl $0x20, %ecx
xorl %r12d, %r12d
orw %ax, %cx
je 0xd309
movl $0x2, %ecx
jmp 0xd1cb
movzbl 0x10(%r15), %eax
shll $0x8, %eax
movzbl 0x11(%r15), %ecx
shll $0x10, %ecx
leal (%rcx,%rax), %edx
addl $0xfffeffff, %edx       # imm = 0xFFFEFFFF
cmpl $0xffff0100, %edx       # imm = 0xFFFF0100
jb 0xd2ff
movl %ecx, %edx
orl %eax, %edx
addl %ecx, %eax
decl %eax
andl %edx, %eax
jne 0xd2ff
movzbl 0x14(%r15), %edi
movl %edx, %ecx
subl %edi, %ecx
movq -0x48(%rbp), %rax
cmpl (%rax), %edx
jne 0xd390
movq 0x8(%r14), %rax
testb $0x1, 0x2e(%rax)
jne 0xd3f8
cmpl %r9d, %r13d
jle 0xd3f8
xorl %r12d, %r12d
movl $0xb, %edi
leaq 0x6c4a7(%rip), %rsi     # 0x79814
movl $0xcb54, %edx           # imm = 0xCB54
leaq 0x6c1c0(%rip), %rcx     # 0x79539
xorl %eax, %eax
movq %r8, %r15
callq 0xb8cc
movl $0xb, %esi
movq %r15, %r8
jmp 0xd2ff
movl %edi, -0x30(%rbp)
testq %r8, %r8
movq %r8, %r15
je 0xd3b8
movq 0x68(%r15), %rdi
movl %edx, %r12d
movq %r15, -0x38(%rbp)
movl %ecx, %r15d
callq 0xdcd4
movl %r15d, %ecx
movq -0x38(%rbp), %r15
movl %r12d, %edx
movl %ecx, 0x34(%r14)
movl %edx, 0x30(%r14)
movq 0x80(%r14), %rdi
callq 0x1cf8c
movq $0x0, 0x80(%r14)
movq (%r14), %rdi
movq -0x48(%rbp), %rsi
movl -0x30(%rbp), %edx
callq 0x1a405
movl %eax, %esi
movl $0x1, %ecx
movl %eax, %r12d
movq %r15, %r8
jmp 0xd1cb
cmpl $0x1e0, %ecx            # imm = 0x1E0
jb 0xd2ff
movl %edx, 0x30(%r14)
movl %ecx, 0x34(%r14)
movl 0x34(%r15), %eax
bswapl %eax
testl %eax, %eax
setne 0x21(%r14)
movl 0x40(%r15), %eax
bswapl %eax
testl %eax, %eax
setne 0x22(%r14)
xorl %ecx, %ecx
xorl %r12d, %r12d
jmp 0xd1cb
movq (%r14), %rdi
callq 0x1ff1b
testl %eax, %eax
jne 0xd457
movq %r14, %rdi
movl $0x1, %esi
leaq -0x50(%rbp), %rdx
xorl %ecx, %ecx
callq 0x2035c
testl %eax, %eax
je 0xd165
movl %eax, %r12d
testl %r12d, %r12d
jne 0xd469
cmpq $0x0, 0x18(%r14)
je 0xd430
xorl %r12d, %r12d
movl -0x2c(%rbp), %r15d
testl %r15d, %r15d
je 0xd6a9
testl %r12d, %r12d
jne 0xd6a9
movl $0x8, %r12d
testb $0x1, 0x26(%r14)
jne 0xd6a9
movq (%r14), %r13
movl 0x2c(%r13), %r12d
testl %r12d, %r12d
jne 0xd699
movq (%rbx), %rax
cmpb $0x2, 0x4f(%rax)
sete 0x18(%r13)
xorl %r12d, %r12d
cmpb $0x1, 0x13(%r13)
jne 0xd699
movq 0x118(%r13), %rax
testq %rax, %rax
je 0xd56e
cmpb $0x0, 0x8(%r13)
je 0xd4db
cmpb $0x0, 0x3f(%rax)
je 0xd608
movq 0x118(%r13), %r15
movl $0x8, %r12d
cmpb $0x0, 0x42(%r15)
jne 0xd674
xorl %r12d, %r12d
cmpb $0x0, 0x3f(%r15)
jne 0xd516
movq 0x8(%r15), %rdi
movq (%rdi), %rax
xorl %esi, %esi
movl $0x1, %edx
movl $0xa, %ecx
callq *0x70(%rax)
movl %eax, %r12d
testl %r12d, %r12d
jne 0xd674
movb $0x1, 0x40(%r15)
leaq 0x48(%r15), %rdi
movq 0x30(%r15), %rax
movq (%rax), %rsi
movl $0x30, %edx
callq 0x8410
testl %eax, %eax
je 0xd671
cmpb $0x0, 0x3f(%r15)
jne 0xd55e
movq 0x8(%r15), %rdi
movq (%rdi), %rax
xorl %esi, %esi
movl $0x1, %edx
movl $0x9, %ecx
callq *0x70(%rax)
movb $0x0, 0x40(%r15)
movl $0x205, %r12d           # imm = 0x205
jmp 0xd674
movzbl 0x14(%r13), %eax
cmpl $0x5, %eax
ja 0xd5a4
movl $0x23, %ecx
btl %eax, %ecx
jae 0xd5a4
movq 0x48(%r13), %rdi
movq (%rdi), %rax
movl $0x2, %esi
callq *0x38(%rax)
movl %eax, %r12d
testl %eax, %eax
jne 0xd5a7
cmpb $0x5, 0x14(%r13)
je 0xd5a4
movb $0x2, 0x14(%r13)
xorl %r12d, %r12d
cmpl $0x2, %r15d
jl 0xd674
testl %r12d, %r12d
jne 0xd674
movzbl 0x14(%r13), %eax
xorl %r12d, %r12d
cmpl $0x4, %eax
jb 0xd5cc
cmpl $0x5, %eax
jne 0xd5ea
movq 0x48(%r13), %rdi
movq (%rdi), %rax
movl $0x4, %esi
callq *0x38(%rax)
movl %eax, %r12d
testl %eax, %eax
jne 0xd5ea
movb $0x4, 0x14(%r13)
xorl %r12d, %r12d
cmpl $0x5, %r12d
jne 0xd674
movq 0xe8(%r13), %rdi
callq *0xe0(%r13)
testl %eax, %eax
jne 0xd5ba
jmp 0xd674
movzbl 0x14(%r13), %eax
xorl %r12d, %r12d
cmpl $0x4, %eax
jb 0xd61a
cmpl $0x5, %eax
jne 0xd638
movq 0x48(%r13), %rdi
movq (%rdi), %rax
movl $0x4, %esi
callq *0x38(%rax)
movl %eax, %r12d
testl %eax, %eax
jne 0xd638
movb $0x4, 0x14(%r13)
xorl %r12d, %r12d
testl %r12d, %r12d
jne 0xd699
movq 0x118(%r13), %r15
cmpb $0x0, 0x3f(%r15)
jne 0xd667
movswl 0x3c(%r15), %esi
addl $0x3, %esi
movq 0x8(%r15), %rdi
movq (%rdi), %rax
movl $0x1, %edx
movl $0x5, %ecx
callq *0x70(%rax)
movb $0x1, 0x3f(%r15)
jmp 0xd4db
xorl %r12d, %r12d
testl %r12d, %r12d
jne 0xd699
movb $0x2, 0x13(%r13)
movl 0x1c(%r13), %eax
movl %eax, 0x28(%r13)
movl %eax, 0x24(%r13)
movl %eax, 0x20(%r13)
movq $0x0, 0x60(%r13)
xorl %r12d, %r12d
testl %r12d, %r12d
jne 0xd6a9
movq %r14, %rdi
callq 0x1fcda
movl %eax, %r12d
testl %r12d, %r12d
je 0xd6cf
cmpb $0x0, 0x24(%r14)
jne 0xd6cf
movq 0x18(%r14), %rax
testq %rax, %rax
je 0xd6cf
movq 0x68(%rax), %rdi
callq 0xdcd4
movq $0x0, 0x18(%r14)
cmpb $0x5, %r12b
jne 0xd723
cmpb $0x0, 0x24(%r14)
jne 0xd723
movq 0x8(%r14), %r15
movq 0x2a8(%r15), %rax
testq %rax, %rax
je 0xd723
movl 0x2b8(%r15), %esi
testl %esi, %esi
js 0xd723
movq 0x2b0(%r15), %rdi
callq *%rax
testl %eax, %eax
je 0xd70f
movl 0x2b8(%r15), %ecx
incl %ecx
jmp 0xd714
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, 0x2b8(%r15)
testl %eax, %eax
jne 0xd155
testl %r12d, %r12d
movl -0x2c(%rbp), %r15d
jne 0xd7c8
cmpb $0x0, 0x10(%rbx)
jne 0xd754
incl 0x38(%r14)
cmpb $0x0, 0x11(%rbx)
je 0xd754
leaq 0x30(%rbx), %rax
movb $0x1, 0x3c(%rbx)
movq 0x70(%r14), %rcx
movq %rcx, 0x40(%rbx)
movq %rax, 0x70(%r14)
cmpl $0x1, %r15d
movb $0x1, %al
sbbb $-0x1, %al
movb %al, 0x10(%rbx)
cmpb 0x24(%r14), %al
jbe 0xd769
movb %al, 0x24(%r14)
xorl %r12d, %r12d
testl %r15d, %r15d
je 0xd7c8
movl %r15d, %edx
movq 0x18(%r14), %r15
movq %rbx, 0x78(%r14)
movzwl 0x26(%r14), %eax
andl $-0x21, %eax
xorl %ecx, %ecx
cmpl $0x2, %edx
setge %cl
shll $0x5, %ecx
orl %eax, %ecx
movw %cx, 0x26(%r14)
movq 0x50(%r15), %rax
movl 0x1c(%rax), %eax
bswapl %eax
cmpl %eax, 0x3c(%r14)
je 0xd7c4
movq 0x68(%r15), %rdi
callq 0xdebf
testl %eax, %eax
je 0xd7b7
movl %eax, %r12d
jmp 0xd7c4
movq 0x50(%r15), %rax
movl 0x3c(%r14), %ecx
bswapl %ecx
movl %ecx, 0x1c(%rax)
movl -0x2c(%rbp), %r15d
testl %r15d, %r15d
je 0xd7f1
testl %r12d, %r12d
jne 0xd7f1
movq (%r14), %rdi
movq (%rbx), %rax
movl 0x30c(%rax), %esi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1fda5
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq