// File: /workspace/github_c_makefile_star2_O1/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, %r15
movq %rdx, 0x38(%rsp)
movq %rsi, 0x58(%rsp)
movq %rdi, %r12
movq 0x240(%rsp), %rcx
xorl %eax, %eax
leaq 0x10(%rsp), %rdx
movq %rax, (%rdx)
movq %rax, (%rcx)
xorl %eax, %eax
movl %r9d, 0x30(%rsp)
testl %r9d, %r9d
setne %al
movl %eax, 0x2c(%rsp)
movl $0x38, %esi
callq 0x283569
movq %rax, 0x20(%rsp)
leaq 0x156425(%rip), %r13    # 0x3daa60
leaq 0x15d386(%rip), %rbx    # 0x3e19c8
xorl %r14d, %r14d
movl $0x0, 0x34(%rsp)
movq %r12, 0x18(%rsp)
movl $0x1a8, %edx            # imm = 0x1A8
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x173a0
movq 0x20(%rsp), %rbp
testq %rbp, %rbp
je 0x284b02
movq %r12, 0x60(%rsp)
movq 0x10(%rsp), %rsi
testq %rsi, %rsi
je 0x284688
movq %r12, %rdi
callq 0x28c3e7
movq $0x0, 0x10(%rsp)
movslq 0x28(%r12), %rax
testq %rax, %rax
jle 0x2846c4
movq 0x20(%r12), %rcx
shlq $0x5, %rax
xorl %edx, %edx
movq 0x8(%rcx,%rdx), %rsi
testq %rsi, %rsi
je 0x2846bb
movq (%rsi), %rdi
movq 0x8(%rsi), %rsi
movq %rdi, 0x8(%rsi)
addq $0x20, %rdx
cmpq %rdx, %rax
jne 0x2846a6
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
movq 0x38(%rsp), %rdx
movq 0x58(%rsp), %rcx
callq 0x284c10
testq %rax, %rax
je 0x284715
cmpl $0x0, 0x54(%rax)
movabsq $0x100000000, %r8       # imm = 0x100000000
je 0x284721
leaq 0x60(%rsp), %rdi
leaq 0x15a96a(%rip), %rsi    # 0x3df065
movq 0x38(%rsp), %rdx
xorl %eax, %eax
callq 0x284eb8
movabsq $0x100000000, %r8       # imm = 0x100000000
xorl %eax, %eax
jmp 0x284721
xorl %eax, %eax
movabsq $0x100000000, %r8       # imm = 0x100000000
testq %rax, %rax
je 0x28474e
testb $-0x80, 0x40(%rax)
je 0x284750
leaq 0x60(%rsp), %rdi
leaq 0x15a94b(%rip), %rsi    # 0x3df083
movq 0x38(%rsp), %rdx
xorl %eax, %eax
callq 0x284eb8
movabsq $0x100000000, %r8       # imm = 0x100000000
xorl %eax, %eax
testq %rax, %rax
je 0x28477e
cmpq $0x0, 0x18(%rax)
je 0x284780
leaq 0x60(%rsp), %rdi
leaq 0x15a93f(%rip), %rsi    # 0x3df0a7
movq 0x38(%rsp), %rdx
xorl %eax, %eax
callq 0x284eb8
movabsq $0x100000000, %r8       # imm = 0x100000000
xorl %eax, %eax
testq %rax, %rax
je 0x284b0c
movq %rax, 0x30(%rbp)
movq 0x20(%r12), %r9
movq 0x70(%rax), %rdx
testq %rdx, %rdx
je 0x2847be
leaq 0x18(%r9), %rsi
movabsq $-0x100000000, %rcx     # imm = 0xFFFFFFFF00000000
addq %r8, %rcx
leaq 0x20(%rsi), %rdi
cmpq %rdx, (%rsi)
movq %rdi, %rsi
jne 0x2847a9
sarq $0x20, %rcx
jmp 0x2847c5
movq $-0x8000, %rcx          # imm = 0x8000
shlq $0x5, %rcx
movq (%r9,%rcx), %rcx
movq %rcx, 0x28(%rbp)
movswl 0x46(%rax), %r11d
testl %r11d, %r11d
jle 0x284828
movq 0x8(%rax), %rcx
movl %r11d, %edx
xorl %ebp, %ebp
movq %rbp, %rsi
shlq $0x5, %rsi
movq (%rcx,%rsi), %rsi
xorl %edi, %edi
movzbl (%rsi,%rdi), %r8d
movzbl (%r15,%rdi), %r9d
cmpb %r9b, %r8b
jne 0x284807
testq %r8, %r8
jne 0x284811
jmp 0x284816
movb (%r8,%r13), %r10b
cmpb (%r9,%r13), %r10b
jne 0x284816
incq %rdi
jmp 0x2847f1
cmpb %r9b, %r8b
je 0x28482a
incq %rbp
cmpq %rdx, %rbp
jne 0x2847e4
movl %r11d, %ebp
jmp 0x28482a
xorl %ebp, %ebp
cmpl %r11d, %ebp
je 0x284b31
cmpl $0x0, 0x30(%rsp)
movq %rax, 0x48(%rsp)
je 0x2848fd
testb $0x40, 0x31(%r12)
je 0x284889
movq 0x20(%rax), %rdi
testq %rdi, %rdi
je 0x284889
xorl %r12d, %r12d
leaq 0x15a873(%rip), %rsi    # 0x3df0d1
movslq 0x28(%rdi), %rcx
testq %rcx, %rcx
jle 0x28487e
shlq $0x4, %rcx
xorl %edx, %edx
cmpl %ebp, 0x40(%rdi,%rdx)
cmoveq %rsi, %r12
addq $0x10, %rdx
cmpq %rdx, %rcx
jne 0x28486d
movq 0x8(%rdi), %rdi
testq %rdi, %rdi
jne 0x28485e
jmp 0x28488c
xorl %r12d, %r12d
movq 0x10(%rax), %rax
testq %rax, %rax
je 0x2848c3
movzwl 0x5e(%rax), %ecx
testq %rcx, %rcx
je 0x2848bd
movq 0x8(%rax), %rdx
xorl %esi, %esi
movswl (%rdx,%rsi,2), %edi
cmpl %edi, %ebp
cmoveq %rbx, %r12
cmpl $-0x2, %edi
cmoveq %rbx, %r12
incq %rsi
cmpq %rsi, %rcx
jne 0x2848a4
movq 0x28(%rax), %rax
jmp 0x284890
testq %r12, %r12
je 0x2848e9
movq 0x18(%rsp), %rdi
leaq 0x15a809(%rip), %rsi    # 0x3df0dd
movq %r12, %rdx
xorl %eax, %eax
callq 0x284f95
movq %rax, 0x10(%rsp)
movl $0x1, %r14d
movl %r14d, 0xc(%rsp)
testq %r12, %r12
movq 0x18(%rsp), %r12
je 0x284902
jmp 0x284b52
movl %r14d, 0xc(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x285019
movq 0x48(%rsp), %rcx
movq %rax, %r12
movq 0x20(%rsp), %rax
movq %r12, 0x18(%rax)
testq %r12, %r12
je 0x284aac
movq 0x70(%rcx), %rax
testq %rax, %rax
je 0x284953
movq 0x18(%rsp), %rcx
movq 0x20(%rcx), %rcx
addq $0x18, %rcx
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
incl %r14d
leaq 0x20(%rcx), %rdx
cmpq %rax, (%rcx)
movq %rdx, %rcx
jne 0x284942
jmp 0x284959
movl $0xffff8000, %r14d      # imm = 0xFFFF8000
movl (%rax), %r8d
movl 0x4(%rax), %eax
movl %eax, 0x40(%rsp)
movq %r12, %rdi
movl $0x2, %esi
movl %r14d, %edx
movl 0x2c(%rsp), %ecx
callq 0x2b200c
movq (%r12), %rcx
cmpb $0x0, 0x61(%rcx)
jne 0x28499c
movq 0x88(%r12), %rcx
cltq
leaq (%rax,%rax,2), %rax
movb $-0x3, 0x1(%rcx,%rax,8)
movl 0x40(%rsp), %edx
movl %edx, 0x10(%rcx,%rax,8)
movslq 0x90(%r12), %rax
testq %rax, %rax
jle 0x2849bc
movq 0x88(%r12), %rcx
leaq (%rax,%rax,2), %rax
movw $0x1, -0x16(%rcx,%rax,8)
movq %r12, %rdi
movl $0x6, %esi
leaq 0x1561d5(%rip), %rdx    # 0x3daba0
callq 0x2850a2
movq %rax, %rsi
movl $0x1, %eax
movl %r14d, %ecx
shll %cl, %eax
orl %eax, 0xc8(%r12)
cmpl $0x1, %r14d
je 0x284a0d
movq (%r12), %rcx
movq 0x20(%rcx), %rcx
movslq %r14d, %rdx
shlq $0x5, %rdx
movq 0x8(%rcx,%rdx), %rcx
cmpb $0x0, 0x11(%rcx)
je 0x284a0d
orl %eax, 0xcc(%r12)
movq 0x18(%rsp), %rax
cmpb $0x0, 0x61(%rax)
movq 0x48(%rsp), %rcx
jne 0x284a54
movl %r14d, 0x4(%rsi)
movl 0x38(%rcx), %eax
movl %eax, 0x8(%rsi)
movl 0x2c(%rsp), %eax
movl %eax, 0xc(%rsi)
movq (%rcx), %rdx
movq %r12, %rdi
movq %rsi, 0x40(%rsp)
movl $0x2, %esi
xorl %ecx, %ecx
callq 0x285198
movq 0x18(%rsp), %rax
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rcx
cmpb $0x0, 0x61(%rax)
jne 0x284aac
cmpl $0x0, 0x30(%rsp)
je 0x284a65
movb $0x61, 0x18(%rsi)
movl 0x38(%rcx), %eax
movl %eax, 0x20(%rsi)
movl %r14d, 0x24(%rsi)
movb $-0x3, 0x19(%rsi)
movswl 0x46(%rcx), %eax
incl %eax
movl %eax, 0x28(%rsi)
movswl 0x46(%rcx), %eax
movl %eax, 0x50(%rsi)
movw $0x0, 0x178(%rsp)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x94(%rsp)
movq %r12, %rdi
leaq 0x60(%rsp), %rsi
callq 0x285203
movq 0x20(%rsp), %rax
movw %bp, 0x8(%rax)
movq 0x18(%rsp), %r12
movq %r12, 0x20(%rax)
cmpb $0x0, 0x61(%r12)
jne 0x284b52
movq 0x20(%rsp), %rdi
movq 0x50(%rsp), %rsi
leaq 0x10(%rsp), %rdx
callq 0x2855ee
movl 0x34(%rsp), %ecx
cmpl $0x30, %ecx
movl %eax, 0xc(%rsp)
ja 0x284b52
incl %ecx
movl %ecx, 0x34(%rsp)
movl $0x11, %r14d
cmpl $0x11, %eax
je 0x284652
jmp 0x284b52
movl $0x0, 0xc(%rsp)
jmp 0x284b52
movl $0x1, 0xc(%rsp)
cmpq $0x0, 0x68(%rsp)
je 0x284b52
movq 0x68(%rsp), %rax
movq %rax, 0x10(%rsp)
movq $0x0, 0x68(%rsp)
jmp 0x284b52
leaq 0x15a584(%rip), %rsi    # 0x3df0bc
movq %r12, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x284f95
movq %rax, 0x10(%rsp)
movl $0x1, 0xc(%rsp)
movl 0xc(%rsp), %ebp
testl %ebp, %ebp
jne 0x284b62
cmpb $0x0, 0x61(%r12)
je 0x284b8c
movq 0x20(%rsp), %rbx
testq %rbx, %rbx
je 0x284b9c
movq 0x18(%rbx), %rdi
testq %rdi, %rdi
je 0x284b7a
callq 0x281916
testq %rbx, %rbx
je 0x284b9c
movq %r12, %rdi
movq %rbx, %rsi
callq 0x28c3e7
jmp 0x284b9c
movq 0x20(%rsp), %rax
movq 0x240(%rsp), %rcx
movq %rax, (%rcx)
movq 0x10(%rsp), %rbx
testq %rbx, %rbx
leaq 0x14d626(%rip), %rdx    # 0x3d21d1
cmoveq %rbx, %rdx
movq %r12, %rdi
movl %ebp, %esi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x27fc17
testq %rbx, %rbx
je 0x284bce
movq %r12, %rdi
movq %rbx, %rsi
callq 0x28c3e7
leaq 0x60(%rsp), %rdi
callq 0x285775
cmpl $0xc0a, %ebp            # imm = 0xC0A
je 0x284bef
cmpb $0x0, 0x61(%r12)
jne 0x284bef
andl 0x54(%r12), %ebp
jmp 0x284bfc
movq %r12, %rdi
callq 0x299b1a
movl $0x7, %ebp
movl %ebp, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq