// File: /workspace/github_c_makefile_star2_O3/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
movl %esi, %ebp
movq %rdi, %r15
movq (%rdi), %rdi
movq 0x8(%r15), %r12
movq (%r12), %r14
movq %rdi, 0x8(%r12)
movb 0x10(%r15), %al
cmpb $0x2, %al
je 0x277167
cmpb $0x1, %al
sete %al
testl %ebp, %ebp
sete %cl
testb %al, %cl
jne 0x277167
movq %rdx, 0x20(%rsp)
testb $0x2, 0x33(%rdi)
je 0x276a72
cmpb $0x0, 0x12(%r14)
jne 0x276a72
andb $-0x2, 0x28(%r12)
movzwl 0x28(%r12), %eax
testl %ebp, %ebp
je 0x276a8d
movl %eax, %ecx
movl $0x8, %r13d
andl $0x1, %ecx
jne 0x2771b2
testl %ebp, %ebp
je 0x276aba
testb %al, %al
js 0x276abe
cmpb $0x2, 0x24(%r12)
je 0x276abe
cmpl $0x2, %ebp
jl 0x276ade
movq 0x78(%r12), %rax
testq %rax, %rax
je 0x276ade
movq (%rax), %rcx
cmpq %r15, %rcx
jne 0x276ac6
movq 0x10(%rax), %rax
jmp 0x276aa7
testb %al, %al
jns 0x276ade
movq 0x80(%r12), %rcx
movq (%rcx), %rsi
testq %rsi, %rsi
je 0x276ade
callq 0x28c88e
movl $0x106, %r13d           # imm = 0x106
jmp 0x2771b2
movq %r15, %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0x28c8fe
testl %eax, %eax
je 0x276afc
movl %eax, %r13d
jmp 0x2771b2
movzwl 0x28(%r12), %eax
movl %eax, %ecx
andl $-0x11, %ecx
movw %cx, 0x28(%r12)
cmpl $0x0, 0x40(%r12)
jne 0x276b1e
orl $0x10, %eax
movw %ax, 0x28(%r12)
leaq 0x34(%r12), %rax
movq %rax, 0x28(%rsp)
xorl %r13d, %r13d
movq %r14, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movl %ebp, 0x4(%rsp)
cmpq $0x0, 0x18(%r12)
je 0x276bb1
testl %ebp, %ebp
je 0x276f88
testl %r13d, %r13d
jne 0x276f88
movl $0x8, %r13d
testb $0x1, 0x28(%r12)
jne 0x276fb8
movl 0x2c(%r14), %eax
testl %eax, %eax
je 0x276e4a
movl %eax, %r13d
cmpl $0x205, %eax            # imm = 0x205
jne 0x276fb8
movb 0x24(%r12), %al
xorl %r13d, %r13d
testb %al, %al
setne %r13b
shll $0x9, %r13d
orl $0x5, %r13d
jmp 0x276fbd
movq 0x70(%rsi), %rdi
callq 0x28cfb5
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %r15
jmp 0x276dc7
movq (%r12), %rdi
callq 0x28ca39
testl %eax, %eax
jne 0x276e42
movq %r12, %rdi
movl $0x1, %esi
leaq 0x30(%rsp), %rdx
xorl %ecx, %ecx
callq 0x28ced9
testl %eax, %eax
jne 0x276e42
movq 0x30(%rsp), %rsi
movq 0x50(%rsi), %r15
movl 0x1c(%r15), %ebp
bswapl %ebp
movq (%r12), %rdi
movl 0x1c(%rdi), %r8d
testl %ebp, %ebp
je 0x276c03
movl 0x18(%r15), %eax
cmpl 0x5c(%r15), %eax
je 0x276c06
movl %r8d, %ebp
movq 0x8(%r12), %rax
testb $0x2, 0x33(%rax)
movl $0x0, %eax
cmovnel %eax, %ebp
testl %ebp, %ebp
je 0x276d58
movdqu (%r15), %xmm0
pcmpeqb 0x15eaf4(%rip), %xmm0   # 0x3d5720
pmovmskb %xmm0, %eax
movl $0x1a, %r13d
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x276f98
cmpb $0x3, 0x12(%r15)
jb 0x276c4e
orb $0x1, 0x28(%r12)
cmpb $0x2, 0x13(%r15)
ja 0x276f98
jne 0x276c94
testb $0x20, 0x28(%r12)
jne 0x276c94
movl %r8d, %r14d
movq %rsi, %rbx
movl $0x0, 0x1c(%rsp)
leaq 0x1c(%rsp), %rsi
callq 0x28cf39
testl %eax, %eax
jne 0x276f92
cmpl $0x0, 0x1c(%rsp)
movq %rbx, %rsi
je 0x276b99
movl %r14d, %r8d
movzwl 0x15(%r15), %eax
xorl $0x2040, %eax           # imm = 0x2040
movzbl 0x17(%r15), %ecx
xorl $0x20, %ecx
orw %ax, %cx
jne 0x276f98
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 0x276f98
movl %ecx, %r14d
orl %eax, %r14d
addl %ecx, %eax
decl %eax
andl %r14d, %eax
jne 0x276f98
orb $0x2, 0x28(%r12)
movzbl 0x14(%r15), %eax
movl %r14d, %ebx
subl %eax, %ebx
cmpl 0x34(%r12), %r14d
jne 0x276dcd
movq 0x8(%r12), %rax
movl 0x30(%rax), %eax
movl $0x10000001, %ecx       # imm = 0x10000001
andl %ecx, %eax
cmpl $0x1, %eax
je 0x276d1f
cmpl %r8d, %ebp
ja 0x276edc
cmpl $0x1e0, %ebx            # imm = 0x1E0
jb 0x276f98
movl %r14d, 0x34(%r12)
movl %ebx, 0x38(%r12)
movl 0x34(%r15), %eax
bswapl %eax
testl %eax, %eax
setne 0x21(%r12)
movl 0x40(%r15), %eax
bswapl %eax
testl %eax, %eax
setne 0x22(%r12)
movq 0x8(%rsp), %r14
jmp 0x276d5d
movl 0x38(%r12), %ebx
movq 0x10(%rsp), %r15
movl %ebx, %eax
shll $0x6, %eax
addl $0xfffffd00, %eax       # imm = 0xFFFFFD00
movl $0x80808081, %edx       # imm = 0x80808081
imulq %rdx, %rax
shrq $0x27, %rax
addl $-0x17, %eax
movw %ax, 0x2a(%r12)
movl %ebx, %ecx
shll $0x5, %ecx
addl $0xfffffe80, %ecx       # imm = 0xFFFFFE80
imulq %rdx, %rcx
shrq $0x27, %rcx
addl $-0x17, %ecx
movw %cx, 0x2c(%r12)
addl $-0x23, %ebx
movw %bx, 0x2e(%r12)
movw %cx, 0x30(%r12)
cmpw $0x7f, %ax
jb 0x276db8
movl $0x7f, %eax
movb %al, 0x25(%r12)
movq %rsi, 0x18(%r12)
movl %ebp, 0x40(%r12)
movl 0x4(%rsp), %ebp
jmp 0x276e2e
movl %eax, %ebp
movq 0x70(%rsi), %rdi
callq 0x28cfb5
movl %ebx, 0x38(%r12)
movl %r14d, 0x34(%r12)
movq 0x88(%r12), %rdi
testq %rdi, %rdi
je 0x276e0c
addq $-0x4, %rdi
movq %rdi, 0x88(%r12)
callq 0x288d1d
movq $0x0, 0x88(%r12)
movq (%r12), %rdi
movq 0x28(%rsp), %rsi
movl %ebp, %edx
callq 0x285a0f
testl %eax, %eax
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %r15
movl 0x4(%rsp), %ebp
jne 0x276e42
cmpq $0x0, 0x18(%r12)
je 0x276bb1
xorl %r13d, %r13d
jmp 0x276b41
movl %eax, %r13d
jmp 0x276fb8
movq (%r15), %rax
cmpb $0x2, 0x60(%rax)
sete 0x19(%r14)
cmpb $0x1, 0x14(%r14)
jne 0x276f7d
movq 0x120(%r14), %rbx
testq %rbx, %rbx
je 0x276f10
cmpb $0x0, 0x8(%r14)
je 0x276e82
cmpb $0x0, 0x3f(%rbx)
je 0x277032
cmpb $0x0, 0x42(%rbx)
jne 0x276fb8
cmpb $0x0, 0x3f(%rbx)
je 0x27708f
movb $0x1, 0x40(%rbx)
leaq 0x48(%rbx), %rdi
movq 0x30(%rbx), %rax
movq (%rax), %rsi
movl $0x30, %edx
callq 0x17830
testl %eax, %eax
je 0x276f60
cmpb $0x0, 0x3f(%rbx)
jne 0x276ed3
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
xorl %esi, %esi
movl $0x1, %edx
movl $0x9, %ecx
callq *0x70(%rax)
movb $0x0, 0x40(%rbx)
jmp 0x276b7e
movl $0xb, %r13d
movl $0xb, %edi
movq %rsi, %rbx
leaq 0x15ecda(%rip), %rsi    # 0x3d5bcb
leaq 0x15ede4(%rip), %rdx    # 0x3d5cdc
movl $0x10855, %ecx          # imm = 0x10855
leaq 0x15eb2d(%rip), %r8     # 0x3d5a31
xorl %eax, %eax
callq 0x274d08
jmp 0x276f95
cmpl $0x2, %ebp
setl %bl
movq %r14, %rdi
movl $0x2, %esi
callq 0x288c38
testl %eax, %eax
setne %cl
orb %bl, %cl
jne 0x276f58
movq %r14, %rdi
movl $0x4, %esi
callq 0x288c38
cmpl $0x5, %eax
jne 0x276f58
movq 0xe8(%r14), %rdi
callq *0xe0(%r14)
movl $0x5, %r13d
testl %eax, %eax
jne 0x276f2c
jmp 0x276fb8
testl %eax, %eax
jne 0x276b70
movb $0x2, 0x14(%r14)
movl 0x1c(%r14), %eax
movl %eax, 0x28(%r14)
movl %eax, 0x24(%r14)
movl %eax, 0x20(%r14)
movq $0x0, 0x60(%r14)
movq %r12, %rdi
callq 0x28c978
movl %eax, %r13d
testl %r13d, %r13d
jne 0x276fb8
jmp 0x2770c1
movl %eax, %r13d
movq %rbx, %rsi
movq 0x70(%rsi), %rdi
callq 0x28cfb5
movq $0x0, 0x18(%r12)
movq 0x8(%rsp), %r14
movq 0x10(%rsp), %r15
movl 0x4(%rsp), %ebp
movb 0x24(%r12), %al
testb %al, %al
jne 0x276fdd
movq 0x18(%r12), %rax
testq %rax, %rax
je 0x276fdd
movq $0x0, 0x18(%r12)
movq 0x70(%rax), %rdi
callq 0x28cfb5
cmpb $0x5, %r13b
jne 0x2771b2
cmpb $0x0, 0x24(%r12)
jne 0x2771b2
movq 0x8(%r12), %rbx
movq 0x250(%rbx), %rax
testq %rax, %rax
je 0x2771b2
movl 0x260(%rbx), %esi
testl %esi, %esi
js 0x2771b2
movq 0x258(%rbx), %rdi
callq *%rax
testl %eax, %eax
je 0x2770b2
incl 0x260(%rbx)
jmp 0x276b39
movq %r14, %rdi
movl $0x4, %esi
callq 0x288c38
testl %eax, %eax
jne 0x276b70
movq 0x120(%r14), %r14
cmpb $0x0, 0x3f(%r14)
movq %r14, %rbx
jne 0x277080
movswl 0x3c(%r14), %esi
addl $0x3, %esi
movq 0x8(%r14), %rdi
movq (%rdi), %rax
movl $0x1, %edx
movl $0x5, %ecx
callq *0x70(%rax)
movq 0x8(%rsp), %rax
movq 0x120(%rax), %rbx
movb $0x1, 0x3f(%r14)
movq 0x8(%rsp), %r14
jmp 0x276e82
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
xorl %esi, %esi
movl $0x1, %edx
movl $0xa, %ecx
callq *0x70(%rax)
testl %eax, %eax
jne 0x276b70
jmp 0x276e96
movl $0xffffffff, 0x260(%rbx) # imm = 0xFFFFFFFF
jmp 0x2771b2
cmpb $0x0, 0x10(%r15)
jne 0x2770eb
incl 0x3c(%r12)
cmpb $0x0, 0x11(%r15)
je 0x2770eb
leaq 0x30(%r15), %rax
movb $0x1, 0x3c(%r15)
movq 0x78(%r12), %rcx
movq %rcx, 0x40(%r15)
movq %rax, 0x78(%r12)
cmpl $0x1, %ebp
movb $0x1, %al
sbbb $-0x1, %al
movb %al, 0x10(%r15)
cmpb 0x24(%r12), %al
movq 0x20(%rsp), %rdx
jbe 0x277107
movb %al, 0x24(%r12)
testl %ebp, %ebp
je 0x277167
xorl %eax, %eax
cmpl $0x2, %ebp
setge %al
movq 0x18(%r12), %rbx
movq %r15, 0x80(%r12)
movl 0x28(%r12), %ecx
andl $-0x41, %ecx
shll $0x6, %eax
orl %ecx, %eax
movw %ax, 0x28(%r12)
movq 0x50(%rbx), %rax
movl 0x1c(%rax), %eax
bswapl %eax
cmpl %eax, 0x40(%r12)
je 0x277167
movq 0x70(%rbx), %rdi
callq 0x2774a1
testl %eax, %eax
jne 0x276af4
movq 0x50(%rbx), %rax
movl 0x40(%r12), %ecx
bswapl %ecx
movl %ecx, 0x1c(%rax)
movq 0x20(%rsp), %rdx
testq %rdx, %rdx
je 0x27717c
movq 0x18(%r12), %rax
movq 0x50(%rax), %rax
movl 0x28(%rax), %eax
bswapl %eax
movl %eax, (%rdx)
xorl %r13d, %r13d
testl %ebp, %ebp
je 0x2771b2
movq (%r15), %rax
movl 0x2b8(%rax), %esi
cmpl %esi, 0x80(%r14)
jge 0x2771b2
cmpb $0x0, 0xa(%r14)
je 0x2771b2
movq %r14, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x28d37f
movl %r13d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq