// File: /workspace/github_c_makefile_star2_O3/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, %r12d
movq %rdi, %rbp
movq 0x8(%rdi), %r14
cmpb $0x0, 0x11(%rdi)
je 0x4095e4
incl 0x14(%rbp)
cmpb $0x0, 0x12(%rbp)
jne 0x4095e4
movq %rbp, %rdi
callq 0x420275
movb 0x10(%rbp), %al
cmpb $0x2, %al
je 0x409c8b
cmpb $0x1, %al
sete %al
testl %r12d, %r12d
sete %cl
testb %al, %cl
jne 0x409c8b
movq %r15, 0x20(%rsp)
movq (%rbp), %rax
testb $0x2, 0x33(%rax)
je 0x40961f
movq (%r14), %rax
cmpb $0x0, 0x12(%rax)
jne 0x40961f
andb $-0x2, 0x26(%r14)
movzwl 0x26(%r14), %eax
testl %r12d, %r12d
je 0x40963a
movl %eax, %ecx
movl $0x8, %r13d
andl $0x1, %ecx
jne 0x409ccb
testl %r12d, %r12d
je 0x409667
testb %al, %al
js 0x40966b
cmpb $0x2, 0x24(%r14)
je 0x40966b
cmpl $0x2, %r12d
jl 0x40967f
movq 0x70(%r14), %rax
testq %rax, %rax
je 0x40967f
movq (%rax), %rcx
cmpq %rbp, %rcx
jne 0x40966f
movq 0x10(%rax), %rax
jmp 0x409654
testb %al, %al
jns 0x40967f
movq 0x78(%r14), %rcx
movl $0x106, %r13d           # imm = 0x106
cmpq $0x0, (%rcx)
jne 0x409ccb
movq %rbp, %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0x420318
testl %eax, %eax
jne 0x409cc8
movzwl 0x26(%r14), %eax
movl %eax, %ecx
andl $-0x11, %ecx
movw %cx, 0x26(%r14)
cmpl $0x0, 0x3c(%r14)
jne 0x4096b7
orl $0x10, %eax
movw %ax, 0x26(%r14)
leaq 0x30(%r14), %rax
movq %rax, 0x28(%rsp)
xorl %r13d, %r13d
movl %r12d, 0x8(%rsp)
movq %rbp, 0x10(%rsp)
cmpq $0x0, 0x18(%r14)
je 0x409742
testl %r12d, %r12d
je 0x409b06
testl %r13d, %r13d
jne 0x409b06
movl $0x8, %r13d
testb $0x1, 0x26(%r14)
jne 0x409b2c
movq (%r14), %r12
movl 0x2c(%r12), %eax
testl %eax, %eax
je 0x4099b3
movl %eax, %r13d
cmpl $0x205, %eax            # imm = 0x205
jne 0x409b2c
movb 0x24(%r14), %al
xorl %r13d, %r13d
testb %al, %al
setne %r13b
shll $0x9, %r13d
orl $0x5, %r13d
jmp 0x409b30
movq 0x70(%rsi), %rdi
callq 0x4209f0
movq 0x10(%rsp), %rbp
jmp 0x409998
movq (%r14), %rdi
callq 0x420424
testl %eax, %eax
jne 0x4099ab
movq %r14, %rdi
movl $0x1, %esi
leaq 0x30(%rsp), %rdx
xorl %ecx, %ecx
callq 0x420914
testl %eax, %eax
jne 0x4099ab
movq 0x30(%rsp), %rsi
movq 0x50(%rsi), %r15
movl 0x1c(%r15), %ebp
bswapl %ebp
movq (%r14), %rdi
movl 0x1c(%rdi), %r8d
testl %ebp, %ebp
je 0x409792
movl 0x18(%r15), %eax
cmpl 0x5c(%r15), %eax
je 0x409795
movl %r8d, %ebp
movq 0x8(%r14), %rax
testb $0x2, 0x33(%rax)
movl $0x0, %eax
cmovnel %eax, %ebp
testl %ebp, %ebp
jle 0x4098d0
movdqu (%r15), %xmm0
pcmpeqb 0x146bd6(%rip), %xmm0   # 0x550390
pmovmskb %xmm0, %eax
movl $0x1a, %r13d
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x409b16
cmpb $0x3, 0x12(%r15)
jb 0x4097db
orb $0x1, 0x26(%r14)
cmpb $0x2, 0x13(%r15)
ja 0x409b16
jne 0x409824
testb $0x20, 0x26(%r14)
jne 0x409824
movl %r8d, 0x1c(%rsp)
movq %rsi, %rbx
movl $0x0, 0xc(%rsp)
leaq 0xc(%rsp), %rsi
callq 0x420974
testl %eax, %eax
jne 0x409b10
cmpl $0x0, 0xc(%rsp)
movq %rbx, %rsi
je 0x40972f
movl 0x1c(%rsp), %r8d
movzwl 0x15(%r15), %eax
xorl $0x2040, %eax           # imm = 0x2040
movzbl 0x17(%r15), %ecx
xorl $0x20, %ecx
orw %ax, %cx
jne 0x409b16
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 0x409b16
movl %ecx, %ebx
orl %eax, %ebx
addl %ecx, %eax
decl %eax
andl %ebx, %eax
jne 0x409b16
movzbl 0x14(%r15), %ecx
movl %ebx, %r12d
subl %ecx, %r12d
movq 0x28(%rsp), %rax
cmpl (%rax), %ebx
jne 0x409941
movq 0x8(%r14), %rax
testb $0x1, 0x30(%rax)
jne 0x40989f
cmpl %r8d, %ebp
jg 0x409a50
cmpl $0x1e0, %r12d           # imm = 0x1E0
jb 0x409b16
movl %ebx, 0x30(%r14)
movl %r12d, 0x34(%r14)
movl 0x34(%r15), %eax
bswapl %eax
testl %eax, %eax
setne 0x21(%r14)
movl 0x40(%r15), %eax
bswapl %eax
testl %eax, %eax
setne 0x22(%r14)
jmp 0x4098d4
movl 0x34(%r14), %r12d
movl %r12d, %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, 0x28(%r14)
movl %r12d, %ecx
shll $0x5, %ecx
addl $0xfffffe80, %ecx       # imm = 0xFFFFFE80
imulq %rdx, %rcx
shrq $0x27, %rcx
addl $-0x17, %ecx
movw %cx, 0x2a(%r14)
addl $-0x23, %r12d
movw %r12w, 0x2c(%r14)
movw %cx, 0x2e(%r14)
cmpw $0x7f, %ax
jb 0x409929
movl $0x7f, %eax
movb %al, 0x25(%r14)
movq %rsi, 0x18(%r14)
movl %ebp, 0x3c(%r14)
movq 0x10(%rsp), %rbp
movl 0x8(%rsp), %r12d
jmp 0x409998
movl %ecx, %ebp
movq 0x70(%rsi), %rdi
callq 0x4209f0
movl %r12d, 0x34(%r14)
movl %ebx, 0x30(%r14)
movq 0x80(%r14), %rdi
testq %rdi, %rdi
je 0x40997b
addq $-0x4, %rdi
movq %rdi, 0x80(%r14)
callq 0x41c637
movq $0x0, 0x80(%r14)
movq (%r14), %rdi
movq 0x28(%rsp), %rsi
movl %ebp, %edx
callq 0x41942b
testl %eax, %eax
movq 0x10(%rsp), %rbp
movl 0x8(%rsp), %r12d
jne 0x4099ab
cmpq $0x0, 0x18(%r14)
je 0x409742
xorl %r13d, %r13d
jmp 0x4096d4
movl %eax, %r13d
jmp 0x409b2c
movq (%rbp), %rax
cmpb $0x2, 0x60(%rax)
sete 0x19(%r12)
cmpb $0x1, 0x14(%r12)
jne 0x409afb
movq 0x120(%r12), %r15
testq %r15, %r15
je 0x409a84
cmpb $0x0, 0x8(%r12)
je 0x4099f1
cmpb $0x0, 0x3f(%r15)
je 0x409b7d
cmpb $0x0, 0x42(%r15)
jne 0x409b2c
cmpb $0x0, 0x3f(%r15)
je 0x409bcf
movb $0x1, 0x40(%r15)
leaq 0x48(%r15), %rdi
movq 0x30(%r15), %rax
movq (%rax), %rsi
movl $0x30, %edx
callq 0x401028
testl %eax, %eax
je 0x409ad8
cmpb $0x0, 0x3f(%r15)
jne 0x409a46
movq 0x8(%r15), %rdi
movq (%rdi), %rax
xorl %esi, %esi
movl $0x1, %edx
movl $0x9, %ecx
callq *0x70(%rax)
movb $0x0, 0x40(%r15)
jmp 0x409715
movl $0xb, %r13d
movl $0xb, %edi
movq %rsi, %rbx
leaq 0x146eca(%rip), %rsi    # 0x55092f
leaq 0x146fe6(%rip), %rdx    # 0x550a52
movl $0x100ea, %ecx          # imm = 0x100EA
leaq 0x146cee(%rip), %r8     # 0x550766
xorl %eax, %eax
callq 0x40765d
jmp 0x409b13
cmpl $0x2, 0x8(%rsp)
setl %bl
movq %r12, %rdi
movl $0x2, %esi
callq 0x41c552
testl %eax, %eax
setne %cl
orb %bl, %cl
jne 0x409ad0
movq %r12, %rdi
movl $0x4, %esi
callq 0x41c552
cmpl $0x5, %eax
jne 0x409ad0
movq 0xe8(%r12), %rdi
callq *0xe0(%r12)
movl $0x5, %r13d
testl %eax, %eax
jne 0x409aa2
jmp 0x409b2c
testl %eax, %eax
jne 0x409707
movb $0x2, 0x14(%r12)
movl 0x1c(%r12), %eax
movl %eax, 0x28(%r12)
movl %eax, 0x24(%r12)
movl %eax, 0x20(%r12)
movq $0x0, 0x60(%r12)
movq %r14, %rdi
callq 0x420363
movl %eax, %r13d
testl %r13d, %r13d
jne 0x409b2c
jmp 0x409bf2
movl %eax, %r13d
movq %rbx, %rsi
movq 0x70(%rsi), %rdi
callq 0x4209f0
movq $0x0, 0x18(%r14)
movq 0x10(%rsp), %rbp
movb 0x24(%r14), %al
testb %al, %al
jne 0x409b4e
movq 0x18(%r14), %rax
testq %rax, %rax
je 0x409b4e
movq $0x0, 0x18(%r14)
movq 0x70(%rax), %rdi
callq 0x4209f0
cmpb $0x5, %r13b
movl 0x8(%rsp), %r12d
jne 0x409ccb
cmpb $0x0, 0x24(%r14)
jne 0x409ccb
movq %r14, %rdi
callq 0x4193d0
testl %eax, %eax
jne 0x4096cd
jmp 0x409ccb
movq %r12, %rdi
movl $0x4, %esi
callq 0x41c552
testl %eax, %eax
jne 0x409707
movq 0x120(%r12), %rbx
cmpb $0x0, 0x3f(%rbx)
movq %rbx, %r15
jne 0x409bc6
movswl 0x3c(%rbx), %esi
addl $0x3, %esi
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x1, %edx
movl $0x5, %ecx
callq *0x70(%rax)
movq 0x120(%r12), %r15
movb $0x1, 0x3f(%rbx)
jmp 0x4099f1
movq 0x8(%r15), %rdi
movq (%rdi), %rax
xorl %esi, %esi
movl $0x1, %edx
movl $0xa, %ecx
callq *0x70(%rax)
testl %eax, %eax
jne 0x409707
jmp 0x409a07
cmpb $0x0, 0x10(%rbp)
jne 0x409c16
incl 0x38(%r14)
cmpb $0x0, 0x11(%rbp)
je 0x409c16
leaq 0x30(%rbp), %rax
movb $0x1, 0x3c(%rbp)
movq 0x70(%r14), %rcx
movq %rcx, 0x40(%rbp)
movq %rax, 0x70(%r14)
movl 0x8(%rsp), %r12d
cmpl $0x1, %r12d
movb $0x1, %al
sbbb $-0x1, %al
movb %al, 0x10(%rbp)
cmpb 0x24(%r14), %al
movq 0x20(%rsp), %r15
jbe 0x409c35
movb %al, 0x24(%r14)
testl %r12d, %r12d
je 0x409c8b
xorl %eax, %eax
cmpl $0x2, %r12d
setge %al
movq 0x18(%r14), %rbx
movq %rbp, 0x78(%r14)
movzwl 0x26(%r14), %ecx
andl $-0x41, %ecx
shll $0x6, %eax
orl %ecx, %eax
movw %ax, 0x26(%r14)
movq 0x50(%rbx), %rax
movl 0x1c(%rax), %eax
bswapl %eax
cmpl %eax, 0x3c(%r14)
je 0x409c8b
movq 0x70(%rbx), %rdi
callq 0x409fdc
testl %eax, %eax
jne 0x409cc8
movq 0x50(%rbx), %rax
movl 0x3c(%r14), %ecx
bswapl %ecx
movl %ecx, 0x1c(%rax)
movq 0x20(%rsp), %r15
testq %r15, %r15
je 0x409ca0
movq 0x18(%r14), %rax
movq 0x50(%rax), %rax
movl 0x28(%rax), %eax
bswapl %eax
movl %eax, (%r15)
xorl %r13d, %r13d
testl %r12d, %r12d
je 0x409ccb
movq (%r14), %rdi
movq (%rbp), %rax
movl 0x284(%rax), %esi
cmpl %esi, 0x80(%rdi)
jge 0x409ccb
cmpb $0x0, 0xa(%rdi)
je 0x409ccb
callq 0x420d8f
movl %eax, %r13d
cmpb $0x0, 0x11(%rbp)
je 0x409cde
decl 0x14(%rbp)
jne 0x409cde
movq %rbp, %rdi
callq 0x4202fb
movl %r13d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq