// File: /workspace/github_c_makefile_star2_O2/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, 0x40(%rsp)
movq %rdx, 0x38(%rsp)
movq %rsi, 0x58(%rsp)
leaq 0x18(%rsp), %rax
andq $0x0, (%rax)
movq 0x240(%rsp), %rax
andq $0x0, (%rax)
movq %rdi, %r13
xorl %eax, %eax
movl %r9d, 0x2c(%rsp)
testl %r9d, %r9d
setne %al
movl %eax, 0x28(%rsp)
pushq $0x38
popq %rsi
callq 0x2258ed
movq %rax, %r12
leaq 0x60(%rsp), %r14
leaq 0x151fd4(%rip), %rbx    # 0x378693
leaq 0x14f6d7(%rip), %r15    # 0x375d9d
xorl %eax, %eax
movl $0x0, 0x30(%rsp)
movq %r13, 0x10(%rsp)
movq %r12, 0x20(%rsp)
movl %eax, 0x34(%rsp)
movl $0x1a8, %edx            # imm = 0x1A8
movq %r14, %rdi
xorl %esi, %esi
callq 0x17380
testq %r12, %r12
je 0x226a13
movq %r13, 0x60(%rsp)
movq 0x18(%rsp), %rsi
movq %r13, %rdi
callq 0x21f26b
andq $0x0, 0x18(%rsp)
movq %r13, %rdi
callq 0x21f0e2
movq %r14, %rdi
xorl %esi, %esi
movq 0x38(%rsp), %rdx
movq 0x58(%rsp), %rcx
callq 0x226b48
testq %rax, %rax
je 0x226a5c
movq %rax, %rbp
cmpl $0x0, 0x54(%rax)
jne 0x226a32
testb $-0x80, 0x40(%rbp)
jne 0x226a3b
cmpq $0x0, 0x18(%rbp)
jne 0x226a44
movq %rbp, 0x30(%r12)
movq 0x20(%r13), %r14
movq 0x70(%rbp), %rsi
movq %r13, %rdi
callq 0x226e9e
cltq
shlq $0x5, %rax
movq (%r14,%rax), %rax
movq %rax, 0x28(%r12)
movswl 0x46(%rbp), %eax
testl %eax, %eax
movl $0x0, %r12d
movl %eax, 0xc(%rsp)
cmovgl %eax, %r12d
xorl %r14d, %r14d
xorl %r13d, %r13d
cmpq %r13, %r12
je 0x2267ba
movq 0x8(%rbp), %rax
movq (%rax,%r14), %rdi
movq 0x40(%rsp), %rsi
callq 0x221779
testl %eax, %eax
je 0x2267b7
incq %r13
addq $0x20, %r14
jmp 0x226793
movl %r13d, %r12d
cmpl 0xc(%rsp), %r12d
leaq 0x60(%rsp), %r14
je 0x226a83
cmpl $0x0, 0x2c(%rsp)
je 0x226827
movq 0x10(%rsp), %rax
testb $0x40, 0x31(%rax)
jne 0x2269b6
xorl %r13d, %r13d
movq %rbp, %rax
addq $0x10, %rax
movq (%rax), %rax
testq %rax, %rax
je 0x22681e
movzwl 0x5e(%rax), %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x226818
movq 0x8(%rax), %rsi
movswl (%rsi,%rdx,2), %esi
cmpl %esi, %r12d
cmoveq %rbx, %r13
cmpl $-0x2, %esi
cmoveq %rbx, %r13
incq %rdx
jmp 0x2267f8
addq $0x28, %rax
jmp 0x2267ea
testq %r13, %r13
jne 0x226aa3
movq %r14, %rdi
callq 0x226f47
movq %rax, %r13
movq 0x20(%rsp), %rax
movq %r13, 0x18(%rax)
testq %r13, %r13
je 0x226960
movq 0x70(%rbp), %r14
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x226e9e
movl (%r14), %r8d
movl 0x4(%r14), %r9d
movq %r13, %rdi
pushq $0x2
popq %rsi
movl %eax, 0xc(%rsp)
movl %eax, %edx
movl 0x28(%rsp), %ecx
callq 0x226fc2
movslq 0x90(%r13), %rax
testq %rax, %rax
jle 0x22688f
movq 0x88(%r13), %rcx
imulq $0x18, %rax, %rax
movw $0x1, -0x16(%rcx,%rax)
movq %r13, %rdi
pushq $0x6
popq %rsi
leaq 0x14af94(%rip), %rdx    # 0x371830
callq 0x226ff9
movq %rax, %r14
movq %r13, %rdi
movq %r13, 0x48(%rsp)
movl 0xc(%rsp), %r13d
movl %r13d, %esi
callq 0x2270bc
movq 0x48(%rsp), %rsi
movq 0x10(%rsp), %rcx
cmpb $0x0, 0x61(%rcx)
jne 0x2269f3
movl %r13d, 0x4(%r14)
movl 0x38(%rbp), %eax
movl %eax, 0x8(%r14)
movl 0x28(%rsp), %eax
movl %eax, 0xc(%r14)
movq (%rbp), %rdx
movq %rsi, %r13
movq %rsi, %rdi
pushq $0x2
popq %rsi
xorl %ecx, %ecx
callq 0x2270ee
movq 0x10(%rsp), %rcx
cmpb $0x0, 0x61(%rcx)
jne 0x2269f3
cmpl $0x0, 0x2c(%rsp)
je 0x22690f
movb $0x61, 0x18(%r14)
movl 0x38(%rbp), %eax
movl %eax, 0x20(%r14)
movl 0xc(%rsp), %eax
movl %eax, 0x24(%r14)
movb $-0x3, 0x19(%r14)
movswl 0x46(%rbp), %eax
incl %eax
movl %eax, 0x28(%r14)
movswl 0x46(%rbp), %eax
movl %eax, 0x50(%r14)
andw $0x0, 0x178(%rsp)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x94(%rsp)
movq %r13, %rdi
leaq 0x60(%rsp), %r14
movq %r14, %rsi
callq 0x227155
movq 0x10(%rsp), %r13
cmpb $0x0, 0x61(%r13)
movq 0x20(%rsp), %rax
movw %r12w, 0x8(%rax)
movq %rax, %r12
movq %r13, 0x20(%rax)
movl 0x34(%rsp), %ebp
jne 0x226a0b
movq %r12, %rdi
movq 0x50(%rsp), %rsi
leaq 0x18(%rsp), %rdx
callq 0x227507
movl %eax, %ebp
movl 0x30(%rsp), %eax
cmpl $0x30, %eax
ja 0x226a0b
incl %eax
movl %eax, 0x30(%rsp)
pushq $0x11
popq %rax
cmpl $0x11, %ebp
je 0x2266da
jmp 0x226a0b
leaq 0x20(%rbp), %rax
xorl %r13d, %r13d
xorl %esi, %esi
movq (%rax), %rax
testq %rax, %rax
je 0x2267e3
movl 0x28(%rax), %ecx
testl %ecx, %ecx
cmovlel %esi, %ecx
shlq $0x4, %rcx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x2269ed
cmpl %r12d, 0x40(%rax,%rdx)
cmoveq %r15, %r13
addq $0x10, %rdx
jmp 0x2269d9
addq $0x8, %rax
jmp 0x2269bf
movq 0x20(%rsp), %rax
movw %r12w, 0x8(%rax)
movq %rax, %r12
movq %rcx, 0x20(%rax)
movl 0x34(%rsp), %ebp
movq %rcx, %r13
testl %ebp, %ebp
jne 0x226adb
xorl %ebp, %ebp
cmpb $0x0, 0x61(%r13)
jne 0x226ad6
movq 0x240(%rsp), %rax
movq %r12, (%rax)
xorl %ebp, %ebp
jmp 0x226af5
leaq 0x14f2f8(%rip), %rsi    # 0x375d31
jmp 0x226a4b
leaq 0x14f30d(%rip), %rsi    # 0x375d4f
jmp 0x226a4b
leaq 0x14f328(%rip), %rsi    # 0x375d73
leaq 0x60(%rsp), %rdi
movq 0x38(%rsp), %rdx
xorl %eax, %eax
callq 0x226dce
pushq $0x1
popq %rbp
cmpq $0x0, 0x68(%rsp)
je 0x226adb
movq %r13, %rdi
xorl %esi, %esi
callq 0x21f26b
movq 0x68(%rsp), %rax
movq %rax, 0x18(%rsp)
andq $0x0, 0x68(%rsp)
jmp 0x226adb
movq 0x10(%rsp), %r13
movq %r13, %rdi
xorl %esi, %esi
callq 0x21f26b
leaq 0x14f2ef(%rip), %rsi    # 0x375d88
movq %r13, %rdi
movq 0x40(%rsp), %rdx
jmp 0x226ac2
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x21f26b
leaq 0x14f2f0(%rip), %rsi    # 0x375da9
movq %rbx, %rdi
movq %r13, %rdx
movq %rbx, %r13
xorl %eax, %eax
callq 0x226ec3
movq %rax, 0x18(%rsp)
pushq $0x1
popq %rbp
movq 0x20(%rsp), %r12
testq %r12, %r12
je 0x226aea
movq 0x18(%r12), %rdi
testq %rdi, %rdi
je 0x226aea
callq 0x224149
movq %r13, %rdi
movq %r12, %rsi
callq 0x21f26b
movq 0x18(%rsp), %r14
testq %r14, %r14
leaq 0x1423a5(%rip), %rdx    # 0x368ea9
cmoveq %r14, %rdx
movq %r13, %rdi
movl %ebp, %esi
movq %r14, %rcx
xorl %eax, %eax
callq 0x2226f3
movq %r13, %rdi
movq %r14, %rsi
callq 0x21f26b
leaq 0x60(%rsp), %rdi
callq 0x22767e
movq %r13, %rdi
movl %ebp, %esi
callq 0x22417d
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq