// File: /workspace/github_c_makefile_star2_O2/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, %r15
movl %esi, %ebp
movq %rdi, %rbx
movq (%rdi), %rdi
movq 0x8(%rbx), %r12
movq (%r12), %r14
movq %rdi, 0x8(%r12)
movb 0x10(%rbx), %al
cmpb $0x2, %al
je 0x2233fd
cmpb $0x1, %al
sete %al
testl %ebp, %ebp
sete %cl
testb %al, %cl
jne 0x2233fd
testb $0x2, 0x33(%rdi)
je 0x222e0e
cmpb $0x0, 0x12(%r14)
jne 0x222e0e
andb $-0x2, 0x28(%r12)
movzwl 0x28(%r12), %eax
testl %ebp, %ebp
je 0x222e28
movl %eax, %ecx
andl $0x1, %ecx
je 0x222e28
pushq $0x8
popq %r13
jmp 0x22343d
testl %ebp, %ebp
je 0x222e58
testb %al, %al
js 0x222e5c
cmpb $0x2, 0x24(%r12)
je 0x222e5c
cmpl $0x2, %ebp
jl 0x222e7c
leaq 0x78(%r12), %rax
movq (%rax), %rax
testq %rax, %rax
je 0x222e7c
movq (%rax), %rcx
addq $0x10, %rax
cmpq %rbx, %rcx
je 0x222e42
jmp 0x222e64
testb %al, %al
jns 0x222e7c
movq 0x80(%r12), %rcx
movq (%rcx), %rsi
testq %rsi, %rsi
je 0x222e7c
callq 0x235d5b
movl $0x106, %r13d           # imm = 0x106
jmp 0x22343d
pushq $0x1
popq %rdx
movq %rbx, %rdi
movl %edx, %esi
callq 0x235d90
testl %eax, %eax
je 0x222e95
movl %eax, %r13d
jmp 0x22343d
movzwl 0x28(%r12), %eax
movl %eax, %ecx
andl $-0x11, %ecx
movw %cx, 0x28(%r12)
cmpl $0x0, 0x40(%r12)
jne 0x222eb7
orl $0x10, %eax
movw %ax, 0x28(%r12)
leaq 0x34(%r12), %rax
movq %rax, 0x28(%rsp)
xorl %eax, %eax
cmpq $0x0, 0x18(%r12)
je 0x222f1b
testl %ebp, %ebp
je 0x223172
testl %eax, %eax
jne 0x223172
testb $0x1, 0x28(%r12)
pushq $0x8
popq %r13
jne 0x22317d
movl 0x2c(%r14), %r13d
testl %r13d, %r13d
je 0x222f4a
cmpl $0x205, %r13d           # imm = 0x205
jne 0x22317d
xorl %r13d, %r13d
cmpb $0x0, 0x24(%r12)
setne %r13b
shll $0x9, %r13d
orl $0x5, %r13d
jmp 0x22317d
movq (%r12), %rdi
callq 0x235f00
testl %eax, %eax
jne 0x222f42
movq %r12, %rdi
pushq $0x1
popq %rsi
leaq 0x30(%rsp), %rdx
xorl %ecx, %ecx
callq 0x23639b
testl %eax, %eax
je 0x222fe1
movl %eax, %r13d
jmp 0x22317d
movq (%rbx), %rax
cmpb $0x2, 0x60(%rax)
sete 0x19(%r14)
cmpb $0x1, 0x14(%r14)
jne 0x22316a
movq 0x120(%r14), %rdi
testq %rdi, %rdi
je 0x22311a
cmpb $0x0, 0x8(%r14)
je 0x222f82
cmpb $0x0, 0x3f(%rdi)
je 0x2231b5
cmpb $0x0, 0x42(%rdi)
pushq $0x8
popq %r13
jne 0x22317d
xorl %esi, %esi
pushq $0x1
popq %rdx
movq %rdi, (%rsp)
callq 0x231569
testl %eax, %eax
jne 0x2231c4
movq (%rsp), %r13
movb $0x1, 0x40(%r13)
leaq 0x48(%r13), %rdi
movq 0x30(%r13), %rax
movq (%rax), %rsi
pushq $0x30
popq %rdx
callq 0x177e0
testl %eax, %eax
je 0x223150
movq %r13, %rdi
xorl %esi, %esi
pushq $0x1
popq %rdx
callq 0x2315f6
movb $0x0, 0x40(%r13)
jmp 0x222f01
movq 0x30(%rsp), %rax
movq %rax, (%rsp)
movq 0x50(%rax), %r13
leaq 0x1c(%r13), %rdi
callq 0x22fc59
movq (%r12), %rdi
movl 0x1c(%rdi), %edx
testl %eax, %eax
je 0x22300f
movl %eax, %r8d
movl 0x18(%r13), %eax
cmpl 0x5c(%r13), %eax
je 0x223012
movl %edx, %r8d
movq 0x8(%r12), %rax
testb $0x2, 0x33(%rax)
movl $0x0, %eax
cmovnel %eax, %r8d
testl %r8d, %r8d
je 0x2230a2
movdqu (%r13), %xmm0
pcmpeqb 0x152b29(%rip), %xmm0   # 0x375b60
pmovmskb %xmm0, %eax
movq %r13, %rcx
pushq $0x1a
popq %r13
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x22321a
cmpb $0x3, 0x12(%rcx)
jb 0x223059
orb $0x1, 0x28(%r12)
cmpb $0x2, 0x13(%rcx)
ja 0x22321a
movl %edx, 0x24(%rsp)
movq %rcx, 0x18(%rsp)
movl %r8d, 0x10(%rsp)
jne 0x2231f2
testb $0x20, 0x28(%r12)
jne 0x2231f2
andl $0x0, 0x14(%rsp)
leaq 0x14(%rsp), %rsi
callq 0x2363dc
testl %eax, %eax
je 0x2231e7
movl %eax, %r13d
jmp 0x22321a
movl 0x38(%r12), %edi
movl %edi, %eax
shll $0x6, %eax
addl $0xfffffd00, %eax       # imm = 0xFFFFFD00
xorl %r13d, %r13d
xorl %edx, %edx
movl $0xff, %esi
divl %esi
movl %eax, %ecx
addl $-0x17, %ecx
movw %cx, 0x2a(%r12)
movl %edi, %eax
shll $0x5, %eax
addl $0xfffffe80, %eax       # imm = 0xFFFFFE80
xorl %edx, %edx
divl %esi
addl $-0x17, %eax
movw %ax, 0x2c(%r12)
addl $-0x23, %edi
movw %di, 0x2e(%r12)
cmpw $0x7f, %cx
pushq $0x7f
popq %rdx
cmovael %edx, %ecx
movw %ax, 0x30(%r12)
movb %cl, 0x25(%r12)
movq (%rsp), %rax
movq %rax, 0x18(%r12)
movl %r8d, 0x40(%r12)
movl $0x0, %eax
testl %r13d, %r13d
jne 0x22317d
jmp 0x222ec3
cmpl $0x2, %ebp
setl (%rsp)
movq %r14, %rdi
pushq $0x2
popq %rsi
callq 0x232911
movl %eax, %r13d
testl %eax, %eax
setne %al
orb (%rsp), %al
jne 0x223147
movq %r14, %rdi
pushq $0x4
popq %rsi
callq 0x2328de
movl %eax, %r13d
testl %r13d, %r13d
jne 0x222ef4
movb $0x2, 0x14(%r14)
movl 0x1c(%r14), %eax
movl %eax, 0x28(%r14)
movl %eax, 0x24(%r14)
movl %eax, 0x20(%r14)
andq $0x0, 0x60(%r14)
movq %r12, %rdi
callq 0x235e0d
movl %eax, %r13d
testl %eax, %eax
je 0x223352
movq %r12, %rdi
callq 0x235ece
cmpb $0x5, %r13b
jne 0x22343d
cmpb $0x0, 0x24(%r12)
jne 0x22343d
movq %r12, %rdi
callq 0x22fc1a
movl %eax, %ecx
movl %r13d, %eax
testl %ecx, %ecx
jne 0x222ec3
jmp 0x22343d
movq %r14, %rdi
pushq $0x4
popq %rsi
callq 0x232911
testl %eax, %eax
je 0x2231cc
movl %eax, %r13d
jmp 0x222ef4
movq 0x120(%r14), %rdi
pushq $0x1
popq %rsi
callq 0x234910
movq 0x120(%r14), %rdi
jmp 0x222f82
cmpl $0x0, 0x14(%rsp)
je 0x223341
movq 0x18(%rsp), %rax
leaq 0x15(%rax), %rdi
leaq 0x152f62(%rip), %rsi    # 0x376164
pushq $0x3
popq %rdx
callq 0x177e0
pushq $0x1a
popq %r13
testl %eax, %eax
movl 0x10(%rsp), %esi
movl 0x24(%rsp), %edi
je 0x22322e
movq (%rsp), %rdi
callq 0x236440
andq $0x0, 0x18(%r12)
jmp 0x22317d
movq 0x18(%rsp), %rcx
movzbl 0x10(%rcx), %eax
shll $0x8, %eax
movzbl 0x11(%rcx), %ecx
shll $0x10, %ecx
leal (%rcx,%rax), %edx
addl $0xfffeffff, %edx       # imm = 0xFFFEFFFF
cmpl $0xffff0100, %edx       # imm = 0xFFFF0100
jb 0x22321a
movl %ecx, %r8d
orl %eax, %r8d
addl %ecx, %eax
decl %eax
andl %r8d, %eax
jne 0x22321a
orb $0x2, 0x28(%r12)
movq 0x18(%rsp), %rax
movzbl 0x14(%rax), %r13d
movl %r8d, %edx
subl %r13d, %edx
cmpl 0x34(%r12), %r8d
jne 0x2232a5
movq 0x8(%r12), %rax
movl 0x30(%rax), %eax
movl $0x10000001, %ecx       # imm = 0x10000001
andl %ecx, %eax
cmpl $0x1, %eax
je 0x2232e8
cmpl %edi, %esi
jbe 0x2232e8
movl $0x10855, %edi          # imm = 0x10855
callq 0x232547
jmp 0x22309a
movq (%rsp), %rdi
movl %edx, 0xc(%rsp)
movl %r8d, (%rsp)
callq 0x236440
movl 0xc(%rsp), %eax
movl %eax, 0x38(%r12)
movl (%rsp), %eax
movl %eax, 0x34(%r12)
movq %r12, %rdi
callq 0x236457
movq (%r12), %rdi
movq 0x28(%rsp), %rsi
movl %r13d, %edx
callq 0x22fc5e
movl %eax, %r13d
jmp 0x22310b
pushq $0x1a
popq %r13
cmpl $0x1e0, %edx            # imm = 0x1E0
movq 0x18(%rsp), %rcx
jb 0x22321a
movl %r8d, 0x34(%r12)
movl %edx, 0xc(%rsp)
movl %edx, 0x38(%r12)
movq %rcx, %r13
leaq 0x34(%rcx), %rdi
callq 0x22fc59
testl %eax, %eax
setne 0x21(%r12)
addq $0x40, %r13
movq %r13, %rdi
callq 0x22fc59
movl 0xc(%rsp), %edi
movl 0x10(%rsp), %r8d
testl %eax, %eax
setne 0x22(%r12)
jmp 0x2230a7
movq (%rsp), %rdi
callq 0x236440
xorl %r13d, %r13d
jmp 0x22310b
cmpb $0x0, 0x10(%rbx)
jne 0x223379
incl 0x3c(%r12)
cmpb $0x0, 0x11(%rbx)
je 0x223379
leaq 0x30(%rbx), %rax
movb $0x1, 0x3c(%rbx)
movq 0x78(%r12), %rcx
movq %rcx, 0x40(%rbx)
movq %rax, 0x78(%r12)
cmpl $0x1, %ebp
movb $0x1, %al
sbbb $-0x1, %al
movb %al, 0x10(%rbx)
cmpb 0x24(%r12), %al
jbe 0x22338f
movb %al, 0x24(%r12)
testl %ebp, %ebp
je 0x2233fd
xorl %eax, %eax
cmpl $0x2, %ebp
setge %al
movq 0x18(%r12), %rdx
movq %rbx, 0x80(%r12)
movl 0x28(%r12), %ecx
andl $-0x41, %ecx
shll $0x6, %eax
orl %ecx, %eax
movw %ax, 0x28(%r12)
movl 0x40(%r12), %r13d
movq %rdx, (%rsp)
movq 0x50(%rdx), %rdi
addq $0x1c, %rdi
callq 0x22fc59
cmpl %eax, %r13d
je 0x2233fd
movq (%rsp), %rax
movq 0x70(%rax), %rdi
callq 0x2236f2
testl %eax, %eax
jne 0x222e8d
movq (%rsp), %rax
movq 0x50(%rax), %rax
movl 0x40(%r12), %ecx
bswapl %ecx
movl %ecx, 0x1c(%rax)
testq %r15, %r15
je 0x223417
movq 0x18(%r12), %rax
movq 0x50(%rax), %rdi
addq $0x28, %rdi
callq 0x22fc59
movl %eax, (%r15)
testl %ebp, %ebp
je 0x22343a
movq (%rbx), %rax
movl 0x2b8(%rax), %esi
movq %r14, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x235eeb
xorl %r13d, %r13d
movl %r13d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq