// File: /workspace/github_c_makefile_star2_O2/ukontainer[P]sqlite-bench/sqlite3.c
// Function: sqlite3BtreeBeginTrans
SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){
  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( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );

  if( (p->db->flags & SQLITE_ResetDatabase) 
   && sqlite3PagerIsreadonly(pBt->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, 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);
        }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 ){
      unlockBtreeIfUnused(pBt);
    }
  }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
          btreeInvokeBusyHandler(pBt) );
  sqlite3PagerResetLockTimeout(pBt->pPager);

  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(pBt->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 0x8(%rdi), %r14
callq 0x4083c8
movb 0x10(%rbx), %al
cmpb $0x2, %al
je 0x4089c2
cmpb $0x1, %al
sete %al
testl %ebp, %ebp
sete %cl
testb %al, %cl
jne 0x4089c2
movq (%rbx), %rax
testb $0x2, 0x33(%rax)
je 0x40842f
movq (%r14), %rax
cmpb $0x0, 0x12(%rax)
jne 0x40842f
andb $-0x2, 0x26(%r14)
movzwl 0x26(%r14), %eax
testl %ebp, %ebp
je 0x408448
movl %eax, %ecx
andl $0x1, %ecx
je 0x408448
pushq $0x8
popq %r12
jmp 0x4089f8
testl %ebp, %ebp
je 0x408476
testb %al, %al
js 0x40847a
cmpb $0x2, 0x24(%r14)
je 0x40847a
cmpl $0x2, %ebp
jl 0x40848e
leaq 0x70(%r14), %rax
movq (%rax), %rax
testq %rax, %rax
je 0x40848e
movq (%rax), %rcx
addq $0x10, %rax
cmpq %rbx, %rcx
je 0x408460
jmp 0x40847e
testb %al, %al
jns 0x40848e
movq 0x78(%r14), %rcx
movl $0x106, %r12d           # imm = 0x106
cmpq $0x0, (%rcx)
jne 0x4089f8
pushq $0x1
popq %rdx
movq %rbx, %rdi
movl %edx, %esi
callq 0x41befa
testl %eax, %eax
jne 0x4089f0
movzwl 0x26(%r14), %eax
movl %eax, %ecx
andl $-0x11, %ecx
movw %cx, 0x26(%r14)
cmpl $0x0, 0x3c(%r14)
jne 0x4084c1
orl $0x10, %eax
movw %ax, 0x26(%r14)
leaq 0x30(%r14), %rax
movq %rax, 0x28(%rsp)
xorl %eax, %eax
cmpq $0x0, 0x18(%r14)
je 0x408524
testl %ebp, %ebp
je 0x408776
testl %eax, %eax
jne 0x408776
testb $0x1, 0x26(%r14)
pushq $0x8
popq %r12
jne 0x408781
movq (%r14), %r13
movl 0x2c(%r13), %r12d
testl %r12d, %r12d
je 0x408552
cmpl $0x205, %r12d           # imm = 0x205
jne 0x408781
xorl %r12d, %r12d
cmpb $0x0, 0x24(%r14)
setne %r12b
shll $0x9, %r12d
orl $0x5, %r12d
jmp 0x408781
movq (%r14), %rdi
callq 0x41c03b
testl %eax, %eax
jne 0x40854a
movq %r14, %rdi
pushq $0x1
popq %rsi
leaq 0x30(%rsp), %rdx
xorl %ecx, %ecx
callq 0x41c4d6
testl %eax, %eax
je 0x4085ef
movl %eax, %r12d
jmp 0x408781
movq (%rbx), %rax
cmpb $0x2, 0x60(%rax)
sete 0x19(%r13)
cmpb $0x1, 0x14(%r13)
jne 0x40876e
movq 0x120(%r13), %rdi
testq %rdi, %rdi
je 0x40871c
cmpb $0x0, 0x8(%r13)
je 0x40858a
cmpb $0x0, 0x3f(%rdi)
je 0x4087b8
cmpb $0x0, 0x42(%rdi)
pushq $0x8
popq %r12
jne 0x408781
xorl %esi, %esi
pushq $0x1
popq %rdx
movq %rdi, 0x8(%rsp)
callq 0x417742
testl %eax, %eax
jne 0x4087c7
movq 0x8(%rsp), %r12
movb $0x1, 0x40(%r12)
leaq 0x48(%r12), %rdi
movq 0x30(%r12), %rax
movq (%rax), %rsi
pushq $0x30
popq %rdx
callq 0x401028
testl %eax, %eax
je 0x408754
movq %r12, %rdi
xorl %esi, %esi
pushq $0x1
popq %rdx
callq 0x4177cb
movb $0x0, 0x40(%r12)
jmp 0x40850b
movq 0x30(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x50(%rax), %r12
leaq 0x1c(%r12), %rdi
callq 0x415e33
movq (%r14), %rdi
movl 0x1c(%rdi), %edx
testl %eax, %eax
je 0x408620
movl %eax, %r13d
movl 0x18(%r12), %eax
cmpl 0x5c(%r12), %eax
je 0x408623
movl %edx, %r13d
movq 0x8(%r14), %rax
testb $0x2, 0x33(%rax)
movl $0x0, %eax
cmovnel %eax, %r13d
testl %r13d, %r13d
jle 0x4086ab
movdqu (%r12), %xmm0
pcmpeqb 0x135d79(%rip), %xmm0   # 0x53e3c0
pmovmskb %xmm0, %eax
movq %r12, %rcx
pushq $0x1a
popq %r12
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x408819
cmpb $0x3, 0x12(%rcx)
jb 0x408668
orb $0x1, 0x26(%r14)
cmpb $0x2, 0x13(%rcx)
ja 0x408819
movl %edx, 0x24(%rsp)
movq %rcx, 0x18(%rsp)
jne 0x4087f5
testb $0x20, 0x26(%r14)
jne 0x4087f5
andl $0x0, 0x14(%rsp)
leaq 0x14(%rsp), %rsi
callq 0x41c517
testl %eax, %eax
je 0x4087ea
movl %eax, %r12d
jmp 0x408819
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 0x8(%rsp), %rax
movq %rax, 0x18(%r14)
movl %r13d, 0x3c(%r14)
movl $0x0, %eax
testl %r12d, %r12d
jne 0x408781
jmp 0x4084cc
cmpl $0x2, %ebp
setl 0x8(%rsp)
movq %r13, %rdi
pushq $0x2
popq %rsi
callq 0x41891c
movl %eax, %r12d
testl %eax, %eax
setne %al
orb 0x8(%rsp), %al
jne 0x40874b
movq %r13, %rdi
pushq $0x4
popq %rsi
callq 0x4188e9
movl %eax, %r12d
testl %r12d, %r12d
jne 0x4084fe
movb $0x2, 0x14(%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 0x41bf48
movl %eax, %r12d
testl %eax, %eax
je 0x408934
movq %r14, %rdi
callq 0x41c009
cmpb $0x5, %r12b
jne 0x4089f8
cmpb $0x0, 0x24(%r14)
jne 0x4089f8
movq %r14, %rdi
callq 0x415dda
movl %eax, %ecx
movl %r12d, %eax
testl %ecx, %ecx
jne 0x4084cc
jmp 0x4089f8
movq %r13, %rdi
pushq $0x4
popq %rsi
callq 0x41891c
testl %eax, %eax
je 0x4087cf
movl %eax, %r12d
jmp 0x4084fe
movq 0x120(%r13), %rdi
pushq $0x1
popq %rsi
callq 0x41a981
movq 0x120(%r13), %rdi
jmp 0x40858a
cmpl $0x0, 0x14(%rsp)
je 0x408922
movq 0x18(%rsp), %rax
leaq 0x15(%rax), %rdi
leaq 0x13623d(%rip), %rsi    # 0x53ea42
pushq $0x3
popq %rdx
callq 0x401028
pushq $0x1a
popq %r12
testl %eax, %eax
movl 0x24(%rsp), %esi
je 0x40882d
movq 0x8(%rsp), %rdi
callq 0x41c57b
andq $0x0, 0x18(%r14)
jmp 0x408781
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 0x408819
movl %ecx, %edx
orl %eax, %edx
addl %ecx, %eax
decl %eax
andl %edx, %eax
jne 0x408819
movq 0x18(%rsp), %rax
movzbl 0x14(%rax), %r12d
movl %edx, %ecx
subl %r12d, %ecx
movq 0x28(%rsp), %rax
cmpl (%rax), %edx
jne 0x408893
movq 0x8(%r14), %rax
testb $0x1, 0x30(%rax)
jne 0x4088d0
cmpl %esi, %r13d
jle 0x4088d0
movl $0x100ea, %edi          # imm = 0x100EA
callq 0x418527
jmp 0x4086a3
movl %ecx, 0x10(%rsp)
movq 0x8(%rsp), %rdi
movl %edx, %r13d
callq 0x41c57b
movl 0x10(%rsp), %eax
movl %eax, 0x34(%r14)
movl %r13d, 0x30(%r14)
movq %r14, %rdi
callq 0x41c592
movq (%r14), %rdi
movq 0x28(%rsp), %rsi
movl %r12d, %edx
callq 0x415e38
movl %eax, %r12d
jmp 0x40870d
pushq $0x1a
popq %r12
movl %ecx, %eax
cmpl $0x1e0, %ecx            # imm = 0x1E0
movq 0x18(%rsp), %rcx
jb 0x408819
movl %edx, 0x30(%r14)
movl %eax, 0x10(%rsp)
movl %eax, 0x34(%r14)
movq %rcx, %r12
leaq 0x34(%rcx), %rdi
callq 0x415e33
testl %eax, %eax
setne 0x21(%r14)
addq $0x40, %r12
movq %r12, %rdi
callq 0x415e33
movl 0x10(%rsp), %edi
testl %eax, %eax
setne 0x22(%r14)
jmp 0x4086af
movq 0x8(%rsp), %rdi
callq 0x41c57b
xorl %r12d, %r12d
jmp 0x40870d
cmpb $0x0, 0x10(%rbx)
jne 0x408958
incl 0x38(%r14)
cmpb $0x0, 0x11(%rbx)
je 0x408958
leaq 0x30(%rbx), %rax
movb $0x1, 0x3c(%rbx)
movq 0x70(%r14), %rcx
movq %rcx, 0x40(%rbx)
movq %rax, 0x70(%r14)
cmpl $0x1, %ebp
movb $0x1, %al
sbbb $-0x1, %al
movb %al, 0x10(%rbx)
cmpb 0x24(%r14), %al
jbe 0x40896c
movb %al, 0x24(%r14)
testl %ebp, %ebp
je 0x4089c2
xorl %eax, %eax
cmpl $0x2, %ebp
setge %al
movq 0x18(%r14), %r13
movq %rbx, 0x78(%r14)
movzwl 0x26(%r14), %ecx
andl $-0x41, %ecx
shll $0x6, %eax
orl %ecx, %eax
movw %ax, 0x26(%r14)
movl 0x3c(%r14), %r12d
movq 0x50(%r13), %rdi
addq $0x1c, %rdi
callq 0x415e33
cmpl %eax, %r12d
je 0x4089c2
movq 0x70(%r13), %rdi
callq 0x408cc0
testl %eax, %eax
jne 0x4089f0
movq 0x50(%r13), %rax
movl 0x3c(%r14), %ecx
bswapl %ecx
movl %ecx, 0x1c(%rax)
testq %r15, %r15
je 0x4089db
movq 0x18(%r14), %rax
movq 0x50(%rax), %rdi
addq $0x28, %rdi
callq 0x415e33
movl %eax, (%r15)
testl %ebp, %ebp
je 0x4089f5
movq (%r14), %rdi
movq (%rbx), %rax
movl 0x284(%rax), %esi
callq 0x41c026
movl %eax, %r12d
jmp 0x4089f8
xorl %r12d, %r12d
movq %rbx, %rdi
callq 0x4092a1
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq