// File: /workspace/github_c_makefile_star2_O3/genometools[P]genometools/src/external/sqlite-3.33.0/sqlite3.c
// Function: sqlite3_blob_open
SQLITE_API int sqlite3_blob_open(
  sqlite3* db,            /* The database connection */
  const char *zDb,        /* The attached database containing the blob */
  const char *zTable,     /* The table containing the blob */
  const char *zColumn,    /* The column containing the blob */
  sqlite_int64 iRow,      /* The row containing the glob */
  int wrFlag,             /* True -> read/write access, false -> read-only */
  sqlite3_blob **ppBlob   /* Handle for accessing the blob returned here */
){
  int nAttempt = 0;
  int iCol;               /* Index of zColumn in row-record */
  int rc = SQLITE_OK;
  char *zErr = 0;
  Table *pTab;
  Incrblob *pBlob = 0;
  Parse sParse;

#ifdef SQLITE_ENABLE_API_ARMOR
  if( ppBlob==0 ){
    return SQLITE_MISUSE_BKPT;
  }
#endif
  *ppBlob = 0;
#ifdef SQLITE_ENABLE_API_ARMOR
  if( !sqlite3SafetyCheckOk(db) || zTable==0 ){
    return SQLITE_MISUSE_BKPT;
  }
#endif
  wrFlag = !!wrFlag;                /* wrFlag = (wrFlag ? 1 : 0); */

  sqlite3_mutex_enter(db->mutex);

  pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
  do {
    memset(&sParse, 0, sizeof(Parse));
    if( !pBlob ) goto blob_open_out;
    sParse.db = db;
    sqlite3DbFree(db, zErr);
    zErr = 0;

    sqlite3BtreeEnterAll(db);
    pTab = sqlite3LocateTable(&sParse, 0, zTable, zDb);
    if( pTab && IsVirtual(pTab) ){
      pTab = 0;
      sqlite3ErrorMsg(&sParse, "cannot open virtual table: %s", zTable);
    }
    if( pTab && !HasRowid(pTab) ){
      pTab = 0;
      sqlite3ErrorMsg(&sParse, "cannot open table without rowid: %s", zTable);
    }
#ifndef SQLITE_OMIT_VIEW
    if( pTab && pTab->pSelect ){
      pTab = 0;
      sqlite3ErrorMsg(&sParse, "cannot open view: %s", zTable);
    }
#endif
    if( !pTab ){
      if( sParse.zErrMsg ){
        sqlite3DbFree(db, zErr);
        zErr = sParse.zErrMsg;
        sParse.zErrMsg = 0;
      }
      rc = SQLITE_ERROR;
      sqlite3BtreeLeaveAll(db);
      goto blob_open_out;
    }
    pBlob->pTab = pTab;
    pBlob->zDb = db->aDb[sqlite3SchemaToIndex(db, pTab->pSchema)].zDbSName;

    /* Now search pTab for the exact column. */
    for(iCol=0; iCol<pTab->nCol; iCol++) {
      if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
        break;
      }
    }
    if( iCol==pTab->nCol ){
      sqlite3DbFree(db, zErr);
      zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
      rc = SQLITE_ERROR;
      sqlite3BtreeLeaveAll(db);
      goto blob_open_out;
    }

    /* If the value is being opened for writing, check that the
    ** column is not indexed, and that it is not part of a foreign key.
    */
    if( wrFlag ){
      const char *zFault = 0;
      Index *pIdx;
#ifndef SQLITE_OMIT_FOREIGN_KEY
      if( db->flags&SQLITE_ForeignKeys ){
        /* Check that the column is not part of an FK child key definition. It
        ** is not necessary to check if it is part of a parent key, as parent
        ** key columns must be indexed. The check below will pick up this
        ** case.  */
        FKey *pFKey;
        for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
          int j;
          for(j=0; j<pFKey->nCol; j++){
            if( pFKey->aCol[j].iFrom==iCol ){
              zFault = "foreign key";
            }
          }
        }
      }
#endif
      for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
        int j;
        for(j=0; j<pIdx->nKeyCol; j++){
          /* FIXME: Be smarter about indexes that use expressions */
          if( pIdx->aiColumn[j]==iCol || pIdx->aiColumn[j]==XN_EXPR ){
            zFault = "indexed";
          }
        }
      }
      if( zFault ){
        sqlite3DbFree(db, zErr);
        zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
        rc = SQLITE_ERROR;
        sqlite3BtreeLeaveAll(db);
        goto blob_open_out;
      }
    }

    pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(&sParse);
    assert( pBlob->pStmt || db->mallocFailed );
    if( pBlob->pStmt ){

      /* This VDBE program seeks a btree cursor to the identified
      ** db/table/row entry. The reason for using a vdbe program instead
      ** of writing code to use the b-tree layer directly is that the
      ** vdbe program will take advantage of the various transaction,
      ** locking and error handling infrastructure built into the vdbe.
      **
      ** After seeking the cursor, the vdbe executes an OP_ResultRow.
      ** Code external to the Vdbe then "borrows" the b-tree cursor and
      ** uses it to implement the blob_read(), blob_write() and
      ** blob_bytes() functions.
      **
      ** The sqlite3_blob_close() function finalizes the vdbe program,
      ** which closes the b-tree cursor and (possibly) commits the
      ** transaction.
      */
      static const int iLn = VDBE_OFFSET_LINENO(2);
      static const VdbeOpList openBlob[] = {
        {OP_TableLock,      0, 0, 0},  /* 0: Acquire a read or write lock */
        {OP_OpenRead,       0, 0, 0},  /* 1: Open a cursor */
        /* blobSeekToRow() will initialize r[1] to the desired rowid */
        {OP_NotExists,      0, 5, 1},  /* 2: Seek the cursor to rowid=r[1] */
        {OP_Column,         0, 0, 1},  /* 3  */
        {OP_ResultRow,      1, 0, 0},  /* 4  */
        {OP_Halt,           0, 0, 0},  /* 5  */
      };
      Vdbe *v = (Vdbe *)pBlob->pStmt;
      int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
      VdbeOp *aOp;

      sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, wrFlag,
                           pTab->pSchema->schema_cookie,
                           pTab->pSchema->iGeneration);
      sqlite3VdbeChangeP5(v, 1);
      assert( sqlite3VdbeCurrentAddr(v)==2 || db->mallocFailed );
      aOp = sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);

      /* Make sure a mutex is held on the table to be accessed */
      sqlite3VdbeUsesBtree(v, iDb);

      if( db->mallocFailed==0 ){
        assert( aOp!=0 );
        /* Configure the OP_TableLock instruction */
#ifdef SQLITE_OMIT_SHARED_CACHE
        aOp[0].opcode = OP_Noop;
#else
        aOp[0].p1 = iDb;
        aOp[0].p2 = pTab->tnum;
        aOp[0].p3 = wrFlag;
        sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
      }
      if( db->mallocFailed==0 ){
#endif

        /* Remove either the OP_OpenWrite or OpenRead. Set the P2
        ** parameter of the other to pTab->tnum.  */
        if( wrFlag ) aOp[1].opcode = OP_OpenWrite;
        aOp[1].p2 = pTab->tnum;
        aOp[1].p3 = iDb;

        /* Configure the number of columns. Configure the cursor to
        ** think that the table has one more column than it really
        ** does. An OP_Column to retrieve this imaginary column will
        ** always return an SQL NULL. This is useful because it means
        ** we can invoke OP_Column to fill in the vdbe cursors type
        ** and offset cache without causing any IO.
        */
        aOp[1].p4type = P4_INT32;
        aOp[1].p4.i = pTab->nCol+1;
        aOp[3].p2 = pTab->nCol;

        sParse.nVar = 0;
        sParse.nMem = 1;
        sParse.nTab = 1;
        sqlite3VdbeMakeReady(v, &sParse);
      }
    }

    pBlob->iCol = iCol;
    pBlob->db = db;
    sqlite3BtreeLeaveAll(db);
    if( db->mallocFailed ){
      goto blob_open_out;
    }
    rc = blobSeekToRow(pBlob, iRow, &zErr);
  } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );

blob_open_out:
  if( rc==SQLITE_OK && db->mallocFailed==0 ){
    *ppBlob = (sqlite3_blob *)pBlob;
  }else{
    if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
    sqlite3DbFree(db, pBlob);
  }
  sqlite3ErrorWithMsg(db, rc, (zErr ? "%s" : 0), zErr);
  sqlite3DbFree(db, zErr);
  sqlite3ParserReset(&sParse);
  rc = sqlite3ApiExit(db, rc);
  sqlite3_mutex_leave(db->mutex);
  return rc;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %r8, 0x50(%rsp)
movq %rcx, %r12
movq %rdx, 0x48(%rsp)
movq %rsi, 0x58(%rsp)
movq %rdi, %rbp
movq 0x240(%rsp), %r15
xorl %eax, %eax
movq %rax, 0x8(%rsp)
movq $0x0, 0x38(%rsp)
movq %rax, (%r15)
xorl %r13d, %r13d
movl %r9d, 0x24(%rsp)
testl %r9d, %r9d
setne %bl
movl $0x38, %esi
callq 0x279b27
movq %rax, %r14
leaq 0x60(%rsp), %rdi
movl $0x1a8, %edx            # imm = 0x1A8
xorl %esi, %esi
callq 0x173a0
testq %r14, %r14
je 0x27b09e
movb %bl, %r13b
movl %r13d, 0x1c(%rsp)
leaq 0x15d613(%rip), %rbx    # 0x3d8273
leaq 0x15ad16(%rip), %r15    # 0x3d597d
xorl %r13d, %r13d
movq %rbp, 0x28(%rsp)
movq %r14, 0x40(%rsp)
movq %rbp, 0x60(%rsp)
movq 0x8(%rsp), %rsi
testq %rsi, %rsi
je 0x27ac8b
movq %rbp, %rdi
callq 0x282904
movq $0x0, 0x8(%rsp)
movslq 0x28(%rbp), %rax
testq %rax, %rax
jle 0x27acc5
movq 0x20(%rbp), %rcx
shlq $0x5, %rax
xorl %edx, %edx
movq 0x8(%rcx,%rdx), %rsi
testq %rsi, %rsi
je 0x27acbc
movq (%rsi), %rdi
movq 0x8(%rsi), %rsi
movq %rdi, 0x8(%rsi)
addq $0x20, %rdx
cmpq %rdx, %rax
jne 0x27aca7
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
movq 0x48(%rsp), %rdx
movq 0x58(%rsp), %rcx
callq 0x27b1c7
testq %rax, %rax
je 0x27b0f8
cmpl $0x0, 0x54(%rax)
jne 0x27b0ce
testb $-0x80, 0x40(%rax)
jne 0x27b0d7
cmpq $0x0, 0x18(%rax)
jne 0x27b0e0
movq %rax, 0x30(%r14)
movq 0x20(%rbp), %r9
movq 0x70(%rax), %rdx
testq %rdx, %rdx
je 0x27ad48
leaq 0x18(%r9), %rsi
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
leaq 0x1565b7(%rip), %r10    # 0x3d12e0
movabsq $0x100000000, %r8       # imm = 0x100000000
addq %r8, %rcx
leaq 0x20(%rsi), %rdi
cmpq %rdx, (%rsi)
movq %rdi, %rsi
jne 0x27ad33
sarq $0x20, %rcx
jmp 0x27ad56
movq $-0x8000, %rcx          # imm = 0x8000
leaq 0x15658a(%rip), %r10    # 0x3d12e0
movl %r13d, 0x20(%rsp)
shlq $0x5, %rcx
movq (%r9,%rcx), %rcx
movq %rcx, 0x28(%r14)
movswl 0x46(%rax), %r11d
testl %r11d, %r11d
jle 0x27adba
movq 0x8(%rax), %rcx
movl %r11d, %edx
xorl %r13d, %r13d
movq %r13, %rsi
shlq $0x5, %rsi
movq (%rcx,%rsi), %rsi
xorl %edi, %edi
movzbl (%rsi,%rdi), %r8d
movzbl (%r12,%rdi), %r9d
cmpb %r9b, %r8b
jne 0x27ad9e
testq %r8, %r8
jne 0x27ada8
jmp 0x27adbd
movb (%r8,%r10), %r8b
cmpb (%r9,%r10), %r8b
jne 0x27adad
incq %rdi
jmp 0x27ad88
incq %r13
cmpq %rdx, %r13
jne 0x27ad7b
jmp 0x27b065
xorl %r13d, %r13d
cmpl %r11d, %r13d
je 0x27b065
cmpl $0x0, 0x24(%rsp)
je 0x27ae54
testb $0x40, 0x31(%rbp)
je 0x27ae0e
movq 0x20(%rax), %rdi
testq %rdi, %rdi
je 0x27ae0e
xorl %edx, %edx
movslq 0x28(%rdi), %rcx
testq %rcx, %rcx
jle 0x27ae03
shlq $0x4, %rcx
xorl %esi, %esi
cmpl %r13d, 0x40(%rdi,%rsi)
cmoveq %r15, %rdx
addq $0x10, %rsi
cmpq %rsi, %rcx
jne 0x27adf1
movq 0x8(%rdi), %rdi
testq %rdi, %rdi
jne 0x27ade2
jmp 0x27ae10
xorl %edx, %edx
movq 0x10(%rax), %r9
testq %r9, %r9
je 0x27ae4b
movzwl 0x5e(%r9), %ecx
testq %rcx, %rcx
je 0x27ae45
movq 0x8(%r9), %rsi
xorl %edi, %edi
movswl (%rsi,%rdi,2), %r8d
cmpl %r8d, %r13d
cmoveq %rbx, %rdx
cmpl $-0x2, %r8d
cmoveq %rbx, %rdx
incq %rdi
cmpq %rdi, %rcx
jne 0x27ae29
movq 0x28(%r9), %r9
jmp 0x27ae14
testq %rdx, %rdx
jne 0x27b118
movq %rax, 0x30(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x27b5ee
movq %rax, 0x18(%r14)
testq %rax, %rax
je 0x27b007
movq %rax, %rsi
movq 0x30(%rsp), %rax
movq 0x70(%rax), %rax
testq %rax, %rax
je 0x27aea0
movq 0x20(%rbp), %rcx
addq $0x18, %rcx
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
incl %r14d
leaq 0x20(%rcx), %rdx
cmpq %rax, (%rcx)
movq %rdx, %rcx
jne 0x27ae8f
jmp 0x27aea6
movl $0xffff8000, %r14d      # imm = 0xFFFF8000
movl (%rax), %r8d
movl 0x4(%rax), %ebp
movq %rsi, 0x10(%rsp)
movq 0x10(%rsp), %rdi
movl $0x2, %esi
movl %r14d, %edx
movl 0x1c(%rsp), %ecx
callq 0x2a83e1
movq 0x10(%rsp), %rdx
movq (%rdx), %rcx
cmpb $0x0, 0x61(%rcx)
jne 0x27aeeb
movq 0x88(%rdx), %rcx
cltq
leaq (%rax,%rax,2), %rax
movb $-0x3, 0x1(%rcx,%rax,8)
movl %ebp, 0x10(%rcx,%rax,8)
movslq 0x90(%rdx), %rax
testq %rax, %rax
jle 0x27af09
movq 0x88(%rdx), %rcx
leaq (%rax,%rax,2), %rax
movw $0x1, -0x16(%rcx,%rax,8)
movq 0x10(%rsp), %rdi
movl $0x6, %esi
leaq 0x156506(%rip), %rdx    # 0x3d1420
callq 0x27b673
movq 0x10(%rsp), %rsi
movq %rax, %rbp
movl $0x1, %eax
movl %r14d, %ecx
shll %cl, %eax
orl %eax, 0xc8(%rsi)
cmpl $0x1, %r14d
je 0x27af5c
movq (%rsi), %rcx
movq 0x20(%rcx), %rcx
movslq %r14d, %rdx
shlq $0x5, %rdx
movq 0x8(%rcx,%rdx), %rcx
cmpb $0x0, 0x11(%rcx)
je 0x27af5c
orl %eax, 0xcc(%rsi)
movq 0x28(%rsp), %rax
cmpb $0x0, 0x61(%rax)
movq 0x30(%rsp), %rcx
jne 0x27b077
movl %r14d, 0x4(%rbp)
movl 0x38(%rcx), %eax
movl %eax, 0x8(%rbp)
movl 0x1c(%rsp), %eax
movl %eax, 0xc(%rbp)
movq (%rcx), %rdx
movq 0x10(%rsp), %rdi
movl $0x2, %esi
xorl %ecx, %ecx
callq 0x27b778
movq 0x28(%rsp), %rax
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %rcx
cmpb $0x0, 0x61(%rax)
jne 0x27b077
cmpl $0x0, 0x24(%rsp)
je 0x27afb9
movb $0x61, 0x18(%rbp)
movl 0x38(%rcx), %eax
movl %eax, 0x20(%rbp)
movl %r14d, 0x24(%rbp)
movb $-0x3, 0x19(%rbp)
movswl 0x46(%rcx), %eax
incl %eax
movl %eax, 0x28(%rbp)
movswl 0x46(%rcx), %eax
movl %eax, 0x50(%rbp)
movw $0x0, 0x178(%rsp)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x94(%rsp)
leaq 0x60(%rsp), %rsi
callq 0x27b7e3
movq 0x28(%rsp), %rbp
movq 0x40(%rsp), %r14
cmpb $0x0, 0x61(%rbp)
movw %r13w, 0x8(%r14)
movq %rbp, 0x20(%r14)
movl 0x20(%rsp), %r13d
jne 0x27b08d
movq %r14, %rdi
movq 0x50(%rsp), %rsi
leaq 0x8(%rsp), %rdx
callq 0x27bc1e
movl %eax, %r13d
cmpl $0x30, 0x38(%rsp)
ja 0x27b08d
cmpl $0x11, %r13d
jne 0x27b08d
movq 0x38(%rsp), %rax
incl %eax
movq %rax, 0x38(%rsp)
movl $0x1a8, %edx            # imm = 0x1A8
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x173a0
movl $0x11, %r13d
jmp 0x27ac74
leaq 0x15a8fc(%rip), %rsi    # 0x3d5968
movq %rbp, %rdi
movq %r12, %rdx
jmp 0x27b122
movq 0x40(%rsp), %r14
movw %r13w, 0x8(%r14)
movq %rax, 0x20(%r14)
movq %rax, %rbp
movl 0x20(%rsp), %r13d
testl %r13d, %r13d
movq 0x240(%rsp), %r15
jne 0x27b139
xorl %r13d, %r13d
cmpb $0x0, 0x61(%rbp)
jne 0x27b134
movq %r14, (%r15)
movq 0x8(%rsp), %r14
testq %r14, %r14
leaq 0x14d9ec(%rip), %rdx    # 0x3c8aa9
cmoveq %r14, %rdx
xorl %r13d, %r13d
movq %rbp, %rdi
xorl %esi, %esi
jmp 0x27b16b
leaq 0x15a83c(%rip), %rsi    # 0x3d5911
jmp 0x27b0e7
leaq 0x15a851(%rip), %rsi    # 0x3d592f
jmp 0x27b0e7
leaq 0x15a86c(%rip), %rsi    # 0x3d5953
leaq 0x60(%rsp), %rdi
movq 0x48(%rsp), %rdx
xorl %eax, %eax
callq 0x27b48d
movq 0x68(%rsp), %rax
movl $0x1, %r13d
testq %rax, %rax
je 0x27b134
movq %rax, 0x8(%rsp)
movq $0x0, 0x68(%rsp)
jmp 0x27b134
leaq 0x15a86a(%rip), %rsi    # 0x3d5989
movq %rbp, %rdi
xorl %eax, %eax
callq 0x27b56a
movq %rax, 0x8(%rsp)
movl $0x1, %r13d
testq %r14, %r14
je 0x27b152
movq 0x18(%r14), %rdi
testq %rdi, %rdi
je 0x27b147
callq 0x277f75
movq %rbp, %rdi
movq %r14, %rsi
callq 0x282904
movq 0x8(%rsp), %r14
testq %r14, %r14
leaq 0x14d948(%rip), %rdx    # 0x3c8aa9
cmoveq %r14, %rdx
movq %rbp, %rdi
movl %r13d, %esi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2762da
testq %r14, %r14
je 0x27b185
movq %rbp, %rdi
movq %r14, %rsi
callq 0x282904
leaq 0x60(%rsp), %rdi
callq 0x27bd9d
cmpl $0xc0a, %r13d           # imm = 0xC0A
je 0x27b1a4
cmpb $0x0, 0x61(%rbp)
jne 0x27b1a4
andl 0x54(%rbp), %r13d
jmp 0x27b1b2
movq %rbp, %rdi
callq 0x28fd56
movl $0x7, %r13d
movl %r13d, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq