// File: /workspace/github_c_makefile_star2_O0/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
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)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x8(%rbp), %rdi
callq 0x3c5aa0
movq -0x8(%rbp), %rax
movzbl 0x10(%rax), %eax
cmpl $0x2, %eax
je 0x3c5b7a
movq -0x8(%rbp), %rax
movzbl 0x10(%rax), %eax
cmpl $0x1, %eax
jne 0x3c5b7f
cmpl $0x0, -0xc(%rbp)
jne 0x3c5b7f
jmp 0x3c5f28
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq 0x30(%rax), %rax
andq $0x2000000, %rax        # imm = 0x2000000
cmpq $0x0, %rax
je 0x3c5bb6
movq -0x28(%rbp), %rdi
callq 0x3db700
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x3c5bb6
movq -0x20(%rbp), %rax
movzwl 0x28(%rax), %ecx
andl $-0x2, %ecx
movw %cx, 0x28(%rax)
movq -0x20(%rbp), %rax
movzwl 0x28(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x3c5bd8
cmpl $0x0, -0xc(%rbp)
je 0x3c5bd8
movl $0x8, -0x2c(%rbp)
jmp 0x3c5f28
movq $0x0, -0x38(%rbp)
cmpl $0x0, -0xc(%rbp)
je 0x3c5bf3
movq -0x20(%rbp), %rax
movzbl 0x24(%rax), %eax
cmpl $0x2, %eax
je 0x3c5c05
movq -0x20(%rbp), %rax
movzwl 0x28(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x3c5c19
movq -0x20(%rbp), %rax
movq 0x80(%rax), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x3c5c63
cmpl $0x1, -0xc(%rbp)
jle 0x3c5c61
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x3c5c5f
movq -0x40(%rbp), %rax
movq (%rax), %rax
cmpq -0x8(%rbp), %rax
je 0x3c5c4f
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq (%rax), %rax
movq %rax, -0x38(%rbp)
jmp 0x3c5c5f
jmp 0x3c5c51
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x3c5c2b
jmp 0x3c5c61
jmp 0x3c5c63
cmpq $0x0, -0x38(%rbp)
je 0x3c5c86
movq -0x8(%rbp), %rax
movq (%rax), %rdi
movq -0x38(%rbp), %rsi
callq 0x3e6310
movl $0x106, -0x2c(%rbp)     # imm = 0x106
jmp 0x3c5f28
movq -0x8(%rbp), %rdi
movl $0x1, %edx
movl %edx, %esi
callq 0x3e6370
movl %eax, -0x2c(%rbp)
xorl %eax, %eax
cmpl -0x2c(%rbp), %eax
je 0x3c5ca5
jmp 0x3c5f28
movq -0x20(%rbp), %rax
movzwl 0x28(%rax), %ecx
andl $-0x11, %ecx
movw %cx, 0x28(%rax)
movq -0x20(%rbp), %rax
cmpl $0x0, 0x40(%rax)
jne 0x3c5ccd
movq -0x20(%rbp), %rax
movzwl 0x28(%rax), %ecx
orl $0x10, %ecx
movw %cx, 0x28(%rax)
jmp 0x3c5ccf
jmp 0x3c5cd1
movq -0x20(%rbp), %rcx
xorl %eax, %eax
cmpq $0x0, 0x18(%rcx)
movb %al, -0x49(%rbp)
jne 0x3c5cf9
movq -0x20(%rbp), %rdi
callq 0x3e6490
movl %eax, %ecx
movl %ecx, -0x2c(%rbp)
xorl %eax, %eax
cmpl %ecx, %eax
sete %al
movb %al, -0x49(%rbp)
movb -0x49(%rbp), %al
testb $0x1, %al
jne 0x3c5d02
jmp 0x3c5d04
jmp 0x3c5cd1
cmpl $0x0, -0x2c(%rbp)
jne 0x3c5d9c
cmpl $0x0, -0xc(%rbp)
je 0x3c5d9c
movq -0x20(%rbp), %rax
movzwl 0x28(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x3c5d31
movl $0x8, -0x2c(%rbp)
jmp 0x3c5d9a
movq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpl $0x1, -0xc(%rbp)
setg %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
callq 0x3dac70
movq -0x58(%rbp), %rdi
movl -0x50(%rbp), %esi
movl %eax, %edx
callq 0x3e68b0
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x3c5d79
movq -0x20(%rbp), %rdi
callq 0x3e6a10
movl %eax, -0x2c(%rbp)
jmp 0x3c5d98
cmpl $0x205, -0x2c(%rbp)     # imm = 0x205
jne 0x3c5d96
movq -0x20(%rbp), %rax
movzbl 0x24(%rax), %eax
cmpl $0x0, %eax
jne 0x3c5d96
movl $0x5, -0x2c(%rbp)
jmp 0x3c5d98
jmp 0x3c5d9a
jmp 0x3c5d9c
cmpl $0x0, -0x2c(%rbp)
je 0x3c5dab
movq -0x20(%rbp), %rdi
callq 0x3e6b80
jmp 0x3c5dad
movl -0x2c(%rbp), %ecx
andl $0xff, %ecx
xorl %eax, %eax
cmpl $0x5, %ecx
movb %al, -0x59(%rbp)
jne 0x3c5de4
movq -0x20(%rbp), %rax
movzbl 0x24(%rax), %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x59(%rbp)
jne 0x3c5de4
movq -0x20(%rbp), %rdi
callq 0x3db6d0
cmpl $0x0, %eax
setne %al
movb %al, -0x59(%rbp)
movb -0x59(%rbp), %al
testb $0x1, %al
jne 0x3c5ccf
cmpl $0x0, -0x2c(%rbp)
jne 0x3c5f26
movq -0x8(%rbp), %rax
movzbl 0x10(%rax), %eax
cmpl $0x0, %eax
jne 0x3c5e47
movq -0x20(%rbp), %rax
movl 0x3c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x3c(%rax)
movq -0x8(%rbp), %rax
cmpb $0x0, 0x11(%rax)
je 0x3c5e45
movq -0x8(%rbp), %rax
movb $0x1, 0x3c(%rax)
movq -0x20(%rbp), %rax
movq 0x78(%rax), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rcx
addq $0x30, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x78(%rax)
jmp 0x3c5e47
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 0x3c5e85
movq -0x8(%rbp), %rax
movb 0x10(%rax), %cl
movq -0x20(%rbp), %rax
movb %cl, 0x24(%rax)
cmpl $0x0, -0xc(%rbp)
je 0x3c5f24
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x80(%rax)
movq -0x20(%rbp), %rax
movzwl 0x28(%rax), %ecx
andl $-0x41, %ecx
movw %cx, 0x28(%rax)
cmpl $0x1, -0xc(%rbp)
jle 0x3c5ece
movq -0x20(%rbp), %rax
movzwl 0x28(%rax), %ecx
orl $0x40, %ecx
movw %cx, 0x28(%rax)
movq -0x20(%rbp), %rax
movl 0x40(%rax), %eax
movl %eax, -0x60(%rbp)
movq -0x48(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x1c, %rdi
callq 0x3db720
movl %eax, %ecx
movl -0x60(%rbp), %eax
cmpl %ecx, %eax
je 0x3c5f22
movq -0x48(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x3c6610
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jne 0x3c5f20
movq -0x48(%rbp), %rax
movq 0x50(%rax), %rdi
addq $0x1c, %rdi
movq -0x20(%rbp), %rax
movl 0x40(%rax), %esi
callq 0x3dd200
jmp 0x3c5f22
jmp 0x3c5f24
jmp 0x3c5f26
jmp 0x3c5f28
cmpl $0x0, -0x2c(%rbp)
jne 0x3c5f73
cmpq $0x0, -0x18(%rbp)
je 0x3c5f52
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x50(%rax), %rdi
addq $0x28, %rdi
callq 0x3db720
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
cmpl $0x0, -0xc(%rbp)
je 0x3c5f71
movq -0x28(%rbp), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl 0x2b8(%rax), %esi
callq 0x3e6bd0
movl %eax, -0x2c(%rbp)
jmp 0x3c5f73
movl -0x2c(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)