// File: /workspace/github_c_makefile_star2_O1/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: sqlite3BtreeBeginTrans
SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){
  BtShared *pBt = p->pBt;
  Pager *pPager = pBt->pPager;
  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( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );

  if( (p->db->flags & SQLITE_ResetDatabase)
   && sqlite3PagerIsreadonly(pPager)==0
  ){
    pBt->btsFlags &= ~BTS_READ_ONLY;
  }

  /* 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
  {
    sqlite3 *pBlock = 0;
    /* 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, SCHEMA_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 {
    sqlite3PagerWalDb(pPager, p->db);

#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
    /* If transitioning from no transaction directly to a write transaction,
    ** block for the WRITER lock first if possible. */
    if( pBt->pPage1==0 && wrflag ){
      assert( pBt->inTransaction==TRANS_NONE );
      rc = sqlite3PagerWalWriteLock(pPager, 1);
      if( rc!=SQLITE_BUSY && rc!=SQLITE_OK ) break;
    }
#endif

    /* 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(pPager, wrflag>1, sqlite3TempInMemory(p->db));
        if( rc==SQLITE_OK ){
          rc = newDatabase(pBt);
        }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){
          /* if there was no transaction opened when this function was
          ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error
          ** code to SQLITE_BUSY. */
          rc = SQLITE_BUSY;
        }
      }
    }

    if( rc!=SQLITE_OK ){
      (void)sqlite3PagerWalWriteLock(pPager, 0);
      unlockBtreeIfUnused(pBt);
    }
  }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
          btreeInvokeBusyHandler(pBt) );
  sqlite3PagerWalDb(pPager, 0);
#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
  if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY;
