// File: /workspace/github_c_makefile_star2_O2/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
cmpb $0x2, %al
jne 0xc4d8
testl %r15d, %r15d
je 0xca30
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 0x1d236
cmpb $0x1, %al
sete %al
testl %r15d, %r15d
sete %cl
testb %al, %cl
jne 0xca30
movzwl 0x26(%r14), %eax
testl %r15d, %r15d
je 0xc505
movl %eax, %ecx
andl $0x1, %ecx
je 0xc505
pushq $0x8
popq %r12
jmp 0xca33
testl %r15d, %r15d
je 0xc535
testb $0x40, %al
jne 0xc539
cmpb $0x2, 0x24(%r14)
je 0xc539
cmpl $0x2, %r15d
jl 0xc54d
leaq 0x70(%r14), %rax
movq (%rax), %rax
testq %rax, %rax
je 0xc54d
movq (%rax), %rcx
addq $0x10, %rax
cmpq %rbx, %rcx
je 0xc51f
jmp 0xc53d
testb $0x40, %al
je 0xc54d
movq 0x78(%r14), %rcx
movl $0x106, %r12d           # imm = 0x106
cmpq $0x0, (%rcx)
jne 0xca33
pushq $0x1
popq %rdx
movq %rbx, %rdi
movl %edx, %esi
callq 0x1d0ea
testl %eax, %eax
je 0xc566
movl %eax, %r12d
jmp 0xca33
movzwl 0x26(%r14), %eax
movl %eax, %ecx
andl $-0x9, %ecx
movw %cx, 0x26(%r14)
cmpl $0x0, 0x3c(%r14)
jne 0xc584
orl $0x8, %eax
movw %ax, 0x26(%r14)
leaq 0x30(%r14), %rax
movq %rax, -0x48(%rbp)
xorl %eax, %eax
cmpq $0x0, 0x18(%r14)
je 0xc666
testl %r15d, %r15d
je 0xc7f5
testl %eax, %eax
jne 0xc7f5
testb $0x1, 0x26(%r14)
pushq $0x8
popq %r12
jne 0xc800
movq (%r14), %r13
movl 0x2c(%r13), %r12d
testl %r12d, %r12d
jne 0xc800
movq (%rbx), %rax
cmpb $0x2, 0x4f(%rax)
sete 0x18(%r13)
cmpb $0x1, 0x13(%r13)
jne 0xc7ed
movq 0x118(%r13), %rdi
testq %rdi, %rdi
je 0xc7a0
cmpb $0x0, 0x8(%r13)
je 0xc601
cmpb $0x0, 0x3f(%rdi)
je 0xc837
cmpb $0x0, 0x42(%rdi)
pushq $0x8
popq %r12
jne 0xc800
xorl %esi, %esi
pushq $0x1
popq %rdx
movq %rdi, -0x30(%rbp)
callq 0x1961e
testl %eax, %eax
jne 0xc687
movq -0x30(%rbp), %r12
movb $0x1, 0x40(%r12)
leaq 0x48(%r12), %rdi
movq 0x30(%r12), %rax
movq (%rax), %rsi
pushq $0x30
popq %rdx
callq 0x83e0
testl %eax, %eax
je 0xc7d3
movq %r12, %rdi
xorl %esi, %esi
pushq $0x1
popq %rdx
callq 0x1963f
movb $0x0, 0x40(%r12)
movl $0x205, %r12d           # imm = 0x205
jmp 0xc800
movq (%r14), %rdi
callq 0x1d350
testl %eax, %eax
jne 0xc687
movq %r14, %rdi
pushq $0x1
popq %rsi
leaq -0x50(%rbp), %rdx
xorl %ecx, %ecx
callq 0x1d84e
testl %eax, %eax
je 0xc68f
movl %eax, %r12d
jmp 0xc800
movq -0x50(%rbp), %rax
movq %rax, -0x30(%rbp)
movq 0x50(%rax), %r13
leaq 0x1c(%r13), %rdi
callq 0x180a3
movq (%r14), %rdi
movl 0x1c(%rdi), %ecx
testl %eax, %eax
je 0xc6bb
movl %eax, %r8d
movl 0x18(%r13), %eax
cmpl 0x5c(%r13), %eax
je 0xc6be
movl %ecx, %r8d
testl %r8d, %r8d
jle 0xc730
movdqu (%r13), %xmm0
pcmpeqb 0x5ecbf(%rip), %xmm0    # 0x6b390
pmovmskb %xmm0, %eax
pushq $0x1a
popq %r12
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0xc88c
cmpb $0x3, 0x12(%r13)
jb 0xc6f0
orb $0x1, 0x26(%r14)
cmpb $0x2, 0x13(%r13)
ja 0xc88c
movl %ecx, -0x3c(%rbp)
movl %r8d, -0x34(%rbp)
jne 0xc86f
testb $0x10, 0x26(%r14)
jne 0xc86f
andl $0x0, -0x38(%rbp)
leaq -0x38(%rbp), %rsi
callq 0x1d8aa
testl %eax, %eax
je 0xc865
movl %eax, %r12d
jmp 0xc88c
movl 0x34(%r14), %edi
movl %edi, %eax
shll $0x6, %eax
addl $0xfffffd00, %eax       # imm = 0xFFFFFD00
xorl %r12d, %r12d
xorl %edx, %edx
movl $0xff, %esi
divl %esi
movl %eax, %ecx
addl $-0x17, %ecx
movw %cx, 0x28(%r14)
movl %edi, %eax
shll $0x5, %eax
addl $0xfffffe80, %eax       # imm = 0xFFFFFE80
xorl %edx, %edx
divl %esi
addl $-0x17, %eax
movw %ax, 0x2a(%r14)
addl $-0x23, %edi
movw %di, 0x2c(%r14)
cmpw $0x7f, %cx
pushq $0x7f
popq %rdx
cmovael %edx, %ecx
movw %ax, 0x2e(%r14)
movb %cl, 0x25(%r14)
movq -0x30(%rbp), %rax
movq %rax, 0x18(%r14)
movl %r8d, 0x3c(%r14)
movl $0x0, %eax
testl %r12d, %r12d
jne 0xc800
jmp 0xc58e
cmpl $0x2, %r15d
setl -0x30(%rbp)
movq %r13, %rdi
pushq $0x2
popq %rsi
callq 0x1a3e8
movl %eax, %r12d
testl %eax, %eax
setne %al
orb -0x30(%rbp), %al
jne 0xc7ce
movq %r13, %rdi
pushq $0x4
popq %rsi
callq 0x1a3a7
movl %eax, %r12d
testl %r12d, %r12d
jne 0xc800
movb $0x2, 0x13(%r13)
movl 0x1c(%r13), %eax
movl %eax, 0x28(%r13)
movl %eax, 0x24(%r13)
movl %eax, 0x20(%r13)
andq $0x0, 0x60(%r13)
movq %r14, %rdi
callq 0x1d141
movl %eax, %r12d
testl %eax, %eax
je 0xc992
movq %r14, %rdi
callq 0x1d20c
cmpb $0x5, %r12b
jne 0xca33
cmpb $0x0, 0x24(%r14)
jne 0xca33
movq %r14, %rdi
callq 0x1805a
movl %eax, %ecx
movl %r12d, %eax
testl %ecx, %ecx
jne 0xc58e
jmp 0xca33
movq %r13, %rdi
pushq $0x4
popq %rsi
callq 0x1a3e8
testl %eax, %eax
jne 0xc687
movq 0x118(%r13), %rdi
pushq $0x1
popq %rsi
callq 0x1d08e
movq 0x118(%r13), %rdi
jmp 0xc601
cmpl $0x0, -0x38(%rbp)
je 0xc981
leaq 0x15(%r13), %rdi
leaq 0x5efff(%rip), %rsi     # 0x6b879
pushq $0x3
popq %rdx
callq 0x83e0
testl %eax, %eax
movl -0x34(%rbp), %esi
movl -0x3c(%rbp), %edi
je 0xc89f
movq -0x30(%rbp), %rdi
callq 0x1d90e
andq $0x0, 0x18(%r14)
jmp 0xc800
movzbl 0x10(%r13), %eax
shll $0x8, %eax
movzbl 0x11(%r13), %ecx
shll $0x10, %ecx
leal (%rcx,%rax), %edx
addl $0xfffeffff, %edx       # imm = 0xFFFEFFFF
cmpl $0xffff0100, %edx       # imm = 0xFFFF0100
jb 0xc88c
movl %ecx, %edx
orl %eax, %edx
addl %ecx, %eax
decl %eax
andl %edx, %eax
jne 0xc88c
movzbl 0x14(%r13), %r12d
movl %edx, %ecx
subl %r12d, %ecx
movq -0x48(%rbp), %rax
cmpl (%rax), %edx
jne 0xc8fc
movq 0x8(%r14), %rax
testb $0x1, 0x2e(%rax)
jne 0xc935
cmpl %edi, %esi
jle 0xc935
movl $0xcb54, %edi           # imm = 0xCB54
callq 0x1a1fc
pushq $0xb
popq %r12
jmp 0xc88c
movq -0x30(%rbp), %rdi
movl %ecx, %r13d
movl %edx, -0x30(%rbp)
callq 0x1d90e
movl %r13d, 0x34(%r14)
movl -0x30(%rbp), %eax
movl %eax, 0x30(%r14)
movq %r14, %rdi
callq 0x1d923
movq (%r14), %rdi
movq -0x48(%rbp), %rsi
movl %r12d, %edx
callq 0x180ad
movl %eax, %r12d
jmp 0xc791
pushq $0x1a
popq %r12
cmpl $0x1e0, %ecx            # imm = 0x1E0
jb 0xc88c
movl %ecx, %eax
movl %edx, 0x30(%r14)
movl %ecx, %r12d
movl %eax, 0x34(%r14)
leaq 0x34(%r13), %rdi
callq 0x180a3
testl %eax, %eax
setne 0x21(%r14)
addq $0x40, %r13
movq %r13, %rdi
callq 0x180a3
movl %r12d, %edi
movl -0x34(%rbp), %r8d
testl %eax, %eax
setne 0x22(%r14)
jmp 0xc734
movq -0x30(%rbp), %rdi
callq 0x1d90e
xorl %r12d, %r12d
jmp 0xc791
cmpb $0x0, 0x10(%rbx)
jne 0xc9b6
incl 0x38(%r14)
cmpb $0x0, 0x11(%rbx)
je 0xc9b6
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 0xc9cb
movb %al, 0x24(%r14)
testl %r15d, %r15d
je 0xca30
xorl %eax, %eax
cmpl $0x2, %r15d
setge %al
movq 0x18(%r14), %r15
movq %rbx, 0x78(%r14)
movzwl 0x26(%r14), %ecx
andl $-0x21, %ecx
shll $0x5, %eax
orl %ecx, %eax
movw %ax, 0x26(%r14)
movl 0x3c(%r14), %r12d
movq 0x50(%r15), %rdi
addq $0x1c, %rdi
callq 0x180a3
cmpl %eax, %r12d
je 0xc4b9
movq 0x68(%r15), %rdi
callq 0xd10b
testl %eax, %eax
jne 0xc55e
movq 0x50(%r15), %rax
movl 0x3c(%r14), %ecx
bswapl %ecx
movl %ecx, 0x1c(%rax)
jmp 0xc4b9
xorl %r12d, %r12d
movl %r12d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq