// File: /workspace/github_c_makefile_star2_O1/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
movl %esi, %ebp
movq %rdi, %rbx
movq 0x8(%rdi), %r14
cmpb $0x0, 0x11(%rdi)
je 0x4093d6
incl 0x14(%rbx)
cmpb $0x0, 0x12(%rbx)
jne 0x4093d6
movq %rbx, %rdi
movq %rdx, %r15
callq 0x42017c
movq %r15, %rdx
movb 0x10(%rbx), %al
xorl %r12d, %r12d
cmpb $0x2, %al
je 0x409a20
cmpb $0x1, %al
sete %al
testl %ebp, %ebp
sete %cl
testb %al, %cl
jne 0x409a20
movq (%rbx), %rax
testb $0x2, 0x33(%rax)
je 0x40940d
movq (%r14), %rax
cmpb $0x0, 0x12(%rax)
jne 0x40940d
andb $-0x2, 0x26(%r14)
movzwl 0x26(%r14), %eax
testl %ebp, %ebp
je 0x409427
movl %eax, %ecx
movl $0x8, %r12d
andl $0x1, %ecx
jne 0x409a20
testl %ebp, %ebp
je 0x409454
testb %al, %al
js 0x409458
cmpb $0x2, 0x24(%r14)
je 0x409458
movb $0x1, %al
cmpl $0x2, %ebp
jl 0x409463
movq 0x70(%r14), %rcx
testq %rcx, %rcx
je 0x409463
movq (%rcx), %rsi
cmpq %rbx, %rsi
jne 0x40945c
movq 0x10(%rcx), %rcx
jmp 0x409441
testb %al, %al
jns 0x409436
movq 0x78(%r14), %rsi
cmpq $0x0, (%rsi)
sete %al
xorl %ecx, %ecx
movl $0x106, %r12d           # imm = 0x106
testb %al, %al
cmovnel %ecx, %r12d
je 0x409a20
movq %rdx, 0x20(%rsp)
movq %rbx, %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0x42021f
testl %eax, %eax
je 0x40949f
movl %eax, %r12d
movq 0x20(%rsp), %rdx
jmp 0x409a20
movzwl 0x26(%r14), %eax
movl %eax, %ecx
andl $-0x11, %ecx
movw %cx, 0x26(%r14)
cmpl $0x0, 0x3c(%r14)
jne 0x4094bd
orl $0x10, %eax
movw %ax, 0x26(%r14)
leaq 0x30(%r14), %rax
movq %rax, 0x28(%rsp)
xorl %r12d, %r12d
cmpq $0x0, 0x18(%r14)
jne 0x4097ee
jmp 0x4097b4
movq 0x30(%rsp), %r8
movq 0x50(%r8), %r13
movl 0x1c(%r13), %r15d
bswapl %r15d
movq (%r14), %rdi
movl 0x1c(%rdi), %r9d
testl %r15d, %r15d
je 0x4094ff
movl 0x18(%r13), %eax
cmpl 0x5c(%r13), %eax
je 0x409502
movl %r9d, %r15d
movq 0x8(%r14), %rax
testb $0x2, 0x33(%rax)
movl $0x0, %eax
cmovnel %eax, %r15d
testl %r15d, %r15d
jle 0x40955f
movdqu (%r13), %xmm0
pcmpeqb 0x146faa(%rip), %xmm0   # 0x5504d0
pmovmskb %xmm0, %eax
movl $0x1a, %esi
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x40954d
cmpb $0x3, 0x12(%r13)
jb 0x409542
orb $0x1, 0x26(%r14)
cmpb $0x2, 0x13(%r13)
jbe 0x4095e8
movl $0x2, %eax
cmpl $0x2, %eax
je 0x4095d0
testl %eax, %eax
jne 0x4097df
movl 0x34(%r14), %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, 0x28(%r14)
movl %ecx, %edx
shll $0x5, %edx
addl $0xfffffe80, %edx       # imm = 0xFFFFFE80
imulq %rsi, %rdx
shrq $0x27, %rdx
addl $-0x17, %edx
movw %dx, 0x2a(%r14)
addl $-0x23, %ecx
movw %cx, 0x2c(%r14)
movw %dx, 0x2e(%r14)
movzwl %ax, %ecx
cmpl $0x80, %ecx
jb 0x4095bc
movb $0x7f, 0x25(%r14)
jmp 0x4095c0
movb %al, 0x25(%r14)
movq %r8, 0x18(%r14)
movl %r15d, 0x3c(%r14)
xorl %r12d, %r12d
jmp 0x4097df
movq %r8, %rdi
movl %esi, %r12d
callq 0x420955
movq $0x0, 0x18(%r14)
jmp 0x4097df
jne 0x40965e
testb $0x20, 0x26(%r14)
jne 0x40965e
movl %r9d, 0xc(%rsp)
movq %r8, 0x10(%rsp)
movl $0x0, 0x1c(%rsp)
leaq 0x1c(%rsp), %rsi
callq 0x4208d9
xorl %r12d, %r12d
testl %eax, %eax
je 0x409624
movl %eax, %esi
movl $0x2, %eax
xorl %ecx, %ecx
movq 0x10(%rsp), %r8
jmp 0x409651
cmpl $0x0, 0x1c(%rsp)
movq 0x10(%rsp), %r8
je 0x40963b
movl $0x1a, %esi
xorl %eax, %eax
movb $0x1, %cl
jmp 0x409651
movq %r8, %rdi
callq 0x420955
movq 0x10(%rsp), %r8
xorl %ecx, %ecx
movl $0x1, %eax
xorl %esi, %esi
testb %cl, %cl
movl 0xc(%rsp), %r9d
je 0x409552
movzwl 0x15(%r13), %eax
xorl $0x2040, %eax           # imm = 0x2040
movzbl 0x17(%r13), %ecx
xorl $0x20, %ecx
xorl %r12d, %r12d
orw %ax, %cx
je 0x409682
movl $0x2, %eax
jmp 0x409552
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 0x409678
movl %ecx, %edx
orl %eax, %edx
addl %ecx, %eax
decl %eax
andl %edx, %eax
jne 0x409678
movzbl 0x14(%r13), %ecx
movl %edx, %edi
subl %ecx, %edi
movq 0x28(%rsp), %rax
cmpl (%rax), %edx
jne 0x409711
movq 0x8(%r14), %rax
testb $0x1, 0x30(%rax)
jne 0x40977c
cmpl %r9d, %r15d
jle 0x40977c
xorl %r12d, %r12d
movl $0xb, %edi
leaq 0x1471c0(%rip), %rsi    # 0x5508a7
leaq 0x1472dc(%rip), %rdx    # 0x5509ca
movl $0x100ea, %ecx          # imm = 0x100EA
movq %r8, %r13
leaq 0x146fe1(%rip), %r8     # 0x5506de
xorl %eax, %eax
callq 0x40743a
movl $0xb, %esi
movq %r13, %r8
jmp 0x409678
movl %ecx, 0xc(%rsp)
movq %r8, 0x10(%rsp)
movl %edi, %r13d
movq %r8, %rdi
movl %edx, %r12d
callq 0x420955
movl %r13d, 0x34(%r14)
movl %r12d, 0x30(%r14)
movq 0x80(%r14), %rdi
testq %rdi, %rdi
je 0x409757
addq $-0x4, %rdi
movq %rdi, 0x80(%r14)
callq 0x41c816
movq $0x0, 0x80(%r14)
movq (%r14), %rdi
movq 0x28(%rsp), %rsi
movl 0xc(%rsp), %edx
callq 0x4193da
movl %eax, %esi
movl $0x1, %eax
movl %esi, %r12d
movq 0x10(%rsp), %r8
jmp 0x409552
cmpl $0x1e0, %edi            # imm = 0x1E0
jb 0x409678
movl %edx, 0x30(%r14)
movl %edi, 0x34(%r14)
movl 0x34(%r13), %eax
bswapl %eax
testl %eax, %eax
setne 0x21(%r14)
movl 0x40(%r13), %eax
bswapl %eax
testl %eax, %eax
setne 0x22(%r14)
xorl %eax, %eax
xorl %r12d, %r12d
jmp 0x409552
movq (%r14), %rdi
callq 0x42032b
testl %eax, %eax
jne 0x4097dc
movq %r14, %rdi
movl $0x1, %esi
leaq 0x30(%rsp), %rdx
xorl %ecx, %ecx
callq 0x420878
testl %eax, %eax
je 0x4094d9
movl %eax, %r12d
testl %r12d, %r12d
jne 0x4097ee
cmpq $0x0, 0x18(%r14)
je 0x4097b4
xorl %r12d, %r12d
testl %ebp, %ebp
je 0x4099d7
testl %r12d, %r12d
jne 0x4099d7
movl $0x8, %r12d
testb $0x1, 0x26(%r14)
jne 0x4099d7
movq (%r14), %r13
movl 0x2c(%r13), %r12d
testl %r12d, %r12d
jne 0x4099a8
movq (%rbx), %rax
cmpb $0x2, 0x60(%rax)
sete 0x19(%r13)
xorl %r12d, %r12d
cmpb $0x1, 0x14(%r13)
jne 0x4099a8
movq 0x120(%r13), %rax
testq %rax, %rax
je 0x4098ee
cmpb $0x0, 0x8(%r13)
je 0x40985b
cmpb $0x0, 0x3f(%rax)
je 0x409938
movq 0x120(%r13), %r15
movl $0x8, %r12d
cmpb $0x0, 0x42(%r15)
jne 0x409983
xorl %r12d, %r12d
cmpb $0x0, 0x3f(%r15)
jne 0x409896
movq 0x8(%r15), %rdi
movq (%rdi), %rax
xorl %esi, %esi
movl $0x1, %edx
movl $0xa, %ecx
callq *0x70(%rax)
movl %eax, %r12d
testl %r12d, %r12d
jne 0x409983
movb $0x1, 0x40(%r15)
leaq 0x48(%r15), %rdi
movq 0x30(%r15), %rax
movq (%rax), %rsi
movl $0x30, %edx
callq 0x401028
testl %eax, %eax
je 0x409980
cmpb $0x0, 0x3f(%r15)
jne 0x4098de
movq 0x8(%r15), %rdi
movq (%rdi), %rax
xorl %esi, %esi
movl $0x1, %edx
movl $0x9, %ecx
callq *0x70(%rax)
movb $0x0, 0x40(%r15)
movl $0x205, %r12d           # imm = 0x205
jmp 0x409983
cmpl $0x2, %ebp
setl %r15b
movq %r13, %rdi
movl $0x2, %esi
callq 0x41c735
movl %eax, %r12d
testl %eax, %eax
setne %al
orb %r15b, %al
jne 0x409983
movq %r13, %rdi
movl $0x4, %esi
callq 0x41c735
movl %eax, %r12d
cmpl $0x5, %eax
jne 0x409983
movq 0xe8(%r13), %rdi
callq *0xe0(%r13)
testl %eax, %eax
jne 0x40990f
jmp 0x409983
movq %r13, %rdi
movl $0x4, %esi
callq 0x41c735
movl %eax, %r12d
testl %eax, %eax
jne 0x4099a8
movq 0x120(%r13), %r15
cmpb $0x0, 0x3f(%r15)
jne 0x409976
movswl 0x3c(%r15), %esi
addl $0x3, %esi
movq 0x8(%r15), %rdi
movq (%rdi), %rax
movl $0x1, %edx
movl $0x5, %ecx
callq *0x70(%rax)
movb $0x1, 0x3f(%r15)
jmp 0x40985b
xorl %r12d, %r12d
testl %r12d, %r12d
jne 0x4099a8
movb $0x2, 0x14(%r13)
movl 0x1c(%r13), %eax
movl %eax, 0x28(%r13)
movl %eax, 0x24(%r13)
movl %eax, 0x20(%r13)
movq $0x0, 0x60(%r13)
xorl %r12d, %r12d
cmpl $0x205, %r12d           # imm = 0x205
je 0x4099c3
testl %r12d, %r12d
jne 0x4099d7
movq %r14, %rdi
callq 0x42026a
movl %eax, %r12d
jmp 0x4099d7
xorl %r12d, %r12d
cmpb $0x0, 0x24(%r14)
setne %r12b
shll $0x9, %r12d
orl $0x5, %r12d
testl %r12d, %r12d
je 0x4099f9
cmpb $0x0, 0x24(%r14)
jne 0x4099f9
movq 0x18(%r14), %rdi
testq %rdi, %rdi
je 0x4099f9
movq $0x0, 0x18(%r14)
callq 0x420955
cmpb $0x5, %r12b
jne 0x409a16
cmpb $0x0, 0x24(%r14)
jne 0x409a16
movq %r14, %rdi
callq 0x41937f
testl %eax, %eax
jne 0x4094c9
testl %r12d, %r12d
movq 0x20(%rsp), %rdx
je 0x409a89
testl %r12d, %r12d
je 0x409a4a
cmpb $0x0, 0x11(%rbx)
je 0x409a38
decl 0x14(%rbx)
jne 0x409a38
movq %rbx, %rdi
callq 0x420202
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %rdx, %rdx
je 0x409a5e
movq 0x18(%r14), %rax
movq 0x50(%rax), %rax
movl 0x28(%rax), %eax
bswapl %eax
movl %eax, (%rdx)
xorl %r12d, %r12d
testl %ebp, %ebp
je 0x409a25
movq (%r14), %rdi
movq (%rbx), %rax
movl 0x284(%rax), %esi
cmpl %esi, 0x80(%rdi)
jge 0x409a25
cmpb $0x0, 0xa(%rdi)
je 0x409a25
callq 0x420d09
movl %eax, %r12d
jmp 0x409a25
cmpb $0x0, 0x10(%rbx)
jne 0x409aad
incl 0x38(%r14)
cmpb $0x0, 0x11(%rbx)
je 0x409aad
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 0x409ac1
movb %al, 0x24(%r14)
xorl %r12d, %r12d
testl %ebp, %ebp
je 0x409a20
movq 0x18(%r14), %r15
movq %rbx, 0x78(%r14)
movzwl 0x26(%r14), %eax
andl $-0x41, %eax
xorl %ecx, %ecx
cmpl $0x2, %ebp
setge %cl
shll $0x6, %ecx
orl %eax, %ecx
movw %cx, 0x26(%r14)
movq 0x50(%r15), %rax
movl 0x1c(%rax), %eax
bswapl %eax
cmpl %eax, 0x3c(%r14)
je 0x409a20
movq 0x70(%r15), %rdi
callq 0x409e0b
testl %eax, %eax
jne 0x409492
movq 0x50(%r15), %rax
movl 0x3c(%r14), %ecx
bswapl %ecx
movl %ecx, 0x1c(%rax)
jmp 0x409495