#endif

  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 ){
    if( pSchemaVersion ){
      *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]);
    }
    if( 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(pPager, p->db->nSavepoint);
    }
  }

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdx, 0x28(%rsp)
movl %esi, %ebp
movq %rdi, %r14
movq (%rdi), %rdi
movq 0x8(%r14), %r12
movq (%r12), %rbx
movq %rdi, 0x8(%r12)
movb 0x10(%r14), %al
xorl %r13d, %r13d
cmpb $0x2, %al
je 0x280af9
cmpb $0x1, %al
sete %al
testl %ebp, %ebp
sete %cl
testb %al, %cl
jne 0x280af9
testb $0x2, 0x33(%rdi)
je 0x2803ca
cmpb $0x0, 0x12(%rbx)
jne 0x2803ca
andb $-0x2, 0x28(%r12)
movzwl 0x28(%r12), %eax
testl %ebp, %ebp
je 0x2803e5
movl %eax, %ecx
movl $0x8, %r13d
andl $0x1, %ecx
jne 0x280af9
testl %ebp, %ebp
je 0x28041a
testb %al, %al
js 0x28041e
cmpb $0x2, 0x24(%r12)
je 0x28041e
cmpl $0x2, %ebp
jl 0x28042b
movq 0x78(%r12), %rax
testq %rax, %rax
je 0x28042b
xorl %r15d, %r15d
movq (%rax), %rcx
cmpq %r14, %rcx
jne 0x280426
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0x280407
jmp 0x28042e
testb %al, %al
jns 0x2803f5
movq 0x80(%r12), %rcx
movq (%rcx), %r15
jmp 0x28042e
xorl %r15d, %r15d
testq %r15, %r15
je 0x280443
movq %r15, %rsi
callq 0x296509
movl $0x106, %r13d           # imm = 0x106
jmp 0x280446
xorl %r13d, %r13d
testq %r15, %r15
jne 0x280af9
movq %r14, %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0x296571
testl %eax, %eax
je 0x28046d
movl %eax, %r13d
jmp 0x280af9
movq %rbx, (%rsp)
movzwl 0x28(%r12), %eax
movl %eax, %ecx
andl $-0x11, %ecx
movw %cx, 0x28(%r12)
cmpl $0x0, 0x40(%r12)
jne 0x280493
orl $0x10, %eax
movw %ax, 0x28(%r12)
leaq 0x34(%r12), %rax
movq %rax, 0x20(%rsp)
xorl %r13d, %r13d
cmpq $0x0, 0x18(%r12)
jne 0x2807e2
jmp 0x2807a6
movq 0x30(%rsp), %r8
movq 0x50(%r8), %rbx
movl 0x1c(%rbx), %r15d
bswapl %r15d
movq (%r12), %rdi
movl 0x1c(%rdi), %r9d
testl %r15d, %r15d
je 0x2804d6
movl 0x18(%rbx), %eax
cmpl 0x5c(%rbx), %eax
je 0x2804d9
movl %r9d, %r15d
movq 0x8(%r12), %rax
testb $0x2, 0x33(%rax)
movl $0x0, %eax
cmovnel %eax, %r15d
testl %r15d, %r15d
je 0x280539
movdqu (%rbx), %xmm0
pcmpeqb 0x15ea94(%rip), %xmm0   # 0x3def90
pmovmskb %xmm0, %ecx
movl $0x1a, %eax
cmpl $0xffff, %ecx           # imm = 0xFFFF
jne 0x280523
cmpb $0x3, 0x12(%rbx)
jb 0x280519
orb $0x1, 0x28(%r12)
cmpb $0x2, 0x13(%rbx)
jbe 0x2805cc
movl $0x2, %edx
cmpl $0x2, %edx
je 0x2805b3
testl %edx, %edx
jne 0x2807d2
movl 0x38(%r12), %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, 0x2a(%r12)
movl %ecx, %edx
shll $0x5, %edx
addl $0xfffffe80, %edx       # imm = 0xFFFFFE80
imulq %rsi, %rdx
shrq $0x27, %rdx
addl $-0x17, %edx
movw %dx, 0x2c(%r12)
addl $-0x23, %ecx
movw %cx, 0x2e(%r12)
movw %dx, 0x30(%r12)
movzwl %ax, %ecx
cmpl $0x80, %ecx
jb 0x28059c
movb $0x7f, 0x25(%r12)
jmp 0x2805a1
movb %al, 0x25(%r12)
movq %r8, 0x18(%r12)
movl %r15d, 0x40(%r12)
xorl %r13d, %r13d
jmp 0x2807d2
movq %r8, %rdi
movl %eax, %r13d
callq 0x296c8e
movq $0x0, 0x18(%r12)
jmp 0x2807d2
jne 0x280641
testb $0x20, 0x28(%r12)
jne 0x280641
movl %r9d, 0xc(%rsp)
movq %r8, 0x10(%rsp)
movl $0x0, 0x1c(%rsp)
leaq 0x1c(%rsp), %rsi
callq 0x296c12
xorl %r13d, %r13d
testl %eax, %eax
je 0x280607
movl $0x2, %edx
xorl %ecx, %ecx
movq 0x10(%rsp), %r8
jmp 0x280634
cmpl $0x0, 0x1c(%rsp)
movq 0x10(%rsp), %r8
je 0x28061e
movl $0x1a, %eax
xorl %edx, %edx
movb $0x1, %cl
jmp 0x280634
movq %r8, %rdi
callq 0x296c8e
movq 0x10(%rsp), %r8
xorl %ecx, %ecx
movl $0x1, %edx
xorl %eax, %eax
testb %cl, %cl
movl 0xc(%rsp), %r9d
je 0x280528
movzwl 0x15(%rbx), %edx
xorl $0x2040, %edx           # imm = 0x2040
movzbl 0x17(%rbx), %ecx
xorl $0x20, %ecx
xorl %r13d, %r13d
orw %dx, %cx
je 0x280664
movl $0x2, %edx
jmp 0x280528
movzbl 0x10(%rbx), %esi
shll $0x8, %esi
movzbl 0x11(%rbx), %ecx
shll $0x10, %ecx
leal (%rcx,%rsi), %edx
addl $0xfffeffff, %edx       # imm = 0xFFFEFFFF
cmpl $0xffff0100, %edx       # imm = 0xFFFF0100
jb 0x28065a
movl %ecx, %edi
orl %esi, %edi
addl %esi, %ecx
decl %ecx
andl %edi, %ecx
jne 0x28065a
orb $0x2, 0x28(%r12)
movzbl 0x14(%rbx), %ecx
movl %edi, %esi
subl %ecx, %esi
cmpl 0x34(%r12), %edi
jne 0x2806fe
movq 0x8(%r12), %rcx
movl 0x30(%rcx), %edx
movl $0x10000001, %ecx       # imm = 0x10000001
andl %ecx, %edx
cmpl $0x1, %edx
je 0x28076c
cmpl %r9d, %r15d
jbe 0x28076c
xorl %r13d, %r13d
movl $0xb, %edi
leaq 0x15ec4b(%rip), %rsi    # 0x3df31f
leaq 0x15ed55(%rip), %rdx    # 0x3df430
movl $0x10855, %ecx          # imm = 0x10855
movq %r8, %rbx
leaq 0x15ea9b(%rip), %r8     # 0x3df185
xorl %eax, %eax
callq 0x27e637
movl $0xb, %eax
movq %rbx, %r8
jmp 0x28065a
movl %ecx, 0xc(%rsp)
movq %r8, 0x10(%rsp)
movl %edi, %r13d
movq %r8, %rdi
movl %esi, %ebx
callq 0x296c8e
movl %ebx, 0x38(%r12)
movl %r13d, 0x34(%r12)
movq 0x88(%r12), %rdi
testq %rdi, %rdi
je 0x280748
addq $-0x4, %rdi
movq %rdi, 0x88(%r12)
callq 0x292b46
movq $0x0, 0x88(%r12)
movq (%r12), %rdi
movq 0x20(%rsp), %rsi
movl 0xc(%rsp), %edx
callq 0x28f528
movl $0x1, %edx
movl %eax, %r13d
movq 0x10(%rsp), %r8
jmp 0x280528
cmpl $0x1e0, %esi            # imm = 0x1E0
jb 0x28065a
movl %edi, 0x34(%r12)
movl %esi, 0x38(%r12)
movl 0x34(%rbx), %ecx
bswapl %ecx
testl %ecx, %ecx
setne 0x21(%r12)
movl 0x40(%rbx), %ecx
bswapl %ecx
testl %ecx, %ecx
setne 0x22(%r12)
xorl %edx, %edx
xorl %r13d, %r13d
jmp 0x280528
movq (%r12), %rdi
callq 0x2966ac
testl %eax, %eax
jne 0x2807cf
movq %r12, %rdi
movl $0x1, %esi
leaq 0x30(%rsp), %rdx
xorl %ecx, %ecx
callq 0x296bb1
testl %eax, %eax
je 0x2804b1
movl %eax, %r13d
testl %r13d, %r13d
jne 0x2807e2
cmpq $0x0, 0x18(%r12)
je 0x2807a6
xorl %r13d, %r13d
testl %ebp, %ebp
movq (%rsp), %r15
je 0x2809d0
testl %r13d, %r13d
jne 0x2809d0
movl $0x8, %r13d
testb $0x1, 0x28(%r12)
jne 0x2809d0
movl 0x2c(%r15), %r13d
testl %r13d, %r13d
jne 0x2809a0
movq (%r14), %rax
cmpb $0x2, 0x60(%rax)
sete 0x19(%r15)
xorl %r13d, %r13d
cmpb $0x1, 0x14(%r15)
jne 0x2809a0
movq 0x120(%r15), %rax
testq %rax, %rax
je 0x2808e3
cmpb $0x0, 0x8(%r15)
je 0x280851
cmpb $0x0, 0x3f(%rax)
je 0x28092b
movq 0x120(%r15), %rbx
movl $0x8, %r13d
cmpb $0x0, 0x42(%rbx)
jne 0x28097b
xorl %r13d, %r13d
cmpb $0x0, 0x3f(%rbx)
jne 0x28088a
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
xorl %esi, %esi
movl $0x1, %edx
movl $0xa, %ecx
callq *0x70(%rax)
movl %eax, %r13d
testl %r13d, %r13d
movq (%rsp), %r15
jne 0x28097b
movb $0x1, 0x40(%rbx)
leaq 0x48(%rbx), %rdi
movq 0x30(%rbx), %rax
movq (%rax), %rsi
movl $0x30, %edx
callq 0x17830
testl %eax, %eax
je 0x280974
cmpb $0x0, 0x3f(%rbx)
jne 0x2808d4
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
xorl %esi, %esi
movl $0x1, %edx
movl $0x9, %ecx
callq *0x70(%rax)
movb $0x0, 0x40(%rbx)
movl $0x205, %r13d           # imm = 0x205
jmp 0x280977
cmpl $0x2, %ebp
setl %bl
movq %r15, %rdi
movl $0x2, %esi
callq 0x292a65
movl %eax, %r13d
testl %eax, %eax
setne %al
orb %bl, %al
jne 0x28097b
movq %r15, %rdi
movl $0x4, %esi
callq 0x292a65
movl %eax, %r13d
cmpl $0x5, %eax
jne 0x28097b
movq 0xe8(%r15), %rdi
callq *0xe0(%r15)
testl %eax, %eax
jne 0x280902
jmp 0x28097b
movq %r15, %rdi
movl $0x4, %esi
callq 0x292a65
movl %eax, %r13d
testl %eax, %eax
jne 0x2809a0
movq 0x120(%r15), %rbx
cmpb $0x0, 0x3f(%rbx)
jne 0x280967
movswl 0x3c(%rbx), %esi
addl $0x3, %esi
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x1, %edx
movl $0x5, %ecx
callq *0x70(%rax)
movb $0x1, 0x3f(%rbx)
movq (%rsp), %r15
jmp 0x280851
xorl %r13d, %r13d
movq (%rsp), %r15
testl %r13d, %r13d
jne 0x2809a0
movb $0x2, 0x14(%r15)
movl 0x1c(%r15), %eax
movl %eax, 0x28(%r15)
movl %eax, 0x24(%r15)
movl %eax, 0x20(%r15)
movq $0x0, 0x60(%r15)
xorl %r13d, %r13d
cmpl $0x205, %r13d           # imm = 0x205
je 0x2809bb
testl %r13d, %r13d
jne 0x2809d0
movq %r12, %rdi
callq 0x2965eb
movl %eax, %r13d
jmp 0x2809d0
xorl %r13d, %r13d
cmpb $0x0, 0x24(%r12)
setne %r13b
shll $0x9, %r13d
orl $0x5, %r13d
testl %r13d, %r13d
je 0x2809f5
cmpb $0x0, 0x24(%r12)
jne 0x2809f5
movq 0x18(%r12), %rdi
testq %rdi, %rdi
je 0x2809f5
movq $0x0, 0x18(%r12)
callq 0x296c8e
cmpb $0x5, %r13b
jne 0x280a48
cmpb $0x0, 0x24(%r12)
jne 0x280a48
movq 0x8(%r12), %rbx
movq 0x250(%rbx), %rax
testq %rax, %rax
je 0x280a48
movl 0x260(%rbx), %esi
testl %esi, %esi
js 0x280a48
movq 0x258(%rbx), %rdi
callq *%rax
testl %eax, %eax
je 0x280a35
movl 0x260(%rbx), %ecx
incl %ecx
jmp 0x280a3a
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, 0x260(%rbx)
testl %eax, %eax
jne 0x2804a0
testl %r13d, %r13d
movq (%rsp), %rbx
jne 0x280af9
cmpb $0x0, 0x10(%r14)
jne 0x280a7f
incl 0x3c(%r12)
cmpb $0x0, 0x11(%r14)
je 0x280a7f
leaq 0x30(%r14), %rax
movb $0x1, 0x3c(%r14)
movq 0x78(%r12), %rcx
movq %rcx, 0x40(%r14)
movq %rax, 0x78(%r12)
cmpl $0x1, %ebp
movb $0x1, %al
sbbb $-0x1, %al
movb %al, 0x10(%r14)
cmpb 0x24(%r12), %al
jbe 0x280a96
movb %al, 0x24(%r12)
xorl %r13d, %r13d
testl %ebp, %ebp
je 0x280af5
movq 0x18(%r12), %rbx
movq %r14, 0x80(%r12)
movl 0x28(%r12), %eax
andl $-0x41, %eax
xorl %ecx, %ecx
cmpl $0x2, %ebp
setge %cl
shll $0x6, %ecx
orl %eax, %ecx
movw %cx, 0x28(%r12)
movq 0x50(%rbx), %rax
movl 0x1c(%rax), %eax
bswapl %eax
cmpl %eax, 0x40(%r12)
je 0x280af5
movq 0x70(%rbx), %rdi
callq 0x280e3a
testl %eax, %eax
je 0x280ae7
movl %eax, %r13d
jmp 0x280af5
movq 0x50(%rbx), %rax
movl 0x40(%r12), %ecx
bswapl %ecx
movl %ecx, 0x1c(%rax)
movq (%rsp), %rbx
testl %r13d, %r13d
je 0x280b10
movl %r13d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x28(%rsp), %rcx
testq %rcx, %rcx
je 0x280b2a
movq 0x18(%r12), %rax
movq 0x50(%rax), %rax
movl 0x28(%rax), %eax
bswapl %eax
movl %eax, (%rcx)
xorl %r13d, %r13d
testl %ebp, %ebp
je 0x280afe
movq (%r14), %rax
movl 0x2b8(%rax), %esi
cmpl %esi, 0x80(%rbx)
jge 0x280afe
cmpb $0x0, 0xa(%rbx)
je 0x280afe
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x29704a