// File: /workspace/github_c_makefile_star2_O3/barabo[P]advanced-shell-history/src/sqlite3.c
// Function: sqlite3BtreeBeginTrans
SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
  sqlite3 *pBlock = 0;
  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( IfNotOmitAV(pBt->bDoTruncate)==0 );

  /* 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
  /* 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);
        }
      }
    }
  
    if( rc!=SQLITE_OK ){
      unlockBtreeIfUnused(pBt);
    }
  }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
          btreeInvokeBusyHandler(pBt) );

  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 && 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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r12
movq (%rdi), %rax
movq 0x8(%rdi), %r14
movq %rax, 0x8(%r14)
movb 0x10(%rdi), %al
cmpb $0x2, %al
jne 0xd200
testl %esi, %esi
je 0xd8cd
movq (%r14), %rdi
movq (%r12), %rax
movl 0x30c(%rax), %esi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1fb6a
cmpb $0x1, %al
sete %al
testl %esi, %esi
sete %cl
testb %al, %cl
jne 0xd8cd
movzwl 0x26(%r14), %eax
testl %esi, %esi
je 0xd22c
movl %eax, %ecx
movl $0x8, %r13d
andl $0x1, %ecx
jne 0xd8d0
testl %esi, %esi
je 0xd257
testb $0x40, %al
jne 0xd25b
cmpb $0x2, 0x24(%r14)
je 0xd25b
cmpl $0x2, %esi
jl 0xd26f
movq 0x70(%r14), %rax
testq %rax, %rax
je 0xd26f
movq (%rax), %rcx
cmpq %r12, %rcx
jne 0xd25f
movq 0x10(%rax), %rax
jmp 0xd244
testb $0x40, %al
je 0xd26f
movq 0x78(%r14), %rcx
movl $0x106, %r13d           # imm = 0x106
cmpq $0x0, (%rcx)
jne 0xd8d0
movl %esi, -0x2c(%rbp)
movq %r12, %rdi
movl $0x1, %esi
movl $0x1, %edx
callq 0x1fa4b
testl %eax, %eax
je 0xd290
movl %eax, %r13d
jmp 0xd8d0
movzwl 0x26(%r14), %eax
movl %eax, %ecx
andl $-0x9, %ecx
movw %cx, 0x26(%r14)
cmpl $0x0, 0x3c(%r14)
jne 0xd2ae
orl $0x8, %eax
movw %ax, 0x26(%r14)
leaq 0x30(%r14), %rax
movq %rax, -0x58(%rbp)
xorl %r13d, %r13d
movq %r14, -0x38(%rbp)
movq %r12, -0x48(%rbp)
cmpq $0x0, 0x18(%r14)
je 0xd399
movl -0x2c(%rbp), %edx
testl %edx, %edx
je 0xd785
testl %r13d, %r13d
jne 0xd785
movl $0x8, %r13d
testb $0x1, 0x26(%r14)
jne 0xd7b0
movq (%r14), %r15
movl 0x2c(%r15), %r13d
testl %r13d, %r13d
jne 0xd7b0
movq (%r12), %rax
cmpb $0x2, 0x4f(%rax)
sete 0x18(%r15)
cmpb $0x1, 0x13(%r15)
jne 0xd777
movq 0x118(%r15), %rbx
testq %rbx, %rbx
je 0xd60d
cmpb $0x0, 0x8(%r15)
je 0xd6ff
cmpb $0x0, 0x3f(%rbx)
jne 0xd6ff
movzbl 0x14(%r15), %eax
cmpl $0x4, %eax
jb 0xd351
cmpl $0x5, %eax
jne 0xd6d1
movq 0x48(%r15), %rdi
movq (%rdi), %rax
movl $0x4, %esi
callq *0x38(%rax)
testl %eax, %eax
jne 0xd7ad
movb $0x4, 0x14(%r15)
movq 0x118(%r15), %rbx
leaq 0x3f(%rbx), %r14
cmpb $0x0, 0x3f(%rbx)
jne 0xd6f7
jmp 0xd6d5
movq 0x68(%r8), %rdi
callq 0xddd3
movq -0x48(%rbp), %r12
jmp 0xd5ca
movq (%r14), %rdi
callq 0x1fc9c
testl %eax, %eax
jne 0xd7ad
movq %r14, %rdi
movl $0x1, %esi
leaq -0x60(%rbp), %rdx
xorl %ecx, %ecx
callq 0x20068
testl %eax, %eax
jne 0xd7ad
movq -0x60(%rbp), %r8
movq 0x50(%r8), %rbx
movl 0x1c(%rbx), %r15d
bswapl %r15d
movq (%r14), %rdi
movl 0x1c(%rdi), %esi
testl %r15d, %r15d
je 0xd3e6
movl 0x18(%rbx), %eax
cmpl 0x5c(%rbx), %eax
je 0xd3e9
movl %esi, %r15d
testl %r15d, %r15d
jle 0xd511
movdqu (%rbx), %xmm0
pcmpeqb 0x6bf72(%rip), %xmm0    # 0x79370
pmovmskb %xmm0, %eax
movl $0x1a, %r13d
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0xd69a
cmpb $0x3, 0x12(%rbx)
jb 0xd41e
orb $0x1, 0x26(%r14)
cmpb $0x2, 0x13(%rbx)
ja 0xd69a
jne 0xd45f
testb $0x10, 0x26(%r14)
jne 0xd45f
movl %esi, -0x4c(%rbp)
movq %r8, %r12
movl $0x0, -0x3c(%rbp)
leaq -0x3c(%rbp), %rsi
callq 0x200c6
testl %eax, %eax
jne 0xd694
cmpl $0x0, -0x3c(%rbp)
movq %r12, %r8
je 0xd387
movl -0x4c(%rbp), %esi
movzwl 0x15(%rbx), %eax
xorl $0x2040, %eax           # imm = 0x2040
movzbl 0x17(%rbx), %ecx
xorl $0x20, %ecx
orw %ax, %cx
jne 0xd69a
movzbl 0x10(%rbx), %eax
shll $0x8, %eax
movzbl 0x11(%rbx), %ecx
shll $0x10, %ecx
leal (%rcx,%rax), %edx
addl $0xfffeffff, %edx       # imm = 0xFFFEFFFF
cmpl $0xffff0100, %edx       # imm = 0xFFFF0100
jb 0xd69a
movl %ecx, %r12d
orl %eax, %r12d
addl %ecx, %eax
decl %eax
andl %r12d, %eax
jne 0xd69a
movzbl 0x14(%rbx), %ecx
movl %r12d, %r14d
subl %ecx, %r14d
movq -0x58(%rbp), %rax
cmpl (%rax), %r12d
jne 0xd57b
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
testb $0x1, 0x2e(%rax)
jne 0xd4dc
cmpl %esi, %r15d
jg 0xd5dd
cmpl $0x1e0, %r14d           # imm = 0x1E0
jb 0xd69a
movq -0x38(%rbp), %rdx
movl %r12d, 0x30(%rdx)
movl %r14d, 0x34(%rdx)
movl 0x34(%rbx), %eax
bswapl %eax
testl %eax, %eax
setne 0x21(%rdx)
movl 0x40(%rbx), %eax
bswapl %eax
testl %eax, %eax
setne 0x22(%rdx)
movq -0x48(%rbp), %r12
jmp 0xd518
movq %r14, %rdx
movl 0x34(%r14), %r14d
movl %r14d, %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(%rdx)
movl %r14d, %ecx
shll $0x5, %ecx
addl $0xfffffe80, %ecx       # imm = 0xFFFFFE80
imulq %rsi, %rcx
shrq $0x27, %rcx
addl $-0x17, %ecx
movw %cx, 0x2a(%rdx)
addl $-0x23, %r14d
movw %r14w, 0x2c(%rdx)
cmpw $0x7f, %ax
movl $0x7f, %esi
cmovael %esi, %eax
movw %cx, 0x2e(%rdx)
movb %al, 0x25(%rdx)
movq %r8, 0x18(%rdx)
movl %r15d, 0x3c(%rdx)
movq %rdx, %r14
jmp 0xd5ca
movq 0x68(%r8), %rdi
movq %rax, %rbx
movl %ecx, %r15d
callq 0xddd3
movq -0x38(%rbp), %rax
movl %r14d, 0x34(%rax)
movq %rax, %r14
movl %r12d, 0x30(%rax)
movq 0x80(%rax), %rdi
callq 0x1cdc9
movq $0x0, 0x80(%r14)
movq (%r14), %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x1a26a
testl %eax, %eax
movq -0x48(%rbp), %r12
jne 0xd7ad
cmpq $0x0, 0x18(%r14)
je 0xd399
xorl %r13d, %r13d
jmp 0xd2cc
movl $0xb, %r13d
movl $0xb, %edi
leaq 0x6c225(%rip), %rsi     # 0x79814
movl $0xcb54, %edx           # imm = 0xCB54
leaq 0x6bf3e(%rip), %rcx     # 0x79539
xorl %eax, %eax
movq %r8, %rbx
callq 0xb9c7
movq %rbx, %r8
jmp 0xd69a
movzbl 0x14(%r15), %eax
cmpl $0x5, %eax
ja 0xd647
movl $0x23, %ecx
btl %eax, %ecx
jae 0xd647
movq 0x48(%r15), %rdi
movq (%rdi), %rax
movl $0x2, %esi
callq *0x38(%rax)
testl %eax, %eax
jne 0xd7ad
cmpb $0x5, 0x14(%r15)
movl -0x2c(%rbp), %edx
je 0xd647
movb $0x2, 0x14(%r15)
cmpl $0x2, %edx
jl 0xd75a
movzbl 0x14(%r15), %eax
cmpl $0x4, %eax
jb 0xd663
cmpl $0x5, %eax
jne 0xd75a
movq 0x48(%r15), %rdi
movq (%rdi), %rax
movl $0x4, %esi
callq *0x38(%rax)
cmpl $0x5, %eax
jne 0xd6b8
movq 0xe8(%r15), %rdi
callq *0xe0(%r15)
movl $0x5, %r13d
testl %eax, %eax
jne 0xd650
jmp 0xd7b0
movl %eax, %r13d
movq %r12, %r8
movq 0x68(%r8), %rdi
callq 0xddd3
movq -0x38(%rbp), %r14
movq $0x0, 0x18(%r14)
movq -0x48(%rbp), %r12
jmp 0xd7b0
movl %eax, %r13d
testl %eax, %eax
jne 0xd6c9
movb $0x4, 0x14(%r15)
jmp 0xd75a
movl -0x2c(%rbp), %edx
jmp 0xd785
leaq 0x3f(%rbx), %r14
movswl 0x3c(%rbx), %esi
addl $0x3, %esi
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
movl $0x1, %edx
movl $0x5, %ecx
callq *0x70(%rax)
movq 0x118(%r15), %rbx
movb $0x1, (%r14)
movq -0x38(%rbp), %r14
cmpb $0x0, 0x42(%rbx)
movl $0x8, %r13d
jne 0xd7b0
cmpb $0x0, 0x3f(%rbx)
je 0xd78f
movb $0x1, 0x40(%rbx)
leaq 0x48(%rbx), %rdi
movq 0x30(%rbx), %rax
movq (%rax), %rsi
movl $0x30, %edx
callq 0x8410
testl %eax, %eax
je 0xd75a
cmpb $0x0, 0x3f(%rbx)
jne 0xd74e
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
xorl %esi, %esi
movl $0x1, %edx
movl $0x9, %ecx
callq *0x70(%rax)
movb $0x0, 0x40(%rbx)
movl $0x205, %r13d           # imm = 0x205
jmp 0xd7b0
movb $0x2, 0x13(%r15)
movl 0x1c(%r15), %eax
movl %eax, 0x28(%r15)
movl %eax, 0x24(%r15)
movl %eax, 0x20(%r15)
movq $0x0, 0x60(%r15)
movq %r14, %rdi
callq 0x1fa9f
movl -0x2c(%rbp), %edx
movl %eax, %r13d
testl %r13d, %r13d
jne 0xd7b0
jmp 0xd82f
movq 0x8(%rbx), %rdi
movq (%rdi), %rax
xorl %esi, %esi
movl $0x1, %edx
movl $0xa, %ecx
callq *0x70(%rax)
testl %eax, %eax
je 0xd715
movl %eax, %r13d
cmpb $0x0, 0x24(%r14)
jne 0xd7d1
movq 0x18(%r14), %rax
testq %rax, %rax
je 0xd7d1
movq 0x68(%rax), %rdi
callq 0xddd3
movq $0x0, 0x18(%r14)
cmpb $0x5, %r13b
jne 0xd8d0
cmpb $0x0, 0x24(%r14)
jne 0xd8d0
movq 0x8(%r14), %rbx
movq 0x2a8(%rbx), %rax
testq %rax, %rax
je 0xd8d0
movl 0x2b8(%rbx), %esi
testl %esi, %esi
js 0xd8d0
movq 0x2b0(%rbx), %rdi
callq *%rax
testl %eax, %eax
je 0xd820
incl 0x2b8(%rbx)
jmp 0xd2c1
movl $0xffffffff, 0x2b8(%rbx) # imm = 0xFFFFFFFF
jmp 0xd8d0
cmpb $0x0, 0x10(%r12)
jne 0xd85b
incl 0x38(%r14)
cmpb $0x0, 0x11(%r12)
je 0xd85b
leaq 0x30(%r12), %rax
movb $0x1, 0x3c(%r12)
movq 0x70(%r14), %rcx
movq %rcx, 0x40(%r12)
movq %rax, 0x70(%r14)
cmpl $0x1, %edx
movb $0x1, %al
sbbb $-0x1, %al
movb %al, 0x10(%r12)
cmpb 0x24(%r14), %al
jbe 0xd871
movb %al, 0x24(%r14)
testl %edx, %edx
je 0xd8cd
movq 0x18(%r14), %rbx
movq %r12, 0x78(%r14)
movzwl 0x26(%r14), %eax
andl $-0x21, %eax
xorl %ecx, %ecx
cmpl $0x2, %edx
setge %cl
shll $0x5, %ecx
orl %eax, %ecx
movw %cx, 0x26(%r14)
movq 0x50(%rbx), %rax
movl 0x1c(%rax), %eax
bswapl %eax
cmpl %eax, 0x3c(%r14)
je 0xd1e0
movq 0x68(%rbx), %rdi
callq 0xdfbf
testl %eax, %eax
jne 0xd288
movq 0x50(%rbx), %rax
movl 0x3c(%r14), %ecx
bswapl %ecx
movl %ecx, 0x1c(%rax)
jmp 0xd1e0
xorl %r13d, %r13d
movl %r13d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq