// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movq -0x8(%rbp), %rdi
callq 0x40b350
movq -0x8(%rbp), %rax
movzbl 0x10(%rax), %eax
cmpl $0x2, %eax
je 0x40b43f
movq -0x8(%rbp), %rax
movzbl 0x10(%rax), %eax
cmpl $0x1, %eax
jne 0x40b444
cmpl $0x0, -0xc(%rbp)
jne 0x40b444
jmp 0x40b7da
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl 0x30(%rax), %eax
andl $0x2000000, %eax        # imm = 0x2000000
cmpl $0x0, %eax
je 0x40b47b
movq -0x20(%rbp), %rax
movq (%rax), %rdi
callq 0x420940
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x40b47b
movq -0x20(%rbp), %rax
movzwl 0x26(%rax), %ecx
andl $-0x2, %ecx
movw %cx, 0x26(%rax)
movq -0x20(%rbp), %rax
movzwl 0x26(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x40b49d
cmpl $0x0, -0xc(%rbp)
je 0x40b49d
movl $0x8, -0x24(%rbp)
jmp 0x40b7da
movq $0x0, -0x30(%rbp)
cmpl $0x0, -0xc(%rbp)
je 0x40b4b8
movq -0x20(%rbp), %rax
movzbl 0x24(%rax), %eax
cmpl $0x2, %eax
je 0x40b4ca
movq -0x20(%rbp), %rax
movzwl 0x26(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x40b4db
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x40b525
cmpl $0x1, -0xc(%rbp)
jle 0x40b523
movq -0x20(%rbp), %rax
movq 0x70(%rax), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x40b521
movq -0x38(%rbp), %rax
movq (%rax), %rax
cmpq -0x8(%rbp), %rax
je 0x40b511
movq -0x38(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
jmp 0x40b521
jmp 0x40b513
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x40b4ed
jmp 0x40b523
jmp 0x40b525
cmpq $0x0, -0x30(%rbp)
je 0x40b538
movl $0x106, -0x24(%rbp)     # imm = 0x106
jmp 0x40b7da
movq -0x8(%rbp), %rdi
movl $0x1, %edx
movl %edx, %esi
callq 0x42b540
movl %eax, -0x24(%rbp)
xorl %eax, %eax
cmpl -0x24(%rbp), %eax
je 0x40b557
jmp 0x40b7da
movq -0x20(%rbp), %rax
movzwl 0x26(%rax), %ecx
andl $-0x11, %ecx
movw %cx, 0x26(%rax)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x3c(%rax)
jne 0x40b57f
movq -0x20(%rbp), %rax
movzwl 0x26(%rax), %ecx
orl $0x10, %ecx
movw %cx, 0x26(%rax)
jmp 0x40b581
jmp 0x40b583
movq -0x20(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, 0x18(%rcx)
movb %al, -0x41(%rbp)
jne 0x40b5ab
movq -0x20(%rbp), %rdi
callq 0x42b620
movl %eax, %ecx
movl %ecx, -0x24(%rbp)
xorl %eax, %eax
cmpl %ecx, %eax
sete %al
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
testb $0x1, %al
jne 0x40b5b4
jmp 0x40b5b6
jmp 0x40b583
cmpl $0x0, -0x24(%rbp)
jne 0x40b651
cmpl $0x0, -0xc(%rbp)
je 0x40b651
movq -0x20(%rbp), %rax
movzwl 0x26(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x40b5e3
movl $0x8, -0x24(%rbp)
jmp 0x40b64f
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
cmpl $0x1, -0xc(%rbp)
setg %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
callq 0x41ff50
movq -0x50(%rbp), %rdi
movl -0x48(%rbp), %esi
movl %eax, %edx
callq 0x42ba30
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x40b62e
movq -0x20(%rbp), %rdi
callq 0x42bb90
movl %eax, -0x24(%rbp)
jmp 0x40b64d
cmpl $0x205, -0x24(%rbp)     # imm = 0x205
jne 0x40b64b
movq -0x20(%rbp), %rax
movzbl 0x24(%rax), %eax
cmpl $0x0, %eax
jne 0x40b64b
movl $0x5, -0x24(%rbp)
jmp 0x40b64d
jmp 0x40b64f
jmp 0x40b651
cmpl $0x0, -0x24(%rbp)
je 0x40b660
movq -0x20(%rbp), %rdi
callq 0x42bd00
jmp 0x40b662
movl -0x24(%rbp), %ecx
andl $0xff, %ecx
xorl %eax, %eax
cmpl $0x5, %ecx
movb %al, -0x51(%rbp)
jne 0x40b699
movq -0x20(%rbp), %rax
movzbl 0x24(%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x51(%rbp)
jne 0x40b699
movq -0x20(%rbp), %rdi
callq 0x4208f0
cmpl $0x0, %eax
setne %al
movb %al, -0x51(%rbp)
movb -0x51(%rbp), %al
testb $0x1, %al
jne 0x40b581
cmpl $0x0, -0x24(%rbp)
jne 0x40b7d8
movq -0x8(%rbp), %rax
movzbl 0x10(%rax), %eax
cmpl $0x0, %eax
jne 0x40b6fc
movq -0x20(%rbp), %rax
movl 0x38(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x38(%rax)
movq -0x8(%rbp), %rax
cmpb $0x0, 0x11(%rax)
je 0x40b6fa
movq -0x8(%rbp), %rax
movb $0x1, 0x3c(%rax)
movq -0x20(%rbp), %rax
movq 0x70(%rax), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rcx
addq $0x30, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x70(%rax)
jmp 0x40b6fc
movl -0xc(%rbp), %edx
movl $0x1, %eax
movl $0x2, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movb %al, %cl
movq -0x8(%rbp), %rax
movb %cl, 0x10(%rax)
movq -0x8(%rbp), %rax
movzbl 0x10(%rax), %eax
movq -0x20(%rbp), %rcx
movzbl 0x24(%rcx), %ecx
cmpl %ecx, %eax
jle 0x40b73a
movq -0x8(%rbp), %rax
movb 0x10(%rax), %cl
movq -0x20(%rbp), %rax
movb %cl, 0x24(%rax)
cmpl $0x0, -0xc(%rbp)
je 0x40b7d6
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x78(%rax)
movq -0x20(%rbp), %rax
movzwl 0x26(%rax), %ecx
andl $-0x41, %ecx
movw %cx, 0x26(%rax)
cmpl $0x1, -0xc(%rbp)
jle 0x40b780
movq -0x20(%rbp), %rax
movzwl 0x26(%rax), %ecx
orl $0x40, %ecx
movw %cx, 0x26(%rax)
movq -0x20(%rbp), %rax
movl 0x3c(%rax), %eax
movl %eax, -0x58(%rbp)
movq -0x40(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x1c, %rdi
callq 0x420960
movl %eax, %ecx
movl -0x58(%rbp), %eax
cmpl %ecx, %eax
je 0x40b7d4
movq -0x40(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x40bed0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jne 0x40b7d2
movq -0x40(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x1c, %rdi
movq -0x20(%rbp), %rax
movl 0x3c(%rax), %esi
callq 0x422420
jmp 0x40b7d4
jmp 0x40b7d6
jmp 0x40b7d8
jmp 0x40b7da
cmpl $0x0, -0x24(%rbp)
jne 0x40b828
cmpq $0x0, -0x18(%rbp)
je 0x40b804
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x50(%rax), %rdi
addq $0x28, %rdi
callq 0x420960
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
cmpl $0x0, -0xc(%rbp)
je 0x40b826
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl 0x284(%rax), %esi
callq 0x42bd50
movl %eax, -0x24(%rbp)
jmp 0x40b828
movq -0x8(%rbp), %rdi
callq 0x40c4a0
movl -0x24(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)